annotate src/opus-1.3/celt/entenc.c @ 79:91c729825bca pa_catalina

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