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.27 2001/02/26 13:31:31 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 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=_ogg_calloc(1,sizeof(vorbis_info_mapping0));
167 codec_setup_info *ci=vi->codec_setup;
168 memset(info,0,sizeof(vorbis_info_mapping0));
170 info->submaps=oggpack_read(opb,4)+1;
173 for(i=0;i<vi->channels;i++){
174 info->chmuxlist[i]=oggpack_read(opb,4);
175 if(info->chmuxlist[i]>=info->submaps)goto err_out;
178 for(i=0;i<info->submaps;i++){
179 info->timesubmap[i]=oggpack_read(opb,8);
180 if(info->timesubmap[i]>=ci->times)goto err_out;
181 info->floorsubmap[i]=oggpack_read(opb,8);
182 if(info->floorsubmap[i]>=ci->floors)goto err_out;
183 info->residuesubmap[i]=oggpack_read(opb,8);
184 if(info->residuesubmap[i]>=ci->residues)goto err_out;
190 mapping0_free_info(info);
197 #include "envelope.h"
202 /* no time mapping implementation for now */
204 static int mapping0_forward(vorbis_block *vb,vorbis_look_mapping *l){
205 vorbis_dsp_state *vd=vb->vd;
206 vorbis_info *vi=vd->vi;
207 backend_lookup_state *b=vb->vd->backend_state;
208 vorbis_look_mapping0 *look=(vorbis_look_mapping0 *)l;
209 vorbis_info_mapping0 *info=look->map;
210 vorbis_info_mode *mode=look->mode;
211 vorbis_block_internal *vbi=(vorbis_block_internal *)vb->internal;
214 float *window=b->window[vb->W][vb->lW][vb->nW][mode->windowtype];
216 float **pcmbundle=alloca(sizeof(float *)*vi->channels);
218 int *nonzero=alloca(sizeof(int)*vi->channels);
220 float **floor=_vorbis_block_alloc(vb,vi->channels*sizeof(float *));
221 float *additional=_vorbis_block_alloc(vb,n*sizeof(float));
222 float newmax=vbi->ampmax;
224 for(i=0;i<vi->channels;i++){
225 float *pcm=vb->pcm[i];
227 int submap=info->chmuxlist[i];
230 _analysis_output("pcm",seq,pcm,n,0,0);
232 /* window the PCM data */
234 additional[j]=pcm[j]*=window[j];
236 _analysis_output("windowed",seq,pcm,n,0,0);
238 /* transform the PCM data */
239 /* only MDCT right now.... */
240 mdct_forward(b->transform[vb->W][0],pcm,pcm);
242 /* FFT yields more accurate tonal estimation (not phase sensitive) */
243 drft_forward(&look->fft_look,additional);
245 additional[0]=fabs(additional[0]*scale);
247 additional[(j+1)>>1]=scale*FAST_HYPOT(additional[j],additional[j+1]);
249 /* set up our masking data working vector, and stash unquantized
251 /*memcpy(pcm+n/2,pcm,n*sizeof(float)/2);*/
252 memcpy(additional+n/2,pcm,n*sizeof(float)/2);
254 /* begin masking work */
255 floor[i]=_vorbis_block_alloc(vb,n*sizeof(float)/2);
257 _analysis_output("fft",seq,additional,n/2,0,1);
258 _analysis_output("mdct",seq,additional+n/2,n/2,0,1);
259 _analysis_output("lfft",seq,additional,n/2,0,0);
260 _analysis_output("lmdct",seq,additional+n/2,n/2,0,0);
262 /* perform psychoacoustics; do masking */
263 ret=_vp_compute_mask(look->psy_look+submap,additional,additional+n/2,
264 floor[i],NULL,vbi->ampmax);
265 if(ret>newmax)newmax=ret;
267 _analysis_output("prefloor",seq,floor[i],n/2,0,0);
269 /* perform floor encoding */
270 nonzero[i]=look->floor_func[submap]->
271 forward(vb,look->floor_look[submap],floor[i]);
273 _analysis_output("floor",seq,floor[i],n/2,0,1);
275 /* apply the floor, do optional noise levelling */
276 _vp_apply_floor(look->psy_look+submap,pcm,floor[i]);
278 _analysis_output("res",seq++,pcm,n/2,0,0);
286 sprintf(buffer,"residue_%d.vqd",vb->mode);
287 of=fopen(buffer,"a");
289 fprintf(of,"%.2f, ",pcm[i]);
290 if(fabs(pcm[i])>1000){
291 fprintf(stderr," %d ",seq-1);
303 /* perform residue encoding with residue mapping; this is
304 multiplexed. All the channels belonging to one submap are
305 encoded (values interleaved), then the next submap, etc */
307 for(i=0;i<info->submaps;i++){
309 for(j=0;j<vi->channels;j++){
310 if(info->chmuxlist[j]==i && nonzero[j]>0){
311 pcmbundle[ch_in_bundle++]=vb->pcm[j];
315 look->residue_func[i]->forward(vb,look->residue_look[i],
316 pcmbundle,ch_in_bundle);
319 look->lastframe=vb->sequence;
323 static int mapping0_inverse(vorbis_block *vb,vorbis_look_mapping *l){
324 vorbis_dsp_state *vd=vb->vd;
325 vorbis_info *vi=vd->vi;
326 codec_setup_info *ci=vi->codec_setup;
327 backend_lookup_state *b=vd->backend_state;
328 vorbis_look_mapping0 *look=(vorbis_look_mapping0 *)l;
329 vorbis_info_mapping0 *info=look->map;
330 vorbis_info_mode *mode=look->mode;
332 long n=vb->pcmend=ci->blocksizes[vb->W];
334 float *window=b->window[vb->W][vb->lW][vb->nW][mode->windowtype];
335 float **pcmbundle=alloca(sizeof(float *)*vi->channels);
336 int *nonzero=alloca(sizeof(int)*vi->channels);
338 /* time domain information decode (note that applying the
339 information would have to happen later; we'll probably add a
340 function entry to the harness for that later */
341 /* NOT IMPLEMENTED */
343 /* recover the spectral envelope; store it in the PCM vector for now */
344 for(i=0;i<vi->channels;i++){
345 float *pcm=vb->pcm[i];
346 int submap=info->chmuxlist[i];
347 nonzero[i]=look->floor_func[submap]->
348 inverse(vb,look->floor_look[submap],pcm);
349 _analysis_output("ifloor",seq+i,pcm,n/2,0,1);
352 /* recover the residue, apply directly to the spectral envelope */
354 for(i=0;i<info->submaps;i++){
356 for(j=0;j<vi->channels;j++){
357 if(info->chmuxlist[j]==i && nonzero[j])
358 pcmbundle[ch_in_bundle++]=vb->pcm[j];
361 look->residue_func[i]->inverse(vb,look->residue_look[i],pcmbundle,ch_in_bundle);
364 /* transform the PCM data; takes PCM vector, vb; modifies PCM vector */
365 /* only MDCT right now.... */
366 for(i=0;i<vi->channels;i++){
367 float *pcm=vb->pcm[i];
368 _analysis_output("out",seq+i,pcm,n/2,0,1);
369 mdct_backward(b->transform[vb->W][0],pcm,pcm);
372 /* now apply the decoded pre-window time information */
373 /* NOT IMPLEMENTED */
375 /* window the data */
376 for(i=0;i<vi->channels;i++){
377 float *pcm=vb->pcm[i];
384 _analysis_output("final",seq++,pcm,n,0,0);
387 /* now apply the decoded post-window time information */
388 /* NOT IMPLEMENTED */
395 vorbis_func_mapping mapping0_exportbundle={
396 &mapping0_pack,&mapping0_unpack,&mapping0_look,&mapping0_copy_info,
397 &mapping0_free_info,&mapping0_free_look,&mapping0_forward,&mapping0_inverse