obs-ffmpeg-aac.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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. #define do_log(level, format, ...) \
  22. blog(level, "[FFmpeg aac encoder: '%s'] " format, \
  23. obs_encoder_get_name(enc->encoder), ##__VA_ARGS__)
  24. #define warn(format, ...) do_log(LOG_WARNING, format, ##__VA_ARGS__)
  25. #define info(format, ...) do_log(LOG_INFO, format, ##__VA_ARGS__)
  26. #define debug(format, ...) do_log(LOG_DEBUG, format, ##__VA_ARGS__)
  27. struct aac_encoder {
  28. obs_encoder_t *encoder;
  29. AVCodec *aac;
  30. AVCodecContext *context;
  31. uint8_t *samples[MAX_AV_PLANES];
  32. AVFrame *aframe;
  33. int64_t total_samples;
  34. DARRAY(uint8_t) packet_buffer;
  35. size_t audio_planes;
  36. size_t audio_size;
  37. int frame_size; /* pretty much always 1024 for AAC */
  38. int frame_size_bytes;
  39. };
  40. static const char *aac_getname(void *unused)
  41. {
  42. UNUSED_PARAMETER(unused);
  43. return obs_module_text("FFmpegAAC");
  44. }
  45. static void aac_destroy(void *data)
  46. {
  47. struct aac_encoder *enc = data;
  48. if (enc->samples[0])
  49. av_freep(&enc->samples[0]);
  50. if (enc->context)
  51. avcodec_close(enc->context);
  52. if (enc->aframe)
  53. av_frame_free(&enc->aframe);
  54. da_free(enc->packet_buffer);
  55. bfree(enc);
  56. }
  57. static bool initialize_codec(struct aac_encoder *enc)
  58. {
  59. int ret;
  60. enc->aframe = av_frame_alloc();
  61. if (!enc->aframe) {
  62. warn("Failed to allocate audio frame");
  63. return false;
  64. }
  65. ret = avcodec_open2(enc->context, enc->aac, NULL);
  66. if (ret < 0) {
  67. warn("Failed to open AAC codec: %s", av_err2str(ret));
  68. return false;
  69. }
  70. enc->frame_size = enc->context->frame_size;
  71. if (!enc->frame_size)
  72. enc->frame_size = 1024;
  73. enc->frame_size_bytes = enc->frame_size * (int)enc->audio_size;
  74. ret = av_samples_alloc(enc->samples, NULL, enc->context->channels,
  75. enc->frame_size, enc->context->sample_fmt, 0);
  76. if (ret < 0) {
  77. warn("Failed to create audio buffer: %s", av_err2str(ret));
  78. return false;
  79. }
  80. return true;
  81. }
  82. static void init_sizes(struct aac_encoder *enc, audio_t *audio)
  83. {
  84. const struct audio_output_info *aoi;
  85. enum audio_format format;
  86. aoi = audio_output_get_info(audio);
  87. format = convert_ffmpeg_sample_format(enc->context->sample_fmt);
  88. enc->audio_planes = get_audio_planes(format, aoi->speakers);
  89. enc->audio_size = get_audio_size(format, aoi->speakers, 1);
  90. }
  91. #ifndef MIN
  92. #define MIN(x, y) ((x) < (y) ? (x) : (y))
  93. #endif
  94. static void *aac_create(obs_data_t *settings, obs_encoder_t *encoder)
  95. {
  96. struct aac_encoder *enc;
  97. int bitrate = (int)obs_data_get_int(settings, "bitrate");
  98. audio_t *audio = obs_encoder_audio(encoder);
  99. avcodec_register_all();
  100. enc = bzalloc(sizeof(struct aac_encoder));
  101. enc->encoder = encoder;
  102. enc->aac = avcodec_find_encoder(AV_CODEC_ID_AAC);
  103. blog(LOG_INFO, "---------------------------------");
  104. if (!enc->aac) {
  105. warn("Couldn't find encoder");
  106. goto fail;
  107. }
  108. if (!bitrate) {
  109. warn("Invalid bitrate specified");
  110. return NULL;
  111. }
  112. enc->context = avcodec_alloc_context3(enc->aac);
  113. if (!enc->context) {
  114. warn("Failed to create codec context");
  115. goto fail;
  116. }
  117. enc->context->bit_rate = bitrate * 1000;
  118. enc->context->channels = (int)audio_output_get_channels(audio);
  119. enc->context->sample_rate = audio_output_get_sample_rate(audio);
  120. enc->context->sample_fmt = enc->aac->sample_fmts ?
  121. enc->aac->sample_fmts[0] : AV_SAMPLE_FMT_FLTP;
  122. /* if using FFmpeg's AAC encoder, at least set a cutoff value
  123. * (recommended by konverter) */
  124. if (strcmp(enc->aac->name, "aac") == 0) {
  125. int cutoff1 = 4000 + (int)enc->context->bit_rate / 8;
  126. int cutoff2 = 12000 + (int)enc->context->bit_rate / 8;
  127. int cutoff3 = enc->context->sample_rate / 2;
  128. int cutoff;
  129. cutoff = MIN(cutoff1, cutoff2);
  130. cutoff = MIN(cutoff, cutoff3);
  131. enc->context->cutoff = cutoff;
  132. }
  133. info("bitrate: %d, channels: %d",
  134. enc->context->bit_rate / 1000, enc->context->channels);
  135. init_sizes(enc, audio);
  136. /* enable experimental FFmpeg encoder if the only one available */
  137. enc->context->strict_std_compliance = -2;
  138. enc->context->flags = CODEC_FLAG_GLOBAL_HEADER;
  139. if (initialize_codec(enc))
  140. return enc;
  141. fail:
  142. aac_destroy(enc);
  143. return NULL;
  144. }
  145. static bool do_aac_encode(struct aac_encoder *enc,
  146. struct encoder_packet *packet, bool *received_packet)
  147. {
  148. AVRational time_base = {1, enc->context->sample_rate};
  149. AVPacket avpacket = {0};
  150. int got_packet;
  151. int ret;
  152. enc->aframe->nb_samples = enc->frame_size;
  153. enc->aframe->pts = av_rescale_q(enc->total_samples,
  154. (AVRational){1, enc->context->sample_rate},
  155. enc->context->time_base);
  156. ret = avcodec_fill_audio_frame(enc->aframe, enc->context->channels,
  157. enc->context->sample_fmt, enc->samples[0],
  158. enc->frame_size_bytes * enc->context->channels, 1);
  159. if (ret < 0) {
  160. warn("avcodec_fill_audio_frame failed: %s", av_err2str(ret));
  161. return false;
  162. }
  163. enc->total_samples += enc->frame_size;
  164. ret = avcodec_encode_audio2(enc->context, &avpacket, enc->aframe,
  165. &got_packet);
  166. if (ret < 0) {
  167. warn("avcodec_encode_audio2 failed: %s", av_err2str(ret));
  168. return false;
  169. }
  170. *received_packet = !!got_packet;
  171. if (!got_packet)
  172. return true;
  173. da_resize(enc->packet_buffer, 0);
  174. da_push_back_array(enc->packet_buffer, avpacket.data, avpacket.size);
  175. packet->pts = rescale_ts(avpacket.pts, enc->context, time_base);
  176. packet->dts = rescale_ts(avpacket.dts, enc->context, time_base);
  177. packet->data = enc->packet_buffer.array;
  178. packet->size = avpacket.size;
  179. packet->type = OBS_ENCODER_AUDIO;
  180. packet->timebase_num = 1;
  181. packet->timebase_den = (int32_t)enc->context->sample_rate;
  182. av_free_packet(&avpacket);
  183. return true;
  184. }
  185. static bool aac_encode(void *data, struct encoder_frame *frame,
  186. struct encoder_packet *packet, bool *received_packet)
  187. {
  188. struct aac_encoder *enc = data;
  189. for (size_t i = 0; i < enc->audio_planes; i++)
  190. memcpy(enc->samples[i], frame->data[i], enc->frame_size_bytes);
  191. return do_aac_encode(enc, packet, received_packet);
  192. }
  193. static void aac_defaults(obs_data_t *settings)
  194. {
  195. obs_data_set_default_int(settings, "bitrate", 128);
  196. }
  197. static obs_properties_t *aac_properties(void *unused)
  198. {
  199. UNUSED_PARAMETER(unused);
  200. obs_properties_t *props = obs_properties_create();
  201. obs_properties_add_int(props, "bitrate",
  202. obs_module_text("Bitrate"), 32, 320, 32);
  203. return props;
  204. }
  205. static bool aac_extra_data(void *data, uint8_t **extra_data, size_t *size)
  206. {
  207. struct aac_encoder *enc = data;
  208. *extra_data = enc->context->extradata;
  209. *size = enc->context->extradata_size;
  210. return true;
  211. }
  212. static void aac_audio_info(void *data, struct audio_convert_info *info)
  213. {
  214. struct aac_encoder *enc = data;
  215. info->format = convert_ffmpeg_sample_format(enc->context->sample_fmt);
  216. }
  217. static size_t aac_frame_size(void *data)
  218. {
  219. struct aac_encoder *enc =data;
  220. return enc->frame_size;
  221. }
  222. struct obs_encoder_info aac_encoder_info = {
  223. .id = "ffmpeg_aac",
  224. .type = OBS_ENCODER_AUDIO,
  225. .codec = "AAC",
  226. .get_name = aac_getname,
  227. .create = aac_create,
  228. .destroy = aac_destroy,
  229. .encode = aac_encode,
  230. .get_frame_size = aac_frame_size,
  231. .get_defaults = aac_defaults,
  232. .get_properties = aac_properties,
  233. .get_extra_data = aac_extra_data,
  234. .get_audio_info = aac_audio_info
  235. };