1 /********************************************************************
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. *
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/ *
12 ********************************************************************
14 function: channel mapping 0 implementation
15 last mod: $Id: mapping0.c,v 1.16 2000/10/12 03:12:53 xiphmont Exp $
17 ********************************************************************/
23 #include "vorbis/codec.h"
24 #include "vorbis/backends.h"
25 #include "bookinternal.h"
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
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 */
40 vorbis_info_mode *mode;
41 vorbis_info_mapping0 *map;
43 vorbis_look_time **time_look;
44 vorbis_look_floor **floor_look;
46 vorbis_look_residue **residue_look;
47 vorbis_look_psy *psy_look;
49 vorbis_func_time **time_func;
50 vorbis_func_floor **floor_func;
51 vorbis_func_residue **residue_func;
55 long lastframe; /* if a different mode is called, we need to
57 } vorbis_look_mapping0;
59 static void mapping0_free_info(vorbis_info_mapping *i){
61 memset(i,0,sizeof(vorbis_info_mapping0));
66 static void mapping0_free_look(vorbis_look_mapping *look){
68 vorbis_look_mapping0 *l=(vorbis_look_mapping0 *)look;
70 for(i=0;i<l->map->submaps;i++){
71 l->time_func[i]->free_look(l->time_look[i]);
72 l->floor_func[i]->free_look(l->floor_look[i]);
73 l->residue_func[i]->free_look(l->residue_look[i]);
74 if(l->psy_look)_vp_psy_clear(l->psy_look+i);
79 if(l->decay[i])free(l->decay[i]);
86 free(l->residue_func);
89 free(l->residue_look);
90 if(l->psy_look)free(l->psy_look);
91 memset(l,0,sizeof(vorbis_look_mapping0));
96 static vorbis_look_mapping *mapping0_look(vorbis_dsp_state *vd,vorbis_info_mode *vm,
97 vorbis_info_mapping *m){
99 vorbis_info *vi=vd->vi;
100 vorbis_look_mapping0 *look=calloc(1,sizeof(vorbis_look_mapping0));
101 vorbis_info_mapping0 *info=look->map=(vorbis_info_mapping0 *)m;
104 look->time_look=calloc(info->submaps,sizeof(vorbis_look_time *));
105 look->floor_look=calloc(info->submaps,sizeof(vorbis_look_floor *));
107 look->residue_look=calloc(info->submaps,sizeof(vorbis_look_residue *));
108 if(vi->psys)look->psy_look=calloc(info->submaps,sizeof(vorbis_look_psy));
110 look->time_func=calloc(info->submaps,sizeof(vorbis_func_time *));
111 look->floor_func=calloc(info->submaps,sizeof(vorbis_func_floor *));
112 look->residue_func=calloc(info->submaps,sizeof(vorbis_func_residue *));
114 for(i=0;i<info->submaps;i++){
115 int timenum=info->timesubmap[i];
116 int floornum=info->floorsubmap[i];
117 int resnum=info->residuesubmap[i];
119 look->time_func[i]=_time_P[vi->time_type[timenum]];
120 look->time_look[i]=look->time_func[i]->
121 look(vd,vm,vi->time_param[timenum]);
122 look->floor_func[i]=_floor_P[vi->floor_type[floornum]];
123 look->floor_look[i]=look->floor_func[i]->
124 look(vd,vm,vi->floor_param[floornum]);
125 look->residue_func[i]=_residue_P[vi->residue_type[resnum]];
126 look->residue_look[i]=look->residue_func[i]->
127 look(vd,vm,vi->residue_param[resnum]);
129 if(vi->psys && vd->analysisp){
130 int psynum=info->psysubmap[i];
131 _vp_psy_init(look->psy_look+i,vi->psy_param[psynum],
132 vi->blocksizes[vm->blockflag]/2,vi->rate);
136 look->ch=vi->channels;
138 look->decay=calloc(vi->channels,sizeof(float *));
139 for(i=0;i<vi->channels;i++)
140 look->decay[i]=calloc(vi->blocksizes[vm->blockflag]/2,sizeof(float));
146 static void mapping0_pack(vorbis_info *vi,vorbis_info_mapping *vm,oggpack_buffer *opb){
148 vorbis_info_mapping0 *info=(vorbis_info_mapping0 *)vm;
150 oggpack_write(opb,info->submaps-1,4);
151 /* we don't write the channel submappings if we only have one... */
153 for(i=0;i<vi->channels;i++)
154 oggpack_write(opb,info->chmuxlist[i],4);
156 for(i=0;i<info->submaps;i++){
157 oggpack_write(opb,info->timesubmap[i],8);
158 oggpack_write(opb,info->floorsubmap[i],8);
159 oggpack_write(opb,info->residuesubmap[i],8);
163 /* also responsible for range checking */
164 static vorbis_info_mapping *mapping0_unpack(vorbis_info *vi,oggpack_buffer *opb){
166 vorbis_info_mapping0 *info=calloc(1,sizeof(vorbis_info_mapping0));
167 memset(info,0,sizeof(vorbis_info_mapping0));
169 info->submaps=oggpack_read(opb,4)+1;
172 for(i=0;i<vi->channels;i++){
173 info->chmuxlist[i]=oggpack_read(opb,4);
174 if(info->chmuxlist[i]>=info->submaps)goto err_out;
177 for(i=0;i<info->submaps;i++){
178 info->timesubmap[i]=oggpack_read(opb,8);
179 if(info->timesubmap[i]>=vi->times)goto err_out;
180 info->floorsubmap[i]=oggpack_read(opb,8);
181 if(info->floorsubmap[i]>=vi->floors)goto err_out;
182 info->residuesubmap[i]=oggpack_read(opb,8);
183 if(info->residuesubmap[i]>=vi->residues)goto err_out;
189 mapping0_free_info(info);
196 #include "envelope.h"
201 /* no time mapping implementation for now */
203 static int mapping0_forward(vorbis_block *vb,vorbis_look_mapping *l){
204 vorbis_dsp_state *vd=vb->vd;
205 vorbis_info *vi=vd->vi;
206 vorbis_look_mapping0 *look=(vorbis_look_mapping0 *)l;
207 vorbis_info_mapping0 *info=look->map;
208 vorbis_info_mode *mode=look->mode;
211 float *window=vd->window[vb->W][vb->lW][vb->nW][mode->windowtype];
213 float **pcmbundle=alloca(sizeof(float *)*vi->channels);
214 int *nonzero=alloca(sizeof(int)*vi->channels);
216 /* time domain pre-window: NONE IMPLEMENTED */
218 /* window the PCM data: takes PCM vector, vb; modifies PCM vector */
220 for(i=0;i<vi->channels;i++){
221 float *pcm=vb->pcm[i];
226 /* time-domain post-window: NONE IMPLEMENTED */
228 /* transform the PCM data; takes PCM vector, vb; modifies PCM vector */
229 /* only MDCT right now.... */
230 for(i=0;i<vi->channels;i++){
231 float *pcm=vb->pcm[i];
232 mdct_forward(vd->transform[vb->W][0],pcm,pcm);
236 float *floor=_vorbis_block_alloc(vb,n*sizeof(float)/2);
238 for(i=0;i<vi->channels;i++){
239 float *pcm=vb->pcm[i];
240 float *decay=look->decay[i];
241 int submap=info->chmuxlist[i];
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(float)/2);
248 /* perform psychoacoustics; do masking */
249 _vp_compute_mask(look->psy_look+submap,pcm,floor,decay);
251 _analysis_output("decay",seq,decay,n/2,0,1);
252 _analysis_output("mdct",seq,pcm,n/2,0,1);
253 _analysis_output("lmdct",seq,pcm,n/2,0,0);
254 _analysis_output("prefloor",seq,floor,n/2,0,1);
256 /* perform floor encoding */
257 nonzero[i]=look->floor_func[submap]->
258 forward(vb,look->floor_look[submap],floor,floor);
260 _analysis_output("floor",seq,floor,n/2,0,1);
262 /* apply the floor, do optional noise levelling */
263 _vp_apply_floor(look->psy_look+submap,pcm,floor);
265 _analysis_output("res",seq++,pcm,n/2,0,0);
273 sprintf(buffer,"residue_%d.vqd",vb->mode);
274 of=fopen(buffer,"a");
276 fprintf(of,"%.2f, ",pcm[i]);
284 /* perform residue encoding with residue mapping; this is
285 multiplexed. All the channels belonging to one submap are
286 encoded (values interleaved), then the next submap, etc */
288 for(i=0;i<info->submaps;i++){
290 for(j=0;j<vi->channels;j++){
291 if(info->chmuxlist[j]==i && nonzero[j]==1){
292 pcmbundle[ch_in_bundle++]=vb->pcm[j];
296 look->residue_func[i]->forward(vb,look->residue_look[i],
297 pcmbundle,ch_in_bundle);
301 look->lastframe=vb->sequence;
305 static int mapping0_inverse(vorbis_block *vb,vorbis_look_mapping *l){
306 vorbis_dsp_state *vd=vb->vd;
307 vorbis_info *vi=vd->vi;
308 vorbis_look_mapping0 *look=(vorbis_look_mapping0 *)l;
309 vorbis_info_mapping0 *info=look->map;
310 vorbis_info_mode *mode=look->mode;
312 long n=vb->pcmend=vi->blocksizes[vb->W];
314 float *window=vd->window[vb->W][vb->lW][vb->nW][mode->windowtype];
315 float **pcmbundle=alloca(sizeof(float *)*vi->channels);
316 int *nonzero=alloca(sizeof(int)*vi->channels);
318 /* time domain information decode (note that applying the
319 information would have to happen later; we'll probably add a
320 function entry to the harness for that later */
321 /* NOT IMPLEMENTED */
323 /* recover the spectral envelope; store it in the PCM vector for now */
324 for(i=0;i<vi->channels;i++){
325 float *pcm=vb->pcm[i];
326 int submap=info->chmuxlist[i];
327 nonzero[i]=look->floor_func[submap]->
328 inverse(vb,look->floor_look[submap],pcm);
329 _analysis_output("ifloor",seq+i,pcm,n/2,0,1);
332 /* recover the residue, apply directly to the spectral envelope */
334 for(i=0;i<info->submaps;i++){
336 for(j=0;j<vi->channels;j++){
337 if(info->chmuxlist[j]==i && nonzero[j])
338 pcmbundle[ch_in_bundle++]=vb->pcm[j];
341 look->residue_func[i]->inverse(vb,look->residue_look[i],pcmbundle,ch_in_bundle);
344 /* transform the PCM data; takes PCM vector, vb; modifies PCM vector */
345 /* only MDCT right now.... */
346 for(i=0;i<vi->channels;i++){
347 float *pcm=vb->pcm[i];
348 _analysis_output("out",seq+i,pcm,n/2,0,1);
349 mdct_backward(vd->transform[vb->W][0],pcm,pcm);
352 /* now apply the decoded pre-window time information */
353 /* NOT IMPLEMENTED */
355 /* window the data */
356 for(i=0;i<vi->channels;i++){
357 float *pcm=vb->pcm[i];
364 _analysis_output("final",seq++,pcm,n,0,0);
367 /* now apply the decoded post-window time information */
368 /* NOT IMPLEMENTED */
375 vorbis_func_mapping mapping0_exportbundle={
376 &mapping0_pack,&mapping0_unpack,&mapping0_look,&mapping0_free_info,
377 &mapping0_free_look,&mapping0_forward,&mapping0_inverse