PR #25874 opened by Romain Beauxis (toots)
URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/25874
Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/25874.patch

This PR makes the Ogg muxer start a new link from `AV_PKT_DATA_NEW_EXTRADATA`, 
so that a stream copied chain keeps each link's own headers.

The tests stream copy two-link Vorbis, Opus and FLAC chains; Vorbis and Opus 
timing at the join is left for future work.



>From df312e94fb7380481f762b69fe95a2ba4e73450e Mon Sep 17 00:00:00 2001
From: Romain Beauxis <[email protected]>
Date: Sun, 8 Nov 2026 15:15:26 -0600
Subject: [PATCH 1/2] fate/ogg-chain-copy: add chained stream copy tests

Two generated links of Vorbis, Opus and FLAC are stream copied through
the Ogg muxer, listing the links written and checking whether the
decoded copy matches the original.

The references record that each copy starts its second link with the
first link's headers, so that the decoded copy differs from the
original.
---
 tests/Makefile                         |  1 +
 tests/fate/ogg-chain-copy.mak          | 42 ++++++++++++++++++++++++++
 tests/ref/fate/ogg-flac-copy-chained   |  5 +++
 tests/ref/fate/ogg-opus-copy-chained   |  5 +++
 tests/ref/fate/ogg-vorbis-copy-chained |  5 +++
 5 files changed, 58 insertions(+)
 create mode 100644 tests/fate/ogg-chain-copy.mak
 create mode 100644 tests/ref/fate/ogg-flac-copy-chained
 create mode 100644 tests/ref/fate/ogg-opus-copy-chained
 create mode 100644 tests/ref/fate/ogg-vorbis-copy-chained

diff --git a/tests/Makefile b/tests/Makefile
index 365c6e7c194..31be08007ad 100644
--- a/tests/Makefile
+++ b/tests/Makefile
@@ -247,6 +247,7 @@ include $(SRC_PATH)/tests/fate/notchlc.mak
 include $(SRC_PATH)/tests/fate/ogg-vorbis.mak
 include $(SRC_PATH)/tests/fate/ogg-flac.mak
 include $(SRC_PATH)/tests/fate/ogg-opus.mak
+include $(SRC_PATH)/tests/fate/ogg-chain-copy.mak
 include $(SRC_PATH)/tests/fate/oma.mak
 include $(SRC_PATH)/tests/fate/opus.mak
 include $(SRC_PATH)/tests/fate/pcm.mak
diff --git a/tests/fate/ogg-chain-copy.mak b/tests/fate/ogg-chain-copy.mak
new file mode 100644
index 00000000000..568ff23b77c
--- /dev/null
+++ b/tests/fate/ogg-chain-copy.mak
@@ -0,0 +1,42 @@
+# One link of a tone, given its frequency $(1), encoder options $(2) and
+# output file $(3).
+OGG_CHAIN_COPY_LINK = ffmpeg -auto_conversion_filters -f lavfi -i 
sine=frequency=$(1):duration=1 \
+                        $(2) -flags +bitexact -fflags +bitexact -f ogg -y $(3)
+OGG_CHAIN_COPY_DECODE = ffmpeg -auto_conversion_filters -i $(1) -f s16le -y 
$(2)
+
+# Stream copies the chain of $(1).1.ogg and $(1).2.ogg, encoded with options
+# $(2) and $(3). The beginning and end of each link written are listed, then
+# whether the decoded copy matches the original.
+OGG_CHAIN_COPY = \
+    $(call OGG_CHAIN_COPY_LINK,440,$(2),$(1).1.ogg) && \
+    $(call OGG_CHAIN_COPY_LINK,660,$(3) -serial_offset 1,$(1).2.ogg) && \
+    cat $(1).1.ogg $(1).2.ogg > $(1).ogg && \
+    ffmpeg -i $(1).ogg -c copy -fflags +bitexact -f ogg -y $(1).copy.ogg && \
+    run libavformat/tests/ogg_pages$(EXESUF) $(1).copy.ogg | grep -o 
"flags=..." | grep "[be]" && \
+    $(call OGG_CHAIN_COPY_DECODE,$(1).ogg,$(1).pcm) && \
+    $(call OGG_CHAIN_COPY_DECODE,$(1).copy.ogg,$(1).copy.pcm); \
+    cmp -s $(1).pcm $(1).copy.pcm && echo "decoded copy matches the original" 
|| echo "decoded copy differs from the original"
+
+OGG_CHAIN_COPY_DEPS = OGG_MUXER OGG_DEMUXER LAVFI_INDEV SINE_FILTER 
ARESAMPLE_FILTER \
+                      PCM_S16LE_DECODER PCM_S16LE_ENCODER PCM_S16LE_MUXER 
FILE_PROTOCOL
+
+OGG_CHAIN_COPY_VORBIS = tests/data/fate/ogg-vorbis-copy-chained.links
+FATE_OGG_CHAIN_COPY-$(call ALLYES, LIBVORBIS_ENCODER VORBIS_DECODER 
$(OGG_CHAIN_COPY_DEPS)) += fate-ogg-vorbis-copy-chained
+fate-ogg-vorbis-copy-chained: CMD = $(call 
OGG_CHAIN_COPY,$(OGG_CHAIN_COPY_VORBIS),-c:a libvorbis -q:a 1,-c:a libvorbis 
-q:a 9)
+fate-ogg-vorbis-copy-chained: CLEANFILES = $(addprefix 
$(OGG_CHAIN_COPY_VORBIS),.1.ogg .2.ogg .ogg .copy.ogg .pcm .copy.pcm)
+
+OGG_CHAIN_COPY_OPUS = tests/data/fate/ogg-opus-copy-chained.links
+FATE_OGG_CHAIN_COPY-$(call ALLYES, OPUS_ENCODER OPUS_DECODER 
$(OGG_CHAIN_COPY_DEPS)) += fate-ogg-opus-copy-chained
+fate-ogg-opus-copy-chained: CMD = $(call 
OGG_CHAIN_COPY,$(OGG_CHAIN_COPY_OPUS),-c:a opus -strict experimental,-c:a opus 
-strict experimental)
+fate-ogg-opus-copy-chained: CLEANFILES = $(addprefix 
$(OGG_CHAIN_COPY_OPUS),.1.ogg .2.ogg .ogg .copy.ogg .pcm .copy.pcm)
+
+OGG_CHAIN_COPY_FLAC = tests/data/fate/ogg-flac-copy-chained.links
+FATE_OGG_CHAIN_COPY-$(call ALLYES, FLAC_ENCODER FLAC_DECODER FLAC_PARSER 
$(OGG_CHAIN_COPY_DEPS)) += fate-ogg-flac-copy-chained
+fate-ogg-flac-copy-chained: CMD = $(call 
OGG_CHAIN_COPY,$(OGG_CHAIN_COPY_FLAC),-c:a flac -frame_size 1024,-c:a flac 
-frame_size 4096)
+fate-ogg-flac-copy-chained: CLEANFILES = $(addprefix 
$(OGG_CHAIN_COPY_FLAC),.1.ogg .2.ogg .ogg .copy.ogg .pcm .copy.pcm)
+
+$(FATE_OGG_CHAIN_COPY-yes): libavformat/tests/ogg_pages$(EXESUF)
+
+FATE_FFMPEG += $(FATE_OGG_CHAIN_COPY-yes)
+
+fate-ogg-chain-copy: $(FATE_OGG_CHAIN_COPY-yes)
diff --git a/tests/ref/fate/ogg-flac-copy-chained 
b/tests/ref/fate/ogg-flac-copy-chained
new file mode 100644
index 00000000000..a112482bf75
--- /dev/null
+++ b/tests/ref/fate/ogg-flac-copy-chained
@@ -0,0 +1,5 @@
+flags=-b-
+flags=--e
+flags=-b-
+flags=--e
+decoded copy differs from the original
diff --git a/tests/ref/fate/ogg-opus-copy-chained 
b/tests/ref/fate/ogg-opus-copy-chained
new file mode 100644
index 00000000000..a112482bf75
--- /dev/null
+++ b/tests/ref/fate/ogg-opus-copy-chained
@@ -0,0 +1,5 @@
+flags=-b-
+flags=--e
+flags=-b-
+flags=--e
+decoded copy differs from the original
diff --git a/tests/ref/fate/ogg-vorbis-copy-chained 
b/tests/ref/fate/ogg-vorbis-copy-chained
new file mode 100644
index 00000000000..a112482bf75
--- /dev/null
+++ b/tests/ref/fate/ogg-vorbis-copy-chained
@@ -0,0 +1,5 @@
+flags=-b-
+flags=--e
+flags=-b-
+flags=--e
+decoded copy differs from the original
-- 
2.54.0


>From 03ad54f093f1ff97784118179c584603eeb66e86 Mon Sep 17 00:00:00 2001
From: Romain Beauxis <[email protected]>
Date: Sun, 8 Nov 2026 15:20:36 -0600
Subject: [PATCH 2/2] avformat/oggenc: start a new link with the headers of new
 extradata

A packet carrying AV_PKT_DATA_NEW_EXTRADATA that differs from the
current extradata starts a new link, whose headers are built from it.

It shares the restart new metadata triggers, so a later link of a
stream copied chain gets its own headers and tags in one restart.

The decoded FLAC copy matches the original, and the Vorbis and Opus
copies carry the headers of their second link.
---
 libavformat/oggenc.c                 | 51 +++++++++++++++++++---------
 tests/ref/fate/ogg-flac-copy-chained |  2 +-
 2 files changed, 36 insertions(+), 17 deletions(-)

diff --git a/libavformat/oggenc.c b/libavformat/oggenc.c
index f07b986e136..5c72492d67b 100644
--- a/libavformat/oggenc.c
+++ b/libavformat/oggenc.c
@@ -697,31 +697,37 @@ static int ogg_write_header(AVFormatContext *s)
     return 0;
 }
 
-static int ogg_check_new_metadata(AVFormatContext *s, AVPacket *pkt)
+static int ogg_check_new_link(AVFormatContext *s, AVPacket *pkt)
 {
     int ret = 0;
-    size_t size;
+    size_t metadata_size, extradata_size;
     OGGContext *oggcontext = s->priv_data;
     AVStream *st = s->streams[pkt->stream_index];
+    AVCodecParameters *par = st->codecpar;
     OGGStreamContext *oggstream = st->priv_data;
-    const uint8_t *side_metadata = av_packet_get_side_data(pkt, 
AV_PKT_DATA_STRINGS_METADATA, &size);
-
-    if (!side_metadata)
-        return 0;
+    const uint8_t *side_metadata = av_packet_get_side_data(pkt, 
AV_PKT_DATA_STRINGS_METADATA, &metadata_size);
+    const uint8_t *new_extradata = av_packet_get_side_data(pkt, 
AV_PKT_DATA_NEW_EXTRADATA, &extradata_size);
 
     // Don't restart on first packet.
     if (!oggstream->packet_seen)
+        side_metadata = NULL;
+
+    if (new_extradata && extradata_size == par->extradata_size &&
+        !memcmp(new_extradata, par->extradata, extradata_size))
+        new_extradata = NULL;
+
+    if (!side_metadata && !new_extradata)
         return 0;
 
     if (s->nb_streams > 1) {
-        av_log(s, AV_LOG_WARNING, "Multiple streams present: cannot insert new 
metadata!\n");
+        av_log(s, AV_LOG_WARNING, "Multiple streams present: cannot start a 
new link!\n");
         return 0;
     }
 
-    if (st->codecpar->codec_id != AV_CODEC_ID_VORBIS &&
-        st->codecpar->codec_id != AV_CODEC_ID_FLAC &&
-        st->codecpar->codec_id != AV_CODEC_ID_OPUS) {
-        av_log(s, AV_LOG_WARNING, "Inserting metadata is only supported for 
vorbis, flac and opus streams!\n");
+    if (par->codec_id != AV_CODEC_ID_VORBIS &&
+        par->codec_id != AV_CODEC_ID_FLAC &&
+        par->codec_id != AV_CODEC_ID_OPUS) {
+        av_log(s, AV_LOG_WARNING, "Starting a new link is only supported for 
vorbis, flac and opus streams!\n");
         return 0;
     }
 
@@ -729,10 +735,23 @@ static int ogg_check_new_metadata(AVFormatContext *s, 
AVPacket *pkt)
     if (ret < 0)
         goto end;
 
-    av_dict_free(&st->metadata);
-    ret = av_packet_unpack_dictionary(side_metadata, size, &st->metadata);
-    if (ret < 0)
-        goto end;
+    if (side_metadata) {
+        av_dict_free(&st->metadata);
+        ret = av_packet_unpack_dictionary(side_metadata, metadata_size, 
&st->metadata);
+        if (ret < 0)
+            goto end;
+    }
+
+    if (new_extradata) {
+        if (extradata_size > INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE) {
+            ret = AVERROR_INVALIDDATA;
+            goto end;
+        }
+        ret = ff_alloc_extradata(par, extradata_size);
+        if (ret < 0)
+            goto end;
+        memcpy(par->extradata, new_extradata, extradata_size);
+    }
 
     ret = ogg_init(s);
     if (ret < 0)
@@ -756,7 +775,7 @@ static int ogg_write_packet_internal(AVFormatContext *s, 
AVPacket *pkt)
     if (oggcontext->failed)
         return AVERROR_INVALIDDATA;
 
-    ret = ogg_check_new_metadata(s, pkt);
+    ret = ogg_check_new_link(s, pkt);
     if (ret < 0)
         return ret;
 
diff --git a/tests/ref/fate/ogg-flac-copy-chained 
b/tests/ref/fate/ogg-flac-copy-chained
index a112482bf75..a436c719d61 100644
--- a/tests/ref/fate/ogg-flac-copy-chained
+++ b/tests/ref/fate/ogg-flac-copy-chained
@@ -2,4 +2,4 @@ flags=-b-
 flags=--e
 flags=-b-
 flags=--e
-decoded copy differs from the original
+decoded copy matches the original
-- 
2.54.0

_______________________________________________
ffmpeg-devel mailing list -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to