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.39 2001/12/16 04:15:46 xiphmont Exp $
16 ********************************************************************/
23 #include "vorbis/codec.h"
24 #include "codec_internal.h"
30 /* simplistic, wasteful way of doing this (unique lookup for each
31 mode/submapping); there should be a central repository for
32 identical lookups. That will require minor work, so I'm putting it
35 Why a lookup for each backend in a given mode? Because the
36 blocksize is set by the mode, and low backend lookups may require
37 parameters from other areas of the mode/mapping */
39 extern int analysis_noisy;
43 vorbis_info_mode *mode;
44 vorbis_info_mapping0 *map;
46 vorbis_look_time **time_look;
47 vorbis_look_floor **floor_look;
49 vorbis_look_residue **residue_look;
50 vorbis_look_psy *psy_look[2];
52 vorbis_func_time **time_func;
53 vorbis_func_floor **floor_func;
54 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(*ret));
64 memcpy(ret,info,sizeof(*ret));
68 static void mapping0_free_info(vorbis_info_mapping *i){
69 vorbis_info_mapping0 *info=(vorbis_info_mapping0 *)i;
71 memset(info,0,sizeof(*info));
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(*l));
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(*look));
112 vorbis_info_mapping0 *info=look->map=(vorbis_info_mapping0 *)m;
115 look->time_look=_ogg_calloc(info->submaps,sizeof(*look->time_look));
116 look->floor_look=_ogg_calloc(info->submaps,sizeof(*look->floor_look));
118 look->residue_look=_ogg_calloc(info->submaps,sizeof(*look->residue_look));
120 look->time_func=_ogg_calloc(info->submaps,sizeof(*look->time_func));
121 look->floor_func=_ogg_calloc(info->submaps,sizeof(*look->floor_func));
122 look->residue_func=_ogg_calloc(info->submaps,sizeof(*look->residue_func));
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(*look->psy_look[0]));
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(*look->psy_look[1]));
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(*look->psy_look[0]));
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,
182 oggpack_buffer *opb){
184 vorbis_info_mapping0 *info=(vorbis_info_mapping0 *)vm;
186 /* another 'we meant to do it this way' hack... up to beta 4, we
187 packed 4 binary zeros here to signify one submapping in use. We
188 now redefine that to mean four bitflags that indicate use of
189 deeper features; bit0:submappings, bit1:coupling,
190 bit2,3:reserved. This is backward compatable with all actual uses
194 oggpack_write(opb,1,1);
195 oggpack_write(opb,info->submaps-1,4);
197 oggpack_write(opb,0,1);
199 if(info->coupling_steps>0){
200 oggpack_write(opb,1,1);
201 oggpack_write(opb,info->coupling_steps-1,8);
203 for(i=0;i<info->coupling_steps;i++){
204 oggpack_write(opb,info->coupling_mag[i],ilog2(vi->channels));
205 oggpack_write(opb,info->coupling_ang[i],ilog2(vi->channels));
208 oggpack_write(opb,0,1);
210 oggpack_write(opb,0,2); /* 2,3:reserved */
212 /* we don't write the channel submappings if we only have one... */
214 for(i=0;i<vi->channels;i++)
215 oggpack_write(opb,info->chmuxlist[i],4);
217 for(i=0;i<info->submaps;i++){
218 oggpack_write(opb,info->timesubmap[i],8);
219 oggpack_write(opb,info->floorsubmap[i],8);
220 oggpack_write(opb,info->residuesubmap[i],8);
224 /* also responsible for range checking */
225 static vorbis_info_mapping *mapping0_unpack(vorbis_info *vi,oggpack_buffer *opb){
227 vorbis_info_mapping0 *info=_ogg_calloc(1,sizeof(*info));
228 codec_setup_info *ci=vi->codec_setup;
229 memset(info,0,sizeof(*info));
231 if(oggpack_read(opb,1))
232 info->submaps=oggpack_read(opb,4)+1;
236 if(oggpack_read(opb,1)){
237 info->coupling_steps=oggpack_read(opb,8)+1;
239 for(i=0;i<info->coupling_steps;i++){
240 int testM=info->coupling_mag[i]=oggpack_read(opb,ilog2(vi->channels));
241 int testA=info->coupling_ang[i]=oggpack_read(opb,ilog2(vi->channels));
246 testM>=vi->channels ||
247 testA>=vi->channels) goto err_out;
252 if(oggpack_read(opb,2)>0)goto err_out; /* 2,3:reserved */
255 for(i=0;i<vi->channels;i++){
256 info->chmuxlist[i]=oggpack_read(opb,4);
257 if(info->chmuxlist[i]>=info->submaps)goto err_out;
260 for(i=0;i<info->submaps;i++){
261 info->timesubmap[i]=oggpack_read(opb,8);
262 if(info->timesubmap[i]>=ci->times)goto err_out;
263 info->floorsubmap[i]=oggpack_read(opb,8);
264 if(info->floorsubmap[i]>=ci->floors)goto err_out;
265 info->residuesubmap[i]=oggpack_read(opb,8);
266 if(info->residuesubmap[i]>=ci->residues)goto err_out;
272 mapping0_free_info(info);
279 #include "envelope.h"
284 /* no time mapping implementation for now */
286 static int mapping0_forward(vorbis_block *vb,vorbis_look_mapping *l){
287 vorbis_dsp_state *vd=vb->vd;
288 vorbis_info *vi=vd->vi;
289 codec_setup_info *ci=vi->codec_setup;
290 backend_lookup_state *b=vb->vd->backend_state;
291 bitrate_manager_state *bm=&b->bms;
292 vorbis_look_mapping0 *look=(vorbis_look_mapping0 *)l;
293 vorbis_info_mapping0 *info=look->map;
294 vorbis_info_mode *mode=look->mode;
295 vorbis_block_internal *vbi=(vorbis_block_internal *)vb->internal;
298 float *window=b->window[vb->W][vb->lW][vb->nW][mode->windowtype];
299 int *nonzero=alloca(sizeof(*nonzero)*vi->channels);
301 float *work=_vorbis_block_alloc(vb,n*sizeof(*work));
303 float global_ampmax=vbi->ampmax;
304 float *local_ampmax=alloca(sizeof(*local_ampmax)*vi->channels);
305 int blocktype=vbi->blocktype;
307 /* we differentiate between short and long block types to help the
308 masking engine; the window shapes also matter.
309 impulse block (a short block in which an impulse occurs)
310 padding block (a short block that pads between a transitional
311 long block and an impulse block, or vice versa)
312 transition block (the wqeird one; a long block with the transition
313 window; affects bass/midrange response and that must be
314 accounted for in masking)
315 long block (run of the mill long block)
318 for(i=0;i<vi->channels;i++){
321 /* the following makes things clearer to *me* anyway */
322 float *pcm =vb->pcm[i];
324 float *logfft =pcm+n/2;
328 float *codedflr=pcm+n/2;
330 float *logmask =work+n/2;*/
332 _analysis_output("pcm",seq+i,pcm,n,0,0);
334 /* window the PCM data */
336 fft[j]=pcm[j]*=window[j];
338 //_analysis_output("windowed",seq+i,pcm,n,0,0);
340 /* transform the PCM data */
341 /* only MDCT right now.... */
342 mdct_forward(b->transform[vb->W][0],pcm,pcm);
344 /* FFT yields more accurate tonal estimation (not phase sensitive) */
345 drft_forward(&look->fft_look,fft);
348 local_ampmax[i]=logfft[0];
350 float temp=scale*FAST_HYPOT(fft[j],fft[j+1]);
351 temp=logfft[(j+1)>>1]=todB(&temp);
352 if(temp>local_ampmax[i])local_ampmax[i]=temp;
355 if(local_ampmax[i]>0.f)local_ampmax[i]=0.f;
356 if(local_ampmax[i]>global_ampmax)global_ampmax=local_ampmax[i];
358 _analysis_output("fft",seq+i,logfft,n/2,1,0);
361 for(i=0;i<vi->channels;i++){
362 int submap=info->chmuxlist[i];
364 /* the following makes things clearer to *me* anyway */
365 float *mdct =vb->pcm[i];
367 float *codedflr=mdct+n/2;
368 float *logfft =mdct+n/2;
370 float *logmdct =work;
371 float *logmax =mdct+n/2;
372 float *logmask =work+n/2;
375 logmdct[j]=todB(mdct+j);
376 _analysis_output("mdct",seq+i,logmdct,n/2,1,0);
379 /* perform psychoacoustics; do masking */
380 _vp_compute_mask(look->psy_look[blocktype],
383 logfft, /* -> logmax */
388 ci->blocksizes[vb->lW]/2,
391 _analysis_output("mask",seq+i,logmask,n/2,1,0);
392 /* perform floor encoding */
393 nonzero[i]=look->floor_func[submap]->
394 forward(vb,look->floor_look[submap],
403 _vp_remove_floor(look->psy_look[blocktype],
412 if(fabs(res[j])>1200){
414 fprintf(stderr,"%ld ",seq+i);
417 //_analysis_output("res",seq+i,res,n/2,1,0);
418 _analysis_output("codedflr",seq+i,codedflr,n/2,1,1);
422 vbi->ampmax=global_ampmax;
424 /* partition based prequantization and channel coupling */
425 /* Steps in prequant and coupling:
427 classify by |mag| across all pcm vectors
429 down-couple/down-quantize from perfect residue -> quantized vector
432 encode quantized vector; add encoded values to 'so-far' vector
433 more? [not yet at bitrate/not yet at target]
435 down-couple/down-quantize from perfect-'so-far' ->
436 quantized vector; when subtracting coupling,
437 account for +/- out-of-phase component
444 quantization in each iteration is done (after circular normalization
445 in coupling) using a by-iteration quantization granule value.
450 float **quantized=alloca(sizeof(*quantized)*vi->channels);
451 float **sofar=alloca(sizeof(*sofar)*vi->channels);
453 long ***classifications=alloca(sizeof(*classifications)*info->submaps);
454 float ***qbundle=alloca(sizeof(*qbundle)*info->submaps);
455 float ***pcmbundle=alloca(sizeof(*pcmbundle)*info->submaps);
456 float ***sobundle=alloca(sizeof(*sobundle)*info->submaps);
457 int **zerobundle=alloca(sizeof(*zerobundle)*info->submaps);
458 int *chbundle=alloca(sizeof(*chbundle)*info->submaps);
461 /* play a little loose with this abstraction */
462 int quant_passes=ci->coupling_passes;
464 for(i=0;i<vi->channels;i++){
465 quantized[i]=_vorbis_block_alloc(vb,n*sizeof(*sofar[i]));
466 sofar[i]=quantized[i]+n/2;
467 memset(sofar[i],0,sizeof(*sofar[i])*n/2);
470 qbundle[0]=alloca(sizeof(*qbundle[0])*vi->channels);
471 pcmbundle[0]=alloca(sizeof(*pcmbundle[0])*vi->channels);
472 sobundle[0]=alloca(sizeof(*sobundle[0])*vi->channels);
473 zerobundle[0]=alloca(sizeof(*zerobundle[0])*vi->channels);
475 /* initial down-quantized coupling */
477 if(info->coupling_steps==0){
478 /* this assumes all or nothing coupling right now. it should pass
479 through any channels left uncoupled, but it doesn't do that now */
480 for(i=0;i<vi->channels;i++){
482 float *lqua=quantized[i];
487 _vp_quantize_couple(look->psy_look[blocktype],
496 //for(i=0;i<vi->channels;i++)
497 //_analysis_output("quant",seq+i,quantized[i],n/2,1,0);
500 /* classify, by submap */
502 for(i=0;i<info->submaps;i++){
504 qbundle[i]=qbundle[0]+chcounter;
505 sobundle[i]=sobundle[0]+chcounter;
506 zerobundle[i]=zerobundle[0]+chcounter;
508 for(j=0;j<vi->channels;j++){
509 if(info->chmuxlist[j]==i){
511 zerobundle[i][ch_in_bundle]=1;
513 zerobundle[i][ch_in_bundle]=0;
514 qbundle[i][ch_in_bundle]=quantized[j];
515 pcmbundle[i][ch_in_bundle]=pcm[j];
516 sobundle[i][ch_in_bundle++]=sofar[j];
519 chbundle[i]=ch_in_bundle;
520 chcounter+=ch_in_bundle;
522 classifications[i]=look->residue_func[i]->
523 class(vb,look->residue_look[i],pcmbundle[i],zerobundle[i],chbundle[i]);
526 /* actual encoding loop; we pack all the iterations to collect
529 for(i=0;i<quant_passes;){
531 /* perform residue encoding of this pass's quantized residue
532 vector, according residue mapping */
534 for(j=0;j<info->submaps;j++){
535 look->residue_func[j]->
536 forward(vb,look->residue_look[j],
537 qbundle[j],sobundle[j],zerobundle[j],chbundle[j],
538 i,classifications[j],vbi->packet_markers);
544 /* down-couple/down-quantize from perfect-'so-far' ->
545 new quantized vector */
546 if(info->coupling_steps==0){
547 /* this assumes all or nothing coupling right now. it should pass
548 through any channels left uncoupled, but it doesn't do that now */
550 for(k=0;k<vi->channels;k++){
552 float *lsof=sofar[k];
553 float *lqua=quantized[k];
555 lqua[j]=lpcm[j]-lsof[j];
560 _vp_quantize_couple(look->psy_look[blocktype],
568 //sprintf(buf,"quant%d",i);
569 //for(j=0;j<vi->channels;j++)
570 //_analysis_output(buf,seq+j,quantized[j],n/2,1,0);
578 look->lastframe=vb->sequence;
582 static int mapping0_inverse(vorbis_block *vb,vorbis_look_mapping *l){
583 vorbis_dsp_state *vd=vb->vd;
584 vorbis_info *vi=vd->vi;
585 codec_setup_info *ci=vi->codec_setup;
586 backend_lookup_state *b=vd->backend_state;
587 vorbis_look_mapping0 *look=(vorbis_look_mapping0 *)l;
588 vorbis_info_mapping0 *info=look->map;
589 vorbis_info_mode *mode=look->mode;
591 long n=vb->pcmend=ci->blocksizes[vb->W];
593 float *window=b->window[vb->W][vb->lW][vb->nW][mode->windowtype];
594 float **pcmbundle=alloca(sizeof(*pcmbundle)*vi->channels);
595 int *zerobundle=alloca(sizeof(*zerobundle)*vi->channels);
597 int *nonzero =alloca(sizeof(*nonzero)*vi->channels);
598 void **floormemo=alloca(sizeof(*floormemo)*vi->channels);
600 /* time domain information decode (note that applying the
601 information would have to happen later; we'll probably add a
602 function entry to the harness for that later */
603 /* NOT IMPLEMENTED */
605 /* recover the spectral envelope; store it in the PCM vector for now */
606 for(i=0;i<vi->channels;i++){
607 int submap=info->chmuxlist[i];
608 floormemo[i]=look->floor_func[submap]->
609 inverse1(vb,look->floor_look[submap]);
614 memset(vb->pcm[i],0,sizeof(*vb->pcm[i])*n/2);
617 /* channel coupling can 'dirty' the nonzero listing */
618 for(i=0;i<info->coupling_steps;i++){
619 if(nonzero[info->coupling_mag[i]] ||
620 nonzero[info->coupling_ang[i]]){
621 nonzero[info->coupling_mag[i]]=1;
622 nonzero[info->coupling_ang[i]]=1;
626 /* recover the residue into our working vectors */
627 for(i=0;i<info->submaps;i++){
629 for(j=0;j<vi->channels;j++){
630 if(info->chmuxlist[j]==i){
632 zerobundle[ch_in_bundle]=1;
634 zerobundle[ch_in_bundle]=0;
635 pcmbundle[ch_in_bundle++]=vb->pcm[j];
639 look->residue_func[i]->inverse(vb,look->residue_look[i],
640 pcmbundle,zerobundle,ch_in_bundle);
643 /* channel coupling */
644 for(i=info->coupling_steps-1;i>=0;i--){
645 float *pcmM=vb->pcm[info->coupling_mag[i]];
646 float *pcmA=vb->pcm[info->coupling_ang[i]];
671 /* compute and apply spectral envelope */
672 for(i=0;i<vi->channels;i++){
673 float *pcm=vb->pcm[i];
674 int submap=info->chmuxlist[i];
675 look->floor_func[submap]->
676 inverse2(vb,look->floor_look[submap],floormemo[i],pcm);
679 /* transform the PCM data; takes PCM vector, vb; modifies PCM vector */
680 /* only MDCT right now.... */
681 for(i=0;i<vi->channels;i++){
682 float *pcm=vb->pcm[i];
683 //_analysis_output("out",seq+i,pcm,n/2,1,1);
684 //_analysis_output("lout",seq+i,pcm,n/2,0,0);
685 mdct_backward(b->transform[vb->W][0],pcm,pcm);
688 /* window the data */
689 for(i=0;i<vi->channels;i++){
690 float *pcm=vb->pcm[i];
698 //_analysis_output("final",seq,pcm,n,0,0);
701 /* now apply the decoded post-window time information */
702 /* NOT IMPLEMENTED */
704 fprintf(stderr,"seq %d\r",seq);
711 vorbis_func_mapping mapping0_exportbundle={