Fix a minor, but bitstream-affecting bug
[opus.git] / libcelt / entenc.c
index 44353f5..e395f33 100644 (file)
 #include "arch.h"
 
 
-#define EC_BUFFER_INCREMENT (256)
-
-void ec_byte_writeinit_buffer(ec_byte_buffer *_b, unsigned char *_buf, long _size){
+void ec_byte_writeinit_buffer(ec_byte_buffer *_b, unsigned char *_buf, ec_uint32 _size){
   _b->ptr=_b->buf=_buf;
   _b->end_ptr=_b->buf+_size-1;
   _b->storage=_size;
 }
 
-void ec_byte_shrink(ec_byte_buffer *_b, long _size){
+void ec_byte_shrink(ec_byte_buffer *_b, ec_uint32 _size){
    _b->end_ptr=_b->buf+_size-1;
    _b->storage=_size;
 }
 
-void ec_byte_write1(ec_byte_buffer *_b,unsigned _value){
+int ec_byte_write1(ec_byte_buffer *_b,unsigned _value){
   ptrdiff_t endbyte;
   endbyte=_b->ptr-_b->buf;
   if(endbyte>=_b->storage){
-    celt_fatal("range encoder overflow\n");
+    return 1;
+  } else {
+    *(_b->ptr++)=(unsigned char)_value;
+    return 0;
   }
-  *(_b->ptr++)=(unsigned char)_value;
 }
 
-void ec_byte_write_at_end(ec_byte_buffer *_b,unsigned _value){
+int ec_byte_write_at_end(ec_byte_buffer *_b,unsigned _value){
   if (_b->end_ptr < _b->ptr)
   {
-    celt_fatal("byte buffer collision");
-  }
-  *(_b->end_ptr--)=(unsigned char)_value;
-}
-
-
-void ec_enc_bits(ec_enc *_this,ec_uint32 _fl,int _ftb){
-  unsigned fl;
-  unsigned ft;
-  while(_ftb>EC_UNIT_BITS){
-    _ftb-=EC_UNIT_BITS;
-    fl=(unsigned)(_fl>>_ftb)&EC_UNIT_MASK;
-    ec_encode_raw(_this,fl,fl+1,EC_UNIT_BITS);
+    return 1;
+  } else {
+    *(_b->end_ptr--)=(unsigned char)_value;
+    return 0;
   }
-  ft=1<<_ftb;
-  fl=(unsigned)_fl&ft-1;
-  ec_encode_raw(_this,fl,fl+1,_ftb);
 }
 
 void ec_enc_uint(ec_enc *_this,ec_uint32 _fl,ec_uint32 _ft){
@@ -95,20 +83,13 @@ void ec_enc_uint(ec_enc *_this,ec_uint32 _fl,ec_uint32 _ft){
     ft=(_ft>>ftb)+1;
     fl=(unsigned)(_fl>>ftb);
     ec_encode(_this,fl,fl+1,ft);
-    ec_enc_bits(_this,_fl,ftb);
+    ec_enc_bits(_this,_fl&(1<<ftb)-1,ftb);
   } else {
     ec_encode(_this,_fl,_fl+1,_ft+1);
   }
 }
 
-/* The probability of having a "one" is given in 1/256 */
-void ec_enc_bit_prob(ec_enc *_this,int val,int _prob)
+int ec_enc_get_error(ec_enc *_this)
 {
-   int fl=0, fh=256-_prob;
-   if (val)
-   {
-      fl=fh;
-      fh=256;
-   }
-   ec_encode_bin(_this,fl,fh,8);
+  return _this->error;
 }