Ejemplo n.º 1
0
  private void recalc_divide_sub(
      final LameInternalFlags gfc,
      final GrInfo cod_info2,
      GrInfo gi,
      final int[] ix,
      final int r01_bits[],
      final int r01_div[],
      final int r0_tbl[],
      final int r1_tbl[]) {
    int bigv = cod_info2.big_values;

    for (int r2 = 2; r2 < Encoder.SBMAX_l + 1; r2++) {
      int a2 = gfc.scalefac_band.l[r2];
      if (a2 >= bigv) break;

      int bits = r01_bits[r2 - 2] + cod_info2.count1bits;
      if (gi.part2_3_length <= bits) break;

      Bits bi = new Bits(bits);
      int r2t = choose_table(ix, a2, bigv, bi);
      bits = bi.bits;
      if (gi.part2_3_length <= bits) continue;

      gi.assign(cod_info2);
      gi.part2_3_length = bits;
      gi.region0_count = r01_div[r2 - 2];
      gi.region1_count = r2 - 2 - r01_div[r2 - 2];
      gi.table_select[0] = r0_tbl[r2 - 2];
      gi.table_select[1] = r1_tbl[r2 - 2];
      gi.table_select[2] = r2t;
    }
  }
Ejemplo n.º 2
0
  /** 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;
  }
Ejemplo n.º 3
0
  private void scfsi_calc(int ch, final IIISideInfo l3_side) {
    int sfb;
    final GrInfo gi = l3_side.tt[1][ch];
    final GrInfo g0 = l3_side.tt[0][ch];

    for (int i = 0; i < Tables.scfsi_band.length - 1; i++) {
      for (sfb = Tables.scfsi_band[i]; sfb < Tables.scfsi_band[i + 1]; sfb++) {
        if (g0.scalefac[sfb] != gi.scalefac[sfb] && gi.scalefac[sfb] >= 0) break;
      }
      if (sfb == Tables.scfsi_band[i + 1]) {
        for (sfb = Tables.scfsi_band[i]; sfb < Tables.scfsi_band[i + 1]; sfb++) {
          gi.scalefac[sfb] = -1;
        }
        l3_side.scfsi[ch][i] = 1;
      }
    }

    int s1 = 0;
    int c1 = 0;
    for (sfb = 0; sfb < 11; sfb++) {
      if (gi.scalefac[sfb] == -1) continue;
      c1++;
      if (s1 < gi.scalefac[sfb]) s1 = gi.scalefac[sfb];
    }

    int s2 = 0;
    int c2 = 0;
    for (; sfb < Encoder.SBPSY_l; sfb++) {
      if (gi.scalefac[sfb] == -1) continue;
      c2++;
      if (s2 < gi.scalefac[sfb]) s2 = gi.scalefac[sfb];
    }

    for (int i = 0; i < 16; i++) {
      if (s1 < slen1_n[i] && s2 < slen2_n[i]) {
        final int c = slen1_tab[i] * c1 + slen2_tab[i] * c2;
        if (gi.part2_length > c) {
          gi.part2_length = c;
          gi.scalefac_compress = i;
        }
      }
    }
  }
Ejemplo n.º 4
0
  protected void set_subblock_gain(final GrInfo cod_info, final int mingain_s[], final int sf[]) {
    final int maxrange1 = 15, maxrange2 = 7;
    final int ifqstepShift = (cod_info.scalefac_scale == 0) ? 1 : 2;
    int[] sbg = cod_info.subblock_gain;
    final int psymax = (int) cod_info.psymax;
    int psydiv = 18;
    int sbg0, sbg1, sbg2;
    int sfb;
    int min_sbg = 7;

    if (psydiv > psymax) {
      psydiv = psymax;
    }
    for (int i = 0; i < 3; ++i) {
      int maxsf1 = 0, maxsf2 = 0, minsf = 1000;
      /* see if we should use subblock gain */
      for (sfb = i; sfb < psydiv; sfb += 3) {
        /* part 1 */
        final int v = -sf[sfb];
        if (maxsf1 < v) {
          maxsf1 = v;
        }
        if (minsf > v) {
          minsf = v;
        }
      }
      for (; sfb < L3Side.SFBMAX; sfb += 3) {
        /* part 2 */
        final int v = -sf[sfb];
        if (maxsf2 < v) {
          maxsf2 = v;
        }
        if (minsf > v) {
          minsf = v;
        }
      }

      /*
       * boost subblock gain as little as possible so we can reach maxsf1
       * with scalefactors 8*sbg >= maxsf1
       */
      {
        final int m1 = maxsf1 - (maxrange1 << ifqstepShift);
        final int m2 = maxsf2 - (maxrange2 << ifqstepShift);

        maxsf1 = Math.max(m1, m2);
      }
      if (minsf > 0) {
        sbg[i] = minsf >> 3;
      } else {
        sbg[i] = 0;
      }
      if (maxsf1 > 0) {
        final int m1 = sbg[i];
        final int m2 = (maxsf1 + 7) >> 3;
        sbg[i] = Math.max(m1, m2);
      }
      if (sbg[i] > 0 && mingain_s[i] > (cod_info.global_gain - sbg[i] * 8)) {
        sbg[i] = (cod_info.global_gain - mingain_s[i]) >> 3;
      }
      if (sbg[i] > 7) {
        sbg[i] = 7;
      }
      if (min_sbg > sbg[i]) {
        min_sbg = sbg[i];
      }
    }
    sbg0 = sbg[0] * 8;
    sbg1 = sbg[1] * 8;
    sbg2 = sbg[2] * 8;
    for (sfb = 0; sfb < L3Side.SFBMAX; sfb += 3) {
      sf[sfb + 0] += sbg0;
      sf[sfb + 1] += sbg1;
      sf[sfb + 2] += sbg2;
    }
    if (min_sbg > 0) {
      for (int i = 0; i < 3; ++i) {
        sbg[i] -= min_sbg;
      }
      cod_info.global_gain -= min_sbg * 8;
    }
  }
Ejemplo n.º 5
0
  /**
   * Calculate the allowed distortion for each scalefactor band, as determined by the psychoacoustic
   * model. xmin(sb) = ratio(sb) * en(sb) / bw(sb)
   *
   * <p>returns number of sfb's with energy > ATH
   */
  public final int calc_xmin(
      final LameGlobalFlags gfp,
      final III_psy_ratio ratio,
      final GrInfo cod_info,
      final float[] pxmin) {
    int pxminPos = 0;
    final LameInternalFlags gfc = gfp.internal_flags;
    int gsfb, j = 0, ath_over = 0;
    final ATH ATH = gfc.ATH;
    final float[] xr = cod_info.xr;
    final int enable_athaa_fix = (gfp.getVBR() == VbrMode.vbr_mtrh) ? 1 : 0;
    float masking_lower = gfc.masking_lower;

    if (gfp.getVBR() == VbrMode.vbr_mtrh || gfp.getVBR() == VbrMode.vbr_mt) {
      /* was already done in PSY-Model */
      masking_lower = 1.0f;
    }

    for (gsfb = 0; gsfb < cod_info.psy_lmax; gsfb++) {
      float en0, xmin;
      float rh1, rh2;
      int width, l;

      if (gfp.getVBR() == VbrMode.vbr_rh || gfp.getVBR() == VbrMode.vbr_mtrh)
        xmin = athAdjust(ATH.adjust, ATH.l[gsfb], ATH.floor);
      else xmin = ATH.adjust * ATH.l[gsfb];

      width = cod_info.width[gsfb];
      rh1 = xmin / width;
      rh2 = DBL_EPSILON;
      l = width >> 1;
      en0 = 0.0f;
      do {
        float xa, xb;
        xa = xr[j] * xr[j];
        en0 += xa;
        rh2 += (xa < rh1) ? xa : rh1;
        j++;
        xb = xr[j] * xr[j];
        en0 += xb;
        rh2 += (xb < rh1) ? xb : rh1;
        j++;
      } while (--l > 0);
      if (en0 > xmin) ath_over++;

      if (gsfb == Encoder.SBPSY_l) {
        float x = xmin * gfc.nsPsy.longfact[gsfb];
        if (rh2 < x) {
          rh2 = x;
        }
      }
      if (enable_athaa_fix != 0) {
        xmin = rh2;
      }
      if (!gfp.ATHonly) {
        final float e = ratio.en.l[gsfb];
        if (e > 0.0f) {
          float x;
          x = en0 * ratio.thm.l[gsfb] * masking_lower / e;
          if (enable_athaa_fix != 0) x *= gfc.nsPsy.longfact[gsfb];
          if (xmin < x) xmin = x;
        }
      }
      if (enable_athaa_fix != 0) pxmin[pxminPos++] = xmin;
      else pxmin[pxminPos++] = xmin * gfc.nsPsy.longfact[gsfb];
    } /* end of long block loop */

    /* use this function to determine the highest non-zero coeff */
    int max_nonzero = 575;
    if (cod_info.block_type != Encoder.SHORT_TYPE) {
      // NORM, START or STOP type, but not SHORT
      int k = 576;
      while (k-- != 0 && BitStream.EQ(xr[k], 0)) {
        max_nonzero = k;
      }
    }
    cod_info.max_nonzero_coeff = max_nonzero;

    for (int sfb = cod_info.sfb_smin; gsfb < cod_info.psymax; sfb++, gsfb += 3) {
      int width, b;
      float tmpATH;
      if (gfp.getVBR() == VbrMode.vbr_rh || gfp.getVBR() == VbrMode.vbr_mtrh)
        tmpATH = athAdjust(ATH.adjust, ATH.s[sfb], ATH.floor);
      else tmpATH = ATH.adjust * ATH.s[sfb];

      width = cod_info.width[gsfb];
      for (b = 0; b < 3; b++) {
        float en0 = 0.0f, xmin;
        float rh1, rh2;
        int l = width >> 1;

        rh1 = tmpATH / width;
        rh2 = DBL_EPSILON;
        do {
          float xa, xb;
          xa = xr[j] * xr[j];
          en0 += xa;
          rh2 += (xa < rh1) ? xa : rh1;
          j++;
          xb = xr[j] * xr[j];
          en0 += xb;
          rh2 += (xb < rh1) ? xb : rh1;
          j++;
        } while (--l > 0);
        if (en0 > tmpATH) ath_over++;
        if (sfb == Encoder.SBPSY_s) {
          float x = tmpATH * gfc.nsPsy.shortfact[sfb];
          if (rh2 < x) {
            rh2 = x;
          }
        }
        if (enable_athaa_fix != 0) xmin = rh2;
        else xmin = tmpATH;

        if (!gfp.ATHonly && !gfp.ATHshort) {
          final float e = ratio.en.s[sfb][b];
          if (e > 0.0f) {
            float x;
            x = en0 * ratio.thm.s[sfb][b] * masking_lower / e;
            if (enable_athaa_fix != 0) x *= gfc.nsPsy.shortfact[sfb];
            if (xmin < x) xmin = x;
          }
        }
        if (enable_athaa_fix != 0) pxmin[pxminPos++] = xmin;
        else pxmin[pxminPos++] = xmin * gfc.nsPsy.shortfact[sfb];
      } /* b */
      if (gfp.useTemporal) {
        if (pxmin[pxminPos - 3] > pxmin[pxminPos - 3 + 1])
          pxmin[pxminPos - 3 + 1] += (pxmin[pxminPos - 3] - pxmin[pxminPos - 3 + 1]) * gfc.decay;
        if (pxmin[pxminPos - 3 + 1] > pxmin[pxminPos - 3 + 2])
          pxmin[pxminPos - 3 + 2] +=
              (pxmin[pxminPos - 3 + 1] - pxmin[pxminPos - 3 + 2]) * gfc.decay;
      }
    } /* end of short block sfb loop */

    return ath_over;
  }
Ejemplo n.º 6
0
  /**
   * Also counts the number of bits to encode the scalefacs but for MPEG 2 Lower sampling
   * frequencies (24, 22.05 and 16 kHz.)
   *
   * <p>This is reverse-engineered from section 2.4.3.2 of the MPEG2 IS, "Audio Decoding Layer III"
   */
  public boolean scale_bitcount_lsf(final LameInternalFlags gfc, final GrInfo cod_info) {
    int table_number, row_in_table, partition, nr_sfb, window;
    boolean over;
    int i, sfb, max_sfac[] = new int[4];
    final int[] partition_table;
    final int[] scalefac = cod_info.scalefac;

    /*
     * Set partition table. Note that should try to use table one, but do
     * not yet...
     */
    if (cod_info.preflag != 0) table_number = 2;
    else table_number = 0;

    for (i = 0; i < 4; i++) max_sfac[i] = 0;

    if (cod_info.block_type == Encoder.SHORT_TYPE) {
      row_in_table = 1;
      partition_table = qupvt.nr_of_sfb_block[table_number][row_in_table];
      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[sfb * 3 + window] > max_sfac[partition])
              max_sfac[partition] = scalefac[sfb * 3 + window];
      }
    } else {
      row_in_table = 0;
      partition_table = qupvt.nr_of_sfb_block[table_number][row_in_table];
      for (sfb = 0, partition = 0; partition < 4; partition++) {
        nr_sfb = partition_table[partition];
        for (i = 0; i < nr_sfb; i++, sfb++)
          if (scalefac[sfb] > max_sfac[partition]) max_sfac[partition] = scalefac[sfb];
      }
    }

    for (over = false, partition = 0; partition < 4; partition++) {
      if (max_sfac[partition] > max_range_sfac_tab[table_number][partition]) over = true;
    }
    if (!over) {

      int slen1, slen2, slen3, slen4;

      cod_info.sfb_partition_table = qupvt.nr_of_sfb_block[table_number][row_in_table];
      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:
          System.err.printf("intensity stereo not implemented yet\n");
          break;
      }
    }
    if (!over) {
      assert (cod_info.sfb_partition_table != null);
      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;
  }
Ejemplo n.º 7
0
  /**
   * 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);
      }
    }
  }
Ejemplo n.º 8
0
  public void best_huffman_divide(final LameInternalFlags gfc, GrInfo gi) {
    GrInfo cod_info2 = new GrInfo();
    final int[] ix = gi.l3_enc;

    int r01_bits[] = new int[7 + 15 + 1];
    int r01_div[] = new int[7 + 15 + 1];
    int r0_tbl[] = new int[7 + 15 + 1];
    int r1_tbl[] = new int[7 + 15 + 1];

    /* SHORT BLOCK stuff fails for MPEG2 */
    if (gi.block_type == Encoder.SHORT_TYPE && gfc.mode_gr == 1) return;

    cod_info2.assign(gi);
    if (gi.block_type == Encoder.NORM_TYPE) {
      recalc_divide_init(gfc, gi, ix, r01_bits, r01_div, r0_tbl, r1_tbl);
      recalc_divide_sub(gfc, cod_info2, gi, ix, r01_bits, r01_div, r0_tbl, r1_tbl);
    }

    int i = cod_info2.big_values;
    if (i == 0 || (ix[i - 2] | ix[i - 1]) > 1) return;

    i = gi.count1 + 2;
    if (i > 576) return;

    /* Determines the number of bits to encode the quadruples. */
    cod_info2.assign(gi);
    cod_info2.count1 = i;
    int a1 = 0;
    int a2 = 0;

    assert (i <= 576);

    for (; i > cod_info2.big_values; i -= 4) {
      final int p = ((ix[i - 4] * 2 + ix[i - 3]) * 2 + ix[i - 2]) * 2 + ix[i - 1];
      a1 += Tables.t32l[p];
      a2 += Tables.t33l[p];
    }
    cod_info2.big_values = i;

    cod_info2.count1table_select = 0;
    if (a1 > a2) {
      a1 = a2;
      cod_info2.count1table_select = 1;
    }

    cod_info2.count1bits = a1;

    if (cod_info2.block_type == Encoder.NORM_TYPE)
      recalc_divide_sub(gfc, cod_info2, gi, ix, r01_bits, r01_div, r0_tbl, r1_tbl);
    else {
      /* Count the number of bits necessary to code the bigvalues region. */
      cod_info2.part2_3_length = a1;
      a1 = gfc.scalefac_band.l[7 + 1];
      if (a1 > i) {
        a1 = i;
      }
      if (a1 > 0) {
        Bits bi = new Bits(cod_info2.part2_3_length);
        cod_info2.table_select[0] = choose_table(ix, 0, a1, bi);
        cod_info2.part2_3_length = bi.bits;
      }
      if (i > a1) {
        Bits bi = new Bits(cod_info2.part2_3_length);
        cod_info2.table_select[1] = choose_table(ix, a1, i, bi);
        cod_info2.part2_3_length = bi.bits;
      }
      if (gi.part2_3_length > cod_info2.part2_3_length) gi.assign(cod_info2);
    }
  }
Ejemplo n.º 9
0
  /** count_bit */
  public int noquant_count_bits(
      final LameInternalFlags gfc, final GrInfo gi, CalcNoiseData prev_noise) {
    final int[] ix = gi.l3_enc;

    int i = Math.min(576, ((gi.max_nonzero_coeff + 2) >> 1) << 1);

    if (prev_noise != null) prev_noise.sfb_count1 = 0;

    /* Determine count1 region */
    for (; i > 1; i -= 2) if ((ix[i - 1] | ix[i - 2]) != 0) break;
    gi.count1 = i;

    /* Determines the number of bits to encode the quadruples. */
    int a1 = 0;
    int a2 = 0;
    for (; i > 3; i -= 4) {
      int p;
      /* hack to check if all values <= 1 */
      if ((((long) ix[i - 1] | (long) ix[i - 2] | (long) ix[i - 3] | (long) ix[i - 4])
              & 0xffffffffL)
          > 1L) break;

      p = ((ix[i - 4] * 2 + ix[i - 3]) * 2 + ix[i - 2]) * 2 + ix[i - 1];
      a1 += Tables.t32l[p];
      a2 += Tables.t33l[p];
    }

    int bits = a1;
    gi.count1table_select = 0;
    if (a1 > a2) {
      bits = a2;
      gi.count1table_select = 1;
    }

    gi.count1bits = bits;
    gi.big_values = i;
    if (i == 0) return bits;

    if (gi.block_type == Encoder.SHORT_TYPE) {
      a1 = 3 * gfc.scalefac_band.s[3];
      if (a1 > gi.big_values) a1 = gi.big_values;
      a2 = gi.big_values;

    } else if (gi.block_type == Encoder.NORM_TYPE) {
      assert (i <= 576); /* bv_scf has 576 entries (0..575) */
      a1 = gi.region0_count = gfc.bv_scf[i - 2];
      a2 = gi.region1_count = gfc.bv_scf[i - 1];

      assert (a1 + a2 + 2 < Encoder.SBPSY_l);
      a2 = gfc.scalefac_band.l[a1 + a2 + 2];
      a1 = gfc.scalefac_band.l[a1 + 1];
      if (a2 < i) {
        Bits bi = new Bits(bits);
        gi.table_select[2] = choose_table(ix, a2, i, bi);
        bits = bi.bits;
      }
    } else {
      gi.region0_count = 7;
      /* gi.region1_count = SBPSY_l - 7 - 1; */
      gi.region1_count = Encoder.SBMAX_l - 1 - 7 - 1;
      a1 = gfc.scalefac_band.l[7 + 1];
      a2 = i;
      if (a1 > a2) {
        a1 = a2;
      }
    }

    /* have to allow for the case when bigvalues < region0 < region1 */
    /* (and region0, region1 are ignored) */
    a1 = Math.min(a1, i);
    a2 = Math.min(a2, i);

    assert (a1 >= 0);
    assert (a2 >= 0);

    /* Count the number of bits necessary to code the bigvalues region. */
    if (0 < a1) {
      Bits bi = new Bits(bits);
      gi.table_select[0] = choose_table(ix, 0, a1, bi);
      bits = bi.bits;
    }
    if (a1 < a2) {
      Bits bi = new Bits(bits);
      gi.table_select[1] = choose_table(ix, a1, a2, bi);
      bits = bi.bits;
    }
    if (gfc.use_best_huffman == 2) {
      gi.part2_3_length = bits;
      best_huffman_divide(gfc, gi);
      bits = gi.part2_3_length;
    }

    if (prev_noise != null) {
      if (gi.block_type == Encoder.NORM_TYPE) {
        int sfb = 0;
        while (gfc.scalefac_band.l[sfb] < gi.big_values) {
          sfb++;
        }
        prev_noise.sfb_count1 = sfb;
      }
    }

    return bits;
  }