FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/ffv1dec.c
Date: 2022-11-28 23:49:43
Exec Total Coverage
Lines: 401 689 58.2%
Branches: 266 589 45.2%

Line Branch Exec Source
1 /*
2 * FFV1 decoder
3 *
4 * Copyright (c) 2003-2013 Michael Niedermayer <michaelni@gmx.at>
5 *
6 * This file is part of FFmpeg.
7 *
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23 /**
24 * @file
25 * FF Video Codec 1 (a lossless codec) decoder
26 */
27
28 #include "libavutil/avassert.h"
29 #include "libavutil/crc.h"
30 #include "libavutil/opt.h"
31 #include "libavutil/imgutils.h"
32 #include "libavutil/pixdesc.h"
33 #include "avcodec.h"
34 #include "codec_internal.h"
35 #include "get_bits.h"
36 #include "rangecoder.h"
37 #include "golomb.h"
38 #include "mathops.h"
39 #include "ffv1.h"
40 #include "thread.h"
41 #include "threadframe.h"
42
43 153603750 static inline av_flatten int get_symbol_inline(RangeCoder *c, uint8_t *state,
44 int is_signed)
45 {
46
2/2
✓ Branch 1 taken 60923828 times.
✓ Branch 2 taken 92679922 times.
153603750 if (get_rac(c, state + 0))
47 60923828 return 0;
48 else {
49 int i, e;
50 unsigned a;
51 92679922 e = 0;
52
4/4
✓ Branch 0 taken 586933039 times.
✓ Branch 1 taken 45940109 times.
✓ Branch 3 taken 540193226 times.
✓ Branch 4 taken 92679922 times.
632873148 while (get_rac(c, state + 1 + FFMIN(e, 9))) { // 1..10
53 540193226 e++;
54
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 540193226 times.
540193226 if (e > 31)
55 return AVERROR_INVALIDDATA;
56 }
57
58 92679922 a = 1;
59
2/2
✓ Branch 0 taken 540193226 times.
✓ Branch 1 taken 92679922 times.
632873148 for (i = e - 1; i >= 0; i--)
60
2/2
✓ Branch 0 taken 519679329 times.
✓ Branch 1 taken 20513897 times.
540193226 a += a + get_rac(c, state + 22 + FFMIN(i, 9)); // 22..31
61
62
6/6
✓ Branch 0 taken 92658298 times.
✓ Branch 1 taken 21624 times.
✓ Branch 2 taken 81255906 times.
✓ Branch 3 taken 11402392 times.
✓ Branch 5 taken 49217065 times.
✓ Branch 6 taken 43441233 times.
92679922 e = -(is_signed && get_rac(c, state + 11 + FFMIN(e, 10))); // 11..21
63 92679922 return (a ^ e) - e;
64 }
65 }
66
67 2000214 static av_noinline int get_symbol(RangeCoder *c, uint8_t *state, int is_signed)
68 {
69 2000214 return get_symbol_inline(c, state, is_signed);
70 }
71
72 113342102 static inline int get_vlc_symbol(GetBitContext *gb, VlcState *const state,
73 int bits)
74 {
75 int k, i, v, ret;
76
77 113342102 i = state->count;
78 113342102 k = 0;
79
2/2
✓ Branch 0 taken 188397450 times.
✓ Branch 1 taken 113342102 times.
301739552 while (i < state->error_sum) { // FIXME: optimize
80 188397450 k++;
81 188397450 i += i;
82 }
83
84 113342102 v = get_sr_golomb(gb, k, 12, bits);
85 ff_dlog(NULL, "v:%d bias:%d error:%d drift:%d count:%d k:%d",
86 v, state->bias, state->error_sum, state->drift, state->count, k);
87
88 113342102 v ^= ((2 * state->drift + state->count) >> 31);
89
90 113342102 ret = fold(v + state->bias, bits);
91
92 113342102 update_vlc_state(state, v);
93
94 113342102 return ret;
95 }
96
97 3501456 static int is_input_end(FFV1Context *s)
98 {
99
2/2
✓ Branch 0 taken 2032800 times.
✓ Branch 1 taken 1468656 times.
3501456 if (s->ac != AC_GOLOMB_RICE) {
100 2032800 RangeCoder *const c = &s->c;
101
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2032800 times.
2032800 if (c->overread > MAX_OVERREAD)
102 return AVERROR_INVALIDDATA;
103 } else {
104
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1468656 times.
1468656 if (get_bits_left(&s->gb) < 1)
105 return AVERROR_INVALIDDATA;
106 }
107 3501456 return 0;
108 }
109
110 #define TYPE int16_t
111 #define RENAME(name) name
112 #include "ffv1dec_template.c"
113 #undef TYPE
114 #undef RENAME
115
116 #define TYPE int32_t
117 #define RENAME(name) name ## 32
118 #include "ffv1dec_template.c"
119
120 12888 static int decode_plane(FFV1Context *s, uint8_t *src,
121 int w, int h, int stride, int plane_index,
122 int pixel_stride)
123 {
124 int x, y;
125 int16_t *sample[2];
126 12888 sample[0] = s->sample_buffer + 3;
127 12888 sample[1] = s->sample_buffer + w + 6 + 3;
128
129 12888 s->run_index = 0;
130
131 12888 memset(s->sample_buffer, 0, 2 * (w + 6) * sizeof(*s->sample_buffer));
132
133
2/2
✓ Branch 0 taken 1201152 times.
✓ Branch 1 taken 12888 times.
1214040 for (y = 0; y < h; y++) {
134 1201152 int16_t *temp = sample[0]; // FIXME: try a normal buffer
135
136 1201152 sample[0] = sample[1];
137 1201152 sample[1] = temp;
138
139 1201152 sample[1][-1] = sample[0][0];
140 1201152 sample[0][w] = sample[0][w - 1];
141
142
2/2
✓ Branch 0 taken 642936 times.
✓ Branch 1 taken 558216 times.
1201152 if (s->avctx->bits_per_raw_sample <= 8) {
143 642936 int ret = decode_line(s, w, sample, plane_index, 8);
144
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 642936 times.
642936 if (ret < 0)
145 return ret;
146
2/2
✓ Branch 0 taken 93579678 times.
✓ Branch 1 taken 642936 times.
94222614 for (x = 0; x < w; x++)
147 93579678 src[x*pixel_stride + stride * y] = sample[1][x];
148 } else {
149 558216 int ret = decode_line(s, w, sample, plane_index, s->avctx->bits_per_raw_sample);
150
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 558216 times.
558216 if (ret < 0)
151 return ret;
152
1/2
✓ Branch 0 taken 558216 times.
✗ Branch 1 not taken.
558216 if (s->packed_at_lsb) {
153
2/2
✓ Branch 0 taken 81537288 times.
✓ Branch 1 taken 558216 times.
82095504 for (x = 0; x < w; x++) {
154 81537288 ((uint16_t*)(src + stride*y))[x*pixel_stride] = sample[1][x];
155 }
156 } else {
157 for (x = 0; x < w; x++) {
158 ((uint16_t*)(src + stride*y))[x*pixel_stride] = sample[1][x] << (16 - s->avctx->bits_per_raw_sample) | ((uint16_t **)sample)[1][x] >> (2 * s->avctx->bits_per_raw_sample - 16);
159 }
160 }
161 }
162 }
163 12888 return 0;
164 }
165
166 5724 static int decode_slice_header(const FFV1Context *f, FFV1Context *fs)
167 {
168 5724 RangeCoder *c = &fs->c;
169 uint8_t state[CONTEXT_SIZE];
170 unsigned ps, i, context_count;
171 int sx, sy, sw, sh;
172
173 5724 memset(state, 128, sizeof(state));
174 5724 sx = get_symbol(c, state, 0);
175 5724 sy = get_symbol(c, state, 0);
176 5724 sw = get_symbol(c, state, 0) + 1U;
177 5724 sh = get_symbol(c, state, 0) + 1U;
178
179
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5724 times.
5724 av_assert0(f->version > 2);
180
181
182
4/8
✓ Branch 0 taken 5724 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5724 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 5724 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 5724 times.
5724 if (sx < 0 || sy < 0 || sw <= 0 || sh <= 0)
183 return AVERROR_INVALIDDATA;
184
2/4
✓ Branch 0 taken 5724 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 5724 times.
5724 if (sx > f->num_h_slices - sw || sy > f->num_v_slices - sh)
185 return AVERROR_INVALIDDATA;
186
187 5724 fs->slice_x = sx * (int64_t)f->width / f->num_h_slices;
188 5724 fs->slice_y = sy * (int64_t)f->height / f->num_v_slices;
189 5724 fs->slice_width = (sx + sw) * (int64_t)f->width / f->num_h_slices - fs->slice_x;
190 5724 fs->slice_height = (sy + sh) * (int64_t)f->height / f->num_v_slices - fs->slice_y;
191
192
2/4
✓ Branch 0 taken 5724 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 5724 times.
5724 av_assert0((unsigned)fs->slice_width <= f->width &&
193 (unsigned)fs->slice_height <= f->height);
194
2/4
✓ Branch 0 taken 5724 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 5724 times.
5724 av_assert0 ( (unsigned)fs->slice_x + (uint64_t)fs->slice_width <= f->width
195 && (unsigned)fs->slice_y + (uint64_t)fs->slice_height <= f->height);
196
197
3/4
✓ Branch 0 taken 2452 times.
✓ Branch 1 taken 3272 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2452 times.
5724 if (fs->ac == AC_GOLOMB_RICE && fs->slice_width >= (1<<23))
198 return AVERROR_INVALIDDATA;
199
200
2/2
✓ Branch 0 taken 11448 times.
✓ Branch 1 taken 5724 times.
17172 for (i = 0; i < f->plane_count; i++) {
201 11448 PlaneContext * const p = &fs->plane[i];
202 11448 int idx = get_symbol(c, state, 0);
203
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11448 times.
11448 if (idx >= (unsigned)f->quant_table_count) {
204 av_log(f->avctx, AV_LOG_ERROR, "quant_table_index out of range\n");
205 return -1;
206 }
207 11448 p->quant_table_index = idx;
208 11448 memcpy(p->quant_table, f->quant_tables[idx], sizeof(p->quant_table));
209 11448 context_count = f->context_count[idx];
210
211
2/2
✓ Branch 0 taken 472 times.
✓ Branch 1 taken 10976 times.
11448 if (p->context_count < context_count) {
212 472 av_freep(&p->state);
213 472 av_freep(&p->vlc_state);
214 }
215 11448 p->context_count = context_count;
216 }
217
218 5724 ps = get_symbol(c, state, 0);
219
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5724 times.
5724 if (ps == 1) {
220 f->cur->interlaced_frame = 1;
221 f->cur->top_field_first = 1;
222
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5724 times.
5724 } else if (ps == 2) {
223 f->cur->interlaced_frame = 1;
224 f->cur->top_field_first = 0;
225
1/2
✓ Branch 0 taken 5724 times.
✗ Branch 1 not taken.
5724 } else if (ps == 3) {
226 5724 f->cur->interlaced_frame = 0;
227 }
228 5724 f->cur->sample_aspect_ratio.num = get_symbol(c, state, 0);
229 5724 f->cur->sample_aspect_ratio.den = get_symbol(c, state, 0);
230
231
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5724 times.
5724 if (av_image_check_sar(f->width, f->height,
232 5724 f->cur->sample_aspect_ratio) < 0) {
233 av_log(f->avctx, AV_LOG_WARNING, "ignoring invalid SAR: %u/%u\n",
234 f->cur->sample_aspect_ratio.num,
235 f->cur->sample_aspect_ratio.den);
236 f->cur->sample_aspect_ratio = (AVRational){ 0, 1 };
237 }
238
239
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5724 times.
5724 if (fs->version > 3) {
240 fs->slice_reset_contexts = get_rac(c, state);
241 fs->slice_coding_mode = get_symbol(c, state, 0);
242 if (fs->slice_coding_mode != 1) {
243 fs->slice_rct_by_coef = get_symbol(c, state, 0);
244 fs->slice_rct_ry_coef = get_symbol(c, state, 0);
245 if ((uint64_t)fs->slice_rct_by_coef + (uint64_t)fs->slice_rct_ry_coef > 4) {
246 av_log(f->avctx, AV_LOG_ERROR, "slice_rct_y_coef out of range\n");
247 return AVERROR_INVALIDDATA;
248 }
249 }
250 }
251
252 5724 return 0;
253 }
254
255 5928 static int decode_slice(AVCodecContext *c, void *arg)
256 {
257 5928 FFV1Context *fs = *(void **)arg;
258 5928 FFV1Context *f = fs->avctx->priv_data;
259 int width, height, x, y, ret;
260 5928 const int ps = av_pix_fmt_desc_get(c->pix_fmt)->comp[0].step;
261 5928 AVFrame * const p = f->cur;
262 int i, si;
263
264
2/2
✓ Branch 0 taken 8586 times.
✓ Branch 1 taken 5928 times.
14514 for( si=0; fs != f->slice_context[si]; si ++)
265 ;
266
267
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 5928 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
5928 if(f->fsrc && !p->key_frame)
268 ff_thread_await_progress(&f->last_picture, si, 0);
269
270
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 5928 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
5928 if(f->fsrc && !p->key_frame) {
271 FFV1Context *fssrc = f->fsrc->slice_context[si];
272 FFV1Context *fsdst = f->slice_context[si];
273 av_assert1(fsdst->plane_count == fssrc->plane_count);
274 av_assert1(fsdst == fs);
275
276 if (!p->key_frame)
277 fsdst->slice_damaged |= fssrc->slice_damaged;
278
279 for (i = 0; i < f->plane_count; i++) {
280 PlaneContext *psrc = &fssrc->plane[i];
281 PlaneContext *pdst = &fsdst->plane[i];
282
283 av_free(pdst->state);
284 av_free(pdst->vlc_state);
285 memcpy(pdst, psrc, sizeof(*pdst));
286 pdst->state = NULL;
287 pdst->vlc_state = NULL;
288
289 if (fssrc->ac) {
290 pdst->state = av_malloc_array(CONTEXT_SIZE, psrc->context_count);
291 memcpy(pdst->state, psrc->state, CONTEXT_SIZE * psrc->context_count);
292 } else {
293 pdst->vlc_state = av_malloc_array(sizeof(*pdst->vlc_state), psrc->context_count);
294 memcpy(pdst->vlc_state, psrc->vlc_state, sizeof(*pdst->vlc_state) * psrc->context_count);
295 }
296 }
297 }
298
299 5928 fs->slice_rct_by_coef = 1;
300 5928 fs->slice_rct_ry_coef = 1;
301
302
2/2
✓ Branch 0 taken 5724 times.
✓ Branch 1 taken 204 times.
5928 if (f->version > 2) {
303
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 5724 times.
5724 if (ff_ffv1_init_slice_state(f, fs) < 0)
304 return AVERROR(ENOMEM);
305
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 5724 times.
5724 if (decode_slice_header(f, fs) < 0) {
306 fs->slice_x = fs->slice_y = fs->slice_height = fs->slice_width = 0;
307 fs->slice_damaged = 1;
308 return AVERROR_INVALIDDATA;
309 }
310 }
311
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 5928 times.
5928 if ((ret = ff_ffv1_init_slice_state(f, fs)) < 0)
312 return ret;
313
3/4
✓ Branch 0 taken 5220 times.
✓ Branch 1 taken 708 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 5220 times.
5928 if (f->cur->key_frame || fs->slice_reset_contexts) {
314 708 ff_ffv1_clear_slice_state(f, fs);
315
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5220 times.
5220 } else if (fs->slice_damaged) {
316 return AVERROR_INVALIDDATA;
317 }
318
319 5928 width = fs->slice_width;
320 5928 height = fs->slice_height;
321 5928 x = fs->slice_x;
322 5928 y = fs->slice_y;
323
324
2/2
✓ Branch 0 taken 2656 times.
✓ Branch 1 taken 3272 times.
5928 if (fs->ac == AC_GOLOMB_RICE) {
325
4/6
✓ Branch 0 taken 2452 times.
✓ Branch 1 taken 204 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2452 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 204 times.
2656 if (f->version == 3 && f->micro_version > 1 || f->version > 3)
326 2452 get_rac(&fs->c, (uint8_t[]) { 129 });
327
4/6
✓ Branch 0 taken 204 times.
✓ Branch 1 taken 2452 times.
✓ Branch 2 taken 204 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 204 times.
✗ Branch 5 not taken.
2656 fs->ac_byte_count = f->version > 2 || (!x && !y) ? fs->c.bytestream - fs->c.bytestream_start - 1 : 0;
328 2656 init_get_bits(&fs->gb,
329 2656 fs->c.bytestream_start + fs->ac_byte_count,
330 2656 (fs->c.bytestream_end - fs->c.bytestream_start - fs->ac_byte_count) * 8);
331 }
332
333 av_assert1(width && height);
334
3/6
✓ Branch 0 taken 4296 times.
✓ Branch 1 taken 1632 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 4296 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
10224 if (f->colorspace == 0 && (f->chroma_planes || !fs->transparency)) {
335 4296 const int chroma_width = AV_CEIL_RSHIFT(width, f->chroma_h_shift);
336 4296 const int chroma_height = AV_CEIL_RSHIFT(height, f->chroma_v_shift);
337 4296 const int cx = x >> f->chroma_h_shift;
338 4296 const int cy = y >> f->chroma_v_shift;
339 4296 decode_plane(fs, p->data[0] + ps*x + y*p->linesize[0], width, height, p->linesize[0], 0, 1);
340
341
1/2
✓ Branch 0 taken 4296 times.
✗ Branch 1 not taken.
4296 if (f->chroma_planes) {
342 4296 decode_plane(fs, p->data[1] + ps*cx+cy*p->linesize[1], chroma_width, chroma_height, p->linesize[1], 1, 1);
343 4296 decode_plane(fs, p->data[2] + ps*cx+cy*p->linesize[2], chroma_width, chroma_height, p->linesize[2], 1, 1);
344 }
345
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4296 times.
4296 if (fs->transparency)
346 decode_plane(fs, p->data[3] + ps*x + y*p->linesize[3], width, height, p->linesize[3], (f->version >= 4 && !f->chroma_planes) ? 1 : 2, 1);
347
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1632 times.
1632 } else if (f->colorspace == 0) {
348 decode_plane(fs, p->data[0] + ps*x + y*p->linesize[0] , width, height, p->linesize[0], 0, 2);
349 decode_plane(fs, p->data[0] + ps*x + y*p->linesize[0] + 1, width, height, p->linesize[0], 1, 2);
350
2/2
✓ Branch 0 taken 816 times.
✓ Branch 1 taken 816 times.
1632 } else if (f->use32bit) {
351 816 uint8_t *planes[4] = { p->data[0] + ps * x + y * p->linesize[0],
352 816 p->data[1] + ps * x + y * p->linesize[1],
353 816 p->data[2] + ps * x + y * p->linesize[2],
354 816 p->data[3] + ps * x + y * p->linesize[3] };
355 816 decode_rgb_frame32(fs, planes, width, height, p->linesize);
356 } else {
357 816 uint8_t *planes[4] = { p->data[0] + ps * x + y * p->linesize[0],
358 816 p->data[1] + ps * x + y * p->linesize[1],
359 816 p->data[2] + ps * x + y * p->linesize[2],
360 816 p->data[3] + ps * x + y * p->linesize[3] };
361 816 decode_rgb_frame(fs, planes, width, height, p->linesize);
362 }
363
3/4
✓ Branch 0 taken 3272 times.
✓ Branch 1 taken 2656 times.
✓ Branch 2 taken 3272 times.
✗ Branch 3 not taken.
5928 if (fs->ac != AC_GOLOMB_RICE && f->version > 2) {
364 int v;
365 3272 get_rac(&fs->c, (uint8_t[]) { 129 });
366 3272 v = fs->c.bytestream_end - fs->c.bytestream - 2 - 5*f->ec;
367
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3272 times.
3272 if (v) {
368 av_log(f->avctx, AV_LOG_ERROR, "bytestream end mismatching by %d\n", v);
369 fs->slice_damaged = 1;
370 }
371 }
372
373 5928 ff_thread_report_progress(&f->picture, si, 0);
374
375 5928 return 0;
376 }
377
378 720 static int read_quant_table(RangeCoder *c, int16_t *quant_table, int scale)
379 {
380 int v;
381 720 int i = 0;
382 uint8_t state[CONTEXT_SIZE];
383
384 720 memset(state, 128, sizeof(state));
385
386
2/2
✓ Branch 0 taken 2805 times.
✓ Branch 1 taken 720 times.
3525 for (v = 0; i < 128; v++) {
387 2805 unsigned len = get_symbol(c, state, 0) + 1U;
388
389
2/4
✓ Branch 0 taken 2805 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2805 times.
2805 if (len > 128 - i || !len)
390 return AVERROR_INVALIDDATA;
391
392
2/2
✓ Branch 0 taken 92160 times.
✓ Branch 1 taken 2805 times.
94965 while (len--) {
393 92160 quant_table[i] = scale * v;
394 92160 i++;
395 }
396 }
397
398
2/2
✓ Branch 0 taken 91440 times.
✓ Branch 1 taken 720 times.
92160 for (i = 1; i < 128; i++)
399 91440 quant_table[256 - i] = -quant_table[i];
400 720 quant_table[128] = -quant_table[127];
401
402 720 return 2 * v - 1;
403 }
404
405 144 static int read_quant_tables(RangeCoder *c,
406 int16_t quant_table[MAX_CONTEXT_INPUTS][256])
407 {
408 int i;
409 144 int context_count = 1;
410
411
2/2
✓ Branch 0 taken 720 times.
✓ Branch 1 taken 144 times.
864 for (i = 0; i < 5; i++) {
412 720 int ret = read_quant_table(c, quant_table[i], context_count);
413
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 720 times.
720 if (ret < 0)
414 return ret;
415 720 context_count *= ret;
416
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 720 times.
720 if (context_count > 32768U) {
417 return AVERROR_INVALIDDATA;
418 }
419 }
420 144 return (context_count + 1) / 2;
421 }
422
423 60 static int read_extra_header(FFV1Context *f)
424 {
425 60 RangeCoder *const c = &f->c;
426 uint8_t state[CONTEXT_SIZE];
427 int i, j, k, ret;
428 uint8_t state2[32][CONTEXT_SIZE];
429 60 unsigned crc = 0;
430
431 60 memset(state2, 128, sizeof(state2));
432 60 memset(state, 128, sizeof(state));
433
434 60 ff_init_range_decoder(c, f->avctx->extradata, f->avctx->extradata_size);
435 60 ff_build_rac_states(c, 0.05 * (1LL << 32), 256 - 8);
436
437 60 f->version = get_symbol(c, state, 0);
438
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 60 times.
60 if (f->version < 2) {
439 av_log(f->avctx, AV_LOG_ERROR, "Invalid version in global header\n");
440 return AVERROR_INVALIDDATA;
441 }
442
1/2
✓ Branch 0 taken 60 times.
✗ Branch 1 not taken.
60 if (f->version > 2) {
443 60 c->bytestream_end -= 4;
444 60 f->micro_version = get_symbol(c, state, 0);
445
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 60 times.
60 if (f->micro_version < 0)
446 return AVERROR_INVALIDDATA;
447 }
448 60 f->ac = get_symbol(c, state, 0);
449
450
2/2
✓ Branch 0 taken 35 times.
✓ Branch 1 taken 25 times.
60 if (f->ac == AC_RANGE_CUSTOM_TAB) {
451
2/2
✓ Branch 0 taken 8925 times.
✓ Branch 1 taken 35 times.
8960 for (i = 1; i < 256; i++)
452 8925 f->state_transition[i] = get_symbol(c, state, 1) + c->one_state[i];
453 }
454
455 60 f->colorspace = get_symbol(c, state, 0); //YUV cs type
456 60 f->avctx->bits_per_raw_sample = get_symbol(c, state, 0);
457 60 f->chroma_planes = get_rac(c, state);
458 60 f->chroma_h_shift = get_symbol(c, state, 0);
459 60 f->chroma_v_shift = get_symbol(c, state, 0);
460 60 f->transparency = get_rac(c, state);
461
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 60 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
60 f->plane_count = 1 + (f->chroma_planes || f->version<4) + f->transparency;
462 60 f->num_h_slices = 1 + get_symbol(c, state, 0);
463 60 f->num_v_slices = 1 + get_symbol(c, state, 0);
464
465
2/4
✓ Branch 0 taken 60 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 60 times.
60 if (f->chroma_h_shift > 4U || f->chroma_v_shift > 4U) {
466 av_log(f->avctx, AV_LOG_ERROR, "chroma shift parameters %d %d are invalid\n",
467 f->chroma_h_shift, f->chroma_v_shift);
468 return AVERROR_INVALIDDATA;
469 }
470
471
2/4
✓ Branch 0 taken 60 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 60 times.
✗ Branch 3 not taken.
60 if (f->num_h_slices > (unsigned)f->width || !f->num_h_slices ||
472
2/4
✓ Branch 0 taken 60 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 60 times.
60 f->num_v_slices > (unsigned)f->height || !f->num_v_slices
473 ) {
474 av_log(f->avctx, AV_LOG_ERROR, "slice count invalid\n");
475 return AVERROR_INVALIDDATA;
476 }
477
478 60 f->quant_table_count = get_symbol(c, state, 0);
479
2/4
✓ Branch 0 taken 60 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 60 times.
60 if (f->quant_table_count > (unsigned)MAX_QUANT_TABLES || !f->quant_table_count) {
480 av_log(f->avctx, AV_LOG_ERROR, "quant table count %d is invalid\n", f->quant_table_count);
481 f->quant_table_count = 0;
482 return AVERROR_INVALIDDATA;
483 }
484
485
2/2
✓ Branch 0 taken 120 times.
✓ Branch 1 taken 60 times.
180 for (i = 0; i < f->quant_table_count; i++) {
486 120 f->context_count[i] = read_quant_tables(c, f->quant_tables[i]);
487
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 120 times.
120 if (f->context_count[i] < 0) {
488 av_log(f->avctx, AV_LOG_ERROR, "read_quant_table error\n");
489 return AVERROR_INVALIDDATA;
490 }
491 }
492
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 60 times.
60 if ((ret = ff_ffv1_allocate_initial_states(f)) < 0)
493 return ret;
494
495
2/2
✓ Branch 0 taken 120 times.
✓ Branch 1 taken 60 times.
180 for (i = 0; i < f->quant_table_count; i++)
496
2/2
✓ Branch 1 taken 8 times.
✓ Branch 2 taken 112 times.
120 if (get_rac(c, state)) {
497
2/2
✓ Branch 0 taken 60504 times.
✓ Branch 1 taken 8 times.
60512 for (j = 0; j < f->context_count[i]; j++)
498
2/2
✓ Branch 0 taken 1936128 times.
✓ Branch 1 taken 60504 times.
1996632 for (k = 0; k < CONTEXT_SIZE; k++) {
499
2/2
✓ Branch 0 taken 1935872 times.
✓ Branch 1 taken 256 times.
1936128 int pred = j ? f->initial_states[i][j - 1][k] : 128;
500 1936128 f->initial_states[i][j][k] =
501 1936128 (pred + get_symbol(c, state2[k], 1)) & 0xFF;
502 }
503 }
504
505
1/2
✓ Branch 0 taken 60 times.
✗ Branch 1 not taken.
60 if (f->version > 2) {
506 60 f->ec = get_symbol(c, state, 0);
507
1/2
✓ Branch 0 taken 60 times.
✗ Branch 1 not taken.
60 if (f->micro_version > 2)
508 60 f->intra = get_symbol(c, state, 0);
509 }
510
511
1/2
✓ Branch 0 taken 60 times.
✗ Branch 1 not taken.
60 if (f->version > 2) {
512 unsigned v;
513 120 v = av_crc(av_crc_get_table(AV_CRC_32_IEEE), 0,
514 60 f->avctx->extradata, f->avctx->extradata_size);
515
2/4
✓ Branch 0 taken 60 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 60 times.
60 if (v || f->avctx->extradata_size < 4) {
516 av_log(f->avctx, AV_LOG_ERROR, "CRC mismatch %X!\n", v);
517 return AVERROR_INVALIDDATA;
518 }
519 60 crc = AV_RB32(f->avctx->extradata + f->avctx->extradata_size - 4);
520 }
521
522
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 60 times.
60 if (f->avctx->debug & FF_DEBUG_PICT_INFO)
523 av_log(f->avctx, AV_LOG_DEBUG,
524 "global: ver:%d.%d, coder:%d, colorspace: %d bpr:%d chroma:%d(%d:%d), alpha:%d slices:%dx%d qtabs:%d ec:%d intra:%d CRC:0x%08X\n",
525 f->version, f->micro_version,
526 f->ac,
527 f->colorspace,
528 f->avctx->bits_per_raw_sample,
529 f->chroma_planes, f->chroma_h_shift, f->chroma_v_shift,
530 f->transparency,
531 f->num_h_slices, f->num_v_slices,
532 f->quant_table_count,
533 f->ec,
534 f->intra,
535 crc
536 );
537 60 return 0;
538 }
539
540 195 static int read_header(FFV1Context *f)
541 {
542 uint8_t state[CONTEXT_SIZE];
543 195 int i, j, context_count = -1; //-1 to avoid warning
544 195 RangeCoder *const c = &f->slice_context[0]->c;
545
546 195 memset(state, 128, sizeof(state));
547
548
2/2
✓ Branch 0 taken 24 times.
✓ Branch 1 taken 171 times.
195 if (f->version < 2) {
549 int chroma_planes, chroma_h_shift, chroma_v_shift, transparency, colorspace, bits_per_raw_sample;
550 24 unsigned v= get_symbol(c, state, 0);
551
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
24 if (v >= 2) {
552 av_log(f->avctx, AV_LOG_ERROR, "invalid version %d in ver01 header\n", v);
553 return AVERROR_INVALIDDATA;
554 }
555 24 f->version = v;
556 24 f->ac = get_symbol(c, state, 0);
557
558
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
24 if (f->ac == AC_RANGE_CUSTOM_TAB) {
559 for (i = 1; i < 256; i++) {
560 int st = get_symbol(c, state, 1) + c->one_state[i];
561 if (st < 1 || st > 255) {
562 av_log(f->avctx, AV_LOG_ERROR, "invalid state transition %d\n", st);
563 return AVERROR_INVALIDDATA;
564 }
565 f->state_transition[i] = st;
566 }
567 }
568
569 24 colorspace = get_symbol(c, state, 0); //YUV cs type
570
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
24 bits_per_raw_sample = f->version > 0 ? get_symbol(c, state, 0) : f->avctx->bits_per_raw_sample;
571 24 chroma_planes = get_rac(c, state);
572 24 chroma_h_shift = get_symbol(c, state, 0);
573 24 chroma_v_shift = get_symbol(c, state, 0);
574 24 transparency = get_rac(c, state);
575
2/4
✓ Branch 0 taken 24 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 24 times.
24 if (colorspace == 0 && f->avctx->skip_alpha)
576 transparency = 0;
577
578
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 8 times.
24 if (f->plane_count) {
579
1/2
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
16 if (colorspace != f->colorspace ||
580
1/2
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
16 bits_per_raw_sample != f->avctx->bits_per_raw_sample ||
581
1/2
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
16 chroma_planes != f->chroma_planes ||
582
1/2
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
16 chroma_h_shift != f->chroma_h_shift ||
583
1/2
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
16 chroma_v_shift != f->chroma_v_shift ||
584
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16 times.
16 transparency != f->transparency) {
585 av_log(f->avctx, AV_LOG_ERROR, "Invalid change of global parameters\n");
586 return AVERROR_INVALIDDATA;
587 }
588 }
589
590
2/4
✓ Branch 0 taken 24 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 24 times.
24 if (chroma_h_shift > 4U || chroma_v_shift > 4U) {
591 av_log(f->avctx, AV_LOG_ERROR, "chroma shift parameters %d %d are invalid\n",
592 chroma_h_shift, chroma_v_shift);
593 return AVERROR_INVALIDDATA;
594 }
595
596 24 f->colorspace = colorspace;
597 24 f->avctx->bits_per_raw_sample = bits_per_raw_sample;
598 24 f->chroma_planes = chroma_planes;
599 24 f->chroma_h_shift = chroma_h_shift;
600 24 f->chroma_v_shift = chroma_v_shift;
601 24 f->transparency = transparency;
602
603 24 f->plane_count = 2 + f->transparency;
604 }
605
606
2/2
✓ Branch 0 taken 147 times.
✓ Branch 1 taken 48 times.
195 if (f->colorspace == 0) {
607
2/4
✓ Branch 0 taken 147 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 147 times.
147 if (!f->transparency && !f->chroma_planes) {
608 if (f->avctx->bits_per_raw_sample <= 8)
609 f->avctx->pix_fmt = AV_PIX_FMT_GRAY8;
610 else if (f->avctx->bits_per_raw_sample == 9) {
611 f->packed_at_lsb = 1;
612 f->avctx->pix_fmt = AV_PIX_FMT_GRAY9;
613 } else if (f->avctx->bits_per_raw_sample == 10) {
614 f->packed_at_lsb = 1;
615 f->avctx->pix_fmt = AV_PIX_FMT_GRAY10;
616 } else if (f->avctx->bits_per_raw_sample == 12) {
617 f->packed_at_lsb = 1;
618 f->avctx->pix_fmt = AV_PIX_FMT_GRAY12;
619 } else if (f->avctx->bits_per_raw_sample == 16) {
620 f->packed_at_lsb = 1;
621 f->avctx->pix_fmt = AV_PIX_FMT_GRAY16;
622 } else if (f->avctx->bits_per_raw_sample < 16) {
623 f->avctx->pix_fmt = AV_PIX_FMT_GRAY16;
624 } else
625 return AVERROR(ENOSYS);
626
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 147 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
147 } else if (f->transparency && !f->chroma_planes) {
627 if (f->avctx->bits_per_raw_sample <= 8)
628 f->avctx->pix_fmt = AV_PIX_FMT_YA8;
629 else
630 return AVERROR(ENOSYS);
631
3/4
✓ Branch 0 taken 97 times.
✓ Branch 1 taken 50 times.
✓ Branch 2 taken 97 times.
✗ Branch 3 not taken.
147 } else if (f->avctx->bits_per_raw_sample<=8 && !f->transparency) {
632
1/7
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 97 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
97 switch(16 * f->chroma_h_shift + f->chroma_v_shift) {
633 case 0x00: f->avctx->pix_fmt = AV_PIX_FMT_YUV444P; break;
634 case 0x01: f->avctx->pix_fmt = AV_PIX_FMT_YUV440P; break;
635 case 0x10: f->avctx->pix_fmt = AV_PIX_FMT_YUV422P; break;
636 97 case 0x11: f->avctx->pix_fmt = AV_PIX_FMT_YUV420P; break;
637 case 0x20: f->avctx->pix_fmt = AV_PIX_FMT_YUV411P; break;
638 case 0x22: f->avctx->pix_fmt = AV_PIX_FMT_YUV410P; break;
639 }
640
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 50 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
50 } else if (f->avctx->bits_per_raw_sample <= 8 && f->transparency) {
641 switch(16*f->chroma_h_shift + f->chroma_v_shift) {
642 case 0x00: f->avctx->pix_fmt = AV_PIX_FMT_YUVA444P; break;
643 case 0x10: f->avctx->pix_fmt = AV_PIX_FMT_YUVA422P; break;
644 case 0x11: f->avctx->pix_fmt = AV_PIX_FMT_YUVA420P; break;
645 }
646
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 50 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
50 } else if (f->avctx->bits_per_raw_sample == 9 && !f->transparency) {
647 f->packed_at_lsb = 1;
648 switch(16 * f->chroma_h_shift + f->chroma_v_shift) {
649 case 0x00: f->avctx->pix_fmt = AV_PIX_FMT_YUV444P9; break;
650 case 0x10: f->avctx->pix_fmt = AV_PIX_FMT_YUV422P9; break;
651 case 0x11: f->avctx->pix_fmt = AV_PIX_FMT_YUV420P9; break;
652 }
653
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 50 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
50 } else if (f->avctx->bits_per_raw_sample == 9 && f->transparency) {
654 f->packed_at_lsb = 1;
655 switch(16 * f->chroma_h_shift + f->chroma_v_shift) {
656 case 0x00: f->avctx->pix_fmt = AV_PIX_FMT_YUVA444P9; break;
657 case 0x10: f->avctx->pix_fmt = AV_PIX_FMT_YUVA422P9; break;
658 case 0x11: f->avctx->pix_fmt = AV_PIX_FMT_YUVA420P9; break;
659 }
660
3/4
✓ Branch 0 taken 26 times.
✓ Branch 1 taken 24 times.
✓ Branch 2 taken 26 times.
✗ Branch 3 not taken.
50 } else if (f->avctx->bits_per_raw_sample == 10 && !f->transparency) {
661 26 f->packed_at_lsb = 1;
662
1/5
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✓ Branch 2 taken 26 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
26 switch(16 * f->chroma_h_shift + f->chroma_v_shift) {
663 case 0x00: f->avctx->pix_fmt = AV_PIX_FMT_YUV444P10; break;
664 case 0x01: f->avctx->pix_fmt = AV_PIX_FMT_YUV440P10; break;
665 26 case 0x10: f->avctx->pix_fmt = AV_PIX_FMT_YUV422P10; break;
666 case 0x11: f->avctx->pix_fmt = AV_PIX_FMT_YUV420P10; break;
667 }
668
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
24 } else if (f->avctx->bits_per_raw_sample == 10 && f->transparency) {
669 f->packed_at_lsb = 1;
670 switch(16 * f->chroma_h_shift + f->chroma_v_shift) {
671 case 0x00: f->avctx->pix_fmt = AV_PIX_FMT_YUVA444P10; break;
672 case 0x10: f->avctx->pix_fmt = AV_PIX_FMT_YUVA422P10; break;
673 case 0x11: f->avctx->pix_fmt = AV_PIX_FMT_YUVA420P10; break;
674 }
675
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
24 } else if (f->avctx->bits_per_raw_sample == 12 && !f->transparency) {
676 f->packed_at_lsb = 1;
677 switch(16 * f->chroma_h_shift + f->chroma_v_shift) {
678 case 0x00: f->avctx->pix_fmt = AV_PIX_FMT_YUV444P12; break;
679 case 0x01: f->avctx->pix_fmt = AV_PIX_FMT_YUV440P12; break;
680 case 0x10: f->avctx->pix_fmt = AV_PIX_FMT_YUV422P12; break;
681 case 0x11: f->avctx->pix_fmt = AV_PIX_FMT_YUV420P12; break;
682 }
683
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
24 } else if (f->avctx->bits_per_raw_sample == 14 && !f->transparency) {
684 f->packed_at_lsb = 1;
685 switch(16 * f->chroma_h_shift + f->chroma_v_shift) {
686 case 0x00: f->avctx->pix_fmt = AV_PIX_FMT_YUV444P14; break;
687 case 0x10: f->avctx->pix_fmt = AV_PIX_FMT_YUV422P14; break;
688 case 0x11: f->avctx->pix_fmt = AV_PIX_FMT_YUV420P14; break;
689 }
690
2/4
✓ Branch 0 taken 24 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 24 times.
✗ Branch 3 not taken.
24 } else if (f->avctx->bits_per_raw_sample == 16 && !f->transparency){
691 24 f->packed_at_lsb = 1;
692
1/4
✓ Branch 0 taken 24 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
24 switch(16 * f->chroma_h_shift + f->chroma_v_shift) {
693 24 case 0x00: f->avctx->pix_fmt = AV_PIX_FMT_YUV444P16; break;
694 case 0x10: f->avctx->pix_fmt = AV_PIX_FMT_YUV422P16; break;
695 case 0x11: f->avctx->pix_fmt = AV_PIX_FMT_YUV420P16; break;
696 }
697 } else if (f->avctx->bits_per_raw_sample == 16 && f->transparency){
698 f->packed_at_lsb = 1;
699 switch(16 * f->chroma_h_shift + f->chroma_v_shift) {
700 case 0x00: f->avctx->pix_fmt = AV_PIX_FMT_YUVA444P16; break;
701 case 0x10: f->avctx->pix_fmt = AV_PIX_FMT_YUVA422P16; break;
702 case 0x11: f->avctx->pix_fmt = AV_PIX_FMT_YUVA420P16; break;
703 }
704 }
705
1/2
✓ Branch 0 taken 48 times.
✗ Branch 1 not taken.
48 } else if (f->colorspace == 1) {
706
2/4
✓ Branch 0 taken 48 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 48 times.
48 if (f->chroma_h_shift || f->chroma_v_shift) {
707 av_log(f->avctx, AV_LOG_ERROR,
708 "chroma subsampling not supported in this colorspace\n");
709 return AVERROR(ENOSYS);
710 }
711
3/4
✓ Branch 0 taken 24 times.
✓ Branch 1 taken 24 times.
✓ Branch 2 taken 24 times.
✗ Branch 3 not taken.
48 if ( f->avctx->bits_per_raw_sample <= 8 && !f->transparency)
712 24 f->avctx->pix_fmt = AV_PIX_FMT_0RGB32;
713
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
24 else if (f->avctx->bits_per_raw_sample <= 8 && f->transparency)
714 f->avctx->pix_fmt = AV_PIX_FMT_RGB32;
715
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
24 else if (f->avctx->bits_per_raw_sample == 9 && !f->transparency)
716 f->avctx->pix_fmt = AV_PIX_FMT_GBRP9;
717
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
24 else if (f->avctx->bits_per_raw_sample == 10 && !f->transparency)
718 f->avctx->pix_fmt = AV_PIX_FMT_GBRP10;
719
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
24 else if (f->avctx->bits_per_raw_sample == 10 && f->transparency)
720 f->avctx->pix_fmt = AV_PIX_FMT_GBRAP10;
721
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
24 else if (f->avctx->bits_per_raw_sample == 12 && !f->transparency)
722 f->avctx->pix_fmt = AV_PIX_FMT_GBRP12;
723
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
24 else if (f->avctx->bits_per_raw_sample == 12 && f->transparency)
724 f->avctx->pix_fmt = AV_PIX_FMT_GBRAP12;
725
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
24 else if (f->avctx->bits_per_raw_sample == 14 && !f->transparency)
726 f->avctx->pix_fmt = AV_PIX_FMT_GBRP14;
727
2/4
✓ Branch 0 taken 24 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 24 times.
✗ Branch 3 not taken.
24 else if (f->avctx->bits_per_raw_sample == 16 && !f->transparency) {
728 24 f->avctx->pix_fmt = AV_PIX_FMT_GBRP16;
729 24 f->use32bit = 1;
730 }
731 else if (f->avctx->bits_per_raw_sample == 16 && f->transparency) {
732 f->avctx->pix_fmt = AV_PIX_FMT_GBRAP16;
733 f->use32bit = 1;
734 }
735 } else {
736 av_log(f->avctx, AV_LOG_ERROR, "colorspace not supported\n");
737 return AVERROR(ENOSYS);
738 }
739
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 195 times.
195 if (f->avctx->pix_fmt == AV_PIX_FMT_NONE) {
740 av_log(f->avctx, AV_LOG_ERROR, "format not supported\n");
741 return AVERROR(ENOSYS);
742 }
743
744 ff_dlog(f->avctx, "%d %d %d\n",
745 f->chroma_h_shift, f->chroma_v_shift, f->avctx->pix_fmt);
746
2/2
✓ Branch 0 taken 24 times.
✓ Branch 1 taken 171 times.
195 if (f->version < 2) {
747 24 context_count = read_quant_tables(c, f->quant_table);
748
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
24 if (context_count < 0) {
749 av_log(f->avctx, AV_LOG_ERROR, "read_quant_table error\n");
750 return AVERROR_INVALIDDATA;
751 }
752 24 f->slice_count = f->max_slice_count;
753
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 171 times.
171 } else if (f->version < 3) {
754 f->slice_count = get_symbol(c, state, 0);
755 } else {
756 171 const uint8_t *p = c->bytestream_end;
757 171 for (f->slice_count = 0;
758
4/6
✓ Branch 0 taken 855 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 855 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 684 times.
✓ Branch 5 taken 171 times.
855 f->slice_count < MAX_SLICES && 3 + 5*!!f->ec < p - c->bytestream_start;
759 684 f->slice_count++) {
760
1/2
✓ Branch 0 taken 684 times.
✗ Branch 1 not taken.
684 int trailer = 3 + 5*!!f->ec;
761 684 int size = AV_RB24(p-trailer);
762
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 684 times.
684 if (size + trailer > p - c->bytestream_start)
763 break;
764 684 p -= size + trailer;
765 }
766 }
767
3/6
✓ Branch 0 taken 195 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 195 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 195 times.
195 if (f->slice_count > (unsigned)MAX_SLICES || f->slice_count <= 0 || f->slice_count > f->max_slice_count) {
768 av_log(f->avctx, AV_LOG_ERROR, "slice count %d is invalid (max=%d)\n", f->slice_count, f->max_slice_count);
769 return AVERROR_INVALIDDATA;
770 }
771
772
2/2
✓ Branch 0 taken 708 times.
✓ Branch 1 taken 195 times.
903 for (j = 0; j < f->slice_count; j++) {
773 708 FFV1Context *fs = f->slice_context[j];
774 708 fs->ac = f->ac;
775 708 fs->packed_at_lsb = f->packed_at_lsb;
776
777 708 fs->slice_damaged = 0;
778
779
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 708 times.
708 if (f->version == 2) {
780 int sx = get_symbol(c, state, 0);
781 int sy = get_symbol(c, state, 0);
782 int sw = get_symbol(c, state, 0) + 1U;
783 int sh = get_symbol(c, state, 0) + 1U;
784
785 if (sx < 0 || sy < 0 || sw <= 0 || sh <= 0)
786 return AVERROR_INVALIDDATA;
787 if (sx > f->num_h_slices - sw || sy > f->num_v_slices - sh)
788 return AVERROR_INVALIDDATA;
789
790 fs->slice_x = sx * (int64_t)f->width / f->num_h_slices;
791 fs->slice_y = sy * (int64_t)f->height / f->num_v_slices;
792 fs->slice_width = (sx + sw) * (int64_t)f->width / f->num_h_slices - fs->slice_x;
793 fs->slice_height = (sy + sh) * (int64_t)f->height / f->num_v_slices - fs->slice_y;
794
795 av_assert0((unsigned)fs->slice_width <= f->width &&
796 (unsigned)fs->slice_height <= f->height);
797 av_assert0 ( (unsigned)fs->slice_x + (uint64_t)fs->slice_width <= f->width
798 && (unsigned)fs->slice_y + (uint64_t)fs->slice_height <= f->height);
799 }
800
801
2/2
✓ Branch 0 taken 1416 times.
✓ Branch 1 taken 708 times.
2124 for (i = 0; i < f->plane_count; i++) {
802 1416 PlaneContext *const p = &fs->plane[i];
803
804
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1416 times.
1416 if (f->version == 2) {
805 int idx = get_symbol(c, state, 0);
806 if (idx >= (unsigned)f->quant_table_count) {
807 av_log(f->avctx, AV_LOG_ERROR,
808 "quant_table_index out of range\n");
809 return AVERROR_INVALIDDATA;
810 }
811 p->quant_table_index = idx;
812 memcpy(p->quant_table, f->quant_tables[idx],
813 sizeof(p->quant_table));
814 context_count = f->context_count[idx];
815 } else {
816 1416 memcpy(p->quant_table, f->quant_table, sizeof(p->quant_table));
817 }
818
819
2/2
✓ Branch 0 taken 48 times.
✓ Branch 1 taken 1368 times.
1416 if (f->version <= 2) {
820
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 48 times.
48 av_assert0(context_count >= 0);
821
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 32 times.
48 if (p->context_count < context_count) {
822 16 av_freep(&p->state);
823 16 av_freep(&p->vlc_state);
824 }
825 48 p->context_count = context_count;
826 }
827 }
828 }
829 195 return 0;
830 }
831
832 68 static av_cold int decode_init(AVCodecContext *avctx)
833 {
834 68 FFV1Context *f = avctx->priv_data;
835 int ret;
836
837
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 68 times.
68 if ((ret = ff_ffv1_common_init(avctx)) < 0)
838 return ret;
839
840 68 f->picture.f = av_frame_alloc();
841 68 f->last_picture.f = av_frame_alloc();
842
2/4
✓ Branch 0 taken 68 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 68 times.
68 if (!f->picture.f || !f->last_picture.f)
843 return AVERROR(ENOMEM);
844
845
3/4
✓ Branch 0 taken 60 times.
✓ Branch 1 taken 8 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 60 times.
68 if (avctx->extradata_size > 0 && (ret = read_extra_header(f)) < 0)
846 return ret;
847
848
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 68 times.
68 if ((ret = ff_ffv1_init_slice_contexts(f)) < 0)
849 return ret;
850
851 68 return 0;
852 }
853
854 1635 static int decode_frame(AVCodecContext *avctx, AVFrame *rframe,
855 int *got_frame, AVPacket *avpkt)
856 {
857 1635 uint8_t *buf = avpkt->data;
858 1635 int buf_size = avpkt->size;
859 1635 FFV1Context *f = avctx->priv_data;
860 1635 RangeCoder *const c = &f->slice_context[0]->c;
861 int i, ret;
862 1635 uint8_t keystate = 128;
863 uint8_t *buf_p;
864 AVFrame *p;
865
866
1/2
✓ Branch 0 taken 1635 times.
✗ Branch 1 not taken.
1635 if (f->last_picture.f)
867 1635 ff_thread_release_ext_buffer(avctx, &f->last_picture);
868 1635 FFSWAP(ThreadFrame, f->picture, f->last_picture);
869
870 1635 f->cur = p = f->picture.f;
871
872
3/4
✓ Branch 0 taken 204 times.
✓ Branch 1 taken 1431 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 204 times.
1635 if (f->version < 3 && avctx->field_order > AV_FIELD_PROGRESSIVE) {
873 /* we have interlaced material flagged in container */
874 p->interlaced_frame = 1;
875 if (avctx->field_order == AV_FIELD_TT || avctx->field_order == AV_FIELD_TB)
876 p->top_field_first = 1;
877 }
878
879 1635 f->avctx = avctx;
880 1635 ff_init_range_decoder(c, buf, buf_size);
881 1635 ff_build_rac_states(c, 0.05 * (1LL << 32), 256 - 8);
882
883 1635 p->pict_type = AV_PICTURE_TYPE_I; //FIXME I vs. P
884
2/2
✓ Branch 1 taken 195 times.
✓ Branch 2 taken 1440 times.
1635 if (get_rac(c, &keystate)) {
885 195 p->key_frame = 1;
886 195 f->key_frame_ok = 0;
887
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 195 times.
195 if ((ret = read_header(f)) < 0)
888 return ret;
889 195 f->key_frame_ok = 1;
890 } else {
891
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1440 times.
1440 if (!f->key_frame_ok) {
892 av_log(avctx, AV_LOG_ERROR,
893 "Cannot decode non-keyframe without valid keyframe\n");
894 return AVERROR_INVALIDDATA;
895 }
896 1440 p->key_frame = 0;
897 }
898
899
2/2
✓ Branch 0 taken 818 times.
✓ Branch 1 taken 817 times.
1635 if (f->ac != AC_GOLOMB_RICE) {
900
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 818 times.
818 if (buf_size < avctx->width * avctx->height / (128*8))
901 return AVERROR_INVALIDDATA;
902 } else {
903 817 int w = avctx->width;
904 817 int s = 1 + w / (1<<23);
905
906 817 w /= s;
907
908
2/2
✓ Branch 0 taken 18589 times.
✓ Branch 1 taken 817 times.
19406 for (i = 0; w > (1<<ff_log2_run[i]); i++)
909 18589 w -= ff_log2_run[i];
910
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 817 times.
817 if (buf_size < (avctx->height + i + 6) / 8 * s)
911 return AVERROR_INVALIDDATA;
912 }
913
914 1635 ret = ff_thread_get_ext_buffer(avctx, &f->picture, AV_GET_BUFFER_FLAG_REF);
915
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1635 times.
1635 if (ret < 0)
916 return ret;
917
918
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1635 times.
1635 if (avctx->debug & FF_DEBUG_PICT_INFO)
919 av_log(avctx, AV_LOG_DEBUG, "ver:%d keyframe:%d coder:%d ec:%d slices:%d bps:%d\n",
920 f->version, p->key_frame, f->ac, f->ec, f->slice_count, f->avctx->bits_per_raw_sample);
921
922 1635 ff_thread_finish_setup(avctx);
923
924 1635 buf_p = buf + buf_size;
925
2/2
✓ Branch 0 taken 5928 times.
✓ Branch 1 taken 1635 times.
7563 for (i = f->slice_count - 1; i >= 0; i--) {
926 5928 FFV1Context *fs = f->slice_context[i];
927
2/2
✓ Branch 0 taken 5724 times.
✓ Branch 1 taken 204 times.
5928 int trailer = 3 + 5*!!f->ec;
928 int v;
929
930
4/4
✓ Branch 0 taken 1635 times.
✓ Branch 1 taken 4293 times.
✓ Branch 2 taken 1431 times.
✓ Branch 3 taken 204 times.
5928 if (i || f->version > 2) {
931
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5724 times.
5724 if (trailer > buf_p - buf) v = INT_MAX;
932 5724 else v = AV_RB24(buf_p-trailer) + trailer;
933 204 } else v = buf_p - c->bytestream_start;
934
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5928 times.
5928 if (buf_p - c->bytestream_start < v) {
935 av_log(avctx, AV_LOG_ERROR, "Slice pointer chain broken\n");
936 ff_thread_report_progress(&f->picture, INT_MAX, 0);
937 return AVERROR_INVALIDDATA;
938 }
939 5928 buf_p -= v;
940
941
2/2
✓ Branch 0 taken 5724 times.
✓ Branch 1 taken 204 times.
5928 if (f->ec) {
942 5724 unsigned crc = av_crc(av_crc_get_table(AV_CRC_32_IEEE), 0, buf_p, v);
943
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5724 times.
5724 if (crc) {
944 int64_t ts = avpkt->pts != AV_NOPTS_VALUE ? avpkt->pts : avpkt->dts;
945 av_log(f->avctx, AV_LOG_ERROR, "slice CRC mismatch %X!", crc);
946 if (ts != AV_NOPTS_VALUE && avctx->pkt_timebase.num) {
947 av_log(f->avctx, AV_LOG_ERROR, "at %f seconds\n", ts*av_q2d(avctx->pkt_timebase));
948 } else if (ts != AV_NOPTS_VALUE) {
949 av_log(f->avctx, AV_LOG_ERROR, "at %"PRId64"\n", ts);
950 } else {
951 av_log(f->avctx, AV_LOG_ERROR, "\n");
952 }
953 fs->slice_damaged = 1;
954 }
955
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5724 times.
5724 if (avctx->debug & FF_DEBUG_PICT_INFO) {
956 av_log(avctx, AV_LOG_DEBUG, "slice %d, CRC: 0x%08"PRIX32"\n", i, AV_RB32(buf_p + v - 4));
957 }
958 }
959
960
2/2
✓ Branch 0 taken 4293 times.
✓ Branch 1 taken 1635 times.
5928 if (i) {
961 4293 ff_init_range_decoder(&fs->c, buf_p, v);
962 } else
963 1635 fs->c.bytestream_end = buf_p + v;
964
965 5928 fs->avctx = avctx;
966 }
967
968 1635 avctx->execute(avctx,
969 decode_slice,
970 1635 &f->slice_context[0],
971 NULL,
972 f->slice_count,
973 sizeof(void*));
974
975
2/2
✓ Branch 0 taken 5928 times.
✓ Branch 1 taken 1635 times.
7563 for (i = f->slice_count - 1; i >= 0; i--) {
976 5928 FFV1Context *fs = f->slice_context[i];
977 int j;
978
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 5928 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
5928 if (fs->slice_damaged && f->last_picture.f->data[0]) {
979 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(avctx->pix_fmt);
980 const uint8_t *src[4];
981 uint8_t *dst[4];
982 ff_thread_await_progress(&f->last_picture, INT_MAX, 0);
983 for (j = 0; j < desc->nb_components; j++) {
984 int pixshift = desc->comp[j].depth > 8;
985 int sh = (j == 1 || j == 2) ? f->chroma_h_shift : 0;
986 int sv = (j == 1 || j == 2) ? f->chroma_v_shift : 0;
987 dst[j] = p->data[j] + p->linesize[j] *
988 (fs->slice_y >> sv) + ((fs->slice_x >> sh) << pixshift);
989 src[j] = f->last_picture.f->data[j] + f->last_picture.f->linesize[j] *
990 (fs->slice_y >> sv) + ((fs->slice_x >> sh) << pixshift);
991
992 }
993 if (desc->flags & AV_PIX_FMT_FLAG_PAL) {
994 dst[1] = p->data[1];
995 src[1] = f->last_picture.f->data[1];
996 }
997 av_image_copy(dst, p->linesize, src,
998 f->last_picture.f->linesize,
999 avctx->pix_fmt,
1000 fs->slice_width,
1001 fs->slice_height);
1002 }
1003 }
1004 1635 ff_thread_report_progress(&f->picture, INT_MAX, 0);
1005
1006
1/2
✓ Branch 0 taken 1635 times.
✗ Branch 1 not taken.
1635 if (f->last_picture.f)
1007 1635 ff_thread_release_ext_buffer(avctx, &f->last_picture);
1008
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1635 times.
1635 if ((ret = av_frame_ref(rframe, f->picture.f)) < 0)
1009 return ret;
1010
1011 1635 *got_frame = 1;
1012
1013 1635 return buf_size;
1014 }
1015
1016 static void copy_fields(FFV1Context *fsdst, const FFV1Context *fssrc,
1017 const FFV1Context *fsrc)
1018 {
1019 fsdst->version = fsrc->version;
1020 fsdst->micro_version = fsrc->micro_version;
1021 fsdst->chroma_planes = fsrc->chroma_planes;
1022 fsdst->chroma_h_shift = fsrc->chroma_h_shift;
1023 fsdst->chroma_v_shift = fsrc->chroma_v_shift;
1024 fsdst->transparency = fsrc->transparency;
1025 fsdst->plane_count = fsrc->plane_count;
1026 fsdst->ac = fsrc->ac;
1027 fsdst->colorspace = fsrc->colorspace;
1028
1029 fsdst->ec = fsrc->ec;
1030 fsdst->intra = fsrc->intra;
1031 fsdst->slice_damaged = fssrc->slice_damaged;
1032 fsdst->key_frame_ok = fsrc->key_frame_ok;
1033
1034 fsdst->packed_at_lsb = fsrc->packed_at_lsb;
1035 fsdst->slice_count = fsrc->slice_count;
1036 if (fsrc->version<3){
1037 fsdst->slice_x = fssrc->slice_x;
1038 fsdst->slice_y = fssrc->slice_y;
1039 fsdst->slice_width = fssrc->slice_width;
1040 fsdst->slice_height = fssrc->slice_height;
1041 }
1042 }
1043
1044 #if HAVE_THREADS
1045 static int update_thread_context(AVCodecContext *dst, const AVCodecContext *src)
1046 {
1047 FFV1Context *fsrc = src->priv_data;
1048 FFV1Context *fdst = dst->priv_data;
1049 int i, ret;
1050
1051 if (dst == src)
1052 return 0;
1053
1054 {
1055 ThreadFrame picture = fdst->picture, last_picture = fdst->last_picture;
1056 uint8_t (*initial_states[MAX_QUANT_TABLES])[32];
1057 struct FFV1Context *slice_context[MAX_SLICES];
1058 memcpy(initial_states, fdst->initial_states, sizeof(fdst->initial_states));
1059 memcpy(slice_context, fdst->slice_context , sizeof(fdst->slice_context));
1060
1061 memcpy(fdst, fsrc, sizeof(*fdst));
1062 memcpy(fdst->initial_states, initial_states, sizeof(fdst->initial_states));
1063 memcpy(fdst->slice_context, slice_context , sizeof(fdst->slice_context));
1064 fdst->picture = picture;
1065 fdst->last_picture = last_picture;
1066 for (i = 0; i<fdst->num_h_slices * fdst->num_v_slices; i++) {
1067 FFV1Context *fssrc = fsrc->slice_context[i];
1068 FFV1Context *fsdst = fdst->slice_context[i];
1069 copy_fields(fsdst, fssrc, fsrc);
1070 }
1071 av_assert0(!fdst->plane[0].state);
1072 av_assert0(!fdst->sample_buffer);
1073 }
1074
1075 av_assert1(fdst->max_slice_count == fsrc->max_slice_count);
1076
1077
1078 ff_thread_release_ext_buffer(dst, &fdst->picture);
1079 if (fsrc->picture.f->data[0]) {
1080 if ((ret = ff_thread_ref_frame(&fdst->picture, &fsrc->picture)) < 0)
1081 return ret;
1082 }
1083
1084 fdst->fsrc = fsrc;
1085
1086 return 0;
1087 }
1088 #endif
1089
1090 68 static av_cold int ffv1_decode_close(AVCodecContext *avctx)
1091 {
1092 68 FFV1Context *const s = avctx->priv_data;
1093
1094
1/2
✓ Branch 0 taken 68 times.
✗ Branch 1 not taken.
68 if (s->picture.f) {
1095 68 ff_thread_release_ext_buffer(avctx, &s->picture);
1096 68 av_frame_free(&s->picture.f);
1097 }
1098
1099
1/2
✓ Branch 0 taken 68 times.
✗ Branch 1 not taken.
68 if (s->last_picture.f) {
1100 68 ff_thread_release_ext_buffer(avctx, &s->last_picture);
1101 68 av_frame_free(&s->last_picture.f);
1102 }
1103 68 return ff_ffv1_close(avctx);
1104 }
1105
1106 const FFCodec ff_ffv1_decoder = {
1107 .p.name = "ffv1",
1108 CODEC_LONG_NAME("FFmpeg video codec #1"),
1109 .p.type = AVMEDIA_TYPE_VIDEO,
1110 .p.id = AV_CODEC_ID_FFV1,
1111 .priv_data_size = sizeof(FFV1Context),
1112 .init = decode_init,
1113 .close = ffv1_decode_close,
1114 FF_CODEC_DECODE_CB(decode_frame),
1115 UPDATE_THREAD_CONTEXT(update_thread_context),
1116 .p.capabilities = AV_CODEC_CAP_DR1 |
1117 AV_CODEC_CAP_FRAME_THREADS | AV_CODEC_CAP_SLICE_THREADS,
1118 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP |
1119 FF_CODEC_CAP_ALLOCATE_PROGRESS,
1120 };
1121