Re: [FFmpeg-devel] [PATCH 3/3] Adds a new hls_flag avg_bw

2018-09-29 Thread Amit Kale
Thanks for your feedback. Will soon make these changes and re-submit.
-Amit

On Fri, Sep 28, 2018 at 7:02 PM Jeyapal, Karthick 
wrote:

> Please find my comments inlined below.
> On 9/28/18 11:36 AM, Amit Kale wrote:
> > If this flag is set, AVERAGE-BANDWIDTH value will be added to a master
> playlist entry. This flag implies peak_segment_bw.
> Better to add a code like below to set peak segment bw flag
> explicitly(with comments) in hls_init function.
> if (hls->flags & HLS_AVG_BW)
>hls->flags |= HLS_PEAK_SEGMENT_BW
> >
> > Signed-off-by: Amit Kale
> > ---
> >  doc/muxers.texi   |  4 
> >  libavformat/dashenc.c |  2 +-
> >  libavformat/hlsenc.c  | 20 
> >  libavformat/hlsplaylist.c |  6 --
> >  libavformat/hlsplaylist.h |  4 ++--
> >  5 files changed, 27 insertions(+), 9 deletions(-)
> >
> > Index: ffmpeg/doc/muxers.texi
> > ===
> > --- ffmpeg.orig/doc/muxers.texi
> > +++ ffmpeg/doc/muxers.texi
> > @@ -793,6 +793,10 @@ Possible values:
> >  If this flag is set, BANDWIDTH value in a master playlist entry will be
> >  set to the peak segment bandwidth.
> >
> > +@item avg_bw
> Rename this to average_bandwidth
> > +If this flag is set, AVERAGE-BANDWIDTH value will be added to a master
> > +playlist entry. This flag implies peak_segment_bw.
> > +
> >  @item single_file
> >  If this flag is set, the muxer will store all segments in a single
> MPEG-TS
> >  file, and will use byte ranges in the playlist. HLS playlists generated
> with
> > Index: ffmpeg/libavformat/dashenc.c
> > ===
> > --- ffmpeg.orig/libavformat/dashenc.c
> > +++ ffmpeg/libavformat/dashenc.c
> > @@ -919,7 +919,7 @@ static int write_manifest(AVFormatContex
> >  av_strlcat(codec_str, audio_codec_str,
> sizeof(codec_str));
> >  }
> >  get_hls_playlist_name(playlist_file, sizeof(playlist_file),
> NULL, i);
> > -ff_hls_write_stream_info(st, out, stream_bitrate,
> playlist_file, agroup,
> > +ff_hls_write_stream_info(st, out, stream_bitrate, 0,
> playlist_file, agroup,
> >   codec_str, NULL);
> >  }
> >  avio_close(out);
> > Index: ffmpeg/libavformat/hlsenc.c
> > ===
> > --- ffmpeg.orig/libavformat/hlsenc.c
> > +++ ffmpeg/libavformat/hlsenc.c
> > @@ -100,6 +100,7 @@ typedef enum HLSFlags {
> >  HLS_PERIODIC_REKEY = (1 << 12),
> >  HLS_INDEPENDENT_SEGMENTS = (1 << 13),
> >  HLS_PEAK_SEGMENT_BW = (1 << 14),
> > +HLS_AVG_BW = (1 << 15),
> >  } HLSFlags;
> >
> >  typedef enum {
> > @@ -115,6 +116,8 @@ typedef struct VariantStream {
> >  AVOutputFormat *vtt_oformat;
> >  AVIOContext *out;
> >  int packets_written;
> > +int64_t bytes_written;
> > +double total_duration;
> These can be local variables. It can be calculated during peak bandwidth
> calculation loop.
> >  int init_range_length;
> >
> >  AVFormatContext *avf;
> > @@ -1183,7 +1186,7 @@ static int create_master_playlist(AVForm
> >  AVStream *vid_st, *aud_st;
> >  AVDictionary *options = NULL;
> >  unsigned int i, j;
> > -int m3u8_name_size, ret, bandwidth;
> > +int m3u8_name_size, ret, bandwidth, avgbw;
> Could rename it to average_bandwidth for the sake of consistency.
> >  char *m3u8_rel_name, *ccgroup;
> >  ClosedCaptionsStream *ccs;
> >
> > @@ -1303,7 +1306,9 @@ static int create_master_playlist(AVForm
> >  }
> >
> >  bandwidth = 0;
> > -if (last && hls->flags & HLS_PEAK_SEGMENT_BW) {
> > +avgbw = 0;
> > +if (last && (hls->flags & HLS_PEAK_SEGMENT_BW ||
> > +hls->flags & HLS_AVG_BW)) {
> '|| hls->flags & HLS_AVG_BW' is not required if HLS_PEAK_SEGMENT_BW flag
> is set in hls_init as mentioned earlier.
> >  HLSSegment *hs = vs->segments;
> >  while (hs) {
> > int64_t segment_bandwidth = hs->size * 8 / hs->duration;
> vs->bytes_written  and vs->total_duration can be made local variables and
> its value could be calculated here.
> Anyways we are reading hs->size and hs->duration here.
> > @@ -1311,6 +1316,8 @@ static int create_master_p

Re: [FFmpeg-devel] [PATCH 1/3] Fix computation of vs->start_pos

2018-09-28 Thread Amit Kale

On Friday 28 September 2018 11:40 AM, Steven Liu wrote:

Amit Kale  于2018年9月28日周五 下午2:01写道:


Reset vs->start_pos when beginning a new file.

Signed-off-by: Amit Kale
---
   libavformat/hlsenc.c | 5 -
   1 file changed, 4 insertions(+), 1 deletion(-)

Index: ffmpeg/libavformat/hlsenc.c
===
--- ffmpeg.orig/libavformat/hlsenc.c
+++ ffmpeg/libavformat/hlsenc.c
@@ -2289,7 +2289,10 @@ static int hls_write_packet(AVFormatCont
   }

   if (hls->segment_type != SEGMENT_TYPE_FMP4) {
-vs->start_pos = new_start_pos;
+if (hls->flags & HLS_SINGLE_FILE)
+vs->start_pos = new_start_pos;

maybe it have problem when process the byterange mode and not only
single file mode.


That's why when HLS_SINGLE_FILE is on, it doesn't change the way vs->start_pos is 
set. It changes vs->start_pos to 0 only when HLS_SINGLE_FILE isn't used. That way at 
the beginning of a new segment, vs->start_pos is 0.
-Amit


+else
+vs->start_pos = 0;
   } else {
   vs->start_pos += vs->size;
   }
___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
http://ffmpeg.org/mailman/listinfo/ffmpeg-devel


Thanks
___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
http://ffmpeg.org/mailman/listinfo/ffmpeg-devel


___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
http://ffmpeg.org/mailman/listinfo/ffmpeg-devel


[FFmpeg-devel] [PATCH 3/3] Adds a new hls_flag avg_bw

2018-09-28 Thread Amit Kale

If this flag is set, AVERAGE-BANDWIDTH value will be added to a master playlist 
entry. This flag implies peak_segment_bw.

Signed-off-by: Amit Kale
---
 doc/muxers.texi   |  4 
 libavformat/dashenc.c |  2 +-
 libavformat/hlsenc.c  | 20 
 libavformat/hlsplaylist.c |  6 --
 libavformat/hlsplaylist.h |  4 ++--
 5 files changed, 27 insertions(+), 9 deletions(-)

Index: ffmpeg/doc/muxers.texi
===
--- ffmpeg.orig/doc/muxers.texi
+++ ffmpeg/doc/muxers.texi
@@ -793,6 +793,10 @@ Possible values:
 If this flag is set, BANDWIDTH value in a master playlist entry will be
 set to the peak segment bandwidth.
 
+@item avg_bw

+If this flag is set, AVERAGE-BANDWIDTH value will be added to a master
+playlist entry. This flag implies peak_segment_bw.
+
 @item single_file
 If this flag is set, the muxer will store all segments in a single MPEG-TS
 file, and will use byte ranges in the playlist. HLS playlists generated with
Index: ffmpeg/libavformat/dashenc.c
===
--- ffmpeg.orig/libavformat/dashenc.c
+++ ffmpeg/libavformat/dashenc.c
@@ -919,7 +919,7 @@ static int write_manifest(AVFormatContex
 av_strlcat(codec_str, audio_codec_str, sizeof(codec_str));
 }
 get_hls_playlist_name(playlist_file, sizeof(playlist_file), NULL, 
i);
-ff_hls_write_stream_info(st, out, stream_bitrate, playlist_file, 
agroup,
+ff_hls_write_stream_info(st, out, stream_bitrate, 0, 
playlist_file, agroup,
  codec_str, NULL);
 }
 avio_close(out);
Index: ffmpeg/libavformat/hlsenc.c
===
--- ffmpeg.orig/libavformat/hlsenc.c
+++ ffmpeg/libavformat/hlsenc.c
@@ -100,6 +100,7 @@ typedef enum HLSFlags {
 HLS_PERIODIC_REKEY = (1 << 12),
 HLS_INDEPENDENT_SEGMENTS = (1 << 13),
 HLS_PEAK_SEGMENT_BW = (1 << 14),
+HLS_AVG_BW = (1 << 15),
 } HLSFlags;
 
 typedef enum {

@@ -115,6 +116,8 @@ typedef struct VariantStream {
 AVOutputFormat *vtt_oformat;
 AVIOContext *out;
 int packets_written;
+int64_t bytes_written;
+double total_duration;
 int init_range_length;
 
 AVFormatContext *avf;

@@ -1183,7 +1186,7 @@ static int create_master_playlist(AVForm
 AVStream *vid_st, *aud_st;
 AVDictionary *options = NULL;
 unsigned int i, j;
-int m3u8_name_size, ret, bandwidth;
+int m3u8_name_size, ret, bandwidth, avgbw;
 char *m3u8_rel_name, *ccgroup;
 ClosedCaptionsStream *ccs;
 
@@ -1303,7 +1306,9 @@ static int create_master_playlist(AVForm

 }
 
 bandwidth = 0;

-if (last && hls->flags & HLS_PEAK_SEGMENT_BW) {
+avgbw = 0;
+if (last && (hls->flags & HLS_PEAK_SEGMENT_BW ||
+hls->flags & HLS_AVG_BW)) {
 HLSSegment *hs = vs->segments;
 while (hs) {
int64_t segment_bandwidth = hs->size * 8 / hs->duration;
@@ -1311,6 +1316,8 @@ static int create_master_playlist(AVForm
 bandwidth = segment_bandwidth;
 hs = hs->next;
 }
+if (hls->flags & HLS_AVG_BW)
+avgbw = (vs->bytes_written * 8) / vs->total_duration;
 } else {
 if (vid_st)
 bandwidth += get_stream_bit_rate(vid_st);
@@ -1334,8 +1341,8 @@ static int create_master_playlist(AVForm
 vs->ccgroup);
 }
 
-ff_hls_write_stream_info(vid_st, hls->m3u8_out, bandwidth, m3u8_rel_name,

-aud_st ? vs->agroup : NULL, vs->codec_attr, ccgroup);
+ff_hls_write_stream_info(vid_st, hls->m3u8_out, bandwidth, avgbw,
+m3u8_rel_name, aud_st ? vs->agroup : NULL, vs->codec_attr, 
ccgroup);
 
 av_freep(_rel_name);

 }
@@ -2211,6 +2218,8 @@ static int hls_write_packet(AVFormatCont
 new_start_pos = avio_tell(vs->avf->pb);
 if (hls->segment_type != SEGMENT_TYPE_FMP4) {
 vs->size = new_start_pos - vs->start_pos;
+vs->bytes_written += vs->size;
+vs->total_duration += vs->duration;
 } else {
 vs->size = new_start_pos;
 }
@@ -2397,6 +2406,8 @@ failed:
 } else {
 vs->size = avio_tell(vs->avf->pb);
 }
+vs->bytes_written += vs->size;
+vs->total_duration += vs->duration;
 if (hls->segment_type != SEGMENT_TYPE_FMP4)
 ff_format_io_close(s, >pb);
 
@@ -2837,6 +2848,7 @@ static const AVOption options[] = {

 {"fmp4",   "make segment file to fragment mp4 files in m3u8", 0, AV_OPT_TYPE_CONST, 
{.i64 = SEGMENT_TYPE_FMP4 }, 0, UIN

[FFmpeg-devel] [PATCH 2/3] Adds a new hls_flag peak_segment_bw

2018-09-28 Thread Amit Kale

If this flag is set, BANDWIDTH value in a master playlist entry will be set to 
the peak segment bandwidth. This patch also delays freeing of hls stream data, 
so that it's available for bandwidth calculation.

Signed-off-by: Amit Kale
---
 doc/muxers.texi  |  4 
 libavformat/hlsenc.c | 29 +
 2 files changed, 25 insertions(+), 8 deletions(-)

Index: ffmpeg/doc/muxers.texi
===
--- ffmpeg.orig/doc/muxers.texi
+++ ffmpeg/doc/muxers.texi
@@ -789,6 +789,10 @@ subdirectories.
 Possible values:
 
 @table @samp

+@item peak_segment_bw
+If this flag is set, BANDWIDTH value in a master playlist entry will be
+set to the peak segment bandwidth.
+
 @item single_file
 If this flag is set, the muxer will store all segments in a single MPEG-TS
 file, and will use byte ranges in the playlist. HLS playlists generated with
Index: ffmpeg/libavformat/hlsenc.c
===
--- ffmpeg.orig/libavformat/hlsenc.c
+++ ffmpeg/libavformat/hlsenc.c
@@ -99,6 +99,7 @@ typedef enum HLSFlags {
 HLS_TEMP_FILE = (1 << 11),
 HLS_PERIODIC_REKEY = (1 << 12),
 HLS_INDEPENDENT_SEGMENTS = (1 << 13),
+HLS_PEAK_SEGMENT_BW = (1 << 14),
 } HLSFlags;
 
 typedef enum {

@@ -1174,7 +1175,8 @@ static int64_t get_stream_bit_rate(AVStr
 }
 
 static int create_master_playlist(AVFormatContext *s,

-  VariantStream * const input_vs)
+  VariantStream * const input_vs,
+  int last)
 {
 HLSContext *hls = s->priv_data;
 VariantStream *vs, *temp_vs;
@@ -1191,13 +1193,16 @@ static int create_master_playlist(AVForm
 for (i = 0; i < hls->nb_varstreams; i++)
 if (!hls->var_streams[i].m3u8_created)
 return 0;
-} else {
+} else if (!last) {
  /* Keep publishing the master playlist at the configured rate */
 if (>var_streams[0] != input_vs || !hls->master_publish_rate ||
 input_vs->number % hls->master_publish_rate)
 return 0;
 }
 
+if (hls->flags & HLS_PEAK_SEGMENT_BW && hls->version < 7)

+hls->version = 7;
+
 set_http_options(s, , hls);
 
 ret = hlsenc_io_open(s, >m3u8_out, hls->master_m3u8_url, );

@@ -1298,11 +1303,21 @@ static int create_master_playlist(AVForm
 }
 
 bandwidth = 0;

-if (vid_st)
-bandwidth += get_stream_bit_rate(vid_st);
-if (aud_st)
-bandwidth += get_stream_bit_rate(aud_st);
-bandwidth += bandwidth / 10;
+if (last && hls->flags & HLS_PEAK_SEGMENT_BW) {
+HLSSegment *hs = vs->segments;
+while (hs) {
+   int64_t segment_bandwidth = hs->size * 8 / hs->duration;
+if (bandwidth < segment_bandwidth)
+bandwidth = segment_bandwidth;
+hs = hs->next;
+}
+} else {
+if (vid_st)
+bandwidth += get_stream_bit_rate(vid_st);
+if (aud_st)
+bandwidth += get_stream_bit_rate(aud_st);
+bandwidth += bandwidth / 10;
+}
 
 ccgroup = NULL;

 if (vid_st && vs->ccgroup) {
@@ -1443,7 +1458,7 @@ fail:
 ff_rename(temp_filename, vs->m3u8_name, s);
 
 if (ret >= 0 && hls->master_pl_name)

-if (create_master_playlist(s, vs) < 0)
+if (create_master_playlist(s, vs, last) < 0)
 av_log(s, AV_LOG_WARNING, "Master playlist creation failed\n");
 
 return ret;

@@ -2421,10 +2436,13 @@ failed:
 av_freep(>vtt_m3u8_name);
 avformat_free_context(vtt_oc);
 }
+av_free(old_filename);
 
+}

+for (i = 0; i < hls->nb_varstreams; i++) {
+vs = >var_streams[i];
 hls_free_segments(vs->segments);
 hls_free_segments(vs->old_segments);
-av_free(old_filename);
 av_freep(>m3u8_name);
 av_freep(>streams);
 av_freep(>agroup);
@@ -2819,6 +2837,7 @@ static const AVOption options[] = {
 {"fmp4",   "make segment file to fragment mp4 files in m3u8", 0, AV_OPT_TYPE_CONST, 
{.i64 = SEGMENT_TYPE_FMP4 }, 0, UINT_MAX,   E, "segment_type"},
 {"hls_fmp4_init_filename", "set fragment mp4 file init filename", 
OFFSET(fmp4_init_filename),   AV_OPT_TYPE_STRING, {.str = "init.mp4"},0,   0,   
  E},
 {"hls_flags", "set flags affecting HLS playlist and media file generation", 
OFFSET(flags), AV_OPT_TYPE_FLAGS, {.i64 = 0 }, 0, UINT_MAX, E, "flags"},
+{"peak_segment_bw",   "sets bandwidth in master play list to peak s

[FFmpeg-devel] [PATCH 1/3] Fix computation of vs->start_pos

2018-09-28 Thread Amit Kale

Reset vs->start_pos when beginning a new file.

Signed-off-by: Amit Kale
---
 libavformat/hlsenc.c | 5 -
 1 file changed, 4 insertions(+), 1 deletion(-)

Index: ffmpeg/libavformat/hlsenc.c
===
--- ffmpeg.orig/libavformat/hlsenc.c
+++ ffmpeg/libavformat/hlsenc.c
@@ -2289,7 +2289,10 @@ static int hls_write_packet(AVFormatCont
 }
 
 if (hls->segment_type != SEGMENT_TYPE_FMP4) {

-vs->start_pos = new_start_pos;
+if (hls->flags & HLS_SINGLE_FILE)
+vs->start_pos = new_start_pos;
+else
+vs->start_pos = 0;
 } else {
 vs->start_pos += vs->size;
 }
___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
http://ffmpeg.org/mailman/listinfo/ffmpeg-devel


[FFmpeg-devel] [PATCH 0/3] Enable peak and average bandwidth to be added to HLS manifest files

2018-09-28 Thread Amit Kale

Hi,

I am sending patches correct bandwidth values in the HLS manifest files output 
by ffmpeg. These are currently based on average bandwidth. These patches enable 
the bandwidth values to be calculated based on the number of bytes present in 
each stream in each segment file.
1/3 - Fix computation of vs->start_pos.
2/3 - Adds a new hls_flag peak_segment_bw. This flag causes peak bandwidths to 
be output in an HLS master manifest file.
3/3 - Adds a new hls_flag avg_bw. Implies peak_segment_bw. This flag adds 
average bandwidths to be added to HLS master manifest files.

Thanks.
-Amit
___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
http://ffmpeg.org/mailman/listinfo/ffmpeg-devel


[FFmpeg-devel] [PATCH 4/4] Delay freeing of hls stream data

2018-09-26 Thread Amit Kale

This patch delays freeing of hls stream data, so that it's available for 
bandwidth calculation. Otherwise the previous patches would cause a segfault in 
this code.

Signed-off-by: Amit Kale
---

Index: ffmpeg/libavformat/hlsenc.c
===
--- ffmpeg.orig/libavformat/hlsenc.c
+++ ffmpeg/libavformat/hlsenc.c
@@ -2447,10 +2447,13 @@ failed:
 av_freep(>vtt_m3u8_name);
 avformat_free_context(vtt_oc);
 }
+av_free(old_filename);
 
+}

+for (i = 0; i < hls->nb_varstreams; i++) {
+vs = >var_streams[i];
 hls_free_segments(vs->segments);
 hls_free_segments(vs->old_segments);
-av_free(old_filename);
 av_freep(>m3u8_name);
 av_freep(>streams);
 av_freep(>agroup);
___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
http://ffmpeg.org/mailman/listinfo/ffmpeg-devel


[FFmpeg-devel] [PATCH 1/4] Reset vs->start_pos when beginning a new file

2018-09-26 Thread Amit Kale

Reset vs->start_pos when beginning a new file.

Signed-off-by: Amit Kale
---
 libavformat/hlsenc.c | 5 -
 1 file changed, 4 insertions(+), 1 deletion(-)

Index: ffmpeg/libavformat/hlsenc.c
===
--- ffmpeg.orig/libavformat/hlsenc.c
+++ ffmpeg/libavformat/hlsenc.c
@@ -2289,7 +2289,10 @@ static int hls_write_packet(AVFormatCont
 }
 
 if (hls->segment_type != SEGMENT_TYPE_FMP4) {

-vs->start_pos = new_start_pos;
+if (hls->flags & HLS_SINGLE_FILE)
+vs->start_pos = new_start_pos;
+else
+vs->start_pos = 0;
 } else {
 vs->start_pos += vs->size;
 }
___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
http://ffmpeg.org/mailman/listinfo/ffmpeg-devel


[FFmpeg-devel] [PATCH 3/4] Adds a new hls_flag avg_bw

2018-09-25 Thread Amit Kale

If this flag is set, AVERAGE-BANDWIDTH value will be added to a master playlist
entry. This flag implies peak_segment_bw.
Signed-off-by: Amit Kale 
---
 doc/muxers.texi   |  4 
 libavformat/dashenc.c |  2 +-
 libavformat/hlsenc.c  | 20 
 libavformat/hlsplaylist.c |  6 --
 libavformat/hlsplaylist.h |  4 ++--
 5 files changed, 27 insertions(+), 9 deletions(-)

Index: ffmpeg/doc/muxers.texi
===
--- ffmpeg.orig/doc/muxers.texi
+++ ffmpeg/doc/muxers.texi
@@ -793,6 +793,10 @@ Possible values:
 If this flag is set, BANDWIDTH value in a master playlist entry will be
 set to the peak segment bandwidth.
 
+@item avg_bw

+If this flag is set, AVERAGE-BANDWIDTH value will be added to a master
+playlist entry. This flag implies peak_segment_bw.
+
 @item single_file
 If this flag is set, the muxer will store all segments in a single MPEG-TS
 file, and will use byte ranges in the playlist. HLS playlists generated with
Index: ffmpeg/libavformat/dashenc.c
===
--- ffmpeg.orig/libavformat/dashenc.c
+++ ffmpeg/libavformat/dashenc.c
@@ -919,7 +919,7 @@ static int write_manifest(AVFormatContex
 av_strlcat(codec_str, audio_codec_str, sizeof(codec_str));
 }
 get_hls_playlist_name(playlist_file, sizeof(playlist_file), NULL, 
i);
-ff_hls_write_stream_info(st, out, stream_bitrate, playlist_file, 
agroup,
+ff_hls_write_stream_info(st, out, stream_bitrate, 0, 
playlist_file, agroup,
  codec_str, NULL);
 }
 avio_close(out);
Index: ffmpeg/libavformat/hlsenc.c
===
--- ffmpeg.orig/libavformat/hlsenc.c
+++ ffmpeg/libavformat/hlsenc.c
@@ -100,6 +100,7 @@ typedef enum HLSFlags {
 HLS_PERIODIC_REKEY = (1 << 12),
 HLS_INDEPENDENT_SEGMENTS = (1 << 13),
 HLS_PEAK_SEGMENT_BW = (1 << 14),
+HLS_AVG_BW = (1 << 15),
 } HLSFlags;
 
 typedef enum {

@@ -115,6 +116,8 @@ typedef struct VariantStream {
 AVOutputFormat *vtt_oformat;
 AVIOContext *out;
 int packets_written;
+int64_t bytes_written;
+double total_duration;
 int init_range_length;
 
 AVFormatContext *avf;

@@ -1183,7 +1186,7 @@ static int create_master_playlist(AVForm
 AVStream *vid_st, *aud_st;
 AVDictionary *options = NULL;
 unsigned int i, j;
-int m3u8_name_size, ret, bandwidth;
+int m3u8_name_size, ret, bandwidth, avgbw;
 char *m3u8_rel_name, *ccgroup;
 ClosedCaptionsStream *ccs;
 
@@ -1303,7 +1306,9 @@ static int create_master_playlist(AVForm

 }
 
 bandwidth = 0;

-if (last && hls->flags & HLS_PEAK_SEGMENT_BW) {
+avgbw = 0;
+if (last && (hls->flags & HLS_PEAK_SEGMENT_BW ||
+hls->flags & HLS_AVG_BW)) {
 HLSSegment *hs = vs->segments;
 while (hs) {
int64_t segment_bandwidth = hs->size * 8 / hs->duration;
@@ -1311,6 +1316,8 @@ static int create_master_playlist(AVForm
 bandwidth = segment_bandwidth;
 hs = hs->next;
 }
+if (hls->flags & HLS_AVG_BW)
+avgbw = (vs->bytes_written * 8) / vs->total_duration;
 } else {
 if (vid_st)
 bandwidth += get_stream_bit_rate(vid_st);
@@ -1334,8 +1341,8 @@ static int create_master_playlist(AVForm
 vs->ccgroup);
 }
 
-ff_hls_write_stream_info(vid_st, hls->m3u8_out, bandwidth, m3u8_rel_name,

-aud_st ? vs->agroup : NULL, vs->codec_attr, ccgroup);
+ff_hls_write_stream_info(vid_st, hls->m3u8_out, bandwidth, avgbw,
+m3u8_rel_name, aud_st ? vs->agroup : NULL, vs->codec_attr, 
ccgroup);
 
 av_freep(_rel_name);

 }
@@ -2211,6 +2218,8 @@ static int hls_write_packet(AVFormatCont
 new_start_pos = avio_tell(vs->avf->pb);
 if (hls->segment_type != SEGMENT_TYPE_FMP4) {
 vs->size = new_start_pos - vs->start_pos;
+vs->bytes_written += vs->size;
+vs->total_duration += vs->duration;
 } else {
 vs->size = new_start_pos;
 }
@@ -2397,6 +2406,8 @@ failed:
 } else {
 vs->size = avio_tell(vs->avf->pb);
 }
+vs->bytes_written += vs->size;
+vs->total_duration += vs->duration;
 if (hls->segment_type != SEGMENT_TYPE_FMP4)
 ff_format_io_close(s, >pb);
 
@@ -2834,6 +2845,7 @@ static const AVOption options[] = {

 {"fmp4",   "make segment file to fragment mp4 files in m3u8", 0, AV_OPT_TYPE_CONST, 
{.i64 = SEGMENT_TYPE_FMP4 }, 0, UIN

[FFmpeg-devel] [PATCH 2/4] Add a new hls_flag peak_segment_bw

2018-09-25 Thread Amit Kale

If this flag is set, BANDWIDTH value in a master playlist entry will be set to
the peak segment bandwidth.

Signed-off-by: Amit Kale 
---
 doc/muxers.texi  |  4 
 libavformat/hlsenc.c | 29 +
 2 files changed, 25 insertions(+), 8 deletions(-)

Index: ffmpeg/doc/muxers.texi
===
--- ffmpeg.orig/doc/muxers.texi
+++ ffmpeg/doc/muxers.texi
@@ -789,6 +789,10 @@ subdirectories.
 Possible values:
 
 @table @samp

+@item peak_segment_bw
+If this flag is set, BANDWIDTH value in a master playlist entry will be
+set to the peak segment bandwidth.
+
 @item single_file
 If this flag is set, the muxer will store all segments in a single MPEG-TS
 file, and will use byte ranges in the playlist. HLS playlists generated with
Index: ffmpeg/libavformat/hlsenc.c
===
--- ffmpeg.orig/libavformat/hlsenc.c
+++ ffmpeg/libavformat/hlsenc.c
@@ -99,6 +99,7 @@ typedef enum HLSFlags {
 HLS_TEMP_FILE = (1 << 11),
 HLS_PERIODIC_REKEY = (1 << 12),
 HLS_INDEPENDENT_SEGMENTS = (1 << 13),
+HLS_PEAK_SEGMENT_BW = (1 << 14),
 } HLSFlags;
 
 typedef enum {

@@ -1174,7 +1175,8 @@ static int64_t get_stream_bit_rate(AVStr
 }
 
 static int create_master_playlist(AVFormatContext *s,

-  VariantStream * const input_vs)
+  VariantStream * const input_vs,
+  int last)
 {
 HLSContext *hls = s->priv_data;
 VariantStream *vs, *temp_vs;
@@ -1191,13 +1193,16 @@ static int create_master_playlist(AVForm
 for (i = 0; i < hls->nb_varstreams; i++)
 if (!hls->var_streams[i].m3u8_created)
 return 0;
-} else {
+} else if (!last) {
  /* Keep publishing the master playlist at the configured rate */
 if (>var_streams[0] != input_vs || !hls->master_publish_rate ||
 input_vs->number % hls->master_publish_rate)
 return 0;
 }
 
+if (hls->flags & HLS_PEAK_SEGMENT_BW && hls->version < 7)

+hls->version = 7;
+
 set_http_options(s, , hls);
 
 ret = hlsenc_io_open(s, >m3u8_out, hls->master_m3u8_url, );

@@ -1298,11 +1303,21 @@ static int create_master_playlist(AVForm
 }
 
 bandwidth = 0;

-if (vid_st)
-bandwidth += get_stream_bit_rate(vid_st);
-if (aud_st)
-bandwidth += get_stream_bit_rate(aud_st);
-bandwidth += bandwidth / 10;
+if (last && hls->flags & HLS_PEAK_SEGMENT_BW) {
+HLSSegment *hs = vs->segments;
+while (hs) {
+   int64_t segment_bandwidth = hs->size * 8 / hs->duration;
+if (bandwidth < segment_bandwidth)
+bandwidth = segment_bandwidth;
+hs = hs->next;
+}
+} else {
+if (vid_st)
+bandwidth += get_stream_bit_rate(vid_st);
+if (aud_st)
+bandwidth += get_stream_bit_rate(aud_st);
+bandwidth += bandwidth / 10;
+}
 
 ccgroup = NULL;

 if (vid_st && vs->ccgroup) {
@@ -1443,7 +1458,7 @@ fail:
 ff_rename(temp_filename, vs->m3u8_name, s);
 
 if (ret >= 0 && hls->master_pl_name)

-if (create_master_playlist(s, vs) < 0)
+if (create_master_playlist(s, vs, last) < 0)
 av_log(s, AV_LOG_WARNING, "Master playlist creation failed\n");
 
 return ret;

@@ -2819,6 +2834,7 @@ static const AVOption options[] = {
 {"fmp4",   "make segment file to fragment mp4 files in m3u8", 0, AV_OPT_TYPE_CONST, 
{.i64 = SEGMENT_TYPE_FMP4 }, 0, UINT_MAX,   E, "segment_type"},
 {"hls_fmp4_init_filename", "set fragment mp4 file init filename", 
OFFSET(fmp4_init_filename),   AV_OPT_TYPE_STRING, {.str = "init.mp4"},0,   0,   
  E},
 {"hls_flags", "set flags affecting HLS playlist and media file generation", 
OFFSET(flags), AV_OPT_TYPE_FLAGS, {.i64 = 0 }, 0, UINT_MAX, E, "flags"},
+{"peak_segment_bw",   "sets bandwidth in master play list to peak segment 
bandwidth", 0, AV_OPT_TYPE_CONST, {.i64 = HLS_PEAK_SEGMENT_BW }, 0, UINT_MAX,   E, "flags"},
 {"single_file",   "generate a single media file indexed with byte ranges", 0, 
AV_OPT_TYPE_CONST, {.i64 = HLS_SINGLE_FILE }, 0, UINT_MAX,   E, "flags"},
 {"temp_file", "write segment to temporary file and rename when complete", 0, 
AV_OPT_TYPE_CONST, {.i64 = HLS_TEMP_FILE }, 0, UINT_MAX,   E, "flags"},
 {"delete_segments", "delete segment files that are no longer part of the playlist", 
0, AV_OPT_TYPE_CONST, {.i64 = HLS_DELETE_SEGMENTS }, 0, UINT_MAX,   E, "flags"},
___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
http://ffmpeg.org/mailman/listinfo/ffmpeg-devel


[FFmpeg-devel] PATCH[0/4]

2018-09-25 Thread Amit Kale

Hi,

I am sending an HLS manifest file output patch containing fixes broken down in 
four parts as follows
0001-Fix-computation-of-vs-start_pos.patch
0002-Add-a-new-hls_flag-peak_segment_bw.patch
0003-Adds-a-new-hls_flag-avg_bw.patch
0004-fix-master_pl-segfault.patch

The overall description is that these patches correct bandwidth values in the 
HLS manifest files output by ffmpeg. These are currently based on average 
bandwidth. These patches enable the bandwidth values to be calculated based on 
the number of bytes present in each stream in each segment file.

Thanks.
-Amit
___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
http://ffmpeg.org/mailman/listinfo/ffmpeg-devel


[FFmpeg-devel] [PATCH 3/3] Adds a new hls_flag avg_bw

2018-02-12 Thread Amit Kale
If this flag is set, AVERAGE-BANDWIDTH value will be added to a master 
playlist

entry. This flag implies peak_segment_bw.
---
 doc/muxers.texi   |  4 
 libavformat/dashenc.c |  2 +-
 libavformat/hlsenc.c  | 20 
 libavformat/hlsplaylist.c |  6 --
 libavformat/hlsplaylist.h |  4 ++--
 5 files changed, 27 insertions(+), 9 deletions(-)

diff --git a/doc/muxers.texi b/doc/muxers.texi
index e2c9cbfa2f..428d4009b3 100644
--- a/doc/muxers.texi
+++ b/doc/muxers.texi
@@ -745,6 +745,10 @@ Possible values:
 If this flag is set, BANDWIDTH value in a master playlist entry will be
 set to the peak segment bandwidth.

+@item avg_bw
+If this flag is set, AVERAGE-BANDWIDTH value will be added to a master
+playlist entry. This flag implies peak_segment_bw.
+
 @item single_file
 If this flag is set, the muxer will store all segments in a single MPEG-TS
 file, and will use byte ranges in the playlist. HLS playlists 
generated with

diff --git a/libavformat/dashenc.c b/libavformat/dashenc.c
index 0f6f4f22fa..a918f7e649 100644
--- a/libavformat/dashenc.c
+++ b/libavformat/dashenc.c
@@ -827,7 +827,7 @@ static int write_manifest(AVFormatContext *s, int final)
 stream_bitrate += max_audio_bitrate;
 }
 get_hls_playlist_name(playlist_file, 
sizeof(playlist_file), NULL, i);
-    ff_hls_write_stream_info(st, out, stream_bitrate, 
playlist_file, agroup, NULL, NULL);
+    ff_hls_write_stream_info(st, out, stream_bitrate, 0, 
playlist_file, agroup, NULL, NULL);

 }
 avio_close(out);
 if (use_rename)
diff --git a/libavformat/hlsenc.c b/libavformat/hlsenc.c
index f40cd0b98f..aa704c94f8 100644
--- a/libavformat/hlsenc.c
+++ b/libavformat/hlsenc.c
@@ -99,6 +99,7 @@ typedef enum HLSFlags {
 HLS_PERIODIC_REKEY = (1 << 12),
 HLS_INDEPENDENT_SEGMENTS = (1 << 13),
 HLS_PEAK_SEGMENT_BW = (1 << 14),
+    HLS_AVG_BW = (1 << 15),
 } HLSFlags;

 typedef enum {
@@ -113,6 +114,8 @@ typedef struct VariantStream {
 AVOutputFormat *vtt_oformat;
 AVIOContext *out;
 int packets_written;
+    int64_t bytes_written;
+    double total_duration;
 int init_range_length;

 AVFormatContext *avf;
@@ -1177,7 +1180,7 @@ 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;
+    int m3u8_name_size, ret, bandwidth, avgbw;
 char *m3u8_rel_name, *ccgroup;
 ClosedCaptionsStream *ccs;

@@ -1294,7 +1297,9 @@ static int create_master_playlist(AVFormatContext *s,
 }

 bandwidth = 0;
-    if (last && hls->flags & HLS_PEAK_SEGMENT_BW) {
+    avgbw = 0;
+    if (last && (hls->flags & HLS_PEAK_SEGMENT_BW ||
+    hls->flags & HLS_AVG_BW)) {
 HLSSegment *hs = vs->segments;
 while (hs) {
 int64_t segment_bandwidth = hs->size * 8 / hs->duration;
@@ -1302,6 +1307,8 @@ static int create_master_playlist(AVFormatContext *s,
 bandwidth = segment_bandwidth;
 hs = hs->next;
 }
+    if (hls->flags & HLS_AVG_BW)
+    avgbw = vs->bytes_written / vs->total_duration;
 } else {
 if (vid_st)
 bandwidth += vid_st->codecpar->bit_rate;
@@ -1325,8 +1332,8 @@ static int create_master_playlist(AVFormatContext *s,
 vs->ccgroup);
 }

-    ff_hls_write_stream_info(vid_st, hls->m3u8_out, bandwidth, 
m3u8_rel_name,

-    aud_st ? vs->agroup : NULL, vs->codec_attr, ccgroup);
+    ff_hls_write_stream_info(vid_st, hls->m3u8_out, bandwidth, avgbw,
+    m3u8_rel_name, aud_st ? vs->agroup : NULL, vs->codec_attr, 
ccgroup);


 av_freep(_rel_name);
 }
@@ -2191,6 +2198,8 @@ static int hls_write_packet(AVFormatContext *s, 
AVPacket *pkt)


 new_start_pos = avio_tell(vs->avf->pb);
 vs->size = new_start_pos - vs->start_pos;
+    vs->bytes_written += vs->size;
+    vs->total_duration += vs->duration;

 if (!byterange_mode) {
 if (hls->segment_type == SEGMENT_TYPE_FMP4) {
@@ -2326,6 +2335,8 @@ failed:
 av_write_trailer(oc);
 if (oc->pb) {
 vs->size = avio_tell(vs->avf->pb) - vs->start_pos;
+    vs->bytes_written += vs->size;
+    vs->total_duration += vs->duration;
 if (hls->segment_type != SEGMENT_TYPE_FMP4)
 ff_format_io_close(s, >pb);

@@ -2765,6 +2776,7 @@ static const AVOption options[] = {
 {"fmp4",   "make segment file to fragment mp4 files in m3u8", 0, 
AV_OPT_TYPE_CONST, {.i64 = SEGMENT_TYPE_FMP4 }, 0, UINT_MAX, E, 
"segment_type"},
 {"hls_fmp4_init_filename", "set fragment mp4 file init filename", 
OFFSET(fmp4_init_filename),   AV_OPT_TYPE_STRING, {.str = 
"init.mp4"},    0,   0, E},
 {"hls_flags", "set flags affecting HLS playlist and media file 

[FFmpeg-devel] [PATCH 1/3] Fix computation of vs->start_pos

2018-02-12 Thread Amit Kale

Reset vs->start_pos when beginning a new file.
---
 libavformat/hlsenc.c | 5 -
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/libavformat/hlsenc.c b/libavformat/hlsenc.c
index cc13c94e97..9970c4c575 100644
--- a/libavformat/hlsenc.c
+++ b/libavformat/hlsenc.c
@@ -2227,7 +2227,10 @@ static int hls_write_packet(AVFormatContext *s, 
AVPacket *pkt)

 ff_format_io_close(s, >out);
 }
 ret = hls_append_segment(s, hls, vs, vs->duration, 
vs->start_pos, vs->size);

-    vs->start_pos = new_start_pos;
+    if (hls->flags & HLS_SINGLE_FILE)
+    vs->start_pos = new_start_pos;
+    else
+    vs->start_pos = 0;
 if (ret < 0) {
 av_free(old_filename);
 return ret;
--
2.14.1


___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
http://ffmpeg.org/mailman/listinfo/ffmpeg-devel


[FFmpeg-devel] [PATCH 2/3] Add a new hls_flag peak_segment_bw

2018-02-12 Thread Amit Kale
If this flag is set, BANDWIDTH value in a master playlist entry will be 
set to

the peak segment bandwidth.
---
 doc/muxers.texi  |  4 
 libavformat/hlsenc.c | 29 +
 2 files changed, 25 insertions(+), 8 deletions(-)

diff --git a/doc/muxers.texi b/doc/muxers.texi
index d9a5cc03dc..e2c9cbfa2f 100644
--- a/doc/muxers.texi
+++ b/doc/muxers.texi
@@ -741,6 +741,10 @@ subdirectories.
 Possible values:

 @table @samp
+@item peak_segment_bw
+If this flag is set, BANDWIDTH value in a master playlist entry will be
+set to the peak segment bandwidth.
+
 @item single_file
 If this flag is set, the muxer will store all segments in a single MPEG-TS
 file, and will use byte ranges in the playlist. HLS playlists 
generated with

diff --git a/libavformat/hlsenc.c b/libavformat/hlsenc.c
index 9970c4c575..f40cd0b98f 100644
--- a/libavformat/hlsenc.c
+++ b/libavformat/hlsenc.c
@@ -98,6 +98,7 @@ typedef enum HLSFlags {
 HLS_TEMP_FILE = (1 << 11),
 HLS_PERIODIC_REKEY = (1 << 12),
 HLS_INDEPENDENT_SEGMENTS = (1 << 13),
+    HLS_PEAK_SEGMENT_BW = (1 << 14),
 } HLSFlags;

 typedef enum {
@@ -1168,7 +1169,8 @@ static int get_relative_url(const char 
*master_url, const char *media_url,

 }

 static int create_master_playlist(AVFormatContext *s,
-  VariantStream * const input_vs)
+  VariantStream * const input_vs,
+  int last)
 {
 HLSContext *hls = s->priv_data;
 VariantStream *vs, *temp_vs;
@@ -1185,7 +1187,7 @@ static int create_master_playlist(AVFormatContext *s,
 for (i = 0; i < hls->nb_varstreams; i++)
 if (!hls->var_streams[i].m3u8_created)
 return 0;
-    } else {
+    } else if (!last) {
  /* Keep publishing the master playlist at the configured rate */
 if (>var_streams[0] != input_vs || 
!hls->master_publish_rate ||

 input_vs->number % hls->master_publish_rate)
@@ -1292,11 +1294,21 @@ static int 
create_master_playlist(AVFormatContext *s,

 }

 bandwidth = 0;
-    if (vid_st)
-    bandwidth += vid_st->codecpar->bit_rate;
-    if (aud_st)
-    bandwidth += aud_st->codecpar->bit_rate;
-    bandwidth += bandwidth / 10;
+    if (last && hls->flags & HLS_PEAK_SEGMENT_BW) {
+    HLSSegment *hs = vs->segments;
+    while (hs) {
+    int64_t segment_bandwidth = hs->size * 8 / hs->duration;
+    if (bandwidth < segment_bandwidth)
+    bandwidth = segment_bandwidth;
+    hs = hs->next;
+    }
+    } else {
+    if (vid_st)
+    bandwidth += vid_st->codecpar->bit_rate;
+    if (aud_st)
+    bandwidth += aud_st->codecpar->bit_rate;
+    bandwidth += bandwidth / 10;
+    }

 ccgroup = NULL;
 if (vid_st && vs->ccgroup) {
@@ -1437,7 +1449,7 @@ fail:
 ff_rename(temp_filename, vs->m3u8_name, s);

 if (ret >= 0 && hls->master_pl_name)
-    if (create_master_playlist(s, vs) < 0)
+    if (create_master_playlist(s, vs, last) < 0)
 av_log(s, AV_LOG_WARNING, "Master playlist creation 
failed\n");


 return ret;
@@ -2753,6 +2765,7 @@ static const AVOption options[] = {
 {"fmp4",   "make segment file to fragment mp4 files in m3u8", 0, 
AV_OPT_TYPE_CONST, {.i64 = SEGMENT_TYPE_FMP4 }, 0, UINT_MAX, E, 
"segment_type"},
 {"hls_fmp4_init_filename", "set fragment mp4 file init filename", 
OFFSET(fmp4_init_filename),   AV_OPT_TYPE_STRING, {.str = 
"init.mp4"},    0,   0, E},
 {"hls_flags", "set flags affecting HLS playlist and media file 
generation", OFFSET(flags), AV_OPT_TYPE_FLAGS, {.i64 = 0 }, 0, UINT_MAX, 
E, "flags"},
+    {"peak_segment_bw",   "sets bandwidth in master play list to peak 
segment bandwidth", 0, AV_OPT_TYPE_CONST, {.i64 = HLS_PEAK_SEGMENT_BW }, 
0, UINT_MAX,   E, "flags"},
 {"single_file",   "generate a single media file indexed with byte 
ranges", 0, AV_OPT_TYPE_CONST, {.i64 = HLS_SINGLE_FILE }, 0, UINT_MAX,   
E, "flags"},
 {"temp_file", "write segment to temporary file and rename when 
complete", 0, AV_OPT_TYPE_CONST, {.i64 = HLS_TEMP_FILE }, 0, UINT_MAX,   
E, "flags"},
 {"delete_segments", "delete segment files that are no longer part 
of the playlist", 0, AV_OPT_TYPE_CONST, {.i64 = HLS_DELETE_SEGMENTS }, 
0, UINT_MAX,   E, "flags"},

--
2.14.1


___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
http://ffmpeg.org/mailman/listinfo/ffmpeg-devel


Re: [FFmpeg-devel] [PATCH] HLS master playlist - peak and average bandwidth settings

2018-02-05 Thread Amit Kale
On Mon, Feb 5, 2018 at 5:52 PM, Carl Eugen Hoyos <ceffm...@gmail.com> wrote:

> 2018-02-05 11:11 GMT+01:00 Amit Kale <am...@hotstar.com>:
> > Kindly below a patch to add peak and average bandwidth settings for HLS
> > master playlist. It adds two hls_flags as described below.
> >
> > peak_segment_bw
> > If this flag is set, BANDWIDTH value in a master playlist entry will be
> set
> > to the
> > peak segment bandwidth.
> >
> > avg_bw
> > If this flag is set, AVERAGE-BANDWIDTH value will be added to a master
> > playlist
> > entry. This flag implies peak_segment_bw.
>
> > It changes behaviors of other codepaths as follows
>
> This sentence indicates that you should split your patch
> to ease both reviewing now and later in the history of the
> changed file.
>

Will send out split patches.


>
> > 1. Corrects setting of start_pos single file is not used in
> > hls_write_packet.
> > 2. Adds a new parameter avgbw to ff_hls_write_playlist_version. If this
> > parameter is non-null AVERAGE-BANDWIDTH is emitted.
>
> > This message and its attachments are confidential (or legally privileged)
> > information and are meant solely for the addressee of such message. Any
> > unauthorized use of the message / its attachments is strictly prohibited.
>
> Please remove this from emails sent to a public mailing list (or use
> another mail provider).
>

Apologies for being so ignorant. Will figure out a way to handle this.
Thanks.
-Amit


>
> Carl Eugen
> ___
> ffmpeg-devel mailing list
> ffmpeg-devel@ffmpeg.org
> http://ffmpeg.org/mailman/listinfo/ffmpeg-devel
>

-- 
This message and its attachments are confidential (or legally privileged) 
information and are meant solely for the addressee of such message. Any 
unauthorized use of the message / its attachments is strictly prohibited.
___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
http://ffmpeg.org/mailman/listinfo/ffmpeg-devel


[FFmpeg-devel] [PATCH] HLS master playlist - peak and average bandwidth settings

2018-02-05 Thread Amit Kale
Hi,

Kindly below a patch to add peak and average bandwidth settings for HLS
master playlist. It adds two hls_flags as described below.

peak_segment_bw
If this flag is set, BANDWIDTH value in a master playlist entry will be set
to the
peak segment bandwidth.

avg_bw
If this flag is set, AVERAGE-BANDWIDTH value will be added to a master
playlist
entry. This flag implies peak_segment_bw.

It changes behaviors of other codepaths as follows
1. Corrects setting of start_pos single file is not used in
hls_write_packet.
2. Adds a new parameter avgbw to ff_hls_write_playlist_version. If this
parameter is non-null AVERAGE-BANDWIDTH is emitted.

Thanks.
-Amit


Signed-off-by: Amit Kale <am...@hotstar.com>
--
diff --git a/doc/muxers.texi b/doc/muxers.texi
index d9a5cc03dc..428d4009b3 100644
--- a/doc/muxers.texi
+++ b/doc/muxers.texi
@@ -741,6 +741,14 @@ subdirectories.
 Possible values:

 @table @samp
+@item peak_segment_bw
+If this flag is set, BANDWIDTH value in a master playlist entry will be
+set to the peak segment bandwidth.
+
+@item avg_bw
+If this flag is set, AVERAGE-BANDWIDTH value will be added to a master
+playlist entry. This flag implies peak_segment_bw.
+
 @item single_file
 If this flag is set, the muxer will store all segments in a single MPEG-TS
 file, and will use byte ranges in the playlist. HLS playlists generated
with
diff --git a/libavformat/dashenc.c b/libavformat/dashenc.c
index 0f6f4f22fa..a918f7e649 100644
--- a/libavformat/dashenc.c
+++ b/libavformat/dashenc.c
@@ -827,7 +827,7 @@ static int write_manifest(AVFormatContext *s, int final)
 stream_bitrate += max_audio_bitrate;
 }
 get_hls_playlist_name(playlist_file, sizeof(playlist_file),
NULL, i);
-ff_hls_write_stream_info(st, out, stream_bitrate,
playlist_file, agroup, NULL, NULL);
+ff_hls_write_stream_info(st, out, stream_bitrate, 0,
playlist_file, agroup, NULL, NULL);
 }
 avio_close(out);
 if (use_rename)
diff --git a/libavformat/hlsenc.c b/libavformat/hlsenc.c
index cc13c94e97..e1592e0577 100644
--- a/libavformat/hlsenc.c
+++ b/libavformat/hlsenc.c
@@ -98,6 +98,8 @@ typedef enum HLSFlags {
 HLS_TEMP_FILE = (1 << 11),
 HLS_PERIODIC_REKEY = (1 << 12),
 HLS_INDEPENDENT_SEGMENTS = (1 << 13),
+HLS_PEAK_SEGMENT_BW = (1 << 14),
+HLS_AVG_BW = (1 << 15),
 } HLSFlags;

 typedef enum {
@@ -112,11 +114,14 @@ typedef struct VariantStream {
 AVOutputFormat *vtt_oformat;
 AVIOContext *out;
 int packets_written;
+int64_t bytes_written;
+double total_duration;
 int init_range_length;

 AVFormatContext *avf;
 AVFormatContext *vtt_avf;

+int avg_bandwidth;
 int has_video;
 int has_subtitle;
 int new_start;
@@ -1168,14 +1173,15 @@ static int get_relative_url(const char *master_url,
const char *media_url,
 }

 static int create_master_playlist(AVFormatContext *s,
-  VariantStream * const input_vs)
+  VariantStream * const input_vs,
+  int last)
 {
 HLSContext *hls = s->priv_data;
 VariantStream *vs, *temp_vs;
 AVStream *vid_st, *aud_st;
 AVDictionary *options = NULL;
 unsigned int i, j;
-int m3u8_name_size, ret, bandwidth;
+int m3u8_name_size, ret, bandwidth, avgbw;
 char *m3u8_rel_name, *ccgroup;
 ClosedCaptionsStream *ccs;

@@ -1185,7 +1191,7 @@ static int create_master_playlist(AVFormatContext *s,
 for (i = 0; i < hls->nb_varstreams; i++)
 if (!hls->var_streams[i].m3u8_created)
 return 0;
-} else {
+} else if (!last) {
  /* Keep publishing the master playlist at the configured rate */
 if (>var_streams[0] != input_vs || !hls->master_publish_rate
||
 input_vs->number % hls->master_publish_rate)
@@ -1290,13 +1296,28 @@ static int create_master_playlist(AVFormatContext
*s,
 }
 }
 }
-
-bandwidth = 0;
-if (vid_st)
-bandwidth += vid_st->codecpar->bit_rate;
-if (aud_st)
-bandwidth += aud_st->codecpar->bit_rate;
-bandwidth += bandwidth / 10;
+
+avgbw = 0;
+if (last && (hls->flags & HLS_PEAK_SEGMENT_BW || hls->flags &
HLS_AVG_BW)) {
+HLSSegment *hs = vs->segments;
+bandwidth = 0;
+while (hs) {
+int64_t segment_bandwidth = hs->size * 8 / hs->duration;
+if (bandwidth < segment_bandwidth)
+bandwidth = segment_bandwidth;
+hs = hs->next;
+}
+if (hls->flags & HLS_AVG_BW)
+avgbw = vs->bytes_written / vs->total_duration;
+} else {
+bandwidth = 0;
+avgbw = 0;
+if (vid_st)
+

Re: [FFmpeg-devel] hls BANDWIDTH field in master playlist

2018-02-01 Thread Amit Kale
Thanks for this info. Will soon get back with a patch.
-Amit

On 01-Feb-2018 3:58 PM, "Jeyapal, Karthick" <kjeya...@akamai.com> wrote:

> On 2/1/18 3:12 PM, Amit Kale wrote:
> > Hi All,
> >
> > When emitting a master playlist, BANDWIDTH field is computed in
> > libavformat/hlsenc.c by adding video and audio stream bitrates and then
> > adding 10% to account for container overhead. However HLS specification
> > (RFC8216) says that BANDWIDTH represents the peak segment bit rate.
> > AVERAGE-BANDWIDTH on the other hand represents the average segment bit
> > rate. If this is correct, I would like to send a patch to fix it.
> Yes, you are right. Ofcourse, you are welcome to send a patch to fix it.
> But please keep the following in mind, while sending the patch
> - BANDWIDTH is mandatory parameter and AVERAGE-BANDWIDTH is optional
> parameter.
> - Master playlist gets created at the beginning of encode during which
> time we neither know the peak bitrate or the average bitrate of the stream.
> Right now, we are just using the codec’s bitrate parameter which could be
> way different from the actual bitrate of the stream.  So the master
> playlist should get created initially with BANDWIDTH as codec bitrate(to
> handle live encode). At the end of encoding, master playlist should be
> updated again with the true peak bitrate and true average bitrate(for
> AVERAGE-BANDWIDTH).
> - The definition for BANDWIDTH changed to peak bitrate only in v7 of the
> HLS spec. Earlier BANDWIDTH was mentioned as overall bitrate. So, there
> should be an option to turn on or turn off peak bitrate calculation.
> >
> > Thanks.
> > -Amit
> >
>
>
> ___
> ffmpeg-devel mailing list
> ffmpeg-devel@ffmpeg.org
> http://ffmpeg.org/mailman/listinfo/ffmpeg-devel
>

-- 
This message and its attachments are confidential (or legally privileged) 
information and are meant solely for the addressee of such message. Any 
unauthorized use of the message / its attachments is strictly prohibited.
___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
http://ffmpeg.org/mailman/listinfo/ffmpeg-devel


[FFmpeg-devel] hls BANDWIDTH field in master playlist

2018-02-01 Thread Amit Kale
Hi All,

When emitting a master playlist, BANDWIDTH field is computed in
libavformat/hlsenc.c by adding video and audio stream bitrates and then
adding 10% to account for container overhead. However HLS specification
(RFC8216) says that BANDWIDTH represents the peak segment bit rate.
AVERAGE-BANDWIDTH on the other hand represents the average segment bit
rate. If this is correct, I would like to send a patch to fix it.

Thanks.
-Amit

-- 
This message and its attachments are confidential (or legally privileged) 
information and are meant solely for the addressee of such message. Any 
unauthorized use of the message / its attachments is strictly prohibited.
___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
http://ffmpeg.org/mailman/listinfo/ffmpeg-devel