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.

243 lines
7.3KB

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