dbf-halloween2015

annotate libs/vorbis/psy.c @ 1:c3f5c32cb210

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