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.

137 lines
4.5KB

  1. /*
  2. * Interface to libaacplus for aac+ (sbr+ps) encoding
  3. * Copyright (c) 2010 tipok <piratfm@gmail.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 libaacplus for aac+ (sbr+ps) encoding.
  24. */
  25. #include "avcodec.h"
  26. #include <aacplus.h>
  27. typedef struct aacPlusAudioContext {
  28. aacplusEncHandle aacplus_handle;
  29. } aacPlusAudioContext;
  30. static av_cold int aacPlus_encode_init(AVCodecContext *avctx)
  31. {
  32. aacPlusAudioContext *s = avctx->priv_data;
  33. aacplusEncConfiguration *aacplus_cfg;
  34. unsigned long samples_input, max_bytes_output;
  35. /* number of channels */
  36. if (avctx->channels < 1 || avctx->channels > 2) {
  37. av_log(avctx, AV_LOG_ERROR, "encoding %d channel(s) is not allowed\n", avctx->channels);
  38. return -1;
  39. }
  40. s->aacplus_handle = aacplusEncOpen(avctx->sample_rate,
  41. avctx->channels,
  42. &samples_input, &max_bytes_output);
  43. if(!s->aacplus_handle) {
  44. av_log(avctx, AV_LOG_ERROR, "can't open encoder\n");
  45. return -1;
  46. }
  47. /* check aacplus version */
  48. aacplus_cfg = aacplusEncGetCurrentConfiguration(s->aacplus_handle);
  49. /* put the options in the configuration struct */
  50. if(avctx->profile != FF_PROFILE_AAC_LOW && avctx->profile != FF_PROFILE_UNKNOWN) {
  51. av_log(avctx, AV_LOG_ERROR, "invalid AAC profile: %d, only LC supported\n", avctx->profile);
  52. aacplusEncClose(s->aacplus_handle);
  53. return -1;
  54. }
  55. aacplus_cfg->bitRate = avctx->bit_rate;
  56. aacplus_cfg->bandWidth = avctx->cutoff;
  57. if (avctx->flags & CODEC_FLAG_GLOBAL_HEADER) {
  58. aacplus_cfg->outputFormat = 0; //raw aac
  59. }
  60. aacplus_cfg->inputFormat = AACPLUS_INPUT_16BIT;
  61. if (!aacplusEncSetConfiguration(s->aacplus_handle, aacplus_cfg)) {
  62. av_log(avctx, AV_LOG_ERROR, "libaacplus doesn't support this output format!\n");
  63. return -1;
  64. }
  65. avctx->frame_size = samples_input / avctx->channels;
  66. avctx->coded_frame= avcodec_alloc_frame();
  67. avctx->coded_frame->key_frame= 1;
  68. /* Set decoder specific info */
  69. avctx->extradata_size = 0;
  70. if (avctx->flags & CODEC_FLAG_GLOBAL_HEADER) {
  71. unsigned char *buffer = NULL;
  72. unsigned long decoder_specific_info_size;
  73. if (aacplusEncGetDecoderSpecificInfo(s->aacplus_handle, &buffer,
  74. &decoder_specific_info_size) == 1) {
  75. avctx->extradata = av_malloc(decoder_specific_info_size + FF_INPUT_BUFFER_PADDING_SIZE);
  76. avctx->extradata_size = decoder_specific_info_size;
  77. memcpy(avctx->extradata, buffer, avctx->extradata_size);
  78. }
  79. #undef free
  80. free(buffer);
  81. #define free please_use_av_free
  82. }
  83. return 0;
  84. }
  85. static int aacPlus_encode_frame(AVCodecContext *avctx,
  86. unsigned char *frame, int buf_size, void *data)
  87. {
  88. aacPlusAudioContext *s = avctx->priv_data;
  89. int bytes_written;
  90. bytes_written = aacplusEncEncode(s->aacplus_handle,
  91. data,
  92. avctx->frame_size * avctx->channels,
  93. frame,
  94. buf_size);
  95. return bytes_written;
  96. }
  97. static av_cold int aacPlus_encode_close(AVCodecContext *avctx)
  98. {
  99. aacPlusAudioContext *s = avctx->priv_data;
  100. av_freep(&avctx->coded_frame);
  101. av_freep(&avctx->extradata);
  102. aacplusEncClose(s->aacplus_handle);
  103. return 0;
  104. }
  105. AVCodec ff_libaacplus_encoder = {
  106. "libaacplus",
  107. AVMEDIA_TYPE_AUDIO,
  108. CODEC_ID_AAC,
  109. sizeof(aacPlusAudioContext),
  110. aacPlus_encode_init,
  111. aacPlus_encode_frame,
  112. aacPlus_encode_close,
  113. .sample_fmts = (const enum SampleFormat[]){SAMPLE_FMT_S16,SAMPLE_FMT_NONE},
  114. .long_name = NULL_IF_CONFIG_SMALL("libaacplus AAC+ (Advanced Audio Codec with SBR+PS)"),
  115. };