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 codebook pack/unpack/code/decode operations
15 last mod: $Id: codebook.c,v 1.13 2000/04/03 08:30:49 xiphmont Exp $
17 ********************************************************************/
22 #include "vorbis/codec.h"
23 #include "vorbis/codebook.h"
25 #include "bookinternal.h"
28 /**** pack/unpack helpers ******************************************/
29 static int ilog(unsigned int v){
38 /* code that packs the 24 bit float can be found in vq/bookutil.c */
40 static double _float24_unpack(long val){
41 double mant=val&0x3ffff;
42 double sign=val&0x800000;
43 double exp =(val&0x7c0000)>>18;
45 return(ldexp(mant,exp-17-VQ_FEXP_BIAS));
48 /* given a list of word lengths, generate a list of codewords. Works
49 for length ordered or unordered, always assigns the lowest valued
51 long *_make_words(long *l,long n){
54 long *r=malloc(n*sizeof(long));
55 memset(marker,0,sizeof(marker));
59 long entry=marker[length];
61 /* when we claim a node for an entry, we also claim the nodes
62 below it (pruning off the imagined tree that may have dangled
63 from it) as well as blocking the use of any nodes directly
67 if(length<32 && (entry>>length)){
68 /* error condition; the lengths must specify an overpopulated tree */
74 /* Look to see if the next shorter marker points to the node
75 above. if so, update it and repeat. */
77 for(j=length;j>0;j--){
80 /* have to jump branches */
84 marker[j]=marker[j-1]<<1;
85 break; /* invariant says next upper marker would already
86 have been moved if it was on the same path */
92 /* prune the tree; the implicit invariant says all the longer
93 markers were dangling from our just-taken node. Dangle them
94 from our *new* node. */
95 for(j=length+1;j<33;j++)
96 if((marker[j]>>1) == entry){
98 marker[j]=marker[j-1]<<1;
103 /* bitreverse the words because our bitwise packer/unpacker is LSb
117 /* build the decode helper tree from the codewords */
118 decode_aux *_make_decode_tree(codebook *c){
119 const static_codebook *s=c->c;
121 decode_aux *t=malloc(sizeof(decode_aux));
122 long *ptr0=t->ptr0=calloc(c->entries*2,sizeof(long));
123 long *ptr1=t->ptr1=calloc(c->entries*2,sizeof(long));
124 long *codelist=_make_words(s->lengthlist,s->entries);
126 if(codelist==NULL)return(NULL);
129 for(i=0;i<c->entries;i++){
131 for(j=0;j<s->lengthlist[i]-1;j++){
132 int bit=(codelist[i]>>j)&1;
143 if(!((codelist[i]>>j)&1))
152 /* unpack the quantized list of values for encode/decode ***********/
153 static double *_book_unquantize(const static_codebook *b){
156 double mindel=_float24_unpack(b->q_min);
157 double delta=_float24_unpack(b->q_delta);
158 double *r=malloc(sizeof(double)*b->entries*b->dim);
160 for(j=0;j<b->entries;j++){
162 for(k=0;k<b->dim;k++){
163 double val=b->quantlist[j*b->dim+k]*delta+last+mindel;
165 if(b->q_sequencep)last=val;
173 void vorbis_staticbook_clear(static_codebook *b){
174 if(b->quantlist)free(b->quantlist);
175 if(b->lengthlist)free(b->lengthlist);
177 free(b->encode_tree->ptr0);
178 free(b->encode_tree->ptr1);
179 free(b->encode_tree->p);
180 free(b->encode_tree->q);
181 memset(b->encode_tree,0,sizeof(encode_aux));
182 free(b->encode_tree);
184 memset(b,0,sizeof(static_codebook));
187 void vorbis_book_clear(codebook *b){
188 /* static book is not cleared; we're likely called on the lookup and
189 the static codebook belongs to the info struct */
191 free(b->decode_tree->ptr0);
192 free(b->decode_tree->ptr1);
193 memset(b->decode_tree,0,sizeof(decode_aux));
194 free(b->decode_tree);
196 if(b->valuelist)free(b->valuelist);
197 if(b->codelist)free(b->codelist);
198 memset(b,0,sizeof(codebook));
201 int vorbis_book_init_encode(codebook *c,const static_codebook *s){
202 memset(c,0,sizeof(codebook));
204 c->entries=s->entries;
206 c->codelist=_make_words(s->lengthlist,s->entries);
207 c->valuelist=_book_unquantize(s);
211 int vorbis_book_init_decode(codebook *c,const static_codebook *s){
212 memset(c,0,sizeof(codebook));
214 c->entries=s->entries;
216 c->valuelist=_book_unquantize(s);
217 c->decode_tree=_make_decode_tree(c);
218 if(c->decode_tree==NULL)goto err_out;
221 vorbis_book_clear(c);
225 /* packs the given codebook into the bitstream **************************/
227 int vorbis_staticbook_pack(const static_codebook *c,oggpack_buffer *opb){
231 /* first the basic parameters */
232 _oggpack_write(opb,0x564342,24);
233 _oggpack_write(opb,c->dim,16);
234 _oggpack_write(opb,c->entries,24);
236 /* pack the codewords. There are two packings; length ordered and
237 length random. Decide between the two now. */
239 for(i=1;i<c->entries;i++)
240 if(c->lengthlist[i]<c->lengthlist[i-1])break;
241 if(i==c->entries)ordered=1;
244 /* length ordered. We only need to say how many codewords of
245 each length. The actual codewords are generated
249 _oggpack_write(opb,1,1); /* ordered */
250 _oggpack_write(opb,c->lengthlist[0]-1,5); /* 1 to 32 */
252 for(i=1;i<c->entries;i++){
253 long this=c->lengthlist[i];
254 long last=c->lengthlist[i-1];
256 for(j=last;j<this;j++){
257 _oggpack_write(opb,i-count,ilog(c->entries-count));
262 _oggpack_write(opb,i-count,ilog(c->entries-count));
265 /* length random. Again, we don't code the codeword itself, just
266 the length. This time, though, we have to encode each length */
267 _oggpack_write(opb,0,1); /* unordered */
268 for(i=0;i<c->entries;i++)
269 _oggpack_write(opb,c->lengthlist[i]-1,5);
272 /* is the entry number the desired return value, or do we have a
275 /* we have a mapping. bundle it out. */
276 _oggpack_write(opb,1,1);
278 /* values that define the dequantization */
279 _oggpack_write(opb,c->q_min,24);
280 _oggpack_write(opb,c->q_delta,24);
281 _oggpack_write(opb,c->q_quant-1,4);
282 _oggpack_write(opb,c->q_sequencep,1);
284 /* quantized values */
285 for(i=0;i<c->entries*c->dim;i++)
286 _oggpack_write(opb,c->quantlist[i],c->q_quant);
290 _oggpack_write(opb,0,1);
296 /* unpacks a codebook from the packet buffer into the codebook struct,
297 readies the codebook auxiliary structures for decode *************/
298 int vorbis_staticbook_unpack(oggpack_buffer *opb,static_codebook *s){
300 memset(s,0,sizeof(static_codebook));
302 /* make sure alignment is correct */
303 if(_oggpack_read(opb,24)!=0x564342)goto _eofout;
305 /* first the basic parameters */
306 s->dim=_oggpack_read(opb,16);
307 s->entries=_oggpack_read(opb,24);
308 if(s->entries==-1)goto _eofout;
310 /* codeword ordering.... length ordered or unordered? */
311 switch(_oggpack_read(opb,1)){
314 s->lengthlist=malloc(sizeof(long)*s->entries);
315 for(i=0;i<s->entries;i++){
316 long num=_oggpack_read(opb,5);
317 if(num==-1)goto _eofout;
318 s->lengthlist[i]=num+1;
325 long length=_oggpack_read(opb,5)+1;
326 s->lengthlist=malloc(sizeof(long)*s->entries);
328 for(i=0;i<s->entries;){
329 long num=_oggpack_read(opb,ilog(s->entries-i));
330 if(num==-1)goto _eofout;
331 for(j=0;j<num;j++,i++)
332 s->lengthlist[i]=length;
342 /* Do we have a mapping to unpack? */
343 if(_oggpack_read(opb,1)){
345 /* values that define the dequantization */
346 s->q_min=_oggpack_read(opb,24);
347 s->q_delta=_oggpack_read(opb,24);
348 s->q_quant=_oggpack_read(opb,4)+1;
349 s->q_sequencep=_oggpack_read(opb,1);
351 /* quantized values */
352 s->quantlist=malloc(sizeof(double)*s->entries*s->dim);
353 for(i=0;i<s->entries*s->dim;i++)
354 s->quantlist[i]=_oggpack_read(opb,s->q_quant);
355 if(s->quantlist[i-1]==-1)goto _eofout;
363 vorbis_staticbook_clear(s);
367 /* returns the number of bits ***************************************/
368 int vorbis_book_encode(codebook *book, int a, oggpack_buffer *b){
369 _oggpack_write(b,book->codelist[a],book->c->lengthlist[a]);
370 return(book->c->lengthlist[a]);
373 static int _best(codebook *book, double *a){
374 encode_aux *t=book->c->encode_tree;
377 /* optimized, using the decision tree */
380 double *p=book->valuelist+t->p[ptr];
381 double *q=book->valuelist+t->q[ptr];
384 c+=(p[k]-q[k])*(a[k]-(p[k]+q[k])*.5);
395 /* returns the number of bits and *modifies a* to the quantization value *****/
396 int vorbis_book_encodev(codebook *book, double *a, oggpack_buffer *b){
398 int best=_best(book,a);
399 memcpy(a,book->valuelist+best*dim,dim*sizeof(double));
400 return(vorbis_book_encode(book,best,b));}
402 /* returns the number of bits and *modifies a* to the quantization error *****/
403 int vorbis_book_encodevE(codebook *book, double *a, oggpack_buffer *b){
405 int best=_best(book,a);
407 a[k]-=(book->valuelist+best*dim)[k];
408 return(vorbis_book_encode(book,best,b));
411 /* returns the total squared quantization error for best match and sets each
412 element of a to local error ***************/
413 double vorbis_book_vE(codebook *book, double *a){
415 int best=_best(book,a);
418 double val=(book->valuelist+best*dim)[k];
425 /* returns the entry number or -1 on eof *************************************/
426 long vorbis_book_decode(codebook *book, oggpack_buffer *b){
428 decode_aux *t=book->decode_tree;
430 switch(_oggpack_read1(b)){
444 /* returns the entry number or -1 on eof *************************************/
445 long vorbis_book_decodev(codebook *book, double *a, oggpack_buffer *b){
446 long entry=vorbis_book_decode(book,b);
448 if(entry==-1)return(-1);
449 for(i=0;i<book->dim;i++)a[i]+=(book->valuelist+entry*book->dim)[i];
455 /* Simple enough; pack a few candidate codebooks, unpack them. Code a
456 number of vectors through (keeping track of the quantized values),
457 and decode using the unpacked book. quantized version of in should
461 #include "vorbis/book/lsp20_0.vqh"
462 #include "vorbis/book/lsp32_0.vqh"
570 static_codebook *testlist[]={&_vq_book_lsp20_0,&_vq_book_lsp32_0,NULL};
571 double *testvec[]={test1,test2};
574 oggpack_buffer write;
577 _oggpack_writeinit(&write);
579 fprintf(stderr,"Testing codebook abstraction...:\n");
581 while(testlist[ptr]){
584 double *qv=alloca(sizeof(double)*TESTSIZE);
585 double *iv=alloca(sizeof(double)*TESTSIZE);
586 memcpy(qv,testvec[ptr],sizeof(double)*TESTSIZE);
587 memset(iv,0,sizeof(double)*TESTSIZE);
589 fprintf(stderr,"\tpacking/coding %ld... ",ptr);
591 /* pack the codebook, write the testvector */
592 _oggpack_reset(&write);
593 vorbis_book_init_encode(&c,testlist[ptr]); /* get it into memory
595 vorbis_staticbook_pack(testlist[ptr],&write);
596 fprintf(stderr,"Codebook size %ld bytes... ",_oggpack_bytes(&write));
597 for(i=0;i<TESTSIZE;i+=TESTDIM)
598 vorbis_book_encodev(&c,qv+i,&write);
599 vorbis_book_clear(&c);
601 fprintf(stderr,"OK.\n");
602 fprintf(stderr,"\tunpacking/decoding %ld... ",ptr);
604 /* transfer the write data to a read buffer and unpack/read */
605 _oggpack_readinit(&read,_oggpack_buffer(&write),_oggpack_bytes(&write));
606 if(vorbis_staticbook_unpack(&read,&s)){
607 fprintf(stderr,"Error unpacking codebook.\n");
610 if(vorbis_book_init_decode(&c,&s)){
611 fprintf(stderr,"Error initializing codebook.\n");
615 for(i=0;i<TESTSIZE;i+=TESTDIM)
616 if(vorbis_book_decodev(&c,iv+i,&read)==-1){
617 fprintf(stderr,"Error reading codebook test data (EOP).\n");
620 for(i=0;i<TESTSIZE;i++)
621 if(fabs(qv[i]-iv[i])>.000001){
622 fprintf(stderr,"input (%g) != output (%g) at position (%ld)\n",
623 iv[i],testvec[ptr][i]-qv[i],i);
627 fprintf(stderr,"OK\n");