From c4dc8cffcdcaae3d489ed991fa7b267fbaad4f90 Mon Sep 17 00:00:00 2001 From: James Almer Date: Tue, 30 Oct 2018 22:31:41 -0300 Subject: [PATCH] avcodec/vp9_parser: reimplement using CBS Signed-off-by: James Almer --- libavcodec/cbs_vp9.h | 1 + libavcodec/cbs_vp9_syntax_template.c | 1 + libavcodec/vp9_parser.c | 148 +++++++++++++++++++++------ 3 files changed, 117 insertions(+), 33 deletions(-) diff --git a/libavcodec/cbs_vp9.h b/libavcodec/cbs_vp9.h index 4c9b2f880d32e..beecd5bf7c9d9 100644 --- a/libavcodec/cbs_vp9.h +++ b/libavcodec/cbs_vp9.h @@ -187,6 +187,7 @@ typedef struct VP9ReferenceFrameState { int subsampling_x; // RefSubsamplingX int subsampling_y; // RefSubsamplingY int bit_depth; // RefBitDepth + int intra_only; } VP9ReferenceFrameState; typedef struct CodedBitstreamVP9Context { diff --git a/libavcodec/cbs_vp9_syntax_template.c b/libavcodec/cbs_vp9_syntax_template.c index 898cede329cfd..26de5d08a63ed 100644 --- a/libavcodec/cbs_vp9_syntax_template.c +++ b/libavcodec/cbs_vp9_syntax_template.c @@ -379,6 +379,7 @@ static int FUNC(uncompressed_header)(CodedBitstreamContext *ctx, RWContext *rw, .subsampling_x = vp9->subsampling_x, .subsampling_y = vp9->subsampling_y, .bit_depth = vp9->bit_depth, + .intra_only = current->intra_only, }; } } diff --git a/libavcodec/vp9_parser.c b/libavcodec/vp9_parser.c index c957a75667b57..6f6b6d9b85aa5 100644 --- a/libavcodec/vp9_parser.c +++ b/libavcodec/vp9_parser.c @@ -1,8 +1,7 @@ /* - * VP9 compatible video decoder + * VP9 parser * - * Copyright (C) 2013 Ronald S. Bultje - * Copyright (C) 2013 Clément Bœsch + * Copyright (C) 2018 James Almer * * This file is part of FFmpeg. * @@ -21,50 +20,133 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ -#include "libavutil/intreadwrite.h" -#include "libavcodec/get_bits.h" +#include "cbs.h" +#include "cbs_vp9.h" #include "parser.h" -static int parse(AVCodecParserContext *ctx, - AVCodecContext *avctx, - const uint8_t **out_data, int *out_size, - const uint8_t *data, int size) +typedef struct VP9ParseContext { + CodedBitstreamContext *cbc; + CodedBitstreamFragment temporal_unit; +} VP9ParseContext; + +static const enum AVPixelFormat pix_fmts_8bit[2][2] = { + { AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV440P }, + { AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV420P }, +}; +static const enum AVPixelFormat pix_fmts_10bit[2][2] = { + { AV_PIX_FMT_YUV444P10, AV_PIX_FMT_YUV440P }, + { AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV420P10 }, +}; +static const enum AVPixelFormat pix_fmts_12bit[2][2] = { + { AV_PIX_FMT_YUV444P12, AV_PIX_FMT_YUV440P }, + { AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV420P12 }, +}; + +static int vp9_parser_parse(AVCodecParserContext *ctx, + AVCodecContext *avctx, + const uint8_t **out_data, int *out_size, + const uint8_t *data, int size) { - GetBitContext gb; - int res, profile, keyframe; + VP9ParseContext *s = ctx->priv_data; + CodedBitstreamFragment *td = &s->temporal_unit; + CodedBitstreamVP9Context *vp9 = s->cbc->priv_data; + int ret; *out_data = data; *out_size = size; - if (!size || (res = init_get_bits8(&gb, data, size)) < 0) - return size; // parsers can't return errors - get_bits(&gb, 2); // frame marker - profile = get_bits1(&gb); - profile |= get_bits1(&gb) << 1; - if (profile == 3) profile += get_bits1(&gb); - if (profile > 3) - return size; - - avctx->profile = profile; - - if (get_bits1(&gb)) { - keyframe = 0; - } else { - keyframe = !get_bits1(&gb); + ctx->key_frame = -1; + ctx->pict_type = AV_PICTURE_TYPE_NONE; + ctx->picture_structure = AV_PICTURE_STRUCTURE_UNKNOWN; + + s->cbc->log_ctx = avctx; + + ret = ff_cbs_read(s->cbc, td, data, size); + if (ret < 0) { + av_log(avctx, AV_LOG_ERROR, "Failed to parse temporal unit.\n"); + goto end; } + for (int i = 0; i < td->nb_units; i++) { + CodedBitstreamUnit *unit = &td->units[i]; + VP9RawFrame *frame = unit->content; + VP9RawFrameHeader *hdr = &frame->header; + int subsampling_x, subsampling_y, bit_depth, intra_only; + + if (hdr->show_existing_frame) { + VP9ReferenceFrameState *ref = &vp9->ref[hdr->frame_to_show_map_idx]; + + ctx->width = ref->frame_width; + ctx->height = ref->frame_height; + + subsampling_x = ref->subsampling_x; + subsampling_y = ref->subsampling_y; + bit_depth = ref->bit_depth; + intra_only = ref->intra_only; + + ctx->key_frame = 0; + } else if (!hdr->show_frame) { + continue; + } else { + ctx->width = vp9->frame_width; + ctx->height = vp9->frame_height; + + subsampling_x = vp9->subsampling_x; + subsampling_y = vp9->subsampling_y; + bit_depth = vp9->bit_depth; + intra_only = 0; + + ctx->key_frame = !hdr->frame_type; + } - if (!keyframe) { - ctx->pict_type = AV_PICTURE_TYPE_P; - ctx->key_frame = 0; - } else { - ctx->pict_type = AV_PICTURE_TYPE_I; - ctx->key_frame = 1; + avctx->profile = vp9->profile; + + ctx->pict_type = (ctx->key_frame || intra_only) ? AV_PICTURE_TYPE_I : AV_PICTURE_TYPE_P; + ctx->picture_structure = AV_PICTURE_STRUCTURE_FRAME; + + switch (bit_depth) { + case 8: + ctx->format = pix_fmts_8bit [subsampling_x][subsampling_y]; + break; + case 10: + ctx->format = pix_fmts_10bit[subsampling_x][subsampling_y]; + break; + case 12: + ctx->format = pix_fmts_12bit[subsampling_x][subsampling_y]; + break; + } } +end: + ff_cbs_fragment_uninit(s->cbc, td); + + s->cbc->log_ctx = NULL; + return size; } +static av_cold int vp9_parser_init(AVCodecParserContext *ctx) +{ + VP9ParseContext *s = ctx->priv_data; + int ret; + + ret = ff_cbs_init(&s->cbc, AV_CODEC_ID_VP9, NULL); + if (ret < 0) + return ret; + + return 0; +} + +static void vp9_parser_close(AVCodecParserContext *ctx) +{ + VP9ParseContext *s = ctx->priv_data; + + ff_cbs_close(&s->cbc); +} + AVCodecParser ff_vp9_parser = { .codec_ids = { AV_CODEC_ID_VP9 }, - .parser_parse = parse, + .priv_data_size = sizeof(VP9ParseContext), + .parser_init = vp9_parser_init, + .parser_close = vp9_parser_close, + .parser_parse = vp9_parser_parse, };