[07/15] hevc_mp4toannexb_bsf: convert to the new API

Message ID 1456412755-9220-8-git-send-email-anton@khirnov.net
State New
Headers show

Commit Message

Anton Khirnov Feb. 25, 2016, 3:05 p.m.
---
 libavcodec/allcodecs.c            |   1 -
 libavcodec/bitstream_filters.c    |   4 ++
 libavcodec/hevc_mp4toannexb_bsf.c | 127 ++++++++++++++++++++------------------
 3 files changed, 72 insertions(+), 60 deletions(-)

Patch

diff --git a/libavcodec/allcodecs.c b/libavcodec/allcodecs.c
index 8ed7955..da8ce31 100644
--- a/libavcodec/allcodecs.c
+++ b/libavcodec/allcodecs.c
@@ -538,7 +538,6 @@  void avcodec_register_all(void)
     REGISTER_PARSER(VP8,                vp8);
 
     /* bitstream filters */
-    REGISTER_BSF(HEVC_MP4TOANNEXB,      hevc_mp4toannexb);
     REGISTER_BSF(IMX_DUMP_HEADER,       imx_dump_header);
     REGISTER_BSF(MJPEG2JPEG,            mjpeg2jpeg);
     REGISTER_BSF(MJPEGA_DUMP_HEADER,    mjpega_dump_header);
diff --git a/libavcodec/bitstream_filters.c b/libavcodec/bitstream_filters.c
index bebaae8..9673ddb 100644
--- a/libavcodec/bitstream_filters.c
+++ b/libavcodec/bitstream_filters.c
@@ -26,6 +26,7 @@  extern const AVBitStreamFilter ff_aac_adtstoasc_bsf;
 extern const AVBitStreamFilter ff_chomp_bsf;
 extern const AVBitStreamFilter ff_dump_extradata_bsf;
 extern const AVBitStreamFilter ff_h264_mp4toannexb_bsf;
+extern const AVBitStreamFilter ff_hevc_mp4toannexb_bsf;
 
 static const AVBitStreamFilter *bitstream_filters[] = {
 #if CONFIG_AAC_ADTSTOASC_BSF
@@ -40,6 +41,9 @@  static const AVBitStreamFilter *bitstream_filters[] = {
 #if CONFIG_H264_MP4TOANNEXB_BSF
     &ff_h264_mp4toannexb_bsf,
 #endif
+#if CONFIG_HEVC_MP4TOANNEXB_BSF
+    &ff_hevc_mp4toannexb_bsf,
+#endif
     NULL,
 };
 
diff --git a/libavcodec/hevc_mp4toannexb_bsf.c b/libavcodec/hevc_mp4toannexb_bsf.c
index dfecf5e..8d7ac58 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,
 };