Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | RSS feed

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