michael@0: /* Copyright (c) 2001-2011 Timothy B. Terriberry michael@0: Copyright (c) 2008-2009 Xiph.Org Foundation */ michael@0: /* michael@0: Redistribution and use in source and binary forms, with or without michael@0: modification, are permitted provided that the following conditions michael@0: are met: michael@0: michael@0: - Redistributions of source code must retain the above copyright michael@0: notice, this list of conditions and the following disclaimer. michael@0: michael@0: - Redistributions in binary form must reproduce the above copyright michael@0: notice, this list of conditions and the following disclaimer in the michael@0: documentation and/or other materials provided with the distribution. michael@0: michael@0: THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS michael@0: ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT michael@0: LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR michael@0: A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER michael@0: OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, michael@0: EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, michael@0: PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR michael@0: PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF michael@0: LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING michael@0: NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS michael@0: SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. michael@0: */ michael@0: michael@0: #ifdef HAVE_CONFIG_H michael@0: #include "config.h" michael@0: #endif michael@0: michael@0: #include michael@0: #include "os_support.h" michael@0: #include "arch.h" michael@0: #include "entdec.h" michael@0: #include "mfrngcod.h" michael@0: michael@0: /*A range decoder. michael@0: This is an entropy decoder based upon \cite{Mar79}, which is itself a michael@0: rediscovery of the FIFO arithmetic code introduced by \cite{Pas76}. michael@0: It is very similar to arithmetic encoding, except that encoding is done with michael@0: digits in any base, instead of with bits, and so it is faster when using michael@0: larger bases (i.e.: a byte). michael@0: The author claims an average waste of $\frac{1}{2}\log_b(2b)$ bits, where $b$ michael@0: is the base, longer than the theoretical optimum, but to my knowledge there michael@0: is no published justification for this claim. michael@0: This only seems true when using near-infinite precision arithmetic so that michael@0: the process is carried out with no rounding errors. michael@0: michael@0: An excellent description of implementation details is available at michael@0: http://www.arturocampos.com/ac_range.html michael@0: A recent work \cite{MNW98} which proposes several changes to arithmetic michael@0: encoding for efficiency actually re-discovers many of the principles michael@0: behind range encoding, and presents a good theoretical analysis of them. michael@0: michael@0: End of stream is handled by writing out the smallest number of bits that michael@0: ensures that the stream will be correctly decoded regardless of the value of michael@0: any subsequent bits. michael@0: ec_tell() can be used to determine how many bits were needed to decode michael@0: all the symbols thus far; other data can be packed in the remaining bits of michael@0: the input buffer. michael@0: @PHDTHESIS{Pas76, michael@0: author="Richard Clark Pasco", michael@0: title="Source coding algorithms for fast data compression", michael@0: school="Dept. of Electrical Engineering, Stanford University", michael@0: address="Stanford, CA", michael@0: month=May, michael@0: year=1976 michael@0: } michael@0: @INPROCEEDINGS{Mar79, michael@0: author="Martin, G.N.N.", michael@0: title="Range encoding: an algorithm for removing redundancy from a digitised michael@0: message", michael@0: booktitle="Video & Data Recording Conference", michael@0: year=1979, michael@0: address="Southampton", michael@0: month=Jul michael@0: } michael@0: @ARTICLE{MNW98, michael@0: author="Alistair Moffat and Radford Neal and Ian H. Witten", michael@0: title="Arithmetic Coding Revisited", michael@0: journal="{ACM} Transactions on Information Systems", michael@0: year=1998, michael@0: volume=16, michael@0: number=3, michael@0: pages="256--294", michael@0: month=Jul, michael@0: URL="http://www.stanford.edu/class/ee398a/handouts/papers/Moffat98ArithmCoding.pdf" michael@0: }*/ michael@0: michael@0: static int ec_read_byte(ec_dec *_this){ michael@0: return _this->offs<_this->storage?_this->buf[_this->offs++]:0; michael@0: } michael@0: michael@0: static int ec_read_byte_from_end(ec_dec *_this){ michael@0: return _this->end_offs<_this->storage? michael@0: _this->buf[_this->storage-++(_this->end_offs)]:0; michael@0: } michael@0: michael@0: /*Normalizes the contents of val and rng so that rng lies entirely in the michael@0: high-order symbol.*/ michael@0: static void ec_dec_normalize(ec_dec *_this){ michael@0: /*If the range is too small, rescale it and input some bits.*/ michael@0: while(_this->rng<=EC_CODE_BOT){ michael@0: int sym; michael@0: _this->nbits_total+=EC_SYM_BITS; michael@0: _this->rng<<=EC_SYM_BITS; michael@0: /*Use up the remaining bits from our last symbol.*/ michael@0: sym=_this->rem; michael@0: /*Read the next value from the input.*/ michael@0: _this->rem=ec_read_byte(_this); michael@0: /*Take the rest of the bits we need from this new symbol.*/ michael@0: sym=(sym<rem)>>(EC_SYM_BITS-EC_CODE_EXTRA); michael@0: /*And subtract them from val, capped to be less than EC_CODE_TOP.*/ michael@0: _this->val=((_this->val<buf=_buf; michael@0: _this->storage=_storage; michael@0: _this->end_offs=0; michael@0: _this->end_window=0; michael@0: _this->nend_bits=0; michael@0: /*This is the offset from which ec_tell() will subtract partial bits. michael@0: The final value after the ec_dec_normalize() call will be the same as in michael@0: the encoder, but we have to compensate for the bits that are added there.*/ michael@0: _this->nbits_total=EC_CODE_BITS+1 michael@0: -((EC_CODE_BITS-EC_CODE_EXTRA)/EC_SYM_BITS)*EC_SYM_BITS; michael@0: _this->offs=0; michael@0: _this->rng=1U<rem=ec_read_byte(_this); michael@0: _this->val=_this->rng-1-(_this->rem>>(EC_SYM_BITS-EC_CODE_EXTRA)); michael@0: _this->error=0; michael@0: /*Normalize the interval.*/ michael@0: ec_dec_normalize(_this); michael@0: } michael@0: michael@0: unsigned ec_decode(ec_dec *_this,unsigned _ft){ michael@0: unsigned s; michael@0: _this->ext=_this->rng/_ft; michael@0: s=(unsigned)(_this->val/_this->ext); michael@0: return _ft-EC_MINI(s+1,_ft); michael@0: } michael@0: michael@0: unsigned ec_decode_bin(ec_dec *_this,unsigned _bits){ michael@0: unsigned s; michael@0: _this->ext=_this->rng>>_bits; michael@0: s=(unsigned)(_this->val/_this->ext); michael@0: return (1U<<_bits)-EC_MINI(s+1U,1U<<_bits); michael@0: } michael@0: michael@0: void ec_dec_update(ec_dec *_this,unsigned _fl,unsigned _fh,unsigned _ft){ michael@0: opus_uint32 s; michael@0: s=IMUL32(_this->ext,_ft-_fh); michael@0: _this->val-=s; michael@0: _this->rng=_fl>0?IMUL32(_this->ext,_fh-_fl):_this->rng-s; michael@0: ec_dec_normalize(_this); michael@0: } michael@0: michael@0: /*The probability of having a "one" is 1/(1<<_logp).*/ michael@0: int ec_dec_bit_logp(ec_dec *_this,unsigned _logp){ michael@0: opus_uint32 r; michael@0: opus_uint32 d; michael@0: opus_uint32 s; michael@0: int ret; michael@0: r=_this->rng; michael@0: d=_this->val; michael@0: s=r>>_logp; michael@0: ret=dval=d-s; michael@0: _this->rng=ret?s:r-s; michael@0: ec_dec_normalize(_this); michael@0: return ret; michael@0: } michael@0: michael@0: int ec_dec_icdf(ec_dec *_this,const unsigned char *_icdf,unsigned _ftb){ michael@0: opus_uint32 r; michael@0: opus_uint32 d; michael@0: opus_uint32 s; michael@0: opus_uint32 t; michael@0: int ret; michael@0: s=_this->rng; michael@0: d=_this->val; michael@0: r=s>>_ftb; michael@0: ret=-1; michael@0: do{ michael@0: t=s; michael@0: s=IMUL32(r,_icdf[++ret]); michael@0: } michael@0: while(dval=d-s; michael@0: _this->rng=t-s; michael@0: ec_dec_normalize(_this); michael@0: return ret; michael@0: } michael@0: michael@0: opus_uint32 ec_dec_uint(ec_dec *_this,opus_uint32 _ft){ michael@0: unsigned ft; michael@0: unsigned s; michael@0: int ftb; michael@0: /*In order to optimize EC_ILOG(), it is undefined for the value 0.*/ michael@0: celt_assert(_ft>1); michael@0: _ft--; michael@0: ftb=EC_ILOG(_ft); michael@0: if(ftb>EC_UINT_BITS){ michael@0: opus_uint32 t; michael@0: ftb-=EC_UINT_BITS; michael@0: ft=(unsigned)(_ft>>ftb)+1; michael@0: s=ec_decode(_this,ft); michael@0: ec_dec_update(_this,s,s+1,ft); michael@0: t=(opus_uint32)s<error=1; michael@0: return _ft; michael@0: } michael@0: else{ michael@0: _ft++; michael@0: s=ec_decode(_this,(unsigned)_ft); michael@0: ec_dec_update(_this,s,s+1,(unsigned)_ft); michael@0: return s; michael@0: } michael@0: } michael@0: michael@0: opus_uint32 ec_dec_bits(ec_dec *_this,unsigned _bits){ michael@0: ec_window window; michael@0: int available; michael@0: opus_uint32 ret; michael@0: window=_this->end_window; michael@0: available=_this->nend_bits; michael@0: if((unsigned)available<_bits){ michael@0: do{ michael@0: window|=(ec_window)ec_read_byte_from_end(_this)<>=_bits; michael@0: available-=_bits; michael@0: _this->end_window=window; michael@0: _this->nend_bits=available; michael@0: _this->nbits_total+=_bits; michael@0: return ret; michael@0: }