Commit 8575cd3e79 for ffmpeg
commit 8575cd3e793973367f247cf34c60a8f7ccee5807
Author: Romain Beauxis <romain.beauxis@gmail.com>
Date: Thu Nov 26 20:54:20 2026 -0600
avformat/ogg/muxer: follow the audio packet durations within the timestamp rounding
Audio granules count samples, while timestamps read from Matroska are
rounded to the millisecond. The granules then carried the rounding of
each packet, which moved the start of an Opus stream by up to 24 samples
and made its first page claim fewer samples than it completes. Count
from the end of the previous packet while the timestamp agrees with it
within its rounding.
diff --git a/libavformat/ogg/muxer.c b/libavformat/ogg/muxer.c
index 5c9dedea13..55764c290b 100644
--- a/libavformat/ogg/muxer.c
+++ b/libavformat/ogg/muxer.c
@@ -454,6 +454,7 @@ static int ogg_init(AVFormatContext *s)
oggstream->page.stream_index = i;
oggstream->mapping = mapping;
oggstream->link_anchor = AV_NOPTS_VALUE;
+ oggstream->next_link_pts = AV_NOPTS_VALUE;
if (mapping->priv_data_size) {
oggstream->priv_data = av_mallocz(mapping->priv_data_size);
if (!oggstream->priv_data)
@@ -697,11 +698,20 @@ static int ogg_write_packet_internal(AVFormatContext *s, AVPacket *pkt)
oggstream->link_anchor = AV_NOPTS_VALUE;
}
+ /* Audio granules count samples: the packet durations are followed while
+ * the timestamps agree with them within their rounding. */
+ if (oggstream->next_link_pts != AV_NOPTS_VALUE &&
+ 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);
if (ret < 0)
return ret;
if (new_link && pkt->duration > 0)
oggstream->link_anchor = link_pts + pkt->duration;
+ if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO)
+ oggstream->next_link_pts = pkt->duration > 0 ? link_pts + pkt->duration
+ : AV_NOPTS_VALUE;
ogg_write_pages(s, 0);
diff --git a/libavformat/ogg/muxer.h b/libavformat/ogg/muxer.h
index 2c7f67d676..889e8689d1 100644
--- a/libavformat/ogg/muxer.h
+++ b/libavformat/ogg/muxer.h
@@ -66,6 +66,7 @@ typedef struct OGGStreamContext {
int64_t link_pts_offset; ///< input timestamp at which the current link starts presenting
/** End of the first packet of a later link, which the second one anchors to. */
int64_t link_anchor;
+ int64_t next_link_pts; ///< end of the previous audio packet in its link
} OGGStreamContext;
/**
diff --git a/tests/ref/fate/ogg-copy-trim-opus-concat b/tests/ref/fate/ogg-copy-trim-opus-concat
index 7d0fd764ad..c1040663c4 100644
--- a/tests/ref/fate/ogg-copy-trim-opus-concat
+++ b/tests/ref/fate/ogg-copy-trim-opus-concat
@@ -11,6 +11,6 @@
0, 3528, 3528, 960, 146, 0x633c4937
0, 4488, 4488, 960, 153, 0x3d0b4f93
streams.stream.0.initial_padding=312
-streams.stream.0.start_time="0.007500"
+streams.stream.0.start_time="0.006500"
decoded copy: 199056 bytes, decoded source: 195608 bytes
timestamp gaps: 1
diff --git a/tests/ref/fate/ogg-copy-trim-opus-end b/tests/ref/fate/ogg-copy-trim-opus-end
index 30999f344f..3dae7bb72c 100644
--- a/tests/ref/fate/ogg-copy-trim-opus-end
+++ b/tests/ref/fate/ogg-copy-trim-opus-end
@@ -11,5 +11,5 @@
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"
+streams.stream.0.start_time="-0.000500"
decoded copy: 99216 bytes, decoded source: 97804 bytes
diff --git a/tests/ref/lavf-fate/vp8.ogg b/tests/ref/lavf-fate/vp8.ogg
index 8a5ef0909a..21d6b93735 100644
--- a/tests/ref/lavf-fate/vp8.ogg
+++ b/tests/ref/lavf-fate/vp8.ogg
@@ -1,3 +1,3 @@
-c396feea7c83ccc361b94af00967da2a *tests/data/lavf-fate/lavf.vp8.ogg
+8b27c48021c147a9ff9f6a82b31d4203 *tests/data/lavf-fate/lavf.vp8.ogg
95009 tests/data/lavf-fate/lavf.vp8.ogg
tests/data/lavf-fate/lavf.vp8.ogg CRC=0xfb8d7a66