shithub: opus

Download patch

ref: c40addcb043ea33528d4f15e16143c74f3ac4a7d
parent: fd54a99e2d36510c785a011ee1457386213c6054
author: Jean-Marc Valin <[email protected]>
date: Fri Oct 22 10:57:07 EDT 2010

Reworked the allocation trim to be absolute (in bits/sample) rather relative

Also making use of alloc_trim_analysis() again because the effect of
inter-channel correlation on the bitstream is really in terms of absolute
number of bits/samples.

--- a/libcelt/celt.c
+++ b/libcelt/celt.c
@@ -54,7 +54,6 @@
 #include "plc.h"
 
 static const int trim_cdf[7] = {0, 4, 10, 23, 119, 125, 128};
-static const int trim_coef[6] = {4, 6, 7, 8, 10, 12};
 
 /** Encoder state 
  @brief Encoder state
@@ -569,12 +568,12 @@
       }
       sum = MULT16_16_Q15(QCONST16(1.f/8, 15), sum);
       /*printf ("%f\n", sum);*/
-      if (sum > QCONST16(.95,10))
+      if (sum > QCONST16(.995,10))
          trim_index-=3;
-      else if (sum > QCONST16(.75,10))
+      else if (sum > QCONST16(.92,10))
          trim_index-=2;
-      else if (sum > QCONST16(.5,10))
-         trim_index--;
+      else if (sum > QCONST16(.75,10))
+         trim_index-=1;
    }
 #if 0
    float diff=0;
@@ -788,13 +787,8 @@
       }
       offsets[i] *= (6<<BITRES);
    }
-   {
-      int trim_index;
-      /*trim_index = alloc_trim_analysis(st->mode, X, bandLogE, st->mode->nbEBands, LM, C, N);*/
-      trim_index = 3;
-      alloc_trim = trim_coef[trim_index];
-      ec_encode_bin(enc, trim_cdf[trim_index], trim_cdf[trim_index+1], 7);
-   }
+   alloc_trim = alloc_trim_analysis(st->mode, X, bandLogE, st->mode->nbEBands, LM, C, N);
+   ec_encode_bin(enc, trim_cdf[alloc_trim], trim_cdf[alloc_trim+1], 7);
 
    /* Variable bitrate */
    if (st->vbr_rate_norm>0)
@@ -1529,12 +1523,11 @@
    ALLOC(fine_quant, st->mode->nbEBands, int);
    {
       int fl;
-      int trim_index=0;
+      alloc_trim = 0;
       fl = ec_decode_bin(dec, 7);
-      while (trim_cdf[trim_index+1] <= fl)
-         trim_index++;
-      ec_dec_update(dec, trim_cdf[trim_index], trim_cdf[trim_index+1], 128);
-      alloc_trim = trim_coef[trim_index];
+      while (trim_cdf[alloc_trim+1] <= fl)
+         alloc_trim++;
+      ec_dec_update(dec, trim_cdf[alloc_trim], trim_cdf[alloc_trim+1], 128);
    }
 
    bits = len*8 - ec_dec_tell(dec, 0) - 1;
--- a/libcelt/rate.c
+++ b/libcelt/rate.c
@@ -268,12 +268,22 @@
    int codedBands;
    VARDECL(int, bits1);
    VARDECL(int, bits2);
+   VARDECL(int, thresh);
+   VARDECL(int, trim_offset);
    SAVE_STACK;
    
    len = m->nbEBands;
    ALLOC(bits1, len, int);
    ALLOC(bits2, len, int);
+   ALLOC(thresh, len, int);
+   ALLOC(trim_offset, len, int);
 
+   for (j=start;j<end;j++)
+      thresh[j] = 3*(C*(m->eBands[j+1]-m->eBands[j])<<LM<<BITRES)>>3;
+   for (j=start;j<end;j++)
+      trim_offset[j] = C*(m->eBands[j+1]-m->eBands[j])*(alloc_trim-3)*(m->nbEBands-j-1)
+            <<(LM+BITRES)>>5;
+
    lo = 0;
    hi = m->nbAllocVectors - 1;
    while (hi-lo != 1)
@@ -283,8 +293,17 @@
       for (j=start;j<end;j++)
       {
          int N = m->eBands[j+1]-m->eBands[j];
-         bits1[j] = ((alloc_trim*C*N*m->allocVectors[mid*len+j]<<LM>>5) + offsets[j]);
-         psum += bits1[j];
+         bits1[j] = C*N*m->allocVectors[mid*len+j]<<LM>>2;
+         if (bits1[j] > 0)
+            bits1[j] += trim_offset[j];
+         if (bits1[j] < 0)
+            bits1[j] = 0;
+         bits1[j] += offsets[j];
+         if (bits1[j] >= thresh[j])
+            psum += bits1[j];
+         else if (bits1[j] >= 1<<BITRES)
+            psum += 1<<BITRES;
+
          /*printf ("%d ", bits[j]);*/
       }
       /*printf ("\n");*/
@@ -298,8 +317,12 @@
    for (j=start;j<end;j++)
    {
       int N = m->eBands[j+1]-m->eBands[j];
-      bits1[j] = (alloc_trim*C*N*m->allocVectors[lo*len+j]<<LM>>5);
-      bits2[j] = (alloc_trim*C*N*m->allocVectors[hi*len+j]<<LM>>5) - bits1[j];
+      bits1[j] = (C*N*m->allocVectors[lo*len+j]<<LM>>2);
+      bits2[j] = (C*N*m->allocVectors[hi*len+j]<<LM>>2) - bits1[j];
+      if (bits1[j] > 0)
+         bits1[j] += trim_offset[j];
+      if (bits1[j] < 0)
+         bits1[j] = 0;
       bits1[j] += offsets[j];
    }
    codedBands = interp_bits2pulses(m, start, end, bits1, bits2, total, pulses, ebits, fine_priority, len, C, LM);