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.2 2000/01/12 11:16:34 xiphmont Exp $
17 ********************************************************************/
21 #include "vorbis/codec.h"
22 #include "vorbis/codebook.h"
24 #include "bookinternal.h"
26 /**** pack/unpack helpers ******************************************/
27 static int ilog(unsigned int v){
36 static long _float24_pack(double val){
44 exp= floor(log(val)/log(2));
45 mant=rint(ldexp(val,17-exp));
46 exp=(exp+VQ_FEXP_BIAS)<<18;
48 return(sign|exp|mant);
51 static double _float24_unpack(long val){
52 double mant=val&0x3ffff;
53 double sign=val&0x800000;
54 double exp =(val&0x7c0000)>>18;
56 return(ldexp(mant,exp-17-VQ_FEXP_BIAS));
59 /* given a list of word lengths, generate a list of codewords. Works
60 for length ordered or unordered, always assigns the lowest valued
62 long *_make_words(long *l,long n){
65 long *r=malloc(n*sizeof(long));
66 memset(marker,0,sizeof(marker));
70 long entry=marker[l[i]];
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
78 if(length<32 && (entry>>length)){
79 /* error condition; the lengths must specify an overpopulated tree */
85 /* Look to see if the next shorter marker points to the node
86 above. if so, update it and repeat. */
88 for(j=length;j>0;j--){
91 /* have to jump branches */
95 marker[j]=marker[j-1]<<1;
96 break; /* invariant says next upper marker would already
97 have been moved if it was on the same path */
103 /* prune the tree; the implicit invariant says all the longer
104 markers were dangling from our just-taken node. Dangle them
105 from our *new* node. */
106 for(j=length+1;j<33;j++)
107 marker[j]=marker[j-1]<<1;
110 /* bitreverse the words because our bitwise packer/unpacker is LSb
124 /* unpack the quantized list of values for encode/decode ***********/
125 static double *_book_unquantize(codebook *b){
127 double mindel=_float24_unpack(b->q_min);
128 double delta=_float24_unpack(b->q_delta);
129 double *r=malloc(sizeof(double)*b->entries*b->dim);
131 for(j=0;j<b->entries;j++){
133 for(k=0;k<b->dim;k++){
134 double val=b->quantlist[j*b->dim+k]*delta+last+mindel;
136 if(b->q_sequencep)last=val;
142 /**** Defend the abstraction ****************************************/
144 /* some elements in the codebook structure are assumed to be pointers
145 to static/shared storage (the pointers are duped, and free below
146 does not touch them. The fields are unused by decode):
154 void vorbis_book_dup(codebook *dest,const codebook *source){
155 long entries=source->entries;
156 long dim=source->dim;
157 memcpy(dest,source,sizeof(codebook));
159 /* handle non-flat storage */
160 if(source->valuelist){
161 dest->valuelist=malloc(sizeof(double)*dim*entries);
162 memcpy(dest->valuelist,source->valuelist,sizeof(double)*dim*entries);
164 if(source->codelist){
165 dest->codelist=malloc(sizeof(long)*entries);
166 memcpy(dest->codelist,source->codelist,sizeof(long)*entries);
169 /* encode tree is assumed to be static storage; don't free it */
171 if(source->decode_tree){
172 long aux=source->decode_tree->aux;
173 dest->decode_tree=malloc(sizeof(decode_aux));
174 dest->decode_tree->aux=aux;
175 dest->decode_tree->ptr0=malloc(sizeof(long)*aux);
176 dest->decode_tree->ptr1=malloc(sizeof(long)*aux);
178 memcpy(dest->decode_tree->ptr0,source->decode_tree->ptr0,sizeof(long)*aux);
179 memcpy(dest->decode_tree->ptr1,source->decode_tree->ptr1,sizeof(long)*aux);
183 void vorbis_book_clear(codebook *b){
185 free(b->decode_tree->ptr0);
186 free(b->decode_tree->ptr1);
187 memset(b->decode_tree,0,sizeof(decode_aux));
188 free(b->decode_tree);
190 if(b->valuelist)free(b->valuelist);
191 if(b->codelist)free(b->codelist);
192 memset(b,0,sizeof(codebook));
195 /* packs the given codebook into the bitstream
196 side effects: populates the valuelist and codeword members ***********/
197 int vorbis_book_pack(codebook *c,oggpack_buffer *b){
201 /* first the basic parameters */
202 _oggpack_write(b,0x564342,24);
203 _oggpack_write(b,c->dim,16);
204 _oggpack_write(b,c->entries,24);
206 /* pack the codewords. There are two packings; length ordered and
207 length random. Decide between the two now. */
209 for(i=1;i<c->entries;i++)
210 if(c->lengthlist[i]<c->lengthlist[i-1])break;
211 if(i==c->entries)ordered=1;
214 /* length ordered. We only need to say how many codewords of
215 each length. The actual codewords are generated
219 _oggpack_write(b,1,1); /* ordered */
220 _oggpack_write(b,c->lengthlist[0]-1,5); /* 1 to 32 */
222 for(i=1;i<c->entries;i++){
223 long this=c->lengthlist[i];
224 long last=c->lengthlist[i-1];
226 for(j=last;j<this;j++){
227 _oggpack_write(b,i-count,ilog(c->entries-count));
232 _oggpack_write(b,i-count,ilog(c->entries-count));
235 /* length random. Again, we don't code the codeword itself, just
236 the length. This time, though, we have to encode each length */
237 _oggpack_write(b,0,1); /* unordered */
238 for(i=0;i<c->entries;i++)
239 _oggpack_write(b,c->lengthlist[i]-1,5);
242 /* is the entry number the desired return value, or do we have a
245 /* we have a mapping. bundle it out. */
246 _oggpack_write(b,1,1);
248 /* values that define the dequantization */
249 _oggpack_write(b,c->q_min,24);
250 _oggpack_write(b,c->q_delta,24);
251 _oggpack_write(b,c->q_quant-1,4);
252 _oggpack_write(b,c->q_sequencep,1);
254 /* quantized values */
255 for(i=0;i<c->entries*c->dim;i++)
256 _oggpack_write(b,c->quantlist[i],c->q_quant);
260 _oggpack_write(b,0,1);
263 c->codelist=_make_words(c->lengthlist,c->entries);
264 c->valuelist=_book_unquantize(c);
269 /* unpacks a codebook from the packet buffer into the codebook struct,
270 readies the codebook auxiliary structures for decode *************/
271 int vorbis_book_unpack(oggpack_buffer *b,codebook *c){
273 memset(c,0,sizeof(codebook));
275 /* make sure alignment is correct */
276 if(_oggpack_read(b,24)!=0x564342)goto _eofout;
278 /* first the basic parameters */
279 c->dim=_oggpack_read(b,16);
280 c->entries=_oggpack_read(b,24);
281 if(c->entries==-1)goto _eofout;
283 /* codeword ordering.... length ordered or unordered? */
284 switch(_oggpack_read(b,1)){
287 c->lengthlist=malloc(sizeof(long)*c->entries);
288 for(i=0;i<c->entries;i++){
289 long num=_oggpack_read(b,5);
290 if(num==-1)goto _eofout;
291 c->lengthlist[i]=num+1;
298 long length=_oggpack_read(b,5)+1;
299 c->lengthlist=malloc(sizeof(long)*c->entries);
301 for(i=0;i<c->entries;){
302 long num=_oggpack_read(b,ilog(c->entries-i));
303 if(num==-1)goto _eofout;
304 for(j=0;j<num;j++,i++)
305 c->lengthlist[i]=length;
315 /* now we generate the codewords for the given lengths */
316 c->codelist=_make_words(c->lengthlist,c->entries);
317 if(c->codelist==NULL)goto _errout;
319 /* ...and the decode helper tree from the codewords */
322 decode_aux *t=c->decode_tree=malloc(sizeof(decode_aux));
323 long *ptr0=t->ptr0=calloc(c->entries*2,sizeof(long));
324 long *ptr1=t->ptr1=calloc(c->entries*2,sizeof(long));
327 for(i=0;i<c->entries;i++){
329 for(j=0;j<c->lengthlist[i]-1;j++){
330 int bit=(c->codelist[i]>>j)&1;
341 if(!((c->codelist[i]>>j)&1))
347 /* no longer needed */
348 free(c->lengthlist);c->lengthlist=NULL;
349 free(c->codelist);c->codelist=NULL;
351 /* Do we have a mapping to unpack? */
352 if(_oggpack_read(b,1)){
354 /* values that define the dequantization */
355 c->q_min=_oggpack_read(b,24);
356 c->q_delta=_oggpack_read(b,24);
357 c->q_quant=_oggpack_read(b,4)+1;
358 c->q_sequencep=_oggpack_read(b,1);
360 /* quantized values */
361 c->quantlist=malloc(sizeof(double)*c->entries*c->dim);
362 for(i=0;i<c->entries*c->dim;i++)
363 c->quantlist[i]=_oggpack_read(b,c->q_quant);
364 if(c->quantlist[i-1]==-1)goto _eofout;
365 c->valuelist=_book_unquantize(c);
366 free(c->quantlist);c->quantlist=NULL;
374 if(c->lengthlist)free(c->lengthlist);c->lengthlist=NULL;
375 if(c->quantlist)free(c->quantlist);c->quantlist=NULL;
376 vorbis_book_clear(c);
381 /* returns the number of bits ***************************************/
382 int vorbis_book_encode(codebook *book, int a, oggpack_buffer *b){
383 _oggpack_write(b,book->codelist[a],book->lengthlist[a]);
384 return(book->lengthlist[a]);
387 /* returns the number of bits and *modifies a* to the entry value *****/
388 int vorbis_book_encodev(codebook *book, double *a, oggpack_buffer *b){
389 encode_aux *t=book->encode_tree;
395 double *p=book->valuelist+t->p[ptr];
396 double *q=book->valuelist+t->q[ptr];
399 c+=(p[k]-q[k])*(a[k]-(p[k]+q[k])*.5);
407 memcpy(a,book->valuelist-ptr*dim,dim*sizeof(double));
408 return(vorbis_book_encode(book,-ptr,b));
411 /* returns the entry number or -1 on eof ****************************/
412 long vorbis_book_decode(codebook *book, oggpack_buffer *b){
414 decode_aux *t=book->decode_tree;
416 switch(_oggpack_read1(b)){
430 /* returns the entry number or -1 on eof ****************************/
431 long vorbis_book_decodev(codebook *book, double *a, oggpack_buffer *b){
432 long entry=vorbis_book_decode(book,b);
433 if(entry==-1)return(-1);
434 memcpy(a,book->valuelist+entry*book->dim,sizeof(double)*book->dim);
440 /* Simple enough; pack a few candidate codebooks, unpack them. Code a
441 number of vectors through (keeping track of the quantized values),
442 and decode using the unpacked book. quantized version of in should
446 #include "vorbis/book/lsp20_0.vqh"
447 #include "vorbis/book/lsp32_0.vqh"
555 codebook *testlist[]={&_vq_book_lsp20_0,&_vq_book_lsp32_0,NULL};
556 double *testvec[]={test1,test2};
559 oggpack_buffer write;
562 _oggpack_writeinit(&write);
564 fprintf(stderr,"Testing codebook abstraction...:\n");
566 while(testlist[ptr]){
568 double *qv=alloca(sizeof(double)*TESTSIZE);
569 double *iv=alloca(sizeof(double)*TESTSIZE);
570 memcpy(qv,testvec[ptr],sizeof(double)*TESTSIZE);
572 fprintf(stderr,"\tpacking/coding %ld... ",ptr);
574 /* pack the codebook, write the testvector */
575 _oggpack_reset(&write);
576 vorbis_book_dup(&c,testlist[ptr]); /* get it into memory we can write */
577 vorbis_book_pack(&c,&write);
578 fprintf(stderr,"Codebook size %ld bytes... ",_oggpack_bytes(&write));
579 for(i=0;i<TESTSIZE;i+=TESTDIM)
580 vorbis_book_encodev(&c,qv+i,&write);
581 vorbis_book_clear(&c);
583 fprintf(stderr,"OK.\n");
584 fprintf(stderr,"\tunpacking/decoding %ld... ",ptr);
586 /* transfer the write data to a read buffer and unpack/read */
587 _oggpack_readinit(&read,_oggpack_buffer(&write),_oggpack_bytes(&write));
588 if(vorbis_book_unpack(&read,&c)){
589 fprintf(stderr,"Error unpacking codebook.\n");
592 for(i=0;i<TESTSIZE;i+=TESTDIM)
593 if(vorbis_book_decodev(&c,iv+i,&read)){
594 fprintf(stderr,"Error reading codebook test data (EOP).\n");
597 for(i=0;i<TESTSIZE;i++)
599 fprintf(stderr,"input (%g) != output (%g) at position (%ld)\n",
604 fprintf(stderr,"OK\n");