diff --git a/libavcodec/libfdk-aacdec.c b/libavcodec/libfdk-aacdec.c index 624d57959c..65bd36a448 100644 --- a/libavcodec/libfdk-aacdec.c +++ b/libavcodec/libfdk-aacdec.c @@ -36,9 +36,16 @@ typedef struct FDKAACDecContext { const AVClass *class; HANDLE_AACDECODER handle; int initialized; + uint8_t *decoder_buffer; + uint8_t *anc_buffer; enum ConcealMethod conceal_method; } FDKAACDecContext; + +#define DMX_ANC_BUFFSIZE 128 +#define DECODER_MAX_CHANNELS 6 +#define DECODER_BUFFSIZE 2048 * sizeof(INT_PCM) + #define OFFSET(x) offsetof(FDKAACDecContext, x) #define AD AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_DECODING_PARAM static const AVOption fdk_aac_dec_options[] = { @@ -170,6 +177,8 @@ static av_cold int fdk_aac_decode_close(AVCodecContext *avctx) if (s->handle) aacDecoder_Close(s->handle); + av_free(s->decoder_buffer); + av_free(s->anc_buffer); return 0; } @@ -178,6 +187,7 @@ static av_cold int fdk_aac_decode_init(AVCodecContext *avctx) { FDKAACDecContext *s = avctx->priv_data; AAC_DECODER_ERROR err; + int ret; s->handle = aacDecoder_Open(avctx->extradata_size ? TT_MP4_RAW : TT_MP4_ADTS, 1); if (!s->handle) { @@ -199,9 +209,49 @@ static av_cold int fdk_aac_decode_init(AVCodecContext *avctx) return AVERROR_UNKNOWN; } + if (avctx->request_channel_layout > 0 && + avctx->request_channel_layout != AV_CH_LAYOUT_NATIVE) { + int downmix_channels = -1; + + switch (avctx->request_channel_layout) { + case AV_CH_LAYOUT_STEREO: + case AV_CH_LAYOUT_STEREO_DOWNMIX: + downmix_channels = 2; + break; + case AV_CH_LAYOUT_MONO: + downmix_channels = 1; + break; + default: + av_log(avctx, AV_LOG_WARNING, "Invalid request_channel_layout\n"); + break; + } + + if (downmix_channels != -1) { + if (aacDecoder_SetParam(s->handle, AAC_PCM_OUTPUT_CHANNELS, + downmix_channels) != AAC_DEC_OK) { + av_log(avctx, AV_LOG_WARNING, "Unable to set output channels in the decoder\n"); + } else { + s->anc_buffer = av_malloc(DMX_ANC_BUFFSIZE); + if (!s->anc_buffer) { + av_log(avctx, AV_LOG_ERROR, "Unable to allocate ancillary buffer for the decoder\n"); + ret = AVERROR(ENOMEM); + goto fail; + } + if (aacDecoder_AncDataInit(s->handle, s->anc_buffer, DMX_ANC_BUFFSIZE)) { + av_log(avctx, AV_LOG_ERROR, "Unable to register downmix ancillary buffer in the decoder\n"); + ret = AVERROR_UNKNOWN; + goto fail; + } + } + } + } + avctx->sample_fmt = AV_SAMPLE_FMT_S16; return 0; +fail: + fdk_aac_decode_close(avctx); + return ret; } static int fdk_aac_decode_frame(AVCodecContext *avctx, void *data, @@ -225,14 +275,24 @@ static int fdk_aac_decode_frame(AVCodecContext *avctx, void *data, frame->nb_samples = avctx->frame_size; if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) return ret; - buf = frame->extended_data[0]; - buf_size = avctx->channels * frame->nb_samples * - av_get_bytes_per_sample(avctx->sample_fmt); + + if (s->anc_buffer) { + buf_size = DECODER_BUFFSIZE * DECODER_MAX_CHANNELS; + buf = s->decoder_buffer; + } else { + buf = frame->extended_data[0]; + buf_size = avctx->channels * frame->nb_samples * + av_get_bytes_per_sample(avctx->sample_fmt); + } } else { - buf_size = 50 * 1024; - buf = tmpptr = av_malloc(buf_size); - if (!buf) + buf_size = DECODER_BUFFSIZE * DECODER_MAX_CHANNELS; + + if (!s->decoder_buffer) + s->decoder_buffer = av_malloc(buf_size); + if (!s->decoder_buffer) return AVERROR(ENOMEM); + + buf = tmpptr = s->decoder_buffer; } err = aacDecoder_DecodeFrame(s->handle, (INT_PCM *) buf, buf_size, 0); @@ -258,16 +318,20 @@ static int fdk_aac_decode_frame(AVCodecContext *avctx, void *data, frame->nb_samples = avctx->frame_size; if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) goto end; - memcpy(frame->extended_data[0], tmpptr, + } + if (s->decoder_buffer) { + memcpy(frame->extended_data[0], buf, avctx->channels * avctx->frame_size * av_get_bytes_per_sample(avctx->sample_fmt)); + + if (!s->anc_buffer) + av_freep(&s->decoder_buffer); } *got_frame_ptr = 1; ret = avpkt->size - valid; end: - av_free(tmpptr); return ret; } diff --git a/libavcodec/version.h b/libavcodec/version.h index 1efe55c621..3e193e5d1b 100644 --- a/libavcodec/version.h +++ b/libavcodec/version.h @@ -30,7 +30,7 @@ #define LIBAVCODEC_VERSION_MAJOR 56 #define LIBAVCODEC_VERSION_MINOR 8 -#define LIBAVCODEC_VERSION_MICRO 100 +#define LIBAVCODEC_VERSION_MICRO 101 #define LIBAVCODEC_VERSION_INT AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \ LIBAVCODEC_VERSION_MINOR, \