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.

170 lines
5.4KB

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