shithub: opus

Download patch

ref: e610864c7414f92eeb26c1b0260d23e0d5133fb7
parent: 91f07dc125247a422c186edbebc05f54380b2dbf
author: Jean-Marc Valin <[email protected]>
date: Sat Aug 1 19:05:47 EDT 2009

This fixes a VBR bug introduced by raw bits. We should not write any raw
bit before the rate is decided (otherwise they'll end up at the wrong place)
and we have to shrink the byte buffer before writing raw bits.

--- a/libcelt/celt.c
+++ b/libcelt/celt.c
@@ -782,10 +782,10 @@
    {
       if (transient_shift)
       {
-         ec_enc_bits(&enc, transient_shift, 2);
+         ec_enc_uint(&enc, transient_shift, 4);
          ec_enc_uint(&enc, transient_time, N+st->overlap);
       } else {
-         ec_enc_bits(&enc, mdct_weight_shift, 2);
+         ec_enc_uint(&enc, mdct_weight_shift, 4);
          if (mdct_weight_shift && st->mode->nbShortMdcts!=2)
             ec_enc_uint(&enc, mdct_weight_pos, st->mode->nbShortMdcts-1);
       }
@@ -833,6 +833,7 @@
      /* In VBR mode the frame size must not be reduced so much that it would result in the coarse energy busting its budget */
      target=IMAX(coarse_needed,(target+64)/128);
      nbCompressedBytes=IMIN(nbCompressedBytes,target);
+     ec_byte_shrink(&buf, nbCompressedBytes);
    }
 
    ALLOC(offsets, st->mode->nbEBands, int);
@@ -1328,7 +1329,7 @@
    decode_flags(&dec, &intra_ener, &has_pitch, &shortBlocks, &has_fold);
    if (shortBlocks)
    {
-      transient_shift = ec_dec_bits(&dec, 2);
+      transient_shift = ec_dec_uint(&dec, 4);
       if (transient_shift == 3)
       {
          transient_time = ec_dec_uint(&dec, N+st->mode->overlap);
--- a/libcelt/entcode.h
+++ b/libcelt/entcode.h
@@ -63,6 +63,7 @@
 
 /*Encoding functions.*/
 void ec_byte_writeinit_buffer(ec_byte_buffer *_b, unsigned char *_buf, long _size);
+void ec_byte_shrink(ec_byte_buffer *_b, long _size);
 void ec_byte_writeinit(ec_byte_buffer *_b);
 void ec_byte_writetrunc(ec_byte_buffer *_b,long _bytes);
 void ec_byte_write1(ec_byte_buffer *_b,unsigned _value);
--- a/libcelt/entenc.c
+++ b/libcelt/entenc.c
@@ -47,6 +47,12 @@
   _b->resizable=0;
 }
 
+void ec_byte_shrink(ec_byte_buffer *_b, long _size){
+   _b->end_ptr=_b->buf+_size-1;
+   _b->storage=_size;
+   _b->resizable=0;
+}
+
 void ec_byte_writeinit(ec_byte_buffer *_b){
   _b->ptr=_b->buf=celt_alloc(EC_BUFFER_INCREMENT*sizeof(char));
   _b->storage=EC_BUFFER_INCREMENT;