From 399301e3bd2f1ae0f313c834aec21c1cd746ff96 Mon Sep 17 00:00:00 2001 From: Monty Date: Wed, 3 Mar 2010 16:12:40 +0000 Subject: [PATCH] New noise norm, coupling and quant code necessary for depth>1 coupling. Also substantially cleans up the old code for mono/stereo usage. svn path=/trunk/vorbis/; revision=16946 --- configure.ac | 6 +- examples/encoder_example.c | 44 ++-- lib/info.c | 6 +- lib/mapping0.c | 94 ++----- lib/modes/psych_44.h | 4 +- lib/psy.c | 643 +++++++++++++++++++++------------------------ lib/psy.h | 51 +--- lib/res0.c | 4 +- lib/vorbisenc.c | 3 +- 9 files changed, 362 insertions(+), 493 deletions(-) diff --git a/configure.ac b/configure.ac index a8af6a7..ccb34f1 100644 --- a/configure.ac +++ b/configure.ac @@ -5,7 +5,7 @@ dnl Initialization and Versioning dnl ------------------------------------------------ -AC_INIT([libvorbis],[1.2.3],[vorbis-dev@xiph.org]) +AC_INIT([libvorbis],[1.3.0],[vorbis-dev@xiph.org]) AC_CONFIG_SRCDIR([lib/mdct.c]) @@ -25,7 +25,7 @@ dnl - interfaces added -> increment AGE dnl - interfaces removed -> AGE = 0 V_LIB_CURRENT=4 -V_LIB_REVISION=3 +V_LIB_REVISION=4 V_LIB_AGE=4 VF_LIB_CURRENT=6 @@ -33,7 +33,7 @@ VF_LIB_REVISION=2 VF_LIB_AGE=3 VE_LIB_CURRENT=2 -VE_LIB_REVISION=6 +VE_LIB_REVISION=7 VE_LIB_AGE=0 AC_SUBST(V_LIB_CURRENT) diff --git a/examples/encoder_example.c b/examples/encoder_example.c index ce0edf3..d8d92bc 100644 --- a/examples/encoder_example.c +++ b/examples/encoder_example.c @@ -44,7 +44,7 @@ int main(){ stream of packets */ ogg_page og; /* one Ogg bitstream page. Vorbis packets are inside */ ogg_packet op; /* one raw packet of data for decode */ - + vorbis_info vi; /* struct that stores all the static vorbis bitstream settings */ vorbis_comment vc; /* struct that stores all the user comments */ @@ -69,7 +69,7 @@ int main(){ #ifdef _WIN32 /* We need to set stdin/stdout to binary mode. Damn windows. */ /* if we were reading/writing a file, it would also need to in binary mode, eg, fopen("file.wav","wb"); */ - /* Beware the evil ifdef. We avoid these where we can, but this one we + /* Beware the evil ifdef. We avoid these where we can, but this one we cannot. Don't add any more, you'll probably go to hell if you do. */ _setmode( _fileno( stdin ), _O_BINARY ); _setmode( _fileno( stdout ), _O_BINARY ); @@ -84,16 +84,16 @@ int main(){ for (i=0, founddata=0; i<30 && ! feof(stdin) && ! ferror(stdin); i++) { fread(readbuffer,1,2,stdin); - + if ( ! strncmp((char*)readbuffer, "da", 2) ){ founddata = 1; fread(readbuffer,1,6,stdin); break; } } - + /********** Encode setup ************/ - + vorbis_info_init(&vi); /* choose an encoding mode. A few possibilities commented out, one @@ -102,15 +102,15 @@ int main(){ /********************************************************************* Encoding using a VBR quality mode. The usable range is -.1 (lowest quality, smallest file) to 1. (highest quality, largest file). - Example quality mode .4: 44kHz stereo coupled, roughly 128kbps VBR - + Example quality mode .4: 44kHz stereo coupled, roughly 128kbps VBR + ret = vorbis_encode_init_vbr(&vi,2,44100,.4); --------------------------------------------------------------------- Encoding using an average bitrate mode (ABR). - example: 44kHz stereo coupled, average 128kbps VBR - + example: 44kHz stereo coupled, average 128kbps VBR + ret = vorbis_encode_init(&vi,2,44100,-1,128000,-1); --------------------------------------------------------------------- @@ -140,7 +140,7 @@ int main(){ /* set up the analysis state and auxiliary encoding storage */ vorbis_analysis_init(&vd,&vi); vorbis_block_init(&vd,&vb); - + /* set up our packet->stream encoder */ /* pick a random serial number; that way we can more likely build chained streams just by concatenation */ @@ -174,9 +174,9 @@ int main(){ fwrite(og.header,1,og.header_len,stdout); fwrite(og.body,1,og.body_len,stdout); } - + } - + while(!eos){ long i; long bytes=fread(readbuffer,1,READ*4,stdin); /* stereo hardwired here */ @@ -187,13 +187,13 @@ int main(){ Tell the library we're at end of stream so that it can handle the last frame and mark end of stream in the output properly */ vorbis_analysis_wrote(&vd,0); - + }else{ /* data to encode */ /* expose the buffer to submit data */ float **buffer=vorbis_analysis_buffer(&vd,READ); - + /* uninterleave samples */ for(i=0;ichannels); int *zerobundle=alloca(sizeof(*zerobundle)*vi->channels); - int **sortindex=alloca(sizeof(*sortindex)*vi->channels); - float **mag_memo=NULL; - int **mag_sort=NULL; - - if(info->coupling_steps){ - mag_memo=_vp_quantize_couple_memo(vb, - &ci->psy_g_param, - psy_look, - info, - gmdct); - - mag_sort=_vp_quantize_couple_sort(vb, - psy_look, - info, - mag_memo); - - hf_reduction(&ci->psy_g_param, - psy_look, - info, - mag_memo); - } - - memset(sortindex,0,sizeof(*sortindex)*vi->channels); - if(psy_look->vi->normal_channel_p){ - for(i=0;ichannels;i++){ - float *mdct =gmdct[i]; - sortindex[i]=alloca(sizeof(**sortindex)*n/2); - _vp_noise_normalize_sort(psy_look,mdct,sortindex[i]); - } - } for(k=(vorbis_bitrate_managed(vb)?0:PACKETBLOBS/2); k<=(vorbis_bitrate_managed(vb)?PACKETBLOBS-1:PACKETBLOBS/2); @@ -650,9 +620,7 @@ static int mapping0_forward(vorbis_block *vb){ /* encode floor, compute masking curve, sep out residue */ for(i=0;ichannels;i++){ int submap=info->chmuxlist[i]; - float *mdct =gmdct[i]; - float *res =vb->pcm[i]; - int *ilogmask=iwork[i]; + int *ilogmask=iwork[i]; nonzero[i]=floor1_encode(opb,vb,b->flr[info->floorsubmap[submap]], floor_posts[i][k], @@ -663,28 +631,8 @@ static int mapping0_forward(vorbis_block *vb){ sprintf(buf,"maskI%c%d",i?'R':'L',k); float work[n/2]; for(j=0;jpsy_g_param.sliding_lowpass[vb->W][k]); - - _vp_noise_normalize(psy_look,res,res+n/2,sortindex[i]); - - -#if 0 - { - char buf[80]; - float work[n/2]; - for(j=0;jcoupling_steps){ - _vp_couple(k, - &ci->psy_g_param, - psy_look, - info, - vb->pcm, - mag_memo, - mag_sort, - iwork, - nonzero, - ci->psy_g_param.sliding_lowpass[vb->W][k]); - } - for(j=0;jchannels;j++){ - int *ires = iwork[j]; - float *res = vb->pcm[j]+n/2; - for(i=0;ipsy_g_param, + psy_look, + info, + gmdct, + iwork, + nonzero, + ci->psy_g_param.sliding_lowpass[vb->W][k], + vi->channels); + +#if 0 + for(i=0;ichannels;i++){ + char buf[80]; + sprintf(buf,"resI%c%d",i?'R':'L',k); + float work[n/2]; + for(j=0;jsubmaps;i++){ diff --git a/lib/modes/psych_44.h b/lib/modes/psych_44.h index 63d8bc4..4c52f7f 100644 --- a/lib/modes/psych_44.h +++ b/lib/modes/psych_44.h @@ -500,8 +500,8 @@ static const vorbis_info_psy _psy_info_template={ 1, -0.f, .5f, .5f, 0,0,0, /* noiseoffset*3, noisecompand, max_curve_dB */ {{-1},{-1},{-1}},{-1},105.f, - /* noise normalization - channel_p, point_p, start, partition, thresh. */ - 0,0,-1,-1,0., + /* noise normalization - noise_p, start, partition, thresh. */ + 0,-1,-1,0., }; /* ath ****************/ diff --git a/lib/psy.c b/lib/psy.c index d8516c6..2361eee 100644 --- a/lib/psy.c +++ b/lib/psy.c @@ -5,7 +5,7 @@ * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE * * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. * * * - * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2009 * + * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2010 * * by the Xiph.Org Foundation http://www.xiph.org/ * * * ******************************************************************** @@ -696,92 +696,6 @@ static void bark_noise_hybridmp(int n,const long *b, } } -static const float FLOOR1_fromdB_INV_LOOKUP[256]={ - 0.F, 8.81683e+06F, 8.27882e+06F, 7.77365e+06F, - 7.29930e+06F, 6.85389e+06F, 6.43567e+06F, 6.04296e+06F, - 5.67422e+06F, 5.32798e+06F, 5.00286e+06F, 4.69759e+06F, - 4.41094e+06F, 4.14178e+06F, 3.88905e+06F, 3.65174e+06F, - 3.42891e+06F, 3.21968e+06F, 3.02321e+06F, 2.83873e+06F, - 2.66551e+06F, 2.50286e+06F, 2.35014e+06F, 2.20673e+06F, - 2.07208e+06F, 1.94564e+06F, 1.82692e+06F, 1.71544e+06F, - 1.61076e+06F, 1.51247e+06F, 1.42018e+06F, 1.33352e+06F, - 1.25215e+06F, 1.17574e+06F, 1.10400e+06F, 1.03663e+06F, - 973377.F, 913981.F, 858210.F, 805842.F, - 756669.F, 710497.F, 667142.F, 626433.F, - 588208.F, 552316.F, 518613.F, 486967.F, - 457252.F, 429351.F, 403152.F, 378551.F, - 355452.F, 333762.F, 313396.F, 294273.F, - 276316.F, 259455.F, 243623.F, 228757.F, - 214798.F, 201691.F, 189384.F, 177828.F, - 166977.F, 156788.F, 147221.F, 138237.F, - 129802.F, 121881.F, 114444.F, 107461.F, - 100903.F, 94746.3F, 88964.9F, 83536.2F, - 78438.8F, 73652.5F, 69158.2F, 64938.1F, - 60975.6F, 57254.9F, 53761.2F, 50480.6F, - 47400.3F, 44507.9F, 41792.0F, 39241.9F, - 36847.3F, 34598.9F, 32487.7F, 30505.3F, - 28643.8F, 26896.0F, 25254.8F, 23713.7F, - 22266.7F, 20908.0F, 19632.2F, 18434.2F, - 17309.4F, 16253.1F, 15261.4F, 14330.1F, - 13455.7F, 12634.6F, 11863.7F, 11139.7F, - 10460.0F, 9821.72F, 9222.39F, 8659.64F, - 8131.23F, 7635.06F, 7169.17F, 6731.70F, - 6320.93F, 5935.23F, 5573.06F, 5232.99F, - 4913.67F, 4613.84F, 4332.30F, 4067.94F, - 3819.72F, 3586.64F, 3367.78F, 3162.28F, - 2969.31F, 2788.13F, 2617.99F, 2458.24F, - 2308.24F, 2167.39F, 2035.14F, 1910.95F, - 1794.35F, 1684.85F, 1582.04F, 1485.51F, - 1394.86F, 1309.75F, 1229.83F, 1154.78F, - 1084.32F, 1018.15F, 956.024F, 897.687F, - 842.910F, 791.475F, 743.179F, 697.830F, - 655.249F, 615.265F, 577.722F, 542.469F, - 509.367F, 478.286F, 449.101F, 421.696F, - 395.964F, 371.803F, 349.115F, 327.812F, - 307.809F, 289.026F, 271.390F, 254.830F, - 239.280F, 224.679F, 210.969F, 198.096F, - 186.008F, 174.658F, 164.000F, 153.993F, - 144.596F, 135.773F, 127.488F, 119.708F, - 112.404F, 105.545F, 99.1046F, 93.0572F, - 87.3788F, 82.0469F, 77.0404F, 72.3394F, - 67.9252F, 63.7804F, 59.8885F, 56.2341F, - 52.8027F, 49.5807F, 46.5553F, 43.7144F, - 41.0470F, 38.5423F, 36.1904F, 33.9821F, - 31.9085F, 29.9614F, 28.1332F, 26.4165F, - 24.8045F, 23.2910F, 21.8697F, 20.5352F, - 19.2822F, 18.1056F, 17.0008F, 15.9634F, - 14.9893F, 14.0746F, 13.2158F, 12.4094F, - 11.6522F, 10.9411F, 10.2735F, 9.64662F, - 9.05798F, 8.50526F, 7.98626F, 7.49894F, - 7.04135F, 6.61169F, 6.20824F, 5.82941F, - 5.47370F, 5.13970F, 4.82607F, 4.53158F, - 4.25507F, 3.99542F, 3.75162F, 3.52269F, - 3.30774F, 3.10590F, 2.91638F, 2.73842F, - 2.57132F, 2.41442F, 2.26709F, 2.12875F, - 1.99885F, 1.87688F, 1.76236F, 1.65482F, - 1.55384F, 1.45902F, 1.36999F, 1.28640F, - 1.20790F, 1.13419F, 1.06499F, 1.F -}; - -void _vp_remove_floor(vorbis_look_psy *p, - float *mdct, - int *codedflr, - float *residue, - int sliding_lowpass){ - - int i,n=p->n; - - if(sliding_lowpass>n)sliding_lowpass=n; - - for(i=0;ifabs(*qB); - test1-= fabs(*qA)fabs(B))<<1)-1; - if(test1==1){ - *qB=(*qA>0.f?*qA-*qB:*qB-*qA); - }else{ - float temp=*qB; - *qB=(*qB>0.f?*qA-*qB:*qB-*qA); - *qA=temp; - } +static float FLOOR1_fromdB_LOOKUP[256]={ + 1.0649863e-07F, 1.1341951e-07F, 1.2079015e-07F, 1.2863978e-07F, + 1.3699951e-07F, 1.4590251e-07F, 1.5538408e-07F, 1.6548181e-07F, + 1.7623575e-07F, 1.8768855e-07F, 1.9988561e-07F, 2.128753e-07F, + 2.2670913e-07F, 2.4144197e-07F, 2.5713223e-07F, 2.7384213e-07F, + 2.9163793e-07F, 3.1059021e-07F, 3.3077411e-07F, 3.5226968e-07F, + 3.7516214e-07F, 3.9954229e-07F, 4.2550680e-07F, 4.5315863e-07F, + 4.8260743e-07F, 5.1396998e-07F, 5.4737065e-07F, 5.8294187e-07F, + 6.2082472e-07F, 6.6116941e-07F, 7.0413592e-07F, 7.4989464e-07F, + 7.9862701e-07F, 8.5052630e-07F, 9.0579828e-07F, 9.6466216e-07F, + 1.0273513e-06F, 1.0941144e-06F, 1.1652161e-06F, 1.2409384e-06F, + 1.3215816e-06F, 1.4074654e-06F, 1.4989305e-06F, 1.5963394e-06F, + 1.7000785e-06F, 1.8105592e-06F, 1.9282195e-06F, 2.0535261e-06F, + 2.1869758e-06F, 2.3290978e-06F, 2.4804557e-06F, 2.6416497e-06F, + 2.8133190e-06F, 2.9961443e-06F, 3.1908506e-06F, 3.3982101e-06F, + 3.6190449e-06F, 3.8542308e-06F, 4.1047004e-06F, 4.3714470e-06F, + 4.6555282e-06F, 4.9580707e-06F, 5.2802740e-06F, 5.6234160e-06F, + 5.9888572e-06F, 6.3780469e-06F, 6.7925283e-06F, 7.2339451e-06F, + 7.7040476e-06F, 8.2047000e-06F, 8.7378876e-06F, 9.3057248e-06F, + 9.9104632e-06F, 1.0554501e-05F, 1.1240392e-05F, 1.1970856e-05F, + 1.2748789e-05F, 1.3577278e-05F, 1.4459606e-05F, 1.5399272e-05F, + 1.6400004e-05F, 1.7465768e-05F, 1.8600792e-05F, 1.9809576e-05F, + 2.1096914e-05F, 2.2467911e-05F, 2.3928002e-05F, 2.5482978e-05F, + 2.7139006e-05F, 2.8902651e-05F, 3.0780908e-05F, 3.2781225e-05F, + 3.4911534e-05F, 3.7180282e-05F, 3.9596466e-05F, 4.2169667e-05F, + 4.4910090e-05F, 4.7828601e-05F, 5.0936773e-05F, 5.4246931e-05F, + 5.7772202e-05F, 6.1526565e-05F, 6.5524908e-05F, 6.9783085e-05F, + 7.4317983e-05F, 7.9147585e-05F, 8.4291040e-05F, 8.9768747e-05F, + 9.5602426e-05F, 0.00010181521F, 0.00010843174F, 0.00011547824F, + 0.00012298267F, 0.00013097477F, 0.00013948625F, 0.00014855085F, + 0.00015820453F, 0.00016848555F, 0.00017943469F, 0.00019109536F, + 0.00020351382F, 0.00021673929F, 0.00023082423F, 0.00024582449F, + 0.00026179955F, 0.00027881276F, 0.00029693158F, 0.00031622787F, + 0.00033677814F, 0.00035866388F, 0.00038197188F, 0.00040679456F, + 0.00043323036F, 0.00046138411F, 0.00049136745F, 0.00052329927F, + 0.00055730621F, 0.00059352311F, 0.00063209358F, 0.00067317058F, + 0.00071691700F, 0.00076350630F, 0.00081312324F, 0.00086596457F, + 0.00092223983F, 0.00098217216F, 0.0010459992F, 0.0011139742F, + 0.0011863665F, 0.0012634633F, 0.0013455702F, 0.0014330129F, + 0.0015261382F, 0.0016253153F, 0.0017309374F, 0.0018434235F, + 0.0019632195F, 0.0020908006F, 0.0022266726F, 0.0023713743F, + 0.0025254795F, 0.0026895994F, 0.0028643847F, 0.0030505286F, + 0.0032487691F, 0.0034598925F, 0.0036847358F, 0.0039241906F, + 0.0041792066F, 0.0044507950F, 0.0047400328F, 0.0050480668F, + 0.0053761186F, 0.0057254891F, 0.0060975636F, 0.0064938176F, + 0.0069158225F, 0.0073652516F, 0.0078438871F, 0.0083536271F, + 0.0088964928F, 0.009474637F, 0.010090352F, 0.010746080F, + 0.011444421F, 0.012188144F, 0.012980198F, 0.013823725F, + 0.014722068F, 0.015678791F, 0.016697687F, 0.017782797F, + 0.018938423F, 0.020169149F, 0.021479854F, 0.022875735F, + 0.024362330F, 0.025945531F, 0.027631618F, 0.029427276F, + 0.031339626F, 0.033376252F, 0.035545228F, 0.037855157F, + 0.040315199F, 0.042935108F, 0.045725273F, 0.048696758F, + 0.051861348F, 0.055231591F, 0.058820850F, 0.062643361F, + 0.066714279F, 0.071049749F, 0.075666962F, 0.080584227F, + 0.085821044F, 0.091398179F, 0.097337747F, 0.10366330F, + 0.11039993F, 0.11757434F, 0.12521498F, 0.13335215F, + 0.14201813F, 0.15124727F, 0.16107617F, 0.17154380F, + 0.18269168F, 0.19456402F, 0.20720788F, 0.22067342F, + 0.23501402F, 0.25028656F, 0.26655159F, 0.28387361F, + 0.30232132F, 0.32196786F, 0.34289114F, 0.36517414F, + 0.38890521F, 0.41417847F, 0.44109412F, 0.46975890F, + 0.50028648F, 0.53279791F, 0.56742212F, 0.60429640F, + 0.64356699F, 0.68538959F, 0.72993007F, 0.77736504F, + 0.82788260F, 0.88168307F, 0.9389798F, 1.F, +}; - if(*qB>fabs(*qA)*1.9999f){ - *qB= -fabs(*qA)*2.f; - *qA= -*qA; - } +/* this is for per-channel noise normalization */ +static int apsort(const void *a, const void *b){ + float f1=**(float**)a; + float f2=**(float**)b; + return (f1f2); } -static const float hypot_lookup[32]={ - -0.009935, -0.011245, -0.012726, -0.014397, - -0.016282, -0.018407, -0.020800, -0.023494, - -0.026522, -0.029923, -0.033737, -0.038010, - -0.042787, -0.048121, -0.054064, -0.060671, - -0.068000, -0.076109, -0.085054, -0.094892, - -0.105675, -0.117451, -0.130260, -0.144134, - -0.159093, -0.175146, -0.192286, -0.210490, - -0.229718, -0.249913, -0.271001, -0.292893}; - -static void precomputed_couple_point(float premag, - int floorA,int floorB, - float *mag, float *ang){ - - int test=(floorA>floorB)-1; - int offset=31-abs(floorA-floorB); - float floormag=hypot_lookup[((offset<0)-1)&offset]+1.f; +static void flag_lossless(int limit, float prepoint, float postpoint, float *mdct, + float *floor, int *flag, int i, int jn){ + int j; + for(j=0;j=limit-i ? postpoint : prepoint; + float r = fabs(mdct[j])/floor[j]; + if(rvi; + float **sort = alloca(n*sizeof(*sort)); + int j,count=0; + int start = (vi->normal_p ? vi->normal_start-i : n); + if(start>n)start=n; -/* just like below, this is currently set up to only do - single-step-depth coupling. Otherwise, we'd have to do more - copying (which will be inevitable later) */ - -/* doing the real circular magnitude calculation is audibly superior - to (A+B)/sqrt(2) */ -static float dipole_hypot(float a, float b){ - if(a>0.){ - if(b>0.)return sqrt(a*a+b*b); - if(a>-b)return sqrt(a*a-b*b); - return -sqrt(b*b-a*a); + /* still responsible for populating *out where noise norm not in + effect. There's no need to [re]populate *q in these areas */ + for(j=0;jb)return -sqrt(a*a-b*b); - return sqrt(b*b-a*a); -} -static float round_hypot(float a, float b){ - if(a>0.){ - if(b>0.)return sqrt(a*a+b*b); - if(a>-b)return sqrt(a*a+b*b); - return -sqrt(b*b+a*a); + + /* sort magnitudes for noise norm portion of partition */ + for(;j pointlimit */ + if(ve<.25f && (!flags || j>=limit-i)){ + acc += ve; + sort[count++]=q+j; /* q is fabs(r) for unflagged element */ + }else{ + /* For now: no acc adjustment for nonzero quantization. populate *out and q as this value is final. */ + if(r[j]<0) + out[j] = -rint(sqrt(ve)); + else + out[j] = rint(sqrt(ve)); + q[j] = out[j]*out[j]*f[j]; + } + }/* else{ + again, no energy adjustment for error in nonzero quant-- for now + }*/ } - if(b<0.)return -sqrt(a*a+b*b); - if(-a>b)return -sqrt(a*a+b*b); - return sqrt(b*b+a*a); -} -/* revert to round hypot for now */ -float **_vp_quantize_couple_memo(vorbis_block *vb, - vorbis_info_psy_global *g, - vorbis_look_psy *p, - vorbis_info_mapping0 *vi, - float **mdct){ - - int i,j,n=p->n; - float **ret=_vorbis_block_alloc(vb,vi->coupling_steps*sizeof(*ret)); - int limit=g->coupling_pointlimit[p->vi->blockflag][PACKETBLOBS/2]; - - for(i=0;icoupling_steps;i++){ - float *mdctM=mdct[vi->coupling_mag[i]]; - float *mdctA=mdct[vi->coupling_ang[i]]; - ret[i]=_vorbis_block_alloc(vb,n*sizeof(**ret)); - for(j=0;j=vi->normal_thresh){ + out[k]=unitnorm(r[k]); + acc-=1.f; + q[k]=f[k]; + }else{ + out[k]=0; + q[k]=0.f; + } + } } - return(ret); + return acc; } -/* this is for per-channel noise normalization */ -static int apsort(const void *a, const void *b){ - float f1=fabs(**(float**)a); - float f2=fabs(**(float**)b); - return (f1f2); -} +/* Noise normalization, quantization and coupling are not wholly + seperable processes in depth>1 coupling. */ +void _vp_couple_quantize_normalize(int blobno, + vorbis_info_psy_global *g, + vorbis_look_psy *p, + vorbis_info_mapping0 *vi, + float **mdct, + int **iwork, + int *nonzero, + int sliding_lowpass, + int ch){ -int **_vp_quantize_couple_sort(vorbis_block *vb, - vorbis_look_psy *p, - vorbis_info_mapping0 *vi, - float **mags){ + int i; + int n = p->n; + int partition=(p->vi->normal_p ? p->vi->normal_partition : 16); + int limit = g->coupling_pointlimit[p->vi->blockflag][blobno]; + float prepoint=stereo_threshholds[g->coupling_prepointamp[blobno]]; + float postpoint=stereo_threshholds[g->coupling_postpointamp[blobno]]; + float de=0.1*p->m_val; /* a blend of the AoTuV M2 and M3 code here and below */ + /* mdct is our raw mdct output, floor not removed. */ + /* inout passes in the ifloor, passes back quantized result */ - if(p->vi->normal_point_p){ - int i,j,k,n=p->n; - int **ret=_vorbis_block_alloc(vb,vi->coupling_steps*sizeof(*ret)); - int partition=p->vi->normal_partition; - float **work=alloca(sizeof(*work)*partition); + /* unquantized energy (negative indicates amplitude has negative sign) */ + float **raw = alloca(ch*sizeof(*raw)); - for(i=0;icoupling_steps;i++){ - ret[i]=_vorbis_block_alloc(vb,n*sizeof(**ret)); + /* dual pupose; quantized energy (if flag set), othersize fabs(raw) */ + float **quant = alloca(ch*sizeof(*quant)); - for(j=0;jn; - vorbis_info_psy *vi=p->vi; - int partition=vi->normal_partition; - float **work=alloca(sizeof(*work)*partition); - int start=vi->normal_start; - - for(j=start;jn)partition=n-j; - for(i=0;in; - vorbis_info_psy *vi=p->vi; - int partition=vi->normal_partition; - int start=vi->normal_start; + /* non-zero flag working vector */ + int *nz = alloca(ch*sizeof(*nz)); - if(start>n)start=n; + /* energy surplus/defecit tracking */ + float *acc = alloca((ch+vi->coupling_steps)*sizeof(*acc)); + + /* The threshold of a stereo is changed with the size of n */ + if(n > 1000) + postpoint=stereo_threshholds_limited[g->coupling_postpointamp[blobno]]; - if(vi->normal_channel_p){ - for(;jcoupling_steps;i++) + acc[i]=0.f; - for(i=j;i n-i ? n-i : partition; + int step,track = 0; - for(i=0;i=.25f){ - out[k]=rint(in[k]); - acc-=in[k]*in[k]; - flag=1; - }else{ - if(accnormal_thresh)break; - out[k]=unitnorm(in[k]); - acc-=1.; + /* prefill */ + memset(flag[0],0,ch*partition*sizeof(**flag)); + for(k=0;kcoupling_steps;step++){ + int Mi = vi->coupling_mag[step]; + int Ai = vi->coupling_ang[step]; + int *iM = &iwork[Mi][i]; + int *iA = &iwork[Ai][i]; + float *reM = raw[Mi]; + float *reA = raw[Ai]; + float *qeM = quant[Mi]; + float *qeA = quant[Ai]; + float *floorM = floor[Mi]; + float *floorA = floor[Ai]; + int *fM = flag[Mi]; + int *fA = flag[Ai]; + + if(nz[Mi] || nz[Ai]){ + nz[Mi] = nz[Ai] = 1; + + for(j=0;jabs(B)||B>abs(A) ? A-B : B-A); + if(abs(B)>abs(A))iM[j]=B; + + /* collapse two equivalent tuples to one */ + if(abs(iM[j])*2==iA[j]){ + iA[j]= -iA[j]; + iM[j]= -iM[j]; + } + } -void _vp_couple(int blobno, - vorbis_info_psy_global *g, - vorbis_look_psy *p, - vorbis_info_mapping0 *vi, - float **res, - float **mag_memo, - int **mag_sort, - int **ifloor, - int *nonzero, - int sliding_lowpass){ - - int i,j,k,n=p->n; - - /* perform any requested channel coupling */ - /* point stereo can only be used in a first stage (in this encoder) - because of the dependency on floor lookups */ - for(i=0;icoupling_steps;i++){ - - /* once we're doing multistage coupling in which a channel goes - through more than one coupling step, the floor vector - magnitudes will also have to be recalculated an propogated - along with PCM. Right now, we're not (that will wait until 5.1 - most likely), so the code isn't here yet. The memory management - here is all assuming single depth couplings anyway. */ - - /* make sure coupling a zero and a nonzero channel results in two - nonzero channels. */ - if(nonzero[vi->coupling_mag[i]] || - nonzero[vi->coupling_ang[i]]){ - - float *rM=res[vi->coupling_mag[i]]; - float *rA=res[vi->coupling_ang[i]]; - float *qM=rM+n; - float *qA=rA+n; - int *floorM=ifloor[vi->coupling_mag[i]]; - int *floorA=ifloor[vi->coupling_ang[i]]; - float prepoint=stereo_threshholds[g->coupling_prepointamp[blobno]]; - float postpoint=stereo_threshholds[g->coupling_postpointamp[blobno]]; - int partition=(p->vi->normal_point_p?p->vi->normal_partition:p->n); - int limit=g->coupling_pointlimit[p->vi->blockflag][blobno]; - - nonzero[vi->coupling_mag[i]]=1; - nonzero[vi->coupling_ang[i]]=1; - - /* The threshold of a stereo is changed with the size of n */ - if(n > 1000) - postpoint=stereo_threshholds_limited[g->coupling_postpointamp[blobno]]; - - for(j=0;jn;j+=partition){ - float acc=0.f; - - for(k=0;k=limit && fabs(rM[l])vi->normal_point_p){ - for(k=0;k=p->vi->normal_thresh;k++){ - int l=mag_sort[i][j+k]; - if(l=limit && rint(qM[l])==0.f){ - qM[l]=unitnorm(qM[l]); - acc-=1.f; + /* lossy (point) coupling */ + + if(jn, de=0.3*p->m_val; - int limit=g->coupling_pointlimit[p->vi->blockflag][PACKETBLOBS/2]; - - for(i=0; icoupling_steps; i++){ - /* for(j=start; jpcmend/2; + int i,used=0; for(i=0;iblockflag=block>>1; if(hi->noise_normalize_p){ - p->normal_channel_p=1; - p->normal_point_p=1; + p->normal_p=1; p->normal_start=nn_start[is]; p->normal_partition=nn_partition[is]; p->normal_thresh=nn_thresh[is]; -- 2.7.4