Skip to content
Snippets Groups Projects
aacdec.c 104 KiB
Newer Older
  • Learn to ignore specific revisions
  •                        GetBitContext *gb, uint8_t max_sfb)
    {
        int sfb;
    
        ltp->lag  = get_bits(gb, 11);
    
        ltp->coef = ltp_coef[get_bits(gb, 3)];
    
        for (sfb = 0; sfb < FFMIN(max_sfb, MAX_LTP_LONG_SFB); sfb++)
            ltp->used[sfb] = get_bits1(gb);
    }
    
    
    /**
     * Decode Individual Channel Stream info; reference: table 4.6.
     */
    
    static int decode_ics_info(AACContext *ac, IndividualChannelStream *ics,
    
                               GetBitContext *gb)
    
        if (get_bits1(gb)) {
    
            av_log(ac->avctx, AV_LOG_ERROR, "Reserved bit set.\n");
    
            return AVERROR_INVALIDDATA;
    
        }
        ics->window_sequence[1] = ics->window_sequence[0];
        ics->window_sequence[0] = get_bits(gb, 2);
    
        ics->use_kb_window[1]   = ics->use_kb_window[0];
        ics->use_kb_window[0]   = get_bits1(gb);
        ics->num_window_groups  = 1;
        ics->group_len[0]       = 1;
    
        if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
            int i;
            ics->max_sfb = get_bits(gb, 4);
            for (i = 0; i < 7; i++) {
                if (get_bits1(gb)) {
    
                    ics->group_len[ics->num_window_groups - 1]++;
    
                } else {
                    ics->num_window_groups++;
    
                    ics->group_len[ics->num_window_groups - 1] = 1;
    
            ics->num_windows       = 8;
    
            ics->swb_offset        =    ff_swb_offset_128[ac->oc[1].m4ac.sampling_index];
            ics->num_swb           =   ff_aac_num_swb_128[ac->oc[1].m4ac.sampling_index];
            ics->tns_max_bands     = ff_tns_max_bands_128[ac->oc[1].m4ac.sampling_index];
    
        } else {
    
            ics->max_sfb               = get_bits(gb, 6);
            ics->num_windows           = 1;
    
            ics->swb_offset            =    ff_swb_offset_1024[ac->oc[1].m4ac.sampling_index];
            ics->num_swb               =   ff_aac_num_swb_1024[ac->oc[1].m4ac.sampling_index];
            ics->tns_max_bands         = ff_tns_max_bands_1024[ac->oc[1].m4ac.sampling_index];
    
            ics->predictor_present     = get_bits1(gb);
    
            ics->predictor_reset_group = 0;
            if (ics->predictor_present) {
    
                if (ac->oc[1].m4ac.object_type == AOT_AAC_MAIN) {
    
                    if (decode_prediction(ac, ics, gb)) {
    
                } else if (ac->oc[1].m4ac.object_type == AOT_AAC_LC) {
    
                    av_log(ac->avctx, AV_LOG_ERROR, "Prediction is not allowed in AAC-LC.\n");
    
                    if ((ics->ltp.present = get_bits(gb, 1)))
    
                        decode_ltp(&ics->ltp, gb, ics->max_sfb);
    
        if (ics->max_sfb > ics->num_swb) {
    
            av_log(ac->avctx, AV_LOG_ERROR,
    
                   "Number of scalefactor bands in group (%d) exceeds limit (%d).\n",
                   ics->max_sfb, ics->num_swb);
    
    fail:
        ics->max_sfb = 0;
        return AVERROR_INVALIDDATA;
    
    }
    
    /**
     * Decode band types (section_data payload); reference: table 4.46.
     *
     * @param   band_type           array of the used band type
     * @param   band_type_run_end   array of the last scalefactor band of a band type run
     *
     * @return  Returns error status. 0 - OK, !0 - error
     */
    
    static int decode_band_types(AACContext *ac, enum BandType band_type[120],
                                 int band_type_run_end[120], GetBitContext *gb,
                                 IndividualChannelStream *ics)
    {
    
        int g, idx = 0;
        const int bits = (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) ? 3 : 5;
        for (g = 0; g < ics->num_window_groups; g++) {
            int k = 0;
            while (k < ics->max_sfb) {
    
                int sect_len_incr;
                int sect_band_type = get_bits(gb, 4);
                if (sect_band_type == 12) {
    
                    av_log(ac->avctx, AV_LOG_ERROR, "invalid band type\n");
    
                do {
                    sect_len_incr = get_bits(gb, bits);
    
                        av_log(ac->avctx, AV_LOG_ERROR, "decode_band_types: "overread_err);
    
                        return -1;
                    }
                    if (sect_end > ics->max_sfb) {
                        av_log(ac->avctx, AV_LOG_ERROR,
                               "Number of bands (%d) exceeds limit (%d).\n",
                               sect_end, ics->max_sfb);
                        return -1;
                    }
                } while (sect_len_incr == (1 << bits) - 1);
    
                    band_type        [idx]   = sect_band_type;
    
    /**
     * Decode scalefactors; reference: table 4.47.
    
     *
     * @param   global_gain         first scalefactor value as scalefactors are differentially coded
     * @param   band_type           array of the used band type
     * @param   band_type_run_end   array of the last scalefactor band of a band type run
     * @param   sf                  array of scalefactors or intensity stereo positions
     *
     * @return  Returns error status. 0 - OK, !0 - error
     */
    
    static int decode_scalefactors(AACContext *ac, float sf[120], GetBitContext *gb,
                                   unsigned int global_gain,
                                   IndividualChannelStream *ics,
                                   enum BandType band_type[120],
                                   int band_type_run_end[120])
    {
    
        int offset[3] = { global_gain, global_gain - 90, 0 };
        int clipped_offset;
    
        int noise_flag = 1;
        for (g = 0; g < ics->num_window_groups; g++) {
            for (i = 0; i < ics->max_sfb;) {
                int run_end = band_type_run_end[idx];
                if (band_type[idx] == ZERO_BT) {
    
                    for (; i < run_end; i++, idx++)
    
                } else if ((band_type[idx] == INTENSITY_BT) || (band_type[idx] == INTENSITY_BT2)) {
                    for (; i < run_end; i++, idx++) {
    
                        offset[2] += get_vlc2(gb, vlc_scalefactors.table, 7, 3) - 60;
    
                        clipped_offset = av_clip(offset[2], -155, 100);
                        if (offset[2] != clipped_offset) {
                            av_log_ask_for_sample(ac->avctx, "Intensity stereo "
                                    "position clipped (%d -> %d).\nIf you heard an "
                                    "audible artifact, there may be a bug in the "
                                    "decoder. ", offset[2], clipped_offset);
    
                        sf[idx] = ff_aac_pow2sf_tab[-clipped_offset + POW_SF2_ZERO];
    
                } else if (band_type[idx] == NOISE_BT) {
                    for (; i < run_end; i++, idx++) {
                        if (noise_flag-- > 0)
    
                            offset[1] += get_bits(gb, 9) - 256;
                        else
                            offset[1] += get_vlc2(gb, vlc_scalefactors.table, 7, 3) - 60;
    
                        clipped_offset = av_clip(offset[1], -100, 155);
    
                        if (offset[1] != clipped_offset) {
    
                            av_log_ask_for_sample(ac->avctx, "Noise gain clipped "
                                    "(%d -> %d).\nIf you heard an audible "
                                    "artifact, there may be a bug in the decoder. ",
                                    offset[1], clipped_offset);
    
                        sf[idx] = -ff_aac_pow2sf_tab[clipped_offset + POW_SF2_ZERO];
    
                } else {
                    for (; i < run_end; i++, idx++) {
    
                        offset[0] += get_vlc2(gb, vlc_scalefactors.table, 7, 3) - 60;
    
                        if (offset[0] > 255U) {
    
                            av_log(ac->avctx, AV_LOG_ERROR,
    
                        sf[idx] = -ff_aac_pow2sf_tab[offset[0] - 100 + POW_SF2_ZERO];
    
    static int decode_pulses(Pulse *pulse, GetBitContext *gb,
                             const uint16_t *swb_offset, int num_swb)
    {
    
        pulse->num_pulse = get_bits(gb, 2) + 1;
    
        pulse_swb        = get_bits(gb, 6);
        if (pulse_swb >= num_swb)
            return -1;
        pulse->pos[0]    = swb_offset[pulse_swb];
    
        pulse->amp[0]    = get_bits(gb, 4);
        for (i = 1; i < pulse->num_pulse; i++) {
    
            pulse->pos[i] = get_bits(gb, 5) + pulse->pos[i - 1];
    
            pulse->amp[i] = get_bits(gb, 4);
    
    /**
     * Decode Temporal Noise Shaping data; reference: table 4.48.
     *
     * @return  Returns error status. 0 - OK, !0 - error
     */
    
    static int decode_tns(AACContext *ac, TemporalNoiseShaping *tns,
                          GetBitContext *gb, const IndividualChannelStream *ics)
    {
    
        int w, filt, i, coef_len, coef_res, coef_compress;
        const int is8 = ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE;
    
        const int tns_max_order = is8 ? 7 : ac->oc[1].m4ac.object_type == AOT_AAC_MAIN ? 20 : 12;
    
        for (w = 0; w < ics->num_windows; w++) {
    
            if ((tns->n_filt[w] = get_bits(gb, 2 - is8))) {
    
                coef_res = get_bits1(gb);
    
    
    Robert Swain's avatar
    Robert Swain committed
                for (filt = 0; filt < tns->n_filt[w]; filt++) {
                    int tmp2_idx;
    
                    tns->length[w][filt] = get_bits(gb, 6 - 2 * is8);
    
                    if ((tns->order[w][filt] = get_bits(gb, 5 - 2 * is8)) > tns_max_order) {
    
                        av_log(ac->avctx, AV_LOG_ERROR, "TNS filter order %d is greater than maximum %d.\n",
    
    Robert Swain's avatar
    Robert Swain committed
                               tns->order[w][filt], tns_max_order);
                        tns->order[w][filt] = 0;
                        return -1;
                    }
    
    Robert Swain's avatar
    Robert Swain committed
                        tns->direction[w][filt] = get_bits1(gb);
                        coef_compress = get_bits1(gb);
                        coef_len = coef_res + 3 - coef_compress;
    
                        tmp2_idx = 2 * coef_compress + coef_res;
    
    Robert Swain's avatar
    Robert Swain committed
                        for (i = 0; i < tns->order[w][filt]; i++)
                            tns->coef[w][filt][i] = tns_tmp2_map[tmp2_idx][get_bits(gb, coef_len)];
    
    /**
     * Decode Mid/Side data; reference: table 4.54.
     *
     * @param   ms_present  Indicates mid/side stereo presence. [0] mask is all 0s;
     *                      [1] mask is decoded from bitstream; [2] mask is all 1s;
     *                      [3] reserved for scalable AAC
     */
    
    static void decode_mid_side_stereo(ChannelElement *cpe, GetBitContext *gb,
                                       int ms_present)
    {
    
        int idx;
        if (ms_present == 1) {
            for (idx = 0; idx < cpe->ch[0].ics.num_window_groups * cpe->ch[0].ics.max_sfb; idx++)
                cpe->ms_mask[idx] = get_bits1(gb);
        } else if (ms_present == 2) {
    
            memset(cpe->ms_mask, 1,  sizeof(cpe->ms_mask[0]) * cpe->ch[0].ics.num_window_groups * cpe->ch[0].ics.max_sfb);
    
    static inline float *VMUL2(float *dst, const float *v, unsigned idx,
                               const float *scale)
    {
        float s = *scale;
        *dst++ = v[idx    & 15] * s;
        *dst++ = v[idx>>4 & 15] * s;
        return dst;
    }
    
    static inline float *VMUL4(float *dst, const float *v, unsigned idx,
                               const float *scale)
    {
        float s = *scale;
        *dst++ = v[idx    & 3] * s;
        *dst++ = v[idx>>2 & 3] * s;
        *dst++ = v[idx>>4 & 3] * s;
        *dst++ = v[idx>>6 & 3] * s;
        return dst;
    }
    
    static inline float *VMUL2S(float *dst, const float *v, unsigned idx,
                                unsigned sign, const float *scale)
    {
    
        union av_intfloat32 s0, s1;
    
    
        s0.f = s1.f = *scale;
        s0.i ^= sign >> 1 << 31;
        s1.i ^= sign      << 31;
    
        *dst++ = v[idx    & 15] * s0.f;
        *dst++ = v[idx>>4 & 15] * s1.f;
    
        return dst;
    }
    
    static inline float *VMUL4S(float *dst, const float *v, unsigned idx,
                                unsigned sign, const float *scale)
    {
        unsigned nz = idx >> 12;
    
        union av_intfloat32 s = { .f = *scale };
        union av_intfloat32 t;
    
        t.i = s.i ^ (sign & 1U<<31);
    
        *dst++ = v[idx    & 3] * t.f;
    
        sign <<= nz & 1; nz >>= 1;
    
        t.i = s.i ^ (sign & 1U<<31);
    
        *dst++ = v[idx>>2 & 3] * t.f;
    
        sign <<= nz & 1; nz >>= 1;
    
        t.i = s.i ^ (sign & 1U<<31);
    
        sign <<= nz & 1;
    
        t.i = s.i ^ (sign & 1U<<31);
    
    /**
     * Decode spectral data; reference: table 4.50.
     * Dequantize and scale spectral data; reference: 4.6.3.3.
     *
     * @param   coef            array of dequantized, scaled spectral data
     * @param   sf              array of scalefactors or intensity stereo positions
     * @param   pulse_present   set if pulses are present
     * @param   pulse           pointer to pulse data struct
     * @param   band_type       array of the used band type
     *
     * @return  Returns error status. 0 - OK, !0 - error
     */
    
    static int decode_spectrum_and_dequant(AACContext *ac, float coef[1024],
    
                                           GetBitContext *gb, const float sf[120],
    
                                           int pulse_present, const Pulse *pulse,
                                           const IndividualChannelStream *ics,
                                           enum BandType band_type[120])
    {
    
        int i, k, g, idx = 0;
    
        const int c = 1024 / ics->num_windows;
        const uint16_t *offsets = ics->swb_offset;
    
        float *coef_base = coef;
    
        for (g = 0; g < ics->num_windows; g++)
    
            memset(coef + g * 128 + offsets[ics->max_sfb], 0, sizeof(float) * (c - offsets[ics->max_sfb]));
    
    
        for (g = 0; g < ics->num_window_groups; g++) {
    
            for (i = 0; i < ics->max_sfb; i++, idx++) {
    
                const unsigned cbt_m1 = band_type[idx] - 1;
                float *cfo = coef + offsets[i];
                int off_len = offsets[i + 1] - offsets[i];
    
                int group;
    
    
                if (cbt_m1 >= INTENSITY_BT2 - 1) {
                    for (group = 0; group < g_len; group++, cfo+=128) {
                        memset(cfo, 0, off_len * sizeof(float));
    
                } else if (cbt_m1 == NOISE_BT - 1) {
                    for (group = 0; group < g_len; group++, cfo+=128) {
    
                        float band_energy;
    
                        for (k = 0; k < off_len; k++) {
    
                            ac->random_state  = lcg_random(ac->random_state);
    
                        band_energy = ac->dsp.scalarproduct_float(cfo, cfo, off_len);
    
                        ac->fdsp.vector_fmul_scalar(cfo, cfo, scale, off_len);
    
                } else {
    
                    const float *vq = ff_aac_codebook_vector_vals[cbt_m1];
                    const uint16_t *cb_vector_idx = ff_aac_codebook_vector_idx[cbt_m1];
                    VLC_TYPE (*vlc_tab)[2] = vlc_spectral[cbt_m1].table;
    
                    switch (cbt_m1 >> 1) {
                    case 0:
                        for (group = 0; group < g_len; group++, cfo+=128) {
                            float *cf = cfo;
                            int len = off_len;
    
                                UPDATE_CACHE(re, gb);
                                GET_VLC(code, re, gb, vlc_tab, 8, 2);
                                cb_idx = cb_vector_idx[code];
    
                                cf = VMUL4(cf, vq, cb_idx, sf + idx);
                            } while (len -= 4);
    
                        }
                        break;
    
                    case 1:
                        for (group = 0; group < g_len; group++, cfo+=128) {
                            float *cf = cfo;
                            int len = off_len;
    
    
                                unsigned nnz;
                                unsigned cb_idx;
                                uint32_t bits;
    
    
                                UPDATE_CACHE(re, gb);
                                GET_VLC(code, re, gb, vlc_tab, 8, 2);
                                cb_idx = cb_vector_idx[code];
    
                                bits = nnz ? GET_CACHE(re, gb) : 0;
    
                                cf = VMUL4S(cf, vq, cb_idx, bits, sf + idx);
                            } while (len -= 4);
    
                        }
                        break;
    
                    case 2:
                        for (group = 0; group < g_len; group++, cfo+=128) {
                            float *cf = cfo;
                            int len = off_len;
    
    
                                UPDATE_CACHE(re, gb);
                                GET_VLC(code, re, gb, vlc_tab, 8, 2);
                                cb_idx = cb_vector_idx[code];
    
                                cf = VMUL2(cf, vq, cb_idx, sf + idx);
                            } while (len -= 2);
    
                        }
                        break;
    
                    case 3:
                    case 4:
                        for (group = 0; group < g_len; group++, cfo+=128) {
                            float *cf = cfo;
                            int len = off_len;
    
    
                                unsigned nnz;
                                unsigned cb_idx;
                                unsigned sign;
    
    
                                UPDATE_CACHE(re, gb);
                                GET_VLC(code, re, gb, vlc_tab, 8, 2);
                                cb_idx = cb_vector_idx[code];
    
                                sign = nnz ? SHOW_UBITS(re, gb, nnz) << (cb_idx >> 12) : 0;
    
                                cf = VMUL2S(cf, vq, cb_idx, sign, sf + idx);
                            } while (len -= 2);
    
                        }
                        break;
    
                    default:
                        for (group = 0; group < g_len; group++, cfo+=128) {
                            float *cf = cfo;
                            uint32_t *icf = (uint32_t *) cf;
                            int len = off_len;
    
    
                                unsigned nzt, nnz;
                                unsigned cb_idx;
                                uint32_t bits;
                                int j;
    
    
                                UPDATE_CACHE(re, gb);
                                GET_VLC(code, re, gb, vlc_tab, 8, 2);
    
                                if (!code) {
    
                                nnz = cb_idx >> 12;
                                nzt = cb_idx >> 8;
    
                                bits = SHOW_UBITS(re, gb, nnz) << (32-nnz);
                                LAST_SKIP_BITS(re, gb, nnz);
    
    
                                for (j = 0; j < 2; j++) {
                                    if (nzt & 1<<j) {
    
                                        /* The total length of escape_sequence must be < 22 bits according
                                           to the specification (i.e. max is 111111110xxxxxxxxxxxx). */
    
                                        UPDATE_CACHE(re, gb);
                                        b = GET_CACHE(re, gb);
                                        b = 31 - av_log2(~b);
    
                                        if (b > 8) {
    
                                            av_log(ac->avctx, AV_LOG_ERROR, "error in spectral data, ESC overflow\n");
    
    
                                        SKIP_BITS(re, gb, b + 1);
                                        b += 4;
                                        n = (1 << b) + SHOW_UBITS(re, gb, b);
                                        LAST_SKIP_BITS(re, gb, b);
    
                                        *icf++ = cbrt_tab[n] | (bits & 1U<<31);
    
                                        bits <<= 1;
                                    } else {
                                        unsigned v = ((const uint32_t*)vq)[cb_idx & 15];
    
                                        *icf++ = (bits & 1U<<31) | v;
    
                            ac->fdsp.vector_fmul_scalar(cfo, cfo, sf[idx], off_len);
    
        }
    
        if (pulse_present) {
    
            for (i = 0; i < pulse->num_pulse; i++) {
                float co = coef_base[ pulse->pos[i] ];
                while (offsets[idx + 1] <= pulse->pos[i])
    
                    idx++;
                if (band_type[idx] != NOISE_BT && sf[idx]) {
    
    Robert Swain's avatar
    Robert Swain committed
                    float ico = -pulse->amp[i];
                    if (co) {
                        co /= sf[idx];
                        ico = co / sqrtf(sqrtf(fabsf(co))) + (co > 0 ? -ico : ico);
                    }
                    coef_base[ pulse->pos[i] ] = cbrtf(fabsf(ico)) * ico * sf[idx];
    
    static av_always_inline float flt16_round(float pf)
    {
    
        union av_intfloat32 tmp;
    
        tmp.f = pf;
        tmp.i = (tmp.i + 0x00008000U) & 0xFFFF0000U;
        return tmp.f;
    
    static av_always_inline float flt16_even(float pf)
    {
    
        union av_intfloat32 tmp;
    
        tmp.i = (tmp.i + 0x00007FFFU + (tmp.i & 0x00010000U >> 16)) & 0xFFFF0000U;
    
    static av_always_inline float flt16_trunc(float pf)
    {
    
        union av_intfloat32 pun;
    
        pun.f = pf;
        pun.i &= 0xFFFF0000U;
        return pun.f;
    
    static av_always_inline void predict(PredictorState *ps, float *coef,
    
                                         int output_enable)
    
    {
        const float a     = 0.953125; // 61.0 / 64
        const float alpha = 0.90625;  // 29.0 / 32
    
        float   r0 = ps->r0,     r1 = ps->r1;
        float cor0 = ps->cor0, cor1 = ps->cor1;
        float var0 = ps->var0, var1 = ps->var1;
    
        k1 = var0 > 1 ? cor0 * flt16_even(a / var0) : 0;
        k2 = var1 > 1 ? cor1 * flt16_even(a / var1) : 0;
    
        pv = flt16_round(k1 * r0 + k2 * r1);
    
        ps->cor1 = flt16_trunc(alpha * cor1 + r1 * e1);
        ps->var1 = flt16_trunc(alpha * var1 + 0.5f * (r1 * r1 + e1 * e1));
        ps->cor0 = flt16_trunc(alpha * cor0 + r0 * e0);
        ps->var0 = flt16_trunc(alpha * var0 + 0.5f * (r0 * r0 + e0 * e0));
    
        ps->r1 = flt16_trunc(a * (r0 - k1 * e0));
    
        ps->r0 = flt16_trunc(a * e0);
    }
    
    /**
     * Apply AAC-Main style frequency domain prediction.
     */
    
    static void apply_prediction(AACContext *ac, SingleChannelElement *sce)
    {
    
        int sfb, k;
    
        if (!sce->ics.predictor_initialized) {
    
            reset_all_predictors(sce->predictor_state);
    
            sce->ics.predictor_initialized = 1;
        }
    
        if (sce->ics.window_sequence[0] != EIGHT_SHORT_SEQUENCE) {
    
            for (sfb = 0; sfb < ff_aac_pred_sfb_max[ac->oc[1].m4ac.sampling_index]; sfb++) {
    
                for (k = sce->ics.swb_offset[sfb]; k < sce->ics.swb_offset[sfb + 1]; k++) {
    
                    predict(&sce->predictor_state[k], &sce->coeffs[k],
    
                            sce->ics.predictor_present && sce->ics.prediction_used[sfb]);
    
                }
            }
            if (sce->ics.predictor_reset_group)
    
                reset_predictor_group(sce->predictor_state, sce->ics.predictor_reset_group);
    
            reset_all_predictors(sce->predictor_state);
    
     * Decode an individual_channel_stream payload; reference: table 4.44.
     *
     * @param   common_window   Channels have independent [0], or shared [1], Individual Channel Stream information.
     * @param   scale_flag      scalable [1] or non-scalable [0] AAC (Unused until scalable AAC is implemented.)
     *
     * @return  Returns error status. 0 - OK, !0 - error
     */
    
    static int decode_ics(AACContext *ac, SingleChannelElement *sce,
                          GetBitContext *gb, int common_window, int scale_flag)
    {
    
        TemporalNoiseShaping    *tns = &sce->tns;
        IndividualChannelStream *ics = &sce->ics;
        float *out = sce->coeffs;
    
        int global_gain, pulse_present = 0;
    
    
        /* This assignment is to silence a GCC warning about the variable being used
         * uninitialized when in fact it always is.
    
         */
        pulse.num_pulse = 0;
    
        global_gain = get_bits(gb, 8);
    
        if (!common_window && !scale_flag) {
    
            if (decode_ics_info(ac, ics, gb) < 0)
                return AVERROR_INVALIDDATA;
    
        }
    
        if (decode_band_types(ac, sce->band_type, sce->band_type_run_end, gb, ics) < 0)
            return -1;
        if (decode_scalefactors(ac, sce->sf, gb, global_gain, ics, sce->band_type, sce->band_type_run_end) < 0)
            return -1;
    
        pulse_present = 0;
        if (!scale_flag) {
            if ((pulse_present = get_bits1(gb))) {
                if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
    
                    av_log(ac->avctx, AV_LOG_ERROR, "Pulse tool not allowed in eight short sequence.\n");
    
                if (decode_pulses(&pulse, gb, ics->swb_offset, ics->num_swb)) {
    
                    av_log(ac->avctx, AV_LOG_ERROR, "Pulse data corrupt or invalid.\n");
    
            }
            if ((tns->present = get_bits1(gb)) && decode_tns(ac, tns, gb, ics))
                return -1;
            if (get_bits1(gb)) {
    
                av_log_missing_feature(ac->avctx, "SSR", 1);
    
                return AVERROR_PATCHWELCOME;
    
        if (decode_spectrum_and_dequant(ac, out, gb, sce->sf, pulse_present, &pulse, ics, sce->band_type) < 0)
    
        if (ac->oc[1].m4ac.object_type == AOT_AAC_MAIN && !common_window)
    
    /**
     * Mid/Side stereo decoding; reference: 4.6.8.1.3.
     */
    
    static void apply_mid_side_stereo(AACContext *ac, ChannelElement *cpe)
    
    {
        const IndividualChannelStream *ics = &cpe->ch[0].ics;
    
        float *ch0 = cpe->ch[0].coeffs;
        float *ch1 = cpe->ch[1].coeffs;
    
        int g, i, group, idx = 0;
    
        const uint16_t *offsets = ics->swb_offset;
    
        for (g = 0; g < ics->num_window_groups; g++) {
            for (i = 0; i < ics->max_sfb; i++, idx++) {
                if (cpe->ms_mask[idx] &&
    
                        cpe->ch[0].band_type[idx] < NOISE_BT && cpe->ch[1].band_type[idx] < NOISE_BT) {
    
                    for (group = 0; group < ics->group_len[g]; group++) {
    
                        ac->dsp.butterflies_float(ch0 + group * 128 + offsets[i],
                                                  ch1 + group * 128 + offsets[i],
                                                  offsets[i+1] - offsets[i]);
    
            ch0 += ics->group_len[g] * 128;
            ch1 += ics->group_len[g] * 128;
    
        }
    }
    
    /**
     * intensity stereo decoding; reference: 4.6.8.2.3
     *
     * @param   ms_present  Indicates mid/side stereo presence. [0] mask is all 0s;
     *                      [1] mask is decoded from bitstream; [2] mask is all 1s;
     *                      [3] reserved for scalable AAC
     */
    
    static void apply_intensity_stereo(AACContext *ac, ChannelElement *cpe, int ms_present)
    
    {
        const IndividualChannelStream *ics = &cpe->ch[1].ics;
        SingleChannelElement         *sce1 = &cpe->ch[1];
    
        float *coef0 = cpe->ch[0].coeffs, *coef1 = cpe->ch[1].coeffs;
    
        const uint16_t *offsets = ics->swb_offset;
    
        int c;
        float scale;
        for (g = 0; g < ics->num_window_groups; g++) {
            for (i = 0; i < ics->max_sfb;) {
                if (sce1->band_type[idx] == INTENSITY_BT || sce1->band_type[idx] == INTENSITY_BT2) {
                    const int bt_run_end = sce1->band_type_run_end[idx];
                    for (; i < bt_run_end; i++, idx++) {
                        c = -1 + 2 * (sce1->band_type[idx] - 14);
                        if (ms_present)
                            c *= 1 - 2 * cpe->ms_mask[idx];
                        scale = c * sce1->sf[idx];
                        for (group = 0; group < ics->group_len[g]; group++)
    
                            ac->fdsp.vector_fmul_scalar(coef1 + group * 128 + offsets[i],
                                                        coef0 + group * 128 + offsets[i],
                                                        scale,
                                                        offsets[i + 1] - offsets[i]);
    
                    }
                } else {
                    int bt_run_end = sce1->band_type_run_end[idx];
                    idx += bt_run_end - i;
                    i    = bt_run_end;
                }
            }
    
            coef0 += ics->group_len[g] * 128;
            coef1 += ics->group_len[g] * 128;
    
    /**
     * Decode a channel_pair_element; reference: table 4.4.
     *
     * @return  Returns error status. 0 - OK, !0 - error
     */
    
    static int decode_cpe(AACContext *ac, GetBitContext *gb, ChannelElement *cpe)
    {
    
        int i, ret, common_window, ms_present = 0;
    
        common_window = get_bits1(gb);
        if (common_window) {
    
            if (decode_ics_info(ac, &cpe->ch[0].ics, gb))
                return AVERROR_INVALIDDATA;
    
            i = cpe->ch[1].ics.use_kb_window[0];
            cpe->ch[1].ics = cpe->ch[0].ics;
            cpe->ch[1].ics.use_kb_window[1] = i;
    
            if (cpe->ch[1].ics.predictor_present && (ac->oc[1].m4ac.object_type != AOT_AAC_MAIN))
    
                if ((cpe->ch[1].ics.ltp.present = get_bits(gb, 1)))
    
                    decode_ltp(&cpe->ch[1].ics.ltp, gb, cpe->ch[1].ics.max_sfb);
    
            ms_present = get_bits(gb, 2);
    
            if (ms_present == 3) {
    
                av_log(ac->avctx, AV_LOG_ERROR, "ms_present = 3 is reserved.\n");
    
            } else if (ms_present)
    
                decode_mid_side_stereo(cpe, gb, ms_present);
        }
        if ((ret = decode_ics(ac, &cpe->ch[0], gb, common_window, 0)))
            return ret;
        if ((ret = decode_ics(ac, &cpe->ch[1], gb, common_window, 0)))
            return ret;
    
    
        if (common_window) {
            if (ms_present)
    
                apply_mid_side_stereo(ac, cpe);
    
            if (ac->oc[1].m4ac.object_type == AOT_AAC_MAIN) {
    
                apply_prediction(ac, &cpe->ch[0]);
                apply_prediction(ac, &cpe->ch[1]);
            }
        }
    
        apply_intensity_stereo(ac, cpe, ms_present);
    
    static const float cce_scale[] = {
        1.09050773266525765921, //2^(1/8)
        1.18920711500272106672, //2^(1/4)
        M_SQRT2,
        2,
    };
    
    
    /**
     * Decode coupling_channel_element; reference: table 4.8.
     *
     * @return  Returns error status. 0 - OK, !0 - error
     */
    
    static int decode_cce(AACContext *ac, GetBitContext *gb, ChannelElement *che)
    {
    
        int num_gain = 0;
    
        int sign;
        float scale;
    
        SingleChannelElement *sce = &che->ch[0];
        ChannelCoupling     *coup = &che->coup;
    
        coup->coupling_point = 2 * get_bits1(gb);
    
        coup->num_coupled = get_bits(gb, 3);
        for (c = 0; c <= coup->num_coupled; c++) {
            num_gain++;
            coup->type[c] = get_bits1(gb) ? TYPE_CPE : TYPE_SCE;
            coup->id_select[c] = get_bits(gb, 4);
            if (coup->type[c] == TYPE_CPE) {
                coup->ch_select[c] = get_bits(gb, 2);
                if (coup->ch_select[c] == 3)
                    num_gain++;
            } else
    
        coup->coupling_point += get_bits1(gb) || (coup->coupling_point >> 1);
    
        sign  = get_bits(gb, 1);
    
        scale = cce_scale[get_bits(gb, 2)];
    
    
        if ((ret = decode_ics(ac, sce, gb, 0, 0)))
            return ret;
    
        for (c = 0; c < num_gain; c++) {
    
            int idx  = 0;
            int cge  = 1;
    
            int gain = 0;
            float gain_cache = 1.;
            if (c) {
                cge = coup->coupling_point == AFTER_IMDCT ? 1 : get_bits1(gb);
                gain = cge ? get_vlc2(gb, vlc_scalefactors.table, 7, 3) - 60: 0;
    
                gain_cache = powf(scale, -gain);
    
            if (coup->coupling_point == AFTER_IMDCT) {
                coup->gain[c][0] = gain_cache;
            } else {
    
                for (g = 0; g < sce->ics.num_window_groups; g++) {
                    for (sfb = 0; sfb < sce->ics.max_sfb; sfb++, idx++) {
                        if (sce->band_type[idx] != ZERO_BT) {
                            if (!cge) {
                                int t = get_vlc2(gb, vlc_scalefactors.table, 7, 3) - 60;
    
                                if (t) {
    
                                    int s = 1;
                                    t = gain += t;
                                    if (sign) {
                                        s  -= 2 * (t & 0x1);
                                        t >>= 1;
                                    }
    
                                    gain_cache = powf(scale, -t) * s;
    
                            coup->gain[c][idx] = gain_cache;
    
        }
        return 0;
    }
    
    /**
     * Parse whether channels are to be excluded from Dynamic Range Compression; reference: table 4.53.
     *
     * @return  Returns number of bytes consumed.
     */
    
    static int decode_drc_channel_exclusions(DynamicRangeControl *che_drc,
                                             GetBitContext *gb)
    {
    
        int i;
        int num_excl_chan = 0;
    
        do {
            for (i = 0; i < 7; i++)
                che_drc->exclude_mask[num_excl_chan++] = get_bits1(gb);
        } while (num_excl_chan < MAX_CHANNELS - 7 && get_bits1(gb));
    
        return num_excl_chan / 7;
    }
    
    
    /**
     * Decode dynamic range information; reference: table 4.52.
     *
     * @return  Returns number of bytes consumed.
     */
    
    static int decode_dynamic_range(DynamicRangeControl *che_drc,
    
                                    GetBitContext *gb)
    
    {
        int n             = 1;
    
        int drc_num_bands = 1;
        int i;
    
        /* pce_tag_present? */
    
        if (get_bits1(gb)) {
    
            che_drc->pce_instance_tag  = get_bits(gb, 4);
            skip_bits(gb, 4); // tag_reserved_bits
            n++;
        }
    
        /* excluded_chns_present? */
    
        if (get_bits1(gb)) {
    
            n += decode_drc_channel_exclusions(che_drc, gb);
        }
    
        /* drc_bands_present? */
        if (get_bits1(gb)) {
            che_drc->band_incr            = get_bits(gb, 4);
            che_drc->interpolation_scheme = get_bits(gb, 4);
            n++;
            drc_num_bands += che_drc->band_incr;
            for (i = 0; i < drc_num_bands; i++) {
                che_drc->band_top[i] = get_bits(gb, 8);
                n++;
            }
        }
    
        /* prog_ref_level_present? */
        if (get_bits1(gb)) {
            che_drc->prog_ref_level = get_bits(gb, 7);
            skip_bits1(gb); // prog_ref_level_reserved_bits
            n++;
        }
    
        for (i = 0; i < drc_num_bands; i++) {
            che_drc->dyn_rng_sgn[i] = get_bits1(gb);
            che_drc->dyn_rng_ctl[i] = get_bits(gb, 7);
            n++;
        }
    
        return n;
    }
    
    
    static int decode_fill(AACContext *ac, GetBitContext *gb, int len) {
        uint8_t buf[256];
        int i, major, minor;
    
        if (len < 13+7*8)
            goto unknown;
    
        get_bits(gb, 13); len -= 13;
    
        for(i=0; i+1<sizeof(buf) && len>=8; i++, len-=8)
            buf[i] = get_bits(gb, 8);
    
        buf[i] = 0;
        if (ac->avctx->debug & FF_DEBUG_PICT_INFO)
            av_log(ac->avctx, AV_LOG_DEBUG, "FILL:%s\n", buf);
    
        if (sscanf(buf, "libfaac %d.%d", &major, &minor) == 2){
            ac->avctx->internal->skip_samples = 1024;
        }
    
    unknown:
        skip_bits_long(gb, len);