This fixes a VBR bug introduced by raw bits. We should not write any raw
authorJean-Marc Valin <jean-marc.valin@usherbrooke.ca>
Sat, 1 Aug 2009 21:05:47 +0000 (23:05 +0200)
committerJean-Marc Valin <jean-marc.valin@usherbrooke.ca>
Sat, 1 Aug 2009 21:05:47 +0000 (23:05 +0200)
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.

libcelt/celt.c
libcelt/entcode.h
libcelt/entenc.c

index fb86278..44386d6 100644 (file)
@@ -782,10 +782,10 @@ int celt_encode_float(CELTEncoder * restrict st, const celt_sig_t * pcm, celt_si
    {
       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 @@ int celt_encode_float(CELTEncoder * restrict st, const celt_sig_t * pcm, celt_si
      /* 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 @@ int celt_decode_float(CELTDecoder * restrict st, const unsigned char *data, int
    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);
index 5148242..57107d4 100644 (file)
@@ -63,6 +63,7 @@ struct ec_byte_buffer{
 
 /*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);
index 1ff1036..095170b 100644 (file)
@@ -47,6 +47,12 @@ void ec_byte_writeinit_buffer(ec_byte_buffer *_b, unsigned char *_buf, long _siz
   _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;