Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
 help / color / mirror / Atom feed
* [FFmpeg-devel] [PATCH 1/2] swresample/rematrix: split filling the matrix array into its own function
@ 2025-02-02 21:46 James Almer
  2025-02-02 21:46 ` [FFmpeg-devel] [PATCH 2/2] swresample/rematrix: add support for custom order channel layouts James Almer
  2025-02-06 16:15 ` [FFmpeg-devel] [PATCH 1/2] swresample/rematrix: split filling the matrix array into its own function Michael Niedermayer
  0 siblings, 2 replies; 6+ messages in thread
From: James Almer @ 2025-02-02 21:46 UTC (permalink / raw)
  To: ffmpeg-devel

Signed-off-by: James Almer <jamrial@gmail.com>
---
 libswresample/rematrix.c | 202 ++++++++++++++++++++-------------------
 1 file changed, 106 insertions(+), 96 deletions(-)

diff --git a/libswresample/rematrix.c b/libswresample/rematrix.c
index b9bf4dcac0..8b6e8ae1c5 100644
--- a/libswresample/rematrix.c
+++ b/libswresample/rematrix.c
@@ -124,85 +124,28 @@ static int sane_layout(AVChannelLayout *ch_layout) {
     return 1;
 }
 
-av_cold int swr_build_matrix2(const AVChannelLayout *in_layout, const AVChannelLayout *out_layout,
-                              double center_mix_level, double surround_mix_level,
-                              double lfe_mix_level, double maxval,
-                              double rematrix_volume, double *matrix_param,
-                              ptrdiff_t stride, enum AVMatrixEncoding matrix_encoding, void *log_context)
+static void build_matrix(const AVChannelLayout *in_ch_layout, const AVChannelLayout *out_ch_layout,
+                         double center_mix_level, double surround_mix_level,
+                         double lfe_mix_level, double maxval, double rematrix_volume, double *matrix_param,
+                         ptrdiff_t stride, enum AVMatrixEncoding matrix_encoding)
 {
-    int i, j, out_i, ret;
-    AVChannelLayout in_ch_layout = { 0 }, out_ch_layout = { 0 };
     double matrix[NUM_NAMED_CHANNELS][NUM_NAMED_CHANNELS]={{0}};
-    int64_t unaccounted;
+    uint64_t unaccounted = in_ch_layout->u.mask & ~out_ch_layout->u.mask;
     double maxcoef=0;
-    char buf[128];
-
-    ret  = clean_layout(&in_ch_layout, in_layout, log_context);
-    ret |= clean_layout(&out_ch_layout, out_layout, log_context);
-    if (ret < 0)
-        goto fail;
-
-    if(   !av_channel_layout_compare(&out_ch_layout, &(AVChannelLayout)AV_CHANNEL_LAYOUT_STEREO_DOWNMIX)
-       && !av_channel_layout_subset(&in_ch_layout, AV_CH_LAYOUT_STEREO_DOWNMIX)
-    ) {
-        av_channel_layout_uninit(&out_ch_layout);
-        out_ch_layout = (AVChannelLayout)AV_CHANNEL_LAYOUT_STEREO;
-    }
-    if(   !av_channel_layout_compare(&in_ch_layout, &(AVChannelLayout)AV_CHANNEL_LAYOUT_STEREO_DOWNMIX)
-       && !av_channel_layout_subset(&out_ch_layout, AV_CH_LAYOUT_STEREO_DOWNMIX)
-    ) {
-        av_channel_layout_uninit(&in_ch_layout);
-        in_ch_layout = (AVChannelLayout)AV_CHANNEL_LAYOUT_STEREO;
-    }
-    if (!av_channel_layout_compare(&in_ch_layout, &(AVChannelLayout)AV_CHANNEL_LAYOUT_22POINT2) &&
-        av_channel_layout_compare(&out_ch_layout, &(AVChannelLayout)AV_CHANNEL_LAYOUT_22POINT2)) {
-        av_channel_layout_from_mask(&in_ch_layout, (AV_CH_LAYOUT_7POINT1_WIDE_BACK|AV_CH_BACK_CENTER));
-        av_channel_layout_describe(&in_ch_layout, buf, sizeof(buf));
-        av_log(log_context, AV_LOG_WARNING,
-               "Full-on remixing from 22.2 has not yet been implemented! "
-               "Processing the input as '%s'\n",
-               buf);
-    }
-
-    if(!av_channel_layout_check(&in_ch_layout)) {
-        av_log(log_context, AV_LOG_ERROR, "Input channel layout is invalid\n");
-        ret = AVERROR(EINVAL);
-        goto fail;
-    }
-    if(!sane_layout(&in_ch_layout)) {
-        av_channel_layout_describe(&in_ch_layout, buf, sizeof(buf));
-        av_log(log_context, AV_LOG_ERROR, "Input channel layout '%s' is not supported\n", buf);
-        ret = AVERROR(EINVAL);
-        goto fail;
-    }
-
-    if(!av_channel_layout_check(&out_ch_layout)) {
-        av_log(log_context, AV_LOG_ERROR, "Output channel layout is invalid\n");
-        ret = AVERROR(EINVAL);
-        goto fail;
-    }
-    if(!sane_layout(&out_ch_layout)) {
-        av_channel_layout_describe(&out_ch_layout, buf, sizeof(buf));
-        av_log(log_context, AV_LOG_ERROR, "Output channel layout '%s' is not supported\n", buf);
-        ret = AVERROR(EINVAL);
-        goto fail;
-    }
+    int i, j, out_i;
 
     for(i=0; i<FF_ARRAY_ELEMS(matrix); i++){
-        if(   av_channel_layout_index_from_channel(&in_ch_layout, i) >= 0
-           && av_channel_layout_index_from_channel(&out_ch_layout, i) >= 0)
+        if(   av_channel_layout_index_from_channel(in_ch_layout, i) >= 0
+           && av_channel_layout_index_from_channel(out_ch_layout, i) >= 0)
             matrix[i][i]= 1.0;
     }
 
-    unaccounted = in_ch_layout.u.mask & ~out_ch_layout.u.mask;
-
 //FIXME implement dolby surround
 //FIXME implement full ac3
 
-
     if(unaccounted & AV_CH_FRONT_CENTER){
-        if (av_channel_layout_subset(&out_ch_layout, AV_CH_LAYOUT_STEREO) == AV_CH_LAYOUT_STEREO) {
-            if (av_channel_layout_subset(&in_ch_layout, AV_CH_LAYOUT_STEREO)) {
+        if (av_channel_layout_subset(out_ch_layout, AV_CH_LAYOUT_STEREO) == AV_CH_LAYOUT_STEREO) {
+            if (av_channel_layout_subset(in_ch_layout, AV_CH_LAYOUT_STEREO)) {
                 matrix[ FRONT_LEFT][FRONT_CENTER]+= center_mix_level;
                 matrix[FRONT_RIGHT][FRONT_CENTER]+= center_mix_level;
             } else {
@@ -213,23 +156,23 @@ av_cold int swr_build_matrix2(const AVChannelLayout *in_layout, const AVChannelL
             av_assert0(0);
     }
     if(unaccounted & AV_CH_LAYOUT_STEREO){
-        if (av_channel_layout_index_from_channel(&out_ch_layout, AV_CHAN_FRONT_CENTER) >= 0) {
+        if (av_channel_layout_index_from_channel(out_ch_layout, AV_CHAN_FRONT_CENTER) >= 0) {
             matrix[FRONT_CENTER][ FRONT_LEFT]+= M_SQRT1_2;
             matrix[FRONT_CENTER][FRONT_RIGHT]+= M_SQRT1_2;
-            if (av_channel_layout_index_from_channel(&in_ch_layout, AV_CHAN_FRONT_CENTER) >= 0)
+            if (av_channel_layout_index_from_channel(in_ch_layout, AV_CHAN_FRONT_CENTER) >= 0)
                 matrix[FRONT_CENTER][ FRONT_CENTER] = center_mix_level*sqrt(2);
         }else
             av_assert0(0);
     }
 
     if(unaccounted & AV_CH_BACK_CENTER){
-        if (av_channel_layout_index_from_channel(&out_ch_layout, AV_CHAN_BACK_LEFT) >= 0) {
+        if (av_channel_layout_index_from_channel(out_ch_layout, AV_CHAN_BACK_LEFT) >= 0) {
             matrix[ BACK_LEFT][BACK_CENTER]+= M_SQRT1_2;
             matrix[BACK_RIGHT][BACK_CENTER]+= M_SQRT1_2;
-        } else if (av_channel_layout_index_from_channel(&out_ch_layout, AV_CHAN_SIDE_LEFT) >= 0) {
+        } else if (av_channel_layout_index_from_channel(out_ch_layout, AV_CHAN_SIDE_LEFT) >= 0) {
             matrix[ SIDE_LEFT][BACK_CENTER]+= M_SQRT1_2;
             matrix[SIDE_RIGHT][BACK_CENTER]+= M_SQRT1_2;
-        } else if (av_channel_layout_index_from_channel(&out_ch_layout, AV_CHAN_FRONT_LEFT) >= 0) {
+        } else if (av_channel_layout_index_from_channel(out_ch_layout, AV_CHAN_FRONT_LEFT) >= 0) {
             if (matrix_encoding == AV_MATRIX_ENCODING_DOLBY ||
                 matrix_encoding == AV_MATRIX_ENCODING_DPLII) {
                 if (unaccounted & (AV_CH_BACK_LEFT | AV_CH_SIDE_LEFT)) {
@@ -243,24 +186,24 @@ av_cold int swr_build_matrix2(const AVChannelLayout *in_layout, const AVChannelL
                 matrix[ FRONT_LEFT][BACK_CENTER]+= surround_mix_level * M_SQRT1_2;
                 matrix[FRONT_RIGHT][BACK_CENTER]+= surround_mix_level * M_SQRT1_2;
             }
-        } else if (av_channel_layout_index_from_channel(&out_ch_layout, AV_CHAN_FRONT_CENTER) >= 0) {
+        } else if (av_channel_layout_index_from_channel(out_ch_layout, AV_CHAN_FRONT_CENTER) >= 0) {
             matrix[ FRONT_CENTER][BACK_CENTER]+= surround_mix_level * M_SQRT1_2;
         }else
             av_assert0(0);
     }
     if(unaccounted & AV_CH_BACK_LEFT){
-        if (av_channel_layout_index_from_channel(&out_ch_layout, AV_CHAN_BACK_CENTER) >= 0) {
+        if (av_channel_layout_index_from_channel(out_ch_layout, AV_CHAN_BACK_CENTER) >= 0) {
             matrix[BACK_CENTER][ BACK_LEFT]+= M_SQRT1_2;
             matrix[BACK_CENTER][BACK_RIGHT]+= M_SQRT1_2;
-        } else if (av_channel_layout_index_from_channel(&out_ch_layout, AV_CHAN_SIDE_LEFT) >= 0) {
-            if (av_channel_layout_index_from_channel(&in_ch_layout, AV_CHAN_SIDE_LEFT) >= 0) {
+        } else if (av_channel_layout_index_from_channel(out_ch_layout, AV_CHAN_SIDE_LEFT) >= 0) {
+            if (av_channel_layout_index_from_channel(in_ch_layout, AV_CHAN_SIDE_LEFT) >= 0) {
                 matrix[ SIDE_LEFT][ BACK_LEFT]+= M_SQRT1_2;
                 matrix[SIDE_RIGHT][BACK_RIGHT]+= M_SQRT1_2;
             }else{
             matrix[ SIDE_LEFT][ BACK_LEFT]+= 1.0;
             matrix[SIDE_RIGHT][BACK_RIGHT]+= 1.0;
             }
-        } else if (av_channel_layout_index_from_channel(&out_ch_layout, AV_CHAN_FRONT_LEFT) >= 0) {
+        } else if (av_channel_layout_index_from_channel(out_ch_layout, AV_CHAN_FRONT_LEFT) >= 0) {
             if (matrix_encoding == AV_MATRIX_ENCODING_DOLBY) {
                 matrix[FRONT_LEFT ][BACK_LEFT ] -= surround_mix_level * M_SQRT1_2;
                 matrix[FRONT_LEFT ][BACK_RIGHT] -= surround_mix_level * M_SQRT1_2;
@@ -275,7 +218,7 @@ av_cold int swr_build_matrix2(const AVChannelLayout *in_layout, const AVChannelL
                 matrix[ FRONT_LEFT][ BACK_LEFT] += surround_mix_level;
                 matrix[FRONT_RIGHT][BACK_RIGHT] += surround_mix_level;
             }
-        } else if (av_channel_layout_index_from_channel(&out_ch_layout, AV_CHAN_FRONT_CENTER) >= 0) {
+        } else if (av_channel_layout_index_from_channel(out_ch_layout, AV_CHAN_FRONT_CENTER) >= 0) {
             matrix[ FRONT_CENTER][BACK_LEFT ]+= surround_mix_level*M_SQRT1_2;
             matrix[ FRONT_CENTER][BACK_RIGHT]+= surround_mix_level*M_SQRT1_2;
         }else
@@ -283,20 +226,20 @@ av_cold int swr_build_matrix2(const AVChannelLayout *in_layout, const AVChannelL
     }
 
     if(unaccounted & AV_CH_SIDE_LEFT){
-        if (av_channel_layout_index_from_channel(&out_ch_layout, AV_CHAN_BACK_LEFT) >= 0) {
+        if (av_channel_layout_index_from_channel(out_ch_layout, AV_CHAN_BACK_LEFT) >= 0) {
             /* if back channels do not exist in the input, just copy side
                channels to back channels, otherwise mix side into back */
-            if (av_channel_layout_index_from_channel(&in_ch_layout, AV_CHAN_BACK_LEFT) >= 0) {
+            if (av_channel_layout_index_from_channel(in_ch_layout, AV_CHAN_BACK_LEFT) >= 0) {
                 matrix[BACK_LEFT ][SIDE_LEFT ] += M_SQRT1_2;
                 matrix[BACK_RIGHT][SIDE_RIGHT] += M_SQRT1_2;
             } else {
                 matrix[BACK_LEFT ][SIDE_LEFT ] += 1.0;
                 matrix[BACK_RIGHT][SIDE_RIGHT] += 1.0;
             }
-        } else if (av_channel_layout_index_from_channel(&out_ch_layout, AV_CHAN_BACK_CENTER) >= 0) {
+        } else if (av_channel_layout_index_from_channel(out_ch_layout, AV_CHAN_BACK_CENTER) >= 0) {
             matrix[BACK_CENTER][ SIDE_LEFT]+= M_SQRT1_2;
             matrix[BACK_CENTER][SIDE_RIGHT]+= M_SQRT1_2;
-        } else if (av_channel_layout_index_from_channel(&out_ch_layout, AV_CHAN_FRONT_LEFT) >= 0) {
+        } else if (av_channel_layout_index_from_channel(out_ch_layout, AV_CHAN_FRONT_LEFT) >= 0) {
             if (matrix_encoding == AV_MATRIX_ENCODING_DOLBY) {
                 matrix[FRONT_LEFT ][SIDE_LEFT ] -= surround_mix_level * M_SQRT1_2;
                 matrix[FRONT_LEFT ][SIDE_RIGHT] -= surround_mix_level * M_SQRT1_2;
@@ -311,7 +254,7 @@ av_cold int swr_build_matrix2(const AVChannelLayout *in_layout, const AVChannelL
                 matrix[ FRONT_LEFT][ SIDE_LEFT] += surround_mix_level;
                 matrix[FRONT_RIGHT][SIDE_RIGHT] += surround_mix_level;
             }
-        } else if (av_channel_layout_index_from_channel(&out_ch_layout, AV_CHAN_FRONT_CENTER) >= 0) {
+        } else if (av_channel_layout_index_from_channel(out_ch_layout, AV_CHAN_FRONT_CENTER) >= 0) {
             matrix[ FRONT_CENTER][SIDE_LEFT ]+= surround_mix_level * M_SQRT1_2;
             matrix[ FRONT_CENTER][SIDE_RIGHT]+= surround_mix_level * M_SQRT1_2;
         }else
@@ -319,10 +262,10 @@ av_cold int swr_build_matrix2(const AVChannelLayout *in_layout, const AVChannelL
     }
 
     if(unaccounted & AV_CH_FRONT_LEFT_OF_CENTER){
-        if (av_channel_layout_index_from_channel(&out_ch_layout, AV_CHAN_FRONT_LEFT) >= 0) {
+        if (av_channel_layout_index_from_channel(out_ch_layout, AV_CHAN_FRONT_LEFT) >= 0) {
             matrix[ FRONT_LEFT][ FRONT_LEFT_OF_CENTER]+= 1.0;
             matrix[FRONT_RIGHT][FRONT_RIGHT_OF_CENTER]+= 1.0;
-        } else if (av_channel_layout_index_from_channel(&out_ch_layout, AV_CHAN_FRONT_CENTER) >= 0) {
+        } else if (av_channel_layout_index_from_channel(out_ch_layout, AV_CHAN_FRONT_CENTER) >= 0) {
             matrix[ FRONT_CENTER][ FRONT_LEFT_OF_CENTER]+= M_SQRT1_2;
             matrix[ FRONT_CENTER][FRONT_RIGHT_OF_CENTER]+= M_SQRT1_2;
         }else
@@ -330,20 +273,20 @@ av_cold int swr_build_matrix2(const AVChannelLayout *in_layout, const AVChannelL
     }
 
     if (unaccounted & AV_CH_TOP_FRONT_LEFT) {
-        if (av_channel_layout_index_from_channel(&out_ch_layout, AV_CHAN_TOP_FRONT_CENTER) >= 0) {
+        if (av_channel_layout_index_from_channel(out_ch_layout, AV_CHAN_TOP_FRONT_CENTER) >= 0) {
             matrix[TOP_FRONT_CENTER][TOP_FRONT_LEFT ] += M_SQRT1_2;
             matrix[TOP_FRONT_CENTER][TOP_FRONT_RIGHT] += M_SQRT1_2;
-            if (av_channel_layout_index_from_channel(&in_ch_layout, AV_CHAN_TOP_FRONT_CENTER) >= 0)
+            if (av_channel_layout_index_from_channel(in_ch_layout, AV_CHAN_TOP_FRONT_CENTER) >= 0)
                 matrix[TOP_FRONT_CENTER][TOP_FRONT_CENTER] = center_mix_level * sqrt(2);
-        } else if (av_channel_layout_index_from_channel(&out_ch_layout, AV_CHAN_FRONT_LEFT) >= 0) {
-            if (av_channel_layout_index_from_channel(&in_ch_layout, AV_CHAN_FRONT_LEFT) >= 0) {
+        } else if (av_channel_layout_index_from_channel(out_ch_layout, AV_CHAN_FRONT_LEFT) >= 0) {
+            if (av_channel_layout_index_from_channel(in_ch_layout, AV_CHAN_FRONT_LEFT) >= 0) {
                 matrix[FRONT_LEFT ][TOP_FRONT_LEFT ] += M_SQRT1_2;
                 matrix[FRONT_RIGHT][TOP_FRONT_RIGHT] += M_SQRT1_2;
             } else {
                 matrix[FRONT_LEFT ][TOP_FRONT_LEFT ] += 1.0;
                 matrix[FRONT_RIGHT][TOP_FRONT_RIGHT] += 1.0;
             }
-        } else if (av_channel_layout_index_from_channel(&out_ch_layout, AV_CHAN_FRONT_CENTER) >= 0) {
+        } else if (av_channel_layout_index_from_channel(out_ch_layout, AV_CHAN_FRONT_CENTER) >= 0) {
             matrix[FRONT_CENTER][TOP_FRONT_LEFT ] += M_SQRT1_2;
             matrix[FRONT_CENTER][TOP_FRONT_RIGHT] += M_SQRT1_2;
         } else
@@ -352,29 +295,30 @@ av_cold int swr_build_matrix2(const AVChannelLayout *in_layout, const AVChannelL
 
     /* mix LFE into front left/right or center */
     if (unaccounted & AV_CH_LOW_FREQUENCY) {
-        if (av_channel_layout_index_from_channel(&out_ch_layout, AV_CHAN_FRONT_CENTER) >= 0) {
+        if (av_channel_layout_index_from_channel(out_ch_layout, AV_CHAN_FRONT_CENTER) >= 0) {
             matrix[FRONT_CENTER][LOW_FREQUENCY] += lfe_mix_level;
-        } else if (av_channel_layout_index_from_channel(&out_ch_layout, AV_CHAN_FRONT_LEFT) >= 0) {
+        } else if (av_channel_layout_index_from_channel(out_ch_layout, AV_CHAN_FRONT_LEFT) >= 0) {
             matrix[FRONT_LEFT ][LOW_FREQUENCY] += lfe_mix_level * M_SQRT1_2;
             matrix[FRONT_RIGHT][LOW_FREQUENCY] += lfe_mix_level * M_SQRT1_2;
         } else
             av_assert0(0);
     }
 
+
     for(out_i=i=0; i<64; i++){
         double sum=0;
         int in_i=0;
-        if (av_channel_layout_index_from_channel(&out_ch_layout, i) < 0)
+        if (av_channel_layout_index_from_channel(out_ch_layout, i) < 0)
             continue;
         for(j=0; j<64; j++){
-            if (av_channel_layout_index_from_channel(&in_ch_layout, j) < 0)
+            if (av_channel_layout_index_from_channel(in_ch_layout, j) < 0)
                continue;
             if (i < FF_ARRAY_ELEMS(matrix) && j < FF_ARRAY_ELEMS(matrix[0]))
                 matrix_param[stride*out_i + in_i] = matrix[i][j];
             else
                 matrix_param[stride*out_i + in_i] = i == j &&
-                (   av_channel_layout_index_from_channel(&in_ch_layout, i) >= 0
-                 && av_channel_layout_index_from_channel(&out_ch_layout, i) >= 0);
+                (   av_channel_layout_index_from_channel(in_ch_layout, i) >= 0
+                 && av_channel_layout_index_from_channel(out_ch_layout, i) >= 0);
             sum += fabs(matrix_param[stride*out_i + in_i]);
             in_i++;
         }
@@ -391,6 +335,72 @@ av_cold int swr_build_matrix2(const AVChannelLayout *in_layout, const AVChannelL
                 matrix_param[stride*i + j] /= maxcoef;
             }
     }
+}
+
+av_cold int swr_build_matrix2(const AVChannelLayout *in_layout, const AVChannelLayout *out_layout,
+                              double center_mix_level, double surround_mix_level,
+                              double lfe_mix_level, double maxval,
+                              double rematrix_volume, double *matrix_param,
+                              ptrdiff_t stride, enum AVMatrixEncoding matrix_encoding, void *log_context)
+{
+    int i, j, ret;
+    AVChannelLayout in_ch_layout = { 0 }, out_ch_layout = { 0 };
+    char buf[128];
+
+    ret  = clean_layout(&in_ch_layout, in_layout, log_context);
+    ret |= clean_layout(&out_ch_layout, out_layout, log_context);
+    if (ret < 0)
+        goto fail;
+
+    if(   !av_channel_layout_compare(&out_ch_layout, &(AVChannelLayout)AV_CHANNEL_LAYOUT_STEREO_DOWNMIX)
+       && !av_channel_layout_subset(&in_ch_layout, AV_CH_LAYOUT_STEREO_DOWNMIX)
+    ) {
+        av_channel_layout_uninit(&out_ch_layout);
+        out_ch_layout = (AVChannelLayout)AV_CHANNEL_LAYOUT_STEREO;
+    }
+    if(   !av_channel_layout_compare(&in_ch_layout, &(AVChannelLayout)AV_CHANNEL_LAYOUT_STEREO_DOWNMIX)
+       && !av_channel_layout_subset(&out_ch_layout, AV_CH_LAYOUT_STEREO_DOWNMIX)
+    ) {
+        av_channel_layout_uninit(&in_ch_layout);
+        in_ch_layout = (AVChannelLayout)AV_CHANNEL_LAYOUT_STEREO;
+    }
+    if (!av_channel_layout_compare(&in_ch_layout, &(AVChannelLayout)AV_CHANNEL_LAYOUT_22POINT2) &&
+        av_channel_layout_compare(&out_ch_layout, &(AVChannelLayout)AV_CHANNEL_LAYOUT_22POINT2)) {
+        av_channel_layout_from_mask(&in_ch_layout, (AV_CH_LAYOUT_7POINT1_WIDE_BACK|AV_CH_BACK_CENTER));
+        av_channel_layout_describe(&in_ch_layout, buf, sizeof(buf));
+        av_log(log_context, AV_LOG_WARNING,
+               "Full-on remixing from 22.2 has not yet been implemented! "
+               "Processing the input as '%s'\n",
+               buf);
+    }
+
+    if(!av_channel_layout_check(&in_ch_layout)) {
+        av_log(log_context, AV_LOG_ERROR, "Input channel layout is invalid\n");
+        ret = AVERROR(EINVAL);
+        goto fail;
+    }
+    if(!sane_layout(&in_ch_layout)) {
+        av_channel_layout_describe(&in_ch_layout, buf, sizeof(buf));
+        av_log(log_context, AV_LOG_ERROR, "Input channel layout '%s' is not supported\n", buf);
+        ret = AVERROR(EINVAL);
+        goto fail;
+    }
+
+    if(!av_channel_layout_check(&out_ch_layout)) {
+        av_log(log_context, AV_LOG_ERROR, "Output channel layout is invalid\n");
+        ret = AVERROR(EINVAL);
+        goto fail;
+    }
+    if(!sane_layout(&out_ch_layout)) {
+        av_channel_layout_describe(&out_ch_layout, buf, sizeof(buf));
+        av_log(log_context, AV_LOG_ERROR, "Output channel layout '%s' is not supported\n", buf);
+        ret = AVERROR(EINVAL);
+        goto fail;
+    }
+
+    build_matrix(&in_ch_layout, &out_ch_layout, center_mix_level,
+                 surround_mix_level, lfe_mix_level, maxval, rematrix_volume,
+                 matrix_param, stride, matrix_encoding);
 
     if(rematrix_volume > 0){
         for(i=0; i<SWR_CH_MAX; i++)
-- 
2.48.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".

^ permalink raw reply	[flat|nested] 6+ messages in thread

* [FFmpeg-devel] [PATCH 2/2] swresample/rematrix: add support for custom order channel layouts
  2025-02-02 21:46 [FFmpeg-devel] [PATCH 1/2] swresample/rematrix: split filling the matrix array into its own function James Almer
@ 2025-02-02 21:46 ` James Almer
  2025-02-02 23:03   ` James Almer
  2025-02-06 16:12   ` Michael Niedermayer
  2025-02-06 16:15 ` [FFmpeg-devel] [PATCH 1/2] swresample/rematrix: split filling the matrix array into its own function Michael Niedermayer
  1 sibling, 2 replies; 6+ messages in thread
From: James Almer @ 2025-02-02 21:46 UTC (permalink / raw)
  To: ffmpeg-devel

Limited to the same channels as a native layout, but not constrained by channel ordering.

Signed-off-by: James Almer <jamrial@gmail.com>
---
Untested, as i don't have a sample from which we export a custom layout (Like PCM in mp4).

 libswresample/rematrix.c | 28 +++++++++++++++++-----------
 1 file changed, 17 insertions(+), 11 deletions(-)

diff --git a/libswresample/rematrix.c b/libswresample/rematrix.c
index 8b6e8ae1c5..e3c2cc1211 100644
--- a/libswresample/rematrix.c
+++ b/libswresample/rematrix.c
@@ -104,6 +104,14 @@ static int clean_layout(AVChannelLayout *out, const AVChannelLayout *in, void *s
 }
 
 static int sane_layout(AVChannelLayout *ch_layout) {
+    if(ch_layout->nb_channels >= SWR_CH_MAX)
+        return 0;
+    if(ch_layout->order == AV_CHANNEL_ORDER_CUSTOM)
+        for (int i = 0; i < ch_layout->nb_channels; i++) {
+            if (ch_layout->u.map[i].id >= 64)
+                return 0;
+        }
+    else
     if (ch_layout->order != AV_CHANNEL_ORDER_NATIVE)
         return 0;
     if(!av_channel_layout_subset(ch_layout, AV_CH_LAYOUT_SURROUND)) // at least 1 front speaker
@@ -118,8 +126,6 @@ static int sane_layout(AVChannelLayout *ch_layout) {
         return 0;
     if(!even(av_channel_layout_subset(ch_layout, (AV_CH_TOP_FRONT_LEFT | AV_CH_TOP_FRONT_RIGHT))))
         return 0;
-    if(ch_layout->nb_channels >= SWR_CH_MAX)
-        return 0;
 
     return 1;
 }
@@ -130,9 +136,10 @@ static void build_matrix(const AVChannelLayout *in_ch_layout, const AVChannelLay
                          ptrdiff_t stride, enum AVMatrixEncoding matrix_encoding)
 {
     double matrix[NUM_NAMED_CHANNELS][NUM_NAMED_CHANNELS]={{0}};
-    uint64_t unaccounted = in_ch_layout->u.mask & ~out_ch_layout->u.mask;
+    uint64_t unaccounted =  av_channel_layout_subset(in_ch_layout,  UINT64_MAX) &
+                           ~av_channel_layout_subset(out_ch_layout, UINT64_MAX);
     double maxcoef=0;
-    int i, j, out_i;
+    int i, j;
 
     for(i=0; i<FF_ARRAY_ELEMS(matrix); i++){
         if(   av_channel_layout_index_from_channel(in_ch_layout, i) >= 0
@@ -305,14 +312,15 @@ static void build_matrix(const AVChannelLayout *in_ch_layout, const AVChannelLay
     }
 
 
-    for(out_i=i=0; i<64; i++){
+    for(i=0; i<64; i++){
         double sum=0;
-        int in_i=0;
-        if (av_channel_layout_index_from_channel(out_ch_layout, i) < 0)
+        int out_i = av_channel_layout_index_from_channel(out_ch_layout, i);
+        if (out_i < 0)
             continue;
         for(j=0; j<64; j++){
-            if (av_channel_layout_index_from_channel(in_ch_layout, j) < 0)
-               continue;
+            int in_i = av_channel_layout_index_from_channel(in_ch_layout, j);
+            if (in_i < 0)
+                continue;
             if (i < FF_ARRAY_ELEMS(matrix) && j < FF_ARRAY_ELEMS(matrix[0]))
                 matrix_param[stride*out_i + in_i] = matrix[i][j];
             else
@@ -320,10 +328,8 @@ static void build_matrix(const AVChannelLayout *in_ch_layout, const AVChannelLay
                 (   av_channel_layout_index_from_channel(in_ch_layout, i) >= 0
                  && av_channel_layout_index_from_channel(out_ch_layout, i) >= 0);
             sum += fabs(matrix_param[stride*out_i + in_i]);
-            in_i++;
         }
         maxcoef= FFMAX(maxcoef, sum);
-        out_i++;
     }
     if(rematrix_volume  < 0)
         maxcoef = -rematrix_volume;
-- 
2.48.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".

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [FFmpeg-devel] [PATCH 2/2] swresample/rematrix: add support for custom order channel layouts
  2025-02-02 21:46 ` [FFmpeg-devel] [PATCH 2/2] swresample/rematrix: add support for custom order channel layouts James Almer
@ 2025-02-02 23:03   ` James Almer
  2025-02-02 23:44     ` Pavel Koshevoy
  2025-02-06 16:12   ` Michael Niedermayer
  1 sibling, 1 reply; 6+ messages in thread
From: James Almer @ 2025-02-02 23:03 UTC (permalink / raw)
  To: ffmpeg-devel


[-- Attachment #1.1.1: Type: text/plain, Size: 3798 bytes --]

On 2/2/2025 6:46 PM, James Almer wrote:
> Limited to the same channels as a native layout, but not constrained by channel ordering.
> 
> Signed-off-by: James Almer <jamrial@gmail.com>
> ---
> Untested, as i don't have a sample from which we export a custom layout (Like PCM in mp4).

Tried with one such sample and it appears to work.

> 
>   libswresample/rematrix.c | 28 +++++++++++++++++-----------
>   1 file changed, 17 insertions(+), 11 deletions(-)
> 
> diff --git a/libswresample/rematrix.c b/libswresample/rematrix.c
> index 8b6e8ae1c5..e3c2cc1211 100644
> --- a/libswresample/rematrix.c
> +++ b/libswresample/rematrix.c
> @@ -104,6 +104,14 @@ static int clean_layout(AVChannelLayout *out, const AVChannelLayout *in, void *s
>   }
>   
>   static int sane_layout(AVChannelLayout *ch_layout) {
> +    if(ch_layout->nb_channels >= SWR_CH_MAX)
> +        return 0;
> +    if(ch_layout->order == AV_CHANNEL_ORDER_CUSTOM)
> +        for (int i = 0; i < ch_layout->nb_channels; i++) {
> +            if (ch_layout->u.map[i].id >= 64)
> +                return 0;
> +        }
> +    else
>       if (ch_layout->order != AV_CHANNEL_ORDER_NATIVE)
>           return 0;
>       if(!av_channel_layout_subset(ch_layout, AV_CH_LAYOUT_SURROUND)) // at least 1 front speaker
> @@ -118,8 +126,6 @@ static int sane_layout(AVChannelLayout *ch_layout) {
>           return 0;
>       if(!even(av_channel_layout_subset(ch_layout, (AV_CH_TOP_FRONT_LEFT | AV_CH_TOP_FRONT_RIGHT))))
>           return 0;
> -    if(ch_layout->nb_channels >= SWR_CH_MAX)
> -        return 0;
>   
>       return 1;
>   }
> @@ -130,9 +136,10 @@ static void build_matrix(const AVChannelLayout *in_ch_layout, const AVChannelLay
>                            ptrdiff_t stride, enum AVMatrixEncoding matrix_encoding)
>   {
>       double matrix[NUM_NAMED_CHANNELS][NUM_NAMED_CHANNELS]={{0}};
> -    uint64_t unaccounted = in_ch_layout->u.mask & ~out_ch_layout->u.mask;
> +    uint64_t unaccounted =  av_channel_layout_subset(in_ch_layout,  UINT64_MAX) &
> +                           ~av_channel_layout_subset(out_ch_layout, UINT64_MAX);
>       double maxcoef=0;
> -    int i, j, out_i;
> +    int i, j;
>   
>       for(i=0; i<FF_ARRAY_ELEMS(matrix); i++){
>           if(   av_channel_layout_index_from_channel(in_ch_layout, i) >= 0
> @@ -305,14 +312,15 @@ static void build_matrix(const AVChannelLayout *in_ch_layout, const AVChannelLay
>       }
>   
>   
> -    for(out_i=i=0; i<64; i++){
> +    for(i=0; i<64; i++){
>           double sum=0;
> -        int in_i=0;
> -        if (av_channel_layout_index_from_channel(out_ch_layout, i) < 0)
> +        int out_i = av_channel_layout_index_from_channel(out_ch_layout, i);
> +        if (out_i < 0)
>               continue;
>           for(j=0; j<64; j++){
> -            if (av_channel_layout_index_from_channel(in_ch_layout, j) < 0)
> -               continue;
> +            int in_i = av_channel_layout_index_from_channel(in_ch_layout, j);
> +            if (in_i < 0)
> +                continue;
>               if (i < FF_ARRAY_ELEMS(matrix) && j < FF_ARRAY_ELEMS(matrix[0]))
>                   matrix_param[stride*out_i + in_i] = matrix[i][j];
>               else
> @@ -320,10 +328,8 @@ static void build_matrix(const AVChannelLayout *in_ch_layout, const AVChannelLay
>                   (   av_channel_layout_index_from_channel(in_ch_layout, i) >= 0
>                    && av_channel_layout_index_from_channel(out_ch_layout, i) >= 0);
>               sum += fabs(matrix_param[stride*out_i + in_i]);
> -            in_i++;
>           }
>           maxcoef= FFMAX(maxcoef, sum);
> -        out_i++;
>       }
>       if(rematrix_volume  < 0)
>           maxcoef = -rematrix_volume;


[-- Attachment #1.2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 495 bytes --]

[-- Attachment #2: Type: text/plain, Size: 251 bytes --]

_______________________________________________
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".

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [FFmpeg-devel] [PATCH 2/2] swresample/rematrix: add support for custom order channel layouts
  2025-02-02 23:03   ` James Almer
@ 2025-02-02 23:44     ` Pavel Koshevoy
  0 siblings, 0 replies; 6+ messages in thread
From: Pavel Koshevoy @ 2025-02-02 23:44 UTC (permalink / raw)
  To: FFmpeg development discussions and patches

On Sun, Feb 2, 2025 at 4:04 PM James Almer <jamrial@gmail.com> wrote:

> On 2/2/2025 6:46 PM, James Almer wrote:
> > Limited to the same channels as a native layout, but not constrained by
> channel ordering.
> >
> > Signed-off-by: James Almer <jamrial@gmail.com>
> > ---
> > Untested, as i don't have a sample from which we export a custom layout
> (Like PCM in mp4).
>
> Tried with one such sample and it appears to work.
>
>
I've also applied both patches and tested with the full
COMMUNITY_HERO_2.mov -- it works fine.

Thank you,
    Pavel.
_______________________________________________
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".

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [FFmpeg-devel] [PATCH 2/2] swresample/rematrix: add support for custom order channel layouts
  2025-02-02 21:46 ` [FFmpeg-devel] [PATCH 2/2] swresample/rematrix: add support for custom order channel layouts James Almer
  2025-02-02 23:03   ` James Almer
@ 2025-02-06 16:12   ` Michael Niedermayer
  1 sibling, 0 replies; 6+ messages in thread
From: Michael Niedermayer @ 2025-02-06 16:12 UTC (permalink / raw)
  To: FFmpeg development discussions and patches


[-- Attachment #1.1: Type: text/plain, Size: 801 bytes --]

On Sun, Feb 02, 2025 at 06:46:42PM -0300, James Almer wrote:
> Limited to the same channels as a native layout, but not constrained by channel ordering.
> 
> Signed-off-by: James Almer <jamrial@gmail.com>
> ---
> Untested, as i don't have a sample from which we export a custom layout (Like PCM in mp4).
> 
>  libswresample/rematrix.c | 28 +++++++++++++++++-----------
>  1 file changed, 17 insertions(+), 11 deletions(-)

probably ok

thx

[...]
-- 
Michael     GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB

Old school: Use the lowest level language in which you can solve the problem
            conveniently.
New school: Use the highest level language in which the latest supercomputer
            can solve the problem without the user falling asleep waiting.

[-- Attachment #1.2: signature.asc --]
[-- Type: application/pgp-signature, Size: 195 bytes --]

[-- Attachment #2: Type: text/plain, Size: 251 bytes --]

_______________________________________________
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".

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [FFmpeg-devel] [PATCH 1/2] swresample/rematrix: split filling the matrix array into its own function
  2025-02-02 21:46 [FFmpeg-devel] [PATCH 1/2] swresample/rematrix: split filling the matrix array into its own function James Almer
  2025-02-02 21:46 ` [FFmpeg-devel] [PATCH 2/2] swresample/rematrix: add support for custom order channel layouts James Almer
@ 2025-02-06 16:15 ` Michael Niedermayer
  1 sibling, 0 replies; 6+ messages in thread
From: Michael Niedermayer @ 2025-02-06 16:15 UTC (permalink / raw)
  To: FFmpeg development discussions and patches


[-- Attachment #1.1: Type: text/plain, Size: 1628 bytes --]

On Sun, Feb 02, 2025 at 06:46:41PM -0300, James Almer wrote:
> Signed-off-by: James Almer <jamrial@gmail.com>
> ---
>  libswresample/rematrix.c | 202 ++++++++++++++++++++-------------------
>  1 file changed, 106 insertions(+), 96 deletions(-)
> 
> diff --git a/libswresample/rematrix.c b/libswresample/rematrix.c
> index b9bf4dcac0..8b6e8ae1c5 100644
> --- a/libswresample/rematrix.c
> +++ b/libswresample/rematrix.c
> @@ -124,85 +124,28 @@ static int sane_layout(AVChannelLayout *ch_layout) {
>      return 1;
>  }
>  
> -av_cold int swr_build_matrix2(const AVChannelLayout *in_layout, const AVChannelLayout *out_layout,
> -                              double center_mix_level, double surround_mix_level,
> -                              double lfe_mix_level, double maxval,
> -                              double rematrix_volume, double *matrix_param,
> -                              ptrdiff_t stride, enum AVMatrixEncoding matrix_encoding, void *log_context)
> +static void build_matrix(const AVChannelLayout *in_ch_layout, const AVChannelLayout *out_ch_layout,
> +                         double center_mix_level, double surround_mix_level,
> +                         double lfe_mix_level, double maxval, double rematrix_volume, double *matrix_param,
> +                         ptrdiff_t stride, enum AVMatrixEncoding matrix_encoding)

Please document new functions, even if its just refering
to swr_build_matrix2() docs

thx

[...]
-- 
Michael     GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB

Why not whip the teacher when the pupil misbehaves? -- Diogenes of Sinope

[-- Attachment #1.2: signature.asc --]
[-- Type: application/pgp-signature, Size: 195 bytes --]

[-- Attachment #2: Type: text/plain, Size: 251 bytes --]

_______________________________________________
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".

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2025-02-06 16:16 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2025-02-02 21:46 [FFmpeg-devel] [PATCH 1/2] swresample/rematrix: split filling the matrix array into its own function James Almer
2025-02-02 21:46 ` [FFmpeg-devel] [PATCH 2/2] swresample/rematrix: add support for custom order channel layouts James Almer
2025-02-02 23:03   ` James Almer
2025-02-02 23:44     ` Pavel Koshevoy
2025-02-06 16:12   ` Michael Niedermayer
2025-02-06 16:15 ` [FFmpeg-devel] [PATCH 1/2] swresample/rematrix: split filling the matrix array into its own function Michael Niedermayer

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