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

Copying Opus with more priming than the 16-bit pre-skip (e.g. 
`caf/opus-large-priming.caf`, 70000 samples) to Ogg wrote a negative first 
granule, which the demuxer rejects. Packets starting before the pre-skip are 
now dropped (RFC 7845 4.5); sync is kept.

Reported by Forgejo_Fairy on https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/26259.

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



From dcb24f42903058671c9e6ab23de1d690e7c5b10a Mon Sep 17 00:00:00 2001
From: Baptiste Coudurier <[email protected]>
Date: Sat, 21 Nov 2026 15:54:32 -0800
Subject: [PATCH] avformat/ogg/muxer: drop Opus packets before the pre-skip

The 16-bit pre-skip is the only start trim, so earlier packets gave a
negative granule position.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Baptiste Coudurier <[email protected]>
---
 libavformat/ogg/muxer.c                    |  17 +-
 tests/fate/ogg-opus.mak                    |   4 +
 tests/ref/fate/ogg-opus-copy-large-priming | 258 +++++++++++++++++++++
 3 files changed, 276 insertions(+), 3 deletions(-)
 create mode 100644 tests/ref/fate/ogg-opus-copy-large-priming

diff --git a/libavformat/ogg/muxer.c b/libavformat/ogg/muxer.c
index 69b9a679b9b..c9378030ec3 100644
--- a/libavformat/ogg/muxer.c
+++ b/libavformat/ogg/muxer.c
@@ -68,6 +68,7 @@ typedef struct OGGStreamContext {
     unsigned serial_num; ///< serial number
     int64_t last_granule; ///< last packet granule
     int packet_seen; ///< true when packets have been submitted
+    int opus_dropped; ///< Opus packets before the pre-skip were dropped
 } OGGStreamContext;
 
 typedef struct OGGPageList {
@@ -840,10 +841,20 @@ static int ogg_write_packet_internal(AVFormatContext *s, 
AVPacket *pkt)
             pframe_count = 0;
         }
         granule = (oggstream->last_kf_pts<<oggstream->kfgshift) | pframe_count;
-    } else if (st->codecpar->codec_id == AV_CODEC_ID_OPUS)
+    } else if (st->codecpar->codec_id == AV_CODEC_ID_OPUS) {
         /* The pre-skip of the link's OpusHead, in the 48 kHz time base. */
-        granule = pkt->pts + pkt->duration + AV_RL16(oggstream->header[0] + 
10);
-    else if (st->codecpar->codec_id == AV_CODEC_ID_VP8) {
+        int pre_skip = AV_RL16(oggstream->header[0] + 10);
+
+        /* The pre-skip is the only start trim, see RFC 7845 4.5. */
+        if (pkt->pts < -pre_skip) {
+            if (!oggstream->opus_dropped)
+                av_log(s, AV_LOG_WARNING, "Dropping Opus packets starting "
+                       "before the pre-skip of %d samples\n", pre_skip);
+            oggstream->opus_dropped = 1;
+            return 0;
+        }
+        granule = pkt->pts + pkt->duration + pre_skip;
+    } else if (st->codecpar->codec_id == AV_CODEC_ID_VP8) {
         int64_t pts, invcnt, dist;
         int visible;
 
diff --git a/tests/fate/ogg-opus.mak b/tests/fate/ogg-opus.mak
index 59e2794b292..47b850146da 100644
--- a/tests/fate/ogg-opus.mak
+++ b/tests/fate/ogg-opus.mak
@@ -7,6 +7,10 @@ fate-ogg-opus-copy-chained-meta: 
$(APITESTSDIR)/api-dump-stream-meta-test$(EXESU
 fate-ogg-opus-copy-chained-meta: REF = 
$(SRC_PATH)/tests/ref/fate/ogg-opus-chained-meta.txt
 fate-ogg-opus-copy-chained-meta: CMD = run_with_temp "$(FFMPEG) -nostdin 
-hide_banner -loglevel quiet -i $(TARGET_SAMPLES)/ogg-opus/chained-meta.ogg -c 
copy -f ogg -y" "$(APITESTSDIR)/api-dump-stream-meta-test$(EXESUF)" ogg
 
+# Priming beyond the 16-bit pre-skip: packets before it are dropped.
+FATE_OGG_COPY_OPUS-$(call REMUX, OGG, CAF_DEMUXER OGG_DEMUXER) += 
fate-ogg-opus-copy-large-priming
+fate-ogg-opus-copy-large-priming: CMD = transcode caf 
$(TARGET_SAMPLES)/caf/opus-large-priming.caf ogg "-c copy" "-c copy" 
"-show_entries stream=start_time,initial_padding"
+
 # One link of a tone, given its frequency $(1), further muxer options $(2)
 # and output file $(3).
 OGG_OPUS_LINK = ffmpeg -auto_conversion_filters -f lavfi -i 
sine=frequency=$(1):duration=1 \
diff --git a/tests/ref/fate/ogg-opus-copy-large-priming 
b/tests/ref/fate/ogg-opus-copy-large-priming
new file mode 100644
index 00000000000..3db7a7bdf34
--- /dev/null
+++ b/tests/ref/fate/ogg-opus-copy-large-priming
@@ -0,0 +1,258 @@
+12ab49c0db96b0fb332cc7903f774688 
*tests/data/fate/ogg-opus-copy-large-priming.ogg
+48663 tests/data/fate/ogg-opus-copy-large-priming.ogg
+#extradata 0:       19, 0x48fd0655
+#tb 0: 1/48000
+#media_type 0: audio
+#codec_id 0: opus
+#sample_rate 0: 48000
+#channel_layout_name 0: stereo
+0,     -65535,     -65535,      960,      189, 0xfd0962e4, S=1, Skip Samples,  
     10, 0x12ed01fe
+0,     -64575,     -64575,      960,      191, 0xdd5c5cb2
+0,     -63615,     -63615,      960,      193, 0xf21762cf
+0,     -62655,     -62655,      960,      192, 0x8c6160d5
+0,     -61695,     -61695,      960,      193, 0xf5e25fef
+0,     -60735,     -60735,      960,      190, 0xeb5c5b28
+0,     -59775,     -59775,      960,      189, 0xecc9669d
+0,     -58815,     -58815,      960,      197, 0x94d06362
+0,     -57855,     -57855,      960,      193, 0x8e2a69fa
+0,     -56895,     -56895,      960,      187, 0x1650527d
+0,     -55935,     -55935,      960,      190, 0xc33d5ab7
+0,     -54975,     -54975,      960,      201, 0x230a6abe
+0,     -54015,     -54015,      960,      185, 0xda9658c4
+0,     -53055,     -53055,      960,      186, 0x62615eb8
+0,     -52095,     -52095,      960,      188, 0xa1546557
+0,     -51135,     -51135,      960,      191, 0xf69e5aa2
+0,     -50175,     -50175,      960,      189, 0x7c345eb7
+0,     -49215,     -49215,      960,      186, 0x5e985cff
+0,     -48255,     -48255,      960,      190, 0x634462e8
+0,     -47295,     -47295,      960,      182, 0xb16b5f17
+0,     -46335,     -46335,      960,      193, 0x91cf6455
+0,     -45375,     -45375,      960,      190, 0x97e85df6
+0,     -44415,     -44415,      960,      188, 0x4bda6615
+0,     -43455,     -43455,      960,      189, 0x51f761a0
+0,     -42495,     -42495,      960,      198, 0xe1fd68da
+0,     -41535,     -41535,      960,      182, 0x0c6f5815
+0,     -40575,     -40575,      960,      188, 0xbc2a5fdb
+0,     -39615,     -39615,      960,      189, 0x21dc5e06
+0,     -38655,     -38655,      960,      187, 0xed936392
+0,     -37695,     -37695,      960,      192, 0x5ff46036
+0,     -36735,     -36735,      960,      188, 0x1fda5cb4
+0,     -35775,     -35775,      960,      189, 0xb2bb672b
+0,     -34815,     -34815,      960,      192, 0xdd9c5c9f
+0,     -33855,     -33855,      960,      192, 0x2ede5d1f
+0,     -32895,     -32895,      960,      193, 0x3936614b
+0,     -31935,     -31935,      960,      188, 0x39215d97
+0,     -30975,     -30975,      960,      193, 0x545d651d
+0,     -30015,     -30015,      960,      193, 0x2712601f
+0,     -29055,     -29055,      960,      192, 0x934a64cf
+0,     -28095,     -28095,      960,      193, 0xdd9e6310
+0,     -27135,     -27135,      960,      178, 0x2c5258c0
+0,     -26175,     -26175,      960,      192, 0x14ed62e7
+0,     -25215,     -25215,      960,      192, 0x3f86611e
+0,     -24255,     -24255,      960,      192, 0x4281616a
+0,     -23295,     -23295,      960,      187, 0x96dd5b4c
+0,     -22335,     -22335,      960,      186, 0xdcf6557b
+0,     -21375,     -21375,      960,      191, 0x752f6135
+0,     -20415,     -20415,      960,      187, 0xb9296225
+0,     -19455,     -19455,      960,      197, 0x66c55a22
+0,     -18495,     -18495,      960,      189, 0x13c15f5c
+0,     -17535,     -17535,      960,      188, 0x79c15fbd
+0,     -16575,     -16575,      960,      189, 0xb849616f
+0,     -15615,     -15615,      960,      194, 0xd5816249
+0,     -14655,     -14655,      960,      188, 0x95c15cf2
+0,     -13695,     -13695,      960,      191, 0xd70059ef
+0,     -12735,     -12735,      960,      191, 0xcd8761d5
+0,     -11775,     -11775,      960,      196, 0xf2846704
+0,     -10815,     -10815,      960,      183, 0xfa165f8c
+0,      -9855,      -9855,      960,      187, 0xe8665b2e
+0,      -8895,      -8895,      960,      186, 0x219c5fd0
+0,      -7935,      -7935,      960,      193, 0x7bd460e8
+0,      -6975,      -6975,      960,      197, 0xc1fe61b7
+0,      -6015,      -6015,      960,      184, 0x376f5b15
+0,      -5055,      -5055,      960,      183, 0xda9b5e88
+0,      -4095,      -4095,      960,      192, 0x02aa5d75
+0,      -3135,      -3135,      960,      203, 0x4e6f61ba
+0,      -2175,      -2175,      960,      190, 0xf16d6202
+0,      -1215,      -1215,      960,      199, 0x57df615b
+0,       -255,       -255,      960,      192, 0x6f596250
+0,        705,        705,      960,      182, 0x1e275bd1
+0,       1665,       1665,      960,      188, 0xaf505e1d
+0,       2625,       2625,      960,      192, 0xa4be5d5e
+0,       3585,       3585,      960,      188, 0x31de5eb4
+0,       4545,       4545,      960,      191, 0xb1fc6324
+0,       5505,       5505,      960,      199, 0xcade6256
+0,       6465,       6465,      960,      210, 0x27d86138
+0,       7425,       7425,      960,      195, 0xb0b662d9
+0,       8385,       8385,      960,      198, 0x4b1961c8
+0,       9345,       9345,      960,      187, 0xe1dc5d11
+0,      10305,      10305,      960,      188, 0x4c625d6c
+0,      11265,      11265,      960,      180, 0x75405791
+0,      12225,      12225,      960,      186, 0x1bc15d41
+0,      13185,      13185,      960,      200, 0x33cf63dc
+0,      14145,      14145,      960,      190, 0x8b9b5f3a
+0,      15105,      15105,      960,      193, 0xcd555d99
+0,      16065,      16065,      960,      198, 0xbb2f6916
+0,      17025,      17025,      960,      193, 0xd3ca5e0c
+0,      17985,      17985,      960,      187, 0x569660e0
+0,      18945,      18945,      960,      183, 0xf0e457f8
+0,      19905,      19905,      960,      186, 0x042f5c86
+0,      20865,      20865,      960,      177, 0x7dca55fc
+0,      21825,      21825,      960,      185, 0x0229612f
+0,      22785,      22785,      960,      183, 0xe3595b8e
+0,      23745,      23745,      960,      192, 0x95f567cf
+0,      24705,      24705,      960,      185, 0x07b05717
+0,      25665,      25665,      960,      177, 0xde785c2b
+0,      26625,      26625,      960,      188, 0x95aa5fbd
+0,      27585,      27585,      960,      185, 0x2c3b5aed
+0,      28545,      28545,      960,      176, 0x97be5a51
+0,      29505,      29505,      960,      177, 0x106851ed
+0,      30465,      30465,      960,      192, 0x69db5f6c
+0,      31425,      31425,      960,      193, 0x10085a90
+0,      32385,      32385,      960,      191, 0x502c650b
+0,      33345,      33345,      960,      191, 0x5bab6004
+0,      34305,      34305,      960,      183, 0x166a5f5e
+0,      35265,      35265,      960,      193, 0x7eff6133
+0,      36225,      36225,      960,      186, 0x54785cf2
+0,      37185,      37185,      960,      186, 0x1576609a
+0,      38145,      38145,      960,      190, 0xd5eb5e6a
+0,      39105,      39105,      960,      195, 0xc3c5681e
+0,      40065,      40065,      960,      183, 0xa155571a
+0,      41025,      41025,      960,      193, 0x50155cc6
+0,      41985,      41985,      960,      200, 0x7f695fa9
+0,      42945,      42945,      960,      189, 0x24816662
+0,      43905,      43905,      960,      187, 0x76eb572d
+0,      44865,      44865,      960,      187, 0xa05c5f62
+0,      45825,      45825,      960,      188, 0xa42b62af
+0,      46785,      46785,      960,      205, 0x2d576b3a
+0,      47745,      47745,      960,      195, 0x9f7e6387
+0,      48705,      48705,      960,      196, 0xf4296704
+0,      49665,      49665,      960,      195, 0x25a55c15
+0,      50625,      50625,      960,      198, 0xc1bd6736
+0,      51585,      51585,      960,      199, 0xdcef6782
+0,      52545,      52545,      960,      198, 0x2b476b80
+0,      53505,      53505,      960,      198, 0xd5166514
+0,      54465,      54465,      960,      180, 0x5ab85e5f
+0,      55425,      55425,      960,      200, 0xf53767b3
+0,      56385,      56385,      960,      200, 0x4fab6c9d
+0,      57345,      57345,      960,      180, 0x22435ef0
+0,      58305,      58305,      960,      192, 0xb5c65bbc
+0,      59265,      59265,      960,      202, 0x66fc6424
+0,      60225,      60225,      960,      199, 0x5a9f677a
+0,      61185,      61185,      960,      192, 0x1eee5e25
+0,      62145,      62145,      960,      183, 0x36705ec1
+0,      63105,      63105,      960,      206, 0x0423653a
+0,      64065,      64065,      960,      197, 0x7b8b60be
+0,      65025,      65025,      960,      195, 0x3a36608c
+0,      65985,      65985,      960,      198, 0xd55b6006
+0,      66945,      66945,      960,      198, 0xff705fc0
+0,      67905,      67905,      960,      201, 0x46156bb3
+0,      68865,      68865,      960,      203, 0xf52b6712
+0,      69825,      69825,      960,      197, 0xcccb63fb
+0,      70785,      70785,      960,      193, 0x2ad15c3f
+0,      71745,      71745,      960,      196, 0x18f464c3
+0,      72705,      72705,      960,      190, 0xd14c58b2
+0,      73665,      73665,      960,      211, 0xc50e65d2
+0,      74625,      74625,      960,      189, 0xfa1559e9
+0,      75585,      75585,      960,      196, 0x5d096109
+0,      76545,      76545,      960,      189, 0x434958c2
+0,      77505,      77505,      960,      199, 0xba876606
+0,      78465,      78465,      960,      204, 0x177d6aad
+0,      79425,      79425,      960,      199, 0x0ca26410
+0,      80385,      80385,      960,      191, 0x0a1e59ca
+0,      81345,      81345,      960,      195, 0x14805de2
+0,      82305,      82305,      960,      186, 0x121961ea
+0,      83265,      83265,      960,      203, 0x68865f4d
+0,      84225,      84225,      960,      205, 0x8f7660bc
+0,      85185,      85185,      960,      206, 0x157065ab
+0,      86145,      86145,      960,      198, 0xc58263b2
+0,      87105,      87105,      960,      190, 0xb893611b
+0,      88065,      88065,      960,      198, 0xd9736103
+0,      89025,      89025,      960,      202, 0xe4c96432
+0,      89985,      89985,      960,      197, 0x39bb633f
+0,      90945,      90945,      960,      187, 0xb9256689
+0,      91905,      91905,      960,      186, 0x59fa5e06
+0,      92865,      92865,      960,      201, 0x50106624
+0,      93825,      93825,      960,      203, 0x598565f6
+0,      94785,      94785,      960,      191, 0x23d55bf2
+0,      95745,      95745,      960,      193, 0xd8186315
+0,      96705,      96705,      960,      194, 0x8ebb5f5a
+0,      97665,      97665,      960,      190, 0x3f216434
+0,      98625,      98625,      960,      198, 0x12436138
+0,      99585,      99585,      960,      195, 0x060861dd
+0,     100545,     100545,      960,      189, 0x0b415bc5
+0,     101505,     101505,      960,      192, 0xe08d626a
+0,     102465,     102465,      960,      194, 0xb9db62b1
+0,     103425,     103425,      960,      203, 0x6d396083
+0,     104385,     104385,      960,      227, 0xd1b06e0d
+0,     105345,     105345,      960,      193, 0xc2fa6060
+0,     106305,     106305,      960,      193, 0xbe0167be
+0,     107265,     107265,      960,      218, 0x12307107
+0,     108225,     108225,      960,      188, 0x43d35fb2
+0,     109185,     109185,      960,      206, 0x00d86eff
+0,     110145,     110145,      960,      199, 0x33546075
+0,     111105,     111105,      960,      196, 0xba615c07
+0,     112065,     112065,      960,      198, 0xd8af627a
+0,     113025,     113025,      960,      194, 0xafc267d6
+0,     113985,     113985,      960,      197, 0x2e1d6a60
+0,     114945,     114945,      960,      201, 0x7fa16847
+0,     115905,     115905,      960,      190, 0x86936026
+0,     116865,     116865,      960,      196, 0x17f9666c
+0,     117825,     117825,      960,      197, 0x9e756aac
+0,     118785,     118785,      960,      196, 0x399c63c7
+0,     119745,     119745,      960,      201, 0x6da7684c
+0,     120705,     120705,      960,      242, 0xbfc4769f
+0,     121665,     121665,      960,      231, 0xaca5763c
+0,     122625,     122625,      960,      198, 0x3c9f62d0
+0,     123585,     123585,      960,      196, 0xa80f6343
+0,     124545,     124545,      960,      193, 0xe62a671a
+0,     125505,     125505,      960,      185, 0x2a635e4f
+0,     126465,     126465,      960,      194, 0x10d85fab
+0,     127425,     127425,      960,      207, 0x14a368c8
+0,     128385,     128385,      960,      212, 0x97636861
+0,     129345,     129345,      960,      214, 0xd0cf6b10
+0,     130305,     130305,      960,      202, 0x1f6e6793
+0,     131265,     131265,      960,      189, 0x5e195cf6
+0,     132225,     132225,      960,      187, 0x1c1c608d
+0,     133185,     133185,      960,      203, 0x334763d8
+0,     134145,     134145,      960,      198, 0x4b7b666a
+0,     135105,     135105,      960,      193, 0xe665625a
+0,     136065,     136065,      960,      202, 0xe03c6188
+0,     137025,     137025,      960,      214, 0x6bea6a40
+0,     137985,     137985,      960,      230, 0xce2e69a4
+0,     138945,     138945,      960,      215, 0xe5bf66b4
+0,     139905,     139905,      960,      208, 0x923b6117
+0,     140865,     140865,      960,      196, 0x66015d55
+0,     141825,     141825,      960,      189, 0x09865802
+0,     142785,     142785,      960,      183, 0x5f335cda
+0,     143745,     143745,      960,      196, 0x31426617
+0,     144705,     144705,      960,      200, 0x23bb5db2
+0,     145665,     145665,      960,      217, 0x6aa56da9
+0,     146625,     146625,      960,      207, 0x5f1367ec
+0,     147585,     147585,      960,      197, 0x47986b3e
+0,     148545,     148545,      960,      209, 0x6c3069ec
+0,     149505,     149505,      960,      214, 0x7a346d85
+0,     150465,     150465,      960,      199, 0x75066326
+0,     151425,     151425,      960,      200, 0xd4af62de
+0,     152385,     152385,      960,      189, 0x8ebb5c98
+0,     153345,     153345,      960,      212, 0xe45a6af9
+0,     154305,     154305,      960,      221, 0xe89771bc
+0,     155265,     155265,      960,      194, 0x37556228
+0,     156225,     156225,      960,      183, 0xec3b5a0c
+0,     157185,     157185,      960,      201, 0xf57f6a31
+0,     158145,     158145,      960,      197, 0x40715ea4
+0,     159105,     159105,      960,      212, 0xf2c3666e
+0,     160065,     160065,      960,      215, 0x606871ce
+0,     161025,     161025,      960,      201, 0xc1cd66fc
+0,     161985,     161985,      960,      180, 0x5ba05e8a
+0,     162945,     162945,      960,      213, 0x7a067186
+0,     163905,     163905,      960,      219, 0x8920703e
+0,     164865,     164865,      960,      201, 0x219c69a9
+0,     165825,     165825,      960,      187, 0x0d9a5ede
+0,     166785,     166785,      960,      205, 0x17b16466
+0,     167745,     167745,      960,      195, 0xce1f6243
+0,     168705,     168705,      960,      207, 0xaa306a1c
+0,     169665,     169665,      312,      397, 0x3674c0e8, F=0x21, S=1, Skip 
Samples,       10, 0x033a008a
+[STREAM]
+initial_padding=65535
+start_time=0.006979
+[/STREAM]
-- 
2.54.1

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

Reply via email to