]> 4ch.mooo.com Git - 16.git/blob - src/lib/dl/ext/vorbis/res0.c
looking at tesuto now~
[16.git] / src / lib / dl / ext / vorbis / 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: res0.c 17556 2010-10-21 18:25:19Z tterribe $
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 <ext/libogg/ogg.h>
27 #include "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 ilog(unsigned int v){
156   int ret=0;
157   while(v){
158     ret++;
159     v>>=1;
160   }
161   return(ret);
162 }
163
164 static int icount(unsigned int v){
165   int ret=0;
166   while(v){
167     ret+=v&1;
168     v>>=1;
169   }
170   return(ret);
171 }
172
173
174 void res0_pack(vorbis_info_residue *vr,oggpack_buffer *opb){
175   vorbis_info_residue0 *info=(vorbis_info_residue0 *)vr;
176   int j,acc=0;
177   oggpack_write(opb,info->begin,24);
178   oggpack_write(opb,info->end,24);
179
180   oggpack_write(opb,info->grouping-1,24);  /* residue vectors to group and
181                                              code with a partitioned book */
182   oggpack_write(opb,info->partitions-1,6); /* possible partition choices */
183   oggpack_write(opb,info->groupbook,8);  /* group huffman book */
184
185   /* secondstages is a bitmask; as encoding progresses pass by pass, a
186      bitmask of one indicates this partition class has bits to write
187      this pass */
188   for(j=0;j<info->partitions;j++){
189     if(ilog(info->secondstages[j])>3){
190       /* yes, this is a minor hack due to not thinking ahead */
191       oggpack_write(opb,info->secondstages[j],3);
192       oggpack_write(opb,1,1);
193       oggpack_write(opb,info->secondstages[j]>>3,5);
194     }else
195       oggpack_write(opb,info->secondstages[j],4); /* trailing zero */
196     acc+=icount(info->secondstages[j]);
197   }
198   for(j=0;j<acc;j++)
199     oggpack_write(opb,info->booklist[j],8);
200
201 }
202
203 /* vorbis_info is for range checking */
204 vorbis_info_residue *res0_unpack(vorbis_info *vi,oggpack_buffer *opb){
205   int j,acc=0;
206   vorbis_info_residue0 *info=_ogg_calloc(1,sizeof(*info));
207   codec_setup_info     *ci=vi->codec_setup;
208
209   info->begin=oggpack_read(opb,24);
210   info->end=oggpack_read(opb,24);
211   info->grouping=oggpack_read(opb,24)+1;
212   info->partitions=oggpack_read(opb,6)+1;
213   info->groupbook=oggpack_read(opb,8);
214
215   /* check for premature EOP */
216   if(info->groupbook<0)goto errout;
217
218   for(j=0;j<info->partitions;j++){
219     int cascade=oggpack_read(opb,3);
220     int cflag=oggpack_read(opb,1);
221     if(cflag<0) goto errout;
222     if(cflag){
223       int c=oggpack_read(opb,5);
224       if(c<0) goto errout;
225       cascade|=(c<<3);
226     }
227     info->secondstages[j]=cascade;
228
229     acc+=icount(cascade);
230   }
231   for(j=0;j<acc;j++){
232     int book=oggpack_read(opb,8);
233     if(book<0) goto errout;
234     info->booklist[j]=book;
235   }
236
237   if(info->groupbook>=ci->books)goto errout;
238   for(j=0;j<acc;j++){
239     if(info->booklist[j]>=ci->books)goto errout;
240     if(ci->book_param[info->booklist[j]]->maptype==0)goto errout;
241   }
242
243   /* verify the phrasebook is not specifying an impossible or
244      inconsistent partitioning scheme. */
245   /* modify the phrasebook ranging check from r16327; an early beta
246      encoder had a bug where it used an oversized phrasebook by
247      accident.  These files should continue to be playable, but don't
248      allow an exploit */
249   {
250     int entries = ci->book_param[info->groupbook]->entries;
251     int dim = ci->book_param[info->groupbook]->dim;
252     int partvals = 1;
253     if (dim<1) goto errout;
254     while(dim>0){
255       partvals *= info->partitions;
256       if(partvals > entries) goto errout;
257       dim--;
258     }
259     info->partvals = partvals;
260   }
261
262   return(info);
263  errout:
264   res0_free_info(info);
265   return(NULL);
266 }
267
268 vorbis_look_residue *res0_look(vorbis_dsp_state *vd,
269                                vorbis_info_residue *vr){
270   vorbis_info_residue0 *info=(vorbis_info_residue0 *)vr;
271   vorbis_look_residue0 *look=_ogg_calloc(1,sizeof(*look));
272   codec_setup_info     *ci=vd->vi->codec_setup;
273
274   int j,k,acc=0;
275   int dim;
276   int maxstage=0;
277   look->info=info;
278
279   look->parts=info->partitions;
280   look->fullbooks=ci->fullbooks;
281   look->phrasebook=ci->fullbooks+info->groupbook;
282   dim=look->phrasebook->dim;
283
284   look->partbooks=_ogg_calloc(look->parts,sizeof(*look->partbooks));
285
286   for(j=0;j<look->parts;j++){
287     int stages=ilog(info->secondstages[j]);
288     if(stages){
289       if(stages>maxstage)maxstage=stages;
290       look->partbooks[j]=_ogg_calloc(stages,sizeof(*look->partbooks[j]));
291       for(k=0;k<stages;k++)
292         if(info->secondstages[j]&(1<<k)){
293           look->partbooks[j][k]=ci->fullbooks+info->booklist[acc++];
294 #ifdef TRAIN_RES
295           look->training_data[k][j]=_ogg_calloc(look->partbooks[j][k]->entries,
296                                            sizeof(***look->training_data));
297 #endif
298         }
299     }
300   }
301
302   look->partvals=1;
303   for(j=0;j<dim;j++)
304       look->partvals*=look->parts;
305
306   look->stages=maxstage;
307   look->decodemap=_ogg_malloc(look->partvals*sizeof(*look->decodemap));
308   for(j=0;j<look->partvals;j++){
309     long val=j;
310     long mult=look->partvals/look->parts;
311     look->decodemap[j]=_ogg_malloc(dim*sizeof(*look->decodemap[j]));
312     for(k=0;k<dim;k++){
313       long deco=val/mult;
314       val-=deco*mult;
315       mult/=look->parts;
316       look->decodemap[j][k]=deco;
317     }
318   }
319 #if defined(TRAIN_RES) || defined (TRAIN_RESAUX)
320   {
321     static int train_seq=0;
322     look->train_seq=train_seq++;
323   }
324 #endif
325   return(look);
326 }
327
328 /* break an abstraction and copy some code for performance purposes */
329 static int local_book_besterror(codebook *book,int *a){
330   int dim=book->dim;
331   int i,j,o;
332   int minval=book->minval;
333   int del=book->delta;
334   int qv=book->quantvals;
335   int ze=(qv>>1);
336   int index=0;
337   /* assumes integer/centered encoder codebook maptype 1 no more than dim 8 */
338   int p[8]={0,0,0,0,0,0,0,0};
339
340   if(del!=1){
341     for(i=0,o=dim;i<dim;i++){
342       int v = (a[--o]-minval+(del>>1))/del;
343       int m = (v<ze ? ((ze-v)<<1)-1 : ((v-ze)<<1));
344       index = index*qv+ (m<0?0:(m>=qv?qv-1:m));
345       p[o]=v*del+minval;
346     }
347   }else{
348     for(i=0,o=dim;i<dim;i++){
349       int v = a[--o]-minval;
350       int m = (v<ze ? ((ze-v)<<1)-1 : ((v-ze)<<1));
351       index = index*qv+ (m<0?0:(m>=qv?qv-1:m));
352       p[o]=v*del+minval;
353     }
354   }
355
356   if(book->c->lengthlist[index]<=0){
357     const static_codebook *c=book->c;
358     int best=-1;
359     /* assumes integer/centered encoder codebook maptype 1 no more than dim 8 */
360     int e[8]={0,0,0,0,0,0,0,0};
361     int maxval = book->minval + book->delta*(book->quantvals-1);
362     for(i=0;i<book->entries;i++){
363       if(c->lengthlist[i]>0){
364         int this=0;
365         for(j=0;j<dim;j++){
366           int val=(e[j]-a[j]);
367           this+=val*val;
368         }
369         if(best==-1 || this<best){
370           memcpy(p,e,sizeof(p));
371           best=this;
372           index=i;
373         }
374       }
375       /* assumes the value patterning created by the tools in vq/ */
376       j=0;
377       while(e[j]>=maxval)
378         e[j++]=0;
379       if(e[j]>=0)
380         e[j]+=book->delta;
381       e[j]= -e[j];
382     }
383   }
384
385   if(index>-1){
386     for(i=0;i<dim;i++)
387       *a++ -= p[i];
388   }
389
390   return(index);
391 }
392
393 static int _encodepart(oggpack_buffer *opb,int *vec, int n,
394                        codebook *book,long *acc){
395   int i,bits=0;
396   int dim=book->dim;
397   int step=n/dim;
398
399   for(i=0;i<step;i++){
400     int entry=local_book_besterror(book,vec+i*dim);
401
402 #ifdef TRAIN_RES
403     if(entry>=0)
404       acc[entry]++;
405 #endif
406
407     bits+=vorbis_book_encode(book,entry,opb);
408
409   }
410
411   return(bits);
412 }
413
414 static long **_01class(vorbis_block *vb,vorbis_look_residue *vl,
415                        int **in,int ch){
416   long i,j,k;
417   vorbis_look_residue0 *look=(vorbis_look_residue0 *)vl;
418   vorbis_info_residue0 *info=look->info;
419
420   /* move all this setup out later */
421   int samples_per_partition=info->grouping;
422   int possible_partitions=info->partitions;
423   int n=info->end-info->begin;
424
425   int partvals=n/samples_per_partition;
426   long **partword=_vorbis_block_alloc(vb,ch*sizeof(*partword));
427   float scale=100./samples_per_partition;
428
429   /* we find the partition type for each partition of each
430      channel.  We'll go back and do the interleaved encoding in a
431      bit.  For now, clarity */
432
433   for(i=0;i<ch;i++){
434     partword[i]=_vorbis_block_alloc(vb,n/samples_per_partition*sizeof(*partword[i]));
435     memset(partword[i],0,n/samples_per_partition*sizeof(*partword[i]));
436   }
437
438   for(i=0;i<partvals;i++){
439     int offset=i*samples_per_partition+info->begin;
440     for(j=0;j<ch;j++){
441       int max=0;
442       int ent=0;
443       for(k=0;k<samples_per_partition;k++){
444         if(abs(in[j][offset+k])>max)max=abs(in[j][offset+k]);
445         ent+=abs(in[j][offset+k]);
446       }
447       ent*=scale;
448
449       for(k=0;k<possible_partitions-1;k++)
450         if(max<=info->classmetric1[k] &&
451            (info->classmetric2[k]<0 || ent<info->classmetric2[k]))
452           break;
453
454       partword[j][i]=k;
455     }
456   }
457
458 #ifdef TRAIN_RESAUX
459   {
460     FILE *of;
461     char buffer[80];
462
463     for(i=0;i<ch;i++){
464       sprintf(buffer,"resaux_%d.vqd",look->train_seq);
465       of=fopen(buffer,"a");
466       for(j=0;j<partvals;j++)
467         fprintf(of,"%ld, ",partword[i][j]);
468       fprintf(of,"\n");
469       fclose(of);
470     }
471   }
472 #endif
473   look->frames++;
474
475   return(partword);
476 }
477
478 /* designed for stereo or other modes where the partition size is an
479    integer multiple of the number of channels encoded in the current
480    submap */
481 static long **_2class(vorbis_block *vb,vorbis_look_residue *vl,int **in,
482                       int ch){
483   long i,j,k,l;
484   vorbis_look_residue0 *look=(vorbis_look_residue0 *)vl;
485   vorbis_info_residue0 *info=look->info;
486
487   /* move all this setup out later */
488   int samples_per_partition=info->grouping;
489   int possible_partitions=info->partitions;
490   int n=info->end-info->begin;
491
492   int partvals=n/samples_per_partition;
493   long **partword=_vorbis_block_alloc(vb,sizeof(*partword));
494
495 #if defined(TRAIN_RES) || defined (TRAIN_RESAUX)
496   FILE *of;
497   char buffer[80];
498 #endif
499
500   partword[0]=_vorbis_block_alloc(vb,partvals*sizeof(*partword[0]));
501   memset(partword[0],0,partvals*sizeof(*partword[0]));
502
503   for(i=0,l=info->begin/ch;i<partvals;i++){
504     int magmax=0;
505     int angmax=0;
506     for(j=0;j<samples_per_partition;j+=ch){
507       if(abs(in[0][l])>magmax)magmax=abs(in[0][l]);
508       for(k=1;k<ch;k++)
509         if(abs(in[k][l])>angmax)angmax=abs(in[k][l]);
510       l++;
511     }
512
513     for(j=0;j<possible_partitions-1;j++)
514       if(magmax<=info->classmetric1[j] &&
515          angmax<=info->classmetric2[j])
516         break;
517
518     partword[0][i]=j;
519
520   }
521
522 #ifdef TRAIN_RESAUX
523   sprintf(buffer,"resaux_%d.vqd",look->train_seq);
524   of=fopen(buffer,"a");
525   for(i=0;i<partvals;i++)
526     fprintf(of,"%ld, ",partword[0][i]);
527   fprintf(of,"\n");
528   fclose(of);
529 #endif
530
531   look->frames++;
532
533   return(partword);
534 }
535
536 static int _01forward(oggpack_buffer *opb,
537                       vorbis_block *vb,vorbis_look_residue *vl,
538                       int **in,int ch,
539                       long **partword,
540                       int (*encode)(oggpack_buffer *,int *,int,
541                                     codebook *,long *),
542                       int submap){
543   long i,j,k,s;
544   vorbis_look_residue0 *look=(vorbis_look_residue0 *)vl;
545   vorbis_info_residue0 *info=look->info;
546
547 #ifdef TRAIN_RES
548   look->submap=submap;
549 #endif
550
551   /* move all this setup out later */
552   int samples_per_partition=info->grouping;
553   int possible_partitions=info->partitions;
554   int partitions_per_word=look->phrasebook->dim;
555   int n=info->end-info->begin;
556
557   int partvals=n/samples_per_partition;
558   long resbits[128];
559   long resvals[128];
560
561 #ifdef TRAIN_RES
562   for(i=0;i<ch;i++)
563     for(j=info->begin;j<info->end;j++){
564       if(in[i][j]>look->tmax)look->tmax=in[i][j];
565       if(in[i][j]<look->tmin)look->tmin=in[i][j];
566     }
567 #endif
568
569   memset(resbits,0,sizeof(resbits));
570   memset(resvals,0,sizeof(resvals));
571
572   /* we code the partition words for each channel, then the residual
573      words for a partition per channel until we've written all the
574      residual words for that partition word.  Then write the next
575      partition channel words... */
576
577   for(s=0;s<look->stages;s++){
578
579     for(i=0;i<partvals;){
580
581       /* first we encode a partition codeword for each channel */
582       if(s==0){
583         for(j=0;j<ch;j++){
584           long val=partword[j][i];
585           for(k=1;k<partitions_per_word;k++){
586             val*=possible_partitions;
587             if(i+k<partvals)
588               val+=partword[j][i+k];
589           }
590
591           /* training hack */
592           if(val<look->phrasebook->entries)
593             look->phrasebits+=vorbis_book_encode(look->phrasebook,val,opb);
594 #if 0 /*def TRAIN_RES*/
595           else
596             fprintf(stderr,"!");
597 #endif
598
599         }
600       }
601
602       /* now we encode interleaved residual values for the partitions */
603       for(k=0;k<partitions_per_word && i<partvals;k++,i++){
604         long offset=i*samples_per_partition+info->begin;
605
606         for(j=0;j<ch;j++){
607           if(s==0)resvals[partword[j][i]]+=samples_per_partition;
608           if(info->secondstages[partword[j][i]]&(1<<s)){
609             codebook *statebook=look->partbooks[partword[j][i]][s];
610             if(statebook){
611               int ret;
612               long *accumulator=NULL;
613
614 #ifdef TRAIN_RES
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 #endif
627
628               ret=encode(opb,in[j]+offset,samples_per_partition,
629                          statebook,accumulator);
630
631               look->postbits+=ret;
632               resbits[partword[j][i]]+=ret;
633             }
634           }
635         }
636       }
637     }
638   }
639
640   /*{
641     long total=0;
642     long totalbits=0;
643     fprintf(stderr,"%d :: ",vb->mode);
644     for(k=0;k<possible_partitions;k++){
645     fprintf(stderr,"%ld/%1.2g, ",resvals[k],(float)resbits[k]/resvals[k]);
646     total+=resvals[k];
647     totalbits+=resbits[k];
648     }
649
650     fprintf(stderr,":: %ld:%1.2g\n",total,(double)totalbits/total);
651     }*/
652
653   return(0);
654 }
655
656 /* a truncated packet here just means 'stop working'; it's not an error */
657 static int _01inverse(vorbis_block *vb,vorbis_look_residue *vl,
658                       float **in,int ch,
659                       long (*decodepart)(codebook *, float *,
660                                          oggpack_buffer *,int)){
661
662   long i,j,k,l,s;
663   vorbis_look_residue0 *look=(vorbis_look_residue0 *)vl;
664   vorbis_info_residue0 *info=look->info;
665
666   /* move all this setup out later */
667   int samples_per_partition=info->grouping;
668   int partitions_per_word=look->phrasebook->dim;
669   int max=vb->pcmend>>1;
670   int end=(info->end<max?info->end:max);
671   int n=end-info->begin;
672
673   if(n>0){
674     int partvals=n/samples_per_partition;
675     int partwords=(partvals+partitions_per_word-1)/partitions_per_word;
676     int ***partword=alloca(ch*sizeof(*partword));
677
678     for(j=0;j<ch;j++)
679       partword[j]=_vorbis_block_alloc(vb,partwords*sizeof(*partword[j]));
680
681     for(s=0;s<look->stages;s++){
682
683       /* each loop decodes on partition codeword containing
684          partitions_per_word partitions */
685       for(i=0,l=0;i<partvals;l++){
686         if(s==0){
687           /* fetch the partition word for each channel */
688           for(j=0;j<ch;j++){
689             int temp=vorbis_book_decode(look->phrasebook,&vb->opb);
690
691             if(temp==-1 || temp>=info->partvals)goto eopbreak;
692             partword[j][l]=look->decodemap[temp];
693             if(partword[j][l]==NULL)goto errout;
694           }
695         }
696
697         /* now we decode residual values for the partitions */
698         for(k=0;k<partitions_per_word && i<partvals;k++,i++)
699           for(j=0;j<ch;j++){
700             long offset=info->begin+i*samples_per_partition;
701             if(info->secondstages[partword[j][l][k]]&(1<<s)){
702               codebook *stagebook=look->partbooks[partword[j][l][k]][s];
703               if(stagebook){
704                 if(decodepart(stagebook,in[j]+offset,&vb->opb,
705                               samples_per_partition)==-1)goto eopbreak;
706               }
707             }
708           }
709       }
710     }
711   }
712  errout:
713  eopbreak:
714   return(0);
715 }
716
717 int res0_inverse(vorbis_block *vb,vorbis_look_residue *vl,
718                  float **in,int *nonzero,int ch){
719   int i,used=0;
720   for(i=0;i<ch;i++)
721     if(nonzero[i])
722       in[used++]=in[i];
723   if(used)
724     return(_01inverse(vb,vl,in,used,vorbis_book_decodevs_add));
725   else
726     return(0);
727 }
728
729 int res1_forward(oggpack_buffer *opb,vorbis_block *vb,vorbis_look_residue *vl,
730                  int **in,int *nonzero,int ch, long **partword, int submap){
731   int i,used=0;
732   for(i=0;i<ch;i++)
733     if(nonzero[i])
734       in[used++]=in[i];
735
736   if(used){
737     return _01forward(opb,vb,vl,in,used,partword,_encodepart,submap);
738   }else{
739     return(0);
740   }
741 }
742
743 long **res1_class(vorbis_block *vb,vorbis_look_residue *vl,
744                   int **in,int *nonzero,int ch){
745   int i,used=0;
746   for(i=0;i<ch;i++)
747     if(nonzero[i])
748       in[used++]=in[i];
749   if(used)
750     return(_01class(vb,vl,in,used));
751   else
752     return(0);
753 }
754
755 int res1_inverse(vorbis_block *vb,vorbis_look_residue *vl,
756                  float **in,int *nonzero,int ch){
757   int i,used=0;
758   for(i=0;i<ch;i++)
759     if(nonzero[i])
760       in[used++]=in[i];
761   if(used)
762     return(_01inverse(vb,vl,in,used,vorbis_book_decodev_add));
763   else
764     return(0);
765 }
766
767 long **res2_class(vorbis_block *vb,vorbis_look_residue *vl,
768                   int **in,int *nonzero,int ch){
769   int i,used=0;
770   for(i=0;i<ch;i++)
771     if(nonzero[i])used++;
772   if(used)
773     return(_2class(vb,vl,in,ch));
774   else
775     return(0);
776 }
777
778 /* res2 is slightly more different; all the channels are interleaved
779    into a single vector and encoded. */
780
781 int res2_forward(oggpack_buffer *opb,
782                  vorbis_block *vb,vorbis_look_residue *vl,
783                  int **in,int *nonzero,int ch, long **partword,int submap){
784   long i,j,k,n=vb->pcmend/2,used=0;
785
786   /* don't duplicate the code; use a working vector hack for now and
787      reshape ourselves into a single channel res1 */
788   /* ugly; reallocs for each coupling pass :-( */
789   int *work=_vorbis_block_alloc(vb,ch*n*sizeof(*work));
790   for(i=0;i<ch;i++){
791     int *pcm=in[i];
792     if(nonzero[i])used++;
793     for(j=0,k=i;j<n;j++,k+=ch)
794       work[k]=pcm[j];
795   }
796
797   if(used){
798     return _01forward(opb,vb,vl,&work,1,partword,_encodepart,submap);
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 };