PR #26232 opened by Forgejo_Fairy
URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/26232
Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/26232.patch

Fixes #26167.

### Cause: every short segment is excluded from the measured maximum

Since e14cdf99905f, segments of at most 0.5 seconds do not contribute to 
`max_bitrate`. If every segment is short, final master-playlist publication 
passes zero bandwidth to `ff_hls_write_stream_info()`, which omits the variant 
despite available media and explicitly configured encoder bitrates.

The regression was verified by building e14cdf99905f and its parent, 
9246cca7f7db. The supplied 0.4-second and 0.5-second reproductions work on the 
parent and fail on the introducing commit; 0.6 seconds works on both.

### Implementation: retain a separate short-segment maximum

Track the maximum bitrate of positive-duration short segments separately. Use 
it at final publication only when the existing longer-segment maximum is 
unavailable. This preserves the existing exclusion of short trailing segments 
when longer segments are present, retains the positive-duration guard, and 
avoids substituting an average for differing short-segment bitrates.

This fixes variant omission without redesigning the existing peak-estimation 
policy. No public API changes or new media samples are required.

### Tests: boundary, multiple short segments and unchanged controls

Five sample-free FATE tests use native MPEG-2 video encoding with bitexact 
integer DCT and one encoder thread. Expected playlist output is stored in 
`tests/ref/fate/`.

The tests are `fate-hls-master-short`, `fate-hls-master-boundary`, 
`fate-hls-master-long`, `fate-hls-master-all-short` and 
`fate-hls-master-short-tail`. They cover 0.4, 0.5 and 0.6-second single 
segments, a one-second presentation split into 0.4/0.4/0.2-second segments, and 
a 0.6-second segment followed by a higher-bitrate one-frame tail.

The short, boundary and all-short tests fail on unpatched master. All five pass 
with the patch on arm64 and x86-64, including unchanged output for both 
controls. All 27 enabled HLS FATE tests pass on arm64 with `make fate-hlsenc 
SAMPLES=/opt/fate-suite`.

The reporter's libx264/AAC command passes after the fix at all three durations. 
An additional 48-case matrix, using 0.4-second output and `-hls_time 10`, 
passes across VOD/event/default modes, MPEG-TS/fMP4, separate files/single-file 
output, and combined A/V, CRF video-only, audio-only and two-video-variant 
layouts. Every corresponding unpatched case omitted its stream entries.

The reported failure also reproduces on release/7.1, release/8.0, release/8.1, 
release/9.0 and release/9.1; the tested release/4.4, release/5.1 and 
release/6.1 tips retain their variants.


>From aad2379260590c4cbdfabb84cf9d93eae4513e37 Mon Sep 17 00:00:00 2001
From: Michael Niedermayer <[email protected]>
Date: Fri, 20 Nov 2026 09:57:09 +0000
Subject: [PATCH] avformat/hlsenc: retain a bitrate fallback for short segments

The peak bitrate calculation excludes segments of at most half a second.
If every segment is that short, max_bitrate remains zero and the final
master playlist silently drops the variant, even with explicit encoder
bitrates.

Track the largest short-segment bitrate separately and use it only when
no longer segment supplied a peak bitrate. This preserves the existing
exclusion of a short tail when longer segments exist, avoids substituting
an average for a varying short-segment peak, and keeps the positive-duration
guard.

Add sample-free FATE coverage below, at and above the half-second boundary,
for multiple short segments with different bitrates, and for a longer
segment followed by a short higher-bitrate tail.

Fixes: e14cdf99905f2d1140f43d5791ecf57c64c5443e
Fixes: #26167
Assisted-by: Fairy
---
 libavformat/hlsenc.c                 | 15 +++++++++------
 tests/fate/hlsenc.mak                | 25 +++++++++++++++++++++++++
 tests/ref/fate/hls-master-all-short  |  4 ++++
 tests/ref/fate/hls-master-boundary   |  4 ++++
 tests/ref/fate/hls-master-long       |  4 ++++
 tests/ref/fate/hls-master-short      |  4 ++++
 tests/ref/fate/hls-master-short-tail |  4 ++++
 7 files changed, 54 insertions(+), 6 deletions(-)
 create mode 100644 tests/ref/fate/hls-master-all-short
 create mode 100644 tests/ref/fate/hls-master-boundary
 create mode 100644 tests/ref/fate/hls-master-long
 create mode 100644 tests/ref/fate/hls-master-short
 create mode 100644 tests/ref/fate/hls-master-short-tail

diff --git a/libavformat/hlsenc.c b/libavformat/hlsenc.c
index f2a26fc302b..de9ca2ff4d4 100644
--- a/libavformat/hlsenc.c
+++ b/libavformat/hlsenc.c
@@ -154,6 +154,7 @@ typedef struct VariantStream {
     double total_duration;
     int64_t avg_bitrate;
     int64_t max_bitrate;
+    int64_t max_short_bitrate;
 
     HLSSegment *segments;
     HLSSegment *last_segment;
@@ -1106,12 +1107,14 @@ static int hls_append_segment(struct AVFormatContext 
*s, HLSContext *hls,
 
     vs->total_size += size;
     vs->total_duration += duration;
-    if (duration > 0.5) {
-        // Don't include the final, possibly very short segment in the
-        // calculation of the max bitrate.
+    if (duration > 0) {
         int cur_bitrate = (int)(8 * size / duration);
-        if (cur_bitrate > vs->max_bitrate)
-            vs->max_bitrate = cur_bitrate;
+        // Prefer longer segments to avoid a possibly very short final one,
+        // but retain a fallback for playlists consisting only of short 
segments.
+        if (duration > 0.5)
+            vs->max_bitrate = FFMAX(vs->max_bitrate, cur_bitrate);
+        else
+            vs->max_short_bitrate = FFMAX(vs->max_short_bitrate, cur_bitrate);
     }
     if (vs->total_duration > 0)
         vs->avg_bitrate = (int)(8 * vs->total_size / vs->total_duration);
@@ -1536,7 +1539,7 @@ static int create_master_playlist(AVFormatContext *s,
         }
 
         if (final) {
-            bandwidth = vs->max_bitrate;
+            bandwidth = vs->max_bitrate ? vs->max_bitrate : 
vs->max_short_bitrate;
             avg_bandwidth = vs->avg_bitrate;
         } else {
             bandwidth = 0;
diff --git a/tests/fate/hlsenc.mak b/tests/fate/hlsenc.mak
index f57275d79ed..4e8f762d7da 100644
--- a/tests/fate/hlsenc.mak
+++ b/tests/fate/hlsenc.mak
@@ -339,6 +339,31 @@ fate-hls-fmp4-frag-keyframe-short: CMP = diff
 
 FATE_HLSENC_LAVFI-yes := $(if $(call FRAMECRC), $(FATE_HLSENC_LAVFI-yes))
 
+# Exercise the short-segment bitrate fallback, its boundary, and the
+# preference for longer segments when a short trailing segment is present.
+define FATE_HLS_MASTER_BITRATE
+tests/data/hls_master_$(1).m3u8: ffmpeg$$(PROGSSUF)$$(EXESUF) | tests/data
+       $$(M)$$(TARGET_EXEC) $$(TARGET_PATH)/$$< -nostdin \
+       -f lavfi -i "testsrc2=size=128x72:rate=30" -frames:v $(2) \
+       -c:v mpeg2video -g 1 -q:v 2 -threads 1 -bitexact -dct int \
+       -f hls -hls_time $(3) -hls_playlist_type vod \
+       -master_pl_name hls_master_$(1)_master.m3u8 \
+       -hls_segment_filename $$(TARGET_PATH)/tests/data/hls_master_$(1)_%d.ts \
+       $$(TARGET_PATH)/tests/data/hls_master_$(1).m3u8 2>/dev/null
+
+FATE_HLSENC_LAVFI-$$(call ALLYES, TESTSRC2_FILTER LAVFI_INDEV 
WRAPPED_AVFRAME_DECODER MPEG2VIDEO_ENCODER HLS_MUXER MPEGTS_MUXER 
FILE_PROTOCOL) += fate-hls-master-$(1)
+fate-hls-master-$(1): tests/data/hls_master_$(1).m3u8
+fate-hls-master-$(1): CLEANFILES = tests/data/hls_master_$(1).m3u8 
tests/data/hls_master_$(1)_master.m3u8 tests/data/hls_master_$(1)_*.ts
+fate-hls-master-$(1): CMD = sed -n /./p 
$$(TARGET_PATH)/tests/data/hls_master_$(1)_master.m3u8
+fate-hls-master-$(1): CMP = diff
+endef
+
+$(eval $(call FATE_HLS_MASTER_BITRATE,short,12,10))
+$(eval $(call FATE_HLS_MASTER_BITRATE,boundary,15,10))
+$(eval $(call FATE_HLS_MASTER_BITRATE,long,18,10))
+$(eval $(call FATE_HLS_MASTER_BITRATE,all-short,30,0.4))
+$(eval $(call FATE_HLS_MASTER_BITRATE,short-tail,19,0.6))
+
 FATE_FFMPEG_FFPROBE += $(FATE_HLSENC_FFMPEG_FFPROBE-yes)
 
 FATE_FFMPEG += $(FATE_HLSENC_LAVFI-yes)
diff --git a/tests/ref/fate/hls-master-all-short 
b/tests/ref/fate/hls-master-all-short
new file mode 100644
index 00000000000..0c01641fbb2
--- /dev/null
+++ b/tests/ref/fate/hls-master-all-short
@@ -0,0 +1,4 @@
+#EXTM3U
+#EXT-X-VERSION:3
+#EXT-X-STREAM-INF:BANDWIDTH=1391200,AVERAGE-BANDWIDTH=1311488,RESOLUTION=128x72
+hls_master_all-short.m3u8
diff --git a/tests/ref/fate/hls-master-boundary 
b/tests/ref/fate/hls-master-boundary
new file mode 100644
index 00000000000..37f8c76ab4a
--- /dev/null
+++ b/tests/ref/fate/hls-master-boundary
@@ -0,0 +1,4 @@
+#EXTM3U
+#EXT-X-VERSION:3
+#EXT-X-STREAM-INF:BANDWIDTH=1254336,AVERAGE-BANDWIDTH=1254336,RESOLUTION=128x72
+hls_master_boundary.m3u8
diff --git a/tests/ref/fate/hls-master-long b/tests/ref/fate/hls-master-long
new file mode 100644
index 00000000000..d23d5002b37
--- /dev/null
+++ b/tests/ref/fate/hls-master-long
@@ -0,0 +1,4 @@
+#EXTM3U
+#EXT-X-VERSION:3
+#EXT-X-STREAM-INF:BANDWIDTH=1268373,AVERAGE-BANDWIDTH=1268373,RESOLUTION=128x72
+hls_master_long.m3u8
diff --git a/tests/ref/fate/hls-master-short b/tests/ref/fate/hls-master-short
new file mode 100644
index 00000000000..c03b85474be
--- /dev/null
+++ b/tests/ref/fate/hls-master-short
@@ -0,0 +1,4 @@
+#EXTM3U
+#EXT-X-VERSION:3
+#EXT-X-STREAM-INF:BANDWIDTH=1244560,AVERAGE-BANDWIDTH=1244560,RESOLUTION=128x72
+hls_master_short.m3u8
diff --git a/tests/ref/fate/hls-master-short-tail 
b/tests/ref/fate/hls-master-short-tail
new file mode 100644
index 00000000000..a3e225b91f5
--- /dev/null
+++ b/tests/ref/fate/hls-master-short-tail
@@ -0,0 +1,4 @@
+#EXTM3U
+#EXT-X-VERSION:3
+#EXT-X-STREAM-INF:BANDWIDTH=1268373,AVERAGE-BANDWIDTH=1275233,RESOLUTION=128x72
+hls_master_short-tail.m3u8
-- 
2.54.1

_______________________________________________
ffmpeg-devel mailing list -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to