You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

390 lines
14KB

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