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