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