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.

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