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