1 /*
2 * AAC decoder wrapper
3 * Copyright (c) 2012 Martin Storsjo
4 *
5 * This file is part of FFmpeg.
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
27
28 #ifdef AACDECODER_LIB_VL0
29 #define FDKDEC_VER_AT_LEAST(vl0, vl1) \
30 ((AACDECODER_LIB_VL0 > vl0) || \
31 (AACDECODER_LIB_VL0 == vl0 && AACDECODER_LIB_VL1 >= vl1))
32 #else
33 #define FDKDEC_VER_AT_LEAST(vl0, vl1) 0
34 #endif
35
36 #if !FDKDEC_VER_AT_LEAST(2, 5) // < 2.5.10
37 #define AAC_PCM_MAX_OUTPUT_CHANNELS AAC_PCM_OUTPUT_CHANNELS
38 #endif
39
45 };
46
62
63
64 #define DMX_ANC_BUFFSIZE 128
65 #define DECODER_MAX_CHANNELS 8
66 #define DECODER_BUFFSIZE 2048 * sizeof(INT_PCM)
67
68 #define OFFSET(x) offsetof(FDKAACDecContext, x)
69 #define AD AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_DECODING_PARAM
75 { "drc_boost", "Dynamic Range Control: boost, where [0] is none and [127] is max boost",
77 { "drc_cut", "Dynamic Range Control: attenuation factor, where [0] is none and [127] is max compression",
79 { "drc_level", "Dynamic Range Control: reference level, quantized to 0.25dB steps where [0] is 0dB and [127] is -31.75dB, -1 for auto, and -2 for disabled",
81 { "drc_heavy", "Dynamic Range Control: heavy compression, where [1] is on (RF mode) and [0] is off",
83 #if FDKDEC_VER_AT_LEAST(2, 5) // 2.5.10
84 { "level_limit", "Signal level limiting",
86 #endif
87 #if FDKDEC_VER_AT_LEAST(3, 0) // 3.0.0
88 { "drc_effect","Dynamic Range Control: effect type, where e.g. [0] is none and [6] is general",
90 #endif
92 };
93
99 };
100
102 {
104 CStreamInfo *
info = aacDecoder_GetStreamInfo(
s->handle);
107 uint64_t ch_layout = 0;
108
112 }
113
114 if (
info->sampleRate <= 0) {
117 }
120 #if FDKDEC_VER_AT_LEAST(2, 5) // 2.5.10
121 s->output_delay =
info->outputDelay;
122 #endif
123
124 for (
i = 0;
i <
info->numChannels;
i++) {
125 AUDIO_CHANNEL_TYPE ctype =
info->pChannelType[
i];
128 break;
129 }
131 }
133 "%d channels - front:%d side:%d back:%d lfe:%d top:%d\n",
139
141 case 4:
144 break;
145 case 3:
147 break;
148 case 2:
150 break;
151 case 1:
153 break;
154 default:
156 "unsupported number of front channels: %d\n",
158 ch_error = 1;
159 break;
160 }
164 } else {
166 "unsupported number of side channels: %d\n",
168 ch_error = 1;
169 }
170 }
173 case 3:
175 break;
176 case 2:
178 break;
179 case 1:
181 break;
182 default:
184 "unsupported number of back channels: %d\n",
186 ch_error = 1;
187 break;
188 }
189 }
193 } else {
195 "unsupported number of LFE channels: %d\n",
197 ch_error = 1;
198 }
199 }
200 if (!ch_error &&
203 ch_error = 1;
204 }
205 if (ch_error)
207 else
209
211
212 return 0;
213 }
214
216 {
218
220 aacDecoder_Close(
s->handle);
223
224 return 0;
225 }
226
228 {
230 AAC_DECODER_ERROR err;
231
232 s->handle = aacDecoder_Open(avctx->
extradata_size ? TT_MP4_RAW : TT_MP4_ADTS, 1);
236 }
237
239 if ((err = aacDecoder_ConfigRaw(
s->handle, &avctx->
extradata,
243 }
244 }
245
246 if ((err = aacDecoder_SetParam(
s->handle, AAC_CONCEAL_METHOD,
247 s->conceal_method)) != AAC_DEC_OK) {
250 }
251
254 int downmix_channels = -1;
255
259 downmix_channels = 2;
260 break;
262 downmix_channels = 1;
263 break;
264 default:
266 break;
267 }
268
269 if (downmix_channels != -1) {
271 downmix_channels) != AAC_DEC_OK) {
273 } else {
275 if (!
s->anc_buffer) {
278 }
280 av_log(avctx,
AV_LOG_ERROR,
"Unable to register downmix ancillary buffer in the decoder\n");
282 }
283 }
284 }
285 }
286
287 if (
s->drc_boost != -1) {
288 if (aacDecoder_SetParam(
s->handle, AAC_DRC_BOOST_FACTOR,
s->drc_boost) != AAC_DEC_OK) {
291 }
292 }
293
294 if (
s->drc_cut != -1) {
295 if (aacDecoder_SetParam(
s->handle, AAC_DRC_ATTENUATION_FACTOR,
s->drc_cut) != AAC_DEC_OK) {
298 }
299 }
300
301 if (
s->drc_level != -1) {
302 // This option defaults to -1, i.e. not calling
303 // aacDecoder_SetParam(AAC_DRC_REFERENCE_LEVEL) at all, which defaults
304 // to the level from DRC metadata, if available. The user can set
305 // -drc_level -2, which calls aacDecoder_SetParam(
306 // AAC_DRC_REFERENCE_LEVEL) with a negative value, which then
307 // explicitly disables the feature.
308 if (aacDecoder_SetParam(
s->handle, AAC_DRC_REFERENCE_LEVEL,
s->drc_level) != AAC_DEC_OK) {
311 }
312 }
313
314 if (
s->drc_heavy != -1) {
315 if (aacDecoder_SetParam(
s->handle, AAC_DRC_HEAVY_COMPRESSION,
s->drc_heavy) != AAC_DEC_OK) {
318 }
319 }
320
321 #if FDKDEC_VER_AT_LEAST(2, 5) // 2.5.10
322 // Setting this parameter to -1 enables the auto behaviour in the library.
323 if (aacDecoder_SetParam(
s->handle, AAC_PCM_LIMITER_ENABLE,
s->level_limit) != AAC_DEC_OK) {
324 av_log(avctx,
AV_LOG_ERROR,
"Unable to set in signal level limiting in the decoder\n");
326 }
327 #endif
328
329 #if FDKDEC_VER_AT_LEAST(3, 0) // 3.0.0
330 if (
s->drc_effect != -1) {
331 if (aacDecoder_SetParam(
s->handle, AAC_UNIDRC_SET_EFFECT,
s->drc_effect) != AAC_DEC_OK) {
334 }
335 }
336 #endif
337
339
341 s->decoder_buffer =
av_malloc(
s->decoder_buffer_size);
342 if (!
s->decoder_buffer)
344
345 return 0;
346 }
347
349 int *got_frame_ptr,
AVPacket *avpkt)
350 {
354 AAC_DECODER_ERROR err;
355 UINT valid = avpkt->
size;
356
357 err = aacDecoder_Fill(
s->handle, &avpkt->
data, &avpkt->
size, &valid);
358 if (err != AAC_DEC_OK) {
361 }
362
363 err = aacDecoder_DecodeFrame(
s->handle, (INT_PCM *)
s->decoder_buffer,
s->decoder_buffer_size /
sizeof(INT_PCM), 0);
364 if (err == AAC_DEC_NOT_ENOUGH_BITS) {
366 goto end;
367 }
368 if (err != AAC_DEC_OK) {
370 "aacDecoder_DecodeFrame() failed: %x\n", err);
372 goto end;
373 }
374
376 goto end;
378
380 goto end;
381
386
387 memcpy(
frame->extended_data[0],
s->decoder_buffer,
390
391 *got_frame_ptr = 1;
393
394 end:
396 }
397
399 {
401 AAC_DECODER_ERROR err;
402
404 return;
405
406 if ((err = aacDecoder_SetParam(
s->handle,
407 AAC_TPDEC_CLEAR_BUFFER, 1)) != AAC_DEC_OK)
409 }
410
412 .
name =
"libfdk_aac",
425 .wrapper_name = "libfdk",
426 };