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