encoder pre-emphasis now in 16-bits
[opus.git] / libcelt / rangeenc.c
index 885854f..51e1ffe 100644 (file)
@@ -2,7 +2,7 @@
 #include "config.h"
 #endif
 
-#include <stddef.h>
+#include "arch.h"
 #include "entenc.h"
 #include "mfrngcod.h"
 
@@ -64,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));
@@ -86,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;
@@ -123,11 +135,11 @@ void ec_enc_done(ec_enc *_this){
      of trailing zeros, and write that to the stream.
     This is guaranteed to yield the smallest possible encoding.*/
   if(_this->low){
-    unsigned end;
+    ec_uint32 end;
     end=EC_CODE_TOP;
     /*Ensure that the end value is in the range.*/
     if(end-_this->low>=_this->rng){
-      unsigned msk;
+      ec_uint32 msk;
       msk=EC_CODE_TOP-1;
       do{
         msk>>=1;