From d63f0817c7810f3ae7e093f09a0754776f7b4170 Mon Sep 17 00:00:00 2001 From: Brian Matherly Date: Sat, 13 Apr 2024 21:57:22 -0500 Subject: [PATCH] Fix channel layout for swresample filter and link --- src/modules/avformat/common.c | 3 +++ src/modules/avformat/common_swr.c | 26 +++++++++++++++++++++++--- 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/src/modules/avformat/common.c b/src/modules/avformat/common.c index b09d15522..a316f8d18 100644 --- a/src/modules/avformat/common.c +++ b/src/modules/avformat/common.c @@ -159,6 +159,9 @@ int64_t mlt_to_av_channel_layout(mlt_channel_layout layout) #if HAVE_FFMPEG_CH_LAYOUT mlt_channel_layout av_channel_layout_to_mlt(AVChannelLayout *layout) { + if (layout->order != AV_CHANNEL_ORDER_NATIVE && layout->order != AV_CHANNEL_ORDER_AMBISONIC) { + return mlt_channel_independent; + } switch (layout->u.mask) { #else mlt_channel_layout av_channel_layout_to_mlt(int64_t layout) diff --git a/src/modules/avformat/common_swr.c b/src/modules/avformat/common_swr.c index 409f51289..4a5388458 100644 --- a/src/modules/avformat/common_swr.c +++ b/src/modules/avformat/common_swr.c @@ -1,6 +1,6 @@ /* - * common.h - * Copyright (C) 2022-2023 Meltytech, LLC + * common_swr.c + * Copyright (C) 2022-2024 Meltytech, LLC * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public @@ -50,16 +50,27 @@ int mlt_configure_swr_context(mlt_service service, mlt_swr_private_data *pdata) // Configure format, frequency and channels. av_opt_set_int(pdata->ctx, "osf", mlt_to_av_sample_format(pdata->out_format), 0); av_opt_set_int(pdata->ctx, "osr", pdata->out_frequency, 0); - av_opt_set_int(pdata->ctx, "och", pdata->out_channels, 0); av_opt_set_int(pdata->ctx, "isf", mlt_to_av_sample_format(pdata->in_format), 0); av_opt_set_int(pdata->ctx, "isr", pdata->in_frequency, 0); +#if !HAVE_FFMPEG_CH_LAYOUT + av_opt_set_int(pdata->ctx, "och", pdata->out_channels, 0); av_opt_set_int(pdata->ctx, "ich", pdata->in_channels, 0); +#endif if (pdata->in_layout != mlt_channel_independent && pdata->out_layout != mlt_channel_independent) { // Use standard channel layout and matrix for known channel configurations. +#if HAVE_FFMPEG_CH_LAYOUT + AVChannelLayout ochl = AV_CHANNEL_LAYOUT_MASK(pdata->out_channels, + mlt_to_av_channel_layout(pdata->out_layout)); + AVChannelLayout ichl = AV_CHANNEL_LAYOUT_MASK(pdata->in_channels, + mlt_to_av_channel_layout(pdata->in_layout)); + av_opt_set_chlayout(pdata->ctx, "ochl", &ochl, 0); + av_opt_set_chlayout(pdata->ctx, "ichl", &ichl, 0); +#else av_opt_set_int(pdata->ctx, "ocl", mlt_to_av_channel_layout(pdata->out_layout), 0); av_opt_set_int(pdata->ctx, "icl", mlt_to_av_channel_layout(pdata->in_layout), 0); +#endif } else { // Use a custom channel layout and matrix for independent channels. // This matrix will simply map input channels to output channels in order. @@ -81,8 +92,17 @@ int mlt_configure_swr_context(mlt_service service, mlt_swr_private_data *pdata) matrix_row[i] = 1.0; } } +#if HAVE_FFMPEG_CH_LAYOUT + AVChannelLayout ochl + = {AV_CHANNEL_ORDER_UNSPEC, pdata->out_channels, {custom_out_layout}, NULL}; + AVChannelLayout ichl + = {AV_CHANNEL_ORDER_UNSPEC, pdata->in_channels, {custom_in_layout}, NULL}; + av_opt_set_chlayout(pdata->ctx, "ochl", &ochl, 0); + av_opt_set_chlayout(pdata->ctx, "ichl", &ichl, 0); +#else av_opt_set_int(pdata->ctx, "ocl", custom_out_layout, 0); av_opt_set_int(pdata->ctx, "icl", custom_in_layout, 0); +#endif error = swr_set_matrix(pdata->ctx, matrix, stride); av_free(matrix); if (error != 0) {