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.

253 lines
7.7KB

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