blob: 0ff3529b81c09af0702dc65c6b6bc88480ba316b [file] [log] [blame]
Gloria Wang37fe1582010-03-12 14:53:20 -08001/************************************************************************
Gloria Wang2da723a2010-03-18 15:56:16 -07002 * Copyright (C) 2002-2009, Xiph.org Foundation
3 * Copyright (C) 2010, Robin Watts for Pinknoise Productions Ltd
Gloria Wang37fe1582010-03-12 14:53:20 -08004 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
Gloria Wang2da723a2010-03-18 15:56:16 -07007 * modification, are permitted provided that the following conditions
8 * are met:
Gloria Wang37fe1582010-03-12 14:53:20 -08009 *
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following disclaimer
14 * in the documentation and/or other materials provided with the
15 * distribution.
Gloria Wang2da723a2010-03-18 15:56:16 -070016 * * Neither the names of the Xiph.org Foundation nor Pinknoise
17 * Productions Ltd nor the names of its contributors may be used to
18 * endorse or promote products derived from this software without
19 * specific prior written permission.
Gloria Wang37fe1582010-03-12 14:53:20 -080020 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 ************************************************************************
Gloria Wang79130732010-02-08 14:41:04 -080033
34 function: basic codebook pack/unpack/code/decode operations
35
Gloria Wang37fe1582010-03-12 14:53:20 -080036 ************************************************************************/
Gloria Wang79130732010-02-08 14:41:04 -080037
38#include <stdlib.h>
39#include <string.h>
40#include <math.h>
41#include <limits.h>
42#include "ogg.h"
43#include "ivorbiscodec.h"
44#include "codebook.h"
45#include "misc.h"
46#include "os.h"
47
48
49/**** pack/unpack helpers ******************************************/
50int _ilog(unsigned int v){
51 int ret=0;
52 while(v){
53 ret++;
54 v>>=1;
55 }
56 return(ret);
57}
58
59static ogg_uint32_t decpack(long entry,long used_entry,long quantvals,
60 codebook *b,oggpack_buffer *opb,int maptype){
61 ogg_uint32_t ret=0;
62 int j;
63
64 switch(b->dec_type){
65
66 case 0:
67 return (ogg_uint32_t)entry;
68
69 case 1:
70 if(maptype==1){
71 /* vals are already read into temporary column vector here */
72 for(j=0;j<b->dim;j++){
73 ogg_uint32_t off=entry%quantvals;
74 entry/=quantvals;
75 ret|=((ogg_uint16_t *)(b->q_val))[off]<<(b->q_bits*j);
76 }
77 }else{
78 for(j=0;j<b->dim;j++)
79 ret|=oggpack_read(opb,b->q_bits)<<(b->q_bits*j);
80 }
81 return ret;
82
83 case 2:
84 for(j=0;j<b->dim;j++){
85 ogg_uint32_t off=entry%quantvals;
86 entry/=quantvals;
87 ret|=off<<(b->q_pack*j);
88 }
89 return ret;
90
91 case 3:
92 return (ogg_uint32_t)used_entry;
93
94 }
95 return 0; /* silence compiler */
96}
97
98/* 32 bit float (not IEEE; nonnormalized mantissa +
99 biased exponent) : neeeeeee eeemmmmm mmmmmmmm mmmmmmmm
100 Why not IEEE? It's just not that important here. */
101
102static ogg_int32_t _float32_unpack(long val,int *point){
103 long mant=val&0x1fffff;
104 int sign=val&0x80000000;
105
106 *point=((val&0x7fe00000L)>>21)-788;
107
108 if(mant){
109 while(!(mant&0x40000000)){
110 mant<<=1;
111 *point-=1;
112 }
113 if(sign)mant= -mant;
114 }else{
115 *point=-9999;
116 }
117 return mant;
118}
119
120/* choose the smallest supported node size that fits our decode table.
121 Legal bytewidths are 1/1 1/2 2/2 2/4 4/4 */
122static int _determine_node_bytes(long used, int leafwidth){
123
124 /* special case small books to size 4 to avoid multiple special
125 cases in repack */
126 if(used<2)
127 return 4;
128
129 if(leafwidth==3)leafwidth=4;
130 if(_ilog(3*used-6)+1 <= leafwidth*4)
131 return leafwidth/2?leafwidth/2:1;
132 return leafwidth;
133}
134
135/* convenience/clarity; leaves are specified as multiple of node word
136 size (1 or 2) */
137static int _determine_leaf_words(int nodeb, int leafwidth){
138 if(leafwidth>nodeb)return 2;
139 return 1;
140}
141
142/* given a list of word lengths, number of used entries, and byte
143 width of a leaf, generate the decode table */
144static int _make_words(char *l,long n,ogg_uint32_t *r,long quantvals,
145 codebook *b, oggpack_buffer *opb,int maptype){
146 long i,j,count=0;
147 long top=0;
148 ogg_uint32_t marker[33];
149
150 if (n<1)
151 return 1;
152
153 if(n<2){
154 r[0]=0x80000000;
155 }else{
156 memset(marker,0,sizeof(marker));
157
158 for(i=0;i<n;i++){
159 long length=l[i];
160 if(length){
161 ogg_uint32_t entry=marker[length];
162 long chase=0;
163 if(count && !entry)return -1; /* overpopulated tree! */
164
165 /* chase the tree as far as it's already populated, fill in past */
166 for(j=0;j<length-1;j++){
167 int bit=(entry>>(length-j-1))&1;
168 if(chase>=top){
169 if (chase < 0 || chase >= n) return 1;
170 top++;
171 r[chase*2]=top;
172 r[chase*2+1]=0;
173 }else
174 if (chase < 0 || chase >= n || chase*2+bit > n*2+1) return 1;
175 if(!r[chase*2+bit])
176 r[chase*2+bit]=top;
177 chase=r[chase*2+bit];
178 if (chase < 0 || chase >= n) return 1;
179 }
180 {
181 int bit=(entry>>(length-j-1))&1;
182 if(chase>=top){
183 top++;
184 r[chase*2+1]=0;
185 }
186 r[chase*2+bit]= decpack(i,count++,quantvals,b,opb,maptype) |
187 0x80000000;
188 }
189
190 /* Look to see if the next shorter marker points to the node
191 above. if so, update it and repeat. */
192 for(j=length;j>0;j--){
193 if(marker[j]&1){
194 marker[j]=marker[j-1]<<1;
195 break;
196 }
197 marker[j]++;
198 }
199
200 /* prune the tree; the implicit invariant says all the longer
201 markers were dangling from our just-taken node. Dangle them
202 from our *new* node. */
203 for(j=length+1;j<33;j++)
204 if((marker[j]>>1) == entry){
205 entry=marker[j];
206 marker[j]=marker[j-1]<<1;
207 }else
208 break;
209 }
210 }
211 }
212
213 return 0;
214}
215
216static int _make_decode_table(codebook *s,char *lengthlist,long quantvals,
217 oggpack_buffer *opb,int maptype){
218 int i;
219 ogg_uint32_t *work;
220
221 if (!lengthlist) return 1;
222 if(s->dec_nodeb==4){
223 /* Over-allocate by using s->entries instead of used_entries.
224 * This means that we can use s->entries to enforce size in
225 * _make_words without messing up length list looping.
226 * This probably wastes a bit of space, but it shouldn't
227 * impact behavior or size too much.
228 */
229 s->dec_table=_ogg_malloc((s->entries*2+1)*sizeof(*work));
230 if (!s->dec_table) return 1;
231 /* +1 (rather than -2) is to accommodate 0 and 1 sized books,
232 which are specialcased to nodeb==4 */
233 if(_make_words(lengthlist,s->entries,
234 s->dec_table,quantvals,s,opb,maptype))return 1;
235
236 return 0;
237 }
238
239 if (s->used_entries > INT_MAX/2 ||
240 s->used_entries*2 > INT_MAX/((long) sizeof(*work)) - 1) return 1;
241 /* Overallocate as above */
Marco Nelissenff9b5b22014-03-26 10:54:53 -0700242 work=calloc((s->entries*2+1),sizeof(*work));
243 if (!work) return 1;
244 if(_make_words(lengthlist,s->entries,work,quantvals,s,opb,maptype)) goto error_out;
245 if (s->used_entries > INT_MAX/(s->dec_leafw+1)) goto error_out;
246 if (s->dec_nodeb && s->used_entries * (s->dec_leafw+1) > INT_MAX/s->dec_nodeb) goto error_out;
Gloria Wang79130732010-02-08 14:41:04 -0800247 s->dec_table=_ogg_malloc((s->used_entries*(s->dec_leafw+1)-2)*
248 s->dec_nodeb);
Marco Nelissenff9b5b22014-03-26 10:54:53 -0700249 if (!s->dec_table) goto error_out;
Gloria Wang79130732010-02-08 14:41:04 -0800250
251 if(s->dec_leafw==1){
252 switch(s->dec_nodeb){
253 case 1:
254 for(i=0;i<s->used_entries*2-2;i++)
255 ((unsigned char *)s->dec_table)[i]=(unsigned char)
256 (((work[i] & 0x80000000UL) >> 24) | work[i]);
257 break;
258 case 2:
259 for(i=0;i<s->used_entries*2-2;i++)
260 ((ogg_uint16_t *)s->dec_table)[i]=(ogg_uint16_t)
261 (((work[i] & 0x80000000UL) >> 16) | work[i]);
262 break;
263 }
264
265 }else{
266 /* more complex; we have to do a two-pass repack that updates the
267 node indexing. */
268 long top=s->used_entries*3-2;
269 if(s->dec_nodeb==1){
270 unsigned char *out=(unsigned char *)s->dec_table;
271
272 for(i=s->used_entries*2-4;i>=0;i-=2){
273 if(work[i]&0x80000000UL){
274 if(work[i+1]&0x80000000UL){
275 top-=4;
276 out[top]=(work[i]>>8 & 0x7f)|0x80;
277 out[top+1]=(work[i+1]>>8 & 0x7f)|0x80;
278 out[top+2]=work[i] & 0xff;
279 out[top+3]=work[i+1] & 0xff;
280 }else{
281 top-=3;
282 out[top]=(work[i]>>8 & 0x7f)|0x80;
283 out[top+1]=work[work[i+1]*2];
284 out[top+2]=work[i] & 0xff;
285 }
286 }else{
287 if(work[i+1]&0x80000000UL){
288 top-=3;
289 out[top]=work[work[i]*2];
290 out[top+1]=(work[i+1]>>8 & 0x7f)|0x80;
291 out[top+2]=work[i+1] & 0xff;
292 }else{
293 top-=2;
294 out[top]=work[work[i]*2];
295 out[top+1]=work[work[i+1]*2];
296 }
297 }
298 work[i]=top;
299 }
300 }else{
301 ogg_uint16_t *out=(ogg_uint16_t *)s->dec_table;
302 for(i=s->used_entries*2-4;i>=0;i-=2){
303 if(work[i]&0x80000000UL){
304 if(work[i+1]&0x80000000UL){
305 top-=4;
306 out[top]=(work[i]>>16 & 0x7fff)|0x8000;
307 out[top+1]=(work[i+1]>>16 & 0x7fff)|0x8000;
308 out[top+2]=work[i] & 0xffff;
309 out[top+3]=work[i+1] & 0xffff;
310 }else{
311 top-=3;
312 out[top]=(work[i]>>16 & 0x7fff)|0x8000;
313 out[top+1]=work[work[i+1]*2];
314 out[top+2]=work[i] & 0xffff;
315 }
316 }else{
317 if(work[i+1]&0x80000000UL){
318 top-=3;
319 out[top]=work[work[i]*2];
320 out[top+1]=(work[i+1]>>16 & 0x7fff)|0x8000;
321 out[top+2]=work[i+1] & 0xffff;
322 }else{
323 top-=2;
324 out[top]=work[work[i]*2];
325 out[top+1]=work[work[i+1]*2];
326 }
327 }
328 work[i]=top;
329 }
330 }
331 }
332
Marco Nelissenff9b5b22014-03-26 10:54:53 -0700333 free(work);
Gloria Wang79130732010-02-08 14:41:04 -0800334 return 0;
Marco Nelissenff9b5b22014-03-26 10:54:53 -0700335error_out:
336 free(work);
337 return 1;
Gloria Wang79130732010-02-08 14:41:04 -0800338}
339
340/* most of the time, entries%dimensions == 0, but we need to be
341 well defined. We define that the possible vales at each
342 scalar is values == entries/dim. If entries%dim != 0, we'll
343 have 'too few' values (values*dim<entries), which means that
344 we'll have 'left over' entries; left over entries use zeroed
345 values (and are wasted). So don't generate codebooks like
346 that */
347/* there might be a straightforward one-line way to do the below
348 that's portable and totally safe against roundoff, but I haven't
349 thought of it. Therefore, we opt on the side of caution */
350long _book_maptype1_quantvals(codebook *b){
351 /* get us a starting hint, we'll polish it below */
352 int bits=_ilog(b->entries);
353 int vals=b->entries>>((bits-1)*(b->dim-1)/b->dim);
354
355 while(1){
356 long acc=1;
357 long acc1=1;
358 int i;
359 for(i=0;i<b->dim;i++){
360 acc*=vals;
361 acc1*=vals+1;
362 }
363 if(acc<=b->entries && acc1>b->entries){
364 return(vals);
365 }else{
366 if(acc>b->entries){
367 vals--;
368 }else{
369 vals++;
370 }
371 }
372 }
373}
374
375void vorbis_book_clear(codebook *b){
376 /* static book is not cleared; we're likely called on the lookup and
377 the static codebook belongs to the info struct */
378 if(b->q_val)_ogg_free(b->q_val);
379 if(b->dec_table)_ogg_free(b->dec_table);
380 if(b->dec_buf)_ogg_free(b->dec_buf);
381
382 memset(b,0,sizeof(*b));
383}
384
385int vorbis_book_unpack(oggpack_buffer *opb,codebook *s){
386 char *lengthlist=NULL;
387 int quantvals=0;
388 long i,j;
389 int maptype;
390
391 memset(s,0,sizeof(*s));
392
393 /* make sure alignment is correct */
394 if(oggpack_read(opb,24)!=0x564342)goto _eofout;
395
396 /* first the basic parameters */
397 s->dim=oggpack_read(opb,16);
398 s->dec_buf=_ogg_malloc(sizeof(ogg_int32_t)*s->dim);
399 if (s->dec_buf == NULL)
400 goto _errout;
401 s->entries=oggpack_read(opb,24);
402 if(s->entries<=0)goto _eofout;
403 if(s->dim<=0)goto _eofout;
404 if(_ilog(s->dim)+_ilog(s->entries)>24)goto _eofout;
405 if (s->dim > INT_MAX/s->entries) goto _eofout;
406
407 /* codeword ordering.... length ordered or unordered? */
408 switch((int)oggpack_read(opb,1)){
409 case 0:
410 /* unordered */
Marco Nelissenafa1f6b2013-10-11 15:46:30 -0700411 lengthlist=(char *)calloc(s->entries, sizeof(*lengthlist));
Gloria Wang79130732010-02-08 14:41:04 -0800412 if(!lengthlist) goto _eofout;
413
414 /* allocated but unused entries? */
415 if(oggpack_read(opb,1)){
416 /* yes, unused entries */
417
418 for(i=0;i<s->entries;i++){
419 if(oggpack_read(opb,1)){
420 long num=oggpack_read(opb,5);
421 if(num==-1)goto _eofout;
422 lengthlist[i]=(char)(num+1);
423 s->used_entries++;
424 if(num+1>s->dec_maxlength)s->dec_maxlength=num+1;
425 }else
426 lengthlist[i]=0;
427 }
428 }else{
429 /* all entries used; no tagging */
430 s->used_entries=s->entries;
431 for(i=0;i<s->entries;i++){
432 long num=oggpack_read(opb,5);
433 if(num==-1)goto _eofout;
434 lengthlist[i]=(char)(num+1);
435 if(num+1>s->dec_maxlength)s->dec_maxlength=num+1;
436 }
437 }
438
439 break;
440 case 1:
441 /* ordered */
442 {
443 long length=oggpack_read(opb,5)+1;
444
445 s->used_entries=s->entries;
Marco Nelissenafa1f6b2013-10-11 15:46:30 -0700446 lengthlist=(char *)calloc(s->entries, sizeof(*lengthlist));
Gloria Wang79130732010-02-08 14:41:04 -0800447 if (!lengthlist) goto _eofout;
448
449 for(i=0;i<s->entries;){
450 long num=oggpack_read(opb,_ilog(s->entries-i));
451 if(num<0)goto _eofout;
452 for(j=0;j<num && i<s->entries;j++,i++)
453 lengthlist[i]=(char)length;
454 s->dec_maxlength=length;
455 length++;
456 }
457 }
458 break;
459 default:
460 /* EOF */
461 goto _eofout;
462 }
463
464
465 /* Do we have a mapping to unpack? */
466
467 if((maptype=oggpack_read(opb,4))>0){
468 s->q_min=_float32_unpack(oggpack_read(opb,32),&s->q_minp);
469 s->q_del=_float32_unpack(oggpack_read(opb,32),&s->q_delp);
470 s->q_bits=oggpack_read(opb,4)+1;
471 s->q_seq=oggpack_read(opb,1);
472
473 s->q_del>>=s->q_bits;
474 s->q_delp+=s->q_bits;
475 }
476
477 switch(maptype){
478 case 0:
479
480 /* no mapping; decode type 0 */
481
482 /* how many bytes for the indexing? */
483 /* this is the correct boundary here; we lose one bit to
484 node/leaf mark */
485 s->dec_nodeb=_determine_node_bytes(s->used_entries,_ilog(s->entries)/8+1);
486 s->dec_leafw=_determine_leaf_words(s->dec_nodeb,_ilog(s->entries)/8+1);
487 s->dec_type=0;
488
489 if(_make_decode_table(s,lengthlist,quantvals,opb,maptype)) goto _errout;
490 break;
491
492 case 1:
493
494 /* mapping type 1; implicit values by lattice position */
495 quantvals=_book_maptype1_quantvals(s);
496
497 /* dec_type choices here are 1,2; 3 doesn't make sense */
498 {
499 /* packed values */
500 long total1=(s->q_bits*s->dim+8)/8; /* remember flag bit */
501 if (s->dim > (INT_MAX-8)/s->q_bits) goto _eofout;
502 /* vector of column offsets; remember flag bit */
503 long total2=(_ilog(quantvals-1)*s->dim+8)/8+(s->q_bits+7)/8;
504
505
506 if(total1<=4 && total1<=total2){
507 /* use dec_type 1: vector of packed values */
508
509 /* need quantized values before */
Marco Nelissen2e941e42015-04-30 13:45:34 -0700510 s->q_val=calloc(sizeof(ogg_uint16_t), quantvals);
Gloria Wang79130732010-02-08 14:41:04 -0800511 if (!s->q_val) goto _eofout;
512 for(i=0;i<quantvals;i++)
513 ((ogg_uint16_t *)s->q_val)[i]=(ogg_uint16_t)oggpack_read(opb,s->q_bits);
514
515 if(oggpack_eop(opb)){
Gloria Wang79130732010-02-08 14:41:04 -0800516 goto _eofout;
517 }
518
519 s->dec_type=1;
520 s->dec_nodeb=_determine_node_bytes(s->used_entries,
521 (s->q_bits*s->dim+8)/8);
522 s->dec_leafw=_determine_leaf_words(s->dec_nodeb,
523 (s->q_bits*s->dim+8)/8);
524 if(_make_decode_table(s,lengthlist,quantvals,opb,maptype)){
Gloria Wang79130732010-02-08 14:41:04 -0800525 goto _errout;
526 }
527
Marco Nelissen2e941e42015-04-30 13:45:34 -0700528 free(s->q_val);
529 s->q_val=0;
Gloria Wang79130732010-02-08 14:41:04 -0800530
531 }else{
532 /* use dec_type 2: packed vector of column offsets */
533
534 /* need quantized values before */
535 if(s->q_bits<=8){
536 s->q_val=_ogg_malloc(quantvals);
537 if (!s->q_val) goto _eofout;
538 for(i=0;i<quantvals;i++)
539 ((unsigned char *)s->q_val)[i]=(unsigned char)oggpack_read(opb,s->q_bits);
540 }else{
541 s->q_val=_ogg_malloc(quantvals*2);
542 if (!s->q_val) goto _eofout;
543 for(i=0;i<quantvals;i++)
544 ((ogg_uint16_t *)s->q_val)[i]=(ogg_uint16_t)oggpack_read(opb,s->q_bits);
545 }
546
547 if(oggpack_eop(opb))goto _eofout;
548
549 s->q_pack=_ilog(quantvals-1);
550 s->dec_type=2;
551 s->dec_nodeb=_determine_node_bytes(s->used_entries,
552 (_ilog(quantvals-1)*s->dim+8)/8);
553 s->dec_leafw=_determine_leaf_words(s->dec_nodeb,
554 (_ilog(quantvals-1)*s->dim+8)/8);
555 if(_make_decode_table(s,lengthlist,quantvals,opb,maptype))goto _errout;
556
557 }
558 }
559 break;
560 case 2:
561
562 /* mapping type 2; explicit array of values */
563 quantvals=s->entries*s->dim;
564 /* dec_type choices here are 1,3; 2 is not possible */
565
566 if( (s->q_bits*s->dim+8)/8 <=4){ /* remember flag bit */
567 /* use dec_type 1: vector of packed values */
568
569 s->dec_type=1;
570 s->dec_nodeb=_determine_node_bytes(s->used_entries,(s->q_bits*s->dim+8)/8);
571 s->dec_leafw=_determine_leaf_words(s->dec_nodeb,(s->q_bits*s->dim+8)/8);
572 if(_make_decode_table(s,lengthlist,quantvals,opb,maptype))goto _errout;
573
574 }else{
575 /* use dec_type 3: scalar offset into packed value array */
576
577 s->dec_type=3;
578 s->dec_nodeb=_determine_node_bytes(s->used_entries,_ilog(s->used_entries-1)/8+1);
579 s->dec_leafw=_determine_leaf_words(s->dec_nodeb,_ilog(s->used_entries-1)/8+1);
580 if(_make_decode_table(s,lengthlist,quantvals,opb,maptype))goto _errout;
581
582 /* get the vals & pack them */
583 s->q_pack=(s->q_bits+7)/8*s->dim;
584 s->q_val=_ogg_malloc(s->q_pack*s->used_entries);
585
586 if(s->q_bits<=8){
587 for(i=0;i<s->used_entries*s->dim;i++)
588 ((unsigned char *)(s->q_val))[i]=(unsigned char)oggpack_read(opb,s->q_bits);
589 }else{
590 for(i=0;i<s->used_entries*s->dim;i++)
591 ((ogg_uint16_t *)(s->q_val))[i]=(ogg_uint16_t)oggpack_read(opb,s->q_bits);
592 }
593 }
594 break;
595 default:
596 goto _errout;
597 }
598
599 if (s->dec_nodeb==1)
600 if (s->dec_leafw == 1)
601 s->dec_method = 0;
602 else
603 s->dec_method = 1;
604 else if (s->dec_nodeb==2)
605 if (s->dec_leafw == 1)
606 s->dec_method = 2;
607 else
608 s->dec_method = 3;
609 else
610 s->dec_method = 4;
611
612 if(oggpack_eop(opb))goto _eofout;
613
Marco Nelissen97fe1972013-10-25 15:31:23 -0700614 free(lengthlist);
Gloria Wang79130732010-02-08 14:41:04 -0800615 return 0;
616 _errout:
617 _eofout:
618 vorbis_book_clear(s);
Marco Nelissenafa1f6b2013-10-11 15:46:30 -0700619 free(lengthlist);
Marco Nelissen2e941e42015-04-30 13:45:34 -0700620 free(s->q_val);
Gloria Wang79130732010-02-08 14:41:04 -0800621 return -1;
622}
623
624#ifndef ONLY_C
625ogg_uint32_t decode_packed_entry_number(codebook *book,
626 oggpack_buffer *b);
627#else
628static inline ogg_uint32_t decode_packed_entry_number(codebook *book,
629 oggpack_buffer *b){
630 ogg_uint32_t chase=0;
631 int read=book->dec_maxlength;
632 long lok = oggpack_look(b,read),i;
633
634 while(lok<0 && read>1)
635 lok = oggpack_look(b, --read);
636
637 if(lok<0){
638 oggpack_adv(b,1); /* force eop */
639 return -1;
640 }
641
642 /* chase the tree with the bits we got */
643 switch (book->dec_method)
644 {
645 case 0:
646 {
647 /* book->dec_nodeb==1, book->dec_leafw==1 */
648 /* 8/8 - Used */
649 unsigned char *t=(unsigned char *)book->dec_table;
650
651 for(i=0;i<read;i++){
652 chase=t[chase*2+((lok>>i)&1)];
653 if(chase&0x80UL)break;
654 }
655 chase&=0x7fUL;
656 break;
657 }
658 case 1:
659 {
660 /* book->dec_nodeb==1, book->dec_leafw!=1 */
661 /* 8/16 - Used by infile2 */
662 unsigned char *t=(unsigned char *)book->dec_table;
663 for(i=0;i<read;i++){
664 int bit=(lok>>i)&1;
665 int next=t[chase+bit];
666 if(next&0x80){
667 chase= (next<<8) | t[chase+bit+1+(!bit || t[chase]&0x80)];
668 break;
669 }
670 chase=next;
671 }
672 //chase&=0x7fffUL;
673 chase&=~0x8000UL;
674 break;
675 }
676 case 2:
677 {
678 /* book->dec_nodeb==2, book->dec_leafw==1 */
679 /* 16/16 - Used */
680 for(i=0;i<read;i++){
681 chase=((ogg_uint16_t *)(book->dec_table))[chase*2+((lok>>i)&1)];
682 if(chase&0x8000UL)break;
683 }
684 //chase&=0x7fffUL;
685 chase&=~0x8000UL;
686 break;
687 }
688 case 3:
689 {
690 /* book->dec_nodeb==2, book->dec_leafw!=1 */
691 /* 16/32 - Used by infile2 */
692 ogg_uint16_t *t=(ogg_uint16_t *)book->dec_table;
693 for(i=0;i<read;i++){
694 int bit=(lok>>i)&1;
695 int next=t[chase+bit];
696 if(next&0x8000){
697 chase= (next<<16) | t[chase+bit+1+(!bit || t[chase]&0x8000)];
698 break;
699 }
700 chase=next;
701 }
702 //chase&=0x7fffffffUL;
703 chase&=~0x80000000UL;
704 break;
705 }
706 case 4:
707 {
Gloria Wangd75baf22010-02-12 16:28:30 -0800708 //Output("32/32");
Gloria Wang79130732010-02-08 14:41:04 -0800709 for(i=0;i<read;i++){
710 chase=((ogg_uint32_t *)(book->dec_table))[chase*2+((lok>>i)&1)];
711 if(chase&0x80000000UL)break;
712 }
713 //chase&=0x7fffffffUL;
714 chase&=~0x80000000UL;
715 break;
716 }
717 }
718
719 if(i<read){
720 oggpack_adv(b,i+1);
721 return chase;
722 }
723 oggpack_adv(b,read+1);
724 return(-1);
725}
726#endif
727
728/* returns the [original, not compacted] entry number or -1 on eof *********/
729long vorbis_book_decode(codebook *book, oggpack_buffer *b){
730 if(book->dec_type)return -1;
731 return decode_packed_entry_number(book,b);
732}
733
734#ifndef ONLY_C
735int decode_map(codebook *s, oggpack_buffer *b, ogg_int32_t *v, int point);
736#else
737static int decode_map(codebook *s, oggpack_buffer *b, ogg_int32_t *v, int point){
738 ogg_uint32_t entry = decode_packed_entry_number(s,b);
739 int i;
740 if(oggpack_eop(b))return(-1);
741
742 /* 1 used by test file 0 */
743
744 /* according to decode type */
745 switch(s->dec_type){
746 case 1:{
747 /* packed vector of values */
748 int mask=(1<<s->q_bits)-1;
749 for(i=0;i<s->dim;i++){
750 v[i]=entry&mask;
751 entry>>=s->q_bits;
752 }
753 break;
754 }
755 case 2:{
756 /* packed vector of column offsets */
757 int mask=(1<<s->q_pack)-1;
758 for(i=0;i<s->dim;i++){
759 if(s->q_bits<=8)
760 v[i]=((unsigned char *)(s->q_val))[entry&mask];
761 else
762 v[i]=((ogg_uint16_t *)(s->q_val))[entry&mask];
763 entry>>=s->q_pack;
764 }
765 break;
766 }
767 case 3:{
768 /* offset into array */
769 void *ptr=s->q_val+entry*s->q_pack;
770
771 if(s->q_bits<=8){
772 for(i=0;i<s->dim;i++)
773 v[i]=((unsigned char *)ptr)[i];
774 }else{
775 for(i=0;i<s->dim;i++)
776 v[i]=((ogg_uint16_t *)ptr)[i];
777 }
778 break;
779 }
780 default:
781 return -1;
782 }
783
784 /* we have the unpacked multiplicands; compute final vals */
785 {
786 int shiftM = point-s->q_delp;
787 ogg_int32_t add = point-s->q_minp;
788 int mul = s->q_del;
789
790 if(add>0)
791 add= s->q_min >> add;
792 else
793 add= s->q_min << -add;
794 if (shiftM<0)
795 {
796 mul <<= -shiftM;
797 shiftM = 0;
798 }
799 add <<= shiftM;
800
801 for(i=0;i<s->dim;i++)
802 v[i]= ((add + v[i] * mul) >> shiftM);
803
804 if(s->q_seq)
805 for(i=1;i<s->dim;i++)
806 v[i]+=v[i-1];
807 }
808
809 return 0;
810}
811#endif
812
813/* returns 0 on OK or -1 on eof *************************************/
814long vorbis_book_decodevs_add(codebook *book,ogg_int32_t *a,
815 oggpack_buffer *b,int n,int point){
816 if(book->used_entries>0){
817 int step=n/book->dim;
818 ogg_int32_t *v = book->dec_buf;//(ogg_int32_t *)alloca(sizeof(*v)*book->dim);
819 int i,j,o;
820 if (!v) return -1;
821
822 for (j=0;j<step;j++){
823 if(decode_map(book,b,v,point))return -1;
824 for(i=0,o=j;i<book->dim;i++,o+=step)
825 a[o]+=v[i];
826 }
827 }
828 return 0;
829}
830
831long vorbis_book_decodev_add(codebook *book,ogg_int32_t *a,
832 oggpack_buffer *b,int n,int point){
833 if(book->used_entries>0){
834 ogg_int32_t *v = book->dec_buf;//(ogg_int32_t *)alloca(sizeof(*v)*book->dim);
835 int i,j;
836
837 if (!v) return -1;
838 for(i=0;i<n;){
839 if(decode_map(book,b,v,point))return -1;
840 for (j=0;j<book->dim;j++)
841 a[i++]+=v[j];
842 }
843 }
844 return 0;
845}
846
847long vorbis_book_decodev_set(codebook *book,ogg_int32_t *a,
848 oggpack_buffer *b,int n,int point){
849 if(book->used_entries>0){
850 ogg_int32_t *v = book->dec_buf;//(ogg_int32_t *)alloca(sizeof(*v)*book->dim);
851 int i,j;
852
853 if (!v) return -1;
854 for(i=0;i<n;){
855 if(decode_map(book,b,v,point))return -1;
856 for (j=0;j<book->dim;j++)
857 a[i++]=v[j];
858 }
859 }else{
860 int i,j;
861
862 for(i=0;i<n;){
863 for (j=0;j<book->dim;j++)
864 a[i++]=0;
865 }
866 }
867
868 return 0;
869}
870
871#ifndef ONLY_C
872long vorbis_book_decodevv_add(codebook *book,ogg_int32_t **a,
873 long offset,int ch,
874 oggpack_buffer *b,int n,int point);
875#else
876long vorbis_book_decodevv_add(codebook *book,ogg_int32_t **a,
877 long offset,int ch,
878 oggpack_buffer *b,int n,int point){
879 if(book->used_entries>0){
880
881 ogg_int32_t *v = book->dec_buf;//(ogg_int32_t *)alloca(sizeof(*v)*book->dim);
882 long i,j;
883 int chptr=0;
884
885 if (!v) return -1;
886 for(i=offset;i<offset+n;){
887 if(decode_map(book,b,v,point))return -1;
888 for (j=0;j<book->dim;j++){
889 a[chptr++][i]+=v[j];
890 if(chptr==ch){
891 chptr=0;
892 i++;
893 }
894 }
895 }
896 }
897
898 return 0;
899}
900#endif