Coverage Report

Created: 2026-07-25 07:52

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.48k
int vorbis_decbook_unpack(dec_codebook *c,oggpack_buffer *opb){
148
1.48k
  long i;
149
150
  /* make sure alignment is correct */
151
1.48k
  if(oggpack_read(opb,24)!=0x564342)goto _eofout;
152
153
  /* first the basic parameters */
154
1.43k
  c->dim=(signed char)oggpack_read(opb,16);
155
1.43k
  c->entries=(ogg_int32_t)oggpack_read(opb,24);
156
1.43k
  if(c->entries==-1)goto _eofout;
157
158
1.43k
  if(ov_ilog(c->dim)+ov_ilog(c->entries)>24)goto _eofout;
159
160
  /* codeword ordering.... length ordered or unordered? */
161
1.42k
  switch((int)oggpack_read(opb,1)){
162
1.03k
  case 0:{
163
1.03k
    long unused;
164
    /* allocated but unused entries? */
165
1.03k
    unused=oggpack_read(opb,1);
166
1.03k
    if((c->entries*(unused?1:5)+7)>>3>opb->storage-oggpack_bytes(opb))
167
12
      goto _eofout;
168
    /* unordered */
169
1.02k
    c->codelengths=_ogg_malloc(sizeof(*c->codelengths)*c->entries);
170
1.02k
    if(c->codelengths==NULL)goto _errout;
171
172
    /* allocated but unused entries? */
173
1.02k
    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.59k
          long num=oggpack_read(opb,5);
179
5.59k
          if(num==-1)goto _eofout;
180
5.58k
          c->codelengths[i]=(signed char)(num+1);
181
5.58k
        }else
182
27.0k
          c->codelengths[i]=0;
183
32.5k
      }
184
985
    }else{
185
      /* all entries used; no tagging */
186
16.7k
      for(i=0;i<c->entries;i++){
187
15.7k
        long num=oggpack_read(opb,5);
188
15.7k
        if(num==-1)goto _eofout;
189
15.7k
        c->codelengths[i]=(signed char)(num+1);
190
15.7k
      }
191
985
    }
192
193
1.02k
    break;
194
1.02k
  }
195
1.02k
  case 1:
196
    /* ordered */
197
382
    {
198
382
      ogg_int32_t cum_entries[32];
199
382
      long minlength=oggpack_read(opb,5)+1;
200
382
      long maxlength;
201
382
      long length;
202
382
      if(minlength==0)goto _eofout;
203
382
      maxlength=length=minlength;
204
1.37k
      for(i=0;i<c->entries;length++){
205
1.06k
        long num=oggpack_read(opb,ov_ilog(c->entries-i));
206
1.06k
        if(num==-1)goto _eofout;
207
1.06k
        if(length>32 || num>c->entries-i ||
208
1.01k
           (num>0 && (num-1)>>(length-1)>1)){
209
69
          goto _errout;
210
69
        }
211
992
        i+=num;
212
992
        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
992
        if(i==0)minlength++;
216
992
        maxlength=length;
217
992
      }
218
309
      c->minlength=(signed char)minlength;
219
309
      c->maxlength=(signed char)maxlength;
220
309
      c->index=_ogg_malloc((maxlength-minlength+1)*sizeof(*c->index));
221
309
      if(c->index==NULL)goto _errout;
222
309
      memcpy(c->index,cum_entries,(maxlength-minlength+1)*sizeof(*c->index));
223
309
    }
224
0
    break;
225
1
  default:
226
    /* EOF */
227
1
    goto _eofout;
228
1.42k
  }
229
230
  /* Do we have a mapping to unpack? */
231
1.32k
  switch((c->maptype=(signed char)oggpack_read(opb,4))){
232
47
  case 0:
233
    /* no mapping */
234
47
    break;
235
1.27k
  case 1: case 2:
236
    /* implicitly populated value mapping */
237
    /* explicitly populated value mapping */
238
239
1.27k
    c->q_min=(ogg_uint32_t)oggpack_read(opb,32);
240
1.27k
    c->q_delta=(ogg_uint32_t)oggpack_read(opb,32);
241
1.27k
    c->q_quant=(signed char)oggpack_read(opb,4)+1;
242
1.27k
    c->q_sequencep=(signed char)oggpack_read(opb,1);
243
1.27k
    if(c->q_sequencep==-1)goto _eofout;
244
245
1.26k
    {
246
1.26k
      long q=0;
247
1.26k
      int quantvals=0;
248
1.26k
      switch(c->maptype){
249
1.22k
      case 1:
250
1.22k
        quantvals=(c->dim==0?0:_book_maptype1_quantvals(c->dim, c->entries));
251
1.22k
        break;
252
40
      case 2:
253
40
        quantvals=c->entries*c->dim;
254
40
        break;
255
1.26k
      }
256
257
      /* quantized values */
258
1.26k
      if(((quantvals*c->q_quant+7)>>3)>opb->storage-oggpack_bytes(opb))
259
4
        goto _eofout;
260
1.26k
      c->quantlist=_ogg_malloc(sizeof(*c->quantlist)*quantvals);
261
1.26k
      if(c->quantlist==NULL)goto _errout;
262
13.8k
      for(i=0;i<quantvals;i++){
263
12.5k
        q=oggpack_read(opb,c->q_quant);
264
12.5k
        c->quantlist[i]=(ogg_uint16_t)q;
265
12.5k
      }
266
267
1.26k
      if(q==-1)goto _eofout;
268
1.26k
    }
269
1.26k
    break;
270
1.26k
  default:
271
12
    goto _errout;
272
1.32k
  }
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.31k
  return(0);
280
281
81
 _errout:
282
173
 _eofout:
283
  /* our caller will clean up the partially constructed book */
284
173
  return(-1);
285
81
}
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.81M
STIN long decode_packed_entry_number(dec_codebook *book, oggpack_buffer *b){
311
3.81M
  ogg_uint32_t testword;
312
3.81M
  int  read=book->maxlength;
313
3.81M
  long lo,hi;
314
3.81M
  long lok = oggpack_look(b,book->firsttablen);
315
316
3.81M
  if (lok >= 0) {
317
3.75M
    long entry = book->firsttable[lok];
318
3.75M
    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.75M
    }else{
322
3.75M
      oggpack_adv(b, (int)(entry&0x3f));
323
3.75M
      return(entry>>6);
324
3.75M
    }
325
3.75M
  }else{
326
61.2k
    lo=0;
327
61.2k
    hi=book->hi_max;
328
61.2k
  }
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
61.2k
  lok = oggpack_look(b, read);
337
338
61.2k
  while(lok<0 && read>1)
339
0
    lok = oggpack_look(b, --read);
340
61.2k
  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
827k
long vorbis_book_decode(dec_codebook *book, oggpack_buffer *b){
390
827k
  if(book->codelist){
391
827k
    long packed_entry=decode_packed_entry_number(book,b);
392
827k
    if(packed_entry>=0){
393
      /* if we have an unordered book, look up the original entry number */
394
772k
      if(book->codelengths)
395
767k
        return(book->index[packed_entry]);
396
      /* if we have an ordered book, the packed_entry number is the original */
397
5.04k
      return(packed_entry);
398
772k
    }
399
827k
  }
400
401
  /* if there's no codelist, the codebook hasn't been init'd */
402
55.3k
  return(-1);
403
827k
}
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
473k
                              int n){
409
473k
  if(book->codelist){
410
473k
    int step=n/book->dim;
411
473k
    long *entry = alloca(sizeof(*entry)*step);
412
473k
    float **t = alloca(sizeof(*t)*step);
413
473k
    int i,j,o;
414
415
938k
    for (i = 0; i < step; i++) {
416
468k
      entry[i]=decode_packed_entry_number(book,b);
417
468k
      if(entry[i]==-1)return(-1);
418
465k
      t[i] = book->valuelist+entry[i]*book->dim;
419
465k
    }
420
3.95M
    for(i=0,o=0;i<book->dim;i++,o+=step)
421
4.04M
      for (j=0;o+j<n && j<step;j++)
422
555k
        a[o+j]+=t[j][i];
423
470k
  }
424
470k
  return(0);
425
473k
}
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
89.2k
                             int n){
430
89.2k
  if(book->codelist){
431
89.2k
    int i,j,entry;
432
89.2k
    float *t;
433
434
459k
    for(i=0;i<n;){
435
371k
      entry = decode_packed_entry_number(book,b);
436
371k
      if(entry==-1)return(-1);
437
369k
      t     = book->valuelist+entry*book->dim;
438
1.52M
      for(j=0;i<n && j<book->dim;)
439
1.15M
        a[i++]+=t[j++];
440
369k
    }
441
89.2k
  }
442
87.9k
  return(0);
443
89.2k
}
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
15.5k
                             int n){
450
15.5k
  if(book->codelist){
451
15.5k
    int i,j,entry;
452
15.5k
    float *t;
453
454
216k
    for(i=0;i<n;){
455
201k
      entry = decode_packed_entry_number(book,b);
456
201k
      if(entry==-1)return(-1);
457
201k
      t     = book->valuelist+entry*book->dim;
458
676k
      for (j=0;i<n && j<book->dim;){
459
475k
        a[i++]=t[j++];
460
475k
      }
461
201k
    }
462
15.5k
  }else{
463
0
    int i;
464
465
0
    for(i=0;i<n;){
466
0
      a[i++]=0.f;
467
0
    }
468
0
  }
469
15.1k
  return(0);
470
15.5k
}
471
472
long vorbis_book_decodevv_add(dec_codebook *book,float **a,long offset,int ch,
473
97.9k
                              oggpack_buffer *b,int n){
474
475
97.9k
  long i,j,entry;
476
97.9k
  int chptr=0;
477
97.9k
  if(book->codelist){
478
97.9k
    int m=(offset+n)/ch;
479
2.04M
    for(i=offset/ch;i<m;){
480
1.94M
      entry = decode_packed_entry_number(book,b);
481
1.94M
      if(entry==-1)return(-1);
482
1.94M
      {
483
1.94M
        const float *t = book->valuelist+entry*book->dim;
484
6.52M
        for (j=0;i<m && j<book->dim;j++){
485
4.58M
          a[chptr++][i]+=t[j];
486
4.58M
          if(chptr==ch){
487
73.9k
            chptr=0;
488
73.9k
            i++;
489
73.9k
          }
490
4.58M
        }
491
1.94M
      }
492
1.94M
    }
493
97.9k
  }
494
96.8k
  return(0);
495
97.9k
}