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