/** * 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); } } }