3 * Copyright (c) 2012 Martin Storsjo
5 * This file is part of FFmpeg.
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 #include <fdk-aac/aacdecoder_lib.h>
24 #include "libavutil/channel_layout.h"
25 #include "libavutil/common.h"
26 #include "libavutil/opt.h"
31 CONCEAL_METHOD_DEFAULT = -1,
32 CONCEAL_METHOD_SPECTRAL_MUTING = 0,
33 CONCEAL_METHOD_NOISE_SUBSTITUTION = 1,
34 CONCEAL_METHOD_ENERGY_INTERPOLATION = 2,
38 typedef struct FDKAACDecContext {
40 HANDLE_AACDECODER handle;
42 enum ConcealMethod conceal_method;
45 #define OFFSET(x) offsetof(FDKAACDecContext, x)
46 #define AD AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_DECODING_PARAM
47 static const AVOption fdk_aac_dec_options[] = {
48 { "conceal", "Error concealment method", OFFSET(conceal_method), AV_OPT_TYPE_INT, { .i64 = CONCEAL_METHOD_DEFAULT }, CONCEAL_METHOD_DEFAULT, CONCEAL_METHOD_NB - 1, AD, "conceal" },
49 { "default", "Default", 0, AV_OPT_TYPE_CONST, { .i64 = CONCEAL_METHOD_DEFAULT }, INT_MIN, INT_MAX, AD, "conceal" },
50 { "spectral", "Spectral muting", 0, AV_OPT_TYPE_CONST, { .i64 = CONCEAL_METHOD_SPECTRAL_MUTING }, INT_MIN, INT_MAX, AD, "conceal" },
51 { "noise", "Noise Substitution", 0, AV_OPT_TYPE_CONST, { .i64 = CONCEAL_METHOD_NOISE_SUBSTITUTION }, INT_MIN, INT_MAX, AD, "conceal" },
52 { "energy", "Energy Interpolation", 0, AV_OPT_TYPE_CONST, { .i64 = CONCEAL_METHOD_ENERGY_INTERPOLATION }, INT_MIN, INT_MAX, AD, "conceal" },
56 static const AVClass fdk_aac_dec_class = {
57 "libfdk-aac decoder", av_default_item_name, fdk_aac_dec_options, LIBAVUTIL_VERSION_INT
60 static int get_stream_info(AVCodecContext *avctx)
62 FDKAACDecContext *s = avctx->priv_data;
63 CStreamInfo *info = aacDecoder_GetStreamInfo(s->handle);
64 int channel_counts[9] = { 0 };
66 uint64_t ch_layout = 0;
69 av_log(avctx, AV_LOG_ERROR, "Unable to get stream info\n");
70 return AVERROR_UNKNOWN;
73 if (info->sampleRate <= 0) {
74 av_log(avctx, AV_LOG_ERROR, "Stream info not initialized\n");
75 return AVERROR_UNKNOWN;
77 avctx->sample_rate = info->sampleRate;
78 avctx->frame_size = info->frameSize;
80 for (i = 0; i < info->numChannels; i++) {
81 AUDIO_CHANNEL_TYPE ctype = info->pChannelType[i];
82 if (ctype <= ACT_NONE || ctype > ACT_TOP) {
83 av_log(avctx, AV_LOG_WARNING, "unknown channel type\n");
86 channel_counts[ctype]++;
88 av_log(avctx, AV_LOG_DEBUG,
89 "%d channels - front:%d side:%d back:%d lfe:%d top:%d\n",
91 channel_counts[ACT_FRONT], channel_counts[ACT_SIDE],
92 channel_counts[ACT_BACK], channel_counts[ACT_LFE],
93 channel_counts[ACT_FRONT_TOP] + channel_counts[ACT_SIDE_TOP] +
94 channel_counts[ACT_BACK_TOP] + channel_counts[ACT_TOP]);
96 switch (channel_counts[ACT_FRONT]) {
98 ch_layout |= AV_CH_LAYOUT_STEREO | AV_CH_FRONT_LEFT_OF_CENTER |
99 AV_CH_FRONT_RIGHT_OF_CENTER;
102 ch_layout |= AV_CH_LAYOUT_STEREO | AV_CH_FRONT_CENTER;
105 ch_layout |= AV_CH_LAYOUT_STEREO;
108 ch_layout |= AV_CH_FRONT_CENTER;
111 av_log(avctx, AV_LOG_WARNING,
112 "unsupported number of front channels: %d\n",
113 channel_counts[ACT_FRONT]);
117 if (channel_counts[ACT_SIDE] > 0) {
118 if (channel_counts[ACT_SIDE] == 2) {
119 ch_layout |= AV_CH_SIDE_LEFT | AV_CH_SIDE_RIGHT;
121 av_log(avctx, AV_LOG_WARNING,
122 "unsupported number of side channels: %d\n",
123 channel_counts[ACT_SIDE]);
127 if (channel_counts[ACT_BACK] > 0) {
128 switch (channel_counts[ACT_BACK]) {
130 ch_layout |= AV_CH_BACK_LEFT | AV_CH_BACK_RIGHT | AV_CH_BACK_CENTER;
133 ch_layout |= AV_CH_BACK_LEFT | AV_CH_BACK_RIGHT;
136 ch_layout |= AV_CH_BACK_CENTER;
139 av_log(avctx, AV_LOG_WARNING,
140 "unsupported number of back channels: %d\n",
141 channel_counts[ACT_BACK]);
146 if (channel_counts[ACT_LFE] > 0) {
147 if (channel_counts[ACT_LFE] == 1) {
148 ch_layout |= AV_CH_LOW_FREQUENCY;
150 av_log(avctx, AV_LOG_WARNING,
151 "unsupported number of LFE channels: %d\n",
152 channel_counts[ACT_LFE]);
157 av_get_channel_layout_nb_channels(ch_layout) != info->numChannels) {
158 av_log(avctx, AV_LOG_WARNING, "unsupported channel configuration\n");
162 avctx->channel_layout = 0;
164 avctx->channel_layout = ch_layout;
166 avctx->channels = info->numChannels;
171 static av_cold int fdk_aac_decode_close(AVCodecContext *avctx)
173 FDKAACDecContext *s = avctx->priv_data;
176 aacDecoder_Close(s->handle);
181 static av_cold int fdk_aac_decode_init(AVCodecContext *avctx)
183 FDKAACDecContext *s = avctx->priv_data;
184 AAC_DECODER_ERROR err;
186 s->handle = aacDecoder_Open(avctx->extradata_size ? TT_MP4_RAW : TT_MP4_ADTS, 1);
188 av_log(avctx, AV_LOG_ERROR, "Error opening decoder\n");
189 return AVERROR_UNKNOWN;
192 if (avctx->extradata_size) {
193 if ((err = aacDecoder_ConfigRaw(s->handle, &avctx->extradata,
194 &avctx->extradata_size)) != AAC_DEC_OK) {
195 av_log(avctx, AV_LOG_ERROR, "Unable to set extradata\n");
196 return AVERROR_INVALIDDATA;
200 if (s->conceal_method != CONCEAL_METHOD_DEFAULT) {
201 if ((err = aacDecoder_SetParam(s->handle, AAC_CONCEAL_METHOD,
202 s->conceal_method)) != AAC_DEC_OK) {
203 av_log(avctx, AV_LOG_ERROR, "Unable to set error concealment method\n");
204 return AVERROR_UNKNOWN;
208 avctx->sample_fmt = AV_SAMPLE_FMT_S16;
213 static int fdk_aac_decode_frame(AVCodecContext *avctx, void *data,
214 int *got_frame_ptr, AVPacket *avpkt)
216 FDKAACDecContext *s = avctx->priv_data;
217 AVFrame *frame = data;
219 AAC_DECODER_ERROR err;
220 UINT valid = avpkt->size;
221 uint8_t *buf, *tmpptr = NULL;
224 err = aacDecoder_Fill(s->handle, &avpkt->data, &avpkt->size, &valid);
225 if (err != AAC_DEC_OK) {
226 av_log(avctx, AV_LOG_ERROR, "aacDecoder_Fill() failed: %x\n", err);
227 return AVERROR_INVALIDDATA;
230 if (s->initialized) {
231 frame->nb_samples = avctx->frame_size;
232 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
234 buf = frame->extended_data[0];
235 buf_size = avctx->channels * frame->nb_samples *
236 av_get_bytes_per_sample(avctx->sample_fmt);
238 buf_size = 50 * 1024;
239 buf = tmpptr = av_malloc(buf_size);
241 return AVERROR(ENOMEM);
244 err = aacDecoder_DecodeFrame(s->handle, (INT_PCM *) buf, buf_size, 0);
245 if (err == AAC_DEC_NOT_ENOUGH_BITS) {
246 ret = avpkt->size - valid;
249 if (err != AAC_DEC_OK) {
250 av_log(avctx, AV_LOG_ERROR,
251 "aacDecoder_DecodeFrame() failed: %x\n", err);
252 ret = AVERROR_UNKNOWN;
256 if (!s->initialized) {
257 if ((ret = get_stream_info(avctx)) < 0)
260 frame->nb_samples = avctx->frame_size;
264 frame->nb_samples = avctx->frame_size;
265 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
267 memcpy(frame->extended_data[0], tmpptr,
268 avctx->channels * avctx->frame_size *
269 av_get_bytes_per_sample(avctx->sample_fmt));
273 ret = avpkt->size - valid;
280 static av_cold void fdk_aac_decode_flush(AVCodecContext *avctx)
282 FDKAACDecContext *s = avctx->priv_data;
283 AAC_DECODER_ERROR err;
288 if ((err = aacDecoder_SetParam(s->handle,
289 AAC_TPDEC_CLEAR_BUFFER, 1)) != AAC_DEC_OK)
290 av_log(avctx, AV_LOG_WARNING, "failed to clear buffer when flushing\n");
293 AVCodec ff_libfdk_aac_decoder = {
294 .name = "libfdk_aac",
295 .long_name = NULL_IF_CONFIG_SMALL("Fraunhofer FDK AAC"),
296 .type = AVMEDIA_TYPE_AUDIO,
297 .id = AV_CODEC_ID_AAC,
298 .priv_data_size = sizeof(FDKAACDecContext),
299 .init = fdk_aac_decode_init,
300 .decode = fdk_aac_decode_frame,
301 .close = fdk_aac_decode_close,
302 .flush = fdk_aac_decode_flush,
303 .capabilities = CODEC_CAP_DR1,
304 .priv_class = &fdk_aac_dec_class,