Coverage Report

Created: 2026-09-14 08:00

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/vorbis/lib/codebook.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 codebook pack/unpack/code/decode operations
14
15
 ********************************************************************/
16
17
#include <stdlib.h>
18
#include <string.h>
19
#include <math.h>
20
#include <ogg/ogg.h>
21
#include "vorbis/codec.h"
22
#include "codebook.h"
23
#include "scales.h"
24
#include "misc.h"
25
#include "os.h"
26
27
/* packs the given codebook into the bitstream **************************/
28
29
0
int vorbis_staticbook_pack(const static_codebook *c,oggpack_buffer *opb){
30
0
  long i,j;
31
0
  int ordered=0;
32
33
  /* first the basic parameters */
34
0
  oggpack_write(opb,0x564342,24);
35
0
  oggpack_write(opb,c->dim,16);
36
0
  oggpack_write(opb,c->entries,24);
37
38
  /* pack the codewords.  There are two packings; length ordered and
39
     length random.  Decide between the two now. */
40
41
0
  for(i=1;i<c->entries;i++)
42
0
    if(c->lengthlist[i-1]==0 || c->lengthlist[i]<c->lengthlist[i-1])break;
43
0
  if(i==c->entries)ordered=1;
44
45
0
  if(ordered){
46
    /* length ordered.  We only need to say how many codewords of
47
       each length.  The actual codewords are generated
48
       deterministically */
49
50
0
    long count=0;
51
0
    oggpack_write(opb,1,1);  /* ordered */
52
0
    oggpack_write(opb,c->lengthlist[0]-1,5); /* 1 to 32 */
53
54
0
    for(i=1;i<c->entries;i++){
55
0
      char this=c->lengthlist[i];
56
0
      char last=c->lengthlist[i-1];
57
0
      if(this>last){
58
0
        for(j=last;j<this;j++){
59
0
          oggpack_write(opb,i-count,ov_ilog(c->entries-count));
60
0
          count=i;
61
0
        }
62
0
      }
63
0
    }
64
0
    oggpack_write(opb,i-count,ov_ilog(c->entries-count));
65
66
0
  }else{
67
    /* length random.  Again, we don't code the codeword itself, just
68
       the length.  This time, though, we have to encode each length */
69
0
    oggpack_write(opb,0,1);   /* unordered */
70
71
    /* algortihmic mapping has use for 'unused entries', which we tag
72
       here.  The algorithmic mapping happens as usual, but the unused
73
       entry has no codeword. */
74
0
    for(i=0;i<c->entries;i++)
75
0
      if(c->lengthlist[i]==0)break;
76
77
0
    if(i==c->entries){
78
0
      oggpack_write(opb,0,1); /* no unused entries */
79
0
      for(i=0;i<c->entries;i++)
80
0
        oggpack_write(opb,c->lengthlist[i]-1,5);
81
0
    }else{
82
0
      oggpack_write(opb,1,1); /* we have unused entries; thus we tag */
83
0
      for(i=0;i<c->entries;i++){
84
0
        if(c->lengthlist[i]==0){
85
0
          oggpack_write(opb,0,1);
86
0
        }else{
87
0
          oggpack_write(opb,1,1);
88
0
          oggpack_write(opb,c->lengthlist[i]-1,5);
89
0
        }
90
0
      }
91
0
    }
92
0
  }
93
94
  /* is the entry number the desired return value, or do we have a
95
     mapping? If we have a mapping, what type? */
96
0
  oggpack_write(opb,c->maptype,4);
97
0
  switch(c->maptype){
98
0
  case 0:
99
    /* no mapping */
100
0
    break;
101
0
  case 1:case 2:
102
    /* implicitly populated value mapping */
103
    /* explicitly populated value mapping */
104
105
0
    if(!c->quantlist){
106
      /* no quantlist?  error */
107
0
      return(-1);
108
0
    }
109
110
    /* values that define the dequantization */
111
0
    oggpack_write(opb,c->q_min,32);
112
0
    oggpack_write(opb,c->q_delta,32);
113
0
    oggpack_write(opb,c->q_quant-1,4);
114
0
    oggpack_write(opb,c->q_sequencep,1);
115
116
0
    {
117
0
      int quantvals;
118
0
      switch(c->maptype){
119
0
      case 1:
120
        /* a single column of (c->entries/c->dim) quantized values for
121
           building a full value list algorithmically (square lattice) */
122
0
        quantvals=_book_maptype1_quantvals(c->dim, c->entries);
123
0
        break;
124
0
      case 2:
125
        /* every value (c->entries*c->dim total) specified explicitly */
126
0
        quantvals=c->entries*c->dim;
127
0
        break;
128
0
      default: /* NOT_REACHABLE */
129
0
        quantvals=-1;
130
0
      }
131
132
      /* quantized values */
133
0
      for(i=0;i<quantvals;i++)
134
0
        oggpack_write(opb,labs(c->quantlist[i]),c->q_quant);
135
136
0
    }
137
0
    break;
138
0
  default:
139
    /* error case; we don't have any other map types now */
140
0
    return(-1);
141
0
  }
142
143
0
  return(0);
144
0
}
145
146
/* unpacks a codebook from the packet buffer into the codebook struct */
147
1.55k
int vorbis_decbook_unpack(dec_codebook *c,oggpack_buffer *opb){
148
1.55k
  long i;
149
150
  /* make sure alignment is correct */
151
1.55k
  if(oggpack_read(opb,24)!=0x564342)goto _eofout;
152
153
  /* first the basic parameters */
154
1.50k
  c->dim=(signed char)oggpack_read(opb,16);
155
1.50k
  c->entries=(ogg_int32_t)oggpack_read(opb,24);
156
1.50k
  if(c->entries==-1)goto _eofout;
157
158
1.50k
  if(ov_ilog(c->dim)+ov_ilog(c->entries)>24)goto _eofout;
159
160
  /* codeword ordering.... length ordered or unordered? */
161
1.48k
  switch((int)oggpack_read(opb,1)){
162
1.16k
  case 0:{
163
1.16k
    long unused;
164
    /* allocated but unused entries? */
165
1.16k
    unused=oggpack_read(opb,1);
166
1.16k
    if((c->entries*(unused?1:5)+7)>>3>opb->storage-oggpack_bytes(opb))
167
13
      goto _eofout;
168
    /* unordered */
169
1.14k
    c->codelengths=_ogg_malloc(sizeof(*c->codelengths)*c->entries);
170
1.14k
    if(c->codelengths==NULL)goto _errout;
171
172
    /* allocated but unused entries? */
173
1.14k
    if(unused){
174
      /* yes, unused entries */
175
176
32.6k
      for(i=0;i<c->entries;i++){
177
32.5k
        if(oggpack_read(opb,1)){
178
5.58k
          long num=oggpack_read(opb,5);
179
5.58k
          if(num==-1)goto _eofout;
180
5.57k
          c->codelengths[i]=(signed char)(num+1);
181
5.57k
        }else
182
26.9k
          c->codelengths[i]=0;
183
32.5k
      }
184
1.10k
    }else{
185
      /* all entries used; no tagging */
186
14.4k
      for(i=0;i<c->entries;i++){
187
13.3k
        long num=oggpack_read(opb,5);
188
13.3k
        if(num==-1)goto _eofout;
189
13.3k
        c->codelengths[i]=(signed char)(num+1);
190
13.3k
      }
191
1.10k
    }
192
193
1.14k
    break;
194
1.14k
  }
195
1.14k
  case 1:
196
    /* ordered */
197
326
    {
198
326
      ogg_int32_t cum_entries[32];
199
326
      long minlength=oggpack_read(opb,5)+1;
200
326
      long maxlength;
201
326
      long length;
202
326
      if(minlength==0)goto _eofout;
203
326
      maxlength=length=minlength;
204
1.26k
      for(i=0;i<c->entries;length++){
205
1.00k
        long num=oggpack_read(opb,ov_ilog(c->entries-i));
206
1.00k
        if(num==-1)goto _eofout;
207
1.00k
        if(length>32 || num>c->entries-i ||
208
957
           (num>0 && (num-1)>>(length-1)>1)){
209
68
          goto _errout;
210
68
        }
211
936
        i+=num;
212
936
        cum_entries[length-minlength]=(ogg_int32_t)i;
213
        /* skip lengths with 0 entries at the start, to avoid creating a
214
           malformed codeword sentinel in vorbis_book_init_decode(). */
215
936
        if(i==0)minlength++;
216
936
        maxlength=length;
217
936
      }
218
254
      c->minlength=(signed char)minlength;
219
254
      c->maxlength=(signed char)maxlength;
220
254
      c->index=_ogg_malloc((maxlength-minlength+1)*sizeof(*c->index));
221
254
      if(c->index==NULL)goto _errout;
222
254
      memcpy(c->index,cum_entries,(maxlength-minlength+1)*sizeof(*c->index));
223
254
    }
224
0
    break;
225
2
  default:
226
    /* EOF */
227
2
    goto _eofout;
228
1.48k
  }
229
230
  /* Do we have a mapping to unpack? */
231
1.39k
  switch((c->maptype=(signed char)oggpack_read(opb,4))){
232
44
  case 0:
233
    /* no mapping */
234
44
    break;
235
1.33k
  case 1: case 2:
236
    /* implicitly populated value mapping */
237
    /* explicitly populated value mapping */
238
239
1.33k
    c->q_min=(ogg_uint32_t)oggpack_read(opb,32);
240
1.33k
    c->q_delta=(ogg_uint32_t)oggpack_read(opb,32);
241
1.33k
    c->q_quant=(signed char)oggpack_read(opb,4)+1;
242
1.33k
    c->q_sequencep=(signed char)oggpack_read(opb,1);
243
1.33k
    if(c->q_sequencep==-1)goto _eofout;
244
245
1.33k
    {
246
1.33k
      long q=0;
247
1.33k
      int quantvals=0;
248
1.33k
      switch(c->maptype){
249
1.30k
      case 1:
250
1.30k
        quantvals=(c->dim==0?0:_book_maptype1_quantvals(c->dim, c->entries));
251
1.30k
        break;
252
32
      case 2:
253
32
        quantvals=c->entries*c->dim;
254
32
        break;
255
1.33k
      }
256
257
      /* quantized values */
258
1.33k
      if(((quantvals*c->q_quant+7)>>3)>opb->storage-oggpack_bytes(opb))
259
3
        goto _eofout;
260
1.33k
      c->quantlist=_ogg_malloc(sizeof(*c->quantlist)*quantvals);
261
1.33k
      if(c->quantlist==NULL)goto _errout;
262
10.7k
      for(i=0;i<quantvals;i++){
263
9.40k
        q=oggpack_read(opb,c->q_quant);
264
9.40k
        c->quantlist[i]=(ogg_uint16_t)q;
265
9.40k
      }
266
267
1.33k
      if(q==-1)goto _eofout;
268
1.33k
    }
269
1.33k
    break;
270
1.33k
  default:
271
14
    goto _errout;
272
1.39k
  }
273
274
  /* all set */
275
  /* we could finish initializing the decode tables here, but this was a
276
     separate step back when we had a shared encoder/decoder codebook struct,
277
     and deferring it ensures we continue to report the same kinds of errors
278
     in the same places. */
279
1.37k
  return(0);
280
281
82
 _errout:
282
172
 _eofout:
283
  /* our caller will clean up the partially constructed book */
284
172
  return(-1);
285
82
}
286
287
/* returns the number of bits ************************************************/
288
0
int vorbis_book_encode(codebook *book, int a, oggpack_buffer *b){
289
0
  if(a<0 || a>=book->c->entries)return(0);
290
0
  oggpack_write(b,book->codelist[a],book->c->lengthlist[a]);
291
0
  return(book->c->lengthlist[a]);
292
0
}
293
294
/* the 'eliminate the decode tree' optimization actually requires the
295
   codewords to be MSb first, not LSb.  This is an annoying inelegancy
296
   (and one of the first places where carefully thought out design
297
   turned out to be wrong; Vorbis II and future Ogg codecs should go
298
   to an MSb bitpacker), but not actually the huge hit it appears to
299
   be.  The first-stage decode table catches most words so that
300
   bitreverse is not in the main execution path. */
301
302
0
static ogg_uint32_t bitreverse(ogg_uint32_t x){
303
0
  x=    ((x>>16)&0x0000ffff) | ((x<<16)&0xffff0000);
304
0
  x=    ((x>> 8)&0x00ff00ff) | ((x<< 8)&0xff00ff00);
305
0
  x=    ((x>> 4)&0x0f0f0f0f) | ((x<< 4)&0xf0f0f0f0);
306
0
  x=    ((x>> 2)&0x33333333) | ((x<< 2)&0xcccccccc);
307
0
  return((x>> 1)&0x55555555) | ((x<< 1)&0xaaaaaaaa);
308
0
}
309
310
3.86M
STIN long decode_packed_entry_number(dec_codebook *book, oggpack_buffer *b){
311
3.86M
  ogg_uint32_t testword;
312
3.86M
  int  read=book->maxlength;
313
3.86M
  long lo,hi;
314
3.86M
  long lok = oggpack_look(b,book->firsttablen);
315
316
3.86M
  if (lok >= 0) {
317
3.79M
    long entry = book->firsttable[lok];
318
3.79M
    if(entry&0x80000000UL){
319
0
      lo=((entry>>15)&0x7fff)<<book->hint_shift;
320
0
      hi=book->hi_max-((entry&0x7fff)<<book->hint_shift);
321
3.79M
    }else{
322
3.79M
      oggpack_adv(b, (int)(entry&0x3f));
323
3.79M
      return(entry>>6);
324
3.79M
    }
325
3.79M
  }else{
326
65.4k
    lo=0;
327
65.4k
    hi=book->hi_max;
328
65.4k
  }
329
330
  /* Single entry codebooks use a firsttablen of 1 and a
331
     dec_maxlength of 1.  If a single-entry codebook gets here (due to
332
     failure to read one bit above), the next look attempt will also
333
     fail and we'll correctly kick out instead of trying to walk the
334
     underformed tree */
335
336
65.4k
  lok = oggpack_look(b, read);
337
338
65.4k
  while(lok<0 && read>1)
339
0
    lok = oggpack_look(b, --read);
340
65.4k
  if(lok<0)return -1;
341
342
0
  testword=bitreverse((ogg_uint32_t)lok);
343
0
  if(book->codelengths==NULL){
344
0
    int length;
345
    /* ordered codebook: search for the codeword length */
346
0
    while(testword>book->codelist[lo])lo++;
347
0
    length=(int)lo+book->minlength;
348
0
    if(length<=read){
349
0
      long entry=
350
0
       book->index[lo]-(((book->codelist[lo]-testword)>>(32-length))+1);
351
0
      oggpack_adv(b, length);
352
0
      return(entry);
353
0
    }
354
0
  }else{
355
    /* bisect search for the codeword in the ordered list */
356
0
    while(hi-lo>1){
357
0
      long p=(hi-lo)>>1;
358
0
      long test=book->codelist[lo+p]>testword;
359
0
      lo+=p&(test-1);
360
0
      hi-=p&(-test);
361
0
      }
362
363
0
    if(book->codelengths[lo]<=read){
364
0
      oggpack_adv(b, book->codelengths[lo]);
365
0
      return(lo);
366
0
    }
367
0
  }
368
369
0
  oggpack_adv(b, read);
370
371
0
  return(-1);
372
0
}
373
374
/* Decode side is specced and easier, because we don't need to find
375
   matches using different criteria; we simply read and map.  There are
376
   two things we need to do 'depending':
377
378
   We may need to support interleave.  We don't really, but it's
379
   convenient to do it here rather than rebuild the vector later.
380
381
   Cascades may be additive or multiplicitive; this is not inherent in
382
   the codebook, but set in the code using the codebook.  Like
383
   interleaving, it's easiest to do it here.
384
   addmul==0 -> declarative (set the value)
385
   addmul==1 -> additive
386
   addmul==2 -> multiplicitive */
387
388
/* returns the [original, not compacted] entry number or -1 on eof *********/
389
1.05M
long vorbis_book_decode(dec_codebook *book, oggpack_buffer *b){
390
1.05M
  if(book->codelist){
391
1.05M
    long packed_entry=decode_packed_entry_number(book,b);
392
1.05M
    if(packed_entry>=0){
393
      /* if we have an unordered book, look up the original entry number */
394
1.00M
      if(book->codelengths)
395
959k
        return(book->index[packed_entry]);
396
      /* if we have an ordered book, the packed_entry number is the original */
397
40.9k
      return(packed_entry);
398
1.00M
    }
399
1.05M
  }
400
401
  /* if there's no codelist, the codebook hasn't been init'd */
402
59.6k
  return(-1);
403
1.05M
}
404
405
/* returns 0 on OK or -1 on eof *************************************/
406
/* decode vector / dim granularity gaurding is done in the upper layer */
407
long vorbis_book_decodevs_add(dec_codebook *book,float *a,oggpack_buffer *b,
408
722k
                              int n){
409
722k
  if(book->codelist){
410
722k
    int step=n/book->dim;
411
722k
    long *entry = alloca(sizeof(*entry)*step);
412
722k
    float **t = alloca(sizeof(*t)*step);
413
722k
    int i,j,o;
414
415
1.32M
    for (i = 0; i < step; i++) {
416
602k
      entry[i]=decode_packed_entry_number(book,b);
417
602k
      if(entry[i]==-1)return(-1);
418
598k
      t[i] = book->valuelist+entry[i]*book->dim;
419
598k
    }
420
9.19M
    for(i=0,o=0;i<book->dim;i++,o+=step)
421
9.37M
      for (j=0;o+j<n && j<step;j++)
422
895k
        a[o+j]+=t[j][i];
423
719k
  }
424
719k
  return(0);
425
722k
}
426
427
/* decode vector / dim granularity gaurding is done in the upper layer */
428
long vorbis_book_decodev_add(dec_codebook *book,float *a,oggpack_buffer *b,
429
88.0k
                             int n){
430
88.0k
  if(book->codelist){
431
88.0k
    int i,j,entry;
432
88.0k
    float *t;
433
434
356k
    for(i=0;i<n;){
435
269k
      entry = decode_packed_entry_number(book,b);
436
269k
      if(entry==-1)return(-1);
437
268k
      t     = book->valuelist+entry*book->dim;
438
953k
      for(j=0;i<n && j<book->dim;)
439
685k
        a[i++]+=t[j++];
440
268k
    }
441
88.0k
  }
442
86.9k
  return(0);
443
88.0k
}
444
445
/* unlike the others, we guard against n not being an integer number
446
   of <dim> internally rather than in the upper layer (called only by
447
   floor0) */
448
long vorbis_book_decodev_set(dec_codebook *book,float *a,oggpack_buffer *b,
449
11.4k
                             int n){
450
11.4k
  if(book->codelist){
451
11.4k
    int i,j,entry;
452
11.4k
    float *t;
453
454
175k
    for(i=0;i<n;){
455
164k
      entry = decode_packed_entry_number(book,b);
456
164k
      if(entry==-1)return(-1);
457
164k
      t     = book->valuelist+entry*book->dim;
458
563k
      for (j=0;i<n && j<book->dim;){
459
399k
        a[i++]=t[j++];
460
399k
      }
461
164k
    }
462
11.4k
  }else{
463
0
    int i;
464
465
0
    for(i=0;i<n;){
466
0
      a[i++]=0.f;
467
0
    }
468
0
  }
469
11.2k
  return(0);
470
11.4k
}
471
472
long vorbis_book_decodevv_add(dec_codebook *book,float **a,long offset,int ch,
473
83.1k
                              oggpack_buffer *b,int n){
474
475
83.1k
  long i,j,entry;
476
83.1k
  int chptr=0;
477
83.1k
  if(book->codelist){
478
83.1k
    int m=(offset+n)/ch;
479
1.84M
    for(i=offset/ch;i<m;){
480
1.76M
      entry = decode_packed_entry_number(book,b);
481
1.76M
      if(entry==-1)return(-1);
482
1.76M
      {
483
1.76M
        const float *t = book->valuelist+entry*book->dim;
484
5.76M
        for (j=0;i<m && j<book->dim;j++){
485
3.99M
          a[chptr++][i]+=t[j];
486
3.99M
          if(chptr==ch){
487
69.6k
            chptr=0;
488
69.6k
            i++;
489
69.6k
          }
490
3.99M
        }
491
1.76M
      }
492
1.76M
    }
493
83.1k
  }
494
82.1k
  return(0);
495
83.1k
}