]> git.sesse.net Git - ffmpeg/blobdiff - libavcodec/vp9.c
vp9: split last/cur_frame from the reference buffers.
[ffmpeg] / libavcodec / vp9.c
index 785b18725c5f2b99c9c7ea8f0e793bb4059d8e51..df9b1975498c2ca26229b4a5f3c01040dbefe658 100644 (file)
@@ -24,6 +24,7 @@
 #include "avcodec.h"
 #include "get_bits.h"
 #include "internal.h"
+#include "thread.h"
 #include "videodsp.h"
 #include "vp56.h"
 #include "vp9.h"
@@ -68,6 +69,13 @@ struct VP9mvrefPair {
     int8_t ref[2];
 };
 
+typedef struct VP9Frame {
+    ThreadFrame tf;
+    AVBufferRef *extradata;
+    uint8_t *segmentation_map;
+    struct VP9mvrefPair *mv;
+} VP9Frame;
+
 struct VP9Filter {
     uint8_t level[8 * 8];
     uint8_t /* bit=col */ mask[2 /* 0=y, 1=uv */][2 /* 0=col, 1=row */]
@@ -81,9 +89,6 @@ typedef struct VP9Block {
     enum BlockSize bs;
     enum TxfmMode tx, uvtx;
 
-    int row, row7, col, col7;
-    uint8_t *dst[3];
-    ptrdiff_t y_stride, uv_stride;
 } VP9Block;
 
 typedef struct VP9Context {
@@ -94,6 +99,9 @@ typedef struct VP9Context {
     VP56RangeCoder *c_b;
     unsigned c_b_size;
     VP9Block b;
+    int row, row7, col, col7;
+    uint8_t *dst[3];
+    ptrdiff_t y_stride, uv_stride;
 
     // bitstream header
     uint8_t profile;
@@ -116,7 +124,10 @@ typedef struct VP9Context {
     uint8_t refidx[3];
     uint8_t signbias[3];
     uint8_t varcompref[2];
-    AVFrame *refs[8], *f, *fb[10];
+    ThreadFrame refs[8], next_refs[8];
+#define CUR_FRAME 0
+#define LAST_FRAME 1
+    VP9Frame frames[2];
 
     struct {
         uint8_t level;
@@ -213,8 +224,6 @@ typedef struct VP9Context {
 
     // whole-frame cache
     uint8_t *intra_pred_data[3];
-    uint8_t *segmentation_map;
-    struct VP9mvrefPair *mv[2];
     struct VP9Filter *lflvl;
     DECLARE_ALIGNED(32, uint8_t, edge_emu_buffer)[71*80];
 
@@ -238,6 +247,48 @@ static const uint8_t bwh_tab[2][N_BS_SIZES][2] = {
     }
 };
 
+static int vp9_alloc_frame(AVCodecContext *ctx, VP9Frame *f)
+{
+    VP9Context *s = ctx->priv_data;
+    int ret, sz;
+
+    if ((ret = ff_thread_get_buffer(ctx, &f->tf, AV_GET_BUFFER_FLAG_REF)) < 0)
+        return ret;
+    sz = 64 * s->sb_cols * s->sb_rows;
+    if (!(f->extradata = av_buffer_allocz(sz * (1 + sizeof(struct VP9mvrefPair))))) {
+        ff_thread_release_buffer(ctx, &f->tf);
+        return AVERROR(ENOMEM);
+    }
+
+    f->segmentation_map = f->extradata->data;
+    f->mv = (struct VP9mvrefPair *) (f->extradata->data + sz);
+
+    return 0;
+}
+
+static void vp9_unref_frame(AVCodecContext *ctx, VP9Frame *f)
+{
+    ff_thread_release_buffer(ctx, &f->tf);
+    av_buffer_unref(&f->extradata);
+}
+
+static int vp9_ref_frame(AVCodecContext *ctx, VP9Frame *dst, VP9Frame *src)
+{
+    int res;
+
+    if ((res = ff_thread_ref_frame(&dst->tf, &src->tf)) < 0) {
+        return res;
+    } else if (!(dst->extradata = av_buffer_ref(src->extradata))) {
+        vp9_unref_frame(ctx, dst);
+        return AVERROR(ENOMEM);
+    }
+
+    dst->segmentation_map = src->segmentation_map;
+    dst->mv = src->mv;
+
+    return 0;
+}
+
 static int update_size(AVCodecContext *ctx, int w, int h)
 {
     VP9Context *s = ctx->priv_data;
@@ -257,8 +308,7 @@ static int update_size(AVCodecContext *ctx, int w, int h)
 
 #define assign(var, type, n) var = (type) p; p += s->sb_cols * n * sizeof(*var)
     av_freep(&s->above_partition_ctx);
-    p = av_malloc(s->sb_cols * (240 + sizeof(*s->lflvl) + 16 * sizeof(*s->above_mv_ctx) +
-                                64 * s->sb_rows * (1 + sizeof(*s->mv[0]) * 2)));
+    p = av_malloc(s->sb_cols * (240 + sizeof(*s->lflvl) + 16 * sizeof(*s->above_mv_ctx)));
     if (!p)
         return AVERROR(ENOMEM);
     assign(s->above_partition_ctx, uint8_t *,              8);
@@ -278,9 +328,6 @@ static int update_size(AVCodecContext *ctx, int w, int h)
     assign(s->above_filter_ctx,    uint8_t *,              8);
     assign(s->lflvl,               struct VP9Filter *,     1);
     assign(s->above_mv_ctx,        VP56mv(*)[2],          16);
-    assign(s->segmentation_map,    uint8_t *,             64 * s->sb_rows);
-    assign(s->mv[0],               struct VP9mvrefPair *, 64 * s->sb_rows);
-    assign(s->mv[1],               struct VP9mvrefPair *, 64 * s->sb_rows);
 #undef assign
 
     return 0;
@@ -427,20 +474,21 @@ static int decode_frame_header(AVCodecContext *ctx,
             s->signbias[1]    = get_bits1(&s->gb);
             s->refidx[2]      = get_bits(&s->gb, 3);
             s->signbias[2]    = get_bits1(&s->gb);
-            if (!s->refs[s->refidx[0]] || !s->refs[s->refidx[1]] ||
-                !s->refs[s->refidx[2]]) {
+            if (!s->refs[s->refidx[0]].f->data[0] ||
+                !s->refs[s->refidx[1]].f->data[0] ||
+                !s->refs[s->refidx[2]].f->data[0]) {
                 av_log(ctx, AV_LOG_ERROR, "Not all references are available\n");
                 return AVERROR_INVALIDDATA;
             }
             if (get_bits1(&s->gb)) {
-                w = s->refs[s->refidx[0]]->width;
-                h = s->refs[s->refidx[0]]->height;
+                w = s->refs[s->refidx[0]].f->width;
+                h = s->refs[s->refidx[0]].f->height;
             } else if (get_bits1(&s->gb)) {
-                w = s->refs[s->refidx[1]]->width;
-                h = s->refs[s->refidx[1]]->height;
+                w = s->refs[s->refidx[1]].f->width;
+                h = s->refs[s->refidx[1]].f->height;
             } else if (get_bits1(&s->gb)) {
-                w = s->refs[s->refidx[2]]->width;
-                h = s->refs[s->refidx[2]]->height;
+                w = s->refs[s->refidx[2]].f->width;
+                h = s->refs[s->refidx[2]].f->height;
             } else {
                 w = get_bits(&s->gb, 16) + 1;
                 h = get_bits(&s->gb, 16) + 1;
@@ -861,7 +909,7 @@ static void find_ref_mvs(VP9Context *s,
                       { -2,  0 }, { -1, -2 }, { -2, -1 }, { -2, -2 }},
     };
     VP9Block *const b = &s->b;
-    int row = b->row, col = b->col, row7 = b->row7;
+    int row = s->row, col = s->col, row7 = s->row7;
     const int8_t (*p)[2] = mv_ref_blk_off[b->bs];
 #define INVALID_MV 0x80008000U
     uint32_t mem = INVALID_MV;
@@ -921,7 +969,7 @@ static void find_ref_mvs(VP9Context *s,
     } while (0)
 
         if (row > 0) {
-            struct VP9mvrefPair *mv = &s->mv[0][(row - 1) * s->sb_cols * 8 + col];
+            struct VP9mvrefPair *mv = &s->frames[CUR_FRAME].mv[(row - 1) * s->sb_cols * 8 + col];
             if (mv->ref[0] == ref) {
                 RETURN_MV(s->above_mv_ctx[2 * col + (sb & 1)][0]);
             } else if (mv->ref[1] == ref) {
@@ -929,7 +977,7 @@ static void find_ref_mvs(VP9Context *s,
             }
         }
         if (col > s->tiling.tile_col_start) {
-            struct VP9mvrefPair *mv = &s->mv[0][row * s->sb_cols * 8 + col - 1];
+            struct VP9mvrefPair *mv = &s->frames[CUR_FRAME].mv[row * s->sb_cols * 8 + col - 1];
             if (mv->ref[0] == ref) {
                 RETURN_MV(s->left_mv_ctx[2 * row7 + (sb >> 1)][0]);
             } else if (mv->ref[1] == ref) {
@@ -946,7 +994,7 @@ static void find_ref_mvs(VP9Context *s,
         int c = p[i][0] + col, r = p[i][1] + row;
 
         if (c >= s->tiling.tile_col_start && c < s->cols && r >= 0 && r < s->rows) {
-            struct VP9mvrefPair *mv = &s->mv[0][r * s->sb_cols * 8 + c];
+            struct VP9mvrefPair *mv = &s->frames[CUR_FRAME].mv[r * s->sb_cols * 8 + c];
 
             if (mv->ref[0] == ref) {
                 RETURN_MV(mv->mv[0]);
@@ -958,7 +1006,7 @@ static void find_ref_mvs(VP9Context *s,
 
     // MV at this position in previous frame, using same reference frame
     if (s->use_last_frame_mvs) {
-        struct VP9mvrefPair *mv = &s->mv[1][row * s->sb_cols * 8 + col];
+        struct VP9mvrefPair *mv = &s->frames[LAST_FRAME].mv[row * s->sb_cols * 8 + col];
 
         if (mv->ref[0] == ref) {
             RETURN_MV(mv->mv[0]);
@@ -982,7 +1030,7 @@ static void find_ref_mvs(VP9Context *s,
         int c = p[i][0] + col, r = p[i][1] + row;
 
         if (c >= s->tiling.tile_col_start && c < s->cols && r >= 0 && r < s->rows) {
-            struct VP9mvrefPair *mv = &s->mv[0][r * s->sb_cols * 8 + c];
+            struct VP9mvrefPair *mv = &s->frames[CUR_FRAME].mv[r * s->sb_cols * 8 + c];
 
             if (mv->ref[0] != ref && mv->ref[0] >= 0) {
                 RETURN_SCALE_MV(mv->mv[0], s->signbias[mv->ref[0]] != s->signbias[ref]);
@@ -998,7 +1046,7 @@ static void find_ref_mvs(VP9Context *s,
 
     // MV at this position in previous frame, using different reference frame
     if (s->use_last_frame_mvs) {
-        struct VP9mvrefPair *mv = &s->mv[1][row * s->sb_cols * 8 + col];
+        struct VP9mvrefPair *mv = &s->frames[LAST_FRAME].mv[row * s->sb_cols * 8 + col];
 
         if (mv->ref[0] != ref && mv->ref[0] >= 0) {
             RETURN_SCALE_MV(mv->mv[0], s->signbias[mv->ref[0]] != s->signbias[ref]);
@@ -1157,7 +1205,7 @@ static void decode_mode(AVCodecContext *ctx)
     };
     VP9Context *s = ctx->priv_data;
     VP9Block *const b = &s->b;
-    int row = b->row, col = b->col, row7 = b->row7;
+    int row = s->row, col = s->col, row7 = s->row7;
     enum TxfmMode max_tx = max_tx_for_bl_bp[b->bs];
     int w4 = FFMIN(s->cols - col, bwh_tab[1][b->bs][0]);
     int h4 = FFMIN(s->rows - row, bwh_tab[1][b->bs][1]), y;
@@ -1174,10 +1222,11 @@ static void decode_mode(AVCodecContext *ctx)
                     s->prob.segpred[s->above_segpred_ctx[col] +
                                     s->left_segpred_ctx[row7]]))) {
         int pred = 8, x;
+        uint8_t *refsegmap = s->frames[LAST_FRAME].segmentation_map;
 
         for (y = 0; y < h4; y++)
             for (x = 0; x < w4; x++)
-                pred = FFMIN(pred, s->segmentation_map[(y + row) * 8 * s->sb_cols + x + col]);
+                pred = FFMIN(pred, refsegmap[(y + row) * 8 * s->sb_cols + x + col]);
         av_assert1(pred < 8);
         b->seg_id = pred;
 
@@ -1191,9 +1240,10 @@ static void decode_mode(AVCodecContext *ctx)
         memset(&s->left_segpred_ctx[row7], 0, h4);
     }
     if ((s->segmentation.enabled && s->segmentation.update_map) || s->keyframe) {
+        uint8_t *segmap = s->frames[CUR_FRAME].segmentation_map;
+
         for (y = 0; y < h4; y++)
-            memset(&s->segmentation_map[(y + row) * 8 * s->sb_cols + col],
-                   b->seg_id, w4);
+            memset(&segmap[(y + row) * 8 * s->sb_cols + col], b->seg_id, w4);
     }
 
     b->skip = s->segmentation.enabled &&
@@ -1767,24 +1817,25 @@ static void decode_mode(AVCodecContext *ctx)
     // FIXME kinda ugly
     for (y = 0; y < h4; y++) {
         int x, o = (row + y) * s->sb_cols * 8 + col;
+        struct VP9mvrefPair *mv = &s->frames[CUR_FRAME].mv[o];
 
         if (b->intra) {
             for (x = 0; x < w4; x++) {
-                s->mv[0][o + x].ref[0] =
-                s->mv[0][o + x].ref[1] = -1;
+                mv[x].ref[0] =
+                mv[x].ref[1] = -1;
             }
         } else if (b->comp) {
             for (x = 0; x < w4; x++) {
-                s->mv[0][o + x].ref[0] = b->ref[0];
-                s->mv[0][o + x].ref[1] = b->ref[1];
-                AV_COPY32(&s->mv[0][o + x].mv[0], &b->mv[3][0]);
-                AV_COPY32(&s->mv[0][o + x].mv[1], &b->mv[3][1]);
+                mv[x].ref[0] = b->ref[0];
+                mv[x].ref[1] = b->ref[1];
+                AV_COPY32(&mv[x].mv[0], &b->mv[3][0]);
+                AV_COPY32(&mv[x].mv[1], &b->mv[3][1]);
             }
         } else {
             for (x = 0; x < w4; x++) {
-                s->mv[0][o + x].ref[0] = b->ref[0];
-                s->mv[0][o + x].ref[1] = -1;
-                AV_COPY32(&s->mv[0][o + x].mv[0], &b->mv[3][0]);
+                mv[x].ref[0] = b->ref[0];
+                mv[x].ref[1] = -1;
+                AV_COPY32(&mv[x].mv[0], &b->mv[3][0]);
             }
         }
     }
@@ -1902,7 +1953,7 @@ static int decode_coeffs(AVCodecContext *ctx)
 {
     VP9Context *s = ctx->priv_data;
     VP9Block *const b = &s->b;
-    int row = b->row, col = b->col;
+    int row = s->row, col = s->col;
     uint8_t (*p)[6][11] = s->prob.coef[b->tx][0 /* y */][!b->intra];
     unsigned (*c)[6][3] = s->counts.coef[b->tx][0 /* y */][!b->intra];
     unsigned (*e)[6][2] = s->counts.eob[b->tx][0 /* y */][!b->intra];
@@ -2146,14 +2197,14 @@ static void intra_recon(AVCodecContext *ctx, ptrdiff_t y_off, ptrdiff_t uv_off)
 {
     VP9Context *s = ctx->priv_data;
     VP9Block *const b = &s->b;
-    int row = b->row, col = b->col;
+    int row = s->row, col = s->col;
     int w4 = bwh_tab[1][b->bs][0] << 1, step1d = 1 << b->tx, n;
     int h4 = bwh_tab[1][b->bs][1] << 1, x, y, step = 1 << (b->tx * 2);
     int end_x = FFMIN(2 * (s->cols - col), w4);
     int end_y = FFMIN(2 * (s->rows - row), h4);
     int tx = 4 * s->lossless + b->tx, uvtx = b->uvtx + 4 * s->lossless;
     int uvstep1d = 1 << b->uvtx, p;
-    uint8_t *dst = b->dst[0], *dst_r = s->f->data[0] + y_off;
+    uint8_t *dst = s->dst[0], *dst_r = s->frames[CUR_FRAME].tf.f->data[0] + y_off;
 
     for (n = 0, y = 0; y < end_y; y += step1d) {
         uint8_t *ptr = dst, *ptr_r = dst_r;
@@ -2166,16 +2217,17 @@ static void intra_recon(AVCodecContext *ctx, ptrdiff_t y_off, ptrdiff_t uv_off)
             enum TxfmType txtp = vp9_intra_txfm_type[mode];
             int eob = b->skip ? 0 : b->tx > TX_8X8 ? AV_RN16A(&s->eob[n]) : s->eob[n];
 
-            mode = check_intra_mode(s, mode, &a, ptr_r, s->f->linesize[0],
-                                    ptr, b->y_stride, l,
+            mode = check_intra_mode(s, mode, &a, ptr_r,
+                                    s->frames[CUR_FRAME].tf.f->linesize[0],
+                                    ptr, s->y_stride, l,
                                     col, x, w4, row, y, b->tx, 0);
-            s->dsp.intra_pred[b->tx][mode](ptr, b->y_stride, l, a);
+            s->dsp.intra_pred[b->tx][mode](ptr, s->y_stride, l, a);
             if (eob)
-                s->dsp.itxfm_add[tx][txtp](ptr, b->y_stride,
+                s->dsp.itxfm_add[tx][txtp](ptr, s->y_stride,
                                            s->block + 16 * n, eob);
         }
-        dst_r += 4 * s->f->linesize[0] * step1d;
-        dst   += 4 * b->y_stride       * step1d;
+        dst_r += 4 * step1d * s->frames[CUR_FRAME].tf.f->linesize[0];
+        dst   += 4 * step1d * s->y_stride;
     }
 
     // U/V
@@ -2185,8 +2237,8 @@ static void intra_recon(AVCodecContext *ctx, ptrdiff_t y_off, ptrdiff_t uv_off)
     end_y >>= 1;
     step = 1 << (b->uvtx * 2);
     for (p = 0; p < 2; p++) {
-        dst   = b->dst[1 + p];
-        dst_r = s->f->data[1 + p] + uv_off;
+        dst   = s->dst[1 + p];
+        dst_r = s->frames[CUR_FRAME].tf.f->data[1 + p] + uv_off;
         for (n = 0, y = 0; y < end_y; y += uvstep1d) {
             uint8_t *ptr = dst, *ptr_r = dst_r;
             for (x = 0; x < end_x; x += uvstep1d, ptr += 4 * uvstep1d,
@@ -2196,16 +2248,17 @@ static void intra_recon(AVCodecContext *ctx, ptrdiff_t y_off, ptrdiff_t uv_off)
                 uint8_t *a = &a_buf[16], l[32];
                 int eob = b->skip ? 0 : b->uvtx > TX_8X8 ? AV_RN16A(&s->uveob[p][n]) : s->uveob[p][n];
 
-                mode = check_intra_mode(s, mode, &a, ptr_r, s->f->linesize[1],
-                                        ptr, b->uv_stride, l,
+                mode = check_intra_mode(s, mode, &a, ptr_r,
+                                        s->frames[CUR_FRAME].tf.f->linesize[1],
+                                        ptr, s->uv_stride, l,
                                         col, x, w4, row, y, b->uvtx, p + 1);
-                s->dsp.intra_pred[b->uvtx][mode](ptr, b->uv_stride, l, a);
+                s->dsp.intra_pred[b->uvtx][mode](ptr, s->uv_stride, l, a);
                 if (eob)
-                    s->dsp.itxfm_add[uvtx][DCT_DCT](ptr, b->uv_stride,
+                    s->dsp.itxfm_add[uvtx][DCT_DCT](ptr, s->uv_stride,
                                                     s->uvblock[p] + 16 * n, eob);
             }
-            dst_r += 4 * uvstep1d * s->f->linesize[1];
-            dst   += 4 * uvstep1d * b->uv_stride;
+            dst_r += 4 * uvstep1d * s->frames[CUR_FRAME].tf.f->linesize[1];
+            dst   += 4 * uvstep1d * s->uv_stride;
         }
     }
 }
@@ -2285,45 +2338,47 @@ static void inter_recon(AVCodecContext *ctx)
     };
     VP9Context *s = ctx->priv_data;
     VP9Block *const b = &s->b;
-    int row = b->row, col = b->col;
-    AVFrame *ref1 = s->refs[s->refidx[b->ref[0]]];
-    AVFrame *ref2 = b->comp ? s->refs[s->refidx[b->ref[1]]] : NULL;
+    int row = s->row, col = s->col;
+    ThreadFrame *tref1 = &s->refs[s->refidx[b->ref[0]]];
+    AVFrame *ref1 = tref1->f;
+    ThreadFrame *tref2 = b->comp ? &s->refs[s->refidx[b->ref[1]]] : NULL;
+    AVFrame *ref2 = b->comp ? tref2->f : NULL;
     int w = ctx->width, h = ctx->height;
-    ptrdiff_t ls_y = b->y_stride, ls_uv = b->uv_stride;
+    ptrdiff_t ls_y = s->y_stride, ls_uv = s->uv_stride;
 
     // y inter pred
     if (b->bs > BS_8x8) {
         if (b->bs == BS_8x4) {
-            mc_luma_dir(s, s->dsp.mc[3][b->filter][0], b->dst[0], ls_y,
+            mc_luma_dir(s, s->dsp.mc[3][b->filter][0], s->dst[0], ls_y,
                         ref1->data[0], ref1->linesize[0],
                         row << 3, col << 3, &b->mv[0][0], 8, 4, w, h);
             mc_luma_dir(s, s->dsp.mc[3][b->filter][0],
-                        b->dst[0] + 4 * ls_y, ls_y,
+                        s->dst[0] + 4 * ls_y, ls_y,
                         ref1->data[0], ref1->linesize[0],
                         (row << 3) + 4, col << 3, &b->mv[2][0], 8, 4, w, h);
 
             if (b->comp) {
-                mc_luma_dir(s, s->dsp.mc[3][b->filter][1], b->dst[0], ls_y,
+                mc_luma_dir(s, s->dsp.mc[3][b->filter][1], s->dst[0], ls_y,
                             ref2->data[0], ref2->linesize[0],
                             row << 3, col << 3, &b->mv[0][1], 8, 4, w, h);
                 mc_luma_dir(s, s->dsp.mc[3][b->filter][1],
-                            b->dst[0] + 4 * ls_y, ls_y,
+                            s->dst[0] + 4 * ls_y, ls_y,
                             ref2->data[0], ref2->linesize[0],
                             (row << 3) + 4, col << 3, &b->mv[2][1], 8, 4, w, h);
             }
         } else if (b->bs == BS_4x8) {
-            mc_luma_dir(s, s->dsp.mc[4][b->filter][0], b->dst[0], ls_y,
+            mc_luma_dir(s, s->dsp.mc[4][b->filter][0], s->dst[0], ls_y,
                         ref1->data[0], ref1->linesize[0],
                         row << 3, col << 3, &b->mv[0][0], 4, 8, w, h);
-            mc_luma_dir(s, s->dsp.mc[4][b->filter][0], b->dst[0] + 4, ls_y,
+            mc_luma_dir(s, s->dsp.mc[4][b->filter][0], s->dst[0] + 4, ls_y,
                         ref1->data[0], ref1->linesize[0],
                         row << 3, (col << 3) + 4, &b->mv[1][0], 4, 8, w, h);
 
             if (b->comp) {
-                mc_luma_dir(s, s->dsp.mc[4][b->filter][1], b->dst[0], ls_y,
+                mc_luma_dir(s, s->dsp.mc[4][b->filter][1], s->dst[0], ls_y,
                             ref2->data[0], ref2->linesize[0],
                             row << 3, col << 3, &b->mv[0][1], 4, 8, w, h);
-                mc_luma_dir(s, s->dsp.mc[4][b->filter][1], b->dst[0] + 4, ls_y,
+                mc_luma_dir(s, s->dsp.mc[4][b->filter][1], s->dst[0] + 4, ls_y,
                             ref2->data[0], ref2->linesize[0],
                             row << 3, (col << 3) + 4, &b->mv[1][1], 4, 8, w, h);
             }
@@ -2332,34 +2387,34 @@ static void inter_recon(AVCodecContext *ctx)
 
             // FIXME if two horizontally adjacent blocks have the same MV,
             // do a w8 instead of a w4 call
-            mc_luma_dir(s, s->dsp.mc[4][b->filter][0], b->dst[0], ls_y,
+            mc_luma_dir(s, s->dsp.mc[4][b->filter][0], s->dst[0], ls_y,
                         ref1->data[0], ref1->linesize[0],
                         row << 3, col << 3, &b->mv[0][0], 4, 4, w, h);
-            mc_luma_dir(s, s->dsp.mc[4][b->filter][0], b->dst[0] + 4, ls_y,
+            mc_luma_dir(s, s->dsp.mc[4][b->filter][0], s->dst[0] + 4, ls_y,
                         ref1->data[0], ref1->linesize[0],
                         row << 3, (col << 3) + 4, &b->mv[1][0], 4, 4, w, h);
             mc_luma_dir(s, s->dsp.mc[4][b->filter][0],
-                        b->dst[0] + 4 * ls_y, ls_y,
+                        s->dst[0] + 4 * ls_y, ls_y,
                         ref1->data[0], ref1->linesize[0],
                         (row << 3) + 4, col << 3, &b->mv[2][0], 4, 4, w, h);
             mc_luma_dir(s, s->dsp.mc[4][b->filter][0],
-                        b->dst[0] + 4 * ls_y + 4, ls_y,
+                        s->dst[0] + 4 * ls_y + 4, ls_y,
                         ref1->data[0], ref1->linesize[0],
                         (row << 3) + 4, (col << 3) + 4, &b->mv[3][0], 4, 4, w, h);
 
             if (b->comp) {
-                mc_luma_dir(s, s->dsp.mc[4][b->filter][1], b->dst[0], ls_y,
+                mc_luma_dir(s, s->dsp.mc[4][b->filter][1], s->dst[0], ls_y,
                             ref2->data[0], ref2->linesize[0],
                             row << 3, col << 3, &b->mv[0][1], 4, 4, w, h);
-                mc_luma_dir(s, s->dsp.mc[4][b->filter][1], b->dst[0] + 4, ls_y,
+                mc_luma_dir(s, s->dsp.mc[4][b->filter][1], s->dst[0] + 4, ls_y,
                             ref2->data[0], ref2->linesize[0],
                             row << 3, (col << 3) + 4, &b->mv[1][1], 4, 4, w, h);
                 mc_luma_dir(s, s->dsp.mc[4][b->filter][1],
-                            b->dst[0] + 4 * ls_y, ls_y,
+                            s->dst[0] + 4 * ls_y, ls_y,
                             ref2->data[0], ref2->linesize[0],
                             (row << 3) + 4, col << 3, &b->mv[2][1], 4, 4, w, h);
                 mc_luma_dir(s, s->dsp.mc[4][b->filter][1],
-                            b->dst[0] + 4 * ls_y + 4, ls_y,
+                            s->dst[0] + 4 * ls_y + 4, ls_y,
                             ref2->data[0], ref2->linesize[0],
                             (row << 3) + 4, (col << 3) + 4, &b->mv[3][1], 4, 4, w, h);
             }
@@ -2368,12 +2423,12 @@ static void inter_recon(AVCodecContext *ctx)
         int bwl = bwlog_tab[0][b->bs];
         int bw = bwh_tab[0][b->bs][0] * 4, bh = bwh_tab[0][b->bs][1] * 4;
 
-        mc_luma_dir(s, s->dsp.mc[bwl][b->filter][0], b->dst[0], ls_y,
+        mc_luma_dir(s, s->dsp.mc[bwl][b->filter][0], s->dst[0], ls_y,
                     ref1->data[0], ref1->linesize[0],
                     row << 3, col << 3, &b->mv[0][0],bw, bh, w, h);
 
         if (b->comp)
-            mc_luma_dir(s, s->dsp.mc[bwl][b->filter][1], b->dst[0], ls_y,
+            mc_luma_dir(s, s->dsp.mc[bwl][b->filter][1], s->dst[0], ls_y,
                         ref2->data[0], ref2->linesize[0],
                         row << 3, col << 3, &b->mv[0][1], bw, bh, w, h);
     }
@@ -2394,7 +2449,7 @@ static void inter_recon(AVCodecContext *ctx)
         }
 
         mc_chroma_dir(s, s->dsp.mc[bwl][b->filter][0],
-                      b->dst[1], b->dst[2], ls_uv,
+                      s->dst[1], s->dst[2], ls_uv,
                       ref1->data[1], ref1->linesize[1],
                       ref1->data[2], ref1->linesize[2],
                       row << 2, col << 2, &mvuv, bw, bh, w, h);
@@ -2407,7 +2462,7 @@ static void inter_recon(AVCodecContext *ctx)
                 mvuv = b->mv[0][1];
             }
             mc_chroma_dir(s, s->dsp.mc[bwl][b->filter][1],
-                          b->dst[1], b->dst[2], ls_uv,
+                          s->dst[1], s->dst[2], ls_uv,
                           ref2->data[1], ref2->linesize[1],
                           ref2->data[2], ref2->linesize[2],
                           row << 2, col << 2, &mvuv, bw, bh, w, h);
@@ -2423,7 +2478,7 @@ static void inter_recon(AVCodecContext *ctx)
         int end_y = FFMIN(2 * (s->rows - row), h4);
         int tx = 4 * s->lossless + b->tx, uvtx = b->uvtx + 4 * s->lossless;
         int uvstep1d = 1 << b->uvtx, p;
-        uint8_t *dst = b->dst[0];
+        uint8_t *dst = s->dst[0];
 
         // y itxfm add
         for (n = 0, y = 0; y < end_y; y += step1d) {
@@ -2432,10 +2487,10 @@ static void inter_recon(AVCodecContext *ctx)
                 int eob = b->tx > TX_8X8 ? AV_RN16A(&s->eob[n]) : s->eob[n];
 
                 if (eob)
-                    s->dsp.itxfm_add[tx][DCT_DCT](ptr, b->y_stride,
+                    s->dsp.itxfm_add[tx][DCT_DCT](ptr, s->y_stride,
                                                   s->block + 16 * n, eob);
             }
-            dst += 4 * b->y_stride * step1d;
+            dst += 4 * s->y_stride * step1d;
         }
 
         // uv itxfm add
@@ -2445,17 +2500,17 @@ static void inter_recon(AVCodecContext *ctx)
         end_y >>= 1;
         step = 1 << (b->uvtx * 2);
         for (p = 0; p < 2; p++) {
-            dst = b->dst[p + 1];
+            dst = s->dst[p + 1];
             for (n = 0, y = 0; y < end_y; y += uvstep1d) {
                 uint8_t *ptr = dst;
                 for (x = 0; x < end_x; x += uvstep1d, ptr += 4 * uvstep1d, n += step) {
                     int eob = b->uvtx > TX_8X8 ? AV_RN16A(&s->uveob[p][n]) : s->uveob[p][n];
 
                     if (eob)
-                        s->dsp.itxfm_add[uvtx][DCT_DCT](ptr, b->uv_stride,
+                        s->dsp.itxfm_add[uvtx][DCT_DCT](ptr, s->uv_stride,
                                                         s->uvblock[p] + 16 * n, eob);
                 }
-                dst += 4 * uvstep1d * b->uv_stride;
+                dst += 4 * uvstep1d * s->uv_stride;
             }
         }
     }
@@ -2603,11 +2658,12 @@ static int decode_b(AVCodecContext *ctx, int row, int col,
     enum BlockSize bs = bl * 3 + bp;
     int res, y, w4 = bwh_tab[1][bs][0], h4 = bwh_tab[1][bs][1], lvl;
     int emu[2];
+    AVFrame *f = s->frames[CUR_FRAME].tf.f;
 
-    b->row = row;
-    b->row7 = row & 7;
-    b->col = col;
-    b->col7 = col & 7;
+    s->row = row;
+    s->row7 = row & 7;
+    s->col = col;
+    s->col7 = col & 7;
     s->min_mv.x = -(128 + col * 64);
     s->min_mv.y = -(128 + row * 64);
     s->max_mv.x = 128 + (s->cols - col - w4) * 64;
@@ -2633,25 +2689,25 @@ static int decode_b(AVCodecContext *ctx, int row, int col,
     // emulated overhangs if the stride of the target buffer can't hold. This
     // allows to support emu-edge and so on even if we have large block
     // overhangs
-    emu[0] = (col + w4) * 8 > s->f->linesize[0] ||
+    emu[0] = (col + w4) * 8 > f->linesize[0] ||
              (row + h4) > s->rows + 2 * !(ctx->flags & CODEC_FLAG_EMU_EDGE);
-    emu[1] = (col + w4) * 4 > s->f->linesize[1] ||
+    emu[1] = (col + w4) * 4 > f->linesize[1] ||
              (row + h4) > s->rows + 2 * !(ctx->flags & CODEC_FLAG_EMU_EDGE);
     if (emu[0]) {
-        b->dst[0] = s->tmp_y;
-        b->y_stride = 64;
+        s->dst[0] = s->tmp_y;
+        s->y_stride = 64;
     } else {
-        b->dst[0] = s->f->data[0] + yoff;
-        b->y_stride = s->f->linesize[0];
+        s->dst[0] = f->data[0] + yoff;
+        s->y_stride = f->linesize[0];
     }
     if (emu[1]) {
-        b->dst[1] = s->tmp_uv[0];
-        b->dst[2] = s->tmp_uv[1];
-        b->uv_stride = 32;
+        s->dst[1] = s->tmp_uv[0];
+        s->dst[2] = s->tmp_uv[1];
+        s->uv_stride = 32;
     } else {
-        b->dst[1] = s->f->data[1] + uvoff;
-        b->dst[2] = s->f->data[2] + uvoff;
-        b->uv_stride = s->f->linesize[1];
+        s->dst[1] = f->data[1] + uvoff;
+        s->dst[2] = f->data[2] + uvoff;
+        s->uv_stride = f->linesize[1];
     }
     if (b->intra) {
         intra_recon(ctx, yoff, uvoff);
@@ -2666,7 +2722,7 @@ static int decode_b(AVCodecContext *ctx, int row, int col,
 
             av_assert2(n <= 4);
             if (w & bw) {
-                s->dsp.mc[n][0][0][0][0](s->f->data[0] + yoff + o, s->f->linesize[0],
+                s->dsp.mc[n][0][0][0][0](f->data[0] + yoff + o, f->linesize[0],
                                          s->tmp_y + o, 64, h, 0, 0);
                 o += bw;
             }
@@ -2680,9 +2736,9 @@ static int decode_b(AVCodecContext *ctx, int row, int col,
 
             av_assert2(n <= 4);
             if (w & bw) {
-                s->dsp.mc[n][0][0][0][0](s->f->data[1] + uvoff + o, s->f->linesize[1],
+                s->dsp.mc[n][0][0][0][0](f->data[1] + uvoff + o, f->linesize[1],
                                          s->tmp_uv[0] + o, 32, h, 0, 0);
-                s->dsp.mc[n][0][0][0][0](s->f->data[2] + uvoff + o, s->f->linesize[2],
+                s->dsp.mc[n][0][0][0][0](f->data[2] + uvoff + o, f->linesize[2],
                                          s->tmp_uv[1] + o, 32, h, 0, 0);
                 o += bw;
             }
@@ -2732,6 +2788,8 @@ static int decode_sb(AVCodecContext *ctx, int row, int col, struct VP9Filter *lf
                                      s->prob.p.partition[bl][c];
     enum BlockPartition bp;
     ptrdiff_t hbs = 4 >> bl;
+    AVFrame *f = s->frames[CUR_FRAME].tf.f;
+    ptrdiff_t y_stride = f->linesize[0], uv_stride = f->linesize[1];
 
     if (bl == BL_8X8) {
         bp = vp8_rac_get_tree(&s->c, vp9_partition_tree, p);
@@ -2745,8 +2803,8 @@ static int decode_sb(AVCodecContext *ctx, int row, int col, struct VP9Filter *lf
                 break;
             case PARTITION_H:
                 if (!(res = decode_b(ctx, row, col, lflvl, yoff, uvoff, bl, bp))) {
-                    yoff  += hbs * 8 * s->f->linesize[0];
-                    uvoff += hbs * 4 * s->f->linesize[1];
+                    yoff  += hbs * 8 * y_stride;
+                    uvoff += hbs * 4 * uv_stride;
                     res = decode_b(ctx, row + hbs, col, lflvl, yoff, uvoff, bl, bp);
                 }
                 break;
@@ -2761,8 +2819,8 @@ static int decode_sb(AVCodecContext *ctx, int row, int col, struct VP9Filter *lf
                 if (!(res = decode_sb(ctx, row, col, lflvl, yoff, uvoff, bl + 1))) {
                     if (!(res = decode_sb(ctx, row, col + hbs, lflvl,
                                           yoff + 8 * hbs, uvoff + 4 * hbs, bl + 1))) {
-                        yoff  += hbs * 8 * s->f->linesize[0];
-                        uvoff += hbs * 4 * s->f->linesize[1];
+                        yoff  += hbs * 8 * y_stride;
+                        uvoff += hbs * 4 * uv_stride;
                         if (!(res = decode_sb(ctx, row + hbs, col, lflvl,
                                               yoff, uvoff, bl + 1)))
                             res = decode_sb(ctx, row + hbs, col + hbs, lflvl,
@@ -2786,8 +2844,8 @@ static int decode_sb(AVCodecContext *ctx, int row, int col, struct VP9Filter *lf
         if (vp56_rac_get_prob_branchy(&s->c, p[2])) {
             bp = PARTITION_SPLIT;
             if (!(res = decode_sb(ctx, row, col, lflvl, yoff, uvoff, bl + 1))) {
-                yoff  += hbs * 8 * s->f->linesize[0];
-                uvoff += hbs * 4 * s->f->linesize[1];
+                yoff  += hbs * 8 * y_stride;
+                uvoff += hbs * 4 * uv_stride;
                 res = decode_sb(ctx, row + hbs, col, lflvl,
                                 yoff, uvoff, bl + 1);
             }
@@ -2808,8 +2866,9 @@ static void loopfilter_sb(AVCodecContext *ctx, struct VP9Filter *lflvl,
                           int row, int col, ptrdiff_t yoff, ptrdiff_t uvoff)
 {
     VP9Context *s = ctx->priv_data;
-    uint8_t *dst = s->f->data[0] + yoff, *lvl = lflvl->level;
-    ptrdiff_t ls_y = s->f->linesize[0], ls_uv = s->f->linesize[1];
+    AVFrame *f = s->frames[CUR_FRAME].tf.f;
+    uint8_t *dst = f->data[0] + yoff, *lvl = lflvl->level;
+    ptrdiff_t ls_y = f->linesize[0], ls_uv = f->linesize[1];
     int y, x, p;
 
     // FIXME in how far can we interleave the v/h loopfilter calls? E.g.
@@ -2886,7 +2945,7 @@ static void loopfilter_sb(AVCodecContext *ctx, struct VP9Filter *lflvl,
     //                                          block1
     // filter edges between rows, Y plane (e.g. ------)
     //                                          block2
-    dst = s->f->data[0] + yoff;
+    dst = f->data[0] + yoff;
     lvl = lflvl->level;
     for (y = 0; y < 8; y++, dst += 8 * ls_y, lvl += 8) {
         uint8_t *ptr = dst, *l = lvl, *vmask = lflvl->mask[0][1][y];
@@ -2950,7 +3009,7 @@ static void loopfilter_sb(AVCodecContext *ctx, struct VP9Filter *lflvl,
     // same principle but for U/V planes
     for (p = 0; p < 2; p++) {
         lvl = lflvl->level;
-        dst = s->f->data[1 + p] + uvoff;
+        dst = f->data[1 + p] + uvoff;
         for (y = 0; y < 8; y += 4, dst += 16 * ls_uv, lvl += 32) {
             uint8_t *ptr = dst, *l = lvl, *hmask1 = lflvl->mask[1][0][y];
             uint8_t *hmask2 = lflvl->mask[1][0][y + 2];
@@ -2995,7 +3054,7 @@ static void loopfilter_sb(AVCodecContext *ctx, struct VP9Filter *lflvl,
             }
         }
         lvl = lflvl->level;
-        dst = s->f->data[1 + p] + uvoff;
+        dst = f->data[1 + p] + uvoff;
         for (y = 0; y < 8; y++, dst += 4 * ls_uv) {
             uint8_t *ptr = dst, *l = lvl, *vmask = lflvl->mask[1][1][y];
             unsigned vm = vmask[0] | vmask[1] | vmask[2];
@@ -3288,22 +3347,49 @@ static void adapt_probs(VP9Context *s)
     }
 }
 
-static int vp9_decode_frame(AVCodecContext *ctx, void *out_pic,
-                            int *got_frame, const uint8_t *data, int size)
+static av_cold int vp9_decode_free(AVCodecContext *ctx)
+{
+    VP9Context *s = ctx->priv_data;
+    int i;
+
+    for (i = 0; i < 2; i++) {
+        if (s->frames[i].tf.f->data[0])
+            vp9_unref_frame(ctx, &s->frames[i]);
+        av_frame_free(&s->frames[i].tf.f);
+    }
+    for (i = 0; i < 8; i++) {
+        if (s->refs[i].f->data[0])
+            ff_thread_release_buffer(ctx, &s->refs[i]);
+        av_frame_free(&s->refs[i].f);
+        if (s->next_refs[i].f->data[0])
+            ff_thread_release_buffer(ctx, &s->next_refs[i]);
+        av_frame_free(&s->next_refs[i].f);
+    }
+    av_freep(&s->above_partition_ctx);
+    av_freep(&s->c_b);
+
+    return 0;
+}
+
+
+static int vp9_decode_frame(AVCodecContext *ctx, AVFrame *frame,
+                            int *got_frame, AVPacket *pkt)
 {
+    const uint8_t *data = pkt->data;
+    int size = pkt->size;
     VP9Context *s = ctx->priv_data;
     int res, tile_row, tile_col, i, ref, row, col;
-    ptrdiff_t yoff = 0, uvoff = 0;
-    //AVFrame *prev_frame = s->f; // for segmentation map
+    ptrdiff_t yoff = 0, uvoff = 0, ls_y, ls_uv;
+    AVFrame *f;
 
     if ((res = decode_frame_header(ctx, data, size, &ref)) < 0) {
         return res;
     } else if (res == 0) {
-        if (!s->refs[ref]) {
+        if (!s->refs[ref].f->data[0]) {
             av_log(ctx, AV_LOG_ERROR, "Requested reference %d not available\n", ref);
             return AVERROR_INVALIDDATA;
         }
-        if ((res = av_frame_ref(out_pic, s->refs[ref])) < 0)
+        if ((res = av_frame_ref(frame, s->refs[ref].f)) < 0)
             return res;
         *got_frame = 1;
         return 0;
@@ -3311,28 +3397,29 @@ static int vp9_decode_frame(AVCodecContext *ctx, void *out_pic,
     data += res;
     size -= res;
 
-    // discard old references
-    for (i = 0; i < 10; i++) {
-        AVFrame *f = s->fb[i];
-        if (f->data[0] && f != s->f &&
-            f != s->refs[0] && f != s->refs[1] &&
-            f != s->refs[2] && f != s->refs[3] &&
-            f != s->refs[4] && f != s->refs[5] &&
-            f != s->refs[6] && f != s->refs[7])
-            av_frame_unref(f);
-    }
-
-    // find unused reference
-    for (i = 0; i < 10; i++)
-        if (!s->fb[i]->data[0])
-            break;
-    av_assert0(i < 10);
-    s->f = s->fb[i];
-    if ((res = ff_get_buffer(ctx, s->f,
-                             s->refreshrefmask ? AV_GET_BUFFER_FLAG_REF : 0)) < 0)
+    if (s->frames[LAST_FRAME].tf.f->data[0])
+        vp9_unref_frame(ctx, &s->frames[LAST_FRAME]);
+    if (!s->keyframe && s->frames[CUR_FRAME].tf.f->data[0] &&
+        (res = vp9_ref_frame(ctx, &s->frames[LAST_FRAME], &s->frames[CUR_FRAME])) < 0)
+        return res;
+    if (s->frames[CUR_FRAME].tf.f->data[0])
+        vp9_unref_frame(ctx, &s->frames[CUR_FRAME]);
+    if ((res = vp9_alloc_frame(ctx, &s->frames[CUR_FRAME])) < 0)
         return res;
-    s->f->key_frame = s->keyframe;
-    s->f->pict_type = s->keyframe ? AV_PICTURE_TYPE_I : AV_PICTURE_TYPE_P;
+    f = s->frames[CUR_FRAME].tf.f;
+    f->key_frame = s->keyframe;
+    f->pict_type = s->keyframe ? AV_PICTURE_TYPE_I : AV_PICTURE_TYPE_P;
+    ls_y = f->linesize[0];
+    ls_uv =f->linesize[1];
+
+    // ref frame setup
+    for (i = 0; i < 8; i++)
+        if (s->refreshrefmask & (1 << i)) {
+            if (s->next_refs[i].f->data[0])
+                ff_thread_release_buffer(ctx, &s->next_refs[i]);
+            if ((res = ff_thread_ref_frame(&s->next_refs[i], &s->frames[CUR_FRAME].tf)) < 0)
+                return res;
+        }
 
     // main tile decode loop
     memset(s->above_partition_ctx, 0, s->cols);
@@ -3369,10 +3456,8 @@ static int vp9_decode_frame(AVCodecContext *ctx, void *out_pic,
             size -= tile_size;
         }
 
-        for (row = s->tiling.tile_row_start;
-             row < s->tiling.tile_row_end;
-             row += 8, yoff += s->f->linesize[0] * 64,
-             uvoff += s->f->linesize[1] * 32) {
+        for (row = s->tiling.tile_row_start; row < s->tiling.tile_row_end;
+             row += 8, yoff += ls_y * 64, uvoff += ls_uv * 32) {
             struct VP9Filter *lflvl_ptr = s->lflvl;
             ptrdiff_t yoff2 = yoff, uvoff2 = uvoff;
 
@@ -3410,13 +3495,13 @@ static int vp9_decode_frame(AVCodecContext *ctx, void *out_pic,
             // prediction of next row of sb64s
             if (row + 8 < s->rows) {
                 memcpy(s->intra_pred_data[0],
-                       s->f->data[0] + yoff + 63 * s->f->linesize[0],
+                       f->data[0] + yoff + 63 * ls_y,
                        8 * s->cols);
                 memcpy(s->intra_pred_data[1],
-                       s->f->data[1] + uvoff + 31 * s->f->linesize[1],
+                       f->data[1] + uvoff + 31 * ls_uv,
                        4 * s->cols);
                 memcpy(s->intra_pred_data[2],
-                       s->f->data[2] + uvoff + 31 * s->f->linesize[2],
+                       f->data[2] + uvoff + 31 * ls_uv,
                        4 * s->cols);
             }
 
@@ -3437,7 +3522,7 @@ static int vp9_decode_frame(AVCodecContext *ctx, void *out_pic,
     // probability maintenance between frames)
     if (s->refreshctx) {
         if (s->parallelmode) {
-            int i, j, k, l, m;
+            int j, k, l, m;
 
             for (i = 0; i < 4; i++)
                 for (j = 0; j < 2; j++)
@@ -3451,15 +3536,16 @@ static int vp9_decode_frame(AVCodecContext *ctx, void *out_pic,
             adapt_probs(s);
         }
     }
-    FFSWAP(struct VP9mvrefPair *, s->mv[0], s->mv[1]);
 
     // ref frame setup
-    for (i = 0; i < 8; i++)
-        if (s->refreshrefmask & (1 << i))
-            s->refs[i] = s->f;
+    for (i = 0; i < 8; i++) {
+        if (s->refs[i].f->data[0])
+            ff_thread_release_buffer(ctx, &s->refs[i]);
+        ff_thread_ref_frame(&s->refs[i], &s->next_refs[i]);
+    }
 
     if (!s->invisible) {
-        if ((res = av_frame_ref(out_pic, s->f)) < 0)
+        if ((res = av_frame_ref(frame, s->frames[CUR_FRAME].tf.f)) < 0)
             return res;
         *got_frame = 1;
     }
@@ -3467,104 +3553,53 @@ static int vp9_decode_frame(AVCodecContext *ctx, void *out_pic,
     return 0;
 }
 
-static int vp9_decode_packet(AVCodecContext *avctx, void *out_pic,
-                             int *got_frame, AVPacket *avpkt)
-{
-    const uint8_t *data = avpkt->data;
-    int size = avpkt->size, marker, res;
-
-    // read superframe index - this is a collection of individual frames that
-    // together lead to one visible frame
-    av_assert1(size > 0); // without CODEC_CAP_DELAY, this is implied
-    marker = data[size - 1];
-    if ((marker & 0xe0) == 0xc0) {
-        int nbytes = 1 + ((marker >> 3) & 0x3);
-        int n_frames = 1 + (marker & 0x7), idx_sz = 2 + n_frames * nbytes;
-
-        if (size >= idx_sz && data[size - idx_sz] == marker) {
-            const uint8_t *idx = data + size + 1 - idx_sz;
-            switch (nbytes) {
-#define case_n(a, rd) \
-                case a: \
-                    while (n_frames--) { \
-                        int sz = rd; \
-                        idx += a; \
-                        if (sz > size) { \
-                            av_log(avctx, AV_LOG_ERROR, \
-                                   "Superframe packet size too big: %d > %d\n", \
-                                   sz, size); \
-                            return AVERROR_INVALIDDATA; \
-                        } \
-                        res = vp9_decode_frame(avctx, out_pic, got_frame, \
-                                               data, sz); \
-                        if (res < 0) \
-                            return res; \
-                        data += sz; \
-                        size -= sz; \
-                    } \
-                    break;
-                case_n(1, *idx);
-                case_n(2, AV_RL16(idx));
-                case_n(3, AV_RL24(idx));
-                case_n(4, AV_RL32(idx));
-            }
-            return avpkt->size;
-        }
-    }
-    // if we get here, there was no valid superframe index, i.e. this is just
-    // one whole single frame - decode it as such from the complete input buf
-    if ((res = vp9_decode_frame(avctx, out_pic, got_frame, data, size)) < 0)
-        return res;
-    return avpkt->size;
-}
-
 static void vp9_decode_flush(AVCodecContext *ctx)
 {
     VP9Context *s = ctx->priv_data;
     int i;
 
-    for (i = 0; i < 10; i++)
-        if (s->fb[i]->data[0])
-            av_frame_unref(s->fb[i]);
+    for (i = 0; i < 2; i++)
+        vp9_unref_frame(ctx, &s->frames[i]);
     for (i = 0; i < 8; i++)
-        s->refs[i] = NULL;
-    s->f = NULL;
+        ff_thread_release_buffer(ctx, &s->refs[i]);
 }
 
-static av_cold int vp9_decode_init(AVCodecContext *ctx)
+static int init_frames(AVCodecContext *ctx)
 {
     VP9Context *s = ctx->priv_data;
     int i;
 
-    ctx->pix_fmt = AV_PIX_FMT_YUV420P;
-    ff_vp9dsp_init(&s->dsp);
-    ff_videodsp_init(&s->vdsp, 8);
-    for (i = 0; i < 10; i++) {
-        s->fb[i] = av_frame_alloc();
-        if (!s->fb[i]) {
+    for (i = 0; i < 2; i++) {
+        s->frames[i].tf.f = av_frame_alloc();
+        if (!s->frames[i].tf.f) {
+            vp9_decode_free(ctx);
+            av_log(ctx, AV_LOG_ERROR, "Failed to allocate frame buffer %d\n", i);
+            return AVERROR(ENOMEM);
+        }
+    }
+    for (i = 0; i < 8; i++) {
+        s->refs[i].f = av_frame_alloc();
+        s->next_refs[i].f = av_frame_alloc();
+        if (!s->refs[i].f || !s->next_refs[i].f) {
+            vp9_decode_free(ctx);
             av_log(ctx, AV_LOG_ERROR, "Failed to allocate frame buffer %d\n", i);
             return AVERROR(ENOMEM);
         }
     }
-    s->filter.sharpness = -1;
 
     return 0;
 }
 
-static av_cold int vp9_decode_free(AVCodecContext *ctx)
+static av_cold int vp9_decode_init(AVCodecContext *ctx)
 {
     VP9Context *s = ctx->priv_data;
-    int i;
 
-    for (i = 0; i < 10; i++) {
-        if (s->fb[i]->data[0])
-            av_frame_unref(s->fb[i]);
-        av_frame_free(&s->fb[i]);
-    }
-    av_freep(&s->above_partition_ctx);
-    av_freep(&s->c_b);
+    ctx->pix_fmt = AV_PIX_FMT_YUV420P;
+    ff_vp9dsp_init(&s->dsp);
+    ff_videodsp_init(&s->vdsp, 8);
+    s->filter.sharpness = -1;
 
-    return 0;
+    return init_frames(ctx);
 }
 
 AVCodec ff_vp9_decoder = {
@@ -3575,7 +3610,7 @@ AVCodec ff_vp9_decoder = {
   .priv_data_size        = sizeof(VP9Context),
   .init                  = vp9_decode_init,
   .close                 = vp9_decode_free,
-  .decode                = vp9_decode_packet,
   .capabilities          = CODEC_CAP_DR1,
   .flush                 = vp9_decode_flush,
+    .decode                = vp9_decode_frame,
 };