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.

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