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.

132 lines
4.0KB

  1. /*
  2. * Interface to libfaac for aac encoding
  3. * Copyright (c) 2002 Gildas Bazin <gbazin@netcourrier.com>
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  18. */
  19. /**
  20. * @file faacaudio.c
  21. * Interface to libfaac for aac encoding.
  22. */
  23. #include "avcodec.h"
  24. #include <faac.h>
  25. typedef struct FaacAudioContext {
  26. faacEncHandle faac_handle;
  27. } FaacAudioContext;
  28. static int Faac_encode_init(AVCodecContext *avctx)
  29. {
  30. FaacAudioContext *s = avctx->priv_data;
  31. faacEncConfigurationPtr faac_cfg;
  32. unsigned long samples_input, max_bytes_output;
  33. /* number of channels */
  34. if (avctx->channels < 1 || avctx->channels > 6)
  35. return -1;
  36. s->faac_handle = faacEncOpen(avctx->sample_rate,
  37. avctx->channels,
  38. &samples_input, &max_bytes_output);
  39. /* check faac version */
  40. faac_cfg = faacEncGetCurrentConfiguration(s->faac_handle);
  41. if (faac_cfg->version != FAAC_CFG_VERSION) {
  42. av_log(avctx, AV_LOG_ERROR, "wrong libfaac version (compiled for: %d, using %d)\n", FAAC_CFG_VERSION, faac_cfg->version);
  43. faacEncClose(s->faac_handle);
  44. return -1;
  45. }
  46. /* put the options in the configuration struct */
  47. faac_cfg->aacObjectType = LOW;
  48. faac_cfg->mpegVersion = MPEG4;
  49. faac_cfg->useTns = 0;
  50. faac_cfg->allowMidside = 1;
  51. faac_cfg->bitRate = avctx->bit_rate / avctx->channels;
  52. faac_cfg->bandWidth = avctx->cutoff;
  53. if(avctx->flags & CODEC_FLAG_QSCALE) {
  54. faac_cfg->bitRate = 0;
  55. faac_cfg->quantqual = avctx->global_quality / FF_QP2LAMBDA;
  56. }
  57. faac_cfg->outputFormat = 1;
  58. faac_cfg->inputFormat = FAAC_INPUT_16BIT;
  59. avctx->frame_size = samples_input / avctx->channels;
  60. avctx->coded_frame= avcodec_alloc_frame();
  61. avctx->coded_frame->key_frame= 1;
  62. /* Set decoder specific info */
  63. avctx->extradata_size = 0;
  64. if (avctx->flags & CODEC_FLAG_GLOBAL_HEADER) {
  65. unsigned char *buffer;
  66. unsigned long decoder_specific_info_size;
  67. if (!faacEncGetDecoderSpecificInfo(s->faac_handle, &buffer,
  68. &decoder_specific_info_size)) {
  69. avctx->extradata = buffer;
  70. avctx->extradata_size = decoder_specific_info_size;
  71. faac_cfg->outputFormat = 0;
  72. }
  73. }
  74. if (!faacEncSetConfiguration(s->faac_handle, faac_cfg)) {
  75. av_log(avctx, AV_LOG_ERROR, "libfaac doesn't support this output format!\n");
  76. return -1;
  77. }
  78. return 0;
  79. }
  80. int Faac_encode_frame(AVCodecContext *avctx,
  81. unsigned char *frame, int buf_size, void *data)
  82. {
  83. FaacAudioContext *s = avctx->priv_data;
  84. int bytes_written;
  85. bytes_written = faacEncEncode(s->faac_handle,
  86. data,
  87. avctx->frame_size * avctx->channels,
  88. frame,
  89. buf_size);
  90. return bytes_written;
  91. }
  92. int Faac_encode_close(AVCodecContext *avctx)
  93. {
  94. FaacAudioContext *s = avctx->priv_data;
  95. av_freep(&avctx->coded_frame);
  96. //if (avctx->extradata_size) free(avctx->extradata);
  97. faacEncClose(s->faac_handle);
  98. return 0;
  99. }
  100. AVCodec faac_encoder = {
  101. "aac",
  102. CODEC_TYPE_AUDIO,
  103. CODEC_ID_AAC,
  104. sizeof(FaacAudioContext),
  105. Faac_encode_init,
  106. Faac_encode_frame,
  107. Faac_encode_close
  108. };