vrshoot

diff libs/vorbis/psy.c @ 0:b2f14e535253

initial commit
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 01 Feb 2014 19:58:19 +0200
parents
children
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/libs/vorbis/psy.c	Sat Feb 01 19:58:19 2014 +0200
     1.3 @@ -0,0 +1,1206 @@
     1.4 +/********************************************************************
     1.5 + *                                                                  *
     1.6 + * THIS FILE IS PART OF THE OggVorbis SOFTWARE CODEC SOURCE CODE.   *
     1.7 + * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS     *
     1.8 + * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE *
     1.9 + * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING.       *
    1.10 + *                                                                  *
    1.11 + * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2010             *
    1.12 + * by the Xiph.Org Foundation http://www.xiph.org/                  *
    1.13 + *                                                                  *
    1.14 + ********************************************************************
    1.15 +
    1.16 + function: psychoacoustics not including preecho
    1.17 + last mod: $Id: psy.c 18077 2011-09-02 02:49:00Z giles $
    1.18 +
    1.19 + ********************************************************************/
    1.20 +
    1.21 +#include <stdlib.h>
    1.22 +#include <math.h>
    1.23 +#include <string.h>
    1.24 +#include "vorbis/codec.h"
    1.25 +#include "codec_internal.h"
    1.26 +
    1.27 +#include "masking.h"
    1.28 +#include "psy.h"
    1.29 +#include "os.h"
    1.30 +#include "lpc.h"
    1.31 +#include "smallft.h"
    1.32 +#include "scales.h"
    1.33 +#include "misc.h"
    1.34 +
    1.35 +#define NEGINF -9999.f
    1.36 +static const double stereo_threshholds[]={0.0, .5, 1.0, 1.5, 2.5, 4.5, 8.5, 16.5, 9e10};
    1.37 +static const double stereo_threshholds_limited[]={0.0, .5, 1.0, 1.5, 2.0, 2.5, 4.5, 8.5, 9e10};
    1.38 +
    1.39 +vorbis_look_psy_global *_vp_global_look(vorbis_info *vi){
    1.40 +  codec_setup_info *ci=vi->codec_setup;
    1.41 +  vorbis_info_psy_global *gi=&ci->psy_g_param;
    1.42 +  vorbis_look_psy_global *look=_ogg_calloc(1,sizeof(*look));
    1.43 +
    1.44 +  look->channels=vi->channels;
    1.45 +
    1.46 +  look->ampmax=-9999.;
    1.47 +  look->gi=gi;
    1.48 +  return(look);
    1.49 +}
    1.50 +
    1.51 +void _vp_global_free(vorbis_look_psy_global *look){
    1.52 +  if(look){
    1.53 +    memset(look,0,sizeof(*look));
    1.54 +    _ogg_free(look);
    1.55 +  }
    1.56 +}
    1.57 +
    1.58 +void _vi_gpsy_free(vorbis_info_psy_global *i){
    1.59 +  if(i){
    1.60 +    memset(i,0,sizeof(*i));
    1.61 +    _ogg_free(i);
    1.62 +  }
    1.63 +}
    1.64 +
    1.65 +void _vi_psy_free(vorbis_info_psy *i){
    1.66 +  if(i){
    1.67 +    memset(i,0,sizeof(*i));
    1.68 +    _ogg_free(i);
    1.69 +  }
    1.70 +}
    1.71 +
    1.72 +static void min_curve(float *c,
    1.73 +                       float *c2){
    1.74 +  int i;
    1.75 +  for(i=0;i<EHMER_MAX;i++)if(c2[i]<c[i])c[i]=c2[i];
    1.76 +}
    1.77 +static void max_curve(float *c,
    1.78 +                       float *c2){
    1.79 +  int i;
    1.80 +  for(i=0;i<EHMER_MAX;i++)if(c2[i]>c[i])c[i]=c2[i];
    1.81 +}
    1.82 +
    1.83 +static void attenuate_curve(float *c,float att){
    1.84 +  int i;
    1.85 +  for(i=0;i<EHMER_MAX;i++)
    1.86 +    c[i]+=att;
    1.87 +}
    1.88 +
    1.89 +static float ***setup_tone_curves(float curveatt_dB[P_BANDS],float binHz,int n,
    1.90 +                                  float center_boost, float center_decay_rate){
    1.91 +  int i,j,k,m;
    1.92 +  float ath[EHMER_MAX];
    1.93 +  float workc[P_BANDS][P_LEVELS][EHMER_MAX];
    1.94 +  float athc[P_LEVELS][EHMER_MAX];
    1.95 +  float *brute_buffer=alloca(n*sizeof(*brute_buffer));
    1.96 +
    1.97 +  float ***ret=_ogg_malloc(sizeof(*ret)*P_BANDS);
    1.98 +
    1.99 +  memset(workc,0,sizeof(workc));
   1.100 +
   1.101 +  for(i=0;i<P_BANDS;i++){
   1.102 +    /* we add back in the ATH to avoid low level curves falling off to
   1.103 +       -infinity and unnecessarily cutting off high level curves in the
   1.104 +       curve limiting (last step). */
   1.105 +
   1.106 +    /* A half-band's settings must be valid over the whole band, and
   1.107 +       it's better to mask too little than too much */
   1.108 +    int ath_offset=i*4;
   1.109 +    for(j=0;j<EHMER_MAX;j++){
   1.110 +      float min=999.;
   1.111 +      for(k=0;k<4;k++)
   1.112 +        if(j+k+ath_offset<MAX_ATH){
   1.113 +          if(min>ATH[j+k+ath_offset])min=ATH[j+k+ath_offset];
   1.114 +        }else{
   1.115 +          if(min>ATH[MAX_ATH-1])min=ATH[MAX_ATH-1];
   1.116 +        }
   1.117 +      ath[j]=min;
   1.118 +    }
   1.119 +
   1.120 +    /* copy curves into working space, replicate the 50dB curve to 30
   1.121 +       and 40, replicate the 100dB curve to 110 */
   1.122 +    for(j=0;j<6;j++)
   1.123 +      memcpy(workc[i][j+2],tonemasks[i][j],EHMER_MAX*sizeof(*tonemasks[i][j]));
   1.124 +    memcpy(workc[i][0],tonemasks[i][0],EHMER_MAX*sizeof(*tonemasks[i][0]));
   1.125 +    memcpy(workc[i][1],tonemasks[i][0],EHMER_MAX*sizeof(*tonemasks[i][0]));
   1.126 +
   1.127 +    /* apply centered curve boost/decay */
   1.128 +    for(j=0;j<P_LEVELS;j++){
   1.129 +      for(k=0;k<EHMER_MAX;k++){
   1.130 +        float adj=center_boost+abs(EHMER_OFFSET-k)*center_decay_rate;
   1.131 +        if(adj<0. && center_boost>0)adj=0.;
   1.132 +        if(adj>0. && center_boost<0)adj=0.;
   1.133 +        workc[i][j][k]+=adj;
   1.134 +      }
   1.135 +    }
   1.136 +
   1.137 +    /* normalize curves so the driving amplitude is 0dB */
   1.138 +    /* make temp curves with the ATH overlayed */
   1.139 +    for(j=0;j<P_LEVELS;j++){
   1.140 +      attenuate_curve(workc[i][j],curveatt_dB[i]+100.-(j<2?2:j)*10.-P_LEVEL_0);
   1.141 +      memcpy(athc[j],ath,EHMER_MAX*sizeof(**athc));
   1.142 +      attenuate_curve(athc[j],+100.-j*10.f-P_LEVEL_0);
   1.143 +      max_curve(athc[j],workc[i][j]);
   1.144 +    }
   1.145 +
   1.146 +    /* Now limit the louder curves.
   1.147 +
   1.148 +       the idea is this: We don't know what the playback attenuation
   1.149 +       will be; 0dB SL moves every time the user twiddles the volume
   1.150 +       knob. So that means we have to use a single 'most pessimal' curve
   1.151 +       for all masking amplitudes, right?  Wrong.  The *loudest* sound
   1.152 +       can be in (we assume) a range of ...+100dB] SL.  However, sounds
   1.153 +       20dB down will be in a range ...+80], 40dB down is from ...+60],
   1.154 +       etc... */
   1.155 +
   1.156 +    for(j=1;j<P_LEVELS;j++){
   1.157 +      min_curve(athc[j],athc[j-1]);
   1.158 +      min_curve(workc[i][j],athc[j]);
   1.159 +    }
   1.160 +  }
   1.161 +
   1.162 +  for(i=0;i<P_BANDS;i++){
   1.163 +    int hi_curve,lo_curve,bin;
   1.164 +    ret[i]=_ogg_malloc(sizeof(**ret)*P_LEVELS);
   1.165 +
   1.166 +    /* low frequency curves are measured with greater resolution than
   1.167 +       the MDCT/FFT will actually give us; we want the curve applied
   1.168 +       to the tone data to be pessimistic and thus apply the minimum
   1.169 +       masking possible for a given bin.  That means that a single bin
   1.170 +       could span more than one octave and that the curve will be a
   1.171 +       composite of multiple octaves.  It also may mean that a single
   1.172 +       bin may span > an eighth of an octave and that the eighth
   1.173 +       octave values may also be composited. */
   1.174 +
   1.175 +    /* which octave curves will we be compositing? */
   1.176 +    bin=floor(fromOC(i*.5)/binHz);
   1.177 +    lo_curve=  ceil(toOC(bin*binHz+1)*2);
   1.178 +    hi_curve=  floor(toOC((bin+1)*binHz)*2);
   1.179 +    if(lo_curve>i)lo_curve=i;
   1.180 +    if(lo_curve<0)lo_curve=0;
   1.181 +    if(hi_curve>=P_BANDS)hi_curve=P_BANDS-1;
   1.182 +
   1.183 +    for(m=0;m<P_LEVELS;m++){
   1.184 +      ret[i][m]=_ogg_malloc(sizeof(***ret)*(EHMER_MAX+2));
   1.185 +
   1.186 +      for(j=0;j<n;j++)brute_buffer[j]=999.;
   1.187 +
   1.188 +      /* render the curve into bins, then pull values back into curve.
   1.189 +         The point is that any inherent subsampling aliasing results in
   1.190 +         a safe minimum */
   1.191 +      for(k=lo_curve;k<=hi_curve;k++){
   1.192 +        int l=0;
   1.193 +
   1.194 +        for(j=0;j<EHMER_MAX;j++){
   1.195 +          int lo_bin= fromOC(j*.125+k*.5-2.0625)/binHz;
   1.196 +          int hi_bin= fromOC(j*.125+k*.5-1.9375)/binHz+1;
   1.197 +
   1.198 +          if(lo_bin<0)lo_bin=0;
   1.199 +          if(lo_bin>n)lo_bin=n;
   1.200 +          if(lo_bin<l)l=lo_bin;
   1.201 +          if(hi_bin<0)hi_bin=0;
   1.202 +          if(hi_bin>n)hi_bin=n;
   1.203 +
   1.204 +          for(;l<hi_bin && l<n;l++)
   1.205 +            if(brute_buffer[l]>workc[k][m][j])
   1.206 +              brute_buffer[l]=workc[k][m][j];
   1.207 +        }
   1.208 +
   1.209 +        for(;l<n;l++)
   1.210 +          if(brute_buffer[l]>workc[k][m][EHMER_MAX-1])
   1.211 +            brute_buffer[l]=workc[k][m][EHMER_MAX-1];
   1.212 +
   1.213 +      }
   1.214 +
   1.215 +      /* be equally paranoid about being valid up to next half ocatve */
   1.216 +      if(i+1<P_BANDS){
   1.217 +        int l=0;
   1.218 +        k=i+1;
   1.219 +        for(j=0;j<EHMER_MAX;j++){
   1.220 +          int lo_bin= fromOC(j*.125+i*.5-2.0625)/binHz;
   1.221 +          int hi_bin= fromOC(j*.125+i*.5-1.9375)/binHz+1;
   1.222 +
   1.223 +          if(lo_bin<0)lo_bin=0;
   1.224 +          if(lo_bin>n)lo_bin=n;
   1.225 +          if(lo_bin<l)l=lo_bin;
   1.226 +          if(hi_bin<0)hi_bin=0;
   1.227 +          if(hi_bin>n)hi_bin=n;
   1.228 +
   1.229 +          for(;l<hi_bin && l<n;l++)
   1.230 +            if(brute_buffer[l]>workc[k][m][j])
   1.231 +              brute_buffer[l]=workc[k][m][j];
   1.232 +        }
   1.233 +
   1.234 +        for(;l<n;l++)
   1.235 +          if(brute_buffer[l]>workc[k][m][EHMER_MAX-1])
   1.236 +            brute_buffer[l]=workc[k][m][EHMER_MAX-1];
   1.237 +
   1.238 +      }
   1.239 +
   1.240 +
   1.241 +      for(j=0;j<EHMER_MAX;j++){
   1.242 +        int bin=fromOC(j*.125+i*.5-2.)/binHz;
   1.243 +        if(bin<0){
   1.244 +          ret[i][m][j+2]=-999.;
   1.245 +        }else{
   1.246 +          if(bin>=n){
   1.247 +            ret[i][m][j+2]=-999.;
   1.248 +          }else{
   1.249 +            ret[i][m][j+2]=brute_buffer[bin];
   1.250 +          }
   1.251 +        }
   1.252 +      }
   1.253 +
   1.254 +      /* add fenceposts */
   1.255 +      for(j=0;j<EHMER_OFFSET;j++)
   1.256 +        if(ret[i][m][j+2]>-200.f)break;
   1.257 +      ret[i][m][0]=j;
   1.258 +
   1.259 +      for(j=EHMER_MAX-1;j>EHMER_OFFSET+1;j--)
   1.260 +        if(ret[i][m][j+2]>-200.f)
   1.261 +          break;
   1.262 +      ret[i][m][1]=j;
   1.263 +
   1.264 +    }
   1.265 +  }
   1.266 +
   1.267 +  return(ret);
   1.268 +}
   1.269 +
   1.270 +void _vp_psy_init(vorbis_look_psy *p,vorbis_info_psy *vi,
   1.271 +                  vorbis_info_psy_global *gi,int n,long rate){
   1.272 +  long i,j,lo=-99,hi=1;
   1.273 +  long maxoc;
   1.274 +  memset(p,0,sizeof(*p));
   1.275 +
   1.276 +  p->eighth_octave_lines=gi->eighth_octave_lines;
   1.277 +  p->shiftoc=rint(log(gi->eighth_octave_lines*8.f)/log(2.f))-1;
   1.278 +
   1.279 +  p->firstoc=toOC(.25f*rate*.5/n)*(1<<(p->shiftoc+1))-gi->eighth_octave_lines;
   1.280 +  maxoc=toOC((n+.25f)*rate*.5/n)*(1<<(p->shiftoc+1))+.5f;
   1.281 +  p->total_octave_lines=maxoc-p->firstoc+1;
   1.282 +  p->ath=_ogg_malloc(n*sizeof(*p->ath));
   1.283 +
   1.284 +  p->octave=_ogg_malloc(n*sizeof(*p->octave));
   1.285 +  p->bark=_ogg_malloc(n*sizeof(*p->bark));
   1.286 +  p->vi=vi;
   1.287 +  p->n=n;
   1.288 +  p->rate=rate;
   1.289 +
   1.290 +  /* AoTuV HF weighting */
   1.291 +  p->m_val = 1.;
   1.292 +  if(rate < 26000) p->m_val = 0;
   1.293 +  else if(rate < 38000) p->m_val = .94;   /* 32kHz */
   1.294 +  else if(rate > 46000) p->m_val = 1.275; /* 48kHz */
   1.295 +
   1.296 +  /* set up the lookups for a given blocksize and sample rate */
   1.297 +
   1.298 +  for(i=0,j=0;i<MAX_ATH-1;i++){
   1.299 +    int endpos=rint(fromOC((i+1)*.125-2.)*2*n/rate);
   1.300 +    float base=ATH[i];
   1.301 +    if(j<endpos){
   1.302 +      float delta=(ATH[i+1]-base)/(endpos-j);
   1.303 +      for(;j<endpos && j<n;j++){
   1.304 +        p->ath[j]=base+100.;
   1.305 +        base+=delta;
   1.306 +      }
   1.307 +    }
   1.308 +  }
   1.309 +
   1.310 +  for(;j<n;j++){
   1.311 +    p->ath[j]=p->ath[j-1];
   1.312 +  }
   1.313 +
   1.314 +  for(i=0;i<n;i++){
   1.315 +    float bark=toBARK(rate/(2*n)*i);
   1.316 +
   1.317 +    for(;lo+vi->noisewindowlomin<i &&
   1.318 +          toBARK(rate/(2*n)*lo)<(bark-vi->noisewindowlo);lo++);
   1.319 +
   1.320 +    for(;hi<=n && (hi<i+vi->noisewindowhimin ||
   1.321 +          toBARK(rate/(2*n)*hi)<(bark+vi->noisewindowhi));hi++);
   1.322 +
   1.323 +    p->bark[i]=((lo-1)<<16)+(hi-1);
   1.324 +
   1.325 +  }
   1.326 +
   1.327 +  for(i=0;i<n;i++)
   1.328 +    p->octave[i]=toOC((i+.25f)*.5*rate/n)*(1<<(p->shiftoc+1))+.5f;
   1.329 +
   1.330 +  p->tonecurves=setup_tone_curves(vi->toneatt,rate*.5/n,n,
   1.331 +                                  vi->tone_centerboost,vi->tone_decay);
   1.332 +
   1.333 +  /* set up rolling noise median */
   1.334 +  p->noiseoffset=_ogg_malloc(P_NOISECURVES*sizeof(*p->noiseoffset));
   1.335 +  for(i=0;i<P_NOISECURVES;i++)
   1.336 +    p->noiseoffset[i]=_ogg_malloc(n*sizeof(**p->noiseoffset));
   1.337 +
   1.338 +  for(i=0;i<n;i++){
   1.339 +    float halfoc=toOC((i+.5)*rate/(2.*n))*2.;
   1.340 +    int inthalfoc;
   1.341 +    float del;
   1.342 +
   1.343 +    if(halfoc<0)halfoc=0;
   1.344 +    if(halfoc>=P_BANDS-1)halfoc=P_BANDS-1;
   1.345 +    inthalfoc=(int)halfoc;
   1.346 +    del=halfoc-inthalfoc;
   1.347 +
   1.348 +    for(j=0;j<P_NOISECURVES;j++)
   1.349 +      p->noiseoffset[j][i]=
   1.350 +        p->vi->noiseoff[j][inthalfoc]*(1.-del) +
   1.351 +        p->vi->noiseoff[j][inthalfoc+1]*del;
   1.352 +
   1.353 +  }
   1.354 +#if 0
   1.355 +  {
   1.356 +    static int ls=0;
   1.357 +    _analysis_output_always("noiseoff0",ls,p->noiseoffset[0],n,1,0,0);
   1.358 +    _analysis_output_always("noiseoff1",ls,p->noiseoffset[1],n,1,0,0);
   1.359 +    _analysis_output_always("noiseoff2",ls++,p->noiseoffset[2],n,1,0,0);
   1.360 +  }
   1.361 +#endif
   1.362 +}
   1.363 +
   1.364 +void _vp_psy_clear(vorbis_look_psy *p){
   1.365 +  int i,j;
   1.366 +  if(p){
   1.367 +    if(p->ath)_ogg_free(p->ath);
   1.368 +    if(p->octave)_ogg_free(p->octave);
   1.369 +    if(p->bark)_ogg_free(p->bark);
   1.370 +    if(p->tonecurves){
   1.371 +      for(i=0;i<P_BANDS;i++){
   1.372 +        for(j=0;j<P_LEVELS;j++){
   1.373 +          _ogg_free(p->tonecurves[i][j]);
   1.374 +        }
   1.375 +        _ogg_free(p->tonecurves[i]);
   1.376 +      }
   1.377 +      _ogg_free(p->tonecurves);
   1.378 +    }
   1.379 +    if(p->noiseoffset){
   1.380 +      for(i=0;i<P_NOISECURVES;i++){
   1.381 +        _ogg_free(p->noiseoffset[i]);
   1.382 +      }
   1.383 +      _ogg_free(p->noiseoffset);
   1.384 +    }
   1.385 +    memset(p,0,sizeof(*p));
   1.386 +  }
   1.387 +}
   1.388 +
   1.389 +/* octave/(8*eighth_octave_lines) x scale and dB y scale */
   1.390 +static void seed_curve(float *seed,
   1.391 +                       const float **curves,
   1.392 +                       float amp,
   1.393 +                       int oc, int n,
   1.394 +                       int linesper,float dBoffset){
   1.395 +  int i,post1;
   1.396 +  int seedptr;
   1.397 +  const float *posts,*curve;
   1.398 +
   1.399 +  int choice=(int)((amp+dBoffset-P_LEVEL_0)*.1f);
   1.400 +  choice=max(choice,0);
   1.401 +  choice=min(choice,P_LEVELS-1);
   1.402 +  posts=curves[choice];
   1.403 +  curve=posts+2;
   1.404 +  post1=(int)posts[1];
   1.405 +  seedptr=oc+(posts[0]-EHMER_OFFSET)*linesper-(linesper>>1);
   1.406 +
   1.407 +  for(i=posts[0];i<post1;i++){
   1.408 +    if(seedptr>0){
   1.409 +      float lin=amp+curve[i];
   1.410 +      if(seed[seedptr]<lin)seed[seedptr]=lin;
   1.411 +    }
   1.412 +    seedptr+=linesper;
   1.413 +    if(seedptr>=n)break;
   1.414 +  }
   1.415 +}
   1.416 +
   1.417 +static void seed_loop(vorbis_look_psy *p,
   1.418 +                      const float ***curves,
   1.419 +                      const float *f,
   1.420 +                      const float *flr,
   1.421 +                      float *seed,
   1.422 +                      float specmax){
   1.423 +  vorbis_info_psy *vi=p->vi;
   1.424 +  long n=p->n,i;
   1.425 +  float dBoffset=vi->max_curve_dB-specmax;
   1.426 +
   1.427 +  /* prime the working vector with peak values */
   1.428 +
   1.429 +  for(i=0;i<n;i++){
   1.430 +    float max=f[i];
   1.431 +    long oc=p->octave[i];
   1.432 +    while(i+1<n && p->octave[i+1]==oc){
   1.433 +      i++;
   1.434 +      if(f[i]>max)max=f[i];
   1.435 +    }
   1.436 +
   1.437 +    if(max+6.f>flr[i]){
   1.438 +      oc=oc>>p->shiftoc;
   1.439 +
   1.440 +      if(oc>=P_BANDS)oc=P_BANDS-1;
   1.441 +      if(oc<0)oc=0;
   1.442 +
   1.443 +      seed_curve(seed,
   1.444 +                 curves[oc],
   1.445 +                 max,
   1.446 +                 p->octave[i]-p->firstoc,
   1.447 +                 p->total_octave_lines,
   1.448 +                 p->eighth_octave_lines,
   1.449 +                 dBoffset);
   1.450 +    }
   1.451 +  }
   1.452 +}
   1.453 +
   1.454 +static void seed_chase(float *seeds, int linesper, long n){
   1.455 +  long  *posstack=alloca(n*sizeof(*posstack));
   1.456 +  float *ampstack=alloca(n*sizeof(*ampstack));
   1.457 +  long   stack=0;
   1.458 +  long   pos=0;
   1.459 +  long   i;
   1.460 +
   1.461 +  for(i=0;i<n;i++){
   1.462 +    if(stack<2){
   1.463 +      posstack[stack]=i;
   1.464 +      ampstack[stack++]=seeds[i];
   1.465 +    }else{
   1.466 +      while(1){
   1.467 +        if(seeds[i]<ampstack[stack-1]){
   1.468 +          posstack[stack]=i;
   1.469 +          ampstack[stack++]=seeds[i];
   1.470 +          break;
   1.471 +        }else{
   1.472 +          if(i<posstack[stack-1]+linesper){
   1.473 +            if(stack>1 && ampstack[stack-1]<=ampstack[stack-2] &&
   1.474 +               i<posstack[stack-2]+linesper){
   1.475 +              /* we completely overlap, making stack-1 irrelevant.  pop it */
   1.476 +              stack--;
   1.477 +              continue;
   1.478 +            }
   1.479 +          }
   1.480 +          posstack[stack]=i;
   1.481 +          ampstack[stack++]=seeds[i];
   1.482 +          break;
   1.483 +
   1.484 +        }
   1.485 +      }
   1.486 +    }
   1.487 +  }
   1.488 +
   1.489 +  /* the stack now contains only the positions that are relevant. Scan
   1.490 +     'em straight through */
   1.491 +
   1.492 +  for(i=0;i<stack;i++){
   1.493 +    long endpos;
   1.494 +    if(i<stack-1 && ampstack[i+1]>ampstack[i]){
   1.495 +      endpos=posstack[i+1];
   1.496 +    }else{
   1.497 +      endpos=posstack[i]+linesper+1; /* +1 is important, else bin 0 is
   1.498 +                                        discarded in short frames */
   1.499 +    }
   1.500 +    if(endpos>n)endpos=n;
   1.501 +    for(;pos<endpos;pos++)
   1.502 +      seeds[pos]=ampstack[i];
   1.503 +  }
   1.504 +
   1.505 +  /* there.  Linear time.  I now remember this was on a problem set I
   1.506 +     had in Grad Skool... I didn't solve it at the time ;-) */
   1.507 +
   1.508 +}
   1.509 +
   1.510 +/* bleaugh, this is more complicated than it needs to be */
   1.511 +#include<stdio.h>
   1.512 +static void max_seeds(vorbis_look_psy *p,
   1.513 +                      float *seed,
   1.514 +                      float *flr){
   1.515 +  long   n=p->total_octave_lines;
   1.516 +  int    linesper=p->eighth_octave_lines;
   1.517 +  long   linpos=0;
   1.518 +  long   pos;
   1.519 +
   1.520 +  seed_chase(seed,linesper,n); /* for masking */
   1.521 +
   1.522 +  pos=p->octave[0]-p->firstoc-(linesper>>1);
   1.523 +
   1.524 +  while(linpos+1<p->n){
   1.525 +    float minV=seed[pos];
   1.526 +    long end=((p->octave[linpos]+p->octave[linpos+1])>>1)-p->firstoc;
   1.527 +    if(minV>p->vi->tone_abs_limit)minV=p->vi->tone_abs_limit;
   1.528 +    while(pos+1<=end){
   1.529 +      pos++;
   1.530 +      if((seed[pos]>NEGINF && seed[pos]<minV) || minV==NEGINF)
   1.531 +        minV=seed[pos];
   1.532 +    }
   1.533 +
   1.534 +    end=pos+p->firstoc;
   1.535 +    for(;linpos<p->n && p->octave[linpos]<=end;linpos++)
   1.536 +      if(flr[linpos]<minV)flr[linpos]=minV;
   1.537 +  }
   1.538 +
   1.539 +  {
   1.540 +    float minV=seed[p->total_octave_lines-1];
   1.541 +    for(;linpos<p->n;linpos++)
   1.542 +      if(flr[linpos]<minV)flr[linpos]=minV;
   1.543 +  }
   1.544 +
   1.545 +}
   1.546 +
   1.547 +static void bark_noise_hybridmp(int n,const long *b,
   1.548 +                                const float *f,
   1.549 +                                float *noise,
   1.550 +                                const float offset,
   1.551 +                                const int fixed){
   1.552 +
   1.553 +  float *N=alloca(n*sizeof(*N));
   1.554 +  float *X=alloca(n*sizeof(*N));
   1.555 +  float *XX=alloca(n*sizeof(*N));
   1.556 +  float *Y=alloca(n*sizeof(*N));
   1.557 +  float *XY=alloca(n*sizeof(*N));
   1.558 +
   1.559 +  float tN, tX, tXX, tY, tXY;
   1.560 +  int i;
   1.561 +
   1.562 +  int lo, hi;
   1.563 +  float R=0.f;
   1.564 +  float A=0.f;
   1.565 +  float B=0.f;
   1.566 +  float D=1.f;
   1.567 +  float w, x, y;
   1.568 +
   1.569 +  tN = tX = tXX = tY = tXY = 0.f;
   1.570 +
   1.571 +  y = f[0] + offset;
   1.572 +  if (y < 1.f) y = 1.f;
   1.573 +
   1.574 +  w = y * y * .5;
   1.575 +
   1.576 +  tN += w;
   1.577 +  tX += w;
   1.578 +  tY += w * y;
   1.579 +
   1.580 +  N[0] = tN;
   1.581 +  X[0] = tX;
   1.582 +  XX[0] = tXX;
   1.583 +  Y[0] = tY;
   1.584 +  XY[0] = tXY;
   1.585 +
   1.586 +  for (i = 1, x = 1.f; i < n; i++, x += 1.f) {
   1.587 +
   1.588 +    y = f[i] + offset;
   1.589 +    if (y < 1.f) y = 1.f;
   1.590 +
   1.591 +    w = y * y;
   1.592 +
   1.593 +    tN += w;
   1.594 +    tX += w * x;
   1.595 +    tXX += w * x * x;
   1.596 +    tY += w * y;
   1.597 +    tXY += w * x * y;
   1.598 +
   1.599 +    N[i] = tN;
   1.600 +    X[i] = tX;
   1.601 +    XX[i] = tXX;
   1.602 +    Y[i] = tY;
   1.603 +    XY[i] = tXY;
   1.604 +  }
   1.605 +
   1.606 +  for (i = 0, x = 0.f;; i++, x += 1.f) {
   1.607 +
   1.608 +    lo = b[i] >> 16;
   1.609 +    if( lo>=0 ) break;
   1.610 +    hi = b[i] & 0xffff;
   1.611 +
   1.612 +    tN = N[hi] + N[-lo];
   1.613 +    tX = X[hi] - X[-lo];
   1.614 +    tXX = XX[hi] + XX[-lo];
   1.615 +    tY = Y[hi] + Y[-lo];
   1.616 +    tXY = XY[hi] - XY[-lo];
   1.617 +
   1.618 +    A = tY * tXX - tX * tXY;
   1.619 +    B = tN * tXY - tX * tY;
   1.620 +    D = tN * tXX - tX * tX;
   1.621 +    R = (A + x * B) / D;
   1.622 +    if (R < 0.f)
   1.623 +      R = 0.f;
   1.624 +
   1.625 +    noise[i] = R - offset;
   1.626 +  }
   1.627 +
   1.628 +  for ( ;; i++, x += 1.f) {
   1.629 +
   1.630 +    lo = b[i] >> 16;
   1.631 +    hi = b[i] & 0xffff;
   1.632 +    if(hi>=n)break;
   1.633 +
   1.634 +    tN = N[hi] - N[lo];
   1.635 +    tX = X[hi] - X[lo];
   1.636 +    tXX = XX[hi] - XX[lo];
   1.637 +    tY = Y[hi] - Y[lo];
   1.638 +    tXY = XY[hi] - XY[lo];
   1.639 +
   1.640 +    A = tY * tXX - tX * tXY;
   1.641 +    B = tN * tXY - tX * tY;
   1.642 +    D = tN * tXX - tX * tX;
   1.643 +    R = (A + x * B) / D;
   1.644 +    if (R < 0.f) R = 0.f;
   1.645 +
   1.646 +    noise[i] = R - offset;
   1.647 +  }
   1.648 +  for ( ; i < n; i++, x += 1.f) {
   1.649 +
   1.650 +    R = (A + x * B) / D;
   1.651 +    if (R < 0.f) R = 0.f;
   1.652 +
   1.653 +    noise[i] = R - offset;
   1.654 +  }
   1.655 +
   1.656 +  if (fixed <= 0) return;
   1.657 +
   1.658 +  for (i = 0, x = 0.f;; i++, x += 1.f) {
   1.659 +    hi = i + fixed / 2;
   1.660 +    lo = hi - fixed;
   1.661 +    if(lo>=0)break;
   1.662 +
   1.663 +    tN = N[hi] + N[-lo];
   1.664 +    tX = X[hi] - X[-lo];
   1.665 +    tXX = XX[hi] + XX[-lo];
   1.666 +    tY = Y[hi] + Y[-lo];
   1.667 +    tXY = XY[hi] - XY[-lo];
   1.668 +
   1.669 +
   1.670 +    A = tY * tXX - tX * tXY;
   1.671 +    B = tN * tXY - tX * tY;
   1.672 +    D = tN * tXX - tX * tX;
   1.673 +    R = (A + x * B) / D;
   1.674 +
   1.675 +    if (R - offset < noise[i]) noise[i] = R - offset;
   1.676 +  }
   1.677 +  for ( ;; i++, x += 1.f) {
   1.678 +
   1.679 +    hi = i + fixed / 2;
   1.680 +    lo = hi - fixed;
   1.681 +    if(hi>=n)break;
   1.682 +
   1.683 +    tN = N[hi] - N[lo];
   1.684 +    tX = X[hi] - X[lo];
   1.685 +    tXX = XX[hi] - XX[lo];
   1.686 +    tY = Y[hi] - Y[lo];
   1.687 +    tXY = XY[hi] - XY[lo];
   1.688 +
   1.689 +    A = tY * tXX - tX * tXY;
   1.690 +    B = tN * tXY - tX * tY;
   1.691 +    D = tN * tXX - tX * tX;
   1.692 +    R = (A + x * B) / D;
   1.693 +
   1.694 +    if (R - offset < noise[i]) noise[i] = R - offset;
   1.695 +  }
   1.696 +  for ( ; i < n; i++, x += 1.f) {
   1.697 +    R = (A + x * B) / D;
   1.698 +    if (R - offset < noise[i]) noise[i] = R - offset;
   1.699 +  }
   1.700 +}
   1.701 +
   1.702 +void _vp_noisemask(vorbis_look_psy *p,
   1.703 +                   float *logmdct,
   1.704 +                   float *logmask){
   1.705 +
   1.706 +  int i,n=p->n;
   1.707 +  float *work=alloca(n*sizeof(*work));
   1.708 +
   1.709 +  bark_noise_hybridmp(n,p->bark,logmdct,logmask,
   1.710 +                      140.,-1);
   1.711 +
   1.712 +  for(i=0;i<n;i++)work[i]=logmdct[i]-logmask[i];
   1.713 +
   1.714 +  bark_noise_hybridmp(n,p->bark,work,logmask,0.,
   1.715 +                      p->vi->noisewindowfixed);
   1.716 +
   1.717 +  for(i=0;i<n;i++)work[i]=logmdct[i]-work[i];
   1.718 +
   1.719 +#if 0
   1.720 +  {
   1.721 +    static int seq=0;
   1.722 +
   1.723 +    float work2[n];
   1.724 +    for(i=0;i<n;i++){
   1.725 +      work2[i]=logmask[i]+work[i];
   1.726 +    }
   1.727 +
   1.728 +    if(seq&1)
   1.729 +      _analysis_output("median2R",seq/2,work,n,1,0,0);
   1.730 +    else
   1.731 +      _analysis_output("median2L",seq/2,work,n,1,0,0);
   1.732 +
   1.733 +    if(seq&1)
   1.734 +      _analysis_output("envelope2R",seq/2,work2,n,1,0,0);
   1.735 +    else
   1.736 +      _analysis_output("envelope2L",seq/2,work2,n,1,0,0);
   1.737 +    seq++;
   1.738 +  }
   1.739 +#endif
   1.740 +
   1.741 +  for(i=0;i<n;i++){
   1.742 +    int dB=logmask[i]+.5;
   1.743 +    if(dB>=NOISE_COMPAND_LEVELS)dB=NOISE_COMPAND_LEVELS-1;
   1.744 +    if(dB<0)dB=0;
   1.745 +    logmask[i]= work[i]+p->vi->noisecompand[dB];
   1.746 +  }
   1.747 +
   1.748 +}
   1.749 +
   1.750 +void _vp_tonemask(vorbis_look_psy *p,
   1.751 +                  float *logfft,
   1.752 +                  float *logmask,
   1.753 +                  float global_specmax,
   1.754 +                  float local_specmax){
   1.755 +
   1.756 +  int i,n=p->n;
   1.757 +
   1.758 +  float *seed=alloca(sizeof(*seed)*p->total_octave_lines);
   1.759 +  float att=local_specmax+p->vi->ath_adjatt;
   1.760 +  for(i=0;i<p->total_octave_lines;i++)seed[i]=NEGINF;
   1.761 +
   1.762 +  /* set the ATH (floating below localmax, not global max by a
   1.763 +     specified att) */
   1.764 +  if(att<p->vi->ath_maxatt)att=p->vi->ath_maxatt;
   1.765 +
   1.766 +  for(i=0;i<n;i++)
   1.767 +    logmask[i]=p->ath[i]+att;
   1.768 +
   1.769 +  /* tone masking */
   1.770 +  seed_loop(p,(const float ***)p->tonecurves,logfft,logmask,seed,global_specmax);
   1.771 +  max_seeds(p,seed,logmask);
   1.772 +
   1.773 +}
   1.774 +
   1.775 +void _vp_offset_and_mix(vorbis_look_psy *p,
   1.776 +                        float *noise,
   1.777 +                        float *tone,
   1.778 +                        int offset_select,
   1.779 +                        float *logmask,
   1.780 +                        float *mdct,
   1.781 +                        float *logmdct){
   1.782 +  int i,n=p->n;
   1.783 +  float de, coeffi, cx;/* AoTuV */
   1.784 +  float toneatt=p->vi->tone_masteratt[offset_select];
   1.785 +
   1.786 +  cx = p->m_val;
   1.787 +
   1.788 +  for(i=0;i<n;i++){
   1.789 +    float val= noise[i]+p->noiseoffset[offset_select][i];
   1.790 +    if(val>p->vi->noisemaxsupp)val=p->vi->noisemaxsupp;
   1.791 +    logmask[i]=max(val,tone[i]+toneatt);
   1.792 +
   1.793 +
   1.794 +    /* AoTuV */
   1.795 +    /** @ M1 **
   1.796 +        The following codes improve a noise problem.
   1.797 +        A fundamental idea uses the value of masking and carries out
   1.798 +        the relative compensation of the MDCT.
   1.799 +        However, this code is not perfect and all noise problems cannot be solved.
   1.800 +        by Aoyumi @ 2004/04/18
   1.801 +    */
   1.802 +
   1.803 +    if(offset_select == 1) {
   1.804 +      coeffi = -17.2;       /* coeffi is a -17.2dB threshold */
   1.805 +      val = val - logmdct[i];  /* val == mdct line value relative to floor in dB */
   1.806 +
   1.807 +      if(val > coeffi){
   1.808 +        /* mdct value is > -17.2 dB below floor */
   1.809 +
   1.810 +        de = 1.0-((val-coeffi)*0.005*cx);
   1.811 +        /* pro-rated attenuation:
   1.812 +           -0.00 dB boost if mdct value is -17.2dB (relative to floor)
   1.813 +           -0.77 dB boost if mdct value is 0dB (relative to floor)
   1.814 +           -1.64 dB boost if mdct value is +17.2dB (relative to floor)
   1.815 +           etc... */
   1.816 +
   1.817 +        if(de < 0) de = 0.0001;
   1.818 +      }else
   1.819 +        /* mdct value is <= -17.2 dB below floor */
   1.820 +
   1.821 +        de = 1.0-((val-coeffi)*0.0003*cx);
   1.822 +      /* pro-rated attenuation:
   1.823 +         +0.00 dB atten if mdct value is -17.2dB (relative to floor)
   1.824 +         +0.45 dB atten if mdct value is -34.4dB (relative to floor)
   1.825 +         etc... */
   1.826 +
   1.827 +      mdct[i] *= de;
   1.828 +
   1.829 +    }
   1.830 +  }
   1.831 +}
   1.832 +
   1.833 +float _vp_ampmax_decay(float amp,vorbis_dsp_state *vd){
   1.834 +  vorbis_info *vi=vd->vi;
   1.835 +  codec_setup_info *ci=vi->codec_setup;
   1.836 +  vorbis_info_psy_global *gi=&ci->psy_g_param;
   1.837 +
   1.838 +  int n=ci->blocksizes[vd->W]/2;
   1.839 +  float secs=(float)n/vi->rate;
   1.840 +
   1.841 +  amp+=secs*gi->ampmax_att_per_sec;
   1.842 +  if(amp<-9999)amp=-9999;
   1.843 +  return(amp);
   1.844 +}
   1.845 +
   1.846 +static float FLOOR1_fromdB_LOOKUP[256]={
   1.847 +  1.0649863e-07F, 1.1341951e-07F, 1.2079015e-07F, 1.2863978e-07F,
   1.848 +  1.3699951e-07F, 1.4590251e-07F, 1.5538408e-07F, 1.6548181e-07F,
   1.849 +  1.7623575e-07F, 1.8768855e-07F, 1.9988561e-07F, 2.128753e-07F,
   1.850 +  2.2670913e-07F, 2.4144197e-07F, 2.5713223e-07F, 2.7384213e-07F,
   1.851 +  2.9163793e-07F, 3.1059021e-07F, 3.3077411e-07F, 3.5226968e-07F,
   1.852 +  3.7516214e-07F, 3.9954229e-07F, 4.2550680e-07F, 4.5315863e-07F,
   1.853 +  4.8260743e-07F, 5.1396998e-07F, 5.4737065e-07F, 5.8294187e-07F,
   1.854 +  6.2082472e-07F, 6.6116941e-07F, 7.0413592e-07F, 7.4989464e-07F,
   1.855 +  7.9862701e-07F, 8.5052630e-07F, 9.0579828e-07F, 9.6466216e-07F,
   1.856 +  1.0273513e-06F, 1.0941144e-06F, 1.1652161e-06F, 1.2409384e-06F,
   1.857 +  1.3215816e-06F, 1.4074654e-06F, 1.4989305e-06F, 1.5963394e-06F,
   1.858 +  1.7000785e-06F, 1.8105592e-06F, 1.9282195e-06F, 2.0535261e-06F,
   1.859 +  2.1869758e-06F, 2.3290978e-06F, 2.4804557e-06F, 2.6416497e-06F,
   1.860 +  2.8133190e-06F, 2.9961443e-06F, 3.1908506e-06F, 3.3982101e-06F,
   1.861 +  3.6190449e-06F, 3.8542308e-06F, 4.1047004e-06F, 4.3714470e-06F,
   1.862 +  4.6555282e-06F, 4.9580707e-06F, 5.2802740e-06F, 5.6234160e-06F,
   1.863 +  5.9888572e-06F, 6.3780469e-06F, 6.7925283e-06F, 7.2339451e-06F,
   1.864 +  7.7040476e-06F, 8.2047000e-06F, 8.7378876e-06F, 9.3057248e-06F,
   1.865 +  9.9104632e-06F, 1.0554501e-05F, 1.1240392e-05F, 1.1970856e-05F,
   1.866 +  1.2748789e-05F, 1.3577278e-05F, 1.4459606e-05F, 1.5399272e-05F,
   1.867 +  1.6400004e-05F, 1.7465768e-05F, 1.8600792e-05F, 1.9809576e-05F,
   1.868 +  2.1096914e-05F, 2.2467911e-05F, 2.3928002e-05F, 2.5482978e-05F,
   1.869 +  2.7139006e-05F, 2.8902651e-05F, 3.0780908e-05F, 3.2781225e-05F,
   1.870 +  3.4911534e-05F, 3.7180282e-05F, 3.9596466e-05F, 4.2169667e-05F,
   1.871 +  4.4910090e-05F, 4.7828601e-05F, 5.0936773e-05F, 5.4246931e-05F,
   1.872 +  5.7772202e-05F, 6.1526565e-05F, 6.5524908e-05F, 6.9783085e-05F,
   1.873 +  7.4317983e-05F, 7.9147585e-05F, 8.4291040e-05F, 8.9768747e-05F,
   1.874 +  9.5602426e-05F, 0.00010181521F, 0.00010843174F, 0.00011547824F,
   1.875 +  0.00012298267F, 0.00013097477F, 0.00013948625F, 0.00014855085F,
   1.876 +  0.00015820453F, 0.00016848555F, 0.00017943469F, 0.00019109536F,
   1.877 +  0.00020351382F, 0.00021673929F, 0.00023082423F, 0.00024582449F,
   1.878 +  0.00026179955F, 0.00027881276F, 0.00029693158F, 0.00031622787F,
   1.879 +  0.00033677814F, 0.00035866388F, 0.00038197188F, 0.00040679456F,
   1.880 +  0.00043323036F, 0.00046138411F, 0.00049136745F, 0.00052329927F,
   1.881 +  0.00055730621F, 0.00059352311F, 0.00063209358F, 0.00067317058F,
   1.882 +  0.00071691700F, 0.00076350630F, 0.00081312324F, 0.00086596457F,
   1.883 +  0.00092223983F, 0.00098217216F, 0.0010459992F, 0.0011139742F,
   1.884 +  0.0011863665F, 0.0012634633F, 0.0013455702F, 0.0014330129F,
   1.885 +  0.0015261382F, 0.0016253153F, 0.0017309374F, 0.0018434235F,
   1.886 +  0.0019632195F, 0.0020908006F, 0.0022266726F, 0.0023713743F,
   1.887 +  0.0025254795F, 0.0026895994F, 0.0028643847F, 0.0030505286F,
   1.888 +  0.0032487691F, 0.0034598925F, 0.0036847358F, 0.0039241906F,
   1.889 +  0.0041792066F, 0.0044507950F, 0.0047400328F, 0.0050480668F,
   1.890 +  0.0053761186F, 0.0057254891F, 0.0060975636F, 0.0064938176F,
   1.891 +  0.0069158225F, 0.0073652516F, 0.0078438871F, 0.0083536271F,
   1.892 +  0.0088964928F, 0.009474637F, 0.010090352F, 0.010746080F,
   1.893 +  0.011444421F, 0.012188144F, 0.012980198F, 0.013823725F,
   1.894 +  0.014722068F, 0.015678791F, 0.016697687F, 0.017782797F,
   1.895 +  0.018938423F, 0.020169149F, 0.021479854F, 0.022875735F,
   1.896 +  0.024362330F, 0.025945531F, 0.027631618F, 0.029427276F,
   1.897 +  0.031339626F, 0.033376252F, 0.035545228F, 0.037855157F,
   1.898 +  0.040315199F, 0.042935108F, 0.045725273F, 0.048696758F,
   1.899 +  0.051861348F, 0.055231591F, 0.058820850F, 0.062643361F,
   1.900 +  0.066714279F, 0.071049749F, 0.075666962F, 0.080584227F,
   1.901 +  0.085821044F, 0.091398179F, 0.097337747F, 0.10366330F,
   1.902 +  0.11039993F, 0.11757434F, 0.12521498F, 0.13335215F,
   1.903 +  0.14201813F, 0.15124727F, 0.16107617F, 0.17154380F,
   1.904 +  0.18269168F, 0.19456402F, 0.20720788F, 0.22067342F,
   1.905 +  0.23501402F, 0.25028656F, 0.26655159F, 0.28387361F,
   1.906 +  0.30232132F, 0.32196786F, 0.34289114F, 0.36517414F,
   1.907 +  0.38890521F, 0.41417847F, 0.44109412F, 0.46975890F,
   1.908 +  0.50028648F, 0.53279791F, 0.56742212F, 0.60429640F,
   1.909 +  0.64356699F, 0.68538959F, 0.72993007F, 0.77736504F,
   1.910 +  0.82788260F, 0.88168307F, 0.9389798F, 1.F,
   1.911 +};
   1.912 +
   1.913 +/* this is for per-channel noise normalization */
   1.914 +static int apsort(const void *a, const void *b){
   1.915 +  float f1=**(float**)a;
   1.916 +  float f2=**(float**)b;
   1.917 +  return (f1<f2)-(f1>f2);
   1.918 +}
   1.919 +
   1.920 +static void flag_lossless(int limit, float prepoint, float postpoint, float *mdct,
   1.921 +                         float *floor, int *flag, int i, int jn){
   1.922 +  int j;
   1.923 +  for(j=0;j<jn;j++){
   1.924 +    float point = j>=limit-i ? postpoint : prepoint;
   1.925 +    float r = fabs(mdct[j])/floor[j];
   1.926 +    if(r<point)
   1.927 +      flag[j]=0;
   1.928 +    else
   1.929 +      flag[j]=1;
   1.930 +  }
   1.931 +}
   1.932 +
   1.933 +/* Overload/Side effect: On input, the *q vector holds either the
   1.934 +   quantized energy (for elements with the flag set) or the absolute
   1.935 +   values of the *r vector (for elements with flag unset).  On output,
   1.936 +   *q holds the quantized energy for all elements */
   1.937 +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){
   1.938 +
   1.939 +  vorbis_info_psy *vi=p->vi;
   1.940 +  float **sort = alloca(n*sizeof(*sort));
   1.941 +  int j,count=0;
   1.942 +  int start = (vi->normal_p ? vi->normal_start-i : n);
   1.943 +  if(start>n)start=n;
   1.944 +
   1.945 +  /* force classic behavior where only energy in the current band is considered */
   1.946 +  acc=0.f;
   1.947 +
   1.948 +  /* still responsible for populating *out where noise norm not in
   1.949 +     effect.  There's no need to [re]populate *q in these areas */
   1.950 +  for(j=0;j<start;j++){
   1.951 +    if(!flags || !flags[j]){ /* lossless coupling already quantized.
   1.952 +                                Don't touch; requantizing based on
   1.953 +                                energy would be incorrect. */
   1.954 +      float ve = q[j]/f[j];
   1.955 +      if(r[j]<0)
   1.956 +        out[j] = -rint(sqrt(ve));
   1.957 +      else
   1.958 +        out[j] = rint(sqrt(ve));
   1.959 +    }
   1.960 +  }
   1.961 +
   1.962 +  /* sort magnitudes for noise norm portion of partition */
   1.963 +  for(;j<n;j++){
   1.964 +    if(!flags || !flags[j]){ /* can't noise norm elements that have
   1.965 +                                already been loslessly coupled; we can
   1.966 +                                only account for their energy error */
   1.967 +      float ve = q[j]/f[j];
   1.968 +      /* Despite all the new, more capable coupling code, for now we
   1.969 +         implement noise norm as it has been up to this point. Only
   1.970 +         consider promotions to unit magnitude from 0.  In addition
   1.971 +         the only energy error counted is quantizations to zero. */
   1.972 +      /* also-- the original point code only applied noise norm at > pointlimit */
   1.973 +      if(ve<.25f && (!flags || j>=limit-i)){
   1.974 +        acc += ve;
   1.975 +        sort[count++]=q+j; /* q is fabs(r) for unflagged element */
   1.976 +      }else{
   1.977 +        /* For now: no acc adjustment for nonzero quantization.  populate *out and q as this value is final. */
   1.978 +        if(r[j]<0)
   1.979 +          out[j] = -rint(sqrt(ve));
   1.980 +        else
   1.981 +          out[j] = rint(sqrt(ve));
   1.982 +        q[j] = out[j]*out[j]*f[j];
   1.983 +      }
   1.984 +    }/* else{
   1.985 +        again, no energy adjustment for error in nonzero quant-- for now
   1.986 +        }*/
   1.987 +  }
   1.988 +
   1.989 +  if(count){
   1.990 +    /* noise norm to do */
   1.991 +    qsort(sort,count,sizeof(*sort),apsort);
   1.992 +    for(j=0;j<count;j++){
   1.993 +      int k=sort[j]-q;
   1.994 +      if(acc>=vi->normal_thresh){
   1.995 +        out[k]=unitnorm(r[k]);
   1.996 +        acc-=1.f;
   1.997 +        q[k]=f[k];
   1.998 +      }else{
   1.999 +        out[k]=0;
  1.1000 +        q[k]=0.f;
  1.1001 +      }
  1.1002 +    }
  1.1003 +  }
  1.1004 +
  1.1005 +  return acc;
  1.1006 +}
  1.1007 +
  1.1008 +/* Noise normalization, quantization and coupling are not wholly
  1.1009 +   seperable processes in depth>1 coupling. */
  1.1010 +void _vp_couple_quantize_normalize(int blobno,
  1.1011 +                                   vorbis_info_psy_global *g,
  1.1012 +                                   vorbis_look_psy *p,
  1.1013 +                                   vorbis_info_mapping0 *vi,
  1.1014 +                                   float **mdct,
  1.1015 +                                   int   **iwork,
  1.1016 +                                   int    *nonzero,
  1.1017 +                                   int     sliding_lowpass,
  1.1018 +                                   int     ch){
  1.1019 +
  1.1020 +  int i;
  1.1021 +  int n = p->n;
  1.1022 +  int partition=(p->vi->normal_p ? p->vi->normal_partition : 16);
  1.1023 +  int limit = g->coupling_pointlimit[p->vi->blockflag][blobno];
  1.1024 +  float prepoint=stereo_threshholds[g->coupling_prepointamp[blobno]];
  1.1025 +  float postpoint=stereo_threshholds[g->coupling_postpointamp[blobno]];
  1.1026 +#if 0
  1.1027 +  float de=0.1*p->m_val; /* a blend of the AoTuV M2 and M3 code here and below */
  1.1028 +#endif
  1.1029 +
  1.1030 +  /* mdct is our raw mdct output, floor not removed. */
  1.1031 +  /* inout passes in the ifloor, passes back quantized result */
  1.1032 +
  1.1033 +  /* unquantized energy (negative indicates amplitude has negative sign) */
  1.1034 +  float **raw = alloca(ch*sizeof(*raw));
  1.1035 +
  1.1036 +  /* dual pupose; quantized energy (if flag set), othersize fabs(raw) */
  1.1037 +  float **quant = alloca(ch*sizeof(*quant));
  1.1038 +
  1.1039 +  /* floor energy */
  1.1040 +  float **floor = alloca(ch*sizeof(*floor));
  1.1041 +
  1.1042 +  /* flags indicating raw/quantized status of elements in raw vector */
  1.1043 +  int   **flag  = alloca(ch*sizeof(*flag));
  1.1044 +
  1.1045 +  /* non-zero flag working vector */
  1.1046 +  int    *nz    = alloca(ch*sizeof(*nz));
  1.1047 +
  1.1048 +  /* energy surplus/defecit tracking */
  1.1049 +  float  *acc   = alloca((ch+vi->coupling_steps)*sizeof(*acc));
  1.1050 +
  1.1051 +  /* The threshold of a stereo is changed with the size of n */
  1.1052 +  if(n > 1000)
  1.1053 +    postpoint=stereo_threshholds_limited[g->coupling_postpointamp[blobno]];
  1.1054 +
  1.1055 +  raw[0]   = alloca(ch*partition*sizeof(**raw));
  1.1056 +  quant[0] = alloca(ch*partition*sizeof(**quant));
  1.1057 +  floor[0] = alloca(ch*partition*sizeof(**floor));
  1.1058 +  flag[0]  = alloca(ch*partition*sizeof(**flag));
  1.1059 +
  1.1060 +  for(i=1;i<ch;i++){
  1.1061 +    raw[i]   = &raw[0][partition*i];
  1.1062 +    quant[i] = &quant[0][partition*i];
  1.1063 +    floor[i] = &floor[0][partition*i];
  1.1064 +    flag[i]  = &flag[0][partition*i];
  1.1065 +  }
  1.1066 +  for(i=0;i<ch+vi->coupling_steps;i++)
  1.1067 +    acc[i]=0.f;
  1.1068 +
  1.1069 +  for(i=0;i<n;i+=partition){
  1.1070 +    int k,j,jn = partition > n-i ? n-i : partition;
  1.1071 +    int step,track = 0;
  1.1072 +
  1.1073 +    memcpy(nz,nonzero,sizeof(*nz)*ch);
  1.1074 +
  1.1075 +    /* prefill */
  1.1076 +    memset(flag[0],0,ch*partition*sizeof(**flag));
  1.1077 +    for(k=0;k<ch;k++){
  1.1078 +      int *iout = &iwork[k][i];
  1.1079 +      if(nz[k]){
  1.1080 +
  1.1081 +        for(j=0;j<jn;j++)
  1.1082 +          floor[k][j] = FLOOR1_fromdB_LOOKUP[iout[j]];
  1.1083 +
  1.1084 +        flag_lossless(limit,prepoint,postpoint,&mdct[k][i],floor[k],flag[k],i,jn);
  1.1085 +
  1.1086 +        for(j=0;j<jn;j++){
  1.1087 +          quant[k][j] = raw[k][j] = mdct[k][i+j]*mdct[k][i+j];
  1.1088 +          if(mdct[k][i+j]<0.f) raw[k][j]*=-1.f;
  1.1089 +          floor[k][j]*=floor[k][j];
  1.1090 +        }
  1.1091 +
  1.1092 +        acc[track]=noise_normalize(p,limit,raw[k],quant[k],floor[k],NULL,acc[track],i,jn,iout);
  1.1093 +
  1.1094 +      }else{
  1.1095 +        for(j=0;j<jn;j++){
  1.1096 +          floor[k][j] = 1e-10f;
  1.1097 +          raw[k][j] = 0.f;
  1.1098 +          quant[k][j] = 0.f;
  1.1099 +          flag[k][j] = 0;
  1.1100 +          iout[j]=0;
  1.1101 +        }
  1.1102 +        acc[track]=0.f;
  1.1103 +      }
  1.1104 +      track++;
  1.1105 +    }
  1.1106 +
  1.1107 +    /* coupling */
  1.1108 +    for(step=0;step<vi->coupling_steps;step++){
  1.1109 +      int Mi = vi->coupling_mag[step];
  1.1110 +      int Ai = vi->coupling_ang[step];
  1.1111 +      int *iM = &iwork[Mi][i];
  1.1112 +      int *iA = &iwork[Ai][i];
  1.1113 +      float *reM = raw[Mi];
  1.1114 +      float *reA = raw[Ai];
  1.1115 +      float *qeM = quant[Mi];
  1.1116 +      float *qeA = quant[Ai];
  1.1117 +      float *floorM = floor[Mi];
  1.1118 +      float *floorA = floor[Ai];
  1.1119 +      int *fM = flag[Mi];
  1.1120 +      int *fA = flag[Ai];
  1.1121 +
  1.1122 +      if(nz[Mi] || nz[Ai]){
  1.1123 +        nz[Mi] = nz[Ai] = 1;
  1.1124 +
  1.1125 +        for(j=0;j<jn;j++){
  1.1126 +
  1.1127 +          if(j<sliding_lowpass-i){
  1.1128 +            if(fM[j] || fA[j]){
  1.1129 +              /* lossless coupling */
  1.1130 +
  1.1131 +              reM[j] = fabs(reM[j])+fabs(reA[j]);
  1.1132 +              qeM[j] = qeM[j]+qeA[j];
  1.1133 +              fM[j]=fA[j]=1;
  1.1134 +
  1.1135 +              /* couple iM/iA */
  1.1136 +              {
  1.1137 +                int A = iM[j];
  1.1138 +                int B = iA[j];
  1.1139 +
  1.1140 +                if(abs(A)>abs(B)){
  1.1141 +                  iA[j]=(A>0?A-B:B-A);
  1.1142 +                }else{
  1.1143 +                  iA[j]=(B>0?A-B:B-A);
  1.1144 +                  iM[j]=B;
  1.1145 +                }
  1.1146 +
  1.1147 +                /* collapse two equivalent tuples to one */
  1.1148 +                if(iA[j]>=abs(iM[j])*2){
  1.1149 +                  iA[j]= -iA[j];
  1.1150 +                  iM[j]= -iM[j];
  1.1151 +                }
  1.1152 +
  1.1153 +              }
  1.1154 +
  1.1155 +            }else{
  1.1156 +              /* lossy (point) coupling */
  1.1157 +              if(j<limit-i){
  1.1158 +                /* dipole */
  1.1159 +                reM[j] += reA[j];
  1.1160 +                qeM[j] = fabs(reM[j]);
  1.1161 +              }else{
  1.1162 +#if 0
  1.1163 +                /* AoTuV */
  1.1164 +                /** @ M2 **
  1.1165 +                    The boost problem by the combination of noise normalization and point stereo is eased.
  1.1166 +                    However, this is a temporary patch.
  1.1167 +                    by Aoyumi @ 2004/04/18
  1.1168 +                */
  1.1169 +                float derate = (1.0 - de*((float)(j-limit+i) / (float)(n-limit)));
  1.1170 +                /* elliptical */
  1.1171 +                if(reM[j]+reA[j]<0){
  1.1172 +                  reM[j] = - (qeM[j] = (fabs(reM[j])+fabs(reA[j]))*derate*derate);
  1.1173 +                }else{
  1.1174 +                  reM[j] =   (qeM[j] = (fabs(reM[j])+fabs(reA[j]))*derate*derate);
  1.1175 +                }
  1.1176 +#else
  1.1177 +                /* elliptical */
  1.1178 +                if(reM[j]+reA[j]<0){
  1.1179 +                  reM[j] = - (qeM[j] = fabs(reM[j])+fabs(reA[j]));
  1.1180 +                }else{
  1.1181 +                  reM[j] =   (qeM[j] = fabs(reM[j])+fabs(reA[j]));
  1.1182 +                }
  1.1183 +#endif
  1.1184 +
  1.1185 +              }
  1.1186 +              reA[j]=qeA[j]=0.f;
  1.1187 +              fA[j]=1;
  1.1188 +              iA[j]=0;
  1.1189 +            }
  1.1190 +          }
  1.1191 +          floorM[j]=floorA[j]=floorM[j]+floorA[j];
  1.1192 +        }
  1.1193 +        /* normalize the resulting mag vector */
  1.1194 +        acc[track]=noise_normalize(p,limit,raw[Mi],quant[Mi],floor[Mi],flag[Mi],acc[track],i,jn,iM);
  1.1195 +        track++;
  1.1196 +      }
  1.1197 +    }
  1.1198 +  }
  1.1199 +
  1.1200 +  for(i=0;i<vi->coupling_steps;i++){
  1.1201 +    /* make sure coupling a zero and a nonzero channel results in two
  1.1202 +       nonzero channels. */
  1.1203 +    if(nonzero[vi->coupling_mag[i]] ||
  1.1204 +       nonzero[vi->coupling_ang[i]]){
  1.1205 +      nonzero[vi->coupling_mag[i]]=1;
  1.1206 +      nonzero[vi->coupling_ang[i]]=1;
  1.1207 +    }
  1.1208 +  }
  1.1209 +}