1 /********************************************************************
3 * THIS FILE IS PART OF THE OggVorbis SOFTWARE CODEC SOURCE CODE. *
4 * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS *
5 * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE *
6 * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. *
8 * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2001 *
9 * by the XIPHOPHORUS Company http://www.xiph.org/ *
11 ********************************************************************
13 function: channel mapping 0 implementation
14 last mod: $Id: mapping0.c,v 1.33 2001/06/17 22:25:50 xiphmont Exp $
16 ********************************************************************/
23 #include "vorbis/codec.h"
24 #include "codec_internal.h"
26 #include "bitbuffer.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 */
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;
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 drft_clear(&l->fft_look);
80 for(i=0;i<l->map->submaps;i++){
81 l->time_func[i]->free_look(l->time_look[i]);
82 l->floor_func[i]->free_look(l->floor_look[i]);
83 l->residue_func[i]->free_look(l->residue_look[i]);
84 if(l->psy_look)_vp_psy_clear(l->psy_look+i);
87 _ogg_free(l->time_func);
88 _ogg_free(l->floor_func);
89 _ogg_free(l->residue_func);
90 _ogg_free(l->time_look);
91 _ogg_free(l->floor_look);
92 _ogg_free(l->residue_look);
93 if(l->psy_look)_ogg_free(l->psy_look);
94 memset(l,0,sizeof(vorbis_look_mapping0));
99 static vorbis_look_mapping *mapping0_look(vorbis_dsp_state *vd,vorbis_info_mode *vm,
100 vorbis_info_mapping *m){
102 vorbis_info *vi=vd->vi;
103 codec_setup_info *ci=vi->codec_setup;
104 vorbis_look_mapping0 *look=_ogg_calloc(1,sizeof(vorbis_look_mapping0));
105 vorbis_info_mapping0 *info=look->map=(vorbis_info_mapping0 *)m;
108 look->time_look=_ogg_calloc(info->submaps,sizeof(vorbis_look_time *));
109 look->floor_look=_ogg_calloc(info->submaps,sizeof(vorbis_look_floor *));
111 look->residue_look=_ogg_calloc(info->submaps,sizeof(vorbis_look_residue *));
112 if(ci->psys)look->psy_look=_ogg_calloc(info->submaps,sizeof(vorbis_look_psy));
114 look->time_func=_ogg_calloc(info->submaps,sizeof(vorbis_func_time *));
115 look->floor_func=_ogg_calloc(info->submaps,sizeof(vorbis_func_floor *));
116 look->residue_func=_ogg_calloc(info->submaps,sizeof(vorbis_func_residue *));
118 for(i=0;i<info->submaps;i++){
119 int timenum=info->timesubmap[i];
120 int floornum=info->floorsubmap[i];
121 int resnum=info->residuesubmap[i];
123 look->time_func[i]=_time_P[ci->time_type[timenum]];
124 look->time_look[i]=look->time_func[i]->
125 look(vd,vm,ci->time_param[timenum]);
126 look->floor_func[i]=_floor_P[ci->floor_type[floornum]];
127 look->floor_look[i]=look->floor_func[i]->
128 look(vd,vm,ci->floor_param[floornum]);
129 look->residue_func[i]=_residue_P[ci->residue_type[resnum]];
130 look->residue_look[i]=look->residue_func[i]->
131 look(vd,vm,ci->residue_param[resnum]);
133 if(ci->psys && vd->analysisp){
134 int psynum=info->psysubmap[i];
135 _vp_psy_init(look->psy_look+i,ci->psy_param[psynum],
136 ci->blocksizes[vm->blockflag]/2,vi->rate);
140 look->ch=vi->channels;
142 if(vd->analysisp)drft_init(&look->fft_look,ci->blocksizes[vm->blockflag]);
146 static int ilog2(unsigned int v){
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 /* another 'we meant to do it this way' hack... up to beta 4, we
160 packed 4 binary zeros here to signify one submapping in use. We
161 now redefine that to mean four bitflags that indicate use of
162 deeper features; bit0:submappings, bit1:coupling,
163 bit2,3:reserved. This is backward compatable with all actual uses
167 oggpack_write(opb,1,1);
168 oggpack_write(opb,info->submaps-1,4);
170 oggpack_write(opb,0,1);
172 if(info->coupling_steps>0){
173 oggpack_write(opb,1,1);
174 oggpack_write(opb,info->coupling_steps-1,8);
176 for(i=0;i<info->coupling_steps;i++){
177 oggpack_write(opb,info->coupling_mag[i],ilog2(vi->channels));
178 oggpack_write(opb,info->coupling_ang[i],ilog2(vi->channels));
181 oggpack_write(opb,0,1);
183 oggpack_write(opb,0,2); /* 2,3:reserved */
185 /* we don't write the channel submappings if we only have one... */
187 for(i=0;i<vi->channels;i++)
188 oggpack_write(opb,info->chmuxlist[i],4);
190 for(i=0;i<info->submaps;i++){
191 oggpack_write(opb,info->timesubmap[i],8);
192 oggpack_write(opb,info->floorsubmap[i],8);
193 oggpack_write(opb,info->residuesubmap[i],8);
197 /* also responsible for range checking */
198 static vorbis_info_mapping *mapping0_unpack(vorbis_info *vi,oggpack_buffer *opb){
200 vorbis_info_mapping0 *info=_ogg_calloc(1,sizeof(vorbis_info_mapping0));
201 codec_setup_info *ci=vi->codec_setup;
202 memset(info,0,sizeof(vorbis_info_mapping0));
204 if(oggpack_read(opb,1))
205 info->submaps=oggpack_read(opb,4)+1;
209 if(oggpack_read(opb,1)){
210 info->coupling_steps=oggpack_read(opb,8)+1;
212 for(i=0;i<info->coupling_steps;i++){
213 int testM=info->coupling_mag[i]=oggpack_read(opb,ilog2(vi->channels));
214 int testA=info->coupling_ang[i]=oggpack_read(opb,ilog2(vi->channels));
219 testM>=vi->channels ||
220 testA>=vi->channels) goto err_out;
225 if(oggpack_read(opb,2)>0)goto err_out; /* 2,3:reserved */
228 for(i=0;i<vi->channels;i++){
229 info->chmuxlist[i]=oggpack_read(opb,4);
230 if(info->chmuxlist[i]>=info->submaps)goto err_out;
233 for(i=0;i<info->submaps;i++){
234 info->timesubmap[i]=oggpack_read(opb,8);
235 if(info->timesubmap[i]>=ci->times)goto err_out;
236 info->floorsubmap[i]=oggpack_read(opb,8);
237 if(info->floorsubmap[i]>=ci->floors)goto err_out;
238 info->residuesubmap[i]=oggpack_read(opb,8);
239 if(info->residuesubmap[i]>=ci->residues)goto err_out;
245 mapping0_free_info(info);
252 #include "envelope.h"
257 /* no time mapping implementation for now */
259 static int mapping0_forward(vorbis_block *vb,vorbis_look_mapping *l){
260 vorbis_dsp_state *vd=vb->vd;
261 vorbis_info *vi=vd->vi;
262 backend_lookup_state *b=vb->vd->backend_state;
263 vorbis_look_mapping0 *look=(vorbis_look_mapping0 *)l;
264 vorbis_info_mapping0 *info=look->map;
265 vorbis_info_mode *mode=look->mode;
266 vorbis_block_internal *vbi=(vorbis_block_internal *)vb->internal;
269 float *window=b->window[vb->W][vb->lW][vb->nW][mode->windowtype];
271 float **pcmbundle=alloca(sizeof(float *)*vi->channels);
272 int *zerobundle=alloca(sizeof(int)*vi->channels);
274 int *nonzero=alloca(sizeof(int)*vi->channels);
276 float *work=_vorbis_block_alloc(vb,n*sizeof(float));
277 float newmax=vbi->ampmax;
279 for(i=0;i<vi->channels;i++){
281 int submap=info->chmuxlist[i];
284 /* the following makes things clearer to *me* anyway */
285 float *pcm =vb->pcm[i];
287 float *logmdct =pcm+n/2;
289 float *codedflr=pcm+n/2;
293 float *logmask =work+n/2;
295 /* window the PCM data */
297 fft[j]=pcm[j]*=window[j];
299 /* transform the PCM data */
300 /* only MDCT right now.... */
301 mdct_forward(b->transform[vb->W][0],pcm,pcm);
303 logmdct[j]=todB(mdct+j);
305 /* FFT yields more accurate tonal estimation (not phase sensitive) */
306 drft_forward(&look->fft_look,fft);
310 float temp=scale*FAST_HYPOT(fft[j],fft[j+1]);
311 logfft[(j+1)>>1]=todB(&temp);
314 _analysis_output("fft",seq,logfft,n/2,0,0);
315 _analysis_output("mdct",seq,logmdct,n/2,0,0);
317 /* perform psychoacoustics; do masking */
318 ret=_vp_compute_mask(look->psy_look+submap,
319 logfft, /* -> logmax */
323 if(ret>newmax)newmax=ret;
325 _analysis_output("mask",seq,logmask,n/2,0,0);
327 /* perform floor encoding */
328 nonzero[i]=look->floor_func[submap]->
329 forward(vb,look->floor_look[submap],
338 if(fabs(vb->pcm[i][j]>200))
339 fprintf(stderr,"%ld ",seq);*/
341 _analysis_output("res",seq-vi->channels+j,vb->pcm[i],n,0,0);
342 _analysis_output("codedflr",seq++,codedflr,n/2,0,1);
348 /* channel coupling */
349 for(i=0;i<info->coupling_steps;i++){
350 if(nonzero[info->coupling_mag[i]] ||
351 nonzero[info->coupling_ang[i]]){
353 float *pcmM=vb->pcm[info->coupling_mag[i]];
354 float *pcmA=vb->pcm[info->coupling_ang[i]];
363 -+ 2-----+-----2----A ++
373 nonzero[info->coupling_mag[i]]=1;
374 nonzero[info->coupling_ang[i]]=1;
376 for(j=n/2-1;j>=0;j--){
377 float A=rint(pcmM[j]);
378 float B=rint(pcmA[j]);
397 ang=rint(ang/(mag*2.f))*mag*2.f;*/
405 if(ang>=fabs(mag*2))ang=-fabs(mag*2);*/
413 /* perform residue encoding with residue mapping; this is
414 multiplexed. All the channels belonging to one submap are
415 encoded (values interleaved), then the next submap, etc */
417 for(i=0;i<info->submaps;i++){
419 for(j=0;j<vi->channels;j++){
420 if(info->chmuxlist[j]==i){
422 zerobundle[ch_in_bundle]=1;
424 zerobundle[ch_in_bundle]=0;
425 pcmbundle[ch_in_bundle++]=vb->pcm[j];
429 look->residue_func[i]->forward(vb,look->residue_look[i],
430 pcmbundle,zerobundle,ch_in_bundle);
433 look->lastframe=vb->sequence;
437 static int mapping0_inverse(vorbis_block *vb,vorbis_look_mapping *l){
438 vorbis_dsp_state *vd=vb->vd;
439 vorbis_info *vi=vd->vi;
440 codec_setup_info *ci=vi->codec_setup;
441 backend_lookup_state *b=vd->backend_state;
442 vorbis_look_mapping0 *look=(vorbis_look_mapping0 *)l;
443 vorbis_info_mapping0 *info=look->map;
444 vorbis_info_mode *mode=look->mode;
446 long n=vb->pcmend=ci->blocksizes[vb->W];
448 float *window=b->window[vb->W][vb->lW][vb->nW][mode->windowtype];
449 float **pcmbundle=alloca(sizeof(float *)*vi->channels);
450 int *zerobundle=alloca(sizeof(int)*vi->channels);
452 int *nonzero =alloca(sizeof(int)*vi->channels);
453 void **floormemo=alloca(sizeof(void *)*vi->channels);
455 /* time domain information decode (note that applying the
456 information would have to happen later; we'll probably add a
457 function entry to the harness for that later */
458 /* NOT IMPLEMENTED */
460 /* recover the spectral envelope; store it in the PCM vector for now */
461 for(i=0;i<vi->channels;i++){
462 int submap=info->chmuxlist[i];
463 floormemo[i]=look->floor_func[submap]->
464 inverse1(vb,look->floor_look[submap]);
469 memset(vb->pcm[i],0,sizeof(float)*n/2);
472 /* channel coupling can 'dirty' the nonzero listing */
473 for(i=0;i<info->coupling_steps;i++){
474 if(nonzero[info->coupling_mag[i]] ||
475 nonzero[info->coupling_ang[i]]){
476 nonzero[info->coupling_mag[i]]=1;
477 nonzero[info->coupling_ang[i]]=1;
481 /* recover the residue into our working vectors */
482 for(i=0;i<info->submaps;i++){
484 for(j=0;j<vi->channels;j++){
485 if(info->chmuxlist[j]==i){
487 zerobundle[ch_in_bundle]=1;
489 zerobundle[ch_in_bundle]=0;
490 pcmbundle[ch_in_bundle++]=vb->pcm[j];
494 look->residue_func[i]->inverse(vb,look->residue_look[i],
495 pcmbundle,zerobundle,ch_in_bundle);
498 /* channel coupling */
499 for(i=info->coupling_steps-1;i>=0;i--){
500 float *pcmM=vb->pcm[info->coupling_mag[i]];
501 float *pcmA=vb->pcm[info->coupling_ang[i]];
526 /* compute and apply spectral envelope */
527 for(i=0;i<vi->channels;i++){
528 float *pcm=vb->pcm[i];
529 int submap=info->chmuxlist[i];
530 look->floor_func[submap]->
531 inverse2(vb,look->floor_look[submap],floormemo[i],pcm);
534 /* transform the PCM data; takes PCM vector, vb; modifies PCM vector */
535 /* only MDCT right now.... */
536 for(i=0;i<vi->channels;i++){
537 float *pcm=vb->pcm[i];
538 _analysis_output("out",seq+i,pcm,n/2,0,1);
539 mdct_backward(b->transform[vb->W][0],pcm,pcm);
542 /* window the data */
543 for(i=0;i<vi->channels;i++){
544 float *pcm=vb->pcm[i];
551 _analysis_output("final",seq++,pcm,n,0,0);
554 /* now apply the decoded post-window time information */
555 /* NOT IMPLEMENTED */
562 vorbis_func_mapping mapping0_exportbundle={
563 &mapping0_pack,&mapping0_unpack,&mapping0_look,&mapping0_copy_info,
564 &mapping0_free_info,&mapping0_free_look,&mapping0_forward,&mapping0_inverse