From bc6e0b64a9100652c1ce52292408d8fd79930d53 Mon Sep 17 00:00:00 2001 From: "Ronald S. Bultje" Date: Sat, 23 Nov 2013 10:27:18 -0500 Subject: [PATCH 1/4] vp9: split last/cur_frame from the reference buffers. We need more information from last/cur_frame than from reference buffers, so we can use a simplified structure for reference buffers, and then store mvs and segmentation map information in last/cur. This prepares the decoder for frame threading support. Signed-off-by: Anton Khirnov --- libavcodec/vp9.c | 197 ++++++++++++++++++++++++++++++------------ libavcodec/vp9.h | 22 ++++- libavcodec/vp9block.c | 74 +++++++++------- libavcodec/vp9mvs.c | 12 +-- 4 files changed, 207 insertions(+), 98 deletions(-) diff --git a/libavcodec/vp9.c b/libavcodec/vp9.c index c11e9b8601..11ed00e3f9 100644 --- a/libavcodec/vp9.c +++ b/libavcodec/vp9.c @@ -34,13 +34,77 @@ #define VP9_SYNCCODE 0x498342 #define MAX_PROB 255 +static void vp9_frame_unref(AVCodecContext *avctx, VP9Frame *f) +{ + ff_thread_release_buffer(avctx, &f->tf); + av_buffer_unref(&f->segmentation_map_buf); + av_buffer_unref(&f->mv_buf); + f->segmentation_map = NULL; + f->mv = NULL; +} + +static int vp9_frame_alloc(AVCodecContext *avctx, VP9Frame *f) +{ + VP9Context *s = avctx->priv_data; + int ret, sz; + + ret = ff_thread_get_buffer(avctx, &f->tf, AV_GET_BUFFER_FLAG_REF); + if (ret < 0) + return ret; + + sz = 64 * s->sb_cols * s->sb_rows; + f->segmentation_map_buf = av_buffer_allocz(sz * sizeof(*f->segmentation_map)); + f->mv_buf = av_buffer_allocz(sz * sizeof(*f->mv)); + if (!f->segmentation_map_buf || !f->mv_buf) { + vp9_frame_unref(avctx, f); + return AVERROR(ENOMEM); + } + + f->segmentation_map = f->segmentation_map_buf->data; + f->mv = (VP9MVRefPair*)f->mv_buf->data; + + if (s->segmentation.enabled && !s->segmentation.update_map && + !s->keyframe && !s->intraonly) + memcpy(f->segmentation_map, s->frames[LAST_FRAME].segmentation_map, sz); + + return 0; +} + +static int vp9_frame_ref(VP9Frame *dst, VP9Frame *src) +{ + int ret; + + dst->segmentation_map_buf = av_buffer_ref(src->segmentation_map_buf); + dst->mv_buf = av_buffer_ref(src->mv_buf); + if (!dst->segmentation_map_buf || !dst->mv_buf) { + ret = AVERROR(ENOMEM); + goto fail; + } + + ret = ff_thread_ref_frame(&dst->tf, &src->tf); + if (ret < 0) + goto fail; + + dst->segmentation_map = src->segmentation_map; + dst->mv = src->mv; + + return 0; +fail: + av_buffer_unref(&dst->segmentation_map_buf); + av_buffer_unref(&dst->mv_buf); + return ret; +} + static void vp9_decode_flush(AVCodecContext *avctx) { VP9Context *s = avctx->priv_data; int i; + for (i = 0; i < FF_ARRAY_ELEMS(s->frames); i++) + vp9_frame_unref(avctx, &s->frames[i]); + for (i = 0; i < FF_ARRAY_ELEMS(s->refs); i++) - av_frame_unref(s->refs[i]); + ff_thread_release_buffer(avctx, &s->refs[i]); } static int update_size(AVCodecContext *avctx, int w, int h) @@ -66,8 +130,7 @@ static int update_size(AVCodecContext *avctx, int w, int h) #define assign(var, type, n) var = (type)p; p += s->sb_cols * n * sizeof(*var) av_free(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))); + (240 + sizeof(*s->lflvl) + 16 * sizeof(*s->above_mv_ctx))); if (!p) return AVERROR(ENOMEM); assign(s->above_partition_ctx, uint8_t *, 8); @@ -87,9 +150,6 @@ static int update_size(AVCodecContext *avctx, int w, int h) assign(s->above_filter_ctx, uint8_t *, 8); assign(s->lflvl, VP9Filter *, 1); assign(s->above_mv_ctx, VP56mv(*)[2], 16); - assign(s->segmentation_map, uint8_t *, 64 * s->sb_rows); - assign(s->mv[0], VP9MVRefPair *, 64 * s->sb_rows); - assign(s->mv[1], VP9MVRefPair *, 64 * s->sb_rows); #undef assign return 0; @@ -268,22 +328,22 @@ static int decode_frame_header(AVCodecContext *avctx, 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]]->buf[0] || - !s->refs[s->refidx[1]]->buf[0] || - !s->refs[s->refidx[2]]->buf[0]) { + if (!s->refs[s->refidx[0]].f->buf[0] || + !s->refs[s->refidx[1]].f->buf[0] || + !s->refs[s->refidx[2]].f->buf[0]) { av_log(avctx, 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; @@ -679,6 +739,7 @@ static int decode_subblock(AVCodecContext *avctx, int row, int col, ptrdiff_t yoff, ptrdiff_t uvoff, enum BlockLevel bl) { VP9Context *s = avctx->priv_data; + AVFrame *f = s->frames[CUR_FRAME].tf.f; int c = ((s->above_partition_ctx[col] >> (3 - bl)) & 1) | (((s->left_partition_ctx[row & 0x7] >> (3 - bl)) & 1) << 1); int ret; @@ -702,8 +763,8 @@ static int decode_subblock(AVCodecContext *avctx, int row, int col, ret = ff_vp9_decode_block(avctx, row, col, lflvl, yoff, uvoff, bl, bp); if (!ret) { - yoff += hbs * 8 * s->cur_frame->linesize[0]; - uvoff += hbs * 4 * s->cur_frame->linesize[1]; + yoff += hbs * 8 * f->linesize[0]; + uvoff += hbs * 4 * f->linesize[1]; ret = ff_vp9_decode_block(avctx, row + hbs, col, lflvl, yoff, uvoff, bl, bp); } @@ -726,8 +787,8 @@ static int decode_subblock(AVCodecContext *avctx, int row, int col, yoff + 8 * hbs, uvoff + 4 * hbs, bl + 1); if (!ret) { - yoff += hbs * 8 * s->cur_frame->linesize[0]; - uvoff += hbs * 4 * s->cur_frame->linesize[1]; + yoff += hbs * 8 * f->linesize[0]; + uvoff += hbs * 4 * f->linesize[1]; ret = decode_subblock(avctx, row + hbs, col, lflvl, yoff, uvoff, bl + 1); if (!ret) { @@ -758,8 +819,8 @@ static int decode_subblock(AVCodecContext *avctx, int row, int col, bp = PARTITION_SPLIT; ret = decode_subblock(avctx, row, col, lflvl, yoff, uvoff, bl + 1); if (!ret) { - yoff += hbs * 8 * s->cur_frame->linesize[0]; - uvoff += hbs * 4 * s->cur_frame->linesize[1]; + yoff += hbs * 8 * f->linesize[0]; + uvoff += hbs * 4 * f->linesize[1]; ret = decode_subblock(avctx, row + hbs, col, lflvl, yoff, uvoff, bl + 1); } @@ -782,8 +843,10 @@ static void loopfilter_subblock(AVCodecContext *avctx, VP9Filter *lflvl, ptrdiff_t yoff, ptrdiff_t uvoff) { VP9Context *s = avctx->priv_data; - uint8_t *dst = s->cur_frame->data[0] + yoff, *lvl = lflvl->level; - ptrdiff_t ls_y = s->cur_frame->linesize[0], ls_uv = s->cur_frame->linesize[1]; + AVFrame *f = s->frames[CUR_FRAME].tf.f; + uint8_t *dst = f->data[0] + yoff; + ptrdiff_t ls_y = f->linesize[0], ls_uv = f->linesize[1]; + uint8_t *lvl = lflvl->level; int y, x, p; /* FIXME: In how far can we interleave the v/h loopfilter calls? E.g. @@ -860,7 +923,7 @@ static void loopfilter_subblock(AVCodecContext *avctx, VP9Filter *lflvl, // block1 // filter edges between rows, Y plane (e.g. ------) // block2 - dst = s->cur_frame->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]; @@ -924,7 +987,7 @@ static void loopfilter_subblock(AVCodecContext *avctx, VP9Filter *lflvl, // same principle but for U/V planes for (p = 0; p < 2; p++) { lvl = lflvl->level; - dst = s->cur_frame->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]; @@ -971,7 +1034,7 @@ static void loopfilter_subblock(AVCodecContext *avctx, VP9Filter *lflvl, } } lvl = lflvl->level; - dst = s->cur_frame->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]; @@ -1030,6 +1093,7 @@ static int vp9_decode_frame(AVCodecContext *avctx, AVFrame *frame, int *got_frame, const uint8_t *data, int size) { VP9Context *s = avctx->priv_data; + AVFrame *f; int ret, tile_row, tile_col, i, ref = -1, row, col; ptrdiff_t yoff = 0, uvoff = 0; @@ -1037,13 +1101,13 @@ static int vp9_decode_frame(AVCodecContext *avctx, AVFrame *frame, if (ret < 0) { return ret; } else if (!ret) { - if (!s->refs[ref]->buf[0]) { + if (!s->refs[ref].f->buf[0]) { av_log(avctx, AV_LOG_ERROR, "Requested reference %d not available\n", ref); return AVERROR_INVALIDDATA; } - ret = av_frame_ref(frame, s->refs[ref]); + ret = av_frame_ref(frame, s->refs[ref].f); if (ret < 0) return ret; *got_frame = 1; @@ -1052,15 +1116,21 @@ static int vp9_decode_frame(AVCodecContext *avctx, AVFrame *frame, data += ret; size -= ret; - s->cur_frame = frame; + vp9_frame_unref(avctx, &s->frames[LAST_FRAME]); + if (!s->keyframe && s->frames[CUR_FRAME].tf.f->buf[0]) { + ret = vp9_frame_ref(&s->frames[LAST_FRAME], &s->frames[CUR_FRAME]); + if (ret < 0) + return ret; + } - av_frame_unref(s->cur_frame); - if ((ret = ff_get_buffer(avctx, s->cur_frame, - s->refreshrefmask ? AV_GET_BUFFER_FLAG_REF : 0)) < 0) + vp9_frame_unref(avctx, &s->frames[CUR_FRAME]); + ret = vp9_frame_alloc(avctx, &s->frames[CUR_FRAME]); + if (ret < 0) return ret; - s->cur_frame->key_frame = s->keyframe; - s->cur_frame->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; if (s->fullrange) avctx->color_range = AVCOL_RANGE_JPEG; @@ -1110,8 +1180,8 @@ static int vp9_decode_frame(AVCodecContext *avctx, AVFrame *frame, for (row = s->tiling.tile_row_start; row < s->tiling.tile_row_end; - row += 8, yoff += s->cur_frame->linesize[0] * 64, - uvoff += s->cur_frame->linesize[1] * 32) { + row += 8, yoff += f->linesize[0] * 64, + uvoff += f->linesize[1] * 32) { VP9Filter *lflvl = s->lflvl; ptrdiff_t yoff2 = yoff, uvoff2 = uvoff; @@ -1149,16 +1219,16 @@ static int vp9_decode_frame(AVCodecContext *avctx, AVFrame *frame, // prediction of next row of sb64s if (row + 8 < s->rows) { memcpy(s->intra_pred_data[0], - s->cur_frame->data[0] + yoff + - 63 * s->cur_frame->linesize[0], + f->data[0] + yoff + + 63 * f->linesize[0], 8 * s->cols); memcpy(s->intra_pred_data[1], - s->cur_frame->data[1] + uvoff + - 31 * s->cur_frame->linesize[1], + f->data[1] + uvoff + + 31 * f->linesize[1], 4 * s->cols); memcpy(s->intra_pred_data[2], - s->cur_frame->data[2] + uvoff + - 31 * s->cur_frame->linesize[2], + f->data[2] + uvoff + + 31 * f->linesize[2], 4 * s->cols); } @@ -1194,21 +1264,23 @@ static int vp9_decode_frame(AVCodecContext *avctx, AVFrame *frame, ff_vp9_adapt_probs(s); } } - FFSWAP(VP9MVRefPair *, s->mv[0], s->mv[1]); // ref frame setup for (i = 0; i < 8; i++) if (s->refreshrefmask & (1 << i)) { - av_frame_unref(s->refs[i]); - ret = av_frame_ref(s->refs[i], s->cur_frame); + ff_thread_release_buffer(avctx, &s->refs[i]); + ret = ff_thread_ref_frame(&s->refs[i], &s->frames[CUR_FRAME].tf); if (ret < 0) return ret; } - if (s->invisible) - av_frame_unref(s->cur_frame); - else + if (!s->invisible) { + av_frame_unref(frame); + ret = av_frame_ref(frame, s->frames[CUR_FRAME].tf.f); + if (ret < 0) + return ret; *got_frame = 1; + } return 0; } @@ -1267,8 +1339,15 @@ static av_cold int vp9_decode_free(AVCodecContext *avctx) VP9Context *s = avctx->priv_data; int i; - for (i = 0; i < FF_ARRAY_ELEMS(s->refs); i++) - av_frame_free(&s->refs[i]); + for (i = 0; i < FF_ARRAY_ELEMS(s->frames); i++) { + vp9_frame_unref(avctx, &s->frames[i]); + av_frame_free(&s->frames[i].tf.f); + } + + for (i = 0; i < FF_ARRAY_ELEMS(s->refs); i++) { + ff_thread_release_buffer(avctx, &s->refs[i]); + av_frame_free(&s->refs[i].f); + } av_freep(&s->c_b); av_freep(&s->above_partition_ctx); @@ -1286,17 +1365,23 @@ static av_cold int vp9_decode_init(AVCodecContext *avctx) ff_vp9dsp_init(&s->dsp); ff_videodsp_init(&s->vdsp, 8); + s->frames[0].tf.f = av_frame_alloc(); + s->frames[1].tf.f = av_frame_alloc(); + if (!s->frames[0].tf.f || !s->frames[1].tf.f) + goto fail; + for (i = 0; i < FF_ARRAY_ELEMS(s->refs); i++) { - s->refs[i] = av_frame_alloc(); - if (!s->refs[i]) { - vp9_decode_free(avctx); - return AVERROR(ENOMEM); - } + s->refs[i].f = av_frame_alloc(); + if (!s->refs[i].f) + goto fail; } s->filter.sharpness = -1; return 0; +fail: + vp9_decode_free(avctx); + return AVERROR(ENOMEM); } AVCodec ff_vp9_decoder = { diff --git a/libavcodec/vp9.h b/libavcodec/vp9.h index 31509bfbc5..8711987b8c 100644 --- a/libavcodec/vp9.h +++ b/libavcodec/vp9.h @@ -27,9 +27,11 @@ #include #include +#include "libavutil/buffer.h" #include "libavutil/internal.h" #include "avcodec.h" +#include "thread.h" #include "vp56.h" enum TxfmMode { @@ -225,6 +227,16 @@ typedef struct VP9Filter { [8 /* rows */][4 /* 0=16, 1=8, 2=4, 3=inner4 */]; } VP9Filter; +typedef struct VP9Frame { + ThreadFrame tf; + + uint8_t *segmentation_map; + VP9MVRefPair *mv; + + AVBufferRef *segmentation_map_buf; + AVBufferRef *mv_buf; +} VP9Frame; + enum BlockLevel { BL_64X64, BL_32X32, @@ -293,8 +305,12 @@ typedef struct VP9Context { uint8_t refidx[3]; uint8_t signbias[3]; uint8_t varcompref[2]; - AVFrame *refs[8]; - AVFrame *cur_frame; + + ThreadFrame refs[8]; + +#define CUR_FRAME 0 +#define LAST_FRAME 1 + VP9Frame frames[2]; struct { uint8_t level; @@ -392,8 +408,6 @@ typedef struct VP9Context { // whole-frame cache uint8_t *intra_pred_data[3]; - uint8_t *segmentation_map; - VP9MVRefPair *mv[2]; VP9Filter *lflvl; DECLARE_ALIGNED(32, uint8_t, edge_emu_buffer)[71 * 80]; diff --git a/libavcodec/vp9block.c b/libavcodec/vp9block.c index f5f7256424..c018fa0310 100644 --- a/libavcodec/vp9block.c +++ b/libavcodec/vp9block.c @@ -70,13 +70,14 @@ static void decode_mode(VP9Context *s, VP9Block *const b) vp56_rac_get_prob_branchy(&s->c, s->prob.segpred[s->above_segpred_ctx[col] + s->left_segpred_ctx[row7]]))) { + uint8_t *refsegmap = s->frames[LAST_FRAME].segmentation_map; int pred = MAX_SEGMENT - 1; int x; 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]); + refsegmap[(y + row) * 8 * s->sb_cols + x + col]); b->seg_id = pred; memset(&s->above_segpred_ctx[col], 1, w4); @@ -89,8 +90,10 @@ static void decode_mode(VP9Context *s, VP9Block *const b) 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], + memset(&segmap[(y + row) * 8 * s->sb_cols + col], b->seg_id, w4); } @@ -684,24 +687,25 @@ static void decode_mode(VP9Context *s, VP9Block *const b) // FIXME kinda ugly for (y = 0; y < h4; y++) { int x, o = (row + y) * s->sb_cols * 8 + col; + 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]); } } } @@ -1071,6 +1075,7 @@ static void intra_recon(AVCodecContext *avctx, ptrdiff_t y_off, ptrdiff_t uv_off { VP9Context *s = avctx->priv_data; VP9Block *const b = &s->b; + AVFrame *f = s->frames[CUR_FRAME].tf.f; int row = b->row, col = b->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); @@ -1078,7 +1083,7 @@ static void intra_recon(AVCodecContext *avctx, ptrdiff_t y_off, ptrdiff_t uv_off 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->cur_frame->data[0] + y_off; + uint8_t *dst = b->dst[0], *dst_r = f->data[0] + y_off; for (n = 0, y = 0; y < end_y; y += step1d) { uint8_t *ptr = dst, *ptr_r = dst_r; @@ -1092,7 +1097,7 @@ static void intra_recon(AVCodecContext *avctx, ptrdiff_t y_off, ptrdiff_t uv_off int eob = b->tx > TX_8X8 ? AV_RN16A(&s->eob[n]) : s->eob[n]; mode = check_intra_mode(s, mode, &a, ptr_r, - s->cur_frame->linesize[0], + f->linesize[0], ptr, b->y_stride, l, col, x, w4, row, y, b->tx, 0); s->dsp.intra_pred[b->tx][mode](ptr, b->y_stride, l, a); @@ -1100,7 +1105,7 @@ static void intra_recon(AVCodecContext *avctx, ptrdiff_t y_off, ptrdiff_t uv_off s->dsp.itxfm_add[tx][txtp](ptr, b->y_stride, s->block + 16 * n, eob); } - dst_r += 4 * s->cur_frame->linesize[0] * step1d; + dst_r += 4 * f->linesize[0] * step1d; dst += 4 * b->y_stride * step1d; } @@ -1112,7 +1117,7 @@ static void intra_recon(AVCodecContext *avctx, ptrdiff_t y_off, ptrdiff_t uv_off step = 1 << (b->uvtx * 2); for (p = 0; p < 2; p++) { dst = b->dst[1 + p]; - dst_r = s->cur_frame->data[1 + p] + uv_off; + dst_r = 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; @@ -1125,7 +1130,7 @@ static void intra_recon(AVCodecContext *avctx, ptrdiff_t y_off, ptrdiff_t uv_off : s->uveob[p][n]; mode = check_intra_mode(s, mode, &a, ptr_r, - s->cur_frame->linesize[1], + f->linesize[1], ptr, b->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); @@ -1134,7 +1139,7 @@ static void intra_recon(AVCodecContext *avctx, ptrdiff_t y_off, ptrdiff_t uv_off s->uvblock[p] + 16 * n, eob); } - dst_r += 4 * uvstep1d * s->cur_frame->linesize[1]; + dst_r += 4 * uvstep1d * f->linesize[1]; dst += 4 * uvstep1d * b->uv_stride; } } @@ -1224,8 +1229,12 @@ static int inter_recon(AVCodecContext *avctx) VP9Context *s = avctx->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; + + ThreadFrame *tref1 = &s->refs[s->refidx[b->ref[0]]]; + ThreadFrame *tref2 = b->comp ? &s->refs[s->refidx[b->ref[1]]] : NULL; + AVFrame *ref1 = tref1->f; + AVFrame *ref2 = tref2 ? tref2->f : NULL; + int w = avctx->width, h = avctx->height; ptrdiff_t ls_y = b->y_stride, ls_uv = b->uv_stride; @@ -1547,6 +1556,7 @@ int ff_vp9_decode_block(AVCodecContext *avctx, int row, int col, { VP9Context *s = avctx->priv_data; VP9Block *const b = &s->b; + AVFrame *f = s->frames[CUR_FRAME].tf.f; enum BlockSize bs = bl * 3 + bp; int ret, y, w4 = bwh_tab[1][bs][0], h4 = bwh_tab[1][bs][1], lvl; int emu[2]; @@ -1582,25 +1592,25 @@ int ff_vp9_decode_block(AVCodecContext *avctx, 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->cur_frame->linesize[0] || + emu[0] = (col + w4) * 8 > f->linesize[0] || (row + h4) > s->rows; - emu[1] = (col + w4) * 4 > s->cur_frame->linesize[1] || + emu[1] = (col + w4) * 4 > f->linesize[1] || (row + h4) > s->rows; if (emu[0]) { b->dst[0] = s->tmp_y; b->y_stride = 64; } else { - b->dst[0] = s->cur_frame->data[0] + yoff; - b->y_stride = s->cur_frame->linesize[0]; + b->dst[0] = f->data[0] + yoff; + b->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; } else { - b->dst[1] = s->cur_frame->data[1] + uvoff; - b->dst[2] = s->cur_frame->data[2] + uvoff; - b->uv_stride = s->cur_frame->linesize[1]; + b->dst[1] = f->data[1] + uvoff; + b->dst[2] = f->data[2] + uvoff; + b->uv_stride = f->linesize[1]; } if (b->intra) { intra_recon(avctx, yoff, uvoff); @@ -1618,9 +1628,9 @@ int ff_vp9_decode_block(AVCodecContext *avctx, int row, int col, av_assert2(n <= 4); if (w & bw) { - s->dsp.mc[n][0][0][0][0](s->cur_frame->data[0] + yoff + o, + s->dsp.mc[n][0][0][0][0](f->data[0] + yoff + o, s->tmp_y + o, - s->cur_frame->linesize[0], + f->linesize[0], 64, h, 0, 0); o += bw; } @@ -1636,13 +1646,13 @@ int ff_vp9_decode_block(AVCodecContext *avctx, int row, int col, av_assert2(n <= 4); if (w & bw) { - s->dsp.mc[n][0][0][0][0](s->cur_frame->data[1] + uvoff + o, + s->dsp.mc[n][0][0][0][0](f->data[1] + uvoff + o, s->tmp_uv[0] + o, - s->cur_frame->linesize[1], + f->linesize[1], 32, h, 0, 0); - s->dsp.mc[n][0][0][0][0](s->cur_frame->data[2] + uvoff + o, + s->dsp.mc[n][0][0][0][0](f->data[2] + uvoff + o, s->tmp_uv[1] + o, - s->cur_frame->linesize[2], + f->linesize[2], 32, h, 0, 0); o += bw; } diff --git a/libavcodec/vp9mvs.c b/libavcodec/vp9mvs.c index 1f65aaac0a..a4ce84c5f3 100644 --- a/libavcodec/vp9mvs.c +++ b/libavcodec/vp9mvs.c @@ -125,7 +125,7 @@ static void find_ref_mvs(VP9Context *s, } while (0) if (row > 0) { - VP9MVRefPair *mv = &s->mv[0][(row - 1) * s->sb_cols * 8 + col]; + 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]); @@ -133,7 +133,7 @@ static void find_ref_mvs(VP9Context *s, RETURN_MV(s->above_mv_ctx[2 * col + (sb & 1)][1]); } if (col > s->tiling.tile_col_start) { - VP9MVRefPair *mv = &s->mv[0][row * s->sb_cols * 8 + col - 1]; + 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]); @@ -151,7 +151,7 @@ static void find_ref_mvs(VP9Context *s, if (c >= s->tiling.tile_col_start && c < s->cols && r >= 0 && r < s->rows) { - VP9MVRefPair *mv = &s->mv[0][r * s->sb_cols * 8 + c]; + VP9MVRefPair *mv = &s->frames[CUR_FRAME].mv[r * s->sb_cols * 8 + c]; if (mv->ref[0] == ref) RETURN_MV(mv->mv[0]); @@ -162,7 +162,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) { - VP9MVRefPair *mv = &s->mv[1][row * s->sb_cols * 8 + col]; + VP9MVRefPair *mv = &s->frames[LAST_FRAME].mv[row * s->sb_cols * 8 + col]; if (mv->ref[0] == ref) RETURN_MV(mv->mv[0]); @@ -186,7 +186,7 @@ static void find_ref_mvs(VP9Context *s, if (c >= s->tiling.tile_col_start && c < s->cols && r >= 0 && r < s->rows) { - VP9MVRefPair *mv = &s->mv[0][r * s->sb_cols * 8 + c]; + 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], @@ -203,7 +203,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) { - VP9MVRefPair *mv = &s->mv[1][row * s->sb_cols * 8 + col]; + 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], From 5b995452a63ed754545a0ac90be79fac63b3390d Mon Sep 17 00:00:00 2001 From: "Ronald S. Bultje" Date: Sat, 23 Nov 2013 12:10:12 -0500 Subject: [PATCH 2/4] vp9: allocate 'b', 'block/uvblock' and 'eob/uveob' dynamically. This will be needed for frame threading. Signed-off-by: Anton Khirnov --- libavcodec/vp9.c | 24 ++++++++++++++++++++++++ libavcodec/vp9.h | 9 ++++----- libavcodec/vp9block.c | 8 ++++---- libavcodec/vp9mvs.c | 4 ++-- 4 files changed, 34 insertions(+), 11 deletions(-) diff --git a/libavcodec/vp9.c b/libavcodec/vp9.c index 11ed00e3f9..497dcf2d15 100644 --- a/libavcodec/vp9.c +++ b/libavcodec/vp9.c @@ -152,6 +152,19 @@ static int update_size(AVCodecContext *avctx, int w, int h) assign(s->above_mv_ctx, VP56mv(*)[2], 16); #undef assign + av_freep(&s->b_base); + av_freep(&s->block_base); + s->b_base = av_malloc(sizeof(*s->b_base)); + s->block_base = av_mallocz((64 * 64 + 128) * 3); + if (!s->b_base || !s->block_base) + return AVERROR(ENOMEM); + + s->uvblock_base[0] = s->block_base + 64 * 64; + s->uvblock_base[1] = s->uvblock_base[0] + 32 * 32; + s->eob_base = (uint8_t *) (s->uvblock_base[1] + 32 * 32); + s->uveob_base[0] = s->eob_base + 256; + s->uveob_base[1] = s->uveob_base[0] + 64; + return 0; } @@ -1155,6 +1168,15 @@ static int vp9_decode_frame(AVCodecContext *avctx, AVFrame *frame, memset(s->above_uv_nnz_ctx[0], 0, s->sb_cols * 8); memset(s->above_uv_nnz_ctx[1], 0, s->sb_cols * 8); memset(s->above_segpred_ctx, 0, s->cols); + + s->b = s->b_base; + s->block = s->block_base; + s->uvblock[0] = s->uvblock_base[0]; + s->uvblock[1] = s->uvblock_base[1]; + s->eob = s->eob_base; + s->uveob[0] = s->uveob_base[0]; + s->uveob[1] = s->uveob_base[1]; + for (tile_row = 0; tile_row < s->tiling.tile_rows; tile_row++) { set_tile_offset(&s->tiling.tile_row_start, &s->tiling.tile_row_end, tile_row, s->tiling.log2_tile_rows, s->sb_rows); @@ -1351,6 +1373,8 @@ static av_cold int vp9_decode_free(AVCodecContext *avctx) av_freep(&s->c_b); av_freep(&s->above_partition_ctx); + av_freep(&s->b_base); + av_freep(&s->block_base); return 0; } diff --git a/libavcodec/vp9.h b/libavcodec/vp9.h index 8711987b8c..e59129818c 100644 --- a/libavcodec/vp9.h +++ b/libavcodec/vp9.h @@ -280,7 +280,8 @@ typedef struct VP9Context { VP56RangeCoder c; VP56RangeCoder *c_b; unsigned c_b_size; - VP9Block b; + VP9Block *b; + VP9Block *b_base; // bitstream header uint8_t profile; @@ -412,10 +413,8 @@ typedef struct VP9Context { DECLARE_ALIGNED(32, uint8_t, edge_emu_buffer)[71 * 80]; // block reconstruction intermediates - DECLARE_ALIGNED(32, int16_t, block)[4096]; - DECLARE_ALIGNED(32, int16_t, uvblock)[2][1024]; - uint8_t eob[256]; - uint8_t uveob[2][64]; + int16_t *block_base, *block, *uvblock_base[2], *uvblock[2]; + uint8_t *eob_base, *uveob_base[2], *eob, *uveob[2]; struct { int x, y; } min_mv, max_mv; DECLARE_ALIGNED(32, uint8_t, tmp_y)[64 * 64]; DECLARE_ALIGNED(32, uint8_t, tmp_uv)[2][32 * 32]; diff --git a/libavcodec/vp9block.c b/libavcodec/vp9block.c index c018fa0310..feb5e6c00f 100644 --- a/libavcodec/vp9block.c +++ b/libavcodec/vp9block.c @@ -823,7 +823,7 @@ skip_eob: static int decode_coeffs(AVCodecContext *avctx) { VP9Context *s = avctx->priv_data; - VP9Block *const b = &s->b; + VP9Block *b = s->b; int row = b->row, col = b->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]; @@ -1074,7 +1074,7 @@ static av_always_inline int check_intra_mode(VP9Context *s, int mode, static void intra_recon(AVCodecContext *avctx, ptrdiff_t y_off, ptrdiff_t uv_off) { VP9Context *s = avctx->priv_data; - VP9Block *const b = &s->b; + VP9Block *b = s->b; AVFrame *f = s->frames[CUR_FRAME].tf.f; int row = b->row, col = b->col; int w4 = bwh_tab[1][b->bs][0] << 1, step1d = 1 << b->tx, n; @@ -1227,7 +1227,7 @@ static int inter_recon(AVCodecContext *avctx) { 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 4, 4 }, }; VP9Context *s = avctx->priv_data; - VP9Block *const b = &s->b; + VP9Block *b = s->b; int row = b->row, col = b->col; ThreadFrame *tref1 = &s->refs[s->refidx[b->ref[0]]]; @@ -1555,7 +1555,7 @@ int ff_vp9_decode_block(AVCodecContext *avctx, int row, int col, enum BlockLevel bl, enum BlockPartition bp) { VP9Context *s = avctx->priv_data; - VP9Block *const b = &s->b; + VP9Block *b = s->b; AVFrame *f = s->frames[CUR_FRAME].tf.f; enum BlockSize bs = bl * 3 + bp; int ret, y, w4 = bwh_tab[1][bs][0], h4 = bwh_tab[1][bs][1], lvl; diff --git a/libavcodec/vp9mvs.c b/libavcodec/vp9mvs.c index a4ce84c5f3..5edcb19039 100644 --- a/libavcodec/vp9mvs.c +++ b/libavcodec/vp9mvs.c @@ -64,7 +64,7 @@ static void find_ref_mvs(VP9Context *s, [BS_4x4] = { { 0, -1 }, { -1, 0 }, { -1, -1 }, { 0, -2 }, { -2, 0 }, { -1, -2 }, { -2, -1 }, { -2, -2 } }, }; - VP9Block *const b = &s->b; + VP9Block *b = s->b; int row = b->row, col = b->col, row7 = b->row7; const int8_t (*p)[2] = mv_ref_blk_off[b->bs]; #define INVALID_MV 0x80008000U @@ -279,7 +279,7 @@ static av_always_inline int read_mv_component(VP9Context *s, int idx, int hp) void ff_vp9_fill_mv(VP9Context *s, VP56mv *mv, int mode, int sb) { - VP9Block *const b = &s->b; + VP9Block *b = s->b; if (mode == ZEROMV) { memset(mv, 0, sizeof(*mv) * 2); From 1730a67ab99de0648dd55e81ea7fec12ab70225c Mon Sep 17 00:00:00 2001 From: "Ronald S. Bultje" Date: Tue, 2 Aug 2016 07:55:31 +0200 Subject: [PATCH 3/4] vp9: add frame threading Signed-off-by: Anton Khirnov --- libavcodec/vp9.c | 273 ++++++++++++++++++++++++++++++++++-------- libavcodec/vp9.h | 11 ++ libavcodec/vp9block.c | 111 ++++++++++++----- libavcodec/vp9mvs.c | 4 + 4 files changed, 318 insertions(+), 81 deletions(-) diff --git a/libavcodec/vp9.c b/libavcodec/vp9.c index 497dcf2d15..7989ca8d14 100644 --- a/libavcodec/vp9.c +++ b/libavcodec/vp9.c @@ -105,14 +105,20 @@ static void vp9_decode_flush(AVCodecContext *avctx) for (i = 0; i < FF_ARRAY_ELEMS(s->refs); i++) ff_thread_release_buffer(avctx, &s->refs[i]); + + s->use_last_frame_mvs = 0; + + s->alloc_width = 0; + s->alloc_height = 0; } static int update_size(AVCodecContext *avctx, int w, int h) { VP9Context *s = avctx->priv_data; uint8_t *p; + int nb_blocks, nb_superblocks; - if (s->above_partition_ctx && w == avctx->width && h == avctx->height) + if (s->above_partition_ctx && w == s->alloc_width && h == s->alloc_height) return 0; vp9_decode_flush(avctx); @@ -154,16 +160,26 @@ static int update_size(AVCodecContext *avctx, int w, int h) av_freep(&s->b_base); av_freep(&s->block_base); - s->b_base = av_malloc(sizeof(*s->b_base)); - s->block_base = av_mallocz((64 * 64 + 128) * 3); + + if (avctx->active_thread_type & FF_THREAD_FRAME) { + nb_blocks = s->cols * s->rows; + nb_superblocks = s->sb_cols * s->sb_rows; + } else { + nb_blocks = nb_superblocks = 1; + } + + s->b_base = av_malloc_array(nb_blocks, sizeof(*s->b_base)); + s->block_base = av_mallocz_array(nb_superblocks, (64 * 64 + 128) * 3); if (!s->b_base || !s->block_base) return AVERROR(ENOMEM); + s->uvblock_base[0] = s->block_base + nb_superblocks * 64 * 64; + s->uvblock_base[1] = s->uvblock_base[0] + nb_superblocks * 32 * 32; + s->eob_base = (uint8_t *)(s->uvblock_base[1] + nb_superblocks * 32 * 32); + s->uveob_base[0] = s->eob_base + nb_superblocks * 256; + s->uveob_base[1] = s->uveob_base[0] + nb_superblocks * 64; - s->uvblock_base[0] = s->block_base + 64 * 64; - s->uvblock_base[1] = s->uvblock_base[0] + 32 * 32; - s->eob_base = (uint8_t *) (s->uvblock_base[1] + 32 * 32); - s->uveob_base[0] = s->eob_base + 256; - s->uveob_base[1] = s->uveob_base[0] + 64; + s->alloc_width = w; + s->alloc_height = h; return 0; } @@ -278,7 +294,6 @@ static int decode_frame_header(AVCodecContext *avctx, last_invisible = s->invisible; s->invisible = !get_bits1(&s->gb); s->errorres = get_bits1(&s->gb); - // FIXME disable this upon resolution change s->use_last_frame_mvs = !s->errorres && !last_invisible; if (s->keyframe) { @@ -851,6 +866,61 @@ static int decode_subblock(AVCodecContext *avctx, int row, int col, return ret; } +static int decode_superblock_mem(AVCodecContext *avctx, int row, int col, struct VP9Filter *lflvl, + ptrdiff_t yoff, ptrdiff_t uvoff, enum BlockLevel bl) +{ + VP9Context *s = avctx->priv_data; + VP9Block *b = s->b; + 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]; + int res; + + if (bl == BL_8X8) { + av_assert2(b->bl == BL_8X8); + res = ff_vp9_decode_block(avctx, row, col, lflvl, yoff, uvoff, b->bl, b->bp); + } else if (s->b->bl == bl) { + if ((res = ff_vp9_decode_block(avctx, row, col, lflvl, yoff, uvoff, b->bl, b->bp)) < 0) + return res; + if (b->bp == PARTITION_H && row + hbs < s->rows) { + yoff += hbs * 8 * y_stride; + uvoff += hbs * 4 * uv_stride; + res = ff_vp9_decode_block(avctx, row + hbs, col, lflvl, yoff, uvoff, b->bl, b->bp); + } else if (b->bp == PARTITION_V && col + hbs < s->cols) { + yoff += hbs * 8; + uvoff += hbs * 4; + res = ff_vp9_decode_block(avctx, row, col + hbs, lflvl, yoff, uvoff, b->bl, b->bp); + } + } else { + if ((res = decode_superblock_mem(avctx, row, col, lflvl, yoff, uvoff, bl + 1)) < 0) + return res; + if (col + hbs < s->cols) { // FIXME why not <=? + if (row + hbs < s->rows) { + if ((res = decode_superblock_mem(avctx, row, col + hbs, lflvl, yoff + 8 * hbs, + uvoff + 4 * hbs, bl + 1)) < 0) + return res; + yoff += hbs * 8 * y_stride; + uvoff += hbs * 4 * uv_stride; + if ((res = decode_superblock_mem(avctx, row + hbs, col, lflvl, yoff, + uvoff, bl + 1)) < 0) + return res; + res = decode_superblock_mem(avctx, row + hbs, col + hbs, lflvl, + yoff + 8 * hbs, uvoff + 4 * hbs, bl + 1); + } else { + yoff += hbs * 8; + uvoff += hbs * 4; + res = decode_superblock_mem(avctx, row, col + hbs, lflvl, yoff, uvoff, bl + 1); + } + } else if (row + hbs < s->rows) { + yoff += hbs * 8 * y_stride; + uvoff += hbs * 4 * uv_stride; + res = decode_superblock_mem(avctx, row + hbs, col, lflvl, yoff, uvoff, bl + 1); + } + } + + return res; +} + static void loopfilter_subblock(AVCodecContext *avctx, VP9Filter *lflvl, int row, int col, ptrdiff_t yoff, ptrdiff_t uvoff) @@ -1102,13 +1172,29 @@ static void set_tile_offset(int *start, int *end, int idx, int log2_n, int n) *end = FFMIN(sb_end, n) << 3; } +static int update_refs(AVCodecContext *avctx) +{ + VP9Context *s = avctx->priv_data; + int i, ret; + + for (i = 0; i < FF_ARRAY_ELEMS(s->refs); i++) + if (s->refreshrefmask & (1 << i)) { + ff_thread_release_buffer(avctx, &s->refs[i]); + ret = ff_thread_ref_frame(&s->refs[i], &s->frames[CUR_FRAME].tf); + if (ret < 0) + return ret; + } + + return 0; +} + static int vp9_decode_frame(AVCodecContext *avctx, AVFrame *frame, - int *got_frame, const uint8_t *data, int size) + int *got_frame, const uint8_t *data, int size, + int can_finish_setup) { VP9Context *s = avctx->priv_data; AVFrame *f; int ret, tile_row, tile_col, i, ref = -1, row, col; - ptrdiff_t yoff = 0, uvoff = 0; ret = decode_frame_header(avctx, data, size, &ref); if (ret < 0) { @@ -1157,6 +1243,29 @@ static int vp9_decode_frame(AVCodecContext *avctx, AVFrame *frame, case 4: avctx->colorspace = AVCOL_SPC_SMPTE240M; break; } + s->pass = s->uses_2pass = + avctx->active_thread_type & FF_THREAD_FRAME && s->refreshctx && !s->parallelmode; + + if (s->refreshctx && s->parallelmode) { + int j, k, l, m; + for (i = 0; i < 4; i++) { + for (j = 0; j < 2; j++) + for (k = 0; k < 2; k++) + for (l = 0; l < 6; l++) + for (m = 0; m < 6; m++) + memcpy(s->prob_ctx[s->framectxid].coef[i][j][k][l][m], + s->prob.coef[i][j][k][l][m], 3); + if (s->txfmmode == i) + break; + } + s->prob_ctx[s->framectxid].p = s->prob.p; + } + if ((s->parallelmode || !s->refreshctx) && + can_finish_setup && avctx->active_thread_type & FF_THREAD_FRAME) { + ff_thread_finish_setup(avctx); + s->setup_finished = 1; + } + // main tile decode loop memset(s->above_partition_ctx, 0, s->cols); memset(s->above_skip_ctx, 0, s->cols); @@ -1169,6 +1278,8 @@ static int vp9_decode_frame(AVCodecContext *avctx, AVFrame *frame, memset(s->above_uv_nnz_ctx[1], 0, s->sb_cols * 8); memset(s->above_segpred_ctx, 0, s->cols); + do { + ptrdiff_t yoff = 0, uvoff = 0; s->b = s->b_base; s->block = s->block_base; s->uvblock[0] = s->uvblock_base[0]; @@ -1180,6 +1291,8 @@ static int vp9_decode_frame(AVCodecContext *avctx, AVFrame *frame, for (tile_row = 0; tile_row < s->tiling.tile_rows; tile_row++) { set_tile_offset(&s->tiling.tile_row_start, &s->tiling.tile_row_end, tile_row, s->tiling.log2_tile_rows, s->sb_rows); + + if (s->pass != 2) { for (tile_col = 0; tile_col < s->tiling.tile_cols; tile_col++) { int64_t tile_size; @@ -1191,14 +1304,19 @@ static int vp9_decode_frame(AVCodecContext *avctx, AVFrame *frame, data += 4; size -= 4; } - if (tile_size > size) - return AVERROR_INVALIDDATA; + if (tile_size > size) { + ret = AVERROR_INVALIDDATA; + goto fail; + } ff_vp56_init_range_decoder(&s->c_b[tile_col], data, tile_size); - if (vp56_rac_get_prob_branchy(&s->c_b[tile_col], 128)) // marker bit - return AVERROR_INVALIDDATA; + if (vp56_rac_get_prob_branchy(&s->c_b[tile_col], 128)) { // marker bit + ret = AVERROR_INVALIDDATA; + goto fail; + } data += tile_size; size -= tile_size; } + } for (row = s->tiling.tile_row_start; row < s->tiling.tile_row_end; @@ -1228,15 +1346,26 @@ static int vp9_decode_frame(AVCodecContext *avctx, AVFrame *frame, col += 8, yoff2 += 64, uvoff2 += 32, lflvl++) { // FIXME integrate with lf code (i.e. zero after each // use, similar to invtxfm coefficients, or similar) - memset(lflvl->mask, 0, sizeof(lflvl->mask)); + if (s->pass != 1) + memset(lflvl->mask, 0, sizeof(lflvl->mask)); - if ((ret = decode_subblock(avctx, row, col, lflvl, - yoff2, uvoff2, BL_64X64)) < 0) - return ret; + if (s->pass == 2) { + ret = decode_superblock_mem(avctx, row, col, lflvl, + yoff2, uvoff2, BL_64X64); + } else { + ret = decode_subblock(avctx, row, col, lflvl, + yoff2, uvoff2, BL_64X64); + } + if (ret < 0) + goto fail; } - memcpy(&s->c_b[tile_col], &s->c, sizeof(s->c)); + if (s->pass != 2) + memcpy(&s->c_b[tile_col], &s->c, sizeof(s->c)); } + if (s->pass == 1) + continue; + // backup pre-loopfilter reconstruction data for intra // prediction of next row of sb64s if (row + 8 < s->rows) { @@ -1263,38 +1392,33 @@ static int vp9_decode_frame(AVCodecContext *avctx, AVFrame *frame, col += 8, yoff2 += 64, uvoff2 += 32, lflvl++) loopfilter_subblock(avctx, lflvl, row, col, yoff2, uvoff2); } + + // FIXME maybe we can make this more finegrained by running the + // loopfilter per-block instead of after each sbrow + // In fact that would also make intra pred left preparation easier? + ff_thread_report_progress(&s->frames[CUR_FRAME].tf, row >> 3, 0); } } - // bw adaptivity (or in case of parallel decoding mode, fw adaptivity - // probability maintenance between frames) - if (s->refreshctx) { - if (s->parallelmode) { - int j, k, l, m; - for (i = 0; i < 4; i++) { - for (j = 0; j < 2; j++) - for (k = 0; k < 2; k++) - for (l = 0; l < 6; l++) - for (m = 0; m < 6; m++) - memcpy(s->prob_ctx[s->framectxid].coef[i][j][k][l][m], - s->prob.coef[i][j][k][l][m], 3); - if (s->txfmmode == i) - break; - } - s->prob_ctx[s->framectxid].p = s->prob.p; - } else { + if (s->pass < 2 && s->refreshctx && !s->parallelmode) { ff_vp9_adapt_probs(s); + if (can_finish_setup && avctx->active_thread_type & FF_THREAD_FRAME) { + ff_thread_finish_setup(avctx); + s->setup_finished = 1; + } } - } + } while (s->pass++ == 1); +fail: + ff_thread_report_progress(&s->frames[CUR_FRAME].tf, INT_MAX, 0); + if (ret < 0) + return ret; // ref frame setup - for (i = 0; i < 8; i++) - if (s->refreshrefmask & (1 << i)) { - ff_thread_release_buffer(avctx, &s->refs[i]); - ret = ff_thread_ref_frame(&s->refs[i], &s->frames[CUR_FRAME].tf); - if (ret < 0) - return ret; - } + if (!s->setup_finished) { + ret = update_refs(avctx); + if (ret < 0) + return ret; + } if (!s->invisible) { av_frame_unref(frame); @@ -1310,10 +1434,13 @@ static int vp9_decode_frame(AVCodecContext *avctx, AVFrame *frame, static int vp9_decode_packet(AVCodecContext *avctx, void *frame, int *got_frame, AVPacket *avpkt) { + VP9Context *s = avctx->priv_data; const uint8_t *data = avpkt->data; int size = avpkt->size; int marker, ret; + s->setup_finished = 0; + /* Read superframe index - this is a collection of individual frames * that together lead to one visible frame */ marker = data[size - 1]; @@ -1339,7 +1466,8 @@ static int vp9_decode_packet(AVCodecContext *avctx, void *frame, return AVERROR_INVALIDDATA; } - ret = vp9_decode_frame(avctx, frame, got_frame, data, sz); + ret = vp9_decode_frame(avctx, frame, got_frame, data, sz, + !n_frames); if (ret < 0) return ret; data += sz; @@ -1351,7 +1479,7 @@ static int vp9_decode_packet(AVCodecContext *avctx, void *frame, /* 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 ((ret = vp9_decode_frame(avctx, frame, got_frame, data, size)) < 0) + if ((ret = vp9_decode_frame(avctx, frame, got_frame, data, size, 1)) < 0) return ret; return size; } @@ -1384,6 +1512,10 @@ static av_cold int vp9_decode_init(AVCodecContext *avctx) VP9Context *s = avctx->priv_data; int i; + memset(s, 0, sizeof(*s)); + + avctx->internal->allocate_progress = 1; + avctx->pix_fmt = AV_PIX_FMT_YUV420P; ff_vp9dsp_init(&s->dsp); @@ -1408,6 +1540,49 @@ fail: return AVERROR(ENOMEM); } +static int vp9_decode_update_thread_context(AVCodecContext *dst, const AVCodecContext *src) +{ + VP9Context *s = dst->priv_data, *ssrc = src->priv_data; + int i, ret; + + ret = update_size(dst, ssrc->alloc_width, ssrc->alloc_height); + if (ret < 0) + return ret; + + for (i = 0; i < 2; i++) { + if (s->frames[i].tf.f->data[0]) + vp9_frame_unref(dst, &s->frames[i]); + if (ssrc->frames[i].tf.f->data[0]) { + if ((ret = vp9_frame_ref(&s->frames[i], &ssrc->frames[i])) < 0) + return ret; + } + } + for (i = 0; i < FF_ARRAY_ELEMS(s->refs); i++) { + ff_thread_release_buffer(dst, &s->refs[i]); + if (ssrc->refs[i].f->buf[0]) { + ret = ff_thread_ref_frame(&s->refs[i], &ssrc->refs[i]); + if (ret < 0) + return ret; + } + } + + s->refreshrefmask = ssrc->refreshrefmask; + ret = update_refs(dst); + if (ret < 0) + return ret; + + s->invisible = ssrc->invisible; + s->keyframe = ssrc->keyframe; + s->last_uses_2pass = ssrc->uses_2pass; + + memcpy(&s->prob_ctx, &ssrc->prob_ctx, sizeof(s->prob_ctx)); + memcpy(&s->lf_delta, &ssrc->lf_delta, sizeof(s->lf_delta)); + memcpy(&s->segmentation.feat, &ssrc->segmentation.feat, + sizeof(s->segmentation.feat)); + + return 0; +} + AVCodec ff_vp9_decoder = { .name = "vp9", .long_name = NULL_IF_CONFIG_SMALL("Google VP9"), @@ -1418,5 +1593,7 @@ AVCodec ff_vp9_decoder = { .decode = vp9_decode_packet, .flush = vp9_decode_flush, .close = vp9_decode_free, - .capabilities = AV_CODEC_CAP_DR1, + .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS, + .init_thread_copy = vp9_decode_init, + .update_thread_context = vp9_decode_update_thread_context, }; diff --git a/libavcodec/vp9.h b/libavcodec/vp9.h index e59129818c..25bb2d159b 100644 --- a/libavcodec/vp9.h +++ b/libavcodec/vp9.h @@ -271,6 +271,9 @@ typedef struct VP9Block { int row, row7, col, col7; uint8_t *dst[3]; ptrdiff_t y_stride, uv_stride; + + enum BlockLevel bl; + enum BlockPartition bp; } VP9Block; typedef struct VP9Context { @@ -283,6 +286,14 @@ typedef struct VP9Context { VP9Block *b; VP9Block *b_base; + int alloc_width; + int alloc_height; + + int pass; + int uses_2pass; + int last_uses_2pass; + int setup_finished; + // bitstream header uint8_t profile; uint8_t keyframe, last_keyframe; diff --git a/libavcodec/vp9block.c b/libavcodec/vp9block.c index feb5e6c00f..9b0d836adc 100644 --- a/libavcodec/vp9block.c +++ b/libavcodec/vp9block.c @@ -74,6 +74,9 @@ static void decode_mode(VP9Context *s, VP9Block *const b) int pred = MAX_SEGMENT - 1; int x; + if (!s->last_uses_2pass) + ff_thread_await_progress(&s->frames[LAST_FRAME].tf, row >> 3, 0); + for (y = 0; y < h4; y++) for (x = 0; x < w4; x++) pred = FFMIN(pred, @@ -1149,17 +1152,25 @@ static av_always_inline void mc_luma_dir(VP9Context *s, vp9_mc_func(*mc)[2], uint8_t *dst, ptrdiff_t dst_stride, const uint8_t *ref, ptrdiff_t ref_stride, + ThreadFrame *ref_frame, ptrdiff_t y, ptrdiff_t x, const VP56mv *mv, int bw, int bh, int w, int h) { int mx = mv->x, my = mv->y; + int th; y += my >> 3; x += mx >> 3; ref += y * ref_stride + x; mx &= 7; my &= 7; + + // we use +7 because the last 7 pixels of each sbrow can be changed in + // the longest loopfilter of the next sbrow + th = (y + bh + 4 * !!my + 7) >> 6; + ff_thread_await_progress(ref_frame, FFMAX(th, 0), 0); + // FIXME bilinear filter only needs 0/1 pixels, not 3/4 if (x < !!mx * 3 || y < !!my * 3 || x + !!mx * 4 > w - bw || y + !!my * 4 > h - bh) { @@ -1182,11 +1193,13 @@ static av_always_inline void mc_chroma_dir(VP9Context *s, vp9_mc_func(*mc)[2], ptrdiff_t src_stride_u, const uint8_t *ref_v, ptrdiff_t src_stride_v, + ThreadFrame *ref_frame, ptrdiff_t y, ptrdiff_t x, const VP56mv *mv, int bw, int bh, int w, int h) { int mx = mv->x, my = mv->y; + int th; y += my >> 4; x += mx >> 4; @@ -1194,6 +1207,12 @@ static av_always_inline void mc_chroma_dir(VP9Context *s, vp9_mc_func(*mc)[2], ref_v += y * src_stride_v + x; mx &= 15; my &= 15; + + // we use +7 because the last 7 pixels of each sbrow can be changed in + // the longest loopfilter of the next sbrow + th = (y + bh + 4 * !!my + 7) >> 5; + ff_thread_await_progress(ref_frame, FFMAX(th, 0), 0); + // FIXME bilinear filter only needs 0/1 pixels, not 3/4 if (x < !!mx * 3 || y < !!my * 3 || x + !!mx * 4 > w - bw || y + !!my * 4 > h - bh) { @@ -1245,36 +1264,36 @@ static int inter_recon(AVCodecContext *avctx) 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, - ref1->data[0], ref1->linesize[0], + ref1->data[0], ref1->linesize[0], tref1, 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, - ref1->data[0], ref1->linesize[0], + ref1->data[0], ref1->linesize[0], tref1, (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, - ref2->data[0], ref2->linesize[0], + ref2->data[0], ref2->linesize[0], tref2, 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, - ref2->data[0], ref2->linesize[0], + ref2->data[0], ref2->linesize[0], tref2, (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, - ref1->data[0], ref1->linesize[0], + ref1->data[0], ref1->linesize[0], tref1, 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, - ref1->data[0], ref1->linesize[0], + ref1->data[0], ref1->linesize[0], tref1, 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, - ref2->data[0], ref2->linesize[0], + ref2->data[0], ref2->linesize[0], tref2, 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, - ref2->data[0], ref2->linesize[0], + ref2->data[0], ref2->linesize[0], tref2, row << 3, (col << 3) + 4, &b->mv[1][1], 4, 8, w, h); } } else { @@ -1283,34 +1302,34 @@ static int inter_recon(AVCodecContext *avctx) // 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, - ref1->data[0], ref1->linesize[0], + ref1->data[0], ref1->linesize[0], tref1, 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, - ref1->data[0], ref1->linesize[0], + ref1->data[0], ref1->linesize[0], tref1, 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, - ref1->data[0], ref1->linesize[0], + ref1->data[0], ref1->linesize[0], tref1, (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, - ref1->data[0], ref1->linesize[0], + ref1->data[0], ref1->linesize[0], tref1, (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, - ref2->data[0], ref2->linesize[0], + ref2->data[0], ref2->linesize[0], tref2, 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, - ref2->data[0], ref2->linesize[0], + ref2->data[0], ref2->linesize[0], tref2, 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, - ref2->data[0], ref2->linesize[0], + ref2->data[0], ref2->linesize[0], tref2, (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, - ref2->data[0], ref2->linesize[0], + ref2->data[0], ref2->linesize[0], tref2, (row << 3) + 4, (col << 3) + 4, &b->mv[3][1], 4, 4, w, h); } } @@ -1320,12 +1339,12 @@ static int inter_recon(AVCodecContext *avctx) int bh = bwh_tab[0][b->bs][1] * 4; mc_luma_dir(s, s->dsp.mc[bwl][b->filter][0], b->dst[0], ls_y, - ref1->data[0], ref1->linesize[0], + ref1->data[0], ref1->linesize[0], tref1, 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, - ref2->data[0], ref2->linesize[0], + ref2->data[0], ref2->linesize[0], tref2, row << 3, col << 3, &b->mv[0][1], bw, bh, w, h); } @@ -1349,7 +1368,7 @@ static int inter_recon(AVCodecContext *avctx) mc_chroma_dir(s, s->dsp.mc[bwl][b->filter][0], b->dst[1], b->dst[2], ls_uv, ref1->data[1], ref1->linesize[1], - ref1->data[2], ref1->linesize[2], + ref1->data[2], ref1->linesize[2], tref1, row << 2, col << 2, &mvuv, bw, bh, w, h); if (b->comp) { @@ -1364,7 +1383,7 @@ static int inter_recon(AVCodecContext *avctx) mc_chroma_dir(s, s->dsp.mc[bwl][b->filter][1], b->dst[1], b->dst[2], ls_uv, ref2->data[1], ref2->linesize[1], - ref2->data[2], ref2->linesize[2], + ref2->data[2], ref2->linesize[2], tref2, row << 2, col << 2, &mvuv, bw, bh, w, h); } } @@ -1571,21 +1590,37 @@ int ff_vp9_decode_block(AVCodecContext *avctx, int row, int col, s->max_mv.x = 128 + (s->cols - col - w4) * 64; s->max_mv.y = 128 + (s->rows - row - h4) * 64; - b->bs = bs; - decode_mode(s, b); - b->uvtx = b->tx - (w4 * 2 == (1 << b->tx) || h4 * 2 == (1 << b->tx)); + if (s->pass < 2) { + b->bs = bs; + b->bl = bl; + b->bp = bp; + decode_mode(s, b); + b->uvtx = b->tx - (w4 * 2 == (1 << b->tx) || h4 * 2 == (1 << b->tx)); - if (!b->skip) { - if ((ret = decode_coeffs(avctx)) < 0) - return ret; - } else { - int pl; + if (!b->skip) { + if ((ret = decode_coeffs(avctx)) < 0) + return ret; + } else { + int pl; - memset(&s->above_y_nnz_ctx[col * 2], 0, w4 * 2); - memset(&s->left_y_nnz_ctx[(row & 7) << 1], 0, h4 * 2); - for (pl = 0; pl < 2; pl++) { - memset(&s->above_uv_nnz_ctx[pl][col], 0, w4); - memset(&s->left_uv_nnz_ctx[pl][row & 7], 0, h4); + memset(&s->above_y_nnz_ctx[col * 2], 0, w4 * 2); + memset(&s->left_y_nnz_ctx[(row & 7) << 1], 0, h4 * 2); + for (pl = 0; pl < 2; pl++) { + memset(&s->above_uv_nnz_ctx[pl][col], 0, w4); + memset(&s->left_uv_nnz_ctx[pl][row & 7], 0, h4); + } + } + + if (s->pass == 1) { + s->b++; + s->block += w4 * h4 * 64; + s->uvblock[0] += w4 * h4 * 16; + s->uvblock[1] += w4 * h4 * 16; + s->eob += w4 * h4 * 4; + s->uveob[0] += w4 * h4; + s->uveob[1] += w4 * h4; + + return 0; } } @@ -1690,5 +1725,15 @@ int ff_vp9_decode_block(AVCodecContext *avctx, int row, int col, } } + if (s->pass == 2) { + s->b++; + s->block += w4 * h4 * 64; + s->uvblock[0] += w4 * h4 * 16; + s->uvblock[1] += w4 * h4 * 16; + s->eob += w4 * h4 * 4; + s->uveob[0] += w4 * h4; + s->uveob[1] += w4 * h4; + } + return 0; } diff --git a/libavcodec/vp9mvs.c b/libavcodec/vp9mvs.c index 5edcb19039..dde0e84355 100644 --- a/libavcodec/vp9mvs.c +++ b/libavcodec/vp9mvs.c @@ -164,6 +164,9 @@ static void find_ref_mvs(VP9Context *s, if (s->use_last_frame_mvs) { VP9MVRefPair *mv = &s->frames[LAST_FRAME].mv[row * s->sb_cols * 8 + col]; + if (!s->last_uses_2pass) + ff_thread_await_progress(&s->frames[LAST_FRAME].tf, row >> 3, 0); + if (mv->ref[0] == ref) RETURN_MV(mv->mv[0]); else if (mv->ref[1] == ref) @@ -205,6 +208,7 @@ static void find_ref_mvs(VP9Context *s, if (s->use_last_frame_mvs) { VP9MVRefPair *mv = &s->frames[LAST_FRAME].mv[row * s->sb_cols * 8 + col]; + // no need to await_progress, because we already did that above if (mv->ref[0] != ref && mv->ref[0] >= 0) RETURN_SCALE_MV(mv->mv[0], s->signbias[mv->ref[0]] != s->signbias[ref]); From f2143c57b6a61fef382f3128138d8558a9bdecee Mon Sep 17 00:00:00 2001 From: Anton Khirnov Date: Sat, 6 Aug 2016 10:07:53 +0200 Subject: [PATCH 4/4] vp9: reindent after last commit --- libavcodec/vp9.c | 234 +++++++++++++++++++++++------------------------ 1 file changed, 117 insertions(+), 117 deletions(-) diff --git a/libavcodec/vp9.c b/libavcodec/vp9.c index 7989ca8d14..32d995f4a1 100644 --- a/libavcodec/vp9.c +++ b/libavcodec/vp9.c @@ -1280,125 +1280,125 @@ static int vp9_decode_frame(AVCodecContext *avctx, AVFrame *frame, do { ptrdiff_t yoff = 0, uvoff = 0; - s->b = s->b_base; - s->block = s->block_base; - s->uvblock[0] = s->uvblock_base[0]; - s->uvblock[1] = s->uvblock_base[1]; - s->eob = s->eob_base; - s->uveob[0] = s->uveob_base[0]; - s->uveob[1] = s->uveob_base[1]; + s->b = s->b_base; + s->block = s->block_base; + s->uvblock[0] = s->uvblock_base[0]; + s->uvblock[1] = s->uvblock_base[1]; + s->eob = s->eob_base; + s->uveob[0] = s->uveob_base[0]; + s->uveob[1] = s->uveob_base[1]; - for (tile_row = 0; tile_row < s->tiling.tile_rows; tile_row++) { - set_tile_offset(&s->tiling.tile_row_start, &s->tiling.tile_row_end, - tile_row, s->tiling.log2_tile_rows, s->sb_rows); + for (tile_row = 0; tile_row < s->tiling.tile_rows; tile_row++) { + set_tile_offset(&s->tiling.tile_row_start, &s->tiling.tile_row_end, + tile_row, s->tiling.log2_tile_rows, s->sb_rows); - if (s->pass != 2) { - for (tile_col = 0; tile_col < s->tiling.tile_cols; tile_col++) { - int64_t tile_size; + if (s->pass != 2) { + for (tile_col = 0; tile_col < s->tiling.tile_cols; tile_col++) { + int64_t tile_size; - if (tile_col == s->tiling.tile_cols - 1 && - tile_row == s->tiling.tile_rows - 1) { - tile_size = size; - } else { - tile_size = AV_RB32(data); - data += 4; - size -= 4; - } - if (tile_size > size) { - ret = AVERROR_INVALIDDATA; - goto fail; - } - ff_vp56_init_range_decoder(&s->c_b[tile_col], data, tile_size); - if (vp56_rac_get_prob_branchy(&s->c_b[tile_col], 128)) { // marker bit - ret = AVERROR_INVALIDDATA; - goto fail; - } - data += tile_size; - size -= tile_size; - } - } - - for (row = s->tiling.tile_row_start; - row < s->tiling.tile_row_end; - row += 8, yoff += f->linesize[0] * 64, - uvoff += f->linesize[1] * 32) { - VP9Filter *lflvl = s->lflvl; - ptrdiff_t yoff2 = yoff, uvoff2 = uvoff; - - for (tile_col = 0; tile_col < s->tiling.tile_cols; tile_col++) { - set_tile_offset(&s->tiling.tile_col_start, - &s->tiling.tile_col_end, - tile_col, s->tiling.log2_tile_cols, s->sb_cols); - - memset(s->left_partition_ctx, 0, 8); - memset(s->left_skip_ctx, 0, 8); - if (s->keyframe || s->intraonly) - memset(s->left_mode_ctx, DC_PRED, 16); - else - memset(s->left_mode_ctx, NEARESTMV, 8); - memset(s->left_y_nnz_ctx, 0, 16); - memset(s->left_uv_nnz_ctx, 0, 16); - memset(s->left_segpred_ctx, 0, 8); - - memcpy(&s->c, &s->c_b[tile_col], sizeof(s->c)); - for (col = s->tiling.tile_col_start; - col < s->tiling.tile_col_end; - col += 8, yoff2 += 64, uvoff2 += 32, lflvl++) { - // FIXME integrate with lf code (i.e. zero after each - // use, similar to invtxfm coefficients, or similar) - if (s->pass != 1) - memset(lflvl->mask, 0, sizeof(lflvl->mask)); - - if (s->pass == 2) { - ret = decode_superblock_mem(avctx, row, col, lflvl, - yoff2, uvoff2, BL_64X64); + if (tile_col == s->tiling.tile_cols - 1 && + tile_row == s->tiling.tile_rows - 1) { + tile_size = size; } else { - ret = decode_subblock(avctx, row, col, lflvl, - yoff2, uvoff2, BL_64X64); + tile_size = AV_RB32(data); + data += 4; + size -= 4; } - if (ret < 0) + if (tile_size > size) { + ret = AVERROR_INVALIDDATA; goto fail; + } + ff_vp56_init_range_decoder(&s->c_b[tile_col], data, tile_size); + if (vp56_rac_get_prob_branchy(&s->c_b[tile_col], 128)) { // marker bit + ret = AVERROR_INVALIDDATA; + goto fail; + } + data += tile_size; + size -= tile_size; } - if (s->pass != 2) - memcpy(&s->c_b[tile_col], &s->c, sizeof(s->c)); } - if (s->pass == 1) - continue; + for (row = s->tiling.tile_row_start; + row < s->tiling.tile_row_end; + row += 8, yoff += f->linesize[0] * 64, + uvoff += f->linesize[1] * 32) { + VP9Filter *lflvl = s->lflvl; + ptrdiff_t yoff2 = yoff, uvoff2 = uvoff; - // backup pre-loopfilter reconstruction data for intra - // prediction of next row of sb64s - if (row + 8 < s->rows) { - memcpy(s->intra_pred_data[0], - f->data[0] + yoff + - 63 * f->linesize[0], - 8 * s->cols); - memcpy(s->intra_pred_data[1], - f->data[1] + uvoff + - 31 * f->linesize[1], - 4 * s->cols); - memcpy(s->intra_pred_data[2], - f->data[2] + uvoff + - 31 * f->linesize[2], - 4 * s->cols); + for (tile_col = 0; tile_col < s->tiling.tile_cols; tile_col++) { + set_tile_offset(&s->tiling.tile_col_start, + &s->tiling.tile_col_end, + tile_col, s->tiling.log2_tile_cols, s->sb_cols); + + memset(s->left_partition_ctx, 0, 8); + memset(s->left_skip_ctx, 0, 8); + if (s->keyframe || s->intraonly) + memset(s->left_mode_ctx, DC_PRED, 16); + else + memset(s->left_mode_ctx, NEARESTMV, 8); + memset(s->left_y_nnz_ctx, 0, 16); + memset(s->left_uv_nnz_ctx, 0, 16); + memset(s->left_segpred_ctx, 0, 8); + + memcpy(&s->c, &s->c_b[tile_col], sizeof(s->c)); + for (col = s->tiling.tile_col_start; + col < s->tiling.tile_col_end; + col += 8, yoff2 += 64, uvoff2 += 32, lflvl++) { + // FIXME integrate with lf code (i.e. zero after each + // use, similar to invtxfm coefficients, or similar) + if (s->pass != 1) + memset(lflvl->mask, 0, sizeof(lflvl->mask)); + + if (s->pass == 2) { + ret = decode_superblock_mem(avctx, row, col, lflvl, + yoff2, uvoff2, BL_64X64); + } else { + ret = decode_subblock(avctx, row, col, lflvl, + yoff2, uvoff2, BL_64X64); + } + if (ret < 0) + goto fail; + } + if (s->pass != 2) + memcpy(&s->c_b[tile_col], &s->c, sizeof(s->c)); + } + + if (s->pass == 1) + continue; + + // backup pre-loopfilter reconstruction data for intra + // prediction of next row of sb64s + if (row + 8 < s->rows) { + memcpy(s->intra_pred_data[0], + f->data[0] + yoff + + 63 * f->linesize[0], + 8 * s->cols); + memcpy(s->intra_pred_data[1], + f->data[1] + uvoff + + 31 * f->linesize[1], + 4 * s->cols); + memcpy(s->intra_pred_data[2], + f->data[2] + uvoff + + 31 * f->linesize[2], + 4 * s->cols); + } + + // loopfilter one row + if (s->filter.level) { + yoff2 = yoff; + uvoff2 = uvoff; + lflvl = s->lflvl; + for (col = 0; col < s->cols; + col += 8, yoff2 += 64, uvoff2 += 32, lflvl++) + loopfilter_subblock(avctx, lflvl, row, col, yoff2, uvoff2); + } + + // FIXME maybe we can make this more finegrained by running the + // loopfilter per-block instead of after each sbrow + // In fact that would also make intra pred left preparation easier? + ff_thread_report_progress(&s->frames[CUR_FRAME].tf, row >> 3, 0); } - - // loopfilter one row - if (s->filter.level) { - yoff2 = yoff; - uvoff2 = uvoff; - lflvl = s->lflvl; - for (col = 0; col < s->cols; - col += 8, yoff2 += 64, uvoff2 += 32, lflvl++) - loopfilter_subblock(avctx, lflvl, row, col, yoff2, uvoff2); - } - - // FIXME maybe we can make this more finegrained by running the - // loopfilter per-block instead of after each sbrow - // In fact that would also make intra pred left preparation easier? - ff_thread_report_progress(&s->frames[CUR_FRAME].tf, row >> 3, 0); } - } if (s->pass < 2 && s->refreshctx && !s->parallelmode) { ff_vp9_adapt_probs(s); @@ -1584,16 +1584,16 @@ static int vp9_decode_update_thread_context(AVCodecContext *dst, const AVCodecCo } AVCodec ff_vp9_decoder = { - .name = "vp9", - .long_name = NULL_IF_CONFIG_SMALL("Google VP9"), - .type = AVMEDIA_TYPE_VIDEO, - .id = AV_CODEC_ID_VP9, - .priv_data_size = sizeof(VP9Context), - .init = vp9_decode_init, - .decode = vp9_decode_packet, - .flush = vp9_decode_flush, - .close = vp9_decode_free, - .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS, + .name = "vp9", + .long_name = NULL_IF_CONFIG_SMALL("Google VP9"), + .type = AVMEDIA_TYPE_VIDEO, + .id = AV_CODEC_ID_VP9, + .priv_data_size = sizeof(VP9Context), + .init = vp9_decode_init, + .decode = vp9_decode_packet, + .flush = vp9_decode_flush, + .close = vp9_decode_free, + .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS, .init_thread_copy = vp9_decode_init, .update_thread_context = vp9_decode_update_thread_context, };