Skip to content
Snippets Groups Projects
aacdec.c 88.6 KiB
Newer Older
  • Learn to ignore specific revisions
  •  * @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->dsp.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 = SHOW_UBITS(re, gb, nnz) << (32-nnz);
                                LAST_SKIP_BITS(re, gb, nnz);
    
                                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 = SHOW_UBITS(re, gb, nnz) << (cb_idx >> 12);
                                LAST_SKIP_BITS(re, gb, nnz);
    
                                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->dsp.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 float754 tmp;
        tmp.f = pf;
        tmp.i = (tmp.i + 0x00008000U) & 0xFFFF0000U;
        return tmp.f;
    
    static av_always_inline float flt16_even(float pf)
    {
    
        tmp.i = (tmp.i + 0x00007FFFU + (tmp.i & 0x00010000U >> 16)) & 0xFFFF0000U;
    
    static av_always_inline float flt16_trunc(float pf)
    {
    
        union float754 pun;
        pun.f = pf;
        pun.i &= 0xFFFF0000U;
        return pun.f;
    
    static av_always_inline void predict(PredictorState *ps, float *coef,
                                         float sf_scale, float inv_sf_scale,
    
                                         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);
    
            *coef += pv * sf_scale;
    
        e0 = *coef * inv_sf_scale;
    
        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)
    {
    
        float sf_scale = ac->sf_scale, inv_sf_scale = 1 / ac->sf_scale;
    
    
        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->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],
                            sf_scale, inv_sf_scale,
    
                            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) < 0)
                return -1;
        }
    
        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);
    
        if (decode_spectrum_and_dequant(ac, out, gb, sce->sf, pulse_present, &pulse, ics, sce->band_type) < 0)
    
        if (ac->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->dsp.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, 1))
                return -1;
            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->m4ac.object_type != AOT_AAC_MAIN))
                if ((cpe->ch[1].ics.ltp.present = get_bits(gb, 1)))
                    decode_ltp(ac, &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->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.
     *
     * @param   cnt length of TYPE_FIL syntactic element in bytes
     *
     * @return  Returns number of bytes consumed.
     */
    
    static int decode_dynamic_range(DynamicRangeControl *che_drc,
                                    GetBitContext *gb, int cnt)
    {
        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;
    }
    
    /**
     * Decode extension data (incomplete); reference: table 4.51.
     *
     * @param   cnt length of TYPE_FIL syntactic element in bytes
     *
     * @return Returns number of bytes consumed
     */
    
    Alex Converse's avatar
    Alex Converse committed
    static int decode_extension_payload(AACContext *ac, GetBitContext *gb, int cnt,
                                        ChannelElement *che, enum RawDataBlockType elem_type)
    
        int crc_flag = 0;
        int res = cnt;
        switch (get_bits(gb, 4)) { // extension type
    
        case EXT_SBR_DATA_CRC:
            crc_flag++;
        case EXT_SBR_DATA:
    
    Alex Converse's avatar
    Alex Converse committed
            if (!che) {
    
                av_log(ac->avctx, AV_LOG_ERROR, "SBR was found before the first channel element.\n");
    
    Alex Converse's avatar
    Alex Converse committed
                return res;
            } else if (!ac->m4ac.sbr) {
    
                av_log(ac->avctx, AV_LOG_ERROR, "SBR signaled to be not-present but was found in the bitstream.\n");
    
    Alex Converse's avatar
    Alex Converse committed
                skip_bits_long(gb, 8 * cnt - 4);
                return res;
            } else if (ac->m4ac.sbr == -1 && ac->output_configured == OC_LOCKED) {
    
                av_log(ac->avctx, AV_LOG_ERROR, "Implicit SBR was found with a first occurrence after the first frame.\n");
    
    Alex Converse's avatar
    Alex Converse committed
                skip_bits_long(gb, 8 * cnt - 4);
                return res;
    
            } else if (ac->m4ac.ps == -1 && ac->output_configured < OC_LOCKED && ac->avctx->channels == 1) {
                ac->m4ac.sbr = 1;
                ac->m4ac.ps = 1;
                output_configure(ac, ac->che_pos, ac->che_pos, ac->m4ac.chan_config, ac->output_configured);
    
    Alex Converse's avatar
    Alex Converse committed
            } else {
                ac->m4ac.sbr = 1;
            }
            res = ff_decode_sbr_extension(ac, &che->sbr, gb, crc_flag, cnt, elem_type);
    
            break;
        case EXT_DYNAMIC_RANGE:
            res = decode_dynamic_range(&ac->che_drc, gb, cnt);
            break;
        case EXT_FILL:
        case EXT_FILL_DATA:
        case EXT_DATA_ELEMENT:
        default:
            skip_bits_long(gb, 8 * cnt - 4);
            break;
    
    /**
     * Decode Temporal Noise Shaping filter coefficients and apply all-pole filters; reference: 4.6.9.3.
     *
     * @param   decode  1 if tool is used normally, 0 if tool is used in LTP.
     * @param   coef    spectral coefficients
     */
    
    static void apply_tns(float coef[1024], TemporalNoiseShaping *tns,
                          IndividualChannelStream *ics, int decode)
    {
        const int mmm = FFMIN(ics->tns_max_bands, ics->max_sfb);
    
    Robert Swain's avatar
    Robert Swain committed
        int w, filt, m, i;
    
        int bottom, top, order, start, end, size, inc;
        float lpc[TNS_MAX_ORDER];
    
        float tmp[TNS_MAX_ORDER];
    
    
        for (w = 0; w < ics->num_windows; w++) {
            bottom = ics->num_swb;
            for (filt = 0; filt < tns->n_filt[w]; filt++) {
                top    = bottom;
                bottom = FFMAX(0, top - tns->length[w][filt]);
                order  = tns->order[w][filt];
                if (order == 0)
                    continue;
    
    
                // tns_decode_coef
                compute_lpc_coefs(tns->coef[w][filt], order, lpc, 0, 0, 0);
    
                start = ics->swb_offset[FFMIN(bottom, mmm)];
                end   = ics->swb_offset[FFMIN(   top, mmm)];
                if ((size = end - start) <= 0)
                    continue;
                if (tns->direction[w][filt]) {
    
                    inc = -1;
                    start = end - 1;
    
                } else {
                    inc = 1;
                }
                start += w * 128;
    
    
                if (decode) {
                    // ar filter
                    for (m = 0; m < size; m++, start += inc)
                        for (i = 1; i <= FFMIN(m, order); i++)
                            coef[start] -= coef[start - i * inc] * lpc[i - 1];
                } else {
                    // ma filter
                    for (m = 0; m < size; m++, start += inc) {
                        tmp[0] = coef[start];
                        for (i = 1; i <= FFMIN(m, order); i++)
                            coef[start] += tmp[i] * lpc[i - 1];
                        for (i = order; i > 0; i--)
                            tmp[i] = tmp[i - 1];
                    }
                }
    
    /**
     *  Apply windowing and MDCT to obtain the spectral
     *  coefficient from the predicted sample by LTP.
     */
    static void windowing_and_mdct_ltp(AACContext *ac, float *out,
                                       float *in, IndividualChannelStream *ics)
    {
        const float *lwindow      = ics->use_kb_window[0] ? ff_aac_kbd_long_1024 : ff_sine_1024;
        const float *swindow      = ics->use_kb_window[0] ? ff_aac_kbd_short_128 : ff_sine_128;
        const float *lwindow_prev = ics->use_kb_window[1] ? ff_aac_kbd_long_1024 : ff_sine_1024;
        const float *swindow_prev = ics->use_kb_window[1] ? ff_aac_kbd_short_128 : ff_sine_128;
    
        if (ics->window_sequence[0] != LONG_STOP_SEQUENCE) {
            ac->dsp.vector_fmul(in, in, lwindow_prev, 1024);
        } else {
            memset(in, 0, 448 * sizeof(float));
            ac->dsp.vector_fmul(in + 448, in + 448, swindow_prev, 128);
            memcpy(in + 576, in + 576, 448 * sizeof(float));
        }
        if (ics->window_sequence[0] != LONG_START_SEQUENCE) {
            ac->dsp.vector_fmul_reverse(in + 1024, in + 1024, lwindow, 1024);
        } else {
            memcpy(in + 1024, in + 1024, 448 * sizeof(float));
            ac->dsp.vector_fmul_reverse(in + 1024 + 448, in + 1024 + 448, swindow, 128);
            memset(in + 1024 + 576, 0, 448 * sizeof(float));
        }
    
        ac->mdct_ltp.mdct_calc(&ac->mdct_ltp, out, in);
    
    }
    
    /**
     * Apply the long term prediction
     */
    static void apply_ltp(AACContext *ac, SingleChannelElement *sce)
    {
        const LongTermPrediction *ltp = &sce->ics.ltp;
        const uint16_t *offsets = sce->ics.swb_offset;
        int i, sfb;
    
        if (sce->ics.window_sequence[0] != EIGHT_SHORT_SEQUENCE) {
    
            float *predTime = sce->ret;
            float *predFreq = ac->buf_mdct;
    
            int16_t num_samples = 2048;
    
            if (ltp->lag < 1024)
                num_samples = ltp->lag + 1024;
            for (i = 0; i < num_samples; i++)
                predTime[i] = sce->ltp_state[i + 2048 - ltp->lag] * ltp->coef;
            memset(&predTime[i], 0, (2048 - i) * sizeof(float));
    
            windowing_and_mdct_ltp(ac, predFreq, predTime, &sce->ics);
    
            if (sce->tns.present)
                apply_tns(predFreq, &sce->tns, &sce->ics, 0);
    
            for (sfb = 0; sfb < FFMIN(sce->ics.max_sfb, MAX_LTP_LONG_SFB); sfb++)
                if (ltp->used[sfb])
                    for (i = offsets[sfb]; i < offsets[sfb + 1]; i++)
                        sce->coeffs[i] += predFreq[i];
        }
    }
    
    /**
     * Update the LTP buffer for next frame
     */
    static void update_ltp(AACContext *ac, SingleChannelElement *sce)
    {
        IndividualChannelStream *ics = &sce->ics;
        float *saved     = sce->saved;
        float *saved_ltp = sce->coeffs;
        const float *lwindow = ics->use_kb_window[0] ? ff_aac_kbd_long_1024 : ff_sine_1024;
        const float *swindow = ics->use_kb_window[0] ? ff_aac_kbd_short_128 : ff_sine_128;
        int i;
    
        if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
            memcpy(saved_ltp,       saved, 512 * sizeof(float));
            memset(saved_ltp + 576, 0,     448 * sizeof(float));
    
            ac->dsp.vector_fmul_reverse(saved_ltp + 448, ac->buf_mdct + 960,     &swindow[64],      64);
            for (i = 0; i < 64; i++)
                saved_ltp[i + 512] = ac->buf_mdct[1023 - i] * swindow[63 - i];
    
        } else if (ics->window_sequence[0] == LONG_START_SEQUENCE) {
            memcpy(saved_ltp,       ac->buf_mdct + 512, 448 * sizeof(float));
            memset(saved_ltp + 576, 0,                  448 * sizeof(float));
    
            ac->dsp.vector_fmul_reverse(saved_ltp + 448, ac->buf_mdct + 960,     &swindow[64],      64);
            for (i = 0; i < 64; i++)
                saved_ltp[i + 512] = ac->buf_mdct[1023 - i] * swindow[63 - i];
    
        } else { // LONG_STOP or ONLY_LONG
    
            ac->dsp.vector_fmul_reverse(saved_ltp,       ac->buf_mdct + 512,     &lwindow[512],     512);
            for (i = 0; i < 512; i++)
                saved_ltp[i + 512] = ac->buf_mdct[1023 - i] * lwindow[511 - i];
    
        }
    
        memcpy(sce->ltp_state, &sce->ltp_state[1024], 1024 * sizeof(int16_t));
        ac->fmt_conv.float_to_int16(&(sce->ltp_state[1024]), sce->ret,  1024);
        ac->fmt_conv.float_to_int16(&(sce->ltp_state[2048]), saved_ltp, 1024);
    }
    
    
    /**
     * Conduct IMDCT and windowing.
     */
    
    static void imdct_and_windowing(AACContext *ac, SingleChannelElement *sce)
    
    {
        IndividualChannelStream *ics = &sce->ics;
        float *in    = sce->coeffs;
        float *out   = sce->ret;
        float *saved = sce->saved;
        const float *swindow      = ics->use_kb_window[0] ? ff_aac_kbd_short_128 : ff_sine_128;
        const float *lwindow_prev = ics->use_kb_window[1] ? ff_aac_kbd_long_1024 : ff_sine_1024;
        const float *swindow_prev = ics->use_kb_window[1] ? ff_aac_kbd_short_128 : ff_sine_128;
        float *buf  = ac->buf_mdct;
        float *temp = ac->temp;
    
        if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
    
                ac->mdct_small.imdct_half(&ac->mdct_small, buf + i, in + i);
    
            ac->mdct.imdct_half(&ac->mdct, buf, in);
    
    
        /* window overlapping
         * NOTE: To simplify the overlapping code, all 'meaningless' short to long
         * and long to short transitions are considered to be short to short
         * transitions. This leaves just two cases (long to long and short to short)
         * with a little special sauce for EIGHT_SHORT_SEQUENCE.
         */
        if ((ics->window_sequence[1] == ONLY_LONG_SEQUENCE || ics->window_sequence[1] == LONG_STOP_SEQUENCE) &&
    
                (ics->window_sequence[0] == ONLY_LONG_SEQUENCE || ics->window_sequence[0] == LONG_START_SEQUENCE)) {
    
            ac->dsp.vector_fmul_window(    out,               saved,            buf,         lwindow_prev, 512);
    
            memcpy(                        out,               saved,            448 * sizeof(float));
    
            if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
    
                ac->dsp.vector_fmul_window(out + 448 + 0*128, saved + 448,      buf + 0*128, swindow_prev, 64);
                ac->dsp.vector_fmul_window(out + 448 + 1*128, buf + 0*128 + 64, buf + 1*128, swindow,      64);
                ac->dsp.vector_fmul_window(out + 448 + 2*128, buf + 1*128 + 64, buf + 2*128, swindow,      64);
                ac->dsp.vector_fmul_window(out + 448 + 3*128, buf + 2*128 + 64, buf + 3*128, swindow,      64);
                ac->dsp.vector_fmul_window(temp,              buf + 3*128 + 64, buf + 4*128, swindow,      64);
    
                memcpy(                    out + 448 + 4*128, temp, 64 * sizeof(float));
    
                ac->dsp.vector_fmul_window(out + 448,         saved + 448,      buf,         swindow_prev, 64);
    
                memcpy(                    out + 576,         buf + 64,         448 * sizeof(float));
    
        // buffer update
        if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
    
            memcpy(                    saved,       temp + 64,         64 * sizeof(float));
    
            ac->dsp.vector_fmul_window(saved + 64,  buf + 4*128 + 64, buf + 5*128, swindow, 64);
            ac->dsp.vector_fmul_window(saved + 192, buf + 5*128 + 64, buf + 6*128, swindow, 64);
            ac->dsp.vector_fmul_window(saved + 320, buf + 6*128 + 64, buf + 7*128, swindow, 64);
    
            memcpy(                    saved + 448, buf + 7*128 + 64,  64 * sizeof(float));
    
        } else if (ics->window_sequence[0] == LONG_START_SEQUENCE) {
    
            memcpy(                    saved,       buf + 512,        448 * sizeof(float));
            memcpy(                    saved + 448, buf + 7*128 + 64,  64 * sizeof(float));
    
            memcpy(                    saved,       buf + 512,        512 * sizeof(float));
    
    /**
     * Apply dependent channel coupling (applied before IMDCT).
     *
     * @param   index   index into coupling gain array
     */
    
    static void apply_dependent_coupling(AACContext *ac,
                                         SingleChannelElement *target,
                                         ChannelElement *cce, int index)
    {
        IndividualChannelStream *ics = &cce->ch[0].ics;
        const uint16_t *offsets = ics->swb_offset;
        float *dest = target->coeffs;
        const float *src = cce->ch[0].coeffs;
    
        if (ac->m4ac.object_type == AOT_AAC_LTP) {
    
            av_log(ac->avctx, AV_LOG_ERROR,
    
                   "Dependent coupling is not supported together with LTP\n");
            return;
        }
        for (g = 0; g < ics->num_window_groups; g++) {
            for (i = 0; i < ics->max_sfb; i++, idx++) {
    
                if (cce->ch[0].band_type[idx] != ZERO_BT) {
    
                    const float gain = cce->coup.gain[index][idx];
    
                    for (group = 0; group < ics->group_len[g]; group++) {
    
                        for (k = offsets[i]; k < offsets[i + 1]; k++) {
    
                            dest[group * 128 + k] += gain * src[group * 128 + k];
    
            dest += ics->group_len[g] * 128;
            src  += ics->group_len[g] * 128;
    
        }
    }
    
    /**
     * Apply independent channel coupling (applied after IMDCT).
     *
     * @param   index   index into coupling gain array
     */
    
    static void apply_independent_coupling(AACContext *ac,
                                           SingleChannelElement *target,
                                           ChannelElement *cce, int index)
    {
    
        const float gain = cce->coup.gain[index][0];
    
        const float *src = cce->ch[0].ret;
        float *dest = target->ret;
    
    Alex Converse's avatar
    Alex Converse committed
        const int len = 1024 << (ac->m4ac.sbr == 1);
    
    Alex Converse's avatar
    Alex Converse committed
        for (i = 0; i < len; i++)
    
    /**
     * channel coupling transformation interface
     *
     * @param   apply_coupling_method   pointer to (in)dependent coupling function
     */
    
    static void apply_channel_coupling(AACContext *ac, ChannelElement *cc,
                                       enum RawDataBlockType type, int elem_id,
                                       enum CouplingPoint coupling_point,
                                       void (*apply_coupling_method)(AACContext *ac, SingleChannelElement *target, ChannelElement *cce, int index))
    
        int i, c;
    
        for (i = 0; i < MAX_ELEM_ID; i++) {
            ChannelElement *cce = ac->che[TYPE_CCE][i];
            int index = 0;
    
            if (cce && cce->coup.coupling_point == coupling_point) {
    
                ChannelCoupling *coup = &cce->coup;
    
    
                for (c = 0; c <= coup->num_coupled; c++) {
                    if (coup->type[c] == type && coup->id_select[c] == elem_id) {
                        if (coup->ch_select[c] != 1) {
                            apply_coupling_method(ac, &cc->ch[0], cce, index);
                            if (coup->ch_select[c] != 0)
                                index++;
                        }
                        if (coup->ch_select[c] != 2)
                            apply_coupling_method(ac, &cc->ch[1], cce, index++);
                    } else
                        index += 1 + (coup->ch_select[c] == 3);
    
                }
            }
        }
    }
    
    /**
     * Convert spectral data to float samples, applying all supported tools as appropriate.
     */
    
    static void spectral_to_sample(AACContext *ac)
    {
    
        int i, type;
        for (type = 3; type >= 0; type--) {
    
                ChannelElement *che = ac->che[type][i];
    
                if (che) {
                    if (type <= TYPE_CPE)
    
                        apply_channel_coupling(ac, che, type, i, BEFORE_TNS, apply_dependent_coupling);
    
                    if (ac->m4ac.object_type == AOT_AAC_LTP) {
                        if (che->ch[0].ics.predictor_present) {
                            if (che->ch[0].ics.ltp.present)