From patchwork Sun Jul 5 11:34:58 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hongcheng Zhong X-Patchwork-Id: 20817 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 63A1844BDAF for ; Sun, 5 Jul 2020 14:35:34 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 4BD7068B31C; Sun, 5 Jul 2020 14:35:34 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from smtp181.sjtu.edu.cn (smtp181.sjtu.edu.cn [202.120.2.181]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id EA37A68B205 for ; Sun, 5 Jul 2020 14:35:27 +0300 (EEST) Received: from proxy02.sjtu.edu.cn (smtp188.sjtu.edu.cn [202.120.2.188]) by smtp181.sjtu.edu.cn (Postfix) with ESMTPS id 3E0541008CBCF; Sun, 5 Jul 2020 19:35:25 +0800 (CST) Received: from localhost (localhost.localdomain [127.0.0.1]) by proxy02.sjtu.edu.cn (Postfix) with ESMTP id 37C17200B4497; Sun, 5 Jul 2020 19:35:25 +0800 (CST) X-Virus-Scanned: amavisd-new at Received: from proxy02.sjtu.edu.cn ([127.0.0.1]) by localhost (proxy02.sjtu.edu.cn [127.0.0.1]) (amavisd-new, port 10026) with ESMTP id JdzbI0CM3f-L; Sun, 5 Jul 2020 19:35:25 +0800 (CST) Received: from localhost.localdomain (unknown [10.162.157.92]) (Authenticated sender: sj.hc_Zhong@sjtu.edu.cn) by proxy02.sjtu.edu.cn (Postfix) with ESMTPSA id 6062E200B4496; Sun, 5 Jul 2020 19:35:23 +0800 (CST) From: Hongcheng Zhong To: ffmpeg-devel@ffmpeg.org Date: Sun, 5 Jul 2020 19:34:58 +0800 Message-Id: <20200705113459.77467-5-sj.hc_Zhong@sjtu.edu.cn> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20200705113459.77467-1-sj.hc_Zhong@sjtu.edu.cn> References: <20200705113459.77467-1-sj.hc_Zhong@sjtu.edu.cn> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH] [GSoC 5/6] avformat/utils: add av_packet_clean to remove AVPackets not needed in pktl 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: spartazhc Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" From: spartazhc Add av_packet_clean to remove AVPackets whose stream_index is not in st_index list. Generally s->internal->packet_buffer may have pkts from different stream, and stream_index will be used to discard pkt that is not needed. But in case of abr, several streams may pass the stream_index check. So we need a function to remove AVPackets not needed in pktl added by hls_read_header. Signed-off-by: spartazhc --- libavformat/avformat.h | 9 +++++++ libavformat/internal.h | 15 +++++++++++ libavformat/utils.c | 59 ++++++++++++++++++++++++++++++++++++++++++ libavformat/version.h | 2 +- 4 files changed, 84 insertions(+), 1 deletion(-) diff --git a/libavformat/avformat.h b/libavformat/avformat.h index e91e7f1d33..90dee0d075 100644 --- a/libavformat/avformat.h +++ b/libavformat/avformat.h @@ -2474,6 +2474,15 @@ int avformat_seek_file(AVFormatContext *s, int stream_index, int64_t min_ts, int */ int avformat_flush(AVFormatContext *s); +/** + * Remove the AVPackets do not needed in the packet list. + * The behaviour is undefined if the packet list is empty. + * + * @param s media file handle + * @param st_index the stream_index list which is needed + */ +int av_packet_clean(AVFormatContext *s, int *st_index); + /** * Start playing a network-based stream (e.g. RTSP stream) at the * current position. diff --git a/libavformat/internal.h b/libavformat/internal.h index 17a6ab07d3..ac943b1441 100644 --- a/libavformat/internal.h +++ b/libavformat/internal.h @@ -764,6 +764,21 @@ int ff_packet_list_put(AVPacketList **head, AVPacketList **tail, int ff_packet_list_get(AVPacketList **head, AVPacketList **tail, AVPacket *pkt); +/** + * Remove the AVPackets do not needed in the packet list. + * The behaviour is undefined if the packet list is empty. + * + * @note The pkt will be overwritten completely. The caller owns the + * packet and must unref it by itself. + * + * @param head List head element + * @param tail List tail element + * @param st_index the stream_index list which is needed + * @return 0 on success. Success is guaranteed + * if the packet list is not empty. + */ +int ff_packet_list_clean(AVPacketList **head, AVPacketList **tail, + int *st_index); /** * Wipe the list and unref all the packets in it. * diff --git a/libavformat/utils.c b/libavformat/utils.c index 45a4179552..26b5a08a19 100644 --- a/libavformat/utils.c +++ b/libavformat/utils.c @@ -1565,6 +1565,65 @@ int ff_packet_list_get(AVPacketList **pkt_buffer, return 0; } +/** + * return 1 if needed + */ +static int ff_check_st_index(int st, int *st_index) +{ + for (int i = 0; i < AVMEDIA_TYPE_NB; ++i) { + if (st_index[i] == st) + return 1; + } + return 0; +} + +int ff_packet_list_clean(AVPacketList **pkt_buffer, + AVPacketList **pkt_buffer_end, + int *st_index) +{ + AVPacketList *pktl, *pktn; + av_assert0(*pkt_buffer); + pktl = *pkt_buffer; + pktn = pktl->next; + + /* num >= 3 */ + while (pktn && pktn != *pkt_buffer_end) { + if (!ff_check_st_index(pktn->pkt.stream_index, st_index)) { + av_packet_unref(&pktn->pkt); + pktl->next = pktn->next; + av_freep(pktn); + pktn = pktl->next; + } else { + pktl = pktn; + pktn = pktn->next; + } + } + /* last one*/ + if (pktn && !ff_check_st_index(pktn->pkt.stream_index, st_index)) { + av_packet_unref(&pktn->pkt); + av_freep(pktn); + pktl->next = NULL; + *pkt_buffer_end = pktl; + } + /* first one*/ + pktl = *pkt_buffer; + if (!ff_check_st_index(pktl->pkt.stream_index, st_index)) { + av_packet_unref(&pktl->pkt); + *pkt_buffer = pktl->next; + if (!pktl->next) + *pkt_buffer_end = NULL; + av_freep(&pktl); + } + + return 0; +} + +int av_packet_clean(AVFormatContext *s, int *st_index) { + int ret = ff_packet_list_clean(&s->internal->packet_buffer, + &s->internal->packet_buffer_end, st_index); + return ret; +} + static int64_t ts_to_samples(AVStream *st, int64_t ts) { return av_rescale(ts, st->time_base.num * st->codecpar->sample_rate, st->time_base.den); diff --git a/libavformat/version.h b/libavformat/version.h index 3c1957b00c..75c03fde0a 100644 --- a/libavformat/version.h +++ b/libavformat/version.h @@ -32,7 +32,7 @@ // Major bumping may affect Ticket5467, 5421, 5451(compatibility with Chromium) // Also please add any ticket numbers that you believe might be affected here #define LIBAVFORMAT_VERSION_MAJOR 58 -#define LIBAVFORMAT_VERSION_MINOR 47 +#define LIBAVFORMAT_VERSION_MINOR 48 #define LIBAVFORMAT_VERSION_MICRO 100 #define LIBAVFORMAT_VERSION_INT AV_VERSION_INT(LIBAVFORMAT_VERSION_MAJOR, \