From patchwork Thu Sep 10 23:49:10 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 22273 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 8708344A710 for ; Fri, 11 Sep 2020 02:49:40 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 66EEB68B9D7; Fri, 11 Sep 2020 02:49:40 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wr1-f41.google.com (mail-wr1-f41.google.com [209.85.221.41]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 3ED9468B9CB for ; Fri, 11 Sep 2020 02:49:34 +0300 (EEST) Received: by mail-wr1-f41.google.com with SMTP id j2so9441446wrx.7 for ; Thu, 10 Sep 2020 16:49:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=cNn1xNjJuYYXEKRjM1SFSHnP5LR8Wo4vhqy8jGFBjkI=; b=Lm5q1YzfDMAKoUK05zDvLq/bDucWqOOOjOv72UDU9EWi8zELAg+nSWrSCK9UsnWK5a ULPJAJwn6u4phGB7aPQU47fjvaJHH9r15FJbeQgMJCGubjOhWqiVb7Y3IpgrHkrX/ZVi aKyDFbTPnH/322eB0PhvRdHnf+TfNHhFoOg8J6+FCsTP22WdnOs2MU/UYJlIXFCxEMPK N9rVjSm/hAPoHVzISeCQ/utJlhnHX7JJ+bKjNLpPyn+3SnKzUgvTo0ioM9CXaDg15jm3 dADjJXUCyWFImfVQqj4ki3ezCt5/iEL5+Me+a4iic6AfuFx5W5XnyBBROokFHpZxmVR0 fklg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=cNn1xNjJuYYXEKRjM1SFSHnP5LR8Wo4vhqy8jGFBjkI=; b=k6EG53pDqDw2sBTxav9CxZcNEN73ihvzSHnQSiWPiLBVyuKMmSMyFktrj4ZRTZ6x/k DfiWMtMEm3dgThBigTB8vrK2jzvDrxTWgdkGvbRjOMt1yV4gjSbj2v7/4jWPWWWPNTtP fB3prYuwTp9GkShwCKnHx7H1oiu2iVf/FO7cJfRR1XVhoS7tBQOs2P7B2ZISWkiQ7D2X aqmdc0rKJJmYIXSkqiYvlhfaRVgyZhG3Ycz7718MKw+iwUfdv9Hw58fv6CZCX9BysKvu GL8AjHFGlsaqbkNqInLdWDWeJ53CEt8vBFMXcGWqKU/vaxoi0ANcInvQLbOPLrbXQkue 0sfg== X-Gm-Message-State: AOAM5331c48rwd9b9ZqihcinaDG892k2VMwlfDaSRUmBBT7QvV4d3wih zFHiS5xpL+k0qZLTnERJAMr03CCW47E= X-Google-Smtp-Source: ABdhPJznQ0OXeozcG84bQYmd/Yn/R6Sj9qrhtXASUqnBs1pmWdeFgAvJQQydzcTM3fOjEvI2f4DBvw== X-Received: by 2002:a05:6000:118a:: with SMTP id g10mr11619665wrx.67.1599781773166; Thu, 10 Sep 2020 16:49:33 -0700 (PDT) Received: from sblaptop.fritz.box (ipbcc1fb0f.dynamic.kabel-deutschland.de. [188.193.251.15]) by smtp.gmail.com with ESMTPSA id j135sm586592wmj.20.2020.09.10.16.49.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 10 Sep 2020 16:49:32 -0700 (PDT) From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Fri, 11 Sep 2020 01:49:10 +0200 Message-Id: <20200910234911.12891-2-andreas.rheinhardt@gmail.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20200910234911.12891-1-andreas.rheinhardt@gmail.com> References: <20200910234911.12891-1-andreas.rheinhardt@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 2/3] avfilter/af_aformat, formats: Unify freeing AVFilterFormatsConfig 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: Andreas Rheinhardt Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" AVFilterFormats and AVFilterChannelLayouts always exist together, namely in the aformat filter as well as in AVFilterLinks. Now that they are grouped together in a single structure in AVFilterLinks, the same can be done in aformat and then this structure can be freed on its own, replacing the freeing functions for AVFilterFormats and AVFilterChannelLayouts structures (which still exist as static functions in formats.c). Signed-off-by: Andreas Rheinhardt --- Btw: There are several quite common combinations of functions like e.g. ff_make_format_list() and ff_formats_ref(). Should they be factored out in new functions or not? libavfilter/af_aformat.c | 26 ++++----- libavfilter/avfilter.c | 8 +-- libavfilter/avfiltergraph.c | 8 +-- libavfilter/formats.c | 105 +++++++++++++++++++----------------- libavfilter/formats.h | 13 ++--- 5 files changed, 76 insertions(+), 84 deletions(-) diff --git a/libavfilter/af_aformat.c b/libavfilter/af_aformat.c index e669f2de83..a8dbdd8147 100644 --- a/libavfilter/af_aformat.c +++ b/libavfilter/af_aformat.c @@ -36,9 +36,7 @@ typedef struct AFormatContext { const AVClass *class; - AVFilterFormats *formats; - AVFilterFormats *sample_rates; - AVFilterChannelLayouts *channel_layouts; + AVFilterFormatsConfig config; char *formats_str; char *sample_rates_str; @@ -100,11 +98,11 @@ static av_cold int init(AVFilterContext *ctx) { AFormatContext *s = ctx->priv; - PARSE_FORMATS(s->formats_str, enum AVSampleFormat, s->formats, + PARSE_FORMATS(s->formats_str, enum AVSampleFormat, s->config.formats, ff_add_format, av_get_sample_fmt, AV_SAMPLE_FMT_NONE, "sample format"); - PARSE_FORMATS(s->sample_rates_str, int, s->sample_rates, ff_add_format, + PARSE_FORMATS(s->sample_rates_str, int, s->config.samplerates, ff_add_format, get_sample_rate, 0, "sample rate"); - PARSE_FORMATS(s->channel_layouts_str, uint64_t, s->channel_layouts, + PARSE_FORMATS(s->channel_layouts_str, uint64_t, s->config.channel_layouts, ff_add_channel_layout, av_get_channel_layout, 0, "channel layout"); @@ -115,9 +113,7 @@ static av_cold void uninit(AVFilterContext *ctx) { AFormatContext *s = ctx->priv; - ff_formats_unref(&s->formats); - ff_formats_unref(&s->sample_rates); - ff_channel_layouts_unref(&s->channel_layouts); + ff_filter_formats_config_unref(&s->config); } static int query_formats(AVFilterContext *ctx) @@ -125,19 +121,19 @@ static int query_formats(AVFilterContext *ctx) AFormatContext *s = ctx->priv; int ret; - ret = ff_set_common_formats(ctx, s->formats ? s->formats : + ret = ff_set_common_formats(ctx, s->config.formats ? s->config.formats : ff_all_formats(AVMEDIA_TYPE_AUDIO)); - s->formats = NULL; + s->config.formats = NULL; if (ret < 0) return ret; - ret = ff_set_common_samplerates(ctx, s->sample_rates ? s->sample_rates : + ret = ff_set_common_samplerates(ctx, s->config.samplerates ? s->config.samplerates : ff_all_samplerates()); - s->sample_rates = NULL; + s->config.samplerates = NULL; if (ret < 0) return ret; - ret = ff_set_common_channel_layouts(ctx, s->channel_layouts ? s->channel_layouts : + ret = ff_set_common_channel_layouts(ctx, s->config.channel_layouts ? s->config.channel_layouts : ff_all_channel_counts()); - s->channel_layouts = NULL; + s->config.channel_layouts = NULL; return ret; } diff --git a/libavfilter/avfilter.c b/libavfilter/avfilter.c index 8ff22c71e3..06cf564270 100644 --- a/libavfilter/avfilter.c +++ b/libavfilter/avfilter.c @@ -746,12 +746,8 @@ static void free_link(AVFilterLink *link) av_buffer_unref(&link->hw_frames_ctx); - ff_formats_unref(&link->incfg.formats); - ff_formats_unref(&link->outcfg.formats); - ff_formats_unref(&link->incfg.samplerates); - ff_formats_unref(&link->outcfg.samplerates); - ff_channel_layouts_unref(&link->incfg.channel_layouts); - ff_channel_layouts_unref(&link->outcfg.channel_layouts); + ff_filter_formats_config_unref(&link->incfg); + ff_filter_formats_config_unref(&link->outcfg); avfilter_link_free(&link); } diff --git a/libavfilter/avfiltergraph.c b/libavfilter/avfiltergraph.c index f6b572b3de..831fc2b985 100644 --- a/libavfilter/avfiltergraph.c +++ b/libavfilter/avfiltergraph.c @@ -727,12 +727,8 @@ static int pick_format(AVFilterLink *link, AVFilterLink *ref) link->channels = av_get_channel_layout_nb_channels(link->channel_layout); } - ff_formats_unref(&link->incfg.formats); - ff_formats_unref(&link->outcfg.formats); - ff_formats_unref(&link->incfg.samplerates); - ff_formats_unref(&link->outcfg.samplerates); - ff_channel_layouts_unref(&link->incfg.channel_layouts); - ff_channel_layouts_unref(&link->outcfg.channel_layouts); + ff_filter_formats_config_unref(&link->incfg); + ff_filter_formats_config_unref(&link->outcfg); return 0; } diff --git a/libavfilter/formats.c b/libavfilter/formats.c index c54aff688f..98ff82a887 100644 --- a/libavfilter/formats.c +++ b/libavfilter/formats.c @@ -255,6 +255,55 @@ int ff_merge_channel_layouts(AVFilterChannelLayouts *a, return 1; } +#define FIND_REF_INDEX(ref, idx) \ +do { \ + int i; \ + for (i = 0; i < (*ref)->refcount; i ++) \ + if((*ref)->refs[i] == ref) { \ + idx = i; \ + break; \ + } \ +} while (0) + +#define FORMATS_UNREF(ref, list) \ +do { \ + int idx = -1; \ + \ + if (!ref || !*ref) \ + return; \ + \ + FIND_REF_INDEX(ref, idx); \ + \ + if (idx >= 0) { \ + memmove((*ref)->refs + idx, (*ref)->refs + idx + 1, \ + sizeof(*(*ref)->refs) * ((*ref)->refcount - idx - 1)); \ + --(*ref)->refcount; \ + } \ + if (!(*ref)->refcount) { \ + av_free((*ref)->list); \ + av_free((*ref)->refs); \ + av_free(*ref); \ + } \ + *ref = NULL; \ +} while (0) + +static void formats_unref(AVFilterFormats **ref) +{ + FORMATS_UNREF(ref, formats); +} + +static void channel_layouts_unref(AVFilterChannelLayouts **ref) +{ + FORMATS_UNREF(ref, channel_layouts); +} + +void ff_filter_formats_config_unref(AVFilterFormatsConfig *cfg) +{ + formats_unref(&cfg->formats); + formats_unref(&cfg->samplerates); + channel_layouts_unref(&cfg->channel_layouts); +} + int ff_fmt_is_in(int fmt, const int *fmts) { const int *p; @@ -332,14 +381,14 @@ do { \ int ff_add_format(AVFilterFormats **avff, int64_t fmt) { - ADD_FORMAT(avff, fmt, ff_formats_unref, int, formats, nb_formats); + ADD_FORMAT(avff, fmt, formats_unref, int, formats, nb_formats); return 0; } int ff_add_channel_layout(AVFilterChannelLayouts **l, uint64_t channel_layout) { av_assert1(!(*l && (*l)->all_layouts)); - ADD_FORMAT(l, channel_layout, ff_channel_layouts_unref, uint64_t, channel_layouts, nb_channel_layouts); + ADD_FORMAT(l, channel_layout, channel_layouts_unref, uint64_t, channel_layouts, nb_channel_layouts); return 0; } @@ -461,54 +510,12 @@ AVFilterChannelLayouts *ff_all_channel_counts(void) int ff_channel_layouts_ref(AVFilterChannelLayouts *f, AVFilterChannelLayouts **ref) { - FORMATS_REF(f, ref, ff_channel_layouts_unref); + FORMATS_REF(f, ref, channel_layouts_unref); } int ff_formats_ref(AVFilterFormats *f, AVFilterFormats **ref) { - FORMATS_REF(f, ref, ff_formats_unref); -} - -#define FIND_REF_INDEX(ref, idx) \ -do { \ - int i; \ - for (i = 0; i < (*ref)->refcount; i ++) \ - if((*ref)->refs[i] == ref) { \ - idx = i; \ - break; \ - } \ -} while (0) - -#define FORMATS_UNREF(ref, list) \ -do { \ - int idx = -1; \ - \ - if (!ref || !*ref) \ - return; \ - \ - FIND_REF_INDEX(ref, idx); \ - \ - if (idx >= 0) { \ - memmove((*ref)->refs + idx, (*ref)->refs + idx + 1, \ - sizeof(*(*ref)->refs) * ((*ref)->refcount - idx - 1)); \ - --(*ref)->refcount; \ - } \ - if (!(*ref)->refcount) { \ - av_free((*ref)->list); \ - av_free((*ref)->refs); \ - av_free(*ref); \ - } \ - *ref = NULL; \ -} while (0) - -void ff_formats_unref(AVFilterFormats **ref) -{ - FORMATS_UNREF(ref, formats); -} - -void ff_channel_layouts_unref(AVFilterChannelLayouts **ref) -{ - FORMATS_UNREF(ref, channel_layouts); + FORMATS_REF(f, ref, formats_unref); } #define FORMATS_CHANGEREF(oldref, newref) \ @@ -570,14 +577,14 @@ int ff_set_common_channel_layouts(AVFilterContext *ctx, AVFilterChannelLayouts *channel_layouts) { SET_COMMON_FORMATS(ctx, channel_layouts, - ff_channel_layouts_ref, ff_channel_layouts_unref); + ff_channel_layouts_ref, channel_layouts_unref); } int ff_set_common_samplerates(AVFilterContext *ctx, AVFilterFormats *samplerates) { SET_COMMON_FORMATS(ctx, samplerates, - ff_formats_ref, ff_formats_unref); + ff_formats_ref, formats_unref); } /** @@ -588,7 +595,7 @@ int ff_set_common_samplerates(AVFilterContext *ctx, int ff_set_common_formats(AVFilterContext *ctx, AVFilterFormats *formats) { SET_COMMON_FORMATS(ctx, formats, - ff_formats_ref, ff_formats_unref); + ff_formats_ref, formats_unref); } int ff_default_query_formats(AVFilterContext *ctx) diff --git a/libavfilter/formats.h b/libavfilter/formats.h index 0520bf42a9..8a7dbb2f97 100644 --- a/libavfilter/formats.h +++ b/libavfilter/formats.h @@ -191,11 +191,6 @@ av_warn_unused_result int ff_channel_layouts_ref(AVFilterChannelLayouts *f, AVFilterChannelLayouts **ref); -/** - * Remove a reference to a channel layouts list. - */ -void ff_channel_layouts_unref(AVFilterChannelLayouts **ref); - void ff_channel_layouts_changeref(AVFilterChannelLayouts **oldref, AVFilterChannelLayouts **newref); @@ -261,9 +256,11 @@ av_warn_unused_result int ff_formats_ref(AVFilterFormats *formats, AVFilterFormats **ref); /** - * If *ref is non-NULL, remove *ref as a reference to the format list + * If cfg->formats is non-NULL, remove it as a reference to the format list * it currently points to, deallocates that list if this was the last - * reference, and sets *ref to NULL. + * reference, and sets cfg->formats to NULL. + * + * Also does the same for cfg->sample_rates and cfg->channel_layouts. * * Before After * ________ ________ NULL @@ -275,7 +272,7 @@ int ff_formats_ref(AVFilterFormats *formats, AVFilterFormats **ref); * | |____| | | |____| | |____| | | |____| * |________| |_____________________ |________| |_____________________ */ -void ff_formats_unref(AVFilterFormats **ref); +void ff_filter_formats_config_unref(AVFilterFormatsConfig *cfg); /** * Before After