Better ebits rounding and making it possible to add remaining ebits even to
authorJean-Marc Valin <jean-marc.valin@usherbrooke.ca>
Thu, 11 Jun 2009 02:46:27 +0000 (22:46 -0400)
committerJean-Marc Valin <jean-marc.valin@usherbrooke.ca>
Thu, 11 Jun 2009 02:46:27 +0000 (22:46 -0400)
bands that weren't rounded down.

libcelt/quant_bands.c
libcelt/rate.c

index 4fe6a1d..323bf4a 100644 (file)
@@ -174,23 +174,26 @@ static void quant_fine_energy_mono(const CELTMode *m, celt_ener_t *eBands, celt_
 
 static void quant_energy_finalise_mono(const CELTMode *m, celt_ener_t *eBands, celt_word16_t *oldEBands, celt_word16_t *error, int *fine_quant, int *fine_priority, int bits_left, ec_enc *enc)
 {
-   int i;
+   int i, prio;
    /* Use up the remaining bits */
-   for (i=0;i<m->nbEBands && bits_left!=0 ;i++)
+   for (prio=0;prio<2;prio++)
    {
-      int q2;
-      celt_word16_t offset;
-      if (fine_quant[i] >= 7 || fine_priority[i]==0)
-         continue;
-      q2 = error[i]<0 ? 0 : 1;
-      ec_enc_bits(enc, q2, 1);
+      for (i=0;i<m->nbEBands && bits_left!=0 ;i++)
+      {
+         int q2;
+         celt_word16_t offset;
+         if (fine_quant[i] >= 7 || fine_priority[i]!=prio)
+            continue;
+         q2 = error[i]<0 ? 0 : 1;
+         ec_enc_bits(enc, q2, 1);
 #ifdef FIXED_POINT
-      offset = SHR16(SHL16(q2,8)-QCONST16(.5,8),fine_quant[i]+1);
+         offset = SHR16(SHL16(q2,8)-QCONST16(.5,8),fine_quant[i]+1);
 #else
-      offset = (q2-.5f)*(1<<(14-fine_quant[i]-1))*(1.f/16384);
+         offset = (q2-.5f)*(1<<(14-fine_quant[i]-1))*(1.f/16384);
 #endif
-      oldEBands[i] += offset;
-      bits_left--;
+         oldEBands[i] += offset;
+         bits_left--;
+      }
    }
    for (i=0;i<m->nbEBands;i++)
    {
@@ -260,22 +263,25 @@ static void unquant_fine_energy_mono(const CELTMode *m, celt_ener_t *eBands, cel
 
 static void unquant_energy_finalise_mono(const CELTMode *m, celt_ener_t *eBands, celt_word16_t *oldEBands, int *fine_quant,  int *fine_priority, int bits_left, ec_dec *dec)
 {
-   int i;
+   int i, prio;
    /* Use up the remaining bits */
-   for (i=0;i<m->nbEBands && bits_left!=0 ;i++)
+   for (prio=0;prio<2;prio++)
    {
-      int q2;
-      celt_word16_t offset;
-      if (fine_quant[i] >= 7 || fine_priority[i]==0)
-         continue;
-      q2 = ec_dec_bits(dec, 1);
+      for (i=0;i<m->nbEBands && bits_left!=0 ;i++)
+      {
+         int q2;
+         celt_word16_t offset;
+         if (fine_quant[i] >= 7 || fine_priority[i]!=prio)
+            continue;
+         q2 = ec_dec_bits(dec, 1);
 #ifdef FIXED_POINT
-      offset = SHR16(SHL16(q2,8)-QCONST16(.5,8),fine_quant[i]+1);
+         offset = SHR16(SHL16(q2,8)-QCONST16(.5,8),fine_quant[i]+1);
 #else
-      offset = (q2-.5f)*(1<<(14-fine_quant[i]-1))*(1.f/16384);
+         offset = (q2-.5f)*(1<<(14-fine_quant[i]-1))*(1.f/16384);
 #endif
-      oldEBands[i] += offset;
-      bits_left--;
+         oldEBands[i] += offset;
+         bits_left--;
+      }
    }
    for (i=0;i<m->nbEBands;i++)
    {
index 061f3e7..9248396 100644 (file)
@@ -145,8 +145,10 @@ static void interp_bits2pulses(const CELTMode *m, int *bits1, int *bits2, int to
       offset = 50 - log2_frac(N, 4);
       /* Offset for the number of fine bits compared to their "fair share" of total/N */
       offset = bits[j]-offset*N*C;
-      ebits[j] = (offset+(d>>1))/d;
-      fine_priority[j] = (ebits[j]*d < offset);
+      if (offset < 0)
+         offset = 0;
+      ebits[j] = (2*offset+d)/(2*d);
+      fine_priority[j] = ebits[j]*d >= offset;
 
       /* Make sure not to bust */
       if (C*ebits[j] > (bits[j]>>BITRES))