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"
27 #include "framesync.h"
32 enum XFadeTransitions {
52 typedef struct XFadeContext {
76 void (*transitionf)(AVFilterContext *ctx, const AVFrame *a, const AVFrame *b, AVFrame *out, float progress,
77 int slice_start, int slice_end, int jobnr);
82 static const char *const var_names[] = { "X", "Y", "W", "H", "A", "B", "PLANE", "P", NULL };
83 enum { VAR_X, VAR_Y, VAR_W, VAR_H, VAR_A, VAR_B, VAR_PLANE, VAR_PROGRESS, VAR_VARS_NB };
85 typedef struct ThreadData {
91 static int query_formats(AVFilterContext *ctx)
93 static const enum AVPixelFormat pix_fmts[] = {
97 AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRAP, AV_PIX_FMT_GRAY8,
98 AV_PIX_FMT_YUVA444P9, AV_PIX_FMT_GBRP9,
100 AV_PIX_FMT_YUVA444P10,
101 AV_PIX_FMT_GBRP10, AV_PIX_FMT_GBRAP10, AV_PIX_FMT_GRAY10,
102 AV_PIX_FMT_YUV444P12,
103 AV_PIX_FMT_YUVA444P12,
104 AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRAP12, AV_PIX_FMT_GRAY12,
105 AV_PIX_FMT_YUV444P14, AV_PIX_FMT_GBRP14,
106 AV_PIX_FMT_YUV444P16,
107 AV_PIX_FMT_YUVA444P16,
108 AV_PIX_FMT_GBRP16, AV_PIX_FMT_GBRAP16, AV_PIX_FMT_GRAY16,
112 AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
114 return AVERROR(ENOMEM);
115 return ff_set_common_formats(ctx, fmts_list);
118 static av_cold void uninit(AVFilterContext *ctx)
120 XFadeContext *s = ctx->priv;
125 #define OFFSET(x) offsetof(XFadeContext, x)
126 #define FLAGS (AV_OPT_FLAG_FILTERING_PARAM | AV_OPT_FLAG_VIDEO_PARAM)
128 static const AVOption xfade_options[] = {
129 { "transition", "set cross fade transition", OFFSET(transition), AV_OPT_TYPE_INT, {.i64=FADE}, -1, NB_TRANSITIONS-1, FLAGS, "transition" },
130 { "custom", "custom transition", 0, AV_OPT_TYPE_CONST, {.i64=CUSTOM}, 0, 0, FLAGS, "transition" },
131 { "fade", "fade transition", 0, AV_OPT_TYPE_CONST, {.i64=FADE}, 0, 0, FLAGS, "transition" },
132 { "wipeleft", "wipe left transition", 0, AV_OPT_TYPE_CONST, {.i64=WIPELEFT}, 0, 0, FLAGS, "transition" },
133 { "wiperight", "wipe right transition", 0, AV_OPT_TYPE_CONST, {.i64=WIPERIGHT}, 0, 0, FLAGS, "transition" },
134 { "wipeup", "wipe up transition", 0, AV_OPT_TYPE_CONST, {.i64=WIPEUP}, 0, 0, FLAGS, "transition" },
135 { "wipedown", "wipe down transition", 0, AV_OPT_TYPE_CONST, {.i64=WIPEDOWN}, 0, 0, FLAGS, "transition" },
136 { "slideleft", "slide left transition", 0, AV_OPT_TYPE_CONST, {.i64=SLIDELEFT}, 0, 0, FLAGS, "transition" },
137 { "slideright", "slide right transition", 0, AV_OPT_TYPE_CONST, {.i64=SLIDERIGHT}, 0, 0, FLAGS, "transition" },
138 { "slideup", "slide up transition", 0, AV_OPT_TYPE_CONST, {.i64=SLIDEUP}, 0, 0, FLAGS, "transition" },
139 { "slidedown", "slide down transition", 0, AV_OPT_TYPE_CONST, {.i64=SLIDEDOWN}, 0, 0, FLAGS, "transition" },
140 { "circlecrop", "circle crop transition", 0, AV_OPT_TYPE_CONST, {.i64=CIRCLECROP}, 0, 0, FLAGS, "transition" },
141 { "rectcrop", "rect crop transition", 0, AV_OPT_TYPE_CONST, {.i64=RECTCROP}, 0, 0, FLAGS, "transition" },
142 { "distance", "distance transition", 0, AV_OPT_TYPE_CONST, {.i64=DISTANCE}, 0, 0, FLAGS, "transition" },
143 { "fadeblack", "fadeblack transition", 0, AV_OPT_TYPE_CONST, {.i64=FADEBLACK}, 0, 0, FLAGS, "transition" },
144 { "fadewhite", "fadewhite transition", 0, AV_OPT_TYPE_CONST, {.i64=FADEWHITE}, 0, 0, FLAGS, "transition" },
145 { "radial", "radial transition", 0, AV_OPT_TYPE_CONST, {.i64=RADIAL}, 0, 0, FLAGS, "transition" },
146 { "duration", "set cross fade duration", OFFSET(duration), AV_OPT_TYPE_DURATION, {.i64=1000000}, 0, 60000000, FLAGS },
147 { "offset", "set cross fade start relative to first input stream", OFFSET(offset), AV_OPT_TYPE_DURATION, {.i64=0}, INT64_MIN, INT64_MAX, FLAGS },
148 { "expr", "set expression for custom transition", OFFSET(custom_str), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, FLAGS },
152 AVFILTER_DEFINE_CLASS(xfade);
154 #define CUSTOM_TRANSITION(name, type, div) \
155 static void custom##name##_transition(AVFilterContext *ctx, \
156 const AVFrame *a, const AVFrame *b, AVFrame *out, \
158 int slice_start, int slice_end, int jobnr) \
160 XFadeContext *s = ctx->priv; \
161 const int height = slice_end - slice_start; \
163 double values[VAR_VARS_NB]; \
164 values[VAR_W] = out->width; \
165 values[VAR_H] = out->height; \
166 values[VAR_PROGRESS] = progress; \
168 for (int p = 0; p < s->nb_planes; p++) { \
169 const type *xf0 = (const type *)(a->data[p] + slice_start * a->linesize[p]); \
170 const type *xf1 = (const type *)(b->data[p] + slice_start * b->linesize[p]); \
171 type *dst = (type *)(out->data[p] + slice_start * out->linesize[p]); \
173 values[VAR_PLANE] = p; \
175 for (int y = 0; y < height; y++) { \
176 values[VAR_Y] = slice_start + y; \
177 for (int x = 0; x < out->width; x++) { \
179 values[VAR_A] = xf0[x]; \
180 values[VAR_B] = xf1[x]; \
181 dst[x] = av_expr_eval(s->e, values, s); \
184 dst += out->linesize[p] / div; \
185 xf0 += a->linesize[p] / div; \
186 xf1 += b->linesize[p] / div; \
191 CUSTOM_TRANSITION(8, uint8_t, 1)
192 CUSTOM_TRANSITION(16, uint16_t, 2)
194 static inline float mix(float a, float b, float mix)
196 return a * mix + b * (1.f - mix);
199 static inline float smoothstep(float edge0, float edge1, float x)
203 t = av_clipf((x - edge0) / (edge1 - edge0), 0.f, 1.f);
205 return t * t * (3.f - 2.f * t);
208 #define FADE_TRANSITION(name, type, div) \
209 static void fade##name##_transition(AVFilterContext *ctx, \
210 const AVFrame *a, const AVFrame *b, AVFrame *out, \
212 int slice_start, int slice_end, int jobnr) \
214 XFadeContext *s = ctx->priv; \
215 const int height = slice_end - slice_start; \
217 for (int p = 0; p < s->nb_planes; p++) { \
218 const type *xf0 = (const type *)(a->data[p] + slice_start * a->linesize[p]); \
219 const type *xf1 = (const type *)(b->data[p] + slice_start * b->linesize[p]); \
220 type *dst = (type *)(out->data[p] + slice_start * out->linesize[p]); \
222 for (int y = 0; y < height; y++) { \
223 for (int x = 0; x < out->width; x++) { \
224 dst[x] = mix(xf0[x], xf1[x], progress); \
227 dst += out->linesize[p] / div; \
228 xf0 += a->linesize[p] / div; \
229 xf1 += b->linesize[p] / div; \
234 FADE_TRANSITION(8, uint8_t, 1)
235 FADE_TRANSITION(16, uint16_t, 2)
237 #define WIPELEFT_TRANSITION(name, type, div) \
238 static void wipeleft##name##_transition(AVFilterContext *ctx, \
239 const AVFrame *a, const AVFrame *b, AVFrame *out, \
241 int slice_start, int slice_end, int jobnr) \
243 XFadeContext *s = ctx->priv; \
244 const int height = slice_end - slice_start; \
245 const int z = out->width * progress; \
247 for (int p = 0; p < s->nb_planes; p++) { \
248 const type *xf0 = (const type *)(a->data[p] + slice_start * a->linesize[p]); \
249 const type *xf1 = (const type *)(b->data[p] + slice_start * b->linesize[p]); \
250 type *dst = (type *)(out->data[p] + slice_start * out->linesize[p]); \
252 for (int y = 0; y < height; y++) { \
253 for (int x = 0; x < out->width; x++) { \
254 dst[x] = x > z ? xf1[x] : xf0[x]; \
257 dst += out->linesize[p] / div; \
258 xf0 += a->linesize[p] / div; \
259 xf1 += b->linesize[p] / div; \
264 WIPELEFT_TRANSITION(8, uint8_t, 1)
265 WIPELEFT_TRANSITION(16, uint16_t, 2)
267 #define WIPERIGHT_TRANSITION(name, type, div) \
268 static void wiperight##name##_transition(AVFilterContext *ctx, \
269 const AVFrame *a, const AVFrame *b, AVFrame *out, \
271 int slice_start, int slice_end, int jobnr) \
273 XFadeContext *s = ctx->priv; \
274 const int height = slice_end - slice_start; \
275 const int z = out->width * (1.f - progress); \
277 for (int p = 0; p < s->nb_planes; p++) { \
278 const type *xf0 = (const type *)(a->data[p] + slice_start * a->linesize[p]); \
279 const type *xf1 = (const type *)(b->data[p] + slice_start * b->linesize[p]); \
280 type *dst = (type *)(out->data[p] + slice_start * out->linesize[p]); \
282 for (int y = 0; y < height; y++) { \
283 for (int x = 0; x < out->width; x++) { \
284 dst[x] = x > z ? xf0[x] : xf1[x]; \
287 dst += out->linesize[p] / div; \
288 xf0 += a->linesize[p] / div; \
289 xf1 += b->linesize[p] / div; \
294 WIPERIGHT_TRANSITION(8, uint8_t, 1)
295 WIPERIGHT_TRANSITION(16, uint16_t, 2)
297 #define WIPEUP_TRANSITION(name, type, div) \
298 static void wipeup##name##_transition(AVFilterContext *ctx, \
299 const AVFrame *a, const AVFrame *b, AVFrame *out, \
301 int slice_start, int slice_end, int jobnr) \
303 XFadeContext *s = ctx->priv; \
304 const int height = slice_end - slice_start; \
305 const int z = out->height * progress; \
307 for (int p = 0; p < s->nb_planes; p++) { \
308 const type *xf0 = (const type *)(a->data[p] + slice_start * a->linesize[p]); \
309 const type *xf1 = (const type *)(b->data[p] + slice_start * b->linesize[p]); \
310 type *dst = (type *)(out->data[p] + slice_start * out->linesize[p]); \
312 for (int y = 0; y < height; y++) { \
313 for (int x = 0; x < out->width; x++) { \
314 dst[x] = slice_start + y > z ? xf1[x] : xf0[x]; \
317 dst += out->linesize[p] / div; \
318 xf0 += a->linesize[p] / div; \
319 xf1 += b->linesize[p] / div; \
324 WIPEUP_TRANSITION(8, uint8_t, 1)
325 WIPEUP_TRANSITION(16, uint16_t, 2)
327 #define WIPEDOWN_TRANSITION(name, type, div) \
328 static void wipedown##name##_transition(AVFilterContext *ctx, \
329 const AVFrame *a, const AVFrame *b, AVFrame *out, \
331 int slice_start, int slice_end, int jobnr) \
333 XFadeContext *s = ctx->priv; \
334 const int height = slice_end - slice_start; \
335 const int z = out->height * (1.f - progress); \
337 for (int p = 0; p < s->nb_planes; p++) { \
338 const type *xf0 = (const type *)(a->data[p] + slice_start * a->linesize[p]); \
339 const type *xf1 = (const type *)(b->data[p] + slice_start * b->linesize[p]); \
340 type *dst = (type *)(out->data[p] + slice_start * out->linesize[p]); \
342 for (int y = 0; y < height; y++) { \
343 for (int x = 0; x < out->width; x++) { \
344 dst[x] = slice_start + y > z ? xf0[x] : xf1[x]; \
347 dst += out->linesize[p] / div; \
348 xf0 += a->linesize[p] / div; \
349 xf1 += b->linesize[p] / div; \
354 WIPEDOWN_TRANSITION(8, uint8_t, 1)
355 WIPEDOWN_TRANSITION(16, uint16_t, 2)
357 #define SLIDELEFT_TRANSITION(name, type, div) \
358 static void slideleft##name##_transition(AVFilterContext *ctx, \
359 const AVFrame *a, const AVFrame *b, AVFrame *out, \
361 int slice_start, int slice_end, int jobnr) \
363 XFadeContext *s = ctx->priv; \
364 const int height = slice_end - slice_start; \
365 const int width = out->width; \
366 const int z = -progress * width; \
368 for (int p = 0; p < s->nb_planes; p++) { \
369 const type *xf0 = (const type *)(a->data[p] + slice_start * a->linesize[p]); \
370 const type *xf1 = (const type *)(b->data[p] + slice_start * b->linesize[p]); \
371 type *dst = (type *)(out->data[p] + slice_start * out->linesize[p]); \
373 for (int y = 0; y < height; y++) { \
374 for (int x = 0; x < width; x++) { \
375 const int zx = z + x; \
376 const int zz = zx % width + width * (zx < 0); \
377 dst[x] = (zx > 0) && (zx < width) ? xf1[zz] : xf0[zz]; \
380 dst += out->linesize[p] / div; \
381 xf0 += a->linesize[p] / div; \
382 xf1 += b->linesize[p] / div; \
387 SLIDELEFT_TRANSITION(8, uint8_t, 1)
388 SLIDELEFT_TRANSITION(16, uint16_t, 2)
390 #define SLIDERIGHT_TRANSITION(name, type, div) \
391 static void slideright##name##_transition(AVFilterContext *ctx, \
392 const AVFrame *a, const AVFrame *b, AVFrame *out, \
394 int slice_start, int slice_end, int jobnr) \
396 XFadeContext *s = ctx->priv; \
397 const int height = slice_end - slice_start; \
398 const int width = out->width; \
399 const int z = progress * width; \
401 for (int p = 0; p < s->nb_planes; p++) { \
402 const type *xf0 = (const type *)(a->data[p] + slice_start * a->linesize[p]); \
403 const type *xf1 = (const type *)(b->data[p] + slice_start * b->linesize[p]); \
404 type *dst = (type *)(out->data[p] + slice_start * out->linesize[p]); \
406 for (int y = 0; y < height; y++) { \
407 for (int x = 0; x < out->width; x++) { \
408 const int zx = z + x; \
409 const int zz = zx % width + width * (zx < 0); \
410 dst[x] = (zx > 0) && (zx < width) ? xf1[zz] : xf0[zz]; \
413 dst += out->linesize[p] / div; \
414 xf0 += a->linesize[p] / div; \
415 xf1 += b->linesize[p] / div; \
420 SLIDERIGHT_TRANSITION(8, uint8_t, 1)
421 SLIDERIGHT_TRANSITION(16, uint16_t, 2)
423 #define SLIDEUP_TRANSITION(name, type, div) \
424 static void slideup##name##_transition(AVFilterContext *ctx, \
425 const AVFrame *a, const AVFrame *b, AVFrame *out, \
427 int slice_start, int slice_end, int jobnr) \
429 XFadeContext *s = ctx->priv; \
430 const int height = out->height; \
431 const int z = -progress * height; \
433 for (int p = 0; p < s->nb_planes; p++) { \
434 type *dst = (type *)(out->data[p] + slice_start * out->linesize[p]); \
436 for (int y = slice_start; y < slice_end; y++) { \
437 const int zy = z + y; \
438 const int zz = zy % height + height * (zy < 0); \
439 const type *xf0 = (const type *)(a->data[p] + zz * a->linesize[p]); \
440 const type *xf1 = (const type *)(b->data[p] + zz * b->linesize[p]); \
442 for (int x = 0; x < out->width; x++) { \
443 dst[x] = (zy > 0) && (zy < height) ? xf1[x] : xf0[x]; \
446 dst += out->linesize[p] / div; \
451 SLIDEUP_TRANSITION(8, uint8_t, 1)
452 SLIDEUP_TRANSITION(16, uint16_t, 2)
454 #define SLIDEDOWN_TRANSITION(name, type, div) \
455 static void slidedown##name##_transition(AVFilterContext *ctx, \
456 const AVFrame *a, const AVFrame *b, AVFrame *out, \
458 int slice_start, int slice_end, int jobnr) \
460 XFadeContext *s = ctx->priv; \
461 const int height = out->height; \
462 const int z = progress * height; \
464 for (int p = 0; p < s->nb_planes; p++) { \
465 type *dst = (type *)(out->data[p] + slice_start * out->linesize[p]); \
467 for (int y = slice_start; y < slice_end; y++) { \
468 const int zy = z + y; \
469 const int zz = zy % height + height * (zy < 0); \
470 const type *xf0 = (const type *)(a->data[p] + zz * a->linesize[p]); \
471 const type *xf1 = (const type *)(b->data[p] + zz * b->linesize[p]); \
473 for (int x = 0; x < out->width; x++) { \
474 dst[x] = (zy > 0) && (zy < height) ? xf1[x] : xf0[x]; \
477 dst += out->linesize[p] / div; \
482 SLIDEDOWN_TRANSITION(8, uint8_t, 1)
483 SLIDEDOWN_TRANSITION(16, uint16_t, 2)
485 #define CIRCLECROP_TRANSITION(name, type, div) \
486 static void circlecrop##name##_transition(AVFilterContext *ctx, \
487 const AVFrame *a, const AVFrame *b, AVFrame *out, \
489 int slice_start, int slice_end, int jobnr) \
491 XFadeContext *s = ctx->priv; \
492 const int width = out->width; \
493 const int height = out->height; \
494 float z = powf(2.f * fabsf(progress - 0.5f), 3.f) * hypotf(width/2, height/2); \
496 for (int p = 0; p < s->nb_planes; p++) { \
497 const int bg = s->black[p]; \
498 type *dst = (type *)(out->data[p] + slice_start * out->linesize[p]); \
500 for (int y = slice_start; y < slice_end; y++) { \
501 const type *xf0 = (const type *)(a->data[p] + y * a->linesize[p]); \
502 const type *xf1 = (const type *)(b->data[p] + y * b->linesize[p]); \
504 for (int x = 0; x < width; x++) { \
505 float dist = hypotf(x - width / 2, y - height / 2); \
506 int val = progress < 0.5f ? xf1[x] : xf0[x]; \
507 dst[x] = (z < dist) ? bg : val; \
510 dst += out->linesize[p] / div; \
515 CIRCLECROP_TRANSITION(8, uint8_t, 1)
516 CIRCLECROP_TRANSITION(16, uint16_t, 2)
518 #define RECTCROP_TRANSITION(name, type, div) \
519 static void rectcrop##name##_transition(AVFilterContext *ctx, \
520 const AVFrame *a, const AVFrame *b, AVFrame *out, \
522 int slice_start, int slice_end, int jobnr) \
524 XFadeContext *s = ctx->priv; \
525 const int width = out->width; \
526 const int height = out->height; \
527 int zh = fabsf(progress - 0.5f) * height; \
528 int zw = fabsf(progress - 0.5f) * width; \
530 for (int p = 0; p < s->nb_planes; p++) { \
531 const int bg = s->black[p]; \
532 type *dst = (type *)(out->data[p] + slice_start * out->linesize[p]); \
534 for (int y = slice_start; y < slice_end; y++) { \
535 const type *xf0 = (const type *)(a->data[p] + y * a->linesize[p]); \
536 const type *xf1 = (const type *)(b->data[p] + y * b->linesize[p]); \
538 for (int x = 0; x < width; x++) { \
539 int dist = FFABS(x - width / 2) < zw && \
540 FFABS(y - height / 2) < zh; \
541 int val = progress < 0.5f ? xf1[x] : xf0[x]; \
542 dst[x] = !dist ? bg : val; \
545 dst += out->linesize[p] / div; \
550 RECTCROP_TRANSITION(8, uint8_t, 1)
551 RECTCROP_TRANSITION(16, uint16_t, 2)
553 #define DISTANCE_TRANSITION(name, type, div) \
554 static void distance##name##_transition(AVFilterContext *ctx, \
555 const AVFrame *a, const AVFrame *b, AVFrame *out, \
557 int slice_start, int slice_end, int jobnr) \
559 XFadeContext *s = ctx->priv; \
560 const int width = out->width; \
561 const float max = s->max_value; \
563 for (int y = slice_start; y < slice_end; y++) { \
564 for (int x = 0; x < width; x++) { \
566 for (int p = 0; p < s->nb_planes; p++) { \
567 const type *xf0 = (const type *)(a->data[p] + y * a->linesize[p]); \
568 const type *xf1 = (const type *)(b->data[p] + y * b->linesize[p]); \
570 dist += (xf0[x] / max - xf1[x] / max) * \
571 (xf0[x] / max - xf1[x] / max); \
574 dist = sqrtf(dist) <= progress; \
575 for (int p = 0; p < s->nb_planes; p++) { \
576 const type *xf0 = (const type *)(a->data[p] + y * a->linesize[p]); \
577 const type *xf1 = (const type *)(b->data[p] + y * b->linesize[p]); \
578 type *dst = (type *)(out->data[p] + y * out->linesize[p]); \
579 dst[x] = mix(mix(xf0[x], xf1[x], dist), xf1[x], progress); \
585 DISTANCE_TRANSITION(8, uint8_t, 1)
586 DISTANCE_TRANSITION(16, uint16_t, 2)
588 #define FADEBLACK_TRANSITION(name, type, div) \
589 static void fadeblack##name##_transition(AVFilterContext *ctx, \
590 const AVFrame *a, const AVFrame *b, AVFrame *out, \
592 int slice_start, int slice_end, int jobnr) \
594 XFadeContext *s = ctx->priv; \
595 const int height = slice_end - slice_start; \
596 const float phase = 0.2f; \
598 for (int p = 0; p < s->nb_planes; p++) { \
599 const type *xf0 = (const type *)(a->data[p] + slice_start * a->linesize[p]); \
600 const type *xf1 = (const type *)(b->data[p] + slice_start * b->linesize[p]); \
601 type *dst = (type *)(out->data[p] + slice_start * out->linesize[p]); \
602 const int bg = s->black[p]; \
604 for (int y = 0; y < height; y++) { \
605 for (int x = 0; x < out->width; x++) { \
606 dst[x] = mix(mix(xf0[x], bg, smoothstep(1.f-phase, 1.f, progress)), \
607 mix(bg, xf1[x], smoothstep(phase, 1.f, progress)), \
611 dst += out->linesize[p] / div; \
612 xf0 += a->linesize[p] / div; \
613 xf1 += b->linesize[p] / div; \
618 FADEBLACK_TRANSITION(8, uint8_t, 1)
619 FADEBLACK_TRANSITION(16, uint16_t, 2)
621 #define FADEWHITE_TRANSITION(name, type, div) \
622 static void fadewhite##name##_transition(AVFilterContext *ctx, \
623 const AVFrame *a, const AVFrame *b, AVFrame *out, \
625 int slice_start, int slice_end, int jobnr) \
627 XFadeContext *s = ctx->priv; \
628 const int height = slice_end - slice_start; \
629 const float phase = 0.2f; \
631 for (int p = 0; p < s->nb_planes; p++) { \
632 const type *xf0 = (const type *)(a->data[p] + slice_start * a->linesize[p]); \
633 const type *xf1 = (const type *)(b->data[p] + slice_start * b->linesize[p]); \
634 type *dst = (type *)(out->data[p] + slice_start * out->linesize[p]); \
635 const int bg = s->white[p]; \
637 for (int y = 0; y < height; y++) { \
638 for (int x = 0; x < out->width; x++) { \
639 dst[x] = mix(mix(xf0[x], bg, smoothstep(1.f-phase, 1.f, progress)), \
640 mix(bg, xf1[x], smoothstep(phase, 1.f, progress)), \
644 dst += out->linesize[p] / div; \
645 xf0 += a->linesize[p] / div; \
646 xf1 += b->linesize[p] / div; \
651 FADEWHITE_TRANSITION(8, uint8_t, 1)
652 FADEWHITE_TRANSITION(16, uint16_t, 2)
654 #define RADIAL_TRANSITION(name, type, div) \
655 static void radial##name##_transition(AVFilterContext *ctx, \
656 const AVFrame *a, const AVFrame *b, AVFrame *out, \
658 int slice_start, int slice_end, int jobnr) \
660 XFadeContext *s = ctx->priv; \
661 const int width = out->width; \
662 const int height = out->height; \
664 for (int y = slice_start; y < slice_end; y++) { \
665 for (int x = 0; x < width; x++) { \
666 const float smooth = atan2f(x - width / 2, y - height / 2) - \
667 (progress - 0.5f) * (M_PI * 2.5f); \
668 for (int p = 0; p < s->nb_planes; p++) { \
669 const type *xf0 = (const type *)(a->data[p] + y * a->linesize[p]); \
670 const type *xf1 = (const type *)(b->data[p] + y * b->linesize[p]); \
671 type *dst = (type *)(out->data[p] + y * out->linesize[p]); \
673 dst[x] = mix(xf1[x], xf0[x], smoothstep(0.f, 1.f, smooth)); \
679 RADIAL_TRANSITION(8, uint8_t, 1)
680 RADIAL_TRANSITION(16, uint16_t, 2)
682 static inline double getpix(void *priv, double x, double y, int plane, int nb)
684 XFadeContext *s = priv;
685 AVFrame *in = s->xf[nb];
686 const uint8_t *src = in->data[FFMIN(plane, s->nb_planes - 1)];
687 int linesize = in->linesize[FFMIN(plane, s->nb_planes - 1)];
688 const int w = in->width;
689 const int h = in->height;
693 xi = av_clipd(x, 0, w - 1);
694 yi = av_clipd(y, 0, h - 1);
697 const uint16_t *src16 = (const uint16_t*)src;
700 return src16[xi + yi * linesize];
702 return src[xi + yi * linesize];
706 static double a0(void *priv, double x, double y) { return getpix(priv, x, y, 0, 0); }
707 static double a1(void *priv, double x, double y) { return getpix(priv, x, y, 1, 0); }
708 static double a2(void *priv, double x, double y) { return getpix(priv, x, y, 2, 0); }
709 static double a3(void *priv, double x, double y) { return getpix(priv, x, y, 3, 0); }
711 static double b0(void *priv, double x, double y) { return getpix(priv, x, y, 0, 1); }
712 static double b1(void *priv, double x, double y) { return getpix(priv, x, y, 1, 1); }
713 static double b2(void *priv, double x, double y) { return getpix(priv, x, y, 2, 1); }
714 static double b3(void *priv, double x, double y) { return getpix(priv, x, y, 3, 1); }
716 static int config_output(AVFilterLink *outlink)
718 AVFilterContext *ctx = outlink->src;
719 AVFilterLink *inlink0 = ctx->inputs[0];
720 AVFilterLink *inlink1 = ctx->inputs[1];
721 XFadeContext *s = ctx->priv;
722 const AVPixFmtDescriptor *pix_desc = av_pix_fmt_desc_get(inlink0->format);
725 if (inlink0->format != inlink1->format) {
726 av_log(ctx, AV_LOG_ERROR, "inputs must be of same pixel format\n");
727 return AVERROR(EINVAL);
729 if (inlink0->w != inlink1->w || inlink0->h != inlink1->h) {
730 av_log(ctx, AV_LOG_ERROR, "First input link %s parameters "
731 "(size %dx%d) do not match the corresponding "
732 "second input link %s parameters (size %dx%d)\n",
733 ctx->input_pads[0].name, inlink0->w, inlink0->h,
734 ctx->input_pads[1].name, inlink1->w, inlink1->h);
735 return AVERROR(EINVAL);
738 if (inlink0->time_base.num != inlink1->time_base.num ||
739 inlink0->time_base.den != inlink1->time_base.den) {
740 av_log(ctx, AV_LOG_ERROR, "First input link %s timebase "
741 "(%d/%d) do not match the corresponding "
742 "second input link %s timebase (%d/%d)\n",
743 ctx->input_pads[0].name, inlink0->time_base.num, inlink0->time_base.den,
744 ctx->input_pads[1].name, inlink1->time_base.num, inlink1->time_base.den);
745 return AVERROR(EINVAL);
748 outlink->w = inlink0->w;
749 outlink->h = inlink0->h;
750 outlink->time_base = inlink0->time_base;
751 outlink->sample_aspect_ratio = inlink0->sample_aspect_ratio;
752 outlink->frame_rate = inlink0->frame_rate;
754 s->depth = pix_desc->comp[0].depth;
755 is_rgb = !!(pix_desc->flags & AV_PIX_FMT_FLAG_RGB);
756 s->nb_planes = av_pix_fmt_count_planes(inlink0->format);
757 s->max_value = (1 << s->depth) - 1;
759 s->black[1] = s->black[2] = is_rgb ? 0 : s->max_value / 2;
760 s->black[3] = s->max_value;
761 s->white[0] = s->white[3] = s->max_value;
762 s->white[1] = s->white[2] = is_rgb ? s->max_value : s->max_value / 2;
764 s->first_pts = s->last_pts = s->pts = AV_NOPTS_VALUE;
767 s->duration_pts = av_rescale_q(s->duration, AV_TIME_BASE_Q, outlink->time_base);
769 s->offset_pts = av_rescale_q(s->offset, AV_TIME_BASE_Q, outlink->time_base);
771 switch (s->transition) {
772 case CUSTOM: s->transitionf = s->depth <= 8 ? custom8_transition : custom16_transition; break;
773 case FADE: s->transitionf = s->depth <= 8 ? fade8_transition : fade16_transition; break;
774 case WIPELEFT: s->transitionf = s->depth <= 8 ? wipeleft8_transition : wipeleft16_transition; break;
775 case WIPERIGHT: s->transitionf = s->depth <= 8 ? wiperight8_transition : wiperight16_transition; break;
776 case WIPEUP: s->transitionf = s->depth <= 8 ? wipeup8_transition : wipeup16_transition; break;
777 case WIPEDOWN: s->transitionf = s->depth <= 8 ? wipedown8_transition : wipedown16_transition; break;
778 case SLIDELEFT: s->transitionf = s->depth <= 8 ? slideleft8_transition : slideleft16_transition; break;
779 case SLIDERIGHT: s->transitionf = s->depth <= 8 ? slideright8_transition : slideright16_transition; break;
780 case SLIDEUP: s->transitionf = s->depth <= 8 ? slideup8_transition : slideup16_transition; break;
781 case SLIDEDOWN: s->transitionf = s->depth <= 8 ? slidedown8_transition : slidedown16_transition; break;
782 case CIRCLECROP: s->transitionf = s->depth <= 8 ? circlecrop8_transition : circlecrop16_transition; break;
783 case RECTCROP: s->transitionf = s->depth <= 8 ? rectcrop8_transition : rectcrop16_transition; break;
784 case DISTANCE: s->transitionf = s->depth <= 8 ? distance8_transition : distance16_transition; break;
785 case FADEBLACK: s->transitionf = s->depth <= 8 ? fadeblack8_transition : fadeblack16_transition; break;
786 case FADEWHITE: s->transitionf = s->depth <= 8 ? fadewhite8_transition : fadewhite16_transition; break;
787 case RADIAL: s->transitionf = s->depth <= 8 ? radial8_transition : radial16_transition; break;
790 if (s->transition == CUSTOM) {
791 static const char *const func2_names[] = {
792 "a0", "a1", "a2", "a3",
793 "b0", "b1", "b2", "b3",
796 double (*func2[])(void *, double, double) = {
803 return AVERROR(EINVAL);
804 ret = av_expr_parse(&s->e, s->custom_str, var_names,
805 NULL, NULL, func2_names, func2, 0, ctx);
813 static int xfade_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
815 XFadeContext *s = ctx->priv;
816 AVFilterLink *outlink = ctx->outputs[0];
817 ThreadData *td = arg;
818 int slice_start = (outlink->h * jobnr ) / nb_jobs;
819 int slice_end = (outlink->h * (jobnr+1)) / nb_jobs;
821 s->transitionf(ctx, td->xf[0], td->xf[1], td->out, td->progress, slice_start, slice_end, jobnr);
826 static int xfade_frame(AVFilterContext *ctx, AVFrame *a, AVFrame *b)
828 XFadeContext *s = ctx->priv;
829 AVFilterLink *outlink = ctx->outputs[0];
830 float progress = av_clipf(1.f - ((float)(s->pts - s->first_pts - s->offset_pts) / s->duration_pts), 0.f, 1.f);
834 out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
836 return AVERROR(ENOMEM);
838 td.xf[0] = a, td.xf[1] = b, td.out = out, td.progress = progress;
839 ctx->internal->execute(ctx, xfade_slice, &td, NULL, FFMIN(outlink->h, ff_filter_get_nb_threads(ctx)));
843 return ff_filter_frame(outlink, out);
846 static int xfade_activate(AVFilterContext *ctx)
848 XFadeContext *s = ctx->priv;
849 AVFilterLink *outlink = ctx->outputs[0];
854 FF_FILTER_FORWARD_STATUS_BACK_ALL(outlink, ctx);
856 if (s->xfade_is_over) {
857 ret = ff_inlink_consume_frame(ctx->inputs[1], &in);
860 } else if (ff_inlink_acknowledge_status(ctx->inputs[1], &status, &pts)) {
861 ff_outlink_set_status(outlink, status, s->pts);
864 if (ff_outlink_frame_wanted(outlink)) {
865 ff_inlink_request_frame(ctx->inputs[1]);
869 in->pts = (in->pts - s->last_pts) + s->pts;
870 return ff_filter_frame(outlink, in);
874 if (ff_inlink_queued_frames(ctx->inputs[0]) > 0) {
875 s->xf[0] = ff_inlink_peek_frame(ctx->inputs[0], 0);
877 if (s->first_pts == AV_NOPTS_VALUE) {
878 s->first_pts = s->xf[0]->pts;
880 s->pts = s->xf[0]->pts;
881 if (s->first_pts + s->offset_pts > s->xf[0]->pts) {
884 ff_inlink_consume_frame(ctx->inputs[0], &in);
885 return ff_filter_frame(outlink, in);
892 if (s->xf[0] && ff_inlink_queued_frames(ctx->inputs[1]) > 0) {
893 ff_inlink_consume_frame(ctx->inputs[0], &s->xf[0]);
894 ff_inlink_consume_frame(ctx->inputs[1], &s->xf[1]);
896 s->last_pts = s->xf[1]->pts;
897 s->pts = s->xf[0]->pts;
898 if (s->xf[0]->pts - (s->first_pts + s->offset_pts) > s->duration_pts)
899 s->xfade_is_over = 1;
900 ret = xfade_frame(ctx, s->xf[0], s->xf[1]);
901 av_frame_free(&s->xf[0]);
902 av_frame_free(&s->xf[1]);
906 if (ff_inlink_queued_frames(ctx->inputs[0]) > 0 &&
907 ff_inlink_queued_frames(ctx->inputs[1]) > 0) {
908 ff_filter_set_ready(ctx, 100);
912 if (ff_outlink_frame_wanted(outlink)) {
913 if (!s->eof[0] && ff_outlink_get_status(ctx->inputs[0])) {
915 s->xfade_is_over = 1;
917 if (!s->eof[1] && ff_outlink_get_status(ctx->inputs[1])) {
920 if (!s->eof[0] && !s->xf[0])
921 ff_inlink_request_frame(ctx->inputs[0]);
922 if (!s->eof[1] && (s->need_second || s->eof[0]))
923 ff_inlink_request_frame(ctx->inputs[1]);
924 if (s->eof[0] && s->eof[1] && (
925 ff_inlink_queued_frames(ctx->inputs[0]) <= 0 ||
926 ff_inlink_queued_frames(ctx->inputs[1]) <= 0))
927 ff_outlink_set_status(outlink, AVERROR_EOF, AV_NOPTS_VALUE);
931 return FFERROR_NOT_READY;
934 static const AVFilterPad xfade_inputs[] = {
937 .type = AVMEDIA_TYPE_VIDEO,
941 .type = AVMEDIA_TYPE_VIDEO,
946 static const AVFilterPad xfade_outputs[] = {
949 .type = AVMEDIA_TYPE_VIDEO,
950 .config_props = config_output,
955 AVFilter ff_vf_xfade = {
957 .description = NULL_IF_CONFIG_SMALL("Cross fade one video with another video."),
958 .priv_size = sizeof(XFadeContext),
959 .priv_class = &xfade_class,
960 .query_formats = query_formats,
961 .activate = xfade_activate,
963 .inputs = xfade_inputs,
964 .outputs = xfade_outputs,
965 .flags = AVFILTER_FLAG_SLICE_THREADS,