From patchwork Sat May 13 14:11:00 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: James Almer X-Patchwork-Id: 41601 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:ba91:b0:105:feb:71f2 with SMTP id fb17csp401951pzb; Sat, 13 May 2023 07:11:29 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ71wH2Lraw+bJAsAks+dV/Cy3seczKDvUI9kt4n0c3/2eLg+Dww9ypZCdjXQVxWHuzeDoXf X-Received: by 2002:aa7:cacc:0:b0:50d:fba2:7265 with SMTP id l12-20020aa7cacc000000b0050dfba27265mr6766659edt.16.1683987089357; Sat, 13 May 2023 07:11:29 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1683987089; cv=none; d=google.com; s=arc-20160816; b=B4DpKmVrO4l7lZ5b5Lx+PINHLzosYPt8qb3oFc7n0knPrEfqMSPV6esE9gYWDN+5no 8vjHntJY02GldRhlDNkqriq74UwEitF5VDYZ12ktmpH3B4Ju4SftRfYAo77BTOKsM0Gq HWYojZZ7+EoNUGzWVvRBdwWPUTAzpaaenRdJxLuUes74BYm351HOlHyM4f/LIa+hmo/l QkMJpROy9fxH75sPVv+0f6mUGCpd7Eqxs7ptDNLwlPkVNcay1ysKOXq8I5ydvWaohTFF F3MIbKNJKb6LHYyICJVhgJ0fqgzoKY/BUy4ivcDYXRYhn3LzBJCBoviQFeIV7K9hj7lW lHQQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:reply-to:list-subscribe :list-help:list-post:list-archive:list-unsubscribe:list-id :precedence:subject:mime-version:message-id:date:to:from :dkim-signature:delivered-to; bh=FmyMRFRV3WRXl5BBGyOzeViRDV4w0LHFtfs68CiHwd8=; b=yb4hBsviZ57WZDeZw6+DPpIplM/o2Lq6GXAUj8vdxzFN5Xk9NjhfTqBJgi8YX+r/hG 2lnIuEbA9CyV35DT7gZ8tQpJQfFRN57jXV+oudToCfWUpYHGlyuymYHt7NyFYJOLMiW0 0RzPeirT+yQVAfbMW2sZjWWlmMIWgcqlvnCEUw7RuDpA/9qQp9kraKYT2Fhtz9G0gRcF l+f+hNdgSr+AN7KJyOS+PuSHH15nXMHVf2dOja47yoQMOcOPF1lu/C4rpd9y33IrGkM2 NpL1Bft36gd3f08x5/nNbA/HBSUrALEruTNs/ywCFTtPIlrO94vub/2jRsvx2VdJrXEN OLbw== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20221208 header.b=Fmzblcz4; 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; dmarc=fail (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id k20-20020a05640212d400b005068d6ae607si5990364edx.574.2023.05.13.07.11.28; Sat, 13 May 2023 07:11:29 -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; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20221208 header.b=Fmzblcz4; 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; dmarc=fail (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id A6FE468BF01; Sat, 13 May 2023 17:11:23 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-oi1-f182.google.com (mail-oi1-f182.google.com [209.85.167.182]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id F0B6568B48D for ; Sat, 13 May 2023 17:11:16 +0300 (EEST) Received: by mail-oi1-f182.google.com with SMTP id 5614622812f47-392116ae103so5743201b6e.0 for ; Sat, 13 May 2023 07:11:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1683987074; x=1686579074; h=content-transfer-encoding:mime-version:message-id:date:subject:to :from:from:to:cc:subject:date:message-id:reply-to; bh=D3es2USDHgFG9ddNRg/HPttEzVaHYA7k8FL3HogtNY8=; b=Fmzblcz4wyMSWB76GJhRzlYyZITFNXVHe86El22evjMSoslv1p6y9KDVFTgEYQiDqS xQD8hxx5CdSdTeb/tAP5nKksQH/bKqfmcAJ/J4k7ZZ6UUPcoBWsgKsdDCyHymoyQE3Vb 3ehZj0xm54hPD0yaGM0Sf+4KgtZ/YobLZsRnxAzyP2GFE8Dj25MgYr1X6TUhcGqU48h4 dq5BQQhrZjbZJ4N0q6wNF0lbeATt6aK7rDJNciolG5+TCb5brkgFZZcmtt5aMLLXJ5L6 cYfEdghBeOyVlz/RUeiCogpPkTZXlFkoL5/gHFQ8v1hEJuNmaxAdNMdiqCo+dATqqoZP vEbw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1683987074; x=1686579074; h=content-transfer-encoding:mime-version:message-id:date:subject:to :from:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=D3es2USDHgFG9ddNRg/HPttEzVaHYA7k8FL3HogtNY8=; b=cKCUlL2/6cdkALbJBcUFQwAeJYkieOOwdTw9YT0hAZszZc489PiG540CFB6jrzAYba wXYIytzlFPj12E1EQquJMG05IixsjAePU+AB2eQh47Vwo/yF+EWBsKUaQTvguFPqLczT rcFgrTh2rn19eF+KPSC2sjL9Klh9l7nfit/tTQuBo04tMWKGqzPZnvkvDTR5w9XHcz40 sethdMar4gBauzqJVWU5EmMtlpHzetzk1CvcTSUGJFmnkZQk+f+1es+rP0DMJICgLmui bAmjEZD013qw/cHKPWfvgtOP3oEcsIEVvhcqQr3QA+77Jn3mm6But6ELbnXeatfihYRH x2oA== X-Gm-Message-State: AC+VfDwNTyFPYuR35+++htQjRfJxsVXRjEzTM/ofkReDJahwT2FBaw4e bxpWOKJoylyEuHqCwZMJSmcRx8ajS/Y= X-Received: by 2002:a05:6808:1910:b0:395:e8ee:6906 with SMTP id bf16-20020a056808191000b00395e8ee6906mr569617oib.14.1683987074353; Sat, 13 May 2023 07:11:14 -0700 (PDT) Received: from localhost.localdomain (host197.190-225-105.telecom.net.ar. [190.225.105.197]) by smtp.gmail.com with ESMTPSA id m20-20020aca1e14000000b003896e31867esm5334141oic.49.2023.05.13.07.11.13 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 13 May 2023 07:11:13 -0700 (PDT) From: James Almer To: ffmpeg-devel@ffmpeg.org Date: Sat, 13 May 2023 11:11:00 -0300 Message-Id: <20230513141100.55077-1-jamrial@gmail.com> X-Mailer: git-send-email 2.40.1 MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH] avfilter/formats: fix format negotiation when multiple channel_layouts are provided X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.29 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 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: 26MxNpfUsSep e.g. ffmpeg -f lavfi -i sine -af "aformat=cl=stereo|5.1|7.1,lowpass,aformat=cl=7.1|5.1|stereo" -f null - Signed-off-by: James Almer --- libavfilter/avfiltergraph.c | 2 +- libavfilter/formats.c | 39 ++++++++++++++++++++++++++++--------- 2 files changed, 31 insertions(+), 10 deletions(-) diff --git a/libavfilter/avfiltergraph.c b/libavfilter/avfiltergraph.c index 744f480e1d..a200ab856c 100644 --- a/libavfilter/avfiltergraph.c +++ b/libavfilter/avfiltergraph.c @@ -441,7 +441,7 @@ static int query_formats(AVFilterGraph *graph, void *log_ctx) neg = ff_filter_get_negotiation(link); av_assert0(neg); - for (neg_step = 1; neg_step < neg->nb_mergers; neg_step++) { + for (neg_step = 0; neg_step < neg->nb_mergers; neg_step++) { const AVFilterFormatsMerger *m = &neg->mergers[neg_step]; void *a = FF_FIELD_AT(void *, m->offset, link->incfg); void *b = FF_FIELD_AT(void *, m->offset, link->outcfg); diff --git a/libavfilter/formats.c b/libavfilter/formats.c index e8c2888c0c..d1c97daf64 100644 --- a/libavfilter/formats.c +++ b/libavfilter/formats.c @@ -209,11 +209,10 @@ static int merge_samplerates(void *a, void *b) /** * See merge_pix_fmts(). */ -static int merge_channel_layouts(void *va, void *vb) +static int merge_channel_layouts_internal(AVFilterChannelLayouts *a, + AVFilterChannelLayouts *b, int check) { - AVFilterChannelLayouts *a = va; - AVFilterChannelLayouts *b = vb; - AVChannelLayout *channel_layouts; + AVChannelLayout *channel_layouts = NULL; 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; @@ -231,8 +230,11 @@ static int merge_channel_layouts(void *va, void *vb) if (a_all == 1 && !b_all) { /* keep only known layouts in b; works also for b_all = 1 */ for (i = j = 0; i < b->nb_channel_layouts; i++) - if (KNOWN(&b->channel_layouts[i]) && i != j++) + if (KNOWN(&b->channel_layouts[i]) && i != j++) { + if (check) + return 1; av_channel_layout_copy(&b->channel_layouts[j], &b->channel_layouts[i]); + } /* Not optimal: the unknown layouts of b may become known after another merge. */ if (!j) @@ -244,7 +246,7 @@ static int merge_channel_layouts(void *va, void *vb) } ret_max = a->nb_channel_layouts + b->nb_channel_layouts; - if (!(channel_layouts = av_calloc(ret_max, sizeof(*channel_layouts)))) + if (!check && !(channel_layouts = av_calloc(ret_max, sizeof(*channel_layouts)))) return AVERROR(ENOMEM); /* a[known] intersect b[known] */ @@ -253,6 +255,8 @@ static int merge_channel_layouts(void *va, void *vb) continue; for (j = 0; j < b->nb_channel_layouts; j++) { if (!av_channel_layout_compare(&a->channel_layouts[i], &b->channel_layouts[j])) { + if (check) + return 1; av_channel_layout_copy(&channel_layouts[ret_nb++], &a->channel_layouts[i]); av_channel_layout_uninit(&a->channel_layouts[i]); av_channel_layout_uninit(&b->channel_layouts[j]); @@ -269,8 +273,11 @@ static int merge_channel_layouts(void *va, void *vb) continue; bfmt = FF_COUNT2LAYOUT(fmt->nb_channels); for (j = 0; j < b->nb_channel_layouts; j++) - if (!av_channel_layout_compare(&b->channel_layouts[j], &bfmt)) + if (!av_channel_layout_compare(&b->channel_layouts[j], &bfmt)) { + if (check) + return 1; av_channel_layout_copy(&channel_layouts[ret_nb++], fmt); + } } /* 1st round: swap to prepare 2nd round; 2nd round: put it back */ FFSWAP(AVFilterChannelLayouts *, a, b); @@ -280,8 +287,11 @@ static int merge_channel_layouts(void *va, void *vb) if (KNOWN(&a->channel_layouts[i])) continue; for (j = 0; j < b->nb_channel_layouts; j++) - if (!av_channel_layout_compare(&a->channel_layouts[i], &b->channel_layouts[j])) + if (!av_channel_layout_compare(&a->channel_layouts[i], &b->channel_layouts[j])) { + if (check) + return 1; av_channel_layout_copy(&channel_layouts[ret_nb++], &a->channel_layouts[i]); + } } if (!ret_nb) { @@ -300,6 +310,17 @@ static int merge_channel_layouts(void *va, void *vb) return 1; } +static int can_merge_channel_layouts(const void *a, const void *b) +{ + return merge_channel_layouts_internal((AVFilterChannelLayouts *)a, + (AVFilterChannelLayouts *)b, 1); +} + +static int merge_channel_layouts(void *a, void *b) +{ + return merge_channel_layouts_internal(a, b, 0); +} + static const AVFilterFormatsMerger mergers_video[] = { { .offset = offsetof(AVFilterFormatsConfig, formats), @@ -312,7 +333,7 @@ static const AVFilterFormatsMerger mergers_audio[] = { { .offset = offsetof(AVFilterFormatsConfig, channel_layouts), .merge = merge_channel_layouts, - .can_merge = NULL, + .can_merge = can_merge_channel_layouts, }, { .offset = offsetof(AVFilterFormatsConfig, samplerates),