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-2002 *
9 * by the XIPHOPHORUS Company http://www.xiph.org/ *
11 ********************************************************************
13 function: channel mapping 0 implementation
14 last mod: $Id: mapping0.c,v 1.49 2002/04/06 03:07:25 xiphmont Exp $
16 ********************************************************************/
23 #include "vorbis/codec.h"
24 #include "codec_internal.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(*ret));
65 memcpy(ret,info,sizeof(*ret));
69 static void mapping0_free_info(vorbis_info_mapping *i){
70 vorbis_info_mapping0 *info=(vorbis_info_mapping0 *)i;
72 memset(info,0,sizeof(*info));
77 static void mapping0_free_look(vorbis_look_mapping *look){
79 vorbis_look_mapping0 *l=(vorbis_look_mapping0 *)look;
81 drft_clear(&l->fft_look);
83 for(i=0;i<l->map->submaps;i++){
84 l->time_func[i]->free_look(l->time_look[i]);
85 l->floor_func[i]->free_look(l->floor_look[i]);
86 l->residue_func[i]->free_look(l->residue_look[i]);
88 if(l->psy_look[1] && l->psy_look[1]!=l->psy_look[0]){
89 _vp_psy_clear(l->psy_look[1]);
90 _ogg_free(l->psy_look[1]);
93 _vp_psy_clear(l->psy_look[0]);
94 _ogg_free(l->psy_look[0]);
96 _ogg_free(l->time_func);
97 _ogg_free(l->floor_func);
98 _ogg_free(l->residue_func);
99 _ogg_free(l->time_look);
100 _ogg_free(l->floor_look);
101 _ogg_free(l->residue_look);
102 memset(l,0,sizeof(*l));
107 static vorbis_look_mapping *mapping0_look(vorbis_dsp_state *vd,vorbis_info_mode *vm,
108 vorbis_info_mapping *m){
110 vorbis_info *vi=vd->vi;
111 codec_setup_info *ci=vi->codec_setup;
112 vorbis_look_mapping0 *look=_ogg_calloc(1,sizeof(*look));
113 vorbis_info_mapping0 *info=look->map=(vorbis_info_mapping0 *)m;
116 look->time_look=_ogg_calloc(info->submaps,sizeof(*look->time_look));
117 look->floor_look=_ogg_calloc(info->submaps,sizeof(*look->floor_look));
119 look->residue_look=_ogg_calloc(info->submaps,sizeof(*look->residue_look));
121 look->time_func=_ogg_calloc(info->submaps,sizeof(*look->time_func));
122 look->floor_func=_ogg_calloc(info->submaps,sizeof(*look->floor_func));
123 look->residue_func=_ogg_calloc(info->submaps,sizeof(*look->residue_func));
125 for(i=0;i<info->submaps;i++){
126 int timenum=info->timesubmap[i];
127 int floornum=info->floorsubmap[i];
128 int resnum=info->residuesubmap[i];
130 look->time_func[i]=_time_P[ci->time_type[timenum]];
131 look->time_look[i]=look->time_func[i]->
132 look(vd,vm,ci->time_param[timenum]);
133 look->floor_func[i]=_floor_P[ci->floor_type[floornum]];
134 look->floor_look[i]=look->floor_func[i]->
135 look(vd,vm,ci->floor_param[floornum]);
136 look->residue_func[i]=_residue_P[ci->residue_type[resnum]];
137 look->residue_look[i]=look->residue_func[i]->
138 look(vd,vm,ci->residue_param[resnum]);
141 if(ci->psys && vd->analysisp){
142 if(info->psy[0] != info->psy[1]){
144 int psynum=info->psy[0];
145 look->psy_look[0]=_ogg_calloc(1,sizeof(*look->psy_look[0]));
146 _vp_psy_init(look->psy_look[0],ci->psy_param[psynum],
148 ci->blocksizes[vm->blockflag]/2,vi->rate);
151 look->psy_look[1]=_ogg_calloc(1,sizeof(*look->psy_look[1]));
152 _vp_psy_init(look->psy_look[1],ci->psy_param[psynum],
154 ci->blocksizes[vm->blockflag]/2,vi->rate);
157 int psynum=info->psy[0];
158 look->psy_look[0]=_ogg_calloc(1,sizeof(*look->psy_look[0]));
159 look->psy_look[1]=look->psy_look[0];
160 _vp_psy_init(look->psy_look[0],ci->psy_param[psynum],
162 ci->blocksizes[vm->blockflag]/2,vi->rate);
167 look->ch=vi->channels;
169 if(vd->analysisp)drft_init(&look->fft_look,ci->blocksizes[vm->blockflag]);
173 static int ilog2(unsigned int v){
182 static void mapping0_pack(vorbis_info *vi,vorbis_info_mapping *vm,
183 oggpack_buffer *opb){
185 vorbis_info_mapping0 *info=(vorbis_info_mapping0 *)vm;
187 /* another 'we meant to do it this way' hack... up to beta 4, we
188 packed 4 binary zeros here to signify one submapping in use. We
189 now redefine that to mean four bitflags that indicate use of
190 deeper features; bit0:submappings, bit1:coupling,
191 bit2,3:reserved. This is backward compatable with all actual uses
195 oggpack_write(opb,1,1);
196 oggpack_write(opb,info->submaps-1,4);
198 oggpack_write(opb,0,1);
200 if(info->coupling_steps>0){
201 oggpack_write(opb,1,1);
202 oggpack_write(opb,info->coupling_steps-1,8);
204 for(i=0;i<info->coupling_steps;i++){
205 oggpack_write(opb,info->coupling_mag[i],ilog2(vi->channels));
206 oggpack_write(opb,info->coupling_ang[i],ilog2(vi->channels));
209 oggpack_write(opb,0,1);
211 oggpack_write(opb,0,2); /* 2,3:reserved */
213 /* we don't write the channel submappings if we only have one... */
215 for(i=0;i<vi->channels;i++)
216 oggpack_write(opb,info->chmuxlist[i],4);
218 for(i=0;i<info->submaps;i++){
219 oggpack_write(opb,info->timesubmap[i],8);
220 oggpack_write(opb,info->floorsubmap[i],8);
221 oggpack_write(opb,info->residuesubmap[i],8);
225 /* also responsible for range checking */
226 static vorbis_info_mapping *mapping0_unpack(vorbis_info *vi,oggpack_buffer *opb){
228 vorbis_info_mapping0 *info=_ogg_calloc(1,sizeof(*info));
229 codec_setup_info *ci=vi->codec_setup;
230 memset(info,0,sizeof(*info));
232 if(oggpack_read(opb,1))
233 info->submaps=oggpack_read(opb,4)+1;
237 if(oggpack_read(opb,1)){
238 info->coupling_steps=oggpack_read(opb,8)+1;
240 for(i=0;i<info->coupling_steps;i++){
241 int testM=info->coupling_mag[i]=oggpack_read(opb,ilog2(vi->channels));
242 int testA=info->coupling_ang[i]=oggpack_read(opb,ilog2(vi->channels));
247 testM>=vi->channels ||
248 testA>=vi->channels) goto err_out;
253 if(oggpack_read(opb,2)>0)goto err_out; /* 2,3:reserved */
256 for(i=0;i<vi->channels;i++){
257 info->chmuxlist[i]=oggpack_read(opb,4);
258 if(info->chmuxlist[i]>=info->submaps)goto err_out;
261 for(i=0;i<info->submaps;i++){
262 info->timesubmap[i]=oggpack_read(opb,8);
263 if(info->timesubmap[i]>=ci->times)goto err_out;
264 info->floorsubmap[i]=oggpack_read(opb,8);
265 if(info->floorsubmap[i]>=ci->floors)goto err_out;
266 info->residuesubmap[i]=oggpack_read(opb,8);
267 if(info->residuesubmap[i]>=ci->residues)goto err_out;
273 mapping0_free_info(info);
280 #include "envelope.h"
285 /* no time mapping implementation for now */
287 static ogg_int64_t total=0;
288 extern void _analysis_output_always(char *base,int i,float *v,int n,int bark,int dB,ogg_int64_t off);
290 static int mapping0_forward(vorbis_block *vb,vorbis_look_mapping *l){
291 vorbis_dsp_state *vd=vb->vd;
292 vorbis_info *vi=vd->vi;
293 codec_setup_info *ci=vi->codec_setup;
294 backend_lookup_state *b=vb->vd->backend_state;
295 bitrate_manager_state *bm=&b->bms;
296 vorbis_look_mapping0 *look=(vorbis_look_mapping0 *)l;
297 vorbis_info_mapping0 *info=look->map;
298 vorbis_info_mode *mode=look->mode;
299 vorbis_block_internal *vbi=(vorbis_block_internal *)vb->internal;
302 int *nonzero=alloca(sizeof(*nonzero)*vi->channels);
304 float *work=_vorbis_block_alloc(vb,n*sizeof(*work));
306 float global_ampmax=vbi->ampmax;
307 float *local_ampmax=alloca(sizeof(*local_ampmax)*vi->channels);
308 int blocktype=vbi->blocktype;
310 /* we differentiate between short and long block types to help the
311 masking engine; the window shapes also matter.
312 impulse block (a short block in which an impulse occurs)
313 padding block (a short block that pads between a transitional
314 long block and an impulse block, or vice versa)
315 transition block (the wqeird one; a long block with the transition
316 window; affects bass/midrange response and that must be
317 accounted for in masking)
318 long block (run of the mill long block)
321 for(i=0;i<vi->channels;i++){
325 /* the following makes things clearer to *me* anyway */
326 float *pcm =vb->pcm[i];
328 float *logfft =pcm+n/2;
332 float *codedflr=pcm+n/2;
334 float *logmask =work+n/2;*/
336 scale_dB=todB(&scale);
337 /*_analysis_output_always("pcm",seq+i,pcm,n,0,0,total-n/2);*/
339 /* window the PCM data */
340 _vorbis_apply_window(pcm,b->window,ci->blocksizes,vb->lW,vb->W,vb->nW);
341 memcpy(fft,pcm,sizeof(*fft)*n);
343 /*_analysis_output_always("windowed",seq+i,pcm,n,0,0,total-n/2);*/
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=fft[j]*fft[j]+fft[j+1]*fft[j+1];
356 temp=logfft[(j+1)>>1]=scale_dB+.5f*todB(&temp);
357 if(temp>local_ampmax[i])local_ampmax[i]=temp;
360 if(local_ampmax[i]>0.f)local_ampmax[i]=0.f;
361 if(local_ampmax[i]>global_ampmax)global_ampmax=local_ampmax[i];
363 /*_analysis_output("fft",seq+i,logfft,n/2,1,0);*/
366 for(i=0;i<vi->channels;i++){
367 int submap=info->chmuxlist[i];
369 /* the following makes things clearer to *me* anyway */
370 float *mdct =vb->pcm[i];
372 float *codedflr=mdct+n/2;
373 float *logfft =mdct+n/2;
375 float *logmdct =work;
376 float *logmax =mdct+n/2;
377 float *logmask =work+n/2;
381 logmdct[j>>1]=FABS(mdct+j)+FABS(mdct+j+1);
382 logmdct[j>>1]=todB(logmdct+(j>>1));
384 _analysis_output_always("mdct2",seq+i,logmdct,n/4,1,0,total-n/2);*/
388 logmdct[j]=todB(mdct+j);
389 //_analysis_output_always("mdct",seq+i,logmdct,n/2,0,0,0);
392 /* perform psychoacoustics; do masking */
393 _vp_compute_mask(look->psy_look[blocktype],
394 logfft, /* -> logmax */
401 /*_analysis_output("mask",seq+i,logmask,n/2,1,0);*/
402 /* perform floor encoding */
403 nonzero[i]=look->floor_func[submap]->
404 forward(vb,look->floor_look[submap],
413 _vp_remove_floor(look->psy_look[blocktype],
419 if(fabs(res[j])>1200){
421 fprintf(stderr,"%ld ",seq+i);
424 /*_analysis_output("codedflr",seq+i,codedflr,n/2,1,1);*/
428 vbi->ampmax=global_ampmax;
430 /* partition based prequantization and channel coupling */
431 /* Steps in prequant and coupling:
433 classify by |mag| across all pcm vectors
435 down-couple/down-quantize from perfect residue -> quantized vector
438 encode quantized vector; add encoded values to 'so-far' vector
439 more? [not yet at bitrate/not yet at target]
441 down-couple/down-quantize from perfect-'so-far' ->
442 quantized vector; when subtracting coupling,
443 account for +/- out-of-phase component
450 quantization in each iteration is done (after circular normalization
451 in coupling) using a by-iteration quantization granule value.
456 float **quantized=alloca(sizeof(*quantized)*vi->channels);
457 float **sofar=alloca(sizeof(*sofar)*vi->channels);
459 long ***classifications=alloca(sizeof(*classifications)*info->submaps);
460 float ***qbundle=alloca(sizeof(*qbundle)*info->submaps);
461 float ***pcmbundle=alloca(sizeof(*pcmbundle)*info->submaps);
462 float ***sobundle=alloca(sizeof(*sobundle)*info->submaps);
463 int **zerobundle=alloca(sizeof(*zerobundle)*info->submaps);
464 int *chbundle=alloca(sizeof(*chbundle)*info->submaps);
467 /* play a little loose with this abstraction */
468 int quant_passes=ci->coupling_passes;
470 for(i=0;i<vi->channels;i++){
471 quantized[i]=_vorbis_block_alloc(vb,n*sizeof(*sofar[i]));
472 sofar[i]=quantized[i]+n/2;
473 memset(sofar[i],0,sizeof(*sofar[i])*n/2);
476 qbundle[0]=alloca(sizeof(*qbundle[0])*vi->channels);
477 pcmbundle[0]=alloca(sizeof(*pcmbundle[0])*vi->channels);
478 sobundle[0]=alloca(sizeof(*sobundle[0])*vi->channels);
479 zerobundle[0]=alloca(sizeof(*zerobundle[0])*vi->channels);
481 /* initial down-quantized coupling */
483 if(info->coupling_steps==0){
484 /* this assumes all or nothing coupling right now. it should pass
485 through any channels left uncoupled, but it doesn't do that now */
486 for(i=0;i<vi->channels;i++){
488 float *lqua=quantized[i];
493 _vp_quantize_couple(look->psy_look[blocktype],
502 /* classify, by submap */
504 for(i=0;i<info->submaps;i++){
506 qbundle[i]=qbundle[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 qbundle[i][ch_in_bundle]=quantized[j];
517 pcmbundle[i][ch_in_bundle]=pcm[j];
518 sobundle[i][ch_in_bundle++]=sofar[j];
521 chbundle[i]=ch_in_bundle;
522 chcounter+=ch_in_bundle;
524 classifications[i]=look->residue_func[i]->
525 class(vb,look->residue_look[i],pcmbundle[i],zerobundle[i],chbundle[i]);
528 /* actual encoding loop; we pack all the iterations to collect
531 for(i=0;i<quant_passes;){
533 /* perform residue encoding of this pass's quantized residue
534 vector, according residue mapping */
536 for(j=0;j<info->submaps;j++){
537 look->residue_func[j]->
538 forward(vb,look->residue_look[j],
539 qbundle[j],sobundle[j],zerobundle[j],chbundle[j],
540 i,classifications[j],vbi->packet_markers);
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 */
552 for(k=0;k<vi->channels;k++){
554 float *lsof=sofar[k];
555 float *lqua=quantized[k];
557 lqua[j]=lpcm[j]-lsof[j];
561 _vp_quantize_couple(look->psy_look[blocktype],
574 total+=ci->blocksizes[vb->W]/4+ci->blocksizes[vb->nW]/4;
575 look->lastframe=vb->sequence;
579 static int mapping0_inverse(vorbis_block *vb,vorbis_look_mapping *l){
580 vorbis_dsp_state *vd=vb->vd;
581 vorbis_info *vi=vd->vi;
582 codec_setup_info *ci=vi->codec_setup;
583 backend_lookup_state *b=vd->backend_state;
584 vorbis_look_mapping0 *look=(vorbis_look_mapping0 *)l;
585 vorbis_info_mapping0 *info=look->map;
586 vorbis_info_mode *mode=look->mode;
588 long n=vb->pcmend=ci->blocksizes[vb->W];
590 float **pcmbundle=alloca(sizeof(*pcmbundle)*vi->channels);
591 int *zerobundle=alloca(sizeof(*zerobundle)*vi->channels);
593 int *nonzero =alloca(sizeof(*nonzero)*vi->channels);
594 void **floormemo=alloca(sizeof(*floormemo)*vi->channels);
596 /* time domain information decode (note that applying the
597 information would have to happen later; we'll probably add a
598 function entry to the harness for that later */
599 /* NOT IMPLEMENTED */
601 /* recover the spectral envelope; store it in the PCM vector for now */
602 for(i=0;i<vi->channels;i++){
603 int submap=info->chmuxlist[i];
604 floormemo[i]=look->floor_func[submap]->
605 inverse1(vb,look->floor_look[submap]);
610 memset(vb->pcm[i],0,sizeof(*vb->pcm[i])*n/2);
613 /* channel coupling can 'dirty' the nonzero listing */
614 for(i=0;i<info->coupling_steps;i++){
615 if(nonzero[info->coupling_mag[i]] ||
616 nonzero[info->coupling_ang[i]]){
617 nonzero[info->coupling_mag[i]]=1;
618 nonzero[info->coupling_ang[i]]=1;
622 /* recover the residue into our working vectors */
623 for(i=0;i<info->submaps;i++){
625 for(j=0;j<vi->channels;j++){
626 if(info->chmuxlist[j]==i){
628 zerobundle[ch_in_bundle]=1;
630 zerobundle[ch_in_bundle]=0;
631 pcmbundle[ch_in_bundle++]=vb->pcm[j];
635 look->residue_func[i]->inverse(vb,look->residue_look[i],
636 pcmbundle,zerobundle,ch_in_bundle);
639 /* channel coupling */
640 for(i=info->coupling_steps-1;i>=0;i--){
641 float *pcmM=vb->pcm[info->coupling_mag[i]];
642 float *pcmA=vb->pcm[info->coupling_ang[i]];
667 /* compute and apply spectral envelope */
668 for(i=0;i<vi->channels;i++){
669 float *pcm=vb->pcm[i];
670 int submap=info->chmuxlist[i];
671 look->floor_func[submap]->
672 inverse2(vb,look->floor_look[submap],floormemo[i],pcm);
675 /* transform the PCM data; takes PCM vector, vb; modifies PCM vector */
676 /* only MDCT right now.... */
677 for(i=0;i<vi->channels;i++){
678 float *pcm=vb->pcm[i];
679 mdct_backward(b->transform[vb->W][0],pcm,pcm);
682 /* window the data */
683 for(i=0;i<vi->channels;i++){
684 float *pcm=vb->pcm[i];
686 _vorbis_apply_window(pcm,b->window,ci->blocksizes,vb->lW,vb->W,vb->nW);
698 vorbis_func_mapping mapping0_exportbundle={