📄 quantize.c
字号:
assert(iteration != 1); /* scale factors too large, not enough bits. use previous quantizaton */ notdone=0; } else { /* if this is the first iteration, see if we can reuse the quantization * computed in bin_search_StepSize above */ int real_bits; if (iteration==1) { if(bits_found>huff_bits) { cod_info->global_gain++; real_bits = inner_loop(gfp,xrpow, l3_enc_w, huff_bits, cod_info); } else real_bits=bits_found; } else { real_bits=inner_loop(gfp,xrpow, l3_enc_w, huff_bits, cod_info); } cod_info->part2_3_length = real_bits; /* compute the distortion in this quantization */ if (gfc->noise_shaping==0) { over=0; }else{ /* coefficients and thresholds both l/r (or both mid/side) */ over=calc_noise( gfp,xr, l3_enc_w, cod_info, xfsf_w,distort, l3_xmin, &scalefac_w, &noise_info); } /* check if this quantization is better the our saved quantization */ if (iteration == 1) better=1; else better=quant_compare(gfp->experimentalX, &best_noise_info, &noise_info); /* save data so we can restore this quantization later */ if (better) { best_noise_info = noise_info; memcpy(scalefac, &scalefac_w, sizeof(III_scalefac_t)); memcpy(l3_enc,l3_enc_w,sizeof(int)*576); memcpy(&save_cod_info,cod_info,sizeof(save_cod_info)); if (gfp->gtkflag) { memcpy(xfsf, xfsf_w, sizeof(xfsf_w)); } } } /* if no bands with distortion, we are done */ if (!gfp->experimentalX) { if (gfc->noise_shaping_stop==0 && over==0) notdone=0; } else { /* do at least 7 tries and stop * if our best quantization so far had no distorted bands * this gives us more possibilities for our different quant_compare modes */ if (iteration>7 && gfc->noise_shaping_stop==0 && best_noise_info.over_count==0) notdone=0; } if (notdone) { amp_scalefac_bands( gfp, xrpow, cod_info, &scalefac_w, distort); /* check to make sure we have not amplified too much */ /* loop_break returns 0 if there is an unamplified scalefac */ /* scale_bitcount returns 0 if no scalefactors are too large */ if ( (status = loop_break(&scalefac_w, cod_info)) == 0 ) { /* not all scalefactors have been amplified. so these * scalefacs are possibly valid. encode them: */ if ( gfp->version == 1 ) { status = scale_bitcount(&scalefac_w, cod_info); }else{ status = scale_bitcount_lsf(&scalefac_w, cod_info); } if (status) { /* some scalefactors are too large. lets try setting * scalefac_scale=1 */ if (gfc->noise_shaping > 1 && !cod_info->scalefac_scale) { inc_scalefac_scale(gfp, &scalefac_w, cod_info, xrpow); status = 0; } else if (cod_info->block_type == SHORT_TYPE && gfp->experimentalZ && gfc->noise_shaping > 1) { inc_subblock_gain(gfp, &scalefac_w, cod_info, xrpow); status = loop_break(&scalefac_w, cod_info); } if (!status) { if ( gfp->version == 1 ) { status = scale_bitcount(&scalefac_w, cod_info); }else{ status = scale_bitcount_lsf(&scalefac_w, cod_info); } } } } notdone = !status; } /* Check if the last scalefactor band is distorted. * in VBR mode we can't get rid of the distortion, so quit now * and VBR mode will try again with more bits. * (makes a 10% speed increase, the files I tested were * binary identical, 2000/05/20 Robert.Hegemann@gmx.de) */ if (gfp->VBR==vbr_rh || iteration>100) { if (cod_info->block_type == SHORT_TYPE) { if ((distort[1][SBMAX_s-1] > 0) ||(distort[2][SBMAX_s-1] > 0) ||(distort[3][SBMAX_s-1] > 0)) notdone=0; } else { if (distort[0][SBMAX_l-1] > 0) notdone=0; } } } /* done with main iteration */ memcpy(cod_info,&save_cod_info,sizeof(save_cod_info)); cod_info->part2_3_length += cod_info->part2_length; /* finish up */ assert( cod_info->global_gain < 256 ); best_noise[0]=best_noise_info.over_count; best_noise[1]=best_noise_info.max_noise; best_noise[2]=best_noise_info.over_avg_noise; best_noise[3]=best_noise_info.tot_avg_noise;} /*************************************************************************//* amp_scalefac_bands *//*************************************************************************//* Amplify the scalefactor bands that violate the masking threshold. See ISO 11172-3 Section C.1.5.4.3.5*/void amp_scalefac_bands(lame_global_flags *gfp, FLOAT8 xrpow[576], gr_info *cod_info, III_scalefac_t *scalefac, FLOAT8 distort[4][SBMAX_l]){ int start, end, l,i,j; u_int sfb; FLOAT8 ifqstep34; FLOAT8 distort_thresh; lame_internal_flags *gfc=gfp->internal_flags; if ( cod_info->scalefac_scale == 0 ) ifqstep34 = 1.29683955465100964055; /* 2**(.75*.5)*/ else ifqstep34 = 1.68179283050742922612; /* 2**(.75*1) */ /* distort_thresh = 0, unless all bands have distortion * less than masking. In that case, just amplify bands with distortion * within 95% of largest distortion/masking ratio */ distort_thresh = -900; for ( sfb = 0; sfb < cod_info->sfb_lmax; sfb++ ) { distort_thresh = Max(distort[0][sfb],distort_thresh); } for ( sfb = cod_info->sfb_smax; sfb < 12; sfb++ ) { for ( i = 0; i < 3; i++ ) { distort_thresh = Max(distort[i+1][sfb],distort_thresh); } } distort_thresh=Min(distort_thresh * 1.05, 0.0); for ( sfb = 0; sfb < cod_info->sfb_lmax; sfb++ ) { if ( distort[0][sfb]>distort_thresh ) { scalefac->l[sfb]++; start = gfc->scalefac_band.l[sfb]; end = gfc->scalefac_band.l[sfb+1]; for ( l = start; l < end; l++ ) xrpow[l] *= ifqstep34; } } for ( j=0,sfb = cod_info->sfb_smax; sfb < 12; sfb++ ) { start = gfc->scalefac_band.s[sfb]; end = gfc->scalefac_band.s[sfb+1]; for ( i = 0; i < 3; i++ ) { int j2 = j; if ( distort[i+1][sfb]>distort_thresh) { scalefac->s[sfb][i]++; for (l = start; l < end; l++) xrpow[j2++] *= ifqstep34; } j += end-start; } }}void inc_scalefac_scale(lame_global_flags *gfp, III_scalefac_t *scalefac, gr_info *cod_info, FLOAT8 xrpow[576]){ int start, end, l,i,j; int sfb; lame_internal_flags *gfc=gfp->internal_flags; const FLOAT8 ifqstep34 = 1.29683955465100964055; for ( sfb = 0; sfb < cod_info->sfb_lmax; sfb++ ) { int s = scalefac->l[sfb]; if (cod_info->preflag) s += pretab[sfb]; if (s & 1) { s++; start = gfc->scalefac_band.l[sfb]; end = gfc->scalefac_band.l[sfb+1]; for ( l = start; l < end; l++ ) xrpow[l] *= ifqstep34; } scalefac->l[sfb] = s >> 1; cod_info->preflag = 0; } for ( j=0,sfb = cod_info->sfb_smax; sfb < 12; sfb++ ) { start = gfc->scalefac_band.s[sfb]; end = gfc->scalefac_band.s[sfb+1]; for ( i = 0; i < 3; i++ ) { int j2=j; if (scalefac->s[sfb][i] & 1) { scalefac->s[sfb][i]++; for (l = start; l < end; l++) xrpow[j2++] *= ifqstep34; } scalefac->s[sfb][i] >>= 1; j += end-start; } } cod_info->scalefac_scale = 1;}void inc_subblock_gain(lame_global_flags *gfp, III_scalefac_t *scalefac, gr_info *cod_info, FLOAT8 xrpow[576]){ int start, end, l,i; int sfb; lame_internal_flags *gfc=gfp->internal_flags; fun_reorder(gfc->scalefac_band.s,xrpow); for ( i = 0; i < 3; i++ ) { if (cod_info->subblock_gain[i] >= 7) continue; for ( sfb = cod_info->sfb_smax; sfb < 12; sfb++ ) { if (scalefac->s[sfb][i] >= 8) { break; } } if (sfb == 12) continue; for ( sfb = cod_info->sfb_smax; sfb < 12; sfb++ ) { if (scalefac->s[sfb][i] >= 2) { scalefac->s[sfb][i] -= 2; } else { FLOAT8 amp = pow(2.0, 0.75*(2 - scalefac->s[sfb][i])); scalefac->s[sfb][i] = 0; start = gfc->scalefac_band.s[sfb]; end = gfc->scalefac_band.s[sfb+1]; for (l = start; l < end; l++) xrpow[l * 3 + i] *= amp; } } { FLOAT8 amp = pow(2.0, 0.75*2); start = gfc->scalefac_band.s[sfb]; for (l = start; l < 192; l++) xrpow[l * 3 + i] *= amp; } cod_info->subblock_gain[i]++; } freorder(gfc->scalefac_band.s,xrpow);}INLINE int quant_compare(int experimentalX, calc_noise_result *best, calc_noise_result *calc){ /* noise is given in decibals (db) relative to masking thesholds. over_noise: sum of quantization noise > masking tot_noise: sum of all quantization noise max_noise: max quantization noise */ int better=0; switch (experimentalX) { default: case 0: better = calc->over_count < best->over_count ||( calc->over_count == best->over_count && calc->over_avg_noise <= best->over_avg_noise ); break; case 1: better = calc->max_noise < best->max_noise; break; case 2: better = calc->tot_avg_noise < best->tot_avg_noise; break; case 3: better = calc->tot_avg_noise < best->tot_avg_noise && calc->max_noise < best->max_noise + 2; break; case 4: better = ( ( (0>=calc->max_noise) && (best->max_noise>2)) || ( (0>=calc->max_noise) && (best->max_noise<0) && ((best->max_noise+2)>calc->max_noise) && (calc->tot_avg_noise<best->tot_avg_noise) ) || ( (0>=calc->max_noise) && (best->max_noise>0) && ((best->max_noise+2)>calc->max_noise) && (calc->tot_avg_noise<(best->tot_avg_noise+best->over_avg_noise)) ) || ( (0<calc->max_noise) && (best->max_noise>-0.5) && ((best->max_noise+1)>calc->max_noise) && ((calc->tot_avg_noise+calc->over_avg_noise)<(best->tot_avg_noise+best->over_avg_noise)) ) || ( (0<calc->max_noise) && (best->max_noise>-1) && ((best->max_noise+1.5)>calc->max_noise) && ((calc->tot_avg_noise+calc->over_avg_noise+calc->over_avg_noise)<(best->tot_avg_noise+best->over_avg_noise+best->over_avg_noise)) ) ); break; case 5: better = calc->over_avg_noise < best->over_avg_noise ||( calc->over_avg_noise == best->over_avg_noise && calc->tot_avg_noise < best->tot_avg_noise ); break; case 6: better = calc->over_avg_noise < best->over_avg_noise ||( calc->over_avg_noise == best->over_avg_noise &&( calc->max_noise < best->max_noise ||( calc->max_noise == best->max_noise && calc->tot_avg_noise <= best->tot_avg_noise ))); break; case 7: better = calc->over_count < best->over_count || calc->over_noise < best->over_noise; break; } return better;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -