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.

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