Two-pass algorithm for filling the remaining bits. Still not perfect, but close
authorJean-Marc Valin <jean-marc.valin@usherbrooke.ca>
Mon, 18 Feb 2008 11:03:18 +0000 (22:03 +1100)
committerJean-Marc Valin <jean-marc.valin@usherbrooke.ca>
Mon, 18 Feb 2008 11:03:18 +0000 (22:03 +1100)
enough.

libcelt/celt.c
libcelt/rate.c

index 5582140..63d2af1 100644 (file)
@@ -361,7 +361,7 @@ int celt_encode(CELTEncoder *st, celt_int16_t *pcm, unsigned char *compressed, i
       }
    }
    
-   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 */
index a358784..b4576bd 100644 (file)
@@ -221,13 +221,15 @@ int interp_bits2pulses(const struct alloc_data *alloc, int *bits1, int *bits2, i
    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 @@ int interp_bits2pulses(const struct alloc_data *alloc, int *bits1, int *bits2, i
          }
       }
       if (!incremented)
-         break;
+      {
+         if (firstpass)
+            firstpass = 0;
+         else
+            break;
+      }
    }
    return (out+BITROUND) >> BITRES;
 }