3be65155b5f5484d5a5e81b321e720b6aa31ed80
[libav.git] / libavcodec / libfdk-aacdec.c
1 /*
2 * AAC decoder wrapper
3 * Copyright (c) 2012 Martin Storsjo
4 *
5 * This file is part of Libav.
6 *
7 * Permission to use, copy, modify, and/or distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
10 *
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 */
19
20 #include <fdk-aac/aacdecoder_lib.h>
21
22 #include "libavutil/channel_layout.h"
23 #include "libavutil/common.h"
24 #include "libavutil/opt.h"
25 #include "avcodec.h"
26 #include "internal.h"
27
28 /* The version macro is introduced the same time as the setting enum was
29 * changed, so this check should suffice. */
30 #ifndef AACDECODER_LIB_VL0
31 #define AAC_PCM_MAX_OUTPUT_CHANNELS AAC_PCM_OUTPUT_CHANNELS
32 #endif
33
34 enum ConcealMethod {
35 CONCEAL_METHOD_SPECTRAL_MUTING = 0,
36 CONCEAL_METHOD_NOISE_SUBSTITUTION = 1,
37 CONCEAL_METHOD_ENERGY_INTERPOLATION = 2,
38 CONCEAL_METHOD_NB,
39 };
40
41 typedef struct FDKAACDecContext {
42 const AVClass *class;
43 HANDLE_AACDECODER handle;
44 uint8_t *decoder_buffer;
45 int decoder_buffer_size;
46 uint8_t *anc_buffer;
47 enum ConcealMethod conceal_method;
48 int drc_level;
49 int drc_boost;
50 int drc_heavy;
51 int drc_cut;
52 int level_limit;
53 } FDKAACDecContext;
54
55
56 #define DMX_ANC_BUFFSIZE 128
57 #define DECODER_MAX_CHANNELS 8
58 #define DECODER_BUFFSIZE 2048 * sizeof(INT_PCM)
59
60 #define OFFSET(x) offsetof(FDKAACDecContext, x)
61 #define AD AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_DECODING_PARAM
62 static const AVOption fdk_aac_dec_options[] = {
63 { "conceal", "Error concealment method", OFFSET(conceal_method), AV_OPT_TYPE_INT, { .i64 = CONCEAL_METHOD_NOISE_SUBSTITUTION }, CONCEAL_METHOD_SPECTRAL_MUTING, CONCEAL_METHOD_NB - 1, AD, "conceal" },
64 { "spectral", "Spectral muting", 0, AV_OPT_TYPE_CONST, { .i64 = CONCEAL_METHOD_SPECTRAL_MUTING }, INT_MIN, INT_MAX, AD, "conceal" },
65 { "noise", "Noise Substitution", 0, AV_OPT_TYPE_CONST, { .i64 = CONCEAL_METHOD_NOISE_SUBSTITUTION }, INT_MIN, INT_MAX, AD, "conceal" },
66 { "energy", "Energy Interpolation", 0, AV_OPT_TYPE_CONST, { .i64 = CONCEAL_METHOD_ENERGY_INTERPOLATION }, INT_MIN, INT_MAX, AD, "conceal" },
67 { "drc_boost", "Dynamic Range Control: boost, where [0] is none and [127] is max boost",
68 OFFSET(drc_boost), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 127, AD, NULL },
69 { "drc_cut", "Dynamic Range Control: attenuation factor, where [0] is none and [127] is max compression",
70 OFFSET(drc_cut), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 127, AD, NULL },
71 { "drc_level", "Dynamic Range Control: reference level, quantized to 0.25dB steps where [0] is 0dB and [127] is -31.75dB",
72 OFFSET(drc_level), AV_OPT_TYPE_INT, { .i64 = -1}, -1, 127, AD, NULL },
73 { "drc_heavy", "Dynamic Range Control: heavy compression, where [1] is on (RF mode) and [0] is off",
74 OFFSET(drc_heavy), AV_OPT_TYPE_INT, { .i64 = -1}, -1, 1, AD, NULL },
75 #ifdef AACDECODER_LIB_VL0
76 { "level_limit", "Signal level limiting", OFFSET(level_limit), AV_OPT_TYPE_INT, { .i64 = 0 }, -1, 1, AD },
77 #endif
78 { NULL }
79 };
80
81 static const AVClass fdk_aac_dec_class = {
82 .class_name = "libfdk-aac decoder",
83 .item_name = av_default_item_name,
84 .option = fdk_aac_dec_options,
85 .version = LIBAVUTIL_VERSION_INT
86 };
87
88 static int get_stream_info(AVCodecContext *avctx)
89 {
90 FDKAACDecContext *s = avctx->priv_data;
91 CStreamInfo *info = aacDecoder_GetStreamInfo(s->handle);
92 int channel_counts[0x24] = { 0 };
93 int i, ch_error = 0;
94 uint64_t ch_layout = 0;
95
96 if (!info) {
97 av_log(avctx, AV_LOG_ERROR, "Unable to get stream info\n");
98 return AVERROR_UNKNOWN;
99 }
100
101 if (info->sampleRate <= 0) {
102 av_log(avctx, AV_LOG_ERROR, "Stream info not initialized\n");
103 return AVERROR_UNKNOWN;
104 }
105 avctx->sample_rate = info->sampleRate;
106 avctx->frame_size = info->frameSize;
107
108 for (i = 0; i < info->numChannels; i++) {
109 AUDIO_CHANNEL_TYPE ctype = info->pChannelType[i];
110 if (ctype <= ACT_NONE || ctype >= FF_ARRAY_ELEMS(channel_counts)) {
111 av_log(avctx, AV_LOG_WARNING, "unknown channel type\n");
112 break;
113 }
114 channel_counts[ctype]++;
115 }
116 av_log(avctx, AV_LOG_DEBUG,
117 "%d channels - front:%d side:%d back:%d lfe:%d top:%d\n",
118 info->numChannels,
119 channel_counts[ACT_FRONT], channel_counts[ACT_SIDE],
120 channel_counts[ACT_BACK], channel_counts[ACT_LFE],
121 channel_counts[ACT_FRONT_TOP] + channel_counts[ACT_SIDE_TOP] +
122 channel_counts[ACT_BACK_TOP] + channel_counts[ACT_TOP]);
123
124 switch (channel_counts[ACT_FRONT]) {
125 case 4:
126 ch_layout |= AV_CH_LAYOUT_STEREO | AV_CH_FRONT_LEFT_OF_CENTER |
127 AV_CH_FRONT_RIGHT_OF_CENTER;
128 break;
129 case 3:
130 ch_layout |= AV_CH_LAYOUT_STEREO | AV_CH_FRONT_CENTER;
131 break;
132 case 2:
133 ch_layout |= AV_CH_LAYOUT_STEREO;
134 break;
135 case 1:
136 ch_layout |= AV_CH_FRONT_CENTER;
137 break;
138 default:
139 av_log(avctx, AV_LOG_WARNING,
140 "unsupported number of front channels: %d\n",
141 channel_counts[ACT_FRONT]);
142 ch_error = 1;
143 break;
144 }
145 if (channel_counts[ACT_SIDE] > 0) {
146 if (channel_counts[ACT_SIDE] == 2) {
147 ch_layout |= AV_CH_SIDE_LEFT | AV_CH_SIDE_RIGHT;
148 } else {
149 av_log(avctx, AV_LOG_WARNING,
150 "unsupported number of side channels: %d\n",
151 channel_counts[ACT_SIDE]);
152 ch_error = 1;
153 }
154 }
155 if (channel_counts[ACT_BACK] > 0) {
156 switch (channel_counts[ACT_BACK]) {
157 case 3:
158 ch_layout |= AV_CH_BACK_LEFT | AV_CH_BACK_RIGHT | AV_CH_BACK_CENTER;
159 break;
160 case 2:
161 ch_layout |= AV_CH_BACK_LEFT | AV_CH_BACK_RIGHT;
162 break;
163 case 1:
164 ch_layout |= AV_CH_BACK_CENTER;
165 break;
166 default:
167 av_log(avctx, AV_LOG_WARNING,
168 "unsupported number of back channels: %d\n",
169 channel_counts[ACT_BACK]);
170 ch_error = 1;
171 break;
172 }
173 }
174 if (channel_counts[ACT_LFE] > 0) {
175 if (channel_counts[ACT_LFE] == 1) {
176 ch_layout |= AV_CH_LOW_FREQUENCY;
177 } else {
178 av_log(avctx, AV_LOG_WARNING,
179 "unsupported number of LFE channels: %d\n",
180 channel_counts[ACT_LFE]);
181 ch_error = 1;
182 }
183 }
184 if (!ch_error &&
185 av_get_channel_layout_nb_channels(ch_layout) != info->numChannels) {
186 av_log(avctx, AV_LOG_WARNING, "unsupported channel configuration\n");
187 ch_error = 1;
188 }
189 if (ch_error)
190 avctx->channel_layout = 0;
191 else
192 avctx->channel_layout = ch_layout;
193
194 avctx->channels = info->numChannels;
195
196 return 0;
197 }
198
199 static av_cold int fdk_aac_decode_close(AVCodecContext *avctx)
200 {
201 FDKAACDecContext *s = avctx->priv_data;
202
203 if (s->handle)
204 aacDecoder_Close(s->handle);
205 av_free(s->decoder_buffer);
206 av_free(s->anc_buffer);
207
208 return 0;
209 }
210
211 static av_cold int fdk_aac_decode_init(AVCodecContext *avctx)
212 {
213 FDKAACDecContext *s = avctx->priv_data;
214 AAC_DECODER_ERROR err;
215
216 s->handle = aacDecoder_Open(avctx->extradata_size ? TT_MP4_RAW : TT_MP4_ADTS, 1);
217 if (!s->handle) {
218 av_log(avctx, AV_LOG_ERROR, "Error opening decoder\n");
219 return AVERROR_UNKNOWN;
220 }
221
222 if (avctx->extradata_size) {
223 if ((err = aacDecoder_ConfigRaw(s->handle, &avctx->extradata,
224 &avctx->extradata_size)) != AAC_DEC_OK) {
225 av_log(avctx, AV_LOG_ERROR, "Unable to set extradata\n");
226 return AVERROR_INVALIDDATA;
227 }
228 }
229
230 if ((err = aacDecoder_SetParam(s->handle, AAC_CONCEAL_METHOD,
231 s->conceal_method)) != AAC_DEC_OK) {
232 av_log(avctx, AV_LOG_ERROR, "Unable to set error concealment method\n");
233 return AVERROR_UNKNOWN;
234 }
235
236 if (avctx->request_channel_layout > 0 &&
237 avctx->request_channel_layout != AV_CH_LAYOUT_NATIVE) {
238 int downmix_channels = -1;
239
240 switch (avctx->request_channel_layout) {
241 case AV_CH_LAYOUT_STEREO:
242 case AV_CH_LAYOUT_STEREO_DOWNMIX:
243 downmix_channels = 2;
244 break;
245 case AV_CH_LAYOUT_MONO:
246 downmix_channels = 1;
247 break;
248 default:
249 av_log(avctx, AV_LOG_WARNING, "Invalid request_channel_layout\n");
250 break;
251 }
252
253 if (downmix_channels != -1) {
254 if (aacDecoder_SetParam(s->handle, AAC_PCM_MAX_OUTPUT_CHANNELS,
255 downmix_channels) != AAC_DEC_OK) {
256 av_log(avctx, AV_LOG_WARNING, "Unable to set output channels in the decoder\n");
257 } else {
258 s->anc_buffer = av_malloc(DMX_ANC_BUFFSIZE);
259 if (!s->anc_buffer) {
260 av_log(avctx, AV_LOG_ERROR, "Unable to allocate ancillary buffer for the decoder\n");
261 return AVERROR(ENOMEM);
262 }
263 if (aacDecoder_AncDataInit(s->handle, s->anc_buffer, DMX_ANC_BUFFSIZE)) {
264 av_log(avctx, AV_LOG_ERROR, "Unable to register downmix ancillary buffer in the decoder\n");
265 return AVERROR_UNKNOWN;
266 }
267 }
268 }
269 }
270
271 if (s->drc_boost != -1) {
272 if (aacDecoder_SetParam(s->handle, AAC_DRC_BOOST_FACTOR, s->drc_boost) != AAC_DEC_OK) {
273 av_log(avctx, AV_LOG_ERROR, "Unable to set DRC boost factor in the decoder\n");
274 return AVERROR_UNKNOWN;
275 }
276 }
277
278 if (s->drc_cut != -1) {
279 if (aacDecoder_SetParam(s->handle, AAC_DRC_ATTENUATION_FACTOR, s->drc_cut) != AAC_DEC_OK) {
280 av_log(avctx, AV_LOG_ERROR, "Unable to set DRC attenuation factor in the decoder\n");
281 return AVERROR_UNKNOWN;
282 }
283 }
284
285 if (s->drc_level != -1) {
286 if (aacDecoder_SetParam(s->handle, AAC_DRC_REFERENCE_LEVEL, s->drc_level) != AAC_DEC_OK) {
287 av_log(avctx, AV_LOG_ERROR, "Unable to set DRC reference level in the decoder\n");
288 return AVERROR_UNKNOWN;
289 }
290 }
291
292 if (s->drc_heavy != -1) {
293 if (aacDecoder_SetParam(s->handle, AAC_DRC_HEAVY_COMPRESSION, s->drc_heavy) != AAC_DEC_OK) {
294 av_log(avctx, AV_LOG_ERROR, "Unable to set DRC heavy compression in the decoder\n");
295 return AVERROR_UNKNOWN;
296 }
297 }
298
299 #ifdef AACDECODER_LIB_VL0
300 if (aacDecoder_SetParam(s->handle, AAC_PCM_LIMITER_ENABLE, s->level_limit) != AAC_DEC_OK) {
301 av_log(avctx, AV_LOG_ERROR, "Unable to set in signal level limiting in the decoder\n");
302 return AVERROR_UNKNOWN;
303 }
304 #endif
305
306 avctx->sample_fmt = AV_SAMPLE_FMT_S16;
307
308 s->decoder_buffer_size = DECODER_BUFFSIZE * DECODER_MAX_CHANNELS;
309 s->decoder_buffer = av_malloc(s->decoder_buffer_size);
310 if (!s->decoder_buffer)
311 return AVERROR(ENOMEM);
312
313 return 0;
314 }
315
316 static int fdk_aac_decode_frame(AVCodecContext *avctx, void *data,
317 int *got_frame_ptr, AVPacket *avpkt)
318 {
319 FDKAACDecContext *s = avctx->priv_data;
320 AVFrame *frame = data;
321 int ret;
322 AAC_DECODER_ERROR err;
323 UINT valid = avpkt->size;
324
325 err = aacDecoder_Fill(s->handle, &avpkt->data, &avpkt->size, &valid);
326 if (err != AAC_DEC_OK) {
327 av_log(avctx, AV_LOG_ERROR, "aacDecoder_Fill() failed: %x\n", err);
328 return AVERROR_INVALIDDATA;
329 }
330
331 err = aacDecoder_DecodeFrame(s->handle, (INT_PCM *) s->decoder_buffer, s->decoder_buffer_size / sizeof(INT_PCM), 0);
332 if (err == AAC_DEC_NOT_ENOUGH_BITS) {
333 ret = avpkt->size - valid;
334 goto end;
335 }
336 if (err != AAC_DEC_OK) {
337 av_log(avctx, AV_LOG_ERROR,
338 "aacDecoder_DecodeFrame() failed: %x\n", err);
339 ret = AVERROR_UNKNOWN;
340 goto end;
341 }
342
343 if ((ret = get_stream_info(avctx)) < 0)
344 goto end;
345 frame->nb_samples = avctx->frame_size;
346
347 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) {
348 av_log(avctx, AV_LOG_ERROR, "ff_get_buffer() failed\n");
349 goto end;
350 }
351 memcpy(frame->extended_data[0], s->decoder_buffer,
352 avctx->channels * avctx->frame_size *
353 av_get_bytes_per_sample(avctx->sample_fmt));
354
355 *got_frame_ptr = 1;
356 ret = avpkt->size - valid;
357
358 end:
359 return ret;
360 }
361
362 static av_cold void fdk_aac_decode_flush(AVCodecContext *avctx)
363 {
364 FDKAACDecContext *s = avctx->priv_data;
365 AAC_DECODER_ERROR err;
366
367 if (!s->handle)
368 return;
369
370 if ((err = aacDecoder_SetParam(s->handle,
371 AAC_TPDEC_CLEAR_BUFFER, 1)) != AAC_DEC_OK)
372 av_log(avctx, AV_LOG_WARNING, "failed to clear buffer when flushing\n");
373 }
374
375 AVCodec ff_libfdk_aac_decoder = {
376 .name = "libfdk_aac",
377 .long_name = NULL_IF_CONFIG_SMALL("Fraunhofer FDK AAC"),
378 .type = AVMEDIA_TYPE_AUDIO,
379 .id = AV_CODEC_ID_AAC,
380 .priv_data_size = sizeof(FDKAACDecContext),
381 .init = fdk_aac_decode_init,
382 .decode = fdk_aac_decode_frame,
383 .close = fdk_aac_decode_close,
384 .flush = fdk_aac_decode_flush,
385 .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_CHANNEL_CONF,
386 .priv_class = &fdk_aac_dec_class,
387 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE |
388 FF_CODEC_CAP_INIT_CLEANUP,
389 .wrapper_name = "libfdk",
390 };