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.

202 lines
6.3KB

  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 "audio_frame_queue.h"
  25. #include "internal.h"
  26. #include "mpeg4audio.h"
  27. #define FRAME_SIZE 1024
  28. #define ENC_DELAY 1600
  29. typedef struct AACContext {
  30. VO_AUDIO_CODECAPI codec_api;
  31. VO_HANDLE handle;
  32. VO_MEM_OPERATOR mem_operator;
  33. VO_CODEC_INIT_USERDATA user_data;
  34. VO_PBYTE end_buffer;
  35. AudioFrameQueue afq;
  36. int last_frame;
  37. int last_samples;
  38. } AACContext;
  39. static int aac_encode_close(AVCodecContext *avctx)
  40. {
  41. AACContext *s = avctx->priv_data;
  42. s->codec_api.Uninit(s->handle);
  43. #if FF_API_OLD_ENCODE_AUDIO
  44. av_freep(&avctx->coded_frame);
  45. #endif
  46. av_freep(&avctx->extradata);
  47. ff_af_queue_close(&s->afq);
  48. av_freep(&s->end_buffer);
  49. return 0;
  50. }
  51. static av_cold int aac_encode_init(AVCodecContext *avctx)
  52. {
  53. AACContext *s = avctx->priv_data;
  54. AACENC_PARAM params = { 0 };
  55. int index, ret;
  56. #if FF_API_OLD_ENCODE_AUDIO
  57. avctx->coded_frame = avcodec_alloc_frame();
  58. if (!avctx->coded_frame)
  59. return AVERROR(ENOMEM);
  60. #endif
  61. avctx->frame_size = FRAME_SIZE;
  62. avctx->delay = ENC_DELAY;
  63. s->last_frame = 2;
  64. ff_af_queue_init(avctx, &s->afq);
  65. s->end_buffer = av_mallocz(avctx->frame_size * avctx->channels * 2);
  66. if (!s->end_buffer) {
  67. ret = AVERROR(ENOMEM);
  68. goto error;
  69. }
  70. voGetAACEncAPI(&s->codec_api);
  71. s->mem_operator.Alloc = cmnMemAlloc;
  72. s->mem_operator.Copy = cmnMemCopy;
  73. s->mem_operator.Free = cmnMemFree;
  74. s->mem_operator.Set = cmnMemSet;
  75. s->mem_operator.Check = cmnMemCheck;
  76. s->user_data.memflag = VO_IMF_USERMEMOPERATOR;
  77. s->user_data.memData = &s->mem_operator;
  78. s->codec_api.Init(&s->handle, VO_AUDIO_CodingAAC, &s->user_data);
  79. params.sampleRate = avctx->sample_rate;
  80. params.bitRate = avctx->bit_rate;
  81. params.nChannels = avctx->channels;
  82. params.adtsUsed = !(avctx->flags & CODEC_FLAG_GLOBAL_HEADER);
  83. if (s->codec_api.SetParam(s->handle, VO_PID_AAC_ENCPARAM, &params)
  84. != VO_ERR_NONE) {
  85. av_log(avctx, AV_LOG_ERROR, "Unable to set encoding parameters\n");
  86. ret = AVERROR(EINVAL);
  87. goto error;
  88. }
  89. for (index = 0; index < 16; index++)
  90. if (avctx->sample_rate == avpriv_mpeg4audio_sample_rates[index])
  91. break;
  92. if (index == 16) {
  93. av_log(avctx, AV_LOG_ERROR, "Unsupported sample rate %d\n",
  94. avctx->sample_rate);
  95. ret = AVERROR(ENOSYS);
  96. goto error;
  97. }
  98. if (avctx->flags & CODEC_FLAG_GLOBAL_HEADER) {
  99. avctx->extradata_size = 2;
  100. avctx->extradata = av_mallocz(avctx->extradata_size +
  101. FF_INPUT_BUFFER_PADDING_SIZE);
  102. if (!avctx->extradata) {
  103. ret = AVERROR(ENOMEM);
  104. goto error;
  105. }
  106. avctx->extradata[0] = 0x02 << 3 | index >> 1;
  107. avctx->extradata[1] = (index & 0x01) << 7 | avctx->channels << 3;
  108. }
  109. return 0;
  110. error:
  111. aac_encode_close(avctx);
  112. return ret;
  113. }
  114. static int aac_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
  115. const AVFrame *frame, int *got_packet_ptr)
  116. {
  117. AACContext *s = avctx->priv_data;
  118. VO_CODECBUFFER input = { 0 }, output = { 0 };
  119. VO_AUDIO_OUTPUTINFO output_info = { { 0 } };
  120. VO_PBYTE samples;
  121. int ret;
  122. /* handle end-of-stream small frame and flushing */
  123. if (!frame) {
  124. if (s->last_frame <= 0)
  125. return 0;
  126. if (s->last_samples > 0 && s->last_samples < ENC_DELAY - FRAME_SIZE) {
  127. s->last_samples = 0;
  128. s->last_frame--;
  129. }
  130. s->last_frame--;
  131. memset(s->end_buffer, 0, 2 * avctx->channels * avctx->frame_size);
  132. samples = s->end_buffer;
  133. } else {
  134. if (frame->nb_samples < avctx->frame_size) {
  135. s->last_samples = frame->nb_samples;
  136. memcpy(s->end_buffer, frame->data[0], 2 * avctx->channels * frame->nb_samples);
  137. samples = s->end_buffer;
  138. } else {
  139. samples = (VO_PBYTE)frame->data[0];
  140. }
  141. /* add current frame to the queue */
  142. if ((ret = ff_af_queue_add(&s->afq, frame) < 0))
  143. return ret;
  144. }
  145. if ((ret = ff_alloc_packet2(avctx, avpkt, FFMAX(8192, 768 * avctx->channels))))
  146. return ret;
  147. input.Buffer = samples;
  148. input.Length = 2 * avctx->channels * avctx->frame_size;
  149. output.Buffer = avpkt->data;
  150. output.Length = avpkt->size;
  151. s->codec_api.SetInputData(s->handle, &input);
  152. if (s->codec_api.GetOutputData(s->handle, &output, &output_info)
  153. != VO_ERR_NONE) {
  154. av_log(avctx, AV_LOG_ERROR, "Unable to encode frame\n");
  155. return AVERROR(EINVAL);
  156. }
  157. /* Get the next frame pts/duration */
  158. ff_af_queue_remove(&s->afq, avctx->frame_size, &avpkt->pts,
  159. &avpkt->duration);
  160. avpkt->size = output.Length;
  161. *got_packet_ptr = 1;
  162. return 0;
  163. }
  164. AVCodec ff_libvo_aacenc_encoder = {
  165. .name = "libvo_aacenc",
  166. .type = AVMEDIA_TYPE_AUDIO,
  167. .id = AV_CODEC_ID_AAC,
  168. .priv_data_size = sizeof(AACContext),
  169. .init = aac_encode_init,
  170. .encode2 = aac_encode_frame,
  171. .close = aac_encode_close,
  172. .supported_samplerates = avpriv_mpeg4audio_sample_rates,
  173. .capabilities = CODEC_CAP_SMALL_LAST_FRAME | CODEC_CAP_DELAY,
  174. .sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S16,
  175. AV_SAMPLE_FMT_NONE },
  176. .long_name = NULL_IF_CONFIG_SMALL("Android VisualOn AAC (Advanced Audio Coding)"),
  177. };