diff options
author | Anton Khirnov <anton@khirnov.net> | 2013-11-23 11:43:13 +0100 |
---|---|---|
committer | Anton Khirnov <anton@khirnov.net> | 2016-03-20 08:15:01 +0100 |
commit | 33d18982fa03feb061c8f744a4f0a9175c1f63ab (patch) | |
tree | 194f6383561b7acf3315eb7982c13aa95ff5ef67 /libavcodec/hevc_mp4toannexb_bsf.c | |
parent | a2d1922bde8db2cdac95051918fe81ae18c0376b (diff) | |
download | ffmpeg-33d18982fa03feb061c8f744a4f0a9175c1f63ab.tar.gz |
lavc: add a new bitstream filtering API
Deprecate the current bitstream filtering API.
Diffstat (limited to 'libavcodec/hevc_mp4toannexb_bsf.c')
-rw-r--r-- | libavcodec/hevc_mp4toannexb_bsf.c | 127 |
1 files changed, 68 insertions, 59 deletions
diff --git a/libavcodec/hevc_mp4toannexb_bsf.c b/libavcodec/hevc_mp4toannexb_bsf.c index dfecf5e99f..8d7ac58f19 100644 --- a/libavcodec/hevc_mp4toannexb_bsf.c +++ b/libavcodec/hevc_mp4toannexb_bsf.c @@ -25,6 +25,7 @@ #include "libavutil/mem.h" #include "avcodec.h" +#include "bsf.h" #include "bytestream.h" #include "hevc.h" @@ -33,11 +34,9 @@ typedef struct HEVCBSFContext { uint8_t length_size; int extradata_parsed; - - int logged_nonmp4_warning; } HEVCBSFContext; -static int hevc_extradata_to_annexb(AVCodecContext *avctx) +static int hevc_extradata_to_annexb(AVBSFContext *ctx) { GetByteContext gb; int length_size, num_arrays, i, j; @@ -46,7 +45,7 @@ static int hevc_extradata_to_annexb(AVCodecContext *avctx) uint8_t *new_extradata = NULL; size_t new_extradata_size = 0;; - bytestream2_init(&gb, avctx->extradata, avctx->extradata_size); + bytestream2_init(&gb, ctx->par_in->extradata, ctx->par_in->extradata_size); bytestream2_skip(&gb, 21); length_size = (bytestream2_get_byte(&gb) & 3) + 1; @@ -58,7 +57,7 @@ static int hevc_extradata_to_annexb(AVCodecContext *avctx) if (!(type == NAL_VPS || type == NAL_SPS || type == NAL_PPS || type == NAL_SEI_PREFIX || type == NAL_SEI_SUFFIX)) { - av_log(avctx, AV_LOG_ERROR, "Invalid NAL unit type in extradata: %d\n", + av_log(ctx, AV_LOG_ERROR, "Invalid NAL unit type in extradata: %d\n", type); ret = AVERROR_INVALIDDATA; goto fail; @@ -82,12 +81,12 @@ static int hevc_extradata_to_annexb(AVCodecContext *avctx) } } - av_freep(&avctx->extradata); - avctx->extradata = new_extradata; - avctx->extradata_size = new_extradata_size; + av_freep(&ctx->par_out->extradata); + ctx->par_out->extradata = new_extradata; + ctx->par_out->extradata_size = new_extradata_size; if (!new_extradata_size) - av_log(avctx, AV_LOG_WARNING, "No parameter sets in the extradata\n"); + av_log(ctx, AV_LOG_WARNING, "No parameter sets in the extradata\n"); return length_size; fail: @@ -95,52 +94,54 @@ fail: return ret; } -static int hevc_mp4toannexb_filter(AVBitStreamFilterContext *bsfc, - AVCodecContext *avctx, const char *args, - uint8_t **poutbuf, int *poutbuf_size, - const uint8_t *buf, int buf_size, - int keyframe) +static int hevc_mp4toannexb_init(AVBSFContext *ctx) { - HEVCBSFContext *ctx = bsfc->priv_data; + HEVCBSFContext *s = ctx->priv_data; + int ret; + + if (ctx->par_in->extradata_size < MIN_HEVCC_LENGTH || + AV_RB24(ctx->par_in->extradata) == 1 || + AV_RB32(ctx->par_in->extradata) == 1) { + av_log(ctx, AV_LOG_VERBOSE, + "The input looks like it is Annex B already\n"); + } else { + ret = hevc_extradata_to_annexb(ctx); + if (ret < 0) + return ret; + s->length_size = ret; + s->extradata_parsed = 1; + } + + return 0; +} + +static int hevc_mp4toannexb_filter(AVBSFContext *ctx, AVPacket *out) +{ + HEVCBSFContext *s = ctx->priv_data; + AVPacket *in; GetByteContext gb; - uint8_t *out = NULL; - size_t out_size = 0; int got_irap = 0; int i, ret = 0; - if (!ctx->extradata_parsed) { - if (avctx->extradata_size < MIN_HEVCC_LENGTH || - AV_RB24(avctx->extradata) == 1 || - AV_RB32(avctx->extradata) == 1) { - if (!ctx->logged_nonmp4_warning) { - av_log(avctx, AV_LOG_VERBOSE, - "The input looks like it is Annex B already\n"); - ctx->logged_nonmp4_warning = 1; - } - *poutbuf = buf; - *poutbuf_size = buf_size; - return 0; - } + ret = ff_bsf_get_packet(ctx, &in); + if (ret < 0) + return ret; - ret = hevc_extradata_to_annexb(avctx); - if (ret < 0) - return ret; - ctx->length_size = ret; - ctx->extradata_parsed = 1; + if (!s->extradata_parsed) { + av_packet_move_ref(out, in); + av_packet_free(&in); + return 0; } - *poutbuf_size = 0; - *poutbuf = NULL; - - bytestream2_init(&gb, buf, buf_size); + bytestream2_init(&gb, in->data, in->size); while (bytestream2_get_bytes_left(&gb)) { uint32_t nalu_size = 0; int nalu_type; - int is_irap, add_extradata, extra_size; + int is_irap, add_extradata, extra_size, prev_size; - for (i = 0; i < ctx->length_size; i++) + for (i = 0; i < s->length_size; i++) nalu_size = (nalu_size << 8) | bytestream2_get_byte(&gb); nalu_type = (bytestream2_peek_byte(&gb) >> 1) & 0x3f; @@ -148,39 +149,47 @@ static int hevc_mp4toannexb_filter(AVBitStreamFilterContext *bsfc, /* prepend extradata to IRAP frames */ is_irap = nalu_type >= 16 && nalu_type <= 23; add_extradata = is_irap && !got_irap; - extra_size = add_extradata * avctx->extradata_size; + extra_size = add_extradata * ctx->par_out->extradata_size; got_irap |= is_irap; - if (SIZE_MAX - out_size < 4 || - SIZE_MAX - out_size - 4 < nalu_size || - SIZE_MAX - out_size - 4 - nalu_size < extra_size) { + if (SIZE_MAX - nalu_size < 4 || + SIZE_MAX - 4 - nalu_size < extra_size) { ret = AVERROR_INVALIDDATA; goto fail; } - ret = av_reallocp(&out, out_size + 4 + nalu_size + extra_size); + prev_size = out->size; + + ret = av_grow_packet(out, 4 + nalu_size + extra_size); if (ret < 0) goto fail; if (add_extradata) - memcpy(out + out_size, avctx->extradata, extra_size); - AV_WB32(out + out_size + extra_size, 1); - bytestream2_get_buffer(&gb, out + out_size + 4 + extra_size, nalu_size); - out_size += 4 + nalu_size + extra_size; + memcpy(out->data + prev_size, ctx->par_out->extradata, extra_size); + AV_WB32(out->data + prev_size + extra_size, 1); + bytestream2_get_buffer(&gb, out->data + prev_size + 4 + extra_size, nalu_size); } - *poutbuf = out; - *poutbuf_size = out_size; - - return 1; + ret = av_packet_copy_props(out, in); + if (ret < 0) + goto fail; fail: - av_freep(&out); + if (ret < 0) + av_packet_unref(out); + av_packet_free(&in); + return ret; } -AVBitStreamFilter ff_hevc_mp4toannexb_bsf = { - "hevc_mp4toannexb", - sizeof(HEVCBSFContext), - hevc_mp4toannexb_filter, +static const enum AVCodecID codec_ids[] = { + AV_CODEC_ID_HEVC, AV_CODEC_ID_NONE, +}; + +const AVBitStreamFilter ff_hevc_mp4toannexb_bsf = { + .name = "hevc_mp4toannexb", + .priv_data_size = sizeof(HEVCBSFContext), + .init = hevc_mp4toannexb_init, + .filter = hevc_mp4toannexb_filter, + .codec_ids = codec_ids, }; |