1 /********************************************************************
3 * THIS FILE IS PART OF THE OggVorbis SOFTWARE CODEC SOURCE CODE. *
4 * USE, DISTRIBUTION AND REPRODUCTION OF THIS SOURCE IS GOVERNED BY *
5 * THE GNU LESSER/LIBRARY PUBLIC LICENSE, WHICH IS INCLUDED WITH *
6 * THIS SOURCE. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. *
8 * THE OggVorbis 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.17 2000/11/06 00:07:01 xiphmont Exp $
17 ********************************************************************/
23 #include "vorbis/codec.h"
24 #include "codec_internal.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 vorbis_info_mapping *mapping0_copy_info(vorbis_info_mapping *vm){
60 vorbis_info_mapping0 *info=(vorbis_info_mapping0 *)vm;
61 vorbis_info_mapping0 *ret=_ogg_malloc(sizeof(vorbis_info_mapping0));
62 memcpy(ret,info,sizeof(vorbis_info_mapping0));
66 static void mapping0_free_info(vorbis_info_mapping *i){
68 memset(i,0,sizeof(vorbis_info_mapping0));
73 static void mapping0_free_look(vorbis_look_mapping *look){
75 vorbis_look_mapping0 *l=(vorbis_look_mapping0 *)look;
77 for(i=0;i<l->map->submaps;i++){
78 l->time_func[i]->free_look(l->time_look[i]);
79 l->floor_func[i]->free_look(l->floor_look[i]);
80 l->residue_func[i]->free_look(l->residue_look[i]);
81 if(l->psy_look)_vp_psy_clear(l->psy_look+i);
86 if(l->decay[i])free(l->decay[i]);
93 free(l->residue_func);
96 free(l->residue_look);
97 if(l->psy_look)free(l->psy_look);
98 memset(l,0,sizeof(vorbis_look_mapping0));
103 static vorbis_look_mapping *mapping0_look(vorbis_dsp_state *vd,vorbis_info_mode *vm,
104 vorbis_info_mapping *m){
106 vorbis_info *vi=vd->vi;
107 codec_setup_info *ci=vi->codec_setup;
108 vorbis_look_mapping0 *look=_ogg_calloc(1,sizeof(vorbis_look_mapping0));
109 vorbis_info_mapping0 *info=look->map=(vorbis_info_mapping0 *)m;
112 look->time_look=_ogg_calloc(info->submaps,sizeof(vorbis_look_time *));
113 look->floor_look=_ogg_calloc(info->submaps,sizeof(vorbis_look_floor *));
115 look->residue_look=_ogg_calloc(info->submaps,sizeof(vorbis_look_residue *));
116 if(ci->psys)look->psy_look=_ogg_calloc(info->submaps,sizeof(vorbis_look_psy));
118 look->time_func=_ogg_calloc(info->submaps,sizeof(vorbis_func_time *));
119 look->floor_func=_ogg_calloc(info->submaps,sizeof(vorbis_func_floor *));
120 look->residue_func=_ogg_calloc(info->submaps,sizeof(vorbis_func_residue *));
122 for(i=0;i<info->submaps;i++){
123 int timenum=info->timesubmap[i];
124 int floornum=info->floorsubmap[i];
125 int resnum=info->residuesubmap[i];
127 look->time_func[i]=_time_P[ci->time_type[timenum]];
128 look->time_look[i]=look->time_func[i]->
129 look(vd,vm,ci->time_param[timenum]);
130 look->floor_func[i]=_floor_P[ci->floor_type[floornum]];
131 look->floor_look[i]=look->floor_func[i]->
132 look(vd,vm,ci->floor_param[floornum]);
133 look->residue_func[i]=_residue_P[ci->residue_type[resnum]];
134 look->residue_look[i]=look->residue_func[i]->
135 look(vd,vm,ci->residue_param[resnum]);
137 if(ci->psys && vd->analysisp){
138 int psynum=info->psysubmap[i];
139 _vp_psy_init(look->psy_look+i,ci->psy_param[psynum],
140 ci->blocksizes[vm->blockflag]/2,vi->rate);
144 look->ch=vi->channels;
146 look->decay=_ogg_calloc(vi->channels,sizeof(float *));
147 for(i=0;i<vi->channels;i++)
148 look->decay[i]=_ogg_calloc(ci->blocksizes[vm->blockflag]/2,sizeof(float));
154 static void mapping0_pack(vorbis_info *vi,vorbis_info_mapping *vm,oggpack_buffer *opb){
156 vorbis_info_mapping0 *info=(vorbis_info_mapping0 *)vm;
158 oggpack_write(opb,info->submaps-1,4);
159 /* we don't write the channel submappings if we only have one... */
161 for(i=0;i<vi->channels;i++)
162 oggpack_write(opb,info->chmuxlist[i],4);
164 for(i=0;i<info->submaps;i++){
165 oggpack_write(opb,info->timesubmap[i],8);
166 oggpack_write(opb,info->floorsubmap[i],8);
167 oggpack_write(opb,info->residuesubmap[i],8);
171 /* also responsible for range checking */
172 static vorbis_info_mapping *mapping0_unpack(vorbis_info *vi,oggpack_buffer *opb){
174 vorbis_info_mapping0 *info=_ogg_calloc(1,sizeof(vorbis_info_mapping0));
175 codec_setup_info *ci=vi->codec_setup;
176 memset(info,0,sizeof(vorbis_info_mapping0));
178 info->submaps=oggpack_read(opb,4)+1;
181 for(i=0;i<vi->channels;i++){
182 info->chmuxlist[i]=oggpack_read(opb,4);
183 if(info->chmuxlist[i]>=info->submaps)goto err_out;
186 for(i=0;i<info->submaps;i++){
187 info->timesubmap[i]=oggpack_read(opb,8);
188 if(info->timesubmap[i]>=ci->times)goto err_out;
189 info->floorsubmap[i]=oggpack_read(opb,8);
190 if(info->floorsubmap[i]>=ci->floors)goto err_out;
191 info->residuesubmap[i]=oggpack_read(opb,8);
192 if(info->residuesubmap[i]>=ci->residues)goto err_out;
198 mapping0_free_info(info);
205 #include "envelope.h"
210 /* no time mapping implementation for now */
212 static int mapping0_forward(vorbis_block *vb,vorbis_look_mapping *l){
213 vorbis_dsp_state *vd=vb->vd;
214 vorbis_info *vi=vd->vi;
215 backend_lookup_state *b=vb->vd->backend_state;
216 vorbis_look_mapping0 *look=(vorbis_look_mapping0 *)l;
217 vorbis_info_mapping0 *info=look->map;
218 vorbis_info_mode *mode=look->mode;
221 float *window=b->window[vb->W][vb->lW][vb->nW][mode->windowtype];
223 float **pcmbundle=alloca(sizeof(float *)*vi->channels);
224 int *nonzero=alloca(sizeof(int)*vi->channels);
226 /* time domain pre-window: NONE IMPLEMENTED */
228 /* window the PCM data: takes PCM vector, vb; modifies PCM vector */
230 for(i=0;i<vi->channels;i++){
231 float *pcm=vb->pcm[i];
236 /* time-domain post-window: NONE IMPLEMENTED */
238 /* transform the PCM data; takes PCM vector, vb; modifies PCM vector */
239 /* only MDCT right now.... */
240 for(i=0;i<vi->channels;i++){
241 float *pcm=vb->pcm[i];
242 mdct_forward(b->transform[vb->W][0],pcm,pcm);
246 float *floor=_vorbis_block_alloc(vb,n*sizeof(float)/2);
248 for(i=0;i<vi->channels;i++){
249 float *pcm=vb->pcm[i];
250 float *decay=look->decay[i];
251 int submap=info->chmuxlist[i];
253 /* if some other mode/mapping was called last frame, our decay
254 accumulator is out of date. Clear it. */
255 if(look->lastframe+1 != vb->sequence)
256 memset(decay,0,n*sizeof(float)/2);
258 /* perform psychoacoustics; do masking */
259 _vp_compute_mask(look->psy_look+submap,pcm,floor,decay);
261 _analysis_output("decay",seq,decay,n/2,0,1);
262 _analysis_output("mdct",seq,pcm,n/2,0,1);
263 _analysis_output("lmdct",seq,pcm,n/2,0,0);
264 _analysis_output("prefloor",seq,floor,n/2,0,1);
266 /* perform floor encoding */
267 nonzero[i]=look->floor_func[submap]->
268 forward(vb,look->floor_look[submap],floor,floor);
270 _analysis_output("floor",seq,floor,n/2,0,1);
272 /* apply the floor, do optional noise levelling */
273 _vp_apply_floor(look->psy_look+submap,pcm,floor);
275 _analysis_output("res",seq++,pcm,n/2,0,0);
283 sprintf(buffer,"residue_%d.vqd",vb->mode);
284 of=fopen(buffer,"a");
286 fprintf(of,"%.2f, ",pcm[i]);
294 /* perform residue encoding with residue mapping; this is
295 multiplexed. All the channels belonging to one submap are
296 encoded (values interleaved), then the next submap, etc */
298 for(i=0;i<info->submaps;i++){
300 for(j=0;j<vi->channels;j++){
301 if(info->chmuxlist[j]==i && nonzero[j]==1){
302 pcmbundle[ch_in_bundle++]=vb->pcm[j];
306 look->residue_func[i]->forward(vb,look->residue_look[i],
307 pcmbundle,ch_in_bundle);
311 look->lastframe=vb->sequence;
315 static int mapping0_inverse(vorbis_block *vb,vorbis_look_mapping *l){
316 vorbis_dsp_state *vd=vb->vd;
317 vorbis_info *vi=vd->vi;
318 codec_setup_info *ci=vi->codec_setup;
319 backend_lookup_state *b=vd->backend_state;
320 vorbis_look_mapping0 *look=(vorbis_look_mapping0 *)l;
321 vorbis_info_mapping0 *info=look->map;
322 vorbis_info_mode *mode=look->mode;
324 long n=vb->pcmend=ci->blocksizes[vb->W];
326 float *window=b->window[vb->W][vb->lW][vb->nW][mode->windowtype];
327 float **pcmbundle=alloca(sizeof(float *)*vi->channels);
328 int *nonzero=alloca(sizeof(int)*vi->channels);
330 /* time domain information decode (note that applying the
331 information would have to happen later; we'll probably add a
332 function entry to the harness for that later */
333 /* NOT IMPLEMENTED */
335 /* recover the spectral envelope; store it in the PCM vector for now */
336 for(i=0;i<vi->channels;i++){
337 float *pcm=vb->pcm[i];
338 int submap=info->chmuxlist[i];
339 nonzero[i]=look->floor_func[submap]->
340 inverse(vb,look->floor_look[submap],pcm);
341 _analysis_output("ifloor",seq+i,pcm,n/2,0,1);
344 /* recover the residue, apply directly to the spectral envelope */
346 for(i=0;i<info->submaps;i++){
348 for(j=0;j<vi->channels;j++){
349 if(info->chmuxlist[j]==i && nonzero[j])
350 pcmbundle[ch_in_bundle++]=vb->pcm[j];
353 look->residue_func[i]->inverse(vb,look->residue_look[i],pcmbundle,ch_in_bundle);
356 /* transform the PCM data; takes PCM vector, vb; modifies PCM vector */
357 /* only MDCT right now.... */
358 for(i=0;i<vi->channels;i++){
359 float *pcm=vb->pcm[i];
360 _analysis_output("out",seq+i,pcm,n/2,0,1);
361 mdct_backward(b->transform[vb->W][0],pcm,pcm);
364 /* now apply the decoded pre-window time information */
365 /* NOT IMPLEMENTED */
367 /* window the data */
368 for(i=0;i<vi->channels;i++){
369 float *pcm=vb->pcm[i];
376 _analysis_output("final",seq++,pcm,n,0,0);
379 /* now apply the decoded post-window time information */
380 /* NOT IMPLEMENTED */
387 vorbis_func_mapping mapping0_exportbundle={
388 &mapping0_pack,&mapping0_unpack,&mapping0_look,&mapping0_copy_info,
389 &mapping0_free_info,&mapping0_free_look,&mapping0_forward,&mapping0_inverse