blob: 0b3433ed8b91a9f785a7bf587dbd948f70d07b96 [file] [log] [blame]
Timothy B. Terriberrya093f4d2011-02-03 14:22:15 -08001/* Copyright (c) 2001-2011 Timothy B. Terriberry
Jean-Marc Valin8b2ff0d2009-10-17 21:40:10 -04002 Copyright (c) 2008-2009 Xiph.Org Foundation */
Gregory Maxwellf40bbf72009-02-03 20:36:57 -05003/*
4 Redistribution and use in source and binary forms, with or without
5 modification, are permitted provided that the following conditions
6 are met:
7
8 - Redistributions of source code must retain the above copyright
9 notice, this list of conditions and the following disclaimer.
10
11 - Redistributions in binary form must reproduce the above copyright
12 notice, this list of conditions and the following disclaimer in the
13 documentation and/or other materials provided with the distribution.
14
Gregory Maxwellf40bbf72009-02-03 20:36:57 -050015 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
Jean-Marc Valincb05e7c2012-04-20 16:40:24 -040018 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
19 OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
Gregory Maxwellf40bbf72009-02-03 20:36:57 -050020 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
23 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
24 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26*/
27
Jean-Marc Valin02fa9132008-02-20 12:09:29 +110028#ifdef HAVE_CONFIG_H
29#include "config.h"
30#endif
31
Timothy B. Terriberry2ec8d9e2007-12-06 15:09:53 +110032#include <stddef.h>
Jean-Marc Valina1bb9c72008-04-28 17:30:26 +100033#include "os_support.h"
Timothy B. Terriberry0268a992008-12-20 22:12:18 -050034#include "arch.h"
Timothy B. Terriberrya093f4d2011-02-03 14:22:15 -080035#include "entdec.h"
36#include "mfrngcod.h"
Timothy B. Terriberry2ec8d9e2007-12-06 15:09:53 +110037
Timothy B. Terriberrya093f4d2011-02-03 14:22:15 -080038/*A range decoder.
39 This is an entropy decoder based upon \cite{Mar79}, which is itself a
40 rediscovery of the FIFO arithmetic code introduced by \cite{Pas76}.
41 It is very similar to arithmetic encoding, except that encoding is done with
42 digits in any base, instead of with bits, and so it is faster when using
43 larger bases (i.e.: a byte).
44 The author claims an average waste of $\frac{1}{2}\log_b(2b)$ bits, where $b$
45 is the base, longer than the theoretical optimum, but to my knowledge there
46 is no published justification for this claim.
47 This only seems true when using near-infinite precision arithmetic so that
48 the process is carried out with no rounding errors.
49
Timothy B. Terriberrya093f4d2011-02-03 14:22:15 -080050 An excellent description of implementation details is available at
51 http://www.arturocampos.com/ac_range.html
52 A recent work \cite{MNW98} which proposes several changes to arithmetic
53 encoding for efficiency actually re-discovers many of the principles
54 behind range encoding, and presents a good theoretical analysis of them.
55
56 End of stream is handled by writing out the smallest number of bits that
57 ensures that the stream will be correctly decoded regardless of the value of
58 any subsequent bits.
59 ec_tell() can be used to determine how many bits were needed to decode
60 all the symbols thus far; other data can be packed in the remaining bits of
61 the input buffer.
62 @PHDTHESIS{Pas76,
63 author="Richard Clark Pasco",
64 title="Source coding algorithms for fast data compression",
65 school="Dept. of Electrical Engineering, Stanford University",
66 address="Stanford, CA",
67 month=May,
68 year=1976
69 }
70 @INPROCEEDINGS{Mar79,
71 author="Martin, G.N.N.",
72 title="Range encoding: an algorithm for removing redundancy from a digitised
73 message",
74 booktitle="Video & Data Recording Conference",
75 year=1979,
76 address="Southampton",
77 month=Jul
78 }
79 @ARTICLE{MNW98,
80 author="Alistair Moffat and Radford Neal and Ian H. Witten",
81 title="Arithmetic Coding Revisited",
82 journal="{ACM} Transactions on Information Systems",
83 year=1998,
84 volume=16,
85 number=3,
86 pages="256--294",
87 month=Jul,
Timothy B. Terriberry0aa8b422013-08-04 22:47:07 -070088 URL="http://www.stanford.edu/class/ee398a/handouts/papers/Moffat98ArithmCoding.pdf"
Timothy B. Terriberrya093f4d2011-02-03 14:22:15 -080089 }*/
90
Timothy B. Terriberrya093f4d2011-02-03 14:22:15 -080091static int ec_read_byte(ec_dec *_this){
92 return _this->offs<_this->storage?_this->buf[_this->offs++]:0;
Timothy B. Terriberry2ec8d9e2007-12-06 15:09:53 +110093}
94
Timothy B. Terriberrya093f4d2011-02-03 14:22:15 -080095static int ec_read_byte_from_end(ec_dec *_this){
96 return _this->end_offs<_this->storage?
97 _this->buf[_this->storage-++(_this->end_offs)]:0;
Jean-Marc Valinc08be442009-06-17 23:23:46 -040098}
99
Timothy B. Terriberrya093f4d2011-02-03 14:22:15 -0800100/*Normalizes the contents of val and rng so that rng lies entirely in the
101 high-order symbol.*/
102static void ec_dec_normalize(ec_dec *_this){
103 /*If the range is too small, rescale it and input some bits.*/
104 while(_this->rng<=EC_CODE_BOT){
105 int sym;
106 _this->nbits_total+=EC_SYM_BITS;
107 _this->rng<<=EC_SYM_BITS;
108 /*Use up the remaining bits from our last symbol.*/
109 sym=_this->rem;
110 /*Read the next value from the input.*/
111 _this->rem=ec_read_byte(_this);
112 /*Take the rest of the bits we need from this new symbol.*/
Gregory Maxwell75d27802011-08-30 14:02:41 -0400113 sym=(sym<<EC_SYM_BITS|_this->rem)>>(EC_SYM_BITS-EC_CODE_EXTRA);
Timothy B. Terriberrya093f4d2011-02-03 14:22:15 -0800114 /*And subtract them from val, capped to be less than EC_CODE_TOP.*/
Gregory Maxwelld6335ab2011-08-30 19:50:41 -0400115 _this->val=((_this->val<<EC_SYM_BITS)+(EC_SYM_MAX&~sym))&(EC_CODE_TOP-1);
Timothy B. Terriberrya093f4d2011-02-03 14:22:15 -0800116 }
117}
118
Jean-Marc Valind77d6a52011-07-29 17:33:06 -0400119void ec_dec_init(ec_dec *_this,unsigned char *_buf,opus_uint32 _storage){
Timothy B. Terriberrya093f4d2011-02-03 14:22:15 -0800120 _this->buf=_buf;
121 _this->storage=_storage;
122 _this->end_offs=0;
123 _this->end_window=0;
124 _this->nend_bits=0;
Timothy B. Terriberry6619a732011-11-29 08:03:03 -0800125 /*This is the offset from which ec_tell() will subtract partial bits.
126 The final value after the ec_dec_normalize() call will be the same as in
127 the encoder, but we have to compensate for the bits that are added there.*/
128 _this->nbits_total=EC_CODE_BITS+1
129 -((EC_CODE_BITS-EC_CODE_EXTRA)/EC_SYM_BITS)*EC_SYM_BITS;
Timothy B. Terriberrya093f4d2011-02-03 14:22:15 -0800130 _this->offs=0;
131 _this->rng=1U<<EC_CODE_EXTRA;
132 _this->rem=ec_read_byte(_this);
Gregory Maxwell75d27802011-08-30 14:02:41 -0400133 _this->val=_this->rng-1-(_this->rem>>(EC_SYM_BITS-EC_CODE_EXTRA));
Timothy B. Terriberrya093f4d2011-02-03 14:22:15 -0800134 _this->error=0;
135 /*Normalize the interval.*/
136 ec_dec_normalize(_this);
Timothy B. Terriberrya093f4d2011-02-03 14:22:15 -0800137}
138
Timothy B. Terriberrya093f4d2011-02-03 14:22:15 -0800139unsigned ec_decode(ec_dec *_this,unsigned _ft){
140 unsigned s;
Jean-Marc Valinec5d01c2014-01-20 16:32:16 -0500141 _this->ext=celt_udiv(_this->rng,_ft);
Timothy B. Terriberrya093f4d2011-02-03 14:22:15 -0800142 s=(unsigned)(_this->val/_this->ext);
143 return _ft-EC_MINI(s+1,_ft);
144}
145
146unsigned ec_decode_bin(ec_dec *_this,unsigned _bits){
147 unsigned s;
148 _this->ext=_this->rng>>_bits;
149 s=(unsigned)(_this->val/_this->ext);
Gregory Maxwell17a29c22011-08-30 18:35:06 -0400150 return (1U<<_bits)-EC_MINI(s+1U,1U<<_bits);
Timothy B. Terriberrya093f4d2011-02-03 14:22:15 -0800151}
152
153void ec_dec_update(ec_dec *_this,unsigned _fl,unsigned _fh,unsigned _ft){
Jean-Marc Valind77d6a52011-07-29 17:33:06 -0400154 opus_uint32 s;
Timothy B. Terriberrya093f4d2011-02-03 14:22:15 -0800155 s=IMUL32(_this->ext,_ft-_fh);
156 _this->val-=s;
157 _this->rng=_fl>0?IMUL32(_this->ext,_fh-_fl):_this->rng-s;
158 ec_dec_normalize(_this);
159}
160
161/*The probability of having a "one" is 1/(1<<_logp).*/
162int ec_dec_bit_logp(ec_dec *_this,unsigned _logp){
Jean-Marc Valind77d6a52011-07-29 17:33:06 -0400163 opus_uint32 r;
164 opus_uint32 d;
165 opus_uint32 s;
Timothy B. Terriberry9bac8c12011-03-02 16:24:32 -0800166 int ret;
Timothy B. Terriberrya093f4d2011-02-03 14:22:15 -0800167 r=_this->rng;
168 d=_this->val;
169 s=r>>_logp;
170 ret=d<s;
171 if(!ret)_this->val=d-s;
172 _this->rng=ret?s:r-s;
173 ec_dec_normalize(_this);
174 return ret;
175}
176
177int ec_dec_icdf(ec_dec *_this,const unsigned char *_icdf,unsigned _ftb){
Jean-Marc Valind77d6a52011-07-29 17:33:06 -0400178 opus_uint32 r;
179 opus_uint32 d;
180 opus_uint32 s;
181 opus_uint32 t;
Timothy B. Terriberry9bac8c12011-03-02 16:24:32 -0800182 int ret;
Timothy B. Terriberrya093f4d2011-02-03 14:22:15 -0800183 s=_this->rng;
184 d=_this->val;
185 r=s>>_ftb;
186 ret=-1;
187 do{
188 t=s;
189 s=IMUL32(r,_icdf[++ret]);
190 }
191 while(d<s);
192 _this->val=d-s;
193 _this->rng=t-s;
194 ec_dec_normalize(_this);
195 return ret;
Timothy B. Terriberry2ec8d9e2007-12-06 15:09:53 +1100196}
197
Jean-Marc Valind77d6a52011-07-29 17:33:06 -0400198opus_uint32 ec_dec_uint(ec_dec *_this,opus_uint32 _ft){
Timothy B. Terriberry30df6cf2010-12-21 08:42:26 -0800199 unsigned ft;
200 unsigned s;
201 int ftb;
Timothy B. Terriberry0268a992008-12-20 22:12:18 -0500202 /*In order to optimize EC_ILOG(), it is undefined for the value 0.*/
203 celt_assert(_ft>1);
Timothy B. Terriberry2ec8d9e2007-12-06 15:09:53 +1100204 _ft--;
205 ftb=EC_ILOG(_ft);
Timothy B. Terriberry30df6cf2010-12-21 08:42:26 -0800206 if(ftb>EC_UINT_BITS){
Jean-Marc Valind77d6a52011-07-29 17:33:06 -0400207 opus_uint32 t;
Timothy B. Terriberry30df6cf2010-12-21 08:42:26 -0800208 ftb-=EC_UINT_BITS;
Timothy B. Terriberryf13fea72007-12-11 13:25:57 +1100209 ft=(unsigned)(_ft>>ftb)+1;
Timothy B. Terriberry2ec8d9e2007-12-06 15:09:53 +1100210 s=ec_decode(_this,ft);
211 ec_dec_update(_this,s,s+1,ft);
Jean-Marc Valind77d6a52011-07-29 17:33:06 -0400212 t=(opus_uint32)s<<ftb|ec_dec_bits(_this,ftb);
Timothy B. Terriberry30df6cf2010-12-21 08:42:26 -0800213 if(t<=_ft)return t;
214 _this->error=1;
215 return _ft;
216 }
217 else{
Jean-Marc Valindc767f62008-03-22 22:23:58 +1100218 _ft++;
219 s=ec_decode(_this,(unsigned)_ft);
220 ec_dec_update(_this,s,s+1,(unsigned)_ft);
Jean-Marc Valine0aa9d12010-10-17 16:25:56 -0400221 return s;
Timothy B. Terriberry2ec8d9e2007-12-06 15:09:53 +1100222 }
Timothy B. Terriberryf13fea72007-12-11 13:25:57 +1100223}
Jean-Marc Valinb1e017f2010-07-18 21:20:35 -0400224
Jean-Marc Valind77d6a52011-07-29 17:33:06 -0400225opus_uint32 ec_dec_bits(ec_dec *_this,unsigned _bits){
Timothy B. Terriberry9bac8c12011-03-02 16:24:32 -0800226 ec_window window;
227 int available;
Jean-Marc Valind77d6a52011-07-29 17:33:06 -0400228 opus_uint32 ret;
Timothy B. Terriberrya093f4d2011-02-03 14:22:15 -0800229 window=_this->end_window;
230 available=_this->nend_bits;
Gregory Maxwell17a29c22011-08-30 18:35:06 -0400231 if((unsigned)available<_bits){
Timothy B. Terriberrya093f4d2011-02-03 14:22:15 -0800232 do{
233 window|=(ec_window)ec_read_byte_from_end(_this)<<available;
234 available+=EC_SYM_BITS;
235 }
236 while(available<=EC_WINDOW_SIZE-EC_SYM_BITS);
237 }
Gregory Maxwelld6335ab2011-08-30 19:50:41 -0400238 ret=(opus_uint32)window&(((opus_uint32)1<<_bits)-1U);
Timothy B. Terriberrya093f4d2011-02-03 14:22:15 -0800239 window>>=_bits;
240 available-=_bits;
241 _this->end_window=window;
242 _this->nend_bits=available;
243 _this->nbits_total+=_bits;
244 return ret;
Jean-Marc Valinb1e017f2010-07-18 21:20:35 -0400245}