uploaded original spice-server-0.12.4 and celt-0.5.1.3
[sdk/emulator/libs/spice-server.git] / lib / celt-0.5.1.3 / libcelt / rangeenc.c
1 #ifdef HAVE_CONFIG_H
2 #include "config.h"
3 #endif
4
5 #include "arch.h"
6 #include "entenc.h"
7 #include "mfrngcod.h"
8
9
10
11 /*A range encoder.
12   See rangedec.c and the references for implementation details
13    \cite{Mar79,MNW98}.
14
15   @INPROCEEDINGS{Mar79,
16    author="Martin, G.N.N.",
17    title="Range encoding: an algorithm for removing redundancy from a digitised
18     message",
19    booktitle="Video \& Data Recording Conference",
20    year=1979,
21    address="Southampton",
22    month=Jul
23   }
24   @ARTICLE{MNW98,
25    author="Alistair Moffat and Radford Neal and Ian H. Witten",
26    title="Arithmetic Coding Revisited",
27    journal="{ACM} Transactions on Information Systems",
28    year=1998,
29    volume=16,
30    number=3,
31    pages="256--294",
32    month=Jul,
33    URL="http://www.stanford.edu/class/ee398/handouts/papers/Moffat98ArithmCoding.pdf"
34   }*/
35
36
37
38 /*Outputs a symbol, with a carry bit.
39   If there is a potential to propagate a carry over several symbols, they are
40    buffered until it can be determined whether or not an actual carry will
41    occur.
42   If the counter for the buffered symbols overflows, then the stream becomes
43    undecodable.
44   This gives a theoretical limit of a few billion symbols in a single packet on
45    32-bit systems.
46   The alternative is to truncate the range in order to force a carry, but
47    requires similar carry tracking in the decoder, needlessly slowing it down.*/
48 static void ec_enc_carry_out(ec_enc *_this,int _c){
49   if(_c!=EC_SYM_MAX){
50     /*No further carry propagation possible, flush buffer.*/
51     int carry;
52     carry=_c>>EC_SYM_BITS;
53     /*Don't output a byte on the first write.
54       This compare should be taken care of by branch-prediction thereafter.*/
55     if(_this->rem>=0)ec_byte_write1(_this->buf,_this->rem+carry);
56     if(_this->ext>0){
57       unsigned sym;
58       sym=EC_SYM_MAX+carry&EC_SYM_MAX;
59       do ec_byte_write1(_this->buf,sym);
60       while(--(_this->ext)>0);
61     }
62     _this->rem=_c&EC_SYM_MAX;
63   }
64   else _this->ext++;
65 }
66
67 static inline void ec_enc_normalize(ec_enc *_this){
68   /*If the range is too small, output some bits and rescale it.*/
69   while(_this->rng<=EC_CODE_BOT){
70     ec_enc_carry_out(_this,(int)(_this->low>>EC_CODE_SHIFT));
71     /*Move the next-to-high-order symbol into the high-order position.*/
72     _this->low=_this->low<<EC_SYM_BITS&EC_CODE_TOP-1;
73     _this->rng<<=EC_SYM_BITS;
74   }
75 }
76
77 void ec_enc_init(ec_enc *_this,ec_byte_buffer *_buf){
78   _this->buf=_buf;
79   _this->rem=-1;
80   _this->ext=0;
81   _this->low=0;
82   _this->rng=EC_CODE_TOP;
83 }
84
85 void ec_encode(ec_enc *_this,unsigned _fl,unsigned _fh,unsigned _ft){
86   ec_uint32 r;
87   r=_this->rng/_ft;
88   if(_fl>0){
89     _this->low+=_this->rng-IMUL32(r,(_ft-_fl));
90     _this->rng=IMUL32(r,(_fh-_fl));
91   }
92   else _this->rng-=IMUL32(r,(_ft-_fh));
93   ec_enc_normalize(_this);
94 }
95
96 void ec_encode_bin(ec_enc *_this,unsigned _fl,unsigned _fh,unsigned bits){
97    ec_uint32 r, ft;
98    r=_this->rng>>bits;
99    ft = (ec_uint32)1<<bits;
100    if(_fl>0){
101      _this->low+=_this->rng-IMUL32(r,(ft-_fl));
102      _this->rng=IMUL32(r,(_fh-_fl));
103    }
104    else _this->rng-=IMUL32(r,(ft-_fh));
105    ec_enc_normalize(_this);
106 }
107
108 long ec_enc_tell(ec_enc *_this,int _b){
109   ec_uint32 r;
110   int       l;
111   long      nbits;
112   nbits=(ec_byte_bytes(_this->buf)+(_this->rem>=0)+_this->ext)*EC_SYM_BITS;
113   /*To handle the non-integral number of bits still left in the encoder state,
114      we compute the number of bits of low that must be encoded to ensure that
115      the value is inside the range for any possible subsequent bits.
116     Note that this is subtly different than the actual value we would end the
117      stream with, which tries to make as many of the trailing bits zeros as
118      possible.*/
119   nbits+=EC_CODE_BITS;
120   nbits<<=_b;
121   l=EC_ILOG(_this->rng);
122   r=_this->rng>>l-16;
123   while(_b-->0){
124     int b;
125     r=r*r>>15;
126     b=(int)(r>>16);
127     l=l<<1|b;
128     r>>=b;
129   }
130   return nbits-l;
131 }
132
133 void ec_enc_done(ec_enc *_this){
134   /*We compute the integer in the current interval that has the largest number
135      of trailing zeros, and write that to the stream.
136     This is guaranteed to yield the smallest possible encoding.*/
137   if(_this->low){
138     ec_uint32 end;
139     end=EC_CODE_TOP;
140     /*Ensure that the end value is in the range.*/
141     if(end-_this->low>=_this->rng){
142       ec_uint32 msk;
143       msk=EC_CODE_TOP-1;
144       do{
145         msk>>=1;
146         end=_this->low+msk&~msk|msk+1;
147       }
148       while(end-_this->low>=_this->rng);
149     }
150     /*The remaining output is the next free end.*/
151     while(end){
152       ec_enc_carry_out(_this,end>>EC_CODE_SHIFT);
153       end=end<<EC_SYM_BITS&EC_CODE_TOP-1;
154     }
155   }
156   /*If we have a buffered byte flush it into the output buffer.*/
157   if(_this->rem>0||_this->ext>0){
158     ec_enc_carry_out(_this,0);
159     _this->rem=-1;
160   }
161 }