Coverage Report

Created: 2026-10-28 08:13

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.57k
int vorbis_decbook_unpack(dec_codebook *c,oggpack_buffer *opb){
148
1.57k
  long i;
149
150
  /* make sure alignment is correct */
151
1.57k
  if(oggpack_read(opb,24)!=0x564342)goto _eofout;
152
153
  /* first the basic parameters */
154
1.52k
  c->dim=(signed char)oggpack_read(opb,16);
155
1.52k
  c->entries=(ogg_int32_t)oggpack_read(opb,24);
156
1.52k
  if(c->entries==-1)goto _eofout;
157
158
1.52k
  if(ov_ilog(c->dim)+ov_ilog(c->entries)>24)goto _eofout;
159
160
  /* codeword ordering.... length ordered or unordered? */
161
1.50k
  switch((int)oggpack_read(opb,1)){
162
1.17k
  case 0:{
163
1.17k
    long unused;
164
    /* allocated but unused entries? */
165
1.17k
    unused=oggpack_read(opb,1);
166
1.17k
    if((c->entries*(unused?1:5)+7)>>3>opb->storage-oggpack_bytes(opb))
167
13
      goto _eofout;
168
    /* unordered */
169
1.15k
    c->codelengths=_ogg_malloc(sizeof(*c->codelengths)*c->entries);
170
1.15k
    if(c->codelengths==NULL)goto _errout;
171
172
    /* allocated but unused entries? */
173
1.15k
    if(unused){
174
      /* yes, unused entries */
175
176
17.4k
      for(i=0;i<c->entries;i++){
177
17.4k
        if(oggpack_read(opb,1)){
178
2.51k
          long num=oggpack_read(opb,5);
179
2.51k
          if(num==-1)goto _eofout;
180
2.50k
          c->codelengths[i]=(signed char)(num+1);
181
2.50k
        }else
182
14.9k
          c->codelengths[i]=0;
183
17.4k
      }
184
1.12k
    }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.12k
    }
192
193
1.15k
    break;
194
1.15k
  }
195
1.15k
  case 1:
196
    /* ordered */
197
334
    {
198
334
      ogg_int32_t cum_entries[32];
199
334
      long minlength=oggpack_read(opb,5)+1;
200
334
      long maxlength;
201
334
      long length;
202
334
      if(minlength==0)goto _eofout;
203
334
      maxlength=length=minlength;
204
1.27k
      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
65
          goto _errout;
210
65
        }
211
938
        i+=num;
212
938
        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
938
        if(i==0)minlength++;
216
938
        maxlength=length;
217
938
      }
218
265
      c->minlength=(signed char)minlength;
219
265
      c->maxlength=(signed char)maxlength;
220
265
      c->index=_ogg_malloc((maxlength-minlength+1)*sizeof(*c->index));
221
265
      if(c->index==NULL)goto _errout;
222
265
      memcpy(c->index,cum_entries,(maxlength-minlength+1)*sizeof(*c->index));
223
265
    }
224
0
    break;
225
2
  default:
226
    /* EOF */
227
2
    goto _eofout;
228
1.50k
  }
229
230
  /* Do we have a mapping to unpack? */
231
1.41k
  switch((c->maptype=(signed char)oggpack_read(opb,4))){
232
44
  case 0:
233
    /* no mapping */
234
44
    break;
235
1.36k
  case 1: case 2:
236
    /* implicitly populated value mapping */
237
    /* explicitly populated value mapping */
238
239
1.36k
    c->q_min=(ogg_uint32_t)oggpack_read(opb,32);
240
1.36k
    c->q_delta=(ogg_uint32_t)oggpack_read(opb,32);
241
1.36k
    c->q_quant=(signed char)oggpack_read(opb,4)+1;
242
1.36k
    c->q_sequencep=(signed char)oggpack_read(opb,1);
243
1.36k
    if(c->q_sequencep==-1)goto _eofout;
244
245
1.35k
    {
246
1.35k
      long q=0;
247
1.35k
      int quantvals=0;
248
1.35k
      switch(c->maptype){
249
1.32k
      case 1:
250
1.32k
        quantvals=(c->dim==0?0:_book_maptype1_quantvals(c->dim, c->entries));
251
1.32k
        break;
252
33
      case 2:
253
33
        quantvals=c->entries*c->dim;
254
33
        break;
255
1.35k
      }
256
257
      /* quantized values */
258
1.35k
      if(((quantvals*c->q_quant+7)>>3)>opb->storage-oggpack_bytes(opb))
259
3
        goto _eofout;
260
1.35k
      c->quantlist=_ogg_malloc(sizeof(*c->quantlist)*quantvals);
261
1.35k
      if(c->quantlist==NULL)goto _errout;
262
12.3k
      for(i=0;i<quantvals;i++){
263
11.0k
        q=oggpack_read(opb,c->q_quant);
264
11.0k
        c->quantlist[i]=(ogg_uint16_t)q;
265
11.0k
      }
266
267
1.35k
      if(q==-1)goto _eofout;
268
1.35k
    }
269
1.35k
    break;
270
1.35k
  default:
271
14
    goto _errout;
272
1.41k
  }
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.40k
  return(0);
280
281
79
 _errout:
282
172
 _eofout:
283
  /* our caller will clean up the partially constructed book */
284
172
  return(-1);
285
79
}
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.57M
STIN long decode_packed_entry_number(dec_codebook *book, oggpack_buffer *b){
311
3.57M
  ogg_uint32_t testword;
312
3.57M
  int  read=book->maxlength;
313
3.57M
  long lo,hi;
314
3.57M
  long lok = oggpack_look(b,book->firsttablen);
315
316
3.57M
  if (lok >= 0) {
317
3.53M
    long entry = book->firsttable[lok];
318
3.53M
    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.53M
    }else{
322
3.53M
      oggpack_adv(b, (int)(entry&0x3f));
323
3.53M
      return(entry>>6);
324
3.53M
    }
325
3.53M
  }else{
326
44.0k
    lo=0;
327
44.0k
    hi=book->hi_max;
328
44.0k
  }
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
44.0k
  lok = oggpack_look(b, read);
337
338
44.0k
  while(lok<0 && read>1)
339
0
    lok = oggpack_look(b, --read);
340
44.0k
  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
762k
long vorbis_book_decode(dec_codebook *book, oggpack_buffer *b){
390
762k
  if(book->codelist){
391
762k
    long packed_entry=decode_packed_entry_number(book,b);
392
762k
    if(packed_entry>=0){
393
      /* if we have an unordered book, look up the original entry number */
394
723k
      if(book->codelengths)
395
676k
        return(book->index[packed_entry]);
396
      /* if we have an ordered book, the packed_entry number is the original */
397
46.4k
      return(packed_entry);
398
723k
    }
399
762k
  }
400
401
  /* if there's no codelist, the codebook hasn't been init'd */
402
39.6k
  return(-1);
403
762k
}
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
694k
                              int n){
409
694k
  if(book->codelist){
410
694k
    int step=n/book->dim;
411
694k
    long *entry = alloca(sizeof(*entry)*step);
412
694k
    float **t = alloca(sizeof(*t)*step);
413
694k
    int i,j,o;
414
415
1.41M
    for (i = 0; i < step; i++) {
416
725k
      entry[i]=decode_packed_entry_number(book,b);
417
725k
      if(entry[i]==-1)return(-1);
418
723k
      t[i] = book->valuelist+entry[i]*book->dim;
419
723k
    }
420
15.7M
    for(i=0,o=0;i<book->dim;i++,o+=step)
421
15.9M
      for (j=0;o+j<n && j<step;j++)
422
877k
        a[o+j]+=t[j][i];
423
692k
  }
424
692k
  return(0);
425
694k
}
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.9k
                             int n){
430
88.9k
  if(book->codelist){
431
88.9k
    int i,j,entry;
432
88.9k
    float *t;
433
434
352k
    for(i=0;i<n;){
435
264k
      entry = decode_packed_entry_number(book,b);
436
264k
      if(entry==-1)return(-1);
437
263k
      t     = book->valuelist+entry*book->dim;
438
870k
      for(j=0;i<n && j<book->dim;)
439
606k
        a[i++]+=t[j++];
440
263k
    }
441
88.9k
  }
442
87.9k
  return(0);
443
88.9k
}
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
17.0k
                             int n){
450
17.0k
  if(book->codelist){
451
17.0k
    int i,j,entry;
452
17.0k
    float *t;
453
454
161k
    for(i=0;i<n;){
455
144k
      entry = decode_packed_entry_number(book,b);
456
144k
      if(entry==-1)return(-1);
457
144k
      t     = book->valuelist+entry*book->dim;
458
792k
      for (j=0;i<n && j<book->dim;){
459
647k
        a[i++]=t[j++];
460
647k
      }
461
144k
    }
462
17.0k
  }else{
463
0
    int i;
464
465
0
    for(i=0;i<n;){
466
0
      a[i++]=0.f;
467
0
    }
468
0
  }
469
16.7k
  return(0);
470
17.0k
}
471
472
long vorbis_book_decodevv_add(dec_codebook *book,float **a,long offset,int ch,
473
192k
                              oggpack_buffer *b,int n){
474
475
192k
  long i,j,entry;
476
192k
  int chptr=0;
477
192k
  if(book->codelist){
478
192k
    int m=(offset+n)/ch;
479
1.86M
    for(i=offset/ch;i<m;){
480
1.67M
      entry = decode_packed_entry_number(book,b);
481
1.67M
      if(entry==-1)return(-1);
482
1.67M
      {
483
1.67M
        const float *t = book->valuelist+entry*book->dim;
484
5.61M
        for (j=0;i<m && j<book->dim;j++){
485
3.93M
          a[chptr++][i]+=t[j];
486
3.93M
          if(chptr==ch){
487
89.8k
            chptr=0;
488
89.8k
            i++;
489
89.8k
          }
490
3.93M
        }
491
1.67M
      }
492
1.67M
    }
493
192k
  }
494
191k
  return(0);
495
192k
}