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 {
61 typedef struct XFadeContext {
85 void (*transitionf)(AVFilterContext *ctx, const AVFrame *a, const AVFrame *b, AVFrame *out, float progress,
86 int slice_start, int slice_end, int jobnr);
91 static const char *const var_names[] = { "X", "Y", "W", "H", "A", "B", "PLANE", "P", NULL };
92 enum { VAR_X, VAR_Y, VAR_W, VAR_H, VAR_A, VAR_B, VAR_PLANE, VAR_PROGRESS, VAR_VARS_NB };
94 typedef struct ThreadData {
100 static int query_formats(AVFilterContext *ctx)
102 static const enum AVPixelFormat pix_fmts[] = {
106 AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRAP, AV_PIX_FMT_GRAY8,
107 AV_PIX_FMT_YUVA444P9, AV_PIX_FMT_GBRP9,
108 AV_PIX_FMT_YUV444P10,
109 AV_PIX_FMT_YUVA444P10,
110 AV_PIX_FMT_GBRP10, AV_PIX_FMT_GBRAP10, AV_PIX_FMT_GRAY10,
111 AV_PIX_FMT_YUV444P12,
112 AV_PIX_FMT_YUVA444P12,
113 AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRAP12, AV_PIX_FMT_GRAY12,
114 AV_PIX_FMT_YUV444P14, AV_PIX_FMT_GBRP14,
115 AV_PIX_FMT_YUV444P16,
116 AV_PIX_FMT_YUVA444P16,
117 AV_PIX_FMT_GBRP16, AV_PIX_FMT_GBRAP16, AV_PIX_FMT_GRAY16,
121 AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
123 return AVERROR(ENOMEM);
124 return ff_set_common_formats(ctx, fmts_list);
127 static av_cold void uninit(AVFilterContext *ctx)
129 XFadeContext *s = ctx->priv;
134 #define OFFSET(x) offsetof(XFadeContext, x)
135 #define FLAGS (AV_OPT_FLAG_FILTERING_PARAM | AV_OPT_FLAG_VIDEO_PARAM)
137 static const AVOption xfade_options[] = {
138 { "transition", "set cross fade transition", OFFSET(transition), AV_OPT_TYPE_INT, {.i64=FADE}, -1, NB_TRANSITIONS-1, FLAGS, "transition" },
139 { "custom", "custom transition", 0, AV_OPT_TYPE_CONST, {.i64=CUSTOM}, 0, 0, FLAGS, "transition" },
140 { "fade", "fade transition", 0, AV_OPT_TYPE_CONST, {.i64=FADE}, 0, 0, FLAGS, "transition" },
141 { "wipeleft", "wipe left transition", 0, AV_OPT_TYPE_CONST, {.i64=WIPELEFT}, 0, 0, FLAGS, "transition" },
142 { "wiperight", "wipe right transition", 0, AV_OPT_TYPE_CONST, {.i64=WIPERIGHT}, 0, 0, FLAGS, "transition" },
143 { "wipeup", "wipe up transition", 0, AV_OPT_TYPE_CONST, {.i64=WIPEUP}, 0, 0, FLAGS, "transition" },
144 { "wipedown", "wipe down transition", 0, AV_OPT_TYPE_CONST, {.i64=WIPEDOWN}, 0, 0, FLAGS, "transition" },
145 { "slideleft", "slide left transition", 0, AV_OPT_TYPE_CONST, {.i64=SLIDELEFT}, 0, 0, FLAGS, "transition" },
146 { "slideright", "slide right transition", 0, AV_OPT_TYPE_CONST, {.i64=SLIDERIGHT}, 0, 0, FLAGS, "transition" },
147 { "slideup", "slide up transition", 0, AV_OPT_TYPE_CONST, {.i64=SLIDEUP}, 0, 0, FLAGS, "transition" },
148 { "slidedown", "slide down transition", 0, AV_OPT_TYPE_CONST, {.i64=SLIDEDOWN}, 0, 0, FLAGS, "transition" },
149 { "circlecrop", "circle crop transition", 0, AV_OPT_TYPE_CONST, {.i64=CIRCLECROP}, 0, 0, FLAGS, "transition" },
150 { "rectcrop", "rect crop transition", 0, AV_OPT_TYPE_CONST, {.i64=RECTCROP}, 0, 0, FLAGS, "transition" },
151 { "distance", "distance transition", 0, AV_OPT_TYPE_CONST, {.i64=DISTANCE}, 0, 0, FLAGS, "transition" },
152 { "fadeblack", "fadeblack transition", 0, AV_OPT_TYPE_CONST, {.i64=FADEBLACK}, 0, 0, FLAGS, "transition" },
153 { "fadewhite", "fadewhite transition", 0, AV_OPT_TYPE_CONST, {.i64=FADEWHITE}, 0, 0, FLAGS, "transition" },
154 { "radial", "radial transition", 0, AV_OPT_TYPE_CONST, {.i64=RADIAL}, 0, 0, FLAGS, "transition" },
155 { "smoothleft", "smoothleft transition", 0, AV_OPT_TYPE_CONST, {.i64=SMOOTHLEFT}, 0, 0, FLAGS, "transition" },
156 { "smoothright","smoothright transition", 0, AV_OPT_TYPE_CONST, {.i64=SMOOTHRIGHT},0, 0, FLAGS, "transition" },
157 { "smoothup", "smoothup transition", 0, AV_OPT_TYPE_CONST, {.i64=SMOOTHUP}, 0, 0, FLAGS, "transition" },
158 { "smoothdown", "smoothdown transition", 0, AV_OPT_TYPE_CONST, {.i64=SMOOTHDOWN}, 0, 0, FLAGS, "transition" },
159 { "circleopen", "circleopen transition", 0, AV_OPT_TYPE_CONST, {.i64=CIRCLEOPEN}, 0, 0, FLAGS, "transition" },
160 { "circleclose","circleclose transition", 0, AV_OPT_TYPE_CONST, {.i64=CIRCLECLOSE},0, 0, FLAGS, "transition" },
161 { "vertopen", "vert open transition", 0, AV_OPT_TYPE_CONST, {.i64=VERTOPEN}, 0, 0, FLAGS, "transition" },
162 { "vertclose", "vert close transition", 0, AV_OPT_TYPE_CONST, {.i64=VERTCLOSE}, 0, 0, FLAGS, "transition" },
163 { "horzopen", "horz open transition", 0, AV_OPT_TYPE_CONST, {.i64=HORZOPEN}, 0, 0, FLAGS, "transition" },
164 { "horzclose", "horz close transition", 0, AV_OPT_TYPE_CONST, {.i64=HORZCLOSE}, 0, 0, FLAGS, "transition" },
165 { "duration", "set cross fade duration", OFFSET(duration), AV_OPT_TYPE_DURATION, {.i64=1000000}, 0, 60000000, FLAGS },
166 { "offset", "set cross fade start relative to first input stream", OFFSET(offset), AV_OPT_TYPE_DURATION, {.i64=0}, INT64_MIN, INT64_MAX, FLAGS },
167 { "expr", "set expression for custom transition", OFFSET(custom_str), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, FLAGS },
171 AVFILTER_DEFINE_CLASS(xfade);
173 #define CUSTOM_TRANSITION(name, type, div) \
174 static void custom##name##_transition(AVFilterContext *ctx, \
175 const AVFrame *a, const AVFrame *b, AVFrame *out, \
177 int slice_start, int slice_end, int jobnr) \
179 XFadeContext *s = ctx->priv; \
180 const int height = slice_end - slice_start; \
182 double values[VAR_VARS_NB]; \
183 values[VAR_W] = out->width; \
184 values[VAR_H] = out->height; \
185 values[VAR_PROGRESS] = progress; \
187 for (int p = 0; p < s->nb_planes; p++) { \
188 const type *xf0 = (const type *)(a->data[p] + slice_start * a->linesize[p]); \
189 const type *xf1 = (const type *)(b->data[p] + slice_start * b->linesize[p]); \
190 type *dst = (type *)(out->data[p] + slice_start * out->linesize[p]); \
192 values[VAR_PLANE] = p; \
194 for (int y = 0; y < height; y++) { \
195 values[VAR_Y] = slice_start + y; \
196 for (int x = 0; x < out->width; x++) { \
198 values[VAR_A] = xf0[x]; \
199 values[VAR_B] = xf1[x]; \
200 dst[x] = av_expr_eval(s->e, values, s); \
203 dst += out->linesize[p] / div; \
204 xf0 += a->linesize[p] / div; \
205 xf1 += b->linesize[p] / div; \
210 CUSTOM_TRANSITION(8, uint8_t, 1)
211 CUSTOM_TRANSITION(16, uint16_t, 2)
213 static inline float mix(float a, float b, float mix)
215 return a * mix + b * (1.f - mix);
218 static inline float smoothstep(float edge0, float edge1, float x)
222 t = av_clipf((x - edge0) / (edge1 - edge0), 0.f, 1.f);
224 return t * t * (3.f - 2.f * t);
227 #define FADE_TRANSITION(name, type, div) \
228 static void fade##name##_transition(AVFilterContext *ctx, \
229 const AVFrame *a, const AVFrame *b, AVFrame *out, \
231 int slice_start, int slice_end, int jobnr) \
233 XFadeContext *s = ctx->priv; \
234 const int height = slice_end - slice_start; \
236 for (int p = 0; p < s->nb_planes; p++) { \
237 const type *xf0 = (const type *)(a->data[p] + slice_start * a->linesize[p]); \
238 const type *xf1 = (const type *)(b->data[p] + slice_start * b->linesize[p]); \
239 type *dst = (type *)(out->data[p] + slice_start * out->linesize[p]); \
241 for (int y = 0; y < height; y++) { \
242 for (int x = 0; x < out->width; x++) { \
243 dst[x] = mix(xf0[x], xf1[x], progress); \
246 dst += out->linesize[p] / div; \
247 xf0 += a->linesize[p] / div; \
248 xf1 += b->linesize[p] / div; \
253 FADE_TRANSITION(8, uint8_t, 1)
254 FADE_TRANSITION(16, uint16_t, 2)
256 #define WIPELEFT_TRANSITION(name, type, div) \
257 static void wipeleft##name##_transition(AVFilterContext *ctx, \
258 const AVFrame *a, const AVFrame *b, AVFrame *out, \
260 int slice_start, int slice_end, int jobnr) \
262 XFadeContext *s = ctx->priv; \
263 const int height = slice_end - slice_start; \
264 const int z = out->width * progress; \
266 for (int p = 0; p < s->nb_planes; p++) { \
267 const type *xf0 = (const type *)(a->data[p] + slice_start * a->linesize[p]); \
268 const type *xf1 = (const type *)(b->data[p] + slice_start * b->linesize[p]); \
269 type *dst = (type *)(out->data[p] + slice_start * out->linesize[p]); \
271 for (int y = 0; y < height; y++) { \
272 for (int x = 0; x < out->width; x++) { \
273 dst[x] = x > z ? xf1[x] : xf0[x]; \
276 dst += out->linesize[p] / div; \
277 xf0 += a->linesize[p] / div; \
278 xf1 += b->linesize[p] / div; \
283 WIPELEFT_TRANSITION(8, uint8_t, 1)
284 WIPELEFT_TRANSITION(16, uint16_t, 2)
286 #define WIPERIGHT_TRANSITION(name, type, div) \
287 static void wiperight##name##_transition(AVFilterContext *ctx, \
288 const AVFrame *a, const AVFrame *b, AVFrame *out, \
290 int slice_start, int slice_end, int jobnr) \
292 XFadeContext *s = ctx->priv; \
293 const int height = slice_end - slice_start; \
294 const int z = out->width * (1.f - progress); \
296 for (int p = 0; p < s->nb_planes; p++) { \
297 const type *xf0 = (const type *)(a->data[p] + slice_start * a->linesize[p]); \
298 const type *xf1 = (const type *)(b->data[p] + slice_start * b->linesize[p]); \
299 type *dst = (type *)(out->data[p] + slice_start * out->linesize[p]); \
301 for (int y = 0; y < height; y++) { \
302 for (int x = 0; x < out->width; x++) { \
303 dst[x] = x > z ? xf0[x] : xf1[x]; \
306 dst += out->linesize[p] / div; \
307 xf0 += a->linesize[p] / div; \
308 xf1 += b->linesize[p] / div; \
313 WIPERIGHT_TRANSITION(8, uint8_t, 1)
314 WIPERIGHT_TRANSITION(16, uint16_t, 2)
316 #define WIPEUP_TRANSITION(name, type, div) \
317 static void wipeup##name##_transition(AVFilterContext *ctx, \
318 const AVFrame *a, const AVFrame *b, AVFrame *out, \
320 int slice_start, int slice_end, int jobnr) \
322 XFadeContext *s = ctx->priv; \
323 const int height = slice_end - slice_start; \
324 const int z = out->height * progress; \
326 for (int p = 0; p < s->nb_planes; p++) { \
327 const type *xf0 = (const type *)(a->data[p] + slice_start * a->linesize[p]); \
328 const type *xf1 = (const type *)(b->data[p] + slice_start * b->linesize[p]); \
329 type *dst = (type *)(out->data[p] + slice_start * out->linesize[p]); \
331 for (int y = 0; y < height; y++) { \
332 for (int x = 0; x < out->width; x++) { \
333 dst[x] = slice_start + y > z ? xf1[x] : xf0[x]; \
336 dst += out->linesize[p] / div; \
337 xf0 += a->linesize[p] / div; \
338 xf1 += b->linesize[p] / div; \
343 WIPEUP_TRANSITION(8, uint8_t, 1)
344 WIPEUP_TRANSITION(16, uint16_t, 2)
346 #define WIPEDOWN_TRANSITION(name, type, div) \
347 static void wipedown##name##_transition(AVFilterContext *ctx, \
348 const AVFrame *a, const AVFrame *b, AVFrame *out, \
350 int slice_start, int slice_end, int jobnr) \
352 XFadeContext *s = ctx->priv; \
353 const int height = slice_end - slice_start; \
354 const int z = out->height * (1.f - progress); \
356 for (int p = 0; p < s->nb_planes; p++) { \
357 const type *xf0 = (const type *)(a->data[p] + slice_start * a->linesize[p]); \
358 const type *xf1 = (const type *)(b->data[p] + slice_start * b->linesize[p]); \
359 type *dst = (type *)(out->data[p] + slice_start * out->linesize[p]); \
361 for (int y = 0; y < height; y++) { \
362 for (int x = 0; x < out->width; x++) { \
363 dst[x] = slice_start + y > z ? xf0[x] : xf1[x]; \
366 dst += out->linesize[p] / div; \
367 xf0 += a->linesize[p] / div; \
368 xf1 += b->linesize[p] / div; \
373 WIPEDOWN_TRANSITION(8, uint8_t, 1)
374 WIPEDOWN_TRANSITION(16, uint16_t, 2)
376 #define SLIDELEFT_TRANSITION(name, type, div) \
377 static void slideleft##name##_transition(AVFilterContext *ctx, \
378 const AVFrame *a, const AVFrame *b, AVFrame *out, \
380 int slice_start, int slice_end, int jobnr) \
382 XFadeContext *s = ctx->priv; \
383 const int height = slice_end - slice_start; \
384 const int width = out->width; \
385 const int z = -progress * width; \
387 for (int p = 0; p < s->nb_planes; p++) { \
388 const type *xf0 = (const type *)(a->data[p] + slice_start * a->linesize[p]); \
389 const type *xf1 = (const type *)(b->data[p] + slice_start * b->linesize[p]); \
390 type *dst = (type *)(out->data[p] + slice_start * out->linesize[p]); \
392 for (int y = 0; y < height; y++) { \
393 for (int x = 0; x < width; x++) { \
394 const int zx = z + x; \
395 const int zz = zx % width + width * (zx < 0); \
396 dst[x] = (zx > 0) && (zx < width) ? xf1[zz] : xf0[zz]; \
399 dst += out->linesize[p] / div; \
400 xf0 += a->linesize[p] / div; \
401 xf1 += b->linesize[p] / div; \
406 SLIDELEFT_TRANSITION(8, uint8_t, 1)
407 SLIDELEFT_TRANSITION(16, uint16_t, 2)
409 #define SLIDERIGHT_TRANSITION(name, type, div) \
410 static void slideright##name##_transition(AVFilterContext *ctx, \
411 const AVFrame *a, const AVFrame *b, AVFrame *out, \
413 int slice_start, int slice_end, int jobnr) \
415 XFadeContext *s = ctx->priv; \
416 const int height = slice_end - slice_start; \
417 const int width = out->width; \
418 const int z = progress * width; \
420 for (int p = 0; p < s->nb_planes; p++) { \
421 const type *xf0 = (const type *)(a->data[p] + slice_start * a->linesize[p]); \
422 const type *xf1 = (const type *)(b->data[p] + slice_start * b->linesize[p]); \
423 type *dst = (type *)(out->data[p] + slice_start * out->linesize[p]); \
425 for (int y = 0; y < height; y++) { \
426 for (int x = 0; x < out->width; x++) { \
427 const int zx = z + x; \
428 const int zz = zx % width + width * (zx < 0); \
429 dst[x] = (zx > 0) && (zx < width) ? xf1[zz] : xf0[zz]; \
432 dst += out->linesize[p] / div; \
433 xf0 += a->linesize[p] / div; \
434 xf1 += b->linesize[p] / div; \
439 SLIDERIGHT_TRANSITION(8, uint8_t, 1)
440 SLIDERIGHT_TRANSITION(16, uint16_t, 2)
442 #define SLIDEUP_TRANSITION(name, type, div) \
443 static void slideup##name##_transition(AVFilterContext *ctx, \
444 const AVFrame *a, const AVFrame *b, AVFrame *out, \
446 int slice_start, int slice_end, int jobnr) \
448 XFadeContext *s = ctx->priv; \
449 const int height = out->height; \
450 const int z = -progress * height; \
452 for (int p = 0; p < s->nb_planes; p++) { \
453 type *dst = (type *)(out->data[p] + slice_start * out->linesize[p]); \
455 for (int y = slice_start; y < slice_end; y++) { \
456 const int zy = z + y; \
457 const int zz = zy % height + height * (zy < 0); \
458 const type *xf0 = (const type *)(a->data[p] + zz * a->linesize[p]); \
459 const type *xf1 = (const type *)(b->data[p] + zz * b->linesize[p]); \
461 for (int x = 0; x < out->width; x++) { \
462 dst[x] = (zy > 0) && (zy < height) ? xf1[x] : xf0[x]; \
465 dst += out->linesize[p] / div; \
470 SLIDEUP_TRANSITION(8, uint8_t, 1)
471 SLIDEUP_TRANSITION(16, uint16_t, 2)
473 #define SLIDEDOWN_TRANSITION(name, type, div) \
474 static void slidedown##name##_transition(AVFilterContext *ctx, \
475 const AVFrame *a, const AVFrame *b, AVFrame *out, \
477 int slice_start, int slice_end, int jobnr) \
479 XFadeContext *s = ctx->priv; \
480 const int height = out->height; \
481 const int z = progress * height; \
483 for (int p = 0; p < s->nb_planes; p++) { \
484 type *dst = (type *)(out->data[p] + slice_start * out->linesize[p]); \
486 for (int y = slice_start; y < slice_end; y++) { \
487 const int zy = z + y; \
488 const int zz = zy % height + height * (zy < 0); \
489 const type *xf0 = (const type *)(a->data[p] + zz * a->linesize[p]); \
490 const type *xf1 = (const type *)(b->data[p] + zz * b->linesize[p]); \
492 for (int x = 0; x < out->width; x++) { \
493 dst[x] = (zy > 0) && (zy < height) ? xf1[x] : xf0[x]; \
496 dst += out->linesize[p] / div; \
501 SLIDEDOWN_TRANSITION(8, uint8_t, 1)
502 SLIDEDOWN_TRANSITION(16, uint16_t, 2)
504 #define CIRCLECROP_TRANSITION(name, type, div) \
505 static void circlecrop##name##_transition(AVFilterContext *ctx, \
506 const AVFrame *a, const AVFrame *b, AVFrame *out, \
508 int slice_start, int slice_end, int jobnr) \
510 XFadeContext *s = ctx->priv; \
511 const int width = out->width; \
512 const int height = out->height; \
513 float z = powf(2.f * fabsf(progress - 0.5f), 3.f) * hypotf(width/2, height/2); \
515 for (int p = 0; p < s->nb_planes; p++) { \
516 const int bg = s->black[p]; \
517 type *dst = (type *)(out->data[p] + slice_start * out->linesize[p]); \
519 for (int y = slice_start; y < slice_end; y++) { \
520 const type *xf0 = (const type *)(a->data[p] + y * a->linesize[p]); \
521 const type *xf1 = (const type *)(b->data[p] + y * b->linesize[p]); \
523 for (int x = 0; x < width; x++) { \
524 float dist = hypotf(x - width / 2, y - height / 2); \
525 int val = progress < 0.5f ? xf1[x] : xf0[x]; \
526 dst[x] = (z < dist) ? bg : val; \
529 dst += out->linesize[p] / div; \
534 CIRCLECROP_TRANSITION(8, uint8_t, 1)
535 CIRCLECROP_TRANSITION(16, uint16_t, 2)
537 #define RECTCROP_TRANSITION(name, type, div) \
538 static void rectcrop##name##_transition(AVFilterContext *ctx, \
539 const AVFrame *a, const AVFrame *b, AVFrame *out, \
541 int slice_start, int slice_end, int jobnr) \
543 XFadeContext *s = ctx->priv; \
544 const int width = out->width; \
545 const int height = out->height; \
546 int zh = fabsf(progress - 0.5f) * height; \
547 int zw = fabsf(progress - 0.5f) * width; \
549 for (int p = 0; p < s->nb_planes; p++) { \
550 const int bg = s->black[p]; \
551 type *dst = (type *)(out->data[p] + slice_start * out->linesize[p]); \
553 for (int y = slice_start; y < slice_end; y++) { \
554 const type *xf0 = (const type *)(a->data[p] + y * a->linesize[p]); \
555 const type *xf1 = (const type *)(b->data[p] + y * b->linesize[p]); \
557 for (int x = 0; x < width; x++) { \
558 int dist = FFABS(x - width / 2) < zw && \
559 FFABS(y - height / 2) < zh; \
560 int val = progress < 0.5f ? xf1[x] : xf0[x]; \
561 dst[x] = !dist ? bg : val; \
564 dst += out->linesize[p] / div; \
569 RECTCROP_TRANSITION(8, uint8_t, 1)
570 RECTCROP_TRANSITION(16, uint16_t, 2)
572 #define DISTANCE_TRANSITION(name, type, div) \
573 static void distance##name##_transition(AVFilterContext *ctx, \
574 const AVFrame *a, const AVFrame *b, AVFrame *out, \
576 int slice_start, int slice_end, int jobnr) \
578 XFadeContext *s = ctx->priv; \
579 const int width = out->width; \
580 const float max = s->max_value; \
582 for (int y = slice_start; y < slice_end; y++) { \
583 for (int x = 0; x < width; x++) { \
585 for (int p = 0; p < s->nb_planes; p++) { \
586 const type *xf0 = (const type *)(a->data[p] + y * a->linesize[p]); \
587 const type *xf1 = (const type *)(b->data[p] + y * b->linesize[p]); \
589 dist += (xf0[x] / max - xf1[x] / max) * \
590 (xf0[x] / max - xf1[x] / max); \
593 dist = sqrtf(dist) <= progress; \
594 for (int p = 0; p < s->nb_planes; p++) { \
595 const type *xf0 = (const type *)(a->data[p] + y * a->linesize[p]); \
596 const type *xf1 = (const type *)(b->data[p] + y * b->linesize[p]); \
597 type *dst = (type *)(out->data[p] + y * out->linesize[p]); \
598 dst[x] = mix(mix(xf0[x], xf1[x], dist), xf1[x], progress); \
604 DISTANCE_TRANSITION(8, uint8_t, 1)
605 DISTANCE_TRANSITION(16, uint16_t, 2)
607 #define FADEBLACK_TRANSITION(name, type, div) \
608 static void fadeblack##name##_transition(AVFilterContext *ctx, \
609 const AVFrame *a, const AVFrame *b, AVFrame *out, \
611 int slice_start, int slice_end, int jobnr) \
613 XFadeContext *s = ctx->priv; \
614 const int height = slice_end - slice_start; \
615 const float phase = 0.2f; \
617 for (int p = 0; p < s->nb_planes; p++) { \
618 const type *xf0 = (const type *)(a->data[p] + slice_start * a->linesize[p]); \
619 const type *xf1 = (const type *)(b->data[p] + slice_start * b->linesize[p]); \
620 type *dst = (type *)(out->data[p] + slice_start * out->linesize[p]); \
621 const int bg = s->black[p]; \
623 for (int y = 0; y < height; y++) { \
624 for (int x = 0; x < out->width; x++) { \
625 dst[x] = mix(mix(xf0[x], bg, smoothstep(1.f-phase, 1.f, progress)), \
626 mix(bg, xf1[x], smoothstep(phase, 1.f, progress)), \
630 dst += out->linesize[p] / div; \
631 xf0 += a->linesize[p] / div; \
632 xf1 += b->linesize[p] / div; \
637 FADEBLACK_TRANSITION(8, uint8_t, 1)
638 FADEBLACK_TRANSITION(16, uint16_t, 2)
640 #define FADEWHITE_TRANSITION(name, type, div) \
641 static void fadewhite##name##_transition(AVFilterContext *ctx, \
642 const AVFrame *a, const AVFrame *b, AVFrame *out, \
644 int slice_start, int slice_end, int jobnr) \
646 XFadeContext *s = ctx->priv; \
647 const int height = slice_end - slice_start; \
648 const float phase = 0.2f; \
650 for (int p = 0; p < s->nb_planes; p++) { \
651 const type *xf0 = (const type *)(a->data[p] + slice_start * a->linesize[p]); \
652 const type *xf1 = (const type *)(b->data[p] + slice_start * b->linesize[p]); \
653 type *dst = (type *)(out->data[p] + slice_start * out->linesize[p]); \
654 const int bg = s->white[p]; \
656 for (int y = 0; y < height; y++) { \
657 for (int x = 0; x < out->width; x++) { \
658 dst[x] = mix(mix(xf0[x], bg, smoothstep(1.f-phase, 1.f, progress)), \
659 mix(bg, xf1[x], smoothstep(phase, 1.f, progress)), \
663 dst += out->linesize[p] / div; \
664 xf0 += a->linesize[p] / div; \
665 xf1 += b->linesize[p] / div; \
670 FADEWHITE_TRANSITION(8, uint8_t, 1)
671 FADEWHITE_TRANSITION(16, uint16_t, 2)
673 #define RADIAL_TRANSITION(name, type, div) \
674 static void radial##name##_transition(AVFilterContext *ctx, \
675 const AVFrame *a, const AVFrame *b, AVFrame *out, \
677 int slice_start, int slice_end, int jobnr) \
679 XFadeContext *s = ctx->priv; \
680 const int width = out->width; \
681 const int height = out->height; \
683 for (int y = slice_start; y < slice_end; y++) { \
684 for (int x = 0; x < width; x++) { \
685 const float smooth = atan2f(x - width / 2, y - height / 2) - \
686 (progress - 0.5f) * (M_PI * 2.5f); \
687 for (int p = 0; p < s->nb_planes; p++) { \
688 const type *xf0 = (const type *)(a->data[p] + y * a->linesize[p]); \
689 const type *xf1 = (const type *)(b->data[p] + y * b->linesize[p]); \
690 type *dst = (type *)(out->data[p] + y * out->linesize[p]); \
692 dst[x] = mix(xf1[x], xf0[x], smoothstep(0.f, 1.f, smooth)); \
698 RADIAL_TRANSITION(8, uint8_t, 1)
699 RADIAL_TRANSITION(16, uint16_t, 2)
701 #define SMOOTHLEFT_TRANSITION(name, type, div) \
702 static void smoothleft##name##_transition(AVFilterContext *ctx, \
703 const AVFrame *a, const AVFrame *b, AVFrame *out, \
705 int slice_start, int slice_end, int jobnr) \
707 XFadeContext *s = ctx->priv; \
708 const int width = out->width; \
709 const float w = width; \
711 for (int y = slice_start; y < slice_end; y++) { \
712 for (int x = 0; x < width; x++) { \
713 const float smooth = 1.f + x / w - progress * 2.f; \
715 for (int p = 0; p < s->nb_planes; p++) { \
716 const type *xf0 = (const type *)(a->data[p] + y * a->linesize[p]); \
717 const type *xf1 = (const type *)(b->data[p] + y * b->linesize[p]); \
718 type *dst = (type *)(out->data[p] + y * out->linesize[p]); \
720 dst[x] = mix(xf1[x], xf0[x], smoothstep(0.f, 1.f, smooth)); \
726 SMOOTHLEFT_TRANSITION(8, uint8_t, 1)
727 SMOOTHLEFT_TRANSITION(16, uint16_t, 2)
729 #define SMOOTHRIGHT_TRANSITION(name, type, div) \
730 static void smoothright##name##_transition(AVFilterContext *ctx, \
731 const AVFrame *a, const AVFrame *b, AVFrame *out, \
733 int slice_start, int slice_end, int jobnr) \
735 XFadeContext *s = ctx->priv; \
736 const int width = out->width; \
737 const float w = width; \
739 for (int y = slice_start; y < slice_end; y++) { \
740 for (int x = 0; x < width; x++) { \
741 const float smooth = 1.f + (w - 1 - x) / w - progress * 2.f; \
743 for (int p = 0; p < s->nb_planes; p++) { \
744 const type *xf0 = (const type *)(a->data[p] + y * a->linesize[p]); \
745 const type *xf1 = (const type *)(b->data[p] + y * b->linesize[p]); \
746 type *dst = (type *)(out->data[p] + y * out->linesize[p]); \
748 dst[x] = mix(xf1[x], xf0[x], smoothstep(0.f, 1.f, smooth)); \
754 SMOOTHRIGHT_TRANSITION(8, uint8_t, 1)
755 SMOOTHRIGHT_TRANSITION(16, uint16_t, 2)
757 #define SMOOTHUP_TRANSITION(name, type, div) \
758 static void smoothup##name##_transition(AVFilterContext *ctx, \
759 const AVFrame *a, const AVFrame *b, AVFrame *out, \
761 int slice_start, int slice_end, int jobnr) \
763 XFadeContext *s = ctx->priv; \
764 const int width = out->width; \
765 const float h = out->height; \
767 for (int y = slice_start; y < slice_end; y++) { \
768 const float smooth = 1.f + y / h - progress * 2.f; \
769 for (int x = 0; x < width; x++) { \
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 SMOOTHUP_TRANSITION(8, uint8_t, 1)
782 SMOOTHUP_TRANSITION(16, uint16_t, 2)
784 #define SMOOTHDOWN_TRANSITION(name, type, div) \
785 static void smoothdown##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 + (h - 1 - 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 SMOOTHDOWN_TRANSITION(8, uint8_t, 1)
809 SMOOTHDOWN_TRANSITION(16, uint16_t, 2)
811 #define CIRCLEOPEN_TRANSITION(name, type, div) \
812 static void circleopen##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 int height = out->height; \
820 const float z = hypotf(width / 2, height / 2); \
821 const float p = (progress - 0.5f) * 3.f; \
823 for (int y = slice_start; y < slice_end; y++) { \
824 for (int x = 0; x < width; x++) { \
825 const float smooth = hypotf(x - width / 2, y - height / 2) / z + p; \
826 for (int p = 0; p < s->nb_planes; p++) { \
827 const type *xf0 = (const type *)(a->data[p] + y * a->linesize[p]); \
828 const type *xf1 = (const type *)(b->data[p] + y * b->linesize[p]); \
829 type *dst = (type *)(out->data[p] + y * out->linesize[p]); \
831 dst[x] = mix(xf0[x], xf1[x], smoothstep(0.f, 1.f, smooth)); \
837 CIRCLEOPEN_TRANSITION(8, uint8_t, 1)
838 CIRCLEOPEN_TRANSITION(16, uint16_t, 2)
840 #define CIRCLECLOSE_TRANSITION(name, type, div) \
841 static void circleclose##name##_transition(AVFilterContext *ctx, \
842 const AVFrame *a, const AVFrame *b, AVFrame *out, \
844 int slice_start, int slice_end, int jobnr) \
846 XFadeContext *s = ctx->priv; \
847 const int width = out->width; \
848 const int height = out->height; \
849 const float z = hypotf(width / 2, height / 2); \
850 const float p = (1.f - progress - 0.5f) * 3.f; \
852 for (int y = slice_start; y < slice_end; y++) { \
853 for (int x = 0; x < width; x++) { \
854 const float smooth = hypotf(x - width / 2, y - height / 2) / z + p; \
855 for (int p = 0; p < s->nb_planes; p++) { \
856 const type *xf0 = (const type *)(a->data[p] + y * a->linesize[p]); \
857 const type *xf1 = (const type *)(b->data[p] + y * b->linesize[p]); \
858 type *dst = (type *)(out->data[p] + y * out->linesize[p]); \
860 dst[x] = mix(xf1[x], xf0[x], smoothstep(0.f, 1.f, smooth)); \
866 CIRCLECLOSE_TRANSITION(8, uint8_t, 1)
867 CIRCLECLOSE_TRANSITION(16, uint16_t, 2)
869 #define VERTOPEN_TRANSITION(name, type, div) \
870 static void vertopen##name##_transition(AVFilterContext *ctx, \
871 const AVFrame *a, const AVFrame *b, AVFrame *out, \
873 int slice_start, int slice_end, int jobnr) \
875 XFadeContext *s = ctx->priv; \
876 const int width = out->width; \
877 const float w2 = out->width / 2; \
879 for (int y = slice_start; y < slice_end; y++) { \
880 for (int x = 0; x < width; x++) { \
881 const float smooth = 2.f - fabsf((x - w2) / w2) - progress * 2.f; \
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 VERTOPEN_TRANSITION(8, uint8_t, 1)
894 VERTOPEN_TRANSITION(16, uint16_t, 2)
896 #define VERTCLOSE_TRANSITION(name, type, div) \
897 static void vertclose##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 = 1.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 VERTCLOSE_TRANSITION(8, uint8_t, 1)
921 VERTCLOSE_TRANSITION(16, uint16_t, 2)
923 #define HORZOPEN_TRANSITION(name, type, div) \
924 static void horzopen##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 h2 = out->height / 2; \
933 for (int y = slice_start; y < slice_end; y++) { \
934 const float smooth = 2.f - fabsf((y - h2) / h2) - progress * 2.f; \
935 for (int x = 0; x < width; x++) { \
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 HORZOPEN_TRANSITION(8, uint8_t, 1)
948 HORZOPEN_TRANSITION(16, uint16_t, 2)
950 #define HORZCLOSE_TRANSITION(name, type, div) \
951 static void horzclose##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 = 1.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 HORZCLOSE_TRANSITION(8, uint8_t, 1)
975 HORZCLOSE_TRANSITION(16, uint16_t, 2)
977 static inline double getpix(void *priv, double x, double y, int plane, int nb)
979 XFadeContext *s = priv;
980 AVFrame *in = s->xf[nb];
981 const uint8_t *src = in->data[FFMIN(plane, s->nb_planes - 1)];
982 int linesize = in->linesize[FFMIN(plane, s->nb_planes - 1)];
983 const int w = in->width;
984 const int h = in->height;
988 xi = av_clipd(x, 0, w - 1);
989 yi = av_clipd(y, 0, h - 1);
992 const uint16_t *src16 = (const uint16_t*)src;
995 return src16[xi + yi * linesize];
997 return src[xi + yi * linesize];
1001 static double a0(void *priv, double x, double y) { return getpix(priv, x, y, 0, 0); }
1002 static double a1(void *priv, double x, double y) { return getpix(priv, x, y, 1, 0); }
1003 static double a2(void *priv, double x, double y) { return getpix(priv, x, y, 2, 0); }
1004 static double a3(void *priv, double x, double y) { return getpix(priv, x, y, 3, 0); }
1006 static double b0(void *priv, double x, double y) { return getpix(priv, x, y, 0, 1); }
1007 static double b1(void *priv, double x, double y) { return getpix(priv, x, y, 1, 1); }
1008 static double b2(void *priv, double x, double y) { return getpix(priv, x, y, 2, 1); }
1009 static double b3(void *priv, double x, double y) { return getpix(priv, x, y, 3, 1); }
1011 static int config_output(AVFilterLink *outlink)
1013 AVFilterContext *ctx = outlink->src;
1014 AVFilterLink *inlink0 = ctx->inputs[0];
1015 AVFilterLink *inlink1 = ctx->inputs[1];
1016 XFadeContext *s = ctx->priv;
1017 const AVPixFmtDescriptor *pix_desc = av_pix_fmt_desc_get(inlink0->format);
1020 if (inlink0->format != inlink1->format) {
1021 av_log(ctx, AV_LOG_ERROR, "inputs must be of same pixel format\n");
1022 return AVERROR(EINVAL);
1024 if (inlink0->w != inlink1->w || inlink0->h != inlink1->h) {
1025 av_log(ctx, AV_LOG_ERROR, "First input link %s parameters "
1026 "(size %dx%d) do not match the corresponding "
1027 "second input link %s parameters (size %dx%d)\n",
1028 ctx->input_pads[0].name, inlink0->w, inlink0->h,
1029 ctx->input_pads[1].name, inlink1->w, inlink1->h);
1030 return AVERROR(EINVAL);
1033 if (inlink0->time_base.num != inlink1->time_base.num ||
1034 inlink0->time_base.den != inlink1->time_base.den) {
1035 av_log(ctx, AV_LOG_ERROR, "First input link %s timebase "
1036 "(%d/%d) do not match the corresponding "
1037 "second input link %s timebase (%d/%d)\n",
1038 ctx->input_pads[0].name, inlink0->time_base.num, inlink0->time_base.den,
1039 ctx->input_pads[1].name, inlink1->time_base.num, inlink1->time_base.den);
1040 return AVERROR(EINVAL);
1043 outlink->w = inlink0->w;
1044 outlink->h = inlink0->h;
1045 outlink->time_base = inlink0->time_base;
1046 outlink->sample_aspect_ratio = inlink0->sample_aspect_ratio;
1047 outlink->frame_rate = inlink0->frame_rate;
1049 s->depth = pix_desc->comp[0].depth;
1050 is_rgb = !!(pix_desc->flags & AV_PIX_FMT_FLAG_RGB);
1051 s->nb_planes = av_pix_fmt_count_planes(inlink0->format);
1052 s->max_value = (1 << s->depth) - 1;
1054 s->black[1] = s->black[2] = is_rgb ? 0 : s->max_value / 2;
1055 s->black[3] = s->max_value;
1056 s->white[0] = s->white[3] = s->max_value;
1057 s->white[1] = s->white[2] = is_rgb ? s->max_value : s->max_value / 2;
1059 s->first_pts = s->last_pts = s->pts = AV_NOPTS_VALUE;
1062 s->duration_pts = av_rescale_q(s->duration, AV_TIME_BASE_Q, outlink->time_base);
1064 s->offset_pts = av_rescale_q(s->offset, AV_TIME_BASE_Q, outlink->time_base);
1066 switch (s->transition) {
1067 case CUSTOM: s->transitionf = s->depth <= 8 ? custom8_transition : custom16_transition; break;
1068 case FADE: s->transitionf = s->depth <= 8 ? fade8_transition : fade16_transition; break;
1069 case WIPELEFT: s->transitionf = s->depth <= 8 ? wipeleft8_transition : wipeleft16_transition; break;
1070 case WIPERIGHT: s->transitionf = s->depth <= 8 ? wiperight8_transition : wiperight16_transition; break;
1071 case WIPEUP: s->transitionf = s->depth <= 8 ? wipeup8_transition : wipeup16_transition; break;
1072 case WIPEDOWN: s->transitionf = s->depth <= 8 ? wipedown8_transition : wipedown16_transition; break;
1073 case SLIDELEFT: s->transitionf = s->depth <= 8 ? slideleft8_transition : slideleft16_transition; break;
1074 case SLIDERIGHT: s->transitionf = s->depth <= 8 ? slideright8_transition : slideright16_transition; break;
1075 case SLIDEUP: s->transitionf = s->depth <= 8 ? slideup8_transition : slideup16_transition; break;
1076 case SLIDEDOWN: s->transitionf = s->depth <= 8 ? slidedown8_transition : slidedown16_transition; break;
1077 case CIRCLECROP: s->transitionf = s->depth <= 8 ? circlecrop8_transition : circlecrop16_transition; break;
1078 case RECTCROP: s->transitionf = s->depth <= 8 ? rectcrop8_transition : rectcrop16_transition; break;
1079 case DISTANCE: s->transitionf = s->depth <= 8 ? distance8_transition : distance16_transition; break;
1080 case FADEBLACK: s->transitionf = s->depth <= 8 ? fadeblack8_transition : fadeblack16_transition; break;
1081 case FADEWHITE: s->transitionf = s->depth <= 8 ? fadewhite8_transition : fadewhite16_transition; break;
1082 case RADIAL: s->transitionf = s->depth <= 8 ? radial8_transition : radial16_transition; break;
1083 case SMOOTHLEFT: s->transitionf = s->depth <= 8 ? smoothleft8_transition : smoothleft16_transition; break;
1084 case SMOOTHRIGHT:s->transitionf = s->depth <= 8 ? smoothright8_transition: smoothright16_transition;break;
1085 case SMOOTHUP: s->transitionf = s->depth <= 8 ? smoothup8_transition : smoothup16_transition; break;
1086 case SMOOTHDOWN: s->transitionf = s->depth <= 8 ? smoothdown8_transition : smoothdown16_transition; break;
1087 case CIRCLEOPEN: s->transitionf = s->depth <= 8 ? circleopen8_transition : circleopen16_transition; break;
1088 case CIRCLECLOSE:s->transitionf = s->depth <= 8 ? circleclose8_transition: circleclose16_transition;break;
1089 case VERTOPEN: s->transitionf = s->depth <= 8 ? vertopen8_transition : vertopen16_transition; break;
1090 case VERTCLOSE: s->transitionf = s->depth <= 8 ? vertclose8_transition : vertclose16_transition; break;
1091 case HORZOPEN: s->transitionf = s->depth <= 8 ? horzopen8_transition : horzopen16_transition; break;
1092 case HORZCLOSE: s->transitionf = s->depth <= 8 ? horzclose8_transition : horzclose16_transition; break;
1095 if (s->transition == CUSTOM) {
1096 static const char *const func2_names[] = {
1097 "a0", "a1", "a2", "a3",
1098 "b0", "b1", "b2", "b3",
1101 double (*func2[])(void *, double, double) = {
1108 return AVERROR(EINVAL);
1109 ret = av_expr_parse(&s->e, s->custom_str, var_names,
1110 NULL, NULL, func2_names, func2, 0, ctx);
1118 static int xfade_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
1120 XFadeContext *s = ctx->priv;
1121 AVFilterLink *outlink = ctx->outputs[0];
1122 ThreadData *td = arg;
1123 int slice_start = (outlink->h * jobnr ) / nb_jobs;
1124 int slice_end = (outlink->h * (jobnr+1)) / nb_jobs;
1126 s->transitionf(ctx, td->xf[0], td->xf[1], td->out, td->progress, slice_start, slice_end, jobnr);
1131 static int xfade_frame(AVFilterContext *ctx, AVFrame *a, AVFrame *b)
1133 XFadeContext *s = ctx->priv;
1134 AVFilterLink *outlink = ctx->outputs[0];
1135 float progress = av_clipf(1.f - ((float)(s->pts - s->first_pts - s->offset_pts) / s->duration_pts), 0.f, 1.f);
1139 out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
1141 return AVERROR(ENOMEM);
1143 td.xf[0] = a, td.xf[1] = b, td.out = out, td.progress = progress;
1144 ctx->internal->execute(ctx, xfade_slice, &td, NULL, FFMIN(outlink->h, ff_filter_get_nb_threads(ctx)));
1148 return ff_filter_frame(outlink, out);
1151 static int xfade_activate(AVFilterContext *ctx)
1153 XFadeContext *s = ctx->priv;
1154 AVFilterLink *outlink = ctx->outputs[0];
1156 int ret = 0, status;
1159 FF_FILTER_FORWARD_STATUS_BACK_ALL(outlink, ctx);
1161 if (s->xfade_is_over) {
1162 ret = ff_inlink_consume_frame(ctx->inputs[1], &in);
1165 } else if (ret > 0) {
1166 in->pts = (in->pts - s->last_pts) + s->pts;
1167 return ff_filter_frame(outlink, in);
1168 } else if (ff_inlink_acknowledge_status(ctx->inputs[1], &status, &pts)) {
1169 ff_outlink_set_status(outlink, status, s->pts);
1172 if (ff_outlink_frame_wanted(outlink)) {
1173 ff_inlink_request_frame(ctx->inputs[1]);
1179 if (ff_inlink_queued_frames(ctx->inputs[0]) > 0) {
1180 s->xf[0] = ff_inlink_peek_frame(ctx->inputs[0], 0);
1182 if (s->first_pts == AV_NOPTS_VALUE) {
1183 s->first_pts = s->xf[0]->pts;
1185 s->pts = s->xf[0]->pts;
1186 if (s->first_pts + s->offset_pts > s->xf[0]->pts) {
1189 ff_inlink_consume_frame(ctx->inputs[0], &in);
1190 return ff_filter_frame(outlink, in);
1197 if (s->xf[0] && ff_inlink_queued_frames(ctx->inputs[1]) > 0) {
1198 ff_inlink_consume_frame(ctx->inputs[0], &s->xf[0]);
1199 ff_inlink_consume_frame(ctx->inputs[1], &s->xf[1]);
1201 s->last_pts = s->xf[1]->pts;
1202 s->pts = s->xf[0]->pts;
1203 if (s->xf[0]->pts - (s->first_pts + s->offset_pts) > s->duration_pts)
1204 s->xfade_is_over = 1;
1205 ret = xfade_frame(ctx, s->xf[0], s->xf[1]);
1206 av_frame_free(&s->xf[0]);
1207 av_frame_free(&s->xf[1]);
1211 if (ff_inlink_queued_frames(ctx->inputs[0]) > 0 &&
1212 ff_inlink_queued_frames(ctx->inputs[1]) > 0) {
1213 ff_filter_set_ready(ctx, 100);
1217 if (ff_outlink_frame_wanted(outlink)) {
1218 if (!s->eof[0] && ff_outlink_get_status(ctx->inputs[0])) {
1220 s->xfade_is_over = 1;
1222 if (!s->eof[1] && ff_outlink_get_status(ctx->inputs[1])) {
1225 if (!s->eof[0] && !s->xf[0])
1226 ff_inlink_request_frame(ctx->inputs[0]);
1227 if (!s->eof[1] && (s->need_second || s->eof[0]))
1228 ff_inlink_request_frame(ctx->inputs[1]);
1229 if (s->eof[0] && s->eof[1] && (
1230 ff_inlink_queued_frames(ctx->inputs[0]) <= 0 ||
1231 ff_inlink_queued_frames(ctx->inputs[1]) <= 0))
1232 ff_outlink_set_status(outlink, AVERROR_EOF, AV_NOPTS_VALUE);
1236 return FFERROR_NOT_READY;
1239 static const AVFilterPad xfade_inputs[] = {
1242 .type = AVMEDIA_TYPE_VIDEO,
1246 .type = AVMEDIA_TYPE_VIDEO,
1251 static const AVFilterPad xfade_outputs[] = {
1254 .type = AVMEDIA_TYPE_VIDEO,
1255 .config_props = config_output,
1260 AVFilter ff_vf_xfade = {
1262 .description = NULL_IF_CONFIG_SMALL("Cross fade one video with another video."),
1263 .priv_size = sizeof(XFadeContext),
1264 .priv_class = &xfade_class,
1265 .query_formats = query_formats,
1266 .activate = xfade_activate,
1268 .inputs = xfade_inputs,
1269 .outputs = xfade_outputs,
1270 .flags = AVFILTER_FLAG_SLICE_THREADS,