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