Coverage Report

Created: 2026-07-25 07:08

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
806k
int ov_ilog(ogg_uint32_t v){
31
806k
  int ret;
32
5.61M
  for(ret=0;v;ret++)v>>=1;
33
806k
  return ret;
34
806k
}
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
347k
#define VQ_FMAN 21
42
173k
#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
173k
float _float32_unpack(long val){
61
173k
  double mant=val&0x1fffff;
62
173k
  int    sign=val&0x80000000;
63
173k
  long   exp =(val&0x7fe00000L)>>VQ_FMAN;
64
173k
  if(sign)mant= -mant;
65
173k
  exp=exp-(VQ_FMAN-1)-VQ_FEXP_BIAS;
66
  /* clamp excessive exponent values */
67
173k
  if (exp>63){
68
2.86k
    exp=63;
69
2.86k
  }
70
173k
  if (exp<-63){
71
109k
    exp=-63;
72
109k
  }
73
173k
  return(ldexp(mant,exp));
74
173k
}
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
78.8k
ogg_uint32_t *_make_words(char *l,long n,long sparsecount){
80
78.8k
  long i,j,count=0;
81
78.8k
  ogg_uint32_t marker[33];
82
78.8k
  ogg_uint32_t *r=_ogg_malloc((sparsecount?sparsecount:n)*sizeof(*r));
83
78.8k
  memset(marker,0,sizeof(marker));
84
85
11.5M
  for(i=0;i<n;i++){
86
11.4M
    long length=l[i];
87
11.4M
    if(length>0){
88
8.80M
      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
8.80M
      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
8.80M
      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
8.80M
      {
106
17.5M
        for(j=length;j>0;j--){
107
108
17.4M
          if(marker[j]&1){
109
            /* have to jump branches */
110
8.72M
            if(j==1)
111
78.8k
              marker[1]++;
112
8.64M
            else
113
8.64M
              marker[j]=marker[j-1]<<1;
114
8.72M
            break; /* invariant says next upper marker would already
115
                      have been moved if it was on the same path */
116
8.72M
          }
117
8.72M
          marker[j]++;
118
8.72M
        }
119
8.80M
      }
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
107M
      for(j=length+1;j<33;j++)
125
102M
        if((marker[j]>>1) == entry){
126
98.6M
          entry=marker[j];
127
98.6M
          marker[j]=marker[j-1]<<1;
128
98.6M
        }else
129
4.23M
          break;
130
8.80M
    }else
131
2.63M
      if(sparsecount==0)count++;
132
11.4M
  }
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
78.8k
  if(!(count==1 && marker[2]==2)){
139
2.60M
    for(i=1;i<33;i++)
140
2.52M
      if(marker[i] & (0xffffffffUL>>(32-i))){
141
0
        _ogg_free(r);
142
0
        return(NULL);
143
0
      }
144
78.8k
  }
145
146
  /* bitreverse the words because our bitwise packer/unpacker is LSb
147
     endian */
148
11.5M
  for(i=0,count=0;i<n;i++){
149
11.4M
    ogg_uint32_t temp=0;
150
104M
    for(j=0;j<l[i];j++){
151
92.8M
      temp<<=1;
152
92.8M
      temp|=(r[count]>>j)&1;
153
92.8M
    }
154
155
11.4M
    if(sparsecount){
156
0
      if(l[i])
157
0
        r[count++]=temp;
158
0
    }else
159
11.4M
      r[count++]=temp;
160
11.4M
  }
161
162
78.8k
  return(r);
163
78.8k
}
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
15.9k
static ogg_int64_t *dec_make_words(signed char *l,long n,long sparsecount){
170
15.9k
  long i,j,count=0;
171
15.9k
  ogg_uint32_t marker[33];
172
15.9k
  ogg_int64_t *r=_ogg_malloc((sparsecount?sparsecount:n)*sizeof(*r));
173
15.9k
  if(r==NULL)return(NULL);
174
15.9k
  memset(marker,0,sizeof(marker));
175
176
1.17M
  for(i=0;i<n;i++){
177
1.16M
    long length=l[i];
178
1.16M
    if(length>0){
179
706k
      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
706k
      if(length<32 && (entry>>length)){
188
        /* error condition; the lengths must specify an overpopulated tree */
189
17
        _ogg_free(r);
190
17
        return(NULL);
191
17
      }
192
706k
      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
706k
      {
197
1.40M
        for(j=length;j>0;j--){
198
199
1.38M
          if(marker[j]&1){
200
            /* have to jump branches */
201
690k
            if(j==1)
202
10.7k
              marker[1]++;
203
679k
            else
204
679k
              marker[j]=marker[j-1]<<1;
205
690k
            break; /* invariant says next upper marker would already
206
                      have been moved if it was on the same path */
207
690k
          }
208
697k
          marker[j]++;
209
697k
        }
210
706k
      }
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
7.88M
      for(j=length+1;j<33;j++)
216
7.56M
        if((marker[j]>>1) == entry){
217
7.18M
          entry=marker[j];
218
7.18M
          marker[j]=marker[j-1]<<1;
219
7.18M
        }else
220
382k
          break;
221
706k
    }else
222
457k
      if(sparsecount==0)count++;
223
1.16M
  }
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
15.9k
  if(!(count==1 && marker[2]==2)){
230
351k
    for(i=1;i<33;i++)
231
341k
      if(marker[i] & (0xffffffffUL>>(32-i))){
232
97
        _ogg_free(r);
233
97
        return(NULL);
234
97
      }
235
10.7k
  }
236
237
15.8k
  return(r);
238
15.9k
}
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
117k
long _book_maptype1_quantvals(long dim, long entries){
244
117k
  long vals;
245
117k
  if(entries<1){
246
23
    return(0);
247
23
  }
248
117k
  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
117k
  if(vals<1){
256
0
    vals=1;
257
0
  }
258
117k
  while(1){
259
117k
    long acc=1;
260
117k
    long acc1=1;
261
117k
    int i;
262
465k
    for(i=0;i<dim;i++){
263
347k
      if(entries/vals<acc)break;
264
347k
      acc*=vals;
265
347k
      if(LONG_MAX/(vals+1)<acc1)acc1=LONG_MAX;
266
332k
      else acc1*=vals+1;
267
347k
    }
268
117k
    if(i>=dim && acc<=entries && acc1>entries){
269
117k
      return(vals);
270
117k
    }else{
271
1
      if(i<dim || acc>entries){
272
1
        vals--;
273
1
      }else{
274
0
        vals++;
275
0
      }
276
1
    }
277
117k
  }
278
117k
}
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
7.99k
                      ogg_int64_t *sparsemap){
287
7.99k
  ogg_int32_t i,j,k;
288
7.99k
  if(b->maptype==1 || b->maptype==2){
289
7.99k
    ogg_int32_t quantvals;
290
7.99k
    float mindel=_float32_unpack(b->q_min);
291
7.99k
    float delta=_float32_unpack(b->q_delta);
292
293
    /* maptype 1 and 2 both use a quantized value vector, but
294
       different sizes */
295
7.99k
    switch(b->maptype){
296
4.75k
    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
4.75k
      quantvals=(ogg_int32_t)_book_maptype1_quantvals(b->dim, b->entries);
305
603k
      for(i=0;i<n;i++){
306
598k
        float last=0.f;
307
598k
        int indexdiv=1;
308
598k
        j=(long)(sparsemap?sparsemap[i]&0xffffff:i);
309
8.31M
        for(k=0;k<b->dim;k++){
310
7.71M
          ogg_int32_t index=(j/indexdiv)%quantvals;
311
7.71M
          float val=b->quantlist[index];
312
7.71M
          val=fabs(val)*delta+mindel+last;
313
7.71M
          if(b->q_sequencep)last=val;
314
7.71M
          r[i*b->dim+k]=val;
315
7.71M
          indexdiv*=quantvals;
316
7.71M
        }
317
598k
      }
318
4.75k
      break;
319
3.24k
    case 2:
320
13.9k
      for(i=0;i<n;i++){
321
10.7k
        float last=0.f;
322
10.7k
        j=(ogg_int32_t)(sparsemap?sparsemap[i]&0xffffff:i);
323
35.9k
        for(k=0;k<b->dim;k++){
324
25.2k
          float val=b->quantlist[j*b->dim+k];
325
25.2k
          val=fabs(val)*delta+mindel+last;
326
25.2k
          if(b->q_sequencep)last=val;
327
25.2k
          r[i*b->dim+k]=val;
328
25.2k
        }
329
10.7k
      }
330
3.24k
      break;
331
7.99k
    }
332
333
7.99k
  }
334
7.99k
}
335
336
78.8k
void vorbis_staticbook_destroy(static_codebook *b){
337
78.8k
  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
78.8k
}
344
345
78.8k
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
78.8k
  if(b->valuelist)_ogg_free(b->valuelist);
349
78.8k
  if(b->codelist)_ogg_free(b->codelist);
350
351
78.8k
  memset(b,0,sizeof(*b));
352
78.8k
}
353
354
46.9k
void vorbis_decbook_clear(dec_codebook *b){
355
46.9k
  if(b->quantlist)_ogg_free(b->quantlist);
356
46.9k
  if(b->firsttable)_ogg_free(b->firsttable);
357
46.9k
  if(b->codelist)_ogg_free(b->codelist);
358
46.9k
  if(b->codelengths)_ogg_free(b->codelengths);
359
46.9k
  if(b->index)_ogg_free(b->index);
360
46.9k
  if(b->valuelist)_ogg_free(b->valuelist);
361
46.9k
  memset(b,0,sizeof(*b));
362
46.9k
}
363
364
78.8k
int vorbis_book_init_encode(codebook *c,const static_codebook *s){
365
366
78.8k
  memset(c,0,sizeof(*c));
367
78.8k
  c->c=s;
368
78.8k
  c->entries=s->entries;
369
78.8k
  c->used_entries=s->entries;
370
78.8k
  c->dim=s->dim;
371
78.8k
  c->codelist=_make_words(s->lengthlist,s->entries,0);
372
78.8k
  c->quantvals=_book_maptype1_quantvals(s->dim, s->entries);
373
78.8k
  c->minval=(int)rint(_float32_unpack(s->q_min));
374
78.8k
  c->delta=(int)rint(_float32_unpack(s->q_delta));
375
376
78.8k
  return(0);
377
78.8k
}
378
379
448k
static ogg_uint32_t bitreverse(ogg_uint32_t x){
380
448k
  x=    ((x>>16)&0x0000ffffUL) | ((x<<16)&0xffff0000UL);
381
448k
  x=    ((x>> 8)&0x00ff00ffUL) | ((x<< 8)&0xff00ff00UL);
382
448k
  x=    ((x>> 4)&0x0f0f0f0fUL) | ((x<< 4)&0xf0f0f0f0UL);
383
448k
  x=    ((x>> 2)&0x33333333UL) | ((x<< 2)&0xccccccccUL);
384
448k
  return((x>> 1)&0x55555555UL) | ((x<< 1)&0xaaaaaaaaUL);
385
448k
}
386
387
2.96M
static int sort64a(const void *a,const void *b){
388
2.96M
  return ( *(ogg_int64_t *)a>*(ogg_int64_t *)b)-
389
2.96M
    ( *(ogg_int64_t *)a<*(ogg_int64_t *)b);
390
2.96M
}
391
392
/* decode codebook arrangement is more heavily optimized than encode */
393
17.7k
int vorbis_book_init_decode(dec_codebook *c){
394
17.7k
  ogg_int32_t i,j,n;
395
17.7k
  ogg_int64_t *codes=NULL;
396
397
  /* only initialize once */
398
17.7k
  if(c->codelist)return(0);
399
400
17.7k
  if(c->codelengths){
401
    /* unordered codebook */
402
    /* count actually used entries */
403
16.8k
    n=0;
404
1.18M
    for(i=0;i<c->entries;i++)
405
1.17M
      if(c->codelengths[i]>0)
406
708k
        n++;
407
408
16.8k
    if(n>0){
409
15.9k
      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
15.9k
      codes=dec_make_words(c->codelengths,c->entries,n);
422
15.9k
      if(codes==NULL)goto err_out;
423
424
15.8k
      qsort(codes,n,sizeof(*codes),sort64a);
425
426
15.8k
      c->codelist=_ogg_malloc(n*sizeof(*c->codelist));
427
15.8k
      if(c->codelist==NULL)goto err_out;
428
429
714k
      for(i=0;i<n;i++){
430
698k
        c->codelist[i]=(ogg_uint32_t)(codes[i]>>24);
431
698k
      }
432
433
15.8k
      if(c->maptype==1 || c->maptype==2){
434
7.88k
        c->valuelist=_ogg_malloc(c->dim*n*sizeof(*c->valuelist));
435
7.88k
        if(c->valuelist==NULL)goto err_out;
436
7.88k
        _book_unquantize(c->valuelist,c,n,codes);
437
7.88k
        _ogg_free(c->quantlist);
438
7.88k
        c->quantlist=NULL;
439
7.88k
      }
440
15.8k
      c->index=_ogg_malloc(n*sizeof(*c->index));
441
15.8k
      if(c->index==NULL)goto err_out;
442
15.8k
      codelengths=_ogg_malloc(n*sizeof(*c->codelengths));
443
15.8k
      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
15.8k
      c->minlength=32;
448
15.8k
      c->maxlength=0;
449
714k
      for(i=0;i<n;i++){
450
698k
        j=(ogg_int32_t)(codes[i]&0xffffff);
451
698k
        c->index[i]=j;
452
698k
        codelengths[i]=c->codelengths[j];
453
698k
        if(codelengths[i]<c->minlength)
454
20.3k
          c->minlength=codelengths[i];
455
698k
        if(codelengths[i]>c->maxlength)
456
59.0k
          c->maxlength=codelengths[i];
457
698k
      }
458
15.8k
      _ogg_free(codes);
459
15.8k
      codes=NULL;
460
15.8k
      _ogg_free(c->codelengths);
461
15.8k
      c->codelengths=codelengths;
462
15.8k
    }
463
16.8k
  }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
929
    n=c->entries;
473
929
    if(n>0){
474
727
      ogg_uint32_t prev_entry;
475
727
      ogg_uint32_t code;
476
727
      int nlengths;
477
727
      int length;
478
727
      int l;
479
727
      nlengths=c->maxlength-c->minlength+1;
480
727
      c->codelist=_ogg_malloc(nlengths*sizeof(*c->codelist));
481
727
      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
727
      prev_entry=0;
487
727
      code=0;
488
727
      length=c->minlength;
489
2.26k
      for(l=0;l<nlengths;l++,length++){
490
1.53k
        ogg_uint32_t nentries;
491
1.53k
        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
1.53k
        if(l+1<nlengths && nentries>((0xffffffffUL-code)>>(32-length)))
497
0
          goto err_out;
498
1.53k
        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
1.53k
        c->codelist[l]=code-1;
503
1.53k
        prev_entry=c->index[l];
504
1.53k
      }
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
727
      if(c->codelist[nlengths-1]!=0xffffffffUL){
508
        /* reject over/underpopulated trees, with the exception of a
509
           single-entry codebook. */
510
8
        if(n!=1 || c->maxlength!=1)goto err_out;
511
8
      }
512
719
      if(c->maptype==1 || c->maptype==2){
513
116
        c->valuelist=_ogg_malloc(c->dim*n*sizeof(*c->valuelist));
514
116
        if(c->valuelist==NULL)goto err_out;
515
116
        _book_unquantize(c->valuelist,c,n,NULL);
516
116
        _ogg_free(c->quantlist);
517
116
        c->quantlist=NULL;
518
116
      }
519
719
    }
520
929
  }
521
  /* build the fastpath lookup table */
522
17.6k
  if(n>0){
523
16.5k
    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
5.22k
      c->firsttablen=1;
528
5.22k
      c->firsttable=_ogg_calloc(2,sizeof(*c->firsttable));
529
5.22k
      if(c->firsttable==NULL)goto err_out;
530
5.22k
      c->firsttable[0]=c->firsttable[1]=1;
531
532
11.3k
    }else{
533
11.3k
      int used_bits;
534
11.3k
      int tabn;
535
11.3k
      used_bits=ov_ilog(n);
536
11.3k
      c->firsttablen=(signed char)(used_bits-4); /* this is magic */
537
11.3k
      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
11.3k
      if(c->firsttablen<c->minlength+1)
542
629
        c->firsttablen=c->minlength+1;
543
      /* also cap the table by the max length, or we are just wasting space */
544
11.3k
      if(c->firsttablen>c->maxlength)
545
2.98k
        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
11.3k
      if(c->codelengths==NULL && c->minlength>8)
550
388
        c->firsttablen=c->maxlength-c->minlength+1;
551
11.3k
      if(c->firsttablen>8)c->firsttablen=8;
552
553
11.3k
      tabn=1<<c->firsttablen;
554
11.3k
      c->firsttable=_ogg_calloc(tabn,sizeof(*c->firsttable));
555
11.3k
      if(c->firsttable==NULL)goto err_out;
556
557
11.3k
      if(c->codelengths!=NULL){
558
        /* unordered codebook:
559
           use the codewords for each entry to build the fastpath table */
560
704k
        for(i=0;i<n;i++){
561
693k
          if(c->codelengths[i]<=c->firsttablen){
562
97.2k
            ogg_uint32_t orig=bitreverse(c->codelist[i]);
563
347k
            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
250k
              c->firsttable[orig|(j<<c->codelengths[i])]=
568
250k
               i<<6|c->codelengths[i];
569
97.2k
          }
570
693k
        }
571
572
        /* now fill in 'unused' entries in the firsttable with hi/lo search
573
           hints for the non-direct-hits */
574
10.6k
        {
575
10.6k
          ogg_uint32_t mask=0xfffffffeUL<<(31-c->firsttablen);
576
10.6k
          long lo=0,hi=0;
577
10.6k
          int hint_shift;
578
579
10.6k
          c->hi_max=n;
580
10.6k
          hint_shift=used_bits>15?used_bits-15:0;
581
10.6k
          c->hint_shift=(signed char)hint_shift;
582
309k
          for(i=0;i<tabn;i++){
583
298k
            ogg_uint32_t word=((ogg_uint32_t)i<<(32-c->firsttablen));
584
298k
            if(c->firsttable[bitreverse(word)]==0){
585
550k
              while((lo+1)<n && c->codelist[lo+1]<=word)lo++;
586
710k
              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
47.8k
              {
595
47.8k
                unsigned long loval=lo>>hint_shift;
596
47.8k
                unsigned long hival=(n-hi)>>hint_shift;
597
598
47.8k
                if(loval>0x7fff)loval=0x7fff;
599
47.8k
                if(hival>0x7fff)hival=0x7fff;
600
47.8k
                c->firsttable[bitreverse(word)]=
601
47.8k
                  0x80000000UL | (loval<<15) | hival;
602
47.8k
              }
603
47.8k
            }
604
298k
          }
605
10.6k
        }
606
10.6k
      }else{
607
719
        ogg_uint32_t code;
608
719
        int nlengths;
609
719
        int length;
610
719
        int l;
611
        /* ordered codebook:
612
           step through the code lengths to build the fastpath table */
613
719
        nlengths=c->maxlength-c->minlength+1;
614
719
        c->hi_max=nlengths-1;
615
719
        c->hint_shift=0;
616
719
        length=c->minlength;
617
719
        code=0;
618
1.47k
        for(i=l=0;length<=c->firsttablen;l++,length++){
619
4.01k
          for(;i<c->index[l];i++,code++){
620
3.25k
            ogg_uint32_t orig=bitreverse(code<<(32-length));
621
8.11k
            for(j=0;j<(1<<(c->firsttablen-length));j++){
622
              /* pack the length into the table to avoid an extra lookup */
623
4.86k
              c->firsttable[(j<<length)|orig]=i<<6|length;
624
4.86k
            }
625
3.25k
          }
626
757
          code<<=1;
627
757
        }
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
719
        if(l<nlengths){
635
564
          int lo=l;
636
2.93k
          do{
637
2.93k
            ogg_uint32_t nleft;
638
2.93k
            ogg_uint32_t slot_count;
639
            /* how many more codes are left of this length? */
640
2.93k
            nleft=(c->codelist[l]>>(32-length))-code+1;
641
2.93k
            slot_count=(ogg_uint32_t)1<<(length-c->firsttablen);
642
2.93k
            if(nleft>=slot_count){
643
2.19k
              ogg_uint32_t word=code>>(length-c->firsttablen);
644
2.19k
              c->firsttable[bitreverse(word<<(32-c->firsttablen))]=
645
2.19k
               0x80000000UL | ((ogg_uint32_t)lo<<15) | (nlengths-1-l);
646
2.19k
              code+=slot_count;
647
2.19k
              lo=l;
648
2.19k
            }else{
649
              /* not enough: consider longer codes */
650
740
              l++;
651
740
              length++;
652
740
              code<<=1;
653
740
              if(nleft==0)lo=l;
654
740
            }
655
2.93k
          }
656
2.93k
          while(l<nlengths);
657
564
        }
658
719
      }
659
11.3k
    }
660
16.5k
  }
661
662
17.6k
  return(0);
663
122
 err_out:
664
122
  if(codes)_ogg_free(codes);
665
  /* our caller will clean up the partially initialized book */
666
122
  return(-1);
667
17.6k
}
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