From patchwork Wed Aug 19 21:41:56 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 21745 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 5A2A444A782 for ; Thu, 20 Aug 2020 00:42:40 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 3081F68AA89; Thu, 20 Aug 2020 00:42:40 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-ej1-f50.google.com (mail-ej1-f50.google.com [209.85.218.50]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id DFF75689C7E for ; Thu, 20 Aug 2020 00:42:32 +0300 (EEST) Received: by mail-ej1-f50.google.com with SMTP id qc22so203835ejb.4 for ; Wed, 19 Aug 2020 14:42:32 -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=O2QdStgF8xAowm1fMRHRlacdBOhOHEoi4Xx/Ak4749I=; b=igM0pISoI4URBUCsDFQTr4s/iN2YEohfyQm0cyVtFDEWIjL1qujfi9zPzTT9r48fal 3kC1uqhXqhjEah9Vm1SDduT0mLkGH+VMGvOocMWWdaX7dP31MwDWK4098AGDNnx3FjCD 6e30OZ4Z43iyw999S6h/A+XUbMk1XQi81XY6DxRzbkvC1Xt8whJJKKiYNnifmBinYDcd WULulGiERh2HihKzAGD/+uc0/Md/11reCf8gKnDWmv3hK9XB5bIzG/8iiEvkc/k6frar ymSJrF8Vo3oyPAniecvr1XuAfF0p00XBoiYJ2paRO+t6fuetw0bcHIgQJbIOoRqCdRMx TTzg== 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=O2QdStgF8xAowm1fMRHRlacdBOhOHEoi4Xx/Ak4749I=; b=N4+8CZJWHuW0Twrsl9sbL+ZrPfTBX901UhyrWvHycxa1Lno0DRclHEne8SnFRaG8ya xi8V6DmwT4MpyKp1TIHY48Iuh/0gS/PD67gQBbGGoWBF96j70ZQhqxb7mN//D4gcWMka fjXFV2AmLXwL0LDYIgXkLcn1AKNFezMOV5GH4hDONxGNdwjLtTHrMiw/FAw6axyZn2gr UGGG9gV80FplO0nSnMlyRzw++LkaYftF649ze4mNYkfJSQ5g2jDlMsuPvdJ1VYrRX8z7 vLTd6C7aqWdYRZ+kuK1dw8o/l5U8O/kgiQkjsrajCV7Copbl/ZSl6l2rqa/FDVLZqhgT 2Cmw== X-Gm-Message-State: AOAM533jr5W6YNrmMxVXF80uedMLwH3zjaM26L8ZHwiTD/lO+fQ3wRp0 vmhKoMygsKJpjN/aXRMnbgBtomfDTvE= X-Google-Smtp-Source: ABdhPJwviOCvLGJUGK++vajpi9BotH4/bzrEuDN12T4zuT/u5Bizf/t5tXABLHkSs6CkYGM1Va+9uQ== X-Received: by 2002:a17:906:9382:: with SMTP id l2mr279774ejx.513.1597873351899; Wed, 19 Aug 2020 14:42:31 -0700 (PDT) Received: from sblaptop.fritz.box (ipbcc1fb0f.dynamic.kabel-deutschland.de. [188.193.251.15]) by smtp.gmail.com with ESMTPSA id w18sm68580ejf.37.2020.08.19.14.42.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 19 Aug 2020 14:42:31 -0700 (PDT) From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Wed, 19 Aug 2020 23:41:56 +0200 Message-Id: <20200819214155.13206-1-andreas.rheinhardt@gmail.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20200819171611.hor2bjbzvfhrihbx@phare.normalesup.org> References: <20200819171611.hor2bjbzvfhrihbx@phare.normalesup.org> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH v2] avfilter/formats: Avoid code duplication when merging samplerates 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" Right now, ff_merge_samplerates() contains three instances of the MERGE_REF() macro, a macro which reallocates an array, updates some pointers in a loop and frees several buffers. This commit makes it possible to contain only one instance of said macro in the function, thereby reducing codesize. Signed-off-by: Andreas Rheinhardt --- You're right. A macro should not contain gotos of its own. This of course implied trivial changes for the cosmetics patch. I don't think I need to resend it. Will apply this tomorrow if no one objects. libavfilter/formats.c | 31 +++++++++++++++++-------------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/libavfilter/formats.c b/libavfilter/formats.c index 28433dcd52..2a40762897 100644 --- a/libavfilter/formats.c +++ b/libavfilter/formats.c @@ -56,11 +56,21 @@ do { \ /** * Add all formats common to a and b to a, add b's refs to a and destroy b. + * If empty_allowed is set and one of a,b->nb is zero, the lists are + * merged; otherwise, it is treated as error. */ -#define MERGE_FORMATS(a, b, fmts, nb, type, fail_statement) \ +#define MERGE_FORMATS(a, b, fmts, nb, type, fail_statement, empty_allowed) \ do { \ - int i, j, k = 0; \ + int i, j, k = 0, skip = 0; \ \ + if (empty_allowed) { \ + if (!a->nb || !b->nb) { \ + if (!a->nb) \ + FFSWAP(type *, a, b); \ + skip = 1; \ + } \ + } \ + if (!skip) { \ for (i = 0; i < a->nb; i++) \ for (j = 0; j < b->nb; j++) \ if (a->fmts[i] == b->fmts[j]) { \ @@ -72,7 +82,8 @@ do { if (!k) \ { fail_statement } \ a->nb = k; \ - \ + } \ + \ MERGE_REF(a, b, fmts, type, fail_statement); \ } while (0) @@ -110,7 +121,7 @@ AVFilterFormats *ff_merge_formats(AVFilterFormats *a, AVFilterFormats *b, if (alpha2 > alpha1 || chroma2 > chroma1) return NULL; - MERGE_FORMATS(a, b, formats, nb_formats, AVFilterFormats, return NULL;); + MERGE_FORMATS(a, b, formats, nb_formats, AVFilterFormats, return NULL;, 0); return a; } @@ -120,16 +131,8 @@ AVFilterFormats *ff_merge_samplerates(AVFilterFormats *a, { if (a == b) return a; - if (a->nb_formats && b->nb_formats) { - MERGE_FORMATS(a, b, formats, nb_formats, AVFilterFormats, return NULL;); - return a; - } else if (a->nb_formats) { - MERGE_REF(a, b, formats, AVFilterFormats, return NULL;); - return a; - } else { - MERGE_REF(b, a, formats, AVFilterFormats, return NULL;); - return b; - } + MERGE_FORMATS(a, b, formats, nb_formats, AVFilterFormats, return NULL;, 1); + return a; } AVFilterChannelLayouts *ff_merge_channel_layouts(AVFilterChannelLayouts *a,