|
| 1 | +From 2a82e6a5d412189c243cac24b3c9352e5c80e240 Mon Sep 17 00:00:00 2001 |
| 2 | +From: qianlongxu <qianlongxu@gmail.com> |
| 3 | +Date: Wed, 28 May 2025 17:34:08 +0800 |
| 4 | +Subject: [PATCH] restore ijk hls support discontinuity tag |
| 5 | + |
| 6 | +--- |
| 7 | + libavformat/hls.c | 59 +++++++++++++++++++++++++++++++++++++++++------ |
| 8 | + 1 file changed, 52 insertions(+), 7 deletions(-) |
| 9 | + |
| 10 | +diff --git a/libavformat/hls.c b/libavformat/hls.c |
| 11 | +index b96c5ab..12b49c8 100644 |
| 12 | +--- a/libavformat/hls.c |
| 13 | ++++ b/libavformat/hls.c |
| 14 | +@@ -75,6 +75,8 @@ enum KeyType { |
| 15 | + }; |
| 16 | + |
| 17 | + struct segment { |
| 18 | ++ int64_t previous_duration; |
| 19 | ++ int64_t start_time; |
| 20 | + int64_t duration; |
| 21 | + int64_t url_offset; |
| 22 | + int64_t size; |
| 23 | +@@ -775,6 +777,8 @@ static int test_segment(AVFormatContext *s, const AVInputFormat *in_fmt, struct |
| 24 | + static int parse_playlist(HLSContext *c, const char *url, |
| 25 | + struct playlist *pls, AVIOContext *in) |
| 26 | + { |
| 27 | ++ int64_t previous_duration1 = 0, previous_duration = 0, total_duration = 0; |
| 28 | ++ |
| 29 | + int ret = 0, is_segment = 0, is_variant = 0; |
| 30 | + int64_t duration = 0; |
| 31 | + enum KeyType key_type = KEY_NONE; |
| 32 | +@@ -846,6 +850,7 @@ static int parse_playlist(HLSContext *c, const char *url, |
| 33 | + pls->finished = 0; |
| 34 | + pls->type = PLS_TYPE_UNSPECIFIED; |
| 35 | + } |
| 36 | ++ int start_seq_no = -1; |
| 37 | + while (!avio_feof(in)) { |
| 38 | + ff_get_chomp_line(in, line, sizeof(line)); |
| 39 | + if (av_strstart(line, "#EXT-X-STREAM-INF:", &ptr)) { |
| 40 | +@@ -895,7 +900,11 @@ static int parse_playlist(HLSContext *c, const char *url, |
| 41 | + "INT64_MAX/2, mask out the highest bit\n"); |
| 42 | + seq_no &= INT64_MAX/2; |
| 43 | + } |
| 44 | +- pls->start_seq_no = seq_no; |
| 45 | ++ /* Some buggy HLS servers write #EXT-X-MEDIA-SEQUENCE more than once */ |
| 46 | ++ if (start_seq_no < 0) { |
| 47 | ++ start_seq_no = seq_no; |
| 48 | ++ pls->start_seq_no = seq_no; |
| 49 | ++ } |
| 50 | + } else if (av_strstart(line, "#EXT-X-PLAYLIST-TYPE:", &ptr)) { |
| 51 | + ret = ensure_playlist(c, &pls, url); |
| 52 | + if (ret < 0) |
| 53 | +@@ -960,6 +969,8 @@ static int parse_playlist(HLSContext *c, const char *url, |
| 54 | + } else if (av_strstart(line, "#EXT-X-ENDLIST", &ptr)) { |
| 55 | + if (pls) |
| 56 | + pls->finished = 1; |
| 57 | ++ } else if (av_strstart(line, "#EXT-X-DISCONTINUITY", &ptr)) { |
| 58 | ++ previous_duration = previous_duration1; |
| 59 | + } else if (av_strstart(line, "#EXTINF:", &ptr)) { |
| 60 | + is_segment = 1; |
| 61 | + duration = atof(ptr) * AV_TIME_BASE; |
| 62 | +@@ -1043,6 +1054,11 @@ static int parse_playlist(HLSContext *c, const char *url, |
| 63 | + " set to default value to 1ms.\n", seg->url); |
| 64 | + duration = 0.001 * AV_TIME_BASE; |
| 65 | + } |
| 66 | ++ previous_duration1 += duration; |
| 67 | ++ seg->previous_duration = previous_duration; |
| 68 | ++ seg->start_time = total_duration; |
| 69 | ++ total_duration += duration; |
| 70 | ++ |
| 71 | + seg->duration = duration; |
| 72 | + seg->key_type = key_type; |
| 73 | + dynarray_add(&pls->segments, &pls->n_segments, seg); |
| 74 | +@@ -2360,6 +2376,7 @@ static int hls_read_packet(AVFormatContext *s, AVPacket *pkt) |
| 75 | + * stream */ |
| 76 | + if (pls->needed && !pls->pkt->data) { |
| 77 | + while (1) { |
| 78 | ++ int64_t pkt_ts = AV_NOPTS_VALUE; |
| 79 | + int64_t ts_diff; |
| 80 | + AVRational tb; |
| 81 | + struct segment *seg = NULL; |
| 82 | +@@ -2373,12 +2390,40 @@ static int hls_read_packet(AVFormatContext *s, AVPacket *pkt) |
| 83 | + if (pls->is_id3_timestamped && pls->pkt->stream_index == 0) { |
| 84 | + /* audio elementary streams are id3 timestamped */ |
| 85 | + fill_timing_for_id3_timestamped_stream(pls); |
| 86 | ++ } else { |
| 87 | ++ //discontinuity:ts pts need add up. |
| 88 | ++ if (pls->finished) { |
| 89 | ++ int seq_no = pls->cur_seq_no - pls->start_seq_no; |
| 90 | ++ if (seq_no < pls->n_segments && s->streams[pkt->stream_index]) { |
| 91 | ++ struct segment *seg = pls->segments[seq_no]; |
| 92 | ++ if (seg->previous_duration > 0) { |
| 93 | ++ int64_t pred = av_rescale_q(seg->previous_duration, |
| 94 | ++ AV_TIME_BASE_Q, |
| 95 | ++ s->streams[pkt->stream_index]->time_base); |
| 96 | ++ int64_t max_ts = av_rescale_q(seg->start_time + seg->duration, |
| 97 | ++ AV_TIME_BASE_Q, |
| 98 | ++ s->streams[pkt->stream_index]->time_base); |
| 99 | ++ /* EXTINF duration is not precise enough */ |
| 100 | ++ max_ts += 2 * AV_TIME_BASE; |
| 101 | ++ if (s->start_time > 0) { |
| 102 | ++ max_ts += av_rescale_q(s->start_time, |
| 103 | ++ AV_TIME_BASE_Q, |
| 104 | ++ s->streams[pkt->stream_index]->time_base); |
| 105 | ++ } |
| 106 | ++ if (pls->pkt->dts != AV_NOPTS_VALUE && pls->pkt->dts + pred < max_ts) pls->pkt->dts += pred; |
| 107 | ++ if (pls->pkt->pts != AV_NOPTS_VALUE && pls->pkt->pts + pred < max_ts) pls->pkt->pts += pred; |
| 108 | ++ } |
| 109 | ++ } |
| 110 | ++ } |
| 111 | + } |
| 112 | + |
| 113 | +- if (c->first_timestamp == AV_NOPTS_VALUE && |
| 114 | +- pls->pkt->dts != AV_NOPTS_VALUE) |
| 115 | +- c->first_timestamp = av_rescale_q(pls->pkt->dts, |
| 116 | +- get_timebase(pls), AV_TIME_BASE_Q); |
| 117 | ++ if (pls->pkt->pts != AV_NOPTS_VALUE) |
| 118 | ++ pkt_ts = pls->pkt->pts; |
| 119 | ++ else if (pls->pkt->dts != AV_NOPTS_VALUE) |
| 120 | ++ pkt_ts = pls->pkt->dts; |
| 121 | ++ |
| 122 | ++ if (c->first_timestamp == AV_NOPTS_VALUE && pkt_ts != AV_NOPTS_VALUE) |
| 123 | ++ c->first_timestamp = av_rescale_q(pkt_ts, get_timebase(pls), AV_TIME_BASE_Q); |
| 124 | + } |
| 125 | + |
| 126 | + seg = current_segment(pls); |
| 127 | +@@ -2395,13 +2440,13 @@ static int hls_read_packet(AVFormatContext *s, AVPacket *pkt) |
| 128 | + if (pls->seek_stream_index < 0 || |
| 129 | + pls->seek_stream_index == pls->pkt->stream_index) { |
| 130 | + |
| 131 | +- if (pls->pkt->dts == AV_NOPTS_VALUE) { |
| 132 | ++ if (pkt_ts == AV_NOPTS_VALUE) { |
| 133 | + pls->seek_timestamp = AV_NOPTS_VALUE; |
| 134 | + break; |
| 135 | + } |
| 136 | + |
| 137 | + tb = get_timebase(pls); |
| 138 | +- ts_diff = av_rescale_rnd(pls->pkt->dts, AV_TIME_BASE, |
| 139 | ++ ts_diff = av_rescale_rnd(pkt_ts, AV_TIME_BASE, |
| 140 | + tb.den, AV_ROUND_DOWN) - |
| 141 | + pls->seek_timestamp; |
| 142 | + if (ts_diff >= 0 && (pls->seek_flags & AVSEEK_FLAG_ANY || |
| 143 | +-- |
| 144 | +2.39.5 (Apple Git-154) |
| 145 | + |
0 commit comments