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