[FFmpeg-devel] [PATCH v4 3/6] Pass secondary ogg/opus chained streams metadata.

Romain Beauxis romain.beauxis at gmail.com
Mon Feb 10 21:25:57 EET 2025


These changes parse ogg/opus comment in secondary chained ogg/opus
streams and attach them as extradata to the corresponding packet.

They can then be decoded in the opus decoder and attached to the next
decoded frame.

libavformat/oggparseopus.c: Parse comments from
 secondary chained ogg/opus streams and pass them as ogg stream new_metadata.

libavcodec/opus/dec.c: Unpack comments from secondary chained ogg/opus
streams and attach them to the next decoded frame.
---
 libavcodec/opus/dec.c      | 25 ++++++++++++++++++++++---
 libavformat/oggparseopus.c | 16 +++++++++++++++-
 2 files changed, 37 insertions(+), 4 deletions(-)

diff --git a/libavcodec/opus/dec.c b/libavcodec/opus/dec.c
index 88a650c81c..cddcefcb5f 100644
--- a/libavcodec/opus/dec.c
+++ b/libavcodec/opus/dec.c
@@ -125,6 +125,8 @@ typedef struct OpusContext {
     AVFloatDSPContext *fdsp;
     float   gain;
 
+    AVDictionary *pending_metadata;
+
     OpusParseContext p;
 } OpusContext;
 
@@ -485,12 +487,24 @@ static int opus_decode_packet(AVCodecContext *avctx, AVFrame *frame,
     int decoded_samples = INT_MAX;
     int delayed_samples = 0;
     int i, ret;
+    size_t size;
+    const uint8_t *side_metadata;
 
-    if (buf_size > 8 && (
-       !memcmp(buf, "OpusHead", 8) ||
-       !memcmp(buf, "OpusTags", 8)))
+    if (buf_size > 8 && !memcmp(buf, "OpusHead", 8))
         return buf_size;
 
+    if (buf_size > 8 && !memcmp(buf, "OpusTags", 8)) {
+        /* New metadata */
+        side_metadata = av_packet_get_side_data(avpkt, AV_PKT_DATA_METADATA_UPDATE, &size);
+        if (side_metadata) {
+            av_dict_free(&c->pending_metadata);
+            ret = av_packet_unpack_dictionary(side_metadata, size, &c->pending_metadata);
+            if (ret < 0)
+                return ret;
+        }
+        return buf_size;
+    }
+
     /* calculate the number of delayed samples */
     for (int i = 0; i < c->p.nb_streams; i++) {
         OpusStreamContext *s = &c->streams[i];
@@ -631,6 +645,11 @@ static int opus_decode_packet(AVCodecContext *avctx, AVFrame *frame,
     frame->nb_samples = decoded_samples;
     *got_frame_ptr    = !!decoded_samples;
 
+    if (c->pending_metadata) {
+        av_dict_copy(&frame->metadata, c->pending_metadata, AV_DICT_APPEND);
+        av_dict_free(&c->pending_metadata);
+    }
+
     return avpkt->size;
 }
 
diff --git a/libavformat/oggparseopus.c b/libavformat/oggparseopus.c
index 950b93bd31..3e94a7df3c 100644
--- a/libavformat/oggparseopus.c
+++ b/libavformat/oggparseopus.c
@@ -116,6 +116,7 @@ static int opus_packet(AVFormatContext *avf, int idx)
     AVStream *st                 = avf->streams[idx];
     struct oggopus_private *priv = os->private;
     uint8_t *packet              = os->buf + os->pstart;
+    AVDictionary *new_metadata   = NULL;
     int ret;
 
     if (!os->psize)
@@ -133,8 +134,21 @@ static int opus_packet(AVFormatContext *avf, int idx)
         return 0;
     }
 
-    if (os->psize > 8 && !memcmp(packet, "OpusTags", 8))
+    if (os->psize > 8 && !memcmp(packet, "OpusTags", 8)) {
+        ret = ff_vorbis_comment(avf, &new_metadata, os->buf + os->pstart + 8,
+                                os->psize - 8, 1);
+
+        if (ret < 0)
+            return ret;
+
+        os->new_metadata = av_packet_pack_dictionary(new_metadata, &os->new_metadata_size);
+        av_dict_free(&new_metadata);
+
+        if (!os->new_metadata)
+            return AVERROR(ENOMEM);
+
         return 0;
+    }
 
     if ((!os->lastpts || os->lastpts == AV_NOPTS_VALUE) && !(os->flags & OGG_FLAG_EOS)) {
         int seg, d;
-- 
2.39.5 (Apple Git-154)



More information about the ffmpeg-devel mailing list