media/libopus/celt/entenc.c

Thu, 15 Jan 2015 15:59:08 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 15 Jan 2015 15:59:08 +0100
branch
TOR_BUG_9701
changeset 10
ac0c01689b40
permissions
-rw-r--r--

Implement a real Private Browsing Mode condition by changing the API/ABI;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.

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

mercurial