diff options
author | Derek Buitenhuis <derek.buitenhuis@gmail.com> | 2016-04-17 18:47:25 +0100 |
---|---|---|
committer | Derek Buitenhuis <derek.buitenhuis@gmail.com> | 2016-04-17 18:47:40 +0100 |
commit | af9cac1be1750ecc0e12c6788a3aeed1f1a778be (patch) | |
tree | 2e34226dbd4010774f6ffef448aab4b7d4f88544 /libavcodec/hevc_mp4toannexb_bsf.c | |
parent | 6d160afab2fa8d3bfb216fee96d3537ffc9e86e8 (diff) | |
parent | 33d18982fa03feb061c8f744a4f0a9175c1f63ab (diff) | |
download | ffmpeg-af9cac1be1750ecc0e12c6788a3aeed1f1a778be.tar.gz |
Merge commit '33d18982fa03feb061c8f744a4f0a9175c1f63ab'
* commit '33d18982fa03feb061c8f744a4f0a9175c1f63ab':
lavc: add a new bitstream filtering API
Conversions-by: Hendrik Leppkes <h.leppkes@gmail.com>
Conversions-by: Derek Buitenguis <derek.buitenhuis@gmail.com>
Merged-by: Derek Buitenhuis <derek.buitenhuis@gmail.com>
Diffstat (limited to 'libavcodec/hevc_mp4toannexb_bsf.c')
-rw-r--r-- | libavcodec/hevc_mp4toannexb_bsf.c | 151 |
1 files changed, 67 insertions, 84 deletions
diff --git a/libavcodec/hevc_mp4toannexb_bsf.c b/libavcodec/hevc_mp4toannexb_bsf.c index d6feb998f8..5cc642b0d3 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,23 +34,9 @@ typedef struct HEVCBSFContext { uint8_t length_size; int extradata_parsed; - - int logged_nonmp4_warning; - - /* When private_spspps is zero then spspps_buf points to global extradata - and bsf does replace a global extradata to own-allocated version (default - behaviour). - When private_spspps is non-zero the bsf uses a private version of spspps buf. - This mode necessary when bsf uses in decoder, else bsf has issues after - decoder re-initialization. Use the "private_spspps_buf" argument to - activate this mode. - */ - int private_spspps; - uint8_t *spspps_buf; - uint32_t spspps_size; } HEVCBSFContext; -static int hevc_extradata_to_annexb(HEVCBSFContext* ctx, AVCodecContext *avctx) +static int hevc_extradata_to_annexb(AVBSFContext *ctx) { GetByteContext gb; int length_size, num_arrays, i, j; @@ -58,7 +45,7 @@ static int hevc_extradata_to_annexb(HEVCBSFContext* ctx, 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; @@ -70,7 +57,7 @@ static int hevc_extradata_to_annexb(HEVCBSFContext* ctx, 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; @@ -94,16 +81,12 @@ static int hevc_extradata_to_annexb(HEVCBSFContext* ctx, AVCodecContext *avctx) } } - if (!ctx->private_spspps) { - av_freep(&avctx->extradata); - avctx->extradata = new_extradata; - avctx->extradata_size = new_extradata_size; - } - ctx->spspps_buf = new_extradata; - ctx->spspps_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: @@ -111,54 +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 *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 *ctx = bsfc->priv_data; + 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 = (uint8_t *)buf; - *poutbuf_size = buf_size; - return 0; - } - if (args && strstr(args, "private_spspps_buf")) - ctx->private_spspps = 1; + ret = ff_bsf_get_packet(ctx, &in); + if (ret < 0) + return ret; - ret = hevc_extradata_to_annexb(ctx, 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; @@ -166,47 +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 * ctx->spspps_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, ctx->spspps_buf, 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; } -static void hevc_mp4toannexb_close(AVBitStreamFilterContext *bsfc) -{ - HEVCBSFContext *ctx = bsfc->priv_data; - if (ctx->private_spspps) - av_freep(&ctx->spspps_buf); -} +static const enum AVCodecID codec_ids[] = { + AV_CODEC_ID_HEVC, AV_CODEC_ID_NONE, +}; -AVBitStreamFilter ff_hevc_mp4toannexb_bsf = { - "hevc_mp4toannexb", - sizeof(HEVCBSFContext), - hevc_mp4toannexb_filter, - hevc_mp4toannexb_close, +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, }; |