PageRenderTime 28ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/src/vxa/vorbis/libvorbis/lib/sharedbook.c

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