Libav
libfdk-aacdec.c
Go to the documentation of this file.
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 
23 #include "libavutil/common.h"
24 #include "libavutil/opt.h"
25 #include "avcodec.h"
26 #include "internal.h"
27 
33 };
34 
35 typedef struct FDKAACDecContext {
36  const AVClass *class;
37  HANDLE_AACDECODER handle;
41 
42 #define OFFSET(x) offsetof(FDKAACDecContext, x)
43 #define AD AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_DECODING_PARAM
44 static const AVOption fdk_aac_dec_options[] = {
45  { "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" },
46  { "spectral", "Spectral muting", 0, AV_OPT_TYPE_CONST, { .i64 = CONCEAL_METHOD_SPECTRAL_MUTING }, INT_MIN, INT_MAX, AD, "conceal" },
47  { "noise", "Noise Substitution", 0, AV_OPT_TYPE_CONST, { .i64 = CONCEAL_METHOD_NOISE_SUBSTITUTION }, INT_MIN, INT_MAX, AD, "conceal" },
48  { "energy", "Energy Interpolation", 0, AV_OPT_TYPE_CONST, { .i64 = CONCEAL_METHOD_ENERGY_INTERPOLATION }, INT_MIN, INT_MAX, AD, "conceal" },
49  { NULL }
50 };
51 
52 static const AVClass fdk_aac_dec_class = {
54 };
55 
56 static int get_stream_info(AVCodecContext *avctx)
57 {
58  FDKAACDecContext *s = avctx->priv_data;
59  CStreamInfo *info = aacDecoder_GetStreamInfo(s->handle);
60  int channel_counts[9] = { 0 };
61  int i, ch_error = 0;
62  uint64_t ch_layout = 0;
63 
64  if (!info) {
65  av_log(avctx, AV_LOG_ERROR, "Unable to get stream info\n");
66  return AVERROR_UNKNOWN;
67  }
68 
69  if (info->sampleRate <= 0) {
70  av_log(avctx, AV_LOG_ERROR, "Stream info not initialized\n");
71  return AVERROR_UNKNOWN;
72  }
73  avctx->sample_rate = info->sampleRate;
74  avctx->frame_size = info->frameSize;
75 
76  for (i = 0; i < info->numChannels; i++) {
77  AUDIO_CHANNEL_TYPE ctype = info->pChannelType[i];
78  if (ctype <= ACT_NONE || ctype > ACT_TOP) {
79  av_log(avctx, AV_LOG_WARNING, "unknown channel type\n");
80  break;
81  }
82  channel_counts[ctype]++;
83  }
84  av_log(avctx, AV_LOG_DEBUG,
85  "%d channels - front:%d side:%d back:%d lfe:%d top:%d\n",
86  info->numChannels,
87  channel_counts[ACT_FRONT], channel_counts[ACT_SIDE],
88  channel_counts[ACT_BACK], channel_counts[ACT_LFE],
89  channel_counts[ACT_FRONT_TOP] + channel_counts[ACT_SIDE_TOP] +
90  channel_counts[ACT_BACK_TOP] + channel_counts[ACT_TOP]);
91 
92  switch (channel_counts[ACT_FRONT]) {
93  case 4:
96  break;
97  case 3:
99  break;
100  case 2:
101  ch_layout |= AV_CH_LAYOUT_STEREO;
102  break;
103  case 1:
104  ch_layout |= AV_CH_FRONT_CENTER;
105  break;
106  default:
107  av_log(avctx, AV_LOG_WARNING,
108  "unsupported number of front channels: %d\n",
109  channel_counts[ACT_FRONT]);
110  ch_error = 1;
111  break;
112  }
113  if (channel_counts[ACT_SIDE] > 0) {
114  if (channel_counts[ACT_SIDE] == 2) {
115  ch_layout |= AV_CH_SIDE_LEFT | AV_CH_SIDE_RIGHT;
116  } else {
117  av_log(avctx, AV_LOG_WARNING,
118  "unsupported number of side channels: %d\n",
119  channel_counts[ACT_SIDE]);
120  ch_error = 1;
121  }
122  }
123  if (channel_counts[ACT_BACK] > 0) {
124  switch (channel_counts[ACT_BACK]) {
125  case 3:
127  break;
128  case 2:
129  ch_layout |= AV_CH_BACK_LEFT | AV_CH_BACK_RIGHT;
130  break;
131  case 1:
132  ch_layout |= AV_CH_BACK_CENTER;
133  break;
134  default:
135  av_log(avctx, AV_LOG_WARNING,
136  "unsupported number of back channels: %d\n",
137  channel_counts[ACT_BACK]);
138  ch_error = 1;
139  break;
140  }
141  }
142  if (channel_counts[ACT_LFE] > 0) {
143  if (channel_counts[ACT_LFE] == 1) {
144  ch_layout |= AV_CH_LOW_FREQUENCY;
145  } else {
146  av_log(avctx, AV_LOG_WARNING,
147  "unsupported number of LFE channels: %d\n",
148  channel_counts[ACT_LFE]);
149  ch_error = 1;
150  }
151  }
152  if (!ch_error &&
153  av_get_channel_layout_nb_channels(ch_layout) != info->numChannels) {
154  av_log(avctx, AV_LOG_WARNING, "unsupported channel configuration\n");
155  ch_error = 1;
156  }
157  if (ch_error)
158  avctx->channel_layout = 0;
159  else
160  avctx->channel_layout = ch_layout;
161 
162  avctx->channels = info->numChannels;
163 
164  return 0;
165 }
166 
168 {
169  FDKAACDecContext *s = avctx->priv_data;
170 
171  if (s->handle)
172  aacDecoder_Close(s->handle);
173 
174  return 0;
175 }
176 
178 {
179  FDKAACDecContext *s = avctx->priv_data;
180  AAC_DECODER_ERROR err;
181 
182  s->handle = aacDecoder_Open(avctx->extradata_size ? TT_MP4_RAW : TT_MP4_ADTS, 1);
183  if (!s->handle) {
184  av_log(avctx, AV_LOG_ERROR, "Error opening decoder\n");
185  return AVERROR_UNKNOWN;
186  }
187 
188  if (avctx->extradata_size) {
189  if ((err = aacDecoder_ConfigRaw(s->handle, &avctx->extradata,
190  &avctx->extradata_size)) != AAC_DEC_OK) {
191  av_log(avctx, AV_LOG_ERROR, "Unable to set extradata\n");
192  return AVERROR_INVALIDDATA;
193  }
194  }
195 
196  if ((err = aacDecoder_SetParam(s->handle, AAC_CONCEAL_METHOD,
197  s->conceal_method)) != AAC_DEC_OK) {
198  av_log(avctx, AV_LOG_ERROR, "Unable to set error concealment method\n");
199  return AVERROR_UNKNOWN;
200  }
201 
202  avctx->sample_fmt = AV_SAMPLE_FMT_S16;
203 
204  return 0;
205 }
206 
207 static int fdk_aac_decode_frame(AVCodecContext *avctx, void *data,
208  int *got_frame_ptr, AVPacket *avpkt)
209 {
210  FDKAACDecContext *s = avctx->priv_data;
211  AVFrame *frame = data;
212  int ret;
213  AAC_DECODER_ERROR err;
214  UINT valid = avpkt->size;
215  uint8_t *buf, *tmpptr = NULL;
216  int buf_size;
217 
218  err = aacDecoder_Fill(s->handle, &avpkt->data, &avpkt->size, &valid);
219  if (err != AAC_DEC_OK) {
220  av_log(avctx, AV_LOG_ERROR, "aacDecoder_Fill() failed: %x\n", err);
221  return AVERROR_INVALIDDATA;
222  }
223 
224  if (s->initialized) {
225  frame->nb_samples = avctx->frame_size;
226  if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) {
227  av_log(avctx, AV_LOG_ERROR, "ff_get_buffer() failed\n");
228  return ret;
229  }
230  buf = frame->extended_data[0];
231  buf_size = avctx->channels * frame->nb_samples *
233  } else {
234  buf_size = 50 * 1024;
235  buf = tmpptr = av_malloc(buf_size);
236  if (!buf)
237  return AVERROR(ENOMEM);
238  }
239 
240  err = aacDecoder_DecodeFrame(s->handle, (INT_PCM *) buf, buf_size, 0);
241  if (err == AAC_DEC_NOT_ENOUGH_BITS) {
242  ret = avpkt->size - valid;
243  goto end;
244  }
245  if (err != AAC_DEC_OK) {
246  av_log(avctx, AV_LOG_ERROR,
247  "aacDecoder_DecodeFrame() failed: %x\n", err);
248  ret = AVERROR_UNKNOWN;
249  goto end;
250  }
251 
252  if (!s->initialized) {
253  if ((ret = get_stream_info(avctx)) < 0)
254  goto end;
255  s->initialized = 1;
256  frame->nb_samples = avctx->frame_size;
257  }
258 
259  if (tmpptr) {
260  frame->nb_samples = avctx->frame_size;
261  if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) {
262  av_log(avctx, AV_LOG_ERROR, "ff_get_buffer() failed\n");
263  goto end;
264  }
265  memcpy(frame->extended_data[0], tmpptr,
266  avctx->channels * avctx->frame_size *
268  }
269 
270  *got_frame_ptr = 1;
271  ret = avpkt->size - valid;
272 
273 end:
274  av_free(tmpptr);
275  return ret;
276 }
277 
279 {
280  FDKAACDecContext *s = avctx->priv_data;
281  AAC_DECODER_ERROR err;
282 
283  if (!s->handle)
284  return;
285 
286  if ((err = aacDecoder_SetParam(s->handle,
287  AAC_TPDEC_CLEAR_BUFFER, 1)) != AAC_DEC_OK)
288  av_log(avctx, AV_LOG_WARNING, "failed to clear buffer when flushing\n");
289 }
290 
292  .name = "libfdk_aac",
293  .long_name = NULL_IF_CONFIG_SMALL("Fraunhofer FDK AAC"),
294  .type = AVMEDIA_TYPE_AUDIO,
295  .id = AV_CODEC_ID_AAC,
296  .priv_data_size = sizeof(FDKAACDecContext),
301  .capabilities = CODEC_CAP_DR1 | CODEC_CAP_CHANNEL_CONF,
302  .priv_class = &fdk_aac_dec_class,
303 };
void * av_malloc(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:62
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:54
This structure describes decoded (raw) audio or video data.
Definition: frame.h:135
AVOption.
Definition: opt.h:234
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:129
int size
Definition: avcodec.h:974
#define AV_CH_LAYOUT_STEREO
AVCodec.
Definition: avcodec.h:2796
static int decode(MimicContext *ctx, int quality, int num_coeffs, int is_iframe)
Definition: mimic.c:275
enum AVSampleFormat sample_fmt
audio sample format
Definition: avcodec.h:1799
uint8_t
#define OFFSET(x)
Definition: libfdk-aacdec.c:42
#define av_cold
Definition: attributes.h:66
#define AD
Definition: libfdk-aacdec.c:43
AVOptions.
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:1164
#define AV_CH_LOW_FREQUENCY
#define CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: avcodec.h:684
const char data[16]
Definition: mxf.c:70
uint8_t * data
Definition: avcodec.h:973
static int get_stream_info(AVCodecContext *avctx)
Definition: libfdk-aacdec.c:56
#define AV_CH_BACK_LEFT
HANDLE_AACDECODER handle
Definition: libfdk-aacdec.c:37
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:123
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
Definition: mem.c:186
static av_cold void fdk_aac_decode_flush(AVCodecContext *avctx)
#define AVERROR(e)
Definition: error.h:43
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:150
AVCodec ff_libfdk_aac_decoder
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:144
int av_get_channel_layout_nb_channels(uint64_t channel_layout)
Return the number of channels in the channel layout.
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:168
const char * name
Name of the codec implementation.
Definition: avcodec.h:2803
uint64_t channel_layout
Audio channel layout.
Definition: avcodec.h:1852
ConcealMethod
Definition: libfdk-aacdec.c:28
audio channel layout utility functions
static const AVOption fdk_aac_dec_options[]
Definition: libfdk-aacdec.c:44
#define AV_CH_FRONT_LEFT_OF_CENTER
#define AV_CH_FRONT_CENTER
LIBAVUTIL_VERSION_INT
Definition: eval.c:55
#define AV_CH_FRONT_RIGHT_OF_CENTER
int frame_size
Number of samples per channel in an audio frame.
Definition: avcodec.h:1811
NULL
Definition: eval.c:55
Libavcodec external API header.
int sample_rate
samples per second
Definition: avcodec.h:1791
static int fdk_aac_decode_frame(AVCodecContext *avctx, void *data, int *got_frame_ptr, AVPacket *avpkt)
static const AVClass fdk_aac_dec_class
Definition: libfdk-aacdec.c:52
av_default_item_name
Definition: dnxhdenc.c:52
main external API structure.
Definition: avcodec.h:1050
static void close(AVCodecParserContext *s)
Definition: h264_parser.c:490
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: utils.c:612
int extradata_size
Definition: avcodec.h:1165
Describe the class of an AVClass context structure.
Definition: log.h:33
#define AV_CH_BACK_CENTER
#define AV_CH_SIDE_RIGHT
int av_get_bytes_per_sample(enum AVSampleFormat sample_fmt)
Return number of bytes per sample.
Definition: samplefmt.c:95
common internal api header.
static av_cold int fdk_aac_decode_close(AVCodecContext *avctx)
common internal and external API header
#define CODEC_CAP_CHANNEL_CONF
Codec should fill in channel configuration and samplerate instead of container.
Definition: avcodec.h:745
static av_cold void flush(AVCodecContext *avctx)
Flush (reset) the frame ID after seeking.
Definition: alsdec.c:1771
signed 16 bits
Definition: samplefmt.h:64
static av_cold int fdk_aac_decode_init(AVCodecContext *avctx)
#define AVERROR_UNKNOWN
Unknown error, typically from an external library.
Definition: error.h:61
static av_cold int init(AVCodecParserContext *s)
Definition: h264_parser.c:499
void * priv_data
Definition: avcodec.h:1092
int channels
number of audio channels
Definition: avcodec.h:1792
#define AV_CH_SIDE_LEFT
enum ConcealMethod conceal_method
Definition: libfdk-aacdec.c:39
uint8_t ** extended_data
pointers to the data planes/channels.
Definition: frame.h:169
This structure stores compressed data.
Definition: avcodec.h:950
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:179
#define AV_CH_BACK_RIGHT