LCOV - code coverage report
Current view: top level - libavcodec - vp9.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 809 1142 70.8 %
Date: 2017-12-14 08:27:08 Functions: 20 28 71.4 %

          Line data    Source code
       1             : /*
       2             :  * VP9 compatible video decoder
       3             :  *
       4             :  * Copyright (C) 2013 Ronald S. Bultje <rsbultje gmail com>
       5             :  * Copyright (C) 2013 Clément Bœsch <u pkh me>
       6             :  *
       7             :  * This file is part of FFmpeg.
       8             :  *
       9             :  * FFmpeg is free software; you can redistribute it and/or
      10             :  * modify it under the terms of the GNU Lesser General Public
      11             :  * License as published by the Free Software Foundation; either
      12             :  * version 2.1 of the License, or (at your option) any later version.
      13             :  *
      14             :  * FFmpeg is distributed in the hope that it will be useful,
      15             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      16             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      17             :  * Lesser General Public License for more details.
      18             :  *
      19             :  * You should have received a copy of the GNU Lesser General Public
      20             :  * License along with FFmpeg; if not, write to the Free Software
      21             :  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
      22             :  */
      23             : 
      24             : #include "avcodec.h"
      25             : #include "get_bits.h"
      26             : #include "hwaccel.h"
      27             : #include "internal.h"
      28             : #include "profiles.h"
      29             : #include "thread.h"
      30             : #include "videodsp.h"
      31             : #include "vp56.h"
      32             : #include "vp9.h"
      33             : #include "vp9data.h"
      34             : #include "vp9dec.h"
      35             : #include "libavutil/avassert.h"
      36             : #include "libavutil/pixdesc.h"
      37             : 
      38             : #define VP9_SYNCCODE 0x498342
      39             : 
      40             : #if HAVE_THREADS
      41         904 : static void vp9_free_entries(AVCodecContext *avctx) {
      42         904 :     VP9Context *s = avctx->priv_data;
      43             : 
      44         904 :     if (avctx->active_thread_type & FF_THREAD_SLICE)  {
      45           0 :         pthread_mutex_destroy(&s->progress_mutex);
      46           0 :         pthread_cond_destroy(&s->progress_cond);
      47           0 :         av_freep(&s->entries);
      48             :     }
      49         904 : }
      50             : 
      51         451 : static int vp9_alloc_entries(AVCodecContext *avctx, int n) {
      52         451 :     VP9Context *s = avctx->priv_data;
      53             :     int i;
      54             : 
      55         451 :     if (avctx->active_thread_type & FF_THREAD_SLICE)  {
      56           0 :         if (s->entries)
      57           0 :             av_freep(&s->entries);
      58             : 
      59           0 :         s->entries = av_malloc_array(n, sizeof(atomic_int));
      60             : 
      61           0 :         if (!s->entries) {
      62           0 :             av_freep(&s->entries);
      63           0 :             return AVERROR(ENOMEM);
      64             :         }
      65             : 
      66           0 :         for (i  = 0; i < n; i++)
      67           0 :             atomic_init(&s->entries[i], 0);
      68             : 
      69           0 :         pthread_mutex_init(&s->progress_mutex, NULL);
      70           0 :         pthread_cond_init(&s->progress_cond, NULL);
      71             :     }
      72         451 :     return 0;
      73             : }
      74             : 
      75           0 : static void vp9_report_tile_progress(VP9Context *s, int field, int n) {
      76           0 :     pthread_mutex_lock(&s->progress_mutex);
      77           0 :     atomic_fetch_add_explicit(&s->entries[field], n, memory_order_release);
      78           0 :     pthread_cond_signal(&s->progress_cond);
      79           0 :     pthread_mutex_unlock(&s->progress_mutex);
      80           0 : }
      81             : 
      82           0 : static void vp9_await_tile_progress(VP9Context *s, int field, int n) {
      83           0 :     if (atomic_load_explicit(&s->entries[field], memory_order_acquire) >= n)
      84           0 :         return;
      85             : 
      86           0 :     pthread_mutex_lock(&s->progress_mutex);
      87           0 :     while (atomic_load_explicit(&s->entries[field], memory_order_relaxed) != n)
      88           0 :         pthread_cond_wait(&s->progress_cond, &s->progress_mutex);
      89           0 :     pthread_mutex_unlock(&s->progress_mutex);
      90             : }
      91             : #else
      92             : static void vp9_free_entries(AVCodecContext *avctx) {}
      93             : static int vp9_alloc_entries(AVCodecContext *avctx, int n) { return 0; }
      94             : #endif
      95             : 
      96        4320 : static void vp9_frame_unref(AVCodecContext *avctx, VP9Frame *f)
      97             : {
      98        4320 :     ff_thread_release_buffer(avctx, &f->tf);
      99        4320 :     av_buffer_unref(&f->extradata);
     100        4320 :     av_buffer_unref(&f->hwaccel_priv_buf);
     101        4320 :     f->segmentation_map = NULL;
     102        4320 :     f->hwaccel_picture_private = NULL;
     103        4320 : }
     104             : 
     105        2257 : static int vp9_frame_alloc(AVCodecContext *avctx, VP9Frame *f)
     106             : {
     107        2257 :     VP9Context *s = avctx->priv_data;
     108             :     int ret, sz;
     109             : 
     110        2257 :     ret = ff_thread_get_buffer(avctx, &f->tf, AV_GET_BUFFER_FLAG_REF);
     111        2257 :     if (ret < 0)
     112           0 :         return ret;
     113             : 
     114        2257 :     sz = 64 * s->sb_cols * s->sb_rows;
     115        2257 :     f->extradata = av_buffer_allocz(sz * (1 + sizeof(VP9mvrefPair)));
     116        2257 :     if (!f->extradata) {
     117           0 :         goto fail;
     118             :     }
     119             : 
     120        2257 :     f->segmentation_map = f->extradata->data;
     121        2257 :     f->mv = (VP9mvrefPair *) (f->extradata->data + sz);
     122             : 
     123        2257 :     if (avctx->hwaccel) {
     124           0 :         const AVHWAccel *hwaccel = avctx->hwaccel;
     125           0 :         av_assert0(!f->hwaccel_picture_private);
     126           0 :         if (hwaccel->frame_priv_data_size) {
     127           0 :             f->hwaccel_priv_buf = av_buffer_allocz(hwaccel->frame_priv_data_size);
     128           0 :             if (!f->hwaccel_priv_buf)
     129           0 :                 goto fail;
     130           0 :             f->hwaccel_picture_private = f->hwaccel_priv_buf->data;
     131             :         }
     132             :     }
     133             : 
     134        2257 :     return 0;
     135             : 
     136           0 : fail:
     137           0 :     vp9_frame_unref(avctx, f);
     138           0 :     return AVERROR(ENOMEM);
     139             : }
     140             : 
     141        2063 : static int vp9_frame_ref(AVCodecContext *avctx, VP9Frame *dst, VP9Frame *src)
     142             : {
     143             :     int ret;
     144             : 
     145        2063 :     ret = ff_thread_ref_frame(&dst->tf, &src->tf);
     146        2063 :     if (ret < 0)
     147           0 :         return ret;
     148             : 
     149        2063 :     dst->extradata = av_buffer_ref(src->extradata);
     150        2063 :     if (!dst->extradata)
     151           0 :         goto fail;
     152             : 
     153        2063 :     dst->segmentation_map = src->segmentation_map;
     154        2063 :     dst->mv = src->mv;
     155        2063 :     dst->uses_2pass = src->uses_2pass;
     156             : 
     157        2063 :     if (src->hwaccel_picture_private) {
     158           0 :         dst->hwaccel_priv_buf = av_buffer_ref(src->hwaccel_priv_buf);
     159           0 :         if (!dst->hwaccel_priv_buf)
     160           0 :             goto fail;
     161           0 :         dst->hwaccel_picture_private = dst->hwaccel_priv_buf->data;
     162             :     }
     163             : 
     164        2063 :     return 0;
     165             : 
     166           0 : fail:
     167           0 :     vp9_frame_unref(avctx, dst);
     168           0 :     return AVERROR(ENOMEM);
     169             : }
     170             : 
     171        2257 : static int update_size(AVCodecContext *avctx, int w, int h)
     172             : {
     173             : #define HWACCEL_MAX (CONFIG_VP9_DXVA2_HWACCEL + \
     174             :                      CONFIG_VP9_D3D11VA_HWACCEL * 2 + \
     175             :                      CONFIG_VP9_NVDEC_HWACCEL + \
     176             :                      CONFIG_VP9_VAAPI_HWACCEL)
     177        2257 :     enum AVPixelFormat pix_fmts[HWACCEL_MAX + 2], *fmtp = pix_fmts;
     178        2257 :     VP9Context *s = avctx->priv_data;
     179             :     uint8_t *p;
     180        2257 :     int bytesperpixel = s->bytesperpixel, ret, cols, rows;
     181             :     int lflvl_len, i;
     182             : 
     183        2257 :     av_assert0(w > 0 && h > 0);
     184             : 
     185        2257 :     if (!(s->pix_fmt == s->gf_fmt && w == s->w && h == s->h)) {
     186         453 :         if ((ret = ff_set_dimensions(avctx, w, h)) < 0)
     187           0 :             return ret;
     188             : 
     189         453 :         switch (s->pix_fmt) {
     190         431 :         case AV_PIX_FMT_YUV420P:
     191             : #if CONFIG_VP9_DXVA2_HWACCEL
     192             :             *fmtp++ = AV_PIX_FMT_DXVA2_VLD;
     193             : #endif
     194             : #if CONFIG_VP9_D3D11VA_HWACCEL
     195             :             *fmtp++ = AV_PIX_FMT_D3D11VA_VLD;
     196             :             *fmtp++ = AV_PIX_FMT_D3D11;
     197             : #endif
     198             : #if CONFIG_VP9_NVDEC_HWACCEL
     199         431 :             *fmtp++ = AV_PIX_FMT_CUDA;
     200             : #endif
     201             : #if CONFIG_VP9_VAAPI_HWACCEL
     202             :             *fmtp++ = AV_PIX_FMT_VAAPI;
     203             : #endif
     204         431 :             break;
     205           4 :         case AV_PIX_FMT_YUV420P10:
     206             :         case AV_PIX_FMT_YUV420P12:
     207             : #if CONFIG_VP9_NVDEC_HWACCEL
     208           4 :             *fmtp++ = AV_PIX_FMT_CUDA;
     209             : #endif
     210             : #if CONFIG_VP9_VAAPI_HWACCEL
     211             :             *fmtp++ = AV_PIX_FMT_VAAPI;
     212             : #endif
     213           4 :             break;
     214             :         }
     215             : 
     216         453 :         *fmtp++ = s->pix_fmt;
     217         453 :         *fmtp = AV_PIX_FMT_NONE;
     218             : 
     219         453 :         ret = ff_thread_get_format(avctx, pix_fmts);
     220         453 :         if (ret < 0)
     221           0 :             return ret;
     222             : 
     223         453 :         avctx->pix_fmt = ret;
     224         453 :         s->gf_fmt  = s->pix_fmt;
     225         453 :         s->w = w;
     226         453 :         s->h = h;
     227             :     }
     228             : 
     229        2257 :     cols = (w + 7) >> 3;
     230        2257 :     rows = (h + 7) >> 3;
     231             : 
     232        2257 :     if (s->intra_pred_data[0] && cols == s->cols && rows == s->rows && s->pix_fmt == s->last_fmt)
     233        1804 :         return 0;
     234             : 
     235         453 :     s->last_fmt  = s->pix_fmt;
     236         453 :     s->sb_cols   = (w + 63) >> 6;
     237         453 :     s->sb_rows   = (h + 63) >> 6;
     238         453 :     s->cols      = (w + 7) >> 3;
     239         453 :     s->rows      = (h + 7) >> 3;
     240         453 :     lflvl_len    = avctx->active_thread_type == FF_THREAD_SLICE ? s->sb_rows : 1;
     241             : 
     242             : #define assign(var, type, n) var = (type) p; p += s->sb_cols * (n) * sizeof(*var)
     243         453 :     av_freep(&s->intra_pred_data[0]);
     244             :     // FIXME we slightly over-allocate here for subsampled chroma, but a little
     245             :     // bit of padding shouldn't affect performance...
     246        1359 :     p = av_malloc(s->sb_cols * (128 + 192 * bytesperpixel +
     247         906 :                                 lflvl_len * sizeof(*s->lflvl) + 16 * sizeof(*s->above_mv_ctx)));
     248         453 :     if (!p)
     249           0 :         return AVERROR(ENOMEM);
     250         453 :     assign(s->intra_pred_data[0],  uint8_t *,             64 * bytesperpixel);
     251         453 :     assign(s->intra_pred_data[1],  uint8_t *,             64 * bytesperpixel);
     252         453 :     assign(s->intra_pred_data[2],  uint8_t *,             64 * bytesperpixel);
     253         453 :     assign(s->above_y_nnz_ctx,     uint8_t *,             16);
     254         453 :     assign(s->above_mode_ctx,      uint8_t *,             16);
     255         453 :     assign(s->above_mv_ctx,        VP56mv(*)[2],          16);
     256         453 :     assign(s->above_uv_nnz_ctx[0], uint8_t *,             16);
     257         453 :     assign(s->above_uv_nnz_ctx[1], uint8_t *,             16);
     258         453 :     assign(s->above_partition_ctx, uint8_t *,              8);
     259         453 :     assign(s->above_skip_ctx,      uint8_t *,              8);
     260         453 :     assign(s->above_txfm_ctx,      uint8_t *,              8);
     261         453 :     assign(s->above_segpred_ctx,   uint8_t *,              8);
     262         453 :     assign(s->above_intra_ctx,     uint8_t *,              8);
     263         453 :     assign(s->above_comp_ctx,      uint8_t *,              8);
     264         453 :     assign(s->above_ref_ctx,       uint8_t *,              8);
     265         453 :     assign(s->above_filter_ctx,    uint8_t *,              8);
     266         453 :     assign(s->lflvl,               VP9Filter *,            lflvl_len);
     267             : #undef assign
     268             : 
     269         453 :     if (s->td) {
     270           4 :         for (i = 0; i < s->active_tile_cols; i++) {
     271           2 :             av_freep(&s->td[i].b_base);
     272           2 :             av_freep(&s->td[i].block_base);
     273             :         }
     274             :     }
     275             : 
     276         453 :     if (s->s.h.bpp != s->last_bpp) {
     277         451 :         ff_vp9dsp_init(&s->dsp, s->s.h.bpp, avctx->flags & AV_CODEC_FLAG_BITEXACT);
     278         451 :         ff_videodsp_init(&s->vdsp, s->s.h.bpp);
     279         451 :         s->last_bpp = s->s.h.bpp;
     280             :     }
     281             : 
     282         453 :     return 0;
     283             : }
     284             : 
     285        2257 : static int update_block_buffers(AVCodecContext *avctx)
     286             : {
     287             :     int i;
     288        2257 :     VP9Context *s = avctx->priv_data;
     289        2257 :     int chroma_blocks, chroma_eobs, bytesperpixel = s->bytesperpixel;
     290        2257 :     VP9TileData *td = &s->td[0];
     291             : 
     292        2257 :     if (td->b_base && td->block_base && s->block_alloc_using_2pass == s->s.frames[CUR_FRAME].uses_2pass)
     293        1804 :         return 0;
     294             : 
     295         453 :     av_free(td->b_base);
     296         453 :     av_free(td->block_base);
     297         453 :     chroma_blocks = 64 * 64 >> (s->ss_h + s->ss_v);
     298         453 :     chroma_eobs   = 16 * 16 >> (s->ss_h + s->ss_v);
     299         453 :     if (s->s.frames[CUR_FRAME].uses_2pass) {
     300           0 :         int sbs = s->sb_cols * s->sb_rows;
     301             : 
     302           0 :         td->b_base = av_malloc_array(s->cols * s->rows, sizeof(VP9Block));
     303           0 :         td->block_base = av_mallocz(((64 * 64 + 2 * chroma_blocks) * bytesperpixel * sizeof(int16_t) +
     304           0 :                                     16 * 16 + 2 * chroma_eobs) * sbs);
     305           0 :         if (!td->b_base || !td->block_base)
     306           0 :             return AVERROR(ENOMEM);
     307           0 :         td->uvblock_base[0] = td->block_base + sbs * 64 * 64 * bytesperpixel;
     308           0 :         td->uvblock_base[1] = td->uvblock_base[0] + sbs * chroma_blocks * bytesperpixel;
     309           0 :         td->eob_base = (uint8_t *) (td->uvblock_base[1] + sbs * chroma_blocks * bytesperpixel);
     310           0 :         td->uveob_base[0] = td->eob_base + 16 * 16 * sbs;
     311           0 :         td->uveob_base[1] = td->uveob_base[0] + chroma_eobs * sbs;
     312             :     } else {
     313         453 :         for (i = 1; i < s->active_tile_cols; i++) {
     314           0 :             if (s->td[i].b_base && s->td[i].block_base) {
     315           0 :                 av_free(s->td[i].b_base);
     316           0 :                 av_free(s->td[i].block_base);
     317             :             }
     318             :         }
     319         906 :         for (i = 0; i < s->active_tile_cols; i++) {
     320         453 :             s->td[i].b_base = av_malloc(sizeof(VP9Block));
     321         906 :             s->td[i].block_base = av_mallocz((64 * 64 + 2 * chroma_blocks) * bytesperpixel * sizeof(int16_t) +
     322         453 :                                        16 * 16 + 2 * chroma_eobs);
     323         453 :             if (!s->td[i].b_base || !s->td[i].block_base)
     324           0 :                 return AVERROR(ENOMEM);
     325         453 :             s->td[i].uvblock_base[0] = s->td[i].block_base + 64 * 64 * bytesperpixel;
     326         453 :             s->td[i].uvblock_base[1] = s->td[i].uvblock_base[0] + chroma_blocks * bytesperpixel;
     327         453 :             s->td[i].eob_base = (uint8_t *) (s->td[i].uvblock_base[1] + chroma_blocks * bytesperpixel);
     328         453 :             s->td[i].uveob_base[0] = s->td[i].eob_base + 16 * 16;
     329         453 :             s->td[i].uveob_base[1] = s->td[i].uveob_base[0] + chroma_eobs;
     330             :         }
     331             :     }
     332         453 :     s->block_alloc_using_2pass = s->s.frames[CUR_FRAME].uses_2pass;
     333             : 
     334         453 :     return 0;
     335             : }
     336             : 
     337             : // The sign bit is at the end, not the start, of a bit sequence
     338        1619 : static av_always_inline int get_sbits_inv(GetBitContext *gb, int n)
     339             : {
     340        1619 :     int v = get_bits(gb, n);
     341        1619 :     return get_bits1(gb) ? -v : v;
     342             : }
     343             : 
     344       24476 : static av_always_inline int inv_recenter_nonneg(int v, int m)
     345             : {
     346       24476 :     if (v > 2 * m)
     347        6559 :         return v;
     348       17917 :     if (v & 1)
     349       10752 :         return m - ((v + 1) >> 1);
     350        7165 :     return m + (v >> 1);
     351             : }
     352             : 
     353             : // differential forward probability updates
     354       24476 : static int update_prob(VP56RangeCoder *c, int p)
     355             : {
     356             :     static const int inv_map_table[255] = {
     357             :           7,  20,  33,  46,  59,  72,  85,  98, 111, 124, 137, 150, 163, 176,
     358             :         189, 202, 215, 228, 241, 254,   1,   2,   3,   4,   5,   6,   8,   9,
     359             :          10,  11,  12,  13,  14,  15,  16,  17,  18,  19,  21,  22,  23,  24,
     360             :          25,  26,  27,  28,  29,  30,  31,  32,  34,  35,  36,  37,  38,  39,
     361             :          40,  41,  42,  43,  44,  45,  47,  48,  49,  50,  51,  52,  53,  54,
     362             :          55,  56,  57,  58,  60,  61,  62,  63,  64,  65,  66,  67,  68,  69,
     363             :          70,  71,  73,  74,  75,  76,  77,  78,  79,  80,  81,  82,  83,  84,
     364             :          86,  87,  88,  89,  90,  91,  92,  93,  94,  95,  96,  97,  99, 100,
     365             :         101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 112, 113, 114, 115,
     366             :         116, 117, 118, 119, 120, 121, 122, 123, 125, 126, 127, 128, 129, 130,
     367             :         131, 132, 133, 134, 135, 136, 138, 139, 140, 141, 142, 143, 144, 145,
     368             :         146, 147, 148, 149, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160,
     369             :         161, 162, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175,
     370             :         177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 190, 191,
     371             :         192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 203, 204, 205, 206,
     372             :         207, 208, 209, 210, 211, 212, 213, 214, 216, 217, 218, 219, 220, 221,
     373             :         222, 223, 224, 225, 226, 227, 229, 230, 231, 232, 233, 234, 235, 236,
     374             :         237, 238, 239, 240, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251,
     375             :         252, 253, 253,
     376             :     };
     377             :     int d;
     378             : 
     379             :     /* This code is trying to do a differential probability update. For a
     380             :      * current probability A in the range [1, 255], the difference to a new
     381             :      * probability of any value can be expressed differentially as 1-A, 255-A
     382             :      * where some part of this (absolute range) exists both in positive as
     383             :      * well as the negative part, whereas another part only exists in one
     384             :      * half. We're trying to code this shared part differentially, i.e.
     385             :      * times two where the value of the lowest bit specifies the sign, and
     386             :      * the single part is then coded on top of this. This absolute difference
     387             :      * then again has a value of [0, 254], but a bigger value in this range
     388             :      * indicates that we're further away from the original value A, so we
     389             :      * can code this as a VLC code, since higher values are increasingly
     390             :      * unlikely. The first 20 values in inv_map_table[] allow 'cheap, rough'
     391             :      * updates vs. the 'fine, exact' updates further down the range, which
     392             :      * adds one extra dimension to this differential update model. */
     393             : 
     394       24476 :     if (!vp8_rac_get(c)) {
     395       18298 :         d = vp8_rac_get_uint(c, 4) + 0;
     396        6178 :     } else if (!vp8_rac_get(c)) {
     397        2319 :         d = vp8_rac_get_uint(c, 4) + 16;
     398        3859 :     } else if (!vp8_rac_get(c)) {
     399        2670 :         d = vp8_rac_get_uint(c, 5) + 32;
     400             :     } else {
     401        1189 :         d = vp8_rac_get_uint(c, 7);
     402        1189 :         if (d >= 65)
     403         188 :             d = (d << 1) - 65 + vp8_rac_get(c);
     404        1189 :         d += 64;
     405             :         av_assert2(d < FF_ARRAY_ELEMS(inv_map_table));
     406             :     }
     407             : 
     408       35265 :     return p <= 128 ? 1 + inv_recenter_nonneg(inv_map_table[d], p - 1) :
     409       10789 :                     255 - inv_recenter_nonneg(inv_map_table[d], 255 - p);
     410             : }
     411             : 
     412         451 : static int read_colorspace_details(AVCodecContext *avctx)
     413             : {
     414             :     static const enum AVColorSpace colorspaces[8] = {
     415             :         AVCOL_SPC_UNSPECIFIED, AVCOL_SPC_BT470BG, AVCOL_SPC_BT709, AVCOL_SPC_SMPTE170M,
     416             :         AVCOL_SPC_SMPTE240M, AVCOL_SPC_BT2020_NCL, AVCOL_SPC_RESERVED, AVCOL_SPC_RGB,
     417             :     };
     418         451 :     VP9Context *s = avctx->priv_data;
     419         451 :     int bits = avctx->profile <= 1 ? 0 : 1 + get_bits1(&s->gb); // 0:8, 1:10, 2:12
     420             : 
     421         451 :     s->bpp_index = bits;
     422         451 :     s->s.h.bpp = 8 + bits * 2;
     423         451 :     s->bytesperpixel = (7 + s->s.h.bpp) >> 3;
     424         451 :     avctx->colorspace = colorspaces[get_bits(&s->gb, 3)];
     425         451 :     if (avctx->colorspace == AVCOL_SPC_RGB) { // RGB = profile 1
     426             :         static const enum AVPixelFormat pix_fmt_rgb[3] = {
     427             :             AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRP10, AV_PIX_FMT_GBRP12
     428             :         };
     429           0 :         s->ss_h = s->ss_v = 0;
     430           0 :         avctx->color_range = AVCOL_RANGE_JPEG;
     431           0 :         s->pix_fmt = pix_fmt_rgb[bits];
     432           0 :         if (avctx->profile & 1) {
     433           0 :             if (get_bits1(&s->gb)) {
     434           0 :                 av_log(avctx, AV_LOG_ERROR, "Reserved bit set in RGB\n");
     435           0 :                 return AVERROR_INVALIDDATA;
     436             :             }
     437             :         } else {
     438           0 :             av_log(avctx, AV_LOG_ERROR, "RGB not supported in profile %d\n",
     439             :                    avctx->profile);
     440           0 :             return AVERROR_INVALIDDATA;
     441             :         }
     442             :     } else {
     443             :         static const enum AVPixelFormat pix_fmt_for_ss[3][2 /* v */][2 /* h */] = {
     444             :             { { AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV422P },
     445             :               { AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV420P } },
     446             :             { { AV_PIX_FMT_YUV444P10, AV_PIX_FMT_YUV422P10 },
     447             :               { AV_PIX_FMT_YUV440P10, AV_PIX_FMT_YUV420P10 } },
     448             :             { { AV_PIX_FMT_YUV444P12, AV_PIX_FMT_YUV422P12 },
     449             :               { AV_PIX_FMT_YUV440P12, AV_PIX_FMT_YUV420P12 } }
     450             :         };
     451         451 :         avctx->color_range = get_bits1(&s->gb) ? AVCOL_RANGE_JPEG : AVCOL_RANGE_MPEG;
     452         451 :         if (avctx->profile & 1) {
     453          18 :             s->ss_h = get_bits1(&s->gb);
     454          18 :             s->ss_v = get_bits1(&s->gb);
     455          18 :             s->pix_fmt = pix_fmt_for_ss[bits][s->ss_v][s->ss_h];
     456          18 :             if (s->pix_fmt == AV_PIX_FMT_YUV420P) {
     457           0 :                 av_log(avctx, AV_LOG_ERROR, "YUV 4:2:0 not supported in profile %d\n",
     458             :                        avctx->profile);
     459           0 :                 return AVERROR_INVALIDDATA;
     460          18 :             } else if (get_bits1(&s->gb)) {
     461           0 :                 av_log(avctx, AV_LOG_ERROR, "Profile %d color details reserved bit set\n",
     462             :                        avctx->profile);
     463           0 :                 return AVERROR_INVALIDDATA;
     464             :             }
     465             :         } else {
     466         433 :             s->ss_h = s->ss_v = 1;
     467         433 :             s->pix_fmt = pix_fmt_for_ss[bits][1][1];
     468             :         }
     469             :     }
     470             : 
     471         451 :     return 0;
     472             : }
     473             : 
     474        2276 : static int decode_frame_header(AVCodecContext *avctx,
     475             :                                const uint8_t *data, int size, int *ref)
     476             : {
     477        2276 :     VP9Context *s = avctx->priv_data;
     478             :     int c, i, j, k, l, m, n, w, h, max, size2, ret, sharp;
     479             :     int last_invisible;
     480             :     const uint8_t *data2;
     481             : 
     482             :     /* general header */
     483        2276 :     if ((ret = init_get_bits8(&s->gb, data, size)) < 0) {
     484           0 :         av_log(avctx, AV_LOG_ERROR, "Failed to initialize bitstream reader\n");
     485           0 :         return ret;
     486             :     }
     487        2276 :     if (get_bits(&s->gb, 2) != 0x2) { // frame marker
     488           0 :         av_log(avctx, AV_LOG_ERROR, "Invalid frame marker\n");
     489           0 :         return AVERROR_INVALIDDATA;
     490             :     }
     491        2276 :     avctx->profile  = get_bits1(&s->gb);
     492        2276 :     avctx->profile |= get_bits1(&s->gb) << 1;
     493        2276 :     if (avctx->profile == 3) avctx->profile += get_bits1(&s->gb);
     494        2276 :     if (avctx->profile > 3) {
     495           0 :         av_log(avctx, AV_LOG_ERROR, "Profile %d is not yet supported\n", avctx->profile);
     496           0 :         return AVERROR_INVALIDDATA;
     497             :     }
     498        2276 :     s->s.h.profile = avctx->profile;
     499        2276 :     if (get_bits1(&s->gb)) {
     500          19 :         *ref = get_bits(&s->gb, 3);
     501          19 :         return 0;
     502             :     }
     503             : 
     504        2257 :     s->last_keyframe  = s->s.h.keyframe;
     505        2257 :     s->s.h.keyframe   = !get_bits1(&s->gb);
     506             : 
     507        2257 :     last_invisible   = s->s.h.invisible;
     508        2257 :     s->s.h.invisible = !get_bits1(&s->gb);
     509        2257 :     s->s.h.errorres  = get_bits1(&s->gb);
     510        2257 :     s->s.h.use_last_frame_mvs = !s->s.h.errorres && !last_invisible;
     511             : 
     512        2257 :     if (s->s.h.keyframe) {
     513         451 :         if (get_bits_long(&s->gb, 24) != VP9_SYNCCODE) { // synccode
     514           0 :             av_log(avctx, AV_LOG_ERROR, "Invalid sync code\n");
     515           0 :             return AVERROR_INVALIDDATA;
     516             :         }
     517         451 :         if ((ret = read_colorspace_details(avctx)) < 0)
     518           0 :             return ret;
     519             :         // for profile 1, here follows the subsampling bits
     520         451 :         s->s.h.refreshrefmask = 0xff;
     521         451 :         w = get_bits(&s->gb, 16) + 1;
     522         451 :         h = get_bits(&s->gb, 16) + 1;
     523         451 :         if (get_bits1(&s->gb)) // display size
     524           0 :             skip_bits(&s->gb, 32);
     525             :     } else {
     526        1806 :         s->s.h.intraonly = s->s.h.invisible ? get_bits1(&s->gb) : 0;
     527        1806 :         s->s.h.resetctx  = s->s.h.errorres ? 0 : get_bits(&s->gb, 2);
     528        1806 :         if (s->s.h.intraonly) {
     529           6 :             if (get_bits_long(&s->gb, 24) != VP9_SYNCCODE) { // synccode
     530           0 :                 av_log(avctx, AV_LOG_ERROR, "Invalid sync code\n");
     531           0 :                 return AVERROR_INVALIDDATA;
     532             :             }
     533           6 :             if (avctx->profile >= 1) {
     534           0 :                 if ((ret = read_colorspace_details(avctx)) < 0)
     535           0 :                     return ret;
     536             :             } else {
     537           6 :                 s->ss_h = s->ss_v = 1;
     538           6 :                 s->s.h.bpp = 8;
     539           6 :                 s->bpp_index = 0;
     540           6 :                 s->bytesperpixel = 1;
     541           6 :                 s->pix_fmt = AV_PIX_FMT_YUV420P;
     542           6 :                 avctx->colorspace = AVCOL_SPC_BT470BG;
     543           6 :                 avctx->color_range = AVCOL_RANGE_MPEG;
     544             :             }
     545           6 :             s->s.h.refreshrefmask = get_bits(&s->gb, 8);
     546           6 :             w = get_bits(&s->gb, 16) + 1;
     547           6 :             h = get_bits(&s->gb, 16) + 1;
     548           6 :             if (get_bits1(&s->gb)) // display size
     549           0 :                 skip_bits(&s->gb, 32);
     550             :         } else {
     551        1800 :             s->s.h.refreshrefmask = get_bits(&s->gb, 8);
     552        1800 :             s->s.h.refidx[0]      = get_bits(&s->gb, 3);
     553        1800 :             s->s.h.signbias[0]    = get_bits1(&s->gb) && !s->s.h.errorres;
     554        1800 :             s->s.h.refidx[1]      = get_bits(&s->gb, 3);
     555        1800 :             s->s.h.signbias[1]    = get_bits1(&s->gb) && !s->s.h.errorres;
     556        1800 :             s->s.h.refidx[2]      = get_bits(&s->gb, 3);
     557        1800 :             s->s.h.signbias[2]    = get_bits1(&s->gb) && !s->s.h.errorres;
     558        3600 :             if (!s->s.refs[s->s.h.refidx[0]].f->buf[0] ||
     559        3600 :                 !s->s.refs[s->s.h.refidx[1]].f->buf[0] ||
     560        1800 :                 !s->s.refs[s->s.h.refidx[2]].f->buf[0]) {
     561           0 :                 av_log(avctx, AV_LOG_ERROR, "Not all references are available\n");
     562           0 :                 return AVERROR_INVALIDDATA;
     563             :             }
     564        1800 :             if (get_bits1(&s->gb)) {
     565        1798 :                 w = s->s.refs[s->s.h.refidx[0]].f->width;
     566        1798 :                 h = s->s.refs[s->s.h.refidx[0]].f->height;
     567           2 :             } else if (get_bits1(&s->gb)) {
     568           1 :                 w = s->s.refs[s->s.h.refidx[1]].f->width;
     569           1 :                 h = s->s.refs[s->s.h.refidx[1]].f->height;
     570           1 :             } else if (get_bits1(&s->gb)) {
     571           0 :                 w = s->s.refs[s->s.h.refidx[2]].f->width;
     572           0 :                 h = s->s.refs[s->s.h.refidx[2]].f->height;
     573             :             } else {
     574           1 :                 w = get_bits(&s->gb, 16) + 1;
     575           1 :                 h = get_bits(&s->gb, 16) + 1;
     576             :             }
     577             :             // Note that in this code, "CUR_FRAME" is actually before we
     578             :             // have formally allocated a frame, and thus actually represents
     579             :             // the _last_ frame
     580        3598 :             s->s.h.use_last_frame_mvs &= s->s.frames[CUR_FRAME].tf.f->width == w &&
     581        1798 :                                        s->s.frames[CUR_FRAME].tf.f->height == h;
     582        1800 :             if (get_bits1(&s->gb)) // display size
     583           3 :                 skip_bits(&s->gb, 32);
     584        1800 :             s->s.h.highprecisionmvs = get_bits1(&s->gb);
     585        2289 :             s->s.h.filtermode = get_bits1(&s->gb) ? FILTER_SWITCHABLE :
     586         489 :                                                   get_bits(&s->gb, 2);
     587        3600 :             s->s.h.allowcompinter = s->s.h.signbias[0] != s->s.h.signbias[1] ||
     588        1800 :                                   s->s.h.signbias[0] != s->s.h.signbias[2];
     589        1800 :             if (s->s.h.allowcompinter) {
     590         446 :                 if (s->s.h.signbias[0] == s->s.h.signbias[1]) {
     591         446 :                     s->s.h.fixcompref    = 2;
     592         446 :                     s->s.h.varcompref[0] = 0;
     593         446 :                     s->s.h.varcompref[1] = 1;
     594           0 :                 } else if (s->s.h.signbias[0] == s->s.h.signbias[2]) {
     595           0 :                     s->s.h.fixcompref    = 1;
     596           0 :                     s->s.h.varcompref[0] = 0;
     597           0 :                     s->s.h.varcompref[1] = 2;
     598             :                 } else {
     599           0 :                     s->s.h.fixcompref    = 0;
     600           0 :                     s->s.h.varcompref[0] = 1;
     601           0 :                     s->s.h.varcompref[1] = 2;
     602             :                 }
     603             :             }
     604             :         }
     605             :     }
     606        2257 :     s->s.h.refreshctx   = s->s.h.errorres ? 0 : get_bits1(&s->gb);
     607        2257 :     s->s.h.parallelmode = s->s.h.errorres ? 1 : get_bits1(&s->gb);
     608        2257 :     s->s.h.framectxid   = c = get_bits(&s->gb, 2);
     609        2257 :     if (s->s.h.keyframe || s->s.h.intraonly)
     610         457 :         s->s.h.framectxid = 0; // BUG: libvpx ignores this field in keyframes
     611             : 
     612             :     /* loopfilter header data */
     613        2257 :     if (s->s.h.keyframe || s->s.h.errorres || s->s.h.intraonly) {
     614             :         // reset loopfilter defaults
     615         466 :         s->s.h.lf_delta.ref[0] = 1;
     616         466 :         s->s.h.lf_delta.ref[1] = 0;
     617         466 :         s->s.h.lf_delta.ref[2] = -1;
     618         466 :         s->s.h.lf_delta.ref[3] = -1;
     619         466 :         s->s.h.lf_delta.mode[0] = 0;
     620         466 :         s->s.h.lf_delta.mode[1] = 0;
     621         466 :         memset(s->s.h.segmentation.feat, 0, sizeof(s->s.h.segmentation.feat));
     622             :     }
     623        2257 :     s->s.h.filter.level = get_bits(&s->gb, 6);
     624        2257 :     sharp = get_bits(&s->gb, 3);
     625             :     // if sharpness changed, reinit lim/mblim LUTs. if it didn't change, keep
     626             :     // the old cache values since they are still valid
     627        2257 :     if (s->s.h.filter.sharpness != sharp) {
     628       29312 :         for (i = 1; i <= 63; i++) {
     629       28854 :             int limit = i;
     630             : 
     631       28854 :             if (sharp > 0) {
     632         441 :                 limit >>= (sharp + 3) >> 2;
     633         441 :                 limit = FFMIN(limit, 9 - sharp);
     634             :             }
     635       28854 :             limit = FFMAX(limit, 1);
     636             : 
     637       28854 :             s->filter_lut.lim_lut[i] = limit;
     638       28854 :             s->filter_lut.mblim_lut[i] = 2 * (i + 2) + limit;
     639             :         }
     640             :     }
     641        2257 :     s->s.h.filter.sharpness = sharp;
     642        2257 :     if ((s->s.h.lf_delta.enabled = get_bits1(&s->gb))) {
     643        2242 :         if ((s->s.h.lf_delta.updated = get_bits1(&s->gb))) {
     644        2400 :             for (i = 0; i < 4; i++)
     645        1920 :                 if (get_bits1(&s->gb))
     646        1401 :                     s->s.h.lf_delta.ref[i] = get_sbits_inv(&s->gb, 6);
     647        1440 :             for (i = 0; i < 2; i++)
     648         960 :                 if (get_bits1(&s->gb))
     649           2 :                     s->s.h.lf_delta.mode[i] = get_sbits_inv(&s->gb, 6);
     650             :         }
     651             :     }
     652             : 
     653             :     /* quantization header data */
     654        2257 :     s->s.h.yac_qi      = get_bits(&s->gb, 8);
     655        2257 :     s->s.h.ydc_qdelta  = get_bits1(&s->gb) ? get_sbits_inv(&s->gb, 4) : 0;
     656        2257 :     s->s.h.uvdc_qdelta = get_bits1(&s->gb) ? get_sbits_inv(&s->gb, 4) : 0;
     657        2257 :     s->s.h.uvac_qdelta = get_bits1(&s->gb) ? get_sbits_inv(&s->gb, 4) : 0;
     658        4548 :     s->s.h.lossless    = s->s.h.yac_qi == 0 && s->s.h.ydc_qdelta == 0 &&
     659        2291 :                        s->s.h.uvdc_qdelta == 0 && s->s.h.uvac_qdelta == 0;
     660        2257 :     if (s->s.h.lossless)
     661          17 :         avctx->properties |= FF_CODEC_PROPERTY_LOSSLESS;
     662             : 
     663             :     /* segmentation header info */
     664        2257 :     if ((s->s.h.segmentation.enabled = get_bits1(&s->gb))) {
     665         229 :         if ((s->s.h.segmentation.update_map = get_bits1(&s->gb))) {
     666         488 :             for (i = 0; i < 7; i++)
     667         732 :                 s->s.h.segmentation.prob[i] = get_bits1(&s->gb) ?
     668         305 :                                  get_bits(&s->gb, 8) : 255;
     669          61 :             if ((s->s.h.segmentation.temporal = get_bits1(&s->gb)))
     670         220 :                 for (i = 0; i < 3; i++)
     671         330 :                     s->s.h.segmentation.pred_prob[i] = get_bits1(&s->gb) ?
     672         165 :                                          get_bits(&s->gb, 8) : 255;
     673             :         }
     674             : 
     675         229 :         if (get_bits1(&s->gb)) {
     676          63 :             s->s.h.segmentation.absolute_vals = get_bits1(&s->gb);
     677         567 :             for (i = 0; i < 8; i++) {
     678         504 :                 if ((s->s.h.segmentation.feat[i].q_enabled = get_bits1(&s->gb)))
     679         207 :                     s->s.h.segmentation.feat[i].q_val = get_sbits_inv(&s->gb, 8);
     680         504 :                 if ((s->s.h.segmentation.feat[i].lf_enabled = get_bits1(&s->gb)))
     681           3 :                     s->s.h.segmentation.feat[i].lf_val = get_sbits_inv(&s->gb, 6);
     682         504 :                 if ((s->s.h.segmentation.feat[i].ref_enabled = get_bits1(&s->gb)))
     683           3 :                     s->s.h.segmentation.feat[i].ref_val = get_bits(&s->gb, 2);
     684         504 :                 s->s.h.segmentation.feat[i].skip_enabled = get_bits1(&s->gb);
     685             :             }
     686             :         }
     687             :     }
     688             : 
     689             :     // set qmul[] based on Y/UV, AC/DC and segmentation Q idx deltas
     690        6117 :     for (i = 0; i < (s->s.h.segmentation.enabled ? 8 : 1); i++) {
     691             :         int qyac, qydc, quvac, quvdc, lflvl, sh;
     692             : 
     693        3860 :         if (s->s.h.segmentation.enabled && s->s.h.segmentation.feat[i].q_enabled) {
     694         764 :             if (s->s.h.segmentation.absolute_vals)
     695           0 :                 qyac = av_clip_uintp2(s->s.h.segmentation.feat[i].q_val, 8);
     696             :             else
     697         382 :                 qyac = av_clip_uintp2(s->s.h.yac_qi + s->s.h.segmentation.feat[i].q_val, 8);
     698             :         } else {
     699        3478 :             qyac  = s->s.h.yac_qi;
     700             :         }
     701        3860 :         qydc  = av_clip_uintp2(qyac + s->s.h.ydc_qdelta, 8);
     702        3860 :         quvdc = av_clip_uintp2(qyac + s->s.h.uvdc_qdelta, 8);
     703        3860 :         quvac = av_clip_uintp2(qyac + s->s.h.uvac_qdelta, 8);
     704        3860 :         qyac  = av_clip_uintp2(qyac, 8);
     705             : 
     706        3860 :         s->s.h.segmentation.feat[i].qmul[0][0] = ff_vp9_dc_qlookup[s->bpp_index][qydc];
     707        3860 :         s->s.h.segmentation.feat[i].qmul[0][1] = ff_vp9_ac_qlookup[s->bpp_index][qyac];
     708        3860 :         s->s.h.segmentation.feat[i].qmul[1][0] = ff_vp9_dc_qlookup[s->bpp_index][quvdc];
     709        3860 :         s->s.h.segmentation.feat[i].qmul[1][1] = ff_vp9_ac_qlookup[s->bpp_index][quvac];
     710             : 
     711        3860 :         sh = s->s.h.filter.level >= 32;
     712        3860 :         if (s->s.h.segmentation.enabled && s->s.h.segmentation.feat[i].lf_enabled) {
     713          34 :             if (s->s.h.segmentation.absolute_vals)
     714           0 :                 lflvl = av_clip_uintp2(s->s.h.segmentation.feat[i].lf_val, 6);
     715             :             else
     716          17 :                 lflvl = av_clip_uintp2(s->s.h.filter.level + s->s.h.segmentation.feat[i].lf_val, 6);
     717             :         } else {
     718        3843 :             lflvl  = s->s.h.filter.level;
     719             :         }
     720        3860 :         if (s->s.h.lf_delta.enabled) {
     721        3845 :             s->s.h.segmentation.feat[i].lflvl[0][0] =
     722        3845 :             s->s.h.segmentation.feat[i].lflvl[0][1] =
     723        3845 :                 av_clip_uintp2(lflvl + (s->s.h.lf_delta.ref[0] * (1 << sh)), 6);
     724       15380 :             for (j = 1; j < 4; j++) {
     725       11535 :                 s->s.h.segmentation.feat[i].lflvl[j][0] =
     726       34605 :                     av_clip_uintp2(lflvl + ((s->s.h.lf_delta.ref[j] +
     727       23070 :                                              s->s.h.lf_delta.mode[0]) * (1 << sh)), 6);
     728       11535 :                 s->s.h.segmentation.feat[i].lflvl[j][1] =
     729       34605 :                     av_clip_uintp2(lflvl + ((s->s.h.lf_delta.ref[j] +
     730       23070 :                                              s->s.h.lf_delta.mode[1]) * (1 << sh)), 6);
     731             :             }
     732             :         } else {
     733          15 :             memset(s->s.h.segmentation.feat[i].lflvl, lflvl,
     734             :                    sizeof(s->s.h.segmentation.feat[i].lflvl));
     735             :         }
     736             :     }
     737             : 
     738             :     /* tiling info */
     739        2257 :     if ((ret = update_size(avctx, w, h)) < 0) {
     740           0 :         av_log(avctx, AV_LOG_ERROR, "Failed to initialize decoder for %dx%d @ %d\n",
     741           0 :                w, h, s->pix_fmt);
     742           0 :         return ret;
     743             :     }
     744        4514 :     for (s->s.h.tiling.log2_tile_cols = 0;
     745        2257 :          s->sb_cols > (64 << s->s.h.tiling.log2_tile_cols);
     746           0 :          s->s.h.tiling.log2_tile_cols++) ;
     747        2257 :     for (max = 0; (s->sb_cols >> max) >= 4; max++) ;
     748        2257 :     max = FFMAX(0, max - 1);
     749        4529 :     while (max > s->s.h.tiling.log2_tile_cols) {
     750         199 :         if (get_bits1(&s->gb))
     751          15 :             s->s.h.tiling.log2_tile_cols++;
     752             :         else
     753         184 :             break;
     754             :     }
     755        2257 :     s->s.h.tiling.log2_tile_rows = decode012(&s->gb);
     756        2257 :     s->s.h.tiling.tile_rows = 1 << s->s.h.tiling.log2_tile_rows;
     757        2257 :     if (s->s.h.tiling.tile_cols != (1 << s->s.h.tiling.log2_tile_cols)) {
     758             :         int n_range_coders;
     759             :         VP56RangeCoder *rc;
     760             : 
     761         451 :         if (s->td) {
     762           0 :             for (i = 0; i < s->active_tile_cols; i++) {
     763           0 :                 av_free(s->td[i].b_base);
     764           0 :                 av_free(s->td[i].block_base);
     765             :             }
     766           0 :             av_free(s->td);
     767             :         }
     768             : 
     769         451 :         s->s.h.tiling.tile_cols = 1 << s->s.h.tiling.log2_tile_cols;
     770         451 :         vp9_free_entries(avctx);
     771         902 :         s->active_tile_cols = avctx->active_thread_type == FF_THREAD_SLICE ?
     772         451 :                               s->s.h.tiling.tile_cols : 1;
     773         451 :         vp9_alloc_entries(avctx, s->sb_rows);
     774         451 :         if (avctx->active_thread_type == FF_THREAD_SLICE) {
     775           0 :             n_range_coders = 4; // max_tile_rows
     776             :         } else {
     777         451 :             n_range_coders = s->s.h.tiling.tile_cols;
     778             :         }
     779         451 :         s->td = av_mallocz_array(s->active_tile_cols, sizeof(VP9TileData) +
     780             :                                  n_range_coders * sizeof(VP56RangeCoder));
     781         451 :         if (!s->td)
     782           0 :             return AVERROR(ENOMEM);
     783         451 :         rc = (VP56RangeCoder *) &s->td[s->active_tile_cols];
     784         902 :         for (i = 0; i < s->active_tile_cols; i++) {
     785         451 :             s->td[i].s = s;
     786         451 :             s->td[i].c_b = rc;
     787         451 :             rc += n_range_coders;
     788             :         }
     789             :     }
     790             : 
     791             :     /* check reference frames */
     792        2257 :     if (!s->s.h.keyframe && !s->s.h.intraonly) {
     793        7200 :         for (i = 0; i < 3; i++) {
     794        5400 :             AVFrame *ref = s->s.refs[s->s.h.refidx[i]].f;
     795        5400 :             int refw = ref->width, refh = ref->height;
     796             : 
     797        5400 :             if (ref->format != avctx->pix_fmt) {
     798           0 :                 av_log(avctx, AV_LOG_ERROR,
     799             :                        "Ref pixfmt (%s) did not match current frame (%s)",
     800           0 :                        av_get_pix_fmt_name(ref->format),
     801             :                        av_get_pix_fmt_name(avctx->pix_fmt));
     802           0 :                 return AVERROR_INVALIDDATA;
     803        5400 :             } else if (refw == w && refh == h) {
     804        5392 :                 s->mvscale[i][0] = s->mvscale[i][1] = 0;
     805             :             } else {
     806           8 :                 if (w * 2 < refw || h * 2 < refh || w > 16 * refw || h > 16 * refh) {
     807           0 :                     av_log(avctx, AV_LOG_ERROR,
     808             :                            "Invalid ref frame dimensions %dx%d for frame size %dx%d\n",
     809             :                            refw, refh, w, h);
     810           0 :                     return AVERROR_INVALIDDATA;
     811             :                 }
     812           8 :                 s->mvscale[i][0] = (refw << 14) / w;
     813           8 :                 s->mvscale[i][1] = (refh << 14) / h;
     814           8 :                 s->mvstep[i][0] = 16 * s->mvscale[i][0] >> 14;
     815           8 :                 s->mvstep[i][1] = 16 * s->mvscale[i][1] >> 14;
     816             :             }
     817             :         }
     818             :     }
     819             : 
     820        2257 :     if (s->s.h.keyframe || s->s.h.errorres || (s->s.h.intraonly && s->s.h.resetctx == 3)) {
     821         460 :         s->prob_ctx[0].p = s->prob_ctx[1].p = s->prob_ctx[2].p =
     822         460 :                            s->prob_ctx[3].p = ff_vp9_default_probs;
     823         460 :         memcpy(s->prob_ctx[0].coef, ff_vp9_default_coef_probs,
     824             :                sizeof(ff_vp9_default_coef_probs));
     825         460 :         memcpy(s->prob_ctx[1].coef, ff_vp9_default_coef_probs,
     826             :                sizeof(ff_vp9_default_coef_probs));
     827         460 :         memcpy(s->prob_ctx[2].coef, ff_vp9_default_coef_probs,
     828             :                sizeof(ff_vp9_default_coef_probs));
     829         460 :         memcpy(s->prob_ctx[3].coef, ff_vp9_default_coef_probs,
     830             :                sizeof(ff_vp9_default_coef_probs));
     831        1797 :     } else if (s->s.h.intraonly && s->s.h.resetctx == 2) {
     832           6 :         s->prob_ctx[c].p = ff_vp9_default_probs;
     833           6 :         memcpy(s->prob_ctx[c].coef, ff_vp9_default_coef_probs,
     834             :                sizeof(ff_vp9_default_coef_probs));
     835             :     }
     836             : 
     837             :     // next 16 bits is size of the rest of the header (arith-coded)
     838        2257 :     s->s.h.compressed_header_size = size2 = get_bits(&s->gb, 16);
     839        2257 :     s->s.h.uncompressed_header_size = (get_bits_count(&s->gb) + 7) / 8;
     840             : 
     841        2257 :     data2 = align_get_bits(&s->gb);
     842        2257 :     if (size2 > size - (data2 - data)) {
     843           0 :         av_log(avctx, AV_LOG_ERROR, "Invalid compressed header size\n");
     844           0 :         return AVERROR_INVALIDDATA;
     845             :     }
     846        2257 :     ret = ff_vp56_init_range_decoder(&s->c, data2, size2);
     847        2257 :     if (ret < 0)
     848           0 :         return ret;
     849             : 
     850        2257 :     if (vp56_rac_get_prob_branchy(&s->c, 128)) { // marker bit
     851           0 :         av_log(avctx, AV_LOG_ERROR, "Marker bit was set\n");
     852           0 :         return AVERROR_INVALIDDATA;
     853             :     }
     854             : 
     855        4514 :     for (i = 0; i < s->active_tile_cols; i++) {
     856        2257 :         if (s->s.h.keyframe || s->s.h.intraonly) {
     857         457 :             memset(s->td[i].counts.coef, 0, sizeof(s->td[0].counts.coef));
     858         457 :             memset(s->td[i].counts.eob,  0, sizeof(s->td[0].counts.eob));
     859             :         } else {
     860        1800 :             memset(&s->td[i].counts, 0, sizeof(s->td[0].counts));
     861             :         }
     862             :     }
     863             : 
     864             :     /* FIXME is it faster to not copy here, but do it down in the fw updates
     865             :      * as explicit copies if the fw update is missing (and skip the copy upon
     866             :      * fw update)? */
     867        2257 :     s->prob.p = s->prob_ctx[c].p;
     868             : 
     869             :     // txfm updates
     870        2257 :     if (s->s.h.lossless) {
     871          17 :         s->s.h.txfmmode = TX_4X4;
     872             :     } else {
     873        2240 :         s->s.h.txfmmode = vp8_rac_get_uint(&s->c, 2);
     874        2240 :         if (s->s.h.txfmmode == 3)
     875        1891 :             s->s.h.txfmmode += vp8_rac_get(&s->c);
     876             : 
     877        2240 :         if (s->s.h.txfmmode == TX_SWITCHABLE) {
     878        5073 :             for (i = 0; i < 2; i++)
     879        3382 :                 if (vp56_rac_get_prob_branchy(&s->c, 252))
     880         204 :                     s->prob.p.tx8p[i] = update_prob(&s->c, s->prob.p.tx8p[i]);
     881        5073 :             for (i = 0; i < 2; i++)
     882       10146 :                 for (j = 0; j < 2; j++)
     883        6764 :                     if (vp56_rac_get_prob_branchy(&s->c, 252))
     884         154 :                         s->prob.p.tx16p[i][j] =
     885         154 :                             update_prob(&s->c, s->prob.p.tx16p[i][j]);
     886        5073 :             for (i = 0; i < 2; i++)
     887       13528 :                 for (j = 0; j < 3; j++)
     888       10146 :                     if (vp56_rac_get_prob_branchy(&s->c, 252))
     889          37 :                         s->prob.p.tx32p[i][j] =
     890          37 :                             update_prob(&s->c, s->prob.p.tx32p[i][j]);
     891             :         }
     892             :     }
     893             : 
     894             :     // coef updates
     895        9853 :     for (i = 0; i < 4; i++) {
     896        8162 :         uint8_t (*ref)[2][6][6][3] = s->prob_ctx[c].coef[i];
     897        8162 :         if (vp8_rac_get(&s->c)) {
     898        4665 :             for (j = 0; j < 2; j++)
     899        9330 :                 for (k = 0; k < 2; k++)
     900       43540 :                     for (l = 0; l < 6; l++)
     901      242580 :                         for (m = 0; m < 6; m++) {
     902      211480 :                             uint8_t *p = s->prob.coef[i][j][k][l][m];
     903      211480 :                             uint8_t *r = ref[j][k][l][m];
     904      211480 :                             if (m >= 3 && l == 0) // dc only has 3 pt
     905        6220 :                                 break;
     906      821040 :                             for (n = 0; n < 3; n++) {
     907      615780 :                                 if (vp56_rac_get_prob_branchy(&s->c, 252))
     908       21976 :                                     p[n] = update_prob(&s->c, r[n]);
     909             :                                 else
     910      593804 :                                     p[n] = r[n];
     911             :                             }
     912      205260 :                             memcpy(&p[3], ff_vp9_model_pareto8[p[2]], 8);
     913             :                         }
     914             :         } else {
     915       19821 :             for (j = 0; j < 2; j++)
     916       39642 :                 for (k = 0; k < 2; k++)
     917      184996 :                     for (l = 0; l < 6; l++)
     918     1057120 :                         for (m = 0; m < 6; m++) {
     919      924980 :                             uint8_t *p = s->prob.coef[i][j][k][l][m];
     920      924980 :                             uint8_t *r = ref[j][k][l][m];
     921      924980 :                             if (m > 3 && l == 0) // dc only has 3 pt
     922       26428 :                                 break;
     923      898552 :                             memcpy(p, r, 3);
     924      898552 :                             memcpy(&p[3], ff_vp9_model_pareto8[p[2]], 8);
     925             :                         }
     926             :         }
     927        8162 :         if (s->s.h.txfmmode == i)
     928         566 :             break;
     929             :     }
     930             : 
     931             :     // mode updates
     932        9028 :     for (i = 0; i < 3; i++)
     933        6771 :         if (vp56_rac_get_prob_branchy(&s->c, 252))
     934         707 :             s->prob.p.skip[i] = update_prob(&s->c, s->prob.p.skip[i]);
     935        2257 :     if (!s->s.h.keyframe && !s->s.h.intraonly) {
     936       14400 :         for (i = 0; i < 7; i++)
     937       50400 :             for (j = 0; j < 3; j++)
     938       37800 :                 if (vp56_rac_get_prob_branchy(&s->c, 252))
     939         310 :                     s->prob.p.mv_mode[i][j] =
     940         310 :                         update_prob(&s->c, s->prob.p.mv_mode[i][j]);
     941             : 
     942        1800 :         if (s->s.h.filtermode == FILTER_SWITCHABLE)
     943        6555 :             for (i = 0; i < 4; i++)
     944       15732 :                 for (j = 0; j < 2; j++)
     945       10488 :                     if (vp56_rac_get_prob_branchy(&s->c, 252))
     946         184 :                         s->prob.p.filter[i][j] =
     947         184 :                             update_prob(&s->c, s->prob.p.filter[i][j]);
     948             : 
     949        9000 :         for (i = 0; i < 4; i++)
     950        7200 :             if (vp56_rac_get_prob_branchy(&s->c, 252))
     951          99 :                 s->prob.p.intra[i] = update_prob(&s->c, s->prob.p.intra[i]);
     952             : 
     953        1800 :         if (s->s.h.allowcompinter) {
     954         446 :             s->s.h.comppredmode = vp8_rac_get(&s->c);
     955         446 :             if (s->s.h.comppredmode)
     956         305 :                 s->s.h.comppredmode += vp8_rac_get(&s->c);
     957         446 :             if (s->s.h.comppredmode == PRED_SWITCHABLE)
     958        1818 :                 for (i = 0; i < 5; i++)
     959        1515 :                     if (vp56_rac_get_prob_branchy(&s->c, 252))
     960          43 :                         s->prob.p.comp[i] =
     961          43 :                             update_prob(&s->c, s->prob.p.comp[i]);
     962             :         } else {
     963        1354 :             s->s.h.comppredmode = PRED_SINGLEREF;
     964             :         }
     965             : 
     966        1800 :         if (s->s.h.comppredmode != PRED_COMPREF) {
     967       10788 :             for (i = 0; i < 5; i++) {
     968        8990 :                 if (vp56_rac_get_prob_branchy(&s->c, 252))
     969         318 :                     s->prob.p.single_ref[i][0] =
     970         318 :                         update_prob(&s->c, s->prob.p.single_ref[i][0]);
     971        8990 :                 if (vp56_rac_get_prob_branchy(&s->c, 252))
     972          53 :                     s->prob.p.single_ref[i][1] =
     973          53 :                         update_prob(&s->c, s->prob.p.single_ref[i][1]);
     974             :             }
     975             :         }
     976             : 
     977        1800 :         if (s->s.h.comppredmode != PRED_SINGLEREF) {
     978        1830 :             for (i = 0; i < 5; i++)
     979        1525 :                 if (vp56_rac_get_prob_branchy(&s->c, 252))
     980          10 :                     s->prob.p.comp_ref[i] =
     981          10 :                         update_prob(&s->c, s->prob.p.comp_ref[i]);
     982             :         }
     983             : 
     984        9000 :         for (i = 0; i < 4; i++)
     985       72000 :             for (j = 0; j < 9; j++)
     986       64800 :                 if (vp56_rac_get_prob_branchy(&s->c, 252))
     987         127 :                     s->prob.p.y_mode[i][j] =
     988         127 :                         update_prob(&s->c, s->prob.p.y_mode[i][j]);
     989             : 
     990        9000 :         for (i = 0; i < 4; i++)
     991       36000 :             for (j = 0; j < 4; j++)
     992      115200 :                 for (k = 0; k < 3; k++)
     993       86400 :                     if (vp56_rac_get_prob_branchy(&s->c, 252))
     994         254 :                         s->prob.p.partition[3 - i][j][k] =
     995         254 :                             update_prob(&s->c,
     996         254 :                                         s->prob.p.partition[3 - i][j][k]);
     997             : 
     998             :         // mv fields don't use the update_prob subexp model for some reason
     999        7200 :         for (i = 0; i < 3; i++)
    1000        5400 :             if (vp56_rac_get_prob_branchy(&s->c, 252))
    1001         257 :                 s->prob.p.mv_joint[i] = (vp8_rac_get_uint(&s->c, 7) << 1) | 1;
    1002             : 
    1003        5400 :         for (i = 0; i < 2; i++) {
    1004        3600 :             if (vp56_rac_get_prob_branchy(&s->c, 252))
    1005         153 :                 s->prob.p.mv_comp[i].sign =
    1006         153 :                     (vp8_rac_get_uint(&s->c, 7) << 1) | 1;
    1007             : 
    1008       39600 :             for (j = 0; j < 10; j++)
    1009       36000 :                 if (vp56_rac_get_prob_branchy(&s->c, 252))
    1010          85 :                     s->prob.p.mv_comp[i].classes[j] =
    1011          85 :                         (vp8_rac_get_uint(&s->c, 7) << 1) | 1;
    1012             : 
    1013        3600 :             if (vp56_rac_get_prob_branchy(&s->c, 252))
    1014          86 :                 s->prob.p.mv_comp[i].class0 =
    1015          86 :                     (vp8_rac_get_uint(&s->c, 7) << 1) | 1;
    1016             : 
    1017       39600 :             for (j = 0; j < 10; j++)
    1018       36000 :                 if (vp56_rac_get_prob_branchy(&s->c, 252))
    1019          15 :                     s->prob.p.mv_comp[i].bits[j] =
    1020          15 :                         (vp8_rac_get_uint(&s->c, 7) << 1) | 1;
    1021             :         }
    1022             : 
    1023        5400 :         for (i = 0; i < 2; i++) {
    1024       10800 :             for (j = 0; j < 2; j++)
    1025       28800 :                 for (k = 0; k < 3; k++)
    1026       21600 :                     if (vp56_rac_get_prob_branchy(&s->c, 252))
    1027         209 :                         s->prob.p.mv_comp[i].class0_fp[j][k] =
    1028         209 :                             (vp8_rac_get_uint(&s->c, 7) << 1) | 1;
    1029             : 
    1030       14400 :             for (j = 0; j < 3; j++)
    1031       10800 :                 if (vp56_rac_get_prob_branchy(&s->c, 252))
    1032           4 :                     s->prob.p.mv_comp[i].fp[j] =
    1033           4 :                         (vp8_rac_get_uint(&s->c, 7) << 1) | 1;
    1034             :         }
    1035             : 
    1036        1800 :         if (s->s.h.highprecisionmvs) {
    1037        4206 :             for (i = 0; i < 2; i++) {
    1038        2804 :                 if (vp56_rac_get_prob_branchy(&s->c, 252))
    1039          86 :                     s->prob.p.mv_comp[i].class0_hp =
    1040          86 :                         (vp8_rac_get_uint(&s->c, 7) << 1) | 1;
    1041             : 
    1042        2804 :                 if (vp56_rac_get_prob_branchy(&s->c, 252))
    1043          30 :                     s->prob.p.mv_comp[i].hp =
    1044          30 :                         (vp8_rac_get_uint(&s->c, 7) << 1) | 1;
    1045             :             }
    1046             :         }
    1047             :     }
    1048             : 
    1049        2257 :     return (data2 - data) + size2;
    1050             : }
    1051             : 
    1052      975128 : static void decode_sb(VP9TileData *td, int row, int col, VP9Filter *lflvl,
    1053             :                       ptrdiff_t yoff, ptrdiff_t uvoff, enum BlockLevel bl)
    1054             : {
    1055      975128 :     const VP9Context *s = td->s;
    1056     1950256 :     int c = ((s->above_partition_ctx[col] >> (3 - bl)) & 1) |
    1057      975128 :             (((td->left_partition_ctx[row & 0x7] >> (3 - bl)) & 1) << 1);
    1058      975128 :     const uint8_t *p = s->s.h.keyframe || s->s.h.intraonly ? ff_vp9_default_kf_partition_probs[bl][c] :
    1059             :                                                      s->prob.p.partition[bl][c];
    1060             :     enum BlockPartition bp;
    1061      975128 :     ptrdiff_t hbs = 4 >> bl;
    1062      975128 :     AVFrame *f = s->s.frames[CUR_FRAME].tf.f;
    1063      975128 :     ptrdiff_t y_stride = f->linesize[0], uv_stride = f->linesize[1];
    1064      975128 :     int bytesperpixel = s->bytesperpixel;
    1065             : 
    1066      975128 :     if (bl == BL_8X8) {
    1067      484346 :         bp = vp8_rac_get_tree(td->c, ff_vp9_partition_tree, p);
    1068      484346 :         ff_vp9_decode_block(td, row, col, lflvl, yoff, uvoff, bl, bp);
    1069      490782 :     } else if (col + hbs < s->cols) { // FIXME why not <=?
    1070      468614 :         if (row + hbs < s->rows) { // FIXME why not <=?
    1071      454939 :             bp = vp8_rac_get_tree(td->c, ff_vp9_partition_tree, p);
    1072      454939 :             switch (bp) {
    1073      187762 :             case PARTITION_NONE:
    1074      187762 :                 ff_vp9_decode_block(td, row, col, lflvl, yoff, uvoff, bl, bp);
    1075      187762 :                 break;
    1076       27731 :             case PARTITION_H:
    1077       27731 :                 ff_vp9_decode_block(td, row, col, lflvl, yoff, uvoff, bl, bp);
    1078       27731 :                 yoff  += hbs * 8 * y_stride;
    1079       27731 :                 uvoff += hbs * 8 * uv_stride >> s->ss_v;
    1080       27731 :                 ff_vp9_decode_block(td, row + hbs, col, lflvl, yoff, uvoff, bl, bp);
    1081       27731 :                 break;
    1082       22033 :             case PARTITION_V:
    1083       22033 :                 ff_vp9_decode_block(td, row, col, lflvl, yoff, uvoff, bl, bp);
    1084       22033 :                 yoff  += hbs * 8 * bytesperpixel;
    1085       22033 :                 uvoff += hbs * 8 * bytesperpixel >> s->ss_h;
    1086       22033 :                 ff_vp9_decode_block(td, row, col + hbs, lflvl, yoff, uvoff, bl, bp);
    1087       22033 :                 break;
    1088      217413 :             case PARTITION_SPLIT:
    1089      217413 :                 decode_sb(td, row, col, lflvl, yoff, uvoff, bl + 1);
    1090      434826 :                 decode_sb(td, row, col + hbs, lflvl,
    1091      217413 :                           yoff + 8 * hbs * bytesperpixel,
    1092      217413 :                           uvoff + (8 * hbs * bytesperpixel >> s->ss_h), bl + 1);
    1093      217413 :                 yoff  += hbs * 8 * y_stride;
    1094      217413 :                 uvoff += hbs * 8 * uv_stride >> s->ss_v;
    1095      217413 :                 decode_sb(td, row + hbs, col, lflvl, yoff, uvoff, bl + 1);
    1096      434826 :                 decode_sb(td, row + hbs, col + hbs, lflvl,
    1097      217413 :                           yoff + 8 * hbs * bytesperpixel,
    1098      217413 :                           uvoff + (8 * hbs * bytesperpixel >> s->ss_h), bl + 1);
    1099      217413 :                 break;
    1100           0 :             default:
    1101           0 :                 av_assert0(0);
    1102             :             }
    1103       13675 :         } else if (vp56_rac_get_prob_branchy(td->c, p[1])) {
    1104        9837 :             bp = PARTITION_SPLIT;
    1105        9837 :             decode_sb(td, row, col, lflvl, yoff, uvoff, bl + 1);
    1106       19674 :             decode_sb(td, row, col + hbs, lflvl,
    1107        9837 :                       yoff + 8 * hbs * bytesperpixel,
    1108        9837 :                       uvoff + (8 * hbs * bytesperpixel >> s->ss_h), bl + 1);
    1109             :         } else {
    1110        3838 :             bp = PARTITION_H;
    1111        3838 :             ff_vp9_decode_block(td, row, col, lflvl, yoff, uvoff, bl, bp);
    1112             :         }
    1113       22168 :     } else if (row + hbs < s->rows) { // FIXME why not <=?
    1114       19332 :         if (vp56_rac_get_prob_branchy(td->c, p[2])) {
    1115       16164 :             bp = PARTITION_SPLIT;
    1116       16164 :             decode_sb(td, row, col, lflvl, yoff, uvoff, bl + 1);
    1117       16164 :             yoff  += hbs * 8 * y_stride;
    1118       16164 :             uvoff += hbs * 8 * uv_stride >> s->ss_v;
    1119       16164 :             decode_sb(td, row + hbs, col, lflvl, yoff, uvoff, bl + 1);
    1120             :         } else {
    1121        3168 :             bp = PARTITION_V;
    1122        3168 :             ff_vp9_decode_block(td, row, col, lflvl, yoff, uvoff, bl, bp);
    1123             :         }
    1124             :     } else {
    1125        2836 :         bp = PARTITION_SPLIT;
    1126        2836 :         decode_sb(td, row, col, lflvl, yoff, uvoff, bl + 1);
    1127             :     }
    1128      975128 :     td->counts.partition[bl][c][bp]++;
    1129      975128 : }
    1130             : 
    1131           0 : static void decode_sb_mem(VP9TileData *td, int row, int col, VP9Filter *lflvl,
    1132             :                           ptrdiff_t yoff, ptrdiff_t uvoff, enum BlockLevel bl)
    1133             : {
    1134           0 :     const VP9Context *s = td->s;
    1135           0 :     VP9Block *b = td->b;
    1136           0 :     ptrdiff_t hbs = 4 >> bl;
    1137           0 :     AVFrame *f = s->s.frames[CUR_FRAME].tf.f;
    1138           0 :     ptrdiff_t y_stride = f->linesize[0], uv_stride = f->linesize[1];
    1139           0 :     int bytesperpixel = s->bytesperpixel;
    1140             : 
    1141           0 :     if (bl == BL_8X8) {
    1142             :         av_assert2(b->bl == BL_8X8);
    1143           0 :         ff_vp9_decode_block(td, row, col, lflvl, yoff, uvoff, b->bl, b->bp);
    1144           0 :     } else if (td->b->bl == bl) {
    1145           0 :         ff_vp9_decode_block(td, row, col, lflvl, yoff, uvoff, b->bl, b->bp);
    1146           0 :         if (b->bp == PARTITION_H && row + hbs < s->rows) {
    1147           0 :             yoff  += hbs * 8 * y_stride;
    1148           0 :             uvoff += hbs * 8 * uv_stride >> s->ss_v;
    1149           0 :             ff_vp9_decode_block(td, row + hbs, col, lflvl, yoff, uvoff, b->bl, b->bp);
    1150           0 :         } else if (b->bp == PARTITION_V && col + hbs < s->cols) {
    1151           0 :             yoff  += hbs * 8 * bytesperpixel;
    1152           0 :             uvoff += hbs * 8 * bytesperpixel >> s->ss_h;
    1153           0 :             ff_vp9_decode_block(td, row, col + hbs, lflvl, yoff, uvoff, b->bl, b->bp);
    1154             :         }
    1155             :     } else {
    1156           0 :         decode_sb_mem(td, row, col, lflvl, yoff, uvoff, bl + 1);
    1157           0 :         if (col + hbs < s->cols) { // FIXME why not <=?
    1158           0 :             if (row + hbs < s->rows) {
    1159           0 :                 decode_sb_mem(td, row, col + hbs, lflvl, yoff + 8 * hbs * bytesperpixel,
    1160           0 :                               uvoff + (8 * hbs * bytesperpixel >> s->ss_h), bl + 1);
    1161           0 :                 yoff  += hbs * 8 * y_stride;
    1162           0 :                 uvoff += hbs * 8 * uv_stride >> s->ss_v;
    1163           0 :                 decode_sb_mem(td, row + hbs, col, lflvl, yoff, uvoff, bl + 1);
    1164           0 :                 decode_sb_mem(td, row + hbs, col + hbs, lflvl,
    1165           0 :                               yoff + 8 * hbs * bytesperpixel,
    1166           0 :                               uvoff + (8 * hbs * bytesperpixel >> s->ss_h), bl + 1);
    1167             :             } else {
    1168           0 :                 yoff  += hbs * 8 * bytesperpixel;
    1169           0 :                 uvoff += hbs * 8 * bytesperpixel >> s->ss_h;
    1170           0 :                 decode_sb_mem(td, row, col + hbs, lflvl, yoff, uvoff, bl + 1);
    1171             :             }
    1172           0 :         } else if (row + hbs < s->rows) {
    1173           0 :             yoff  += hbs * 8 * y_stride;
    1174           0 :             uvoff += hbs * 8 * uv_stride >> s->ss_v;
    1175           0 :             decode_sb_mem(td, row + hbs, col, lflvl, yoff, uvoff, bl + 1);
    1176             :         }
    1177             :     }
    1178           0 : }
    1179             : 
    1180       10473 : static void set_tile_offset(int *start, int *end, int idx, int log2_n, int n)
    1181             : {
    1182       10473 :     int sb_start = ( idx      * n) >> log2_n;
    1183       10473 :     int sb_end   = ((idx + 1) * n) >> log2_n;
    1184       10473 :     *start = FFMIN(sb_start, n) << 3;
    1185       10473 :     *end   = FFMIN(sb_end,   n) << 3;
    1186       10473 : }
    1187             : 
    1188         453 : static void free_buffers(VP9Context *s)
    1189             : {
    1190             :     int i;
    1191             : 
    1192         453 :     av_freep(&s->intra_pred_data[0]);
    1193         904 :     for (i = 0; i < s->active_tile_cols; i++) {
    1194         451 :         av_freep(&s->td[i].b_base);
    1195         451 :         av_freep(&s->td[i].block_base);
    1196             :     }
    1197         453 : }
    1198             : 
    1199         453 : static av_cold int vp9_decode_free(AVCodecContext *avctx)
    1200             : {
    1201         453 :     VP9Context *s = avctx->priv_data;
    1202             :     int i;
    1203             : 
    1204        1812 :     for (i = 0; i < 3; i++) {
    1205        1359 :         if (s->s.frames[i].tf.f->buf[0])
    1206         901 :             vp9_frame_unref(avctx, &s->s.frames[i]);
    1207        1359 :         av_frame_free(&s->s.frames[i].tf.f);
    1208             :     }
    1209        4077 :     for (i = 0; i < 8; i++) {
    1210        3624 :         if (s->s.refs[i].f->buf[0])
    1211        3601 :             ff_thread_release_buffer(avctx, &s->s.refs[i]);
    1212        3624 :         av_frame_free(&s->s.refs[i].f);
    1213        3624 :         if (s->next_refs[i].f->buf[0])
    1214        3601 :             ff_thread_release_buffer(avctx, &s->next_refs[i]);
    1215        3624 :         av_frame_free(&s->next_refs[i].f);
    1216             :     }
    1217             : 
    1218         453 :     free_buffers(s);
    1219         453 :     vp9_free_entries(avctx);
    1220         453 :     av_freep(&s->td);
    1221         453 :     return 0;
    1222             : }
    1223             : 
    1224        2257 : static int decode_tiles(AVCodecContext *avctx,
    1225             :                         const uint8_t *data, int size)
    1226             : {
    1227        2257 :     VP9Context *s = avctx->priv_data;
    1228        2257 :     VP9TileData *td = &s->td[0];
    1229             :     int row, col, tile_row, tile_col, ret;
    1230             :     int bytesperpixel;
    1231             :     int tile_row_start, tile_row_end, tile_col_start, tile_col_end;
    1232             :     AVFrame *f;
    1233             :     ptrdiff_t yoff, uvoff, ls_y, ls_uv;
    1234             : 
    1235        2257 :     f = s->s.frames[CUR_FRAME].tf.f;
    1236        2257 :     ls_y = f->linesize[0];
    1237        2257 :     ls_uv =f->linesize[1];
    1238        2257 :     bytesperpixel = s->bytesperpixel;
    1239             : 
    1240        2257 :     yoff = uvoff = 0;
    1241        4517 :     for (tile_row = 0; tile_row < s->s.h.tiling.tile_rows; tile_row++) {
    1242        4520 :         set_tile_offset(&tile_row_start, &tile_row_end,
    1243        4520 :                         tile_row, s->s.h.tiling.log2_tile_rows, s->sb_rows);
    1244             : 
    1245        4544 :         for (tile_col = 0; tile_col < s->s.h.tiling.tile_cols; tile_col++) {
    1246             :             int64_t tile_size;
    1247             : 
    1248        4544 :             if (tile_col == s->s.h.tiling.tile_cols - 1 &&
    1249        2260 :                 tile_row == s->s.h.tiling.tile_rows - 1) {
    1250        2257 :                 tile_size = size;
    1251             :             } else {
    1252          27 :                 tile_size = AV_RB32(data);
    1253          27 :                 data += 4;
    1254          27 :                 size -= 4;
    1255             :             }
    1256        2284 :             if (tile_size > size) {
    1257           0 :                 ff_thread_report_progress(&s->s.frames[CUR_FRAME].tf, INT_MAX, 0);
    1258           0 :                 return AVERROR_INVALIDDATA;
    1259             :             }
    1260        2284 :             ret = ff_vp56_init_range_decoder(&td->c_b[tile_col], data, tile_size);
    1261        2284 :             if (ret < 0)
    1262           0 :                 return ret;
    1263        2284 :             if (vp56_rac_get_prob_branchy(&td->c_b[tile_col], 128)) { // marker bit
    1264           0 :                 ff_thread_report_progress(&s->s.frames[CUR_FRAME].tf, INT_MAX, 0);
    1265           0 :                 return AVERROR_INVALIDDATA;
    1266             :             }
    1267        2284 :             data += tile_size;
    1268        2284 :             size -= tile_size;
    1269             :         }
    1270             : 
    1271       12466 :         for (row = tile_row_start; row < tile_row_end;
    1272        7946 :              row += 8, yoff += ls_y * 64, uvoff += ls_uv * 64 >> s->ss_v) {
    1273        7946 :             VP9Filter *lflvl_ptr = s->lflvl;
    1274        7946 :             ptrdiff_t yoff2 = yoff, uvoff2 = uvoff;
    1275             : 
    1276       16159 :             for (tile_col = 0; tile_col < s->s.h.tiling.tile_cols; tile_col++) {
    1277       16426 :                 set_tile_offset(&tile_col_start, &tile_col_end,
    1278       16426 :                                 tile_col, s->s.h.tiling.log2_tile_cols, s->sb_cols);
    1279        8213 :                 td->tile_col_start = tile_col_start;
    1280        8213 :                 if (s->pass != 2) {
    1281        8213 :                     memset(td->left_partition_ctx, 0, 8);
    1282        8213 :                     memset(td->left_skip_ctx, 0, 8);
    1283        8213 :                     if (s->s.h.keyframe || s->s.h.intraonly) {
    1284        1742 :                         memset(td->left_mode_ctx, DC_PRED, 16);
    1285             :                     } else {
    1286        6471 :                         memset(td->left_mode_ctx, NEARESTMV, 8);
    1287             :                     }
    1288        8213 :                     memset(td->left_y_nnz_ctx, 0, 16);
    1289        8213 :                     memset(td->left_uv_nnz_ctx, 0, 32);
    1290        8213 :                     memset(td->left_segpred_ctx, 0, 8);
    1291             : 
    1292        8213 :                     td->c = &td->c_b[tile_col];
    1293             :                 }
    1294             : 
    1295       67064 :                 for (col = tile_col_start;
    1296       58851 :                      col < tile_col_end;
    1297       50638 :                      col += 8, yoff2 += 64 * bytesperpixel,
    1298       50638 :                      uvoff2 += 64 * bytesperpixel >> s->ss_h, lflvl_ptr++) {
    1299             :                     // FIXME integrate with lf code (i.e. zero after each
    1300             :                     // use, similar to invtxfm coefficients, or similar)
    1301       50638 :                     if (s->pass != 1) {
    1302       50638 :                         memset(lflvl_ptr->mask, 0, sizeof(lflvl_ptr->mask));
    1303             :                     }
    1304             : 
    1305       50638 :                     if (s->pass == 2) {
    1306           0 :                         decode_sb_mem(td, row, col, lflvl_ptr,
    1307             :                                       yoff2, uvoff2, BL_64X64);
    1308             :                     } else {
    1309       50638 :                         decode_sb(td, row, col, lflvl_ptr,
    1310             :                                   yoff2, uvoff2, BL_64X64);
    1311             :                     }
    1312             :                 }
    1313             :             }
    1314             : 
    1315        7946 :             if (s->pass == 1)
    1316           0 :                 continue;
    1317             : 
    1318             :             // backup pre-loopfilter reconstruction data for intra
    1319             :             // prediction of next row of sb64s
    1320        7946 :             if (row + 8 < s->rows) {
    1321       11378 :                 memcpy(s->intra_pred_data[0],
    1322        5689 :                        f->data[0] + yoff + 63 * ls_y,
    1323        5689 :                        8 * s->cols * bytesperpixel);
    1324       11378 :                 memcpy(s->intra_pred_data[1],
    1325        5689 :                        f->data[1] + uvoff + ((64 >> s->ss_v) - 1) * ls_uv,
    1326        5689 :                        8 * s->cols * bytesperpixel >> s->ss_h);
    1327       11378 :                 memcpy(s->intra_pred_data[2],
    1328        5689 :                        f->data[2] + uvoff + ((64 >> s->ss_v) - 1) * ls_uv,
    1329        5689 :                        8 * s->cols * bytesperpixel >> s->ss_h);
    1330             :             }
    1331             : 
    1332             :             // loopfilter one row
    1333        7946 :             if (s->s.h.filter.level) {
    1334        7420 :                 yoff2 = yoff;
    1335        7420 :                 uvoff2 = uvoff;
    1336        7420 :                 lflvl_ptr = s->lflvl;
    1337       62158 :                 for (col = 0; col < s->cols;
    1338       47318 :                      col += 8, yoff2 += 64 * bytesperpixel,
    1339       47318 :                      uvoff2 += 64 * bytesperpixel >> s->ss_h, lflvl_ptr++) {
    1340       47318 :                     ff_vp9_loopfilter_sb(avctx, lflvl_ptr, row, col,
    1341             :                                          yoff2, uvoff2);
    1342             :                 }
    1343             :             }
    1344             : 
    1345             :             // FIXME maybe we can make this more finegrained by running the
    1346             :             // loopfilter per-block instead of after each sbrow
    1347             :             // In fact that would also make intra pred left preparation easier?
    1348        7946 :             ff_thread_report_progress(&s->s.frames[CUR_FRAME].tf, row >> 3, 0);
    1349             :         }
    1350             :     }
    1351        2257 :     return 0;
    1352             : }
    1353             : 
    1354             : #if HAVE_THREADS
    1355             : static av_always_inline
    1356           0 : int decode_tiles_mt(AVCodecContext *avctx, void *tdata, int jobnr,
    1357             :                               int threadnr)
    1358             : {
    1359           0 :     VP9Context *s = avctx->priv_data;
    1360           0 :     VP9TileData *td = &s->td[jobnr];
    1361             :     ptrdiff_t uvoff, yoff, ls_y, ls_uv;
    1362           0 :     int bytesperpixel = s->bytesperpixel, row, col, tile_row;
    1363             :     unsigned tile_cols_len;
    1364             :     int tile_row_start, tile_row_end, tile_col_start, tile_col_end;
    1365             :     VP9Filter *lflvl_ptr_base;
    1366             :     AVFrame *f;
    1367             : 
    1368           0 :     f = s->s.frames[CUR_FRAME].tf.f;
    1369           0 :     ls_y = f->linesize[0];
    1370           0 :     ls_uv =f->linesize[1];
    1371             : 
    1372           0 :     set_tile_offset(&tile_col_start, &tile_col_end,
    1373           0 :                     jobnr, s->s.h.tiling.log2_tile_cols, s->sb_cols);
    1374           0 :     td->tile_col_start  = tile_col_start;
    1375           0 :     uvoff = (64 * bytesperpixel >> s->ss_h)*(tile_col_start >> 3);
    1376           0 :     yoff = (64 * bytesperpixel)*(tile_col_start >> 3);
    1377           0 :     lflvl_ptr_base = s->lflvl+(tile_col_start >> 3);
    1378             : 
    1379           0 :     for (tile_row = 0; tile_row < s->s.h.tiling.tile_rows; tile_row++) {
    1380           0 :         set_tile_offset(&tile_row_start, &tile_row_end,
    1381           0 :                         tile_row, s->s.h.tiling.log2_tile_rows, s->sb_rows);
    1382             : 
    1383           0 :         td->c = &td->c_b[tile_row];
    1384           0 :         for (row = tile_row_start; row < tile_row_end;
    1385           0 :              row += 8, yoff += ls_y * 64, uvoff += ls_uv * 64 >> s->ss_v) {
    1386           0 :             ptrdiff_t yoff2 = yoff, uvoff2 = uvoff;
    1387           0 :             VP9Filter *lflvl_ptr = lflvl_ptr_base+s->sb_cols*(row >> 3);
    1388             : 
    1389           0 :             memset(td->left_partition_ctx, 0, 8);
    1390           0 :             memset(td->left_skip_ctx, 0, 8);
    1391           0 :             if (s->s.h.keyframe || s->s.h.intraonly) {
    1392           0 :                 memset(td->left_mode_ctx, DC_PRED, 16);
    1393             :             } else {
    1394           0 :                 memset(td->left_mode_ctx, NEARESTMV, 8);
    1395             :             }
    1396           0 :             memset(td->left_y_nnz_ctx, 0, 16);
    1397           0 :             memset(td->left_uv_nnz_ctx, 0, 32);
    1398           0 :             memset(td->left_segpred_ctx, 0, 8);
    1399             : 
    1400           0 :             for (col = tile_col_start;
    1401           0 :                  col < tile_col_end;
    1402           0 :                  col += 8, yoff2 += 64 * bytesperpixel,
    1403           0 :                  uvoff2 += 64 * bytesperpixel >> s->ss_h, lflvl_ptr++) {
    1404             :                 // FIXME integrate with lf code (i.e. zero after each
    1405             :                 // use, similar to invtxfm coefficients, or similar)
    1406           0 :                 memset(lflvl_ptr->mask, 0, sizeof(lflvl_ptr->mask));
    1407           0 :                 decode_sb(td, row, col, lflvl_ptr,
    1408             :                             yoff2, uvoff2, BL_64X64);
    1409             :             }
    1410             : 
    1411             :             // backup pre-loopfilter reconstruction data for intra
    1412             :             // prediction of next row of sb64s
    1413           0 :             tile_cols_len = tile_col_end - tile_col_start;
    1414           0 :             if (row + 8 < s->rows) {
    1415           0 :                 memcpy(s->intra_pred_data[0] + (tile_col_start * 8 * bytesperpixel),
    1416           0 :                        f->data[0] + yoff + 63 * ls_y,
    1417           0 :                        8 * tile_cols_len * bytesperpixel);
    1418           0 :                 memcpy(s->intra_pred_data[1] + (tile_col_start * 8 * bytesperpixel >> s->ss_h),
    1419           0 :                        f->data[1] + uvoff + ((64 >> s->ss_v) - 1) * ls_uv,
    1420           0 :                        8 * tile_cols_len * bytesperpixel >> s->ss_h);
    1421           0 :                 memcpy(s->intra_pred_data[2] + (tile_col_start * 8 * bytesperpixel >> s->ss_h),
    1422           0 :                        f->data[2] + uvoff + ((64 >> s->ss_v) - 1) * ls_uv,
    1423           0 :                        8 * tile_cols_len * bytesperpixel >> s->ss_h);
    1424             :             }
    1425             : 
    1426           0 :             vp9_report_tile_progress(s, row >> 3, 1);
    1427             :         }
    1428             :     }
    1429           0 :     return 0;
    1430             : }
    1431             : 
    1432             : static av_always_inline
    1433           0 : int loopfilter_proc(AVCodecContext *avctx)
    1434             : {
    1435           0 :     VP9Context *s = avctx->priv_data;
    1436             :     ptrdiff_t uvoff, yoff, ls_y, ls_uv;
    1437             :     VP9Filter *lflvl_ptr;
    1438           0 :     int bytesperpixel = s->bytesperpixel, col, i;
    1439             :     AVFrame *f;
    1440             : 
    1441           0 :     f = s->s.frames[CUR_FRAME].tf.f;
    1442           0 :     ls_y = f->linesize[0];
    1443           0 :     ls_uv =f->linesize[1];
    1444             : 
    1445           0 :     for (i = 0; i < s->sb_rows; i++) {
    1446           0 :         vp9_await_tile_progress(s, i, s->s.h.tiling.tile_cols);
    1447             : 
    1448           0 :         if (s->s.h.filter.level) {
    1449           0 :             yoff = (ls_y * 64)*i;
    1450           0 :             uvoff =  (ls_uv * 64 >> s->ss_v)*i;
    1451           0 :             lflvl_ptr = s->lflvl+s->sb_cols*i;
    1452           0 :             for (col = 0; col < s->cols;
    1453           0 :                  col += 8, yoff += 64 * bytesperpixel,
    1454           0 :                  uvoff += 64 * bytesperpixel >> s->ss_h, lflvl_ptr++) {
    1455           0 :                 ff_vp9_loopfilter_sb(avctx, lflvl_ptr, i << 3, col,
    1456             :                                      yoff, uvoff);
    1457             :             }
    1458             :         }
    1459             :     }
    1460           0 :     return 0;
    1461             : }
    1462             : #endif
    1463             : 
    1464        2276 : static int vp9_decode_frame(AVCodecContext *avctx, void *frame,
    1465             :                             int *got_frame, AVPacket *pkt)
    1466             : {
    1467        2276 :     const uint8_t *data = pkt->data;
    1468        2276 :     int size = pkt->size;
    1469        2276 :     VP9Context *s = avctx->priv_data;
    1470             :     int ret, i, j, ref;
    1471        5397 :     int retain_segmap_ref = s->s.frames[REF_FRAME_SEGMAP].segmentation_map &&
    1472        1794 :                             (!s->s.h.segmentation.enabled || !s->s.h.segmentation.update_map);
    1473             :     AVFrame *f;
    1474             : 
    1475        2276 :     if ((ret = decode_frame_header(avctx, data, size, &ref)) < 0) {
    1476           0 :         return ret;
    1477        2276 :     } else if (ret == 0) {
    1478          19 :         if (!s->s.refs[ref].f->buf[0]) {
    1479           0 :             av_log(avctx, AV_LOG_ERROR, "Requested reference %d not available\n", ref);
    1480           0 :             return AVERROR_INVALIDDATA;
    1481             :         }
    1482          19 :         if ((ret = av_frame_ref(frame, s->s.refs[ref].f)) < 0)
    1483           0 :             return ret;
    1484          19 :         ((AVFrame *)frame)->pts = pkt->pts;
    1485             : #if FF_API_PKT_PTS
    1486             : FF_DISABLE_DEPRECATION_WARNINGS
    1487          19 :         ((AVFrame *)frame)->pkt_pts = pkt->pts;
    1488             : FF_ENABLE_DEPRECATION_WARNINGS
    1489             : #endif
    1490          19 :         ((AVFrame *)frame)->pkt_dts = pkt->dts;
    1491         171 :         for (i = 0; i < 8; i++) {
    1492         152 :             if (s->next_refs[i].f->buf[0])
    1493         143 :                 ff_thread_release_buffer(avctx, &s->next_refs[i]);
    1494         295 :             if (s->s.refs[i].f->buf[0] &&
    1495         143 :                 (ret = ff_thread_ref_frame(&s->next_refs[i], &s->s.refs[i])) < 0)
    1496           0 :                 return ret;
    1497             :         }
    1498          19 :         *got_frame = 1;
    1499          19 :         return pkt->size;
    1500             :     }
    1501        2257 :     data += ret;
    1502        2257 :     size -= ret;
    1503             : 
    1504        2257 :     if (!retain_segmap_ref || s->s.h.keyframe || s->s.h.intraonly) {
    1505         738 :         if (s->s.frames[REF_FRAME_SEGMAP].tf.f->buf[0])
    1506          45 :             vp9_frame_unref(avctx, &s->s.frames[REF_FRAME_SEGMAP]);
    1507        1010 :         if (!s->s.h.keyframe && !s->s.h.intraonly && !s->s.h.errorres && s->s.frames[CUR_FRAME].tf.f->buf[0] &&
    1508         272 :             (ret = vp9_frame_ref(avctx, &s->s.frames[REF_FRAME_SEGMAP], &s->s.frames[CUR_FRAME])) < 0)
    1509           0 :             return ret;
    1510             :     }
    1511        2257 :     if (s->s.frames[REF_FRAME_MVPAIR].tf.f->buf[0])
    1512        1566 :         vp9_frame_unref(avctx, &s->s.frames[REF_FRAME_MVPAIR]);
    1513        4048 :     if (!s->s.h.intraonly && !s->s.h.keyframe && !s->s.h.errorres && s->s.frames[CUR_FRAME].tf.f->buf[0] &&
    1514        1791 :         (ret = vp9_frame_ref(avctx, &s->s.frames[REF_FRAME_MVPAIR], &s->s.frames[CUR_FRAME])) < 0)
    1515           0 :         return ret;
    1516        2257 :     if (s->s.frames[CUR_FRAME].tf.f->buf[0])
    1517        1806 :         vp9_frame_unref(avctx, &s->s.frames[CUR_FRAME]);
    1518        2257 :     if ((ret = vp9_frame_alloc(avctx, &s->s.frames[CUR_FRAME])) < 0)
    1519           0 :         return ret;
    1520        2257 :     f = s->s.frames[CUR_FRAME].tf.f;
    1521        2257 :     f->key_frame = s->s.h.keyframe;
    1522        2257 :     f->pict_type = (s->s.h.keyframe || s->s.h.intraonly) ? AV_PICTURE_TYPE_I : AV_PICTURE_TYPE_P;
    1523             : 
    1524        4048 :     if (s->s.frames[REF_FRAME_SEGMAP].tf.f->buf[0] &&
    1525        3580 :         (s->s.frames[REF_FRAME_MVPAIR].tf.f->width  != s->s.frames[CUR_FRAME].tf.f->width ||
    1526        1789 :          s->s.frames[REF_FRAME_MVPAIR].tf.f->height != s->s.frames[CUR_FRAME].tf.f->height)) {
    1527           2 :         vp9_frame_unref(avctx, &s->s.frames[REF_FRAME_SEGMAP]);
    1528             :     }
    1529             : 
    1530             :     // ref frame setup
    1531       20313 :     for (i = 0; i < 8; i++) {
    1532       18056 :         if (s->next_refs[i].f->buf[0])
    1533       14402 :             ff_thread_release_buffer(avctx, &s->next_refs[i]);
    1534       18056 :         if (s->s.h.refreshrefmask & (1 << i)) {
    1535        5441 :             ret = ff_thread_ref_frame(&s->next_refs[i], &s->s.frames[CUR_FRAME].tf);
    1536       12615 :         } else if (s->s.refs[i].f->buf[0]) {
    1537       12562 :             ret = ff_thread_ref_frame(&s->next_refs[i], &s->s.refs[i]);
    1538             :         }
    1539       18056 :         if (ret < 0)
    1540           0 :             return ret;
    1541             :     }
    1542             : 
    1543        2257 :     if (avctx->hwaccel) {
    1544           0 :         ret = avctx->hwaccel->start_frame(avctx, NULL, 0);
    1545           0 :         if (ret < 0)
    1546           0 :             return ret;
    1547           0 :         ret = avctx->hwaccel->decode_slice(avctx, pkt->data, pkt->size);
    1548           0 :         if (ret < 0)
    1549           0 :             return ret;
    1550           0 :         ret = avctx->hwaccel->end_frame(avctx);
    1551           0 :         if (ret < 0)
    1552           0 :             return ret;
    1553           0 :         goto finish;
    1554             :     }
    1555             : 
    1556             :     // main tile decode loop
    1557        2257 :     memset(s->above_partition_ctx, 0, s->cols);
    1558        2257 :     memset(s->above_skip_ctx, 0, s->cols);
    1559        2257 :     if (s->s.h.keyframe || s->s.h.intraonly) {
    1560         457 :         memset(s->above_mode_ctx, DC_PRED, s->cols * 2);
    1561             :     } else {
    1562        1800 :         memset(s->above_mode_ctx, NEARESTMV, s->cols);
    1563             :     }
    1564        2257 :     memset(s->above_y_nnz_ctx, 0, s->sb_cols * 16);
    1565        2257 :     memset(s->above_uv_nnz_ctx[0], 0, s->sb_cols * 16 >> s->ss_h);
    1566        2257 :     memset(s->above_uv_nnz_ctx[1], 0, s->sb_cols * 16 >> s->ss_h);
    1567        2257 :     memset(s->above_segpred_ctx, 0, s->cols);
    1568        2257 :     s->pass = s->s.frames[CUR_FRAME].uses_2pass =
    1569        2257 :         avctx->active_thread_type == FF_THREAD_FRAME && s->s.h.refreshctx && !s->s.h.parallelmode;
    1570        2257 :     if ((ret = update_block_buffers(avctx)) < 0) {
    1571           0 :         av_log(avctx, AV_LOG_ERROR,
    1572             :                "Failed to allocate block buffers\n");
    1573           0 :         return ret;
    1574             :     }
    1575        2290 :     if (s->s.h.refreshctx && s->s.h.parallelmode) {
    1576             :         int j, k, l, m;
    1577             : 
    1578          97 :         for (i = 0; i < 4; i++) {
    1579         270 :             for (j = 0; j < 2; j++)
    1580         540 :                 for (k = 0; k < 2; k++)
    1581        2520 :                     for (l = 0; l < 6; l++)
    1582       15120 :                         for (m = 0; m < 6; m++)
    1583       12960 :                             memcpy(s->prob_ctx[s->s.h.framectxid].coef[i][j][k][l][m],
    1584       12960 :                                    s->prob.coef[i][j][k][l][m], 3);
    1585          90 :             if (s->s.h.txfmmode == i)
    1586          26 :                 break;
    1587             :         }
    1588          33 :         s->prob_ctx[s->s.h.framectxid].p = s->prob.p;
    1589          33 :         ff_thread_finish_setup(avctx);
    1590        2224 :     } else if (!s->s.h.refreshctx) {
    1591          11 :         ff_thread_finish_setup(avctx);
    1592             :     }
    1593             : 
    1594             : #if HAVE_THREADS
    1595        2257 :     if (avctx->active_thread_type & FF_THREAD_SLICE) {
    1596           0 :         for (i = 0; i < s->sb_rows; i++)
    1597           0 :             atomic_store(&s->entries[i], 0);
    1598             :     }
    1599             : #endif
    1600             : 
    1601             :     do {
    1602        4514 :         for (i = 0; i < s->active_tile_cols; i++) {
    1603        2257 :             s->td[i].b = s->td[i].b_base;
    1604        2257 :             s->td[i].block = s->td[i].block_base;
    1605        2257 :             s->td[i].uvblock[0] = s->td[i].uvblock_base[0];
    1606        2257 :             s->td[i].uvblock[1] = s->td[i].uvblock_base[1];
    1607        2257 :             s->td[i].eob = s->td[i].eob_base;
    1608        2257 :             s->td[i].uveob[0] = s->td[i].uveob_base[0];
    1609        2257 :             s->td[i].uveob[1] = s->td[i].uveob_base[1];
    1610             :         }
    1611             : 
    1612             : #if HAVE_THREADS
    1613        2257 :         if (avctx->active_thread_type == FF_THREAD_SLICE) {
    1614             :             int tile_row, tile_col;
    1615             : 
    1616             :             av_assert1(!s->pass);
    1617             : 
    1618           0 :             for (tile_row = 0; tile_row < s->s.h.tiling.tile_rows; tile_row++) {
    1619           0 :                 for (tile_col = 0; tile_col < s->s.h.tiling.tile_cols; tile_col++) {
    1620             :                     int64_t tile_size;
    1621             : 
    1622           0 :                     if (tile_col == s->s.h.tiling.tile_cols - 1 &&
    1623           0 :                         tile_row == s->s.h.tiling.tile_rows - 1) {
    1624           0 :                         tile_size = size;
    1625             :                     } else {
    1626           0 :                         tile_size = AV_RB32(data);
    1627           0 :                         data += 4;
    1628           0 :                         size -= 4;
    1629             :                     }
    1630           0 :                     if (tile_size > size)
    1631           0 :                         return AVERROR_INVALIDDATA;
    1632           0 :                     ret = ff_vp56_init_range_decoder(&s->td[tile_col].c_b[tile_row], data, tile_size);
    1633           0 :                     if (ret < 0)
    1634           0 :                         return ret;
    1635           0 :                     if (vp56_rac_get_prob_branchy(&s->td[tile_col].c_b[tile_row], 128)) // marker bit
    1636           0 :                         return AVERROR_INVALIDDATA;
    1637           0 :                     data += tile_size;
    1638           0 :                     size -= tile_size;
    1639             :                 }
    1640             :             }
    1641             : 
    1642           0 :             ff_slice_thread_execute_with_mainfunc(avctx, decode_tiles_mt, loopfilter_proc, s->td, NULL, s->s.h.tiling.tile_cols);
    1643             :         } else
    1644             : #endif
    1645             :         {
    1646        2257 :             ret = decode_tiles(avctx, data, size);
    1647        2257 :             if (ret < 0)
    1648           0 :                 return ret;
    1649             :         }
    1650             : 
    1651             :         // Sum all counts fields into td[0].counts for tile threading
    1652        2257 :         if (avctx->active_thread_type == FF_THREAD_SLICE)
    1653           0 :             for (i = 1; i < s->s.h.tiling.tile_cols; i++)
    1654           0 :                 for (j = 0; j < sizeof(s->td[i].counts) / sizeof(unsigned); j++)
    1655           0 :                     ((unsigned *)&s->td[0].counts)[j] += ((unsigned *)&s->td[i].counts)[j];
    1656             : 
    1657        2257 :         if (s->pass < 2 && s->s.h.refreshctx && !s->s.h.parallelmode) {
    1658        2213 :             ff_vp9_adapt_probs(s);
    1659        2213 :             ff_thread_finish_setup(avctx);
    1660             :         }
    1661        2257 :     } while (s->pass++ == 1);
    1662        2257 :     ff_thread_report_progress(&s->s.frames[CUR_FRAME].tf, INT_MAX, 0);
    1663             : 
    1664        2257 : finish:
    1665             :     // ref frame setup
    1666       20313 :     for (i = 0; i < 8; i++) {
    1667       18056 :         if (s->s.refs[i].f->buf[0])
    1668       14402 :             ff_thread_release_buffer(avctx, &s->s.refs[i]);
    1669       36059 :         if (s->next_refs[i].f->buf[0] &&
    1670       18003 :             (ret = ff_thread_ref_frame(&s->s.refs[i], &s->next_refs[i])) < 0)
    1671           0 :             return ret;
    1672             :     }
    1673             : 
    1674        2257 :     if (!s->s.h.invisible) {
    1675        2194 :         if ((ret = av_frame_ref(frame, s->s.frames[CUR_FRAME].tf.f)) < 0)
    1676           0 :             return ret;
    1677        2194 :         *got_frame = 1;
    1678             :     }
    1679             : 
    1680        2257 :     return pkt->size;
    1681             : }
    1682             : 
    1683           0 : static void vp9_decode_flush(AVCodecContext *avctx)
    1684             : {
    1685           0 :     VP9Context *s = avctx->priv_data;
    1686             :     int i;
    1687             : 
    1688           0 :     for (i = 0; i < 3; i++)
    1689           0 :         vp9_frame_unref(avctx, &s->s.frames[i]);
    1690           0 :     for (i = 0; i < 8; i++)
    1691           0 :         ff_thread_release_buffer(avctx, &s->s.refs[i]);
    1692           0 : }
    1693             : 
    1694         453 : static int init_frames(AVCodecContext *avctx)
    1695             : {
    1696         453 :     VP9Context *s = avctx->priv_data;
    1697             :     int i;
    1698             : 
    1699        1812 :     for (i = 0; i < 3; i++) {
    1700        1359 :         s->s.frames[i].tf.f = av_frame_alloc();
    1701        1359 :         if (!s->s.frames[i].tf.f) {
    1702           0 :             vp9_decode_free(avctx);
    1703           0 :             av_log(avctx, AV_LOG_ERROR, "Failed to allocate frame buffer %d\n", i);
    1704           0 :             return AVERROR(ENOMEM);
    1705             :         }
    1706             :     }
    1707        4077 :     for (i = 0; i < 8; i++) {
    1708        3624 :         s->s.refs[i].f = av_frame_alloc();
    1709        3624 :         s->next_refs[i].f = av_frame_alloc();
    1710        3624 :         if (!s->s.refs[i].f || !s->next_refs[i].f) {
    1711           0 :             vp9_decode_free(avctx);
    1712           0 :             av_log(avctx, AV_LOG_ERROR, "Failed to allocate frame buffer %d\n", i);
    1713           0 :             return AVERROR(ENOMEM);
    1714             :         }
    1715             :     }
    1716             : 
    1717         453 :     return 0;
    1718             : }
    1719             : 
    1720         453 : static av_cold int vp9_decode_init(AVCodecContext *avctx)
    1721             : {
    1722         453 :     VP9Context *s = avctx->priv_data;
    1723             : 
    1724         453 :     avctx->internal->allocate_progress = 1;
    1725         453 :     s->last_bpp = 0;
    1726         453 :     s->s.h.filter.sharpness = -1;
    1727             : 
    1728         453 :     return init_frames(avctx);
    1729             : }
    1730             : 
    1731             : #if HAVE_THREADS
    1732           0 : static av_cold int vp9_decode_init_thread_copy(AVCodecContext *avctx)
    1733             : {
    1734           0 :     return init_frames(avctx);
    1735             : }
    1736             : 
    1737           0 : static int vp9_decode_update_thread_context(AVCodecContext *dst, const AVCodecContext *src)
    1738             : {
    1739             :     int i, ret;
    1740           0 :     VP9Context *s = dst->priv_data, *ssrc = src->priv_data;
    1741             : 
    1742           0 :     for (i = 0; i < 3; i++) {
    1743           0 :         if (s->s.frames[i].tf.f->buf[0])
    1744           0 :             vp9_frame_unref(dst, &s->s.frames[i]);
    1745           0 :         if (ssrc->s.frames[i].tf.f->buf[0]) {
    1746           0 :             if ((ret = vp9_frame_ref(dst, &s->s.frames[i], &ssrc->s.frames[i])) < 0)
    1747           0 :                 return ret;
    1748             :         }
    1749             :     }
    1750           0 :     for (i = 0; i < 8; i++) {
    1751           0 :         if (s->s.refs[i].f->buf[0])
    1752           0 :             ff_thread_release_buffer(dst, &s->s.refs[i]);
    1753           0 :         if (ssrc->next_refs[i].f->buf[0]) {
    1754           0 :             if ((ret = ff_thread_ref_frame(&s->s.refs[i], &ssrc->next_refs[i])) < 0)
    1755           0 :                 return ret;
    1756             :         }
    1757             :     }
    1758             : 
    1759           0 :     s->s.h.invisible = ssrc->s.h.invisible;
    1760           0 :     s->s.h.keyframe = ssrc->s.h.keyframe;
    1761           0 :     s->s.h.intraonly = ssrc->s.h.intraonly;
    1762           0 :     s->ss_v = ssrc->ss_v;
    1763           0 :     s->ss_h = ssrc->ss_h;
    1764           0 :     s->s.h.segmentation.enabled = ssrc->s.h.segmentation.enabled;
    1765           0 :     s->s.h.segmentation.update_map = ssrc->s.h.segmentation.update_map;
    1766           0 :     s->s.h.segmentation.absolute_vals = ssrc->s.h.segmentation.absolute_vals;
    1767           0 :     s->bytesperpixel = ssrc->bytesperpixel;
    1768           0 :     s->gf_fmt = ssrc->gf_fmt;
    1769           0 :     s->w = ssrc->w;
    1770           0 :     s->h = ssrc->h;
    1771           0 :     s->s.h.bpp = ssrc->s.h.bpp;
    1772           0 :     s->bpp_index = ssrc->bpp_index;
    1773           0 :     s->pix_fmt = ssrc->pix_fmt;
    1774           0 :     memcpy(&s->prob_ctx, &ssrc->prob_ctx, sizeof(s->prob_ctx));
    1775           0 :     memcpy(&s->s.h.lf_delta, &ssrc->s.h.lf_delta, sizeof(s->s.h.lf_delta));
    1776           0 :     memcpy(&s->s.h.segmentation.feat, &ssrc->s.h.segmentation.feat,
    1777             :            sizeof(s->s.h.segmentation.feat));
    1778             : 
    1779           0 :     return 0;
    1780             : }
    1781             : #endif
    1782             : 
    1783             : AVCodec ff_vp9_decoder = {
    1784             :     .name                  = "vp9",
    1785             :     .long_name             = NULL_IF_CONFIG_SMALL("Google VP9"),
    1786             :     .type                  = AVMEDIA_TYPE_VIDEO,
    1787             :     .id                    = AV_CODEC_ID_VP9,
    1788             :     .priv_data_size        = sizeof(VP9Context),
    1789             :     .init                  = vp9_decode_init,
    1790             :     .close                 = vp9_decode_free,
    1791             :     .decode                = vp9_decode_frame,
    1792             :     .capabilities          = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS | AV_CODEC_CAP_SLICE_THREADS,
    1793             :     .caps_internal         = FF_CODEC_CAP_SLICE_THREAD_HAS_MF,
    1794             :     .flush                 = vp9_decode_flush,
    1795             :     .init_thread_copy      = ONLY_IF_THREADS_ENABLED(vp9_decode_init_thread_copy),
    1796             :     .update_thread_context = ONLY_IF_THREADS_ENABLED(vp9_decode_update_thread_context),
    1797             :     .profiles              = NULL_IF_CONFIG_SMALL(ff_vp9_profiles),
    1798             :     .bsfs                  = "vp9_superframe_split",
    1799             :     .hw_configs            = (const AVCodecHWConfigInternal*[]) {
    1800             : #if CONFIG_VP9_DXVA2_HWACCEL
    1801             :                                HWACCEL_DXVA2(vp9),
    1802             : #endif
    1803             : #if CONFIG_VP9_D3D11VA_HWACCEL
    1804             :                                HWACCEL_D3D11VA(vp9),
    1805             : #endif
    1806             : #if CONFIG_VP9_D3D11VA2_HWACCEL
    1807             :                                HWACCEL_D3D11VA2(vp9),
    1808             : #endif
    1809             : #if CONFIG_VP9_NVDEC_HWACCEL
    1810             :                                HWACCEL_NVDEC(vp9),
    1811             : #endif
    1812             : #if CONFIG_VP9_VAAPI_HWACCEL
    1813             :                                HWACCEL_VAAPI(vp9),
    1814             : #endif
    1815             :                                NULL
    1816             :                            },
    1817             : };

Generated by: LCOV version 1.13