Coverage Report

Created: 2026-11-12 07:18

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/vorbis/lib/sharedbook.c
Line
Count
Source
1
/********************************************************************
2
 *                                                                  *
3
 * THIS FILE IS PART OF THE OggVorbis SOFTWARE CODEC SOURCE CODE.   *
4
 * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS     *
5
 * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE *
6
 * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING.       *
7
 *                                                                  *
8
 * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2015             *
9
 * by the Xiph.Org Foundation https://xiph.org/                     *
10
 *                                                                  *
11
 ********************************************************************
12
13
 function: basic shared codebook operations
14
15
 ********************************************************************/
16
17
#include <stdlib.h>
18
#include <limits.h>
19
#include <math.h>
20
#include <string.h>
21
#include <ogg/ogg.h>
22
#include "os.h"
23
#include "misc.h"
24
#include "vorbis/codec.h"
25
#include "codebook.h"
26
#include "scales.h"
27
28
/**** pack/unpack helpers ******************************************/
29
30
396k
int ov_ilog(ogg_uint32_t v){
31
396k
  int ret;
32
2.69M
  for(ret=0;v;ret++)v>>=1;
33
396k
  return ret;
34
396k
}
35
36
/* 32 bit float (not IEEE; nonnormalized mantissa +
37
   biased exponent) : neeeeeee eeemmmmm mmmmmmmm mmmmmmmm
38
   Why not IEEE?  It's just not that important here. */
39
40
#define VQ_FEXP 10
41
323k
#define VQ_FMAN 21
42
161k
#define VQ_FEXP_BIAS 768 /* bias toward values smaller than 1. */
43
44
/* doesn't currently guard under/overflow */
45
0
long _float32_pack(float val){
46
0
  int sign=0;
47
0
  long exp;
48
0
  long mant;
49
0
  if(val<0){
50
0
    sign=0x80000000;
51
0
    val= -val;
52
0
  }
53
0
  exp= floor(log(val)/log(2.f)+.001); /* +epsilon */
54
0
  mant=rint(ldexp(val,(VQ_FMAN-1)-exp));
55
0
  exp=(exp+VQ_FEXP_BIAS)<<VQ_FMAN;
56
57
0
  return(sign|exp|mant);
58
0
}
59
60
161k
float _float32_unpack(long val){
61
161k
  double mant=val&0x1fffff;
62
161k
  int    sign=val&0x80000000;
63
161k
  long   exp =(val&0x7fe00000L)>>VQ_FMAN;
64
161k
  if(sign)mant= -mant;
65
161k
  exp=exp-(VQ_FMAN-1)-VQ_FEXP_BIAS;
66
  /* clamp excessive exponent values */
67
161k
  if (exp>63){
68
0
    exp=63;
69
0
  }
70
161k
  if (exp<-63){
71
104k
    exp=-63;
72
104k
  }
73
161k
  return(ldexp(mant,exp));
74
161k
}
75
76
/* given a list of word lengths, generate a list of codewords.  Works
77
   for length ordered or unordered, always assigns the lowest valued
78
   codewords first.  Extended to handle unused entries (length 0) */
79
80.9k
ogg_uint32_t *_make_words(char *l,long n,long sparsecount){
80
80.9k
  long i,j,count=0;
81
80.9k
  ogg_uint32_t marker[33];
82
80.9k
  ogg_uint32_t *r=_ogg_malloc((sparsecount?sparsecount:n)*sizeof(*r));
83
80.9k
  memset(marker,0,sizeof(marker));
84
85
11.8M
  for(i=0;i<n;i++){
86
11.8M
    long length=l[i];
87
11.8M
    if(length>0){
88
9.01M
      ogg_uint32_t entry=marker[length];
89
90
      /* when we claim a node for an entry, we also claim the nodes
91
         below it (pruning off the imagined tree that may have dangled
92
         from it) as well as blocking the use of any nodes directly
93
         above for leaves */
94
95
      /* update ourself */
96
9.01M
      if(length<32 && (entry>>length)){
97
        /* error condition; the lengths must specify an overpopulated tree */
98
0
        _ogg_free(r);
99
0
        return(NULL);
100
0
      }
101
9.01M
      r[count++]=entry;
102
103
      /* Look to see if the next shorter marker points to the node
104
         above. if so, update it and repeat.  */
105
9.01M
      {
106
17.9M
        for(j=length;j>0;j--){
107
108
17.8M
          if(marker[j]&1){
109
            /* have to jump branches */
110
8.93M
            if(j==1)
111
80.9k
              marker[1]++;
112
8.85M
            else
113
8.85M
              marker[j]=marker[j-1]<<1;
114
8.93M
            break; /* invariant says next upper marker would already
115
                      have been moved if it was on the same path */
116
8.93M
          }
117
8.93M
          marker[j]++;
118
8.93M
        }
119
9.01M
      }
120
121
      /* prune the tree; the implicit invariant says all the longer
122
         markers were dangling from our just-taken node.  Dangle them
123
         from our *new* node. */
124
110M
      for(j=length+1;j<33;j++)
125
105M
        if((marker[j]>>1) == entry){
126
101M
          entry=marker[j];
127
101M
          marker[j]=marker[j-1]<<1;
128
101M
        }else
129
4.33M
          break;
130
9.01M
    }else
131
2.79M
      if(sparsecount==0)count++;
132
11.8M
  }
133
134
  /* any underpopulated tree must be rejected. */
135
  /* Single-entry codebooks are a retconned extension to the spec.
136
     They have a single codeword '0' of length 1 that results in an
137
     underpopulated tree.  Shield that case from the underformed tree check. */
138
80.9k
  if(!(count==1 && marker[2]==2)){
139
2.67M
    for(i=1;i<33;i++)
140
2.59M
      if(marker[i] & (0xffffffffUL>>(32-i))){
141
0
        _ogg_free(r);
142
0
        return(NULL);
143
0
      }
144
80.9k
  }
145
146
  /* bitreverse the words because our bitwise packer/unpacker is LSb
147
     endian */
148
11.8M
  for(i=0,count=0;i<n;i++){
149
11.8M
    ogg_uint32_t temp=0;
150
106M
    for(j=0;j<l[i];j++){
151
95.1M
      temp<<=1;
152
95.1M
      temp|=(r[count]>>j)&1;
153
95.1M
    }
154
155
11.8M
    if(sparsecount){
156
0
      if(l[i])
157
0
        r[count++]=temp;
158
0
    }else
159
11.8M
      r[count++]=temp;
160
11.8M
  }
161
162
80.9k
  return(r);
163
80.9k
}
164
165
/* given a list of word lengths, generate a list of packed MSb codewords and
166
   entry numbers.  This is like the above, except that the codewords are MSb
167
   first and aligned to be 32-bits in size, and the lower 24 bits include the
168
   entry number the codeword corresponds to. */
169
0
static ogg_int64_t *dec_make_words(signed char *l,long n,long sparsecount){
170
0
  long i,j,count=0;
171
0
  ogg_uint32_t marker[33];
172
0
  ogg_int64_t *r=_ogg_malloc((sparsecount?sparsecount:n)*sizeof(*r));
173
0
  if(r==NULL)return(NULL);
174
0
  memset(marker,0,sizeof(marker));
175
176
0
  for(i=0;i<n;i++){
177
0
    long length=l[i];
178
0
    if(length>0){
179
0
      ogg_uint32_t entry=marker[length];
180
181
      /* when we claim a node for an entry, we also claim the nodes
182
         below it (pruning off the imagined tree that may have dangled
183
         from it) as well as blocking the use of any nodes directly
184
         above for leaves */
185
186
      /* update ourself */
187
0
      if(length<32 && (entry>>length)){
188
        /* error condition; the lengths must specify an overpopulated tree */
189
0
        _ogg_free(r);
190
0
        return(NULL);
191
0
      }
192
0
      r[count++]=(ogg_int64_t)entry<<(32-l[i]+24)|i;
193
194
      /* Look to see if the next shorter marker points to the node
195
         above. if so, update it and repeat.  */
196
0
      {
197
0
        for(j=length;j>0;j--){
198
199
0
          if(marker[j]&1){
200
            /* have to jump branches */
201
0
            if(j==1)
202
0
              marker[1]++;
203
0
            else
204
0
              marker[j]=marker[j-1]<<1;
205
0
            break; /* invariant says next upper marker would already
206
                      have been moved if it was on the same path */
207
0
          }
208
0
          marker[j]++;
209
0
        }
210
0
      }
211
212
      /* prune the tree; the implicit invariant says all the longer
213
         markers were dangling from our just-taken node.  Dangle them
214
         from our *new* node. */
215
0
      for(j=length+1;j<33;j++)
216
0
        if((marker[j]>>1) == entry){
217
0
          entry=marker[j];
218
0
          marker[j]=marker[j-1]<<1;
219
0
        }else
220
0
          break;
221
0
    }else
222
0
      if(sparsecount==0)count++;
223
0
  }
224
225
  /* any underpopulated tree must be rejected. */
226
  /* Single-entry codebooks are a retconned extension to the spec.
227
     They have a single codeword '0' of length 1 that results in an
228
     underpopulated tree.  Shield that case from the underformed tree check. */
229
0
  if(!(count==1 && marker[2]==2)){
230
0
    for(i=1;i<33;i++)
231
0
      if(marker[i] & (0xffffffffUL>>(32-i))){
232
0
        _ogg_free(r);
233
0
        return(NULL);
234
0
      }
235
0
  }
236
237
0
  return(r);
238
0
}
239
240
/* there might be a straightforward one-line way to do the below
241
   that's portable and totally safe against roundoff, but I haven't
242
   thought of it.  Therefore, we opt on the side of caution */
243
109k
long _book_maptype1_quantvals(long dim, long entries){
244
109k
  long vals;
245
109k
  if(entries<1){
246
0
    return(0);
247
0
  }
248
109k
  vals=floor(pow((float)entries,1.f/dim));
249
250
  /* the above *should* be reliable, but we'll not assume that FP is
251
     ever reliable when bitstream sync is at stake; verify via integer
252
     means that vals really is the greatest value of dim for which
253
     vals^dim <= entries */
254
  /* treat the above as an initial guess */
255
109k
  if(vals<1){
256
0
    vals=1;
257
0
  }
258
109k
  while(1){
259
109k
    long acc=1;
260
109k
    long acc1=1;
261
109k
    int i;
262
317k
    for(i=0;i<dim;i++){
263
208k
      if(entries/vals<acc)break;
264
208k
      acc*=vals;
265
208k
      if(LONG_MAX/(vals+1)<acc1)acc1=LONG_MAX;
266
208k
      else acc1*=vals+1;
267
208k
    }
268
109k
    if(i>=dim && acc<=entries && acc1>entries){
269
109k
      return(vals);
270
109k
    }else{
271
0
      if(i<dim || acc>entries){
272
0
        vals--;
273
0
      }else{
274
0
        vals++;
275
0
      }
276
0
    }
277
109k
  }
278
109k
}
279
280
/* unpack the quantized list of values for decode ***********/
281
/* we need to deal with two map types: in map type 1, the values are
282
   generated algorithmically (each column of the vector counts through
283
   the values in the quant vector). in map type 2, all the values came
284
   in in an explicit list.  Both value lists must be unpacked */
285
void _book_unquantize(float *r,const dec_codebook *b,ogg_int32_t n,
286
0
                      ogg_int64_t *sparsemap){
287
0
  ogg_int32_t i,j,k;
288
0
  if(b->maptype==1 || b->maptype==2){
289
0
    ogg_int32_t quantvals;
290
0
    float mindel=_float32_unpack(b->q_min);
291
0
    float delta=_float32_unpack(b->q_delta);
292
293
    /* maptype 1 and 2 both use a quantized value vector, but
294
       different sizes */
295
0
    switch(b->maptype){
296
0
    case 1:
297
      /* most of the time, entries%dimensions == 0, but we need to be
298
         well defined.  We define that the possible vales at each
299
         scalar is values == entries/dim.  If entries%dim != 0, we'll
300
         have 'too few' values (values*dim<entries), which means that
301
         we'll have 'left over' entries; left over entries use zeroed
302
         values (and are wasted).  So don't generate codebooks like
303
         that */
304
0
      quantvals=(ogg_int32_t)_book_maptype1_quantvals(b->dim, b->entries);
305
0
      for(i=0;i<n;i++){
306
0
        float last=0.f;
307
0
        int indexdiv=1;
308
0
        j=(long)(sparsemap?sparsemap[i]&0xffffff:i);
309
0
        for(k=0;k<b->dim;k++){
310
0
          ogg_int32_t index=(j/indexdiv)%quantvals;
311
0
          float val=b->quantlist[index];
312
0
          val=fabs(val)*delta+mindel+last;
313
0
          if(b->q_sequencep)last=val;
314
0
          r[i*b->dim+k]=val;
315
0
          indexdiv*=quantvals;
316
0
        }
317
0
      }
318
0
      break;
319
0
    case 2:
320
0
      for(i=0;i<n;i++){
321
0
        float last=0.f;
322
0
        j=(ogg_int32_t)(sparsemap?sparsemap[i]&0xffffff:i);
323
0
        for(k=0;k<b->dim;k++){
324
0
          float val=b->quantlist[j*b->dim+k];
325
0
          val=fabs(val)*delta+mindel+last;
326
0
          if(b->q_sequencep)last=val;
327
0
          r[i*b->dim+k]=val;
328
0
        }
329
0
      }
330
0
      break;
331
0
    }
332
333
0
  }
334
0
}
335
336
80.9k
void vorbis_staticbook_destroy(static_codebook *b){
337
80.9k
  if(b->allocedp){
338
0
    if(b->quantlist)_ogg_free(b->quantlist);
339
0
    if(b->lengthlist)_ogg_free(b->lengthlist);
340
0
    memset(b,0,sizeof(*b));
341
0
    _ogg_free(b);
342
0
  } /* otherwise, it is in static memory */
343
80.9k
}
344
345
80.9k
void vorbis_book_clear(codebook *b){
346
  /* static book is not cleared; we're likely called on the lookup and
347
     the static codebook belongs to the info struct */
348
80.9k
  if(b->valuelist)_ogg_free(b->valuelist);
349
80.9k
  if(b->codelist)_ogg_free(b->codelist);
350
351
80.9k
  memset(b,0,sizeof(*b));
352
80.9k
}
353
354
0
void vorbis_decbook_clear(dec_codebook *b){
355
0
  if(b->quantlist)_ogg_free(b->quantlist);
356
0
  if(b->firsttable)_ogg_free(b->firsttable);
357
0
  if(b->codelist)_ogg_free(b->codelist);
358
0
  if(b->codelengths)_ogg_free(b->codelengths);
359
0
  if(b->index)_ogg_free(b->index);
360
0
  if(b->valuelist)_ogg_free(b->valuelist);
361
0
  memset(b,0,sizeof(*b));
362
0
}
363
364
80.9k
int vorbis_book_init_encode(codebook *c,const static_codebook *s){
365
366
80.9k
  memset(c,0,sizeof(*c));
367
80.9k
  c->c=s;
368
80.9k
  c->entries=s->entries;
369
80.9k
  c->used_entries=s->entries;
370
80.9k
  c->dim=s->dim;
371
80.9k
  c->codelist=_make_words(s->lengthlist,s->entries,0);
372
80.9k
  c->quantvals=_book_maptype1_quantvals(s->dim, s->entries);
373
80.9k
  c->minval=(int)rint(_float32_unpack(s->q_min));
374
80.9k
  c->delta=(int)rint(_float32_unpack(s->q_delta));
375
376
80.9k
  return(0);
377
80.9k
}
378
379
0
static ogg_uint32_t bitreverse(ogg_uint32_t x){
380
0
  x=    ((x>>16)&0x0000ffffUL) | ((x<<16)&0xffff0000UL);
381
0
  x=    ((x>> 8)&0x00ff00ffUL) | ((x<< 8)&0xff00ff00UL);
382
0
  x=    ((x>> 4)&0x0f0f0f0fUL) | ((x<< 4)&0xf0f0f0f0UL);
383
0
  x=    ((x>> 2)&0x33333333UL) | ((x<< 2)&0xccccccccUL);
384
0
  return((x>> 1)&0x55555555UL) | ((x<< 1)&0xaaaaaaaaUL);
385
0
}
386
387
0
static int sort64a(const void *a,const void *b){
388
0
  return ( *(ogg_int64_t *)a>*(ogg_int64_t *)b)-
389
0
    ( *(ogg_int64_t *)a<*(ogg_int64_t *)b);
390
0
}
391
392
/* decode codebook arrangement is more heavily optimized than encode */
393
0
int vorbis_book_init_decode(dec_codebook *c){
394
0
  ogg_int32_t i,j,n;
395
0
  ogg_int64_t *codes=NULL;
396
397
  /* only initialize once */
398
0
  if(c->codelist)return(0);
399
400
0
  if(c->codelengths){
401
    /* unordered codebook */
402
    /* count actually used entries */
403
0
    n=0;
404
0
    for(i=0;i<c->entries;i++)
405
0
      if(c->codelengths[i]>0)
406
0
        n++;
407
408
0
    if(n>0){
409
0
      signed char *codelengths;
410
      /* two different remappings go on here.
411
412
      First, we collapse the likely sparse codebook down only to
413
      actually represented values/words.  This collapsing needs to be
414
      indexed as map-valueless books are used to encode original entry
415
      positions as integers.
416
417
      Second, we reorder all vectors, including the entry index above,
418
      by sorted bitreversed codeword to allow treeless decode. */
419
420
      /* perform sort */
421
0
      codes=dec_make_words(c->codelengths,c->entries,n);
422
0
      if(codes==NULL)goto err_out;
423
424
0
      qsort(codes,n,sizeof(*codes),sort64a);
425
426
0
      c->codelist=_ogg_malloc(n*sizeof(*c->codelist));
427
0
      if(c->codelist==NULL)goto err_out;
428
429
0
      for(i=0;i<n;i++){
430
0
        c->codelist[i]=(ogg_uint32_t)(codes[i]>>24);
431
0
      }
432
433
0
      if(c->maptype==1 || c->maptype==2){
434
0
        c->valuelist=_ogg_malloc(c->dim*n*sizeof(*c->valuelist));
435
0
        if(c->valuelist==NULL)goto err_out;
436
0
        _book_unquantize(c->valuelist,c,n,codes);
437
0
        _ogg_free(c->quantlist);
438
0
        c->quantlist=NULL;
439
0
      }
440
0
      c->index=_ogg_malloc(n*sizeof(*c->index));
441
0
      if(c->index==NULL)goto err_out;
442
0
      codelengths=_ogg_malloc(n*sizeof(*c->codelengths));
443
0
      if(codelengths==NULL)goto err_out;
444
445
      /* save the index of used entries, compact and reorder the codelengths,
446
         and find min/max length */
447
0
      c->minlength=32;
448
0
      c->maxlength=0;
449
0
      for(i=0;i<n;i++){
450
0
        j=(ogg_int32_t)(codes[i]&0xffffff);
451
0
        c->index[i]=j;
452
0
        codelengths[i]=c->codelengths[j];
453
0
        if(codelengths[i]<c->minlength)
454
0
          c->minlength=codelengths[i];
455
0
        if(codelengths[i]>c->maxlength)
456
0
          c->maxlength=codelengths[i];
457
0
      }
458
0
      _ogg_free(codes);
459
0
      codes=NULL;
460
0
      _ogg_free(c->codelengths);
461
0
      c->codelengths=codelengths;
462
0
    }
463
0
  }else{
464
    /* ordered codebook:
465
       we can avoid building an explicit list of codepoints, which can be a big
466
       advantage as we approach the limits of the spec with upwards of 16
467
       million codebook entries.  We follow the approach of Alistair Moffat and
468
       Andrew Turpin: On the Implementation of Minimum Redundancy Prefix Codes,
469
       IEEE Transactions on Communications, 45(10):1200--1207, October, 1997,
470
       except modified to have codewords in order of increasing length, instead
471
       of decreasing. */
472
0
    n=c->entries;
473
0
    if(n>0){
474
0
      ogg_uint32_t prev_entry;
475
0
      ogg_uint32_t code;
476
0
      int nlengths;
477
0
      int length;
478
0
      int l;
479
0
      nlengths=c->maxlength-c->minlength+1;
480
0
      c->codelist=_ogg_malloc(nlengths*sizeof(*c->codelist));
481
0
      if(c->codelist==NULL)goto err_out;
482
      /* for this case, codelist[l] contains the last codeword of the l'th
483
         length, left-aligned in a 32-bit word, with all remaining bits filled
484
         with trailing 1's.  This ensures that 32 bits taken from the stream
485
         will be <= codelist[l] if the codeword length is <= length l. */
486
0
      prev_entry=0;
487
0
      code=0;
488
0
      length=c->minlength;
489
0
      for(l=0;l<nlengths;l++,length++){
490
0
        ogg_uint32_t nentries;
491
0
        nentries=c->index[l]-prev_entry;
492
        /* guard against an over-populated tree.  We do not check the last
493
           length, because it should exactly use the remaining space, which
494
           causes code to wrap to 0 (something this check prevents for any
495
           earlier length).  We'll check it after the loop. */
496
0
        if(l+1<nlengths && nentries>((0xffffffffUL-code)>>(32-length)))
497
0
          goto err_out;
498
0
        code+=nentries<<(32-length);
499
        /* this requires that the first length have a non-zero number of
500
           entries, or c->codelist[0] will wrap to 0xffffffff, making the list
501
           non-monotonic. */
502
0
        c->codelist[l]=code-1;
503
0
        prev_entry=c->index[l];
504
0
      }
505
      /* for a complete tree, the last value will have all bits set, which
506
         guarantees we will not walk past the end of the array during decode */
507
0
      if(c->codelist[nlengths-1]!=0xffffffffUL){
508
        /* reject over/underpopulated trees, with the exception of a
509
           single-entry codebook. */
510
0
        if(n!=1 || c->maxlength!=1)goto err_out;
511
0
      }
512
0
      if(c->maptype==1 || c->maptype==2){
513
0
        c->valuelist=_ogg_malloc(c->dim*n*sizeof(*c->valuelist));
514
0
        if(c->valuelist==NULL)goto err_out;
515
0
        _book_unquantize(c->valuelist,c,n,NULL);
516
0
        _ogg_free(c->quantlist);
517
0
        c->quantlist=NULL;
518
0
      }
519
0
    }
520
0
  }
521
  /* build the fastpath lookup table */
522
0
  if(n>0){
523
0
    if(n==1 && c->maxlength==1){
524
      /* special case the 'single entry codebook' with a single bit
525
       fastpath table (that always returns entry 0 )in order to use
526
       unmodified decode paths. */
527
0
      c->firsttablen=1;
528
0
      c->firsttable=_ogg_calloc(2,sizeof(*c->firsttable));
529
0
      if(c->firsttable==NULL)goto err_out;
530
0
      c->firsttable[0]=c->firsttable[1]=1;
531
532
0
    }else{
533
0
      int used_bits;
534
0
      int tabn;
535
0
      used_bits=ov_ilog(n);
536
0
      c->firsttablen=(signed char)(used_bits-4); /* this is magic */
537
0
      if(c->firsttablen<5)c->firsttablen=5;
538
      /* if the codewords all have similar lengths, we might wind up with a
539
         table smaller than the shorter codes: increase the size or we could
540
         take the slow path in a large fraction of cases */
541
0
      if(c->firsttablen<c->minlength+1)
542
0
        c->firsttablen=c->minlength+1;
543
      /* also cap the table by the max length, or we are just wasting space */
544
0
      if(c->firsttablen>c->maxlength)
545
0
        c->firsttablen=c->maxlength;
546
      /* if we have an ordered book with a very large minimum length, an 8-bit
547
         table would be mostly wasted.  Instead, try to make one just large
548
         enough to get a good guess for the codeword length. */
549
0
      if(c->codelengths==NULL && c->minlength>8)
550
0
        c->firsttablen=c->maxlength-c->minlength+1;
551
0
      if(c->firsttablen>8)c->firsttablen=8;
552
553
0
      tabn=1<<c->firsttablen;
554
0
      c->firsttable=_ogg_calloc(tabn,sizeof(*c->firsttable));
555
0
      if(c->firsttable==NULL)goto err_out;
556
557
0
      if(c->codelengths!=NULL){
558
        /* unordered codebook:
559
           use the codewords for each entry to build the fastpath table */
560
0
        for(i=0;i<n;i++){
561
0
          if(c->codelengths[i]<=c->firsttablen){
562
0
            ogg_uint32_t orig=bitreverse(c->codelist[i]);
563
0
            for(j=0;j<(1<<(c->firsttablen-c->codelengths[i]));j++)
564
              /* pack the length into the table, too, to avoid an extra lookup.
565
                 This also guarantees the table value is non-zero, since the
566
                 length of any used entry is positive. */
567
0
              c->firsttable[orig|(j<<c->codelengths[i])]=
568
0
               i<<6|c->codelengths[i];
569
0
          }
570
0
        }
571
572
        /* now fill in 'unused' entries in the firsttable with hi/lo search
573
           hints for the non-direct-hits */
574
0
        {
575
0
          ogg_uint32_t mask=0xfffffffeUL<<(31-c->firsttablen);
576
0
          long lo=0,hi=0;
577
0
          int hint_shift;
578
579
0
          c->hi_max=n;
580
0
          hint_shift=used_bits>15?used_bits-15:0;
581
0
          c->hint_shift=(signed char)hint_shift;
582
0
          for(i=0;i<tabn;i++){
583
0
            ogg_uint32_t word=((ogg_uint32_t)i<<(32-c->firsttablen));
584
0
            if(c->firsttable[bitreverse(word)]==0){
585
0
              while((lo+1)<n && c->codelist[lo+1]<=word)lo++;
586
0
              while(    hi<n && word>=(c->codelist[hi]&mask))hi++;
587
588
              /* we only actually have 15 bits per hint to play with here.
589
                 In order to overflow gracefully (nothing breaks, efficiency
590
                 just drops), shift the values down when the range gets too
591
                 large.  We encode the hi value as a difference from the max so
592
                 truncation error moves it upwards without risk of stepping
593
                 past the end of the arrays. */
594
0
              {
595
0
                unsigned long loval=lo>>hint_shift;
596
0
                unsigned long hival=(n-hi)>>hint_shift;
597
598
0
                if(loval>0x7fff)loval=0x7fff;
599
0
                if(hival>0x7fff)hival=0x7fff;
600
0
                c->firsttable[bitreverse(word)]=
601
0
                  0x80000000UL | (loval<<15) | hival;
602
0
              }
603
0
            }
604
0
          }
605
0
        }
606
0
      }else{
607
0
        ogg_uint32_t code;
608
0
        int nlengths;
609
0
        int length;
610
0
        int l;
611
        /* ordered codebook:
612
           step through the code lengths to build the fastpath table */
613
0
        nlengths=c->maxlength-c->minlength+1;
614
0
        c->hi_max=nlengths-1;
615
0
        c->hint_shift=0;
616
0
        length=c->minlength;
617
0
        code=0;
618
0
        for(i=l=0;length<=c->firsttablen;l++,length++){
619
0
          for(;i<c->index[l];i++,code++){
620
0
            ogg_uint32_t orig=bitreverse(code<<(32-length));
621
0
            for(j=0;j<(1<<(c->firsttablen-length));j++){
622
              /* pack the length into the table to avoid an extra lookup */
623
0
              c->firsttable[(j<<length)|orig]=i<<6|length;
624
0
            }
625
0
          }
626
0
          code<<=1;
627
0
        }
628
629
        /* now fill in 'unused' entries in the firsttable with length search
630
           hints for the non-direct hits.  In this case, instead of storing the
631
           hi/lo entry codewords in the table, we store the (index of) the
632
           shortest and longest lengths of any codeword that starts with that
633
           prefix. */
634
0
        if(l<nlengths){
635
0
          int lo=l;
636
0
          do{
637
0
            ogg_uint32_t nleft;
638
0
            ogg_uint32_t slot_count;
639
            /* how many more codes are left of this length? */
640
0
            nleft=(c->codelist[l]>>(32-length))-code+1;
641
0
            slot_count=(ogg_uint32_t)1<<(length-c->firsttablen);
642
0
            if(nleft>=slot_count){
643
0
              ogg_uint32_t word=code>>(length-c->firsttablen);
644
0
              c->firsttable[bitreverse(word<<(32-c->firsttablen))]=
645
0
               0x80000000UL | ((ogg_uint32_t)lo<<15) | (nlengths-1-l);
646
0
              code+=slot_count;
647
0
              lo=l;
648
0
            }else{
649
              /* not enough: consider longer codes */
650
0
              l++;
651
0
              length++;
652
0
              code<<=1;
653
0
              if(nleft==0)lo=l;
654
0
            }
655
0
          }
656
0
          while(l<nlengths);
657
0
        }
658
0
      }
659
0
    }
660
0
  }
661
662
0
  return(0);
663
0
 err_out:
664
0
  if(codes)_ogg_free(codes);
665
  /* our caller will clean up the partially initialized book */
666
0
  return(-1);
667
0
}
668
669
0
long vorbis_book_codeword(codebook *book,int entry){
670
0
  if(book->c) /* only use with encode; decode optimizations are
671
                 allowed to break this */
672
0
    return book->codelist[entry];
673
0
  return -1;
674
0
}
675
676
0
long vorbis_book_codelen(codebook *book,int entry){
677
0
  if(book->c) /* only use with encode; decode optimizations are
678
                 allowed to break this */
679
0
    return book->c->lengthlist[entry];
680
0
  return -1;
681
0
}
682
683
#ifdef _V_SELFTEST
684
685
/* Unit tests of the dequantizer; this stuff will be OK
686
   cross-platform, I simply want to be sure that special mapping cases
687
   actually work properly; a bug could go unnoticed for a while */
688
689
#include <stdio.h>
690
691
/* cases:
692
693
   no mapping
694
   full, explicit mapping
695
   algorithmic mapping
696
697
   nonsequential
698
   sequential
699
*/
700
701
static ogg_uint16_t full_quantlist1[]={0,1,2,3,    4,5,6,7, 8,3,6,1};
702
static ogg_uint16_t partial_quantlist1[]={0,7,2};
703
704
/* no mapping */
705
dec_codebook test1={
706
  4,0,0,0,16,0,0,
707
  0,0,0,0,0,
708
  NULL,
709
  NULL,NULL,NULL,NULL,NULL
710
};
711
static float *test1_result=NULL;
712
713
/* linear, full mapping, nonsequential */
714
dec_codebook test2={
715
  4,0,0,0,3,0,0,
716
  2,4,0,3761766400U,1611661312,
717
  full_quantlist1,
718
  NULL,NULL,NULL,NULL,NULL
719
};
720
static float test2_result[]={-3,-2,-1,0, 1,2,3,4, 5,0,3,-2};
721
722
/* linear, full mapping, sequential */
723
dec_codebook test3={
724
  4,0,0,0,3,0,0,
725
  2,4,1,3761766400U,1611661312,
726
  full_quantlist1,
727
  NULL,NULL,NULL,NULL,NULL
728
};
729
static float test3_result[]={-3,-5,-6,-6, 1,3,6,10, 5,5,8,6};
730
731
/* linear, algorithmic mapping, nonsequential */
732
dec_codebook test4={
733
  3,0,0,0,27,0,0,
734
  1,4,0,3761766400U,1611661312,
735
  partial_quantlist1,
736
  NULL,NULL,NULL,NULL,NULL
737
};
738
static float test4_result[]={-3,-3,-3, 4,-3,-3, -1,-3,-3,
739
                              -3, 4,-3, 4, 4,-3, -1, 4,-3,
740
                              -3,-1,-3, 4,-1,-3, -1,-1,-3,
741
                              -3,-3, 4, 4,-3, 4, -1,-3, 4,
742
                              -3, 4, 4, 4, 4, 4, -1, 4, 4,
743
                              -3,-1, 4, 4,-1, 4, -1,-1, 4,
744
                              -3,-3,-1, 4,-3,-1, -1,-3,-1,
745
                              -3, 4,-1, 4, 4,-1, -1, 4,-1,
746
                              -3,-1,-1, 4,-1,-1, -1,-1,-1};
747
748
/* linear, algorithmic mapping, sequential */
749
dec_codebook test5={
750
  3,0,0,0,27,0,0,
751
  1,4,1,3761766400U,1611661312,
752
  partial_quantlist1,
753
  NULL,NULL,NULL,NULL,NULL,
754
};
755
static float test5_result[]={-3,-6,-9, 4, 1,-2, -1,-4,-7,
756
                              -3, 1,-2, 4, 8, 5, -1, 3, 0,
757
                              -3,-4,-7, 4, 3, 0, -1,-2,-5,
758
                              -3,-6,-2, 4, 1, 5, -1,-4, 0,
759
                              -3, 1, 5, 4, 8,12, -1, 3, 7,
760
                              -3,-4, 0, 4, 3, 7, -1,-2, 2,
761
                              -3,-6,-7, 4, 1, 0, -1,-4,-5,
762
                              -3, 1, 0, 4, 8, 7, -1, 3, 2,
763
                              -3,-4,-5, 4, 3, 2, -1,-2,-3};
764
765
void run_test(dec_codebook *b,float *comp){
766
  float out[3*27];
767
  int i;
768
769
  if(comp){
770
    _book_unquantize(out,b,b->entries,NULL);
771
    for(i=0;i<b->entries*b->dim;i++)
772
      if(fabs(out[i]-comp[i])>.0001){
773
        fprintf(stderr,"disagreement in unquantized and reference data:\n"
774
                "position %d, %g != %g\n",i,out[i],comp[i]);
775
        exit(1);
776
      }
777
778
  }
779
}
780
781
int main(){
782
  /* run the nine dequant tests, and compare to the hand-rolled results */
783
  fprintf(stderr,"Dequant test 1... ");
784
  run_test(&test1,test1_result);
785
  fprintf(stderr,"OK\nDequant test 2... ");
786
  run_test(&test2,test2_result);
787
  fprintf(stderr,"OK\nDequant test 3... ");
788
  run_test(&test3,test3_result);
789
  fprintf(stderr,"OK\nDequant test 4... ");
790
  run_test(&test4,test4_result);
791
  fprintf(stderr,"OK\nDequant test 5... ");
792
  run_test(&test5,test5_result);
793
  fprintf(stderr,"OK\n\n");
794
795
  return(0);
796
}
797
798
#endif