shithub: opus

Download patch

ref: 25767d1ca7fbbb7160a1db1bc6a034903185a672
parent: a9ffc14ab7f00496b41f84a89c395b839e8452c4
author: Jean-Marc Valin <[email protected]>
date: Wed Oct 21 19:24:18 EDT 2009

Better computation of the VBR rate upper bound and reducing the coarse energy
budget at low bitrate.

--- a/libcelt/celt.c
+++ b/libcelt/celt.c
@@ -764,16 +764,28 @@
    ALLOC(fine_quant, st->mode->nbEBands, int);
    ALLOC(pulses, st->mode->nbEBands, int);
 
+   /* Computes the max bit-rate allowed in VBR more to avoid busting the budget */
+   if (st->vbr_rate>0)
+   {
+      celt_int32 vbr_bound, max_allowed;
+
+      vbr_bound = st->vbr_rate;
+      max_allowed = (st->vbr_rate + vbr_bound - st->vbr_reservoir)>>(BITRES+3); 
+      if (max_allowed < nbCompressedBytes)
+         nbCompressedBytes = max_allowed;
+   }
+
    /* Bit allocation */
    ALLOC(error, C*st->mode->nbEBands, celt_word16);
-   coarse_needed = quant_coarse_energy(st->mode, bandLogE, st->oldBandE, nbCompressedBytes*8-16, intra_ener, st->mode->prob, error, &enc, C);
+   coarse_needed = quant_coarse_energy(st->mode, bandLogE, st->oldBandE, nbCompressedBytes*4-8, intra_ener, st->mode->prob, error, &enc, C);
    coarse_needed = ((coarse_needed*3-1)>>3)+1;
-
+   if (coarse_needed > nbCompressedBytes)
+      coarse_needed = nbCompressedBytes;
    /* Variable bitrate */
    if (st->vbr_rate>0)
    {
      celt_word16 alpha;
-     celt_int32 delta, vbr_bound;
+     celt_int32 delta;
      /* The target rate in 16th bits per frame */
      celt_int32 target=st->vbr_rate;
    
@@ -812,16 +824,8 @@
      /*printf ("%d\n", st->vbr_drift);*/
 
      /* We could use any multiple of vbr_rate as bound (depending on the delay) */
-     vbr_bound = st->vbr_rate;
-     if (st->vbr_reservoir > vbr_bound)
+     if (st->vbr_reservoir < 0)
      {
-        /* Busted the reservoir -- reduce the rate */
-        int adjust = 1+(st->vbr_reservoir-vbr_bound-1)/(8<<BITRES);
-        nbCompressedBytes -= adjust;
-        st->vbr_reservoir -= adjust*(8<<BITRES);
-        /*printf ("-%d\n", adjust);*/
-     } else if (st->vbr_reservoir < 0)
-     {
         /* We're under the min value -- increase rate */
         int adjust = 1-(st->vbr_reservoir-1)/(8<<BITRES);
         st->vbr_reservoir += adjust*(8<<BITRES);
@@ -1373,7 +1377,7 @@
 
    ALLOC(fine_quant, st->mode->nbEBands, int);
    /* Get band energies */
-   unquant_coarse_energy(st->mode, bandE, st->oldBandE, len*8-16, intra_ener, st->mode->prob, &dec, C);
+   unquant_coarse_energy(st->mode, bandE, st->oldBandE, len*4-8, intra_ener, st->mode->prob, &dec, C);
    
    ALLOC(pulses, st->mode->nbEBands, int);
    ALLOC(offsets, st->mode->nbEBands, int);