1 /********************************************************************
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. *
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/ *
12 ********************************************************************
14 function: basic shared codebook operations
15 last mod: $Id: sharedbook.c,v 1.2 2000/05/08 20:49:49 xiphmont Exp $
17 ********************************************************************/
21 #include "vorbis/codec.h"
22 #include "vorbis/codebook.h"
25 #include "sharedbook.h"
27 /**** pack/unpack helpers ******************************************/
28 int _ilog(unsigned int v){
37 /* 32 bit float (not IEEE; nonnormalized mantissa +
38 biased exponent) : neeeeeee eeemmmmm mmmmmmmm mmmmmmmm
39 Why not IEEE? It's just not that important here. */
43 #define VQ_FEXP_BIAS 768 /* bias toward values smaller than 1. */
45 /* doesn't currently guard under/overflow */
46 long _float32_pack(double val){
54 exp= floor(log(val)/log(2));
55 mant=rint(ldexp(val,(VQ_FMAN-1)-exp));
56 exp=(exp+VQ_FEXP_BIAS)<<VQ_FMAN;
58 return(sign|exp|mant);
61 double _float32_unpack(long val){
62 double mant=val&0x1fffff;
63 double sign=val&0x80000000;
64 double exp =(val&0x7fe00000)>>VQ_FMAN;
66 return(ldexp(mant,exp-(VQ_FMAN-1)-VQ_FEXP_BIAS));
69 /* given a list of word lengths, generate a list of codewords. Works
70 for length ordered or unordered, always assigns the lowest valued
71 codewords first. Extended to handle unused entries (length 0) */
72 long *_make_words(long *l,long n){
75 long *r=malloc(n*sizeof(long));
76 memset(marker,0,sizeof(marker));
81 long entry=marker[length];
83 /* when we claim a node for an entry, we also claim the nodes
84 below it (pruning off the imagined tree that may have dangled
85 from it) as well as blocking the use of any nodes directly
89 if(length<32 && (entry>>length)){
90 /* error condition; the lengths must specify an overpopulated tree */
96 /* Look to see if the next shorter marker points to the node
97 above. if so, update it and repeat. */
99 for(j=length;j>0;j--){
102 /* have to jump branches */
106 marker[j]=marker[j-1]<<1;
107 break; /* invariant says next upper marker would already
108 have been moved if it was on the same path */
114 /* prune the tree; the implicit invariant says all the longer
115 markers were dangling from our just-taken node. Dangle them
116 from our *new* node. */
117 for(j=length+1;j<33;j++)
118 if((marker[j]>>1) == entry){
120 marker[j]=marker[j-1]<<1;
126 /* bitreverse the words because our bitwise packer/unpacker is LSb
140 /* build the decode helper tree from the codewords */
141 decode_aux *_make_decode_tree(codebook *c){
142 const static_codebook *s=c->c;
144 decode_aux *t=malloc(sizeof(decode_aux));
145 long *ptr0=t->ptr0=calloc(c->entries*2,sizeof(long));
146 long *ptr1=t->ptr1=calloc(c->entries*2,sizeof(long));
147 long *codelist=_make_words(s->lengthlist,s->entries);
149 if(codelist==NULL)return(NULL);
152 for(i=0;i<c->entries;i++){
153 if(s->lengthlist[i]>0){
155 for(j=0;j<s->lengthlist[i]-1;j++){
156 int bit=(codelist[i]>>j)&1;
167 if(!((codelist[i]>>j)&1))
177 /* there might be a straightforward one-line way to do the below
178 that's portable and totally safe against roundoff, but I haven't
179 thought of it. Therefore, we opt on the side of caution */
180 long _book_maptype1_quantvals(const static_codebook *b){
181 long vals=floor(pow(b->entries,1./b->dim));
183 /* the above *should* be reliable, but we'll not assume that FP is
184 ever reliable when bitstream sync is at stake; verify via integer
185 means that vals really is the greatest value of dim for which
186 vals^b->bim <= b->entries */
187 /* treat the above as an initial guess */
192 for(i=0;i<b->dim;i++){
196 if(acc<=b->entries && acc1>b->entries){
208 /* unpack the quantized list of values for encode/decode ***********/
209 /* we need to deal with two map types: in map type 1, the values are
210 generated algorithmically (each column of the vector counts through
211 the values in the quant vector). in map type 2, all the values came
212 in in an explicit list. Both value lists must be unpacked */
213 double *_book_unquantize(const static_codebook *b){
215 if(b->maptype==1 || b->maptype==2){
217 double mindel=_float32_unpack(b->q_min);
218 double delta=_float32_unpack(b->q_delta);
219 double *r=calloc(b->entries*b->dim,sizeof(double));
221 /* maptype 1 and 2 both use a quantized value vector, but
225 /* most of the time, entries%dimensions == 0, but we need to be
226 well defined. We define that the possible vales at each
227 scalar is values == entries/dim. If entries%dim != 0, we'll
228 have 'too few' values (values*dim<entries), which means that
229 we'll have 'left over' entries; left over entries use zeroed
230 values (and are wasted). So don't generate codebooks like
232 quantvals=_book_maptype1_quantvals(b);
233 for(j=0;j<b->entries;j++){
236 for(k=0;k<b->dim;k++){
237 int index= (j/indexdiv)%quantvals;
238 double val=b->quantlist[index];
239 val=fabs(val)*delta+mindel+last;
240 if(b->q_sequencep)last=val;
247 for(j=0;j<b->entries;j++){
249 for(k=0;k<b->dim;k++){
250 double val=b->quantlist[j*b->dim+k];
251 val=fabs(val)*delta+mindel+last;
252 if(b->q_sequencep)last=val;
262 void vorbis_staticbook_clear(static_codebook *b){
263 if(b->quantlist)free(b->quantlist);
264 if(b->lengthlist)free(b->lengthlist);
266 free(b->nearest_tree->ptr0);
267 free(b->nearest_tree->ptr1);
268 free(b->nearest_tree->p);
269 free(b->nearest_tree->q);
270 memset(b->nearest_tree,0,sizeof(encode_aux_nearestmatch));
271 free(b->nearest_tree);
274 free(b->thresh_tree->quantthresh);
275 free(b->thresh_tree->quantmap);
276 memset(b->thresh_tree,0,sizeof(encode_aux_threshmatch));
277 free(b->thresh_tree);
279 memset(b,0,sizeof(static_codebook));
282 void vorbis_book_clear(codebook *b){
283 /* static book is not cleared; we're likely called on the lookup and
284 the static codebook belongs to the info struct */
286 free(b->decode_tree->ptr0);
287 free(b->decode_tree->ptr1);
288 memset(b->decode_tree,0,sizeof(decode_aux));
289 free(b->decode_tree);
291 if(b->valuelist)free(b->valuelist);
292 if(b->codelist)free(b->codelist);
293 memset(b,0,sizeof(codebook));
296 int vorbis_book_init_encode(codebook *c,const static_codebook *s){
297 memset(c,0,sizeof(codebook));
299 c->entries=s->entries;
301 c->codelist=_make_words(s->lengthlist,s->entries);
302 c->valuelist=_book_unquantize(s);
306 int vorbis_book_init_decode(codebook *c,const static_codebook *s){
307 memset(c,0,sizeof(codebook));
309 c->entries=s->entries;
311 c->valuelist=_book_unquantize(s);
312 c->decode_tree=_make_decode_tree(c);
313 if(c->decode_tree==NULL)goto err_out;
316 vorbis_book_clear(c);
320 int _best(codebook *book, double *a, int step){
321 encode_aux_nearestmatch *nt=book->c->nearest_tree;
322 encode_aux_threshmatch *tt=book->c->thresh_tree;
326 /* we assume for now that a thresh tree is the only other possibility */
329 /* find the quant val of each scalar */
330 for(k=0,o=step*(dim-1);k<dim;k++,o-=step){
332 /* linear search the quant list for now; it's small and although
333 with > 8 entries, it would be faster to bisect, this would be
334 a misplaced optimization for now */
335 for(i=0;i<tt->threshvals-1;i++)
336 if(a[o]<tt->quantthresh[i])break;
338 index=(index*tt->quantvals)+tt->quantmap[i];
340 /* regular lattices are easy :-) */
341 if(book->c->lengthlist[index]>0) /* is this unused? If so, we'll
342 use a decision tree after all
348 /* optimized using the decision tree */
351 double *p=book->valuelist+nt->p[ptr];
352 double *q=book->valuelist+nt->q[ptr];
354 for(k=0,o=0;k<dim;k++,o+=step)
355 c+=(p[k]-q[k])*(a[o]-(p[k]+q[k])*.5);
369 static double _dist(int el,double *a, double *b){
373 double val=(a[i]-b[i]);
379 /* returns the entry number and *modifies a* to the remainder value ********/
380 int vorbis_book_besterror(codebook *book,double *a,int step,int addmul){
381 int dim=book->dim,i,o;
382 int best=_best(book,a,step);
385 for(i=0,o=0;i<dim;i++,o+=step)
386 a[o]-=(book->valuelist+best*dim)[i];
389 for(i=0,o=0;i<dim;i++,o+=step){
390 double val=(book->valuelist+best*dim)[i];
402 long vorbis_book_codeword(codebook *book,int entry){
403 return book->codelist[entry];
406 long vorbis_book_codelen(codebook *book,int entry){
407 return book->c->lengthlist[entry];
412 /* Unit tests of the dequantizer; this stuff will be OK
413 cross-platform, I simply want to be sure that special mapping cases
414 actually work properly; a bug could go unnoticed for a while */
421 full, explicit mapping
428 static long full_quantlist1[]={0,1,2,3, 4,5,6,7, 8,3,6,1};
429 static long partial_quantlist1[]={0,7,2};
432 static_codebook test1={
440 static double *test1_result=NULL;
442 /* linear, full mapping, nonsequential */
443 static_codebook test2={
447 -533200896,1611661312,4,0,
451 static double test2_result[]={-3,-2,-1,0, 1,2,3,4, 5,0,3,-2};
453 /* linear, full mapping, sequential */
454 static_codebook test3={
458 -533200896,1611661312,4,1,
462 static double test3_result[]={-3,-5,-6,-6, 1,3,6,10, 5,5,8,6};
464 /* linear, algorithmic mapping, nonsequential */
465 static_codebook test4={
469 -533200896,1611661312,4,0,
473 static double test4_result[]={-3,-3,-3, 4,-3,-3, -1,-3,-3,
474 -3, 4,-3, 4, 4,-3, -1, 4,-3,
475 -3,-1,-3, 4,-1,-3, -1,-1,-3,
476 -3,-3, 4, 4,-3, 4, -1,-3, 4,
477 -3, 4, 4, 4, 4, 4, -1, 4, 4,
478 -3,-1, 4, 4,-1, 4, -1,-1, 4,
479 -3,-3,-1, 4,-3,-1, -1,-3,-1,
480 -3, 4,-1, 4, 4,-1, -1, 4,-1,
481 -3,-1,-1, 4,-1,-1, -1,-1,-1};
483 /* linear, algorithmic mapping, sequential */
484 static_codebook test5={
488 -533200896,1611661312,4,1,
492 static double test5_result[]={-3,-6,-9, 4, 1,-2, -1,-4,-7,
493 -3, 1,-2, 4, 8, 5, -1, 3, 0,
494 -3,-4,-7, 4, 3, 0, -1,-2,-5,
495 -3,-6,-2, 4, 1, 5, -1,-4, 0,
496 -3, 1, 5, 4, 8,12, -1, 3, 7,
497 -3,-4, 0, 4, 3, 7, -1,-2, 2,
498 -3,-6,-7, 4, 1, 0, -1,-4,-5,
499 -3, 1, 0, 4, 8, 7, -1, 3, 2,
500 -3,-4,-5, 4, 3, 2, -1,-2,-3};
502 void run_test(static_codebook *b,double *comp){
503 double *out=_book_unquantize(b);
508 fprintf(stderr,"_book_unquantize incorrectly returned NULL\n");
512 for(i=0;i<b->entries*b->dim;i++)
513 if(fabs(out[i]-comp[i])>.0001){
514 fprintf(stderr,"disagreement in unquantized and reference data:\n"
515 "position %d, %g != %g\n",i,out[i],comp[i]);
521 fprintf(stderr,"_book_unquantize returned a value array: \n"
522 " correct result should have been NULL\n");
529 /* run the nine dequant tests, and compare to the hand-rolled results */
530 fprintf(stderr,"Dequant test 1... ");
531 run_test(&test1,test1_result);
532 fprintf(stderr,"OK\nDequant test 2... ");
533 run_test(&test2,test2_result);
534 fprintf(stderr,"OK\nDequant test 3... ");
535 run_test(&test3,test3_result);
536 fprintf(stderr,"OK\nDequant test 4... ");
537 run_test(&test4,test4_result);
538 fprintf(stderr,"OK\nDequant test 5... ");
539 run_test(&test5,test5_result);
540 fprintf(stderr,"OK\n\n");