static inline define hack for GCC where it really helps the mdct
[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 SOURCE IS GOVERNED BY *
5  * THE GNU LESSER/LIBRARY PUBLIC LICENSE, WHICH IS INCLUDED WITH    *
6  * THIS SOURCE. PLEASE READ THESE TERMS BEFORE DISTRIBUTING.        *
7  *                                                                  *
8  * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2000             *
9  * by Monty <monty@xiph.org> and the XIPHOPHORUS Company            *
10  * http://www.xiph.org/                                             *
11  *                                                                  *
12  ********************************************************************
13
14  function: channel mapping 0 implementation
15  last mod: $Id: mapping0.c,v 1.21 2000/12/12 08:54:27 xiphmont Exp $
16
17  ********************************************************************/
18
19 #include <stdlib.h>
20 #include <stdio.h>
21 #include <string.h>
22 #include <math.h>
23 #include <ogg/ogg.h>
24 #include "vorbis/codec.h"
25 #include "codec_internal.h"
26 #include "codebook.h"
27 #include "bitbuffer.h"
28 #include "registry.h"
29 #include "psy.h"
30 #include "misc.h"
31
32 /* simplistic, wasteful way of doing this (unique lookup for each
33    mode/submapping); there should be a central repository for
34    identical lookups.  That will require minor work, so I'm putting it
35    off as low priority.
36
37    Why a lookup for each backend in a given mode?  Because the
38    blocksize is set by the mode, and low backend lookups may require
39    parameters from other areas of the mode/mapping */
40
41 typedef struct {
42   vorbis_info_mode *mode;
43   vorbis_info_mapping0 *map;
44
45   vorbis_look_time **time_look;
46   vorbis_look_floor **floor_look;
47
48   vorbis_look_residue **residue_look;
49   vorbis_look_psy *psy_look;
50
51   vorbis_func_time **time_func;
52   vorbis_func_floor **floor_func;
53   vorbis_func_residue **residue_func;
54
55   int ch;
56   float **decay;
57   long lastframe; /* if a different mode is called, we need to 
58                      invalidate decay */
59 } vorbis_look_mapping0;
60
61 static vorbis_info_mapping *mapping0_copy_info(vorbis_info_mapping *vm){
62   vorbis_info_mapping0 *info=(vorbis_info_mapping0 *)vm;
63   vorbis_info_mapping0 *ret=_ogg_malloc(sizeof(vorbis_info_mapping0));
64   memcpy(ret,info,sizeof(vorbis_info_mapping0));
65   return(ret);
66 }
67
68 static void mapping0_free_info(vorbis_info_mapping *i){
69   if(i){
70     memset(i,0,sizeof(vorbis_info_mapping0));
71     _ogg_free(i);
72   }
73 }
74
75 static void mapping0_free_look(vorbis_look_mapping *look){
76   int i;
77   vorbis_look_mapping0 *l=(vorbis_look_mapping0 *)look;
78   if(l){
79     for(i=0;i<l->map->submaps;i++){
80       l->time_func[i]->free_look(l->time_look[i]);
81       l->floor_func[i]->free_look(l->floor_look[i]);
82       l->residue_func[i]->free_look(l->residue_look[i]);
83       if(l->psy_look)_vp_psy_clear(l->psy_look+i);
84     }
85
86     if(l->decay){
87       for(i=0;i<l->ch;i++){
88         if(l->decay[i])_ogg_free(l->decay[i]);
89       }
90       _ogg_free(l->decay);
91     }
92
93     _ogg_free(l->time_func);
94     _ogg_free(l->floor_func);
95     _ogg_free(l->residue_func);
96     _ogg_free(l->time_look);
97     _ogg_free(l->floor_look);
98     _ogg_free(l->residue_look);
99     if(l->psy_look)_ogg_free(l->psy_look);
100     memset(l,0,sizeof(vorbis_look_mapping0));
101     _ogg_free(l);
102   }
103 }
104
105 static vorbis_look_mapping *mapping0_look(vorbis_dsp_state *vd,vorbis_info_mode *vm,
106                           vorbis_info_mapping *m){
107   int i;
108   vorbis_info          *vi=vd->vi;
109   codec_setup_info     *ci=vi->codec_setup;
110   vorbis_look_mapping0 *look=_ogg_calloc(1,sizeof(vorbis_look_mapping0));
111   vorbis_info_mapping0 *info=look->map=(vorbis_info_mapping0 *)m;
112   look->mode=vm;
113   
114   look->time_look=_ogg_calloc(info->submaps,sizeof(vorbis_look_time *));
115   look->floor_look=_ogg_calloc(info->submaps,sizeof(vorbis_look_floor *));
116
117   look->residue_look=_ogg_calloc(info->submaps,sizeof(vorbis_look_residue *));
118   if(ci->psys)look->psy_look=_ogg_calloc(info->submaps,sizeof(vorbis_look_psy));
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     if(ci->psys && vd->analysisp){
140       int psynum=info->psysubmap[i];
141       _vp_psy_init(look->psy_look+i,ci->psy_param[psynum],
142                    ci->blocksizes[vm->blockflag]/2,vi->rate);
143     }
144   }
145
146   look->ch=vi->channels;
147   if(ci->psys){
148     look->decay=_ogg_calloc(vi->channels,sizeof(float *));
149     for(i=0;i<vi->channels;i++)
150       look->decay[i]=_ogg_calloc(ci->blocksizes[vm->blockflag]/2,sizeof(float));
151   }
152
153   return(look);
154 }
155
156 static void mapping0_pack(vorbis_info *vi,vorbis_info_mapping *vm,oggpack_buffer *opb){
157   int i;
158   vorbis_info_mapping0 *info=(vorbis_info_mapping0 *)vm;
159
160   oggpack_write(opb,info->submaps-1,4);
161   /* we don't write the channel submappings if we only have one... */
162   if(info->submaps>1){
163     for(i=0;i<vi->channels;i++)
164       oggpack_write(opb,info->chmuxlist[i],4);
165   }
166   for(i=0;i<info->submaps;i++){
167     oggpack_write(opb,info->timesubmap[i],8);
168     oggpack_write(opb,info->floorsubmap[i],8);
169     oggpack_write(opb,info->residuesubmap[i],8);
170   }
171 }
172
173 /* also responsible for range checking */
174 static vorbis_info_mapping *mapping0_unpack(vorbis_info *vi,oggpack_buffer *opb){
175   int i;
176   vorbis_info_mapping0 *info=_ogg_calloc(1,sizeof(vorbis_info_mapping0));
177   codec_setup_info     *ci=vi->codec_setup;
178   memset(info,0,sizeof(vorbis_info_mapping0));
179
180   info->submaps=oggpack_read(opb,4)+1;
181
182   if(info->submaps>1){
183     for(i=0;i<vi->channels;i++){
184       info->chmuxlist[i]=oggpack_read(opb,4);
185       if(info->chmuxlist[i]>=info->submaps)goto err_out;
186     }
187   }
188   for(i=0;i<info->submaps;i++){
189     info->timesubmap[i]=oggpack_read(opb,8);
190     if(info->timesubmap[i]>=ci->times)goto err_out;
191     info->floorsubmap[i]=oggpack_read(opb,8);
192     if(info->floorsubmap[i]>=ci->floors)goto err_out;
193     info->residuesubmap[i]=oggpack_read(opb,8);
194     if(info->residuesubmap[i]>=ci->residues)goto err_out;
195   }
196
197   return info;
198
199  err_out:
200   mapping0_free_info(info);
201   return(NULL);
202 }
203
204 #include "os.h"
205 #include "lpc.h"
206 #include "lsp.h"
207 #include "envelope.h"
208 #include "mdct.h"
209 #include "psy.h"
210 #include "scales.h"
211
212 /* no time mapping implementation for now */
213 static long seq=0;
214 static int mapping0_forward(vorbis_block *vb,vorbis_look_mapping *l){
215   vorbis_dsp_state     *vd=vb->vd;
216   vorbis_info          *vi=vd->vi;
217   backend_lookup_state *b=vb->vd->backend_state;
218   vorbis_look_mapping0 *look=(vorbis_look_mapping0 *)l;
219   vorbis_info_mapping0 *info=look->map;
220   vorbis_info_mode     *mode=look->mode;
221   int                   n=vb->pcmend;
222   int i,j;
223   float *window=b->window[vb->W][vb->lW][vb->nW][mode->windowtype];
224
225   float **pcmbundle=alloca(sizeof(float *)*vi->channels);
226
227   int    *nonzero=alloca(sizeof(int)*vi->channels);
228   float **floor=_vorbis_block_alloc(vb,vi->channels*sizeof(float *));
229   float **nonquant=_vorbis_block_alloc(vb,vi->channels*sizeof(float *));
230
231   vorbis_bitbuffer      vbb_res;
232   vorbis_bitbuffer      *vbb_flr=_vorbis_block_alloc(vb,
233                                                      sizeof(vorbis_bitbuffer)*
234                                                      vi->channels);
235
236   for(i=0;i<vi->channels;i++)
237     bitbuf_init(vbb_flr+i,vb);
238   bitbuf_init(&vbb_res,vb);
239   
240   /* time domain pre-window: NONE IMPLEMENTED */
241
242   /* window the PCM data: takes PCM vector, vb; modifies PCM vector */
243
244   for(i=0;i<vi->channels;i++){
245     float *pcm=vb->pcm[i];
246     for(j=0;j<n;j++)
247       pcm[j]*=window[j];
248   }
249             
250   /* time-domain post-window: NONE IMPLEMENTED */
251
252   /* transform the PCM data; takes PCM vector, vb; modifies PCM vector */
253   /* only MDCT right now.... */
254   for(i=0;i<vi->channels;i++){
255     float *pcm=vb->pcm[i];
256     float *outq=nonquant[i]=_vorbis_block_alloc(vb,n*sizeof(float));
257     mdct_forward(b->transform[vb->W][0],pcm,pcm);
258
259     memcpy(pcm+n/2,pcm,n*sizeof(float)/2);
260     mdct_backward(b->transform[vb->W][0],pcm,outq);
261     for(j=0;j<n;j++)
262       outq[j]*=window[j];
263     
264   }
265
266   {
267     for(i=0;i<vi->channels;i++){
268       float *pcm=vb->pcm[i];
269       float *decay=look->decay[i];
270       int submap=info->chmuxlist[i];
271       floor[i]=_vorbis_block_alloc(vb,n*sizeof(float)/2);
272
273       /* if some other mode/mapping was called last frame, our decay
274          accumulator is out of date.  Clear it. */
275       if(look->lastframe+1 != vb->sequence)
276         memset(decay,0,n*sizeof(float)/2);
277
278       /* perform psychoacoustics; do masking */
279       _vp_compute_mask(look->psy_look+submap,pcm,floor[i],decay);
280  
281       _analysis_output("decay",seq,decay,n/2,0,1);
282       _analysis_output("mdct",seq,pcm,n/2,0,1);
283       _analysis_output("lmdct",seq,pcm,n/2,0,0);
284       _analysis_output("prefloor",seq,floor[i],n/2,0,1);
285
286       /* perform floor encoding */
287       nonzero[i]=look->floor_func[submap]->
288         forward(vb,look->floor_look[submap],floor[i],floor[i],vbb_flr+i);
289
290       _analysis_output("floor",seq,floor[i],n/2,0,1);
291
292       /* apply the floor, do optional noise levelling */
293       _vp_apply_floor(look->psy_look+submap,pcm,floor[i]);
294       
295       _analysis_output("res",seq++,pcm,n/2,0,0);
296       
297 #ifdef TRAIN_RES
298       if(nonzero[i]){
299         FILE *of;
300         char buffer[80];
301         int i;
302         
303         sprintf(buffer,"residue_%d.vqd",vb->mode);
304         of=fopen(buffer,"a");
305         for(i=0;i<n/2;i++)
306           fprintf(of,"%.2f, ",pcm[i]);
307         fprintf(of,"\n");
308         fclose(of);
309       }
310 #endif      
311
312     }
313     
314     /* perform residue encoding with residue mapping; this is
315        multiplexed.  All the channels belonging to one submap are
316        encoded (values interleaved), then the next submap, etc */
317     
318     for(i=0;i<info->submaps;i++){
319       int ch_in_bundle=0;
320       for(j=0;j<vi->channels;j++){
321         if(info->chmuxlist[j]==i && nonzero[j]>0){
322           pcmbundle[ch_in_bundle++]=vb->pcm[j];
323         }
324       }
325       
326       look->residue_func[i]->forward(vb,look->residue_look[i],
327                                      pcmbundle,ch_in_bundle,&vbb_res);
328     }
329
330     /* go back and compute the original MDCT inverse and our quantized
331        inverse; we'll want to give the floor mapping a shot at
332        massaging things before we write out the final version. */
333
334     {
335       for(i=0;i<vi->channels;i++){
336         float *pcm=vb->pcm[i];
337         float *pcmori=vb->pcm[i]+n/2;
338         float *orig=nonquant[i];
339         float *quant=floor[i];
340         float num=0.,den=0.;
341         int submap=info->chmuxlist[i];
342         
343         if(nonzero[i]){
344
345           for(j=0;j<n/2;j++)
346             pcm[j]=pcmori[j]-pcm[j]*quant[j];
347           
348           mdct_backward(b->transform[vb->W][0],pcm,pcm);
349           for(j=0;j<n;j++)
350             pcm[j]*=window[j];
351           
352           /* weighted compare twixt original and quantized mdct output */
353           for(j=0;j<n;j++){
354             den+=(pcm[j]*pcm[j]);
355             num+=(orig[j]*orig[j]);
356           }
357           
358           num=sqrt(num);
359           den=sqrt(den);
360
361         }
362
363         num=look->floor_func[submap]->
364           forward2(vb,look->floor_look[submap],
365                    nonzero[i],den==0.?0:num/den,vbb_flr+i);
366
367         /*for(j=0;j<n;j++)
368           pcm[j]*=num;
369         _analysis_output("i_orig",seq,orig,n,0,0);
370         _analysis_output("i_quant",seq++,pcm,n,0,0);
371         */
372
373       }
374     }
375
376     bitbuf_pack(&vb->opb,&vbb_res);
377
378   }
379
380   look->lastframe=vb->sequence;
381   return(0);
382 }
383
384 static int mapping0_inverse(vorbis_block *vb,vorbis_look_mapping *l){
385   vorbis_dsp_state     *vd=vb->vd;
386   vorbis_info          *vi=vd->vi;
387   codec_setup_info     *ci=vi->codec_setup;
388   backend_lookup_state *b=vd->backend_state;
389   vorbis_look_mapping0 *look=(vorbis_look_mapping0 *)l;
390   vorbis_info_mapping0 *info=look->map;
391   vorbis_info_mode     *mode=look->mode;
392   int                   i,j;
393   long                  n=vb->pcmend=ci->blocksizes[vb->W];
394
395   float *window=b->window[vb->W][vb->lW][vb->nW][mode->windowtype];
396   float **pcmbundle=alloca(sizeof(float *)*vi->channels);
397   int *nonzero=alloca(sizeof(int)*vi->channels);
398   
399   /* time domain information decode (note that applying the
400      information would have to happen later; we'll probably add a
401      function entry to the harness for that later */
402   /* NOT IMPLEMENTED */
403
404   /* recover the spectral envelope; store it in the PCM vector for now */
405   for(i=0;i<vi->channels;i++){
406     float *pcm=vb->pcm[i];
407     int submap=info->chmuxlist[i];
408     nonzero[i]=look->floor_func[submap]->
409       inverse(vb,look->floor_look[submap],pcm);
410     _analysis_output("ifloor",seq+i,pcm,n/2,0,1);
411   }
412
413   /* recover the residue, apply directly to the spectral envelope */
414
415   for(i=0;i<info->submaps;i++){
416     int ch_in_bundle=0;
417     for(j=0;j<vi->channels;j++){
418       if(info->chmuxlist[j]==i && nonzero[j])
419         pcmbundle[ch_in_bundle++]=vb->pcm[j];
420     }
421
422     look->residue_func[i]->inverse(vb,look->residue_look[i],pcmbundle,ch_in_bundle);
423   }
424
425   /* transform the PCM data; takes PCM vector, vb; modifies PCM vector */
426   /* only MDCT right now.... */
427   for(i=0;i<vi->channels;i++){
428     float *pcm=vb->pcm[i];
429     _analysis_output("out",seq+i,pcm,n/2,0,1);
430     mdct_backward(b->transform[vb->W][0],pcm,pcm);
431   }
432
433   /* now apply the decoded pre-window time information */
434   /* NOT IMPLEMENTED */
435   
436   /* window the data */
437   for(i=0;i<vi->channels;i++){
438     float *pcm=vb->pcm[i];
439     if(nonzero[i])
440       for(j=0;j<n;j++)
441         pcm[j]*=window[j];
442     else
443       for(j=0;j<n;j++)
444         pcm[j]=0.;
445     _analysis_output("final",seq++,pcm,n,0,0);
446   }
447             
448   /* now apply the decoded post-window time information */
449   /* NOT IMPLEMENTED */
450
451   /* all done! */
452   return(0);
453 }
454
455 /* export hooks */
456 vorbis_func_mapping mapping0_exportbundle={
457   &mapping0_pack,&mapping0_unpack,&mapping0_look,&mapping0_copy_info,
458   &mapping0_free_info,&mapping0_free_look,&mapping0_forward,&mapping0_inverse
459 };
460
461
462