Merging the postbeta2 branch onto the mainline.
[platform/upstream/libvorbis.git] / lib / psy.c
1 /********************************************************************
2  *                                                                  *
3  * THIS FILE IS PART OF THE Ogg Vorbis SOFTWARE CODEC SOURCE CODE.  *
4  * USE, DISTRIBUTION AND REPRODUCTION OF THIS SOURCE IS GOVERNED BY *
5  * THE GNU PUBLIC LICENSE 2, WHICH IS INCLUDED WITH THIS SOURCE.    *
6  * PLEASE READ THESE TERMS DISTRIBUTING.                            *
7  *                                                                  *
8  * THE OggSQUISH SOURCE CODE IS (C) COPYRIGHT 1994-2000             *
9  * by Monty <monty@xiph.org> and The XIPHOPHORUS Company            *
10  * http://www.xiph.org/                                             *
11  *                                                                  *
12  ********************************************************************
13
14  function: psychoacoustics not including preecho
15  last mod: $Id: psy.c,v 1.29 2000/10/12 03:12:53 xiphmont Exp $
16
17  ********************************************************************/
18
19 #include <stdlib.h>
20 #include <math.h>
21 #include <string.h>
22 #include "vorbis/codec.h"
23
24 #include "masking.h"
25 #include "psy.h"
26 #include "os.h"
27 #include "lpc.h"
28 #include "smallft.h"
29 #include "scales.h"
30 #include "misc.h"
31
32 /* Why Bark scale for encoding but not masking computation? Because
33    masking has a strong harmonic dependancy */
34
35 /* the beginnings of real psychoacoustic infrastructure.  This is
36    still not tightly tuned */
37 void _vi_psy_free(vorbis_info_psy *i){
38   if(i){
39     memset(i,0,sizeof(vorbis_info_psy));
40     free(i);
41   }
42 }
43
44 /* Set up decibel threshhold slopes on a Bark frequency scale */
45 /* ATH is the only bit left on a Bark scale.  No reason to change it
46    right now */
47 static void set_curve(float *ref,float *c,int n, float crate){
48   int i,j=0;
49
50   for(i=0;i<MAX_BARK-1;i++){
51     int endpos=rint(fromBARK(i+1)*2*n/crate);
52     float base=ref[i];
53     if(j<endpos){
54       float delta=(ref[i+1]-base)/(endpos-j);
55       for(;j<endpos && j<n;j++){
56         c[j]=base;
57         base+=delta;
58       }
59     }
60   }
61 }
62
63 static void min_curve(float *c,
64                        float *c2){
65   int i;  
66   for(i=0;i<EHMER_MAX;i++)if(c2[i]<c[i])c[i]=c2[i];
67 }
68 static void max_curve(float *c,
69                        float *c2){
70   int i;  
71   for(i=0;i<EHMER_MAX;i++)if(c2[i]>c[i])c[i]=c2[i];
72 }
73
74 static void attenuate_curve(float *c,float att){
75   int i;
76   for(i=0;i<EHMER_MAX;i++)
77     c[i]+=att;
78 }
79
80 static void linear_curve(float *c){
81   int i;  
82   for(i=0;i<EHMER_MAX;i++)
83     if(c[i]<=-200.)
84       c[i]=0.;
85     else
86       c[i]=fromdB(c[i]);
87 }
88
89 static void interp_curve(float *c,float *c1,float *c2,float del){
90   int i;
91   for(i=0;i<EHMER_MAX;i++)
92     c[i]=c2[i]*del+c1[i]*(1.-del);
93 }
94
95 static void setup_curve(float **c,
96                         int band,
97                         float *curveatt_dB){
98   int i,j;
99   float ath[EHMER_MAX];
100   float tempc[P_LEVELS][EHMER_MAX];
101
102   memcpy(c[0],c[4],sizeof(float)*EHMER_MAX);
103   memcpy(c[2],c[4],sizeof(float)*EHMER_MAX);
104
105   /* we add back in the ATH to avoid low level curves falling off to
106      -infinity and unneccessarily cutting off high level curves in the
107      curve limiting (last step).  But again, remember... a half-band's
108      settings must be valid over the whole band, and it's better to
109      mask too little than too much, so be pessimal. */
110
111   for(i=0;i<EHMER_MAX;i++){
112     float oc_min=band*.5-1+(i-EHMER_OFFSET)*.125;
113     float oc_max=band*.5-1+(i-EHMER_OFFSET+1)*.125;
114     float bark=toBARK(fromOC(oc_min));
115     int ibark=floor(bark);
116     float del=bark-ibark;
117     float ath_min,ath_max;
118
119     if(ibark<26)
120       ath_min=ATH_Bark_dB[ibark]*(1.-del)+ATH_Bark_dB[ibark+1]*del;
121     else
122       ath_min=200.;
123
124     bark=toBARK(fromOC(oc_max));
125     ibark=floor(bark);
126     del=bark-ibark;
127
128     if(ibark<26)
129       ath_max=ATH_Bark_dB[ibark]*(1.-del)+ATH_Bark_dB[ibark+1]*del;
130     else
131       ath_max=200.;
132
133     ath[i]=min(ath_min,ath_max);
134   }
135
136   /* The c array is comes in as dB curves at 20 40 60 80 100 dB.
137      interpolate intermediate dB curves */
138   for(i=1;i<P_LEVELS;i+=2){
139     interp_curve(c[i],c[i-1],c[i+1],.5);
140   }
141
142   /* normalize curves so the driving amplitude is 0dB */
143   /* make temp curves with the ATH overlayed */
144   for(i=0;i<P_LEVELS;i++){
145     attenuate_curve(c[i],curveatt_dB[i]);
146     memcpy(tempc[i],ath,EHMER_MAX*sizeof(float));
147     attenuate_curve(tempc[i],-i*10.);
148     max_curve(tempc[i],c[i]);
149   }
150
151   /* Now limit the louder curves.
152
153      the idea is this: We don't know what the playback attenuation
154      will be; 0dB SL moves every time the user twiddles the volume
155      knob. So that means we have to use a single 'most pessimal' curve
156      for all masking amplitudes, right?  Wrong.  The *loudest* sound
157      can be in (we assume) a range of ...+100dB] SL.  However, sounds
158      20dB down will be in a range ...+80], 40dB down is from ...+60],
159      etc... */
160
161   for(i=P_LEVELS-1;i>0;i--){
162     for(j=0;j<i;j++)
163       min_curve(c[i],tempc[j]);
164   }
165
166   /* take things out of dB domain into linear amplitude */
167   for(i=0;i<P_LEVELS;i++)
168     linear_curve(c[i]);
169       
170 }
171
172 void _vp_psy_init(vorbis_look_psy *p,vorbis_info_psy *vi,int n,long rate){
173   long i,j;
174   memset(p,0,sizeof(vorbis_look_psy));
175   p->ath=malloc(n*sizeof(float));
176   p->octave=malloc(n*sizeof(int));
177   p->bark=malloc(n*sizeof(float));
178   p->vi=vi;
179   p->n=n;
180
181   /* set up the lookups for a given blocksize and sample rate */
182   /* Vorbis max sample rate is limited by 26 Bark (54kHz) */
183   set_curve(ATH_Bark_dB, p->ath,n,rate);
184   for(i=0;i<n;i++)
185     p->ath[i]=fromdB(p->ath[i]);
186   for(i=0;i<n;i++)
187     p->bark[i]=toBARK(rate/(2*n)*i); 
188
189   for(i=0;i<n;i++){
190     int oc=toOC((i+.5)*rate/(2*n))*2.+2; /* half octaves, actually */
191     if(oc<0)oc=0;
192     if(oc>=P_BANDS)oc=P_BANDS-1;
193     p->octave[i]=oc;
194   }  
195
196   p->tonecurves=malloc(P_BANDS*sizeof(float **));
197   p->noiseatt=malloc(P_BANDS*sizeof(float **));
198   p->peakatt=malloc(P_BANDS*sizeof(float *));
199   for(i=0;i<P_BANDS;i++){
200     p->tonecurves[i]=malloc(P_LEVELS*sizeof(float *));
201     p->noiseatt[i]=malloc(P_LEVELS*sizeof(float));
202     p->peakatt[i]=malloc(P_LEVELS*sizeof(float));
203   }
204
205   for(i=0;i<P_BANDS;i++)
206     for(j=0;j<P_LEVELS;j++){
207       p->tonecurves[i][j]=malloc(EHMER_MAX*sizeof(float));
208     }
209
210   /* OK, yeah, this was a silly way to do it */
211   memcpy(p->tonecurves[0][4],tone_125_40dB_SL,sizeof(float)*EHMER_MAX);
212   memcpy(p->tonecurves[0][6],tone_125_60dB_SL,sizeof(float)*EHMER_MAX);
213   memcpy(p->tonecurves[0][8],tone_125_80dB_SL,sizeof(float)*EHMER_MAX);
214   memcpy(p->tonecurves[0][10],tone_125_100dB_SL,sizeof(float)*EHMER_MAX);
215
216   memcpy(p->tonecurves[2][4],tone_125_40dB_SL,sizeof(float)*EHMER_MAX);
217   memcpy(p->tonecurves[2][6],tone_125_60dB_SL,sizeof(float)*EHMER_MAX);
218   memcpy(p->tonecurves[2][8],tone_125_80dB_SL,sizeof(float)*EHMER_MAX);
219   memcpy(p->tonecurves[2][10],tone_125_100dB_SL,sizeof(float)*EHMER_MAX);
220
221   memcpy(p->tonecurves[4][4],tone_250_40dB_SL,sizeof(float)*EHMER_MAX);
222   memcpy(p->tonecurves[4][6],tone_250_60dB_SL,sizeof(float)*EHMER_MAX);
223   memcpy(p->tonecurves[4][8],tone_250_80dB_SL,sizeof(float)*EHMER_MAX);
224   memcpy(p->tonecurves[4][10],tone_250_100dB_SL,sizeof(float)*EHMER_MAX);
225
226   memcpy(p->tonecurves[6][4],tone_500_40dB_SL,sizeof(float)*EHMER_MAX);
227   memcpy(p->tonecurves[6][6],tone_500_60dB_SL,sizeof(float)*EHMER_MAX);
228   memcpy(p->tonecurves[6][8],tone_500_80dB_SL,sizeof(float)*EHMER_MAX);
229   memcpy(p->tonecurves[6][10],tone_500_100dB_SL,sizeof(float)*EHMER_MAX);
230
231   memcpy(p->tonecurves[8][4],tone_1000_40dB_SL,sizeof(float)*EHMER_MAX);
232   memcpy(p->tonecurves[8][6],tone_1000_60dB_SL,sizeof(float)*EHMER_MAX);
233   memcpy(p->tonecurves[8][8],tone_1000_80dB_SL,sizeof(float)*EHMER_MAX);
234   memcpy(p->tonecurves[8][10],tone_1000_100dB_SL,sizeof(float)*EHMER_MAX);
235
236   memcpy(p->tonecurves[10][4],tone_2000_40dB_SL,sizeof(float)*EHMER_MAX);
237   memcpy(p->tonecurves[10][6],tone_2000_60dB_SL,sizeof(float)*EHMER_MAX);
238   memcpy(p->tonecurves[10][8],tone_2000_80dB_SL,sizeof(float)*EHMER_MAX);
239   memcpy(p->tonecurves[10][10],tone_2000_100dB_SL,sizeof(float)*EHMER_MAX);
240
241   memcpy(p->tonecurves[12][4],tone_4000_40dB_SL,sizeof(float)*EHMER_MAX);
242   memcpy(p->tonecurves[12][6],tone_4000_60dB_SL,sizeof(float)*EHMER_MAX);
243   memcpy(p->tonecurves[12][8],tone_4000_80dB_SL,sizeof(float)*EHMER_MAX);
244   memcpy(p->tonecurves[12][10],tone_4000_100dB_SL,sizeof(float)*EHMER_MAX);
245
246   memcpy(p->tonecurves[14][4],tone_8000_40dB_SL,sizeof(float)*EHMER_MAX);
247   memcpy(p->tonecurves[14][6],tone_8000_60dB_SL,sizeof(float)*EHMER_MAX);
248   memcpy(p->tonecurves[14][8],tone_8000_80dB_SL,sizeof(float)*EHMER_MAX);
249   memcpy(p->tonecurves[14][10],tone_8000_100dB_SL,sizeof(float)*EHMER_MAX);
250
251   memcpy(p->tonecurves[16][4],tone_8000_40dB_SL,sizeof(float)*EHMER_MAX);
252   memcpy(p->tonecurves[16][6],tone_8000_60dB_SL,sizeof(float)*EHMER_MAX);
253   memcpy(p->tonecurves[16][8],tone_8000_80dB_SL,sizeof(float)*EHMER_MAX);
254   memcpy(p->tonecurves[16][10],tone_8000_100dB_SL,sizeof(float)*EHMER_MAX);
255
256   /* interpolate curves between */
257   for(i=1;i<P_BANDS;i+=2)
258     for(j=4;j<P_LEVELS;j+=2){
259       memcpy(p->tonecurves[i][j],p->tonecurves[i-1][j],EHMER_MAX*sizeof(float));
260       /*interp_curve(p->tonecurves[i][j],
261                    p->tonecurves[i-1][j],
262                    p->tonecurves[i+1][j],.5);*/
263       min_curve(p->tonecurves[i][j],p->tonecurves[i+1][j]);
264       /*min_curve(p->tonecurves[i][j],p->tonecurves[i-1][j]);*/
265     }
266
267   /*for(i=0;i<P_BANDS-1;i++)
268     for(j=4;j<P_LEVELS;j+=2)
269     min_curve(p->tonecurves[i][j],p->tonecurves[i+1][j]);*/
270
271   /* set up the final curves */
272   for(i=0;i<P_BANDS;i++)
273     setup_curve(p->tonecurves[i],i,vi->toneatt[i]);
274
275   /* set up attenuation levels */
276   for(i=0;i<P_BANDS;i++)
277     for(j=0;j<P_LEVELS;j++){
278       p->peakatt[i][j]=fromdB(p->vi->peakatt[i][j]);
279       p->noiseatt[i][j]=fromdB(p->vi->noiseatt[i][j]);
280     }
281
282 }
283
284 void _vp_psy_clear(vorbis_look_psy *p){
285   int i,j;
286   if(p){
287     if(p->ath)free(p->ath);
288     if(p->octave)free(p->octave);
289     if(p->tonecurves){
290       for(i=0;i<P_BANDS;i++){
291         for(j=0;j<P_LEVELS;j++){
292           free(p->tonecurves[i][j]);
293         }
294         free(p->noiseatt[i]);
295         free(p->tonecurves[i]);
296         free(p->peakatt[i]);
297       }
298       free(p->tonecurves);
299       free(p->noiseatt);
300       free(p->peakatt);
301     }
302     memset(p,0,sizeof(vorbis_look_psy));
303   }
304 }
305
306 static void compute_decay_fixed(vorbis_look_psy *p,float *f, float *decay, int n){
307   /* handle decay */
308   int i;
309   float decscale=fromdB(p->vi->decay_coeff*n); 
310   float attscale=1./fromdB(p->vi->attack_coeff); 
311
312   for(i=10;i<n;i++){
313     float pre=decay[i];
314     if(decay[i]){
315       float val=decay[i]*decscale;
316       float att=fabs(f[i]/val);
317
318       if(att>attscale)
319         decay[i]=fabs(f[i]/attscale);
320       else
321         decay[i]=val;
322     }else{
323       decay[i]=fabs(f[i]/attscale);
324     }
325     if(pre>f[i])f[i]=pre;
326   }
327 }
328
329 static long _eights[EHMER_MAX+1]={
330   981,1069,1166,1272,
331   1387,1512,1649,1798,
332   1961,2139,2332,2543,
333   2774,3025,3298,3597,
334   3922,4277,4664,5087,
335   5547,6049,6597,7194,
336   7845,8555,9329,10173,
337   11094,12098,13193,14387,
338   15689,17109,18658,20347,
339   22188,24196,26386,28774,
340   31379,34219,37316,40693,
341   44376,48393,52772,57549,
342   62757,68437,74631,81386,
343   88752,96785,105545,115097,
344   125515};
345
346 static int seed_curve(float *flr,
347                       float **curves,
348                        float amp,float specmax,
349                        int x,int n,float specatt,
350                        int maxEH){
351   int i;
352   float *curve;
353
354   /* make this attenuation adjustable */
355   int choice=(int)((todB(amp)-specmax+specatt)/10.+.5);
356   choice=max(choice,0);
357   choice=min(choice,P_LEVELS-1);
358
359   for(i=maxEH;i>=0;i--)
360     if(((x*_eights[i])>>12)<n)break;
361   maxEH=i;
362   curve=curves[choice];
363
364   for(;i>=0;i--)
365     if(curve[i]>0.)break;
366   
367   for(;i>=0;i--){
368     float lin=curve[i];
369     if(lin>0.){
370       float *fp=flr+((x*_eights[i])>>12);
371       lin*=amp; 
372       if(*fp<lin)*fp=lin;
373     }else break;
374   }    
375   return(maxEH);
376 }
377
378 static void seed_peak(float *flr,
379                       float *att,
380                       float amp,float specmax,
381                       int x,int n,float specatt){
382   int prevx=(x*_eights[16])>>12;
383
384   /* make this attenuation adjustable */
385   int choice=rint((todB(amp)-specmax+specatt)/10.+.5);
386   if(choice<0)choice=0;
387   if(choice>=P_LEVELS)choice=P_LEVELS-1;
388
389   if(prevx<n){
390     float lin=att[choice];
391     if(lin){
392       lin*=amp; 
393       if(flr[prevx]<lin)flr[prevx]=lin;
394     }
395   }
396 }
397
398 static void seed_generic(vorbis_look_psy *p,
399                          float ***curves,
400                          float *f, 
401                          float *flr,
402                          float *seeds,
403                          float specmax){
404   vorbis_info_psy *vi=p->vi;
405   long n=p->n,i;
406   int maxEH=EHMER_MAX-1;
407
408   /* prime the working vector with peak values */
409   /* Use the 125 Hz curve up to 125 Hz and 8kHz curve after 8kHz. */
410   for(i=0;i<n;i++)
411     if(f[i]>flr[i])
412       maxEH=seed_curve(seeds,curves[p->octave[i]],
413                        f[i],specmax,i,n,vi->max_curve_dB,maxEH);
414 }
415
416 static void seed_att(vorbis_look_psy *p,
417                      float **att,
418                      float *f, 
419                      float *flr,
420                      float specmax){
421   vorbis_info_psy *vi=p->vi;
422   long n=p->n,i;
423   
424   for(i=0;i<n;i++)
425     if(f[i]>flr[i])
426       seed_peak(flr,att[p->octave[i]],f[i],
427                 specmax,i,n,vi->max_curve_dB);
428 }
429
430 static void seed_point(vorbis_look_psy *p,
431                      float **att,
432                      float *f, 
433                      float *flr,
434                      float specmax){
435   vorbis_info_psy *vi=p->vi;
436   long n=p->n,i;
437   
438   for(i=0;i<n;i++){
439     /* make this attenuation adjustable */
440     int choice=rint((todB(f[i])-specmax+vi->max_curve_dB)/10.+.5);
441     float lin;
442     if(choice<0)choice=0;
443     if(choice>=P_LEVELS)choice=P_LEVELS-1;
444     lin=att[p->octave[i]][choice]*f[i];
445     if(flr[i]<lin)flr[i]=lin;
446   }
447 }
448
449 /* bleaugh, this is more complicated than it needs to be */
450 static void max_seeds(vorbis_look_psy *p,float *seeds,float *flr){
451   long n=p->n,i,j;
452   long *posstack=alloca(n*sizeof(long));
453   float *ampstack=alloca(n*sizeof(float));
454   long stack=0;
455
456   for(i=0;i<n;i++){
457     if(stack<2){
458       posstack[stack]=i;
459       ampstack[stack++]=seeds[i];
460     }else{
461       while(1){
462         if(seeds[i]<ampstack[stack-1]){
463           posstack[stack]=i;
464           ampstack[stack++]=seeds[i];
465           break;
466         }else{
467           if(i<posstack[stack-1]*1.0905077080){
468             if(stack>1 && ampstack[stack-1]<ampstack[stack-2] &&
469                i<posstack[stack-2]*1.0905077080){
470               /* we completely overlap, making stack-1 irrelevant.  pop it */
471               stack--;
472               continue;
473             }
474           }
475           posstack[stack]=i;
476           ampstack[stack++]=seeds[i];
477           break;
478
479         }
480       }
481     }
482   }
483
484   /* the stack now contains only the positions that are relevant. Scan
485      'em straight through */
486   {
487     long pos=0;
488     for(i=0;i<stack;i++){
489       long endpos;
490       if(i<stack-1 && ampstack[i+1]>ampstack[i]){
491         endpos=posstack[i+1];
492       }else{
493         endpos=posstack[i]*1.0905077080+1; /* +1 is important, else bin 0 is
494                                        discarded in short frames */
495       }
496       if(endpos>n)endpos=n;
497       for(j=pos;j<endpos;j++)
498         if(flr[j]<ampstack[i])
499           flr[j]=ampstack[i];
500       pos=endpos;
501     }
502   }   
503
504   /* there.  Linear time.  I now remember this was on a problem set I
505      had in Grad Skool... I didn't solve it at the time ;-) */
506 }
507
508 static void bark_noise(long n,float *b,float *f,float *noise){
509   long i=1,lo=0,hi=2;
510   float acc=0.,val,del=0.;
511
512   float *norm=alloca(n*sizeof(float));
513
514   memset(noise,0,n*sizeof(float));
515   memset(norm,0,n*sizeof(float));
516
517   while(hi<n){
518     val=todB_nn(f[i]*f[i])+400.;
519     del=1./(i-lo);
520     noise[lo]+=val*del;
521     noise[i]-=val*del;
522     norm[lo]+=del;
523     norm[i]-=del;
524  
525     del=1./(hi-i);
526     noise[i]-=val*del;
527     noise[hi]+=val*del;
528     norm[hi]+=del;
529     norm[i]-=del;
530     
531
532     i++;
533     for(;hi<n && b[hi]-.3<b[i];hi++);
534     for(;lo<i-1 && b[lo]+.3<b[i];lo++);
535     if(i==hi)hi++;
536   }
537
538   {
539     long ilo=i-lo;
540     long hii=hi-i;
541
542     for(;i<n;i++){
543       val=todB_nn(f[i]*f[i])+400.;
544       del=1./(hii);
545       noise[i]-=val*del;
546       norm[i]-=del;
547      
548       del=1./(ilo);
549       noise[i-ilo]+=val*del;
550       noise[i]-=val*del;      
551       norm[i-ilo]+=del;
552       norm[i]-=del;      
553     }
554     for(i=1,lo=n-ilo;lo<n;lo++,i++){
555       val=todB_nn(f[n-i]*f[n-i])+400.;
556       del=1./ilo;
557       noise[lo]+=val*del;
558       norm[lo]+=del;
559     }
560   }
561
562
563   acc=0;
564   val=0;
565
566   for(i=0;i<n;i++){
567     val+=norm[i];
568     norm[i]=val;
569     acc+=noise[i];
570     noise[i]=acc;
571   }
572
573   val=0;
574   acc=0;
575   for(i=0;i<n;i++){
576     val+=norm[i];
577     acc+=noise[i];
578     if(val==0){
579       noise[i]=0.;
580       norm[i]=0;
581     }else{
582       float v=acc/val-400;
583       noise[i]=sqrt(fromdB(v));
584     }
585   }
586 }
587
588 void _vp_compute_mask(vorbis_look_psy *p,float *f, 
589                       float *flr, 
590                       float *decay){
591   float *smooth=alloca(sizeof(float)*p->n);
592   int i,n=p->n;
593   float specmax=0.;
594
595   float *seed=alloca(sizeof(float)*p->n);
596   float *seed2=alloca(sizeof(float)*p->n);
597
598   memset(flr,0,n*sizeof(float));
599
600   /* noise masking */
601   if(p->vi->noisemaskp){
602     memset(seed,0,n*sizeof(float));
603     bark_noise(n,p->bark,f,seed);
604     seed_point(p,p->noiseatt,seed,flr,specmax);
605
606   }
607
608   /* smooth the data is that's called for ********************************/
609   for(i=0;i<n;i++)smooth[i]=fabs(f[i]);
610   if(p->vi->smoothp){
611     /* compute power^.5 of three neighboring bins to smooth for peaks
612        that get split twixt bins/peaks that nail the bin.  This evens
613        out treatment as we're not doing additive masking any longer. */
614     float acc=smooth[0]*smooth[0]+smooth[1]*smooth[1];
615     float prev=smooth[0];
616
617     smooth[0]=sqrt(acc);
618     for(i=1;i<n-1;i++){
619       float this=smooth[i];
620       acc+=smooth[i+1]*smooth[i+1];
621       if(acc<0)acc=0; /* it can happen due to finite precision */
622       smooth[i]=sqrt(acc);
623       acc-=prev*prev;
624       prev=this;
625     }
626     if(acc<0)acc=0; /* in case it happens on the final iteration */
627     smooth[n-1]=sqrt(acc);
628   }
629
630   /* find the highest peak so we know the limits *************************/
631   for(i=0;i<n;i++){
632     if(smooth[i]>specmax)specmax=smooth[i];
633   }
634   specmax=todB(specmax);
635
636   /* set the ATH (floating below specmax by a specified att) */
637   if(p->vi->athp){
638     float att=specmax+p->vi->ath_adjatt;
639     if(att<p->vi->ath_maxatt)att=p->vi->ath_maxatt;
640     att=fromdB(att);
641
642     for(i=0;i<n;i++){
643       float av=p->ath[i]*att;
644       if(av>flr[i])flr[i]=av;
645     }
646   }
647
648   /* peak attenuation ******/
649   if(p->vi->peakattp){
650     memset(seed,0,n*sizeof(float));
651     seed_att(p,p->peakatt,smooth,seed,specmax);
652     max_seeds(p,seed,flr);
653   }
654
655   /* tone masking */
656   if(p->vi->tonemaskp){
657     memset(seed,0,n*sizeof(float));
658     memset(seed2,0,n*sizeof(float));
659
660     seed_generic(p,p->tonecurves,smooth,flr,seed2,specmax);
661     max_seeds(p,seed2,seed2);
662
663     for(i=0;i<n;i++)if(seed2[i]<flr[i])seed2[i]=flr[i];
664     for(i=0;i<n;i++)if(seed2[i]<decay[i])seed2[i]=decay[i];
665
666     seed_generic(p,p->tonecurves,smooth,seed2,seed,specmax);
667     max_seeds(p,seed,seed);
668     
669     if(p->vi->decayp)
670       compute_decay_fixed(p,seed,decay,n);
671     
672     for(i=0;i<n;i++)if(flr[i]<seed[i])flr[i]=seed[i];
673     
674   }
675
676   /* doing this here is clean, but we need to find a faster way to do
677      it than to just tack it on */
678
679   for(i=0;i<n;i++)if(2.*f[i]>flr[i] || -2.*f[i]>flr[i])break;
680   if(i==n)memset(flr,0,sizeof(float)*n);
681
682 }
683
684
685 /* this applies the floor and (optionally) tries to preserve noise
686    energy in low resolution portions of the spectrum */
687 /* f and flr are *linear* scale, not dB */
688 void _vp_apply_floor(vorbis_look_psy *p,float *f, float *flr){
689   float *work=alloca(p->n*sizeof(float));
690   int j;
691
692   /* subtract the floor */
693   for(j=0;j<p->n;j++){
694     if(flr[j]<=0)
695       work[j]=0.;
696     else
697       work[j]=f[j]/flr[j];
698   }
699
700   memcpy(f,work,p->n*sizeof(float));
701 }
702
703