FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/ffv1dec.c
Date: 2024-07-17 14:05:47
Exec Total Coverage
Lines: 397 690 57.5%
Functions: 14 16 87.5%
Branches: 264 592 44.6%

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 "thread.h"
42
43 153604942 static inline av_flatten int get_symbol_inline(RangeCoder *c, uint8_t *state,
44 int is_signed)
45 {
46
2/2
✓ Branch 1 taken 60924434 times.
✓ Branch 2 taken 92680508 times.
153604942 if (get_rac(c, state + 0))
47 60924434 return 0;
48 else {
49 int i, e;
50 unsigned a;
51 92680508 e = 0;
52
4/4
✓ Branch 0 taken 586934973 times.
✓ Branch 1 taken 45940109 times.
✓ Branch 3 taken 540194574 times.
✓ Branch 4 taken 92680508 times.
632875082 while (get_rac(c, state + 1 + FFMIN(e, 9))) { // 1..10
53 540194574 e++;
54
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 540194574 times.
540194574 if (e > 31)
55 return AVERROR_INVALIDDATA;
56 }
57
58 92680508 a = 1;
59
2/2
✓ Branch 0 taken 540194574 times.
✓ Branch 1 taken 92680508 times.
632875082 for (i = e - 1; i >= 0; i--)
60
2/2
✓ Branch 0 taken 519680677 times.
✓ Branch 1 taken 20513897 times.
540194574 a += a + get_rac(c, state + 22 + FFMIN(i, 9)); // 22..31
61
62
6/6
✓ Branch 0 taken 92658298 times.
✓ Branch 1 taken 22210 times.
✓ Branch 2 taken 81255906 times.
✓ Branch 3 taken 11402392 times.
✓ Branch 5 taken 49217065 times.
✓ Branch 6 taken 43441233 times.
92680508 e = -(is_signed && get_rac(c, state + 11 + FFMIN(e, 10))); // 11..21
63 92680508 return (a ^ e) - e;
64 }
65 }
66
67 2001406 static av_noinline int get_symbol(RangeCoder *c, uint8_t *state, int is_signed)
68 {
69 2001406 return get_symbol_inline(c, state, is_signed);
70 }
71
72 144478585 static inline int get_vlc_symbol(GetBitContext *gb, VlcState *const state,
73 int bits)
74 {
75 int k, i, v, ret;
76
77 144478585 i = state->count;
78 144478585 k = 0;
79
2/2
✓ Branch 0 taken 241148336 times.
✓ Branch 1 taken 144478585 times.
385626921 while (i < state->error_sum) { // FIXME: optimize
80 241148336 k++;
81 241148336 i += i;
82 }
83
84 144478585 v = get_sr_golomb(gb, k, 12, bits);
85 ff_dlog(NULL, "v:%d bias:%d error:%d drift:%d count:%d k:%d",
86 v, state->bias, state->error_sum, state->drift, state->count, k);
87
88 144478585 v ^= ((2 * state->drift + state->count) >> 31);
89
90 144478585 ret = fold(v + state->bias, bits);
91
92 144478585 update_vlc_state(state, v);
93
94 144478585 return ret;
95 }
96
97 3680016 static int is_input_end(FFV1Context *s)
98 {
99
2/2
✓ Branch 0 taken 2032800 times.
✓ Branch 1 taken 1647216 times.
3680016 if (s->ac != AC_GOLOMB_RICE) {
100 2032800 RangeCoder *const c = &s->c;
101
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2032800 times.
2032800 if (c->overread > MAX_OVERREAD)
102 return AVERROR_INVALIDDATA;
103 } else {
104
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1647216 times.
1647216 if (get_bits_left(&s->gb) < 1)
105 return AVERROR_INVALIDDATA;
106 }
107 3680016 return 0;
108 }
109
110 #define TYPE int16_t
111 #define RENAME(name) name
112 #include "ffv1dec_template.c"
113 #undef TYPE
114 #undef RENAME
115
116 #define TYPE int32_t
117 #define RENAME(name) name ## 32
118 #include "ffv1dec_template.c"
119
120 13353 static int decode_plane(FFV1Context *s, uint8_t *src,
121 int w, int h, int stride, int plane_index,
122 int pixel_stride)
123 {
124 int x, y;
125 int16_t *sample[2];
126 13353 sample[0] = s->sample_buffer + 3;
127 13353 sample[1] = s->sample_buffer + w + 6 + 3;
128
129 13353 s->run_index = 0;
130
131 13353 memset(s->sample_buffer, 0, 2 * (w + 6) * sizeof(*s->sample_buffer));
132
133
2/2
✓ Branch 0 taken 1290432 times.
✓ Branch 1 taken 13353 times.
1303785 for (y = 0; y < h; y++) {
134 1290432 int16_t *temp = sample[0]; // FIXME: try a normal buffer
135
136 1290432 sample[0] = sample[1];
137 1290432 sample[1] = temp;
138
139 1290432 sample[1][-1] = sample[0][0];
140 1290432 sample[0][w] = sample[0][w - 1];
141
142
2/2
✓ Branch 0 taken 732216 times.
✓ Branch 1 taken 558216 times.
1290432 if (s->avctx->bits_per_raw_sample <= 8) {
143 732216 int ret = decode_line(s, w, sample, plane_index, 8);
144
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 732216 times.
732216 if (ret < 0)
145 return ret;
146
2/2
✓ Branch 0 taken 125264286 times.
✓ Branch 1 taken 732216 times.
125996502 for (x = 0; x < w; x++)
147 125264286 src[x*pixel_stride + stride * y] = sample[1][x];
148 } else {
149 558216 int ret = decode_line(s, w, sample, plane_index, s->avctx->bits_per_raw_sample);
150
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 558216 times.
558216 if (ret < 0)
151 return ret;
152
1/2
✓ Branch 0 taken 558216 times.
✗ Branch 1 not taken.
558216 if (s->packed_at_lsb) {
153
2/2
✓ Branch 0 taken 81537288 times.
✓ Branch 1 taken 558216 times.
82095504 for (x = 0; x < w; x++) {
154 81537288 ((uint16_t*)(src + stride*y))[x*pixel_stride] = sample[1][x];
155 }
156 } else {
157 for (x = 0; x < w; x++) {
158 ((uint16_t*)(src + stride*y))[x*pixel_stride] = sample[1][x] << (16 - s->avctx->bits_per_raw_sample) | ((uint16_t **)sample)[1][x] >> (2 * s->avctx->bits_per_raw_sample - 16);
159 }
160 }
161 }
162 }
163 13353 return 0;
164 }
165
166 5828 static int decode_slice_header(const FFV1Context *f, FFV1Context *fs)
167 {
168 5828 RangeCoder *c = &fs->c;
169 uint8_t state[CONTEXT_SIZE];
170 unsigned ps, i, context_count;
171 int sx, sy, sw, sh;
172
173 5828 memset(state, 128, sizeof(state));
174 5828 sx = get_symbol(c, state, 0);
175 5828 sy = get_symbol(c, state, 0);
176 5828 sw = get_symbol(c, state, 0) + 1U;
177 5828 sh = get_symbol(c, state, 0) + 1U;
178
179
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5828 times.
5828 av_assert0(f->version > 2);
180
181
182
4/8
✓ Branch 0 taken 5828 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5828 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 5828 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 5828 times.
5828 if (sx < 0 || sy < 0 || sw <= 0 || sh <= 0)
183 return AVERROR_INVALIDDATA;
184
2/4
✓ Branch 0 taken 5828 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 5828 times.
5828 if (sx > f->num_h_slices - sw || sy > f->num_v_slices - sh)
185 return AVERROR_INVALIDDATA;
186
187 5828 fs->slice_x = sx * (int64_t)f->width / f->num_h_slices;
188 5828 fs->slice_y = sy * (int64_t)f->height / f->num_v_slices;
189 5828 fs->slice_width = (sx + sw) * (int64_t)f->width / f->num_h_slices - fs->slice_x;
190 5828 fs->slice_height = (sy + sh) * (int64_t)f->height / f->num_v_slices - fs->slice_y;
191
192
2/4
✓ Branch 0 taken 5828 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 5828 times.
5828 av_assert0((unsigned)fs->slice_width <= f->width &&
193 (unsigned)fs->slice_height <= f->height);
194
2/4
✓ Branch 0 taken 5828 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 5828 times.
5828 av_assert0 ( (unsigned)fs->slice_x + (uint64_t)fs->slice_width <= f->width
195 && (unsigned)fs->slice_y + (uint64_t)fs->slice_height <= f->height);
196
197
3/4
✓ Branch 0 taken 2556 times.
✓ Branch 1 taken 3272 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2556 times.
5828 if (fs->ac == AC_GOLOMB_RICE && fs->slice_width >= (1<<23))
198 return AVERROR_INVALIDDATA;
199
200
2/2
✓ Branch 0 taken 11656 times.
✓ Branch 1 taken 5828 times.
17484 for (i = 0; i < f->plane_count; i++) {
201 11656 PlaneContext * const p = &fs->plane[i];
202 11656 int idx = get_symbol(c, state, 0);
203
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11656 times.
11656 if (idx >= (unsigned)f->quant_table_count) {
204 av_log(f->avctx, AV_LOG_ERROR, "quant_table_index out of range\n");
205 return -1;
206 }
207 11656 p->quant_table_index = idx;
208 11656 memcpy(p->quant_table, f->quant_tables[idx], sizeof(p->quant_table));
209 11656 context_count = f->context_count[idx];
210
211
2/2
✓ Branch 0 taken 488 times.
✓ Branch 1 taken 11168 times.
11656 if (p->context_count < context_count) {
212 488 av_freep(&p->state);
213 488 av_freep(&p->vlc_state);
214 }
215 11656 p->context_count = context_count;
216 }
217
218 5828 ps = get_symbol(c, state, 0);
219
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5828 times.
5828 if (ps == 1) {
220 f->cur->flags |= AV_FRAME_FLAG_INTERLACED;
221 f->cur->flags |= AV_FRAME_FLAG_TOP_FIELD_FIRST;
222
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5828 times.
5828 } else if (ps == 2) {
223 f->cur->flags |= AV_FRAME_FLAG_INTERLACED;
224 f->cur->flags &= ~AV_FRAME_FLAG_TOP_FIELD_FIRST;
225
1/2
✓ Branch 0 taken 5828 times.
✗ Branch 1 not taken.
5828 } else if (ps == 3) {
226 5828 f->cur->flags &= ~AV_FRAME_FLAG_INTERLACED;
227 }
228 5828 f->cur->sample_aspect_ratio.num = get_symbol(c, state, 0);
229 5828 f->cur->sample_aspect_ratio.den = get_symbol(c, state, 0);
230
231
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5828 times.
5828 if (av_image_check_sar(f->width, f->height,
232 5828 f->cur->sample_aspect_ratio) < 0) {
233 av_log(f->avctx, AV_LOG_WARNING, "ignoring invalid SAR: %u/%u\n",
234 f->cur->sample_aspect_ratio.num,
235 f->cur->sample_aspect_ratio.den);
236 f->cur->sample_aspect_ratio = (AVRational){ 0, 1 };
237 }
238
239
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5828 times.
5828 if (fs->version > 3) {
240 fs->slice_reset_contexts = get_rac(c, state);
241 fs->slice_coding_mode = get_symbol(c, state, 0);
242 if (fs->slice_coding_mode != 1) {
243 fs->slice_rct_by_coef = get_symbol(c, state, 0);
244 fs->slice_rct_ry_coef = get_symbol(c, state, 0);
245 if ((uint64_t)fs->slice_rct_by_coef + (uint64_t)fs->slice_rct_ry_coef > 4) {
246 av_log(f->avctx, AV_LOG_ERROR, "slice_rct_y_coef out of range\n");
247 return AVERROR_INVALIDDATA;
248 }
249 }
250 }
251
252 5828 return 0;
253 }
254
255 6083 static int decode_slice(AVCodecContext *c, void *arg)
256 {
257 6083 FFV1Context *fs = *(void **)arg;
258 6083 FFV1Context *f = fs->avctx->priv_data;
259 int width, height, x, y, ret;
260 6083 const int ps = av_pix_fmt_desc_get(c->pix_fmt)->comp[0].step;
261 6083 AVFrame * const p = f->cur;
262 int i, si;
263
264
2/2
✓ Branch 0 taken 8742 times.
✓ Branch 1 taken 6083 times.
14825 for( si=0; fs != f->slice_context[si]; si ++)
265 ;
266
267
1/6
✗ Branch 0 not taken.
✓ Branch 1 taken 6083 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
6083 if (f->fsrc && !(p->flags & AV_FRAME_FLAG_KEY) && f->last_picture.f)
268 ff_progress_frame_await(&f->last_picture, si);
269
270
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 6083 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
6083 if(f->fsrc && !(p->flags & AV_FRAME_FLAG_KEY)) {
271 FFV1Context *fssrc = f->fsrc->slice_context[si];
272 FFV1Context *fsdst = f->slice_context[si];
273 av_assert1(fsdst->plane_count == fssrc->plane_count);
274 av_assert1(fsdst == fs);
275
276 if (!(p->flags & AV_FRAME_FLAG_KEY))
277 fsdst->slice_damaged |= fssrc->slice_damaged;
278
279 for (i = 0; i < f->plane_count; i++) {
280 PlaneContext *psrc = &fssrc->plane[i];
281 PlaneContext *pdst = &fsdst->plane[i];
282
283 av_free(pdst->state);
284 av_free(pdst->vlc_state);
285 memcpy(pdst, psrc, sizeof(*pdst));
286 pdst->state = NULL;
287 pdst->vlc_state = NULL;
288
289 if (fssrc->ac) {
290 pdst->state = av_malloc_array(CONTEXT_SIZE, psrc->context_count);
291 memcpy(pdst->state, psrc->state, CONTEXT_SIZE * psrc->context_count);
292 } else {
293 pdst->vlc_state = av_malloc_array(sizeof(*pdst->vlc_state), psrc->context_count);
294 memcpy(pdst->vlc_state, psrc->vlc_state, sizeof(*pdst->vlc_state) * psrc->context_count);
295 }
296 }
297 }
298
299 6083 fs->slice_rct_by_coef = 1;
300 6083 fs->slice_rct_ry_coef = 1;
301
302
2/2
✓ Branch 0 taken 5828 times.
✓ Branch 1 taken 255 times.
6083 if (f->version > 2) {
303
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 5828 times.
5828 if (ff_ffv1_init_slice_state(f, fs) < 0)
304 return AVERROR(ENOMEM);
305
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 5828 times.
5828 if (decode_slice_header(f, fs) < 0) {
306 fs->slice_x = fs->slice_y = fs->slice_height = fs->slice_width = 0;
307 fs->slice_damaged = 1;
308 return AVERROR_INVALIDDATA;
309 }
310 }
311
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 6083 times.
6083 if ((ret = ff_ffv1_init_slice_state(f, fs)) < 0)
312 return ret;
313
3/4
✓ Branch 0 taken 5353 times.
✓ Branch 1 taken 730 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 5353 times.
6083 if ((f->cur->flags & AV_FRAME_FLAG_KEY) || fs->slice_reset_contexts) {
314 730 ff_ffv1_clear_slice_state(f, fs);
315
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5353 times.
5353 } else if (fs->slice_damaged) {
316 return AVERROR_INVALIDDATA;
317 }
318
319 6083 width = fs->slice_width;
320 6083 height = fs->slice_height;
321 6083 x = fs->slice_x;
322 6083 y = fs->slice_y;
323
324
2/2
✓ Branch 0 taken 2811 times.
✓ Branch 1 taken 3272 times.
6083 if (fs->ac == AC_GOLOMB_RICE) {
325
4/6
✓ Branch 0 taken 2556 times.
✓ Branch 1 taken 255 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2556 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 255 times.
2811 if (f->version == 3 && f->micro_version > 1 || f->version > 3)
326 2556 get_rac(&fs->c, (uint8_t[]) { 129 });
327
4/6
✓ Branch 0 taken 255 times.
✓ Branch 1 taken 2556 times.
✓ Branch 2 taken 255 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 255 times.
✗ Branch 5 not taken.
2811 fs->ac_byte_count = f->version > 2 || (!x && !y) ? fs->c.bytestream - fs->c.bytestream_start - 1 : 0;
328 2811 init_get_bits(&fs->gb,
329 2811 fs->c.bytestream_start + fs->ac_byte_count,
330 2811 (fs->c.bytestream_end - fs->c.bytestream_start - fs->ac_byte_count) * 8);
331 }
332
333 av_assert1(width && height);
334
3/6
✓ Branch 0 taken 4451 times.
✓ Branch 1 taken 1632 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 4451 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
10534 if (f->colorspace == 0 && (f->chroma_planes || !fs->transparency)) {
335 4451 const int chroma_width = AV_CEIL_RSHIFT(width, f->chroma_h_shift);
336 4451 const int chroma_height = AV_CEIL_RSHIFT(height, f->chroma_v_shift);
337 4451 const int cx = x >> f->chroma_h_shift;
338 4451 const int cy = y >> f->chroma_v_shift;
339 4451 decode_plane(fs, p->data[0] + ps*x + y*p->linesize[0], width, height, p->linesize[0], 0, 1);
340
341
1/2
✓ Branch 0 taken 4451 times.
✗ Branch 1 not taken.
4451 if (f->chroma_planes) {
342 4451 decode_plane(fs, p->data[1] + ps*cx+cy*p->linesize[1], chroma_width, chroma_height, p->linesize[1], 1, 1);
343 4451 decode_plane(fs, p->data[2] + ps*cx+cy*p->linesize[2], chroma_width, chroma_height, p->linesize[2], 1, 1);
344 }
345
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4451 times.
4451 if (fs->transparency)
346 decode_plane(fs, p->data[3] + ps*x + y*p->linesize[3], width, height, p->linesize[3], (f->version >= 4 && !f->chroma_planes) ? 1 : 2, 1);
347
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1632 times.
1632 } else if (f->colorspace == 0) {
348 decode_plane(fs, p->data[0] + ps*x + y*p->linesize[0] , width, height, p->linesize[0], 0, 2);
349 decode_plane(fs, p->data[0] + ps*x + y*p->linesize[0] + 1, width, height, p->linesize[0], 1, 2);
350
2/2
✓ Branch 0 taken 816 times.
✓ Branch 1 taken 816 times.
1632 } else if (f->use32bit) {
351 816 uint8_t *planes[4] = { p->data[0] + ps * x + y * p->linesize[0],
352 816 p->data[1] + ps * x + y * p->linesize[1],
353 816 p->data[2] + ps * x + y * p->linesize[2],
354 816 p->data[3] + ps * x + y * p->linesize[3] };
355 816 decode_rgb_frame32(fs, planes, width, height, p->linesize);
356 } else {
357 816 uint8_t *planes[4] = { p->data[0] + ps * x + y * p->linesize[0],
358 816 p->data[1] + ps * x + y * p->linesize[1],
359 816 p->data[2] + ps * x + y * p->linesize[2],
360 816 p->data[3] + ps * x + y * p->linesize[3] };
361 816 decode_rgb_frame(fs, planes, width, height, p->linesize);
362 }
363
3/4
✓ Branch 0 taken 3272 times.
✓ Branch 1 taken 2811 times.
✓ Branch 2 taken 3272 times.
✗ Branch 3 not taken.
6083 if (fs->ac != AC_GOLOMB_RICE && f->version > 2) {
364 int v;
365 3272 get_rac(&fs->c, (uint8_t[]) { 129 });
366 3272 v = fs->c.bytestream_end - fs->c.bytestream - 2 - 5*f->ec;
367
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3272 times.
3272 if (v) {
368 av_log(f->avctx, AV_LOG_ERROR, "bytestream end mismatching by %d\n", v);
369 fs->slice_damaged = 1;
370 }
371 }
372
373 6083 ff_progress_frame_report(&f->picture, si);
374
375 6083 return 0;
376 }
377
378 770 static int read_quant_table(RangeCoder *c, int16_t *quant_table, int scale)
379 {
380 int v;
381 770 int i = 0;
382 uint8_t state[CONTEXT_SIZE];
383
384 770 memset(state, 128, sizeof(state));
385
386
2/2
✓ Branch 0 taken 3007 times.
✓ Branch 1 taken 770 times.
3777 for (v = 0; i < 128; v++) {
387 3007 unsigned len = get_symbol(c, state, 0) + 1U;
388
389
2/4
✓ Branch 0 taken 3007 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 3007 times.
3007 if (len > 128 - i || !len)
390 return AVERROR_INVALIDDATA;
391
392
2/2
✓ Branch 0 taken 98560 times.
✓ Branch 1 taken 3007 times.
101567 while (len--) {
393 98560 quant_table[i] = scale * v;
394 98560 i++;
395 }
396 }
397
398
2/2
✓ Branch 0 taken 97790 times.
✓ Branch 1 taken 770 times.
98560 for (i = 1; i < 128; i++)
399 97790 quant_table[256 - i] = -quant_table[i];
400 770 quant_table[128] = -quant_table[127];
401
402 770 return 2 * v - 1;
403 }
404
405 154 static int read_quant_tables(RangeCoder *c,
406 int16_t quant_table[MAX_CONTEXT_INPUTS][256])
407 {
408 int i;
409 154 int context_count = 1;
410
411
2/2
✓ Branch 0 taken 770 times.
✓ Branch 1 taken 154 times.
924 for (i = 0; i < 5; i++) {
412 770 int ret = read_quant_table(c, quant_table[i], context_count);
413
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 770 times.
770 if (ret < 0)
414 return ret;
415 770 context_count *= ret;
416
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 770 times.
770 if (context_count > 32768U) {
417 return AVERROR_INVALIDDATA;
418 }
419 }
420 154 return (context_count + 1) / 2;
421 }
422
423 62 static int read_extra_header(FFV1Context *f)
424 {
425 62 RangeCoder *const c = &f->c;
426 uint8_t state[CONTEXT_SIZE];
427 int i, j, k, ret;
428 uint8_t state2[32][CONTEXT_SIZE];
429 62 unsigned crc = 0;
430
431 62 memset(state2, 128, sizeof(state2));
432 62 memset(state, 128, sizeof(state));
433
434 62 ff_init_range_decoder(c, f->avctx->extradata, f->avctx->extradata_size);
435 62 ff_build_rac_states(c, 0.05 * (1LL << 32), 256 - 8);
436
437 62 f->version = get_symbol(c, state, 0);
438
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 62 times.
62 if (f->version < 2) {
439 av_log(f->avctx, AV_LOG_ERROR, "Invalid version in global header\n");
440 return AVERROR_INVALIDDATA;
441 }
442
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 62 times.
62 if (f->version > 4) {
443 av_log(f->avctx, AV_LOG_ERROR, "unsupported version %d\n",
444 f->version);
445 return AVERROR_PATCHWELCOME;
446 }
447
1/2
✓ Branch 0 taken 62 times.
✗ Branch 1 not taken.
62 if (f->version > 2) {
448 62 c->bytestream_end -= 4;
449 62 f->micro_version = get_symbol(c, state, 0);
450
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 62 times.
62 if (f->micro_version < 0)
451 return AVERROR_INVALIDDATA;
452 }
453 62 f->ac = get_symbol(c, state, 0);
454
455
2/2
✓ Branch 0 taken 35 times.
✓ Branch 1 taken 27 times.
62 if (f->ac == AC_RANGE_CUSTOM_TAB) {
456
2/2
✓ Branch 0 taken 8925 times.
✓ Branch 1 taken 35 times.
8960 for (i = 1; i < 256; i++)
457 8925 f->state_transition[i] = get_symbol(c, state, 1) + c->one_state[i];
458 }
459
460 62 f->colorspace = get_symbol(c, state, 0); //YUV cs type
461 62 f->avctx->bits_per_raw_sample = get_symbol(c, state, 0);
462 62 f->chroma_planes = get_rac(c, state);
463 62 f->chroma_h_shift = get_symbol(c, state, 0);
464 62 f->chroma_v_shift = get_symbol(c, state, 0);
465 62 f->transparency = get_rac(c, state);
466
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 62 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
62 f->plane_count = 1 + (f->chroma_planes || f->version<4) + f->transparency;
467 62 f->num_h_slices = 1 + get_symbol(c, state, 0);
468 62 f->num_v_slices = 1 + get_symbol(c, state, 0);
469
470
2/4
✓ Branch 0 taken 62 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 62 times.
62 if (f->chroma_h_shift > 4U || f->chroma_v_shift > 4U) {
471 av_log(f->avctx, AV_LOG_ERROR, "chroma shift parameters %d %d are invalid\n",
472 f->chroma_h_shift, f->chroma_v_shift);
473 return AVERROR_INVALIDDATA;
474 }
475
476
2/4
✓ Branch 0 taken 62 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 62 times.
✗ Branch 3 not taken.
62 if (f->num_h_slices > (unsigned)f->width || !f->num_h_slices ||
477
2/4
✓ Branch 0 taken 62 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 62 times.
62 f->num_v_slices > (unsigned)f->height || !f->num_v_slices
478 ) {
479 av_log(f->avctx, AV_LOG_ERROR, "slice count invalid\n");
480 return AVERROR_INVALIDDATA;
481 }
482
483
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 62 times.
62 if (f->num_h_slices > MAX_SLICES / f->num_v_slices) {
484 av_log(f->avctx, AV_LOG_ERROR, "slice count unsupported\n");
485 return AVERROR_PATCHWELCOME;
486 }
487
488 62 f->quant_table_count = get_symbol(c, state, 0);
489
2/4
✓ Branch 0 taken 62 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 62 times.
62 if (f->quant_table_count > (unsigned)MAX_QUANT_TABLES || !f->quant_table_count) {
490 av_log(f->avctx, AV_LOG_ERROR, "quant table count %d is invalid\n", f->quant_table_count);
491 f->quant_table_count = 0;
492 return AVERROR_INVALIDDATA;
493 }
494
495
2/2
✓ Branch 0 taken 124 times.
✓ Branch 1 taken 62 times.
186 for (i = 0; i < f->quant_table_count; i++) {
496 124 f->context_count[i] = read_quant_tables(c, f->quant_tables[i]);
497
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 124 times.
124 if (f->context_count[i] < 0) {
498 av_log(f->avctx, AV_LOG_ERROR, "read_quant_table error\n");
499 return AVERROR_INVALIDDATA;
500 }
501 }
502
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 62 times.
62 if ((ret = ff_ffv1_allocate_initial_states(f)) < 0)
503 return ret;
504
505
2/2
✓ Branch 0 taken 124 times.
✓ Branch 1 taken 62 times.
186 for (i = 0; i < f->quant_table_count; i++)
506
2/2
✓ Branch 1 taken 8 times.
✓ Branch 2 taken 116 times.
124 if (get_rac(c, state)) {
507
2/2
✓ Branch 0 taken 60504 times.
✓ Branch 1 taken 8 times.
60512 for (j = 0; j < f->context_count[i]; j++)
508
2/2
✓ Branch 0 taken 1936128 times.
✓ Branch 1 taken 60504 times.
1996632 for (k = 0; k < CONTEXT_SIZE; k++) {
509
2/2
✓ Branch 0 taken 1935872 times.
✓ Branch 1 taken 256 times.
1936128 int pred = j ? f->initial_states[i][j - 1][k] : 128;
510 1936128 f->initial_states[i][j][k] =
511 1936128 (pred + get_symbol(c, state2[k], 1)) & 0xFF;
512 }
513 }
514
515
1/2
✓ Branch 0 taken 62 times.
✗ Branch 1 not taken.
62 if (f->version > 2) {
516 62 f->ec = get_symbol(c, state, 0);
517
1/2
✓ Branch 0 taken 62 times.
✗ Branch 1 not taken.
62 if (f->micro_version > 2)
518 62 f->intra = get_symbol(c, state, 0);
519 }
520
521
1/2
✓ Branch 0 taken 62 times.
✗ Branch 1 not taken.
62 if (f->version > 2) {
522 unsigned v;
523 124 v = av_crc(av_crc_get_table(AV_CRC_32_IEEE), 0,
524 62 f->avctx->extradata, f->avctx->extradata_size);
525
2/4
✓ Branch 0 taken 62 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 62 times.
62 if (v || f->avctx->extradata_size < 4) {
526 av_log(f->avctx, AV_LOG_ERROR, "CRC mismatch %X!\n", v);
527 return AVERROR_INVALIDDATA;
528 }
529 62 crc = AV_RB32(f->avctx->extradata + f->avctx->extradata_size - 4);
530 }
531
532
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 62 times.
62 if (f->avctx->debug & FF_DEBUG_PICT_INFO)
533 av_log(f->avctx, AV_LOG_DEBUG,
534 "global: ver:%d.%d, coder:%d, colorspace: %d bpr:%d chroma:%d(%d:%d), alpha:%d slices:%dx%d qtabs:%d ec:%d intra:%d CRC:0x%08X\n",
535 f->version, f->micro_version,
536 f->ac,
537 f->colorspace,
538 f->avctx->bits_per_raw_sample,
539 f->chroma_planes, f->chroma_h_shift, f->chroma_v_shift,
540 f->transparency,
541 f->num_h_slices, f->num_v_slices,
542 f->quant_table_count,
543 f->ec,
544 f->intra,
545 crc
546 );
547 62 return 0;
548 }
549
550 205 static int read_header(FFV1Context *f)
551 {
552 uint8_t state[CONTEXT_SIZE];
553 205 int i, j, context_count = -1; //-1 to avoid warning
554 205 RangeCoder *const c = &f->slice_context[0]->c;
555
556 205 memset(state, 128, sizeof(state));
557
558
2/2
✓ Branch 0 taken 30 times.
✓ Branch 1 taken 175 times.
205 if (f->version < 2) {
559 int chroma_planes, chroma_h_shift, chroma_v_shift, transparency, colorspace, bits_per_raw_sample;
560 30 unsigned v= get_symbol(c, state, 0);
561
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 30 times.
30 if (v >= 2) {
562 av_log(f->avctx, AV_LOG_ERROR, "invalid version %d in ver01 header\n", v);
563 return AVERROR_INVALIDDATA;
564 }
565 30 f->version = v;
566 30 f->ac = get_symbol(c, state, 0);
567
568
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 30 times.
30 if (f->ac == AC_RANGE_CUSTOM_TAB) {
569 for (i = 1; i < 256; i++) {
570 int st = get_symbol(c, state, 1) + c->one_state[i];
571 if (st < 1 || st > 255) {
572 av_log(f->avctx, AV_LOG_ERROR, "invalid state transition %d\n", st);
573 return AVERROR_INVALIDDATA;
574 }
575 f->state_transition[i] = st;
576 }
577 }
578
579 30 colorspace = get_symbol(c, state, 0); //YUV cs type
580
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 30 times.
30 bits_per_raw_sample = f->version > 0 ? get_symbol(c, state, 0) : f->avctx->bits_per_raw_sample;
581 30 chroma_planes = get_rac(c, state);
582 30 chroma_h_shift = get_symbol(c, state, 0);
583 30 chroma_v_shift = get_symbol(c, state, 0);
584 30 transparency = get_rac(c, state);
585
2/4
✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 30 times.
30 if (colorspace == 0 && f->avctx->skip_alpha)
586 transparency = 0;
587
588
2/2
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 10 times.
30 if (f->plane_count) {
589
1/2
✓ Branch 0 taken 20 times.
✗ Branch 1 not taken.
20 if (colorspace != f->colorspace ||
590
1/2
✓ Branch 0 taken 20 times.
✗ Branch 1 not taken.
20 bits_per_raw_sample != f->avctx->bits_per_raw_sample ||
591
1/2
✓ Branch 0 taken 20 times.
✗ Branch 1 not taken.
20 chroma_planes != f->chroma_planes ||
592
1/2
✓ Branch 0 taken 20 times.
✗ Branch 1 not taken.
20 chroma_h_shift != f->chroma_h_shift ||
593
1/2
✓ Branch 0 taken 20 times.
✗ Branch 1 not taken.
20 chroma_v_shift != f->chroma_v_shift ||
594
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20 times.
20 transparency != f->transparency) {
595 av_log(f->avctx, AV_LOG_ERROR, "Invalid change of global parameters\n");
596 return AVERROR_INVALIDDATA;
597 }
598 }
599
600
2/4
✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 30 times.
30 if (chroma_h_shift > 4U || chroma_v_shift > 4U) {
601 av_log(f->avctx, AV_LOG_ERROR, "chroma shift parameters %d %d are invalid\n",
602 chroma_h_shift, chroma_v_shift);
603 return AVERROR_INVALIDDATA;
604 }
605
606 30 f->colorspace = colorspace;
607 30 f->avctx->bits_per_raw_sample = bits_per_raw_sample;
608 30 f->chroma_planes = chroma_planes;
609 30 f->chroma_h_shift = chroma_h_shift;
610 30 f->chroma_v_shift = chroma_v_shift;
611 30 f->transparency = transparency;
612
613 30 f->plane_count = 2 + f->transparency;
614 }
615
616
2/2
✓ Branch 0 taken 157 times.
✓ Branch 1 taken 48 times.
205 if (f->colorspace == 0) {
617
2/4
✓ Branch 0 taken 157 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 157 times.
157 if (!f->transparency && !f->chroma_planes) {
618 if (f->avctx->bits_per_raw_sample <= 8)
619 f->avctx->pix_fmt = AV_PIX_FMT_GRAY8;
620 else if (f->avctx->bits_per_raw_sample == 9) {
621 f->packed_at_lsb = 1;
622 f->avctx->pix_fmt = AV_PIX_FMT_GRAY9;
623 } else if (f->avctx->bits_per_raw_sample == 10) {
624 f->packed_at_lsb = 1;
625 f->avctx->pix_fmt = AV_PIX_FMT_GRAY10;
626 } else if (f->avctx->bits_per_raw_sample == 12) {
627 f->packed_at_lsb = 1;
628 f->avctx->pix_fmt = AV_PIX_FMT_GRAY12;
629 } else if (f->avctx->bits_per_raw_sample == 14) {
630 f->packed_at_lsb = 1;
631 f->avctx->pix_fmt = AV_PIX_FMT_GRAY14;
632 } else if (f->avctx->bits_per_raw_sample == 16) {
633 f->packed_at_lsb = 1;
634 f->avctx->pix_fmt = AV_PIX_FMT_GRAY16;
635 } else if (f->avctx->bits_per_raw_sample < 16) {
636 f->avctx->pix_fmt = AV_PIX_FMT_GRAY16;
637 } else
638 return AVERROR(ENOSYS);
639
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 157 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
157 } else if (f->transparency && !f->chroma_planes) {
640 if (f->avctx->bits_per_raw_sample <= 8)
641 f->avctx->pix_fmt = AV_PIX_FMT_YA8;
642 else
643 return AVERROR(ENOSYS);
644
3/4
✓ Branch 0 taken 107 times.
✓ Branch 1 taken 50 times.
✓ Branch 2 taken 107 times.
✗ Branch 3 not taken.
157 } else if (f->avctx->bits_per_raw_sample<=8 && !f->transparency) {
645
1/7
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 107 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
107 switch(16 * f->chroma_h_shift + f->chroma_v_shift) {
646 case 0x00: f->avctx->pix_fmt = AV_PIX_FMT_YUV444P; break;
647 case 0x01: f->avctx->pix_fmt = AV_PIX_FMT_YUV440P; break;
648 case 0x10: f->avctx->pix_fmt = AV_PIX_FMT_YUV422P; break;
649 107 case 0x11: f->avctx->pix_fmt = AV_PIX_FMT_YUV420P; break;
650 case 0x20: f->avctx->pix_fmt = AV_PIX_FMT_YUV411P; break;
651 case 0x22: f->avctx->pix_fmt = AV_PIX_FMT_YUV410P; break;
652 }
653
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 50 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
50 } else if (f->avctx->bits_per_raw_sample <= 8 && f->transparency) {
654 switch(16*f->chroma_h_shift + f->chroma_v_shift) {
655 case 0x00: f->avctx->pix_fmt = AV_PIX_FMT_YUVA444P; break;
656 case 0x10: f->avctx->pix_fmt = AV_PIX_FMT_YUVA422P; break;
657 case 0x11: f->avctx->pix_fmt = AV_PIX_FMT_YUVA420P; break;
658 }
659
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 50 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
50 } else if (f->avctx->bits_per_raw_sample == 9 && !f->transparency) {
660 f->packed_at_lsb = 1;
661 switch(16 * f->chroma_h_shift + f->chroma_v_shift) {
662 case 0x00: f->avctx->pix_fmt = AV_PIX_FMT_YUV444P9; break;
663 case 0x10: f->avctx->pix_fmt = AV_PIX_FMT_YUV422P9; break;
664 case 0x11: f->avctx->pix_fmt = AV_PIX_FMT_YUV420P9; break;
665 }
666
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 50 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
50 } else if (f->avctx->bits_per_raw_sample == 9 && f->transparency) {
667 f->packed_at_lsb = 1;
668 switch(16 * f->chroma_h_shift + f->chroma_v_shift) {
669 case 0x00: f->avctx->pix_fmt = AV_PIX_FMT_YUVA444P9; break;
670 case 0x10: f->avctx->pix_fmt = AV_PIX_FMT_YUVA422P9; break;
671 case 0x11: f->avctx->pix_fmt = AV_PIX_FMT_YUVA420P9; break;
672 }
673
3/4
✓ Branch 0 taken 26 times.
✓ Branch 1 taken 24 times.
✓ Branch 2 taken 26 times.
✗ Branch 3 not taken.
50 } else if (f->avctx->bits_per_raw_sample == 10 && !f->transparency) {
674 26 f->packed_at_lsb = 1;
675
1/5
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✓ Branch 2 taken 26 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
26 switch(16 * f->chroma_h_shift + f->chroma_v_shift) {
676 case 0x00: f->avctx->pix_fmt = AV_PIX_FMT_YUV444P10; break;
677 case 0x01: f->avctx->pix_fmt = AV_PIX_FMT_YUV440P10; break;
678 26 case 0x10: f->avctx->pix_fmt = AV_PIX_FMT_YUV422P10; break;
679 case 0x11: f->avctx->pix_fmt = AV_PIX_FMT_YUV420P10; break;
680 }
681
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
24 } else if (f->avctx->bits_per_raw_sample == 10 && f->transparency) {
682 f->packed_at_lsb = 1;
683 switch(16 * f->chroma_h_shift + f->chroma_v_shift) {
684 case 0x00: f->avctx->pix_fmt = AV_PIX_FMT_YUVA444P10; break;
685 case 0x10: f->avctx->pix_fmt = AV_PIX_FMT_YUVA422P10; break;
686 case 0x11: f->avctx->pix_fmt = AV_PIX_FMT_YUVA420P10; break;
687 }
688
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
24 } else if (f->avctx->bits_per_raw_sample == 12 && !f->transparency) {
689 f->packed_at_lsb = 1;
690 switch(16 * f->chroma_h_shift + f->chroma_v_shift) {
691 case 0x00: f->avctx->pix_fmt = AV_PIX_FMT_YUV444P12; break;
692 case 0x01: f->avctx->pix_fmt = AV_PIX_FMT_YUV440P12; break;
693 case 0x10: f->avctx->pix_fmt = AV_PIX_FMT_YUV422P12; break;
694 case 0x11: f->avctx->pix_fmt = AV_PIX_FMT_YUV420P12; break;
695 }
696
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
24 } else if (f->avctx->bits_per_raw_sample == 12 && f->transparency) {
697 f->packed_at_lsb = 1;
698 switch(16 * f->chroma_h_shift + f->chroma_v_shift) {
699 case 0x00: f->avctx->pix_fmt = AV_PIX_FMT_YUVA444P12; break;
700 case 0x10: f->avctx->pix_fmt = AV_PIX_FMT_YUVA422P12; break;
701 }
702
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
24 } else if (f->avctx->bits_per_raw_sample == 14 && !f->transparency) {
703 f->packed_at_lsb = 1;
704 switch(16 * f->chroma_h_shift + f->chroma_v_shift) {
705 case 0x00: f->avctx->pix_fmt = AV_PIX_FMT_YUV444P14; break;
706 case 0x10: f->avctx->pix_fmt = AV_PIX_FMT_YUV422P14; break;
707 case 0x11: f->avctx->pix_fmt = AV_PIX_FMT_YUV420P14; break;
708 }
709
2/4
✓ Branch 0 taken 24 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 24 times.
✗ Branch 3 not taken.
24 } else if (f->avctx->bits_per_raw_sample == 16 && !f->transparency){
710 24 f->packed_at_lsb = 1;
711
1/4
✓ Branch 0 taken 24 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
24 switch(16 * f->chroma_h_shift + f->chroma_v_shift) {
712 24 case 0x00: f->avctx->pix_fmt = AV_PIX_FMT_YUV444P16; break;
713 case 0x10: f->avctx->pix_fmt = AV_PIX_FMT_YUV422P16; break;
714 case 0x11: f->avctx->pix_fmt = AV_PIX_FMT_YUV420P16; break;
715 }
716 } else if (f->avctx->bits_per_raw_sample == 16 && f->transparency){
717 f->packed_at_lsb = 1;
718 switch(16 * f->chroma_h_shift + f->chroma_v_shift) {
719 case 0x00: f->avctx->pix_fmt = AV_PIX_FMT_YUVA444P16; break;
720 case 0x10: f->avctx->pix_fmt = AV_PIX_FMT_YUVA422P16; break;
721 case 0x11: f->avctx->pix_fmt = AV_PIX_FMT_YUVA420P16; break;
722 }
723 }
724
1/2
✓ Branch 0 taken 48 times.
✗ Branch 1 not taken.
48 } else if (f->colorspace == 1) {
725
2/4
✓ Branch 0 taken 48 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 48 times.
48 if (f->chroma_h_shift || f->chroma_v_shift) {
726 av_log(f->avctx, AV_LOG_ERROR,
727 "chroma subsampling not supported in this colorspace\n");
728 return AVERROR(ENOSYS);
729 }
730
3/4
✓ Branch 0 taken 24 times.
✓ Branch 1 taken 24 times.
✓ Branch 2 taken 24 times.
✗ Branch 3 not taken.
48 if ( f->avctx->bits_per_raw_sample <= 8 && !f->transparency)
731 24 f->avctx->pix_fmt = AV_PIX_FMT_0RGB32;
732
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
24 else if (f->avctx->bits_per_raw_sample <= 8 && f->transparency)
733 f->avctx->pix_fmt = AV_PIX_FMT_RGB32;
734
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
24 else if (f->avctx->bits_per_raw_sample == 9 && !f->transparency)
735 f->avctx->pix_fmt = AV_PIX_FMT_GBRP9;
736
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
24 else if (f->avctx->bits_per_raw_sample == 10 && !f->transparency)
737 f->avctx->pix_fmt = AV_PIX_FMT_GBRP10;
738
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
24 else if (f->avctx->bits_per_raw_sample == 10 && f->transparency)
739 f->avctx->pix_fmt = AV_PIX_FMT_GBRAP10;
740
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
24 else if (f->avctx->bits_per_raw_sample == 12 && !f->transparency)
741 f->avctx->pix_fmt = AV_PIX_FMT_GBRP12;
742
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
24 else if (f->avctx->bits_per_raw_sample == 12 && f->transparency)
743 f->avctx->pix_fmt = AV_PIX_FMT_GBRAP12;
744
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
24 else if (f->avctx->bits_per_raw_sample == 14 && !f->transparency)
745 f->avctx->pix_fmt = AV_PIX_FMT_GBRP14;
746
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
24 else if (f->avctx->bits_per_raw_sample == 14 && f->transparency)
747 f->avctx->pix_fmt = AV_PIX_FMT_GBRAP14;
748
2/4
✓ Branch 0 taken 24 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 24 times.
✗ Branch 3 not taken.
24 else if (f->avctx->bits_per_raw_sample == 16 && !f->transparency) {
749 24 f->avctx->pix_fmt = AV_PIX_FMT_GBRP16;
750 24 f->use32bit = 1;
751 }
752 else if (f->avctx->bits_per_raw_sample == 16 && f->transparency) {
753 f->avctx->pix_fmt = AV_PIX_FMT_GBRAP16;
754 f->use32bit = 1;
755 }
756 } else {
757 av_log(f->avctx, AV_LOG_ERROR, "colorspace not supported\n");
758 return AVERROR(ENOSYS);
759 }
760
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 205 times.
205 if (f->avctx->pix_fmt == AV_PIX_FMT_NONE) {
761 av_log(f->avctx, AV_LOG_ERROR, "format not supported\n");
762 return AVERROR(ENOSYS);
763 }
764
765 ff_dlog(f->avctx, "%d %d %d\n",
766 f->chroma_h_shift, f->chroma_v_shift, f->avctx->pix_fmt);
767
2/2
✓ Branch 0 taken 30 times.
✓ Branch 1 taken 175 times.
205 if (f->version < 2) {
768 30 context_count = read_quant_tables(c, f->quant_table);
769
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 30 times.
30 if (context_count < 0) {
770 av_log(f->avctx, AV_LOG_ERROR, "read_quant_table error\n");
771 return AVERROR_INVALIDDATA;
772 }
773 30 f->slice_count = f->max_slice_count;
774
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 175 times.
175 } else if (f->version < 3) {
775 f->slice_count = get_symbol(c, state, 0);
776 } else {
777 175 const uint8_t *p = c->bytestream_end;
778 175 for (f->slice_count = 0;
779
4/6
✓ Branch 0 taken 875 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 875 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 700 times.
✓ Branch 5 taken 175 times.
875 f->slice_count < MAX_SLICES && 3 + 5*!!f->ec < p - c->bytestream_start;
780 700 f->slice_count++) {
781
1/2
✓ Branch 0 taken 700 times.
✗ Branch 1 not taken.
700 int trailer = 3 + 5*!!f->ec;
782 700 int size = AV_RB24(p-trailer);
783
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 700 times.
700 if (size + trailer > p - c->bytestream_start)
784 break;
785 700 p -= size + trailer;
786 }
787 }
788
3/6
✓ Branch 0 taken 205 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 205 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 205 times.
205 if (f->slice_count > (unsigned)MAX_SLICES || f->slice_count <= 0 || f->slice_count > f->max_slice_count) {
789 av_log(f->avctx, AV_LOG_ERROR, "slice count %d is invalid (max=%d)\n", f->slice_count, f->max_slice_count);
790 return AVERROR_INVALIDDATA;
791 }
792
793
2/2
✓ Branch 0 taken 730 times.
✓ Branch 1 taken 205 times.
935 for (j = 0; j < f->slice_count; j++) {
794 730 FFV1Context *fs = f->slice_context[j];
795 730 fs->ac = f->ac;
796 730 fs->packed_at_lsb = f->packed_at_lsb;
797
798 730 fs->slice_damaged = 0;
799
800
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 730 times.
730 if (f->version == 2) {
801 int sx = get_symbol(c, state, 0);
802 int sy = get_symbol(c, state, 0);
803 int sw = get_symbol(c, state, 0) + 1U;
804 int sh = get_symbol(c, state, 0) + 1U;
805
806 if (sx < 0 || sy < 0 || sw <= 0 || sh <= 0)
807 return AVERROR_INVALIDDATA;
808 if (sx > f->num_h_slices - sw || sy > f->num_v_slices - sh)
809 return AVERROR_INVALIDDATA;
810
811 fs->slice_x = sx * (int64_t)f->width / f->num_h_slices;
812 fs->slice_y = sy * (int64_t)f->height / f->num_v_slices;
813 fs->slice_width = (sx + sw) * (int64_t)f->width / f->num_h_slices - fs->slice_x;
814 fs->slice_height = (sy + sh) * (int64_t)f->height / f->num_v_slices - fs->slice_y;
815
816 av_assert0((unsigned)fs->slice_width <= f->width &&
817 (unsigned)fs->slice_height <= f->height);
818 av_assert0 ( (unsigned)fs->slice_x + (uint64_t)fs->slice_width <= f->width
819 && (unsigned)fs->slice_y + (uint64_t)fs->slice_height <= f->height);
820 }
821
822
2/2
✓ Branch 0 taken 1460 times.
✓ Branch 1 taken 730 times.
2190 for (i = 0; i < f->plane_count; i++) {
823 1460 PlaneContext *const p = &fs->plane[i];
824
825
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1460 times.
1460 if (f->version == 2) {
826 int idx = get_symbol(c, state, 0);
827 if (idx >= (unsigned)f->quant_table_count) {
828 av_log(f->avctx, AV_LOG_ERROR,
829 "quant_table_index out of range\n");
830 return AVERROR_INVALIDDATA;
831 }
832 p->quant_table_index = idx;
833 memcpy(p->quant_table, f->quant_tables[idx],
834 sizeof(p->quant_table));
835 context_count = f->context_count[idx];
836 } else {
837 1460 memcpy(p->quant_table, f->quant_table, sizeof(p->quant_table));
838 }
839
840
2/2
✓ Branch 0 taken 60 times.
✓ Branch 1 taken 1400 times.
1460 if (f->version <= 2) {
841
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 60 times.
60 av_assert0(context_count >= 0);
842
2/2
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 40 times.
60 if (p->context_count < context_count) {
843 20 av_freep(&p->state);
844 20 av_freep(&p->vlc_state);
845 }
846 60 p->context_count = context_count;
847 }
848 }
849 }
850 205 return 0;
851 }
852
853 72 static av_cold int decode_init(AVCodecContext *avctx)
854 {
855 72 FFV1Context *f = avctx->priv_data;
856 int ret;
857
858
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 72 times.
72 if ((ret = ff_ffv1_common_init(avctx)) < 0)
859 return ret;
860
861
3/4
✓ Branch 0 taken 62 times.
✓ Branch 1 taken 10 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 62 times.
72 if (avctx->extradata_size > 0 && (ret = read_extra_header(f)) < 0)
862 return ret;
863
864
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 72 times.
72 if ((ret = ff_ffv1_init_slice_contexts(f)) < 0)
865 return ret;
866
867 72 return 0;
868 }
869
870 1712 static int decode_frame(AVCodecContext *avctx, AVFrame *rframe,
871 int *got_frame, AVPacket *avpkt)
872 {
873 1712 uint8_t *buf = avpkt->data;
874 1712 int buf_size = avpkt->size;
875 1712 FFV1Context *f = avctx->priv_data;
876 1712 RangeCoder *const c = &f->slice_context[0]->c;
877 int i, ret, key_frame;
878 1712 uint8_t keystate = 128;
879 uint8_t *buf_p;
880 AVFrame *p;
881
882 1712 ff_progress_frame_unref(&f->last_picture);
883 1712 FFSWAP(ProgressFrame, f->picture, f->last_picture);
884
885
886 1712 f->avctx = avctx;
887 1712 ff_init_range_decoder(c, buf, buf_size);
888 1712 ff_build_rac_states(c, 0.05 * (1LL << 32), 256 - 8);
889
890
2/2
✓ Branch 1 taken 205 times.
✓ Branch 2 taken 1507 times.
1712 if (get_rac(c, &keystate)) {
891 205 key_frame = AV_FRAME_FLAG_KEY;
892 205 f->key_frame_ok = 0;
893
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 205 times.
205 if ((ret = read_header(f)) < 0)
894 return ret;
895 205 f->key_frame_ok = 1;
896 } else {
897
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1507 times.
1507 if (!f->key_frame_ok) {
898 av_log(avctx, AV_LOG_ERROR,
899 "Cannot decode non-keyframe without valid keyframe\n");
900 return AVERROR_INVALIDDATA;
901 }
902 1507 key_frame = 0;
903 }
904
905
2/2
✓ Branch 0 taken 818 times.
✓ Branch 1 taken 894 times.
1712 if (f->ac != AC_GOLOMB_RICE) {
906
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 818 times.
818 if (buf_size < avctx->width * avctx->height / (128*8))
907 return AVERROR_INVALIDDATA;
908 } else {
909 894 int w = avctx->width;
910 894 int s = 1 + w / (1<<23);
911
912 894 w /= s;
913
914
2/2
✓ Branch 0 taken 20591 times.
✓ Branch 1 taken 894 times.
21485 for (i = 0; w > (1<<ff_log2_run[i]); i++)
915 20591 w -= ff_log2_run[i];
916
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 894 times.
894 if (buf_size < (avctx->height + i + 6) / 8 * s)
917 return AVERROR_INVALIDDATA;
918 }
919
920 1712 ret = ff_progress_frame_get_buffer(avctx, &f->picture,
921 AV_GET_BUFFER_FLAG_REF);
922
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1712 times.
1712 if (ret < 0)
923 return ret;
924
925 1712 f->cur = p = f->picture.f;
926
927 1712 p->pict_type = AV_PICTURE_TYPE_I; //FIXME I vs. P
928 1712 p->flags = (p->flags & ~AV_FRAME_FLAG_KEY) | key_frame;
929
930
3/4
✓ Branch 0 taken 255 times.
✓ Branch 1 taken 1457 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 255 times.
1712 if (f->version < 3 && avctx->field_order > AV_FIELD_PROGRESSIVE) {
931 /* we have interlaced material flagged in container */
932 p->flags |= AV_FRAME_FLAG_INTERLACED;
933 if (avctx->field_order == AV_FIELD_TT || avctx->field_order == AV_FIELD_TB)
934 p->flags |= AV_FRAME_FLAG_TOP_FIELD_FIRST;
935 }
936
937
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1712 times.
1712 if (avctx->debug & FF_DEBUG_PICT_INFO)
938 av_log(avctx, AV_LOG_DEBUG, "ver:%d keyframe:%d coder:%d ec:%d slices:%d bps:%d\n",
939 f->version, !!(p->flags & AV_FRAME_FLAG_KEY), f->ac, f->ec, f->slice_count, f->avctx->bits_per_raw_sample);
940
941 1712 ff_thread_finish_setup(avctx);
942
943 1712 buf_p = buf + buf_size;
944
2/2
✓ Branch 0 taken 6083 times.
✓ Branch 1 taken 1712 times.
7795 for (i = f->slice_count - 1; i >= 0; i--) {
945 6083 FFV1Context *fs = f->slice_context[i];
946
2/2
✓ Branch 0 taken 5828 times.
✓ Branch 1 taken 255 times.
6083 int trailer = 3 + 5*!!f->ec;
947 int v;
948
949
4/4
✓ Branch 0 taken 1712 times.
✓ Branch 1 taken 4371 times.
✓ Branch 2 taken 1457 times.
✓ Branch 3 taken 255 times.
6083 if (i || f->version > 2) {
950
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5828 times.
5828 if (trailer > buf_p - buf) v = INT_MAX;
951 5828 else v = AV_RB24(buf_p-trailer) + trailer;
952 255 } else v = buf_p - c->bytestream_start;
953
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6083 times.
6083 if (buf_p - c->bytestream_start < v) {
954 av_log(avctx, AV_LOG_ERROR, "Slice pointer chain broken\n");
955 ff_progress_frame_report(&f->picture, INT_MAX);
956 return AVERROR_INVALIDDATA;
957 }
958 6083 buf_p -= v;
959
960
2/2
✓ Branch 0 taken 5828 times.
✓ Branch 1 taken 255 times.
6083 if (f->ec) {
961 5828 unsigned crc = av_crc(av_crc_get_table(AV_CRC_32_IEEE), 0, buf_p, v);
962
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5828 times.
5828 if (crc) {
963 int64_t ts = avpkt->pts != AV_NOPTS_VALUE ? avpkt->pts : avpkt->dts;
964 av_log(f->avctx, AV_LOG_ERROR, "slice CRC mismatch %X!", crc);
965 if (ts != AV_NOPTS_VALUE && avctx->pkt_timebase.num) {
966 av_log(f->avctx, AV_LOG_ERROR, "at %f seconds\n", ts*av_q2d(avctx->pkt_timebase));
967 } else if (ts != AV_NOPTS_VALUE) {
968 av_log(f->avctx, AV_LOG_ERROR, "at %"PRId64"\n", ts);
969 } else {
970 av_log(f->avctx, AV_LOG_ERROR, "\n");
971 }
972 fs->slice_damaged = 1;
973 }
974
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5828 times.
5828 if (avctx->debug & FF_DEBUG_PICT_INFO) {
975 av_log(avctx, AV_LOG_DEBUG, "slice %d, CRC: 0x%08"PRIX32"\n", i, AV_RB32(buf_p + v - 4));
976 }
977 }
978
979
2/2
✓ Branch 0 taken 4371 times.
✓ Branch 1 taken 1712 times.
6083 if (i) {
980 4371 ff_init_range_decoder(&fs->c, buf_p, v);
981 } else
982 1712 fs->c.bytestream_end = buf_p + v;
983
984 6083 fs->avctx = avctx;
985 }
986
987 1712 avctx->execute(avctx,
988 decode_slice,
989 1712 &f->slice_context[0],
990 NULL,
991 f->slice_count,
992 sizeof(void*));
993
994
2/2
✓ Branch 0 taken 6083 times.
✓ Branch 1 taken 1712 times.
7795 for (i = f->slice_count - 1; i >= 0; i--) {
995 6083 FFV1Context *fs = f->slice_context[i];
996 int j;
997
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 6083 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
6083 if (fs->slice_damaged && f->last_picture.f) {
998 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(avctx->pix_fmt);
999 const uint8_t *src[4];
1000 uint8_t *dst[4];
1001 ff_progress_frame_await(&f->last_picture, INT_MAX);
1002 for (j = 0; j < desc->nb_components; j++) {
1003 int pixshift = desc->comp[j].depth > 8;
1004 int sh = (j == 1 || j == 2) ? f->chroma_h_shift : 0;
1005 int sv = (j == 1 || j == 2) ? f->chroma_v_shift : 0;
1006 dst[j] = p->data[j] + p->linesize[j] *
1007 (fs->slice_y >> sv) + ((fs->slice_x >> sh) << pixshift);
1008 src[j] = f->last_picture.f->data[j] + f->last_picture.f->linesize[j] *
1009 (fs->slice_y >> sv) + ((fs->slice_x >> sh) << pixshift);
1010
1011 }
1012 if (desc->flags & AV_PIX_FMT_FLAG_PAL) {
1013 dst[1] = p->data[1];
1014 src[1] = f->last_picture.f->data[1];
1015 }
1016 av_image_copy(dst, p->linesize, src,
1017 f->last_picture.f->linesize,
1018 avctx->pix_fmt,
1019 fs->slice_width,
1020 fs->slice_height);
1021 }
1022 }
1023 1712 ff_progress_frame_report(&f->picture, INT_MAX);
1024
1025 1712 ff_progress_frame_unref(&f->last_picture);
1026
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1712 times.
1712 if ((ret = av_frame_ref(rframe, f->picture.f)) < 0)
1027 return ret;
1028
1029 1712 *got_frame = 1;
1030
1031 1712 return buf_size;
1032 }
1033
1034 static void copy_fields(FFV1Context *fsdst, const FFV1Context *fssrc,
1035 const FFV1Context *fsrc)
1036 {
1037 fsdst->version = fsrc->version;
1038 fsdst->micro_version = fsrc->micro_version;
1039 fsdst->chroma_planes = fsrc->chroma_planes;
1040 fsdst->chroma_h_shift = fsrc->chroma_h_shift;
1041 fsdst->chroma_v_shift = fsrc->chroma_v_shift;
1042 fsdst->transparency = fsrc->transparency;
1043 fsdst->plane_count = fsrc->plane_count;
1044 fsdst->ac = fsrc->ac;
1045 fsdst->colorspace = fsrc->colorspace;
1046
1047 fsdst->ec = fsrc->ec;
1048 fsdst->intra = fsrc->intra;
1049 fsdst->slice_damaged = fssrc->slice_damaged;
1050 fsdst->key_frame_ok = fsrc->key_frame_ok;
1051
1052 fsdst->packed_at_lsb = fsrc->packed_at_lsb;
1053 fsdst->slice_count = fsrc->slice_count;
1054 if (fsrc->version<3){
1055 fsdst->slice_x = fssrc->slice_x;
1056 fsdst->slice_y = fssrc->slice_y;
1057 fsdst->slice_width = fssrc->slice_width;
1058 fsdst->slice_height = fssrc->slice_height;
1059 }
1060 }
1061
1062 #if HAVE_THREADS
1063 static int update_thread_context(AVCodecContext *dst, const AVCodecContext *src)
1064 {
1065 FFV1Context *fsrc = src->priv_data;
1066 FFV1Context *fdst = dst->priv_data;
1067 int i;
1068
1069 if (dst == src)
1070 return 0;
1071
1072 copy_fields(fdst, fsrc, fsrc);
1073 fdst->use32bit = fsrc->use32bit;
1074 memcpy(fdst->state_transition, fsrc->state_transition,
1075 sizeof(fdst->state_transition));
1076 memcpy(fdst->quant_table, fsrc->quant_table, sizeof(fsrc->quant_table));
1077
1078 for (i = 0; i < fdst->num_h_slices * fdst->num_v_slices; i++) {
1079 FFV1Context *fssrc = fsrc->slice_context[i];
1080 FFV1Context *fsdst = fdst->slice_context[i];
1081 copy_fields(fsdst, fssrc, fsrc);
1082 }
1083 av_assert0(!fdst->plane[0].state);
1084 av_assert0(!fdst->sample_buffer);
1085
1086 av_assert1(fdst->max_slice_count == fsrc->max_slice_count);
1087
1088 ff_progress_frame_replace(&fdst->picture, &fsrc->picture);
1089
1090 fdst->fsrc = fsrc;
1091
1092 return 0;
1093 }
1094 #endif
1095
1096 72 static av_cold int ffv1_decode_close(AVCodecContext *avctx)
1097 {
1098 72 FFV1Context *const s = avctx->priv_data;
1099
1100 72 ff_progress_frame_unref(&s->picture);
1101 72 ff_progress_frame_unref(&s->last_picture);
1102
1103 72 return ff_ffv1_close(avctx);
1104 }
1105
1106 const FFCodec ff_ffv1_decoder = {
1107 .p.name = "ffv1",
1108 CODEC_LONG_NAME("FFmpeg video codec #1"),
1109 .p.type = AVMEDIA_TYPE_VIDEO,
1110 .p.id = AV_CODEC_ID_FFV1,
1111 .priv_data_size = sizeof(FFV1Context),
1112 .init = decode_init,
1113 .close = ffv1_decode_close,
1114 FF_CODEC_DECODE_CB(decode_frame),
1115 UPDATE_THREAD_CONTEXT(update_thread_context),
1116 .p.capabilities = AV_CODEC_CAP_DR1 |
1117 AV_CODEC_CAP_FRAME_THREADS | AV_CODEC_CAP_SLICE_THREADS,
1118 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP |
1119 FF_CODEC_CAP_USES_PROGRESSFRAMES,
1120 };
1121