PR #26253 opened by Baptiste Coudurier (bcoudurier)
URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/26253
Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/26253.patch

Two pre-existing fMP4 init section bugs:
- single_file with hls_fmp4_init_resend crashed on a NULL filename; the init 
section is already in the file, so it is no longer resent.
- init section write errors were ignored, leaving a truncated init file and a 
successful exit.

🤖 Generated with [Claude Code](https://claude.com/claude-code)



From 6ee834794c5720651fec0dfbc3de897ea65f160e Mon Sep 17 00:00:00 2001
From: Baptiste Coudurier <[email protected]>
Date: Sat, 21 Nov 2026 11:58:14 -0800
Subject: [PATCH 1/2] avformat/hlsenc: do not resend the init section in single
 file mode

It is part of the single file. The resend opened a NULL filename and crashed.

Signed-off-by: Baptiste Coudurier <[email protected]>
---
 libavformat/hlsenc.c                       |  5 +++--
 tests/fate/hlsenc.mak                      | 16 ++++++++++++++++
 tests/ref/fate/hls-fmp4-single-file-resend |  4 ++++
 3 files changed, 23 insertions(+), 2 deletions(-)
 create mode 100644 tests/ref/fate/hls-fmp4-single-file-resend

diff --git a/libavformat/hlsenc.c b/libavformat/hlsenc.c
index ab529d54311..8ab4d3100d5 100644
--- a/libavformat/hlsenc.c
+++ b/libavformat/hlsenc.c
@@ -530,7 +530,7 @@ static int hls_write_init_file(AVFormatContext *s, 
VariantStream *vs)
         ret = media_pb->error;
         goto fail;
     }
-    if (hls->resend_init_file) {
+    if (hls->resend_init_file && !(hls->flags & HLS_SINGLE_FILE)) {
         init_buffer = av_memdup(buffer, init_size);
         if (!init_buffer) {
             ret = AVERROR(ENOMEM);
@@ -2704,7 +2704,8 @@ static int hls_write_packet(AVFormatContext *s, AVPacket 
*pkt)
             }
         }
 
-        if (hls->resend_init_file && hls->segment_type == SEGMENT_TYPE_FMP4) {
+        if (hls->resend_init_file && hls->segment_type == SEGMENT_TYPE_FMP4 &&
+            !(hls->flags & HLS_SINGLE_FILE)) {
             ret = hls_init_file_resend(s, vs);
             if (ret < 0) {
                 av_freep(&old_filename);
diff --git a/tests/fate/hlsenc.mak b/tests/fate/hlsenc.mak
index f57275d79ed..85b594395f3 100644
--- a/tests/fate/hlsenc.mak
+++ b/tests/fate/hlsenc.mak
@@ -337,6 +337,22 @@ fate-hls-fmp4-frag-keyframe-short: CLEANFILES = 
tests/data/hls_fmp4_frag_keyfram
 fate-hls-fmp4-frag-keyframe-short: CMD = sed -n -e /^\#EXT-X-MAP:/p -e 
/^\#EXT-X-BYTERANGE:/p 
$(TARGET_PATH)/tests/data/hls_fmp4_frag_keyframe_short.m3u8
 fate-hls-fmp4-frag-keyframe-short: CMP = diff
 
+# The init section is part of the single file, there is nothing to resend.
+tests/data/hls_fmp4_single_file_resend.m3u8: ffmpeg$(PROGSSUF)$(EXESUF) | 
tests/data
+       $(M)$(TARGET_EXEC) $(TARGET_PATH)/$< -nostdin \
+       -f lavfi -i "testsrc2=size=128x72:rate=2:d=3" -map 0:v \
+       -c:v mpeg2video -g 1 -bitexact -dct int -f hls \
+       -hls_time 1 -hls_list_size 0 -hls_segment_type fmp4 \
+       -hls_flags single_file -hls_fmp4_init_resend 1 \
+       -hls_segment_filename 
$(TARGET_PATH)/tests/data/hls_fmp4_single_file_resend.mp4 \
+       $(TARGET_PATH)/$@ 2>/dev/null
+
+FATE_HLSENC_LAVFI-$(call ALLYES, TESTSRC2_FILTER LAVFI_INDEV 
WRAPPED_AVFRAME_DECODER MPEG2VIDEO_ENCODER HLS_MUXER MP4_MUXER FILE_PROTOCOL) 
+= fate-hls-fmp4-single-file-resend
+fate-hls-fmp4-single-file-resend: tests/data/hls_fmp4_single_file_resend.m3u8
+fate-hls-fmp4-single-file-resend: CLEANFILES = 
tests/data/hls_fmp4_single_file_resend.m3u8 
tests/data/hls_fmp4_single_file_resend.mp4
+fate-hls-fmp4-single-file-resend: CMD = sed -n -e /^\#EXT-X-MAP:/p -e 
/^\#EXT-X-BYTERANGE:/p 
$(TARGET_PATH)/tests/data/hls_fmp4_single_file_resend.m3u8
+fate-hls-fmp4-single-file-resend: CMP = diff
+
 FATE_HLSENC_LAVFI-yes := $(if $(call FRAMECRC), $(FATE_HLSENC_LAVFI-yes))
 
 FATE_FFMPEG_FFPROBE += $(FATE_HLSENC_FFMPEG_FFPROBE-yes)
diff --git a/tests/ref/fate/hls-fmp4-single-file-resend 
b/tests/ref/fate/hls-fmp4-single-file-resend
new file mode 100644
index 00000000000..0750ee19f6b
--- /dev/null
+++ b/tests/ref/fate/hls-fmp4-single-file-resend
@@ -0,0 +1,4 @@
+#EXT-X-MAP:URI="hls_fmp4_single_file_resend.mp4",BYTERANGE="810@0"
+#EXT-X-BYTERANGE:9440@810
+#EXT-X-BYTERANGE:11183@10250
+#EXT-X-BYTERANGE:11045@21433
-- 
2.54.1


From 2e965dd8b123af68e0164cda39e953e08b57d09f Mon Sep 17 00:00:00 2001
From: Baptiste Coudurier <[email protected]>
Date: Sat, 21 Nov 2026 11:58:37 -0800
Subject: [PATCH 2/2] avformat/hlsenc: check init section write errors

A failed write left a truncated init file and the muxer reported success.

Signed-off-by: Baptiste Coudurier <[email protected]>
---
 libavformat/hlsenc.c | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/libavformat/hlsenc.c b/libavformat/hlsenc.c
index 8ab4d3100d5..4fa1479fcbc 100644
--- a/libavformat/hlsenc.c
+++ b/libavformat/hlsenc.c
@@ -539,6 +539,12 @@ static int hls_write_init_file(AVFormatContext *s, 
VariantStream *vs)
     }
 
     avio_write(vs->out, buffer, init_size);
+    avio_flush(vs->out);
+    if (vs->out->error < 0) {
+        ret = vs->out->error;
+        av_freep(&init_buffer);
+        goto fail;
+    }
     ffio_free_dyn_buf(&oc->pb);
     oc->pb = media_pb;
     vs->init_buffer = init_buffer;
@@ -2440,6 +2446,8 @@ static int hls_init_file_resend(AVFormatContext *s, 
VariantStream *vs)
     if (ret < 0)
         return ret;
     avio_write(vs->out, vs->init_buffer, vs->init_range_length);
+    avio_flush(vs->out);
+    ret = vs->out->error;
     hlsenc_io_close(s, &vs->out, hls->fmp4_init_filename);
 
     return ret;
-- 
2.54.1

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

Reply via email to