From f8695185331460106f34286ebe466cb605f85bb6 Mon Sep 17 00:00:00 2001
From: Sylvain <sylvain.becker@gmail.com>
Date: Tue, 27 Apr 2021 11:07:51 +0200
Subject: [PATCH] Audio: normalize conversion Stereo to 5.1, Quad to 7.1, 5.1
 to 7.1 (bug #4104)

---
 src/audio/SDL_audiocvt.c | 19 ++++++++-----------
 1 file changed, 8 insertions(+), 11 deletions(-)

diff --git a/src/audio/SDL_audiocvt.c b/src/audio/SDL_audiocvt.c
index ca307f84e..e4e858f14 100644
--- a/src/audio/SDL_audiocvt.c
+++ b/src/audio/SDL_audiocvt.c
@@ -246,9 +246,8 @@ SDL_ConvertStereoTo51(SDL_AudioCVT * cvt, SDL_AudioFormat format)
         lf = src[0];
         rf = src[1];
         ce = (lf + rf) * 0.5f;
-        /* !!! FIXME: FL and FR may clip */
-        dst[0] = lf + (lf - ce);  /* FL */
-        dst[1] = rf + (rf - ce);  /* FR */
+        dst[0] = 0.5 * (lf + (lf - ce));  /* FL */
+        dst[1] = 0.5 * (rf + (rf - ce));  /* FR */
         dst[2] = ce;  /* FC */
         dst[3] = 0;   /* LFE (only meant for special LFE effects) */
         dst[4] = lf;  /* BL */
@@ -283,9 +282,8 @@ SDL_ConvertQuadTo51(SDL_AudioCVT * cvt, SDL_AudioFormat format)
         lb = src[2];
         rb = src[3];
         ce = (lf + rf) * 0.5f;
-        /* !!! FIXME: FL and FR may clip */
-        dst[0] = lf + (lf - ce);  /* FL */
-        dst[1] = rf + (rf - ce);  /* FR */
+        dst[0] = 0.5 * (lf + (lf - ce));  /* FL */
+        dst[1] = 0.5 * (rf + (rf - ce));  /* FR */
         dst[2] = ce;  /* FC */
         dst[3] = 0;   /* LFE (only meant for special LFE effects) */
         dst[4] = lb;  /* BL */
@@ -351,7 +349,6 @@ SDL_Convert51To71(SDL_AudioCVT * cvt, SDL_AudioFormat format)
         rb = src[5];
         ls = (lf + lb) * 0.5f;
         rs = (rf + rb) * 0.5f;
-        /* !!! FIXME: these four may clip */
         lf += lf - ls;
         rf += rf - rs;
         lb += lb - ls;
@@ -360,10 +357,10 @@ SDL_Convert51To71(SDL_AudioCVT * cvt, SDL_AudioFormat format)
         dst[2] = src[2];  /* FC */
         dst[7] = rs; /* SR */
         dst[6] = ls; /* SL */
-        dst[5] = rb;  /* BR */
-        dst[4] = lb;  /* BL */
-        dst[1] = rf;  /* FR */
-        dst[0] = lf;  /* FL */
+        dst[5] = 0.5 * rb;  /* BR */
+        dst[4] = 0.5 * lb;  /* BL */
+        dst[1] = 0.5 * rf;  /* FR */
+        dst[0] = 0.5 * lf;  /* FL */
     }
 
     cvt->len_cvt = cvt->len_cvt * 4 / 3;