sharedbook.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754
  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-2007 *
  9. * by the Xiph.Org Foundation http://www.xiph.org/ *
  10. * *
  11. ********************************************************************
  12. function: basic shared codebook operations
  13. last mod: $Id$
  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. /* sanity check the huffman tree; an underpopulated tree must be
  112. rejected. The only exception is the one-node pseudo-nil tree,
  113. which appears to be underpopulated because the tree doesn't
  114. really exist; there's only one possible 'codeword' or zero bits,
  115. but the above tree-gen code doesn't mark that. */
  116. if(sparsecount != 1){
  117. for(i=1;i<33;i++)
  118. if(marker[i] & (0xffffffffUL>>(32-i))){
  119. _ogg_free(r);
  120. return(NULL);
  121. }
  122. }
  123. /* bitreverse the words because our bitwise packer/unpacker is LSb
  124. endian */
  125. for(i=0,count=0;i<n;i++){
  126. ogg_uint32_t temp=0;
  127. for(j=0;j<l[i];j++){
  128. temp<<=1;
  129. temp|=(r[count]>>j)&1;
  130. }
  131. if(sparsecount){
  132. if(l[i])
  133. r[count++]=temp;
  134. }else
  135. r[count++]=temp;
  136. }
  137. return(r);
  138. }
  139. /* there might be a straightforward one-line way to do the below
  140. that's portable and totally safe against roundoff, but I haven't
  141. thought of it. Therefore, we opt on the side of caution */
  142. long _book_maptype1_quantvals(const static_codebook *b){
  143. long vals=floor(pow((float)b->entries,1.f/b->dim));
  144. /* the above *should* be reliable, but we'll not assume that FP is
  145. ever reliable when bitstream sync is at stake; verify via integer
  146. means that vals really is the greatest value of dim for which
  147. vals^b->bim <= b->entries */
  148. /* treat the above as an initial guess */
  149. while(1){
  150. long acc=1;
  151. long acc1=1;
  152. int i;
  153. for(i=0;i<b->dim;i++){
  154. acc*=vals;
  155. acc1*=vals+1;
  156. }
  157. if(acc<=b->entries && acc1>b->entries){
  158. return(vals);
  159. }else{
  160. if(acc>b->entries){
  161. vals--;
  162. }else{
  163. vals++;
  164. }
  165. }
  166. }
  167. }
  168. /* unpack the quantized list of values for encode/decode ***********/
  169. /* we need to deal with two map types: in map type 1, the values are
  170. generated algorithmically (each column of the vector counts through
  171. the values in the quant vector). in map type 2, all the values came
  172. in in an explicit list. Both value lists must be unpacked */
  173. float *_book_unquantize(const static_codebook *b,int n,int *sparsemap){
  174. long j,k,count=0;
  175. if(b->maptype==1 || b->maptype==2){
  176. int quantvals;
  177. float mindel=_float32_unpack(b->q_min);
  178. float delta=_float32_unpack(b->q_delta);
  179. float *r=_ogg_calloc(n*b->dim,sizeof(*r));
  180. /* maptype 1 and 2 both use a quantized value vector, but
  181. different sizes */
  182. switch(b->maptype){
  183. case 1:
  184. /* most of the time, entries%dimensions == 0, but we need to be
  185. well defined. We define that the possible vales at each
  186. scalar is values == entries/dim. If entries%dim != 0, we'll
  187. have 'too few' values (values*dim<entries), which means that
  188. we'll have 'left over' entries; left over entries use zeroed
  189. values (and are wasted). So don't generate codebooks like
  190. that */
  191. quantvals=_book_maptype1_quantvals(b);
  192. for(j=0;j<b->entries;j++){
  193. if((sparsemap && b->lengthlist[j]) || !sparsemap){
  194. float last=0.f;
  195. int indexdiv=1;
  196. for(k=0;k<b->dim;k++){
  197. int index= (j/indexdiv)%quantvals;
  198. float val=b->quantlist[index];
  199. val=fabs(val)*delta+mindel+last;
  200. if(b->q_sequencep)last=val;
  201. if(sparsemap)
  202. r[sparsemap[count]*b->dim+k]=val;
  203. else
  204. r[count*b->dim+k]=val;
  205. indexdiv*=quantvals;
  206. }
  207. count++;
  208. }
  209. }
  210. break;
  211. case 2:
  212. for(j=0;j<b->entries;j++){
  213. if((sparsemap && b->lengthlist[j]) || !sparsemap){
  214. float last=0.f;
  215. for(k=0;k<b->dim;k++){
  216. float val=b->quantlist[j*b->dim+k];
  217. val=fabs(val)*delta+mindel+last;
  218. if(b->q_sequencep)last=val;
  219. if(sparsemap)
  220. r[sparsemap[count]*b->dim+k]=val;
  221. else
  222. r[count*b->dim+k]=val;
  223. }
  224. count++;
  225. }
  226. }
  227. break;
  228. }
  229. return(r);
  230. }
  231. return(NULL);
  232. }
  233. void vorbis_staticbook_clear(static_codebook *b){
  234. if(b->allocedp){
  235. if(b->quantlist)_ogg_free(b->quantlist);
  236. if(b->lengthlist)_ogg_free(b->lengthlist);
  237. if(b->nearest_tree){
  238. _ogg_free(b->nearest_tree->ptr0);
  239. _ogg_free(b->nearest_tree->ptr1);
  240. _ogg_free(b->nearest_tree->p);
  241. _ogg_free(b->nearest_tree->q);
  242. memset(b->nearest_tree,0,sizeof(*b->nearest_tree));
  243. _ogg_free(b->nearest_tree);
  244. }
  245. if(b->thresh_tree){
  246. _ogg_free(b->thresh_tree->quantthresh);
  247. _ogg_free(b->thresh_tree->quantmap);
  248. memset(b->thresh_tree,0,sizeof(*b->thresh_tree));
  249. _ogg_free(b->thresh_tree);
  250. }
  251. memset(b,0,sizeof(*b));
  252. }
  253. }
  254. void vorbis_staticbook_destroy(static_codebook *b){
  255. if(b->allocedp){
  256. vorbis_staticbook_clear(b);
  257. _ogg_free(b);
  258. }
  259. }
  260. void vorbis_book_clear(codebook *b){
  261. /* static book is not cleared; we're likely called on the lookup and
  262. the static codebook belongs to the info struct */
  263. if(b->valuelist)_ogg_free(b->valuelist);
  264. if(b->codelist)_ogg_free(b->codelist);
  265. if(b->dec_index)_ogg_free(b->dec_index);
  266. if(b->dec_codelengths)_ogg_free(b->dec_codelengths);
  267. if(b->dec_firsttable)_ogg_free(b->dec_firsttable);
  268. memset(b,0,sizeof(*b));
  269. }
  270. int vorbis_book_init_encode(codebook *c,const static_codebook *s){
  271. memset(c,0,sizeof(*c));
  272. c->c=s;
  273. c->entries=s->entries;
  274. c->used_entries=s->entries;
  275. c->dim=s->dim;
  276. c->codelist=_make_words(s->lengthlist,s->entries,0);
  277. c->valuelist=_book_unquantize(s,s->entries,NULL);
  278. return(0);
  279. }
  280. static ogg_uint32_t bitreverse(ogg_uint32_t x){
  281. x= ((x>>16)&0x0000ffffUL) | ((x<<16)&0xffff0000UL);
  282. x= ((x>> 8)&0x00ff00ffUL) | ((x<< 8)&0xff00ff00UL);
  283. x= ((x>> 4)&0x0f0f0f0fUL) | ((x<< 4)&0xf0f0f0f0UL);
  284. x= ((x>> 2)&0x33333333UL) | ((x<< 2)&0xccccccccUL);
  285. return((x>> 1)&0x55555555UL) | ((x<< 1)&0xaaaaaaaaUL);
  286. }
  287. static int sort32a(const void *a,const void *b){
  288. return ( **(ogg_uint32_t **)a>**(ogg_uint32_t **)b)-
  289. ( **(ogg_uint32_t **)a<**(ogg_uint32_t **)b);
  290. }
  291. /* decode codebook arrangement is more heavily optimized than encode */
  292. int vorbis_book_init_decode(codebook *c,const static_codebook *s){
  293. int i,j,n=0,tabn;
  294. int *sortindex;
  295. memset(c,0,sizeof(*c));
  296. /* count actually used entries */
  297. for(i=0;i<s->entries;i++)
  298. if(s->lengthlist[i]>0)
  299. n++;
  300. c->entries=s->entries;
  301. c->used_entries=n;
  302. c->dim=s->dim;
  303. if(n>0){
  304. /* two different remappings go on here.
  305. First, we collapse the likely sparse codebook down only to
  306. actually represented values/words. This collapsing needs to be
  307. indexed as map-valueless books are used to encode original entry
  308. positions as integers.
  309. Second, we reorder all vectors, including the entry index above,
  310. by sorted bitreversed codeword to allow treeless decode. */
  311. /* perform sort */
  312. ogg_uint32_t *codes=_make_words(s->lengthlist,s->entries,c->used_entries);
  313. ogg_uint32_t **codep=alloca(sizeof(*codep)*n);
  314. if(codes==NULL)goto err_out;
  315. for(i=0;i<n;i++){
  316. codes[i]=bitreverse(codes[i]);
  317. codep[i]=codes+i;
  318. }
  319. qsort(codep,n,sizeof(*codep),sort32a);
  320. sortindex=alloca(n*sizeof(*sortindex));
  321. c->codelist=_ogg_malloc(n*sizeof(*c->codelist));
  322. /* the index is a reverse index */
  323. for(i=0;i<n;i++){
  324. int position=codep[i]-codes;
  325. sortindex[position]=i;
  326. }
  327. for(i=0;i<n;i++)
  328. c->codelist[sortindex[i]]=codes[i];
  329. _ogg_free(codes);
  330. c->valuelist=_book_unquantize(s,n,sortindex);
  331. c->dec_index=_ogg_malloc(n*sizeof(*c->dec_index));
  332. for(n=0,i=0;i<s->entries;i++)
  333. if(s->lengthlist[i]>0)
  334. c->dec_index[sortindex[n++]]=i;
  335. c->dec_codelengths=_ogg_malloc(n*sizeof(*c->dec_codelengths));
  336. for(n=0,i=0;i<s->entries;i++)
  337. if(s->lengthlist[i]>0)
  338. c->dec_codelengths[sortindex[n++]]=s->lengthlist[i];
  339. c->dec_firsttablen=_ilog(c->used_entries)-4; /* this is magic */
  340. if(c->dec_firsttablen<5)c->dec_firsttablen=5;
  341. if(c->dec_firsttablen>8)c->dec_firsttablen=8;
  342. tabn=1<<c->dec_firsttablen;
  343. c->dec_firsttable=_ogg_calloc(tabn,sizeof(*c->dec_firsttable));
  344. c->dec_maxlength=0;
  345. for(i=0;i<n;i++){
  346. if(c->dec_maxlength<c->dec_codelengths[i])
  347. c->dec_maxlength=c->dec_codelengths[i];
  348. if(c->dec_codelengths[i]<=c->dec_firsttablen){
  349. ogg_uint32_t orig=bitreverse(c->codelist[i]);
  350. for(j=0;j<(1<<(c->dec_firsttablen-c->dec_codelengths[i]));j++)
  351. c->dec_firsttable[orig|(j<<c->dec_codelengths[i])]=i+1;
  352. }
  353. }
  354. /* now fill in 'unused' entries in the firsttable with hi/lo search
  355. hints for the non-direct-hits */
  356. {
  357. ogg_uint32_t mask=0xfffffffeUL<<(31-c->dec_firsttablen);
  358. long lo=0,hi=0;
  359. for(i=0;i<tabn;i++){
  360. ogg_uint32_t word=i<<(32-c->dec_firsttablen);
  361. if(c->dec_firsttable[bitreverse(word)]==0){
  362. while((lo+1)<n && c->codelist[lo+1]<=word)lo++;
  363. while( hi<n && word>=(c->codelist[hi]&mask))hi++;
  364. /* we only actually have 15 bits per hint to play with here.
  365. In order to overflow gracefully (nothing breaks, efficiency
  366. just drops), encode as the difference from the extremes. */
  367. {
  368. unsigned long loval=lo;
  369. unsigned long hival=n-hi;
  370. if(loval>0x7fff)loval=0x7fff;
  371. if(hival>0x7fff)hival=0x7fff;
  372. c->dec_firsttable[bitreverse(word)]=
  373. 0x80000000UL | (loval<<15) | hival;
  374. }
  375. }
  376. }
  377. }
  378. }
  379. return(0);
  380. err_out:
  381. vorbis_book_clear(c);
  382. return(-1);
  383. }
  384. static float _dist(int el,float *ref, float *b,int step){
  385. int i;
  386. float acc=0.f;
  387. for(i=0;i<el;i++){
  388. float val=(ref[i]-b[i*step]);
  389. acc+=val*val;
  390. }
  391. return(acc);
  392. }
  393. int _best(codebook *book, float *a, int step){
  394. encode_aux_threshmatch *tt=book->c->thresh_tree;
  395. #if 0
  396. encode_aux_nearestmatch *nt=book->c->nearest_tree;
  397. encode_aux_pigeonhole *pt=book->c->pigeon_tree;
  398. #endif
  399. int dim=book->dim;
  400. int k,o;
  401. /*int savebest=-1;
  402. float saverr;*/
  403. /* do we have a threshhold encode hint? */
  404. if(tt){
  405. int index=0,i;
  406. /* find the quant val of each scalar */
  407. for(k=0,o=step*(dim-1);k<dim;k++,o-=step){
  408. i=tt->threshvals>>1;
  409. if(a[o]<tt->quantthresh[i]){
  410. for(;i>0;i--)
  411. if(a[o]>=tt->quantthresh[i-1])
  412. break;
  413. }else{
  414. for(i++;i<tt->threshvals-1;i++)
  415. if(a[o]<tt->quantthresh[i])break;
  416. }
  417. index=(index*tt->quantvals)+tt->quantmap[i];
  418. }
  419. /* regular lattices are easy :-) */
  420. if(book->c->lengthlist[index]>0) /* is this unused? If so, we'll
  421. use a decision tree after all
  422. and fall through*/
  423. return(index);
  424. }
  425. #if 0
  426. /* do we have a pigeonhole encode hint? */
  427. if(pt){
  428. const static_codebook *c=book->c;
  429. int i,besti=-1;
  430. float best=0.f;
  431. int entry=0;
  432. /* dealing with sequentialness is a pain in the ass */
  433. if(c->q_sequencep){
  434. int pv;
  435. long mul=1;
  436. float qlast=0;
  437. for(k=0,o=0;k<dim;k++,o+=step){
  438. pv=(int)((a[o]-qlast-pt->min)/pt->del);
  439. if(pv<0 || pv>=pt->mapentries)break;
  440. entry+=pt->pigeonmap[pv]*mul;
  441. mul*=pt->quantvals;
  442. qlast+=pv*pt->del+pt->min;
  443. }
  444. }else{
  445. for(k=0,o=step*(dim-1);k<dim;k++,o-=step){
  446. int pv=(int)((a[o]-pt->min)/pt->del);
  447. if(pv<0 || pv>=pt->mapentries)break;
  448. entry=entry*pt->quantvals+pt->pigeonmap[pv];
  449. }
  450. }
  451. /* must be within the pigeonholable range; if we quant outside (or
  452. in an entry that we define no list for), brute force it */
  453. if(k==dim && pt->fitlength[entry]){
  454. /* search the abbreviated list */
  455. long *list=pt->fitlist+pt->fitmap[entry];
  456. for(i=0;i<pt->fitlength[entry];i++){
  457. float this=_dist(dim,book->valuelist+list[i]*dim,a,step);
  458. if(besti==-1 || this<best){
  459. best=this;
  460. besti=list[i];
  461. }
  462. }
  463. return(besti);
  464. }
  465. }
  466. if(nt){
  467. /* optimized using the decision tree */
  468. while(1){
  469. float c=0.f;
  470. float *p=book->valuelist+nt->p[ptr];
  471. float *q=book->valuelist+nt->q[ptr];
  472. for(k=0,o=0;k<dim;k++,o+=step)
  473. c+=(p[k]-q[k])*(a[o]-(p[k]+q[k])*.5);
  474. if(c>0.f) /* in A */
  475. ptr= -nt->ptr0[ptr];
  476. else /* in B */
  477. ptr= -nt->ptr1[ptr];
  478. if(ptr<=0)break;
  479. }
  480. return(-ptr);
  481. }
  482. #endif
  483. /* brute force it! */
  484. {
  485. const static_codebook *c=book->c;
  486. int i,besti=-1;
  487. float best=0.f;
  488. float *e=book->valuelist;
  489. for(i=0;i<book->entries;i++){
  490. if(c->lengthlist[i]>0){
  491. float this=_dist(dim,e,a,step);
  492. if(besti==-1 || this<best){
  493. best=this;
  494. besti=i;
  495. }
  496. }
  497. e+=dim;
  498. }
  499. /*if(savebest!=-1 && savebest!=besti){
  500. fprintf(stderr,"brute force/pigeonhole disagreement:\n"
  501. "original:");
  502. for(i=0;i<dim*step;i+=step)fprintf(stderr,"%g,",a[i]);
  503. fprintf(stderr,"\n"
  504. "pigeonhole (entry %d, err %g):",savebest,saverr);
  505. for(i=0;i<dim;i++)fprintf(stderr,"%g,",
  506. (book->valuelist+savebest*dim)[i]);
  507. fprintf(stderr,"\n"
  508. "bruteforce (entry %d, err %g):",besti,best);
  509. for(i=0;i<dim;i++)fprintf(stderr,"%g,",
  510. (book->valuelist+besti*dim)[i]);
  511. fprintf(stderr,"\n");
  512. }*/
  513. return(besti);
  514. }
  515. }
  516. long vorbis_book_codeword(codebook *book,int entry){
  517. if(book->c) /* only use with encode; decode optimizations are
  518. allowed to break this */
  519. return book->codelist[entry];
  520. return -1;
  521. }
  522. long vorbis_book_codelen(codebook *book,int entry){
  523. if(book->c) /* only use with encode; decode optimizations are
  524. allowed to break this */
  525. return book->c->lengthlist[entry];
  526. return -1;
  527. }
  528. #ifdef _V_SELFTEST
  529. /* Unit tests of the dequantizer; this stuff will be OK
  530. cross-platform, I simply want to be sure that special mapping cases
  531. actually work properly; a bug could go unnoticed for a while */
  532. #include <stdio.h>
  533. /* cases:
  534. no mapping
  535. full, explicit mapping
  536. algorithmic mapping
  537. nonsequential
  538. sequential
  539. */
  540. static long full_quantlist1[]={0,1,2,3, 4,5,6,7, 8,3,6,1};
  541. static long partial_quantlist1[]={0,7,2};
  542. /* no mapping */
  543. static_codebook test1={
  544. 4,16,
  545. NULL,
  546. 0,
  547. 0,0,0,0,
  548. NULL,
  549. NULL,NULL,NULL,
  550. 0
  551. };
  552. static float *test1_result=NULL;
  553. /* linear, full mapping, nonsequential */
  554. static_codebook test2={
  555. 4,3,
  556. NULL,
  557. 2,
  558. -533200896,1611661312,4,0,
  559. full_quantlist1,
  560. NULL,NULL,NULL,
  561. 0
  562. };
  563. static float test2_result[]={-3,-2,-1,0, 1,2,3,4, 5,0,3,-2};
  564. /* linear, full mapping, sequential */
  565. static_codebook test3={
  566. 4,3,
  567. NULL,
  568. 2,
  569. -533200896,1611661312,4,1,
  570. full_quantlist1,
  571. NULL,NULL,NULL,
  572. 0
  573. };
  574. static float test3_result[]={-3,-5,-6,-6, 1,3,6,10, 5,5,8,6};
  575. /* linear, algorithmic mapping, nonsequential */
  576. static_codebook test4={
  577. 3,27,
  578. NULL,
  579. 1,
  580. -533200896,1611661312,4,0,
  581. partial_quantlist1,
  582. NULL,NULL,NULL,
  583. 0
  584. };
  585. static float test4_result[]={-3,-3,-3, 4,-3,-3, -1,-3,-3,
  586. -3, 4,-3, 4, 4,-3, -1, 4,-3,
  587. -3,-1,-3, 4,-1,-3, -1,-1,-3,
  588. -3,-3, 4, 4,-3, 4, -1,-3, 4,
  589. -3, 4, 4, 4, 4, 4, -1, 4, 4,
  590. -3,-1, 4, 4,-1, 4, -1,-1, 4,
  591. -3,-3,-1, 4,-3,-1, -1,-3,-1,
  592. -3, 4,-1, 4, 4,-1, -1, 4,-1,
  593. -3,-1,-1, 4,-1,-1, -1,-1,-1};
  594. /* linear, algorithmic mapping, sequential */
  595. static_codebook test5={
  596. 3,27,
  597. NULL,
  598. 1,
  599. -533200896,1611661312,4,1,
  600. partial_quantlist1,
  601. NULL,NULL,NULL,
  602. 0
  603. };
  604. static float test5_result[]={-3,-6,-9, 4, 1,-2, -1,-4,-7,
  605. -3, 1,-2, 4, 8, 5, -1, 3, 0,
  606. -3,-4,-7, 4, 3, 0, -1,-2,-5,
  607. -3,-6,-2, 4, 1, 5, -1,-4, 0,
  608. -3, 1, 5, 4, 8,12, -1, 3, 7,
  609. -3,-4, 0, 4, 3, 7, -1,-2, 2,
  610. -3,-6,-7, 4, 1, 0, -1,-4,-5,
  611. -3, 1, 0, 4, 8, 7, -1, 3, 2,
  612. -3,-4,-5, 4, 3, 2, -1,-2,-3};
  613. void run_test(static_codebook *b,float *comp){
  614. float *out=_book_unquantize(b,b->entries,NULL);
  615. int i;
  616. if(comp){
  617. if(!out){
  618. fprintf(stderr,"_book_unquantize incorrectly returned NULL\n");
  619. exit(1);
  620. }
  621. for(i=0;i<b->entries*b->dim;i++)
  622. if(fabs(out[i]-comp[i])>.0001){
  623. fprintf(stderr,"disagreement in unquantized and reference data:\n"
  624. "position %d, %g != %g\n",i,out[i],comp[i]);
  625. exit(1);
  626. }
  627. }else{
  628. if(out){
  629. fprintf(stderr,"_book_unquantize returned a value array: \n"
  630. " correct result should have been NULL\n");
  631. exit(1);
  632. }
  633. }
  634. }
  635. int main(){
  636. /* run the nine dequant tests, and compare to the hand-rolled results */
  637. fprintf(stderr,"Dequant test 1... ");
  638. run_test(&test1,test1_result);
  639. fprintf(stderr,"OK\nDequant test 2... ");
  640. run_test(&test2,test2_result);
  641. fprintf(stderr,"OK\nDequant test 3... ");
  642. run_test(&test3,test3_result);
  643. fprintf(stderr,"OK\nDequant test 4... ");
  644. run_test(&test4,test4_result);
  645. fprintf(stderr,"OK\nDequant test 5... ");
  646. run_test(&test5,test5_result);
  647. fprintf(stderr,"OK\n\n");
  648. return(0);
  649. }
  650. #endif