obs-ffmpeg-source.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  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_BT470BG: obs_cs = VIDEO_CS_601; break;
  70. case AVCOL_SPC_UNSPECIFIED: obs_cs = VIDEO_CS_DEFAULT; break;
  71. default:
  72. blog(LOG_WARNING, "frame using an unsupported colorspace %d",
  73. frame_cs);
  74. return false;
  75. }
  76. enum video_range_type range;
  77. obs_frame->format = ffmpeg_to_obs_video_format(frame->frame->format);
  78. obs_frame->full_range =
  79. frame->frame->color_range == AVCOL_RANGE_JPEG;
  80. range = obs_frame->full_range ? VIDEO_RANGE_FULL : VIDEO_RANGE_PARTIAL;
  81. if (!video_format_get_parameters(obs_cs,
  82. range, obs_frame->color_matrix,
  83. obs_frame->color_range_min,
  84. obs_frame->color_range_max)) {
  85. blog(LOG_ERROR, "Failed to get video format "
  86. "parameters for video format %u",
  87. obs_cs);
  88. return false;
  89. }
  90. return true;
  91. }
  92. bool update_sws_context(struct ffmpeg_source *source, AVFrame *frame)
  93. {
  94. struct SwsContext *sws_ctx = sws_getCachedContext(
  95. source->sws_ctx,
  96. frame->width,
  97. frame->height,
  98. frame->format,
  99. frame->width,
  100. frame->height,
  101. AV_PIX_FMT_BGRA,
  102. SWS_BILINEAR,
  103. NULL, NULL, NULL);
  104. if (sws_ctx != NULL && sws_ctx != source->sws_ctx) {
  105. if (source->sws_data != NULL)
  106. bfree(source->sws_data);
  107. source->sws_data = bzalloc(frame->width * frame->height * 4);
  108. }
  109. source->sws_ctx = sws_ctx;
  110. source->sws_linesize = frame->width * 4;
  111. return source->sws_ctx != NULL;
  112. }
  113. static bool video_frame_scale(struct ff_frame *frame,
  114. struct ffmpeg_source *s, struct obs_source_frame *obs_frame)
  115. {
  116. if (!update_sws_context(s, frame->frame))
  117. return false;
  118. sws_scale(
  119. s->sws_ctx,
  120. (uint8_t const *const *)frame->frame->data,
  121. frame->frame->linesize,
  122. 0,
  123. frame->frame->height,
  124. &s->sws_data,
  125. &s->sws_linesize
  126. );
  127. obs_frame->data[0] = s->sws_data;
  128. obs_frame->linesize[0] = s->sws_linesize;
  129. obs_frame->format = VIDEO_FORMAT_BGRA;
  130. obs_source_output_video(s->source, obs_frame);
  131. return true;
  132. }
  133. static bool video_frame_hwaccel(struct ff_frame *frame,
  134. obs_source_t *source, struct obs_source_frame *obs_frame)
  135. {
  136. // 4th plane is pixelbuf reference for mac
  137. for (int i = 0; i < 3; i++) {
  138. obs_frame->data[i] = frame->frame->data[i];
  139. obs_frame->linesize[i] = frame->frame->linesize[i];
  140. }
  141. if (!set_obs_frame_colorprops(frame, obs_frame))
  142. return false;
  143. obs_source_output_video(source, obs_frame);
  144. return true;
  145. }
  146. static bool video_frame_direct(struct ff_frame *frame,
  147. struct ffmpeg_source *s, struct obs_source_frame *obs_frame)
  148. {
  149. int i;
  150. for (i = 0; i < MAX_AV_PLANES; i++) {
  151. obs_frame->data[i] = frame->frame->data[i];
  152. obs_frame->linesize[i] = frame->frame->linesize[i];
  153. }
  154. if (!set_obs_frame_colorprops(frame, obs_frame))
  155. return false;
  156. obs_source_output_video(s->source, obs_frame);
  157. return true;
  158. }
  159. static bool video_frame(struct ff_frame *frame, void *opaque)
  160. {
  161. struct ffmpeg_source *s = opaque;
  162. struct obs_source_frame obs_frame = {0};
  163. double d_pts = ff_get_sync_clock(&s->demuxer->clock) - frame->pts;
  164. uint64_t pts = os_gettime_ns() - (uint64_t)(d_pts * 1000000000.0L);
  165. obs_frame.timestamp = pts;
  166. obs_frame.width = frame->frame->width;
  167. obs_frame.height = frame->frame->height;
  168. enum video_format format =
  169. ffmpeg_to_obs_video_format(frame->frame->format);
  170. if (s->is_forcing_scale || format == VIDEO_FORMAT_NONE)
  171. return video_frame_scale(frame, s, &obs_frame);
  172. else if (s->is_hw_decoding)
  173. return video_frame_hwaccel(frame, s->source, &obs_frame);
  174. else
  175. return video_frame_direct(frame, s, &obs_frame);
  176. }
  177. static bool audio_frame(struct ff_frame *frame, void *opaque)
  178. {
  179. struct ffmpeg_source *s = opaque;
  180. struct obs_source_audio audio_data = {0};
  181. double d_pts = ff_get_sync_clock(&s->demuxer->clock) - frame->pts;
  182. uint64_t pts = os_gettime_ns() - (uint64_t)(d_pts * 1000000000.0L);
  183. int channels = av_get_channel_layout_nb_channels(
  184. av_frame_get_channel_layout(frame->frame));
  185. for(int i = 0; i < channels; i++)
  186. audio_data.data[i] = frame->frame->data[i];
  187. audio_data.samples_per_sec = frame->frame->sample_rate;
  188. audio_data.frames = frame->frame->nb_samples;
  189. audio_data.timestamp = pts;
  190. audio_data.format =
  191. convert_ffmpeg_sample_format(frame->frame->format);
  192. audio_data.speakers = channels;
  193. obs_source_output_audio(s->source, &audio_data);
  194. return true;
  195. }
  196. static bool is_local_file_modified(obs_properties_t *props,
  197. obs_property_t *prop, obs_data_t *settings)
  198. {
  199. UNUSED_PARAMETER(prop);
  200. bool enabled = obs_data_get_bool(settings, "is_local_file");
  201. obs_property_t *input = obs_properties_get(props, "input");
  202. obs_property_t *input_format =obs_properties_get(props,
  203. "input_format");
  204. obs_property_t *local_file = obs_properties_get(props, "local_file");
  205. obs_property_t *looping = obs_properties_get(props, "looping");
  206. obs_property_set_visible(input, !enabled);
  207. obs_property_set_visible(input_format, !enabled);
  208. obs_property_set_visible(local_file, enabled);
  209. obs_property_set_visible(looping, enabled);
  210. return true;
  211. }
  212. static bool is_advanced_modified(obs_properties_t *props,
  213. obs_property_t *prop, obs_data_t *settings)
  214. {
  215. UNUSED_PARAMETER(prop);
  216. bool enabled = obs_data_get_bool(settings, "advanced");
  217. obs_property_t *abuf = obs_properties_get(props, "audio_buffer_size");
  218. obs_property_t *vbuf = obs_properties_get(props, "video_buffer_size");
  219. obs_property_t *frame_drop = obs_properties_get(props, "frame_drop");
  220. obs_property_set_visible(abuf, enabled);
  221. obs_property_set_visible(vbuf, enabled);
  222. obs_property_set_visible(frame_drop, enabled);
  223. return true;
  224. }
  225. static obs_properties_t *ffmpeg_source_getproperties(void *data)
  226. {
  227. UNUSED_PARAMETER(data);
  228. obs_properties_t *props = obs_properties_create();
  229. obs_property_t *prop;
  230. // use this when obs allows non-readonly paths
  231. prop = obs_properties_add_bool(props, "is_local_file",
  232. obs_module_text("LocalFile"));
  233. obs_property_set_modified_callback(prop, is_local_file_modified);
  234. obs_properties_add_path(props, "local_file",
  235. obs_module_text("LocalFile"), OBS_PATH_FILE, "*.*",
  236. NULL);
  237. obs_properties_add_bool(props, "looping", obs_module_text("Looping"));
  238. obs_properties_add_text(props, "input",
  239. obs_module_text("Input"), OBS_TEXT_DEFAULT);
  240. obs_properties_add_text(props, "input_format",
  241. obs_module_text("InputFormat"), OBS_TEXT_DEFAULT);
  242. obs_properties_add_bool(props, "force_scale",
  243. obs_module_text("ForceFormat"));
  244. obs_properties_add_bool(props, "hw_decode",
  245. obs_module_text("HardwareDecode"));
  246. prop = obs_properties_add_bool(props, "advanced",
  247. obs_module_text("Advanced"));
  248. obs_property_set_modified_callback(prop, is_advanced_modified);
  249. prop = obs_properties_add_int(props, "audio_buffer_size",
  250. obs_module_text("AudioBufferSize"), 1, 9999, 1);
  251. obs_property_set_visible(prop, false);
  252. prop = obs_properties_add_int(props, "video_buffer_size",
  253. obs_module_text("VideoBufferSize"), 1, 9999, 1);
  254. obs_property_set_visible(prop, false);
  255. prop = obs_properties_add_list(props, "frame_drop",
  256. obs_module_text("FrameDropping"), OBS_COMBO_TYPE_LIST,
  257. OBS_COMBO_FORMAT_INT);
  258. obs_property_list_add_int(prop, obs_module_text("DiscardNone"),
  259. AVDISCARD_NONE);
  260. obs_property_list_add_int(prop, obs_module_text("DiscardDefault"),
  261. AVDISCARD_DEFAULT);
  262. obs_property_list_add_int(prop, obs_module_text("DiscardNonRef"),
  263. AVDISCARD_NONREF);
  264. obs_property_list_add_int(prop, obs_module_text("DiscardBiDir"),
  265. AVDISCARD_BIDIR);
  266. obs_property_list_add_int(prop, obs_module_text("DiscardNonIntra"),
  267. AVDISCARD_NONINTRA);
  268. obs_property_list_add_int(prop, obs_module_text("DiscardNonKey"),
  269. AVDISCARD_NONKEY);
  270. obs_property_list_add_int(prop, obs_module_text("DiscardAll"),
  271. AVDISCARD_ALL);
  272. obs_property_set_visible(prop, false);
  273. return props;
  274. }
  275. static void ffmpeg_source_update(void *data, obs_data_t *settings)
  276. {
  277. struct ffmpeg_source *s = data;
  278. bool is_local_file = obs_data_get_bool(settings, "is_local_file");
  279. bool is_advanced = obs_data_get_bool(settings, "advanced");
  280. bool is_looping;
  281. char *input;
  282. char *input_format;
  283. if (is_local_file) {
  284. input = (char *)obs_data_get_string(settings, "local_file");
  285. input_format = NULL;
  286. is_looping = obs_data_get_bool(settings, "looping");
  287. } else {
  288. input = (char *)obs_data_get_string(settings, "input");
  289. input_format = (char *)obs_data_get_string(settings,
  290. "input_format");
  291. is_looping = false;
  292. }
  293. s->is_forcing_scale = obs_data_get_bool(settings, "force_scale");
  294. s->is_hw_decoding = obs_data_get_bool(settings, "hw_decode");
  295. if (s->demuxer != NULL)
  296. ff_demuxer_free(s->demuxer);
  297. s->demuxer = ff_demuxer_init();
  298. s->demuxer->options.is_hw_decoding = s->is_hw_decoding;
  299. s->demuxer->options.is_looping = is_looping;
  300. if (is_advanced) {
  301. int audio_buffer_size = (int)obs_data_get_int(settings,
  302. "audio_buffer_size");
  303. int video_buffer_size = (int)obs_data_get_int(settings,
  304. "video_buffer_size");
  305. enum AVDiscard frame_drop =
  306. (enum AVDiscard)obs_data_get_int(settings,
  307. "frame_drop");
  308. if (audio_buffer_size < 1) {
  309. audio_buffer_size = 1;
  310. blog(LOG_WARNING, "invalid audio_buffer_size %d",
  311. audio_buffer_size);
  312. }
  313. if (video_buffer_size < 1) {
  314. video_buffer_size = 1;
  315. blog(LOG_WARNING, "invalid audio_buffer_size %d",
  316. audio_buffer_size);
  317. }
  318. s->demuxer->options.audio_frame_queue_size = audio_buffer_size;
  319. s->demuxer->options.video_frame_queue_size = video_buffer_size;
  320. if (frame_drop < AVDISCARD_NONE || frame_drop > AVDISCARD_ALL) {
  321. frame_drop = AVDISCARD_NONE;
  322. blog(LOG_WARNING, "invalid frame_drop %d", frame_drop);
  323. }
  324. s->demuxer->options.frame_drop = frame_drop;
  325. }
  326. ff_demuxer_set_callbacks(&s->demuxer->video_callbacks,
  327. video_frame, NULL,
  328. NULL, NULL, NULL, s);
  329. ff_demuxer_set_callbacks(&s->demuxer->audio_callbacks,
  330. audio_frame, NULL,
  331. NULL, NULL, NULL, s);
  332. ff_demuxer_open(s->demuxer, input, input_format);
  333. }
  334. static const char *ffmpeg_source_getname(void)
  335. {
  336. return obs_module_text("FFMpegSource");
  337. }
  338. static void *ffmpeg_source_create(obs_data_t *settings, obs_source_t *source)
  339. {
  340. UNUSED_PARAMETER(settings);
  341. struct ffmpeg_source *s = bzalloc(sizeof(struct ffmpeg_source));
  342. s->source = source;
  343. ffmpeg_source_update(s, settings);
  344. return s;
  345. }
  346. static void ffmpeg_source_destroy(void *data)
  347. {
  348. struct ffmpeg_source *s = data;
  349. ff_demuxer_free(s->demuxer);
  350. if (s->sws_ctx != NULL)
  351. sws_freeContext(s->sws_ctx);
  352. if (s->sws_data != NULL)
  353. bfree(s->sws_data);
  354. bfree(s);
  355. }
  356. struct obs_source_info ffmpeg_source = {
  357. .id = "ffmpeg_source",
  358. .type = OBS_SOURCE_TYPE_INPUT,
  359. .output_flags = OBS_SOURCE_ASYNC_VIDEO | OBS_SOURCE_AUDIO,
  360. .get_name = ffmpeg_source_getname,
  361. .create = ffmpeg_source_create,
  362. .destroy = ffmpeg_source_destroy,
  363. .get_properties = ffmpeg_source_getproperties,
  364. .update = ffmpeg_source_update
  365. };