image-source.c 10 KB

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