shithub: opus

Download patch

ref: dcacb73c33bd0b9c4be2b88d6449b0f5543c7f7d
parent: 6cbfbc383a22eb6a84f0dddb0b4991cb0ef6b022
author: Jean-Marc Valin <[email protected]>
date: Tue Dec 14 08:39:30 EST 2010

Preventing negative bit allocation

--- a/libcelt/rate.c
+++ b/libcelt/rate.c
@@ -207,8 +207,14 @@
    }
    for (i=0;i<*skip;i++)
    {
-      psum = psum - bits[codedBands-1] + ((C+1)<<BITRES);
-      bits[codedBands-1] = C<<BITRES;
+      if (bits[codedBands-1] >= C<<BITRES)
+      {
+         psum = psum - bits[codedBands-1] + ((C+1)<<BITRES);
+         bits[codedBands-1] = C<<BITRES;
+      } else {
+         psum = psum - bits[codedBands-1];
+         bits[codedBands-1] = 0;
+      }
       codedBands--;
    }
    /* Allocate the remaining bits */
@@ -262,8 +268,10 @@
 
       /* 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;
-
+      {
+         ebits[j] = IMAX(0,(bits[j]/C >> BITRES)-1);
+         fine_priority[j] = (ebits[j]+1)*C<<BITRES >= bits[j];
+      }
       /* Make sure not to bust */
       if (C*ebits[j] > (bits[j]>>BITRES))
          ebits[j] = bits[j]/C >> BITRES;