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.

300 lines
9.9KB

  1. /*
  2. * AAC decoder wrapper
  3. * Copyright (c) 2012 Martin Storsjo
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * Libav is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with Libav; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include <fdk-aac/aacdecoder_lib.h>
  22. #include "libavutil/channel_layout.h"
  23. #include "libavutil/common.h"
  24. #include "libavutil/opt.h"
  25. #include "avcodec.h"
  26. #include "internal.h"
  27. enum ConcealMethod {
  28. CONCEAL_METHOD_DEFAULT = -1,
  29. CONCEAL_METHOD_SPECTRAL_MUTING = 0,
  30. CONCEAL_METHOD_NOISE_SUBSTITUTION = 1,
  31. CONCEAL_METHOD_ENERGY_INTERPOLATION = 2,
  32. CONCEAL_METHOD_NB,
  33. };
  34. typedef struct FDKAACDecContext {
  35. const AVClass *class;
  36. HANDLE_AACDECODER handle;
  37. int initialized;
  38. enum ConcealMethod conceal_method;
  39. } FDKAACDecContext;
  40. #define OFFSET(x) offsetof(FDKAACDecContext, x)
  41. #define AD AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_DECODING_PARAM
  42. static const AVOption fdk_aac_dec_options[] = {
  43. { "conceal", "Error concealment method", OFFSET(conceal_method), AV_OPT_TYPE_INT, { .i64 = CONCEAL_METHOD_DEFAULT }, CONCEAL_METHOD_DEFAULT, CONCEAL_METHOD_NB - 1, AD, "conceal" },
  44. { "default", "Default", 0, AV_OPT_TYPE_CONST, { .i64 = CONCEAL_METHOD_DEFAULT }, INT_MIN, INT_MAX, AD, "conceal" },
  45. { "spectral", "Spectral muting", 0, AV_OPT_TYPE_CONST, { .i64 = CONCEAL_METHOD_SPECTRAL_MUTING }, INT_MIN, INT_MAX, AD, "conceal" },
  46. { "noise", "Noise Substitution", 0, AV_OPT_TYPE_CONST, { .i64 = CONCEAL_METHOD_NOISE_SUBSTITUTION }, INT_MIN, INT_MAX, AD, "conceal" },
  47. { "energy", "Energy Interpolation", 0, AV_OPT_TYPE_CONST, { .i64 = CONCEAL_METHOD_ENERGY_INTERPOLATION }, INT_MIN, INT_MAX, AD, "conceal" },
  48. { NULL }
  49. };
  50. static const AVClass fdk_aac_dec_class = {
  51. "libfdk-aac decoder", av_default_item_name, fdk_aac_dec_options, LIBAVUTIL_VERSION_INT
  52. };
  53. static int get_stream_info(AVCodecContext *avctx)
  54. {
  55. FDKAACDecContext *s = avctx->priv_data;
  56. CStreamInfo *info = aacDecoder_GetStreamInfo(s->handle);
  57. int channel_counts[9] = { 0 };
  58. int i, ch_error = 0;
  59. uint64_t ch_layout = 0;
  60. if (!info) {
  61. av_log(avctx, AV_LOG_ERROR, "Unable to get stream info\n");
  62. return AVERROR_UNKNOWN;
  63. }
  64. if (info->sampleRate <= 0) {
  65. av_log(avctx, AV_LOG_ERROR, "Stream info not initialized\n");
  66. return AVERROR_UNKNOWN;
  67. }
  68. avctx->sample_rate = info->sampleRate;
  69. avctx->frame_size = info->frameSize;
  70. for (i = 0; i < info->numChannels; i++) {
  71. AUDIO_CHANNEL_TYPE ctype = info->pChannelType[i];
  72. if (ctype <= ACT_NONE || ctype > ACT_TOP) {
  73. av_log(avctx, AV_LOG_WARNING, "unknown channel type\n");
  74. break;
  75. }
  76. channel_counts[ctype]++;
  77. }
  78. av_log(avctx, AV_LOG_DEBUG, "%d channels - front:%d side:%d back:%d lfe:%d top:%d\n",
  79. info->numChannels,
  80. channel_counts[ACT_FRONT], channel_counts[ACT_SIDE],
  81. channel_counts[ACT_BACK], channel_counts[ACT_LFE],
  82. channel_counts[ACT_FRONT_TOP] + channel_counts[ACT_SIDE_TOP] +
  83. channel_counts[ACT_BACK_TOP] + channel_counts[ACT_TOP]);
  84. switch (channel_counts[ACT_FRONT]) {
  85. case 4:
  86. ch_layout |= AV_CH_LAYOUT_STEREO | AV_CH_FRONT_LEFT_OF_CENTER | AV_CH_FRONT_RIGHT_OF_CENTER;
  87. break;
  88. case 3:
  89. ch_layout |= AV_CH_LAYOUT_STEREO | AV_CH_FRONT_CENTER;
  90. break;
  91. case 2:
  92. ch_layout |= AV_CH_LAYOUT_STEREO;
  93. break;
  94. case 1:
  95. ch_layout |= AV_CH_FRONT_CENTER;
  96. break;
  97. default:
  98. av_log(avctx, AV_LOG_WARNING, "unsupported number of front channels: %d\n",
  99. channel_counts[ACT_FRONT]);
  100. ch_error = 1;
  101. break;
  102. }
  103. if (channel_counts[ACT_SIDE] > 0) {
  104. if (channel_counts[ACT_SIDE] == 2) {
  105. ch_layout |= AV_CH_SIDE_LEFT | AV_CH_SIDE_RIGHT;
  106. } else {
  107. av_log(avctx, AV_LOG_WARNING, "unsupported number of side channels: %d\n",
  108. channel_counts[ACT_SIDE]);
  109. ch_error = 1;
  110. }
  111. }
  112. if (channel_counts[ACT_BACK] > 0) {
  113. switch (channel_counts[ACT_BACK]) {
  114. case 3:
  115. ch_layout |= AV_CH_BACK_LEFT | AV_CH_BACK_RIGHT | AV_CH_BACK_CENTER;
  116. break;
  117. case 2:
  118. ch_layout |= AV_CH_BACK_LEFT | AV_CH_BACK_RIGHT;
  119. break;
  120. case 1:
  121. ch_layout |= AV_CH_BACK_CENTER;
  122. break;
  123. default:
  124. av_log(avctx, AV_LOG_WARNING, "unsupported number of back channels: %d\n",
  125. channel_counts[ACT_BACK]);
  126. ch_error = 1;
  127. break;
  128. }
  129. }
  130. if (channel_counts[ACT_LFE] > 0) {
  131. if (channel_counts[ACT_LFE] == 1) {
  132. ch_layout |= AV_CH_LOW_FREQUENCY;
  133. } else {
  134. av_log(avctx, AV_LOG_WARNING, "unsupported number of LFE channels: %d\n",
  135. channel_counts[ACT_LFE]);
  136. ch_error = 1;
  137. }
  138. }
  139. if (!ch_error &&
  140. av_get_channel_layout_nb_channels(ch_layout) != info->numChannels) {
  141. av_log(avctx, AV_LOG_WARNING, "unsupported channel configuration\n");
  142. ch_error = 1;
  143. }
  144. if (ch_error)
  145. avctx->channel_layout = 0;
  146. else
  147. avctx->channel_layout = ch_layout;
  148. avctx->channels = info->numChannels;
  149. return 0;
  150. }
  151. static av_cold int fdk_aac_decode_close(AVCodecContext *avctx)
  152. {
  153. FDKAACDecContext *s = avctx->priv_data;
  154. if (s->handle)
  155. aacDecoder_Close(s->handle);
  156. return 0;
  157. }
  158. static av_cold int fdk_aac_decode_init(AVCodecContext *avctx)
  159. {
  160. FDKAACDecContext *s = avctx->priv_data;
  161. AAC_DECODER_ERROR err;
  162. s->handle = aacDecoder_Open(avctx->extradata_size ? TT_MP4_RAW : TT_MP4_ADTS, 1);
  163. if (!s->handle) {
  164. av_log(avctx, AV_LOG_ERROR, "Error opening decoder\n");
  165. return AVERROR_UNKNOWN;
  166. }
  167. if (avctx->extradata_size) {
  168. if ((err = aacDecoder_ConfigRaw(s->handle, &avctx->extradata, &avctx->extradata_size)) != AAC_DEC_OK) {
  169. av_log(avctx, AV_LOG_ERROR, "Unable to set extradata\n");
  170. return AVERROR_INVALIDDATA;
  171. }
  172. }
  173. if (s->conceal_method != CONCEAL_METHOD_DEFAULT) {
  174. if ((err = aacDecoder_SetParam(s->handle, AAC_CONCEAL_METHOD, s->conceal_method)) != AAC_DEC_OK) {
  175. av_log(avctx, AV_LOG_ERROR, "Unable to set error concealment method\n");
  176. return AVERROR_UNKNOWN;
  177. }
  178. }
  179. avctx->sample_fmt = AV_SAMPLE_FMT_S16;
  180. return 0;
  181. }
  182. static int fdk_aac_decode_frame(AVCodecContext *avctx, void *data,
  183. int *got_frame_ptr, AVPacket *avpkt)
  184. {
  185. FDKAACDecContext *s = avctx->priv_data;
  186. AVFrame *frame = data;
  187. int ret;
  188. AAC_DECODER_ERROR err;
  189. UINT valid = avpkt->size;
  190. uint8_t *buf, *tmpptr = NULL;
  191. int buf_size;
  192. err = aacDecoder_Fill(s->handle, &avpkt->data, &avpkt->size, &valid);
  193. if (err != AAC_DEC_OK) {
  194. av_log(avctx, AV_LOG_ERROR, "aacDecoder_Fill() failed: %x\n", err);
  195. return AVERROR_INVALIDDATA;
  196. }
  197. if (s->initialized) {
  198. frame->nb_samples = avctx->frame_size;
  199. if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) {
  200. av_log(avctx, AV_LOG_ERROR, "ff_get_buffer() failed\n");
  201. return ret;
  202. }
  203. buf = frame->extended_data[0];
  204. buf_size = avctx->channels * frame->nb_samples *
  205. av_get_bytes_per_sample(avctx->sample_fmt);
  206. } else {
  207. buf_size = 50 * 1024;
  208. buf = tmpptr = av_malloc(buf_size);
  209. if (!buf)
  210. return AVERROR(ENOMEM);
  211. }
  212. err = aacDecoder_DecodeFrame(s->handle, (INT_PCM *) buf, buf_size, 0);
  213. if (err == AAC_DEC_NOT_ENOUGH_BITS) {
  214. ret = avpkt->size - valid;
  215. goto end;
  216. }
  217. if (err != AAC_DEC_OK) {
  218. av_log(avctx, AV_LOG_ERROR, "aacDecoder_DecodeFrame() failed: %x\n", err);
  219. ret = AVERROR_UNKNOWN;
  220. goto end;
  221. }
  222. if (!s->initialized) {
  223. if ((ret = get_stream_info(avctx)) < 0)
  224. goto end;
  225. s->initialized = 1;
  226. frame->nb_samples = avctx->frame_size;
  227. }
  228. if (tmpptr) {
  229. frame->nb_samples = avctx->frame_size;
  230. if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) {
  231. av_log(avctx, AV_LOG_ERROR, "ff_get_buffer() failed\n");
  232. goto end;
  233. }
  234. memcpy(frame->extended_data[0], tmpptr,
  235. avctx->channels * avctx->frame_size *
  236. av_get_bytes_per_sample(avctx->sample_fmt));
  237. }
  238. *got_frame_ptr = 1;
  239. ret = avpkt->size - valid;
  240. end:
  241. av_free(tmpptr);
  242. return ret;
  243. }
  244. static av_cold void fdk_aac_decode_flush(AVCodecContext *avctx)
  245. {
  246. FDKAACDecContext *s = avctx->priv_data;
  247. AAC_DECODER_ERROR err;
  248. if (!s->handle)
  249. return;
  250. if ((err = aacDecoder_SetParam(s->handle, AAC_TPDEC_CLEAR_BUFFER, 1)) != AAC_DEC_OK)
  251. av_log(avctx, AV_LOG_WARNING, "failed to clear buffer when flushing\n");
  252. }
  253. AVCodec ff_libfdk_aac_decoder = {
  254. .name = "libfdk_aac",
  255. .long_name = NULL_IF_CONFIG_SMALL("Fraunhofer FDK AAC"),
  256. .type = AVMEDIA_TYPE_AUDIO,
  257. .id = AV_CODEC_ID_AAC,
  258. .priv_data_size = sizeof(FDKAACDecContext),
  259. .init = fdk_aac_decode_init,
  260. .decode = fdk_aac_decode_frame,
  261. .close = fdk_aac_decode_close,
  262. .flush = fdk_aac_decode_flush,
  263. .capabilities = CODEC_CAP_DR1,
  264. .priv_class = &fdk_aac_dec_class,
  265. };