2 * Copyright (c) 2020 Paul B Mahol
4 * This file is part of FFmpeg.
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.
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.
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
21 #include "libavutil/imgutils.h"
22 #include "libavutil/eval.h"
23 #include "libavutil/opt.h"
24 #include "libavutil/pixfmt.h"
31 enum XFadeTransitions {
77 typedef struct XFadeContext {
102 void (*transitionf)(AVFilterContext *ctx, const AVFrame *a, const AVFrame *b, AVFrame *out, float progress,
103 int slice_start, int slice_end, int jobnr);
108 static const char *const var_names[] = { "X", "Y", "W", "H", "A", "B", "PLANE", "P", NULL };
109 enum { VAR_X, VAR_Y, VAR_W, VAR_H, VAR_A, VAR_B, VAR_PLANE, VAR_PROGRESS, VAR_VARS_NB };
111 typedef struct ThreadData {
112 const AVFrame *xf[2];
117 static int query_formats(AVFilterContext *ctx)
119 static const enum AVPixelFormat pix_fmts[] = {
123 AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRAP, AV_PIX_FMT_GRAY8,
124 AV_PIX_FMT_YUVA444P9, AV_PIX_FMT_GBRP9,
125 AV_PIX_FMT_YUV444P10,
126 AV_PIX_FMT_YUVA444P10,
127 AV_PIX_FMT_GBRP10, AV_PIX_FMT_GBRAP10, AV_PIX_FMT_GRAY10,
128 AV_PIX_FMT_YUV444P12,
129 AV_PIX_FMT_YUVA444P12,
130 AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRAP12, AV_PIX_FMT_GRAY12,
131 AV_PIX_FMT_YUV444P14, AV_PIX_FMT_GBRP14,
132 AV_PIX_FMT_YUV444P16,
133 AV_PIX_FMT_YUVA444P16,
134 AV_PIX_FMT_GBRP16, AV_PIX_FMT_GBRAP16, AV_PIX_FMT_GRAY16,
138 AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
140 return AVERROR(ENOMEM);
141 return ff_set_common_formats(ctx, fmts_list);
144 static av_cold void uninit(AVFilterContext *ctx)
146 XFadeContext *s = ctx->priv;
151 #define OFFSET(x) offsetof(XFadeContext, x)
152 #define FLAGS (AV_OPT_FLAG_FILTERING_PARAM | AV_OPT_FLAG_VIDEO_PARAM)
154 static const AVOption xfade_options[] = {
155 { "transition", "set cross fade transition", OFFSET(transition), AV_OPT_TYPE_INT, {.i64=FADE}, -1, NB_TRANSITIONS-1, FLAGS, "transition" },
156 { "custom", "custom transition", 0, AV_OPT_TYPE_CONST, {.i64=CUSTOM}, 0, 0, FLAGS, "transition" },
157 { "fade", "fade transition", 0, AV_OPT_TYPE_CONST, {.i64=FADE}, 0, 0, FLAGS, "transition" },
158 { "wipeleft", "wipe left transition", 0, AV_OPT_TYPE_CONST, {.i64=WIPELEFT}, 0, 0, FLAGS, "transition" },
159 { "wiperight", "wipe right transition", 0, AV_OPT_TYPE_CONST, {.i64=WIPERIGHT}, 0, 0, FLAGS, "transition" },
160 { "wipeup", "wipe up transition", 0, AV_OPT_TYPE_CONST, {.i64=WIPEUP}, 0, 0, FLAGS, "transition" },
161 { "wipedown", "wipe down transition", 0, AV_OPT_TYPE_CONST, {.i64=WIPEDOWN}, 0, 0, FLAGS, "transition" },
162 { "slideleft", "slide left transition", 0, AV_OPT_TYPE_CONST, {.i64=SLIDELEFT}, 0, 0, FLAGS, "transition" },
163 { "slideright", "slide right transition", 0, AV_OPT_TYPE_CONST, {.i64=SLIDERIGHT}, 0, 0, FLAGS, "transition" },
164 { "slideup", "slide up transition", 0, AV_OPT_TYPE_CONST, {.i64=SLIDEUP}, 0, 0, FLAGS, "transition" },
165 { "slidedown", "slide down transition", 0, AV_OPT_TYPE_CONST, {.i64=SLIDEDOWN}, 0, 0, FLAGS, "transition" },
166 { "circlecrop", "circle crop transition", 0, AV_OPT_TYPE_CONST, {.i64=CIRCLECROP}, 0, 0, FLAGS, "transition" },
167 { "rectcrop", "rect crop transition", 0, AV_OPT_TYPE_CONST, {.i64=RECTCROP}, 0, 0, FLAGS, "transition" },
168 { "distance", "distance transition", 0, AV_OPT_TYPE_CONST, {.i64=DISTANCE}, 0, 0, FLAGS, "transition" },
169 { "fadeblack", "fadeblack transition", 0, AV_OPT_TYPE_CONST, {.i64=FADEBLACK}, 0, 0, FLAGS, "transition" },
170 { "fadewhite", "fadewhite transition", 0, AV_OPT_TYPE_CONST, {.i64=FADEWHITE}, 0, 0, FLAGS, "transition" },
171 { "radial", "radial transition", 0, AV_OPT_TYPE_CONST, {.i64=RADIAL}, 0, 0, FLAGS, "transition" },
172 { "smoothleft", "smoothleft transition", 0, AV_OPT_TYPE_CONST, {.i64=SMOOTHLEFT}, 0, 0, FLAGS, "transition" },
173 { "smoothright","smoothright transition", 0, AV_OPT_TYPE_CONST, {.i64=SMOOTHRIGHT},0, 0, FLAGS, "transition" },
174 { "smoothup", "smoothup transition", 0, AV_OPT_TYPE_CONST, {.i64=SMOOTHUP}, 0, 0, FLAGS, "transition" },
175 { "smoothdown", "smoothdown transition", 0, AV_OPT_TYPE_CONST, {.i64=SMOOTHDOWN}, 0, 0, FLAGS, "transition" },
176 { "circleopen", "circleopen transition", 0, AV_OPT_TYPE_CONST, {.i64=CIRCLEOPEN}, 0, 0, FLAGS, "transition" },
177 { "circleclose","circleclose transition", 0, AV_OPT_TYPE_CONST, {.i64=CIRCLECLOSE},0, 0, FLAGS, "transition" },
178 { "vertopen", "vert open transition", 0, AV_OPT_TYPE_CONST, {.i64=VERTOPEN}, 0, 0, FLAGS, "transition" },
179 { "vertclose", "vert close transition", 0, AV_OPT_TYPE_CONST, {.i64=VERTCLOSE}, 0, 0, FLAGS, "transition" },
180 { "horzopen", "horz open transition", 0, AV_OPT_TYPE_CONST, {.i64=HORZOPEN}, 0, 0, FLAGS, "transition" },
181 { "horzclose", "horz close transition", 0, AV_OPT_TYPE_CONST, {.i64=HORZCLOSE}, 0, 0, FLAGS, "transition" },
182 { "dissolve", "dissolve transition", 0, AV_OPT_TYPE_CONST, {.i64=DISSOLVE}, 0, 0, FLAGS, "transition" },
183 { "pixelize", "pixelize transition", 0, AV_OPT_TYPE_CONST, {.i64=PIXELIZE}, 0, 0, FLAGS, "transition" },
184 { "diagtl", "diag tl transition", 0, AV_OPT_TYPE_CONST, {.i64=DIAGTL}, 0, 0, FLAGS, "transition" },
185 { "diagtr", "diag tr transition", 0, AV_OPT_TYPE_CONST, {.i64=DIAGTR}, 0, 0, FLAGS, "transition" },
186 { "diagbl", "diag bl transition", 0, AV_OPT_TYPE_CONST, {.i64=DIAGBL}, 0, 0, FLAGS, "transition" },
187 { "diagbr", "diag br transition", 0, AV_OPT_TYPE_CONST, {.i64=DIAGBR}, 0, 0, FLAGS, "transition" },
188 { "hlslice", "hl slice transition", 0, AV_OPT_TYPE_CONST, {.i64=HLSLICE}, 0, 0, FLAGS, "transition" },
189 { "hrslice", "hr slice transition", 0, AV_OPT_TYPE_CONST, {.i64=HRSLICE}, 0, 0, FLAGS, "transition" },
190 { "vuslice", "vu slice transition", 0, AV_OPT_TYPE_CONST, {.i64=VUSLICE}, 0, 0, FLAGS, "transition" },
191 { "vdslice", "vd slice transition", 0, AV_OPT_TYPE_CONST, {.i64=VDSLICE}, 0, 0, FLAGS, "transition" },
192 { "hblur", "hblur transition", 0, AV_OPT_TYPE_CONST, {.i64=HBLUR}, 0, 0, FLAGS, "transition" },
193 { "fadegrays", "fadegrays transition", 0, AV_OPT_TYPE_CONST, {.i64=FADEGRAYS}, 0, 0, FLAGS, "transition" },
194 { "wipetl", "wipe tl transition", 0, AV_OPT_TYPE_CONST, {.i64=WIPETL}, 0, 0, FLAGS, "transition" },
195 { "wipetr", "wipe tr transition", 0, AV_OPT_TYPE_CONST, {.i64=WIPETR}, 0, 0, FLAGS, "transition" },
196 { "wipebl", "wipe bl transition", 0, AV_OPT_TYPE_CONST, {.i64=WIPEBL}, 0, 0, FLAGS, "transition" },
197 { "wipebr", "wipe br transition", 0, AV_OPT_TYPE_CONST, {.i64=WIPEBR}, 0, 0, FLAGS, "transition" },
198 { "duration", "set cross fade duration", OFFSET(duration), AV_OPT_TYPE_DURATION, {.i64=1000000}, 0, 60000000, FLAGS },
199 { "offset", "set cross fade start relative to first input stream", OFFSET(offset), AV_OPT_TYPE_DURATION, {.i64=0}, INT64_MIN, INT64_MAX, FLAGS },
200 { "expr", "set expression for custom transition", OFFSET(custom_str), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, FLAGS },
204 AVFILTER_DEFINE_CLASS(xfade);
206 #define CUSTOM_TRANSITION(name, type, div) \
207 static void custom##name##_transition(AVFilterContext *ctx, \
208 const AVFrame *a, const AVFrame *b, AVFrame *out, \
210 int slice_start, int slice_end, int jobnr) \
212 XFadeContext *s = ctx->priv; \
213 const int height = slice_end - slice_start; \
215 double values[VAR_VARS_NB]; \
216 values[VAR_W] = out->width; \
217 values[VAR_H] = out->height; \
218 values[VAR_PROGRESS] = progress; \
220 for (int p = 0; p < s->nb_planes; p++) { \
221 const type *xf0 = (const type *)(a->data[p] + slice_start * a->linesize[p]); \
222 const type *xf1 = (const type *)(b->data[p] + slice_start * b->linesize[p]); \
223 type *dst = (type *)(out->data[p] + slice_start * out->linesize[p]); \
225 values[VAR_PLANE] = p; \
227 for (int y = 0; y < height; y++) { \
228 values[VAR_Y] = slice_start + y; \
229 for (int x = 0; x < out->width; x++) { \
231 values[VAR_A] = xf0[x]; \
232 values[VAR_B] = xf1[x]; \
233 dst[x] = av_expr_eval(s->e, values, s); \
236 dst += out->linesize[p] / div; \
237 xf0 += a->linesize[p] / div; \
238 xf1 += b->linesize[p] / div; \
243 CUSTOM_TRANSITION(8, uint8_t, 1)
244 CUSTOM_TRANSITION(16, uint16_t, 2)
246 static inline float mix(float a, float b, float mix)
248 return a * mix + b * (1.f - mix);
251 static inline float fract(float a)
253 return a - floorf(a);
256 static inline float smoothstep(float edge0, float edge1, float x)
260 t = av_clipf((x - edge0) / (edge1 - edge0), 0.f, 1.f);
262 return t * t * (3.f - 2.f * t);
265 #define FADE_TRANSITION(name, type, div) \
266 static void fade##name##_transition(AVFilterContext *ctx, \
267 const AVFrame *a, const AVFrame *b, AVFrame *out, \
269 int slice_start, int slice_end, int jobnr) \
271 XFadeContext *s = ctx->priv; \
272 const int height = slice_end - slice_start; \
274 for (int p = 0; p < s->nb_planes; p++) { \
275 const type *xf0 = (const type *)(a->data[p] + slice_start * a->linesize[p]); \
276 const type *xf1 = (const type *)(b->data[p] + slice_start * b->linesize[p]); \
277 type *dst = (type *)(out->data[p] + slice_start * out->linesize[p]); \
279 for (int y = 0; y < height; y++) { \
280 for (int x = 0; x < out->width; x++) { \
281 dst[x] = mix(xf0[x], xf1[x], progress); \
284 dst += out->linesize[p] / div; \
285 xf0 += a->linesize[p] / div; \
286 xf1 += b->linesize[p] / div; \
291 FADE_TRANSITION(8, uint8_t, 1)
292 FADE_TRANSITION(16, uint16_t, 2)
294 #define WIPELEFT_TRANSITION(name, type, div) \
295 static void wipeleft##name##_transition(AVFilterContext *ctx, \
296 const AVFrame *a, const AVFrame *b, AVFrame *out, \
298 int slice_start, int slice_end, int jobnr) \
300 XFadeContext *s = ctx->priv; \
301 const int height = slice_end - slice_start; \
302 const int z = out->width * progress; \
304 for (int p = 0; p < s->nb_planes; p++) { \
305 const type *xf0 = (const type *)(a->data[p] + slice_start * a->linesize[p]); \
306 const type *xf1 = (const type *)(b->data[p] + slice_start * b->linesize[p]); \
307 type *dst = (type *)(out->data[p] + slice_start * out->linesize[p]); \
309 for (int y = 0; y < height; y++) { \
310 for (int x = 0; x < out->width; x++) { \
311 dst[x] = x > z ? xf1[x] : xf0[x]; \
314 dst += out->linesize[p] / div; \
315 xf0 += a->linesize[p] / div; \
316 xf1 += b->linesize[p] / div; \
321 WIPELEFT_TRANSITION(8, uint8_t, 1)
322 WIPELEFT_TRANSITION(16, uint16_t, 2)
324 #define WIPERIGHT_TRANSITION(name, type, div) \
325 static void wiperight##name##_transition(AVFilterContext *ctx, \
326 const AVFrame *a, const AVFrame *b, AVFrame *out, \
328 int slice_start, int slice_end, int jobnr) \
330 XFadeContext *s = ctx->priv; \
331 const int height = slice_end - slice_start; \
332 const int z = out->width * (1.f - progress); \
334 for (int p = 0; p < s->nb_planes; p++) { \
335 const type *xf0 = (const type *)(a->data[p] + slice_start * a->linesize[p]); \
336 const type *xf1 = (const type *)(b->data[p] + slice_start * b->linesize[p]); \
337 type *dst = (type *)(out->data[p] + slice_start * out->linesize[p]); \
339 for (int y = 0; y < height; y++) { \
340 for (int x = 0; x < out->width; x++) { \
341 dst[x] = x > z ? xf0[x] : xf1[x]; \
344 dst += out->linesize[p] / div; \
345 xf0 += a->linesize[p] / div; \
346 xf1 += b->linesize[p] / div; \
351 WIPERIGHT_TRANSITION(8, uint8_t, 1)
352 WIPERIGHT_TRANSITION(16, uint16_t, 2)
354 #define WIPEUP_TRANSITION(name, type, div) \
355 static void wipeup##name##_transition(AVFilterContext *ctx, \
356 const AVFrame *a, const AVFrame *b, AVFrame *out, \
358 int slice_start, int slice_end, int jobnr) \
360 XFadeContext *s = ctx->priv; \
361 const int height = slice_end - slice_start; \
362 const int z = out->height * progress; \
364 for (int p = 0; p < s->nb_planes; p++) { \
365 const type *xf0 = (const type *)(a->data[p] + slice_start * a->linesize[p]); \
366 const type *xf1 = (const type *)(b->data[p] + slice_start * b->linesize[p]); \
367 type *dst = (type *)(out->data[p] + slice_start * out->linesize[p]); \
369 for (int y = 0; y < height; y++) { \
370 for (int x = 0; x < out->width; x++) { \
371 dst[x] = slice_start + y > z ? xf1[x] : xf0[x]; \
374 dst += out->linesize[p] / div; \
375 xf0 += a->linesize[p] / div; \
376 xf1 += b->linesize[p] / div; \
381 WIPEUP_TRANSITION(8, uint8_t, 1)
382 WIPEUP_TRANSITION(16, uint16_t, 2)
384 #define WIPEDOWN_TRANSITION(name, type, div) \
385 static void wipedown##name##_transition(AVFilterContext *ctx, \
386 const AVFrame *a, const AVFrame *b, AVFrame *out, \
388 int slice_start, int slice_end, int jobnr) \
390 XFadeContext *s = ctx->priv; \
391 const int height = slice_end - slice_start; \
392 const int z = out->height * (1.f - progress); \
394 for (int p = 0; p < s->nb_planes; p++) { \
395 const type *xf0 = (const type *)(a->data[p] + slice_start * a->linesize[p]); \
396 const type *xf1 = (const type *)(b->data[p] + slice_start * b->linesize[p]); \
397 type *dst = (type *)(out->data[p] + slice_start * out->linesize[p]); \
399 for (int y = 0; y < height; y++) { \
400 for (int x = 0; x < out->width; x++) { \
401 dst[x] = slice_start + y > z ? xf0[x] : xf1[x]; \
404 dst += out->linesize[p] / div; \
405 xf0 += a->linesize[p] / div; \
406 xf1 += b->linesize[p] / div; \
411 WIPEDOWN_TRANSITION(8, uint8_t, 1)
412 WIPEDOWN_TRANSITION(16, uint16_t, 2)
414 #define SLIDELEFT_TRANSITION(name, type, div) \
415 static void slideleft##name##_transition(AVFilterContext *ctx, \
416 const AVFrame *a, const AVFrame *b, AVFrame *out, \
418 int slice_start, int slice_end, int jobnr) \
420 XFadeContext *s = ctx->priv; \
421 const int height = slice_end - slice_start; \
422 const int width = out->width; \
423 const int z = -progress * width; \
425 for (int p = 0; p < s->nb_planes; p++) { \
426 const type *xf0 = (const type *)(a->data[p] + slice_start * a->linesize[p]); \
427 const type *xf1 = (const type *)(b->data[p] + slice_start * b->linesize[p]); \
428 type *dst = (type *)(out->data[p] + slice_start * out->linesize[p]); \
430 for (int y = 0; y < height; y++) { \
431 for (int x = 0; x < width; x++) { \
432 const int zx = z + x; \
433 const int zz = zx % width + width * (zx < 0); \
434 dst[x] = (zx > 0) && (zx < width) ? xf1[zz] : xf0[zz]; \
437 dst += out->linesize[p] / div; \
438 xf0 += a->linesize[p] / div; \
439 xf1 += b->linesize[p] / div; \
444 SLIDELEFT_TRANSITION(8, uint8_t, 1)
445 SLIDELEFT_TRANSITION(16, uint16_t, 2)
447 #define SLIDERIGHT_TRANSITION(name, type, div) \
448 static void slideright##name##_transition(AVFilterContext *ctx, \
449 const AVFrame *a, const AVFrame *b, AVFrame *out, \
451 int slice_start, int slice_end, int jobnr) \
453 XFadeContext *s = ctx->priv; \
454 const int height = slice_end - slice_start; \
455 const int width = out->width; \
456 const int z = progress * width; \
458 for (int p = 0; p < s->nb_planes; p++) { \
459 const type *xf0 = (const type *)(a->data[p] + slice_start * a->linesize[p]); \
460 const type *xf1 = (const type *)(b->data[p] + slice_start * b->linesize[p]); \
461 type *dst = (type *)(out->data[p] + slice_start * out->linesize[p]); \
463 for (int y = 0; y < height; y++) { \
464 for (int x = 0; x < out->width; x++) { \
465 const int zx = z + x; \
466 const int zz = zx % width + width * (zx < 0); \
467 dst[x] = (zx > 0) && (zx < width) ? xf1[zz] : xf0[zz]; \
470 dst += out->linesize[p] / div; \
471 xf0 += a->linesize[p] / div; \
472 xf1 += b->linesize[p] / div; \
477 SLIDERIGHT_TRANSITION(8, uint8_t, 1)
478 SLIDERIGHT_TRANSITION(16, uint16_t, 2)
480 #define SLIDEUP_TRANSITION(name, type, div) \
481 static void slideup##name##_transition(AVFilterContext *ctx, \
482 const AVFrame *a, const AVFrame *b, AVFrame *out, \
484 int slice_start, int slice_end, int jobnr) \
486 XFadeContext *s = ctx->priv; \
487 const int height = out->height; \
488 const int z = -progress * height; \
490 for (int p = 0; p < s->nb_planes; p++) { \
491 type *dst = (type *)(out->data[p] + slice_start * out->linesize[p]); \
493 for (int y = slice_start; y < slice_end; y++) { \
494 const int zy = z + y; \
495 const int zz = zy % height + height * (zy < 0); \
496 const type *xf0 = (const type *)(a->data[p] + zz * a->linesize[p]); \
497 const type *xf1 = (const type *)(b->data[p] + zz * b->linesize[p]); \
499 for (int x = 0; x < out->width; x++) { \
500 dst[x] = (zy > 0) && (zy < height) ? xf1[x] : xf0[x]; \
503 dst += out->linesize[p] / div; \
508 SLIDEUP_TRANSITION(8, uint8_t, 1)
509 SLIDEUP_TRANSITION(16, uint16_t, 2)
511 #define SLIDEDOWN_TRANSITION(name, type, div) \
512 static void slidedown##name##_transition(AVFilterContext *ctx, \
513 const AVFrame *a, const AVFrame *b, AVFrame *out, \
515 int slice_start, int slice_end, int jobnr) \
517 XFadeContext *s = ctx->priv; \
518 const int height = out->height; \
519 const int z = progress * height; \
521 for (int p = 0; p < s->nb_planes; p++) { \
522 type *dst = (type *)(out->data[p] + slice_start * out->linesize[p]); \
524 for (int y = slice_start; y < slice_end; y++) { \
525 const int zy = z + y; \
526 const int zz = zy % height + height * (zy < 0); \
527 const type *xf0 = (const type *)(a->data[p] + zz * a->linesize[p]); \
528 const type *xf1 = (const type *)(b->data[p] + zz * b->linesize[p]); \
530 for (int x = 0; x < out->width; x++) { \
531 dst[x] = (zy > 0) && (zy < height) ? xf1[x] : xf0[x]; \
534 dst += out->linesize[p] / div; \
539 SLIDEDOWN_TRANSITION(8, uint8_t, 1)
540 SLIDEDOWN_TRANSITION(16, uint16_t, 2)
542 #define CIRCLECROP_TRANSITION(name, type, div) \
543 static void circlecrop##name##_transition(AVFilterContext *ctx, \
544 const AVFrame *a, const AVFrame *b, AVFrame *out, \
546 int slice_start, int slice_end, int jobnr) \
548 XFadeContext *s = ctx->priv; \
549 const int width = out->width; \
550 const int height = out->height; \
551 float z = powf(2.f * fabsf(progress - 0.5f), 3.f) * hypotf(width/2, height/2); \
553 for (int p = 0; p < s->nb_planes; p++) { \
554 const int bg = s->black[p]; \
555 type *dst = (type *)(out->data[p] + slice_start * out->linesize[p]); \
557 for (int y = slice_start; y < slice_end; y++) { \
558 const type *xf0 = (const type *)(a->data[p] + y * a->linesize[p]); \
559 const type *xf1 = (const type *)(b->data[p] + y * b->linesize[p]); \
561 for (int x = 0; x < width; x++) { \
562 float dist = hypotf(x - width / 2, y - height / 2); \
563 int val = progress < 0.5f ? xf1[x] : xf0[x]; \
564 dst[x] = (z < dist) ? bg : val; \
567 dst += out->linesize[p] / div; \
572 CIRCLECROP_TRANSITION(8, uint8_t, 1)
573 CIRCLECROP_TRANSITION(16, uint16_t, 2)
575 #define RECTCROP_TRANSITION(name, type, div) \
576 static void rectcrop##name##_transition(AVFilterContext *ctx, \
577 const AVFrame *a, const AVFrame *b, AVFrame *out, \
579 int slice_start, int slice_end, int jobnr) \
581 XFadeContext *s = ctx->priv; \
582 const int width = out->width; \
583 const int height = out->height; \
584 int zh = fabsf(progress - 0.5f) * height; \
585 int zw = fabsf(progress - 0.5f) * width; \
587 for (int p = 0; p < s->nb_planes; p++) { \
588 const int bg = s->black[p]; \
589 type *dst = (type *)(out->data[p] + slice_start * out->linesize[p]); \
591 for (int y = slice_start; y < slice_end; y++) { \
592 const type *xf0 = (const type *)(a->data[p] + y * a->linesize[p]); \
593 const type *xf1 = (const type *)(b->data[p] + y * b->linesize[p]); \
595 for (int x = 0; x < width; x++) { \
596 int dist = FFABS(x - width / 2) < zw && \
597 FFABS(y - height / 2) < zh; \
598 int val = progress < 0.5f ? xf1[x] : xf0[x]; \
599 dst[x] = !dist ? bg : val; \
602 dst += out->linesize[p] / div; \
607 RECTCROP_TRANSITION(8, uint8_t, 1)
608 RECTCROP_TRANSITION(16, uint16_t, 2)
610 #define DISTANCE_TRANSITION(name, type, div) \
611 static void distance##name##_transition(AVFilterContext *ctx, \
612 const AVFrame *a, const AVFrame *b, AVFrame *out, \
614 int slice_start, int slice_end, int jobnr) \
616 XFadeContext *s = ctx->priv; \
617 const int width = out->width; \
618 const float max = s->max_value; \
620 for (int y = slice_start; y < slice_end; y++) { \
621 for (int x = 0; x < width; x++) { \
623 for (int p = 0; p < s->nb_planes; p++) { \
624 const type *xf0 = (const type *)(a->data[p] + y * a->linesize[p]); \
625 const type *xf1 = (const type *)(b->data[p] + y * b->linesize[p]); \
627 dist += (xf0[x] / max - xf1[x] / max) * \
628 (xf0[x] / max - xf1[x] / max); \
631 dist = sqrtf(dist) <= progress; \
632 for (int p = 0; p < s->nb_planes; p++) { \
633 const type *xf0 = (const type *)(a->data[p] + y * a->linesize[p]); \
634 const type *xf1 = (const type *)(b->data[p] + y * b->linesize[p]); \
635 type *dst = (type *)(out->data[p] + y * out->linesize[p]); \
636 dst[x] = mix(mix(xf0[x], xf1[x], dist), xf1[x], progress); \
642 DISTANCE_TRANSITION(8, uint8_t, 1)
643 DISTANCE_TRANSITION(16, uint16_t, 2)
645 #define FADEBLACK_TRANSITION(name, type, div) \
646 static void fadeblack##name##_transition(AVFilterContext *ctx, \
647 const AVFrame *a, const AVFrame *b, AVFrame *out, \
649 int slice_start, int slice_end, int jobnr) \
651 XFadeContext *s = ctx->priv; \
652 const int height = slice_end - slice_start; \
653 const float phase = 0.2f; \
655 for (int p = 0; p < s->nb_planes; p++) { \
656 const type *xf0 = (const type *)(a->data[p] + slice_start * a->linesize[p]); \
657 const type *xf1 = (const type *)(b->data[p] + slice_start * b->linesize[p]); \
658 type *dst = (type *)(out->data[p] + slice_start * out->linesize[p]); \
659 const int bg = s->black[p]; \
661 for (int y = 0; y < height; y++) { \
662 for (int x = 0; x < out->width; x++) { \
663 dst[x] = mix(mix(xf0[x], bg, smoothstep(1.f-phase, 1.f, progress)), \
664 mix(bg, xf1[x], smoothstep(phase, 1.f, progress)), \
668 dst += out->linesize[p] / div; \
669 xf0 += a->linesize[p] / div; \
670 xf1 += b->linesize[p] / div; \
675 FADEBLACK_TRANSITION(8, uint8_t, 1)
676 FADEBLACK_TRANSITION(16, uint16_t, 2)
678 #define FADEWHITE_TRANSITION(name, type, div) \
679 static void fadewhite##name##_transition(AVFilterContext *ctx, \
680 const AVFrame *a, const AVFrame *b, AVFrame *out, \
682 int slice_start, int slice_end, int jobnr) \
684 XFadeContext *s = ctx->priv; \
685 const int height = slice_end - slice_start; \
686 const float phase = 0.2f; \
688 for (int p = 0; p < s->nb_planes; p++) { \
689 const type *xf0 = (const type *)(a->data[p] + slice_start * a->linesize[p]); \
690 const type *xf1 = (const type *)(b->data[p] + slice_start * b->linesize[p]); \
691 type *dst = (type *)(out->data[p] + slice_start * out->linesize[p]); \
692 const int bg = s->white[p]; \
694 for (int y = 0; y < height; y++) { \
695 for (int x = 0; x < out->width; x++) { \
696 dst[x] = mix(mix(xf0[x], bg, smoothstep(1.f-phase, 1.f, progress)), \
697 mix(bg, xf1[x], smoothstep(phase, 1.f, progress)), \
701 dst += out->linesize[p] / div; \
702 xf0 += a->linesize[p] / div; \
703 xf1 += b->linesize[p] / div; \
708 FADEWHITE_TRANSITION(8, uint8_t, 1)
709 FADEWHITE_TRANSITION(16, uint16_t, 2)
711 #define RADIAL_TRANSITION(name, type, div) \
712 static void radial##name##_transition(AVFilterContext *ctx, \
713 const AVFrame *a, const AVFrame *b, AVFrame *out, \
715 int slice_start, int slice_end, int jobnr) \
717 XFadeContext *s = ctx->priv; \
718 const int width = out->width; \
719 const int height = out->height; \
721 for (int y = slice_start; y < slice_end; y++) { \
722 for (int x = 0; x < width; x++) { \
723 const float smooth = atan2f(x - width / 2, y - height / 2) - \
724 (progress - 0.5f) * (M_PI * 2.5f); \
725 for (int p = 0; p < s->nb_planes; p++) { \
726 const type *xf0 = (const type *)(a->data[p] + y * a->linesize[p]); \
727 const type *xf1 = (const type *)(b->data[p] + y * b->linesize[p]); \
728 type *dst = (type *)(out->data[p] + y * out->linesize[p]); \
730 dst[x] = mix(xf1[x], xf0[x], smoothstep(0.f, 1.f, smooth)); \
736 RADIAL_TRANSITION(8, uint8_t, 1)
737 RADIAL_TRANSITION(16, uint16_t, 2)
739 #define SMOOTHLEFT_TRANSITION(name, type, div) \
740 static void smoothleft##name##_transition(AVFilterContext *ctx, \
741 const AVFrame *a, const AVFrame *b, AVFrame *out, \
743 int slice_start, int slice_end, int jobnr) \
745 XFadeContext *s = ctx->priv; \
746 const int width = out->width; \
747 const float w = width; \
749 for (int y = slice_start; y < slice_end; y++) { \
750 for (int x = 0; x < width; x++) { \
751 const float smooth = 1.f + x / w - progress * 2.f; \
753 for (int p = 0; p < s->nb_planes; p++) { \
754 const type *xf0 = (const type *)(a->data[p] + y * a->linesize[p]); \
755 const type *xf1 = (const type *)(b->data[p] + y * b->linesize[p]); \
756 type *dst = (type *)(out->data[p] + y * out->linesize[p]); \
758 dst[x] = mix(xf1[x], xf0[x], smoothstep(0.f, 1.f, smooth)); \
764 SMOOTHLEFT_TRANSITION(8, uint8_t, 1)
765 SMOOTHLEFT_TRANSITION(16, uint16_t, 2)
767 #define SMOOTHRIGHT_TRANSITION(name, type, div) \
768 static void smoothright##name##_transition(AVFilterContext *ctx, \
769 const AVFrame *a, const AVFrame *b, AVFrame *out, \
771 int slice_start, int slice_end, int jobnr) \
773 XFadeContext *s = ctx->priv; \
774 const int width = out->width; \
775 const float w = width; \
777 for (int y = slice_start; y < slice_end; y++) { \
778 for (int x = 0; x < width; x++) { \
779 const float smooth = 1.f + (w - 1 - x) / w - progress * 2.f; \
781 for (int p = 0; p < s->nb_planes; p++) { \
782 const type *xf0 = (const type *)(a->data[p] + y * a->linesize[p]); \
783 const type *xf1 = (const type *)(b->data[p] + y * b->linesize[p]); \
784 type *dst = (type *)(out->data[p] + y * out->linesize[p]); \
786 dst[x] = mix(xf1[x], xf0[x], smoothstep(0.f, 1.f, smooth)); \
792 SMOOTHRIGHT_TRANSITION(8, uint8_t, 1)
793 SMOOTHRIGHT_TRANSITION(16, uint16_t, 2)
795 #define SMOOTHUP_TRANSITION(name, type, div) \
796 static void smoothup##name##_transition(AVFilterContext *ctx, \
797 const AVFrame *a, const AVFrame *b, AVFrame *out, \
799 int slice_start, int slice_end, int jobnr) \
801 XFadeContext *s = ctx->priv; \
802 const int width = out->width; \
803 const float h = out->height; \
805 for (int y = slice_start; y < slice_end; y++) { \
806 const float smooth = 1.f + y / h - progress * 2.f; \
807 for (int x = 0; x < width; x++) { \
808 for (int p = 0; p < s->nb_planes; p++) { \
809 const type *xf0 = (const type *)(a->data[p] + y * a->linesize[p]); \
810 const type *xf1 = (const type *)(b->data[p] + y * b->linesize[p]); \
811 type *dst = (type *)(out->data[p] + y * out->linesize[p]); \
813 dst[x] = mix(xf1[x], xf0[x], smoothstep(0.f, 1.f, smooth)); \
819 SMOOTHUP_TRANSITION(8, uint8_t, 1)
820 SMOOTHUP_TRANSITION(16, uint16_t, 2)
822 #define SMOOTHDOWN_TRANSITION(name, type, div) \
823 static void smoothdown##name##_transition(AVFilterContext *ctx, \
824 const AVFrame *a, const AVFrame *b, AVFrame *out, \
826 int slice_start, int slice_end, int jobnr) \
828 XFadeContext *s = ctx->priv; \
829 const int width = out->width; \
830 const float h = out->height; \
832 for (int y = slice_start; y < slice_end; y++) { \
833 const float smooth = 1.f + (h - 1 - y) / h - progress * 2.f; \
834 for (int x = 0; x < width; x++) { \
835 for (int p = 0; p < s->nb_planes; p++) { \
836 const type *xf0 = (const type *)(a->data[p] + y * a->linesize[p]); \
837 const type *xf1 = (const type *)(b->data[p] + y * b->linesize[p]); \
838 type *dst = (type *)(out->data[p] + y * out->linesize[p]); \
840 dst[x] = mix(xf1[x], xf0[x], smoothstep(0.f, 1.f, smooth)); \
846 SMOOTHDOWN_TRANSITION(8, uint8_t, 1)
847 SMOOTHDOWN_TRANSITION(16, uint16_t, 2)
849 #define CIRCLEOPEN_TRANSITION(name, type, div) \
850 static void circleopen##name##_transition(AVFilterContext *ctx, \
851 const AVFrame *a, const AVFrame *b, AVFrame *out, \
853 int slice_start, int slice_end, int jobnr) \
855 XFadeContext *s = ctx->priv; \
856 const int width = out->width; \
857 const int height = out->height; \
858 const float z = hypotf(width / 2, height / 2); \
859 const float p = (progress - 0.5f) * 3.f; \
861 for (int y = slice_start; y < slice_end; y++) { \
862 for (int x = 0; x < width; x++) { \
863 const float smooth = hypotf(x - width / 2, y - height / 2) / z + p; \
864 for (int p = 0; p < s->nb_planes; p++) { \
865 const type *xf0 = (const type *)(a->data[p] + y * a->linesize[p]); \
866 const type *xf1 = (const type *)(b->data[p] + y * b->linesize[p]); \
867 type *dst = (type *)(out->data[p] + y * out->linesize[p]); \
869 dst[x] = mix(xf0[x], xf1[x], smoothstep(0.f, 1.f, smooth)); \
875 CIRCLEOPEN_TRANSITION(8, uint8_t, 1)
876 CIRCLEOPEN_TRANSITION(16, uint16_t, 2)
878 #define CIRCLECLOSE_TRANSITION(name, type, div) \
879 static void circleclose##name##_transition(AVFilterContext *ctx, \
880 const AVFrame *a, const AVFrame *b, AVFrame *out, \
882 int slice_start, int slice_end, int jobnr) \
884 XFadeContext *s = ctx->priv; \
885 const int width = out->width; \
886 const int height = out->height; \
887 const float z = hypotf(width / 2, height / 2); \
888 const float p = (1.f - progress - 0.5f) * 3.f; \
890 for (int y = slice_start; y < slice_end; y++) { \
891 for (int x = 0; x < width; x++) { \
892 const float smooth = hypotf(x - width / 2, y - height / 2) / z + p; \
893 for (int p = 0; p < s->nb_planes; p++) { \
894 const type *xf0 = (const type *)(a->data[p] + y * a->linesize[p]); \
895 const type *xf1 = (const type *)(b->data[p] + y * b->linesize[p]); \
896 type *dst = (type *)(out->data[p] + y * out->linesize[p]); \
898 dst[x] = mix(xf1[x], xf0[x], smoothstep(0.f, 1.f, smooth)); \
904 CIRCLECLOSE_TRANSITION(8, uint8_t, 1)
905 CIRCLECLOSE_TRANSITION(16, uint16_t, 2)
907 #define VERTOPEN_TRANSITION(name, type, div) \
908 static void vertopen##name##_transition(AVFilterContext *ctx, \
909 const AVFrame *a, const AVFrame *b, AVFrame *out, \
911 int slice_start, int slice_end, int jobnr) \
913 XFadeContext *s = ctx->priv; \
914 const int width = out->width; \
915 const float w2 = out->width / 2; \
917 for (int y = slice_start; y < slice_end; y++) { \
918 for (int x = 0; x < width; x++) { \
919 const float smooth = 2.f - fabsf((x - w2) / w2) - progress * 2.f; \
920 for (int p = 0; p < s->nb_planes; p++) { \
921 const type *xf0 = (const type *)(a->data[p] + y * a->linesize[p]); \
922 const type *xf1 = (const type *)(b->data[p] + y * b->linesize[p]); \
923 type *dst = (type *)(out->data[p] + y * out->linesize[p]); \
925 dst[x] = mix(xf1[x], xf0[x], smoothstep(0.f, 1.f, smooth)); \
931 VERTOPEN_TRANSITION(8, uint8_t, 1)
932 VERTOPEN_TRANSITION(16, uint16_t, 2)
934 #define VERTCLOSE_TRANSITION(name, type, div) \
935 static void vertclose##name##_transition(AVFilterContext *ctx, \
936 const AVFrame *a, const AVFrame *b, AVFrame *out, \
938 int slice_start, int slice_end, int jobnr) \
940 XFadeContext *s = ctx->priv; \
941 const int width = out->width; \
942 const float w2 = out->width / 2; \
944 for (int y = slice_start; y < slice_end; y++) { \
945 for (int x = 0; x < width; x++) { \
946 const float smooth = 1.f + fabsf((x - w2) / w2) - progress * 2.f; \
947 for (int p = 0; p < s->nb_planes; p++) { \
948 const type *xf0 = (const type *)(a->data[p] + y * a->linesize[p]); \
949 const type *xf1 = (const type *)(b->data[p] + y * b->linesize[p]); \
950 type *dst = (type *)(out->data[p] + y * out->linesize[p]); \
952 dst[x] = mix(xf1[x], xf0[x], smoothstep(0.f, 1.f, smooth)); \
958 VERTCLOSE_TRANSITION(8, uint8_t, 1)
959 VERTCLOSE_TRANSITION(16, uint16_t, 2)
961 #define HORZOPEN_TRANSITION(name, type, div) \
962 static void horzopen##name##_transition(AVFilterContext *ctx, \
963 const AVFrame *a, const AVFrame *b, AVFrame *out, \
965 int slice_start, int slice_end, int jobnr) \
967 XFadeContext *s = ctx->priv; \
968 const int width = out->width; \
969 const float h2 = out->height / 2; \
971 for (int y = slice_start; y < slice_end; y++) { \
972 const float smooth = 2.f - fabsf((y - h2) / h2) - progress * 2.f; \
973 for (int x = 0; x < width; x++) { \
974 for (int p = 0; p < s->nb_planes; p++) { \
975 const type *xf0 = (const type *)(a->data[p] + y * a->linesize[p]); \
976 const type *xf1 = (const type *)(b->data[p] + y * b->linesize[p]); \
977 type *dst = (type *)(out->data[p] + y * out->linesize[p]); \
979 dst[x] = mix(xf1[x], xf0[x], smoothstep(0.f, 1.f, smooth)); \
985 HORZOPEN_TRANSITION(8, uint8_t, 1)
986 HORZOPEN_TRANSITION(16, uint16_t, 2)
988 #define HORZCLOSE_TRANSITION(name, type, div) \
989 static void horzclose##name##_transition(AVFilterContext *ctx, \
990 const AVFrame *a, const AVFrame *b, AVFrame *out, \
992 int slice_start, int slice_end, int jobnr) \
994 XFadeContext *s = ctx->priv; \
995 const int width = out->width; \
996 const float h2 = out->height / 2; \
998 for (int y = slice_start; y < slice_end; y++) { \
999 const float smooth = 1.f + fabsf((y - h2) / h2) - progress * 2.f; \
1000 for (int x = 0; x < width; x++) { \
1001 for (int p = 0; p < s->nb_planes; p++) { \
1002 const type *xf0 = (const type *)(a->data[p] + y * a->linesize[p]); \
1003 const type *xf1 = (const type *)(b->data[p] + y * b->linesize[p]); \
1004 type *dst = (type *)(out->data[p] + y * out->linesize[p]); \
1006 dst[x] = mix(xf1[x], xf0[x], smoothstep(0.f, 1.f, smooth)); \
1012 HORZCLOSE_TRANSITION(8, uint8_t, 1)
1013 HORZCLOSE_TRANSITION(16, uint16_t, 2)
1015 static float frand(int x, int y)
1017 const float r = sinf(x * 12.9898f + y * 78.233f) * 43758.545f;
1019 return r - floorf(r);
1022 #define DISSOLVE_TRANSITION(name, type, div) \
1023 static void dissolve##name##_transition(AVFilterContext *ctx, \
1024 const AVFrame *a, const AVFrame *b, AVFrame *out, \
1026 int slice_start, int slice_end, int jobnr) \
1028 XFadeContext *s = ctx->priv; \
1029 const int width = out->width; \
1031 for (int y = slice_start; y < slice_end; y++) { \
1032 for (int x = 0; x < width; x++) { \
1033 const float smooth = frand(x, y) * 2.f + progress * 2.f - 1.5f; \
1034 for (int p = 0; p < s->nb_planes; p++) { \
1035 const type *xf0 = (const type *)(a->data[p] + y * a->linesize[p]); \
1036 const type *xf1 = (const type *)(b->data[p] + y * b->linesize[p]); \
1037 type *dst = (type *)(out->data[p] + y * out->linesize[p]); \
1039 dst[x] = smooth >= 0.5f ? xf0[x] : xf1[x]; \
1045 DISSOLVE_TRANSITION(8, uint8_t, 1)
1046 DISSOLVE_TRANSITION(16, uint16_t, 2)
1048 #define PIXELIZE_TRANSITION(name, type, div) \
1049 static void pixelize##name##_transition(AVFilterContext *ctx, \
1050 const AVFrame *a, const AVFrame *b, AVFrame *out, \
1052 int slice_start, int slice_end, int jobnr) \
1054 XFadeContext *s = ctx->priv; \
1055 const int w = out->width; \
1056 const int h = out->height; \
1057 const float d = fminf(progress, 1.f - progress); \
1058 const float dist = ceilf(d * 50.f) / 50.f; \
1059 const float sqx = 2.f * dist * FFMIN(w, h) / 20.f; \
1060 const float sqy = 2.f * dist * FFMIN(w, h) / 20.f; \
1062 for (int y = slice_start; y < slice_end; y++) { \
1063 for (int x = 0; x < w; x++) { \
1064 int sx = dist > 0.f ? FFMIN((floorf(x / sqx) + .5f) * sqx, w - 1) : x; \
1065 int sy = dist > 0.f ? FFMIN((floorf(y / sqy) + .5f) * sqy, h - 1) : y; \
1066 for (int p = 0; p < s->nb_planes; p++) { \
1067 const type *xf0 = (const type *)(a->data[p] + sy * a->linesize[p]); \
1068 const type *xf1 = (const type *)(b->data[p] + sy * b->linesize[p]); \
1069 type *dst = (type *)(out->data[p] + y * out->linesize[p]); \
1071 dst[x] = mix(xf0[sx], xf1[sx], progress); \
1077 PIXELIZE_TRANSITION(8, uint8_t, 1)
1078 PIXELIZE_TRANSITION(16, uint16_t, 2)
1080 #define DIAGTL_TRANSITION(name, type, div) \
1081 static void diagtl##name##_transition(AVFilterContext *ctx, \
1082 const AVFrame *a, const AVFrame *b, AVFrame *out, \
1084 int slice_start, int slice_end, int jobnr) \
1086 XFadeContext *s = ctx->priv; \
1087 const int width = out->width; \
1088 const float w = width; \
1089 const float h = out->height; \
1091 for (int y = slice_start; y < slice_end; y++) { \
1092 for (int x = 0; x < width; x++) { \
1093 const float smooth = 1.f + x / w * y / h - progress * 2.f; \
1095 for (int p = 0; p < s->nb_planes; p++) { \
1096 const type *xf0 = (const type *)(a->data[p] + y * a->linesize[p]); \
1097 const type *xf1 = (const type *)(b->data[p] + y * b->linesize[p]); \
1098 type *dst = (type *)(out->data[p] + y * out->linesize[p]); \
1100 dst[x] = mix(xf1[x], xf0[x], smoothstep(0.f, 1.f, smooth)); \
1106 DIAGTL_TRANSITION(8, uint8_t, 1)
1107 DIAGTL_TRANSITION(16, uint16_t, 2)
1109 #define DIAGTR_TRANSITION(name, type, div) \
1110 static void diagtr##name##_transition(AVFilterContext *ctx, \
1111 const AVFrame *a, const AVFrame *b, AVFrame *out, \
1113 int slice_start, int slice_end, int jobnr) \
1115 XFadeContext *s = ctx->priv; \
1116 const int width = out->width; \
1117 const float w = width; \
1118 const float h = out->height; \
1120 for (int y = slice_start; y < slice_end; y++) { \
1121 for (int x = 0; x < width; x++) { \
1122 const float smooth = 1.f + (w - 1 - x) / w * y / h - progress * 2.f; \
1124 for (int p = 0; p < s->nb_planes; p++) { \
1125 const type *xf0 = (const type *)(a->data[p] + y * a->linesize[p]); \
1126 const type *xf1 = (const type *)(b->data[p] + y * b->linesize[p]); \
1127 type *dst = (type *)(out->data[p] + y * out->linesize[p]); \
1129 dst[x] = mix(xf1[x], xf0[x], smoothstep(0.f, 1.f, smooth)); \
1135 DIAGTR_TRANSITION(8, uint8_t, 1)
1136 DIAGTR_TRANSITION(16, uint16_t, 2)
1138 #define DIAGBL_TRANSITION(name, type, div) \
1139 static void diagbl##name##_transition(AVFilterContext *ctx, \
1140 const AVFrame *a, const AVFrame *b, AVFrame *out, \
1142 int slice_start, int slice_end, int jobnr) \
1144 XFadeContext *s = ctx->priv; \
1145 const int width = out->width; \
1146 const float w = width; \
1147 const float h = out->height; \
1149 for (int y = slice_start; y < slice_end; y++) { \
1150 for (int x = 0; x < width; x++) { \
1151 const float smooth = 1.f + x / w * (h - 1 - y) / h - progress * 2.f; \
1153 for (int p = 0; p < s->nb_planes; p++) { \
1154 const type *xf0 = (const type *)(a->data[p] + y * a->linesize[p]); \
1155 const type *xf1 = (const type *)(b->data[p] + y * b->linesize[p]); \
1156 type *dst = (type *)(out->data[p] + y * out->linesize[p]); \
1158 dst[x] = mix(xf1[x], xf0[x], smoothstep(0.f, 1.f, smooth)); \
1164 DIAGBL_TRANSITION(8, uint8_t, 1)
1165 DIAGBL_TRANSITION(16, uint16_t, 2)
1167 #define DIAGBR_TRANSITION(name, type, div) \
1168 static void diagbr##name##_transition(AVFilterContext *ctx, \
1169 const AVFrame *a, const AVFrame *b, AVFrame *out, \
1171 int slice_start, int slice_end, int jobnr) \
1173 XFadeContext *s = ctx->priv; \
1174 const int width = out->width; \
1175 const float w = width; \
1176 const float h = out->height; \
1178 for (int y = slice_start; y < slice_end; y++) { \
1179 for (int x = 0; x < width; x++) { \
1180 const float smooth = 1.f + (w - 1 - x) / w * (h - 1 - y) / h - \
1183 for (int p = 0; p < s->nb_planes; p++) { \
1184 const type *xf0 = (const type *)(a->data[p] + y * a->linesize[p]); \
1185 const type *xf1 = (const type *)(b->data[p] + y * b->linesize[p]); \
1186 type *dst = (type *)(out->data[p] + y * out->linesize[p]); \
1188 dst[x] = mix(xf1[x], xf0[x], smoothstep(0.f, 1.f, smooth)); \
1194 DIAGBR_TRANSITION(8, uint8_t, 1)
1195 DIAGBR_TRANSITION(16, uint16_t, 2)
1197 #define HLSLICE_TRANSITION(name, type, div) \
1198 static void hlslice##name##_transition(AVFilterContext *ctx, \
1199 const AVFrame *a, const AVFrame *b, AVFrame *out, \
1201 int slice_start, int slice_end, int jobnr) \
1203 XFadeContext *s = ctx->priv; \
1204 const int width = out->width; \
1205 const float w = width; \
1207 for (int y = slice_start; y < slice_end; y++) { \
1208 for (int x = 0; x < width; x++) { \
1209 const float smooth = smoothstep(-0.5f, 0.f, x / w - progress * 1.5f); \
1210 const float ss = smooth <= fract(10.f * x / w) ? 0.f : 1.f; \
1212 for (int p = 0; p < s->nb_planes; p++) { \
1213 const type *xf0 = (const type *)(a->data[p] + y * a->linesize[p]); \
1214 const type *xf1 = (const type *)(b->data[p] + y * b->linesize[p]); \
1215 type *dst = (type *)(out->data[p] + y * out->linesize[p]); \
1217 dst[x] = mix(xf1[x], xf0[x], ss); \
1223 HLSLICE_TRANSITION(8, uint8_t, 1)
1224 HLSLICE_TRANSITION(16, uint16_t, 2)
1226 #define HRSLICE_TRANSITION(name, type, div) \
1227 static void hrslice##name##_transition(AVFilterContext *ctx, \
1228 const AVFrame *a, const AVFrame *b, AVFrame *out, \
1230 int slice_start, int slice_end, int jobnr) \
1232 XFadeContext *s = ctx->priv; \
1233 const int width = out->width; \
1234 const float w = width; \
1236 for (int y = slice_start; y < slice_end; y++) { \
1237 for (int x = 0; x < width; x++) { \
1238 const float xx = (w - 1 - x) / w; \
1239 const float smooth = smoothstep(-0.5f, 0.f, xx - progress * 1.5f); \
1240 const float ss = smooth <= fract(10.f * xx) ? 0.f : 1.f; \
1242 for (int p = 0; p < s->nb_planes; p++) { \
1243 const type *xf0 = (const type *)(a->data[p] + y * a->linesize[p]); \
1244 const type *xf1 = (const type *)(b->data[p] + y * b->linesize[p]); \
1245 type *dst = (type *)(out->data[p] + y * out->linesize[p]); \
1247 dst[x] = mix(xf1[x], xf0[x], ss); \
1253 HRSLICE_TRANSITION(8, uint8_t, 1)
1254 HRSLICE_TRANSITION(16, uint16_t, 2)
1256 #define VUSLICE_TRANSITION(name, type, div) \
1257 static void vuslice##name##_transition(AVFilterContext *ctx, \
1258 const AVFrame *a, const AVFrame *b, AVFrame *out, \
1260 int slice_start, int slice_end, int jobnr) \
1262 XFadeContext *s = ctx->priv; \
1263 const int width = out->width; \
1264 const float h = out->height; \
1266 for (int y = slice_start; y < slice_end; y++) { \
1267 const float smooth = smoothstep(-0.5f, 0.f, y / h - progress * 1.5f); \
1268 const float ss = smooth <= fract(10.f * y / h) ? 0.f : 1.f; \
1270 for (int x = 0; x < width; x++) { \
1271 for (int p = 0; p < s->nb_planes; p++) { \
1272 const type *xf0 = (const type *)(a->data[p] + y * a->linesize[p]); \
1273 const type *xf1 = (const type *)(b->data[p] + y * b->linesize[p]); \
1274 type *dst = (type *)(out->data[p] + y * out->linesize[p]); \
1276 dst[x] = mix(xf1[x], xf0[x], ss); \
1282 VUSLICE_TRANSITION(8, uint8_t, 1)
1283 VUSLICE_TRANSITION(16, uint16_t, 2)
1285 #define VDSLICE_TRANSITION(name, type, div) \
1286 static void vdslice##name##_transition(AVFilterContext *ctx, \
1287 const AVFrame *a, const AVFrame *b, AVFrame *out, \
1289 int slice_start, int slice_end, int jobnr) \
1291 XFadeContext *s = ctx->priv; \
1292 const int width = out->width; \
1293 const float h = out->height; \
1295 for (int y = slice_start; y < slice_end; y++) { \
1296 const float yy = (h - 1 - y) / h; \
1297 const float smooth = smoothstep(-0.5f, 0.f, yy - progress * 1.5f); \
1298 const float ss = smooth <= fract(10.f * yy) ? 0.f : 1.f; \
1300 for (int x = 0; x < width; x++) { \
1301 for (int p = 0; p < s->nb_planes; p++) { \
1302 const type *xf0 = (const type *)(a->data[p] + y * a->linesize[p]); \
1303 const type *xf1 = (const type *)(b->data[p] + y * b->linesize[p]); \
1304 type *dst = (type *)(out->data[p] + y * out->linesize[p]); \
1306 dst[x] = mix(xf1[x], xf0[x], ss); \
1312 VDSLICE_TRANSITION(8, uint8_t, 1)
1313 VDSLICE_TRANSITION(16, uint16_t, 2)
1315 #define HBLUR_TRANSITION(name, type, div) \
1316 static void hblur##name##_transition(AVFilterContext *ctx, \
1317 const AVFrame *a, const AVFrame *b, AVFrame *out, \
1319 int slice_start, int slice_end, int jobnr) \
1321 XFadeContext *s = ctx->priv; \
1322 const int width = out->width; \
1323 const float prog = progress <= 0.5f ? progress * 2.f : (1.f - progress) * 2.f; \
1324 const int size = 1 + (width / 2) * prog; \
1326 for (int y = slice_start; y < slice_end; y++) { \
1327 for (int p = 0; p < s->nb_planes; p++) { \
1328 const type *xf0 = (const type *)(a->data[p] + y * a->linesize[p]); \
1329 const type *xf1 = (const type *)(b->data[p] + y * b->linesize[p]); \
1330 type *dst = (type *)(out->data[p] + y * out->linesize[p]); \
1335 for (int x = 0; x < size; x++) { \
1340 for (int x = 0; x < width; x++) { \
1341 dst[x] = mix(sum0 / cnt, sum1 / cnt, progress); \
1343 if (x + size < width) { \
1344 sum0 += xf0[x + size] - xf0[x]; \
1345 sum1 += xf1[x + size] - xf1[x]; \
1356 HBLUR_TRANSITION(8, uint8_t, 1)
1357 HBLUR_TRANSITION(16, uint16_t, 2)
1359 #define FADEGRAYS_TRANSITION(name, type, div) \
1360 static void fadegrays##name##_transition(AVFilterContext *ctx, \
1361 const AVFrame *a, const AVFrame *b, AVFrame *out, \
1363 int slice_start, int slice_end, int jobnr) \
1365 XFadeContext *s = ctx->priv; \
1366 const int width = out->width; \
1367 const int is_rgb = s->is_rgb; \
1368 const int mid = (s->max_value + 1) / 2; \
1369 const float phase = 0.2f; \
1371 for (int y = slice_start; y < slice_end; y++) { \
1372 for (int x = 0; x < width; x++) { \
1375 for (int p = 0; p < s->nb_planes; p++) { \
1376 const type *xf0 = (const type *)(a->data[p] + \
1377 y * a->linesize[p]); \
1378 const type *xf1 = (const type *)(b->data[p] + \
1379 y * b->linesize[p]); \
1381 bg[0][3] = xf0[x]; \
1382 bg[1][3] = xf1[x]; \
1384 bg[0][0] += xf0[x]; \
1385 bg[1][0] += xf1[x]; \
1388 bg[0][0] = bg[0][0] / 3; \
1389 bg[1][0] = bg[1][0] / 3; \
1390 bg[0][1] = bg[0][2] = bg[0][0]; \
1391 bg[1][1] = bg[1][2] = bg[1][0]; \
1393 const type *yf0 = (const type *)(a->data[0] + \
1394 y * a->linesize[0]); \
1395 const type *yf1 = (const type *)(b->data[0] + \
1396 y * a->linesize[0]); \
1397 bg[0][0] = yf0[x]; \
1398 bg[1][0] = yf1[x]; \
1399 if (s->nb_planes == 4) { \
1400 const type *af0 = (const type *)(a->data[3] + \
1401 y * a->linesize[3]); \
1402 const type *af1 = (const type *)(b->data[3] + \
1403 y * a->linesize[3]); \
1404 bg[0][3] = af0[x]; \
1405 bg[1][3] = af1[x]; \
1407 bg[0][1] = bg[1][1] = mid; \
1408 bg[0][2] = bg[1][2] = mid; \
1411 for (int p = 0; p < s->nb_planes; p++) { \
1412 const type *xf0 = (const type *)(a->data[p] + y * a->linesize[p]); \
1413 const type *xf1 = (const type *)(b->data[p] + y * b->linesize[p]); \
1414 type *dst = (type *)(out->data[p] + y * out->linesize[p]); \
1416 dst[x] = mix(mix(xf0[x], bg[0][p], \
1417 smoothstep(1.f-phase, 1.f, progress)), \
1418 mix(bg[1][p], xf1[x], smoothstep(phase, 1.f, progress)), \
1425 FADEGRAYS_TRANSITION(8, uint8_t, 1)
1426 FADEGRAYS_TRANSITION(16, uint16_t, 2)
1428 #define WIPETL_TRANSITION(name, type, div) \
1429 static void wipetl##name##_transition(AVFilterContext *ctx, \
1430 const AVFrame *a, const AVFrame *b, AVFrame *out, \
1432 int slice_start, int slice_end, int jobnr) \
1434 XFadeContext *s = ctx->priv; \
1435 const int height = slice_end - slice_start; \
1436 const int zw = out->width * progress; \
1437 const int zh = out->height * progress; \
1439 for (int p = 0; p < s->nb_planes; p++) { \
1440 const type *xf0 = (const type *)(a->data[p] + slice_start * a->linesize[p]); \
1441 const type *xf1 = (const type *)(b->data[p] + slice_start * b->linesize[p]); \
1442 type *dst = (type *)(out->data[p] + slice_start * out->linesize[p]); \
1444 for (int y = 0; y < height; y++) { \
1445 for (int x = 0; x < out->width; x++) { \
1446 dst[x] = slice_start + y <= zh && \
1447 x <= zw ? xf0[x] : xf1[x]; \
1450 dst += out->linesize[p] / div; \
1451 xf0 += a->linesize[p] / div; \
1452 xf1 += b->linesize[p] / div; \
1457 WIPETL_TRANSITION(8, uint8_t, 1)
1458 WIPETL_TRANSITION(16, uint16_t, 2)
1460 #define WIPETR_TRANSITION(name, type, div) \
1461 static void wipetr##name##_transition(AVFilterContext *ctx, \
1462 const AVFrame *a, const AVFrame *b, AVFrame *out, \
1464 int slice_start, int slice_end, int jobnr) \
1466 XFadeContext *s = ctx->priv; \
1467 const int height = slice_end - slice_start; \
1468 const int zw = out->width * (1.f - progress); \
1469 const int zh = out->height * progress; \
1471 for (int p = 0; p < s->nb_planes; p++) { \
1472 const type *xf0 = (const type *)(a->data[p] + slice_start * a->linesize[p]); \
1473 const type *xf1 = (const type *)(b->data[p] + slice_start * b->linesize[p]); \
1474 type *dst = (type *)(out->data[p] + slice_start * out->linesize[p]); \
1476 for (int y = 0; y < height; y++) { \
1477 for (int x = 0; x < out->width; x++) { \
1478 dst[x] = slice_start + y <= zh && \
1479 x > zw ? xf0[x] : xf1[x]; \
1482 dst += out->linesize[p] / div; \
1483 xf0 += a->linesize[p] / div; \
1484 xf1 += b->linesize[p] / div; \
1489 WIPETR_TRANSITION(8, uint8_t, 1)
1490 WIPETR_TRANSITION(16, uint16_t, 2)
1492 #define WIPEBL_TRANSITION(name, type, div) \
1493 static void wipebl##name##_transition(AVFilterContext *ctx, \
1494 const AVFrame *a, const AVFrame *b, AVFrame *out, \
1496 int slice_start, int slice_end, int jobnr) \
1498 XFadeContext *s = ctx->priv; \
1499 const int height = slice_end - slice_start; \
1500 const int zw = out->width * progress; \
1501 const int zh = out->height * (1.f - progress); \
1503 for (int p = 0; p < s->nb_planes; p++) { \
1504 const type *xf0 = (const type *)(a->data[p] + slice_start * a->linesize[p]); \
1505 const type *xf1 = (const type *)(b->data[p] + slice_start * b->linesize[p]); \
1506 type *dst = (type *)(out->data[p] + slice_start * out->linesize[p]); \
1508 for (int y = 0; y < height; y++) { \
1509 for (int x = 0; x < out->width; x++) { \
1510 dst[x] = slice_start + y > zh && \
1511 x <= zw ? xf0[x] : xf1[x]; \
1514 dst += out->linesize[p] / div; \
1515 xf0 += a->linesize[p] / div; \
1516 xf1 += b->linesize[p] / div; \
1521 WIPEBL_TRANSITION(8, uint8_t, 1)
1522 WIPEBL_TRANSITION(16, uint16_t, 2)
1524 #define WIPEBR_TRANSITION(name, type, div) \
1525 static void wipebr##name##_transition(AVFilterContext *ctx, \
1526 const AVFrame *a, const AVFrame *b, AVFrame *out, \
1528 int slice_start, int slice_end, int jobnr) \
1530 XFadeContext *s = ctx->priv; \
1531 const int height = slice_end - slice_start; \
1532 const int zh = out->height * (1.f - progress); \
1533 const int zw = out->width * (1.f - progress); \
1535 for (int p = 0; p < s->nb_planes; p++) { \
1536 const type *xf0 = (const type *)(a->data[p] + slice_start * a->linesize[p]); \
1537 const type *xf1 = (const type *)(b->data[p] + slice_start * b->linesize[p]); \
1538 type *dst = (type *)(out->data[p] + slice_start * out->linesize[p]); \
1540 for (int y = 0; y < height; y++) { \
1541 for (int x = 0; x < out->width; x++) { \
1542 dst[x] = slice_start + y > zh && \
1543 x > zw ? xf0[x] : xf1[x]; \
1546 dst += out->linesize[p] / div; \
1547 xf0 += a->linesize[p] / div; \
1548 xf1 += b->linesize[p] / div; \
1553 WIPEBR_TRANSITION(8, uint8_t, 1)
1554 WIPEBR_TRANSITION(16, uint16_t, 2)
1556 static inline double getpix(void *priv, double x, double y, int plane, int nb)
1558 XFadeContext *s = priv;
1559 AVFrame *in = s->xf[nb];
1560 const uint8_t *src = in->data[FFMIN(plane, s->nb_planes - 1)];
1561 int linesize = in->linesize[FFMIN(plane, s->nb_planes - 1)];
1562 const int w = in->width;
1563 const int h = in->height;
1567 xi = av_clipd(x, 0, w - 1);
1568 yi = av_clipd(y, 0, h - 1);
1571 const uint16_t *src16 = (const uint16_t*)src;
1574 return src16[xi + yi * linesize];
1576 return src[xi + yi * linesize];
1580 static double a0(void *priv, double x, double y) { return getpix(priv, x, y, 0, 0); }
1581 static double a1(void *priv, double x, double y) { return getpix(priv, x, y, 1, 0); }
1582 static double a2(void *priv, double x, double y) { return getpix(priv, x, y, 2, 0); }
1583 static double a3(void *priv, double x, double y) { return getpix(priv, x, y, 3, 0); }
1585 static double b0(void *priv, double x, double y) { return getpix(priv, x, y, 0, 1); }
1586 static double b1(void *priv, double x, double y) { return getpix(priv, x, y, 1, 1); }
1587 static double b2(void *priv, double x, double y) { return getpix(priv, x, y, 2, 1); }
1588 static double b3(void *priv, double x, double y) { return getpix(priv, x, y, 3, 1); }
1590 static int config_output(AVFilterLink *outlink)
1592 AVFilterContext *ctx = outlink->src;
1593 AVFilterLink *inlink0 = ctx->inputs[0];
1594 AVFilterLink *inlink1 = ctx->inputs[1];
1595 XFadeContext *s = ctx->priv;
1596 const AVPixFmtDescriptor *pix_desc = av_pix_fmt_desc_get(inlink0->format);
1598 if (inlink0->format != inlink1->format) {
1599 av_log(ctx, AV_LOG_ERROR, "inputs must be of same pixel format\n");
1600 return AVERROR(EINVAL);
1602 if (inlink0->w != inlink1->w || inlink0->h != inlink1->h) {
1603 av_log(ctx, AV_LOG_ERROR, "First input link %s parameters "
1604 "(size %dx%d) do not match the corresponding "
1605 "second input link %s parameters (size %dx%d)\n",
1606 ctx->input_pads[0].name, inlink0->w, inlink0->h,
1607 ctx->input_pads[1].name, inlink1->w, inlink1->h);
1608 return AVERROR(EINVAL);
1611 if (inlink0->time_base.num != inlink1->time_base.num ||
1612 inlink0->time_base.den != inlink1->time_base.den) {
1613 av_log(ctx, AV_LOG_ERROR, "First input link %s timebase "
1614 "(%d/%d) do not match the corresponding "
1615 "second input link %s timebase (%d/%d)\n",
1616 ctx->input_pads[0].name, inlink0->time_base.num, inlink0->time_base.den,
1617 ctx->input_pads[1].name, inlink1->time_base.num, inlink1->time_base.den);
1618 return AVERROR(EINVAL);
1621 if (!inlink0->frame_rate.num || !inlink0->frame_rate.den) {
1622 av_log(ctx, AV_LOG_ERROR, "The inputs needs to be a constant frame rate; "
1623 "current rate of %d/%d is invalid\n", inlink0->frame_rate.num, inlink0->frame_rate.den);
1624 return AVERROR(EINVAL);
1627 if (inlink0->frame_rate.num != inlink1->frame_rate.num ||
1628 inlink0->frame_rate.den != inlink1->frame_rate.den) {
1629 av_log(ctx, AV_LOG_ERROR, "First input link %s frame rate "
1630 "(%d/%d) do not match the corresponding "
1631 "second input link %s frame rate (%d/%d)\n",
1632 ctx->input_pads[0].name, inlink0->frame_rate.num, inlink0->frame_rate.den,
1633 ctx->input_pads[1].name, inlink1->frame_rate.num, inlink1->frame_rate.den);
1634 return AVERROR(EINVAL);
1637 outlink->w = inlink0->w;
1638 outlink->h = inlink0->h;
1639 outlink->time_base = inlink0->time_base;
1640 outlink->sample_aspect_ratio = inlink0->sample_aspect_ratio;
1641 outlink->frame_rate = inlink0->frame_rate;
1643 s->depth = pix_desc->comp[0].depth;
1644 s->is_rgb = !!(pix_desc->flags & AV_PIX_FMT_FLAG_RGB);
1645 s->nb_planes = av_pix_fmt_count_planes(inlink0->format);
1646 s->max_value = (1 << s->depth) - 1;
1648 s->black[1] = s->black[2] = s->is_rgb ? 0 : s->max_value / 2;
1649 s->black[3] = s->max_value;
1650 s->white[0] = s->white[3] = s->max_value;
1651 s->white[1] = s->white[2] = s->is_rgb ? s->max_value : s->max_value / 2;
1653 s->first_pts = s->last_pts = s->pts = AV_NOPTS_VALUE;
1656 s->duration_pts = av_rescale_q(s->duration, AV_TIME_BASE_Q, outlink->time_base);
1658 s->offset_pts = av_rescale_q(s->offset, AV_TIME_BASE_Q, outlink->time_base);
1660 switch (s->transition) {
1661 case CUSTOM: s->transitionf = s->depth <= 8 ? custom8_transition : custom16_transition; break;
1662 case FADE: s->transitionf = s->depth <= 8 ? fade8_transition : fade16_transition; break;
1663 case WIPELEFT: s->transitionf = s->depth <= 8 ? wipeleft8_transition : wipeleft16_transition; break;
1664 case WIPERIGHT: s->transitionf = s->depth <= 8 ? wiperight8_transition : wiperight16_transition; break;
1665 case WIPEUP: s->transitionf = s->depth <= 8 ? wipeup8_transition : wipeup16_transition; break;
1666 case WIPEDOWN: s->transitionf = s->depth <= 8 ? wipedown8_transition : wipedown16_transition; break;
1667 case SLIDELEFT: s->transitionf = s->depth <= 8 ? slideleft8_transition : slideleft16_transition; break;
1668 case SLIDERIGHT: s->transitionf = s->depth <= 8 ? slideright8_transition : slideright16_transition; break;
1669 case SLIDEUP: s->transitionf = s->depth <= 8 ? slideup8_transition : slideup16_transition; break;
1670 case SLIDEDOWN: s->transitionf = s->depth <= 8 ? slidedown8_transition : slidedown16_transition; break;
1671 case CIRCLECROP: s->transitionf = s->depth <= 8 ? circlecrop8_transition : circlecrop16_transition; break;
1672 case RECTCROP: s->transitionf = s->depth <= 8 ? rectcrop8_transition : rectcrop16_transition; break;
1673 case DISTANCE: s->transitionf = s->depth <= 8 ? distance8_transition : distance16_transition; break;
1674 case FADEBLACK: s->transitionf = s->depth <= 8 ? fadeblack8_transition : fadeblack16_transition; break;
1675 case FADEWHITE: s->transitionf = s->depth <= 8 ? fadewhite8_transition : fadewhite16_transition; break;
1676 case RADIAL: s->transitionf = s->depth <= 8 ? radial8_transition : radial16_transition; break;
1677 case SMOOTHLEFT: s->transitionf = s->depth <= 8 ? smoothleft8_transition : smoothleft16_transition; break;
1678 case SMOOTHRIGHT:s->transitionf = s->depth <= 8 ? smoothright8_transition: smoothright16_transition;break;
1679 case SMOOTHUP: s->transitionf = s->depth <= 8 ? smoothup8_transition : smoothup16_transition; break;
1680 case SMOOTHDOWN: s->transitionf = s->depth <= 8 ? smoothdown8_transition : smoothdown16_transition; break;
1681 case CIRCLEOPEN: s->transitionf = s->depth <= 8 ? circleopen8_transition : circleopen16_transition; break;
1682 case CIRCLECLOSE:s->transitionf = s->depth <= 8 ? circleclose8_transition: circleclose16_transition;break;
1683 case VERTOPEN: s->transitionf = s->depth <= 8 ? vertopen8_transition : vertopen16_transition; break;
1684 case VERTCLOSE: s->transitionf = s->depth <= 8 ? vertclose8_transition : vertclose16_transition; break;
1685 case HORZOPEN: s->transitionf = s->depth <= 8 ? horzopen8_transition : horzopen16_transition; break;
1686 case HORZCLOSE: s->transitionf = s->depth <= 8 ? horzclose8_transition : horzclose16_transition; break;
1687 case DISSOLVE: s->transitionf = s->depth <= 8 ? dissolve8_transition : dissolve16_transition; break;
1688 case PIXELIZE: s->transitionf = s->depth <= 8 ? pixelize8_transition : pixelize16_transition; break;
1689 case DIAGTL: s->transitionf = s->depth <= 8 ? diagtl8_transition : diagtl16_transition; break;
1690 case DIAGTR: s->transitionf = s->depth <= 8 ? diagtr8_transition : diagtr16_transition; break;
1691 case DIAGBL: s->transitionf = s->depth <= 8 ? diagbl8_transition : diagbl16_transition; break;
1692 case DIAGBR: s->transitionf = s->depth <= 8 ? diagbr8_transition : diagbr16_transition; break;
1693 case HLSLICE: s->transitionf = s->depth <= 8 ? hlslice8_transition : hlslice16_transition; break;
1694 case HRSLICE: s->transitionf = s->depth <= 8 ? hrslice8_transition : hrslice16_transition; break;
1695 case VUSLICE: s->transitionf = s->depth <= 8 ? vuslice8_transition : vuslice16_transition; break;
1696 case VDSLICE: s->transitionf = s->depth <= 8 ? vdslice8_transition : vdslice16_transition; break;
1697 case HBLUR: s->transitionf = s->depth <= 8 ? hblur8_transition : hblur16_transition; break;
1698 case FADEGRAYS: s->transitionf = s->depth <= 8 ? fadegrays8_transition : fadegrays16_transition; break;
1699 case WIPETL: s->transitionf = s->depth <= 8 ? wipetl8_transition : wipetl16_transition; break;
1700 case WIPETR: s->transitionf = s->depth <= 8 ? wipetr8_transition : wipetr16_transition; break;
1701 case WIPEBL: s->transitionf = s->depth <= 8 ? wipebl8_transition : wipebl16_transition; break;
1702 case WIPEBR: s->transitionf = s->depth <= 8 ? wipebr8_transition : wipebr16_transition; break;
1705 if (s->transition == CUSTOM) {
1706 static const char *const func2_names[] = {
1707 "a0", "a1", "a2", "a3",
1708 "b0", "b1", "b2", "b3",
1711 double (*func2[])(void *, double, double) = {
1718 return AVERROR(EINVAL);
1719 ret = av_expr_parse(&s->e, s->custom_str, var_names,
1720 NULL, NULL, func2_names, func2, 0, ctx);
1728 static int xfade_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
1730 XFadeContext *s = ctx->priv;
1731 AVFilterLink *outlink = ctx->outputs[0];
1732 ThreadData *td = arg;
1733 int slice_start = (outlink->h * jobnr ) / nb_jobs;
1734 int slice_end = (outlink->h * (jobnr+1)) / nb_jobs;
1736 s->transitionf(ctx, td->xf[0], td->xf[1], td->out, td->progress, slice_start, slice_end, jobnr);
1741 static int xfade_frame(AVFilterContext *ctx, AVFrame *a, AVFrame *b)
1743 XFadeContext *s = ctx->priv;
1744 AVFilterLink *outlink = ctx->outputs[0];
1745 float progress = av_clipf(1.f - ((float)(s->pts - s->first_pts - s->offset_pts) / s->duration_pts), 0.f, 1.f);
1749 out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
1751 return AVERROR(ENOMEM);
1753 td.xf[0] = a, td.xf[1] = b, td.out = out, td.progress = progress;
1754 ctx->internal->execute(ctx, xfade_slice, &td, NULL, FFMIN(outlink->h, ff_filter_get_nb_threads(ctx)));
1758 return ff_filter_frame(outlink, out);
1761 static int xfade_activate(AVFilterContext *ctx)
1763 XFadeContext *s = ctx->priv;
1764 AVFilterLink *outlink = ctx->outputs[0];
1766 int ret = 0, status;
1769 FF_FILTER_FORWARD_STATUS_BACK_ALL(outlink, ctx);
1771 if (s->xfade_is_over) {
1772 ret = ff_inlink_consume_frame(ctx->inputs[1], &in);
1775 } else if (ret > 0) {
1776 in->pts = (in->pts - s->last_pts) + s->pts;
1777 return ff_filter_frame(outlink, in);
1778 } else if (ff_inlink_acknowledge_status(ctx->inputs[1], &status, &pts)) {
1779 ff_outlink_set_status(outlink, status, s->pts);
1782 if (ff_outlink_frame_wanted(outlink))
1783 ff_inlink_request_frame(ctx->inputs[1]);
1788 if (ff_inlink_queued_frames(ctx->inputs[0]) > 0) {
1789 s->xf[0] = ff_inlink_peek_frame(ctx->inputs[0], 0);
1791 if (s->first_pts == AV_NOPTS_VALUE) {
1792 s->first_pts = s->xf[0]->pts;
1794 s->pts = s->xf[0]->pts;
1795 if (s->first_pts + s->offset_pts > s->xf[0]->pts) {
1798 ff_inlink_consume_frame(ctx->inputs[0], &in);
1799 return ff_filter_frame(outlink, in);
1806 if (s->xf[0] && ff_inlink_queued_frames(ctx->inputs[1]) > 0) {
1807 ff_inlink_consume_frame(ctx->inputs[0], &s->xf[0]);
1808 ff_inlink_consume_frame(ctx->inputs[1], &s->xf[1]);
1810 s->last_pts = s->xf[1]->pts;
1811 s->pts = s->xf[0]->pts;
1812 if (s->xf[0]->pts - (s->first_pts + s->offset_pts) > s->duration_pts)
1813 s->xfade_is_over = 1;
1814 ret = xfade_frame(ctx, s->xf[0], s->xf[1]);
1815 av_frame_free(&s->xf[0]);
1816 av_frame_free(&s->xf[1]);
1820 if (ff_inlink_queued_frames(ctx->inputs[0]) > 0 &&
1821 ff_inlink_queued_frames(ctx->inputs[1]) > 0) {
1822 ff_filter_set_ready(ctx, 100);
1826 if (ff_outlink_frame_wanted(outlink)) {
1827 if (!s->eof[0] && ff_outlink_get_status(ctx->inputs[0])) {
1829 s->xfade_is_over = 1;
1831 if (!s->eof[1] && ff_outlink_get_status(ctx->inputs[1])) {
1834 if (!s->eof[0] && !s->xf[0] && ff_inlink_queued_frames(ctx->inputs[0]) == 0)
1835 ff_inlink_request_frame(ctx->inputs[0]);
1836 if (!s->eof[1] && (s->need_second || s->eof[0]) && ff_inlink_queued_frames(ctx->inputs[1]) == 0)
1837 ff_inlink_request_frame(ctx->inputs[1]);
1838 if (s->eof[0] && s->eof[1] && (
1839 ff_inlink_queued_frames(ctx->inputs[0]) <= 0 &&
1840 ff_inlink_queued_frames(ctx->inputs[1]) <= 0)) {
1841 ff_outlink_set_status(outlink, AVERROR_EOF, AV_NOPTS_VALUE);
1842 } else if (s->xfade_is_over) {
1843 ff_filter_set_ready(ctx, 100);
1848 return FFERROR_NOT_READY;
1851 static const AVFilterPad xfade_inputs[] = {
1854 .type = AVMEDIA_TYPE_VIDEO,
1858 .type = AVMEDIA_TYPE_VIDEO,
1863 static const AVFilterPad xfade_outputs[] = {
1866 .type = AVMEDIA_TYPE_VIDEO,
1867 .config_props = config_output,
1872 AVFilter ff_vf_xfade = {
1874 .description = NULL_IF_CONFIG_SMALL("Cross fade one video with another video."),
1875 .priv_size = sizeof(XFadeContext),
1876 .priv_class = &xfade_class,
1877 .query_formats = query_formats,
1878 .activate = xfade_activate,
1880 .inputs = xfade_inputs,
1881 .outputs = xfade_outputs,
1882 .flags = AVFILTER_FLAG_SLICE_THREADS,