1dc0e80d52a0cf9c54e061bd36157bd8b1ae33e9
[platform/upstream/libvorbis.git] / lib / mapping0.c
1 /********************************************************************
2  *                                                                  *
3  * THIS FILE IS PART OF THE Ogg Vorbis SOFTWARE CODEC SOURCE CODE.  *
4  * USE, DISTRIBUTION AND REPRODUCTION OF THIS SOURCE IS GOVERNED BY *
5  * THE GNU PUBLIC LICENSE 2, WHICH IS INCLUDED WITH THIS SOURCE.    *
6  * PLEASE READ THESE TERMS DISTRIBUTING.                            *
7  *                                                                  *
8  * THE OggSQUISH 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.15 2000/08/15 09:09:43 xiphmont Exp $
16
17  ********************************************************************/
18
19 #include <stdlib.h>
20 #include <string.h>
21 #include <math.h>
22 #include "vorbis/codec.h"
23 #include "vorbis/backends.h"
24 #include "bitwise.h"
25 #include "bookinternal.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   double **decay;
55   long lastframe; /* if a different mode is called, we need to 
56                      invalidate decay */
57 } vorbis_look_mapping0;
58
59 static void free_info(vorbis_info_mapping *i){
60   if(i){
61     memset(i,0,sizeof(vorbis_info_mapping0));
62     free(i);
63   }
64 }
65
66 static void free_look(vorbis_look_mapping *look){
67   int i;
68   vorbis_look_mapping0 *l=(vorbis_look_mapping0 *)look;
69   if(l){
70     for(i=0;i<l->map->submaps;i++){
71       l->time_func[i]->free_look(l->time_look[i]);
72       l->floor_func[i]->free_look(l->floor_look[i]);
73       l->residue_func[i]->free_look(l->residue_look[i]);
74       if(l->psy_look)_vp_psy_clear(l->psy_look+i);
75     }
76
77     if(l->decay){
78       for(i=0;i<l->ch;i++){
79         if(l->decay[i])free(l->decay[i]);
80       }
81       free(l->decay);
82     }
83
84     free(l->time_func);
85     free(l->floor_func);
86     free(l->residue_func);
87     free(l->time_look);
88     free(l->floor_look);
89     free(l->residue_look);
90     if(l->psy_look)free(l->psy_look);
91     memset(l,0,sizeof(vorbis_look_mapping0));
92     free(l);
93   }
94 }
95
96 static vorbis_look_mapping *look(vorbis_dsp_state *vd,vorbis_info_mode *vm,
97                           vorbis_info_mapping *m){
98   int i;
99   vorbis_info *vi=vd->vi;
100   vorbis_look_mapping0 *look=calloc(1,sizeof(vorbis_look_mapping0));
101   vorbis_info_mapping0 *info=look->map=(vorbis_info_mapping0 *)m;
102   look->mode=vm;
103   
104   look->time_look=calloc(info->submaps,sizeof(vorbis_look_time *));
105   look->floor_look=calloc(info->submaps,sizeof(vorbis_look_floor *));
106
107   look->residue_look=calloc(info->submaps,sizeof(vorbis_look_residue *));
108   if(vi->psys)look->psy_look=calloc(info->submaps,sizeof(vorbis_look_psy));
109
110   look->time_func=calloc(info->submaps,sizeof(vorbis_func_time *));
111   look->floor_func=calloc(info->submaps,sizeof(vorbis_func_floor *));
112   look->residue_func=calloc(info->submaps,sizeof(vorbis_func_residue *));
113   
114   for(i=0;i<info->submaps;i++){
115     int timenum=info->timesubmap[i];
116     int floornum=info->floorsubmap[i];
117     int resnum=info->residuesubmap[i];
118
119     look->time_func[i]=_time_P[vi->time_type[timenum]];
120     look->time_look[i]=look->time_func[i]->
121       look(vd,vm,vi->time_param[timenum]);
122     look->floor_func[i]=_floor_P[vi->floor_type[floornum]];
123     look->floor_look[i]=look->floor_func[i]->
124       look(vd,vm,vi->floor_param[floornum]);
125     look->residue_func[i]=_residue_P[vi->residue_type[resnum]];
126     look->residue_look[i]=look->residue_func[i]->
127       look(vd,vm,vi->residue_param[resnum]);
128     
129     if(vi->psys && vd->analysisp){
130       int psynum=info->psysubmap[i];
131       _vp_psy_init(look->psy_look+i,vi->psy_param[psynum],
132                    vi->blocksizes[vm->blockflag]/2,vi->rate);
133     }
134   }
135
136   look->ch=vi->channels;
137   if(vi->psys){
138     look->decay=calloc(vi->channels,sizeof(double *));
139     for(i=0;i<vi->channels;i++)
140       look->decay[i]=calloc(vi->blocksizes[vm->blockflag]/2,sizeof(double));
141   }
142
143   return(look);
144 }
145
146 static void pack(vorbis_info *vi,vorbis_info_mapping *vm,oggpack_buffer *opb){
147   int i;
148   vorbis_info_mapping0 *info=(vorbis_info_mapping0 *)vm;
149
150   _oggpack_write(opb,info->submaps-1,4);
151   /* we don't write the channel submappings if we only have one... */
152   if(info->submaps>1){
153     for(i=0;i<vi->channels;i++)
154       _oggpack_write(opb,info->chmuxlist[i],4);
155   }
156   for(i=0;i<info->submaps;i++){
157     _oggpack_write(opb,info->timesubmap[i],8);
158     _oggpack_write(opb,info->floorsubmap[i],8);
159     _oggpack_write(opb,info->residuesubmap[i],8);
160   }
161 }
162
163 /* also responsible for range checking */
164 static vorbis_info_mapping *unpack(vorbis_info *vi,oggpack_buffer *opb){
165   int i;
166   vorbis_info_mapping0 *info=calloc(1,sizeof(vorbis_info_mapping0));
167   memset(info,0,sizeof(vorbis_info_mapping0));
168
169   info->submaps=_oggpack_read(opb,4)+1;
170
171   if(info->submaps>1){
172     for(i=0;i<vi->channels;i++){
173       info->chmuxlist[i]=_oggpack_read(opb,4);
174       if(info->chmuxlist[i]>=info->submaps)goto err_out;
175     }
176   }
177   for(i=0;i<info->submaps;i++){
178     info->timesubmap[i]=_oggpack_read(opb,8);
179     if(info->timesubmap[i]>=vi->times)goto err_out;
180     info->floorsubmap[i]=_oggpack_read(opb,8);
181     if(info->floorsubmap[i]>=vi->floors)goto err_out;
182     info->residuesubmap[i]=_oggpack_read(opb,8);
183     if(info->residuesubmap[i]>=vi->residues)goto err_out;
184   }
185
186   return info;
187
188  err_out:
189   free_info(info);
190   return(NULL);
191 }
192
193 #include <stdio.h>
194 #include "os.h"
195 #include "lpc.h"
196 #include "lsp.h"
197 #include "envelope.h"
198 #include "mdct.h"
199 #include "psy.h"
200 #include "bitwise.h"
201 #include "spectrum.h"
202 #include "scales.h"
203
204 /* no time mapping implementation for now */
205 static long seq=0;
206 static int forward(vorbis_block *vb,vorbis_look_mapping *l){
207   vorbis_dsp_state     *vd=vb->vd;
208   vorbis_info          *vi=vd->vi;
209   vorbis_look_mapping0 *look=(vorbis_look_mapping0 *)l;
210   vorbis_info_mapping0 *info=look->map;
211   vorbis_info_mode     *mode=look->mode;
212   int                   n=vb->pcmend;
213   int i,j;
214   double *window=vd->window[vb->W][vb->lW][vb->nW][mode->windowtype];
215
216   double **pcmbundle=alloca(sizeof(double *)*vi->channels);
217   int *nonzero=alloca(sizeof(int)*vi->channels);
218  
219   /* time domain pre-window: NONE IMPLEMENTED */
220
221   /* window the PCM data: takes PCM vector, vb; modifies PCM vector */
222
223   for(i=0;i<vi->channels;i++){
224     double *pcm=vb->pcm[i];
225     for(j=0;j<n;j++)
226       pcm[j]*=window[j];
227   }
228             
229   /* time-domain post-window: NONE IMPLEMENTED */
230
231   /* transform the PCM data; takes PCM vector, vb; modifies PCM vector */
232   /* only MDCT right now.... */
233   for(i=0;i<vi->channels;i++){
234     double *pcm=vb->pcm[i];
235     mdct_forward(vd->transform[vb->W][0],pcm,pcm);
236   }
237
238   {
239     double *floor=_vorbis_block_alloc(vb,n*sizeof(double)/2);
240     
241     for(i=0;i<vi->channels;i++){
242       double *pcm=vb->pcm[i];
243       double *decay=look->decay[i];
244       int submap=info->chmuxlist[i];
245
246       /* if some other mode/mapping was called last frame, our decay
247          accumulator is out of date.  Clear it. */
248       if(look->lastframe+1 != vb->sequence)
249         memset(decay,0,n*sizeof(double)/2);
250
251       /* perform psychoacoustics; do masking */
252       _vp_compute_mask(look->psy_look+submap,pcm,floor,decay);
253  
254       _analysis_output("decay",seq,decay,n/2,0,1);
255       _analysis_output("mdct",seq,pcm,n/2,0,1);
256       _analysis_output("lmdct",seq,pcm,n/2,0,0);
257       _analysis_output("prefloor",seq,floor,n/2,0,1);
258
259       /* perform floor encoding */
260       nonzero[i]=look->floor_func[submap]->
261         forward(vb,look->floor_look[submap],floor,floor);
262
263       _analysis_output("floor",seq,floor,n/2,0,1);
264
265       /* apply the floor, do optional noise levelling */
266       _vp_apply_floor(look->psy_look+submap,pcm,floor);
267       
268       _analysis_output("res",seq++,pcm,n/2,0,0);
269       
270 #ifdef TRAIN_RES
271       if(nonzero[i]){
272         FILE *of;
273         char buffer[80];
274         int i;
275         
276         sprintf(buffer,"residue_%d.vqd",vb->mode);
277         of=fopen(buffer,"a");
278         for(i=0;i<n/2;i++)
279           fprintf(of,"%.2f, ",pcm[i]);
280         fprintf(of,"\n");
281         fclose(of);
282       }
283 #endif      
284
285     }
286     
287     /* perform residue encoding with residue mapping; this is
288        multiplexed.  All the channels belonging to one submap are
289        encoded (values interleaved), then the next submap, etc */
290     
291     for(i=0;i<info->submaps;i++){
292       int ch_in_bundle=0;
293       for(j=0;j<vi->channels;j++){
294         if(info->chmuxlist[j]==i && nonzero[j]==1){
295           pcmbundle[ch_in_bundle++]=vb->pcm[j];
296         }
297       }
298       
299       look->residue_func[i]->forward(vb,look->residue_look[i],
300                                      pcmbundle,ch_in_bundle);
301     }
302   }
303
304   look->lastframe=vb->sequence;
305   return(0);
306 }
307
308 static int inverse(vorbis_block *vb,vorbis_look_mapping *l){
309   vorbis_dsp_state     *vd=vb->vd;
310   vorbis_info          *vi=vd->vi;
311   vorbis_look_mapping0 *look=(vorbis_look_mapping0 *)l;
312   vorbis_info_mapping0 *info=look->map;
313   vorbis_info_mode     *mode=look->mode;
314   int                   i,j;
315   long                  n=vb->pcmend=vi->blocksizes[vb->W];
316
317   double *window=vd->window[vb->W][vb->lW][vb->nW][mode->windowtype];
318   double **pcmbundle=alloca(sizeof(double *)*vi->channels);
319   int *nonzero=alloca(sizeof(int)*vi->channels);
320   
321   /* time domain information decode (note that applying the
322      information would have to happen later; we'll probably add a
323      function entry to the harness for that later */
324   /* NOT IMPLEMENTED */
325
326   /* recover the spectral envelope; store it in the PCM vector for now */
327   for(i=0;i<vi->channels;i++){
328     double *pcm=vb->pcm[i];
329     int submap=info->chmuxlist[i];
330     nonzero[i]=look->floor_func[submap]->
331       inverse(vb,look->floor_look[submap],pcm);
332     _analysis_output("ifloor",seq+i,pcm,n/2,0,1);
333   }
334
335   /* recover the residue, apply directly to the spectral envelope */
336
337   for(i=0;i<info->submaps;i++){
338     int ch_in_bundle=0;
339     for(j=0;j<vi->channels;j++){
340       if(info->chmuxlist[j]==i && nonzero[j])
341         pcmbundle[ch_in_bundle++]=vb->pcm[j];
342     }
343
344     look->residue_func[i]->inverse(vb,look->residue_look[i],pcmbundle,ch_in_bundle);
345   }
346
347   /* transform the PCM data; takes PCM vector, vb; modifies PCM vector */
348   /* only MDCT right now.... */
349   for(i=0;i<vi->channels;i++){
350     double *pcm=vb->pcm[i];
351     _analysis_output("out",seq+i,pcm,n/2,0,1);
352     mdct_backward(vd->transform[vb->W][0],pcm,pcm);
353   }
354
355   /* now apply the decoded pre-window time information */
356   /* NOT IMPLEMENTED */
357   
358   /* window the data */
359   for(i=0;i<vi->channels;i++){
360     double *pcm=vb->pcm[i];
361     if(nonzero[i])
362       for(j=0;j<n;j++)
363         pcm[j]*=window[j];
364     else
365       for(j=0;j<n;j++)
366         pcm[j]=0.;
367     _analysis_output("final",seq++,pcm,n,0,0);
368   }
369             
370   /* now apply the decoded post-window time information */
371   /* NOT IMPLEMENTED */
372
373   /* all done! */
374   return(0);
375 }
376
377 /* export hooks */
378 vorbis_func_mapping mapping0_exportbundle={
379   &pack,&unpack,&look,&free_info,&free_look,&forward,&inverse
380 };
381
382
383