[FFmpeg-devel] [PATCH 3/4] avformat/hlsenc: Avoid duplicating strings when parsing

Andreas Rheinhardt andreas.rheinhardt at gmail.com
Mon May 25 22:43:01 EEST 2020


Up until now, the HLS muxer uses av_strtok() to split an input string
controlling parameters of the VariantStreams and then duplicates
parts of this string containing parameters such as the language or the
name of the VariantStream. But these parts are proper zero-terminated
strings of their own that are never modified lateron, so one can simply
use the substring as-is without creating a copy. This commit implements
this.

The same also happened for the string controlling the closed caption
groups.

Furthermore, add const to indicate that the pointers to these substrings
are not used to modify them and also to indicate that these strings are
not allocated on their own.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt at gmail.com>
---
 libavformat/hlsenc.c | 77 +++++++++++---------------------------------
 1 file changed, 19 insertions(+), 58 deletions(-)

diff --git a/libavformat/hlsenc.c b/libavformat/hlsenc.c
index 77e49b44b1..02bd5c3528 100644
--- a/libavformat/hlsenc.c
+++ b/libavformat/hlsenc.c
@@ -173,17 +173,17 @@ typedef struct VariantStream {
     unsigned int nb_streams;
     int m3u8_created; /* status of media play-list creation */
     int is_default; /* default status of audio group */
-    char *language; /* audio lauguage name */
-    char *agroup; /* audio group name */
-    char *sgroup; /* subtitle group name */
-    char *ccgroup; /* closed caption group name */
-    char *varname; // variant name
+    const char *language; /* audio lauguage name */
+    const char *agroup;   /* audio group name */
+    const char *sgroup;   /* subtitle group name */
+    const char *ccgroup;  /* closed caption group name */
+    const char *varname;  /* variant name */
 } VariantStream;
 
 typedef struct ClosedCaptionsStream {
-    char *ccgroup; /* closed caption group name */
-    char *instreamid; /* closed captions INSTREAM-ID */
-    char *language; /* closed captions langauge */
+    const char *ccgroup;    /* closed caption group name */
+    const char *instreamid; /* closed captions INSTREAM-ID */
+    const char *language;   /* closed captions langauge */
 } ClosedCaptionsStream;
 
 typedef struct HLSContext {
@@ -1290,8 +1290,8 @@ static int create_master_playlist(AVFormatContext *s,
     int ret, bandwidth;
     const char *m3u8_rel_name = NULL;
     const char *vtt_m3u8_rel_name = NULL;
-    char *ccgroup;
-    char *sgroup = NULL;
+    const char *ccgroup;
+    const char *sgroup = NULL;
     ClosedCaptionsStream *ccs;
     const char *proto = avio_find_protocol_name(hls->master_m3u8_url);
     int is_file_proto = proto && !strcmp(proto, "file");
@@ -1962,10 +1962,7 @@ static int parse_variant_stream_mapstring(AVFormatContext *s)
             char *end;
             varstr = NULL;
             if (av_strstart(keyval, "language:", &val)) {
-                av_free(vs->language);
-                vs->language = av_strdup(val);
-                if (!vs->language)
-                    return AVERROR(ENOMEM);
+                vs->language = val;
                 continue;
             } else if (av_strstart(keyval, "default:", &val)) {
                 vs->is_default = (!av_strncasecmp(val, "YES", strlen("YES")) ||
@@ -1973,28 +1970,16 @@ static int parse_variant_stream_mapstring(AVFormatContext *s)
                 hls->has_default_key = 1;
                 continue;
             } else if (av_strstart(keyval, "name:", &val)) {
-                av_free(vs->varname);
-                vs->varname = av_strdup(val);
-                if (!vs->varname)
-                    return AVERROR(ENOMEM);
+                vs->varname  = val;
                 continue;
             } else if (av_strstart(keyval, "agroup:", &val)) {
-                av_free(vs->agroup);
-                vs->agroup = av_strdup(val);
-                if (!vs->agroup)
-                    return AVERROR(ENOMEM);
+                vs->agroup   = val;
                 continue;
             } else if (av_strstart(keyval, "sgroup:", &val)) {
-                av_free(vs->sgroup);
-                vs->sgroup = av_strdup(val);
-                if (!vs->sgroup)
-                    return AVERROR(ENOMEM);
+                vs->sgroup   = val;
                 continue;
             } else if (av_strstart(keyval, "ccgroup:", &val)) {
-                av_free(vs->ccgroup);
-                vs->ccgroup = av_strdup(val);
-                if (!vs->ccgroup)
-                    return AVERROR(ENOMEM);
+                vs->ccgroup  = val;
                 continue;
             } else if (av_strstart(keyval, "v:", &val)) {
                 codec_type = AVMEDIA_TYPE_VIDEO;
@@ -2085,20 +2070,11 @@ static int parse_cc_stream_mapstring(AVFormatContext *s)
             ccstr = NULL;
 
             if (av_strstart(keyval, "ccgroup:", &val)) {
-                av_free(ccs->ccgroup);
-                ccs->ccgroup = av_strdup(val);
-                if (!ccs->ccgroup)
-                    return AVERROR(ENOMEM);
+                ccs->ccgroup    = val;
             } else if (av_strstart(keyval, "instreamid:", &val)) {
-                av_free(ccs->instreamid);
-                ccs->instreamid = av_strdup(val);
-                if (!ccs->instreamid)
-                    return AVERROR(ENOMEM);
+                ccs->instreamid = val;
             } else if (av_strstart(keyval, "language:", &val)) {
-                av_free(ccs->language);
-                ccs->language = av_strdup(val);
-                if (!ccs->language)
-                    return AVERROR(ENOMEM);
+                ccs->language   = val;
             } else {
                 av_log(s, AV_LOG_ERROR, "Invalid keyval %s\n", keyval);
                 return AVERROR(EINVAL);
@@ -2162,9 +2138,7 @@ static int update_variant_stream_info(AVFormatContext *s)
 
         //by default, the first available ccgroup is mapped to the variant stream
         if (hls->nb_ccstreams) {
-            hls->var_streams[0].ccgroup = av_strdup(hls->cc_streams[0].ccgroup);
-            if (!hls->var_streams[0].ccgroup)
-                return AVERROR(ENOMEM);
+            hls->var_streams[0].ccgroup = hls->cc_streams[0].ccgroup;
         }
 
         for (i = 0; i < s->nb_streams; i++)
@@ -2576,19 +2550,6 @@ static void hls_deinit(AVFormatContext *s)
         hls_free_segments(vs->old_segments);
         av_freep(&vs->m3u8_name);
         av_freep(&vs->streams);
-        av_freep(&vs->agroup);
-        av_freep(&vs->sgroup);
-        av_freep(&vs->language);
-        av_freep(&vs->ccgroup);
-        av_freep(&vs->varname);
-    }
-
-    for (i = 0; i < hls->nb_ccstreams; i++) {
-        ClosedCaptionsStream *ccs = &hls->cc_streams[i];
-
-        av_freep(&ccs->ccgroup);
-        av_freep(&ccs->instreamid);
-        av_freep(&ccs->language);
     }
 
     ff_format_io_close(s, &hls->m3u8_out);
-- 
2.20.1



More information about the ffmpeg-devel mailing list