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