static void read_sbr_envelope(SpectralBandReplication *sbr, GetBitContext *gb,

                              SBRData *ch_data, int ch)

{

    int bits;

    int i, j, k;

    VLC_TYPE (*t_huff)[2], (*f_huff)[2];

    int t_lav, f_lav;

    const int delta = (ch == 1 && sbr->bs_coupling == 1) + 1;

    const int odd = sbr->n[1] & 1;



    if (sbr->bs_coupling && ch) {

        if (ch_data->bs_amp_res) {

            bits   = 5;

            t_huff = vlc_sbr[T_HUFFMAN_ENV_BAL_3_0DB].table;

            t_lav  = vlc_sbr_lav[T_HUFFMAN_ENV_BAL_3_0DB];

            f_huff = vlc_sbr[F_HUFFMAN_ENV_BAL_3_0DB].table;

            f_lav  = vlc_sbr_lav[F_HUFFMAN_ENV_BAL_3_0DB];

        } else {

            bits   = 6;

            t_huff = vlc_sbr[T_HUFFMAN_ENV_BAL_1_5DB].table;

            t_lav  = vlc_sbr_lav[T_HUFFMAN_ENV_BAL_1_5DB];

            f_huff = vlc_sbr[F_HUFFMAN_ENV_BAL_1_5DB].table;

            f_lav  = vlc_sbr_lav[F_HUFFMAN_ENV_BAL_1_5DB];

        }

    } else {

        if (ch_data->bs_amp_res) {

            bits   = 6;

            t_huff = vlc_sbr[T_HUFFMAN_ENV_3_0DB].table;

            t_lav  = vlc_sbr_lav[T_HUFFMAN_ENV_3_0DB];

            f_huff = vlc_sbr[F_HUFFMAN_ENV_3_0DB].table;

            f_lav  = vlc_sbr_lav[F_HUFFMAN_ENV_3_0DB];

        } else {

            bits   = 7;

            t_huff = vlc_sbr[T_HUFFMAN_ENV_1_5DB].table;

            t_lav  = vlc_sbr_lav[T_HUFFMAN_ENV_1_5DB];

            f_huff = vlc_sbr[F_HUFFMAN_ENV_1_5DB].table;

            f_lav  = vlc_sbr_lav[F_HUFFMAN_ENV_1_5DB];

        }

    }



    for (i = 0; i < ch_data->bs_num_env; i++) {

        if (ch_data->bs_df_env[i]) {

            // bs_freq_res[0] == bs_freq_res[bs_num_env] from prev frame

            if (ch_data->bs_freq_res[i + 1] == ch_data->bs_freq_res[i]) {

                for (j = 0; j < sbr->n[ch_data->bs_freq_res[i + 1]]; j++)

                    ch_data->env_facs_q[i + 1][j] = ch_data->env_facs_q[i][j] + delta * (get_vlc2(gb, t_huff, 9, 3) - t_lav);

            } else if (ch_data->bs_freq_res[i + 1]) {

                for (j = 0; j < sbr->n[ch_data->bs_freq_res[i + 1]]; j++) {

                    k = (j + odd) >> 1; // find k such that f_tablelow[k] <= f_tablehigh[j] < f_tablelow[k + 1]

                    ch_data->env_facs_q[i + 1][j] = ch_data->env_facs_q[i][k] + delta * (get_vlc2(gb, t_huff, 9, 3) - t_lav);

                }

            } else {

                for (j = 0; j < sbr->n[ch_data->bs_freq_res[i + 1]]; j++) {

                    k = j ? 2*j - odd : 0; // find k such that f_tablehigh[k] == f_tablelow[j]

                    ch_data->env_facs_q[i + 1][j] = ch_data->env_facs_q[i][k] + delta * (get_vlc2(gb, t_huff, 9, 3) - t_lav);

                }

            }

        } else {

            ch_data->env_facs_q[i + 1][0] = delta * get_bits(gb, bits); // bs_env_start_value_balance

            for (j = 1; j < sbr->n[ch_data->bs_freq_res[i + 1]]; j++)

                ch_data->env_facs_q[i + 1][j] = ch_data->env_facs_q[i + 1][j - 1] + delta * (get_vlc2(gb, f_huff, 9, 3) - f_lav);

        }

    }



    //assign 0th elements of env_facs_q from last elements

    memcpy(ch_data->env_facs_q[0], ch_data->env_facs_q[ch_data->bs_num_env],

           sizeof(ch_data->env_facs_q[0]));

}

static void read_sbr_envelope(SpectralBandReplication *sbr, GetBitContext *gb,

                              SBRData *ch_data, int ch)

{

    int bits;

    int i, j, k;

    VLC_TYPE (*t_huff)[2], (*f_huff)[2];

    int t_lav, f_lav;

    const int delta = (ch == 1 && sbr->bs_coupling == 1) + 1;

    const int odd = sbr->n[1] & 1;



    if (sbr->bs_coupling && ch) {

        if (ch_data->bs_amp_res) {

            bits   = 5;

            t_huff = vlc_sbr[T_HUFFMAN_ENV_BAL_3_0DB].table;

            t_lav  = vlc_sbr_lav[T_HUFFMAN_ENV_BAL_3_0DB];

            f_huff = vlc_sbr[F_HUFFMAN_ENV_BAL_3_0DB].table;

            f_lav  = vlc_sbr_lav[F_HUFFMAN_ENV_BAL_3_0DB];

        } else {

            bits   = 6;

            t_huff = vlc_sbr[T_HUFFMAN_ENV_BAL_1_5DB].table;

            t_lav  = vlc_sbr_lav[T_HUFFMAN_ENV_BAL_1_5DB];

            f_huff = vlc_sbr[F_HUFFMAN_ENV_BAL_1_5DB].table;

            f_lav  = vlc_sbr_lav[F_HUFFMAN_ENV_BAL_1_5DB];

        }

    } else {

        if (ch_data->bs_amp_res) {

            bits   = 6;

            t_huff = vlc_sbr[T_HUFFMAN_ENV_3_0DB].table;

            t_lav  = vlc_sbr_lav[T_HUFFMAN_ENV_3_0DB];

            f_huff = vlc_sbr[F_HUFFMAN_ENV_3_0DB].table;

            f_lav  = vlc_sbr_lav[F_HUFFMAN_ENV_3_0DB];

        } else {

            bits   = 7;

            t_huff = vlc_sbr[T_HUFFMAN_ENV_1_5DB].table;

            t_lav  = vlc_sbr_lav[T_HUFFMAN_ENV_1_5DB];

            f_huff = vlc_sbr[F_HUFFMAN_ENV_1_5DB].table;

            f_lav  = vlc_sbr_lav[F_HUFFMAN_ENV_1_5DB];

        }

    }



    for (i = 0; i < ch_data->bs_num_env; i++) {

        if (ch_data->bs_df_env[i]) {

            

            if (ch_data->bs_freq_res[i + 1] == ch_data->bs_freq_res[i]) {

                for (j = 0; j < sbr->n[ch_data->bs_freq_res[i + 1]]; j++)

                    ch_data->env_facs_q[i + 1][j] = ch_data->env_facs_q[i][j] + delta * (get_vlc2(gb, t_huff, 9, 3) - t_lav);

            } else if (ch_data->bs_freq_res[i + 1]) {

                for (j = 0; j < sbr->n[ch_data->bs_freq_res[i + 1]]; j++) {

                    k = (j + odd) >> 1; 

                    ch_data->env_facs_q[i + 1][j] = ch_data->env_facs_q[i][k] + delta * (get_vlc2(gb, t_huff, 9, 3) - t_lav);

                }

            } else {

                for (j = 0; j < sbr->n[ch_data->bs_freq_res[i + 1]]; j++) {

                    k = j ? 2*j - odd : 0; 

                    ch_data->env_facs_q[i + 1][j] = ch_data->env_facs_q[i][k] + delta * (get_vlc2(gb, t_huff, 9, 3) - t_lav);

                }

            }

        } else {

            ch_data->env_facs_q[i + 1][0] = delta * get_bits(gb, bits); 

            for (j = 1; j < sbr->n[ch_data->bs_freq_res[i + 1]]; j++)

                ch_data->env_facs_q[i + 1][j] = ch_data->env_facs_q[i + 1][j - 1] + delta * (get_vlc2(gb, f_huff, 9, 3) - f_lav);

        }

    }



    

    memcpy(ch_data->env_facs_q[0], ch_data->env_facs_q[ch_data->bs_num_env],

           sizeof(ch_data->env_facs_q[0]));

}

static void FUNC_0(SpectralBandReplication *VAR_0, GetBitContext *VAR_1,

                              SBRData *VAR_2, int VAR_3)

{

    int VAR_4;

    int VAR_5, VAR_6, VAR_7;

    VLC_TYPE (*t_huff)[2], (*f_huff)[2];

    int VAR_8, VAR_9;

    const int VAR_10 = (VAR_3 == 1 && VAR_0->bs_coupling == 1) + 1;

    const int VAR_11 = VAR_0->n[1] & 1;



    if (VAR_0->bs_coupling && VAR_3) {

        if (VAR_2->bs_amp_res) {

            VAR_4   = 5;

            t_huff = vlc_sbr[T_HUFFMAN_ENV_BAL_3_0DB].table;

            VAR_8  = vlc_sbr_lav[T_HUFFMAN_ENV_BAL_3_0DB];

            f_huff = vlc_sbr[F_HUFFMAN_ENV_BAL_3_0DB].table;

            VAR_9  = vlc_sbr_lav[F_HUFFMAN_ENV_BAL_3_0DB];

        } else {

            VAR_4   = 6;

            t_huff = vlc_sbr[T_HUFFMAN_ENV_BAL_1_5DB].table;

            VAR_8  = vlc_sbr_lav[T_HUFFMAN_ENV_BAL_1_5DB];

            f_huff = vlc_sbr[F_HUFFMAN_ENV_BAL_1_5DB].table;

            VAR_9  = vlc_sbr_lav[F_HUFFMAN_ENV_BAL_1_5DB];

        }

    } else {

        if (VAR_2->bs_amp_res) {

            VAR_4   = 6;

            t_huff = vlc_sbr[T_HUFFMAN_ENV_3_0DB].table;

            VAR_8  = vlc_sbr_lav[T_HUFFMAN_ENV_3_0DB];

            f_huff = vlc_sbr[F_HUFFMAN_ENV_3_0DB].table;

            VAR_9  = vlc_sbr_lav[F_HUFFMAN_ENV_3_0DB];

        } else {

            VAR_4   = 7;

            t_huff = vlc_sbr[T_HUFFMAN_ENV_1_5DB].table;

            VAR_8  = vlc_sbr_lav[T_HUFFMAN_ENV_1_5DB];

            f_huff = vlc_sbr[F_HUFFMAN_ENV_1_5DB].table;

            VAR_9  = vlc_sbr_lav[F_HUFFMAN_ENV_1_5DB];

        }

    }



    for (VAR_5 = 0; VAR_5 < VAR_2->bs_num_env; VAR_5++) {

        if (VAR_2->bs_df_env[VAR_5]) {

            

            if (VAR_2->bs_freq_res[VAR_5 + 1] == VAR_2->bs_freq_res[VAR_5]) {

                for (VAR_6 = 0; VAR_6 < VAR_0->n[VAR_2->bs_freq_res[VAR_5 + 1]]; VAR_6++)

                    VAR_2->env_facs_q[VAR_5 + 1][VAR_6] = VAR_2->env_facs_q[VAR_5][VAR_6] + VAR_10 * (get_vlc2(VAR_1, t_huff, 9, 3) - VAR_8);

            } else if (VAR_2->bs_freq_res[VAR_5 + 1]) {

                for (VAR_6 = 0; VAR_6 < VAR_0->n[VAR_2->bs_freq_res[VAR_5 + 1]]; VAR_6++) {

                    VAR_7 = (VAR_6 + VAR_11) >> 1; 

                    VAR_2->env_facs_q[VAR_5 + 1][VAR_6] = VAR_2->env_facs_q[VAR_5][VAR_7] + VAR_10 * (get_vlc2(VAR_1, t_huff, 9, 3) - VAR_8);

                }

            } else {

                for (VAR_6 = 0; VAR_6 < VAR_0->n[VAR_2->bs_freq_res[VAR_5 + 1]]; VAR_6++) {

                    VAR_7 = VAR_6 ? 2*VAR_6 - VAR_11 : 0; 

                    VAR_2->env_facs_q[VAR_5 + 1][VAR_6] = VAR_2->env_facs_q[VAR_5][VAR_7] + VAR_10 * (get_vlc2(VAR_1, t_huff, 9, 3) - VAR_8);

                }

            }

        } else {

            VAR_2->env_facs_q[VAR_5 + 1][0] = VAR_10 * get_bits(VAR_1, VAR_4); 

            for (VAR_6 = 1; VAR_6 < VAR_0->n[VAR_2->bs_freq_res[VAR_5 + 1]]; VAR_6++)

                VAR_2->env_facs_q[VAR_5 + 1][VAR_6] = VAR_2->env_facs_q[VAR_5 + 1][VAR_6 - 1] + VAR_10 * (get_vlc2(VAR_1, f_huff, 9, 3) - VAR_9);

        }

    }



    

    memcpy(VAR_2->env_facs_q[0], VAR_2->env_facs_q[VAR_2->bs_num_env],

           sizeof(VAR_2->env_facs_q[0]));

}

static void FUNC_0(SpectralBandReplication *VAR_0, GetBitContext *VAR_1,
SBRData *VAR_2, int VAR_3)
{ int VAR_4; int VAR_5, VAR_6, VAR_7; VLC_TYPE (*t_huff)[2], (*f_huff)[2]; int VAR_8, VAR_9; const int VAR_10 = (VAR_3 == 1 && VAR_0->bs_coupling == 1) + 1; const int VAR_11 = VAR_0->n[1] & 1; if (VAR_0->bs_coupling && VAR_3) { if (VAR_2->bs_amp_res) { VAR_4   = 5; t_huff = vlc_sbr[T_HUFFMAN_ENV_BAL_3_0DB].table; VAR_8  = vlc_sbr_lav[T_HUFFMAN_ENV_BAL_3_0DB]; f_huff = vlc_sbr[F_HUFFMAN_ENV_BAL_3_0DB].table; VAR_9  = vlc_sbr_lav[F_HUFFMAN_ENV_BAL_3_0DB]; } else { VAR_4   = 6; t_huff = vlc_sbr[T_HUFFMAN_ENV_BAL_1_5DB].table; VAR_8  = vlc_sbr_lav[T_HUFFMAN_ENV_BAL_1_5DB]; f_huff = vlc_sbr[F_HUFFMAN_ENV_BAL_1_5DB].table; VAR_9  = vlc_sbr_lav[F_HUFFMAN_ENV_BAL_1_5DB]; } } else { if (VAR_2->bs_amp_res) { VAR_4   = 6; t_huff = vlc_sbr[T_HUFFMAN_ENV_3_0DB].table; VAR_8  = vlc_sbr_lav[T_HUFFMAN_ENV_3_0DB]; f_huff = vlc_sbr[F_HUFFMAN_ENV_3_0DB].table; VAR_9  = vlc_sbr_lav[F_HUFFMAN_ENV_3_0DB]; } else { VAR_4   = 7; t_huff = vlc_sbr[T_HUFFMAN_ENV_1_5DB].table; VAR_8  = vlc_sbr_lav[T_HUFFMAN_ENV_1_5DB]; f_huff = vlc_sbr[F_HUFFMAN_ENV_1_5DB].table; VAR_9  = vlc_sbr_lav[F_HUFFMAN_ENV_1_5DB]; } } for (VAR_5 = 0; VAR_5 < VAR_2->bs_num_env; VAR_5++) { if (VAR_2->bs_df_env[VAR_5]) { if (VAR_2->bs_freq_res[VAR_5 + 1] == VAR_2->bs_freq_res[VAR_5]) { for (VAR_6 = 0; VAR_6 < VAR_0->n[VAR_2->bs_freq_res[VAR_5 + 1]]; VAR_6++) VAR_2->env_facs_q[VAR_5 + 1][VAR_6] = VAR_2->env_facs_q[VAR_5][VAR_6] + VAR_10 * (get_vlc2(VAR_1, t_huff, 9, 3) - VAR_8); } else if (VAR_2->bs_freq_res[VAR_5 + 1]) { for (VAR_6 = 0; VAR_6 < VAR_0->n[VAR_2->bs_freq_res[VAR_5 + 1]]; VAR_6++) { VAR_7 = (VAR_6 + VAR_11) >> 1; VAR_2->env_facs_q[VAR_5 + 1][VAR_6] = VAR_2->env_facs_q[VAR_5][VAR_7] + VAR_10 * (get_vlc2(VAR_1, t_huff, 9, 3) - VAR_8); } } else { for (VAR_6 = 0; VAR_6 < VAR_0->n[VAR_2->bs_freq_res[VAR_5 + 1]]; VAR_6++) { VAR_7 = VAR_6 ? 2*VAR_6 - VAR_11 : 0; VAR_2->env_facs_q[VAR_5 + 1][VAR_6] = VAR_2->env_facs_q[VAR_5][VAR_7] + VAR_10 * (get_vlc2(VAR_1, t_huff, 9, 3) - VAR_8); } } } else { VAR_2->env_facs_q[VAR_5 + 1][0] = VAR_10 * get_bits(VAR_1, VAR_4); for (VAR_6 = 1; VAR_6 < VAR_0->n[VAR_2->bs_freq_res[VAR_5 + 1]]; VAR_6++) VAR_2->env_facs_q[VAR_5 + 1][VAR_6] = VAR_2->env_facs_q[VAR_5 + 1][VAR_6 - 1] + VAR_10 * (get_vlc2(VAR_1, f_huff, 9, 3) - VAR_9); } } memcpy(VAR_2->env_facs_q[0], VAR_2->env_facs_q[VAR_2->bs_num_env],
sizeof(VAR_2->env_facs_q[0])); }