• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /********************************************************************
2  *                                                                  *
3  * THIS FILE IS PART OF THE OggVorbis 'TREMOR' CODEC SOURCE CODE.   *
4  *                                                                  *
5  * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS     *
6  * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE *
7  * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING.       *
8  *                                                                  *
9  * THE OggVorbis 'TREMOR' SOURCE CODE IS (C) COPYRIGHT 1994-2002    *
10  * BY THE Xiph.Org FOUNDATION http://www.xiph.org/                  *
11  *                                                                  *
12  ********************************************************************
13 
14  function: basic codebook pack/unpack/code/decode operations
15 
16  ********************************************************************/
17 
18 #include <stdlib.h>
19 #include <string.h>
20 #include <math.h>
21 #include <limits.h>
22 #include "ogg.h"
23 #include "ivorbiscodec.h"
24 #include "codebook.h"
25 #include "misc.h"
26 #include "os.h"
27 
28 
29 /**** pack/unpack helpers ******************************************/
_ilog(unsigned int v)30 int _ilog(unsigned int v){
31   int ret=0;
32   while(v){
33     ret++;
34     v>>=1;
35   }
36   return(ret);
37 }
38 
decpack(long entry,long used_entry,long quantvals,codebook * b,oggpack_buffer * opb,int maptype)39 static ogg_uint32_t decpack(long entry,long used_entry,long quantvals,
40 			    codebook *b,oggpack_buffer *opb,int maptype){
41   ogg_uint32_t ret=0;
42   int j;
43 
44   switch(b->dec_type){
45 
46   case 0:
47     return (ogg_uint32_t)entry;
48 
49   case 1:
50     if(maptype==1){
51       /* vals are already read into temporary column vector here */
52       for(j=0;j<b->dim;j++){
53 	ogg_uint32_t off=entry%quantvals;
54 	entry/=quantvals;
55 	ret|=((ogg_uint16_t *)(b->q_val))[off]<<(b->q_bits*j);
56       }
57     }else{
58       for(j=0;j<b->dim;j++)
59 	ret|=oggpack_read(opb,b->q_bits)<<(b->q_bits*j);
60     }
61     return ret;
62 
63   case 2:
64     for(j=0;j<b->dim;j++){
65       ogg_uint32_t off=entry%quantvals;
66       entry/=quantvals;
67       ret|=off<<(b->q_pack*j);
68     }
69     return ret;
70 
71   case 3:
72     return (ogg_uint32_t)used_entry;
73 
74   }
75   return 0; /* silence compiler */
76 }
77 
78 /* 32 bit float (not IEEE; nonnormalized mantissa +
79    biased exponent) : neeeeeee eeemmmmm mmmmmmmm mmmmmmmm
80    Why not IEEE?  It's just not that important here. */
81 
_float32_unpack(long val,int * point)82 static ogg_int32_t _float32_unpack(long val,int *point){
83   long   mant=val&0x1fffff;
84   int    sign=val&0x80000000;
85 
86   *point=((val&0x7fe00000L)>>21)-788;
87 
88   if(mant){
89     while(!(mant&0x40000000)){
90       mant<<=1;
91       *point-=1;
92     }
93     if(sign)mant= -mant;
94   }else{
95     *point=-9999;
96   }
97   return mant;
98 }
99 
100 /* choose the smallest supported node size that fits our decode table.
101    Legal bytewidths are 1/1 1/2 2/2 2/4 4/4 */
_determine_node_bytes(long used,int leafwidth)102 static int _determine_node_bytes(long used, int leafwidth){
103 
104   /* special case small books to size 4 to avoid multiple special
105      cases in repack */
106   if(used<2)
107     return 4;
108 
109   if(leafwidth==3)leafwidth=4;
110   if(_ilog(3*used-6)+1 <= leafwidth*4)
111     return leafwidth/2?leafwidth/2:1;
112   return leafwidth;
113 }
114 
115 /* convenience/clarity; leaves are specified as multiple of node word
116    size (1 or 2) */
_determine_leaf_words(int nodeb,int leafwidth)117 static int _determine_leaf_words(int nodeb, int leafwidth){
118   if(leafwidth>nodeb)return 2;
119   return 1;
120 }
121 
122 /* given a list of word lengths, number of used entries, and byte
123    width of a leaf, generate the decode table */
_make_words(char * l,long n,ogg_uint32_t * r,long quantvals,codebook * b,oggpack_buffer * opb,int maptype)124 static int _make_words(char *l,long n,ogg_uint32_t *r,long quantvals,
125 		       codebook *b, oggpack_buffer *opb,int maptype){
126   long i,j,count=0;
127   long top=0;
128   ogg_uint32_t marker[33];
129 
130   if (n<1)
131     return 1;
132 
133   if(n<2){
134     r[0]=0x80000000;
135   }else{
136     memset(marker,0,sizeof(marker));
137 
138     for(i=0;i<n;i++){
139       long length=l[i];
140       if(length){
141 	ogg_uint32_t entry=marker[length];
142 	long chase=0;
143 	if(count && !entry)return -1; /* overpopulated tree! */
144 
145 	/* chase the tree as far as it's already populated, fill in past */
146 	for(j=0;j<length-1;j++){
147 	  int bit=(entry>>(length-j-1))&1;
148 	  if(chase>=top){
149 	    if (chase < 0 || chase >= n) return 1;
150 	    top++;
151 	    r[chase*2]=top;
152 	    r[chase*2+1]=0;
153 	  }else
154 	    if (chase < 0 || chase >= n || chase*2+bit > n*2+1) return 1;
155 	    if(!r[chase*2+bit])
156 	      r[chase*2+bit]=top;
157 	  chase=r[chase*2+bit];
158 	  if (chase < 0 || chase >= n) return 1;
159 	}
160 	{
161 	  int bit=(entry>>(length-j-1))&1;
162 	  if(chase>=top){
163 	    top++;
164 	    r[chase*2+1]=0;
165 	  }
166 	  r[chase*2+bit]= decpack(i,count++,quantvals,b,opb,maptype) |
167 	    0x80000000;
168 	}
169 
170 	/* Look to see if the next shorter marker points to the node
171 	   above. if so, update it and repeat.  */
172 	for(j=length;j>0;j--){
173 	  if(marker[j]&1){
174 	    marker[j]=marker[j-1]<<1;
175 	    break;
176 	  }
177 	  marker[j]++;
178 	}
179 
180 	/* prune the tree; the implicit invariant says all the longer
181 	   markers were dangling from our just-taken node.  Dangle them
182 	   from our *new* node. */
183 	for(j=length+1;j<33;j++)
184 	  if((marker[j]>>1) == entry){
185 	    entry=marker[j];
186 	    marker[j]=marker[j-1]<<1;
187 	  }else
188 	    break;
189       }
190     }
191   }
192 
193   return 0;
194 }
195 
_make_decode_table(codebook * s,char * lengthlist,long quantvals,oggpack_buffer * opb,int maptype)196 static int _make_decode_table(codebook *s,char *lengthlist,long quantvals,
197 			      oggpack_buffer *opb,int maptype){
198   int i;
199   ogg_uint32_t *work;
200 
201   if (!lengthlist) return 1;
202   if(s->dec_nodeb==4){
203     /* Over-allocate by using s->entries instead of used_entries.
204      * This means that we can use s->entries to enforce size in
205      * _make_words without messing up length list looping.
206      * This probably wastes a bit of space, but it shouldn't
207      * impact behavior or size too much.
208      */
209     s->dec_table=_ogg_malloc((s->entries*2+1)*sizeof(*work));
210     if (!s->dec_table) return 1;
211     /* +1 (rather than -2) is to accommodate 0 and 1 sized books,
212        which are specialcased to nodeb==4 */
213     if(_make_words(lengthlist,s->entries,
214 		   s->dec_table,quantvals,s,opb,maptype))return 1;
215 
216     return 0;
217   }
218 
219   if (s->used_entries > INT_MAX/2 ||
220       s->used_entries*2 > INT_MAX/((long) sizeof(*work)) - 1) return 1;
221   /* Overallocate as above */
222   work=alloca((s->entries*2+1)*sizeof(*work));
223   if (!work) return 1;
224   if(_make_words(lengthlist,s->entries,work,quantvals,s,opb,maptype))return 1;
225   if (s->used_entries > INT_MAX/(s->dec_leafw+1)) return 1;
226   if (s->dec_nodeb && s->used_entries * (s->dec_leafw+1) > INT_MAX/s->dec_nodeb) return 1;
227   s->dec_table=_ogg_malloc((s->used_entries*(s->dec_leafw+1)-2)*
228 			   s->dec_nodeb);
229   if (!s->dec_table) return 1;
230 
231   if(s->dec_leafw==1){
232     switch(s->dec_nodeb){
233     case 1:
234       for(i=0;i<s->used_entries*2-2;i++)
235 	  ((unsigned char *)s->dec_table)[i]=
236 	    ((work[i] & 0x80000000UL) >> 24) | work[i];
237       break;
238     case 2:
239       for(i=0;i<s->used_entries*2-2;i++)
240 	  ((ogg_uint16_t *)s->dec_table)[i]=
241 	    ((work[i] & 0x80000000UL) >> 16) | work[i];
242       break;
243     }
244 
245   }else{
246     /* more complex; we have to do a two-pass repack that updates the
247        node indexing. */
248     long top=s->used_entries*3-2;
249     if(s->dec_nodeb==1){
250       unsigned char *out=(unsigned char *)s->dec_table;
251 
252       for(i=s->used_entries*2-4;i>=0;i-=2){
253 	if(work[i]&0x80000000UL){
254 	  if(work[i+1]&0x80000000UL){
255 	    top-=4;
256 	    out[top]=(work[i]>>8 & 0x7f)|0x80;
257 	    out[top+1]=(work[i+1]>>8 & 0x7f)|0x80;
258 	    out[top+2]=work[i] & 0xff;
259 	    out[top+3]=work[i+1] & 0xff;
260 	  }else{
261 	    top-=3;
262 	    out[top]=(work[i]>>8 & 0x7f)|0x80;
263 	    out[top+1]=work[work[i+1]*2];
264 	    out[top+2]=work[i] & 0xff;
265 	  }
266 	}else{
267 	  if(work[i+1]&0x80000000UL){
268 	    top-=3;
269 	    out[top]=work[work[i]*2];
270 	    out[top+1]=(work[i+1]>>8 & 0x7f)|0x80;
271 	    out[top+2]=work[i+1] & 0xff;
272 	  }else{
273 	    top-=2;
274 	    out[top]=work[work[i]*2];
275 	    out[top+1]=work[work[i+1]*2];
276 	  }
277 	}
278 	work[i]=top;
279       }
280     }else{
281       ogg_uint16_t *out=(ogg_uint16_t *)s->dec_table;
282       for(i=s->used_entries*2-4;i>=0;i-=2){
283 	if(work[i]&0x80000000UL){
284 	  if(work[i+1]&0x80000000UL){
285 	    top-=4;
286 	    out[top]=(work[i]>>16 & 0x7fff)|0x8000;
287 	    out[top+1]=(work[i+1]>>16 & 0x7fff)|0x8000;
288 	    out[top+2]=work[i] & 0xffff;
289 	    out[top+3]=work[i+1] & 0xffff;
290 	  }else{
291 	    top-=3;
292 	    out[top]=(work[i]>>16 & 0x7fff)|0x8000;
293 	    out[top+1]=work[work[i+1]*2];
294 	    out[top+2]=work[i] & 0xffff;
295 	  }
296 	}else{
297 	  if(work[i+1]&0x80000000UL){
298 	    top-=3;
299 	    out[top]=work[work[i]*2];
300 	    out[top+1]=(work[i+1]>>16 & 0x7fff)|0x8000;
301 	    out[top+2]=work[i+1] & 0xffff;
302 	  }else{
303 	    top-=2;
304 	    out[top]=work[work[i]*2];
305 	    out[top+1]=work[work[i+1]*2];
306 	  }
307 	}
308 	work[i]=top;
309       }
310     }
311   }
312 
313   return 0;
314 }
315 
316 /* most of the time, entries%dimensions == 0, but we need to be
317    well defined.  We define that the possible vales at each
318    scalar is values == entries/dim.  If entries%dim != 0, we'll
319    have 'too few' values (values*dim<entries), which means that
320    we'll have 'left over' entries; left over entries use zeroed
321    values (and are wasted).  So don't generate codebooks like
322    that */
323 /* there might be a straightforward one-line way to do the below
324    that's portable and totally safe against roundoff, but I haven't
325    thought of it.  Therefore, we opt on the side of caution */
_book_maptype1_quantvals(codebook * b)326 long _book_maptype1_quantvals(codebook *b){
327   /* get us a starting hint, we'll polish it below */
328   int bits=_ilog(b->entries);
329   int vals=b->entries>>((bits-1)*(b->dim-1)/b->dim);
330 
331   while(1){
332     long acc=1;
333     long acc1=1;
334     int i;
335     for(i=0;i<b->dim;i++){
336       acc*=vals;
337       acc1*=vals+1;
338     }
339     if(acc<=b->entries && acc1>b->entries){
340       return(vals);
341     }else{
342       if(acc>b->entries){
343         vals--;
344       }else{
345         vals++;
346       }
347     }
348   }
349 }
350 
vorbis_book_clear(codebook * b)351 void vorbis_book_clear(codebook *b){
352   /* static book is not cleared; we're likely called on the lookup and
353      the static codebook belongs to the info struct */
354   if(b->q_val)_ogg_free(b->q_val);
355   if(b->dec_table)_ogg_free(b->dec_table);
356 
357   memset(b,0,sizeof(*b));
358 }
359 
vorbis_book_unpack(oggpack_buffer * opb,codebook * s)360 int vorbis_book_unpack(oggpack_buffer *opb,codebook *s){
361   char         *lengthlist=NULL;
362   int           quantvals=0;
363   long          i,j;
364   int           maptype;
365 
366   memset(s,0,sizeof(*s));
367 
368   /* make sure alignment is correct */
369   if(oggpack_read(opb,24)!=0x564342)goto _eofout;
370 
371   /* first the basic parameters */
372   s->dim=oggpack_read(opb,16);
373   s->entries=oggpack_read(opb,24);
374   if(s->entries<=0)goto _eofout;
375   if(s->dim<=0)goto _eofout;
376   if(_ilog(s->dim)+_ilog(s->entries)>24)goto _eofout;
377   if (s->dim > INT_MAX/s->entries) goto _eofout;
378 
379   /* codeword ordering.... length ordered or unordered? */
380   switch((int)oggpack_read(opb,1)){
381   case 0:
382     /* unordered */
383     lengthlist=(char *)alloca(sizeof(*lengthlist)*s->entries);
384     if(!lengthlist) goto _eofout;
385 
386     /* allocated but unused entries? */
387     if(oggpack_read(opb,1)){
388       /* yes, unused entries */
389 
390       for(i=0;i<s->entries;i++){
391 	if(oggpack_read(opb,1)){
392 	  long num=oggpack_read(opb,5);
393 	  if(num==-1)goto _eofout;
394 	  lengthlist[i]=num+1;
395 	  s->used_entries++;
396 	  if(num+1>s->dec_maxlength)s->dec_maxlength=num+1;
397 	}else
398 	  lengthlist[i]=0;
399       }
400     }else{
401       /* all entries used; no tagging */
402       s->used_entries=s->entries;
403       for(i=0;i<s->entries;i++){
404 	long num=oggpack_read(opb,5);
405 	if(num==-1)goto _eofout;
406 	lengthlist[i]=num+1;
407 	if(num+1>s->dec_maxlength)s->dec_maxlength=num+1;
408       }
409     }
410 
411     break;
412   case 1:
413     /* ordered */
414     {
415       long length=oggpack_read(opb,5)+1;
416 
417       s->used_entries=s->entries;
418       lengthlist=(char *)alloca(sizeof(*lengthlist)*s->entries);
419       if (!lengthlist) goto _eofout;
420 
421       for(i=0;i<s->entries;){
422 	long num=oggpack_read(opb,_ilog(s->entries-i));
423 	if(num<0)goto _eofout;
424 	for(j=0;j<num && i<s->entries;j++,i++)
425 	  lengthlist[i]=length;
426 	s->dec_maxlength=length;
427 	length++;
428       }
429     }
430     break;
431   default:
432     /* EOF */
433     goto _eofout;
434   }
435 
436 
437   /* Do we have a mapping to unpack? */
438 
439   if((maptype=oggpack_read(opb,4))>0){
440     s->q_min=_float32_unpack(oggpack_read(opb,32),&s->q_minp);
441     s->q_del=_float32_unpack(oggpack_read(opb,32),&s->q_delp);
442     s->q_bits=oggpack_read(opb,4)+1;
443     s->q_seq=oggpack_read(opb,1);
444 
445     s->q_del>>=s->q_bits;
446     s->q_delp+=s->q_bits;
447   }
448 
449   switch(maptype){
450   case 0:
451 
452     /* no mapping; decode type 0 */
453 
454     /* how many bytes for the indexing? */
455     /* this is the correct boundary here; we lose one bit to
456        node/leaf mark */
457     s->dec_nodeb=_determine_node_bytes(s->used_entries,_ilog(s->entries)/8+1);
458     s->dec_leafw=_determine_leaf_words(s->dec_nodeb,_ilog(s->entries)/8+1);
459     s->dec_type=0;
460 
461     if(_make_decode_table(s,lengthlist,quantvals,opb,maptype)) goto _errout;
462     break;
463 
464   case 1:
465 
466     /* mapping type 1; implicit values by lattice  position */
467     quantvals=_book_maptype1_quantvals(s);
468 
469     /* dec_type choices here are 1,2; 3 doesn't make sense */
470     {
471       /* packed values */
472       long total1=(s->q_bits*s->dim+8)/8; /* remember flag bit */
473       if (s->dim > (INT_MAX-8)/s->q_bits) goto _eofout;
474       /* vector of column offsets; remember flag bit */
475       long total2=(_ilog(quantvals-1)*s->dim+8)/8+(s->q_bits+7)/8;
476 
477 
478       if(total1<=4 && total1<=total2){
479 	/* use dec_type 1: vector of packed values */
480 
481 	/* need quantized values before  */
482 	s->q_val=alloca(sizeof(ogg_uint16_t)*quantvals);
483 	if (!s->q_val) goto _eofout;
484 	for(i=0;i<quantvals;i++)
485 	  ((ogg_uint16_t *)s->q_val)[i]=oggpack_read(opb,s->q_bits);
486 
487 	if(oggpack_eop(opb)){
488 	  s->q_val=0; /* cleanup must not free alloca memory */
489 	  goto _eofout;
490 	}
491 
492 	s->dec_type=1;
493 	s->dec_nodeb=_determine_node_bytes(s->used_entries,
494 					   (s->q_bits*s->dim+8)/8);
495 	s->dec_leafw=_determine_leaf_words(s->dec_nodeb,
496 					   (s->q_bits*s->dim+8)/8);
497 	if(_make_decode_table(s,lengthlist,quantvals,opb,maptype)){
498 	  s->q_val=0; /* cleanup must not free alloca memory */
499 	  goto _errout;
500 	}
501 
502 	s->q_val=0; /* about to go out of scope; _make_decode_table
503                        was using it */
504 
505       }else{
506 	/* use dec_type 2: packed vector of column offsets */
507 
508 	/* need quantized values before */
509 	if(s->q_bits<=8){
510 	  s->q_val=_ogg_malloc(quantvals);
511 	  if (!s->q_val) goto _eofout;
512 	  for(i=0;i<quantvals;i++)
513 	    ((unsigned char *)s->q_val)[i]=oggpack_read(opb,s->q_bits);
514 	}else{
515 	  s->q_val=_ogg_malloc(quantvals*2);
516 	  if (!s->q_val) goto _eofout;
517 	  for(i=0;i<quantvals;i++)
518 	    ((ogg_uint16_t *)s->q_val)[i]=oggpack_read(opb,s->q_bits);
519 	}
520 
521 	if(oggpack_eop(opb))goto _eofout;
522 
523 	s->q_pack=_ilog(quantvals-1);
524 	s->dec_type=2;
525 	s->dec_nodeb=_determine_node_bytes(s->used_entries,
526 					   (_ilog(quantvals-1)*s->dim+8)/8);
527 	s->dec_leafw=_determine_leaf_words(s->dec_nodeb,
528 					   (_ilog(quantvals-1)*s->dim+8)/8);
529 	if(_make_decode_table(s,lengthlist,quantvals,opb,maptype))goto _errout;
530 
531       }
532     }
533     break;
534   case 2:
535 
536     /* mapping type 2; explicit array of values */
537     quantvals=s->entries*s->dim;
538     /* dec_type choices here are 1,3; 2 is not possible */
539 
540     if( (s->q_bits*s->dim+8)/8 <=4){ /* remember flag bit */
541       /* use dec_type 1: vector of packed values */
542 
543       s->dec_type=1;
544       s->dec_nodeb=_determine_node_bytes(s->used_entries,(s->q_bits*s->dim+8)/8);
545       s->dec_leafw=_determine_leaf_words(s->dec_nodeb,(s->q_bits*s->dim+8)/8);
546       if(_make_decode_table(s,lengthlist,quantvals,opb,maptype))goto _errout;
547 
548     }else{
549       /* use dec_type 3: scalar offset into packed value array */
550 
551       s->dec_type=3;
552       s->dec_nodeb=_determine_node_bytes(s->used_entries,_ilog(s->used_entries-1)/8+1);
553       s->dec_leafw=_determine_leaf_words(s->dec_nodeb,_ilog(s->used_entries-1)/8+1);
554       if(_make_decode_table(s,lengthlist,quantvals,opb,maptype))goto _errout;
555 
556       /* get the vals & pack them */
557       s->q_pack=(s->q_bits+7)/8*s->dim;
558       s->q_val=_ogg_malloc(s->q_pack*s->used_entries);
559 
560       if(s->q_bits<=8){
561 	for(i=0;i<s->used_entries*s->dim;i++)
562 	  ((unsigned char *)(s->q_val))[i]=oggpack_read(opb,s->q_bits);
563       }else{
564 	for(i=0;i<s->used_entries*s->dim;i++)
565 	  ((ogg_uint16_t *)(s->q_val))[i]=oggpack_read(opb,s->q_bits);
566       }
567     }
568     break;
569   default:
570     goto _errout;
571   }
572 
573   if(oggpack_eop(opb))goto _eofout;
574 
575   return 0;
576  _errout:
577  _eofout:
578   vorbis_book_clear(s);
579   return -1;
580 }
581 
decode_packed_entry_number(codebook * book,oggpack_buffer * b)582 static inline ogg_uint32_t decode_packed_entry_number(codebook *book,
583 						      oggpack_buffer *b){
584   ogg_uint32_t chase=0;
585   int  read=book->dec_maxlength;
586   long lok = oggpack_look(b,read),i;
587 
588   while(lok<0 && read>1)
589     lok = oggpack_look(b, --read);
590 
591   if(lok<0){
592     oggpack_adv(b,1); /* force eop */
593     return -1;
594   }
595 
596   /* chase the tree with the bits we got */
597   if(book->dec_nodeb==1){
598     if(book->dec_leafw==1){
599 
600       /* 8/8 */
601       unsigned char *t=(unsigned char *)book->dec_table;
602       for(i=0;i<read;i++){
603 	chase=t[chase*2+((lok>>i)&1)];
604 	if(chase&0x80UL)break;
605       }
606       chase&=0x7fUL;
607 
608     }else{
609 
610       /* 8/16 */
611       unsigned char *t=(unsigned char *)book->dec_table;
612       for(i=0;i<read;i++){
613 	int bit=(lok>>i)&1;
614 	int next=t[chase+bit];
615 	if(next&0x80){
616 	  chase= (next<<8) | t[chase+bit+1+(!bit || t[chase]&0x80)];
617 	  break;
618 	}
619 	chase=next;
620       }
621       chase&=0x7fffUL;
622     }
623 
624   }else{
625     if(book->dec_nodeb==2){
626       if(book->dec_leafw==1){
627 
628 	/* 16/16 */
629 	for(i=0;i<read;i++){
630 	  chase=((ogg_uint16_t *)(book->dec_table))[chase*2+((lok>>i)&1)];
631 	  if(chase&0x8000UL)break;
632 	}
633 	chase&=0x7fffUL;
634 
635       }else{
636 
637 	/* 16/32 */
638 	ogg_uint16_t *t=(ogg_uint16_t *)book->dec_table;
639 	for(i=0;i<read;i++){
640 	  int bit=(lok>>i)&1;
641 	  int next=t[chase+bit];
642 	  if(next&0x8000){
643 	    chase= (next<<16) | t[chase+bit+1+(!bit || t[chase]&0x8000)];
644 	    break;
645 	  }
646 	  chase=next;
647 	}
648 	chase&=0x7fffffffUL;
649       }
650 
651     }else{
652 
653       for(i=0;i<read;i++){
654 	chase=((ogg_uint32_t *)(book->dec_table))[chase*2+((lok>>i)&1)];
655 	if(chase&0x80000000UL)break;
656       }
657       chase&=0x7fffffffUL;
658 
659     }
660   }
661 
662   if(i<read){
663     oggpack_adv(b,i+1);
664     return chase;
665   }
666   oggpack_adv(b,read+1);
667   return(-1);
668 }
669 
670 /* returns the [original, not compacted] entry number or -1 on eof *********/
vorbis_book_decode(codebook * book,oggpack_buffer * b)671 long vorbis_book_decode(codebook *book, oggpack_buffer *b){
672   if(book->dec_type)return -1;
673  return decode_packed_entry_number(book,b);
674 }
675 
decode_map(codebook * s,oggpack_buffer * b,ogg_int32_t * v,int point)676 int decode_map(codebook *s, oggpack_buffer *b, ogg_int32_t *v, int point){
677   ogg_uint32_t entry = decode_packed_entry_number(s,b);
678   int i;
679   if(oggpack_eop(b))return(-1);
680 
681   /* according to decode type */
682   switch(s->dec_type){
683   case 1:{
684     /* packed vector of values */
685     int mask=(1<<s->q_bits)-1;
686     for(i=0;i<s->dim;i++){
687       v[i]=entry&mask;
688       entry>>=s->q_bits;
689     }
690     break;
691   }
692   case 2:{
693     /* packed vector of column offsets */
694     int mask=(1<<s->q_pack)-1;
695     for(i=0;i<s->dim;i++){
696       if(s->q_bits<=8)
697 	v[i]=((unsigned char *)(s->q_val))[entry&mask];
698       else
699 	v[i]=((ogg_uint16_t *)(s->q_val))[entry&mask];
700       entry>>=s->q_pack;
701     }
702     break;
703   }
704   case 3:{
705     /* offset into array */
706     void *ptr=s->q_val+entry*s->q_pack;
707 
708     if(s->q_bits<=8){
709       for(i=0;i<s->dim;i++)
710 	v[i]=((unsigned char *)ptr)[i];
711     }else{
712       for(i=0;i<s->dim;i++)
713 	v[i]=((ogg_uint16_t *)ptr)[i];
714     }
715     break;
716   }
717   default:
718     return -1;
719   }
720 
721   /* we have the unpacked multiplicands; compute final vals */
722   {
723     int shiftM=point-s->q_delp;
724     ogg_int32_t add=point-s->q_minp;
725     if(add>0)
726       add= s->q_min >> add;
727     else
728       add= s->q_min << -add;
729 
730     if(shiftM>0)
731       for(i=0;i<s->dim;i++)
732 	v[i]= add + ((v[i] * s->q_del) >> shiftM);
733     else
734       for(i=0;i<s->dim;i++)
735 	v[i]= add + ((v[i] * s->q_del) << -shiftM);
736 
737     if(s->q_seq)
738       for(i=1;i<s->dim;i++)
739 	v[i]+=v[i-1];
740   }
741 
742   return 0;
743 }
744 
745 /* returns 0 on OK or -1 on eof *************************************/
vorbis_book_decodevs_add(codebook * book,ogg_int32_t * a,oggpack_buffer * b,int n,int point)746 long vorbis_book_decodevs_add(codebook *book,ogg_int32_t *a,
747 			      oggpack_buffer *b,int n,int point){
748   if(book->used_entries>0){
749     int step=n/book->dim;
750     ogg_int32_t *v = (ogg_int32_t *)alloca(sizeof(*v)*book->dim);
751     int i,j,o;
752     if (!v) return -1;
753 
754     for (j=0;j<step;j++){
755       if(decode_map(book,b,v,point))return -1;
756       for(i=0,o=j;i<book->dim;i++,o+=step)
757 	a[o]+=v[i];
758     }
759   }
760   return 0;
761 }
762 
vorbis_book_decodev_add(codebook * book,ogg_int32_t * a,oggpack_buffer * b,int n,int point)763 long vorbis_book_decodev_add(codebook *book,ogg_int32_t *a,
764 			     oggpack_buffer *b,int n,int point){
765   if(book->used_entries>0){
766     ogg_int32_t *v = (ogg_int32_t *)alloca(sizeof(*v)*book->dim);
767     int i,j;
768 
769     if (!v) return -1;
770     for(i=0;i<n;){
771       if(decode_map(book,b,v,point))return -1;
772       for (j=0;j<book->dim;j++)
773 	a[i++]+=v[j];
774     }
775   }
776   return 0;
777 }
778 
vorbis_book_decodev_set(codebook * book,ogg_int32_t * a,oggpack_buffer * b,int n,int point)779 long vorbis_book_decodev_set(codebook *book,ogg_int32_t *a,
780 			     oggpack_buffer *b,int n,int point){
781   if(book->used_entries>0){
782     ogg_int32_t *v = (ogg_int32_t *)alloca(sizeof(*v)*book->dim);
783     int i,j;
784 
785     if (!v) return -1;
786     for(i=0;i<n;){
787       if(decode_map(book,b,v,point))return -1;
788       for (j=0;j<book->dim;j++)
789 	a[i++]=v[j];
790     }
791   }else{
792     int i,j;
793 
794     for(i=0;i<n;){
795       for (j=0;j<book->dim;j++)
796 	a[i++]=0;
797     }
798   }
799 
800   return 0;
801 }
802 
vorbis_book_decodevv_add(codebook * book,ogg_int32_t ** a,long offset,int ch,oggpack_buffer * b,int n,int point)803 long vorbis_book_decodevv_add(codebook *book,ogg_int32_t **a,
804 			      long offset,int ch,
805 			      oggpack_buffer *b,int n,int point){
806   if(book->used_entries>0){
807 
808     ogg_int32_t *v = (ogg_int32_t *)alloca(sizeof(*v)*book->dim);
809     long i,j;
810     int chptr=0;
811 
812     if (!v) return -1;
813     for(i=offset;i<offset+n;){
814       if(decode_map(book,b,v,point))return -1;
815       for (j=0;j<book->dim;j++){
816 	a[chptr++][i]+=v[j];
817 	if(chptr==ch){
818 	  chptr=0;
819 	  i++;
820 	}
821       }
822     }
823   }
824 
825   return 0;
826 }
827