obs-ffmpeg-output.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. /******************************************************************************
  2. Copyright (C) 2013 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 <obs.h>
  15. #include "obs-ffmpeg-output.h"
  16. /* TODO: remove these later */
  17. #define FILENAME_TODO "D:\\test.mp4"
  18. #define SPS_TODO 44100
  19. static inline enum AVPixelFormat obs_to_ffmpeg_video_format(
  20. enum video_format format)
  21. {
  22. switch (format) {
  23. case VIDEO_FORMAT_NONE: return AV_PIX_FMT_NONE;
  24. case VIDEO_FORMAT_I420: return AV_PIX_FMT_YUV420P;
  25. case VIDEO_FORMAT_NV12: return AV_PIX_FMT_NV12;
  26. case VIDEO_FORMAT_YVYU: return AV_PIX_FMT_NONE;
  27. case VIDEO_FORMAT_YUY2: return AV_PIX_FMT_YUYV422;
  28. case VIDEO_FORMAT_UYVY: return AV_PIX_FMT_UYVY422;
  29. case VIDEO_FORMAT_YUVX: return AV_PIX_FMT_NONE;
  30. case VIDEO_FORMAT_UYVX: return AV_PIX_FMT_NONE;
  31. case VIDEO_FORMAT_RGBA: return AV_PIX_FMT_RGBA;
  32. case VIDEO_FORMAT_BGRA: return AV_PIX_FMT_BGRA;
  33. case VIDEO_FORMAT_BGRX: return AV_PIX_FMT_BGRA;
  34. }
  35. return AV_PIX_FMT_NONE;
  36. }
  37. static bool new_stream(struct ffmpeg_data *data, AVStream **stream,
  38. AVCodec **codec, enum AVCoecID id)
  39. {
  40. *codec = avcodec_find_encoder(id);
  41. if (!*codec) {
  42. blog(LOG_ERROR, "Couldn't find encoder '%s'",
  43. avcodec_get_name(id));
  44. return false;
  45. }
  46. *stream = avformat_new_stream(data->output, *codec);
  47. if (!*stream) {
  48. blog(LOG_ERROR, "Couldn't create stream for encoder '%s'",
  49. avcodec_get_name(id));
  50. return false;
  51. }
  52. (*stream)->id = data->output->nb_streams-1;
  53. return true;
  54. }
  55. static bool open_video_codec(struct ffmpeg_data *data,
  56. struct obs_video_info *ovi)
  57. {
  58. AVCodecContext *context = data->video->codec;
  59. int ret;
  60. ret = avcodec_open2(context, data->vcodec, NULL);
  61. if (ret < 0) {
  62. blog(LOG_ERROR, "Failed to open video codec: %s",
  63. av_err2str(ret));
  64. return false;
  65. }
  66. data->vframe = av_frame_alloc();
  67. if (!data->vframe) {
  68. blog(LOG_ERROR, "Failed to allocate video frame");
  69. return false;
  70. }
  71. data->vframe->format = context->pix_fmt;
  72. data->vframe->width = context->width;
  73. data->vframe->height = context->height;
  74. ret = avpicture_alloc(&data->dst_picture, context->pix_fmt,
  75. context->width, context->height);
  76. if (ret < 0) {
  77. blog(LOG_ERROR, "Failed to allocate dst_picture: %s",
  78. av_err2str(ret));
  79. return false;
  80. }
  81. if (context->pix_fmt != AV_PIX_FMT_YUV420P) {
  82. ret = avpicture_alloc(&data->src_picture, AV_PIX_FMT_YUV420P,
  83. context->width, context->height);
  84. if (ret < 0) {
  85. blog(LOG_ERROR, "Failed to allocate src_picture: %s",
  86. av_err2str(ret));
  87. return false;
  88. }
  89. }
  90. *((AVPicture*)data->vframe) = data->dst_picture;
  91. return true;
  92. }
  93. static bool create_video_stream(struct ffmpeg_data *data)
  94. {
  95. AVCodecContext *context;
  96. struct obs_video_info ovi;
  97. if (!obs_get_video_info(&ovi)) {
  98. blog(LOG_ERROR, "No active video");
  99. return false;
  100. }
  101. if (!new_stream(data, &data->video, &data->vcodec,
  102. data->output->oformat->video_codec))
  103. return false;
  104. context = data->video->codec;
  105. context->codec_id = data->output->oformat->video_codec;
  106. context->bit_rate = 6000000;
  107. context->width = ovi.output_width;
  108. context->height = ovi.output_height;
  109. context->time_base.num = ovi.fps_den;
  110. context->time_base.den = ovi.fps_num;
  111. context->gop_size = 12;
  112. context->pix_fmt = AV_PIX_FMT_YUV420P;
  113. if (data->output->oformat->flags & AVFMT_GLOBALHEADER)
  114. context->flags |= CODEC_FLAG_GLOBAL_HEADER;
  115. return open_video_codec(data, &ovi);
  116. }
  117. static bool open_audio_codec(struct ffmpeg_data *data,
  118. struct audio_output_info *aoi)
  119. {
  120. AVCodecContext *context = data->audio->codec;
  121. int ret;
  122. data->aframe = av_frame_alloc();
  123. if (!data->aframe) {
  124. blog(LOG_ERROR, "Failed to allocate audio frame");
  125. return false;
  126. }
  127. ret = avcodec_open2(context, data->acodec, NULL);
  128. if (ret < 0) {
  129. blog(LOG_ERROR, "Failed to open audio codec: %s",
  130. av_err2str(ret));
  131. return false;
  132. }
  133. return true;
  134. }
  135. static bool create_audio_stream(struct ffmpeg_data *data)
  136. {
  137. AVCodecContext *context;
  138. struct audio_output_info aoi;
  139. if (!obs_get_audio_info(&aoi)) {
  140. blog(LOG_ERROR, "No active audio");
  141. return false;
  142. }
  143. if (!new_stream(data, &data->audio, &data->acodec,
  144. data->output->oformat->audio_codec))
  145. return false;
  146. context = data->audio->codec;
  147. context->bit_rate = 128000;
  148. context->channels = get_audio_channels(aoi.speakers);
  149. context->sample_rate = aoi.samples_per_sec;
  150. if (data->output->oformat->flags & AVFMT_GLOBALHEADER)
  151. context->flags |= CODEC_FLAG_GLOBAL_HEADER;
  152. return open_audio_codec(data, &aoi);
  153. }
  154. static inline bool init_streams(struct ffmpeg_data *data)
  155. {
  156. AVOutputFormat *format = data->output->oformat;
  157. if (format->video_codec != AV_CODEC_ID_NONE) {
  158. if (!create_video_stream(data))
  159. return false;
  160. }
  161. if (format->audio_codec != AV_CODEC_ID_NONE) {
  162. if (!create_audio_stream(data))
  163. return false;
  164. }
  165. return true;
  166. }
  167. static inline bool open_output_file(struct ffmpeg_data *data)
  168. {
  169. AVOutputFormat *format = data->output->oformat;
  170. int ret;
  171. if ((format->flags & AVFMT_NOFILE) == 0) {
  172. ret = avio_open(&data->output->pb, FILENAME_TODO,
  173. AVIO_FLAG_WRITE);
  174. if (ret < 0) {
  175. blog(LOG_ERROR, "Couldn't open file '%s', %s",
  176. FILENAME_TODO, av_err2str(ret));
  177. return false;
  178. }
  179. }
  180. ret = avformat_write_header(data->output, NULL);
  181. if (ret < 0) {
  182. blog(LOG_ERROR, "Error opening file '%s': %s",
  183. FILENAME_TODO, av_err2str(ret));
  184. return false;
  185. }
  186. return true;
  187. }
  188. static void close_video(struct ffmpeg_data *data)
  189. {
  190. avcodec_close(data->video->codec);
  191. av_free(data->src_picture.data[0]);
  192. av_free(data->dst_picture.data[0]);
  193. av_frame_free(&data->vframe);
  194. }
  195. static void close_audio(struct ffmpeg_data *data)
  196. {
  197. avcodec_close(data->audio->codec);
  198. av_free(data->samples[0]);
  199. av_free(data->samples);
  200. av_frame_free(&data->aframe);
  201. }
  202. static void ffmpeg_data_free(struct ffmpeg_data *data)
  203. {
  204. if (data->initialized)
  205. av_write_trailer(data->output);
  206. if (data->video)
  207. close_video(data);
  208. if (data->audio)
  209. close_audio(data);
  210. if ((data->output->oformat->flags & AVFMT_NOFILE) == 0)
  211. avio_close(data->output->pb);
  212. avformat_free_context(data->output);
  213. }
  214. static bool ffmpeg_data_init(struct ffmpeg_data *data)
  215. {
  216. memset(data, 0, sizeof(struct ffmpeg_data));
  217. av_register_all();
  218. /* TODO: settings */
  219. avformat_alloc_output_context2(&data->output, NULL, NULL,
  220. "D:\\test.mp4");
  221. if (!data->output) {
  222. blog(LOG_ERROR, "Couldn't create avformat context");
  223. goto fail;
  224. }
  225. if (!init_streams(data))
  226. goto fail;
  227. if (!open_output_file(data))
  228. goto fail;
  229. data->initialized = true;
  230. return true;
  231. fail:
  232. blog(LOG_ERROR, "ffmpeg_data_init failed");
  233. ffmpeg_data_free(data);
  234. return false;
  235. }
  236. /* ------------------------------------------------------------------------- */
  237. const char *ffmpeg_output_getname(const char *locale)
  238. {
  239. /* TODO: translation */
  240. return "FFmpeg file output";
  241. }
  242. struct ffmpeg_output *ffmpeg_output_create(const char *settings,
  243. obs_output_t output)
  244. {
  245. struct ffmpeg_output *data = bmalloc(sizeof(struct ffmpeg_output));
  246. memset(data, 0, sizeof(struct ffmpeg_output));
  247. data->output = output;
  248. return data;
  249. }
  250. void ffmpeg_output_destroy(struct ffmpeg_output *data)
  251. {
  252. if (data) {
  253. ffmpeg_data_free(&data->ff_data);
  254. bfree(data);
  255. }
  256. }
  257. void ffmpeg_output_update(struct ffmpeg_output *data, const char *settings)
  258. {
  259. }
  260. static void receive_video(void *param, const struct video_frame *frame)
  261. {
  262. }
  263. static void receive_audio(void *param, const struct audio_data *frame)
  264. {
  265. }
  266. bool ffmpeg_output_start(struct ffmpeg_output *data)
  267. {
  268. video_t video = obs_video();
  269. audio_t audio = obs_audio();
  270. if (!video || !audio) {
  271. blog(LOG_ERROR, "ffmpeg_output_start: audio and video must "
  272. "both be active (at least as of this writing)");
  273. return false;
  274. }
  275. if (!ffmpeg_data_init(&data->ff_data))
  276. return false;
  277. struct audio_convert_info aci;
  278. aci.samples_per_sec = SPS_TODO;
  279. aci.format = AUDIO_FORMAT_16BIT;
  280. aci.speakers = SPEAKERS_STEREO;
  281. struct video_convert_info vci;
  282. vci.format = VIDEO_FORMAT_I420;
  283. vci.width = 0;
  284. vci.height = 0;
  285. vci.row_align = 1;
  286. video_output_connect(video, &vci, receive_video, data);
  287. audio_output_connect(audio, &aci, receive_audio, data);
  288. data->active = true;
  289. return true;
  290. }
  291. void ffmpeg_output_stop(struct ffmpeg_output *data)
  292. {
  293. if (data->active) {
  294. data->active = false;
  295. video_output_disconnect(obs_video(), receive_video, data);
  296. audio_output_disconnect(obs_audio(), receive_audio, data);
  297. ffmpeg_data_free(&data->ff_data);
  298. }
  299. }
  300. bool ffmpeg_output_active(struct ffmpeg_output *data)
  301. {
  302. return data->active;
  303. }