PR #22701 opened by Marton Balint (cus)
URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/22701
Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/22701.patch

As suggested by Andreas Rheinhardt.

Supersedes: #22536.



>From ba40daabe04fdae5fd9b26b476ed5f6a56377102 Mon Sep 17 00:00:00 2001
From: Marton Balint <[email protected]>
Date: Sun, 29 Mar 2026 15:05:26 +0200
Subject: [PATCH 1/2] avformat/hlsenc: dynamically allocate segment uris along
 with the segment struct

As suggested by Andreas Rheinhardt.

Supersedes: #22536.

Signed-off-by: Marton Balint <[email protected]>
---
 libavformat/hlsenc.c | 51 +++++++++++++++++++++++++++-----------------
 1 file changed, 31 insertions(+), 20 deletions(-)

diff --git a/libavformat/hlsenc.c b/libavformat/hlsenc.c
index 8eaab37a2c..4c7518c85b 100644
--- a/libavformat/hlsenc.c
+++ b/libavformat/hlsenc.c
@@ -74,8 +74,8 @@ typedef enum {
 #define POSTFIX_PATTERN "_%d"
 
 typedef struct HLSSegment {
-    char filename[MAX_URL_SIZE];
-    char sub_filename[MAX_URL_SIZE];
+    const char *filename;
+    const char *sub_filename;
     double duration; /* in seconds */
     int discont;
     int64_t pos;
@@ -84,11 +84,13 @@ typedef struct HLSSegment {
     int64_t keyframe_size;
     unsigned var_stream_idx;
 
-    char key_uri[LINE_BUFFER_SIZE + 1];
+    const char *key_uri;
     char iv_string[KEYSIZE*2 + 1];
 
     struct HLSSegment *next;
     double discont_program_date_time;
+
+    char buf[]; /* for filename, sub_filename and key_uri */
 } HLSSegment;
 
 typedef enum HLSFlags {
@@ -1033,13 +1035,12 @@ static int hls_append_segment(struct AVFormatContext 
*s, HLSContext *hls,
                               VariantStream *vs, double duration, int64_t pos,
                               int64_t size)
 {
-    HLSSegment *en = av_malloc(sizeof(*en));
+    HLSSegment en0 = {0};
+    HLSSegment *en = &en0;
     const char  *filename;
     int byterange_mode = (hls->flags & HLS_SINGLE_FILE) || (hls->max_seg_size 
> 0);
     int ret;
-
-    if (!en)
-        return AVERROR(ENOMEM);
+    AVBPrint bp;
 
     vs->total_size += size;
     vs->total_duration += duration;
@@ -1055,10 +1056,8 @@ static int hls_append_segment(struct AVFormatContext *s, 
HLSContext *hls,
 
     en->var_stream_idx = vs->var_stream_idx;
     ret = sls_flags_filename_process(s, hls, vs, en, duration, pos, size);
-    if (ret < 0) {
-        av_freep(&en);
+    if (ret < 0)
         return ret;
-    }
 
     filename = av_basename(vs->avf->url);
 
@@ -1069,21 +1068,16 @@ static int hls_append_segment(struct AVFormatContext 
*s, HLSContext *hls,
         && !byterange_mode) {
         av_log(hls, AV_LOG_WARNING, "Duplicated segment filename detected: 
%s\n", filename);
     }
-    av_strlcpy(en->filename, filename, sizeof(en->filename));
 
-    if (vs->has_subtitle)
-        av_strlcpy(en->sub_filename, av_basename(vs->vtt_avf->url), 
sizeof(en->sub_filename));
-    else
-        en->sub_filename[0] = '\0';
+    av_bprint_init(&bp, 0, AV_BPRINT_SIZE_UNLIMITED);
+    av_bprintf(&bp, "%s%c", filename, 0);
+    av_bprintf(&bp, "%s%c", vs->has_subtitle ? av_basename(vs->vtt_avf->url) : 
"", 0);
 
     en->duration = duration;
     en->pos      = pos;
     en->size     = size;
     en->keyframe_pos      = vs->video_keyframe_pos;
     en->keyframe_size     = vs->video_keyframe_size;
-    en->next     = NULL;
-    en->discont  = 0;
-    en->discont_program_date_time = 0;
 
     if (vs->discontinuity) {
         en->discont = 1;
@@ -1091,10 +1085,27 @@ static int hls_append_segment(struct AVFormatContext 
*s, HLSContext *hls,
     }
 
     if (hls->key_info_file || hls->encrypt) {
-        av_strlcpy(en->key_uri, vs->key_uri, sizeof(en->key_uri));
+        av_bprintf(&bp, "%s%c", vs->key_uri, 0);
         av_strlcpy(en->iv_string, vs->iv_string, sizeof(en->iv_string));
+    } else {
+        av_bprint_chars(&bp, 0, 1);
     }
 
+    en = av_malloc(sizeof(*en) + bp.len);
+    if (!av_bprint_is_complete(&bp) || !en) {
+        av_freep(&en);
+        av_bprint_finalize(&bp, NULL);
+        return AVERROR(ENOMEM);
+    }
+    *en = en0;
+    memcpy(en->buf, bp.str, bp.len);
+    av_bprint_finalize(&bp, NULL);
+#define NEXT(s) ((s) + strlen(s) + 1)
+    en->filename = en->buf;
+    en->sub_filename = NEXT(en->filename);
+    en->key_uri = NEXT(en->sub_filename);
+#undef NEXT
+
     if (!vs->segments)
         vs->segments = en;
     else
@@ -1528,7 +1539,7 @@ static int hls_window(AVFormatContext *s, int last, 
VariantStream *vs)
     int is_file_proto = proto && !strcmp(proto, "file");
     int use_temp_file = is_file_proto && ((hls->flags & HLS_TEMP_FILE) || 
!(hls->pl_type == PLAYLIST_TYPE_VOD));
     static unsigned warned_non_file;
-    char *key_uri = NULL;
+    const char *key_uri = NULL;
     char *iv_string = NULL;
     AVDictionary *options = NULL;
     double prog_date_time = vs->initial_prog_date_time;
-- 
2.52.0


>From 9eaf275df004196adf46ce747930386c2b4057b5 Mon Sep 17 00:00:00 2001
From: Marton Balint <[email protected]>
Date: Sat, 4 Apr 2026 00:06:13 +0200
Subject: [PATCH 2/2] avformat/hlsenc: remove unused function parameter

Signed-off-by: Marton Balint <[email protected]>
---
 libavformat/hlsenc.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/libavformat/hlsenc.c b/libavformat/hlsenc.c
index 4c7518c85b..45c451a3a5 100644
--- a/libavformat/hlsenc.c
+++ b/libavformat/hlsenc.c
@@ -898,7 +898,7 @@ static HLSSegment *find_segment_by_filename(HLSSegment 
*segment, const char *fil
 }
 
 static int sls_flags_filename_process(struct AVFormatContext *s, HLSContext 
*hls,
-                                      VariantStream *vs, HLSSegment *en,
+                                      VariantStream *vs,
                                       double duration, int64_t pos, int64_t 
size)
 {
     if ((hls->flags & (HLS_SECOND_LEVEL_SEGMENT_SIZE | 
HLS_SECOND_LEVEL_SEGMENT_DURATION)) &&
@@ -1055,7 +1055,7 @@ static int hls_append_segment(struct AVFormatContext *s, 
HLSContext *hls,
         vs->avg_bitrate = (int)(8 * vs->total_size / vs->total_duration);
 
     en->var_stream_idx = vs->var_stream_idx;
-    ret = sls_flags_filename_process(s, hls, vs, en, duration, pos, size);
+    ret = sls_flags_filename_process(s, hls, vs, duration, pos, size);
     if (ret < 0)
         return ret;
 
-- 
2.52.0

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

Reply via email to