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 {
57 typedef struct XFadeContext {
81 void (*transitionf)(AVFilterContext *ctx, const AVFrame *a, const AVFrame *b, AVFrame *out, float progress,
82 int slice_start, int slice_end, int jobnr);
87 static const char *const var_names[] = { "X", "Y", "W", "H", "A", "B", "PLANE", "P", NULL };
88 enum { VAR_X, VAR_Y, VAR_W, VAR_H, VAR_A, VAR_B, VAR_PLANE, VAR_PROGRESS, VAR_VARS_NB };
90 typedef struct ThreadData {
96 static int query_formats(AVFilterContext *ctx)
98 static const enum AVPixelFormat pix_fmts[] = {
102 AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRAP, AV_PIX_FMT_GRAY8,
103 AV_PIX_FMT_YUVA444P9, AV_PIX_FMT_GBRP9,
104 AV_PIX_FMT_YUV444P10,
105 AV_PIX_FMT_YUVA444P10,
106 AV_PIX_FMT_GBRP10, AV_PIX_FMT_GBRAP10, AV_PIX_FMT_GRAY10,
107 AV_PIX_FMT_YUV444P12,
108 AV_PIX_FMT_YUVA444P12,
109 AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRAP12, AV_PIX_FMT_GRAY12,
110 AV_PIX_FMT_YUV444P14, AV_PIX_FMT_GBRP14,
111 AV_PIX_FMT_YUV444P16,
112 AV_PIX_FMT_YUVA444P16,
113 AV_PIX_FMT_GBRP16, AV_PIX_FMT_GBRAP16, AV_PIX_FMT_GRAY16,
117 AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
119 return AVERROR(ENOMEM);
120 return ff_set_common_formats(ctx, fmts_list);
123 static av_cold void uninit(AVFilterContext *ctx)
125 XFadeContext *s = ctx->priv;
130 #define OFFSET(x) offsetof(XFadeContext, x)
131 #define FLAGS (AV_OPT_FLAG_FILTERING_PARAM | AV_OPT_FLAG_VIDEO_PARAM)
133 static const AVOption xfade_options[] = {
134 { "transition", "set cross fade transition", OFFSET(transition), AV_OPT_TYPE_INT, {.i64=FADE}, -1, NB_TRANSITIONS-1, FLAGS, "transition" },
135 { "custom", "custom transition", 0, AV_OPT_TYPE_CONST, {.i64=CUSTOM}, 0, 0, FLAGS, "transition" },
136 { "fade", "fade transition", 0, AV_OPT_TYPE_CONST, {.i64=FADE}, 0, 0, FLAGS, "transition" },
137 { "wipeleft", "wipe left transition", 0, AV_OPT_TYPE_CONST, {.i64=WIPELEFT}, 0, 0, FLAGS, "transition" },
138 { "wiperight", "wipe right transition", 0, AV_OPT_TYPE_CONST, {.i64=WIPERIGHT}, 0, 0, FLAGS, "transition" },
139 { "wipeup", "wipe up transition", 0, AV_OPT_TYPE_CONST, {.i64=WIPEUP}, 0, 0, FLAGS, "transition" },
140 { "wipedown", "wipe down transition", 0, AV_OPT_TYPE_CONST, {.i64=WIPEDOWN}, 0, 0, FLAGS, "transition" },
141 { "slideleft", "slide left transition", 0, AV_OPT_TYPE_CONST, {.i64=SLIDELEFT}, 0, 0, FLAGS, "transition" },
142 { "slideright", "slide right transition", 0, AV_OPT_TYPE_CONST, {.i64=SLIDERIGHT}, 0, 0, FLAGS, "transition" },
143 { "slideup", "slide up transition", 0, AV_OPT_TYPE_CONST, {.i64=SLIDEUP}, 0, 0, FLAGS, "transition" },
144 { "slidedown", "slide down transition", 0, AV_OPT_TYPE_CONST, {.i64=SLIDEDOWN}, 0, 0, FLAGS, "transition" },
145 { "circlecrop", "circle crop transition", 0, AV_OPT_TYPE_CONST, {.i64=CIRCLECROP}, 0, 0, FLAGS, "transition" },
146 { "rectcrop", "rect crop transition", 0, AV_OPT_TYPE_CONST, {.i64=RECTCROP}, 0, 0, FLAGS, "transition" },
147 { "distance", "distance transition", 0, AV_OPT_TYPE_CONST, {.i64=DISTANCE}, 0, 0, FLAGS, "transition" },
148 { "fadeblack", "fadeblack transition", 0, AV_OPT_TYPE_CONST, {.i64=FADEBLACK}, 0, 0, FLAGS, "transition" },
149 { "fadewhite", "fadewhite transition", 0, AV_OPT_TYPE_CONST, {.i64=FADEWHITE}, 0, 0, FLAGS, "transition" },
150 { "radial", "radial transition", 0, AV_OPT_TYPE_CONST, {.i64=RADIAL}, 0, 0, FLAGS, "transition" },
151 { "smoothleft", "smoothleft transition", 0, AV_OPT_TYPE_CONST, {.i64=SMOOTHLEFT}, 0, 0, FLAGS, "transition" },
152 { "smoothright","smoothright transition", 0, AV_OPT_TYPE_CONST, {.i64=SMOOTHRIGHT},0, 0, FLAGS, "transition" },
153 { "smoothup", "smoothup transition", 0, AV_OPT_TYPE_CONST, {.i64=SMOOTHUP}, 0, 0, FLAGS, "transition" },
154 { "smoothdown", "smoothdown transition", 0, AV_OPT_TYPE_CONST, {.i64=SMOOTHDOWN}, 0, 0, FLAGS, "transition" },
155 { "circleopen", "circleopen transition", 0, AV_OPT_TYPE_CONST, {.i64=CIRCLEOPEN}, 0, 0, FLAGS, "transition" },
156 { "circleclose","circleclose transition", 0, AV_OPT_TYPE_CONST, {.i64=CIRCLECLOSE},0, 0, FLAGS, "transition" },
157 { "duration", "set cross fade duration", OFFSET(duration), AV_OPT_TYPE_DURATION, {.i64=1000000}, 0, 60000000, FLAGS },
158 { "offset", "set cross fade start relative to first input stream", OFFSET(offset), AV_OPT_TYPE_DURATION, {.i64=0}, INT64_MIN, INT64_MAX, FLAGS },
159 { "expr", "set expression for custom transition", OFFSET(custom_str), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, FLAGS },
163 AVFILTER_DEFINE_CLASS(xfade);
165 #define CUSTOM_TRANSITION(name, type, div) \
166 static void custom##name##_transition(AVFilterContext *ctx, \
167 const AVFrame *a, const AVFrame *b, AVFrame *out, \
169 int slice_start, int slice_end, int jobnr) \
171 XFadeContext *s = ctx->priv; \
172 const int height = slice_end - slice_start; \
174 double values[VAR_VARS_NB]; \
175 values[VAR_W] = out->width; \
176 values[VAR_H] = out->height; \
177 values[VAR_PROGRESS] = progress; \
179 for (int p = 0; p < s->nb_planes; p++) { \
180 const type *xf0 = (const type *)(a->data[p] + slice_start * a->linesize[p]); \
181 const type *xf1 = (const type *)(b->data[p] + slice_start * b->linesize[p]); \
182 type *dst = (type *)(out->data[p] + slice_start * out->linesize[p]); \
184 values[VAR_PLANE] = p; \
186 for (int y = 0; y < height; y++) { \
187 values[VAR_Y] = slice_start + y; \
188 for (int x = 0; x < out->width; x++) { \
190 values[VAR_A] = xf0[x]; \
191 values[VAR_B] = xf1[x]; \
192 dst[x] = av_expr_eval(s->e, values, s); \
195 dst += out->linesize[p] / div; \
196 xf0 += a->linesize[p] / div; \
197 xf1 += b->linesize[p] / div; \
202 CUSTOM_TRANSITION(8, uint8_t, 1)
203 CUSTOM_TRANSITION(16, uint16_t, 2)
205 static inline float mix(float a, float b, float mix)
207 return a * mix + b * (1.f - mix);
210 static inline float smoothstep(float edge0, float edge1, float x)
214 t = av_clipf((x - edge0) / (edge1 - edge0), 0.f, 1.f);
216 return t * t * (3.f - 2.f * t);
219 #define FADE_TRANSITION(name, type, div) \
220 static void fade##name##_transition(AVFilterContext *ctx, \
221 const AVFrame *a, const AVFrame *b, AVFrame *out, \
223 int slice_start, int slice_end, int jobnr) \
225 XFadeContext *s = ctx->priv; \
226 const int height = slice_end - slice_start; \
228 for (int p = 0; p < s->nb_planes; p++) { \
229 const type *xf0 = (const type *)(a->data[p] + slice_start * a->linesize[p]); \
230 const type *xf1 = (const type *)(b->data[p] + slice_start * b->linesize[p]); \
231 type *dst = (type *)(out->data[p] + slice_start * out->linesize[p]); \
233 for (int y = 0; y < height; y++) { \
234 for (int x = 0; x < out->width; x++) { \
235 dst[x] = mix(xf0[x], xf1[x], progress); \
238 dst += out->linesize[p] / div; \
239 xf0 += a->linesize[p] / div; \
240 xf1 += b->linesize[p] / div; \
245 FADE_TRANSITION(8, uint8_t, 1)
246 FADE_TRANSITION(16, uint16_t, 2)
248 #define WIPELEFT_TRANSITION(name, type, div) \
249 static void wipeleft##name##_transition(AVFilterContext *ctx, \
250 const AVFrame *a, const AVFrame *b, AVFrame *out, \
252 int slice_start, int slice_end, int jobnr) \
254 XFadeContext *s = ctx->priv; \
255 const int height = slice_end - slice_start; \
256 const int z = out->width * progress; \
258 for (int p = 0; p < s->nb_planes; p++) { \
259 const type *xf0 = (const type *)(a->data[p] + slice_start * a->linesize[p]); \
260 const type *xf1 = (const type *)(b->data[p] + slice_start * b->linesize[p]); \
261 type *dst = (type *)(out->data[p] + slice_start * out->linesize[p]); \
263 for (int y = 0; y < height; y++) { \
264 for (int x = 0; x < out->width; x++) { \
265 dst[x] = x > z ? xf1[x] : xf0[x]; \
268 dst += out->linesize[p] / div; \
269 xf0 += a->linesize[p] / div; \
270 xf1 += b->linesize[p] / div; \
275 WIPELEFT_TRANSITION(8, uint8_t, 1)
276 WIPELEFT_TRANSITION(16, uint16_t, 2)
278 #define WIPERIGHT_TRANSITION(name, type, div) \
279 static void wiperight##name##_transition(AVFilterContext *ctx, \
280 const AVFrame *a, const AVFrame *b, AVFrame *out, \
282 int slice_start, int slice_end, int jobnr) \
284 XFadeContext *s = ctx->priv; \
285 const int height = slice_end - slice_start; \
286 const int z = out->width * (1.f - progress); \
288 for (int p = 0; p < s->nb_planes; p++) { \
289 const type *xf0 = (const type *)(a->data[p] + slice_start * a->linesize[p]); \
290 const type *xf1 = (const type *)(b->data[p] + slice_start * b->linesize[p]); \
291 type *dst = (type *)(out->data[p] + slice_start * out->linesize[p]); \
293 for (int y = 0; y < height; y++) { \
294 for (int x = 0; x < out->width; x++) { \
295 dst[x] = x > z ? xf0[x] : xf1[x]; \
298 dst += out->linesize[p] / div; \
299 xf0 += a->linesize[p] / div; \
300 xf1 += b->linesize[p] / div; \
305 WIPERIGHT_TRANSITION(8, uint8_t, 1)
306 WIPERIGHT_TRANSITION(16, uint16_t, 2)
308 #define WIPEUP_TRANSITION(name, type, div) \
309 static void wipeup##name##_transition(AVFilterContext *ctx, \
310 const AVFrame *a, const AVFrame *b, AVFrame *out, \
312 int slice_start, int slice_end, int jobnr) \
314 XFadeContext *s = ctx->priv; \
315 const int height = slice_end - slice_start; \
316 const int z = out->height * progress; \
318 for (int p = 0; p < s->nb_planes; p++) { \
319 const type *xf0 = (const type *)(a->data[p] + slice_start * a->linesize[p]); \
320 const type *xf1 = (const type *)(b->data[p] + slice_start * b->linesize[p]); \
321 type *dst = (type *)(out->data[p] + slice_start * out->linesize[p]); \
323 for (int y = 0; y < height; y++) { \
324 for (int x = 0; x < out->width; x++) { \
325 dst[x] = slice_start + y > z ? xf1[x] : xf0[x]; \
328 dst += out->linesize[p] / div; \
329 xf0 += a->linesize[p] / div; \
330 xf1 += b->linesize[p] / div; \
335 WIPEUP_TRANSITION(8, uint8_t, 1)
336 WIPEUP_TRANSITION(16, uint16_t, 2)
338 #define WIPEDOWN_TRANSITION(name, type, div) \
339 static void wipedown##name##_transition(AVFilterContext *ctx, \
340 const AVFrame *a, const AVFrame *b, AVFrame *out, \
342 int slice_start, int slice_end, int jobnr) \
344 XFadeContext *s = ctx->priv; \
345 const int height = slice_end - slice_start; \
346 const int z = out->height * (1.f - progress); \
348 for (int p = 0; p < s->nb_planes; p++) { \
349 const type *xf0 = (const type *)(a->data[p] + slice_start * a->linesize[p]); \
350 const type *xf1 = (const type *)(b->data[p] + slice_start * b->linesize[p]); \
351 type *dst = (type *)(out->data[p] + slice_start * out->linesize[p]); \
353 for (int y = 0; y < height; y++) { \
354 for (int x = 0; x < out->width; x++) { \
355 dst[x] = slice_start + y > z ? xf0[x] : xf1[x]; \
358 dst += out->linesize[p] / div; \
359 xf0 += a->linesize[p] / div; \
360 xf1 += b->linesize[p] / div; \
365 WIPEDOWN_TRANSITION(8, uint8_t, 1)
366 WIPEDOWN_TRANSITION(16, uint16_t, 2)
368 #define SLIDELEFT_TRANSITION(name, type, div) \
369 static void slideleft##name##_transition(AVFilterContext *ctx, \
370 const AVFrame *a, const AVFrame *b, AVFrame *out, \
372 int slice_start, int slice_end, int jobnr) \
374 XFadeContext *s = ctx->priv; \
375 const int height = slice_end - slice_start; \
376 const int width = out->width; \
377 const int z = -progress * width; \
379 for (int p = 0; p < s->nb_planes; p++) { \
380 const type *xf0 = (const type *)(a->data[p] + slice_start * a->linesize[p]); \
381 const type *xf1 = (const type *)(b->data[p] + slice_start * b->linesize[p]); \
382 type *dst = (type *)(out->data[p] + slice_start * out->linesize[p]); \
384 for (int y = 0; y < height; y++) { \
385 for (int x = 0; x < width; x++) { \
386 const int zx = z + x; \
387 const int zz = zx % width + width * (zx < 0); \
388 dst[x] = (zx > 0) && (zx < width) ? xf1[zz] : xf0[zz]; \
391 dst += out->linesize[p] / div; \
392 xf0 += a->linesize[p] / div; \
393 xf1 += b->linesize[p] / div; \
398 SLIDELEFT_TRANSITION(8, uint8_t, 1)
399 SLIDELEFT_TRANSITION(16, uint16_t, 2)
401 #define SLIDERIGHT_TRANSITION(name, type, div) \
402 static void slideright##name##_transition(AVFilterContext *ctx, \
403 const AVFrame *a, const AVFrame *b, AVFrame *out, \
405 int slice_start, int slice_end, int jobnr) \
407 XFadeContext *s = ctx->priv; \
408 const int height = slice_end - slice_start; \
409 const int width = out->width; \
410 const int z = progress * width; \
412 for (int p = 0; p < s->nb_planes; p++) { \
413 const type *xf0 = (const type *)(a->data[p] + slice_start * a->linesize[p]); \
414 const type *xf1 = (const type *)(b->data[p] + slice_start * b->linesize[p]); \
415 type *dst = (type *)(out->data[p] + slice_start * out->linesize[p]); \
417 for (int y = 0; y < height; y++) { \
418 for (int x = 0; x < out->width; x++) { \
419 const int zx = z + x; \
420 const int zz = zx % width + width * (zx < 0); \
421 dst[x] = (zx > 0) && (zx < width) ? xf1[zz] : xf0[zz]; \
424 dst += out->linesize[p] / div; \
425 xf0 += a->linesize[p] / div; \
426 xf1 += b->linesize[p] / div; \
431 SLIDERIGHT_TRANSITION(8, uint8_t, 1)
432 SLIDERIGHT_TRANSITION(16, uint16_t, 2)
434 #define SLIDEUP_TRANSITION(name, type, div) \
435 static void slideup##name##_transition(AVFilterContext *ctx, \
436 const AVFrame *a, const AVFrame *b, AVFrame *out, \
438 int slice_start, int slice_end, int jobnr) \
440 XFadeContext *s = ctx->priv; \
441 const int height = out->height; \
442 const int z = -progress * height; \
444 for (int p = 0; p < s->nb_planes; p++) { \
445 type *dst = (type *)(out->data[p] + slice_start * out->linesize[p]); \
447 for (int y = slice_start; y < slice_end; y++) { \
448 const int zy = z + y; \
449 const int zz = zy % height + height * (zy < 0); \
450 const type *xf0 = (const type *)(a->data[p] + zz * a->linesize[p]); \
451 const type *xf1 = (const type *)(b->data[p] + zz * b->linesize[p]); \
453 for (int x = 0; x < out->width; x++) { \
454 dst[x] = (zy > 0) && (zy < height) ? xf1[x] : xf0[x]; \
457 dst += out->linesize[p] / div; \
462 SLIDEUP_TRANSITION(8, uint8_t, 1)
463 SLIDEUP_TRANSITION(16, uint16_t, 2)
465 #define SLIDEDOWN_TRANSITION(name, type, div) \
466 static void slidedown##name##_transition(AVFilterContext *ctx, \
467 const AVFrame *a, const AVFrame *b, AVFrame *out, \
469 int slice_start, int slice_end, int jobnr) \
471 XFadeContext *s = ctx->priv; \
472 const int height = out->height; \
473 const int z = progress * height; \
475 for (int p = 0; p < s->nb_planes; p++) { \
476 type *dst = (type *)(out->data[p] + slice_start * out->linesize[p]); \
478 for (int y = slice_start; y < slice_end; y++) { \
479 const int zy = z + y; \
480 const int zz = zy % height + height * (zy < 0); \
481 const type *xf0 = (const type *)(a->data[p] + zz * a->linesize[p]); \
482 const type *xf1 = (const type *)(b->data[p] + zz * b->linesize[p]); \
484 for (int x = 0; x < out->width; x++) { \
485 dst[x] = (zy > 0) && (zy < height) ? xf1[x] : xf0[x]; \
488 dst += out->linesize[p] / div; \
493 SLIDEDOWN_TRANSITION(8, uint8_t, 1)
494 SLIDEDOWN_TRANSITION(16, uint16_t, 2)
496 #define CIRCLECROP_TRANSITION(name, type, div) \
497 static void circlecrop##name##_transition(AVFilterContext *ctx, \
498 const AVFrame *a, const AVFrame *b, AVFrame *out, \
500 int slice_start, int slice_end, int jobnr) \
502 XFadeContext *s = ctx->priv; \
503 const int width = out->width; \
504 const int height = out->height; \
505 float z = powf(2.f * fabsf(progress - 0.5f), 3.f) * hypotf(width/2, height/2); \
507 for (int p = 0; p < s->nb_planes; p++) { \
508 const int bg = s->black[p]; \
509 type *dst = (type *)(out->data[p] + slice_start * out->linesize[p]); \
511 for (int y = slice_start; y < slice_end; y++) { \
512 const type *xf0 = (const type *)(a->data[p] + y * a->linesize[p]); \
513 const type *xf1 = (const type *)(b->data[p] + y * b->linesize[p]); \
515 for (int x = 0; x < width; x++) { \
516 float dist = hypotf(x - width / 2, y - height / 2); \
517 int val = progress < 0.5f ? xf1[x] : xf0[x]; \
518 dst[x] = (z < dist) ? bg : val; \
521 dst += out->linesize[p] / div; \
526 CIRCLECROP_TRANSITION(8, uint8_t, 1)
527 CIRCLECROP_TRANSITION(16, uint16_t, 2)
529 #define RECTCROP_TRANSITION(name, type, div) \
530 static void rectcrop##name##_transition(AVFilterContext *ctx, \
531 const AVFrame *a, const AVFrame *b, AVFrame *out, \
533 int slice_start, int slice_end, int jobnr) \
535 XFadeContext *s = ctx->priv; \
536 const int width = out->width; \
537 const int height = out->height; \
538 int zh = fabsf(progress - 0.5f) * height; \
539 int zw = fabsf(progress - 0.5f) * width; \
541 for (int p = 0; p < s->nb_planes; p++) { \
542 const int bg = s->black[p]; \
543 type *dst = (type *)(out->data[p] + slice_start * out->linesize[p]); \
545 for (int y = slice_start; y < slice_end; y++) { \
546 const type *xf0 = (const type *)(a->data[p] + y * a->linesize[p]); \
547 const type *xf1 = (const type *)(b->data[p] + y * b->linesize[p]); \
549 for (int x = 0; x < width; x++) { \
550 int dist = FFABS(x - width / 2) < zw && \
551 FFABS(y - height / 2) < zh; \
552 int val = progress < 0.5f ? xf1[x] : xf0[x]; \
553 dst[x] = !dist ? bg : val; \
556 dst += out->linesize[p] / div; \
561 RECTCROP_TRANSITION(8, uint8_t, 1)
562 RECTCROP_TRANSITION(16, uint16_t, 2)
564 #define DISTANCE_TRANSITION(name, type, div) \
565 static void distance##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 float max = s->max_value; \
574 for (int y = slice_start; y < slice_end; y++) { \
575 for (int x = 0; x < width; x++) { \
577 for (int p = 0; p < s->nb_planes; p++) { \
578 const type *xf0 = (const type *)(a->data[p] + y * a->linesize[p]); \
579 const type *xf1 = (const type *)(b->data[p] + y * b->linesize[p]); \
581 dist += (xf0[x] / max - xf1[x] / max) * \
582 (xf0[x] / max - xf1[x] / max); \
585 dist = sqrtf(dist) <= progress; \
586 for (int p = 0; p < s->nb_planes; p++) { \
587 const type *xf0 = (const type *)(a->data[p] + y * a->linesize[p]); \
588 const type *xf1 = (const type *)(b->data[p] + y * b->linesize[p]); \
589 type *dst = (type *)(out->data[p] + y * out->linesize[p]); \
590 dst[x] = mix(mix(xf0[x], xf1[x], dist), xf1[x], progress); \
596 DISTANCE_TRANSITION(8, uint8_t, 1)
597 DISTANCE_TRANSITION(16, uint16_t, 2)
599 #define FADEBLACK_TRANSITION(name, type, div) \
600 static void fadeblack##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 height = slice_end - slice_start; \
607 const float phase = 0.2f; \
609 for (int p = 0; p < s->nb_planes; p++) { \
610 const type *xf0 = (const type *)(a->data[p] + slice_start * a->linesize[p]); \
611 const type *xf1 = (const type *)(b->data[p] + slice_start * b->linesize[p]); \
612 type *dst = (type *)(out->data[p] + slice_start * out->linesize[p]); \
613 const int bg = s->black[p]; \
615 for (int y = 0; y < height; y++) { \
616 for (int x = 0; x < out->width; x++) { \
617 dst[x] = mix(mix(xf0[x], bg, smoothstep(1.f-phase, 1.f, progress)), \
618 mix(bg, xf1[x], smoothstep(phase, 1.f, progress)), \
622 dst += out->linesize[p] / div; \
623 xf0 += a->linesize[p] / div; \
624 xf1 += b->linesize[p] / div; \
629 FADEBLACK_TRANSITION(8, uint8_t, 1)
630 FADEBLACK_TRANSITION(16, uint16_t, 2)
632 #define FADEWHITE_TRANSITION(name, type, div) \
633 static void fadewhite##name##_transition(AVFilterContext *ctx, \
634 const AVFrame *a, const AVFrame *b, AVFrame *out, \
636 int slice_start, int slice_end, int jobnr) \
638 XFadeContext *s = ctx->priv; \
639 const int height = slice_end - slice_start; \
640 const float phase = 0.2f; \
642 for (int p = 0; p < s->nb_planes; p++) { \
643 const type *xf0 = (const type *)(a->data[p] + slice_start * a->linesize[p]); \
644 const type *xf1 = (const type *)(b->data[p] + slice_start * b->linesize[p]); \
645 type *dst = (type *)(out->data[p] + slice_start * out->linesize[p]); \
646 const int bg = s->white[p]; \
648 for (int y = 0; y < height; y++) { \
649 for (int x = 0; x < out->width; x++) { \
650 dst[x] = mix(mix(xf0[x], bg, smoothstep(1.f-phase, 1.f, progress)), \
651 mix(bg, xf1[x], smoothstep(phase, 1.f, progress)), \
655 dst += out->linesize[p] / div; \
656 xf0 += a->linesize[p] / div; \
657 xf1 += b->linesize[p] / div; \
662 FADEWHITE_TRANSITION(8, uint8_t, 1)
663 FADEWHITE_TRANSITION(16, uint16_t, 2)
665 #define RADIAL_TRANSITION(name, type, div) \
666 static void radial##name##_transition(AVFilterContext *ctx, \
667 const AVFrame *a, const AVFrame *b, AVFrame *out, \
669 int slice_start, int slice_end, int jobnr) \
671 XFadeContext *s = ctx->priv; \
672 const int width = out->width; \
673 const int height = out->height; \
675 for (int y = slice_start; y < slice_end; y++) { \
676 for (int x = 0; x < width; x++) { \
677 const float smooth = atan2f(x - width / 2, y - height / 2) - \
678 (progress - 0.5f) * (M_PI * 2.5f); \
679 for (int p = 0; p < s->nb_planes; p++) { \
680 const type *xf0 = (const type *)(a->data[p] + y * a->linesize[p]); \
681 const type *xf1 = (const type *)(b->data[p] + y * b->linesize[p]); \
682 type *dst = (type *)(out->data[p] + y * out->linesize[p]); \
684 dst[x] = mix(xf1[x], xf0[x], smoothstep(0.f, 1.f, smooth)); \
690 RADIAL_TRANSITION(8, uint8_t, 1)
691 RADIAL_TRANSITION(16, uint16_t, 2)
693 #define SMOOTHLEFT_TRANSITION(name, type, div) \
694 static void smoothleft##name##_transition(AVFilterContext *ctx, \
695 const AVFrame *a, const AVFrame *b, AVFrame *out, \
697 int slice_start, int slice_end, int jobnr) \
699 XFadeContext *s = ctx->priv; \
700 const int width = out->width; \
701 const float w = width; \
703 for (int y = slice_start; y < slice_end; y++) { \
704 for (int x = 0; x < width; x++) { \
705 const float smooth = 1.f + x / w - progress * 2.f; \
707 for (int p = 0; p < s->nb_planes; p++) { \
708 const type *xf0 = (const type *)(a->data[p] + y * a->linesize[p]); \
709 const type *xf1 = (const type *)(b->data[p] + y * b->linesize[p]); \
710 type *dst = (type *)(out->data[p] + y * out->linesize[p]); \
712 dst[x] = mix(xf1[x], xf0[x], smoothstep(0.f, 1.f, smooth)); \
718 SMOOTHLEFT_TRANSITION(8, uint8_t, 1)
719 SMOOTHLEFT_TRANSITION(16, uint16_t, 2)
721 #define SMOOTHRIGHT_TRANSITION(name, type, div) \
722 static void smoothright##name##_transition(AVFilterContext *ctx, \
723 const AVFrame *a, const AVFrame *b, AVFrame *out, \
725 int slice_start, int slice_end, int jobnr) \
727 XFadeContext *s = ctx->priv; \
728 const int width = out->width; \
729 const float w = width; \
731 for (int y = slice_start; y < slice_end; y++) { \
732 for (int x = 0; x < width; x++) { \
733 const float smooth = 1.f + (w - 1 - x) / w - progress * 2.f; \
735 for (int p = 0; p < s->nb_planes; p++) { \
736 const type *xf0 = (const type *)(a->data[p] + y * a->linesize[p]); \
737 const type *xf1 = (const type *)(b->data[p] + y * b->linesize[p]); \
738 type *dst = (type *)(out->data[p] + y * out->linesize[p]); \
740 dst[x] = mix(xf1[x], xf0[x], smoothstep(0.f, 1.f, smooth)); \
746 SMOOTHRIGHT_TRANSITION(8, uint8_t, 1)
747 SMOOTHRIGHT_TRANSITION(16, uint16_t, 2)
749 #define SMOOTHUP_TRANSITION(name, type, div) \
750 static void smoothup##name##_transition(AVFilterContext *ctx, \
751 const AVFrame *a, const AVFrame *b, AVFrame *out, \
753 int slice_start, int slice_end, int jobnr) \
755 XFadeContext *s = ctx->priv; \
756 const int width = out->width; \
757 const float h = out->height; \
759 for (int y = slice_start; y < slice_end; y++) { \
760 const float smooth = 1.f + y / h - progress * 2.f; \
761 for (int x = 0; x < width; x++) { \
762 for (int p = 0; p < s->nb_planes; p++) { \
763 const type *xf0 = (const type *)(a->data[p] + y * a->linesize[p]); \
764 const type *xf1 = (const type *)(b->data[p] + y * b->linesize[p]); \
765 type *dst = (type *)(out->data[p] + y * out->linesize[p]); \
767 dst[x] = mix(xf1[x], xf0[x], smoothstep(0.f, 1.f, smooth)); \
773 SMOOTHUP_TRANSITION(8, uint8_t, 1)
774 SMOOTHUP_TRANSITION(16, uint16_t, 2)
776 #define SMOOTHDOWN_TRANSITION(name, type, div) \
777 static void smoothdown##name##_transition(AVFilterContext *ctx, \
778 const AVFrame *a, const AVFrame *b, AVFrame *out, \
780 int slice_start, int slice_end, int jobnr) \
782 XFadeContext *s = ctx->priv; \
783 const int width = out->width; \
784 const float h = out->height; \
786 for (int y = slice_start; y < slice_end; y++) { \
787 const float smooth = 1.f + (h - 1 - y) / h - progress * 2.f; \
788 for (int x = 0; x < width; x++) { \
789 for (int p = 0; p < s->nb_planes; p++) { \
790 const type *xf0 = (const type *)(a->data[p] + y * a->linesize[p]); \
791 const type *xf1 = (const type *)(b->data[p] + y * b->linesize[p]); \
792 type *dst = (type *)(out->data[p] + y * out->linesize[p]); \
794 dst[x] = mix(xf1[x], xf0[x], smoothstep(0.f, 1.f, smooth)); \
800 SMOOTHDOWN_TRANSITION(8, uint8_t, 1)
801 SMOOTHDOWN_TRANSITION(16, uint16_t, 2)
803 #define CIRCLEOPEN_TRANSITION(name, type, div) \
804 static void circleopen##name##_transition(AVFilterContext *ctx, \
805 const AVFrame *a, const AVFrame *b, AVFrame *out, \
807 int slice_start, int slice_end, int jobnr) \
809 XFadeContext *s = ctx->priv; \
810 const int width = out->width; \
811 const int height = out->height; \
812 const float z = hypotf(width / 2, height / 2); \
813 const float p = (progress - 0.5f) * 3.f; \
815 for (int y = slice_start; y < slice_end; y++) { \
816 for (int x = 0; x < width; x++) { \
817 const float smooth = hypotf(x - width / 2, y - height / 2) / z + p; \
818 for (int p = 0; p < s->nb_planes; p++) { \
819 const type *xf0 = (const type *)(a->data[p] + y * a->linesize[p]); \
820 const type *xf1 = (const type *)(b->data[p] + y * b->linesize[p]); \
821 type *dst = (type *)(out->data[p] + y * out->linesize[p]); \
823 dst[x] = mix(xf0[x], xf1[x], smoothstep(0.f, 1.f, smooth)); \
829 CIRCLEOPEN_TRANSITION(8, uint8_t, 1)
830 CIRCLEOPEN_TRANSITION(16, uint16_t, 2)
832 #define CIRCLECLOSE_TRANSITION(name, type, div) \
833 static void circleclose##name##_transition(AVFilterContext *ctx, \
834 const AVFrame *a, const AVFrame *b, AVFrame *out, \
836 int slice_start, int slice_end, int jobnr) \
838 XFadeContext *s = ctx->priv; \
839 const int width = out->width; \
840 const int height = out->height; \
841 const float z = hypotf(width / 2, height / 2); \
842 const float p = (1.f - progress - 0.5f) * 3.f; \
844 for (int y = slice_start; y < slice_end; y++) { \
845 for (int x = 0; x < width; x++) { \
846 const float smooth = hypotf(x - width / 2, y - height / 2) / z + p; \
847 for (int p = 0; p < s->nb_planes; p++) { \
848 const type *xf0 = (const type *)(a->data[p] + y * a->linesize[p]); \
849 const type *xf1 = (const type *)(b->data[p] + y * b->linesize[p]); \
850 type *dst = (type *)(out->data[p] + y * out->linesize[p]); \
852 dst[x] = mix(xf1[x], xf0[x], smoothstep(0.f, 1.f, smooth)); \
858 CIRCLECLOSE_TRANSITION(8, uint8_t, 1)
859 CIRCLECLOSE_TRANSITION(16, uint16_t, 2)
861 static inline double getpix(void *priv, double x, double y, int plane, int nb)
863 XFadeContext *s = priv;
864 AVFrame *in = s->xf[nb];
865 const uint8_t *src = in->data[FFMIN(plane, s->nb_planes - 1)];
866 int linesize = in->linesize[FFMIN(plane, s->nb_planes - 1)];
867 const int w = in->width;
868 const int h = in->height;
872 xi = av_clipd(x, 0, w - 1);
873 yi = av_clipd(y, 0, h - 1);
876 const uint16_t *src16 = (const uint16_t*)src;
879 return src16[xi + yi * linesize];
881 return src[xi + yi * linesize];
885 static double a0(void *priv, double x, double y) { return getpix(priv, x, y, 0, 0); }
886 static double a1(void *priv, double x, double y) { return getpix(priv, x, y, 1, 0); }
887 static double a2(void *priv, double x, double y) { return getpix(priv, x, y, 2, 0); }
888 static double a3(void *priv, double x, double y) { return getpix(priv, x, y, 3, 0); }
890 static double b0(void *priv, double x, double y) { return getpix(priv, x, y, 0, 1); }
891 static double b1(void *priv, double x, double y) { return getpix(priv, x, y, 1, 1); }
892 static double b2(void *priv, double x, double y) { return getpix(priv, x, y, 2, 1); }
893 static double b3(void *priv, double x, double y) { return getpix(priv, x, y, 3, 1); }
895 static int config_output(AVFilterLink *outlink)
897 AVFilterContext *ctx = outlink->src;
898 AVFilterLink *inlink0 = ctx->inputs[0];
899 AVFilterLink *inlink1 = ctx->inputs[1];
900 XFadeContext *s = ctx->priv;
901 const AVPixFmtDescriptor *pix_desc = av_pix_fmt_desc_get(inlink0->format);
904 if (inlink0->format != inlink1->format) {
905 av_log(ctx, AV_LOG_ERROR, "inputs must be of same pixel format\n");
906 return AVERROR(EINVAL);
908 if (inlink0->w != inlink1->w || inlink0->h != inlink1->h) {
909 av_log(ctx, AV_LOG_ERROR, "First input link %s parameters "
910 "(size %dx%d) do not match the corresponding "
911 "second input link %s parameters (size %dx%d)\n",
912 ctx->input_pads[0].name, inlink0->w, inlink0->h,
913 ctx->input_pads[1].name, inlink1->w, inlink1->h);
914 return AVERROR(EINVAL);
917 if (inlink0->time_base.num != inlink1->time_base.num ||
918 inlink0->time_base.den != inlink1->time_base.den) {
919 av_log(ctx, AV_LOG_ERROR, "First input link %s timebase "
920 "(%d/%d) do not match the corresponding "
921 "second input link %s timebase (%d/%d)\n",
922 ctx->input_pads[0].name, inlink0->time_base.num, inlink0->time_base.den,
923 ctx->input_pads[1].name, inlink1->time_base.num, inlink1->time_base.den);
924 return AVERROR(EINVAL);
927 outlink->w = inlink0->w;
928 outlink->h = inlink0->h;
929 outlink->time_base = inlink0->time_base;
930 outlink->sample_aspect_ratio = inlink0->sample_aspect_ratio;
931 outlink->frame_rate = inlink0->frame_rate;
933 s->depth = pix_desc->comp[0].depth;
934 is_rgb = !!(pix_desc->flags & AV_PIX_FMT_FLAG_RGB);
935 s->nb_planes = av_pix_fmt_count_planes(inlink0->format);
936 s->max_value = (1 << s->depth) - 1;
938 s->black[1] = s->black[2] = is_rgb ? 0 : s->max_value / 2;
939 s->black[3] = s->max_value;
940 s->white[0] = s->white[3] = s->max_value;
941 s->white[1] = s->white[2] = is_rgb ? s->max_value : s->max_value / 2;
943 s->first_pts = s->last_pts = s->pts = AV_NOPTS_VALUE;
946 s->duration_pts = av_rescale_q(s->duration, AV_TIME_BASE_Q, outlink->time_base);
948 s->offset_pts = av_rescale_q(s->offset, AV_TIME_BASE_Q, outlink->time_base);
950 switch (s->transition) {
951 case CUSTOM: s->transitionf = s->depth <= 8 ? custom8_transition : custom16_transition; break;
952 case FADE: s->transitionf = s->depth <= 8 ? fade8_transition : fade16_transition; break;
953 case WIPELEFT: s->transitionf = s->depth <= 8 ? wipeleft8_transition : wipeleft16_transition; break;
954 case WIPERIGHT: s->transitionf = s->depth <= 8 ? wiperight8_transition : wiperight16_transition; break;
955 case WIPEUP: s->transitionf = s->depth <= 8 ? wipeup8_transition : wipeup16_transition; break;
956 case WIPEDOWN: s->transitionf = s->depth <= 8 ? wipedown8_transition : wipedown16_transition; break;
957 case SLIDELEFT: s->transitionf = s->depth <= 8 ? slideleft8_transition : slideleft16_transition; break;
958 case SLIDERIGHT: s->transitionf = s->depth <= 8 ? slideright8_transition : slideright16_transition; break;
959 case SLIDEUP: s->transitionf = s->depth <= 8 ? slideup8_transition : slideup16_transition; break;
960 case SLIDEDOWN: s->transitionf = s->depth <= 8 ? slidedown8_transition : slidedown16_transition; break;
961 case CIRCLECROP: s->transitionf = s->depth <= 8 ? circlecrop8_transition : circlecrop16_transition; break;
962 case RECTCROP: s->transitionf = s->depth <= 8 ? rectcrop8_transition : rectcrop16_transition; break;
963 case DISTANCE: s->transitionf = s->depth <= 8 ? distance8_transition : distance16_transition; break;
964 case FADEBLACK: s->transitionf = s->depth <= 8 ? fadeblack8_transition : fadeblack16_transition; break;
965 case FADEWHITE: s->transitionf = s->depth <= 8 ? fadewhite8_transition : fadewhite16_transition; break;
966 case RADIAL: s->transitionf = s->depth <= 8 ? radial8_transition : radial16_transition; break;
967 case SMOOTHLEFT: s->transitionf = s->depth <= 8 ? smoothleft8_transition : smoothleft16_transition; break;
968 case SMOOTHRIGHT:s->transitionf = s->depth <= 8 ? smoothright8_transition: smoothright16_transition;break;
969 case SMOOTHUP: s->transitionf = s->depth <= 8 ? smoothup8_transition : smoothup16_transition; break;
970 case SMOOTHDOWN: s->transitionf = s->depth <= 8 ? smoothdown8_transition : smoothdown16_transition; break;
971 case CIRCLEOPEN: s->transitionf = s->depth <= 8 ? circleopen8_transition : circleopen16_transition; break;
972 case CIRCLECLOSE:s->transitionf = s->depth <= 8 ? circleclose8_transition: circleclose16_transition;break;
975 if (s->transition == CUSTOM) {
976 static const char *const func2_names[] = {
977 "a0", "a1", "a2", "a3",
978 "b0", "b1", "b2", "b3",
981 double (*func2[])(void *, double, double) = {
988 return AVERROR(EINVAL);
989 ret = av_expr_parse(&s->e, s->custom_str, var_names,
990 NULL, NULL, func2_names, func2, 0, ctx);
998 static int xfade_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
1000 XFadeContext *s = ctx->priv;
1001 AVFilterLink *outlink = ctx->outputs[0];
1002 ThreadData *td = arg;
1003 int slice_start = (outlink->h * jobnr ) / nb_jobs;
1004 int slice_end = (outlink->h * (jobnr+1)) / nb_jobs;
1006 s->transitionf(ctx, td->xf[0], td->xf[1], td->out, td->progress, slice_start, slice_end, jobnr);
1011 static int xfade_frame(AVFilterContext *ctx, AVFrame *a, AVFrame *b)
1013 XFadeContext *s = ctx->priv;
1014 AVFilterLink *outlink = ctx->outputs[0];
1015 float progress = av_clipf(1.f - ((float)(s->pts - s->first_pts - s->offset_pts) / s->duration_pts), 0.f, 1.f);
1019 out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
1021 return AVERROR(ENOMEM);
1023 td.xf[0] = a, td.xf[1] = b, td.out = out, td.progress = progress;
1024 ctx->internal->execute(ctx, xfade_slice, &td, NULL, FFMIN(outlink->h, ff_filter_get_nb_threads(ctx)));
1028 return ff_filter_frame(outlink, out);
1031 static int xfade_activate(AVFilterContext *ctx)
1033 XFadeContext *s = ctx->priv;
1034 AVFilterLink *outlink = ctx->outputs[0];
1036 int ret = 0, status;
1039 FF_FILTER_FORWARD_STATUS_BACK_ALL(outlink, ctx);
1041 if (s->xfade_is_over) {
1042 ret = ff_inlink_consume_frame(ctx->inputs[1], &in);
1045 } else if (ret > 0) {
1046 in->pts = (in->pts - s->last_pts) + s->pts;
1047 return ff_filter_frame(outlink, in);
1048 } else if (ff_inlink_acknowledge_status(ctx->inputs[1], &status, &pts)) {
1049 ff_outlink_set_status(outlink, status, s->pts);
1052 if (ff_outlink_frame_wanted(outlink)) {
1053 ff_inlink_request_frame(ctx->inputs[1]);
1059 if (ff_inlink_queued_frames(ctx->inputs[0]) > 0) {
1060 s->xf[0] = ff_inlink_peek_frame(ctx->inputs[0], 0);
1062 if (s->first_pts == AV_NOPTS_VALUE) {
1063 s->first_pts = s->xf[0]->pts;
1065 s->pts = s->xf[0]->pts;
1066 if (s->first_pts + s->offset_pts > s->xf[0]->pts) {
1069 ff_inlink_consume_frame(ctx->inputs[0], &in);
1070 return ff_filter_frame(outlink, in);
1077 if (s->xf[0] && ff_inlink_queued_frames(ctx->inputs[1]) > 0) {
1078 ff_inlink_consume_frame(ctx->inputs[0], &s->xf[0]);
1079 ff_inlink_consume_frame(ctx->inputs[1], &s->xf[1]);
1081 s->last_pts = s->xf[1]->pts;
1082 s->pts = s->xf[0]->pts;
1083 if (s->xf[0]->pts - (s->first_pts + s->offset_pts) > s->duration_pts)
1084 s->xfade_is_over = 1;
1085 ret = xfade_frame(ctx, s->xf[0], s->xf[1]);
1086 av_frame_free(&s->xf[0]);
1087 av_frame_free(&s->xf[1]);
1091 if (ff_inlink_queued_frames(ctx->inputs[0]) > 0 &&
1092 ff_inlink_queued_frames(ctx->inputs[1]) > 0) {
1093 ff_filter_set_ready(ctx, 100);
1097 if (ff_outlink_frame_wanted(outlink)) {
1098 if (!s->eof[0] && ff_outlink_get_status(ctx->inputs[0])) {
1100 s->xfade_is_over = 1;
1102 if (!s->eof[1] && ff_outlink_get_status(ctx->inputs[1])) {
1105 if (!s->eof[0] && !s->xf[0])
1106 ff_inlink_request_frame(ctx->inputs[0]);
1107 if (!s->eof[1] && (s->need_second || s->eof[0]))
1108 ff_inlink_request_frame(ctx->inputs[1]);
1109 if (s->eof[0] && s->eof[1] && (
1110 ff_inlink_queued_frames(ctx->inputs[0]) <= 0 ||
1111 ff_inlink_queued_frames(ctx->inputs[1]) <= 0))
1112 ff_outlink_set_status(outlink, AVERROR_EOF, AV_NOPTS_VALUE);
1116 return FFERROR_NOT_READY;
1119 static const AVFilterPad xfade_inputs[] = {
1122 .type = AVMEDIA_TYPE_VIDEO,
1126 .type = AVMEDIA_TYPE_VIDEO,
1131 static const AVFilterPad xfade_outputs[] = {
1134 .type = AVMEDIA_TYPE_VIDEO,
1135 .config_props = config_output,
1140 AVFilter ff_vf_xfade = {
1142 .description = NULL_IF_CONFIG_SMALL("Cross fade one video with another video."),
1143 .priv_size = sizeof(XFadeContext),
1144 .priv_class = &xfade_class,
1145 .query_formats = query_formats,
1146 .activate = xfade_activate,
1148 .inputs = xfade_inputs,
1149 .outputs = xfade_outputs,
1150 .flags = AVFILTER_FLAG_SLICE_THREADS,