FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/ffv1dec.c
Date: 2026-01-16 07:34:38
Exec Total Coverage
Lines: 317 562 56.4%
Functions: 13 17 76.5%
Branches: 187 426 43.9%

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/mem.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 "progressframe.h"
41 #include "libavutil/refstruct.h"
42 #include "thread.h"
43 #include "decode.h"
44 #include "hwconfig.h"
45 #include "hwaccel_internal.h"
46 #include "config_components.h"
47
48 163659991 static inline int get_vlc_symbol(GetBitContext *gb, VlcState *const state,
49 int bits)
50 {
51 int k, i, v, ret;
52
53 163659991 i = state->count;
54 163659991 k = 0;
55
2/2
✓ Branch 0 taken 272532174 times.
✓ Branch 1 taken 163659991 times.
436192165 while (i < state->error_sum) { // FIXME: optimize
56 272532174 k++;
57 272532174 i += i;
58 }
59
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 163659991 times.
163659991 if (k > bits) {
60 ff_dlog(NULL, "k-overflow bias:%d error:%d drift:%d count:%d k:%d",
61 state->bias, state->error_sum, state->drift, state->count, k);
62 k = bits;
63 }
64
65 163659991 v = get_sr_golomb(gb, k, 12, bits);
66 ff_dlog(NULL, "v:%d bias:%d error:%d drift:%d count:%d k:%d",
67 v, state->bias, state->error_sum, state->drift, state->count, k);
68
69 163659991 v ^= ((2 * state->drift + state->count) >> 31);
70
71 163659991 ret = fold(v + state->bias, bits);
72
73 163659991 update_vlc_state(state, v);
74
75 163659991 return ret;
76 }
77
78 4542400 static int is_input_end(RangeCoder *c, GetBitContext *gb, int ac)
79 {
80
2/2
✓ Branch 0 taken 2514800 times.
✓ Branch 1 taken 2027600 times.
4542400 if (ac != AC_GOLOMB_RICE) {
81
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2514800 times.
2514800 if (c->overread > MAX_OVERREAD)
82 return AVERROR_INVALIDDATA;
83 } else {
84
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2027600 times.
2027600 if (get_bits_left(gb) < 1)
85 return AVERROR_INVALIDDATA;
86 }
87 4542400 return 0;
88 }
89
90 #define TYPE int16_t
91 #define RENAME(name) name
92 #include "ffv1dec_template.c"
93 #undef TYPE
94 #undef RENAME
95
96 #define TYPE int32_t
97 #define RENAME(name) name ## 32
98 #include "ffv1dec_template.c"
99
100 18300 static int decode_plane(FFV1Context *f, FFV1SliceContext *sc,
101 GetBitContext *gb,
102 uint8_t *src, int w, int h, int stride, int plane_index,
103 int remap_index, int pixel_stride, int ac)
104 {
105 int x, y;
106 int16_t *sample[2];
107 int bits;
108 unsigned mask;
109
110
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 18300 times.
18300 if (sc->remap) {
111 bits = av_ceil_log2(sc->remap_count[remap_index]);
112 mask = (1<<bits)-1;
113 } else {
114 18300 bits = f->avctx->bits_per_raw_sample;
115 }
116
117 18300 sample[0] = sc->sample_buffer + 3;
118 18300 sample[1] = sc->sample_buffer + w + 6 + 3;
119
120 18300 sc->run_index = 0;
121
122 18300 memset(sc->sample_buffer, 0, 2 * (w + 6) * sizeof(*sc->sample_buffer));
123
124
2/2
✓ Branch 0 taken 1732400 times.
✓ Branch 1 taken 18300 times.
1750700 for (y = 0; y < h; y++) {
125 1732400 int16_t *temp = sample[0]; // FIXME: try a normal buffer
126
127 1732400 sample[0] = sample[1];
128 1732400 sample[1] = temp;
129
130 1732400 sample[1][-1] = sample[0][0];
131 1732400 sample[0][w] = sample[0][w - 1];
132
133
2/2
✓ Branch 0 taken 924200 times.
✓ Branch 1 taken 808200 times.
1732400 if (f->avctx->bits_per_raw_sample <= 8) {
134 924200 int ret = decode_line(f, sc, gb, w, sample, plane_index, 8, ac);
135
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 924200 times.
924200 if (ret < 0)
136 return ret;
137
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 924200 times.
924200 if (sc->remap)
138 for (x = 0; x < w; x++)
139 sample[1][x] = sc->fltmap[remap_index][sample[1][x]];
140
2/2
✓ Branch 0 taken 145253900 times.
✓ Branch 1 taken 924200 times.
146178100 for (x = 0; x < w; x++)
141 145253900 src[x*pixel_stride + stride * y] = sample[1][x];
142 } else {
143 808200 int ret = decode_line(f, sc, gb, w, sample, plane_index, bits, ac);
144
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 808200 times.
808200 if (ret < 0)
145 return ret;
146
147
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 808200 times.
808200 if (sc->remap) {
148 if (f->packed_at_lsb || f->avctx->bits_per_raw_sample == 16) {
149 for (x = 0; x < w; x++) {
150 ((uint16_t*)(src + stride*y))[x*pixel_stride] = sc->fltmap[remap_index][sample[1][x] & mask];
151 }
152 } else {
153 for (x = 0; x < w; x++) {
154 int v = sc->fltmap[remap_index][sample[1][x] & mask];
155 ((uint16_t*)(src + stride*y))[x*pixel_stride] = v << (16 - f->avctx->bits_per_raw_sample) | v >> (2 * f->avctx->bits_per_raw_sample - 16);
156 }
157 }
158 } else {
159
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 808200 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
808200 if (f->packed_at_lsb || f->avctx->bits_per_raw_sample == 16) {
160
2/2
✓ Branch 0 taken 106856200 times.
✓ Branch 1 taken 808200 times.
107664400 for (x = 0; x < w; x++) {
161 106856200 ((uint16_t*)(src + stride*y))[x*pixel_stride] = sample[1][x];
162 }
163 } else {
164 for (x = 0; x < w; x++) {
165 ((uint16_t*)(src + stride*y))[x*pixel_stride] = sample[1][x] << (16 - f->avctx->bits_per_raw_sample) | ((uint16_t **)sample)[1][x] >> (2 * f->avctx->bits_per_raw_sample - 16);
166 }
167 }
168 }
169 }
170 }
171 18300 return 0;
172 }
173
174 6700 static int decode_slice_header(const FFV1Context *f,
175 FFV1SliceContext *sc, AVFrame *frame)
176 {
177 6700 RangeCoder *c = &sc->c;
178 uint8_t state[CONTEXT_SIZE];
179 unsigned ps, context_count;
180 int sx, sy, sw, sh;
181
182 6700 memset(state, 128, sizeof(state));
183 6700 sx = ff_ffv1_get_symbol(c, state, 0);
184 6700 sy = ff_ffv1_get_symbol(c, state, 0);
185 6700 sw = ff_ffv1_get_symbol(c, state, 0) + 1U;
186 6700 sh = ff_ffv1_get_symbol(c, state, 0) + 1U;
187
188
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6700 times.
6700 av_assert0(f->version > 2);
189
190
191
4/8
✓ Branch 0 taken 6700 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6700 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 6700 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 6700 times.
6700 if (sx < 0 || sy < 0 || sw <= 0 || sh <= 0)
192 return AVERROR_INVALIDDATA;
193
2/4
✓ Branch 0 taken 6700 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 6700 times.
6700 if (sx > f->num_h_slices - sw || sy > f->num_v_slices - sh)
194 return AVERROR_INVALIDDATA;
195
196 6700 sc->slice_x = ff_slice_coord(f, f->width , sx , f->num_h_slices, f->chroma_h_shift);
197 6700 sc->slice_y = ff_slice_coord(f, f->height, sy , f->num_v_slices, f->chroma_v_shift);
198 6700 sc->slice_width = ff_slice_coord(f, f->width , sx + sw, f->num_h_slices, f->chroma_h_shift) - sc->slice_x;
199 6700 sc->slice_height = ff_slice_coord(f, f->height, sy + sh, f->num_v_slices, f->chroma_v_shift) - sc->slice_y;
200
201
2/4
✓ Branch 0 taken 6700 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 6700 times.
6700 av_assert0((unsigned)sc->slice_width <= f->width &&
202 (unsigned)sc->slice_height <= f->height);
203
2/4
✓ Branch 0 taken 6700 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 6700 times.
6700 av_assert0 ( (unsigned)sc->slice_x + (uint64_t)sc->slice_width <= f->width
204 && (unsigned)sc->slice_y + (uint64_t)sc->slice_height <= f->height);
205
206
3/4
✓ Branch 0 taken 2700 times.
✓ Branch 1 taken 4000 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2700 times.
6700 if (f->ac == AC_GOLOMB_RICE && sc->slice_width >= (1<<23))
207 return AVERROR_INVALIDDATA;
208
209
2/2
✓ Branch 0 taken 13400 times.
✓ Branch 1 taken 6700 times.
20100 for (unsigned i = 0; i < f->plane_count; i++) {
210 13400 PlaneContext * const p = &sc->plane[i];
211 13400 int idx = ff_ffv1_get_symbol(c, state, 0);
212
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 13400 times.
13400 if (idx >= (unsigned)f->quant_table_count) {
213 av_log(f->avctx, AV_LOG_ERROR, "quant_table_index out of range\n");
214 return -1;
215 }
216 13400 p->quant_table_index = idx;
217 13400 context_count = f->context_count[idx];
218
219
2/2
✓ Branch 0 taken 1344 times.
✓ Branch 1 taken 12056 times.
13400 if (p->context_count < context_count) {
220 1344 av_freep(&p->state);
221 1344 av_freep(&p->vlc_state);
222 }
223 13400 p->context_count = context_count;
224 }
225
226 6700 ps = ff_ffv1_get_symbol(c, state, 0);
227
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6700 times.
6700 if (ps == 1) {
228 frame->flags |= AV_FRAME_FLAG_INTERLACED;
229 frame->flags |= AV_FRAME_FLAG_TOP_FIELD_FIRST;
230
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6700 times.
6700 } else if (ps == 2) {
231 frame->flags |= AV_FRAME_FLAG_INTERLACED;
232 frame->flags &= ~AV_FRAME_FLAG_TOP_FIELD_FIRST;
233
1/2
✓ Branch 0 taken 6700 times.
✗ Branch 1 not taken.
6700 } else if (ps == 3) {
234 6700 frame->flags &= ~AV_FRAME_FLAG_INTERLACED;
235 }
236 6700 frame->sample_aspect_ratio.num = ff_ffv1_get_symbol(c, state, 0);
237 6700 frame->sample_aspect_ratio.den = ff_ffv1_get_symbol(c, state, 0);
238
239
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 6700 times.
6700 if (av_image_check_sar(f->width, f->height,
240 frame->sample_aspect_ratio) < 0) {
241 av_log(f->avctx, AV_LOG_WARNING, "ignoring invalid SAR: %u/%u\n",
242 frame->sample_aspect_ratio.num,
243 frame->sample_aspect_ratio.den);
244 frame->sample_aspect_ratio = (AVRational){ 0, 1 };
245 }
246
247
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6700 times.
6700 if (f->version > 3) {
248 sc->slice_reset_contexts = get_rac(c, state);
249 sc->slice_coding_mode = ff_ffv1_get_symbol(c, state, 0);
250 if (sc->slice_coding_mode != 1 && f->colorspace == 1) {
251 sc->slice_rct_by_coef = ff_ffv1_get_symbol(c, state, 0);
252 sc->slice_rct_ry_coef = ff_ffv1_get_symbol(c, state, 0);
253 if ((uint64_t)sc->slice_rct_by_coef + (uint64_t)sc->slice_rct_ry_coef > 4) {
254 av_log(f->avctx, AV_LOG_ERROR, "slice_rct_y_coef out of range\n");
255 return AVERROR_INVALIDDATA;
256 }
257 }
258 if (f->combined_version >= 0x40004) {
259 sc->remap = ff_ffv1_get_symbol(c, state, 0);
260 if (sc->remap > 2U ||
261 sc->remap && !f->flt) {
262 av_log(f->avctx, AV_LOG_ERROR, "unsupported remap %d\n", sc->remap);
263 return AVERROR_INVALIDDATA;
264 }
265 }
266 }
267
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6700 times.
6700 if (f->avctx->bits_per_raw_sample == 32) {
268 if (!sc->remap) {
269 av_log(f->avctx, AV_LOG_ERROR, "unsupported remap\n");
270 return AVERROR_INVALIDDATA;
271 }
272 }
273
274 6700 return 0;
275 }
276
277 static void slice_set_damaged(FFV1Context *f, FFV1SliceContext *sc)
278 {
279 sc->slice_damaged = 1;
280
281 // only set this for frame threading, as for slice threading its value is
282 // not used and setting it would be a race
283 if (f->avctx->active_thread_type & FF_THREAD_FRAME)
284 f->frame_damaged = 1;
285 }
286
287 static int decode_current_mul(RangeCoder *rc, uint8_t state[32], int *mul, int mul_count, int64_t i)
288 {
289 int ndx = (i * mul_count) >> 32;
290 av_assert2(ndx <= 4096U);
291
292 if (mul[ndx] < 0)
293 mul[ndx] = ff_ffv1_get_symbol(rc, state, 0) & 0x3FFFFFFF;
294
295 return mul[ndx];
296 }
297
298 static int decode_remap(FFV1Context *f, FFV1SliceContext *sc)
299 {
300 unsigned int end = (1LL<<f->avctx->bits_per_raw_sample) - 1;
301 int flip = sc->remap == 2 ? (end>>1) : 0;
302 const int pixel_num = sc->slice_width * sc->slice_height;
303
304 for (int p= 0; p < 1 + 2*f->chroma_planes + f->transparency; p++) {
305 int j = 0;
306 int lu = 0;
307 uint8_t state[2][3][32];
308 int64_t i;
309 int mul[4096+1];
310 int mul_count;
311
312 memset(state, 128, sizeof(state));
313 mul_count = ff_ffv1_get_symbol(&sc->c, state[0][0], 0);
314
315 if (mul_count > 4096U)
316 return AVERROR_INVALIDDATA;
317 for (int i = 0; i<mul_count; i++) {
318 mul[i] = -1;
319
320 }
321 mul[mul_count] = 1;
322
323 memset(state, 128, sizeof(state));
324 int current_mul = 1;
325 for (i=0; i <= end ;) {
326 unsigned run = get_symbol_inline(&sc->c, state[lu][0], 0);
327 unsigned run0 = lu ? 0 : run;
328 unsigned run1 = lu ? run : 1;
329
330 i += run0 * current_mul;
331
332 while (run1--) {
333 if (current_mul > 1) {
334 int delta = get_symbol_inline(&sc->c, state[lu][1], 1);
335 if (delta <= -current_mul || delta > current_mul/2)
336 return AVERROR_INVALIDDATA; //not sure we should check this
337 i += current_mul - 1 + delta;
338 }
339 if (i - 1 >= end)
340 break;
341 if (j >= pixel_num)
342 return AVERROR_INVALIDDATA;
343 if (end <= 0xFFFF) {
344 sc->fltmap [p][j++] = i ^ ((i& 0x8000) ? 0 : flip);
345 } else
346 sc->fltmap32[p][j++] = i ^ ((i&0x80000000) ? 0 : flip);
347 i++;
348 current_mul = decode_current_mul(&sc->c, state[0][2], mul, mul_count, i);
349 }
350 if (lu) {
351 i += current_mul;
352 }
353 lu ^= !run;
354 }
355 sc->remap_count[p] = j;
356 }
357 return 0;
358 }
359
360 7700 static int decode_slice(AVCodecContext *c, void *arg)
361 {
362 7700 FFV1Context *f = c->priv_data;
363 7700 FFV1SliceContext *sc = arg;
364 int width, height, x, y, ret;
365 7700 const int ps = av_pix_fmt_desc_get(f->pix_fmt)->comp[0].step;
366 7700 AVFrame * const p = f->picture.f;
367 7700 const int si = sc - f->slices;
368 GetBitContext gb;
369
3/4
✓ Branch 0 taken 3700 times.
✓ Branch 1 taken 4000 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 3700 times.
7700 int ac = f->ac || sc->slice_coding_mode == 1;
370
371
3/4
✓ Branch 0 taken 6928 times.
✓ Branch 1 taken 772 times.
✓ Branch 2 taken 6928 times.
✗ Branch 3 not taken.
7700 if (!(p->flags & AV_FRAME_FLAG_KEY) && f->last_picture.f)
372 6928 ff_progress_frame_await(&f->last_picture, si);
373
374
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7700 times.
7700 if (f->slice_damaged[si])
375 slice_set_damaged(f, sc);
376
377 7700 sc->slice_rct_by_coef = 1;
378 7700 sc->slice_rct_ry_coef = 1;
379
380
2/2
✓ Branch 0 taken 6700 times.
✓ Branch 1 taken 1000 times.
7700 if (f->version > 2) {
381
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 6700 times.
6700 if (ff_ffv1_init_slice_state(f, sc) < 0)
382 return AVERROR(ENOMEM);
383
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 6700 times.
6700 if (decode_slice_header(f, sc, p) < 0) {
384 sc->slice_x = sc->slice_y = sc->slice_height = sc->slice_width = 0;
385 slice_set_damaged(f, sc);
386 return AVERROR_INVALIDDATA;
387 }
388 }
389
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 7700 times.
7700 if ((ret = ff_ffv1_init_slice_state(f, sc)) < 0)
390 return ret;
391
3/4
✓ Branch 0 taken 6928 times.
✓ Branch 1 taken 772 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 6928 times.
7700 if ((p->flags & AV_FRAME_FLAG_KEY) || sc->slice_reset_contexts) {
392 772 ff_ffv1_clear_slice_state(f, sc);
393
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6928 times.
6928 } else if (sc->slice_damaged) {
394 return AVERROR_INVALIDDATA;
395 }
396
397 7700 width = sc->slice_width;
398 7700 height = sc->slice_height;
399 7700 x = sc->slice_x;
400 7700 y = sc->slice_y;
401
402
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7700 times.
7700 if (sc->remap) {
403 const int pixel_num = sc->slice_width * sc->slice_height;
404
405 for(int p = 0; p < 1 + 2*f->chroma_planes + f->transparency ; p++) {
406 if (f->avctx->bits_per_raw_sample == 32) {
407 av_fast_malloc(&sc->fltmap32[p], &sc->fltmap32_size[p], pixel_num * sizeof(*sc->fltmap32[p]));
408 if (!sc->fltmap32[p])
409 return AVERROR(ENOMEM);
410 } else {
411 av_fast_malloc(&sc->fltmap[p], &sc->fltmap_size[p], pixel_num * sizeof(*sc->fltmap[p]));
412 if (!sc->fltmap[p])
413 return AVERROR(ENOMEM);
414 }
415 }
416
417 ret = decode_remap(f, sc);
418 if (ret < 0)
419 return ret;
420 }
421
422
2/2
✓ Branch 0 taken 3700 times.
✓ Branch 1 taken 4000 times.
7700 if (ac == AC_GOLOMB_RICE) {
423
2/2
✓ Branch 0 taken 2700 times.
✓ Branch 1 taken 1000 times.
3700 if (f->combined_version >= 0x30002)
424 2700 get_rac(&sc->c, (uint8_t[]) { 129 });
425
6/6
✓ Branch 0 taken 1000 times.
✓ Branch 1 taken 2700 times.
✓ Branch 2 taken 600 times.
✓ Branch 3 taken 400 times.
✓ Branch 4 taken 400 times.
✓ Branch 5 taken 200 times.
3700 sc->ac_byte_count = f->version > 2 || (!x && !y) ? sc->c.bytestream - sc->c.bytestream_start - 1 : 0;
426 3700 init_get_bits(&gb,
427 3700 sc->c.bytestream_start + sc->ac_byte_count,
428 3700 (sc->c.bytestream_end - sc->c.bytestream_start - sc->ac_byte_count) * 8);
429 }
430
431 av_assert1(width && height);
432
3/6
✓ Branch 0 taken 6100 times.
✓ Branch 1 taken 1600 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 6100 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
13800 if (f->colorspace == 0 && (f->chroma_planes || !f->transparency)) {
433 6100 const int chroma_width = AV_CEIL_RSHIFT(width, f->chroma_h_shift);
434 6100 const int chroma_height = AV_CEIL_RSHIFT(height, f->chroma_v_shift);
435 6100 const int cx = x >> f->chroma_h_shift;
436 6100 const int cy = y >> f->chroma_v_shift;
437 6100 decode_plane(f, sc, &gb, p->data[0] + ps*x + y*p->linesize[0], width, height, p->linesize[0], 0, 0, 1, ac);
438
439
1/2
✓ Branch 0 taken 6100 times.
✗ Branch 1 not taken.
6100 if (f->chroma_planes) {
440 6100 decode_plane(f, sc, &gb, p->data[1] + ps*cx+cy*p->linesize[1], chroma_width, chroma_height, p->linesize[1], 1, 1, 1, ac);
441 6100 decode_plane(f, sc, &gb, p->data[2] + ps*cx+cy*p->linesize[2], chroma_width, chroma_height, p->linesize[2], 1, 2, 1, ac);
442 }
443
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6100 times.
6100 if (f->transparency)
444 decode_plane(f, sc, &gb, p->data[3] + ps*x + y*p->linesize[3], width, height, p->linesize[3], (f->version >= 4 && !f->chroma_planes) ? 1 : 2,
445 (f->version >= 4 && !f->chroma_planes) ? 1 : 3, 1, ac);
446
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1600 times.
1600 } else if (f->colorspace == 0) {
447 decode_plane(f, sc, &gb, p->data[0] + ps*x + y*p->linesize[0] , width, height, p->linesize[0], 0, 0, 2, ac);
448 decode_plane(f, sc, &gb, p->data[0] + ps*x + y*p->linesize[0] + (ps>>1), width, height, p->linesize[0], 1, 1, 2, ac);
449
2/2
✓ Branch 0 taken 800 times.
✓ Branch 1 taken 800 times.
1600 } else if (f->use32bit) {
450 800 uint8_t *planes[4] = { p->data[0] + ps * x + y * p->linesize[0],
451 800 p->data[1] + ps * x + y * p->linesize[1],
452 800 p->data[2] + ps * x + y * p->linesize[2] };
453
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 800 times.
800 if (f->transparency)
454 planes[3] = p->data[3] + ps * x + y * p->linesize[3];
455 800 decode_rgb_frame32(f, sc, &gb, planes, width, height, p->linesize);
456 } else {
457 800 uint8_t *planes[4] = { p->data[0] + ps * x + y * p->linesize[0] };
458
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 800 times.
800 if (f->avctx->bits_per_raw_sample > 8) {
459 planes[1] = p->data[1] + ps * x + y * p->linesize[1];
460 planes[2] = p->data[2] + ps * x + y * p->linesize[2];
461 if (f->transparency)
462 planes[3] = p->data[3] + ps * x + y * p->linesize[3];
463 }
464 800 decode_rgb_frame(f, sc, &gb, planes, width, height, p->linesize);
465 }
466
3/4
✓ Branch 0 taken 4000 times.
✓ Branch 1 taken 3700 times.
✓ Branch 2 taken 4000 times.
✗ Branch 3 not taken.
7700 if (ac != AC_GOLOMB_RICE && f->version > 2) {
467 int v;
468 4000 get_rac(&sc->c, (uint8_t[]) { 129 });
469
1/2
✓ Branch 0 taken 4000 times.
✗ Branch 1 not taken.
4000 v = sc->c.bytestream_end - sc->c.bytestream - 2 - 5*!!f->ec;
470
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4000 times.
4000 if (v) {
471 av_log(f->avctx, AV_LOG_ERROR, "bytestream end mismatching by %d\n", v);
472 slice_set_damaged(f, sc);
473 }
474 }
475
476
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 7700 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
7700 if (sc->slice_damaged && (f->avctx->err_recognition & AV_EF_EXPLODE))
477 return AVERROR_INVALIDDATA;
478
479
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 7700 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
7700 if ((c->active_thread_type & FF_THREAD_FRAME) && !f->frame_damaged)
480 ff_progress_frame_report(&f->picture, si);
481
482 7700 return 0;
483 }
484
485 87 static enum AVPixelFormat get_pixel_format(FFV1Context *f)
486 {
487 87 enum AVPixelFormat pix_fmts[] = {
488 #if CONFIG_FFV1_VULKAN_HWACCEL
489 AV_PIX_FMT_VULKAN,
490 #endif
491 87 f->pix_fmt,
492 AV_PIX_FMT_NONE,
493 };
494
495 87 return ff_get_format(f->avctx, pix_fmts);
496 }
497
498 253 static int read_header(FFV1Context *f, RangeCoder *c)
499 {
500 uint8_t state[CONTEXT_SIZE];
501 253 int context_count = -1; //-1 to avoid warning
502 int ret;
503
504 253 memset(state, 128, sizeof(state));
505
506 253 ret = ff_ffv1_parse_header(f, c, state);
507
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 253 times.
253 if (ret < 0)
508 return ret;
509
510
2/2
✓ Branch 0 taken 166 times.
✓ Branch 1 taken 87 times.
253 if (f->configured_pix_fmt != f->pix_fmt ||
511
1/2
✓ Branch 0 taken 166 times.
✗ Branch 1 not taken.
166 f->configured_width != f->avctx->width ||
512
1/2
✓ Branch 0 taken 166 times.
✗ Branch 1 not taken.
166 f->configured_height != f->avctx->height ||
513
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 166 times.
166 f->configured_ac != f->ac) {
514 87 f->avctx->pix_fmt = get_pixel_format(f);
515
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 87 times.
87 if (f->avctx->pix_fmt < 0)
516 return AVERROR(EINVAL);
517 87 f->configured_pix_fmt = f->pix_fmt;
518 87 f->configured_width = f->avctx->width;
519 87 f->configured_height = f->avctx->height;
520 87 f->configured_ac = f->ac;
521 }
522
523 ff_dlog(f->avctx, "%d %d %d\n",
524 f->chroma_h_shift, f->chroma_v_shift, f->pix_fmt);
525
2/2
✓ Branch 0 taken 24 times.
✓ Branch 1 taken 229 times.
253 if (f->version < 2) {
526 24 context_count = ff_ffv1_read_quant_tables(c, f->quant_tables[0]);
527
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
24 if (context_count < 0) {
528 av_log(f->avctx, AV_LOG_ERROR, "read_quant_table error\n");
529 return AVERROR_INVALIDDATA;
530 }
531 24 f->slice_count = f->max_slice_count;
532
2/2
✓ Branch 0 taken 24 times.
✓ Branch 1 taken 205 times.
229 } else if (f->version < 3) {
533 24 f->slice_count = ff_ffv1_get_symbol(c, state, 0);
534 } else {
535 205 const uint8_t *p = c->bytestream_end;
536 205 for (f->slice_count = 0;
537
4/6
✓ Branch 0 taken 1035 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1035 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 830 times.
✓ Branch 5 taken 205 times.
1035 f->slice_count < MAX_SLICES && 3 + 5*!!f->ec < p - c->bytestream_start;
538 830 f->slice_count++) {
539
1/2
✓ Branch 0 taken 830 times.
✗ Branch 1 not taken.
830 int trailer = 3 + 5*!!f->ec;
540 830 int size = AV_RB24(p-trailer);
541
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 830 times.
830 if (size + trailer > p - c->bytestream_start)
542 break;
543 830 p -= size + trailer;
544 }
545 }
546
3/6
✓ Branch 0 taken 253 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 253 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 253 times.
253 if (f->slice_count > (unsigned)MAX_SLICES || f->slice_count <= 0 || f->slice_count > f->max_slice_count) {
547 av_log(f->avctx, AV_LOG_ERROR, "slice count %d is invalid (max=%d)\n", f->slice_count, f->max_slice_count);
548 return AVERROR_INVALIDDATA;
549 }
550
551 253 av_refstruct_unref(&f->slice_damaged);
552 253 f->slice_damaged = av_refstruct_allocz(f->slice_count * sizeof(*f->slice_damaged));
553
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 253 times.
253 if (!f->slice_damaged)
554 return AVERROR(ENOMEM);
555
556
2/2
✓ Branch 0 taken 950 times.
✓ Branch 1 taken 253 times.
1203 for (int j = 0; j < f->slice_count; j++) {
557 950 FFV1SliceContext *sc = &f->slices[j];
558
559
2/2
✓ Branch 0 taken 96 times.
✓ Branch 1 taken 854 times.
950 if (f->version == 2) {
560 96 int sx = ff_ffv1_get_symbol(c, state, 0);
561 96 int sy = ff_ffv1_get_symbol(c, state, 0);
562 96 int sw = ff_ffv1_get_symbol(c, state, 0) + 1U;
563 96 int sh = ff_ffv1_get_symbol(c, state, 0) + 1U;
564
565
4/8
✓ Branch 0 taken 96 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 96 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 96 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 96 times.
96 if (sx < 0 || sy < 0 || sw <= 0 || sh <= 0)
566 return AVERROR_INVALIDDATA;
567
2/4
✓ Branch 0 taken 96 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 96 times.
96 if (sx > f->num_h_slices - sw || sy > f->num_v_slices - sh)
568 return AVERROR_INVALIDDATA;
569
570 96 sc->slice_x = sx * (int64_t)f->width / f->num_h_slices;
571 96 sc->slice_y = sy * (int64_t)f->height / f->num_v_slices;
572 96 sc->slice_width = (sx + sw) * (int64_t)f->width / f->num_h_slices - sc->slice_x;
573 96 sc->slice_height = (sy + sh) * (int64_t)f->height / f->num_v_slices - sc->slice_y;
574
575
2/4
✓ Branch 0 taken 96 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 96 times.
96 av_assert0((unsigned)sc->slice_width <= f->width &&
576 (unsigned)sc->slice_height <= f->height);
577
2/4
✓ Branch 0 taken 96 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 96 times.
96 av_assert0 ( (unsigned)sc->slice_x + (uint64_t)sc->slice_width <= f->width
578 && (unsigned)sc->slice_y + (uint64_t)sc->slice_height <= f->height);
579 }
580
581 950 av_refstruct_unref(&sc->plane);
582 950 sc->plane = ff_ffv1_planes_alloc();
583
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 950 times.
950 if (!sc->plane)
584 return AVERROR(ENOMEM);
585
586
2/2
✓ Branch 0 taken 1900 times.
✓ Branch 1 taken 950 times.
2850 for (int i = 0; i < f->plane_count; i++) {
587 1900 PlaneContext *const p = &sc->plane[i];
588
589
2/2
✓ Branch 0 taken 192 times.
✓ Branch 1 taken 1708 times.
1900 if (f->version == 2) {
590 192 int idx = ff_ffv1_get_symbol(c, state, 0);
591
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 192 times.
192 if (idx >= (unsigned)f->quant_table_count) {
592 av_log(f->avctx, AV_LOG_ERROR,
593 "quant_table_index out of range\n");
594 return AVERROR_INVALIDDATA;
595 }
596 192 p->quant_table_index = idx;
597 192 context_count = f->context_count[idx];
598 }
599
600
2/2
✓ Branch 0 taken 240 times.
✓ Branch 1 taken 1660 times.
1900 if (f->version <= 2) {
601
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 240 times.
240 av_assert0(context_count >= 0);
602 240 p->context_count = context_count;
603 }
604 }
605 }
606 253 return 0;
607 }
608
609 88 static av_cold int decode_init(AVCodecContext *avctx)
610 {
611 88 FFV1Context *f = avctx->priv_data;
612 int ret;
613
614 88 f->pix_fmt = AV_PIX_FMT_NONE;
615 88 f->configured_pix_fmt = AV_PIX_FMT_NONE;
616
617
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 88 times.
88 if ((ret = ff_ffv1_common_init(avctx, f)) < 0)
618 return ret;
619
620
3/4
✓ Branch 0 taken 80 times.
✓ Branch 1 taken 8 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 80 times.
88 if (avctx->extradata_size > 0 && (ret = ff_ffv1_read_extra_header(f)) < 0)
621 return ret;
622
623
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 88 times.
88 if ((ret = ff_ffv1_init_slice_contexts(f)) < 0)
624 return ret;
625
626 88 return 0;
627 }
628
629 7700 static int find_next_slice(AVCodecContext *avctx,
630 uint8_t *buf, uint8_t *buf_end, int idx,
631 uint8_t **pos, uint32_t *len)
632 {
633 7700 FFV1Context *f = avctx->priv_data;
634
635 /* Length field */
636 7700 uint32_t v = buf_end - buf;
637
4/4
✓ Branch 0 taken 2075 times.
✓ Branch 1 taken 5625 times.
✓ Branch 2 taken 1675 times.
✓ Branch 3 taken 400 times.
7700 if (idx || f->version > 2) {
638 /* Three bytes of length, plus flush bit + CRC */
639
2/2
✓ Branch 0 taken 6700 times.
✓ Branch 1 taken 600 times.
7300 uint32_t trailer = 3 + 5*!!f->ec;
640
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7300 times.
7300 if (trailer > buf_end - buf)
641 v = INT_MAX;
642 else
643 7300 v = AV_RB24(buf_end - trailer) + trailer;
644 }
645
646
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7700 times.
7700 if (buf_end - buf < v) {
647 av_log(avctx, AV_LOG_ERROR, "Slice pointer chain broken\n");
648 ff_progress_frame_report(&f->picture, INT_MAX);
649 return AVERROR_INVALIDDATA;
650 }
651
652 7700 *len = v;
653
2/2
✓ Branch 0 taken 5625 times.
✓ Branch 1 taken 2075 times.
7700 if (idx)
654 5625 *pos = buf_end - v;
655 else
656 2075 *pos = buf;
657
658 7700 return 0;
659 }
660
661 2120 static int decode_header(AVCodecContext *avctx, RangeCoder *c,
662 uint8_t *buf, size_t buf_size)
663 {
664 int ret;
665 2120 FFV1Context *f = avctx->priv_data;
666
667 2120 uint8_t keystate = 128;
668 2120 ff_init_range_decoder(c, buf, buf_size);
669 2120 ff_build_rac_states(c, 0.05 * (1LL << 32), 256 - 8);
670
671
2/2
✓ Branch 1 taken 253 times.
✓ Branch 2 taken 1867 times.
2120 if (get_rac(c, &keystate)) {
672 253 f->key_frame = AV_FRAME_FLAG_KEY;
673 253 f->key_frame_ok = 0;
674
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 253 times.
253 if ((ret = read_header(f, c)) < 0)
675 return ret;
676 253 f->key_frame_ok = 1;
677 } else {
678
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1867 times.
1867 if (!f->key_frame_ok) {
679 av_log(avctx, AV_LOG_ERROR,
680 "Cannot decode non-keyframe without valid keyframe\n");
681 return AVERROR_INVALIDDATA;
682 }
683 1867 f->key_frame = 0;
684 }
685
686
2/2
✓ Branch 0 taken 1022 times.
✓ Branch 1 taken 1098 times.
2120 if (f->ac != AC_GOLOMB_RICE) {
687
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1022 times.
1022 if (buf_size < avctx->width * avctx->height / (128*8))
688 return AVERROR_INVALIDDATA;
689 } else {
690 1098 int w = avctx->width;
691 1098 int s = 1 + w / (1<<23);
692 int i;
693
694 1098 w /= s;
695
696
2/2
✓ Branch 0 taken 25232 times.
✓ Branch 1 taken 1098 times.
26330 for (i = 0; w > (1<<ff_log2_run[i]); i++)
697 25232 w -= ff_log2_run[i];
698
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1098 times.
1098 if (buf_size < (avctx->height + i + 6) / 8 * s)
699 return AVERROR_INVALIDDATA;
700 }
701
702 2120 return 0;
703 }
704
705 2075 static int decode_slices(AVCodecContext *avctx, RangeCoder c,
706 AVPacket *avpkt)
707 {
708 2075 FFV1Context *f = avctx->priv_data;
709 2075 AVFrame *p = f->picture.f;
710
711 2075 uint8_t *buf = avpkt->data;
712 2075 size_t buf_size = avpkt->size;
713 2075 uint8_t *buf_end = buf + buf_size;
714
715
2/2
✓ Branch 0 taken 7700 times.
✓ Branch 1 taken 2075 times.
9775 for (int i = f->slice_count - 1; i >= 0; i--) {
716 7700 FFV1SliceContext *sc = &f->slices[i];
717
718 uint8_t *pos;
719 uint32_t len;
720 7700 int err = find_next_slice(avctx, buf, buf_end, i,
721 &pos, &len);
722
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7700 times.
7700 if (err < 0)
723 return err;
724
725 7700 buf_end -= len;
726
727 7700 sc->slice_damaged = 0;
728
729
2/2
✓ Branch 0 taken 6700 times.
✓ Branch 1 taken 1000 times.
7700 if (f->ec) {
730 6700 unsigned crc = av_crc(av_crc_get_table(AV_CRC_32_IEEE), f->crcref, pos, len);
731
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6700 times.
6700 if (crc != f->crcref) {
732 int64_t ts = avpkt->pts != AV_NOPTS_VALUE ? avpkt->pts : avpkt->dts;
733 av_log(f->avctx, AV_LOG_ERROR, "slice CRC mismatch %X!", crc);
734 if (ts != AV_NOPTS_VALUE && avctx->pkt_timebase.num) {
735 av_log(f->avctx, AV_LOG_ERROR, "at %f seconds\n", ts*av_q2d(avctx->pkt_timebase));
736 } else if (ts != AV_NOPTS_VALUE) {
737 av_log(f->avctx, AV_LOG_ERROR, "at %"PRId64"\n", ts);
738 } else {
739 av_log(f->avctx, AV_LOG_ERROR, "\n");
740 }
741 slice_set_damaged(f, sc);
742 }
743
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6700 times.
6700 if (avctx->debug & FF_DEBUG_PICT_INFO) {
744 av_log(avctx, AV_LOG_DEBUG, "slice %d, CRC: 0x%08"PRIX32"\n", i, AV_RB32(pos + len - 4));
745 }
746 }
747
748
2/2
✓ Branch 0 taken 5625 times.
✓ Branch 1 taken 2075 times.
7700 if (i) {
749 5625 ff_init_range_decoder(&sc->c, pos, len);
750 5625 ff_build_rac_states(&sc->c, 0.05 * (1LL << 32), 256 - 8);
751 } else {
752 2075 sc->c = c;
753 2075 sc->c.bytestream_end = pos + len;
754 }
755 }
756
757 2075 avctx->execute(avctx,
758 decode_slice,
759 2075 f->slices,
760 NULL,
761 f->slice_count,
762 sizeof(*f->slices));
763
764
2/2
✓ Branch 0 taken 7700 times.
✓ Branch 1 taken 2075 times.
9775 for (int i = f->slice_count - 1; i >= 0; i--) {
765 7700 FFV1SliceContext *sc = &f->slices[i];
766
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 7700 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
7700 if (sc->slice_damaged && f->last_picture.f) {
767 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(f->pix_fmt);
768 const uint8_t *src[4];
769 uint8_t *dst[4];
770 ff_progress_frame_await(&f->last_picture, INT_MAX);
771 for (int j = 0; j < desc->nb_components; j++) {
772 int pixshift = desc->comp[j].depth > 8;
773 int sh = (j == 1 || j == 2) ? f->chroma_h_shift : 0;
774 int sv = (j == 1 || j == 2) ? f->chroma_v_shift : 0;
775 dst[j] = p->data[j] + p->linesize[j] *
776 (sc->slice_y >> sv) + ((sc->slice_x >> sh) << pixshift);
777 src[j] = f->last_picture.f->data[j] + f->last_picture.f->linesize[j] *
778 (sc->slice_y >> sv) + ((sc->slice_x >> sh) << pixshift);
779
780 }
781
782 av_image_copy(dst, p->linesize, src,
783 f->last_picture.f->linesize,
784 f->pix_fmt,
785 sc->slice_width,
786 sc->slice_height);
787
788 f->slice_damaged[i] = 1;
789 }
790 }
791
792 2075 return 0;
793 }
794
795 2120 static int decode_frame(AVCodecContext *avctx, AVFrame *rframe,
796 int *got_frame, AVPacket *avpkt)
797 {
798 2120 FFV1Context *f = avctx->priv_data;
799 int ret;
800 AVFrame *p;
801 2120 const FFHWAccel *hwaccel = NULL;
802
803 /* This is copied onto the first slice's range coder context */
804 RangeCoder c;
805
806 2120 ff_progress_frame_unref(&f->last_picture);
807 2120 av_refstruct_unref(&f->hwaccel_last_picture_private);
808
809 2120 FFSWAP(ProgressFrame, f->picture, f->last_picture);
810 2120 FFSWAP(void *, f->hwaccel_picture_private, f->hwaccel_last_picture_private);
811
812 2120 f->avctx = avctx;
813 2120 f->frame_damaged = 0;
814
815 2120 ret = decode_header(avctx, &c, avpkt->data, avpkt->size);
816
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2120 times.
2120 if (ret < 0)
817 return ret;
818
819
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2120 times.
2120 if (avctx->debug & FF_DEBUG_PICT_INFO)
820 av_log(avctx, AV_LOG_DEBUG, "ver:%d keyframe:%d coder:%d ec:%d slices:%d bps:%d\n",
821 f->version, !!f->key_frame, f->ac, f->ec, f->slice_count, f->avctx->bits_per_raw_sample);
822
823
2/2
✓ Branch 0 taken 45 times.
✓ Branch 1 taken 2075 times.
2120 if (avctx->skip_frame >= AVDISCARD_ALL)
824 45 return avpkt->size;
825
826
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2075 times.
2075 if (avctx->hwaccel)
827 hwaccel = ffhwaccel(avctx->hwaccel);
828
829 2075 ret = ff_progress_frame_get_buffer(avctx, &f->picture,
830 AV_GET_BUFFER_FLAG_REF);
831
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2075 times.
2075 if (ret < 0)
832 return ret;
833
834 2075 ret = ff_hwaccel_frame_priv_alloc(avctx, &f->hwaccel_picture_private);
835
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2075 times.
2075 if (ret < 0)
836 return ret;
837
838 2075 p = f->picture.f;
839
840 2075 p->pict_type = AV_PICTURE_TYPE_I; //FIXME I vs. P
841 2075 p->flags = (p->flags & ~AV_FRAME_FLAG_KEY) | f->key_frame;
842
843
3/4
✓ Branch 0 taken 400 times.
✓ Branch 1 taken 1675 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 400 times.
2075 if (f->version < 3 && avctx->field_order > AV_FIELD_PROGRESSIVE) {
844 /* we have interlaced material flagged in container */
845 p->flags |= AV_FRAME_FLAG_INTERLACED;
846 if (avctx->field_order == AV_FIELD_TT || avctx->field_order == AV_FIELD_TB)
847 p->flags |= AV_FRAME_FLAG_TOP_FIELD_FIRST;
848 }
849
850 /* Start */
851
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2075 times.
2075 if (hwaccel) {
852 ret = hwaccel->start_frame(avctx, avpkt->buf, avpkt->data, avpkt->size);
853 if (ret < 0)
854 return ret;
855 }
856
857 2075 ff_thread_finish_setup(avctx);
858
859 /* Decode slices */
860
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2075 times.
2075 if (hwaccel) {
861 uint8_t *buf_end = avpkt->data + avpkt->size;
862
863 if (!(p->flags & AV_FRAME_FLAG_KEY) && f->last_picture.f)
864 ff_progress_frame_await(&f->last_picture, f->slice_count - 1);
865
866 for (int i = f->slice_count - 1; i >= 0; i--) {
867 uint8_t *pos;
868 uint32_t len;
869 ret = find_next_slice(avctx, avpkt->data, buf_end, i,
870 &pos, &len);
871 if (ret < 0)
872 return ret;
873
874 buf_end -= len;
875
876 ret = hwaccel->decode_slice(avctx, pos, len);
877 if (ret < 0)
878 return ret;
879 }
880 } else {
881 2075 ret = decode_slices(avctx, c, avpkt);
882
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2075 times.
2075 if (ret < 0)
883 return ret;
884 }
885
886 /* Finalize */
887
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2075 times.
2075 if (hwaccel) {
888 ret = hwaccel->end_frame(avctx);
889 if (ret < 0)
890 return ret;
891 }
892
893 2075 ff_progress_frame_report(&f->picture, INT_MAX);
894
895 2075 ff_progress_frame_unref(&f->last_picture);
896 2075 av_refstruct_unref(&f->hwaccel_last_picture_private);
897
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2075 times.
2075 if ((ret = av_frame_ref(rframe, f->picture.f)) < 0)
898 return ret;
899
900 2075 *got_frame = 1;
901
902 2075 return avpkt->size;
903 }
904
905 #if HAVE_THREADS
906 static int update_thread_context(AVCodecContext *dst, const AVCodecContext *src)
907 {
908 FFV1Context *fsrc = src->priv_data;
909 FFV1Context *fdst = dst->priv_data;
910
911 if (dst == src)
912 return 0;
913
914 fdst->version = fsrc->version;
915 fdst->micro_version = fsrc->micro_version;
916 fdst->combined_version = fsrc->combined_version;
917 fdst->chroma_planes = fsrc->chroma_planes;
918 fdst->chroma_h_shift = fsrc->chroma_h_shift;
919 fdst->chroma_v_shift = fsrc->chroma_v_shift;
920 fdst->transparency = fsrc->transparency;
921 fdst->plane_count = fsrc->plane_count;
922 fdst->ac = fsrc->ac;
923 fdst->colorspace = fsrc->colorspace;
924 fdst->pix_fmt = fsrc->pix_fmt;
925 fdst->configured_pix_fmt = fsrc->configured_pix_fmt;
926 fdst->configured_ac = fsrc->configured_ac;
927
928 fdst->ec = fsrc->ec;
929 fdst->intra = fsrc->intra;
930 fdst->key_frame_ok = fsrc->key_frame_ok;
931
932 fdst->packed_at_lsb = fsrc->packed_at_lsb;
933 fdst->slice_count = fsrc->slice_count;
934 fdst->use32bit = fsrc->use32bit;
935 memcpy(fdst->state_transition, fsrc->state_transition,
936 sizeof(fdst->state_transition));
937
938 // in version 1 there is a single per-keyframe quant table, so
939 // we need to propagate it between threads
940 if (fsrc->version < 2)
941 memcpy(fdst->quant_tables[0], fsrc->quant_tables[0], sizeof(fsrc->quant_tables[0]));
942
943 for (int i = 0; i < fdst->num_h_slices * fdst->num_v_slices; i++) {
944 FFV1SliceContext *sc = &fdst->slices[i];
945 const FFV1SliceContext *sc0 = &fsrc->slices[i];
946
947 av_refstruct_replace(&sc->plane, sc0->plane);
948
949 if (fsrc->version < 3) {
950 sc->slice_x = sc0->slice_x;
951 sc->slice_y = sc0->slice_y;
952 sc->slice_width = sc0->slice_width;
953 sc->slice_height = sc0->slice_height;
954 }
955 }
956
957 av_refstruct_replace(&fdst->slice_damaged, fsrc->slice_damaged);
958
959 av_assert1(fdst->max_slice_count == fsrc->max_slice_count);
960
961 ff_progress_frame_replace(&fdst->picture, &fsrc->picture);
962 av_refstruct_replace(&fdst->hwaccel_picture_private,
963 fsrc->hwaccel_picture_private);
964
965 return 0;
966 }
967 #endif
968
969 88 static av_cold int ffv1_decode_close(AVCodecContext *avctx)
970 {
971 88 FFV1Context *const s = avctx->priv_data;
972
973 88 ff_progress_frame_unref(&s->picture);
974 88 av_refstruct_unref(&s->hwaccel_picture_private);
975
976 88 ff_progress_frame_unref(&s->last_picture);
977 88 av_refstruct_unref(&s->hwaccel_last_picture_private);
978
979 88 ff_ffv1_close(s);
980
981 88 return 0;
982 }
983
984 const FFCodec ff_ffv1_decoder = {
985 .p.name = "ffv1",
986 CODEC_LONG_NAME("FFmpeg video codec #1"),
987 .p.type = AVMEDIA_TYPE_VIDEO,
988 .p.id = AV_CODEC_ID_FFV1,
989 .priv_data_size = sizeof(FFV1Context),
990 .init = decode_init,
991 .close = ffv1_decode_close,
992 FF_CODEC_DECODE_CB(decode_frame),
993 UPDATE_THREAD_CONTEXT(update_thread_context),
994 .p.capabilities = AV_CODEC_CAP_DR1 |
995 AV_CODEC_CAP_FRAME_THREADS | AV_CODEC_CAP_SLICE_THREADS,
996 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP |
997 FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM |
998 FF_CODEC_CAP_USES_PROGRESSFRAMES,
999 .hw_configs = (const AVCodecHWConfigInternal *const []) {
1000 #if CONFIG_FFV1_VULKAN_HWACCEL
1001 HWACCEL_VULKAN(ffv1),
1002 #endif
1003 NULL
1004 },
1005 };
1006