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.

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