[FFmpeg-devel] [PATCH v2 12/13] av1_metadata_bsf: Use common cbs bsf implementation

Mark Thompson sw at jkqxz.net
Tue Jan 19 00:44:54 EET 2021


---
 libavcodec/av1_metadata_bsf.c | 176 ++++------------------------------
 1 file changed, 21 insertions(+), 155 deletions(-)

diff --git a/libavcodec/av1_metadata_bsf.c b/libavcodec/av1_metadata_bsf.c
index 3158ba995b..6114dd6b31 100644
--- a/libavcodec/av1_metadata_bsf.c
+++ b/libavcodec/av1_metadata_bsf.c
@@ -20,22 +20,12 @@
 #include "libavutil/opt.h"
 
 #include "bsf.h"
-#include "bsf_internal.h"
 #include "cbs.h"
+#include "cbs_bsf.h"
 #include "cbs_av1.h"
 
-enum {
-    PASS,
-    INSERT,
-    REMOVE,
-};
-
 typedef struct AV1MetadataContext {
-    const AVClass *class;
-
-    CodedBitstreamContext *input;
-    CodedBitstreamContext *output;
-    CodedBitstreamFragment access_unit;
+    CBSBSFContext common;
 
     int td;
 
@@ -113,91 +103,27 @@ static int av1_metadata_update_sequence_header(AVBSFContext *bsf,
     return 0;
 }
 
-static int av1_metadata_update_side_data(AVBSFContext *bsf, AVPacket *pkt)
+static int av1_metadata_update_fragment(AVBSFContext *bsf, AVPacket *pkt,
+                                        CodedBitstreamFragment *frag)
 {
     AV1MetadataContext *ctx = bsf->priv_data;
-    CodedBitstreamFragment *frag = &ctx->access_unit;
-    uint8_t *side_data;
-    int side_data_size;
-    int err, i;
-
-    side_data = av_packet_get_side_data(pkt, AV_PKT_DATA_NEW_EXTRADATA,
-                                        &side_data_size);
-    if (!side_data_size)
-        return 0;
-
-    err = ff_cbs_read(ctx->input, frag, side_data, side_data_size);
-    if (err < 0) {
-        av_log(bsf, AV_LOG_ERROR, "Failed to read extradata from packet side data.\n");
-        return err;
-    }
-
-    for (i = 0; i < frag->nb_units; i++) {
-        if (frag->units[i].type == AV1_OBU_SEQUENCE_HEADER) {
-            AV1RawOBU *obu = frag->units[i].content;
-            err = av1_metadata_update_sequence_header(bsf, &obu->obu.sequence_header);
-            if (err < 0)
-                return err;
-        }
-    }
-
-    err = ff_cbs_write_fragment_data(ctx->output, frag);
-    if (err < 0) {
-        av_log(bsf, AV_LOG_ERROR, "Failed to write extradata into packet side data.\n");
-        return err;
-    }
-
-    side_data = av_packet_new_side_data(pkt, AV_PKT_DATA_NEW_EXTRADATA, frag->data_size);
-    if (!side_data)
-        return AVERROR(ENOMEM);
-    memcpy(side_data, frag->data, frag->data_size);
-
-    ff_cbs_fragment_reset(frag);
-
-    return 0;
-}
-
-static int av1_metadata_filter(AVBSFContext *bsf, AVPacket *pkt)
-{
-    AV1MetadataContext *ctx = bsf->priv_data;
-    CodedBitstreamFragment *frag = &ctx->access_unit;
     AV1RawOBU td, *obu;
     int err, i;
 
-    err = ff_bsf_get_packet_ref(bsf, pkt);
-    if (err < 0)
-        return err;
-
-    err = av1_metadata_update_side_data(bsf, pkt);
-    if (err < 0)
-        goto fail;
-
-    err = ff_cbs_read_packet(ctx->input, frag, pkt);
-    if (err < 0) {
-        av_log(bsf, AV_LOG_ERROR, "Failed to read packet.\n");
-        goto fail;
-    }
-
-    if (frag->nb_units == 0) {
-        av_log(bsf, AV_LOG_ERROR, "No OBU in packet.\n");
-        err = AVERROR_INVALIDDATA;
-        goto fail;
-    }
-
     for (i = 0; i < frag->nb_units; i++) {
         if (frag->units[i].type == AV1_OBU_SEQUENCE_HEADER) {
             obu = frag->units[i].content;
             err = av1_metadata_update_sequence_header(bsf, &obu->obu.sequence_header);
             if (err < 0)
-                goto fail;
+                return err;
         }
     }
 
     // If a Temporal Delimiter is present, it must be the first OBU.
     if (frag->units[0].type == AV1_OBU_TEMPORAL_DELIMITER) {
-        if (ctx->td == REMOVE)
+        if (ctx->td == BSF_ELEMENT_REMOVE)
             ff_cbs_delete_unit(frag, 0);
-    } else if (ctx->td == INSERT) {
+    } else if (pkt && ctx->td == BSF_ELEMENT_INSERT) {
         td = (AV1RawOBU) {
             .header.obu_type = AV1_OBU_TEMPORAL_DELIMITER,
         };
@@ -206,7 +132,7 @@ static int av1_metadata_filter(AVBSFContext *bsf, AVPacket *pkt)
                                          &td, NULL);
         if (err < 0) {
             av_log(bsf, AV_LOG_ERROR, "Failed to insert Temporal Delimiter.\n");
-            goto fail;
+            return err;
         }
     }
 
@@ -217,86 +143,26 @@ static int av1_metadata_filter(AVBSFContext *bsf, AVPacket *pkt)
         }
     }
 
-    err = ff_cbs_write_packet(ctx->output, pkt, frag);
-    if (err < 0) {
-        av_log(bsf, AV_LOG_ERROR, "Failed to write packet.\n");
-        goto fail;
-    }
-
-    err = 0;
-fail:
-    ff_cbs_fragment_reset(frag);
-
-    if (err < 0)
-        av_packet_unref(pkt);
-
-    return err;
+    return 0;
 }
 
-static int av1_metadata_init(AVBSFContext *bsf)
-{
-    AV1MetadataContext *ctx = bsf->priv_data;
-    CodedBitstreamFragment *frag = &ctx->access_unit;
-    AV1RawOBU *obu;
-    int err, i;
-
-    err = ff_cbs_init(&ctx->input, AV_CODEC_ID_AV1, bsf);
-    if (err < 0)
-        return err;
-    err = ff_cbs_init(&ctx->output, AV_CODEC_ID_AV1, bsf);
-    if (err < 0)
-        return err;
-
-    if (bsf->par_in->extradata) {
-        err = ff_cbs_read_extradata(ctx->input, frag, bsf->par_in);
-        if (err < 0) {
-            av_log(bsf, AV_LOG_ERROR, "Failed to read extradata.\n");
-            goto fail;
-        }
-
-        for (i = 0; i < frag->nb_units; i++) {
-            if (frag->units[i].type == AV1_OBU_SEQUENCE_HEADER) {
-                obu = frag->units[i].content;
-                err = av1_metadata_update_sequence_header(bsf, &obu->obu.sequence_header);
-                if (err < 0)
-                    goto fail;
-            }
-        }
-
-        err = ff_cbs_write_extradata(ctx->output, bsf->par_out, frag);
-        if (err < 0) {
-            av_log(bsf, AV_LOG_ERROR, "Failed to write extradata.\n");
-            goto fail;
-        }
-    }
-
-    err = 0;
-fail:
-    ff_cbs_fragment_reset(frag);
-    return err;
-}
+static const CBSBSFType av1_metadata_type = {
+    .codec_id        = AV_CODEC_ID_AV1,
+    .fragment_name   = "temporal unit",
+    .unit_name       = "OBU",
+    .update_fragment = &av1_metadata_update_fragment,
+};
 
-static void av1_metadata_close(AVBSFContext *bsf)
+static int av1_metadata_init(AVBSFContext *bsf)
 {
-    AV1MetadataContext *ctx = bsf->priv_data;
-
-    ff_cbs_fragment_free(&ctx->access_unit);
-    ff_cbs_close(&ctx->input);
-    ff_cbs_close(&ctx->output);
+    return ff_cbs_bsf_generic_init(bsf, &av1_metadata_type);
 }
 
 #define OFFSET(x) offsetof(AV1MetadataContext, x)
 #define FLAGS (AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_BSF_PARAM)
 static const AVOption av1_metadata_options[] = {
-    { "td", "Temporal Delimiter OBU",
-        OFFSET(td), AV_OPT_TYPE_INT,
-        { .i64 = PASS }, PASS, REMOVE, FLAGS, "td" },
-    { "pass",   NULL, 0, AV_OPT_TYPE_CONST,
-        { .i64 = PASS   }, .flags = FLAGS, .unit = "td" },
-    { "insert", NULL, 0, AV_OPT_TYPE_CONST,
-        { .i64 = INSERT }, .flags = FLAGS, .unit = "td" },
-    { "remove", NULL, 0, AV_OPT_TYPE_CONST,
-        { .i64 = REMOVE }, .flags = FLAGS, .unit = "td" },
+    BSF_ELEMENT_OPTIONS_PIR("td", "Temporal Delimiter OBU",
+                            td, "td"),
 
     { "color_primaries", "Set color primaries (section 6.4.2)",
         OFFSET(color_primaries), AV_OPT_TYPE_INT,
@@ -356,7 +222,7 @@ const AVBitStreamFilter ff_av1_metadata_bsf = {
     .priv_data_size = sizeof(AV1MetadataContext),
     .priv_class     = &av1_metadata_class,
     .init           = &av1_metadata_init,
-    .close          = &av1_metadata_close,
-    .filter         = &av1_metadata_filter,
+    .close          = &ff_cbs_bsf_generic_close,
+    .filter         = &ff_cbs_bsf_generic_filter,
     .codec_ids      = av1_metadata_codec_ids,
 };
-- 
2.29.2



More information about the ffmpeg-devel mailing list