1
0

image-source.c 9.6 KB

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