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.

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