FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/ffv1dec.c
Date: 2025-07-16 17:38:15
Exec Total Coverage
Lines: 312 552 56.5%
Functions: 13 17 76.5%
Branches: 183 416 44.0%

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