Commit 8576207aec for ffmpeg
commit 8576207aecfa9344f47f234bda2de4eb44fb0de8
Author: Romain Beauxis <romain.beauxis@gmail.com>
Date: Thu Dec 3 08:10:59 2026 -0600
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, which the start of
a link also needs.
diff --git a/libavformat/ogg/muxer.c b/libavformat/ogg/muxer.c
index 8ed55e5b7d..a755a4747a 100644
--- a/libavformat/ogg/muxer.c
+++ b/libavformat/ogg/muxer.c
@@ -587,18 +587,49 @@ end:
}
/**
- * Input timestamp at which a link starting with pkt starts presenting.
+ * Number of samples to skip from the start of pkt, from its side data.
*/
-static int64_t ogg_link_start(const AVPacket *pkt)
+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);
- int64_t start = pkt->pts;
- if (skip && size >= 10)
- start += AV_RL32(skip);
+ 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 AVPacket *pkt)
+{
+ return pkt->pts + ogg_packet_start_trim(pkt);
+}
+
+/**
+ * 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;
- return start;
+ oggstream->last_granule = granule;
+
+ return 0;
}
/**
@@ -615,8 +646,8 @@ 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, new_link = 0;
- int64_t granule, link_pts;
if (oggcontext->failed)
return AVERROR_INVALIDDATA;
@@ -626,12 +657,14 @@ static int ogg_write_packet_internal(AVFormatContext *s, AVPacket *pkt)
ret = ogg_check_new_link(s, pkt);
if (ret < 0 || oggcontext->ended)
return ret;
+ oggstream->packet_seen = 1;
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. */
@@ -643,15 +676,7 @@ static int ogg_write_packet_internal(AVFormatContext *s, AVPacket *pkt)
oggstream->link_anchor = AV_NOPTS_VALUE;
}
- 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, link_pts);
if (ret < 0)
return ret;
if (new_link && pkt->duration > 0)
@@ -659,9 +684,6 @@ static int ogg_write_packet_internal(AVFormatContext *s, AVPacket *pkt)
ogg_write_pages(s, 0);
- oggstream->last_granule = granule;
- oggstream->packet_seen = 1;
-
return 0;
}