FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/ffv1dec.c
Date: 2022-07-05 19:52:29
Exec Total Coverage
Lines: 379 662 57.3%
Branches: 246 543 45.3%

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