shithub: aacenc

Download patch

ref: 64fc74460b0b2141c3eb6a9b82d050cec90c38b3
parent: 8b213ec1fb33d48f9496c56be0e96f57d784e0ed
author: Krzysztof Nikiel <knik@users.sourceforge.net>
date: Sat Oct 21 07:37:49 EDT 2017

midside: better energy inequations

--- a/libfaac/midside.c
+++ b/libfaac/midside.c
@@ -26,7 +26,7 @@
 static void midside(CoderInfo *coder, ChannelInfo *channel,
                     double *sl0, double *sr0, int *mscnt,
                     int wstart, int wend,
-                    double mutethr
+                    double thrmid, double thrside
                    )
 {
     int sfb;
@@ -38,9 +38,8 @@
         int l, start, end;
         double sum, diff;
         double enrgs, enrgd, enrgl, enrgr;
-        double maxs, maxd, maxl, maxr;
 
-	if (sfb < 1)
+        if (sfb < 1)
             goto setms;
 
         start = coder->sfb_offset[sfb];
@@ -47,7 +46,6 @@
         end = coder->sfb_offset[sfb + 1];
 
         enrgs = enrgd = enrgl = enrgr = 0.0;
-        maxs = maxd = maxl = maxr = 0.0;
         for (win = wstart; win < wend; win++)
         {
             double *sl = sl0 + win * BLOCK_LEN_SHORT;
@@ -62,20 +60,13 @@
                 diff = 0.5 * (lx - rx);
 
                 enrgs += sum * sum;
-                maxs = max(maxs, fabs(sum));
-
                 enrgd += diff * diff;
-                maxd = max(maxd, fabs(diff));
-
                 enrgl += lx * lx;
                 enrgr += rx * rx;
-
-                maxl = max(maxl, fabs(lx));
-                maxr = max(maxr, fabs(rx));
             }
         }
 
-        if (min(enrgs, enrgd) < mutethr * max(enrgs, enrgd))
+        if ((min(enrgl, enrgr) * thrmid) >= max(enrgs, enrgd))
         {
             ms = 1;
             for (win = wstart; win < wend; win++)
@@ -85,17 +76,15 @@
                 for (l = start; l < end; l++)
                 {
                     sum = sl[l] + sr[l];
-                    diff = sl[l] - sr[l];
-                    if (enrgs < enrgd)
-                        sum = 0.0;
-                    else
-                        diff = 0.0;
+                    diff = 0;
+
                     sl[l] = 0.5 * sum;
                     sr[l] = 0.5 * diff;
                 }
             }
         }
-        if (min(enrgl, enrgr) < mutethr * max(enrgl, enrgr))
+
+        if (min(enrgl, enrgr) <= (thrside * max(enrgl, enrgr)))
         {
             for (win = wstart; win < wend; win++)
             {
@@ -126,23 +115,27 @@
 {
     int chn;
     int usems;
-    double mutethr;
     static const double thr075 = 0.1885; /* ~0.75dB */
-    static const double thrmax = 1.5; /* ~1.76dB */
+    static const double thrmax = 0.5; /* ~1.76dB */
+    double thrmid, thrside;
 
     if (quality > 0.01)
     {
         usems = 1;
-        mutethr = thr075 / quality;
-        if (mutethr > thrmax)
-            mutethr = thrmax;
+        thrmid = thr075 / quality;
+        if (thrmid > thrmax)
+            thrmid = thrmax;
     }
     else
     {
         usems = 0;
-        mutethr = 0.0;
+        thrmid = 0.0;
     }
 
+    thrmid += 1.0;
+    thrside = sqrt(thrmid) - 1.0;
+    thrside *= thrside;
+
     for (chn = 0; chn < maxchan; chn++)
     {
         int rch;
@@ -180,7 +173,7 @@
         {
             int end = start + coder->groups.len[group];
             midside(coder + chn, channel + chn, s[chn], s[rch], &mscnt,
-                    start, end, mutethr);
+                    start, end, thrmid, thrside);
             start = end;
         }
         skip:;