shithub: opus

Download patch

ref: a4badac92eb367218c74bbb92c89c632df1bc225
parent: 30165bbae0526e1927c418256f29ddde8c665b4e
author: Jean-Marc Valin <[email protected]>
date: Fri Dec 3 10:20:11 EST 2010

Making VBR slightly exceed the budget rather than fail

--- a/libcelt/celt.c
+++ b/libcelt/celt.c
@@ -977,11 +977,11 @@
    if (st->vbr_rate_norm>0)
    {
      celt_word16 alpha;
-     celt_int32 delta;
+     celt_int32 delta, tell;
      /* The target rate in 8th bits per frame */
      celt_int32 vbr_rate;
      celt_int32 target;
-     celt_int32 vbr_bound, max_allowed;
+     celt_int32 vbr_bound, max_allowed, min_allowed;
 
      target = vbr_rate = M*st->vbr_rate_norm;
 
@@ -996,9 +996,11 @@
      else if (M > 1)
         target-=(target+14)/28;
 
+     tell = ec_enc_tell(enc, BITRES);
+
      /* The current offset is removed from the target and the space used
         so far is added*/
-     target=target+ec_enc_tell(enc, BITRES);
+     target=target+tell;
      /* By how much did we "miss" the target on that frame */
      delta = target - vbr_rate;
 
@@ -1005,10 +1007,11 @@
      /* Computes the max bit-rate allowed in VBR more to avoid violating the target rate and buffering */
      vbr_bound = vbr_rate;
      max_allowed = IMIN(vbr_rate+vbr_bound-st->vbr_reservoir>>(BITRES+3),nbAvailableBytes);
+     min_allowed = (tell>>(BITRES+3)) + 2 - nbFilledBytes;
 
      /* In VBR mode the frame size must not be reduced so much that it would result in the encoder running out of bits */
      nbAvailableBytes = target+(1<<(BITRES+2))>>(BITRES+3);
-     nbAvailableBytes=IMAX(16,IMIN(max_allowed,nbAvailableBytes));
+     nbAvailableBytes=IMAX(min_allowed,IMIN(max_allowed,nbAvailableBytes));
      target=nbAvailableBytes<<(BITRES+3);
 
      if (st->vbr_count < 970)