Commit 86394097d0 for ffmpeg
commit 86394097d0704160bec2a639f1bd8da197e4b157
Author: Michael Niedermayer <michael@niedermayer.cc>
Date: Tue Dec 1 11:01:55 2026 -0600
avformat/segment: let a muxer skip the flush before its trailer
The flush before the trailer of each segment writes every page the Ogg
muxer holds, so the last one goes out without the EOS flag.
A muxer flag skips that flush when the trailer follows, and the Ogg
muxers set it.
Segments ending without a trailer are still flushed, as fragmented MP4
needs.
Assisted-by: Fairy
diff --git a/libavformat/mux.h b/libavformat/mux.h
index 0b69109174..459dceb614 100644
--- a/libavformat/mux.h
+++ b/libavformat/mux.h
@@ -57,6 +57,8 @@ struct AVDeviceInfoList;
* are disallowed.
*/
#define FF_OFMT_FLAG_ONLY_DEFAULT_CODECS (1 << 3)
+/** The trailer needs buffered media and must not be preceded by a flush. */
+#define FF_OFMT_FLAG_NO_PRE_TRAILER_FLUSH (1 << 4)
typedef struct FFOutputFormat {
/**
diff --git a/libavformat/ogg/muxer.c b/libavformat/ogg/muxer.c
index 55764c290b..2df84433c0 100644
--- a/libavformat/ogg/muxer.c
+++ b/libavformat/ogg/muxer.c
@@ -792,7 +792,7 @@ const FFOutputFormat ff_ogg_muxer = {
.deinit = ogg_free,
.p.flags = AVFMT_TS_NEGATIVE | AVFMT_TS_NONSTRICT,
.p.priv_class = &ogg_muxer_class,
- .flags_internal = FF_OFMT_FLAG_ALLOW_FLUSH,
+ .flags_internal = FF_OFMT_FLAG_ALLOW_FLUSH | FF_OFMT_FLAG_NO_PRE_TRAILER_FLUSH,
};
#endif
@@ -811,7 +811,7 @@ const FFOutputFormat ff_oga_muxer = {
.deinit = ogg_free,
.p.flags = AVFMT_TS_NEGATIVE,
.p.priv_class = &ogg_muxer_class,
- .flags_internal = FF_OFMT_FLAG_ALLOW_FLUSH,
+ .flags_internal = FF_OFMT_FLAG_ALLOW_FLUSH | FF_OFMT_FLAG_NO_PRE_TRAILER_FLUSH,
};
#endif
@@ -833,7 +833,7 @@ const FFOutputFormat ff_ogv_muxer = {
.deinit = ogg_free,
.p.flags = AVFMT_TS_NEGATIVE | AVFMT_TS_NONSTRICT,
.p.priv_class = &ogg_muxer_class,
- .flags_internal = FF_OFMT_FLAG_ALLOW_FLUSH,
+ .flags_internal = FF_OFMT_FLAG_ALLOW_FLUSH | FF_OFMT_FLAG_NO_PRE_TRAILER_FLUSH,
};
#endif
@@ -852,7 +852,7 @@ const FFOutputFormat ff_spx_muxer = {
.deinit = ogg_free,
.p.flags = AVFMT_TS_NEGATIVE,
.p.priv_class = &ogg_muxer_class,
- .flags_internal = FF_OFMT_FLAG_ALLOW_FLUSH,
+ .flags_internal = FF_OFMT_FLAG_ALLOW_FLUSH | FF_OFMT_FLAG_NO_PRE_TRAILER_FLUSH,
};
#endif
@@ -871,6 +871,6 @@ const FFOutputFormat ff_opus_muxer = {
.deinit = ogg_free,
.p.flags = AVFMT_TS_NEGATIVE,
.p.priv_class = &ogg_muxer_class,
- .flags_internal = FF_OFMT_FLAG_ALLOW_FLUSH,
+ .flags_internal = FF_OFMT_FLAG_ALLOW_FLUSH | FF_OFMT_FLAG_NO_PRE_TRAILER_FLUSH,
};
#endif
diff --git a/libavformat/segment.c b/libavformat/segment.c
index 4bcfc0b3b2..2011b9437e 100644
--- a/libavformat/segment.c
+++ b/libavformat/segment.c
@@ -377,10 +377,14 @@ static int segment_end(AVFormatContext *s, int write_trailer, int is_last)
if (!oc || !oc->pb)
return AVERROR(EINVAL);
- ret = av_write_frame(oc, NULL);
- /* Finish pending media before rotating a shared-header output. */
- if (ret == 0 && !write_trailer)
+ /* Ogg marks its last page EOS in its trailer. */
+ if (!write_trailer ||
+ !(ffofmt(oc->oformat)->flags_internal & FF_OFMT_FLAG_NO_PRE_TRAILER_FLUSH)) {
ret = av_write_frame(oc, NULL);
+ /* Finish pending media before rotating a shared-header output. */
+ if (ret == 0 && !write_trailer)
+ ret = av_write_frame(oc, NULL);
+ }
if (ret >= 0 && write_trailer)
ret = av_write_trailer(oc);
diff --git a/tests/ref/fate/ogg-copy-trim-opus-segment b/tests/ref/fate/ogg-copy-trim-opus-segment
index ae08458fec..836e346d13 100644
--- a/tests/ref/fate/ogg-copy-trim-opus-segment
+++ b/tests/ref/fate/ogg-copy-trim-opus-segment
@@ -1,2 +1,2 @@
-granule=96312 flags=---
-decoded copy: different
+granule=96312 flags=--e
+decoded copy: same