obs-ffmpeg-aac.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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.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(const char *locale)
  35. {
  36. UNUSED_PARAMETER(locale);
  37. return "FFmpeg Default AAC Encoder";
  38. }
  39. static void aac_warn(const char *func, const char *format, ...)
  40. {
  41. va_list args;
  42. char msg[1024];
  43. va_start(args, format);
  44. vsnprintf(msg, sizeof(msg), format, args);
  45. blog(LOG_WARNING, "[%s]: %s", func, msg);
  46. va_end(args);
  47. }
  48. static void aac_destroy(void *data)
  49. {
  50. struct aac_encoder *enc = data;
  51. if (enc->samples[0])
  52. av_freep(&enc->samples[0]);
  53. if (enc->context)
  54. avcodec_close(enc->context);
  55. if (enc->aframe)
  56. av_frame_free(&enc->aframe);
  57. da_free(enc->packet_buffer);
  58. bfree(enc);
  59. }
  60. static bool initialize_codec(struct aac_encoder *enc)
  61. {
  62. int ret;
  63. enc->aframe = av_frame_alloc();
  64. if (!enc->aframe) {
  65. aac_warn("initialize_codec", "Failed to allocate audio frame");
  66. return false;
  67. }
  68. ret = avcodec_open2(enc->context, enc->aac, NULL);
  69. if (ret < 0) {
  70. aac_warn("initialize_codec", "Failed to open AAC codec: %s",
  71. av_err2str(ret));
  72. return false;
  73. }
  74. enc->frame_size = enc->context->frame_size;
  75. if (!enc->frame_size)
  76. enc->frame_size = 1024;
  77. enc->frame_size_bytes = enc->frame_size * (int)enc->audio_size;
  78. ret = av_samples_alloc(enc->samples, NULL, enc->context->channels,
  79. enc->frame_size, enc->context->sample_fmt, 0);
  80. if (ret < 0) {
  81. aac_warn("initialize_codec", "Failed to create audio buffer: "
  82. "%s", av_err2str(ret));
  83. return false;
  84. }
  85. return true;
  86. }
  87. static void init_sizes(struct aac_encoder *enc, audio_t audio)
  88. {
  89. const struct audio_output_info *aoi;
  90. enum audio_format format;
  91. aoi = audio_output_getinfo(audio);
  92. format = convert_ffmpeg_sample_format(enc->context->sample_fmt);
  93. enc->audio_planes = get_audio_planes(format, aoi->speakers);
  94. enc->audio_size = get_audio_size(format, aoi->speakers, 1);
  95. }
  96. static void *aac_create(obs_data_t settings, obs_encoder_t encoder)
  97. {
  98. struct aac_encoder *enc;
  99. int bitrate = (int)obs_data_getint(settings, "bitrate");
  100. audio_t audio = obs_encoder_audio(encoder);
  101. if (!bitrate) {
  102. aac_warn("aac_create", "Invalid bitrate specified");
  103. return NULL;
  104. }
  105. avcodec_register_all();
  106. enc = bzalloc(sizeof(struct aac_encoder));
  107. enc->encoder = encoder;
  108. enc->aac = avcodec_find_encoder(AV_CODEC_ID_AAC);
  109. if (!enc->aac) {
  110. aac_warn("aac_create", "Couldn't find encoder");
  111. goto fail;
  112. }
  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_channels(audio);
  120. enc->context->sample_rate = audio_output_samplerate(audio);
  121. enc->context->sample_fmt = enc->aac->sample_fmts ?
  122. enc->aac->sample_fmts[0] : AV_SAMPLE_FMT_FLTP;
  123. init_sizes(enc, audio);
  124. /* enable experimental FFmpeg encoder if the only one available */
  125. enc->context->strict_std_compliance = -2;
  126. if (initialize_codec(enc))
  127. return enc;
  128. fail:
  129. aac_destroy(enc);
  130. return NULL;
  131. }
  132. static bool do_aac_encode(struct aac_encoder *enc,
  133. struct encoder_packet *packet, bool *received_packet)
  134. {
  135. AVRational time_base = {1, enc->context->sample_rate};
  136. AVPacket avpacket = {0};
  137. int got_packet;
  138. int ret;
  139. enc->aframe->nb_samples = enc->frame_size;
  140. enc->aframe->pts = av_rescale_q(enc->total_samples,
  141. (AVRational){1, enc->context->sample_rate},
  142. enc->context->time_base);
  143. ret = avcodec_fill_audio_frame(enc->aframe, enc->context->channels,
  144. enc->context->sample_fmt, enc->samples[0],
  145. enc->frame_size_bytes * enc->context->channels, 1);
  146. if (ret < 0) {
  147. aac_warn("do_aac_encode", "avcodec_fill_audio_frame failed: %s",
  148. av_err2str(ret));
  149. return false;
  150. }
  151. enc->total_samples += enc->frame_size;
  152. ret = avcodec_encode_audio2(enc->context, &avpacket, enc->aframe,
  153. &got_packet);
  154. if (ret < 0) {
  155. aac_warn("do_aac_encode", "avcodec_encode_audio2 failed: %s",
  156. av_err2str(ret));
  157. return false;
  158. }
  159. *received_packet = !!got_packet;
  160. if (!got_packet)
  161. return true;
  162. da_resize(enc->packet_buffer, 0);
  163. da_push_back_array(enc->packet_buffer, avpacket.data, avpacket.size);
  164. packet->pts = rescale_ts(avpacket.pts, enc->context, time_base);
  165. packet->dts = rescale_ts(avpacket.dts, enc->context, time_base);
  166. packet->data = enc->packet_buffer.array;
  167. packet->size = avpacket.size;
  168. packet->type = OBS_ENCODER_AUDIO;
  169. packet->timebase_num = 1;
  170. packet->timebase_den = (int32_t)enc->context->sample_rate;
  171. av_free_packet(&avpacket);
  172. return true;
  173. }
  174. static bool aac_encode(void *data, struct encoder_frame *frame,
  175. struct encoder_packet *packet, bool *received_packet)
  176. {
  177. struct aac_encoder *enc = data;
  178. for (size_t i = 0; i < enc->audio_planes; i++)
  179. memcpy(enc->samples[i], frame->data[i], enc->frame_size_bytes);
  180. return do_aac_encode(enc, packet, received_packet);
  181. }
  182. static void aac_defaults(obs_data_t settings)
  183. {
  184. obs_data_set_default_int(settings, "bitrate", 128);
  185. }
  186. static obs_properties_t aac_properties(const char *locale)
  187. {
  188. obs_properties_t props = obs_properties_create(locale);
  189. /* TODO: locale */
  190. obs_properties_add_int(props, "bitrate", "Bitrate", 32, 320, 32);
  191. return props;
  192. }
  193. static bool aac_extra_data(void *data, uint8_t **extra_data, size_t *size)
  194. {
  195. struct aac_encoder *enc = data;
  196. *extra_data = enc->context->extradata;
  197. *size = enc->context->extradata_size;
  198. return true;
  199. }
  200. static bool aac_audio_info(void *data, struct audio_convert_info *info)
  201. {
  202. struct aac_encoder *enc = data;
  203. memset(info, 0, sizeof(struct audio_convert_info));
  204. info->format = convert_ffmpeg_sample_format(enc->context->sample_fmt);
  205. return true;
  206. }
  207. static size_t aac_frame_size(void *data)
  208. {
  209. struct aac_encoder *enc =data;
  210. return enc->frame_size;
  211. }
  212. struct obs_encoder_info aac_encoder_info = {
  213. .id = "ffmpeg_aac",
  214. .type = OBS_ENCODER_AUDIO,
  215. .codec = "AAC",
  216. .getname = aac_getname,
  217. .create = aac_create,
  218. .destroy = aac_destroy,
  219. .encode = aac_encode,
  220. .frame_size = aac_frame_size,
  221. .defaults = aac_defaults,
  222. .properties = aac_properties,
  223. .extra_data = aac_extra_data,
  224. .audio_info = aac_audio_info
  225. };