[FFmpeg-devel] [PATCH 16/16] avformat/hlsenc: Cosmetics

Andreas Rheinhardt andreas.rheinhardt at gmail.com
Mon Dec 16 02:04:18 EET 2019


Mainly includes reindentation, moving variables into smaller scopes (in
particular when this allows to remove unnecessary initializations) and
also returning directly (i.e. without a goto fail when possible).

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

diff --git a/libavformat/hlsenc.c b/libavformat/hlsenc.c
index 62a52b176f..c5246bfa62 100644
--- a/libavformat/hlsenc.c
+++ b/libavformat/hlsenc.c
@@ -1638,7 +1638,7 @@ static int hls_start(AVFormatContext *s, VariantStream *vs)
             }
             ff_format_set_url(oc, filename);
         }
-        if ( vs->vtt_basename) {
+        if (vs->vtt_basename) {
             char *filename = NULL;
             if (replace_int_data_in_filename(&filename,
 #if FF_API_HLS_WRAP
@@ -1777,10 +1777,8 @@ static int validate_name(int nb_vs, const char *fn)
     char *fn_dup = NULL;
     int ret = 0;
 
-    if (!fn) {
-        ret = AVERROR(EINVAL);
-        goto fail;
-    }
+    if (!fn)
+        return AVERROR(EINVAL);
 
     fn_dup = av_strdup(fn);
     if (!fn_dup)
@@ -1814,14 +1812,12 @@ static int format_name(const char *buf, char **s, int index, const char *varname
     int ret = 0;
 
     orig_buf_dup = av_strdup(buf);
-    if (!orig_buf_dup) {
-        ret = AVERROR(ENOMEM);
-        goto fail;
-    }
+    if (!orig_buf_dup)
+        return AVERROR(ENOMEM);
 
     if (!av_stristr(buf, "%v")) {
         *s = orig_buf_dup;
-        return ret;
+        return 0;
     }
 
     if (!varname) {
@@ -2106,7 +2102,6 @@ static int parse_cc_stream_mapstring(AVFormatContext *s)
 static int update_variant_stream_info(AVFormatContext *s)
 {
     HLSContext *hls = s->priv_data;
-    unsigned int i;
     int ret = 0;
 
     if (hls->cc_stream_map) {
@@ -2128,19 +2123,17 @@ static int update_variant_stream_info(AVFormatContext *s)
         hls->var_streams[0].nb_streams = s->nb_streams;
         hls->var_streams[0].streams = av_mallocz(sizeof(AVStream *) *
                                             hls->var_streams[0].nb_streams);
-        if (!hls->var_streams[0].streams) {
+        if (!hls->var_streams[0].streams)
             return AVERROR(ENOMEM);
-        }
 
         //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) {
+            if (!hls->var_streams[0].ccgroup)
                 return AVERROR(ENOMEM);
-            }
         }
 
-        for (i = 0; i < s->nb_streams; i++)
+        for (int i = 0; i < s->nb_streams; i++)
             hls->var_streams[0].streams[i] = s->streams[i];
     }
     return 0;
@@ -2192,10 +2185,9 @@ static int hls_write_header(AVFormatContext *s)
 {
     HLSContext *hls = s->priv_data;
     int ret, i, j;
-    VariantStream *vs = NULL;
 
     for (i = 0; i < hls->nb_varstreams; i++) {
-        vs = &hls->var_streams[i];
+        VariantStream *vs = &hls->var_streams[i];
 
         ret = avformat_write_header(vs->avf, NULL);
         if (ret < 0)
@@ -2496,13 +2488,9 @@ static int hls_write_packet(AVFormatContext *s, AVPacket *pkt)
 static void hls_deinit(AVFormatContext *s)
 {
     HLSContext *hls = s->priv_data;
-    int i = 0;
-    AVFormatContext *vtt_oc = NULL;
-    VariantStream *vs = NULL;
 
-    for (i = 0; i < hls->nb_varstreams; i++) {
-        vs = &hls->var_streams[i];
-        vtt_oc = vs->vtt_avf;
+    for (int i = 0; i < hls->nb_varstreams; i++) {
+        VariantStream *vs = &hls->var_streams[i];
 
         av_freep(&vs->basename);
         av_freep(&vs->base_output_dirname);
@@ -2510,7 +2498,7 @@ static void hls_deinit(AVFormatContext *s)
         av_freep(&vs->vtt_basename);
         av_freep(&vs->vtt_m3u8_name);
 
-        avformat_free_context(vtt_oc);
+        avformat_free_context(vs->vtt_avf);
         avformat_free_context(vs->avf);
 
         hls_free_segments(vs->segments);
@@ -2524,8 +2512,9 @@ static void hls_deinit(AVFormatContext *s)
         av_freep(&vs->varname);
     }
 
-    for (i = 0; i < hls->nb_ccstreams; i++) {
+    for (int 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);
@@ -2542,24 +2531,17 @@ static void hls_deinit(AVFormatContext *s)
 static int hls_write_trailer(struct AVFormatContext *s)
 {
     HLSContext *hls = s->priv_data;
-    AVFormatContext *oc = NULL;
-    AVFormatContext *vtt_oc = NULL;
-    char *old_filename = NULL;
-    const char *proto = NULL;
-    int use_temp_file = 0;
-    int i;
     int ret = 0;
-    VariantStream *vs = NULL;
     AVDictionary *options = NULL;
     int range_length, byterange_mode;
 
-    for (i = 0; i < hls->nb_varstreams; i++) {
+    for (int i = 0; i < hls->nb_varstreams; i++) {
         char *filename = NULL;
-        vs = &hls->var_streams[i];
-        oc = vs->avf;
-        vtt_oc = vs->vtt_avf;
-        old_filename = av_strdup(oc->url);
-        use_temp_file = 0;
+        VariantStream *vs = &hls->var_streams[i];
+        AVFormatContext *oc = vs->avf;
+        AVFormatContext *vtt_oc = vs->vtt_avf;
+        char *old_filename = av_strdup(oc->url);
+        int use_temp_file = 0;
 
         if (!old_filename) {
             return AVERROR(ENOMEM);
@@ -2576,7 +2558,7 @@ static int hls_write_trailer(struct AVFormatContext *s)
             return AVERROR(ENOMEM);
         }
 
-        if ( hls->segment_type == SEGMENT_TYPE_FMP4) {
+        if (hls->segment_type == SEGMENT_TYPE_FMP4) {
             int range_length = 0;
             if (!vs->init_range_length) {
                 uint8_t *buffer = NULL;
@@ -2633,7 +2615,7 @@ failed:
         av_freep(&filename);
         av_write_trailer(oc);
         if (oc->url[0]) {
-            proto = avio_find_protocol_name(oc->url);
+            const char *proto = avio_find_protocol_name(oc->url);
             use_temp_file = proto && !strcmp(proto, "file") && (hls->flags & HLS_TEMP_FILE);
         }
 
@@ -2677,11 +2659,8 @@ failed:
 static int hls_init(AVFormatContext *s)
 {
     int ret = 0;
-    int i = 0;
-    int j = 0;
     HLSContext *hls = s->priv_data;
     const char *pattern = "%d.ts";
-    VariantStream *vs = NULL;
     int basename_size = 0;
     const char *pattern_localtime_fmt = get_default_pattern_localtime_fmt(s);
     char *p = NULL;
@@ -2694,13 +2673,12 @@ static int hls_init(AVFormatContext *s)
     if (ret < 0) {
         av_log(s, AV_LOG_ERROR, "Variant stream info update failed with status %x\n",
                ret);
-        goto fail;
+        return ret;
     }
     //TODO: Updates needed to encryption functionality with periodic re-key when more than one variant streams are present
     if (hls->nb_varstreams > 1 && hls->flags & HLS_PERIODIC_REKEY) {
-        ret = AVERROR(EINVAL);
         av_log(s, AV_LOG_ERROR, "Periodic re-key not supported when more than one variant streams are present\n");
-        goto fail;
+        return AVERROR(EINVAL);
     }
 
     if (!hls->method && http_base_proto) {
@@ -2709,24 +2687,24 @@ static int hls_init(AVFormatContext *s)
 
     ret = validate_name(hls->nb_varstreams, s->url);
     if (ret < 0)
-        goto fail;
+        return ret;
 
     if (hls->segment_filename) {
         ret = validate_name(hls->nb_varstreams, hls->segment_filename);
         if (ret < 0)
-            goto fail;
+            return ret;
     }
 
     if (av_strcasecmp(hls->fmp4_init_filename, "init.mp4")) {
         ret = validate_name(hls->nb_varstreams, hls->fmp4_init_filename);
         if (ret < 0)
-            goto fail;
+            return ret;
     }
 
     if (hls->subtitle_filename) {
         ret = validate_name(hls->nb_varstreams, hls->subtitle_filename);
         if (ret < 0)
-            goto fail;
+            return ret;
     }
 
     if (hls->master_pl_name) {
@@ -2734,7 +2712,7 @@ static int hls_init(AVFormatContext *s)
         if (ret < 0) {
             av_log(s, AV_LOG_ERROR, "Master stream info update failed with status %x\n",
                    ret);
-            goto fail;
+            return ret;
         }
     }
 
@@ -2760,24 +2738,24 @@ static int hls_init(AVFormatContext *s)
 
     hls->recording_time = (hls->init_time ? hls->init_time : hls->time) * AV_TIME_BASE;
 
-        if (hls->flags & HLS_SPLIT_BY_TIME && hls->flags & HLS_INDEPENDENT_SEGMENTS) {
-            // Independent segments cannot be guaranteed when splitting by time
-            hls->flags &= ~HLS_INDEPENDENT_SEGMENTS;
-            av_log(s, AV_LOG_WARNING,
-                   "'split_by_time' and 'independent_segments' cannot be enabled together. "
-                   "Disabling 'independent_segments' flag\n");
-        }
+    if (hls->flags & HLS_SPLIT_BY_TIME && hls->flags & HLS_INDEPENDENT_SEGMENTS) {
+        // Independent segments cannot be guaranteed when splitting by time
+        hls->flags &= ~HLS_INDEPENDENT_SEGMENTS;
+        av_log(s, AV_LOG_WARNING,
+               "'split_by_time' and 'independent_segments' cannot be "
+               "enabled together. Disabling 'independent_segments' flag\n");
+    }
 
-    for (i = 0; i < hls->nb_varstreams; i++) {
-        vs = &hls->var_streams[i];
+    for (int i = 0; i < hls->nb_varstreams; i++) {
+        VariantStream *vs = &hls->var_streams[i];
 
         ret = format_name(s->url, &vs->m3u8_name, i, vs->varname);
         if (ret < 0)
-            goto fail;
+            return ret;
 
-        vs->sequence       = hls->start_sequence;
-        vs->start_pts      = AV_NOPTS_VALUE;
-        vs->end_pts      = AV_NOPTS_VALUE;
+        vs->sequence  = hls->start_sequence;
+        vs->start_pts = AV_NOPTS_VALUE;
+        vs->end_pts   = AV_NOPTS_VALUE;
         vs->current_segment_final_filename_fmt[0] = '\0';
 
         if (hls->flags & HLS_PROGRAM_DATE_TIME) {
@@ -2789,11 +2767,11 @@ static int hls_init(AVFormatContext *s)
             ret = av_dict_parse_string(&hls->format_options, hls->format_options_str, "=", ":", 0);
             if (ret < 0) {
                 av_log(s, AV_LOG_ERROR, "Could not parse format options list '%s'\n", hls->format_options_str);
-                goto fail;
+                return ret;
             }
         }
 
-        for (j = 0; j < vs->nb_streams; j++) {
+        for (int j = 0; j < vs->nb_streams; j++) {
             vs->has_video += vs->streams[j]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO;
             /* Get one video stream to reference for split segments
              * so use the first video stream index. */
@@ -2811,15 +2789,13 @@ static int hls_init(AVFormatContext *s)
             vs->oformat = av_guess_format("mpegts", NULL, NULL);
         }
 
-        if (!vs->oformat) {
-            ret = AVERROR_MUXER_NOT_FOUND;
-            goto fail;
-        }
+        if (!vs->oformat)
+            return AVERROR_MUXER_NOT_FOUND;
 
         if (hls->segment_filename) {
             ret = format_name(hls->segment_filename, &vs->basename, i, vs->varname);
             if (ret < 0)
-                goto fail;
+                return ret;
         } else {
             if (hls->flags & HLS_SINGLE_FILE) {
                 if (hls->segment_type == SEGMENT_TYPE_FMP4) {
@@ -2836,10 +2812,8 @@ static int hls_init(AVFormatContext *s)
             }
 
             vs->basename = av_malloc(basename_size);
-            if (!vs->basename) {
-                ret = AVERROR(ENOMEM);
-                goto fail;
-            }
+            if (!vs->basename)
+                return AVERROR(ENOMEM);
 
             av_strlcpy(vs->basename, vs->m3u8_name, basename_size);
 
@@ -2858,16 +2832,12 @@ static int hls_init(AVFormatContext *s)
                 fmp4_init_filename_len += strlen(POSTFIX_PATTERN);
             if (hls->flags & HLS_SINGLE_FILE) {
                 vs->fmp4_init_filename  = av_strdup(vs->basename);
-                if (!vs->fmp4_init_filename) {
-                    ret = AVERROR(ENOMEM);
-                    goto fail;
-                }
+                if (!vs->fmp4_init_filename)
+                    return AVERROR(ENOMEM);
             } else {
                 vs->fmp4_init_filename = av_malloc(fmp4_init_filename_len);
-                if (!vs->fmp4_init_filename ) {
-                    ret = AVERROR(ENOMEM);
-                    goto fail;
-                }
+                if (!vs->fmp4_init_filename)
+                    return AVERROR(ENOMEM);
                 av_strlcpy(vs->fmp4_init_filename, hls->fmp4_init_filename,
                            fmp4_init_filename_len);
                 if (hls->nb_varstreams > 1) {
@@ -2879,17 +2849,15 @@ static int hls_init(AVFormatContext *s)
                         ret = append_postfix(vs->fmp4_init_filename, fmp4_init_filename_len, i);
                     }
                     if (ret < 0)
-                        goto fail;
+                        return ret;
                 }
 
                 fmp4_init_filename_len = strlen(vs->m3u8_name) +
                     strlen(vs->fmp4_init_filename) + 1;
 
                 vs->base_output_dirname = av_malloc(fmp4_init_filename_len);
-                if (!vs->base_output_dirname) {
-                    ret = AVERROR(ENOMEM);
-                    goto fail;
-                }
+                if (!vs->base_output_dirname)
+                    return AVERROR(ENOMEM);
 
                 av_strlcpy(vs->base_output_dirname, vs->m3u8_name,
                            fmp4_init_filename_len);
@@ -2907,7 +2875,7 @@ static int hls_init(AVFormatContext *s)
 
         ret = hls->use_localtime ? sls_flag_check_duration_size(hls, vs) : sls_flag_check_duration_size_index(hls);
         if (ret < 0)
-            goto fail;
+            return ret;
 
         if (vs->has_subtitle) {
             const char *vtt_pattern = "%d.vtt";
@@ -2922,25 +2890,21 @@ static int hls_init(AVFormatContext *s)
             vtt_basename_size = strlen(vs->m3u8_name) + strlen(vtt_pattern) + 1;
 
             vs->vtt_basename = av_malloc(vtt_basename_size);
-            if (!vs->vtt_basename) {
-                ret = AVERROR(ENOMEM);
-                goto fail;
-            }
+            if (!vs->vtt_basename)
+                return AVERROR(ENOMEM);
             av_strlcpy(vs->vtt_basename, vs->m3u8_name, vtt_basename_size);
             p = strrchr(vs->vtt_basename, '.');
             if (p)
                 *p = '\0';
 
-            if ( hls->subtitle_filename ) {
+            if (hls->subtitle_filename) {
                 ret = format_name(hls->subtitle_filename, &vs->vtt_m3u8_name, i, vs->varname);
                 if (ret < 0)
-                    goto fail;
+                    return ret;
             } else {
                 vs->vtt_m3u8_name = av_malloc(vtt_basename_size);
-                if (!vs->vtt_m3u8_name) {
-                    ret = AVERROR(ENOMEM);
-                    goto fail;
-                }
+                if (!vs->vtt_m3u8_name)
+                    return AVERROR(ENOMEM);
                 strcpy(vs->vtt_m3u8_name, vs->vtt_basename);
                 av_strlcat(vs->vtt_m3u8_name, "_vtt.m3u8", vtt_basename_size);
             }
@@ -2949,14 +2913,12 @@ static int hls_init(AVFormatContext *s)
 
         if (hls->baseurl) {
             vs->baseurl = av_strdup(hls->baseurl);
-            if (!vs->baseurl) {
-                ret = AVERROR(ENOMEM);
-                goto fail;
-            }
+            if (!vs->baseurl)
+                return AVERROR(ENOMEM);
         }
 
         if ((ret = hls_mux_init(s, vs)) < 0)
-            goto fail;
+            return ret;
 
         if (hls->flags & HLS_APPEND_LIST) {
             parse_playlist(s, vs->m3u8_name, vs);
@@ -2970,10 +2932,9 @@ static int hls_init(AVFormatContext *s)
         }
 
         if ((ret = hls_start(s, vs)) < 0)
-            goto fail;
+            return ret;
     }
 
-fail:
     return ret;
 }
 
-- 
2.20.1



More information about the ffmpeg-devel mailing list