sizeof() cleanup. the occasional void * didn't make this easier.
[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-2001             *
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.37 2001/10/02 00:14:31 segher 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 "bitbuffer.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,oggpack_buffer *opb){
183   int i;
184   vorbis_info_mapping0 *info=(vorbis_info_mapping0 *)vm;
185
186   /* another 'we meant to do it this way' hack...  up to beta 4, we
187      packed 4 binary zeros here to signify one submapping in use.  We
188      now redefine that to mean four bitflags that indicate use of
189      deeper features; bit0:submappings, bit1:coupling,
190      bit2,3:reserved. This is backward compatable with all actual uses
191      of the beta code. */
192
193   if(info->submaps>1){
194     oggpack_write(opb,1,1);
195     oggpack_write(opb,info->submaps-1,4);
196   }else
197     oggpack_write(opb,0,1);
198
199   if(info->coupling_steps>0){
200     oggpack_write(opb,1,1);
201     oggpack_write(opb,info->coupling_steps-1,8);
202     
203     for(i=0;i<info->coupling_steps;i++){
204       oggpack_write(opb,info->coupling_mag[i],ilog2(vi->channels));
205       oggpack_write(opb,info->coupling_ang[i],ilog2(vi->channels));
206     }
207   }else
208     oggpack_write(opb,0,1);
209   
210   oggpack_write(opb,0,2); /* 2,3:reserved */
211
212   /* we don't write the channel submappings if we only have one... */
213   if(info->submaps>1){
214     for(i=0;i<vi->channels;i++)
215       oggpack_write(opb,info->chmuxlist[i],4);
216   }
217   for(i=0;i<info->submaps;i++){
218     oggpack_write(opb,info->timesubmap[i],8);
219     oggpack_write(opb,info->floorsubmap[i],8);
220     oggpack_write(opb,info->residuesubmap[i],8);
221   }
222 }
223
224 /* also responsible for range checking */
225 static vorbis_info_mapping *mapping0_unpack(vorbis_info *vi,oggpack_buffer *opb){
226   int i;
227   vorbis_info_mapping0 *info=_ogg_calloc(1,sizeof(*info));
228   codec_setup_info     *ci=vi->codec_setup;
229   memset(info,0,sizeof(*info));
230
231   if(oggpack_read(opb,1))
232     info->submaps=oggpack_read(opb,4)+1;
233   else
234     info->submaps=1;
235
236   if(oggpack_read(opb,1)){
237     info->coupling_steps=oggpack_read(opb,8)+1;
238
239     for(i=0;i<info->coupling_steps;i++){
240       int testM=info->coupling_mag[i]=oggpack_read(opb,ilog2(vi->channels));
241       int testA=info->coupling_ang[i]=oggpack_read(opb,ilog2(vi->channels));
242
243       if(testM<0 || 
244          testA<0 || 
245          testM==testA || 
246          testM>=vi->channels ||
247          testA>=vi->channels) goto err_out;
248     }
249
250   }
251
252   if(oggpack_read(opb,2)>0)goto err_out; /* 2,3:reserved */
253     
254   if(info->submaps>1){
255     for(i=0;i<vi->channels;i++){
256       info->chmuxlist[i]=oggpack_read(opb,4);
257       if(info->chmuxlist[i]>=info->submaps)goto err_out;
258     }
259   }
260   for(i=0;i<info->submaps;i++){
261     info->timesubmap[i]=oggpack_read(opb,8);
262     if(info->timesubmap[i]>=ci->times)goto err_out;
263     info->floorsubmap[i]=oggpack_read(opb,8);
264     if(info->floorsubmap[i]>=ci->floors)goto err_out;
265     info->residuesubmap[i]=oggpack_read(opb,8);
266     if(info->residuesubmap[i]>=ci->residues)goto err_out;
267   }
268
269   return info;
270
271  err_out:
272   mapping0_free_info(info);
273   return(NULL);
274 }
275
276 #include "os.h"
277 #include "lpc.h"
278 #include "lsp.h"
279 #include "envelope.h"
280 #include "mdct.h"
281 #include "psy.h"
282 #include "scales.h"
283
284 /* no time mapping implementation for now */
285 static long seq=0;
286 static int mapping0_forward(vorbis_block *vb,vorbis_look_mapping *l){
287   vorbis_dsp_state      *vd=vb->vd;
288   vorbis_info           *vi=vd->vi;
289   codec_setup_info      *ci=vi->codec_setup;
290   backend_lookup_state  *b=vb->vd->backend_state;
291   vorbis_look_mapping0  *look=(vorbis_look_mapping0 *)l;
292   vorbis_info_mapping0  *info=look->map;
293   vorbis_info_mode      *mode=look->mode;
294   vorbis_block_internal *vbi=(vorbis_block_internal *)vb->internal;
295   int                    n=vb->pcmend;
296   int i,j;
297   float *window=b->window[vb->W][vb->lW][vb->nW][mode->windowtype];
298   int   *nonzero=alloca(sizeof(*nonzero)*vi->channels);
299
300   float *work=_vorbis_block_alloc(vb,n*sizeof(*work));
301
302   float global_ampmax=vbi->ampmax;
303   float *local_ampmax=alloca(sizeof(*local_ampmax)*vi->channels);
304   int blocktype;
305
306   /* we differentiate between short and long block types to help the
307      masking engine; the window shapes also matter.
308      impulse block (a short block in which an impulse occurs)
309      padding block (a short block that pads between a transitional 
310           long block and an impulse block, or vice versa)
311      transition block (the wqeird one; a long block with the transition 
312           window; affects bass/midrange response and that must be 
313           accounted for in masking) 
314      long block (run of the mill long block)
315   */
316
317   if(vb->W){
318     if(!vb->lW || !vb->nW)
319       blocktype=BLOCKTYPE_TRANSITION;
320     else
321       blocktype=BLOCKTYPE_LONG;
322   }else{
323     /* right now we're missing the infrastructure to distingush the
324        two short types */
325     blocktype=BLOCKTYPE_IMPULSE;
326   }
327
328   for(i=0;i<vi->channels;i++){
329     float scale=4.f/n;
330
331     /* the following makes things clearer to *me* anyway */
332     float *pcm     =vb->pcm[i]; 
333     float *fft     =work;
334     float *logfft  =pcm+n/2;
335
336     /*float *res     =pcm;
337     float *mdct    =pcm;
338     float *codedflr=pcm+n/2;
339     float *logmax  =work;
340     float *logmask =work+n/2;*/
341
342     /* window the PCM data */
343     for(j=0;j<n;j++)
344       fft[j]=pcm[j]*=window[j];
345     
346     /* transform the PCM data */
347     /* only MDCT right now.... */
348     mdct_forward(b->transform[vb->W][0],pcm,pcm);
349     
350     /* FFT yields more accurate tonal estimation (not phase sensitive) */
351     drft_forward(&look->fft_look,fft);
352     fft[0]*=scale;
353     logfft[0]=todB(fft);
354     local_ampmax[i]=logfft[0];
355     for(j=1;j<n-1;j+=2){
356       float temp=scale*FAST_HYPOT(fft[j],fft[j+1]);
357       temp=logfft[(j+1)>>1]=todB(&temp);
358       if(temp>local_ampmax[i])local_ampmax[i]=temp;
359     }
360     if(local_ampmax[i]>global_ampmax)global_ampmax=local_ampmax[i];
361
362     _analysis_output("fft",seq+i,logfft,n/2,1,0);
363   }
364
365   for(i=0;i<vi->channels;i++){
366     int submap=info->chmuxlist[i];
367
368     /* the following makes things clearer to *me* anyway */
369     float *mdct    =vb->pcm[i]; 
370     float *res     =mdct;
371     float *codedflr=mdct+n/2;
372     float *logfft  =mdct+n/2;
373
374     float *logmdct =work;
375     float *logmax  =mdct+n/2;
376     float *logmask =work+n/2;
377
378     for(j=0;j<n/2;j++)
379       logmdct[j]=todB(mdct+j);
380     _analysis_output("mdct",seq+i,logmdct,n/2,1,0);
381     _analysis_output("lmdct",seq+i,mdct,n/2,0,0);
382
383
384     /* perform psychoacoustics; do masking */
385     _vp_compute_mask(look->psy_look[blocktype],
386                      b->psy_g_look,
387                      i,
388                      logfft, /* -> logmax */
389                      logmdct,
390                      logmask,
391                      global_ampmax,
392                      local_ampmax[i],
393                      ci->blocksizes[vb->lW]/2);
394
395     _analysis_output("mask",seq+i,logmask,n/2,1,0);
396     /* perform floor encoding */
397     nonzero[i]=look->floor_func[submap]->
398       forward(vb,look->floor_look[submap],
399               mdct,
400               logmdct,
401               logmask,
402               logmax,
403
404               codedflr);
405
406
407     _analysis_output("mdct2",seq+i,mdct,n/2,1,1);
408     _vp_remove_floor(look->psy_look[blocktype],
409                      b->psy_g_look,
410                      logmdct,
411                      mdct,
412                      codedflr,
413                      res,
414                      local_ampmax[i]);
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("res",seq+i,res,n/2,1,0);
423     _analysis_output("codedflr",seq+i,codedflr,n/2,1,1);
424       
425   }
426
427   vbi->ampmax=global_ampmax;
428
429   /* partition based prequantization and channel coupling */
430   /* Steps in prequant and coupling:
431      
432      down-couple/down-quantize from perfect residue ->  quantized vector 
433      classify by this first 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 ***pcmbundle=alloca(sizeof(*pcmbundle)*info->submaps);
459     float ***sobundle=alloca(sizeof(*sobundle)*info->submaps);
460     int    **zerobundle=alloca(sizeof(*zerobundle)*info->submaps);
461     int     *chbundle=alloca(sizeof(*chbundle)*info->submaps);
462     int      chcounter=0;
463
464     /* play a little loose with this abstraction */
465     int   quant_passes=look->psy_look[blocktype]->vi->coupling_passes;
466     int   stopflag=0;
467
468     for(i=0;i<vi->channels;i++){
469       quantized[i]=pcm[i]+n/2;
470       sofar[i]=_vorbis_block_alloc(vb,n/2*sizeof(*sofar[i]));
471       memset(sofar[i],0,sizeof(*sofar[i])*n/2);
472     }
473
474     pcmbundle[0]=alloca(sizeof(*pcmbundle[0])*vi->channels);
475     sobundle[0]=alloca(sizeof(*sobundle[0])*vi->channels);
476     zerobundle[0]=alloca(sizeof(*zerobundle[0])*vi->channels);
477
478     /* initial down-quantized coupling */
479     
480     if(info->coupling_steps==0){
481       /* this assumes all or nothing coupling right now.  it should pass
482          through any channels left uncoupled, but it doesn't do that now */
483       for(i=0;i<vi->channels;i++){
484         float *lpcm=pcm[i];
485         float *lqua=quantized[i];
486         for(j=0;j<n/2;j++)
487           lqua[j]=lpcm[j];
488       }
489     }else{
490       _vp_quantize_couple(look->psy_look[blocktype],
491                           info,
492                           pcm,
493                           sofar,
494                           quantized,
495                           nonzero,
496                           0);
497     }
498
499     for(i=0;i<vi->channels;i++)
500       _analysis_output("quant",seq+i,quantized[i],n/2,1,0);
501
502   
503     /* classify, by submap */
504
505     for(i=0;i<info->submaps;i++){
506       int ch_in_bundle=0;
507       pcmbundle[i]=pcmbundle[0]+chcounter;
508       sobundle[i]=sobundle[0]+chcounter;
509       zerobundle[i]=zerobundle[0]+chcounter;
510
511       for(j=0;j<vi->channels;j++){
512         if(info->chmuxlist[j]==i){
513           if(nonzero[j])
514             zerobundle[i][ch_in_bundle]=1;
515           else
516             zerobundle[i][ch_in_bundle]=0;
517           pcmbundle[i][ch_in_bundle]=quantized[j];
518           sobundle[i][ch_in_bundle++]=sofar[j];
519         }
520       }
521       chbundle[i]=ch_in_bundle;
522       chcounter+=ch_in_bundle;
523
524       classifications[i]=look->residue_func[i]->
525         class(vb,look->residue_look[i],pcmbundle[i],zerobundle[i],chbundle[i]);
526     }
527
528     /* actual encoding loop */
529     for(i=0;!stopflag;){
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                   pcmbundle[j],sobundle[j],zerobundle[j],chbundle[j],
538                   i,classifications[j]);
539       i++;
540       
541       /* bitrate management decision hook; the following if() is where
542          we tell progressive encoding to halt, right now it just
543          avoids falling off the edge */
544       if(i>=quant_passes /* || yadda yadda */)stopflag=1;
545
546       if(!stopflag){
547         /* down-couple/down-quantize from perfect-'so-far' -> 
548            new quantized vector */
549         if(info->coupling_steps==0){
550           /* this assumes all or nothing coupling right now.  it should pass
551              through any channels left uncoupled, but it doesn't do that now */
552           for(i=0;i<vi->channels;i++){
553             float *lpcm=pcm[i];
554             float *lsof=sofar[i];
555             float *lqua=quantized[i];
556             for(j=0;j<n/2;j++)
557               lqua[j]=lpcm[j]-lsof[j];
558           }
559         }else{
560           _vp_quantize_couple(look->psy_look[blocktype],
561                               info,
562                               pcm,
563                               sofar,
564                               quantized,
565                               nonzero,
566                               i);
567         }
568       }
569       /* steady as she goes */
570     }
571     seq+=vi->channels;
572   }
573   
574   look->lastframe=vb->sequence;
575   return(0);
576 }
577
578 static int mapping0_inverse(vorbis_block *vb,vorbis_look_mapping *l){
579   vorbis_dsp_state     *vd=vb->vd;
580   vorbis_info          *vi=vd->vi;
581   codec_setup_info     *ci=vi->codec_setup;
582   backend_lookup_state *b=vd->backend_state;
583   vorbis_look_mapping0 *look=(vorbis_look_mapping0 *)l;
584   vorbis_info_mapping0 *info=look->map;
585   vorbis_info_mode     *mode=look->mode;
586   int                   i,j;
587   long                  n=vb->pcmend=ci->blocksizes[vb->W];
588
589   float *window=b->window[vb->W][vb->lW][vb->nW][mode->windowtype];
590   float **pcmbundle=alloca(sizeof(*pcmbundle)*vi->channels);
591   int    *zerobundle=alloca(sizeof(*zerobundle)*vi->channels);
592
593   int   *nonzero  =alloca(sizeof(*nonzero)*vi->channels);
594   void **floormemo=alloca(sizeof(*floormemo)*vi->channels);
595   
596   /* time domain information decode (note that applying the
597      information would have to happen later; we'll probably add a
598      function entry to the harness for that later */
599   /* NOT IMPLEMENTED */
600
601   /* recover the spectral envelope; store it in the PCM vector for now */
602   for(i=0;i<vi->channels;i++){
603     int submap=info->chmuxlist[i];
604     floormemo[i]=look->floor_func[submap]->
605       inverse1(vb,look->floor_look[submap]);
606     if(floormemo[i])
607       nonzero[i]=1;
608     else
609       nonzero[i]=0;      
610     memset(vb->pcm[i],0,sizeof(*vb->pcm[i])*n/2);
611   }
612
613   /* channel coupling can 'dirty' the nonzero listing */
614   for(i=0;i<info->coupling_steps;i++){
615     if(nonzero[info->coupling_mag[i]] ||
616        nonzero[info->coupling_ang[i]]){
617       nonzero[info->coupling_mag[i]]=1; 
618       nonzero[info->coupling_ang[i]]=1; 
619     }
620   }
621
622   /* recover the residue into our working vectors */
623   for(i=0;i<info->submaps;i++){
624     int ch_in_bundle=0;
625     for(j=0;j<vi->channels;j++){
626       if(info->chmuxlist[j]==i){
627         if(nonzero[j])
628           zerobundle[ch_in_bundle]=1;
629         else
630           zerobundle[ch_in_bundle]=0;
631         pcmbundle[ch_in_bundle++]=vb->pcm[j];
632       }
633     }
634     
635     look->residue_func[i]->inverse(vb,look->residue_look[i],
636                                    pcmbundle,zerobundle,ch_in_bundle);
637   }
638
639   /* channel coupling */
640   for(i=info->coupling_steps-1;i>=0;i--){
641     float *pcmM=vb->pcm[info->coupling_mag[i]];
642     float *pcmA=vb->pcm[info->coupling_ang[i]];
643
644     for(j=0;j<n/2;j++){
645       float mag=pcmM[j];
646       float ang=pcmA[j];
647
648       if(mag>0)
649         if(ang>0){
650           pcmM[j]=mag;
651           pcmA[j]=mag-ang;
652         }else{
653           pcmA[j]=mag;
654           pcmM[j]=mag+ang;
655         }
656       else
657         if(ang>0){
658           pcmM[j]=mag;
659           pcmA[j]=mag+ang;
660         }else{
661           pcmA[j]=mag;
662           pcmM[j]=mag-ang;
663         }
664     }
665   }
666
667   /* compute and apply spectral envelope */
668   for(i=0;i<vi->channels;i++){
669     float *pcm=vb->pcm[i];
670     int submap=info->chmuxlist[i];
671     look->floor_func[submap]->
672       inverse2(vb,look->floor_look[submap],floormemo[i],pcm);
673   }
674
675   /* transform the PCM data; takes PCM vector, vb; modifies PCM vector */
676   /* only MDCT right now.... */
677   for(i=0;i<vi->channels;i++){
678     float *pcm=vb->pcm[i];
679     _analysis_output("out",seq+i,pcm,n/2,1,1);
680     _analysis_output("lout",seq+i,pcm,n/2,0,0);
681     mdct_backward(b->transform[vb->W][0],pcm,pcm);
682   }
683
684   /* window the data */
685   for(i=0;i<vi->channels;i++){
686     float *pcm=vb->pcm[i];
687     if(nonzero[i])
688       for(j=0;j<n;j++)
689         pcm[j]*=window[j];
690     else
691       for(j=0;j<n;j++)
692         pcm[j]=0.f;
693     _analysis_output("final",seq++,pcm,n,0,0);
694   }
695             
696   /* now apply the decoded post-window time information */
697   /* NOT IMPLEMENTED */
698
699   /* all done! */
700   return(0);
701 }
702
703 /* export hooks */
704 vorbis_func_mapping mapping0_exportbundle={
705   &mapping0_pack,
706   &mapping0_unpack,
707   &mapping0_look,
708   &mapping0_copy_info,
709   &mapping0_free_info,
710   &mapping0_free_look,
711   &mapping0_forward,
712   &mapping0_inverse
713 };