123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246 |
- #ifdef HAVE_CONFIG_H
- #include "config.h"
- #endif
- #include <stddef.h>
- #include "os_support.h"
- #include "arch.h"
- #include "entdec.h"
- #include "mfrngcod.h"
- static int ec_read_byte(ec_dec *_this){
- return _this->offs<_this->storage?_this->buf[_this->offs++]:0;
- }
- static int ec_read_byte_from_end(ec_dec *_this){
- return _this->end_offs<_this->storage?
- _this->buf[_this->storage-++(_this->end_offs)]:0;
- }
- static void ec_dec_normalize(ec_dec *_this){
-
- while(_this->rng<=EC_CODE_BOT){
- int sym;
- _this->nbits_total+=EC_SYM_BITS;
- _this->rng<<=EC_SYM_BITS;
-
- sym=_this->rem;
-
- _this->rem=ec_read_byte(_this);
-
- sym=(sym<<EC_SYM_BITS|_this->rem)>>(EC_SYM_BITS-EC_CODE_EXTRA);
-
- _this->val=((_this->val<<EC_SYM_BITS)+(EC_SYM_MAX&~sym))&(EC_CODE_TOP-1);
- }
- }
- void ec_dec_init(ec_dec *_this,unsigned char *_buf,opus_uint32 _storage){
- _this->buf=_buf;
- _this->storage=_storage;
- _this->end_offs=0;
- _this->end_window=0;
- _this->nend_bits=0;
-
- _this->nbits_total=EC_CODE_BITS+1
- -((EC_CODE_BITS-EC_CODE_EXTRA)/EC_SYM_BITS)*EC_SYM_BITS;
- _this->offs=0;
- _this->rng=1U<<EC_CODE_EXTRA;
- _this->rem=ec_read_byte(_this);
- _this->val=_this->rng-1-(_this->rem>>(EC_SYM_BITS-EC_CODE_EXTRA));
- _this->error=0;
-
- ec_dec_normalize(_this);
- }
- unsigned ec_decode(ec_dec *_this,unsigned _ft){
- unsigned s;
- _this->ext=celt_udiv(_this->rng,_ft);
- s=(unsigned)(_this->val/_this->ext);
- return _ft-EC_MINI(s+1,_ft);
- }
- unsigned ec_decode_bin(ec_dec *_this,unsigned _bits){
- unsigned s;
- _this->ext=_this->rng>>_bits;
- s=(unsigned)(_this->val/_this->ext);
- return (1U<<_bits)-EC_MINI(s+1U,1U<<_bits);
- }
- void ec_dec_update(ec_dec *_this,unsigned _fl,unsigned _fh,unsigned _ft){
- opus_uint32 s;
- s=IMUL32(_this->ext,_ft-_fh);
- _this->val-=s;
- _this->rng=_fl>0?IMUL32(_this->ext,_fh-_fl):_this->rng-s;
- ec_dec_normalize(_this);
- }
- int ec_dec_bit_logp(ec_dec *_this,unsigned _logp){
- opus_uint32 r;
- opus_uint32 d;
- opus_uint32 s;
- int ret;
- r=_this->rng;
- d=_this->val;
- s=r>>_logp;
- ret=d<s;
- if(!ret)_this->val=d-s;
- _this->rng=ret?s:r-s;
- ec_dec_normalize(_this);
- return ret;
- }
- int ec_dec_icdf(ec_dec *_this,const unsigned char *_icdf,unsigned _ftb){
- opus_uint32 r;
- opus_uint32 d;
- opus_uint32 s;
- opus_uint32 t;
- int ret;
- s=_this->rng;
- d=_this->val;
- r=s>>_ftb;
- ret=-1;
- do{
- t=s;
- s=IMUL32(r,_icdf[++ret]);
- }
- while(d<s);
- _this->val=d-s;
- _this->rng=t-s;
- ec_dec_normalize(_this);
- return ret;
- }
- opus_uint32 ec_dec_uint(ec_dec *_this,opus_uint32 _ft){
- unsigned ft;
- unsigned s;
- int ftb;
-
- celt_assert(_ft>1);
- _ft--;
- ftb=EC_ILOG(_ft);
- if(ftb>EC_UINT_BITS){
- opus_uint32 t;
- ftb-=EC_UINT_BITS;
- ft=(unsigned)(_ft>>ftb)+1;
- s=ec_decode(_this,ft);
- ec_dec_update(_this,s,s+1,ft);
- t=(opus_uint32)s<<ftb|ec_dec_bits(_this,ftb);
- if(t<=_ft)return t;
- _this->error=1;
- return _ft;
- }
- else{
- _ft++;
- s=ec_decode(_this,(unsigned)_ft);
- ec_dec_update(_this,s,s+1,(unsigned)_ft);
- return s;
- }
- }
- opus_uint32 ec_dec_bits(ec_dec *_this,unsigned _bits){
- ec_window window;
- int available;
- opus_uint32 ret;
- window=_this->end_window;
- available=_this->nend_bits;
- if((unsigned)available<_bits){
- do{
- window|=(ec_window)ec_read_byte_from_end(_this)<<available;
- available+=EC_SYM_BITS;
- }
- while(available<=EC_WINDOW_SIZE-EC_SYM_BITS);
- }
- ret=(opus_uint32)window&(((opus_uint32)1<<_bits)-1U);
- window>>=_bits;
- available-=_bits;
- _this->end_window=window;
- _this->nend_bits=available;
- _this->nbits_total+=_bits;
- return ret;
- }
|