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.

149 lines
5.0KB

  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 <aacplus.h>
  26. #include "avcodec.h"
  27. #include "internal.h"
  28. typedef struct aacPlusAudioContext {
  29. aacplusEncHandle aacplus_handle;
  30. unsigned long max_output_bytes;
  31. unsigned long samples_input;
  32. } aacPlusAudioContext;
  33. static av_cold int aacPlus_encode_init(AVCodecContext *avctx)
  34. {
  35. aacPlusAudioContext *s = avctx->priv_data;
  36. aacplusEncConfiguration *aacplus_cfg;
  37. /* number of channels */
  38. if (avctx->channels < 1 || avctx->channels > 2) {
  39. av_log(avctx, AV_LOG_ERROR, "encoding %d channel(s) is not allowed\n", avctx->channels);
  40. return -1;
  41. }
  42. s->aacplus_handle = aacplusEncOpen(avctx->sample_rate, avctx->channels,
  43. &s->samples_input, &s->max_output_bytes);
  44. if(!s->aacplus_handle) {
  45. av_log(avctx, AV_LOG_ERROR, "can't open encoder\n");
  46. return -1;
  47. }
  48. /* check aacplus version */
  49. aacplus_cfg = aacplusEncGetCurrentConfiguration(s->aacplus_handle);
  50. /* put the options in the configuration struct */
  51. if(avctx->profile != FF_PROFILE_AAC_LOW && avctx->profile != FF_PROFILE_UNKNOWN) {
  52. av_log(avctx, AV_LOG_ERROR, "invalid AAC profile: %d, only LC supported\n", avctx->profile);
  53. aacplusEncClose(s->aacplus_handle);
  54. return -1;
  55. }
  56. aacplus_cfg->bitRate = avctx->bit_rate;
  57. aacplus_cfg->bandWidth = avctx->cutoff;
  58. aacplus_cfg->outputFormat = !(avctx->flags & CODEC_FLAG_GLOBAL_HEADER);
  59. aacplus_cfg->inputFormat = AACPLUS_INPUT_16BIT;
  60. if (!aacplusEncSetConfiguration(s->aacplus_handle, aacplus_cfg)) {
  61. av_log(avctx, AV_LOG_ERROR, "libaacplus doesn't support this output format!\n");
  62. return -1;
  63. }
  64. avctx->frame_size = s->samples_input / avctx->channels;
  65. /* Set decoder specific info */
  66. avctx->extradata_size = 0;
  67. if (avctx->flags & CODEC_FLAG_GLOBAL_HEADER) {
  68. unsigned char *buffer = NULL;
  69. unsigned long decoder_specific_info_size;
  70. if (aacplusEncGetDecoderSpecificInfo(s->aacplus_handle, &buffer,
  71. &decoder_specific_info_size) == 1) {
  72. avctx->extradata = av_malloc(decoder_specific_info_size + FF_INPUT_BUFFER_PADDING_SIZE);
  73. avctx->extradata_size = decoder_specific_info_size;
  74. memcpy(avctx->extradata, buffer, avctx->extradata_size);
  75. }
  76. free(buffer);
  77. }
  78. return 0;
  79. }
  80. static int aacPlus_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
  81. const AVFrame *frame, int *got_packet)
  82. {
  83. aacPlusAudioContext *s = avctx->priv_data;
  84. int32_t *input_buffer = (int32_t *)frame->data[0];
  85. int ret;
  86. if ((ret = ff_alloc_packet2(avctx, pkt, s->max_output_bytes)) < 0)
  87. return ret;
  88. pkt->size = aacplusEncEncode(s->aacplus_handle, input_buffer,
  89. s->samples_input, pkt->data, pkt->size);
  90. *got_packet = 1;
  91. pkt->pts = frame->pts;
  92. return 0;
  93. }
  94. static av_cold int aacPlus_encode_close(AVCodecContext *avctx)
  95. {
  96. aacPlusAudioContext *s = avctx->priv_data;
  97. av_freep(&avctx->extradata);
  98. aacplusEncClose(s->aacplus_handle);
  99. return 0;
  100. }
  101. static const AVProfile profiles[] = {
  102. { FF_PROFILE_AAC_LOW, "LC" },
  103. { FF_PROFILE_UNKNOWN },
  104. };
  105. static const uint64_t channel_layouts[] = {
  106. AV_CH_LAYOUT_MONO,
  107. AV_CH_LAYOUT_STEREO,
  108. 0,
  109. };
  110. AVCodec ff_libaacplus_encoder = {
  111. .name = "libaacplus",
  112. .type = AVMEDIA_TYPE_AUDIO,
  113. .id = AV_CODEC_ID_AAC,
  114. .priv_data_size = sizeof(aacPlusAudioContext),
  115. .init = aacPlus_encode_init,
  116. .encode2 = aacPlus_encode_frame,
  117. .close = aacPlus_encode_close,
  118. .sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S16,
  119. AV_SAMPLE_FMT_NONE },
  120. .long_name = NULL_IF_CONFIG_SMALL("libaacplus AAC+ (Advanced Audio Codec with SBR+PS)"),
  121. .profiles = profiles,
  122. .channel_layouts = (const uint64_t[]) { AV_CH_LAYOUT_MONO,
  123. AV_CH_LAYOUT_STEREO,
  124. 0 },
  125. };