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.

195 lines
6.2KB

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