1
0

obs-ffmpeg-source.c 11 KB

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