]> git.sesse.net Git - ffmpeg/blob - libavfilter/vf_xfade.c
avfilter/vf_xfade: add horzopen/close 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     NB_TRANSITIONS,
59 };
60
61 typedef struct XFadeContext {
62     const AVClass *class;
63
64     int     transition;
65     int64_t duration;
66     int64_t offset;
67     char   *custom_str;
68
69     int nb_planes;
70     int depth;
71
72     int64_t duration_pts;
73     int64_t offset_pts;
74     int64_t first_pts;
75     int64_t last_pts;
76     int64_t pts;
77     int xfade_is_over;
78     int need_second;
79     int eof[2];
80     AVFrame *xf[2];
81     int max_value;
82     uint16_t black[4];
83     uint16_t white[4];
84
85     void (*transitionf)(AVFilterContext *ctx, const AVFrame *a, const AVFrame *b, AVFrame *out, float progress,
86                         int slice_start, int slice_end, int jobnr);
87
88     AVExpr *e;
89 } XFadeContext;
90
91 static const char *const var_names[] = {   "X",   "Y",   "W",   "H",   "A",   "B",   "PLANE",          "P",        NULL };
92 enum                                   { VAR_X, VAR_Y, VAR_W, VAR_H, VAR_A, VAR_B, VAR_PLANE, VAR_PROGRESS, VAR_VARS_NB };
93
94 typedef struct ThreadData {
95     const AVFrame *xf[2];
96     AVFrame *out;
97     float progress;
98 } ThreadData;
99
100 static int query_formats(AVFilterContext *ctx)
101 {
102     static const enum AVPixelFormat pix_fmts[] = {
103         AV_PIX_FMT_YUVA444P,
104         AV_PIX_FMT_YUVJ444P,
105         AV_PIX_FMT_YUV444P,
106         AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRAP, AV_PIX_FMT_GRAY8,
107         AV_PIX_FMT_YUVA444P9, AV_PIX_FMT_GBRP9,
108         AV_PIX_FMT_YUV444P10,
109         AV_PIX_FMT_YUVA444P10,
110         AV_PIX_FMT_GBRP10, AV_PIX_FMT_GBRAP10, AV_PIX_FMT_GRAY10,
111         AV_PIX_FMT_YUV444P12,
112         AV_PIX_FMT_YUVA444P12,
113         AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRAP12, AV_PIX_FMT_GRAY12,
114         AV_PIX_FMT_YUV444P14, AV_PIX_FMT_GBRP14,
115         AV_PIX_FMT_YUV444P16,
116         AV_PIX_FMT_YUVA444P16,
117         AV_PIX_FMT_GBRP16, AV_PIX_FMT_GBRAP16, AV_PIX_FMT_GRAY16,
118         AV_PIX_FMT_NONE
119     };
120
121     AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
122     if (!fmts_list)
123         return AVERROR(ENOMEM);
124     return ff_set_common_formats(ctx, fmts_list);
125 }
126
127 static av_cold void uninit(AVFilterContext *ctx)
128 {
129     XFadeContext *s = ctx->priv;
130
131     av_expr_free(s->e);
132 }
133
134 #define OFFSET(x) offsetof(XFadeContext, x)
135 #define FLAGS (AV_OPT_FLAG_FILTERING_PARAM | AV_OPT_FLAG_VIDEO_PARAM)
136
137 static const AVOption xfade_options[] = {
138     { "transition", "set cross fade transition", OFFSET(transition), AV_OPT_TYPE_INT, {.i64=FADE}, -1, NB_TRANSITIONS-1, FLAGS, "transition" },
139     {   "custom",    "custom transition",     0, AV_OPT_TYPE_CONST, {.i64=CUSTOM},    0, 0, FLAGS, "transition" },
140     {   "fade",      "fade transition",       0, AV_OPT_TYPE_CONST, {.i64=FADE},      0, 0, FLAGS, "transition" },
141     {   "wipeleft",  "wipe left transition",  0, AV_OPT_TYPE_CONST, {.i64=WIPELEFT},  0, 0, FLAGS, "transition" },
142     {   "wiperight", "wipe right transition", 0, AV_OPT_TYPE_CONST, {.i64=WIPERIGHT}, 0, 0, FLAGS, "transition" },
143     {   "wipeup",    "wipe up transition",    0, AV_OPT_TYPE_CONST, {.i64=WIPEUP},    0, 0, FLAGS, "transition" },
144     {   "wipedown",  "wipe down transition",  0, AV_OPT_TYPE_CONST, {.i64=WIPEDOWN},  0, 0, FLAGS, "transition" },
145     {   "slideleft",  "slide left transition",  0, AV_OPT_TYPE_CONST, {.i64=SLIDELEFT},  0, 0, FLAGS, "transition" },
146     {   "slideright", "slide right transition", 0, AV_OPT_TYPE_CONST, {.i64=SLIDERIGHT}, 0, 0, FLAGS, "transition" },
147     {   "slideup",    "slide up transition",    0, AV_OPT_TYPE_CONST, {.i64=SLIDEUP},    0, 0, FLAGS, "transition" },
148     {   "slidedown",  "slide down transition",  0, AV_OPT_TYPE_CONST, {.i64=SLIDEDOWN},  0, 0, FLAGS, "transition" },
149     {   "circlecrop", "circle crop transition", 0, AV_OPT_TYPE_CONST, {.i64=CIRCLECROP}, 0, 0, FLAGS, "transition" },
150     {   "rectcrop",   "rect crop transition",   0, AV_OPT_TYPE_CONST, {.i64=RECTCROP},   0, 0, FLAGS, "transition" },
151     {   "distance",   "distance transition",    0, AV_OPT_TYPE_CONST, {.i64=DISTANCE},   0, 0, FLAGS, "transition" },
152     {   "fadeblack",  "fadeblack transition",   0, AV_OPT_TYPE_CONST, {.i64=FADEBLACK},  0, 0, FLAGS, "transition" },
153     {   "fadewhite",  "fadewhite transition",   0, AV_OPT_TYPE_CONST, {.i64=FADEWHITE},  0, 0, FLAGS, "transition" },
154     {   "radial",     "radial transition",      0, AV_OPT_TYPE_CONST, {.i64=RADIAL},     0, 0, FLAGS, "transition" },
155     {   "smoothleft", "smoothleft transition",  0, AV_OPT_TYPE_CONST, {.i64=SMOOTHLEFT}, 0, 0, FLAGS, "transition" },
156     {   "smoothright","smoothright transition", 0, AV_OPT_TYPE_CONST, {.i64=SMOOTHRIGHT},0, 0, FLAGS, "transition" },
157     {   "smoothup",   "smoothup transition",    0, AV_OPT_TYPE_CONST, {.i64=SMOOTHUP},   0, 0, FLAGS, "transition" },
158     {   "smoothdown", "smoothdown transition",  0, AV_OPT_TYPE_CONST, {.i64=SMOOTHDOWN}, 0, 0, FLAGS, "transition" },
159     {   "circleopen", "circleopen transition",  0, AV_OPT_TYPE_CONST, {.i64=CIRCLEOPEN}, 0, 0, FLAGS, "transition" },
160     {   "circleclose","circleclose transition", 0, AV_OPT_TYPE_CONST, {.i64=CIRCLECLOSE},0, 0, FLAGS, "transition" },
161     {   "vertopen",   "vert open transition",   0, AV_OPT_TYPE_CONST, {.i64=VERTOPEN},   0, 0, FLAGS, "transition" },
162     {   "vertclose",  "vert close transition",  0, AV_OPT_TYPE_CONST, {.i64=VERTCLOSE},  0, 0, FLAGS, "transition" },
163     {   "horzopen",   "horz open transition",   0, AV_OPT_TYPE_CONST, {.i64=HORZOPEN},   0, 0, FLAGS, "transition" },
164     {   "horzclose",  "horz close transition",  0, AV_OPT_TYPE_CONST, {.i64=HORZCLOSE},  0, 0, FLAGS, "transition" },
165     { "duration", "set cross fade duration", OFFSET(duration), AV_OPT_TYPE_DURATION, {.i64=1000000}, 0, 60000000, FLAGS },
166     { "offset",   "set cross fade start relative to first input stream", OFFSET(offset), AV_OPT_TYPE_DURATION, {.i64=0}, INT64_MIN, INT64_MAX, FLAGS },
167     { "expr",   "set expression for custom transition", OFFSET(custom_str), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, FLAGS },
168     { NULL }
169 };
170
171 AVFILTER_DEFINE_CLASS(xfade);
172
173 #define CUSTOM_TRANSITION(name, type, div)                                           \
174 static void custom##name##_transition(AVFilterContext *ctx,                          \
175                             const AVFrame *a, const AVFrame *b, AVFrame *out,        \
176                             float progress,                                          \
177                             int slice_start, int slice_end, int jobnr)               \
178 {                                                                                    \
179     XFadeContext *s = ctx->priv;                                                     \
180     const int height = slice_end - slice_start;                                      \
181                                                                                      \
182     double values[VAR_VARS_NB];                                                      \
183     values[VAR_W] = out->width;                                                      \
184     values[VAR_H] = out->height;                                                     \
185     values[VAR_PROGRESS] = progress;                                                 \
186                                                                                      \
187     for (int p = 0; p < s->nb_planes; p++) {                                         \
188         const type *xf0 = (const type *)(a->data[p] + slice_start * a->linesize[p]); \
189         const type *xf1 = (const type *)(b->data[p] + slice_start * b->linesize[p]); \
190         type *dst = (type *)(out->data[p] + slice_start * out->linesize[p]);         \
191                                                                                      \
192         values[VAR_PLANE] = p;                                                       \
193                                                                                      \
194         for (int y = 0; y < height; y++) {                                           \
195             values[VAR_Y] = slice_start + y;                                         \
196             for (int x = 0; x < out->width; x++) {                                   \
197                 values[VAR_X] = x;                                                   \
198                 values[VAR_A] = xf0[x];                                              \
199                 values[VAR_B] = xf1[x];                                              \
200                 dst[x] = av_expr_eval(s->e, values, s);                              \
201             }                                                                        \
202                                                                                      \
203             dst += out->linesize[p] / div;                                           \
204             xf0 += a->linesize[p] / div;                                             \
205             xf1 += b->linesize[p] / div;                                             \
206         }                                                                            \
207     }                                                                                \
208 }
209
210 CUSTOM_TRANSITION(8, uint8_t, 1)
211 CUSTOM_TRANSITION(16, uint16_t, 2)
212
213 static inline float mix(float a, float b, float mix)
214 {
215     return a * mix + b * (1.f - mix);
216 }
217
218 static inline float smoothstep(float edge0, float edge1, float x)
219 {
220     float t;
221
222     t = av_clipf((x - edge0) / (edge1 - edge0), 0.f, 1.f);
223
224     return t * t * (3.f - 2.f * t);
225 }
226
227 #define FADE_TRANSITION(name, type, div)                                             \
228 static void fade##name##_transition(AVFilterContext *ctx,                            \
229                             const AVFrame *a, const AVFrame *b, AVFrame *out,        \
230                             float progress,                                          \
231                             int slice_start, int slice_end, int jobnr)               \
232 {                                                                                    \
233     XFadeContext *s = ctx->priv;                                                     \
234     const int height = slice_end - slice_start;                                      \
235                                                                                      \
236     for (int p = 0; p < s->nb_planes; p++) {                                         \
237         const type *xf0 = (const type *)(a->data[p] + slice_start * a->linesize[p]); \
238         const type *xf1 = (const type *)(b->data[p] + slice_start * b->linesize[p]); \
239         type *dst = (type *)(out->data[p] + slice_start * out->linesize[p]);         \
240                                                                                      \
241         for (int y = 0; y < height; y++) {                                           \
242             for (int x = 0; x < out->width; x++) {                                   \
243                 dst[x] = mix(xf0[x], xf1[x], progress);                              \
244             }                                                                        \
245                                                                                      \
246             dst += out->linesize[p] / div;                                           \
247             xf0 += a->linesize[p] / div;                                             \
248             xf1 += b->linesize[p] / div;                                             \
249         }                                                                            \
250     }                                                                                \
251 }
252
253 FADE_TRANSITION(8, uint8_t, 1)
254 FADE_TRANSITION(16, uint16_t, 2)
255
256 #define WIPELEFT_TRANSITION(name, type, div)                                         \
257 static void wipeleft##name##_transition(AVFilterContext *ctx,                        \
258                                 const AVFrame *a, const AVFrame *b, AVFrame *out,    \
259                                 float progress,                                      \
260                                 int slice_start, int slice_end, int jobnr)           \
261 {                                                                                    \
262     XFadeContext *s = ctx->priv;                                                     \
263     const int height = slice_end - slice_start;                                      \
264     const int z = out->width * progress;                                             \
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] = x > z ? xf1[x] : xf0[x];                                    \
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 WIPELEFT_TRANSITION(8, uint8_t, 1)
284 WIPELEFT_TRANSITION(16, uint16_t, 2)
285
286 #define WIPERIGHT_TRANSITION(name, type, div)                                        \
287 static void wiperight##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 * (1.f - 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 ? xf0[x] : xf1[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 WIPERIGHT_TRANSITION(8, uint8_t, 1)
314 WIPERIGHT_TRANSITION(16, uint16_t, 2)
315
316 #define WIPEUP_TRANSITION(name, type, div)                                           \
317 static void wipeup##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->height * 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] = slice_start + y > z ? xf1[x] : xf0[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 WIPEUP_TRANSITION(8, uint8_t, 1)
344 WIPEUP_TRANSITION(16, uint16_t, 2)
345
346 #define WIPEDOWN_TRANSITION(name, type, div)                                         \
347 static void wipedown##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 * (1.f - 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 ? xf0[x] : xf1[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 WIPEDOWN_TRANSITION(8, uint8_t, 1)
374 WIPEDOWN_TRANSITION(16, uint16_t, 2)
375
376 #define SLIDELEFT_TRANSITION(name, type, div)                                        \
377 static void slideleft##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 width = out->width;                                                    \
385     const int z = -progress * width;                                                 \
386                                                                                      \
387     for (int p = 0; p < s->nb_planes; p++) {                                         \
388         const type *xf0 = (const type *)(a->data[p] + slice_start * a->linesize[p]); \
389         const type *xf1 = (const type *)(b->data[p] + slice_start * b->linesize[p]); \
390         type *dst = (type *)(out->data[p] + slice_start * out->linesize[p]);         \
391                                                                                      \
392         for (int y = 0; y < height; y++) {                                           \
393             for (int x = 0; x < width; x++) {                                        \
394                 const int zx = z + x;                                                \
395                 const int zz = zx % width + width * (zx < 0);                        \
396                 dst[x] = (zx > 0) && (zx < width) ? xf1[zz] : xf0[zz];               \
397             }                                                                        \
398                                                                                      \
399             dst += out->linesize[p] / div;                                           \
400             xf0 += a->linesize[p] / div;                                             \
401             xf1 += b->linesize[p] / div;                                             \
402         }                                                                            \
403     }                                                                                \
404 }
405
406 SLIDELEFT_TRANSITION(8, uint8_t, 1)
407 SLIDELEFT_TRANSITION(16, uint16_t, 2)
408
409 #define SLIDERIGHT_TRANSITION(name, type, div)                                       \
410 static void slideright##name##_transition(AVFilterContext *ctx,                      \
411                                   const AVFrame *a, const AVFrame *b, AVFrame *out,  \
412                                   float progress,                                    \
413                                   int slice_start, int slice_end, int jobnr)         \
414 {                                                                                    \
415     XFadeContext *s = ctx->priv;                                                     \
416     const int height = slice_end - slice_start;                                      \
417     const int width = out->width;                                                    \
418     const int z = progress * width;                                                  \
419                                                                                      \
420     for (int p = 0; p < s->nb_planes; p++) {                                         \
421         const type *xf0 = (const type *)(a->data[p] + slice_start * a->linesize[p]); \
422         const type *xf1 = (const type *)(b->data[p] + slice_start * b->linesize[p]); \
423         type *dst = (type *)(out->data[p] + slice_start * out->linesize[p]);         \
424                                                                                      \
425         for (int y = 0; y < height; y++) {                                           \
426             for (int x = 0; x < out->width; x++) {                                   \
427                 const int zx = z + x;                                                \
428                 const int zz = zx % width + width * (zx < 0);                        \
429                 dst[x] = (zx > 0) && (zx < width) ? xf1[zz] : xf0[zz];               \
430             }                                                                        \
431                                                                                      \
432             dst += out->linesize[p] / div;                                           \
433             xf0 += a->linesize[p] / div;                                             \
434             xf1 += b->linesize[p] / div;                                             \
435         }                                                                            \
436     }                                                                                \
437 }
438
439 SLIDERIGHT_TRANSITION(8, uint8_t, 1)
440 SLIDERIGHT_TRANSITION(16, uint16_t, 2)
441
442 #define SLIDEUP_TRANSITION(name, type, div)                                         \
443 static void slideup##name##_transition(AVFilterContext *ctx,                        \
444                                const AVFrame *a, const AVFrame *b, AVFrame *out,    \
445                                float progress,                                      \
446                                int slice_start, int slice_end, int jobnr)           \
447 {                                                                                   \
448     XFadeContext *s = ctx->priv;                                                    \
449     const int height = out->height;                                                 \
450     const int z = -progress * height;                                               \
451                                                                                     \
452     for (int p = 0; p < s->nb_planes; p++) {                                        \
453         type *dst = (type *)(out->data[p] + slice_start * out->linesize[p]);        \
454                                                                                     \
455         for (int y = slice_start; y < slice_end; y++) {                             \
456             const int zy = z + y;                                                   \
457             const int zz = zy % height + height * (zy < 0);                         \
458             const type *xf0 = (const type *)(a->data[p] + zz * a->linesize[p]);     \
459             const type *xf1 = (const type *)(b->data[p] + zz * b->linesize[p]);     \
460                                                                                     \
461             for (int x = 0; x < out->width; x++) {                                  \
462                 dst[x] = (zy > 0) && (zy < height) ? xf1[x] : xf0[x];               \
463             }                                                                       \
464                                                                                     \
465             dst += out->linesize[p] / div;                                          \
466         }                                                                           \
467     }                                                                               \
468 }
469
470 SLIDEUP_TRANSITION(8, uint8_t, 1)
471 SLIDEUP_TRANSITION(16, uint16_t, 2)
472
473 #define SLIDEDOWN_TRANSITION(name, type, div)                                       \
474 static void slidedown##name##_transition(AVFilterContext *ctx,                      \
475                                  const AVFrame *a, const AVFrame *b, AVFrame *out,  \
476                                  float progress,                                    \
477                                  int slice_start, int slice_end, int jobnr)         \
478 {                                                                                   \
479     XFadeContext *s = ctx->priv;                                                    \
480     const int height = out->height;                                                 \
481     const int z = progress * height;                                                \
482                                                                                     \
483     for (int p = 0; p < s->nb_planes; p++) {                                        \
484         type *dst = (type *)(out->data[p] + slice_start * out->linesize[p]);        \
485                                                                                     \
486         for (int y = slice_start; y < slice_end; y++) {                             \
487             const int zy = z + y;                                                   \
488             const int zz = zy % height + height * (zy < 0);                         \
489             const type *xf0 = (const type *)(a->data[p] + zz * a->linesize[p]);     \
490             const type *xf1 = (const type *)(b->data[p] + zz * b->linesize[p]);     \
491                                                                                     \
492             for (int x = 0; x < out->width; x++) {                                  \
493                 dst[x] = (zy > 0) && (zy < height) ? xf1[x] : xf0[x];               \
494             }                                                                       \
495                                                                                     \
496             dst += out->linesize[p] / div;                                          \
497         }                                                                           \
498     }                                                                               \
499 }
500
501 SLIDEDOWN_TRANSITION(8, uint8_t, 1)
502 SLIDEDOWN_TRANSITION(16, uint16_t, 2)
503
504 #define CIRCLECROP_TRANSITION(name, type, div)                                      \
505 static void circlecrop##name##_transition(AVFilterContext *ctx,                     \
506                                  const AVFrame *a, const AVFrame *b, AVFrame *out,  \
507                                  float progress,                                    \
508                                  int slice_start, int slice_end, int jobnr)         \
509 {                                                                                   \
510     XFadeContext *s = ctx->priv;                                                    \
511     const int width = out->width;                                                   \
512     const int height = out->height;                                                 \
513     float z = powf(2.f * fabsf(progress - 0.5f), 3.f) * hypotf(width/2, height/2);  \
514                                                                                     \
515     for (int p = 0; p < s->nb_planes; p++) {                                        \
516         const int bg = s->black[p];                                                 \
517         type *dst = (type *)(out->data[p] + slice_start * out->linesize[p]);        \
518                                                                                     \
519         for (int y = slice_start; y < slice_end; y++) {                             \
520             const type *xf0 = (const type *)(a->data[p] + y * a->linesize[p]);      \
521             const type *xf1 = (const type *)(b->data[p] + y * b->linesize[p]);      \
522                                                                                     \
523             for (int x = 0; x < width; x++) {                                       \
524                 float dist = hypotf(x - width / 2, y - height / 2);                 \
525                 int val = progress < 0.5f ? xf1[x] : xf0[x];                        \
526                 dst[x] = (z < dist) ? bg : val;                                     \
527             }                                                                       \
528                                                                                     \
529             dst += out->linesize[p] / div;                                          \
530         }                                                                           \
531     }                                                                               \
532 }
533
534 CIRCLECROP_TRANSITION(8, uint8_t, 1)
535 CIRCLECROP_TRANSITION(16, uint16_t, 2)
536
537 #define RECTCROP_TRANSITION(name, type, div)                                        \
538 static void rectcrop##name##_transition(AVFilterContext *ctx,                       \
539                                  const AVFrame *a, const AVFrame *b, AVFrame *out,  \
540                                  float progress,                                    \
541                                  int slice_start, int slice_end, int jobnr)         \
542 {                                                                                   \
543     XFadeContext *s = ctx->priv;                                                    \
544     const int width = out->width;                                                   \
545     const int height = out->height;                                                 \
546     int zh = fabsf(progress - 0.5f) * height;                                       \
547     int zw = fabsf(progress - 0.5f) * width;                                        \
548                                                                                     \
549     for (int p = 0; p < s->nb_planes; p++) {                                        \
550         const int bg = s->black[p];                                                 \
551         type *dst = (type *)(out->data[p] + slice_start * out->linesize[p]);        \
552                                                                                     \
553         for (int y = slice_start; y < slice_end; y++) {                             \
554             const type *xf0 = (const type *)(a->data[p] + y * a->linesize[p]);      \
555             const type *xf1 = (const type *)(b->data[p] + y * b->linesize[p]);      \
556                                                                                     \
557             for (int x = 0; x < width; x++) {                                       \
558                 int dist = FFABS(x - width  / 2) < zw &&                            \
559                            FFABS(y - height / 2) < zh;                              \
560                 int val = progress < 0.5f ? xf1[x] : xf0[x];                        \
561                 dst[x] = !dist ? bg : val;                                          \
562             }                                                                       \
563                                                                                     \
564             dst += out->linesize[p] / div;                                          \
565         }                                                                           \
566     }                                                                               \
567 }
568
569 RECTCROP_TRANSITION(8, uint8_t, 1)
570 RECTCROP_TRANSITION(16, uint16_t, 2)
571
572 #define DISTANCE_TRANSITION(name, type, div)                                        \
573 static void distance##name##_transition(AVFilterContext *ctx,                       \
574                                  const AVFrame *a, const AVFrame *b, AVFrame *out,  \
575                                  float progress,                                    \
576                                  int slice_start, int slice_end, int jobnr)         \
577 {                                                                                   \
578     XFadeContext *s = ctx->priv;                                                    \
579     const int width = out->width;                                                   \
580     const float max = s->max_value;                                                 \
581                                                                                     \
582     for (int y = slice_start; y < slice_end; y++) {                                 \
583         for (int x = 0; x < width; x++) {                                           \
584             float dist = 0.f;                                                       \
585             for (int p = 0; p < s->nb_planes; p++) {                                \
586                 const type *xf0 = (const type *)(a->data[p] + y * a->linesize[p]);  \
587                 const type *xf1 = (const type *)(b->data[p] + y * b->linesize[p]);  \
588                                                                                     \
589                 dist += (xf0[x] / max - xf1[x] / max) *                             \
590                         (xf0[x] / max - xf1[x] / max);                              \
591             }                                                                       \
592                                                                                     \
593             dist = sqrtf(dist) <= progress;                                         \
594             for (int p = 0; p < s->nb_planes; p++) {                                \
595                 const type *xf0 = (const type *)(a->data[p] + y * a->linesize[p]);  \
596                 const type *xf1 = (const type *)(b->data[p] + y * b->linesize[p]);  \
597                 type *dst = (type *)(out->data[p] + y * out->linesize[p]);          \
598                 dst[x] = mix(mix(xf0[x], xf1[x], dist), xf1[x], progress);          \
599             }                                                                       \
600         }                                                                           \
601     }                                                                               \
602 }
603
604 DISTANCE_TRANSITION(8, uint8_t, 1)
605 DISTANCE_TRANSITION(16, uint16_t, 2)
606
607 #define FADEBLACK_TRANSITION(name, type, div)                                        \
608 static void fadeblack##name##_transition(AVFilterContext *ctx,                       \
609                             const AVFrame *a, const AVFrame *b, AVFrame *out,        \
610                             float progress,                                          \
611                             int slice_start, int slice_end, int jobnr)               \
612 {                                                                                    \
613     XFadeContext *s = ctx->priv;                                                     \
614     const int height = slice_end - slice_start;                                      \
615     const float phase = 0.2f;                                                        \
616                                                                                      \
617     for (int p = 0; p < s->nb_planes; p++) {                                         \
618         const type *xf0 = (const type *)(a->data[p] + slice_start * a->linesize[p]); \
619         const type *xf1 = (const type *)(b->data[p] + slice_start * b->linesize[p]); \
620         type *dst = (type *)(out->data[p] + slice_start * out->linesize[p]);         \
621         const int bg = s->black[p];                                                  \
622                                                                                      \
623         for (int y = 0; y < height; y++) {                                           \
624             for (int x = 0; x < out->width; x++) {                                   \
625                 dst[x] = mix(mix(xf0[x], bg, smoothstep(1.f-phase, 1.f, progress)),  \
626                          mix(bg, xf1[x], smoothstep(phase, 1.f, progress)),          \
627                              progress);                                              \
628             }                                                                        \
629                                                                                      \
630             dst += out->linesize[p] / div;                                           \
631             xf0 += a->linesize[p] / div;                                             \
632             xf1 += b->linesize[p] / div;                                             \
633         }                                                                            \
634     }                                                                                \
635 }
636
637 FADEBLACK_TRANSITION(8, uint8_t, 1)
638 FADEBLACK_TRANSITION(16, uint16_t, 2)
639
640 #define FADEWHITE_TRANSITION(name, type, div)                                        \
641 static void fadewhite##name##_transition(AVFilterContext *ctx,                       \
642                             const AVFrame *a, const AVFrame *b, AVFrame *out,        \
643                             float progress,                                          \
644                             int slice_start, int slice_end, int jobnr)               \
645 {                                                                                    \
646     XFadeContext *s = ctx->priv;                                                     \
647     const int height = slice_end - slice_start;                                      \
648     const float phase = 0.2f;                                                        \
649                                                                                      \
650     for (int p = 0; p < s->nb_planes; p++) {                                         \
651         const type *xf0 = (const type *)(a->data[p] + slice_start * a->linesize[p]); \
652         const type *xf1 = (const type *)(b->data[p] + slice_start * b->linesize[p]); \
653         type *dst = (type *)(out->data[p] + slice_start * out->linesize[p]);         \
654         const int bg = s->white[p];                                                  \
655                                                                                      \
656         for (int y = 0; y < height; y++) {                                           \
657             for (int x = 0; x < out->width; x++) {                                   \
658                 dst[x] = mix(mix(xf0[x], bg, smoothstep(1.f-phase, 1.f, progress)),  \
659                          mix(bg, xf1[x], smoothstep(phase, 1.f, progress)),          \
660                              progress);                                              \
661             }                                                                        \
662                                                                                      \
663             dst += out->linesize[p] / div;                                           \
664             xf0 += a->linesize[p] / div;                                             \
665             xf1 += b->linesize[p] / div;                                             \
666         }                                                                            \
667     }                                                                                \
668 }
669
670 FADEWHITE_TRANSITION(8, uint8_t, 1)
671 FADEWHITE_TRANSITION(16, uint16_t, 2)
672
673 #define RADIAL_TRANSITION(name, type, div)                                           \
674 static void radial##name##_transition(AVFilterContext *ctx,                          \
675                             const AVFrame *a, const AVFrame *b, AVFrame *out,        \
676                             float progress,                                          \
677                             int slice_start, int slice_end, int jobnr)               \
678 {                                                                                    \
679     XFadeContext *s = ctx->priv;                                                     \
680     const int width = out->width;                                                    \
681     const int height = out->height;                                                  \
682                                                                                      \
683     for (int y = slice_start; y < slice_end; y++) {                                  \
684         for (int x = 0; x < width; x++) {                                            \
685             const float smooth = atan2f(x - width / 2, y - height / 2) -             \
686                                  (progress - 0.5f) * (M_PI * 2.5f);                  \
687             for (int p = 0; p < s->nb_planes; p++) {                                 \
688                 const type *xf0 = (const type *)(a->data[p] + y * a->linesize[p]);   \
689                 const type *xf1 = (const type *)(b->data[p] + y * b->linesize[p]);   \
690                 type *dst = (type *)(out->data[p] + y * out->linesize[p]);           \
691                                                                                      \
692                 dst[x] = mix(xf1[x], xf0[x], smoothstep(0.f, 1.f, smooth));          \
693             }                                                                        \
694         }                                                                            \
695     }                                                                                \
696 }
697
698 RADIAL_TRANSITION(8, uint8_t, 1)
699 RADIAL_TRANSITION(16, uint16_t, 2)
700
701 #define SMOOTHLEFT_TRANSITION(name, type, div)                                       \
702 static void smoothleft##name##_transition(AVFilterContext *ctx,                      \
703                             const AVFrame *a, const AVFrame *b, AVFrame *out,        \
704                             float progress,                                          \
705                             int slice_start, int slice_end, int jobnr)               \
706 {                                                                                    \
707     XFadeContext *s = ctx->priv;                                                     \
708     const int width = out->width;                                                    \
709     const float w = width;                                                           \
710                                                                                      \
711     for (int y = slice_start; y < slice_end; y++) {                                  \
712         for (int x = 0; x < width; x++) {                                            \
713             const float smooth = 1.f + x / w - progress * 2.f;                       \
714                                                                                      \
715             for (int p = 0; p < s->nb_planes; p++) {                                 \
716                 const type *xf0 = (const type *)(a->data[p] + y * a->linesize[p]);   \
717                 const type *xf1 = (const type *)(b->data[p] + y * b->linesize[p]);   \
718                 type *dst = (type *)(out->data[p] + y * out->linesize[p]);           \
719                                                                                      \
720                 dst[x] = mix(xf1[x], xf0[x], smoothstep(0.f, 1.f, smooth));          \
721             }                                                                        \
722         }                                                                            \
723     }                                                                                \
724 }
725
726 SMOOTHLEFT_TRANSITION(8, uint8_t, 1)
727 SMOOTHLEFT_TRANSITION(16, uint16_t, 2)
728
729 #define SMOOTHRIGHT_TRANSITION(name, type, div)                                      \
730 static void smoothright##name##_transition(AVFilterContext *ctx,                     \
731                             const AVFrame *a, const AVFrame *b, AVFrame *out,        \
732                             float progress,                                          \
733                             int slice_start, int slice_end, int jobnr)               \
734 {                                                                                    \
735     XFadeContext *s = ctx->priv;                                                     \
736     const int width = out->width;                                                    \
737     const float w = width;                                                           \
738                                                                                      \
739     for (int y = slice_start; y < slice_end; y++) {                                  \
740         for (int x = 0; x < width; x++) {                                            \
741             const float smooth = 1.f + (w - 1 - x) / w - progress * 2.f;             \
742                                                                                      \
743             for (int p = 0; p < s->nb_planes; p++) {                                 \
744                 const type *xf0 = (const type *)(a->data[p] + y * a->linesize[p]);   \
745                 const type *xf1 = (const type *)(b->data[p] + y * b->linesize[p]);   \
746                 type *dst = (type *)(out->data[p] + y * out->linesize[p]);           \
747                                                                                      \
748                 dst[x] = mix(xf1[x], xf0[x], smoothstep(0.f, 1.f, smooth));          \
749             }                                                                        \
750         }                                                                            \
751     }                                                                                \
752 }
753
754 SMOOTHRIGHT_TRANSITION(8, uint8_t, 1)
755 SMOOTHRIGHT_TRANSITION(16, uint16_t, 2)
756
757 #define SMOOTHUP_TRANSITION(name, type, div)                                         \
758 static void smoothup##name##_transition(AVFilterContext *ctx,                        \
759                             const AVFrame *a, const AVFrame *b, AVFrame *out,        \
760                             float progress,                                          \
761                             int slice_start, int slice_end, int jobnr)               \
762 {                                                                                    \
763     XFadeContext *s = ctx->priv;                                                     \
764     const int width = out->width;                                                    \
765     const float h = out->height;                                                     \
766                                                                                      \
767     for (int y = slice_start; y < slice_end; y++) {                                  \
768         const float smooth = 1.f + y / h - progress * 2.f;                           \
769         for (int x = 0; x < width; x++) {                                            \
770             for (int p = 0; p < s->nb_planes; p++) {                                 \
771                 const type *xf0 = (const type *)(a->data[p] + y * a->linesize[p]);   \
772                 const type *xf1 = (const type *)(b->data[p] + y * b->linesize[p]);   \
773                 type *dst = (type *)(out->data[p] + y * out->linesize[p]);           \
774                                                                                      \
775                 dst[x] = mix(xf1[x], xf0[x], smoothstep(0.f, 1.f, smooth));          \
776             }                                                                        \
777         }                                                                            \
778     }                                                                                \
779 }
780
781 SMOOTHUP_TRANSITION(8, uint8_t, 1)
782 SMOOTHUP_TRANSITION(16, uint16_t, 2)
783
784 #define SMOOTHDOWN_TRANSITION(name, type, div)                                       \
785 static void smoothdown##name##_transition(AVFilterContext *ctx,                      \
786                             const AVFrame *a, const AVFrame *b, AVFrame *out,        \
787                             float progress,                                          \
788                             int slice_start, int slice_end, int jobnr)               \
789 {                                                                                    \
790     XFadeContext *s = ctx->priv;                                                     \
791     const int width = out->width;                                                    \
792     const float h = out->height;                                                     \
793                                                                                      \
794     for (int y = slice_start; y < slice_end; y++) {                                  \
795         const float smooth = 1.f + (h - 1 - y) / h - progress * 2.f;                 \
796         for (int x = 0; x < width; x++) {                                            \
797             for (int p = 0; p < s->nb_planes; p++) {                                 \
798                 const type *xf0 = (const type *)(a->data[p] + y * a->linesize[p]);   \
799                 const type *xf1 = (const type *)(b->data[p] + y * b->linesize[p]);   \
800                 type *dst = (type *)(out->data[p] + y * out->linesize[p]);           \
801                                                                                      \
802                 dst[x] = mix(xf1[x], xf0[x], smoothstep(0.f, 1.f, smooth));          \
803             }                                                                        \
804         }                                                                            \
805     }                                                                                \
806 }
807
808 SMOOTHDOWN_TRANSITION(8, uint8_t, 1)
809 SMOOTHDOWN_TRANSITION(16, uint16_t, 2)
810
811 #define CIRCLEOPEN_TRANSITION(name, type, div)                                       \
812 static void circleopen##name##_transition(AVFilterContext *ctx,                      \
813                             const AVFrame *a, const AVFrame *b, AVFrame *out,        \
814                             float progress,                                          \
815                             int slice_start, int slice_end, int jobnr)               \
816 {                                                                                    \
817     XFadeContext *s = ctx->priv;                                                     \
818     const int width = out->width;                                                    \
819     const int height = out->height;                                                  \
820     const float z = hypotf(width / 2, height / 2);                                   \
821     const float p = (progress - 0.5f) * 3.f;                                         \
822                                                                                      \
823     for (int y = slice_start; y < slice_end; y++) {                                  \
824         for (int x = 0; x < width; x++) {                                            \
825             const float smooth = hypotf(x - width / 2, y - height / 2) / z + p;      \
826             for (int p = 0; p < s->nb_planes; p++) {                                 \
827                 const type *xf0 = (const type *)(a->data[p] + y * a->linesize[p]);   \
828                 const type *xf1 = (const type *)(b->data[p] + y * b->linesize[p]);   \
829                 type *dst = (type *)(out->data[p] + y * out->linesize[p]);           \
830                                                                                      \
831                 dst[x] = mix(xf0[x], xf1[x], smoothstep(0.f, 1.f, smooth));          \
832             }                                                                        \
833         }                                                                            \
834     }                                                                                \
835 }
836
837 CIRCLEOPEN_TRANSITION(8, uint8_t, 1)
838 CIRCLEOPEN_TRANSITION(16, uint16_t, 2)
839
840 #define CIRCLECLOSE_TRANSITION(name, type, div)                                      \
841 static void circleclose##name##_transition(AVFilterContext *ctx,                     \
842                             const AVFrame *a, const AVFrame *b, AVFrame *out,        \
843                             float progress,                                          \
844                             int slice_start, int slice_end, int jobnr)               \
845 {                                                                                    \
846     XFadeContext *s = ctx->priv;                                                     \
847     const int width = out->width;                                                    \
848     const int height = out->height;                                                  \
849     const float z = hypotf(width / 2, height / 2);                                   \
850     const float p = (1.f - progress - 0.5f) * 3.f;                                   \
851                                                                                      \
852     for (int y = slice_start; y < slice_end; y++) {                                  \
853         for (int x = 0; x < width; x++) {                                            \
854             const float smooth = hypotf(x - width / 2, y - height / 2) / z + p;      \
855             for (int p = 0; p < s->nb_planes; p++) {                                 \
856                 const type *xf0 = (const type *)(a->data[p] + y * a->linesize[p]);   \
857                 const type *xf1 = (const type *)(b->data[p] + y * b->linesize[p]);   \
858                 type *dst = (type *)(out->data[p] + y * out->linesize[p]);           \
859                                                                                      \
860                 dst[x] = mix(xf1[x], xf0[x], smoothstep(0.f, 1.f, smooth));          \
861             }                                                                        \
862         }                                                                            \
863     }                                                                                \
864 }
865
866 CIRCLECLOSE_TRANSITION(8, uint8_t, 1)
867 CIRCLECLOSE_TRANSITION(16, uint16_t, 2)
868
869 #define VERTOPEN_TRANSITION(name, type, div)                                         \
870 static void vertopen##name##_transition(AVFilterContext *ctx,                        \
871                             const AVFrame *a, const AVFrame *b, AVFrame *out,        \
872                             float progress,                                          \
873                             int slice_start, int slice_end, int jobnr)               \
874 {                                                                                    \
875     XFadeContext *s = ctx->priv;                                                     \
876     const int width = out->width;                                                    \
877     const float w2 = out->width / 2;                                                 \
878                                                                                      \
879     for (int y = slice_start; y < slice_end; y++) {                                  \
880         for (int x = 0; x < width; x++) {                                            \
881             const float smooth = 2.f - fabsf((x - w2) / w2) - progress * 2.f;        \
882             for (int p = 0; p < s->nb_planes; p++) {                                 \
883                 const type *xf0 = (const type *)(a->data[p] + y * a->linesize[p]);   \
884                 const type *xf1 = (const type *)(b->data[p] + y * b->linesize[p]);   \
885                 type *dst = (type *)(out->data[p] + y * out->linesize[p]);           \
886                                                                                      \
887                 dst[x] = mix(xf1[x], xf0[x], smoothstep(0.f, 1.f, smooth));          \
888             }                                                                        \
889         }                                                                            \
890     }                                                                                \
891 }
892
893 VERTOPEN_TRANSITION(8, uint8_t, 1)
894 VERTOPEN_TRANSITION(16, uint16_t, 2)
895
896 #define VERTCLOSE_TRANSITION(name, type, div)                                        \
897 static void vertclose##name##_transition(AVFilterContext *ctx,                       \
898                             const AVFrame *a, const AVFrame *b, AVFrame *out,        \
899                             float progress,                                          \
900                             int slice_start, int slice_end, int jobnr)               \
901 {                                                                                    \
902     XFadeContext *s = ctx->priv;                                                     \
903     const int width = out->width;                                                    \
904     const float w2 = out->width / 2;                                                 \
905                                                                                      \
906     for (int y = slice_start; y < slice_end; y++) {                                  \
907         for (int x = 0; x < width; x++) {                                            \
908             const float smooth = 1.f + fabsf((x - w2) / w2) - progress * 2.f;        \
909             for (int p = 0; p < s->nb_planes; p++) {                                 \
910                 const type *xf0 = (const type *)(a->data[p] + y * a->linesize[p]);   \
911                 const type *xf1 = (const type *)(b->data[p] + y * b->linesize[p]);   \
912                 type *dst = (type *)(out->data[p] + y * out->linesize[p]);           \
913                                                                                      \
914                 dst[x] = mix(xf1[x], xf0[x], smoothstep(0.f, 1.f, smooth));          \
915             }                                                                        \
916         }                                                                            \
917     }                                                                                \
918 }
919
920 VERTCLOSE_TRANSITION(8, uint8_t, 1)
921 VERTCLOSE_TRANSITION(16, uint16_t, 2)
922
923 #define HORZOPEN_TRANSITION(name, type, div)                                         \
924 static void horzopen##name##_transition(AVFilterContext *ctx,                        \
925                             const AVFrame *a, const AVFrame *b, AVFrame *out,        \
926                             float progress,                                          \
927                             int slice_start, int slice_end, int jobnr)               \
928 {                                                                                    \
929     XFadeContext *s = ctx->priv;                                                     \
930     const int width = out->width;                                                    \
931     const float h2 = out->height / 2;                                                \
932                                                                                      \
933     for (int y = slice_start; y < slice_end; y++) {                                  \
934         const float smooth = 2.f - fabsf((y - h2) / h2) - progress * 2.f;            \
935         for (int x = 0; x < width; x++) {                                            \
936             for (int p = 0; p < s->nb_planes; p++) {                                 \
937                 const type *xf0 = (const type *)(a->data[p] + y * a->linesize[p]);   \
938                 const type *xf1 = (const type *)(b->data[p] + y * b->linesize[p]);   \
939                 type *dst = (type *)(out->data[p] + y * out->linesize[p]);           \
940                                                                                      \
941                 dst[x] = mix(xf1[x], xf0[x], smoothstep(0.f, 1.f, smooth));          \
942             }                                                                        \
943         }                                                                            \
944     }                                                                                \
945 }
946
947 HORZOPEN_TRANSITION(8, uint8_t, 1)
948 HORZOPEN_TRANSITION(16, uint16_t, 2)
949
950 #define HORZCLOSE_TRANSITION(name, type, div)                                        \
951 static void horzclose##name##_transition(AVFilterContext *ctx,                       \
952                             const AVFrame *a, const AVFrame *b, AVFrame *out,        \
953                             float progress,                                          \
954                             int slice_start, int slice_end, int jobnr)               \
955 {                                                                                    \
956     XFadeContext *s = ctx->priv;                                                     \
957     const int width = out->width;                                                    \
958     const float h2 = out->height / 2;                                                \
959                                                                                      \
960     for (int y = slice_start; y < slice_end; y++) {                                  \
961         const float smooth = 1.f + fabsf((y - h2) / h2) - progress * 2.f;            \
962         for (int x = 0; x < width; x++) {                                            \
963             for (int p = 0; p < s->nb_planes; p++) {                                 \
964                 const type *xf0 = (const type *)(a->data[p] + y * a->linesize[p]);   \
965                 const type *xf1 = (const type *)(b->data[p] + y * b->linesize[p]);   \
966                 type *dst = (type *)(out->data[p] + y * out->linesize[p]);           \
967                                                                                      \
968                 dst[x] = mix(xf1[x], xf0[x], smoothstep(0.f, 1.f, smooth));          \
969             }                                                                        \
970         }                                                                            \
971     }                                                                                \
972 }
973
974 HORZCLOSE_TRANSITION(8, uint8_t, 1)
975 HORZCLOSE_TRANSITION(16, uint16_t, 2)
976
977 static inline double getpix(void *priv, double x, double y, int plane, int nb)
978 {
979     XFadeContext *s = priv;
980     AVFrame *in = s->xf[nb];
981     const uint8_t *src = in->data[FFMIN(plane, s->nb_planes - 1)];
982     int linesize = in->linesize[FFMIN(plane, s->nb_planes - 1)];
983     const int w = in->width;
984     const int h = in->height;
985
986     int xi, yi;
987
988     xi = av_clipd(x, 0, w - 1);
989     yi = av_clipd(y, 0, h - 1);
990
991     if (s->depth > 8) {
992         const uint16_t *src16 = (const uint16_t*)src;
993
994         linesize /= 2;
995         return src16[xi + yi * linesize];
996     } else {
997         return src[xi + yi * linesize];
998     }
999 }
1000
1001 static double a0(void *priv, double x, double y) { return getpix(priv, x, y, 0, 0); }
1002 static double a1(void *priv, double x, double y) { return getpix(priv, x, y, 1, 0); }
1003 static double a2(void *priv, double x, double y) { return getpix(priv, x, y, 2, 0); }
1004 static double a3(void *priv, double x, double y) { return getpix(priv, x, y, 3, 0); }
1005
1006 static double b0(void *priv, double x, double y) { return getpix(priv, x, y, 0, 1); }
1007 static double b1(void *priv, double x, double y) { return getpix(priv, x, y, 1, 1); }
1008 static double b2(void *priv, double x, double y) { return getpix(priv, x, y, 2, 1); }
1009 static double b3(void *priv, double x, double y) { return getpix(priv, x, y, 3, 1); }
1010
1011 static int config_output(AVFilterLink *outlink)
1012 {
1013     AVFilterContext *ctx = outlink->src;
1014     AVFilterLink *inlink0 = ctx->inputs[0];
1015     AVFilterLink *inlink1 = ctx->inputs[1];
1016     XFadeContext *s = ctx->priv;
1017     const AVPixFmtDescriptor *pix_desc = av_pix_fmt_desc_get(inlink0->format);
1018     int is_rgb;
1019
1020     if (inlink0->format != inlink1->format) {
1021         av_log(ctx, AV_LOG_ERROR, "inputs must be of same pixel format\n");
1022         return AVERROR(EINVAL);
1023     }
1024     if (inlink0->w != inlink1->w || inlink0->h != inlink1->h) {
1025         av_log(ctx, AV_LOG_ERROR, "First input link %s parameters "
1026                "(size %dx%d) do not match the corresponding "
1027                "second input link %s parameters (size %dx%d)\n",
1028                ctx->input_pads[0].name, inlink0->w, inlink0->h,
1029                ctx->input_pads[1].name, inlink1->w, inlink1->h);
1030         return AVERROR(EINVAL);
1031     }
1032
1033     if (inlink0->time_base.num != inlink1->time_base.num ||
1034         inlink0->time_base.den != inlink1->time_base.den) {
1035         av_log(ctx, AV_LOG_ERROR, "First input link %s timebase "
1036                "(%d/%d) do not match the corresponding "
1037                "second input link %s timebase (%d/%d)\n",
1038                ctx->input_pads[0].name, inlink0->time_base.num, inlink0->time_base.den,
1039                ctx->input_pads[1].name, inlink1->time_base.num, inlink1->time_base.den);
1040         return AVERROR(EINVAL);
1041     }
1042
1043     outlink->w = inlink0->w;
1044     outlink->h = inlink0->h;
1045     outlink->time_base = inlink0->time_base;
1046     outlink->sample_aspect_ratio = inlink0->sample_aspect_ratio;
1047     outlink->frame_rate = inlink0->frame_rate;
1048
1049     s->depth = pix_desc->comp[0].depth;
1050     is_rgb = !!(pix_desc->flags & AV_PIX_FMT_FLAG_RGB);
1051     s->nb_planes = av_pix_fmt_count_planes(inlink0->format);
1052     s->max_value = (1 << s->depth) - 1;
1053     s->black[0] = 0;
1054     s->black[1] = s->black[2] = is_rgb ? 0 : s->max_value / 2;
1055     s->black[3] = s->max_value;
1056     s->white[0] = s->white[3] = s->max_value;
1057     s->white[1] = s->white[2] = is_rgb ? s->max_value : s->max_value / 2;
1058
1059     s->first_pts = s->last_pts = s->pts = AV_NOPTS_VALUE;
1060
1061     if (s->duration)
1062         s->duration_pts = av_rescale_q(s->duration, AV_TIME_BASE_Q, outlink->time_base);
1063     if (s->offset)
1064         s->offset_pts = av_rescale_q(s->offset, AV_TIME_BASE_Q, outlink->time_base);
1065
1066     switch (s->transition) {
1067     case CUSTOM:     s->transitionf = s->depth <= 8 ? custom8_transition     : custom16_transition;     break;
1068     case FADE:       s->transitionf = s->depth <= 8 ? fade8_transition       : fade16_transition;       break;
1069     case WIPELEFT:   s->transitionf = s->depth <= 8 ? wipeleft8_transition   : wipeleft16_transition;   break;
1070     case WIPERIGHT:  s->transitionf = s->depth <= 8 ? wiperight8_transition  : wiperight16_transition;  break;
1071     case WIPEUP:     s->transitionf = s->depth <= 8 ? wipeup8_transition     : wipeup16_transition;     break;
1072     case WIPEDOWN:   s->transitionf = s->depth <= 8 ? wipedown8_transition   : wipedown16_transition;   break;
1073     case SLIDELEFT:  s->transitionf = s->depth <= 8 ? slideleft8_transition  : slideleft16_transition;  break;
1074     case SLIDERIGHT: s->transitionf = s->depth <= 8 ? slideright8_transition : slideright16_transition; break;
1075     case SLIDEUP:    s->transitionf = s->depth <= 8 ? slideup8_transition    : slideup16_transition;    break;
1076     case SLIDEDOWN:  s->transitionf = s->depth <= 8 ? slidedown8_transition  : slidedown16_transition;  break;
1077     case CIRCLECROP: s->transitionf = s->depth <= 8 ? circlecrop8_transition : circlecrop16_transition; break;
1078     case RECTCROP:   s->transitionf = s->depth <= 8 ? rectcrop8_transition   : rectcrop16_transition;   break;
1079     case DISTANCE:   s->transitionf = s->depth <= 8 ? distance8_transition   : distance16_transition;   break;
1080     case FADEBLACK:  s->transitionf = s->depth <= 8 ? fadeblack8_transition  : fadeblack16_transition;  break;
1081     case FADEWHITE:  s->transitionf = s->depth <= 8 ? fadewhite8_transition  : fadewhite16_transition;  break;
1082     case RADIAL:     s->transitionf = s->depth <= 8 ? radial8_transition     : radial16_transition;     break;
1083     case SMOOTHLEFT: s->transitionf = s->depth <= 8 ? smoothleft8_transition : smoothleft16_transition; break;
1084     case SMOOTHRIGHT:s->transitionf = s->depth <= 8 ? smoothright8_transition: smoothright16_transition;break;
1085     case SMOOTHUP:   s->transitionf = s->depth <= 8 ? smoothup8_transition   : smoothup16_transition;   break;
1086     case SMOOTHDOWN: s->transitionf = s->depth <= 8 ? smoothdown8_transition : smoothdown16_transition; break;
1087     case CIRCLEOPEN: s->transitionf = s->depth <= 8 ? circleopen8_transition : circleopen16_transition; break;
1088     case CIRCLECLOSE:s->transitionf = s->depth <= 8 ? circleclose8_transition: circleclose16_transition;break;
1089     case VERTOPEN:   s->transitionf = s->depth <= 8 ? vertopen8_transition   : vertopen16_transition;   break;
1090     case VERTCLOSE:  s->transitionf = s->depth <= 8 ? vertclose8_transition  : vertclose16_transition;  break;
1091     case HORZOPEN:   s->transitionf = s->depth <= 8 ? horzopen8_transition   : horzopen16_transition;   break;
1092     case HORZCLOSE:  s->transitionf = s->depth <= 8 ? horzclose8_transition  : horzclose16_transition;  break;
1093     }
1094
1095     if (s->transition == CUSTOM) {
1096         static const char *const func2_names[]    = {
1097             "a0", "a1", "a2", "a3",
1098             "b0", "b1", "b2", "b3",
1099             NULL
1100         };
1101         double (*func2[])(void *, double, double) = {
1102             a0, a1, a2, a3,
1103             b0, b1, b2, b3,
1104             NULL };
1105         int ret;
1106
1107         if (!s->custom_str)
1108             return AVERROR(EINVAL);
1109         ret = av_expr_parse(&s->e, s->custom_str, var_names,
1110                             NULL, NULL, func2_names, func2, 0, ctx);
1111         if (ret < 0)
1112             return ret;
1113     }
1114
1115     return 0;
1116 }
1117
1118 static int xfade_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
1119 {
1120     XFadeContext *s = ctx->priv;
1121     AVFilterLink *outlink = ctx->outputs[0];
1122     ThreadData *td = arg;
1123     int slice_start = (outlink->h *  jobnr   ) / nb_jobs;
1124     int slice_end   = (outlink->h * (jobnr+1)) / nb_jobs;
1125
1126     s->transitionf(ctx, td->xf[0], td->xf[1], td->out, td->progress, slice_start, slice_end, jobnr);
1127
1128     return 0;
1129 }
1130
1131 static int xfade_frame(AVFilterContext *ctx, AVFrame *a, AVFrame *b)
1132 {
1133     XFadeContext *s = ctx->priv;
1134     AVFilterLink *outlink = ctx->outputs[0];
1135     float progress = av_clipf(1.f - ((float)(s->pts - s->first_pts - s->offset_pts) / s->duration_pts), 0.f, 1.f);
1136     ThreadData td;
1137     AVFrame *out;
1138
1139     out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
1140     if (!out)
1141         return AVERROR(ENOMEM);
1142
1143     td.xf[0] = a, td.xf[1] = b, td.out = out, td.progress = progress;
1144     ctx->internal->execute(ctx, xfade_slice, &td, NULL, FFMIN(outlink->h, ff_filter_get_nb_threads(ctx)));
1145
1146     out->pts = s->pts;
1147
1148     return ff_filter_frame(outlink, out);
1149 }
1150
1151 static int xfade_activate(AVFilterContext *ctx)
1152 {
1153     XFadeContext *s = ctx->priv;
1154     AVFilterLink *outlink = ctx->outputs[0];
1155     AVFrame *in = NULL;
1156     int ret = 0, status;
1157     int64_t pts;
1158
1159     FF_FILTER_FORWARD_STATUS_BACK_ALL(outlink, ctx);
1160
1161     if (s->xfade_is_over) {
1162         ret = ff_inlink_consume_frame(ctx->inputs[1], &in);
1163         if (ret < 0) {
1164             return ret;
1165         } else if (ret > 0) {
1166             in->pts = (in->pts - s->last_pts) + s->pts;
1167             return ff_filter_frame(outlink, in);
1168         } else if (ff_inlink_acknowledge_status(ctx->inputs[1], &status, &pts)) {
1169             ff_outlink_set_status(outlink, status, s->pts);
1170             return 0;
1171         } else if (!ret) {
1172             if (ff_outlink_frame_wanted(outlink)) {
1173                 ff_inlink_request_frame(ctx->inputs[1]);
1174                 return 0;
1175             }
1176         }
1177     }
1178
1179     if (ff_inlink_queued_frames(ctx->inputs[0]) > 0) {
1180         s->xf[0] = ff_inlink_peek_frame(ctx->inputs[0], 0);
1181         if (s->xf[0]) {
1182             if (s->first_pts == AV_NOPTS_VALUE) {
1183                 s->first_pts = s->xf[0]->pts;
1184             }
1185             s->pts = s->xf[0]->pts;
1186             if (s->first_pts + s->offset_pts > s->xf[0]->pts) {
1187                 s->xf[0] = NULL;
1188                 s->need_second = 0;
1189                 ff_inlink_consume_frame(ctx->inputs[0], &in);
1190                 return ff_filter_frame(outlink, in);
1191             }
1192
1193             s->need_second = 1;
1194         }
1195     }
1196
1197     if (s->xf[0] && ff_inlink_queued_frames(ctx->inputs[1]) > 0) {
1198         ff_inlink_consume_frame(ctx->inputs[0], &s->xf[0]);
1199         ff_inlink_consume_frame(ctx->inputs[1], &s->xf[1]);
1200
1201         s->last_pts = s->xf[1]->pts;
1202         s->pts = s->xf[0]->pts;
1203         if (s->xf[0]->pts - (s->first_pts + s->offset_pts) > s->duration_pts)
1204             s->xfade_is_over = 1;
1205         ret = xfade_frame(ctx, s->xf[0], s->xf[1]);
1206         av_frame_free(&s->xf[0]);
1207         av_frame_free(&s->xf[1]);
1208         return ret;
1209     }
1210
1211     if (ff_inlink_queued_frames(ctx->inputs[0]) > 0 &&
1212         ff_inlink_queued_frames(ctx->inputs[1]) > 0) {
1213         ff_filter_set_ready(ctx, 100);
1214         return 0;
1215     }
1216
1217     if (ff_outlink_frame_wanted(outlink)) {
1218         if (!s->eof[0] && ff_outlink_get_status(ctx->inputs[0])) {
1219             s->eof[0] = 1;
1220             s->xfade_is_over = 1;
1221         }
1222         if (!s->eof[1] && ff_outlink_get_status(ctx->inputs[1])) {
1223             s->eof[1] = 1;
1224         }
1225         if (!s->eof[0] && !s->xf[0])
1226             ff_inlink_request_frame(ctx->inputs[0]);
1227         if (!s->eof[1] && (s->need_second || s->eof[0]))
1228             ff_inlink_request_frame(ctx->inputs[1]);
1229         if (s->eof[0] && s->eof[1] && (
1230             ff_inlink_queued_frames(ctx->inputs[0]) <= 0 ||
1231             ff_inlink_queued_frames(ctx->inputs[1]) <= 0))
1232             ff_outlink_set_status(outlink, AVERROR_EOF, AV_NOPTS_VALUE);
1233         return 0;
1234     }
1235
1236     return FFERROR_NOT_READY;
1237 }
1238
1239 static const AVFilterPad xfade_inputs[] = {
1240     {
1241         .name          = "main",
1242         .type          = AVMEDIA_TYPE_VIDEO,
1243     },
1244     {
1245         .name          = "xfade",
1246         .type          = AVMEDIA_TYPE_VIDEO,
1247     },
1248     { NULL }
1249 };
1250
1251 static const AVFilterPad xfade_outputs[] = {
1252     {
1253         .name          = "default",
1254         .type          = AVMEDIA_TYPE_VIDEO,
1255         .config_props  = config_output,
1256     },
1257     { NULL }
1258 };
1259
1260 AVFilter ff_vf_xfade = {
1261     .name          = "xfade",
1262     .description   = NULL_IF_CONFIG_SMALL("Cross fade one video with another video."),
1263     .priv_size     = sizeof(XFadeContext),
1264     .priv_class    = &xfade_class,
1265     .query_formats = query_formats,
1266     .activate      = xfade_activate,
1267     .uninit        = uninit,
1268     .inputs        = xfade_inputs,
1269     .outputs       = xfade_outputs,
1270     .flags         = AVFILTER_FLAG_SLICE_THREADS,
1271 };