Skip to content
Snippets Groups Projects
aacdec.c 104 KiB
Newer Older
  • Learn to ignore specific revisions
  • /**
     * 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->oc[1].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->oc[1].m4ac.sbr == -1 && ac->oc[1].status == 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->oc[1].m4ac.ps == -1 && ac->oc[1].status < OC_LOCKED && ac->avctx->channels == 1) {
                ac->oc[1].m4ac.sbr = 1;
                ac->oc[1].m4ac.ps = 1;
                output_configure(ac, ac->oc[1].layout_map, ac->oc[1].layout_map_tags,
    
                                 ac->oc[1].status, 1);
    
    Alex Converse's avatar
    Alex Converse committed
            } else {
    
                ac->oc[1].m4ac.sbr = 1;
    
    Alex Converse's avatar
    Alex Converse committed
            }
            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);
    
            break;
        case EXT_FILL:
    
            decode_fill(ac, gb, 8 * cnt - 4);
            break;
    
        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+1];
    
    
        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->fdsp.vector_fmul(in, in, lwindow_prev, 1024);
    
        } else {
            memset(in, 0, 448 * sizeof(float));
    
            ac->fdsp.vector_fmul(in + 448, in + 448, swindow_prev, 128);
    
        }
        if (ics->window_sequence[0] != LONG_START_SEQUENCE) {
            ac->dsp.vector_fmul_reverse(in + 1024, in + 1024, lwindow, 1024);
        } else {
            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(*sce->ltp_state));
        memcpy(sce->ltp_state+1024, sce->ret,            1024 * sizeof(*sce->ltp_state));
        memcpy(sce->ltp_state+2048, saved_ltp,           1024 * sizeof(*sce->ltp_state));
    
    /**
     * 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->oc[1].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;
    
        const int len = 1024 << (ac->oc[1].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->oc[1].m4ac.object_type == AOT_AAC_LTP) {
    
                        if (che->ch[0].ics.predictor_present) {
                            if (che->ch[0].ics.ltp.present)
                                apply_ltp(ac, &che->ch[0]);
                            if (che->ch[1].ics.ltp.present && type == TYPE_CPE)
                                apply_ltp(ac, &che->ch[1]);
                        }
                    }
    
                    if (che->ch[0].tns.present)
    
                        apply_tns(che->ch[0].coeffs, &che->ch[0].tns, &che->ch[0].ics, 1);
    
                    if (che->ch[1].tns.present)
    
                        apply_tns(che->ch[1].coeffs, &che->ch[1].tns, &che->ch[1].ics, 1);
    
                    if (type <= TYPE_CPE)
    
                        apply_channel_coupling(ac, che, type, i, BETWEEN_TNS_AND_IMDCT, apply_dependent_coupling);
    
    Alex Converse's avatar
    Alex Converse committed
                    if (type != TYPE_CCE || che->coup.coupling_point == AFTER_IMDCT) {
    
                        if (ac->oc[1].m4ac.object_type == AOT_AAC_LTP)
    
                            update_ltp(ac, &che->ch[0]);
    
    Alex Converse's avatar
    Alex Converse committed
                        if (type == TYPE_CPE) {
    
                            if (ac->oc[1].m4ac.object_type == AOT_AAC_LTP)
    
                                update_ltp(ac, &che->ch[1]);
    
    Alex Converse's avatar
    Alex Converse committed
                        }
    
                        if (ac->oc[1].m4ac.sbr > 0) {
    
                            ff_sbr_apply(ac, &che->sbr, type, che->ch[0].ret, che->ch[1].ret);
                        }
    
                    if (type <= TYPE_CCE)
    
                        apply_channel_coupling(ac, che, type, i, AFTER_IMDCT, apply_independent_coupling);
    
    static int parse_adts_frame_header(AACContext *ac, GetBitContext *gb)
    {
    
        uint8_t layout_map[MAX_ELEM_ID*4][3];
        int layout_map_tags;
    
        size = avpriv_aac_parse_header(gb, &hdr_info);
    
            if (!ac->warned_num_aac_frames && hdr_info.num_aac_frames != 1) {
                // This is 2 for "VLB " audio in NSV files.
                // See samples/nsv/vlb_audio.
    
                av_log_missing_feature(ac->avctx, "More than one AAC RDB per ADTS frame", 0);
    
                ac->warned_num_aac_frames = 1;
    
            }
            push_output_configuration(ac);
    
                ac->oc[1].m4ac.chan_config = hdr_info.chan_config;
    
                if (set_default_channel_config(ac->avctx, layout_map,
                        &layout_map_tags, hdr_info.chan_config))
    
                if (output_configure(ac, layout_map, layout_map_tags,
    
                                     FFMAX(ac->oc[1].status, OC_TRIAL_FRAME), 0))
    
            } else {
    
                ac->oc[1].m4ac.chan_config = 0;
    
                /**
                 * dual mono frames in Japanese DTV can have chan_config 0
                 * WITHOUT specifying PCE.
                 *  thus, set dual mono as default.
                 */
    
                if (ac->dmono_mode && ac->oc[0].status == OC_NONE) {
    
                    layout_map_tags = 2;
                    layout_map[0][0] = layout_map[1][0] = TYPE_SCE;
                    layout_map[0][2] = layout_map[1][2] = AAC_CHANNEL_FRONT;
                    layout_map[0][1] = 0;
                    layout_map[1][1] = 1;
                    if (output_configure(ac, layout_map, layout_map_tags,
    
                                         OC_TRIAL_FRAME, 0))
    
            ac->oc[1].m4ac.sample_rate     = hdr_info.sample_rate;
            ac->oc[1].m4ac.sampling_index  = hdr_info.sampling_index;
            ac->oc[1].m4ac.object_type     = hdr_info.object_type;
            if (ac->oc[0].status != OC_LOCKED ||
                ac->oc[0].m4ac.chan_config != hdr_info.chan_config ||
                ac->oc[0].m4ac.sample_rate != hdr_info.sample_rate) {
                ac->oc[1].m4ac.sbr = -1;
                ac->oc[1].m4ac.ps  = -1;
    
            if (!hdr_info.crc_absent)
                skip_bits(gb, 16);
    
    static int aac_decode_frame_int(AVCodecContext *avctx, void *data,
    
                                    int *got_frame_ptr, GetBitContext *gb, AVPacket *avpkt)
    
        AACContext *ac = avctx->priv_data;
    
    Alex Converse's avatar
    Alex Converse committed
        ChannelElement *che = NULL, *che_prev = NULL;
        enum RawDataBlockType elem_type, elem_type_prev = TYPE_END;
    
        int err, elem_id;
    
        int samples = 0, multiplier, audio_found = 0, pce_found = 0;
    
        int is_dmono, sce_count = 0;
    
        if (show_bits(gb, 12) == 0xfff) {
            if (parse_adts_frame_header(ac, gb) < 0) {
    
                av_log(avctx, AV_LOG_ERROR, "Error decoding AAC frame header.\n");
    
                err = -1;
                goto fail;
    
            if (ac->oc[1].m4ac.sampling_index > 12) {
                av_log(ac->avctx, AV_LOG_ERROR, "invalid sampling rate index %d\n", ac->oc[1].m4ac.sampling_index);
                err = -1;
                goto fail;
    
        if (frame_configure_elements(avctx) < 0) {
            err = -1;
            goto fail;
        }
    
    
        while ((elem_type = get_bits(gb, 3)) != TYPE_END) {
            elem_id = get_bits(gb, 4);
    
            if (elem_type < TYPE_DSE) {
    
                if (!(che=get_che(ac, elem_type, elem_id))) {
                    av_log(ac->avctx, AV_LOG_ERROR, "channel element %d.%d is not allocated\n",
                           elem_type, elem_id);
    
                    err = -1;
                    goto fail;
    
            switch (elem_type) {
    
            case TYPE_SCE:
    
                err = decode_ics(ac, &che->ch[0], gb, 0, 0);
    
                err = decode_ics(ac, &che->ch[0], gb, 0, 0);
    
                err = skip_data_stream_element(ac, gb);
    
            case TYPE_PCE: {
    
                uint8_t layout_map[MAX_ELEM_ID*4][3];
                int tags;
    
                push_output_configuration(ac);
                tags = decode_pce(avctx, &ac->oc[1].m4ac, layout_map, gb);
    
                if (pce_found) {
    
                    av_log(avctx, AV_LOG_ERROR,
                           "Not evaluating a further program_config_element as this construct is dubious at best.\n");
                    pop_output_configuration(ac);
                } else {
    
                    err = output_configure(ac, layout_map, tags, OC_TRIAL_PCE, 1);
    
                    if (!err)
                        ac->oc[1].m4ac.chan_config = 0;
                    pce_found = 1;
    
                break;
            }
    
            case TYPE_FIL:
                if (elem_id == 15)
    
                    elem_id += get_bits(gb, 8) - 1;
                if (get_bits_left(gb) < 8 * elem_id) {
    
                        av_log(avctx, AV_LOG_ERROR, "TYPE_FIL: "overread_err);
    
                        err = -1;
                        goto fail;
    
                while (elem_id > 0)
    
                    elem_id -= decode_extension_payload(ac, gb, elem_id, che_prev, elem_type_prev);
    
                err = 0; /* FIXME */
                break;
    
            default:
                err = -1; /* should not happen, but keeps compiler happy */
                break;
            }
    
    
    Alex Converse's avatar
    Alex Converse committed
            che_prev       = che;
            elem_type_prev = elem_type;
    
    
            if (err)
    
                av_log(avctx, AV_LOG_ERROR, overread_err);
    
                err = -1;
                goto fail;
    
        multiplier = (ac->oc[1].m4ac.sbr == 1) ? ac->oc[1].m4ac.ext_sample_rate > ac->oc[1].m4ac.sample_rate : 0;
    
        /* for dual-mono audio (SCE + SCE) */
    
        is_dmono = ac->dmono_mode && sce_count == 2 &&
    
                   ac->oc[1].channel_layout == (AV_CH_FRONT_LEFT | AV_CH_FRONT_RIGHT);
    
    
        if (samples) {
    
            ac->frame.nb_samples = samples;
            *(AVFrame *)data = ac->frame;
    
        *got_frame_ptr = !!samples;
    
            if (ac->dmono_mode == 1)
    
                ((AVFrame *)data)->data[1] =((AVFrame *)data)->data[0];
    
            else if (ac->dmono_mode == 2)
    
                ((AVFrame *)data)->data[0] =((AVFrame *)data)->data[1];
    
        if (ac->oc[1].status && audio_found) {
            avctx->sample_rate = ac->oc[1].m4ac.sample_rate << multiplier;
            avctx->frame_size = samples;
            ac->oc[1].status = OC_LOCKED;
        }
    
        if (multiplier) {
            int side_size;
            uint32_t *side = av_packet_get_side_data(avpkt, AV_PKT_DATA_SKIP_SAMPLES, &side_size);
            if (side && side_size>=4)
                AV_WL32(side, 2*AV_RL32(side));
        }
    
    fail:
        pop_output_configuration(ac);
        return err;
    
    }
    
    static int aac_decode_frame(AVCodecContext *avctx, void *data,
    
                                int *got_frame_ptr, AVPacket *avpkt)
    
        AACContext *ac = avctx->priv_data;
    
        const uint8_t *buf = avpkt->data;
        int buf_size = avpkt->size;
        GetBitContext gb;
        int buf_consumed;
        int buf_offset;
        int err;
    
        int new_extradata_size;
        const uint8_t *new_extradata = av_packet_get_side_data(avpkt,
                                           AV_PKT_DATA_NEW_EXTRADATA,
                                           &new_extradata_size);
    
        int jp_dualmono_size;
        const uint8_t *jp_dualmono   = av_packet_get_side_data(avpkt,
                                           AV_PKT_DATA_JP_DUALMONO,
                                           &jp_dualmono_size);
    
        if (new_extradata && 0) {
    
            av_free(avctx->extradata);
            avctx->extradata = av_mallocz(new_extradata_size +
                                          FF_INPUT_BUFFER_PADDING_SIZE);
            if (!avctx->extradata)
                return AVERROR(ENOMEM);
            avctx->extradata_size = new_extradata_size;
            memcpy(avctx->extradata, new_extradata, new_extradata_size);
    
            push_output_configuration(ac);
            if (decode_audio_specific_config(ac, ac->avctx, &ac->oc[1].m4ac,
    
                                             avctx->extradata_size*8, 1) < 0) {
                pop_output_configuration(ac);
    
                return AVERROR_INVALIDDATA;
    
        ac->dmono_mode = 0;
        if (jp_dualmono && jp_dualmono_size > 0)
    
            ac->dmono_mode =  1 + *jp_dualmono;
    
        init_get_bits(&gb, buf, buf_size * 8);
    
    
        if ((err = aac_decode_frame_int(avctx, data, got_frame_ptr, &gb, avpkt)) < 0)
    
        buf_consumed = (get_bits_count(&gb) + 7) >> 3;
    
        for (buf_offset = buf_consumed; buf_offset < buf_size; buf_offset++)
            if (buf[buf_offset])
                break;
    
        return buf_size > buf_offset ? buf_consumed : buf_size;
    
    static av_cold int aac_decode_close(AVCodecContext *avctx)
    
        AACContext *ac = avctx->priv_data;
    
    Alex Converse's avatar
    Alex Converse committed
            for (type = 0; type < 4; type++) {
                if (ac->che[type][i])
                    ff_aac_sbr_ctx_close(&ac->che[type][i]->sbr);
    
                av_freep(&ac->che[type][i]);
    
        }
    
        ff_mdct_end(&ac->mdct);
        ff_mdct_end(&ac->mdct_small);
    
        ff_mdct_end(&ac->mdct_ltp);
    
        return 0;
    
    
    #define LOAS_SYNC_WORD   0x2b7       ///< 11 bits LOAS sync word
    
    struct LATMContext {
        AACContext      aac_ctx;             ///< containing AACContext
    
        int             initialized;         ///< initialized after a valid extradata was seen
    
    
        // parser data
        int             audio_mux_version_A; ///< LATM syntax version
        int             frame_length_type;   ///< 0/1 variable/fixed frame length
        int             frame_length;        ///< frame length for fixed frame length
    };
    
    static inline uint32_t latm_get_value(GetBitContext *b)
    {
        int length = get_bits(b, 2);
    
        return get_bits_long(b, (length+1)*8);
    }
    
    static int latm_decode_audio_specific_config(struct LATMContext *latmctx,
    
        AACContext *ac        = &latmctx->aac_ctx;
        AVCodecContext *avctx = ac->avctx;
    
        MPEG4AudioConfig m4ac = { 0 };
    
        int config_start_bit  = get_bits_count(gb);
        int sync_extension    = 0;
        int bits_consumed, esize;
    
        if (asclen) {
            sync_extension = 1;
            asclen         = FFMIN(asclen, get_bits_left(gb));
        } else
            asclen         = get_bits_left(gb);
    
    
        if (config_start_bit % 8) {
    
            av_log_missing_feature(latmctx->aac_ctx.avctx,
                                   "Non-byte-aligned audio-specific config", 1);
    
            return AVERROR_PATCHWELCOME;
    
        if (asclen <= 0)
            return AVERROR_INVALIDDATA;
    
        bits_consumed = decode_audio_specific_config(NULL, avctx, &m4ac,
    
                                             gb->buffer + (config_start_bit / 8),
    
                                             asclen, sync_extension);
    
        if (bits_consumed < 0)
            return AVERROR_INVALIDDATA;
    
    
        if (!latmctx->initialized ||
            ac->oc[1].m4ac.sample_rate != m4ac.sample_rate ||
    
            ac->oc[1].m4ac.chan_config != m4ac.chan_config) {
    
            if(latmctx->initialized) {
                av_log(avctx, AV_LOG_INFO, "audio config changed\n");
            } else {
                av_log(avctx, AV_LOG_INFO, "initializing latmctx\n");
            }
    
    
            esize = (bits_consumed+7) / 8;
    
    
            if (avctx->extradata_size < esize) {
    
                av_free(avctx->extradata);
                avctx->extradata = av_malloc(esize + FF_INPUT_BUFFER_PADDING_SIZE);
                if (!avctx->extradata)
                    return AVERROR(ENOMEM);
            }
    
            avctx->extradata_size = esize;
            memcpy(avctx->extradata, gb->buffer + (config_start_bit/8), esize);
            memset(avctx->extradata+esize, 0, FF_INPUT_BUFFER_PADDING_SIZE);
        }
    
        skip_bits_long(gb, bits_consumed);
    
    
        return bits_consumed;
    }
    
    static int read_stream_mux_config(struct LATMContext *latmctx,
                                      GetBitContext *gb)
    {
        int ret, audio_mux_version = get_bits(gb, 1);
    
        latmctx->audio_mux_version_A = 0;
        if (audio_mux_version)
            latmctx->audio_mux_version_A = get_bits(gb, 1);
    
        if (!latmctx->audio_mux_version_A) {
    
            if (audio_mux_version)
                latm_get_value(gb);                 // taraFullness
    
            skip_bits(gb, 1);                       // allStreamSameTimeFraming
            skip_bits(gb, 6);                       // numSubFrames
            // numPrograms
            if (get_bits(gb, 4)) {                  // numPrograms
                av_log_missing_feature(latmctx->aac_ctx.avctx,
    
                return AVERROR_PATCHWELCOME;
            }
    
    
            // for each program (which there is only one in DVB)
    
            // for each layer (which there is only one in DVB)
    
            if (get_bits(gb, 3)) {                   // numLayer
                av_log_missing_feature(latmctx->aac_ctx.avctx,
    
                return AVERROR_PATCHWELCOME;
            }
    
            // for all but first stream: use_same_config = get_bits(gb, 1);
            if (!audio_mux_version) {
    
                if ((ret = latm_decode_audio_specific_config(latmctx, gb, 0)) < 0)
    
                    return ret;
            } else {
                int ascLen = latm_get_value(gb);
    
                if ((ret = latm_decode_audio_specific_config(latmctx, gb, ascLen)) < 0)
    
                    return ret;
                ascLen -= ret;
                skip_bits_long(gb, ascLen);
            }
    
            latmctx->frame_length_type = get_bits(gb, 3);
            switch (latmctx->frame_length_type) {
            case 0:
                skip_bits(gb, 8);       // latmBufferFullness
                break;
            case 1:
                latmctx->frame_length = get_bits(gb, 9);
                break;
            case 3:
            case 4:
            case 5:
                skip_bits(gb, 6);       // CELP frame length table index
                break;
            case 6:
            case 7:
                skip_bits(gb, 1);       // HVXC frame length table index
                break;
            }
    
            if (get_bits(gb, 1)) {                  // other data
                if (audio_mux_version) {
                    latm_get_value(gb);             // other_data_bits
                } else {
                    int esc;
                    do {
                        esc = get_bits(gb, 1);
                        skip_bits(gb, 8);
                    } while (esc);
                }
            }
    
            if (get_bits(gb, 1))                     // crc present
                skip_bits(gb, 8);                    // config_crc
        }
    
        return 0;
    }
    
    static int read_payload_length_info(struct LATMContext *ctx, GetBitContext *gb)
    {
        uint8_t tmp;
    
        if (ctx->frame_length_type == 0) {
            int mux_slot_length = 0;
            do {
                tmp = get_bits(gb, 8);
                mux_slot_length += tmp;
            } while (tmp == 255);
            return mux_slot_length;
        } else if (ctx->frame_length_type == 1) {
            return ctx->frame_length;
        } else if (ctx->frame_length_type == 3 ||
                   ctx->frame_length_type == 5 ||
                   ctx->frame_length_type == 7) {
            skip_bits(gb, 2);          // mux_slot_length_coded
        }
        return 0;
    }
    
    static int read_audio_mux_element(struct LATMContext *latmctx,
                                      GetBitContext *gb)
    {
        int err;
        uint8_t use_same_mux = get_bits(gb, 1);
        if (!use_same_mux) {
            if ((err = read_stream_mux_config(latmctx, gb)) < 0)
                return err;
        } else if (!latmctx->aac_ctx.avctx->extradata) {
            av_log(latmctx->aac_ctx.avctx, AV_LOG_DEBUG,
                   "no decoder config found\n");
            return AVERROR(EAGAIN);
        }
        if (latmctx->audio_mux_version_A == 0) {
            int mux_slot_length_bytes = read_payload_length_info(latmctx, gb);
            if (mux_slot_length_bytes * 8 > get_bits_left(gb)) {
                av_log(latmctx->aac_ctx.avctx, AV_LOG_ERROR, "incomplete frame\n");
                return AVERROR_INVALIDDATA;
            } else if (mux_slot_length_bytes * 8 + 256 < get_bits_left(gb)) {
                av_log(latmctx->aac_ctx.avctx, AV_LOG_ERROR,
                       "frame length mismatch %d << %d\n",
                       mux_slot_length_bytes * 8, get_bits_left(gb));
                return AVERROR_INVALIDDATA;
            }
        }
        return 0;
    }
    
    
    
    static int latm_decode_frame(AVCodecContext *avctx, void *out,
                                 int *got_frame_ptr, AVPacket *avpkt)
    
    {
        struct LATMContext *latmctx = avctx->priv_data;
        int                 muxlength, err;
        GetBitContext       gb;
    
        init_get_bits(&gb, avpkt->data, avpkt->size * 8);
    
        // check for LOAS sync word
        if (get_bits(&gb, 11) != LOAS_SYNC_WORD)
            return AVERROR_INVALIDDATA;
    
    
        muxlength = get_bits(&gb, 13) + 3;
    
        // not enough data, the parser should have sorted this out
    
            return AVERROR_INVALIDDATA;
    
        if ((err = read_audio_mux_element(latmctx, &gb)) < 0)
            return err;
    
        if (!latmctx->initialized) {
            if (!avctx->extradata) {
    
                *got_frame_ptr = 0;
    
                return avpkt->size;
            } else {
    
                push_output_configuration(&latmctx->aac_ctx);
    
                if ((err = decode_audio_specific_config(
    
                        &latmctx->aac_ctx, avctx, &latmctx->aac_ctx.oc[1].m4ac,
                        avctx->extradata, avctx->extradata_size*8, 1)) < 0) {
                    pop_output_configuration(&latmctx->aac_ctx);
    
                    return err;
    
                latmctx->initialized = 1;
            }
        }
    
        if (show_bits(&gb, 12) == 0xfff) {
            av_log(latmctx->aac_ctx.avctx, AV_LOG_ERROR,
                   "ADTS header detected, probably as result of configuration "
                   "misparsing\n");
            return AVERROR_INVALIDDATA;
        }
    
    
        if ((err = aac_decode_frame_int(avctx, out, got_frame_ptr, &gb, avpkt)) < 0)
    
            return err;
    
        return muxlength;
    }
    
    
    static av_cold int latm_decode_init(AVCodecContext *avctx)
    
    {
        struct LATMContext *latmctx = avctx->priv_data;
    
        int ret = aac_decode_init(avctx);
    
        if (avctx->extradata_size > 0)
    
            latmctx->initialized = !ret;
    
        return ret;
    }
    
    
    
        .name            = "aac",
        .type            = AVMEDIA_TYPE_AUDIO,
    
        .id              = AV_CODEC_ID_AAC,
    
        .priv_data_size  = sizeof(AACContext),
        .init            = aac_decode_init,
        .close           = aac_decode_close,
        .decode          = aac_decode_frame,
    
        .long_name       = NULL_IF_CONFIG_SMALL("AAC (Advanced Audio Coding)"),
    
        .sample_fmts     = (const enum AVSampleFormat[]) {
    
            AV_SAMPLE_FMT_FLTP, AV_SAMPLE_FMT_NONE
    
        .capabilities    = CODEC_CAP_CHANNEL_CONF | CODEC_CAP_DR1,
    
        .channel_layouts = aac_channel_layout,
    
        .flush = flush,
    
    
    /*
        Note: This decoder filter is intended to decode LATM streams transferred
        in MPEG transport streams which only contain one program.
        To do a more complex LATM demuxing a separate LATM demuxer should be used.
    */
    
        .name            = "aac_latm",
        .type            = AVMEDIA_TYPE_AUDIO,
    
        .id              = AV_CODEC_ID_AAC_LATM,
    
        .priv_data_size  = sizeof(struct LATMContext),
        .init            = latm_decode_init,
        .close           = aac_decode_close,
        .decode          = latm_decode_frame,
    
        .long_name       = NULL_IF_CONFIG_SMALL("AAC LATM (Advanced Audio Coding LATM syntax)"),