From: James Almer <jamrial@gmail.com> To: ffmpeg-devel@ffmpeg.org Subject: [FFmpeg-devel] [PATCH] avfilter/formats: fix format negotiation when multiple channel_layouts are provided Date: Sat, 13 May 2023 11:11:00 -0300 Message-ID: <20230513141100.55077-1-jamrial@gmail.com> (raw) 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 <jamrial@gmail.com> --- 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), -- 2.40.1 _______________________________________________ ffmpeg-devel mailing list ffmpeg-devel@ffmpeg.org https://ffmpeg.org/mailman/listinfo/ffmpeg-devel To unsubscribe, visit link above, or email ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".
next reply other threads:[~2023-05-13 14:11 UTC|newest] Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top 2023-05-13 14:11 James Almer [this message] 2023-05-13 17:11 ` Paul B Mahol
Reply instructions: You may reply publicly to this message via plain-text email using any one of the following methods: * Save the following mbox file, import it into your mail client, and reply-to-all from there: mbox Avoid top-posting and favor interleaved quoting: https://en.wikipedia.org/wiki/Posting_style#Interleaved_style * Reply using the --to, --cc, and --in-reply-to switches of git-send-email(1): git send-email \ --in-reply-to=20230513141100.55077-1-jamrial@gmail.com \ --to=jamrial@gmail.com \ --cc=ffmpeg-devel@ffmpeg.org \ /path/to/YOUR_REPLY https://kernel.org/pub/software/scm/git/docs/git-send-email.html * If your mail client supports setting the In-Reply-To header via mailto: links, try the mailto: link
Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel This inbox may be cloned and mirrored by anyone: git clone --mirror https://master.gitmailbox.com/ffmpegdev/0 ffmpegdev/git/0.git # If you have public-inbox 1.1+ installed, you may # initialize and index your mirror using the following commands: public-inbox-init -V2 ffmpegdev ffmpegdev/ https://master.gitmailbox.com/ffmpegdev \ ffmpegdev@gitmailbox.com public-inbox-index ffmpegdev Example config snippet for mirrors. AGPL code for this site: git clone https://public-inbox.org/public-inbox.git