annotate src/opus-1.3/celt/entdec.c @ 69:7aeed7906520

Add Opus sources and macOS builds
author Chris Cannam
date Wed, 23 Jan 2019 13:48:08 +0000
parents
children
rev   line source
Chris@69 1 /* Copyright (c) 2001-2011 Timothy B. Terriberry
Chris@69 2 Copyright (c) 2008-2009 Xiph.Org Foundation */
Chris@69 3 /*
Chris@69 4 Redistribution and use in source and binary forms, with or without
Chris@69 5 modification, are permitted provided that the following conditions
Chris@69 6 are met:
Chris@69 7
Chris@69 8 - Redistributions of source code must retain the above copyright
Chris@69 9 notice, this list of conditions and the following disclaimer.
Chris@69 10
Chris@69 11 - Redistributions in binary form must reproduce the above copyright
Chris@69 12 notice, this list of conditions and the following disclaimer in the
Chris@69 13 documentation and/or other materials provided with the distribution.
Chris@69 14
Chris@69 15 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
Chris@69 16 ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
Chris@69 17 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
Chris@69 18 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
Chris@69 19 OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
Chris@69 20 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
Chris@69 21 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
Chris@69 22 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
Chris@69 23 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
Chris@69 24 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
Chris@69 25 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Chris@69 26 */
Chris@69 27
Chris@69 28 #ifdef HAVE_CONFIG_H
Chris@69 29 #include "config.h"
Chris@69 30 #endif
Chris@69 31
Chris@69 32 #include <stddef.h>
Chris@69 33 #include "os_support.h"
Chris@69 34 #include "arch.h"
Chris@69 35 #include "entdec.h"
Chris@69 36 #include "mfrngcod.h"
Chris@69 37
Chris@69 38 /*A range decoder.
Chris@69 39 This is an entropy decoder based upon \cite{Mar79}, which is itself a
Chris@69 40 rediscovery of the FIFO arithmetic code introduced by \cite{Pas76}.
Chris@69 41 It is very similar to arithmetic encoding, except that encoding is done with
Chris@69 42 digits in any base, instead of with bits, and so it is faster when using
Chris@69 43 larger bases (i.e.: a byte).
Chris@69 44 The author claims an average waste of $\frac{1}{2}\log_b(2b)$ bits, where $b$
Chris@69 45 is the base, longer than the theoretical optimum, but to my knowledge there
Chris@69 46 is no published justification for this claim.
Chris@69 47 This only seems true when using near-infinite precision arithmetic so that
Chris@69 48 the process is carried out with no rounding errors.
Chris@69 49
Chris@69 50 An excellent description of implementation details is available at
Chris@69 51 http://www.arturocampos.com/ac_range.html
Chris@69 52 A recent work \cite{MNW98} which proposes several changes to arithmetic
Chris@69 53 encoding for efficiency actually re-discovers many of the principles
Chris@69 54 behind range encoding, and presents a good theoretical analysis of them.
Chris@69 55
Chris@69 56 End of stream is handled by writing out the smallest number of bits that
Chris@69 57 ensures that the stream will be correctly decoded regardless of the value of
Chris@69 58 any subsequent bits.
Chris@69 59 ec_tell() can be used to determine how many bits were needed to decode
Chris@69 60 all the symbols thus far; other data can be packed in the remaining bits of
Chris@69 61 the input buffer.
Chris@69 62 @PHDTHESIS{Pas76,
Chris@69 63 author="Richard Clark Pasco",
Chris@69 64 title="Source coding algorithms for fast data compression",
Chris@69 65 school="Dept. of Electrical Engineering, Stanford University",
Chris@69 66 address="Stanford, CA",
Chris@69 67 month=May,
Chris@69 68 year=1976
Chris@69 69 }
Chris@69 70 @INPROCEEDINGS{Mar79,
Chris@69 71 author="Martin, G.N.N.",
Chris@69 72 title="Range encoding: an algorithm for removing redundancy from a digitised
Chris@69 73 message",
Chris@69 74 booktitle="Video & Data Recording Conference",
Chris@69 75 year=1979,
Chris@69 76 address="Southampton",
Chris@69 77 month=Jul
Chris@69 78 }
Chris@69 79 @ARTICLE{MNW98,
Chris@69 80 author="Alistair Moffat and Radford Neal and Ian H. Witten",
Chris@69 81 title="Arithmetic Coding Revisited",
Chris@69 82 journal="{ACM} Transactions on Information Systems",
Chris@69 83 year=1998,
Chris@69 84 volume=16,
Chris@69 85 number=3,
Chris@69 86 pages="256--294",
Chris@69 87 month=Jul,
Chris@69 88 URL="http://www.stanford.edu/class/ee398a/handouts/papers/Moffat98ArithmCoding.pdf"
Chris@69 89 }*/
Chris@69 90
Chris@69 91 static int ec_read_byte(ec_dec *_this){
Chris@69 92 return _this->offs<_this->storage?_this->buf[_this->offs++]:0;
Chris@69 93 }
Chris@69 94
Chris@69 95 static int ec_read_byte_from_end(ec_dec *_this){
Chris@69 96 return _this->end_offs<_this->storage?
Chris@69 97 _this->buf[_this->storage-++(_this->end_offs)]:0;
Chris@69 98 }
Chris@69 99
Chris@69 100 /*Normalizes the contents of val and rng so that rng lies entirely in the
Chris@69 101 high-order symbol.*/
Chris@69 102 static void ec_dec_normalize(ec_dec *_this){
Chris@69 103 /*If the range is too small, rescale it and input some bits.*/
Chris@69 104 while(_this->rng<=EC_CODE_BOT){
Chris@69 105 int sym;
Chris@69 106 _this->nbits_total+=EC_SYM_BITS;
Chris@69 107 _this->rng<<=EC_SYM_BITS;
Chris@69 108 /*Use up the remaining bits from our last symbol.*/
Chris@69 109 sym=_this->rem;
Chris@69 110 /*Read the next value from the input.*/
Chris@69 111 _this->rem=ec_read_byte(_this);
Chris@69 112 /*Take the rest of the bits we need from this new symbol.*/
Chris@69 113 sym=(sym<<EC_SYM_BITS|_this->rem)>>(EC_SYM_BITS-EC_CODE_EXTRA);
Chris@69 114 /*And subtract them from val, capped to be less than EC_CODE_TOP.*/
Chris@69 115 _this->val=((_this->val<<EC_SYM_BITS)+(EC_SYM_MAX&~sym))&(EC_CODE_TOP-1);
Chris@69 116 }
Chris@69 117 }
Chris@69 118
Chris@69 119 void ec_dec_init(ec_dec *_this,unsigned char *_buf,opus_uint32 _storage){
Chris@69 120 _this->buf=_buf;
Chris@69 121 _this->storage=_storage;
Chris@69 122 _this->end_offs=0;
Chris@69 123 _this->end_window=0;
Chris@69 124 _this->nend_bits=0;
Chris@69 125 /*This is the offset from which ec_tell() will subtract partial bits.
Chris@69 126 The final value after the ec_dec_normalize() call will be the same as in
Chris@69 127 the encoder, but we have to compensate for the bits that are added there.*/
Chris@69 128 _this->nbits_total=EC_CODE_BITS+1
Chris@69 129 -((EC_CODE_BITS-EC_CODE_EXTRA)/EC_SYM_BITS)*EC_SYM_BITS;
Chris@69 130 _this->offs=0;
Chris@69 131 _this->rng=1U<<EC_CODE_EXTRA;
Chris@69 132 _this->rem=ec_read_byte(_this);
Chris@69 133 _this->val=_this->rng-1-(_this->rem>>(EC_SYM_BITS-EC_CODE_EXTRA));
Chris@69 134 _this->error=0;
Chris@69 135 /*Normalize the interval.*/
Chris@69 136 ec_dec_normalize(_this);
Chris@69 137 }
Chris@69 138
Chris@69 139 unsigned ec_decode(ec_dec *_this,unsigned _ft){
Chris@69 140 unsigned s;
Chris@69 141 _this->ext=celt_udiv(_this->rng,_ft);
Chris@69 142 s=(unsigned)(_this->val/_this->ext);
Chris@69 143 return _ft-EC_MINI(s+1,_ft);
Chris@69 144 }
Chris@69 145
Chris@69 146 unsigned ec_decode_bin(ec_dec *_this,unsigned _bits){
Chris@69 147 unsigned s;
Chris@69 148 _this->ext=_this->rng>>_bits;
Chris@69 149 s=(unsigned)(_this->val/_this->ext);
Chris@69 150 return (1U<<_bits)-EC_MINI(s+1U,1U<<_bits);
Chris@69 151 }
Chris@69 152
Chris@69 153 void ec_dec_update(ec_dec *_this,unsigned _fl,unsigned _fh,unsigned _ft){
Chris@69 154 opus_uint32 s;
Chris@69 155 s=IMUL32(_this->ext,_ft-_fh);
Chris@69 156 _this->val-=s;
Chris@69 157 _this->rng=_fl>0?IMUL32(_this->ext,_fh-_fl):_this->rng-s;
Chris@69 158 ec_dec_normalize(_this);
Chris@69 159 }
Chris@69 160
Chris@69 161 /*The probability of having a "one" is 1/(1<<_logp).*/
Chris@69 162 int ec_dec_bit_logp(ec_dec *_this,unsigned _logp){
Chris@69 163 opus_uint32 r;
Chris@69 164 opus_uint32 d;
Chris@69 165 opus_uint32 s;
Chris@69 166 int ret;
Chris@69 167 r=_this->rng;
Chris@69 168 d=_this->val;
Chris@69 169 s=r>>_logp;
Chris@69 170 ret=d<s;
Chris@69 171 if(!ret)_this->val=d-s;
Chris@69 172 _this->rng=ret?s:r-s;
Chris@69 173 ec_dec_normalize(_this);
Chris@69 174 return ret;
Chris@69 175 }
Chris@69 176
Chris@69 177 int ec_dec_icdf(ec_dec *_this,const unsigned char *_icdf,unsigned _ftb){
Chris@69 178 opus_uint32 r;
Chris@69 179 opus_uint32 d;
Chris@69 180 opus_uint32 s;
Chris@69 181 opus_uint32 t;
Chris@69 182 int ret;
Chris@69 183 s=_this->rng;
Chris@69 184 d=_this->val;
Chris@69 185 r=s>>_ftb;
Chris@69 186 ret=-1;
Chris@69 187 do{
Chris@69 188 t=s;
Chris@69 189 s=IMUL32(r,_icdf[++ret]);
Chris@69 190 }
Chris@69 191 while(d<s);
Chris@69 192 _this->val=d-s;
Chris@69 193 _this->rng=t-s;
Chris@69 194 ec_dec_normalize(_this);
Chris@69 195 return ret;
Chris@69 196 }
Chris@69 197
Chris@69 198 opus_uint32 ec_dec_uint(ec_dec *_this,opus_uint32 _ft){
Chris@69 199 unsigned ft;
Chris@69 200 unsigned s;
Chris@69 201 int ftb;
Chris@69 202 /*In order to optimize EC_ILOG(), it is undefined for the value 0.*/
Chris@69 203 celt_assert(_ft>1);
Chris@69 204 _ft--;
Chris@69 205 ftb=EC_ILOG(_ft);
Chris@69 206 if(ftb>EC_UINT_BITS){
Chris@69 207 opus_uint32 t;
Chris@69 208 ftb-=EC_UINT_BITS;
Chris@69 209 ft=(unsigned)(_ft>>ftb)+1;
Chris@69 210 s=ec_decode(_this,ft);
Chris@69 211 ec_dec_update(_this,s,s+1,ft);
Chris@69 212 t=(opus_uint32)s<<ftb|ec_dec_bits(_this,ftb);
Chris@69 213 if(t<=_ft)return t;
Chris@69 214 _this->error=1;
Chris@69 215 return _ft;
Chris@69 216 }
Chris@69 217 else{
Chris@69 218 _ft++;
Chris@69 219 s=ec_decode(_this,(unsigned)_ft);
Chris@69 220 ec_dec_update(_this,s,s+1,(unsigned)_ft);
Chris@69 221 return s;
Chris@69 222 }
Chris@69 223 }
Chris@69 224
Chris@69 225 opus_uint32 ec_dec_bits(ec_dec *_this,unsigned _bits){
Chris@69 226 ec_window window;
Chris@69 227 int available;
Chris@69 228 opus_uint32 ret;
Chris@69 229 window=_this->end_window;
Chris@69 230 available=_this->nend_bits;
Chris@69 231 if((unsigned)available<_bits){
Chris@69 232 do{
Chris@69 233 window|=(ec_window)ec_read_byte_from_end(_this)<<available;
Chris@69 234 available+=EC_SYM_BITS;
Chris@69 235 }
Chris@69 236 while(available<=EC_WINDOW_SIZE-EC_SYM_BITS);
Chris@69 237 }
Chris@69 238 ret=(opus_uint32)window&(((opus_uint32)1<<_bits)-1U);
Chris@69 239 window>>=_bits;
Chris@69 240 available-=_bits;
Chris@69 241 _this->end_window=window;
Chris@69 242 _this->nend_bits=available;
Chris@69 243 _this->nbits_total+=_bits;
Chris@69 244 return ret;
Chris@69 245 }