[04/14] dump_extradata_bsf: convert to the new API

Message ID 1457079353-1877-5-git-send-email-anton@khirnov.net
State New
Headers show

Commit Message

Anton Khirnov March 4, 2016, 8:15 a.m.
---
 libavcodec/allcodecs.c          |  1 -
 libavcodec/bitstream_filters.c  |  4 ++
 libavcodec/dump_extradata_bsf.c | 94 ++++++++++++++++++++++++++++++-----------
 3 files changed, 74 insertions(+), 25 deletions(-)

Comments

Luca Barbato March 7, 2016, 8:02 a.m. | #1
On 04/03/16 09:15, Anton Khirnov wrote:
> ---
>  libavcodec/allcodecs.c          |  1 -
>  libavcodec/bitstream_filters.c  |  4 ++
>  libavcodec/dump_extradata_bsf.c | 94 ++++++++++++++++++++++++++++++-----------
>  3 files changed, 74 insertions(+), 25 deletions(-)


Ok.

Patch

diff --git a/libavcodec/allcodecs.c b/libavcodec/allcodecs.c
index 35d14c3..ac3e230 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(DUMP_EXTRADATA,        dump_extradata);
     REGISTER_BSF(H264_MP4TOANNEXB,      h264_mp4toannexb);
     REGISTER_BSF(HEVC_MP4TOANNEXB,      hevc_mp4toannexb);
     REGISTER_BSF(IMX_DUMP_HEADER,       imx_dump_header);
diff --git a/libavcodec/bitstream_filters.c b/libavcodec/bitstream_filters.c
index a1277dd..c8d4d50 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;
 
 static const AVBitStreamFilter *bitstream_filters[] = {
 #if CONFIG_AAC_ADTSTOASC_BSF
@@ -34,6 +35,9 @@  static const AVBitStreamFilter *bitstream_filters[] = {
 #if CONFIG_CHOMP_BSF
     &ff_chomp_bsf,
 #endif
+#if CONFIG_DUMP_EXTRADATA_BSF
+    &ff_dump_extradata_bsf,
+#endif
     NULL,
 };
 
diff --git a/libavcodec/dump_extradata_bsf.c b/libavcodec/dump_extradata_bsf.c
index 3f54899..c960d6a 100644
--- a/libavcodec/dump_extradata_bsf.c
+++ b/libavcodec/dump_extradata_bsf.c
@@ -21,35 +21,81 @@ 
 #include <string.h>
 
 #include "avcodec.h"
+#include "bsf.h"
+
+#include "libavutil/log.h"
 #include "libavutil/mem.h"
+#include "libavutil/opt.h"
+
+enum DumpFreq {
+    DUMP_FREQ_KEYFRAME,
+    DUMP_FREQ_ALL,
+};
+
+typedef struct DumpExtradataContext {
+    const AVClass *class;
+    int freq;
+} DumpExtradataContext;
+
+static int dump_extradata(AVBSFContext *ctx, AVPacket *out)
+{
+    DumpExtradataContext *s = ctx->priv_data;
+    AVPacket *in;
+    int ret = 0;
+
+    ret = ff_bsf_get_packet(ctx, &in);
+    if (ret < 0)
+        return ret;
+
+    if (ctx->par_in->extradata &&
+        (s->freq == DUMP_FREQ_ALL ||
+         (s->freq == DUMP_FREQ_KEYFRAME && in->flags & AV_PKT_FLAG_KEY))) {
+        if (in->size >= INT_MAX - ctx->par_in->extradata_size) {
+            ret = AVERROR(ERANGE);
+            goto fail;
+        }
 
+        ret = av_new_packet(out, in->size + ctx->par_in->extradata_size);
+        if (ret < 0)
+            goto fail;
 
-static int dump_extradata(AVBitStreamFilterContext *bsfc, AVCodecContext *avctx, const char *args,
-                     uint8_t **poutbuf, int *poutbuf_size,
-                     const uint8_t *buf, int buf_size, int keyframe){
-    int cmd= args ? *args : 0;
-    /* cast to avoid warning about discarding qualifiers */
-    if(avctx->extradata){
-        if(  (keyframe && (avctx->flags2 & AV_CODEC_FLAG2_LOCAL_HEADER) && cmd == 'a')
-           ||(keyframe && (cmd=='k' || !cmd))
-           ||(cmd=='e')
-            /*||(? && (s->flags & PARSER_FLAG_DUMP_EXTRADATA_AT_BEGIN)*/){
-            int size= buf_size + avctx->extradata_size;
-            *poutbuf_size= size;
-            *poutbuf= av_malloc(size + AV_INPUT_BUFFER_PADDING_SIZE);
-            if (!*poutbuf)
-                return AVERROR(ENOMEM);
-
-            memcpy(*poutbuf, avctx->extradata, avctx->extradata_size);
-            memcpy((*poutbuf) + avctx->extradata_size, buf, buf_size + AV_INPUT_BUFFER_PADDING_SIZE);
-            return 1;
+        ret = av_packet_copy_props(out, in);
+        if (ret < 0) {
+            av_packet_unref(out);
+            goto fail;
         }
+
+        memcpy(out->data, ctx->par_in->extradata, ctx->par_in->extradata_size);
+        memcpy(out->data + ctx->par_in->extradata_size, in->data, in->size);
+    } else {
+        av_packet_move_ref(out, in);
     }
-    return 0;
+
+fail:
+    av_packet_free(&in);
+
+    return ret;
 }
 
-AVBitStreamFilter ff_dump_extradata_bsf={
-    "dump_extra",
-    0,
-    dump_extradata,
+#define OFFSET(x) offsetof(DumpExtradataContext, x)
+static const AVOption options[] = {
+    { "freq", "When do dump extradata", OFFSET(freq), AV_OPT_TYPE_INT,
+        { .i64 = DUMP_FREQ_KEYFRAME }, DUMP_FREQ_KEYFRAME, DUMP_FREQ_ALL, 0, "freq" },
+        { "keyframe", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = DUMP_FREQ_KEYFRAME }, .unit = "freq" },
+        { "all",      NULL, 0, AV_OPT_TYPE_CONST, { .i64 = DUMP_FREQ_ALL      }, .unit = "freq" },
+    { NULL },
+};
+
+static const AVClass dump_extradata_class = {
+    .class_name = "dump_extradata bsf",
+    .item_name  = av_default_item_name,
+    .option     = options,
+    .version    = LIBAVUTIL_VERSION_MAJOR,
+};
+
+const AVBitStreamFilter ff_dump_extradata_bsf = {
+    .name           = "dump_extra",
+    .priv_data_size = sizeof(DumpExtradataContext),
+    .priv_class     = &dump_extradata_class,
+    .filter         = dump_extradata,
 };