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.

190 lines
6.0KB

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