Commit 0d3a0b091e for ffmpeg
commit 0d3a0b091e66f24f9b55d1208e45f4ab351a5f4f
Author: Romain Beauxis <romain.beauxis@gmail.com>
Date: Thu Dec 3 10:42:19 2026 -0600
avformat/ogg/muxer: count the samples of Vorbis packets
The Vorbis parser gives the duration of each packet, so the granules
count samples from the start of each link as they do for Opus.
The end trim of the last packet then comes off the EOS granule (Vorbis
I A.2), and the copies of limited_input_seek, lavf-fate/vp8.ogg and the
chained Vorbis tests end on the samples they decode.
The count of the first packet guesses the window before it, so a longer
duration is kept instead.
diff --git a/doc/muxers.texi b/doc/muxers.texi
index b8716890ec..dbcdc0e17a 100644
--- a/doc/muxers.texi
+++ b/doc/muxers.texi
@@ -3461,13 +3461,17 @@ The packet carrying it and the ones that follow are dropped, and their writes
still return success.
New extradata on the first packet is ignored.
-Opus granules count the coded samples of the packets.
+Opus and Vorbis granules count the coded samples of the packets.
A gap between the timestamps of two packets of a link is therefore closed.
A packet timestamp within 1 ms of that count can be taken as rounding.
Such a timestamp follows the count, which can absorb a small
@option{-output_ts_offset}.
Samples trimmed in the middle of a link cannot be signalled, they are played.
The muxer warns about those that side data trims.
+A last packet whose duration is more than 1 ms shorter than its coded samples
+trims the end of the link.
+A smaller difference is taken as rounding: an exact end trim needs discard
+padding side data.
@table @option
@item -page_duration @var{duration}
diff --git a/libavformat/ogg/muxer.c b/libavformat/ogg/muxer.c
index 25ef6ca51c..9fbc47b4b6 100644
--- a/libavformat/ogg/muxer.c
+++ b/libavformat/ogg/muxer.c
@@ -95,6 +95,8 @@ static void ogg_cleanup_stream(AVStream *st)
for (int i = 0; i < 3; i++)
av_freep(&oggstream->header[i]);
+ if (oggstream->priv_data && oggstream->mapping->deinit)
+ oggstream->mapping->deinit(st);
av_freep(&oggstream->priv_data);
}
@@ -892,7 +894,7 @@ static int ogg_write_packet_internal(AVFormatContext *s, AVPacket *pkt)
OGGContext *oggcontext = s->priv_data;
OGGStreamContext *oggstream = st->priv_data;
int64_t link_pts, end_trim = ogg_packet_end_trim(pkt);
- int ret, new_link = 0, starting, end_trim_signalled = 1;
+ int ret, new_link = 0, starting, end_trim_signalled = 1, samples = -1;
if (oggcontext->failed)
return AVERROR_INVALIDDATA;
@@ -914,7 +916,7 @@ static int ogg_write_packet_internal(AVFormatContext *s, AVPacket *pkt)
}
if (oggstream->mapping->packet_samples) {
- int samples = oggstream->mapping->packet_samples(st, pkt);
+ samples = oggstream->mapping->packet_samples(st, pkt);
/* Without explicit end padding, a duration shorter than the coded
* samples by more than its rounding ends the presentation early,
* as MP4 does. Start skip on the same packet is independent. */
@@ -956,7 +958,7 @@ static int ogg_write_packet_internal(AVFormatContext *s, AVPacket *pkt)
* the timestamps agree with them within their rounding, and always when
* the durations are read from the packets. */
if (oggstream->next_link_pts != AV_NOPTS_VALUE &&
- (oggstream->mapping->packet_samples ||
+ (samples >= 0 ||
FFABS(link_pts - oggstream->next_link_pts) <= ogg_timestamp_rounding(st)))
link_pts = oggstream->next_link_pts;
diff --git a/libavformat/ogg/muxer.h b/libavformat/ogg/muxer.h
index f70deabff9..88116e760f 100644
--- a/libavformat/ogg/muxer.h
+++ b/libavformat/ogg/muxer.h
@@ -144,6 +144,10 @@ struct OGGMuxerMapping {
* value when it cannot be read
*/
int (*packet_samples)(AVStream *st, const AVPacket *pkt);
+ /**
+ * Free what init allocated in OGGStreamContext.priv_data.
+ */
+ void (*deinit)(AVStream *st);
};
/**
diff --git a/libavformat/ogg/muxer/vorbis.c b/libavformat/ogg/muxer/vorbis.c
index b72a0d8034..e8ab13340a 100644
--- a/libavformat/ogg/muxer/vorbis.c
+++ b/libavformat/ogg/muxer/vorbis.c
@@ -19,17 +19,63 @@
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
+#include "libavcodec/vorbis_parser.h"
#include "libavformat/ogg/muxer.h"
+typedef struct VorbisMuxerContext {
+ AVVorbisParseContext *parser;
+ int window_known; ///< a packet was parsed: the next count is exact
+} VorbisMuxerContext;
+
static int vorbis_init(AVFormatContext *s, AVStream *st)
{
- return ff_ogg_build_xiph_headers(s, st, 30, 3, "vorbis", 1);
+ OGGStreamContext *oggstream = st->priv_data;
+ VorbisMuxerContext *vorbis = oggstream->priv_data;
+ int ret = ff_ogg_build_xiph_headers(s, st, 30, 3, "vorbis", 1);
+
+ if (ret < 0)
+ return ret;
+ /* Without the parser, the granules follow the timestamps. */
+ vorbis->parser = av_vorbis_parse_init(st->codecpar->extradata,
+ st->codecpar->extradata_size);
+ if (vorbis->parser)
+ av_vorbis_parse_reset(vorbis->parser);
+ vorbis->window_known = 0;
+ return 0;
+}
+
+static int vorbis_packet_samples(AVStream *st, const AVPacket *pkt)
+{
+ OGGStreamContext *oggstream = st->priv_data;
+ VorbisMuxerContext *vorbis = oggstream->priv_data;
+ int samples;
+
+ if (!vorbis->parser)
+ return -1;
+ samples = av_vorbis_parse_frame(vorbis->parser, pkt->data, pkt->size);
+ /* The window before the first packet is a guess: a longer duration
+ * tells a longer window. */
+ if (!vorbis->window_known && pkt->duration > samples)
+ samples = -1;
+ vorbis->window_known = 1;
+ return samples;
+}
+
+static void vorbis_deinit(AVStream *st)
+{
+ OGGStreamContext *oggstream = st->priv_data;
+ VorbisMuxerContext *vorbis = oggstream->priv_data;
+
+ av_vorbis_parse_free(&vorbis->parser);
}
const OGGMuxerMapping ff_ogg_vorbis_muxer_mapping = {
- .codec_id = AV_CODEC_ID_VORBIS,
- .new_links = 1,
+ .codec_id = AV_CODEC_ID_VORBIS,
+ .new_links = 1,
+ .priv_data_size = sizeof(VorbisMuxerContext),
/* A Vorbis start trim needs its own page layout, see Vorbis I A.2. */
- .start_preroll = OGG_START_PREROLL_ALL,
- .init = vorbis_init,
+ .start_preroll = OGG_START_PREROLL_ALL,
+ .init = vorbis_init,
+ .packet_samples = vorbis_packet_samples,
+ .deinit = vorbis_deinit,
};
diff --git a/tests/fate/ogg-copy-trim.mak b/tests/fate/ogg-copy-trim.mak
index 1bd488f93e..9d33bb65fc 100644
--- a/tests/fate/ogg-copy-trim.mak
+++ b/tests/fate/ogg-copy-trim.mak
@@ -139,7 +139,7 @@ fate-ogg-copy-trim-vorbis-cut-last: CLEANFILES = $(addprefix $(OGG_COPY_TRIM_VOR
# The first Vorbis link through Matroska: the primer has PTS zero, and
# the first presented packet starts later.
OGG_COPY_TRIM_VORBIS_OFFSET = tests/data/fate/ogg-copy-trim-vorbis-offset
-FATE_OGG_COPY_TRIM-$(call ALLYES, MATROSKA_MUXER MATROSKA_DEMUXER VORBIS_PARSER VORBIS_DECODER $(OGG_COPY_TRIM_DEPS)) += fate-ogg-copy-trim-vorbis-offset
+FATE_OGG_COPY_TRIM-$(call ALLYES, MATROSKA_MUXER MATROSKA_DEMUXER VORBIS_DECODER $(OGG_COPY_TRIM_DEPS)) += fate-ogg-copy-trim-vorbis-offset
fate-ogg-copy-trim-vorbis-offset: CMD = \
ffmpeg -i $(TARGET_SAMPLES)/ogg-vorbis/chained-meta.ogg -c copy -frames:a 3 -fflags +bitexact -f matroska -y $(TARGET_PATH)/$(OGG_COPY_TRIM_VORBIS_OFFSET).mkv && \
$(call OGG_COPY_TRIM,$(TARGET_PATH)/$(OGG_COPY_TRIM_VORBIS_OFFSET).mkv,,$(OGG_COPY_TRIM_VORBIS_OFFSET)) && \
diff --git a/tests/ref/fate/limited_input_seek b/tests/ref/fate/limited_input_seek
index 0b4500ca38..b54ace3505 100644
--- a/tests/ref/fate/limited_input_seek
+++ b/tests/ref/fate/limited_input_seek
@@ -1 +1 @@
-6e30db839c32436310270b69e842f096
+8f500a3ccdb5390b14fd744385a17994
diff --git a/tests/ref/fate/limited_input_seek-copyts b/tests/ref/fate/limited_input_seek-copyts
index f9b47b070e..5aecbf38c4 100644
--- a/tests/ref/fate/limited_input_seek-copyts
+++ b/tests/ref/fate/limited_input_seek-copyts
@@ -1 +1 @@
-63e0dd3f351d2fb43f0cdfd12e517496
+a6d5487102ad442679ee164a47df80b7
diff --git a/tests/ref/fate/ogg-copy-trim-vorbis-chained b/tests/ref/fate/ogg-copy-trim-vorbis-chained
index 878e91da21..b030610389 100644
--- a/tests/ref/fate/ogg-copy-trim-vorbis-chained
+++ b/tests/ref/fate/ogg-copy-trim-vorbis-chained
@@ -6,10 +6,10 @@
#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
+0, 576, 576, 1024, 85, 0x34322b3b, S=1, Skip Samples, 10, 0x00830016
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, 0x037e0095
streams.stream.0.initial_padding=0
streams.stream.0.start_time="0.000000"
-decoded copy: 6400 bytes, decoded source: 5292 bytes
+decoded copy: 5548 bytes, decoded source: 5292 bytes
diff --git a/tests/ref/fate/ogg-copy-trim-vorbis-cut-last b/tests/ref/fate/ogg-copy-trim-vorbis-cut-last
index 596af00cd3..bd2ba99aff 100644
--- a/tests/ref/fate/ogg-copy-trim-vorbis-cut-last
+++ b/tests/ref/fate/ogg-copy-trim-vorbis-cut-last
@@ -12,5 +12,5 @@
0, 4096, 4096, 1024, 399, 0xa8668fda
streams.stream.0.initial_padding=0
streams.stream.0.start_time="0.000000"
-decoded copy: 3091968 bytes, decoded source: 2052 bytes
-end of the decoded copy: different
+decoded copy: 3082020 bytes, decoded source: 2052 bytes
+end of the decoded copy: same
diff --git a/tests/ref/fate/ogg-copy-trim-vorbis-offset b/tests/ref/fate/ogg-copy-trim-vorbis-offset
index 8c2a6f8ce8..590879af89 100644
--- a/tests/ref/fate/ogg-copy-trim-vorbis-offset
+++ b/tests/ref/fate/ogg-copy-trim-vorbis-offset
@@ -6,12 +6,12 @@
#channel_layout_name 0: mono
0, -128, -128, 128, 28, 0xa5cb0b38, S=1, Strings Metadata, 79, 0xca1515d3
0, 0, 0, 576, 59, 0xf30f1b21
-0, 576, 576, 1024, 85, 0x34322b3b
+0, 576, 576, 1024, 85, 0x34322b3b, S=1, Skip Samples, 10, 0x037e0095
streams.stream.0.initial_padding=0
streams.stream.0.start_time="0.000000"
-decoded copy: 3200 bytes, decoded source: 2646 bytes
+decoded copy: 2902 bytes, decoded source: 2646 bytes
decoded copy: different
frame|pts=0|nb_samples=576
-frame|pts=576|nb_samples=1024
+frame|pts=576|nb_samples=875
frame|pts=0|nb_samples=576
-frame|pts=576|nb_samples=1024
+frame|pts=576|nb_samples=875
diff --git a/tests/ref/lavf-fate/vp8.ogg b/tests/ref/lavf-fate/vp8.ogg
index 21d6b93735..d3bbd0c6a8 100644
--- a/tests/ref/lavf-fate/vp8.ogg
+++ b/tests/ref/lavf-fate/vp8.ogg
@@ -1,3 +1,3 @@
-8b27c48021c147a9ff9f6a82b31d4203 *tests/data/lavf-fate/lavf.vp8.ogg
+8589ef98be2326f0827a0107665e785a *tests/data/lavf-fate/lavf.vp8.ogg
95009 tests/data/lavf-fate/lavf.vp8.ogg
-tests/data/lavf-fate/lavf.vp8.ogg CRC=0xfb8d7a66
+tests/data/lavf-fate/lavf.vp8.ogg CRC=0x10157a66