-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathxfade-easing.h
More file actions
3250 lines (3043 loc) · 126 KB
/
xfade-easing.h
File metadata and controls
3250 lines (3043 loc) · 126 KB
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
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// FFmpeg XFade easing and extensions by Raymond Luckhurst, Scriptit UK, https://scriptit.uk
// GitHub: https://github.com/scriptituk/xfade-easing September 2023 MIT Licence
//
// This is a port of standard easing equations and CSS easing functions for the FFmpeg XFade filter
// It also ports extended transitions, notably GLSL transitions, for use with or without easing
// It adds XFade options <easing> and <reverse> and modifies option <transition>
//
// See https://github.com/scriptituk/xfade-easing for documentation
#include <stdbool.h>
#include <float.h>
#include <ctype.h>
#include "libavfilter/version.h"
#include "libavutil/avstring.h"
#include "libavutil/mem.h"
#include "libavutil/mem_internal.h"
#include "libavutil/parseutils.h"
////////////////////////////////////////////////////////////////////////////////
// definitions & prototypes
////////////////////////////////////////////////////////////////////////////////
#define P5f 0.5f /* ubiquitous point 5 float */
#define M_TAUf (M_PIf + M_PIf) /* 2*pi */
#define M_1_TAUf (M_1_PIf * P5f) /* 1/(2*pi) */
//#define USEf(func, ...) USE_##func(__VA_ARGS__)
#ifdef USEf /* debug: trap stdlib double calls */
#define acos(...) USEf(acosf, __VA_ARGS__)
#define asin(...) USEf(asinf, __VA_ARGS__)
#define atan2(...) USEf(atan2f, __VA_ARGS__)
#define cbrt(...) USEf(cbrtf, __VA_ARGS__)
#define ceil(...) USEf(ceilf, __VA_ARGS__)
#define copysign(...) USEf(copysignf, __VA_ARGS__)
#define cos(...) USEf(cosf, __VA_ARGS__)
#define exp2(...) USEf(exp2f, __VA_ARGS__)
#define fabs(...) USEf(absf, __VA_ARGS__)
#define floor(...) USEf(floorf, __VA_ARGS__)
#define fma(...) USEf(fmaf, __VA_ARGS__)
#define fmax(...) USEf(maxf, __VA_ARGS__)
#define fmin(...) USEf(minf, __VA_ARGS__)
#define fmaxf(...) USEf(maxf, __VA_ARGS__)
#define fminf(...) USEf(minf, __VA_ARGS__)
#define fmod(...) USEf(fmodf, __VA_ARGS__)
#define hypot(...) USEf(hypotf, __VA_ARGS__)
#define log(...) USEf(logf, __VA_ARGS__)
#define pow(...) USEf(powf, __VA_ARGS__)
#define round(...) USEf(roundf, __VA_ARGS__)
#define sin(...) USEf(sinf, __VA_ARGS__)
#define sqrt(...) USEf(sqrtf, __VA_ARGS__)
#define tan(...) USEf(tanf, __VA_ARGS__)
#define trunc(...) USEf(truncf, __VA_ARGS__)
#endif
static int xe_error(void *avcl, const char *fmt, ...);
static void xe_warning(void *avcl, const char *fmt, ...);
static void xe_debug(void *avcl, const char *fmt, ...);
////////////////////////////////////////////////////////////////////////////////
// aggregate types
////////////////////////////////////////////////////////////////////////////////
// reverse bit flags
typedef enum { REVERSE_TRANSITION = 1, REVERSE_EASING = 2, REVERSE_OVERSHOOT = 8 } ReverseFlags;
// blend modes
typedef enum {
NORMAL, MULTIPLY, SCREEN, OVERLAY, DARKEN, LIGHTEN, COLORDODGE, COLORBURN,
HARDLIGHT, SOFTLIGHT, DIFFERENCE, EXCLUSION, HUE, SATURATION, COLOR, LUMINOSITY
} BlendMode;
// integer pair
typedef struct {
int x, y;
} ivec2;
// normalised pixel coordinates
typedef struct {
float x, y;
} vec2;
// normalised plane data
typedef union {
DECLARE_ALIGNED(64, float, p)[4];
struct { float p0, p1, p2, p3; };
} vec4;
// colour value
typedef double Colour;
// easing arguments
typedef struct {
enum { STANDARD, LINEAR, BEZIER, STEPS } type; // easing type
union {
struct Standard { // Robert Penner & supplementary easings
enum { EASE_INOUT, EASE_IN, EASE_OUT } mode;
} e;
struct Linear { // CSS linear
int stops;
vec2 *points; // alloc
} l;
union Bezier { // CSS cubic-bezier
struct { float x1, y1, x2, y2; };
float p[4];
} b;
struct Steps { // CSS steps
int steps;
enum { JUMP_START, JUMP_END, JUMP_NONE, JUMP_BOTH } position;
} s;
};
} EasingArgs;
// extended transition arguments
typedef struct {
int argc;
struct Argv {
char *param; // optional name
double value;
} *argv; // alloc
} XTransitionArgs;
// xfade-easing context (member of XFadeContext)
struct XTransition;
typedef struct XFadeEasingContext {
float (*easingf)(const struct XFadeEasingContext *k, float progress);
vec4 (*xtransitionf)(const struct XTransition *e);
EasingArgs eargs;
XTransitionArgs targs;
double tdata[20]; // transition parameters and constants
float framerate;
float duration; // seconds
float r; // frame aspect ratio
int n; // number of planes
int mw, mh; // maximum width, height
int mv; // maximum pixel value
bool is_rgb; // pixel format is RGB type
bool is_16; // pixel depth > 8
bool init; // true when initialised
const struct XFadeContext *s; // the XFadeContext
} XFadeEasingContext;
// transition thread data (unit intervals) modelled on GL Transition Specification v1
typedef struct XTransition {
const AVFrame *xf[2]; // input frame data
float ratio; // frame width / height (cf. W / H)
float progress; // transition progress, 0.0 to 1.0 (cf. P)
vec2 p; // pixel position, .y==0 is bottom (cf. X, Y)
vec4 a, b; // plane data at p (cf. A, B)
const struct XFadeEasingContext *k; // the XFadeEasingContext
} XTransition;
////////////////////////////////////////////////////////////////////////////////
// easing functions
////////////////////////////////////////////////////////////////////////////////
// standard easings --------------------------------------------------
static float rp_quadratic(const XFadeEasingContext *k, float t)
{
const int mode = k->eargs.e.mode;
if (mode == EASE_IN) return t * t;
if (mode == EASE_OUT) return (2 - t) * t;
return (t < P5f) ? t * t * 2 : (2 - t) * t * 2 - 1;
}
static float rp_cubic(const XFadeEasingContext *k, float t)
{
const int mode = k->eargs.e.mode;
if (mode == EASE_IN) return t * t * t;
if (mode == EASE_OUT) return --t, t * t * t + 1;
return (t < P5f) ? (t * t * t * 4) : (--t, t * t * t * 4 + 1);
}
static float rp_quartic(const XFadeEasingContext *k, float t)
{
const int mode = k->eargs.e.mode;
if (mode == EASE_IN) return t *= t, t * t;
if (mode == EASE_OUT) return --t, t *= t, 1 - t * t;
return (t < P5f) ? (t *= t, t * t * 8) : (--t, t *= t, t * t * -8 + 1);
}
static float rp_quintic(const XFadeEasingContext *k, float t)
{
const int mode = k->eargs.e.mode;
float s;
if (mode == EASE_IN) return s = t, t *= t, t * t * s;
if (mode == EASE_OUT) return s = --t, t *= t, t * t * s + 1;
return (t < P5f) ? (s = t, t *= t, t * t * s * 16) : (s = --t, t *= t, t * t * s * 16 + 1);
}
static float rp_sinusoidal(const XFadeEasingContext *k, float t)
{
const int mode = k->eargs.e.mode;
if (mode == EASE_IN) return 1 - cosf(t * M_PI_2f);
if (mode == EASE_OUT) return sinf(t * M_PI_2f);
return (1 - cosf(t * M_PIf)) / 2;
}
static float rp_exponential(const XFadeEasingContext *k, float t)
{
const int mode = k->eargs.e.mode;
if (t <= 0 || t >= 1) return t > P5f;
if (mode == EASE_IN) return exp2f((t - 1) * 10);
if (mode == EASE_OUT) return 1 - exp2f(t * -10);
return (t < P5f) ? exp2f(20 * t - 11) : 1 - exp2f(9 - 20 * t);
}
static float rp_circular(const XFadeEasingContext *k, float t)
{
const int mode = k->eargs.e.mode;
if (mode == EASE_IN) return 1 - sqrtf(1 - t * t);
if (mode == EASE_OUT) return sqrtf((2 - t) * t);
return (t < P5f) ? (1 - sqrtf(1 - t * t * 4)) / 2 : (--t, (1 + sqrtf(1 - t * t * 4)) / 2);
}
static float rp_elastic(const XFadeEasingContext *k, float t)
{
const int mode = k->eargs.e.mode;
if (mode == EASE_IN) return --t, cosf(t * (M_PIf * 20.f / 3)) * exp2f(10 * t);
if (mode == EASE_OUT) return 1 - cosf(t * (M_PIf * 20.f / 3)) / exp2f(10 * t);
float p = t + t - 1, c = cosf(p * (M_PIf * 40.f / 9)) / 2; p = exp2f(10 * p);
return (t < P5f) ? c * p : 1 - c / p;
}
static float rp_back(const XFadeEasingContext *k, float t)
{
const int mode = k->eargs.e.mode;
float r = 1 - t, b = 1.70158f; // for 10% back
if (mode == EASE_IN) return t * t * (t * (b + 1) - b);
if (mode == EASE_OUT) return 1 - r * r * (r * (b + 1) - b);
b *= 1.525f;
return (t < P5f) ? t * t * (t * (b + 1) * 2 - b) * 2
: 1 - r * r * (r * (b + 1) * 2 - b) * 2;
}
static float rp_bounce(const XFadeEasingContext *k, float t)
{
const int mode = k->eargs.e.mode;
float c, s = (t < P5f) ? 1 : -1;
if (mode == EASE_IN) t = 1 - t;
else if (mode == EASE_INOUT) t = (1 - t - t) * s;
if (t < (4.f / 11.f)) c = 0;
else if (t < (8.f / 11.f)) t -= 6.f / 11.f, c = 3.f / 4.f;
else if (t < (10.f / 11.f)) t -= 9.f / 11.f, c = 15.f / 16.f;
else t -= 21.f / 22.f, c = 63.f / 64.f;
t = t * t * (121.f / 16.f) + c;
if (mode == EASE_IN) return 1 - t;
if (mode == EASE_OUT) return t;
return (1 - t * s) / 2;
}
// supplementary easings --------------------------------------------------
static float se_squareroot(const XFadeEasingContext *k, float t)
{
const int mode = k->eargs.e.mode;
if (mode == EASE_IN) return sqrtf(t);
if (mode == EASE_OUT) return 1 - sqrtf(1 - t);
return (t < P5f) ? sqrtf(t + t) / 2 : 1 - sqrtf(2 - t - t) / 2;
}
static float se_cuberoot(const XFadeEasingContext *k, float t)
{
const int mode = k->eargs.e.mode;
if (mode == EASE_IN) return cbrtf(t);
if (mode == EASE_OUT) return 1 - cbrtf(1 - t);
return (t < P5f) ? cbrtf(t + t) / 2 : 1 - cbrtf(2 - t - t) / 2;
}
static float se_flipelastic(const XFadeEasingContext *k, float t)
{
t = rp_elastic(k, t);
return (t < 0) ? -t : (t > 1) ? 2 - t : t;
}
static float se_flipback(const XFadeEasingContext *k, float t)
{
t = rp_back(k, t);
return (t < 0) ? -t : (t > 1) ? 2 - t : t;
}
// CSS easings --------------------------------------------------
// see https://drafts.csswg.org/css-easing-2/
// WebKit: https://github.com/WebKit/WebKit/blob/main/Source/WebCore/platform/animation/TimingFunction.cpp
// Gecko: https://github.com/mozilla/gecko-dev/blob/master/servo/components/style/piecewise_linear.rs
static float css_linear(const XFadeEasingContext *k, float t)
{
const struct Linear *a = &k->eargs.l;
vec2 *p = a->points;
int i, n = a->stops;
if (n == 0)
return t;
if (n == 1) // ? see https://searchfox.org/mozilla-central/source/servo/components/style/piecewise_linear.rs
return 1 - p[0].y; // y is constant (I can't see this in the spec)
for (i = n - 1; i; i--)
if (p[i].x <= t)
break;
if (i < 0)
i = 0;
else if (i == n - 1)
--i;
p += i;
if (p[1].x - p[0].x < FLT_EPSILON) // nearly equal
return p[1].y;
return p[0].y + (t - p[0].x) / (p[1].x - p[0].x) * (p[1].y - p[0].y);
}
// see https://drafts.csswg.org/css-easing-2/
// see https://cubic-bezier.com/
// see solve_cubic_bezier() at end of this file
// WebKit: https://github.com/WebKit/WebKit/blob/main/Source/WebCore/platform/animation/TimingFunction.cpp
static float solve_cubic_bezier(float x1, float y1, float x2, float y2, float x, float epsilon);
static float css_cubic_bezier(const XFadeEasingContext *k, float t)
{
const union Bezier *a = &k->eargs.b;
float epsilon = 1 / (1000 * k->duration); // as per TimingFunction.cpp
return solve_cubic_bezier(a->x1, a->y1, a->x2, a->y2, t, epsilon); // licensed code
}
// see https://drafts.csswg.org/css-easing-2/
// WebKit: https://github.com/WebKit/WebKit/blob/main/Source/WebCore/platform/animation/TimingFunction.cpp
static float css_steps(const XFadeEasingContext *k, float t)
{
const struct Steps *a = &k->eargs.s;
bool before = 0; // TODO: CSS before flag, if needed
int n = a->steps, s = floorf(t * n);
if (a->position == JUMP_START || a->position == JUMP_BOTH)
++s;
if (before && !fmodf(t * n, 1))
s--;
if (t >= 0 && s < 0)
s = 0;
if (a->position == JUMP_NONE)
--n;
else if (a->position == JUMP_BOTH)
++n;
if (t <= 1 && s > n)
s = n;
return (float)s / n;
}
////////////////////////////////////////////////////////////////////////////////
// extended transitions
////////////////////////////////////////////////////////////////////////////////
// FF functions:
// mix() fract() smoothstep()(clamped) in libavfilter/vf_xfade.c
// av_strtod() in libavutil/eval.c
// av_parse_color() in libavutil/parseutils.h
// av_clip*() in libavutil/common.h
// av_str*() in libavutil/avstring.h
// various in libavutil/mem.h libavutil/log.h
// FFDIFFSIGN in libavutil/macros.h
#define IVEC2(i, j) ((ivec2) { .x = (i), .y = (j) })
#define VEC2(i, j) ((vec2) { .x = (i), .y = (j) })
#define VEC3(i, j, k) ((vec4) { .p0 = (i), .p1 = (j), .p2 = (k) }) /* omit alpha */
#define VEC4(i, j, k, a) ((vec4) { .p0 = (i), .p1 = (j), .p2 = (k), .p3 = (a) })
// scalar functions --------------------------------------------------
static inline float degrees(float a) { return a * (360 / M_TAUf); }
static inline float radians(float a) { return a * (M_TAUf / 360); }
static inline float sign(float x) { return FFDIFFSIGN(x, 0); }
static inline float absf(float x) { return FFABS(x); }
static inline float minf(float x, float y) { return FFMIN(x, y); }
static inline float maxf(float x, float y) { return FFMAX(x, y); }
static inline float glmod(float x, float y) { return x - floorf(x / y) * y; } // C fmod uses trunc
static inline int step(float edge, float x) { return (x < edge) ? 0 : 1; }
static inline float lerp(float x, float y, float z) { return x + (y - x) * z; }
static inline float mixf(float a, float b, float m) { return mix(b, a, m); } // vf_xfade.c mix() args are swapped
static inline bool betweenf(float x, float min, float max) { return x >= min && x <= max; }
static inline bool betweenUI(float x) { return betweenf(x, 0, 1); } // within unit interval
static inline float clipUI(float x) { return av_clipf(x, 0, 1); } // clip to unit interval
static inline float frandf(float x, float y)
{
return fract(sinf(x * 12.9898f + y * 78.233f) * 43758.545f); // cf. vf_xfade.c frand()
// return fract(sin(x * 12.9898 + y * 78.233) * 43758.545); // doubles render like GL Transitions
}
static inline float r2f(AVRational r) { return (float)r.num / r.den; } // cf. libavutil/rational.h av_q2d()
// coordinate functions --------------------------------------------------
static inline vec2 vec2f(float f) { return VEC2(f, f); }
static inline vec2 vec2i(ivec2 p) { return VEC2(p.x, p.y); } // as float
static inline vec2 flip2(vec2 p) { return VEC2(p.y, p.x); }
static inline vec2 add2f(vec2 p, float f) { return VEC2(p.x + f, p.y + f); }
static inline vec2 sub2f(vec2 p, float f) { return VEC2(p.x - f, p.y - f); }
static inline vec2 mul2f(vec2 p, float f) { return VEC2(p.x * f, p.y * f); }
static inline vec2 div2f(vec2 p, float f) { return mul2f(p, 1 / f); }
static inline vec2 add2(vec2 a, vec2 b) { return VEC2(a.x + b.x, a.y + b.y); }
static inline vec2 sub2(vec2 a, vec2 b) { return VEC2(a.x - b.x, a.y - b.y); }
static inline vec2 mul2(vec2 a, vec2 b) { return VEC2(a.x * b.x, a.y * b.y); }
static inline vec2 div2(vec2 a, vec2 b) { return VEC2(a.x / b.x, a.y / b.y); }
static inline vec2 abs2(vec2 p) { return VEC2(absf(p.x), absf(p.y)); }
static inline vec2 floor2(vec2 p) { return VEC2(floorf(p.x), floorf(p.y)); }
static inline vec2 fract2(vec2 p) { return VEC2(fract(p.x), fract(p.y)); }
static inline vec2 mix2(vec2 a, vec2 b, float m) { return add2(mul2f(a, 1 - m), mul2f(b, m)); }
static inline vec2 mod2(vec2 p, float f) { return VEC2(glmod(p.x, f), glmod(p.y, f)); }
static inline vec2 rcp2(vec2 p) { return VEC2(1 / p.x, 1 / p.y); }
static inline vec2 sign2(vec2 p) { return VEC2(sign(p.x), sign(p.y)); }
static inline float min2(vec2 p) { return minf(p.x, p.y); }
static inline float max2(vec2 p) { return maxf(p.x, p.y); }
static inline float asum2(vec2 p) { return absf(p.x) + absf(p.y); }
static inline float atn2(vec2 p) { return atan2f(p.y, p.x); }
static inline float frand2(vec2 p) { return frandf(p.x, p.y); }
static inline float length2(vec2 p) { return hypotf(p.x, p.y); }
static inline float distance2(vec2 a, vec2 b) { return length2(sub2(a, b)); }
static inline float dot2(vec2 a, vec2 b) { return a.x * b.x + a.y * b.y; }
static inline vec2 normalize2(vec2 p) { return div2f(p, length2(p)); }
static inline vec2 cossin2(float a) { return VEC2(cosf(a), sinf(a)); } // cf. sincosf()
static inline vec2 rot2(vec2 p, float a) // clockwise
{
vec2 q = cossin2(a);
return VEC2(p.x * q.x + p.y * q.y, p.y * q.x - p.x * q.y);
}
static inline bool between2(vec2 p, float min, float max) { return min2(p) >= min && max2(p) <= max; }
static inline bool betweenUI2(vec2 p) { return between2(p, 0, 1); }
// colour functions --------------------------------------------------
static inline vec4 vec4f(float f) { return VEC4(f, f, f, f); }
static inline vec4 mul4f(vec4 c, float f) { return VEC4(c.p0 * f, c.p1 * f, c.p2 * f, c.p3 * f); }
static inline vec4 div4f(vec4 c, float f) { return mul4f(c, 1 / f); }
static inline vec4 add4(vec4 a, vec4 b) { return VEC4(a.p0 + b.p0, a.p1 + b.p1, a.p2 + b.p2, a.p3 + b.p3); }
static inline vec4 sub4(vec4 a, vec4 b) { return VEC4(a.p0 - b.p0, a.p1 - b.p1, a.p2 - b.p2, a.p3 - b.p3); }
static inline vec4 mix4(vec4 a, vec4 b, float m) { return add4(mul4f(a, 1 - m), mul4f(b, m)); }
static inline vec4 clipUI4(vec4 c) { return VEC4(clipUI(c.p0), clipUI(c.p1), clipUI(c.p2), clipUI(c.p3)); }
// these vec3 functions return .p3 of first vec4 parameter unmodified
static inline vec4 vec3f(float f) { return VEC3(f, f, f); } // omits alpha
static inline vec4 add3f(vec4 c, float f) { c.p0 += f; c.p1 += f; c.p2 += f; return c; }
static inline vec4 sub3f(vec4 c, float f) { c.p0 -= f; c.p1 -= f; c.p2 -= f; return c; }
static inline vec4 mul3f(vec4 c, float f) { c.p0 *= f; c.p1 *= f; c.p2 *= f; return c; }
static inline vec4 div3f(vec4 c, float f) { return mul3f(c, 1 / f); }
static inline vec4 add3(vec4 a, vec4 b) { a.p0 += b.p0; a.p1 += b.p1; a.p2 += b.p2; return a; }
static inline vec4 sub3(vec4 a, vec4 b) { a.p0 -= b.p0; a.p1 -= b.p1; a.p2 -= b.p2; return a; }
static inline vec4 cpl3(vec4 c) { c.p0 = 1 - c.p0; c.p1 = 1 - c.p1; c.p2 = 1 - c.p2; return c; }
static inline vec4 abs3(vec4 c) { c.p0 = absf(c.p0); c.p1 = absf(c.p1); c.p2 = absf(c.p2); return c; }
static inline vec4 fract3(vec4 c) { c.p0 = fract(c.p0); c.p1 = fract(c.p1); c.p2 = fract(c.p2); return c; }
static inline vec4 sqrt3(vec4 c) { c.p0 = sqrtf(c.p0); c.p1 = sqrtf(c.p1); c.p2 = sqrtf(c.p2); return c; }
static inline float min3(vec4 c) { return FFMIN3(c.p0, c.p1, c.p2); }
static inline float max3(vec4 c) { return FFMAX3(c.p0, c.p1, c.p2); }
static inline float dot3(vec4 a, vec4 b) { return a.p0 * b.p0 + a.p1 * b.p1 + a.p2 * b.p2; }
static inline float length3(vec4 c) { return sqrtf(dot3(c, c)); }
static inline vec4 normalize3(vec4 c) { return div3f(c, length3(c)); }
// colour conversion --------------------------------------------------
// convert GBR to YUV standard-definition BT.601
// TODO: support high-definition BT.709 too?
// see https://en.wikipedia.org/wiki/YCbCr#ITU-R_BT.601_conversion
static const vec4 Od = {{ 16./255, 128./255, 128./255, 0 }}; // digital headroom/toeroom offsets
static inline vec4 gbr2yuv(vec4 c)
{
// BT.601 matrix G B R
static const vec4 Y = {{ 128.553/255, 24.966/255, 65.481/255 }},
U = {{ -74.203/255, 112./255, -37.797/255 }},
V = {{ -93.786/255, -18.214/255, 112./255 }};
return add4(VEC4(dot3(c, Y), dot3(c, U), dot3(c, V), c.p3), Od);
}
// convert YUV to GBR
static inline vec4 yuv2gbr(vec4 c)
{
// inverse BT.601 matrix
#define BU 255./224*1.772
#define RV 255./224*1.402
static const float Bu = BU, Rv = RV, Gy = 255./219, Gu = -0.114/0.587*BU, Gv = -0.299/0.587*RV;
c = sub4(c, Od);
float y = c.p0 * Gy; // Y G=B=R
return VEC4(y + c.p1 * Gu + c.p2 * Gv, y + c.p1 * Bu, y + c.p2 * Rv, c.p3);
}
// blending --------------------------------------------------
// see https://www.w3.org/TR/compositing-1/#blending
// see https://opensource.adobe.com/dc-acrobat-sdk-docs/pdfstandards/PDF32000_2008.pdf#page=322
static inline float normal(float b, float f) { return f; }
static inline float multiply(float b, float f) { return b * f; }
static inline float screen(float b, float f) { return b + f - b * f; }
static inline float darken(float b, float f) { return minf(b, f); }
static inline float lighten(float b, float f) { return maxf(b, f); }
static inline float colordodge(float b, float f) { return (b <= 0) ? 0 : (f >= 1) ? 1 : minf(1, b / (1 - f)); }
static inline float colorburn(float b, float f) { return (b >= 1) ? 1 : (f <= 0) ? 0 : 1 - minf(1, (1 - b) / f); }
static inline float hardlight(float b, float f) { return (f <= P5f) ? multiply(b, f + f) : screen(b, f + f - 1); }
static inline float overlay(float b, float f) { return hardlight(f, b); }
static inline float difference(float b, float f) { return absf(b - f); }
static inline float exclusion(float b, float f) { return b + f - b * f * 2; }
static inline float softlight(float b, float f) {
bool l = f <= P5f;
float m = l ? b : 1, d = l ? 1 : (b <= 0.25f) ? ((b * 16 - 12) * b + 4) * b : sqrtf(b); // fmaf?
return b + (f + f - 1) * m * (d - b);
}
static inline float lum3(vec4 c) { return dot3(c, VEC3(0.587f, 0.114f, 0.299f)); } // Rec. 601
static inline float sat3(vec4 c) { return max3(c) - min3(c); }
static vec4 lum3f(vec4 c, float l) { // set luminance
c = add3f(c, l - lum3(c));
// scale luminance
float n = min3(c), x = max3(c);
l = lum3(c);
if (n < 0)
c = add3f(mul3f(sub3f(c, l), l / (l - n)), l);
if (x > 1)
c = add3f(mul3f(sub3f(c, l), (1 - l) / (x - l)), l);
return c;
}
static vec4 sat3f(vec4 c, float s) { // set saturation
float *p = c.p;
// sort into component value order: min mid max
int o[] = {0, 1, 2};
if (p[0] > p[1])
o[1] = 0, o[0] = 1;
if (p[o[1]] > c.p2) {
o[2] = o[1], o[1] = 2;
if (p[o[0]] > p[o[1]])
o[1] = o[0], o[0] = 2;
}
// scale saturation
float *n = &p[o[0]], *d = &p[o[1]], *x = &p[o[2]];
if (*x > *n)
*d = (*d - *n) * s / (*x - *n), *x = s;
else
*d = 0, *x = 0;
*n = 0;
return c;
}
// composite background, foreground & blended colours with alpha
// this is the colour compositing formula from PDF32000_2008.pdf 11.3.6 but simplified
// see https://stackoverflow.com/a/40962043
static inline float comp(float c, float d, float f, float r, float b) { return fmaf(fmaf(c, d, f), r, b); }
static vec4 composite(vec4 b, vec4 f, vec4 c) { // bg, fg, blended
float a = f.p3 + b.p3 - f.p3 * b.p3; // resulting alpha
float r = f.p3 / a;
if (isnan(r))
r = 0;
c = sub3(c, f);
f = sub3(f, b);
return VEC4(comp(c.p0, b.p3, f.p0, r, b.p0),
comp(c.p1, b.p3, f.p1, r, b.p1),
comp(c.p2, b.p3, f.p2, r, b.p2),
a);
}
// blend background & foreground colours
// see https://www.w3.org/TR/compositing-1/#blending
static vec4 blend(const XTransition *e, vec4 b, vec4 f, BlendMode mode) { // bg, fg, mode
vec4 c;
if (!e->k->is_rgb)
b = yuv2gbr(b), f = yuv2gbr(f);
#define BLEND3(n) VEC3(n(b.p0, f.p0), n(b.p1, f.p1), n(b.p2, f.p2))
switch (mode) {
default: c = BLEND3(normal); break; // NORMAL
case MULTIPLY: c = BLEND3(multiply); break;
case SCREEN: c = BLEND3(screen); break;
case OVERLAY: c = BLEND3(overlay); break;
case DARKEN: c = BLEND3(darken); break;
case LIGHTEN: c = BLEND3(lighten); break;
case COLORDODGE: c = BLEND3(colordodge); break;
case COLORBURN: c = BLEND3(colorburn); break;
case HARDLIGHT: c = BLEND3(hardlight); break;
case SOFTLIGHT: c = BLEND3(softlight); break;
case DIFFERENCE: c = BLEND3(difference); break;
case EXCLUSION: c = BLEND3(exclusion); break;
case HUE: c = lum3f(sat3f(f, sat3(b)), lum3(b)); break;
case SATURATION: c = lum3f(sat3f(b, sat3(f)), lum3(b)); break;
case COLOR: c = lum3f(f, lum3(b)); break;
case LUMINOSITY: c = lum3f(b, lum3(f)); break;
}
c = composite(b, f, c);
c = clipUI4(c);
if (!e->k->is_rgb)
c = gbr2yuv(c);
return c;
}
// get/set pixel data --------------------------------------------------
// nb_planes is always 1, 3 or 4
// nb_planes = 1: (gray/mono) processed as YUV so set u,v to 0.5
// nb_planes < 4: (opaque) set alpha to 1
#define PLANED ((vec4) { .p1 = P5f, .p2 = P5f, .p3 = 1 }) // default plane data
// scale unit interval to clipped integer
// see https://stackoverflow.com/a/46575472 Converting color value from float 0..1 to byte 0..255
static inline int scaleUI(float val, int max) { return av_clip(val * max + P5f, 0, max); } // trunc rounded
// get pointer to line of plane data at y
static av_always_inline uint8_t *pix8(const AVFrame *f, int p, int x, int y) { return &(&f->data[p][f->linesize[p] * y])[x]; }
static av_always_inline uint16_t *pix16(const AVFrame *f, int p, int x, int y) { return &((uint16_t*)pix8(f, p, y, 0))[x]; }
#define _getFromColor1(v) getColor(e, v.x, v.y, 0)
#define _getFromColor2(x, y) getColor(e, (x), (y), 0)
#define _getFromColorVA(_1,_2,NAME,...) NAME
#define getFromColor(...) _getFromColorVA(__VA_ARGS__, _getFromColor2, _getFromColor1)(__VA_ARGS__)
#define _getToColor1(v) getColor(e, v.x, v.y, 1)
#define _getToColor2(x, y) getColor(e, (x), (y), 1)
#define _getToColorVA(_1,_2,NAME,...) NAME
#define getToColor(...) _getToColorVA(__VA_ARGS__, _getToColor2, _getToColor1)(__VA_ARGS__)
// get from or to colour at pixel point
static av_noinline vec4 getColor(const XTransition *e, float x, float y, int nb) // cf. vf_xfade.c getpix()
{
const XFadeEasingContext *k = e->k;
const AVFrame *f = e->xf[nb];
const int i = scaleUI(x, k->mw), j = scaleUI(1 - y, k->mh), n = k->n;
const float sv = 1.f / k->mv; // UI scale value
vec4 c = PLANED; // default plane values
int p = 0;
if (k->is_16)
do
c.p[p] = *pix16(f, p, i, j) * sv;
while (++p < n);
else
do
c.p[p] = *pix8(f, p, i, j) * sv;
while (++p < n);
return c;
}
// transition arguments --------------------------------------------------
// convert colour arg to plane data
// value > 1 is RGBA (argv() parser below adds 1^32 for colours)
// 0 <= value <= 1 is opaque greyscale
// value < -1 is a texture type
// -1 <= value < 0 is fully transparent greyscale
static vec4 texture(const XTransition *e, int type); // delegate
static vec4 colour(const XTransition *e, Colour value)
{
const XFadeEasingContext *k = e->k;
vec4 c;
if (value > 1) { // RGBA
uint32_t rgba = value; // packed RGBA (clips bit 32 colour flag)
uint8_t r = rgba >> 24, g = rgba >> 16, b = rgba >> 8, a = rgba;
c = mul4f(VEC4(g, b, r, a), 1.f / 255); // normalised GBRA
if (!k->is_rgb)
c = gbr2yuv(c); // normalised YUVA
} else if (value <= -2) { // texture
int type = value; // texture type (trunc)
c = texture(e, type); // create texture
if (!k->is_rgb)
c = gbr2yuv(c);
} else { // greyscale
bool s = signbit(value); // for neg zero
float grey = s ? clipUI(-value) : value;
float p12 = k->is_rgb ? grey : P5f;
c = VEC4(grey, p12, p12, !s); // opaque/transparent
}
return c;
}
// colour arg debugging
static void arg4(const XTransition *e, double value)
{
const XFadeEasingContext *k = e->k;
vec4 c = colour(e, value);
const char *t = (value < -1) ? "texture"
: signbit(value) ? "transparent"
: (value <= 1) ? "grey"
: k->is_rgb ? "gbra" : "yuva";
if (value <= -2) { // texture
xe_debug(NULL, "colour: %s = %d\n", t, (int)value);
} else if (value <= 1 || k->is_rgb) { // transparent/greyscale/RGBA
xe_debug(NULL, "colour: %s = %g %g %g %g\n", t, c.p0, c.p1, c.p2, c.p3);
} else { // YUVA
vec4 d = yuv2gbr(c); // test conversions
int v[4]; for (int i = 0; i < 4; i++) v[i] = scaleUI(d.p[i], 255);
xe_debug(NULL, "colour: %s = %g %g %g %g (#%02X%02X%02X%02X)\n",
t, c.p0, c.p1, c.p2, c.p3, v[2], v[0], v[1], v[3]);
}
}
// simple caching of transition constants
#define INIT if (!e->k->init)
#define INIT_BEGIN int argi = 0;
#define INIT_END INIT return (vec4){{0}};
#define ARG1(type, param, def) \
argi++; INIT arg(e->k, argi-1, #type, #param, def); \
const type param = e->k->tdata[argi-1];
#define ARG2(type, param, defx, defy) \
argi+=2; INIT arg(e->k, argi-2, #type, #param ".x", defx), arg(e->k, argi-1, #type, #param ".y", defy); \
const type param = (type) { e->k->tdata[argi-2], e->k->tdata[argi-1] };
#define ARG4(type, param, def) \
argi++; INIT arg(e->k, argi-1, #type, #param, def), arg4(e, e->k->tdata[argi-1]); \
const type param = e->k->tdata[argi-1];
#define VAR1(type, param, val) \
argi++; INIT var(e->k, argi-1, val); \
const type param = e->k->tdata[argi-1];
#define VAR2(type, param, valx, valy) \
argi+=2; INIT var(e->k, argi-2, valx), var(e->k, argi-1, valy); \
const type param = (type) { e->k->tdata[argi-2], e->k->tdata[argi-1] };
// set const variable value during initialisation
static inline void var(const XFadeEasingContext *k, int argi, double value)
{
((double*)k->tdata)[argi] = value; // cast away const on mutable when initialising to keep const when not
}
// set parameter arg or default value during initialisation
static av_noinline void arg(
const XFadeEasingContext *k,
int argi,
const char *type,
const char *param,
double value) // default
{
const XTransitionArgs *a = &k->targs;
for (int j = 0; j < a->argc; j++)
if (a->argv[j].param && !av_strcasecmp(a->argv[j].param, param))
{ value = a->argv[j].value; goto ret; } // named param
if (a->argc > argi && !a->argv[argi].param && !isnan(a->argv[argi].value))
value = a->argv[argi].value; // positional param
ret:
var(k, argi, value); // double to store 32-bit (10-digit) precision lossless colour values
xe_debug(NULL, "param: %s %s = %g == %d(int) == 0x%08X(unsigned)\n", type, param, value, (int)value, (unsigned)value);
}
// extended transitions --------------------------------------------------
// GL transition names, algorithms, variable names & credits are replicated from the distribution source
static vec4 gl_angular(const XTransition *e) // by Fernando Kuteken
{ // License: MIT
INIT_BEGIN
ARG1(float, startingAngle, 90)
ARG1(bool, clockwise, 0)
VAR1(float, offset, radians(startingAngle))
INIT_END
float angle = atn2(sub2f(e->p, P5f)) + offset;
float normalizedAngle = angle * M_1_TAUf + P5f;
if (clockwise)
normalizedAngle = -normalizedAngle;
normalizedAngle = fract(normalizedAngle);
return step(normalizedAngle, e->progress) ? e->b : e->a;
}
static vec4 gl_Bars(const XTransition *e) // by Mark Craig
{ // License: MIT (assumed)
INIT_BEGIN
ARG1(bool, vertical, 0)
INIT_END
float r = frandf(vertical ? e->p.x : e->p.y, 0);
return (r > e->progress) ? e->a : e->b;
}
static vec4 gl_blend(const XTransition *e) // by scriptituk
{ // License: MIT
INIT_BEGIN
ARG1(int, mode, 0)
INIT_END
vec4 blended = blend(e, e->a, e->b, mode);
return (e->progress < P5f)
? mix4(e->a, blended, e->progress * 2)
: mix4(blended, e->b, e->progress * 2 - 1);
}
static vec4 gl_BookFlip(const XTransition *e) // by hong
{ // License: MIT
INIT_END
vec4 colour;
float p = P5f - e->progress;
vec2 c = sub2f(e->p, P5f);
bool pr = step(p, c.x);
if (c.x < 0) {
if (!pr)
return e->a;
vec2 skewLeft = {
(1 - c.x / p) * P5f,
(c.y / (P5f - (p + p + 1) * c.x) + 1) * P5f
};
colour = getToColor(skewLeft);
} else {
if (pr)
return e->b;
vec2 skewRight = {
(1 + c.x / p) * P5f,
(c.y / (P5f - (p + p - 1) * c.x) + 1) * P5f
};
colour = getFromColor(skewRight);
}
float shadeVal = maxf(0.7f, absf(p) * 2);
colour.p0 *= shadeVal;
if (e->k->is_rgb)
colour.p1 *= shadeVal, colour.p2 *= shadeVal;
return colour;
}
static vec4 gl_Bounce(const XTransition *e) // by Adrian Purser
{ // License: MIT
INIT_BEGIN
ARG1(float, bounces, 3)
ARG1(int, direction, 0) // S,W,N,E
ARG1(float, shadowAlpha, 0.6)
ARG1(float, shadowHeight, 0.075)
ARG4(Colour, shadowColor, 0)
INIT_END
float phase = e->progress * M_PIf * bounces;
float p = absf(cosf(phase)) * (1 - sinf(e->progress * M_PI_2f));
if (direction & 2)
p = 1 - p;
vec2 v = e->p;
float d = ((direction & 1) ? v.x : v.y) - p;
if (step(d, 0)) {
if (direction & 1)
v.x = 1 + d;
else
v.y = 1 + d;
return getFromColor(v);
}
if (!step(d, shadowHeight))
return e->b;
float m = mixf(
d / shadowHeight * shadowAlpha + (1 - shadowAlpha),
1,
smoothstep(0.95f, 1, e->progress) // fade-out the shadow at the end
);
return mix4(e->b, colour(e, shadowColor), 1 - m);
}
static vec4 gl_BowTie(const XTransition *e) // by huynx
{ // License: MIT
INIT_BEGIN
ARG1(bool, vertical, 0)
INIT_END
vec2 p = e->p, a = vec2f(P5f), b = a, c = a;
if (vertical)
a.y = e->progress, b.x -= e->progress, c.x += e->progress, b.y = 0, c.y = 0;
else
a.x = e->progress, b.y -= e->progress, c.y += e->progress, b.x = 0, c.x = 0;
bool pass = 0;
do {
bool b1 = dot2(VEC2(p.x - a.x, p.y - a.y), VEC2(c.y - a.y, a.x - c.x)) < 0,
b2 = dot2(VEC2(p.x - b.x, p.y - b.y), VEC2(a.y - b.y, b.x - a.x)) < 0,
b3 = dot2(VEC2(p.x - c.x, p.y - c.y), VEC2(b.y - c.y, c.x - b.x)) < 0;
if (b1 == b2 && b2 == b3) { // in triangle
if (e->progress < 0.1f)
break;
if (!pass != (vertical ? p.y : p.x) < P5f)
return pass ? e->a : e->b;
// blur edge
vec2 lineDir = sub2(b, a);
vec2 perpDir = VEC2(lineDir.y, -lineDir.x);
vec2 dirToPt = sub2(b, p);
float dist1 = absf(dot2(normalize2(perpDir), dirToPt));
lineDir = sub2(c, a);
perpDir = VEC2(lineDir.y, -lineDir.x);
dirToPt = sub2(c, p);
float dist2 = absf(dot2(normalize2(perpDir), dirToPt));
float min_dist = minf(dist1, dist2);
float m = (min_dist < 0.005f) ? min_dist * 200 : 1;
return mix4(e->a, e->b, m);
}
if (vertical)
a.y = 1 - a.y, b.y = 1, c.y = 1;
else
a.x = 1 - a.x, b.x = 1, c.x = 1;
} while ((pass = !pass));
return e->a;
}
static vec4 gl_ButterflyWaveScrawler(const XTransition *e) // by mandubian
{ // License: MIT
INIT_BEGIN
ARG1(float, amplitude, 1)
ARG1(float, waves, 30)
ARG1(float, colorSeparation, 0.3)
INIT_END
// func compute
vec2 o = sub2f(mul2f(e->p, sinf(e->progress * amplitude)), P5f);
vec2 h = { 1, 0 }; // horizontal vector
float theta = acosf(dot2(o, h)) * waves; // butterfly polar function
float disp = (expf(cosf(theta)) - cosf(theta * 4) * 2 + powf(sinf((theta * 2 - M_PIf) / 24), 5)) * 0.1f;
// end compute
float dp = disp * e->progress;
vec4 texTo = getToColor(add2f(e->p, disp - dp)); // inv
vec4 texFrom = getFromColor(add2f(e->p, dp));
texFrom.p1 = getFromColor(add2f(e->p, dp * (1 + colorSeparation))).p1;
texFrom.p2 = getFromColor(add2f(e->p, dp * (1 - colorSeparation))).p2;
return mix4(texFrom, texTo, e->progress);
}
static vec4 gl_cannabisleaf(const XTransition *e) // by Flexi23
{ // License: MIT
INIT_END
if (e->progress == 0)
return e->a;
vec2 leaf_uv = div2f(sub2f(e->p, P5f), 10 * powf(e->progress, 3.5f));
leaf_uv.y += 0.35f; // leaf offset
float r = 0.18f; // leaf size
float o = atn2(leaf_uv);
// for curve see https://www.wolframalpha.com/input/?i=cannabis+curve
float curve = (1 + sinf(o)) * (1 + 0.9f * cosf(8 * o)) * (1 + 0.1f * cosf(24 * o)) * (0.9f + 0.05f * cosf(200 * o));
return step(r * curve, length2(leaf_uv)) ? e->a : e->b;
}
static vec4 gl_chessboard(const XTransition *e) // by lql
{ // License: MIT
INIT_BEGIN
ARG1(int, grid, 8)
INIT_END
vec2 st = mul2f(e->p, grid);
vec2 idx = floor2(st);
float g = st.x - idx.x;
int checker = (int) (idx.x + idx.y) % 2;
bool mixFactor = (e->progress < P5f)
? checker && step(g, e->progress * 2)
: checker || step(g, e->progress * 2 - 1);
return mixFactor ? e->b : e->a;
}
static vec4 gl_CornerVanish(const XTransition *e) // by Mark Craig
{ // License: MIT (assumed)
INIT_END
float b1 = (1 - e->progress) / 2, b2 = 1 - b1;
return (betweenf(e->p.x, b1, b2) || betweenf(e->p.y, b1, b2)) ? e->b : e->a;
}
static vec4 gl_CrazyParametricFun(const XTransition *e) // by mandubian
{ // License: MIT
INIT_BEGIN
ARG1(float, a, 4)
ARG1(float, b, 1)
ARG1(float, amplitude, 120)
ARG1(float, smoothness, 0.1)
INIT_END
vec2 p = mul2f(cossin2(e->progress), a - b);
vec2 o = mul2f(cossin2(e->progress * (a / b - 1)), b);
p.x += o.x;
p.y -= o.y;
o = sub2f(e->p, P5f);
p = mul2f(p, e->progress * length2(o) * amplitude);
p = div2f(VEC2(sinf(p.x), sinf(p.y)), smoothness);
o = mul2(o, p);
vec4 f = getFromColor(add2(e->p, o));
return mix4(f, e->b, smoothstep(0.2f, 1, e->progress));
}
static vec4 gl_crosshatch(const XTransition *e) // by pthrasher
{ // License: MIT
INIT_BEGIN
ARG2(vec2, center, 0.5, 0.5)
ARG1(float, threshold, 3)
ARG1(float, fadeEdge, 0.1)
INIT_END
float dist = distance2(center, e->p) / threshold;
float r = e->progress - minf(frandf(e->p.y, 0), frandf(0, e->p.x));
r = mixf(step(dist, r), 1, smoothstep(1 - fadeEdge, 1, e->progress));
return mix4(e->a, e->b, r * smoothstep(0, fadeEdge, e->progress));
}
static vec4 gl_CrossOut(const XTransition *e) // by Mark Craig
{ // License: MIT (assumed)
INIT_BEGIN
ARG1(float, smoothness, 0.05)
INIT_END
float c = e->progress / 2;
vec2 p = sub2f(e->p, P5f);
float ds = p.x + p.y, dd = p.y - p.x;
if (betweenf(ds, -c, c) || betweenf(dd, -c, c))
return e->b;
float cs = c + smoothness;
if (!(betweenf(ds, -cs, cs) || betweenf(dd, -cs, cs)))
return e->a;
float d = absf((p.x >= 0 != p.y >= 0) ? ds : dd);
return mix4(e->b, e->a, (d - c) / smoothness);
}
static vec4 gl_crosswarp(const XTransition *e) // by Eke Péter
{ // License: MIT
INIT_END
float x = smoothstep(0, 1, e->progress * 2 + e->p.x - 1);
vec2 c = sub2f(e->p, P5f);
vec4 a = getFromColor(add2f(mul2f(c, 1 - x), P5f));
vec4 b = getToColor(add2f(mul2f(c, x), P5f));
return mix4(a, b, x);
}
static vec4 gl_CrossZoom(const XTransition *e) // by rectalogic
{ // License: MIT
INIT_BEGIN
ARG1(float, strength, 0.4)
ARG2(vec2, centerFrom, 0.25, 0.5)
ARG2(vec2, centerTo, 0.75, 0.5)
INIT_END
// linear interpolate centerpoint travel
vec2 center = { lerp(centerFrom.x, centerTo.x, e->progress),
lerp(centerFrom.y, centerTo.y, e->progress) };
XFadeEasingContext x = { .eargs = { .e.mode = EASE_INOUT } };
float dissolve = rp_exponential(&x, e->progress);
// mirrored sinusoidal loop: 0->strength then strength->0
float strength2 = strength * rp_sinusoidal(&x, e->progress * 2);
vec4 color = vec3f(0);
float total = 0;
vec2 toCenter = sub2(center, e->p);