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.12 2000/03/10 13:21:18 xiphmont Exp $
17 ********************************************************************/
21 #include "vorbis/codec.h"
22 #include "vorbis/codebook.h"
24 #include "bookinternal.h"
27 /**** pack/unpack helpers ******************************************/
28 static int ilog(unsigned int v){
37 /* code that packs the 24 bit float can be found in vq/bookutil.c */
39 static double _float24_unpack(long val){
40 double mant=val&0x3ffff;
41 double sign=val&0x800000;
42 double exp =(val&0x7c0000)>>18;
44 return(ldexp(mant,exp-17-VQ_FEXP_BIAS));
47 /* given a list of word lengths, generate a list of codewords. Works
48 for length ordered or unordered, always assigns the lowest valued
50 long *_make_words(long *l,long n){
53 long *r=malloc(n*sizeof(long));
54 memset(marker,0,sizeof(marker));
58 long entry=marker[length];
60 /* when we claim a node for an entry, we also claim the nodes
61 below it (pruning off the imagined tree that may have dangled
62 from it) as well as blocking the use of any nodes directly
66 if(length<32 && (entry>>length)){
67 /* error condition; the lengths must specify an overpopulated tree */
73 /* Look to see if the next shorter marker points to the node
74 above. if so, update it and repeat. */
76 for(j=length;j>0;j--){
79 /* have to jump branches */
83 marker[j]=marker[j-1]<<1;
84 break; /* invariant says next upper marker would already
85 have been moved if it was on the same path */
91 /* prune the tree; the implicit invariant says all the longer
92 markers were dangling from our just-taken node. Dangle them
93 from our *new* node. */
94 for(j=length+1;j<33;j++)
95 if((marker[j]>>1) == entry){
97 marker[j]=marker[j-1]<<1;
102 /* bitreverse the words because our bitwise packer/unpacker is LSb
116 /* build the decode helper tree from the codewords */
117 decode_aux *_make_decode_tree(codebook *c){
118 const static_codebook *s=c->c;
120 decode_aux *t=malloc(sizeof(decode_aux));
121 long *ptr0=t->ptr0=calloc(c->entries*2,sizeof(long));
122 long *ptr1=t->ptr1=calloc(c->entries*2,sizeof(long));
123 long *codelist=_make_words(s->lengthlist,s->entries);
125 if(codelist==NULL)return(NULL);
128 for(i=0;i<c->entries;i++){
130 for(j=0;j<s->lengthlist[i]-1;j++){
131 int bit=(codelist[i]>>j)&1;
142 if(!((codelist[i]>>j)&1))
151 /* unpack the quantized list of values for encode/decode ***********/
152 static double *_book_unquantize(const static_codebook *b){
155 double mindel=_float24_unpack(b->q_min);
156 double delta=_float24_unpack(b->q_delta);
157 double *r=malloc(sizeof(double)*b->entries*b->dim);
159 for(j=0;j<b->entries;j++){
161 for(k=0;k<b->dim;k++){
162 double val=b->quantlist[j*b->dim+k]*delta+last+mindel;
164 if(b->q_sequencep)last=val;
172 void vorbis_staticbook_clear(static_codebook *b){
173 if(b->quantlist)free(b->quantlist);
174 if(b->lengthlist)free(b->lengthlist);
176 free(b->encode_tree->ptr0);
177 free(b->encode_tree->ptr1);
178 free(b->encode_tree->p);
179 free(b->encode_tree->q);
180 memset(b->encode_tree,0,sizeof(encode_aux));
181 free(b->encode_tree);
183 memset(b,0,sizeof(static_codebook));
186 void vorbis_book_clear(codebook *b){
187 /* static book is not cleared; we're likely called on the lookup and
188 the static codebook belongs to the info struct */
190 free(b->decode_tree->ptr0);
191 free(b->decode_tree->ptr1);
192 memset(b->decode_tree,0,sizeof(decode_aux));
193 free(b->decode_tree);
195 if(b->valuelist)free(b->valuelist);
196 if(b->codelist)free(b->codelist);
197 memset(b,0,sizeof(codebook));
200 int vorbis_book_init_encode(codebook *c,const static_codebook *s){
201 memset(c,0,sizeof(codebook));
203 c->entries=s->entries;
205 c->codelist=_make_words(s->lengthlist,s->entries);
206 c->valuelist=_book_unquantize(s);
210 int vorbis_book_init_decode(codebook *c,const static_codebook *s){
211 memset(c,0,sizeof(codebook));
213 c->entries=s->entries;
215 c->valuelist=_book_unquantize(s);
216 c->decode_tree=_make_decode_tree(c);
217 if(c->decode_tree==NULL)goto err_out;
220 vorbis_book_clear(c);
224 /* packs the given codebook into the bitstream **************************/
226 int vorbis_staticbook_pack(const static_codebook *c,oggpack_buffer *opb){
230 /* first the basic parameters */
231 _oggpack_write(opb,0x564342,24);
232 _oggpack_write(opb,c->dim,16);
233 _oggpack_write(opb,c->entries,24);
235 /* pack the codewords. There are two packings; length ordered and
236 length random. Decide between the two now. */
238 for(i=1;i<c->entries;i++)
239 if(c->lengthlist[i]<c->lengthlist[i-1])break;
240 if(i==c->entries)ordered=1;
243 /* length ordered. We only need to say how many codewords of
244 each length. The actual codewords are generated
248 _oggpack_write(opb,1,1); /* ordered */
249 _oggpack_write(opb,c->lengthlist[0]-1,5); /* 1 to 32 */
251 for(i=1;i<c->entries;i++){
252 long this=c->lengthlist[i];
253 long last=c->lengthlist[i-1];
255 for(j=last;j<this;j++){
256 _oggpack_write(opb,i-count,ilog(c->entries-count));
261 _oggpack_write(opb,i-count,ilog(c->entries-count));
264 /* length random. Again, we don't code the codeword itself, just
265 the length. This time, though, we have to encode each length */
266 _oggpack_write(opb,0,1); /* unordered */
267 for(i=0;i<c->entries;i++)
268 _oggpack_write(opb,c->lengthlist[i]-1,5);
271 /* is the entry number the desired return value, or do we have a
274 /* we have a mapping. bundle it out. */
275 _oggpack_write(opb,1,1);
277 /* values that define the dequantization */
278 _oggpack_write(opb,c->q_min,24);
279 _oggpack_write(opb,c->q_delta,24);
280 _oggpack_write(opb,c->q_quant-1,4);
281 _oggpack_write(opb,c->q_sequencep,1);
283 /* quantized values */
284 for(i=0;i<c->entries*c->dim;i++)
285 _oggpack_write(opb,c->quantlist[i],c->q_quant);
289 _oggpack_write(opb,0,1);
295 /* unpacks a codebook from the packet buffer into the codebook struct,
296 readies the codebook auxiliary structures for decode *************/
297 int vorbis_staticbook_unpack(oggpack_buffer *opb,static_codebook *s){
299 memset(s,0,sizeof(static_codebook));
301 /* make sure alignment is correct */
302 if(_oggpack_read(opb,24)!=0x564342)goto _eofout;
304 /* first the basic parameters */
305 s->dim=_oggpack_read(opb,16);
306 s->entries=_oggpack_read(opb,24);
307 if(s->entries==-1)goto _eofout;
309 /* codeword ordering.... length ordered or unordered? */
310 switch(_oggpack_read(opb,1)){
313 s->lengthlist=malloc(sizeof(long)*s->entries);
314 for(i=0;i<s->entries;i++){
315 long num=_oggpack_read(opb,5);
316 if(num==-1)goto _eofout;
317 s->lengthlist[i]=num+1;
324 long length=_oggpack_read(opb,5)+1;
325 s->lengthlist=malloc(sizeof(long)*s->entries);
327 for(i=0;i<s->entries;){
328 long num=_oggpack_read(opb,ilog(s->entries-i));
329 if(num==-1)goto _eofout;
330 for(j=0;j<num;j++,i++)
331 s->lengthlist[i]=length;
341 /* Do we have a mapping to unpack? */
342 if(_oggpack_read(opb,1)){
344 /* values that define the dequantization */
345 s->q_min=_oggpack_read(opb,24);
346 s->q_delta=_oggpack_read(opb,24);
347 s->q_quant=_oggpack_read(opb,4)+1;
348 s->q_sequencep=_oggpack_read(opb,1);
350 /* quantized values */
351 s->quantlist=malloc(sizeof(double)*s->entries*s->dim);
352 for(i=0;i<s->entries*s->dim;i++)
353 s->quantlist[i]=_oggpack_read(opb,s->q_quant);
354 if(s->quantlist[i-1]==-1)goto _eofout;
362 vorbis_staticbook_clear(s);
366 /* returns the number of bits ***************************************/
367 int vorbis_book_encode(codebook *book, int a, oggpack_buffer *b){
368 _oggpack_write(b,book->codelist[a],book->c->lengthlist[a]);
369 return(book->c->lengthlist[a]);
372 static int _best(codebook *book, double *a){
373 encode_aux *t=book->c->encode_tree;
376 /* optimized, using the decision tree */
379 double *p=book->valuelist+t->p[ptr];
380 double *q=book->valuelist+t->q[ptr];
383 c+=(p[k]-q[k])*(a[k]-(p[k]+q[k])*.5);
394 /* returns the number of bits and *modifies a* to the quantization value *****/
395 int vorbis_book_encodev(codebook *book, double *a, oggpack_buffer *b){
397 int best=_best(book,a);
398 memcpy(a,book->valuelist+best*dim,dim*sizeof(double));
399 return(vorbis_book_encode(book,best,b));}
401 /* returns the number of bits and *modifies a* to the quantization error *****/
402 int vorbis_book_encodevE(codebook *book, double *a, oggpack_buffer *b){
404 int best=_best(book,a);
406 a[k]-=(book->valuelist+best*dim)[k];
407 return(vorbis_book_encode(book,best,b));
410 /* returns the total squared quantization error for best match and sets each
411 element of a to local error ***************/
412 double vorbis_book_vE(codebook *book, double *a){
414 int best=_best(book,a);
417 double val=(book->valuelist+best*dim)[k];
424 /* returns the entry number or -1 on eof *************************************/
425 long vorbis_book_decode(codebook *book, oggpack_buffer *b){
427 decode_aux *t=book->decode_tree;
429 switch(_oggpack_read1(b)){
443 /* returns the entry number or -1 on eof *************************************/
444 long vorbis_book_decodev(codebook *book, double *a, oggpack_buffer *b){
445 long entry=vorbis_book_decode(book,b);
447 if(entry==-1)return(-1);
448 for(i=0;i<book->dim;i++)a[i]+=(book->valuelist+entry*book->dim)[i];
454 /* Simple enough; pack a few candidate codebooks, unpack them. Code a
455 number of vectors through (keeping track of the quantized values),
456 and decode using the unpacked book. quantized version of in should
460 #include "vorbis/book/lsp20_0.vqh"
461 #include "vorbis/book/lsp32_0.vqh"
569 static_codebook *testlist[]={&_vq_book_lsp20_0,&_vq_book_lsp32_0,NULL};
570 double *testvec[]={test1,test2};
573 oggpack_buffer write;
576 _oggpack_writeinit(&write);
578 fprintf(stderr,"Testing codebook abstraction...:\n");
580 while(testlist[ptr]){
583 double *qv=alloca(sizeof(double)*TESTSIZE);
584 double *iv=alloca(sizeof(double)*TESTSIZE);
585 memcpy(qv,testvec[ptr],sizeof(double)*TESTSIZE);
586 memset(iv,0,sizeof(double)*TESTSIZE);
588 fprintf(stderr,"\tpacking/coding %ld... ",ptr);
590 /* pack the codebook, write the testvector */
591 _oggpack_reset(&write);
592 vorbis_book_init_encode(&c,testlist[ptr]); /* get it into memory
594 vorbis_staticbook_pack(testlist[ptr],&write);
595 fprintf(stderr,"Codebook size %ld bytes... ",_oggpack_bytes(&write));
596 for(i=0;i<TESTSIZE;i+=TESTDIM)
597 vorbis_book_encodev(&c,qv+i,&write);
598 vorbis_book_clear(&c);
600 fprintf(stderr,"OK.\n");
601 fprintf(stderr,"\tunpacking/decoding %ld... ",ptr);
603 /* transfer the write data to a read buffer and unpack/read */
604 _oggpack_readinit(&read,_oggpack_buffer(&write),_oggpack_bytes(&write));
605 if(vorbis_staticbook_unpack(&read,&s)){
606 fprintf(stderr,"Error unpacking codebook.\n");
609 if(vorbis_book_init_decode(&c,&s)){
610 fprintf(stderr,"Error initializing codebook.\n");
614 for(i=0;i<TESTSIZE;i+=TESTDIM)
615 if(vorbis_book_decodev(&c,iv+i,&read)==-1){
616 fprintf(stderr,"Error reading codebook test data (EOP).\n");
619 for(i=0;i<TESTSIZE;i++)
620 if(fabs(qv[i]-iv[i])>.000001){
621 fprintf(stderr,"input (%g) != output (%g) at position (%ld)\n",
622 iv[i],testvec[ptr][i]-qv[i],i);
626 fprintf(stderr,"OK\n");