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.

157 lines
4.9KB

  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 libfaac.c
  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_init(AVCodecContext *avctx)
  31. {
  32. FaacAudioContext *s = avctx->priv_data;
  33. faacEncConfigurationPtr faac_cfg;
  34. unsigned long samples_input, max_bytes_output;
  35. /* number of channels */
  36. if (avctx->channels < 1 || avctx->channels > 6)
  37. return -1;
  38. s->faac_handle = faacEncOpen(avctx->sample_rate,
  39. avctx->channels,
  40. &samples_input, &max_bytes_output);
  41. /* check faac version */
  42. faac_cfg = faacEncGetCurrentConfiguration(s->faac_handle);
  43. if (faac_cfg->version != FAAC_CFG_VERSION) {
  44. av_log(avctx, AV_LOG_ERROR, "wrong libfaac version (compiled for: %d, using %d)\n", FAAC_CFG_VERSION, faac_cfg->version);
  45. faacEncClose(s->faac_handle);
  46. return -1;
  47. }
  48. /* put the options in the configuration struct */
  49. switch(avctx->profile) {
  50. case FF_PROFILE_AAC_MAIN:
  51. faac_cfg->aacObjectType = MAIN;
  52. break;
  53. case FF_PROFILE_UNKNOWN:
  54. case FF_PROFILE_AAC_LOW:
  55. faac_cfg->aacObjectType = LOW;
  56. break;
  57. case FF_PROFILE_AAC_SSR:
  58. faac_cfg->aacObjectType = SSR;
  59. break;
  60. case FF_PROFILE_AAC_LTP:
  61. faac_cfg->aacObjectType = LTP;
  62. break;
  63. default:
  64. av_log(avctx, AV_LOG_ERROR, "invalid AAC profile\n");
  65. faacEncClose(s->faac_handle);
  66. return -1;
  67. }
  68. faac_cfg->mpegVersion = MPEG4;
  69. faac_cfg->useTns = 0;
  70. faac_cfg->allowMidside = 1;
  71. faac_cfg->bitRate = avctx->bit_rate / avctx->channels;
  72. faac_cfg->bandWidth = avctx->cutoff;
  73. if(avctx->flags & CODEC_FLAG_QSCALE) {
  74. faac_cfg->bitRate = 0;
  75. faac_cfg->quantqual = avctx->global_quality / FF_QP2LAMBDA;
  76. }
  77. faac_cfg->outputFormat = 1;
  78. faac_cfg->inputFormat = FAAC_INPUT_16BIT;
  79. avctx->frame_size = samples_input / avctx->channels;
  80. avctx->coded_frame= avcodec_alloc_frame();
  81. avctx->coded_frame->key_frame= 1;
  82. /* Set decoder specific info */
  83. avctx->extradata_size = 0;
  84. if (avctx->flags & CODEC_FLAG_GLOBAL_HEADER) {
  85. unsigned char *buffer = NULL;
  86. unsigned long decoder_specific_info_size;
  87. if (!faacEncGetDecoderSpecificInfo(s->faac_handle, &buffer,
  88. &decoder_specific_info_size)) {
  89. avctx->extradata = av_malloc(decoder_specific_info_size + FF_INPUT_BUFFER_PADDING_SIZE);
  90. avctx->extradata_size = decoder_specific_info_size;
  91. memcpy(avctx->extradata, buffer, avctx->extradata_size);
  92. faac_cfg->outputFormat = 0;
  93. }
  94. #undef free
  95. free(buffer);
  96. #define free please_use_av_free
  97. }
  98. if (!faacEncSetConfiguration(s->faac_handle, faac_cfg)) {
  99. av_log(avctx, AV_LOG_ERROR, "libfaac doesn't support this output format!\n");
  100. return -1;
  101. }
  102. return 0;
  103. }
  104. static int Faac_encode_frame(AVCodecContext *avctx,
  105. unsigned char *frame, int buf_size, void *data)
  106. {
  107. FaacAudioContext *s = avctx->priv_data;
  108. int bytes_written;
  109. bytes_written = faacEncEncode(s->faac_handle,
  110. data,
  111. avctx->frame_size * avctx->channels,
  112. frame,
  113. buf_size);
  114. return bytes_written;
  115. }
  116. static av_cold int Faac_encode_close(AVCodecContext *avctx)
  117. {
  118. FaacAudioContext *s = avctx->priv_data;
  119. av_freep(&avctx->coded_frame);
  120. av_freep(&avctx->extradata);
  121. faacEncClose(s->faac_handle);
  122. return 0;
  123. }
  124. AVCodec libfaac_encoder = {
  125. "libfaac",
  126. CODEC_TYPE_AUDIO,
  127. CODEC_ID_AAC,
  128. sizeof(FaacAudioContext),
  129. Faac_encode_init,
  130. Faac_encode_frame,
  131. Faac_encode_close,
  132. .sample_fmts = (enum SampleFormat[]){SAMPLE_FMT_S16,SAMPLE_FMT_NONE},
  133. .long_name = NULL_IF_CONFIG_SMALL("libfaac AAC (Advanced Audio Codec)"),
  134. };