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.

387 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. s->handle = aacDecoder_Open(avctx->extradata_size ? TT_MP4_RAW : TT_MP4_ADTS, 1);
  193. if (!s->handle) {
  194. av_log(avctx, AV_LOG_ERROR, "Error opening decoder\n");
  195. return AVERROR_UNKNOWN;
  196. }
  197. if (avctx->extradata_size) {
  198. if ((err = aacDecoder_ConfigRaw(s->handle, &avctx->extradata,
  199. &avctx->extradata_size)) != AAC_DEC_OK) {
  200. av_log(avctx, AV_LOG_ERROR, "Unable to set extradata\n");
  201. return AVERROR_INVALIDDATA;
  202. }
  203. }
  204. if ((err = aacDecoder_SetParam(s->handle, AAC_CONCEAL_METHOD,
  205. s->conceal_method)) != AAC_DEC_OK) {
  206. av_log(avctx, AV_LOG_ERROR, "Unable to set error concealment method\n");
  207. return AVERROR_UNKNOWN;
  208. }
  209. if (avctx->request_channel_layout > 0 &&
  210. avctx->request_channel_layout != AV_CH_LAYOUT_NATIVE) {
  211. int downmix_channels = -1;
  212. switch (avctx->request_channel_layout) {
  213. case AV_CH_LAYOUT_STEREO:
  214. case AV_CH_LAYOUT_STEREO_DOWNMIX:
  215. downmix_channels = 2;
  216. break;
  217. case AV_CH_LAYOUT_MONO:
  218. downmix_channels = 1;
  219. break;
  220. default:
  221. av_log(avctx, AV_LOG_WARNING, "Invalid request_channel_layout\n");
  222. break;
  223. }
  224. if (downmix_channels != -1) {
  225. if (aacDecoder_SetParam(s->handle, AAC_PCM_MAX_OUTPUT_CHANNELS,
  226. downmix_channels) != AAC_DEC_OK) {
  227. av_log(avctx, AV_LOG_WARNING, "Unable to set output channels in the decoder\n");
  228. } else {
  229. s->anc_buffer = av_malloc(DMX_ANC_BUFFSIZE);
  230. if (!s->anc_buffer) {
  231. av_log(avctx, AV_LOG_ERROR, "Unable to allocate ancillary buffer for the decoder\n");
  232. return AVERROR(ENOMEM);
  233. }
  234. if (aacDecoder_AncDataInit(s->handle, s->anc_buffer, DMX_ANC_BUFFSIZE)) {
  235. av_log(avctx, AV_LOG_ERROR, "Unable to register downmix ancillary buffer in the decoder\n");
  236. return AVERROR_UNKNOWN;
  237. }
  238. }
  239. }
  240. }
  241. if (s->drc_boost != -1) {
  242. if (aacDecoder_SetParam(s->handle, AAC_DRC_BOOST_FACTOR, s->drc_boost) != AAC_DEC_OK) {
  243. av_log(avctx, AV_LOG_ERROR, "Unable to set DRC boost factor in the decoder\n");
  244. return AVERROR_UNKNOWN;
  245. }
  246. }
  247. if (s->drc_cut != -1) {
  248. if (aacDecoder_SetParam(s->handle, AAC_DRC_ATTENUATION_FACTOR, s->drc_cut) != AAC_DEC_OK) {
  249. av_log(avctx, AV_LOG_ERROR, "Unable to set DRC attenuation factor in the decoder\n");
  250. return AVERROR_UNKNOWN;
  251. }
  252. }
  253. if (s->drc_level != -1) {
  254. if (aacDecoder_SetParam(s->handle, AAC_DRC_REFERENCE_LEVEL, s->drc_level) != AAC_DEC_OK) {
  255. av_log(avctx, AV_LOG_ERROR, "Unable to set DRC reference level in the decoder\n");
  256. return AVERROR_UNKNOWN;
  257. }
  258. }
  259. if (s->drc_heavy != -1) {
  260. if (aacDecoder_SetParam(s->handle, AAC_DRC_HEAVY_COMPRESSION, s->drc_heavy) != AAC_DEC_OK) {
  261. av_log(avctx, AV_LOG_ERROR, "Unable to set DRC heavy compression in the decoder\n");
  262. return AVERROR_UNKNOWN;
  263. }
  264. }
  265. #ifdef AACDECODER_LIB_VL0
  266. if (aacDecoder_SetParam(s->handle, AAC_PCM_LIMITER_ENABLE, s->level_limit) != AAC_DEC_OK) {
  267. av_log(avctx, AV_LOG_ERROR, "Unable to set in signal level limiting in the decoder\n");
  268. return AVERROR_UNKNOWN;
  269. }
  270. #endif
  271. avctx->sample_fmt = AV_SAMPLE_FMT_S16;
  272. s->decoder_buffer_size = DECODER_BUFFSIZE * DECODER_MAX_CHANNELS;
  273. s->decoder_buffer = av_malloc(s->decoder_buffer_size);
  274. if (!s->decoder_buffer)
  275. return AVERROR(ENOMEM);
  276. return 0;
  277. }
  278. static int fdk_aac_decode_frame(AVCodecContext *avctx, void *data,
  279. int *got_frame_ptr, AVPacket *avpkt)
  280. {
  281. FDKAACDecContext *s = avctx->priv_data;
  282. AVFrame *frame = data;
  283. int ret;
  284. AAC_DECODER_ERROR err;
  285. UINT valid = avpkt->size;
  286. err = aacDecoder_Fill(s->handle, &avpkt->data, &avpkt->size, &valid);
  287. if (err != AAC_DEC_OK) {
  288. av_log(avctx, AV_LOG_ERROR, "aacDecoder_Fill() failed: %x\n", err);
  289. return AVERROR_INVALIDDATA;
  290. }
  291. err = aacDecoder_DecodeFrame(s->handle, (INT_PCM *) s->decoder_buffer, s->decoder_buffer_size, 0);
  292. if (err == AAC_DEC_NOT_ENOUGH_BITS) {
  293. ret = avpkt->size - valid;
  294. goto end;
  295. }
  296. if (err != AAC_DEC_OK) {
  297. av_log(avctx, AV_LOG_ERROR,
  298. "aacDecoder_DecodeFrame() failed: %x\n", err);
  299. ret = AVERROR_UNKNOWN;
  300. goto end;
  301. }
  302. if ((ret = get_stream_info(avctx)) < 0)
  303. goto end;
  304. frame->nb_samples = avctx->frame_size;
  305. if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) {
  306. av_log(avctx, AV_LOG_ERROR, "ff_get_buffer() failed\n");
  307. goto end;
  308. }
  309. memcpy(frame->extended_data[0], s->decoder_buffer,
  310. avctx->channels * avctx->frame_size *
  311. av_get_bytes_per_sample(avctx->sample_fmt));
  312. *got_frame_ptr = 1;
  313. ret = avpkt->size - valid;
  314. end:
  315. return ret;
  316. }
  317. static av_cold void fdk_aac_decode_flush(AVCodecContext *avctx)
  318. {
  319. FDKAACDecContext *s = avctx->priv_data;
  320. AAC_DECODER_ERROR err;
  321. if (!s->handle)
  322. return;
  323. if ((err = aacDecoder_SetParam(s->handle,
  324. AAC_TPDEC_CLEAR_BUFFER, 1)) != AAC_DEC_OK)
  325. av_log(avctx, AV_LOG_WARNING, "failed to clear buffer when flushing\n");
  326. }
  327. AVCodec ff_libfdk_aac_decoder = {
  328. .name = "libfdk_aac",
  329. .long_name = NULL_IF_CONFIG_SMALL("Fraunhofer FDK AAC"),
  330. .type = AVMEDIA_TYPE_AUDIO,
  331. .id = AV_CODEC_ID_AAC,
  332. .priv_data_size = sizeof(FDKAACDecContext),
  333. .init = fdk_aac_decode_init,
  334. .decode = fdk_aac_decode_frame,
  335. .close = fdk_aac_decode_close,
  336. .flush = fdk_aac_decode_flush,
  337. .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_CHANNEL_CONF,
  338. .priv_class = &fdk_aac_dec_class,
  339. .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE |
  340. FF_CODEC_CAP_INIT_CLEANUP,
  341. };