Commit 5de003807b for ffmpeg
commit 5de003807baaf9359ab84073b894a6be6f84bef0
Author: Romain Beauxis <romain.beauxis@gmail.com>
Date: Sun Nov 22 15:15:42 2026 -0500
avformat/ogg/muxer: restart the timestamps of each audio link
Each link of a chained audio stream counts its granule positions from
its own start, where the demuxer restarts its timestamps.
A new link takes its timestamps from where it starts presenting, past
the start trim of its first packet.
The first packet of a link read from a chained Ogg file still carries
the timestamps of the previous link, so the second packet anchors the
link when the two disagree by more than the rounding of the timestamps.
diff --git a/libavformat/ogg/muxer.c b/libavformat/ogg/muxer.c
index 4513f01396..8ed55e5b7d 100644
--- a/libavformat/ogg/muxer.c
+++ b/libavformat/ogg/muxer.c
@@ -425,6 +425,7 @@ static int ogg_init(AVFormatContext *s)
oggstream->page.stream_index = i;
oggstream->mapping = mapping;
+ oggstream->link_anchor = AV_NOPTS_VALUE;
if (mapping->priv_data_size) {
oggstream->priv_data = av_mallocz(mapping->priv_data_size);
if (!oggstream->priv_data)
@@ -575,6 +576,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:
@@ -582,13 +586,37 @@ end:
return ret;
}
+/**
+ * Input timestamp at which a link starting with pkt starts presenting.
+ */
+static int64_t ogg_link_start(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);
+
+ return start;
+}
+
+/**
+ * Largest error of timestamps rounded by the input: Matroska rounds them to
+ * the millisecond.
+ */
+static int64_t ogg_timestamp_rounding(const AVStream *st)
+{
+ return av_rescale_q(1, (AVRational){ 1, 1000 }, st->time_base);
+}
+
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;
+ int ret, new_link = 0;
+ int64_t granule, link_pts;
if (oggcontext->failed)
return AVERROR_INVALIDDATA;
@@ -599,17 +627,35 @@ 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(pkt);
+ oggstream->link_start_pending = 0;
+ new_link = 1;
+ }
+ link_pts = pkt->pts - oggstream->link_pts_offset;
+ /* The first packet of a later link may carry the timestamps of the
+ * previous link. */
+ if (oggstream->link_anchor != AV_NOPTS_VALUE) {
+ if (FFABS(link_pts - oggstream->link_anchor) > ogg_timestamp_rounding(st)) {
+ oggstream->link_pts_offset = pkt->pts - oggstream->link_anchor;
+ link_pts = oggstream->link_anchor;
+ }
+ oggstream->link_anchor = AV_NOPTS_VALUE;
+ }
+
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)
return ret;
+ if (new_link && pkt->duration > 0)
+ oggstream->link_anchor = link_pts + pkt->duration;
ogg_write_pages(s, 0);
diff --git a/libavformat/ogg/muxer.h b/libavformat/ogg/muxer.h
index 6b0a7cf625..2c7f67d676 100644
--- a/libavformat/ogg/muxer.h
+++ b/libavformat/ogg/muxer.h
@@ -62,6 +62,10 @@ 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
+ /** End of the first packet of a later link, which the second one anchors to. */
+ int64_t link_anchor;
} OGGStreamContext;
/**
@@ -86,10 +90,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 93911a8fb6..a9155a53c2 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/vp8.c b/libavformat/ogg/muxer/vp8.c
index a61bdd3f3e..14ca3b4af0 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 a907e93059..c1f97692e9 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-flac-serial-default b/tests/ref/fate/ogg-flac-serial-default
index d2bfab60a2..1f61b2e068 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 37d6287a38..66c737fac1 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
diff --git a/tests/ref/fate/ogg-opus-copy-chained b/tests/ref/fate/ogg-opus-copy-chained
index 1e49314ee5..09a734a9e7 100644
--- a/tests/ref/fate/ogg-opus-copy-chained
+++ b/tests/ref/fate/ogg-opus-copy-chained
@@ -8,4 +8,4 @@ flags=-b-
flags=--e
copy headers: same
cut headers: different
-decoded copy: different
+decoded copy: same