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