FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/ffv1dec.c
Date: 2026-11-08 03:48:22
Exec Total Coverage
Lines: 382 616 62.0%
Functions: 15 18 83.3%
Branches: 241 460 52.4%

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