Better allocation rounding as suggested by derf
authorJean-Marc Valin <jean-marc.valin@usherbrooke.ca>
Tue, 10 Feb 2009 04:17:44 +0000 (23:17 -0500)
committerJean-Marc Valin <jean-marc.valin@usherbrooke.ca>
Tue, 10 Feb 2009 04:17:44 +0000 (23:17 -0500)
libcelt/modes.c
libcelt/rate.c

index c5b8738..6f1937a 100644 (file)
@@ -248,11 +248,11 @@ static void compute_allocation_table(CELTMode *mode, int res)
       for (j=0;j<mode->nbEBands;j++)
          allocVectors[i*mode->nbEBands+j] = (allocVectors[i*mode->nbEBands+j]+128)/256;
    }
-   for (i=0;i<mode->nbAllocVectors;i++)
+   /*for (i=0;i<mode->nbAllocVectors;i++)
    {
       for (j=0;j<mode->nbEBands;j++)
          allocVectors[i*mode->nbEBands+j] += C;
-   }
+   }*/
    mode->energy_alloc = allocEnergy;
    mode->allocVectors = allocVectors;
 }
index 9c83049..38b193d 100644 (file)
@@ -113,13 +113,18 @@ static void interp_bits2pulses(const CELTMode *m, int *bits1, int *bits2, int to
    }
    for (j=0;j<len;j++)
    {
+      int N, d;
       int offset;
       int min_bits=0;
-      if (bits[j] >= C>>BITRES)
+      if (bits[j] >= C<<BITRES)
          min_bits = 1;
+
+      N=m->eBands[j+1]-m->eBands[j]; 
+      d=C*N<<BITRES; 
+      offset = 50 - log2_frac(N, 4);
       /* Offset for the number of fine bits compared to their "fair share" of total/N */
-      offset = 45 - log2_frac(m->eBands[j+1]-m->eBands[j], 4);
-      ebits[j] = IMAX(min_bits , ((bits[j]+C*(m->eBands[j+1]-m->eBands[j])/2) / (C*(m->eBands[j+1]-m->eBands[j])) - offset)>>BITRES  );
+      ebits[j] = IMAX(min_bits, (bits[j]-offset*N*C+(d>>1))/d);
+
       /* Make sure not to bust */
       if (C*ebits[j] > (bits[j]>>BITRES))
          ebits[j] = bits[j]/C >> BITRES;