shithub: opus

Download patch

ref: 3f9857b99e3f6d38685116d3190b537e09cc9c68
parent: c1c40a76c24fd918bc2db9171d1c01794d7ab94e
author: Jean-Marc Valin <[email protected]>
date: Fri Aug 13 17:20:37 EDT 2010

Making the fine energy allocation code less ugly.

No change to behaviour except that for stereo we now only force a minimum
of one energy bit per channel (previously two because of an error).

--- a/libcelt/rate.c
+++ b/libcelt/rate.c
@@ -154,13 +154,13 @@
    }
    for (j=start;j<end;j++)
    {
-      int N0, N, d;
+      int N0, N, den;
       int offset;
       int fine_offset;
       N0 = m->eBands[j+1]-m->eBands[j];
       N=M*N0;
       /* Compensate for the extra DoF in stereo */
-      d=(C*N+ ((C==2 && N>2) ? 1 : 0))<<BITRES;
+      den=(C*N+ ((C==2 && N>2) ? 1 : 0));
 
       if (N0==1)
          fine_offset = 19;
@@ -169,21 +169,25 @@
       else
          fine_offset = 12;
 
-      offset = fine_offset - ((m->logN[j] + logM)>>1);
       /* Offset for the number of fine bits compared to their "fair share" of total/N */
-      offset = bits[j]-offset*N*C;
+      offset = N*C*((m->logN[j] + logM - 2*fine_offset)>>1);
+
       /* Compensate for the prediction gain in stereo */
       if (C==2)
          offset -= 1<<BITRES;
-      if (offset < 0)
-         offset = 0;
-      ebits[j] = (2*offset+d)/(2*d);
-      fine_priority[j] = ebits[j]*d >= offset;
 
+      ebits[j] = (bits[j] + offset + (den<<(BITRES-1))) / (den<<BITRES);
+
+      /* If we rounded down, make it a candidate for final fine energy pass */
+      fine_priority[j] = ebits[j]*(den<<BITRES) >= bits[j]+offset;
+
+      /* For N=1, all bits go to fine energy except for a single sign bit */
       if (N==1)
          ebits[j] = (bits[j]/C >> BITRES)-1;
-      if (ebits[j] < C)
-         ebits[j] = C;
+      /* Make sure the first bit is spent on fine energy */
+      if (ebits[j] < 1)
+         ebits[j] = 1;
+
       /* Make sure not to bust */
       if (C*ebits[j] > (bits[j]>>BITRES))
          ebits[j] = bits[j]/C >> BITRES;
@@ -190,6 +194,9 @@
 
       if (ebits[j]>7)
          ebits[j]=7;
+      if (ebits[j]<0)
+         ebits[j]=0;
+
       /* The bits used for fine allocation can't be used for pulses */
       bits[j] -= C*ebits[j]<<BITRES;
       if (bits[j] < 0)