annotate src/libvorbis-1.3.3/lib/sharedbook.c @ 83:ae30d91d2ffe

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