vrshoot

annotate libs/vorbis/psy.c @ 3:c179c72369be

rename candy->vr
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 03 Feb 2014 08:52:13 +0200
parents
children
rev   line source
nuclear@0 1 /********************************************************************
nuclear@0 2 * *
nuclear@0 3 * THIS FILE IS PART OF THE OggVorbis SOFTWARE CODEC SOURCE CODE. *
nuclear@0 4 * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS *
nuclear@0 5 * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE *
nuclear@0 6 * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. *
nuclear@0 7 * *
nuclear@0 8 * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2010 *
nuclear@0 9 * by the Xiph.Org Foundation http://www.xiph.org/ *
nuclear@0 10 * *
nuclear@0 11 ********************************************************************
nuclear@0 12
nuclear@0 13 function: psychoacoustics not including preecho
nuclear@0 14 last mod: $Id: psy.c 18077 2011-09-02 02:49:00Z giles $
nuclear@0 15
nuclear@0 16 ********************************************************************/
nuclear@0 17
nuclear@0 18 #include <stdlib.h>
nuclear@0 19 #include <math.h>
nuclear@0 20 #include <string.h>
nuclear@0 21 #include "vorbis/codec.h"
nuclear@0 22 #include "codec_internal.h"
nuclear@0 23
nuclear@0 24 #include "masking.h"
nuclear@0 25 #include "psy.h"
nuclear@0 26 #include "os.h"
nuclear@0 27 #include "lpc.h"
nuclear@0 28 #include "smallft.h"
nuclear@0 29 #include "scales.h"
nuclear@0 30 #include "misc.h"
nuclear@0 31
nuclear@0 32 #define NEGINF -9999.f
nuclear@0 33 static const double stereo_threshholds[]={0.0, .5, 1.0, 1.5, 2.5, 4.5, 8.5, 16.5, 9e10};
nuclear@0 34 static const double stereo_threshholds_limited[]={0.0, .5, 1.0, 1.5, 2.0, 2.5, 4.5, 8.5, 9e10};
nuclear@0 35
nuclear@0 36 vorbis_look_psy_global *_vp_global_look(vorbis_info *vi){
nuclear@0 37 codec_setup_info *ci=vi->codec_setup;
nuclear@0 38 vorbis_info_psy_global *gi=&ci->psy_g_param;
nuclear@0 39 vorbis_look_psy_global *look=_ogg_calloc(1,sizeof(*look));
nuclear@0 40
nuclear@0 41 look->channels=vi->channels;
nuclear@0 42
nuclear@0 43 look->ampmax=-9999.;
nuclear@0 44 look->gi=gi;
nuclear@0 45 return(look);
nuclear@0 46 }
nuclear@0 47
nuclear@0 48 void _vp_global_free(vorbis_look_psy_global *look){
nuclear@0 49 if(look){
nuclear@0 50 memset(look,0,sizeof(*look));
nuclear@0 51 _ogg_free(look);
nuclear@0 52 }
nuclear@0 53 }
nuclear@0 54
nuclear@0 55 void _vi_gpsy_free(vorbis_info_psy_global *i){
nuclear@0 56 if(i){
nuclear@0 57 memset(i,0,sizeof(*i));
nuclear@0 58 _ogg_free(i);
nuclear@0 59 }
nuclear@0 60 }
nuclear@0 61
nuclear@0 62 void _vi_psy_free(vorbis_info_psy *i){
nuclear@0 63 if(i){
nuclear@0 64 memset(i,0,sizeof(*i));
nuclear@0 65 _ogg_free(i);
nuclear@0 66 }
nuclear@0 67 }
nuclear@0 68
nuclear@0 69 static void min_curve(float *c,
nuclear@0 70 float *c2){
nuclear@0 71 int i;
nuclear@0 72 for(i=0;i<EHMER_MAX;i++)if(c2[i]<c[i])c[i]=c2[i];
nuclear@0 73 }
nuclear@0 74 static void max_curve(float *c,
nuclear@0 75 float *c2){
nuclear@0 76 int i;
nuclear@0 77 for(i=0;i<EHMER_MAX;i++)if(c2[i]>c[i])c[i]=c2[i];
nuclear@0 78 }
nuclear@0 79
nuclear@0 80 static void attenuate_curve(float *c,float att){
nuclear@0 81 int i;
nuclear@0 82 for(i=0;i<EHMER_MAX;i++)
nuclear@0 83 c[i]+=att;
nuclear@0 84 }
nuclear@0 85
nuclear@0 86 static float ***setup_tone_curves(float curveatt_dB[P_BANDS],float binHz,int n,
nuclear@0 87 float center_boost, float center_decay_rate){
nuclear@0 88 int i,j,k,m;
nuclear@0 89 float ath[EHMER_MAX];
nuclear@0 90 float workc[P_BANDS][P_LEVELS][EHMER_MAX];
nuclear@0 91 float athc[P_LEVELS][EHMER_MAX];
nuclear@0 92 float *brute_buffer=alloca(n*sizeof(*brute_buffer));
nuclear@0 93
nuclear@0 94 float ***ret=_ogg_malloc(sizeof(*ret)*P_BANDS);
nuclear@0 95
nuclear@0 96 memset(workc,0,sizeof(workc));
nuclear@0 97
nuclear@0 98 for(i=0;i<P_BANDS;i++){
nuclear@0 99 /* we add back in the ATH to avoid low level curves falling off to
nuclear@0 100 -infinity and unnecessarily cutting off high level curves in the
nuclear@0 101 curve limiting (last step). */
nuclear@0 102
nuclear@0 103 /* A half-band's settings must be valid over the whole band, and
nuclear@0 104 it's better to mask too little than too much */
nuclear@0 105 int ath_offset=i*4;
nuclear@0 106 for(j=0;j<EHMER_MAX;j++){
nuclear@0 107 float min=999.;
nuclear@0 108 for(k=0;k<4;k++)
nuclear@0 109 if(j+k+ath_offset<MAX_ATH){
nuclear@0 110 if(min>ATH[j+k+ath_offset])min=ATH[j+k+ath_offset];
nuclear@0 111 }else{
nuclear@0 112 if(min>ATH[MAX_ATH-1])min=ATH[MAX_ATH-1];
nuclear@0 113 }
nuclear@0 114 ath[j]=min;
nuclear@0 115 }
nuclear@0 116
nuclear@0 117 /* copy curves into working space, replicate the 50dB curve to 30
nuclear@0 118 and 40, replicate the 100dB curve to 110 */
nuclear@0 119 for(j=0;j<6;j++)
nuclear@0 120 memcpy(workc[i][j+2],tonemasks[i][j],EHMER_MAX*sizeof(*tonemasks[i][j]));
nuclear@0 121 memcpy(workc[i][0],tonemasks[i][0],EHMER_MAX*sizeof(*tonemasks[i][0]));
nuclear@0 122 memcpy(workc[i][1],tonemasks[i][0],EHMER_MAX*sizeof(*tonemasks[i][0]));
nuclear@0 123
nuclear@0 124 /* apply centered curve boost/decay */
nuclear@0 125 for(j=0;j<P_LEVELS;j++){
nuclear@0 126 for(k=0;k<EHMER_MAX;k++){
nuclear@0 127 float adj=center_boost+abs(EHMER_OFFSET-k)*center_decay_rate;
nuclear@0 128 if(adj<0. && center_boost>0)adj=0.;
nuclear@0 129 if(adj>0. && center_boost<0)adj=0.;
nuclear@0 130 workc[i][j][k]+=adj;
nuclear@0 131 }
nuclear@0 132 }
nuclear@0 133
nuclear@0 134 /* normalize curves so the driving amplitude is 0dB */
nuclear@0 135 /* make temp curves with the ATH overlayed */
nuclear@0 136 for(j=0;j<P_LEVELS;j++){
nuclear@0 137 attenuate_curve(workc[i][j],curveatt_dB[i]+100.-(j<2?2:j)*10.-P_LEVEL_0);
nuclear@0 138 memcpy(athc[j],ath,EHMER_MAX*sizeof(**athc));
nuclear@0 139 attenuate_curve(athc[j],+100.-j*10.f-P_LEVEL_0);
nuclear@0 140 max_curve(athc[j],workc[i][j]);
nuclear@0 141 }
nuclear@0 142
nuclear@0 143 /* Now limit the louder curves.
nuclear@0 144
nuclear@0 145 the idea is this: We don't know what the playback attenuation
nuclear@0 146 will be; 0dB SL moves every time the user twiddles the volume
nuclear@0 147 knob. So that means we have to use a single 'most pessimal' curve
nuclear@0 148 for all masking amplitudes, right? Wrong. The *loudest* sound
nuclear@0 149 can be in (we assume) a range of ...+100dB] SL. However, sounds
nuclear@0 150 20dB down will be in a range ...+80], 40dB down is from ...+60],
nuclear@0 151 etc... */
nuclear@0 152
nuclear@0 153 for(j=1;j<P_LEVELS;j++){
nuclear@0 154 min_curve(athc[j],athc[j-1]);
nuclear@0 155 min_curve(workc[i][j],athc[j]);
nuclear@0 156 }
nuclear@0 157 }
nuclear@0 158
nuclear@0 159 for(i=0;i<P_BANDS;i++){
nuclear@0 160 int hi_curve,lo_curve,bin;
nuclear@0 161 ret[i]=_ogg_malloc(sizeof(**ret)*P_LEVELS);
nuclear@0 162
nuclear@0 163 /* low frequency curves are measured with greater resolution than
nuclear@0 164 the MDCT/FFT will actually give us; we want the curve applied
nuclear@0 165 to the tone data to be pessimistic and thus apply the minimum
nuclear@0 166 masking possible for a given bin. That means that a single bin
nuclear@0 167 could span more than one octave and that the curve will be a
nuclear@0 168 composite of multiple octaves. It also may mean that a single
nuclear@0 169 bin may span > an eighth of an octave and that the eighth
nuclear@0 170 octave values may also be composited. */
nuclear@0 171
nuclear@0 172 /* which octave curves will we be compositing? */
nuclear@0 173 bin=floor(fromOC(i*.5)/binHz);
nuclear@0 174 lo_curve= ceil(toOC(bin*binHz+1)*2);
nuclear@0 175 hi_curve= floor(toOC((bin+1)*binHz)*2);
nuclear@0 176 if(lo_curve>i)lo_curve=i;
nuclear@0 177 if(lo_curve<0)lo_curve=0;
nuclear@0 178 if(hi_curve>=P_BANDS)hi_curve=P_BANDS-1;
nuclear@0 179
nuclear@0 180 for(m=0;m<P_LEVELS;m++){
nuclear@0 181 ret[i][m]=_ogg_malloc(sizeof(***ret)*(EHMER_MAX+2));
nuclear@0 182
nuclear@0 183 for(j=0;j<n;j++)brute_buffer[j]=999.;
nuclear@0 184
nuclear@0 185 /* render the curve into bins, then pull values back into curve.
nuclear@0 186 The point is that any inherent subsampling aliasing results in
nuclear@0 187 a safe minimum */
nuclear@0 188 for(k=lo_curve;k<=hi_curve;k++){
nuclear@0 189 int l=0;
nuclear@0 190
nuclear@0 191 for(j=0;j<EHMER_MAX;j++){
nuclear@0 192 int lo_bin= fromOC(j*.125+k*.5-2.0625)/binHz;
nuclear@0 193 int hi_bin= fromOC(j*.125+k*.5-1.9375)/binHz+1;
nuclear@0 194
nuclear@0 195 if(lo_bin<0)lo_bin=0;
nuclear@0 196 if(lo_bin>n)lo_bin=n;
nuclear@0 197 if(lo_bin<l)l=lo_bin;
nuclear@0 198 if(hi_bin<0)hi_bin=0;
nuclear@0 199 if(hi_bin>n)hi_bin=n;
nuclear@0 200
nuclear@0 201 for(;l<hi_bin && l<n;l++)
nuclear@0 202 if(brute_buffer[l]>workc[k][m][j])
nuclear@0 203 brute_buffer[l]=workc[k][m][j];
nuclear@0 204 }
nuclear@0 205
nuclear@0 206 for(;l<n;l++)
nuclear@0 207 if(brute_buffer[l]>workc[k][m][EHMER_MAX-1])
nuclear@0 208 brute_buffer[l]=workc[k][m][EHMER_MAX-1];
nuclear@0 209
nuclear@0 210 }
nuclear@0 211
nuclear@0 212 /* be equally paranoid about being valid up to next half ocatve */
nuclear@0 213 if(i+1<P_BANDS){
nuclear@0 214 int l=0;
nuclear@0 215 k=i+1;
nuclear@0 216 for(j=0;j<EHMER_MAX;j++){
nuclear@0 217 int lo_bin= fromOC(j*.125+i*.5-2.0625)/binHz;
nuclear@0 218 int hi_bin= fromOC(j*.125+i*.5-1.9375)/binHz+1;
nuclear@0 219
nuclear@0 220 if(lo_bin<0)lo_bin=0;
nuclear@0 221 if(lo_bin>n)lo_bin=n;
nuclear@0 222 if(lo_bin<l)l=lo_bin;
nuclear@0 223 if(hi_bin<0)hi_bin=0;
nuclear@0 224 if(hi_bin>n)hi_bin=n;
nuclear@0 225
nuclear@0 226 for(;l<hi_bin && l<n;l++)
nuclear@0 227 if(brute_buffer[l]>workc[k][m][j])
nuclear@0 228 brute_buffer[l]=workc[k][m][j];
nuclear@0 229 }
nuclear@0 230
nuclear@0 231 for(;l<n;l++)
nuclear@0 232 if(brute_buffer[l]>workc[k][m][EHMER_MAX-1])
nuclear@0 233 brute_buffer[l]=workc[k][m][EHMER_MAX-1];
nuclear@0 234
nuclear@0 235 }
nuclear@0 236
nuclear@0 237
nuclear@0 238 for(j=0;j<EHMER_MAX;j++){
nuclear@0 239 int bin=fromOC(j*.125+i*.5-2.)/binHz;
nuclear@0 240 if(bin<0){
nuclear@0 241 ret[i][m][j+2]=-999.;
nuclear@0 242 }else{
nuclear@0 243 if(bin>=n){
nuclear@0 244 ret[i][m][j+2]=-999.;
nuclear@0 245 }else{
nuclear@0 246 ret[i][m][j+2]=brute_buffer[bin];
nuclear@0 247 }
nuclear@0 248 }
nuclear@0 249 }
nuclear@0 250
nuclear@0 251 /* add fenceposts */
nuclear@0 252 for(j=0;j<EHMER_OFFSET;j++)
nuclear@0 253 if(ret[i][m][j+2]>-200.f)break;
nuclear@0 254 ret[i][m][0]=j;
nuclear@0 255
nuclear@0 256 for(j=EHMER_MAX-1;j>EHMER_OFFSET+1;j--)
nuclear@0 257 if(ret[i][m][j+2]>-200.f)
nuclear@0 258 break;
nuclear@0 259 ret[i][m][1]=j;
nuclear@0 260
nuclear@0 261 }
nuclear@0 262 }
nuclear@0 263
nuclear@0 264 return(ret);
nuclear@0 265 }
nuclear@0 266
nuclear@0 267 void _vp_psy_init(vorbis_look_psy *p,vorbis_info_psy *vi,
nuclear@0 268 vorbis_info_psy_global *gi,int n,long rate){
nuclear@0 269 long i,j,lo=-99,hi=1;
nuclear@0 270 long maxoc;
nuclear@0 271 memset(p,0,sizeof(*p));
nuclear@0 272
nuclear@0 273 p->eighth_octave_lines=gi->eighth_octave_lines;
nuclear@0 274 p->shiftoc=rint(log(gi->eighth_octave_lines*8.f)/log(2.f))-1;
nuclear@0 275
nuclear@0 276 p->firstoc=toOC(.25f*rate*.5/n)*(1<<(p->shiftoc+1))-gi->eighth_octave_lines;
nuclear@0 277 maxoc=toOC((n+.25f)*rate*.5/n)*(1<<(p->shiftoc+1))+.5f;
nuclear@0 278 p->total_octave_lines=maxoc-p->firstoc+1;
nuclear@0 279 p->ath=_ogg_malloc(n*sizeof(*p->ath));
nuclear@0 280
nuclear@0 281 p->octave=_ogg_malloc(n*sizeof(*p->octave));
nuclear@0 282 p->bark=_ogg_malloc(n*sizeof(*p->bark));
nuclear@0 283 p->vi=vi;
nuclear@0 284 p->n=n;
nuclear@0 285 p->rate=rate;
nuclear@0 286
nuclear@0 287 /* AoTuV HF weighting */
nuclear@0 288 p->m_val = 1.;
nuclear@0 289 if(rate < 26000) p->m_val = 0;
nuclear@0 290 else if(rate < 38000) p->m_val = .94; /* 32kHz */
nuclear@0 291 else if(rate > 46000) p->m_val = 1.275; /* 48kHz */
nuclear@0 292
nuclear@0 293 /* set up the lookups for a given blocksize and sample rate */
nuclear@0 294
nuclear@0 295 for(i=0,j=0;i<MAX_ATH-1;i++){
nuclear@0 296 int endpos=rint(fromOC((i+1)*.125-2.)*2*n/rate);
nuclear@0 297 float base=ATH[i];
nuclear@0 298 if(j<endpos){
nuclear@0 299 float delta=(ATH[i+1]-base)/(endpos-j);
nuclear@0 300 for(;j<endpos && j<n;j++){
nuclear@0 301 p->ath[j]=base+100.;
nuclear@0 302 base+=delta;
nuclear@0 303 }
nuclear@0 304 }
nuclear@0 305 }
nuclear@0 306
nuclear@0 307 for(;j<n;j++){
nuclear@0 308 p->ath[j]=p->ath[j-1];
nuclear@0 309 }
nuclear@0 310
nuclear@0 311 for(i=0;i<n;i++){
nuclear@0 312 float bark=toBARK(rate/(2*n)*i);
nuclear@0 313
nuclear@0 314 for(;lo+vi->noisewindowlomin<i &&
nuclear@0 315 toBARK(rate/(2*n)*lo)<(bark-vi->noisewindowlo);lo++);
nuclear@0 316
nuclear@0 317 for(;hi<=n && (hi<i+vi->noisewindowhimin ||
nuclear@0 318 toBARK(rate/(2*n)*hi)<(bark+vi->noisewindowhi));hi++);
nuclear@0 319
nuclear@0 320 p->bark[i]=((lo-1)<<16)+(hi-1);
nuclear@0 321
nuclear@0 322 }
nuclear@0 323
nuclear@0 324 for(i=0;i<n;i++)
nuclear@0 325 p->octave[i]=toOC((i+.25f)*.5*rate/n)*(1<<(p->shiftoc+1))+.5f;
nuclear@0 326
nuclear@0 327 p->tonecurves=setup_tone_curves(vi->toneatt,rate*.5/n,n,
nuclear@0 328 vi->tone_centerboost,vi->tone_decay);
nuclear@0 329
nuclear@0 330 /* set up rolling noise median */
nuclear@0 331 p->noiseoffset=_ogg_malloc(P_NOISECURVES*sizeof(*p->noiseoffset));
nuclear@0 332 for(i=0;i<P_NOISECURVES;i++)
nuclear@0 333 p->noiseoffset[i]=_ogg_malloc(n*sizeof(**p->noiseoffset));
nuclear@0 334
nuclear@0 335 for(i=0;i<n;i++){
nuclear@0 336 float halfoc=toOC((i+.5)*rate/(2.*n))*2.;
nuclear@0 337 int inthalfoc;
nuclear@0 338 float del;
nuclear@0 339
nuclear@0 340 if(halfoc<0)halfoc=0;
nuclear@0 341 if(halfoc>=P_BANDS-1)halfoc=P_BANDS-1;
nuclear@0 342 inthalfoc=(int)halfoc;
nuclear@0 343 del=halfoc-inthalfoc;
nuclear@0 344
nuclear@0 345 for(j=0;j<P_NOISECURVES;j++)
nuclear@0 346 p->noiseoffset[j][i]=
nuclear@0 347 p->vi->noiseoff[j][inthalfoc]*(1.-del) +
nuclear@0 348 p->vi->noiseoff[j][inthalfoc+1]*del;
nuclear@0 349
nuclear@0 350 }
nuclear@0 351 #if 0
nuclear@0 352 {
nuclear@0 353 static int ls=0;
nuclear@0 354 _analysis_output_always("noiseoff0",ls,p->noiseoffset[0],n,1,0,0);
nuclear@0 355 _analysis_output_always("noiseoff1",ls,p->noiseoffset[1],n,1,0,0);
nuclear@0 356 _analysis_output_always("noiseoff2",ls++,p->noiseoffset[2],n,1,0,0);
nuclear@0 357 }
nuclear@0 358 #endif
nuclear@0 359 }
nuclear@0 360
nuclear@0 361 void _vp_psy_clear(vorbis_look_psy *p){
nuclear@0 362 int i,j;
nuclear@0 363 if(p){
nuclear@0 364 if(p->ath)_ogg_free(p->ath);
nuclear@0 365 if(p->octave)_ogg_free(p->octave);
nuclear@0 366 if(p->bark)_ogg_free(p->bark);
nuclear@0 367 if(p->tonecurves){
nuclear@0 368 for(i=0;i<P_BANDS;i++){
nuclear@0 369 for(j=0;j<P_LEVELS;j++){
nuclear@0 370 _ogg_free(p->tonecurves[i][j]);
nuclear@0 371 }
nuclear@0 372 _ogg_free(p->tonecurves[i]);
nuclear@0 373 }
nuclear@0 374 _ogg_free(p->tonecurves);
nuclear@0 375 }
nuclear@0 376 if(p->noiseoffset){
nuclear@0 377 for(i=0;i<P_NOISECURVES;i++){
nuclear@0 378 _ogg_free(p->noiseoffset[i]);
nuclear@0 379 }
nuclear@0 380 _ogg_free(p->noiseoffset);
nuclear@0 381 }
nuclear@0 382 memset(p,0,sizeof(*p));
nuclear@0 383 }
nuclear@0 384 }
nuclear@0 385
nuclear@0 386 /* octave/(8*eighth_octave_lines) x scale and dB y scale */
nuclear@0 387 static void seed_curve(float *seed,
nuclear@0 388 const float **curves,
nuclear@0 389 float amp,
nuclear@0 390 int oc, int n,
nuclear@0 391 int linesper,float dBoffset){
nuclear@0 392 int i,post1;
nuclear@0 393 int seedptr;
nuclear@0 394 const float *posts,*curve;
nuclear@0 395
nuclear@0 396 int choice=(int)((amp+dBoffset-P_LEVEL_0)*.1f);
nuclear@0 397 choice=max(choice,0);
nuclear@0 398 choice=min(choice,P_LEVELS-1);
nuclear@0 399 posts=curves[choice];
nuclear@0 400 curve=posts+2;
nuclear@0 401 post1=(int)posts[1];
nuclear@0 402 seedptr=oc+(posts[0]-EHMER_OFFSET)*linesper-(linesper>>1);
nuclear@0 403
nuclear@0 404 for(i=posts[0];i<post1;i++){
nuclear@0 405 if(seedptr>0){
nuclear@0 406 float lin=amp+curve[i];
nuclear@0 407 if(seed[seedptr]<lin)seed[seedptr]=lin;
nuclear@0 408 }
nuclear@0 409 seedptr+=linesper;
nuclear@0 410 if(seedptr>=n)break;
nuclear@0 411 }
nuclear@0 412 }
nuclear@0 413
nuclear@0 414 static void seed_loop(vorbis_look_psy *p,
nuclear@0 415 const float ***curves,
nuclear@0 416 const float *f,
nuclear@0 417 const float *flr,
nuclear@0 418 float *seed,
nuclear@0 419 float specmax){
nuclear@0 420 vorbis_info_psy *vi=p->vi;
nuclear@0 421 long n=p->n,i;
nuclear@0 422 float dBoffset=vi->max_curve_dB-specmax;
nuclear@0 423
nuclear@0 424 /* prime the working vector with peak values */
nuclear@0 425
nuclear@0 426 for(i=0;i<n;i++){
nuclear@0 427 float max=f[i];
nuclear@0 428 long oc=p->octave[i];
nuclear@0 429 while(i+1<n && p->octave[i+1]==oc){
nuclear@0 430 i++;
nuclear@0 431 if(f[i]>max)max=f[i];
nuclear@0 432 }
nuclear@0 433
nuclear@0 434 if(max+6.f>flr[i]){
nuclear@0 435 oc=oc>>p->shiftoc;
nuclear@0 436
nuclear@0 437 if(oc>=P_BANDS)oc=P_BANDS-1;
nuclear@0 438 if(oc<0)oc=0;
nuclear@0 439
nuclear@0 440 seed_curve(seed,
nuclear@0 441 curves[oc],
nuclear@0 442 max,
nuclear@0 443 p->octave[i]-p->firstoc,
nuclear@0 444 p->total_octave_lines,
nuclear@0 445 p->eighth_octave_lines,
nuclear@0 446 dBoffset);
nuclear@0 447 }
nuclear@0 448 }
nuclear@0 449 }
nuclear@0 450
nuclear@0 451 static void seed_chase(float *seeds, int linesper, long n){
nuclear@0 452 long *posstack=alloca(n*sizeof(*posstack));
nuclear@0 453 float *ampstack=alloca(n*sizeof(*ampstack));
nuclear@0 454 long stack=0;
nuclear@0 455 long pos=0;
nuclear@0 456 long i;
nuclear@0 457
nuclear@0 458 for(i=0;i<n;i++){
nuclear@0 459 if(stack<2){
nuclear@0 460 posstack[stack]=i;
nuclear@0 461 ampstack[stack++]=seeds[i];
nuclear@0 462 }else{
nuclear@0 463 while(1){
nuclear@0 464 if(seeds[i]<ampstack[stack-1]){
nuclear@0 465 posstack[stack]=i;
nuclear@0 466 ampstack[stack++]=seeds[i];
nuclear@0 467 break;
nuclear@0 468 }else{
nuclear@0 469 if(i<posstack[stack-1]+linesper){
nuclear@0 470 if(stack>1 && ampstack[stack-1]<=ampstack[stack-2] &&
nuclear@0 471 i<posstack[stack-2]+linesper){
nuclear@0 472 /* we completely overlap, making stack-1 irrelevant. pop it */
nuclear@0 473 stack--;
nuclear@0 474 continue;
nuclear@0 475 }
nuclear@0 476 }
nuclear@0 477 posstack[stack]=i;
nuclear@0 478 ampstack[stack++]=seeds[i];
nuclear@0 479 break;
nuclear@0 480
nuclear@0 481 }
nuclear@0 482 }
nuclear@0 483 }
nuclear@0 484 }
nuclear@0 485
nuclear@0 486 /* the stack now contains only the positions that are relevant. Scan
nuclear@0 487 'em straight through */
nuclear@0 488
nuclear@0 489 for(i=0;i<stack;i++){
nuclear@0 490 long endpos;
nuclear@0 491 if(i<stack-1 && ampstack[i+1]>ampstack[i]){
nuclear@0 492 endpos=posstack[i+1];
nuclear@0 493 }else{
nuclear@0 494 endpos=posstack[i]+linesper+1; /* +1 is important, else bin 0 is
nuclear@0 495 discarded in short frames */
nuclear@0 496 }
nuclear@0 497 if(endpos>n)endpos=n;
nuclear@0 498 for(;pos<endpos;pos++)
nuclear@0 499 seeds[pos]=ampstack[i];
nuclear@0 500 }
nuclear@0 501
nuclear@0 502 /* there. Linear time. I now remember this was on a problem set I
nuclear@0 503 had in Grad Skool... I didn't solve it at the time ;-) */
nuclear@0 504
nuclear@0 505 }
nuclear@0 506
nuclear@0 507 /* bleaugh, this is more complicated than it needs to be */
nuclear@0 508 #include<stdio.h>
nuclear@0 509 static void max_seeds(vorbis_look_psy *p,
nuclear@0 510 float *seed,
nuclear@0 511 float *flr){
nuclear@0 512 long n=p->total_octave_lines;
nuclear@0 513 int linesper=p->eighth_octave_lines;
nuclear@0 514 long linpos=0;
nuclear@0 515 long pos;
nuclear@0 516
nuclear@0 517 seed_chase(seed,linesper,n); /* for masking */
nuclear@0 518
nuclear@0 519 pos=p->octave[0]-p->firstoc-(linesper>>1);
nuclear@0 520
nuclear@0 521 while(linpos+1<p->n){
nuclear@0 522 float minV=seed[pos];
nuclear@0 523 long end=((p->octave[linpos]+p->octave[linpos+1])>>1)-p->firstoc;
nuclear@0 524 if(minV>p->vi->tone_abs_limit)minV=p->vi->tone_abs_limit;
nuclear@0 525 while(pos+1<=end){
nuclear@0 526 pos++;
nuclear@0 527 if((seed[pos]>NEGINF && seed[pos]<minV) || minV==NEGINF)
nuclear@0 528 minV=seed[pos];
nuclear@0 529 }
nuclear@0 530
nuclear@0 531 end=pos+p->firstoc;
nuclear@0 532 for(;linpos<p->n && p->octave[linpos]<=end;linpos++)
nuclear@0 533 if(flr[linpos]<minV)flr[linpos]=minV;
nuclear@0 534 }
nuclear@0 535
nuclear@0 536 {
nuclear@0 537 float minV=seed[p->total_octave_lines-1];
nuclear@0 538 for(;linpos<p->n;linpos++)
nuclear@0 539 if(flr[linpos]<minV)flr[linpos]=minV;
nuclear@0 540 }
nuclear@0 541
nuclear@0 542 }
nuclear@0 543
nuclear@0 544 static void bark_noise_hybridmp(int n,const long *b,
nuclear@0 545 const float *f,
nuclear@0 546 float *noise,
nuclear@0 547 const float offset,
nuclear@0 548 const int fixed){
nuclear@0 549
nuclear@0 550 float *N=alloca(n*sizeof(*N));
nuclear@0 551 float *X=alloca(n*sizeof(*N));
nuclear@0 552 float *XX=alloca(n*sizeof(*N));
nuclear@0 553 float *Y=alloca(n*sizeof(*N));
nuclear@0 554 float *XY=alloca(n*sizeof(*N));
nuclear@0 555
nuclear@0 556 float tN, tX, tXX, tY, tXY;
nuclear@0 557 int i;
nuclear@0 558
nuclear@0 559 int lo, hi;
nuclear@0 560 float R=0.f;
nuclear@0 561 float A=0.f;
nuclear@0 562 float B=0.f;
nuclear@0 563 float D=1.f;
nuclear@0 564 float w, x, y;
nuclear@0 565
nuclear@0 566 tN = tX = tXX = tY = tXY = 0.f;
nuclear@0 567
nuclear@0 568 y = f[0] + offset;
nuclear@0 569 if (y < 1.f) y = 1.f;
nuclear@0 570
nuclear@0 571 w = y * y * .5;
nuclear@0 572
nuclear@0 573 tN += w;
nuclear@0 574 tX += w;
nuclear@0 575 tY += w * y;
nuclear@0 576
nuclear@0 577 N[0] = tN;
nuclear@0 578 X[0] = tX;
nuclear@0 579 XX[0] = tXX;
nuclear@0 580 Y[0] = tY;
nuclear@0 581 XY[0] = tXY;
nuclear@0 582
nuclear@0 583 for (i = 1, x = 1.f; i < n; i++, x += 1.f) {
nuclear@0 584
nuclear@0 585 y = f[i] + offset;
nuclear@0 586 if (y < 1.f) y = 1.f;
nuclear@0 587
nuclear@0 588 w = y * y;
nuclear@0 589
nuclear@0 590 tN += w;
nuclear@0 591 tX += w * x;
nuclear@0 592 tXX += w * x * x;
nuclear@0 593 tY += w * y;
nuclear@0 594 tXY += w * x * y;
nuclear@0 595
nuclear@0 596 N[i] = tN;
nuclear@0 597 X[i] = tX;
nuclear@0 598 XX[i] = tXX;
nuclear@0 599 Y[i] = tY;
nuclear@0 600 XY[i] = tXY;
nuclear@0 601 }
nuclear@0 602
nuclear@0 603 for (i = 0, x = 0.f;; i++, x += 1.f) {
nuclear@0 604
nuclear@0 605 lo = b[i] >> 16;
nuclear@0 606 if( lo>=0 ) break;
nuclear@0 607 hi = b[i] & 0xffff;
nuclear@0 608
nuclear@0 609 tN = N[hi] + N[-lo];
nuclear@0 610 tX = X[hi] - X[-lo];
nuclear@0 611 tXX = XX[hi] + XX[-lo];
nuclear@0 612 tY = Y[hi] + Y[-lo];
nuclear@0 613 tXY = XY[hi] - XY[-lo];
nuclear@0 614
nuclear@0 615 A = tY * tXX - tX * tXY;
nuclear@0 616 B = tN * tXY - tX * tY;
nuclear@0 617 D = tN * tXX - tX * tX;
nuclear@0 618 R = (A + x * B) / D;
nuclear@0 619 if (R < 0.f)
nuclear@0 620 R = 0.f;
nuclear@0 621
nuclear@0 622 noise[i] = R - offset;
nuclear@0 623 }
nuclear@0 624
nuclear@0 625 for ( ;; i++, x += 1.f) {
nuclear@0 626
nuclear@0 627 lo = b[i] >> 16;
nuclear@0 628 hi = b[i] & 0xffff;
nuclear@0 629 if(hi>=n)break;
nuclear@0 630
nuclear@0 631 tN = N[hi] - N[lo];
nuclear@0 632 tX = X[hi] - X[lo];
nuclear@0 633 tXX = XX[hi] - XX[lo];
nuclear@0 634 tY = Y[hi] - Y[lo];
nuclear@0 635 tXY = XY[hi] - XY[lo];
nuclear@0 636
nuclear@0 637 A = tY * tXX - tX * tXY;
nuclear@0 638 B = tN * tXY - tX * tY;
nuclear@0 639 D = tN * tXX - tX * tX;
nuclear@0 640 R = (A + x * B) / D;
nuclear@0 641 if (R < 0.f) R = 0.f;
nuclear@0 642
nuclear@0 643 noise[i] = R - offset;
nuclear@0 644 }
nuclear@0 645 for ( ; i < n; i++, x += 1.f) {
nuclear@0 646
nuclear@0 647 R = (A + x * B) / D;
nuclear@0 648 if (R < 0.f) R = 0.f;
nuclear@0 649
nuclear@0 650 noise[i] = R - offset;
nuclear@0 651 }
nuclear@0 652
nuclear@0 653 if (fixed <= 0) return;
nuclear@0 654
nuclear@0 655 for (i = 0, x = 0.f;; i++, x += 1.f) {
nuclear@0 656 hi = i + fixed / 2;
nuclear@0 657 lo = hi - fixed;
nuclear@0 658 if(lo>=0)break;
nuclear@0 659
nuclear@0 660 tN = N[hi] + N[-lo];
nuclear@0 661 tX = X[hi] - X[-lo];
nuclear@0 662 tXX = XX[hi] + XX[-lo];
nuclear@0 663 tY = Y[hi] + Y[-lo];
nuclear@0 664 tXY = XY[hi] - XY[-lo];
nuclear@0 665
nuclear@0 666
nuclear@0 667 A = tY * tXX - tX * tXY;
nuclear@0 668 B = tN * tXY - tX * tY;
nuclear@0 669 D = tN * tXX - tX * tX;
nuclear@0 670 R = (A + x * B) / D;
nuclear@0 671
nuclear@0 672 if (R - offset < noise[i]) noise[i] = R - offset;
nuclear@0 673 }
nuclear@0 674 for ( ;; i++, x += 1.f) {
nuclear@0 675
nuclear@0 676 hi = i + fixed / 2;
nuclear@0 677 lo = hi - fixed;
nuclear@0 678 if(hi>=n)break;
nuclear@0 679
nuclear@0 680 tN = N[hi] - N[lo];
nuclear@0 681 tX = X[hi] - X[lo];
nuclear@0 682 tXX = XX[hi] - XX[lo];
nuclear@0 683 tY = Y[hi] - Y[lo];
nuclear@0 684 tXY = XY[hi] - XY[lo];
nuclear@0 685
nuclear@0 686 A = tY * tXX - tX * tXY;
nuclear@0 687 B = tN * tXY - tX * tY;
nuclear@0 688 D = tN * tXX - tX * tX;
nuclear@0 689 R = (A + x * B) / D;
nuclear@0 690
nuclear@0 691 if (R - offset < noise[i]) noise[i] = R - offset;
nuclear@0 692 }
nuclear@0 693 for ( ; i < n; i++, x += 1.f) {
nuclear@0 694 R = (A + x * B) / D;
nuclear@0 695 if (R - offset < noise[i]) noise[i] = R - offset;
nuclear@0 696 }
nuclear@0 697 }
nuclear@0 698
nuclear@0 699 void _vp_noisemask(vorbis_look_psy *p,
nuclear@0 700 float *logmdct,
nuclear@0 701 float *logmask){
nuclear@0 702
nuclear@0 703 int i,n=p->n;
nuclear@0 704 float *work=alloca(n*sizeof(*work));
nuclear@0 705
nuclear@0 706 bark_noise_hybridmp(n,p->bark,logmdct,logmask,
nuclear@0 707 140.,-1);
nuclear@0 708
nuclear@0 709 for(i=0;i<n;i++)work[i]=logmdct[i]-logmask[i];
nuclear@0 710
nuclear@0 711 bark_noise_hybridmp(n,p->bark,work,logmask,0.,
nuclear@0 712 p->vi->noisewindowfixed);
nuclear@0 713
nuclear@0 714 for(i=0;i<n;i++)work[i]=logmdct[i]-work[i];
nuclear@0 715
nuclear@0 716 #if 0
nuclear@0 717 {
nuclear@0 718 static int seq=0;
nuclear@0 719
nuclear@0 720 float work2[n];
nuclear@0 721 for(i=0;i<n;i++){
nuclear@0 722 work2[i]=logmask[i]+work[i];
nuclear@0 723 }
nuclear@0 724
nuclear@0 725 if(seq&1)
nuclear@0 726 _analysis_output("median2R",seq/2,work,n,1,0,0);
nuclear@0 727 else
nuclear@0 728 _analysis_output("median2L",seq/2,work,n,1,0,0);
nuclear@0 729
nuclear@0 730 if(seq&1)
nuclear@0 731 _analysis_output("envelope2R",seq/2,work2,n,1,0,0);
nuclear@0 732 else
nuclear@0 733 _analysis_output("envelope2L",seq/2,work2,n,1,0,0);
nuclear@0 734 seq++;
nuclear@0 735 }
nuclear@0 736 #endif
nuclear@0 737
nuclear@0 738 for(i=0;i<n;i++){
nuclear@0 739 int dB=logmask[i]+.5;
nuclear@0 740 if(dB>=NOISE_COMPAND_LEVELS)dB=NOISE_COMPAND_LEVELS-1;
nuclear@0 741 if(dB<0)dB=0;
nuclear@0 742 logmask[i]= work[i]+p->vi->noisecompand[dB];
nuclear@0 743 }
nuclear@0 744
nuclear@0 745 }
nuclear@0 746
nuclear@0 747 void _vp_tonemask(vorbis_look_psy *p,
nuclear@0 748 float *logfft,
nuclear@0 749 float *logmask,
nuclear@0 750 float global_specmax,
nuclear@0 751 float local_specmax){
nuclear@0 752
nuclear@0 753 int i,n=p->n;
nuclear@0 754
nuclear@0 755 float *seed=alloca(sizeof(*seed)*p->total_octave_lines);
nuclear@0 756 float att=local_specmax+p->vi->ath_adjatt;
nuclear@0 757 for(i=0;i<p->total_octave_lines;i++)seed[i]=NEGINF;
nuclear@0 758
nuclear@0 759 /* set the ATH (floating below localmax, not global max by a
nuclear@0 760 specified att) */
nuclear@0 761 if(att<p->vi->ath_maxatt)att=p->vi->ath_maxatt;
nuclear@0 762
nuclear@0 763 for(i=0;i<n;i++)
nuclear@0 764 logmask[i]=p->ath[i]+att;
nuclear@0 765
nuclear@0 766 /* tone masking */
nuclear@0 767 seed_loop(p,(const float ***)p->tonecurves,logfft,logmask,seed,global_specmax);
nuclear@0 768 max_seeds(p,seed,logmask);
nuclear@0 769
nuclear@0 770 }
nuclear@0 771
nuclear@0 772 void _vp_offset_and_mix(vorbis_look_psy *p,
nuclear@0 773 float *noise,
nuclear@0 774 float *tone,
nuclear@0 775 int offset_select,
nuclear@0 776 float *logmask,
nuclear@0 777 float *mdct,
nuclear@0 778 float *logmdct){
nuclear@0 779 int i,n=p->n;
nuclear@0 780 float de, coeffi, cx;/* AoTuV */
nuclear@0 781 float toneatt=p->vi->tone_masteratt[offset_select];
nuclear@0 782
nuclear@0 783 cx = p->m_val;
nuclear@0 784
nuclear@0 785 for(i=0;i<n;i++){
nuclear@0 786 float val= noise[i]+p->noiseoffset[offset_select][i];
nuclear@0 787 if(val>p->vi->noisemaxsupp)val=p->vi->noisemaxsupp;
nuclear@0 788 logmask[i]=max(val,tone[i]+toneatt);
nuclear@0 789
nuclear@0 790
nuclear@0 791 /* AoTuV */
nuclear@0 792 /** @ M1 **
nuclear@0 793 The following codes improve a noise problem.
nuclear@0 794 A fundamental idea uses the value of masking and carries out
nuclear@0 795 the relative compensation of the MDCT.
nuclear@0 796 However, this code is not perfect and all noise problems cannot be solved.
nuclear@0 797 by Aoyumi @ 2004/04/18
nuclear@0 798 */
nuclear@0 799
nuclear@0 800 if(offset_select == 1) {
nuclear@0 801 coeffi = -17.2; /* coeffi is a -17.2dB threshold */
nuclear@0 802 val = val - logmdct[i]; /* val == mdct line value relative to floor in dB */
nuclear@0 803
nuclear@0 804 if(val > coeffi){
nuclear@0 805 /* mdct value is > -17.2 dB below floor */
nuclear@0 806
nuclear@0 807 de = 1.0-((val-coeffi)*0.005*cx);
nuclear@0 808 /* pro-rated attenuation:
nuclear@0 809 -0.00 dB boost if mdct value is -17.2dB (relative to floor)
nuclear@0 810 -0.77 dB boost if mdct value is 0dB (relative to floor)
nuclear@0 811 -1.64 dB boost if mdct value is +17.2dB (relative to floor)
nuclear@0 812 etc... */
nuclear@0 813
nuclear@0 814 if(de < 0) de = 0.0001;
nuclear@0 815 }else
nuclear@0 816 /* mdct value is <= -17.2 dB below floor */
nuclear@0 817
nuclear@0 818 de = 1.0-((val-coeffi)*0.0003*cx);
nuclear@0 819 /* pro-rated attenuation:
nuclear@0 820 +0.00 dB atten if mdct value is -17.2dB (relative to floor)
nuclear@0 821 +0.45 dB atten if mdct value is -34.4dB (relative to floor)
nuclear@0 822 etc... */
nuclear@0 823
nuclear@0 824 mdct[i] *= de;
nuclear@0 825
nuclear@0 826 }
nuclear@0 827 }
nuclear@0 828 }
nuclear@0 829
nuclear@0 830 float _vp_ampmax_decay(float amp,vorbis_dsp_state *vd){
nuclear@0 831 vorbis_info *vi=vd->vi;
nuclear@0 832 codec_setup_info *ci=vi->codec_setup;
nuclear@0 833 vorbis_info_psy_global *gi=&ci->psy_g_param;
nuclear@0 834
nuclear@0 835 int n=ci->blocksizes[vd->W]/2;
nuclear@0 836 float secs=(float)n/vi->rate;
nuclear@0 837
nuclear@0 838 amp+=secs*gi->ampmax_att_per_sec;
nuclear@0 839 if(amp<-9999)amp=-9999;
nuclear@0 840 return(amp);
nuclear@0 841 }
nuclear@0 842
nuclear@0 843 static float FLOOR1_fromdB_LOOKUP[256]={
nuclear@0 844 1.0649863e-07F, 1.1341951e-07F, 1.2079015e-07F, 1.2863978e-07F,
nuclear@0 845 1.3699951e-07F, 1.4590251e-07F, 1.5538408e-07F, 1.6548181e-07F,
nuclear@0 846 1.7623575e-07F, 1.8768855e-07F, 1.9988561e-07F, 2.128753e-07F,
nuclear@0 847 2.2670913e-07F, 2.4144197e-07F, 2.5713223e-07F, 2.7384213e-07F,
nuclear@0 848 2.9163793e-07F, 3.1059021e-07F, 3.3077411e-07F, 3.5226968e-07F,
nuclear@0 849 3.7516214e-07F, 3.9954229e-07F, 4.2550680e-07F, 4.5315863e-07F,
nuclear@0 850 4.8260743e-07F, 5.1396998e-07F, 5.4737065e-07F, 5.8294187e-07F,
nuclear@0 851 6.2082472e-07F, 6.6116941e-07F, 7.0413592e-07F, 7.4989464e-07F,
nuclear@0 852 7.9862701e-07F, 8.5052630e-07F, 9.0579828e-07F, 9.6466216e-07F,
nuclear@0 853 1.0273513e-06F, 1.0941144e-06F, 1.1652161e-06F, 1.2409384e-06F,
nuclear@0 854 1.3215816e-06F, 1.4074654e-06F, 1.4989305e-06F, 1.5963394e-06F,
nuclear@0 855 1.7000785e-06F, 1.8105592e-06F, 1.9282195e-06F, 2.0535261e-06F,
nuclear@0 856 2.1869758e-06F, 2.3290978e-06F, 2.4804557e-06F, 2.6416497e-06F,
nuclear@0 857 2.8133190e-06F, 2.9961443e-06F, 3.1908506e-06F, 3.3982101e-06F,
nuclear@0 858 3.6190449e-06F, 3.8542308e-06F, 4.1047004e-06F, 4.3714470e-06F,
nuclear@0 859 4.6555282e-06F, 4.9580707e-06F, 5.2802740e-06F, 5.6234160e-06F,
nuclear@0 860 5.9888572e-06F, 6.3780469e-06F, 6.7925283e-06F, 7.2339451e-06F,
nuclear@0 861 7.7040476e-06F, 8.2047000e-06F, 8.7378876e-06F, 9.3057248e-06F,
nuclear@0 862 9.9104632e-06F, 1.0554501e-05F, 1.1240392e-05F, 1.1970856e-05F,
nuclear@0 863 1.2748789e-05F, 1.3577278e-05F, 1.4459606e-05F, 1.5399272e-05F,
nuclear@0 864 1.6400004e-05F, 1.7465768e-05F, 1.8600792e-05F, 1.9809576e-05F,
nuclear@0 865 2.1096914e-05F, 2.2467911e-05F, 2.3928002e-05F, 2.5482978e-05F,
nuclear@0 866 2.7139006e-05F, 2.8902651e-05F, 3.0780908e-05F, 3.2781225e-05F,
nuclear@0 867 3.4911534e-05F, 3.7180282e-05F, 3.9596466e-05F, 4.2169667e-05F,
nuclear@0 868 4.4910090e-05F, 4.7828601e-05F, 5.0936773e-05F, 5.4246931e-05F,
nuclear@0 869 5.7772202e-05F, 6.1526565e-05F, 6.5524908e-05F, 6.9783085e-05F,
nuclear@0 870 7.4317983e-05F, 7.9147585e-05F, 8.4291040e-05F, 8.9768747e-05F,
nuclear@0 871 9.5602426e-05F, 0.00010181521F, 0.00010843174F, 0.00011547824F,
nuclear@0 872 0.00012298267F, 0.00013097477F, 0.00013948625F, 0.00014855085F,
nuclear@0 873 0.00015820453F, 0.00016848555F, 0.00017943469F, 0.00019109536F,
nuclear@0 874 0.00020351382F, 0.00021673929F, 0.00023082423F, 0.00024582449F,
nuclear@0 875 0.00026179955F, 0.00027881276F, 0.00029693158F, 0.00031622787F,
nuclear@0 876 0.00033677814F, 0.00035866388F, 0.00038197188F, 0.00040679456F,
nuclear@0 877 0.00043323036F, 0.00046138411F, 0.00049136745F, 0.00052329927F,
nuclear@0 878 0.00055730621F, 0.00059352311F, 0.00063209358F, 0.00067317058F,
nuclear@0 879 0.00071691700F, 0.00076350630F, 0.00081312324F, 0.00086596457F,
nuclear@0 880 0.00092223983F, 0.00098217216F, 0.0010459992F, 0.0011139742F,
nuclear@0 881 0.0011863665F, 0.0012634633F, 0.0013455702F, 0.0014330129F,
nuclear@0 882 0.0015261382F, 0.0016253153F, 0.0017309374F, 0.0018434235F,
nuclear@0 883 0.0019632195F, 0.0020908006F, 0.0022266726F, 0.0023713743F,
nuclear@0 884 0.0025254795F, 0.0026895994F, 0.0028643847F, 0.0030505286F,
nuclear@0 885 0.0032487691F, 0.0034598925F, 0.0036847358F, 0.0039241906F,
nuclear@0 886 0.0041792066F, 0.0044507950F, 0.0047400328F, 0.0050480668F,
nuclear@0 887 0.0053761186F, 0.0057254891F, 0.0060975636F, 0.0064938176F,
nuclear@0 888 0.0069158225F, 0.0073652516F, 0.0078438871F, 0.0083536271F,
nuclear@0 889 0.0088964928F, 0.009474637F, 0.010090352F, 0.010746080F,
nuclear@0 890 0.011444421F, 0.012188144F, 0.012980198F, 0.013823725F,
nuclear@0 891 0.014722068F, 0.015678791F, 0.016697687F, 0.017782797F,
nuclear@0 892 0.018938423F, 0.020169149F, 0.021479854F, 0.022875735F,
nuclear@0 893 0.024362330F, 0.025945531F, 0.027631618F, 0.029427276F,
nuclear@0 894 0.031339626F, 0.033376252F, 0.035545228F, 0.037855157F,
nuclear@0 895 0.040315199F, 0.042935108F, 0.045725273F, 0.048696758F,
nuclear@0 896 0.051861348F, 0.055231591F, 0.058820850F, 0.062643361F,
nuclear@0 897 0.066714279F, 0.071049749F, 0.075666962F, 0.080584227F,
nuclear@0 898 0.085821044F, 0.091398179F, 0.097337747F, 0.10366330F,
nuclear@0 899 0.11039993F, 0.11757434F, 0.12521498F, 0.13335215F,
nuclear@0 900 0.14201813F, 0.15124727F, 0.16107617F, 0.17154380F,
nuclear@0 901 0.18269168F, 0.19456402F, 0.20720788F, 0.22067342F,
nuclear@0 902 0.23501402F, 0.25028656F, 0.26655159F, 0.28387361F,
nuclear@0 903 0.30232132F, 0.32196786F, 0.34289114F, 0.36517414F,
nuclear@0 904 0.38890521F, 0.41417847F, 0.44109412F, 0.46975890F,
nuclear@0 905 0.50028648F, 0.53279791F, 0.56742212F, 0.60429640F,
nuclear@0 906 0.64356699F, 0.68538959F, 0.72993007F, 0.77736504F,
nuclear@0 907 0.82788260F, 0.88168307F, 0.9389798F, 1.F,
nuclear@0 908 };
nuclear@0 909
nuclear@0 910 /* this is for per-channel noise normalization */
nuclear@0 911 static int apsort(const void *a, const void *b){
nuclear@0 912 float f1=**(float**)a;
nuclear@0 913 float f2=**(float**)b;
nuclear@0 914 return (f1<f2)-(f1>f2);
nuclear@0 915 }
nuclear@0 916
nuclear@0 917 static void flag_lossless(int limit, float prepoint, float postpoint, float *mdct,
nuclear@0 918 float *floor, int *flag, int i, int jn){
nuclear@0 919 int j;
nuclear@0 920 for(j=0;j<jn;j++){
nuclear@0 921 float point = j>=limit-i ? postpoint : prepoint;
nuclear@0 922 float r = fabs(mdct[j])/floor[j];
nuclear@0 923 if(r<point)
nuclear@0 924 flag[j]=0;
nuclear@0 925 else
nuclear@0 926 flag[j]=1;
nuclear@0 927 }
nuclear@0 928 }
nuclear@0 929
nuclear@0 930 /* Overload/Side effect: On input, the *q vector holds either the
nuclear@0 931 quantized energy (for elements with the flag set) or the absolute
nuclear@0 932 values of the *r vector (for elements with flag unset). On output,
nuclear@0 933 *q holds the quantized energy for all elements */
nuclear@0 934 static float noise_normalize(vorbis_look_psy *p, int limit, float *r, float *q, float *f, int *flags, float acc, int i, int n, int *out){
nuclear@0 935
nuclear@0 936 vorbis_info_psy *vi=p->vi;
nuclear@0 937 float **sort = alloca(n*sizeof(*sort));
nuclear@0 938 int j,count=0;
nuclear@0 939 int start = (vi->normal_p ? vi->normal_start-i : n);
nuclear@0 940 if(start>n)start=n;
nuclear@0 941
nuclear@0 942 /* force classic behavior where only energy in the current band is considered */
nuclear@0 943 acc=0.f;
nuclear@0 944
nuclear@0 945 /* still responsible for populating *out where noise norm not in
nuclear@0 946 effect. There's no need to [re]populate *q in these areas */
nuclear@0 947 for(j=0;j<start;j++){
nuclear@0 948 if(!flags || !flags[j]){ /* lossless coupling already quantized.
nuclear@0 949 Don't touch; requantizing based on
nuclear@0 950 energy would be incorrect. */
nuclear@0 951 float ve = q[j]/f[j];
nuclear@0 952 if(r[j]<0)
nuclear@0 953 out[j] = -rint(sqrt(ve));
nuclear@0 954 else
nuclear@0 955 out[j] = rint(sqrt(ve));
nuclear@0 956 }
nuclear@0 957 }
nuclear@0 958
nuclear@0 959 /* sort magnitudes for noise norm portion of partition */
nuclear@0 960 for(;j<n;j++){
nuclear@0 961 if(!flags || !flags[j]){ /* can't noise norm elements that have
nuclear@0 962 already been loslessly coupled; we can
nuclear@0 963 only account for their energy error */
nuclear@0 964 float ve = q[j]/f[j];
nuclear@0 965 /* Despite all the new, more capable coupling code, for now we
nuclear@0 966 implement noise norm as it has been up to this point. Only
nuclear@0 967 consider promotions to unit magnitude from 0. In addition
nuclear@0 968 the only energy error counted is quantizations to zero. */
nuclear@0 969 /* also-- the original point code only applied noise norm at > pointlimit */
nuclear@0 970 if(ve<.25f && (!flags || j>=limit-i)){
nuclear@0 971 acc += ve;
nuclear@0 972 sort[count++]=q+j; /* q is fabs(r) for unflagged element */
nuclear@0 973 }else{
nuclear@0 974 /* For now: no acc adjustment for nonzero quantization. populate *out and q as this value is final. */
nuclear@0 975 if(r[j]<0)
nuclear@0 976 out[j] = -rint(sqrt(ve));
nuclear@0 977 else
nuclear@0 978 out[j] = rint(sqrt(ve));
nuclear@0 979 q[j] = out[j]*out[j]*f[j];
nuclear@0 980 }
nuclear@0 981 }/* else{
nuclear@0 982 again, no energy adjustment for error in nonzero quant-- for now
nuclear@0 983 }*/
nuclear@0 984 }
nuclear@0 985
nuclear@0 986 if(count){
nuclear@0 987 /* noise norm to do */
nuclear@0 988 qsort(sort,count,sizeof(*sort),apsort);
nuclear@0 989 for(j=0;j<count;j++){
nuclear@0 990 int k=sort[j]-q;
nuclear@0 991 if(acc>=vi->normal_thresh){
nuclear@0 992 out[k]=unitnorm(r[k]);
nuclear@0 993 acc-=1.f;
nuclear@0 994 q[k]=f[k];
nuclear@0 995 }else{
nuclear@0 996 out[k]=0;
nuclear@0 997 q[k]=0.f;
nuclear@0 998 }
nuclear@0 999 }
nuclear@0 1000 }
nuclear@0 1001
nuclear@0 1002 return acc;
nuclear@0 1003 }
nuclear@0 1004
nuclear@0 1005 /* Noise normalization, quantization and coupling are not wholly
nuclear@0 1006 seperable processes in depth>1 coupling. */
nuclear@0 1007 void _vp_couple_quantize_normalize(int blobno,
nuclear@0 1008 vorbis_info_psy_global *g,
nuclear@0 1009 vorbis_look_psy *p,
nuclear@0 1010 vorbis_info_mapping0 *vi,
nuclear@0 1011 float **mdct,
nuclear@0 1012 int **iwork,
nuclear@0 1013 int *nonzero,
nuclear@0 1014 int sliding_lowpass,
nuclear@0 1015 int ch){
nuclear@0 1016
nuclear@0 1017 int i;
nuclear@0 1018 int n = p->n;
nuclear@0 1019 int partition=(p->vi->normal_p ? p->vi->normal_partition : 16);
nuclear@0 1020 int limit = g->coupling_pointlimit[p->vi->blockflag][blobno];
nuclear@0 1021 float prepoint=stereo_threshholds[g->coupling_prepointamp[blobno]];
nuclear@0 1022 float postpoint=stereo_threshholds[g->coupling_postpointamp[blobno]];
nuclear@0 1023 #if 0
nuclear@0 1024 float de=0.1*p->m_val; /* a blend of the AoTuV M2 and M3 code here and below */
nuclear@0 1025 #endif
nuclear@0 1026
nuclear@0 1027 /* mdct is our raw mdct output, floor not removed. */
nuclear@0 1028 /* inout passes in the ifloor, passes back quantized result */
nuclear@0 1029
nuclear@0 1030 /* unquantized energy (negative indicates amplitude has negative sign) */
nuclear@0 1031 float **raw = alloca(ch*sizeof(*raw));
nuclear@0 1032
nuclear@0 1033 /* dual pupose; quantized energy (if flag set), othersize fabs(raw) */
nuclear@0 1034 float **quant = alloca(ch*sizeof(*quant));
nuclear@0 1035
nuclear@0 1036 /* floor energy */
nuclear@0 1037 float **floor = alloca(ch*sizeof(*floor));
nuclear@0 1038
nuclear@0 1039 /* flags indicating raw/quantized status of elements in raw vector */
nuclear@0 1040 int **flag = alloca(ch*sizeof(*flag));
nuclear@0 1041
nuclear@0 1042 /* non-zero flag working vector */
nuclear@0 1043 int *nz = alloca(ch*sizeof(*nz));
nuclear@0 1044
nuclear@0 1045 /* energy surplus/defecit tracking */
nuclear@0 1046 float *acc = alloca((ch+vi->coupling_steps)*sizeof(*acc));
nuclear@0 1047
nuclear@0 1048 /* The threshold of a stereo is changed with the size of n */
nuclear@0 1049 if(n > 1000)
nuclear@0 1050 postpoint=stereo_threshholds_limited[g->coupling_postpointamp[blobno]];
nuclear@0 1051
nuclear@0 1052 raw[0] = alloca(ch*partition*sizeof(**raw));
nuclear@0 1053 quant[0] = alloca(ch*partition*sizeof(**quant));
nuclear@0 1054 floor[0] = alloca(ch*partition*sizeof(**floor));
nuclear@0 1055 flag[0] = alloca(ch*partition*sizeof(**flag));
nuclear@0 1056
nuclear@0 1057 for(i=1;i<ch;i++){
nuclear@0 1058 raw[i] = &raw[0][partition*i];
nuclear@0 1059 quant[i] = &quant[0][partition*i];
nuclear@0 1060 floor[i] = &floor[0][partition*i];
nuclear@0 1061 flag[i] = &flag[0][partition*i];
nuclear@0 1062 }
nuclear@0 1063 for(i=0;i<ch+vi->coupling_steps;i++)
nuclear@0 1064 acc[i]=0.f;
nuclear@0 1065
nuclear@0 1066 for(i=0;i<n;i+=partition){
nuclear@0 1067 int k,j,jn = partition > n-i ? n-i : partition;
nuclear@0 1068 int step,track = 0;
nuclear@0 1069
nuclear@0 1070 memcpy(nz,nonzero,sizeof(*nz)*ch);
nuclear@0 1071
nuclear@0 1072 /* prefill */
nuclear@0 1073 memset(flag[0],0,ch*partition*sizeof(**flag));
nuclear@0 1074 for(k=0;k<ch;k++){
nuclear@0 1075 int *iout = &iwork[k][i];
nuclear@0 1076 if(nz[k]){
nuclear@0 1077
nuclear@0 1078 for(j=0;j<jn;j++)
nuclear@0 1079 floor[k][j] = FLOOR1_fromdB_LOOKUP[iout[j]];
nuclear@0 1080
nuclear@0 1081 flag_lossless(limit,prepoint,postpoint,&mdct[k][i],floor[k],flag[k],i,jn);
nuclear@0 1082
nuclear@0 1083 for(j=0;j<jn;j++){
nuclear@0 1084 quant[k][j] = raw[k][j] = mdct[k][i+j]*mdct[k][i+j];
nuclear@0 1085 if(mdct[k][i+j]<0.f) raw[k][j]*=-1.f;
nuclear@0 1086 floor[k][j]*=floor[k][j];
nuclear@0 1087 }
nuclear@0 1088
nuclear@0 1089 acc[track]=noise_normalize(p,limit,raw[k],quant[k],floor[k],NULL,acc[track],i,jn,iout);
nuclear@0 1090
nuclear@0 1091 }else{
nuclear@0 1092 for(j=0;j<jn;j++){
nuclear@0 1093 floor[k][j] = 1e-10f;
nuclear@0 1094 raw[k][j] = 0.f;
nuclear@0 1095 quant[k][j] = 0.f;
nuclear@0 1096 flag[k][j] = 0;
nuclear@0 1097 iout[j]=0;
nuclear@0 1098 }
nuclear@0 1099 acc[track]=0.f;
nuclear@0 1100 }
nuclear@0 1101 track++;
nuclear@0 1102 }
nuclear@0 1103
nuclear@0 1104 /* coupling */
nuclear@0 1105 for(step=0;step<vi->coupling_steps;step++){
nuclear@0 1106 int Mi = vi->coupling_mag[step];
nuclear@0 1107 int Ai = vi->coupling_ang[step];
nuclear@0 1108 int *iM = &iwork[Mi][i];
nuclear@0 1109 int *iA = &iwork[Ai][i];
nuclear@0 1110 float *reM = raw[Mi];
nuclear@0 1111 float *reA = raw[Ai];
nuclear@0 1112 float *qeM = quant[Mi];
nuclear@0 1113 float *qeA = quant[Ai];
nuclear@0 1114 float *floorM = floor[Mi];
nuclear@0 1115 float *floorA = floor[Ai];
nuclear@0 1116 int *fM = flag[Mi];
nuclear@0 1117 int *fA = flag[Ai];
nuclear@0 1118
nuclear@0 1119 if(nz[Mi] || nz[Ai]){
nuclear@0 1120 nz[Mi] = nz[Ai] = 1;
nuclear@0 1121
nuclear@0 1122 for(j=0;j<jn;j++){
nuclear@0 1123
nuclear@0 1124 if(j<sliding_lowpass-i){
nuclear@0 1125 if(fM[j] || fA[j]){
nuclear@0 1126 /* lossless coupling */
nuclear@0 1127
nuclear@0 1128 reM[j] = fabs(reM[j])+fabs(reA[j]);
nuclear@0 1129 qeM[j] = qeM[j]+qeA[j];
nuclear@0 1130 fM[j]=fA[j]=1;
nuclear@0 1131
nuclear@0 1132 /* couple iM/iA */
nuclear@0 1133 {
nuclear@0 1134 int A = iM[j];
nuclear@0 1135 int B = iA[j];
nuclear@0 1136
nuclear@0 1137 if(abs(A)>abs(B)){
nuclear@0 1138 iA[j]=(A>0?A-B:B-A);
nuclear@0 1139 }else{
nuclear@0 1140 iA[j]=(B>0?A-B:B-A);
nuclear@0 1141 iM[j]=B;
nuclear@0 1142 }
nuclear@0 1143
nuclear@0 1144 /* collapse two equivalent tuples to one */
nuclear@0 1145 if(iA[j]>=abs(iM[j])*2){
nuclear@0 1146 iA[j]= -iA[j];
nuclear@0 1147 iM[j]= -iM[j];
nuclear@0 1148 }
nuclear@0 1149
nuclear@0 1150 }
nuclear@0 1151
nuclear@0 1152 }else{
nuclear@0 1153 /* lossy (point) coupling */
nuclear@0 1154 if(j<limit-i){
nuclear@0 1155 /* dipole */
nuclear@0 1156 reM[j] += reA[j];
nuclear@0 1157 qeM[j] = fabs(reM[j]);
nuclear@0 1158 }else{
nuclear@0 1159 #if 0
nuclear@0 1160 /* AoTuV */
nuclear@0 1161 /** @ M2 **
nuclear@0 1162 The boost problem by the combination of noise normalization and point stereo is eased.
nuclear@0 1163 However, this is a temporary patch.
nuclear@0 1164 by Aoyumi @ 2004/04/18
nuclear@0 1165 */
nuclear@0 1166 float derate = (1.0 - de*((float)(j-limit+i) / (float)(n-limit)));
nuclear@0 1167 /* elliptical */
nuclear@0 1168 if(reM[j]+reA[j]<0){
nuclear@0 1169 reM[j] = - (qeM[j] = (fabs(reM[j])+fabs(reA[j]))*derate*derate);
nuclear@0 1170 }else{
nuclear@0 1171 reM[j] = (qeM[j] = (fabs(reM[j])+fabs(reA[j]))*derate*derate);
nuclear@0 1172 }
nuclear@0 1173 #else
nuclear@0 1174 /* elliptical */
nuclear@0 1175 if(reM[j]+reA[j]<0){
nuclear@0 1176 reM[j] = - (qeM[j] = fabs(reM[j])+fabs(reA[j]));
nuclear@0 1177 }else{
nuclear@0 1178 reM[j] = (qeM[j] = fabs(reM[j])+fabs(reA[j]));
nuclear@0 1179 }
nuclear@0 1180 #endif
nuclear@0 1181
nuclear@0 1182 }
nuclear@0 1183 reA[j]=qeA[j]=0.f;
nuclear@0 1184 fA[j]=1;
nuclear@0 1185 iA[j]=0;
nuclear@0 1186 }
nuclear@0 1187 }
nuclear@0 1188 floorM[j]=floorA[j]=floorM[j]+floorA[j];
nuclear@0 1189 }
nuclear@0 1190 /* normalize the resulting mag vector */
nuclear@0 1191 acc[track]=noise_normalize(p,limit,raw[Mi],quant[Mi],floor[Mi],flag[Mi],acc[track],i,jn,iM);
nuclear@0 1192 track++;
nuclear@0 1193 }
nuclear@0 1194 }
nuclear@0 1195 }
nuclear@0 1196
nuclear@0 1197 for(i=0;i<vi->coupling_steps;i++){
nuclear@0 1198 /* make sure coupling a zero and a nonzero channel results in two
nuclear@0 1199 nonzero channels. */
nuclear@0 1200 if(nonzero[vi->coupling_mag[i]] ||
nuclear@0 1201 nonzero[vi->coupling_ang[i]]){
nuclear@0 1202 nonzero[vi->coupling_mag[i]]=1;
nuclear@0 1203 nonzero[vi->coupling_ang[i]]=1;
nuclear@0 1204 }
nuclear@0 1205 }
nuclear@0 1206 }