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.

320 lines
11KB

  1. /*
  2. * AAC decoder wrapper
  3. * Copyright (c) 2012 Martin Storsjo
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are
  9. * met:
  10. *
  11. * * Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. *
  14. * * Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in
  16. * the documentation and/or other materials provided with the
  17. * distribution.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  23. * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  25. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. */
  31. #include <fdk-aac/aacdecoder_lib.h>
  32. #include "libavutil/channel_layout.h"
  33. #include "libavutil/common.h"
  34. #include "libavutil/opt.h"
  35. #include "avcodec.h"
  36. #include "internal.h"
  37. enum ConcealMethod {
  38. CONCEAL_METHOD_DEFAULT = -1,
  39. CONCEAL_METHOD_SPECTRAL_MUTING = 0,
  40. CONCEAL_METHOD_NOISE_SUBSTITUTION = 1,
  41. CONCEAL_METHOD_ENERGY_INTERPOLATION = 2,
  42. CONCEAL_METHOD_NB,
  43. };
  44. typedef struct FDKAACDecContext {
  45. const AVClass *class;
  46. HANDLE_AACDECODER handle;
  47. int initialized;
  48. enum ConcealMethod conceal_method;
  49. } FDKAACDecContext;
  50. #define OFFSET(x) offsetof(FDKAACDecContext, x)
  51. #define AD AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_DECODING_PARAM
  52. static const AVOption fdk_aac_dec_options[] = {
  53. { "conceal", "Error concealment method", OFFSET(conceal_method), AV_OPT_TYPE_INT, { .i64 = CONCEAL_METHOD_DEFAULT }, CONCEAL_METHOD_DEFAULT, CONCEAL_METHOD_NB - 1, AD, "conceal" },
  54. { "default", "Default", 0, AV_OPT_TYPE_CONST, { .i64 = CONCEAL_METHOD_DEFAULT }, INT_MIN, INT_MAX, 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. { NULL }
  59. };
  60. static const AVClass fdk_aac_dec_class = {
  61. "libfdk-aac decoder", av_default_item_name, fdk_aac_dec_options, LIBAVUTIL_VERSION_INT
  62. };
  63. static int get_stream_info(AVCodecContext *avctx)
  64. {
  65. FDKAACDecContext *s = avctx->priv_data;
  66. CStreamInfo *info = aacDecoder_GetStreamInfo(s->handle);
  67. int channel_counts[9] = { 0 };
  68. int i, ch_error = 0;
  69. uint64_t ch_layout = 0;
  70. if (!info) {
  71. av_log(avctx, AV_LOG_ERROR, "Unable to get stream info\n");
  72. return AVERROR_UNKNOWN;
  73. }
  74. if (info->sampleRate <= 0) {
  75. av_log(avctx, AV_LOG_ERROR, "Stream info not initialized\n");
  76. return AVERROR_UNKNOWN;
  77. }
  78. avctx->sample_rate = info->sampleRate;
  79. avctx->frame_size = info->frameSize;
  80. for (i = 0; i < info->numChannels; i++) {
  81. AUDIO_CHANNEL_TYPE ctype = info->pChannelType[i];
  82. if (ctype <= ACT_NONE || ctype > ACT_TOP) {
  83. av_log(avctx, AV_LOG_WARNING, "unknown channel type\n");
  84. break;
  85. }
  86. channel_counts[ctype]++;
  87. }
  88. av_log(avctx, AV_LOG_DEBUG,
  89. "%d channels - front:%d side:%d back:%d lfe:%d top:%d\n",
  90. info->numChannels,
  91. channel_counts[ACT_FRONT], channel_counts[ACT_SIDE],
  92. channel_counts[ACT_BACK], channel_counts[ACT_LFE],
  93. channel_counts[ACT_FRONT_TOP] + channel_counts[ACT_SIDE_TOP] +
  94. channel_counts[ACT_BACK_TOP] + channel_counts[ACT_TOP]);
  95. switch (channel_counts[ACT_FRONT]) {
  96. case 4:
  97. ch_layout |= AV_CH_LAYOUT_STEREO | AV_CH_FRONT_LEFT_OF_CENTER |
  98. AV_CH_FRONT_RIGHT_OF_CENTER;
  99. break;
  100. case 3:
  101. ch_layout |= AV_CH_LAYOUT_STEREO | AV_CH_FRONT_CENTER;
  102. break;
  103. case 2:
  104. ch_layout |= AV_CH_LAYOUT_STEREO;
  105. break;
  106. case 1:
  107. ch_layout |= AV_CH_FRONT_CENTER;
  108. break;
  109. default:
  110. av_log(avctx, AV_LOG_WARNING,
  111. "unsupported number of front channels: %d\n",
  112. channel_counts[ACT_FRONT]);
  113. ch_error = 1;
  114. break;
  115. }
  116. if (channel_counts[ACT_SIDE] > 0) {
  117. if (channel_counts[ACT_SIDE] == 2) {
  118. ch_layout |= AV_CH_SIDE_LEFT | AV_CH_SIDE_RIGHT;
  119. } else {
  120. av_log(avctx, AV_LOG_WARNING,
  121. "unsupported number of side channels: %d\n",
  122. channel_counts[ACT_SIDE]);
  123. ch_error = 1;
  124. }
  125. }
  126. if (channel_counts[ACT_BACK] > 0) {
  127. switch (channel_counts[ACT_BACK]) {
  128. case 3:
  129. ch_layout |= AV_CH_BACK_LEFT | AV_CH_BACK_RIGHT | AV_CH_BACK_CENTER;
  130. break;
  131. case 2:
  132. ch_layout |= AV_CH_BACK_LEFT | AV_CH_BACK_RIGHT;
  133. break;
  134. case 1:
  135. ch_layout |= AV_CH_BACK_CENTER;
  136. break;
  137. default:
  138. av_log(avctx, AV_LOG_WARNING,
  139. "unsupported number of back channels: %d\n",
  140. channel_counts[ACT_BACK]);
  141. ch_error = 1;
  142. break;
  143. }
  144. }
  145. if (channel_counts[ACT_LFE] > 0) {
  146. if (channel_counts[ACT_LFE] == 1) {
  147. ch_layout |= AV_CH_LOW_FREQUENCY;
  148. } else {
  149. av_log(avctx, AV_LOG_WARNING,
  150. "unsupported number of LFE channels: %d\n",
  151. channel_counts[ACT_LFE]);
  152. ch_error = 1;
  153. }
  154. }
  155. if (!ch_error &&
  156. av_get_channel_layout_nb_channels(ch_layout) != info->numChannels) {
  157. av_log(avctx, AV_LOG_WARNING, "unsupported channel configuration\n");
  158. ch_error = 1;
  159. }
  160. if (ch_error)
  161. avctx->channel_layout = 0;
  162. else
  163. avctx->channel_layout = ch_layout;
  164. avctx->channels = info->numChannels;
  165. return 0;
  166. }
  167. static av_cold int fdk_aac_decode_close(AVCodecContext *avctx)
  168. {
  169. FDKAACDecContext *s = avctx->priv_data;
  170. if (s->handle)
  171. aacDecoder_Close(s->handle);
  172. return 0;
  173. }
  174. static av_cold int fdk_aac_decode_init(AVCodecContext *avctx)
  175. {
  176. FDKAACDecContext *s = avctx->priv_data;
  177. AAC_DECODER_ERROR err;
  178. s->handle = aacDecoder_Open(avctx->extradata_size ? TT_MP4_RAW : TT_MP4_ADTS, 1);
  179. if (!s->handle) {
  180. av_log(avctx, AV_LOG_ERROR, "Error opening decoder\n");
  181. return AVERROR_UNKNOWN;
  182. }
  183. if (avctx->extradata_size) {
  184. if ((err = aacDecoder_ConfigRaw(s->handle, &avctx->extradata,
  185. &avctx->extradata_size)) != AAC_DEC_OK) {
  186. av_log(avctx, AV_LOG_ERROR, "Unable to set extradata\n");
  187. return AVERROR_INVALIDDATA;
  188. }
  189. }
  190. if (s->conceal_method != CONCEAL_METHOD_DEFAULT) {
  191. if ((err = aacDecoder_SetParam(s->handle, AAC_CONCEAL_METHOD,
  192. s->conceal_method)) != AAC_DEC_OK) {
  193. av_log(avctx, AV_LOG_ERROR, "Unable to set error concealment method\n");
  194. return AVERROR_UNKNOWN;
  195. }
  196. }
  197. avctx->sample_fmt = AV_SAMPLE_FMT_S16;
  198. return 0;
  199. }
  200. static int fdk_aac_decode_frame(AVCodecContext *avctx, void *data,
  201. int *got_frame_ptr, AVPacket *avpkt)
  202. {
  203. FDKAACDecContext *s = avctx->priv_data;
  204. AVFrame *frame = data;
  205. int ret;
  206. AAC_DECODER_ERROR err;
  207. UINT valid = avpkt->size;
  208. uint8_t *buf, *tmpptr = NULL;
  209. int buf_size;
  210. err = aacDecoder_Fill(s->handle, &avpkt->data, &avpkt->size, &valid);
  211. if (err != AAC_DEC_OK) {
  212. av_log(avctx, AV_LOG_ERROR, "aacDecoder_Fill() failed: %x\n", err);
  213. return AVERROR_INVALIDDATA;
  214. }
  215. if (s->initialized) {
  216. frame->nb_samples = avctx->frame_size;
  217. if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) {
  218. av_log(avctx, AV_LOG_ERROR, "ff_get_buffer() failed\n");
  219. return ret;
  220. }
  221. buf = frame->extended_data[0];
  222. buf_size = avctx->channels * frame->nb_samples *
  223. av_get_bytes_per_sample(avctx->sample_fmt);
  224. } else {
  225. buf_size = 50 * 1024;
  226. buf = tmpptr = av_malloc(buf_size);
  227. if (!buf)
  228. return AVERROR(ENOMEM);
  229. }
  230. err = aacDecoder_DecodeFrame(s->handle, (INT_PCM *) buf, buf_size, 0);
  231. if (err == AAC_DEC_NOT_ENOUGH_BITS) {
  232. ret = avpkt->size - valid;
  233. goto end;
  234. }
  235. if (err != AAC_DEC_OK) {
  236. av_log(avctx, AV_LOG_ERROR,
  237. "aacDecoder_DecodeFrame() failed: %x\n", err);
  238. ret = AVERROR_UNKNOWN;
  239. goto end;
  240. }
  241. if (!s->initialized) {
  242. if ((ret = get_stream_info(avctx)) < 0)
  243. goto end;
  244. s->initialized = 1;
  245. frame->nb_samples = avctx->frame_size;
  246. }
  247. if (tmpptr) {
  248. frame->nb_samples = avctx->frame_size;
  249. if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) {
  250. av_log(avctx, AV_LOG_ERROR, "ff_get_buffer() failed\n");
  251. goto end;
  252. }
  253. memcpy(frame->extended_data[0], tmpptr,
  254. avctx->channels * avctx->frame_size *
  255. av_get_bytes_per_sample(avctx->sample_fmt));
  256. }
  257. *got_frame_ptr = 1;
  258. ret = avpkt->size - valid;
  259. end:
  260. av_free(tmpptr);
  261. return ret;
  262. }
  263. static av_cold void fdk_aac_decode_flush(AVCodecContext *avctx)
  264. {
  265. FDKAACDecContext *s = avctx->priv_data;
  266. AAC_DECODER_ERROR err;
  267. if (!s->handle)
  268. return;
  269. if ((err = aacDecoder_SetParam(s->handle,
  270. AAC_TPDEC_CLEAR_BUFFER, 1)) != AAC_DEC_OK)
  271. av_log(avctx, AV_LOG_WARNING, "failed to clear buffer when flushing\n");
  272. }
  273. AVCodec ff_libfdk_aac_decoder = {
  274. .name = "libfdk_aac",
  275. .long_name = NULL_IF_CONFIG_SMALL("Fraunhofer FDK AAC"),
  276. .type = AVMEDIA_TYPE_AUDIO,
  277. .id = AV_CODEC_ID_AAC,
  278. .priv_data_size = sizeof(FDKAACDecContext),
  279. .init = fdk_aac_decode_init,
  280. .decode = fdk_aac_decode_frame,
  281. .close = fdk_aac_decode_close,
  282. .flush = fdk_aac_decode_flush,
  283. .capabilities = CODEC_CAP_DR1,
  284. .priv_class = &fdk_aac_dec_class,
  285. };