From b7928799e5eba95e83e1d5f169f3119bf6a93954 Mon Sep 17 00:00:00 2001 From: Wim Taymans Date: Thu, 29 Sep 2022 12:29:36 +0200 Subject: [PATCH] channelmix: fix normalization Don't count the sum for unused matrix coefficients. When doing pairing, we set the mask to ~0 and so we would otherwise count too much. Apply normalization to the active matrix coefficients, not whatever the loop variables end up being at the end of the loops. --- spa/plugins/audioconvert/channelmix-ops.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/spa/plugins/audioconvert/channelmix-ops.c b/spa/plugins/audioconvert/channelmix-ops.c index fa2029885..c7ea09a51 100644 --- a/spa/plugins/audioconvert/channelmix-ops.c +++ b/spa/plugins/audioconvert/channelmix-ops.c @@ -483,6 +483,8 @@ done: for (jc = 0, j = 0; j < SPA_AUDIO_MAX_CHANNELS; j++) { if ((src_mask & (1UL << j)) == 0) continue; + if (ic >= dst_chan || jc >= src_chan) + continue; mix->matrix_orig[ic][jc++] = matrix[i][j]; sum += fabs(matrix[i][j]); } @@ -501,11 +503,10 @@ done: if (SPA_FLAG_IS_SET(mix->options, CHANNELMIX_OPTION_NORMALIZE) && maxsum > 1.0f) { spa_log_debug(mix->log, "normalize %f", maxsum); - for (i = 0; i < ic; i++) - for (j = 0; j < jc; j++) + for (i = 0; i < dst_chan; i++) + for (j = 0; j < src_chan; j++) mix->matrix_orig[i][j] /= maxsum; } - return 0; }