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 {
72 typedef struct XFadeContext {
96 void (*transitionf)(AVFilterContext *ctx, const AVFrame *a, const AVFrame *b, AVFrame *out, float progress,
97 int slice_start, int slice_end, int jobnr);
102 static const char *const var_names[] = { "X", "Y", "W", "H", "A", "B", "PLANE", "P", NULL };
103 enum { VAR_X, VAR_Y, VAR_W, VAR_H, VAR_A, VAR_B, VAR_PLANE, VAR_PROGRESS, VAR_VARS_NB };
105 typedef struct ThreadData {
106 const AVFrame *xf[2];
111 static int query_formats(AVFilterContext *ctx)
113 static const enum AVPixelFormat pix_fmts[] = {
117 AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRAP, AV_PIX_FMT_GRAY8,
118 AV_PIX_FMT_YUVA444P9, AV_PIX_FMT_GBRP9,
119 AV_PIX_FMT_YUV444P10,
120 AV_PIX_FMT_YUVA444P10,
121 AV_PIX_FMT_GBRP10, AV_PIX_FMT_GBRAP10, AV_PIX_FMT_GRAY10,
122 AV_PIX_FMT_YUV444P12,
123 AV_PIX_FMT_YUVA444P12,
124 AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRAP12, AV_PIX_FMT_GRAY12,
125 AV_PIX_FMT_YUV444P14, AV_PIX_FMT_GBRP14,
126 AV_PIX_FMT_YUV444P16,
127 AV_PIX_FMT_YUVA444P16,
128 AV_PIX_FMT_GBRP16, AV_PIX_FMT_GBRAP16, AV_PIX_FMT_GRAY16,
132 AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
134 return AVERROR(ENOMEM);
135 return ff_set_common_formats(ctx, fmts_list);
138 static av_cold void uninit(AVFilterContext *ctx)
140 XFadeContext *s = ctx->priv;
145 #define OFFSET(x) offsetof(XFadeContext, x)
146 #define FLAGS (AV_OPT_FLAG_FILTERING_PARAM | AV_OPT_FLAG_VIDEO_PARAM)
148 static const AVOption xfade_options[] = {
149 { "transition", "set cross fade transition", OFFSET(transition), AV_OPT_TYPE_INT, {.i64=FADE}, -1, NB_TRANSITIONS-1, FLAGS, "transition" },
150 { "custom", "custom transition", 0, AV_OPT_TYPE_CONST, {.i64=CUSTOM}, 0, 0, FLAGS, "transition" },
151 { "fade", "fade transition", 0, AV_OPT_TYPE_CONST, {.i64=FADE}, 0, 0, FLAGS, "transition" },
152 { "wipeleft", "wipe left transition", 0, AV_OPT_TYPE_CONST, {.i64=WIPELEFT}, 0, 0, FLAGS, "transition" },
153 { "wiperight", "wipe right transition", 0, AV_OPT_TYPE_CONST, {.i64=WIPERIGHT}, 0, 0, FLAGS, "transition" },
154 { "wipeup", "wipe up transition", 0, AV_OPT_TYPE_CONST, {.i64=WIPEUP}, 0, 0, FLAGS, "transition" },
155 { "wipedown", "wipe down transition", 0, AV_OPT_TYPE_CONST, {.i64=WIPEDOWN}, 0, 0, FLAGS, "transition" },
156 { "slideleft", "slide left transition", 0, AV_OPT_TYPE_CONST, {.i64=SLIDELEFT}, 0, 0, FLAGS, "transition" },
157 { "slideright", "slide right transition", 0, AV_OPT_TYPE_CONST, {.i64=SLIDERIGHT}, 0, 0, FLAGS, "transition" },
158 { "slideup", "slide up transition", 0, AV_OPT_TYPE_CONST, {.i64=SLIDEUP}, 0, 0, FLAGS, "transition" },
159 { "slidedown", "slide down transition", 0, AV_OPT_TYPE_CONST, {.i64=SLIDEDOWN}, 0, 0, FLAGS, "transition" },
160 { "circlecrop", "circle crop transition", 0, AV_OPT_TYPE_CONST, {.i64=CIRCLECROP}, 0, 0, FLAGS, "transition" },
161 { "rectcrop", "rect crop transition", 0, AV_OPT_TYPE_CONST, {.i64=RECTCROP}, 0, 0, FLAGS, "transition" },
162 { "distance", "distance transition", 0, AV_OPT_TYPE_CONST, {.i64=DISTANCE}, 0, 0, FLAGS, "transition" },
163 { "fadeblack", "fadeblack transition", 0, AV_OPT_TYPE_CONST, {.i64=FADEBLACK}, 0, 0, FLAGS, "transition" },
164 { "fadewhite", "fadewhite transition", 0, AV_OPT_TYPE_CONST, {.i64=FADEWHITE}, 0, 0, FLAGS, "transition" },
165 { "radial", "radial transition", 0, AV_OPT_TYPE_CONST, {.i64=RADIAL}, 0, 0, FLAGS, "transition" },
166 { "smoothleft", "smoothleft transition", 0, AV_OPT_TYPE_CONST, {.i64=SMOOTHLEFT}, 0, 0, FLAGS, "transition" },
167 { "smoothright","smoothright transition", 0, AV_OPT_TYPE_CONST, {.i64=SMOOTHRIGHT},0, 0, FLAGS, "transition" },
168 { "smoothup", "smoothup transition", 0, AV_OPT_TYPE_CONST, {.i64=SMOOTHUP}, 0, 0, FLAGS, "transition" },
169 { "smoothdown", "smoothdown transition", 0, AV_OPT_TYPE_CONST, {.i64=SMOOTHDOWN}, 0, 0, FLAGS, "transition" },
170 { "circleopen", "circleopen transition", 0, AV_OPT_TYPE_CONST, {.i64=CIRCLEOPEN}, 0, 0, FLAGS, "transition" },
171 { "circleclose","circleclose transition", 0, AV_OPT_TYPE_CONST, {.i64=CIRCLECLOSE},0, 0, FLAGS, "transition" },
172 { "vertopen", "vert open transition", 0, AV_OPT_TYPE_CONST, {.i64=VERTOPEN}, 0, 0, FLAGS, "transition" },
173 { "vertclose", "vert close transition", 0, AV_OPT_TYPE_CONST, {.i64=VERTCLOSE}, 0, 0, FLAGS, "transition" },
174 { "horzopen", "horz open transition", 0, AV_OPT_TYPE_CONST, {.i64=HORZOPEN}, 0, 0, FLAGS, "transition" },
175 { "horzclose", "horz close transition", 0, AV_OPT_TYPE_CONST, {.i64=HORZCLOSE}, 0, 0, FLAGS, "transition" },
176 { "dissolve", "dissolve transition", 0, AV_OPT_TYPE_CONST, {.i64=DISSOLVE}, 0, 0, FLAGS, "transition" },
177 { "pixelize", "pixelize transition", 0, AV_OPT_TYPE_CONST, {.i64=PIXELIZE}, 0, 0, FLAGS, "transition" },
178 { "diagtl", "diag tl transition", 0, AV_OPT_TYPE_CONST, {.i64=DIAGTL}, 0, 0, FLAGS, "transition" },
179 { "diagtr", "diag tr transition", 0, AV_OPT_TYPE_CONST, {.i64=DIAGTR}, 0, 0, FLAGS, "transition" },
180 { "diagbl", "diag bl transition", 0, AV_OPT_TYPE_CONST, {.i64=DIAGBL}, 0, 0, FLAGS, "transition" },
181 { "diagbr", "diag br transition", 0, AV_OPT_TYPE_CONST, {.i64=DIAGBR}, 0, 0, FLAGS, "transition" },
182 { "hlslice", "hl slice transition", 0, AV_OPT_TYPE_CONST, {.i64=HLSLICE}, 0, 0, FLAGS, "transition" },
183 { "hrslice", "hr slice transition", 0, AV_OPT_TYPE_CONST, {.i64=HRSLICE}, 0, 0, FLAGS, "transition" },
184 { "vuslice", "vu slice transition", 0, AV_OPT_TYPE_CONST, {.i64=VUSLICE}, 0, 0, FLAGS, "transition" },
185 { "vdslice", "vd slice transition", 0, AV_OPT_TYPE_CONST, {.i64=VDSLICE}, 0, 0, FLAGS, "transition" },
186 { "hblur", "hblur transition", 0, AV_OPT_TYPE_CONST, {.i64=HBLUR}, 0, 0, FLAGS, "transition" },
187 { "duration", "set cross fade duration", OFFSET(duration), AV_OPT_TYPE_DURATION, {.i64=1000000}, 0, 60000000, FLAGS },
188 { "offset", "set cross fade start relative to first input stream", OFFSET(offset), AV_OPT_TYPE_DURATION, {.i64=0}, INT64_MIN, INT64_MAX, FLAGS },
189 { "expr", "set expression for custom transition", OFFSET(custom_str), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, FLAGS },
193 AVFILTER_DEFINE_CLASS(xfade);
195 #define CUSTOM_TRANSITION(name, type, div) \
196 static void custom##name##_transition(AVFilterContext *ctx, \
197 const AVFrame *a, const AVFrame *b, AVFrame *out, \
199 int slice_start, int slice_end, int jobnr) \
201 XFadeContext *s = ctx->priv; \
202 const int height = slice_end - slice_start; \
204 double values[VAR_VARS_NB]; \
205 values[VAR_W] = out->width; \
206 values[VAR_H] = out->height; \
207 values[VAR_PROGRESS] = progress; \
209 for (int p = 0; p < s->nb_planes; p++) { \
210 const type *xf0 = (const type *)(a->data[p] + slice_start * a->linesize[p]); \
211 const type *xf1 = (const type *)(b->data[p] + slice_start * b->linesize[p]); \
212 type *dst = (type *)(out->data[p] + slice_start * out->linesize[p]); \
214 values[VAR_PLANE] = p; \
216 for (int y = 0; y < height; y++) { \
217 values[VAR_Y] = slice_start + y; \
218 for (int x = 0; x < out->width; x++) { \
220 values[VAR_A] = xf0[x]; \
221 values[VAR_B] = xf1[x]; \
222 dst[x] = av_expr_eval(s->e, values, s); \
225 dst += out->linesize[p] / div; \
226 xf0 += a->linesize[p] / div; \
227 xf1 += b->linesize[p] / div; \
232 CUSTOM_TRANSITION(8, uint8_t, 1)
233 CUSTOM_TRANSITION(16, uint16_t, 2)
235 static inline float mix(float a, float b, float mix)
237 return a * mix + b * (1.f - mix);
240 static inline float fract(float a)
242 return a - floorf(a);
245 static inline float smoothstep(float edge0, float edge1, float x)
249 t = av_clipf((x - edge0) / (edge1 - edge0), 0.f, 1.f);
251 return t * t * (3.f - 2.f * t);
254 #define FADE_TRANSITION(name, type, div) \
255 static void fade##name##_transition(AVFilterContext *ctx, \
256 const AVFrame *a, const AVFrame *b, AVFrame *out, \
258 int slice_start, int slice_end, int jobnr) \
260 XFadeContext *s = ctx->priv; \
261 const int height = slice_end - slice_start; \
263 for (int p = 0; p < s->nb_planes; p++) { \
264 const type *xf0 = (const type *)(a->data[p] + slice_start * a->linesize[p]); \
265 const type *xf1 = (const type *)(b->data[p] + slice_start * b->linesize[p]); \
266 type *dst = (type *)(out->data[p] + slice_start * out->linesize[p]); \
268 for (int y = 0; y < height; y++) { \
269 for (int x = 0; x < out->width; x++) { \
270 dst[x] = mix(xf0[x], xf1[x], progress); \
273 dst += out->linesize[p] / div; \
274 xf0 += a->linesize[p] / div; \
275 xf1 += b->linesize[p] / div; \
280 FADE_TRANSITION(8, uint8_t, 1)
281 FADE_TRANSITION(16, uint16_t, 2)
283 #define WIPELEFT_TRANSITION(name, type, div) \
284 static void wipeleft##name##_transition(AVFilterContext *ctx, \
285 const AVFrame *a, const AVFrame *b, AVFrame *out, \
287 int slice_start, int slice_end, int jobnr) \
289 XFadeContext *s = ctx->priv; \
290 const int height = slice_end - slice_start; \
291 const int z = out->width * progress; \
293 for (int p = 0; p < s->nb_planes; p++) { \
294 const type *xf0 = (const type *)(a->data[p] + slice_start * a->linesize[p]); \
295 const type *xf1 = (const type *)(b->data[p] + slice_start * b->linesize[p]); \
296 type *dst = (type *)(out->data[p] + slice_start * out->linesize[p]); \
298 for (int y = 0; y < height; y++) { \
299 for (int x = 0; x < out->width; x++) { \
300 dst[x] = x > z ? xf1[x] : xf0[x]; \
303 dst += out->linesize[p] / div; \
304 xf0 += a->linesize[p] / div; \
305 xf1 += b->linesize[p] / div; \
310 WIPELEFT_TRANSITION(8, uint8_t, 1)
311 WIPELEFT_TRANSITION(16, uint16_t, 2)
313 #define WIPERIGHT_TRANSITION(name, type, div) \
314 static void wiperight##name##_transition(AVFilterContext *ctx, \
315 const AVFrame *a, const AVFrame *b, AVFrame *out, \
317 int slice_start, int slice_end, int jobnr) \
319 XFadeContext *s = ctx->priv; \
320 const int height = slice_end - slice_start; \
321 const int z = out->width * (1.f - progress); \
323 for (int p = 0; p < s->nb_planes; p++) { \
324 const type *xf0 = (const type *)(a->data[p] + slice_start * a->linesize[p]); \
325 const type *xf1 = (const type *)(b->data[p] + slice_start * b->linesize[p]); \
326 type *dst = (type *)(out->data[p] + slice_start * out->linesize[p]); \
328 for (int y = 0; y < height; y++) { \
329 for (int x = 0; x < out->width; x++) { \
330 dst[x] = x > z ? xf0[x] : xf1[x]; \
333 dst += out->linesize[p] / div; \
334 xf0 += a->linesize[p] / div; \
335 xf1 += b->linesize[p] / div; \
340 WIPERIGHT_TRANSITION(8, uint8_t, 1)
341 WIPERIGHT_TRANSITION(16, uint16_t, 2)
343 #define WIPEUP_TRANSITION(name, type, div) \
344 static void wipeup##name##_transition(AVFilterContext *ctx, \
345 const AVFrame *a, const AVFrame *b, AVFrame *out, \
347 int slice_start, int slice_end, int jobnr) \
349 XFadeContext *s = ctx->priv; \
350 const int height = slice_end - slice_start; \
351 const int z = out->height * progress; \
353 for (int p = 0; p < s->nb_planes; p++) { \
354 const type *xf0 = (const type *)(a->data[p] + slice_start * a->linesize[p]); \
355 const type *xf1 = (const type *)(b->data[p] + slice_start * b->linesize[p]); \
356 type *dst = (type *)(out->data[p] + slice_start * out->linesize[p]); \
358 for (int y = 0; y < height; y++) { \
359 for (int x = 0; x < out->width; x++) { \
360 dst[x] = slice_start + y > z ? xf1[x] : xf0[x]; \
363 dst += out->linesize[p] / div; \
364 xf0 += a->linesize[p] / div; \
365 xf1 += b->linesize[p] / div; \
370 WIPEUP_TRANSITION(8, uint8_t, 1)
371 WIPEUP_TRANSITION(16, uint16_t, 2)
373 #define WIPEDOWN_TRANSITION(name, type, div) \
374 static void wipedown##name##_transition(AVFilterContext *ctx, \
375 const AVFrame *a, const AVFrame *b, AVFrame *out, \
377 int slice_start, int slice_end, int jobnr) \
379 XFadeContext *s = ctx->priv; \
380 const int height = slice_end - slice_start; \
381 const int z = out->height * (1.f - progress); \
383 for (int p = 0; p < s->nb_planes; p++) { \
384 const type *xf0 = (const type *)(a->data[p] + slice_start * a->linesize[p]); \
385 const type *xf1 = (const type *)(b->data[p] + slice_start * b->linesize[p]); \
386 type *dst = (type *)(out->data[p] + slice_start * out->linesize[p]); \
388 for (int y = 0; y < height; y++) { \
389 for (int x = 0; x < out->width; x++) { \
390 dst[x] = slice_start + y > z ? xf0[x] : xf1[x]; \
393 dst += out->linesize[p] / div; \
394 xf0 += a->linesize[p] / div; \
395 xf1 += b->linesize[p] / div; \
400 WIPEDOWN_TRANSITION(8, uint8_t, 1)
401 WIPEDOWN_TRANSITION(16, uint16_t, 2)
403 #define SLIDELEFT_TRANSITION(name, type, div) \
404 static void slideleft##name##_transition(AVFilterContext *ctx, \
405 const AVFrame *a, const AVFrame *b, AVFrame *out, \
407 int slice_start, int slice_end, int jobnr) \
409 XFadeContext *s = ctx->priv; \
410 const int height = slice_end - slice_start; \
411 const int width = out->width; \
412 const int z = -progress * width; \
414 for (int p = 0; p < s->nb_planes; p++) { \
415 const type *xf0 = (const type *)(a->data[p] + slice_start * a->linesize[p]); \
416 const type *xf1 = (const type *)(b->data[p] + slice_start * b->linesize[p]); \
417 type *dst = (type *)(out->data[p] + slice_start * out->linesize[p]); \
419 for (int y = 0; y < height; y++) { \
420 for (int x = 0; x < width; x++) { \
421 const int zx = z + x; \
422 const int zz = zx % width + width * (zx < 0); \
423 dst[x] = (zx > 0) && (zx < width) ? xf1[zz] : xf0[zz]; \
426 dst += out->linesize[p] / div; \
427 xf0 += a->linesize[p] / div; \
428 xf1 += b->linesize[p] / div; \
433 SLIDELEFT_TRANSITION(8, uint8_t, 1)
434 SLIDELEFT_TRANSITION(16, uint16_t, 2)
436 #define SLIDERIGHT_TRANSITION(name, type, div) \
437 static void slideright##name##_transition(AVFilterContext *ctx, \
438 const AVFrame *a, const AVFrame *b, AVFrame *out, \
440 int slice_start, int slice_end, int jobnr) \
442 XFadeContext *s = ctx->priv; \
443 const int height = slice_end - slice_start; \
444 const int width = out->width; \
445 const int z = progress * width; \
447 for (int p = 0; p < s->nb_planes; p++) { \
448 const type *xf0 = (const type *)(a->data[p] + slice_start * a->linesize[p]); \
449 const type *xf1 = (const type *)(b->data[p] + slice_start * b->linesize[p]); \
450 type *dst = (type *)(out->data[p] + slice_start * out->linesize[p]); \
452 for (int y = 0; y < height; y++) { \
453 for (int x = 0; x < out->width; x++) { \
454 const int zx = z + x; \
455 const int zz = zx % width + width * (zx < 0); \
456 dst[x] = (zx > 0) && (zx < width) ? xf1[zz] : xf0[zz]; \
459 dst += out->linesize[p] / div; \
460 xf0 += a->linesize[p] / div; \
461 xf1 += b->linesize[p] / div; \
466 SLIDERIGHT_TRANSITION(8, uint8_t, 1)
467 SLIDERIGHT_TRANSITION(16, uint16_t, 2)
469 #define SLIDEUP_TRANSITION(name, type, div) \
470 static void slideup##name##_transition(AVFilterContext *ctx, \
471 const AVFrame *a, const AVFrame *b, AVFrame *out, \
473 int slice_start, int slice_end, int jobnr) \
475 XFadeContext *s = ctx->priv; \
476 const int height = out->height; \
477 const int z = -progress * height; \
479 for (int p = 0; p < s->nb_planes; p++) { \
480 type *dst = (type *)(out->data[p] + slice_start * out->linesize[p]); \
482 for (int y = slice_start; y < slice_end; y++) { \
483 const int zy = z + y; \
484 const int zz = zy % height + height * (zy < 0); \
485 const type *xf0 = (const type *)(a->data[p] + zz * a->linesize[p]); \
486 const type *xf1 = (const type *)(b->data[p] + zz * b->linesize[p]); \
488 for (int x = 0; x < out->width; x++) { \
489 dst[x] = (zy > 0) && (zy < height) ? xf1[x] : xf0[x]; \
492 dst += out->linesize[p] / div; \
497 SLIDEUP_TRANSITION(8, uint8_t, 1)
498 SLIDEUP_TRANSITION(16, uint16_t, 2)
500 #define SLIDEDOWN_TRANSITION(name, type, div) \
501 static void slidedown##name##_transition(AVFilterContext *ctx, \
502 const AVFrame *a, const AVFrame *b, AVFrame *out, \
504 int slice_start, int slice_end, int jobnr) \
506 XFadeContext *s = ctx->priv; \
507 const int height = out->height; \
508 const int z = progress * height; \
510 for (int p = 0; p < s->nb_planes; p++) { \
511 type *dst = (type *)(out->data[p] + slice_start * out->linesize[p]); \
513 for (int y = slice_start; y < slice_end; y++) { \
514 const int zy = z + y; \
515 const int zz = zy % height + height * (zy < 0); \
516 const type *xf0 = (const type *)(a->data[p] + zz * a->linesize[p]); \
517 const type *xf1 = (const type *)(b->data[p] + zz * b->linesize[p]); \
519 for (int x = 0; x < out->width; x++) { \
520 dst[x] = (zy > 0) && (zy < height) ? xf1[x] : xf0[x]; \
523 dst += out->linesize[p] / div; \
528 SLIDEDOWN_TRANSITION(8, uint8_t, 1)
529 SLIDEDOWN_TRANSITION(16, uint16_t, 2)
531 #define CIRCLECROP_TRANSITION(name, type, div) \
532 static void circlecrop##name##_transition(AVFilterContext *ctx, \
533 const AVFrame *a, const AVFrame *b, AVFrame *out, \
535 int slice_start, int slice_end, int jobnr) \
537 XFadeContext *s = ctx->priv; \
538 const int width = out->width; \
539 const int height = out->height; \
540 float z = powf(2.f * fabsf(progress - 0.5f), 3.f) * hypotf(width/2, height/2); \
542 for (int p = 0; p < s->nb_planes; p++) { \
543 const int bg = s->black[p]; \
544 type *dst = (type *)(out->data[p] + slice_start * out->linesize[p]); \
546 for (int y = slice_start; y < slice_end; y++) { \
547 const type *xf0 = (const type *)(a->data[p] + y * a->linesize[p]); \
548 const type *xf1 = (const type *)(b->data[p] + y * b->linesize[p]); \
550 for (int x = 0; x < width; x++) { \
551 float dist = hypotf(x - width / 2, y - height / 2); \
552 int val = progress < 0.5f ? xf1[x] : xf0[x]; \
553 dst[x] = (z < dist) ? bg : val; \
556 dst += out->linesize[p] / div; \
561 CIRCLECROP_TRANSITION(8, uint8_t, 1)
562 CIRCLECROP_TRANSITION(16, uint16_t, 2)
564 #define RECTCROP_TRANSITION(name, type, div) \
565 static void rectcrop##name##_transition(AVFilterContext *ctx, \
566 const AVFrame *a, const AVFrame *b, AVFrame *out, \
568 int slice_start, int slice_end, int jobnr) \
570 XFadeContext *s = ctx->priv; \
571 const int width = out->width; \
572 const int height = out->height; \
573 int zh = fabsf(progress - 0.5f) * height; \
574 int zw = fabsf(progress - 0.5f) * width; \
576 for (int p = 0; p < s->nb_planes; p++) { \
577 const int bg = s->black[p]; \
578 type *dst = (type *)(out->data[p] + slice_start * out->linesize[p]); \
580 for (int y = slice_start; y < slice_end; y++) { \
581 const type *xf0 = (const type *)(a->data[p] + y * a->linesize[p]); \
582 const type *xf1 = (const type *)(b->data[p] + y * b->linesize[p]); \
584 for (int x = 0; x < width; x++) { \
585 int dist = FFABS(x - width / 2) < zw && \
586 FFABS(y - height / 2) < zh; \
587 int val = progress < 0.5f ? xf1[x] : xf0[x]; \
588 dst[x] = !dist ? bg : val; \
591 dst += out->linesize[p] / div; \
596 RECTCROP_TRANSITION(8, uint8_t, 1)
597 RECTCROP_TRANSITION(16, uint16_t, 2)
599 #define DISTANCE_TRANSITION(name, type, div) \
600 static void distance##name##_transition(AVFilterContext *ctx, \
601 const AVFrame *a, const AVFrame *b, AVFrame *out, \
603 int slice_start, int slice_end, int jobnr) \
605 XFadeContext *s = ctx->priv; \
606 const int width = out->width; \
607 const float max = s->max_value; \
609 for (int y = slice_start; y < slice_end; y++) { \
610 for (int x = 0; x < width; x++) { \
612 for (int p = 0; p < s->nb_planes; p++) { \
613 const type *xf0 = (const type *)(a->data[p] + y * a->linesize[p]); \
614 const type *xf1 = (const type *)(b->data[p] + y * b->linesize[p]); \
616 dist += (xf0[x] / max - xf1[x] / max) * \
617 (xf0[x] / max - xf1[x] / max); \
620 dist = sqrtf(dist) <= progress; \
621 for (int p = 0; p < s->nb_planes; p++) { \
622 const type *xf0 = (const type *)(a->data[p] + y * a->linesize[p]); \
623 const type *xf1 = (const type *)(b->data[p] + y * b->linesize[p]); \
624 type *dst = (type *)(out->data[p] + y * out->linesize[p]); \
625 dst[x] = mix(mix(xf0[x], xf1[x], dist), xf1[x], progress); \
631 DISTANCE_TRANSITION(8, uint8_t, 1)
632 DISTANCE_TRANSITION(16, uint16_t, 2)
634 #define FADEBLACK_TRANSITION(name, type, div) \
635 static void fadeblack##name##_transition(AVFilterContext *ctx, \
636 const AVFrame *a, const AVFrame *b, AVFrame *out, \
638 int slice_start, int slice_end, int jobnr) \
640 XFadeContext *s = ctx->priv; \
641 const int height = slice_end - slice_start; \
642 const float phase = 0.2f; \
644 for (int p = 0; p < s->nb_planes; p++) { \
645 const type *xf0 = (const type *)(a->data[p] + slice_start * a->linesize[p]); \
646 const type *xf1 = (const type *)(b->data[p] + slice_start * b->linesize[p]); \
647 type *dst = (type *)(out->data[p] + slice_start * out->linesize[p]); \
648 const int bg = s->black[p]; \
650 for (int y = 0; y < height; y++) { \
651 for (int x = 0; x < out->width; x++) { \
652 dst[x] = mix(mix(xf0[x], bg, smoothstep(1.f-phase, 1.f, progress)), \
653 mix(bg, xf1[x], smoothstep(phase, 1.f, progress)), \
657 dst += out->linesize[p] / div; \
658 xf0 += a->linesize[p] / div; \
659 xf1 += b->linesize[p] / div; \
664 FADEBLACK_TRANSITION(8, uint8_t, 1)
665 FADEBLACK_TRANSITION(16, uint16_t, 2)
667 #define FADEWHITE_TRANSITION(name, type, div) \
668 static void fadewhite##name##_transition(AVFilterContext *ctx, \
669 const AVFrame *a, const AVFrame *b, AVFrame *out, \
671 int slice_start, int slice_end, int jobnr) \
673 XFadeContext *s = ctx->priv; \
674 const int height = slice_end - slice_start; \
675 const float phase = 0.2f; \
677 for (int p = 0; p < s->nb_planes; p++) { \
678 const type *xf0 = (const type *)(a->data[p] + slice_start * a->linesize[p]); \
679 const type *xf1 = (const type *)(b->data[p] + slice_start * b->linesize[p]); \
680 type *dst = (type *)(out->data[p] + slice_start * out->linesize[p]); \
681 const int bg = s->white[p]; \
683 for (int y = 0; y < height; y++) { \
684 for (int x = 0; x < out->width; x++) { \
685 dst[x] = mix(mix(xf0[x], bg, smoothstep(1.f-phase, 1.f, progress)), \
686 mix(bg, xf1[x], smoothstep(phase, 1.f, progress)), \
690 dst += out->linesize[p] / div; \
691 xf0 += a->linesize[p] / div; \
692 xf1 += b->linesize[p] / div; \
697 FADEWHITE_TRANSITION(8, uint8_t, 1)
698 FADEWHITE_TRANSITION(16, uint16_t, 2)
700 #define RADIAL_TRANSITION(name, type, div) \
701 static void radial##name##_transition(AVFilterContext *ctx, \
702 const AVFrame *a, const AVFrame *b, AVFrame *out, \
704 int slice_start, int slice_end, int jobnr) \
706 XFadeContext *s = ctx->priv; \
707 const int width = out->width; \
708 const int height = out->height; \
710 for (int y = slice_start; y < slice_end; y++) { \
711 for (int x = 0; x < width; x++) { \
712 const float smooth = atan2f(x - width / 2, y - height / 2) - \
713 (progress - 0.5f) * (M_PI * 2.5f); \
714 for (int p = 0; p < s->nb_planes; p++) { \
715 const type *xf0 = (const type *)(a->data[p] + y * a->linesize[p]); \
716 const type *xf1 = (const type *)(b->data[p] + y * b->linesize[p]); \
717 type *dst = (type *)(out->data[p] + y * out->linesize[p]); \
719 dst[x] = mix(xf1[x], xf0[x], smoothstep(0.f, 1.f, smooth)); \
725 RADIAL_TRANSITION(8, uint8_t, 1)
726 RADIAL_TRANSITION(16, uint16_t, 2)
728 #define SMOOTHLEFT_TRANSITION(name, type, div) \
729 static void smoothleft##name##_transition(AVFilterContext *ctx, \
730 const AVFrame *a, const AVFrame *b, AVFrame *out, \
732 int slice_start, int slice_end, int jobnr) \
734 XFadeContext *s = ctx->priv; \
735 const int width = out->width; \
736 const float w = width; \
738 for (int y = slice_start; y < slice_end; y++) { \
739 for (int x = 0; x < width; x++) { \
740 const float smooth = 1.f + x / w - progress * 2.f; \
742 for (int p = 0; p < s->nb_planes; p++) { \
743 const type *xf0 = (const type *)(a->data[p] + y * a->linesize[p]); \
744 const type *xf1 = (const type *)(b->data[p] + y * b->linesize[p]); \
745 type *dst = (type *)(out->data[p] + y * out->linesize[p]); \
747 dst[x] = mix(xf1[x], xf0[x], smoothstep(0.f, 1.f, smooth)); \
753 SMOOTHLEFT_TRANSITION(8, uint8_t, 1)
754 SMOOTHLEFT_TRANSITION(16, uint16_t, 2)
756 #define SMOOTHRIGHT_TRANSITION(name, type, div) \
757 static void smoothright##name##_transition(AVFilterContext *ctx, \
758 const AVFrame *a, const AVFrame *b, AVFrame *out, \
760 int slice_start, int slice_end, int jobnr) \
762 XFadeContext *s = ctx->priv; \
763 const int width = out->width; \
764 const float w = width; \
766 for (int y = slice_start; y < slice_end; y++) { \
767 for (int x = 0; x < width; x++) { \
768 const float smooth = 1.f + (w - 1 - x) / w - progress * 2.f; \
770 for (int p = 0; p < s->nb_planes; p++) { \
771 const type *xf0 = (const type *)(a->data[p] + y * a->linesize[p]); \
772 const type *xf1 = (const type *)(b->data[p] + y * b->linesize[p]); \
773 type *dst = (type *)(out->data[p] + y * out->linesize[p]); \
775 dst[x] = mix(xf1[x], xf0[x], smoothstep(0.f, 1.f, smooth)); \
781 SMOOTHRIGHT_TRANSITION(8, uint8_t, 1)
782 SMOOTHRIGHT_TRANSITION(16, uint16_t, 2)
784 #define SMOOTHUP_TRANSITION(name, type, div) \
785 static void smoothup##name##_transition(AVFilterContext *ctx, \
786 const AVFrame *a, const AVFrame *b, AVFrame *out, \
788 int slice_start, int slice_end, int jobnr) \
790 XFadeContext *s = ctx->priv; \
791 const int width = out->width; \
792 const float h = out->height; \
794 for (int y = slice_start; y < slice_end; y++) { \
795 const float smooth = 1.f + y / h - progress * 2.f; \
796 for (int x = 0; x < width; x++) { \
797 for (int p = 0; p < s->nb_planes; p++) { \
798 const type *xf0 = (const type *)(a->data[p] + y * a->linesize[p]); \
799 const type *xf1 = (const type *)(b->data[p] + y * b->linesize[p]); \
800 type *dst = (type *)(out->data[p] + y * out->linesize[p]); \
802 dst[x] = mix(xf1[x], xf0[x], smoothstep(0.f, 1.f, smooth)); \
808 SMOOTHUP_TRANSITION(8, uint8_t, 1)
809 SMOOTHUP_TRANSITION(16, uint16_t, 2)
811 #define SMOOTHDOWN_TRANSITION(name, type, div) \
812 static void smoothdown##name##_transition(AVFilterContext *ctx, \
813 const AVFrame *a, const AVFrame *b, AVFrame *out, \
815 int slice_start, int slice_end, int jobnr) \
817 XFadeContext *s = ctx->priv; \
818 const int width = out->width; \
819 const float h = out->height; \
821 for (int y = slice_start; y < slice_end; y++) { \
822 const float smooth = 1.f + (h - 1 - y) / h - progress * 2.f; \
823 for (int x = 0; x < width; x++) { \
824 for (int p = 0; p < s->nb_planes; p++) { \
825 const type *xf0 = (const type *)(a->data[p] + y * a->linesize[p]); \
826 const type *xf1 = (const type *)(b->data[p] + y * b->linesize[p]); \
827 type *dst = (type *)(out->data[p] + y * out->linesize[p]); \
829 dst[x] = mix(xf1[x], xf0[x], smoothstep(0.f, 1.f, smooth)); \
835 SMOOTHDOWN_TRANSITION(8, uint8_t, 1)
836 SMOOTHDOWN_TRANSITION(16, uint16_t, 2)
838 #define CIRCLEOPEN_TRANSITION(name, type, div) \
839 static void circleopen##name##_transition(AVFilterContext *ctx, \
840 const AVFrame *a, const AVFrame *b, AVFrame *out, \
842 int slice_start, int slice_end, int jobnr) \
844 XFadeContext *s = ctx->priv; \
845 const int width = out->width; \
846 const int height = out->height; \
847 const float z = hypotf(width / 2, height / 2); \
848 const float p = (progress - 0.5f) * 3.f; \
850 for (int y = slice_start; y < slice_end; y++) { \
851 for (int x = 0; x < width; x++) { \
852 const float smooth = hypotf(x - width / 2, y - height / 2) / z + p; \
853 for (int p = 0; p < s->nb_planes; p++) { \
854 const type *xf0 = (const type *)(a->data[p] + y * a->linesize[p]); \
855 const type *xf1 = (const type *)(b->data[p] + y * b->linesize[p]); \
856 type *dst = (type *)(out->data[p] + y * out->linesize[p]); \
858 dst[x] = mix(xf0[x], xf1[x], smoothstep(0.f, 1.f, smooth)); \
864 CIRCLEOPEN_TRANSITION(8, uint8_t, 1)
865 CIRCLEOPEN_TRANSITION(16, uint16_t, 2)
867 #define CIRCLECLOSE_TRANSITION(name, type, div) \
868 static void circleclose##name##_transition(AVFilterContext *ctx, \
869 const AVFrame *a, const AVFrame *b, AVFrame *out, \
871 int slice_start, int slice_end, int jobnr) \
873 XFadeContext *s = ctx->priv; \
874 const int width = out->width; \
875 const int height = out->height; \
876 const float z = hypotf(width / 2, height / 2); \
877 const float p = (1.f - progress - 0.5f) * 3.f; \
879 for (int y = slice_start; y < slice_end; y++) { \
880 for (int x = 0; x < width; x++) { \
881 const float smooth = hypotf(x - width / 2, y - height / 2) / z + p; \
882 for (int p = 0; p < s->nb_planes; p++) { \
883 const type *xf0 = (const type *)(a->data[p] + y * a->linesize[p]); \
884 const type *xf1 = (const type *)(b->data[p] + y * b->linesize[p]); \
885 type *dst = (type *)(out->data[p] + y * out->linesize[p]); \
887 dst[x] = mix(xf1[x], xf0[x], smoothstep(0.f, 1.f, smooth)); \
893 CIRCLECLOSE_TRANSITION(8, uint8_t, 1)
894 CIRCLECLOSE_TRANSITION(16, uint16_t, 2)
896 #define VERTOPEN_TRANSITION(name, type, div) \
897 static void vertopen##name##_transition(AVFilterContext *ctx, \
898 const AVFrame *a, const AVFrame *b, AVFrame *out, \
900 int slice_start, int slice_end, int jobnr) \
902 XFadeContext *s = ctx->priv; \
903 const int width = out->width; \
904 const float w2 = out->width / 2; \
906 for (int y = slice_start; y < slice_end; y++) { \
907 for (int x = 0; x < width; x++) { \
908 const float smooth = 2.f - fabsf((x - w2) / w2) - progress * 2.f; \
909 for (int p = 0; p < s->nb_planes; p++) { \
910 const type *xf0 = (const type *)(a->data[p] + y * a->linesize[p]); \
911 const type *xf1 = (const type *)(b->data[p] + y * b->linesize[p]); \
912 type *dst = (type *)(out->data[p] + y * out->linesize[p]); \
914 dst[x] = mix(xf1[x], xf0[x], smoothstep(0.f, 1.f, smooth)); \
920 VERTOPEN_TRANSITION(8, uint8_t, 1)
921 VERTOPEN_TRANSITION(16, uint16_t, 2)
923 #define VERTCLOSE_TRANSITION(name, type, div) \
924 static void vertclose##name##_transition(AVFilterContext *ctx, \
925 const AVFrame *a, const AVFrame *b, AVFrame *out, \
927 int slice_start, int slice_end, int jobnr) \
929 XFadeContext *s = ctx->priv; \
930 const int width = out->width; \
931 const float w2 = out->width / 2; \
933 for (int y = slice_start; y < slice_end; y++) { \
934 for (int x = 0; x < width; x++) { \
935 const float smooth = 1.f + fabsf((x - w2) / w2) - progress * 2.f; \
936 for (int p = 0; p < s->nb_planes; p++) { \
937 const type *xf0 = (const type *)(a->data[p] + y * a->linesize[p]); \
938 const type *xf1 = (const type *)(b->data[p] + y * b->linesize[p]); \
939 type *dst = (type *)(out->data[p] + y * out->linesize[p]); \
941 dst[x] = mix(xf1[x], xf0[x], smoothstep(0.f, 1.f, smooth)); \
947 VERTCLOSE_TRANSITION(8, uint8_t, 1)
948 VERTCLOSE_TRANSITION(16, uint16_t, 2)
950 #define HORZOPEN_TRANSITION(name, type, div) \
951 static void horzopen##name##_transition(AVFilterContext *ctx, \
952 const AVFrame *a, const AVFrame *b, AVFrame *out, \
954 int slice_start, int slice_end, int jobnr) \
956 XFadeContext *s = ctx->priv; \
957 const int width = out->width; \
958 const float h2 = out->height / 2; \
960 for (int y = slice_start; y < slice_end; y++) { \
961 const float smooth = 2.f - fabsf((y - h2) / h2) - progress * 2.f; \
962 for (int x = 0; x < width; x++) { \
963 for (int p = 0; p < s->nb_planes; p++) { \
964 const type *xf0 = (const type *)(a->data[p] + y * a->linesize[p]); \
965 const type *xf1 = (const type *)(b->data[p] + y * b->linesize[p]); \
966 type *dst = (type *)(out->data[p] + y * out->linesize[p]); \
968 dst[x] = mix(xf1[x], xf0[x], smoothstep(0.f, 1.f, smooth)); \
974 HORZOPEN_TRANSITION(8, uint8_t, 1)
975 HORZOPEN_TRANSITION(16, uint16_t, 2)
977 #define HORZCLOSE_TRANSITION(name, type, div) \
978 static void horzclose##name##_transition(AVFilterContext *ctx, \
979 const AVFrame *a, const AVFrame *b, AVFrame *out, \
981 int slice_start, int slice_end, int jobnr) \
983 XFadeContext *s = ctx->priv; \
984 const int width = out->width; \
985 const float h2 = out->height / 2; \
987 for (int y = slice_start; y < slice_end; y++) { \
988 const float smooth = 1.f + fabsf((y - h2) / h2) - progress * 2.f; \
989 for (int x = 0; x < width; x++) { \
990 for (int p = 0; p < s->nb_planes; p++) { \
991 const type *xf0 = (const type *)(a->data[p] + y * a->linesize[p]); \
992 const type *xf1 = (const type *)(b->data[p] + y * b->linesize[p]); \
993 type *dst = (type *)(out->data[p] + y * out->linesize[p]); \
995 dst[x] = mix(xf1[x], xf0[x], smoothstep(0.f, 1.f, smooth)); \
1001 HORZCLOSE_TRANSITION(8, uint8_t, 1)
1002 HORZCLOSE_TRANSITION(16, uint16_t, 2)
1004 static float frand(int x, int y)
1006 const float r = sinf(x * 12.9898f + y * 78.233f) * 43758.545f;
1008 return r - floorf(r);
1011 #define DISSOLVE_TRANSITION(name, type, div) \
1012 static void dissolve##name##_transition(AVFilterContext *ctx, \
1013 const AVFrame *a, const AVFrame *b, AVFrame *out, \
1015 int slice_start, int slice_end, int jobnr) \
1017 XFadeContext *s = ctx->priv; \
1018 const int width = out->width; \
1020 for (int y = slice_start; y < slice_end; y++) { \
1021 for (int x = 0; x < width; x++) { \
1022 const float smooth = frand(x, y) * 2.f + progress * 2.f - 1.5f; \
1023 for (int p = 0; p < s->nb_planes; p++) { \
1024 const type *xf0 = (const type *)(a->data[p] + y * a->linesize[p]); \
1025 const type *xf1 = (const type *)(b->data[p] + y * b->linesize[p]); \
1026 type *dst = (type *)(out->data[p] + y * out->linesize[p]); \
1028 dst[x] = smooth >= 0.5f ? xf0[x] : xf1[x]; \
1034 DISSOLVE_TRANSITION(8, uint8_t, 1)
1035 DISSOLVE_TRANSITION(16, uint16_t, 2)
1037 #define PIXELIZE_TRANSITION(name, type, div) \
1038 static void pixelize##name##_transition(AVFilterContext *ctx, \
1039 const AVFrame *a, const AVFrame *b, AVFrame *out, \
1041 int slice_start, int slice_end, int jobnr) \
1043 XFadeContext *s = ctx->priv; \
1044 const int w = out->width; \
1045 const int h = out->height; \
1046 const float d = fminf(progress, 1.f - progress); \
1047 const float dist = ceilf(d * 50.f) / 50.f; \
1048 const float sqx = 2.f * dist * FFMIN(w, h) / 20.f; \
1049 const float sqy = 2.f * dist * FFMIN(w, h) / 20.f; \
1051 for (int y = slice_start; y < slice_end; y++) { \
1052 for (int x = 0; x < w; x++) { \
1053 int sx = dist > 0.f ? FFMIN((floorf(x / sqx) + .5f) * sqx, w - 1) : x; \
1054 int sy = dist > 0.f ? FFMIN((floorf(y / sqy) + .5f) * sqy, h - 1) : y; \
1055 for (int p = 0; p < s->nb_planes; p++) { \
1056 const type *xf0 = (const type *)(a->data[p] + sy * a->linesize[p]); \
1057 const type *xf1 = (const type *)(b->data[p] + sy * b->linesize[p]); \
1058 type *dst = (type *)(out->data[p] + y * out->linesize[p]); \
1060 dst[x] = mix(xf0[sx], xf1[sx], progress); \
1066 PIXELIZE_TRANSITION(8, uint8_t, 1)
1067 PIXELIZE_TRANSITION(16, uint16_t, 2)
1069 #define DIAGTL_TRANSITION(name, type, div) \
1070 static void diagtl##name##_transition(AVFilterContext *ctx, \
1071 const AVFrame *a, const AVFrame *b, AVFrame *out, \
1073 int slice_start, int slice_end, int jobnr) \
1075 XFadeContext *s = ctx->priv; \
1076 const int width = out->width; \
1077 const float w = width; \
1078 const float h = out->height; \
1080 for (int y = slice_start; y < slice_end; y++) { \
1081 for (int x = 0; x < width; x++) { \
1082 const float smooth = 1.f + x / w * y / h - progress * 2.f; \
1084 for (int p = 0; p < s->nb_planes; p++) { \
1085 const type *xf0 = (const type *)(a->data[p] + y * a->linesize[p]); \
1086 const type *xf1 = (const type *)(b->data[p] + y * b->linesize[p]); \
1087 type *dst = (type *)(out->data[p] + y * out->linesize[p]); \
1089 dst[x] = mix(xf1[x], xf0[x], smoothstep(0.f, 1.f, smooth)); \
1095 DIAGTL_TRANSITION(8, uint8_t, 1)
1096 DIAGTL_TRANSITION(16, uint16_t, 2)
1098 #define DIAGTR_TRANSITION(name, type, div) \
1099 static void diagtr##name##_transition(AVFilterContext *ctx, \
1100 const AVFrame *a, const AVFrame *b, AVFrame *out, \
1102 int slice_start, int slice_end, int jobnr) \
1104 XFadeContext *s = ctx->priv; \
1105 const int width = out->width; \
1106 const float w = width; \
1107 const float h = out->height; \
1109 for (int y = slice_start; y < slice_end; y++) { \
1110 for (int x = 0; x < width; x++) { \
1111 const float smooth = 1.f + (w - 1 - x) / w * y / h - progress * 2.f; \
1113 for (int p = 0; p < s->nb_planes; p++) { \
1114 const type *xf0 = (const type *)(a->data[p] + y * a->linesize[p]); \
1115 const type *xf1 = (const type *)(b->data[p] + y * b->linesize[p]); \
1116 type *dst = (type *)(out->data[p] + y * out->linesize[p]); \
1118 dst[x] = mix(xf1[x], xf0[x], smoothstep(0.f, 1.f, smooth)); \
1124 DIAGTR_TRANSITION(8, uint8_t, 1)
1125 DIAGTR_TRANSITION(16, uint16_t, 2)
1127 #define DIAGBL_TRANSITION(name, type, div) \
1128 static void diagbl##name##_transition(AVFilterContext *ctx, \
1129 const AVFrame *a, const AVFrame *b, AVFrame *out, \
1131 int slice_start, int slice_end, int jobnr) \
1133 XFadeContext *s = ctx->priv; \
1134 const int width = out->width; \
1135 const float w = width; \
1136 const float h = out->height; \
1138 for (int y = slice_start; y < slice_end; y++) { \
1139 for (int x = 0; x < width; x++) { \
1140 const float smooth = 1.f + x / w * (h - 1 - y) / h - progress * 2.f; \
1142 for (int p = 0; p < s->nb_planes; p++) { \
1143 const type *xf0 = (const type *)(a->data[p] + y * a->linesize[p]); \
1144 const type *xf1 = (const type *)(b->data[p] + y * b->linesize[p]); \
1145 type *dst = (type *)(out->data[p] + y * out->linesize[p]); \
1147 dst[x] = mix(xf1[x], xf0[x], smoothstep(0.f, 1.f, smooth)); \
1153 DIAGBL_TRANSITION(8, uint8_t, 1)
1154 DIAGBL_TRANSITION(16, uint16_t, 2)
1156 #define DIAGBR_TRANSITION(name, type, div) \
1157 static void diagbr##name##_transition(AVFilterContext *ctx, \
1158 const AVFrame *a, const AVFrame *b, AVFrame *out, \
1160 int slice_start, int slice_end, int jobnr) \
1162 XFadeContext *s = ctx->priv; \
1163 const int width = out->width; \
1164 const float w = width; \
1165 const float h = out->height; \
1167 for (int y = slice_start; y < slice_end; y++) { \
1168 for (int x = 0; x < width; x++) { \
1169 const float smooth = 1.f + (w - 1 - x) / w * (h - 1 - y) / h - \
1172 for (int p = 0; p < s->nb_planes; p++) { \
1173 const type *xf0 = (const type *)(a->data[p] + y * a->linesize[p]); \
1174 const type *xf1 = (const type *)(b->data[p] + y * b->linesize[p]); \
1175 type *dst = (type *)(out->data[p] + y * out->linesize[p]); \
1177 dst[x] = mix(xf1[x], xf0[x], smoothstep(0.f, 1.f, smooth)); \
1183 DIAGBR_TRANSITION(8, uint8_t, 1)
1184 DIAGBR_TRANSITION(16, uint16_t, 2)
1186 #define HLSLICE_TRANSITION(name, type, div) \
1187 static void hlslice##name##_transition(AVFilterContext *ctx, \
1188 const AVFrame *a, const AVFrame *b, AVFrame *out, \
1190 int slice_start, int slice_end, int jobnr) \
1192 XFadeContext *s = ctx->priv; \
1193 const int width = out->width; \
1194 const float w = width; \
1196 for (int y = slice_start; y < slice_end; y++) { \
1197 for (int x = 0; x < width; x++) { \
1198 const float smooth = smoothstep(-0.5f, 0.f, x / w - progress * 1.5f); \
1199 const float ss = smooth <= fract(10.f * x / w) ? 0.f : 1.f; \
1201 for (int p = 0; p < s->nb_planes; p++) { \
1202 const type *xf0 = (const type *)(a->data[p] + y * a->linesize[p]); \
1203 const type *xf1 = (const type *)(b->data[p] + y * b->linesize[p]); \
1204 type *dst = (type *)(out->data[p] + y * out->linesize[p]); \
1206 dst[x] = mix(xf1[x], xf0[x], ss); \
1212 HLSLICE_TRANSITION(8, uint8_t, 1)
1213 HLSLICE_TRANSITION(16, uint16_t, 2)
1215 #define HRSLICE_TRANSITION(name, type, div) \
1216 static void hrslice##name##_transition(AVFilterContext *ctx, \
1217 const AVFrame *a, const AVFrame *b, AVFrame *out, \
1219 int slice_start, int slice_end, int jobnr) \
1221 XFadeContext *s = ctx->priv; \
1222 const int width = out->width; \
1223 const float w = width; \
1225 for (int y = slice_start; y < slice_end; y++) { \
1226 for (int x = 0; x < width; x++) { \
1227 const float xx = (w - 1 - x) / w; \
1228 const float smooth = smoothstep(-0.5f, 0.f, xx - progress * 1.5f); \
1229 const float ss = smooth <= fract(10.f * xx) ? 0.f : 1.f; \
1231 for (int p = 0; p < s->nb_planes; p++) { \
1232 const type *xf0 = (const type *)(a->data[p] + y * a->linesize[p]); \
1233 const type *xf1 = (const type *)(b->data[p] + y * b->linesize[p]); \
1234 type *dst = (type *)(out->data[p] + y * out->linesize[p]); \
1236 dst[x] = mix(xf1[x], xf0[x], ss); \
1242 HRSLICE_TRANSITION(8, uint8_t, 1)
1243 HRSLICE_TRANSITION(16, uint16_t, 2)
1245 #define VUSLICE_TRANSITION(name, type, div) \
1246 static void vuslice##name##_transition(AVFilterContext *ctx, \
1247 const AVFrame *a, const AVFrame *b, AVFrame *out, \
1249 int slice_start, int slice_end, int jobnr) \
1251 XFadeContext *s = ctx->priv; \
1252 const int width = out->width; \
1253 const float h = out->height; \
1255 for (int y = slice_start; y < slice_end; y++) { \
1256 const float smooth = smoothstep(-0.5f, 0.f, y / h - progress * 1.5f); \
1257 const float ss = smooth <= fract(10.f * y / h) ? 0.f : 1.f; \
1259 for (int x = 0; x < width; x++) { \
1260 for (int p = 0; p < s->nb_planes; p++) { \
1261 const type *xf0 = (const type *)(a->data[p] + y * a->linesize[p]); \
1262 const type *xf1 = (const type *)(b->data[p] + y * b->linesize[p]); \
1263 type *dst = (type *)(out->data[p] + y * out->linesize[p]); \
1265 dst[x] = mix(xf1[x], xf0[x], ss); \
1271 VUSLICE_TRANSITION(8, uint8_t, 1)
1272 VUSLICE_TRANSITION(16, uint16_t, 2)
1274 #define VDSLICE_TRANSITION(name, type, div) \
1275 static void vdslice##name##_transition(AVFilterContext *ctx, \
1276 const AVFrame *a, const AVFrame *b, AVFrame *out, \
1278 int slice_start, int slice_end, int jobnr) \
1280 XFadeContext *s = ctx->priv; \
1281 const int width = out->width; \
1282 const float h = out->height; \
1284 for (int y = slice_start; y < slice_end; y++) { \
1285 const float yy = (h - 1 - y) / h; \
1286 const float smooth = smoothstep(-0.5f, 0.f, yy - progress * 1.5f); \
1287 const float ss = smooth <= fract(10.f * yy) ? 0.f : 1.f; \
1289 for (int x = 0; x < width; x++) { \
1290 for (int p = 0; p < s->nb_planes; p++) { \
1291 const type *xf0 = (const type *)(a->data[p] + y * a->linesize[p]); \
1292 const type *xf1 = (const type *)(b->data[p] + y * b->linesize[p]); \
1293 type *dst = (type *)(out->data[p] + y * out->linesize[p]); \
1295 dst[x] = mix(xf1[x], xf0[x], ss); \
1301 VDSLICE_TRANSITION(8, uint8_t, 1)
1302 VDSLICE_TRANSITION(16, uint16_t, 2)
1304 #define HBLUR_TRANSITION(name, type, div) \
1305 static void hblur##name##_transition(AVFilterContext *ctx, \
1306 const AVFrame *a, const AVFrame *b, AVFrame *out, \
1308 int slice_start, int slice_end, int jobnr) \
1310 XFadeContext *s = ctx->priv; \
1311 const int width = out->width; \
1312 const float prog = progress <= 0.5f ? progress * 2.f : (1.f - progress) * 2.f; \
1313 const int size = 1 + (width / 2) * prog; \
1315 for (int y = slice_start; y < slice_end; y++) { \
1316 for (int p = 0; p < s->nb_planes; p++) { \
1317 const type *xf0 = (const type *)(a->data[p] + y * a->linesize[p]); \
1318 const type *xf1 = (const type *)(b->data[p] + y * b->linesize[p]); \
1319 type *dst = (type *)(out->data[p] + y * out->linesize[p]); \
1324 for (int x = 0; x < size; x++) { \
1329 for (int x = 0; x < width; x++) { \
1330 dst[x] = mix(sum0 / cnt, sum1 / cnt, progress); \
1332 if (x + size < width) { \
1333 sum0 += xf0[x + size] - xf0[x]; \
1334 sum1 += xf1[x + size] - xf1[x]; \
1345 HBLUR_TRANSITION(8, uint8_t, 1)
1346 HBLUR_TRANSITION(16, uint16_t, 2)
1348 static inline double getpix(void *priv, double x, double y, int plane, int nb)
1350 XFadeContext *s = priv;
1351 AVFrame *in = s->xf[nb];
1352 const uint8_t *src = in->data[FFMIN(plane, s->nb_planes - 1)];
1353 int linesize = in->linesize[FFMIN(plane, s->nb_planes - 1)];
1354 const int w = in->width;
1355 const int h = in->height;
1359 xi = av_clipd(x, 0, w - 1);
1360 yi = av_clipd(y, 0, h - 1);
1363 const uint16_t *src16 = (const uint16_t*)src;
1366 return src16[xi + yi * linesize];
1368 return src[xi + yi * linesize];
1372 static double a0(void *priv, double x, double y) { return getpix(priv, x, y, 0, 0); }
1373 static double a1(void *priv, double x, double y) { return getpix(priv, x, y, 1, 0); }
1374 static double a2(void *priv, double x, double y) { return getpix(priv, x, y, 2, 0); }
1375 static double a3(void *priv, double x, double y) { return getpix(priv, x, y, 3, 0); }
1377 static double b0(void *priv, double x, double y) { return getpix(priv, x, y, 0, 1); }
1378 static double b1(void *priv, double x, double y) { return getpix(priv, x, y, 1, 1); }
1379 static double b2(void *priv, double x, double y) { return getpix(priv, x, y, 2, 1); }
1380 static double b3(void *priv, double x, double y) { return getpix(priv, x, y, 3, 1); }
1382 static int config_output(AVFilterLink *outlink)
1384 AVFilterContext *ctx = outlink->src;
1385 AVFilterLink *inlink0 = ctx->inputs[0];
1386 AVFilterLink *inlink1 = ctx->inputs[1];
1387 XFadeContext *s = ctx->priv;
1388 const AVPixFmtDescriptor *pix_desc = av_pix_fmt_desc_get(inlink0->format);
1391 if (inlink0->format != inlink1->format) {
1392 av_log(ctx, AV_LOG_ERROR, "inputs must be of same pixel format\n");
1393 return AVERROR(EINVAL);
1395 if (inlink0->w != inlink1->w || inlink0->h != inlink1->h) {
1396 av_log(ctx, AV_LOG_ERROR, "First input link %s parameters "
1397 "(size %dx%d) do not match the corresponding "
1398 "second input link %s parameters (size %dx%d)\n",
1399 ctx->input_pads[0].name, inlink0->w, inlink0->h,
1400 ctx->input_pads[1].name, inlink1->w, inlink1->h);
1401 return AVERROR(EINVAL);
1404 if (inlink0->time_base.num != inlink1->time_base.num ||
1405 inlink0->time_base.den != inlink1->time_base.den) {
1406 av_log(ctx, AV_LOG_ERROR, "First input link %s timebase "
1407 "(%d/%d) do not match the corresponding "
1408 "second input link %s timebase (%d/%d)\n",
1409 ctx->input_pads[0].name, inlink0->time_base.num, inlink0->time_base.den,
1410 ctx->input_pads[1].name, inlink1->time_base.num, inlink1->time_base.den);
1411 return AVERROR(EINVAL);
1414 if (!inlink0->frame_rate.num || !inlink0->frame_rate.den) {
1415 av_log(ctx, AV_LOG_ERROR, "The inputs needs to be a constant frame rate; "
1416 "current rate of %d/%d is invalid\n", inlink0->frame_rate.num, inlink0->frame_rate.den);
1417 return AVERROR(EINVAL);
1420 if (inlink0->frame_rate.num != inlink1->frame_rate.num ||
1421 inlink0->frame_rate.den != inlink1->frame_rate.den) {
1422 av_log(ctx, AV_LOG_ERROR, "First input link %s frame rate "
1423 "(%d/%d) do not match the corresponding "
1424 "second input link %s frame rate (%d/%d)\n",
1425 ctx->input_pads[0].name, inlink0->frame_rate.num, inlink0->frame_rate.den,
1426 ctx->input_pads[1].name, inlink1->frame_rate.num, inlink1->frame_rate.den);
1427 return AVERROR(EINVAL);
1430 outlink->w = inlink0->w;
1431 outlink->h = inlink0->h;
1432 outlink->time_base = inlink0->time_base;
1433 outlink->sample_aspect_ratio = inlink0->sample_aspect_ratio;
1434 outlink->frame_rate = inlink0->frame_rate;
1436 s->depth = pix_desc->comp[0].depth;
1437 is_rgb = !!(pix_desc->flags & AV_PIX_FMT_FLAG_RGB);
1438 s->nb_planes = av_pix_fmt_count_planes(inlink0->format);
1439 s->max_value = (1 << s->depth) - 1;
1441 s->black[1] = s->black[2] = is_rgb ? 0 : s->max_value / 2;
1442 s->black[3] = s->max_value;
1443 s->white[0] = s->white[3] = s->max_value;
1444 s->white[1] = s->white[2] = is_rgb ? s->max_value : s->max_value / 2;
1446 s->first_pts = s->last_pts = s->pts = AV_NOPTS_VALUE;
1449 s->duration_pts = av_rescale_q(s->duration, AV_TIME_BASE_Q, outlink->time_base);
1451 s->offset_pts = av_rescale_q(s->offset, AV_TIME_BASE_Q, outlink->time_base);
1453 switch (s->transition) {
1454 case CUSTOM: s->transitionf = s->depth <= 8 ? custom8_transition : custom16_transition; break;
1455 case FADE: s->transitionf = s->depth <= 8 ? fade8_transition : fade16_transition; break;
1456 case WIPELEFT: s->transitionf = s->depth <= 8 ? wipeleft8_transition : wipeleft16_transition; break;
1457 case WIPERIGHT: s->transitionf = s->depth <= 8 ? wiperight8_transition : wiperight16_transition; break;
1458 case WIPEUP: s->transitionf = s->depth <= 8 ? wipeup8_transition : wipeup16_transition; break;
1459 case WIPEDOWN: s->transitionf = s->depth <= 8 ? wipedown8_transition : wipedown16_transition; break;
1460 case SLIDELEFT: s->transitionf = s->depth <= 8 ? slideleft8_transition : slideleft16_transition; break;
1461 case SLIDERIGHT: s->transitionf = s->depth <= 8 ? slideright8_transition : slideright16_transition; break;
1462 case SLIDEUP: s->transitionf = s->depth <= 8 ? slideup8_transition : slideup16_transition; break;
1463 case SLIDEDOWN: s->transitionf = s->depth <= 8 ? slidedown8_transition : slidedown16_transition; break;
1464 case CIRCLECROP: s->transitionf = s->depth <= 8 ? circlecrop8_transition : circlecrop16_transition; break;
1465 case RECTCROP: s->transitionf = s->depth <= 8 ? rectcrop8_transition : rectcrop16_transition; break;
1466 case DISTANCE: s->transitionf = s->depth <= 8 ? distance8_transition : distance16_transition; break;
1467 case FADEBLACK: s->transitionf = s->depth <= 8 ? fadeblack8_transition : fadeblack16_transition; break;
1468 case FADEWHITE: s->transitionf = s->depth <= 8 ? fadewhite8_transition : fadewhite16_transition; break;
1469 case RADIAL: s->transitionf = s->depth <= 8 ? radial8_transition : radial16_transition; break;
1470 case SMOOTHLEFT: s->transitionf = s->depth <= 8 ? smoothleft8_transition : smoothleft16_transition; break;
1471 case SMOOTHRIGHT:s->transitionf = s->depth <= 8 ? smoothright8_transition: smoothright16_transition;break;
1472 case SMOOTHUP: s->transitionf = s->depth <= 8 ? smoothup8_transition : smoothup16_transition; break;
1473 case SMOOTHDOWN: s->transitionf = s->depth <= 8 ? smoothdown8_transition : smoothdown16_transition; break;
1474 case CIRCLEOPEN: s->transitionf = s->depth <= 8 ? circleopen8_transition : circleopen16_transition; break;
1475 case CIRCLECLOSE:s->transitionf = s->depth <= 8 ? circleclose8_transition: circleclose16_transition;break;
1476 case VERTOPEN: s->transitionf = s->depth <= 8 ? vertopen8_transition : vertopen16_transition; break;
1477 case VERTCLOSE: s->transitionf = s->depth <= 8 ? vertclose8_transition : vertclose16_transition; break;
1478 case HORZOPEN: s->transitionf = s->depth <= 8 ? horzopen8_transition : horzopen16_transition; break;
1479 case HORZCLOSE: s->transitionf = s->depth <= 8 ? horzclose8_transition : horzclose16_transition; break;
1480 case DISSOLVE: s->transitionf = s->depth <= 8 ? dissolve8_transition : dissolve16_transition; break;
1481 case PIXELIZE: s->transitionf = s->depth <= 8 ? pixelize8_transition : pixelize16_transition; break;
1482 case DIAGTL: s->transitionf = s->depth <= 8 ? diagtl8_transition : diagtl16_transition; break;
1483 case DIAGTR: s->transitionf = s->depth <= 8 ? diagtr8_transition : diagtr16_transition; break;
1484 case DIAGBL: s->transitionf = s->depth <= 8 ? diagbl8_transition : diagbl16_transition; break;
1485 case DIAGBR: s->transitionf = s->depth <= 8 ? diagbr8_transition : diagbr16_transition; break;
1486 case HLSLICE: s->transitionf = s->depth <= 8 ? hlslice8_transition : hlslice16_transition; break;
1487 case HRSLICE: s->transitionf = s->depth <= 8 ? hrslice8_transition : hrslice16_transition; break;
1488 case VUSLICE: s->transitionf = s->depth <= 8 ? vuslice8_transition : vuslice16_transition; break;
1489 case VDSLICE: s->transitionf = s->depth <= 8 ? vdslice8_transition : vdslice16_transition; break;
1490 case HBLUR: s->transitionf = s->depth <= 8 ? hblur8_transition : hblur16_transition; break;
1493 if (s->transition == CUSTOM) {
1494 static const char *const func2_names[] = {
1495 "a0", "a1", "a2", "a3",
1496 "b0", "b1", "b2", "b3",
1499 double (*func2[])(void *, double, double) = {
1506 return AVERROR(EINVAL);
1507 ret = av_expr_parse(&s->e, s->custom_str, var_names,
1508 NULL, NULL, func2_names, func2, 0, ctx);
1516 static int xfade_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
1518 XFadeContext *s = ctx->priv;
1519 AVFilterLink *outlink = ctx->outputs[0];
1520 ThreadData *td = arg;
1521 int slice_start = (outlink->h * jobnr ) / nb_jobs;
1522 int slice_end = (outlink->h * (jobnr+1)) / nb_jobs;
1524 s->transitionf(ctx, td->xf[0], td->xf[1], td->out, td->progress, slice_start, slice_end, jobnr);
1529 static int xfade_frame(AVFilterContext *ctx, AVFrame *a, AVFrame *b)
1531 XFadeContext *s = ctx->priv;
1532 AVFilterLink *outlink = ctx->outputs[0];
1533 float progress = av_clipf(1.f - ((float)(s->pts - s->first_pts - s->offset_pts) / s->duration_pts), 0.f, 1.f);
1537 out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
1539 return AVERROR(ENOMEM);
1541 td.xf[0] = a, td.xf[1] = b, td.out = out, td.progress = progress;
1542 ctx->internal->execute(ctx, xfade_slice, &td, NULL, FFMIN(outlink->h, ff_filter_get_nb_threads(ctx)));
1546 return ff_filter_frame(outlink, out);
1549 static int xfade_activate(AVFilterContext *ctx)
1551 XFadeContext *s = ctx->priv;
1552 AVFilterLink *outlink = ctx->outputs[0];
1554 int ret = 0, status;
1557 FF_FILTER_FORWARD_STATUS_BACK_ALL(outlink, ctx);
1559 if (s->xfade_is_over) {
1560 ret = ff_inlink_consume_frame(ctx->inputs[1], &in);
1563 } else if (ret > 0) {
1564 in->pts = (in->pts - s->last_pts) + s->pts;
1565 return ff_filter_frame(outlink, in);
1566 } else if (ff_inlink_acknowledge_status(ctx->inputs[1], &status, &pts)) {
1567 ff_outlink_set_status(outlink, status, s->pts);
1570 if (ff_outlink_frame_wanted(outlink)) {
1571 ff_inlink_request_frame(ctx->inputs[1]);
1577 if (ff_inlink_queued_frames(ctx->inputs[0]) > 0) {
1578 s->xf[0] = ff_inlink_peek_frame(ctx->inputs[0], 0);
1580 if (s->first_pts == AV_NOPTS_VALUE) {
1581 s->first_pts = s->xf[0]->pts;
1583 s->pts = s->xf[0]->pts;
1584 if (s->first_pts + s->offset_pts > s->xf[0]->pts) {
1587 ff_inlink_consume_frame(ctx->inputs[0], &in);
1588 return ff_filter_frame(outlink, in);
1595 if (s->xf[0] && ff_inlink_queued_frames(ctx->inputs[1]) > 0) {
1596 ff_inlink_consume_frame(ctx->inputs[0], &s->xf[0]);
1597 ff_inlink_consume_frame(ctx->inputs[1], &s->xf[1]);
1599 s->last_pts = s->xf[1]->pts;
1600 s->pts = s->xf[0]->pts;
1601 if (s->xf[0]->pts - (s->first_pts + s->offset_pts) > s->duration_pts)
1602 s->xfade_is_over = 1;
1603 ret = xfade_frame(ctx, s->xf[0], s->xf[1]);
1604 av_frame_free(&s->xf[0]);
1605 av_frame_free(&s->xf[1]);
1609 if (ff_inlink_queued_frames(ctx->inputs[0]) > 0 &&
1610 ff_inlink_queued_frames(ctx->inputs[1]) > 0) {
1611 ff_filter_set_ready(ctx, 100);
1615 if (ff_outlink_frame_wanted(outlink)) {
1616 if (!s->eof[0] && ff_outlink_get_status(ctx->inputs[0])) {
1618 s->xfade_is_over = 1;
1620 if (!s->eof[1] && ff_outlink_get_status(ctx->inputs[1])) {
1623 if (!s->eof[0] && !s->xf[0])
1624 ff_inlink_request_frame(ctx->inputs[0]);
1625 if (!s->eof[1] && (s->need_second || s->eof[0]))
1626 ff_inlink_request_frame(ctx->inputs[1]);
1627 if (s->eof[0] && s->eof[1] && (
1628 ff_inlink_queued_frames(ctx->inputs[0]) <= 0 ||
1629 ff_inlink_queued_frames(ctx->inputs[1]) <= 0))
1630 ff_outlink_set_status(outlink, AVERROR_EOF, AV_NOPTS_VALUE);
1634 return FFERROR_NOT_READY;
1637 static const AVFilterPad xfade_inputs[] = {
1640 .type = AVMEDIA_TYPE_VIDEO,
1644 .type = AVMEDIA_TYPE_VIDEO,
1649 static const AVFilterPad xfade_outputs[] = {
1652 .type = AVMEDIA_TYPE_VIDEO,
1653 .config_props = config_output,
1658 AVFilter ff_vf_xfade = {
1660 .description = NULL_IF_CONFIG_SMALL("Cross fade one video with another video."),
1661 .priv_size = sizeof(XFadeContext),
1662 .priv_class = &xfade_class,
1663 .query_formats = query_formats,
1664 .activate = xfade_activate,
1666 .inputs = xfade_inputs,
1667 .outputs = xfade_outputs,
1668 .flags = AVFILTER_FLAG_SLICE_THREADS,