obs-ffmpeg-aac.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. /******************************************************************************
  2. Copyright (C) 2014 by Hugh Bailey <[email protected]>
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. ******************************************************************************/
  14. #include <util/base.h>
  15. #include <util/circlebuf.h>
  16. #include <util/darray.h>
  17. #include <obs-module.h>
  18. #include <libavformat/avformat.h>
  19. #include "obs-ffmpeg-formats.h"
  20. #include "obs-ffmpeg-compat.h"
  21. struct aac_encoder {
  22. obs_encoder_t encoder;
  23. AVCodec *aac;
  24. AVCodecContext *context;
  25. uint8_t *samples[MAX_AV_PLANES];
  26. AVFrame *aframe;
  27. int total_samples;
  28. DARRAY(uint8_t) packet_buffer;
  29. size_t audio_planes;
  30. size_t audio_size;
  31. int frame_size; /* pretty much always 1024 for AAC */
  32. int frame_size_bytes;
  33. };
  34. static const char *aac_getname(void)
  35. {
  36. return obs_module_text("FFmpegAAC");
  37. }
  38. static void aac_warn(const char *func, const char *format, ...)
  39. {
  40. va_list args;
  41. char msg[1024];
  42. va_start(args, format);
  43. vsnprintf(msg, sizeof(msg), format, args);
  44. blog(LOG_WARNING, "[%s]: %s", func, msg);
  45. va_end(args);
  46. }
  47. static void aac_destroy(void *data)
  48. {
  49. struct aac_encoder *enc = data;
  50. if (enc->samples[0])
  51. av_freep(&enc->samples[0]);
  52. if (enc->context)
  53. avcodec_close(enc->context);
  54. if (enc->aframe)
  55. av_frame_free(&enc->aframe);
  56. da_free(enc->packet_buffer);
  57. bfree(enc);
  58. }
  59. static bool initialize_codec(struct aac_encoder *enc)
  60. {
  61. int ret;
  62. enc->aframe = av_frame_alloc();
  63. if (!enc->aframe) {
  64. aac_warn("initialize_codec", "Failed to allocate audio frame");
  65. return false;
  66. }
  67. ret = avcodec_open2(enc->context, enc->aac, NULL);
  68. if (ret < 0) {
  69. aac_warn("initialize_codec", "Failed to open AAC codec: %s",
  70. av_err2str(ret));
  71. return false;
  72. }
  73. enc->frame_size = enc->context->frame_size;
  74. if (!enc->frame_size)
  75. enc->frame_size = 1024;
  76. enc->frame_size_bytes = enc->frame_size * (int)enc->audio_size;
  77. ret = av_samples_alloc(enc->samples, NULL, enc->context->channels,
  78. enc->frame_size, enc->context->sample_fmt, 0);
  79. if (ret < 0) {
  80. aac_warn("initialize_codec", "Failed to create audio buffer: "
  81. "%s", av_err2str(ret));
  82. return false;
  83. }
  84. return true;
  85. }
  86. static void init_sizes(struct aac_encoder *enc, audio_t audio)
  87. {
  88. const struct audio_output_info *aoi;
  89. enum audio_format format;
  90. aoi = audio_output_get_info(audio);
  91. format = convert_ffmpeg_sample_format(enc->context->sample_fmt);
  92. enc->audio_planes = get_audio_planes(format, aoi->speakers);
  93. enc->audio_size = get_audio_size(format, aoi->speakers, 1);
  94. }
  95. static void *aac_create(obs_data_t settings, obs_encoder_t encoder)
  96. {
  97. struct aac_encoder *enc;
  98. int bitrate = (int)obs_data_get_int(settings, "bitrate");
  99. audio_t audio = obs_encoder_audio(encoder);
  100. if (!bitrate) {
  101. aac_warn("aac_create", "Invalid bitrate specified");
  102. return NULL;
  103. }
  104. avcodec_register_all();
  105. enc = bzalloc(sizeof(struct aac_encoder));
  106. enc->encoder = encoder;
  107. enc->aac = avcodec_find_encoder(AV_CODEC_ID_AAC);
  108. if (!enc->aac) {
  109. aac_warn("aac_create", "Couldn't find encoder");
  110. goto fail;
  111. }
  112. blog(LOG_INFO, "Using ffmpeg \"%s\" aac encoder", enc->aac->name);
  113. enc->context = avcodec_alloc_context3(enc->aac);
  114. if (!enc->context) {
  115. aac_warn("aac_create", "Failed to create codec context");
  116. goto fail;
  117. }
  118. enc->context->bit_rate = bitrate * 1000;
  119. enc->context->channels = (int)audio_output_get_channels(audio);
  120. enc->context->sample_rate = audio_output_get_sample_rate(audio);
  121. enc->context->sample_fmt = enc->aac->sample_fmts ?
  122. enc->aac->sample_fmts[0] : AV_SAMPLE_FMT_FLTP;
  123. blog(LOG_INFO, "FFmpeg AAC: bitrate: %d, channels: %d",
  124. enc->context->bit_rate / 1000, enc->context->channels);
  125. init_sizes(enc, audio);
  126. /* enable experimental FFmpeg encoder if the only one available */
  127. enc->context->strict_std_compliance = -2;
  128. enc->context->flags = CODEC_FLAG_GLOBAL_HEADER;
  129. if (initialize_codec(enc))
  130. return enc;
  131. fail:
  132. aac_destroy(enc);
  133. return NULL;
  134. }
  135. static bool do_aac_encode(struct aac_encoder *enc,
  136. struct encoder_packet *packet, bool *received_packet)
  137. {
  138. AVRational time_base = {1, enc->context->sample_rate};
  139. AVPacket avpacket = {0};
  140. int got_packet;
  141. int ret;
  142. enc->aframe->nb_samples = enc->frame_size;
  143. enc->aframe->pts = av_rescale_q(enc->total_samples,
  144. (AVRational){1, enc->context->sample_rate},
  145. enc->context->time_base);
  146. ret = avcodec_fill_audio_frame(enc->aframe, enc->context->channels,
  147. enc->context->sample_fmt, enc->samples[0],
  148. enc->frame_size_bytes * enc->context->channels, 1);
  149. if (ret < 0) {
  150. aac_warn("do_aac_encode", "avcodec_fill_audio_frame failed: %s",
  151. av_err2str(ret));
  152. return false;
  153. }
  154. enc->total_samples += enc->frame_size;
  155. ret = avcodec_encode_audio2(enc->context, &avpacket, enc->aframe,
  156. &got_packet);
  157. if (ret < 0) {
  158. aac_warn("do_aac_encode", "avcodec_encode_audio2 failed: %s",
  159. av_err2str(ret));
  160. return false;
  161. }
  162. *received_packet = !!got_packet;
  163. if (!got_packet)
  164. return true;
  165. da_resize(enc->packet_buffer, 0);
  166. da_push_back_array(enc->packet_buffer, avpacket.data, avpacket.size);
  167. packet->pts = rescale_ts(avpacket.pts, enc->context, time_base);
  168. packet->dts = rescale_ts(avpacket.dts, enc->context, time_base);
  169. packet->data = enc->packet_buffer.array;
  170. packet->size = avpacket.size;
  171. packet->type = OBS_ENCODER_AUDIO;
  172. packet->timebase_num = 1;
  173. packet->timebase_den = (int32_t)enc->context->sample_rate;
  174. av_free_packet(&avpacket);
  175. return true;
  176. }
  177. static bool aac_encode(void *data, struct encoder_frame *frame,
  178. struct encoder_packet *packet, bool *received_packet)
  179. {
  180. struct aac_encoder *enc = data;
  181. for (size_t i = 0; i < enc->audio_planes; i++)
  182. memcpy(enc->samples[i], frame->data[i], enc->frame_size_bytes);
  183. return do_aac_encode(enc, packet, received_packet);
  184. }
  185. static void aac_defaults(obs_data_t settings)
  186. {
  187. obs_data_set_default_int(settings, "bitrate", 128);
  188. }
  189. static obs_properties_t aac_properties(void)
  190. {
  191. obs_properties_t props = obs_properties_create();
  192. obs_properties_add_int(props, "bitrate",
  193. obs_module_text("Bitrate"), 32, 320, 32);
  194. return props;
  195. }
  196. static bool aac_extra_data(void *data, uint8_t **extra_data, size_t *size)
  197. {
  198. struct aac_encoder *enc = data;
  199. *extra_data = enc->context->extradata;
  200. *size = enc->context->extradata_size;
  201. return true;
  202. }
  203. static bool aac_audio_info(void *data, struct audio_convert_info *info)
  204. {
  205. struct aac_encoder *enc = data;
  206. memset(info, 0, sizeof(struct audio_convert_info));
  207. info->format = convert_ffmpeg_sample_format(enc->context->sample_fmt);
  208. return true;
  209. }
  210. static size_t aac_frame_size(void *data)
  211. {
  212. struct aac_encoder *enc =data;
  213. return enc->frame_size;
  214. }
  215. struct obs_encoder_info aac_encoder_info = {
  216. .id = "ffmpeg_aac",
  217. .type = OBS_ENCODER_AUDIO,
  218. .codec = "AAC",
  219. .get_name = aac_getname,
  220. .create = aac_create,
  221. .destroy = aac_destroy,
  222. .encode = aac_encode,
  223. .get_frame_size = aac_frame_size,
  224. .get_defaults = aac_defaults,
  225. .get_properties = aac_properties,
  226. .get_extra_data = aac_extra_data,
  227. .get_audio_info = aac_audio_info
  228. };