cannam@154
|
1 /* Copyright (c) 2001-2011 Timothy B. Terriberry
|
cannam@154
|
2 Copyright (c) 2008-2009 Xiph.Org Foundation */
|
cannam@154
|
3 /*
|
cannam@154
|
4 Redistribution and use in source and binary forms, with or without
|
cannam@154
|
5 modification, are permitted provided that the following conditions
|
cannam@154
|
6 are met:
|
cannam@154
|
7
|
cannam@154
|
8 - Redistributions of source code must retain the above copyright
|
cannam@154
|
9 notice, this list of conditions and the following disclaimer.
|
cannam@154
|
10
|
cannam@154
|
11 - Redistributions in binary form must reproduce the above copyright
|
cannam@154
|
12 notice, this list of conditions and the following disclaimer in the
|
cannam@154
|
13 documentation and/or other materials provided with the distribution.
|
cannam@154
|
14
|
cannam@154
|
15 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
cannam@154
|
16 ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
cannam@154
|
17 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
cannam@154
|
18 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
|
cannam@154
|
19 OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
cannam@154
|
20 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
cannam@154
|
21 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
cannam@154
|
22 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
cannam@154
|
23 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
cannam@154
|
24 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
cannam@154
|
25 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
cannam@154
|
26 */
|
cannam@154
|
27
|
cannam@154
|
28 #if defined(HAVE_CONFIG_H)
|
cannam@154
|
29 # include "config.h"
|
cannam@154
|
30 #endif
|
cannam@154
|
31 #include "os_support.h"
|
cannam@154
|
32 #include "arch.h"
|
cannam@154
|
33 #include "entenc.h"
|
cannam@154
|
34 #include "mfrngcod.h"
|
cannam@154
|
35
|
cannam@154
|
36 /*A range encoder.
|
cannam@154
|
37 See entdec.c and the references for implementation details \cite{Mar79,MNW98}.
|
cannam@154
|
38
|
cannam@154
|
39 @INPROCEEDINGS{Mar79,
|
cannam@154
|
40 author="Martin, G.N.N.",
|
cannam@154
|
41 title="Range encoding: an algorithm for removing redundancy from a digitised
|
cannam@154
|
42 message",
|
cannam@154
|
43 booktitle="Video \& Data Recording Conference",
|
cannam@154
|
44 year=1979,
|
cannam@154
|
45 address="Southampton",
|
cannam@154
|
46 month=Jul
|
cannam@154
|
47 }
|
cannam@154
|
48 @ARTICLE{MNW98,
|
cannam@154
|
49 author="Alistair Moffat and Radford Neal and Ian H. Witten",
|
cannam@154
|
50 title="Arithmetic Coding Revisited",
|
cannam@154
|
51 journal="{ACM} Transactions on Information Systems",
|
cannam@154
|
52 year=1998,
|
cannam@154
|
53 volume=16,
|
cannam@154
|
54 number=3,
|
cannam@154
|
55 pages="256--294",
|
cannam@154
|
56 month=Jul,
|
cannam@154
|
57 URL="http://www.stanford.edu/class/ee398/handouts/papers/Moffat98ArithmCoding.pdf"
|
cannam@154
|
58 }*/
|
cannam@154
|
59
|
cannam@154
|
60 static int ec_write_byte(ec_enc *_this,unsigned _value){
|
cannam@154
|
61 if(_this->offs+_this->end_offs>=_this->storage)return -1;
|
cannam@154
|
62 _this->buf[_this->offs++]=(unsigned char)_value;
|
cannam@154
|
63 return 0;
|
cannam@154
|
64 }
|
cannam@154
|
65
|
cannam@154
|
66 static int ec_write_byte_at_end(ec_enc *_this,unsigned _value){
|
cannam@154
|
67 if(_this->offs+_this->end_offs>=_this->storage)return -1;
|
cannam@154
|
68 _this->buf[_this->storage-++(_this->end_offs)]=(unsigned char)_value;
|
cannam@154
|
69 return 0;
|
cannam@154
|
70 }
|
cannam@154
|
71
|
cannam@154
|
72 /*Outputs a symbol, with a carry bit.
|
cannam@154
|
73 If there is a potential to propagate a carry over several symbols, they are
|
cannam@154
|
74 buffered until it can be determined whether or not an actual carry will
|
cannam@154
|
75 occur.
|
cannam@154
|
76 If the counter for the buffered symbols overflows, then the stream becomes
|
cannam@154
|
77 undecodable.
|
cannam@154
|
78 This gives a theoretical limit of a few billion symbols in a single packet on
|
cannam@154
|
79 32-bit systems.
|
cannam@154
|
80 The alternative is to truncate the range in order to force a carry, but
|
cannam@154
|
81 requires similar carry tracking in the decoder, needlessly slowing it down.*/
|
cannam@154
|
82 static void ec_enc_carry_out(ec_enc *_this,int _c){
|
cannam@154
|
83 if(_c!=EC_SYM_MAX){
|
cannam@154
|
84 /*No further carry propagation possible, flush buffer.*/
|
cannam@154
|
85 int carry;
|
cannam@154
|
86 carry=_c>>EC_SYM_BITS;
|
cannam@154
|
87 /*Don't output a byte on the first write.
|
cannam@154
|
88 This compare should be taken care of by branch-prediction thereafter.*/
|
cannam@154
|
89 if(_this->rem>=0)_this->error|=ec_write_byte(_this,_this->rem+carry);
|
cannam@154
|
90 if(_this->ext>0){
|
cannam@154
|
91 unsigned sym;
|
cannam@154
|
92 sym=(EC_SYM_MAX+carry)&EC_SYM_MAX;
|
cannam@154
|
93 do _this->error|=ec_write_byte(_this,sym);
|
cannam@154
|
94 while(--(_this->ext)>0);
|
cannam@154
|
95 }
|
cannam@154
|
96 _this->rem=_c&EC_SYM_MAX;
|
cannam@154
|
97 }
|
cannam@154
|
98 else _this->ext++;
|
cannam@154
|
99 }
|
cannam@154
|
100
|
cannam@154
|
101 static OPUS_INLINE void ec_enc_normalize(ec_enc *_this){
|
cannam@154
|
102 /*If the range is too small, output some bits and rescale it.*/
|
cannam@154
|
103 while(_this->rng<=EC_CODE_BOT){
|
cannam@154
|
104 ec_enc_carry_out(_this,(int)(_this->val>>EC_CODE_SHIFT));
|
cannam@154
|
105 /*Move the next-to-high-order symbol into the high-order position.*/
|
cannam@154
|
106 _this->val=(_this->val<<EC_SYM_BITS)&(EC_CODE_TOP-1);
|
cannam@154
|
107 _this->rng<<=EC_SYM_BITS;
|
cannam@154
|
108 _this->nbits_total+=EC_SYM_BITS;
|
cannam@154
|
109 }
|
cannam@154
|
110 }
|
cannam@154
|
111
|
cannam@154
|
112 void ec_enc_init(ec_enc *_this,unsigned char *_buf,opus_uint32 _size){
|
cannam@154
|
113 _this->buf=_buf;
|
cannam@154
|
114 _this->end_offs=0;
|
cannam@154
|
115 _this->end_window=0;
|
cannam@154
|
116 _this->nend_bits=0;
|
cannam@154
|
117 /*This is the offset from which ec_tell() will subtract partial bits.*/
|
cannam@154
|
118 _this->nbits_total=EC_CODE_BITS+1;
|
cannam@154
|
119 _this->offs=0;
|
cannam@154
|
120 _this->rng=EC_CODE_TOP;
|
cannam@154
|
121 _this->rem=-1;
|
cannam@154
|
122 _this->val=0;
|
cannam@154
|
123 _this->ext=0;
|
cannam@154
|
124 _this->storage=_size;
|
cannam@154
|
125 _this->error=0;
|
cannam@154
|
126 }
|
cannam@154
|
127
|
cannam@154
|
128 void ec_encode(ec_enc *_this,unsigned _fl,unsigned _fh,unsigned _ft){
|
cannam@154
|
129 opus_uint32 r;
|
cannam@154
|
130 r=celt_udiv(_this->rng,_ft);
|
cannam@154
|
131 if(_fl>0){
|
cannam@154
|
132 _this->val+=_this->rng-IMUL32(r,(_ft-_fl));
|
cannam@154
|
133 _this->rng=IMUL32(r,(_fh-_fl));
|
cannam@154
|
134 }
|
cannam@154
|
135 else _this->rng-=IMUL32(r,(_ft-_fh));
|
cannam@154
|
136 ec_enc_normalize(_this);
|
cannam@154
|
137 }
|
cannam@154
|
138
|
cannam@154
|
139 void ec_encode_bin(ec_enc *_this,unsigned _fl,unsigned _fh,unsigned _bits){
|
cannam@154
|
140 opus_uint32 r;
|
cannam@154
|
141 r=_this->rng>>_bits;
|
cannam@154
|
142 if(_fl>0){
|
cannam@154
|
143 _this->val+=_this->rng-IMUL32(r,((1U<<_bits)-_fl));
|
cannam@154
|
144 _this->rng=IMUL32(r,(_fh-_fl));
|
cannam@154
|
145 }
|
cannam@154
|
146 else _this->rng-=IMUL32(r,((1U<<_bits)-_fh));
|
cannam@154
|
147 ec_enc_normalize(_this);
|
cannam@154
|
148 }
|
cannam@154
|
149
|
cannam@154
|
150 /*The probability of having a "one" is 1/(1<<_logp).*/
|
cannam@154
|
151 void ec_enc_bit_logp(ec_enc *_this,int _val,unsigned _logp){
|
cannam@154
|
152 opus_uint32 r;
|
cannam@154
|
153 opus_uint32 s;
|
cannam@154
|
154 opus_uint32 l;
|
cannam@154
|
155 r=_this->rng;
|
cannam@154
|
156 l=_this->val;
|
cannam@154
|
157 s=r>>_logp;
|
cannam@154
|
158 r-=s;
|
cannam@154
|
159 if(_val)_this->val=l+r;
|
cannam@154
|
160 _this->rng=_val?s:r;
|
cannam@154
|
161 ec_enc_normalize(_this);
|
cannam@154
|
162 }
|
cannam@154
|
163
|
cannam@154
|
164 void ec_enc_icdf(ec_enc *_this,int _s,const unsigned char *_icdf,unsigned _ftb){
|
cannam@154
|
165 opus_uint32 r;
|
cannam@154
|
166 r=_this->rng>>_ftb;
|
cannam@154
|
167 if(_s>0){
|
cannam@154
|
168 _this->val+=_this->rng-IMUL32(r,_icdf[_s-1]);
|
cannam@154
|
169 _this->rng=IMUL32(r,_icdf[_s-1]-_icdf[_s]);
|
cannam@154
|
170 }
|
cannam@154
|
171 else _this->rng-=IMUL32(r,_icdf[_s]);
|
cannam@154
|
172 ec_enc_normalize(_this);
|
cannam@154
|
173 }
|
cannam@154
|
174
|
cannam@154
|
175 void ec_enc_uint(ec_enc *_this,opus_uint32 _fl,opus_uint32 _ft){
|
cannam@154
|
176 unsigned ft;
|
cannam@154
|
177 unsigned fl;
|
cannam@154
|
178 int ftb;
|
cannam@154
|
179 /*In order to optimize EC_ILOG(), it is undefined for the value 0.*/
|
cannam@154
|
180 celt_assert(_ft>1);
|
cannam@154
|
181 _ft--;
|
cannam@154
|
182 ftb=EC_ILOG(_ft);
|
cannam@154
|
183 if(ftb>EC_UINT_BITS){
|
cannam@154
|
184 ftb-=EC_UINT_BITS;
|
cannam@154
|
185 ft=(_ft>>ftb)+1;
|
cannam@154
|
186 fl=(unsigned)(_fl>>ftb);
|
cannam@154
|
187 ec_encode(_this,fl,fl+1,ft);
|
cannam@154
|
188 ec_enc_bits(_this,_fl&(((opus_uint32)1<<ftb)-1U),ftb);
|
cannam@154
|
189 }
|
cannam@154
|
190 else ec_encode(_this,_fl,_fl+1,_ft+1);
|
cannam@154
|
191 }
|
cannam@154
|
192
|
cannam@154
|
193 void ec_enc_bits(ec_enc *_this,opus_uint32 _fl,unsigned _bits){
|
cannam@154
|
194 ec_window window;
|
cannam@154
|
195 int used;
|
cannam@154
|
196 window=_this->end_window;
|
cannam@154
|
197 used=_this->nend_bits;
|
cannam@154
|
198 celt_assert(_bits>0);
|
cannam@154
|
199 if(used+_bits>EC_WINDOW_SIZE){
|
cannam@154
|
200 do{
|
cannam@154
|
201 _this->error|=ec_write_byte_at_end(_this,(unsigned)window&EC_SYM_MAX);
|
cannam@154
|
202 window>>=EC_SYM_BITS;
|
cannam@154
|
203 used-=EC_SYM_BITS;
|
cannam@154
|
204 }
|
cannam@154
|
205 while(used>=EC_SYM_BITS);
|
cannam@154
|
206 }
|
cannam@154
|
207 window|=(ec_window)_fl<<used;
|
cannam@154
|
208 used+=_bits;
|
cannam@154
|
209 _this->end_window=window;
|
cannam@154
|
210 _this->nend_bits=used;
|
cannam@154
|
211 _this->nbits_total+=_bits;
|
cannam@154
|
212 }
|
cannam@154
|
213
|
cannam@154
|
214 void ec_enc_patch_initial_bits(ec_enc *_this,unsigned _val,unsigned _nbits){
|
cannam@154
|
215 int shift;
|
cannam@154
|
216 unsigned mask;
|
cannam@154
|
217 celt_assert(_nbits<=EC_SYM_BITS);
|
cannam@154
|
218 shift=EC_SYM_BITS-_nbits;
|
cannam@154
|
219 mask=((1<<_nbits)-1)<<shift;
|
cannam@154
|
220 if(_this->offs>0){
|
cannam@154
|
221 /*The first byte has been finalized.*/
|
cannam@154
|
222 _this->buf[0]=(unsigned char)((_this->buf[0]&~mask)|_val<<shift);
|
cannam@154
|
223 }
|
cannam@154
|
224 else if(_this->rem>=0){
|
cannam@154
|
225 /*The first byte is still awaiting carry propagation.*/
|
cannam@154
|
226 _this->rem=(_this->rem&~mask)|_val<<shift;
|
cannam@154
|
227 }
|
cannam@154
|
228 else if(_this->rng<=(EC_CODE_TOP>>_nbits)){
|
cannam@154
|
229 /*The renormalization loop has never been run.*/
|
cannam@154
|
230 _this->val=(_this->val&~((opus_uint32)mask<<EC_CODE_SHIFT))|
|
cannam@154
|
231 (opus_uint32)_val<<(EC_CODE_SHIFT+shift);
|
cannam@154
|
232 }
|
cannam@154
|
233 /*The encoder hasn't even encoded _nbits of data yet.*/
|
cannam@154
|
234 else _this->error=-1;
|
cannam@154
|
235 }
|
cannam@154
|
236
|
cannam@154
|
237 void ec_enc_shrink(ec_enc *_this,opus_uint32 _size){
|
cannam@154
|
238 celt_assert(_this->offs+_this->end_offs<=_size);
|
cannam@154
|
239 OPUS_MOVE(_this->buf+_size-_this->end_offs,
|
cannam@154
|
240 _this->buf+_this->storage-_this->end_offs,_this->end_offs);
|
cannam@154
|
241 _this->storage=_size;
|
cannam@154
|
242 }
|
cannam@154
|
243
|
cannam@154
|
244 void ec_enc_done(ec_enc *_this){
|
cannam@154
|
245 ec_window window;
|
cannam@154
|
246 int used;
|
cannam@154
|
247 opus_uint32 msk;
|
cannam@154
|
248 opus_uint32 end;
|
cannam@154
|
249 int l;
|
cannam@154
|
250 /*We output the minimum number of bits that ensures that the symbols encoded
|
cannam@154
|
251 thus far will be decoded correctly regardless of the bits that follow.*/
|
cannam@154
|
252 l=EC_CODE_BITS-EC_ILOG(_this->rng);
|
cannam@154
|
253 msk=(EC_CODE_TOP-1)>>l;
|
cannam@154
|
254 end=(_this->val+msk)&~msk;
|
cannam@154
|
255 if((end|msk)>=_this->val+_this->rng){
|
cannam@154
|
256 l++;
|
cannam@154
|
257 msk>>=1;
|
cannam@154
|
258 end=(_this->val+msk)&~msk;
|
cannam@154
|
259 }
|
cannam@154
|
260 while(l>0){
|
cannam@154
|
261 ec_enc_carry_out(_this,(int)(end>>EC_CODE_SHIFT));
|
cannam@154
|
262 end=(end<<EC_SYM_BITS)&(EC_CODE_TOP-1);
|
cannam@154
|
263 l-=EC_SYM_BITS;
|
cannam@154
|
264 }
|
cannam@154
|
265 /*If we have a buffered byte flush it into the output buffer.*/
|
cannam@154
|
266 if(_this->rem>=0||_this->ext>0)ec_enc_carry_out(_this,0);
|
cannam@154
|
267 /*If we have buffered extra bits, flush them as well.*/
|
cannam@154
|
268 window=_this->end_window;
|
cannam@154
|
269 used=_this->nend_bits;
|
cannam@154
|
270 while(used>=EC_SYM_BITS){
|
cannam@154
|
271 _this->error|=ec_write_byte_at_end(_this,(unsigned)window&EC_SYM_MAX);
|
cannam@154
|
272 window>>=EC_SYM_BITS;
|
cannam@154
|
273 used-=EC_SYM_BITS;
|
cannam@154
|
274 }
|
cannam@154
|
275 /*Clear any excess space and add any remaining extra bits to the last byte.*/
|
cannam@154
|
276 if(!_this->error){
|
cannam@154
|
277 OPUS_CLEAR(_this->buf+_this->offs,
|
cannam@154
|
278 _this->storage-_this->offs-_this->end_offs);
|
cannam@154
|
279 if(used>0){
|
cannam@154
|
280 /*If there's no range coder data at all, give up.*/
|
cannam@154
|
281 if(_this->end_offs>=_this->storage)_this->error=-1;
|
cannam@154
|
282 else{
|
cannam@154
|
283 l=-l;
|
cannam@154
|
284 /*If we've busted, don't add too many extra bits to the last byte; it
|
cannam@154
|
285 would corrupt the range coder data, and that's more important.*/
|
cannam@154
|
286 if(_this->offs+_this->end_offs>=_this->storage&&l<used){
|
cannam@154
|
287 window&=(1<<l)-1;
|
cannam@154
|
288 _this->error=-1;
|
cannam@154
|
289 }
|
cannam@154
|
290 _this->buf[_this->storage-_this->end_offs-1]|=(unsigned char)window;
|
cannam@154
|
291 }
|
cannam@154
|
292 }
|
cannam@154
|
293 }
|
cannam@154
|
294 }
|