📄 quantize.c
字号:
for (gr = 0; gr < gfc->mode_gr; gr++) { for (ch = 0; ch < gfc->channels_out; ch++) { if (bits > frameBits[gfc->VBR_max_bitrate]) { max_bits[gr][ch] *= frameBits[gfc->VBR_max_bitrate]; max_bits[gr][ch] /= bits; } if (min_bits[gr][ch] > max_bits[gr][ch]) min_bits[gr][ch] = max_bits[gr][ch]; } /* for ch */ } /* for gr */ *min_mean_bits = Max(*min_mean_bits, 126); return analog_silence;}static voidbitpressure_strategy( lame_internal_flags * gfc, FLOAT l3_xmin[2][2][SFBMAX], int min_bits[2][2], int max_bits[2][2] ) { int gr, ch, sfb; for (gr = 0; gr < gfc->mode_gr; gr++) { for (ch = 0; ch < gfc->channels_out; ch++) { gr_info *gi = &gfc->l3_side.tt[gr][ch]; FLOAT *pxmin = l3_xmin[gr][ch]; for (sfb = 0; sfb < gi->psy_lmax; sfb++) *pxmin++ *= 1.+.029*sfb*sfb/SBMAX_l/SBMAX_l; if (gi->block_type == SHORT_TYPE) { for (sfb = gi->sfb_smin; sfb < SBMAX_s; sfb++) { *pxmin++ *= 1.+.029*sfb*sfb/SBMAX_s/SBMAX_s; *pxmin++ *= 1.+.029*sfb*sfb/SBMAX_s/SBMAX_s; *pxmin++ *= 1.+.029*sfb*sfb/SBMAX_s/SBMAX_s; } } max_bits[gr][ch] = Max(min_bits[gr][ch], 0.9*max_bits[gr][ch]); } }}/************************************************************************ * * VBR_iteration_loop() * * tries to find out how many bits are needed for each granule and channel * to get an acceptable quantization. An appropriate bitrate will then be * choosed for quantization. rh 8/99 * * Robert Hegemann 2000-09-06 rewrite * ************************************************************************/void VBR_iteration_loop ( lame_global_flags *gfp, FLOAT pe [2][2], FLOAT ms_ener_ratio[2], III_psy_ratio ratio[2][2]){ lame_internal_flags *gfc=gfp->internal_flags; FLOAT l3_xmin[2][2][SFBMAX]; FLOAT xrpow[576]; int bands[2][2]; int frameBits[15]; int save_bits[2][2]; int used_bits, used_bits2; int bits; int min_bits[2][2], max_bits[2][2]; int analog_mean_bits, min_mean_bits; int mean_bits; int ch, gr, analog_silence; III_side_info_t *l3_side = &gfc->l3_side; analog_silence = VBR_prepare (gfp, pe, ms_ener_ratio, ratio, l3_xmin, frameBits, &analog_mean_bits, &min_mean_bits, min_bits, max_bits, bands); /*---------------------------------*/ for(;;) { /* quantize granules with lowest possible number of bits */ used_bits = 0; used_bits2 = 0; for (gr = 0; gr < gfc->mode_gr; gr++) { for (ch = 0; ch < gfc->channels_out; ch++) { int ret; gr_info *cod_info = &l3_side->tt[gr][ch]; /* init_outer_loop sets up cod_info, scalefac and xrpow */ ret = init_xrpow(gfc, cod_info, xrpow); if (ret == 0 || max_bits[gr][ch] == 0) { /* xr contains no energy * l3_enc, our encoding data, will be quantized to zero */ save_bits[gr][ch] = 0; continue; /* with next channel */ } if (gfp->VBR == vbr_mtrh) { VBR_noise_shaping (gfc, xrpow, l3_xmin[gr][ch], max_bits[gr][ch], gr, ch ); } else VBR_encode_granule (gfp, cod_info, l3_xmin[gr][ch], xrpow, ch, min_bits[gr][ch], max_bits[gr][ch] ); /* do the 'substep shaping' */ if (gfc->substep_shaping & 1) { trancate_smallspectrums(gfc, &l3_side->tt[gr][ch], l3_xmin[gr][ch], xrpow); } ret = cod_info->part2_3_length + cod_info->part2_length; used_bits += ret; save_bits[gr][ch] = Min(MAX_BITS, ret); used_bits2 += Min(MAX_BITS, ret); } /* for ch */ } /* for gr */ /* find lowest bitrate able to hold used bits */ if (analog_silence && !gfp->VBR_hard_min) /* we detected analog silence and the user did not specify * any hard framesize limit, so start with smallest possible frame */ gfc->bitrate_index = 1; else gfc->bitrate_index = gfc->VBR_min_bitrate; for( ; gfc->bitrate_index < gfc->VBR_max_bitrate; gfc->bitrate_index++) { if (used_bits <= frameBits[gfc->bitrate_index]) break; } bits = ResvFrameBegin (gfp, &mean_bits); if (used_bits <= bits) break; bitpressure_strategy( gfc, l3_xmin, min_bits, max_bits ); } /* breaks adjusted */ /*--------------------------------------*/ for (gr = 0; gr < gfc->mode_gr; gr++) { for (ch = 0; ch < gfc->channels_out; ch++) { iteration_finish_one(gfc, gr, ch); } /* for ch */ } /* for gr */ ResvFrameEnd (gfc, mean_bits);}/******************************************************************** * * calc_target_bits() * * calculates target bits for ABR encoding * * mt 2000/05/31 * ********************************************************************/static void calc_target_bits ( lame_global_flags * gfp, FLOAT pe [2][2], FLOAT ms_ener_ratio [2], int targ_bits [2][2], int *analog_silence_bits, int *max_frame_bits ){ lame_internal_flags *gfc=gfp->internal_flags; III_side_info_t *l3_side = &gfc->l3_side; FLOAT res_factor; int gr, ch, totbits, mean_bits; gfc->bitrate_index = gfc->VBR_max_bitrate; *max_frame_bits = ResvFrameBegin (gfp, &mean_bits); gfc->bitrate_index = 1; mean_bits = getframebits(gfp) - gfc->sideinfo_len * 8; *analog_silence_bits = mean_bits / (gfc->mode_gr * gfc->channels_out); mean_bits = gfp->VBR_mean_bitrate_kbps * gfp->framesize * 1000; if (gfc->substep_shaping & 1) mean_bits *= 1.09; mean_bits /= gfp->out_samplerate; mean_bits -= gfc->sideinfo_len*8; mean_bits /= (gfc->mode_gr * gfc->channels_out); /* res_factor is the percentage of the target bitrate that should be used on average. the remaining bits are added to the bitreservoir and used for difficult to encode frames. Since we are tracking the average bitrate, we should adjust res_factor "on the fly", increasing it if the average bitrate is greater than the requested bitrate, and decreasing it otherwise. Reasonable ranges are from .9 to 1.0 Until we get the above suggestion working, we use the following tuning: compression ratio res_factor 5.5 (256kbps) 1.0 no need for bitreservoir 11 (128kbps) .93 7% held for reservoir with linear interpolation for other values. */ res_factor = .93 + .07 * (11.0 - gfp->compression_ratio) / (11.0 - 5.5); if (res_factor < .90) res_factor = .90; if (res_factor > 1.00) res_factor = 1.00; for (gr = 0; gr < gfc->mode_gr; gr++) { for (ch = 0; ch < gfc->channels_out; ch++) { targ_bits[gr][ch] = res_factor * mean_bits; if (pe[gr][ch] > 700) { int add_bits = (pe[gr][ch] - 700) / 1.4; gr_info *cod_info = &l3_side->tt[gr][ch]; targ_bits[gr][ch] = res_factor * mean_bits; /* short blocks use a little extra, no matter what the pe */ if (cod_info->block_type == SHORT_TYPE) { if (add_bits < mean_bits/2) add_bits = mean_bits/2; } /* at most increase bits by 1.5*average */ if (add_bits > mean_bits*3/2) add_bits = mean_bits*3/2; else if (add_bits < 0) add_bits = 0; targ_bits[gr][ch] += add_bits; } }/* for ch */ } /* for gr */ if (gfc->mode_ext == MPG_MD_MS_LR) for (gr = 0; gr < gfc->mode_gr; gr++) { reduce_side (targ_bits[gr], ms_ener_ratio[gr], mean_bits*gfc->channels_out, MAX_BITS); } /* sum target bits */ totbits=0; for (gr = 0; gr < gfc->mode_gr; gr++) { for (ch = 0; ch < gfc->channels_out; ch++) { if (targ_bits[gr][ch] > MAX_BITS) targ_bits[gr][ch] = MAX_BITS; totbits += targ_bits[gr][ch]; } } /* repartion target bits if needed */ if (totbits > *max_frame_bits) { for(gr = 0; gr < gfc->mode_gr; gr++) { for(ch = 0; ch < gfc->channels_out; ch++) { targ_bits[gr][ch] *= *max_frame_bits; targ_bits[gr][ch] /= totbits; } } }}/******************************************************************** * * ABR_iteration_loop() * * encode a frame with a disired average bitrate * * mt 2000/05/31 * ********************************************************************/void ABR_iteration_loop( lame_global_flags *gfp, FLOAT pe [2][2], FLOAT ms_ener_ratio[2], III_psy_ratio ratio [2][2]){ lame_internal_flags *gfc=gfp->internal_flags; FLOAT l3_xmin[SFBMAX]; FLOAT xrpow[576]; int targ_bits[2][2]; int mean_bits, max_frame_bits; int ch, gr, ath_over; int analog_silence_bits; gr_info *cod_info; III_side_info_t *l3_side = &gfc->l3_side; calc_target_bits (gfp, pe, ms_ener_ratio, targ_bits, &analog_silence_bits, &max_frame_bits); /* encode granules */ for (gr = 0; gr < gfc->mode_gr; gr++) { if (gfc->mode_ext == MPG_MD_MS_LR) { ms_convert (&gfc->l3_side, gr); ms_sparsing( gfc, gr ); } for (ch = 0; ch < gfc->channels_out; ch++) { FLOAT adjust, masking_lower_db; cod_info = &l3_side->tt[gr][ch]; if (cod_info->block_type == NORM_TYPE) { /* adjust = 1.28/(1+exp(3.5-pe[gr][ch]/300.))-0.05;*/ adjust = 0; masking_lower_db = gfc->PSY->mask_adjust - adjust; } else { /* adjust = 2.56/(1+exp(3.5-pe[gr][ch]/300.))-0.14;*/ adjust = 0; masking_lower_db = gfc->PSY->mask_adjust_short - adjust; } gfc->masking_lower = pow (10.0, masking_lower_db * 0.1); /* cod_info, scalefac and xrpow get initialized in init_outer_loop */ init_outer_loop(gfp, gfc, cod_info); if (init_xrpow(gfc, cod_info, xrpow)) { /* xr contains energy we will have to encode * calculate the masking abilities * find some good quantization in outer_loop */ ath_over = calc_xmin (gfp, &ratio[gr][ch], cod_info, l3_xmin); if (0 == ath_over) /* analog silence */ targ_bits[gr][ch] = analog_silence_bits; outer_loop (gfp, cod_info, l3_xmin, xrpow, ch, targ_bits[gr][ch]); } iteration_finish_one(gfc, gr, ch); } /* ch */ } /* gr */ /* find a bitrate which can refill the resevoir to positive size. */ for (gfc->bitrate_index = gfc->VBR_min_bitrate ; gfc->bitrate_index <= gfc->VBR_max_bitrate; gfc->bitrate_index++ ) { if (ResvFrameBegin (gfp, &mean_bits) >= 0) break; } assert (gfc->bitrate_index <= gfc->VBR_max_bitrate); ResvFrameEnd (gfc, mean_bits);}/************************************************************************ * * CBR_iteration_loop() * * author/date?? * * encodes one frame of MP3 data with constant bitrate * ************************************************************************/void CBR_iteration_loop( lame_global_flags *gfp, FLOAT pe [2][2], FLOAT ms_ener_ratio[2], III_psy_ratio ratio [2][2]){ lame_internal_flags *gfc=gfp->internal_flags; FLOAT l3_xmin[SFBMAX]; FLOAT xrpow[576]; int targ_bits[2]; int mean_bits, max_bits; int gr, ch; III_side_info_t *l3_side = &gfc->l3_side; gr_info *cod_info; ResvFrameBegin (gfp, &mean_bits); /* quantize! */ for (gr = 0; gr < gfc->mode_gr; gr++) { /* calculate needed bits */ max_bits = on_pe (gfp, pe, l3_side, targ_bits, mean_bits, gr, gr); if (gfc->mode_ext == MPG_MD_MS_LR) { ms_convert (&gfc->l3_side, gr); ms_sparsing( gfc, gr ); reduce_side (targ_bits, ms_ener_ratio[gr], mean_bits, max_bits); } for (ch=0 ; ch < gfc->channels_out ; ch ++) { FLOAT adjust, masking_lower_db; cod_info = &l3_side->tt[gr][ch]; if (cod_info->block_type == NORM_TYPE) { /* adjust = 1.28/(1+exp(3.5-pe[gr][ch]/300.))-0.05;*/ adjust = 0; masking_lower_db = gfc->PSY->mask_adjust - adjust; } else { /* adjust = 2.56/(1+exp(3.5-pe[gr][ch]/300.))-0.14;*/ adjust
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -