annotate src/opus-1.3/celt/entdec.c @ 169:223a55898ab9 tip default

Add null config files
author Chris Cannam <cannam@all-day-breakfast.com>
date Mon, 02 Mar 2020 14:03:47 +0000
parents 4664ac0c1032
children
rev   line source
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 #ifdef HAVE_CONFIG_H
cannam@154 29 #include "config.h"
cannam@154 30 #endif
cannam@154 31
cannam@154 32 #include <stddef.h>
cannam@154 33 #include "os_support.h"
cannam@154 34 #include "arch.h"
cannam@154 35 #include "entdec.h"
cannam@154 36 #include "mfrngcod.h"
cannam@154 37
cannam@154 38 /*A range decoder.
cannam@154 39 This is an entropy decoder based upon \cite{Mar79}, which is itself a
cannam@154 40 rediscovery of the FIFO arithmetic code introduced by \cite{Pas76}.
cannam@154 41 It is very similar to arithmetic encoding, except that encoding is done with
cannam@154 42 digits in any base, instead of with bits, and so it is faster when using
cannam@154 43 larger bases (i.e.: a byte).
cannam@154 44 The author claims an average waste of $\frac{1}{2}\log_b(2b)$ bits, where $b$
cannam@154 45 is the base, longer than the theoretical optimum, but to my knowledge there
cannam@154 46 is no published justification for this claim.
cannam@154 47 This only seems true when using near-infinite precision arithmetic so that
cannam@154 48 the process is carried out with no rounding errors.
cannam@154 49
cannam@154 50 An excellent description of implementation details is available at
cannam@154 51 http://www.arturocampos.com/ac_range.html
cannam@154 52 A recent work \cite{MNW98} which proposes several changes to arithmetic
cannam@154 53 encoding for efficiency actually re-discovers many of the principles
cannam@154 54 behind range encoding, and presents a good theoretical analysis of them.
cannam@154 55
cannam@154 56 End of stream is handled by writing out the smallest number of bits that
cannam@154 57 ensures that the stream will be correctly decoded regardless of the value of
cannam@154 58 any subsequent bits.
cannam@154 59 ec_tell() can be used to determine how many bits were needed to decode
cannam@154 60 all the symbols thus far; other data can be packed in the remaining bits of
cannam@154 61 the input buffer.
cannam@154 62 @PHDTHESIS{Pas76,
cannam@154 63 author="Richard Clark Pasco",
cannam@154 64 title="Source coding algorithms for fast data compression",
cannam@154 65 school="Dept. of Electrical Engineering, Stanford University",
cannam@154 66 address="Stanford, CA",
cannam@154 67 month=May,
cannam@154 68 year=1976
cannam@154 69 }
cannam@154 70 @INPROCEEDINGS{Mar79,
cannam@154 71 author="Martin, G.N.N.",
cannam@154 72 title="Range encoding: an algorithm for removing redundancy from a digitised
cannam@154 73 message",
cannam@154 74 booktitle="Video & Data Recording Conference",
cannam@154 75 year=1979,
cannam@154 76 address="Southampton",
cannam@154 77 month=Jul
cannam@154 78 }
cannam@154 79 @ARTICLE{MNW98,
cannam@154 80 author="Alistair Moffat and Radford Neal and Ian H. Witten",
cannam@154 81 title="Arithmetic Coding Revisited",
cannam@154 82 journal="{ACM} Transactions on Information Systems",
cannam@154 83 year=1998,
cannam@154 84 volume=16,
cannam@154 85 number=3,
cannam@154 86 pages="256--294",
cannam@154 87 month=Jul,
cannam@154 88 URL="http://www.stanford.edu/class/ee398a/handouts/papers/Moffat98ArithmCoding.pdf"
cannam@154 89 }*/
cannam@154 90
cannam@154 91 static int ec_read_byte(ec_dec *_this){
cannam@154 92 return _this->offs<_this->storage?_this->buf[_this->offs++]:0;
cannam@154 93 }
cannam@154 94
cannam@154 95 static int ec_read_byte_from_end(ec_dec *_this){
cannam@154 96 return _this->end_offs<_this->storage?
cannam@154 97 _this->buf[_this->storage-++(_this->end_offs)]:0;
cannam@154 98 }
cannam@154 99
cannam@154 100 /*Normalizes the contents of val and rng so that rng lies entirely in the
cannam@154 101 high-order symbol.*/
cannam@154 102 static void ec_dec_normalize(ec_dec *_this){
cannam@154 103 /*If the range is too small, rescale it and input some bits.*/
cannam@154 104 while(_this->rng<=EC_CODE_BOT){
cannam@154 105 int sym;
cannam@154 106 _this->nbits_total+=EC_SYM_BITS;
cannam@154 107 _this->rng<<=EC_SYM_BITS;
cannam@154 108 /*Use up the remaining bits from our last symbol.*/
cannam@154 109 sym=_this->rem;
cannam@154 110 /*Read the next value from the input.*/
cannam@154 111 _this->rem=ec_read_byte(_this);
cannam@154 112 /*Take the rest of the bits we need from this new symbol.*/
cannam@154 113 sym=(sym<<EC_SYM_BITS|_this->rem)>>(EC_SYM_BITS-EC_CODE_EXTRA);
cannam@154 114 /*And subtract them from val, capped to be less than EC_CODE_TOP.*/
cannam@154 115 _this->val=((_this->val<<EC_SYM_BITS)+(EC_SYM_MAX&~sym))&(EC_CODE_TOP-1);
cannam@154 116 }
cannam@154 117 }
cannam@154 118
cannam@154 119 void ec_dec_init(ec_dec *_this,unsigned char *_buf,opus_uint32 _storage){
cannam@154 120 _this->buf=_buf;
cannam@154 121 _this->storage=_storage;
cannam@154 122 _this->end_offs=0;
cannam@154 123 _this->end_window=0;
cannam@154 124 _this->nend_bits=0;
cannam@154 125 /*This is the offset from which ec_tell() will subtract partial bits.
cannam@154 126 The final value after the ec_dec_normalize() call will be the same as in
cannam@154 127 the encoder, but we have to compensate for the bits that are added there.*/
cannam@154 128 _this->nbits_total=EC_CODE_BITS+1
cannam@154 129 -((EC_CODE_BITS-EC_CODE_EXTRA)/EC_SYM_BITS)*EC_SYM_BITS;
cannam@154 130 _this->offs=0;
cannam@154 131 _this->rng=1U<<EC_CODE_EXTRA;
cannam@154 132 _this->rem=ec_read_byte(_this);
cannam@154 133 _this->val=_this->rng-1-(_this->rem>>(EC_SYM_BITS-EC_CODE_EXTRA));
cannam@154 134 _this->error=0;
cannam@154 135 /*Normalize the interval.*/
cannam@154 136 ec_dec_normalize(_this);
cannam@154 137 }
cannam@154 138
cannam@154 139 unsigned ec_decode(ec_dec *_this,unsigned _ft){
cannam@154 140 unsigned s;
cannam@154 141 _this->ext=celt_udiv(_this->rng,_ft);
cannam@154 142 s=(unsigned)(_this->val/_this->ext);
cannam@154 143 return _ft-EC_MINI(s+1,_ft);
cannam@154 144 }
cannam@154 145
cannam@154 146 unsigned ec_decode_bin(ec_dec *_this,unsigned _bits){
cannam@154 147 unsigned s;
cannam@154 148 _this->ext=_this->rng>>_bits;
cannam@154 149 s=(unsigned)(_this->val/_this->ext);
cannam@154 150 return (1U<<_bits)-EC_MINI(s+1U,1U<<_bits);
cannam@154 151 }
cannam@154 152
cannam@154 153 void ec_dec_update(ec_dec *_this,unsigned _fl,unsigned _fh,unsigned _ft){
cannam@154 154 opus_uint32 s;
cannam@154 155 s=IMUL32(_this->ext,_ft-_fh);
cannam@154 156 _this->val-=s;
cannam@154 157 _this->rng=_fl>0?IMUL32(_this->ext,_fh-_fl):_this->rng-s;
cannam@154 158 ec_dec_normalize(_this);
cannam@154 159 }
cannam@154 160
cannam@154 161 /*The probability of having a "one" is 1/(1<<_logp).*/
cannam@154 162 int ec_dec_bit_logp(ec_dec *_this,unsigned _logp){
cannam@154 163 opus_uint32 r;
cannam@154 164 opus_uint32 d;
cannam@154 165 opus_uint32 s;
cannam@154 166 int ret;
cannam@154 167 r=_this->rng;
cannam@154 168 d=_this->val;
cannam@154 169 s=r>>_logp;
cannam@154 170 ret=d<s;
cannam@154 171 if(!ret)_this->val=d-s;
cannam@154 172 _this->rng=ret?s:r-s;
cannam@154 173 ec_dec_normalize(_this);
cannam@154 174 return ret;
cannam@154 175 }
cannam@154 176
cannam@154 177 int ec_dec_icdf(ec_dec *_this,const unsigned char *_icdf,unsigned _ftb){
cannam@154 178 opus_uint32 r;
cannam@154 179 opus_uint32 d;
cannam@154 180 opus_uint32 s;
cannam@154 181 opus_uint32 t;
cannam@154 182 int ret;
cannam@154 183 s=_this->rng;
cannam@154 184 d=_this->val;
cannam@154 185 r=s>>_ftb;
cannam@154 186 ret=-1;
cannam@154 187 do{
cannam@154 188 t=s;
cannam@154 189 s=IMUL32(r,_icdf[++ret]);
cannam@154 190 }
cannam@154 191 while(d<s);
cannam@154 192 _this->val=d-s;
cannam@154 193 _this->rng=t-s;
cannam@154 194 ec_dec_normalize(_this);
cannam@154 195 return ret;
cannam@154 196 }
cannam@154 197
cannam@154 198 opus_uint32 ec_dec_uint(ec_dec *_this,opus_uint32 _ft){
cannam@154 199 unsigned ft;
cannam@154 200 unsigned s;
cannam@154 201 int ftb;
cannam@154 202 /*In order to optimize EC_ILOG(), it is undefined for the value 0.*/
cannam@154 203 celt_assert(_ft>1);
cannam@154 204 _ft--;
cannam@154 205 ftb=EC_ILOG(_ft);
cannam@154 206 if(ftb>EC_UINT_BITS){
cannam@154 207 opus_uint32 t;
cannam@154 208 ftb-=EC_UINT_BITS;
cannam@154 209 ft=(unsigned)(_ft>>ftb)+1;
cannam@154 210 s=ec_decode(_this,ft);
cannam@154 211 ec_dec_update(_this,s,s+1,ft);
cannam@154 212 t=(opus_uint32)s<<ftb|ec_dec_bits(_this,ftb);
cannam@154 213 if(t<=_ft)return t;
cannam@154 214 _this->error=1;
cannam@154 215 return _ft;
cannam@154 216 }
cannam@154 217 else{
cannam@154 218 _ft++;
cannam@154 219 s=ec_decode(_this,(unsigned)_ft);
cannam@154 220 ec_dec_update(_this,s,s+1,(unsigned)_ft);
cannam@154 221 return s;
cannam@154 222 }
cannam@154 223 }
cannam@154 224
cannam@154 225 opus_uint32 ec_dec_bits(ec_dec *_this,unsigned _bits){
cannam@154 226 ec_window window;
cannam@154 227 int available;
cannam@154 228 opus_uint32 ret;
cannam@154 229 window=_this->end_window;
cannam@154 230 available=_this->nend_bits;
cannam@154 231 if((unsigned)available<_bits){
cannam@154 232 do{
cannam@154 233 window|=(ec_window)ec_read_byte_from_end(_this)<<available;
cannam@154 234 available+=EC_SYM_BITS;
cannam@154 235 }
cannam@154 236 while(available<=EC_WINDOW_SIZE-EC_SYM_BITS);
cannam@154 237 }
cannam@154 238 ret=(opus_uint32)window&(((opus_uint32)1<<_bits)-1U);
cannam@154 239 window>>=_bits;
cannam@154 240 available-=_bits;
cannam@154 241 _this->end_window=window;
cannam@154 242 _this->nend_bits=available;
cannam@154 243 _this->nbits_total+=_bits;
cannam@154 244 return ret;
cannam@154 245 }