[FFmpeg-devel] [PATCH v1] lavf/hlsenc: refine the get_relative_url function to avoid extra malloc for relation path

Limin Wang lance.lmwang at gmail.com
Tue Aug 27 17:40:05 EEST 2019


ping, I have tested with below command and check the m3u8 is ok:

./ffmpeg -threads 4 -vsync 1 -i ~/Movies/1.mp4 -r 25 \
 -b:v:0 5250k -c:v h264 -pix_fmt yuv420p -profile:v main -level 4.1 \
 -b:v:1 4200k -c:v h264 -pix_fmt yuv420p -profile:v main -level 4.1 \
 -b:v:1 3150k -c:v h264 -pix_fmt yuv420p -profile:v main -level 4.1 \
 -b:a:0 256k \
 -b:a:0 192k \
 -b:a:0 128k \
 -c:a aac -ar 48000  -map 0:v -map 0:a:0 -map 0:v -map 0:a:0 -map 0:v -map 0:a:0 \
 -f hls -var_stream_map "v:0,a:0  v:1,a:1 v:2,a:2" \
 -master_pl_name  master.m3u8 -t 300 -hls_time 10 -hls_init_time 4 -hls_list_size
 10 -master_pl_publish_rate 10 -hls_flags
 delete_segments+discont_start+split_by_time  \
 "'~/Movies/vs%v/manifest.m3u8"


On Tue, Aug 20, 2019 at 12:15:56AM +0800, lance.lmwang at gmail.com wrote:
> From: Limin Wang <lance.lmwang at gmail.com>
> 
> Signed-off-by: Limin Wang <lance.lmwang at gmail.com>
> ---
>  libavformat/hlsenc.c      | 50 ++++++++++++++---------------------------------
>  libavformat/hlsplaylist.c |  4 ++--
>  libavformat/hlsplaylist.h |  4 ++--
>  3 files changed, 19 insertions(+), 39 deletions(-)
> 
> diff --git a/libavformat/hlsenc.c b/libavformat/hlsenc.c
> index f6f9c81..e5d7791 100644
> --- a/libavformat/hlsenc.c
> +++ b/libavformat/hlsenc.c
> @@ -1229,22 +1229,22 @@ static int hls_rename_temp_file(AVFormatContext *s, AVFormatContext *oc)
>      return ret;
>  }
>  
> -static int get_relative_url(const char *master_url, const char *media_url,
> -                            char *rel_url, int rel_url_buf_size)
> +static const char* get_relative_url(const char *master_url, const char *media_url)
>  {
> -    char *p = NULL;
> -    int base_len = -1;
> -    p = strrchr(master_url, '/') ? strrchr(master_url, '/') :\
> -            strrchr(master_url, '\\');
> +    const char *p = strrchr(master_url, '/');
> +    size_t base_len = 0;
> +
> +    if (!p) p = strrchr(master_url, '\\');
> +
>      if (p) {
> -        base_len = FFABS(p - master_url);
> +        base_len = p + 1 - master_url;
>          if (av_strncasecmp(master_url, media_url, base_len)) {
>              av_log(NULL, AV_LOG_WARNING, "Unable to find relative url\n");
> -            return AVERROR(EINVAL);
> +            return NULL;
>          }
>      }
> -    av_strlcpy(rel_url, &(media_url[base_len + 1]), rel_url_buf_size);
> -    return 0;
> +
> +    return media_url + base_len;
>  }
>  
>  static int64_t get_stream_bit_rate(AVStream *stream) {
> @@ -1270,8 +1270,9 @@ static int create_master_playlist(AVFormatContext *s,
>      AVStream *vid_st, *aud_st;
>      AVDictionary *options = NULL;
>      unsigned int i, j;
> -    int m3u8_name_size, ret, bandwidth;
> -    char *m3u8_rel_name = NULL, *ccgroup;
> +    int ret, bandwidth;
> +    const char *m3u8_rel_name = NULL;
> +    char *ccgroup;
>      ClosedCaptionsStream *ccs;
>      const char *proto = avio_find_protocol_name(hls->master_m3u8_url);
>      int is_file_proto = proto && !strcmp(proto, "file");
> @@ -1320,39 +1321,21 @@ static int create_master_playlist(AVFormatContext *s,
>          if (vs->has_video || vs->has_subtitle || !vs->agroup)
>              continue;
>  
> -        m3u8_name_size = strlen(vs->m3u8_name) + 1;
> -        m3u8_rel_name = av_malloc(m3u8_name_size);
> +        m3u8_rel_name = get_relative_url(hls->master_m3u8_url, vs->m3u8_name);
>          if (!m3u8_rel_name) {
> -            ret = AVERROR(ENOMEM);
> -            goto fail;
> -        }
> -        av_strlcpy(m3u8_rel_name, vs->m3u8_name, m3u8_name_size);
> -        ret = get_relative_url(hls->master_m3u8_url, vs->m3u8_name,
> -                               m3u8_rel_name, m3u8_name_size);
> -        if (ret < 0) {
>              av_log(s, AV_LOG_ERROR, "Unable to find relative URL\n");
>              goto fail;
>          }
>  
>          ff_hls_write_audio_rendition(hls->m3u8_out, vs->agroup, m3u8_rel_name, vs->language, i, hls->has_default_key ? vs->is_default : 1);
> -
> -        av_freep(&m3u8_rel_name);
>      }
>  
>      /* For variant streams with video add #EXT-X-STREAM-INF tag with attributes*/
>      for (i = 0; i < hls->nb_varstreams; i++) {
>          vs = &(hls->var_streams[i]);
>  
> -        m3u8_name_size = strlen(vs->m3u8_name) + 1;
> -        m3u8_rel_name = av_malloc(m3u8_name_size);
> +        m3u8_rel_name = get_relative_url(hls->master_m3u8_url, vs->m3u8_name);
>          if (!m3u8_rel_name) {
> -            ret = AVERROR(ENOMEM);
> -            goto fail;
> -        }
> -        av_strlcpy(m3u8_rel_name, vs->m3u8_name, m3u8_name_size);
> -        ret = get_relative_url(hls->master_m3u8_url, vs->m3u8_name,
> -                               m3u8_rel_name, m3u8_name_size);
> -        if (ret < 0) {
>              av_log(NULL, AV_LOG_ERROR, "Unable to find relative URL\n");
>              goto fail;
>          }
> @@ -1421,13 +1404,10 @@ static int create_master_playlist(AVFormatContext *s,
>                                           aud_st ? vs->agroup : NULL, vs->codec_attr, ccgroup);
>              }
>          }
> -
> -        av_freep(&m3u8_rel_name);
>      }
>  fail:
>      if(ret >=0)
>          hls->master_m3u8_created = 1;
> -    av_freep(&m3u8_rel_name);
>      hlsenc_io_close(s, &hls->m3u8_out, temp_filename);
>      if (use_temp_file)
>          ff_rename(temp_filename, hls->master_m3u8_url, s);
> diff --git a/libavformat/hlsplaylist.c b/libavformat/hlsplaylist.c
> index e8b5667..9cbd023 100644
> --- a/libavformat/hlsplaylist.c
> +++ b/libavformat/hlsplaylist.c
> @@ -36,7 +36,7 @@ void ff_hls_write_playlist_version(AVIOContext *out, int version) {
>  }
>  
>  void ff_hls_write_audio_rendition(AVIOContext *out, char *agroup,
> -                                  char *filename, char *language, int name_id, int is_default) {
> +                                  const char *filename, char *language, int name_id, int is_default) {
>      if (!out || !agroup || !filename)
>          return;
>  
> @@ -49,7 +49,7 @@ void ff_hls_write_audio_rendition(AVIOContext *out, char *agroup,
>  }
>  
>  void ff_hls_write_stream_info(AVStream *st, AVIOContext *out,
> -                              int bandwidth, char *filename, char *agroup,
> +                              int bandwidth, const char *filename, char *agroup,
>                                char *codecs, char *ccgroup) {
>  
>      if (!out || !filename)
> diff --git a/libavformat/hlsplaylist.h b/libavformat/hlsplaylist.h
> index 8d2d83b..a8d29d6 100644
> --- a/libavformat/hlsplaylist.h
> +++ b/libavformat/hlsplaylist.h
> @@ -38,9 +38,9 @@ typedef enum {
>  
>  void ff_hls_write_playlist_version(AVIOContext *out, int version);
>  void ff_hls_write_audio_rendition(AVIOContext *out, char *agroup,
> -                                  char *filename, char *language, int name_id, int is_default);
> +                                  const char *filename, char *language, int name_id, int is_default);
>  void ff_hls_write_stream_info(AVStream *st, AVIOContext *out,
> -                              int bandwidth, char *filename, char *agroup,
> +                              int bandwidth, const char *filename, char *agroup,
>                                char *codecs, char *ccgroup);
>  void ff_hls_write_playlist_header(AVIOContext *out, int version, int allowcache,
>                                    int target_duration, int64_t sequence,
> -- 
> 2.6.4
> 


More information about the ffmpeg-devel mailing list