dbf-halloween2015

annotate libs/vorbis/sharedbook.c @ 4:4316c0c879e9

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