image-source.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. #include <obs-module.h>
  2. #include <graphics/image-file.h>
  3. #include <util/threading.h>
  4. #include <util/platform.h>
  5. #include <util/dstr.h>
  6. #include <sys/stat.h>
  7. #define blog(log_level, format, ...) \
  8. blog(log_level, "[image_source: '%s'] " format, \
  9. obs_source_get_name(context->source), ##__VA_ARGS__)
  10. #define debug(format, ...) blog(LOG_DEBUG, format, ##__VA_ARGS__)
  11. #define info(format, ...) blog(LOG_INFO, format, ##__VA_ARGS__)
  12. #define warn(format, ...) blog(LOG_WARNING, format, ##__VA_ARGS__)
  13. struct image_source {
  14. obs_source_t *source;
  15. char *file;
  16. bool persistent;
  17. bool is_slide;
  18. bool linear_alpha;
  19. time_t file_timestamp;
  20. float update_time_elapsed;
  21. uint64_t last_time;
  22. bool active;
  23. bool restart_gif;
  24. volatile bool file_decoded;
  25. volatile bool texture_loaded;
  26. gs_image_file4_t if4;
  27. };
  28. static time_t get_modified_timestamp(const char *filename)
  29. {
  30. struct stat stats;
  31. if (os_stat(filename, &stats) != 0)
  32. return -1;
  33. return stats.st_mtime;
  34. }
  35. static const char *image_source_get_name(void *unused)
  36. {
  37. UNUSED_PARAMETER(unused);
  38. return obs_module_text("ImageInput");
  39. }
  40. void image_source_preload_image(void *data)
  41. {
  42. struct image_source *context = data;
  43. if (os_atomic_load_bool(&context->file_decoded))
  44. return;
  45. context->file_timestamp = get_modified_timestamp(context->file);
  46. gs_image_file4_init(&context->if4, context->file,
  47. context->linear_alpha
  48. ? GS_IMAGE_ALPHA_PREMULTIPLY_SRGB
  49. : GS_IMAGE_ALPHA_PREMULTIPLY);
  50. os_atomic_set_bool(&context->file_decoded, true);
  51. }
  52. static void image_source_load_texture(void *data)
  53. {
  54. struct image_source *context = data;
  55. if (os_atomic_load_bool(&context->texture_loaded))
  56. return;
  57. debug("loading texture '%s'", context->file);
  58. obs_enter_graphics();
  59. gs_image_file4_init_texture(&context->if4);
  60. obs_leave_graphics();
  61. if (!context->if4.image3.image2.image.loaded)
  62. warn("failed to load texture '%s'", context->file);
  63. context->update_time_elapsed = 0;
  64. os_atomic_set_bool(&context->texture_loaded, true);
  65. }
  66. static void image_source_unload(void *data)
  67. {
  68. struct image_source *context = data;
  69. os_atomic_set_bool(&context->file_decoded, false);
  70. os_atomic_set_bool(&context->texture_loaded, false);
  71. obs_enter_graphics();
  72. gs_image_file4_free(&context->if4);
  73. obs_leave_graphics();
  74. }
  75. static void image_source_load(struct image_source *context)
  76. {
  77. image_source_unload(context);
  78. if (context->file && *context->file) {
  79. image_source_preload_image(context);
  80. image_source_load_texture(context);
  81. }
  82. }
  83. static void image_source_update(void *data, obs_data_t *settings)
  84. {
  85. struct image_source *context = data;
  86. const char *file = obs_data_get_string(settings, "file");
  87. const bool unload = obs_data_get_bool(settings, "unload");
  88. const bool linear_alpha = obs_data_get_bool(settings, "linear_alpha");
  89. const bool is_slide = obs_data_get_bool(settings, "is_slide");
  90. if (context->file)
  91. bfree(context->file);
  92. context->file = bstrdup(file);
  93. context->persistent = !unload;
  94. context->linear_alpha = linear_alpha;
  95. context->is_slide = is_slide;
  96. if (is_slide)
  97. return;
  98. /* Load the image if the source is persistent or showing */
  99. if (context->persistent || obs_source_showing(context->source))
  100. image_source_load(data);
  101. else
  102. image_source_unload(data);
  103. }
  104. static void image_source_defaults(obs_data_t *settings)
  105. {
  106. obs_data_set_default_bool(settings, "unload", false);
  107. obs_data_set_default_bool(settings, "linear_alpha", false);
  108. }
  109. static void image_source_show(void *data)
  110. {
  111. struct image_source *context = data;
  112. if (!context->persistent && !context->is_slide)
  113. image_source_load(context);
  114. }
  115. static void image_source_hide(void *data)
  116. {
  117. struct image_source *context = data;
  118. if (!context->persistent && !context->is_slide)
  119. image_source_unload(context);
  120. }
  121. static void restart_gif(void *data)
  122. {
  123. struct image_source *context = data;
  124. if (context->if4.image3.image2.image.is_animated_gif) {
  125. context->if4.image3.image2.image.cur_frame = 0;
  126. context->if4.image3.image2.image.cur_loop = 0;
  127. context->if4.image3.image2.image.cur_time = 0;
  128. obs_enter_graphics();
  129. gs_image_file4_update_texture(&context->if4);
  130. obs_leave_graphics();
  131. context->restart_gif = false;
  132. }
  133. }
  134. static void image_source_activate(void *data)
  135. {
  136. struct image_source *context = data;
  137. context->restart_gif = true;
  138. }
  139. static void *image_source_create(obs_data_t *settings, obs_source_t *source)
  140. {
  141. struct image_source *context = bzalloc(sizeof(struct image_source));
  142. context->source = source;
  143. image_source_update(context, settings);
  144. return context;
  145. }
  146. static void image_source_destroy(void *data)
  147. {
  148. struct image_source *context = data;
  149. image_source_unload(context);
  150. if (context->file)
  151. bfree(context->file);
  152. bfree(context);
  153. }
  154. static uint32_t image_source_getwidth(void *data)
  155. {
  156. struct image_source *context = data;
  157. return context->if4.image3.image2.image.cx;
  158. }
  159. static uint32_t image_source_getheight(void *data)
  160. {
  161. struct image_source *context = data;
  162. return context->if4.image3.image2.image.cy;
  163. }
  164. static void image_source_render(void *data, gs_effect_t *effect)
  165. {
  166. struct image_source *context = data;
  167. if (!os_atomic_load_bool(&context->texture_loaded))
  168. return;
  169. struct gs_image_file *const image = &context->if4.image3.image2.image;
  170. gs_texture_t *const texture = image->texture;
  171. if (!texture)
  172. return;
  173. const bool previous = gs_framebuffer_srgb_enabled();
  174. gs_enable_framebuffer_srgb(true);
  175. gs_blend_state_push();
  176. gs_blend_function(GS_BLEND_ONE, GS_BLEND_INVSRCALPHA);
  177. gs_eparam_t *const param = gs_effect_get_param_by_name(effect, "image");
  178. gs_effect_set_texture_srgb(param, texture);
  179. gs_draw_sprite(texture, 0, image->cx, image->cy);
  180. gs_blend_state_pop();
  181. gs_enable_framebuffer_srgb(previous);
  182. }
  183. static void image_source_tick(void *data, float seconds)
  184. {
  185. struct image_source *context = data;
  186. if (!os_atomic_load_bool(&context->texture_loaded)) {
  187. if (os_atomic_load_bool(&context->file_decoded))
  188. image_source_load_texture(context);
  189. else
  190. return;
  191. }
  192. uint64_t frame_time = obs_get_video_frame_time();
  193. context->update_time_elapsed += seconds;
  194. if (obs_source_showing(context->source)) {
  195. if (context->update_time_elapsed >= 1.0f) {
  196. time_t t = get_modified_timestamp(context->file);
  197. context->update_time_elapsed = 0.0f;
  198. if (context->file_timestamp != t) {
  199. image_source_load(context);
  200. }
  201. }
  202. }
  203. if (obs_source_showing(context->source)) {
  204. if (!context->active) {
  205. if (context->if4.image3.image2.image.is_animated_gif)
  206. context->last_time = frame_time;
  207. context->active = true;
  208. }
  209. if (context->restart_gif)
  210. restart_gif(context);
  211. } else {
  212. if (context->active) {
  213. restart_gif(context);
  214. context->active = false;
  215. }
  216. return;
  217. }
  218. if (context->last_time &&
  219. context->if4.image3.image2.image.is_animated_gif) {
  220. uint64_t elapsed = frame_time - context->last_time;
  221. bool updated = gs_image_file4_tick(&context->if4, elapsed);
  222. if (updated) {
  223. obs_enter_graphics();
  224. gs_image_file4_update_texture(&context->if4);
  225. obs_leave_graphics();
  226. }
  227. }
  228. context->last_time = frame_time;
  229. }
  230. static const char *image_filter =
  231. #ifdef _WIN32
  232. "All formats (*.bmp *.tga *.png *.jpeg *.jpg *.jxr *.gif *.psd *.webp);;"
  233. #else
  234. "All formats (*.bmp *.tga *.png *.jpeg *.jpg *.gif *.psd *.webp);;"
  235. #endif
  236. "BMP Files (*.bmp);;"
  237. "Targa Files (*.tga);;"
  238. "PNG Files (*.png);;"
  239. "JPEG Files (*.jpeg *.jpg);;"
  240. #ifdef _WIN32
  241. "JXR Files (*.jxr);;"
  242. #endif
  243. "GIF Files (*.gif);;"
  244. "PSD Files (*.psd);;"
  245. "WebP Files (*.webp);;"
  246. "All Files (*.*)";
  247. static obs_properties_t *image_source_properties(void *data)
  248. {
  249. struct image_source *s = data;
  250. struct dstr path = {0};
  251. obs_properties_t *props = obs_properties_create();
  252. if (s && s->file && *s->file) {
  253. const char *slash;
  254. dstr_copy(&path, s->file);
  255. dstr_replace(&path, "\\", "/");
  256. slash = strrchr(path.array, '/');
  257. if (slash)
  258. dstr_resize(&path, slash - path.array + 1);
  259. }
  260. obs_properties_add_path(props, "file", obs_module_text("File"),
  261. OBS_PATH_FILE, image_filter, path.array);
  262. obs_properties_add_bool(props, "unload",
  263. obs_module_text("UnloadWhenNotShowing"));
  264. obs_properties_add_bool(props, "linear_alpha",
  265. obs_module_text("LinearAlpha"));
  266. dstr_free(&path);
  267. return props;
  268. }
  269. uint64_t image_source_get_memory_usage(void *data)
  270. {
  271. struct image_source *s = data;
  272. return s->if4.image3.image2.mem_usage;
  273. }
  274. static void missing_file_callback(void *src, const char *new_path, void *data)
  275. {
  276. struct image_source *s = src;
  277. obs_source_t *source = s->source;
  278. obs_data_t *settings = obs_source_get_settings(source);
  279. obs_data_set_string(settings, "file", new_path);
  280. obs_source_update(source, settings);
  281. obs_data_release(settings);
  282. UNUSED_PARAMETER(data);
  283. }
  284. static obs_missing_files_t *image_source_missingfiles(void *data)
  285. {
  286. struct image_source *s = data;
  287. obs_missing_files_t *files = obs_missing_files_create();
  288. if (strcmp(s->file, "") != 0) {
  289. if (!os_file_exists(s->file)) {
  290. obs_missing_file_t *file = obs_missing_file_create(
  291. s->file, missing_file_callback,
  292. OBS_MISSING_FILE_SOURCE, s->source, NULL);
  293. obs_missing_files_add_file(files, file);
  294. }
  295. }
  296. return files;
  297. }
  298. static enum gs_color_space
  299. image_source_get_color_space(void *data, size_t count,
  300. const enum gs_color_space *preferred_spaces)
  301. {
  302. UNUSED_PARAMETER(count);
  303. UNUSED_PARAMETER(preferred_spaces);
  304. struct image_source *const s = data;
  305. gs_image_file4_t *const if4 = &s->if4;
  306. return if4->image3.image2.image.texture ? if4->space : GS_CS_SRGB;
  307. }
  308. static struct obs_source_info image_source_info = {
  309. .id = "image_source",
  310. .type = OBS_SOURCE_TYPE_INPUT,
  311. .output_flags = OBS_SOURCE_VIDEO | OBS_SOURCE_SRGB,
  312. .get_name = image_source_get_name,
  313. .create = image_source_create,
  314. .destroy = image_source_destroy,
  315. .update = image_source_update,
  316. .get_defaults = image_source_defaults,
  317. .show = image_source_show,
  318. .hide = image_source_hide,
  319. .get_width = image_source_getwidth,
  320. .get_height = image_source_getheight,
  321. .video_render = image_source_render,
  322. .video_tick = image_source_tick,
  323. .missing_files = image_source_missingfiles,
  324. .get_properties = image_source_properties,
  325. .icon_type = OBS_ICON_TYPE_IMAGE,
  326. .activate = image_source_activate,
  327. .video_get_color_space = image_source_get_color_space,
  328. };
  329. OBS_DECLARE_MODULE()
  330. OBS_MODULE_USE_DEFAULT_LOCALE("image-source", "en-US")
  331. MODULE_EXPORT const char *obs_module_description(void)
  332. {
  333. return "Image/color/slideshow sources";
  334. }
  335. extern struct obs_source_info slideshow_info;
  336. extern struct obs_source_info slideshow_info_mk2;
  337. extern struct obs_source_info color_source_info_v1;
  338. extern struct obs_source_info color_source_info_v2;
  339. extern struct obs_source_info color_source_info_v3;
  340. bool obs_module_load(void)
  341. {
  342. obs_register_source(&image_source_info);
  343. obs_register_source(&color_source_info_v1);
  344. obs_register_source(&color_source_info_v2);
  345. obs_register_source(&color_source_info_v3);
  346. obs_register_source(&slideshow_info);
  347. obs_register_source(&slideshow_info_mk2);
  348. return true;
  349. }