Coverage Report

Created: 2026-11-23 06:59

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