PR #26259 opened by Baptiste Coudurier (bcoudurier)
URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/26259
Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/26259.patch

The OpusHead that cafdec builds stores the CAF priming in the 16-bit pre-skip, 
so priming above 65535 frames wrapped: 70000 became 4464, and a copy to Ogg 
kept that. It is now clamped to 65535 with a warning, and `initial_padding` 
keeps the full priming.

Adds `fate-caf-opus-large-priming`. Its sample, `caf/opus-large-priming.caf`, 
is `caf/opus.caf` with the packet table priming set to 70000. It is on 
fate-suite.

Found by Forgejo_Fairy on https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/25966, 
which moves this code; whichever lands second needs a small rebase.

🤖 Generated with [Claude Code](https://claude.com/claude-code)



From e24498967a452af5745ba56dc18fd045916cf4c2 Mon Sep 17 00:00:00 2001
From: Baptiste Coudurier <[email protected]>
Date: Sat, 21 Nov 2026 12:09:25 -0800
Subject: [PATCH] avformat/cafdec: clamp the Opus pre-skip to 16 bits

Priming beyond 65535 frames wrapped in the OpusHead; warn and clamp
instead, keeping the full priming in initial_padding.

Signed-off-by: Baptiste Coudurier <[email protected]>
---
 libavformat/cafdec.c                  | 16 ++++++++++++++--
 tests/fate/caf.mak                    |  7 ++++++-
 tests/ref/fate/caf-opus-large-priming |  7 +++++++
 3 files changed, 27 insertions(+), 3 deletions(-)
 create mode 100644 tests/ref/fate/caf-opus-large-priming

diff --git a/libavformat/cafdec.c b/libavformat/cafdec.c
index fae4d7db5ec..c296946bba6 100644
--- a/libavformat/cafdec.c
+++ b/libavformat/cafdec.c
@@ -114,6 +114,18 @@ static int read_desc_chunk(AVFormatContext *s)
     return 0;
 }
 
+/**
+ * Store the priming in the OpusHead pre-skip, which is limited to 16 bits.
+ * initial_padding keeps the full priming.
+ */
+static void set_opus_pre_skip(AVFormatContext *s, AVCodecParameters *par)
+{
+    if (par->initial_padding > UINT16_MAX)
+        av_log(s, AV_LOG_WARNING, "priming of %d frames does not fit "
+               "in the Opus pre-skip, clamping\n", par->initial_padding);
+    AV_WL16A(par->extradata + 10, FFMIN(par->initial_padding, UINT16_MAX));
+}
+
 /** Read magic cookie chunk */
 static int read_kuki_chunk(AVFormatContext *s, int64_t size)
 {
@@ -244,7 +256,7 @@ static int read_kuki_chunk(AVFormatContext *s, int64_t size)
         AV_WB32A(st->codecpar->extradata + 4, MKBETAG('H','e','a','d'));
         AV_WB8(st->codecpar->extradata + 8, 1); /* OpusHead version */
         AV_WB8(st->codecpar->extradata + 9, 
st->codecpar->ch_layout.nb_channels);
-        AV_WL16A(st->codecpar->extradata + 10, st->codecpar->initial_padding);
+        set_opus_pre_skip(s, st->codecpar);
         AV_WL32A(st->codecpar->extradata + 12, st->codecpar->sample_rate);
         AV_WL16A(st->codecpar->extradata + 16, 0);
         AV_WB8(st->codecpar->extradata + 18, 0);
@@ -286,7 +298,7 @@ static int read_pakt_chunk(AVFormatContext *s, int64_t size)
     st->nb_frames += caf->remainder;
 
     if (st->codecpar->codec_id == AV_CODEC_ID_OPUS && 
st->codecpar->extradata_size)
-        AV_WL16A(st->codecpar->extradata + 10, st->codecpar->initial_padding);
+        set_opus_pre_skip(s, st->codecpar);
 
     if (caf->bytes_per_packet > 0 && caf->frames_per_packet > 0) {
         if (!num_packets) {
diff --git a/tests/fate/caf.mak b/tests/fate/caf.mak
index 4ee9c95a5c8..a15895efdb1 100644
--- a/tests/fate/caf.mak
+++ b/tests/fate/caf.mak
@@ -21,6 +21,11 @@ fate-caf-pcm_s24-remux: CMD = transcode wav 
$(TARGET_SAMPLES)/audio-reference/di
 FATE_CAF_FFMPEG-$(call REMUX, CAF, MOV_DEMUXER) += fate-caf-mace6-remux
 fate-caf-mace6-remux: CMD = transcode mov 
$(TARGET_SAMPLES)/qtrle/Animation-16Greys.mov caf "-map 0:a -c copy" "-c copy 
-t 0.003"
 
+# priming beyond the 16-bit OpusHead pre-skip
+FATE_CAF_FFPROBE-$(call DEMDEC, CAF, OPUS) += fate-caf-opus-large-priming
+fate-caf-opus-large-priming: CMD = run ffprobe$(PROGSSUF)$(EXESUF) -show_data 
-show_entries stream=initial_padding,extradata 
$(TARGET_SAMPLES)/caf/opus-large-priming.caf
+
 FATE_SAMPLES_FFMPEG         += $(FATE_CAF_FFMPEG-yes)
 FATE_SAMPLES_FFMPEG_FFPROBE += $(FATE_CAF_FFMPEG_FFPROBE-yes)
-fate-caf: $(FATE_CAF_FFMPEG-yes) $(FATE_CAF_FFMPEG_FFPROBE-yes)
+FATE_SAMPLES_FFPROBE        += $(FATE_CAF_FFPROBE-yes)
+fate-caf: $(FATE_CAF_FFMPEG-yes) $(FATE_CAF_FFMPEG_FFPROBE-yes) 
$(FATE_CAF_FFPROBE-yes)
diff --git a/tests/ref/fate/caf-opus-large-priming 
b/tests/ref/fate/caf-opus-large-priming
new file mode 100644
index 00000000000..4e2d0a531b2
--- /dev/null
+++ b/tests/ref/fate/caf-opus-large-priming
@@ -0,0 +1,7 @@
+[STREAM]
+initial_padding=70000
+extradata=
+00000000: 4f70 7573 4865 6164 0102 ffff 80bb 0000  OpusHead........
+00000010: 0000 00                                  ...
+
+[/STREAM]
-- 
2.54.1

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

Reply via email to