shithub: opus

Download patch

ref: ccea9ce91757055a7741a202013565839092ce50
parent: 17683ebefd7e806b16645ee4db1aec9cbdcde960
author: Jean-Marc Valin <[email protected]>
date: Mon Feb 18 17:03:18 EST 2008

Two-pass algorithm for filling the remaining bits. Still not perfect, but close
enough.

--- a/libcelt/celt.c
+++ b/libcelt/celt.c
@@ -361,7 +361,7 @@
       }
    }
    
-   if (ec_enc_tell(&st->enc, 0) < nbCompressedBytes*8 - 16)
+   if (ec_enc_tell(&st->enc, 0) < nbCompressedBytes*8 - 7)
       celt_warning_int ("many unused bits: ", nbCompressedBytes*8-ec_enc_tell(&st->enc, 0));
    //printf ("%d\n", ec_enc_tell(&st->enc, 0)-8*nbCompressedBytes);
    /* Finishing the stream with a 0101... pattern so that the decoder can check is everything's right */
--- a/libcelt/rate.c
+++ b/libcelt/rate.c
@@ -221,13 +221,15 @@
    for (j=0;j<len;j++)
       bits[j] = ((1<<BITRES)-lo)*bits1[j] + lo*bits2[j];
    out = vec_bits2pulses(alloc, bands, bits, pulses, len);
-   /* Do some refinement to use up all bits */
+   /* Do some refinement to use up all bits. In the first pass, we can only add pulses to 
+      bands that are under their allocated budget. In the second pass, anything goes */
+   int firstpass = 1;
    while(1)
    {
       int incremented = 0;
       for (j=0;j<len;j++)
       {
-         if (alloc->bits[j][pulses[j]] < bits[j] && pulses[j]<MAX_PULSES-1)
+         if ((!firstpass || alloc->bits[j][pulses[j]] < bits[j]) && pulses[j]<MAX_PULSES-1)
          {
             if (out+alloc->bits[j][pulses[j]+1]-alloc->bits[j][pulses[j]] <= total<<BITRES)
             {
@@ -239,7 +241,12 @@
          }
       }
       if (!incremented)
-         break;
+      {
+         if (firstpass)
+            firstpass = 0;
+         else
+            break;
+      }
    }
    return (out+BITROUND) >> BITRES;
 }