shithub: aacenc

Download patch

ref: 459dbbc305d6cf91a97203fff54c93e3d6e25487
parent: 66758c3778c468aca198e78c59f928e50e8cea16
author: Krzysztof Nikiel <knik@users.sourceforge.net>
date: Mon Oct 23 06:23:34 EDT 2017

fixed section_data (codebook list) bug and low volume band bug

--- a/libfaac/huff2.c
+++ b/libfaac/huff2.c
@@ -441,10 +441,7 @@
 
 int writebooks(CoderInfo *coder, BitStream *stream, int write)
 {
-    int cnt;
-    int bookcnt;
     int bits = 0;
-    int previous;
     int maxcnt, cntbits;
     int group;
     int bookbits = 4;
@@ -464,80 +461,45 @@
     for (group = 0; group < coder->groups.n; group++)
     {
         int band = group * coder->sfbn;
-        int book = coder->book[band];
+        int maxband = band + coder->sfbn;
 
-        previous = book;
-        bookcnt = 1;
-
-        if (write) {
-            PutBit(stream, book, bookbits);
-        }
-        bits += bookbits;
-
-        for (cnt = band + 1; cnt < (band + coder->sfbn); cnt++)
+        while (band < maxband)
         {
-            book = coder->book[cnt];
+            int book = coder->book[band++];
+            int bookcnt = 1;
+            if (write) {
+                PutBit(stream, book, bookbits);
+            }
+            bits += bookbits;
+
 #ifdef DRM
             /* sect_len is not transmitted in case the codebook for a */
             /* section is 11 or in the range of 16 and 31 */
-            if ((previous == 11) ||
-                ((previous >= 16) && (previous <= 32)))
-            {
-                if (write)
-                    PutBit(stream, book, bookbits);
-                bits += bookbits;
-                previous = book;
-                bookcnt=1;
-            } else
+            if ((previous == 11) || ((previous >= 16) && (previous <= 32)))
+                continue;
 #endif
-            if (book != previous)
-            {
-                if (write) {
-                    PutBit(stream, bookcnt, cntbits);
-                }
-                bits += cntbits;
 
-                if (bookcnt >= maxcnt)
+            if (band < maxband)
+            {
+                while (book == coder->book[band])
                 {
-                    if (write)
-                        PutBit(stream, 0, cntbits);
-                    bits += cntbits;
+                    band++;
+                    bookcnt++;
+                    if (band >= maxband)
+                        break;
                 }
-
-                if (write)
-                    PutBit(stream, book, bookbits);
-                bits += bookbits;
-                previous = book;
-                bookcnt = 1;
-                continue;
             }
-            if (bookcnt >= maxcnt)
+
+            while (bookcnt >= maxcnt)
             {
-                if (write) {
-                    PutBit(stream, bookcnt, cntbits);
-                }
+                if (write)
+                    PutBit(stream, maxcnt, cntbits);
                 bits += cntbits;
-                bookcnt = 1;
+                bookcnt -= maxcnt;
             }
-            else {
-                bookcnt++;
-            }
-        }
-
-#ifdef DRM
-        if (!((previous == 11) || ((previous >= 16) && (previous <= 32))))
-#endif
-        {
             if (write)
                 PutBit(stream, bookcnt, cntbits);
             bits += cntbits;
-
-            if (bookcnt >= maxcnt)
-            {
-                if (write)
-                    PutBit(stream, 0, cntbits);
-                bits += cntbits;
-            }
         }
     }
 
--- a/libfaac/quantize.c
+++ b/libfaac/quantize.c
@@ -41,6 +41,7 @@
 #endif
 
 #define MAGIC_NUMBER  0.4054
+#define NOISEFLOOR 0.4
 
 // band sound masking
 static void bmask(CoderInfo *coderInfo, double *xr0, double *bandqual,
@@ -55,6 +56,7 @@
   int gsize = coderInfo->groups.len[gnum];
   double *xr;
   int win;
+  int enrgcnt = 0;
 
 
   for (sfb = 0; sfb < coderInfo->sfbn; sfb++)
@@ -66,12 +68,23 @@
       for (win = 0; win < gsize; win++)
       {
           for (cnt = start; cnt < end; cnt++)
+          {
               totenrg += xr[cnt] * xr[cnt];
+              enrgcnt++;
+          }
 
           xr += BLOCK_LEN_SHORT;
       }
   }
 
+  if (totenrg < ((NOISEFLOOR * NOISEFLOOR) * (double)enrgcnt))
+  {
+      for (sfb = 0; sfb < coderInfo->sfbn; sfb++)
+          bandqual[sfb] = 0.0;
+
+      return;
+  }
+
   for (sfb = 0; sfb < coderInfo->sfbn; sfb++)
   {
     double avge, maxe;
@@ -155,7 +168,6 @@
     {
       double sfacfix;
       int sfac;
-      double maxx;
       double rmsx;
       int xitab[8 * MAXSHORTBAND];
       int *xi;
@@ -166,7 +178,6 @@
       start = coderInfo->sfb_offset[sb];
       end = coderInfo->sfb_offset[sb+1];
 
-      maxx = 0.0;
       rmsx = 0.0;
       xr = xr0;
       for (win = 0; win < gsize; win++)
@@ -174,8 +185,6 @@
           for (cnt = start; cnt < end; cnt++)
           {
               double e = xr[cnt] * xr[cnt];
-              if (maxx < e)
-                  maxx = e;
               rmsx += e;
           }
           xr += BLOCK_LEN_SHORT;
@@ -182,9 +191,8 @@
       }
       rmsx /= ((end - start) * gsize);
       rmsx = sqrt(rmsx);
-      maxx = sqrt(maxx);
 
-      if (maxx < 10.0)
+      if ((rmsx < NOISEFLOOR) || (!bandqual[sb]))
       {
           coderInfo->book[coderInfo->bandcnt] = ZERO_HCB;
           coderInfo->sf[coderInfo->bandcnt++] = 0;
@@ -193,7 +201,10 @@
 
       //printf("qual:%f/%f\n", bandqual[sb], bandqual[sb]/rmsx);
       sfac = lrint(log(bandqual[sb] / rmsx) * sfstep);
-      sfacfix = exp(sfac / sfstep);
+      if ((SF_OFFSET - sfac) < 10)
+          sfacfix = 0.0;
+      else
+          sfacfix = exp(sfac / sfstep);
 
       xr = xr0 + start;
       end -= start;
@@ -251,7 +262,6 @@
 {
     double bandlvl[MAX_SCFAC_BANDS];
     int cnt;
-    int nonzero = 0;
     double *gxr;
 
     coder->global_gain = 0;
@@ -266,10 +276,6 @@
     coder->cur_cw = 0; /* init codeword counter */
 #endif
 
-    for (cnt = 0; cnt < FRAME_LEN; cnt++)
-        nonzero += (fabs(xr[cnt]) > 1E-20);
-
-    if (nonzero)
     {
         int lastis;
         int lastsf;