res0.c 24.1 KB
Newer Older
Monty's avatar
   
Monty committed
1
2
/********************************************************************
 *                                                                  *
Monty's avatar
   
Monty committed
3
 * THIS FILE IS PART OF THE OggVorbis SOFTWARE CODEC SOURCE CODE.   *
Monty's avatar
   
Monty committed
4
5
6
 * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS     *
 * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE *
 * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING.       *
Monty's avatar
   
Monty committed
7
 *                                                                  *
8
 * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2010             *
9
 * by the Xiph.Org Foundation http://www.xiph.org/                  *
Monty's avatar
   
Monty committed
10
 *                                                                  *
Monty's avatar
   
Monty committed
11
12
 ********************************************************************

Monty's avatar
   
Monty committed
13
 function: residue backend 0, 1 and 2 implementation
14
 last mod: $Id$
Monty's avatar
   
Monty committed
15
16
17

 ********************************************************************/

Monty's avatar
   
Monty committed
18
19
20
21
22
/* Slow, slow, slow, simpleminded and did I mention it was slow?  The
   encode/decode loops are coded for clarity and performance is not
   yet even a nagging little idea lurking in the shadows.  Oh and BTW,
   it's slow. */

Monty's avatar
   
Monty committed
23
24
#include <stdlib.h>
#include <string.h>
Monty's avatar
   
Monty committed
25
#include <math.h>
Monty's avatar
   
Monty committed
26
#include <ogg/ogg.h>
Monty's avatar
   
Monty committed
27
#include "vorbis/codec.h"
Monty's avatar
   
Monty committed
28
#include "codec_internal.h"
Monty's avatar
   
Monty committed
29
#include "registry.h"
Monty's avatar
   
Monty committed
30
#include "codebook.h"
Monty's avatar
   
Monty committed
31
#include "misc.h"
Monty's avatar
   
Monty committed
32
#include "os.h"
Monty's avatar
   
Monty committed
33

34
#if defined(TRAIN_RES) || defined (TRAIN_RESAUX)
Monty's avatar
   
Monty committed
35
#include <stdio.h>
Monty's avatar
Monty committed
36
#endif
Monty's avatar
   
Monty committed
37

Monty's avatar
   
Monty committed
38
39
typedef struct {
  vorbis_info_residue0 *info;
Monty's avatar
Monty committed
40

Monty's avatar
   
Monty committed
41
  int         parts;
Monty's avatar
   
Monty committed
42
43
  int         stages;
  codebook   *fullbooks;
Monty's avatar
   
Monty committed
44
45
46
47
48
  codebook   *phrasebook;
  codebook ***partbooks;

  int         partvals;
  int       **decodemap;
Monty's avatar
   
Monty committed
49

Monty's avatar
   
Monty committed
50
  long      postbits;
Monty's avatar
   
Monty committed
51
  long      phrasebits;
Monty's avatar
   
Monty committed
52
  long      frames;
Monty's avatar
   
Monty committed
53

54
#if defined(TRAIN_RES) || defined(TRAIN_RESAUX)
Monty's avatar
   
Monty committed
55
  int        train_seq;
Monty's avatar
   
Monty committed
56
  long      *training_data[8][64];
Monty's avatar
   
Monty committed
57
58
59
60
  float      training_max[8][64];
  float      training_min[8][64];
  float     tmin;
  float     tmax;
Monty's avatar
   
Monty committed
61
62
#endif

Monty's avatar
   
Monty committed
63
} vorbis_look_residue0;
Monty's avatar
   
Monty committed
64

Monty's avatar
   
Monty committed
65
void res0_free_info(vorbis_info_residue *i){
66
67
68
69
  vorbis_info_residue0 *info=(vorbis_info_residue0 *)i;
  if(info){
    memset(info,0,sizeof(*info));
    _ogg_free(info);
Monty's avatar
   
Monty committed
70
  }
Monty's avatar
   
Monty committed
71
72
}

Monty's avatar
   
Monty committed
73
void res0_free_look(vorbis_look_residue *i){
Monty's avatar
   
Monty committed
74
  int j;
Monty's avatar
   
Monty committed
75
  if(i){
Monty's avatar
   
Monty committed
76

Monty's avatar
   
Monty committed
77
    vorbis_look_residue0 *look=(vorbis_look_residue0 *)i;
Monty's avatar
   
Monty committed
78

Monty's avatar
   
Monty committed
79
80
81
82
#ifdef TRAIN_RES
    {
      int j,k,l;
      for(j=0;j<look->parts;j++){
83
84
85
86
87
88
        /*fprintf(stderr,"partition %d: ",j);*/
        for(k=0;k<8;k++)
          if(look->training_data[k][j]){
            char buffer[80];
            FILE *of;
            codebook *statebook=look->partbooks[j][k];
Monty's avatar
Monty committed
89

90
91
92
93
94
95
            /* long and short into the same bucket by current convention */
            sprintf(buffer,"res_part%d_pass%d.vqd",j,k);
            of=fopen(buffer,"a");

            for(l=0;l<statebook->entries;l++)
              fprintf(of,"%d:%ld\n",l,look->training_data[k][j][l]);
Monty's avatar
Monty committed
96

97
            fclose(of);
Monty's avatar
Monty committed
98

99
100
101
102
103
104
105
            /*fprintf(stderr,"%d(%.2f|%.2f) ",k,
              look->training_min[k][j],look->training_max[k][j]);*/

            _ogg_free(look->training_data[k][j]);
            look->training_data[k][j]=NULL;
          }
        /*fprintf(stderr,"\n");*/
Monty's avatar
   
Monty committed
106
107
108
109
      }
    }
    fprintf(stderr,"min/max residue: %g::%g\n",look->tmin,look->tmax);

Monty's avatar
   
Monty committed
110
    /*fprintf(stderr,"residue bit usage %f:%f (%f total)\n",
111
112
113
            (float)look->phrasebits/look->frames,
            (float)look->postbits/look->frames,
            (float)(look->postbits+look->phrasebits)/look->frames);*/
Monty's avatar
   
Monty committed
114
115
#endif

Monty's avatar
   
Monty committed
116
117
118
119

    /*vorbis_info_residue0 *info=look->info;

    fprintf(stderr,
120
121
122
123
            "%ld frames encoded in %ld phrasebits and %ld residue bits "
            "(%g/frame) \n",look->frames,look->phrasebits,
            look->resbitsflat,
            (look->phrasebits+look->resbitsflat)/(float)look->frames);
Monty's avatar
Monty committed
124

Monty's avatar
   
Monty committed
125
126
127
128
    for(j=0;j<look->parts;j++){
      long acc=0;
      fprintf(stderr,"\t[%d] == ",j);
      for(k=0;k<look->stages;k++)
129
130
131
132
        if((info->secondstages[j]>>k)&1){
          fprintf(stderr,"%ld,",look->resbits[j][k]);
          acc+=look->resbits[j][k];
        }
Monty's avatar
   
Monty committed
133
134

      fprintf(stderr,":: (%ld vals) %1.2fbits/sample\n",look->resvals[j],
135
              acc?(float)acc/(look->resvals[j]*info->grouping):0);
Monty's avatar
   
Monty committed
136
137
    }
    fprintf(stderr,"\n");*/
Monty's avatar
   
Monty committed
138

Monty's avatar
   
Monty committed
139
    for(j=0;j<look->parts;j++)
Monty's avatar
   
Monty committed
140
141
      if(look->partbooks[j])_ogg_free(look->partbooks[j]);
    _ogg_free(look->partbooks);
Monty's avatar
   
Monty committed
142
    for(j=0;j<look->partvals;j++)
Monty's avatar
   
Monty committed
143
144
      _ogg_free(look->decodemap[j]);
    _ogg_free(look->decodemap);
Monty's avatar
   
Monty committed
145

146
147
    memset(look,0,sizeof(*look));
    _ogg_free(look);
Monty's avatar
   
Monty committed
148
  }
Monty's avatar
   
Monty committed
149
150
}

Monty's avatar
   
Monty committed
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
static int ilog(unsigned int v){
  int ret=0;
  while(v){
    ret++;
    v>>=1;
  }
  return(ret);
}

static int icount(unsigned int v){
  int ret=0;
  while(v){
    ret+=v&1;
    v>>=1;
  }
  return(ret);
}


Monty's avatar
   
Monty committed
170
void res0_pack(vorbis_info_residue *vr,oggpack_buffer *opb){
Monty's avatar
   
Monty committed
171
172
  vorbis_info_residue0 *info=(vorbis_info_residue0 *)vr;
  int j,acc=0;
Monty's avatar
   
Monty committed
173
174
  oggpack_write(opb,info->begin,24);
  oggpack_write(opb,info->end,24);
Monty's avatar
   
Monty committed
175

Monty's avatar
Monty committed
176
  oggpack_write(opb,info->grouping-1,24);  /* residue vectors to group and
177
                                             code with a partitioned book */
Monty's avatar
   
Monty committed
178
179
  oggpack_write(opb,info->partitions-1,6); /* possible partition choices */
  oggpack_write(opb,info->groupbook,8);  /* group huffman book */
Monty's avatar
   
Monty committed
180
181
182
183

  /* secondstages is a bitmask; as encoding progresses pass by pass, a
     bitmask of one indicates this partition class has bits to write
     this pass */
Monty's avatar
   
Monty committed
184
  for(j=0;j<info->partitions;j++){
Monty's avatar
   
Monty committed
185
186
    if(ilog(info->secondstages[j])>3){
      /* yes, this is a minor hack due to not thinking ahead */
Monty's avatar
Monty committed
187
      oggpack_write(opb,info->secondstages[j],3);
Monty's avatar
   
Monty committed
188
      oggpack_write(opb,1,1);
Monty's avatar
Monty committed
189
      oggpack_write(opb,info->secondstages[j]>>3,5);
Monty's avatar
   
Monty committed
190
191
192
    }else
      oggpack_write(opb,info->secondstages[j],4); /* trailing zero */
    acc+=icount(info->secondstages[j]);
Monty's avatar
   
Monty committed
193
194
  }
  for(j=0;j<acc;j++)
Monty's avatar
   
Monty committed
195
    oggpack_write(opb,info->booklist[j],8);
Monty's avatar
   
Monty committed
196

Monty's avatar
   
Monty committed
197
198
199
}

/* vorbis_info is for range checking */
Monty's avatar
   
Monty committed
200
vorbis_info_residue *res0_unpack(vorbis_info *vi,oggpack_buffer *opb){
Monty's avatar
   
Monty committed
201
  int j,acc=0;
202
  vorbis_info_residue0 *info=_ogg_calloc(1,sizeof(*info));
Monty's avatar
   
Monty committed
203
  codec_setup_info     *ci=vi->codec_setup;
Monty's avatar
   
Monty committed
204

Monty's avatar
   
Monty committed
205
206
207
208
209
  info->begin=oggpack_read(opb,24);
  info->end=oggpack_read(opb,24);
  info->grouping=oggpack_read(opb,24)+1;
  info->partitions=oggpack_read(opb,6)+1;
  info->groupbook=oggpack_read(opb,8);
Monty's avatar
   
Monty committed
210

Monty's avatar
Monty committed
211
212
213
  /* check for premature EOP */
  if(info->groupbook<0)goto errout;

Monty's avatar
   
Monty committed
214
  for(j=0;j<info->partitions;j++){
Monty's avatar
   
Monty committed
215
    int cascade=oggpack_read(opb,3);
Monty's avatar
Monty committed
216
217
218
219
220
221
222
    int cflag=oggpack_read(opb,1);
    if(cflag<0) goto errout;
    if(cflag){
      int c=oggpack_read(opb,5);
      if(c<0) goto errout;
      cascade|=(c<<3);
    }
Monty's avatar
   
Monty committed
223
224
    info->secondstages[j]=cascade;

Monty's avatar
   
Monty committed
225
    acc+=icount(cascade);
Monty's avatar
   
Monty committed
226
  }
Monty's avatar
Monty committed
227
228
229
230
231
  for(j=0;j<acc;j++){
    int book=oggpack_read(opb,8);
    if(book<0) goto errout;
    info->booklist[j]=book;
  }
Monty's avatar
   
Monty committed
232

Monty's avatar
   
Monty committed
233
  if(info->groupbook>=ci->books)goto errout;
234
  for(j=0;j<acc;j++){
Monty's avatar
   
Monty committed
235
    if(info->booklist[j]>=ci->books)goto errout;
236
237
    if(ci->book_param[info->booklist[j]]->maptype==0)goto errout;
  }
Monty's avatar
   
Monty committed
238

239
240
  /* verify the phrasebook is not specifying an impossible or
     inconsistent partitioning scheme. */
241
242
243
244
  /* modify the phrasebook ranging check from r16327; an early beta
     encoder had a bug where it used an oversized phrasebook by
     accident.  These files should continue to be playable, but don't
     allow an exploit */
245
246
247
248
249
250
251
252
253
  {
    int entries = ci->book_param[info->groupbook]->entries;
    int dim = ci->book_param[info->groupbook]->dim;
    int partvals = 1;
    while(dim>0){
      partvals *= info->partitions;
      if(partvals > entries) goto errout;
      dim--;
    }
254
    info->partvals = partvals;
255
256
  }

Monty's avatar
   
Monty committed
257
  return(info);
Monty's avatar
   
Monty committed
258
 errout:
Monty's avatar
   
Monty committed
259
  res0_free_info(info);
Monty's avatar
   
Monty committed
260
  return(NULL);
Monty's avatar
   
Monty committed
261
262
}

Monty's avatar
   
Monty committed
263
vorbis_look_residue *res0_look(vorbis_dsp_state *vd,
264
                               vorbis_info_residue *vr){
Monty's avatar
   
Monty committed
265
  vorbis_info_residue0 *info=(vorbis_info_residue0 *)vr;
266
  vorbis_look_residue0 *look=_ogg_calloc(1,sizeof(*look));
Monty's avatar
   
Monty committed
267
  codec_setup_info     *ci=vd->vi->codec_setup;
Monty's avatar
   
Monty committed
268

Monty's avatar
   
Monty committed
269
270
  int j,k,acc=0;
  int dim;
Monty's avatar
   
Monty committed
271
  int maxstage=0;
Monty's avatar
   
Monty committed
272
  look->info=info;
Monty's avatar
   
Monty committed
273
274

  look->parts=info->partitions;
Monty's avatar
   
Monty committed
275
276
  look->fullbooks=ci->fullbooks;
  look->phrasebook=ci->fullbooks+info->groupbook;
Monty's avatar
   
Monty committed
277
278
  dim=look->phrasebook->dim;

279
  look->partbooks=_ogg_calloc(look->parts,sizeof(*look->partbooks));
Monty's avatar
   
Monty committed
280
281

  for(j=0;j<look->parts;j++){
Monty's avatar
   
Monty committed
282
    int stages=ilog(info->secondstages[j]);
Monty's avatar
   
Monty committed
283
    if(stages){
Monty's avatar
   
Monty committed
284
      if(stages>maxstage)maxstage=stages;
285
      look->partbooks[j]=_ogg_calloc(stages,sizeof(*look->partbooks[j]));
Monty's avatar
   
Monty committed
286
      for(k=0;k<stages;k++)
287
288
        if(info->secondstages[j]&(1<<k)){
          look->partbooks[j][k]=ci->fullbooks+info->booklist[acc++];
Monty's avatar
   
Monty committed
289
#ifdef TRAIN_RES
290
291
          look->training_data[k][j]=_ogg_calloc(look->partbooks[j][k]->entries,
                                           sizeof(***look->training_data));
Monty's avatar
   
Monty committed
292
#endif
293
        }
Monty's avatar
   
Monty committed
294
295
296
    }
  }

Monty's avatar
Monty committed
297
298
299
300
  look->partvals=1;
  for(j=0;j<dim;j++)
      look->partvals*=look->parts;

Monty's avatar
   
Monty committed
301
  look->stages=maxstage;
302
  look->decodemap=_ogg_malloc(look->partvals*sizeof(*look->decodemap));
Monty's avatar
   
Monty committed
303
304
305
  for(j=0;j<look->partvals;j++){
    long val=j;
    long mult=look->partvals/look->parts;
306
    look->decodemap[j]=_ogg_malloc(dim*sizeof(*look->decodemap[j]));
Monty's avatar
   
Monty committed
307
308
309
310
311
312
    for(k=0;k<dim;k++){
      long deco=val/mult;
      val-=deco*mult;
      mult/=look->parts;
      look->decodemap[j][k]=deco;
    }
Monty's avatar
   
Monty committed
313
  }
314
#if defined(TRAIN_RES) || defined (TRAIN_RESAUX)
Monty's avatar
   
Monty committed
315
  {
Monty's avatar
   
Monty committed
316
317
    static int train_seq=0;
    look->train_seq=train_seq++;
Monty's avatar
   
Monty committed
318
  }
Monty's avatar
   
Monty committed
319
#endif
Monty's avatar
   
Monty committed
320
  return(look);
Monty's avatar
   
Monty committed
321
}
Monty's avatar
   
Monty committed
322

Monty's avatar
   
Monty committed
323
/* break an abstraction and copy some code for performance purposes */
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
static int local_book_besterror(codebook *book,int *a){
  int dim=book->dim;
  int i,j,o;
  int minval=book->minval;
  int del=book->delta;
  int qv=book->quantvals;
  int ze=(qv>>1);
  int index=0;
  /* assumes integer/centered encoder codebook maptype 1 no more than dim 8 */
  int p[8]={0,0,0,0,0,0,0,0};

  if(del!=1){
    for(i=0,o=dim;i<dim;i++){
      int v = (a[--o]-minval+(del>>1))/del;
      int m = (v<ze ? ((ze-v)<<1)-1 : ((v-ze)<<1));
      index = index*qv+ (m<0?0:(m>=qv?qv-1:m));
      p[o]=v*del+minval;
    }
  }else{
    for(i=0,o=dim;i<dim;i++){
      int v = a[--o]-minval;
      int m = (v<ze ? ((ze-v)<<1)-1 : ((v-ze)<<1));
      index = index*qv+ (m<0?0:(m>=qv?qv-1:m));
      p[o]=v*del+minval;
Monty's avatar
   
Monty committed
348
349
    }
  }
Monty's avatar
Monty committed
350

351
  if(book->c->lengthlist[index]<=0){
Monty's avatar
   
Monty committed
352
    const static_codebook *c=book->c;
353
354
355
356
    int best=-1;
    /* assumes integer/centered encoder codebook maptype 1 no more than dim 8 */
    int e[8]={0,0,0,0,0,0,0,0};
    int maxval = book->minval + book->delta*(book->quantvals-1);
Monty's avatar
   
Monty committed
357
358
    for(i=0;i<book->entries;i++){
      if(c->lengthlist[i]>0){
359
        int this=0;
360
        for(j=0;j<dim;j++){
361
          int val=(e[j]-a[j]);
362
363
          this+=val*val;
        }
364
365
366
367
        if(best==-1 || this<best){
          memcpy(p,e,sizeof(p));
          best=this;
          index=i;
368
        }
Monty's avatar
   
Monty committed
369
      }
370
371
372
373
374
375
376
      /* assumes the value patterning created by the tools in vq/ */
      j=0;
      while(e[j]>=maxval)
        e[j++]=0;
      if(e[j]>=0)
        e[j]+=book->delta;
      e[j]= -e[j];
Monty's avatar
   
Monty committed
377
378
379
    }
  }

380
  if(index>-1){
Monty's avatar
   
Monty committed
381
    for(i=0;i<dim;i++)
382
      *a++ -= p[i];
Monty's avatar
   
Monty committed
383
  }
Monty's avatar
   
Monty committed
384

385
  return(index);
Monty's avatar
   
Monty committed
386
}
Monty's avatar
   
Monty committed
387

388
static int _encodepart(oggpack_buffer *opb,int *vec, int n,
389
                       codebook *book,long *acc){
Monty's avatar
   
Monty committed
390
391
392
  int i,bits=0;
  int dim=book->dim;
  int step=n/dim;
Monty's avatar
   
Monty committed
393

Monty's avatar
   
Monty committed
394
  for(i=0;i<step;i++){
Monty's avatar
   
Monty committed
395
    int entry=local_book_besterror(book,vec+i*dim);
Monty's avatar
   
Monty committed
396

Monty's avatar
   
Monty committed
397
#ifdef TRAIN_RES
398
    if(entry>=0)
399
      acc[entry]++;
Monty's avatar
   
Monty committed
400
#endif
Monty's avatar
Monty committed
401

Monty's avatar
   
Monty committed
402
    bits+=vorbis_book_encode(book,entry,opb);
Monty's avatar
Monty committed
403

Monty's avatar
   
Monty committed
404
  }
Monty's avatar
   
Monty committed
405
406

  return(bits);
Monty's avatar
   
Monty committed
407
408
}

Monty's avatar
   
Monty committed
409
static long **_01class(vorbis_block *vb,vorbis_look_residue *vl,
410
                       int **in,int ch){
Monty's avatar
   
Monty committed
411
  long i,j,k;
Monty's avatar
   
Monty committed
412
413
414
415
416
417
  vorbis_look_residue0 *look=(vorbis_look_residue0 *)vl;
  vorbis_info_residue0 *info=look->info;

  /* move all this setup out later */
  int samples_per_partition=info->grouping;
  int possible_partitions=info->partitions;
418
  int n=info->end-info->begin;
Monty's avatar
Monty committed
419

420
421
422
  int partvals=n/samples_per_partition;
  long **partword=_vorbis_block_alloc(vb,ch*sizeof(*partword));
  float scale=100./samples_per_partition;
Monty's avatar
Monty committed
423

424
425
426
  /* we find the partition type for each partition of each
     channel.  We'll go back and do the interleaved encoding in a
     bit.  For now, clarity */
Monty's avatar
Monty committed
427

428
429
430
431
  for(i=0;i<ch;i++){
    partword[i]=_vorbis_block_alloc(vb,n/samples_per_partition*sizeof(*partword[i]));
    memset(partword[i],0,n/samples_per_partition*sizeof(*partword[i]));
  }
Monty's avatar
Monty committed
432

433
434
435
  for(i=0;i<partvals;i++){
    int offset=i*samples_per_partition+info->begin;
    for(j=0;j<ch;j++){
436
437
      int max=0;
      int ent=0;
438
      for(k=0;k<samples_per_partition;k++){
439
440
        if(abs(in[j][offset+k])>max)max=abs(in[j][offset+k]);
        ent+=abs(in[j][offset+k]);
Monty's avatar
   
Monty committed
441
      }
442
      ent*=scale;
Monty's avatar
Monty committed
443

444
      for(k=0;k<possible_partitions-1;k++)
445
        if(max<=info->classmetric1[k] &&
446
           (info->classmetric2[k]<0 || ent<info->classmetric2[k]))
447
          break;
Monty's avatar
Monty committed
448
449

      partword[j][i]=k;
Monty's avatar
   
Monty committed
450
    }
451
  }
Monty's avatar
Monty committed
452

Monty's avatar
   
Monty committed
453
#ifdef TRAIN_RESAUX
454
455
456
  {
    FILE *of;
    char buffer[80];
Monty's avatar
Monty committed
457

458
459
460
461
    for(i=0;i<ch;i++){
      sprintf(buffer,"resaux_%d.vqd",look->train_seq);
      of=fopen(buffer,"a");
      for(j=0;j<partvals;j++)
462
        fprintf(of,"%ld, ",partword[i][j]);
463
464
      fprintf(of,"\n");
      fclose(of);
Monty's avatar
   
Monty committed
465
    }
466
  }
Monty's avatar
   
Monty committed
467
#endif
468
  look->frames++;
Monty's avatar
Monty committed
469

470
  return(partword);
Monty's avatar
   
Monty committed
471
472
}

Monty's avatar
   
Monty committed
473
474
475
/* designed for stereo or other modes where the partition size is an
   integer multiple of the number of channels encoded in the current
   submap */
476
static long **_2class(vorbis_block *vb,vorbis_look_residue *vl,int **in,
477
                      int ch){
Monty's avatar
   
Monty committed
478
479
480
481
482
483
484
  long i,j,k,l;
  vorbis_look_residue0 *look=(vorbis_look_residue0 *)vl;
  vorbis_info_residue0 *info=look->info;

  /* move all this setup out later */
  int samples_per_partition=info->grouping;
  int possible_partitions=info->partitions;
485
  int n=info->end-info->begin;
Monty's avatar
   
Monty committed
486

487
488
  int partvals=n/samples_per_partition;
  long **partword=_vorbis_block_alloc(vb,sizeof(*partword));
Monty's avatar
Monty committed
489

490
#if defined(TRAIN_RES) || defined (TRAIN_RESAUX)
491
492
  FILE *of;
  char buffer[80];
Monty's avatar
   
Monty committed
493
#endif
Monty's avatar
Monty committed
494

495
496
  partword[0]=_vorbis_block_alloc(vb,n*ch/samples_per_partition*sizeof(*partword[0]));
  memset(partword[0],0,n*ch/samples_per_partition*sizeof(*partword[0]));
Monty's avatar
Monty committed
497

498
  for(i=0,l=info->begin/ch;i<partvals;i++){
499
500
    int magmax=0.f;
    int angmax=0.f;
501
    for(j=0;j<samples_per_partition;j+=ch){
502
      if(abs(in[0][l])>magmax)magmax=abs(in[0][l]);
503
      for(k=1;k<ch;k++)
504
        if(abs(in[k][l])>angmax)angmax=abs(in[k][l]);
505
        l++;
506
    }
Monty's avatar
Monty committed
507

508
509
    for(j=0;j<possible_partitions-1;j++)
      if(magmax<=info->classmetric1[j] &&
510
511
         angmax<=info->classmetric2[j])
        break;
Monty's avatar
Monty committed
512

513
    partword[0][i]=j;
Monty's avatar
Monty committed
514
515
516

  }

Monty's avatar
   
Monty committed
517
#ifdef TRAIN_RESAUX
518
519
520
521
522
523
  sprintf(buffer,"resaux_%d.vqd",look->train_seq);
  of=fopen(buffer,"a");
  for(i=0;i<partvals;i++)
    fprintf(of,"%ld, ",partword[0][i]);
  fprintf(of,"\n");
  fclose(of);
Monty's avatar
   
Monty committed
524
#endif
Monty's avatar
Monty committed
525

526
  look->frames++;
Monty's avatar
Monty committed
527

528
  return(partword);
Monty's avatar
   
Monty committed
529
530
}

Monty's avatar
   
Monty committed
531
static int _01forward(oggpack_buffer *opb,
532
                      vorbis_block *vb,vorbis_look_residue *vl,
533
                      int **in,int ch,
534
                      long **partword,
535
                      int (*encode)(oggpack_buffer *,int *,int,
536
                                    codebook *,long *)){
Monty's avatar
   
Monty committed
537
  long i,j,k,s;
Monty's avatar
   
Monty committed
538
  vorbis_look_residue0 *look=(vorbis_look_residue0 *)vl;
Monty's avatar
   
Monty committed
539
  vorbis_info_residue0 *info=look->info;
Monty's avatar
   
Monty committed
540
541
542
543
544

  /* move all this setup out later */
  int samples_per_partition=info->grouping;
  int possible_partitions=info->partitions;
  int partitions_per_word=look->phrasebook->dim;
545
  int n=info->end-info->begin;
Monty's avatar
   
Monty committed
546

547
548
549
  int partvals=n/samples_per_partition;
  long resbits[128];
  long resvals[128];
Monty's avatar
Monty committed
550

Monty's avatar
   
Monty committed
551
#ifdef TRAIN_RES
552
  for(i=0;i<ch;i++)
553
    for(j=info->begin;j<info->end;j++){
554
555
556
      if(in[i][j]>look->tmax)look->tmax=in[i][j];
      if(in[i][j]<look->tmin)look->tmin=in[i][j];
    }
Monty's avatar
   
Monty committed
557
#endif
Monty's avatar
Monty committed
558

559
560
  memset(resbits,0,sizeof(resbits));
  memset(resvals,0,sizeof(resvals));
Monty's avatar
Monty committed
561

562
563
564
565
  /* we code the partition words for each channel, then the residual
     words for a partition per channel until we've written all the
     residual words for that partition word.  Then write the next
     partition channel words... */
Monty's avatar
Monty committed
566

567
  for(s=0;s<look->stages;s++){
Monty's avatar
Monty committed
568

569
    for(i=0;i<partvals;){
Monty's avatar
Monty committed
570

571
572
      /* first we encode a partition codeword for each channel */
      if(s==0){
573
574
575
576
577
578
        for(j=0;j<ch;j++){
          long val=partword[j][i];
          for(k=1;k<partitions_per_word;k++){
            val*=possible_partitions;
            if(i+k<partvals)
              val+=partword[j][i+k];
Monty's avatar
Monty committed
579
580
          }

581
582
583
          /* training hack */
          if(val<look->phrasebook->entries)
            look->phrasebits+=vorbis_book_encode(look->phrasebook,val,opb);
Monty's avatar
   
Monty committed
584
#if 0 /*def TRAIN_RES*/
585
586
          else
            fprintf(stderr,"!");
Monty's avatar
   
Monty committed
587
#endif
Monty's avatar
Monty committed
588

589
        }
590
      }
Monty's avatar
Monty committed
591

592
593
      /* now we encode interleaved residual values for the partitions */
      for(k=0;k<partitions_per_word && i<partvals;k++,i++){
594
        long offset=i*samples_per_partition+info->begin;
Monty's avatar
Monty committed
595

596
597
598
599
600
601
602
        for(j=0;j<ch;j++){
          if(s==0)resvals[partword[j][i]]+=samples_per_partition;
          if(info->secondstages[partword[j][i]]&(1<<s)){
            codebook *statebook=look->partbooks[partword[j][i]][s];
            if(statebook){
              int ret;
              long *accumulator=NULL;
Monty's avatar
Monty committed
603

Monty's avatar
   
Monty committed
604
#ifdef TRAIN_RES
605
606
607
608
609
610
611
612
613
614
615
              accumulator=look->training_data[s][partword[j][i]];
              {
                int l;
                float *samples=in[j]+offset;
                for(l=0;l<samples_per_partition;l++){
                  if(samples[l]<look->training_min[s][partword[j][i]])
                    look->training_min[s][partword[j][i]]=samples[l];
                  if(samples[l]>look->training_max[s][partword[j][i]])
                    look->training_max[s][partword[j][i]]=samples[l];
                }
              }
616
#endif
Monty's avatar
Monty committed
617

618
619
              ret=encode(opb,in[j]+offset,samples_per_partition,
                         statebook,accumulator);
Monty's avatar
Monty committed
620

621
622
623
624
625
              look->postbits+=ret;
              resbits[partword[j][i]]+=ret;
            }
          }
        }
Monty's avatar
   
Monty committed
626
627
      }
    }
628
  }
Monty's avatar
Monty committed
629

Monty's avatar
   
Monty committed
630
  /*{
Monty's avatar
   
Monty committed
631
632
633
634
    long total=0;
    long totalbits=0;
    fprintf(stderr,"%d :: ",vb->mode);
    for(k=0;k<possible_partitions;k++){
635
636
637
638
    fprintf(stderr,"%ld/%1.2g, ",resvals[k],(float)resbits[k]/resvals[k]);
    total+=resvals[k];
    totalbits+=resbits[k];
    }
Monty's avatar
Monty committed
639

Monty's avatar
   
Monty committed
640
    fprintf(stderr,":: %ld:%1.2g\n",total,(double)totalbits/total);
Monty's avatar
   
Monty committed
641
    }*/
642

Monty's avatar
   
Monty committed
643
  return(0);
Monty's avatar
   
Monty committed
644
}
Monty's avatar
   
Monty committed
645

Monty's avatar
   
Monty committed
646
/* a truncated packet here just means 'stop working'; it's not an error */
Monty's avatar
   
Monty committed
647
static int _01inverse(vorbis_block *vb,vorbis_look_residue *vl,
648
                      float **in,int ch,
Monty's avatar
Monty committed
649
                      long (*decodepart)(codebook *, float *,
650
                                         oggpack_buffer *,int)){
Monty's avatar
   
Monty committed
651

Monty's avatar
   
Monty committed
652
  long i,j,k,l,s;
Monty's avatar
   
Monty committed
653
654
655
656
657
658
  vorbis_look_residue0 *look=(vorbis_look_residue0 *)vl;
  vorbis_info_residue0 *info=look->info;

  /* move all this setup out later */
  int samples_per_partition=info->grouping;
  int partitions_per_word=look->phrasebook->dim;
659
660
  int max=vb->pcmend>>1;
  int end=(info->end<max?info->end:max);
661
  int n=end-info->begin;
Monty's avatar
Monty committed
662

663
664
665
666
  if(n>0){
    int partvals=n/samples_per_partition;
    int partwords=(partvals+partitions_per_word-1)/partitions_per_word;
    int ***partword=alloca(ch*sizeof(*partword));
Monty's avatar
Monty committed
667

668
669
    for(j=0;j<ch;j++)
      partword[j]=_vorbis_block_alloc(vb,partwords*sizeof(*partword[j]));
Monty's avatar
Monty committed
670

671
    for(s=0;s<look->stages;s++){
Monty's avatar
Monty committed
672
673

      /* each loop decodes on partition codeword containing
674
         partitions_per_word partitions */
675
      for(i=0,l=0;i<partvals;l++){
676
677
678
679
        if(s==0){
          /* fetch the partition word for each channel */
          for(j=0;j<ch;j++){
            int temp=vorbis_book_decode(look->phrasebook,&vb->opb);
Monty's avatar
Monty committed
680

681
            if(temp==-1 || temp>=info->partvals)goto eopbreak;
682
683
684
685
            partword[j][l]=look->decodemap[temp];
            if(partword[j][l]==NULL)goto errout;
          }
        }
Monty's avatar
Monty committed
686

687
688
689
690
691
692
693
694
695
696
697
698
        /* now we decode residual values for the partitions */
        for(k=0;k<partitions_per_word && i<partvals;k++,i++)
          for(j=0;j<ch;j++){
            long offset=info->begin+i*samples_per_partition;
            if(info->secondstages[partword[j][l][k]]&(1<<s)){
              codebook *stagebook=look->partbooks[partword[j][l][k]][s];
              if(stagebook){
                if(decodepart(stagebook,in[j]+offset,&vb->opb,
                              samples_per_partition)==-1)goto eopbreak;
              }
            }
          }
Monty's avatar
Monty committed
699
      }
700
    }
Monty's avatar
   
Monty committed
701
  }
Monty's avatar
   
Monty committed
702
 errout:
Monty's avatar
   
Monty committed
703
 eopbreak:
Monty's avatar
   
Monty committed
704
  return(0);
Monty's avatar
   
Monty committed
705
706
}

Monty's avatar
   
Monty committed
707
int res0_inverse(vorbis_block *vb,vorbis_look_residue *vl,
708
                 float **in,int *nonzero,int ch){
Monty's avatar
   
Monty committed
709
710
711
712
713
714
715
716
  int i,used=0;
  for(i=0;i<ch;i++)
    if(nonzero[i])
      in[used++]=in[i];
  if(used)
    return(_01inverse(vb,vl,in,used,vorbis_book_decodevs_add));
  else
    return(0);
Monty's avatar
   
Monty committed
717
718
}

Monty's avatar
   
Monty committed
719
int res1_forward(oggpack_buffer *opb,vorbis_block *vb,vorbis_look_residue *vl,
720
                 int **in,int *nonzero,int ch, long **partword){
721
  int i,used=0;
Monty's avatar
   
Monty committed
722
  for(i=0;i<ch;i++)
723
    if(nonzero[i])
Monty's avatar
   
Monty committed
724
725
726
      in[used++]=in[i];

  if(used){
727
    return _01forward(opb,vb,vl,in,used,partword,_encodepart);
Monty's avatar
   
Monty committed
728
  }else{
Monty's avatar
   
Monty committed
729
    return(0);
Monty's avatar
   
Monty committed
730
  }
Monty's avatar
   
Monty committed
731
732
733
}

long **res1_class(vorbis_block *vb,vorbis_look_residue *vl,
734
                  int **in,int *nonzero,int ch){
Monty's avatar
   
Monty committed
735
736
737
738
739
  int i,used=0;
  for(i=0;i<ch;i++)
    if(nonzero[i])
      in[used++]=in[i];
  if(used)
Monty's avatar
   
Monty committed
740
    return(_01class(vb,vl,in,used));
Monty's avatar
   
Monty committed
741
742
  else
    return(0);
Monty's avatar
   
Monty committed
743
744
}

Monty's avatar
   
Monty committed
745
int res1_inverse(vorbis_block *vb,vorbis_look_residue *vl,
746
                 float **in,int *nonzero,int ch){
Monty's avatar
   
Monty committed
747
748
749
750
751
752
753
754
  int i,used=0;
  for(i=0;i<ch;i++)
    if(nonzero[i])
      in[used++]=in[i];
  if(used)
    return(_01inverse(vb,vl,in,used,vorbis_book_decodev_add));
  else
    return(0);
Monty's avatar
   
Monty committed
755
756
}

Monty's avatar
   
Monty committed
757
long **res2_class(vorbis_block *vb,vorbis_look_residue *vl,
758
                  int **in,int *nonzero,int ch){
Monty's avatar
   
Monty committed
759
760
  int i,used=0;
  for(i=0;i<ch;i++)
Monty's avatar
   
Monty committed
761
    if(nonzero[i])used++;
Monty's avatar
   
Monty committed
762
  if(used)
Monty's avatar
   
Monty committed
763
    return(_2class(vb,vl,in,ch));
Monty's avatar
   
Monty committed
764
765
766
767
  else
    return(0);
}

Monty's avatar
   
Monty committed
768
769
/* res2 is slightly more different; all the channels are interleaved
   into a single vector and encoded. */
Monty's avatar
   
Monty committed
770

Monty's avatar
   
Monty committed
771
int res2_forward(oggpack_buffer *opb,
772
                 vorbis_block *vb,vorbis_look_residue *vl,
773
                 int **in,int *nonzero,int ch, long **partword){
Monty's avatar
   
Monty committed
774
  long i,j,k,n=vb->pcmend/2,used=0;
Monty's avatar
   
Monty committed
775
776
777

  /* don't duplicate the code; use a working vector hack for now and
     reshape ourselves into a single channel res1 */
Monty's avatar
   
Monty committed
778
  /* ugly; reallocs for each coupling pass :-( */
779
  int *work=_vorbis_block_alloc(vb,ch*n*sizeof(*work));
Monty's avatar
   
Monty committed
780
  for(i=0;i<ch;i++){
781
    int *pcm=in[i];
Monty's avatar
   
Monty committed
782
    if(nonzero[i])used++;
Monty's avatar
   
Monty committed
783
784
785
    for(j=0,k=i;j<n;j++,k+=ch)
      work[k]=pcm[j];
  }
Monty's avatar
Monty committed
786

Monty's avatar
   
Monty committed
787
  if(used){
788
    return _01forward(opb,vb,vl,&work,1,partword,_encodepart);
Monty's avatar
   
Monty committed
789
  }else{
Monty's avatar
   
Monty committed
790
    return(0);
Monty's avatar
   
Monty committed
791
  }
Monty's avatar
   
Monty committed
792
793
794
}

/* duplicate code here as speed is somewhat more important */
Monty's avatar
   
Monty committed
795
int res2_inverse(vorbis_block *vb,vorbis_look_residue *vl,
796
                 float **in,int *nonzero,int ch){
Monty's avatar
   
Monty committed
797
798
799
800
801
802
803
  long i,k,l,s;
  vorbis_look_residue0 *look=(vorbis_look_residue0 *)vl;
  vorbis_info_residue0 *info=look->info;

  /* move all this setup out later */
  int samples_per_partition=info->grouping;
  int partitions_per_word=look->phrasebook->dim;
804
805
  int max=(vb->pcmend*ch)>>1;
  int end=(info->end<max?info->end:max);
806
  int n=end-info->begin;
Monty's avatar
   
Monty committed
807

808
809
810
811
  if(n>0){
    int partvals=n/samples_per_partition;
    int partwords=(partvals+partitions_per_word-1)/partitions_per_word;
    int **partword=_vorbis_block_alloc(vb,partwords*sizeof(*partword));
Monty's avatar
Monty committed
812

813
814
    for(i=0;i<ch;i++)if(nonzero[i])break;
    if(i==ch)return(0); /* no nonzero vectors */
Monty's avatar
Monty committed
815

816
817
    for(s=0;s<look->stages;s++){
      for(i=0,l=0;i<partvals;l++){
Monty's avatar
Monty committed
818

819
820
821
        if(s==0){
          /* fetch the partition word */
          int temp=vorbis_book_decode(look->phrasebook,&vb->opb);
822
          if(temp==-1 || temp>info->partvals)goto eopbreak;
823
824
825
          partword[l]=look->decodemap[temp];
          if(partword[l]==NULL)goto errout;
        }
Monty's avatar
Monty committed
826

827
828
829
830
        /* now we decode residual values for the partitions */
        for(k=0;k<partitions_per_word && i<partvals;k++,i++)
          if(info->secondstages[partword[l][k]]&(1<<s)){
            codebook *stagebook=look->partbooks[partword[l][k]][s];
Monty's avatar
Monty committed
831

832
833
834
835
836
837
838
            if(stagebook){
              if(vorbis_book_decodevv_add(stagebook,in,
                                          i*samples_per_partition+info->begin,ch,
                                          &vb->opb,samples_per_partition)==-1)
                goto eopbreak;
            }
          }
Monty's avatar
Monty committed
839
      }
840
    }
Monty's avatar
   
Monty committed
841
842
843
844
  }
 errout:
 eopbreak:
  return(0);
Monty's avatar
   
Monty committed
845
846
}

Monty's avatar
   
Monty committed
847

848
const vorbis_func_residue residue0_exportbundle={
Monty's avatar
   
Monty committed
849
  NULL,
Monty's avatar
   
Monty committed
850
851
852
853
  &res0_unpack,
  &res0_look,
  &res0_free_info,
  &res0_free_look,
Monty's avatar
   
Monty committed
854
855
  NULL,
  NULL,
Monty's avatar
   
Monty committed
856
  &res0_inverse
Monty's avatar
   
Monty committed
857
};
Monty's avatar
   
Monty committed
858

859
const vorbis_func_residue residue1_exportbundle={
Monty's avatar
   
Monty committed
860
861
862
863
864
  &res0_pack,
  &res0_unpack,
  &res0_look,
  &res0_free_info,
  &res0_free_look,
Monty's avatar
   
Monty committed
865
  &res1_class,
Monty's avatar
   
Monty committed
866
867
868
  &res1_forward,
  &res1_inverse
};
Monty's avatar
   
Monty committed
869

870
const vorbis_func_residue residue2_exportbundle={
Monty's avatar
   
Monty committed
871
872
873
874
875
  &res0_pack,
  &res0_unpack,
  &res0_look,
  &res0_free_info,
  &res0_free_look,
Monty's avatar
   
Monty committed
876
  &res2_class,
Monty's avatar
   
Monty committed
877
878
879
  &res2_forward,
  &res2_inverse
};