obs-ffmpeg-aac.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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(void)
  35. {
  36. /* TODO: 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. blog(LOG_INFO, "Using ffmpeg \"%s\" aac encoder", enc->aac->name);
  114. enc->context = avcodec_alloc_context3(enc->aac);
  115. if (!enc->context) {
  116. aac_warn("aac_create", "Failed to create codec context");
  117. goto fail;
  118. }
  119. enc->context->bit_rate = bitrate * 1000;
  120. enc->context->channels = (int)audio_output_channels(audio);
  121. enc->context->sample_rate = audio_output_samplerate(audio);
  122. enc->context->sample_fmt = enc->aac->sample_fmts ?
  123. enc->aac->sample_fmts[0] : AV_SAMPLE_FMT_FLTP;
  124. init_sizes(enc, audio);
  125. /* enable experimental FFmpeg encoder if the only one available */
  126. enc->context->strict_std_compliance = -2;
  127. enc->context->flags = CODEC_FLAG_GLOBAL_HEADER;
  128. if (initialize_codec(enc))
  129. return enc;
  130. fail:
  131. aac_destroy(enc);
  132. return NULL;
  133. }
  134. static bool do_aac_encode(struct aac_encoder *enc,
  135. struct encoder_packet *packet, bool *received_packet)
  136. {
  137. AVRational time_base = {1, enc->context->sample_rate};
  138. AVPacket avpacket = {0};
  139. int got_packet;
  140. int ret;
  141. enc->aframe->nb_samples = enc->frame_size;
  142. enc->aframe->pts = av_rescale_q(enc->total_samples,
  143. (AVRational){1, enc->context->sample_rate},
  144. enc->context->time_base);
  145. ret = avcodec_fill_audio_frame(enc->aframe, enc->context->channels,
  146. enc->context->sample_fmt, enc->samples[0],
  147. enc->frame_size_bytes * enc->context->channels, 1);
  148. if (ret < 0) {
  149. aac_warn("do_aac_encode", "avcodec_fill_audio_frame failed: %s",
  150. av_err2str(ret));
  151. return false;
  152. }
  153. enc->total_samples += enc->frame_size;
  154. ret = avcodec_encode_audio2(enc->context, &avpacket, enc->aframe,
  155. &got_packet);
  156. if (ret < 0) {
  157. aac_warn("do_aac_encode", "avcodec_encode_audio2 failed: %s",
  158. av_err2str(ret));
  159. return false;
  160. }
  161. *received_packet = !!got_packet;
  162. if (!got_packet)
  163. return true;
  164. da_resize(enc->packet_buffer, 0);
  165. da_push_back_array(enc->packet_buffer, avpacket.data, avpacket.size);
  166. packet->pts = rescale_ts(avpacket.pts, enc->context, time_base);
  167. packet->dts = rescale_ts(avpacket.dts, enc->context, time_base);
  168. packet->data = enc->packet_buffer.array;
  169. packet->size = avpacket.size;
  170. packet->type = OBS_ENCODER_AUDIO;
  171. packet->timebase_num = 1;
  172. packet->timebase_den = (int32_t)enc->context->sample_rate;
  173. av_free_packet(&avpacket);
  174. return true;
  175. }
  176. static bool aac_encode(void *data, struct encoder_frame *frame,
  177. struct encoder_packet *packet, bool *received_packet)
  178. {
  179. struct aac_encoder *enc = data;
  180. for (size_t i = 0; i < enc->audio_planes; i++)
  181. memcpy(enc->samples[i], frame->data[i], enc->frame_size_bytes);
  182. return do_aac_encode(enc, packet, received_packet);
  183. }
  184. static void aac_defaults(obs_data_t settings)
  185. {
  186. obs_data_set_default_int(settings, "bitrate", 128);
  187. }
  188. static obs_properties_t aac_properties(void)
  189. {
  190. obs_properties_t props = obs_properties_create();
  191. /* TODO: locale */
  192. obs_properties_add_int(props, "bitrate", "Bitrate", 32, 320, 32);
  193. return props;
  194. }
  195. static bool aac_extra_data(void *data, uint8_t **extra_data, size_t *size)
  196. {
  197. struct aac_encoder *enc = data;
  198. *extra_data = enc->context->extradata;
  199. *size = enc->context->extradata_size;
  200. return true;
  201. }
  202. static bool aac_audio_info(void *data, struct audio_convert_info *info)
  203. {
  204. struct aac_encoder *enc = data;
  205. memset(info, 0, sizeof(struct audio_convert_info));
  206. info->format = convert_ffmpeg_sample_format(enc->context->sample_fmt);
  207. return true;
  208. }
  209. static size_t aac_frame_size(void *data)
  210. {
  211. struct aac_encoder *enc =data;
  212. return enc->frame_size;
  213. }
  214. struct obs_encoder_info aac_encoder_info = {
  215. .id = "ffmpeg_aac",
  216. .type = OBS_ENCODER_AUDIO,
  217. .codec = "AAC",
  218. .getname = aac_getname,
  219. .create = aac_create,
  220. .destroy = aac_destroy,
  221. .encode = aac_encode,
  222. .frame_size = aac_frame_size,
  223. .defaults = aac_defaults,
  224. .properties = aac_properties,
  225. .extra_data = aac_extra_data,
  226. .audio_info = aac_audio_info
  227. };