obs-ffmpeg-aac.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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. #ifndef MIN
  96. #define MIN(x, y) ((x) < (y) ? (x) : (y))
  97. #endif
  98. static void *aac_create(obs_data_t *settings, obs_encoder_t *encoder)
  99. {
  100. struct aac_encoder *enc;
  101. int bitrate = (int)obs_data_get_int(settings, "bitrate");
  102. audio_t *audio = obs_encoder_audio(encoder);
  103. if (!bitrate) {
  104. aac_warn("aac_create", "Invalid bitrate specified");
  105. return NULL;
  106. }
  107. avcodec_register_all();
  108. enc = bzalloc(sizeof(struct aac_encoder));
  109. enc->encoder = encoder;
  110. enc->aac = avcodec_find_encoder(AV_CODEC_ID_AAC);
  111. if (!enc->aac) {
  112. aac_warn("aac_create", "Couldn't find encoder");
  113. goto fail;
  114. }
  115. blog(LOG_INFO, "Using ffmpeg \"%s\" aac encoder", enc->aac->name);
  116. enc->context = avcodec_alloc_context3(enc->aac);
  117. if (!enc->context) {
  118. aac_warn("aac_create", "Failed to create codec context");
  119. goto fail;
  120. }
  121. enc->context->bit_rate = bitrate * 1000;
  122. enc->context->channels = (int)audio_output_get_channels(audio);
  123. enc->context->sample_rate = audio_output_get_sample_rate(audio);
  124. enc->context->sample_fmt = enc->aac->sample_fmts ?
  125. enc->aac->sample_fmts[0] : AV_SAMPLE_FMT_FLTP;
  126. /* if using FFmpeg's AAC encoder, at least set a cutoff value
  127. * (recommended by konverter) */
  128. if (strcmp(enc->aac->name, "aac") == 0) {
  129. int cutoff1 = 4000 + enc->context->bit_rate / 8;
  130. int cutoff2 = 12000 + enc->context->bit_rate / 8;
  131. int cutoff3 = enc->context->sample_rate / 2;
  132. int cutoff;
  133. cutoff = MIN(cutoff1, cutoff2);
  134. cutoff = MIN(cutoff, cutoff3);
  135. enc->context->cutoff = cutoff;
  136. }
  137. blog(LOG_INFO, "FFmpeg AAC: bitrate: %d, channels: %d",
  138. enc->context->bit_rate / 1000, enc->context->channels);
  139. init_sizes(enc, audio);
  140. /* enable experimental FFmpeg encoder if the only one available */
  141. enc->context->strict_std_compliance = -2;
  142. enc->context->flags = CODEC_FLAG_GLOBAL_HEADER;
  143. if (initialize_codec(enc))
  144. return enc;
  145. fail:
  146. aac_destroy(enc);
  147. return NULL;
  148. }
  149. static bool do_aac_encode(struct aac_encoder *enc,
  150. struct encoder_packet *packet, bool *received_packet)
  151. {
  152. AVRational time_base = {1, enc->context->sample_rate};
  153. AVPacket avpacket = {0};
  154. int got_packet;
  155. int ret;
  156. enc->aframe->nb_samples = enc->frame_size;
  157. enc->aframe->pts = av_rescale_q(enc->total_samples,
  158. (AVRational){1, enc->context->sample_rate},
  159. enc->context->time_base);
  160. ret = avcodec_fill_audio_frame(enc->aframe, enc->context->channels,
  161. enc->context->sample_fmt, enc->samples[0],
  162. enc->frame_size_bytes * enc->context->channels, 1);
  163. if (ret < 0) {
  164. aac_warn("do_aac_encode", "avcodec_fill_audio_frame failed: %s",
  165. av_err2str(ret));
  166. return false;
  167. }
  168. enc->total_samples += enc->frame_size;
  169. ret = avcodec_encode_audio2(enc->context, &avpacket, enc->aframe,
  170. &got_packet);
  171. if (ret < 0) {
  172. aac_warn("do_aac_encode", "avcodec_encode_audio2 failed: %s",
  173. av_err2str(ret));
  174. return false;
  175. }
  176. *received_packet = !!got_packet;
  177. if (!got_packet)
  178. return true;
  179. da_resize(enc->packet_buffer, 0);
  180. da_push_back_array(enc->packet_buffer, avpacket.data, avpacket.size);
  181. packet->pts = rescale_ts(avpacket.pts, enc->context, time_base);
  182. packet->dts = rescale_ts(avpacket.dts, enc->context, time_base);
  183. packet->data = enc->packet_buffer.array;
  184. packet->size = avpacket.size;
  185. packet->type = OBS_ENCODER_AUDIO;
  186. packet->timebase_num = 1;
  187. packet->timebase_den = (int32_t)enc->context->sample_rate;
  188. av_free_packet(&avpacket);
  189. return true;
  190. }
  191. static bool aac_encode(void *data, struct encoder_frame *frame,
  192. struct encoder_packet *packet, bool *received_packet)
  193. {
  194. struct aac_encoder *enc = data;
  195. for (size_t i = 0; i < enc->audio_planes; i++)
  196. memcpy(enc->samples[i], frame->data[i], enc->frame_size_bytes);
  197. return do_aac_encode(enc, packet, received_packet);
  198. }
  199. static void aac_defaults(obs_data_t *settings)
  200. {
  201. obs_data_set_default_int(settings, "bitrate", 128);
  202. }
  203. static obs_properties_t *aac_properties(void *unused)
  204. {
  205. UNUSED_PARAMETER(unused);
  206. obs_properties_t *props = obs_properties_create();
  207. obs_properties_add_int(props, "bitrate",
  208. obs_module_text("Bitrate"), 32, 320, 32);
  209. return props;
  210. }
  211. static bool aac_extra_data(void *data, uint8_t **extra_data, size_t *size)
  212. {
  213. struct aac_encoder *enc = data;
  214. *extra_data = enc->context->extradata;
  215. *size = enc->context->extradata_size;
  216. return true;
  217. }
  218. static void aac_audio_info(void *data, struct audio_convert_info *info)
  219. {
  220. struct aac_encoder *enc = data;
  221. info->format = convert_ffmpeg_sample_format(enc->context->sample_fmt);
  222. }
  223. static size_t aac_frame_size(void *data)
  224. {
  225. struct aac_encoder *enc =data;
  226. return enc->frame_size;
  227. }
  228. struct obs_encoder_info aac_encoder_info = {
  229. .id = "ffmpeg_aac",
  230. .type = OBS_ENCODER_AUDIO,
  231. .codec = "AAC",
  232. .get_name = aac_getname,
  233. .create = aac_create,
  234. .destroy = aac_destroy,
  235. .encode = aac_encode,
  236. .get_frame_size = aac_frame_size,
  237. .get_defaults = aac_defaults,
  238. .get_properties = aac_properties,
  239. .get_extra_data = aac_extra_data,
  240. .get_audio_info = aac_audio_info
  241. };