Moved the content of libentcode into libcelt to reduce dependencies,
[opus.git] / libcelt / probenc.c
1 #include <string.h>
2 #include "probenc.h"
3 #include "bitrenc.h"
4
5
6
7 static void ec_probmod_samp_and_update(ec_probmod *_this,ec_probsamp *_samp,
8  unsigned _sym){
9   unsigned sz;
10   sz=_this->sz;
11   _samp->fs=ec_bitree_get_freq(_this->bitree,_sym);
12   _samp->fl=ec_bitree_get_cumul(_this->bitree,_sym);
13   _samp->ft=_this->ft;
14   ec_bitree_update(_this->bitree,sz,_sym,_this->inc);
15   _this->ft+=_this->inc;
16   if(_this->ft>_this->thresh){
17     ec_bitree_halve(_this->bitree,sz,_this->split);
18     _this->ft=ec_bitree_get_cumul(_this->bitree,sz);
19   }
20 }
21
22 static void ec_probmod_samp_and_update_range(ec_probmod *_this,
23  ec_probsamp *_samp,int _sym,int _lo,int _hi){
24   unsigned base;
25   int      sz;
26   sz=_this->sz;
27   base=ec_bitree_get_cumul(_this->bitree,_lo);
28   _samp->fs=ec_bitree_get_freq(_this->bitree,_sym);
29   _samp->fl=ec_bitree_get_cumul(_this->bitree,_sym)-base;
30   _samp->ft=ec_bitree_get_cumul(_this->bitree,_hi)-base;
31   ec_bitree_update(_this->bitree,sz,_sym,_this->inc);
32   _this->ft+=_this->inc;
33   if(_this->ft>_this->thresh){
34     ec_bitree_halve(_this->bitree,sz,_this->split);
35     _this->ft=ec_bitree_get_cumul(_this->bitree,sz);
36   }
37 }
38
39 void ec_probmod_write(ec_probmod *_this,ec_enc *_enc,int _sym){
40   ec_probsamp samp;
41   ec_probmod_samp_and_update(_this,&samp,_sym);
42   ec_encode(_enc,samp.fl,samp.fl+samp.fs,samp.ft);
43 }
44
45 void ec_probmod_write_range(ec_probmod *_this,ec_enc *_enc,int _sym,
46  int _lo,int _hi){
47   ec_probsamp samp;
48   ec_probmod_samp_and_update_range(_this,&samp,_sym,_lo,_hi);
49   ec_encode(_enc,samp.fl,samp.fl+samp.fs,samp.ft);
50 }