sharedbook.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  1. /********************************************************************
  2. * *
  3. * THIS FILE IS PART OF THE Ogg Vorbis SOFTWARE CODEC SOURCE CODE. *
  4. * USE, DISTRIBUTION AND REPRODUCTION OF THIS SOURCE IS GOVERNED BY *
  5. * THE GNU PUBLIC LICENSE 2, WHICH IS INCLUDED WITH THIS SOURCE. *
  6. * PLEASE READ THESE TERMS DISTRIBUTING. *
  7. * *
  8. * THE OggSQUISH SOURCE CODE IS (C) COPYRIGHT 1994-2000 *
  9. * by Monty <monty@xiph.org> and The XIPHOPHORUS Company *
  10. * http://www.xiph.org/ *
  11. * *
  12. ********************************************************************
  13. function: basic shared codebook operations
  14. last mod: $Id: sharedbook.c,v 1.1.2.6 2000/04/26 07:10:15 xiphmont Exp $
  15. ********************************************************************/
  16. #include <stdlib.h>
  17. #include <math.h>
  18. #include "vorbis/codec.h"
  19. #include "vorbis/codebook.h"
  20. #include "bitwise.h"
  21. #include "scales.h"
  22. #include "sharedbook.h"
  23. /**** pack/unpack helpers ******************************************/
  24. int _ilog(unsigned int v){
  25. int ret=0;
  26. while(v){
  27. ret++;
  28. v>>=1;
  29. }
  30. return(ret);
  31. }
  32. /* 32 bit float (not IEEE; nonnormalized mantissa +
  33. biased exponent) : neeeeeee eeemmmmm mmmmmmmm mmmmmmmm
  34. Why not IEEE? It's just not that important here. */
  35. #define VQ_FEXP 10
  36. #define VQ_FMAN 21
  37. #define VQ_FEXP_BIAS 768 /* bias toward values smaller than 1. */
  38. /* doesn't currently guard under/overflow */
  39. long _float32_pack(double val){
  40. int sign=0;
  41. long exp;
  42. long mant;
  43. if(val<0){
  44. sign=0x80000000;
  45. val= -val;
  46. }
  47. exp= floor(log(val)/log(2));
  48. mant=rint(ldexp(val,(VQ_FMAN-1)-exp));
  49. exp=(exp+VQ_FEXP_BIAS)<<VQ_FMAN;
  50. return(sign|exp|mant);
  51. }
  52. double _float32_unpack(long val){
  53. double mant=val&0x1fffff;
  54. double sign=val&0x80000000;
  55. double exp =(val&0x7fe00000)>>VQ_FMAN;
  56. if(sign)mant= -mant;
  57. return(ldexp(mant,exp-(VQ_FMAN-1)-VQ_FEXP_BIAS));
  58. }
  59. /* given a list of word lengths, generate a list of codewords. Works
  60. for length ordered or unordered, always assigns the lowest valued
  61. codewords first. Extended to handle unused entries (length 0) */
  62. long *_make_words(long *l,long n){
  63. long i,j;
  64. long marker[33];
  65. long *r=malloc(n*sizeof(long));
  66. memset(marker,0,sizeof(marker));
  67. for(i=0;i<n;i++){
  68. long length=l[i];
  69. if(length>0){
  70. long entry=marker[length];
  71. /* when we claim a node for an entry, we also claim the nodes
  72. below it (pruning off the imagined tree that may have dangled
  73. from it) as well as blocking the use of any nodes directly
  74. above for leaves */
  75. /* update ourself */
  76. if(length<32 && (entry>>length)){
  77. /* error condition; the lengths must specify an overpopulated tree */
  78. free(r);
  79. return(NULL);
  80. }
  81. r[i]=entry;
  82. /* Look to see if the next shorter marker points to the node
  83. above. if so, update it and repeat. */
  84. {
  85. for(j=length;j>0;j--){
  86. if(marker[j]&1){
  87. /* have to jump branches */
  88. if(j==1)
  89. marker[1]++;
  90. else
  91. marker[j]=marker[j-1]<<1;
  92. break; /* invariant says next upper marker would already
  93. have been moved if it was on the same path */
  94. }
  95. marker[j]++;
  96. }
  97. }
  98. /* prune the tree; the implicit invariant says all the longer
  99. markers were dangling from our just-taken node. Dangle them
  100. from our *new* node. */
  101. for(j=length+1;j<33;j++)
  102. if((marker[j]>>1) == entry){
  103. entry=marker[j];
  104. marker[j]=marker[j-1]<<1;
  105. }else
  106. break;
  107. }
  108. }
  109. /* bitreverse the words because our bitwise packer/unpacker is LSb
  110. endian */
  111. for(i=0;i<n;i++){
  112. long temp=0;
  113. for(j=0;j<l[i];j++){
  114. temp<<=1;
  115. temp|=(r[i]>>j)&1;
  116. }
  117. r[i]=temp;
  118. }
  119. return(r);
  120. }
  121. /* build the decode helper tree from the codewords */
  122. decode_aux *_make_decode_tree(codebook *c){
  123. const static_codebook *s=c->c;
  124. long top=0,i,j;
  125. decode_aux *t=malloc(sizeof(decode_aux));
  126. long *ptr0=t->ptr0=calloc(c->entries*2,sizeof(long));
  127. long *ptr1=t->ptr1=calloc(c->entries*2,sizeof(long));
  128. long *codelist=_make_words(s->lengthlist,s->entries);
  129. if(codelist==NULL)return(NULL);
  130. t->aux=c->entries*2;
  131. for(i=0;i<c->entries;i++){
  132. if(s->lengthlist[i]>0){
  133. long ptr=0;
  134. for(j=0;j<s->lengthlist[i]-1;j++){
  135. int bit=(codelist[i]>>j)&1;
  136. if(!bit){
  137. if(!ptr0[ptr])
  138. ptr0[ptr]= ++top;
  139. ptr=ptr0[ptr];
  140. }else{
  141. if(!ptr1[ptr])
  142. ptr1[ptr]= ++top;
  143. ptr=ptr1[ptr];
  144. }
  145. }
  146. if(!((codelist[i]>>j)&1))
  147. ptr0[ptr]=-i;
  148. else
  149. ptr1[ptr]=-i;
  150. }
  151. }
  152. free(codelist);
  153. return(t);
  154. }
  155. /* there might be a straightforward one-line way to do the below
  156. that's portable and totally safe against roundoff, but I haven't
  157. thought of it. Therefore, we opt on the side of caution */
  158. long _book_maptype1_quantvals(const static_codebook *b){
  159. long vals=floor(pow(b->entries,1./b->dim));
  160. /* the above *should* be reliable, but we'll not assume that FP is
  161. ever reliable when bitstream sync is at stake; verify via integer
  162. means that vals really is the greatest value of dim for which
  163. vals^b->bim <= b->entries */
  164. /* treat the above as an initial guess */
  165. while(1){
  166. long acc=1;
  167. long acc1=1;
  168. int i;
  169. for(i=0;i<b->dim;i++){
  170. acc*=vals;
  171. acc1*=vals+1;
  172. }
  173. if(acc<=b->entries && acc1>b->entries){
  174. return(vals);
  175. }else{
  176. if(acc>b->entries){
  177. vals--;
  178. }else{
  179. vals++;
  180. }
  181. }
  182. }
  183. }
  184. /* unpack the quantized list of values for encode/decode ***********/
  185. /* we need to deal with two map types: in map type 1, the values are
  186. generated algorithmically (each column of the vector counts through
  187. the values in the quant vector). in map type 2, all the values came
  188. in in an explicit list. Both value lists must be unpacked */
  189. double *_book_unquantize(const static_codebook *b){
  190. long j,k;
  191. if(b->maptype==1 || b->maptype==2){
  192. int quantvals;
  193. double mindel=_float32_unpack(b->q_min);
  194. double delta=_float32_unpack(b->q_delta);
  195. double *r=calloc(b->entries*b->dim,sizeof(double));
  196. /* maptype 1 and 2 both use a quantized value vector, but
  197. different sizes */
  198. switch(b->maptype){
  199. case 1:
  200. /* most of the time, entries%dimensions == 0, but we need to be
  201. well defined. We define that the possible vales at each
  202. scalar is values == entries/dim. If entries%dim != 0, we'll
  203. have 'too few' values (values*dim<entries), which means that
  204. we'll have 'left over' entries; left over entries use zeroed
  205. values (and are wasted). So don't generate codebooks like
  206. that */
  207. quantvals=_book_maptype1_quantvals(b);
  208. for(j=0;j<b->entries;j++){
  209. double last=0.;
  210. int indexdiv=1;
  211. for(k=0;k<b->dim;k++){
  212. int index= (j/indexdiv)%quantvals;
  213. double val=b->quantlist[index];
  214. val=fabs(val)*delta+mindel+last;
  215. if(b->q_sequencep)last=val;
  216. r[j*b->dim+k]=val;
  217. indexdiv*=quantvals;
  218. }
  219. }
  220. break;
  221. case 2:
  222. for(j=0;j<b->entries;j++){
  223. double last=0.;
  224. for(k=0;k<b->dim;k++){
  225. double val=b->quantlist[j*b->dim+k];
  226. val=fabs(val)*delta+mindel+last;
  227. if(b->q_sequencep)last=val;
  228. r[j*b->dim+k]=val;
  229. }
  230. }
  231. }
  232. return(r);
  233. }
  234. return(NULL);
  235. }
  236. void vorbis_staticbook_clear(static_codebook *b){
  237. if(b->quantlist)free(b->quantlist);
  238. if(b->lengthlist)free(b->lengthlist);
  239. if(b->nearest_tree){
  240. free(b->nearest_tree->ptr0);
  241. free(b->nearest_tree->ptr1);
  242. free(b->nearest_tree->p);
  243. free(b->nearest_tree->q);
  244. memset(b->nearest_tree,0,sizeof(encode_aux_nearestmatch));
  245. free(b->nearest_tree);
  246. }
  247. if(b->thresh_tree){
  248. free(b->thresh_tree->quantthresh);
  249. free(b->thresh_tree->quantmap);
  250. memset(b->thresh_tree,0,sizeof(encode_aux_threshmatch));
  251. free(b->thresh_tree);
  252. }
  253. memset(b,0,sizeof(static_codebook));
  254. }
  255. void vorbis_book_clear(codebook *b){
  256. /* static book is not cleared; we're likely called on the lookup and
  257. the static codebook belongs to the info struct */
  258. if(b->decode_tree){
  259. free(b->decode_tree->ptr0);
  260. free(b->decode_tree->ptr1);
  261. memset(b->decode_tree,0,sizeof(decode_aux));
  262. free(b->decode_tree);
  263. }
  264. if(b->valuelist)free(b->valuelist);
  265. if(b->codelist)free(b->codelist);
  266. memset(b,0,sizeof(codebook));
  267. }
  268. int vorbis_book_init_encode(codebook *c,const static_codebook *s){
  269. memset(c,0,sizeof(codebook));
  270. c->c=s;
  271. c->entries=s->entries;
  272. c->dim=s->dim;
  273. c->codelist=_make_words(s->lengthlist,s->entries);
  274. c->valuelist=_book_unquantize(s);
  275. return(0);
  276. }
  277. int vorbis_book_init_decode(codebook *c,const static_codebook *s){
  278. memset(c,0,sizeof(codebook));
  279. c->c=s;
  280. c->entries=s->entries;
  281. c->dim=s->dim;
  282. c->valuelist=_book_unquantize(s);
  283. c->decode_tree=_make_decode_tree(c);
  284. if(c->decode_tree==NULL)goto err_out;
  285. return(0);
  286. err_out:
  287. vorbis_book_clear(c);
  288. return(-1);
  289. }
  290. int _best(codebook *book, double *a, int step){
  291. encode_aux_nearestmatch *nt=book->c->nearest_tree;
  292. encode_aux_threshmatch *tt=book->c->thresh_tree;
  293. int dim=book->dim;
  294. int ptr=0,k,o;
  295. /* we assume for now that a thresh tree is the only other possibility */
  296. if(tt){
  297. int index=0;
  298. /* find the quant val of each scalar */
  299. for(k=0,o=step*(dim-1);k<dim;k++,o-=step){
  300. int i;
  301. /* linear search the quant list for now; it's small and although
  302. with > 8 entries, it would be faster to bisect, this would be
  303. a misplaced optimization for now */
  304. for(i=0;i<tt->threshvals-1;i++)
  305. if(a[o]<tt->quantthresh[i])break;
  306. index=(index*tt->quantvals)+tt->quantmap[i];
  307. }
  308. /* regular lattices are easy :-) */
  309. if(book->c->lengthlist[index]>0) /* is this unused? If so, we'll
  310. use a decision tree after all
  311. and fall through*/
  312. return(index);
  313. }
  314. if(nt){
  315. /* optimized using the decision tree */
  316. while(1){
  317. double c=0.;
  318. double *p=book->valuelist+nt->p[ptr];
  319. double *q=book->valuelist+nt->q[ptr];
  320. for(k=0,o=0;k<dim;k++,o+=step)
  321. c+=(p[k]-q[k])*(a[o]-(p[k]+q[k])*.5);
  322. if(c>0.) /* in A */
  323. ptr= -nt->ptr0[ptr];
  324. else /* in B */
  325. ptr= -nt->ptr1[ptr];
  326. if(ptr<=0)break;
  327. }
  328. return(-ptr);
  329. }
  330. return(-1);
  331. }
  332. static double _dist(int el,double *a, double *b){
  333. int i;
  334. double acc=0.;
  335. for(i=0;i<el;i++){
  336. double val=(a[i]-b[i]);
  337. acc+=val*val;
  338. }
  339. return(acc);
  340. }
  341. /* returns the entry number and *modifies a* to the remainder value ********/
  342. int vorbis_book_besterror(codebook *book,double *a,int step,int addmul){
  343. int dim=book->dim,i,o;
  344. int best=_best(book,a,step);
  345. switch(addmul){
  346. case 0:
  347. for(i=0,o=0;i<dim;i++,o+=step)
  348. a[o]-=(book->valuelist+best*dim)[i];
  349. break;
  350. case 1:
  351. for(i=0,o=0;i<dim;i++,o+=step){
  352. double val=(book->valuelist+best*dim)[i];
  353. if(val==0){
  354. a[o]=0;
  355. }else{
  356. a[o]/=val;
  357. }
  358. }
  359. break;
  360. }
  361. return(best);
  362. }
  363. long vorbis_book_codeword(codebook *book,int entry){
  364. return book->codelist[entry];
  365. }
  366. long vorbis_book_codelen(codebook *book,int entry){
  367. return book->c->lengthlist[entry];
  368. }
  369. #ifdef _V_SELFTEST
  370. /* Unit tests of the dequantizer; this stuff will be OK
  371. cross-platform, I simply want to be sure that special mapping cases
  372. actually work properly; a bug could go unnoticed for a while */
  373. #include <stdio.h>
  374. /* cases:
  375. no mapping
  376. full, explicit mapping
  377. algorithmic mapping
  378. nonsequential
  379. sequential
  380. */
  381. static long full_quantlist1[]={0,1,2,3, 4,5,6,7, 8,3,6,1};
  382. static long partial_quantlist1[]={0,7,2};
  383. /* no mapping */
  384. static_codebook test1={
  385. 4,16,
  386. NULL,
  387. 0,
  388. 0,0,0,0,
  389. NULL,
  390. NULL,NULL
  391. };
  392. static double *test1_result=NULL;
  393. /* linear, full mapping, nonsequential */
  394. static_codebook test2={
  395. 4,3,
  396. NULL,
  397. 2,
  398. -533200896,1611661312,4,0,
  399. full_quantlist1,
  400. NULL,NULL
  401. };
  402. static double test2_result[]={-3,-2,-1,0, 1,2,3,4, 5,0,3,-2};
  403. /* linear, full mapping, sequential */
  404. static_codebook test3={
  405. 4,3,
  406. NULL,
  407. 2,
  408. -533200896,1611661312,4,1,
  409. full_quantlist1,
  410. NULL,NULL
  411. };
  412. static double test3_result[]={-3,-5,-6,-6, 1,3,6,10, 5,5,8,6};
  413. /* linear, algorithmic mapping, nonsequential */
  414. static_codebook test4={
  415. 3,27,
  416. NULL,
  417. 1,
  418. -533200896,1611661312,4,0,
  419. partial_quantlist1,
  420. NULL,NULL
  421. };
  422. static double test4_result[]={-3,-3,-3, 4,-3,-3, -1,-3,-3,
  423. -3, 4,-3, 4, 4,-3, -1, 4,-3,
  424. -3,-1,-3, 4,-1,-3, -1,-1,-3,
  425. -3,-3, 4, 4,-3, 4, -1,-3, 4,
  426. -3, 4, 4, 4, 4, 4, -1, 4, 4,
  427. -3,-1, 4, 4,-1, 4, -1,-1, 4,
  428. -3,-3,-1, 4,-3,-1, -1,-3,-1,
  429. -3, 4,-1, 4, 4,-1, -1, 4,-1,
  430. -3,-1,-1, 4,-1,-1, -1,-1,-1};
  431. /* linear, algorithmic mapping, sequential */
  432. static_codebook test5={
  433. 3,27,
  434. NULL,
  435. 1,
  436. -533200896,1611661312,4,1,
  437. partial_quantlist1,
  438. NULL,NULL
  439. };
  440. static double test5_result[]={-3,-6,-9, 4, 1,-2, -1,-4,-7,
  441. -3, 1,-2, 4, 8, 5, -1, 3, 0,
  442. -3,-4,-7, 4, 3, 0, -1,-2,-5,
  443. -3,-6,-2, 4, 1, 5, -1,-4, 0,
  444. -3, 1, 5, 4, 8,12, -1, 3, 7,
  445. -3,-4, 0, 4, 3, 7, -1,-2, 2,
  446. -3,-6,-7, 4, 1, 0, -1,-4,-5,
  447. -3, 1, 0, 4, 8, 7, -1, 3, 2,
  448. -3,-4,-5, 4, 3, 2, -1,-2,-3};
  449. void run_test(static_codebook *b,double *comp){
  450. double *out=_book_unquantize(b);
  451. int i;
  452. if(comp){
  453. if(!out){
  454. fprintf(stderr,"_book_unquantize incorrectly returned NULL\n");
  455. exit(1);
  456. }
  457. for(i=0;i<b->entries*b->dim;i++)
  458. if(fabs(out[i]-comp[i])>.0001){
  459. fprintf(stderr,"disagreement in unquantized and reference data:\n"
  460. "position %d, %g != %g\n",i,out[i],comp[i]);
  461. exit(1);
  462. }
  463. }else{
  464. if(out){
  465. fprintf(stderr,"_book_unquantize returned a value array: \n"
  466. " correct result should have been NULL\n");
  467. exit(1);
  468. }
  469. }
  470. }
  471. int main(){
  472. /* run the nine dequant tests, and compare to the hand-rolled results */
  473. fprintf(stderr,"Dequant test 1... ");
  474. run_test(&test1,test1_result);
  475. fprintf(stderr,"OK\nDequant test 2... ");
  476. run_test(&test2,test2_result);
  477. fprintf(stderr,"OK\nDequant test 3... ");
  478. run_test(&test3,test3_result);
  479. fprintf(stderr,"OK\nDequant test 4... ");
  480. run_test(&test4,test4_result);
  481. fprintf(stderr,"OK\nDequant test 5... ");
  482. run_test(&test5,test5_result);
  483. fprintf(stderr,"OK\n\n");
  484. return(0);
  485. }
  486. #endif