Move skip coding into interp_bits2pulses().
[opus.git] / libcelt / entenc.c
index a1702e8..8b1e1e3 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){
-   _b->end_ptr=_b->buf+_size-1;
+void ec_byte_shrink(ec_byte_buffer *_b, ec_uint32 _size){
+   int i;
+   int d;
+   int N;
+   d = _b->storage-_size;
+   N = _b->storage-(_b->end_ptr-_b->buf)-1;
+   /* Copy "raw bytes" */
+   _b->end_ptr=_b->buf+_size-1-N;
+   for (i=0;i<N;i++)
+      _b->end_ptr[i+1] = _b->end_ptr[i+1+d];
    _b->storage=_size;
 }
 
-void ec_byte_writetrunc(ec_byte_buffer *_b,long _bytes){
-  _b->ptr=_b->buf+_bytes;
-}
-
-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){
@@ -99,9 +91,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);
   }
 }
 
+int ec_enc_get_error(ec_enc *_this)
+{
+  return _this->error;
+}