/** Also calculates the number of bits necessary to code the scalefactors. */ public boolean scale_bitcount(final GrInfo cod_info) { int k, sfb, max_slen1 = 0, max_slen2 = 0; /* maximum values */ int[] tab; final int[] scalefac = cod_info.scalefac; assert (all_scalefactors_not_negative(scalefac, cod_info.sfbmax)); if (cod_info.block_type == Encoder.SHORT_TYPE) { tab = scale_short; if (cod_info.mixed_block_flag != 0) tab = scale_mixed; } else { /* block_type == 1,2,or 3 */ tab = scale_long; if (0 == cod_info.preflag) { for (sfb = 11; sfb < Encoder.SBPSY_l; sfb++) if (scalefac[sfb] < qupvt.pretab[sfb]) break; if (sfb == Encoder.SBPSY_l) { cod_info.preflag = 1; for (sfb = 11; sfb < Encoder.SBPSY_l; sfb++) scalefac[sfb] -= qupvt.pretab[sfb]; } } } for (sfb = 0; sfb < cod_info.sfbdivide; sfb++) if (max_slen1 < scalefac[sfb]) max_slen1 = scalefac[sfb]; for (; sfb < cod_info.sfbmax; sfb++) if (max_slen2 < scalefac[sfb]) max_slen2 = scalefac[sfb]; /* * from Takehiro TOMINAGA <*****@*****.**> 10/99 loop over *all* * posible values of scalefac_compress to find the one which uses the * smallest number of bits. ISO would stop at first valid index */ cod_info.part2_length = QuantizePVT.LARGE_BITS; for (k = 0; k < 16; k++) { if (max_slen1 < slen1_n[k] && max_slen2 < slen2_n[k] && cod_info.part2_length > tab[k]) { cod_info.part2_length = tab[k]; cod_info.scalefac_compress = k; } } return cod_info.part2_length == QuantizePVT.LARGE_BITS; }
/** * Find the optimal way to store the scalefactors. Only call this routine after final scalefactors * have been chosen and the channel/granule will not be re-encoded. */ public void best_scalefac_store( final LameInternalFlags gfc, final int gr, final int ch, final IIISideInfo l3_side) { /* use scalefac_scale if we can */ final GrInfo gi = l3_side.tt[gr][ch]; int sfb, i, j, l; int recalc = 0; /* * remove scalefacs from bands with ix=0. This idea comes from the AAC * ISO docs. added mt 3/00 */ /* check if l3_enc=0 */ j = 0; for (sfb = 0; sfb < gi.sfbmax; sfb++) { final int width = gi.width[sfb]; assert (width >= 0); j += width; for (l = -width; l < 0; l++) { if (gi.l3_enc[l + j] != 0) break; } if (l == 0) gi.scalefac[sfb] = recalc = -2; /* anything goes. */ /* * only best_scalefac_store and calc_scfsi know--and only they * should know--about the magic number -2. */ } if (0 == gi.scalefac_scale && 0 == gi.preflag) { int s = 0; for (sfb = 0; sfb < gi.sfbmax; sfb++) if (gi.scalefac[sfb] > 0) s |= gi.scalefac[sfb]; if (0 == (s & 1) && s != 0) { for (sfb = 0; sfb < gi.sfbmax; sfb++) if (gi.scalefac[sfb] > 0) gi.scalefac[sfb] >>= 1; gi.scalefac_scale = recalc = 1; } } if (0 == gi.preflag && gi.block_type != Encoder.SHORT_TYPE && gfc.mode_gr == 2) { for (sfb = 11; sfb < Encoder.SBPSY_l; sfb++) if (gi.scalefac[sfb] < qupvt.pretab[sfb] && gi.scalefac[sfb] != -2) break; if (sfb == Encoder.SBPSY_l) { for (sfb = 11; sfb < Encoder.SBPSY_l; sfb++) if (gi.scalefac[sfb] > 0) gi.scalefac[sfb] -= qupvt.pretab[sfb]; gi.preflag = recalc = 1; } } for (i = 0; i < 4; i++) l3_side.scfsi[ch][i] = 0; if (gfc.mode_gr == 2 && gr == 1 && l3_side.tt[0][ch].block_type != Encoder.SHORT_TYPE && l3_side.tt[1][ch].block_type != Encoder.SHORT_TYPE) { scfsi_calc(ch, l3_side); recalc = 0; } for (sfb = 0; sfb < gi.sfbmax; sfb++) { if (gi.scalefac[sfb] == -2) { gi.scalefac[sfb] = 0; /* if anything goes, then 0 is a good choice */ } } if (recalc != 0) { if (gfc.mode_gr == 2) { scale_bitcount(gi); } else { scale_bitcount_lsf(gfc, gi); } } }