]> git.sesse.net Git - ffmpeg/blob - libavfilter/vf_xfade.c
avfilter/vf_xfade: add fadegrays transition
[ffmpeg] / libavfilter / vf_xfade.c
1 /*
2  * Copyright (c) 2020 Paul B Mahol
3  *
4  * This file is part of FFmpeg.
5  *
6  * FFmpeg is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * FFmpeg is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with FFmpeg; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20
21 #include "libavutil/imgutils.h"
22 #include "libavutil/eval.h"
23 #include "libavutil/opt.h"
24 #include "libavutil/pixfmt.h"
25 #include "avfilter.h"
26 #include "formats.h"
27 #include "internal.h"
28 #include "filters.h"
29 #include "video.h"
30
31 enum XFadeTransitions {
32     CUSTOM = -1,
33     FADE,
34     WIPELEFT,
35     WIPERIGHT,
36     WIPEUP,
37     WIPEDOWN,
38     SLIDELEFT,
39     SLIDERIGHT,
40     SLIDEUP,
41     SLIDEDOWN,
42     CIRCLECROP,
43     RECTCROP,
44     DISTANCE,
45     FADEBLACK,
46     FADEWHITE,
47     RADIAL,
48     SMOOTHLEFT,
49     SMOOTHRIGHT,
50     SMOOTHUP,
51     SMOOTHDOWN,
52     CIRCLEOPEN,
53     CIRCLECLOSE,
54     VERTOPEN,
55     VERTCLOSE,
56     HORZOPEN,
57     HORZCLOSE,
58     DISSOLVE,
59     PIXELIZE,
60     DIAGTL,
61     DIAGTR,
62     DIAGBL,
63     DIAGBR,
64     HLSLICE,
65     HRSLICE,
66     VUSLICE,
67     VDSLICE,
68     HBLUR,
69     FADEGRAYS,
70     NB_TRANSITIONS,
71 };
72
73 typedef struct XFadeContext {
74     const AVClass *class;
75
76     int     transition;
77     int64_t duration;
78     int64_t offset;
79     char   *custom_str;
80
81     int nb_planes;
82     int depth;
83     int is_rgb;
84
85     int64_t duration_pts;
86     int64_t offset_pts;
87     int64_t first_pts;
88     int64_t last_pts;
89     int64_t pts;
90     int xfade_is_over;
91     int need_second;
92     int eof[2];
93     AVFrame *xf[2];
94     int max_value;
95     uint16_t black[4];
96     uint16_t white[4];
97
98     void (*transitionf)(AVFilterContext *ctx, const AVFrame *a, const AVFrame *b, AVFrame *out, float progress,
99                         int slice_start, int slice_end, int jobnr);
100
101     AVExpr *e;
102 } XFadeContext;
103
104 static const char *const var_names[] = {   "X",   "Y",   "W",   "H",   "A",   "B",   "PLANE",          "P",        NULL };
105 enum                                   { VAR_X, VAR_Y, VAR_W, VAR_H, VAR_A, VAR_B, VAR_PLANE, VAR_PROGRESS, VAR_VARS_NB };
106
107 typedef struct ThreadData {
108     const AVFrame *xf[2];
109     AVFrame *out;
110     float progress;
111 } ThreadData;
112
113 static int query_formats(AVFilterContext *ctx)
114 {
115     static const enum AVPixelFormat pix_fmts[] = {
116         AV_PIX_FMT_YUVA444P,
117         AV_PIX_FMT_YUVJ444P,
118         AV_PIX_FMT_YUV444P,
119         AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRAP, AV_PIX_FMT_GRAY8,
120         AV_PIX_FMT_YUVA444P9, AV_PIX_FMT_GBRP9,
121         AV_PIX_FMT_YUV444P10,
122         AV_PIX_FMT_YUVA444P10,
123         AV_PIX_FMT_GBRP10, AV_PIX_FMT_GBRAP10, AV_PIX_FMT_GRAY10,
124         AV_PIX_FMT_YUV444P12,
125         AV_PIX_FMT_YUVA444P12,
126         AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRAP12, AV_PIX_FMT_GRAY12,
127         AV_PIX_FMT_YUV444P14, AV_PIX_FMT_GBRP14,
128         AV_PIX_FMT_YUV444P16,
129         AV_PIX_FMT_YUVA444P16,
130         AV_PIX_FMT_GBRP16, AV_PIX_FMT_GBRAP16, AV_PIX_FMT_GRAY16,
131         AV_PIX_FMT_NONE
132     };
133
134     AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
135     if (!fmts_list)
136         return AVERROR(ENOMEM);
137     return ff_set_common_formats(ctx, fmts_list);
138 }
139
140 static av_cold void uninit(AVFilterContext *ctx)
141 {
142     XFadeContext *s = ctx->priv;
143
144     av_expr_free(s->e);
145 }
146
147 #define OFFSET(x) offsetof(XFadeContext, x)
148 #define FLAGS (AV_OPT_FLAG_FILTERING_PARAM | AV_OPT_FLAG_VIDEO_PARAM)
149
150 static const AVOption xfade_options[] = {
151     { "transition", "set cross fade transition", OFFSET(transition), AV_OPT_TYPE_INT, {.i64=FADE}, -1, NB_TRANSITIONS-1, FLAGS, "transition" },
152     {   "custom",    "custom transition",     0, AV_OPT_TYPE_CONST, {.i64=CUSTOM},    0, 0, FLAGS, "transition" },
153     {   "fade",      "fade transition",       0, AV_OPT_TYPE_CONST, {.i64=FADE},      0, 0, FLAGS, "transition" },
154     {   "wipeleft",  "wipe left transition",  0, AV_OPT_TYPE_CONST, {.i64=WIPELEFT},  0, 0, FLAGS, "transition" },
155     {   "wiperight", "wipe right transition", 0, AV_OPT_TYPE_CONST, {.i64=WIPERIGHT}, 0, 0, FLAGS, "transition" },
156     {   "wipeup",    "wipe up transition",    0, AV_OPT_TYPE_CONST, {.i64=WIPEUP},    0, 0, FLAGS, "transition" },
157     {   "wipedown",  "wipe down transition",  0, AV_OPT_TYPE_CONST, {.i64=WIPEDOWN},  0, 0, FLAGS, "transition" },
158     {   "slideleft",  "slide left transition",  0, AV_OPT_TYPE_CONST, {.i64=SLIDELEFT},  0, 0, FLAGS, "transition" },
159     {   "slideright", "slide right transition", 0, AV_OPT_TYPE_CONST, {.i64=SLIDERIGHT}, 0, 0, FLAGS, "transition" },
160     {   "slideup",    "slide up transition",    0, AV_OPT_TYPE_CONST, {.i64=SLIDEUP},    0, 0, FLAGS, "transition" },
161     {   "slidedown",  "slide down transition",  0, AV_OPT_TYPE_CONST, {.i64=SLIDEDOWN},  0, 0, FLAGS, "transition" },
162     {   "circlecrop", "circle crop transition", 0, AV_OPT_TYPE_CONST, {.i64=CIRCLECROP}, 0, 0, FLAGS, "transition" },
163     {   "rectcrop",   "rect crop transition",   0, AV_OPT_TYPE_CONST, {.i64=RECTCROP},   0, 0, FLAGS, "transition" },
164     {   "distance",   "distance transition",    0, AV_OPT_TYPE_CONST, {.i64=DISTANCE},   0, 0, FLAGS, "transition" },
165     {   "fadeblack",  "fadeblack transition",   0, AV_OPT_TYPE_CONST, {.i64=FADEBLACK},  0, 0, FLAGS, "transition" },
166     {   "fadewhite",  "fadewhite transition",   0, AV_OPT_TYPE_CONST, {.i64=FADEWHITE},  0, 0, FLAGS, "transition" },
167     {   "radial",     "radial transition",      0, AV_OPT_TYPE_CONST, {.i64=RADIAL},     0, 0, FLAGS, "transition" },
168     {   "smoothleft", "smoothleft transition",  0, AV_OPT_TYPE_CONST, {.i64=SMOOTHLEFT}, 0, 0, FLAGS, "transition" },
169     {   "smoothright","smoothright transition", 0, AV_OPT_TYPE_CONST, {.i64=SMOOTHRIGHT},0, 0, FLAGS, "transition" },
170     {   "smoothup",   "smoothup transition",    0, AV_OPT_TYPE_CONST, {.i64=SMOOTHUP},   0, 0, FLAGS, "transition" },
171     {   "smoothdown", "smoothdown transition",  0, AV_OPT_TYPE_CONST, {.i64=SMOOTHDOWN}, 0, 0, FLAGS, "transition" },
172     {   "circleopen", "circleopen transition",  0, AV_OPT_TYPE_CONST, {.i64=CIRCLEOPEN}, 0, 0, FLAGS, "transition" },
173     {   "circleclose","circleclose transition", 0, AV_OPT_TYPE_CONST, {.i64=CIRCLECLOSE},0, 0, FLAGS, "transition" },
174     {   "vertopen",   "vert open transition",   0, AV_OPT_TYPE_CONST, {.i64=VERTOPEN},   0, 0, FLAGS, "transition" },
175     {   "vertclose",  "vert close transition",  0, AV_OPT_TYPE_CONST, {.i64=VERTCLOSE},  0, 0, FLAGS, "transition" },
176     {   "horzopen",   "horz open transition",   0, AV_OPT_TYPE_CONST, {.i64=HORZOPEN},   0, 0, FLAGS, "transition" },
177     {   "horzclose",  "horz close transition",  0, AV_OPT_TYPE_CONST, {.i64=HORZCLOSE},  0, 0, FLAGS, "transition" },
178     {   "dissolve",   "dissolve transition",    0, AV_OPT_TYPE_CONST, {.i64=DISSOLVE},   0, 0, FLAGS, "transition" },
179     {   "pixelize",   "pixelize transition",    0, AV_OPT_TYPE_CONST, {.i64=PIXELIZE},   0, 0, FLAGS, "transition" },
180     {   "diagtl",     "diag tl transition",     0, AV_OPT_TYPE_CONST, {.i64=DIAGTL},     0, 0, FLAGS, "transition" },
181     {   "diagtr",     "diag tr transition",     0, AV_OPT_TYPE_CONST, {.i64=DIAGTR},     0, 0, FLAGS, "transition" },
182     {   "diagbl",     "diag bl transition",     0, AV_OPT_TYPE_CONST, {.i64=DIAGBL},     0, 0, FLAGS, "transition" },
183     {   "diagbr",     "diag br transition",     0, AV_OPT_TYPE_CONST, {.i64=DIAGBR},     0, 0, FLAGS, "transition" },
184     {   "hlslice",    "hl slice transition",    0, AV_OPT_TYPE_CONST, {.i64=HLSLICE},    0, 0, FLAGS, "transition" },
185     {   "hrslice",    "hr slice transition",    0, AV_OPT_TYPE_CONST, {.i64=HRSLICE},    0, 0, FLAGS, "transition" },
186     {   "vuslice",    "vu slice transition",    0, AV_OPT_TYPE_CONST, {.i64=VUSLICE},    0, 0, FLAGS, "transition" },
187     {   "vdslice",    "vd slice transition",    0, AV_OPT_TYPE_CONST, {.i64=VDSLICE},    0, 0, FLAGS, "transition" },
188     {   "hblur",      "hblur transition",       0, AV_OPT_TYPE_CONST, {.i64=HBLUR},      0, 0, FLAGS, "transition" },
189     {   "fadegrays",  "fadegrays transition",   0, AV_OPT_TYPE_CONST, {.i64=FADEGRAYS},  0, 0, FLAGS, "transition" },
190     { "duration", "set cross fade duration", OFFSET(duration), AV_OPT_TYPE_DURATION, {.i64=1000000}, 0, 60000000, FLAGS },
191     { "offset",   "set cross fade start relative to first input stream", OFFSET(offset), AV_OPT_TYPE_DURATION, {.i64=0}, INT64_MIN, INT64_MAX, FLAGS },
192     { "expr",   "set expression for custom transition", OFFSET(custom_str), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, FLAGS },
193     { NULL }
194 };
195
196 AVFILTER_DEFINE_CLASS(xfade);
197
198 #define CUSTOM_TRANSITION(name, type, div)                                           \
199 static void custom##name##_transition(AVFilterContext *ctx,                          \
200                             const AVFrame *a, const AVFrame *b, AVFrame *out,        \
201                             float progress,                                          \
202                             int slice_start, int slice_end, int jobnr)               \
203 {                                                                                    \
204     XFadeContext *s = ctx->priv;                                                     \
205     const int height = slice_end - slice_start;                                      \
206                                                                                      \
207     double values[VAR_VARS_NB];                                                      \
208     values[VAR_W] = out->width;                                                      \
209     values[VAR_H] = out->height;                                                     \
210     values[VAR_PROGRESS] = progress;                                                 \
211                                                                                      \
212     for (int p = 0; p < s->nb_planes; p++) {                                         \
213         const type *xf0 = (const type *)(a->data[p] + slice_start * a->linesize[p]); \
214         const type *xf1 = (const type *)(b->data[p] + slice_start * b->linesize[p]); \
215         type *dst = (type *)(out->data[p] + slice_start * out->linesize[p]);         \
216                                                                                      \
217         values[VAR_PLANE] = p;                                                       \
218                                                                                      \
219         for (int y = 0; y < height; y++) {                                           \
220             values[VAR_Y] = slice_start + y;                                         \
221             for (int x = 0; x < out->width; x++) {                                   \
222                 values[VAR_X] = x;                                                   \
223                 values[VAR_A] = xf0[x];                                              \
224                 values[VAR_B] = xf1[x];                                              \
225                 dst[x] = av_expr_eval(s->e, values, s);                              \
226             }                                                                        \
227                                                                                      \
228             dst += out->linesize[p] / div;                                           \
229             xf0 += a->linesize[p] / div;                                             \
230             xf1 += b->linesize[p] / div;                                             \
231         }                                                                            \
232     }                                                                                \
233 }
234
235 CUSTOM_TRANSITION(8, uint8_t, 1)
236 CUSTOM_TRANSITION(16, uint16_t, 2)
237
238 static inline float mix(float a, float b, float mix)
239 {
240     return a * mix + b * (1.f - mix);
241 }
242
243 static inline float fract(float a)
244 {
245     return a - floorf(a);
246 }
247
248 static inline float smoothstep(float edge0, float edge1, float x)
249 {
250     float t;
251
252     t = av_clipf((x - edge0) / (edge1 - edge0), 0.f, 1.f);
253
254     return t * t * (3.f - 2.f * t);
255 }
256
257 #define FADE_TRANSITION(name, type, div)                                             \
258 static void fade##name##_transition(AVFilterContext *ctx,                            \
259                             const AVFrame *a, const AVFrame *b, AVFrame *out,        \
260                             float progress,                                          \
261                             int slice_start, int slice_end, int jobnr)               \
262 {                                                                                    \
263     XFadeContext *s = ctx->priv;                                                     \
264     const int height = slice_end - slice_start;                                      \
265                                                                                      \
266     for (int p = 0; p < s->nb_planes; p++) {                                         \
267         const type *xf0 = (const type *)(a->data[p] + slice_start * a->linesize[p]); \
268         const type *xf1 = (const type *)(b->data[p] + slice_start * b->linesize[p]); \
269         type *dst = (type *)(out->data[p] + slice_start * out->linesize[p]);         \
270                                                                                      \
271         for (int y = 0; y < height; y++) {                                           \
272             for (int x = 0; x < out->width; x++) {                                   \
273                 dst[x] = mix(xf0[x], xf1[x], progress);                              \
274             }                                                                        \
275                                                                                      \
276             dst += out->linesize[p] / div;                                           \
277             xf0 += a->linesize[p] / div;                                             \
278             xf1 += b->linesize[p] / div;                                             \
279         }                                                                            \
280     }                                                                                \
281 }
282
283 FADE_TRANSITION(8, uint8_t, 1)
284 FADE_TRANSITION(16, uint16_t, 2)
285
286 #define WIPELEFT_TRANSITION(name, type, div)                                         \
287 static void wipeleft##name##_transition(AVFilterContext *ctx,                        \
288                                 const AVFrame *a, const AVFrame *b, AVFrame *out,    \
289                                 float progress,                                      \
290                                 int slice_start, int slice_end, int jobnr)           \
291 {                                                                                    \
292     XFadeContext *s = ctx->priv;                                                     \
293     const int height = slice_end - slice_start;                                      \
294     const int z = out->width * progress;                                             \
295                                                                                      \
296     for (int p = 0; p < s->nb_planes; p++) {                                         \
297         const type *xf0 = (const type *)(a->data[p] + slice_start * a->linesize[p]); \
298         const type *xf1 = (const type *)(b->data[p] + slice_start * b->linesize[p]); \
299         type *dst = (type *)(out->data[p] + slice_start * out->linesize[p]);         \
300                                                                                      \
301         for (int y = 0; y < height; y++) {                                           \
302             for (int x = 0; x < out->width; x++) {                                   \
303                 dst[x] = x > z ? xf1[x] : xf0[x];                                    \
304             }                                                                        \
305                                                                                      \
306             dst += out->linesize[p] / div;                                           \
307             xf0 += a->linesize[p] / div;                                             \
308             xf1 += b->linesize[p] / div;                                             \
309         }                                                                            \
310     }                                                                                \
311 }
312
313 WIPELEFT_TRANSITION(8, uint8_t, 1)
314 WIPELEFT_TRANSITION(16, uint16_t, 2)
315
316 #define WIPERIGHT_TRANSITION(name, type, div)                                        \
317 static void wiperight##name##_transition(AVFilterContext *ctx,                       \
318                                  const AVFrame *a, const AVFrame *b, AVFrame *out,   \
319                                  float progress,                                     \
320                                  int slice_start, int slice_end, int jobnr)          \
321 {                                                                                    \
322     XFadeContext *s = ctx->priv;                                                     \
323     const int height = slice_end - slice_start;                                      \
324     const int z = out->width * (1.f - progress);                                     \
325                                                                                      \
326     for (int p = 0; p < s->nb_planes; p++) {                                         \
327         const type *xf0 = (const type *)(a->data[p] + slice_start * a->linesize[p]); \
328         const type *xf1 = (const type *)(b->data[p] + slice_start * b->linesize[p]); \
329         type *dst = (type *)(out->data[p] + slice_start * out->linesize[p]);         \
330                                                                                      \
331         for (int y = 0; y < height; y++) {                                           \
332             for (int x = 0; x < out->width; x++) {                                   \
333                 dst[x] = x > z ? xf0[x] : xf1[x];                                    \
334             }                                                                        \
335                                                                                      \
336             dst += out->linesize[p] / div;                                           \
337             xf0 += a->linesize[p] / div;                                             \
338             xf1 += b->linesize[p] / div;                                             \
339         }                                                                            \
340     }                                                                                \
341 }
342
343 WIPERIGHT_TRANSITION(8, uint8_t, 1)
344 WIPERIGHT_TRANSITION(16, uint16_t, 2)
345
346 #define WIPEUP_TRANSITION(name, type, div)                                           \
347 static void wipeup##name##_transition(AVFilterContext *ctx,                          \
348                               const AVFrame *a, const AVFrame *b, AVFrame *out,      \
349                               float progress,                                        \
350                               int slice_start, int slice_end, int jobnr)             \
351 {                                                                                    \
352     XFadeContext *s = ctx->priv;                                                     \
353     const int height = slice_end - slice_start;                                      \
354     const int z = out->height * progress;                                            \
355                                                                                      \
356     for (int p = 0; p < s->nb_planes; p++) {                                         \
357         const type *xf0 = (const type *)(a->data[p] + slice_start * a->linesize[p]); \
358         const type *xf1 = (const type *)(b->data[p] + slice_start * b->linesize[p]); \
359         type *dst = (type *)(out->data[p] + slice_start * out->linesize[p]);         \
360                                                                                      \
361         for (int y = 0; y < height; y++) {                                           \
362             for (int x = 0; x < out->width; x++) {                                   \
363                 dst[x] = slice_start + y > z ? xf1[x] : xf0[x];                      \
364             }                                                                        \
365                                                                                      \
366             dst += out->linesize[p] / div;                                           \
367             xf0 += a->linesize[p] / div;                                             \
368             xf1 += b->linesize[p] / div;                                             \
369         }                                                                            \
370     }                                                                                \
371 }
372
373 WIPEUP_TRANSITION(8, uint8_t, 1)
374 WIPEUP_TRANSITION(16, uint16_t, 2)
375
376 #define WIPEDOWN_TRANSITION(name, type, div)                                         \
377 static void wipedown##name##_transition(AVFilterContext *ctx,                        \
378                                 const AVFrame *a, const AVFrame *b, AVFrame *out,    \
379                                 float progress,                                      \
380                                 int slice_start, int slice_end, int jobnr)           \
381 {                                                                                    \
382     XFadeContext *s = ctx->priv;                                                     \
383     const int height = slice_end - slice_start;                                      \
384     const int z = out->height * (1.f - progress);                                    \
385                                                                                      \
386     for (int p = 0; p < s->nb_planes; p++) {                                         \
387         const type *xf0 = (const type *)(a->data[p] + slice_start * a->linesize[p]); \
388         const type *xf1 = (const type *)(b->data[p] + slice_start * b->linesize[p]); \
389         type *dst = (type *)(out->data[p] + slice_start * out->linesize[p]);         \
390                                                                                      \
391         for (int y = 0; y < height; y++) {                                           \
392             for (int x = 0; x < out->width; x++) {                                   \
393                 dst[x] = slice_start + y > z ? xf0[x] : xf1[x];                      \
394             }                                                                        \
395                                                                                      \
396             dst += out->linesize[p] / div;                                           \
397             xf0 += a->linesize[p] / div;                                             \
398             xf1 += b->linesize[p] / div;                                             \
399         }                                                                            \
400     }                                                                                \
401 }
402
403 WIPEDOWN_TRANSITION(8, uint8_t, 1)
404 WIPEDOWN_TRANSITION(16, uint16_t, 2)
405
406 #define SLIDELEFT_TRANSITION(name, type, div)                                        \
407 static void slideleft##name##_transition(AVFilterContext *ctx,                       \
408                                  const AVFrame *a, const AVFrame *b, AVFrame *out,   \
409                                  float progress,                                     \
410                                  int slice_start, int slice_end, int jobnr)          \
411 {                                                                                    \
412     XFadeContext *s = ctx->priv;                                                     \
413     const int height = slice_end - slice_start;                                      \
414     const int width = out->width;                                                    \
415     const int z = -progress * width;                                                 \
416                                                                                      \
417     for (int p = 0; p < s->nb_planes; p++) {                                         \
418         const type *xf0 = (const type *)(a->data[p] + slice_start * a->linesize[p]); \
419         const type *xf1 = (const type *)(b->data[p] + slice_start * b->linesize[p]); \
420         type *dst = (type *)(out->data[p] + slice_start * out->linesize[p]);         \
421                                                                                      \
422         for (int y = 0; y < height; y++) {                                           \
423             for (int x = 0; x < width; x++) {                                        \
424                 const int zx = z + x;                                                \
425                 const int zz = zx % width + width * (zx < 0);                        \
426                 dst[x] = (zx > 0) && (zx < width) ? xf1[zz] : xf0[zz];               \
427             }                                                                        \
428                                                                                      \
429             dst += out->linesize[p] / div;                                           \
430             xf0 += a->linesize[p] / div;                                             \
431             xf1 += b->linesize[p] / div;                                             \
432         }                                                                            \
433     }                                                                                \
434 }
435
436 SLIDELEFT_TRANSITION(8, uint8_t, 1)
437 SLIDELEFT_TRANSITION(16, uint16_t, 2)
438
439 #define SLIDERIGHT_TRANSITION(name, type, div)                                       \
440 static void slideright##name##_transition(AVFilterContext *ctx,                      \
441                                   const AVFrame *a, const AVFrame *b, AVFrame *out,  \
442                                   float progress,                                    \
443                                   int slice_start, int slice_end, int jobnr)         \
444 {                                                                                    \
445     XFadeContext *s = ctx->priv;                                                     \
446     const int height = slice_end - slice_start;                                      \
447     const int width = out->width;                                                    \
448     const int z = progress * width;                                                  \
449                                                                                      \
450     for (int p = 0; p < s->nb_planes; p++) {                                         \
451         const type *xf0 = (const type *)(a->data[p] + slice_start * a->linesize[p]); \
452         const type *xf1 = (const type *)(b->data[p] + slice_start * b->linesize[p]); \
453         type *dst = (type *)(out->data[p] + slice_start * out->linesize[p]);         \
454                                                                                      \
455         for (int y = 0; y < height; y++) {                                           \
456             for (int x = 0; x < out->width; x++) {                                   \
457                 const int zx = z + x;                                                \
458                 const int zz = zx % width + width * (zx < 0);                        \
459                 dst[x] = (zx > 0) && (zx < width) ? xf1[zz] : xf0[zz];               \
460             }                                                                        \
461                                                                                      \
462             dst += out->linesize[p] / div;                                           \
463             xf0 += a->linesize[p] / div;                                             \
464             xf1 += b->linesize[p] / div;                                             \
465         }                                                                            \
466     }                                                                                \
467 }
468
469 SLIDERIGHT_TRANSITION(8, uint8_t, 1)
470 SLIDERIGHT_TRANSITION(16, uint16_t, 2)
471
472 #define SLIDEUP_TRANSITION(name, type, div)                                         \
473 static void slideup##name##_transition(AVFilterContext *ctx,                        \
474                                const AVFrame *a, const AVFrame *b, AVFrame *out,    \
475                                float progress,                                      \
476                                int slice_start, int slice_end, int jobnr)           \
477 {                                                                                   \
478     XFadeContext *s = ctx->priv;                                                    \
479     const int height = out->height;                                                 \
480     const int z = -progress * height;                                               \
481                                                                                     \
482     for (int p = 0; p < s->nb_planes; p++) {                                        \
483         type *dst = (type *)(out->data[p] + slice_start * out->linesize[p]);        \
484                                                                                     \
485         for (int y = slice_start; y < slice_end; y++) {                             \
486             const int zy = z + y;                                                   \
487             const int zz = zy % height + height * (zy < 0);                         \
488             const type *xf0 = (const type *)(a->data[p] + zz * a->linesize[p]);     \
489             const type *xf1 = (const type *)(b->data[p] + zz * b->linesize[p]);     \
490                                                                                     \
491             for (int x = 0; x < out->width; x++) {                                  \
492                 dst[x] = (zy > 0) && (zy < height) ? xf1[x] : xf0[x];               \
493             }                                                                       \
494                                                                                     \
495             dst += out->linesize[p] / div;                                          \
496         }                                                                           \
497     }                                                                               \
498 }
499
500 SLIDEUP_TRANSITION(8, uint8_t, 1)
501 SLIDEUP_TRANSITION(16, uint16_t, 2)
502
503 #define SLIDEDOWN_TRANSITION(name, type, div)                                       \
504 static void slidedown##name##_transition(AVFilterContext *ctx,                      \
505                                  const AVFrame *a, const AVFrame *b, AVFrame *out,  \
506                                  float progress,                                    \
507                                  int slice_start, int slice_end, int jobnr)         \
508 {                                                                                   \
509     XFadeContext *s = ctx->priv;                                                    \
510     const int height = out->height;                                                 \
511     const int z = progress * height;                                                \
512                                                                                     \
513     for (int p = 0; p < s->nb_planes; p++) {                                        \
514         type *dst = (type *)(out->data[p] + slice_start * out->linesize[p]);        \
515                                                                                     \
516         for (int y = slice_start; y < slice_end; y++) {                             \
517             const int zy = z + y;                                                   \
518             const int zz = zy % height + height * (zy < 0);                         \
519             const type *xf0 = (const type *)(a->data[p] + zz * a->linesize[p]);     \
520             const type *xf1 = (const type *)(b->data[p] + zz * b->linesize[p]);     \
521                                                                                     \
522             for (int x = 0; x < out->width; x++) {                                  \
523                 dst[x] = (zy > 0) && (zy < height) ? xf1[x] : xf0[x];               \
524             }                                                                       \
525                                                                                     \
526             dst += out->linesize[p] / div;                                          \
527         }                                                                           \
528     }                                                                               \
529 }
530
531 SLIDEDOWN_TRANSITION(8, uint8_t, 1)
532 SLIDEDOWN_TRANSITION(16, uint16_t, 2)
533
534 #define CIRCLECROP_TRANSITION(name, type, div)                                      \
535 static void circlecrop##name##_transition(AVFilterContext *ctx,                     \
536                                  const AVFrame *a, const AVFrame *b, AVFrame *out,  \
537                                  float progress,                                    \
538                                  int slice_start, int slice_end, int jobnr)         \
539 {                                                                                   \
540     XFadeContext *s = ctx->priv;                                                    \
541     const int width = out->width;                                                   \
542     const int height = out->height;                                                 \
543     float z = powf(2.f * fabsf(progress - 0.5f), 3.f) * hypotf(width/2, height/2);  \
544                                                                                     \
545     for (int p = 0; p < s->nb_planes; p++) {                                        \
546         const int bg = s->black[p];                                                 \
547         type *dst = (type *)(out->data[p] + slice_start * out->linesize[p]);        \
548                                                                                     \
549         for (int y = slice_start; y < slice_end; y++) {                             \
550             const type *xf0 = (const type *)(a->data[p] + y * a->linesize[p]);      \
551             const type *xf1 = (const type *)(b->data[p] + y * b->linesize[p]);      \
552                                                                                     \
553             for (int x = 0; x < width; x++) {                                       \
554                 float dist = hypotf(x - width / 2, y - height / 2);                 \
555                 int val = progress < 0.5f ? xf1[x] : xf0[x];                        \
556                 dst[x] = (z < dist) ? bg : val;                                     \
557             }                                                                       \
558                                                                                     \
559             dst += out->linesize[p] / div;                                          \
560         }                                                                           \
561     }                                                                               \
562 }
563
564 CIRCLECROP_TRANSITION(8, uint8_t, 1)
565 CIRCLECROP_TRANSITION(16, uint16_t, 2)
566
567 #define RECTCROP_TRANSITION(name, type, div)                                        \
568 static void rectcrop##name##_transition(AVFilterContext *ctx,                       \
569                                  const AVFrame *a, const AVFrame *b, AVFrame *out,  \
570                                  float progress,                                    \
571                                  int slice_start, int slice_end, int jobnr)         \
572 {                                                                                   \
573     XFadeContext *s = ctx->priv;                                                    \
574     const int width = out->width;                                                   \
575     const int height = out->height;                                                 \
576     int zh = fabsf(progress - 0.5f) * height;                                       \
577     int zw = fabsf(progress - 0.5f) * width;                                        \
578                                                                                     \
579     for (int p = 0; p < s->nb_planes; p++) {                                        \
580         const int bg = s->black[p];                                                 \
581         type *dst = (type *)(out->data[p] + slice_start * out->linesize[p]);        \
582                                                                                     \
583         for (int y = slice_start; y < slice_end; y++) {                             \
584             const type *xf0 = (const type *)(a->data[p] + y * a->linesize[p]);      \
585             const type *xf1 = (const type *)(b->data[p] + y * b->linesize[p]);      \
586                                                                                     \
587             for (int x = 0; x < width; x++) {                                       \
588                 int dist = FFABS(x - width  / 2) < zw &&                            \
589                            FFABS(y - height / 2) < zh;                              \
590                 int val = progress < 0.5f ? xf1[x] : xf0[x];                        \
591                 dst[x] = !dist ? bg : val;                                          \
592             }                                                                       \
593                                                                                     \
594             dst += out->linesize[p] / div;                                          \
595         }                                                                           \
596     }                                                                               \
597 }
598
599 RECTCROP_TRANSITION(8, uint8_t, 1)
600 RECTCROP_TRANSITION(16, uint16_t, 2)
601
602 #define DISTANCE_TRANSITION(name, type, div)                                        \
603 static void distance##name##_transition(AVFilterContext *ctx,                       \
604                                  const AVFrame *a, const AVFrame *b, AVFrame *out,  \
605                                  float progress,                                    \
606                                  int slice_start, int slice_end, int jobnr)         \
607 {                                                                                   \
608     XFadeContext *s = ctx->priv;                                                    \
609     const int width = out->width;                                                   \
610     const float max = s->max_value;                                                 \
611                                                                                     \
612     for (int y = slice_start; y < slice_end; y++) {                                 \
613         for (int x = 0; x < width; x++) {                                           \
614             float dist = 0.f;                                                       \
615             for (int p = 0; p < s->nb_planes; p++) {                                \
616                 const type *xf0 = (const type *)(a->data[p] + y * a->linesize[p]);  \
617                 const type *xf1 = (const type *)(b->data[p] + y * b->linesize[p]);  \
618                                                                                     \
619                 dist += (xf0[x] / max - xf1[x] / max) *                             \
620                         (xf0[x] / max - xf1[x] / max);                              \
621             }                                                                       \
622                                                                                     \
623             dist = sqrtf(dist) <= progress;                                         \
624             for (int p = 0; p < s->nb_planes; p++) {                                \
625                 const type *xf0 = (const type *)(a->data[p] + y * a->linesize[p]);  \
626                 const type *xf1 = (const type *)(b->data[p] + y * b->linesize[p]);  \
627                 type *dst = (type *)(out->data[p] + y * out->linesize[p]);          \
628                 dst[x] = mix(mix(xf0[x], xf1[x], dist), xf1[x], progress);          \
629             }                                                                       \
630         }                                                                           \
631     }                                                                               \
632 }
633
634 DISTANCE_TRANSITION(8, uint8_t, 1)
635 DISTANCE_TRANSITION(16, uint16_t, 2)
636
637 #define FADEBLACK_TRANSITION(name, type, div)                                        \
638 static void fadeblack##name##_transition(AVFilterContext *ctx,                       \
639                             const AVFrame *a, const AVFrame *b, AVFrame *out,        \
640                             float progress,                                          \
641                             int slice_start, int slice_end, int jobnr)               \
642 {                                                                                    \
643     XFadeContext *s = ctx->priv;                                                     \
644     const int height = slice_end - slice_start;                                      \
645     const float phase = 0.2f;                                                        \
646                                                                                      \
647     for (int p = 0; p < s->nb_planes; p++) {                                         \
648         const type *xf0 = (const type *)(a->data[p] + slice_start * a->linesize[p]); \
649         const type *xf1 = (const type *)(b->data[p] + slice_start * b->linesize[p]); \
650         type *dst = (type *)(out->data[p] + slice_start * out->linesize[p]);         \
651         const int bg = s->black[p];                                                  \
652                                                                                      \
653         for (int y = 0; y < height; y++) {                                           \
654             for (int x = 0; x < out->width; x++) {                                   \
655                 dst[x] = mix(mix(xf0[x], bg, smoothstep(1.f-phase, 1.f, progress)),  \
656                          mix(bg, xf1[x], smoothstep(phase, 1.f, progress)),          \
657                              progress);                                              \
658             }                                                                        \
659                                                                                      \
660             dst += out->linesize[p] / div;                                           \
661             xf0 += a->linesize[p] / div;                                             \
662             xf1 += b->linesize[p] / div;                                             \
663         }                                                                            \
664     }                                                                                \
665 }
666
667 FADEBLACK_TRANSITION(8, uint8_t, 1)
668 FADEBLACK_TRANSITION(16, uint16_t, 2)
669
670 #define FADEWHITE_TRANSITION(name, type, div)                                        \
671 static void fadewhite##name##_transition(AVFilterContext *ctx,                       \
672                             const AVFrame *a, const AVFrame *b, AVFrame *out,        \
673                             float progress,                                          \
674                             int slice_start, int slice_end, int jobnr)               \
675 {                                                                                    \
676     XFadeContext *s = ctx->priv;                                                     \
677     const int height = slice_end - slice_start;                                      \
678     const float phase = 0.2f;                                                        \
679                                                                                      \
680     for (int p = 0; p < s->nb_planes; p++) {                                         \
681         const type *xf0 = (const type *)(a->data[p] + slice_start * a->linesize[p]); \
682         const type *xf1 = (const type *)(b->data[p] + slice_start * b->linesize[p]); \
683         type *dst = (type *)(out->data[p] + slice_start * out->linesize[p]);         \
684         const int bg = s->white[p];                                                  \
685                                                                                      \
686         for (int y = 0; y < height; y++) {                                           \
687             for (int x = 0; x < out->width; x++) {                                   \
688                 dst[x] = mix(mix(xf0[x], bg, smoothstep(1.f-phase, 1.f, progress)),  \
689                          mix(bg, xf1[x], smoothstep(phase, 1.f, progress)),          \
690                              progress);                                              \
691             }                                                                        \
692                                                                                      \
693             dst += out->linesize[p] / div;                                           \
694             xf0 += a->linesize[p] / div;                                             \
695             xf1 += b->linesize[p] / div;                                             \
696         }                                                                            \
697     }                                                                                \
698 }
699
700 FADEWHITE_TRANSITION(8, uint8_t, 1)
701 FADEWHITE_TRANSITION(16, uint16_t, 2)
702
703 #define RADIAL_TRANSITION(name, type, div)                                           \
704 static void radial##name##_transition(AVFilterContext *ctx,                          \
705                             const AVFrame *a, const AVFrame *b, AVFrame *out,        \
706                             float progress,                                          \
707                             int slice_start, int slice_end, int jobnr)               \
708 {                                                                                    \
709     XFadeContext *s = ctx->priv;                                                     \
710     const int width = out->width;                                                    \
711     const int height = out->height;                                                  \
712                                                                                      \
713     for (int y = slice_start; y < slice_end; y++) {                                  \
714         for (int x = 0; x < width; x++) {                                            \
715             const float smooth = atan2f(x - width / 2, y - height / 2) -             \
716                                  (progress - 0.5f) * (M_PI * 2.5f);                  \
717             for (int p = 0; p < s->nb_planes; p++) {                                 \
718                 const type *xf0 = (const type *)(a->data[p] + y * a->linesize[p]);   \
719                 const type *xf1 = (const type *)(b->data[p] + y * b->linesize[p]);   \
720                 type *dst = (type *)(out->data[p] + y * out->linesize[p]);           \
721                                                                                      \
722                 dst[x] = mix(xf1[x], xf0[x], smoothstep(0.f, 1.f, smooth));          \
723             }                                                                        \
724         }                                                                            \
725     }                                                                                \
726 }
727
728 RADIAL_TRANSITION(8, uint8_t, 1)
729 RADIAL_TRANSITION(16, uint16_t, 2)
730
731 #define SMOOTHLEFT_TRANSITION(name, type, div)                                       \
732 static void smoothleft##name##_transition(AVFilterContext *ctx,                      \
733                             const AVFrame *a, const AVFrame *b, AVFrame *out,        \
734                             float progress,                                          \
735                             int slice_start, int slice_end, int jobnr)               \
736 {                                                                                    \
737     XFadeContext *s = ctx->priv;                                                     \
738     const int width = out->width;                                                    \
739     const float w = width;                                                           \
740                                                                                      \
741     for (int y = slice_start; y < slice_end; y++) {                                  \
742         for (int x = 0; x < width; x++) {                                            \
743             const float smooth = 1.f + x / w - progress * 2.f;                       \
744                                                                                      \
745             for (int p = 0; p < s->nb_planes; p++) {                                 \
746                 const type *xf0 = (const type *)(a->data[p] + y * a->linesize[p]);   \
747                 const type *xf1 = (const type *)(b->data[p] + y * b->linesize[p]);   \
748                 type *dst = (type *)(out->data[p] + y * out->linesize[p]);           \
749                                                                                      \
750                 dst[x] = mix(xf1[x], xf0[x], smoothstep(0.f, 1.f, smooth));          \
751             }                                                                        \
752         }                                                                            \
753     }                                                                                \
754 }
755
756 SMOOTHLEFT_TRANSITION(8, uint8_t, 1)
757 SMOOTHLEFT_TRANSITION(16, uint16_t, 2)
758
759 #define SMOOTHRIGHT_TRANSITION(name, type, div)                                      \
760 static void smoothright##name##_transition(AVFilterContext *ctx,                     \
761                             const AVFrame *a, const AVFrame *b, AVFrame *out,        \
762                             float progress,                                          \
763                             int slice_start, int slice_end, int jobnr)               \
764 {                                                                                    \
765     XFadeContext *s = ctx->priv;                                                     \
766     const int width = out->width;                                                    \
767     const float w = width;                                                           \
768                                                                                      \
769     for (int y = slice_start; y < slice_end; y++) {                                  \
770         for (int x = 0; x < width; x++) {                                            \
771             const float smooth = 1.f + (w - 1 - x) / w - progress * 2.f;             \
772                                                                                      \
773             for (int p = 0; p < s->nb_planes; p++) {                                 \
774                 const type *xf0 = (const type *)(a->data[p] + y * a->linesize[p]);   \
775                 const type *xf1 = (const type *)(b->data[p] + y * b->linesize[p]);   \
776                 type *dst = (type *)(out->data[p] + y * out->linesize[p]);           \
777                                                                                      \
778                 dst[x] = mix(xf1[x], xf0[x], smoothstep(0.f, 1.f, smooth));          \
779             }                                                                        \
780         }                                                                            \
781     }                                                                                \
782 }
783
784 SMOOTHRIGHT_TRANSITION(8, uint8_t, 1)
785 SMOOTHRIGHT_TRANSITION(16, uint16_t, 2)
786
787 #define SMOOTHUP_TRANSITION(name, type, div)                                         \
788 static void smoothup##name##_transition(AVFilterContext *ctx,                        \
789                             const AVFrame *a, const AVFrame *b, AVFrame *out,        \
790                             float progress,                                          \
791                             int slice_start, int slice_end, int jobnr)               \
792 {                                                                                    \
793     XFadeContext *s = ctx->priv;                                                     \
794     const int width = out->width;                                                    \
795     const float h = out->height;                                                     \
796                                                                                      \
797     for (int y = slice_start; y < slice_end; y++) {                                  \
798         const float smooth = 1.f + y / h - progress * 2.f;                           \
799         for (int x = 0; x < width; x++) {                                            \
800             for (int p = 0; p < s->nb_planes; p++) {                                 \
801                 const type *xf0 = (const type *)(a->data[p] + y * a->linesize[p]);   \
802                 const type *xf1 = (const type *)(b->data[p] + y * b->linesize[p]);   \
803                 type *dst = (type *)(out->data[p] + y * out->linesize[p]);           \
804                                                                                      \
805                 dst[x] = mix(xf1[x], xf0[x], smoothstep(0.f, 1.f, smooth));          \
806             }                                                                        \
807         }                                                                            \
808     }                                                                                \
809 }
810
811 SMOOTHUP_TRANSITION(8, uint8_t, 1)
812 SMOOTHUP_TRANSITION(16, uint16_t, 2)
813
814 #define SMOOTHDOWN_TRANSITION(name, type, div)                                       \
815 static void smoothdown##name##_transition(AVFilterContext *ctx,                      \
816                             const AVFrame *a, const AVFrame *b, AVFrame *out,        \
817                             float progress,                                          \
818                             int slice_start, int slice_end, int jobnr)               \
819 {                                                                                    \
820     XFadeContext *s = ctx->priv;                                                     \
821     const int width = out->width;                                                    \
822     const float h = out->height;                                                     \
823                                                                                      \
824     for (int y = slice_start; y < slice_end; y++) {                                  \
825         const float smooth = 1.f + (h - 1 - y) / h - progress * 2.f;                 \
826         for (int x = 0; x < width; x++) {                                            \
827             for (int p = 0; p < s->nb_planes; p++) {                                 \
828                 const type *xf0 = (const type *)(a->data[p] + y * a->linesize[p]);   \
829                 const type *xf1 = (const type *)(b->data[p] + y * b->linesize[p]);   \
830                 type *dst = (type *)(out->data[p] + y * out->linesize[p]);           \
831                                                                                      \
832                 dst[x] = mix(xf1[x], xf0[x], smoothstep(0.f, 1.f, smooth));          \
833             }                                                                        \
834         }                                                                            \
835     }                                                                                \
836 }
837
838 SMOOTHDOWN_TRANSITION(8, uint8_t, 1)
839 SMOOTHDOWN_TRANSITION(16, uint16_t, 2)
840
841 #define CIRCLEOPEN_TRANSITION(name, type, div)                                       \
842 static void circleopen##name##_transition(AVFilterContext *ctx,                      \
843                             const AVFrame *a, const AVFrame *b, AVFrame *out,        \
844                             float progress,                                          \
845                             int slice_start, int slice_end, int jobnr)               \
846 {                                                                                    \
847     XFadeContext *s = ctx->priv;                                                     \
848     const int width = out->width;                                                    \
849     const int height = out->height;                                                  \
850     const float z = hypotf(width / 2, height / 2);                                   \
851     const float p = (progress - 0.5f) * 3.f;                                         \
852                                                                                      \
853     for (int y = slice_start; y < slice_end; y++) {                                  \
854         for (int x = 0; x < width; x++) {                                            \
855             const float smooth = hypotf(x - width / 2, y - height / 2) / z + p;      \
856             for (int p = 0; p < s->nb_planes; p++) {                                 \
857                 const type *xf0 = (const type *)(a->data[p] + y * a->linesize[p]);   \
858                 const type *xf1 = (const type *)(b->data[p] + y * b->linesize[p]);   \
859                 type *dst = (type *)(out->data[p] + y * out->linesize[p]);           \
860                                                                                      \
861                 dst[x] = mix(xf0[x], xf1[x], smoothstep(0.f, 1.f, smooth));          \
862             }                                                                        \
863         }                                                                            \
864     }                                                                                \
865 }
866
867 CIRCLEOPEN_TRANSITION(8, uint8_t, 1)
868 CIRCLEOPEN_TRANSITION(16, uint16_t, 2)
869
870 #define CIRCLECLOSE_TRANSITION(name, type, div)                                      \
871 static void circleclose##name##_transition(AVFilterContext *ctx,                     \
872                             const AVFrame *a, const AVFrame *b, AVFrame *out,        \
873                             float progress,                                          \
874                             int slice_start, int slice_end, int jobnr)               \
875 {                                                                                    \
876     XFadeContext *s = ctx->priv;                                                     \
877     const int width = out->width;                                                    \
878     const int height = out->height;                                                  \
879     const float z = hypotf(width / 2, height / 2);                                   \
880     const float p = (1.f - progress - 0.5f) * 3.f;                                   \
881                                                                                      \
882     for (int y = slice_start; y < slice_end; y++) {                                  \
883         for (int x = 0; x < width; x++) {                                            \
884             const float smooth = hypotf(x - width / 2, y - height / 2) / z + p;      \
885             for (int p = 0; p < s->nb_planes; p++) {                                 \
886                 const type *xf0 = (const type *)(a->data[p] + y * a->linesize[p]);   \
887                 const type *xf1 = (const type *)(b->data[p] + y * b->linesize[p]);   \
888                 type *dst = (type *)(out->data[p] + y * out->linesize[p]);           \
889                                                                                      \
890                 dst[x] = mix(xf1[x], xf0[x], smoothstep(0.f, 1.f, smooth));          \
891             }                                                                        \
892         }                                                                            \
893     }                                                                                \
894 }
895
896 CIRCLECLOSE_TRANSITION(8, uint8_t, 1)
897 CIRCLECLOSE_TRANSITION(16, uint16_t, 2)
898
899 #define VERTOPEN_TRANSITION(name, type, div)                                         \
900 static void vertopen##name##_transition(AVFilterContext *ctx,                        \
901                             const AVFrame *a, const AVFrame *b, AVFrame *out,        \
902                             float progress,                                          \
903                             int slice_start, int slice_end, int jobnr)               \
904 {                                                                                    \
905     XFadeContext *s = ctx->priv;                                                     \
906     const int width = out->width;                                                    \
907     const float w2 = out->width / 2;                                                 \
908                                                                                      \
909     for (int y = slice_start; y < slice_end; y++) {                                  \
910         for (int x = 0; x < width; x++) {                                            \
911             const float smooth = 2.f - fabsf((x - w2) / w2) - progress * 2.f;        \
912             for (int p = 0; p < s->nb_planes; p++) {                                 \
913                 const type *xf0 = (const type *)(a->data[p] + y * a->linesize[p]);   \
914                 const type *xf1 = (const type *)(b->data[p] + y * b->linesize[p]);   \
915                 type *dst = (type *)(out->data[p] + y * out->linesize[p]);           \
916                                                                                      \
917                 dst[x] = mix(xf1[x], xf0[x], smoothstep(0.f, 1.f, smooth));          \
918             }                                                                        \
919         }                                                                            \
920     }                                                                                \
921 }
922
923 VERTOPEN_TRANSITION(8, uint8_t, 1)
924 VERTOPEN_TRANSITION(16, uint16_t, 2)
925
926 #define VERTCLOSE_TRANSITION(name, type, div)                                        \
927 static void vertclose##name##_transition(AVFilterContext *ctx,                       \
928                             const AVFrame *a, const AVFrame *b, AVFrame *out,        \
929                             float progress,                                          \
930                             int slice_start, int slice_end, int jobnr)               \
931 {                                                                                    \
932     XFadeContext *s = ctx->priv;                                                     \
933     const int width = out->width;                                                    \
934     const float w2 = out->width / 2;                                                 \
935                                                                                      \
936     for (int y = slice_start; y < slice_end; y++) {                                  \
937         for (int x = 0; x < width; x++) {                                            \
938             const float smooth = 1.f + fabsf((x - w2) / w2) - progress * 2.f;        \
939             for (int p = 0; p < s->nb_planes; p++) {                                 \
940                 const type *xf0 = (const type *)(a->data[p] + y * a->linesize[p]);   \
941                 const type *xf1 = (const type *)(b->data[p] + y * b->linesize[p]);   \
942                 type *dst = (type *)(out->data[p] + y * out->linesize[p]);           \
943                                                                                      \
944                 dst[x] = mix(xf1[x], xf0[x], smoothstep(0.f, 1.f, smooth));          \
945             }                                                                        \
946         }                                                                            \
947     }                                                                                \
948 }
949
950 VERTCLOSE_TRANSITION(8, uint8_t, 1)
951 VERTCLOSE_TRANSITION(16, uint16_t, 2)
952
953 #define HORZOPEN_TRANSITION(name, type, div)                                         \
954 static void horzopen##name##_transition(AVFilterContext *ctx,                        \
955                             const AVFrame *a, const AVFrame *b, AVFrame *out,        \
956                             float progress,                                          \
957                             int slice_start, int slice_end, int jobnr)               \
958 {                                                                                    \
959     XFadeContext *s = ctx->priv;                                                     \
960     const int width = out->width;                                                    \
961     const float h2 = out->height / 2;                                                \
962                                                                                      \
963     for (int y = slice_start; y < slice_end; y++) {                                  \
964         const float smooth = 2.f - fabsf((y - h2) / h2) - progress * 2.f;            \
965         for (int x = 0; x < width; x++) {                                            \
966             for (int p = 0; p < s->nb_planes; p++) {                                 \
967                 const type *xf0 = (const type *)(a->data[p] + y * a->linesize[p]);   \
968                 const type *xf1 = (const type *)(b->data[p] + y * b->linesize[p]);   \
969                 type *dst = (type *)(out->data[p] + y * out->linesize[p]);           \
970                                                                                      \
971                 dst[x] = mix(xf1[x], xf0[x], smoothstep(0.f, 1.f, smooth));          \
972             }                                                                        \
973         }                                                                            \
974     }                                                                                \
975 }
976
977 HORZOPEN_TRANSITION(8, uint8_t, 1)
978 HORZOPEN_TRANSITION(16, uint16_t, 2)
979
980 #define HORZCLOSE_TRANSITION(name, type, div)                                        \
981 static void horzclose##name##_transition(AVFilterContext *ctx,                       \
982                             const AVFrame *a, const AVFrame *b, AVFrame *out,        \
983                             float progress,                                          \
984                             int slice_start, int slice_end, int jobnr)               \
985 {                                                                                    \
986     XFadeContext *s = ctx->priv;                                                     \
987     const int width = out->width;                                                    \
988     const float h2 = out->height / 2;                                                \
989                                                                                      \
990     for (int y = slice_start; y < slice_end; y++) {                                  \
991         const float smooth = 1.f + fabsf((y - h2) / h2) - progress * 2.f;            \
992         for (int x = 0; x < width; x++) {                                            \
993             for (int p = 0; p < s->nb_planes; p++) {                                 \
994                 const type *xf0 = (const type *)(a->data[p] + y * a->linesize[p]);   \
995                 const type *xf1 = (const type *)(b->data[p] + y * b->linesize[p]);   \
996                 type *dst = (type *)(out->data[p] + y * out->linesize[p]);           \
997                                                                                      \
998                 dst[x] = mix(xf1[x], xf0[x], smoothstep(0.f, 1.f, smooth));          \
999             }                                                                        \
1000         }                                                                            \
1001     }                                                                                \
1002 }
1003
1004 HORZCLOSE_TRANSITION(8, uint8_t, 1)
1005 HORZCLOSE_TRANSITION(16, uint16_t, 2)
1006
1007 static float frand(int x, int y)
1008 {
1009     const float r = sinf(x * 12.9898f + y * 78.233f) * 43758.545f;
1010
1011     return r - floorf(r);
1012 }
1013
1014 #define DISSOLVE_TRANSITION(name, type, div)                                         \
1015 static void dissolve##name##_transition(AVFilterContext *ctx,                        \
1016                             const AVFrame *a, const AVFrame *b, AVFrame *out,        \
1017                             float progress,                                          \
1018                             int slice_start, int slice_end, int jobnr)               \
1019 {                                                                                    \
1020     XFadeContext *s = ctx->priv;                                                     \
1021     const int width = out->width;                                                    \
1022                                                                                      \
1023     for (int y = slice_start; y < slice_end; y++) {                                  \
1024         for (int x = 0; x < width; x++) {                                            \
1025             const float smooth = frand(x, y) * 2.f + progress * 2.f - 1.5f;          \
1026             for (int p = 0; p < s->nb_planes; p++) {                                 \
1027                 const type *xf0 = (const type *)(a->data[p] + y * a->linesize[p]);   \
1028                 const type *xf1 = (const type *)(b->data[p] + y * b->linesize[p]);   \
1029                 type *dst = (type *)(out->data[p] + y * out->linesize[p]);           \
1030                                                                                      \
1031                 dst[x] = smooth >= 0.5f ? xf0[x] : xf1[x];                           \
1032             }                                                                        \
1033         }                                                                            \
1034     }                                                                                \
1035 }
1036
1037 DISSOLVE_TRANSITION(8, uint8_t, 1)
1038 DISSOLVE_TRANSITION(16, uint16_t, 2)
1039
1040 #define PIXELIZE_TRANSITION(name, type, div)                                         \
1041 static void pixelize##name##_transition(AVFilterContext *ctx,                        \
1042                             const AVFrame *a, const AVFrame *b, AVFrame *out,        \
1043                             float progress,                                          \
1044                             int slice_start, int slice_end, int jobnr)               \
1045 {                                                                                    \
1046     XFadeContext *s = ctx->priv;                                                     \
1047     const int w = out->width;                                                        \
1048     const int h = out->height;                                                       \
1049     const float d = fminf(progress, 1.f - progress);                                 \
1050     const float dist = ceilf(d * 50.f) / 50.f;                                       \
1051     const float sqx = 2.f * dist * FFMIN(w, h) / 20.f;                               \
1052     const float sqy = 2.f * dist * FFMIN(w, h) / 20.f;                               \
1053                                                                                      \
1054     for (int y = slice_start; y < slice_end; y++) {                                  \
1055         for (int x = 0; x < w; x++) {                                                \
1056             int sx = dist > 0.f ? FFMIN((floorf(x / sqx) + .5f) * sqx, w - 1) : x;   \
1057             int sy = dist > 0.f ? FFMIN((floorf(y / sqy) + .5f) * sqy, h - 1) : y;   \
1058             for (int p = 0; p < s->nb_planes; p++) {                                 \
1059                 const type *xf0 = (const type *)(a->data[p] + sy * a->linesize[p]);  \
1060                 const type *xf1 = (const type *)(b->data[p] + sy * b->linesize[p]);  \
1061                 type *dst = (type *)(out->data[p] + y * out->linesize[p]);           \
1062                                                                                      \
1063                 dst[x] = mix(xf0[sx], xf1[sx], progress);                            \
1064             }                                                                        \
1065         }                                                                            \
1066     }                                                                                \
1067 }
1068
1069 PIXELIZE_TRANSITION(8, uint8_t, 1)
1070 PIXELIZE_TRANSITION(16, uint16_t, 2)
1071
1072 #define DIAGTL_TRANSITION(name, type, div)                                           \
1073 static void diagtl##name##_transition(AVFilterContext *ctx,                          \
1074                             const AVFrame *a, const AVFrame *b, AVFrame *out,        \
1075                             float progress,                                          \
1076                             int slice_start, int slice_end, int jobnr)               \
1077 {                                                                                    \
1078     XFadeContext *s = ctx->priv;                                                     \
1079     const int width = out->width;                                                    \
1080     const float w = width;                                                           \
1081     const float h = out->height;                                                     \
1082                                                                                      \
1083     for (int y = slice_start; y < slice_end; y++) {                                  \
1084         for (int x = 0; x < width; x++) {                                            \
1085             const float smooth = 1.f + x / w * y / h - progress * 2.f;               \
1086                                                                                      \
1087             for (int p = 0; p < s->nb_planes; p++) {                                 \
1088                 const type *xf0 = (const type *)(a->data[p] + y * a->linesize[p]);   \
1089                 const type *xf1 = (const type *)(b->data[p] + y * b->linesize[p]);   \
1090                 type *dst = (type *)(out->data[p] + y * out->linesize[p]);           \
1091                                                                                      \
1092                 dst[x] = mix(xf1[x], xf0[x], smoothstep(0.f, 1.f, smooth));          \
1093             }                                                                        \
1094         }                                                                            \
1095     }                                                                                \
1096 }
1097
1098 DIAGTL_TRANSITION(8, uint8_t, 1)
1099 DIAGTL_TRANSITION(16, uint16_t, 2)
1100
1101 #define DIAGTR_TRANSITION(name, type, div)                                           \
1102 static void diagtr##name##_transition(AVFilterContext *ctx,                          \
1103                             const AVFrame *a, const AVFrame *b, AVFrame *out,        \
1104                             float progress,                                          \
1105                             int slice_start, int slice_end, int jobnr)               \
1106 {                                                                                    \
1107     XFadeContext *s = ctx->priv;                                                     \
1108     const int width = out->width;                                                    \
1109     const float w = width;                                                           \
1110     const float h = out->height;                                                     \
1111                                                                                      \
1112     for (int y = slice_start; y < slice_end; y++) {                                  \
1113         for (int x = 0; x < width; x++) {                                            \
1114             const float smooth = 1.f + (w - 1 - x) / w * y / h - progress * 2.f;     \
1115                                                                                      \
1116             for (int p = 0; p < s->nb_planes; p++) {                                 \
1117                 const type *xf0 = (const type *)(a->data[p] + y * a->linesize[p]);   \
1118                 const type *xf1 = (const type *)(b->data[p] + y * b->linesize[p]);   \
1119                 type *dst = (type *)(out->data[p] + y * out->linesize[p]);           \
1120                                                                                      \
1121                 dst[x] = mix(xf1[x], xf0[x], smoothstep(0.f, 1.f, smooth));          \
1122             }                                                                        \
1123         }                                                                            \
1124     }                                                                                \
1125 }
1126
1127 DIAGTR_TRANSITION(8, uint8_t, 1)
1128 DIAGTR_TRANSITION(16, uint16_t, 2)
1129
1130 #define DIAGBL_TRANSITION(name, type, div)                                           \
1131 static void diagbl##name##_transition(AVFilterContext *ctx,                          \
1132                             const AVFrame *a, const AVFrame *b, AVFrame *out,        \
1133                             float progress,                                          \
1134                             int slice_start, int slice_end, int jobnr)               \
1135 {                                                                                    \
1136     XFadeContext *s = ctx->priv;                                                     \
1137     const int width = out->width;                                                    \
1138     const float w = width;                                                           \
1139     const float h = out->height;                                                     \
1140                                                                                      \
1141     for (int y = slice_start; y < slice_end; y++) {                                  \
1142         for (int x = 0; x < width; x++) {                                            \
1143             const float smooth = 1.f + x / w * (h - 1 - y) / h - progress * 2.f;     \
1144                                                                                      \
1145             for (int p = 0; p < s->nb_planes; p++) {                                 \
1146                 const type *xf0 = (const type *)(a->data[p] + y * a->linesize[p]);   \
1147                 const type *xf1 = (const type *)(b->data[p] + y * b->linesize[p]);   \
1148                 type *dst = (type *)(out->data[p] + y * out->linesize[p]);           \
1149                                                                                      \
1150                 dst[x] = mix(xf1[x], xf0[x], smoothstep(0.f, 1.f, smooth));          \
1151             }                                                                        \
1152         }                                                                            \
1153     }                                                                                \
1154 }
1155
1156 DIAGBL_TRANSITION(8, uint8_t, 1)
1157 DIAGBL_TRANSITION(16, uint16_t, 2)
1158
1159 #define DIAGBR_TRANSITION(name, type, div)                                           \
1160 static void diagbr##name##_transition(AVFilterContext *ctx,                          \
1161                             const AVFrame *a, const AVFrame *b, AVFrame *out,        \
1162                             float progress,                                          \
1163                             int slice_start, int slice_end, int jobnr)               \
1164 {                                                                                    \
1165     XFadeContext *s = ctx->priv;                                                     \
1166     const int width = out->width;                                                    \
1167     const float w = width;                                                           \
1168     const float h = out->height;                                                     \
1169                                                                                      \
1170     for (int y = slice_start; y < slice_end; y++) {                                  \
1171         for (int x = 0; x < width; x++) {                                            \
1172             const float smooth = 1.f + (w - 1 - x) / w * (h - 1 - y) / h -           \
1173                                  progress * 2.f;                                     \
1174                                                                                      \
1175             for (int p = 0; p < s->nb_planes; p++) {                                 \
1176                 const type *xf0 = (const type *)(a->data[p] + y * a->linesize[p]);   \
1177                 const type *xf1 = (const type *)(b->data[p] + y * b->linesize[p]);   \
1178                 type *dst = (type *)(out->data[p] + y * out->linesize[p]);           \
1179                                                                                      \
1180                 dst[x] = mix(xf1[x], xf0[x], smoothstep(0.f, 1.f, smooth));          \
1181             }                                                                        \
1182         }                                                                            \
1183     }                                                                                \
1184 }
1185
1186 DIAGBR_TRANSITION(8, uint8_t, 1)
1187 DIAGBR_TRANSITION(16, uint16_t, 2)
1188
1189 #define HLSLICE_TRANSITION(name, type, div)                                          \
1190 static void hlslice##name##_transition(AVFilterContext *ctx,                         \
1191                             const AVFrame *a, const AVFrame *b, AVFrame *out,        \
1192                             float progress,                                          \
1193                             int slice_start, int slice_end, int jobnr)               \
1194 {                                                                                    \
1195     XFadeContext *s = ctx->priv;                                                     \
1196     const int width = out->width;                                                    \
1197     const float w = width;                                                           \
1198                                                                                      \
1199     for (int y = slice_start; y < slice_end; y++) {                                  \
1200         for (int x = 0; x < width; x++) {                                            \
1201             const float smooth = smoothstep(-0.5f, 0.f, x / w - progress * 1.5f);    \
1202             const float ss = smooth <= fract(10.f * x / w) ? 0.f : 1.f;              \
1203                                                                                      \
1204             for (int p = 0; p < s->nb_planes; p++) {                                 \
1205                 const type *xf0 = (const type *)(a->data[p] + y * a->linesize[p]);   \
1206                 const type *xf1 = (const type *)(b->data[p] + y * b->linesize[p]);   \
1207                 type *dst = (type *)(out->data[p] + y * out->linesize[p]);           \
1208                                                                                      \
1209                 dst[x] = mix(xf1[x], xf0[x], ss);                                    \
1210             }                                                                        \
1211         }                                                                            \
1212     }                                                                                \
1213 }
1214
1215 HLSLICE_TRANSITION(8, uint8_t, 1)
1216 HLSLICE_TRANSITION(16, uint16_t, 2)
1217
1218 #define HRSLICE_TRANSITION(name, type, div)                                          \
1219 static void hrslice##name##_transition(AVFilterContext *ctx,                         \
1220                             const AVFrame *a, const AVFrame *b, AVFrame *out,        \
1221                             float progress,                                          \
1222                             int slice_start, int slice_end, int jobnr)               \
1223 {                                                                                    \
1224     XFadeContext *s = ctx->priv;                                                     \
1225     const int width = out->width;                                                    \
1226     const float w = width;                                                           \
1227                                                                                      \
1228     for (int y = slice_start; y < slice_end; y++) {                                  \
1229         for (int x = 0; x < width; x++) {                                            \
1230             const float xx = (w - 1 - x) / w;                                        \
1231             const float smooth = smoothstep(-0.5f, 0.f, xx - progress * 1.5f);       \
1232             const float ss = smooth <= fract(10.f * xx) ? 0.f : 1.f;                 \
1233                                                                                      \
1234             for (int p = 0; p < s->nb_planes; p++) {                                 \
1235                 const type *xf0 = (const type *)(a->data[p] + y * a->linesize[p]);   \
1236                 const type *xf1 = (const type *)(b->data[p] + y * b->linesize[p]);   \
1237                 type *dst = (type *)(out->data[p] + y * out->linesize[p]);           \
1238                                                                                      \
1239                 dst[x] = mix(xf1[x], xf0[x], ss);                                    \
1240             }                                                                        \
1241         }                                                                            \
1242     }                                                                                \
1243 }
1244
1245 HRSLICE_TRANSITION(8, uint8_t, 1)
1246 HRSLICE_TRANSITION(16, uint16_t, 2)
1247
1248 #define VUSLICE_TRANSITION(name, type, div)                                          \
1249 static void vuslice##name##_transition(AVFilterContext *ctx,                         \
1250                             const AVFrame *a, const AVFrame *b, AVFrame *out,        \
1251                             float progress,                                          \
1252                             int slice_start, int slice_end, int jobnr)               \
1253 {                                                                                    \
1254     XFadeContext *s = ctx->priv;                                                     \
1255     const int width = out->width;                                                    \
1256     const float h = out->height;                                                     \
1257                                                                                      \
1258     for (int y = slice_start; y < slice_end; y++) {                                  \
1259          const float smooth = smoothstep(-0.5f, 0.f, y / h - progress * 1.5f);       \
1260          const float ss = smooth <= fract(10.f * y / h) ? 0.f : 1.f;                 \
1261                                                                                      \
1262          for (int x = 0; x < width; x++) {                                           \
1263             for (int p = 0; p < s->nb_planes; p++) {                                 \
1264                 const type *xf0 = (const type *)(a->data[p] + y * a->linesize[p]);   \
1265                 const type *xf1 = (const type *)(b->data[p] + y * b->linesize[p]);   \
1266                 type *dst = (type *)(out->data[p] + y * out->linesize[p]);           \
1267                                                                                      \
1268                 dst[x] = mix(xf1[x], xf0[x], ss);                                    \
1269             }                                                                        \
1270         }                                                                            \
1271     }                                                                                \
1272 }
1273
1274 VUSLICE_TRANSITION(8, uint8_t, 1)
1275 VUSLICE_TRANSITION(16, uint16_t, 2)
1276
1277 #define VDSLICE_TRANSITION(name, type, div)                                          \
1278 static void vdslice##name##_transition(AVFilterContext *ctx,                         \
1279                             const AVFrame *a, const AVFrame *b, AVFrame *out,        \
1280                             float progress,                                          \
1281                             int slice_start, int slice_end, int jobnr)               \
1282 {                                                                                    \
1283     XFadeContext *s = ctx->priv;                                                     \
1284     const int width = out->width;                                                    \
1285     const float h = out->height;                                                     \
1286                                                                                      \
1287     for (int y = slice_start; y < slice_end; y++) {                                  \
1288          const float yy = (h - 1 - y) / h;                                           \
1289          const float smooth = smoothstep(-0.5f, 0.f, yy - progress * 1.5f);          \
1290          const float ss = smooth <= fract(10.f * yy) ? 0.f : 1.f;                    \
1291                                                                                      \
1292          for (int x = 0; x < width; x++) {                                           \
1293             for (int p = 0; p < s->nb_planes; p++) {                                 \
1294                 const type *xf0 = (const type *)(a->data[p] + y * a->linesize[p]);   \
1295                 const type *xf1 = (const type *)(b->data[p] + y * b->linesize[p]);   \
1296                 type *dst = (type *)(out->data[p] + y * out->linesize[p]);           \
1297                                                                                      \
1298                 dst[x] = mix(xf1[x], xf0[x], ss);                                    \
1299             }                                                                        \
1300         }                                                                            \
1301     }                                                                                \
1302 }
1303
1304 VDSLICE_TRANSITION(8, uint8_t, 1)
1305 VDSLICE_TRANSITION(16, uint16_t, 2)
1306
1307 #define HBLUR_TRANSITION(name, type, div)                                            \
1308 static void hblur##name##_transition(AVFilterContext *ctx,                           \
1309                             const AVFrame *a, const AVFrame *b, AVFrame *out,        \
1310                             float progress,                                          \
1311                             int slice_start, int slice_end, int jobnr)               \
1312 {                                                                                    \
1313     XFadeContext *s = ctx->priv;                                                     \
1314     const int width = out->width;                                                    \
1315     const float prog = progress <= 0.5f ? progress * 2.f : (1.f - progress) * 2.f;   \
1316     const int size = 1 + (width / 2) * prog;                                         \
1317                                                                                      \
1318     for (int y = slice_start; y < slice_end; y++) {                                  \
1319         for (int p = 0; p < s->nb_planes; p++) {                                     \
1320             const type *xf0 = (const type *)(a->data[p] + y * a->linesize[p]);       \
1321             const type *xf1 = (const type *)(b->data[p] + y * b->linesize[p]);       \
1322             type *dst = (type *)(out->data[p] + y * out->linesize[p]);               \
1323             float sum0 = 0.f;                                                        \
1324             float sum1 = 0.f;                                                        \
1325             float cnt = size;                                                        \
1326                                                                                      \
1327             for (int x = 0; x < size; x++) {                                         \
1328                 sum0 += xf0[x];                                                      \
1329                 sum1 += xf1[x];                                                      \
1330             }                                                                        \
1331                                                                                      \
1332             for (int x = 0; x < width; x++) {                                        \
1333                 dst[x] = mix(sum0 / cnt, sum1 / cnt, progress);                      \
1334                                                                                      \
1335                 if (x + size < width) {                                              \
1336                     sum0 += xf0[x + size] - xf0[x];                                  \
1337                     sum1 += xf1[x + size] - xf1[x];                                  \
1338                 } else {                                                             \
1339                     sum0 -= xf0[x];                                                  \
1340                     sum1 -= xf1[x];                                                  \
1341                     cnt--;                                                           \
1342                 }                                                                    \
1343             }                                                                        \
1344         }                                                                            \
1345     }                                                                                \
1346 }
1347
1348 HBLUR_TRANSITION(8, uint8_t, 1)
1349 HBLUR_TRANSITION(16, uint16_t, 2)
1350
1351 #define FADEGRAYS_TRANSITION(name, type, div)                                        \
1352 static void fadegrays##name##_transition(AVFilterContext *ctx,                       \
1353                             const AVFrame *a, const AVFrame *b, AVFrame *out,        \
1354                             float progress,                                          \
1355                             int slice_start, int slice_end, int jobnr)               \
1356 {                                                                                    \
1357     XFadeContext *s = ctx->priv;                                                     \
1358     const int width = out->width;                                                    \
1359     const int is_rgb = s->is_rgb;                                                    \
1360     const int mid = (s->max_value + 1) / 2;                                          \
1361     const float phase = 0.2f;                                                        \
1362                                                                                      \
1363     for (int y = slice_start; y < slice_end; y++) {                                  \
1364         for (int x = 0; x < width; x++) {                                            \
1365             int bg[2][4];                                                            \
1366             if (is_rgb) {                                                            \
1367                 for (int p = 0; p < s->nb_planes; p++) {                             \
1368                     const type *xf0 = (const type *)(a->data[p] +                    \
1369                                                      y * a->linesize[p]);            \
1370                     const type *xf1 = (const type *)(b->data[p] +                    \
1371                                                      y * b->linesize[p]);            \
1372                     bg[0][0] += xf0[x];                                              \
1373                     bg[1][0] += xf1[x];                                              \
1374                     if (p == 3) {                                                    \
1375                         bg[0][3] = xf0[x];                                           \
1376                         bg[1][3] = xf1[x];                                           \
1377                     }                                                                \
1378                 }                                                                    \
1379                 bg[0][0] = bg[0][0] / s->nb_planes;                                  \
1380                 bg[1][0] = bg[1][0] / s->nb_planes;                                  \
1381                 bg[0][1] = bg[0][2] = bg[0][0];                                      \
1382                 bg[1][1] = bg[1][2] = bg[1][0];                                      \
1383             } else {                                                                 \
1384                 const type *yf0 = (const type *)(a->data[0] +                        \
1385                                                  y * a->linesize[0]);                \
1386                 const type *yf1 = (const type *)(b->data[0] +                        \
1387                                                  y * a->linesize[0]);                \
1388                 bg[0][0] = yf0[x];                                                   \
1389                 bg[1][0] = yf1[x];                                                   \
1390                 if (s->nb_planes == 4) {                                             \
1391                     const type *af0 = (const type *)(a->data[3] +                    \
1392                                                      y * a->linesize[3]);            \
1393                     const type *af1 = (const type *)(b->data[3] +                    \
1394                                                      y * a->linesize[3]);            \
1395                     bg[0][3] = af0[x];                                               \
1396                     bg[1][3] = af1[x];                                               \
1397                 }                                                                    \
1398                 bg[0][1] = bg[1][1] = mid;                                           \
1399                 bg[0][2] = bg[1][2] = mid;                                           \
1400             }                                                                        \
1401                                                                                      \
1402             for (int p = 0; p < s->nb_planes; p++) {                                 \
1403                 const type *xf0 = (const type *)(a->data[p] + y * a->linesize[p]);   \
1404                 const type *xf1 = (const type *)(b->data[p] + y * b->linesize[p]);   \
1405                 type *dst = (type *)(out->data[p] + y * out->linesize[p]);           \
1406                                                                                      \
1407                 dst[x] = mix(mix(xf0[x], bg[0][p],                                   \
1408                                  smoothstep(1.f-phase, 1.f, progress)),              \
1409                          mix(bg[1][p], xf1[x], smoothstep(phase, 1.f, progress)),    \
1410                              progress);                                              \
1411             }                                                                        \
1412         }                                                                            \
1413     }                                                                                \
1414 }
1415
1416 FADEGRAYS_TRANSITION(8, uint8_t, 1)
1417 FADEGRAYS_TRANSITION(16, uint16_t, 2)
1418
1419 static inline double getpix(void *priv, double x, double y, int plane, int nb)
1420 {
1421     XFadeContext *s = priv;
1422     AVFrame *in = s->xf[nb];
1423     const uint8_t *src = in->data[FFMIN(plane, s->nb_planes - 1)];
1424     int linesize = in->linesize[FFMIN(plane, s->nb_planes - 1)];
1425     const int w = in->width;
1426     const int h = in->height;
1427
1428     int xi, yi;
1429
1430     xi = av_clipd(x, 0, w - 1);
1431     yi = av_clipd(y, 0, h - 1);
1432
1433     if (s->depth > 8) {
1434         const uint16_t *src16 = (const uint16_t*)src;
1435
1436         linesize /= 2;
1437         return src16[xi + yi * linesize];
1438     } else {
1439         return src[xi + yi * linesize];
1440     }
1441 }
1442
1443 static double a0(void *priv, double x, double y) { return getpix(priv, x, y, 0, 0); }
1444 static double a1(void *priv, double x, double y) { return getpix(priv, x, y, 1, 0); }
1445 static double a2(void *priv, double x, double y) { return getpix(priv, x, y, 2, 0); }
1446 static double a3(void *priv, double x, double y) { return getpix(priv, x, y, 3, 0); }
1447
1448 static double b0(void *priv, double x, double y) { return getpix(priv, x, y, 0, 1); }
1449 static double b1(void *priv, double x, double y) { return getpix(priv, x, y, 1, 1); }
1450 static double b2(void *priv, double x, double y) { return getpix(priv, x, y, 2, 1); }
1451 static double b3(void *priv, double x, double y) { return getpix(priv, x, y, 3, 1); }
1452
1453 static int config_output(AVFilterLink *outlink)
1454 {
1455     AVFilterContext *ctx = outlink->src;
1456     AVFilterLink *inlink0 = ctx->inputs[0];
1457     AVFilterLink *inlink1 = ctx->inputs[1];
1458     XFadeContext *s = ctx->priv;
1459     const AVPixFmtDescriptor *pix_desc = av_pix_fmt_desc_get(inlink0->format);
1460
1461     if (inlink0->format != inlink1->format) {
1462         av_log(ctx, AV_LOG_ERROR, "inputs must be of same pixel format\n");
1463         return AVERROR(EINVAL);
1464     }
1465     if (inlink0->w != inlink1->w || inlink0->h != inlink1->h) {
1466         av_log(ctx, AV_LOG_ERROR, "First input link %s parameters "
1467                "(size %dx%d) do not match the corresponding "
1468                "second input link %s parameters (size %dx%d)\n",
1469                ctx->input_pads[0].name, inlink0->w, inlink0->h,
1470                ctx->input_pads[1].name, inlink1->w, inlink1->h);
1471         return AVERROR(EINVAL);
1472     }
1473
1474     if (inlink0->time_base.num != inlink1->time_base.num ||
1475         inlink0->time_base.den != inlink1->time_base.den) {
1476         av_log(ctx, AV_LOG_ERROR, "First input link %s timebase "
1477                "(%d/%d) do not match the corresponding "
1478                "second input link %s timebase (%d/%d)\n",
1479                ctx->input_pads[0].name, inlink0->time_base.num, inlink0->time_base.den,
1480                ctx->input_pads[1].name, inlink1->time_base.num, inlink1->time_base.den);
1481         return AVERROR(EINVAL);
1482     }
1483
1484     if (!inlink0->frame_rate.num || !inlink0->frame_rate.den) {
1485         av_log(ctx, AV_LOG_ERROR, "The inputs needs to be a constant frame rate; "
1486                "current rate of %d/%d is invalid\n", inlink0->frame_rate.num, inlink0->frame_rate.den);
1487         return AVERROR(EINVAL);
1488     }
1489
1490     if (inlink0->frame_rate.num != inlink1->frame_rate.num ||
1491         inlink0->frame_rate.den != inlink1->frame_rate.den) {
1492         av_log(ctx, AV_LOG_ERROR, "First input link %s frame rate "
1493                "(%d/%d) do not match the corresponding "
1494                "second input link %s frame rate (%d/%d)\n",
1495                ctx->input_pads[0].name, inlink0->frame_rate.num, inlink0->frame_rate.den,
1496                ctx->input_pads[1].name, inlink1->frame_rate.num, inlink1->frame_rate.den);
1497         return AVERROR(EINVAL);
1498     }
1499
1500     outlink->w = inlink0->w;
1501     outlink->h = inlink0->h;
1502     outlink->time_base = inlink0->time_base;
1503     outlink->sample_aspect_ratio = inlink0->sample_aspect_ratio;
1504     outlink->frame_rate = inlink0->frame_rate;
1505
1506     s->depth = pix_desc->comp[0].depth;
1507     s->is_rgb = !!(pix_desc->flags & AV_PIX_FMT_FLAG_RGB);
1508     s->nb_planes = av_pix_fmt_count_planes(inlink0->format);
1509     s->max_value = (1 << s->depth) - 1;
1510     s->black[0] = 0;
1511     s->black[1] = s->black[2] = s->is_rgb ? 0 : s->max_value / 2;
1512     s->black[3] = s->max_value;
1513     s->white[0] = s->white[3] = s->max_value;
1514     s->white[1] = s->white[2] = s->is_rgb ? s->max_value : s->max_value / 2;
1515
1516     s->first_pts = s->last_pts = s->pts = AV_NOPTS_VALUE;
1517
1518     if (s->duration)
1519         s->duration_pts = av_rescale_q(s->duration, AV_TIME_BASE_Q, outlink->time_base);
1520     if (s->offset)
1521         s->offset_pts = av_rescale_q(s->offset, AV_TIME_BASE_Q, outlink->time_base);
1522
1523     switch (s->transition) {
1524     case CUSTOM:     s->transitionf = s->depth <= 8 ? custom8_transition     : custom16_transition;     break;
1525     case FADE:       s->transitionf = s->depth <= 8 ? fade8_transition       : fade16_transition;       break;
1526     case WIPELEFT:   s->transitionf = s->depth <= 8 ? wipeleft8_transition   : wipeleft16_transition;   break;
1527     case WIPERIGHT:  s->transitionf = s->depth <= 8 ? wiperight8_transition  : wiperight16_transition;  break;
1528     case WIPEUP:     s->transitionf = s->depth <= 8 ? wipeup8_transition     : wipeup16_transition;     break;
1529     case WIPEDOWN:   s->transitionf = s->depth <= 8 ? wipedown8_transition   : wipedown16_transition;   break;
1530     case SLIDELEFT:  s->transitionf = s->depth <= 8 ? slideleft8_transition  : slideleft16_transition;  break;
1531     case SLIDERIGHT: s->transitionf = s->depth <= 8 ? slideright8_transition : slideright16_transition; break;
1532     case SLIDEUP:    s->transitionf = s->depth <= 8 ? slideup8_transition    : slideup16_transition;    break;
1533     case SLIDEDOWN:  s->transitionf = s->depth <= 8 ? slidedown8_transition  : slidedown16_transition;  break;
1534     case CIRCLECROP: s->transitionf = s->depth <= 8 ? circlecrop8_transition : circlecrop16_transition; break;
1535     case RECTCROP:   s->transitionf = s->depth <= 8 ? rectcrop8_transition   : rectcrop16_transition;   break;
1536     case DISTANCE:   s->transitionf = s->depth <= 8 ? distance8_transition   : distance16_transition;   break;
1537     case FADEBLACK:  s->transitionf = s->depth <= 8 ? fadeblack8_transition  : fadeblack16_transition;  break;
1538     case FADEWHITE:  s->transitionf = s->depth <= 8 ? fadewhite8_transition  : fadewhite16_transition;  break;
1539     case RADIAL:     s->transitionf = s->depth <= 8 ? radial8_transition     : radial16_transition;     break;
1540     case SMOOTHLEFT: s->transitionf = s->depth <= 8 ? smoothleft8_transition : smoothleft16_transition; break;
1541     case SMOOTHRIGHT:s->transitionf = s->depth <= 8 ? smoothright8_transition: smoothright16_transition;break;
1542     case SMOOTHUP:   s->transitionf = s->depth <= 8 ? smoothup8_transition   : smoothup16_transition;   break;
1543     case SMOOTHDOWN: s->transitionf = s->depth <= 8 ? smoothdown8_transition : smoothdown16_transition; break;
1544     case CIRCLEOPEN: s->transitionf = s->depth <= 8 ? circleopen8_transition : circleopen16_transition; break;
1545     case CIRCLECLOSE:s->transitionf = s->depth <= 8 ? circleclose8_transition: circleclose16_transition;break;
1546     case VERTOPEN:   s->transitionf = s->depth <= 8 ? vertopen8_transition   : vertopen16_transition;   break;
1547     case VERTCLOSE:  s->transitionf = s->depth <= 8 ? vertclose8_transition  : vertclose16_transition;  break;
1548     case HORZOPEN:   s->transitionf = s->depth <= 8 ? horzopen8_transition   : horzopen16_transition;   break;
1549     case HORZCLOSE:  s->transitionf = s->depth <= 8 ? horzclose8_transition  : horzclose16_transition;  break;
1550     case DISSOLVE:   s->transitionf = s->depth <= 8 ? dissolve8_transition   : dissolve16_transition;   break;
1551     case PIXELIZE:   s->transitionf = s->depth <= 8 ? pixelize8_transition   : pixelize16_transition;   break;
1552     case DIAGTL:     s->transitionf = s->depth <= 8 ? diagtl8_transition     : diagtl16_transition;     break;
1553     case DIAGTR:     s->transitionf = s->depth <= 8 ? diagtr8_transition     : diagtr16_transition;     break;
1554     case DIAGBL:     s->transitionf = s->depth <= 8 ? diagbl8_transition     : diagbl16_transition;     break;
1555     case DIAGBR:     s->transitionf = s->depth <= 8 ? diagbr8_transition     : diagbr16_transition;     break;
1556     case HLSLICE:    s->transitionf = s->depth <= 8 ? hlslice8_transition    : hlslice16_transition;    break;
1557     case HRSLICE:    s->transitionf = s->depth <= 8 ? hrslice8_transition    : hrslice16_transition;    break;
1558     case VUSLICE:    s->transitionf = s->depth <= 8 ? vuslice8_transition    : vuslice16_transition;    break;
1559     case VDSLICE:    s->transitionf = s->depth <= 8 ? vdslice8_transition    : vdslice16_transition;    break;
1560     case HBLUR:      s->transitionf = s->depth <= 8 ? hblur8_transition      : hblur16_transition;      break;
1561     case FADEGRAYS:  s->transitionf = s->depth <= 8 ? fadegrays8_transition  : fadegrays16_transition;  break;
1562     }
1563
1564     if (s->transition == CUSTOM) {
1565         static const char *const func2_names[]    = {
1566             "a0", "a1", "a2", "a3",
1567             "b0", "b1", "b2", "b3",
1568             NULL
1569         };
1570         double (*func2[])(void *, double, double) = {
1571             a0, a1, a2, a3,
1572             b0, b1, b2, b3,
1573             NULL };
1574         int ret;
1575
1576         if (!s->custom_str)
1577             return AVERROR(EINVAL);
1578         ret = av_expr_parse(&s->e, s->custom_str, var_names,
1579                             NULL, NULL, func2_names, func2, 0, ctx);
1580         if (ret < 0)
1581             return ret;
1582     }
1583
1584     return 0;
1585 }
1586
1587 static int xfade_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
1588 {
1589     XFadeContext *s = ctx->priv;
1590     AVFilterLink *outlink = ctx->outputs[0];
1591     ThreadData *td = arg;
1592     int slice_start = (outlink->h *  jobnr   ) / nb_jobs;
1593     int slice_end   = (outlink->h * (jobnr+1)) / nb_jobs;
1594
1595     s->transitionf(ctx, td->xf[0], td->xf[1], td->out, td->progress, slice_start, slice_end, jobnr);
1596
1597     return 0;
1598 }
1599
1600 static int xfade_frame(AVFilterContext *ctx, AVFrame *a, AVFrame *b)
1601 {
1602     XFadeContext *s = ctx->priv;
1603     AVFilterLink *outlink = ctx->outputs[0];
1604     float progress = av_clipf(1.f - ((float)(s->pts - s->first_pts - s->offset_pts) / s->duration_pts), 0.f, 1.f);
1605     ThreadData td;
1606     AVFrame *out;
1607
1608     out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
1609     if (!out)
1610         return AVERROR(ENOMEM);
1611
1612     td.xf[0] = a, td.xf[1] = b, td.out = out, td.progress = progress;
1613     ctx->internal->execute(ctx, xfade_slice, &td, NULL, FFMIN(outlink->h, ff_filter_get_nb_threads(ctx)));
1614
1615     out->pts = s->pts;
1616
1617     return ff_filter_frame(outlink, out);
1618 }
1619
1620 static int xfade_activate(AVFilterContext *ctx)
1621 {
1622     XFadeContext *s = ctx->priv;
1623     AVFilterLink *outlink = ctx->outputs[0];
1624     AVFrame *in = NULL;
1625     int ret = 0, status;
1626     int64_t pts;
1627
1628     FF_FILTER_FORWARD_STATUS_BACK_ALL(outlink, ctx);
1629
1630     if (s->xfade_is_over) {
1631         ret = ff_inlink_consume_frame(ctx->inputs[1], &in);
1632         if (ret < 0) {
1633             return ret;
1634         } else if (ret > 0) {
1635             in->pts = (in->pts - s->last_pts) + s->pts;
1636             return ff_filter_frame(outlink, in);
1637         } else if (ff_inlink_acknowledge_status(ctx->inputs[1], &status, &pts)) {
1638             ff_outlink_set_status(outlink, status, s->pts);
1639             return 0;
1640         } else if (!ret) {
1641             if (ff_outlink_frame_wanted(outlink)) {
1642                 ff_inlink_request_frame(ctx->inputs[1]);
1643                 return 0;
1644             }
1645         }
1646     }
1647
1648     if (ff_inlink_queued_frames(ctx->inputs[0]) > 0) {
1649         s->xf[0] = ff_inlink_peek_frame(ctx->inputs[0], 0);
1650         if (s->xf[0]) {
1651             if (s->first_pts == AV_NOPTS_VALUE) {
1652                 s->first_pts = s->xf[0]->pts;
1653             }
1654             s->pts = s->xf[0]->pts;
1655             if (s->first_pts + s->offset_pts > s->xf[0]->pts) {
1656                 s->xf[0] = NULL;
1657                 s->need_second = 0;
1658                 ff_inlink_consume_frame(ctx->inputs[0], &in);
1659                 return ff_filter_frame(outlink, in);
1660             }
1661
1662             s->need_second = 1;
1663         }
1664     }
1665
1666     if (s->xf[0] && ff_inlink_queued_frames(ctx->inputs[1]) > 0) {
1667         ff_inlink_consume_frame(ctx->inputs[0], &s->xf[0]);
1668         ff_inlink_consume_frame(ctx->inputs[1], &s->xf[1]);
1669
1670         s->last_pts = s->xf[1]->pts;
1671         s->pts = s->xf[0]->pts;
1672         if (s->xf[0]->pts - (s->first_pts + s->offset_pts) > s->duration_pts)
1673             s->xfade_is_over = 1;
1674         ret = xfade_frame(ctx, s->xf[0], s->xf[1]);
1675         av_frame_free(&s->xf[0]);
1676         av_frame_free(&s->xf[1]);
1677         return ret;
1678     }
1679
1680     if (ff_inlink_queued_frames(ctx->inputs[0]) > 0 &&
1681         ff_inlink_queued_frames(ctx->inputs[1]) > 0) {
1682         ff_filter_set_ready(ctx, 100);
1683         return 0;
1684     }
1685
1686     if (ff_outlink_frame_wanted(outlink)) {
1687         if (!s->eof[0] && ff_outlink_get_status(ctx->inputs[0])) {
1688             s->eof[0] = 1;
1689             s->xfade_is_over = 1;
1690         }
1691         if (!s->eof[1] && ff_outlink_get_status(ctx->inputs[1])) {
1692             s->eof[1] = 1;
1693         }
1694         if (!s->eof[0] && !s->xf[0])
1695             ff_inlink_request_frame(ctx->inputs[0]);
1696         if (!s->eof[1] && (s->need_second || s->eof[0]))
1697             ff_inlink_request_frame(ctx->inputs[1]);
1698         if (s->eof[0] && s->eof[1] && (
1699             ff_inlink_queued_frames(ctx->inputs[0]) <= 0 ||
1700             ff_inlink_queued_frames(ctx->inputs[1]) <= 0))
1701             ff_outlink_set_status(outlink, AVERROR_EOF, AV_NOPTS_VALUE);
1702         return 0;
1703     }
1704
1705     return FFERROR_NOT_READY;
1706 }
1707
1708 static const AVFilterPad xfade_inputs[] = {
1709     {
1710         .name          = "main",
1711         .type          = AVMEDIA_TYPE_VIDEO,
1712     },
1713     {
1714         .name          = "xfade",
1715         .type          = AVMEDIA_TYPE_VIDEO,
1716     },
1717     { NULL }
1718 };
1719
1720 static const AVFilterPad xfade_outputs[] = {
1721     {
1722         .name          = "default",
1723         .type          = AVMEDIA_TYPE_VIDEO,
1724         .config_props  = config_output,
1725     },
1726     { NULL }
1727 };
1728
1729 AVFilter ff_vf_xfade = {
1730     .name          = "xfade",
1731     .description   = NULL_IF_CONFIG_SMALL("Cross fade one video with another video."),
1732     .priv_size     = sizeof(XFadeContext),
1733     .priv_class    = &xfade_class,
1734     .query_formats = query_formats,
1735     .activate      = xfade_activate,
1736     .uninit        = uninit,
1737     .inputs        = xfade_inputs,
1738     .outputs       = xfade_outputs,
1739     .flags         = AVFILTER_FLAG_SLICE_THREADS,
1740 };