blob: 5f46594dcba6b18854d9fddbe26a57eeea758580 [file] [log] [blame]
Gloria Wang79130732010-02-08 14:41:04 -08001/********************************************************************
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 ******************************************/
30int _ilog(unsigned int v){
31 int ret=0;
32 while(v){
33 ret++;
34 v>>=1;
35 }
36 return(ret);
37}
38
39static 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
82static 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 */
102static 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) */
117static 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 */
124static 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
196static 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(_make_words(lengthlist,s->entries,work,quantvals,s,opb,maptype))return 1;
224 if (s->used_entries > INT_MAX/(s->dec_leafw+1)) return 1;
225 if (s->dec_nodeb && s->used_entries * (s->dec_leafw+1) > INT_MAX/s->dec_nodeb) return 1;
226 s->dec_table=_ogg_malloc((s->used_entries*(s->dec_leafw+1)-2)*
227 s->dec_nodeb);
228 if (!s->dec_table) return 1;
229
230 if(s->dec_leafw==1){
231 switch(s->dec_nodeb){
232 case 1:
233 for(i=0;i<s->used_entries*2-2;i++)
234 ((unsigned char *)s->dec_table)[i]=(unsigned char)
235 (((work[i] & 0x80000000UL) >> 24) | work[i]);
236 break;
237 case 2:
238 for(i=0;i<s->used_entries*2-2;i++)
239 ((ogg_uint16_t *)s->dec_table)[i]=(ogg_uint16_t)
240 (((work[i] & 0x80000000UL) >> 16) | work[i]);
241 break;
242 }
243
244 }else{
245 /* more complex; we have to do a two-pass repack that updates the
246 node indexing. */
247 long top=s->used_entries*3-2;
248 if(s->dec_nodeb==1){
249 unsigned char *out=(unsigned char *)s->dec_table;
250
251 for(i=s->used_entries*2-4;i>=0;i-=2){
252 if(work[i]&0x80000000UL){
253 if(work[i+1]&0x80000000UL){
254 top-=4;
255 out[top]=(work[i]>>8 & 0x7f)|0x80;
256 out[top+1]=(work[i+1]>>8 & 0x7f)|0x80;
257 out[top+2]=work[i] & 0xff;
258 out[top+3]=work[i+1] & 0xff;
259 }else{
260 top-=3;
261 out[top]=(work[i]>>8 & 0x7f)|0x80;
262 out[top+1]=work[work[i+1]*2];
263 out[top+2]=work[i] & 0xff;
264 }
265 }else{
266 if(work[i+1]&0x80000000UL){
267 top-=3;
268 out[top]=work[work[i]*2];
269 out[top+1]=(work[i+1]>>8 & 0x7f)|0x80;
270 out[top+2]=work[i+1] & 0xff;
271 }else{
272 top-=2;
273 out[top]=work[work[i]*2];
274 out[top+1]=work[work[i+1]*2];
275 }
276 }
277 work[i]=top;
278 }
279 }else{
280 ogg_uint16_t *out=(ogg_uint16_t *)s->dec_table;
281 for(i=s->used_entries*2-4;i>=0;i-=2){
282 if(work[i]&0x80000000UL){
283 if(work[i+1]&0x80000000UL){
284 top-=4;
285 out[top]=(work[i]>>16 & 0x7fff)|0x8000;
286 out[top+1]=(work[i+1]>>16 & 0x7fff)|0x8000;
287 out[top+2]=work[i] & 0xffff;
288 out[top+3]=work[i+1] & 0xffff;
289 }else{
290 top-=3;
291 out[top]=(work[i]>>16 & 0x7fff)|0x8000;
292 out[top+1]=work[work[i+1]*2];
293 out[top+2]=work[i] & 0xffff;
294 }
295 }else{
296 if(work[i+1]&0x80000000UL){
297 top-=3;
298 out[top]=work[work[i]*2];
299 out[top+1]=(work[i+1]>>16 & 0x7fff)|0x8000;
300 out[top+2]=work[i+1] & 0xffff;
301 }else{
302 top-=2;
303 out[top]=work[work[i]*2];
304 out[top+1]=work[work[i+1]*2];
305 }
306 }
307 work[i]=top;
308 }
309 }
310 }
311
312 return 0;
313}
314
315/* most of the time, entries%dimensions == 0, but we need to be
316 well defined. We define that the possible vales at each
317 scalar is values == entries/dim. If entries%dim != 0, we'll
318 have 'too few' values (values*dim<entries), which means that
319 we'll have 'left over' entries; left over entries use zeroed
320 values (and are wasted). So don't generate codebooks like
321 that */
322/* there might be a straightforward one-line way to do the below
323 that's portable and totally safe against roundoff, but I haven't
324 thought of it. Therefore, we opt on the side of caution */
325long _book_maptype1_quantvals(codebook *b){
326 /* get us a starting hint, we'll polish it below */
327 int bits=_ilog(b->entries);
328 int vals=b->entries>>((bits-1)*(b->dim-1)/b->dim);
329
330 while(1){
331 long acc=1;
332 long acc1=1;
333 int i;
334 for(i=0;i<b->dim;i++){
335 acc*=vals;
336 acc1*=vals+1;
337 }
338 if(acc<=b->entries && acc1>b->entries){
339 return(vals);
340 }else{
341 if(acc>b->entries){
342 vals--;
343 }else{
344 vals++;
345 }
346 }
347 }
348}
349
350void vorbis_book_clear(codebook *b){
351 /* static book is not cleared; we're likely called on the lookup and
352 the static codebook belongs to the info struct */
353 if(b->q_val)_ogg_free(b->q_val);
354 if(b->dec_table)_ogg_free(b->dec_table);
355 if(b->dec_buf)_ogg_free(b->dec_buf);
356
357 memset(b,0,sizeof(*b));
358}
359
360int 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->dec_buf=_ogg_malloc(sizeof(ogg_int32_t)*s->dim);
374 if (s->dec_buf == NULL)
375 goto _errout;
376 s->entries=oggpack_read(opb,24);
377 if(s->entries<=0)goto _eofout;
378 if(s->dim<=0)goto _eofout;
379 if(_ilog(s->dim)+_ilog(s->entries)>24)goto _eofout;
380 if (s->dim > INT_MAX/s->entries) goto _eofout;
381
382 /* codeword ordering.... length ordered or unordered? */
383 switch((int)oggpack_read(opb,1)){
384 case 0:
385 /* unordered */
386 lengthlist=(char *)alloca(sizeof(*lengthlist)*s->entries);
387 if(!lengthlist) goto _eofout;
388
389 /* allocated but unused entries? */
390 if(oggpack_read(opb,1)){
391 /* yes, unused entries */
392
393 for(i=0;i<s->entries;i++){
394 if(oggpack_read(opb,1)){
395 long num=oggpack_read(opb,5);
396 if(num==-1)goto _eofout;
397 lengthlist[i]=(char)(num+1);
398 s->used_entries++;
399 if(num+1>s->dec_maxlength)s->dec_maxlength=num+1;
400 }else
401 lengthlist[i]=0;
402 }
403 }else{
404 /* all entries used; no tagging */
405 s->used_entries=s->entries;
406 for(i=0;i<s->entries;i++){
407 long num=oggpack_read(opb,5);
408 if(num==-1)goto _eofout;
409 lengthlist[i]=(char)(num+1);
410 if(num+1>s->dec_maxlength)s->dec_maxlength=num+1;
411 }
412 }
413
414 break;
415 case 1:
416 /* ordered */
417 {
418 long length=oggpack_read(opb,5)+1;
419
420 s->used_entries=s->entries;
421 lengthlist=(char *)alloca(sizeof(*lengthlist)*s->entries);
422 if (!lengthlist) goto _eofout;
423
424 for(i=0;i<s->entries;){
425 long num=oggpack_read(opb,_ilog(s->entries-i));
426 if(num<0)goto _eofout;
427 for(j=0;j<num && i<s->entries;j++,i++)
428 lengthlist[i]=(char)length;
429 s->dec_maxlength=length;
430 length++;
431 }
432 }
433 break;
434 default:
435 /* EOF */
436 goto _eofout;
437 }
438
439
440 /* Do we have a mapping to unpack? */
441
442 if((maptype=oggpack_read(opb,4))>0){
443 s->q_min=_float32_unpack(oggpack_read(opb,32),&s->q_minp);
444 s->q_del=_float32_unpack(oggpack_read(opb,32),&s->q_delp);
445 s->q_bits=oggpack_read(opb,4)+1;
446 s->q_seq=oggpack_read(opb,1);
447
448 s->q_del>>=s->q_bits;
449 s->q_delp+=s->q_bits;
450 }
451
452 switch(maptype){
453 case 0:
454
455 /* no mapping; decode type 0 */
456
457 /* how many bytes for the indexing? */
458 /* this is the correct boundary here; we lose one bit to
459 node/leaf mark */
460 s->dec_nodeb=_determine_node_bytes(s->used_entries,_ilog(s->entries)/8+1);
461 s->dec_leafw=_determine_leaf_words(s->dec_nodeb,_ilog(s->entries)/8+1);
462 s->dec_type=0;
463
464 if(_make_decode_table(s,lengthlist,quantvals,opb,maptype)) goto _errout;
465 break;
466
467 case 1:
468
469 /* mapping type 1; implicit values by lattice position */
470 quantvals=_book_maptype1_quantvals(s);
471
472 /* dec_type choices here are 1,2; 3 doesn't make sense */
473 {
474 /* packed values */
475 long total1=(s->q_bits*s->dim+8)/8; /* remember flag bit */
476 if (s->dim > (INT_MAX-8)/s->q_bits) goto _eofout;
477 /* vector of column offsets; remember flag bit */
478 long total2=(_ilog(quantvals-1)*s->dim+8)/8+(s->q_bits+7)/8;
479
480
481 if(total1<=4 && total1<=total2){
482 /* use dec_type 1: vector of packed values */
483
484 /* need quantized values before */
485 s->q_val=alloca(sizeof(ogg_uint16_t)*quantvals);
486 if (!s->q_val) goto _eofout;
487 for(i=0;i<quantvals;i++)
488 ((ogg_uint16_t *)s->q_val)[i]=(ogg_uint16_t)oggpack_read(opb,s->q_bits);
489
490 if(oggpack_eop(opb)){
491 s->q_val=0; /* cleanup must not free alloca memory */
492 goto _eofout;
493 }
494
495 s->dec_type=1;
496 s->dec_nodeb=_determine_node_bytes(s->used_entries,
497 (s->q_bits*s->dim+8)/8);
498 s->dec_leafw=_determine_leaf_words(s->dec_nodeb,
499 (s->q_bits*s->dim+8)/8);
500 if(_make_decode_table(s,lengthlist,quantvals,opb,maptype)){
501 s->q_val=0; /* cleanup must not free alloca memory */
502 goto _errout;
503 }
504
505 s->q_val=0; /* about to go out of scope; _make_decode_table
506 was using it */
507
508 }else{
509 /* use dec_type 2: packed vector of column offsets */
510
511 /* need quantized values before */
512 if(s->q_bits<=8){
513 s->q_val=_ogg_malloc(quantvals);
514 if (!s->q_val) goto _eofout;
515 for(i=0;i<quantvals;i++)
516 ((unsigned char *)s->q_val)[i]=(unsigned char)oggpack_read(opb,s->q_bits);
517 }else{
518 s->q_val=_ogg_malloc(quantvals*2);
519 if (!s->q_val) goto _eofout;
520 for(i=0;i<quantvals;i++)
521 ((ogg_uint16_t *)s->q_val)[i]=(ogg_uint16_t)oggpack_read(opb,s->q_bits);
522 }
523
524 if(oggpack_eop(opb))goto _eofout;
525
526 s->q_pack=_ilog(quantvals-1);
527 s->dec_type=2;
528 s->dec_nodeb=_determine_node_bytes(s->used_entries,
529 (_ilog(quantvals-1)*s->dim+8)/8);
530 s->dec_leafw=_determine_leaf_words(s->dec_nodeb,
531 (_ilog(quantvals-1)*s->dim+8)/8);
532 if(_make_decode_table(s,lengthlist,quantvals,opb,maptype))goto _errout;
533
534 }
535 }
536 break;
537 case 2:
538
539 /* mapping type 2; explicit array of values */
540 quantvals=s->entries*s->dim;
541 /* dec_type choices here are 1,3; 2 is not possible */
542
543 if( (s->q_bits*s->dim+8)/8 <=4){ /* remember flag bit */
544 /* use dec_type 1: vector of packed values */
545
546 s->dec_type=1;
547 s->dec_nodeb=_determine_node_bytes(s->used_entries,(s->q_bits*s->dim+8)/8);
548 s->dec_leafw=_determine_leaf_words(s->dec_nodeb,(s->q_bits*s->dim+8)/8);
549 if(_make_decode_table(s,lengthlist,quantvals,opb,maptype))goto _errout;
550
551 }else{
552 /* use dec_type 3: scalar offset into packed value array */
553
554 s->dec_type=3;
555 s->dec_nodeb=_determine_node_bytes(s->used_entries,_ilog(s->used_entries-1)/8+1);
556 s->dec_leafw=_determine_leaf_words(s->dec_nodeb,_ilog(s->used_entries-1)/8+1);
557 if(_make_decode_table(s,lengthlist,quantvals,opb,maptype))goto _errout;
558
559 /* get the vals & pack them */
560 s->q_pack=(s->q_bits+7)/8*s->dim;
561 s->q_val=_ogg_malloc(s->q_pack*s->used_entries);
562
563 if(s->q_bits<=8){
564 for(i=0;i<s->used_entries*s->dim;i++)
565 ((unsigned char *)(s->q_val))[i]=(unsigned char)oggpack_read(opb,s->q_bits);
566 }else{
567 for(i=0;i<s->used_entries*s->dim;i++)
568 ((ogg_uint16_t *)(s->q_val))[i]=(ogg_uint16_t)oggpack_read(opb,s->q_bits);
569 }
570 }
571 break;
572 default:
573 goto _errout;
574 }
575
576 if (s->dec_nodeb==1)
577 if (s->dec_leafw == 1)
578 s->dec_method = 0;
579 else
580 s->dec_method = 1;
581 else if (s->dec_nodeb==2)
582 if (s->dec_leafw == 1)
583 s->dec_method = 2;
584 else
585 s->dec_method = 3;
586 else
587 s->dec_method = 4;
588
589 if(oggpack_eop(opb))goto _eofout;
590
591 return 0;
592 _errout:
593 _eofout:
594 vorbis_book_clear(s);
595 return -1;
596}
597
598#ifndef ONLY_C
599ogg_uint32_t decode_packed_entry_number(codebook *book,
600 oggpack_buffer *b);
601#else
602static inline ogg_uint32_t decode_packed_entry_number(codebook *book,
603 oggpack_buffer *b){
604 ogg_uint32_t chase=0;
605 int read=book->dec_maxlength;
606 long lok = oggpack_look(b,read),i;
607
608 while(lok<0 && read>1)
609 lok = oggpack_look(b, --read);
610
611 if(lok<0){
612 oggpack_adv(b,1); /* force eop */
613 return -1;
614 }
615
616 /* chase the tree with the bits we got */
617 switch (book->dec_method)
618 {
619 case 0:
620 {
621 /* book->dec_nodeb==1, book->dec_leafw==1 */
622 /* 8/8 - Used */
623 unsigned char *t=(unsigned char *)book->dec_table;
624
625 for(i=0;i<read;i++){
626 chase=t[chase*2+((lok>>i)&1)];
627 if(chase&0x80UL)break;
628 }
629 chase&=0x7fUL;
630 break;
631 }
632 case 1:
633 {
634 /* book->dec_nodeb==1, book->dec_leafw!=1 */
635 /* 8/16 - Used by infile2 */
636 unsigned char *t=(unsigned char *)book->dec_table;
637 for(i=0;i<read;i++){
638 int bit=(lok>>i)&1;
639 int next=t[chase+bit];
640 if(next&0x80){
641 chase= (next<<8) | t[chase+bit+1+(!bit || t[chase]&0x80)];
642 break;
643 }
644 chase=next;
645 }
646 //chase&=0x7fffUL;
647 chase&=~0x8000UL;
648 break;
649 }
650 case 2:
651 {
652 /* book->dec_nodeb==2, book->dec_leafw==1 */
653 /* 16/16 - Used */
654 for(i=0;i<read;i++){
655 chase=((ogg_uint16_t *)(book->dec_table))[chase*2+((lok>>i)&1)];
656 if(chase&0x8000UL)break;
657 }
658 //chase&=0x7fffUL;
659 chase&=~0x8000UL;
660 break;
661 }
662 case 3:
663 {
664 /* book->dec_nodeb==2, book->dec_leafw!=1 */
665 /* 16/32 - Used by infile2 */
666 ogg_uint16_t *t=(ogg_uint16_t *)book->dec_table;
667 for(i=0;i<read;i++){
668 int bit=(lok>>i)&1;
669 int next=t[chase+bit];
670 if(next&0x8000){
671 chase= (next<<16) | t[chase+bit+1+(!bit || t[chase]&0x8000)];
672 break;
673 }
674 chase=next;
675 }
676 //chase&=0x7fffffffUL;
677 chase&=~0x80000000UL;
678 break;
679 }
680 case 4:
681 {
682 Output("32/32");
683 for(i=0;i<read;i++){
684 chase=((ogg_uint32_t *)(book->dec_table))[chase*2+((lok>>i)&1)];
685 if(chase&0x80000000UL)break;
686 }
687 //chase&=0x7fffffffUL;
688 chase&=~0x80000000UL;
689 break;
690 }
691 }
692
693 if(i<read){
694 oggpack_adv(b,i+1);
695 return chase;
696 }
697 oggpack_adv(b,read+1);
698 return(-1);
699}
700#endif
701
702/* returns the [original, not compacted] entry number or -1 on eof *********/
703long vorbis_book_decode(codebook *book, oggpack_buffer *b){
704 if(book->dec_type)return -1;
705 return decode_packed_entry_number(book,b);
706}
707
708#ifndef ONLY_C
709int decode_map(codebook *s, oggpack_buffer *b, ogg_int32_t *v, int point);
710#else
711static int decode_map(codebook *s, oggpack_buffer *b, ogg_int32_t *v, int point){
712 ogg_uint32_t entry = decode_packed_entry_number(s,b);
713 int i;
714 if(oggpack_eop(b))return(-1);
715
716 /* 1 used by test file 0 */
717
718 /* according to decode type */
719 switch(s->dec_type){
720 case 1:{
721 /* packed vector of values */
722 int mask=(1<<s->q_bits)-1;
723 for(i=0;i<s->dim;i++){
724 v[i]=entry&mask;
725 entry>>=s->q_bits;
726 }
727 break;
728 }
729 case 2:{
730 /* packed vector of column offsets */
731 int mask=(1<<s->q_pack)-1;
732 for(i=0;i<s->dim;i++){
733 if(s->q_bits<=8)
734 v[i]=((unsigned char *)(s->q_val))[entry&mask];
735 else
736 v[i]=((ogg_uint16_t *)(s->q_val))[entry&mask];
737 entry>>=s->q_pack;
738 }
739 break;
740 }
741 case 3:{
742 /* offset into array */
743 void *ptr=s->q_val+entry*s->q_pack;
744
745 if(s->q_bits<=8){
746 for(i=0;i<s->dim;i++)
747 v[i]=((unsigned char *)ptr)[i];
748 }else{
749 for(i=0;i<s->dim;i++)
750 v[i]=((ogg_uint16_t *)ptr)[i];
751 }
752 break;
753 }
754 default:
755 return -1;
756 }
757
758 /* we have the unpacked multiplicands; compute final vals */
759 {
760 int shiftM = point-s->q_delp;
761 ogg_int32_t add = point-s->q_minp;
762 int mul = s->q_del;
763
764 if(add>0)
765 add= s->q_min >> add;
766 else
767 add= s->q_min << -add;
768 if (shiftM<0)
769 {
770 mul <<= -shiftM;
771 shiftM = 0;
772 }
773 add <<= shiftM;
774
775 for(i=0;i<s->dim;i++)
776 v[i]= ((add + v[i] * mul) >> shiftM);
777
778 if(s->q_seq)
779 for(i=1;i<s->dim;i++)
780 v[i]+=v[i-1];
781 }
782
783 return 0;
784}
785#endif
786
787/* returns 0 on OK or -1 on eof *************************************/
788long vorbis_book_decodevs_add(codebook *book,ogg_int32_t *a,
789 oggpack_buffer *b,int n,int point){
790 if(book->used_entries>0){
791 int step=n/book->dim;
792 ogg_int32_t *v = book->dec_buf;//(ogg_int32_t *)alloca(sizeof(*v)*book->dim);
793 int i,j,o;
794 if (!v) return -1;
795
796 for (j=0;j<step;j++){
797 if(decode_map(book,b,v,point))return -1;
798 for(i=0,o=j;i<book->dim;i++,o+=step)
799 a[o]+=v[i];
800 }
801 }
802 return 0;
803}
804
805long vorbis_book_decodev_add(codebook *book,ogg_int32_t *a,
806 oggpack_buffer *b,int n,int point){
807 if(book->used_entries>0){
808 ogg_int32_t *v = book->dec_buf;//(ogg_int32_t *)alloca(sizeof(*v)*book->dim);
809 int i,j;
810
811 if (!v) return -1;
812 for(i=0;i<n;){
813 if(decode_map(book,b,v,point))return -1;
814 for (j=0;j<book->dim;j++)
815 a[i++]+=v[j];
816 }
817 }
818 return 0;
819}
820
821long vorbis_book_decodev_set(codebook *book,ogg_int32_t *a,
822 oggpack_buffer *b,int n,int point){
823 if(book->used_entries>0){
824 ogg_int32_t *v = book->dec_buf;//(ogg_int32_t *)alloca(sizeof(*v)*book->dim);
825 int i,j;
826
827 if (!v) return -1;
828 for(i=0;i<n;){
829 if(decode_map(book,b,v,point))return -1;
830 for (j=0;j<book->dim;j++)
831 a[i++]=v[j];
832 }
833 }else{
834 int i,j;
835
836 for(i=0;i<n;){
837 for (j=0;j<book->dim;j++)
838 a[i++]=0;
839 }
840 }
841
842 return 0;
843}
844
845#ifndef ONLY_C
846long vorbis_book_decodevv_add(codebook *book,ogg_int32_t **a,
847 long offset,int ch,
848 oggpack_buffer *b,int n,int point);
849#else
850long vorbis_book_decodevv_add(codebook *book,ogg_int32_t **a,
851 long offset,int ch,
852 oggpack_buffer *b,int n,int point){
853 if(book->used_entries>0){
854
855 ogg_int32_t *v = book->dec_buf;//(ogg_int32_t *)alloca(sizeof(*v)*book->dim);
856 long i,j;
857 int chptr=0;
858
859 if (!v) return -1;
860 for(i=offset;i<offset+n;){
861 if(decode_map(book,b,v,point))return -1;
862 for (j=0;j<book->dim;j++){
863 a[chptr++][i]+=v[j];
864 if(chptr==ch){
865 chptr=0;
866 i++;
867 }
868 }
869 }
870 }
871
872 return 0;
873}
874#endif