PR #26358 opened by Romain Beauxis (toots) URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/26358 Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/26358.patch
A stream copy cut (`ffmpeg -ss T -i x -c copy out.ogg`) hands the Ogg muxer negative timestamps: encoder priming plus pre-roll back to the previous video keyframe. The muxer wrote them as negative granules, which the demuxer rejects, and kept the input Opus pre-skip, so the copy decoded wrong. Each link now trims its start the way its codec allows. Opus keeps 80 ms of pre-roll (RFC 7845), or the encoder priming when that is longer, as pre-skip. Vorbis keeps one priming packet and puts the trim in the first page granule. Other codecs drop the pre-roll. Packets holding samples of the presentation are always kept. The muxer warns when a cut keeps less pre-roll than Opus needs. Links starting after zero were cut upstream and are not reported. Tests: `fate-ogg-copy-trim` comes first and records the current behaviour; each code commit updates the refs it changes. End trims are left for later. >From 49b4b3e897b734b4d260d6f16446640a223796e1 Mon Sep 17 00:00:00 2001 From: Romain Beauxis <[email protected]> Date: Sun, 22 Nov 2026 08:18:06 -0500 Subject: [PATCH 1/8] tests/fate: add Ogg stream copy trim tests Stream copy audio to Ogg and compare the decoded size of the copy with that of the source decoded with the same options. Cuts of audio muxed next to video whose only keyframe is at 0 start the copied packets well before the cut point, as a real cut does with sparse keyframes. The refs record the current behaviour: cuts beyond the Opus pre-skip produce unreadable files, shorter cuts keep a stale pre-skip, Vorbis and FLAC play the audio before the cut, and chained links and Matroska end trims come out longer than their source. --- tests/Makefile | 1 + tests/fate/ogg-copy-trim.mak | 89 +++++++++++++++++++ tests/ref/fate/ogg-copy-trim-flac-cut | 15 ++++ tests/ref/fate/ogg-copy-trim-flac-straddle | 15 ++++ tests/ref/fate/ogg-copy-trim-opus-chained | 15 ++++ tests/ref/fate/ogg-copy-trim-opus-cut | 9 ++ tests/ref/fate/ogg-copy-trim-opus-cut-preskip | 15 ++++ tests/ref/fate/ogg-copy-trim-opus-end | 15 ++++ tests/ref/fate/ogg-copy-trim-opus-uncut | 15 ++++ tests/ref/fate/ogg-copy-trim-vorbis-chained | 15 ++++ tests/ref/fate/ogg-copy-trim-vorbis-cut | 15 ++++ tests/ref/fate/ogg-copy-trim-vorbis-edit-list | 15 ++++ 12 files changed, 234 insertions(+) create mode 100644 tests/fate/ogg-copy-trim.mak create mode 100644 tests/ref/fate/ogg-copy-trim-flac-cut create mode 100644 tests/ref/fate/ogg-copy-trim-flac-straddle create mode 100644 tests/ref/fate/ogg-copy-trim-opus-chained create mode 100644 tests/ref/fate/ogg-copy-trim-opus-cut create mode 100644 tests/ref/fate/ogg-copy-trim-opus-cut-preskip create mode 100644 tests/ref/fate/ogg-copy-trim-opus-end create mode 100644 tests/ref/fate/ogg-copy-trim-opus-uncut create mode 100644 tests/ref/fate/ogg-copy-trim-vorbis-chained create mode 100644 tests/ref/fate/ogg-copy-trim-vorbis-cut create mode 100644 tests/ref/fate/ogg-copy-trim-vorbis-edit-list diff --git a/tests/Makefile b/tests/Makefile index 2fb8a368ccb..cb81a988352 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -258,6 +258,7 @@ 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/ogg-copy-trim.mak include $(SRC_PATH)/tests/fate/ogg-theora.mak include $(SRC_PATH)/tests/fate/oma.mak include $(SRC_PATH)/tests/fate/opus.mak diff --git a/tests/fate/ogg-copy-trim.mak b/tests/fate/ogg-copy-trim.mak new file mode 100644 index 00000000000..eb69d2f8129 --- /dev/null +++ b/tests/fate/ogg-copy-trim.mak @@ -0,0 +1,89 @@ +# Stream copies the audio of $(1), read with input options $(2), to $(3).ogg. +# Lists the first packets and the start of the copy, then the decoded sizes +# of the copy and of $(1) decoded with the same options, which match when the +# copy keeps the start and end trims. +OGG_COPY_TRIM = \ + ffmpeg $(2) -i $(1) -map 0:a -c copy -fflags +bitexact -f ogg -y $(3).ogg && \ + framecrc -i $(3).ogg -c copy -frames:a 6 && \ + probe -show_entries stream=start_time,initial_padding -of flat $(3).ogg && \ + ffmpeg -auto_conversion_filters -i $(3).ogg -f s16le -y $(3).copy.pcm && \ + ffmpeg -auto_conversion_filters $(2) -i $(1) -map 0:a -f s16le -y $(3).pcm && \ + echo "decoded copy: $$(($$(wc -c < $(3).copy.pcm))) bytes, decoded source: $$(($$(wc -c < $(3).pcm))) bytes" + +# Muxes the audio of sample $(1) next to video whose only keyframe is at 0, +# to $(2).mkv: a cut then starts the copied audio at the beginning of the file. +OGG_COPY_TRIM_AV = \ + ffmpeg -i $(TARGET_SAMPLES)/mov/spherical.mov -i $(TARGET_SAMPLES)/$(1) \ + -map 0:v -map 1:a -c copy -t 2 -fflags +bitexact -f matroska -y $(2).mkv + +# Cuts sample $(1) muxed with video at $(2) seconds, into $(3). +OGG_COPY_TRIM_CUT = \ + $(call OGG_COPY_TRIM_AV,$(1),$(3)) && \ + $(call OGG_COPY_TRIM,$(3).mkv,-ss $(2),$(3)) + +OGG_COPY_TRIM_FILES = .ogg .copy.pcm .pcm +OGG_COPY_TRIM_DEPS = OGG_MUXER OGG_DEMUXER FRAMECRC_MUXER ARESAMPLE_FILTER \ + PCM_S16LE_ENCODER PCM_S16LE_MUXER FILE_PROTOCOL +OGG_COPY_TRIM_AV_DEPS = MOV_DEMUXER MATROSKA_MUXER MATROSKA_DEMUXER H264_PARSER $(OGG_COPY_TRIM_DEPS) + +# Opus cut 1.5 s after the start of the copy, more than the pre-skip can hold. +OGG_COPY_TRIM_OPUS_CUT = tests/data/fate/ogg-copy-trim-opus-cut +FATE_OGG_COPY_TRIM-$(call ALLYES, OPUS_DECODER $(OGG_COPY_TRIM_AV_DEPS)) += fate-ogg-copy-trim-opus-cut +fate-ogg-copy-trim-opus-cut: CMD = $(call OGG_COPY_TRIM_CUT,audiomatch/tones_opus_48000_stereo.opus,1.5,$(OGG_COPY_TRIM_OPUS_CUT)) +fate-ogg-copy-trim-opus-cut: CLEANFILES = $(addprefix $(OGG_COPY_TRIM_OPUS_CUT),.mkv $(OGG_COPY_TRIM_FILES)) + +# Opus cut 1 s after the start of the copy, which the pre-skip can hold. +OGG_COPY_TRIM_OPUS_PRESKIP = tests/data/fate/ogg-copy-trim-opus-cut-preskip +FATE_OGG_COPY_TRIM-$(call ALLYES, OPUS_DECODER $(OGG_COPY_TRIM_AV_DEPS)) += fate-ogg-copy-trim-opus-cut-preskip +fate-ogg-copy-trim-opus-cut-preskip: CMD = $(call OGG_COPY_TRIM_CUT,audiomatch/tones_opus_48000_stereo.opus,1.0,$(OGG_COPY_TRIM_OPUS_PRESKIP)) +fate-ogg-copy-trim-opus-cut-preskip: CLEANFILES = $(addprefix $(OGG_COPY_TRIM_OPUS_PRESKIP),.mkv $(OGG_COPY_TRIM_FILES)) + +# Opus copied whole: only the encoder priming is trimmed. +OGG_COPY_TRIM_OPUS_UNCUT = tests/data/fate/ogg-copy-trim-opus-uncut +FATE_OGG_COPY_TRIM-$(call ALLYES, OPUS_DECODER $(OGG_COPY_TRIM_AV_DEPS)) += fate-ogg-copy-trim-opus-uncut +fate-ogg-copy-trim-opus-uncut: CMD = $(call OGG_COPY_TRIM_AV,audiomatch/tones_opus_48000_stereo.opus,$(OGG_COPY_TRIM_OPUS_UNCUT)) && \ + $(call OGG_COPY_TRIM,$(OGG_COPY_TRIM_OPUS_UNCUT).mkv,,$(OGG_COPY_TRIM_OPUS_UNCUT)) +fate-ogg-copy-trim-opus-uncut: CLEANFILES = $(addprefix $(OGG_COPY_TRIM_OPUS_UNCUT),.mkv $(OGG_COPY_TRIM_FILES)) + +OGG_COPY_TRIM_VORBIS_CUT = tests/data/fate/ogg-copy-trim-vorbis-cut +FATE_OGG_COPY_TRIM-$(call ALLYES, VORBIS_DECODER $(OGG_COPY_TRIM_AV_DEPS)) += fate-ogg-copy-trim-vorbis-cut +fate-ogg-copy-trim-vorbis-cut: CMD = $(call OGG_COPY_TRIM_CUT,ogg-vorbis/tos.ogg,1.3,$(OGG_COPY_TRIM_VORBIS_CUT)) +fate-ogg-copy-trim-vorbis-cut: CLEANFILES = $(addprefix $(OGG_COPY_TRIM_VORBIS_CUT),.mkv $(OGG_COPY_TRIM_FILES)) + +OGG_COPY_TRIM_FLAC_CUT = tests/data/fate/ogg-copy-trim-flac-cut +FATE_OGG_COPY_TRIM-$(call ALLYES, FLAC_DEMUXER FLAC_DECODER $(OGG_COPY_TRIM_AV_DEPS)) += fate-ogg-copy-trim-flac-cut +fate-ogg-copy-trim-flac-cut: CMD = $(call OGG_COPY_TRIM_CUT,filter/hdcd.flac,1.3,$(OGG_COPY_TRIM_FLAC_CUT)) +fate-ogg-copy-trim-flac-cut: CLEANFILES = $(addprefix $(OGG_COPY_TRIM_FLAC_CUT),.mkv $(OGG_COPY_TRIM_FILES)) + +# FLAC cut within its first packet, which straddles the start. +OGG_COPY_TRIM_FLAC_STRADDLE = tests/data/fate/ogg-copy-trim-flac-straddle +FATE_OGG_COPY_TRIM-$(call ALLYES, FLAC_DEMUXER FLAC_DECODER $(OGG_COPY_TRIM_DEPS)) += fate-ogg-copy-trim-flac-straddle +fate-ogg-copy-trim-flac-straddle: CMD = $(call OGG_COPY_TRIM,$(TARGET_SAMPLES)/filter/hdcd.flac,-ss 1.3,$(OGG_COPY_TRIM_FLAC_STRADDLE)) +fate-ogg-copy-trim-flac-straddle: CLEANFILES = $(addprefix $(OGG_COPY_TRIM_FLAC_STRADDLE),$(OGG_COPY_TRIM_FILES)) + +# Vorbis whose MP4 edit list starts after the first second of samples. +OGG_COPY_TRIM_VORBIS_EDIT = tests/data/fate/ogg-copy-trim-vorbis-edit-list +FATE_OGG_COPY_TRIM-$(call ALLYES, MOV_DEMUXER VORBIS_DECODER $(OGG_COPY_TRIM_DEPS)) += fate-ogg-copy-trim-vorbis-edit-list +fate-ogg-copy-trim-vorbis-edit-list: CMD = $(call OGG_COPY_TRIM,$(TARGET_SAMPLES)/mov/mov_neg_first_pts_discard_vorbis.mp4,,$(OGG_COPY_TRIM_VORBIS_EDIT)) +fate-ogg-copy-trim-vorbis-edit-list: CLEANFILES = $(addprefix $(OGG_COPY_TRIM_VORBIS_EDIT),$(OGG_COPY_TRIM_FILES)) + +# Later links start at the timestamps of the end of the previous ones. +OGG_COPY_TRIM_OPUS_CHAINED = tests/data/fate/ogg-copy-trim-opus-chained +FATE_OGG_COPY_TRIM-$(call ALLYES, OPUS_DECODER $(OGG_COPY_TRIM_DEPS)) += fate-ogg-copy-trim-opus-chained +fate-ogg-copy-trim-opus-chained: CMD = $(call OGG_COPY_TRIM,$(TARGET_SAMPLES)/ogg-opus/chained-meta.ogg,,$(OGG_COPY_TRIM_OPUS_CHAINED)) +fate-ogg-copy-trim-opus-chained: CLEANFILES = $(addprefix $(OGG_COPY_TRIM_OPUS_CHAINED),$(OGG_COPY_TRIM_FILES)) + +OGG_COPY_TRIM_VORBIS_CHAINED = tests/data/fate/ogg-copy-trim-vorbis-chained +FATE_OGG_COPY_TRIM-$(call ALLYES, VORBIS_DECODER $(OGG_COPY_TRIM_DEPS)) += fate-ogg-copy-trim-vorbis-chained +fate-ogg-copy-trim-vorbis-chained: CMD = $(call OGG_COPY_TRIM,$(TARGET_SAMPLES)/ogg-vorbis/chained-meta.ogg,,$(OGG_COPY_TRIM_VORBIS_CHAINED)) +fate-ogg-copy-trim-vorbis-chained: CLEANFILES = $(addprefix $(OGG_COPY_TRIM_VORBIS_CHAINED),$(OGG_COPY_TRIM_FILES)) + +# Opus from Matroska whose last packet carries an end trim. +OGG_COPY_TRIM_OPUS_END = tests/data/fate/ogg-copy-trim-opus-end +FATE_OGG_COPY_TRIM-$(call ALLYES, MATROSKA_DEMUXER OPUS_DECODER $(OGG_COPY_TRIM_DEPS)) += fate-ogg-copy-trim-opus-end +fate-ogg-copy-trim-opus-end: CMD = $(call OGG_COPY_TRIM,$(TARGET_SAMPLES)/mkv/codec_delay_opus.mkv,,$(OGG_COPY_TRIM_OPUS_END)) +fate-ogg-copy-trim-opus-end: CLEANFILES = $(addprefix $(OGG_COPY_TRIM_OPUS_END),$(OGG_COPY_TRIM_FILES)) + +FATE_SAMPLES_FFMPEG += $(FATE_OGG_COPY_TRIM-yes) + +fate-ogg-copy-trim: $(FATE_OGG_COPY_TRIM-yes) diff --git a/tests/ref/fate/ogg-copy-trim-flac-cut b/tests/ref/fate/ogg-copy-trim-flac-cut new file mode 100644 index 00000000000..a32b6e2dc67 --- /dev/null +++ b/tests/ref/fate/ogg-copy-trim-flac-cut @@ -0,0 +1,15 @@ +#extradata 0: 34, 0x8f590b9b +#tb 0: 1/44100 +#media_type 0: audio +#codec_id 0: flac +#sample_rate 0: 44100 +#channel_layout_name 0: stereo +0, 0, 0, 4096, 11736, 0xdb0073ee +0, 4096, 4096, 4096, 12370, 0xca19a21f +0, 8192, 8192, 4096, 11903, 0xb493bd69 +0, 12288, 12288, 4096, 11242, 0x3240cab4 +0, 16384, 16384, 4096, 11372, 0x96b59721 +0, 16384, 16384, 4096, 10645, 0x05b74a79 +streams.stream.0.initial_padding=0 +streams.stream.0.start_time="0.000000" +decoded copy: 360448 bytes, decoded source: 131128 bytes diff --git a/tests/ref/fate/ogg-copy-trim-flac-straddle b/tests/ref/fate/ogg-copy-trim-flac-straddle new file mode 100644 index 00000000000..70f70b40b34 --- /dev/null +++ b/tests/ref/fate/ogg-copy-trim-flac-straddle @@ -0,0 +1,15 @@ +#extradata 0: 34, 0x8f590b9b +#tb 0: 1/44100 +#media_type 0: audio +#codec_id 0: flac +#sample_rate 0: 44100 +#channel_layout_name 0: stereo +0, 0, 0, 4096, 8113, 0x9acd9413 +0, 4096, 4096, 4096, 8521, 0x2070136c +0, 8192, 8192, 4096, 8687, 0xe48f4c68 +0, 12288, 12288, 4096, 10582, 0xe2d10986 +0, 16384, 16384, 4096, 11175, 0xda5442f1 +0, 20480, 20480, 4096, 8563, 0x31a3565e +streams.stream.0.initial_padding=0 +streams.stream.0.start_time="0.000000" +decoded copy: 1409024 bytes, decoded source: 1392696 bytes diff --git a/tests/ref/fate/ogg-copy-trim-opus-chained b/tests/ref/fate/ogg-copy-trim-opus-chained new file mode 100644 index 00000000000..a907e93059b --- /dev/null +++ b/tests/ref/fate/ogg-copy-trim-opus-chained @@ -0,0 +1,15 @@ +#extradata 0: 19, 0x3a04048f +#tb 0: 1/48000 +#media_type 0: audio +#codec_id 0: opus +#sample_rate 0: 48000 +#channel_layout_name 0: mono +0, -312, -312, 960, 283, 0xe9f38292, S=1, Skip Samples, 10, 0x02390039 +0, 648, 648, 960, 219, 0x85986e41 +0, 1608, 1608, 960, 213, 0xe9a5717a +0, 2568, 2568, 960, 213, 0x698a6b4c +0, 3528, 3528, 960, 213, 0xed936c20 +0, 4488, 4488, 312, 315, 0x111b9206, F=0x21, S=1, Skip Samples, 10, 0x033a008a +streams.stream.0.initial_padding=312 +streams.stream.0.start_time="0.000000" +decoded copy: 20496 bytes, decoded source: 19200 bytes diff --git a/tests/ref/fate/ogg-copy-trim-opus-cut b/tests/ref/fate/ogg-copy-trim-opus-cut new file mode 100644 index 00000000000..a231f6ee95f --- /dev/null +++ b/tests/ref/fate/ogg-copy-trim-opus-cut @@ -0,0 +1,9 @@ +#extradata 0: 19, 0x38100445 +#tb 0: 1/48000 +#media_type 0: audio +#codec_id 0: opus +#sample_rate 0: 48000 +#channel_layout_name 0: stereo +streams.stream.0.initial_padding=312 +streams.stream.0.start_time="N/A" +decoded copy: 0 bytes, decoded source: 96000 bytes diff --git a/tests/ref/fate/ogg-copy-trim-opus-cut-preskip b/tests/ref/fate/ogg-copy-trim-opus-cut-preskip new file mode 100644 index 00000000000..a40c2256247 --- /dev/null +++ b/tests/ref/fate/ogg-copy-trim-opus-cut-preskip @@ -0,0 +1,15 @@ +#extradata 0: 19, 0x38100445 +#tb 0: 1/48000 +#media_type 0: audio +#codec_id 0: opus +#sample_rate 0: 48000 +#channel_layout_name 0: stereo +0, -312, -312, 960, 354, 0xa2a7b0c8, S=1, Skip Samples, 10, 0x02390039 +0, 648, 648, 960, 366, 0xf507c0ab +0, 1608, 1608, 960, 359, 0x0379b6ca +0, 2568, 2568, 960, 377, 0x7a62c054 +0, 3528, 3528, 960, 369, 0x08c1c043 +0, 4488, 4488, 960, 357, 0x689bab86 +streams.stream.0.initial_padding=312 +streams.stream.0.start_time="-0.980000" +decoded copy: 380160 bytes, decoded source: 192000 bytes diff --git a/tests/ref/fate/ogg-copy-trim-opus-end b/tests/ref/fate/ogg-copy-trim-opus-end new file mode 100644 index 00000000000..30999f344fc --- /dev/null +++ b/tests/ref/fate/ogg-copy-trim-opus-end @@ -0,0 +1,15 @@ +#extradata 0: 19, 0x3a04048f +#tb 0: 1/48000 +#media_type 0: audio +#codec_id 0: opus +#sample_rate 0: 48000 +#channel_layout_name 0: mono +0, -312, -312, 960, 320, 0x58b9a88d, S=1, Skip Samples, 10, 0x02390039 +0, 648, 648, 960, 159, 0x6c9c4b4c +0, 1608, 1608, 960, 148, 0x0caf4b5d +0, 2568, 2568, 960, 139, 0xc5624226 +0, 3528, 3528, 960, 146, 0x633c4937 +0, 4488, 4488, 960, 153, 0x3d0b4f93 +streams.stream.0.initial_padding=312 +streams.stream.0.start_time="0.000500" +decoded copy: 99216 bytes, decoded source: 97804 bytes diff --git a/tests/ref/fate/ogg-copy-trim-opus-uncut b/tests/ref/fate/ogg-copy-trim-opus-uncut new file mode 100644 index 00000000000..fe5ca5fb221 --- /dev/null +++ b/tests/ref/fate/ogg-copy-trim-opus-uncut @@ -0,0 +1,15 @@ +#extradata 0: 19, 0x38100445 +#tb 0: 1/48000 +#media_type 0: audio +#codec_id 0: opus +#sample_rate 0: 48000 +#channel_layout_name 0: stereo +0, -312, -312, 960, 489, 0xb843f4b8, S=1, Skip Samples, 10, 0x02390039 +0, 648, 648, 960, 354, 0xa2a7b0c8 +0, 1608, 1608, 960, 366, 0xf507c0ab +0, 2568, 2568, 960, 359, 0x0379b6ca +0, 3528, 3528, 960, 377, 0x7a62c054 +0, 4488, 4488, 960, 369, 0x08c1c043 +streams.stream.0.initial_padding=312 +streams.stream.0.start_time="0.000000" +decoded copy: 384000 bytes, decoded source: 384000 bytes diff --git a/tests/ref/fate/ogg-copy-trim-vorbis-chained b/tests/ref/fate/ogg-copy-trim-vorbis-chained new file mode 100644 index 00000000000..1e02a846369 --- /dev/null +++ b/tests/ref/fate/ogg-copy-trim-vorbis-chained @@ -0,0 +1,15 @@ +#extradata 0: 3244, 0x21fbfdf4 +#tb 0: 1/44100 +#media_type 0: audio +#codec_id 0: vorbis +#sample_rate 0: 44100 +#channel_layout_name 0: mono +0, -128, -128, 128, 28, 0xa5cb0b38, S=1, Strings Metadata, 51, 0xaec210c6 +0, 0, 0, 576, 59, 0xf30f1b21 +0, 576, 576, 1024, 85, 0x34322b3b, S=1, Skip Samples, 10, 0x03000080 +0, 1600, 1600, 128, 28, 0xa5cb0b38, S=2, Strings Metadata, 52, 0xbfe6111a, New Extradata, 3302, 0xe4d01005 +0, 1728, 1728, 576, 59, 0xf30f1b21 +0, 2304, 2304, 1024, 85, 0x34322b3b +streams.stream.0.initial_padding=0 +streams.stream.0.start_time="0.000000" +decoded copy: 6144 bytes, decoded source: 4780 bytes diff --git a/tests/ref/fate/ogg-copy-trim-vorbis-cut b/tests/ref/fate/ogg-copy-trim-vorbis-cut new file mode 100644 index 00000000000..6e86d6a39d9 --- /dev/null +++ b/tests/ref/fate/ogg-copy-trim-vorbis-cut @@ -0,0 +1,15 @@ +#extradata 0: 3887, 0x4fb856b4 +#tb 0: 1/44100 +#media_type 0: audio +#codec_id 0: vorbis +#sample_rate 0: 44100 +#channel_layout_name 0: stereo +0, -576, -576, 576, 267, 0x5b048bc9, S=1, Strings Metadata, 121, 0xc2832208 +0, 0, 0, 1024, 275, 0x777286f5 +0, 1024, 1024, 1024, 201, 0x0d106229 +0, 2048, 2048, 1024, 204, 0xc1766392 +0, 3072, 3072, 1024, 212, 0xde0b6eca +0, 4096, 4096, 1024, 224, 0xdba36edc +streams.stream.0.initial_padding=0 +streams.stream.0.start_time="0.000000" +decoded copy: 351744 bytes, decoded source: 125240 bytes diff --git a/tests/ref/fate/ogg-copy-trim-vorbis-edit-list b/tests/ref/fate/ogg-copy-trim-vorbis-edit-list new file mode 100644 index 00000000000..239edff9315 --- /dev/null +++ b/tests/ref/fate/ogg-copy-trim-vorbis-edit-list @@ -0,0 +1,15 @@ +#extradata 0: 3887, 0x4fb856b4 +#tb 0: 1/44100 +#media_type 0: audio +#codec_id 0: vorbis +#sample_rate 0: 44100 +#channel_layout_name 0: stereo +0, -45056, -45056, 1024, 270, 0x9f588389, S=1, Strings Metadata, 62, 0xc650157d +0, -44032, -44032, 1024, 259, 0xcfeb7fad +0, -43008, -43008, 1024, 260, 0xf34d83b8 +0, -41984, -41984, 1024, 274, 0xbe048550 +0, -40960, -40960, 1024, 273, 0x36e68bf3 +0, -39936, -39936, 1024, 289, 0x0d399472 +streams.stream.0.initial_padding=0 +streams.stream.0.start_time="0.000000" +decoded copy: 1130496 bytes, decoded source: 954368 bytes -- 2.54.1 >From d78657dc498300bf7931f4f82f8ad7eaab78b5c4 Mon Sep 17 00:00:00 2001 From: Romain Beauxis <[email protected]> Date: Sun, 22 Nov 2026 08:23:22 -0500 Subject: [PATCH 2/8] avformat/ogg/demuxer/vorbis: factor out the page start timestamp Move the scan deriving the timestamp of the first packet of a page from its granule into a function, for use at the start of the stream. --- libavformat/ogg/demuxer/vorbis.c | 50 ++++++++++++++++++++------------ 1 file changed, 31 insertions(+), 19 deletions(-) diff --git a/libavformat/ogg/demuxer/vorbis.c b/libavformat/ogg/demuxer/vorbis.c index fd5b26fbd31..2b4ccea305c 100644 --- a/libavformat/ogg/demuxer/vorbis.c +++ b/libavformat/ogg/demuxer/vorbis.c @@ -347,6 +347,36 @@ static int vorbis_check_packet_order(AVFormatContext *s, return error ? AVERROR_INVALIDDATA : 0; } +/** + * Timestamp of the current packet, the first one of its page, from the page + * granule and the durations of the packets the page completes. + */ +static int64_t vorbis_page_start_pts(struct ogg_stream *os, + struct oggvorbis_private *priv) +{ + int page_duration = os->pduration, flags = 0; + uint8_t *last_pkt = os->buf + os->pstart + os->psize; + uint8_t *next_pkt = last_pkt; + + for (int seg = os->segp; seg < os->nsegs; seg++) { + next_pkt += os->segments[seg]; + if (os->segments[seg] < 255 && next_pkt != last_pkt) { + int pkt_flags = 0; + int d = av_vorbis_parse_frame_flags(priv->vp, last_pkt, 1, &pkt_flags); + if (d < 0) { + page_duration = os->granule; + break; + } + page_duration += d; + last_pkt = next_pkt; + } + } + /* Restore the parser state to the one following the current packet. */ + av_vorbis_parse_frame_flags(priv->vp, os->buf + os->pstart, 1, &flags); + + return os->granule - page_duration; +} + static int vorbis_packet(AVFormatContext *s, int idx) { struct ogg *ogg = s->priv_data; @@ -403,26 +433,8 @@ static int vorbis_packet(AVFormatContext *s, int idx) os->lastdts -= priv->ts_offset; } else if (os->page_start && os->lastpts == AV_NOPTS_VALUE && !(os->flags & OGG_FLAG_EOS) && (int64_t)os->granule >= 0) { - int seg, page_duration = os->pduration; - uint8_t *last_pkt = os->buf + os->pstart + os->psize; - uint8_t *next_pkt = last_pkt; - - for (seg = os->segp; seg < os->nsegs; seg++) { - next_pkt += os->segments[seg]; - if (os->segments[seg] < 255 && next_pkt != last_pkt) { - int pkt_flags = 0; - int d = av_vorbis_parse_frame_flags(priv->vp, last_pkt, 1, &pkt_flags); - if (d < 0) { - page_duration = os->granule; - break; - } - page_duration += d; - last_pkt = next_pkt; - } - } - av_vorbis_parse_frame_flags(priv->vp, os->buf + os->pstart, 1, &flags); os->lastpts = - os->lastdts = os->granule - page_duration; + os->lastdts = vorbis_page_start_pts(os, priv); } /* Broken files may have granule=0 on pages that contain audio data. -- 2.54.1 >From 2ca7bc53348a4d1b7c9f575e85acd2fcd3b02690 Mon Sep 17 00:00:00 2001 From: Romain Beauxis <[email protected]> Date: Sun, 22 Nov 2026 15:15:42 -0500 Subject: [PATCH 3/8] avformat/ogg/muxer: restart the timestamps of each audio link Each link of a chained stream is a stream of its own, whose granule positions count from its start. Packets of a new link keep the input timestamps, which continue from the previous link, so the granules of later links were offset by the duration of the links before them, and the demuxer, which restarts its timestamps at each link, lost their end trim. Take the timestamps of a new link from where it starts presenting: past the start trim of its first packet, and past the Vorbis priming packet. --- libavformat/ogg/muxer.c | 34 ++++++++++++++++++--- libavformat/ogg/muxer.h | 6 +++- libavformat/ogg/muxer/theora.c | 5 +-- libavformat/ogg/muxer/vorbis.c | 7 +++-- libavformat/ogg/muxer/vp8.c | 6 ++-- tests/ref/fate/ogg-copy-trim-opus-chained | 2 +- tests/ref/fate/ogg-copy-trim-vorbis-chained | 4 +-- tests/ref/fate/ogg-flac-serial-default | 2 +- tests/ref/fate/ogg-flac-serial-offset | 2 +- 9 files changed, 50 insertions(+), 18 deletions(-) diff --git a/libavformat/ogg/muxer.c b/libavformat/ogg/muxer.c index ee939b9623e..0e3d94e16a8 100644 --- a/libavformat/ogg/muxer.c +++ b/libavformat/ogg/muxer.c @@ -574,6 +574,9 @@ static int ogg_check_new_link(AVFormatContext *s, AVPacket *pkt) if (ret < 0) goto end; + /* Each link restarts its timestamps, the input packets do not. */ + oggstream->link_start_pending = par->codec_type == AVMEDIA_TYPE_AUDIO; + ret = ogg_write_header(s); end: @@ -581,13 +584,30 @@ end: return ret; } +/** + * Input timestamp at which a link starting with pkt starts presenting. + */ +static int64_t ogg_link_start(const OGGStreamContext *oggstream, const AVPacket *pkt) +{ + size_t size; + const uint8_t *skip = av_packet_get_side_data(pkt, AV_PKT_DATA_SKIP_SAMPLES, &size); + int64_t start = pkt->pts; + + if (skip && size >= 10) + start += AV_RL32(skip); + if (oggstream->mapping->priming_packet) + start += pkt->duration; + + return start; +} + static int ogg_write_packet_internal(AVFormatContext *s, AVPacket *pkt) { AVStream *st = s->streams[pkt->stream_index]; OGGContext *oggcontext = s->priv_data; OGGStreamContext *oggstream = st->priv_data; int ret; - int64_t granule; + int64_t granule, link_pts; if (oggcontext->failed) return AVERROR_INVALIDDATA; @@ -598,13 +618,19 @@ static int ogg_write_packet_internal(AVFormatContext *s, AVPacket *pkt) if (ret < 0 || oggcontext->ended) return ret; + if (oggstream->link_start_pending) { + oggstream->link_pts_offset = ogg_link_start(oggstream, pkt); + oggstream->link_start_pending = 0; + } + link_pts = pkt->pts - oggstream->link_pts_offset; + if (oggstream->mapping->packet_granule) - granule = oggstream->mapping->packet_granule(st, pkt); + granule = oggstream->mapping->packet_granule(st, pkt, link_pts); else - granule = pkt->pts + pkt->duration + oggstream->packet_granule_offset; + granule = link_pts + pkt->duration + oggstream->packet_granule_offset; if (oggstream->page.start_granule == AV_NOPTS_VALUE) - oggstream->page.start_granule = pkt->pts; + oggstream->page.start_granule = link_pts; ret = ogg_buffer_data(s, st, pkt->data, pkt->size, granule, 0); if (ret < 0) diff --git a/libavformat/ogg/muxer.h b/libavformat/ogg/muxer.h index 6b0a7cf625d..18e0a0f56e4 100644 --- a/libavformat/ogg/muxer.h +++ b/libavformat/ogg/muxer.h @@ -62,6 +62,8 @@ 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 link_start_pending; ///< the next packet sets link_pts_offset + int64_t link_pts_offset; ///< input timestamp at which the current link starts presenting } OGGStreamContext; /** @@ -73,6 +75,7 @@ struct OGGMuxerMapping { size_t priv_data_size; ///< size of OGGStreamContext.priv_data int optional_extradata; ///< the headers can be built without extradata int new_links; ///< a new link can replace the current one + int priming_packet; ///< the first packet of a link only primes the decoder /** * Build the header packets in OGGStreamContext.header, adjusting the * stream time base when the codec sets its own. @@ -86,10 +89,11 @@ struct OGGMuxerMapping { const char *(*new_link_refused)(const AVStream *st, const AVPacket *pkt, const uint8_t *new_extradata, size_t size); /** + * @param pts the packet timestamp within its link * @return the granule of a packet. When NULL, the granule is the * packet's end timestamp plus OGGStreamContext.packet_granule_offset. */ - int64_t (*packet_granule)(AVStream *st, const AVPacket *pkt); + int64_t (*packet_granule)(AVStream *st, const AVPacket *pkt, int64_t pts); }; /** diff --git a/libavformat/ogg/muxer/theora.c b/libavformat/ogg/muxer/theora.c index 93911a8fb65..a9155a53c22 100644 --- a/libavformat/ogg/muxer/theora.c +++ b/libavformat/ogg/muxer/theora.c @@ -89,13 +89,14 @@ static const char *theora_new_link_refused(const AVStream *st, const AVPacket *p return NULL; } -static int64_t theora_packet_granule(AVStream *st, const AVPacket *pkt) +static int64_t theora_packet_granule(AVStream *st, const AVPacket *pkt, int64_t pts) { OGGStreamContext *oggstream = st->priv_data; TheoraMuxerContext *theora = oggstream->priv_data; - int64_t pts = theora->vrev < 1 ? pkt->pts : pkt->pts + pkt->duration; int pframe_count; + if (theora->vrev >= 1) + pts += pkt->duration; if (pkt->flags & AV_PKT_FLAG_KEY) theora->last_kf_pts = pts; pframe_count = pts - theora->last_kf_pts; diff --git a/libavformat/ogg/muxer/vorbis.c b/libavformat/ogg/muxer/vorbis.c index a3a219706bc..504570e2087 100644 --- a/libavformat/ogg/muxer/vorbis.c +++ b/libavformat/ogg/muxer/vorbis.c @@ -27,7 +27,8 @@ static int vorbis_init(AVFormatContext *s, AVStream *st) } const OGGMuxerMapping ff_ogg_vorbis_muxer_mapping = { - .codec_id = AV_CODEC_ID_VORBIS, - .new_links = 1, - .init = vorbis_init, + .codec_id = AV_CODEC_ID_VORBIS, + .new_links = 1, + .priming_packet = 1, + .init = vorbis_init, }; diff --git a/libavformat/ogg/muxer/vp8.c b/libavformat/ogg/muxer/vp8.c index a61bdd3f3e6..14ca3b4af0d 100644 --- a/libavformat/ogg/muxer/vp8.c +++ b/libavformat/ogg/muxer/vp8.c @@ -92,14 +92,14 @@ static int vp8_init(AVFormatContext *s, AVStream *st) return 0; } -static int64_t vp8_packet_granule(AVStream *st, const AVPacket *pkt) +static int64_t vp8_packet_granule(AVStream *st, const AVPacket *pkt, int64_t pts) { const OGGStreamContext *oggstream = st->priv_data; - int64_t pts, invcnt, dist; + int64_t invcnt, dist; int visible; visible = (pkt->data[0] >> 4) & 1; - pts = pkt->pts + pkt->duration; + pts += pkt->duration; invcnt = (oggstream->last_granule >> 30) & 3; invcnt = visible ? 3 : (invcnt == 3 ? 0 : invcnt + 1); dist = (pkt->flags & AV_PKT_FLAG_KEY) ? 0 : ((oggstream->last_granule >> 3) & 0x07ffffff) + 1; diff --git a/tests/ref/fate/ogg-copy-trim-opus-chained b/tests/ref/fate/ogg-copy-trim-opus-chained index a907e93059b..c1f97692e97 100644 --- a/tests/ref/fate/ogg-copy-trim-opus-chained +++ b/tests/ref/fate/ogg-copy-trim-opus-chained @@ -12,4 +12,4 @@ 0, 4488, 4488, 312, 315, 0x111b9206, F=0x21, S=1, Skip Samples, 10, 0x033a008a streams.stream.0.initial_padding=312 streams.stream.0.start_time="0.000000" -decoded copy: 20496 bytes, decoded source: 19200 bytes +decoded copy: 19200 bytes, decoded source: 19200 bytes diff --git a/tests/ref/fate/ogg-copy-trim-vorbis-chained b/tests/ref/fate/ogg-copy-trim-vorbis-chained index 1e02a846369..89a684f2e1d 100644 --- a/tests/ref/fate/ogg-copy-trim-vorbis-chained +++ b/tests/ref/fate/ogg-copy-trim-vorbis-chained @@ -9,7 +9,7 @@ 0, 576, 576, 1024, 85, 0x34322b3b, S=1, Skip Samples, 10, 0x03000080 0, 1600, 1600, 128, 28, 0xa5cb0b38, S=2, Strings Metadata, 52, 0xbfe6111a, New Extradata, 3302, 0xe4d01005 0, 1728, 1728, 576, 59, 0xf30f1b21 -0, 2304, 2304, 1024, 85, 0x34322b3b +0, 2304, 2304, 1024, 85, 0x34322b3b, S=1, Skip Samples, 10, 0x03000080 streams.stream.0.initial_padding=0 streams.stream.0.start_time="0.000000" -decoded copy: 6144 bytes, decoded source: 4780 bytes +decoded copy: 5888 bytes, decoded source: 4780 bytes diff --git a/tests/ref/fate/ogg-flac-serial-default b/tests/ref/fate/ogg-flac-serial-default index d2bfab60a20..1f61b2e0685 100644 --- a/tests/ref/fate/ogg-flac-serial-default +++ b/tests/ref/fate/ogg-flac-serial-default @@ -3,4 +3,4 @@ pos=79 serial=1798652137 sequence=1 granule=0 flags=--- segments=1 size=70 pos=177 serial=1798652137 sequence=2 granule=8820 flags=--e segments=10 size=2418 pos=2632 serial=670444992 sequence=0 granule=0 flags=-b- segments=1 size=51 pos=2711 serial=670444992 sequence=1 granule=0 flags=--- segments=1 size=71 -pos=2810 serial=670444992 sequence=2 granule=17640 flags=--e segments=10 size=2418 +pos=2810 serial=670444992 sequence=2 granule=8820 flags=--e segments=10 size=2418 diff --git a/tests/ref/fate/ogg-flac-serial-offset b/tests/ref/fate/ogg-flac-serial-offset index 37d6287a384..66c737fac17 100644 --- a/tests/ref/fate/ogg-flac-serial-offset +++ b/tests/ref/fate/ogg-flac-serial-offset @@ -3,4 +3,4 @@ pos=79 serial=67574 sequence=1 granule=0 flags=--- segments=1 size=70 pos=177 serial=67574 sequence=2 granule=8820 flags=--e segments=10 size=2418 pos=2632 serial=67574 sequence=0 granule=0 flags=-b- segments=1 size=51 pos=2711 serial=67574 sequence=1 granule=0 flags=--- segments=1 size=71 -pos=2810 serial=67574 sequence=2 granule=17640 flags=--e segments=10 size=2418 +pos=2810 serial=67574 sequence=2 granule=8820 flags=--e segments=10 size=2418 -- 2.54.1 >From 75f22b5e0cee4788cc528e1264ac952a207fc6c8 Mon Sep 17 00:00:00 2001 From: Romain Beauxis <[email protected]> Date: Sun, 22 Nov 2026 15:16:16 -0500 Subject: [PATCH 4/8] avformat/ogg/muxer: split packet buffering out of the packet writer Move computing the granule and buffering a data packet into a function, and reading the start trim of a packet into another, for use when packets are held at the start of a link. --- libavformat/ogg/muxer.c | 60 ++++++++++++++++++++++++++++------------- 1 file changed, 41 insertions(+), 19 deletions(-) diff --git a/libavformat/ogg/muxer.c b/libavformat/ogg/muxer.c index 0e3d94e16a8..056218a77b8 100644 --- a/libavformat/ogg/muxer.c +++ b/libavformat/ogg/muxer.c @@ -584,30 +584,64 @@ end: return ret; } +/** + * Number of samples to skip from the start of pkt, from its side data. + */ +static int64_t ogg_packet_start_trim(const AVPacket *pkt) +{ + size_t size; + const uint8_t *skip = av_packet_get_side_data(pkt, AV_PKT_DATA_SKIP_SAMPLES, &size); + + return skip && size >= 10 ? AV_RL32(skip) : 0; +} + /** * Input timestamp at which a link starting with pkt starts presenting. */ static int64_t ogg_link_start(const OGGStreamContext *oggstream, const AVPacket *pkt) { - size_t size; - const uint8_t *skip = av_packet_get_side_data(pkt, AV_PKT_DATA_SKIP_SAMPLES, &size); - int64_t start = pkt->pts; + int64_t start = pkt->pts + ogg_packet_start_trim(pkt); - if (skip && size >= 10) - start += AV_RL32(skip); if (oggstream->mapping->priming_packet) start += pkt->duration; return start; } +/** + * Buffer a data packet at its link timestamp link_pts. + */ +static int ogg_buffer_packet(AVFormatContext *s, AVStream *st, + const AVPacket *pkt, int64_t link_pts) +{ + OGGStreamContext *oggstream = st->priv_data; + int64_t granule; + int ret; + + if (oggstream->mapping->packet_granule) + granule = oggstream->mapping->packet_granule(st, pkt, link_pts); + else + granule = link_pts + pkt->duration + oggstream->packet_granule_offset; + + if (oggstream->page.start_granule == AV_NOPTS_VALUE) + oggstream->page.start_granule = link_pts; + + ret = ogg_buffer_data(s, st, pkt->data, pkt->size, granule, 0); + if (ret < 0) + return ret; + + oggstream->last_granule = granule; + oggstream->packet_seen = 1; + + return 0; +} + static int ogg_write_packet_internal(AVFormatContext *s, AVPacket *pkt) { AVStream *st = s->streams[pkt->stream_index]; OGGContext *oggcontext = s->priv_data; OGGStreamContext *oggstream = st->priv_data; int ret; - int64_t granule, link_pts; if (oggcontext->failed) return AVERROR_INVALIDDATA; @@ -622,25 +656,13 @@ static int ogg_write_packet_internal(AVFormatContext *s, AVPacket *pkt) oggstream->link_pts_offset = ogg_link_start(oggstream, pkt); oggstream->link_start_pending = 0; } - link_pts = pkt->pts - oggstream->link_pts_offset; - if (oggstream->mapping->packet_granule) - granule = oggstream->mapping->packet_granule(st, pkt, link_pts); - else - granule = link_pts + pkt->duration + oggstream->packet_granule_offset; - - if (oggstream->page.start_granule == AV_NOPTS_VALUE) - oggstream->page.start_granule = link_pts; - - ret = ogg_buffer_data(s, st, pkt->data, pkt->size, granule, 0); + ret = ogg_buffer_packet(s, st, pkt, pkt->pts - oggstream->link_pts_offset); if (ret < 0) return ret; ogg_write_pages(s, 0); - oggstream->last_granule = granule; - oggstream->packet_seen = 1; - return 0; } -- 2.54.1 >From 4138eab0986b2461c95d101f2ede43eb94fe8d1b Mon Sep 17 00:00:00 2001 From: Romain Beauxis <[email protected]> Date: Sun, 22 Nov 2026 15:17:07 -0500 Subject: [PATCH 5/8] avformat/ogg/muxer: keep link starts within the pre-roll the decoder needs Negative timestamps mark samples decoded before the start of the presentation: encoder priming, but also the packets a stream copy keeps before a cut point, which reach back to the previous video keyframe. The muxer wrote them as negative granule positions, which the demuxer rejects, and copied the pre-skip of the input OpusHead, which only accounts for the encoder priming. Hold the headers of each link until its first packet is known. Packets that end before the start are dropped once the pre-roll kept covers what the decoder needs to converge: 80 ms for Opus (RFC 7845), or the encoder priming of its headers when longer, and nothing for the other codecs, except for the latest Vorbis packet, which primes the decoder. The Opus pre-skip is then set from the timestamp of the first packet, or from its start trim when they differ by less than a packet, as timestamps can be rounded by the input time base. Packets holding samples of the presentation are always kept. --- libavformat/ogg/muxer.c | 88 ++++++++++++++++++- libavformat/ogg/muxer.h | 10 +++ libavformat/ogg/muxer/opus.c | 22 ++++- tests/fate/ogg-copy-trim.mak | 5 +- tests/ref/fate/limited_input_seek | 2 +- tests/ref/fate/ogg-copy-trim-flac-cut | 14 +-- tests/ref/fate/ogg-copy-trim-opus-cut | 14 ++- tests/ref/fate/ogg-copy-trim-opus-cut-preskip | 20 ++--- tests/ref/fate/ogg-copy-trim-vorbis-cut | 14 +-- tests/ref/fate/ogg-copy-trim-vorbis-edit-list | 14 +-- 10 files changed, 160 insertions(+), 43 deletions(-) diff --git a/libavformat/ogg/muxer.c b/libavformat/ogg/muxer.c index 056218a77b8..3598aa94a19 100644 --- a/libavformat/ogg/muxer.c +++ b/libavformat/ogg/muxer.c @@ -80,6 +80,7 @@ static void ogg_cleanup_stream(AVStream *st) for (int i = 0; i < 3; i++) av_freep(&oggstream->header[i]); av_freep(&oggstream->priv_data); + av_packet_free(&oggstream->primer); } static void ogg_write_page(AVFormatContext *s, OGGPage *page, int extra_flags) @@ -342,6 +343,14 @@ static void ogg_write_pages(AVFormatContext *s, int flush) if (!ogg->page_list) return; + /* Headers stay buffered until the start trim of their link is known. */ + if (flush != 1) + for (int i = 0; i < s->nb_streams; i++) { + const OGGStreamContext *oggstream = s->streams[i]->priv_data; + if (oggstream->start_pending) + return; + } + for (p = ogg->page_list; p; ) { OGGStreamContext *oggstream = s->streams[p->page.stream_index]->priv_data; @@ -424,6 +433,7 @@ static int ogg_init(AVFormatContext *s) oggstream->page.stream_index = i; oggstream->mapping = mapping; + oggstream->start_pending = st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO; if (mapping->priv_data_size) { oggstream->priv_data = av_mallocz(mapping->priv_data_size); if (!oggstream->priv_data) @@ -636,11 +646,80 @@ static int ogg_buffer_packet(AVFormatContext *s, AVStream *st, return 0; } +/** + * Write the start trim of a link into its header packets and its held + * identification header page. + */ +static void ogg_set_start_trim(AVFormatContext *s, AVStream *st, int64_t trim) +{ + OGGContext *ogg = s->priv_data; + OGGStreamContext *oggstream = st->priv_data; + + oggstream->mapping->set_start_trim(st, trim); + /* The identification header is alone on the first page of the link. */ + for (OGGPageList *p = ogg->page_list; p; p = p->next) + if (p->page.stream_index == st->index && (p->page.flags & 2)) { + memcpy(p->page.data, oggstream->header[0], oggstream->header_len[0]); + break; + } +} + +/** + * Handle a packet of a link of which no packet is written yet. Pre-roll + * beyond what the decoder needs is dropped, except a priming packet; the + * first packet kept sets the start trim and releases the headers. + * + * @return 1 when pkt is to be written, 0 when it was dropped or held + */ +static int ogg_start_link(AVFormatContext *s, AVStream *st, + const AVPacket *pkt, int64_t link_pts) +{ + OGGStreamContext *oggstream = st->priv_data; + const OGGMuxerMapping *mapping = oggstream->mapping; + /* Encoder priming is kept whole; the pre-roll of a cut only as far as + * the decoder needs it to converge. */ + int64_t preroll = FFMAX(mapping->start_preroll, oggstream->codec_start_trim); + int ret; + + if (link_pts < 0 && link_pts + pkt->duration <= -preroll) { + if (!mapping->priming_packet) + return 0; + if (!oggstream->primer && !(oggstream->primer = av_packet_alloc())) + return AVERROR(ENOMEM); + av_packet_unref(oggstream->primer); + return av_packet_ref(oggstream->primer, pkt); + } + + if (link_pts < 0 && mapping->set_start_trim) { + int64_t trim = -link_pts, packet_trim = ogg_packet_start_trim(pkt); + + /* Timestamps may be rounded to the input time base, while the trim of + * the packet is exact. A cut differs from it by whole packets. */ + if (packet_trim && FFABS(trim - packet_trim) < pkt->duration) + trim = packet_trim; + ogg_set_start_trim(s, st, trim); + } + + oggstream->start_pending = 0; + ogg_write_pages(s, 2); + + if (oggstream->primer) { + ret = ogg_buffer_packet(s, st, oggstream->primer, + oggstream->primer->pts - oggstream->link_pts_offset); + av_packet_free(&oggstream->primer); + if (ret < 0) + return ret; + } + + return 1; +} + static int ogg_write_packet_internal(AVFormatContext *s, AVPacket *pkt) { AVStream *st = s->streams[pkt->stream_index]; OGGContext *oggcontext = s->priv_data; OGGStreamContext *oggstream = st->priv_data; + int64_t link_pts; int ret; if (oggcontext->failed) @@ -656,8 +735,15 @@ static int ogg_write_packet_internal(AVFormatContext *s, AVPacket *pkt) oggstream->link_pts_offset = ogg_link_start(oggstream, pkt); oggstream->link_start_pending = 0; } + link_pts = pkt->pts - oggstream->link_pts_offset; - ret = ogg_buffer_packet(s, st, pkt, pkt->pts - oggstream->link_pts_offset); + if (oggstream->start_pending) { + ret = ogg_start_link(s, st, pkt, link_pts); + if (ret <= 0) + return ret; + } + + ret = ogg_buffer_packet(s, st, pkt, link_pts); if (ret < 0) return ret; diff --git a/libavformat/ogg/muxer.h b/libavformat/ogg/muxer.h index 18e0a0f56e4..143c740b3dd 100644 --- a/libavformat/ogg/muxer.h +++ b/libavformat/ogg/muxer.h @@ -56,6 +56,7 @@ typedef struct OGGStreamContext { int64_t granule_delta_mask; int64_t granule_key_mask; ///< bits all clear on keyframes, 0 when no granule marks one int packet_granule_offset; ///< added to the end timestamp of a packet without packet_granule + int codec_start_trim; ///< start trim of the input headers, the encoder priming int eos; unsigned page_count; ///< number of page buffered OGGPage page; ///< current page @@ -64,6 +65,9 @@ typedef struct OGGStreamContext { int packet_seen; ///< true when packets have been submitted int link_start_pending; ///< the next packet sets link_pts_offset int64_t link_pts_offset; ///< input timestamp at which the current link starts presenting + /** No packet of the link is written yet, so its header pages are held. */ + int start_pending; + AVPacket *primer; ///< latest pre-roll packet, held to prime the decoder } OGGStreamContext; /** @@ -76,11 +80,17 @@ struct OGGMuxerMapping { int optional_extradata; ///< the headers can be built without extradata int new_links; ///< a new link can replace the current one int priming_packet; ///< the first packet of a link only primes the decoder + int start_preroll; ///< samples the decoder needs to converge when starting mid-stream /** * Build the header packets in OGGStreamContext.header, adjusting the * stream time base when the codec sets its own. */ int (*init)(AVFormatContext *s, AVStream *st); + /** + * Write the start trim of the link into OGGStreamContext.header, and into + * the granules when they count it. + */ + void (*set_start_trim)(AVStream *st, int64_t trim); /** * @return why a new link cannot start with this packet, NULL if it can. * When the callback is NULL, the codec imposes no additional diff --git a/libavformat/ogg/muxer/opus.c b/libavformat/ogg/muxer/opus.c index 90fbf788d73..1c472288c11 100644 --- a/libavformat/ogg/muxer/opus.c +++ b/libavformat/ogg/muxer/opus.c @@ -71,12 +71,26 @@ static int opus_init(AVFormatContext *s, AVStream *st) } /* The pre-skip of the link's OpusHead, in the 48 kHz time base. */ - oggstream->packet_granule_offset = AV_RL16(oggstream->header[0] + 10); + oggstream->packet_granule_offset = + oggstream->codec_start_trim = AV_RL16(oggstream->header[0] + 10); return 0; } +static void opus_set_start_trim(AVStream *st, int64_t trim) +{ + OGGStreamContext *oggstream = st->priv_data; + + /* The pre-skip field has 16 bits. */ + trim = FFMIN(trim, UINT16_MAX); + AV_WL16(oggstream->header[0] + 10, trim); + oggstream->packet_granule_offset = trim; +} + const OGGMuxerMapping ff_ogg_opus_muxer_mapping = { - .codec_id = AV_CODEC_ID_OPUS, - .new_links = 1, - .init = opus_init, + .codec_id = AV_CODEC_ID_OPUS, + .new_links = 1, + /* RFC 7845: 80 ms of pre-roll ensures convergence. */ + .start_preroll = 80 * OPUS_TIMESTAMP_RATE / 1000, + .init = opus_init, + .set_start_trim = opus_set_start_trim, }; diff --git a/tests/fate/ogg-copy-trim.mak b/tests/fate/ogg-copy-trim.mak index eb69d2f8129..04eec292738 100644 --- a/tests/fate/ogg-copy-trim.mak +++ b/tests/fate/ogg-copy-trim.mak @@ -26,13 +26,14 @@ OGG_COPY_TRIM_DEPS = OGG_MUXER OGG_DEMUXER FRAMECRC_MUXER ARESAMPLE_FILTER \ PCM_S16LE_ENCODER PCM_S16LE_MUXER FILE_PROTOCOL OGG_COPY_TRIM_AV_DEPS = MOV_DEMUXER MATROSKA_MUXER MATROSKA_DEMUXER H264_PARSER $(OGG_COPY_TRIM_DEPS) -# Opus cut 1.5 s after the start of the copy, more than the pre-skip can hold. +# Opus cut 1.5 s after the start of the copy, more than the pre-skip field can hold. OGG_COPY_TRIM_OPUS_CUT = tests/data/fate/ogg-copy-trim-opus-cut FATE_OGG_COPY_TRIM-$(call ALLYES, OPUS_DECODER $(OGG_COPY_TRIM_AV_DEPS)) += fate-ogg-copy-trim-opus-cut fate-ogg-copy-trim-opus-cut: CMD = $(call OGG_COPY_TRIM_CUT,audiomatch/tones_opus_48000_stereo.opus,1.5,$(OGG_COPY_TRIM_OPUS_CUT)) fate-ogg-copy-trim-opus-cut: CLEANFILES = $(addprefix $(OGG_COPY_TRIM_OPUS_CUT),.mkv $(OGG_COPY_TRIM_FILES)) -# Opus cut 1 s after the start of the copy, which the pre-skip can hold. +# Opus cut 1 s after the start of the copy, which the pre-skip field can hold: +# only the pre-roll the decoder needs is kept either way. OGG_COPY_TRIM_OPUS_PRESKIP = tests/data/fate/ogg-copy-trim-opus-cut-preskip FATE_OGG_COPY_TRIM-$(call ALLYES, OPUS_DECODER $(OGG_COPY_TRIM_AV_DEPS)) += fate-ogg-copy-trim-opus-cut-preskip fate-ogg-copy-trim-opus-cut-preskip: CMD = $(call OGG_COPY_TRIM_CUT,audiomatch/tones_opus_48000_stereo.opus,1.0,$(OGG_COPY_TRIM_OPUS_PRESKIP)) diff --git a/tests/ref/fate/limited_input_seek b/tests/ref/fate/limited_input_seek index 0b4500ca380..a862384d13b 100644 --- a/tests/ref/fate/limited_input_seek +++ b/tests/ref/fate/limited_input_seek @@ -1 +1 @@ -6e30db839c32436310270b69e842f096 +8b0b6985439e5d22723e201db4eafd8c diff --git a/tests/ref/fate/ogg-copy-trim-flac-cut b/tests/ref/fate/ogg-copy-trim-flac-cut index a32b6e2dc67..2fb97eafaa8 100644 --- a/tests/ref/fate/ogg-copy-trim-flac-cut +++ b/tests/ref/fate/ogg-copy-trim-flac-cut @@ -4,12 +4,12 @@ #codec_id 0: flac #sample_rate 0: 44100 #channel_layout_name 0: stereo -0, 0, 0, 4096, 11736, 0xdb0073ee -0, 4096, 4096, 4096, 12370, 0xca19a21f -0, 8192, 8192, 4096, 11903, 0xb493bd69 -0, 12288, 12288, 4096, 11242, 0x3240cab4 -0, 16384, 16384, 4096, 11372, 0x96b59721 -0, 16384, 16384, 4096, 10645, 0x05b74a79 +0, 0, 0, 4096, 8113, 0x9acd9413 +0, 4096, 4096, 4096, 8521, 0x2070136c +0, 8192, 8192, 4096, 8687, 0xe48f4c68 +0, 12288, 12288, 4096, 10582, 0xe2d10986 +0, 16384, 16384, 4096, 11175, 0xda5442f1 +0, 20480, 20480, 4096, 8563, 0x31a3565e streams.stream.0.initial_padding=0 streams.stream.0.start_time="0.000000" -decoded copy: 360448 bytes, decoded source: 131128 bytes +decoded copy: 147456 bytes, decoded source: 131128 bytes diff --git a/tests/ref/fate/ogg-copy-trim-opus-cut b/tests/ref/fate/ogg-copy-trim-opus-cut index a231f6ee95f..1c545de4e09 100644 --- a/tests/ref/fate/ogg-copy-trim-opus-cut +++ b/tests/ref/fate/ogg-copy-trim-opus-cut @@ -1,9 +1,15 @@ -#extradata 0: 19, 0x38100445 +#extradata 0: 19, 0x38880454 #tb 0: 1/48000 #media_type 0: audio #codec_id 0: opus #sample_rate 0: 48000 #channel_layout_name 0: stereo -streams.stream.0.initial_padding=312 -streams.stream.0.start_time="N/A" -decoded copy: 0 bytes, decoded source: 96000 bytes +0, -4152, -4152, 960, 180, 0x79645678, S=1, Skip Samples, 10, 0x02c00048 +0, -3192, -3192, 960, 487, 0xc0f1e554 +0, -2232, -2232, 960, 358, 0xd494b064 +0, -1272, -1272, 960, 371, 0x2db6bd3c +0, -312, -312, 960, 358, 0x2e7ea8e9 +0, 648, 648, 960, 366, 0xcc95b454 +streams.stream.0.initial_padding=4152 +streams.stream.0.start_time="0.000000" +decoded copy: 96000 bytes, decoded source: 96000 bytes diff --git a/tests/ref/fate/ogg-copy-trim-opus-cut-preskip b/tests/ref/fate/ogg-copy-trim-opus-cut-preskip index a40c2256247..0a423d828ed 100644 --- a/tests/ref/fate/ogg-copy-trim-opus-cut-preskip +++ b/tests/ref/fate/ogg-copy-trim-opus-cut-preskip @@ -1,15 +1,15 @@ -#extradata 0: 19, 0x38100445 +#extradata 0: 19, 0x38880454 #tb 0: 1/48000 #media_type 0: audio #codec_id 0: opus #sample_rate 0: 48000 #channel_layout_name 0: stereo -0, -312, -312, 960, 354, 0xa2a7b0c8, S=1, Skip Samples, 10, 0x02390039 -0, 648, 648, 960, 366, 0xf507c0ab -0, 1608, 1608, 960, 359, 0x0379b6ca -0, 2568, 2568, 960, 377, 0x7a62c054 -0, 3528, 3528, 960, 369, 0x08c1c043 -0, 4488, 4488, 960, 357, 0x689bab86 -streams.stream.0.initial_padding=312 -streams.stream.0.start_time="-0.980000" -decoded copy: 380160 bytes, decoded source: 192000 bytes +0, -4152, -4152, 960, 474, 0x770cec85, S=1, Skip Samples, 10, 0x02c00048 +0, -3192, -3192, 960, 163, 0x9f1655d7 +0, -2232, -2232, 960, 144, 0xd7764913 +0, -1272, -1272, 960, 152, 0x2c155282 +0, -312, -312, 960, 138, 0xd62641ed +0, 648, 648, 960, 150, 0x070f46d0 +streams.stream.0.initial_padding=4152 +streams.stream.0.start_time="0.000000" +decoded copy: 192000 bytes, decoded source: 192000 bytes diff --git a/tests/ref/fate/ogg-copy-trim-vorbis-cut b/tests/ref/fate/ogg-copy-trim-vorbis-cut index 6e86d6a39d9..6cdd44e52ae 100644 --- a/tests/ref/fate/ogg-copy-trim-vorbis-cut +++ b/tests/ref/fate/ogg-copy-trim-vorbis-cut @@ -4,12 +4,12 @@ #codec_id 0: vorbis #sample_rate 0: 44100 #channel_layout_name 0: stereo -0, -576, -576, 576, 267, 0x5b048bc9, S=1, Strings Metadata, 121, 0xc2832208 -0, 0, 0, 1024, 275, 0x777286f5 -0, 1024, 1024, 1024, 201, 0x0d106229 -0, 2048, 2048, 1024, 204, 0xc1766392 -0, 3072, 3072, 1024, 212, 0xde0b6eca -0, 4096, 4096, 1024, 224, 0xdba36edc +0, -1024, -1024, 1024, 248, 0x101c6ae2, S=1, Strings Metadata, 121, 0xc2832208 +0, 0, 0, 1024, 251, 0x80357455 +0, 1024, 1024, 1024, 255, 0x8f756ca4 +0, 2048, 2048, 1024, 243, 0xf2b36de7 +0, 3072, 3072, 1024, 251, 0x9a657269 +0, 4096, 4096, 1024, 256, 0xce2a727f streams.stream.0.initial_padding=0 streams.stream.0.start_time="0.000000" -decoded copy: 351744 bytes, decoded source: 125240 bytes +decoded copy: 126464 bytes, decoded source: 125240 bytes diff --git a/tests/ref/fate/ogg-copy-trim-vorbis-edit-list b/tests/ref/fate/ogg-copy-trim-vorbis-edit-list index 239edff9315..c7d60f6607a 100644 --- a/tests/ref/fate/ogg-copy-trim-vorbis-edit-list +++ b/tests/ref/fate/ogg-copy-trim-vorbis-edit-list @@ -4,12 +4,12 @@ #codec_id 0: vorbis #sample_rate 0: 44100 #channel_layout_name 0: stereo -0, -45056, -45056, 1024, 270, 0x9f588389, S=1, Strings Metadata, 62, 0xc650157d -0, -44032, -44032, 1024, 259, 0xcfeb7fad -0, -43008, -43008, 1024, 260, 0xf34d83b8 -0, -41984, -41984, 1024, 274, 0xbe048550 -0, -40960, -40960, 1024, 273, 0x36e68bf3 -0, -39936, -39936, 1024, 289, 0x0d399472 +0, -1024, -1024, 1024, 254, 0x01b176f0, S=1, Strings Metadata, 62, 0xc650157d +0, 0, 0, 1024, 269, 0x8d648490 +0, 1024, 1024, 1024, 264, 0x04d88285 +0, 2048, 2048, 1024, 280, 0x5d3f91a5 +0, 3072, 3072, 1024, 272, 0x3cac8969 +0, 4096, 4096, 1024, 280, 0x77d9936b streams.stream.0.initial_padding=0 streams.stream.0.start_time="0.000000" -decoded copy: 1130496 bytes, decoded source: 954368 bytes +decoded copy: 954368 bytes, decoded source: 954368 bytes -- 2.54.1 >From 9607d7d4dbf1957521ed2c69f2d5c84fd7480e8f Mon Sep 17 00:00:00 2001 From: Romain Beauxis <[email protected]> Date: Sun, 22 Nov 2026 15:17:25 -0500 Subject: [PATCH 6/8] avformat/ogg/muxer: end the first Vorbis page after a start trim A Vorbis start trim is signalled by the granule position of the first audio page, and an end trim by that of the last one. When both are the same page, as in a short stream, the start trim cannot be told apart. End the page holding the first presented packet when the link starts with a trim, as the Vorbis I specification asks of streams starting at a positive time. --- libavformat/ogg/muxer.c | 10 ++++++++++ libavformat/ogg/muxer.h | 1 + tests/ref/fate/limited_input_seek | 2 +- tests/ref/fate/ogg-copy-trim-vorbis-cut | 8 ++++---- 4 files changed, 16 insertions(+), 5 deletions(-) diff --git a/libavformat/ogg/muxer.c b/libavformat/ogg/muxer.c index 3598aa94a19..147fef0198c 100644 --- a/libavformat/ogg/muxer.c +++ b/libavformat/ogg/muxer.c @@ -701,6 +701,9 @@ static int ogg_start_link(AVFormatContext *s, AVStream *st, } oggstream->start_pending = 0; + /* A start trim in the granules must not share its page with the end trim, + * see Vorbis I A.2. */ + oggstream->end_start_page = mapping->priming_packet && link_pts < 0; ogg_write_pages(s, 2); if (oggstream->primer) { @@ -747,6 +750,13 @@ static int ogg_write_packet_internal(AVFormatContext *s, AVPacket *pkt) if (ret < 0) return ret; + if (oggstream->end_start_page) { + oggstream->end_start_page = 0; + ret = ogg_buffer_page(s, oggstream); + if (ret < 0) + return ret; + } + ogg_write_pages(s, 0); return 0; diff --git a/libavformat/ogg/muxer.h b/libavformat/ogg/muxer.h index 143c740b3dd..58fe62a71d8 100644 --- a/libavformat/ogg/muxer.h +++ b/libavformat/ogg/muxer.h @@ -68,6 +68,7 @@ typedef struct OGGStreamContext { /** No packet of the link is written yet, so its header pages are held. */ int start_pending; AVPacket *primer; ///< latest pre-roll packet, held to prime the decoder + int end_start_page; ///< end the page after the next packet } OGGStreamContext; /** diff --git a/tests/ref/fate/limited_input_seek b/tests/ref/fate/limited_input_seek index a862384d13b..f89a7465fab 100644 --- a/tests/ref/fate/limited_input_seek +++ b/tests/ref/fate/limited_input_seek @@ -1 +1 @@ -8b0b6985439e5d22723e201db4eafd8c +de34f4b48dc76b8a530bb2ef2ff5b2cf diff --git a/tests/ref/fate/ogg-copy-trim-vorbis-cut b/tests/ref/fate/ogg-copy-trim-vorbis-cut index 6cdd44e52ae..c2736c8b1f7 100644 --- a/tests/ref/fate/ogg-copy-trim-vorbis-cut +++ b/tests/ref/fate/ogg-copy-trim-vorbis-cut @@ -6,10 +6,10 @@ #channel_layout_name 0: stereo 0, -1024, -1024, 1024, 248, 0x101c6ae2, S=1, Strings Metadata, 121, 0xc2832208 0, 0, 0, 1024, 251, 0x80357455 -0, 1024, 1024, 1024, 255, 0x8f756ca4 -0, 2048, 2048, 1024, 243, 0xf2b36de7 -0, 3072, 3072, 1024, 251, 0x9a657269 -0, 4096, 4096, 1024, 256, 0xce2a727f +0, 718, 718, 1024, 255, 0x8f756ca4 +0, 1742, 1742, 1024, 243, 0xf2b36de7 +0, 2766, 2766, 1024, 251, 0x9a657269 +0, 3790, 3790, 1024, 256, 0xce2a727f streams.stream.0.initial_padding=0 streams.stream.0.start_time="0.000000" decoded copy: 126464 bytes, decoded source: 125240 bytes -- 2.54.1 >From d6b9b296cad579d4a68d761876de1ca97225a10f Mon Sep 17 00:00:00 2001 From: Romain Beauxis <[email protected]> Date: Sun, 22 Nov 2026 08:35:23 -0500 Subject: [PATCH 7/8] avformat/ogg/demuxer/vorbis: trim the samples before time zero The granule position of the first audio page places the first packet. A negative start time means the samples before time zero are discarded (Vorbis I, A.2), which was ignored: the stream was assumed to start at zero and played the trimmed samples. Derive the timestamp of the first packet from the first page, unless it is also the last one, and trim the samples before zero from the packet following it, the priming packet returning none. --- libavformat/ogg/demuxer/vorbis.c | 40 +++++++++++++++++++------ tests/ref/fate/ogg-copy-trim-vorbis-cut | 6 ++-- 2 files changed, 34 insertions(+), 12 deletions(-) diff --git a/libavformat/ogg/demuxer/vorbis.c b/libavformat/ogg/demuxer/vorbis.c index 2b4ccea305c..16a75928393 100644 --- a/libavformat/ogg/demuxer/vorbis.c +++ b/libavformat/ogg/demuxer/vorbis.c @@ -69,6 +69,7 @@ struct oggvorbis_private { int ts_offset; int64_t last_page_granule; int eos_page_duration; + int start_trim; ///< samples before time zero, trimmed after the priming packet }; static int fixup_vorbis_headers(AVFormatContext *as, @@ -399,11 +400,18 @@ static int vorbis_packet(AVFormatContext *s, int idx) bos = os->page_start && os->page_pos == priv->bos_page_pos; - if (bos || (os->page_start && os->lastpts == AV_NOPTS_VALUE)) + if (bos || (os->page_start && os->lastpts == AV_NOPTS_VALUE)) { av_vorbis_parse_reset(priv->vp); + priv->start_trim = 0; + } if (bos) priv->last_page_granule = 0; + /* The priming packet returns no samples, so the packet after it carries + * the start trim. */ + os->start_trimming = priv->start_trim; + priv->start_trim = 0; + duration = av_vorbis_parse_frame_flags(priv->vp, os->buf + os->pstart, 1, &flags); if (duration < 0) { os->pflags |= AV_PKT_FLAG_CORRUPT; @@ -421,16 +429,30 @@ static int vorbis_packet(AVFormatContext *s, int idx) /* Initial position shift to account for encoder delay, or page scan * to recover the page start timestamp after a seek. */ if (bos) { - if (os->lastpts != AV_NOPTS_VALUE) { - if (s->streams[idx]->start_time == AV_NOPTS_VALUE) { - s->streams[idx]->start_time = os->lastpts; - if (s->streams[idx]->duration != AV_NOPTS_VALUE) - s->streams[idx]->duration -= s->streams[idx]->start_time; + int64_t first_pts = AV_NOPTS_VALUE; + + if (!(os->flags & OGG_FLAG_EOS) && (int64_t)os->granule > 0) + first_pts = vorbis_page_start_pts(os, priv); + + /* Samples before time zero are discarded, see Vorbis I A.2. */ + if (first_pts != AV_NOPTS_VALUE && first_pts + priv->ts_offset < 0) { + priv->start_trim = FFMIN(-(first_pts + priv->ts_offset), INT_MAX); + os->lastpts = + os->lastdts = first_pts; + if (s->streams[idx]->start_time == AV_NOPTS_VALUE) + s->streams[idx]->start_time = 0; + } else { + if (os->lastpts != AV_NOPTS_VALUE) { + if (s->streams[idx]->start_time == AV_NOPTS_VALUE) { + s->streams[idx]->start_time = os->lastpts; + if (s->streams[idx]->duration != AV_NOPTS_VALUE) + s->streams[idx]->duration -= s->streams[idx]->start_time; + } + os->lastpts -= priv->ts_offset; } - os->lastpts -= priv->ts_offset; + if (os->lastdts != AV_NOPTS_VALUE) + os->lastdts -= priv->ts_offset; } - if (os->lastdts != AV_NOPTS_VALUE) - os->lastdts -= priv->ts_offset; } else if (os->page_start && os->lastpts == AV_NOPTS_VALUE && !(os->flags & OGG_FLAG_EOS) && (int64_t)os->granule >= 0) { os->lastpts = diff --git a/tests/ref/fate/ogg-copy-trim-vorbis-cut b/tests/ref/fate/ogg-copy-trim-vorbis-cut index c2736c8b1f7..79ca1b33938 100644 --- a/tests/ref/fate/ogg-copy-trim-vorbis-cut +++ b/tests/ref/fate/ogg-copy-trim-vorbis-cut @@ -4,12 +4,12 @@ #codec_id 0: vorbis #sample_rate 0: 44100 #channel_layout_name 0: stereo -0, -1024, -1024, 1024, 248, 0x101c6ae2, S=1, Strings Metadata, 121, 0xc2832208 -0, 0, 0, 1024, 251, 0x80357455 +0, -1330, -1330, 1024, 248, 0x101c6ae2, S=1, Strings Metadata, 121, 0xc2832208 +0, -306, -306, 1024, 251, 0x80357455, S=1, Skip Samples, 10, 0x01fd0033 0, 718, 718, 1024, 255, 0x8f756ca4 0, 1742, 1742, 1024, 243, 0xf2b36de7 0, 2766, 2766, 1024, 251, 0x9a657269 0, 3790, 3790, 1024, 256, 0xce2a727f streams.stream.0.initial_padding=0 streams.stream.0.start_time="0.000000" -decoded copy: 126464 bytes, decoded source: 125240 bytes +decoded copy: 125240 bytes, decoded source: 125240 bytes -- 2.54.1 >From bee92cfcf50e3a6d028b88a402c35a34c495e824 Mon Sep 17 00:00:00 2001 From: Romain Beauxis <[email protected]> Date: Sun, 22 Nov 2026 15:17:53 -0500 Subject: [PATCH 8/8] avformat/ogg/muxer: warn about link starts the mapping cannot represent Report the pre-roll packets dropped at the start of a link, the samples before the start that a mapping without start trim plays, and a cut keeping less pre-roll than the decoder needs to converge, which plays unconverged samples at the start. A cut is told apart from the start of the stream by the trim side data of its first packet, which only the start of the stream carries: a cut on a packet boundary can keep exactly as much pre-roll as the encoder priming. A link starting after time zero comes from a stream cut upstream, most likely on purpose, and is not reported. --- libavformat/ogg/muxer.c | 48 +++++++++++++++++++++++++++++++++++++---- libavformat/ogg/muxer.h | 2 ++ 2 files changed, 46 insertions(+), 4 deletions(-) diff --git a/libavformat/ogg/muxer.c b/libavformat/ogg/muxer.c index 147fef0198c..50c2d6aed23 100644 --- a/libavformat/ogg/muxer.c +++ b/libavformat/ogg/muxer.c @@ -664,6 +664,26 @@ static void ogg_set_start_trim(AVFormatContext *s, AVStream *st, int64_t trim) } } +/** + * Warn about the start of a link the mapping cannot represent, link_pts + * being that of its first packet kept after the priming one. + */ +static void ogg_warn_link_start(AVFormatContext *s, const AVStream *st, + int64_t link_pts) +{ + const OGGStreamContext *oggstream = st->priv_data; + const OGGMuxerMapping *mapping = oggstream->mapping; + const char *codec = avcodec_get_name(st->codecpar->codec_id); + + if (oggstream->dropped_packets) + av_log(s, AV_LOG_WARNING, "Stream %d: dropped %d %s pre-roll packets " + "(%"PRId64" samples) before the start\n", st->index, + oggstream->dropped_packets, codec, oggstream->dropped_samples); + if (link_pts < 0 && !mapping->set_start_trim && !mapping->priming_packet) + av_log(s, AV_LOG_WARNING, "Stream %d: %"PRId64" samples before the start " + "cannot be trimmed in %s and are played\n", st->index, -link_pts, codec); +} + /** * Handle a packet of a link of which no packet is written yet. Pre-roll * beyond what the decoder needs is dropped, except a priming packet; the @@ -682,22 +702,42 @@ static int ogg_start_link(AVFormatContext *s, AVStream *st, int ret; if (link_pts < 0 && link_pts + pkt->duration <= -preroll) { + const AVPacket *dropped = pkt; + + if (mapping->priming_packet) { + if (!oggstream->primer && !(oggstream->primer = av_packet_alloc())) + return AVERROR(ENOMEM); + dropped = oggstream->primer->data ? oggstream->primer : NULL; + } + if (dropped) { + oggstream->dropped_packets++; + oggstream->dropped_samples += dropped->duration; + } if (!mapping->priming_packet) return 0; - if (!oggstream->primer && !(oggstream->primer = av_packet_alloc())) - return AVERROR(ENOMEM); av_packet_unref(oggstream->primer); return av_packet_ref(oggstream->primer, pkt); } - if (link_pts < 0 && mapping->set_start_trim) { + ogg_warn_link_start(s, st, link_pts); + + if (link_pts <= 0 && mapping->set_start_trim) { int64_t trim = -link_pts, packet_trim = ogg_packet_start_trim(pkt); /* Timestamps may be rounded to the input time base, while the trim of * the packet is exact. A cut differs from it by whole packets. */ if (packet_trim && FFABS(trim - packet_trim) < pkt->duration) trim = packet_trim; - ogg_set_start_trim(s, st, trim); + /* The first packet of the stream carries the encoder priming as its + * trim; a cut does not, even when its pre-roll has the same length. A + * later start was cut upstream, which is not for the muxer to report. */ + if (packet_trim != oggstream->codec_start_trim && trim < mapping->start_preroll) + av_log(s, AV_LOG_WARNING, "Stream %d: the link starts mid-stream with " + "%"PRId64" samples of pre-roll, less than the %d %s needs to " + "converge\n", st->index, trim, mapping->start_preroll, + avcodec_get_name(st->codecpar->codec_id)); + if (link_pts < 0) + ogg_set_start_trim(s, st, trim); } oggstream->start_pending = 0; diff --git a/libavformat/ogg/muxer.h b/libavformat/ogg/muxer.h index 58fe62a71d8..5732ec795d4 100644 --- a/libavformat/ogg/muxer.h +++ b/libavformat/ogg/muxer.h @@ -69,6 +69,8 @@ typedef struct OGGStreamContext { int start_pending; AVPacket *primer; ///< latest pre-roll packet, held to prime the decoder int end_start_page; ///< end the page after the next packet + int dropped_packets; ///< pre-roll packets dropped at the start of the link + int64_t dropped_samples; ///< duration of dropped_packets } OGGStreamContext; /** -- 2.54.1 _______________________________________________ ffmpeg-devel mailing list -- [email protected] To unsubscribe send an email to [email protected]
