shithub: opus

Download patch

ref: fb031119d39fbb9bb94f5f1d0bb7fac946f71c7a
parent: 9b4ed5e2907d632659193766e02eaf98db9aff4e
author: Jean-Marc Valin <[email protected]>
date: Thu Nov 25 11:32:54 EST 2010

Allocate remaining bits with constant SNR offset

--- a/libcelt/rate.c
+++ b/libcelt/rate.c
@@ -153,7 +153,7 @@
 
    ALLOC(thresh, len, int);
 
-   /* Threshold: don't allow any band to go below 3/8 bit/sample */
+   /* Below this threshold, we don't allocate any PVQ bits */
    for (j=start;j<end;j++)
       thresh[j] = 2*(C*(m->eBands[j+1]-m->eBands[j])<<LM<<BITRES)>>3;
    logM = LM<<BITRES;
@@ -196,12 +196,16 @@
    if (codedBands) {
       int left, perband;
       left = (total<<BITRES)-psum;
-      perband = left/(codedBands-start);
+      perband = left/(m->eBands[codedBands]-m->eBands[start]);
       for (j=start;j<codedBands;j++)
-         bits[j] += perband;
-      left = left-codedBands*perband;
-      for (j=start;j<start+left;j++)
-         bits[j]++;
+         bits[j] += perband*(m->eBands[j+1]-m->eBands[j]);
+      left = left-(m->eBands[codedBands]-m->eBands[start])*perband;
+      for (j=start;j<codedBands;j++)
+      {
+         int tmp = IMIN(left, m->eBands[j+1]-m->eBands[j]);
+         bits[j] += tmp;
+         left -= tmp;
+      }
    }
    /*for (j=0;j<end;j++)printf("%d ", bits[j]);printf("\n");*/
    for (j=start;j<end;j++)