2 * quantize_pvt source file
4 * Copyright (c) 1999 Takehiro TOMINAGA
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: quantize_pvt.c,v 1.2 2006/02/09 16:55:31 kramm Exp $ */
24 #include "config_static.h"
28 #include "lame-analysis.h"
30 #include "reservoir.h"
31 #include "quantize_pvt.h"
38 #define NSATHSCALE 100 // Assuming dynamic range=96dB, this value should be 92
40 const char slen1_tab [16] = { 0, 0, 0, 0, 3, 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4 };
41 const char slen2_tab [16] = { 0, 1, 2, 3, 0, 1, 2, 3, 1, 2, 3, 1, 2, 3, 2, 3 };
45 The following table is used to implement the scalefactor
46 partitioning for MPEG2 as described in section
47 2.4.3.2 of the IS. The indexing corresponds to the
48 way the tables are presented in the IS:
50 [table_number][row_in_table][column of nr_of_sfb]
52 const int nr_of_sfb_block [6] [3] [4] =
87 /* Table B.6: layer3 preemphasis */
88 const char pretab [SBMAX_l] =
90 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
91 1, 1, 1, 1, 2, 2, 3, 3, 3, 2, 0
95 Here are MPEG1 Table B.8 and MPEG2 Table B.1
96 -- Layer III scalefactor bands.
97 Index into this using a method such as:
98 idx = fr_ps->header->sampling_frequency
99 + (fr_ps->header->version * 3)
103 const scalefac_struct sfBandIndex[9] =
105 { /* Table B.2.b: 22.05 kHz */
106 {0,6,12,18,24,30,36,44,54,66,80,96,116,140,168,200,238,284,336,396,464,522,576},
107 {0,4,8,12,18,24,32,42,56,74,100,132,174,192}
109 { /* Table B.2.c: 24 kHz */ /* docs: 332. mpg123(broken): 330 */
110 {0,6,12,18,24,30,36,44,54,66,80,96,114,136,162,194,232,278, 332, 394,464,540,576},
111 {0,4,8,12,18,26,36,48,62,80,104,136,180,192}
113 { /* Table B.2.a: 16 kHz */
114 {0,6,12,18,24,30,36,44,54,66,80,96,116,140,168,200,238,284,336,396,464,522,576},
115 {0,4,8,12,18,26,36,48,62,80,104,134,174,192}
117 { /* Table B.8.b: 44.1 kHz */
118 {0,4,8,12,16,20,24,30,36,44,52,62,74,90,110,134,162,196,238,288,342,418,576},
119 {0,4,8,12,16,22,30,40,52,66,84,106,136,192}
121 { /* Table B.8.c: 48 kHz */
122 {0,4,8,12,16,20,24,30,36,42,50,60,72,88,106,128,156,190,230,276,330,384,576},
123 {0,4,8,12,16,22,28,38,50,64,80,100,126,192}
125 { /* Table B.8.a: 32 kHz */
126 {0,4,8,12,16,20,24,30,36,44,54,66,82,102,126,156,194,240,296,364,448,550,576},
127 {0,4,8,12,16,22,30,42,58,78,104,138,180,192}
129 { /* MPEG-2.5 11.025 kHz */
130 {0,6,12,18,24,30,36,44,54,66,80,96,116,140,168,200,238,284,336,396,464,522,576},
131 {0/3,12/3,24/3,36/3,54/3,78/3,108/3,144/3,186/3,240/3,312/3,402/3,522/3,576/3}
133 { /* MPEG-2.5 12 kHz */
134 {0,6,12,18,24,30,36,44,54,66,80,96,116,140,168,200,238,284,336,396,464,522,576},
135 {0/3,12/3,24/3,36/3,54/3,78/3,108/3,144/3,186/3,240/3,312/3,402/3,522/3,576/3}
137 { /* MPEG-2.5 8 kHz */
138 {0,12,24,36,48,60,72,88,108,132,160,192,232,280,336,400,476,566,568,570,572,574,576},
139 {0/3,24/3,48/3,72/3,108/3,156/3,216/3,288/3,372/3,480/3,486/3,492/3,498/3,576/3}
146 FLOAT8 ipow20[Q_MAX];
147 FLOAT8 iipow20[Q_MAX];
149 FLOAT8 pow43[PRECALC_SIZE];
150 /* initialized in first call to iteration_init */
151 FLOAT8 adj43asm[PRECALC_SIZE];
152 FLOAT8 adj43[PRECALC_SIZE];
154 /************************************************************************/
155 /* initialization for iteration_loop */
156 /************************************************************************/
158 iteration_init( lame_global_flags *gfp)
160 lame_internal_flags *gfc=gfp->internal_flags;
161 III_side_info_t * const l3_side = &gfc->l3_side;
164 if ( gfc->iteration_init_init==0 ) {
165 gfc->iteration_init_init=1;
167 l3_side->main_data_begin = 0;
168 compute_ath(gfp,gfc->ATH->l,gfc->ATH->s);
171 for(i=1;i<PRECALC_SIZE;i++)
172 pow43[i] = pow((FLOAT8)i, 4.0/3.0);
175 for (i = 1; i < PRECALC_SIZE; i++)
176 adj43asm[i] = i - 0.5 - pow(0.5 * (pow43[i - 1] + pow43[i]),0.75);
177 for (i = 0; i < PRECALC_SIZE-1; i++)
178 adj43[i] = (i + 1) - pow(0.5 * (pow43[i] + pow43[i + 1]), 0.75);
180 iipow20_ = &iipow20[210];
181 for (i = 0; i < Q_MAX; i++) {
182 iipow20[i] = pow(2.0, (double)(i - 210) * 0.1875);
183 ipow20[i] = pow(2.0, (double)(i - 210) * -0.1875);
184 pow20[i] = pow(2.0, (double)(i - 210) * 0.25);
195 compute the ATH for each scalefactor band
198 Input: 3.3kHz signal 32767 amplitude (3.3kHz is where ATH is smallest = -5db)
199 longblocks: sfb=12 en0/bw=-11db max_en0 = 1.3db
200 shortblocks: sfb=5 -9db 0db
202 Input: 1 1 1 1 1 1 1 -1 -1 -1 -1 -1 -1 -1 (repeated)
203 longblocks: amp=1 sfb=12 en0/bw=-103 db max_en0 = -92db
204 amp=32767 sfb=12 -12 db -1.4db
206 Input: 1 1 1 1 1 1 1 -1 -1 -1 -1 -1 -1 -1 (repeated)
207 shortblocks: amp=1 sfb=5 en0/bw= -99 -86
208 amp=32767 sfb=5 -9 db 4db
211 MAX energy of largest wave at 3.3kHz = 1db
212 AVE energy of largest wave at 3.3kHz = -11db
213 Let's take AVE: -11db = maximum signal in sfb=12.
214 Dynamic range of CD: 96db. Therefor energy of smallest audible wave
215 in sfb=12 = -11 - 96 = -107db = ATH at 3.3kHz.
217 ATH formula for this wave: -5db. To adjust to LAME scaling, we need
218 ATH = ATH_formula - 103 (db)
219 ATH = ATH * 2.5e-10 (ener)
223 FLOAT8 ATHmdct( lame_global_flags *gfp, FLOAT8 f )
225 lame_internal_flags *gfc = gfp->internal_flags;
228 ath = ATHformula( f , gfp );
230 if (gfc->nsPsy.use) {
236 /* modify the MDCT scaling for the ATH */
237 ath -= gfp->ATHlower;
239 /* convert to energy */
240 ath = pow( 10.0, ath/10.0 );
245 void compute_ath( lame_global_flags *gfp, FLOAT8 ATH_l[], FLOAT8 ATH_s[] )
247 lame_internal_flags *gfc = gfp->internal_flags;
248 int sfb, i, start, end;
250 FLOAT8 samp_freq = gfp->out_samplerate;
252 for (sfb = 0; sfb < SBMAX_l; sfb++) {
253 start = gfc->scalefac_band.l[ sfb ];
254 end = gfc->scalefac_band.l[ sfb+1 ];
255 ATH_l[sfb]=FLOAT8_MAX;
256 for (i = start ; i < end; i++) {
257 FLOAT8 freq = i*samp_freq/(2*576);
258 ATH_f = ATHmdct( gfp, freq ); /* freq in kHz */
259 ATH_l[sfb] = Min( ATH_l[sfb], ATH_f );
263 for (sfb = 0; sfb < SBMAX_s; sfb++){
264 start = gfc->scalefac_band.s[ sfb ];
265 end = gfc->scalefac_band.s[ sfb+1 ];
266 ATH_s[sfb] = FLOAT8_MAX;
267 for (i = start ; i < end; i++) {
268 FLOAT8 freq = i*samp_freq/(2*192);
269 ATH_f = ATHmdct( gfp, freq ); /* freq in kHz */
270 ATH_s[sfb] = Min( ATH_s[sfb], ATH_f );
275 * reduce ATH to -200 dB
279 for (sfb = 0; sfb < SBMAX_l; sfb++) {
282 for (sfb = 0; sfb < SBMAX_s; sfb++) {
287 /* work in progress, don't rely on it too much
289 gfc->ATH-> floor = 10. * log10( ATHmdct( gfp, -1. ) );
292 { FLOAT8 g=10000, t=1e30, x;
293 for ( f = 100; f < 10000; f++ ) {
294 x = ATHmdct( gfp, f );
295 if ( t > x ) t = x, g = f;
297 printf("min=%g\n", g);
305 /* convert from L/R <-> Mid/Side, src == dst allowed */
306 void ms_convert(FLOAT8 dst[2][576], FLOAT8 src[2][576])
311 for (i = 0; i < 576; ++i) {
314 dst[0][i] = (l+r) * (FLOAT8)(SQRT2*0.5);
315 dst[1][i] = (l-r) * (FLOAT8)(SQRT2*0.5);
321 /************************************************************************
322 * allocate bits among 2 channels based on PE
324 * bugfixes rh 8/01: often allocated more than the allowed 4095 bits
325 ************************************************************************/
326 int on_pe( lame_global_flags *gfp, FLOAT8 pe[][2], III_side_info_t *l3_side,
327 int targ_bits[2], int mean_bits, int gr )
329 lame_internal_flags * gfc = gfp->internal_flags;
331 int extra_bits, tbits, bits;
333 int max_bits; /* maximum allowed bits for this granule */
336 /* allocate targ_bits for granule */
337 ResvMaxBits( gfp, mean_bits, &tbits, &extra_bits );
338 max_bits = tbits + extra_bits;
339 if (max_bits > MAX_BITS) /* hard limit per granule */
342 for ( bits = 0, ch = 0; ch < gfc->channels_out; ++ch ) {
343 /******************************************************************
344 * allocate bits for each channel
345 ******************************************************************/
346 cod_info = &l3_side->gr[gr].ch[ch].tt;
348 targ_bits[ch] = Min( MAX_BITS, tbits/gfc->channels_out );
350 if (gfc->nsPsy.use) {
351 add_bits[ch] = targ_bits[ch] * pe[gr][ch] / 700.0 - targ_bits[ch];
354 add_bits[ch] = (pe[gr][ch]-750) / 1.4;
355 /* short blocks us a little extra, no matter what the pe */
356 if ( cod_info->block_type == SHORT_TYPE ) {
357 if (add_bits[ch] < mean_bits/4)
358 add_bits[ch] = mean_bits/4;
362 /* at most increase bits by 1.5*average */
363 if (add_bits[ch] > mean_bits*3/4)
364 add_bits[ch] = mean_bits*3/4;
365 if (add_bits[ch] < 0)
368 if (add_bits[ch]+targ_bits[ch] > MAX_BITS)
369 add_bits[ch] = Max( 0, MAX_BITS-targ_bits[ch] );
371 bits += add_bits[ch];
373 if (bits > extra_bits) {
374 for ( ch = 0; ch < gfc->channels_out; ++ch ) {
375 add_bits[ch] = extra_bits * add_bits[ch] / bits;
379 for ( ch = 0; ch < gfc->channels_out; ++ch ) {
380 targ_bits[ch] += add_bits[ch];
381 extra_bits -= add_bits[ch];
382 assert( targ_bits[ch] <= MAX_BITS );
384 assert( max_bits <= MAX_BITS );
391 void reduce_side(int targ_bits[2],FLOAT8 ms_ener_ratio,int mean_bits,int max_bits)
397 /* ms_ener_ratio = 0: allocate 66/33 mid/side fac=.33
398 * ms_ener_ratio =.5: allocate 50/50 mid/side fac= 0 */
399 /* 75/25 split is fac=.5 */
400 /* float fac = .50*(.5-ms_ener_ratio[gr])/.5;*/
401 fac = .33*(.5-ms_ener_ratio)/.5;
405 /* number of bits to move from side channel to mid channel */
406 /* move_bits = fac*targ_bits[1]; */
407 move_bits = fac*.5*(targ_bits[0]+targ_bits[1]);
409 if (move_bits > MAX_BITS - targ_bits[0]) {
410 move_bits = MAX_BITS - targ_bits[0];
412 if (move_bits<0) move_bits=0;
414 if (targ_bits[1] >= 125) {
415 /* dont reduce side channel below 125 bits */
416 if (targ_bits[1]-move_bits > 125) {
418 /* if mid channel already has 2x more than average, dont bother */
419 /* mean_bits = bits per granule (for both channels) */
420 if (targ_bits[0] < mean_bits)
421 targ_bits[0] += move_bits;
422 targ_bits[1] -= move_bits;
424 targ_bits[0] += targ_bits[1] - 125;
429 move_bits=targ_bits[0]+targ_bits[1];
430 if (move_bits > max_bits) {
431 targ_bits[0]=(max_bits*targ_bits[0])/move_bits;
432 targ_bits[1]=(max_bits*targ_bits[1])/move_bits;
434 assert (targ_bits[0] <= MAX_BITS);
435 assert (targ_bits[1] <= MAX_BITS);
440 * Robert Hegemann 2001-04-27:
441 * this adjusts the ATH, keeping the original noise floor
442 * affects the higher frequencies more than the lower ones
445 FLOAT8 athAdjust( FLOAT8 a, FLOAT8 x, FLOAT8 athFloor )
449 FLOAT8 const o = 90.30873362;
450 FLOAT8 const p = 94.82444863;
451 FLOAT8 u = 10. * log10(x);
454 u -= athFloor; // undo scaling
455 if ( v > 1E-20 ) w = 1. + 10. * log10(v) / o;
458 u += athFloor + o-p; // redo scaling
460 return pow( 10., 0.1*u );
464 /*************************************************************************/
466 /*************************************************************************/
469 Calculate the allowed distortion for each scalefactor band,
470 as determined by the psychoacoustic model.
471 xmin(sb) = ratio(sb) * en(sb) / bw(sb)
473 returns number of sfb's with energy > ATH
476 lame_global_flags *gfp,
477 const FLOAT8 xr [576],
478 const III_psy_ratio * const ratio,
479 const gr_info * const cod_info,
480 III_psy_xmin * const l3_xmin )
482 lame_internal_flags *gfc = gfp->internal_flags;
483 int sfb,j,start, end, bw,l, b, ath_over=0;
484 FLOAT8 en0, xmin, ener, tmpATH;
485 ATH_t * ATH = gfc->ATH;
487 if (cod_info->block_type == SHORT_TYPE) {
489 for ( j = 0, sfb = 0; sfb < SBMAX_s; sfb++ ) {
490 if ( gfp->VBR == vbr_rh || gfp->VBR == vbr_mtrh )
491 tmpATH = athAdjust( ATH->adjust, ATH->s[sfb], ATH->floor );
493 tmpATH = ATH->adjust * ATH->s[sfb];
494 start = gfc->scalefac_band.s[ sfb ];
495 end = gfc->scalefac_band.s[ sfb + 1 ];
498 for ( b = 0; b < 3; b++ ) {
499 for (en0 = 0.0, l = start; l < end; l++) {
506 if (gfp->ATHonly || gfp->ATHshort) {
510 xmin = ratio->en.s[sfb][b];
512 xmin = en0 * ratio->thm.s[sfb][b] * gfc->masking_lower / xmin;
518 if (gfc->nsPsy.use) {
519 if (sfb <= 5) xmin *= gfc->nsPsy.bass;
520 else if (sfb <= 10) xmin *= gfc->nsPsy.alto;
521 else xmin *= gfc->nsPsy.treble;
522 if ((gfp->VBR == vbr_off || gfp->VBR == vbr_abr) && gfp->quality <= 1)
525 l3_xmin->s[sfb][b] = xmin;
526 if (en0 > tmpATH) ath_over++;
530 if (gfp->useTemporal) {
531 for (sfb = 0; sfb < SBMAX_s; sfb++ ) {
532 for ( b = 1; b < 3; b++ ) {
533 xmin = l3_xmin->s[sfb][b] * (1.0 - gfc->decay)
534 + l3_xmin->s[sfb][b-1] * gfc->decay;
535 if (l3_xmin->s[sfb][b] < xmin)
536 l3_xmin->s[sfb][b] = xmin;
541 } /* end of short block case */
544 for ( sfb = 0; sfb < SBMAX_l; sfb++ ){
545 if ( gfp->VBR == vbr_rh || gfp->VBR == vbr_mtrh )
546 tmpATH = athAdjust( ATH->adjust, ATH->l[sfb], ATH->floor );
548 tmpATH = ATH->adjust * ATH->l[sfb];
549 start = gfc->scalefac_band.l[ sfb ];
550 end = gfc->scalefac_band.l[ sfb+1 ];
553 for (en0 = 0.0, l = start; l < end; l++ ) {
554 ener = xr[l] * xr[l];
557 /* why is it different from short blocks <?> */
558 if ( !gfc->nsPsy.use ) en0 /= bw;
564 xmin = ratio->en.l[sfb];
566 xmin = en0 * ratio->thm.l[sfb] * gfc->masking_lower / xmin;
570 /* why is it different from short blocks <?> */
571 if ( !gfc->nsPsy.use ) {
575 if (sfb <= 6) xmin *= gfc->nsPsy.bass;
576 else if (sfb <= 13) xmin *= gfc->nsPsy.alto;
577 else if (sfb <= 20) xmin *= gfc->nsPsy.treble;
578 else xmin *= gfc->nsPsy.sfb21;
579 if ((gfp->VBR == vbr_off || gfp->VBR == vbr_abr) && gfp->quality <= 1)
582 l3_xmin->l[sfb] = xmin;
583 if (en0 > tmpATH) ath_over++;
586 } /* end of long block case */
597 /*************************************************************************/
599 /*************************************************************************/
613 double penalties ( double noise )
615 return log ( 0.368 + 0.632 * noise * noise * noise );
618 /* mt 5/99: Function: Improved calc_noise for a single channel */
621 const lame_internal_flags * const gfc,
622 const FLOAT8 xr [576],
624 const gr_info * const cod_info,
625 const III_psy_xmin * const l3_xmin,
626 const III_scalefac_t * const scalefac,
628 calc_noise_result * const res )
630 int sfb,start, end, j,l, i, over=0;
634 FLOAT8 noise,noise_db;
635 FLOAT8 over_noise_db = 0;
636 FLOAT8 tot_noise_db = 0; /* 0 dB relative to masking */
637 FLOAT8 max_noise = 1E-20; /* -200 dB relative to masking */
638 double klemm_noise = 1E-37;
640 if (cod_info->block_type == SHORT_TYPE) {
641 int max_index = gfc->sfb21_extra ? SBMAX_s : SBPSY_s;
643 for ( j=0, sfb = 0; sfb < max_index; sfb++ ) {
644 start = gfc->scalefac_band.s[ sfb ];
645 end = gfc->scalefac_band.s[ sfb+1 ];
646 for ( i = 0; i < 3; i++ ) {
650 s = (scalefac->s[sfb][i] << (cod_info->scalefac_scale + 1))
651 + cod_info->subblock_gain[i] * 8;
652 s = cod_info->global_gain - s;
668 noise = xfsf->s[sfb][i] = sum / l3_xmin->s[sfb][i];
670 max_noise = Max(max_noise,noise);
671 klemm_noise += penalties (noise);
673 noise_db=10*log10(Max(noise,1E-20));
674 tot_noise_db += noise_db;
678 over_noise_db += noise_db;
683 }else{ /* cod_info->block_type == SHORT_TYPE */
684 int max_index = gfc->sfb21_extra ? SBMAX_l : SBPSY_l;
686 for ( sfb = 0; sfb < max_index; sfb++ ) {
688 int s = scalefac->l[sfb];
690 if (cod_info->preflag)
693 s = cod_info->global_gain - (s << (cod_info->scalefac_scale + 1));
698 start = gfc->scalefac_band.l[ sfb ];
699 end = gfc->scalefac_band.l[ sfb+1 ];
701 for ( sum = 0.0, l = start; l < end; l++ ) {
703 temp = fabs(xr[l]) - pow43[ix[l]] * step;
706 noise = xfsf->l[sfb] = sum / l3_xmin->l[sfb];
707 max_noise=Max(max_noise,noise);
708 klemm_noise += penalties (noise);
710 noise_db=10*log10(Max(noise,1E-20));
711 /* multiplying here is adding in dB, but can overflow */
712 //tot_noise *= Max(noise, 1E-20);
713 tot_noise_db += noise_db;
717 /* multiplying here is adding in dB -but can overflow */
718 //over_noise *= noise;
719 over_noise_db += noise_db;
724 } /* cod_info->block_type == SHORT_TYPE */
726 /* normalization at this point by "count" is not necessary, since
727 * the values are only used to compare with previous values */
728 res->over_count = over;
730 /* convert to db. DO NOT CHANGE THESE */
731 /* tot_noise = is really the average over each sfb of:
732 [noise(db) - allowed_noise(db)]
734 and over_noise is the same average, only over only the
735 bands with noise > allowed_noise.
739 //res->tot_noise = 10.*log10(Max(1e-20,tot_noise ));
740 //res->over_noise = 10.*log10(Max(1e+00,over_noise));
741 res->tot_noise = tot_noise_db;
742 res->over_noise = over_noise_db;
743 res->max_noise = 10.*log10(Max(1e-20,max_noise ));
744 res->klemm_noise = 10.*log10(Max(1e-20,klemm_noise));
762 /************************************************************************
766 * updates plotting data
768 * Mark Taylor 2000-??-??
770 * Robert Hegemann: moved noise/distortion calc into it
772 ************************************************************************/
776 lame_global_flags *gfp,
777 const gr_info * const cod_info,
778 const III_psy_ratio * const ratio,
779 const III_scalefac_t * const scalefac,
780 const FLOAT8 xr[576],
781 const int l3_enc[576],
785 lame_internal_flags *gfc=gfp->internal_flags;
787 int j,i,l,start,end,bw;
789 FLOAT ifqstep = ( cod_info->scalefac_scale == 0 ) ? .5 : 1.0;
792 III_psy_xmin l3_xmin;
793 calc_noise_result noise;
796 calc_xmin (gfp,xr, ratio, cod_info, &l3_xmin);
798 calc_noise (gfc, xr, l3_enc, cod_info, &l3_xmin, scalefac, &xfsf, &noise);
800 if (cod_info->block_type == SHORT_TYPE) {
801 for (j=0, sfb = 0; sfb < SBMAX_s; sfb++ ) {
802 start = gfc->scalefac_band.s[ sfb ];
803 end = gfc->scalefac_band.s[ sfb + 1 ];
805 for ( i = 0; i < 3; i++ ) {
806 for ( en0 = 0.0, l = start; l < end; l++ ) {
807 en0 += xr[j] * xr[j];
810 en0=Max(en0/bw,1e-20);
822 tot2 += ratio->en.s[sfb][i];
824 DEBUGF("%i %i sfb=%i mdct=%f fft=%f fft-mdct=%f db \n",
826 10*log10(Max(1e-25,ratio->en.s[sfb][i])),
827 10*log10(Max(1e-25,en0)),
828 10*log10((Max(1e-25,en0)/Max(1e-25,ratio->en.s[sfb][i]))));
830 if (sfb==SBMAX_s-2) {
831 DEBUGF("%i %i toti %f %f ratio=%f db \n",gr,ch,
832 10*log10(Max(1e-25,tot2)),
833 10*log(Max(1e-25,tot1)),
834 10*log10(Max(1e-25,tot1)/(Max(1e-25,tot2))));
839 masking: multiplied by en0/fft_energy
840 average seems to be about -135db.
846 /* convert to MDCT units */
847 en1=1e15; /* scaling so it shows up on FFT plot */
848 gfc->pinfo->xfsf_s[gr][ch][3*sfb+i]
849 = en1*xfsf.s[sfb][i]*l3_xmin.s[sfb][i]/bw;
850 gfc->pinfo->en_s[gr][ch][3*sfb+i] = en1*en0;
852 if (ratio->en.s[sfb][i]>0)
853 en0 = en0/ratio->en.s[sfb][i];
856 if (gfp->ATHonly || gfp->ATHshort)
859 gfc->pinfo->thr_s[gr][ch][3*sfb+i] =
860 en1*Max(en0*ratio->thm.s[sfb][i],gfc->ATH->s[sfb]);
863 /* there is no scalefactor bands >= SBPSY_s */
865 gfc->pinfo->LAMEsfb_s[gr][ch][3*sfb+i]=
866 -ifqstep*scalefac->s[sfb][i];
868 gfc->pinfo->LAMEsfb_s[gr][ch][3*sfb+i]=0;
870 gfc->pinfo->LAMEsfb_s[gr][ch][3*sfb+i] -=
871 2*cod_info->subblock_gain[i];
875 for ( sfb = 0; sfb < SBMAX_l; sfb++ ) {
876 start = gfc->scalefac_band.l[ sfb ];
877 end = gfc->scalefac_band.l[ sfb+1 ];
879 for ( en0 = 0.0, l = start; l < end; l++ )
880 en0 += xr[l] * xr[l];
881 if (!gfc->nsPsy.use) en0/=bw;
883 DEBUGF("diff = %f \n",10*log10(Max(ratio[gr][ch].en.l[sfb],1e-20))
884 -(10*log10(en0)+150));
895 tot2 += ratio->en.l[sfb];
898 DEBUGF("%i %i sfb=%i mdct=%f fft=%f fft-mdct=%f db \n",
900 10*log10(Max(1e-25,ratio->en.l[sfb])),
901 10*log10(Max(1e-25,en0)),
902 10*log10((Max(1e-25,en0)/Max(1e-25,ratio->en.l[sfb]))));
904 if (sfb==SBMAX_l-1) {
905 DEBUGF("%i %i toti %f %f ratio=%f db \n",
907 10*log10(Max(1e-25,tot2)),
908 10*log(Max(1e-25,tot1)),
909 10*log10(Max(1e-25,tot1)/(Max(1e-25,tot2))));
912 masking: multiplied by en0/fft_energy
913 average seems to be about -147db.
919 /* convert to MDCT units */
920 en1=1e15; /* scaling so it shows up on FFT plot */
921 gfc->pinfo->xfsf[gr][ch][sfb] = en1*xfsf.l[sfb]*l3_xmin.l[sfb]/bw;
922 gfc->pinfo->en[gr][ch][sfb] = en1*en0;
923 if (ratio->en.l[sfb]>0)
924 en0 = en0/ratio->en.l[sfb];
929 gfc->pinfo->thr[gr][ch][sfb] =
930 en1*Max(en0*ratio->thm.l[sfb],gfc->ATH->l[sfb]);
932 /* there is no scalefactor bands >= SBPSY_l */
934 if (scalefac->l[sfb]<0) /* scfsi! */
935 gfc->pinfo->LAMEsfb[gr][ch][sfb] =
936 gfc->pinfo->LAMEsfb[0][ch][sfb];
938 gfc->pinfo->LAMEsfb[gr][ch][sfb] = -ifqstep*scalefac->l[sfb];
940 gfc->pinfo->LAMEsfb[gr][ch][sfb] = 0;
943 if (cod_info->preflag && sfb>=11)
944 gfc->pinfo->LAMEsfb[gr][ch][sfb] -= ifqstep*pretab[sfb];
946 } /* block type long */
947 gfc->pinfo->LAMEqss [gr][ch] = cod_info->global_gain;
948 gfc->pinfo->LAMEmainbits[gr][ch] = cod_info->part2_3_length;
949 gfc->pinfo->LAMEsfbits [gr][ch] = cod_info->part2_length;
951 gfc->pinfo->over [gr][ch] = noise.over_count;
952 gfc->pinfo->max_noise [gr][ch] = noise.max_noise;
953 gfc->pinfo->over_noise[gr][ch] = noise.over_noise;
954 gfc->pinfo->tot_noise [gr][ch] = noise.tot_noise;
958 /************************************************************************
962 * updates plotting data for a whole frame
964 * Robert Hegemann 2000-10-21
966 ************************************************************************/
968 void set_frame_pinfo(
969 lame_global_flags *gfp,
970 FLOAT8 xr [2][2][576],
971 III_psy_ratio ratio [2][2],
972 int l3_enc [2][2][576],
973 III_scalefac_t scalefac [2][2] )
975 lame_internal_flags *gfc=gfp->internal_flags;
980 III_scalefac_t act_scalefac [2];
981 int scsfi[2] = {0,0};
984 gfc->masking_lower = 1.0;
986 /* reconstruct the scalefactors in case SCSFI was used
988 for (ch = 0; ch < gfc->channels_out; ch ++) {
989 for (sfb = 0; sfb < SBMAX_l; sfb ++) {
990 if (scalefac[1][ch].l[sfb] == -1) {/* scfsi */
991 act_scalefac[ch].l[sfb] = scalefac[0][ch].l[sfb];
994 act_scalefac[ch].l[sfb] = scalefac[1][ch].l[sfb];
999 /* for every granule and channel patch l3_enc and set info
1001 for (gr = 0; gr < gfc->mode_gr; gr ++) {
1002 for (ch = 0; ch < gfc->channels_out; ch ++) {
1004 gr_info *cod_info = &gfc->l3_side.gr[gr].ch[ch].tt;
1006 /* revert back the sign of l3enc */
1007 for ( i = 0; i < 576; i++) {
1008 if (xr[gr][ch][i] < 0)
1009 act_l3enc[i] = -l3_enc[gr][ch][i];
1011 act_l3enc[i] = +l3_enc[gr][ch][i];
1013 if (gr == 1 && scsfi[ch])
1014 set_pinfo (gfp, cod_info, &ratio[gr][ch], &act_scalefac[ch],
1015 xr[gr][ch], act_l3enc, gr, ch);
1017 set_pinfo (gfp, cod_info, &ratio[gr][ch], &scalefac[gr][ch],
1018 xr[gr][ch], act_l3enc, gr, ch);
1026 /*********************************************************************
1027 * nonlinear quantization of xr
1028 * More accurate formula than the ISO formula. Takes into account
1029 * the fact that we are quantizing xr -> ix, but we want ix^4/3 to be
1030 * as close as possible to x^4/3. (taking the nearest int would mean
1031 * ix is as close as possible to xr, which is different.)
1032 * From Segher Boessenkool <segher@eastsite.nl> 11/1999
1033 * ASM optimization from
1034 * Mathew Hendry <scampi@dial.pipex.com> 11/1999
1035 * Acy Stapp <AStapp@austin.rr.com> 11/1999
1036 * Takehiro Tominaga <tominaga@isoternet.org> 11/1999
1037 * 9/00: ASM code removed in favor of IEEE754 hack. If you need
1038 * the ASM code, check CVS circa Aug 2000.
1039 *********************************************************************/
1042 #ifdef TAKEHIRO_IEEE754_HACK
1049 #define MAGIC_FLOAT (65536*(128))
1050 #define MAGIC_INT 0x4b000000
1052 void quantize_xrpow(const FLOAT8 *xp, int *pi, FLOAT8 istep)
1054 /* quantize on xr^(3/4) instead of xr */
1058 fi = (fi_union *)pi;
1059 for (j = 576 / 4 - 1; j >= 0; --j) {
1060 double x0 = istep * xp[0];
1061 double x1 = istep * xp[1];
1062 double x2 = istep * xp[2];
1063 double x3 = istep * xp[3];
1065 x0 += MAGIC_FLOAT; fi[0].f = x0;
1066 x1 += MAGIC_FLOAT; fi[1].f = x1;
1067 x2 += MAGIC_FLOAT; fi[2].f = x2;
1068 x3 += MAGIC_FLOAT; fi[3].f = x3;
1070 fi[0].f = x0 + (adj43asm - MAGIC_INT)[fi[0].i];
1071 fi[1].f = x1 + (adj43asm - MAGIC_INT)[fi[1].i];
1072 fi[2].f = x2 + (adj43asm - MAGIC_INT)[fi[2].i];
1073 fi[3].f = x3 + (adj43asm - MAGIC_INT)[fi[3].i];
1075 fi[0].i -= MAGIC_INT;
1076 fi[1].i -= MAGIC_INT;
1077 fi[2].i -= MAGIC_INT;
1078 fi[3].i -= MAGIC_INT;
1084 # define ROUNDFAC -0.0946
1085 void quantize_xrpow_ISO(const FLOAT8 *xp, int *pi, FLOAT8 istep)
1087 /* quantize on xr^(3/4) instead of xr */
1091 fi = (fi_union *)pi;
1092 for (j=576/4 - 1;j>=0;j--) {
1093 fi[0].f = istep * xp[0] + (ROUNDFAC + MAGIC_FLOAT);
1094 fi[1].f = istep * xp[1] + (ROUNDFAC + MAGIC_FLOAT);
1095 fi[2].f = istep * xp[2] + (ROUNDFAC + MAGIC_FLOAT);
1096 fi[3].f = istep * xp[3] + (ROUNDFAC + MAGIC_FLOAT);
1098 fi[0].i -= MAGIC_INT;
1099 fi[1].i -= MAGIC_INT;
1100 fi[2].i -= MAGIC_INT;
1101 fi[3].i -= MAGIC_INT;
1109 /*********************************************************************
1110 * XRPOW_FTOI is a macro to convert floats to ints.
1111 * if XRPOW_FTOI(x) = nearest_int(x), then QUANTFAC(x)=adj43asm[x]
1114 * if XRPOW_FTOI(x) = floor(x), then QUANTFAC(x)=asj43[x]
1117 * Note: using floor() or (int) is extermely slow. On machines where
1118 * the TAKEHIRO_IEEE754_HACK code above does not work, it is worthwile
1119 * to write some ASM for XRPOW_FTOI().
1120 *********************************************************************/
1121 #define XRPOW_FTOI(src,dest) ((dest) = (int)(src))
1122 #define QUANTFAC(rx) adj43[rx]
1123 #define ROUNDFAC 0.4054
1126 void quantize_xrpow(const FLOAT8 *xr, int *ix, FLOAT8 istep) {
1127 /* quantize on xr^(3/4) instead of xr */
1128 /* from Wilfried.Behne@t-online.de. Reported to be 2x faster than
1129 the above code (when not using ASM) on PowerPC */
1132 for ( j = 576/8; j > 0; --j) {
1133 FLOAT8 x1, x2, x3, x4, x5, x6, x7, x8;
1134 int rx1, rx2, rx3, rx4, rx5, rx6, rx7, rx8;
1137 XRPOW_FTOI(x1, rx1);
1139 XRPOW_FTOI(x2, rx2);
1141 XRPOW_FTOI(x3, rx3);
1143 XRPOW_FTOI(x4, rx4);
1145 XRPOW_FTOI(x5, rx5);
1147 XRPOW_FTOI(x6, rx6);
1149 XRPOW_FTOI(x7, rx7);
1150 x1 += QUANTFAC(rx1);
1151 XRPOW_FTOI(x8, rx8);
1152 x2 += QUANTFAC(rx2);
1153 XRPOW_FTOI(x1,*ix++);
1154 x3 += QUANTFAC(rx3);
1155 XRPOW_FTOI(x2,*ix++);
1156 x4 += QUANTFAC(rx4);
1157 XRPOW_FTOI(x3,*ix++);
1158 x5 += QUANTFAC(rx5);
1159 XRPOW_FTOI(x4,*ix++);
1160 x6 += QUANTFAC(rx6);
1161 XRPOW_FTOI(x5,*ix++);
1162 x7 += QUANTFAC(rx7);
1163 XRPOW_FTOI(x6,*ix++);
1164 x8 += QUANTFAC(rx8);
1165 XRPOW_FTOI(x7,*ix++);
1166 XRPOW_FTOI(x8,*ix++);
1175 void quantize_xrpow_ISO( const FLOAT8 *xr, int *ix, FLOAT8 istep )
1177 /* quantize on xr^(3/4) instead of xr */
1178 const FLOAT8 compareval0 = (1.0 - 0.4054)/istep;
1180 /* depending on architecture, it may be worth calculating a few more
1183 eg. compareval1 = (2.0 - 0.4054/istep);
1184 .. and then after the first compare do this ...
1185 if compareval1>*xr then ix = 1;
1187 On a pentium166, it's only worth doing the one compare (as done here),
1188 as the second compare becomes more expensive than just calculating
1189 the value. Architectures with slow FP operations may want to add some
1190 more comparevals. try it and send your diffs statistically speaking
1192 73% of all xr*istep values give ix=0
1196 for (j=576;j>0;j--) {
1197 if (compareval0 > *xr) {
1201 /* *(ix++) = (int)( istep*(*(xr++)) + 0.4054); */
1202 XRPOW_FTOI( istep*(*(xr++)) + ROUNDFAC , *(ix++) );