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

This PR makes the Theora decoder follow a chained Ogg stream into a new link 
with its own headers when frame threading is used; master loses the second link 
there.

oggparsetheora attaches a later link's headers as `AV_PKT_DATA_NEW_EXTRADATA`, 
as the other Ogg parsers do, and vp3 sets itself up again from them, handing 
them on to the following frame threads.

The tests chain two links of different frame sizes and check the decoded 
pictures against each link decoded alone, with and without frame threads.


>From 17a3b8a01e253b93d15b80d4cc9b43968567075c Mon Sep 17 00:00:00 2001
From: Romain Beauxis <[email protected]>
Date: Sat, 31 Oct 2026 09:57:15 -0500
Subject: [PATCH 1/5] fate/ogg-theora: add chained decoding tests

Two generated links, the second at a larger frame size, are piped
into one decoder, with and without frame threading.

The references record the size of each decoded frame today, with the
second link lost when frame threading is used.

A further test checks that the chained pictures match those of each
link decoded on its own.
---
 tests/Makefile                                |  1 +
 tests/fate/ogg-theora.mak                     | 48 +++++++++++++++++++
 tests/ref/fate/ogg-theora-chained-size        | 30 ++++++++++++
 .../ogg-theora-chained-size-frame-threads     | 15 ++++++
 4 files changed, 94 insertions(+)
 create mode 100644 tests/fate/ogg-theora.mak
 create mode 100644 tests/ref/fate/ogg-theora-chained-size
 create mode 100644 tests/ref/fate/ogg-theora-chained-size-frame-threads

diff --git a/tests/Makefile b/tests/Makefile
index 365c6e7c194..31e11c99ba8 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-theora.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-theora.mak b/tests/fate/ogg-theora.mak
new file mode 100644
index 00000000000..9d4ca609a26
--- /dev/null
+++ b/tests/fate/ogg-theora.mak
@@ -0,0 +1,48 @@
+# Two links of a test pattern piped as one chained stream, the second at a
+# larger frame size. Each decoded frame's size is listed, since the encoded
+# bytes depend on the libtheora version.
+OGG_THEORA_CHAIN = \
+    { ffmpeg -auto_conversion_filters -f lavfi -i testsrc=s=160x120:r=15:d=1 
-c:v libtheora \
+        -flags +bitexact -fflags +bitexact -f ogg - && \
+      ffmpeg -auto_conversion_filters -f lavfi -i testsrc=s=320x240:r=15:d=1 
-c:v libtheora \
+        -flags +bitexact -fflags +bitexact -serial_offset 1 -f ogg -; } | \
+    run ffprobe$(PROGSSUF)$(EXESUF) -bitexact -of compact=p=0 -show_entries 
frame=width,height
+
+FATE_OGG_THEORA_CHAIN-$(call ENCDEC, LIBTHEORA THEORA, OGG, LAVFI_INDEV 
TESTSRC_FILTER SCALE_FILTER WRAPPED_AVFRAME_DECODER PIPE_PROTOCOL) += 
fate-ogg-theora-chained-size
+fate-ogg-theora-chained-size: CMD = $(OGG_THEORA_CHAIN) -threads 1 -i pipe:
+
+FATE_OGG_THEORA_CHAIN-$(call ENCDEC, LIBTHEORA THEORA, OGG, LAVFI_INDEV 
TESTSRC_FILTER SCALE_FILTER WRAPPED_AVFRAME_DECODER PIPE_PROTOCOL) += 
fate-ogg-theora-chained-size-frame-threads
+fate-ogg-theora-chained-size-frame-threads: CMD = $(OGG_THEORA_CHAIN) -threads 
2 -thread_type frame -i pipe:
+
+FATE_FFMPEG_FFPROBE += $(FATE_OGG_THEORA_CHAIN-yes)
+
+# One link of a test pattern, given its frame size $(1), further muxer
+# options $(2) and output file $(3).
+OGG_THEORA_LINK = ffmpeg -auto_conversion_filters -f lavfi -i 
testsrc=s=$(1):r=15:d=1 -c:v libtheora \
+                    -flags +bitexact -fflags +bitexact $(2) -f ogg -y $(3)
+OGG_THEORA_DECODE = ffmpeg -i $(1) -noautoscale -fps_mode passthrough -f 
rawvideo -
+
+# Each link of a chain is a stream of its own, so decoding the links
+# $(1).1.ogg and $(1).2.ogg chained must give what decoding them one by one
+# does, written to $(1).raw as the reference.
+OGG_THEORA_CHAINED = \
+    $(call OGG_THEORA_LINK,160x120,,$(1).1.ogg) && \
+    $(call OGG_THEORA_LINK,320x240,-serial_offset 1,$(1).2.ogg) && \
+    { $(call OGG_THEORA_DECODE,$(1).1.ogg) && $(call 
OGG_THEORA_DECODE,$(1).2.ogg); } > $(1).raw && \
+    test -s $(1).raw && \
+    cat $(1).1.ogg $(1).2.ogg | $(call OGG_THEORA_DECODE,pipe:)
+
+OGG_THEORA_CHAINED_CONTENT_DEPS = LAVFI_INDEV TESTSRC_FILTER SCALE_FILTER 
WRAPPED_AVFRAME_DECODER RAWVIDEO_MUXER
+
+OGG_THEORA_CHAINED_CONTENT_LINKS = 
tests/data/fate/ogg-theora-chained-content.links
+FATE_OGG_THEORA_CONTENT-$(call TRANSCODE, LIBTHEORA THEORA, OGG, 
$(OGG_THEORA_CHAINED_CONTENT_DEPS)) += fate-ogg-theora-chained-content
+fate-ogg-theora-chained-content: CMD = $(call 
OGG_THEORA_CHAINED,$(OGG_THEORA_CHAINED_CONTENT_LINKS))
+fate-ogg-theora-chained-content: REF = $(OGG_THEORA_CHAINED_CONTENT_LINKS).raw
+fate-ogg-theora-chained-content: CLEANFILES = $(addprefix 
$(OGG_THEORA_CHAINED_CONTENT_LINKS),.1.ogg .2.ogg .raw)
+fate-ogg-theora-chained-content: CMP = stddev
+fate-ogg-theora-chained-content: CMP_TARGET = 0
+fate-ogg-theora-chained-content: FUZZ = 0
+
+FATE_FFMPEG += $(FATE_OGG_THEORA_CONTENT-yes)
+
+fate-ogg-theora: $(FATE_OGG_THEORA_CHAIN-yes) $(FATE_OGG_THEORA_CONTENT-yes)
diff --git a/tests/ref/fate/ogg-theora-chained-size 
b/tests/ref/fate/ogg-theora-chained-size
new file mode 100644
index 00000000000..75daa41cde5
--- /dev/null
+++ b/tests/ref/fate/ogg-theora-chained-size
@@ -0,0 +1,30 @@
+width=160|height=120
+width=160|height=120
+width=160|height=120
+width=160|height=120
+width=160|height=120
+width=160|height=120
+width=160|height=120
+width=160|height=120
+width=160|height=120
+width=160|height=120
+width=160|height=120
+width=160|height=120
+width=160|height=120
+width=160|height=120
+width=160|height=120
+width=320|height=240
+width=320|height=240
+width=320|height=240
+width=320|height=240
+width=320|height=240
+width=320|height=240
+width=320|height=240
+width=320|height=240
+width=320|height=240
+width=320|height=240
+width=320|height=240
+width=320|height=240
+width=320|height=240
+width=320|height=240
+width=320|height=240
diff --git a/tests/ref/fate/ogg-theora-chained-size-frame-threads 
b/tests/ref/fate/ogg-theora-chained-size-frame-threads
new file mode 100644
index 00000000000..6f700c35ed8
--- /dev/null
+++ b/tests/ref/fate/ogg-theora-chained-size-frame-threads
@@ -0,0 +1,15 @@
+width=160|height=120
+width=160|height=120
+width=160|height=120
+width=160|height=120
+width=160|height=120
+width=160|height=120
+width=160|height=120
+width=160|height=120
+width=160|height=120
+width=160|height=120
+width=160|height=120
+width=160|height=120
+width=160|height=120
+width=160|height=120
+width=160|height=120
-- 
2.54.0


>From b7d19b8a3de56b0be14feccdc809f8e7f2d511f3 Mon Sep 17 00:00:00 2001
From: Romain Beauxis <[email protected]>
Date: Sat, 31 Oct 2026 09:55:32 -0500
Subject: [PATCH 2/5] avcodec/vp3: factor out setting Theora up from headers

Move parsing the Theora headers out of initialization, so that headers
arriving later can be parsed the same way.
---
 libavcodec/vp3.c | 14 +++++++++++---
 1 file changed, 11 insertions(+), 3 deletions(-)

diff --git a/libavcodec/vp3.c b/libavcodec/vp3.c
index 0bf14d155e8..71e28184d22 100644
--- a/libavcodec/vp3.c
+++ b/libavcodec/vp3.c
@@ -3053,7 +3053,9 @@ static int theora_decode_tables(AVCodecContext *avctx, 
GetBitContext *gb)
     return 0;
 }
 
-static av_cold int theora_decode_init(AVCodecContext *avctx)
+static av_cold int theora_init_from_headers(AVCodecContext *avctx,
+                                            const uint8_t *headers,
+                                            int headers_size)
 {
     Vp3DecodeContext *s = avctx->priv_data;
     GetBitContext gb;
@@ -3066,12 +3068,12 @@ static av_cold int theora_decode_init(AVCodecContext 
*avctx)
 
     s->theora = 1;
 
-    if (!avctx->extradata_size) {
+    if (!headers_size) {
         av_log(avctx, AV_LOG_ERROR, "Missing extradata!\n");
         return -1;
     }
 
-    if (avpriv_split_xiph_headers(avctx->extradata, avctx->extradata_size,
+    if (avpriv_split_xiph_headers(headers, headers_size,
                                   42, header_start, header_len) < 0) {
         av_log(avctx, AV_LOG_ERROR, "Corrupt extradata\n");
         return -1;
@@ -3123,6 +3125,12 @@ static av_cold int theora_decode_init(AVCodecContext 
*avctx)
     return vp3_decode_init(avctx);
 }
 
+static av_cold int theora_decode_init(AVCodecContext *avctx)
+{
+    return theora_init_from_headers(avctx, avctx->extradata,
+                                    avctx->extradata_size);
+}
+
 const FFCodec ff_theora_decoder = {
     .p.name                = "theora",
     CODEC_LONG_NAME("Theora"),
-- 
2.54.0


>From b83ec00cacc8ca11376610e3aef8119f10cbac5a Mon Sep 17 00:00:00 2001
From: Romain Beauxis <[email protected]>
Date: Sat, 31 Oct 2026 09:55:33 -0500
Subject: [PATCH 3/5] avcodec/vp3: take new Theora headers from packet side
 data

A chained Ogg Theora link has its own headers and starts a new
logical stream, which the decoder only takes up from header packets
and without frame threading.

Set the decoder up again from headers passed as
AV_PKT_DATA_NEW_EXTRADATA before decoding the packet that carries them,
building its own coefficient VLCs rather than taking the first thread's.
---
 libavcodec/vp3.c | 58 +++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 57 insertions(+), 1 deletion(-)

diff --git a/libavcodec/vp3.c b/libavcodec/vp3.c
index 71e28184d22..70218c1a11d 100644
--- a/libavcodec/vp3.c
+++ b/libavcodec/vp3.c
@@ -94,6 +94,8 @@ typedef struct Vp3Fragment {
 
 static int theora_decode_header(AVCodecContext *avctx, GetBitContext *gb);
 static int theora_decode_tables(AVCodecContext *avctx, GetBitContext *gb);
+static int theora_reinit(AVCodecContext *avctx, uint8_t *headers,
+                         int headers_size);
 
 
 /* There are 6 preset schemes, plus a free-form scheme */
@@ -302,6 +304,13 @@ typedef struct Vp3DecodeContext {
      */
     CoeffVLCs *coeff_vlc;
 
+    /**
+     * Headers taken from packet side data, NULL while the codec extradata
+     * applies; a RefStruct reference, padded for the bit reader.
+     */
+    uint8_t *headers;
+    int headers_size;
+
     /* these arrays need to be on 16-byte boundaries since SSE2 operations
      * index into them */
     DECLARE_ALIGNED(16, int16_t, qmat)[3][2][3][64];     ///< 
qmat[qpi][is_inter][plane]
@@ -369,6 +378,7 @@ static av_cold int vp3_decode_end(AVCodecContext *avctx)
     vp3_decode_flush(avctx);
 
     av_refstruct_unref(&s->coeff_vlc);
+    av_refstruct_unref(&s->headers);
 
     return 0;
 }
@@ -2453,7 +2463,9 @@ static av_cold int vp3_decode_init(AVCodecContext *avctx)
         }
     }
 
-    if (ff_thread_sync_ref(avctx, offsetof(Vp3DecodeContext, coeff_vlc)) != 
FF_THREAD_IS_COPY) {
+    /* The first thread's tables may be another link's than these headers'. */
+    if (s->headers ||
+        ff_thread_sync_ref(avctx, offsetof(Vp3DecodeContext, coeff_vlc)) != 
FF_THREAD_IS_COPY) {
         CoeffVLCs *vlcs = av_refstruct_alloc_ext(sizeof(*s->coeff_vlc), 0,
                                                  NULL, free_vlc_tables);
         if (!vlcs)
@@ -2542,6 +2554,30 @@ static int vp3_decode_frame(AVCodecContext *avctx, 
AVFrame *frame,
     GetBitContext gb;
     int ret;
 
+#if CONFIG_THEORA_DECODER
+    if (s->theora) {
+        size_t new_headers_size;
+        const uint8_t *new_headers = av_packet_get_side_data(avpkt,
+                                                             
AV_PKT_DATA_NEW_EXTRADATA,
+                                                             
&new_headers_size);
+        if (new_headers) {
+            uint8_t *headers;
+
+            if (new_headers_size > INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE)
+                return AVERROR_INVALIDDATA;
+            headers = av_refstruct_allocz(new_headers_size +
+                                          AV_INPUT_BUFFER_PADDING_SIZE);
+            if (!headers)
+                return AVERROR(ENOMEM);
+            memcpy(headers, new_headers, new_headers_size);
+            ret = theora_reinit(avctx, headers, new_headers_size);
+            av_refstruct_unref(&headers);
+            if (ret < 0)
+                return ret;
+        }
+    }
+#endif
+
     if ((ret = init_get_bits8(&gb, buf, buf_size)) < 0)
         return ret;
 
@@ -3131,6 +3167,26 @@ static av_cold int theora_decode_init(AVCodecContext 
*avctx)
                                     avctx->extradata_size);
 }
 
+/**
+ * Set the decoder up again from the headers of a new logical stream, taking
+ * a reference to them.
+ */
+static int theora_reinit(AVCodecContext *avctx, uint8_t *headers,
+                         int headers_size)
+{
+    Vp3DecodeContext *s = avctx->priv_data;
+    int ret;
+
+    vp3_decode_end(avctx);
+    av_refstruct_replace(&s->headers, headers);
+    s->headers_size = headers_size;
+    ret = theora_init_from_headers(avctx, s->headers, s->headers_size);
+    if (ret < 0)
+        vp3_decode_end(avctx);
+
+    return ret;
+}
+
 const FFCodec ff_theora_decoder = {
     .p.name                = "theora",
     CODEC_LONG_NAME("Theora"),
-- 
2.54.0


>From 658a75109220edec25308c2a411a734c5b0a0161 Mon Sep 17 00:00:00 2001
From: Romain Beauxis <[email protected]>
Date: Sat, 31 Oct 2026 09:55:33 -0500
Subject: [PATCH 4/5] avcodec/vp3: hand new Theora headers on to the next frame
 thread

Headers from packet side data only reach the frame thread decoding
that packet.

Keep them as a shared reference and set each following thread up
again from them, before it takes over the reference frames of the
thread before it.

Headers that fail to set the decoder up are kept as well, so that
every following thread rejects their link as the first one does.
---
 libavcodec/vp3.c | 14 +++++++++++++-
 1 file changed, 13 insertions(+), 1 deletion(-)

diff --git a/libavcodec/vp3.c b/libavcodec/vp3.c
index 70218c1a11d..cb9971e6a3d 100644
--- a/libavcodec/vp3.c
+++ b/libavcodec/vp3.c
@@ -2520,6 +2520,15 @@ static int vp3_update_thread_context(AVCodecContext 
*dst, const AVCodecContext *
     const Vp3DecodeContext *s1 = src->priv_data;
     int qps_changed = 0;
 
+#if CONFIG_THEORA_DECODER
+    /* A thread before this one took up the headers of a new logical stream. */
+    if (s1->headers && s->headers != s1->headers) {
+        int ret = theora_reinit(dst, s1->headers, s1->headers_size);
+        if (ret < 0)
+            return ret;
+    }
+#endif
+
     // copy previous frame data
     ref_frames(s, s1);
 
@@ -3181,8 +3190,11 @@ static int theora_reinit(AVCodecContext *avctx, uint8_t 
*headers,
     av_refstruct_replace(&s->headers, headers);
     s->headers_size = headers_size;
     ret = theora_init_from_headers(avctx, s->headers, s->headers_size);
-    if (ret < 0)
+    if (ret < 0) {
         vp3_decode_end(avctx);
+        /* Kept, so that the next frame thread rejects this stream too. */
+        av_refstruct_replace(&s->headers, headers);
+    }
 
     return ret;
 }
-- 
2.54.0


>From dd3d9705734843a2b24e3428fa243569700ff93e Mon Sep 17 00:00:00 2001
From: Romain Beauxis <[email protected]>
Date: Sat, 31 Oct 2026 09:55:33 -0500
Subject: [PATCH 5/5] avformat/oggparsetheora: pass a later link's headers to
 the decoder

When a chained stream moves to a new link, its headers are passed on
as packets, which the decoder refuses with frame threading.

Gather the new link's headers in the layout of the extradata and
attach them to its first packet as AV_PKT_DATA_NEW_EXTRADATA, as the
other Ogg parsers do.

The frame-threaded chained Theora tests now decode the second link,
one of them checking its pictures against the link decoded alone.
---
 libavformat/oggparsetheora.c                  | 21 +++++++++++++++++++
 tests/fate/ogg-theora.mak                     | 11 ++++++++++
 .../ogg-theora-chained-size-frame-threads     | 15 +++++++++++++
 3 files changed, 47 insertions(+)

diff --git a/libavformat/oggparsetheora.c b/libavformat/oggparsetheora.c
index e104d63229e..f3bce796148 100644
--- a/libavformat/oggparsetheora.c
+++ b/libavformat/oggparsetheora.c
@@ -176,6 +176,27 @@ static int theora_packet(AVFormatContext *s, int idx)
     struct ogg_stream *os = ogg->streams + idx;
     int duration;
 
+    /* A later link's headers, gathered for the decoder as its extradata is. */
+    if (os->psize > 0 && os->buf[os->pstart] & 0x80) {
+        size_t size;
+        uint8_t *headers;
+
+        if (os->buf[os->pstart] == 0x80)
+            os->new_extradata_size = 0;
+        size = os->new_extradata_size + 2 + os->psize;
+        if (os->psize > UINT16_MAX || size > INT_MAX)
+            return AVERROR_INVALIDDATA;
+        headers = av_realloc(os->new_extradata, size);
+        if (!headers)
+            return AVERROR(ENOMEM);
+        AV_WB16(headers + os->new_extradata_size, os->psize);
+        memcpy(headers + os->new_extradata_size + 2, os->buf + os->pstart, 
os->psize);
+        os->new_extradata      = headers;
+        os->new_extradata_size = size;
+
+        return 1;
+    }
+
     /* first packet handling
        here we parse the duration of each packet in the first page and compare
        the total duration to the page granule to find the encoder delay and
diff --git a/tests/fate/ogg-theora.mak b/tests/fate/ogg-theora.mak
index 9d4ca609a26..3ded229737b 100644
--- a/tests/fate/ogg-theora.mak
+++ b/tests/fate/ogg-theora.mak
@@ -43,6 +43,17 @@ fate-ogg-theora-chained-content: CMP = stddev
 fate-ogg-theora-chained-content: CMP_TARGET = 0
 fate-ogg-theora-chained-content: FUZZ = 0
 
+OGG_THEORA_CHAINED_CONTENT_FT_LINKS = 
tests/data/fate/ogg-theora-chained-content-frame-threads.links
+FATE_OGG_THEORA_CONTENT-$(call TRANSCODE, LIBTHEORA THEORA, OGG, 
$(OGG_THEORA_CHAINED_CONTENT_DEPS)) += 
fate-ogg-theora-chained-content-frame-threads
+fate-ogg-theora-chained-content-frame-threads: CMD = $(call 
OGG_THEORA_CHAINED,$(OGG_THEORA_CHAINED_CONTENT_FT_LINKS))
+fate-ogg-theora-chained-content-frame-threads: REF = 
$(OGG_THEORA_CHAINED_CONTENT_FT_LINKS).raw
+fate-ogg-theora-chained-content-frame-threads: CLEANFILES = $(addprefix 
$(OGG_THEORA_CHAINED_CONTENT_FT_LINKS),.1.ogg .2.ogg .raw)
+fate-ogg-theora-chained-content-frame-threads: CMP = stddev
+fate-ogg-theora-chained-content-frame-threads: CMP_TARGET = 0
+fate-ogg-theora-chained-content-frame-threads: FUZZ = 0
+fate-ogg-theora-chained-content-frame-threads: THREADS = 2
+fate-ogg-theora-chained-content-frame-threads: THREAD_TYPE = frame
+
 FATE_FFMPEG += $(FATE_OGG_THEORA_CONTENT-yes)
 
 fate-ogg-theora: $(FATE_OGG_THEORA_CHAIN-yes) $(FATE_OGG_THEORA_CONTENT-yes)
diff --git a/tests/ref/fate/ogg-theora-chained-size-frame-threads 
b/tests/ref/fate/ogg-theora-chained-size-frame-threads
index 6f700c35ed8..75daa41cde5 100644
--- a/tests/ref/fate/ogg-theora-chained-size-frame-threads
+++ b/tests/ref/fate/ogg-theora-chained-size-frame-threads
@@ -13,3 +13,18 @@ width=160|height=120
 width=160|height=120
 width=160|height=120
 width=160|height=120
+width=320|height=240
+width=320|height=240
+width=320|height=240
+width=320|height=240
+width=320|height=240
+width=320|height=240
+width=320|height=240
+width=320|height=240
+width=320|height=240
+width=320|height=240
+width=320|height=240
+width=320|height=240
+width=320|height=240
+width=320|height=240
+width=320|height=240
-- 
2.54.0

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

Reply via email to