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 /* 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
39 };
40
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
67 { "drc_boost", "Dynamic Range Control: boost, where [0] is none and [127] is max boost",
69 { "drc_cut", "Dynamic Range Control: attenuation factor, where [0] is none and [127] is max compression",
71 { "drc_level", "Dynamic Range Control: reference level, quantized to 0.25dB steps where [0] is 0dB and [127] is -31.75dB",
73 { "drc_heavy", "Dynamic Range Control: heavy compression, where [1] is on (RF mode) and [0] is off",
75 #ifdef AACDECODER_LIB_VL0
77 #endif
79 };
80
86 };
87
89 {
91 CStreamInfo *info = aacDecoder_GetStreamInfo(s->
handle);
93 int i, ch_error = 0;
94 uint64_t ch_layout = 0;
95
96 if (!info) {
99 }
100
101 if (info->sampleRate <= 0) {
104 }
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)) {
112 break;
113 }
114 channel_counts[ctype]++;
115 }
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:
128 break;
129 case 3:
131 break;
132 case 2:
134 break;
135 case 1:
137 break;
138 default:
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) {
148 } else {
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:
159 break;
160 case 2:
162 break;
163 case 1:
165 break;
166 default:
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) {
177 } else {
179 "unsupported number of LFE channels: %d\n",
180 channel_counts[ACT_LFE]);
181 ch_error = 1;
182 }
183 }
184 if (!ch_error &&
187 ch_error = 1;
188 }
189 if (ch_error)
191 else
193
194 avctx->
channels = info->numChannels;
195
196 return 0;
197 }
198
200 {
202
204 aacDecoder_Close(s->
handle);
207
208 return 0;
209 }
210
212 {
214 AAC_DECODER_ERROR err;
215
220 }
221
227 }
228 }
229
230 if ((err = aacDecoder_SetParam(s->
handle, AAC_CONCEAL_METHOD,
234 }
235
238 int downmix_channels = -1;
239
243 downmix_channels = 2;
244 break;
246 downmix_channels = 1;
247 break;
248 default:
250 break;
251 }
252
253 if (downmix_channels != -1) {
255 downmix_channels) != AAC_DEC_OK) {
257 } else {
262 }
264 av_log(avctx,
AV_LOG_ERROR,
"Unable to register downmix ancillary buffer in the decoder\n");
266 }
267 }
268 }
269 }
270
272 if (aacDecoder_SetParam(s->
handle, AAC_DRC_BOOST_FACTOR, s->
drc_boost) != AAC_DEC_OK) {
275 }
276 }
277
279 if (aacDecoder_SetParam(s->
handle, AAC_DRC_ATTENUATION_FACTOR, s->
drc_cut) != AAC_DEC_OK) {
282 }
283 }
284
286 if (aacDecoder_SetParam(s->
handle, AAC_DRC_REFERENCE_LEVEL, s->
drc_level) != AAC_DEC_OK) {
289 }
290 }
291
293 if (aacDecoder_SetParam(s->
handle, AAC_DRC_HEAVY_COMPRESSION, s->
drc_heavy) != AAC_DEC_OK) {
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");
303 }
304 #endif
305
307
312
313 return 0;
314 }
315
317 int *got_frame_ptr,
AVPacket *avpkt)
318 {
321 int ret;
322 AAC_DECODER_ERROR err;
324
325 err = aacDecoder_Fill(s->
handle, &avpkt->
data, &avpkt->
size, &valid);
326 if (err != AAC_DEC_OK) {
329 }
330
332 if (err == AAC_DEC_NOT_ENOUGH_BITS) {
333 ret = avpkt->
size - valid;
335 }
336 if (err != AAC_DEC_OK) {
338 "aacDecoder_DecodeFrame() failed: %x\n", err);
341 }
342
346
349
353
354 *got_frame_ptr = 1;
355 ret = avpkt->
size - valid;
356
358 return ret;
359 }
360
362 {
364 AAC_DECODER_ERROR err;
365
367 return;
368
369 if ((err = aacDecoder_SetParam(s->
handle,
370 AAC_TPDEC_CLEAR_BUFFER, 1)) != AAC_DEC_OK)
372 }
373
375 .
name =
"libfdk_aac",
385 .priv_class = &fdk_aac_dec_class,
388 .wrapper_name = "libfdk",
389 };
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
This structure describes decoded (raw) audio or video data.
ptrdiff_t const GLvoid * data
static void flush(AVCodecContext *avctx)
#define AV_LOG_WARNING
Something somehow does not look correct.
#define LIBAVUTIL_VERSION_INT
static av_cold int init(AVCodecContext *avctx)
const char * av_default_item_name(void *ptr)
Return the context name.
#define AV_CODEC_CAP_CHANNEL_CONF
Codec should fill in channel configuration and samplerate instead of container.
#define AV_CH_LAYOUT_STEREO
static void decode(AVCodecContext *dec_ctx, AVPacket *pkt, AVFrame *frame, FILE *outfile)
int av_get_channel_layout_nb_channels(uint64_t channel_layout)
Return the number of channels in the channel layout.
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
#define FF_CODEC_CAP_INIT_THREADSAFE
The codec does not modify any global variables in the init function, allowing to call the init functi...
enum AVSampleFormat sample_fmt
audio sample format
static av_cold int end(AVCodecContext *avctx)
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
#define AV_CH_LOW_FREQUENCY
static int get_stream_info(AVCodecContext *avctx)
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
static av_cold void fdk_aac_decode_flush(AVCodecContext *avctx)
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
AVCodec ff_libfdk_aac_decoder
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
const char * name
Name of the codec implementation.
uint64_t channel_layout
Audio channel layout.
#define AAC_PCM_MAX_OUTPUT_CHANNELS
audio channel layout utility functions
#define AV_CH_LAYOUT_STEREO_DOWNMIX
static const AVOption fdk_aac_dec_options[]
#define AV_CH_FRONT_LEFT_OF_CENTER
#define AV_CH_FRONT_CENTER
static const uint8_t channel_counts[7]
#define FF_ARRAY_ELEMS(a)
#define AV_CH_FRONT_RIGHT_OF_CENTER
int frame_size
Number of samples per channel in an audio frame.
Libavcodec external API header.
int sample_rate
samples per second
static int fdk_aac_decode_frame(AVCodecContext *avctx, void *data, int *got_frame_ptr, AVPacket *avpkt)
static const AVClass fdk_aac_dec_class
#define AV_CH_LAYOUT_NATIVE
Channel mask value used for AVCodecContext.request_channel_layout to indicate that the user requests ...
main external API structure.
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Describe the class of an AVClass context structure.
#define AV_CH_BACK_CENTER
int av_get_bytes_per_sample(enum AVSampleFormat sample_fmt)
Return number of bytes per sample.
common internal api header.
static av_cold int fdk_aac_decode_close(AVCodecContext *avctx)
common internal and external API header
static av_cold int fdk_aac_decode_init(AVCodecContext *avctx)
#define AVERROR_UNKNOWN
Unknown error, typically from an external library.
int channels
number of audio channels
#define DECODER_MAX_CHANNELS
uint8_t ** extended_data
pointers to the data planes/channels.
#define AV_CH_LAYOUT_MONO
uint64_t request_channel_layout
Request decoder to use this channel layout if it can (0 for default)
This structure stores compressed data.
int nb_samples
number of audio samples (per channel) described by this frame
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.