Coverage Report

Created: 2026-07-25 07:52

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/vorbis/lib/res0.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-2010             *
9
 * by the Xiph.Org Foundation https://xiph.org/                     *
10
 *                                                                  *
11
 ********************************************************************
12
13
 function: residue backend 0, 1 and 2 implementation
14
15
 ********************************************************************/
16
17
/* Slow, slow, slow, simpleminded and did I mention it was slow?  The
18
   encode/decode loops are coded for clarity and performance is not
19
   yet even a nagging little idea lurking in the shadows.  Oh and BTW,
20
   it's slow. */
21
22
#include <stdlib.h>
23
#include <string.h>
24
#include <math.h>
25
#include <ogg/ogg.h>
26
#include "vorbis/codec.h"
27
#include "codec_internal.h"
28
#include "registry.h"
29
#include "codebook.h"
30
#include "misc.h"
31
#include "os.h"
32
33
#if defined(TRAIN_RES) || defined (TRAIN_RESAUX)
34
#include <stdio.h>
35
#endif
36
37
typedef struct {
38
  vorbis_info_residue0 *info;
39
40
  int            parts;
41
  int            stages;
42
  codebook      *fullbooks;
43
  dec_codebook  *decbooks;
44
  int            phrasebook;
45
  int          **partbooks;
46
47
  int            partvals;
48
  int          **decodemap;
49
50
  long           postbits;
51
  long           phrasebits;
52
  long           frames;
53
54
#if defined(TRAIN_RES) || defined(TRAIN_RESAUX)
55
  int            train_seq;
56
  long          *training_data[8][64];
57
  float          training_max[8][64];
58
  float          training_min[8][64];
59
  float          tmin;
60
  float          tmax;
61
  int            submap;
62
#endif
63
64
} vorbis_look_residue0;
65
66
2.18k
void res0_free_info(vorbis_info_residue *i){
67
2.18k
  vorbis_info_residue0 *info=(vorbis_info_residue0 *)i;
68
2.18k
  if(info){
69
2.18k
    memset(info,0,sizeof(*info));
70
2.18k
    _ogg_free(info);
71
2.18k
  }
72
2.18k
}
73
74
1.71k
void res0_free_look(vorbis_look_residue *i){
75
1.71k
  int j;
76
1.71k
  if(i){
77
78
1.71k
    vorbis_look_residue0 *look=(vorbis_look_residue0 *)i;
79
80
#ifdef TRAIN_RES
81
    {
82
      int j,k,l;
83
      for(j=0;j<look->parts;j++){
84
        /*fprintf(stderr,"partition %d: ",j);*/
85
        for(k=0;k<8;k++)
86
          if(look->training_data[k][j]){
87
            char buffer[80];
88
            FILE *of;
89
            codebook *statebook=look->fullbooks+look->partbooks[j][k];
90
91
            /* long and short into the same bucket by current convention */
92
            sprintf(buffer,"res_sub%d_part%d_pass%d.vqd",look->submap,j,k);
93
            of=fopen(buffer,"a");
94
95
            for(l=0;l<statebook->entries;l++)
96
              fprintf(of,"%d:%ld\n",l,look->training_data[k][j][l]);
97
98
            fclose(of);
99
100
            /*fprintf(stderr,"%d(%.2f|%.2f) ",k,
101
              look->training_min[k][j],look->training_max[k][j]);*/
102
103
            _ogg_free(look->training_data[k][j]);
104
            look->training_data[k][j]=NULL;
105
          }
106
        /*fprintf(stderr,"\n");*/
107
      }
108
    }
109
    fprintf(stderr,"min/max residue: %g::%g\n",look->tmin,look->tmax);
110
111
    /*fprintf(stderr,"residue bit usage %f:%f (%f total)\n",
112
            (float)look->phrasebits/look->frames,
113
            (float)look->postbits/look->frames,
114
            (float)(look->postbits+look->phrasebits)/look->frames);*/
115
#endif
116
117
118
    /*vorbis_info_residue0 *info=look->info;
119
120
    fprintf(stderr,
121
            "%ld frames encoded in %ld phrasebits and %ld residue bits "
122
            "(%g/frame) \n",look->frames,look->phrasebits,
123
            look->resbitsflat,
124
            (look->phrasebits+look->resbitsflat)/(float)look->frames);
125
126
    for(j=0;j<look->parts;j++){
127
      long acc=0;
128
      fprintf(stderr,"\t[%d] == ",j);
129
      for(k=0;k<look->stages;k++)
130
        if((info->secondstages[j]>>k)&1){
131
          fprintf(stderr,"%ld,",look->resbits[j][k]);
132
          acc+=look->resbits[j][k];
133
        }
134
135
      fprintf(stderr,":: (%ld vals) %1.2fbits/sample\n",look->resvals[j],
136
              acc?(float)acc/(look->resvals[j]*info->grouping):0);
137
    }
138
    fprintf(stderr,"\n");*/
139
140
3.42k
    for(j=0;j<look->parts;j++)
141
1.71k
      if(look->partbooks[j])_ogg_free(look->partbooks[j]);
142
1.71k
    _ogg_free(look->partbooks);
143
3.42k
    for(j=0;j<look->partvals;j++)
144
1.71k
      _ogg_free(look->decodemap[j]);
145
1.71k
    _ogg_free(look->decodemap);
146
147
1.71k
    memset(look,0,sizeof(*look));
148
1.71k
    _ogg_free(look);
149
1.71k
  }
150
1.71k
}
151
152
3.38k
static int icount(unsigned int v){
153
3.38k
  int ret=0;
154
10.2k
  while(v){
155
6.91k
    ret+=v&1;
156
6.91k
    v>>=1;
157
6.91k
  }
158
3.38k
  return(ret);
159
3.38k
}
160
161
162
0
void res0_pack(vorbis_info_residue *vr,oggpack_buffer *opb){
163
0
  vorbis_info_residue0 *info=(vorbis_info_residue0 *)vr;
164
0
  int j,acc=0;
165
0
  oggpack_write(opb,info->begin,24);
166
0
  oggpack_write(opb,info->end,24);
167
168
0
  oggpack_write(opb,info->grouping-1,24);  /* residue vectors to group and
169
                                             code with a partitioned book */
170
0
  oggpack_write(opb,info->partitions-1,6); /* possible partition choices */
171
0
  oggpack_write(opb,info->groupbook,8);  /* group huffman book */
172
173
  /* secondstages is a bitmask; as encoding progresses pass by pass, a
174
     bitmask of one indicates this partition class has bits to write
175
     this pass */
176
0
  for(j=0;j<info->partitions;j++){
177
0
    if(ov_ilog(info->secondstages[j])>3){
178
      /* yes, this is a minor hack due to not thinking ahead */
179
0
      oggpack_write(opb,info->secondstages[j],3);
180
0
      oggpack_write(opb,1,1);
181
0
      oggpack_write(opb,info->secondstages[j]>>3,5);
182
0
    }else
183
0
      oggpack_write(opb,info->secondstages[j],4); /* trailing zero */
184
0
    acc+=icount(info->secondstages[j]);
185
0
  }
186
0
  for(j=0;j<acc;j++)
187
0
    oggpack_write(opb,info->booklist[j],8);
188
189
0
}
190
191
/* vorbis_info is for range checking */
192
2.18k
vorbis_info_residue *res0_unpack(vorbis_info *vi,oggpack_buffer *opb){
193
2.18k
  int j,acc=0;
194
2.18k
  vorbis_info_residue0 *info=_ogg_calloc(1,sizeof(*info));
195
2.18k
  codec_setup_info     *ci=vi->codec_setup;
196
197
2.18k
  info->begin=oggpack_read(opb,24);
198
2.18k
  info->end=oggpack_read(opb,24);
199
2.18k
  info->grouping=oggpack_read(opb,24)+1;
200
2.18k
  info->partitions=oggpack_read(opb,6)+1;
201
2.18k
  info->groupbook=oggpack_read(opb,8);
202
203
  /* check for premature EOP */
204
2.18k
  if(info->groupbook<0)goto errout;
205
206
5.56k
  for(j=0;j<info->partitions;j++){
207
3.38k
    int cascade=oggpack_read(opb,3);
208
3.38k
    int cflag=oggpack_read(opb,1);
209
3.38k
    if(cflag<0) goto errout;
210
3.38k
    if(cflag){
211
1.42k
      int c=oggpack_read(opb,5);
212
1.42k
      if(c<0) goto errout;
213
1.42k
      cascade|=(c<<3);
214
1.42k
    }
215
3.38k
    info->secondstages[j]=cascade;
216
217
3.38k
    acc+=icount(cascade);
218
3.38k
  }
219
5.32k
  for(j=0;j<acc;j++){
220
3.14k
    int book=oggpack_read(opb,8);
221
3.14k
    if(book<0) goto errout;
222
3.14k
    info->booklist[j]=book;
223
3.14k
  }
224
225
2.18k
  if(info->groupbook>=ci->books)goto errout;
226
3.69k
  for(j=0;j<acc;j++){
227
1.54k
    if(info->booklist[j]>=ci->books)goto errout;
228
1.53k
    if(ci->decbooks[info->booklist[j]].maptype==0)goto errout;
229
1.53k
  }
230
231
  /* verify the phrasebook is not specifying an impossible or
232
     inconsistent partitioning scheme. */
233
  /* modify the phrasebook ranging check from r16327; an early beta
234
     encoder had a bug where it used an oversized phrasebook by
235
     accident.  These files should continue to be playable, but don't
236
     allow an exploit */
237
2.14k
  {
238
2.14k
    int entries = ci->decbooks[info->groupbook].entries;
239
2.14k
    int dim = ci->decbooks[info->groupbook].dim;
240
2.14k
    int partvals = 1;
241
2.14k
    if (dim<1) goto errout;
242
28.5k
    while(dim>0){
243
26.3k
      partvals *= info->partitions;
244
26.3k
      if(partvals > entries) goto errout;
245
26.3k
      dim--;
246
26.3k
    }
247
2.13k
    info->partvals = partvals;
248
2.13k
  }
249
250
0
  return(info);
251
50
 errout:
252
50
  res0_free_info(info);
253
50
  return(NULL);
254
2.14k
}
255
256
vorbis_look_residue *res0_look(vorbis_dsp_state *vd,
257
1.71k
                               vorbis_info_residue *vr){
258
1.71k
  vorbis_info_residue0 *info=(vorbis_info_residue0 *)vr;
259
1.71k
  vorbis_look_residue0 *look=_ogg_calloc(1,sizeof(*look));
260
1.71k
  codec_setup_info     *ci=vd->vi->codec_setup;
261
262
1.71k
  int j,k,acc=0;
263
1.71k
  int dim;
264
1.71k
  int maxstage=0;
265
1.71k
  look->info=info;
266
267
1.71k
  look->parts=info->partitions;
268
1.71k
  look->fullbooks=ci->fullbooks;
269
1.71k
  look->decbooks=ci->decbooks;
270
1.71k
  look->phrasebook=info->groupbook;
271
1.71k
  dim=look->fullbooks?
272
1.71k
   look->fullbooks[look->phrasebook].dim:look->decbooks[look->phrasebook].dim;
273
274
1.71k
  look->partbooks=_ogg_calloc(look->parts,sizeof(*look->partbooks));
275
276
3.42k
  for(j=0;j<look->parts;j++){
277
1.71k
    int stages=ov_ilog(info->secondstages[j]);
278
1.71k
    if(stages){
279
1.03k
      if(stages>maxstage)maxstage=stages;
280
1.03k
      look->partbooks[j]=_ogg_calloc(stages,sizeof(*look->partbooks[j]));
281
4.07k
      for(k=0;k<stages;k++)
282
3.04k
        if(info->secondstages[j]&(1<<k)){
283
1.20k
          look->partbooks[j][k]=info->booklist[acc++];
284
#ifdef TRAIN_RES
285
          look->training_data[k][j]=_ogg_calloc(
286
           look->fullbooks[look->partbooks[j][k]].entries,
287
           sizeof(***look->training_data));
288
#endif
289
1.83k
        }else{
290
1.83k
          look->partbooks[j][k]=-1;
291
1.83k
        }
292
1.03k
    }
293
1.71k
  }
294
295
1.71k
  look->partvals=1;
296
19.3k
  for(j=0;j<dim;j++)
297
17.6k
      look->partvals*=look->parts;
298
299
1.71k
  look->stages=maxstage;
300
1.71k
  look->decodemap=_ogg_malloc(look->partvals*sizeof(*look->decodemap));
301
3.42k
  for(j=0;j<look->partvals;j++){
302
1.71k
    long val=j;
303
1.71k
    long mult=look->partvals/look->parts;
304
1.71k
    look->decodemap[j]=_ogg_malloc(dim*sizeof(*look->decodemap[j]));
305
19.3k
    for(k=0;k<dim;k++){
306
17.6k
      long deco=val/mult;
307
17.6k
      val-=deco*mult;
308
17.6k
      mult/=look->parts;
309
17.6k
      look->decodemap[j][k]=deco;
310
17.6k
    }
311
1.71k
  }
312
#if defined(TRAIN_RES) || defined (TRAIN_RESAUX)
313
  {
314
    static int train_seq=0;
315
    look->train_seq=train_seq++;
316
  }
317
#endif
318
1.71k
  return(look);
319
1.71k
}
320
321
/* break an abstraction and copy some code for performance purposes */
322
0
static int local_book_besterror(codebook *book,int *a){
323
0
  int dim=book->dim;
324
0
  int i,j,o;
325
0
  int minval=book->minval;
326
0
  int del=book->delta;
327
0
  int qv=book->quantvals;
328
0
  int ze=(qv>>1);
329
0
  int index=0;
330
  /* assumes integer/centered encoder codebook maptype 1 no more than dim 8 */
331
0
  int p[8]={0,0,0,0,0,0,0,0};
332
333
0
  if(del!=1){
334
0
    for(i=0,o=dim;i<dim;i++){
335
0
      int v = (a[--o]-minval+(del>>1))/del;
336
0
      int m = (v<ze ? ((ze-v)<<1)-1 : ((v-ze)<<1));
337
0
      index = index*qv+ (m<0?0:(m>=qv?qv-1:m));
338
0
      p[o]=v*del+minval;
339
0
    }
340
0
  }else{
341
0
    for(i=0,o=dim;i<dim;i++){
342
0
      int v = a[--o]-minval;
343
0
      int m = (v<ze ? ((ze-v)<<1)-1 : ((v-ze)<<1));
344
0
      index = index*qv+ (m<0?0:(m>=qv?qv-1:m));
345
0
      p[o]=v*del+minval;
346
0
    }
347
0
  }
348
349
0
  if(book->c->lengthlist[index]<=0){
350
0
    const static_codebook *c=book->c;
351
0
    int best=-1;
352
    /* assumes integer/centered encoder codebook maptype 1 no more than dim 8 */
353
0
    int e[8]={0,0,0,0,0,0,0,0};
354
0
    int maxval = book->minval + book->delta*(book->quantvals-1);
355
0
    for(i=0;i<book->entries;i++){
356
0
      if(c->lengthlist[i]>0){
357
0
        int this=0;
358
0
        for(j=0;j<dim;j++){
359
0
          int val=(e[j]-a[j]);
360
0
          this+=val*val;
361
0
        }
362
0
        if(best==-1 || this<best){
363
0
          memcpy(p,e,sizeof(p));
364
0
          best=this;
365
0
          index=i;
366
0
        }
367
0
      }
368
      /* assumes the value patterning created by the tools in vq/ */
369
0
      j=0;
370
0
      while(e[j]>=maxval)
371
0
        e[j++]=0;
372
0
      if(e[j]>=0)
373
0
        e[j]+=book->delta;
374
0
      e[j]= -e[j];
375
0
    }
376
0
  }
377
378
0
  if(index>-1){
379
0
    for(i=0;i<dim;i++)
380
0
      *a++ -= p[i];
381
0
  }
382
383
0
  return(index);
384
0
}
385
386
#ifdef TRAIN_RES
387
static int _encodepart(oggpack_buffer *opb,int *vec, int n,
388
                       codebook *book,long *acc){
389
#else
390
static int _encodepart(oggpack_buffer *opb,int *vec, int n,
391
0
                       codebook *book){
392
0
#endif
393
0
  int i,bits=0;
394
0
  int dim=book->dim;
395
0
  int step=n/dim;
396
397
0
  for(i=0;i<step;i++){
398
0
    int entry=local_book_besterror(book,vec+i*dim);
399
400
#ifdef TRAIN_RES
401
    if(entry>=0)
402
      acc[entry]++;
403
#endif
404
405
0
    bits+=vorbis_book_encode(book,entry,opb);
406
407
0
  }
408
409
0
  return(bits);
410
0
}
411
412
static long **_01class(vorbis_block *vb,vorbis_look_residue *vl,
413
0
                       int **in,int ch){
414
0
  long i,j,k;
415
0
  vorbis_look_residue0 *look=(vorbis_look_residue0 *)vl;
416
0
  vorbis_info_residue0 *info=look->info;
417
418
  /* move all this setup out later */
419
0
  int samples_per_partition=info->grouping;
420
0
  int possible_partitions=info->partitions;
421
0
  int n=info->end-info->begin;
422
423
0
  int partvals=n/samples_per_partition;
424
0
  long **partword=_vorbis_block_alloc(vb,ch*sizeof(*partword));
425
0
  float scale=100./samples_per_partition;
426
427
  /* we find the partition type for each partition of each
428
     channel.  We'll go back and do the interleaved encoding in a
429
     bit.  For now, clarity */
430
431
0
  for(i=0;i<ch;i++){
432
0
    partword[i]=_vorbis_block_alloc(vb,n/samples_per_partition*sizeof(*partword[i]));
433
0
    memset(partword[i],0,n/samples_per_partition*sizeof(*partword[i]));
434
0
  }
435
436
0
  for(i=0;i<partvals;i++){
437
0
    int offset=i*samples_per_partition+info->begin;
438
0
    for(j=0;j<ch;j++){
439
0
      int max=0;
440
0
      int ent=0;
441
0
      for(k=0;k<samples_per_partition;k++){
442
0
        if(abs(in[j][offset+k])>max)max=abs(in[j][offset+k]);
443
0
        ent+=abs(in[j][offset+k]);
444
0
      }
445
0
      ent*=scale;
446
447
0
      for(k=0;k<possible_partitions-1;k++)
448
0
        if(max<=info->classmetric1[k] &&
449
0
           (info->classmetric2[k]<0 || ent<info->classmetric2[k]))
450
0
          break;
451
452
0
      partword[j][i]=k;
453
0
    }
454
0
  }
455
456
#ifdef TRAIN_RESAUX
457
  {
458
    FILE *of;
459
    char buffer[80];
460
461
    for(i=0;i<ch;i++){
462
      sprintf(buffer,"resaux_%d.vqd",look->train_seq);
463
      of=fopen(buffer,"a");
464
      for(j=0;j<partvals;j++)
465
        fprintf(of,"%ld, ",partword[i][j]);
466
      fprintf(of,"\n");
467
      fclose(of);
468
    }
469
  }
470
#endif
471
0
  look->frames++;
472
473
0
  return(partword);
474
0
}
475
476
/* designed for stereo or other modes where the partition size is an
477
   integer multiple of the number of channels encoded in the current
478
   submap */
479
static long **_2class(vorbis_block *vb,vorbis_look_residue *vl,int **in,
480
0
                      int ch){
481
0
  long i,j,k,l;
482
0
  vorbis_look_residue0 *look=(vorbis_look_residue0 *)vl;
483
0
  vorbis_info_residue0 *info=look->info;
484
485
  /* move all this setup out later */
486
0
  int samples_per_partition=info->grouping;
487
0
  int possible_partitions=info->partitions;
488
0
  int n=info->end-info->begin;
489
490
0
  int partvals=n/samples_per_partition;
491
0
  long **partword=_vorbis_block_alloc(vb,sizeof(*partword));
492
493
#if defined(TRAIN_RES) || defined (TRAIN_RESAUX)
494
  FILE *of;
495
  char buffer[80];
496
#endif
497
498
0
  partword[0]=_vorbis_block_alloc(vb,partvals*sizeof(*partword[0]));
499
0
  memset(partword[0],0,partvals*sizeof(*partword[0]));
500
501
0
  for(i=0,l=info->begin/ch;i<partvals;i++){
502
0
    int magmax=0;
503
0
    int angmax=0;
504
0
    for(j=0;j<samples_per_partition;j+=ch){
505
0
      if(abs(in[0][l])>magmax)magmax=abs(in[0][l]);
506
0
      for(k=1;k<ch;k++)
507
0
        if(abs(in[k][l])>angmax)angmax=abs(in[k][l]);
508
0
      l++;
509
0
    }
510
511
0
    for(j=0;j<possible_partitions-1;j++)
512
0
      if(magmax<=info->classmetric1[j] &&
513
0
         angmax<=info->classmetric2[j])
514
0
        break;
515
516
0
    partword[0][i]=j;
517
518
0
  }
519
520
#ifdef TRAIN_RESAUX
521
  sprintf(buffer,"resaux_%d.vqd",look->train_seq);
522
  of=fopen(buffer,"a");
523
  for(i=0;i<partvals;i++)
524
    fprintf(of,"%ld, ",partword[0][i]);
525
  fprintf(of,"\n");
526
  fclose(of);
527
#endif
528
529
0
  look->frames++;
530
531
0
  return(partword);
532
0
}
533
534
static int _01forward(oggpack_buffer *opb,
535
                      vorbis_look_residue *vl,
536
                      int **in,int ch,
537
                      long **partword,
538
#ifdef TRAIN_RES
539
                      int (*encode)(oggpack_buffer *,int *,int,
540
                                    codebook *,long *),
541
                      int submap
542
#else
543
                      int (*encode)(oggpack_buffer *,int *,int,
544
                                    codebook *)
545
#endif
546
0
){
547
0
  long i,j,k,s;
548
0
  vorbis_look_residue0 *look=(vorbis_look_residue0 *)vl;
549
0
  vorbis_info_residue0 *info=look->info;
550
0
  codebook *phrasebook=look->fullbooks+look->phrasebook;
551
552
#ifdef TRAIN_RES
553
  look->submap=submap;
554
#endif
555
556
  /* move all this setup out later */
557
0
  int samples_per_partition=info->grouping;
558
0
  int possible_partitions=info->partitions;
559
0
  int partitions_per_word=phrasebook->dim;
560
0
  int n=info->end-info->begin;
561
562
0
  int partvals=n/samples_per_partition;
563
0
  long resbits[128];
564
0
  long resvals[128];
565
566
#ifdef TRAIN_RES
567
  for(i=0;i<ch;i++)
568
    for(j=info->begin;j<info->end;j++){
569
      if(in[i][j]>look->tmax)look->tmax=in[i][j];
570
      if(in[i][j]<look->tmin)look->tmin=in[i][j];
571
    }
572
#endif
573
574
0
  memset(resbits,0,sizeof(resbits));
575
0
  memset(resvals,0,sizeof(resvals));
576
577
  /* we code the partition words for each channel, then the residual
578
     words for a partition per channel until we've written all the
579
     residual words for that partition word.  Then write the next
580
     partition channel words... */
581
582
0
  for(s=0;s<look->stages;s++){
583
584
0
    for(i=0;i<partvals;){
585
586
      /* first we encode a partition codeword for each channel */
587
0
      if(s==0){
588
0
        for(j=0;j<ch;j++){
589
0
          long val=partword[j][i];
590
0
          for(k=1;k<partitions_per_word;k++){
591
0
            val*=possible_partitions;
592
0
            if(i+k<partvals)
593
0
              val+=partword[j][i+k];
594
0
          }
595
596
          /* training hack */
597
0
          if(val<phrasebook->entries)
598
0
            look->phrasebits+=vorbis_book_encode(phrasebook,val,opb);
599
#if 0 /*def TRAIN_RES*/
600
          else
601
            fprintf(stderr,"!");
602
#endif
603
604
0
        }
605
0
      }
606
607
      /* now we encode interleaved residual values for the partitions */
608
0
      for(k=0;k<partitions_per_word && i<partvals;k++,i++){
609
0
        long offset=i*samples_per_partition+info->begin;
610
611
0
        for(j=0;j<ch;j++){
612
0
          if(s==0)resvals[partword[j][i]]+=samples_per_partition;
613
0
          if(info->secondstages[partword[j][i]]&(1<<s)){
614
0
            int statebooknum=look->partbooks[partword[j][i]][s];
615
0
            if(statebooknum>=0){
616
0
              codebook *statebook=look->fullbooks+statebooknum;
617
0
              int ret;
618
#ifdef TRAIN_RES
619
              long *accumulator=NULL;
620
              accumulator=look->training_data[s][partword[j][i]];
621
              {
622
                int l;
623
                int *samples=in[j]+offset;
624
                for(l=0;l<samples_per_partition;l++){
625
                  if(samples[l]<look->training_min[s][partword[j][i]])
626
                    look->training_min[s][partword[j][i]]=samples[l];
627
                  if(samples[l]>look->training_max[s][partword[j][i]])
628
                    look->training_max[s][partword[j][i]]=samples[l];
629
                }
630
              }
631
              ret=encode(opb,in[j]+offset,samples_per_partition,
632
                         statebook,accumulator);
633
#else
634
0
              ret=encode(opb,in[j]+offset,samples_per_partition,
635
0
                         statebook);
636
0
#endif
637
638
0
              look->postbits+=ret;
639
0
              resbits[partword[j][i]]+=ret;
640
0
            }
641
0
          }
642
0
        }
643
0
      }
644
0
    }
645
0
  }
646
647
0
  return(0);
648
0
}
649
650
/* a truncated packet here just means 'stop working'; it's not an error */
651
static int _01inverse(vorbis_block *vb,vorbis_look_residue *vl,
652
                      float **in,int ch,
653
                      long (*decodepart)(dec_codebook *, float *,
654
59.1k
                                         oggpack_buffer *,int)){
655
656
59.1k
  long i,j,k,l,s;
657
59.1k
  vorbis_look_residue0 *look=(vorbis_look_residue0 *)vl;
658
59.1k
  vorbis_info_residue0 *info=look->info;
659
59.1k
  dec_codebook *phrasebook=look->decbooks+look->phrasebook;
660
661
  /* move all this setup out later */
662
59.1k
  int samples_per_partition=info->grouping;
663
59.1k
  int partitions_per_word=phrasebook->dim;
664
59.1k
  int max=vb->pcmend>>1;
665
59.1k
  int end=(info->end<max?info->end:max);
666
59.1k
  int n=end-info->begin;
667
668
59.1k
  if(n>0){
669
58.1k
    int partvals=n/samples_per_partition;
670
58.1k
    int partwords=(partvals+partitions_per_word-1)/partitions_per_word;
671
58.1k
    int ***partword=alloca(ch*sizeof(*partword));
672
673
145k
    for(j=0;j<ch;j++)
674
87.4k
      partword[j]=_vorbis_block_alloc(vb,partwords*sizeof(*partword[j]));
675
676
87.0k
    for(s=0;s<look->stages;s++){
677
678
      /* each loop decodes on partition codeword containing
679
         partitions_per_word partitions */
680
690k
      for(i=0,l=0;i<partvals;l++){
681
661k
        if(s==0){
682
          /* fetch the partition word for each channel */
683
995k
          for(j=0;j<ch;j++){
684
696k
            int temp=vorbis_book_decode(phrasebook,&vb->opb);
685
686
696k
            if(temp==-1 || temp>=info->partvals)goto eopbreak;
687
648k
            partword[j][l]=look->decodemap[temp];
688
648k
            if(partword[j][l]==NULL)goto errout;
689
648k
          }
690
347k
        }
691
692
        /* now we decode residual values for the partitions */
693
1.48M
        for(k=0;k<partitions_per_word && i<partvals;k++,i++)
694
2.80M
          for(j=0;j<ch;j++){
695
1.92M
            long offset=info->begin+i*samples_per_partition;
696
1.92M
            if(info->secondstages[partword[j][l][k]]&(1<<s)){
697
562k
              int stagebooknum=look->partbooks[partword[j][l][k]][s];
698
562k
              if(stagebooknum>=0){
699
562k
                dec_codebook *stagebook=look->decbooks+stagebooknum;
700
562k
                if(decodepart(stagebook,in[j]+offset,&vb->opb,
701
562k
                              samples_per_partition)==-1)goto eopbreak;
702
562k
              }
703
562k
            }
704
1.92M
          }
705
613k
      }
706
81.6k
    }
707
58.1k
  }
708
6.44k
 errout:
709
59.1k
 eopbreak:
710
59.1k
  return(0);
711
6.44k
}
712
713
int res0_inverse(vorbis_block *vb,vorbis_look_residue *vl,
714
87.0k
                 float **in,int *nonzero,int ch){
715
87.0k
  int i,used=0;
716
1.08M
  for(i=0;i<ch;i++)
717
995k
    if(nonzero[i])
718
80.4k
      in[used++]=in[i];
719
87.0k
  if(used)
720
51.3k
    return(_01inverse(vb,vl,in,used,vorbis_book_decodevs_add));
721
35.7k
  else
722
35.7k
    return(0);
723
87.0k
}
724
725
int res1_forward(oggpack_buffer *opb,vorbis_block *vb,vorbis_look_residue *vl,
726
0
                 int **in,int *nonzero,int ch, long **partword, int submap){
727
0
  int i,used=0;
728
0
  (void)vb;
729
0
  for(i=0;i<ch;i++)
730
0
    if(nonzero[i])
731
0
      in[used++]=in[i];
732
733
0
  if(used){
734
#ifdef TRAIN_RES
735
    return _01forward(opb,vl,in,used,partword,_encodepart,submap);
736
#else
737
0
    (void)submap;
738
0
    return _01forward(opb,vl,in,used,partword,_encodepart);
739
0
#endif
740
0
  }else{
741
0
    return(0);
742
0
  }
743
0
}
744
745
long **res1_class(vorbis_block *vb,vorbis_look_residue *vl,
746
0
                  int **in,int *nonzero,int ch){
747
0
  int i,used=0;
748
0
  for(i=0;i<ch;i++)
749
0
    if(nonzero[i])
750
0
      in[used++]=in[i];
751
0
  if(used)
752
0
    return(_01class(vb,vl,in,used));
753
0
  else
754
0
    return(0);
755
0
}
756
757
int res1_inverse(vorbis_block *vb,vorbis_look_residue *vl,
758
13.9k
                 float **in,int *nonzero,int ch){
759
13.9k
  int i,used=0;
760
96.0k
  for(i=0;i<ch;i++)
761
82.1k
    if(nonzero[i])
762
13.6k
      in[used++]=in[i];
763
13.9k
  if(used)
764
7.88k
    return(_01inverse(vb,vl,in,used,vorbis_book_decodev_add));
765
6.05k
  else
766
6.05k
    return(0);
767
13.9k
}
768
769
long **res2_class(vorbis_block *vb,vorbis_look_residue *vl,
770
0
                  int **in,int *nonzero,int ch){
771
0
  int i,used=0;
772
0
  for(i=0;i<ch;i++)
773
0
    if(nonzero[i])used++;
774
0
  if(used)
775
0
    return(_2class(vb,vl,in,ch));
776
0
  else
777
0
    return(0);
778
0
}
779
780
/* res2 is slightly more different; all the channels are interleaved
781
   into a single vector and encoded. */
782
783
int res2_forward(oggpack_buffer *opb,
784
                 vorbis_block *vb,vorbis_look_residue *vl,
785
0
                 int **in,int *nonzero,int ch, long **partword,int submap){
786
0
  long i,j,k,n=vb->pcmend/2,used=0;
787
788
  /* don't duplicate the code; use a working vector hack for now and
789
     reshape ourselves into a single channel res1 */
790
  /* ugly; reallocs for each coupling pass :-( */
791
0
  int *work=_vorbis_block_alloc(vb,ch*n*sizeof(*work));
792
0
  for(i=0;i<ch;i++){
793
0
    int *pcm=in[i];
794
0
    if(nonzero[i])used++;
795
0
    for(j=0,k=i;j<n;j++,k+=ch)
796
0
      work[k]=pcm[j];
797
0
  }
798
799
0
  if(used){
800
#ifdef TRAIN_RES
801
    return _01forward(opb,vl,&work,1,partword,_encodepart,submap);
802
#else
803
0
    (void)submap;
804
0
    return _01forward(opb,vl,&work,1,partword,_encodepart);
805
0
#endif
806
0
  }else{
807
0
    return(0);
808
0
  }
809
0
}
810
811
/* duplicate code here as speed is somewhat more important */
812
int res2_inverse(vorbis_block *vb,vorbis_look_residue *vl,
813
16.2k
                 float **in,int *nonzero,int ch){
814
16.2k
  long i,k,l,s;
815
16.2k
  vorbis_look_residue0 *look=(vorbis_look_residue0 *)vl;
816
16.2k
  vorbis_info_residue0 *info=look->info;
817
16.2k
  dec_codebook *phrasebook=look->decbooks+look->phrasebook;
818
819
  /* move all this setup out later */
820
16.2k
  int samples_per_partition=info->grouping;
821
16.2k
  int partitions_per_word=phrasebook->dim;
822
16.2k
  int max=(vb->pcmend*ch)>>1;
823
16.2k
  int end=(info->end<max?info->end:max);
824
16.2k
  int n=end-info->begin;
825
826
16.2k
  if(n>0){
827
12.9k
    int partvals=n/samples_per_partition;
828
12.9k
    int partwords=(partvals+partitions_per_word-1)/partitions_per_word;
829
12.9k
    int **partword=_vorbis_block_alloc(vb,partwords*sizeof(*partword));
830
831
39.4k
    for(i=0;i<ch;i++)if(nonzero[i])break;
832
12.9k
    if(i==ch)return(0); /* no nonzero vectors */
833
834
21.0k
    for(s=0;s<look->stages;s++){
835
178k
      for(i=0,l=0;i<partvals;l++){
836
837
170k
        if(s==0){
838
          /* fetch the partition word */
839
130k
          int temp=vorbis_book_decode(phrasebook,&vb->opb);
840
130k
          if(temp==-1 || temp>=info->partvals)goto eopbreak;
841
123k
          partword[l]=look->decodemap[temp];
842
123k
          if(partword[l]==NULL)goto errout;
843
123k
        }
844
845
        /* now we decode residual values for the partitions */
846
575k
        for(k=0;k<partitions_per_word && i<partvals;k++,i++)
847
413k
          if(info->secondstages[partword[l][k]]&(1<<s)){
848
97.9k
            int stagebooknum=look->partbooks[partword[l][k]][s];
849
850
97.9k
            if(stagebooknum>=0){
851
97.9k
              dec_codebook *stagebook=look->decbooks+stagebooknum;
852
97.9k
              if(vorbis_book_decodevv_add(stagebook,in,
853
97.9k
                                          i*samples_per_partition+info->begin,ch,
854
97.9k
                                          &vb->opb,samples_per_partition)==-1)
855
1.08k
                goto eopbreak;
856
97.9k
            }
857
97.9k
          }
858
162k
      }
859
17.2k
    }
860
12.4k
  }
861
7.04k
 errout:
862
15.7k
 eopbreak:
863
15.7k
  return(0);
864
7.04k
}
865
866
867
const vorbis_func_residue residue0_exportbundle={
868
  NULL,
869
  &res0_unpack,
870
  &res0_look,
871
  &res0_free_info,
872
  &res0_free_look,
873
  NULL,
874
  NULL,
875
  &res0_inverse
876
};
877
878
const vorbis_func_residue residue1_exportbundle={
879
  &res0_pack,
880
  &res0_unpack,
881
  &res0_look,
882
  &res0_free_info,
883
  &res0_free_look,
884
  &res1_class,
885
  &res1_forward,
886
  &res1_inverse
887
};
888
889
const vorbis_func_residue residue2_exportbundle={
890
  &res0_pack,
891
  &res0_unpack,
892
  &res0_look,
893
  &res0_free_info,
894
  &res0_free_look,
895
  &res2_class,
896
  &res2_forward,
897
  &res2_inverse
898
};