[FFmpeg-devel] [PATCH] avcodec/extract_extradata: filter what Metadata OBUs are included in AV1 extradata

James Almer jamrial at gmail.com
Mon Sep 18 20:18:26 EEST 2023


Only those that are relevant for the entire coded stream should be included.

Signed-off-by: James Almer <jamrial at gmail.com>
---
 libavcodec/extract_extradata_bsf.c | 27 +++++++++++++++++++++++++++
 1 file changed, 27 insertions(+)

diff --git a/libavcodec/extract_extradata_bsf.c b/libavcodec/extract_extradata_bsf.c
index d5c81a2768..3bf225ee28 100644
--- a/libavcodec/extract_extradata_bsf.c
+++ b/libavcodec/extract_extradata_bsf.c
@@ -58,6 +58,23 @@ static int val_in_array(const int *arr, int len, int val)
     return 0;
 }
 
+static int add_metadata(const AV1OBU *obu)
+{
+    static const int metadata_obu_types[] = {
+        AV1_METADATA_TYPE_HDR_CLL, AV1_METADATA_TYPE_HDR_MDCV,
+    };
+    GetBitContext gb;
+    int metadata_type;
+
+    if (init_get_bits(&gb, obu->data, obu->size_bits) < 0)
+        return 0;
+
+    metadata_type = leb128(&gb);
+
+    return val_in_array(metadata_obu_types, FF_ARRAY_ELEMS(metadata_obu_types),
+                        metadata_type);
+}
+
 static int extract_extradata_av1(AVBSFContext *ctx, AVPacket *pkt,
                                  uint8_t **data, int *size)
 {
@@ -77,6 +94,11 @@ static int extract_extradata_av1(AVBSFContext *ctx, AVPacket *pkt,
     for (i = 0; i < s->av1_pkt.nb_obus; i++) {
         AV1OBU *obu = &s->av1_pkt.obus[i];
         if (val_in_array(extradata_obu_types, nb_extradata_obu_types, obu->type)) {
+            if (obu->type == AV1_OBU_METADATA && !add_metadata(obu)) {
+                if (s->remove)
+                    filtered_size += obu->raw_size;
+                continue;
+            }
             extradata_size += obu->raw_size;
             if (obu->type == AV1_OBU_SEQUENCE_HEADER)
                 has_seq = 1;
@@ -115,6 +137,11 @@ static int extract_extradata_av1(AVBSFContext *ctx, AVPacket *pkt,
             AV1OBU *obu = &s->av1_pkt.obus[i];
             if (val_in_array(extradata_obu_types, nb_extradata_obu_types,
                              obu->type)) {
+                if (obu->type == AV1_OBU_METADATA && !add_metadata(obu)) {
+                    if (s->remove)
+                        bytestream2_put_bufferu(&pb_filtered_data, obu->raw_data, obu->raw_size);
+                    continue;
+                }
                 bytestream2_put_bufferu(&pb_extradata, obu->raw_data, obu->raw_size);
             } else if (s->remove) {
                 bytestream2_put_bufferu(&pb_filtered_data, obu->raw_data, obu->raw_size);
-- 
2.42.0



More information about the ffmpeg-devel mailing list