Tue, 06 Jan 2015 21:39:09 +0100
Conditionally force memory storage according to privacy.thirdparty.isolate;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.
michael@0 | 1 | /******************************************************************** |
michael@0 | 2 | * * |
michael@0 | 3 | * THIS FILE IS PART OF THE OggVorbis SOFTWARE CODEC SOURCE CODE. * |
michael@0 | 4 | * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS * |
michael@0 | 5 | * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE * |
michael@0 | 6 | * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. * |
michael@0 | 7 | * * |
michael@0 | 8 | * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2009 * |
michael@0 | 9 | * by the Xiph.Org Foundation http://www.xiph.org/ * |
michael@0 | 10 | * * |
michael@0 | 11 | ******************************************************************** |
michael@0 | 12 | |
michael@0 | 13 | function: basic shared codebook operations |
michael@0 | 14 | last mod: $Id: sharedbook.c 19057 2014-01-22 12:32:31Z xiphmont $ |
michael@0 | 15 | |
michael@0 | 16 | ********************************************************************/ |
michael@0 | 17 | |
michael@0 | 18 | #include <stdlib.h> |
michael@0 | 19 | #include <math.h> |
michael@0 | 20 | #include <string.h> |
michael@0 | 21 | #include <ogg/ogg.h> |
michael@0 | 22 | #include "os.h" |
michael@0 | 23 | #include "misc.h" |
michael@0 | 24 | #include "vorbis/codec.h" |
michael@0 | 25 | #include "codebook.h" |
michael@0 | 26 | #include "scales.h" |
michael@0 | 27 | |
michael@0 | 28 | /**** pack/unpack helpers ******************************************/ |
michael@0 | 29 | int _ilog(unsigned int v){ |
michael@0 | 30 | int ret=0; |
michael@0 | 31 | while(v){ |
michael@0 | 32 | ret++; |
michael@0 | 33 | v>>=1; |
michael@0 | 34 | } |
michael@0 | 35 | return(ret); |
michael@0 | 36 | } |
michael@0 | 37 | |
michael@0 | 38 | /* 32 bit float (not IEEE; nonnormalized mantissa + |
michael@0 | 39 | biased exponent) : neeeeeee eeemmmmm mmmmmmmm mmmmmmmm |
michael@0 | 40 | Why not IEEE? It's just not that important here. */ |
michael@0 | 41 | |
michael@0 | 42 | #define VQ_FEXP 10 |
michael@0 | 43 | #define VQ_FMAN 21 |
michael@0 | 44 | #define VQ_FEXP_BIAS 768 /* bias toward values smaller than 1. */ |
michael@0 | 45 | |
michael@0 | 46 | /* doesn't currently guard under/overflow */ |
michael@0 | 47 | long _float32_pack(float val){ |
michael@0 | 48 | int sign=0; |
michael@0 | 49 | long exp; |
michael@0 | 50 | long mant; |
michael@0 | 51 | if(val<0){ |
michael@0 | 52 | sign=0x80000000; |
michael@0 | 53 | val= -val; |
michael@0 | 54 | } |
michael@0 | 55 | exp= floor(log(val)/log(2.f)+.001); //+epsilon |
michael@0 | 56 | mant=rint(ldexp(val,(VQ_FMAN-1)-exp)); |
michael@0 | 57 | exp=(exp+VQ_FEXP_BIAS)<<VQ_FMAN; |
michael@0 | 58 | |
michael@0 | 59 | return(sign|exp|mant); |
michael@0 | 60 | } |
michael@0 | 61 | |
michael@0 | 62 | float _float32_unpack(long val){ |
michael@0 | 63 | double mant=val&0x1fffff; |
michael@0 | 64 | int sign=val&0x80000000; |
michael@0 | 65 | long exp =(val&0x7fe00000L)>>VQ_FMAN; |
michael@0 | 66 | if(sign)mant= -mant; |
michael@0 | 67 | return(ldexp(mant,exp-(VQ_FMAN-1)-VQ_FEXP_BIAS)); |
michael@0 | 68 | } |
michael@0 | 69 | |
michael@0 | 70 | /* given a list of word lengths, generate a list of codewords. Works |
michael@0 | 71 | for length ordered or unordered, always assigns the lowest valued |
michael@0 | 72 | codewords first. Extended to handle unused entries (length 0) */ |
michael@0 | 73 | ogg_uint32_t *_make_words(char *l,long n,long sparsecount){ |
michael@0 | 74 | long i,j,count=0; |
michael@0 | 75 | ogg_uint32_t marker[33]; |
michael@0 | 76 | ogg_uint32_t *r=_ogg_malloc((sparsecount?sparsecount:n)*sizeof(*r)); |
michael@0 | 77 | memset(marker,0,sizeof(marker)); |
michael@0 | 78 | |
michael@0 | 79 | for(i=0;i<n;i++){ |
michael@0 | 80 | long length=l[i]; |
michael@0 | 81 | if(length>0){ |
michael@0 | 82 | ogg_uint32_t entry=marker[length]; |
michael@0 | 83 | |
michael@0 | 84 | /* when we claim a node for an entry, we also claim the nodes |
michael@0 | 85 | below it (pruning off the imagined tree that may have dangled |
michael@0 | 86 | from it) as well as blocking the use of any nodes directly |
michael@0 | 87 | above for leaves */ |
michael@0 | 88 | |
michael@0 | 89 | /* update ourself */ |
michael@0 | 90 | if(length<32 && (entry>>length)){ |
michael@0 | 91 | /* error condition; the lengths must specify an overpopulated tree */ |
michael@0 | 92 | _ogg_free(r); |
michael@0 | 93 | return(NULL); |
michael@0 | 94 | } |
michael@0 | 95 | r[count++]=entry; |
michael@0 | 96 | |
michael@0 | 97 | /* Look to see if the next shorter marker points to the node |
michael@0 | 98 | above. if so, update it and repeat. */ |
michael@0 | 99 | { |
michael@0 | 100 | for(j=length;j>0;j--){ |
michael@0 | 101 | |
michael@0 | 102 | if(marker[j]&1){ |
michael@0 | 103 | /* have to jump branches */ |
michael@0 | 104 | if(j==1) |
michael@0 | 105 | marker[1]++; |
michael@0 | 106 | else |
michael@0 | 107 | marker[j]=marker[j-1]<<1; |
michael@0 | 108 | break; /* invariant says next upper marker would already |
michael@0 | 109 | have been moved if it was on the same path */ |
michael@0 | 110 | } |
michael@0 | 111 | marker[j]++; |
michael@0 | 112 | } |
michael@0 | 113 | } |
michael@0 | 114 | |
michael@0 | 115 | /* prune the tree; the implicit invariant says all the longer |
michael@0 | 116 | markers were dangling from our just-taken node. Dangle them |
michael@0 | 117 | from our *new* node. */ |
michael@0 | 118 | for(j=length+1;j<33;j++) |
michael@0 | 119 | if((marker[j]>>1) == entry){ |
michael@0 | 120 | entry=marker[j]; |
michael@0 | 121 | marker[j]=marker[j-1]<<1; |
michael@0 | 122 | }else |
michael@0 | 123 | break; |
michael@0 | 124 | }else |
michael@0 | 125 | if(sparsecount==0)count++; |
michael@0 | 126 | } |
michael@0 | 127 | |
michael@0 | 128 | /* sanity check the huffman tree; an underpopulated tree must be |
michael@0 | 129 | rejected. The only exception is the one-node pseudo-nil tree, |
michael@0 | 130 | which appears to be underpopulated because the tree doesn't |
michael@0 | 131 | really exist; there's only one possible 'codeword' or zero bits, |
michael@0 | 132 | but the above tree-gen code doesn't mark that. */ |
michael@0 | 133 | if(sparsecount != 1){ |
michael@0 | 134 | for(i=1;i<33;i++) |
michael@0 | 135 | if(marker[i] & (0xffffffffUL>>(32-i))){ |
michael@0 | 136 | _ogg_free(r); |
michael@0 | 137 | return(NULL); |
michael@0 | 138 | } |
michael@0 | 139 | } |
michael@0 | 140 | |
michael@0 | 141 | /* bitreverse the words because our bitwise packer/unpacker is LSb |
michael@0 | 142 | endian */ |
michael@0 | 143 | for(i=0,count=0;i<n;i++){ |
michael@0 | 144 | ogg_uint32_t temp=0; |
michael@0 | 145 | for(j=0;j<l[i];j++){ |
michael@0 | 146 | temp<<=1; |
michael@0 | 147 | temp|=(r[count]>>j)&1; |
michael@0 | 148 | } |
michael@0 | 149 | |
michael@0 | 150 | if(sparsecount){ |
michael@0 | 151 | if(l[i]) |
michael@0 | 152 | r[count++]=temp; |
michael@0 | 153 | }else |
michael@0 | 154 | r[count++]=temp; |
michael@0 | 155 | } |
michael@0 | 156 | |
michael@0 | 157 | return(r); |
michael@0 | 158 | } |
michael@0 | 159 | |
michael@0 | 160 | /* there might be a straightforward one-line way to do the below |
michael@0 | 161 | that's portable and totally safe against roundoff, but I haven't |
michael@0 | 162 | thought of it. Therefore, we opt on the side of caution */ |
michael@0 | 163 | long _book_maptype1_quantvals(const static_codebook *b){ |
michael@0 | 164 | long vals=floor(pow((float)b->entries,1.f/b->dim)); |
michael@0 | 165 | |
michael@0 | 166 | /* the above *should* be reliable, but we'll not assume that FP is |
michael@0 | 167 | ever reliable when bitstream sync is at stake; verify via integer |
michael@0 | 168 | means that vals really is the greatest value of dim for which |
michael@0 | 169 | vals^b->bim <= b->entries */ |
michael@0 | 170 | /* treat the above as an initial guess */ |
michael@0 | 171 | while(1){ |
michael@0 | 172 | long acc=1; |
michael@0 | 173 | long acc1=1; |
michael@0 | 174 | int i; |
michael@0 | 175 | for(i=0;i<b->dim;i++){ |
michael@0 | 176 | acc*=vals; |
michael@0 | 177 | acc1*=vals+1; |
michael@0 | 178 | } |
michael@0 | 179 | if(acc<=b->entries && acc1>b->entries){ |
michael@0 | 180 | return(vals); |
michael@0 | 181 | }else{ |
michael@0 | 182 | if(acc>b->entries){ |
michael@0 | 183 | vals--; |
michael@0 | 184 | }else{ |
michael@0 | 185 | vals++; |
michael@0 | 186 | } |
michael@0 | 187 | } |
michael@0 | 188 | } |
michael@0 | 189 | } |
michael@0 | 190 | |
michael@0 | 191 | /* unpack the quantized list of values for encode/decode ***********/ |
michael@0 | 192 | /* we need to deal with two map types: in map type 1, the values are |
michael@0 | 193 | generated algorithmically (each column of the vector counts through |
michael@0 | 194 | the values in the quant vector). in map type 2, all the values came |
michael@0 | 195 | in in an explicit list. Both value lists must be unpacked */ |
michael@0 | 196 | float *_book_unquantize(const static_codebook *b,int n,int *sparsemap){ |
michael@0 | 197 | long j,k,count=0; |
michael@0 | 198 | if(b->maptype==1 || b->maptype==2){ |
michael@0 | 199 | int quantvals; |
michael@0 | 200 | float mindel=_float32_unpack(b->q_min); |
michael@0 | 201 | float delta=_float32_unpack(b->q_delta); |
michael@0 | 202 | float *r=_ogg_calloc(n*b->dim,sizeof(*r)); |
michael@0 | 203 | |
michael@0 | 204 | /* maptype 1 and 2 both use a quantized value vector, but |
michael@0 | 205 | different sizes */ |
michael@0 | 206 | switch(b->maptype){ |
michael@0 | 207 | case 1: |
michael@0 | 208 | /* most of the time, entries%dimensions == 0, but we need to be |
michael@0 | 209 | well defined. We define that the possible vales at each |
michael@0 | 210 | scalar is values == entries/dim. If entries%dim != 0, we'll |
michael@0 | 211 | have 'too few' values (values*dim<entries), which means that |
michael@0 | 212 | we'll have 'left over' entries; left over entries use zeroed |
michael@0 | 213 | values (and are wasted). So don't generate codebooks like |
michael@0 | 214 | that */ |
michael@0 | 215 | quantvals=_book_maptype1_quantvals(b); |
michael@0 | 216 | for(j=0;j<b->entries;j++){ |
michael@0 | 217 | if((sparsemap && b->lengthlist[j]) || !sparsemap){ |
michael@0 | 218 | float last=0.f; |
michael@0 | 219 | int indexdiv=1; |
michael@0 | 220 | for(k=0;k<b->dim;k++){ |
michael@0 | 221 | int index= (j/indexdiv)%quantvals; |
michael@0 | 222 | float val=b->quantlist[index]; |
michael@0 | 223 | val=fabs(val)*delta+mindel+last; |
michael@0 | 224 | if(b->q_sequencep)last=val; |
michael@0 | 225 | if(sparsemap) |
michael@0 | 226 | r[sparsemap[count]*b->dim+k]=val; |
michael@0 | 227 | else |
michael@0 | 228 | r[count*b->dim+k]=val; |
michael@0 | 229 | indexdiv*=quantvals; |
michael@0 | 230 | } |
michael@0 | 231 | count++; |
michael@0 | 232 | } |
michael@0 | 233 | |
michael@0 | 234 | } |
michael@0 | 235 | break; |
michael@0 | 236 | case 2: |
michael@0 | 237 | for(j=0;j<b->entries;j++){ |
michael@0 | 238 | if((sparsemap && b->lengthlist[j]) || !sparsemap){ |
michael@0 | 239 | float last=0.f; |
michael@0 | 240 | |
michael@0 | 241 | for(k=0;k<b->dim;k++){ |
michael@0 | 242 | float val=b->quantlist[j*b->dim+k]; |
michael@0 | 243 | val=fabs(val)*delta+mindel+last; |
michael@0 | 244 | if(b->q_sequencep)last=val; |
michael@0 | 245 | if(sparsemap) |
michael@0 | 246 | r[sparsemap[count]*b->dim+k]=val; |
michael@0 | 247 | else |
michael@0 | 248 | r[count*b->dim+k]=val; |
michael@0 | 249 | } |
michael@0 | 250 | count++; |
michael@0 | 251 | } |
michael@0 | 252 | } |
michael@0 | 253 | break; |
michael@0 | 254 | } |
michael@0 | 255 | |
michael@0 | 256 | return(r); |
michael@0 | 257 | } |
michael@0 | 258 | return(NULL); |
michael@0 | 259 | } |
michael@0 | 260 | |
michael@0 | 261 | void vorbis_staticbook_destroy(static_codebook *b){ |
michael@0 | 262 | if(b->allocedp){ |
michael@0 | 263 | if(b->quantlist)_ogg_free(b->quantlist); |
michael@0 | 264 | if(b->lengthlist)_ogg_free(b->lengthlist); |
michael@0 | 265 | memset(b,0,sizeof(*b)); |
michael@0 | 266 | _ogg_free(b); |
michael@0 | 267 | } /* otherwise, it is in static memory */ |
michael@0 | 268 | } |
michael@0 | 269 | |
michael@0 | 270 | void vorbis_book_clear(codebook *b){ |
michael@0 | 271 | /* static book is not cleared; we're likely called on the lookup and |
michael@0 | 272 | the static codebook belongs to the info struct */ |
michael@0 | 273 | if(b->valuelist)_ogg_free(b->valuelist); |
michael@0 | 274 | if(b->codelist)_ogg_free(b->codelist); |
michael@0 | 275 | |
michael@0 | 276 | if(b->dec_index)_ogg_free(b->dec_index); |
michael@0 | 277 | if(b->dec_codelengths)_ogg_free(b->dec_codelengths); |
michael@0 | 278 | if(b->dec_firsttable)_ogg_free(b->dec_firsttable); |
michael@0 | 279 | |
michael@0 | 280 | memset(b,0,sizeof(*b)); |
michael@0 | 281 | } |
michael@0 | 282 | |
michael@0 | 283 | int vorbis_book_init_encode(codebook *c,const static_codebook *s){ |
michael@0 | 284 | |
michael@0 | 285 | memset(c,0,sizeof(*c)); |
michael@0 | 286 | c->c=s; |
michael@0 | 287 | c->entries=s->entries; |
michael@0 | 288 | c->used_entries=s->entries; |
michael@0 | 289 | c->dim=s->dim; |
michael@0 | 290 | c->codelist=_make_words(s->lengthlist,s->entries,0); |
michael@0 | 291 | //c->valuelist=_book_unquantize(s,s->entries,NULL); |
michael@0 | 292 | c->quantvals=_book_maptype1_quantvals(s); |
michael@0 | 293 | c->minval=(int)rint(_float32_unpack(s->q_min)); |
michael@0 | 294 | c->delta=(int)rint(_float32_unpack(s->q_delta)); |
michael@0 | 295 | |
michael@0 | 296 | return(0); |
michael@0 | 297 | } |
michael@0 | 298 | |
michael@0 | 299 | static ogg_uint32_t bitreverse(ogg_uint32_t x){ |
michael@0 | 300 | x= ((x>>16)&0x0000ffffUL) | ((x<<16)&0xffff0000UL); |
michael@0 | 301 | x= ((x>> 8)&0x00ff00ffUL) | ((x<< 8)&0xff00ff00UL); |
michael@0 | 302 | x= ((x>> 4)&0x0f0f0f0fUL) | ((x<< 4)&0xf0f0f0f0UL); |
michael@0 | 303 | x= ((x>> 2)&0x33333333UL) | ((x<< 2)&0xccccccccUL); |
michael@0 | 304 | return((x>> 1)&0x55555555UL) | ((x<< 1)&0xaaaaaaaaUL); |
michael@0 | 305 | } |
michael@0 | 306 | |
michael@0 | 307 | static int sort32a(const void *a,const void *b){ |
michael@0 | 308 | return ( **(ogg_uint32_t **)a>**(ogg_uint32_t **)b)- |
michael@0 | 309 | ( **(ogg_uint32_t **)a<**(ogg_uint32_t **)b); |
michael@0 | 310 | } |
michael@0 | 311 | |
michael@0 | 312 | /* decode codebook arrangement is more heavily optimized than encode */ |
michael@0 | 313 | int vorbis_book_init_decode(codebook *c,const static_codebook *s){ |
michael@0 | 314 | int i,j,n=0,tabn; |
michael@0 | 315 | int *sortindex; |
michael@0 | 316 | memset(c,0,sizeof(*c)); |
michael@0 | 317 | |
michael@0 | 318 | /* count actually used entries */ |
michael@0 | 319 | for(i=0;i<s->entries;i++) |
michael@0 | 320 | if(s->lengthlist[i]>0) |
michael@0 | 321 | n++; |
michael@0 | 322 | |
michael@0 | 323 | c->entries=s->entries; |
michael@0 | 324 | c->used_entries=n; |
michael@0 | 325 | c->dim=s->dim; |
michael@0 | 326 | |
michael@0 | 327 | if(n>0){ |
michael@0 | 328 | |
michael@0 | 329 | /* two different remappings go on here. |
michael@0 | 330 | |
michael@0 | 331 | First, we collapse the likely sparse codebook down only to |
michael@0 | 332 | actually represented values/words. This collapsing needs to be |
michael@0 | 333 | indexed as map-valueless books are used to encode original entry |
michael@0 | 334 | positions as integers. |
michael@0 | 335 | |
michael@0 | 336 | Second, we reorder all vectors, including the entry index above, |
michael@0 | 337 | by sorted bitreversed codeword to allow treeless decode. */ |
michael@0 | 338 | |
michael@0 | 339 | /* perform sort */ |
michael@0 | 340 | ogg_uint32_t *codes=_make_words(s->lengthlist,s->entries,c->used_entries); |
michael@0 | 341 | ogg_uint32_t **codep=alloca(sizeof(*codep)*n); |
michael@0 | 342 | |
michael@0 | 343 | if(codes==NULL)goto err_out; |
michael@0 | 344 | |
michael@0 | 345 | for(i=0;i<n;i++){ |
michael@0 | 346 | codes[i]=bitreverse(codes[i]); |
michael@0 | 347 | codep[i]=codes+i; |
michael@0 | 348 | } |
michael@0 | 349 | |
michael@0 | 350 | qsort(codep,n,sizeof(*codep),sort32a); |
michael@0 | 351 | |
michael@0 | 352 | sortindex=alloca(n*sizeof(*sortindex)); |
michael@0 | 353 | c->codelist=_ogg_malloc(n*sizeof(*c->codelist)); |
michael@0 | 354 | /* the index is a reverse index */ |
michael@0 | 355 | for(i=0;i<n;i++){ |
michael@0 | 356 | int position=codep[i]-codes; |
michael@0 | 357 | sortindex[position]=i; |
michael@0 | 358 | } |
michael@0 | 359 | |
michael@0 | 360 | for(i=0;i<n;i++) |
michael@0 | 361 | c->codelist[sortindex[i]]=codes[i]; |
michael@0 | 362 | _ogg_free(codes); |
michael@0 | 363 | |
michael@0 | 364 | |
michael@0 | 365 | c->valuelist=_book_unquantize(s,n,sortindex); |
michael@0 | 366 | c->dec_index=_ogg_malloc(n*sizeof(*c->dec_index)); |
michael@0 | 367 | |
michael@0 | 368 | for(n=0,i=0;i<s->entries;i++) |
michael@0 | 369 | if(s->lengthlist[i]>0) |
michael@0 | 370 | c->dec_index[sortindex[n++]]=i; |
michael@0 | 371 | |
michael@0 | 372 | c->dec_codelengths=_ogg_malloc(n*sizeof(*c->dec_codelengths)); |
michael@0 | 373 | for(n=0,i=0;i<s->entries;i++) |
michael@0 | 374 | if(s->lengthlist[i]>0) |
michael@0 | 375 | c->dec_codelengths[sortindex[n++]]=s->lengthlist[i]; |
michael@0 | 376 | |
michael@0 | 377 | c->dec_firsttablen=_ilog(c->used_entries)-4; /* this is magic */ |
michael@0 | 378 | if(c->dec_firsttablen<5)c->dec_firsttablen=5; |
michael@0 | 379 | if(c->dec_firsttablen>8)c->dec_firsttablen=8; |
michael@0 | 380 | |
michael@0 | 381 | tabn=1<<c->dec_firsttablen; |
michael@0 | 382 | c->dec_firsttable=_ogg_calloc(tabn,sizeof(*c->dec_firsttable)); |
michael@0 | 383 | c->dec_maxlength=0; |
michael@0 | 384 | |
michael@0 | 385 | for(i=0;i<n;i++){ |
michael@0 | 386 | if(c->dec_maxlength<c->dec_codelengths[i]) |
michael@0 | 387 | c->dec_maxlength=c->dec_codelengths[i]; |
michael@0 | 388 | if(c->dec_codelengths[i]<=c->dec_firsttablen){ |
michael@0 | 389 | ogg_uint32_t orig=bitreverse(c->codelist[i]); |
michael@0 | 390 | for(j=0;j<(1<<(c->dec_firsttablen-c->dec_codelengths[i]));j++) |
michael@0 | 391 | c->dec_firsttable[orig|(j<<c->dec_codelengths[i])]=i+1; |
michael@0 | 392 | } |
michael@0 | 393 | } |
michael@0 | 394 | |
michael@0 | 395 | /* now fill in 'unused' entries in the firsttable with hi/lo search |
michael@0 | 396 | hints for the non-direct-hits */ |
michael@0 | 397 | { |
michael@0 | 398 | ogg_uint32_t mask=0xfffffffeUL<<(31-c->dec_firsttablen); |
michael@0 | 399 | long lo=0,hi=0; |
michael@0 | 400 | |
michael@0 | 401 | for(i=0;i<tabn;i++){ |
michael@0 | 402 | ogg_uint32_t word=i<<(32-c->dec_firsttablen); |
michael@0 | 403 | if(c->dec_firsttable[bitreverse(word)]==0){ |
michael@0 | 404 | while((lo+1)<n && c->codelist[lo+1]<=word)lo++; |
michael@0 | 405 | while( hi<n && word>=(c->codelist[hi]&mask))hi++; |
michael@0 | 406 | |
michael@0 | 407 | /* we only actually have 15 bits per hint to play with here. |
michael@0 | 408 | In order to overflow gracefully (nothing breaks, efficiency |
michael@0 | 409 | just drops), encode as the difference from the extremes. */ |
michael@0 | 410 | { |
michael@0 | 411 | unsigned long loval=lo; |
michael@0 | 412 | unsigned long hival=n-hi; |
michael@0 | 413 | |
michael@0 | 414 | if(loval>0x7fff)loval=0x7fff; |
michael@0 | 415 | if(hival>0x7fff)hival=0x7fff; |
michael@0 | 416 | c->dec_firsttable[bitreverse(word)]= |
michael@0 | 417 | 0x80000000UL | (loval<<15) | hival; |
michael@0 | 418 | } |
michael@0 | 419 | } |
michael@0 | 420 | } |
michael@0 | 421 | } |
michael@0 | 422 | } |
michael@0 | 423 | |
michael@0 | 424 | return(0); |
michael@0 | 425 | err_out: |
michael@0 | 426 | vorbis_book_clear(c); |
michael@0 | 427 | return(-1); |
michael@0 | 428 | } |
michael@0 | 429 | |
michael@0 | 430 | long vorbis_book_codeword(codebook *book,int entry){ |
michael@0 | 431 | if(book->c) /* only use with encode; decode optimizations are |
michael@0 | 432 | allowed to break this */ |
michael@0 | 433 | return book->codelist[entry]; |
michael@0 | 434 | return -1; |
michael@0 | 435 | } |
michael@0 | 436 | |
michael@0 | 437 | long vorbis_book_codelen(codebook *book,int entry){ |
michael@0 | 438 | if(book->c) /* only use with encode; decode optimizations are |
michael@0 | 439 | allowed to break this */ |
michael@0 | 440 | return book->c->lengthlist[entry]; |
michael@0 | 441 | return -1; |
michael@0 | 442 | } |
michael@0 | 443 | |
michael@0 | 444 | #ifdef _V_SELFTEST |
michael@0 | 445 | |
michael@0 | 446 | /* Unit tests of the dequantizer; this stuff will be OK |
michael@0 | 447 | cross-platform, I simply want to be sure that special mapping cases |
michael@0 | 448 | actually work properly; a bug could go unnoticed for a while */ |
michael@0 | 449 | |
michael@0 | 450 | #include <stdio.h> |
michael@0 | 451 | |
michael@0 | 452 | /* cases: |
michael@0 | 453 | |
michael@0 | 454 | no mapping |
michael@0 | 455 | full, explicit mapping |
michael@0 | 456 | algorithmic mapping |
michael@0 | 457 | |
michael@0 | 458 | nonsequential |
michael@0 | 459 | sequential |
michael@0 | 460 | */ |
michael@0 | 461 | |
michael@0 | 462 | static long full_quantlist1[]={0,1,2,3, 4,5,6,7, 8,3,6,1}; |
michael@0 | 463 | static long partial_quantlist1[]={0,7,2}; |
michael@0 | 464 | |
michael@0 | 465 | /* no mapping */ |
michael@0 | 466 | static_codebook test1={ |
michael@0 | 467 | 4,16, |
michael@0 | 468 | NULL, |
michael@0 | 469 | 0, |
michael@0 | 470 | 0,0,0,0, |
michael@0 | 471 | NULL, |
michael@0 | 472 | 0 |
michael@0 | 473 | }; |
michael@0 | 474 | static float *test1_result=NULL; |
michael@0 | 475 | |
michael@0 | 476 | /* linear, full mapping, nonsequential */ |
michael@0 | 477 | static_codebook test2={ |
michael@0 | 478 | 4,3, |
michael@0 | 479 | NULL, |
michael@0 | 480 | 2, |
michael@0 | 481 | -533200896,1611661312,4,0, |
michael@0 | 482 | full_quantlist1, |
michael@0 | 483 | 0 |
michael@0 | 484 | }; |
michael@0 | 485 | static float test2_result[]={-3,-2,-1,0, 1,2,3,4, 5,0,3,-2}; |
michael@0 | 486 | |
michael@0 | 487 | /* linear, full mapping, sequential */ |
michael@0 | 488 | static_codebook test3={ |
michael@0 | 489 | 4,3, |
michael@0 | 490 | NULL, |
michael@0 | 491 | 2, |
michael@0 | 492 | -533200896,1611661312,4,1, |
michael@0 | 493 | full_quantlist1, |
michael@0 | 494 | 0 |
michael@0 | 495 | }; |
michael@0 | 496 | static float test3_result[]={-3,-5,-6,-6, 1,3,6,10, 5,5,8,6}; |
michael@0 | 497 | |
michael@0 | 498 | /* linear, algorithmic mapping, nonsequential */ |
michael@0 | 499 | static_codebook test4={ |
michael@0 | 500 | 3,27, |
michael@0 | 501 | NULL, |
michael@0 | 502 | 1, |
michael@0 | 503 | -533200896,1611661312,4,0, |
michael@0 | 504 | partial_quantlist1, |
michael@0 | 505 | 0 |
michael@0 | 506 | }; |
michael@0 | 507 | static float test4_result[]={-3,-3,-3, 4,-3,-3, -1,-3,-3, |
michael@0 | 508 | -3, 4,-3, 4, 4,-3, -1, 4,-3, |
michael@0 | 509 | -3,-1,-3, 4,-1,-3, -1,-1,-3, |
michael@0 | 510 | -3,-3, 4, 4,-3, 4, -1,-3, 4, |
michael@0 | 511 | -3, 4, 4, 4, 4, 4, -1, 4, 4, |
michael@0 | 512 | -3,-1, 4, 4,-1, 4, -1,-1, 4, |
michael@0 | 513 | -3,-3,-1, 4,-3,-1, -1,-3,-1, |
michael@0 | 514 | -3, 4,-1, 4, 4,-1, -1, 4,-1, |
michael@0 | 515 | -3,-1,-1, 4,-1,-1, -1,-1,-1}; |
michael@0 | 516 | |
michael@0 | 517 | /* linear, algorithmic mapping, sequential */ |
michael@0 | 518 | static_codebook test5={ |
michael@0 | 519 | 3,27, |
michael@0 | 520 | NULL, |
michael@0 | 521 | 1, |
michael@0 | 522 | -533200896,1611661312,4,1, |
michael@0 | 523 | partial_quantlist1, |
michael@0 | 524 | 0 |
michael@0 | 525 | }; |
michael@0 | 526 | static float test5_result[]={-3,-6,-9, 4, 1,-2, -1,-4,-7, |
michael@0 | 527 | -3, 1,-2, 4, 8, 5, -1, 3, 0, |
michael@0 | 528 | -3,-4,-7, 4, 3, 0, -1,-2,-5, |
michael@0 | 529 | -3,-6,-2, 4, 1, 5, -1,-4, 0, |
michael@0 | 530 | -3, 1, 5, 4, 8,12, -1, 3, 7, |
michael@0 | 531 | -3,-4, 0, 4, 3, 7, -1,-2, 2, |
michael@0 | 532 | -3,-6,-7, 4, 1, 0, -1,-4,-5, |
michael@0 | 533 | -3, 1, 0, 4, 8, 7, -1, 3, 2, |
michael@0 | 534 | -3,-4,-5, 4, 3, 2, -1,-2,-3}; |
michael@0 | 535 | |
michael@0 | 536 | void run_test(static_codebook *b,float *comp){ |
michael@0 | 537 | float *out=_book_unquantize(b,b->entries,NULL); |
michael@0 | 538 | int i; |
michael@0 | 539 | |
michael@0 | 540 | if(comp){ |
michael@0 | 541 | if(!out){ |
michael@0 | 542 | fprintf(stderr,"_book_unquantize incorrectly returned NULL\n"); |
michael@0 | 543 | exit(1); |
michael@0 | 544 | } |
michael@0 | 545 | |
michael@0 | 546 | for(i=0;i<b->entries*b->dim;i++) |
michael@0 | 547 | if(fabs(out[i]-comp[i])>.0001){ |
michael@0 | 548 | fprintf(stderr,"disagreement in unquantized and reference data:\n" |
michael@0 | 549 | "position %d, %g != %g\n",i,out[i],comp[i]); |
michael@0 | 550 | exit(1); |
michael@0 | 551 | } |
michael@0 | 552 | |
michael@0 | 553 | }else{ |
michael@0 | 554 | if(out){ |
michael@0 | 555 | fprintf(stderr,"_book_unquantize returned a value array: \n" |
michael@0 | 556 | " correct result should have been NULL\n"); |
michael@0 | 557 | exit(1); |
michael@0 | 558 | } |
michael@0 | 559 | } |
michael@0 | 560 | } |
michael@0 | 561 | |
michael@0 | 562 | int main(){ |
michael@0 | 563 | /* run the nine dequant tests, and compare to the hand-rolled results */ |
michael@0 | 564 | fprintf(stderr,"Dequant test 1... "); |
michael@0 | 565 | run_test(&test1,test1_result); |
michael@0 | 566 | fprintf(stderr,"OK\nDequant test 2... "); |
michael@0 | 567 | run_test(&test2,test2_result); |
michael@0 | 568 | fprintf(stderr,"OK\nDequant test 3... "); |
michael@0 | 569 | run_test(&test3,test3_result); |
michael@0 | 570 | fprintf(stderr,"OK\nDequant test 4... "); |
michael@0 | 571 | run_test(&test4,test4_result); |
michael@0 | 572 | fprintf(stderr,"OK\nDequant test 5... "); |
michael@0 | 573 | run_test(&test5,test5_result); |
michael@0 | 574 | fprintf(stderr,"OK\n\n"); |
michael@0 | 575 | |
michael@0 | 576 | return(0); |
michael@0 | 577 | } |
michael@0 | 578 | |
michael@0 | 579 | #endif |