Using high-band noise less aggressively
[opus.git] / libcelt / rangeenc.c
index f7f171b..b4a7343 100644 (file)
@@ -83,11 +83,11 @@ static void ec_enc_carry_out(ec_enc *_this,int _c){
     carry=_c>>EC_SYM_BITS;
     /*Don't output a byte on the first write.
       This compare should be taken care of by branch-prediction thereafter.*/
-    if(_this->rem>=0)ec_byte_write1(_this->buf,_this->rem+carry);
+    if(_this->rem>=0)_this->error|=ec_byte_write1(_this->buf,_this->rem+carry);
     if(_this->ext>0){
       unsigned sym;
       sym=EC_SYM_MAX+carry&EC_SYM_MAX;
-      do ec_byte_write1(_this->buf,sym);
+      do _this->error|=ec_byte_write1(_this->buf,sym);
       while(--(_this->ext)>0);
     }
     _this->rem=_c&EC_SYM_MAX;
@@ -114,6 +114,7 @@ void ec_enc_init(ec_enc *_this,ec_byte_buffer *_buf){
   _this->end_byte=0;
   _this->end_bits_left=8;
   _this->nb_end_bits=0;
+  _this->error=0;
 }
 
 void ec_encode(ec_enc *_this,unsigned _fl,unsigned _fh,unsigned _ft){
@@ -138,27 +139,27 @@ void ec_encode_bin(ec_enc *_this,unsigned _fl,unsigned _fh,unsigned _bits){
    ec_enc_normalize(_this);
 }
 
-/*The probability of having a "one" is given in 1/256.*/
-void ec_enc_bit_prob(ec_enc *_this,int _val,int _prob){
+/*The probability of having a "one" is given in 1/65536.*/
+void ec_enc_bit_prob(ec_enc *_this,int _val,unsigned _prob){
    ec_uint32 r;
    ec_uint32 s;
    ec_uint32 l;
    r=_this->rng;
    l=_this->low;
-   s=IMUL32(r>>8,_prob);
+   s=(r>>16)*_prob;
    r-=s;
    if(_val)_this->low=l+r;
    _this->rng=_val?s:r;
    ec_enc_normalize(_this);
 }
 
-void ec_encode_raw(ec_enc *_this,unsigned _fl,unsigned _fh,unsigned bits){
+void ec_enc_bits(ec_enc *_this,unsigned _fl,unsigned bits){
   _this->nb_end_bits += bits;
   while (bits >= _this->end_bits_left)
   {
     _this->end_byte |= (_fl<<(8-_this->end_bits_left)) & 0xff;
     _fl >>= _this->end_bits_left;
-    ec_byte_write_at_end(_this->buf, _this->end_byte);
+    _this->error|=ec_byte_write_at_end(_this->buf, _this->end_byte);
     _this->end_byte = 0;
     bits -= _this->end_bits_left;
     _this->end_bits_left = 8;
@@ -167,10 +168,10 @@ void ec_encode_raw(ec_enc *_this,unsigned _fl,unsigned _fh,unsigned bits){
   _this->end_bits_left -= bits;
 }
 
-long ec_enc_tell(ec_enc *_this,int _b){
+ec_uint32 ec_enc_tell(ec_enc *_this,int _b){
   ec_uint32 r;
   int       l;
-  long      nbits;
+  ec_uint32      nbits;
   nbits=(ec_byte_bytes(_this->buf)+(_this->rem>=0)+_this->ext)*EC_SYM_BITS;
   /*To handle the non-integral number of bits still left in the encoder state,
      we compute the number of bits of low that must be encoded to ensure that