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