From patchwork Fri Jan 15 12:27:19 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Zhao Zhili X-Patchwork-Id: 24962 Return-Path: X-Original-To: patchwork@ffaux-bg.ffmpeg.org Delivered-To: patchwork@ffaux-bg.ffmpeg.org Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by ffaux.localdomain (Postfix) with ESMTP id 1758144B26D for ; Fri, 15 Jan 2021 14:27:37 +0200 (EET) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id D3CB4680C0F; Fri, 15 Jan 2021 14:27:36 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from qq.com (out162-62-58-216.mail.qq.com [162.62.58.216]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id C135268097A for ; Fri, 15 Jan 2021 14:27:29 +0200 (EET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=foxmail.com; s=s201512; t=1610713642; bh=uOxPQ/4upwYhPp7rtfSTP8Zj+c7LUUpaKrVDNw9Bxfo=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=FaZWp5nQegB1lvJbkk58rzdecmXW04bH+A81GYHCwXupg77D7XfHYj3FNJVphvDNw 0hy0Fo05b57emHjS/UHS5elpLlma/SGEi1Axe7ozkK5NKGWQEv1NZq4BYmi5hfpfZD LD4+C4k0wLsGoU7i8bn0+fiZoujkdq3R/Ds0NMp0= Received: from ZHILIZHAO-MB2.tencent.com ([59.37.125.48]) by newxmesmtplogicsvrsza6.qq.com (NewEsmtp) with SMTP id 6D4AEC8F; Fri, 15 Jan 2021 20:27:20 +0800 X-QQ-mid: xmsmtpt1610713640tm1s6zq7d Message-ID: X-QQ-XMAILINFO: MuOyEFI6hXvbqpFQjRs4T84hRSDyTrrXy67IJDxkMqVk1YbNVdKqyF+sVklkQS 8waD1gNmbPodpbXiB0GSWb6yKmo3iar7+bdjfTzll8+E4LF5FoHc5BUPeMwsEv4TAlRyXTGFPBMF R45B0SYOpLjlekvUSBOY65+btGf2sLIq/PjrR9THqYf8S4/pXrFP/O4LuBtA0PQk9plDgfhdYnJV pm5bsUqJ9E9QcpiRHyzcP7xTOEJCTNCRtGmqnvKnNw7vO72bh/QVm7mOG7mYH2ZuHkKUaIR+PJMk YzqwWiIiizs2QoistJXIBGDwWLYMMBi7fXJETq8PjmO4jzGFoaLAuHGiLC3F21XuZf3ovfEkyvlY aFPYlOqyDXbanlgSj/Zr5x5/dsyokY7X+NYaKYffS5oQHXnsQqPNTvw96m2orEHZD9OcJ0VYWtTn xxZF/WGJohI+PRB+ipzPrOltNyE0xSnLdyMi/jndN1i3m7uANRHVn+ANwEG12TTYAN5kO0He86Zp 9rSJtXoABDkuR7kC/nuiiFbr35aNX8j1bvOkfqSRY44ApV2RY6d15WDgN7yExj8q+Hu7GN4U0Pt/ AGl30MWmKOU6uaOu75trYBdnjIHKw3Msm2Qid+GQ9gDuluIXvYAB4W6aIeW+M+sX0jVXy1icGRW+ xeAz3bi/G8Akv6foswi4AlD6ZrGJz13CW3x0yhMTx1qWFGT0BdYl9OuXqHQ0NQvDX682B4SgW0cL ihEiGsJ/k+uWAjPvgnvqLp42PYFWv1DopejwGtKKjIWxAfPMrbv9S88LkudqO+V36lww3fNdhSQ6 AJWyoTlrr2yw3JAw/nXt8ciIl1sY5J7WTolYIldgc1S2udk7knaYmW From: Zhao Zhili To: ffmpeg-devel@ffmpeg.org Date: Fri, 15 Jan 2021 20:27:19 +0800 X-OQ-MSGID: <20210115122719.24367-1-quinkblack@foxmail.com> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20210115114332.337mniq7qezamf56@phare.normalesup.org> References: <20210115114332.337mniq7qezamf56@phare.normalesup.org> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH v2] avformat/hls: change sequence number type to int64_t X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Cc: Zhao Zhili Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Fix atoi() overflow for large EXT-X-MEDIA-SEQUENCE. The spec says the type of sequence number is uint64_t. Use int64_t here since current implementation requires it to be signed integer, and hlsenc use int64_t too. --- v2: AV_WB32 -> AV_WB64 libavformat/hls.c | 49 ++++++++++++++++++++++++----------------------- 1 file changed, 25 insertions(+), 24 deletions(-) diff --git a/libavformat/hls.c b/libavformat/hls.c index 619e4800de..ddf914621f 100644 --- a/libavformat/hls.c +++ b/libavformat/hls.c @@ -112,13 +112,13 @@ struct playlist { int finished; enum PlaylistType type; int64_t target_duration; - int start_seq_no; + int64_t start_seq_no; int n_segments; struct segment **segments; int needed; int broken; - int cur_seq_no; - int last_seq_no; + int64_t cur_seq_no; + int64_t last_seq_no; int m3u8_hold_counters; int64_t cur_seg_offset; int64_t last_load_time; @@ -199,7 +199,7 @@ typedef struct HLSContext { int n_renditions; struct rendition **renditions; - int cur_seq_no; + int64_t cur_seq_no; int m3u8_hold_counters; int live_start_index; int first_packet; @@ -722,7 +722,7 @@ static int parse_playlist(HLSContext *c, const char *url, int is_http = av_strstart(url, "http", NULL); struct segment **prev_segments = NULL; int prev_n_segments = 0; - int prev_start_seq_no = -1; + int64_t prev_start_seq_no = -1; if (is_http && !in && c->http_persistent && c->playlist_pb) { in = c->playlist_pb; @@ -811,7 +811,7 @@ static int parse_playlist(HLSContext *c, const char *url, ret = ensure_playlist(c, &pls, url); if (ret < 0) goto fail; - pls->start_seq_no = atoi(ptr); + pls->start_seq_no = strtoll(ptr, NULL, 10); } else if (av_strstart(line, "#EXT-X-PLAYLIST-TYPE:", &ptr)) { ret = ensure_playlist(c, &pls, url); if (ret < 0) @@ -832,9 +832,9 @@ static int parse_playlist(HLSContext *c, const char *url, if (has_iv) { memcpy(cur_init_section->iv, iv, sizeof(iv)); } else { - int seq = pls->start_seq_no + pls->n_segments; + int64_t seq = pls->start_seq_no + pls->n_segments; memset(cur_init_section->iv, 0, sizeof(cur_init_section->iv)); - AV_WB32(cur_init_section->iv + 12, seq); + AV_WB64(cur_init_section->iv + 8, seq); } if (key_type != KEY_NONE) { @@ -889,9 +889,9 @@ static int parse_playlist(HLSContext *c, const char *url, if (has_iv) { memcpy(seg->iv, iv, sizeof(iv)); } else { - int seq = pls->start_seq_no + pls->n_segments; + int64_t seq = pls->start_seq_no + pls->n_segments; memset(seg->iv, 0, sizeof(seg->iv)); - AV_WB32(seg->iv + 12, seq); + AV_WB64(seg->iv + 8, seq); } if (key_type != KEY_NONE) { @@ -954,16 +954,17 @@ static int parse_playlist(HLSContext *c, const char *url, if (prev_segments) { if (pls->start_seq_no > prev_start_seq_no && c->first_timestamp != AV_NOPTS_VALUE) { int64_t prev_timestamp = c->first_timestamp; - int i, diff = pls->start_seq_no - prev_start_seq_no; + int i; + int64_t diff = pls->start_seq_no - prev_start_seq_no; for (i = 0; i < prev_n_segments && i < diff; i++) { c->first_timestamp += prev_segments[i]->duration; } - av_log(c->ctx, AV_LOG_DEBUG, "Media sequence change (%d -> %d)" + av_log(c->ctx, AV_LOG_DEBUG, "Media sequence change (%"PRId64" -> %"PRId64")" " reflected in first_timestamp: %"PRId64" -> %"PRId64"\n", prev_start_seq_no, pls->start_seq_no, prev_timestamp, c->first_timestamp); } else if (pls->start_seq_no < prev_start_seq_no) { - av_log(c->ctx, AV_LOG_WARNING, "Media sequence changed unexpectedly: %d -> %d\n", + av_log(c->ctx, AV_LOG_WARNING, "Media sequence changed unexpectedly: %"PRId64" -> %"PRId64"\n", prev_start_seq_no, pls->start_seq_no); } free_segment_dynarray(prev_segments, prev_n_segments); @@ -991,7 +992,7 @@ static struct segment *current_segment(struct playlist *pls) static struct segment *next_segment(struct playlist *pls) { - int n = pls->cur_seq_no - pls->start_seq_no + 1; + int64_t n = pls->cur_seq_no - pls->start_seq_no + 1; if (n >= pls->n_segments) return NULL; return pls->segments[n]; @@ -1457,7 +1458,7 @@ reload: } if (v->cur_seq_no < v->start_seq_no) { av_log(v->parent, AV_LOG_WARNING, - "skipping %d segments ahead, expired from playlists\n", + "skipping %"PRId64" segments ahead, expired from playlists\n", v->start_seq_no - v->cur_seq_no); v->cur_seq_no = v->start_seq_no; } @@ -1503,7 +1504,7 @@ reload: if (ret < 0) { if (ff_check_interrupt(c->interrupt_callback)) return AVERROR_EXIT; - av_log(v->parent, AV_LOG_WARNING, "Failed to open segment %d of playlist %d\n", + av_log(v->parent, AV_LOG_WARNING, "Failed to open segment %"PRId64" of playlist %d\n", v->cur_seq_no, v->index); v->cur_seq_no += 1; @@ -1528,7 +1529,7 @@ reload: if (ret < 0) { if (ff_check_interrupt(c->interrupt_callback)) return AVERROR_EXIT; - av_log(v->parent, AV_LOG_WARNING, "Failed to open next segment %d of playlist %d\n", + av_log(v->parent, AV_LOG_WARNING, "Failed to open segment %"PRId64" of playlist %d\n", v->cur_seq_no + 1, v->index); } else { @@ -1625,7 +1626,7 @@ static void add_metadata_from_renditions(AVFormatContext *s, struct playlist *pl /* if timestamp was in valid range: returns 1 and sets seq_no * if not: returns 0 and sets seq_no to closest segment */ static int find_timestamp_in_playlist(HLSContext *c, struct playlist *pls, - int64_t timestamp, int *seq_no) + int64_t timestamp, int64_t *seq_no) { int i; int64_t pos = c->first_timestamp == AV_NOPTS_VALUE ? @@ -1650,9 +1651,9 @@ static int find_timestamp_in_playlist(HLSContext *c, struct playlist *pls, return 0; } -static int select_cur_seq_no(HLSContext *c, struct playlist *pls) +static int64_t select_cur_seq_no(HLSContext *c, struct playlist *pls) { - int seq_no; + int64_t seq_no; if (!pls->finished && !c->first_packet && av_gettime_relative() - pls->last_load_time >= default_reload_interval(pls)) @@ -1844,7 +1845,7 @@ static int hls_read_header(AVFormatContext *s) { HLSContext *c = s->priv_data; int ret = 0, i; - int highest_cur_seq_no = 0; + int64_t highest_cur_seq_no = 0; c->ctx = s; c->interrupt_callback = &s->interrupt_callback; @@ -2077,7 +2078,7 @@ static int recheck_discard_flags(AVFormatContext *s, int first) pls->seek_flags = AVSEEK_FLAG_ANY; pls->seek_stream_index = -1; } - av_log(s, AV_LOG_INFO, "Now receiving playlist %d, segment %d\n", i, pls->cur_seq_no); + av_log(s, AV_LOG_INFO, "Now receiving playlist %d, segment %"PRId64"\n", i, pls->cur_seq_no); } else if (first && !cur_needed && pls->needed) { ff_format_io_close(pls->parent, &pls->input); pls->input_read_done = 0; @@ -2274,10 +2275,10 @@ static int hls_read_seek(AVFormatContext *s, int stream_index, { HLSContext *c = s->priv_data; struct playlist *seek_pls = NULL; - int i, seq_no; - int j; + int i, j; int stream_subdemuxer_index; int64_t first_timestamp, seek_timestamp, duration; + int64_t seq_no; if ((flags & AVSEEK_FLAG_BYTE) || (c->ctx->ctx_flags & AVFMTCTX_UNSEEKABLE)) return AVERROR(ENOSYS);