obs-ffmpeg-source.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. /*
  2. * Copyright (c) 2015 John R. Bradley <[email protected]>
  3. *
  4. * Permission to use, copy, modify, and distribute this software for any
  5. * purpose with or without fee is hereby granted, provided that the above
  6. * copyright notice and this permission notice appear in all copies.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  11. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  13. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  14. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #include <obs-module.h>
  17. #include <util/platform.h>
  18. #include "obs-ffmpeg-compat.h"
  19. #include "obs-ffmpeg-formats.h"
  20. #include <libff/ff-demuxer.h>
  21. #include <libswscale/swscale.h>
  22. static bool video_frame(struct ff_frame *frame, void *opaque);
  23. static bool video_format(AVCodecContext *codec_context, void *opaque);
  24. static void ffmpeg_log_callback(void* context, int level, const char* format,
  25. va_list args)
  26. {
  27. UNUSED_PARAMETER(context);
  28. int obs_level;
  29. switch (level) {
  30. case AV_LOG_PANIC:
  31. case AV_LOG_FATAL:
  32. obs_level = LOG_ERROR;
  33. break;
  34. case AV_LOG_ERROR:
  35. case AV_LOG_WARNING:
  36. obs_level = LOG_WARNING;
  37. break;
  38. case AV_LOG_INFO:
  39. case AV_LOG_VERBOSE:
  40. obs_level = LOG_INFO;
  41. break;
  42. case AV_LOG_DEBUG:
  43. default:
  44. obs_level = LOG_DEBUG;
  45. }
  46. blogva(obs_level, format, args);
  47. }
  48. void initialize_ffmpeg_source()
  49. {
  50. av_log_set_callback(ffmpeg_log_callback);
  51. }
  52. struct ffmpeg_source {
  53. struct ff_demuxer *demuxer;
  54. struct SwsContext *sws_ctx;
  55. uint8_t *sws_data;
  56. int sws_linesize;
  57. obs_source_t *source;
  58. enum AVPixelFormat format;
  59. bool is_forcing_scale;
  60. bool is_hw_decoding;
  61. };
  62. static bool set_obs_frame_colorprops(struct ff_frame *frame,
  63. struct obs_source_frame *obs_frame)
  64. {
  65. enum AVColorSpace frame_cs = av_frame_get_colorspace(frame->frame);
  66. enum video_colorspace obs_cs;
  67. switch(frame_cs) {
  68. case AVCOL_SPC_BT709: obs_cs = VIDEO_CS_709; break;
  69. case AVCOL_SPC_SMPTE170M:
  70. case AVCOL_SPC_BT470BG: obs_cs = VIDEO_CS_601; break;
  71. case AVCOL_SPC_UNSPECIFIED: obs_cs = VIDEO_CS_DEFAULT; break;
  72. default:
  73. blog(LOG_WARNING, "frame using an unsupported colorspace %d",
  74. frame_cs);
  75. return false;
  76. }
  77. enum video_range_type range;
  78. obs_frame->format = ffmpeg_to_obs_video_format(frame->frame->format);
  79. obs_frame->full_range =
  80. frame->frame->color_range == AVCOL_RANGE_JPEG;
  81. range = obs_frame->full_range ? VIDEO_RANGE_FULL : VIDEO_RANGE_PARTIAL;
  82. if (!video_format_get_parameters(obs_cs,
  83. range, obs_frame->color_matrix,
  84. obs_frame->color_range_min,
  85. obs_frame->color_range_max)) {
  86. blog(LOG_ERROR, "Failed to get video format "
  87. "parameters for video format %u",
  88. obs_cs);
  89. return false;
  90. }
  91. return true;
  92. }
  93. bool update_sws_context(struct ffmpeg_source *source, AVFrame *frame)
  94. {
  95. struct SwsContext *sws_ctx = sws_getCachedContext(
  96. source->sws_ctx,
  97. frame->width,
  98. frame->height,
  99. frame->format,
  100. frame->width,
  101. frame->height,
  102. AV_PIX_FMT_BGRA,
  103. SWS_BILINEAR,
  104. NULL, NULL, NULL);
  105. if (sws_ctx != NULL && sws_ctx != source->sws_ctx) {
  106. if (source->sws_data != NULL)
  107. bfree(source->sws_data);
  108. source->sws_data = bzalloc(frame->width * frame->height * 4);
  109. }
  110. source->sws_ctx = sws_ctx;
  111. source->sws_linesize = frame->width * 4;
  112. return source->sws_ctx != NULL;
  113. }
  114. static bool video_frame_scale(struct ff_frame *frame,
  115. struct ffmpeg_source *s, struct obs_source_frame *obs_frame)
  116. {
  117. if (!update_sws_context(s, frame->frame))
  118. return false;
  119. sws_scale(
  120. s->sws_ctx,
  121. (uint8_t const *const *)frame->frame->data,
  122. frame->frame->linesize,
  123. 0,
  124. frame->frame->height,
  125. &s->sws_data,
  126. &s->sws_linesize
  127. );
  128. obs_frame->data[0] = s->sws_data;
  129. obs_frame->linesize[0] = s->sws_linesize;
  130. obs_frame->format = VIDEO_FORMAT_BGRA;
  131. obs_source_output_video(s->source, obs_frame);
  132. return true;
  133. }
  134. static bool video_frame_hwaccel(struct ff_frame *frame,
  135. obs_source_t *source, struct obs_source_frame *obs_frame)
  136. {
  137. // 4th plane is pixelbuf reference for mac
  138. for (int i = 0; i < 3; i++) {
  139. obs_frame->data[i] = frame->frame->data[i];
  140. obs_frame->linesize[i] = frame->frame->linesize[i];
  141. }
  142. if (!set_obs_frame_colorprops(frame, obs_frame))
  143. return false;
  144. obs_source_output_video(source, obs_frame);
  145. return true;
  146. }
  147. static bool video_frame_direct(struct ff_frame *frame,
  148. struct ffmpeg_source *s, struct obs_source_frame *obs_frame)
  149. {
  150. int i;
  151. for (i = 0; i < MAX_AV_PLANES; i++) {
  152. obs_frame->data[i] = frame->frame->data[i];
  153. obs_frame->linesize[i] = frame->frame->linesize[i];
  154. }
  155. if (!set_obs_frame_colorprops(frame, obs_frame))
  156. return false;
  157. obs_source_output_video(s->source, obs_frame);
  158. return true;
  159. }
  160. static bool video_frame(struct ff_frame *frame, void *opaque)
  161. {
  162. struct ffmpeg_source *s = opaque;
  163. struct obs_source_frame obs_frame = {0};
  164. double d_pts = ff_get_sync_clock(&s->demuxer->clock) - frame->pts;
  165. uint64_t pts = os_gettime_ns() - (uint64_t)(d_pts * 1000000000.0L);
  166. obs_frame.timestamp = pts;
  167. obs_frame.width = frame->frame->width;
  168. obs_frame.height = frame->frame->height;
  169. enum video_format format =
  170. ffmpeg_to_obs_video_format(frame->frame->format);
  171. if (s->is_forcing_scale || format == VIDEO_FORMAT_NONE)
  172. return video_frame_scale(frame, s, &obs_frame);
  173. else if (s->is_hw_decoding)
  174. return video_frame_hwaccel(frame, s->source, &obs_frame);
  175. else
  176. return video_frame_direct(frame, s, &obs_frame);
  177. }
  178. static bool audio_frame(struct ff_frame *frame, void *opaque)
  179. {
  180. struct ffmpeg_source *s = opaque;
  181. struct obs_source_audio audio_data = {0};
  182. double d_pts = ff_get_sync_clock(&s->demuxer->clock) - frame->pts;
  183. uint64_t pts = os_gettime_ns() - (uint64_t)(d_pts * 1000000000.0L);
  184. int channels = av_get_channel_layout_nb_channels(
  185. av_frame_get_channel_layout(frame->frame));
  186. for(int i = 0; i < channels; i++)
  187. audio_data.data[i] = frame->frame->data[i];
  188. audio_data.samples_per_sec = frame->frame->sample_rate;
  189. audio_data.frames = frame->frame->nb_samples;
  190. audio_data.timestamp = pts;
  191. audio_data.format =
  192. convert_ffmpeg_sample_format(frame->frame->format);
  193. audio_data.speakers = channels;
  194. obs_source_output_audio(s->source, &audio_data);
  195. return true;
  196. }
  197. static bool is_local_file_modified(obs_properties_t *props,
  198. obs_property_t *prop, obs_data_t *settings)
  199. {
  200. UNUSED_PARAMETER(prop);
  201. bool enabled = obs_data_get_bool(settings, "is_local_file");
  202. obs_property_t *input = obs_properties_get(props, "input");
  203. obs_property_t *input_format =obs_properties_get(props,
  204. "input_format");
  205. obs_property_t *local_file = obs_properties_get(props, "local_file");
  206. obs_property_t *looping = obs_properties_get(props, "looping");
  207. obs_property_set_visible(input, !enabled);
  208. obs_property_set_visible(input_format, !enabled);
  209. obs_property_set_visible(local_file, enabled);
  210. obs_property_set_visible(looping, enabled);
  211. return true;
  212. }
  213. static bool is_advanced_modified(obs_properties_t *props,
  214. obs_property_t *prop, obs_data_t *settings)
  215. {
  216. UNUSED_PARAMETER(prop);
  217. bool enabled = obs_data_get_bool(settings, "advanced");
  218. obs_property_t *abuf = obs_properties_get(props, "audio_buffer_size");
  219. obs_property_t *vbuf = obs_properties_get(props, "video_buffer_size");
  220. obs_property_t *frame_drop = obs_properties_get(props, "frame_drop");
  221. obs_property_set_visible(abuf, enabled);
  222. obs_property_set_visible(vbuf, enabled);
  223. obs_property_set_visible(frame_drop, enabled);
  224. return true;
  225. }
  226. static obs_properties_t *ffmpeg_source_getproperties(void *data)
  227. {
  228. UNUSED_PARAMETER(data);
  229. obs_properties_t *props = obs_properties_create();
  230. obs_property_t *prop;
  231. // use this when obs allows non-readonly paths
  232. prop = obs_properties_add_bool(props, "is_local_file",
  233. obs_module_text("LocalFile"));
  234. obs_property_set_modified_callback(prop, is_local_file_modified);
  235. obs_properties_add_path(props, "local_file",
  236. obs_module_text("LocalFile"), OBS_PATH_FILE, "*.*",
  237. NULL);
  238. obs_properties_add_bool(props, "looping", obs_module_text("Looping"));
  239. obs_properties_add_text(props, "input",
  240. obs_module_text("Input"), OBS_TEXT_DEFAULT);
  241. obs_properties_add_text(props, "input_format",
  242. obs_module_text("InputFormat"), OBS_TEXT_DEFAULT);
  243. obs_properties_add_bool(props, "force_scale",
  244. obs_module_text("ForceFormat"));
  245. obs_properties_add_bool(props, "hw_decode",
  246. obs_module_text("HardwareDecode"));
  247. prop = obs_properties_add_bool(props, "advanced",
  248. obs_module_text("Advanced"));
  249. obs_property_set_modified_callback(prop, is_advanced_modified);
  250. prop = obs_properties_add_int(props, "audio_buffer_size",
  251. obs_module_text("AudioBufferSize"), 1, 9999, 1);
  252. obs_property_set_visible(prop, false);
  253. prop = obs_properties_add_int(props, "video_buffer_size",
  254. obs_module_text("VideoBufferSize"), 1, 9999, 1);
  255. obs_property_set_visible(prop, false);
  256. prop = obs_properties_add_list(props, "frame_drop",
  257. obs_module_text("FrameDropping"), OBS_COMBO_TYPE_LIST,
  258. OBS_COMBO_FORMAT_INT);
  259. obs_property_list_add_int(prop, obs_module_text("DiscardNone"),
  260. AVDISCARD_NONE);
  261. obs_property_list_add_int(prop, obs_module_text("DiscardDefault"),
  262. AVDISCARD_DEFAULT);
  263. obs_property_list_add_int(prop, obs_module_text("DiscardNonRef"),
  264. AVDISCARD_NONREF);
  265. obs_property_list_add_int(prop, obs_module_text("DiscardBiDir"),
  266. AVDISCARD_BIDIR);
  267. obs_property_list_add_int(prop, obs_module_text("DiscardNonIntra"),
  268. AVDISCARD_NONINTRA);
  269. obs_property_list_add_int(prop, obs_module_text("DiscardNonKey"),
  270. AVDISCARD_NONKEY);
  271. obs_property_list_add_int(prop, obs_module_text("DiscardAll"),
  272. AVDISCARD_ALL);
  273. obs_property_set_visible(prop, false);
  274. return props;
  275. }
  276. static void ffmpeg_source_update(void *data, obs_data_t *settings)
  277. {
  278. struct ffmpeg_source *s = data;
  279. bool is_local_file = obs_data_get_bool(settings, "is_local_file");
  280. bool is_advanced = obs_data_get_bool(settings, "advanced");
  281. bool is_looping;
  282. char *input;
  283. char *input_format;
  284. if (is_local_file) {
  285. input = (char *)obs_data_get_string(settings, "local_file");
  286. input_format = NULL;
  287. is_looping = obs_data_get_bool(settings, "looping");
  288. } else {
  289. input = (char *)obs_data_get_string(settings, "input");
  290. input_format = (char *)obs_data_get_string(settings,
  291. "input_format");
  292. is_looping = false;
  293. }
  294. s->is_forcing_scale = obs_data_get_bool(settings, "force_scale");
  295. s->is_hw_decoding = obs_data_get_bool(settings, "hw_decode");
  296. if (s->demuxer != NULL)
  297. ff_demuxer_free(s->demuxer);
  298. s->demuxer = ff_demuxer_init();
  299. s->demuxer->options.is_hw_decoding = s->is_hw_decoding;
  300. s->demuxer->options.is_looping = is_looping;
  301. if (is_advanced) {
  302. int audio_buffer_size = (int)obs_data_get_int(settings,
  303. "audio_buffer_size");
  304. int video_buffer_size = (int)obs_data_get_int(settings,
  305. "video_buffer_size");
  306. enum AVDiscard frame_drop =
  307. (enum AVDiscard)obs_data_get_int(settings,
  308. "frame_drop");
  309. if (audio_buffer_size < 1) {
  310. audio_buffer_size = 1;
  311. blog(LOG_WARNING, "invalid audio_buffer_size %d",
  312. audio_buffer_size);
  313. }
  314. if (video_buffer_size < 1) {
  315. video_buffer_size = 1;
  316. blog(LOG_WARNING, "invalid audio_buffer_size %d",
  317. audio_buffer_size);
  318. }
  319. s->demuxer->options.audio_frame_queue_size = audio_buffer_size;
  320. s->demuxer->options.video_frame_queue_size = video_buffer_size;
  321. if (frame_drop < AVDISCARD_NONE || frame_drop > AVDISCARD_ALL) {
  322. frame_drop = AVDISCARD_NONE;
  323. blog(LOG_WARNING, "invalid frame_drop %d", frame_drop);
  324. }
  325. s->demuxer->options.frame_drop = frame_drop;
  326. }
  327. ff_demuxer_set_callbacks(&s->demuxer->video_callbacks,
  328. video_frame, NULL,
  329. NULL, NULL, NULL, s);
  330. ff_demuxer_set_callbacks(&s->demuxer->audio_callbacks,
  331. audio_frame, NULL,
  332. NULL, NULL, NULL, s);
  333. ff_demuxer_open(s->demuxer, input, input_format);
  334. }
  335. static const char *ffmpeg_source_getname(void)
  336. {
  337. return obs_module_text("FFMpegSource");
  338. }
  339. static void *ffmpeg_source_create(obs_data_t *settings, obs_source_t *source)
  340. {
  341. UNUSED_PARAMETER(settings);
  342. struct ffmpeg_source *s = bzalloc(sizeof(struct ffmpeg_source));
  343. s->source = source;
  344. ffmpeg_source_update(s, settings);
  345. return s;
  346. }
  347. static void ffmpeg_source_destroy(void *data)
  348. {
  349. struct ffmpeg_source *s = data;
  350. ff_demuxer_free(s->demuxer);
  351. if (s->sws_ctx != NULL)
  352. sws_freeContext(s->sws_ctx);
  353. if (s->sws_data != NULL)
  354. bfree(s->sws_data);
  355. bfree(s);
  356. }
  357. struct obs_source_info ffmpeg_source = {
  358. .id = "ffmpeg_source",
  359. .type = OBS_SOURCE_TYPE_INPUT,
  360. .output_flags = OBS_SOURCE_ASYNC_VIDEO | OBS_SOURCE_AUDIO,
  361. .get_name = ffmpeg_source_getname,
  362. .create = ffmpeg_source_create,
  363. .destroy = ffmpeg_source_destroy,
  364. .get_properties = ffmpeg_source_getproperties,
  365. .update = ffmpeg_source_update
  366. };