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.36 2001/09/11 05:06:57 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 */
40 extern int analysis_noisy;
44 vorbis_info_mode *mode;
45 vorbis_info_mapping0 *map;
47 vorbis_look_time **time_look;
48 vorbis_look_floor **floor_look;
50 vorbis_look_residue **residue_look;
51 vorbis_look_psy *psy_look[2];
53 vorbis_func_time **time_func;
54 vorbis_func_floor **floor_func;
55 vorbis_func_residue **residue_func;
58 long lastframe; /* if a different mode is called, we need to
60 } vorbis_look_mapping0;
62 static vorbis_info_mapping *mapping0_copy_info(vorbis_info_mapping *vm){
63 vorbis_info_mapping0 *info=(vorbis_info_mapping0 *)vm;
64 vorbis_info_mapping0 *ret=_ogg_malloc(sizeof(vorbis_info_mapping0));
65 memcpy(ret,info,sizeof(vorbis_info_mapping0));
69 static void mapping0_free_info(vorbis_info_mapping *i){
71 memset(i,0,sizeof(vorbis_info_mapping0));
76 static void mapping0_free_look(vorbis_look_mapping *look){
78 vorbis_look_mapping0 *l=(vorbis_look_mapping0 *)look;
80 drft_clear(&l->fft_look);
82 for(i=0;i<l->map->submaps;i++){
83 l->time_func[i]->free_look(l->time_look[i]);
84 l->floor_func[i]->free_look(l->floor_look[i]);
85 l->residue_func[i]->free_look(l->residue_look[i]);
87 if(l->psy_look[1] && l->psy_look[1]!=l->psy_look[0]){
88 _vp_psy_clear(l->psy_look[1]);
89 _ogg_free(l->psy_look[1]);
92 _vp_psy_clear(l->psy_look[0]);
93 _ogg_free(l->psy_look[0]);
95 _ogg_free(l->time_func);
96 _ogg_free(l->floor_func);
97 _ogg_free(l->residue_func);
98 _ogg_free(l->time_look);
99 _ogg_free(l->floor_look);
100 _ogg_free(l->residue_look);
101 memset(l,0,sizeof(vorbis_look_mapping0));
106 static vorbis_look_mapping *mapping0_look(vorbis_dsp_state *vd,vorbis_info_mode *vm,
107 vorbis_info_mapping *m){
109 vorbis_info *vi=vd->vi;
110 codec_setup_info *ci=vi->codec_setup;
111 vorbis_look_mapping0 *look=_ogg_calloc(1,sizeof(vorbis_look_mapping0));
112 vorbis_info_mapping0 *info=look->map=(vorbis_info_mapping0 *)m;
115 look->time_look=_ogg_calloc(info->submaps,sizeof(vorbis_look_time *));
116 look->floor_look=_ogg_calloc(info->submaps,sizeof(vorbis_look_floor *));
118 look->residue_look=_ogg_calloc(info->submaps,sizeof(vorbis_look_residue *));
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]);
140 if(ci->psys && vd->analysisp){
141 if(info->psy[0] != info->psy[1]){
143 int psynum=info->psy[0];
144 look->psy_look[0]=_ogg_calloc(1,sizeof(vorbis_look_psy));
145 _vp_psy_init(look->psy_look[0],ci->psy_param[psynum],
147 ci->blocksizes[vm->blockflag]/2,vi->rate);
150 look->psy_look[1]=_ogg_calloc(1,sizeof(vorbis_look_psy));
151 _vp_psy_init(look->psy_look[1],ci->psy_param[psynum],
153 ci->blocksizes[vm->blockflag]/2,vi->rate);
156 int psynum=info->psy[0];
157 look->psy_look[0]=_ogg_calloc(1,sizeof(vorbis_look_psy));
158 look->psy_look[1]=look->psy_look[0];
159 _vp_psy_init(look->psy_look[0],ci->psy_param[psynum],
161 ci->blocksizes[vm->blockflag]/2,vi->rate);
166 look->ch=vi->channels;
168 if(vd->analysisp)drft_init(&look->fft_look,ci->blocksizes[vm->blockflag]);
172 static int ilog2(unsigned int v){
181 static void mapping0_pack(vorbis_info *vi,vorbis_info_mapping *vm,oggpack_buffer *opb){
183 vorbis_info_mapping0 *info=(vorbis_info_mapping0 *)vm;
185 /* another 'we meant to do it this way' hack... up to beta 4, we
186 packed 4 binary zeros here to signify one submapping in use. We
187 now redefine that to mean four bitflags that indicate use of
188 deeper features; bit0:submappings, bit1:coupling,
189 bit2,3:reserved. This is backward compatable with all actual uses
193 oggpack_write(opb,1,1);
194 oggpack_write(opb,info->submaps-1,4);
196 oggpack_write(opb,0,1);
198 if(info->coupling_steps>0){
199 oggpack_write(opb,1,1);
200 oggpack_write(opb,info->coupling_steps-1,8);
202 for(i=0;i<info->coupling_steps;i++){
203 oggpack_write(opb,info->coupling_mag[i],ilog2(vi->channels));
204 oggpack_write(opb,info->coupling_ang[i],ilog2(vi->channels));
207 oggpack_write(opb,0,1);
209 oggpack_write(opb,0,2); /* 2,3:reserved */
211 /* we don't write the channel submappings if we only have one... */
213 for(i=0;i<vi->channels;i++)
214 oggpack_write(opb,info->chmuxlist[i],4);
216 for(i=0;i<info->submaps;i++){
217 oggpack_write(opb,info->timesubmap[i],8);
218 oggpack_write(opb,info->floorsubmap[i],8);
219 oggpack_write(opb,info->residuesubmap[i],8);
223 /* also responsible for range checking */
224 static vorbis_info_mapping *mapping0_unpack(vorbis_info *vi,oggpack_buffer *opb){
226 vorbis_info_mapping0 *info=_ogg_calloc(1,sizeof(vorbis_info_mapping0));
227 codec_setup_info *ci=vi->codec_setup;
228 memset(info,0,sizeof(vorbis_info_mapping0));
230 if(oggpack_read(opb,1))
231 info->submaps=oggpack_read(opb,4)+1;
235 if(oggpack_read(opb,1)){
236 info->coupling_steps=oggpack_read(opb,8)+1;
238 for(i=0;i<info->coupling_steps;i++){
239 int testM=info->coupling_mag[i]=oggpack_read(opb,ilog2(vi->channels));
240 int testA=info->coupling_ang[i]=oggpack_read(opb,ilog2(vi->channels));
245 testM>=vi->channels ||
246 testA>=vi->channels) goto err_out;
251 if(oggpack_read(opb,2)>0)goto err_out; /* 2,3:reserved */
254 for(i=0;i<vi->channels;i++){
255 info->chmuxlist[i]=oggpack_read(opb,4);
256 if(info->chmuxlist[i]>=info->submaps)goto err_out;
259 for(i=0;i<info->submaps;i++){
260 info->timesubmap[i]=oggpack_read(opb,8);
261 if(info->timesubmap[i]>=ci->times)goto err_out;
262 info->floorsubmap[i]=oggpack_read(opb,8);
263 if(info->floorsubmap[i]>=ci->floors)goto err_out;
264 info->residuesubmap[i]=oggpack_read(opb,8);
265 if(info->residuesubmap[i]>=ci->residues)goto err_out;
271 mapping0_free_info(info);
278 #include "envelope.h"
283 /* no time mapping implementation for now */
285 static int mapping0_forward(vorbis_block *vb,vorbis_look_mapping *l){
286 vorbis_dsp_state *vd=vb->vd;
287 vorbis_info *vi=vd->vi;
288 codec_setup_info *ci=vi->codec_setup;
289 backend_lookup_state *b=vb->vd->backend_state;
290 vorbis_look_mapping0 *look=(vorbis_look_mapping0 *)l;
291 vorbis_info_mapping0 *info=look->map;
292 vorbis_info_mode *mode=look->mode;
293 vorbis_block_internal *vbi=(vorbis_block_internal *)vb->internal;
296 float *window=b->window[vb->W][vb->lW][vb->nW][mode->windowtype];
297 int *nonzero=alloca(sizeof(int)*vi->channels);
299 float *work=_vorbis_block_alloc(vb,n*sizeof(float));
301 float global_ampmax=vbi->ampmax;
302 float *local_ampmax=alloca(sizeof(float)*vi->channels);
305 /* we differentiate between short and long block types to help the
306 masking engine; the window shapes also matter.
307 impulse block (a short block in which an impulse occurs)
308 padding block (a short block that pads between a transitional
309 long block and an impulse block, or vice versa)
310 transition block (the wqeird one; a long block with the transition
311 window; affects bass/midrange response and that must be
312 accounted for in masking)
313 long block (run of the mill long block)
317 if(!vb->lW || !vb->nW)
318 blocktype=BLOCKTYPE_TRANSITION;
320 blocktype=BLOCKTYPE_LONG;
322 /* right now we're missing the infrastructure to distingush the
324 blocktype=BLOCKTYPE_IMPULSE;
327 for(i=0;i<vi->channels;i++){
330 /* the following makes things clearer to *me* anyway */
331 float *pcm =vb->pcm[i];
333 float *logfft =pcm+n/2;
337 float *codedflr=pcm+n/2;
339 float *logmask =work+n/2;*/
341 /* window the PCM data */
343 fft[j]=pcm[j]*=window[j];
345 /* transform the PCM data */
346 /* only MDCT right now.... */
347 mdct_forward(b->transform[vb->W][0],pcm,pcm);
349 /* FFT yields more accurate tonal estimation (not phase sensitive) */
350 drft_forward(&look->fft_look,fft);
353 local_ampmax[i]=logfft[0];
355 float temp=scale*FAST_HYPOT(fft[j],fft[j+1]);
356 temp=logfft[(j+1)>>1]=todB(&temp);
357 if(temp>local_ampmax[i])local_ampmax[i]=temp;
359 if(local_ampmax[i]>global_ampmax)global_ampmax=local_ampmax[i];
361 _analysis_output("fft",seq+i,logfft,n/2,1,0);
364 for(i=0;i<vi->channels;i++){
365 int submap=info->chmuxlist[i];
367 /* the following makes things clearer to *me* anyway */
368 float *mdct =vb->pcm[i];
370 float *codedflr=mdct+n/2;
371 float *logfft =mdct+n/2;
373 float *logmdct =work;
374 float *logmax =mdct+n/2;
375 float *logmask =work+n/2;
378 logmdct[j]=todB(mdct+j);
379 _analysis_output("mdct",seq+i,logmdct,n/2,1,0);
380 _analysis_output("lmdct",seq+i,mdct,n/2,0,0);
383 /* perform psychoacoustics; do masking */
384 _vp_compute_mask(look->psy_look[blocktype],
387 logfft, /* -> logmax */
392 ci->blocksizes[vb->lW]/2);
394 _analysis_output("mask",seq+i,logmask,n/2,1,0);
395 /* perform floor encoding */
396 nonzero[i]=look->floor_func[submap]->
397 forward(vb,look->floor_look[submap],
406 _analysis_output("mdct2",seq+i,mdct,n/2,1,1);
407 _vp_remove_floor(look->psy_look[blocktype],
416 if(fabs(res[j])>1200){
418 fprintf(stderr,"%ld ",seq+i);
421 _analysis_output("res",seq+i,res,n/2,1,0);
422 _analysis_output("codedflr",seq+i,codedflr,n/2,1,1);
426 vbi->ampmax=global_ampmax;
428 /* partition based prequantization and channel coupling */
429 /* Steps in prequant and coupling:
431 down-couple/down-quantize from perfect residue -> quantized vector
432 classify by this first quantized vector
435 encode quantized vector; add encoded values to 'so-far' vector
436 more? [not yet at bitrate/not yet at target]
438 down-couple/down-quantize from perfect-'so-far' ->
439 quantized vector; when subtracting coupling,
440 account for +/- out-of-phase component
447 quantization in each iteration is done (after circular normalization
448 in coupling) using a by-iteration quantization granule value.
453 float **quantized=alloca(sizeof(float*)*vi->channels);
454 float **sofar=alloca(sizeof(float*)*vi->channels);
456 long ***classifications=alloca(sizeof(long**)*info->submaps);
457 float ***pcmbundle=alloca(sizeof(float **)*info->submaps);
458 float ***sobundle=alloca(sizeof(float **)*info->submaps);
459 int **zerobundle=alloca(sizeof(int *)*info->submaps);
460 int *chbundle=alloca(sizeof(int)*info->submaps);
463 /* play a little loose with this abstraction */
464 int quant_passes=look->psy_look[blocktype]->vi->coupling_passes;
467 for(i=0;i<vi->channels;i++){
468 quantized[i]=pcm[i]+n/2;
469 sofar[i]=_vorbis_block_alloc(vb,n/2*sizeof(float));
470 memset(sofar[i],0,sizeof(float)*n/2);
473 pcmbundle[0]=alloca(sizeof(float *)*vi->channels);
474 sobundle[0]=alloca(sizeof(float *)*vi->channels);
475 zerobundle[0]=alloca(sizeof(int)*vi->channels);
477 /* initial down-quantized coupling */
479 if(info->coupling_steps==0){
480 /* this assumes all or nothing coupling right now. it should pass
481 through any channels left uncoupled, but it doesn't do that now */
482 for(i=0;i<vi->channels;i++){
484 float *lqua=quantized[i];
489 _vp_quantize_couple(look->psy_look[blocktype],
498 for(i=0;i<vi->channels;i++)
499 _analysis_output("quant",seq+i,quantized[i],n/2,1,0);
502 /* classify, by submap */
504 for(i=0;i<info->submaps;i++){
506 pcmbundle[i]=pcmbundle[0]+chcounter;
507 sobundle[i]=sobundle[0]+chcounter;
508 zerobundle[i]=zerobundle[0]+chcounter;
510 for(j=0;j<vi->channels;j++){
511 if(info->chmuxlist[j]==i){
513 zerobundle[i][ch_in_bundle]=1;
515 zerobundle[i][ch_in_bundle]=0;
516 pcmbundle[i][ch_in_bundle]=quantized[j];
517 sobundle[i][ch_in_bundle++]=sofar[j];
520 chbundle[i]=ch_in_bundle;
521 chcounter+=ch_in_bundle;
523 classifications[i]=look->residue_func[i]->
524 class(vb,look->residue_look[i],pcmbundle[i],zerobundle[i],chbundle[i]);
527 /* actual encoding loop */
530 /* perform residue encoding of this pass's quantized residue
531 vector, according residue mapping */
533 for(j=0;j<info->submaps;j++)
534 look->residue_func[j]->
535 forward(vb,look->residue_look[j],
536 pcmbundle[j],sobundle[j],zerobundle[j],chbundle[j],
537 i,classifications[j]);
540 /* bitrate management decision hook; the following if() is where
541 we tell progressive encoding to halt, right now it just
542 avoids falling off the edge */
543 if(i>=quant_passes /* || yadda yadda */)stopflag=1;
546 /* down-couple/down-quantize from perfect-'so-far' ->
547 new quantized vector */
548 if(info->coupling_steps==0){
549 /* this assumes all or nothing coupling right now. it should pass
550 through any channels left uncoupled, but it doesn't do that now */
551 for(i=0;i<vi->channels;i++){
553 float *lsof=sofar[i];
554 float *lqua=quantized[i];
556 lqua[j]=lpcm[j]-lsof[j];
559 _vp_quantize_couple(look->psy_look[blocktype],
568 /* steady as she goes */
573 look->lastframe=vb->sequence;
577 static int mapping0_inverse(vorbis_block *vb,vorbis_look_mapping *l){
578 vorbis_dsp_state *vd=vb->vd;
579 vorbis_info *vi=vd->vi;
580 codec_setup_info *ci=vi->codec_setup;
581 backend_lookup_state *b=vd->backend_state;
582 vorbis_look_mapping0 *look=(vorbis_look_mapping0 *)l;
583 vorbis_info_mapping0 *info=look->map;
584 vorbis_info_mode *mode=look->mode;
586 long n=vb->pcmend=ci->blocksizes[vb->W];
588 float *window=b->window[vb->W][vb->lW][vb->nW][mode->windowtype];
589 float **pcmbundle=alloca(sizeof(float *)*vi->channels);
590 int *zerobundle=alloca(sizeof(int)*vi->channels);
592 int *nonzero =alloca(sizeof(int)*vi->channels);
593 void **floormemo=alloca(sizeof(void *)*vi->channels);
595 /* time domain information decode (note that applying the
596 information would have to happen later; we'll probably add a
597 function entry to the harness for that later */
598 /* NOT IMPLEMENTED */
600 /* recover the spectral envelope; store it in the PCM vector for now */
601 for(i=0;i<vi->channels;i++){
602 int submap=info->chmuxlist[i];
603 floormemo[i]=look->floor_func[submap]->
604 inverse1(vb,look->floor_look[submap]);
609 memset(vb->pcm[i],0,sizeof(float)*n/2);
612 /* channel coupling can 'dirty' the nonzero listing */
613 for(i=0;i<info->coupling_steps;i++){
614 if(nonzero[info->coupling_mag[i]] ||
615 nonzero[info->coupling_ang[i]]){
616 nonzero[info->coupling_mag[i]]=1;
617 nonzero[info->coupling_ang[i]]=1;
621 /* recover the residue into our working vectors */
622 for(i=0;i<info->submaps;i++){
624 for(j=0;j<vi->channels;j++){
625 if(info->chmuxlist[j]==i){
627 zerobundle[ch_in_bundle]=1;
629 zerobundle[ch_in_bundle]=0;
630 pcmbundle[ch_in_bundle++]=vb->pcm[j];
634 look->residue_func[i]->inverse(vb,look->residue_look[i],
635 pcmbundle,zerobundle,ch_in_bundle);
638 /* channel coupling */
639 for(i=info->coupling_steps-1;i>=0;i--){
640 float *pcmM=vb->pcm[info->coupling_mag[i]];
641 float *pcmA=vb->pcm[info->coupling_ang[i]];
666 /* compute and apply spectral envelope */
667 for(i=0;i<vi->channels;i++){
668 float *pcm=vb->pcm[i];
669 int submap=info->chmuxlist[i];
670 look->floor_func[submap]->
671 inverse2(vb,look->floor_look[submap],floormemo[i],pcm);
674 /* transform the PCM data; takes PCM vector, vb; modifies PCM vector */
675 /* only MDCT right now.... */
676 for(i=0;i<vi->channels;i++){
677 float *pcm=vb->pcm[i];
678 _analysis_output("out",seq+i,pcm,n/2,1,1);
679 _analysis_output("lout",seq+i,pcm,n/2,0,0);
680 mdct_backward(b->transform[vb->W][0],pcm,pcm);
683 /* window the data */
684 for(i=0;i<vi->channels;i++){
685 float *pcm=vb->pcm[i];
692 _analysis_output("final",seq++,pcm,n,0,0);
695 /* now apply the decoded post-window time information */
696 /* NOT IMPLEMENTED */
703 vorbis_func_mapping mapping0_exportbundle={