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.18 2000/11/07 09:51:43 xiphmont Exp $
17 ********************************************************************/
24 #include "vorbis/codec.h"
25 #include "codec_internal.h"
31 /* simplistic, wasteful way of doing this (unique lookup for each
32 mode/submapping); there should be a central repository for
33 identical lookups. That will require minor work, so I'm putting it
36 Why a lookup for each backend in a given mode? Because the
37 blocksize is set by the mode, and low backend lookups may require
38 parameters from other areas of the mode/mapping */
41 vorbis_info_mode *mode;
42 vorbis_info_mapping0 *map;
44 vorbis_look_time **time_look;
45 vorbis_look_floor **floor_look;
47 vorbis_look_residue **residue_look;
48 vorbis_look_psy *psy_look;
50 vorbis_func_time **time_func;
51 vorbis_func_floor **floor_func;
52 vorbis_func_residue **residue_func;
56 long lastframe; /* if a different mode is called, we need to
58 } vorbis_look_mapping0;
60 static vorbis_info_mapping *mapping0_copy_info(vorbis_info_mapping *vm){
61 vorbis_info_mapping0 *info=(vorbis_info_mapping0 *)vm;
62 vorbis_info_mapping0 *ret=_ogg_malloc(sizeof(vorbis_info_mapping0));
63 memcpy(ret,info,sizeof(vorbis_info_mapping0));
67 static void mapping0_free_info(vorbis_info_mapping *i){
69 memset(i,0,sizeof(vorbis_info_mapping0));
74 static void mapping0_free_look(vorbis_look_mapping *look){
76 vorbis_look_mapping0 *l=(vorbis_look_mapping0 *)look;
78 for(i=0;i<l->map->submaps;i++){
79 l->time_func[i]->free_look(l->time_look[i]);
80 l->floor_func[i]->free_look(l->floor_look[i]);
81 l->residue_func[i]->free_look(l->residue_look[i]);
82 if(l->psy_look)_vp_psy_clear(l->psy_look+i);
87 if(l->decay[i])free(l->decay[i]);
94 free(l->residue_func);
97 free(l->residue_look);
98 if(l->psy_look)free(l->psy_look);
99 memset(l,0,sizeof(vorbis_look_mapping0));
104 static vorbis_look_mapping *mapping0_look(vorbis_dsp_state *vd,vorbis_info_mode *vm,
105 vorbis_info_mapping *m){
107 vorbis_info *vi=vd->vi;
108 codec_setup_info *ci=vi->codec_setup;
109 vorbis_look_mapping0 *look=_ogg_calloc(1,sizeof(vorbis_look_mapping0));
110 vorbis_info_mapping0 *info=look->map=(vorbis_info_mapping0 *)m;
113 look->time_look=_ogg_calloc(info->submaps,sizeof(vorbis_look_time *));
114 look->floor_look=_ogg_calloc(info->submaps,sizeof(vorbis_look_floor *));
116 look->residue_look=_ogg_calloc(info->submaps,sizeof(vorbis_look_residue *));
117 if(ci->psys)look->psy_look=_ogg_calloc(info->submaps,sizeof(vorbis_look_psy));
119 look->time_func=_ogg_calloc(info->submaps,sizeof(vorbis_func_time *));
120 look->floor_func=_ogg_calloc(info->submaps,sizeof(vorbis_func_floor *));
121 look->residue_func=_ogg_calloc(info->submaps,sizeof(vorbis_func_residue *));
123 for(i=0;i<info->submaps;i++){
124 int timenum=info->timesubmap[i];
125 int floornum=info->floorsubmap[i];
126 int resnum=info->residuesubmap[i];
128 look->time_func[i]=_time_P[ci->time_type[timenum]];
129 look->time_look[i]=look->time_func[i]->
130 look(vd,vm,ci->time_param[timenum]);
131 look->floor_func[i]=_floor_P[ci->floor_type[floornum]];
132 look->floor_look[i]=look->floor_func[i]->
133 look(vd,vm,ci->floor_param[floornum]);
134 look->residue_func[i]=_residue_P[ci->residue_type[resnum]];
135 look->residue_look[i]=look->residue_func[i]->
136 look(vd,vm,ci->residue_param[resnum]);
138 if(ci->psys && vd->analysisp){
139 int psynum=info->psysubmap[i];
140 _vp_psy_init(look->psy_look+i,ci->psy_param[psynum],
141 ci->blocksizes[vm->blockflag]/2,vi->rate);
145 look->ch=vi->channels;
147 look->decay=_ogg_calloc(vi->channels,sizeof(float *));
148 for(i=0;i<vi->channels;i++)
149 look->decay[i]=_ogg_calloc(ci->blocksizes[vm->blockflag]/2,sizeof(float));
155 static void mapping0_pack(vorbis_info *vi,vorbis_info_mapping *vm,oggpack_buffer *opb){
157 vorbis_info_mapping0 *info=(vorbis_info_mapping0 *)vm;
159 oggpack_write(opb,info->submaps-1,4);
160 /* we don't write the channel submappings if we only have one... */
162 for(i=0;i<vi->channels;i++)
163 oggpack_write(opb,info->chmuxlist[i],4);
165 for(i=0;i<info->submaps;i++){
166 oggpack_write(opb,info->timesubmap[i],8);
167 oggpack_write(opb,info->floorsubmap[i],8);
168 oggpack_write(opb,info->residuesubmap[i],8);
172 /* also responsible for range checking */
173 static vorbis_info_mapping *mapping0_unpack(vorbis_info *vi,oggpack_buffer *opb){
175 vorbis_info_mapping0 *info=_ogg_calloc(1,sizeof(vorbis_info_mapping0));
176 codec_setup_info *ci=vi->codec_setup;
177 memset(info,0,sizeof(vorbis_info_mapping0));
179 info->submaps=oggpack_read(opb,4)+1;
182 for(i=0;i<vi->channels;i++){
183 info->chmuxlist[i]=oggpack_read(opb,4);
184 if(info->chmuxlist[i]>=info->submaps)goto err_out;
187 for(i=0;i<info->submaps;i++){
188 info->timesubmap[i]=oggpack_read(opb,8);
189 if(info->timesubmap[i]>=ci->times)goto err_out;
190 info->floorsubmap[i]=oggpack_read(opb,8);
191 if(info->floorsubmap[i]>=ci->floors)goto err_out;
192 info->residuesubmap[i]=oggpack_read(opb,8);
193 if(info->residuesubmap[i]>=ci->residues)goto err_out;
199 mapping0_free_info(info);
206 #include "envelope.h"
211 /* no time mapping implementation for now */
213 static int mapping0_forward(vorbis_block *vb,vorbis_look_mapping *l){
214 vorbis_dsp_state *vd=vb->vd;
215 vorbis_info *vi=vd->vi;
216 backend_lookup_state *b=vb->vd->backend_state;
217 vorbis_look_mapping0 *look=(vorbis_look_mapping0 *)l;
218 vorbis_info_mapping0 *info=look->map;
219 vorbis_info_mode *mode=look->mode;
222 float *window=b->window[vb->W][vb->lW][vb->nW][mode->windowtype];
224 float **pcmbundle=alloca(sizeof(float *)*vi->channels);
225 int *nonzero=alloca(sizeof(int)*vi->channels);
227 /* time domain pre-window: NONE IMPLEMENTED */
229 /* window the PCM data: takes PCM vector, vb; modifies PCM vector */
231 for(i=0;i<vi->channels;i++){
232 float *pcm=vb->pcm[i];
237 /* time-domain post-window: NONE IMPLEMENTED */
239 /* transform the PCM data; takes PCM vector, vb; modifies PCM vector */
240 /* only MDCT right now.... */
241 for(i=0;i<vi->channels;i++){
242 float *pcm=vb->pcm[i];
243 mdct_forward(b->transform[vb->W][0],pcm,pcm);
247 float *floor=_vorbis_block_alloc(vb,n*sizeof(float)/2);
249 for(i=0;i<vi->channels;i++){
250 float *pcm=vb->pcm[i];
251 float *decay=look->decay[i];
252 int submap=info->chmuxlist[i];
254 /* if some other mode/mapping was called last frame, our decay
255 accumulator is out of date. Clear it. */
256 if(look->lastframe+1 != vb->sequence)
257 memset(decay,0,n*sizeof(float)/2);
259 /* perform psychoacoustics; do masking */
260 _vp_compute_mask(look->psy_look+submap,pcm,floor,decay);
262 _analysis_output("decay",seq,decay,n/2,0,1);
263 _analysis_output("mdct",seq,pcm,n/2,0,1);
264 _analysis_output("lmdct",seq,pcm,n/2,0,0);
265 _analysis_output("prefloor",seq,floor,n/2,0,1);
267 /* perform floor encoding */
268 nonzero[i]=look->floor_func[submap]->
269 forward(vb,look->floor_look[submap],floor,floor);
271 _analysis_output("floor",seq,floor,n/2,0,1);
273 /* apply the floor, do optional noise levelling */
274 _vp_apply_floor(look->psy_look+submap,pcm,floor);
276 _analysis_output("res",seq++,pcm,n/2,0,0);
284 sprintf(buffer,"residue_%d.vqd",vb->mode);
285 of=fopen(buffer,"a");
287 fprintf(of,"%.2f, ",pcm[i]);
295 /* perform residue encoding with residue mapping; this is
296 multiplexed. All the channels belonging to one submap are
297 encoded (values interleaved), then the next submap, etc */
299 for(i=0;i<info->submaps;i++){
301 for(j=0;j<vi->channels;j++){
302 if(info->chmuxlist[j]==i && nonzero[j]==1){
303 pcmbundle[ch_in_bundle++]=vb->pcm[j];
307 look->residue_func[i]->forward(vb,look->residue_look[i],
308 pcmbundle,ch_in_bundle);
312 look->lastframe=vb->sequence;
316 static int mapping0_inverse(vorbis_block *vb,vorbis_look_mapping *l){
317 vorbis_dsp_state *vd=vb->vd;
318 vorbis_info *vi=vd->vi;
319 codec_setup_info *ci=vi->codec_setup;
320 backend_lookup_state *b=vd->backend_state;
321 vorbis_look_mapping0 *look=(vorbis_look_mapping0 *)l;
322 vorbis_info_mapping0 *info=look->map;
323 vorbis_info_mode *mode=look->mode;
325 long n=vb->pcmend=ci->blocksizes[vb->W];
327 float *window=b->window[vb->W][vb->lW][vb->nW][mode->windowtype];
328 float **pcmbundle=alloca(sizeof(float *)*vi->channels);
329 int *nonzero=alloca(sizeof(int)*vi->channels);
331 /* time domain information decode (note that applying the
332 information would have to happen later; we'll probably add a
333 function entry to the harness for that later */
334 /* NOT IMPLEMENTED */
336 /* recover the spectral envelope; store it in the PCM vector for now */
337 for(i=0;i<vi->channels;i++){
338 float *pcm=vb->pcm[i];
339 int submap=info->chmuxlist[i];
340 nonzero[i]=look->floor_func[submap]->
341 inverse(vb,look->floor_look[submap],pcm);
342 _analysis_output("ifloor",seq+i,pcm,n/2,0,1);
345 /* recover the residue, apply directly to the spectral envelope */
347 for(i=0;i<info->submaps;i++){
349 for(j=0;j<vi->channels;j++){
350 if(info->chmuxlist[j]==i && nonzero[j])
351 pcmbundle[ch_in_bundle++]=vb->pcm[j];
354 look->residue_func[i]->inverse(vb,look->residue_look[i],pcmbundle,ch_in_bundle);
357 /* transform the PCM data; takes PCM vector, vb; modifies PCM vector */
358 /* only MDCT right now.... */
359 for(i=0;i<vi->channels;i++){
360 float *pcm=vb->pcm[i];
361 _analysis_output("out",seq+i,pcm,n/2,0,1);
362 mdct_backward(b->transform[vb->W][0],pcm,pcm);
365 /* now apply the decoded pre-window time information */
366 /* NOT IMPLEMENTED */
368 /* window the data */
369 for(i=0;i<vi->channels;i++){
370 float *pcm=vb->pcm[i];
377 _analysis_output("final",seq++,pcm,n,0,0);
380 /* now apply the decoded post-window time information */
381 /* NOT IMPLEMENTED */
388 vorbis_func_mapping mapping0_exportbundle={
389 &mapping0_pack,&mapping0_unpack,&mapping0_look,&mapping0_copy_info,
390 &mapping0_free_info,&mapping0_free_look,&mapping0_forward,&mapping0_inverse