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