[FFmpeg-cvslog] avformat/hls: add #EXT-X-START tag support by prefer_x_start opt

Li Kai git at videolan.org
Wed Jun 29 09:04:09 EEST 2022


ffmpeg | branch: master | Li Kai <wolfleekay at gmail.com> | Thu Jun 23 00:55:38 2022 +0800| [df8ad4e3914c65d3ebcaac10bac6038eadb70d74] | committer: Steven Liu

avformat/hls: add #EXT-X-START tag support by prefer_x_start opt

Reviewed-by: Steven Liu <liuqi05 at kuaishou.com>
Signed-off-by: Li Kai <wolfleekay at gmail.com>

> http://git.videolan.org/gitweb.cgi/ffmpeg.git/?a=commit;h=df8ad4e3914c65d3ebcaac10bac6038eadb70d74
---

 doc/demuxers.texi |  3 +++
 libavformat/hls.c | 60 +++++++++++++++++++++++++++++++++++++++++++++++++++++--
 2 files changed, 61 insertions(+), 2 deletions(-)

diff --git a/doc/demuxers.texi b/doc/demuxers.texi
index c95a9ae594..2b6dd86c2a 100644
--- a/doc/demuxers.texi
+++ b/doc/demuxers.texi
@@ -373,6 +373,9 @@ It accepts the following options:
 @item live_start_index
 segment index to start live streams at (negative values are from the end).
 
+ at item prefer_x_start
+prefer to use #EXT-X-START if it's in playlist instead of live_start_index.
+
 @item allowed_extensions
 ',' separated list of file extensions that hls is allowed to access.
 
diff --git a/libavformat/hls.c b/libavformat/hls.c
index b736f093a9..3dc7bd3930 100644
--- a/libavformat/hls.c
+++ b/libavformat/hls.c
@@ -120,6 +120,8 @@ struct playlist {
     enum PlaylistType type;
     int64_t target_duration;
     int64_t start_seq_no;
+    int time_offset_flag;
+    int64_t start_time_offset;
     int n_segments;
     struct segment **segments;
     int needed;
@@ -211,6 +213,7 @@ typedef struct HLSContext {
     int64_t cur_seq_no;
     int m3u8_hold_counters;
     int live_start_index;
+    int prefer_x_start;
     int first_packet;
     int64_t first_timestamp;
     int64_t cur_timestamp;
@@ -889,6 +892,21 @@ static int parse_playlist(HLSContext *c, const char *url,
                 cur_init_section->key = NULL;
             }
 
+        } else if (av_strstart(line, "#EXT-X-START:", &ptr)) {
+            const char *time_offset_value = NULL;
+            ret = ensure_playlist(c, &pls, url);
+            if (ret < 0) {
+                goto fail;
+            }
+            if (av_strstart(ptr, "TIME-OFFSET=", &time_offset_value)) {
+                float offset = strtof(time_offset_value, NULL);
+                pls->start_time_offset = offset * AV_TIME_BASE;
+                pls->time_offset_flag = 1;
+            } else {
+                av_log(c->ctx, AV_LOG_WARNING, "#EXT-X-START value is"
+                                                "invalid, it will be ignored");
+                continue;
+            }
         } else if (av_strstart(line, "#EXT-X-ENDLIST", &ptr)) {
             if (pls)
                 pls->finished = 1;
@@ -1722,9 +1740,45 @@ static int64_t select_cur_seq_no(HLSContext *c, struct playlist *pls)
         /* If this is a live stream, start live_start_index segments from the
          * start or end */
         if (c->live_start_index < 0)
-            return pls->start_seq_no + FFMAX(pls->n_segments + c->live_start_index, 0);
+            seq_no = pls->start_seq_no + FFMAX(pls->n_segments +
+                                            c->live_start_index, 0);
         else
-            return pls->start_seq_no + FFMIN(c->live_start_index, pls->n_segments - 1);
+            seq_no = pls->start_seq_no + FFMIN(c->live_start_index,
+                                            pls->n_segments - 1);
+
+        /* If #EXT-X-START in playlist, need to recalculate */
+        if (pls->time_offset_flag && c->prefer_x_start) {
+            int64_t start_timestamp;
+            int64_t playlist_duration = 0;
+            int64_t cur_timestamp = c->cur_timestamp == AV_NOPTS_VALUE ? 0 :
+                                    c->cur_timestamp;
+
+            for (int i = 0; i < pls->n_segments; i++)
+                playlist_duration += pls->segments[i]->duration;
+
+            /* If the absolute value of TIME-OFFSET exceeds
+             * the duration of the playlist, it indicates either the end of the
+             * playlist (if positive) or the beginning of the playlist (if
+             * negative). */
+            if (pls->start_time_offset >=0 &&
+                pls->start_time_offset > playlist_duration)
+                start_timestamp = cur_timestamp + playlist_duration;
+            else if (pls->start_time_offset >= 0 &&
+                        pls->start_time_offset <= playlist_duration)
+                start_timestamp = cur_timestamp + pls->start_time_offset;
+            else if (pls->start_time_offset < 0 &&
+                        pls->start_time_offset < -playlist_duration)
+                start_timestamp = cur_timestamp;
+            else if (pls->start_time_offset < 0 &&
+                        pls->start_time_offset > -playlist_duration)
+                start_timestamp = cur_timestamp + playlist_duration +
+                                    pls->start_time_offset;
+            else
+                start_timestamp = cur_timestamp;
+
+            find_timestamp_in_playlist(c, pls, start_timestamp, &seq_no, NULL);
+        }
+        return seq_no;
     }
 
     /* Otherwise just start on the first segment. */
@@ -2476,6 +2530,8 @@ static int hls_probe(const AVProbeData *p)
 static const AVOption hls_options[] = {
     {"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},
+    {"prefer_x_start", "prefer to use #EXT-X-START if it's in playlist instead of live_start_index",
+        OFFSET(prefer_x_start), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, FLAGS},
     {"allowed_extensions", "List of file extensions that hls is allowed to access",
         OFFSET(allowed_extensions), AV_OPT_TYPE_STRING,
         {.str = "3gp,aac,avi,ac3,eac3,flac,mkv,m3u8,m4a,m4s,m4v,mpg,mov,mp2,mp3,mp4,mpeg,mpegts,ogg,ogv,oga,ts,vob,wav"},



More information about the ffmpeg-cvslog mailing list