From patchwork Thu Jul 16 12:51:15 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hongcheng Zhong X-Patchwork-Id: 21115 Delivered-To: andriy.gelman@gmail.com Received: by 2002:a25:80ca:0:0:0:0:0 with SMTP id c10csp1198356ybm; Thu, 16 Jul 2020 05:52:20 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwgltQ1WHLlUeGZM0QR4b2U7G9IJTmRtjLuVH/VCY4mhOIMFeRh7+ugtrkgaVfN6u2ObAF8 X-Received: by 2002:a7b:c218:: with SMTP id x24mr4071603wmi.109.1594903940372; Thu, 16 Jul 2020 05:52:20 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1594903940; cv=none; d=google.com; s=arc-20160816; b=dO51y4qNn40Ci1Q1nEQTwYYsC/8r4aOfKeY7CKhT1nqTqe5u5hckS5joclajKNkr8H U9SSe4e5HDFe+SFI0DflCALGczuq1UgrdbdE0BB9CpQZVHUPxJ2r/BD/i7whgjkDyH6S cfZjDE8Ec3uzYQoGZXEdlzRYY8gZSYE45EDqd1TDn4O2JsA2GkU6o5Kn1GfH1gbLs2yS xa/rFpWr6mgaf0Jnbt1PUEkDLbcrGl8dQdYb9BhnkmhnQGJ6CT27U/EEtZPy8C/6tzSj 0pYxnVWGnL9+XRPmb/Qz7kOPi+OBVzqgL/VOzJWpEteLBPbhC63BIZbDIxNEe6/0fBqY BaIg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:cc:reply-to :list-subscribe:list-help:list-post:list-archive:list-unsubscribe :list-id:precedence:subject:mime-version:references:in-reply-to :message-id:date:to:from:delivered-to; bh=5nmI6wLYO0ZGYF2Co6c3M51r0imqAmr6V94vhlExuac=; b=TVKVfZ7uXiv+Z0VzKO60WyF/D4gUusUcxqhjqpx0esT3Oh2YQV43QO4o84lqKai6Xo h2YzCegu7SqiUkOLiOpDXMuFjUI8Njk09QajWXYCipCd0qRoA6BqsKRUH0kFQYb+U0c+ tx+Oq6ihnwoq7UV4oq4s4qaZo5GBmFpTXB8x/O9tUkHNBavt4cTz8OiSnOyXKt8w5Bdq 19dKEWOCrD9J4QKNUWmY3h9Bk7bxvwjYI/j5nSrF9V2kwFOLkKrEr8pIIMFGGBo7YO7H dnZ83TDrbN1BLTZJjxhM2KXSt0bzPyOXIOR9gZu2yyT2KH6G3KLQDubAsofPmuBRG5PG F/ew== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id w8si4329159wrn.267.2020.07.16.05.52.20; Thu, 16 Jul 2020 05:52:20 -0700 (PDT) Received-SPF: pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) client-ip=79.124.17.100; Authentication-Results: mx.google.com; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 25EA868B5CF; Thu, 16 Jul 2020 15:51:35 +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 21C9068B5B7 for ; Thu, 16 Jul 2020 15:51: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 805D51008CBD6; Thu, 16 Jul 2020 20:51:24 +0800 (CST) Received: from localhost (localhost.localdomain [127.0.0.1]) by proxy02.sjtu.edu.cn (Postfix) with ESMTP id CD069200B448D; Thu, 16 Jul 2020 20:51:23 +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 5dge18lYTOva; Thu, 16 Jul 2020 20:51:23 +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 C42EF200B4496; Thu, 16 Jul 2020 20:51:22 +0800 (CST) From: Hongcheng Zhong To: ffmpeg-devel@ffmpeg.org Date: Thu, 16 Jul 2020 20:51:15 +0800 Message-Id: <20200716125116.187031-5-sj.hc_Zhong@sjtu.edu.cn> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20200716125116.187031-1-sj.hc_Zhong@sjtu.edu.cn> References: <20200716125116.187031-1-sj.hc_Zhong@sjtu.edu.cn> MIME-Version: 1.0 Subject: [FFmpeg-devel] [RFC][GSoC][PATCH v2 5/6] avformat/utils: add av_packet_buffer_filter to filter packet_buffer 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" X-TUID: TiUV/hBjJxQa Content-Length: 7437 From: spartazhc Add av_packet_buffer_filter to remove AVPackets whose stream_index is not in st_index list. st_index has length of AVMEDIA_TYPE_NB, contains the stream_index of all these media types. 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. v1 fixed: 1. rename function name *_clean to *_filter 2. fix memory leak in ff_packet_buffer_filter 3. update the doc Signed-off-by: spartazhc --- libavformat/avformat.h | 12 ++++++++++ libavformat/internal.h | 16 +++++++++++++ libavformat/utils.c | 54 ++++++++++++++++++++++++++++++++++++++++++ libavformat/version.h | 2 +- 4 files changed, 83 insertions(+), 1 deletion(-) diff --git a/libavformat/avformat.h b/libavformat/avformat.h index e91e7f1d33..c796fd0391 100644 --- a/libavformat/avformat.h +++ b/libavformat/avformat.h @@ -2474,6 +2474,18 @@ int avformat_seek_file(AVFormatContext *s, int stream_index, int64_t min_ts, int */ int avformat_flush(AVFormatContext *s); +/** + * Filter the packet buffer list of the AVFormatContext, remove the AVPackets + * do not need according to st_index. + * Only filter the packet_buffer list. + * + * @param s media file handle + * @param st_index the stream_index list which is needed + * st_index has length of AVMEDIA_TYPE_NB + * in index AVMEDIA_TYPE_XXX contains the stream_index needed of type XXX + */ +int av_packet_buffer_filter(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..58ebcb2e35 100644 --- a/libavformat/internal.h +++ b/libavformat/internal.h @@ -772,6 +772,22 @@ int ff_packet_list_get(AVPacketList **head, AVPacketList **tail, */ void ff_packet_list_free(AVPacketList **head, AVPacketList **tail); +/** + * Remove the AVPackets do not need in the packet buffer list. + * For each type in AVMediaType, at most keep one stream and + * the others will be removed. + * + * @param head List head element + * @param tail List tail element + * @param st_index the stream_index list which is needed + * st_index has length of AVMEDIA_TYPE_NB + * in index AVMEDIA_TYPE_XXX contains the stream_index needed of type XXX + * @return 0 on success. Success is guaranteed + * if the packet list is not empty. + */ +int ff_packet_buffer_filter(AVPacketList **head, AVPacketList **tail, + int *st_index); + void avpriv_register_devices(const AVOutputFormat * const o[], const AVInputFormat * const i[]); #endif /* AVFORMAT_INTERNAL_H */ diff --git a/libavformat/utils.c b/libavformat/utils.c index 807d9f10cb..7674e4ea3d 100644 --- a/libavformat/utils.c +++ b/libavformat/utils.c @@ -1565,6 +1565,60 @@ 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_buffer_filter(AVPacketList **pkt_buffer, + AVPacketList **pkt_buffer_end, + int *st_index) +{ + AVPacketList *pktl, *pktn; + av_assert0(*pkt_buffer); + pktl = *pkt_buffer; + pktn = pktl->next; + + /* num >= 2 */ + while (pktn) { + 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; + } + } + *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_buffer_filter(AVFormatContext *s, int *st_index) +{ + int ret = ff_packet_buffer_filter(&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 75c03fde0a..c17727cf73 100644 --- a/libavformat/version.h +++ b/libavformat/version.h @@ -33,7 +33,7 @@ // Also please add any ticket numbers that you believe might be affected here #define LIBAVFORMAT_VERSION_MAJOR 58 #define LIBAVFORMAT_VERSION_MINOR 48 -#define LIBAVFORMAT_VERSION_MICRO 100 +#define LIBAVFORMAT_VERSION_MICRO 101 #define LIBAVFORMAT_VERSION_INT AV_VERSION_INT(LIBAVFORMAT_VERSION_MAJOR, \ LIBAVFORMAT_VERSION_MINOR, \