shithub: opus

Download patch

ref: f1fea66b53148f6d05c0317555436fd7df44385a
parent: 8226ac083ea19f57b69e863251fe487228bc2925
author: Jean-Marc Valin <[email protected]>
date: Sat Oct 9 18:58:52 EDT 2010

Dynamic allocation before VBR

--- a/libcelt/celt.c
+++ b/libcelt/celt.c
@@ -574,6 +574,7 @@
    int nbFilledBytes, nbAvailableBytes;
    int effEnd;
    int codedBands;
+   int alloc_trim;
    SAVE_STACK;
 
    if (nbCompressedBytes<0 || pcm==NULL)
@@ -785,6 +786,56 @@
    ec_enc_bit_prob(enc, has_fold>>1, 8192);
    ec_enc_bit_prob(enc, has_fold&1, (has_fold>>1) ? 32768 : 49152);
 
+   ALLOC(offsets, st->mode->nbEBands, int);
+
+   for (i=0;i<st->mode->nbEBands;i++)
+      offsets[i] = 0;
+   /* Dynamic allocation code */
+   /* Make sure that dynamic allocation can't make us bust the budget */
+   if (nbCompressedBytes > 30)
+   {
+      int t1, t2;
+      if (LM <= 1)
+      {
+         t1 = 3;
+         t2 = 5;
+      } else {
+         t1 = 2;
+         t2 = 4;
+      }
+      for (i=1;i<st->mode->nbEBands-1;i++)
+      {
+         if (2*bandLogE[i]-bandLogE[i-1]-bandLogE[i+1] > SHL16(t1,DB_SHIFT))
+            offsets[i] += 1;
+         if (2*bandLogE[i]-bandLogE[i-1]-bandLogE[i+1] > SHL16(t2,DB_SHIFT))
+            offsets[i] += 1;
+      }
+   }
+   for (i=0;i<st->mode->nbEBands;i++)
+   {
+      int j;
+      ec_enc_bit_prob(enc, offsets[i]!=0, 1024);
+      if (offsets[i]!=0)
+      {
+         for (j=0;j<offsets[i]-1;j++)
+            ec_enc_bit_prob(enc, 1, 32768);
+         ec_enc_bit_prob(enc, 0, 32768);
+      }
+      offsets[i] *= (6<<BITRES);
+   }
+   {
+      int trim_index = 3;
+
+      /*if (isTransient)
+         trim_index--;
+      if (has_fold==0)
+         trim_index--;
+      if (C==2)
+         trim_index--;*/
+      alloc_trim = trim_coef[trim_index];
+      ec_encode_bin(enc, trim_cdf[trim_index], trim_cdf[trim_index+1], 7);
+   }
+
    /* Variable bitrate */
    if (st->vbr_rate_norm>0)
    {
@@ -859,57 +910,11 @@
    /* Bit allocation */
    ALLOC(fine_quant, st->mode->nbEBands, int);
    ALLOC(pulses, st->mode->nbEBands, int);
-   ALLOC(offsets, st->mode->nbEBands, int);
    ALLOC(fine_priority, st->mode->nbEBands, int);
 
-   for (i=0;i<st->mode->nbEBands;i++)
-      offsets[i] = 0;
-   /* Dynamic allocation code */
-   /* Make sure that dynamic allocation can't make us bust the budget */
-   if (nbCompressedBytes > 30)
-   {
-      int t1, t2;
-      if (LM <= 1)
-      {
-         t1 = 3;
-         t2 = 5;
-      } else {
-         t1 = 2;
-         t2 = 4;
-      }
-      for (i=1;i<st->mode->nbEBands-1;i++)
-      {
-         if (2*bandLogE[i]-bandLogE[i-1]-bandLogE[i+1] > SHL16(t1,DB_SHIFT))
-            offsets[i] += 1;
-         if (2*bandLogE[i]-bandLogE[i-1]-bandLogE[i+1] > SHL16(t2,DB_SHIFT))
-            offsets[i] += 1;
-      }
-   }
-   for (i=0;i<st->mode->nbEBands;i++)
-   {
-      int j;
-      ec_enc_bit_prob(enc, offsets[i]!=0, 1024);
-      if (offsets[i]!=0)
-      {
-         for (j=0;j<offsets[i]-1;j++)
-            ec_enc_bit_prob(enc, 1, 32768);
-         ec_enc_bit_prob(enc, 0, 32768);
-      }
-      offsets[i] *= (6<<BITRES);
-   }
    bits = nbCompressedBytes*8 - ec_enc_tell(enc, 0) - 1;
-   {
-      int alloc_trim;
-      int trim_index = 3;
+   codedBands = compute_allocation(st->mode, st->start, st->end, offsets, alloc_trim, bits, pulses, fine_quant, fine_priority, C, LM);
 
-      /*if (isTransient)
-         trim_index = 2;
-      if (has_fold==0)
-         trim_index = 2;*/
-      alloc_trim = trim_coef[trim_index];
-      ec_encode_bin(enc, trim_cdf[trim_index], trim_cdf[trim_index+1], 7);
-      codedBands = compute_allocation(st->mode, st->start, st->end, offsets, alloc_trim, bits, pulses, fine_quant, fine_priority, C, LM);
-   }
    quant_fine_energy(st->mode, st->start, st->end, bandE, oldBandE, error, fine_quant, enc, C);
 
 #ifdef MEASURE_NORM_MSE
@@ -1472,6 +1477,7 @@
    int nbFilledBytes, nbAvailableBytes;
    int effEnd;
    int codedBands;
+   int alloc_trim;
    SAVE_STACK;
 
    if (pcm==NULL)
@@ -1591,10 +1597,9 @@
       }
    }
 
-   bits = len*8 - ec_dec_tell(dec, 0) - 1;
    ALLOC(fine_quant, st->mode->nbEBands, int);
    {
-      int fl, alloc_trim;
+      int fl;
       int trim_index=0;
       fl = ec_decode_bin(dec, 7);
       while (trim_cdf[trim_index+1] <= fl)
@@ -1601,10 +1606,10 @@
          trim_index++;
       ec_dec_update(dec, trim_cdf[trim_index], trim_cdf[trim_index+1], 128);
       alloc_trim = trim_coef[trim_index];
-      codedBands = compute_allocation(st->mode, st->start, st->end, offsets, alloc_trim, bits, pulses, fine_quant, fine_priority, C, LM);
    }
-   /*bits = ec_dec_tell(dec, 0);
-   compute_fine_allocation(st->mode, fine_quant, (20*C+len*8/5-(ec_dec_tell(dec, 0)-bits))/C);*/
+
+   bits = len*8 - ec_dec_tell(dec, 0) - 1;
+   codedBands = compute_allocation(st->mode, st->start, st->end, offsets, alloc_trim, bits, pulses, fine_quant, fine_priority, C, LM);
    
    unquant_fine_energy(st->mode, st->start, st->end, bandE, oldBandE, fine_quant, dec, C);