From patchwork Wed Aug 12 20:49:40 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 21614 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 B623C449720 for ; Wed, 12 Aug 2020 23:49:58 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 6C47168B507; Wed, 12 Aug 2020 23:49:58 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wm1-f65.google.com (mail-wm1-f65.google.com [209.85.128.65]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 41F6568AE6F for ; Wed, 12 Aug 2020 23:49:52 +0300 (EEST) Received: by mail-wm1-f65.google.com with SMTP id t14so3170082wmi.3 for ; Wed, 12 Aug 2020 13:49:52 -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=UdmP30nJJAWBkzs+9cCvjMgiXG4fjrY7dHkAHIsDQ0k=; b=YLgzUgG1IVKKJshHWPqO8HwNQIT+K0vcTtyO1qHcZ3DwObqyLnlX1E33YmhdoWTcVP UghaR2UilS/AdcgIyoQZWtst5l54k+d6mMHdPPMXgZijQTXn0OWyiZWmSx5K1AH6tqxv bq6mP3KwadTDFTXCVqUKmAL29y1bfUY9U6kxHHGXDB13L7IiqnCq5trnIPMmCi/XTMDw Hx8x+vO9El5oKDxLabnB/4FIM76pYUwlhnzAPilgucB+BiWZXfv7TKeJq7BcMpcRuRic WQV1ZzudazmqKPSoMsMs33i3NjfAXz6E9A7jeMMaQV7Dgbxwd0JR+XVW8EPWwZrywQdK parA== 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=UdmP30nJJAWBkzs+9cCvjMgiXG4fjrY7dHkAHIsDQ0k=; b=LK8qaXBjv7yMtlpR0J0YATuJVzJJT81kmKBcmzm6OnTWmIsqmxf0Nh6+FWG5Mpy/ns aYzUFJzjY1loyXLY0V7gB3VqgkloGb/Wh5BJVYKgiIl+30nvZYfFkzKYoOj/dgdahHFG +t6b7MlMJMmn6ScsiBQzuxyvuMqhn9t/kSqAmuxuw3WAqqXyktJdB84aKyTZFi5HclOW L5h2auyCOoJxuvwPaxpRDMXeROrjk3cheaJLTl/Ojp0IUe4n6Xu/V3aVCXkOOBH8BsFb 4BempJwRjAjEHXueB19rYG/I7Wa4W6TtHDrQcpq2YE+KqUYaHVNWUK/Rw1HUib+oKeds dLzA== X-Gm-Message-State: AOAM531ZdKenoXCSmzkvpv/yZHdZXOvmMA/0XTAxG2YBpDx8WV7sxoNK 7cPjxfFO5NMIIdXM5I/HKf9ZgEk8 X-Google-Smtp-Source: ABdhPJwsvQ0WsdbILoH2SUsY24K/ywBMuaKapsrUnJtaiBfejwQdwfKR+zyweE10E09DRcHH0JExnw== X-Received: by 2002:a05:600c:224e:: with SMTP id a14mr1344832wmm.80.1597265391300; Wed, 12 Aug 2020 13:49:51 -0700 (PDT) Received: from sblaptop.fritz.box (ipbcc10296.dynamic.kabel-deutschland.de. [188.193.2.150]) by smtp.gmail.com with ESMTPSA id s19sm7131145wrb.54.2020.08.12.13.49.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 12 Aug 2020 13:49:50 -0700 (PDT) From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Wed, 12 Aug 2020 22:49:40 +0200 Message-Id: <20200812204940.32100-1-andreas.rheinhardt@gmail.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20200808140202.586-1-andreas.rheinhardt@gmail.com> References: <20200808140202.586-1-andreas.rheinhardt@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 22/22] avfilter/formats: Avoid allocations when merging channel layouts 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" When one merges two AVFilterChannelLayouts structs, there is no need to allocate a new one. Instead one can reuse one of the two given ones. If one does this, one also doesn't need to update the references of the AVFilterChannelLayouts that is reused. Therefore this commit reuses the structure with the higher refcount. Signed-off-by: Andreas Rheinhardt --- This can of course be applied independently of 7-21. libavfilter/formats.c | 48 +++++++++++++++++++------------------------ 1 file changed, 21 insertions(+), 27 deletions(-) diff --git a/libavfilter/formats.c b/libavfilter/formats.c index 00d050e439..2d33dd7afe 100644 --- a/libavfilter/formats.c +++ b/libavfilter/formats.c @@ -48,13 +48,13 @@ do { \ av_freep(&a); \ } while (0) -#define MERGE_REF(ret, a, fmts, type, fail) \ +#define MERGE_REF(ret, a, fmts, type, fail_statement) \ do { \ type ***tmp; \ \ if (!(tmp = av_realloc_array(ret->refs, ret->refcount + a->refcount, \ sizeof(*tmp)))) \ - goto fail; \ + { fail_statement } \ ret->refs = tmp; \ MERGE_REF_NO_ALLOC(ret, a, fmts); \ } while (0) @@ -157,10 +157,10 @@ AVFilterFormats *ff_merge_samplerates(AVFilterFormats *a, if (a->nb_formats && b->nb_formats) { MERGE_FORMATS(ret, a, b, formats, nb_formats, AVFilterFormats, fail); } else if (a->nb_formats) { - MERGE_REF(a, b, formats, AVFilterFormats, fail); + MERGE_REF(a, b, formats, AVFilterFormats, return NULL;); ret = a; } else { - MERGE_REF(b, a, formats, AVFilterFormats, fail); + MERGE_REF(b, a, formats, AVFilterFormats, return NULL;); ret = b; } @@ -177,7 +177,7 @@ fail: AVFilterChannelLayouts *ff_merge_channel_layouts(AVFilterChannelLayouts *a, AVFilterChannelLayouts *b) { - AVFilterChannelLayouts *ret = NULL; + uint64_t *channel_layouts; unsigned a_all = a->all_layouts + a->all_counts; unsigned b_all = b->all_layouts + b->all_counts; int ret_max, ret_nb = 0, i, j, round; @@ -201,15 +201,13 @@ AVFilterChannelLayouts *ff_merge_channel_layouts(AVFilterChannelLayouts *a, return NULL; b->nb_channel_layouts = j; } - MERGE_REF(b, a, channel_layouts, AVFilterChannelLayouts, fail); + MERGE_REF(b, a, channel_layouts, AVFilterChannelLayouts, return NULL;); return b; } ret_max = a->nb_channel_layouts + b->nb_channel_layouts; - if (!(ret = av_mallocz(sizeof(*ret))) || - !(ret->channel_layouts = av_malloc_array(ret_max, - sizeof(*ret->channel_layouts)))) - goto fail; + if (!(channel_layouts = av_malloc_array(ret_max, sizeof(channel_layouts)))) + return NULL; /* a[known] intersect b[known] */ for (i = 0; i < a->nb_channel_layouts; i++) { @@ -217,7 +215,7 @@ AVFilterChannelLayouts *ff_merge_channel_layouts(AVFilterChannelLayouts *a, continue; for (j = 0; j < b->nb_channel_layouts; j++) { if (a->channel_layouts[i] == b->channel_layouts[j]) { - ret->channel_layouts[ret_nb++] = a->channel_layouts[i]; + channel_layouts[ret_nb++] = a->channel_layouts[i]; a->channel_layouts[i] = b->channel_layouts[j] = 0; } } @@ -232,7 +230,7 @@ AVFilterChannelLayouts *ff_merge_channel_layouts(AVFilterChannelLayouts *a, bfmt = FF_COUNT2LAYOUT(av_get_channel_layout_nb_channels(fmt)); for (j = 0; j < b->nb_channel_layouts; j++) if (b->channel_layouts[j] == bfmt) - ret->channel_layouts[ret_nb++] = a->channel_layouts[i]; + channel_layouts[ret_nb++] = a->channel_layouts[i]; } /* 1st round: swap to prepare 2nd round; 2nd round: put it back */ FFSWAP(AVFilterChannelLayouts *, a, b); @@ -243,27 +241,23 @@ AVFilterChannelLayouts *ff_merge_channel_layouts(AVFilterChannelLayouts *a, continue; for (j = 0; j < b->nb_channel_layouts; j++) if (a->channel_layouts[i] == b->channel_layouts[j]) - ret->channel_layouts[ret_nb++] = a->channel_layouts[i]; + channel_layouts[ret_nb++] = a->channel_layouts[i]; } - ret->nb_channel_layouts = ret_nb; - if (!ret->nb_channel_layouts) + if (!ret_nb) goto fail; - ret->refs = av_realloc_array(NULL, a->refcount + b->refcount, - sizeof(*ret->refs)); - if (!ret->refs) - goto fail; - MERGE_REF_NO_ALLOC(ret, a, channel_layouts); - MERGE_REF_NO_ALLOC(ret, b, channel_layouts); - return ret; + if (a->refcount > b->refcount) + FFSWAP(AVFilterChannelLayouts *, a, b); + + MERGE_REF(b, a, channel_layouts, AVFilterChannelLayouts, goto fail;); + av_freep(&b->channel_layouts); + b->channel_layouts = channel_layouts; + b->nb_channel_layouts = ret_nb; + return b; fail: - if (ret) { - av_assert1(!ret->refs); - av_freep(&ret->channel_layouts); - av_freep(&ret); - } + av_free(channel_layouts); return NULL; }