FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/huffyuvdec.c
Date: 2021-09-24 20:55:06
Exec Total Coverage
Lines: 390 751 51.9%
Branches: 320 587 54.5%

Line Branch Exec Source
1 /*
2 * huffyuv decoder
3 *
4 * Copyright (c) 2002-2014 Michael Niedermayer <michaelni@gmx.at>
5 *
6 * see http://www.pcisys.net/~melanson/codecs/huffyuv.txt for a description of
7 * the algorithm used
8 *
9 * This file is part of FFmpeg.
10 *
11 * FFmpeg is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2.1 of the License, or (at your option) any later version.
15 *
16 * FFmpeg is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
20 *
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with FFmpeg; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 *
25 * yuva, gray, 4:4:4, 4:1:1, 4:1:0 and >8 bit per sample support sponsored by NOA
26 */
27
28 /**
29 * @file
30 * huffyuv decoder
31 */
32
33 #define UNCHECKED_BITSTREAM_READER 1
34
35 #include "avcodec.h"
36 #include "get_bits.h"
37 #include "huffyuv.h"
38 #include "huffyuvdsp.h"
39 #include "internal.h"
40 #include "lossless_videodsp.h"
41 #include "thread.h"
42 #include "libavutil/imgutils.h"
43 #include "libavutil/pixdesc.h"
44
45 #define classic_shift_luma_table_size 42
46 static const unsigned char classic_shift_luma[classic_shift_luma_table_size + AV_INPUT_BUFFER_PADDING_SIZE] = {
47 34, 36, 35, 69, 135, 232, 9, 16, 10, 24, 11, 23, 12, 16, 13, 10,
48 14, 8, 15, 8, 16, 8, 17, 20, 16, 10, 207, 206, 205, 236, 11, 8,
49 10, 21, 9, 23, 8, 8, 199, 70, 69, 68, 0,
50 0,0,0,0,0,0,0,0,
51 };
52
53 #define classic_shift_chroma_table_size 59
54 static const unsigned char classic_shift_chroma[classic_shift_chroma_table_size + AV_INPUT_BUFFER_PADDING_SIZE] = {
55 66, 36, 37, 38, 39, 40, 41, 75, 76, 77, 110, 239, 144, 81, 82, 83,
56 84, 85, 118, 183, 56, 57, 88, 89, 56, 89, 154, 57, 58, 57, 26, 141,
57 57, 56, 58, 57, 58, 57, 184, 119, 214, 245, 116, 83, 82, 49, 80, 79,
58 78, 77, 44, 75, 41, 40, 39, 38, 37, 36, 34, 0,
59 0,0,0,0,0,0,0,0,
60 };
61
62 static const unsigned char classic_add_luma[256] = {
63 3, 9, 5, 12, 10, 35, 32, 29, 27, 50, 48, 45, 44, 41, 39, 37,
64 73, 70, 68, 65, 64, 61, 58, 56, 53, 50, 49, 46, 44, 41, 38, 36,
65 68, 65, 63, 61, 58, 55, 53, 51, 48, 46, 45, 43, 41, 39, 38, 36,
66 35, 33, 32, 30, 29, 27, 26, 25, 48, 47, 46, 44, 43, 41, 40, 39,
67 37, 36, 35, 34, 32, 31, 30, 28, 27, 26, 24, 23, 22, 20, 19, 37,
68 35, 34, 33, 31, 30, 29, 27, 26, 24, 23, 21, 20, 18, 17, 15, 29,
69 27, 26, 24, 22, 21, 19, 17, 16, 14, 26, 25, 23, 21, 19, 18, 16,
70 15, 27, 25, 23, 21, 19, 17, 16, 14, 26, 25, 23, 21, 18, 17, 14,
71 12, 17, 19, 13, 4, 9, 2, 11, 1, 7, 8, 0, 16, 3, 14, 6,
72 12, 10, 5, 15, 18, 11, 10, 13, 15, 16, 19, 20, 22, 24, 27, 15,
73 18, 20, 22, 24, 26, 14, 17, 20, 22, 24, 27, 15, 18, 20, 23, 25,
74 28, 16, 19, 22, 25, 28, 32, 36, 21, 25, 29, 33, 38, 42, 45, 49,
75 28, 31, 34, 37, 40, 42, 44, 47, 49, 50, 52, 54, 56, 57, 59, 60,
76 62, 64, 66, 67, 69, 35, 37, 39, 40, 42, 43, 45, 47, 48, 51, 52,
77 54, 55, 57, 59, 60, 62, 63, 66, 67, 69, 71, 72, 38, 40, 42, 43,
78 46, 47, 49, 51, 26, 28, 30, 31, 33, 34, 18, 19, 11, 13, 7, 8,
79 };
80
81 static const unsigned char classic_add_chroma[256] = {
82 3, 1, 2, 2, 2, 2, 3, 3, 7, 5, 7, 5, 8, 6, 11, 9,
83 7, 13, 11, 10, 9, 8, 7, 5, 9, 7, 6, 4, 7, 5, 8, 7,
84 11, 8, 13, 11, 19, 15, 22, 23, 20, 33, 32, 28, 27, 29, 51, 77,
85 43, 45, 76, 81, 46, 82, 75, 55, 56, 144, 58, 80, 60, 74, 147, 63,
86 143, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
87 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 27, 30, 21, 22,
88 17, 14, 5, 6, 100, 54, 47, 50, 51, 53, 106, 107, 108, 109, 110, 111,
89 112, 113, 114, 115, 4, 117, 118, 92, 94, 121, 122, 3, 124, 103, 2, 1,
90 0, 129, 130, 131, 120, 119, 126, 125, 136, 137, 138, 139, 140, 141, 142, 134,
91 135, 132, 133, 104, 64, 101, 62, 57, 102, 95, 93, 59, 61, 28, 97, 96,
92 52, 49, 48, 29, 32, 25, 24, 46, 23, 98, 45, 44, 43, 20, 42, 41,
93 19, 18, 99, 40, 15, 39, 38, 16, 13, 12, 11, 37, 10, 9, 8, 36,
94 7, 128, 127, 105, 123, 116, 35, 34, 33, 145, 31, 79, 42, 146, 78, 26,
95 83, 48, 49, 50, 44, 47, 26, 31, 30, 18, 17, 19, 21, 24, 25, 13,
96 14, 16, 17, 18, 20, 21, 12, 14, 15, 9, 10, 6, 9, 6, 5, 8,
97 6, 12, 8, 10, 7, 9, 6, 4, 6, 2, 2, 3, 3, 3, 3, 2,
98 };
99
100 255 static int read_len_table(uint8_t *dst, GetBitContext *gb, int n)
101 {
102 int i, val, repeat;
103
104
2/2
✓ Branch 0 taken 9327 times.
✓ Branch 1 taken 255 times.
9582 for (i = 0; i < n;) {
105 9327 repeat = get_bits(gb, 3);
106 9327 val = get_bits(gb, 5);
107
2/2
✓ Branch 0 taken 5055 times.
✓ Branch 1 taken 4272 times.
9327 if (repeat == 0)
108 5055 repeat = get_bits(gb, 8);
109
2/4
✓ Branch 0 taken 9327 times.
✗ Branch 1 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 9327 times.
9327 if (i + repeat > n || get_bits_left(gb) < 0) {
110 av_log(NULL, AV_LOG_ERROR, "Error reading huffman table\n");
111 return AVERROR_INVALIDDATA;
112 }
113
2/2
✓ Branch 0 taken 562944 times.
✓ Branch 1 taken 9327 times.
572271 while (repeat--)
114 562944 dst[i++] = val;
115 }
116 255 return 0;
117 }
118
119 85 static int generate_joint_tables(HYuvContext *s)
120 {
121 int ret;
122 85 uint16_t *symbols = av_mallocz(5 << VLC_BITS);
123 uint16_t *bits;
124 uint8_t *len;
125
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 85 times.
85 if (!symbols)
126 return AVERROR(ENOMEM);
127 85 bits = symbols + (1 << VLC_BITS);
128 85 len = (uint8_t *)(bits + (1 << VLC_BITS));
129
130
3/4
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 69 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 16 times.
154 if (s->bitstream_bpp < 24 || s->version > 2) {
131 int p, i, y, u;
132
2/2
✓ Branch 0 taken 276 times.
✓ Branch 1 taken 69 times.
345 for (p = 0; p < 4; p++) {
133
2/2
✓ Branch 0 taken 128 times.
✓ Branch 1 taken 148 times.
276 int p0 = s->version > 2 ? p : 0;
134
2/2
✓ Branch 0 taken 734208 times.
✓ Branch 1 taken 276 times.
734484 for (i = y = 0; y < s->vlc_n; y++) {
135 734208 int len0 = s->len[p0][y];
136 734208 int limit = VLC_BITS - len0;
137
4/4
✓ Branch 0 taken 189916 times.
✓ Branch 1 taken 544292 times.
✓ Branch 2 taken 174080 times.
✓ Branch 3 taken 15836 times.
734208 if (limit <= 0 || !len0)
138 718372 continue;
139
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15836 times.
15836 if ((sign_extend(y, 8) & (s->vlc_n-1)) != y)
140 continue;
141
2/2
✓ Branch 0 taken 36015104 times.
✓ Branch 1 taken 15836 times.
36030940 for (u = 0; u < s->vlc_n; u++) {
142 36015104 int len1 = s->len[p][u];
143
4/4
✓ Branch 0 taken 692951 times.
✓ Branch 1 taken 35322153 times.
✓ Branch 2 taken 615680 times.
✓ Branch 3 taken 77271 times.
36015104 if (len1 > limit || !len1)
144 35937833 continue;
145
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 77271 times.
77271 if ((sign_extend(u, 8) & (s->vlc_n-1)) != u)
146 continue;
147
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 77271 times.
77271 av_assert0(i < (1 << VLC_BITS));
148 77271 len[i] = len0 + len1;
149 77271 bits[i] = (s->bits[p0][y] << len1) + s->bits[p][u];
150 77271 symbols[i] = (y << 8) + (u & 0xFF);
151 77271 i++;
152 }
153 }
154 276 ff_free_vlc(&s->vlc[4 + p]);
155
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 276 times.
276 if ((ret = ff_init_vlc_sparse(&s->vlc[4 + p], VLC_BITS, i, len, 1, 1,
156 bits, 2, 2, symbols, 2, 2, 0)) < 0)
157 goto out;
158 }
159 } else {
160 16 uint8_t (*map)[4] = (uint8_t(*)[4]) s->pix_bgr_map;
161 int i, b, g, r, code;
162 16 int p0 = s->decorrelate;
163 16 int p1 = !s->decorrelate;
164 /* Restrict the range to +/-16 because that's pretty much guaranteed
165 * to cover all the combinations that fit in 11 bits total, and it
166 * does not matter if we miss a few rare codes. */
167
2/2
✓ Branch 0 taken 512 times.
✓ Branch 1 taken 16 times.
528 for (i = 0, g = -16; g < 16; g++) {
168 512 int len0 = s->len[p0][g & 255];
169 512 int limit0 = VLC_BITS - len0;
170
2/4
✓ Branch 0 taken 512 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 512 times.
512 if (limit0 < 2 || !len0)
171 continue;
172
2/2
✓ Branch 0 taken 16384 times.
✓ Branch 1 taken 512 times.
16896 for (b = -16; b < 16; b++) {
173 16384 int len1 = s->len[p1][b & 255];
174 16384 int limit1 = limit0 - len1;
175
3/4
✓ Branch 0 taken 3728 times.
✓ Branch 1 taken 12656 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 3728 times.
16384 if (limit1 < 1 || !len1)
176 12656 continue;
177 3728 code = (s->bits[p0][g & 255] << len1) + s->bits[p1][b & 255];
178
2/2
✓ Branch 0 taken 119296 times.
✓ Branch 1 taken 3728 times.
123024 for (r = -16; r < 16; r++) {
179 119296 int len2 = s->len[2][r & 255];
180
3/4
✓ Branch 0 taken 10928 times.
✓ Branch 1 taken 108368 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 10928 times.
119296 if (len2 > limit1 || !len2)
181 108368 continue;
182
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10928 times.
10928 av_assert0(i < (1 << VLC_BITS));
183 10928 len[i] = len0 + len1 + len2;
184 10928 bits[i] = (code << len2) + s->bits[2][r & 255];
185
1/2
✓ Branch 0 taken 10928 times.
✗ Branch 1 not taken.
10928 if (s->decorrelate) {
186 10928 map[i][G] = g;
187 10928 map[i][B] = g + b;
188 10928 map[i][R] = g + r;
189 } else {
190 map[i][B] = g;
191 map[i][G] = b;
192 map[i][R] = r;
193 }
194 10928 i++;
195 }
196 }
197 }
198 16 ff_free_vlc(&s->vlc[4]);
199
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 16 times.
16 if ((ret = init_vlc(&s->vlc[4], VLC_BITS, i, len, 1, 1,
200 bits, 2, 2, 0)) < 0)
201 goto out;
202 }
203 85 ret = 0;
204 85 out:
205 85 av_freep(&symbols);
206 85 return ret;
207 }
208
209 85 static int read_huffman_tables(HYuvContext *s, const uint8_t *src, int length)
210 {
211 GetBitContext gb;
212 int i, ret;
213 85 int count = 3;
214
215
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 85 times.
85 if ((ret = init_get_bits(&gb, src, length * 8)) < 0)
216 return ret;
217
218
2/2
✓ Branch 0 taken 32 times.
✓ Branch 1 taken 53 times.
85 if (s->version > 2)
219 32 count = 1 + s->alpha + 2*s->chroma;
220
221
2/2
✓ Branch 0 taken 255 times.
✓ Branch 1 taken 85 times.
340 for (i = 0; i < count; i++) {
222
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 255 times.
255 if ((ret = read_len_table(s->len[i], &gb, s->vlc_n)) < 0)
223 return ret;
224
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 255 times.
255 if ((ret = ff_huffyuv_generate_bits_table(s->bits[i], s->len[i], s->vlc_n)) < 0)
225 return ret;
226 255 ff_free_vlc(&s->vlc[i]);
227
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 255 times.
255 if ((ret = init_vlc(&s->vlc[i], VLC_BITS, s->vlc_n, s->len[i], 1, 1,
228 s->bits[i], 4, 4, 0)) < 0)
229 return ret;
230 }
231
232
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 85 times.
85 if ((ret = generate_joint_tables(s)) < 0)
233 return ret;
234
235 85 return (get_bits_count(&gb) + 7) / 8;
236 }
237
238 static int read_old_huffman_tables(HYuvContext *s)
239 {
240 GetBitContext gb;
241 int i, ret;
242
243 init_get_bits(&gb, classic_shift_luma,
244 classic_shift_luma_table_size * 8);
245 if ((ret = read_len_table(s->len[0], &gb, 256)) < 0)
246 return ret;
247
248 init_get_bits(&gb, classic_shift_chroma,
249 classic_shift_chroma_table_size * 8);
250 if ((ret = read_len_table(s->len[1], &gb, 256)) < 0)
251 return ret;
252
253 for (i = 0; i < 256; i++)
254 s->bits[0][i] = classic_add_luma[i];
255 for (i = 0; i < 256; i++)
256 s->bits[1][i] = classic_add_chroma[i];
257
258 if (s->bitstream_bpp >= 24) {
259 memcpy(s->bits[1], s->bits[0], 256 * sizeof(uint32_t));
260 memcpy(s->len[1], s->len[0], 256 * sizeof(uint8_t));
261 }
262 memcpy(s->bits[2], s->bits[1], 256 * sizeof(uint32_t));
263 memcpy(s->len[2], s->len[1], 256 * sizeof(uint8_t));
264
265 for (i = 0; i < 4; i++) {
266 ff_free_vlc(&s->vlc[i]);
267 if ((ret = init_vlc(&s->vlc[i], VLC_BITS, 256, s->len[i], 1, 1,
268 s->bits[i], 4, 4, 0)) < 0)
269 return ret;
270 }
271
272 if ((ret = generate_joint_tables(s)) < 0)
273 return ret;
274
275 return 0;
276 }
277
278 85 static av_cold int decode_end(AVCodecContext *avctx)
279 {
280 85 HYuvContext *s = avctx->priv_data;
281 int i;
282
283 85 ff_huffyuv_common_end(s);
284 85 av_freep(&s->bitstream_buffer);
285
286
2/2
✓ Branch 0 taken 680 times.
✓ Branch 1 taken 85 times.
765 for (i = 0; i < 8; i++)
287 680 ff_free_vlc(&s->vlc[i]);
288
289 85 return 0;
290 }
291
292 85 static av_cold int decode_init(AVCodecContext *avctx)
293 {
294 85 HYuvContext *s = avctx->priv_data;
295 int ret;
296
297 85 ret = av_image_check_size(avctx->width, avctx->height, 0, avctx);
298
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 85 times.
85 if (ret < 0)
299 return ret;
300
301 85 ff_huffyuvdsp_init(&s->hdsp, avctx->pix_fmt);
302 85 ff_llviddsp_init(&s->llviddsp);
303 85 memset(s->vlc, 0, 4 * sizeof(VLC));
304
305 85 s->interlaced = avctx->height > 288;
306 85 s->bgr32 = 1;
307
308
1/2
✓ Branch 0 taken 85 times.
✗ Branch 1 not taken.
85 if (avctx->extradata_size) {
309
2/2
✓ Branch 0 taken 28 times.
✓ Branch 1 taken 57 times.
85 if ((avctx->bits_per_coded_sample & 7) &&
310
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 28 times.
28 avctx->bits_per_coded_sample != 12)
311 s->version = 1; // do such files exist at all?
312
3/4
✓ Branch 0 taken 85 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 53 times.
✓ Branch 3 taken 32 times.
85 else if (avctx->extradata_size > 3 && avctx->extradata[3] == 0)
313 53 s->version = 2;
314 else
315 32 s->version = 3;
316 } else
317 s->version = 0;
318
319 85 s->bps = 8;
320 85 s->n = 1<<s->bps;
321 85 s->vlc_n = FFMIN(s->n, MAX_VLC_N);
322 85 s->chroma = 1;
323
1/2
✓ Branch 0 taken 85 times.
✗ Branch 1 not taken.
85 if (s->version >= 2) {
324 int method, interlace;
325
326
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 85 times.
85 if (avctx->extradata_size < 4)
327 return AVERROR_INVALIDDATA;
328
329 85 method = avctx->extradata[0];
330 85 s->decorrelate = method & 64 ? 1 : 0;
331 85 s->predictor = method & 63;
332
2/2
✓ Branch 0 taken 53 times.
✓ Branch 1 taken 32 times.
85 if (s->version == 2) {
333 53 s->bitstream_bpp = avctx->extradata[1];
334
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 53 times.
53 if (s->bitstream_bpp == 0)
335 s->bitstream_bpp = avctx->bits_per_coded_sample & ~7;
336 } else {
337 32 s->bps = (avctx->extradata[1] >> 4) + 1;
338 32 s->n = 1<<s->bps;
339 32 s->vlc_n = FFMIN(s->n, MAX_VLC_N);
340 32 s->chroma_h_shift = avctx->extradata[1] & 3;
341 32 s->chroma_v_shift = (avctx->extradata[1] >> 2) & 3;
342 32 s->yuv = !!(avctx->extradata[2] & 1);
343 32 s->chroma= !!(avctx->extradata[2] & 3);
344 32 s->alpha = !!(avctx->extradata[2] & 4);
345 }
346 85 interlace = (avctx->extradata[2] & 0x30) >> 4;
347
2/4
✓ Branch 0 taken 85 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 85 times.
85 s->interlaced = (interlace == 1) ? 1 : (interlace == 2) ? 0 : s->interlaced;
348 85 s->context = avctx->extradata[2] & 0x40 ? 1 : 0;
349
350
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 85 times.
85 if ((ret = read_huffman_tables(s, avctx->extradata + 4,
351 85 avctx->extradata_size - 4)) < 0)
352 return ret;
353 } else {
354 switch (avctx->bits_per_coded_sample & 7) {
355 case 1:
356 s->predictor = LEFT;
357 s->decorrelate = 0;
358 break;
359 case 2:
360 s->predictor = LEFT;
361 s->decorrelate = 1;
362 break;
363 case 3:
364 s->predictor = PLANE;
365 s->decorrelate = avctx->bits_per_coded_sample >= 24;
366 break;
367 case 4:
368 s->predictor = MEDIAN;
369 s->decorrelate = 0;
370 break;
371 default:
372 s->predictor = LEFT; // OLD
373 s->decorrelate = 0;
374 break;
375 }
376 s->bitstream_bpp = avctx->bits_per_coded_sample & ~7;
377 s->context = 0;
378
379 if ((ret = read_old_huffman_tables(s)) < 0)
380 return ret;
381 }
382
383
2/2
✓ Branch 0 taken 53 times.
✓ Branch 1 taken 32 times.
85 if (s->version <= 2) {
384
4/5
✓ Branch 0 taken 28 times.
✓ Branch 1 taken 9 times.
✓ Branch 2 taken 8 times.
✓ Branch 3 taken 8 times.
✗ Branch 4 not taken.
53 switch (s->bitstream_bpp) {
385 28 case 12:
386 28 avctx->pix_fmt = AV_PIX_FMT_YUV420P;
387 28 s->yuv = 1;
388 28 break;
389 9 case 16:
390
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
9 if (s->yuy2)
391 avctx->pix_fmt = AV_PIX_FMT_YUYV422;
392 else
393 9 avctx->pix_fmt = AV_PIX_FMT_YUV422P;
394 9 s->yuv = 1;
395 9 break;
396 8 case 24:
397
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 if (s->bgr32)
398 8 avctx->pix_fmt = AV_PIX_FMT_0RGB32;
399 else
400 avctx->pix_fmt = AV_PIX_FMT_BGR24;
401 8 break;
402 8 case 32:
403
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
8 av_assert0(s->bgr32);
404 8 avctx->pix_fmt = AV_PIX_FMT_RGB32;
405 8 s->alpha = 1;
406 8 break;
407 default:
408 return AVERROR_INVALIDDATA;
409 }
410 53 av_pix_fmt_get_chroma_sub_sample(avctx->pix_fmt,
411 &s->chroma_h_shift,
412 &s->chroma_v_shift);
413 } else {
414
4/43
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✓ Branch 9 taken 8 times.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✓ Branch 14 taken 8 times.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
✓ Branch 17 taken 8 times.
✗ Branch 18 not taken.
✗ Branch 19 not taken.
✗ Branch 20 not taken.
✗ Branch 21 not taken.
✗ Branch 22 not taken.
✗ Branch 23 not taken.
✗ Branch 24 not taken.
✗ Branch 25 not taken.
✓ Branch 26 taken 8 times.
✗ Branch 27 not taken.
✗ Branch 28 not taken.
✗ Branch 29 not taken.
✗ Branch 30 not taken.
✗ Branch 31 not taken.
✗ Branch 32 not taken.
✗ Branch 33 not taken.
✗ Branch 34 not taken.
✗ Branch 35 not taken.
✗ Branch 36 not taken.
✗ Branch 37 not taken.
✗ Branch 38 not taken.
✗ Branch 39 not taken.
✗ Branch 40 not taken.
✗ Branch 41 not taken.
✗ Branch 42 not taken.
32 switch ( (s->chroma<<10) | (s->yuv<<9) | (s->alpha<<8) | ((s->bps-1)<<4) | s->chroma_h_shift | (s->chroma_v_shift<<2)) {
415 case 0x070:
416 avctx->pix_fmt = AV_PIX_FMT_GRAY8;
417 break;
418 case 0x0F0:
419 avctx->pix_fmt = AV_PIX_FMT_GRAY16;
420 break;
421 case 0x470:
422 avctx->pix_fmt = AV_PIX_FMT_GBRP;
423 break;
424 case 0x480:
425 avctx->pix_fmt = AV_PIX_FMT_GBRP9;
426 break;
427 case 0x490:
428 avctx->pix_fmt = AV_PIX_FMT_GBRP10;
429 break;
430 case 0x4B0:
431 avctx->pix_fmt = AV_PIX_FMT_GBRP12;
432 break;
433 case 0x4D0:
434 avctx->pix_fmt = AV_PIX_FMT_GBRP14;
435 break;
436 case 0x4F0:
437 avctx->pix_fmt = AV_PIX_FMT_GBRP16;
438 break;
439 case 0x570:
440 avctx->pix_fmt = AV_PIX_FMT_GBRAP;
441 break;
442 8 case 0x670:
443 8 avctx->pix_fmt = AV_PIX_FMT_YUV444P;
444 8 break;
445 case 0x680:
446 avctx->pix_fmt = AV_PIX_FMT_YUV444P9;
447 break;
448 case 0x690:
449 avctx->pix_fmt = AV_PIX_FMT_YUV444P10;
450 break;
451 case 0x6B0:
452 avctx->pix_fmt = AV_PIX_FMT_YUV444P12;
453 break;
454 case 0x6D0:
455 avctx->pix_fmt = AV_PIX_FMT_YUV444P14;
456 break;
457 8 case 0x6F0:
458 8 avctx->pix_fmt = AV_PIX_FMT_YUV444P16;
459 8 break;
460 case 0x671:
461 avctx->pix_fmt = AV_PIX_FMT_YUV422P;
462 break;
463 case 0x681:
464 avctx->pix_fmt = AV_PIX_FMT_YUV422P9;
465 break;
466 8 case 0x691:
467 8 avctx->pix_fmt = AV_PIX_FMT_YUV422P10;
468 8 break;
469 case 0x6B1:
470 avctx->pix_fmt = AV_PIX_FMT_YUV422P12;
471 break;
472 case 0x6D1:
473 avctx->pix_fmt = AV_PIX_FMT_YUV422P14;
474 break;
475 case 0x6F1:
476 avctx->pix_fmt = AV_PIX_FMT_YUV422P16;
477 break;
478 case 0x672:
479 avctx->pix_fmt = AV_PIX_FMT_YUV411P;
480 break;
481 case 0x674:
482 avctx->pix_fmt = AV_PIX_FMT_YUV440P;
483 break;
484 case 0x675:
485 avctx->pix_fmt = AV_PIX_FMT_YUV420P;
486 break;
487 case 0x685:
488 avctx->pix_fmt = AV_PIX_FMT_YUV420P9;
489 break;
490 case 0x695:
491 avctx->pix_fmt = AV_PIX_FMT_YUV420P10;
492 break;
493 8 case 0x6B5:
494 8 avctx->pix_fmt = AV_PIX_FMT_YUV420P12;
495 8 break;
496 case 0x6D5:
497 avctx->pix_fmt = AV_PIX_FMT_YUV420P14;
498 break;
499 case 0x6F5:
500 avctx->pix_fmt = AV_PIX_FMT_YUV420P16;
501 break;
502 case 0x67A:
503 avctx->pix_fmt = AV_PIX_FMT_YUV410P;
504 break;
505 case 0x770:
506 avctx->pix_fmt = AV_PIX_FMT_YUVA444P;
507 break;
508 case 0x780:
509 avctx->pix_fmt = AV_PIX_FMT_YUVA444P9;
510 break;
511 case 0x790:
512 avctx->pix_fmt = AV_PIX_FMT_YUVA444P10;
513 break;
514 case 0x7F0:
515 avctx->pix_fmt = AV_PIX_FMT_YUVA444P16;
516 break;
517 case 0x771:
518 avctx->pix_fmt = AV_PIX_FMT_YUVA422P;
519 break;
520 case 0x781:
521 avctx->pix_fmt = AV_PIX_FMT_YUVA422P9;
522 break;
523 case 0x791:
524 avctx->pix_fmt = AV_PIX_FMT_YUVA422P10;
525 break;
526 case 0x7F1:
527 avctx->pix_fmt = AV_PIX_FMT_YUVA422P16;
528 break;
529 case 0x775:
530 avctx->pix_fmt = AV_PIX_FMT_YUVA420P;
531 break;
532 case 0x785:
533 avctx->pix_fmt = AV_PIX_FMT_YUVA420P9;
534 break;
535 case 0x795:
536 avctx->pix_fmt = AV_PIX_FMT_YUVA420P10;
537 break;
538 case 0x7F5:
539 avctx->pix_fmt = AV_PIX_FMT_YUVA420P16;
540 break;
541 default:
542 return AVERROR_INVALIDDATA;
543 }
544 }
545
546 85 ff_huffyuv_common_init(avctx);
547
548
5/6
✓ Branch 0 taken 76 times.
✓ Branch 1 taken 9 times.
✓ Branch 2 taken 28 times.
✓ Branch 3 taken 48 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 37 times.
85 if ((avctx->pix_fmt == AV_PIX_FMT_YUV422P || avctx->pix_fmt == AV_PIX_FMT_YUV420P) && avctx->width & 1) {
549 av_log(avctx, AV_LOG_ERROR, "width must be even for this colorspace\n");
550 return AVERROR_INVALIDDATA;
551 }
552
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 85 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
85 if (s->predictor == MEDIAN && avctx->pix_fmt == AV_PIX_FMT_YUV422P &&
553 avctx->width % 4) {
554 av_log(avctx, AV_LOG_ERROR, "width must be a multiple of 4 "
555 "for this combination of colorspace and predictor type.\n");
556 return AVERROR_INVALIDDATA;
557 }
558
559
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 85 times.
85 if ((ret = ff_huffyuv_alloc_temp(s)) < 0)
560 return ret;
561
562 85 return 0;
563 }
564
565 /** Subset of GET_VLC for use in hand-roller VLC code */
566 #define VLC_INTERN(dst, table, gb, name, bits, max_depth) \
567 code = table[index][0]; \
568 n = table[index][1]; \
569 if (max_depth > 1 && n < 0) { \
570 LAST_SKIP_BITS(name, gb, bits); \
571 UPDATE_CACHE(name, gb); \
572 \
573 nb_bits = -n; \
574 index = SHOW_UBITS(name, gb, nb_bits) + code; \
575 code = table[index][0]; \
576 n = table[index][1]; \
577 if (max_depth > 2 && n < 0) { \
578 LAST_SKIP_BITS(name, gb, nb_bits); \
579 UPDATE_CACHE(name, gb); \
580 \
581 nb_bits = -n; \
582 index = SHOW_UBITS(name, gb, nb_bits) + code; \
583 code = table[index][0]; \
584 n = table[index][1]; \
585 } \
586 } \
587 dst = code; \
588 LAST_SKIP_BITS(name, gb, n)
589
590
591 #define GET_VLC_DUAL(dst0, dst1, name, gb, dtable, table1, table2, \
592 bits, max_depth, OP) \
593 do { \
594 unsigned int index = SHOW_UBITS(name, gb, bits); \
595 int code, n = dtable[index][1]; \
596 \
597 if (n<=0) { \
598 int nb_bits; \
599 VLC_INTERN(dst0, table1, gb, name, bits, max_depth); \
600 \
601 UPDATE_CACHE(re, gb); \
602 index = SHOW_UBITS(name, gb, bits); \
603 VLC_INTERN(dst1, table2, gb, name, bits, max_depth); \
604 } else { \
605 code = dtable[index][0]; \
606 OP(dst0, dst1, code); \
607 LAST_SKIP_BITS(name, gb, n); \
608 } \
609 } while (0)
610
611 #define OP8bits(dst0, dst1, code) dst0 = code>>8; dst1 = code
612
613 #define READ_2PIX(dst0, dst1, plane1) \
614 UPDATE_CACHE(re, &s->gb); \
615 GET_VLC_DUAL(dst0, dst1, re, &s->gb, s->vlc[4+plane1].table, \
616 s->vlc[0].table, s->vlc[plane1].table, VLC_BITS, 3, OP8bits)
617
618 70390 static void decode_422_bitstream(HYuvContext *s, int count)
619 {
620 int i, icount;
621 70390 OPEN_READER(re, &s->gb);
622 70390 count /= 2;
623
624 70390 icount = get_bits_left(&s->gb) / (32 * 4);
625
2/2
✓ Branch 0 taken 1665 times.
✓ Branch 1 taken 68725 times.
70390 if (count >= icount) {
626
2/2
✓ Branch 0 taken 145894 times.
✓ Branch 1 taken 1665 times.
147559 for (i = 0; i < icount; i++) {
627
8/10
✓ Branch 1 taken 35884 times.
✓ Branch 2 taken 110010 times.
✓ Branch 3 taken 5174 times.
✓ Branch 4 taken 30710 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 5174 times.
✓ Branch 10 taken 4241 times.
✓ Branch 11 taken 31643 times.
✗ Branch 13 not taken.
✓ Branch 14 taken 4241 times.
145894 READ_2PIX(s->temp[0][2 * i], s->temp[1][i], 1);
628
8/10
✓ Branch 1 taken 60494 times.
✓ Branch 2 taken 85400 times.
✓ Branch 3 taken 4323 times.
✓ Branch 4 taken 56171 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 4323 times.
✓ Branch 10 taken 3011 times.
✓ Branch 11 taken 57483 times.
✗ Branch 13 not taken.
✓ Branch 14 taken 3011 times.
145894 READ_2PIX(s->temp[0][2 * i + 1], s->temp[2][i], 2);
629 }
630
3/4
✓ Branch 0 taken 119540 times.
✓ Branch 1 taken 1665 times.
✓ Branch 2 taken 119540 times.
✗ Branch 3 not taken.
121205 for (; i < count && BITS_LEFT(re, &s->gb) > 0; i++) {
631
8/10
✓ Branch 1 taken 30418 times.
✓ Branch 2 taken 89122 times.
✓ Branch 3 taken 3373 times.
✓ Branch 4 taken 27045 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 3373 times.
✓ Branch 10 taken 2240 times.
✓ Branch 11 taken 28178 times.
✗ Branch 13 not taken.
✓ Branch 14 taken 2240 times.
119540 READ_2PIX(s->temp[0][2 * i ], s->temp[1][i], 1);
632
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 119540 times.
119540 if (BITS_LEFT(re, &s->gb) <= 0) break;
633
8/10
✓ Branch 1 taken 40541 times.
✓ Branch 2 taken 78999 times.
✓ Branch 3 taken 3330 times.
✓ Branch 4 taken 37211 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 3330 times.
✓ Branch 10 taken 1831 times.
✓ Branch 11 taken 38710 times.
✗ Branch 13 not taken.
✓ Branch 14 taken 1831 times.
119540 READ_2PIX(s->temp[0][2 * i + 1], s->temp[2][i], 2);
634 }
635
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1665 times.
1665 for (; i < count; i++)
636 s->temp[0][2 * i ] = s->temp[1][i] =
637 s->temp[0][2 * i + 1] = s->temp[2][i] = 0;
638 } else {
639
2/2
✓ Branch 0 taken 12276706 times.
✓ Branch 1 taken 68725 times.
12345431 for (i = 0; i < count; i++) {
640
8/10
✓ Branch 1 taken 3139971 times.
✓ Branch 2 taken 9136735 times.
✓ Branch 3 taken 424733 times.
✓ Branch 4 taken 2715238 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 424733 times.
✓ Branch 10 taken 405857 times.
✓ Branch 11 taken 2734114 times.
✗ Branch 13 not taken.
✓ Branch 14 taken 405857 times.
12276706 READ_2PIX(s->temp[0][2 * i], s->temp[1][i], 1);
641
8/10
✓ Branch 1 taken 5538372 times.
✓ Branch 2 taken 6738334 times.
✓ Branch 3 taken 393571 times.
✓ Branch 4 taken 5144801 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 393571 times.
✓ Branch 10 taken 279448 times.
✓ Branch 11 taken 5258924 times.
✗ Branch 13 not taken.
✓ Branch 14 taken 279448 times.
12276706 READ_2PIX(s->temp[0][2 * i + 1], s->temp[2][i], 2);
642 }
643 }
644 70390 CLOSE_READER(re, &s->gb);
645 70390 }
646
647 #define READ_2PIX_PLANE(dst0, dst1, plane, OP) \
648 UPDATE_CACHE(re, &s->gb); \
649 GET_VLC_DUAL(dst0, dst1, re, &s->gb, s->vlc[4+plane].table, \
650 s->vlc[plane].table, s->vlc[plane].table, VLC_BITS, 3, OP)
651
652 #define OP14bits(dst0, dst1, code) dst0 = code>>8; dst1 = sign_extend(code, 8)
653
654 /* TODO instead of restarting the read when the code isn't in the first level
655 * of the joint table, jump into the 2nd level of the individual table. */
656 #define READ_2PIX_PLANE16(dst0, dst1, plane){\
657 dst0 = get_vlc2(&s->gb, s->vlc[plane].table, VLC_BITS, 3)<<2;\
658 dst0 += get_bits(&s->gb, 2);\
659 dst1 = get_vlc2(&s->gb, s->vlc[plane].table, VLC_BITS, 3)<<2;\
660 dst1 += get_bits(&s->gb, 2);\
661 }
662 493900 static void decode_plane_bitstream(HYuvContext *s, int width, int plane)
663 {
664 493900 int i, count = width/2;
665
666
2/2
✓ Branch 0 taken 134700 times.
✓ Branch 1 taken 359200 times.
493900 if (s->bps <= 8) {
667 134700 OPEN_READER(re, &s->gb);
668
2/2
✓ Branch 1 taken 1387 times.
✓ Branch 2 taken 133313 times.
134700 if (count >= (get_bits_left(&s->gb)) / (32 * 2)) {
669
3/4
✓ Branch 0 taken 212153 times.
✓ Branch 1 taken 1387 times.
✓ Branch 2 taken 212153 times.
✗ Branch 3 not taken.
213540 for (i = 0; i < count && BITS_LEFT(re, &s->gb) > 0; i++) {
670
8/10
✓ Branch 1 taken 24131 times.
✓ Branch 2 taken 188022 times.
✓ Branch 3 taken 542 times.
✓ Branch 4 taken 23589 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 542 times.
✓ Branch 10 taken 65 times.
✓ Branch 11 taken 24066 times.
✗ Branch 13 not taken.
✓ Branch 14 taken 65 times.
212153 READ_2PIX_PLANE(s->temp[0][2 * i], s->temp[0][2 * i + 1], plane, OP8bits);
671 }
672 } else {
673
2/2
✓ Branch 0 taken 22684147 times.
✓ Branch 1 taken 133313 times.
22817460 for(i=0; i<count; i++){
674
8/10
✓ Branch 1 taken 6319215 times.
✓ Branch 2 taken 16364932 times.
✓ Branch 3 taken 350124 times.
✓ Branch 4 taken 5969091 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 350124 times.
✓ Branch 10 taken 266353 times.
✓ Branch 11 taken 6052862 times.
✗ Branch 13 not taken.
✓ Branch 14 taken 266353 times.
22684147 READ_2PIX_PLANE(s->temp[0][2 * i], s->temp[0][2 * i + 1], plane, OP8bits);
675 }
676 }
677
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 134700 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
134700 if( width&1 && BITS_LEFT(re, &s->gb)>0 ) {
678 unsigned int index;
679 int nb_bits, code, n;
680 UPDATE_CACHE(re, &s->gb);
681 index = SHOW_UBITS(re, &s->gb, VLC_BITS);
682 VLC_INTERN(s->temp[0][width-1], s->vlc[plane].table,
683 &s->gb, re, VLC_BITS, 3);
684 }
685 134700 CLOSE_READER(re, &s->gb);
686
2/2
✓ Branch 0 taken 224500 times.
✓ Branch 1 taken 134700 times.
359200 } else if (s->bps <= 14) {
687 224500 OPEN_READER(re, &s->gb);
688
2/2
✓ Branch 1 taken 1073 times.
✓ Branch 2 taken 223427 times.
224500 if (count >= (get_bits_left(&s->gb)) / (32 * 2)) {
689
3/4
✓ Branch 0 taken 82104 times.
✓ Branch 1 taken 1073 times.
✓ Branch 2 taken 82104 times.
✗ Branch 3 not taken.
83177 for (i = 0; i < count && BITS_LEFT(re, &s->gb) > 0; i++) {
690
8/10
✓ Branch 1 taken 57626 times.
✓ Branch 2 taken 24478 times.
✓ Branch 3 taken 21687 times.
✓ Branch 4 taken 35939 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 21687 times.
✓ Branch 10 taken 21105 times.
✓ Branch 11 taken 36521 times.
✗ Branch 13 not taken.
✓ Branch 14 taken 21105 times.
82104 READ_2PIX_PLANE(s->temp16[0][2 * i], s->temp16[0][2 * i + 1], plane, OP14bits);
691 }
692 } else {
693
2/2
✓ Branch 0 taken 26627696 times.
✓ Branch 1 taken 223427 times.
26851123 for(i=0; i<count; i++){
694
8/10
✓ Branch 1 taken 21066126 times.
✓ Branch 2 taken 5561570 times.
✓ Branch 3 taken 9246504 times.
✓ Branch 4 taken 11819622 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 9246504 times.
✓ Branch 10 taken 9120068 times.
✓ Branch 11 taken 11946058 times.
✗ Branch 13 not taken.
✓ Branch 14 taken 9120068 times.
26627696 READ_2PIX_PLANE(s->temp16[0][2 * i], s->temp16[0][2 * i + 1], plane, OP14bits);
695 }
696 }
697
3/4
✓ Branch 0 taken 5100 times.
✓ Branch 1 taken 219400 times.
✓ Branch 2 taken 5100 times.
✗ Branch 3 not taken.
224500 if( width&1 && BITS_LEFT(re, &s->gb)>0 ) {
698 unsigned int index;
699 int nb_bits, code, n;
700 5100 UPDATE_CACHE(re, &s->gb);
701 5100 index = SHOW_UBITS(re, &s->gb, VLC_BITS);
702
3/4
✓ Branch 0 taken 2272 times.
✓ Branch 1 taken 2828 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2272 times.
5100 VLC_INTERN(s->temp16[0][width-1], s->vlc[plane].table,
703 &s->gb, re, VLC_BITS, 3);
704 }
705 224500 CLOSE_READER(re, &s->gb);
706 } else {
707
2/2
✓ Branch 1 taken 372 times.
✓ Branch 2 taken 134328 times.
134700 if (count >= (get_bits_left(&s->gb)) / (32 * 2)) {
708
3/4
✓ Branch 0 taken 56886 times.
✓ Branch 1 taken 372 times.
✓ Branch 3 taken 56886 times.
✗ Branch 4 not taken.
57258 for (i = 0; i < count && get_bits_left(&s->gb) > 0; i++) {
709 56886 READ_2PIX_PLANE16(s->temp16[0][2 * i], s->temp16[0][2 * i + 1], plane);
710 }
711 } else {
712
2/2
✓ Branch 0 taken 22839414 times.
✓ Branch 1 taken 134328 times.
22973742 for(i=0; i<count; i++){
713 22839414 READ_2PIX_PLANE16(s->temp16[0][2 * i], s->temp16[0][2 * i + 1], plane);
714 }
715 }
716
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 134700 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
134700 if( width&1 && get_bits_left(&s->gb)>0 ) {
717 int dst = get_vlc2(&s->gb, s->vlc[plane].table, VLC_BITS, 3)<<2;
718 s->temp16[0][width-1] = dst + get_bits(&s->gb, 2);
719 }
720 }
721 493900 }
722
723 25490 static void decode_gray_bitstream(HYuvContext *s, int count)
724 {
725 int i;
726 25490 OPEN_READER(re, &s->gb);
727 25490 count /= 2;
728
729
2/2
✓ Branch 1 taken 427 times.
✓ Branch 2 taken 25063 times.
25490 if (count >= (get_bits_left(&s->gb)) / (32 * 2)) {
730
3/4
✓ Branch 0 taken 74930 times.
✓ Branch 1 taken 427 times.
✓ Branch 2 taken 74930 times.
✗ Branch 3 not taken.
75357 for (i = 0; i < count && BITS_LEFT(re, &s->gb) > 0; i++) {
731
8/10
✓ Branch 1 taken 31336 times.
✓ Branch 2 taken 43594 times.
✓ Branch 3 taken 1978 times.
✓ Branch 4 taken 29358 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 1978 times.
✓ Branch 10 taken 1840 times.
✓ Branch 11 taken 29496 times.
✗ Branch 13 not taken.
✓ Branch 14 taken 1840 times.
74930 READ_2PIX(s->temp[0][2 * i], s->temp[0][2 * i + 1], 0);
732 }
733 } else {
734
2/2
✓ Branch 0 taken 4835520 times.
✓ Branch 1 taken 25063 times.
4860583 for (i = 0; i < count; i++) {
735
8/10
✓ Branch 1 taken 2242950 times.
✓ Branch 2 taken 2592570 times.
✓ Branch 3 taken 144924 times.
✓ Branch 4 taken 2098026 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 144924 times.
✓ Branch 10 taken 134202 times.
✓ Branch 11 taken 2108748 times.
✗ Branch 13 not taken.
✓ Branch 14 taken 134202 times.
4835520 READ_2PIX(s->temp[0][2 * i], s->temp[0][2 * i + 1], 0);
736 }
737 }
738 25490 CLOSE_READER(re, &s->gb);
739 25490 }
740
741 89800 static av_always_inline void decode_bgr_1(HYuvContext *s, int count,
742 int decorrelate, int alpha)
743 {
744 int i;
745 89800 OPEN_READER(re, &s->gb);
746
747
3/4
✓ Branch 0 taken 30528000 times.
✓ Branch 1 taken 89800 times.
✓ Branch 2 taken 30528000 times.
✗ Branch 3 not taken.
30617800 for (i = 0; i < count && BITS_LEFT(re, &s->gb) > 0; i++) {
748 unsigned int index;
749 int code, n, nb_bits;
750
751 30528000 UPDATE_CACHE(re, &s->gb);
752 30528000 index = SHOW_UBITS(re, &s->gb, VLC_BITS);
753 30528000 n = s->vlc[4].table[index][1];
754
755
2/2
✓ Branch 0 taken 13538088 times.
✓ Branch 1 taken 16989912 times.
30528000 if (n>0) {
756 13538088 code = s->vlc[4].table[index][0];
757 13538088 *(uint32_t *) &s->temp[0][4 * i] = s->pix_bgr_map[code];
758 13538088 LAST_SKIP_BITS(re, &s->gb, n);
759 } else {
760
1/2
✓ Branch 0 taken 16989912 times.
✗ Branch 1 not taken.
16989912 if (decorrelate) {
761
3/4
✓ Branch 0 taken 1469318 times.
✓ Branch 1 taken 15520594 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1469318 times.
16989912 VLC_INTERN(s->temp[0][4 * i + G], s->vlc[1].table,
762 &s->gb, re, VLC_BITS, 3);
763
764 16989912 UPDATE_CACHE(re, &s->gb);
765 16989912 index = SHOW_UBITS(re, &s->gb, VLC_BITS);
766
3/4
✓ Branch 0 taken 1019554 times.
✓ Branch 1 taken 15970358 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1019554 times.
16989912 VLC_INTERN(code, s->vlc[0].table, &s->gb, re, VLC_BITS, 3);
767 16989912 s->temp[0][4 * i + B] = code + s->temp[0][4 * i + G];
768
769 16989912 UPDATE_CACHE(re, &s->gb);
770 16989912 index = SHOW_UBITS(re, &s->gb, VLC_BITS);
771
3/4
✓ Branch 0 taken 957442 times.
✓ Branch 1 taken 16032470 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 957442 times.
16989912 VLC_INTERN(code, s->vlc[2].table, &s->gb, re, VLC_BITS, 3);
772 16989912 s->temp[0][4 * i + R] = code + s->temp[0][4 * i + G];
773 } else {
774 VLC_INTERN(s->temp[0][4 * i + B], s->vlc[0].table,
775 &s->gb, re, VLC_BITS, 3);
776
777 UPDATE_CACHE(re, &s->gb);
778 index = SHOW_UBITS(re, &s->gb, VLC_BITS);
779 VLC_INTERN(s->temp[0][4 * i + G], s->vlc[1].table,
780 &s->gb, re, VLC_BITS, 3);
781
782 UPDATE_CACHE(re, &s->gb);
783 index = SHOW_UBITS(re, &s->gb, VLC_BITS);
784 VLC_INTERN(s->temp[0][4 * i + R], s->vlc[2].table,
785 &s->gb, re, VLC_BITS, 3);
786 }
787 }
788
2/2
✓ Branch 0 taken 15264000 times.
✓ Branch 1 taken 15264000 times.
30528000 if (alpha) {
789 15264000 UPDATE_CACHE(re, &s->gb);
790 15264000 index = SHOW_UBITS(re, &s->gb, VLC_BITS);
791
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 15264000 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
15264000 VLC_INTERN(s->temp[0][4 * i + A], s->vlc[2].table,
792 &s->gb, re, VLC_BITS, 3);
793 } else
794 15264000 s->temp[0][4 * i + A] = 0;
795 }
796 89800 CLOSE_READER(re, &s->gb);
797 89800 }
798
799 89800 static void decode_bgr_bitstream(HYuvContext *s, int count)
800 {
801
1/2
✓ Branch 0 taken 89800 times.
✗ Branch 1 not taken.
89800 if (s->decorrelate) {
802
2/2
✓ Branch 0 taken 44900 times.
✓ Branch 1 taken 44900 times.
89800 if (s->bitstream_bpp == 24)
803 44900 decode_bgr_1(s, count, 1, 0);
804 else
805 44900 decode_bgr_1(s, count, 1, 1);
806 } else {
807 if (s->bitstream_bpp == 24)
808 decode_bgr_1(s, count, 0, 0);
809 else
810 decode_bgr_1(s, count, 0, 1);
811 }
812 89800 }
813
814 71590 static void draw_slice(HYuvContext *s, AVFrame *frame, int y)
815 {
816 int h, cy, i;
817 int offset[AV_NUM_DATA_POINTERS];
818
819
1/2
✓ Branch 0 taken 71590 times.
✗ Branch 1 not taken.
71590 if (!s->avctx->draw_horiz_band)
820 71590 return;
821
822 h = y - s->last_slice_end;
823 y -= h;
824
825 if (s->bitstream_bpp == 12)
826 cy = y >> 1;
827 else
828 cy = y;
829
830 offset[0] = frame->linesize[0] * y;
831 offset[1] = frame->linesize[1] * cy;
832 offset[2] = frame->linesize[2] * cy;
833 for (i = 3; i < AV_NUM_DATA_POINTERS; i++)
834 offset[i] = 0;
835 emms_c();
836
837 s->avctx->draw_horiz_band(s->avctx, frame, offset, y, 3, h);
838
839 s->last_slice_end = y + h;
840 }
841
842 493900 static int left_prediction(HYuvContext *s, uint8_t *dst, const uint8_t *src, int w, int acc)
843 {
844
2/2
✓ Branch 0 taken 134700 times.
✓ Branch 1 taken 359200 times.
493900 if (s->bps <= 8) {
845 134700 return s->llviddsp.add_left_pred(dst, src, w, acc);
846 } else {
847 359200 return s->llviddsp.add_left_pred_int16(( uint16_t *)dst, (const uint16_t *)src, s->n-1, w, acc);
848 }
849 }
850
851 134100 static void add_bytes(HYuvContext *s, uint8_t *dst, uint8_t *src, int w)
852 {
853
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 134100 times.
134100 if (s->bps <= 8) {
854 s->llviddsp.add_bytes(dst, src, w);
855 } else {
856 134100 s->hdsp.add_int16((uint16_t*)dst, (const uint16_t*)src, s->n - 1, w);
857 }
858 134100 }
859
860 static void add_median_prediction(HYuvContext *s, uint8_t *dst, const uint8_t *src, const uint8_t *diff, int w, int *left, int *left_top)
861 {
862 if (s->bps <= 8) {
863 s->llviddsp.add_median_pred(dst, src, diff, w, left, left_top);
864 } else {
865 s->hdsp.add_hfyu_median_pred_int16((uint16_t *)dst, (const uint16_t *)src, (const uint16_t *)diff, s->n-1, w, left, left_top);
866 }
867 }
868
869 1610 static int decode_slice(AVCodecContext *avctx, AVFrame *p, int height,
870 int buf_size, int y_offset, int table_size)
871 {
872 1610 HYuvContext *s = avctx->priv_data;
873 int fake_ystride, fake_ustride, fake_vstride;
874 1610 const int width = s->width;
875 1610 const int width2 = s->width >> 1;
876 int ret;
877
878
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1610 times.
1610 if ((ret = init_get_bits8(&s->gb, s->bitstream_buffer + table_size, buf_size - table_size)) < 0)
879 return ret;
880
881
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1610 times.
1610 fake_ystride = s->interlaced ? p->linesize[0] * 2 : p->linesize[0];
882
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1610 times.
1610 fake_ustride = s->interlaced ? p->linesize[1] * 2 : p->linesize[1];
883
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1610 times.
1610 fake_vstride = s->interlaced ? p->linesize[2] * 2 : p->linesize[2];
884
885
2/2
✓ Branch 0 taken 800 times.
✓ Branch 1 taken 810 times.
1610 if (s->version > 2) {
886 int plane;
887
2/2
✓ Branch 0 taken 2400 times.
✓ Branch 1 taken 800 times.
3200 for(plane = 0; plane < 1 + 2*s->chroma + s->alpha; plane++) {
888 int left, lefttop, y;
889 2400 int w = width;
890 2400 int h = height;
891 2400 int fake_stride = fake_ystride;
892
893
5/6
✓ Branch 0 taken 2400 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1600 times.
✓ Branch 3 taken 800 times.
✓ Branch 4 taken 800 times.
✓ Branch 5 taken 800 times.
2400 if (s->chroma && (plane == 1 || plane == 2)) {
894 1600 w >>= s->chroma_h_shift;
895 1600 h >>= s->chroma_v_shift;
896
2/2
✓ Branch 0 taken 800 times.
✓ Branch 1 taken 800 times.
1600 fake_stride = plane == 1 ? fake_ustride : fake_vstride;
897 }
898
899
1/3
✓ Branch 0 taken 2400 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
2400 switch (s->predictor) {
900 2400 case LEFT:
901 case PLANE:
902 2400 decode_plane_bitstream(s, w, plane);
903 2400 left = left_prediction(s, p->data[plane], s->temp[0], w, 0);
904
905
2/2
✓ Branch 0 taken 491500 times.
✓ Branch 1 taken 2400 times.
493900 for (y = 1; y < h; y++) {
906 491500 uint8_t *dst = p->data[plane] + p->linesize[plane]*y;
907
908 491500 decode_plane_bitstream(s, w, plane);
909 491500 left = left_prediction(s, dst, s->temp[0], w, left);
910
2/2
✓ Branch 0 taken 134100 times.
✓ Branch 1 taken 357400 times.
491500 if (s->predictor == PLANE) {
911
1/2
✓ Branch 0 taken 134100 times.
✗ Branch 1 not taken.
134100 if (y > s->interlaced) {
912 134100 add_bytes(s, dst, dst - fake_stride, w);
913 }
914 }
915 }
916
917 2400 break;
918 case MEDIAN:
919 decode_plane_bitstream(s, w, plane);
920 left= left_prediction(s, p->data[plane], s->temp[0], w, 0);
921
922 y = 1;
923 if (y >= h)
924 break;
925
926 /* second line is left predicted for interlaced case */
927 if (s->interlaced) {
928 decode_plane_bitstream(s, w, plane);
929 left = left_prediction(s, p->data[plane] + p->linesize[plane], s->temp[0], w, left);
930 y++;
931 if (y >= h)
932 break;
933 }
934
935 lefttop = p->data[plane][0];
936 decode_plane_bitstream(s, w, plane);
937 add_median_prediction(s, p->data[plane] + fake_stride, p->data[plane], s->temp[0], w, &left, &lefttop);
938 y++;
939
940 for (; y<h; y++) {
941 uint8_t *dst;
942
943 decode_plane_bitstream(s, w, plane);
944
945 dst = p->data[plane] + p->linesize[plane] * y;
946
947 add_median_prediction(s, dst, dst - fake_stride, s->temp[0], w, &left, &lefttop);
948 }
949
950 break;
951 }
952 }
953 800 draw_slice(s, p, height);
954
2/2
✓ Branch 0 taken 410 times.
✓ Branch 1 taken 400 times.
810 } else if (s->bitstream_bpp < 24) {
955 int y, cy;
956 int lefty, leftu, leftv;
957 int lefttopy, lefttopu, lefttopv;
958
959
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 410 times.
410 if (s->yuy2) {
960 p->data[0][3] = get_bits(&s->gb, 8);
961 p->data[0][2] = get_bits(&s->gb, 8);
962 p->data[0][1] = get_bits(&s->gb, 8);
963 p->data[0][0] = get_bits(&s->gb, 8);
964
965 av_log(avctx, AV_LOG_ERROR,
966 "YUY2 output is not implemented yet\n");
967 return AVERROR_PATCHWELCOME;
968 } else {
969 410 leftv =
970 410 p->data[2][0 + y_offset * p->linesize[2]] = get_bits(&s->gb, 8);
971 410 lefty =
972 410 p->data[0][1 + y_offset * p->linesize[0]] = get_bits(&s->gb, 8);
973 410 leftu =
974 410 p->data[1][0 + y_offset * p->linesize[1]] = get_bits(&s->gb, 8);
975 410 p->data[0][0 + y_offset * p->linesize[0]] = get_bits(&s->gb, 8);
976
977
1/3
✓ Branch 0 taken 410 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
410 switch (s->predictor) {
978 410 case LEFT:
979 case PLANE:
980 410 decode_422_bitstream(s, width - 2);
981 820 lefty = s->llviddsp.add_left_pred(p->data[0] + p->linesize[0] * y_offset + 2, s->temp[0],
982 410 width - 2, lefty);
983
1/2
✓ Branch 0 taken 410 times.
✗ Branch 1 not taken.
410 if (!(s->flags & AV_CODEC_FLAG_GRAY)) {
984 410 leftu = s->llviddsp.add_left_pred(p->data[1] + p->linesize[1] * y_offset + 1, s->temp[1], width2 - 1, leftu);
985 410 leftv = s->llviddsp.add_left_pred(p->data[2] + p->linesize[2] * y_offset + 1, s->temp[2], width2 - 1, leftv);
986 }
987
988
2/2
✓ Branch 0 taken 70190 times.
✓ Branch 1 taken 200 times.
70390 for (cy = y = 1; y < height; y++, cy++) {
989 uint8_t *ydst, *udst, *vdst;
990
991
2/2
✓ Branch 0 taken 25490 times.
✓ Branch 1 taken 44700 times.
70190 if (s->bitstream_bpp == 12) {
992 25490 decode_gray_bitstream(s, width);
993
994 25490 ydst = p->data[0] + p->linesize[0] * (y + y_offset);
995
996 25490 lefty = s->llviddsp.add_left_pred(ydst, s->temp[0],
997 width, lefty);
998
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 25490 times.
25490 if (s->predictor == PLANE) {
999 if (y > s->interlaced)
1000 s->llviddsp.add_bytes(ydst, ydst - fake_ystride, width);
1001 }
1002 25490 y++;
1003
2/2
✓ Branch 0 taken 210 times.
✓ Branch 1 taken 25280 times.
25490 if (y >= height)
1004 210 break;
1005 }
1006
1007 69980 draw_slice(s, p, y);
1008
1009 69980 ydst = p->data[0] + p->linesize[0] * (y + y_offset);
1010 69980 udst = p->data[1] + p->linesize[1] * (cy + y_offset);
1011 69980 vdst = p->data[2] + p->linesize[2] * (cy + y_offset);
1012
1013 69980 decode_422_bitstream(s, width);
1014 69980 lefty = s->llviddsp.add_left_pred(ydst, s->temp[0],
1015 width, lefty);
1016
1/2
✓ Branch 0 taken 69980 times.
✗ Branch 1 not taken.
69980 if (!(s->flags & AV_CODEC_FLAG_GRAY)) {
1017 69980 leftu = s->llviddsp.add_left_pred(udst, s->temp[1], width2, leftu);
1018 69980 leftv = s->llviddsp.add_left_pred(vdst, s->temp[2], width2, leftv);
1019 }
1020
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 69980 times.
69980 if (s->predictor == PLANE) {
1021 if (cy > s->interlaced) {
1022 s->llviddsp.add_bytes(ydst, ydst - fake_ystride, width);
1023 if (!(s->flags & AV_CODEC_FLAG_GRAY)) {
1024 s->llviddsp.add_bytes(udst, udst - fake_ustride, width2);
1025 s->llviddsp.add_bytes(vdst, vdst - fake_vstride, width2);
1026 }
1027 }
1028 }
1029 }
1030 410 draw_slice(s, p, height);
1031
1032 410 break;
1033 case MEDIAN:
1034 /* first line except first 2 pixels is left predicted */
1035 decode_422_bitstream(s, width - 2);
1036 lefty = s->llviddsp.add_left_pred(p->data[0] + 2, s->temp[0],
1037 width - 2, lefty);
1038 if (!(s->flags & AV_CODEC_FLAG_GRAY)) {
1039 leftu = s->llviddsp.add_left_pred(p->data[1] + 1, s->temp[1], width2 - 1, leftu);
1040 leftv = s->llviddsp.add_left_pred(p->data[2] + 1, s->temp[2], width2 - 1, leftv);
1041 }
1042
1043 cy = y = 1;
1044 if (y >= height)
1045 break;
1046
1047 /* second line is left predicted for interlaced case */
1048 if (s->interlaced) {
1049 decode_422_bitstream(s, width);
1050 lefty = s->llviddsp.add_left_pred(p->data[0] + p->linesize[0],
1051 s->temp[0], width, lefty);
1052 if (!(s->flags & AV_CODEC_FLAG_GRAY)) {
1053 leftu = s->llviddsp.add_left_pred(p->data[1] + p->linesize[2], s->temp[1], width2, leftu);
1054 leftv = s->llviddsp.add_left_pred(p->data[2] + p->linesize[1], s->temp[2], width2, leftv);
1055 }
1056 y++;
1057 cy++;
1058 if (y >= height)
1059 break;
1060 }
1061
1062 /* next 4 pixels are left predicted too */
1063 decode_422_bitstream(s, 4);
1064 lefty = s->llviddsp.add_left_pred(p->data[0] + fake_ystride,
1065 s->temp[0], 4, lefty);
1066 if (!(s->flags & AV_CODEC_FLAG_GRAY)) {
1067 leftu = s->llviddsp.add_left_pred(p->data[1] + fake_ustride, s->temp[1], 2, leftu);
1068 leftv = s->llviddsp.add_left_pred(p->data[2] + fake_vstride, s->temp[2], 2, leftv);
1069 }
1070
1071 /* next line except the first 4 pixels is median predicted */
1072 lefttopy = p->data[0][3];
1073 decode_422_bitstream(s, width - 4);
1074 s->llviddsp.add_median_pred(p->data[0] + fake_ystride + 4,
1075 p->data[0] + 4, s->temp[0],
1076 width - 4, &lefty, &lefttopy);
1077 if (!(s->flags & AV_CODEC_FLAG_GRAY)) {
1078 lefttopu = p->data[1][1];
1079 lefttopv = p->data[2][1];
1080 s->llviddsp.add_median_pred(p->data[1] + fake_ustride + 2, p->data[1] + 2, s->temp[1], width2 - 2, &leftu, &lefttopu);
1081 s->llviddsp.add_median_pred(p->data[2] + fake_vstride + 2, p->data[2] + 2, s->temp[2], width2 - 2, &leftv, &lefttopv);
1082 }
1083 y++;
1084 cy++;
1085
1086 for (; y < height; y++, cy++) {
1087 uint8_t *ydst, *udst, *vdst;
1088
1089 if (s->bitstream_bpp == 12) {
1090 while (2 * cy > y) {
1091 decode_gray_bitstream(s, width);
1092 ydst = p->data[0] + p->linesize[0] * y;
1093 s->llviddsp.add_median_pred(ydst, ydst - fake_ystride,
1094 s->temp[0], width,
1095 &lefty, &lefttopy);
1096 y++;
1097 }
1098 if (y >= height)
1099 break;
1100 }
1101 draw_slice(s, p, y);
1102
1103 decode_422_bitstream(s, width);
1104
1105 ydst = p->data[0] + p->linesize[0] * y;
1106 udst = p->data[1] + p->linesize[1] * cy;
1107 vdst = p->data[2] + p->linesize[2] * cy;
1108
1109 s->llviddsp.add_median_pred(ydst, ydst - fake_ystride,
1110 s->temp[0], width,
1111 &lefty, &lefttopy);
1112 if (!(s->flags & AV_CODEC_FLAG_GRAY)) {
1113 s->llviddsp.add_median_pred(udst, udst - fake_ustride, s->temp[1], width2, &leftu, &lefttopu);
1114 s->llviddsp.add_median_pred(vdst, vdst - fake_vstride, s->temp[2], width2, &leftv, &lefttopv);
1115 }
1116 }
1117
1118 draw_slice(s, p, height);
1119 break;
1120 }
1121 }
1122 } else {
1123 int y;
1124 uint8_t left[4];
1125 400 const int last_line = (y_offset + height - 1) * p->linesize[0];
1126
1127
2/2
✓ Branch 0 taken 200 times.
✓ Branch 1 taken 200 times.
400 if (s->bitstream_bpp == 32) {
1128 200 left[A] = p->data[0][last_line + A] = get_bits(&s->gb, 8);
1129 200 left[R] = p->data[0][last_line + R] = get_bits(&s->gb, 8);
1130 200 left[G] = p->data[0][last_line + G] = get_bits(&s->gb, 8);
1131 200 left[B] = p->data[0][last_line + B] = get_bits(&s->gb, 8);
1132 } else {
1133 200 left[R] = p->data[0][last_line + R] = get_bits(&s->gb, 8);
1134 200 left[G] = p->data[0][last_line + G] = get_bits(&s->gb, 8);
1135 200 left[B] = p->data[0][last_line + B] = get_bits(&s->gb, 8);
1136 200 left[A] = p->data[0][last_line + A] = 255;
1137 200 skip_bits(&s->gb, 8);
1138 }
1139
1140
1/2
✓ Branch 0 taken 400 times.
✗ Branch 1 not taken.
400 if (s->bgr32) {
1141
1/2
✓ Branch 0 taken 400 times.
✗ Branch 1 not taken.
400 switch (s->predictor) {
1142 400 case LEFT:
1143 case PLANE:
1144 400 decode_bgr_bitstream(s, width - 1);
1145 400 s->hdsp.add_hfyu_left_pred_bgr32(p->data[0] + last_line + 4,
1146 400 s->temp[0], width - 1, left);
1147
1148
2/2
✓ Branch 0 taken 89400 times.
✓ Branch 1 taken 400 times.
89800 for (y = height - 2; y >= 0; y--) { // Yes it is stored upside down.
1149 89400 decode_bgr_bitstream(s, width);
1150
1151 89400 s->hdsp.add_hfyu_left_pred_bgr32(p->data[0] + p->linesize[0] * (y + y_offset),
1152 89400 s->temp[0], width, left);
1153
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 89400 times.
89400 if (s->predictor == PLANE) {
1154 if (s->bitstream_bpp != 32)
1155 left[A] = 0;
1156 if (y < height - 1 - s->interlaced) {
1157 s->llviddsp.add_bytes(p->data[0] + p->linesize[0] * (y + y_offset),
1158 p->data[0] + p->linesize[0] * (y + y_offset) +
1159 fake_ystride, 4 * width);
1160 }
1161 }
1162 }
1163 // just 1 large slice as this is not possible in reverse order
1164 400 draw_slice(s, p, height);
1165 400 break;
1166 default:
1167 av_log(avctx, AV_LOG_ERROR,
1168 "prediction type not supported!\n");
1169 }
1170 } else {
1171 av_log(avctx, AV_LOG_ERROR,
1172 "BGR24 output is not implemented yet\n");
1173 return AVERROR_PATCHWELCOME;
1174 }
1175 }
1176
1177 1610 return 0;
1178 }
1179
1180 1610 static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
1181 AVPacket *avpkt)
1182 {
1183 1610 const uint8_t *buf = avpkt->data;
1184 1610 int buf_size = avpkt->size;
1185 1610 HYuvContext *s = avctx->priv_data;
1186 1610 const int width = s->width;
1187 1610 const int height = s->height;
1188 1610 ThreadFrame frame = { .f = data };
1189 1610 AVFrame *const p = data;
1190 1610 int slice, table_size = 0, ret, nb_slices;
1191 unsigned slices_info_offset;
1192 int slice_height;
1193
1194
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1610 times.
1610 if (buf_size < (width * height + 7)/8)
1195 return AVERROR_INVALIDDATA;
1196
1197 1610 av_fast_padded_malloc(&s->bitstream_buffer,
1198 &s->bitstream_buffer_size,
1199 buf_size);
1200
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1610 times.
1610 if (!s->bitstream_buffer)
1201 return AVERROR(ENOMEM);
1202
1203 1610 s->bdsp.bswap_buf((uint32_t *) s->bitstream_buffer,
1204 (const uint32_t *) buf, buf_size / 4);
1205
1206
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1610 times.
1610 if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0)
1207 return ret;
1208
1209
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1610 times.
1610 if (s->context) {
1210 table_size = read_huffman_tables(s, s->bitstream_buffer, buf_size);
1211 if (table_size < 0)
1212 return table_size;
1213 }
1214
1215
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1610 times.
1610 if ((unsigned) (buf_size - table_size) >= INT_MAX / 8)
1216 return AVERROR_INVALIDDATA;
1217
1218 1610 s->last_slice_end = 0;
1219
1220
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 1610 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
1610 if (avctx->codec_id == AV_CODEC_ID_HYMT &&
1221 (buf_size > 32 && AV_RL32(avpkt->data + buf_size - 16) == 0)) {
1222 slices_info_offset = AV_RL32(avpkt->data + buf_size - 4);
1223 slice_height = AV_RL32(avpkt->data + buf_size - 8);
1224 nb_slices = AV_RL32(avpkt->data + buf_size - 12);
1225 if (nb_slices * 8LL + slices_info_offset > buf_size - 16 ||
1226 s->chroma_v_shift ||
1227 slice_height <= 0 || nb_slices * (uint64_t)slice_height > height)
1228 return AVERROR_INVALIDDATA;
1229 } else {
1230 1610 slice_height = height;
1231 1610 nb_slices = 1;
1232 }
1233
1234
2/2
✓ Branch 0 taken 1610 times.
✓ Branch 1 taken 1610 times.
3220 for (slice = 0; slice < nb_slices; slice++) {
1235 int y_offset, slice_offset, slice_size;
1236
1237
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1610 times.
1610 if (nb_slices > 1) {
1238 slice_offset = AV_RL32(avpkt->data + slices_info_offset + slice * 8);
1239 slice_size = AV_RL32(avpkt->data + slices_info_offset + slice * 8 + 4);
1240
1241 if (slice_offset < 0 || slice_size <= 0 || (slice_offset&3) ||
1242 slice_offset + (int64_t)slice_size > buf_size)
1243 return AVERROR_INVALIDDATA;
1244
1245 y_offset = height - (slice + 1) * slice_height;
1246 s->bdsp.bswap_buf((uint32_t *)s->bitstream_buffer,
1247 (const uint32_t *)(buf + slice_offset), slice_size / 4);
1248 } else {
1249 1610 y_offset = 0;
1250 1610 slice_offset = 0;
1251 1610 slice_size = buf_size;
1252 }
1253
1254 1610 ret = decode_slice(avctx, p, slice_height, slice_size, y_offset, table_size);
1255 1610 emms_c();
1256
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1610 times.
1610 if (ret < 0)
1257 return ret;
1258 }
1259
1260 1610 *got_frame = 1;
1261
1262 1610 return (get_bits_count(&s->gb) + 31) / 32 * 4 + table_size;
1263 }
1264
1265 const AVCodec ff_huffyuv_decoder = {
1266 .name = "huffyuv",
1267 .long_name = NULL_IF_CONFIG_SMALL("Huffyuv / HuffYUV"),
1268 .type = AVMEDIA_TYPE_VIDEO,
1269 .id = AV_CODEC_ID_HUFFYUV,
1270 .priv_data_size = sizeof(HYuvContext),
1271 .init = decode_init,
1272 .close = decode_end,
1273 .decode = decode_frame,
1274 .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DRAW_HORIZ_BAND |
1275 AV_CODEC_CAP_FRAME_THREADS,
1276 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_INIT_CLEANUP,
1277 };
1278
1279 #if CONFIG_FFVHUFF_DECODER
1280 const AVCodec ff_ffvhuff_decoder = {
1281 .name = "ffvhuff",
1282 .long_name = NULL_IF_CONFIG_SMALL("Huffyuv FFmpeg variant"),
1283 .type = AVMEDIA_TYPE_VIDEO,
1284 .id = AV_CODEC_ID_FFVHUFF,
1285 .priv_data_size = sizeof(HYuvContext),
1286 .init = decode_init,
1287 .close = decode_end,
1288 .decode = decode_frame,
1289 .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DRAW_HORIZ_BAND |
1290 AV_CODEC_CAP_FRAME_THREADS,
1291 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_INIT_CLEANUP,
1292 };
1293 #endif /* CONFIG_FFVHUFF_DECODER */
1294
1295 #if CONFIG_HYMT_DECODER
1296 const AVCodec ff_hymt_decoder = {
1297 .name = "hymt",
1298 .long_name = NULL_IF_CONFIG_SMALL("HuffYUV MT"),
1299 .type = AVMEDIA_TYPE_VIDEO,
1300 .id = AV_CODEC_ID_HYMT,
1301 .priv_data_size = sizeof(HYuvContext),
1302 .init = decode_init,
1303 .close = decode_end,
1304 .decode = decode_frame,
1305 .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DRAW_HORIZ_BAND |
1306 AV_CODEC_CAP_FRAME_THREADS,
1307 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_INIT_CLEANUP,
1308 };
1309 #endif /* CONFIG_HYMT_DECODER */
1310