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.

388 lines
14KB

  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. #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. enum ConcealMethod 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. "libfdk-aac decoder", av_default_item_name, fdk_aac_dec_options, LIBAVUTIL_VERSION_INT
  74. };
  75. static int get_stream_info(AVCodecContext *avctx)
  76. {
  77. FDKAACDecContext *s = avctx->priv_data;
  78. CStreamInfo *info = aacDecoder_GetStreamInfo(s->handle);
  79. int channel_counts[0x24] = { 0 };
  80. int i, ch_error = 0;
  81. uint64_t ch_layout = 0;
  82. if (!info) {
  83. av_log(avctx, AV_LOG_ERROR, "Unable to get stream info\n");
  84. return AVERROR_UNKNOWN;
  85. }
  86. if (info->sampleRate <= 0) {
  87. av_log(avctx, AV_LOG_ERROR, "Stream info not initialized\n");
  88. return AVERROR_UNKNOWN;
  89. }
  90. avctx->sample_rate = info->sampleRate;
  91. avctx->frame_size = info->frameSize;
  92. for (i = 0; i < info->numChannels; i++) {
  93. AUDIO_CHANNEL_TYPE ctype = info->pChannelType[i];
  94. if (ctype <= ACT_NONE || ctype >= FF_ARRAY_ELEMS(channel_counts)) {
  95. av_log(avctx, AV_LOG_WARNING, "unknown channel type\n");
  96. break;
  97. }
  98. channel_counts[ctype]++;
  99. }
  100. av_log(avctx, AV_LOG_DEBUG,
  101. "%d channels - front:%d side:%d back:%d lfe:%d top:%d\n",
  102. info->numChannels,
  103. channel_counts[ACT_FRONT], channel_counts[ACT_SIDE],
  104. channel_counts[ACT_BACK], channel_counts[ACT_LFE],
  105. channel_counts[ACT_FRONT_TOP] + channel_counts[ACT_SIDE_TOP] +
  106. channel_counts[ACT_BACK_TOP] + channel_counts[ACT_TOP]);
  107. switch (channel_counts[ACT_FRONT]) {
  108. case 4:
  109. ch_layout |= AV_CH_LAYOUT_STEREO | AV_CH_FRONT_LEFT_OF_CENTER |
  110. AV_CH_FRONT_RIGHT_OF_CENTER;
  111. break;
  112. case 3:
  113. ch_layout |= AV_CH_LAYOUT_STEREO | AV_CH_FRONT_CENTER;
  114. break;
  115. case 2:
  116. ch_layout |= AV_CH_LAYOUT_STEREO;
  117. break;
  118. case 1:
  119. ch_layout |= AV_CH_FRONT_CENTER;
  120. break;
  121. default:
  122. av_log(avctx, AV_LOG_WARNING,
  123. "unsupported number of front channels: %d\n",
  124. channel_counts[ACT_FRONT]);
  125. ch_error = 1;
  126. break;
  127. }
  128. if (channel_counts[ACT_SIDE] > 0) {
  129. if (channel_counts[ACT_SIDE] == 2) {
  130. ch_layout |= AV_CH_SIDE_LEFT | AV_CH_SIDE_RIGHT;
  131. } else {
  132. av_log(avctx, AV_LOG_WARNING,
  133. "unsupported number of side channels: %d\n",
  134. channel_counts[ACT_SIDE]);
  135. ch_error = 1;
  136. }
  137. }
  138. if (channel_counts[ACT_BACK] > 0) {
  139. switch (channel_counts[ACT_BACK]) {
  140. case 3:
  141. ch_layout |= AV_CH_BACK_LEFT | AV_CH_BACK_RIGHT | AV_CH_BACK_CENTER;
  142. break;
  143. case 2:
  144. ch_layout |= AV_CH_BACK_LEFT | AV_CH_BACK_RIGHT;
  145. break;
  146. case 1:
  147. ch_layout |= AV_CH_BACK_CENTER;
  148. break;
  149. default:
  150. av_log(avctx, AV_LOG_WARNING,
  151. "unsupported number of back channels: %d\n",
  152. channel_counts[ACT_BACK]);
  153. ch_error = 1;
  154. break;
  155. }
  156. }
  157. if (channel_counts[ACT_LFE] > 0) {
  158. if (channel_counts[ACT_LFE] == 1) {
  159. ch_layout |= AV_CH_LOW_FREQUENCY;
  160. } else {
  161. av_log(avctx, AV_LOG_WARNING,
  162. "unsupported number of LFE channels: %d\n",
  163. channel_counts[ACT_LFE]);
  164. ch_error = 1;
  165. }
  166. }
  167. if (!ch_error &&
  168. av_get_channel_layout_nb_channels(ch_layout) != info->numChannels) {
  169. av_log(avctx, AV_LOG_WARNING, "unsupported channel configuration\n");
  170. ch_error = 1;
  171. }
  172. if (ch_error)
  173. avctx->channel_layout = 0;
  174. else
  175. avctx->channel_layout = ch_layout;
  176. avctx->channels = info->numChannels;
  177. return 0;
  178. }
  179. static av_cold int fdk_aac_decode_close(AVCodecContext *avctx)
  180. {
  181. FDKAACDecContext *s = avctx->priv_data;
  182. if (s->handle)
  183. aacDecoder_Close(s->handle);
  184. av_free(s->decoder_buffer);
  185. av_free(s->anc_buffer);
  186. return 0;
  187. }
  188. static av_cold int fdk_aac_decode_init(AVCodecContext *avctx)
  189. {
  190. FDKAACDecContext *s = avctx->priv_data;
  191. AAC_DECODER_ERROR err;
  192. int ret;
  193. s->handle = aacDecoder_Open(avctx->extradata_size ? TT_MP4_RAW : TT_MP4_ADTS, 1);
  194. if (!s->handle) {
  195. av_log(avctx, AV_LOG_ERROR, "Error opening decoder\n");
  196. return AVERROR_UNKNOWN;
  197. }
  198. if (avctx->extradata_size) {
  199. if ((err = aacDecoder_ConfigRaw(s->handle, &avctx->extradata,
  200. &avctx->extradata_size)) != AAC_DEC_OK) {
  201. av_log(avctx, AV_LOG_ERROR, "Unable to set extradata\n");
  202. return AVERROR_INVALIDDATA;
  203. }
  204. }
  205. if ((err = aacDecoder_SetParam(s->handle, AAC_CONCEAL_METHOD,
  206. s->conceal_method)) != AAC_DEC_OK) {
  207. av_log(avctx, AV_LOG_ERROR, "Unable to set error concealment method\n");
  208. return AVERROR_UNKNOWN;
  209. }
  210. if (avctx->request_channel_layout > 0 &&
  211. avctx->request_channel_layout != AV_CH_LAYOUT_NATIVE) {
  212. int downmix_channels = -1;
  213. switch (avctx->request_channel_layout) {
  214. case AV_CH_LAYOUT_STEREO:
  215. case AV_CH_LAYOUT_STEREO_DOWNMIX:
  216. downmix_channels = 2;
  217. break;
  218. case AV_CH_LAYOUT_MONO:
  219. downmix_channels = 1;
  220. break;
  221. default:
  222. av_log(avctx, AV_LOG_WARNING, "Invalid request_channel_layout\n");
  223. break;
  224. }
  225. if (downmix_channels != -1) {
  226. if (aacDecoder_SetParam(s->handle, AAC_PCM_MAX_OUTPUT_CHANNELS,
  227. downmix_channels) != AAC_DEC_OK) {
  228. av_log(avctx, AV_LOG_WARNING, "Unable to set output channels in the decoder\n");
  229. } else {
  230. s->anc_buffer = av_malloc(DMX_ANC_BUFFSIZE);
  231. if (!s->anc_buffer) {
  232. av_log(avctx, AV_LOG_ERROR, "Unable to allocate ancillary buffer for the decoder\n");
  233. return AVERROR(ENOMEM);
  234. }
  235. if (aacDecoder_AncDataInit(s->handle, s->anc_buffer, DMX_ANC_BUFFSIZE)) {
  236. av_log(avctx, AV_LOG_ERROR, "Unable to register downmix ancillary buffer in the decoder\n");
  237. return AVERROR_UNKNOWN;
  238. }
  239. }
  240. }
  241. }
  242. if (s->drc_boost != -1) {
  243. if (aacDecoder_SetParam(s->handle, AAC_DRC_BOOST_FACTOR, s->drc_boost) != AAC_DEC_OK) {
  244. av_log(avctx, AV_LOG_ERROR, "Unable to set DRC boost factor in the decoder\n");
  245. return AVERROR_UNKNOWN;
  246. }
  247. }
  248. if (s->drc_cut != -1) {
  249. if (aacDecoder_SetParam(s->handle, AAC_DRC_ATTENUATION_FACTOR, s->drc_cut) != AAC_DEC_OK) {
  250. av_log(avctx, AV_LOG_ERROR, "Unable to set DRC attenuation factor in the decoder\n");
  251. return AVERROR_UNKNOWN;
  252. }
  253. }
  254. if (s->drc_level != -1) {
  255. if (aacDecoder_SetParam(s->handle, AAC_DRC_REFERENCE_LEVEL, s->drc_level) != AAC_DEC_OK) {
  256. av_log(avctx, AV_LOG_ERROR, "Unable to set DRC reference level in the decoder\n");
  257. return AVERROR_UNKNOWN;
  258. }
  259. }
  260. if (s->drc_heavy != -1) {
  261. if (aacDecoder_SetParam(s->handle, AAC_DRC_HEAVY_COMPRESSION, s->drc_heavy) != AAC_DEC_OK) {
  262. av_log(avctx, AV_LOG_ERROR, "Unable to set DRC heavy compression in the decoder\n");
  263. return AVERROR_UNKNOWN;
  264. }
  265. }
  266. #ifdef AACDECODER_LIB_VL0
  267. if (aacDecoder_SetParam(s->handle, AAC_PCM_LIMITER_ENABLE, s->level_limit) != AAC_DEC_OK) {
  268. av_log(avctx, AV_LOG_ERROR, "Unable to set in signal level limiting in the decoder\n");
  269. return AVERROR_UNKNOWN;
  270. }
  271. #endif
  272. avctx->sample_fmt = AV_SAMPLE_FMT_S16;
  273. s->decoder_buffer_size = DECODER_BUFFSIZE * DECODER_MAX_CHANNELS;
  274. s->decoder_buffer = av_malloc(s->decoder_buffer_size);
  275. if (!s->decoder_buffer)
  276. return AVERROR(ENOMEM);
  277. return 0;
  278. }
  279. static int fdk_aac_decode_frame(AVCodecContext *avctx, void *data,
  280. int *got_frame_ptr, AVPacket *avpkt)
  281. {
  282. FDKAACDecContext *s = avctx->priv_data;
  283. AVFrame *frame = data;
  284. int ret;
  285. AAC_DECODER_ERROR err;
  286. UINT valid = avpkt->size;
  287. err = aacDecoder_Fill(s->handle, &avpkt->data, &avpkt->size, &valid);
  288. if (err != AAC_DEC_OK) {
  289. av_log(avctx, AV_LOG_ERROR, "aacDecoder_Fill() failed: %x\n", err);
  290. return AVERROR_INVALIDDATA;
  291. }
  292. err = aacDecoder_DecodeFrame(s->handle, (INT_PCM *) s->decoder_buffer, s->decoder_buffer_size, 0);
  293. if (err == AAC_DEC_NOT_ENOUGH_BITS) {
  294. ret = avpkt->size - valid;
  295. goto end;
  296. }
  297. if (err != AAC_DEC_OK) {
  298. av_log(avctx, AV_LOG_ERROR,
  299. "aacDecoder_DecodeFrame() failed: %x\n", err);
  300. ret = AVERROR_UNKNOWN;
  301. goto end;
  302. }
  303. if ((ret = get_stream_info(avctx)) < 0)
  304. goto end;
  305. frame->nb_samples = avctx->frame_size;
  306. if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) {
  307. av_log(avctx, AV_LOG_ERROR, "ff_get_buffer() failed\n");
  308. goto end;
  309. }
  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. };