Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
 help / color / mirror / Atom feed
From: toots via ffmpeg-devel <ffmpeg-devel@ffmpeg.org>
To: ffmpeg-devel@ffmpeg.org
Cc: toots <code@ffmpeg.org>
Subject: [FFmpeg-devel] [PATCH] libavformat/oggparseopus.c: Parse comments from secondary chained streams header packet. (PR #20327)
Date: Sun, 24 Aug 2025 18:24:24 +0300 (EEST)
Message-ID: <20250824152424.E3AB268E6EB@ffbox0-bg.ffmpeg.org> (raw)

PR #20327 opened by toots
URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/20327
Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/20327.patch


From 67dae45b13b6cc5468178b81faa0124b62c100d5 Mon Sep 17 00:00:00 2001
From: Romain Beauxis <romain.beauxis@gmail.com>
Date: Mon, 4 Aug 2025 09:00:28 -0500
Subject: [PATCH 1/5] ogg/vorbis: implement header packet skip in chained ogg
 bitstreams.

---
 doc/APIchanges                             |  4 +
 libavformat/oggparsevorbis.c               | 85 +++++++++++++++++++++-
 tests/ref/fate/ogg-vorbis-chained-meta.txt |  3 -
 3 files changed, 85 insertions(+), 7 deletions(-)

diff --git a/doc/APIchanges b/doc/APIchanges
index 5d4fb8d127..a8bc75a571 100644
--- a/doc/APIchanges
+++ b/doc/APIchanges
@@ -2,6 +2,10 @@ The last version increases of all libraries were on 2025-03-28
 
 API changes, most recent first:
 
+2025-08-xx - xxxxxxxxxx - lavf 62.4.100 - oggparsevorbis.h oggparseopus.h oggparseflac.h
+  Drop header packets from secondary chained ogg/{flac, opus, vorbis} streams
+  from demuxer output.
+
 2025-08-xx - xxxxxxxx - lavc 62.13.101 - exif.h
   Add AV_EXIF_FLAG_RECURSIVE
 
diff --git a/libavformat/oggparsevorbis.c b/libavformat/oggparsevorbis.c
index 62cc2da6de..1af2f21a82 100644
--- a/libavformat/oggparsevorbis.c
+++ b/libavformat/oggparsevorbis.c
@@ -215,6 +215,12 @@ struct oggvorbis_private {
     AVVorbisParseContext *vp;
     int64_t final_pts;
     int final_duration;
+    uint8_t *header;
+    int header_size;
+    uint8_t *comment;
+    int comment_size;
+    uint8_t *setup;
+    int setup_size;
 };
 
 static int fixup_vorbis_headers(AVFormatContext *as,
@@ -260,6 +266,10 @@ static void vorbis_cleanup(AVFormatContext *s, int idx)
         av_vorbis_parse_free(&priv->vp);
         for (i = 0; i < 3; i++)
             av_freep(&priv->packet[i]);
+
+        av_freep(&priv->header);
+        av_freep(&priv->comment);
+        av_freep(&priv->setup);
     }
 }
 
@@ -434,6 +444,9 @@ static int vorbis_packet(AVFormatContext *s, int idx)
     struct ogg_stream *os = ogg->streams + idx;
     struct oggvorbis_private *priv = os->private;
     int duration, flags = 0;
+    int skip_packet = 0;
+    int ret, new_extradata_size;
+    PutByteContext pb;
 
     if (!priv->vp)
         return AVERROR_INVALIDDATA;
@@ -496,10 +509,50 @@ static int vorbis_packet(AVFormatContext *s, int idx)
         if (duration < 0) {
             os->pflags |= AV_PKT_FLAG_CORRUPT;
             return 0;
-        } else if (flags & VORBIS_FLAG_COMMENT) {
-            vorbis_update_metadata(s, idx);
-            flags = 0;
         }
+
+        if (flags & VORBIS_FLAG_HEADER) {
+            ret = vorbis_parse_header(s, s->streams[idx], os->buf + os->pstart, os->psize);
+            if (ret < 0)
+                return ret;
+
+            ret = av_reallocp(&priv->header, os->psize);
+            if (ret < 0)
+                return ret;
+
+            memcpy(priv->header, os->buf + os->pstart, os->psize);
+            priv->header_size = os->psize;
+
+            skip_packet = 1;
+        }
+
+        if (flags & VORBIS_FLAG_COMMENT) {
+            ret = vorbis_update_metadata(s, idx);
+            if (ret < 0)
+                return ret;
+
+            ret = av_reallocp(&priv->comment, os->psize);
+            if (ret < 0)
+                return ret;
+
+            memcpy(priv->comment, os->buf + os->pstart, os->psize);
+            priv->comment_size = os->psize;
+
+            flags = 0;
+            skip_packet = 1;
+        }
+
+        if (flags & VORBIS_FLAG_SETUP) {
+            ret = av_reallocp(&priv->setup, os->psize);
+            if (ret < 0)
+                return ret;
+
+            memcpy(priv->setup, os->buf + os->pstart, os->psize);
+            priv->setup_size = os->psize;
+
+            skip_packet = 1;
+        }
+
         os->pduration = duration;
     }
 
@@ -521,7 +574,31 @@ static int vorbis_packet(AVFormatContext *s, int idx)
         priv->final_duration += os->pduration;
     }
 
-    return 0;
+    if (priv->header && priv->comment && priv->setup) {
+        new_extradata_size = priv->header_size + priv->comment_size + priv->setup_size + 6;
+
+        ret = av_reallocp(&os->new_extradata, new_extradata_size);
+        if (ret < 0)
+            return ret;
+
+        os->new_extradata_size = new_extradata_size;
+        bytestream2_init_writer(&pb, os->new_extradata, new_extradata_size);
+        bytestream2_put_be16(&pb, priv->header_size);
+        bytestream2_put_buffer(&pb, priv->header, priv->header_size);
+        bytestream2_put_be16(&pb, priv->comment_size);
+        bytestream2_put_buffer(&pb, priv->comment, priv->comment_size);
+        bytestream2_put_be16(&pb, priv->setup_size);
+        bytestream2_put_buffer(&pb, priv->setup, priv->setup_size);
+
+        av_freep(&priv->header);
+        priv->header_size = 0;
+        av_freep(&priv->comment);
+        priv->comment_size = 0;
+        av_freep(&priv->setup);
+        priv->setup_size = 0;
+    }
+
+    return skip_packet;
 }
 
 const struct ogg_codec ff_vorbis_codec = {
diff --git a/tests/ref/fate/ogg-vorbis-chained-meta.txt b/tests/ref/fate/ogg-vorbis-chained-meta.txt
index b7a97c90e2..1206f86c1f 100644
--- a/tests/ref/fate/ogg-vorbis-chained-meta.txt
+++ b/tests/ref/fate/ogg-vorbis-chained-meta.txt
@@ -6,10 +6,7 @@ Stream ID: 0, frame PTS: 128, metadata: N/A
 Stream ID: 0, packet PTS: 704, packet DTS: 704
 Stream ID: 0, frame PTS: 704, metadata: N/A
 Stream ID: 0, packet PTS: 0, packet DTS: 0
-Stream ID: 0, packet PTS: 0, packet DTS: 0
 Stream ID: 0, new metadata: encoder=Lavc61.19.100 libvorbis:title=Second Stream
-Stream ID: 0, packet PTS: 0, packet DTS: 0
-Stream ID: 0, packet PTS: 0, packet DTS: 0
 Stream ID: 0, frame PTS: 0, metadata: N/A
 Stream ID: 0, packet PTS: 128, packet DTS: 128
 Stream ID: 0, frame PTS: 128, metadata: N/A
-- 
2.49.1


From ecf282c33f36f9667dc7a68974f9993b59ea79a3 Mon Sep 17 00:00:00 2001
From: Romain Beauxis <romain.beauxis@gmail.com>
Date: Tue, 18 Feb 2025 22:32:03 -0600
Subject: [PATCH 2/5] libavformat/oggdec.h, libavformat/oggparsevorbis.c:
 Factor out vorbis metadata update mechanism.

---
 libavformat/oggdec.h         | 14 ++++++++++++++
 libavformat/oggparsevorbis.c | 25 +++++++++++++++++--------
 2 files changed, 31 insertions(+), 8 deletions(-)

diff --git a/libavformat/oggdec.h b/libavformat/oggdec.h
index 256a17c5e3..fb87f298e3 100644
--- a/libavformat/oggdec.h
+++ b/libavformat/oggdec.h
@@ -160,6 +160,20 @@ int ff_vorbis_comment(AVFormatContext *ms, AVDictionary **m,
 int ff_vorbis_stream_comment(AVFormatContext *as, AVStream *st,
                              const uint8_t *buf, int size);
 
+/**
+ * Parse Vorbis comments, add metadata to an AVStream
+ *
+ * This function also attaches the metadata to the next decoded
+ * packet as AV_PKT_DATA_METADATA_UPDATE
+ *
+ * @note  The buffer will be temporarily modifed by this function,
+ *        so it needs to be writable. Furthermore it must be padded
+ *        by a single byte (not counted in size).
+ *        All changes will have been reverted upon return.
+ */
+int ff_vorbis_update_metadata(AVFormatContext *s, AVStream *st,
+                              const uint8_t *buf, int size);
+
 static inline int
 ogg_find_stream (struct ogg * ogg, int serial)
 {
diff --git a/libavformat/oggparsevorbis.c b/libavformat/oggparsevorbis.c
index 1af2f21a82..7c4f7624f8 100644
--- a/libavformat/oggparsevorbis.c
+++ b/libavformat/oggparsevorbis.c
@@ -273,20 +273,16 @@ static void vorbis_cleanup(AVFormatContext *s, int idx)
     }
 }
 
-static int vorbis_update_metadata(AVFormatContext *s, int idx)
+int ff_vorbis_update_metadata(AVFormatContext *s, AVStream *st,
+                              const uint8_t *buf, int size)
 {
     struct ogg *ogg = s->priv_data;
-    struct ogg_stream *os = ogg->streams + idx;
-    AVStream *st = s->streams[idx];
+    struct ogg_stream *os = ogg->streams + st->index;
     int ret;
 
-    if (os->psize <= 8)
-        return 0;
-
     /* New metadata packet; release old data. */
     av_dict_free(&st->metadata);
-    ret = ff_vorbis_stream_comment(s, st, os->buf + os->pstart + 7,
-                                   os->psize - 8);
+    ret = ff_vorbis_stream_comment(s, st, buf, size);
     if (ret < 0)
         return ret;
 
@@ -359,6 +355,19 @@ static int vorbis_parse_header(AVFormatContext *s, AVStream *st,
     return 1;
 }
 
+static int vorbis_update_metadata(AVFormatContext *s, int idx)
+{
+    struct ogg *ogg = s->priv_data;
+    struct ogg_stream *os = ogg->streams + idx;
+    AVStream *st = s->streams[idx];
+
+    if (os->psize <= 8)
+        return 0;
+
+    return ff_vorbis_update_metadata(s, st, os->buf + os->pstart + 7,
+                                     os->psize - 8);
+}
+
 static int vorbis_header(AVFormatContext *s, int idx)
 {
     struct ogg *ogg = s->priv_data;
-- 
2.49.1


From 48148765b89b000c950403ae48f366a5243ce9f1 Mon Sep 17 00:00:00 2001
From: Romain Beauxis <romain.beauxis@gmail.com>
Date: Fri, 14 Feb 2025 09:39:45 -0600
Subject: [PATCH 3/5] libavformat/oggdec.c: Use AV_PKT_DATA_STRINGS_METADATA to
 pass metadata updates.

---
 libavformat/oggdec.c                       | 2 +-
 tests/ref/fate/ogg-vorbis-chained-meta.txt | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/libavformat/oggdec.c b/libavformat/oggdec.c
index da3ef815db..10b9725bfb 100644
--- a/libavformat/oggdec.c
+++ b/libavformat/oggdec.c
@@ -880,7 +880,7 @@ retry:
     }
 
     if (os->new_metadata) {
-        ret = av_packet_add_side_data(pkt, AV_PKT_DATA_METADATA_UPDATE,
+        ret = av_packet_add_side_data(pkt, AV_PKT_DATA_STRINGS_METADATA,
                                       os->new_metadata, os->new_metadata_size);
         if (ret < 0)
             return ret;
diff --git a/tests/ref/fate/ogg-vorbis-chained-meta.txt b/tests/ref/fate/ogg-vorbis-chained-meta.txt
index 1206f86c1f..60d9aeb9fa 100644
--- a/tests/ref/fate/ogg-vorbis-chained-meta.txt
+++ b/tests/ref/fate/ogg-vorbis-chained-meta.txt
@@ -7,7 +7,7 @@ Stream ID: 0, packet PTS: 704, packet DTS: 704
 Stream ID: 0, frame PTS: 704, metadata: N/A
 Stream ID: 0, packet PTS: 0, packet DTS: 0
 Stream ID: 0, new metadata: encoder=Lavc61.19.100 libvorbis:title=Second Stream
-Stream ID: 0, frame PTS: 0, metadata: N/A
+Stream ID: 0, frame PTS: 0, metadata: encoder=Lavc61.19.100 libvorbis:title=Second Stream
 Stream ID: 0, packet PTS: 128, packet DTS: 128
 Stream ID: 0, frame PTS: 128, metadata: N/A
 Stream ID: 0, packet PTS: 704, packet DTS: 704
-- 
2.49.1


From 076b79b3a61ceee9cdd3afdac69f92ea43fc1a97 Mon Sep 17 00:00:00 2001
From: Romain Beauxis <romain.beauxis@gmail.com>
Date: Tue, 4 Feb 2025 07:03:49 -0500
Subject: [PATCH 4/5] libavformat/oggparseflac.c: Parse ogg/flac comments in
 new ogg packets, add them to ogg stream new_metadata.

---
 libavformat/oggparseflac.c               | 7 +++++++
 tests/ref/fate/ogg-flac-chained-meta.txt | 3 ++-
 2 files changed, 9 insertions(+), 1 deletion(-)

diff --git a/libavformat/oggparseflac.c b/libavformat/oggparseflac.c
index d66b85b09e..e81e4021a1 100644
--- a/libavformat/oggparseflac.c
+++ b/libavformat/oggparseflac.c
@@ -85,6 +85,8 @@ flac_packet (AVFormatContext * s, int idx)
 {
     struct ogg *ogg = s->priv_data;
     struct ogg_stream *os = ogg->streams + idx;
+    AVStream *st = s->streams[idx];
+    int ret;
 
     if (os->psize > OGG_FLAC_MAGIC_SIZE &&
         !memcmp(
@@ -95,6 +97,11 @@ flac_packet (AVFormatContext * s, int idx)
 
     if (os->psize > 0 &&
         ((os->buf[os->pstart] & 0x7F) == FLAC_METADATA_TYPE_VORBIS_COMMENT)) {
+        ret = ff_vorbis_update_metadata(s, st, os->buf + os->pstart + 4,
+                                        os->psize - 4);
+        if (ret < 0)
+            return ret;
+
         return 1;
     }
 
diff --git a/tests/ref/fate/ogg-flac-chained-meta.txt b/tests/ref/fate/ogg-flac-chained-meta.txt
index 28e22aa29e..5abf37dcee 100644
--- a/tests/ref/fate/ogg-flac-chained-meta.txt
+++ b/tests/ref/fate/ogg-flac-chained-meta.txt
@@ -5,6 +5,7 @@ Stream ID: 0, frame PTS: 0, metadata: N/A
 Stream ID: 0, packet PTS: 4608, packet DTS: 4608
 Stream ID: 0, frame PTS: 4608, metadata: N/A
 Stream ID: 0, packet PTS: 0, packet DTS: 0
-Stream ID: 0, frame PTS: 0, metadata: N/A
+Stream ID: 0, new metadata: encoder=Lavc61.19.100 flac:title=Second Stream
+Stream ID: 0, frame PTS: 0, metadata: encoder=Lavc61.19.100 flac:title=Second Stream
 Stream ID: 0, packet PTS: 4608, packet DTS: 4608
 Stream ID: 0, frame PTS: 4608, metadata: N/A
-- 
2.49.1


From 4c06f4c4a78e6db86f9c7375647e4ba8267392b1 Mon Sep 17 00:00:00 2001
From: Romain Beauxis <romain.beauxis@gmail.com>
Date: Mon, 10 Feb 2025 11:48:03 -0600
Subject: [PATCH 5/5] libavformat/oggparseopus.c: Parse comments from secondary
 chained streams header packet.

---
 libavformat/oggparseopus.c               | 5 +++++
 tests/ref/fate/ogg-opus-chained-meta.txt | 3 ++-
 2 files changed, 7 insertions(+), 1 deletion(-)

diff --git a/libavformat/oggparseopus.c b/libavformat/oggparseopus.c
index 65b93b4053..ae4ff22c53 100644
--- a/libavformat/oggparseopus.c
+++ b/libavformat/oggparseopus.c
@@ -154,6 +154,11 @@ static int opus_packet(AVFormatContext *avf, int idx)
     }
 
     if (os->psize > 8 && !memcmp(packet, "OpusTags", 8)) {
+        ret = ff_vorbis_update_metadata(avf, st, os->buf + os->pstart + 8,
+                                        os->psize - 8);
+        if (ret < 0)
+            return ret;
+
         priv->need_comments = 0;
         return 1;
     }
diff --git a/tests/ref/fate/ogg-opus-chained-meta.txt b/tests/ref/fate/ogg-opus-chained-meta.txt
index addc41c1eb..aad9b83700 100644
--- a/tests/ref/fate/ogg-opus-chained-meta.txt
+++ b/tests/ref/fate/ogg-opus-chained-meta.txt
@@ -13,7 +13,8 @@ Stream ID: 0, frame PTS: 3528, metadata: N/A
 Stream ID: 0, packet PTS: 4488, packet DTS: 4488
 Stream ID: 0, frame PTS: 4488, metadata: N/A
 Stream ID: 0, packet PTS: -312, packet DTS: -312
-Stream ID: 0, frame PTS: -312, metadata: N/A
+Stream ID: 0, new metadata: encoder=Lavc61.19.100 libopus:title=Second Stream
+Stream ID: 0, frame PTS: -312, metadata: encoder=Lavc61.19.100 libopus:title=Second Stream
 Stream ID: 0, packet PTS: 648, packet DTS: 648
 Stream ID: 0, frame PTS: 648, metadata: N/A
 Stream ID: 0, packet PTS: 1608, packet DTS: 1608
-- 
2.49.1

_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

                 reply	other threads:[~2025-08-24 15:24 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20250824152424.E3AB268E6EB@ffbox0-bg.ffmpeg.org \
    --to=ffmpeg-devel@ffmpeg.org \
    --cc=code@ffmpeg.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

This inbox may be cloned and mirrored by anyone:

	git clone --mirror https://master.gitmailbox.com/ffmpegdev/0 ffmpegdev/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 ffmpegdev ffmpegdev/ https://master.gitmailbox.com/ffmpegdev \
		ffmpegdev@gitmailbox.com
	public-inbox-index ffmpegdev

Example config snippet for mirrors.


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git