FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/ffv1dec.c
Date: 2023-03-22 23:59:29
Exec Total Coverage
Lines: 402 692 58.1%
Functions: 14 16 87.5%
Branches: 267 591 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
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 60 times.
60 if (f->num_h_slices > MAX_SLICES / f->num_v_slices) {
479 av_log(f->avctx, AV_LOG_ERROR, "slice count unsupported\n");
480 return AVERROR_PATCHWELCOME;
481 }
482
483 60 f->quant_table_count = get_symbol(c, state, 0);
484
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) {
485 av_log(f->avctx, AV_LOG_ERROR, "quant table count %d is invalid\n", f->quant_table_count);
486 f->quant_table_count = 0;
487 return AVERROR_INVALIDDATA;
488 }
489
490
2/2
✓ Branch 0 taken 120 times.
✓ Branch 1 taken 60 times.
180 for (i = 0; i < f->quant_table_count; i++) {
491 120 f->context_count[i] = read_quant_tables(c, f->quant_tables[i]);
492
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 120 times.
120 if (f->context_count[i] < 0) {
493 av_log(f->avctx, AV_LOG_ERROR, "read_quant_table error\n");
494 return AVERROR_INVALIDDATA;
495 }
496 }
497
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 60 times.
60 if ((ret = ff_ffv1_allocate_initial_states(f)) < 0)
498 return ret;
499
500
2/2
✓ Branch 0 taken 120 times.
✓ Branch 1 taken 60 times.
180 for (i = 0; i < f->quant_table_count; i++)
501
2/2
✓ Branch 1 taken 8 times.
✓ Branch 2 taken 112 times.
120 if (get_rac(c, state)) {
502
2/2
✓ Branch 0 taken 60504 times.
✓ Branch 1 taken 8 times.
60512 for (j = 0; j < f->context_count[i]; j++)
503
2/2
✓ Branch 0 taken 1936128 times.
✓ Branch 1 taken 60504 times.
1996632 for (k = 0; k < CONTEXT_SIZE; k++) {
504
2/2
✓ Branch 0 taken 1935872 times.
✓ Branch 1 taken 256 times.
1936128 int pred = j ? f->initial_states[i][j - 1][k] : 128;
505 1936128 f->initial_states[i][j][k] =
506 1936128 (pred + get_symbol(c, state2[k], 1)) & 0xFF;
507 }
508 }
509
510
1/2
✓ Branch 0 taken 60 times.
✗ Branch 1 not taken.
60 if (f->version > 2) {
511 60 f->ec = get_symbol(c, state, 0);
512
1/2
✓ Branch 0 taken 60 times.
✗ Branch 1 not taken.
60 if (f->micro_version > 2)
513 60 f->intra = get_symbol(c, state, 0);
514 }
515
516
1/2
✓ Branch 0 taken 60 times.
✗ Branch 1 not taken.
60 if (f->version > 2) {
517 unsigned v;
518 120 v = av_crc(av_crc_get_table(AV_CRC_32_IEEE), 0,
519 60 f->avctx->extradata, f->avctx->extradata_size);
520
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) {
521 av_log(f->avctx, AV_LOG_ERROR, "CRC mismatch %X!\n", v);
522 return AVERROR_INVALIDDATA;
523 }
524 60 crc = AV_RB32(f->avctx->extradata + f->avctx->extradata_size - 4);
525 }
526
527
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 60 times.
60 if (f->avctx->debug & FF_DEBUG_PICT_INFO)
528 av_log(f->avctx, AV_LOG_DEBUG,
529 "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",
530 f->version, f->micro_version,
531 f->ac,
532 f->colorspace,
533 f->avctx->bits_per_raw_sample,
534 f->chroma_planes, f->chroma_h_shift, f->chroma_v_shift,
535 f->transparency,
536 f->num_h_slices, f->num_v_slices,
537 f->quant_table_count,
538 f->ec,
539 f->intra,
540 crc
541 );
542 60 return 0;
543 }
544
545 195 static int read_header(FFV1Context *f)
546 {
547 uint8_t state[CONTEXT_SIZE];
548 195 int i, j, context_count = -1; //-1 to avoid warning
549 195 RangeCoder *const c = &f->slice_context[0]->c;
550
551 195 memset(state, 128, sizeof(state));
552
553
2/2
✓ Branch 0 taken 24 times.
✓ Branch 1 taken 171 times.
195 if (f->version < 2) {
554 int chroma_planes, chroma_h_shift, chroma_v_shift, transparency, colorspace, bits_per_raw_sample;
555 24 unsigned v= get_symbol(c, state, 0);
556
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
24 if (v >= 2) {
557 av_log(f->avctx, AV_LOG_ERROR, "invalid version %d in ver01 header\n", v);
558 return AVERROR_INVALIDDATA;
559 }
560 24 f->version = v;
561 24 f->ac = get_symbol(c, state, 0);
562
563
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
24 if (f->ac == AC_RANGE_CUSTOM_TAB) {
564 for (i = 1; i < 256; i++) {
565 int st = get_symbol(c, state, 1) + c->one_state[i];
566 if (st < 1 || st > 255) {
567 av_log(f->avctx, AV_LOG_ERROR, "invalid state transition %d\n", st);
568 return AVERROR_INVALIDDATA;
569 }
570 f->state_transition[i] = st;
571 }
572 }
573
574 24 colorspace = get_symbol(c, state, 0); //YUV cs type
575
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;
576 24 chroma_planes = get_rac(c, state);
577 24 chroma_h_shift = get_symbol(c, state, 0);
578 24 chroma_v_shift = get_symbol(c, state, 0);
579 24 transparency = get_rac(c, state);
580
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)
581 transparency = 0;
582
583
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 8 times.
24 if (f->plane_count) {
584
1/2
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
16 if (colorspace != f->colorspace ||
585
1/2
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
16 bits_per_raw_sample != f->avctx->bits_per_raw_sample ||
586
1/2
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
16 chroma_planes != f->chroma_planes ||
587
1/2
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
16 chroma_h_shift != f->chroma_h_shift ||
588
1/2
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
16 chroma_v_shift != f->chroma_v_shift ||
589
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16 times.
16 transparency != f->transparency) {
590 av_log(f->avctx, AV_LOG_ERROR, "Invalid change of global parameters\n");
591 return AVERROR_INVALIDDATA;
592 }
593 }
594
595
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) {
596 av_log(f->avctx, AV_LOG_ERROR, "chroma shift parameters %d %d are invalid\n",
597 chroma_h_shift, chroma_v_shift);
598 return AVERROR_INVALIDDATA;
599 }
600
601 24 f->colorspace = colorspace;
602 24 f->avctx->bits_per_raw_sample = bits_per_raw_sample;
603 24 f->chroma_planes = chroma_planes;
604 24 f->chroma_h_shift = chroma_h_shift;
605 24 f->chroma_v_shift = chroma_v_shift;
606 24 f->transparency = transparency;
607
608 24 f->plane_count = 2 + f->transparency;
609 }
610
611
2/2
✓ Branch 0 taken 147 times.
✓ Branch 1 taken 48 times.
195 if (f->colorspace == 0) {
612
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) {
613 if (f->avctx->bits_per_raw_sample <= 8)
614 f->avctx->pix_fmt = AV_PIX_FMT_GRAY8;
615 else if (f->avctx->bits_per_raw_sample == 9) {
616 f->packed_at_lsb = 1;
617 f->avctx->pix_fmt = AV_PIX_FMT_GRAY9;
618 } else if (f->avctx->bits_per_raw_sample == 10) {
619 f->packed_at_lsb = 1;
620 f->avctx->pix_fmt = AV_PIX_FMT_GRAY10;
621 } else if (f->avctx->bits_per_raw_sample == 12) {
622 f->packed_at_lsb = 1;
623 f->avctx->pix_fmt = AV_PIX_FMT_GRAY12;
624 } else if (f->avctx->bits_per_raw_sample == 16) {
625 f->packed_at_lsb = 1;
626 f->avctx->pix_fmt = AV_PIX_FMT_GRAY16;
627 } else if (f->avctx->bits_per_raw_sample < 16) {
628 f->avctx->pix_fmt = AV_PIX_FMT_GRAY16;
629 } else
630 return AVERROR(ENOSYS);
631
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) {
632 if (f->avctx->bits_per_raw_sample <= 8)
633 f->avctx->pix_fmt = AV_PIX_FMT_YA8;
634 else
635 return AVERROR(ENOSYS);
636
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) {
637
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) {
638 case 0x00: f->avctx->pix_fmt = AV_PIX_FMT_YUV444P; break;
639 case 0x01: f->avctx->pix_fmt = AV_PIX_FMT_YUV440P; break;
640 case 0x10: f->avctx->pix_fmt = AV_PIX_FMT_YUV422P; break;
641 97 case 0x11: f->avctx->pix_fmt = AV_PIX_FMT_YUV420P; break;
642 case 0x20: f->avctx->pix_fmt = AV_PIX_FMT_YUV411P; break;
643 case 0x22: f->avctx->pix_fmt = AV_PIX_FMT_YUV410P; break;
644 }
645
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) {
646 switch(16*f->chroma_h_shift + f->chroma_v_shift) {
647 case 0x00: f->avctx->pix_fmt = AV_PIX_FMT_YUVA444P; break;
648 case 0x10: f->avctx->pix_fmt = AV_PIX_FMT_YUVA422P; break;
649 case 0x11: f->avctx->pix_fmt = AV_PIX_FMT_YUVA420P; break;
650 }
651
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) {
652 f->packed_at_lsb = 1;
653 switch(16 * f->chroma_h_shift + f->chroma_v_shift) {
654 case 0x00: f->avctx->pix_fmt = AV_PIX_FMT_YUV444P9; break;
655 case 0x10: f->avctx->pix_fmt = AV_PIX_FMT_YUV422P9; break;
656 case 0x11: f->avctx->pix_fmt = AV_PIX_FMT_YUV420P9; break;
657 }
658
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) {
659 f->packed_at_lsb = 1;
660 switch(16 * f->chroma_h_shift + f->chroma_v_shift) {
661 case 0x00: f->avctx->pix_fmt = AV_PIX_FMT_YUVA444P9; break;
662 case 0x10: f->avctx->pix_fmt = AV_PIX_FMT_YUVA422P9; break;
663 case 0x11: f->avctx->pix_fmt = AV_PIX_FMT_YUVA420P9; break;
664 }
665
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) {
666 26 f->packed_at_lsb = 1;
667
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) {
668 case 0x00: f->avctx->pix_fmt = AV_PIX_FMT_YUV444P10; break;
669 case 0x01: f->avctx->pix_fmt = AV_PIX_FMT_YUV440P10; break;
670 26 case 0x10: f->avctx->pix_fmt = AV_PIX_FMT_YUV422P10; break;
671 case 0x11: f->avctx->pix_fmt = AV_PIX_FMT_YUV420P10; break;
672 }
673
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) {
674 f->packed_at_lsb = 1;
675 switch(16 * f->chroma_h_shift + f->chroma_v_shift) {
676 case 0x00: f->avctx->pix_fmt = AV_PIX_FMT_YUVA444P10; break;
677 case 0x10: f->avctx->pix_fmt = AV_PIX_FMT_YUVA422P10; break;
678 case 0x11: f->avctx->pix_fmt = AV_PIX_FMT_YUVA420P10; break;
679 }
680
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) {
681 f->packed_at_lsb = 1;
682 switch(16 * f->chroma_h_shift + f->chroma_v_shift) {
683 case 0x00: f->avctx->pix_fmt = AV_PIX_FMT_YUV444P12; break;
684 case 0x01: f->avctx->pix_fmt = AV_PIX_FMT_YUV440P12; break;
685 case 0x10: f->avctx->pix_fmt = AV_PIX_FMT_YUV422P12; break;
686 case 0x11: f->avctx->pix_fmt = AV_PIX_FMT_YUV420P12; break;
687 }
688
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) {
689 f->packed_at_lsb = 1;
690 switch(16 * f->chroma_h_shift + f->chroma_v_shift) {
691 case 0x00: f->avctx->pix_fmt = AV_PIX_FMT_YUV444P14; break;
692 case 0x10: f->avctx->pix_fmt = AV_PIX_FMT_YUV422P14; break;
693 case 0x11: f->avctx->pix_fmt = AV_PIX_FMT_YUV420P14; break;
694 }
695
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){
696 24 f->packed_at_lsb = 1;
697
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) {
698 24 case 0x00: f->avctx->pix_fmt = AV_PIX_FMT_YUV444P16; break;
699 case 0x10: f->avctx->pix_fmt = AV_PIX_FMT_YUV422P16; break;
700 case 0x11: f->avctx->pix_fmt = AV_PIX_FMT_YUV420P16; break;
701 }
702 } else if (f->avctx->bits_per_raw_sample == 16 && f->transparency){
703 f->packed_at_lsb = 1;
704 switch(16 * f->chroma_h_shift + f->chroma_v_shift) {
705 case 0x00: f->avctx->pix_fmt = AV_PIX_FMT_YUVA444P16; break;
706 case 0x10: f->avctx->pix_fmt = AV_PIX_FMT_YUVA422P16; break;
707 case 0x11: f->avctx->pix_fmt = AV_PIX_FMT_YUVA420P16; break;
708 }
709 }
710
1/2
✓ Branch 0 taken 48 times.
✗ Branch 1 not taken.
48 } else if (f->colorspace == 1) {
711
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) {
712 av_log(f->avctx, AV_LOG_ERROR,
713 "chroma subsampling not supported in this colorspace\n");
714 return AVERROR(ENOSYS);
715 }
716
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)
717 24 f->avctx->pix_fmt = AV_PIX_FMT_0RGB32;
718
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)
719 f->avctx->pix_fmt = AV_PIX_FMT_RGB32;
720
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)
721 f->avctx->pix_fmt = AV_PIX_FMT_GBRP9;
722
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)
723 f->avctx->pix_fmt = AV_PIX_FMT_GBRP10;
724
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)
725 f->avctx->pix_fmt = AV_PIX_FMT_GBRAP10;
726
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)
727 f->avctx->pix_fmt = AV_PIX_FMT_GBRP12;
728
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)
729 f->avctx->pix_fmt = AV_PIX_FMT_GBRAP12;
730
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)
731 f->avctx->pix_fmt = AV_PIX_FMT_GBRP14;
732
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) {
733 24 f->avctx->pix_fmt = AV_PIX_FMT_GBRP16;
734 24 f->use32bit = 1;
735 }
736 else if (f->avctx->bits_per_raw_sample == 16 && f->transparency) {
737 f->avctx->pix_fmt = AV_PIX_FMT_GBRAP16;
738 f->use32bit = 1;
739 }
740 } else {
741 av_log(f->avctx, AV_LOG_ERROR, "colorspace not supported\n");
742 return AVERROR(ENOSYS);
743 }
744
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 195 times.
195 if (f->avctx->pix_fmt == AV_PIX_FMT_NONE) {
745 av_log(f->avctx, AV_LOG_ERROR, "format not supported\n");
746 return AVERROR(ENOSYS);
747 }
748
749 ff_dlog(f->avctx, "%d %d %d\n",
750 f->chroma_h_shift, f->chroma_v_shift, f->avctx->pix_fmt);
751
2/2
✓ Branch 0 taken 24 times.
✓ Branch 1 taken 171 times.
195 if (f->version < 2) {
752 24 context_count = read_quant_tables(c, f->quant_table);
753
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
24 if (context_count < 0) {
754 av_log(f->avctx, AV_LOG_ERROR, "read_quant_table error\n");
755 return AVERROR_INVALIDDATA;
756 }
757 24 f->slice_count = f->max_slice_count;
758
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 171 times.
171 } else if (f->version < 3) {
759 f->slice_count = get_symbol(c, state, 0);
760 } else {
761 171 const uint8_t *p = c->bytestream_end;
762 171 for (f->slice_count = 0;
763
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;
764 684 f->slice_count++) {
765
1/2
✓ Branch 0 taken 684 times.
✗ Branch 1 not taken.
684 int trailer = 3 + 5*!!f->ec;
766 684 int size = AV_RB24(p-trailer);
767
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 684 times.
684 if (size + trailer > p - c->bytestream_start)
768 break;
769 684 p -= size + trailer;
770 }
771 }
772
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) {
773 av_log(f->avctx, AV_LOG_ERROR, "slice count %d is invalid (max=%d)\n", f->slice_count, f->max_slice_count);
774 return AVERROR_INVALIDDATA;
775 }
776
777
2/2
✓ Branch 0 taken 708 times.
✓ Branch 1 taken 195 times.
903 for (j = 0; j < f->slice_count; j++) {
778 708 FFV1Context *fs = f->slice_context[j];
779 708 fs->ac = f->ac;
780 708 fs->packed_at_lsb = f->packed_at_lsb;
781
782 708 fs->slice_damaged = 0;
783
784
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 708 times.
708 if (f->version == 2) {
785 int sx = get_symbol(c, state, 0);
786 int sy = get_symbol(c, state, 0);
787 int sw = get_symbol(c, state, 0) + 1U;
788 int sh = get_symbol(c, state, 0) + 1U;
789
790 if (sx < 0 || sy < 0 || sw <= 0 || sh <= 0)
791 return AVERROR_INVALIDDATA;
792 if (sx > f->num_h_slices - sw || sy > f->num_v_slices - sh)
793 return AVERROR_INVALIDDATA;
794
795 fs->slice_x = sx * (int64_t)f->width / f->num_h_slices;
796 fs->slice_y = sy * (int64_t)f->height / f->num_v_slices;
797 fs->slice_width = (sx + sw) * (int64_t)f->width / f->num_h_slices - fs->slice_x;
798 fs->slice_height = (sy + sh) * (int64_t)f->height / f->num_v_slices - fs->slice_y;
799
800 av_assert0((unsigned)fs->slice_width <= f->width &&
801 (unsigned)fs->slice_height <= f->height);
802 av_assert0 ( (unsigned)fs->slice_x + (uint64_t)fs->slice_width <= f->width
803 && (unsigned)fs->slice_y + (uint64_t)fs->slice_height <= f->height);
804 }
805
806
2/2
✓ Branch 0 taken 1416 times.
✓ Branch 1 taken 708 times.
2124 for (i = 0; i < f->plane_count; i++) {
807 1416 PlaneContext *const p = &fs->plane[i];
808
809
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1416 times.
1416 if (f->version == 2) {
810 int idx = get_symbol(c, state, 0);
811 if (idx >= (unsigned)f->quant_table_count) {
812 av_log(f->avctx, AV_LOG_ERROR,
813 "quant_table_index out of range\n");
814 return AVERROR_INVALIDDATA;
815 }
816 p->quant_table_index = idx;
817 memcpy(p->quant_table, f->quant_tables[idx],
818 sizeof(p->quant_table));
819 context_count = f->context_count[idx];
820 } else {
821 1416 memcpy(p->quant_table, f->quant_table, sizeof(p->quant_table));
822 }
823
824
2/2
✓ Branch 0 taken 48 times.
✓ Branch 1 taken 1368 times.
1416 if (f->version <= 2) {
825
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 48 times.
48 av_assert0(context_count >= 0);
826
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 32 times.
48 if (p->context_count < context_count) {
827 16 av_freep(&p->state);
828 16 av_freep(&p->vlc_state);
829 }
830 48 p->context_count = context_count;
831 }
832 }
833 }
834 195 return 0;
835 }
836
837 68 static av_cold int decode_init(AVCodecContext *avctx)
838 {
839 68 FFV1Context *f = avctx->priv_data;
840 int ret;
841
842
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 68 times.
68 if ((ret = ff_ffv1_common_init(avctx)) < 0)
843 return ret;
844
845 68 f->picture.f = av_frame_alloc();
846 68 f->last_picture.f = av_frame_alloc();
847
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)
848 return AVERROR(ENOMEM);
849
850
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)
851 return ret;
852
853
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 68 times.
68 if ((ret = ff_ffv1_init_slice_contexts(f)) < 0)
854 return ret;
855
856 68 return 0;
857 }
858
859 1635 static int decode_frame(AVCodecContext *avctx, AVFrame *rframe,
860 int *got_frame, AVPacket *avpkt)
861 {
862 1635 uint8_t *buf = avpkt->data;
863 1635 int buf_size = avpkt->size;
864 1635 FFV1Context *f = avctx->priv_data;
865 1635 RangeCoder *const c = &f->slice_context[0]->c;
866 int i, ret;
867 1635 uint8_t keystate = 128;
868 uint8_t *buf_p;
869 AVFrame *p;
870
871
1/2
✓ Branch 0 taken 1635 times.
✗ Branch 1 not taken.
1635 if (f->last_picture.f)
872 1635 ff_thread_release_ext_buffer(avctx, &f->last_picture);
873 1635 FFSWAP(ThreadFrame, f->picture, f->last_picture);
874
875 1635 f->cur = p = f->picture.f;
876
877
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) {
878 /* we have interlaced material flagged in container */
879 p->interlaced_frame = 1;
880 if (avctx->field_order == AV_FIELD_TT || avctx->field_order == AV_FIELD_TB)
881 p->top_field_first = 1;
882 }
883
884 1635 f->avctx = avctx;
885 1635 ff_init_range_decoder(c, buf, buf_size);
886 1635 ff_build_rac_states(c, 0.05 * (1LL << 32), 256 - 8);
887
888 1635 p->pict_type = AV_PICTURE_TYPE_I; //FIXME I vs. P
889
2/2
✓ Branch 1 taken 195 times.
✓ Branch 2 taken 1440 times.
1635 if (get_rac(c, &keystate)) {
890 195 p->key_frame = 1;
891 195 f->key_frame_ok = 0;
892
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 195 times.
195 if ((ret = read_header(f)) < 0)
893 return ret;
894 195 f->key_frame_ok = 1;
895 } else {
896
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1440 times.
1440 if (!f->key_frame_ok) {
897 av_log(avctx, AV_LOG_ERROR,
898 "Cannot decode non-keyframe without valid keyframe\n");
899 return AVERROR_INVALIDDATA;
900 }
901 1440 p->key_frame = 0;
902 }
903
904
2/2
✓ Branch 0 taken 818 times.
✓ Branch 1 taken 817 times.
1635 if (f->ac != AC_GOLOMB_RICE) {
905
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 818 times.
818 if (buf_size < avctx->width * avctx->height / (128*8))
906 return AVERROR_INVALIDDATA;
907 } else {
908 817 int w = avctx->width;
909 817 int s = 1 + w / (1<<23);
910
911 817 w /= s;
912
913
2/2
✓ Branch 0 taken 18589 times.
✓ Branch 1 taken 817 times.
19406 for (i = 0; w > (1<<ff_log2_run[i]); i++)
914 18589 w -= ff_log2_run[i];
915
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 817 times.
817 if (buf_size < (avctx->height + i + 6) / 8 * s)
916 return AVERROR_INVALIDDATA;
917 }
918
919 1635 ret = ff_thread_get_ext_buffer(avctx, &f->picture, AV_GET_BUFFER_FLAG_REF);
920
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1635 times.
1635 if (ret < 0)
921 return ret;
922
923
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1635 times.
1635 if (avctx->debug & FF_DEBUG_PICT_INFO)
924 av_log(avctx, AV_LOG_DEBUG, "ver:%d keyframe:%d coder:%d ec:%d slices:%d bps:%d\n",
925 f->version, p->key_frame, f->ac, f->ec, f->slice_count, f->avctx->bits_per_raw_sample);
926
927 1635 ff_thread_finish_setup(avctx);
928
929 1635 buf_p = buf + buf_size;
930
2/2
✓ Branch 0 taken 5928 times.
✓ Branch 1 taken 1635 times.
7563 for (i = f->slice_count - 1; i >= 0; i--) {
931 5928 FFV1Context *fs = f->slice_context[i];
932
2/2
✓ Branch 0 taken 5724 times.
✓ Branch 1 taken 204 times.
5928 int trailer = 3 + 5*!!f->ec;
933 int v;
934
935
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) {
936
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5724 times.
5724 if (trailer > buf_p - buf) v = INT_MAX;
937 5724 else v = AV_RB24(buf_p-trailer) + trailer;
938 204 } else v = buf_p - c->bytestream_start;
939
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5928 times.
5928 if (buf_p - c->bytestream_start < v) {
940 av_log(avctx, AV_LOG_ERROR, "Slice pointer chain broken\n");
941 ff_thread_report_progress(&f->picture, INT_MAX, 0);
942 return AVERROR_INVALIDDATA;
943 }
944 5928 buf_p -= v;
945
946
2/2
✓ Branch 0 taken 5724 times.
✓ Branch 1 taken 204 times.
5928 if (f->ec) {
947 5724 unsigned crc = av_crc(av_crc_get_table(AV_CRC_32_IEEE), 0, buf_p, v);
948
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5724 times.
5724 if (crc) {
949 int64_t ts = avpkt->pts != AV_NOPTS_VALUE ? avpkt->pts : avpkt->dts;
950 av_log(f->avctx, AV_LOG_ERROR, "slice CRC mismatch %X!", crc);
951 if (ts != AV_NOPTS_VALUE && avctx->pkt_timebase.num) {
952 av_log(f->avctx, AV_LOG_ERROR, "at %f seconds\n", ts*av_q2d(avctx->pkt_timebase));
953 } else if (ts != AV_NOPTS_VALUE) {
954 av_log(f->avctx, AV_LOG_ERROR, "at %"PRId64"\n", ts);
955 } else {
956 av_log(f->avctx, AV_LOG_ERROR, "\n");
957 }
958 fs->slice_damaged = 1;
959 }
960
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5724 times.
5724 if (avctx->debug & FF_DEBUG_PICT_INFO) {
961 av_log(avctx, AV_LOG_DEBUG, "slice %d, CRC: 0x%08"PRIX32"\n", i, AV_RB32(buf_p + v - 4));
962 }
963 }
964
965
2/2
✓ Branch 0 taken 4293 times.
✓ Branch 1 taken 1635 times.
5928 if (i) {
966 4293 ff_init_range_decoder(&fs->c, buf_p, v);
967 } else
968 1635 fs->c.bytestream_end = buf_p + v;
969
970 5928 fs->avctx = avctx;
971 }
972
973 1635 avctx->execute(avctx,
974 decode_slice,
975 1635 &f->slice_context[0],
976 NULL,
977 f->slice_count,
978 sizeof(void*));
979
980
2/2
✓ Branch 0 taken 5928 times.
✓ Branch 1 taken 1635 times.
7563 for (i = f->slice_count - 1; i >= 0; i--) {
981 5928 FFV1Context *fs = f->slice_context[i];
982 int j;
983
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]) {
984 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(avctx->pix_fmt);
985 const uint8_t *src[4];
986 uint8_t *dst[4];
987 ff_thread_await_progress(&f->last_picture, INT_MAX, 0);
988 for (j = 0; j < desc->nb_components; j++) {
989 int pixshift = desc->comp[j].depth > 8;
990 int sh = (j == 1 || j == 2) ? f->chroma_h_shift : 0;
991 int sv = (j == 1 || j == 2) ? f->chroma_v_shift : 0;
992 dst[j] = p->data[j] + p->linesize[j] *
993 (fs->slice_y >> sv) + ((fs->slice_x >> sh) << pixshift);
994 src[j] = f->last_picture.f->data[j] + f->last_picture.f->linesize[j] *
995 (fs->slice_y >> sv) + ((fs->slice_x >> sh) << pixshift);
996
997 }
998 if (desc->flags & AV_PIX_FMT_FLAG_PAL) {
999 dst[1] = p->data[1];
1000 src[1] = f->last_picture.f->data[1];
1001 }
1002 av_image_copy(dst, p->linesize, src,
1003 f->last_picture.f->linesize,
1004 avctx->pix_fmt,
1005 fs->slice_width,
1006 fs->slice_height);
1007 }
1008 }
1009 1635 ff_thread_report_progress(&f->picture, INT_MAX, 0);
1010
1011
1/2
✓ Branch 0 taken 1635 times.
✗ Branch 1 not taken.
1635 if (f->last_picture.f)
1012 1635 ff_thread_release_ext_buffer(avctx, &f->last_picture);
1013
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1635 times.
1635 if ((ret = av_frame_ref(rframe, f->picture.f)) < 0)
1014 return ret;
1015
1016 1635 *got_frame = 1;
1017
1018 1635 return buf_size;
1019 }
1020
1021 static void copy_fields(FFV1Context *fsdst, const FFV1Context *fssrc,
1022 const FFV1Context *fsrc)
1023 {
1024 fsdst->version = fsrc->version;
1025 fsdst->micro_version = fsrc->micro_version;
1026 fsdst->chroma_planes = fsrc->chroma_planes;
1027 fsdst->chroma_h_shift = fsrc->chroma_h_shift;
1028 fsdst->chroma_v_shift = fsrc->chroma_v_shift;
1029 fsdst->transparency = fsrc->transparency;
1030 fsdst->plane_count = fsrc->plane_count;
1031 fsdst->ac = fsrc->ac;
1032 fsdst->colorspace = fsrc->colorspace;
1033
1034 fsdst->ec = fsrc->ec;
1035 fsdst->intra = fsrc->intra;
1036 fsdst->slice_damaged = fssrc->slice_damaged;
1037 fsdst->key_frame_ok = fsrc->key_frame_ok;
1038
1039 fsdst->packed_at_lsb = fsrc->packed_at_lsb;
1040 fsdst->slice_count = fsrc->slice_count;
1041 if (fsrc->version<3){
1042 fsdst->slice_x = fssrc->slice_x;
1043 fsdst->slice_y = fssrc->slice_y;
1044 fsdst->slice_width = fssrc->slice_width;
1045 fsdst->slice_height = fssrc->slice_height;
1046 }
1047 }
1048
1049 #if HAVE_THREADS
1050 static int update_thread_context(AVCodecContext *dst, const AVCodecContext *src)
1051 {
1052 FFV1Context *fsrc = src->priv_data;
1053 FFV1Context *fdst = dst->priv_data;
1054 int i, ret;
1055
1056 if (dst == src)
1057 return 0;
1058
1059 {
1060 ThreadFrame picture = fdst->picture, last_picture = fdst->last_picture;
1061 uint8_t (*initial_states[MAX_QUANT_TABLES])[32];
1062 struct FFV1Context *slice_context[MAX_SLICES];
1063 memcpy(initial_states, fdst->initial_states, sizeof(fdst->initial_states));
1064 memcpy(slice_context, fdst->slice_context , sizeof(fdst->slice_context));
1065
1066 memcpy(fdst, fsrc, sizeof(*fdst));
1067 memcpy(fdst->initial_states, initial_states, sizeof(fdst->initial_states));
1068 memcpy(fdst->slice_context, slice_context , sizeof(fdst->slice_context));
1069 fdst->picture = picture;
1070 fdst->last_picture = last_picture;
1071 for (i = 0; i<fdst->num_h_slices * fdst->num_v_slices; i++) {
1072 FFV1Context *fssrc = fsrc->slice_context[i];
1073 FFV1Context *fsdst = fdst->slice_context[i];
1074 copy_fields(fsdst, fssrc, fsrc);
1075 }
1076 av_assert0(!fdst->plane[0].state);
1077 av_assert0(!fdst->sample_buffer);
1078 }
1079
1080 av_assert1(fdst->max_slice_count == fsrc->max_slice_count);
1081
1082
1083 ff_thread_release_ext_buffer(dst, &fdst->picture);
1084 if (fsrc->picture.f->data[0]) {
1085 if ((ret = ff_thread_ref_frame(&fdst->picture, &fsrc->picture)) < 0)
1086 return ret;
1087 }
1088
1089 fdst->fsrc = fsrc;
1090
1091 return 0;
1092 }
1093 #endif
1094
1095 68 static av_cold int ffv1_decode_close(AVCodecContext *avctx)
1096 {
1097 68 FFV1Context *const s = avctx->priv_data;
1098
1099
1/2
✓ Branch 0 taken 68 times.
✗ Branch 1 not taken.
68 if (s->picture.f) {
1100 68 ff_thread_release_ext_buffer(avctx, &s->picture);
1101 68 av_frame_free(&s->picture.f);
1102 }
1103
1104
1/2
✓ Branch 0 taken 68 times.
✗ Branch 1 not taken.
68 if (s->last_picture.f) {
1105 68 ff_thread_release_ext_buffer(avctx, &s->last_picture);
1106 68 av_frame_free(&s->last_picture.f);
1107 }
1108 68 return ff_ffv1_close(avctx);
1109 }
1110
1111 const FFCodec ff_ffv1_decoder = {
1112 .p.name = "ffv1",
1113 CODEC_LONG_NAME("FFmpeg video codec #1"),
1114 .p.type = AVMEDIA_TYPE_VIDEO,
1115 .p.id = AV_CODEC_ID_FFV1,
1116 .priv_data_size = sizeof(FFV1Context),
1117 .init = decode_init,
1118 .close = ffv1_decode_close,
1119 FF_CODEC_DECODE_CB(decode_frame),
1120 UPDATE_THREAD_CONTEXT(update_thread_context),
1121 .p.capabilities = AV_CODEC_CAP_DR1 |
1122 AV_CODEC_CAP_FRAME_THREADS | AV_CODEC_CAP_SLICE_THREADS,
1123 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP |
1124 FF_CODEC_CAP_ALLOCATE_PROGRESS,
1125 };
1126