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.

411 lines
15KB

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