-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
dynamic-delay.c
730 lines (663 loc) · 20.3 KB
/
dynamic-delay.c
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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
#include <obs-module.h>
#include <util/circlebuf.h>
#include <util/dstr.h>
#include "dynamic-delay.h"
#include "easing.h"
#include "version.h"
struct frame {
gs_texrender_t *render;
uint64_t ts;
};
struct dynamic_delay_info {
obs_source_t *source;
struct circlebuf frames;
obs_hotkey_id skip_begin_hotkey;
obs_hotkey_id skip_end_hotkey;
obs_hotkey_id forward_hotkey;
obs_hotkey_id forward_slow_hotkey;
obs_hotkey_id forward_fast_hotkey;
obs_hotkey_id backward_hotkey;
obs_hotkey_id backward_slow_hotkey;
obs_hotkey_id backward_fast_hotkey;
obs_hotkey_id pause_hotkey;
bool hotkeys_loaded;
double max_duration;
double speed;
double start_speed;
double target_speed;
double slow_forward_speed;
double fast_forward_speed;
double slow_backward_speed;
double fast_backward_speed;
uint32_t cx;
uint32_t cy;
bool processed_frame;
double time_diff;
bool target_valid;
uint32_t easing;
float easing_duration;
float easing_max_duration;
uint64_t easing_started;
char *text_source_name;
char *text_format;
bool enabled;
};
static void replace_text(struct dstr *str, size_t pos, size_t len,
const char *new_text)
{
struct dstr front = {0};
struct dstr back = {0};
dstr_left(&front, str, pos);
dstr_right(&back, str, pos + len);
dstr_copy_dstr(str, &front);
dstr_cat(str, new_text);
dstr_cat_dstr(str, &back);
dstr_free(&front);
dstr_free(&back);
}
static void dynamic_delay_text(struct dynamic_delay_info *c)
{
if (!c->text_source_name || !strlen(c->text_source_name) ||
!c->text_format)
return;
obs_source_t *s = obs_get_source_by_name(c->text_source_name);
if (!s)
return;
struct dstr sf;
size_t pos = 0;
struct dstr buffer;
dstr_init(&buffer);
dstr_init_copy(&sf, c->text_format);
while (pos < sf.len) {
const char *cmp = sf.array + pos;
if (astrcmp_n(cmp, "%SPEED%", 7) == 0) {
dstr_printf(&buffer, "%.1f", c->speed * 100.0);
dstr_cat_ch(&buffer, '%');
replace_text(&sf, pos, 7, buffer.array);
pos += buffer.len;
} else if (astrcmp_n(cmp, "%TARGET%", 8) == 0) {
dstr_printf(&buffer, "%.1f", c->target_speed * 100.0);
dstr_cat_ch(&buffer, '%');
replace_text(&sf, pos, 8, buffer.array);
pos += buffer.len;
} else if (astrcmp_n(cmp, "%TIME%", 6) == 0) {
dstr_printf(&buffer, "%.1f", c->time_diff);
replace_text(&sf, pos, 6, buffer.array);
pos += buffer.len;
} else {
pos++;
}
}
obs_data_t *settings = obs_data_create();
obs_data_set_string(settings, "text", sf.array);
obs_source_update(s, settings);
obs_data_release(settings);
dstr_free(&sf);
dstr_free(&buffer);
obs_source_release(s);
}
static const char *dynamic_delay_get_name(void *type_data)
{
UNUSED_PARAMETER(type_data);
return obs_module_text("DynamicDelay");
}
static void free_textures(struct dynamic_delay_info *f)
{
while (f->frames.size) {
struct frame frame;
circlebuf_pop_front(&f->frames, &frame, sizeof(frame));
obs_enter_graphics();
gs_texrender_destroy(frame.render);
obs_leave_graphics();
}
circlebuf_free(&f->frames);
}
static void dynamic_delay_update(void *data, obs_data_t *settings)
{
struct dynamic_delay_info *d = data;
double duration = obs_data_get_double(settings, S_DURATION);
if (duration < d->max_duration) {
free_textures(d);
}
d->max_duration = duration;
d->easing = (uint32_t)obs_data_get_int(settings, S_EASING);
d->easing_max_duration =
(float)obs_data_get_double(settings, S_EASING_DURATION);
d->slow_forward_speed =
obs_data_get_double(settings, S_SLOW_FORWARD) / 100.0;
d->fast_forward_speed =
obs_data_get_double(settings, S_FAST_FORWARD) / 100.0;
d->slow_backward_speed =
obs_data_get_double(settings, S_SLOW_BACKWARD) / 100.0;
d->fast_backward_speed =
obs_data_get_double(settings, S_FAST_BACKWARD) / 100.0;
const char *text_source = obs_data_get_string(settings, S_TEXT_SOURCE);
if (d->text_source_name) {
if (strcmp(d->text_source_name, text_source) != 0) {
bfree(d->text_source_name);
d->text_source_name = bstrdup(text_source);
}
} else {
d->text_source_name = bstrdup(text_source);
}
const char *text_format = obs_data_get_string(settings, S_TEXT_FORMAT);
if (d->text_format) {
if (strcmp(d->text_format, text_format) != 0) {
bfree(d->text_format);
d->text_format = bstrdup(text_format);
}
} else {
d->text_format = bstrdup(text_format);
}
}
static void *dynamic_delay_create(obs_data_t *settings, obs_source_t *source)
{
struct dynamic_delay_info *d =
bzalloc(sizeof(struct dynamic_delay_info));
d->source = source;
d->speed = 1.0;
d->target_speed = 1.0;
d->start_speed = 1.0;
dynamic_delay_update(d, settings);
return d;
}
static void dynamic_delay_destroy(void *data)
{
struct dynamic_delay_info *c = data;
obs_hotkey_unregister(c->skip_begin_hotkey);
obs_hotkey_unregister(c->skip_end_hotkey);
obs_hotkey_unregister(c->forward_hotkey);
obs_hotkey_unregister(c->forward_slow_hotkey);
obs_hotkey_unregister(c->forward_fast_hotkey);
obs_hotkey_unregister(c->backward_hotkey);
obs_hotkey_unregister(c->backward_slow_hotkey);
obs_hotkey_unregister(c->backward_fast_hotkey);
obs_hotkey_unregister(c->pause_hotkey);
free_textures(c);
if (c->text_source_name)
bfree(c->text_source_name);
if (c->text_format)
bfree(c->text_format);
bfree(c);
}
void dynamic_delay_skip_begin_hotkey(void *data, obs_hotkey_id id,
obs_hotkey_t *hotkey, bool pressed)
{
UNUSED_PARAMETER(id);
UNUSED_PARAMETER(hotkey);
if (!pressed)
return;
struct dynamic_delay_info *d = data;
if (d->start_speed < 1.0 || d->speed < 1.0) {
d->start_speed = 1.0;
d->target_speed = 1.0;
d->easing_started = 0;
}
d->time_diff = d->max_duration;
}
void dynamic_delay_skip_end_hotkey(void *data, obs_hotkey_id id,
obs_hotkey_t *hotkey, bool pressed)
{
UNUSED_PARAMETER(id);
UNUSED_PARAMETER(hotkey);
if (!pressed)
return;
struct dynamic_delay_info *d = data;
if (d->start_speed > 1.0 || d->speed > 1.0) {
d->start_speed = 1.0;
d->target_speed = 1.0;
d->easing_started = 0;
}
d->time_diff = 0;
}
void dynamic_delay_slow_forward_hotkey(void *data, obs_hotkey_id id,
obs_hotkey_t *hotkey, bool pressed)
{
UNUSED_PARAMETER(id);
UNUSED_PARAMETER(hotkey);
if (!pressed)
return;
struct dynamic_delay_info *d = data;
d->start_speed = d->speed;
d->target_speed = d->slow_forward_speed;
d->easing_started = 0;
}
void dynamic_delay_forward_hotkey(void *data, obs_hotkey_id id,
obs_hotkey_t *hotkey, bool pressed)
{
UNUSED_PARAMETER(id);
UNUSED_PARAMETER(hotkey);
if (!pressed)
return;
struct dynamic_delay_info *d = data;
d->start_speed = d->speed;
d->target_speed = 1.0;
d->easing_started = 0;
}
void dynamic_delay_fast_forward_hotkey(void *data, obs_hotkey_id id,
obs_hotkey_t *hotkey, bool pressed)
{
UNUSED_PARAMETER(id);
UNUSED_PARAMETER(hotkey);
if (!pressed)
return;
struct dynamic_delay_info *d = data;
d->start_speed = d->speed;
d->target_speed = d->fast_forward_speed;
d->easing_started = 0;
}
void dynamic_delay_slow_backward_hotkey(void *data, obs_hotkey_id id,
obs_hotkey_t *hotkey, bool pressed)
{
UNUSED_PARAMETER(id);
UNUSED_PARAMETER(hotkey);
if (!pressed)
return;
struct dynamic_delay_info *d = data;
d->start_speed = d->speed;
d->target_speed = d->slow_backward_speed;
d->easing_started = 0;
}
void dynamic_delay_backward_hotkey(void *data, obs_hotkey_id id,
obs_hotkey_t *hotkey, bool pressed)
{
UNUSED_PARAMETER(id);
UNUSED_PARAMETER(hotkey);
if (!pressed)
return;
struct dynamic_delay_info *d = data;
d->start_speed = d->speed;
d->target_speed = -1.0;
d->easing_started = 0;
}
void dynamic_delay_fast_backward_hotkey(void *data, obs_hotkey_id id,
obs_hotkey_t *hotkey, bool pressed)
{
UNUSED_PARAMETER(id);
UNUSED_PARAMETER(hotkey);
if (!pressed)
return;
struct dynamic_delay_info *d = data;
d->start_speed = d->speed;
d->target_speed = d->fast_backward_speed;
d->easing_started = 0;
}
void dynamic_delay_pause_hotkey(void *data, obs_hotkey_id id,
obs_hotkey_t *hotkey, bool pressed)
{
UNUSED_PARAMETER(id);
UNUSED_PARAMETER(hotkey);
if (!pressed)
return;
struct dynamic_delay_info *d = data;
d->start_speed = d->speed;
d->target_speed = 0.0;
d->easing_started = 0;
}
static void dynamic_delay_load_hotkeys(void *data)
{
struct dynamic_delay_info *d = data;
obs_source_t *parent = obs_filter_get_parent(d->source);
if (parent) {
d->skip_begin_hotkey = obs_hotkey_register_source(
parent, "skip_begin", obs_module_text("SkipBegin"),
dynamic_delay_skip_begin_hotkey, data);
d->skip_end_hotkey = obs_hotkey_register_source(
parent, "skip_end", obs_module_text("SkipEnd"),
dynamic_delay_skip_end_hotkey, data);
d->forward_hotkey = obs_hotkey_register_source(
parent, "forward", obs_module_text("Forward"),
dynamic_delay_forward_hotkey, data);
d->forward_slow_hotkey = obs_hotkey_register_source(
parent, "slow_forward", obs_module_text("SlowForward"),
dynamic_delay_slow_forward_hotkey, data);
d->forward_fast_hotkey = obs_hotkey_register_source(
parent, "fast_forward", obs_module_text("FastForward"),
dynamic_delay_fast_forward_hotkey, data);
d->backward_hotkey = obs_hotkey_register_source(
parent, "backward", obs_module_text("Backward"),
dynamic_delay_backward_hotkey, data);
d->backward_slow_hotkey = obs_hotkey_register_source(
parent, "slow_backward",
obs_module_text("SlowBackward"),
dynamic_delay_slow_backward_hotkey, data);
d->backward_fast_hotkey = obs_hotkey_register_source(
parent, "fast_backward",
obs_module_text("FastBackward"),
dynamic_delay_fast_backward_hotkey, data);
d->pause_hotkey = obs_hotkey_register_source(
parent, "pause", obs_module_text("Pause"),
dynamic_delay_pause_hotkey, data);
d->hotkeys_loaded = true;
}
}
static void dynamic_delay_load(void *data, obs_data_t *settings)
{
dynamic_delay_load_hotkeys(data);
dynamic_delay_update(data, settings);
}
static void draw_frame(struct dynamic_delay_info *d)
{
struct frame *frame = NULL;
if (!d->frames.size)
return;
const size_t count = d->frames.size / sizeof(struct frame);
if (d->time_diff <= 0.0) {
frame = circlebuf_data(&d->frames,
(count - 1) * sizeof(struct frame));
} else {
size_t i = 0;
const uint64_t ts = obs_get_video_frame_time();
while (i < count) {
frame = circlebuf_data(&d->frames,
i * sizeof(struct frame));
if (ts - frame->ts <
(uint64_t)(d->time_diff * 1000000000.0))
break;
i++;
}
}
if (!frame)
return;
gs_effect_t *effect = obs_get_base_effect(OBS_EFFECT_DEFAULT);
gs_texture_t *tex = gs_texrender_get_texture(frame->render);
if (tex) {
gs_eparam_t *image =
gs_effect_get_param_by_name(effect, "image");
gs_effect_set_texture(image, tex);
while (gs_effect_loop(effect, "Draw"))
gs_draw_sprite(tex, 0, d->cx, d->cy);
}
}
static void dynamic_delay_video_render(void *data, gs_effect_t *effect)
{
UNUSED_PARAMETER(effect);
struct dynamic_delay_info *d = data;
obs_source_t *target = obs_filter_get_target(d->source);
obs_source_t *parent = obs_filter_get_parent(d->source);
if (!d->target_valid || !target || !parent) {
obs_source_skip_video_filter(d->source);
return;
}
if (d->processed_frame) {
draw_frame(d);
return;
}
const uint64_t ts = obs_get_video_frame_time();
struct frame frame;
frame.render = NULL;
if (d->frames.size) {
circlebuf_peek_front(&d->frames, &frame, sizeof(frame));
if (ts > frame.ts &&
ts - frame.ts <
(uint64_t)(d->max_duration * 1000000000.0)) {
frame.render = NULL;
} else {
circlebuf_pop_front(&d->frames, NULL, sizeof(frame));
if (d->frames.size) {
struct frame next_frame;
circlebuf_peek_front(&d->frames, &next_frame,
sizeof(next_frame));
if ((ts > next_frame.ts ? ts - next_frame.ts
: next_frame.ts - ts) >=
(uint64_t)(d->max_duration *
1000000000.0)) {
gs_texrender_destroy(frame.render);
circlebuf_pop_front(&d->frames, &frame,
sizeof(frame));
}
}
}
}
if (!frame.render) {
frame.render = gs_texrender_create(GS_RGBA, GS_ZS_NONE);
} else {
gs_texrender_reset(frame.render);
}
frame.ts = ts;
gs_blend_state_push();
gs_blend_function(GS_BLEND_ONE, GS_BLEND_ZERO);
if (gs_texrender_begin(frame.render, d->cx, d->cy)) {
uint32_t parent_flags = obs_source_get_output_flags(target);
bool custom_draw = (parent_flags & OBS_SOURCE_CUSTOM_DRAW) != 0;
bool async = (parent_flags & OBS_SOURCE_ASYNC) != 0;
struct vec4 clear_color;
vec4_zero(&clear_color);
gs_clear(GS_CLEAR_COLOR, &clear_color, 0.0f, 0);
gs_ortho(0.0f, (float)d->cx, 0.0f, (float)d->cy, -100.0f,
100.0f);
if (target == parent && !custom_draw && !async)
obs_source_default_render(target);
else
obs_source_video_render(target);
gs_texrender_end(frame.render);
}
gs_blend_state_pop();
circlebuf_push_back(&d->frames, &frame, sizeof(frame));
draw_frame(d);
d->processed_frame = true;
}
static bool dynamic_delay_text_source_modified(obs_properties_t *props,
obs_property_t *property,
obs_data_t *data)
{
UNUSED_PARAMETER(property);
const char *source_name = obs_data_get_string(data, S_TEXT_SOURCE);
bool text_source = false;
if (source_name && strlen(source_name)) {
text_source = true;
}
obs_property_t *prop = obs_properties_get(props, S_TEXT_FORMAT);
obs_property_set_visible(prop, text_source);
return true;
}
static obs_properties_t *dynamic_delay_properties(void *data)
{
UNUSED_PARAMETER(data);
obs_properties_t *ppts = obs_properties_create();
obs_property_t *p = obs_properties_add_float(
ppts, S_DURATION, obs_module_text("Duration"), 0.0, 10000.0,
1.0);
obs_property_float_set_suffix(p, "s");
p = obs_properties_add_list(ppts, S_EASING, obs_module_text("Easing"),
OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_INT);
obs_property_list_add_int(p, obs_module_text("EasingFunction.Linear"),
EASING_LINEAR);
obs_property_list_add_int(p,
obs_module_text("EasingFunction.Quadratic"),
EASING_QUADRATIC);
obs_property_list_add_int(p, obs_module_text("EasingFunction.Cubic"),
EASING_CUBIC);
obs_property_list_add_int(p, obs_module_text("EasingFunction.Quartic"),
EASING_QUARTIC);
obs_property_list_add_int(p, obs_module_text("EasingFunction.Quintic"),
EASING_QUINTIC);
obs_property_list_add_int(p, obs_module_text("EasingFunction.Sine"),
EASING_SINE);
obs_property_list_add_int(p, obs_module_text("EasingFunction.Circular"),
EASING_CIRCULAR);
obs_property_list_add_int(p,
obs_module_text("EasingFunction.Exponential"),
EASING_EXPONENTIAL);
obs_property_list_add_int(p, obs_module_text("EasingFunction.Elastic"),
EASING_ELASTIC);
obs_property_list_add_int(p, obs_module_text("EasingFunction.Bounce"),
EASING_BOUNCE);
obs_property_list_add_int(p, obs_module_text("EasingFunction.Back"),
EASING_BACK);
p = obs_properties_add_float(ppts, S_EASING_DURATION,
obs_module_text("EasingDuration"), 0.0,
10.0, 1.0);
obs_property_float_set_suffix(p, "s");
p = obs_properties_add_float_slider(ppts, S_SLOW_FORWARD,
obs_module_text("SlowForward"), 0.1,
99.9, 1.0);
obs_property_float_set_suffix(p, "%");
p = obs_properties_add_float_slider(ppts, S_FAST_FORWARD,
obs_module_text("FastForward"),
100.1, 1000.0, 1.0);
obs_property_float_set_suffix(p, "%");
p = obs_properties_add_float_slider(ppts, S_SLOW_BACKWARD,
obs_module_text("SlowBackward"),
-99.9, -0.1, 1.0);
obs_property_float_set_suffix(p, "%");
p = obs_properties_add_float_slider(ppts, S_FAST_BACKWARD,
obs_module_text("FastBackward"),
-1000.0, -100.1, 1.0);
obs_property_float_set_suffix(p, "%");
p = obs_properties_add_text(ppts, S_TEXT_SOURCE,
obs_module_text("TextSource"),
OBS_TEXT_DEFAULT);
obs_property_set_modified_callback(p,
dynamic_delay_text_source_modified);
obs_properties_add_text(ppts, S_TEXT_FORMAT,
obs_module_text("TextFormat"),
OBS_TEXT_MULTILINE);
obs_properties_add_text(
ppts, "plugin_info",
"<a href=\"https://obsproject.com/forum/resources/dynamic-delay.1035/\">Dynamic Delay</a> (" PROJECT_VERSION
") by <a href=\"https://www.exeldro.com\">Exeldro</a>",
OBS_TEXT_INFO);
return ppts;
}
void dynamic_delay_defaults(obs_data_t *settings)
{
obs_data_set_default_double(settings, S_DURATION, 10.0);
obs_data_set_default_double(settings, S_EASING_DURATION, 1.0);
obs_data_set_default_int(settings, S_EASING, EASING_CUBIC);
obs_data_set_default_double(settings, S_SLOW_FORWARD, 50.0);
obs_data_set_default_double(settings, S_FAST_FORWARD, 200.0);
obs_data_set_default_double(settings, S_SLOW_BACKWARD, -50.0);
obs_data_set_default_double(settings, S_FAST_BACKWARD, -200.0);
obs_data_set_default_string(
settings, S_TEXT_FORMAT,
"Speed: %SPEED%%\nTarget speed: %TARGET%\nTime diff: %TIME% seconds");
}
static inline void check_size(struct dynamic_delay_info *d)
{
obs_source_t *target = obs_filter_get_target(d->source);
d->target_valid = !!target;
if (!d->target_valid)
return;
const uint32_t cx = obs_source_get_base_width(target);
const uint32_t cy = obs_source_get_base_height(target);
d->target_valid = !!cx && !!cy;
if (!d->target_valid)
return;
if (cx != d->cx || cy != d->cy) {
d->cx = cx;
d->cy = cy;
free_textures(d);
}
}
static void dynamic_delay_tick(void *data, float t)
{
struct dynamic_delay_info *d = data;
bool enabled = obs_source_enabled(d->source);
if (enabled != d->enabled) {
d->enabled = enabled;
if (!enabled) {
free_textures(d);
d->time_diff = 0.0;
d->speed = 1.0;
dynamic_delay_text(d);
}
}
if (!d->hotkeys_loaded)
dynamic_delay_load_hotkeys(data);
if (!enabled)
return;
d->processed_frame = false;
if (d->speed != d->target_speed) {
const uint64_t ts = obs_get_video_frame_time();
if (!d->easing_started)
d->easing_started = ts;
const double duration =
(double)(ts - d->easing_started) / 1000000000.0;
if (duration > d->easing_max_duration ||
d->easing_max_duration <= 0.0) {
d->speed = d->target_speed;
} else {
AHFloat t2 =
(AHFloat)(duration / d->easing_max_duration);
if (d->easing == EASING_QUADRATIC) {
t2 = QuadraticEaseInOut(t2);
} else if (d->easing == EASING_CUBIC) {
t2 = CubicEaseInOut(t2);
} else if (d->easing == EASING_QUARTIC) {
t2 = QuarticEaseInOut(t2);
} else if (d->easing == EASING_QUINTIC) {
t2 = QuinticEaseInOut(t2);
} else if (d->easing == EASING_SINE) {
t2 = SineEaseInOut(t2);
} else if (d->easing == EASING_CIRCULAR) {
t2 = CircularEaseInOut(t2);
} else if (d->easing == EASING_EXPONENTIAL) {
t2 = ExponentialEaseInOut(t2);
} else if (d->easing == EASING_ELASTIC) {
t2 = ElasticEaseInOut(t2);
} else if (d->easing == EASING_BOUNCE) {
t2 = BounceEaseInOut(t2);
} else if (d->easing == EASING_BACK) {
t2 = BackEaseInOut(t2);
}
d->speed =
d->start_speed +
(d->target_speed - d->start_speed) * (double)t2;
}
} else if (d->easing_started) {
d->easing_started = 0;
}
if (d->speed > 1.0 && d->target_speed > 1.0 &&
d->time_diff < d->easing_max_duration / 2.0) {
d->start_speed = d->speed;
d->target_speed = 1.0;
d->easing_started = 0;
} else if (d->speed < 1.0 && d->target_speed < 1.0 &&
d->max_duration - d->time_diff <
d->easing_max_duration / 2.0) {
d->start_speed = d->speed;
d->target_speed = 1.0;
d->easing_started = 0;
}
double time_diff = d->time_diff;
time_diff += (1.0 - d->speed) * t;
if (time_diff < 0.0)
time_diff = 0.0;
if (time_diff > d->max_duration)
time_diff = d->max_duration;
d->time_diff = time_diff;
dynamic_delay_text(d);
check_size(d);
}
struct obs_source_info dynamic_delay_filter = {
.id = "dynamic_delay_filter",
.type = OBS_SOURCE_TYPE_FILTER,
.output_flags = OBS_OUTPUT_VIDEO,
.get_name = dynamic_delay_get_name,
.create = dynamic_delay_create,
.destroy = dynamic_delay_destroy,
.load = dynamic_delay_load,
.update = dynamic_delay_update,
.video_render = dynamic_delay_video_render,
.get_properties = dynamic_delay_properties,
.get_defaults = dynamic_delay_defaults,
.video_tick = dynamic_delay_tick,
};
OBS_DECLARE_MODULE()
OBS_MODULE_AUTHOR("Exeldro");
OBS_MODULE_USE_DEFAULT_LOCALE("dynamic-delay", "en-US")
MODULE_EXPORT const char *obs_module_description(void)
{
return obs_module_text("Description");
}
MODULE_EXPORT const char *obs_module_name(void)
{
return obs_module_text("DynamicDelay");
}
bool obs_module_load(void)
{
blog(LOG_INFO, "[Dynamic Delay] loaded version %s", PROJECT_VERSION);
obs_register_source(&dynamic_delay_filter);
return true;
}