*finally* got the lpc excitation amplitude quantization nailed down.
[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.8 2000/02/09 22:04:14 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 **auxbundle=alloca(sizeof(int *)*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     int **pcmaux=_vorbis_block_alloc(vb,vi->channels*sizeof(int *));
220     
221     for(i=0;i<vi->channels;i++){
222       double *pcm=vb->pcm[i];
223       int submap=map->chmuxlist[i];
224
225       pcmaux[i]=_vorbis_block_alloc(vb,n/2*sizeof(int));
226  
227       /* perform psychoacoustics; takes PCM vector; 
228          returns two curves: the desired transform floor and the masking curve */
229       memset(floor,0,sizeof(double)*n/2);
230       memset(mask,0,sizeof(double)*n/2);
231       _vp_mask_floor(look->psy_look+submap,pcm,mask,floor);
232  
233       /* perform floor encoding; takes transform floor, returns decoded floor */
234       nonzero[i]=look->floor_func[submap]->
235         forward(vb,look->floor_look[submap],floor,decfloor);
236       
237       /* no iterative residue/floor tuning at the moment */
238       
239       /* perform residue prequantization.  Do it now so we have all
240          the values if we need them */
241       _vp_quantize(look->psy_look+submap,pcm,mask,decfloor,pcmaux[i],0,n/2,n/2);
242       
243     }
244   
245     /* perform residue encoding with residue mapping; this is
246        multiplexed.  All the channels belonging to one submap are
247        encoded (values interleaved), then the next submap, etc */
248     
249     for(i=0;i<map->submaps;i++){
250       int ch_in_bundle=0;
251       for(j=0;j<vi->channels;j++){
252         if(map->chmuxlist[j]==i && nonzero[j]==1){
253           pcmbundle[ch_in_bundle]=vb->pcm[j];
254           auxbundle[ch_in_bundle++]=pcmaux[j];
255         }
256       }
257       
258       look->residue_func[i]->forward(vb,look->residue_look[i],pcmbundle,auxbundle,
259                                  ch_in_bundle);
260     }
261   }
262
263   return(0);
264 }
265
266 static int inverse(vorbis_block *vb,vorbis_look_mapping *l){
267   vorbis_dsp_state     *vd=vb->vd;
268   vorbis_info          *vi=vd->vi;
269   vorbis_look_mapping0 *look=(vorbis_look_mapping0 *)l;
270   vorbis_info_mapping0 *map=look->map;
271   vorbis_info_mode     *mode=look->mode;
272   int                   i,j;
273   long                  n=vb->pcmend=vi->blocksizes[vb->W];
274
275   double *window=vd->window[vb->W][vb->lW][vb->nW][mode->windowtype];
276   double **pcmbundle=alloca(sizeof(double *)*vi->channels);
277   int *nonzero=alloca(sizeof(int)*vi->channels);
278   
279   /* time domain information decode (note that applying the
280      information would have to happen later; we'll probably add a
281      function entry to the harness for that later */
282   /* NOT IMPLEMENTED */
283
284   /* recover the spectral envelope; store it in the PCM vector for now */
285   for(i=0;i<vi->channels;i++){
286     double *pcm=vb->pcm[i];
287     int submap=map->chmuxlist[i];
288     nonzero[i]=look->floor_func[submap]->
289       inverse(vb,look->floor_look[submap],pcm);
290   }
291
292   /* recover the residue, apply directly to the spectral envelope */
293
294   for(i=0;i<map->submaps;i++){
295     int ch_in_bundle=0;
296     for(j=0;j<vi->channels;j++){
297       if(map->chmuxlist[j]==i && nonzero[j])
298         pcmbundle[ch_in_bundle++]=vb->pcm[j];
299     }
300
301     look->residue_func[i]->inverse(vb,look->residue_look[i],pcmbundle,ch_in_bundle);
302   }
303
304   /* transform the PCM data; takes PCM vector, vb; modifies PCM vector */
305   /* only MDCT right now.... */
306   for(i=0;i<vi->channels;i++){
307     double *pcm=vb->pcm[i];
308     mdct_backward(vd->transform[vb->W][0],pcm,pcm);
309   }
310
311   /* now apply the decoded pre-window time information */
312   /* NOT IMPLEMENTED */
313   
314   /* window the data */
315   for(i=0;i<vi->channels;i++){
316     double *pcm=vb->pcm[i];
317     if(nonzero[i])
318       for(j=0;j<n;j++)
319         pcm[j]*=window[j];
320     else
321       for(j=0;j<n;j++)
322         pcm[j]=0.;
323   }
324             
325   /* now apply the decoded post-window time information */
326   /* NOT IMPLEMENTED */
327
328   /* all done! */
329   return(0);
330 }
331
332 /* export hooks */
333 vorbis_func_mapping mapping0_exportbundle={
334   &pack,&unpack,&look,&free_info,&free_look,&forward,&inverse
335 };
336
337
338