annotate src/libvorbis-1.3.3/lib/sharedbook.c @ 168:ceec0dd9ec9c

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