From patchwork Wed Apr 8 22:42:30 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marton Balint X-Patchwork-Id: 18786 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 AD95C4497E1 for ; Thu, 9 Apr 2020 01:42:44 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 890C368B75F; Thu, 9 Apr 2020 01:42:44 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from iq.passwd.hu (iq.passwd.hu [217.27.212.140]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 17B8468B6F6 for ; Thu, 9 Apr 2020 01:42:38 +0300 (EEST) Received: from localhost (localhost [127.0.0.1]) by iq.passwd.hu (Postfix) with ESMTP id C92A1E3CEF; Thu, 9 Apr 2020 00:42:37 +0200 (CEST) X-Virus-Scanned: amavisd-new at passwd.hu Received: from iq.passwd.hu ([127.0.0.1]) by localhost (iq.passwd.hu [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id HxD6I4lsE2B4; Thu, 9 Apr 2020 00:42:35 +0200 (CEST) Received: from bluegene.passwd.hu (localhost [127.0.0.1]) by iq.passwd.hu (Postfix) with ESMTP id A0A04E3472; Thu, 9 Apr 2020 00:42:35 +0200 (CEST) From: Marton Balint To: ffmpeg-devel@ffmpeg.org Date: Thu, 9 Apr 2020 00:42:30 +0200 Message-Id: <20200408224231.23126-1-cus@passwd.hu> X-Mailer: git-send-email 2.16.4 Subject: [FFmpeg-devel] [PATCH 1/2] avcodec/bsf: use simplified algorithm for bsf_list chained filtering 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: Marton Balint MIME-Version: 1.0 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Based on the one in ffmpeg.c and it is not using an extra flush_idx variable. Signed-off-by: Marton Balint --- libavcodec/bsf.c | 64 ++++++++++++++++++++++---------------------------------- 1 file changed, 25 insertions(+), 39 deletions(-) diff --git a/libavcodec/bsf.c b/libavcodec/bsf.c index 7b96183e64..b9fc771a88 100644 --- a/libavcodec/bsf.c +++ b/libavcodec/bsf.c @@ -18,6 +18,7 @@ #include +#include "libavutil/avassert.h" #include "libavutil/log.h" #include "libavutil/mem.h" #include "libavutil/opt.h" @@ -266,7 +267,6 @@ typedef struct BSFListContext { int nb_bsfs; unsigned idx; // index of currently processed BSF - unsigned flushed_idx; // index of BSF being flushed char * item_name; } BSFListContext; @@ -304,57 +304,43 @@ fail: static int bsf_list_filter(AVBSFContext *bsf, AVPacket *out) { BSFListContext *lst = bsf->priv_data; - int ret; + int ret, eof = 0; if (!lst->nb_bsfs) return ff_bsf_get_packet_ref(bsf, out); while (1) { - if (lst->idx > lst->flushed_idx) { + /* get a packet from the previous filter up the chain */ + if (lst->idx) ret = av_bsf_receive_packet(lst->bsfs[lst->idx-1], out); - if (ret == AVERROR(EAGAIN)) { - /* no more packets from idx-1, try with previous */ - lst->idx--; - continue; - } else if (ret == AVERROR_EOF) { - /* filter idx-1 is done, continue with idx...nb_bsfs */ - lst->flushed_idx = lst->idx; - continue; - }else if (ret < 0) { - /* filtering error */ - break; - } - } else { + else ret = ff_bsf_get_packet_ref(bsf, out); - if (ret == AVERROR_EOF) { - lst->idx = lst->flushed_idx; - } else if (ret < 0) - break; - } + if (ret == AVERROR(EAGAIN)) { + if (!lst->idx) + return ret; + lst->idx--; + continue; + } else if (ret == AVERROR_EOF) { + eof = 1; + } else if (ret < 0) + return ret; + /* send it to the next filter down the chain */ if (lst->idx < lst->nb_bsfs) { - AVPacket *pkt; - if (ret == AVERROR_EOF && lst->idx == lst->flushed_idx) { - /* ff_bsf_get_packet_ref returned EOF and idx is first - * filter of yet not flushed filter chain */ - pkt = NULL; - } else { - pkt = out; + ret = av_bsf_send_packet(lst->bsfs[lst->idx], eof ? NULL : out); + av_assert1(ret != AVERROR(EAGAIN)); + if (ret < 0) { + av_packet_unref(out); + return ret; } - ret = av_bsf_send_packet(lst->bsfs[lst->idx], pkt); - if (ret < 0) - break; lst->idx++; + eof = 0; + } else if (eof) { + return ret; } else { - /* The end of filter chain, break to return result */ - break; + return 0; } } - - if (ret < 0) - av_packet_unref(out); - - return ret; } static void bsf_list_flush(AVBSFContext *bsf) @@ -363,7 +349,7 @@ static void bsf_list_flush(AVBSFContext *bsf) for (int i = 0; i < lst->nb_bsfs; i++) av_bsf_flush(lst->bsfs[i]); - lst->idx = lst->flushed_idx = 0; + lst->idx = 0; } static void bsf_list_close(AVBSFContext *bsf) From patchwork Wed Apr 8 22:42:31 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marton Balint X-Patchwork-Id: 18787 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 ABD6A4497E1 for ; Thu, 9 Apr 2020 01:42:48 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 8E7CE68B7FF; Thu, 9 Apr 2020 01:42:48 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from iq.passwd.hu (iq.passwd.hu [217.27.212.140]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 8269568B76B for ; Thu, 9 Apr 2020 01:42:41 +0300 (EEST) Received: from localhost (localhost [127.0.0.1]) by iq.passwd.hu (Postfix) with ESMTP id 6231CE3CF2; Thu, 9 Apr 2020 00:42:41 +0200 (CEST) X-Virus-Scanned: amavisd-new at passwd.hu Received: from iq.passwd.hu ([127.0.0.1]) by localhost (iq.passwd.hu [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 72Ya51dzO42g; Thu, 9 Apr 2020 00:42:39 +0200 (CEST) Received: from bluegene.passwd.hu (localhost [127.0.0.1]) by iq.passwd.hu (Postfix) with ESMTP id 67753E3472; Thu, 9 Apr 2020 00:42:39 +0200 (CEST) From: Marton Balint To: ffmpeg-devel@ffmpeg.org Date: Thu, 9 Apr 2020 00:42:31 +0200 Message-Id: <20200408224231.23126-2-cus@passwd.hu> X-Mailer: git-send-email 2.16.4 In-Reply-To: <20200408224231.23126-1-cus@passwd.hu> References: <20200408224231.23126-1-cus@passwd.hu> Subject: [FFmpeg-devel] [PATCH 2/2] avcodec/bsf: add av_bsf_join() to chain bitstream filters 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: Marton Balint MIME-Version: 1.0 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Signed-off-by: Marton Balint --- doc/APIchanges | 3 +++ libavcodec/avcodec.h | 19 ++++++++++++++++ libavcodec/bsf.c | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++++ libavcodec/version.h | 2 +- 4 files changed, 85 insertions(+), 1 deletion(-) diff --git a/doc/APIchanges b/doc/APIchanges index f1d7eac2ee..1473742139 100644 --- a/doc/APIchanges +++ b/doc/APIchanges @@ -15,6 +15,9 @@ libavutil: 2017-10-21 API changes, most recent first: +2020-04-xx - xxxxxxxxxx - lavc 58.77.102 - avcodec.h + Add av_bsf_join() to chain bitstream filters. + 2020-03-29 - xxxxxxxxxx - lavf 58.42.100 - avformat.h av_read_frame() now guarantees to handle uninitialized input packets and to return refcounted packets on success. diff --git a/libavcodec/avcodec.h b/libavcodec/avcodec.h index 8fc0ad92c9..2812055e8a 100644 --- a/libavcodec/avcodec.h +++ b/libavcodec/avcodec.h @@ -6056,6 +6056,25 @@ int av_bsf_receive_packet(AVBSFContext *ctx, AVPacket *pkt); */ void av_bsf_flush(AVBSFContext *ctx); +/** + * Join a new bitstream filter to an already operating one. + * + * @param[in,out] out_bsf *out_bsf contains the existing, already initialized bitstream + * filter, the new filter will be joined to the output of this. + * It can be NULL, in which case an empty filter is assumed. + * Upon successful return *out_bsf will contain the new, combined + * bitstream filter which will be initialized. + * @param[in] bsf the bitstream filter to be joined to the output of *out_bsf. + * This filter must be uninitialized but it must be ready to be + * initalized, so input codec parameters and time base must be + * set if *out_bsf is NULL, otherwise the function will set these + * automatically based on the output parameters of *out_bsf. + * Upon successful return the bsf will be initialized. + * + * @return 0 on success, negative on error. + */ +int av_bsf_join(AVBSFContext **out_bsf, AVBSFContext *bsf); + /** * Free a bitstream filter context and everything associated with it; write NULL * into the supplied pointer. diff --git a/libavcodec/bsf.c b/libavcodec/bsf.c index b9fc771a88..1bca28d1ae 100644 --- a/libavcodec/bsf.c +++ b/libavcodec/bsf.c @@ -487,6 +487,68 @@ end: return ret; } +int av_bsf_join(AVBSFContext **out_bsf, AVBSFContext *bsf) +{ + BSFListContext *lst; + AVBSFContext *obsf = *out_bsf; + AVBSFContext *new_list_bsf = NULL; + int ret; + + if (!obsf) { + ret = av_bsf_init(bsf); + if (ret < 0) + return ret; + *out_bsf = bsf; + return 0; + } + + if (obsf->filter != &ff_list_bsf) { + ret = av_bsf_alloc(&ff_list_bsf, &new_list_bsf); + if (ret < 0) + return ret; + lst = new_list_bsf->priv_data; + ret = av_dynarray_add_nofree(&lst->bsfs, &lst->nb_bsfs, obsf); + if (ret < 0) + goto fail; + ret = avcodec_parameters_copy(new_list_bsf->par_in, obsf->par_in); + if (ret < 0) + goto fail; + new_list_bsf->time_base_in = obsf->time_base_in; + obsf = new_list_bsf; + } else { + lst = obsf->priv_data; + } + + ret = avcodec_parameters_copy(bsf->par_in, lst->bsfs[lst->nb_bsfs-1]->par_out); + if (ret < 0) + goto fail; + bsf->time_base_in = lst->bsfs[lst->nb_bsfs-1]->time_base_out; + + ret = av_bsf_init(&bsf); + if (ret < 0) + goto fail; + + ret = avcodec_parameters_copy(obsf->par_out, bsf->par_out); + if (ret < 0) + goto fail; + obsf->time_base_out = bsf->time_base_out; + + ret = av_dynarray_add_nofree(&lst->bsfs, &lst->nb_bsfs, bsf); + if (ret < 0) + goto fail; + + *out_bsf = obsf; + return 0; + +fail: + if (new_list_bsf) { + if (lst->nb_bsfs) + lst->bsfs[0] = NULL; + av_bsf_free(&new_list_bsf); + } + return ret; +} + static int bsf_parse_single(const char *str, AVBSFList *bsf_lst) { char *bsf_name, *bsf_options_str, *buf; diff --git a/libavcodec/version.h b/libavcodec/version.h index f4d1d4de21..dadca75430 100644 --- a/libavcodec/version.h +++ b/libavcodec/version.h @@ -29,7 +29,7 @@ #define LIBAVCODEC_VERSION_MAJOR 58 #define LIBAVCODEC_VERSION_MINOR 77 -#define LIBAVCODEC_VERSION_MICRO 101 +#define LIBAVCODEC_VERSION_MICRO 102 #define LIBAVCODEC_VERSION_INT AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \ LIBAVCODEC_VERSION_MINOR, \