encoder pre-emphasis now in 16-bits
[opus.git] / libcelt / rangeenc.c
index 7fcd3d1..51e1ffe 100644 (file)
@@ -2,6 +2,7 @@
 #include "config.h"
 #endif
 
+#include "arch.h"
 #include "entenc.h"
 #include "mfrngcod.h"
 
@@ -63,7 +64,7 @@ static void ec_enc_carry_out(ec_enc *_this,int _c){
   else _this->ext++;
 }
 
-static void ec_enc_normalize(ec_enc *_this){
+static inline void ec_enc_normalize(ec_enc *_this){
   /*If the range is too small, output some bits and rescale it.*/
   while(_this->rng<=EC_CODE_BOT){
     ec_enc_carry_out(_this,(int)(_this->low>>EC_CODE_SHIFT));
@@ -85,13 +86,25 @@ void ec_encode(ec_enc *_this,unsigned _fl,unsigned _fh,unsigned _ft){
   ec_uint32 r;
   r=_this->rng/_ft;
   if(_fl>0){
-    _this->low+=_this->rng-r*(_ft-_fl);
-    _this->rng=r*(_fh-_fl);
+    _this->low+=_this->rng-IMUL32(r,(_ft-_fl));
+    _this->rng=IMUL32(r,(_fh-_fl));
   }
-  else _this->rng-=r*(_ft-_fh);
+  else _this->rng-=IMUL32(r,(_ft-_fh));
   ec_enc_normalize(_this);
 }
 
+void ec_encode_bin(ec_enc *_this,unsigned _fl,unsigned _fh,unsigned bits){
+   ec_uint32 r, ft;
+   r=_this->rng>>bits;
+   ft = (ec_uint32)1<<bits;
+   if(_fl>0){
+     _this->low+=_this->rng-IMUL32(r,(ft-_fl));
+     _this->rng=IMUL32(r,(_fh-_fl));
+   }
+   else _this->rng-=IMUL32(r,(ft-_fh));
+   ec_enc_normalize(_this);
+}
+
 long ec_enc_tell(ec_enc *_this,int _b){
   ec_uint32 r;
   int       l;