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.21 2000/12/12 08:54:27 xiphmont Exp $
17 ********************************************************************/
24 #include "vorbis/codec.h"
25 #include "codec_internal.h"
27 #include "bitbuffer.h"
32 /* simplistic, wasteful way of doing this (unique lookup for each
33 mode/submapping); there should be a central repository for
34 identical lookups. That will require minor work, so I'm putting it
37 Why a lookup for each backend in a given mode? Because the
38 blocksize is set by the mode, and low backend lookups may require
39 parameters from other areas of the mode/mapping */
42 vorbis_info_mode *mode;
43 vorbis_info_mapping0 *map;
45 vorbis_look_time **time_look;
46 vorbis_look_floor **floor_look;
48 vorbis_look_residue **residue_look;
49 vorbis_look_psy *psy_look;
51 vorbis_func_time **time_func;
52 vorbis_func_floor **floor_func;
53 vorbis_func_residue **residue_func;
57 long lastframe; /* if a different mode is called, we need to
59 } vorbis_look_mapping0;
61 static vorbis_info_mapping *mapping0_copy_info(vorbis_info_mapping *vm){
62 vorbis_info_mapping0 *info=(vorbis_info_mapping0 *)vm;
63 vorbis_info_mapping0 *ret=_ogg_malloc(sizeof(vorbis_info_mapping0));
64 memcpy(ret,info,sizeof(vorbis_info_mapping0));
68 static void mapping0_free_info(vorbis_info_mapping *i){
70 memset(i,0,sizeof(vorbis_info_mapping0));
75 static void mapping0_free_look(vorbis_look_mapping *look){
77 vorbis_look_mapping0 *l=(vorbis_look_mapping0 *)look;
79 for(i=0;i<l->map->submaps;i++){
80 l->time_func[i]->free_look(l->time_look[i]);
81 l->floor_func[i]->free_look(l->floor_look[i]);
82 l->residue_func[i]->free_look(l->residue_look[i]);
83 if(l->psy_look)_vp_psy_clear(l->psy_look+i);
88 if(l->decay[i])_ogg_free(l->decay[i]);
93 _ogg_free(l->time_func);
94 _ogg_free(l->floor_func);
95 _ogg_free(l->residue_func);
96 _ogg_free(l->time_look);
97 _ogg_free(l->floor_look);
98 _ogg_free(l->residue_look);
99 if(l->psy_look)_ogg_free(l->psy_look);
100 memset(l,0,sizeof(vorbis_look_mapping0));
105 static vorbis_look_mapping *mapping0_look(vorbis_dsp_state *vd,vorbis_info_mode *vm,
106 vorbis_info_mapping *m){
108 vorbis_info *vi=vd->vi;
109 codec_setup_info *ci=vi->codec_setup;
110 vorbis_look_mapping0 *look=_ogg_calloc(1,sizeof(vorbis_look_mapping0));
111 vorbis_info_mapping0 *info=look->map=(vorbis_info_mapping0 *)m;
114 look->time_look=_ogg_calloc(info->submaps,sizeof(vorbis_look_time *));
115 look->floor_look=_ogg_calloc(info->submaps,sizeof(vorbis_look_floor *));
117 look->residue_look=_ogg_calloc(info->submaps,sizeof(vorbis_look_residue *));
118 if(ci->psys)look->psy_look=_ogg_calloc(info->submaps,sizeof(vorbis_look_psy));
120 look->time_func=_ogg_calloc(info->submaps,sizeof(vorbis_func_time *));
121 look->floor_func=_ogg_calloc(info->submaps,sizeof(vorbis_func_floor *));
122 look->residue_func=_ogg_calloc(info->submaps,sizeof(vorbis_func_residue *));
124 for(i=0;i<info->submaps;i++){
125 int timenum=info->timesubmap[i];
126 int floornum=info->floorsubmap[i];
127 int resnum=info->residuesubmap[i];
129 look->time_func[i]=_time_P[ci->time_type[timenum]];
130 look->time_look[i]=look->time_func[i]->
131 look(vd,vm,ci->time_param[timenum]);
132 look->floor_func[i]=_floor_P[ci->floor_type[floornum]];
133 look->floor_look[i]=look->floor_func[i]->
134 look(vd,vm,ci->floor_param[floornum]);
135 look->residue_func[i]=_residue_P[ci->residue_type[resnum]];
136 look->residue_look[i]=look->residue_func[i]->
137 look(vd,vm,ci->residue_param[resnum]);
139 if(ci->psys && vd->analysisp){
140 int psynum=info->psysubmap[i];
141 _vp_psy_init(look->psy_look+i,ci->psy_param[psynum],
142 ci->blocksizes[vm->blockflag]/2,vi->rate);
146 look->ch=vi->channels;
148 look->decay=_ogg_calloc(vi->channels,sizeof(float *));
149 for(i=0;i<vi->channels;i++)
150 look->decay[i]=_ogg_calloc(ci->blocksizes[vm->blockflag]/2,sizeof(float));
156 static void mapping0_pack(vorbis_info *vi,vorbis_info_mapping *vm,oggpack_buffer *opb){
158 vorbis_info_mapping0 *info=(vorbis_info_mapping0 *)vm;
160 oggpack_write(opb,info->submaps-1,4);
161 /* we don't write the channel submappings if we only have one... */
163 for(i=0;i<vi->channels;i++)
164 oggpack_write(opb,info->chmuxlist[i],4);
166 for(i=0;i<info->submaps;i++){
167 oggpack_write(opb,info->timesubmap[i],8);
168 oggpack_write(opb,info->floorsubmap[i],8);
169 oggpack_write(opb,info->residuesubmap[i],8);
173 /* also responsible for range checking */
174 static vorbis_info_mapping *mapping0_unpack(vorbis_info *vi,oggpack_buffer *opb){
176 vorbis_info_mapping0 *info=_ogg_calloc(1,sizeof(vorbis_info_mapping0));
177 codec_setup_info *ci=vi->codec_setup;
178 memset(info,0,sizeof(vorbis_info_mapping0));
180 info->submaps=oggpack_read(opb,4)+1;
183 for(i=0;i<vi->channels;i++){
184 info->chmuxlist[i]=oggpack_read(opb,4);
185 if(info->chmuxlist[i]>=info->submaps)goto err_out;
188 for(i=0;i<info->submaps;i++){
189 info->timesubmap[i]=oggpack_read(opb,8);
190 if(info->timesubmap[i]>=ci->times)goto err_out;
191 info->floorsubmap[i]=oggpack_read(opb,8);
192 if(info->floorsubmap[i]>=ci->floors)goto err_out;
193 info->residuesubmap[i]=oggpack_read(opb,8);
194 if(info->residuesubmap[i]>=ci->residues)goto err_out;
200 mapping0_free_info(info);
207 #include "envelope.h"
212 /* no time mapping implementation for now */
214 static int mapping0_forward(vorbis_block *vb,vorbis_look_mapping *l){
215 vorbis_dsp_state *vd=vb->vd;
216 vorbis_info *vi=vd->vi;
217 backend_lookup_state *b=vb->vd->backend_state;
218 vorbis_look_mapping0 *look=(vorbis_look_mapping0 *)l;
219 vorbis_info_mapping0 *info=look->map;
220 vorbis_info_mode *mode=look->mode;
223 float *window=b->window[vb->W][vb->lW][vb->nW][mode->windowtype];
225 float **pcmbundle=alloca(sizeof(float *)*vi->channels);
227 int *nonzero=alloca(sizeof(int)*vi->channels);
228 float **floor=_vorbis_block_alloc(vb,vi->channels*sizeof(float *));
229 float **nonquant=_vorbis_block_alloc(vb,vi->channels*sizeof(float *));
231 vorbis_bitbuffer vbb_res;
232 vorbis_bitbuffer *vbb_flr=_vorbis_block_alloc(vb,
233 sizeof(vorbis_bitbuffer)*
236 for(i=0;i<vi->channels;i++)
237 bitbuf_init(vbb_flr+i,vb);
238 bitbuf_init(&vbb_res,vb);
240 /* time domain pre-window: NONE IMPLEMENTED */
242 /* window the PCM data: takes PCM vector, vb; modifies PCM vector */
244 for(i=0;i<vi->channels;i++){
245 float *pcm=vb->pcm[i];
250 /* time-domain post-window: NONE IMPLEMENTED */
252 /* transform the PCM data; takes PCM vector, vb; modifies PCM vector */
253 /* only MDCT right now.... */
254 for(i=0;i<vi->channels;i++){
255 float *pcm=vb->pcm[i];
256 float *outq=nonquant[i]=_vorbis_block_alloc(vb,n*sizeof(float));
257 mdct_forward(b->transform[vb->W][0],pcm,pcm);
259 memcpy(pcm+n/2,pcm,n*sizeof(float)/2);
260 mdct_backward(b->transform[vb->W][0],pcm,outq);
267 for(i=0;i<vi->channels;i++){
268 float *pcm=vb->pcm[i];
269 float *decay=look->decay[i];
270 int submap=info->chmuxlist[i];
271 floor[i]=_vorbis_block_alloc(vb,n*sizeof(float)/2);
273 /* if some other mode/mapping was called last frame, our decay
274 accumulator is out of date. Clear it. */
275 if(look->lastframe+1 != vb->sequence)
276 memset(decay,0,n*sizeof(float)/2);
278 /* perform psychoacoustics; do masking */
279 _vp_compute_mask(look->psy_look+submap,pcm,floor[i],decay);
281 _analysis_output("decay",seq,decay,n/2,0,1);
282 _analysis_output("mdct",seq,pcm,n/2,0,1);
283 _analysis_output("lmdct",seq,pcm,n/2,0,0);
284 _analysis_output("prefloor",seq,floor[i],n/2,0,1);
286 /* perform floor encoding */
287 nonzero[i]=look->floor_func[submap]->
288 forward(vb,look->floor_look[submap],floor[i],floor[i],vbb_flr+i);
290 _analysis_output("floor",seq,floor[i],n/2,0,1);
292 /* apply the floor, do optional noise levelling */
293 _vp_apply_floor(look->psy_look+submap,pcm,floor[i]);
295 _analysis_output("res",seq++,pcm,n/2,0,0);
303 sprintf(buffer,"residue_%d.vqd",vb->mode);
304 of=fopen(buffer,"a");
306 fprintf(of,"%.2f, ",pcm[i]);
314 /* perform residue encoding with residue mapping; this is
315 multiplexed. All the channels belonging to one submap are
316 encoded (values interleaved), then the next submap, etc */
318 for(i=0;i<info->submaps;i++){
320 for(j=0;j<vi->channels;j++){
321 if(info->chmuxlist[j]==i && nonzero[j]>0){
322 pcmbundle[ch_in_bundle++]=vb->pcm[j];
326 look->residue_func[i]->forward(vb,look->residue_look[i],
327 pcmbundle,ch_in_bundle,&vbb_res);
330 /* go back and compute the original MDCT inverse and our quantized
331 inverse; we'll want to give the floor mapping a shot at
332 massaging things before we write out the final version. */
335 for(i=0;i<vi->channels;i++){
336 float *pcm=vb->pcm[i];
337 float *pcmori=vb->pcm[i]+n/2;
338 float *orig=nonquant[i];
339 float *quant=floor[i];
341 int submap=info->chmuxlist[i];
346 pcm[j]=pcmori[j]-pcm[j]*quant[j];
348 mdct_backward(b->transform[vb->W][0],pcm,pcm);
352 /* weighted compare twixt original and quantized mdct output */
354 den+=(pcm[j]*pcm[j]);
355 num+=(orig[j]*orig[j]);
363 num=look->floor_func[submap]->
364 forward2(vb,look->floor_look[submap],
365 nonzero[i],den==0.?0:num/den,vbb_flr+i);
369 _analysis_output("i_orig",seq,orig,n,0,0);
370 _analysis_output("i_quant",seq++,pcm,n,0,0);
376 bitbuf_pack(&vb->opb,&vbb_res);
380 look->lastframe=vb->sequence;
384 static int mapping0_inverse(vorbis_block *vb,vorbis_look_mapping *l){
385 vorbis_dsp_state *vd=vb->vd;
386 vorbis_info *vi=vd->vi;
387 codec_setup_info *ci=vi->codec_setup;
388 backend_lookup_state *b=vd->backend_state;
389 vorbis_look_mapping0 *look=(vorbis_look_mapping0 *)l;
390 vorbis_info_mapping0 *info=look->map;
391 vorbis_info_mode *mode=look->mode;
393 long n=vb->pcmend=ci->blocksizes[vb->W];
395 float *window=b->window[vb->W][vb->lW][vb->nW][mode->windowtype];
396 float **pcmbundle=alloca(sizeof(float *)*vi->channels);
397 int *nonzero=alloca(sizeof(int)*vi->channels);
399 /* time domain information decode (note that applying the
400 information would have to happen later; we'll probably add a
401 function entry to the harness for that later */
402 /* NOT IMPLEMENTED */
404 /* recover the spectral envelope; store it in the PCM vector for now */
405 for(i=0;i<vi->channels;i++){
406 float *pcm=vb->pcm[i];
407 int submap=info->chmuxlist[i];
408 nonzero[i]=look->floor_func[submap]->
409 inverse(vb,look->floor_look[submap],pcm);
410 _analysis_output("ifloor",seq+i,pcm,n/2,0,1);
413 /* recover the residue, apply directly to the spectral envelope */
415 for(i=0;i<info->submaps;i++){
417 for(j=0;j<vi->channels;j++){
418 if(info->chmuxlist[j]==i && nonzero[j])
419 pcmbundle[ch_in_bundle++]=vb->pcm[j];
422 look->residue_func[i]->inverse(vb,look->residue_look[i],pcmbundle,ch_in_bundle);
425 /* transform the PCM data; takes PCM vector, vb; modifies PCM vector */
426 /* only MDCT right now.... */
427 for(i=0;i<vi->channels;i++){
428 float *pcm=vb->pcm[i];
429 _analysis_output("out",seq+i,pcm,n/2,0,1);
430 mdct_backward(b->transform[vb->W][0],pcm,pcm);
433 /* now apply the decoded pre-window time information */
434 /* NOT IMPLEMENTED */
436 /* window the data */
437 for(i=0;i<vi->channels;i++){
438 float *pcm=vb->pcm[i];
445 _analysis_output("final",seq++,pcm,n,0,0);
448 /* now apply the decoded post-window time information */
449 /* NOT IMPLEMENTED */
456 vorbis_func_mapping mapping0_exportbundle={
457 &mapping0_pack,&mapping0_unpack,&mapping0_look,&mapping0_copy_info,
458 &mapping0_free_info,&mapping0_free_look,&mapping0_forward,&mapping0_inverse