FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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 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 
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 
39 };
40 
41 typedef struct FDKAACDecContext {
42  const AVClass *class;
43  HANDLE_AACDECODER handle;
48  int drc_level;
49  int drc_boost;
50  int drc_heavy;
51  int drc_cut;
54 
55 
56 #define DMX_ANC_BUFFSIZE 128
57 #define DECODER_MAX_CHANNELS 6
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 = {
83 };
84 
85 static int get_stream_info(AVCodecContext *avctx)
86 {
87  FDKAACDecContext *s = avctx->priv_data;
88  CStreamInfo *info = aacDecoder_GetStreamInfo(s->handle);
89  int channel_counts[0x24] = { 0 };
90  int i, ch_error = 0;
91  uint64_t ch_layout = 0;
92 
93  if (!info) {
94  av_log(avctx, AV_LOG_ERROR, "Unable to get stream info\n");
95  return AVERROR_UNKNOWN;
96  }
97 
98  if (info->sampleRate <= 0) {
99  av_log(avctx, AV_LOG_ERROR, "Stream info not initialized\n");
100  return AVERROR_UNKNOWN;
101  }
102  avctx->sample_rate = info->sampleRate;
103  avctx->frame_size = info->frameSize;
104 
105  for (i = 0; i < info->numChannels; i++) {
106  AUDIO_CHANNEL_TYPE ctype = info->pChannelType[i];
107  if (ctype <= ACT_NONE || ctype >= FF_ARRAY_ELEMS(channel_counts)) {
108  av_log(avctx, AV_LOG_WARNING, "unknown channel type\n");
109  break;
110  }
111  channel_counts[ctype]++;
112  }
113  av_log(avctx, AV_LOG_DEBUG,
114  "%d channels - front:%d side:%d back:%d lfe:%d top:%d\n",
115  info->numChannels,
116  channel_counts[ACT_FRONT], channel_counts[ACT_SIDE],
117  channel_counts[ACT_BACK], channel_counts[ACT_LFE],
118  channel_counts[ACT_FRONT_TOP] + channel_counts[ACT_SIDE_TOP] +
119  channel_counts[ACT_BACK_TOP] + channel_counts[ACT_TOP]);
120 
121  switch (channel_counts[ACT_FRONT]) {
122  case 4:
125  break;
126  case 3:
128  break;
129  case 2:
130  ch_layout |= AV_CH_LAYOUT_STEREO;
131  break;
132  case 1:
133  ch_layout |= AV_CH_FRONT_CENTER;
134  break;
135  default:
136  av_log(avctx, AV_LOG_WARNING,
137  "unsupported number of front channels: %d\n",
138  channel_counts[ACT_FRONT]);
139  ch_error = 1;
140  break;
141  }
142  if (channel_counts[ACT_SIDE] > 0) {
143  if (channel_counts[ACT_SIDE] == 2) {
144  ch_layout |= AV_CH_SIDE_LEFT | AV_CH_SIDE_RIGHT;
145  } else {
146  av_log(avctx, AV_LOG_WARNING,
147  "unsupported number of side channels: %d\n",
148  channel_counts[ACT_SIDE]);
149  ch_error = 1;
150  }
151  }
152  if (channel_counts[ACT_BACK] > 0) {
153  switch (channel_counts[ACT_BACK]) {
154  case 3:
156  break;
157  case 2:
158  ch_layout |= AV_CH_BACK_LEFT | AV_CH_BACK_RIGHT;
159  break;
160  case 1:
161  ch_layout |= AV_CH_BACK_CENTER;
162  break;
163  default:
164  av_log(avctx, AV_LOG_WARNING,
165  "unsupported number of back channels: %d\n",
166  channel_counts[ACT_BACK]);
167  ch_error = 1;
168  break;
169  }
170  }
171  if (channel_counts[ACT_LFE] > 0) {
172  if (channel_counts[ACT_LFE] == 1) {
173  ch_layout |= AV_CH_LOW_FREQUENCY;
174  } else {
175  av_log(avctx, AV_LOG_WARNING,
176  "unsupported number of LFE channels: %d\n",
177  channel_counts[ACT_LFE]);
178  ch_error = 1;
179  }
180  }
181  if (!ch_error &&
182  av_get_channel_layout_nb_channels(ch_layout) != info->numChannels) {
183  av_log(avctx, AV_LOG_WARNING, "unsupported channel configuration\n");
184  ch_error = 1;
185  }
186  if (ch_error)
187  avctx->channel_layout = 0;
188  else
189  avctx->channel_layout = ch_layout;
190 
191  avctx->channels = info->numChannels;
192 
193  return 0;
194 }
195 
197 {
198  FDKAACDecContext *s = avctx->priv_data;
199 
200  if (s->handle)
201  aacDecoder_Close(s->handle);
203  av_freep(&s->anc_buffer);
204 
205  return 0;
206 }
207 
209 {
210  FDKAACDecContext *s = avctx->priv_data;
211  AAC_DECODER_ERROR err;
212  int ret;
213 
214  s->handle = aacDecoder_Open(avctx->extradata_size ? TT_MP4_RAW : TT_MP4_ADTS, 1);
215  if (!s->handle) {
216  av_log(avctx, AV_LOG_ERROR, "Error opening decoder\n");
217  return AVERROR_UNKNOWN;
218  }
219 
220  if (avctx->extradata_size) {
221  if ((err = aacDecoder_ConfigRaw(s->handle, &avctx->extradata,
222  &avctx->extradata_size)) != AAC_DEC_OK) {
223  av_log(avctx, AV_LOG_ERROR, "Unable to set extradata\n");
224  return AVERROR_INVALIDDATA;
225  }
226  }
227 
228  if ((err = aacDecoder_SetParam(s->handle, AAC_CONCEAL_METHOD,
229  s->conceal_method)) != AAC_DEC_OK) {
230  av_log(avctx, AV_LOG_ERROR, "Unable to set error concealment method\n");
231  return AVERROR_UNKNOWN;
232  }
233 
234  if (avctx->request_channel_layout > 0 &&
236  int downmix_channels = -1;
237 
238  switch (avctx->request_channel_layout) {
239  case AV_CH_LAYOUT_STEREO:
241  downmix_channels = 2;
242  break;
243  case AV_CH_LAYOUT_MONO:
244  downmix_channels = 1;
245  break;
246  default:
247  av_log(avctx, AV_LOG_WARNING, "Invalid request_channel_layout\n");
248  break;
249  }
250 
251  if (downmix_channels != -1) {
252  if (aacDecoder_SetParam(s->handle, AAC_PCM_MAX_OUTPUT_CHANNELS,
253  downmix_channels) != AAC_DEC_OK) {
254  av_log(avctx, AV_LOG_WARNING, "Unable to set output channels in the decoder\n");
255  } else {
257  if (!s->anc_buffer) {
258  av_log(avctx, AV_LOG_ERROR, "Unable to allocate ancillary buffer for the decoder\n");
259  ret = AVERROR(ENOMEM);
260  goto fail;
261  }
262  if (aacDecoder_AncDataInit(s->handle, s->anc_buffer, DMX_ANC_BUFFSIZE)) {
263  av_log(avctx, AV_LOG_ERROR, "Unable to register downmix ancillary buffer in the decoder\n");
264  ret = AVERROR_UNKNOWN;
265  goto fail;
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  return 0;
309 fail:
310  fdk_aac_decode_close(avctx);
311  return ret;
312 }
313 
314 static int fdk_aac_decode_frame(AVCodecContext *avctx, void *data,
315  int *got_frame_ptr, AVPacket *avpkt)
316 {
317  FDKAACDecContext *s = avctx->priv_data;
318  AVFrame *frame = data;
319  int ret;
320  AAC_DECODER_ERROR err;
321  UINT valid = avpkt->size;
322  uint8_t *buf, *tmpptr = NULL;
323  int buf_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  if (s->initialized) {
332  frame->nb_samples = avctx->frame_size;
333  if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
334  return ret;
335 
336  if (s->anc_buffer) {
338  buf = s->decoder_buffer;
339  } else {
340  buf = frame->extended_data[0];
341  buf_size = avctx->channels * frame->nb_samples *
343  }
344  } else {
346 
347  if (!s->decoder_buffer)
348  s->decoder_buffer = av_malloc(buf_size);
349  if (!s->decoder_buffer)
350  return AVERROR(ENOMEM);
351 
352  buf = tmpptr = s->decoder_buffer;
353  }
354 
355  err = aacDecoder_DecodeFrame(s->handle, (INT_PCM *) buf, buf_size, 0);
356  if (err == AAC_DEC_NOT_ENOUGH_BITS) {
357  ret = avpkt->size - valid;
358  goto end;
359  }
360  if (err != AAC_DEC_OK) {
361  av_log(avctx, AV_LOG_ERROR,
362  "aacDecoder_DecodeFrame() failed: %x\n", err);
363  ret = AVERROR_UNKNOWN;
364  goto end;
365  }
366 
367  if (!s->initialized) {
368  if ((ret = get_stream_info(avctx)) < 0)
369  goto end;
370  s->initialized = 1;
371  frame->nb_samples = avctx->frame_size;
372  }
373 
374  if (tmpptr) {
375  frame->nb_samples = avctx->frame_size;
376  if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
377  goto end;
378  }
379  if (s->decoder_buffer) {
380  memcpy(frame->extended_data[0], buf,
381  avctx->channels * avctx->frame_size *
383 
384  if (!s->anc_buffer)
386  }
387 
388  *got_frame_ptr = 1;
389  ret = avpkt->size - valid;
390 
391 end:
392  return ret;
393 }
394 
396 {
397  FDKAACDecContext *s = avctx->priv_data;
398  AAC_DECODER_ERROR err;
399 
400  if (!s->handle)
401  return;
402 
403  if ((err = aacDecoder_SetParam(s->handle,
404  AAC_TPDEC_CLEAR_BUFFER, 1)) != AAC_DEC_OK)
405  av_log(avctx, AV_LOG_WARNING, "failed to clear buffer when flushing\n");
406 }
407 
409  .name = "libfdk_aac",
410  .long_name = NULL_IF_CONFIG_SMALL("Fraunhofer FDK AAC"),
411  .type = AVMEDIA_TYPE_AUDIO,
412  .id = AV_CODEC_ID_AAC,
413  .priv_data_size = sizeof(FDKAACDecContext),
416  .close = fdk_aac_decode_close,
418  .capabilities = CODEC_CAP_DR1 | CODEC_CAP_CHANNEL_CONF,
419  .priv_class = &fdk_aac_dec_class,
420 };