shithub: opus

Download patch

ref: a76a5e8f0c8797e902924a8f186800707038aa2d
parent: eb5aa094c479bc26f3ff88c8e0f262b977a0652e
author: Jean-Marc Valin <[email protected]>
date: Wed Jun 10 18:46:27 EDT 2009

Better ebits rounding and making it possible to add remaining ebits even to
bands that weren't rounded down.

--- a/libcelt/quant_bands.c
+++ b/libcelt/quant_bands.c
@@ -174,23 +174,26 @@
 
 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_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++)
    {
--- a/libcelt/rate.c
+++ b/libcelt/rate.c
@@ -145,8 +145,10 @@
       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))