diff mbox series

[FFmpeg-devel,5/6] avformat/hlsenc: Simplify setting basename with av_asprintf()

Message ID 20200509191509.9812-5-andreas.rheinhardt@gmail.com
State Accepted
Commit 21553d2082a8b5997c25b7b13a9b3426ac43e116
Headers show
Series [FFmpeg-devel,1/6] avformat/hlsenc: Don't reset AVIOContext pointer manually a second time
Related show

Checks

Context Check Description
andriy/default pending
andriy/make success Make finished
andriy/make_fate success Make fate finished

Commit Message

Andreas Rheinhardt May 9, 2020, 7:15 p.m. UTC
Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
---
 libavformat/hlsenc.c | 43 ++++++++++++++-----------------------------
 1 file changed, 14 insertions(+), 29 deletions(-)

Comments

Steven Liu May 10, 2020, 3:04 a.m. UTC | #1
> 2020年5月10日 上午3:15,Andreas Rheinhardt <andreas.rheinhardt@gmail.com> 写道:
> 
> Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
> ---
> libavformat/hlsenc.c | 43 ++++++++++++++-----------------------------
> 1 file changed, 14 insertions(+), 29 deletions(-)
> 
> diff --git a/libavformat/hlsenc.c b/libavformat/hlsenc.c
> index 5517cb4354..d80852739e 100644
> --- a/libavformat/hlsenc.c
> +++ b/libavformat/hlsenc.c
> @@ -2739,15 +2739,21 @@ static int hls_init(AVFormatContext *s)
>     int i = 0;
>     int j = 0;
>     HLSContext *hls = s->priv_data;
> -    const char *pattern = "%d.ts";
> +    const char *pattern;
>     VariantStream *vs = NULL;
> -    int basename_size = 0;
> -    const char *pattern_localtime_fmt = get_default_pattern_localtime_fmt(s);
>     const char *vtt_pattern = hls->flags & HLS_SINGLE_FILE ? ".vtt" : "%d.vtt";
>     char *p = NULL;
>     int http_base_proto = ff_is_http_proto(s->url);
>     int fmp4_init_filename_len = strlen(hls->fmp4_init_filename) + 1;
> 
> +    if (hls->use_localtime) {
> +        pattern = get_default_pattern_localtime_fmt(s);
> +    } else {
> +        pattern = hls->segment_type == SEGMENT_TYPE_FMP4 ? "%d.m4s" : "%d.ts";
> +        if (hls->flags & HLS_SINGLE_FILE)
> +            pattern += 2;
> +    }
> +
>     hls->has_default_key = 0;
>     hls->has_video_m3u8 = 0;
>     ret = update_variant_stream_info(s);
> @@ -2792,9 +2798,6 @@ static int hls_init(AVFormatContext *s)
>         }
>     }
> 
> -    if (hls->segment_type == SEGMENT_TYPE_FMP4) {
> -        pattern = "%d.m4s";
> -    }
>     if ((hls->start_sequence_source_type == HLS_START_SEQUENCE_AS_SECONDS_SINCE_EPOCH) ||
>         (hls->start_sequence_source_type == HLS_START_SEQUENCE_AS_MICROSECONDS_SINCE_EPOCH) ||
>         (hls->start_sequence_source_type == HLS_START_SEQUENCE_AS_FORMATTED_DATETIME)) {
> @@ -2868,34 +2871,16 @@ static int hls_init(AVFormatContext *s)
>             if (ret < 0)
>                 return ret;
>         } else {
> -            if (hls->flags & HLS_SINGLE_FILE) {
> -                if (hls->segment_type == SEGMENT_TYPE_FMP4) {
> -                    pattern = ".m4s";
> -                } else {
> -                    pattern = ".ts";
> -                }
> -            }
> -
> -            if (hls->use_localtime) {
> -                basename_size = strlen(vs->m3u8_name) + strlen(pattern_localtime_fmt) + 1;
> -            } else {
> -                basename_size = strlen(vs->m3u8_name) + strlen(pattern) + 1;
> -            }
> +            p = strrchr(vs->m3u8_name, '.');
> +            if (p)
> +                *p = '\0';
> 
> -            vs->basename = av_malloc(basename_size);
> +            vs->basename = av_asprintf("%s%s", vs->m3u8_name, pattern);
>             if (!vs->basename)
>                 return AVERROR(ENOMEM);
> 
> -            av_strlcpy(vs->basename, vs->m3u8_name, basename_size);
> -
> -            p = strrchr(vs->basename, '.');
>             if (p)
> -                *p = '\0';
> -            if (hls->use_localtime) {
> -                av_strlcat(vs->basename, pattern_localtime_fmt, basename_size);
> -            } else {
> -                av_strlcat(vs->basename, pattern, basename_size);
> -            }
> +                *p = '.';
>         }
> 
>         if (hls->segment_type == SEGMENT_TYPE_FMP4) {
> -- 
> 2.20.1
> 
> _______________________________________________
> ffmpeg-devel mailing list
> ffmpeg-devel@ffmpeg.org
> https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
> 
> To unsubscribe, visit link above, or email
> ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

LGTM

Thanks

Steven Liu
diff mbox series

Patch

diff --git a/libavformat/hlsenc.c b/libavformat/hlsenc.c
index 5517cb4354..d80852739e 100644
--- a/libavformat/hlsenc.c
+++ b/libavformat/hlsenc.c
@@ -2739,15 +2739,21 @@  static int hls_init(AVFormatContext *s)
     int i = 0;
     int j = 0;
     HLSContext *hls = s->priv_data;
-    const char *pattern = "%d.ts";
+    const char *pattern;
     VariantStream *vs = NULL;
-    int basename_size = 0;
-    const char *pattern_localtime_fmt = get_default_pattern_localtime_fmt(s);
     const char *vtt_pattern = hls->flags & HLS_SINGLE_FILE ? ".vtt" : "%d.vtt";
     char *p = NULL;
     int http_base_proto = ff_is_http_proto(s->url);
     int fmp4_init_filename_len = strlen(hls->fmp4_init_filename) + 1;
 
+    if (hls->use_localtime) {
+        pattern = get_default_pattern_localtime_fmt(s);
+    } else {
+        pattern = hls->segment_type == SEGMENT_TYPE_FMP4 ? "%d.m4s" : "%d.ts";
+        if (hls->flags & HLS_SINGLE_FILE)
+            pattern += 2;
+    }
+
     hls->has_default_key = 0;
     hls->has_video_m3u8 = 0;
     ret = update_variant_stream_info(s);
@@ -2792,9 +2798,6 @@  static int hls_init(AVFormatContext *s)
         }
     }
 
-    if (hls->segment_type == SEGMENT_TYPE_FMP4) {
-        pattern = "%d.m4s";
-    }
     if ((hls->start_sequence_source_type == HLS_START_SEQUENCE_AS_SECONDS_SINCE_EPOCH) ||
         (hls->start_sequence_source_type == HLS_START_SEQUENCE_AS_MICROSECONDS_SINCE_EPOCH) ||
         (hls->start_sequence_source_type == HLS_START_SEQUENCE_AS_FORMATTED_DATETIME)) {
@@ -2868,34 +2871,16 @@  static int hls_init(AVFormatContext *s)
             if (ret < 0)
                 return ret;
         } else {
-            if (hls->flags & HLS_SINGLE_FILE) {
-                if (hls->segment_type == SEGMENT_TYPE_FMP4) {
-                    pattern = ".m4s";
-                } else {
-                    pattern = ".ts";
-                }
-            }
-
-            if (hls->use_localtime) {
-                basename_size = strlen(vs->m3u8_name) + strlen(pattern_localtime_fmt) + 1;
-            } else {
-                basename_size = strlen(vs->m3u8_name) + strlen(pattern) + 1;
-            }
+            p = strrchr(vs->m3u8_name, '.');
+            if (p)
+                *p = '\0';
 
-            vs->basename = av_malloc(basename_size);
+            vs->basename = av_asprintf("%s%s", vs->m3u8_name, pattern);
             if (!vs->basename)
                 return AVERROR(ENOMEM);
 
-            av_strlcpy(vs->basename, vs->m3u8_name, basename_size);
-
-            p = strrchr(vs->basename, '.');
             if (p)
-                *p = '\0';
-            if (hls->use_localtime) {
-                av_strlcat(vs->basename, pattern_localtime_fmt, basename_size);
-            } else {
-                av_strlcat(vs->basename, pattern, basename_size);
-            }
+                *p = '.';
         }
 
         if (hls->segment_type == SEGMENT_TYPE_FMP4) {