📄 quantize-pvt.c
字号:
table_number = 2; else table_number = 0; for ( i = 0; i < 4; i++ ) max_sfac[i] = 0; if ( cod_info->block_type == SHORT_TYPE ) { row_in_table = 1; partition_table = &nr_of_sfb_block[table_number][row_in_table][0]; for ( sfb = 0, partition = 0; partition < 4; partition++ ) { nr_sfb = partition_table[ partition ] / 3; for ( i = 0; i < nr_sfb; i++, sfb++ ) for ( window = 0; window < 3; window++ ) if ( scalefac->s[sfb][window] > max_sfac[partition] ) max_sfac[partition] = scalefac->s[sfb][window]; } } else { row_in_table = 0; partition_table = &nr_of_sfb_block[table_number][row_in_table][0]; for ( sfb = 0, partition = 0; partition < 4; partition++ ) { nr_sfb = partition_table[ partition ]; for ( i = 0; i < nr_sfb; i++, sfb++ ) if ( scalefac->l[sfb] > max_sfac[partition] ) max_sfac[partition] = scalefac->l[sfb]; } } for ( over = 0, partition = 0; partition < 4; partition++ ) { if ( max_sfac[partition] > (int)max_range_sfac_tab[table_number][partition] ) over++; } if ( !over ) { /* Since no bands have been over-amplified, we can set scalefac_compress and slen[] for the formatter */ static const int log2tab[] = { 0, 1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4 }; unsigned int slen1, slen2, slen3, slen4; cod_info->sfb_partition_table = &nr_of_sfb_block[table_number][row_in_table][0]; for ( partition = 0; partition < 4; partition++ ) cod_info->slen[partition] = log2tab[max_sfac[partition]]; /* set scalefac_compress */ slen1 = cod_info->slen[ 0 ]; slen2 = cod_info->slen[ 1 ]; slen3 = cod_info->slen[ 2 ]; slen4 = cod_info->slen[ 3 ]; switch ( table_number ) { case 0: cod_info->scalefac_compress = (((slen1 * 5) + slen2) << 4) + (slen3 << 2) + slen4; break; case 1: cod_info->scalefac_compress = 400 + (((slen1 * 5) + slen2) << 2) + slen3; break; case 2: cod_info->scalefac_compress = 500 + (slen1 * 3) + slen2; break; default: ERRORF("intensity stereo not implemented yet\n" ); break; } }#ifdef DEBUG if ( over ) ERRORF( "---WARNING !! Amplification of some bands over limits\n" );#endif if (!over) { assert( cod_info->sfb_partition_table ); cod_info->part2_length=0; for ( partition = 0; partition < 4; partition++ ) cod_info->part2_length += cod_info->slen[partition] * cod_info->sfb_partition_table[partition]; } return over;}/*************************************************************************//* calc_xmin *//*************************************************************************//* Calculate the allowed distortion for each scalefactor band, as determined by the psychoacoustic model. xmin(sb) = ratio(sb) * en(sb) / bw(sb) returns number of sfb's with energy > ATH*/int calc_xmin( lame_global_flags *gfp,FLOAT8 xr[576], III_psy_ratio *ratio, gr_info *cod_info, III_psy_xmin *l3_xmin){ lame_internal_flags *gfc=gfp->internal_flags; int j,start, end, bw,l, b, ath_over=0; u_int sfb; FLOAT8 en0, xmin, ener; if (cod_info->block_type==SHORT_TYPE) { for ( j=0, sfb = 0; sfb < SBMAX_s; sfb++ ) { start = gfc->scalefac_band.s[ sfb ]; end = gfc->scalefac_band.s[ sfb + 1 ]; bw = end - start; for ( b = 0; b < 3; b++ ) { for (en0 = 0.0, l = start; l < end; l++) { ener = xr[j++]; ener = ener * ener; en0 += ener; } en0 /= bw; if (gfp->ATHonly || gfp->ATHshort) { l3_xmin->s[sfb][b]=gfc->ATH_s[sfb]; } else { xmin = ratio->en.s[sfb][b]; if (xmin > 0.0) xmin = en0 * ratio->thm.s[sfb][b] * gfc->masking_lower / xmin; l3_xmin->s[sfb][b] = Max(gfc->ATH_s[sfb], xmin); } if (en0 > gfc->ATH_s[sfb]) ath_over++; } } }else{ for ( sfb = 0; sfb < SBMAX_l; sfb++ ){ start = gfc->scalefac_band.l[ sfb ]; end = gfc->scalefac_band.l[ sfb+1 ]; bw = end - start; for (en0 = 0.0, l = start; l < end; l++ ) { ener = xr[l] * xr[l]; en0 += ener; } en0 /= bw; if (gfp->ATHonly) { l3_xmin->l[sfb]=gfc->ATH_l[sfb]; } else { xmin = ratio->en.l[sfb]; if (xmin > 0.0) xmin = en0 * ratio->thm.l[sfb] * gfc->masking_lower / xmin; l3_xmin->l[sfb]=Max(gfc->ATH_l[sfb], xmin); } if (en0 > gfc->ATH_l[sfb]) ath_over++; } } return ath_over;}/*************************************************************************//* calc_noise *//*************************************************************************//* mt 5/99: Function: Improved calc_noise for a single channel */int calc_noise( lame_global_flags *gfp, FLOAT8 xr[576], int ix[576], gr_info *cod_info, FLOAT8 xfsf[4][SBMAX_l], FLOAT8 distort[4][SBMAX_l], III_psy_xmin *l3_xmin, III_scalefac_t *scalefac, calc_noise_result *res){ int start, end, j,l, i, over=0; u_int sfb; FLOAT8 sum, bw; lame_internal_flags *gfc=gfp->internal_flags; int count=0; FLOAT8 noise; FLOAT8 over_noise=0; FLOAT8 tot_noise=0; FLOAT8 max_noise = -999; if (cod_info->block_type == SHORT_TYPE) { int max_index = SBPSY_s; if (gfp->VBR==vbr_rh || gfp->VBR==vbr_mt) { max_index = SBMAX_s; } for ( j=0, sfb = 0; sfb < max_index; sfb++ ) { start = gfc->scalefac_band.s[ sfb ]; end = gfc->scalefac_band.s[ sfb+1 ]; bw = end - start; for ( i = 0; i < 3; i++ ) { FLOAT8 step; int s; s = (scalefac->s[sfb][i] << (cod_info->scalefac_scale + 1)) + cod_info->subblock_gain[i] * 8; s = cod_info->global_gain - s; assert(s<Q_MAX); assert(s>=0); step = POW20(s); for ( sum = 0.0, l = start; l < end; l++ ) { FLOAT8 temp; temp = fabs(xr[j]) - pow43[ix[j]] * step; ++j;#ifdef MAXNOISE temp = bw*temp*temp; sum = Max(sum,temp);#else sum += temp * temp;#endif } xfsf[i+1][sfb] = sum / bw; /* max -30db noise below threshold */ noise = 10*log10(Max(.001,xfsf[i+1][sfb] / l3_xmin->s[sfb][i] )); distort[i+1][sfb] = noise; if (noise > 0) { over++; over_noise += noise; } tot_noise += noise; max_noise=Max(max_noise,noise); count++; } } }else{ int max_index = SBPSY_l; if (gfp->VBR==vbr_rh || gfp->VBR==vbr_mt) { max_index = SBMAX_l; } for ( sfb = 0; sfb < max_index; sfb++ ) { FLOAT8 step; int s = scalefac->l[sfb]; if (cod_info->preflag) s += pretab[sfb]; s = cod_info->global_gain - (s << (cod_info->scalefac_scale + 1)); assert(s<Q_MAX); assert(s>=0); step = POW20(s); start = gfc->scalefac_band.l[ sfb ]; end = gfc->scalefac_band.l[ sfb+1 ]; bw = end - start; for ( sum = 0.0, l = start; l < end; l++ ) { FLOAT8 temp; temp = fabs(xr[l]) - pow43[ix[l]] * step;#ifdef MAXNOISE temp = bw*temp*temp; sum = Max(sum,temp);#else sum += temp * temp;#endif } xfsf[0][sfb] = sum / bw; /* max -30db noise below threshold */ noise = 10*log10(Max(.001,xfsf[0][sfb] / l3_xmin->l[sfb])); distort[0][sfb] = noise; if (noise>0) { over++; over_noise += noise; } tot_noise += noise; max_noise=Max(max_noise,noise); count++; } } res->tot_count = count; res->over_count = over; res->tot_noise = tot_noise; res->over_noise = over_noise; res->max_noise = max_noise; res->tot_avg_noise = tot_noise / count; res->over_avg_noise = over_noise / count; return over;}/*************************************************************************//* loop_break *//*************************************************************************//* Function: Returns zero if there is a scalefac which has not been amplified. Otherwise it returns one. */int loop_break( III_scalefac_t *scalefac, gr_info *cod_info){ int i; u_int sfb; for ( sfb = 0; sfb < cod_info->sfb_lmax; sfb++ ) if ( scalefac->l[sfb] == 0 ) return 0; for ( sfb = cod_info->sfb_smax; sfb < SBPSY_s; sfb++ ) for ( i = 0; i < 3; i++ ) if ( scalefac->s[sfb][i]+cod_info->subblock_gain[i] == 0 ) return 0; return 1;}/* ---------------------------------------------------------------------- if someone wants to try to find a faster step search function, here is some code which gives a lower bound for the step size: for (max_xrspow = 0, i = 0; i < 576; ++i) { max_xrspow = Max(max_xrspow, xrspow[i]); } lowerbound = 210+log10(max_xrspow/IXMAX_VAL)/(0.1875*LOG2); Robert.Hegemann@gmx.de ----------------------------------------------------------------------*/typedef enum { BINSEARCH_NONE, BINSEARCH_UP, BINSEARCH_DOWN} binsearchDirection_t;/*-------------------------------------------------------------------------*/int bin_search_StepSize2 (lame_global_flags *gfp,int desired_rate, int start, int *ix, FLOAT8 xrspow[576], gr_info *cod_info)/*-------------------------------------------------------------------------*/{ int nBits; int flag_GoneOver = 0; int StepSize = start; int CurrentStep; lame_internal_flags *gfc=gfp->internal_flags; binsearchDirection_t Direction = BINSEARCH_NONE; assert(gfc->CurrentStep); CurrentStep = gfc->CurrentStep; do { cod_info->global_gain = StepSize; nBits = count_bits(gfp,ix, xrspow, cod_info); if (CurrentStep == 1 ) { break; /* nothing to adjust anymore */ } if (flag_GoneOver) { CurrentStep /= 2; } if (nBits > desired_rate) /* increase Quantize_StepSize */ { if (Direction == BINSEARCH_DOWN && !flag_GoneOver) { flag_GoneOver = 1; CurrentStep /= 2; /* late adjust */ } Direction = BINSEARCH_UP; StepSize += CurrentStep; if (StepSize > 255) break; } else if (nBits < desired_rate) { if (Direction == BINSEARCH_UP && !flag_GoneOver) { flag_GoneOver = 1; CurrentStep /= 2; /* late adjust */ } Direction = BINSEARCH_DOWN; StepSize -= CurrentStep; if (StepSize < 0) break; } else break; /* nBits == desired_rate;; most unlikely to happen.*/ } while (1); /* For-ever, break is adjusted. */ CurrentStep = abs(start - StepSize); if (CurrentStep >= 4) { CurrentStep = 4; } else { CurrentStep = 2; } gfc->CurrentStep = CurrentStep; return nBits;}/************************************************************************//* updates plotting data *//************************************************************************/void set_pinfo (lame_global_flags *gfp, gr_info *cod_info, III_psy_ratio *ratio, III_scalefac_t *scalefac, FLOAT8 xr[576], FLOAT8 xfsf[4][SBMAX_l], FLOAT8 noise[4], int gr, int ch){ lame_internal_flags *gfc=gfp->internal_flags; int sfb; int j,i,l,start,end,bw; FLOAT8 en0,en1; D192_3 *xr_s = (D192_3 *)xr; FLOAT ifqstep = ( cod_info->scalefac_scale == 0 ) ? .5 : 1.0; if (cod_info->block_type == SHORT_TYPE) { for (j=0, sfb = 0; sfb < SBMAX_s; sfb++ ) { start = gfc->scalefac_band.s[ sfb ]; end = gfc->scalefac_band.s[ sfb + 1 ]; bw = end - start; for ( i = 0; i < 3; i++ ) { for ( en0 = 0.0, l = start; l < end; l++ ) { en0 += xr[j] * xr[j]; ++j; } en0=Max(en0/bw,1e-20);#if 0 {double tot1,tot2; if (sfb<SBMAX_s-1) { if (sfb==0) { tot1=0; tot2=0; } tot1 += en0; tot2 += ratio->en.s[sfb][i]; DEBUGF("%i %i sfb=%i mdct=%f fft=%f fft-mdct=%f db \n",gr,ch,sfb,10*log10(Max(1e-25,ratio->en.s[sfb][i])),10*log10(Max(1e-25,en0)),10*log10((Max(1e-25,en0)/Max(1e-25,ratio->en.s[sfb][i])))); if (sfb==SBMAX_s-2) { DEBUGF("%i %i toti %f %f ratio=%f db \n",gr,ch,10*log10(Max(1e-25,tot2)),10*log(Max(1e-25,tot1)),10*log10(Max(1e-25,tot1)/(Max(1e-25,tot2)))); } } /*masking: multiplied by en0/fft_energyaverage seems to be about -135db. */ }#endif /* convert to MDCT units */ en1=1e15; /* scaling so it shows up on FFT plot */ gfc->pinfo->xfsf_s[gr][ch][3*sfb+i] = en1*xfsf[i+1][sfb]; gfc->pinfo->en_s[gr][ch][3*sfb+i] = en1*en0; if (ratio->en.s[sfb][i]>0) en0 = en0/ratio->en.s[sfb][i]; else en0=0; if (gfp->ATHonly || gfp->ATHshort) en0=0; gfc->pinfo->thr_s[gr][ch][3*sfb+i] = en1*Max(en0*ratio->thm.s[sfb][i],gfc->ATH_s[sfb]); /* there is no scalefactor bands >= SBPSY_s */ if (sfb < SBPSY_s) { gfc->pinfo->LAMEsfb_s[gr][ch][3*sfb+i]= -ifqstep*scalefac->s[sfb][i]; } else { gfc->pinfo->LAMEsfb_s[gr][ch][3*sfb+i]=0; } gfc->pinfo->LAMEsfb_s[gr][ch][3*sfb+i] -= 2*cod_info->subblock_gain[i]; } } }else{ for ( sfb = 0; sfb < SBMAX_l; sfb++ ) { start = gfc->scalefac_band.l[ sfb ]; end = gfc->scalefac_band.l[ sfb+1 ]; bw = end - start; for ( en0 = 0.0, l = start; l < end; l++ ) en0 += xr[l] * xr[l]; en0/=bw; /* DEBUGF("diff = %f \n",10*log10(Max(ratio[gr][ch].en.l[sfb],1e-20)) -(10*log10(en0)+150)); */#if 0 {double tot1,tot2; if (sfb==0) { tot1=0;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -