-
Notifications
You must be signed in to change notification settings - Fork 10
/
xfade-easing.h
2433 lines (2254 loc) · 91.5 KB
/
xfade-easing.h
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: owner scriptituk; repository xfade-easing; https://github.com/scriptituk/xfade-easing
//
// 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
//
// See https://github.com/scriptituk/xfade-easing for documentation
#include <stdbool.h>
#include <float.h>
#include "libavutil/avstring.h"
#include "libavutil/mem.h"
#include "libavutil/parseutils.h"
//#define RGB2YUV_SWSSCALE
#ifdef RGB2YUV_SWSSCALE
#include "libavutil/pixfmt.h"
#include "libswscale/swscale.h"
#endif
////////////////////////////////////////////////////////////////////////////////
// aggregate types
////////////////////////////////////////////////////////////////////////////////
// reverse option bit flags
enum { REVERSE_TRANSITION = 1, REVERSE_EASING = 2 } ReverseOpts;
// normalised pixel position
typedef struct {
float x, y;
} vec2;
// normalised plane data
typedef union {
struct { float p0, p1, p2, p3; };
float p[4];
} vec4;
// 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;
// transition thread data, modelled on GL Transition Specification v1
typedef struct XTransition {
const float progress; // transition progress, moves from 0.0 to 1.0 (cf. P)
const float ratio; // viewport width / height (cf. W / H)
vec2 p; // pixel position in slice, .y==0 is bottom (cf. X, Y)
vec4 a, b; // plane data at p (cf. A, B)
double data[20]; // initialised parameters and constants
bool init; // true to initialise data
const struct XFadeContext *s; // the XFadeContext with its XFadeEasingContext
} XTransition;
// xfade-easing context (member of XFadeContext)
typedef struct XFadeEasingContext {
float (*easingf)(const struct XFadeEasingContext *k, float progress);
vec4 (*xtransitionf)(const struct XTransition *e);
EasingArgs eargs;
XTransitionArgs targs;
float duration; // seconds
float framerate;
vec4 black, white, transparent;
} XFadeEasingContext;
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, ...);
#define P5f 0.5f /* ubiquitous point 5 float */
#define M_1_2PIf (M_1_PIf * P5f) /* 1/(2*pi) */
#define M_2PIf (M_PIf + M_PIf) /* 2*pi */
////////////////////////////////////////////////////////////////////////////////
// easing functions
////////////////////////////////////////////////////////////////////////////////
static float power_ease(const XFadeEasingContext *k, float t, float power)
{
const int mode = k->eargs.e.mode;
return (mode == EASE_IN) ? powf(t, power)
: (mode == EASE_OUT) ? 1 - powf(1 - t, power)
: (t < P5f) ? powf(2 * t, power) / 2 : 1 - powf(2 * (1 - t), power) / 2;
}
static float rp_quadratic(const XFadeEasingContext *k, float t) { return power_ease(k, t, 2); }
static float rp_cubic(const XFadeEasingContext *k, float t) { return power_ease(k, t, 3); }
static float rp_quartic(const XFadeEasingContext *k, float t) { return power_ease(k, t, 4); }
static float rp_quintic(const XFadeEasingContext *k, float t) { return power_ease(k, t, 5); }
static float rp_sinusoidal(const XFadeEasingContext *k, float t)
{
const int mode = k->eargs.e.mode;
return (mode == EASE_IN) ? 1 - cosf(t * M_PI_2f)
: (mode == EASE_OUT) ? sinf(t * M_PI_2f)
: (1 - cosf(t * M_PIf)) / 2;
}
static float rp_exponential(const XFadeEasingContext *k, float t)
{
const int mode = k->eargs.e.mode;
return (t == 0 || t == 1) ? t
: (mode == EASE_IN) ? powf(2, -10 * (1 - t))
: (mode == EASE_OUT) ? 1 - powf(2, -10 * t)
: (t < P5f) ? powf(2, 20 * t - 11) : 1 - powf(2, 9 - 20 * t);
}
static float rp_circular(const XFadeEasingContext *k, float t)
{
const int mode = k->eargs.e.mode;
return (mode == EASE_IN) ? 1 - sqrtf(1 - t * t)
: (mode == EASE_OUT) ? sqrtf(t * (2 - t))
: (t < P5f) ? (1 - sqrtf(1 - 4 * t * t)) / 2 : (--t, (1 + sqrtf(1 - 4 * t * t)) / 2);
}
static float rp_elastic(const XFadeEasingContext *k, float t)
{
const int mode = k->eargs.e.mode;
float p, c;
if (mode == EASE_IN) return --t, cosf(20 * t * M_PIf / 3) * powf(2, 10 * t);
if (mode == EASE_OUT) return 1 - cosf(20 * t * M_PIf / 3) / powf(2, 10 * t);
p = 2 * t - 1, c = cosf(40 * p * M_PIf / 9) / 2, p = powf(2, 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) ? 2 * t * t * (2 * t * (b + 1) - b)
: 1 - 2 * r * r * (2 * r * (b + 1) - b);
}
static float rp_bounce(const XFadeEasingContext *k, float t)
{
const int mode = k->eargs.e.mode;
const float f = 121.f / 16.f;
float s;
if (mode == EASE_IN) t = 1 - t;
else if (mode == EASE_INOUT) s = (t < P5f) ? 1 : -1, t = s * (1 - 2 * t);
t = (t < 4.f / 11.f) ? f * t * t
: (t < 8.f / 11.f) ? f * powf(t - 6.f / 11.f, 2) + 3.f / 4.f
: (t < 10.f / 11.f) ? f * powf(t - 9.f / 11.f, 2) + 15.f / 16.f
: f * powf(t - 21.f / 22.f, 2) + 63.f / 64.f;
return (mode == EASE_IN) ? 1 - t
: (mode == EASE_INOUT) ? (1 - s * t) / 2
: t;
}
// supplementary easings
static float se_squareroot(const XFadeEasingContext *k, float t)
{
return power_ease(k, t, 0.5f);
}
static float se_cuberoot(const XFadeEasingContext *k, float t)
{
return power_ease(k, t, 1.f / 3.f);
}
// 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/
// https://cubic-bezier.com/
// 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_clip() av_clipf() in libavutil/common.h
// av_str*() in libavutil/avstring.h
// various in libavutil/mem.h libavutil/log.h
// FFDIFFSIGN in libavutil/macros.h
typedef struct {
int x, y;
} ivec2;
#define IVEC2(x, y) ((ivec2) { (x), (y) })
#define VEC2(x, y) ((vec2) { (x), (y) })
#define VEC4(a, b, c, d) ((vec4) {{ (a), (b), (c), (d) }})
#define sign(x) FFDIFFSIGN((x), 0)
static inline vec2 vec2i(ivec2 v) { return VEC2(v.x, v.y); }
static inline vec2 add2f(vec2 v, float f) { return VEC2(v.x + f, v.y + f); }
static inline vec2 sub2f(vec2 v, float f) { return VEC2(v.x - f, v.y - f); }
static inline vec2 mul2f(vec2 v, float f) { return VEC2(v.x * f, v.y * f); }
static inline vec2 div2f(vec2 v, float f) { return VEC2(v.x / f, v.y / 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 inv2(vec2 v) { return VEC2(1 / v.x, 1 / v.y); }
static inline vec2 floor2(vec2 v) { return VEC2(floorf(v.x), floorf(v.y)); }
static inline vec2 fract2(vec2 v) { return VEC2(fract(v.x), fract(v.y)); }
static inline float glmod(float x, float y) { return x - y * floorf(x / y); } // C fmod uses trunc
static inline vec2 mod2(vec2 v, float f) { return VEC2(glmod(v.x, f), glmod(v.y, f)); }
static inline vec2 abs2(vec2 v) { return VEC2(fabsf(v.x), fabsf(v.y)); }
static inline float asum(vec2 v) { return fabsf(v.x) + fabsf(v.y); }
static inline float atn2(vec2 v) { return atan2f(v.y, v.x); }
static inline vec2 sign2(vec2 v) { return VEC2(sign(v.x), sign(v.y)); }
static inline float length(vec2 v) { return hypotf(v.x, v.y); }
static inline vec2 normalize(vec2 v) { return div2f(v, length(v)); }
static inline float distance(vec2 p, vec2 q) { return length(sub2(p, q)); }
static inline float dot(vec2 p, vec2 q) { return p.x * q.x + p.y * q.y; }
static inline vec2 rot(vec2 v, float angle)
{
const float c = cosf(angle), s = sinf(angle);
return VEC2(v.x * c + v.y * s, v.y * c - v.x * s);
}
static inline vec2 flip2(vec2 v) { return VEC2(v.y, v.x); }
static inline int step(float edge, float x) { return (x < edge) ? 0 : 1; }
static inline float mixf(float a, float b, float m) { return mix(b, a, m); } // vf_xfade.c mix() args are swapped
static inline vec2 mix2(vec2 a, vec2 b, float m) { return VEC2(mix(b.x, a.x, m), mix(b.y, a.y, m)); }
static inline vec4 mix4(vec4 a, vec4 b, float m)
{
return VEC4(mix(b.p0, a.p0, m), mix(b.p1, a.p1, m), mix(b.p2, a.p2, m), mix(b.p3, a.p3, m));
}
static inline float degrees(float a) { return a * 180 / M_PIf; }
static inline float radians(float a) { return a / 180 * M_PIf; }
static inline float frandf(float x, float y)
{
return fract(sin(x * 12.9898 + y * 78.233) * 43758.5453); // doubles render like GL Transitions
}
static inline float frand2(vec2 v) { return frandf(v.x, v.y); }
static inline bool betweenf(float x, float min, float max)
{
return x >= min && x <= max;
}
static inline bool between2(vec2 v, float min, float max)
{
return v.x >= min && v.x <= max && v.y >= min && v.y <= max;
}
#define LINE(type, frame, plane, y) ((type*)(frame->data[plane] + (y) * frame->linesize[plane]))
#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/to colour at pixel point
static vec4 getColor(const XTransition *e, float x, float y, int nb) // cf. vf_xfade.c getpix()
{
const XFadeContext *s = e->s;
const AVFrame *f = s->xf[nb ^ s->reverse & REVERSE_TRANSITION];
const float max_value = s->max_value; // as float
const int n = s->nb_planes, d = (s->depth > 8), w = f->width, h = f->height;
const int i = av_clip(x * w, 0, w - 1), j = av_clip((1 - y) * h, 0, h - 1);
vec4 c;
int p;
for (p = 0; p < n; p++)
c.p[p] = (d ? LINE(uint16_t, f, p, j)[i] : LINE(uint8_t, f, p, j)[i]) / max_value;
for (; p < 4; p++)
c.p[p] = c.p[p - 1];
return c;
}
// convert packed RGBA to texture
static vec4 rgba(const XTransition *e, uint32_t rgba)
{
const uint8_t R = rgba >> 24, G = rgba >> 16, B = rgba >> 8, A = rgba;
uint8_t dst[] = { G, B, R };
if (!e->s->is_rgb) { // convert to digital YCbCr from analog RGB (8 bits) for ITU-R BT.601
#ifndef RGB2YUV_SWSSCALE
// convert using RGB to YPbPr(analogue) to YCbCr(digital)
// see https://en.wikipedia.org/wiki/YCbCr
const float Kr = 0.299f, Kg = 0.587f, Kb = 0.114f;
const float r = R / 255.f, g = G / 255.f, b = B / 255.f;
const float y = Kr * r + Kg * g + Kb * b, u = (b - y) / (1 - Kb) / 2, v = (r - y) / (1 - Kr) / 2;
dst[0] = roundf(16 + 219 * y); // derive digital from analogue
dst[1] = roundf(128 + 224 * u);
dst[2] = roundf(128 + 224 * v);
#else
// convert using ffmpeg
// see http://www.dranger.com/ffmpeg/tutorial08.html
const uint8_t src[] = { R, G, B };
const uint8_t *psrc[] = { src }; // planes
uint8_t *pdst[] = { dst, dst+1, dst+2 };
const int lsrc[] = { 3 }, ldst[] = { 1, 1, 1 }; // strides
struct SwsContext *ctx = sws_getContext(
1, 1, AV_PIX_FMT_RGB24,
1, 1, AV_PIX_FMT_YUV444P,
SWS_POINT, NULL, NULL, NULL);
if (!ctx) {
av_log(NULL, AV_LOG_ERROR, "create scale context failed for RGB-YUV conversion\n");
return (vec4){{0}};
}
sws_scale(ctx, psrc, lsrc, 0, 1, pdst, ldst);
sws_freeContext(ctx);
#endif
}
return VEC4(dst[0] / 255.f, dst[1] / 255.f, dst[2] / 255.f, A / 255.f);
}
// convert greyscale fraction to texture
static vec4 grey(const XTransition *e, float grey)
{
const float p12 = e->s->is_rgb ? grey : P5f;
return VEC4(grey, p12, p12, 1);
}
// convert black/white/transparent to texture
static vec4 bwt(const XTransition *e, int bwt)
{
const XFadeEasingContext *k = e->s->k;
return (bwt < 0) ? k->transparent : bwt ? k->white : k->black;
}
// convert any colour to texture
// 0 <= colour <= 1 considered greyscale else colour considered RGBA
static vec4 colour(const XTransition *e, double colour)
{
// if (isnan(colour) return e->s->k->transparent; // future feature
uint32_t c = colour; // lossless conversion for 4 bytes
return (c <= 1 && ceil(colour) <= 1) ? grey(e, colour) : rgba(e, c);
}
// simple slice caching of transition constants
#define INIT if (e->init)
#define INIT_BEGIN int argi = 0;
#define INIT_END INIT return (vec4){{0}};
#define ARG1(type, param, def) \
argi++; INIT arg(e, argi-1, #type, #param, def); \
const type param = e->data[argi-1];
#define ARG2(type, param, defx, defy) \
argi+=2; INIT arg(e, argi-2, #type, #param ".x", defx), arg(e, argi-1, #type, #param ".y", defy); \
const type param = (type) { e->data[argi-2], e->data[argi-1] };
#define ARG4(type, param, def) \
argi++; INIT arg(e, argi-1, #type, #param, def); \
const type param = colour(e, e->data[argi-1]);
#define VAR1(type, param, val) \
argi++; INIT var(e, argi-1, val); \
const type param = e->data[argi-1];
#define VAR2(type, param, valx, valy) \
argi+=2; INIT var(e, argi-2, valx), var(e, argi-1, valy); \
const type param = (type) { e->data[argi-2], e->data[argi-1] };
// set const variable value during initialisation
static inline void var(const XTransition *e, int argi, double value)
{
((double*)e->data)[argi] = value; // cast away const on mutable when initialising to keep const when not
}
// set parameter arg or default value during initialisation
static __attribute__ ((noinline)) void arg(
const XTransition *e,
int argi,
const char *type,
const char *param,
double value) // default
{
const XTransitionArgs *a = &e->s->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:
xe_debug(NULL, "param: %s %s = %g == %d(int) == 0x%X(unsigned)\n", type, param, value, (int)value, (unsigned)value);
var(e, argi, value); // double to store 32-bit (10-digit) precision colour values
}
// extended transitions
// GL transition names, algorithms, variable names & credits are replicated from the gl-transitions repo
static vec4 gl_angular(const XTransition *e) // by Fernando Kuteken
{
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_2PIf + P5f;
if (clockwise)
normalizedAngle *= -1;
normalizedAngle = fract(normalizedAngle);
return step(normalizedAngle, e->progress) ? e->b : e->a;
}
static vec4 gl_BookFlip(const XTransition *e) // by hong
{
INIT_END
bool pr = step(1 - e->progress, e->p.x);
vec4 colour;
if (e->p.x < P5f) {
if (!pr)
return e->a;
vec2 skewLeft = {
(e->p.x - P5f) / (e->progress - P5f) / 2 + P5f,
(e->p.y - P5f) / (P5f + (1 - e->progress) * (P5f - e->p.x) * 2) / 2 + P5f
};
colour = getToColor(skewLeft);
} else {
if (pr)
return e->b;
vec2 skewRight = {
(e->p.x - e->progress) / (P5f - e->progress) / 2,
(e->p.y - P5f) / (P5f + e->progress * (e->p.x - P5f) * 2) / 2 + P5f
};
colour = getFromColor(skewRight);
}
float shadeVal = fmaxf(0.7f, fabsf(e->progress - P5f) * 2);
colour.p0 *= shadeVal;
if (e->s->is_rgb)
colour.p1 *= shadeVal, colour.p2 *= shadeVal;
return colour;
}
static vec4 gl_Bounce(const XTransition *e) // by Adrian Purser
{
INIT_BEGIN
ARG1(float, shadowAlpha, 0.6)
ARG1(float, shadowHeight, 0.075)
ARG1(float, bounces, 3)
ARG1(int, direction, 0) // S,W,N,E
INIT_END
float phase = e->progress * M_PIf * bounces;
float p = fabsf(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 - p : 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, e->s->k->black, 1 - m);
}
static vec4 gl_BowTie(const XTransition *e) // by huynx
{
INIT_BEGIN
ARG1(bool, vertical, 0)
INIT_END
vec2 p = e->p, a = { P5f, P5f }, b = a, c = a;
if (vertical)
a.y = e->progress, b.x -= e->progress, c.x += e->progress, b.y = c.y = 0;
else
a.x = e->progress, b.y -= e->progress, c.y += e->progress, b.x = c.x = 0;
bool pass = 0;
do {
bool b1 = dot(VEC2(p.x - a.x, p.y - a.y), VEC2(c.y - a.y, a.x - c.x)) < 0,
b2 = dot(VEC2(p.x - b.x, p.y - b.y), VEC2(a.y - b.y, b.x - a.x)) < 0,
b3 = dot(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 = fabsf(dot(normalize(perpDir), dirToPt));
lineDir = sub2(c, a);
perpDir = VEC2(lineDir.y, -lineDir.x);
dirToPt = sub2(c, p);
float dist2 = fabsf(dot(normalize(perpDir), dirToPt));
float min_dist = fminf(dist1, dist2);
float m = (min_dist < 0.005f) ? min_dist / 0.005f : 1;
return mix4(e->a, e->b, m);
}
if (vertical)
a.y = 1 - a.y, b.y = c.y = 1;
else
a.x = 1 - a.x, b.x = c.x = 1;
} while (pass = !pass);
return e->a;
}
static vec4 gl_cannabisleaf(const XTransition *e) // by Flexi23
{
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, length(leaf_uv)) ? e->a : e->b;
}
static vec4 gl_CornerVanish(const XTransition *e) // by Mark Craig
{
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
{
INIT_BEGIN
ARG1(float, a, 4)
ARG1(float, b, 1)
ARG1(float, amplitude, 120)
ARG1(float, smoothness, 0.1)
INIT_END
float x = (a - b) * cosf(e->progress) + b * cosf(e->progress * ((a / b) - 1));
float y = (a - b) * sinf(e->progress) - b * sinf(e->progress * ((a / b) - 1));
vec2 dir = sub2f(e->p, P5f);
float z = e->progress * length(dir) * amplitude;
vec2 offset = {
dir.x * sinf(z * x) / smoothness,
dir.y * sinf(z * y) / smoothness
};
vec4 f = getFromColor(add2(e->p, offset));
return mix4(f, e->b, smoothstep(0.2f, 1, e->progress));
}
static vec4 gl_crosshatch(const XTransition *e) // by pthrasher
{
INIT_BEGIN
ARG2(vec2, center, 0.5, 0.5)
ARG1(float, threshold, 3)
ARG1(float, fadeEdge, 0.1)
INIT_END
float dist = distance(center, e->p) / threshold;
float r = e->progress - fminf(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, mixf(0, r, smoothstep(0, fadeEdge, e->progress)));
}
static vec4 gl_CrossOut(const XTransition *e) // by pthrasher
{
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 = fabsf((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
{
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_cube(const XTransition *e) // by gre
{
INIT_BEGIN
ARG1(float, persp, 0.7)
ARG1(float, unzoom, 0.3)
ARG1(float, reflection, 0.4)
ARG1(float, floating, 3)
ARG1(int, bgBkWhTr, 0)
INIT_END
float uz = unzoom * 2 * (P5f - fabsf(P5f - e->progress));
vec2 p = sub2f(mul2f(e->p, 1 + uz), uz / 2);
float persp2 = e->progress * (1 - persp);
vec2 fromP = {
(p.x - e->progress) / (1 - e->progress),
(p.y - persp2 * fromP.x / 2) / (1 - persp2 * fromP.x)
};
if (between2(fromP, 0, 1))
return getFromColor(fromP);
persp2 = 1 - mixf(e->progress * e->progress, 1, persp);
vec2 toP = {
p.x / e->progress,
(p.y - persp2 * (1 - toP.x) / 2) / (1 - persp2 * (1 - toP.x))
};
if (between2(toP, 0, 1))
return getToColor(toP);
vec4 back = bwt(e, bgBkWhTr), c = back;
fromP.y = fromP.y * -1.2f - floating / 100;
if (between2(fromP, 0, 1))
c = mix4(back, getFromColor(fromP), reflection * (1 - fromP.y));
toP.y = toP.y * -1.2f - floating / 100;
if (between2(toP, 0, 1))
c = mix4(back, getToColor(toP), reflection * (1 - toP.y));
return c;
}
static vec4 gl_Diamond(const XTransition *e) // by Mark Craig
{
INIT_BEGIN
ARG1(float, smoothness, 0.05)
INIT_END
float d = asum(sub2f(e->p, P5f));
if (d < e->progress)
return e->b;
return (d > e->progress + smoothness)
? e->a : mix4(e->b, e->a, (d - e->progress) / smoothness);
}
static vec4 gl_DirectionalScaled(const XTransition *e) // by Thibaut Foussard
{
INIT_BEGIN
ARG2(vec2, direction, 0, 1)
ARG1(float, scale, 0.7)
ARG1(int, bgBkWhTr, 0)
INIT_END
float easedProgress = powf(sinf(e->progress * M_PI_2f), 3);
vec2 p = add2(e->p, mul2f(sign2(direction), easedProgress));
float s = 1 - (1 - 1 / scale) * sinf(e->progress * M_PIf);
vec2 f = add2f(mul2f(sub2f(fract2(p), P5f), s), P5f);
if (between2(f, 0, 1))
return between2(p, 0, 1) ? getFromColor(f) : getToColor(f);
return bwt(e, bgBkWhTr);
}
static vec4 gl_directionalwarp(const XTransition *e) // by pschroen
{
INIT_BEGIN
ARG1(float, smoothness, 0.1)
ARG2(vec2, direction, -1, 1)
INIT_END
vec2 v = normalize(direction);
v = div2f(v, asum(v));
float d = (v.x + v.y) / 2;
float m = dot(e->p, v) - (d - P5f + e->progress * (1 + smoothness));
m = 1 - smoothstep(-smoothness, 0, m);
v = sub2f(e->p, P5f);
vec4 a = getFromColor(add2f(mul2f(v, 1 - m), P5f));
vec4 b = getToColor(add2f(mul2f(v, m), P5f));
return mix4(a, b, m);
}
static vec4 gl_doorway(const XTransition *e) // by gre
{
INIT_BEGIN
ARG1(float, reflection, 0.4)
ARG1(float, perspective, 0.4)
ARG1(float, depth, 3)
ARG1(int, bgBkWhTr, 0)
INIT_END
float middleSlit = 2 * fabsf(e->p.x - P5f) - e->progress;
if (middleSlit > 0) {
float d = 1 / (1 + perspective * e->progress * (1 - middleSlit));
vec2 pfr = {
e->p.x + (e->p.x > P5f ? -P5f : P5f) * e->progress,
(e->p.y + (1 - d) / 2) * d
};
if (between2(pfr, 0, 1))
return getFromColor(pfr);
}
float size = mixf(1, depth, 1 - e->progress);
vec2 pto = { (e->p.x - P5f) * size + P5f, (e->p.y - P5f) * size + P5f };
if (between2(pto, 0, 1))
return getToColor(pto);
vec4 c = bwt(e, bgBkWhTr);
pto.y = pto.y * -1.2f - 0.02f;
if (between2(pto, 0, 1))
c = mix4(c, getToColor(pto), reflection * (1 - pto.y));
return c;
}
static vec4 gl_DoubleDiamond(const XTransition *e) // by Mark Craig
{
INIT_BEGIN
ARG1(float, smoothness, 0.05)
INIT_END
float b1 = (1 - e->progress) / 2, b2 = 1 - b1;
float d = asum(sub2f(e->p, P5f));
if (betweenf(d, b1, b2)) {
if (betweenf(d, b1 + smoothness, b2 - smoothness))
return e->b;
return mix4(e->a, e->b, fminf(d - b1, b2 - d) / smoothness);
}
return e->a;
}
static vec4 gl_Dreamy(const XTransition *e) // by mikolalysenko
{
INIT_END
float shifty = 0.03f * e->progress * cosf(10 * (e->progress + e->p.x));
vec4 a = getFromColor(e->p.x, e->p.y + shifty);
shifty = 0.03f * (1 - e->progress) * cosf(10 * ((1 - e->progress) + e->p.x));
vec4 b = getToColor(e->p.x, e->p.y + shifty);
return mix4(a, b, e->progress);
}
static vec4 gl_Exponential_Swish(const XTransition *e) // by Boundless
{
INIT_BEGIN
ARG1(float, zoom, 0.8)
ARG1(float, angle, 0)
ARG2(vec2, offset, 0, 0)
ARG1(int, exponent, 4)
ARG2(ivec2, wrap, 2, 2)
ARG1(float, blur, 0) // changed from 0.5 which makes it extremely slow
ARG1(int, bgBkWhTr, 0)
VAR1(float, frames, e->s->k->duration * e->s->k->framerate)
VAR1(float, deg, radians(angle))
VAR1(float, ratio2, (e->ratio - 1) / 2)
INIT_END
const vec4 bgcolor = bwt(e, bgBkWhTr);
const int iters = 50; // TODO: experiment with this
const vec2 uv = sub2f(e->p, P5f);
vec4 comp = {{ 0 }};
for (int i = 0; i < iters; i++) {
float p = av_clipf(e->progress + (float)i * blur / frames / iters, 0, 1);
float pa0 = powf(2 * p, exponent), pa1 = powf(-2 * p + 2, exponent),
px0 = 1 - pa0 * fabsf(zoom), px1 = 1 - pa1 * fabsf(zoom),
px2 = 1 - pa0 * fmaxf(-zoom, 0), px3 = 1 - pa1 * fmaxf(zoom, 0);
vec2 uv0, uv1;
if (zoom > 0)
uv0 = mul2f(uv, px0), uv1 = div2f(uv, px1);
else if (zoom < 0)
uv0 = div2f(uv, px0), uv1 = mul2f(uv, px1);
else
uv0 = uv1 = uv;
uv0 = sub2(add2f(uv0, P5f), mul2f(offset, pa0 / px2));
uv0.x = uv0.x * e->ratio - ratio2;
uv0 = add2f(rot(sub2f(uv0, P5f), -deg * pa0), P5f);
uv0.x = (uv0.x + ratio2) / e->ratio;
uv1 = add2(add2f(uv1, P5f), mul2f(offset, pa1 / px3));
uv1.x = uv1.x * e->ratio - ratio2;
uv1 = add2f(rot(sub2f(uv1, P5f), deg * pa1), P5f);
uv1.x = (uv1.x + ratio2) / e->ratio;
if (wrap.x == 2)
uv0.x = acosf(cosf(M_PIf * uv0.x)) * M_1_PIf, uv1.x = acosf(cosf(M_PIf * uv1.x)) * M_1_PIf;
else if (wrap.x == 1)
uv0.x = fract(uv0.x), uv1.x = fract(uv1.x);
if (wrap.y == 2)
uv0.y = acosf(cosf(M_PIf * uv0.y)) * M_1_PIf, uv1.y = acosf(cosf(M_PIf * uv1.y)) * M_1_PIf;
else if (wrap.y == 1)
uv0.y = fract(uv0.y), uv1.y = fract(uv1.y);
bool b = (p < P5f);
vec4 c = !wrap.x && (b && !betweenf(uv0.x, 0, 1) || !b && !betweenf(uv1.x, 0, 1)) ||
!wrap.y && (b && !betweenf(uv0.y, 0, 1) || !b && !betweenf(uv1.y, 0, 1))
? bgcolor : b ? getFromColor(uv0) : getToColor(uv1);
if (blur == 0)
return c;
comp.p0 += c.p0 / iters;
if (e->s->is_rgb)
comp.p1 += c.p1 / iters, comp.p2 += c.p2 / iters;
else
comp.p1 = comp.p1, comp.p2 = c.p2;
comp.p3 = c.p3;
}
return comp;
}
static vec4 gl_FanIn(const XTransition *e) // by Mark Craig
{
INIT_BEGIN
ARG1(float, smoothness, 0.05)
INIT_END
float theta = M_PIf * e->progress;
float d = atan2f(fabsf(e->p.x - P5f), (e->p.y < P5f) ? 0.25f - e->p.y : e->p.y - 0.75f) - theta;
if (d < 0)
return e->b;
return (d < smoothness) ? mix4(e->b, e->a, d / smoothness) : e->a;
}
static vec4 gl_FanOut(const XTransition *e) // by Mark Craig
{
INIT_BEGIN
ARG1(float, smoothness, 0.05)
INIT_END
float theta = M_2PIf * e->progress;
float d = M_PIf + atan2f(P5f - e->p.y, (e->p.x < P5f) ? 0.25f - e->p.x : e->p.x - 0.75f) - theta;
if (d < 0)
return e->b;
return (d < smoothness) ? mix4(e->b, e->a, d / smoothness) : e->a;
}
static vec4 gl_FanUp(const XTransition *e) // by Mark Craig
{
INIT_BEGIN
ARG1(float, smoothness, 0.05)
INIT_END
float theta = M_PI_2f * e->progress;
float d = atan2f(fabsf(e->p.x - P5f), 1 - e->p.y) - theta;
if (d < 0)
return e->b;
return (d < smoothness) ? mix4(e->b, e->a, d / smoothness) : e->a;
}
static vec4 gl_Flower(const XTransition *e) // by Mark Craig
{
INIT_BEGIN
ARG1(float, smoothness, 0.05)
ARG1(float, rotation, 360)
float h, r;
vec2 v;
INIT {
r = radians(162);
v = VEC2(cosf(r), sinf(r) - 1);
h = dot(v, v);
r = radians(234);
v = VEC2(cosf(r), sinf(r) - 1);
h -= dot(v, v) / 4;
}
VAR1(float, ang, radians(36))
VAR1(float, fang, (1 - sqrtf(h)) / cosf(ang))
INIT_END
v = VEC2((e->p.x - P5f) * e->ratio, P5f - e->p.y);
float theta = radians(e->progress * rotation);
float theta1 = atan2f(v.x, v.y) + theta;
float theta2 = glmod(fabsf(theta1), ang);
float ro = e->ratio / 0.731f * e->progress;
float ri = ro * fang;
if (glmod(truncf(theta1 / ang), 2) == 0)
r = theta2 / ang * (ro - ri) + ri;
else
r = (1 - theta2 / ang) * (ro - ri) + ri;
float r2 = length(v);
if (r2 > r + smoothness)
return e->a;
if (r2 > r)
return mix4(e->b, e->a, (r2 - r) / smoothness);
return e->b;
}
static vec4 gl_GridFlip(const XTransition *e) // by TimDonselaar
{
INIT_BEGIN
ARG2(ivec2, size, 4, 4)
ARG1(float, pause, 0.1)
ARG1(float, dividerWidth, 0.05)
ARG1(float, randomness, 0.1)
ARG1(int, bgBkWhTr, 0)
INIT_END
const vec4 bgcolor = bwt(e, bgBkWhTr);
const vec2 rectangleSize = inv2(vec2i(size));
const vec2 rectanglePos = floor2(mul2(vec2i(size), e->p));
float top = rectangleSize.y * (rectanglePos.y + 1),
bottom = rectangleSize.y * rectanglePos.y,
minY = fminf(fabsf(e->p.y - top), fabsf(e->p.y - bottom)),
left = rectangleSize.x * rectanglePos.x,
right = rectangleSize.x * (rectanglePos.x + 1),
minX = fminf(fabsf(e->p.x - left), fabsf(e->p.x - right));
float dividerSize = fminf(rectangleSize.x, rectangleSize.y) * dividerWidth;
bool individer = fminf(minX, minY) < dividerSize;
if (e->progress < pause)
return mix4(bgcolor, e->a, individer ? 1 - e->progress / pause : 1);
if (1 - e->progress < pause)
return mix4(bgcolor, e->b, individer ? 1 - (1 - e->progress) / pause : 1);
if (individer)
return bgcolor;
float r = frand2(rectanglePos) - randomness;
float cp = smoothstep(0, 1 - r, (e->progress - pause) / (1 - pause * 2));
float offset = rectangleSize.x / 2 + left;
vec2 p = { (e->p.x - offset) / fabsf(cp - P5f) / 2 + offset, e->p.y };
float s = step(fabsf(size.x * (e->p.x - left) - P5f), fabsf(cp - P5f));
return mix4(bgcolor, mix4(getToColor(p), getFromColor(p), step(cp, P5f)), s);
}
static vec4 gl_heart(const XTransition *e) // by gre
{
INIT_END
if (e->progress == 0)