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.

199 lines
6.3KB

  1. /*
  2. * Interface to libfaac for aac encoding
  3. * Copyright (c) 2002 Gildas Bazin <gbazin@netcourrier.com>
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg 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. * FFmpeg 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 FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /**
  22. * @file
  23. * Interface to libfaac for aac encoding.
  24. */
  25. #include "avcodec.h"
  26. #include <faac.h>
  27. typedef struct FaacAudioContext {
  28. faacEncHandle faac_handle;
  29. } FaacAudioContext;
  30. static const int channel_maps[][6] = {
  31. { 2, 0, 1 }, //< C L R
  32. { 2, 0, 1, 3 }, //< C L R Cs
  33. { 2, 0, 1, 3, 4 }, //< C L R Ls Rs
  34. { 2, 0, 1, 4, 5, 3 }, //< C L R Ls Rs LFE
  35. };
  36. static av_cold int Faac_encode_close(AVCodecContext *avctx)
  37. {
  38. FaacAudioContext *s = avctx->priv_data;
  39. av_freep(&avctx->coded_frame);
  40. av_freep(&avctx->extradata);
  41. if (s->faac_handle)
  42. faacEncClose(s->faac_handle);
  43. return 0;
  44. }
  45. static av_cold int Faac_encode_init(AVCodecContext *avctx)
  46. {
  47. FaacAudioContext *s = avctx->priv_data;
  48. faacEncConfigurationPtr faac_cfg;
  49. unsigned long samples_input, max_bytes_output;
  50. int ret;
  51. /* number of channels */
  52. if (avctx->channels < 1 || avctx->channels > 6) {
  53. av_log(avctx, AV_LOG_ERROR, "encoding %d channel(s) is not allowed\n", avctx->channels);
  54. ret = AVERROR(EINVAL);
  55. goto error;
  56. }
  57. s->faac_handle = faacEncOpen(avctx->sample_rate,
  58. avctx->channels,
  59. &samples_input, &max_bytes_output);
  60. if (!s->faac_handle) {
  61. av_log(avctx, AV_LOG_ERROR, "error in faacEncOpen()\n");
  62. ret = AVERROR_UNKNOWN;
  63. goto error;
  64. }
  65. /* check faac version */
  66. faac_cfg = faacEncGetCurrentConfiguration(s->faac_handle);
  67. if (faac_cfg->version != FAAC_CFG_VERSION) {
  68. av_log(avctx, AV_LOG_ERROR, "wrong libfaac version (compiled for: %d, using %d)\n", FAAC_CFG_VERSION, faac_cfg->version);
  69. ret = AVERROR(EINVAL);
  70. goto error;
  71. }
  72. /* put the options in the configuration struct */
  73. switch(avctx->profile) {
  74. case FF_PROFILE_AAC_MAIN:
  75. faac_cfg->aacObjectType = MAIN;
  76. break;
  77. case FF_PROFILE_UNKNOWN:
  78. case FF_PROFILE_AAC_LOW:
  79. faac_cfg->aacObjectType = LOW;
  80. break;
  81. case FF_PROFILE_AAC_SSR:
  82. faac_cfg->aacObjectType = SSR;
  83. break;
  84. case FF_PROFILE_AAC_LTP:
  85. faac_cfg->aacObjectType = LTP;
  86. break;
  87. default:
  88. av_log(avctx, AV_LOG_ERROR, "invalid AAC profile\n");
  89. ret = AVERROR(EINVAL);
  90. goto error;
  91. }
  92. faac_cfg->mpegVersion = MPEG4;
  93. faac_cfg->useTns = 0;
  94. faac_cfg->allowMidside = 1;
  95. faac_cfg->bitRate = avctx->bit_rate / avctx->channels;
  96. faac_cfg->bandWidth = avctx->cutoff;
  97. if(avctx->flags & CODEC_FLAG_QSCALE) {
  98. faac_cfg->bitRate = 0;
  99. faac_cfg->quantqual = avctx->global_quality / FF_QP2LAMBDA;
  100. }
  101. faac_cfg->outputFormat = 1;
  102. faac_cfg->inputFormat = FAAC_INPUT_16BIT;
  103. if (avctx->channels > 2)
  104. memcpy(faac_cfg->channel_map, channel_maps[avctx->channels-3],
  105. avctx->channels * sizeof(int));
  106. avctx->frame_size = samples_input / avctx->channels;
  107. avctx->coded_frame= avcodec_alloc_frame();
  108. if (!avctx->coded_frame) {
  109. ret = AVERROR(ENOMEM);
  110. goto error;
  111. }
  112. /* Set decoder specific info */
  113. avctx->extradata_size = 0;
  114. if (avctx->flags & CODEC_FLAG_GLOBAL_HEADER) {
  115. unsigned char *buffer = NULL;
  116. unsigned long decoder_specific_info_size;
  117. if (!faacEncGetDecoderSpecificInfo(s->faac_handle, &buffer,
  118. &decoder_specific_info_size)) {
  119. avctx->extradata = av_malloc(decoder_specific_info_size + FF_INPUT_BUFFER_PADDING_SIZE);
  120. if (!avctx->extradata) {
  121. ret = AVERROR(ENOMEM);
  122. goto error;
  123. }
  124. avctx->extradata_size = decoder_specific_info_size;
  125. memcpy(avctx->extradata, buffer, avctx->extradata_size);
  126. faac_cfg->outputFormat = 0;
  127. }
  128. #undef free
  129. free(buffer);
  130. #define free please_use_av_free
  131. }
  132. if (!faacEncSetConfiguration(s->faac_handle, faac_cfg)) {
  133. av_log(avctx, AV_LOG_ERROR, "libfaac doesn't support this output format!\n");
  134. ret = AVERROR(EINVAL);
  135. goto error;
  136. }
  137. return 0;
  138. error:
  139. Faac_encode_close(avctx);
  140. return ret;
  141. }
  142. static int Faac_encode_frame(AVCodecContext *avctx,
  143. unsigned char *frame, int buf_size, void *data)
  144. {
  145. FaacAudioContext *s = avctx->priv_data;
  146. int bytes_written;
  147. int num_samples = data ? avctx->frame_size : 0;
  148. bytes_written = faacEncEncode(s->faac_handle,
  149. data,
  150. num_samples * avctx->channels,
  151. frame,
  152. buf_size);
  153. return bytes_written;
  154. }
  155. static const AVProfile profiles[] = {
  156. { FF_PROFILE_AAC_MAIN, "Main" },
  157. { FF_PROFILE_AAC_LOW, "LC" },
  158. { FF_PROFILE_AAC_SSR, "SSR" },
  159. { FF_PROFILE_AAC_LTP, "LTP" },
  160. { FF_PROFILE_UNKNOWN },
  161. };
  162. AVCodec ff_libfaac_encoder = {
  163. .name = "libfaac",
  164. .type = AVMEDIA_TYPE_AUDIO,
  165. .id = CODEC_ID_AAC,
  166. .priv_data_size = sizeof(FaacAudioContext),
  167. .init = Faac_encode_init,
  168. .encode = Faac_encode_frame,
  169. .close = Faac_encode_close,
  170. .capabilities = CODEC_CAP_SMALL_LAST_FRAME | CODEC_CAP_DELAY,
  171. .sample_fmts = (const enum AVSampleFormat[]){AV_SAMPLE_FMT_S16,AV_SAMPLE_FMT_NONE},
  172. .long_name = NULL_IF_CONFIG_SMALL("libfaac AAC (Advanced Audio Codec)"),
  173. .profiles = NULL_IF_CONFIG_SMALL(profiles),
  174. };