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