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.

425 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. /* 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. int initialized;
  40. uint8_t *decoder_buffer;
  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 6
  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. ret = AVERROR(ENOMEM);
  234. goto fail;
  235. }
  236. if (aacDecoder_AncDataInit(s->handle, s->anc_buffer, DMX_ANC_BUFFSIZE)) {
  237. av_log(avctx, AV_LOG_ERROR, "Unable to register downmix ancillary buffer in the decoder\n");
  238. ret = AVERROR_UNKNOWN;
  239. goto fail;
  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. return 0;
  276. fail:
  277. fdk_aac_decode_close(avctx);
  278. return ret;
  279. }
  280. static int fdk_aac_decode_frame(AVCodecContext *avctx, void *data,
  281. int *got_frame_ptr, AVPacket *avpkt)
  282. {
  283. FDKAACDecContext *s = avctx->priv_data;
  284. AVFrame *frame = data;
  285. int ret;
  286. AAC_DECODER_ERROR err;
  287. UINT valid = avpkt->size;
  288. uint8_t *buf, *tmpptr = NULL;
  289. int buf_size;
  290. err = aacDecoder_Fill(s->handle, &avpkt->data, &avpkt->size, &valid);
  291. if (err != AAC_DEC_OK) {
  292. av_log(avctx, AV_LOG_ERROR, "aacDecoder_Fill() failed: %x\n", err);
  293. return AVERROR_INVALIDDATA;
  294. }
  295. if (s->initialized) {
  296. frame->nb_samples = avctx->frame_size;
  297. if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) {
  298. av_log(avctx, AV_LOG_ERROR, "ff_get_buffer() failed\n");
  299. return ret;
  300. }
  301. if (s->anc_buffer) {
  302. buf_size = DECODER_BUFFSIZE * DECODER_MAX_CHANNELS;
  303. buf = s->decoder_buffer;
  304. } else {
  305. buf = frame->extended_data[0];
  306. buf_size = avctx->channels * frame->nb_samples *
  307. av_get_bytes_per_sample(avctx->sample_fmt);
  308. }
  309. } else {
  310. buf_size = DECODER_BUFFSIZE * DECODER_MAX_CHANNELS;
  311. if (!s->decoder_buffer)
  312. s->decoder_buffer = av_malloc(buf_size);
  313. if (!s->decoder_buffer)
  314. return AVERROR(ENOMEM);
  315. buf = tmpptr = s->decoder_buffer;
  316. }
  317. err = aacDecoder_DecodeFrame(s->handle, (INT_PCM *) buf, buf_size, 0);
  318. if (err == AAC_DEC_NOT_ENOUGH_BITS) {
  319. ret = avpkt->size - valid;
  320. goto end;
  321. }
  322. if (err != AAC_DEC_OK) {
  323. av_log(avctx, AV_LOG_ERROR,
  324. "aacDecoder_DecodeFrame() failed: %x\n", err);
  325. ret = AVERROR_UNKNOWN;
  326. goto end;
  327. }
  328. if (!s->initialized) {
  329. if ((ret = get_stream_info(avctx)) < 0)
  330. goto end;
  331. s->initialized = 1;
  332. frame->nb_samples = avctx->frame_size;
  333. }
  334. if (tmpptr) {
  335. frame->nb_samples = avctx->frame_size;
  336. if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) {
  337. av_log(avctx, AV_LOG_ERROR, "ff_get_buffer() failed\n");
  338. goto end;
  339. }
  340. }
  341. if (s->decoder_buffer) {
  342. memcpy(frame->extended_data[0], buf,
  343. avctx->channels * avctx->frame_size *
  344. av_get_bytes_per_sample(avctx->sample_fmt));
  345. if (!s->anc_buffer)
  346. av_freep(&s->decoder_buffer);
  347. }
  348. *got_frame_ptr = 1;
  349. ret = avpkt->size - valid;
  350. end:
  351. return ret;
  352. }
  353. static av_cold void fdk_aac_decode_flush(AVCodecContext *avctx)
  354. {
  355. FDKAACDecContext *s = avctx->priv_data;
  356. AAC_DECODER_ERROR err;
  357. if (!s->handle)
  358. return;
  359. if ((err = aacDecoder_SetParam(s->handle,
  360. AAC_TPDEC_CLEAR_BUFFER, 1)) != AAC_DEC_OK)
  361. av_log(avctx, AV_LOG_WARNING, "failed to clear buffer when flushing\n");
  362. }
  363. AVCodec ff_libfdk_aac_decoder = {
  364. .name = "libfdk_aac",
  365. .long_name = NULL_IF_CONFIG_SMALL("Fraunhofer FDK AAC"),
  366. .type = AVMEDIA_TYPE_AUDIO,
  367. .id = AV_CODEC_ID_AAC,
  368. .priv_data_size = sizeof(FDKAACDecContext),
  369. .init = fdk_aac_decode_init,
  370. .decode = fdk_aac_decode_frame,
  371. .close = fdk_aac_decode_close,
  372. .flush = fdk_aac_decode_flush,
  373. .capabilities = CODEC_CAP_DR1 | CODEC_CAP_CHANNEL_CONF,
  374. .priv_class = &fdk_aac_dec_class,
  375. };