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