Using high-band noise less aggressively
[opus.git] / libcelt / rangeenc.c
index 6f8682d..b4a7343 100644 (file)
@@ -1,5 +1,5 @@
-/* (C) 2001-2008 Timothy B. Terriberry
-   (C) 2008 Jean-Marc Valin */
+/* Copyright (c) 2001-2008 Timothy B. Terriberry
+   Copyright (c) 2008-2009 Xiph.Org Foundation */
 /*
    Redistribution and use in source and binary forms, with or without
    modification, are permitted provided that the following conditions
@@ -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){
@@ -127,37 +128,50 @@ void ec_encode(ec_enc *_this,unsigned _fl,unsigned _fh,unsigned _ft){
   ec_enc_normalize(_this);
 }
 
-void ec_encode_bin(ec_enc *_this,unsigned _fl,unsigned _fh,unsigned bits){
-#if 0
-   ec_uint32 r, ft;
-   r=_this->rng>>bits;
-   ft = (ec_uint32)1<<bits;
+void ec_encode_bin(ec_enc *_this,unsigned _fl,unsigned _fh,unsigned _bits){
+   ec_uint32 r;
+   r=_this->rng>>_bits;
    if(_fl>0){
-     _this->low+=_this->rng-IMUL32(r,(ft-_fl));
-     _this->rng=IMUL32(r,(_fh-_fl));
+      _this->low+=_this->rng-IMUL32(r,((1<<_bits)-_fl));
+      _this->rng=IMUL32(r,(_fh-_fl));
    }
-   else _this->rng-=IMUL32(r,(ft-_fh));
+   else _this->rng-=IMUL32(r,((1<<_bits)-_fh));
    ec_enc_normalize(_this);
-#else
+}
+
+/*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=(r>>16)*_prob;
+   r-=s;
+   if(_val)_this->low=l+r;
+   _this->rng=_val?s:r;
+   ec_enc_normalize(_this);
+}
+
+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;
   }
   _this->end_byte |= (_fl<<(8-_this->end_bits_left)) & 0xff;
   _this->end_bits_left -= bits;
-#endif
 }
 
-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