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