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.

230 lines
7.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 <faac.h>
  26. #include "avcodec.h"
  27. #include "audio_frame_queue.h"
  28. #include "internal.h"
  29. /* libfaac has an encoder delay of 1024 samples */
  30. #define FAAC_DELAY_SAMPLES 1024
  31. typedef struct FaacAudioContext {
  32. faacEncHandle faac_handle;
  33. AudioFrameQueue afq;
  34. } FaacAudioContext;
  35. static av_cold int Faac_encode_close(AVCodecContext *avctx)
  36. {
  37. FaacAudioContext *s = avctx->priv_data;
  38. #if FF_API_OLD_ENCODE_AUDIO
  39. av_freep(&avctx->coded_frame);
  40. #endif
  41. av_freep(&avctx->extradata);
  42. ff_af_queue_close(&s->afq);
  43. if (s->faac_handle)
  44. faacEncClose(s->faac_handle);
  45. return 0;
  46. }
  47. static av_cold int Faac_encode_init(AVCodecContext *avctx)
  48. {
  49. FaacAudioContext *s = avctx->priv_data;
  50. faacEncConfigurationPtr faac_cfg;
  51. unsigned long samples_input, max_bytes_output;
  52. int ret;
  53. /* number of channels */
  54. if (avctx->channels < 1 || avctx->channels > 6) {
  55. av_log(avctx, AV_LOG_ERROR, "encoding %d channel(s) is not allowed\n", avctx->channels);
  56. ret = AVERROR(EINVAL);
  57. goto error;
  58. }
  59. s->faac_handle = faacEncOpen(avctx->sample_rate,
  60. avctx->channels,
  61. &samples_input, &max_bytes_output);
  62. if (!s->faac_handle) {
  63. av_log(avctx, AV_LOG_ERROR, "error in faacEncOpen()\n");
  64. ret = AVERROR_UNKNOWN;
  65. goto error;
  66. }
  67. /* check faac version */
  68. faac_cfg = faacEncGetCurrentConfiguration(s->faac_handle);
  69. if (faac_cfg->version != FAAC_CFG_VERSION) {
  70. av_log(avctx, AV_LOG_ERROR, "wrong libfaac version (compiled for: %d, using %d)\n", FAAC_CFG_VERSION, faac_cfg->version);
  71. ret = AVERROR(EINVAL);
  72. goto error;
  73. }
  74. /* put the options in the configuration struct */
  75. switch(avctx->profile) {
  76. case FF_PROFILE_AAC_MAIN:
  77. faac_cfg->aacObjectType = MAIN;
  78. break;
  79. case FF_PROFILE_UNKNOWN:
  80. case FF_PROFILE_AAC_LOW:
  81. faac_cfg->aacObjectType = LOW;
  82. break;
  83. case FF_PROFILE_AAC_SSR:
  84. faac_cfg->aacObjectType = SSR;
  85. break;
  86. case FF_PROFILE_AAC_LTP:
  87. faac_cfg->aacObjectType = LTP;
  88. break;
  89. default:
  90. av_log(avctx, AV_LOG_ERROR, "invalid AAC profile\n");
  91. ret = AVERROR(EINVAL);
  92. goto error;
  93. }
  94. faac_cfg->mpegVersion = MPEG4;
  95. faac_cfg->useTns = 0;
  96. faac_cfg->allowMidside = 1;
  97. faac_cfg->bitRate = avctx->bit_rate / avctx->channels;
  98. faac_cfg->bandWidth = avctx->cutoff;
  99. if(avctx->flags & CODEC_FLAG_QSCALE) {
  100. faac_cfg->bitRate = 0;
  101. faac_cfg->quantqual = avctx->global_quality / FF_QP2LAMBDA;
  102. }
  103. faac_cfg->outputFormat = 1;
  104. faac_cfg->inputFormat = FAAC_INPUT_16BIT;
  105. avctx->frame_size = samples_input / avctx->channels;
  106. #if FF_API_OLD_ENCODE_AUDIO
  107. avctx->coded_frame= avcodec_alloc_frame();
  108. if (!avctx->coded_frame) {
  109. ret = AVERROR(ENOMEM);
  110. goto error;
  111. }
  112. #endif
  113. /* Set decoder specific info */
  114. avctx->extradata_size = 0;
  115. if (avctx->flags & CODEC_FLAG_GLOBAL_HEADER) {
  116. unsigned char *buffer = NULL;
  117. unsigned long decoder_specific_info_size;
  118. if (!faacEncGetDecoderSpecificInfo(s->faac_handle, &buffer,
  119. &decoder_specific_info_size)) {
  120. avctx->extradata = av_malloc(decoder_specific_info_size + FF_INPUT_BUFFER_PADDING_SIZE);
  121. if (!avctx->extradata) {
  122. ret = AVERROR(ENOMEM);
  123. goto error;
  124. }
  125. avctx->extradata_size = decoder_specific_info_size;
  126. memcpy(avctx->extradata, buffer, avctx->extradata_size);
  127. faac_cfg->outputFormat = 0;
  128. }
  129. #undef free
  130. free(buffer);
  131. #define free please_use_av_free
  132. }
  133. if (!faacEncSetConfiguration(s->faac_handle, faac_cfg)) {
  134. av_log(avctx, AV_LOG_ERROR, "libfaac doesn't support this output format!\n");
  135. ret = AVERROR(EINVAL);
  136. goto error;
  137. }
  138. avctx->delay = FAAC_DELAY_SAMPLES;
  139. ff_af_queue_init(avctx, &s->afq);
  140. return 0;
  141. error:
  142. Faac_encode_close(avctx);
  143. return ret;
  144. }
  145. static int Faac_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
  146. const AVFrame *frame, int *got_packet_ptr)
  147. {
  148. FaacAudioContext *s = avctx->priv_data;
  149. int bytes_written, ret;
  150. int num_samples = frame ? frame->nb_samples : 0;
  151. void *samples = frame ? frame->data[0] : NULL;
  152. if ((ret = ff_alloc_packet(avpkt, (7 + 768) * avctx->channels))) {
  153. av_log(avctx, AV_LOG_ERROR, "Error getting output packet\n");
  154. return ret;
  155. }
  156. bytes_written = faacEncEncode(s->faac_handle, samples,
  157. num_samples * avctx->channels,
  158. avpkt->data, avpkt->size);
  159. if (bytes_written < 0) {
  160. av_log(avctx, AV_LOG_ERROR, "faacEncEncode() error\n");
  161. return bytes_written;
  162. }
  163. /* add current frame to the queue */
  164. if (frame) {
  165. if ((ret = ff_af_queue_add(&s->afq, frame) < 0))
  166. return ret;
  167. }
  168. if (!bytes_written)
  169. return 0;
  170. /* Get the next frame pts/duration */
  171. ff_af_queue_remove(&s->afq, avctx->frame_size, &avpkt->pts,
  172. &avpkt->duration);
  173. avpkt->size = bytes_written;
  174. *got_packet_ptr = 1;
  175. return 0;
  176. }
  177. static const AVProfile profiles[] = {
  178. { FF_PROFILE_AAC_MAIN, "Main" },
  179. { FF_PROFILE_AAC_LOW, "LC" },
  180. { FF_PROFILE_AAC_SSR, "SSR" },
  181. { FF_PROFILE_AAC_LTP, "LTP" },
  182. { FF_PROFILE_UNKNOWN },
  183. };
  184. AVCodec ff_libfaac_encoder = {
  185. .name = "libfaac",
  186. .type = AVMEDIA_TYPE_AUDIO,
  187. .id = CODEC_ID_AAC,
  188. .priv_data_size = sizeof(FaacAudioContext),
  189. .init = Faac_encode_init,
  190. .encode2 = Faac_encode_frame,
  191. .close = Faac_encode_close,
  192. .capabilities = CODEC_CAP_SMALL_LAST_FRAME | CODEC_CAP_DELAY,
  193. .sample_fmts = (const enum AVSampleFormat[]){AV_SAMPLE_FMT_S16,AV_SAMPLE_FMT_NONE},
  194. .long_name = NULL_IF_CONFIG_SMALL("libfaac AAC (Advanced Audio Codec)"),
  195. .profiles = NULL_IF_CONFIG_SMALL(profiles),
  196. };