4 * Copyright (c) 1999 Mark Taylor
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
22 /* $Id: psymodel.c,v 1.2 2006/02/09 16:56:23 kramm Exp $ */
29 This routine computes the psycho acoustics, delayed by
32 Input: buffer of PCM data (1024 samples).
34 This window should be centered over the 576 sample granule window.
35 The routine will compute the psycho acoustics for
36 this granule, but return the psycho acoustics computed
37 for the *previous* granule. This is because the block
38 type of the previous granule can only be determined
39 after we have computed the psycho acoustics for the following
42 Output: maskings and energies for each scalefactor band.
43 block type, PE, and some correlation measures.
44 The PE is used by CBR modes to determine if extra bits
45 from the bit reservoir should be used. The correlation
46 measures are used to determine mid/side or regular stereo.
51 barks: a non-linear frequency scale. Mapping from frequency to
52 barks is given by freq2bark()
54 scalefactor bands: The spectrum (frequencies) are broken into
55 SBMAX "scalefactor bands". Thes bands
56 are determined by the MPEG ISO spec. In
57 the noise shaping/quantization code, we allocate
58 bits among the partition bands to achieve the
61 partition bands: The spectrum is also broken into about
62 64 "partition bands". Each partition
63 band is about .34 barks wide. There are about 2-5
64 partition bands for each scalefactor band.
66 LAME computes all psycho acoustic information for each partition
67 band. Then at the end of the computations, this information
68 is mapped to scalefactor bands. The energy in each scalefactor
69 band is taken as the sum of the energy in all partition bands
70 which overlap the scalefactor band. The maskings can be computed
71 in the same way (and thus represent the average masking in that band)
72 or by taking the minmum value multiplied by the number of
73 partition bands used (which represents a minimum masking in that band).
76 The general outline is as follows:
79 1. compute the energy in each partition band
80 2. compute the tonality in each partition band
81 3. compute the strength of each partion band "masker"
82 4. compute the masking (via the spreading function applied to each masker)
83 5. Modifications for mid/side masking.
85 Each partition band is considiered a "masker". The strength
86 of the i'th masker in band j is given by:
88 s3(bark(i)-bark(j))*strength(i)
90 The strength of the masker is a function of the energy and tonality.
91 The more tonal, the less masking. LAME uses a simple linear formula
92 (controlled by NMT and TMN) which says the strength is given by the
93 energy divided by a linear function of the tonality.
96 s3() is the "spreading function". It is given by a formula
97 determined via listening tests.
99 The total masking in the j'th partition band is the sum over
100 all maskings i. It is thus given by the convolution of
101 the strength with s3(), the "spreading function."
103 masking(j) = sum_over_i s3(i-j)*strength(i) = s3 o strength
105 where "o" = convolution operator. s3 is given by a formula determined
106 via listening tests. It is normalized so that s3 o 1 = 1.
108 Note: instead of a simple convolution, LAME also has the
109 option of using "additive masking"
111 The most critical part is step 2, computing the tonality of each
112 partition band. LAME has two tonality estimators. The first
113 is based on the ISO spec, and measures how predictiable the
114 signal is over time. The more predictable, the more tonal.
115 The second measure is based on looking at the spectrum of
116 a single granule. The more peaky the spectrum, the more
117 tonal. By most indications, the latter approach is better.
119 Finally, in step 5, the maskings for the mid and side
120 channel are possibly increased. Under certain circumstances,
121 noise in the mid & side channels is assumed to also
122 be masked by strong maskers in the L or R channels.
125 Other data computed by the psy-model:
127 ms_ratio side-channel / mid-channel masking ratio (for previous granule)
128 ms_ratio_next side-channel / mid-channel masking ratio for this granule
130 percep_entropy[2] L and R values (prev granule) of PE - A measure of how
131 much pre-echo is in the previous granule
132 percep_entropy_MS[2] mid and side channel values (prev granule) of percep_entropy
133 energy[4] L,R,M,S energy in each channel, prev granule
134 blocktype_d[2] block type to use for previous granule
143 #include "config_static.h"
147 #include "psymodel.h"
163 /* size of each partition band, in barks: */
168 /* AAC values, results in more masking over MP3 values */
177 #define NBPSY_l (SBMAX_l)
178 #define NBPSY_s (SBMAX_s)
182 #define LN_TO_LOG10 (M_LN10/10)
184 #define LN_TO_LOG10 0.2302585093
188 psycho_loudness_approx( FLOAT *energy, lame_global_flags *gfp );
195 L3psycho_anal. Compute psycho acoustics.
197 Data returned to the calling program must be delayed by one
200 This is done in two places.
201 If we do not need to know the blocktype, the copying
202 can be done here at the top of the program: we copy the data for
203 the last granule (computed during the last call) before it is
204 overwritten with the new data. It looks like this:
206 0. static psymodel_data
207 1. calling_program_data = psymodel_data
208 2. compute psymodel_data
210 For data which needs to know the blocktype, the copying must be
211 done at the end of this loop, and the old values must be saved:
213 0. static psymodel_data_old
214 1. compute psymodel_data
215 2. compute possible block type of this granule
216 3. compute final block type of previous granule based on #2.
217 4. calling_program_data = psymodel_data_old
218 5. psymodel_data_old = psymodel_data
220 int L3psycho_anal( lame_global_flags * gfp,
221 const sample_t *buffer[2], int gr_out,
223 FLOAT8 *ms_ratio_next,
224 III_psy_ratio masking_ratio[2][2],
225 III_psy_ratio masking_MS_ratio[2][2],
226 FLOAT8 percep_entropy[2],FLOAT8 percep_MS_entropy[2],
230 /* to get a good cache performance, one has to think about
231 * the sequence, in which the variables are used.
232 * (Note: these static variables have been moved to the gfc-> struct,
233 * and their order in memory is layed out in util.h)
235 lame_internal_flags *gfc=gfp->internal_flags;
238 /* fft and energy calculation */
239 FLOAT (*wsamp_l)[BLKSIZE];
240 FLOAT (*wsamp_s)[3][BLKSIZE_s];
248 FLOAT8 ms_ratio_l=0,ms_ratio_s=0;
251 int blocktype[2],uselongblock[2];
253 /* usual variables like loop indices, etc.. */
259 if(gfc->psymodel_init==0) {
262 gfc->psymodel_init=1;
264 for (chn = 0; chn < 4; ++chn )
265 for (b = 0; b < CBANDS; ++b )
266 gfc->nb_s1[chn][b] = gfc->nb_s2[chn][b] = 1.0;
274 numchn = gfc->channels_out;
275 /* chn=2 and 3 = Mid and Side channels */
276 if (gfp->mode == JOINT_STEREO) numchn=4;
278 for (chn=0; chn<numchn; chn++) {
279 for (i=0; i<numchn; ++i) {
280 energy[chn]=gfc->tot_ener[chn];
284 for (chn=0; chn<numchn; chn++) {
286 /* there is a one granule delay. Copy maskings computed last call
287 * into masking_ratio to return to calling program.
291 percep_entropy [chn] = gfc -> pe [chn];
292 masking_ratio [gr_out] [chn] .en = gfc -> en [chn];
293 masking_ratio [gr_out] [chn] .thm = gfc -> thm [chn];
296 percep_MS_entropy [chn-2] = gfc -> pe [chn];
297 masking_MS_ratio [gr_out] [chn-2].en = gfc -> en [chn];
298 masking_MS_ratio [gr_out] [chn-2].thm = gfc -> thm [chn];
303 /**********************************************************************
305 **********************************************************************/
306 wsamp_s = gfc->wsamp_S+(chn & 1);
307 wsamp_l = gfc->wsamp_L+(chn & 1);
309 fft_long ( gfc, *wsamp_l, chn, buffer);
310 fft_short( gfc, *wsamp_s, chn, buffer);
312 /* FFT data for mid and side channel is derived from L & R */
315 for (j = BLKSIZE-1; j >=0 ; --j)
317 FLOAT l = gfc->wsamp_L[0][j];
318 FLOAT r = gfc->wsamp_L[1][j];
319 gfc->wsamp_L[0][j] = (l+r)*(FLOAT)(SQRT2*0.5);
320 gfc->wsamp_L[1][j] = (l-r)*(FLOAT)(SQRT2*0.5);
322 for (b = 2; b >= 0; --b)
324 for (j = BLKSIZE_s-1; j >= 0 ; --j)
326 FLOAT l = gfc->wsamp_S[0][b][j];
327 FLOAT r = gfc->wsamp_S[1][b][j];
328 gfc->wsamp_S[0][b][j] = (l+r)*(FLOAT)(SQRT2*0.5);
329 gfc->wsamp_S[1][b][j] = (l-r)*(FLOAT)(SQRT2*0.5);
335 /**********************************************************************
337 **********************************************************************/
341 gfc->energy[0] = (*wsamp_l)[0];
342 gfc->energy[0] *= gfc->energy[0];
344 gfc->tot_ener[chn] = gfc->energy[0]; /* sum total energy at nearly no extra cost */
346 for (j=BLKSIZE/2-1; j >= 0; --j)
348 FLOAT re = (*wsamp_l)[BLKSIZE/2-j];
349 FLOAT im = (*wsamp_l)[BLKSIZE/2+j];
350 gfc->energy[BLKSIZE/2-j] = (re * re + im * im) * (FLOAT)0.5;
352 if (BLKSIZE/2-j > 10)
353 gfc->tot_ener[chn] += gfc->energy[BLKSIZE/2-j];
355 for (b = 2; b >= 0; --b)
357 gfc->energy_s[b][0] = (*wsamp_s)[b][0];
358 gfc->energy_s[b][0] *= gfc->energy_s [b][0];
359 for (j=BLKSIZE_s/2-1; j >= 0; --j)
361 FLOAT re = (*wsamp_s)[b][BLKSIZE_s/2-j];
362 FLOAT im = (*wsamp_s)[b][BLKSIZE_s/2+j];
363 gfc->energy_s[b][BLKSIZE_s/2-j] = (re * re + im * im) * (FLOAT)0.5;
367 #if defined(HAVE_GTK)
369 for (j=0; j<HBLKSIZE ; j++) {
370 gfc->pinfo->energy[gr_out][chn][j]=gfc->energy_save[chn][j];
371 gfc->energy_save[chn][j]=gfc->energy[j];
378 /**********************************************************************
379 * compute loudness approximation (used for ATH auto-level adjustment)
380 **********************************************************************/
381 if( gfp->athaa_loudapprox == 2 ) {
382 if( chn < 2 ) { /* no loudness for mid and side channels */
383 gfc->loudness_sq[gr_out][chn] = gfc->loudness_sq_save[chn];
384 gfc->loudness_sq_save[chn]
385 = psycho_loudness_approx( gfc->energy, gfp);
391 /**********************************************************************
392 * compute unpredicatability of first six spectral lines *
393 **********************************************************************/
394 for ( j = 0; j < gfc->cw_lower_index; j++ )
395 { /* calculate unpredictability measure cw */
399 FLOAT numre, numim, den;
401 a2 = gfc-> ax_sav[chn][1][j];
402 b2 = gfc-> bx_sav[chn][1][j];
403 r2 = gfc-> rx_sav[chn][1][j];
404 a1 = gfc-> ax_sav[chn][1][j] = gfc-> ax_sav[chn][0][j];
405 b1 = gfc-> bx_sav[chn][1][j] = gfc-> bx_sav[chn][0][j];
406 r1 = gfc-> rx_sav[chn][1][j] = gfc-> rx_sav[chn][0][j];
407 an = gfc-> ax_sav[chn][0][j] = (*wsamp_l)[j];
408 bn = gfc-> bx_sav[chn][0][j] = j==0 ? (*wsamp_l)[0] : (*wsamp_l)[BLKSIZE-j];
409 rn = gfc-> rx_sav[chn][0][j] = sqrt(gfc->energy[j]);
411 { /* square (x1,y1) */
414 numim = (a1*a1-b1*b1)*(FLOAT)0.5;
423 { /* multiply by (x2,-y2) */
425 FLOAT tmp2 = (numim+numre)*(a2+b2)*(FLOAT)0.5;
426 FLOAT tmp1 = -a2*numre+tmp2;
427 numre = -b2*numim+tmp2;
435 { /* r-prime factor */
436 FLOAT tmp = (2*r1-r2)/den;
440 den=rn+fabs(2*r1-r2);
442 numre = (an+bn)*(FLOAT)0.5-numre;
443 numim = (an-bn)*(FLOAT)0.5-numim;
444 den = sqrt(numre*numre+numim*numim)/den;
451 /**********************************************************************
452 * compute unpredicatibility of next 200 spectral lines *
453 **********************************************************************/
454 for ( j = gfc->cw_lower_index; j < gfc->cw_upper_index; j += 4 )
455 {/* calculate unpredictability measure cw */
457 FLOAT numre, numim, den;
461 { /* square (x1,y1) */
462 r1 = gfc->energy_s[0][k];
464 FLOAT a1 = (*wsamp_s)[0][k];
465 FLOAT b1 = (*wsamp_s)[0][BLKSIZE_s-k]; /* k is never 0 */
467 numim = (a1*a1-b1*b1)*(FLOAT)0.5;
478 { /* multiply by (x2,-y2) */
479 r2 = gfc->energy_s[2][k];
481 FLOAT a2 = (*wsamp_s)[2][k];
482 FLOAT b2 = (*wsamp_s)[2][BLKSIZE_s-k];
485 FLOAT tmp2 = (numim+numre)*(a2+b2)*(FLOAT)0.5;
486 FLOAT tmp1 = -a2*numre+tmp2;
487 numre = -b2*numim+tmp2;
497 { /* r-prime factor */
498 FLOAT tmp = (2*r1-r2)/den;
503 rn = sqrt(gfc->energy_s[1][k]);
504 den=rn+fabs(2*r1-r2);
506 FLOAT an = (*wsamp_s)[1][k];
507 FLOAT bn = (*wsamp_s)[1][BLKSIZE_s-k];
508 numre = (an+bn)*(FLOAT)0.5-numre;
509 numim = (an-bn)*(FLOAT)0.5-numim;
510 den = sqrt(numre*numre+numim*numim)/den;
513 gfc->cw[j+1] = gfc->cw[j+2] = gfc->cw[j+3] = gfc->cw[j] = den;
517 for ( j = 14; j < HBLKSIZE-4; j += 4 )
518 {/* calculate energy from short ffts */
521 for (tot=0, sblock=0; sblock < 3; sblock++)
522 tot+=gfc->energy_s[sblock][k];
523 ave = gfc->energy[j+1]+ gfc->energy[j+2]+ gfc->energy[j+3]+ gfc->energy[j];
525 gfc->energy[j+1] = gfc->energy[j+2] = gfc->energy[j+3] = gfc->energy[j]=tot;
529 /**********************************************************************
530 * Calculate the energy and the unpredictability in the threshold *
531 * calculation partitions *
532 **********************************************************************/
535 for (j = 0; j < gfc->cw_upper_index && gfc->numlines_l[b] && b<gfc->npart_l_orig; )
539 ebb = gfc->energy[j];
540 cbb = gfc->energy[j] * gfc->cw[j];
543 for (i = gfc->numlines_l[b] - 1; i > 0; i--)
545 ebb += gfc->energy[j];
546 cbb += gfc->energy[j] * gfc->cw[j];
554 for (; b < gfc->npart_l_orig; b++ )
556 FLOAT8 ebb = gfc->energy[j++];
557 assert(gfc->numlines_l[b]);
558 for (i = gfc->numlines_l[b] - 1; i > 0; i--)
560 ebb += gfc->energy[j++];
566 /**********************************************************************
567 * convolve the partitioned energy and unpredictability *
568 * with the spreading function, s3_l[b][k](packed into s3_ll) *
569 ******************************************************************** */
570 gfc->pe[chn] = 0; /* calculate percetual entropy */
573 for ( b = 0;b < gfc->npart_l; b++ )
580 for ( k = gfc->s3ind[b][0]; k <= gfc->s3ind[b][1]; k++ )
582 ecb += gfc->s3_ll[kk] * eb[k]; /* sprdngf for Layer III */
583 ctb += gfc->s3_ll[kk] * cb[k];
587 /* calculate the tonality of each threshold calculation partition
588 * calculate the SNR in each threshold calculation partition
589 * tonality = -0.299 - .43*log(ctb/ecb);
590 * tonality = 0: use NMT (lots of masking)
591 * tonality = 1: use TMN (little masking)
595 #define CONV1 (-.299)
602 if (tbb <= exp((1-CONV1)/CONV2))
603 { /* tonality near 1 */
604 tbb = exp(-LN_TO_LOG10 * TMN);
606 else if (tbb >= exp((0-CONV1)/CONV2))
607 {/* tonality near 0 */
608 tbb = exp(-LN_TO_LOG10 * NMT);
612 /* convert to tonality index */
613 /* tonality small: tbb=1 */
614 /* tonality large: tbb=-.299 */
615 tbb = CONV1 + CONV2*log(tbb);
616 tbb = exp(-LN_TO_LOG10 * ( TMN*tbb + (1-tbb)*NMT) );
620 /* at this point, tbb represents the amount the spreading function
621 * will be reduced. The smaller the value, the less masking.
622 * minval[] = 1 (0db) says just use tbb.
623 * minval[]= .01 (-20db) says reduce spreading function by
627 tbb = Min(gfc->minval[b], tbb);
629 /* stabilize tonality estimation */
630 if ( gfc->PSY->tonalityPatch ) {
633 FLOAT8 const x = 1.8699422;
634 FLOAT8 w = gfc->PSY->prvTonRed[b/2] * x;
638 gfc->PSY->prvTonRed[b] = tbb;
643 /* long block pre-echo control. */
644 /* rpelev=2.0, rpelev2=16.0 */
645 /* note: all surges in PE are because of this pre-echo formula
646 * for thr[b]. If it this is not used, PE is always around 600
648 /* dont use long block pre-echo control if previous granule was
649 * a short block. This is to avoid the situation:
650 * frame0: quiet (very low masking)
651 * frame1: surge (triggers short blocks)
652 * frame2: regular frame. looks like pre-echo when compared to
653 * frame0, but all pre-echo was in frame1.
655 /* chn=0,1 L and R channels
656 chn=2,3 S and M channels.
659 if (gfc->blocktype_old[chn>1 ? chn-2 : chn] == SHORT_TYPE )
660 thr[b] = ecb; /* Min(ecb, rpelev*gfc->nb_1[chn][b]); */
662 thr[b] = Min(ecb, Min(rpelev*gfc->nb_1[chn][b],rpelev2*gfc->nb_2[chn][b]) );
666 thrpe = Max(thr[b],gfc->ATH->cb[b]);
668 printf("%i thr=%e ATH=%e \n",b,thr[b],gfc->ATH->cb[b]);
671 gfc->pe[chn] -= gfc->numlines_l[b] * log(thrpe / eb[b]);
674 if ( gfp->VBR == vbr_rh || gfp->VBR == vbr_mtrh ) {
675 thr[b] = Min(ecb, rpelev*gfc->nb_1[chn][b]);
676 if (gfc->blocktype_old[chn>1 ? chn-2 : chn] != SHORT_TYPE )
677 thr[b] = Min(thr[b], rpelev2*gfc->nb_2[chn][b]);
678 thr[b] = Max( thr[b], 1e-37 );
681 gfc->nb_2[chn][b] = gfc->nb_1[chn][b];
682 gfc->nb_1[chn][b] = ecb;
686 /***************************************************************
687 * determine the block type (window type) based on L & R channels
689 ***************************************************************/
690 { /* compute PE for all 4 channels */
691 FLOAT mn,mx,ma=0,mb=0,mc=0;
693 for ( j = HBLKSIZE_s/2; j < HBLKSIZE_s; j ++)
695 ma += gfc->energy_s[0][j];
696 mb += gfc->energy_s[1][j];
697 mc += gfc->energy_s[2][j];
704 /* bit allocation is based on pe. */
706 FLOAT8 tmp = 400*log(mx/(1e-12+mn));
707 if (tmp>gfc->pe[chn]) gfc->pe[chn]=tmp;
710 /* block type is based just on L or R channel */
712 uselongblock[chn] = 1;
714 /* tuned for t1.wav. doesnt effect most other samples */
715 if (gfc->pe[chn] > 3000)
719 {/* big surge of energy - always use short blocks */
720 uselongblock[chn] = 0;
722 else if ((mx > 10*mn) && (gfc->pe[chn] > 1000))
723 {/* medium surge, medium pe - use short blocks */
724 uselongblock[chn] = 0;
727 /* disable short blocks */
728 if (gfp->short_blocks == short_block_dispensed)
730 if (gfp->short_blocks == short_block_forced)
735 #if defined(HAVE_GTK)
737 FLOAT mn,mx,ma=0,mb=0,mc=0;
739 for ( j = HBLKSIZE_s/2; j < HBLKSIZE_s; j ++)
741 ma += gfc->energy_s[0][j];
742 mb += gfc->energy_s[1][j];
743 mc += gfc->energy_s[2][j];
750 gfc->pinfo->ers[gr_out][chn]=gfc->ers_save[chn];
751 gfc->ers_save[chn]=(mx/(1e-12+mn));
752 gfc->pinfo->pe[gr_out][chn]=gfc->pe_save[chn];
753 gfc->pe_save[chn]=gfc->pe[chn];
758 /***************************************************************
759 * compute masking thresholds for both short and long blocks
760 ***************************************************************/
761 /* longblock threshold calculation (part 2) */
762 for ( sb = 0; sb < NBPSY_l; sb++ )
764 FLOAT8 enn = gfc->w1_l[sb] * eb[gfc->bu_l[sb]] + gfc->w2_l[sb] * eb[gfc->bo_l[sb]];
765 FLOAT8 thmm = gfc->w1_l[sb] *thr[gfc->bu_l[sb]] + gfc->w2_l[sb] * thr[gfc->bo_l[sb]];
767 for ( b = gfc->bu_l[sb]+1; b < gfc->bo_l[sb]; b++ )
773 gfc->en [chn].l[sb] = enn;
774 gfc->thm[chn].l[sb] = thmm;
778 /* threshold calculation for short blocks */
779 for ( sblock = 0; sblock < 3; sblock++ )
782 for ( b = 0; b < gfc->npart_s_orig; b++ )
784 FLOAT ecb = gfc->energy_s[sblock][j++];
785 for (i = 1 ; i<gfc->numlines_s[b]; ++i)
787 ecb += gfc->energy_s[sblock][j++];
793 for ( b = 0; b < gfc->npart_s; b++ )
796 for ( k = gfc->s3ind_s[b][0]; k <= gfc->s3ind_s[b][1]; k++ ) {
797 ecb += gfc->s3_ss[kk++] * eb[k];
799 ecb *= gfc->SNR_s[b];
802 if ( gfp->VBR == vbr_off || gfp->VBR == vbr_abr ) {
803 /* this looks like a BUG to me. robert */
804 thr[b] = Max (1e-6, ecb);
807 thr[b] = Min( ecb, rpelev_s * gfc->nb_s1[chn][b] );
808 if (gfc->blocktype_old[chn>1 ? chn-2 : chn] == SHORT_TYPE ) {
809 thr[b] = Min( thr[b], rpelev2_s * gfc->nb_s2[chn][b] );
811 thr[b] = Max( thr[b], 1e-37 );
812 gfc->nb_s2[chn][b] = gfc->nb_s1[chn][b];
813 gfc->nb_s1[chn][b] = ecb;
818 for ( sb = 0; sb < NBPSY_s; sb++ )
820 FLOAT8 enn = gfc->w1_s[sb] * eb[gfc->bu_s[sb]] + gfc->w2_s[sb] * eb[gfc->bo_s[sb]];
821 FLOAT8 thmm = gfc->w1_s[sb] *thr[gfc->bu_s[sb]] + gfc->w2_s[sb] * thr[gfc->bo_s[sb]];
823 for ( b = gfc->bu_s[sb]+1; b < gfc->bo_s[sb]; b++ ) {
828 gfc->en [chn].s[sb][sblock] = enn;
829 gfc->thm[chn].s[sb][sblock] = thmm;
833 } /* end loop over chn */
837 /***************************************************************
838 * compute M/S thresholds
839 ***************************************************************/
840 /* compute M/S thresholds from Johnston & Ferreira 1992 ICASSP paper */
841 if (gfp->mode == JOINT_STEREO) {
842 FLOAT8 rside,rmid,mld;
843 int chmid=2,chside=3;
845 for ( sb = 0; sb < NBPSY_l; sb++ ) {
846 /* use this fix if L & R masking differs by 2db or less */
847 /* if db = 10*log10(x2/x1) < 2 */
848 /* if (x2 < 1.58*x1) { */
849 if (gfc->thm[0].l[sb] <= 1.58*gfc->thm[1].l[sb]
850 && gfc->thm[1].l[sb] <= 1.58*gfc->thm[0].l[sb]) {
852 mld = gfc->mld_l[sb]*gfc->en[chside].l[sb];
853 rmid = Max(gfc->thm[chmid].l[sb], Min(gfc->thm[chside].l[sb],mld));
855 mld = gfc->mld_l[sb]*gfc->en[chmid].l[sb];
856 rside = Max(gfc->thm[chside].l[sb],Min(gfc->thm[chmid].l[sb],mld));
858 gfc->thm[chmid].l[sb]=rmid;
859 gfc->thm[chside].l[sb]=rside;
862 for ( sb = 0; sb < NBPSY_s; sb++ ) {
863 for ( sblock = 0; sblock < 3; sblock++ ) {
864 if (gfc->thm[0].s[sb][sblock] <= 1.58*gfc->thm[1].s[sb][sblock]
865 && gfc->thm[1].s[sb][sblock] <= 1.58*gfc->thm[0].s[sb][sblock]) {
867 mld = gfc->mld_s[sb]*gfc->en[chside].s[sb][sblock];
868 rmid = Max(gfc->thm[chmid].s[sb][sblock],Min(gfc->thm[chside].s[sb][sblock],mld));
870 mld = gfc->mld_s[sb]*gfc->en[chmid].s[sb][sblock];
871 rside = Max(gfc->thm[chside].s[sb][sblock],Min(gfc->thm[chmid].s[sb][sblock],mld));
873 gfc->thm[chmid].s[sb][sblock]=rmid;
874 gfc->thm[chside].s[sb][sblock]=rside;
882 /***************************************************************
883 * Adjust M/S maskings if user set "msfix"
884 ***************************************************************/
885 /* Naoki Shibata 2000 */
886 if (numchn == 4 && gfp->msfix!=0) {
887 FLOAT msfix = gfp->msfix;
889 for ( sb = 0; sb < NBPSY_l; sb++ )
891 FLOAT8 thmL,thmR,thmM,thmS,ath;
892 ath = (gfc->ATH->cb[(gfc->bu_l[sb] + gfc->bo_l[sb])/2])*pow(10,-gfp->ATHlower/10.0);
893 thmL = Max(gfc->thm[0].l[sb],ath);
894 thmR = Max(gfc->thm[1].l[sb],ath);
895 thmM = Max(gfc->thm[2].l[sb],ath);
896 thmS = Max(gfc->thm[3].l[sb],ath);
898 if (thmL*msfix < (thmM+thmS)/2) {
899 FLOAT8 f = thmL*msfix / ((thmM+thmS)/2);
903 if (thmR*msfix < (thmM+thmS)/2) {
904 FLOAT8 f = thmR*msfix / ((thmM+thmS)/2);
909 gfc->thm[2].l[sb] = Min(thmM,gfc->thm[2].l[sb]);
910 gfc->thm[3].l[sb] = Min(thmS,gfc->thm[3].l[sb]);
913 for ( sb = 0; sb < NBPSY_s; sb++ ) {
914 for ( sblock = 0; sblock < 3; sblock++ ) {
915 FLOAT8 thmL,thmR,thmM,thmS,ath;
916 ath = (gfc->ATH->cb[(gfc->bu_s[sb] + gfc->bo_s[sb])/2])*pow(10,-gfp->ATHlower/10.0);
917 thmL = Max(gfc->thm[0].s[sb][sblock],ath);
918 thmR = Max(gfc->thm[1].s[sb][sblock],ath);
919 thmM = Max(gfc->thm[2].s[sb][sblock],ath);
920 thmS = Max(gfc->thm[3].s[sb][sblock],ath);
922 if (thmL*msfix < (thmM+thmS)/2) {
923 FLOAT8 f = thmL*msfix / ((thmM+thmS)/2);
927 if (thmR*msfix < (thmM+thmS)/2) {
928 FLOAT8 f = thmR*msfix / ((thmM+thmS)/2);
933 gfc->thm[2].s[sb][sblock] = Min(gfc->thm[2].s[sb][sblock],thmM);
934 gfc->thm[3].s[sb][sblock] = Min(gfc->thm[3].s[sb][sblock],thmS);
949 if (gfp->mode == JOINT_STEREO) {
950 /* determin ms_ratio from masking thresholds*/
951 /* use ms_stereo (ms_ratio < .35) if average thresh. diff < 5 db */
952 FLOAT8 db,x1,x2,sidetot=0,tot=0;
953 for (sb= NBPSY_l/4 ; sb< NBPSY_l; sb ++ ) {
954 x1 = Min(gfc->thm[0].l[sb],gfc->thm[1].l[sb]);
955 x2 = Max(gfc->thm[0].l[sb],gfc->thm[1].l[sb]);
956 /* thresholds difference in db */
957 if (x2 >= 1000*x1) db=3;
958 else db = log10(x2/x1);
959 /* DEBUGF(gfc,"db = %f %e %e \n",db,gfc->thm[0].l[sb],gfc->thm[1].l[sb]);*/
963 ms_ratio_l= (sidetot/tot)*0.7; /* was .35*(sidetot/tot)/5.0*10 */
964 ms_ratio_l = Min(ms_ratio_l,0.5);
967 for ( sblock = 0; sblock < 3; sblock++ )
968 for ( sb = NBPSY_s/4; sb < NBPSY_s; sb++ ) {
969 x1 = Min(gfc->thm[0].s[sb][sblock],gfc->thm[1].s[sb][sblock]);
970 x2 = Max(gfc->thm[0].s[sb][sblock],gfc->thm[1].s[sb][sblock]);
971 /* thresholds difference in db */
972 if (x2 >= 1000*x1) db=3;
973 else db = log10(x2/x1);
977 ms_ratio_s = (sidetot/tot)*0.7; /* was .35*(sidetot/tot)/5.0*10 */
978 ms_ratio_s = Min(ms_ratio_s,.5);
981 /***************************************************************
982 * determine final block type
983 ***************************************************************/
985 for (chn=0; chn<gfc->channels_out; chn++) {
986 blocktype[chn] = NORM_TYPE;
990 if (gfp->short_blocks == short_block_coupled) {
991 /* force both channels to use the same block type */
992 /* this is necessary if the frame is to be encoded in ms_stereo. */
993 /* But even without ms_stereo, FhG does this */
994 int bothlong= (uselongblock[0] && uselongblock[1]);
1003 /* update the blocktype of the previous granule, since it depends on what
1004 * happend in this granule */
1005 for (chn=0; chn<gfc->channels_out; chn++) {
1006 if ( uselongblock[chn])
1007 { /* no attack : use long blocks */
1008 assert( gfc->blocktype_old[chn] != START_TYPE );
1009 switch( gfc->blocktype_old[chn] )
1013 blocktype[chn] = NORM_TYPE;
1016 blocktype[chn] = STOP_TYPE;
1020 /* attack : use short blocks */
1021 blocktype[chn] = SHORT_TYPE;
1022 if ( gfc->blocktype_old[chn] == NORM_TYPE ) {
1023 gfc->blocktype_old[chn] = START_TYPE;
1025 if ( gfc->blocktype_old[chn] == STOP_TYPE ) {
1026 gfc->blocktype_old[chn] = SHORT_TYPE ;
1030 blocktype_d[chn] = gfc->blocktype_old[chn]; /* value returned to calling program */
1031 gfc->blocktype_old[chn] = blocktype[chn]; /* save for next call to l3psy_anal */
1034 if (blocktype_d[0]==2 && blocktype_d[1]==2)
1035 *ms_ratio = gfc->ms_ratio_s_old;
1037 *ms_ratio = gfc->ms_ratio_l_old;
1039 gfc->ms_ratio_s_old = ms_ratio_s;
1040 gfc->ms_ratio_l_old = ms_ratio_l;
1042 /* we dont know the block type of this frame yet - assume long */
1043 *ms_ratio_next = ms_ratio_l;
1048 /* addition of simultaneous masking Naoki Shibata 2000/7 */
1049 inline static FLOAT8 mask_add(FLOAT8 m1,FLOAT8 m2,int k,int b, lame_internal_flags * const gfc)
1051 static const FLOAT8 table1[] = {
1052 3.3246 *3.3246 ,3.23837*3.23837,3.15437*3.15437,3.00412*3.00412,2.86103*2.86103,2.65407*2.65407,2.46209*2.46209,2.284 *2.284 ,
1053 2.11879*2.11879,1.96552*1.96552,1.82335*1.82335,1.69146*1.69146,1.56911*1.56911,1.46658*1.46658,1.37074*1.37074,1.31036*1.31036,
1054 1.25264*1.25264,1.20648*1.20648,1.16203*1.16203,1.12765*1.12765,1.09428*1.09428,1.0659 *1.0659 ,1.03826*1.03826,1.01895*1.01895,
1058 static const FLOAT8 table2[] = {
1059 1.33352*1.33352,1.35879*1.35879,1.38454*1.38454,1.39497*1.39497,1.40548*1.40548,1.3537 *1.3537 ,1.30382*1.30382,1.22321*1.22321,
1063 static const FLOAT8 table3[] = {
1064 2.35364*2.35364,2.29259*2.29259,2.23313*2.23313,2.12675*2.12675,2.02545*2.02545,1.87894*1.87894,1.74303*1.74303,1.61695*1.61695,
1065 1.49999*1.49999,1.39148*1.39148,1.29083*1.29083,1.19746*1.19746,1.11084*1.11084,1.03826*1.03826
1072 if (m1 == 0) return m2;
1076 i = 10*log10(m2 / m1)/10*16;
1077 m = 10*log10((m1+m2)/gfc->ATH->cb[k]);
1081 if (b <= 3) { /* approximately, 1 bark = 3 partitions */
1082 if (i > 8) return m1+m2;
1083 return (m1+m2)*table2[i];
1089 if (i > 24) return m1+m2;
1090 if (i > 13) f = 1; else f = table3[i];
1092 return (m1+m2)*(table1[i]*r+f*(1-r));
1094 if (i > 13) return m1+m2;
1095 return (m1+m2)*table3[i];
1098 if (i > 24) return m1+m2;
1099 return (m1+m2)*table1[i];
1102 int L3psycho_anal_ns( lame_global_flags * gfp,
1103 const sample_t *buffer[2], int gr_out,
1105 FLOAT8 *ms_ratio_next,
1106 III_psy_ratio masking_ratio[2][2],
1107 III_psy_ratio masking_MS_ratio[2][2],
1108 FLOAT8 percep_entropy[2],FLOAT8 percep_MS_entropy[2],
1112 /* to get a good cache performance, one has to think about
1113 * the sequence, in which the variables are used.
1114 * (Note: these static variables have been moved to the gfc-> struct,
1115 * and their order in memory is layed out in util.h)
1117 lame_internal_flags *gfc=gfp->internal_flags;
1119 /* fft and energy calculation */
1120 FLOAT (*wsamp_l)[BLKSIZE];
1121 FLOAT (*wsamp_s)[3][BLKSIZE_s];
1124 FLOAT8 eb[CBANDS],eb2[CBANDS];
1127 FLOAT8 max[CBANDS],avg[CBANDS],tonality2[CBANDS];
1130 FLOAT8 ms_ratio_l=0,ms_ratio_s=0;
1133 int blocktype[2],uselongblock[2];
1135 /* usual variables like loop indices, etc.. */
1140 /* variables used for --nspsytune */
1142 FLOAT ns_hpfsmpl[4][576+576/3+NSFIRLEN];
1143 FLOAT pe_l[4],pe_s[4];
1147 if(gfc->psymodel_init==0) {
1150 gfc->psymodel_init=1;
1154 numchn = gfc->channels_out;
1155 /* chn=2 and 3 = Mid and Side channels */
1156 if (gfp->mode == JOINT_STEREO) numchn=4;
1158 if (gfp->VBR==vbr_off) pcfact = gfc->ResvMax == 0 ? 0 : ((FLOAT)gfc->ResvSize)/gfc->ResvMax*0.5;
1159 else if (gfp->VBR == vbr_rh || gfp->VBR == vbr_mtrh || gfp->VBR == vbr_mt) {
1160 static const FLOAT8 pcQns[10]={1.0,1.0,1.0,0.8,0.6,0.5,0.4,0.3,0.2,0.1};
1161 pcfact = pcQns[gfp->VBR_q];
1164 /**********************************************************************
1165 * Apply HPF of fs/4 to the input signal.
1166 * This is used for attack detection / handling.
1167 **********************************************************************/
1170 static const FLOAT fircoef[] = {
1171 -8.65163e-18,-0.00851586,-6.74764e-18, 0.0209036,
1172 -3.36639e-17,-0.0438162 ,-1.54175e-17, 0.0931738,
1173 -5.52212e-17,-0.313819 , 0.5 ,-0.313819,
1174 -5.52212e-17, 0.0931738 ,-1.54175e-17,-0.0438162,
1175 -3.36639e-17, 0.0209036 ,-6.74764e-18,-0.00851586,
1179 for(chn=0;chn<gfc->channels_out;chn++)
1181 FLOAT firbuf[576+576/3+NSFIRLEN];
1183 /* apply high pass filter of fs/4 */
1185 for(i=-NSFIRLEN;i<576+576/3;i++)
1186 firbuf[i+NSFIRLEN] = buffer[chn][576-350+(i)];
1188 for(i=0;i<576+576/3-NSFIRLEN;i++)
1191 for(j=0;j<NSFIRLEN;j++)
1192 sum += fircoef[j] * firbuf[i+j];
1193 ns_hpfsmpl[chn][i] = sum;
1195 for(;i<576+576/3;i++)
1196 ns_hpfsmpl[chn][i] = 0;
1199 if (gfp->mode == JOINT_STEREO) {
1200 for(i=0;i<576+576/3;i++)
1202 ns_hpfsmpl[2][i] = ns_hpfsmpl[0][i]+ns_hpfsmpl[1][i];
1203 ns_hpfsmpl[3][i] = ns_hpfsmpl[0][i]-ns_hpfsmpl[1][i];
1210 /* there is a one granule delay. Copy maskings computed last call
1211 * into masking_ratio to return to calling program.
1213 for (chn=0; chn<numchn; chn++) {
1214 for (i=0; i<numchn; ++i) {
1215 energy[chn]=gfc->tot_ener[chn];
1220 for (chn=0; chn<numchn; chn++) {
1221 /* there is a one granule delay. Copy maskings computed last call
1222 * into masking_ratio to return to calling program.
1224 pe_l[chn] = gfc->nsPsy.pe_l[chn];
1225 pe_s[chn] = gfc->nsPsy.pe_s[chn];
1229 //percep_entropy [chn] = gfc -> pe [chn];
1230 masking_ratio [gr_out] [chn] .en = gfc -> en [chn];
1231 masking_ratio [gr_out] [chn] .thm = gfc -> thm [chn];
1234 //percep_MS_entropy [chn-2] = gfc -> pe [chn];
1235 masking_MS_ratio [gr_out] [chn-2].en = gfc -> en [chn];
1236 masking_MS_ratio [gr_out] [chn-2].thm = gfc -> thm [chn];
1240 for (chn=0; chn<numchn; chn++) {
1242 /**********************************************************************
1244 **********************************************************************/
1246 wsamp_s = gfc->wsamp_S+(chn & 1);
1247 wsamp_l = gfc->wsamp_L+(chn & 1);
1250 fft_long ( gfc, *wsamp_l, chn, buffer);
1251 fft_short( gfc, *wsamp_s, chn, buffer);
1254 /* FFT data for mid and side channel is derived from L & R */
1258 for (j = BLKSIZE-1; j >=0 ; --j)
1260 FLOAT l = gfc->wsamp_L[0][j];
1261 FLOAT r = gfc->wsamp_L[1][j];
1262 gfc->wsamp_L[0][j] = (l+r)*(FLOAT)(SQRT2*0.5);
1263 gfc->wsamp_L[1][j] = (l-r)*(FLOAT)(SQRT2*0.5);
1265 for (b = 2; b >= 0; --b)
1267 for (j = BLKSIZE_s-1; j >= 0 ; --j)
1269 FLOAT l = gfc->wsamp_S[0][b][j];
1270 FLOAT r = gfc->wsamp_S[1][b][j];
1271 gfc->wsamp_S[0][b][j] = (l+r)*(FLOAT)(SQRT2*0.5);
1272 gfc->wsamp_S[1][b][j] = (l-r)*(FLOAT)(SQRT2*0.5);
1278 /**********************************************************************
1279 * compute energies for each spectral line
1280 **********************************************************************/
1284 gfc->energy[0] = (*wsamp_l)[0];
1285 gfc->energy[0] *= gfc->energy[0];
1287 gfc->tot_ener[chn] = gfc->energy[0]; /* sum total energy at nearly no extra cost */
1289 for (j=BLKSIZE/2-1; j >= 0; --j)
1291 FLOAT re = (*wsamp_l)[BLKSIZE/2-j];
1292 FLOAT im = (*wsamp_l)[BLKSIZE/2+j];
1293 gfc->energy[BLKSIZE/2-j] = (re * re + im * im) * (FLOAT)0.5;
1295 if (BLKSIZE/2-j > 10)
1296 gfc->tot_ener[chn] += gfc->energy[BLKSIZE/2-j];
1302 for (b = 2; b >= 0; --b)
1304 gfc->energy_s[b][0] = (*wsamp_s)[b][0];
1305 gfc->energy_s[b][0] *= gfc->energy_s [b][0];
1306 for (j=BLKSIZE_s/2-1; j >= 0; --j)
1308 FLOAT re = (*wsamp_s)[b][BLKSIZE_s/2-j];
1309 FLOAT im = (*wsamp_s)[b][BLKSIZE_s/2+j];
1310 gfc->energy_s[b][BLKSIZE_s/2-j] = (re * re + im * im) * (FLOAT)0.5;
1315 /* output data for analysis */
1317 #if defined(HAVE_GTK)
1318 if (gfp->analysis) {
1319 for (j=0; j<HBLKSIZE ; j++) {
1320 gfc->pinfo->energy[gr_out][chn][j]=gfc->energy_save[chn][j];
1321 gfc->energy_save[chn][j]=gfc->energy[j];
1327 /**********************************************************************
1328 * compute loudness approximation (used for ATH auto-level adjustment)
1329 **********************************************************************/
1330 if( gfp->athaa_loudapprox == 2 ) {
1331 if( chn < 2 ) { /* no loudness for mid and side channels */
1332 gfc->loudness_sq[gr_out][chn] = gfc->loudness_sq_save[chn];
1333 gfc->loudness_sq_save[chn]
1334 = psycho_loudness_approx( gfc->energy, gfp);
1340 /**********************************************************************
1341 * Calculate the energy and the tonality of each partition.
1342 **********************************************************************/
1344 for (b=0, j=0; b<gfc->npart_l_orig; b++)
1348 ebb = gfc->energy[j];
1349 m = a = gfc->energy[j];
1352 for (i = gfc->numlines_l[b] - 1; i > 0; i--)
1354 FLOAT8 el = gfc->energy[j];
1355 ebb += gfc->energy[j];
1357 m = m < el ? el : m;
1362 avg[b] = a / gfc->numlines_l[b];
1366 for (b=0; b < gfc->npart_l_orig; b++ )
1373 for(k=b-1;k<=b+1;k++)
1375 if (k >= 0 && k < gfc->npart_l_orig) {
1377 c2 += gfc->numlines_l[k];
1379 m = m < max[k] ? max[k] : m;
1384 tonality2[b] = a == 0 ? 0 : (m / a - 1)/(c2-1);
1387 for (b=0; b < gfc->npart_l_orig; b++ )
1390 static FLOAT8 tab[20] =
1391 { 0, 1, 2, 2, 2, 2, 2, 6,9.3,9.3,9.3,9.3,9.3,9.3,9.3,9.3,9.3,9.3,9.3,9.3};
1393 static int init = 1;
1397 tab[j] = pow(10.0,-tab[j]/10.0);
1402 static FLOAT8 tab[20] = {
1403 1,0.79433,0.63096,0.63096,0.63096,0.63096,0.63096,0.25119,0.11749,0.11749,
1404 0.11749,0.11749,0.11749,0.11749,0.11749,0.11749,0.11749,0.11749,0.11749,0.11749
1408 int t = 20*tonality2[b];
1410 eb2[b] = eb[b] * tab[t];
1414 /**********************************************************************
1415 * convolve the partitioned energy and unpredictability
1416 * with the spreading function, s3_l[b][k]
1417 ******************************************************************** */
1420 for ( b = 0;b < gfc->npart_l; b++ )
1424 /**** convolve the partitioned energy with the spreading function ****/
1429 for ( k = gfc->s3ind[b][0]; k <= gfc->s3ind[b][1]; k++ )
1431 ecb = mask_add(ecb,gfc->s3_ll[kk++] * eb2[k],k,k-b,gfc);
1434 ecb *= 0.158489319246111; // pow(10,-0.8)
1438 for ( k = gfc->s3ind[b][0]; k <= gfc->s3ind[b][1]; k++ )
1440 ecb += gfc->s3_ll[kk++] * eb2[k];
1443 ecb *= 0.223872113856834; // pow(10,-0.65);
1446 /**** long block pre-echo control ****/
1448 /* dont use long block pre-echo control if previous granule was
1449 * a short block. This is to avoid the situation:
1450 * frame0: quiet (very low masking)
1451 * frame1: surge (triggers short blocks)
1452 * frame2: regular frame. looks like pre-echo when compared to
1453 * frame0, but all pre-echo was in frame1.
1456 /* chn=0,1 L and R channels
1457 chn=2,3 S and M channels.
1460 #define NS_INTERP(x,y,r) (pow((x),(r))*pow((y),1-(r)))
1462 if (gfc->blocktype_old[chn>1 ? chn-2 : chn] == SHORT_TYPE )
1463 thr[b] = ecb; /* Min(ecb, rpelev*gfc->nb_1[chn][b]); */
1465 thr[b] = NS_INTERP(Min(ecb, Min(rpelev*gfc->nb_1[chn][b],rpelev2*gfc->nb_2[chn][b])),ecb,pcfact);
1467 gfc->nb_2[chn][b] = gfc->nb_1[chn][b];
1468 gfc->nb_1[chn][b] = ecb;
1472 /***************************************************************
1473 * determine the block type (window type)
1474 ***************************************************************/
1478 FLOAT en_subshort[12];
1479 FLOAT attack_intensity[12];
1480 int ns_uselongblock = 1;
1482 /* calculate energies of each sub-shortblocks */
1488 for(j=0;j<576/9;j++)
1490 en_subshort[i] += ns_hpfsmpl[chn][k] * ns_hpfsmpl[chn][k];
1494 if (en_subshort[i] < 100) en_subshort[i] = 100;
1497 /* compare energies between sub-shortblocks */
1499 #define NSATTACKTHRE 150
1500 #define NSATTACKTHRE_S 300
1503 attack_intensity[i] = en_subshort[i] / gfc->nsPsy.last_en_subshort[chn][7+i];
1507 attack_intensity[i] = en_subshort[i] / en_subshort[i-2];
1510 ns_attacks[0] = ns_attacks[1] = ns_attacks[2] = ns_attacks[3] = 0;
1514 if (!ns_attacks[i/3] && attack_intensity[i] > (chn == 3 ? (gfc->presetTune.use ? gfc->presetTune.attackthre_s : NSATTACKTHRE_S)
1515 : (gfc->presetTune.use ? gfc->presetTune.attackthre : NSATTACKTHRE))) ns_attacks[i/3] = (i % 3)+1;
1518 if (ns_attacks[0] && gfc->nsPsy.last_attacks[chn][2]) ns_attacks[0] = 0;
1519 if (ns_attacks[1] && ns_attacks[0]) ns_attacks[1] = 0;
1520 if (ns_attacks[2] && ns_attacks[1]) ns_attacks[2] = 0;
1521 if (ns_attacks[3] && ns_attacks[2]) ns_attacks[3] = 0;
1523 if (gfc->nsPsy.last_attacks[chn][2] == 3 ||
1524 ns_attacks[0] || ns_attacks[1] || ns_attacks[2] || ns_attacks[3]) ns_uselongblock = 0;
1526 if (chn < 4) count++;
1530 gfc->nsPsy.last_en_subshort[chn][i] = en_subshort[i];
1531 gfc->nsPsy.last_attack_intensity[chn][i] = attack_intensity[i];
1534 if (gfp->short_blocks == short_block_dispensed) {
1535 uselongblock[chn] = 1;
1537 else if (gfp->short_blocks == short_block_forced) {
1538 uselongblock[chn] = 0;
1542 uselongblock[chn] = ns_uselongblock;
1544 if (ns_uselongblock == 0) uselongblock[0] = uselongblock[1] = 0;
1549 #if defined(HAVE_GTK)
1550 if (gfp->analysis) {
1551 FLOAT mn,mx,ma=0,mb=0,mc=0;
1553 for ( j = HBLKSIZE_s/2; j < HBLKSIZE_s; j ++)
1555 ma += gfc->energy_s[0][j];
1556 mb += gfc->energy_s[1][j];
1557 mc += gfc->energy_s[2][j];
1564 gfc->pinfo->ers[gr_out][chn]=gfc->ers_save[chn];
1565 gfc->ers_save[chn]=(mx/(1e-12+mn));
1570 /***************************************************************
1571 * compute masking thresholds for long blocks
1572 ***************************************************************/
1574 for ( sb = 0; sb < NBPSY_l; sb++ )
1576 FLOAT8 enn = gfc->w1_l[sb] * eb[gfc->bu_l[sb]] + gfc->w2_l[sb] * eb[gfc->bo_l[sb]];
1577 FLOAT8 thmm = gfc->w1_l[sb] *thr[gfc->bu_l[sb]] + gfc->w2_l[sb] * thr[gfc->bo_l[sb]];
1579 for ( b = gfc->bu_l[sb]+1; b < gfc->bo_l[sb]; b++ )
1585 gfc->en [chn].l[sb] = enn;
1586 gfc->thm[chn].l[sb] = thmm;
1590 /***************************************************************
1591 * compute masking thresholds for short blocks
1592 ***************************************************************/
1594 for ( sblock = 0; sblock < 3; sblock++ )
1597 for ( b = 0; b < gfc->npart_s_orig; b++ )
1599 FLOAT ecb = gfc->energy_s[sblock][j++];
1600 for (i = 1 ; i<gfc->numlines_s[b]; ++i)
1602 ecb += gfc->energy_s[sblock][j++];
1609 for ( b = 0; b < gfc->npart_s; b++ )
1612 for ( k = gfc->s3ind_s[b][0]; k <= gfc->s3ind_s[b][1]; k++ )
1614 ecb += gfc->s3_ss[kk++] * eb[k];
1618 /* this looks like a BUG */
1619 thr[b] = Max (1e-6, ecb);
1621 if (gfp->VBR == vbr_mtrh) {
1622 thr[b] = Min( ecb, rpelev_s * gfc->nb_s1[chn][b] );
1623 if (gfc->blocktype_old[chn>1 ? chn-2 : chn] == SHORT_TYPE ) {
1624 thr[b] = Min( thr[b], rpelev2_s * gfc->nb_s2[chn][b] );
1626 thr[b] = Max( thr[b], 1e-37 );
1627 gfc->nb_s2[chn][b] = gfc->nb_s1[chn][b];
1628 gfc->nb_s1[chn][b] = ecb;
1632 for ( sb = 0; sb < NBPSY_s; sb++ )
1634 FLOAT8 enn = gfc->w1_s[sb] * eb[gfc->bu_s[sb]] + gfc->w2_s[sb] * eb[gfc->bo_s[sb]];
1635 FLOAT8 thmm = gfc->w1_s[sb] *thr[gfc->bu_s[sb]] + gfc->w2_s[sb] * thr[gfc->bo_s[sb]];
1637 for ( b = gfc->bu_s[sb]+1; b < gfc->bo_s[sb]; b++ )
1643 /**** short block pre-echo control ****/
1645 #define NS_PREECHO_ATT0 0.8
1646 #define NS_PREECHO_ATT1 0.6
1647 #define NS_PREECHO_ATT2 0.3
1649 thmm *= NS_PREECHO_ATT0;
1651 if (ns_attacks[sblock] >= 2) {
1653 double p = NS_INTERP(gfc->thm[chn].s[sb][sblock-1],thmm,NS_PREECHO_ATT1*pcfact);
1656 double p = NS_INTERP(gfc->nsPsy.last_thm[chn][sb][2],thmm,NS_PREECHO_ATT1*pcfact);
1659 } else if (ns_attacks[sblock+1] == 1) {
1661 double p = NS_INTERP(gfc->thm[chn].s[sb][sblock-1],thmm,NS_PREECHO_ATT1*pcfact);
1664 double p = NS_INTERP(gfc->nsPsy.last_thm[chn][sb][2],thmm,NS_PREECHO_ATT1*pcfact);
1669 if (ns_attacks[sblock] == 1) {
1670 double p = sblock == 0 ? gfc->nsPsy.last_thm[chn][sb][2] : gfc->thm[chn].s[sb][sblock-1];
1671 p = NS_INTERP(p,thmm,NS_PREECHO_ATT2*pcfact);
1673 } else if ((sblock != 0 && ns_attacks[sblock-1] == 3) ||
1674 (sblock == 0 && gfc->nsPsy.last_attacks[chn][2] == 3)) {
1675 double p = sblock <= 1 ? gfc->nsPsy.last_thm[chn][sb][sblock+1] : gfc->thm[chn].s[sb][0];
1676 p = NS_INTERP(p,thmm,NS_PREECHO_ATT2*pcfact);
1680 gfc->en [chn].s[sb][sblock] = enn;
1681 gfc->thm[chn].s[sb][sblock] = thmm;
1686 /***************************************************************
1687 * save some values for analysis of the next granule
1688 ***************************************************************/
1690 for ( sblock = 0; sblock < 3; sblock++ )
1692 for ( sb = 0; sb < NBPSY_s; sb++ )
1694 gfc->nsPsy.last_thm[chn][sb][sblock] = gfc->thm[chn].s[sb][sblock];
1699 gfc->nsPsy.last_attacks[chn][i] = ns_attacks[i];
1701 } /* end loop over chn */
1705 /***************************************************************
1706 * compute M/S thresholds
1707 ***************************************************************/
1709 /* from Johnston & Ferreira 1992 ICASSP paper */
1711 if ( numchn==4 /* mid/side and r/l */) {
1712 FLOAT8 rside,rmid,mld;
1713 int chmid=2,chside=3;
1715 for ( sb = 0; sb < NBPSY_l; sb++ ) {
1716 /* use this fix if L & R masking differs by 2db or less */
1717 /* if db = 10*log10(x2/x1) < 2 */
1718 /* if (x2 < 1.58*x1) { */
1719 if (gfc->thm[0].l[sb] <= 1.58*gfc->thm[1].l[sb]
1720 && gfc->thm[1].l[sb] <= 1.58*gfc->thm[0].l[sb]) {
1722 mld = gfc->mld_l[sb]*gfc->en[chside].l[sb];
1723 rmid = Max(gfc->thm[chmid].l[sb], Min(gfc->thm[chside].l[sb],mld));
1725 mld = gfc->mld_l[sb]*gfc->en[chmid].l[sb];
1726 rside = Max(gfc->thm[chside].l[sb],Min(gfc->thm[chmid].l[sb],mld));
1728 gfc->thm[chmid].l[sb]=rmid;
1729 gfc->thm[chside].l[sb]=rside;
1732 for ( sb = 0; sb < NBPSY_s; sb++ ) {
1733 for ( sblock = 0; sblock < 3; sblock++ ) {
1734 if (gfc->thm[0].s[sb][sblock] <= 1.58*gfc->thm[1].s[sb][sblock]
1735 && gfc->thm[1].s[sb][sblock] <= 1.58*gfc->thm[0].s[sb][sblock]) {
1737 mld = gfc->mld_s[sb]*gfc->en[chside].s[sb][sblock];
1738 rmid = Max(gfc->thm[chmid].s[sb][sblock],Min(gfc->thm[chside].s[sb][sblock],mld));
1740 mld = gfc->mld_s[sb]*gfc->en[chmid].s[sb][sblock];
1741 rside = Max(gfc->thm[chside].s[sb][sblock],Min(gfc->thm[chmid].s[sb][sblock],mld));
1743 gfc->thm[chmid].s[sb][sblock]=rmid;
1744 gfc->thm[chside].s[sb][sblock]=rside;
1751 /* Naoki Shibata 2000 */
1753 #define NS_MSFIX 3.5
1756 FLOAT msfix = NS_MSFIX;
1757 if (gfc->nsPsy.safejoint) msfix = 1;
1758 if (gfp->msfix) msfix = gfp->msfix;
1760 if (gfc->presetTune.use && gfc->ATH->adjust >=
1761 gfc->presetTune.athadjust_switch_level &&
1762 gfc->presetTune.athadjust_msfix > 0)
1763 msfix = gfc->presetTune.athadjust_msfix;
1765 for ( sb = 0; sb < NBPSY_l; sb++ )
1767 FLOAT8 thmL,thmR,thmM,thmS,ath;
1768 ath = (gfc->ATH->cb[(gfc->bu_l[sb] + gfc->bo_l[sb])/2])*pow(10,-gfp->ATHlower/10.0);
1769 thmL = Max(gfc->thm[0].l[sb],ath);
1770 thmR = Max(gfc->thm[1].l[sb],ath);
1771 thmM = Max(gfc->thm[2].l[sb],ath);
1772 thmS = Max(gfc->thm[3].l[sb],ath);
1774 if (thmL*msfix < (thmM+thmS)/2) {
1775 FLOAT8 f = thmL * (gfc->presetTune.use ? gfc->presetTune.ms_maskadjust : msfix) / ((thmM+thmS)/2);
1779 if (thmR*msfix < (thmM+thmS)/2) {
1780 FLOAT8 f = thmR * (gfc->presetTune.use ? gfc->presetTune.ms_maskadjust : msfix) / ((thmM+thmS)/2);
1785 gfc->thm[2].l[sb] = Min(thmM,gfc->thm[2].l[sb]);
1786 gfc->thm[3].l[sb] = Min(thmS,gfc->thm[3].l[sb]);
1789 for ( sb = 0; sb < NBPSY_s; sb++ ) {
1790 for ( sblock = 0; sblock < 3; sblock++ ) {
1791 FLOAT8 thmL,thmR,thmM,thmS,ath;
1792 ath = (gfc->ATH->cb[(gfc->bu_s[sb] + gfc->bo_s[sb])/2])*pow(10,-gfp->ATHlower/10.0);
1793 thmL = Max(gfc->thm[0].s[sb][sblock],ath);
1794 thmR = Max(gfc->thm[1].s[sb][sblock],ath);
1795 thmM = Max(gfc->thm[2].s[sb][sblock],ath);
1796 thmS = Max(gfc->thm[3].s[sb][sblock],ath);
1798 if (thmL*msfix < (thmM+thmS)/2) {
1799 FLOAT8 f = thmL*msfix / ((thmM+thmS)/2);
1803 if (thmR*msfix < (thmM+thmS)/2) {
1804 FLOAT8 f = thmR*msfix / ((thmM+thmS)/2);
1809 gfc->thm[2].s[sb][sblock] = Min(gfc->thm[2].s[sb][sblock],thmM);
1810 gfc->thm[3].s[sb][sblock] = Min(gfc->thm[3].s[sb][sblock],thmS);
1819 /***************************************************************
1820 * compute estimation of the amount of bit used in the granule
1821 ***************************************************************/
1823 for(chn=0;chn<numchn;chn++)
1826 static FLOAT8 regcoef[] = {
1827 1124.23,10.0583,10.7484,7.29006,16.2714,6.2345,4.09743,3.05468,3.33196,2.54688,
1828 3.68168,5.83109,2.93817,-8.03277,-10.8458,8.48777,9.13182,2.05212,8.6674,50.3937,73.267,97.5664,0
1831 FLOAT8 msum = regcoef[0]/4;
1834 for ( sb = 0; sb < NBPSY_l; sb++ )
1838 if (gfc->thm[chn].l[sb]*gfc->masking_lower != 0 &&
1839 gfc->en[chn].l[sb]/(gfc->thm[chn].l[sb]*gfc->masking_lower) > 1)
1840 t = log(gfc->en[chn].l[sb]/(gfc->thm[chn].l[sb]*gfc->masking_lower));
1843 msum += regcoef[sb+1] * t;
1846 gfc->nsPsy.pe_l[chn] = msum;
1850 static FLOAT8 regcoef[] = {
1851 1236.28,0,0,0,0.434542,25.0738,0,0,0,19.5442,19.7486,60,100,0
1854 FLOAT8 msum = regcoef[0]/4;
1857 for(sblock=0;sblock<3;sblock++)
1859 for ( sb = 0; sb < NBPSY_s; sb++ )
1863 if (gfc->thm[chn].s[sb][sblock] * gfc->masking_lower != 0 &&
1864 gfc->en[chn].s[sb][sblock] / (gfc->thm[chn].s[sb][sblock] * gfc->masking_lower) > 1)
1865 t = log(gfc->en[chn].s[sb][sblock] / (gfc->thm[chn].s[sb][sblock] * gfc->masking_lower));
1868 msum += regcoef[sb+1] * t;
1872 gfc->nsPsy.pe_s[chn] = msum;
1875 //gfc->pe[chn] -= 150;
1878 /***************************************************************
1879 * determine final block type
1880 ***************************************************************/
1882 for (chn=0; chn<gfc->channels_out; chn++) {
1883 blocktype[chn] = NORM_TYPE;
1886 if (gfp->short_blocks == short_block_coupled) {
1887 /* force both channels to use the same block type */
1888 /* this is necessary if the frame is to be encoded in ms_stereo. */
1889 /* But even without ms_stereo, FhG does this */
1890 int bothlong= (uselongblock[0] && uselongblock[1]);
1897 /* update the blocktype of the previous granule, since it depends on what
1898 * happend in this granule */
1899 for (chn=0; chn<gfc->channels_out; chn++) {
1900 if ( uselongblock[chn])
1901 { /* no attack : use long blocks */
1902 assert( gfc->blocktype_old[chn] != START_TYPE );
1903 switch( gfc->blocktype_old[chn] )
1907 blocktype[chn] = NORM_TYPE;
1910 blocktype[chn] = STOP_TYPE;
1914 /* attack : use short blocks */
1915 blocktype[chn] = SHORT_TYPE;
1916 if ( gfc->blocktype_old[chn] == NORM_TYPE ) {
1917 gfc->blocktype_old[chn] = START_TYPE;
1919 if ( gfc->blocktype_old[chn] == STOP_TYPE ) {
1920 gfc->blocktype_old[chn] = SHORT_TYPE ;
1924 blocktype_d[chn] = gfc->blocktype_old[chn]; /* value returned to calling program */
1925 gfc->blocktype_old[chn] = blocktype[chn]; /* save for next call to l3psy_anal */
1927 if (gfc->presetTune.use) {
1928 if (blocktype_d[chn] != NORM_TYPE)
1929 gfc->presetTune.quantcomp_current = gfc->presetTune.quantcomp_type_s;
1931 gfc->presetTune.quantcomp_current = gfp->experimentalX;
1933 if (gfc->ATH->adjust >= gfc->presetTune.athadjust_switch_level &&
1934 blocktype_d[chn] == NORM_TYPE &&
1935 gfc->presetTune.quantcomp_alt_type > -1) {
1936 gfc->presetTune.quantcomp_current = gfc->presetTune.quantcomp_alt_type;
1941 /*********************************************************************
1942 * compute the value of PE to return (one granule delay)
1943 *********************************************************************/
1944 for(chn=0;chn<numchn;chn++)
1947 if (blocktype_d[chn] == SHORT_TYPE) {
1948 percep_entropy[chn] = pe_s[chn];
1950 percep_entropy[chn] = pe_l[chn];
1952 if (gfp->analysis) gfc->pinfo->pe[gr_out][chn] = percep_entropy[chn];
1954 if (blocktype_d[0] == SHORT_TYPE) {
1955 percep_MS_entropy[chn-2] = pe_s[chn];
1957 percep_MS_entropy[chn-2] = pe_l[chn];
1959 if (gfp->analysis) gfc->pinfo->pe[gr_out][chn] = percep_MS_entropy[chn-2];
1971 * The spreading function. Values returned in units of energy
1973 FLOAT8 s3_func(FLOAT8 bark) {
1975 FLOAT8 tempx,x,tempy,temp;
1977 if (tempx>=0) tempx *= 3;
1980 if(tempx>=0.5 && tempx<=2.5)
1983 x = 8.0 * (temp*temp - 2.0 * temp);
1987 tempy = 15.811389 + 7.5*tempx - 17.5*sqrt(1.0+tempx*tempx);
1989 if (tempy <= -60.0) return 0.0;
1991 tempx = exp( (x + tempy)*LN_TO_LOG10 );
1993 /* Normalization. The spreading function should be normalized so that:
1996 | s3 [ bark ] d(bark) = 1
2012 int L3para_read(lame_global_flags * gfp, FLOAT8 sfreq, int *numlines_l,int *numlines_s,
2014 FLOAT8 s3_l[CBANDS][CBANDS], FLOAT8 s3_s[CBANDS][CBANDS],
2016 int *bu_l, int *bo_l, FLOAT8 *w1_l, FLOAT8 *w2_l,
2017 int *bu_s, int *bo_s, FLOAT8 *w1_s, FLOAT8 *w2_s,
2018 int *npart_l_orig,int *npart_l,int *npart_s_orig,int *npart_s)
2020 lame_internal_flags *gfc=gfp->internal_flags;
2023 FLOAT8 bval_l[CBANDS], bval_s[CBANDS];
2024 FLOAT8 bval_l_width[CBANDS], bval_s_width[CBANDS];
2026 int partition[HBLKSIZE];
2030 /* compute numlines, the number of spectral lines in each partition band */
2031 /* each partition band should be about DELBARK wide. */
2033 for(i=0;i<CBANDS;i++)
2035 FLOAT8 ji, bark1,bark2;
2039 j2 = Min(j2,BLKSIZE/2);
2042 /* find smallest j2 >= j so that (bark - bark_l[i-1]) < DELBARK */
2044 bark1 = freq2bark(sfreq*ji/BLKSIZE);
2048 bark2 = freq2bark(sfreq*ji/BLKSIZE);
2049 } while ((bark2 - bark1) < DELBARK && j2<=BLKSIZE/2);
2051 for (k=j; k<j2; ++k)
2053 numlines_l[i]=(j2-j);
2055 if (j > BLKSIZE/2) break;
2057 *npart_l_orig = i+1;
2058 assert(*npart_l_orig <= CBANDS);
2060 /* compute which partition bands are in which scalefactor bands */
2061 { int i1,i2,sfb,start,end;
2063 for ( sfb = 0; sfb < SBMAX_l; sfb++ ) {
2064 start = gfc->scalefac_band.l[ sfb ];
2065 end = gfc->scalefac_band.l[ sfb+1 ];
2066 freq1 = sfreq*(start-.5)/(2*576);
2067 freq2 = sfreq*(end-1+.5)/(2*576);
2069 i1 = floor(.5 + BLKSIZE*freq1/sfreq);
2071 i2 = floor(.5 + BLKSIZE*freq2/sfreq);
2072 if (i2>BLKSIZE/2) i2=BLKSIZE/2;
2074 // DEBUGF(gfc,"longblock: old: (%i,%i) new: (%i,%i) %i %i \n",bu_l[sfb],bo_l[sfb],partition[i1],partition[i2],i1,i2);
2078 bu_l[sfb]=partition[i1];
2079 bo_l[sfb]=partition[i2];
2085 /* compute bark value and ATH of each critical band */
2087 for ( i = 0; i < *npart_l_orig; i++ ) {
2090 /* FLOAT8 mval,freq; */
2092 // Calculating the medium bark scaled frequency of the spectral lines
2093 // from j ... j + numlines[i]-1 (=spectral lines in parition band i)
2095 k = numlines_l[i] - 1;
2096 bark1 = freq2bark(sfreq*(j+0)/BLKSIZE);
2097 bark2 = freq2bark(sfreq*(j+k)/BLKSIZE);
2098 bval_l[i] = .5*(bark1+bark2);
2100 bark1 = freq2bark(sfreq*(j+0-.5)/BLKSIZE);
2101 bark2 = freq2bark(sfreq*(j+k+.5)/BLKSIZE);
2102 bval_l_width[i] = bark2-bark1;
2104 gfc->ATH->cb [i] = 1.e37; // preinit for minimum search
2105 for (k=0; k < numlines_l[i]; k++, j++) {
2106 FLOAT8 freq = sfreq*j/(1000.0*BLKSIZE);
2108 assert( freq <= 24 ); // or only '<'
2109 // freq = Min(.1,freq); // ATH below 100 Hz constant, not further climbing
2110 level = ATHformula (freq*1000, gfp) - 20; // scale to FFT units; returned value is in dB
2111 level = pow ( 10., 0.1*level ); // convert from dB -> energy
2112 level *= numlines_l [i];
2113 if ( level < gfc->ATH->cb [i] )
2114 gfc->ATH->cb [i] = level;
2120 /* MINVAL. For low freq, the strength of the masking is limited by minval
2121 * this is an ISO MPEG1 thing, dont know if it is really needed */
2122 for(i=0;i<*npart_l_orig;i++){
2123 double x = (-20+bval_l[i]*20.0/10.0);
2124 if (bval_l[i]>10) x = 0;
2125 minval[i]=pow(10.0,x/10);
2126 gfc->PSY->prvTonRed[i] = minval[i];
2135 /************************************************************************/
2137 /************************************************************************/
2139 /* compute numlines */
2141 for(i=0;i<CBANDS;i++)
2143 FLOAT8 ji, bark1,bark2;
2147 j2 = Min(j2,BLKSIZE_s/2);
2150 /* find smallest j2 >= j so that (bark - bark_s[i-1]) < DELBARK */
2152 bark1 = freq2bark(sfreq*ji/BLKSIZE_s);
2156 bark2 = freq2bark(sfreq*ji/BLKSIZE_s);
2158 } while ((bark2 - bark1) < DELBARK && j2<=BLKSIZE_s/2);
2160 for (k=j; k<j2; ++k)
2162 numlines_s[i]=(j2-j);
2164 if (j > BLKSIZE_s/2) break;
2166 *npart_s_orig = i+1;
2167 assert(*npart_s_orig <= CBANDS);
2169 /* compute which partition bands are in which scalefactor bands */
2170 { int i1,i2,sfb,start,end;
2172 for ( sfb = 0; sfb < SBMAX_s; sfb++ ) {
2173 start = gfc->scalefac_band.s[ sfb ];
2174 end = gfc->scalefac_band.s[ sfb+1 ];
2175 freq1 = sfreq*(start-.5)/(2*192);
2176 freq2 = sfreq*(end-1+.5)/(2*192);
2178 i1 = floor(.5 + BLKSIZE_s*freq1/sfreq);
2180 i2 = floor(.5 + BLKSIZE_s*freq2/sfreq);
2181 if (i2>BLKSIZE_s/2) i2=BLKSIZE_s/2;
2183 //DEBUGF(gfc,"shortblock: old: (%i,%i) new: (%i,%i) %i %i \n",bu_s[sfb],bo_s[sfb], partition[i1],partition[i2],i1,i2);
2187 bu_s[sfb]=partition[i1];
2188 bo_s[sfb]=partition[i2];
2197 /* compute bark values of each critical band */
2199 for(i=0;i<*npart_s_orig;i++)
2202 FLOAT8 bark1,bark2,snr;
2203 k = numlines_s[i] - 1;
2205 bark1 = freq2bark (sfreq*(j+0)/BLKSIZE_s);
2206 bark2 = freq2bark (sfreq*(j+k)/BLKSIZE_s);
2207 bval_s[i] = .5*(bark1+bark2);
2209 bark1 = freq2bark (sfreq*(j+0-.5)/BLKSIZE_s);
2210 bark2 = freq2bark (sfreq*(j+k+.5)/BLKSIZE_s);
2211 bval_s_width[i] = bark2-bark1;
2218 snr = -4.5 * (bval_s[i]-13)/(24.0-13.0) +
2219 -8.25*(bval_s[i]-24)/(13.0-24.0);
2221 SNR[i]=pow(10.0,snr/10.0);
2229 /************************************************************************
2230 * Now compute the spreading function, s[j][i], the value of the spread-*
2231 * ing function, centered at band j, for band i, store for later use *
2232 ************************************************************************/
2233 /* i.e.: sum over j to spread into signal barkval=i
2234 NOTE: i and j are used opposite as in the ISO docs */
2235 for(i=0;i<*npart_l_orig;i++) {
2236 for(j=0;j<*npart_l_orig;j++) {
2237 s3_l[i][j]=s3_func(bval_l[i]-bval_l[j])*bval_l_width[j];
2240 for(i=0;i<*npart_s_orig;i++) {
2241 for(j=0;j<*npart_s_orig;j++) {
2242 s3_s[i][j]=s3_func(bval_s[i]-bval_s[j])*bval_s_width[j];
2250 /* npart_l_orig = number of partition bands before convolution */
2251 /* npart_l = number of partition bands after convolution */
2253 *npart_l=bo_l[NBPSY_l-1]+1;
2254 *npart_s=bo_s[NBPSY_s-1]+1;
2256 assert(*npart_l <= *npart_l_orig);
2257 assert(*npart_s <= *npart_s_orig);
2260 /* setup stereo demasking thresholds */
2261 /* formula reverse enginerred from plot in paper */
2262 for ( i = 0; i < NBPSY_s; i++ ) {
2264 arg = freq2bark(sfreq*gfc->scalefac_band.s[i]/(2*192));
2265 arg = (Min(arg, 15.5)/15.5);
2267 mld = 1.25*(1-cos(PI*arg))-2.5;
2268 gfc->mld_s[i] = pow(10.0,mld);
2270 for ( i = 0; i < NBPSY_l; i++ ) {
2272 arg = freq2bark(sfreq*gfc->scalefac_band.l[i]/(2*576));
2273 arg = (Min(arg, 15.5)/15.5);
2275 mld = 1.25*(1-cos(PI*arg))-2.5;
2276 gfc->mld_l[i] = pow(10.0,mld);
2279 #define temporalmask_sustain_sec 0.01
2281 /* setup temporal masking */
2282 gfc->decay = exp(-1.0*LOG10/(temporalmask_sustain_sec*sfreq/192.0));
2297 int psymodel_init(lame_global_flags *gfp)
2299 lame_internal_flags *gfc=gfp->internal_flags;
2300 int i,j,b,sb,k,samplerate;
2302 FLOAT8 s3_s[CBANDS][CBANDS];
2303 FLOAT8 s3_l[CBANDS][CBANDS];
2304 int numberOfNoneZero;
2306 samplerate = gfp->out_samplerate;
2307 gfc->ms_ener_ratio_old=.25;
2308 gfc->blocktype_old[0]=STOP_TYPE;
2309 gfc->blocktype_old[1]=STOP_TYPE;
2310 gfc->blocktype_old[0]=SHORT_TYPE;
2311 gfc->blocktype_old[1]=SHORT_TYPE;
2313 for (i=0; i<4; ++i) {
2314 for (j=0; j<CBANDS; ++j) {
2315 gfc->nb_1[i][j]=1e20;
2316 gfc->nb_2[i][j]=1e20;
2318 for ( sb = 0; sb < NBPSY_l; sb++ ) {
2319 gfc->en[i].l[sb] = 1e20;
2320 gfc->thm[i].l[sb] = 1e20;
2322 for (j=0; j<3; ++j) {
2323 for ( sb = 0; sb < NBPSY_s; sb++ ) {
2324 gfc->en[i].s[sb][j] = 1e20;
2325 gfc->thm[i].s[sb][j] = 1e20;
2329 for (i=0; i<4; ++i) {
2330 for (j=0; j<3; ++j) {
2331 for ( sb = 0; sb < NBPSY_s; sb++ ) {
2332 gfc->nsPsy.last_thm[i][sb][j] = 1e20;
2338 gfc->nsPsy.last_en_subshort[i][j] = 100;
2340 gfc->nsPsy.last_attacks[i][j] = 0;
2341 gfc->nsPsy.pe_l[i] = gfc->nsPsy.pe_s[i] = 0;
2347 /* gfp->cwlimit = sfreq*j/1024.0; */
2348 gfc->cw_lower_index=6;
2349 gfc->cw_upper_index = gfc->PSY->cwlimit*1024.0/gfp->out_samplerate;
2350 gfc->cw_upper_index=Min(HBLKSIZE-4,gfc->cw_upper_index); /* j+3 < HBLKSIZE-1 */
2351 gfc->cw_upper_index=Max(6,gfc->cw_upper_index);
2353 for ( j = 0; j < HBLKSIZE; j++ )
2358 i=L3para_read( gfp,(FLOAT8) samplerate,gfc->numlines_l,gfc->numlines_s,
2359 gfc->minval,s3_l,s3_s,gfc->SNR_s,gfc->bu_l,
2360 gfc->bo_l,gfc->w1_l,gfc->w2_l, gfc->bu_s,gfc->bo_s,
2361 gfc->w1_s,gfc->w2_s,&gfc->npart_l_orig,&gfc->npart_l,
2362 &gfc->npart_s_orig,&gfc->npart_s );
2363 if (i!=0) return -1;
2367 /* npart_l_orig = number of partition bands before convolution */
2368 /* npart_l = number of partition bands after convolution */
2370 numberOfNoneZero = 0;
2371 for (i=0; i<gfc->npart_l; i++) {
2372 for (j = 0; j < gfc->npart_l_orig; j++) {
2373 if (s3_l[i][j] != 0.0)
2376 gfc->s3ind[i][0] = j;
2378 for (j = gfc->npart_l_orig - 1; j > 0; j--) {
2379 if (s3_l[i][j] != 0.0)
2382 gfc->s3ind[i][1] = j;
2383 numberOfNoneZero += (gfc->s3ind[i][1] - gfc->s3ind[i][0] + 1);
2385 gfc->s3_ll = malloc(sizeof(FLOAT8)*numberOfNoneZero);
2390 for (i=0; i<gfc->npart_l; i++) {
2391 for (j = gfc->s3ind[i][0]; j <= gfc->s3ind[i][1]; j++) {
2392 gfc->s3_ll[k++] = s3_l[i][j];
2398 numberOfNoneZero = 0;
2399 for (i=0; i<gfc->npart_s; i++) {
2400 for (j = 0; j < gfc->npart_s_orig; j++) {
2401 if (s3_s[i][j] != 0.0)
2404 gfc->s3ind_s[i][0] = j;
2406 for (j = gfc->npart_s_orig - 1; j > 0; j--) {
2407 if (s3_s[i][j] != 0.0)
2410 gfc->s3ind_s[i][1] = j;
2411 numberOfNoneZero += (gfc->s3ind_s[i][1] - gfc->s3ind_s[i][0] + 1);
2413 gfc->s3_ss = malloc(sizeof(FLOAT8)*numberOfNoneZero);
2421 #include "debugscalefac.c"
2426 if (gfc->nsPsy.use) {
2427 /* long block spreading function normalization */
2428 for ( b = 0;b < gfc->npart_l; b++ ) {
2429 for ( k = gfc->s3ind[b][0]; k <= gfc->s3ind[b][1]; k++ ) {
2430 // spreading function has been properly normalized by
2431 // multiplying by DELBARK/.6609193 = .515.
2432 // It looks like Naoki was
2433 // way ahead of me and added this factor here!
2434 // it is no longer needed.
2435 //gfc->s3_l[b][k] *= 0.5;
2438 /* short block spreading function normalization */
2439 // no longer needs to be normalized, but nspsytune wants
2440 // SNR_s applied here istead of later to save CPU cycles
2441 for ( b = 0;b < gfc->npart_s; b++ ) {
2443 for ( k = gfc->s3ind_s[b][0]; k <= gfc->s3ind_s[b][1]; k++ ) {
2446 for ( k = gfc->s3ind_s[b][0]; k <= gfc->s3ind_s[b][1]; k++ ) {
2447 s3_s[b][k] *= gfc->SNR_s[b] /* / norm */;
2454 if (gfc->nsPsy.use) {
2456 /* spread only from npart_l bands. Normally, we use the spreading
2457 * function to convolve from npart_l_orig down to npart_l bands
2459 for(b=0;b<gfc->npart_l;b++)
2460 if (gfc->s3ind[b][1] > gfc->npart_l-1) gfc->s3ind[b][1] = gfc->npart_l-1;
2464 for (i=0; i<gfc->npart_s; i++) {
2465 for (j = gfc->s3ind_s[i][0]; j <= gfc->s3ind_s[i][1]; j++) {
2466 gfc->s3_ss[k++] = s3_s[i][j];
2472 /* init. for loudness approx. -jd 2001 mar 27*/
2473 gfc->loudness_sq_save[0] = 0.0;
2474 gfc->loudness_sq_save[1] = 0.0;
2485 /* psycho_loudness_approx
2487 in: energy - BLKSIZE/2 elements of frequency magnitudes ^ 2
2488 gfp - uses out_samplerate, ATHtype (also needed for ATHformula)
2489 returns: loudness^2 approximation, a positive value roughly tuned for a value
2490 of 1.0 for signals near clipping.
2491 notes: When calibrated, feeding this function binary white noise at sample
2492 values +32767 or -32768 should return values that approach 3.
2493 ATHformula is used to approximate an equal loudness curve.
2494 future: Data indicates that the shape of the equal loudness curve varies
2495 with intensity. This function might be improved by using an equal
2496 loudness curve shaped for typical playback levels (instead of the
2497 ATH, that is shaped for the threshold). A flexible realization might
2498 simply bend the existing ATH curve to achieve the desired shape.
2499 However, the potential gain may not be enough to justify an effort.
2502 psycho_loudness_approx( FLOAT *energy, lame_global_flags *gfp )
2505 static int eql_type = -1;
2506 static FLOAT eql_w[BLKSIZE/2];/* equal loudness weights (based on ATH) */
2507 const FLOAT vo_scale= 1./( 14752 ); /* tuned for output level */
2508 /* (sensitive to energy scale) */
2509 FLOAT loudness_power;
2511 if( eql_type != gfp->ATHtype ) {
2512 /* compute equal loudness weights (eql_w) */
2514 FLOAT freq_inc = gfp->out_samplerate / (BLKSIZE);
2515 FLOAT eql_balance = 0.0;
2516 eql_type = gfp->ATHtype;
2518 for( i = 0; i < BLKSIZE/2; ++i ) {
2520 /* convert ATH dB to relative power (not dB) */
2521 /* to determine eql_w */
2522 eql_w[i] = 1. / pow( 10, ATHformula( freq, gfp ) / 10 );
2523 eql_balance += eql_w[i];
2525 eql_balance = 1 / eql_balance;
2526 for( i = BLKSIZE/2; --i >= 0; ) { /* scale weights */
2527 eql_w[i] *= eql_balance;
2531 loudness_power = 0.0;
2532 for( i = 0; i < BLKSIZE/2; ++i ) { /* apply weights to power in freq. bands*/
2533 loudness_power += energy[i] * eql_w[i];
2535 loudness_power /= (BLKSIZE/2);
2536 loudness_power *= vo_scale * vo_scale;
2538 return( loudness_power );