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.

241 lines
7.5KB

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