diff options
Diffstat (limited to 'libavcodec/aacdec.c')
-rw-r--r-- | libavcodec/aacdec.c | 286 |
1 files changed, 239 insertions, 47 deletions
diff --git a/libavcodec/aacdec.c b/libavcodec/aacdec.c index 70fbb534fb..365df2117d 100644 --- a/libavcodec/aacdec.c +++ b/libavcodec/aacdec.c @@ -8,20 +8,20 @@ * Copyright (c) 2008-2010 Paul Kendall <paul@kcbbs.gen.nz> * Copyright (c) 2010 Janne Grunau <janne-libav@jannau.net> * - * This file is part of Libav. + * This file is part of FFmpeg. * - * Libav is free software; you can redistribute it and/or + * FFmpeg is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * - * Libav is distributed in the hope that it will be useful, + * FFmpeg is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public - * License along with Libav; if not, write to the Free Software + * License along with FFmpeg; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ @@ -74,6 +74,7 @@ * N SinuSoidal Coding (Transient, Sinusoid, Noise) * Y Parametric Stereo * N Direct Stream Transfer + * Y Enhanced AAC Low Delay (ER AAC ELD) * * Note: - HE AAC v1 comprises LC AAC with Spectral Band Replication. * - HE AAC v2 comprises LC AAC with Spectral Band Replication and @@ -81,6 +82,7 @@ */ #include "libavutil/float_dsp.h" +#include "libavutil/opt.h" #include "avcodec.h" #include "internal.h" #include "get_bits.h" @@ -108,12 +110,18 @@ #if ARCH_ARM # include "arm/aac.h" +#elif ARCH_MIPS +# include "mips/aacdec_mips.h" #endif static VLC vlc_scalefactors; static VLC vlc_spectral[11]; -static const char overread_err[] = "Input buffer exhausted before END element found\n"; +static int output_configure(AACContext *ac, + uint8_t layout_map[MAX_ELEM_ID*4][3], int tags, + enum OCStatus oc_type, int get_new_frame); + +#define overread_err "Input buffer exhausted before END element found\n" static int count_channels(uint8_t (*layout)[3], int tags) { @@ -130,7 +138,7 @@ static int count_channels(uint8_t (*layout)[3], int tags) /** * Check for the channel element in the current channel position configuration. * If it exists, make sure the appropriate element is allocated and map the - * channel order to match the internal Libav channel layout. + * channel order to match the internal FFmpeg channel layout. * * @param che_pos current channel position configuration * @param type channel element type @@ -152,6 +160,10 @@ static av_cold int che_configure(AACContext *ac, ff_aac_sbr_ctx_init(ac, &ac->che[type][id]->sbr); } if (type != TYPE_CCE) { + if (*channels >= MAX_CHANNELS - (type == TYPE_CPE || (type == TYPE_SCE && ac->oc[1].m4ac.ps == 1))) { + av_log(ac->avctx, AV_LOG_ERROR, "Too many channels\n"); + return AVERROR_INVALIDDATA; + } ac->output_element[(*channels)++] = &ac->che[type][id]->ch[0]; if (type == TYPE_CPE || (type == TYPE_SCE && ac->oc[1].m4ac.ps == 1)) { @@ -185,10 +197,8 @@ static int frame_configure_elements(AVCodecContext *avctx) /* get output buffer */ av_frame_unref(ac->frame); ac->frame->nb_samples = 2048; - if ((ret = ff_get_buffer(avctx, ac->frame, 0)) < 0) { - av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n"); + if ((ret = ff_get_buffer(avctx, ac->frame, 0)) < 0) return ret; - } /* map output channel pointers to AVFrame data */ for (ch = 0; ch < avctx->channels; ch++) { @@ -427,6 +437,8 @@ static void pop_output_configuration(AACContext *ac) { ac->oc[1] = ac->oc[0]; ac->avctx->channels = ac->oc[1].channels; ac->avctx->channel_layout = ac->oc[1].channel_layout; + output_configure(ac, ac->oc[1].layout_map, ac->oc[1].layout_map_tags, + ac->oc[1].status, 0); } } @@ -472,7 +484,8 @@ static int output_configure(AACContext *ac, } memcpy(ac->tag_che_map, ac->che, 4 * MAX_ELEM_ID * sizeof(ac->che[0][0])); - avctx->channel_layout = ac->oc[1].channel_layout = layout; + if (layout) avctx->channel_layout = layout; + ac->oc[1].channel_layout = layout; avctx->channels = ac->oc[1].channels = channels; ac->oc[1].status = oc_type; @@ -484,6 +497,23 @@ static int output_configure(AACContext *ac, return 0; } +static void flush(AVCodecContext *avctx) +{ + AACContext *ac= avctx->priv_data; + int type, i, j; + + for (type = 3; type >= 0; type--) { + for (i = 0; i < MAX_ELEM_ID; i++) { + ChannelElement *che = ac->che[type][i]; + if (che) { + for (j = 0; j <= 1; j++) { + memset(che->ch[j].saved, 0, sizeof(che->ch[j].saved)); + } + } + } + } +} + /** * Set up channel positions based on a default channel configuration * as specified in table 1.17. @@ -504,6 +534,25 @@ static int set_default_channel_config(AVCodecContext *avctx, *tags = tags_per_config[channel_config]; memcpy(layout_map, aac_channel_layout_map[channel_config - 1], *tags * sizeof(*layout_map)); + + /* + * AAC specification has 7.1(wide) as a default layout for 8-channel streams. + * However, at least Nero AAC encoder encodes 7.1 streams using the default + * channel config 7, mapping the side channels of the original audio stream + * to the second AAC_CHANNEL_FRONT pair in the AAC stream. Similarly, e.g. FAAD + * decodes the second AAC_CHANNEL_FRONT pair as side channels, therefore decoding + * the incorrect streams as if they were correct (and as the encoder intended). + * + * As actual intended 7.1(wide) streams are very rare, default to assuming a + * 7.1 layout was intended. + */ + if (channel_config == 7 && avctx->strict_std_compliance < FF_COMPLIANCE_STRICT) { + av_log(avctx, AV_LOG_INFO, "Assuming an incorrectly encoded 7.1 channel layout" + " instead of a spec-compliant 7.1(wide) layout, use -strict %d to decode" + " according to the specification instead.\n", FF_COMPLIANCE_STRICT); + layout_map[2][2] = AAC_CHANNEL_SIDE; + } + return 0; } @@ -521,6 +570,8 @@ static ChannelElement *get_che(AACContext *ac, int type, int elem_id) int layout_map_tags; push_output_configuration(ac); + av_log(ac->avctx, AV_LOG_DEBUG, "mono with CPE\n"); + if (set_default_channel_config(ac->avctx, layout_map, &layout_map_tags, 2) < 0) return NULL; @@ -538,6 +589,8 @@ static ChannelElement *get_che(AACContext *ac, int type, int elem_id) int layout_map_tags; push_output_configuration(ac); + av_log(ac->avctx, AV_LOG_DEBUG, "stereo with SCE\n"); + if (set_default_channel_config(ac->avctx, layout_map, &layout_map_tags, 1) < 0) return NULL; @@ -624,6 +677,8 @@ static void decode_channel_map(uint8_t layout_map[][3], case AAC_CHANNEL_LFE: syn_ele = TYPE_LFE; break; + default: + av_assert0(0); } layout_map[0][0] = syn_ele; layout_map[0][1] = get_bits(gb, 4); @@ -669,6 +724,10 @@ static int decode_pce(AVCodecContext *avctx, MPEG4AudioConfig *m4ac, if (get_bits1(gb)) skip_bits(gb, 3); // mixdown_coeff_index and pseudo_surround + if (get_bits_left(gb) < 4 * (num_front + num_side + num_back + num_lfe + num_assoc_data + num_cc)) { + av_log(avctx, AV_LOG_ERROR, "decode_pce: " overread_err); + return -1; + } decode_channel_map(layout_map , AAC_CHANNEL_FRONT, gb, num_front); tags = num_front; decode_channel_map(layout_map + tags, AAC_CHANNEL_SIDE, gb, num_side); @@ -688,7 +747,7 @@ static int decode_pce(AVCodecContext *avctx, MPEG4AudioConfig *m4ac, /* comment field, first byte is length */ comment_len = get_bits(gb, 8) * 8; if (get_bits_left(gb) < comment_len) { - av_log(avctx, AV_LOG_ERROR, overread_err); + av_log(avctx, AV_LOG_ERROR, "decode_pce: " overread_err); return AVERROR_INVALIDDATA; } skip_bits_long(gb, comment_len); @@ -862,9 +921,9 @@ static int decode_audio_specific_config(AACContext *ac, GetBitContext gb; int i, ret; - av_dlog(avctx, "extradata size %d\n", avctx->extradata_size); - for (i = 0; i < avctx->extradata_size; i++) - av_dlog(avctx, "%02x ", avctx->extradata[i]); + av_dlog(avctx, "audio specific config size %d\n", bit_size >> 3); + for (i = 0; i < bit_size >> 3; i++) + av_dlog(avctx, "%02x ", data[i]); av_dlog(avctx, "\n"); if ((ret = init_get_bits(&gb, data, bit_size)) < 0) @@ -928,7 +987,7 @@ static int decode_audio_specific_config(AACContext *ac, * * @return Returns a 32-bit pseudorandom integer */ -static av_always_inline int lcg_random(int previous_val) +static av_always_inline int lcg_random(unsigned previous_val) { union { unsigned u; int s; } v = { previous_val * 1664525u + 1013904223 }; return v.s; @@ -982,6 +1041,8 @@ static void reset_predictor_group(PredictorState *ps, int group_num) sizeof(ff_aac_spectral_codes[num][0]), \ size); +static void aacdec_init(AACContext *ac); + static av_cold int aac_decode_init(AVCodecContext *avctx) { AACContext *ac = avctx->priv_data; @@ -990,6 +1051,8 @@ static av_cold int aac_decode_init(AVCodecContext *avctx) ac->avctx = avctx; ac->oc[1].m4ac.sample_rate = avctx->sample_rate; + aacdec_init(ac); + avctx->sample_fmt = AV_SAMPLE_FMT_FLTP; if (avctx->extradata_size > 0) { @@ -1028,6 +1091,11 @@ static av_cold int aac_decode_init(AVCodecContext *avctx) } } + if (avctx->channels > MAX_CHANNELS) { + av_log(avctx, AV_LOG_ERROR, "Too many channels\n"); + return AVERROR_INVALIDDATA; + } + AAC_INIT_VLC_STATIC( 0, 304); AAC_INIT_VLC_STATIC( 1, 270); AAC_INIT_VLC_STATIC( 2, 550); @@ -1088,7 +1156,7 @@ static int skip_data_stream_element(AACContext *ac, GetBitContext *gb) align_get_bits(gb); if (get_bits_left(gb) < 8 * count) { - av_log(ac->avctx, AV_LOG_ERROR, overread_err); + av_log(ac->avctx, AV_LOG_ERROR, "skip_data_stream_element: "overread_err); return AVERROR_INVALIDDATA; } skip_bits_long(gb, 8 * count); @@ -1192,13 +1260,13 @@ static int decode_ics_info(AACContext *ac, IndividualChannelStream *ics, if (ics->predictor_present) { if (aot == AOT_AAC_MAIN) { if (decode_prediction(ac, ics, gb)) { - return AVERROR_INVALIDDATA; + goto fail; } } else if (aot == AOT_AAC_LC || aot == AOT_ER_AAC_LC) { av_log(ac->avctx, AV_LOG_ERROR, "Prediction is not allowed in AAC-LC.\n"); - return AVERROR_INVALIDDATA; + goto fail; } else { if (aot == AOT_ER_AAC_LD) { av_log(ac->avctx, AV_LOG_ERROR, @@ -1216,10 +1284,13 @@ static int decode_ics_info(AACContext *ac, IndividualChannelStream *ics, "Number of scalefactor bands in group (%d) " "exceeds limit (%d).\n", ics->max_sfb, ics->num_swb); - return AVERROR_INVALIDDATA; + goto fail; } return 0; +fail: + ics->max_sfb = 0; + return AVERROR_INVALIDDATA; } /** @@ -1250,7 +1321,7 @@ static int decode_band_types(AACContext *ac, enum BandType band_type[120], sect_len_incr = get_bits(gb, bits); sect_end += sect_len_incr; if (get_bits_left(gb) < 0) { - av_log(ac->avctx, AV_LOG_ERROR, overread_err); + av_log(ac->avctx, AV_LOG_ERROR, "decode_band_types: "overread_err); return AVERROR_INVALIDDATA; } if (sect_end > ics->max_sfb) { @@ -1422,7 +1493,7 @@ static void decode_mid_side_stereo(ChannelElement *cpe, GetBitContext *gb, idx++) cpe->ms_mask[idx] = get_bits1(gb); } else if (ms_present == 2) { - memset(cpe->ms_mask, 1, cpe->ch[0].ics.num_window_groups * cpe->ch[0].ics.max_sfb * sizeof(cpe->ms_mask[0])); + memset(cpe->ms_mask, 1, sizeof(cpe->ms_mask[0]) * cpe->ch[0].ics.num_window_groups * cpe->ch[0].ics.max_sfb); } } @@ -2156,6 +2227,32 @@ static int decode_dynamic_range(DynamicRangeControl *che_drc, 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); + + return 0; +} + /** * Decode extension data (incomplete); reference: table 4.51. * @@ -2199,6 +2296,8 @@ static int decode_extension_payload(AACContext *ac, GetBitContext *gb, int cnt, 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: @@ -2221,7 +2320,7 @@ static void apply_tns(float coef[1024], TemporalNoiseShaping *tns, int w, filt, m, i; int bottom, top, order, start, end, size, inc; float lpc[TNS_MAX_ORDER]; - float tmp[TNS_MAX_ORDER + 1]; + float tmp[TNS_MAX_ORDER+1]; for (w = 0; w < ics->num_windows; w++) { bottom = ics->num_swb; @@ -2313,10 +2412,10 @@ static void apply_ltp(AACContext *ac, SingleChannelElement *sce) 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); + ac->windowing_and_mdct_ltp(ac, predFreq, predTime, &sce->ics); if (sce->tns.present) - apply_tns(predFreq, &sce->tns, &sce->ics, 0); + ac->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]) @@ -2463,7 +2562,7 @@ static void imdct_and_windowing_eld(AACContext *ac, SingleChannelElement *sce) // Inverse transform, mapped to the conventional IMDCT by // Chivukula, R.K.; Reznik, Y.A.; Devarajan, V., // "Efficient algorithms for MPEG-4 AAC-ELD, AAC-LD and AAC-LC filterbanks," - // Audio, Language and Image Processing, 2008. ICALIP 2008. International Conference on + // International Conference on Audio, Language and Image Processing, ICALIP 2008. // URL: http://ieeexplore.ieee.org/stamp/stamp.jsp?tp=&arnumber=4590245&isnumber=4589950 for (i = 0; i < n2; i+=2) { float temp; @@ -2609,7 +2708,7 @@ static void spectral_to_sample(AACContext *ac) imdct_and_window = imdct_and_windowing_eld; break; default: - imdct_and_window = imdct_and_windowing; + imdct_and_window = ac->imdct_and_windowing; } for (type = 3; type >= 0; type--) { for (i = 0; i < MAX_ELEM_ID; i++) { @@ -2620,25 +2719,25 @@ static void spectral_to_sample(AACContext *ac) 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]); + ac->apply_ltp(ac, &che->ch[0]); if (che->ch[1].ics.ltp.present && type == TYPE_CPE) - apply_ltp(ac, &che->ch[1]); + ac->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); + ac->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); + ac->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); if (type != TYPE_CCE || che->coup.coupling_point == AFTER_IMDCT) { imdct_and_window(ac, &che->ch[0]); if (ac->oc[1].m4ac.object_type == AOT_AAC_LTP) - update_ltp(ac, &che->ch[0]); + ac->update_ltp(ac, &che->ch[0]); if (type == TYPE_CPE) { imdct_and_window(ac, &che->ch[1]); if (ac->oc[1].m4ac.object_type == AOT_AAC_LTP) - update_ltp(ac, &che->ch[1]); + ac->update_ltp(ac, &che->ch[1]); } if (ac->oc[1].m4ac.sbr > 0) { ff_sbr_apply(ac, &che->sbr, type, che->ch[0].ret, che->ch[1].ret); @@ -2660,10 +2759,12 @@ static int parse_adts_frame_header(AACContext *ac, GetBitContext *gb) size = avpriv_aac_parse_header(gb, &hdr_info); if (size > 0) { - if (hdr_info.num_aac_frames != 1) { + 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. avpriv_report_missing_feature(ac->avctx, "More than one AAC RDB per ADTS frame"); - return AVERROR_PATCHWELCOME; + ac->warned_num_aac_frames = 1; } push_output_configuration(ac); if (hdr_info.chan_config) { @@ -2679,6 +2780,21 @@ static int parse_adts_frame_header(AACContext *ac, GetBitContext *gb) return ret; } 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)) + return -7; + } } ac->oc[1].m4ac.sample_rate = hdr_info.sample_rate; ac->oc[1].m4ac.sampling_index = hdr_info.sampling_index; @@ -2760,13 +2876,14 @@ static int aac_decode_er_frame(AVCodecContext *avctx, void *data, } static int aac_decode_frame_int(AVCodecContext *avctx, void *data, - int *got_frame_ptr, GetBitContext *gb) + int *got_frame_ptr, GetBitContext *gb, AVPacket *avpkt) { AACContext *ac = avctx->priv_data; 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; ac->frame = data; @@ -2809,6 +2926,7 @@ static int aac_decode_frame_int(AVCodecContext *avctx, void *data, case TYPE_SCE: err = decode_ics(ac, &che->ch[0], gb, 0, 0); audio_found = 1; + sce_count++; break; case TYPE_CPE: @@ -2841,9 +2959,10 @@ static int aac_decode_frame_int(AVCodecContext *avctx, void *data, 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; @@ -2853,7 +2972,7 @@ static int aac_decode_frame_int(AVCodecContext *avctx, void *data, 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, overread_err); + av_log(avctx, AV_LOG_ERROR, "TYPE_FIL: "overread_err); err = AVERROR_INVALIDDATA; goto fail; } @@ -2884,17 +3003,35 @@ static int aac_decode_frame_int(AVCodecContext *avctx, void *data, multiplier = (ac->oc[1].m4ac.sbr == 1) ? ac->oc[1].m4ac.ext_sample_rate > ac->oc[1].m4ac.sample_rate : 0; samples <<= multiplier; + /* 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; + else + av_frame_unref(ac->frame); *got_frame_ptr = !!samples; + if (is_dmono) { + 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; + const uint8_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)); + } return 0; fail: pop_output_configuration(ac); @@ -2915,8 +3052,12 @@ static int aac_decode_frame(AVCodecContext *avctx, void *data, 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) { + if (new_extradata && 0) { av_free(avctx->extradata); avctx->extradata = av_mallocz(new_extradata_size + FF_INPUT_BUFFER_PADDING_SIZE); @@ -2933,6 +3074,15 @@ static int aac_decode_frame(AVCodecContext *avctx, void *data, } } + ac->dmono_mode = 0; + if (jp_dualmono && jp_dualmono_size > 0) + ac->dmono_mode = 1 + *jp_dualmono; + if (ac->force_dmono_mode >= 0) + ac->dmono_mode = ac->force_dmono_mode; + + if (INT_MAX / 8 <= buf_size) + return AVERROR_INVALIDDATA; + if ((err = init_get_bits(&gb, buf, buf_size * 8)) < 0) return err; @@ -2944,7 +3094,7 @@ static int aac_decode_frame(AVCodecContext *avctx, void *data, err = aac_decode_er_frame(avctx, data, got_frame_ptr, &gb); break; default: - err = aac_decode_frame_int(avctx, data, got_frame_ptr, &gb); + err = aac_decode_frame_int(avctx, data, got_frame_ptr, &gb, avpkt); } if (err < 0) return err; @@ -2982,7 +3132,7 @@ static av_cold int aac_decode_close(AVCodecContext *avctx) struct LATMContext { AACContext aac_ctx; ///< containing AACContext - int initialized; ///< initilized after a valid extradata was seen + int initialized; ///< initialized after a valid extradata was seen // parser data int audio_mux_version_A; ///< LATM syntax version @@ -3027,10 +3177,15 @@ static int latm_decode_audio_specific_config(struct LATMContext *latmctx, if (bits_consumed < 0) return AVERROR_INVALIDDATA; - if (ac->oc[1].m4ac.sample_rate != m4ac.sample_rate || + if (!latmctx->initialized || + ac->oc[1].m4ac.sample_rate != m4ac.sample_rate || ac->oc[1].m4ac.chan_config != m4ac.chan_config) { - av_log(avctx, AV_LOG_INFO, "audio config changed\n"); + if(latmctx->initialized) { + av_log(avctx, AV_LOG_INFO, "audio config changed\n"); + } else { + av_log(avctx, AV_LOG_DEBUG, "initializing latmctx\n"); + } latmctx->initialized = 0; esize = (bits_consumed+7) / 8; @@ -3073,9 +3228,9 @@ static int read_stream_mux_config(struct LATMContext *latmctx, return AVERROR_PATCHWELCOME; } - // for each program (which there is only on in DVB) + // for each program (which there is only one in DVB) - // for each layer (which there is only on in DVB) + // for each layer (which there is only one in DVB) if (get_bits(gb, 3)) { // numLayer avpriv_request_sample(latmctx->aac_ctx.avctx, "Multiple layers"); return AVERROR_PATCHWELCOME; @@ -3188,7 +3343,7 @@ static int latm_decode_frame(AVCodecContext *avctx, void *out, int muxlength, err; GetBitContext gb; - if ((err = init_get_bits(&gb, avpkt->data, avpkt->size * 8)) < 0) + if ((err = init_get_bits8(&gb, avpkt->data, avpkt->size)) < 0) return err; // check for LOAS sync word @@ -3196,7 +3351,7 @@ static int latm_decode_frame(AVCodecContext *avctx, void *out, return AVERROR_INVALIDDATA; muxlength = get_bits(&gb, 13) + 3; - // not enough data, the parser should have sorted this + // not enough data, the parser should have sorted this out if (muxlength > avpkt->size) return AVERROR_INVALIDDATA; @@ -3226,7 +3381,7 @@ static int latm_decode_frame(AVCodecContext *avctx, void *out, return AVERROR_INVALIDDATA; } - if ((err = aac_decode_frame_int(avctx, out, got_frame_ptr, &gb)) < 0) + if ((err = aac_decode_frame_int(avctx, out, got_frame_ptr, &gb, avpkt)) < 0) return err; return muxlength; @@ -3243,6 +3398,40 @@ static av_cold int latm_decode_init(AVCodecContext *avctx) return ret; } +static void aacdec_init(AACContext *c) +{ + c->imdct_and_windowing = imdct_and_windowing; + c->apply_ltp = apply_ltp; + c->apply_tns = apply_tns; + c->windowing_and_mdct_ltp = windowing_and_mdct_ltp; + c->update_ltp = update_ltp; + + if(ARCH_MIPS) + ff_aacdec_init_mips(c); +} +/** + * AVOptions for Japanese DTV specific extensions (ADTS only) + */ +#define AACDEC_FLAGS AV_OPT_FLAG_DECODING_PARAM | AV_OPT_FLAG_AUDIO_PARAM +static const AVOption options[] = { + {"dual_mono_mode", "Select the channel to decode for dual mono", + offsetof(AACContext, force_dmono_mode), AV_OPT_TYPE_INT, {.i64=-1}, -1, 2, + AACDEC_FLAGS, "dual_mono_mode"}, + + {"auto", "autoselection", 0, AV_OPT_TYPE_CONST, {.i64=-1}, INT_MIN, INT_MAX, AACDEC_FLAGS, "dual_mono_mode"}, + {"main", "Select Main/Left channel", 0, AV_OPT_TYPE_CONST, {.i64= 1}, INT_MIN, INT_MAX, AACDEC_FLAGS, "dual_mono_mode"}, + {"sub" , "Select Sub/Right channel", 0, AV_OPT_TYPE_CONST, {.i64= 2}, INT_MIN, INT_MAX, AACDEC_FLAGS, "dual_mono_mode"}, + {"both", "Select both channels", 0, AV_OPT_TYPE_CONST, {.i64= 0}, INT_MIN, INT_MAX, AACDEC_FLAGS, "dual_mono_mode"}, + + {NULL}, +}; + +static const AVClass aac_decoder_class = { + .class_name = "AAC decoder", + .item_name = av_default_item_name, + .option = options, + .version = LIBAVUTIL_VERSION_INT, +}; AVCodec ff_aac_decoder = { .name = "aac", @@ -3258,6 +3447,8 @@ AVCodec ff_aac_decoder = { }, .capabilities = CODEC_CAP_CHANNEL_CONF | CODEC_CAP_DR1, .channel_layouts = aac_channel_layout, + .flush = flush, + .priv_class = &aac_decoder_class, }; /* @@ -3279,4 +3470,5 @@ AVCodec ff_aac_latm_decoder = { }, .capabilities = CODEC_CAP_CHANNEL_CONF | CODEC_CAP_DR1, .channel_layouts = aac_channel_layout, + .flush = flush, }; |