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.

131 lines
4.2KB

  1. /*
  2. * AAC encoder wrapper
  3. * Copyright (c) 2010 Martin Storsjo
  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. #include <vo-aacenc/voAAC.h>
  22. #include <vo-aacenc/cmnMemory.h>
  23. #include "avcodec.h"
  24. #include "mpeg4audio.h"
  25. typedef struct AACContext {
  26. VO_AUDIO_CODECAPI codec_api;
  27. VO_HANDLE handle;
  28. VO_MEM_OPERATOR mem_operator;
  29. VO_CODEC_INIT_USERDATA user_data;
  30. } AACContext;
  31. static av_cold int aac_encode_init(AVCodecContext *avctx)
  32. {
  33. AACContext *s = avctx->priv_data;
  34. AACENC_PARAM params = { 0 };
  35. int index;
  36. avctx->coded_frame = avcodec_alloc_frame();
  37. if (!avctx->coded_frame)
  38. return AVERROR(ENOMEM);
  39. avctx->frame_size = 1024;
  40. voGetAACEncAPI(&s->codec_api);
  41. s->mem_operator.Alloc = cmnMemAlloc;
  42. s->mem_operator.Copy = cmnMemCopy;
  43. s->mem_operator.Free = cmnMemFree;
  44. s->mem_operator.Set = cmnMemSet;
  45. s->mem_operator.Check = cmnMemCheck;
  46. s->user_data.memflag = VO_IMF_USERMEMOPERATOR;
  47. s->user_data.memData = &s->mem_operator;
  48. s->codec_api.Init(&s->handle, VO_AUDIO_CodingAAC, &s->user_data);
  49. params.sampleRate = avctx->sample_rate;
  50. params.bitRate = avctx->bit_rate;
  51. params.nChannels = avctx->channels;
  52. params.adtsUsed = !(avctx->flags & CODEC_FLAG_GLOBAL_HEADER);
  53. if (s->codec_api.SetParam(s->handle, VO_PID_AAC_ENCPARAM, &params)
  54. != VO_ERR_NONE) {
  55. av_log(avctx, AV_LOG_ERROR, "Unable to set encoding parameters\n");
  56. return AVERROR(EINVAL);
  57. }
  58. for (index = 0; index < 16; index++)
  59. if (avctx->sample_rate == avpriv_mpeg4audio_sample_rates[index])
  60. break;
  61. if (index == 16) {
  62. av_log(avctx, AV_LOG_ERROR, "Unsupported sample rate %d\n",
  63. avctx->sample_rate);
  64. return AVERROR(ENOSYS);
  65. }
  66. if (avctx->flags & CODEC_FLAG_GLOBAL_HEADER) {
  67. avctx->extradata_size = 2;
  68. avctx->extradata = av_mallocz(avctx->extradata_size +
  69. FF_INPUT_BUFFER_PADDING_SIZE);
  70. if (!avctx->extradata)
  71. return AVERROR(ENOMEM);
  72. avctx->extradata[0] = 0x02 << 3 | index >> 1;
  73. avctx->extradata[1] = (index & 0x01) << 7 | avctx->channels << 3;
  74. }
  75. return 0;
  76. }
  77. static int aac_encode_close(AVCodecContext *avctx)
  78. {
  79. AACContext *s = avctx->priv_data;
  80. s->codec_api.Uninit(s->handle);
  81. av_freep(&avctx->coded_frame);
  82. return 0;
  83. }
  84. static int aac_encode_frame(AVCodecContext *avctx,
  85. unsigned char *frame/*out*/,
  86. int buf_size, void *data/*in*/)
  87. {
  88. AACContext *s = avctx->priv_data;
  89. VO_CODECBUFFER input = { 0 }, output = { 0 };
  90. VO_AUDIO_OUTPUTINFO output_info = { { 0 } };
  91. input.Buffer = data;
  92. input.Length = 2 * avctx->channels * avctx->frame_size;
  93. output.Buffer = frame;
  94. output.Length = buf_size;
  95. s->codec_api.SetInputData(s->handle, &input);
  96. if (s->codec_api.GetOutputData(s->handle, &output, &output_info)
  97. != VO_ERR_NONE) {
  98. av_log(avctx, AV_LOG_ERROR, "Unable to encode frame\n");
  99. return AVERROR(EINVAL);
  100. }
  101. return output.Length;
  102. }
  103. AVCodec ff_libvo_aacenc_encoder = {
  104. .name = "libvo_aacenc",
  105. .type = AVMEDIA_TYPE_AUDIO,
  106. .id = CODEC_ID_AAC,
  107. .priv_data_size = sizeof(AACContext),
  108. .init = aac_encode_init,
  109. .encode = aac_encode_frame,
  110. .close = aac_encode_close,
  111. .sample_fmts = (const enum AVSampleFormat[]){AV_SAMPLE_FMT_S16,AV_SAMPLE_FMT_NONE},
  112. .long_name = NULL_IF_CONFIG_SMALL("Android VisualOn AAC"),
  113. };