Continued preecho tuning/fixes. Gone to average dB with even/odd
[platform/upstream/libvorbis.git] / lib / mapping0.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-2002             *
9  * by the XIPHOPHORUS Company http://www.xiph.org/                  *
10  *                                                                  *
11  ********************************************************************
12
13  function: channel mapping 0 implementation
14  last mod: $Id: mapping0.c,v 1.47 2002/03/24 21:04:00 xiphmont Exp $
15
16  ********************************************************************/
17
18 #include <stdlib.h>
19 #include <stdio.h>
20 #include <string.h>
21 #include <math.h>
22 #include <ogg/ogg.h>
23 #include "vorbis/codec.h"
24 #include "codec_internal.h"
25 #include "codebook.h"
26 #include "window.h"
27 #include "registry.h"
28 #include "psy.h"
29 #include "misc.h"
30
31 /* simplistic, wasteful way of doing this (unique lookup for each
32    mode/submapping); there should be a central repository for
33    identical lookups.  That will require minor work, so I'm putting it
34    off as low priority.
35
36    Why a lookup for each backend in a given mode?  Because the
37    blocksize is set by the mode, and low backend lookups may require
38    parameters from other areas of the mode/mapping */
39
40 extern int analysis_noisy;
41
42 typedef struct {
43   drft_lookup fft_look;
44   vorbis_info_mode *mode;
45   vorbis_info_mapping0 *map;
46
47   vorbis_look_time **time_look;
48   vorbis_look_floor **floor_look;
49
50   vorbis_look_residue **residue_look;
51   vorbis_look_psy *psy_look[2];
52
53   vorbis_func_time **time_func;
54   vorbis_func_floor **floor_func;
55   vorbis_func_residue **residue_func;
56
57   int ch;
58   long lastframe; /* if a different mode is called, we need to 
59                      invalidate decay */
60 } vorbis_look_mapping0;
61
62 static vorbis_info_mapping *mapping0_copy_info(vorbis_info_mapping *vm){
63   vorbis_info_mapping0 *info=(vorbis_info_mapping0 *)vm;
64   vorbis_info_mapping0 *ret=_ogg_malloc(sizeof(*ret));
65   memcpy(ret,info,sizeof(*ret));
66   return(ret);
67 }
68
69 static void mapping0_free_info(vorbis_info_mapping *i){
70   vorbis_info_mapping0 *info=(vorbis_info_mapping0 *)i;
71   if(info){
72     memset(info,0,sizeof(*info));
73     _ogg_free(info);
74   }
75 }
76
77 static void mapping0_free_look(vorbis_look_mapping *look){
78   int i;
79   vorbis_look_mapping0 *l=(vorbis_look_mapping0 *)look;
80   if(l){
81     drft_clear(&l->fft_look);
82
83     for(i=0;i<l->map->submaps;i++){
84       l->time_func[i]->free_look(l->time_look[i]);
85       l->floor_func[i]->free_look(l->floor_look[i]);
86       l->residue_func[i]->free_look(l->residue_look[i]);
87     }
88     if(l->psy_look[1] && l->psy_look[1]!=l->psy_look[0]){
89       _vp_psy_clear(l->psy_look[1]);
90       _ogg_free(l->psy_look[1]);
91     }
92     if(l->psy_look[0]){
93       _vp_psy_clear(l->psy_look[0]);
94       _ogg_free(l->psy_look[0]);
95     }
96     _ogg_free(l->time_func);
97     _ogg_free(l->floor_func);
98     _ogg_free(l->residue_func);
99     _ogg_free(l->time_look);
100     _ogg_free(l->floor_look);
101     _ogg_free(l->residue_look);
102     memset(l,0,sizeof(*l));
103     _ogg_free(l);
104   }
105 }
106
107 static vorbis_look_mapping *mapping0_look(vorbis_dsp_state *vd,vorbis_info_mode *vm,
108                           vorbis_info_mapping *m){
109   int i;
110   vorbis_info          *vi=vd->vi;
111   codec_setup_info     *ci=vi->codec_setup;
112   vorbis_look_mapping0 *look=_ogg_calloc(1,sizeof(*look));
113   vorbis_info_mapping0 *info=look->map=(vorbis_info_mapping0 *)m;
114   look->mode=vm;
115   
116   look->time_look=_ogg_calloc(info->submaps,sizeof(*look->time_look));
117   look->floor_look=_ogg_calloc(info->submaps,sizeof(*look->floor_look));
118
119   look->residue_look=_ogg_calloc(info->submaps,sizeof(*look->residue_look));
120
121   look->time_func=_ogg_calloc(info->submaps,sizeof(*look->time_func));
122   look->floor_func=_ogg_calloc(info->submaps,sizeof(*look->floor_func));
123   look->residue_func=_ogg_calloc(info->submaps,sizeof(*look->residue_func));
124   
125   for(i=0;i<info->submaps;i++){
126     int timenum=info->timesubmap[i];
127     int floornum=info->floorsubmap[i];
128     int resnum=info->residuesubmap[i];
129
130     look->time_func[i]=_time_P[ci->time_type[timenum]];
131     look->time_look[i]=look->time_func[i]->
132       look(vd,vm,ci->time_param[timenum]);
133     look->floor_func[i]=_floor_P[ci->floor_type[floornum]];
134     look->floor_look[i]=look->floor_func[i]->
135       look(vd,vm,ci->floor_param[floornum]);
136     look->residue_func[i]=_residue_P[ci->residue_type[resnum]];
137     look->residue_look[i]=look->residue_func[i]->
138       look(vd,vm,ci->residue_param[resnum]);
139     
140   }
141   if(ci->psys && vd->analysisp){
142     if(info->psy[0] != info->psy[1]){
143
144       int psynum=info->psy[0];
145       look->psy_look[0]=_ogg_calloc(1,sizeof(*look->psy_look[0]));      
146       _vp_psy_init(look->psy_look[0],ci->psy_param[psynum],
147                    &ci->psy_g_param,
148                    ci->blocksizes[vm->blockflag]/2,vi->rate);
149
150       psynum=info->psy[1];
151       look->psy_look[1]=_ogg_calloc(1,sizeof(*look->psy_look[1]));      
152       _vp_psy_init(look->psy_look[1],ci->psy_param[psynum],
153                    &ci->psy_g_param,
154                    ci->blocksizes[vm->blockflag]/2,vi->rate);
155     }else{
156
157       int psynum=info->psy[0];
158       look->psy_look[0]=_ogg_calloc(1,sizeof(*look->psy_look[0]));      
159       look->psy_look[1]=look->psy_look[0];
160       _vp_psy_init(look->psy_look[0],ci->psy_param[psynum],
161                    &ci->psy_g_param,
162                    ci->blocksizes[vm->blockflag]/2,vi->rate);
163
164     }
165   }
166
167   look->ch=vi->channels;
168
169   if(vd->analysisp)drft_init(&look->fft_look,ci->blocksizes[vm->blockflag]);
170   return(look);
171 }
172
173 static int ilog2(unsigned int v){
174   int ret=0;
175   while(v>1){
176     ret++;
177     v>>=1;
178   }
179   return(ret);
180 }
181
182 static void mapping0_pack(vorbis_info *vi,vorbis_info_mapping *vm,
183                           oggpack_buffer *opb){
184   int i;
185   vorbis_info_mapping0 *info=(vorbis_info_mapping0 *)vm;
186
187   /* another 'we meant to do it this way' hack...  up to beta 4, we
188      packed 4 binary zeros here to signify one submapping in use.  We
189      now redefine that to mean four bitflags that indicate use of
190      deeper features; bit0:submappings, bit1:coupling,
191      bit2,3:reserved. This is backward compatable with all actual uses
192      of the beta code. */
193
194   if(info->submaps>1){
195     oggpack_write(opb,1,1);
196     oggpack_write(opb,info->submaps-1,4);
197   }else
198     oggpack_write(opb,0,1);
199
200   if(info->coupling_steps>0){
201     oggpack_write(opb,1,1);
202     oggpack_write(opb,info->coupling_steps-1,8);
203     
204     for(i=0;i<info->coupling_steps;i++){
205       oggpack_write(opb,info->coupling_mag[i],ilog2(vi->channels));
206       oggpack_write(opb,info->coupling_ang[i],ilog2(vi->channels));
207     }
208   }else
209     oggpack_write(opb,0,1);
210   
211   oggpack_write(opb,0,2); /* 2,3:reserved */
212
213   /* we don't write the channel submappings if we only have one... */
214   if(info->submaps>1){
215     for(i=0;i<vi->channels;i++)
216       oggpack_write(opb,info->chmuxlist[i],4);
217   }
218   for(i=0;i<info->submaps;i++){
219     oggpack_write(opb,info->timesubmap[i],8);
220     oggpack_write(opb,info->floorsubmap[i],8);
221     oggpack_write(opb,info->residuesubmap[i],8);
222   }
223 }
224
225 /* also responsible for range checking */
226 static vorbis_info_mapping *mapping0_unpack(vorbis_info *vi,oggpack_buffer *opb){
227   int i;
228   vorbis_info_mapping0 *info=_ogg_calloc(1,sizeof(*info));
229   codec_setup_info     *ci=vi->codec_setup;
230   memset(info,0,sizeof(*info));
231
232   if(oggpack_read(opb,1))
233     info->submaps=oggpack_read(opb,4)+1;
234   else
235     info->submaps=1;
236
237   if(oggpack_read(opb,1)){
238     info->coupling_steps=oggpack_read(opb,8)+1;
239
240     for(i=0;i<info->coupling_steps;i++){
241       int testM=info->coupling_mag[i]=oggpack_read(opb,ilog2(vi->channels));
242       int testA=info->coupling_ang[i]=oggpack_read(opb,ilog2(vi->channels));
243
244       if(testM<0 || 
245          testA<0 || 
246          testM==testA || 
247          testM>=vi->channels ||
248          testA>=vi->channels) goto err_out;
249     }
250
251   }
252
253   if(oggpack_read(opb,2)>0)goto err_out; /* 2,3:reserved */
254     
255   if(info->submaps>1){
256     for(i=0;i<vi->channels;i++){
257       info->chmuxlist[i]=oggpack_read(opb,4);
258       if(info->chmuxlist[i]>=info->submaps)goto err_out;
259     }
260   }
261   for(i=0;i<info->submaps;i++){
262     info->timesubmap[i]=oggpack_read(opb,8);
263     if(info->timesubmap[i]>=ci->times)goto err_out;
264     info->floorsubmap[i]=oggpack_read(opb,8);
265     if(info->floorsubmap[i]>=ci->floors)goto err_out;
266     info->residuesubmap[i]=oggpack_read(opb,8);
267     if(info->residuesubmap[i]>=ci->residues)goto err_out;
268   }
269
270   return info;
271
272  err_out:
273   mapping0_free_info(info);
274   return(NULL);
275 }
276
277 #include "os.h"
278 #include "lpc.h"
279 #include "lsp.h"
280 #include "envelope.h"
281 #include "mdct.h"
282 #include "psy.h"
283 #include "scales.h"
284
285 /* no time mapping implementation for now */
286 static long seq=0;
287
288 static int mapping0_forward(vorbis_block *vb,vorbis_look_mapping *l){
289   vorbis_dsp_state      *vd=vb->vd;
290   vorbis_info           *vi=vd->vi;
291   codec_setup_info      *ci=vi->codec_setup;
292   backend_lookup_state  *b=vb->vd->backend_state;
293   bitrate_manager_state *bm=&b->bms;
294   vorbis_look_mapping0  *look=(vorbis_look_mapping0 *)l;
295   vorbis_info_mapping0  *info=look->map;
296   vorbis_info_mode      *mode=look->mode;
297   vorbis_block_internal *vbi=(vorbis_block_internal *)vb->internal;
298   int                    n=vb->pcmend;
299   int i,j;
300   int   *nonzero=alloca(sizeof(*nonzero)*vi->channels);
301
302   float *work=_vorbis_block_alloc(vb,n*sizeof(*work));
303
304   float global_ampmax=vbi->ampmax;
305   float *local_ampmax=alloca(sizeof(*local_ampmax)*vi->channels);
306   int blocktype=vbi->blocktype;
307
308   /* we differentiate between short and long block types to help the
309      masking engine; the window shapes also matter.
310      impulse block (a short block in which an impulse occurs)
311      padding block (a short block that pads between a transitional 
312           long block and an impulse block, or vice versa)
313      transition block (the wqeird one; a long block with the transition 
314           window; affects bass/midrange response and that must be 
315           accounted for in masking) 
316      long block (run of the mill long block)
317   */
318
319   for(i=0;i<vi->channels;i++){
320     float scale=4.f/n;
321     float scale_dB;
322
323     /* the following makes things clearer to *me* anyway */
324     float *pcm     =vb->pcm[i]; 
325     float *fft     =work;
326     float *logfft  =pcm+n/2;
327
328     /*float *res     =pcm;
329     float *mdct    =pcm;
330     float *codedflr=pcm+n/2;
331     float *logmax  =work;
332     float *logmask =work+n/2;*/
333
334     scale_dB=todB(&scale);
335     /*_analysis_output_always("pcm",seq+i,pcm,n,0,0,total-n/2);*/
336
337     /* window the PCM data */
338     _vorbis_apply_window(pcm,b->window,ci->blocksizes,vb->lW,vb->W,vb->nW);
339     memcpy(fft,pcm,sizeof(*fft)*n);
340     
341     /*_analysis_output_always("windowed",seq+i,pcm,n,0,0,total-n/2);*/
342
343     /* transform the PCM data */
344     /* only MDCT right now.... */
345     mdct_forward(b->transform[vb->W][0],pcm,pcm);
346     
347     /* FFT yields more accurate tonal estimation (not phase sensitive) */
348     drft_forward(&look->fft_look,fft);
349     fft[0]*=scale;
350     logfft[0]=todB(fft);
351     local_ampmax[i]=logfft[0];
352     for(j=1;j<n-1;j+=2){
353       float temp=fft[j]*fft[j]+fft[j+1]*fft[j+1];
354       temp=logfft[(j+1)>>1]=scale_dB+.5f*todB(&temp);
355       if(temp>local_ampmax[i])local_ampmax[i]=temp;
356     }
357
358     if(local_ampmax[i]>0.f)local_ampmax[i]=0.f;
359     if(local_ampmax[i]>global_ampmax)global_ampmax=local_ampmax[i];
360
361     /*_analysis_output("fft",seq+i,logfft,n/2,1,0);*/
362   }
363
364   for(i=0;i<vi->channels;i++){
365     int submap=info->chmuxlist[i];
366
367     /* the following makes things clearer to *me* anyway */
368     float *mdct    =vb->pcm[i]; 
369     float *res     =mdct;
370     float *codedflr=mdct+n/2;
371     float *logfft  =mdct+n/2;
372
373     float *logmdct =work;
374     float *logmax  =mdct+n/2;
375     float *logmask =work+n/2;
376
377     /*
378     for(j=0;j<n/2;j+=2){
379       logmdct[j>>1]=FABS(mdct+j)+FABS(mdct+j+1);
380       logmdct[j>>1]=todB(logmdct+(j>>1));
381     }
382     _analysis_output_always("mdct2",seq+i,logmdct,n/4,1,0,total-n/2);*/
383
384
385     for(j=0;j<n/2;j++)
386       logmdct[j]=todB(mdct+j);
387     /*_analysis_output_always("mdct",seq+i,logmdct,n/2,1,0,total-n/2);*/
388
389
390     /* perform psychoacoustics; do masking */
391     _vp_compute_mask(look->psy_look[blocktype],
392                      logfft, /* -> logmax */
393                      logmdct,
394                      logmask,
395                      global_ampmax,
396                      local_ampmax[i],
397                      bm->avgnoise);
398
399     /*_analysis_output("mask",seq+i,logmask,n/2,1,0);*/
400     /* perform floor encoding */
401     nonzero[i]=look->floor_func[submap]->
402       forward(vb,look->floor_look[submap],
403               mdct,
404               logmdct,
405               logmask,
406               logmax,
407
408               codedflr);
409
410
411     _vp_remove_floor(look->psy_look[blocktype],
412                      mdct,
413                      codedflr,
414                      res);
415
416     /*for(j=0;j<n/2;j++)
417       if(fabs(res[j])>1200){
418         analysis_noisy=1;
419         fprintf(stderr,"%ld ",seq+i);
420         }*/
421
422     /*_analysis_output("codedflr",seq+i,codedflr,n/2,1,1);*/
423       
424   }
425
426   vbi->ampmax=global_ampmax;
427
428   /* partition based prequantization and channel coupling */
429   /* Steps in prequant and coupling:
430
431      classify by |mag| across all pcm vectors 
432
433      down-couple/down-quantize from perfect residue ->  quantized vector 
434      
435      do{ 
436         encode quantized vector; add encoded values to 'so-far' vector
437         more? [not yet at bitrate/not yet at target]
438           yes{
439               down-couple/down-quantize from perfect-'so-far' -> 
440                 quantized vector; when subtracting coupling, 
441                 account for +/- out-of-phase component
442           }no{  
443               break
444           }
445      }
446      done.
447
448      quantization in each iteration is done (after circular normalization 
449      in coupling) using a by-iteration quantization granule value.
450   */
451    
452   {
453     float  **pcm=vb->pcm;
454     float  **quantized=alloca(sizeof(*quantized)*vi->channels);
455     float  **sofar=alloca(sizeof(*sofar)*vi->channels);
456
457     long  ***classifications=alloca(sizeof(*classifications)*info->submaps);
458     float ***qbundle=alloca(sizeof(*qbundle)*info->submaps);
459     float ***pcmbundle=alloca(sizeof(*pcmbundle)*info->submaps);
460     float ***sobundle=alloca(sizeof(*sobundle)*info->submaps);
461     int    **zerobundle=alloca(sizeof(*zerobundle)*info->submaps);
462     int     *chbundle=alloca(sizeof(*chbundle)*info->submaps);
463     int      chcounter=0;
464
465     /* play a little loose with this abstraction */
466     int   quant_passes=ci->coupling_passes;
467
468     for(i=0;i<vi->channels;i++){
469       quantized[i]=_vorbis_block_alloc(vb,n*sizeof(*sofar[i]));
470       sofar[i]=quantized[i]+n/2;
471       memset(sofar[i],0,sizeof(*sofar[i])*n/2);
472     }
473
474     qbundle[0]=alloca(sizeof(*qbundle[0])*vi->channels);
475     pcmbundle[0]=alloca(sizeof(*pcmbundle[0])*vi->channels);
476     sobundle[0]=alloca(sizeof(*sobundle[0])*vi->channels);
477     zerobundle[0]=alloca(sizeof(*zerobundle[0])*vi->channels);
478
479     /* initial down-quantized coupling */
480     
481     if(info->coupling_steps==0){
482       /* this assumes all or nothing coupling right now.  it should pass
483          through any channels left uncoupled, but it doesn't do that now */
484       for(i=0;i<vi->channels;i++){
485         float *lpcm=pcm[i];
486         float *lqua=quantized[i];
487         for(j=0;j<n/2;j++)
488           lqua[j]=lpcm[j];
489       }
490     }else{
491       _vp_quantize_couple(look->psy_look[blocktype],
492                           info,
493                           pcm,
494                           sofar,
495                           quantized,
496                           nonzero,
497                           0);
498     }
499
500     /* classify, by submap */
501
502     for(i=0;i<info->submaps;i++){
503       int ch_in_bundle=0;
504       qbundle[i]=qbundle[0]+chcounter;
505       sobundle[i]=sobundle[0]+chcounter;
506       zerobundle[i]=zerobundle[0]+chcounter;
507
508       for(j=0;j<vi->channels;j++){
509         if(info->chmuxlist[j]==i){
510           if(nonzero[j])
511             zerobundle[i][ch_in_bundle]=1;
512           else
513             zerobundle[i][ch_in_bundle]=0;
514           qbundle[i][ch_in_bundle]=quantized[j];
515           pcmbundle[i][ch_in_bundle]=pcm[j];
516           sobundle[i][ch_in_bundle++]=sofar[j];
517         }
518       }
519       chbundle[i]=ch_in_bundle;
520       chcounter+=ch_in_bundle;
521
522       classifications[i]=look->residue_func[i]->
523         class(vb,look->residue_look[i],pcmbundle[i],zerobundle[i],chbundle[i]);
524     }
525
526     /* actual encoding loop; we pack all the iterations to collect
527        management data */
528
529     for(i=0;i<quant_passes;){
530
531       /* perform residue encoding of this pass's quantized residue
532          vector, according residue mapping */
533     
534       for(j=0;j<info->submaps;j++){
535         look->residue_func[j]->
536           forward(vb,look->residue_look[j],
537                   qbundle[j],sobundle[j],zerobundle[j],chbundle[j],
538                   i,classifications[j],vbi->packet_markers);
539         
540       }
541       i++;
542         
543       if(i<quant_passes){
544         /* down-couple/down-quantize from perfect-'so-far' -> 
545          new quantized vector */
546         if(info->coupling_steps==0){
547           /* this assumes all or nothing coupling right now.  it should pass
548              through any channels left uncoupled, but it doesn't do that now */
549           int k;
550           for(k=0;k<vi->channels;k++){
551             float *lpcm=pcm[k];
552             float *lsof=sofar[k];
553             float *lqua=quantized[k];
554             for(j=0;j<n/2;j++)
555               lqua[j]=lpcm[j]-lsof[j];
556           }
557         }else{
558
559           _vp_quantize_couple(look->psy_look[blocktype],
560                               info,
561                               pcm,
562                               sofar,
563                               quantized,
564                               nonzero,
565                               i);
566         }
567       }
568     }
569     seq+=vi->channels;
570   } 
571
572   /*total+=ci->blocksizes[vb->W]/4+ci->blocksizes[vb->nW]/4;*/
573   look->lastframe=vb->sequence;
574   return(0);
575 }
576
577 static int mapping0_inverse(vorbis_block *vb,vorbis_look_mapping *l){
578   vorbis_dsp_state     *vd=vb->vd;
579   vorbis_info          *vi=vd->vi;
580   codec_setup_info     *ci=vi->codec_setup;
581   backend_lookup_state *b=vd->backend_state;
582   vorbis_look_mapping0 *look=(vorbis_look_mapping0 *)l;
583   vorbis_info_mapping0 *info=look->map;
584   vorbis_info_mode     *mode=look->mode;
585   int                   i,j;
586   long                  n=vb->pcmend=ci->blocksizes[vb->W];
587
588   float **pcmbundle=alloca(sizeof(*pcmbundle)*vi->channels);
589   int    *zerobundle=alloca(sizeof(*zerobundle)*vi->channels);
590
591   int   *nonzero  =alloca(sizeof(*nonzero)*vi->channels);
592   void **floormemo=alloca(sizeof(*floormemo)*vi->channels);
593   
594   /* time domain information decode (note that applying the
595      information would have to happen later; we'll probably add a
596      function entry to the harness for that later */
597   /* NOT IMPLEMENTED */
598
599   /* recover the spectral envelope; store it in the PCM vector for now */
600   for(i=0;i<vi->channels;i++){
601     int submap=info->chmuxlist[i];
602     floormemo[i]=look->floor_func[submap]->
603       inverse1(vb,look->floor_look[submap]);
604     if(floormemo[i])
605       nonzero[i]=1;
606     else
607       nonzero[i]=0;      
608     memset(vb->pcm[i],0,sizeof(*vb->pcm[i])*n/2);
609   }
610
611   /* channel coupling can 'dirty' the nonzero listing */
612   for(i=0;i<info->coupling_steps;i++){
613     if(nonzero[info->coupling_mag[i]] ||
614        nonzero[info->coupling_ang[i]]){
615       nonzero[info->coupling_mag[i]]=1; 
616       nonzero[info->coupling_ang[i]]=1; 
617     }
618   }
619
620   /* recover the residue into our working vectors */
621   for(i=0;i<info->submaps;i++){
622     int ch_in_bundle=0;
623     for(j=0;j<vi->channels;j++){
624       if(info->chmuxlist[j]==i){
625         if(nonzero[j])
626           zerobundle[ch_in_bundle]=1;
627         else
628           zerobundle[ch_in_bundle]=0;
629         pcmbundle[ch_in_bundle++]=vb->pcm[j];
630       }
631     }
632     
633     look->residue_func[i]->inverse(vb,look->residue_look[i],
634                                    pcmbundle,zerobundle,ch_in_bundle);
635   }
636
637   /* channel coupling */
638   for(i=info->coupling_steps-1;i>=0;i--){
639     float *pcmM=vb->pcm[info->coupling_mag[i]];
640     float *pcmA=vb->pcm[info->coupling_ang[i]];
641
642     for(j=0;j<n/2;j++){
643       float mag=pcmM[j];
644       float ang=pcmA[j];
645
646       if(mag>0)
647         if(ang>0){
648           pcmM[j]=mag;
649           pcmA[j]=mag-ang;
650         }else{
651           pcmA[j]=mag;
652           pcmM[j]=mag+ang;
653         }
654       else
655         if(ang>0){
656           pcmM[j]=mag;
657           pcmA[j]=mag+ang;
658         }else{
659           pcmA[j]=mag;
660           pcmM[j]=mag-ang;
661         }
662     }
663   }
664
665   /* compute and apply spectral envelope */
666   for(i=0;i<vi->channels;i++){
667     float *pcm=vb->pcm[i];
668     int submap=info->chmuxlist[i];
669     look->floor_func[submap]->
670       inverse2(vb,look->floor_look[submap],floormemo[i],pcm);
671   }
672
673   /* transform the PCM data; takes PCM vector, vb; modifies PCM vector */
674   /* only MDCT right now.... */
675   for(i=0;i<vi->channels;i++){
676     float *pcm=vb->pcm[i];
677     mdct_backward(b->transform[vb->W][0],pcm,pcm);
678   }
679
680   /* window the data */
681   for(i=0;i<vi->channels;i++){
682     float *pcm=vb->pcm[i];
683     if(nonzero[i])
684       _vorbis_apply_window(pcm,b->window,ci->blocksizes,vb->lW,vb->W,vb->nW);
685     else
686       for(j=0;j<n;j++)
687         pcm[j]=0.f;
688
689   }
690
691   /* all done! */
692   return(0);
693 }
694
695 /* export hooks */
696 vorbis_func_mapping mapping0_exportbundle={
697   &mapping0_pack,
698   &mapping0_unpack,
699   &mapping0_look,
700   &mapping0_copy_info,
701   &mapping0_free_info,
702   &mapping0_free_look,
703   &mapping0_forward,
704   &mapping0_inverse
705 };
706