[FFmpeg-devel] [PATCH] [GSoC v3 6/7] avformat/hls add metadata "abr_initial" for ffplay

Hongcheng Zhong sj.hc_Zhong at sjtu.edu.cn
Sun Aug 23 15:23:54 EEST 2020


From: spartazhc <spartazhc at gmail.com>

Add an AVDictionary option "abr_initial", which could be used to send
message to ffplay. Currently, the first entry "abr_init_duration" is
added.

Signed-off-by: spartazhc <spartazhc at gmail.com>
---
 doc/demuxers.texi |  3 +++
 libavformat/hls.c | 14 ++++++++++++++
 2 files changed, 17 insertions(+)

diff --git a/doc/demuxers.texi b/doc/demuxers.texi
index 4cdbd95962..761a372b86 100644
--- a/doc/demuxers.texi
+++ b/doc/demuxers.texi
@@ -324,6 +324,9 @@ It accepts the following options:
 @item abr
 enable abr to switch streams.
 
+ at item abr_initial
+metadata used by abr for ffplay.
+
 @item live_start_index
 segment index to start live streams at (negative values are from the end).
 
diff --git a/libavformat/hls.c b/libavformat/hls.c
index 37a5a017b1..5c251cd002 100644
--- a/libavformat/hls.c
+++ b/libavformat/hls.c
@@ -243,6 +243,7 @@ typedef struct HLSContext {
     AVIOContext *playlist_pb;
 
     int abr;
+    AVDictionary *abr_initial;
     struct throughput  *throughputs;
     struct switch_task *switch_tasks;
     struct switch_info *switch_info;
@@ -2308,6 +2309,7 @@ static int hls_read_header(AVFormatContext *s)
     }
 
     if (c->abr) {
+        int64_t abr_init_duration = AV_NOPTS_VALUE;
         c->switch_tasks = av_malloc(c->n_playlists * sizeof(struct switch_task));
         if (!c->switch_tasks) {
             ret = AVERROR(ENOMEM);
@@ -2326,6 +2328,15 @@ static int hls_read_header(AVFormatContext *s)
         for (i = 0; i < c->n_playlists; i++) {
             struct playlist *pls = c->playlists[i];
             int type;
+            int64_t start = 0;
+            for (int i = 0; i < c->cur_seq_no + 1; i++) {
+                start += pls->segments[i]->duration;
+            }
+            start = av_rescale_q(start,
+                                    AV_TIME_BASE_Q,
+                                    get_timebase(pls));
+            if (abr_init_duration == AV_NOPTS_VALUE || abr_init_duration > start)
+                abr_init_duration = start;
 
             c->switch_tasks[i].switch_timestamp = AV_NOPTS_VALUE;
             type = playlist_type_simple(pls);
@@ -2337,6 +2348,7 @@ static int hls_read_header(AVFormatContext *s)
                     c->switch_info[SWITCH_VIDEO].pls_index = pls->index;
             }
         }
+        av_dict_set_int(&c->abr_initial, "abr_init_duration", abr_init_duration, 0);
     }
     update_noheader_flag(s);
 
@@ -2768,6 +2780,8 @@ static int hls_probe(const AVProbeData *p)
 static const AVOption hls_options[] = {
     {"abr", "enable abr to switch streams",
         OFFSET(abr), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, FLAGS },
+    {"abr_initial",  "metadata used by abr for ffplay",
+        OFFSET(abr_initial), AV_OPT_TYPE_DICT, { 0 }, 0, 0, FLAGS },
     {"live_start_index", "segment index to start live streams at (negative values are from the end)",
         OFFSET(live_start_index), AV_OPT_TYPE_INT, {.i64 = -3}, INT_MIN, INT_MAX, FLAGS},
     {"allowed_extensions", "List of file extensions that hls is allowed to access",
-- 
2.28.0



More information about the ffmpeg-devel mailing list