Merge branch_beta3 onto the mainline.
[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.17 2000/11/06 00:07:01 xiphmont Exp $
16
17  ********************************************************************/
18
19 #include <stdlib.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 "registry.h"
27 #include "psy.h"
28 #include "misc.h"
29
30 /* simplistic, wasteful way of doing this (unique lookup for each
31    mode/submapping); there should be a central repository for
32    identical lookups.  That will require minor work, so I'm putting it
33    off as low priority.
34
35    Why a lookup for each backend in a given mode?  Because the
36    blocksize is set by the mode, and low backend lookups may require
37    parameters from other areas of the mode/mapping */
38
39 typedef struct {
40   vorbis_info_mode *mode;
41   vorbis_info_mapping0 *map;
42
43   vorbis_look_time **time_look;
44   vorbis_look_floor **floor_look;
45
46   vorbis_look_residue **residue_look;
47   vorbis_look_psy *psy_look;
48
49   vorbis_func_time **time_func;
50   vorbis_func_floor **floor_func;
51   vorbis_func_residue **residue_func;
52
53   int ch;
54   float **decay;
55   long lastframe; /* if a different mode is called, we need to 
56                      invalidate decay */
57 } vorbis_look_mapping0;
58
59 static vorbis_info_mapping *mapping0_copy_info(vorbis_info_mapping *vm){
60   vorbis_info_mapping0 *info=(vorbis_info_mapping0 *)vm;
61   vorbis_info_mapping0 *ret=_ogg_malloc(sizeof(vorbis_info_mapping0));
62   memcpy(ret,info,sizeof(vorbis_info_mapping0));
63   return(ret);
64 }
65
66 static void mapping0_free_info(vorbis_info_mapping *i){
67   if(i){
68     memset(i,0,sizeof(vorbis_info_mapping0));
69     free(i);
70   }
71 }
72
73 static void mapping0_free_look(vorbis_look_mapping *look){
74   int i;
75   vorbis_look_mapping0 *l=(vorbis_look_mapping0 *)look;
76   if(l){
77     for(i=0;i<l->map->submaps;i++){
78       l->time_func[i]->free_look(l->time_look[i]);
79       l->floor_func[i]->free_look(l->floor_look[i]);
80       l->residue_func[i]->free_look(l->residue_look[i]);
81       if(l->psy_look)_vp_psy_clear(l->psy_look+i);
82     }
83
84     if(l->decay){
85       for(i=0;i<l->ch;i++){
86         if(l->decay[i])free(l->decay[i]);
87       }
88       free(l->decay);
89     }
90
91     free(l->time_func);
92     free(l->floor_func);
93     free(l->residue_func);
94     free(l->time_look);
95     free(l->floor_look);
96     free(l->residue_look);
97     if(l->psy_look)free(l->psy_look);
98     memset(l,0,sizeof(vorbis_look_mapping0));
99     free(l);
100   }
101 }
102
103 static vorbis_look_mapping *mapping0_look(vorbis_dsp_state *vd,vorbis_info_mode *vm,
104                           vorbis_info_mapping *m){
105   int i;
106   vorbis_info          *vi=vd->vi;
107   codec_setup_info     *ci=vi->codec_setup;
108   vorbis_look_mapping0 *look=_ogg_calloc(1,sizeof(vorbis_look_mapping0));
109   vorbis_info_mapping0 *info=look->map=(vorbis_info_mapping0 *)m;
110   look->mode=vm;
111   
112   look->time_look=_ogg_calloc(info->submaps,sizeof(vorbis_look_time *));
113   look->floor_look=_ogg_calloc(info->submaps,sizeof(vorbis_look_floor *));
114
115   look->residue_look=_ogg_calloc(info->submaps,sizeof(vorbis_look_residue *));
116   if(ci->psys)look->psy_look=_ogg_calloc(info->submaps,sizeof(vorbis_look_psy));
117
118   look->time_func=_ogg_calloc(info->submaps,sizeof(vorbis_func_time *));
119   look->floor_func=_ogg_calloc(info->submaps,sizeof(vorbis_func_floor *));
120   look->residue_func=_ogg_calloc(info->submaps,sizeof(vorbis_func_residue *));
121   
122   for(i=0;i<info->submaps;i++){
123     int timenum=info->timesubmap[i];
124     int floornum=info->floorsubmap[i];
125     int resnum=info->residuesubmap[i];
126
127     look->time_func[i]=_time_P[ci->time_type[timenum]];
128     look->time_look[i]=look->time_func[i]->
129       look(vd,vm,ci->time_param[timenum]);
130     look->floor_func[i]=_floor_P[ci->floor_type[floornum]];
131     look->floor_look[i]=look->floor_func[i]->
132       look(vd,vm,ci->floor_param[floornum]);
133     look->residue_func[i]=_residue_P[ci->residue_type[resnum]];
134     look->residue_look[i]=look->residue_func[i]->
135       look(vd,vm,ci->residue_param[resnum]);
136     
137     if(ci->psys && vd->analysisp){
138       int psynum=info->psysubmap[i];
139       _vp_psy_init(look->psy_look+i,ci->psy_param[psynum],
140                    ci->blocksizes[vm->blockflag]/2,vi->rate);
141     }
142   }
143
144   look->ch=vi->channels;
145   if(ci->psys){
146     look->decay=_ogg_calloc(vi->channels,sizeof(float *));
147     for(i=0;i<vi->channels;i++)
148       look->decay[i]=_ogg_calloc(ci->blocksizes[vm->blockflag]/2,sizeof(float));
149   }
150
151   return(look);
152 }
153
154 static void mapping0_pack(vorbis_info *vi,vorbis_info_mapping *vm,oggpack_buffer *opb){
155   int i;
156   vorbis_info_mapping0 *info=(vorbis_info_mapping0 *)vm;
157
158   oggpack_write(opb,info->submaps-1,4);
159   /* we don't write the channel submappings if we only have one... */
160   if(info->submaps>1){
161     for(i=0;i<vi->channels;i++)
162       oggpack_write(opb,info->chmuxlist[i],4);
163   }
164   for(i=0;i<info->submaps;i++){
165     oggpack_write(opb,info->timesubmap[i],8);
166     oggpack_write(opb,info->floorsubmap[i],8);
167     oggpack_write(opb,info->residuesubmap[i],8);
168   }
169 }
170
171 /* also responsible for range checking */
172 static vorbis_info_mapping *mapping0_unpack(vorbis_info *vi,oggpack_buffer *opb){
173   int i;
174   vorbis_info_mapping0 *info=_ogg_calloc(1,sizeof(vorbis_info_mapping0));
175   codec_setup_info     *ci=vi->codec_setup;
176   memset(info,0,sizeof(vorbis_info_mapping0));
177
178   info->submaps=oggpack_read(opb,4)+1;
179
180   if(info->submaps>1){
181     for(i=0;i<vi->channels;i++){
182       info->chmuxlist[i]=oggpack_read(opb,4);
183       if(info->chmuxlist[i]>=info->submaps)goto err_out;
184     }
185   }
186   for(i=0;i<info->submaps;i++){
187     info->timesubmap[i]=oggpack_read(opb,8);
188     if(info->timesubmap[i]>=ci->times)goto err_out;
189     info->floorsubmap[i]=oggpack_read(opb,8);
190     if(info->floorsubmap[i]>=ci->floors)goto err_out;
191     info->residuesubmap[i]=oggpack_read(opb,8);
192     if(info->residuesubmap[i]>=ci->residues)goto err_out;
193   }
194
195   return info;
196
197  err_out:
198   mapping0_free_info(info);
199   return(NULL);
200 }
201
202 #include "os.h"
203 #include "lpc.h"
204 #include "lsp.h"
205 #include "envelope.h"
206 #include "mdct.h"
207 #include "psy.h"
208 #include "scales.h"
209
210 /* no time mapping implementation for now */
211 static long seq=0;
212 static int mapping0_forward(vorbis_block *vb,vorbis_look_mapping *l){
213   vorbis_dsp_state     *vd=vb->vd;
214   vorbis_info          *vi=vd->vi;
215   backend_lookup_state *b=vb->vd->backend_state;
216   vorbis_look_mapping0 *look=(vorbis_look_mapping0 *)l;
217   vorbis_info_mapping0 *info=look->map;
218   vorbis_info_mode     *mode=look->mode;
219   int                   n=vb->pcmend;
220   int i,j;
221   float *window=b->window[vb->W][vb->lW][vb->nW][mode->windowtype];
222
223   float **pcmbundle=alloca(sizeof(float *)*vi->channels);
224   int *nonzero=alloca(sizeof(int)*vi->channels);
225  
226   /* time domain pre-window: NONE IMPLEMENTED */
227
228   /* window the PCM data: takes PCM vector, vb; modifies PCM vector */
229
230   for(i=0;i<vi->channels;i++){
231     float *pcm=vb->pcm[i];
232     for(j=0;j<n;j++)
233       pcm[j]*=window[j];
234   }
235             
236   /* time-domain post-window: NONE IMPLEMENTED */
237
238   /* transform the PCM data; takes PCM vector, vb; modifies PCM vector */
239   /* only MDCT right now.... */
240   for(i=0;i<vi->channels;i++){
241     float *pcm=vb->pcm[i];
242     mdct_forward(b->transform[vb->W][0],pcm,pcm);
243   }
244
245   {
246     float *floor=_vorbis_block_alloc(vb,n*sizeof(float)/2);
247     
248     for(i=0;i<vi->channels;i++){
249       float *pcm=vb->pcm[i];
250       float *decay=look->decay[i];
251       int submap=info->chmuxlist[i];
252
253       /* if some other mode/mapping was called last frame, our decay
254          accumulator is out of date.  Clear it. */
255       if(look->lastframe+1 != vb->sequence)
256         memset(decay,0,n*sizeof(float)/2);
257
258       /* perform psychoacoustics; do masking */
259       _vp_compute_mask(look->psy_look+submap,pcm,floor,decay);
260  
261       _analysis_output("decay",seq,decay,n/2,0,1);
262       _analysis_output("mdct",seq,pcm,n/2,0,1);
263       _analysis_output("lmdct",seq,pcm,n/2,0,0);
264       _analysis_output("prefloor",seq,floor,n/2,0,1);
265
266       /* perform floor encoding */
267       nonzero[i]=look->floor_func[submap]->
268         forward(vb,look->floor_look[submap],floor,floor);
269
270       _analysis_output("floor",seq,floor,n/2,0,1);
271
272       /* apply the floor, do optional noise levelling */
273       _vp_apply_floor(look->psy_look+submap,pcm,floor);
274       
275       _analysis_output("res",seq++,pcm,n/2,0,0);
276       
277 #ifdef TRAIN_RES
278       if(nonzero[i]){
279         FILE *of;
280         char buffer[80];
281         int i;
282         
283         sprintf(buffer,"residue_%d.vqd",vb->mode);
284         of=fopen(buffer,"a");
285         for(i=0;i<n/2;i++)
286           fprintf(of,"%.2f, ",pcm[i]);
287         fprintf(of,"\n");
288         fclose(of);
289       }
290 #endif      
291
292     }
293     
294     /* perform residue encoding with residue mapping; this is
295        multiplexed.  All the channels belonging to one submap are
296        encoded (values interleaved), then the next submap, etc */
297     
298     for(i=0;i<info->submaps;i++){
299       int ch_in_bundle=0;
300       for(j=0;j<vi->channels;j++){
301         if(info->chmuxlist[j]==i && nonzero[j]==1){
302           pcmbundle[ch_in_bundle++]=vb->pcm[j];
303         }
304       }
305       
306       look->residue_func[i]->forward(vb,look->residue_look[i],
307                                      pcmbundle,ch_in_bundle);
308     }
309   }
310
311   look->lastframe=vb->sequence;
312   return(0);
313 }
314
315 static int mapping0_inverse(vorbis_block *vb,vorbis_look_mapping *l){
316   vorbis_dsp_state     *vd=vb->vd;
317   vorbis_info          *vi=vd->vi;
318   codec_setup_info     *ci=vi->codec_setup;
319   backend_lookup_state *b=vd->backend_state;
320   vorbis_look_mapping0 *look=(vorbis_look_mapping0 *)l;
321   vorbis_info_mapping0 *info=look->map;
322   vorbis_info_mode     *mode=look->mode;
323   int                   i,j;
324   long                  n=vb->pcmend=ci->blocksizes[vb->W];
325
326   float *window=b->window[vb->W][vb->lW][vb->nW][mode->windowtype];
327   float **pcmbundle=alloca(sizeof(float *)*vi->channels);
328   int *nonzero=alloca(sizeof(int)*vi->channels);
329   
330   /* time domain information decode (note that applying the
331      information would have to happen later; we'll probably add a
332      function entry to the harness for that later */
333   /* NOT IMPLEMENTED */
334
335   /* recover the spectral envelope; store it in the PCM vector for now */
336   for(i=0;i<vi->channels;i++){
337     float *pcm=vb->pcm[i];
338     int submap=info->chmuxlist[i];
339     nonzero[i]=look->floor_func[submap]->
340       inverse(vb,look->floor_look[submap],pcm);
341     _analysis_output("ifloor",seq+i,pcm,n/2,0,1);
342   }
343
344   /* recover the residue, apply directly to the spectral envelope */
345
346   for(i=0;i<info->submaps;i++){
347     int ch_in_bundle=0;
348     for(j=0;j<vi->channels;j++){
349       if(info->chmuxlist[j]==i && nonzero[j])
350         pcmbundle[ch_in_bundle++]=vb->pcm[j];
351     }
352
353     look->residue_func[i]->inverse(vb,look->residue_look[i],pcmbundle,ch_in_bundle);
354   }
355
356   /* transform the PCM data; takes PCM vector, vb; modifies PCM vector */
357   /* only MDCT right now.... */
358   for(i=0;i<vi->channels;i++){
359     float *pcm=vb->pcm[i];
360     _analysis_output("out",seq+i,pcm,n/2,0,1);
361     mdct_backward(b->transform[vb->W][0],pcm,pcm);
362   }
363
364   /* now apply the decoded pre-window time information */
365   /* NOT IMPLEMENTED */
366   
367   /* window the data */
368   for(i=0;i<vi->channels;i++){
369     float *pcm=vb->pcm[i];
370     if(nonzero[i])
371       for(j=0;j<n;j++)
372         pcm[j]*=window[j];
373     else
374       for(j=0;j<n;j++)
375         pcm[j]=0.;
376     _analysis_output("final",seq++,pcm,n,0,0);
377   }
378             
379   /* now apply the decoded post-window time information */
380   /* NOT IMPLEMENTED */
381
382   /* all done! */
383   return(0);
384 }
385
386 /* export hooks */
387 vorbis_func_mapping mapping0_exportbundle={
388   &mapping0_pack,&mapping0_unpack,&mapping0_look,&mapping0_copy_info,
389   &mapping0_free_info,&mapping0_free_look,&mapping0_forward,&mapping0_inverse
390 };
391
392
393