FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/ffv1dec.c
Date: 2025-03-08 20:38:41
Exec Total Coverage
Lines: 289 433 66.7%
Functions: 13 15 86.7%
Branches: 167 310 53.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
45 167385877 static inline int get_vlc_symbol(GetBitContext *gb, VlcState *const state,
46 int bits)
47 {
48 int k, i, v, ret;
49
50 167385877 i = state->count;
51 167385877 k = 0;
52
2/2
✓ Branch 0 taken 280052558 times.
✓ Branch 1 taken 167385877 times.
447438435 while (i < state->error_sum) { // FIXME: optimize
53 280052558 k++;
54 280052558 i += i;
55 }
56
57 167385877 v = get_sr_golomb(gb, k, 12, bits);
58 ff_dlog(NULL, "v:%d bias:%d error:%d drift:%d count:%d k:%d",
59 v, state->bias, state->error_sum, state->drift, state->count, k);
60
61 167385877 v ^= ((2 * state->drift + state->count) >> 31);
62
63 167385877 ret = fold(v + state->bias, bits);
64
65 167385877 update_vlc_state(state, v);
66
67 167385877 return ret;
68 }
69
70 4596384 static int is_input_end(RangeCoder *c, GetBitContext *gb, int ac)
71 {
72
2/2
✓ Branch 0 taken 2582376 times.
✓ Branch 1 taken 2014008 times.
4596384 if (ac != AC_GOLOMB_RICE) {
73
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2582376 times.
2582376 if (c->overread > MAX_OVERREAD)
74 return AVERROR_INVALIDDATA;
75 } else {
76
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2014008 times.
2014008 if (get_bits_left(gb) < 1)
77 return AVERROR_INVALIDDATA;
78 }
79 4596384 return 0;
80 }
81
82 #define TYPE int16_t
83 #define RENAME(name) name
84 #include "ffv1dec_template.c"
85 #undef TYPE
86 #undef RENAME
87
88 #define TYPE int32_t
89 #define RENAME(name) name ## 32
90 #include "ffv1dec_template.c"
91
92 18249 static int decode_plane(FFV1Context *f, FFV1SliceContext *sc,
93 GetBitContext *gb,
94 uint8_t *src, int w, int h, int stride, int plane_index,
95 int pixel_stride, int ac)
96 {
97 int x, y;
98 int16_t *sample[2];
99 18249 sample[0] = sc->sample_buffer + 3;
100 18249 sample[1] = sc->sample_buffer + w + 6 + 3;
101
102 18249 sc->run_index = 0;
103
104 18249 memset(sc->sample_buffer, 0, 2 * (w + 6) * sizeof(*sc->sample_buffer));
105
106
2/2
✓ Branch 0 taken 1748616 times.
✓ Branch 1 taken 18249 times.
1766865 for (y = 0; y < h; y++) {
107 1748616 int16_t *temp = sample[0]; // FIXME: try a normal buffer
108
109 1748616 sample[0] = sample[1];
110 1748616 sample[1] = temp;
111
112 1748616 sample[1][-1] = sample[0][0];
113 1748616 sample[0][w] = sample[0][w - 1];
114
115
2/2
✓ Branch 0 taken 915612 times.
✓ Branch 1 taken 833004 times.
1748616 if (f->avctx->bits_per_raw_sample <= 8) {
116 915612 int ret = decode_line(f, sc, gb, w, sample, plane_index, 8, ac);
117
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 915612 times.
915612 if (ret < 0)
118 return ret;
119
2/2
✓ Branch 0 taken 148622082 times.
✓ Branch 1 taken 915612 times.
149537694 for (x = 0; x < w; x++)
120 148622082 src[x*pixel_stride + stride * y] = sample[1][x];
121 } else {
122 833004 int ret = decode_line(f, sc, gb, w, sample, plane_index, f->avctx->bits_per_raw_sample, ac);
123
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 833004 times.
833004 if (ret < 0)
124 return ret;
125
1/2
✓ Branch 0 taken 833004 times.
✗ Branch 1 not taken.
833004 if (f->packed_at_lsb) {
126
2/2
✓ Branch 0 taken 112679724 times.
✓ Branch 1 taken 833004 times.
113512728 for (x = 0; x < w; x++) {
127 112679724 ((uint16_t*)(src + stride*y))[x*pixel_stride] = sample[1][x];
128 }
129 } else {
130 for (x = 0; x < w; x++) {
131 ((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);
132 }
133 }
134 }
135 }
136 18249 return 0;
137 }
138
139 6644 static int decode_slice_header(const FFV1Context *f,
140 FFV1SliceContext *sc, AVFrame *frame)
141 {
142 6644 RangeCoder *c = &sc->c;
143 uint8_t state[CONTEXT_SIZE];
144 unsigned ps, context_count;
145 int sx, sy, sw, sh;
146
147 6644 memset(state, 128, sizeof(state));
148 6644 sx = ff_ffv1_get_symbol(c, state, 0);
149 6644 sy = ff_ffv1_get_symbol(c, state, 0);
150 6644 sw = ff_ffv1_get_symbol(c, state, 0) + 1U;
151 6644 sh = ff_ffv1_get_symbol(c, state, 0) + 1U;
152
153
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6644 times.
6644 av_assert0(f->version > 2);
154
155
156
4/8
✓ Branch 0 taken 6644 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6644 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 6644 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 6644 times.
6644 if (sx < 0 || sy < 0 || sw <= 0 || sh <= 0)
157 return AVERROR_INVALIDDATA;
158
2/4
✓ Branch 0 taken 6644 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 6644 times.
6644 if (sx > f->num_h_slices - sw || sy > f->num_v_slices - sh)
159 return AVERROR_INVALIDDATA;
160
161 6644 sc->slice_x = ff_slice_coord(f, f->width , sx , f->num_h_slices, f->chroma_h_shift);
162 6644 sc->slice_y = ff_slice_coord(f, f->height, sy , f->num_v_slices, f->chroma_v_shift);
163 6644 sc->slice_width = ff_slice_coord(f, f->width , sx + sw, f->num_h_slices, f->chroma_h_shift) - sc->slice_x;
164 6644 sc->slice_height = ff_slice_coord(f, f->height, sy + sh, f->num_v_slices, f->chroma_v_shift) - sc->slice_y;
165
166
2/4
✓ Branch 0 taken 6644 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 6644 times.
6644 av_assert0((unsigned)sc->slice_width <= f->width &&
167 (unsigned)sc->slice_height <= f->height);
168
2/4
✓ Branch 0 taken 6644 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 6644 times.
6644 av_assert0 ( (unsigned)sc->slice_x + (uint64_t)sc->slice_width <= f->width
169 && (unsigned)sc->slice_y + (uint64_t)sc->slice_height <= f->height);
170
171
3/4
✓ Branch 0 taken 2556 times.
✓ Branch 1 taken 4088 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2556 times.
6644 if (f->ac == AC_GOLOMB_RICE && sc->slice_width >= (1<<23))
172 return AVERROR_INVALIDDATA;
173
174
2/2
✓ Branch 0 taken 13288 times.
✓ Branch 1 taken 6644 times.
19932 for (unsigned i = 0; i < f->plane_count; i++) {
175 13288 PlaneContext * const p = &sc->plane[i];
176 13288 int idx = ff_ffv1_get_symbol(c, state, 0);
177
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 13288 times.
13288 if (idx >= (unsigned)f->quant_table_count) {
178 av_log(f->avctx, AV_LOG_ERROR, "quant_table_index out of range\n");
179 return -1;
180 }
181 13288 p->quant_table_index = idx;
182 13288 context_count = f->context_count[idx];
183
184
2/2
✓ Branch 0 taken 1592 times.
✓ Branch 1 taken 11696 times.
13288 if (p->context_count < context_count) {
185 1592 av_freep(&p->state);
186 1592 av_freep(&p->vlc_state);
187 }
188 13288 p->context_count = context_count;
189 }
190
191 6644 ps = ff_ffv1_get_symbol(c, state, 0);
192
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6644 times.
6644 if (ps == 1) {
193 frame->flags |= AV_FRAME_FLAG_INTERLACED;
194 frame->flags |= AV_FRAME_FLAG_TOP_FIELD_FIRST;
195
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6644 times.
6644 } else if (ps == 2) {
196 frame->flags |= AV_FRAME_FLAG_INTERLACED;
197 frame->flags &= ~AV_FRAME_FLAG_TOP_FIELD_FIRST;
198
1/2
✓ Branch 0 taken 6644 times.
✗ Branch 1 not taken.
6644 } else if (ps == 3) {
199 6644 frame->flags &= ~AV_FRAME_FLAG_INTERLACED;
200 }
201 6644 frame->sample_aspect_ratio.num = ff_ffv1_get_symbol(c, state, 0);
202 6644 frame->sample_aspect_ratio.den = ff_ffv1_get_symbol(c, state, 0);
203
204
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 6644 times.
6644 if (av_image_check_sar(f->width, f->height,
205 frame->sample_aspect_ratio) < 0) {
206 av_log(f->avctx, AV_LOG_WARNING, "ignoring invalid SAR: %u/%u\n",
207 frame->sample_aspect_ratio.num,
208 frame->sample_aspect_ratio.den);
209 frame->sample_aspect_ratio = (AVRational){ 0, 1 };
210 }
211
212
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6644 times.
6644 if (f->version > 3) {
213 sc->slice_reset_contexts = get_rac(c, state);
214 sc->slice_coding_mode = ff_ffv1_get_symbol(c, state, 0);
215 if (sc->slice_coding_mode != 1 && f->colorspace == 1) {
216 sc->slice_rct_by_coef = ff_ffv1_get_symbol(c, state, 0);
217 sc->slice_rct_ry_coef = ff_ffv1_get_symbol(c, state, 0);
218 if ((uint64_t)sc->slice_rct_by_coef + (uint64_t)sc->slice_rct_ry_coef > 4) {
219 av_log(f->avctx, AV_LOG_ERROR, "slice_rct_y_coef out of range\n");
220 return AVERROR_INVALIDDATA;
221 }
222 }
223 if (f->combined_version >= 0x40004) {
224 sc->remap = ff_ffv1_get_symbol(c, state, 0);
225 if (sc->remap > 1U ||
226 sc->remap == 1 && !f->flt) {
227 av_log(f->avctx, AV_LOG_ERROR, "unsupported remap %d\n", sc->remap);
228 return AVERROR_INVALIDDATA;
229 }
230 }
231 }
232
233 6644 return 0;
234 }
235
236 static void slice_set_damaged(FFV1Context *f, FFV1SliceContext *sc)
237 {
238 sc->slice_damaged = 1;
239
240 // only set this for frame threading, as for slice threading its value is
241 // not used and setting it would be a race
242 if (f->avctx->active_thread_type & FF_THREAD_FRAME)
243 f->frame_damaged = 1;
244 }
245
246 7715 static int decode_slice(AVCodecContext *c, void *arg)
247 {
248 7715 FFV1Context *f = c->priv_data;
249 7715 FFV1SliceContext *sc = arg;
250 int width, height, x, y, ret;
251 7715 const int ps = av_pix_fmt_desc_get(f->pix_fmt)->comp[0].step;
252 7715 AVFrame * const p = f->picture.f;
253 7715 const int si = sc - f->slices;
254 GetBitContext gb;
255
3/4
✓ Branch 0 taken 3627 times.
✓ Branch 1 taken 4088 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 3627 times.
7715 int ac = f->ac || sc->slice_coding_mode == 1;
256
257
3/4
✓ Branch 0 taken 6793 times.
✓ Branch 1 taken 922 times.
✓ Branch 2 taken 6793 times.
✗ Branch 3 not taken.
7715 if (!(p->flags & AV_FRAME_FLAG_KEY) && f->last_picture.f)
258 6793 ff_progress_frame_await(&f->last_picture, si);
259
260
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7715 times.
7715 if (f->slice_damaged[si])
261 slice_set_damaged(f, sc);
262
263 7715 sc->slice_rct_by_coef = 1;
264 7715 sc->slice_rct_ry_coef = 1;
265
266
2/2
✓ Branch 0 taken 6644 times.
✓ Branch 1 taken 1071 times.
7715 if (f->version > 2) {
267
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 6644 times.
6644 if (ff_ffv1_init_slice_state(f, sc) < 0)
268 return AVERROR(ENOMEM);
269
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 6644 times.
6644 if (decode_slice_header(f, sc, p) < 0) {
270 sc->slice_x = sc->slice_y = sc->slice_height = sc->slice_width = 0;
271 slice_set_damaged(f, sc);
272 return AVERROR_INVALIDDATA;
273 }
274 }
275
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 7715 times.
7715 if ((ret = ff_ffv1_init_slice_state(f, sc)) < 0)
276 return ret;
277
3/4
✓ Branch 0 taken 6793 times.
✓ Branch 1 taken 922 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 6793 times.
7715 if ((p->flags & AV_FRAME_FLAG_KEY) || sc->slice_reset_contexts) {
278 922 ff_ffv1_clear_slice_state(f, sc);
279
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6793 times.
6793 } else if (sc->slice_damaged) {
280 return AVERROR_INVALIDDATA;
281 }
282
283 7715 width = sc->slice_width;
284 7715 height = sc->slice_height;
285 7715 x = sc->slice_x;
286 7715 y = sc->slice_y;
287
288
2/2
✓ Branch 0 taken 3627 times.
✓ Branch 1 taken 4088 times.
7715 if (ac == AC_GOLOMB_RICE) {
289
2/2
✓ Branch 0 taken 2556 times.
✓ Branch 1 taken 1071 times.
3627 if (f->combined_version >= 0x30002)
290 2556 get_rac(&sc->c, (uint8_t[]) { 129 });
291
6/6
✓ Branch 0 taken 1071 times.
✓ Branch 1 taken 2556 times.
✓ Branch 2 taken 663 times.
✓ Branch 3 taken 408 times.
✓ Branch 4 taken 459 times.
✓ Branch 5 taken 204 times.
3627 sc->ac_byte_count = f->version > 2 || (!x && !y) ? sc->c.bytestream - sc->c.bytestream_start - 1 : 0;
292 3627 init_get_bits(&gb,
293 3627 sc->c.bytestream_start + sc->ac_byte_count,
294 3627 (sc->c.bytestream_end - sc->c.bytestream_start - sc->ac_byte_count) * 8);
295 }
296
297 av_assert1(width && height);
298
3/6
✓ Branch 0 taken 6083 times.
✓ Branch 1 taken 1632 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 6083 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
13798 if (f->colorspace == 0 && (f->chroma_planes || !f->transparency)) {
299 6083 const int chroma_width = AV_CEIL_RSHIFT(width, f->chroma_h_shift);
300 6083 const int chroma_height = AV_CEIL_RSHIFT(height, f->chroma_v_shift);
301 6083 const int cx = x >> f->chroma_h_shift;
302 6083 const int cy = y >> f->chroma_v_shift;
303 6083 decode_plane(f, sc, &gb, p->data[0] + ps*x + y*p->linesize[0], width, height, p->linesize[0], 0, 1, ac);
304
305
1/2
✓ Branch 0 taken 6083 times.
✗ Branch 1 not taken.
6083 if (f->chroma_planes) {
306 6083 decode_plane(f, sc, &gb, p->data[1] + ps*cx+cy*p->linesize[1], chroma_width, chroma_height, p->linesize[1], 1, 1, ac);
307 6083 decode_plane(f, sc, &gb, p->data[2] + ps*cx+cy*p->linesize[2], chroma_width, chroma_height, p->linesize[2], 1, 1, ac);
308 }
309
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6083 times.
6083 if (f->transparency)
310 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, 1, ac);
311
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1632 times.
1632 } else if (f->colorspace == 0) {
312 decode_plane(f, sc, &gb, p->data[0] + ps*x + y*p->linesize[0] , width, height, p->linesize[0], 0, 2, ac);
313 decode_plane(f, sc, &gb, p->data[0] + ps*x + y*p->linesize[0] + 1, width, height, p->linesize[0], 1, 2, ac);
314
2/2
✓ Branch 0 taken 816 times.
✓ Branch 1 taken 816 times.
1632 } else if (f->use32bit) {
315 816 uint8_t *planes[4] = { p->data[0] + ps * x + y * p->linesize[0],
316 816 p->data[1] + ps * x + y * p->linesize[1],
317 816 p->data[2] + ps * x + y * p->linesize[2],
318 816 p->data[3] + ps * x + y * p->linesize[3] };
319 816 decode_rgb_frame32(f, sc, &gb, planes, width, height, p->linesize);
320 } else {
321 816 uint8_t *planes[4] = { p->data[0] + ps * x + y * p->linesize[0],
322 816 p->data[1] + ps * x + y * p->linesize[1],
323 816 p->data[2] + ps * x + y * p->linesize[2],
324 816 p->data[3] + ps * x + y * p->linesize[3] };
325 816 decode_rgb_frame(f, sc, &gb, planes, width, height, p->linesize);
326 }
327
3/4
✓ Branch 0 taken 4088 times.
✓ Branch 1 taken 3627 times.
✓ Branch 2 taken 4088 times.
✗ Branch 3 not taken.
7715 if (ac != AC_GOLOMB_RICE && f->version > 2) {
328 int v;
329 4088 get_rac(&sc->c, (uint8_t[]) { 129 });
330
1/2
✓ Branch 0 taken 4088 times.
✗ Branch 1 not taken.
4088 v = sc->c.bytestream_end - sc->c.bytestream - 2 - 5*!!f->ec;
331
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4088 times.
4088 if (v) {
332 av_log(f->avctx, AV_LOG_ERROR, "bytestream end mismatching by %d\n", v);
333 slice_set_damaged(f, sc);
334 }
335 }
336
337
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 7715 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
7715 if (sc->slice_damaged && (f->avctx->err_recognition & AV_EF_EXPLODE))
338 return AVERROR_INVALIDDATA;
339
340
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 7715 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
7715 if ((c->active_thread_type & FF_THREAD_FRAME) && !f->frame_damaged)
341 ff_progress_frame_report(&f->picture, si);
342
343 7715 return 0;
344 }
345
346 253 static enum AVPixelFormat get_pixel_format(FFV1Context *f)
347 {
348 253 enum AVPixelFormat pix_fmts[] = {
349 253 f->pix_fmt,
350 AV_PIX_FMT_NONE,
351 };
352
353 253 return ff_get_format(f->avctx, pix_fmts);
354 }
355
356 253 static int read_header(FFV1Context *f, RangeCoder *c)
357 {
358 uint8_t state[CONTEXT_SIZE];
359 253 int context_count = -1; //-1 to avoid warning
360 int ret;
361
362 253 memset(state, 128, sizeof(state));
363
364 253 ret = ff_ffv1_parse_header(f, c, state);
365
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 253 times.
253 if (ret < 0)
366 return ret;
367
368 253 f->avctx->pix_fmt = get_pixel_format(f);
369
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 253 times.
253 if (f->avctx->pix_fmt < 0)
370 return AVERROR(EINVAL);
371
372 ff_dlog(f->avctx, "%d %d %d\n",
373 f->chroma_h_shift, f->chroma_v_shift, f->pix_fmt);
374
2/2
✓ Branch 0 taken 30 times.
✓ Branch 1 taken 223 times.
253 if (f->version < 2) {
375 30 context_count = ff_ffv1_read_quant_tables(c, f->quant_tables[0]);
376
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 30 times.
30 if (context_count < 0) {
377 av_log(f->avctx, AV_LOG_ERROR, "read_quant_table error\n");
378 return AVERROR_INVALIDDATA;
379 }
380 30 f->slice_count = f->max_slice_count;
381
2/2
✓ Branch 0 taken 24 times.
✓ Branch 1 taken 199 times.
223 } else if (f->version < 3) {
382 24 f->slice_count = ff_ffv1_get_symbol(c, state, 0);
383 } else {
384 199 const uint8_t *p = c->bytestream_end;
385 199 for (f->slice_count = 0;
386
4/6
✓ Branch 0 taken 995 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 995 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 796 times.
✓ Branch 5 taken 199 times.
995 f->slice_count < MAX_SLICES && 3 + 5*!!f->ec < p - c->bytestream_start;
387 796 f->slice_count++) {
388
1/2
✓ Branch 0 taken 796 times.
✗ Branch 1 not taken.
796 int trailer = 3 + 5*!!f->ec;
389 796 int size = AV_RB24(p-trailer);
390
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 796 times.
796 if (size + trailer > p - c->bytestream_start)
391 break;
392 796 p -= size + trailer;
393 }
394 }
395
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) {
396 av_log(f->avctx, AV_LOG_ERROR, "slice count %d is invalid (max=%d)\n", f->slice_count, f->max_slice_count);
397 return AVERROR_INVALIDDATA;
398 }
399
400 253 av_refstruct_unref(&f->slice_damaged);
401 253 f->slice_damaged = av_refstruct_allocz(f->slice_count * sizeof(*f->slice_damaged));
402
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 253 times.
253 if (!f->slice_damaged)
403 return AVERROR(ENOMEM);
404
405
2/2
✓ Branch 0 taken 922 times.
✓ Branch 1 taken 253 times.
1175 for (int j = 0; j < f->slice_count; j++) {
406 922 FFV1SliceContext *sc = &f->slices[j];
407
408
2/2
✓ Branch 0 taken 96 times.
✓ Branch 1 taken 826 times.
922 if (f->version == 2) {
409 96 int sx = ff_ffv1_get_symbol(c, state, 0);
410 96 int sy = ff_ffv1_get_symbol(c, state, 0);
411 96 int sw = ff_ffv1_get_symbol(c, state, 0) + 1U;
412 96 int sh = ff_ffv1_get_symbol(c, state, 0) + 1U;
413
414
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)
415 return AVERROR_INVALIDDATA;
416
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)
417 return AVERROR_INVALIDDATA;
418
419 96 sc->slice_x = sx * (int64_t)f->width / f->num_h_slices;
420 96 sc->slice_y = sy * (int64_t)f->height / f->num_v_slices;
421 96 sc->slice_width = (sx + sw) * (int64_t)f->width / f->num_h_slices - sc->slice_x;
422 96 sc->slice_height = (sy + sh) * (int64_t)f->height / f->num_v_slices - sc->slice_y;
423
424
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 &&
425 (unsigned)sc->slice_height <= f->height);
426
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
427 && (unsigned)sc->slice_y + (uint64_t)sc->slice_height <= f->height);
428 }
429
430 922 av_refstruct_unref(&sc->plane);
431 922 sc->plane = ff_ffv1_planes_alloc();
432
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 922 times.
922 if (!sc->plane)
433 return AVERROR(ENOMEM);
434
435
2/2
✓ Branch 0 taken 1844 times.
✓ Branch 1 taken 922 times.
2766 for (int i = 0; i < f->plane_count; i++) {
436 1844 PlaneContext *const p = &sc->plane[i];
437
438
2/2
✓ Branch 0 taken 192 times.
✓ Branch 1 taken 1652 times.
1844 if (f->version == 2) {
439 192 int idx = ff_ffv1_get_symbol(c, state, 0);
440
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 192 times.
192 if (idx >= (unsigned)f->quant_table_count) {
441 av_log(f->avctx, AV_LOG_ERROR,
442 "quant_table_index out of range\n");
443 return AVERROR_INVALIDDATA;
444 }
445 192 p->quant_table_index = idx;
446 192 context_count = f->context_count[idx];
447 }
448
449
2/2
✓ Branch 0 taken 252 times.
✓ Branch 1 taken 1592 times.
1844 if (f->version <= 2) {
450
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 252 times.
252 av_assert0(context_count >= 0);
451 252 p->context_count = context_count;
452 }
453 }
454 }
455 253 return 0;
456 }
457
458 88 static av_cold int decode_init(AVCodecContext *avctx)
459 {
460 88 FFV1Context *f = avctx->priv_data;
461 int ret;
462
463
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 88 times.
88 if ((ret = ff_ffv1_common_init(avctx, f)) < 0)
464 return ret;
465
466
3/4
✓ Branch 0 taken 78 times.
✓ Branch 1 taken 10 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 78 times.
88 if (avctx->extradata_size > 0 && (ret = ff_ffv1_read_extra_header(f)) < 0)
467 return ret;
468
469
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 88 times.
88 if ((ret = ff_ffv1_init_slice_contexts(f)) < 0)
470 return ret;
471
472 88 return 0;
473 }
474
475 7715 static int find_next_slice(AVCodecContext *avctx,
476 uint8_t *buf, uint8_t *buf_end, int idx,
477 uint8_t **pos, uint32_t *len)
478 {
479 7715 FFV1Context *f = avctx->priv_data;
480
481 /* Length field */
482 7715 uint32_t v = buf_end - buf;
483
4/4
✓ Branch 0 taken 2120 times.
✓ Branch 1 taken 5595 times.
✓ Branch 2 taken 1661 times.
✓ Branch 3 taken 459 times.
7715 if (idx || f->version > 2) {
484 /* Three bytes of length, plus flush bit + CRC */
485
2/2
✓ Branch 0 taken 6644 times.
✓ Branch 1 taken 612 times.
7256 uint32_t trailer = 3 + 5*!!f->ec;
486
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7256 times.
7256 if (trailer > buf_end - buf)
487 v = INT_MAX;
488 else
489 7256 v = AV_RB24(buf_end - trailer) + trailer;
490 }
491
492
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7715 times.
7715 if (buf_end - buf < v) {
493 av_log(avctx, AV_LOG_ERROR, "Slice pointer chain broken\n");
494 ff_progress_frame_report(&f->picture, INT_MAX);
495 return AVERROR_INVALIDDATA;
496 }
497
498 7715 *len = v;
499
2/2
✓ Branch 0 taken 5595 times.
✓ Branch 1 taken 2120 times.
7715 if (idx)
500 5595 *pos = buf_end - v;
501 else
502 2120 *pos = buf;
503
504 7715 return 0;
505 }
506
507 2120 static int decode_header(AVCodecContext *avctx, RangeCoder *c,
508 uint8_t *buf, size_t buf_size)
509 {
510 int ret;
511 2120 FFV1Context *f = avctx->priv_data;
512
513 2120 uint8_t keystate = 128;
514 2120 ff_init_range_decoder(c, buf, buf_size);
515 2120 ff_build_rac_states(c, 0.05 * (1LL << 32), 256 - 8);
516
517
2/2
✓ Branch 1 taken 253 times.
✓ Branch 2 taken 1867 times.
2120 if (get_rac(c, &keystate)) {
518 253 f->key_frame = AV_FRAME_FLAG_KEY;
519 253 f->key_frame_ok = 0;
520
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 253 times.
253 if ((ret = read_header(f, c)) < 0)
521 return ret;
522 253 f->key_frame_ok = 1;
523 } else {
524
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1867 times.
1867 if (!f->key_frame_ok) {
525 av_log(avctx, AV_LOG_ERROR,
526 "Cannot decode non-keyframe without valid keyframe\n");
527 return AVERROR_INVALIDDATA;
528 }
529 1867 f->key_frame = 0;
530 }
531
532
2/2
✓ Branch 0 taken 1022 times.
✓ Branch 1 taken 1098 times.
2120 if (f->ac != AC_GOLOMB_RICE) {
533
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1022 times.
1022 if (buf_size < avctx->width * avctx->height / (128*8))
534 return AVERROR_INVALIDDATA;
535 } else {
536 1098 int w = avctx->width;
537 1098 int s = 1 + w / (1<<23);
538 int i;
539
540 1098 w /= s;
541
542
2/2
✓ Branch 0 taken 25232 times.
✓ Branch 1 taken 1098 times.
26330 for (i = 0; w > (1<<ff_log2_run[i]); i++)
543 25232 w -= ff_log2_run[i];
544
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1098 times.
1098 if (buf_size < (avctx->height + i + 6) / 8 * s)
545 return AVERROR_INVALIDDATA;
546 }
547
548 2120 return 0;
549 }
550
551 2120 static int decode_slices(AVCodecContext *avctx, RangeCoder c,
552 AVPacket *avpkt)
553 {
554 2120 FFV1Context *f = avctx->priv_data;
555 2120 AVFrame *p = f->picture.f;
556
557 2120 uint8_t *buf = avpkt->data;
558 2120 size_t buf_size = avpkt->size;
559 2120 uint8_t *buf_end = buf + buf_size;
560
561
2/2
✓ Branch 0 taken 7715 times.
✓ Branch 1 taken 2120 times.
9835 for (int i = f->slice_count - 1; i >= 0; i--) {
562 7715 FFV1SliceContext *sc = &f->slices[i];
563
564 uint8_t *pos;
565 uint32_t len;
566 7715 int err = find_next_slice(avctx, buf, buf_end, i,
567 &pos, &len);
568
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7715 times.
7715 if (err < 0)
569 return err;
570
571 7715 buf_end -= len;
572
573 7715 sc->slice_damaged = 0;
574
575
2/2
✓ Branch 0 taken 6644 times.
✓ Branch 1 taken 1071 times.
7715 if (f->ec) {
576 6644 unsigned crc = av_crc(av_crc_get_table(AV_CRC_32_IEEE), f->crcref, pos, len);
577
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6644 times.
6644 if (crc != f->crcref) {
578 int64_t ts = avpkt->pts != AV_NOPTS_VALUE ? avpkt->pts : avpkt->dts;
579 av_log(f->avctx, AV_LOG_ERROR, "slice CRC mismatch %X!", crc);
580 if (ts != AV_NOPTS_VALUE && avctx->pkt_timebase.num) {
581 av_log(f->avctx, AV_LOG_ERROR, "at %f seconds\n", ts*av_q2d(avctx->pkt_timebase));
582 } else if (ts != AV_NOPTS_VALUE) {
583 av_log(f->avctx, AV_LOG_ERROR, "at %"PRId64"\n", ts);
584 } else {
585 av_log(f->avctx, AV_LOG_ERROR, "\n");
586 }
587 slice_set_damaged(f, sc);
588 }
589
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6644 times.
6644 if (avctx->debug & FF_DEBUG_PICT_INFO) {
590 av_log(avctx, AV_LOG_DEBUG, "slice %d, CRC: 0x%08"PRIX32"\n", i, AV_RB32(pos + len - 4));
591 }
592 }
593
594
2/2
✓ Branch 0 taken 5595 times.
✓ Branch 1 taken 2120 times.
7715 if (i) {
595 5595 ff_init_range_decoder(&sc->c, pos, len);
596 5595 ff_build_rac_states(&sc->c, 0.05 * (1LL << 32), 256 - 8);
597 } else {
598 2120 sc->c = c;
599 2120 sc->c.bytestream_end = pos + len;
600 }
601 }
602
603 2120 avctx->execute(avctx,
604 decode_slice,
605 2120 f->slices,
606 NULL,
607 f->slice_count,
608 sizeof(*f->slices));
609
610
2/2
✓ Branch 0 taken 7715 times.
✓ Branch 1 taken 2120 times.
9835 for (int i = f->slice_count - 1; i >= 0; i--) {
611 7715 FFV1SliceContext *sc = &f->slices[i];
612
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 7715 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
7715 if (sc->slice_damaged && f->last_picture.f) {
613 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(f->pix_fmt);
614 const uint8_t *src[4];
615 uint8_t *dst[4];
616 ff_progress_frame_await(&f->last_picture, INT_MAX);
617 for (int j = 0; j < desc->nb_components; j++) {
618 int pixshift = desc->comp[j].depth > 8;
619 int sh = (j == 1 || j == 2) ? f->chroma_h_shift : 0;
620 int sv = (j == 1 || j == 2) ? f->chroma_v_shift : 0;
621 dst[j] = p->data[j] + p->linesize[j] *
622 (sc->slice_y >> sv) + ((sc->slice_x >> sh) << pixshift);
623 src[j] = f->last_picture.f->data[j] + f->last_picture.f->linesize[j] *
624 (sc->slice_y >> sv) + ((sc->slice_x >> sh) << pixshift);
625
626 }
627
628 av_image_copy(dst, p->linesize, src,
629 f->last_picture.f->linesize,
630 f->pix_fmt,
631 sc->slice_width,
632 sc->slice_height);
633
634 f->slice_damaged[i] = 1;
635 }
636 }
637
638 2120 return 0;
639 }
640
641 2120 static int decode_frame(AVCodecContext *avctx, AVFrame *rframe,
642 int *got_frame, AVPacket *avpkt)
643 {
644 2120 FFV1Context *f = avctx->priv_data;
645 int ret;
646 AVFrame *p;
647
648 /* This is copied onto the first slice's range coder context */
649 RangeCoder c;
650
651 2120 ff_progress_frame_unref(&f->last_picture);
652 2120 FFSWAP(ProgressFrame, f->picture, f->last_picture);
653
654
655 2120 f->avctx = avctx;
656 2120 f->frame_damaged = 0;
657
658 2120 ret = decode_header(avctx, &c, avpkt->data, avpkt->size);
659
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2120 times.
2120 if (ret < 0)
660 return ret;
661
662 2120 ret = ff_progress_frame_get_buffer(avctx, &f->picture,
663 AV_GET_BUFFER_FLAG_REF);
664
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2120 times.
2120 if (ret < 0)
665 return ret;
666
667 2120 p = f->picture.f;
668
669 2120 p->pict_type = AV_PICTURE_TYPE_I; //FIXME I vs. P
670 2120 p->flags = (p->flags & ~AV_FRAME_FLAG_KEY) | f->key_frame;
671
672
3/4
✓ Branch 0 taken 459 times.
✓ Branch 1 taken 1661 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 459 times.
2120 if (f->version < 3 && avctx->field_order > AV_FIELD_PROGRESSIVE) {
673 /* we have interlaced material flagged in container */
674 p->flags |= AV_FRAME_FLAG_INTERLACED;
675 if (avctx->field_order == AV_FIELD_TT || avctx->field_order == AV_FIELD_TB)
676 p->flags |= AV_FRAME_FLAG_TOP_FIELD_FIRST;
677 }
678
679
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2120 times.
2120 if (avctx->debug & FF_DEBUG_PICT_INFO)
680 av_log(avctx, AV_LOG_DEBUG, "ver:%d keyframe:%d coder:%d ec:%d slices:%d bps:%d\n",
681 f->version, !!(p->flags & AV_FRAME_FLAG_KEY), f->ac, f->ec, f->slice_count, f->avctx->bits_per_raw_sample);
682
683 2120 ff_thread_finish_setup(avctx);
684
685 2120 ret = decode_slices(avctx, c, avpkt);
686
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2120 times.
2120 if (ret < 0)
687 return ret;
688
689 2120 ff_progress_frame_report(&f->picture, INT_MAX);
690
691 2120 ff_progress_frame_unref(&f->last_picture);
692
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2120 times.
2120 if ((ret = av_frame_ref(rframe, f->picture.f)) < 0)
693 return ret;
694
695 2120 *got_frame = 1;
696
697 2120 return avpkt->size;
698 }
699
700 #if HAVE_THREADS
701 static int update_thread_context(AVCodecContext *dst, const AVCodecContext *src)
702 {
703 FFV1Context *fsrc = src->priv_data;
704 FFV1Context *fdst = dst->priv_data;
705
706 if (dst == src)
707 return 0;
708
709 fdst->version = fsrc->version;
710 fdst->micro_version = fsrc->micro_version;
711 fdst->combined_version = fsrc->combined_version;
712 fdst->chroma_planes = fsrc->chroma_planes;
713 fdst->chroma_h_shift = fsrc->chroma_h_shift;
714 fdst->chroma_v_shift = fsrc->chroma_v_shift;
715 fdst->transparency = fsrc->transparency;
716 fdst->plane_count = fsrc->plane_count;
717 fdst->ac = fsrc->ac;
718 fdst->colorspace = fsrc->colorspace;
719 fdst->pix_fmt = fsrc->pix_fmt;
720
721 fdst->ec = fsrc->ec;
722 fdst->intra = fsrc->intra;
723 fdst->key_frame_ok = fsrc->key_frame_ok;
724
725 fdst->packed_at_lsb = fsrc->packed_at_lsb;
726 fdst->slice_count = fsrc->slice_count;
727 fdst->use32bit = fsrc->use32bit;
728 memcpy(fdst->state_transition, fsrc->state_transition,
729 sizeof(fdst->state_transition));
730
731 // in version 1 there is a single per-keyframe quant table, so
732 // we need to propagate it between threads
733 if (fsrc->version < 2)
734 memcpy(fdst->quant_tables[0], fsrc->quant_tables[0], sizeof(fsrc->quant_tables[0]));
735
736 for (int i = 0; i < fdst->num_h_slices * fdst->num_v_slices; i++) {
737 FFV1SliceContext *sc = &fdst->slices[i];
738 const FFV1SliceContext *sc0 = &fsrc->slices[i];
739
740 av_refstruct_replace(&sc->plane, sc0->plane);
741
742 if (fsrc->version < 3) {
743 sc->slice_x = sc0->slice_x;
744 sc->slice_y = sc0->slice_y;
745 sc->slice_width = sc0->slice_width;
746 sc->slice_height = sc0->slice_height;
747 }
748 }
749
750 av_refstruct_replace(&fdst->slice_damaged, fsrc->slice_damaged);
751
752 av_assert1(fdst->max_slice_count == fsrc->max_slice_count);
753
754 ff_progress_frame_replace(&fdst->picture, &fsrc->picture);
755
756 return 0;
757 }
758 #endif
759
760 88 static av_cold int ffv1_decode_close(AVCodecContext *avctx)
761 {
762 88 FFV1Context *const s = avctx->priv_data;
763
764 88 ff_progress_frame_unref(&s->picture);
765 88 ff_progress_frame_unref(&s->last_picture);
766 88 av_freep(&avctx->stats_out);
767
768 88 ff_ffv1_close(s);
769
770 88 return 0;
771 }
772
773 const FFCodec ff_ffv1_decoder = {
774 .p.name = "ffv1",
775 CODEC_LONG_NAME("FFmpeg video codec #1"),
776 .p.type = AVMEDIA_TYPE_VIDEO,
777 .p.id = AV_CODEC_ID_FFV1,
778 .priv_data_size = sizeof(FFV1Context),
779 .init = decode_init,
780 .close = ffv1_decode_close,
781 FF_CODEC_DECODE_CB(decode_frame),
782 UPDATE_THREAD_CONTEXT(update_thread_context),
783 .p.capabilities = AV_CODEC_CAP_DR1 |
784 AV_CODEC_CAP_FRAME_THREADS | AV_CODEC_CAP_SLICE_THREADS,
785 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP |
786 FF_CODEC_CAP_USES_PROGRESSFRAMES,
787 };
788