Commit de9ea1999d for ffmpeg
commit de9ea1999db13bca85c096b6141917da4522412f
Author: Romain Beauxis <romain.beauxis@gmail.com>
Date: Thu Dec 3 08:15:24 2026 -0600
avformat/ogg/muxer: end the first page of an Opus link starting later
The first page of an Opus link starting after zero ends when the next
packet arrives, so that its granule tells the start offset apart from
an end trim on the last page (RFC 7845 4.5) without holding back the
pages of the other streams.
A link of a single packet keeps its end trim and warns that its start
offset is lost.
The gap and offset tests now start at the timestamps of their input.
diff --git a/libavformat/ogg/muxer.c b/libavformat/ogg/muxer.c
index 186c602c0d..25ef6ca51c 100644
--- a/libavformat/ogg/muxer.c
+++ b/libavformat/ogg/muxer.c
@@ -204,9 +204,13 @@ static int ogg_buffer_page(AVFormatContext *s, OGGStreamContext *oggstream)
return 0;
}
+/**
+ * @param keep_open leave the page completing the packet open until the
+ * caller ends it
+ */
static int ogg_buffer_data(AVFormatContext *s, AVStream *st,
const uint8_t *data, unsigned size, int64_t granule,
- int header)
+ int header, int keep_open)
{
OGGStreamContext *oggstream = st->priv_data;
OGGContext *ogg = s->priv_data;
@@ -269,9 +273,10 @@ static int ogg_buffer_data(AVFormatContext *s, AVStream *st,
time_base, AV_TIME_BASE_Q);
if (page->segments_count == 255) {
- if ((ret = ogg_buffer_page(s, oggstream)) < 0)
+ if ((!keep_open || i < total_segments) &&
+ (ret = ogg_buffer_page(s, oggstream)) < 0)
return ret;
- } else if (!header) {
+ } else if (!header && !keep_open) {
if (ogg->pref_duration > 0 && next - start >= ogg->pref_duration) {
if ((ret = ogg_buffer_page(s, oggstream)) < 0)
return ret;
@@ -513,7 +518,7 @@ static int ogg_write_header(AVFormatContext *s)
oggstream = s->streams[j]->priv_data;
oggstream->page.flags |= 2; // bos
ret = ogg_buffer_data(s, s->streams[j], oggstream->header[0],
- oggstream->header_len[0], 0, 1);
+ oggstream->header_len[0], 0, 1, 0);
if (ret < 0)
return ret;
/* 255 lacing values end the page within ogg_buffer_data(). */
@@ -527,7 +532,7 @@ static int ogg_write_header(AVFormatContext *s)
for (i = 1; i < 3; i++) {
if (oggstream->header_len[i]) {
ret = ogg_buffer_data(s, st, oggstream->header[i],
- oggstream->header_len[i], 0, 1);
+ oggstream->header_len[i], 0, 1, 0);
if (ret < 0)
return ret;
}
@@ -668,9 +673,11 @@ static int64_t ogg_packet_end_trim(const AVPacket *pkt)
/**
* Buffer a data packet at its link timestamp link_pts.
+ *
+ * @param keep_open see ogg_buffer_data()
*/
static int ogg_buffer_packet(AVFormatContext *s, AVStream *st,
- const AVPacket *pkt, int64_t link_pts)
+ const AVPacket *pkt, int64_t link_pts, int keep_open)
{
OGGStreamContext *oggstream = st->priv_data;
int64_t granule;
@@ -686,7 +693,7 @@ static int ogg_buffer_packet(AVFormatContext *s, AVStream *st,
oggstream->page.start_granule = oggstream->mapping->packet_granule ? link_pts :
link_pts + oggstream->packet_granule_offset;
- ret = ogg_buffer_data(s, st, pkt->data, pkt->size, granule, 0);
+ ret = ogg_buffer_data(s, st, pkt->data, pkt->size, granule, 0, keep_open);
if (ret < 0)
return ret;
@@ -871,6 +878,10 @@ static int ogg_start_link(AVFormatContext *s, AVStream *st,
/* What the start trim cannot skip is presented from the start. */
coded = FFMAX(coded, coded + trim - written);
oggstream->link_pts_offset = pkt->pts - coded;
+ /* A start in the granules must not share its page with the end trim,
+ * see RFC 7845 4.5. */
+ if (mapping->set_start_trim)
+ oggstream->opus_start_offset = FFMAX(coded + oggstream->packet_granule_offset, 0);
return 1;
}
@@ -893,6 +904,15 @@ static int ogg_write_packet_internal(AVFormatContext *s, AVPacket *pkt)
return ret;
oggstream->packet_seen = 1;
+ /* Keep a positive-start page open until the next packet needs its own
+ * page. A queued last page can block other streams. */
+ if (oggstream->opus_start_offset) {
+ if (oggstream->page.segments_count &&
+ (ret = ogg_buffer_page(s, oggstream)) < 0)
+ return ret;
+ oggstream->opus_start_offset = 0;
+ }
+
if (oggstream->mapping->packet_samples) {
int samples = oggstream->mapping->packet_samples(st, pkt);
/* Without explicit end padding, a duration shorter than the coded
@@ -940,7 +960,7 @@ static int ogg_write_packet_internal(AVFormatContext *s, AVPacket *pkt)
FFABS(link_pts - oggstream->next_link_pts) <= ogg_timestamp_rounding(st)))
link_pts = oggstream->next_link_pts;
- ret = ogg_buffer_packet(s, st, pkt, link_pts);
+ ret = ogg_buffer_packet(s, st, pkt, link_pts, !!oggstream->opus_start_offset);
if (ret < 0)
return ret;
if (oggstream->mapping->packet_samples) {
@@ -990,6 +1010,15 @@ static void ogg_apply_end_trim(AVFormatContext *s, AVStream *st)
last = &p->page;
if (last) {
last->granule -= end_trim;
+ /* A single Opus page cannot carry both a start offset and an end
+ * trim, see RFC 7845 4.5. The pre-skip must not exceed the granule. */
+ if (oggstream->opus_start_offset) {
+ last->granule = FFMAX(last->granule - oggstream->opus_start_offset,
+ oggstream->packet_granule_offset);
+ av_log(s, AV_LOG_WARNING, "Stream %d: keeping the Opus end trim, "
+ "the positive start offset of %"PRId64" samples is lost\n",
+ st->index, oggstream->opus_start_offset);
+ }
} else {
av_log(s, AV_LOG_WARNING, "Stream %d: the last page was flushed before "
"the end of the link, its end trim of %"PRId64" samples is played\n",
diff --git a/libavformat/ogg/muxer.h b/libavformat/ogg/muxer.h
index 7e08d4c58e..f70deabff9 100644
--- a/libavformat/ogg/muxer.h
+++ b/libavformat/ogg/muxer.h
@@ -91,6 +91,7 @@ typedef struct OGGStreamContext {
int64_t coded_start;
int64_t last_dropped_pts; ///< input link timestamp of the last dropped packet
int64_t start_skip; ///< samples the input decoder still skips before the start
+ int64_t opus_start_offset; ///< positive start offset until a second packet arrives
int dropped_packets; ///< pre-roll packets dropped at the start of the link
int64_t dropped_samples; ///< duration of dropped_packets
} OGGStreamContext;
diff --git a/tests/ref/fate/ogg-copy-trim-opus-end-only b/tests/ref/fate/ogg-copy-trim-opus-end-only
index 1cc3734c42..a8b0d2a32f 100644
--- a/tests/ref/fate/ogg-copy-trim-opus-end-only
+++ b/tests/ref/fate/ogg-copy-trim-opus-end-only
@@ -1 +1 @@
-decoded copy: different
+decoded copy: same
diff --git a/tests/ref/fate/ogg-copy-trim-opus-gap b/tests/ref/fate/ogg-copy-trim-opus-gap
index 8bba5bf899..b29aaca544 100644
--- a/tests/ref/fate/ogg-copy-trim-opus-gap
+++ b/tests/ref/fate/ogg-copy-trim-opus-gap
@@ -1,3 +1,3 @@
-frame|pts=0|nb_samples=960
-frame|pts=960|nb_samples=960
-stream|initial_padding=0|start_time=0.000000
+frame|pts=648|nb_samples=960
+frame|pts=1608|nb_samples=960
+stream|initial_padding=0|start_time=0.013500
diff --git a/tests/ref/fate/ogg-copy-trim-opus-gap-partial b/tests/ref/fate/ogg-copy-trim-opus-gap-partial
index 43e9168954..771480729a 100644
--- a/tests/ref/fate/ogg-copy-trim-opus-gap-partial
+++ b/tests/ref/fate/ogg-copy-trim-opus-gap-partial
@@ -1,9 +1,9 @@
-frame|pts=0|nb_samples=648
-frame|pts=648|nb_samples=960
-frame|pts=1608|nb_samples=960
-frame|pts=2568|nb_samples=960
-frame|pts=3528|nb_samples=960
+frame|pts=3840|nb_samples=648
frame|pts=4488|nb_samples=960
frame|pts=5448|nb_samples=960
frame|pts=6408|nb_samples=960
-stream|initial_padding=3192|start_time=0.000000
+frame|pts=7368|nb_samples=960
+frame|pts=8328|nb_samples=960
+frame|pts=9288|nb_samples=960
+frame|pts=10248|nb_samples=960
+stream|initial_padding=3192|start_time=0.080000
diff --git a/tests/ref/fate/ogg-copy-trim-opus-mp4-one-offset b/tests/ref/fate/ogg-copy-trim-opus-mp4-one-offset
index d0221e6c61..da83066579 100644
--- a/tests/ref/fate/ogg-copy-trim-opus-mp4-one-offset
+++ b/tests/ref/fate/ogg-copy-trim-opus-mp4-one-offset
@@ -4,7 +4,7 @@
#codec_id 0: opus
#sample_rate 0: 48000
#channel_layout_name 0: stereo
-0, -312, -312, 960, 489, 0xb843f4b8, F=0x21, S=1, Skip Samples, 10, 0x02390039
+0, -312, -312, 792, 489, 0xb843f4b8, F=0x21, S=1, Skip Samples, 10, 0x062900e1
streams.stream.0.initial_padding=312
streams.stream.0.start_time="0.000000"
-decoded copy: 2592 bytes, decoded source: 1920 bytes
+decoded copy: 1920 bytes, decoded source: 1920 bytes
diff --git a/tests/ref/fate/ogg-copy-trim-opus-offset-page b/tests/ref/fate/ogg-copy-trim-opus-offset-page
index d7a5dcc45c..e2c9726795 100644
--- a/tests/ref/fate/ogg-copy-trim-opus-offset-page
+++ b/tests/ref/fate/ogg-copy-trim-opus-offset-page
@@ -11,5 +11,5 @@
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: 386592 bytes, decoded source: 384000 bytes
+streams.stream.0.start_time="0.500000"
+decoded copy: 384000 bytes, decoded source: 384000 bytes
diff --git a/tests/ref/fate/ogg-copy-trim-opus-offset-priming b/tests/ref/fate/ogg-copy-trim-opus-offset-priming
index 698304abb5..af47920b46 100644
--- a/tests/ref/fate/ogg-copy-trim-opus-offset-priming
+++ b/tests/ref/fate/ogg-copy-trim-opus-offset-priming
@@ -11,5 +11,5 @@
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: 384768 bytes, decoded source: 384000 bytes
+streams.stream.0.start_time="0.004000"
+decoded copy: 384000 bytes, decoded source: 384000 bytes