You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1607 lines
101KB

  1. /*
  2. * Copyright (c) 2020 Paul B Mahol
  3. *
  4. * This file is part of FFmpeg.
  5. *
  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.
  10. *
  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.
  15. *
  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
  19. */
  20. #include "libavutil/imgutils.h"
  21. #include "libavutil/eval.h"
  22. #include "libavutil/opt.h"
  23. #include "libavutil/pixfmt.h"
  24. #include "avfilter.h"
  25. #include "formats.h"
  26. #include "internal.h"
  27. #include "filters.h"
  28. #include "video.h"
  29. enum XFadeTransitions {
  30. CUSTOM = -1,
  31. FADE,
  32. WIPELEFT,
  33. WIPERIGHT,
  34. WIPEUP,
  35. WIPEDOWN,
  36. SLIDELEFT,
  37. SLIDERIGHT,
  38. SLIDEUP,
  39. SLIDEDOWN,
  40. CIRCLECROP,
  41. RECTCROP,
  42. DISTANCE,
  43. FADEBLACK,
  44. FADEWHITE,
  45. RADIAL,
  46. SMOOTHLEFT,
  47. SMOOTHRIGHT,
  48. SMOOTHUP,
  49. SMOOTHDOWN,
  50. CIRCLEOPEN,
  51. CIRCLECLOSE,
  52. VERTOPEN,
  53. VERTCLOSE,
  54. HORZOPEN,
  55. HORZCLOSE,
  56. DISSOLVE,
  57. PIXELIZE,
  58. DIAGTL,
  59. DIAGTR,
  60. DIAGBL,
  61. DIAGBR,
  62. HLSLICE,
  63. HRSLICE,
  64. VUSLICE,
  65. VDSLICE,
  66. NB_TRANSITIONS,
  67. };
  68. typedef struct XFadeContext {
  69. const AVClass *class;
  70. int transition;
  71. int64_t duration;
  72. int64_t offset;
  73. char *custom_str;
  74. int nb_planes;
  75. int depth;
  76. int64_t duration_pts;
  77. int64_t offset_pts;
  78. int64_t first_pts;
  79. int64_t last_pts;
  80. int64_t pts;
  81. int xfade_is_over;
  82. int need_second;
  83. int eof[2];
  84. AVFrame *xf[2];
  85. int max_value;
  86. uint16_t black[4];
  87. uint16_t white[4];
  88. void (*transitionf)(AVFilterContext *ctx, const AVFrame *a, const AVFrame *b, AVFrame *out, float progress,
  89. int slice_start, int slice_end, int jobnr);
  90. AVExpr *e;
  91. } XFadeContext;
  92. static const char *const var_names[] = { "X", "Y", "W", "H", "A", "B", "PLANE", "P", NULL };
  93. enum { VAR_X, VAR_Y, VAR_W, VAR_H, VAR_A, VAR_B, VAR_PLANE, VAR_PROGRESS, VAR_VARS_NB };
  94. typedef struct ThreadData {
  95. const AVFrame *xf[2];
  96. AVFrame *out;
  97. float progress;
  98. } ThreadData;
  99. static int query_formats(AVFilterContext *ctx)
  100. {
  101. static const enum AVPixelFormat pix_fmts[] = {
  102. AV_PIX_FMT_YUVA444P,
  103. AV_PIX_FMT_YUVJ444P,
  104. AV_PIX_FMT_YUV444P,
  105. AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRAP, AV_PIX_FMT_GRAY8,
  106. AV_PIX_FMT_YUVA444P9, AV_PIX_FMT_GBRP9,
  107. AV_PIX_FMT_YUV444P10,
  108. AV_PIX_FMT_YUVA444P10,
  109. AV_PIX_FMT_GBRP10, AV_PIX_FMT_GBRAP10, AV_PIX_FMT_GRAY10,
  110. AV_PIX_FMT_YUV444P12,
  111. AV_PIX_FMT_YUVA444P12,
  112. AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRAP12, AV_PIX_FMT_GRAY12,
  113. AV_PIX_FMT_YUV444P14, AV_PIX_FMT_GBRP14,
  114. AV_PIX_FMT_YUV444P16,
  115. AV_PIX_FMT_YUVA444P16,
  116. AV_PIX_FMT_GBRP16, AV_PIX_FMT_GBRAP16, AV_PIX_FMT_GRAY16,
  117. AV_PIX_FMT_NONE
  118. };
  119. AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
  120. if (!fmts_list)
  121. return AVERROR(ENOMEM);
  122. return ff_set_common_formats(ctx, fmts_list);
  123. }
  124. static av_cold void uninit(AVFilterContext *ctx)
  125. {
  126. XFadeContext *s = ctx->priv;
  127. av_expr_free(s->e);
  128. }
  129. #define OFFSET(x) offsetof(XFadeContext, x)
  130. #define FLAGS (AV_OPT_FLAG_FILTERING_PARAM | AV_OPT_FLAG_VIDEO_PARAM)
  131. static const AVOption xfade_options[] = {
  132. { "transition", "set cross fade transition", OFFSET(transition), AV_OPT_TYPE_INT, {.i64=FADE}, -1, NB_TRANSITIONS-1, FLAGS, "transition" },
  133. { "custom", "custom transition", 0, AV_OPT_TYPE_CONST, {.i64=CUSTOM}, 0, 0, FLAGS, "transition" },
  134. { "fade", "fade transition", 0, AV_OPT_TYPE_CONST, {.i64=FADE}, 0, 0, FLAGS, "transition" },
  135. { "wipeleft", "wipe left transition", 0, AV_OPT_TYPE_CONST, {.i64=WIPELEFT}, 0, 0, FLAGS, "transition" },
  136. { "wiperight", "wipe right transition", 0, AV_OPT_TYPE_CONST, {.i64=WIPERIGHT}, 0, 0, FLAGS, "transition" },
  137. { "wipeup", "wipe up transition", 0, AV_OPT_TYPE_CONST, {.i64=WIPEUP}, 0, 0, FLAGS, "transition" },
  138. { "wipedown", "wipe down transition", 0, AV_OPT_TYPE_CONST, {.i64=WIPEDOWN}, 0, 0, FLAGS, "transition" },
  139. { "slideleft", "slide left transition", 0, AV_OPT_TYPE_CONST, {.i64=SLIDELEFT}, 0, 0, FLAGS, "transition" },
  140. { "slideright", "slide right transition", 0, AV_OPT_TYPE_CONST, {.i64=SLIDERIGHT}, 0, 0, FLAGS, "transition" },
  141. { "slideup", "slide up transition", 0, AV_OPT_TYPE_CONST, {.i64=SLIDEUP}, 0, 0, FLAGS, "transition" },
  142. { "slidedown", "slide down transition", 0, AV_OPT_TYPE_CONST, {.i64=SLIDEDOWN}, 0, 0, FLAGS, "transition" },
  143. { "circlecrop", "circle crop transition", 0, AV_OPT_TYPE_CONST, {.i64=CIRCLECROP}, 0, 0, FLAGS, "transition" },
  144. { "rectcrop", "rect crop transition", 0, AV_OPT_TYPE_CONST, {.i64=RECTCROP}, 0, 0, FLAGS, "transition" },
  145. { "distance", "distance transition", 0, AV_OPT_TYPE_CONST, {.i64=DISTANCE}, 0, 0, FLAGS, "transition" },
  146. { "fadeblack", "fadeblack transition", 0, AV_OPT_TYPE_CONST, {.i64=FADEBLACK}, 0, 0, FLAGS, "transition" },
  147. { "fadewhite", "fadewhite transition", 0, AV_OPT_TYPE_CONST, {.i64=FADEWHITE}, 0, 0, FLAGS, "transition" },
  148. { "radial", "radial transition", 0, AV_OPT_TYPE_CONST, {.i64=RADIAL}, 0, 0, FLAGS, "transition" },
  149. { "smoothleft", "smoothleft transition", 0, AV_OPT_TYPE_CONST, {.i64=SMOOTHLEFT}, 0, 0, FLAGS, "transition" },
  150. { "smoothright","smoothright transition", 0, AV_OPT_TYPE_CONST, {.i64=SMOOTHRIGHT},0, 0, FLAGS, "transition" },
  151. { "smoothup", "smoothup transition", 0, AV_OPT_TYPE_CONST, {.i64=SMOOTHUP}, 0, 0, FLAGS, "transition" },
  152. { "smoothdown", "smoothdown transition", 0, AV_OPT_TYPE_CONST, {.i64=SMOOTHDOWN}, 0, 0, FLAGS, "transition" },
  153. { "circleopen", "circleopen transition", 0, AV_OPT_TYPE_CONST, {.i64=CIRCLEOPEN}, 0, 0, FLAGS, "transition" },
  154. { "circleclose","circleclose transition", 0, AV_OPT_TYPE_CONST, {.i64=CIRCLECLOSE},0, 0, FLAGS, "transition" },
  155. { "vertopen", "vert open transition", 0, AV_OPT_TYPE_CONST, {.i64=VERTOPEN}, 0, 0, FLAGS, "transition" },
  156. { "vertclose", "vert close transition", 0, AV_OPT_TYPE_CONST, {.i64=VERTCLOSE}, 0, 0, FLAGS, "transition" },
  157. { "horzopen", "horz open transition", 0, AV_OPT_TYPE_CONST, {.i64=HORZOPEN}, 0, 0, FLAGS, "transition" },
  158. { "horzclose", "horz close transition", 0, AV_OPT_TYPE_CONST, {.i64=HORZCLOSE}, 0, 0, FLAGS, "transition" },
  159. { "dissolve", "dissolve transition", 0, AV_OPT_TYPE_CONST, {.i64=DISSOLVE}, 0, 0, FLAGS, "transition" },
  160. { "pixelize", "pixelize transition", 0, AV_OPT_TYPE_CONST, {.i64=PIXELIZE}, 0, 0, FLAGS, "transition" },
  161. { "diagtl", "diag tl transition", 0, AV_OPT_TYPE_CONST, {.i64=DIAGTL}, 0, 0, FLAGS, "transition" },
  162. { "diagtr", "diag tr transition", 0, AV_OPT_TYPE_CONST, {.i64=DIAGTR}, 0, 0, FLAGS, "transition" },
  163. { "diagbl", "diag bl transition", 0, AV_OPT_TYPE_CONST, {.i64=DIAGBL}, 0, 0, FLAGS, "transition" },
  164. { "diagbr", "diag br transition", 0, AV_OPT_TYPE_CONST, {.i64=DIAGBR}, 0, 0, FLAGS, "transition" },
  165. { "hlslice", "hl slice transition", 0, AV_OPT_TYPE_CONST, {.i64=HLSLICE}, 0, 0, FLAGS, "transition" },
  166. { "hrslice", "hr slice transition", 0, AV_OPT_TYPE_CONST, {.i64=HRSLICE}, 0, 0, FLAGS, "transition" },
  167. { "vuslice", "vu slice transition", 0, AV_OPT_TYPE_CONST, {.i64=VUSLICE}, 0, 0, FLAGS, "transition" },
  168. { "vdslice", "vd slice transition", 0, AV_OPT_TYPE_CONST, {.i64=VDSLICE}, 0, 0, FLAGS, "transition" },
  169. { "duration", "set cross fade duration", OFFSET(duration), AV_OPT_TYPE_DURATION, {.i64=1000000}, 0, 60000000, FLAGS },
  170. { "offset", "set cross fade start relative to first input stream", OFFSET(offset), AV_OPT_TYPE_DURATION, {.i64=0}, INT64_MIN, INT64_MAX, FLAGS },
  171. { "expr", "set expression for custom transition", OFFSET(custom_str), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, FLAGS },
  172. { NULL }
  173. };
  174. AVFILTER_DEFINE_CLASS(xfade);
  175. #define CUSTOM_TRANSITION(name, type, div) \
  176. static void custom##name##_transition(AVFilterContext *ctx, \
  177. const AVFrame *a, const AVFrame *b, AVFrame *out, \
  178. float progress, \
  179. int slice_start, int slice_end, int jobnr) \
  180. { \
  181. XFadeContext *s = ctx->priv; \
  182. const int height = slice_end - slice_start; \
  183. \
  184. double values[VAR_VARS_NB]; \
  185. values[VAR_W] = out->width; \
  186. values[VAR_H] = out->height; \
  187. values[VAR_PROGRESS] = progress; \
  188. \
  189. for (int p = 0; p < s->nb_planes; p++) { \
  190. const type *xf0 = (const type *)(a->data[p] + slice_start * a->linesize[p]); \
  191. const type *xf1 = (const type *)(b->data[p] + slice_start * b->linesize[p]); \
  192. type *dst = (type *)(out->data[p] + slice_start * out->linesize[p]); \
  193. \
  194. values[VAR_PLANE] = p; \
  195. \
  196. for (int y = 0; y < height; y++) { \
  197. values[VAR_Y] = slice_start + y; \
  198. for (int x = 0; x < out->width; x++) { \
  199. values[VAR_X] = x; \
  200. values[VAR_A] = xf0[x]; \
  201. values[VAR_B] = xf1[x]; \
  202. dst[x] = av_expr_eval(s->e, values, s); \
  203. } \
  204. \
  205. dst += out->linesize[p] / div; \
  206. xf0 += a->linesize[p] / div; \
  207. xf1 += b->linesize[p] / div; \
  208. } \
  209. } \
  210. }
  211. CUSTOM_TRANSITION(8, uint8_t, 1)
  212. CUSTOM_TRANSITION(16, uint16_t, 2)
  213. static inline float mix(float a, float b, float mix)
  214. {
  215. return a * mix + b * (1.f - mix);
  216. }
  217. static inline float fract(float a)
  218. {
  219. return a - floorf(a);
  220. }
  221. static inline float smoothstep(float edge0, float edge1, float x)
  222. {
  223. float t;
  224. t = av_clipf((x - edge0) / (edge1 - edge0), 0.f, 1.f);
  225. return t * t * (3.f - 2.f * t);
  226. }
  227. #define FADE_TRANSITION(name, type, div) \
  228. static void fade##name##_transition(AVFilterContext *ctx, \
  229. const AVFrame *a, const AVFrame *b, AVFrame *out, \
  230. float progress, \
  231. int slice_start, int slice_end, int jobnr) \
  232. { \
  233. XFadeContext *s = ctx->priv; \
  234. const int height = slice_end - slice_start; \
  235. \
  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]); \
  240. \
  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); \
  244. } \
  245. \
  246. dst += out->linesize[p] / div; \
  247. xf0 += a->linesize[p] / div; \
  248. xf1 += b->linesize[p] / div; \
  249. } \
  250. } \
  251. }
  252. FADE_TRANSITION(8, uint8_t, 1)
  253. FADE_TRANSITION(16, uint16_t, 2)
  254. #define WIPELEFT_TRANSITION(name, type, div) \
  255. static void wipeleft##name##_transition(AVFilterContext *ctx, \
  256. const AVFrame *a, const AVFrame *b, AVFrame *out, \
  257. float progress, \
  258. int slice_start, int slice_end, int jobnr) \
  259. { \
  260. XFadeContext *s = ctx->priv; \
  261. const int height = slice_end - slice_start; \
  262. const int z = out->width * progress; \
  263. \
  264. for (int p = 0; p < s->nb_planes; p++) { \
  265. const type *xf0 = (const type *)(a->data[p] + slice_start * a->linesize[p]); \
  266. const type *xf1 = (const type *)(b->data[p] + slice_start * b->linesize[p]); \
  267. type *dst = (type *)(out->data[p] + slice_start * out->linesize[p]); \
  268. \
  269. for (int y = 0; y < height; y++) { \
  270. for (int x = 0; x < out->width; x++) { \
  271. dst[x] = x > z ? xf1[x] : xf0[x]; \
  272. } \
  273. \
  274. dst += out->linesize[p] / div; \
  275. xf0 += a->linesize[p] / div; \
  276. xf1 += b->linesize[p] / div; \
  277. } \
  278. } \
  279. }
  280. WIPELEFT_TRANSITION(8, uint8_t, 1)
  281. WIPELEFT_TRANSITION(16, uint16_t, 2)
  282. #define WIPERIGHT_TRANSITION(name, type, div) \
  283. static void wiperight##name##_transition(AVFilterContext *ctx, \
  284. const AVFrame *a, const AVFrame *b, AVFrame *out, \
  285. float progress, \
  286. int slice_start, int slice_end, int jobnr) \
  287. { \
  288. XFadeContext *s = ctx->priv; \
  289. const int height = slice_end - slice_start; \
  290. const int z = out->width * (1.f - progress); \
  291. \
  292. for (int p = 0; p < s->nb_planes; p++) { \
  293. const type *xf0 = (const type *)(a->data[p] + slice_start * a->linesize[p]); \
  294. const type *xf1 = (const type *)(b->data[p] + slice_start * b->linesize[p]); \
  295. type *dst = (type *)(out->data[p] + slice_start * out->linesize[p]); \
  296. \
  297. for (int y = 0; y < height; y++) { \
  298. for (int x = 0; x < out->width; x++) { \
  299. dst[x] = x > z ? xf0[x] : xf1[x]; \
  300. } \
  301. \
  302. dst += out->linesize[p] / div; \
  303. xf0 += a->linesize[p] / div; \
  304. xf1 += b->linesize[p] / div; \
  305. } \
  306. } \
  307. }
  308. WIPERIGHT_TRANSITION(8, uint8_t, 1)
  309. WIPERIGHT_TRANSITION(16, uint16_t, 2)
  310. #define WIPEUP_TRANSITION(name, type, div) \
  311. static void wipeup##name##_transition(AVFilterContext *ctx, \
  312. const AVFrame *a, const AVFrame *b, AVFrame *out, \
  313. float progress, \
  314. int slice_start, int slice_end, int jobnr) \
  315. { \
  316. XFadeContext *s = ctx->priv; \
  317. const int height = slice_end - slice_start; \
  318. const int z = out->height * progress; \
  319. \
  320. for (int p = 0; p < s->nb_planes; p++) { \
  321. const type *xf0 = (const type *)(a->data[p] + slice_start * a->linesize[p]); \
  322. const type *xf1 = (const type *)(b->data[p] + slice_start * b->linesize[p]); \
  323. type *dst = (type *)(out->data[p] + slice_start * out->linesize[p]); \
  324. \
  325. for (int y = 0; y < height; y++) { \
  326. for (int x = 0; x < out->width; x++) { \
  327. dst[x] = slice_start + y > z ? xf1[x] : xf0[x]; \
  328. } \
  329. \
  330. dst += out->linesize[p] / div; \
  331. xf0 += a->linesize[p] / div; \
  332. xf1 += b->linesize[p] / div; \
  333. } \
  334. } \
  335. }
  336. WIPEUP_TRANSITION(8, uint8_t, 1)
  337. 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, \
  341. float progress, \
  342. int slice_start, int slice_end, int jobnr) \
  343. { \
  344. XFadeContext *s = ctx->priv; \
  345. const int height = slice_end - slice_start; \
  346. const int z = out->height * (1.f - progress); \
  347. \
  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]); \
  352. \
  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]; \
  356. } \
  357. \
  358. dst += out->linesize[p] / div; \
  359. xf0 += a->linesize[p] / div; \
  360. xf1 += b->linesize[p] / div; \
  361. } \
  362. } \
  363. }
  364. WIPEDOWN_TRANSITION(8, uint8_t, 1)
  365. WIPEDOWN_TRANSITION(16, uint16_t, 2)
  366. #define SLIDELEFT_TRANSITION(name, type, div) \
  367. static void slideleft##name##_transition(AVFilterContext *ctx, \
  368. const AVFrame *a, const AVFrame *b, AVFrame *out, \
  369. float progress, \
  370. int slice_start, int slice_end, int jobnr) \
  371. { \
  372. XFadeContext *s = ctx->priv; \
  373. const int height = slice_end - slice_start; \
  374. const int width = out->width; \
  375. const int z = -progress * width; \
  376. \
  377. for (int p = 0; p < s->nb_planes; p++) { \
  378. const type *xf0 = (const type *)(a->data[p] + slice_start * a->linesize[p]); \
  379. const type *xf1 = (const type *)(b->data[p] + slice_start * b->linesize[p]); \
  380. type *dst = (type *)(out->data[p] + slice_start * out->linesize[p]); \
  381. \
  382. for (int y = 0; y < height; y++) { \
  383. for (int x = 0; x < width; x++) { \
  384. const int zx = z + x; \
  385. const int zz = zx % width + width * (zx < 0); \
  386. dst[x] = (zx > 0) && (zx < width) ? xf1[zz] : xf0[zz]; \
  387. } \
  388. \
  389. dst += out->linesize[p] / div; \
  390. xf0 += a->linesize[p] / div; \
  391. xf1 += b->linesize[p] / div; \
  392. } \
  393. } \
  394. }
  395. SLIDELEFT_TRANSITION(8, uint8_t, 1)
  396. SLIDELEFT_TRANSITION(16, uint16_t, 2)
  397. #define SLIDERIGHT_TRANSITION(name, type, div) \
  398. static void slideright##name##_transition(AVFilterContext *ctx, \
  399. const AVFrame *a, const AVFrame *b, AVFrame *out, \
  400. float progress, \
  401. int slice_start, int slice_end, int jobnr) \
  402. { \
  403. XFadeContext *s = ctx->priv; \
  404. const int height = slice_end - slice_start; \
  405. const int width = out->width; \
  406. const int z = progress * width; \
  407. \
  408. for (int p = 0; p < s->nb_planes; p++) { \
  409. const type *xf0 = (const type *)(a->data[p] + slice_start * a->linesize[p]); \
  410. const type *xf1 = (const type *)(b->data[p] + slice_start * b->linesize[p]); \
  411. type *dst = (type *)(out->data[p] + slice_start * out->linesize[p]); \
  412. \
  413. for (int y = 0; y < height; y++) { \
  414. for (int x = 0; x < out->width; x++) { \
  415. const int zx = z + x; \
  416. const int zz = zx % width + width * (zx < 0); \
  417. dst[x] = (zx > 0) && (zx < width) ? xf1[zz] : xf0[zz]; \
  418. } \
  419. \
  420. dst += out->linesize[p] / div; \
  421. xf0 += a->linesize[p] / div; \
  422. xf1 += b->linesize[p] / div; \
  423. } \
  424. } \
  425. }
  426. SLIDERIGHT_TRANSITION(8, uint8_t, 1)
  427. SLIDERIGHT_TRANSITION(16, uint16_t, 2)
  428. #define SLIDEUP_TRANSITION(name, type, div) \
  429. static void slideup##name##_transition(AVFilterContext *ctx, \
  430. const AVFrame *a, const AVFrame *b, AVFrame *out, \
  431. float progress, \
  432. int slice_start, int slice_end, int jobnr) \
  433. { \
  434. XFadeContext *s = ctx->priv; \
  435. const int height = out->height; \
  436. const int z = -progress * height; \
  437. \
  438. for (int p = 0; p < s->nb_planes; p++) { \
  439. type *dst = (type *)(out->data[p] + slice_start * out->linesize[p]); \
  440. \
  441. for (int y = slice_start; y < slice_end; y++) { \
  442. const int zy = z + y; \
  443. const int zz = zy % height + height * (zy < 0); \
  444. const type *xf0 = (const type *)(a->data[p] + zz * a->linesize[p]); \
  445. const type *xf1 = (const type *)(b->data[p] + zz * b->linesize[p]); \
  446. \
  447. for (int x = 0; x < out->width; x++) { \
  448. dst[x] = (zy > 0) && (zy < height) ? xf1[x] : xf0[x]; \
  449. } \
  450. \
  451. dst += out->linesize[p] / div; \
  452. } \
  453. } \
  454. }
  455. SLIDEUP_TRANSITION(8, uint8_t, 1)
  456. SLIDEUP_TRANSITION(16, uint16_t, 2)
  457. #define SLIDEDOWN_TRANSITION(name, type, div) \
  458. static void slidedown##name##_transition(AVFilterContext *ctx, \
  459. const AVFrame *a, const AVFrame *b, AVFrame *out, \
  460. float progress, \
  461. int slice_start, int slice_end, int jobnr) \
  462. { \
  463. XFadeContext *s = ctx->priv; \
  464. const int height = out->height; \
  465. const int z = progress * height; \
  466. \
  467. for (int p = 0; p < s->nb_planes; p++) { \
  468. type *dst = (type *)(out->data[p] + slice_start * out->linesize[p]); \
  469. \
  470. for (int y = slice_start; y < slice_end; y++) { \
  471. const int zy = z + y; \
  472. const int zz = zy % height + height * (zy < 0); \
  473. const type *xf0 = (const type *)(a->data[p] + zz * a->linesize[p]); \
  474. const type *xf1 = (const type *)(b->data[p] + zz * b->linesize[p]); \
  475. \
  476. for (int x = 0; x < out->width; x++) { \
  477. dst[x] = (zy > 0) && (zy < height) ? xf1[x] : xf0[x]; \
  478. } \
  479. \
  480. dst += out->linesize[p] / div; \
  481. } \
  482. } \
  483. }
  484. SLIDEDOWN_TRANSITION(8, uint8_t, 1)
  485. SLIDEDOWN_TRANSITION(16, uint16_t, 2)
  486. #define CIRCLECROP_TRANSITION(name, type, div) \
  487. static void circlecrop##name##_transition(AVFilterContext *ctx, \
  488. const AVFrame *a, const AVFrame *b, AVFrame *out, \
  489. float progress, \
  490. int slice_start, int slice_end, int jobnr) \
  491. { \
  492. XFadeContext *s = ctx->priv; \
  493. const int width = out->width; \
  494. const int height = out->height; \
  495. float z = powf(2.f * fabsf(progress - 0.5f), 3.f) * hypotf(width/2, height/2); \
  496. \
  497. for (int p = 0; p < s->nb_planes; p++) { \
  498. const int bg = s->black[p]; \
  499. type *dst = (type *)(out->data[p] + slice_start * out->linesize[p]); \
  500. \
  501. for (int y = slice_start; y < slice_end; y++) { \
  502. const type *xf0 = (const type *)(a->data[p] + y * a->linesize[p]); \
  503. const type *xf1 = (const type *)(b->data[p] + y * b->linesize[p]); \
  504. \
  505. for (int x = 0; x < width; x++) { \
  506. float dist = hypotf(x - width / 2, y - height / 2); \
  507. int val = progress < 0.5f ? xf1[x] : xf0[x]; \
  508. dst[x] = (z < dist) ? bg : val; \
  509. } \
  510. \
  511. dst += out->linesize[p] / div; \
  512. } \
  513. } \
  514. }
  515. CIRCLECROP_TRANSITION(8, uint8_t, 1)
  516. CIRCLECROP_TRANSITION(16, uint16_t, 2)
  517. #define RECTCROP_TRANSITION(name, type, div) \
  518. static void rectcrop##name##_transition(AVFilterContext *ctx, \
  519. const AVFrame *a, const AVFrame *b, AVFrame *out, \
  520. float progress, \
  521. int slice_start, int slice_end, int jobnr) \
  522. { \
  523. XFadeContext *s = ctx->priv; \
  524. const int width = out->width; \
  525. const int height = out->height; \
  526. int zh = fabsf(progress - 0.5f) * height; \
  527. int zw = fabsf(progress - 0.5f) * width; \
  528. \
  529. for (int p = 0; p < s->nb_planes; p++) { \
  530. const int bg = s->black[p]; \
  531. type *dst = (type *)(out->data[p] + slice_start * out->linesize[p]); \
  532. \
  533. for (int y = slice_start; y < slice_end; y++) { \
  534. const type *xf0 = (const type *)(a->data[p] + y * a->linesize[p]); \
  535. const type *xf1 = (const type *)(b->data[p] + y * b->linesize[p]); \
  536. \
  537. for (int x = 0; x < width; x++) { \
  538. int dist = FFABS(x - width / 2) < zw && \
  539. FFABS(y - height / 2) < zh; \
  540. int val = progress < 0.5f ? xf1[x] : xf0[x]; \
  541. dst[x] = !dist ? bg : val; \
  542. } \
  543. \
  544. dst += out->linesize[p] / div; \
  545. } \
  546. } \
  547. }
  548. RECTCROP_TRANSITION(8, uint8_t, 1)
  549. RECTCROP_TRANSITION(16, uint16_t, 2)
  550. #define DISTANCE_TRANSITION(name, type, div) \
  551. static void distance##name##_transition(AVFilterContext *ctx, \
  552. const AVFrame *a, const AVFrame *b, AVFrame *out, \
  553. float progress, \
  554. int slice_start, int slice_end, int jobnr) \
  555. { \
  556. XFadeContext *s = ctx->priv; \
  557. const int width = out->width; \
  558. const float max = s->max_value; \
  559. \
  560. for (int y = slice_start; y < slice_end; y++) { \
  561. for (int x = 0; x < width; x++) { \
  562. float dist = 0.f; \
  563. for (int p = 0; p < s->nb_planes; p++) { \
  564. const type *xf0 = (const type *)(a->data[p] + y * a->linesize[p]); \
  565. const type *xf1 = (const type *)(b->data[p] + y * b->linesize[p]); \
  566. \
  567. dist += (xf0[x] / max - xf1[x] / max) * \
  568. (xf0[x] / max - xf1[x] / max); \
  569. } \
  570. \
  571. dist = sqrtf(dist) <= progress; \
  572. for (int p = 0; p < s->nb_planes; p++) { \
  573. const type *xf0 = (const type *)(a->data[p] + y * a->linesize[p]); \
  574. const type *xf1 = (const type *)(b->data[p] + y * b->linesize[p]); \
  575. type *dst = (type *)(out->data[p] + y * out->linesize[p]); \
  576. dst[x] = mix(mix(xf0[x], xf1[x], dist), xf1[x], progress); \
  577. } \
  578. } \
  579. } \
  580. }
  581. DISTANCE_TRANSITION(8, uint8_t, 1)
  582. DISTANCE_TRANSITION(16, uint16_t, 2)
  583. #define FADEBLACK_TRANSITION(name, type, div) \
  584. static void fadeblack##name##_transition(AVFilterContext *ctx, \
  585. const AVFrame *a, const AVFrame *b, AVFrame *out, \
  586. float progress, \
  587. int slice_start, int slice_end, int jobnr) \
  588. { \
  589. XFadeContext *s = ctx->priv; \
  590. const int height = slice_end - slice_start; \
  591. const float phase = 0.2f; \
  592. \
  593. for (int p = 0; p < s->nb_planes; p++) { \
  594. const type *xf0 = (const type *)(a->data[p] + slice_start * a->linesize[p]); \
  595. const type *xf1 = (const type *)(b->data[p] + slice_start * b->linesize[p]); \
  596. type *dst = (type *)(out->data[p] + slice_start * out->linesize[p]); \
  597. const int bg = s->black[p]; \
  598. \
  599. for (int y = 0; y < height; y++) { \
  600. for (int x = 0; x < out->width; x++) { \
  601. dst[x] = mix(mix(xf0[x], bg, smoothstep(1.f-phase, 1.f, progress)), \
  602. mix(bg, xf1[x], smoothstep(phase, 1.f, progress)), \
  603. progress); \
  604. } \
  605. \
  606. dst += out->linesize[p] / div; \
  607. xf0 += a->linesize[p] / div; \
  608. xf1 += b->linesize[p] / div; \
  609. } \
  610. } \
  611. }
  612. FADEBLACK_TRANSITION(8, uint8_t, 1)
  613. FADEBLACK_TRANSITION(16, uint16_t, 2)
  614. #define FADEWHITE_TRANSITION(name, type, div) \
  615. static void fadewhite##name##_transition(AVFilterContext *ctx, \
  616. const AVFrame *a, const AVFrame *b, AVFrame *out, \
  617. float progress, \
  618. int slice_start, int slice_end, int jobnr) \
  619. { \
  620. XFadeContext *s = ctx->priv; \
  621. const int height = slice_end - slice_start; \
  622. const float phase = 0.2f; \
  623. \
  624. for (int p = 0; p < s->nb_planes; p++) { \
  625. const type *xf0 = (const type *)(a->data[p] + slice_start * a->linesize[p]); \
  626. const type *xf1 = (const type *)(b->data[p] + slice_start * b->linesize[p]); \
  627. type *dst = (type *)(out->data[p] + slice_start * out->linesize[p]); \
  628. const int bg = s->white[p]; \
  629. \
  630. for (int y = 0; y < height; y++) { \
  631. for (int x = 0; x < out->width; x++) { \
  632. dst[x] = mix(mix(xf0[x], bg, smoothstep(1.f-phase, 1.f, progress)), \
  633. mix(bg, xf1[x], smoothstep(phase, 1.f, progress)), \
  634. progress); \
  635. } \
  636. \
  637. dst += out->linesize[p] / div; \
  638. xf0 += a->linesize[p] / div; \
  639. xf1 += b->linesize[p] / div; \
  640. } \
  641. } \
  642. }
  643. FADEWHITE_TRANSITION(8, uint8_t, 1)
  644. FADEWHITE_TRANSITION(16, uint16_t, 2)
  645. #define RADIAL_TRANSITION(name, type, div) \
  646. static void radial##name##_transition(AVFilterContext *ctx, \
  647. const AVFrame *a, const AVFrame *b, AVFrame *out, \
  648. float progress, \
  649. int slice_start, int slice_end, int jobnr) \
  650. { \
  651. XFadeContext *s = ctx->priv; \
  652. const int width = out->width; \
  653. const int height = out->height; \
  654. \
  655. for (int y = slice_start; y < slice_end; y++) { \
  656. for (int x = 0; x < width; x++) { \
  657. const float smooth = atan2f(x - width / 2, y - height / 2) - \
  658. (progress - 0.5f) * (M_PI * 2.5f); \
  659. for (int p = 0; p < s->nb_planes; p++) { \
  660. const type *xf0 = (const type *)(a->data[p] + y * a->linesize[p]); \
  661. const type *xf1 = (const type *)(b->data[p] + y * b->linesize[p]); \
  662. type *dst = (type *)(out->data[p] + y * out->linesize[p]); \
  663. \
  664. dst[x] = mix(xf1[x], xf0[x], smoothstep(0.f, 1.f, smooth)); \
  665. } \
  666. } \
  667. } \
  668. }
  669. RADIAL_TRANSITION(8, uint8_t, 1)
  670. RADIAL_TRANSITION(16, uint16_t, 2)
  671. #define SMOOTHLEFT_TRANSITION(name, type, div) \
  672. static void smoothleft##name##_transition(AVFilterContext *ctx, \
  673. const AVFrame *a, const AVFrame *b, AVFrame *out, \
  674. float progress, \
  675. int slice_start, int slice_end, int jobnr) \
  676. { \
  677. XFadeContext *s = ctx->priv; \
  678. const int width = out->width; \
  679. const float w = width; \
  680. \
  681. for (int y = slice_start; y < slice_end; y++) { \
  682. for (int x = 0; x < width; x++) { \
  683. const float smooth = 1.f + x / w - progress * 2.f; \
  684. \
  685. for (int p = 0; p < s->nb_planes; p++) { \
  686. const type *xf0 = (const type *)(a->data[p] + y * a->linesize[p]); \
  687. const type *xf1 = (const type *)(b->data[p] + y * b->linesize[p]); \
  688. type *dst = (type *)(out->data[p] + y * out->linesize[p]); \
  689. \
  690. dst[x] = mix(xf1[x], xf0[x], smoothstep(0.f, 1.f, smooth)); \
  691. } \
  692. } \
  693. } \
  694. }
  695. SMOOTHLEFT_TRANSITION(8, uint8_t, 1)
  696. SMOOTHLEFT_TRANSITION(16, uint16_t, 2)
  697. #define SMOOTHRIGHT_TRANSITION(name, type, div) \
  698. static void smoothright##name##_transition(AVFilterContext *ctx, \
  699. const AVFrame *a, const AVFrame *b, AVFrame *out, \
  700. float progress, \
  701. int slice_start, int slice_end, int jobnr) \
  702. { \
  703. XFadeContext *s = ctx->priv; \
  704. const int width = out->width; \
  705. const float w = width; \
  706. \
  707. for (int y = slice_start; y < slice_end; y++) { \
  708. for (int x = 0; x < width; x++) { \
  709. const float smooth = 1.f + (w - 1 - x) / w - progress * 2.f; \
  710. \
  711. for (int p = 0; p < s->nb_planes; p++) { \
  712. const type *xf0 = (const type *)(a->data[p] + y * a->linesize[p]); \
  713. const type *xf1 = (const type *)(b->data[p] + y * b->linesize[p]); \
  714. type *dst = (type *)(out->data[p] + y * out->linesize[p]); \
  715. \
  716. dst[x] = mix(xf1[x], xf0[x], smoothstep(0.f, 1.f, smooth)); \
  717. } \
  718. } \
  719. } \
  720. }
  721. SMOOTHRIGHT_TRANSITION(8, uint8_t, 1)
  722. SMOOTHRIGHT_TRANSITION(16, uint16_t, 2)
  723. #define SMOOTHUP_TRANSITION(name, type, div) \
  724. static void smoothup##name##_transition(AVFilterContext *ctx, \
  725. const AVFrame *a, const AVFrame *b, AVFrame *out, \
  726. float progress, \
  727. int slice_start, int slice_end, int jobnr) \
  728. { \
  729. XFadeContext *s = ctx->priv; \
  730. const int width = out->width; \
  731. const float h = out->height; \
  732. \
  733. for (int y = slice_start; y < slice_end; y++) { \
  734. const float smooth = 1.f + y / h - progress * 2.f; \
  735. for (int x = 0; x < width; x++) { \
  736. for (int p = 0; p < s->nb_planes; p++) { \
  737. const type *xf0 = (const type *)(a->data[p] + y * a->linesize[p]); \
  738. const type *xf1 = (const type *)(b->data[p] + y * b->linesize[p]); \
  739. type *dst = (type *)(out->data[p] + y * out->linesize[p]); \
  740. \
  741. dst[x] = mix(xf1[x], xf0[x], smoothstep(0.f, 1.f, smooth)); \
  742. } \
  743. } \
  744. } \
  745. }
  746. SMOOTHUP_TRANSITION(8, uint8_t, 1)
  747. SMOOTHUP_TRANSITION(16, uint16_t, 2)
  748. #define SMOOTHDOWN_TRANSITION(name, type, div) \
  749. static void smoothdown##name##_transition(AVFilterContext *ctx, \
  750. const AVFrame *a, const AVFrame *b, AVFrame *out, \
  751. float progress, \
  752. int slice_start, int slice_end, int jobnr) \
  753. { \
  754. XFadeContext *s = ctx->priv; \
  755. const int width = out->width; \
  756. const float h = out->height; \
  757. \
  758. for (int y = slice_start; y < slice_end; y++) { \
  759. const float smooth = 1.f + (h - 1 - y) / h - progress * 2.f; \
  760. for (int x = 0; x < width; x++) { \
  761. for (int p = 0; p < s->nb_planes; p++) { \
  762. const type *xf0 = (const type *)(a->data[p] + y * a->linesize[p]); \
  763. const type *xf1 = (const type *)(b->data[p] + y * b->linesize[p]); \
  764. type *dst = (type *)(out->data[p] + y * out->linesize[p]); \
  765. \
  766. dst[x] = mix(xf1[x], xf0[x], smoothstep(0.f, 1.f, smooth)); \
  767. } \
  768. } \
  769. } \
  770. }
  771. SMOOTHDOWN_TRANSITION(8, uint8_t, 1)
  772. SMOOTHDOWN_TRANSITION(16, uint16_t, 2)
  773. #define CIRCLEOPEN_TRANSITION(name, type, div) \
  774. static void circleopen##name##_transition(AVFilterContext *ctx, \
  775. const AVFrame *a, const AVFrame *b, AVFrame *out, \
  776. float progress, \
  777. int slice_start, int slice_end, int jobnr) \
  778. { \
  779. XFadeContext *s = ctx->priv; \
  780. const int width = out->width; \
  781. const int height = out->height; \
  782. const float z = hypotf(width / 2, height / 2); \
  783. const float p = (progress - 0.5f) * 3.f; \
  784. \
  785. for (int y = slice_start; y < slice_end; y++) { \
  786. for (int x = 0; x < width; x++) { \
  787. const float smooth = hypotf(x - width / 2, y - height / 2) / z + p; \
  788. for (int p = 0; p < s->nb_planes; p++) { \
  789. const type *xf0 = (const type *)(a->data[p] + y * a->linesize[p]); \
  790. const type *xf1 = (const type *)(b->data[p] + y * b->linesize[p]); \
  791. type *dst = (type *)(out->data[p] + y * out->linesize[p]); \
  792. \
  793. dst[x] = mix(xf0[x], xf1[x], smoothstep(0.f, 1.f, smooth)); \
  794. } \
  795. } \
  796. } \
  797. }
  798. CIRCLEOPEN_TRANSITION(8, uint8_t, 1)
  799. CIRCLEOPEN_TRANSITION(16, uint16_t, 2)
  800. #define CIRCLECLOSE_TRANSITION(name, type, div) \
  801. static void circleclose##name##_transition(AVFilterContext *ctx, \
  802. const AVFrame *a, const AVFrame *b, AVFrame *out, \
  803. float progress, \
  804. int slice_start, int slice_end, int jobnr) \
  805. { \
  806. XFadeContext *s = ctx->priv; \
  807. const int width = out->width; \
  808. const int height = out->height; \
  809. const float z = hypotf(width / 2, height / 2); \
  810. const float p = (1.f - progress - 0.5f) * 3.f; \
  811. \
  812. for (int y = slice_start; y < slice_end; y++) { \
  813. for (int x = 0; x < width; x++) { \
  814. const float smooth = hypotf(x - width / 2, y - height / 2) / z + p; \
  815. for (int p = 0; p < s->nb_planes; p++) { \
  816. const type *xf0 = (const type *)(a->data[p] + y * a->linesize[p]); \
  817. const type *xf1 = (const type *)(b->data[p] + y * b->linesize[p]); \
  818. type *dst = (type *)(out->data[p] + y * out->linesize[p]); \
  819. \
  820. dst[x] = mix(xf1[x], xf0[x], smoothstep(0.f, 1.f, smooth)); \
  821. } \
  822. } \
  823. } \
  824. }
  825. CIRCLECLOSE_TRANSITION(8, uint8_t, 1)
  826. CIRCLECLOSE_TRANSITION(16, uint16_t, 2)
  827. #define VERTOPEN_TRANSITION(name, type, div) \
  828. static void vertopen##name##_transition(AVFilterContext *ctx, \
  829. const AVFrame *a, const AVFrame *b, AVFrame *out, \
  830. float progress, \
  831. int slice_start, int slice_end, int jobnr) \
  832. { \
  833. XFadeContext *s = ctx->priv; \
  834. const int width = out->width; \
  835. const float w2 = out->width / 2; \
  836. \
  837. for (int y = slice_start; y < slice_end; y++) { \
  838. for (int x = 0; x < width; x++) { \
  839. const float smooth = 2.f - fabsf((x - w2) / w2) - progress * 2.f; \
  840. for (int p = 0; p < s->nb_planes; p++) { \
  841. const type *xf0 = (const type *)(a->data[p] + y * a->linesize[p]); \
  842. const type *xf1 = (const type *)(b->data[p] + y * b->linesize[p]); \
  843. type *dst = (type *)(out->data[p] + y * out->linesize[p]); \
  844. \
  845. dst[x] = mix(xf1[x], xf0[x], smoothstep(0.f, 1.f, smooth)); \
  846. } \
  847. } \
  848. } \
  849. }
  850. VERTOPEN_TRANSITION(8, uint8_t, 1)
  851. VERTOPEN_TRANSITION(16, uint16_t, 2)
  852. #define VERTCLOSE_TRANSITION(name, type, div) \
  853. static void vertclose##name##_transition(AVFilterContext *ctx, \
  854. const AVFrame *a, const AVFrame *b, AVFrame *out, \
  855. float progress, \
  856. int slice_start, int slice_end, int jobnr) \
  857. { \
  858. XFadeContext *s = ctx->priv; \
  859. const int width = out->width; \
  860. const float w2 = out->width / 2; \
  861. \
  862. for (int y = slice_start; y < slice_end; y++) { \
  863. for (int x = 0; x < width; x++) { \
  864. const float smooth = 1.f + fabsf((x - w2) / w2) - progress * 2.f; \
  865. for (int p = 0; p < s->nb_planes; p++) { \
  866. const type *xf0 = (const type *)(a->data[p] + y * a->linesize[p]); \
  867. const type *xf1 = (const type *)(b->data[p] + y * b->linesize[p]); \
  868. type *dst = (type *)(out->data[p] + y * out->linesize[p]); \
  869. \
  870. dst[x] = mix(xf1[x], xf0[x], smoothstep(0.f, 1.f, smooth)); \
  871. } \
  872. } \
  873. } \
  874. }
  875. VERTCLOSE_TRANSITION(8, uint8_t, 1)
  876. VERTCLOSE_TRANSITION(16, uint16_t, 2)
  877. #define HORZOPEN_TRANSITION(name, type, div) \
  878. static void horzopen##name##_transition(AVFilterContext *ctx, \
  879. const AVFrame *a, const AVFrame *b, AVFrame *out, \
  880. float progress, \
  881. int slice_start, int slice_end, int jobnr) \
  882. { \
  883. XFadeContext *s = ctx->priv; \
  884. const int width = out->width; \
  885. const float h2 = out->height / 2; \
  886. \
  887. for (int y = slice_start; y < slice_end; y++) { \
  888. const float smooth = 2.f - fabsf((y - h2) / h2) - progress * 2.f; \
  889. for (int x = 0; x < width; x++) { \
  890. for (int p = 0; p < s->nb_planes; p++) { \
  891. const type *xf0 = (const type *)(a->data[p] + y * a->linesize[p]); \
  892. const type *xf1 = (const type *)(b->data[p] + y * b->linesize[p]); \
  893. type *dst = (type *)(out->data[p] + y * out->linesize[p]); \
  894. \
  895. dst[x] = mix(xf1[x], xf0[x], smoothstep(0.f, 1.f, smooth)); \
  896. } \
  897. } \
  898. } \
  899. }
  900. HORZOPEN_TRANSITION(8, uint8_t, 1)
  901. HORZOPEN_TRANSITION(16, uint16_t, 2)
  902. #define HORZCLOSE_TRANSITION(name, type, div) \
  903. static void horzclose##name##_transition(AVFilterContext *ctx, \
  904. const AVFrame *a, const AVFrame *b, AVFrame *out, \
  905. float progress, \
  906. int slice_start, int slice_end, int jobnr) \
  907. { \
  908. XFadeContext *s = ctx->priv; \
  909. const int width = out->width; \
  910. const float h2 = out->height / 2; \
  911. \
  912. for (int y = slice_start; y < slice_end; y++) { \
  913. const float smooth = 1.f + fabsf((y - h2) / h2) - progress * 2.f; \
  914. for (int x = 0; x < width; x++) { \
  915. for (int p = 0; p < s->nb_planes; p++) { \
  916. const type *xf0 = (const type *)(a->data[p] + y * a->linesize[p]); \
  917. const type *xf1 = (const type *)(b->data[p] + y * b->linesize[p]); \
  918. type *dst = (type *)(out->data[p] + y * out->linesize[p]); \
  919. \
  920. dst[x] = mix(xf1[x], xf0[x], smoothstep(0.f, 1.f, smooth)); \
  921. } \
  922. } \
  923. } \
  924. }
  925. HORZCLOSE_TRANSITION(8, uint8_t, 1)
  926. HORZCLOSE_TRANSITION(16, uint16_t, 2)
  927. static float frand(int x, int y)
  928. {
  929. const float r = sinf(x * 12.9898f + y * 78.233f) * 43758.545f;
  930. return r - floorf(r);
  931. }
  932. #define DISSOLVE_TRANSITION(name, type, div) \
  933. static void dissolve##name##_transition(AVFilterContext *ctx, \
  934. const AVFrame *a, const AVFrame *b, AVFrame *out, \
  935. float progress, \
  936. int slice_start, int slice_end, int jobnr) \
  937. { \
  938. XFadeContext *s = ctx->priv; \
  939. const int width = out->width; \
  940. \
  941. for (int y = slice_start; y < slice_end; y++) { \
  942. for (int x = 0; x < width; x++) { \
  943. const float smooth = frand(x, y) * 2.f + progress * 2.f - 1.5f; \
  944. for (int p = 0; p < s->nb_planes; p++) { \
  945. const type *xf0 = (const type *)(a->data[p] + y * a->linesize[p]); \
  946. const type *xf1 = (const type *)(b->data[p] + y * b->linesize[p]); \
  947. type *dst = (type *)(out->data[p] + y * out->linesize[p]); \
  948. \
  949. dst[x] = smooth >= 0.5f ? xf0[x] : xf1[x]; \
  950. } \
  951. } \
  952. } \
  953. }
  954. DISSOLVE_TRANSITION(8, uint8_t, 1)
  955. DISSOLVE_TRANSITION(16, uint16_t, 2)
  956. #define PIXELIZE_TRANSITION(name, type, div) \
  957. static void pixelize##name##_transition(AVFilterContext *ctx, \
  958. const AVFrame *a, const AVFrame *b, AVFrame *out, \
  959. float progress, \
  960. int slice_start, int slice_end, int jobnr) \
  961. { \
  962. XFadeContext *s = ctx->priv; \
  963. const int w = out->width; \
  964. const int h = out->height; \
  965. const float d = fminf(progress, 1.f - progress); \
  966. const float dist = ceilf(d * 50.f) / 50.f; \
  967. const float sqx = 2.f * dist * FFMIN(w, h) / 20.f; \
  968. const float sqy = 2.f * dist * FFMIN(w, h) / 20.f; \
  969. \
  970. for (int y = slice_start; y < slice_end; y++) { \
  971. for (int x = 0; x < w; x++) { \
  972. int sx = dist > 0.f ? FFMIN((floorf(x / sqx) + .5f) * sqx, w - 1) : x; \
  973. int sy = dist > 0.f ? FFMIN((floorf(y / sqy) + .5f) * sqy, h - 1) : y; \
  974. for (int p = 0; p < s->nb_planes; p++) { \
  975. const type *xf0 = (const type *)(a->data[p] + sy * a->linesize[p]); \
  976. const type *xf1 = (const type *)(b->data[p] + sy * b->linesize[p]); \
  977. type *dst = (type *)(out->data[p] + y * out->linesize[p]); \
  978. \
  979. dst[x] = mix(xf0[sx], xf1[sx], progress); \
  980. } \
  981. } \
  982. } \
  983. }
  984. PIXELIZE_TRANSITION(8, uint8_t, 1)
  985. PIXELIZE_TRANSITION(16, uint16_t, 2)
  986. #define DIAGTL_TRANSITION(name, type, div) \
  987. static void diagtl##name##_transition(AVFilterContext *ctx, \
  988. const AVFrame *a, const AVFrame *b, AVFrame *out, \
  989. float progress, \
  990. int slice_start, int slice_end, int jobnr) \
  991. { \
  992. XFadeContext *s = ctx->priv; \
  993. const int width = out->width; \
  994. const float w = width; \
  995. const float h = out->height; \
  996. \
  997. for (int y = slice_start; y < slice_end; y++) { \
  998. for (int x = 0; x < width; x++) { \
  999. const float smooth = 1.f + x / w * y / h - progress * 2.f; \
  1000. \
  1001. for (int p = 0; p < s->nb_planes; p++) { \
  1002. const type *xf0 = (const type *)(a->data[p] + y * a->linesize[p]); \
  1003. const type *xf1 = (const type *)(b->data[p] + y * b->linesize[p]); \
  1004. type *dst = (type *)(out->data[p] + y * out->linesize[p]); \
  1005. \
  1006. dst[x] = mix(xf1[x], xf0[x], smoothstep(0.f, 1.f, smooth)); \
  1007. } \
  1008. } \
  1009. } \
  1010. }
  1011. DIAGTL_TRANSITION(8, uint8_t, 1)
  1012. DIAGTL_TRANSITION(16, uint16_t, 2)
  1013. #define DIAGTR_TRANSITION(name, type, div) \
  1014. static void diagtr##name##_transition(AVFilterContext *ctx, \
  1015. const AVFrame *a, const AVFrame *b, AVFrame *out, \
  1016. float progress, \
  1017. int slice_start, int slice_end, int jobnr) \
  1018. { \
  1019. XFadeContext *s = ctx->priv; \
  1020. const int width = out->width; \
  1021. const float w = width; \
  1022. const float h = out->height; \
  1023. \
  1024. for (int y = slice_start; y < slice_end; y++) { \
  1025. for (int x = 0; x < width; x++) { \
  1026. const float smooth = 1.f + (w - 1 - x) / w * y / h - progress * 2.f; \
  1027. \
  1028. for (int p = 0; p < s->nb_planes; p++) { \
  1029. const type *xf0 = (const type *)(a->data[p] + y * a->linesize[p]); \
  1030. const type *xf1 = (const type *)(b->data[p] + y * b->linesize[p]); \
  1031. type *dst = (type *)(out->data[p] + y * out->linesize[p]); \
  1032. \
  1033. dst[x] = mix(xf1[x], xf0[x], smoothstep(0.f, 1.f, smooth)); \
  1034. } \
  1035. } \
  1036. } \
  1037. }
  1038. DIAGTR_TRANSITION(8, uint8_t, 1)
  1039. DIAGTR_TRANSITION(16, uint16_t, 2)
  1040. #define DIAGBL_TRANSITION(name, type, div) \
  1041. static void diagbl##name##_transition(AVFilterContext *ctx, \
  1042. const AVFrame *a, const AVFrame *b, AVFrame *out, \
  1043. float progress, \
  1044. int slice_start, int slice_end, int jobnr) \
  1045. { \
  1046. XFadeContext *s = ctx->priv; \
  1047. const int width = out->width; \
  1048. const float w = width; \
  1049. const float h = out->height; \
  1050. \
  1051. for (int y = slice_start; y < slice_end; y++) { \
  1052. for (int x = 0; x < width; x++) { \
  1053. const float smooth = 1.f + x / w * (h - 1 - y) / h - progress * 2.f; \
  1054. \
  1055. for (int p = 0; p < s->nb_planes; p++) { \
  1056. const type *xf0 = (const type *)(a->data[p] + y * a->linesize[p]); \
  1057. const type *xf1 = (const type *)(b->data[p] + y * b->linesize[p]); \
  1058. type *dst = (type *)(out->data[p] + y * out->linesize[p]); \
  1059. \
  1060. dst[x] = mix(xf1[x], xf0[x], smoothstep(0.f, 1.f, smooth)); \
  1061. } \
  1062. } \
  1063. } \
  1064. }
  1065. DIAGBL_TRANSITION(8, uint8_t, 1)
  1066. DIAGBL_TRANSITION(16, uint16_t, 2)
  1067. #define DIAGBR_TRANSITION(name, type, div) \
  1068. static void diagbr##name##_transition(AVFilterContext *ctx, \
  1069. const AVFrame *a, const AVFrame *b, AVFrame *out, \
  1070. float progress, \
  1071. int slice_start, int slice_end, int jobnr) \
  1072. { \
  1073. XFadeContext *s = ctx->priv; \
  1074. const int width = out->width; \
  1075. const float w = width; \
  1076. const float h = out->height; \
  1077. \
  1078. for (int y = slice_start; y < slice_end; y++) { \
  1079. for (int x = 0; x < width; x++) { \
  1080. const float smooth = 1.f + (w - 1 - x) / w * (h - 1 - y) / h - \
  1081. progress * 2.f; \
  1082. \
  1083. for (int p = 0; p < s->nb_planes; p++) { \
  1084. const type *xf0 = (const type *)(a->data[p] + y * a->linesize[p]); \
  1085. const type *xf1 = (const type *)(b->data[p] + y * b->linesize[p]); \
  1086. type *dst = (type *)(out->data[p] + y * out->linesize[p]); \
  1087. \
  1088. dst[x] = mix(xf1[x], xf0[x], smoothstep(0.f, 1.f, smooth)); \
  1089. } \
  1090. } \
  1091. } \
  1092. }
  1093. DIAGBR_TRANSITION(8, uint8_t, 1)
  1094. DIAGBR_TRANSITION(16, uint16_t, 2)
  1095. #define HLSLICE_TRANSITION(name, type, div) \
  1096. static void hlslice##name##_transition(AVFilterContext *ctx, \
  1097. const AVFrame *a, const AVFrame *b, AVFrame *out, \
  1098. float progress, \
  1099. int slice_start, int slice_end, int jobnr) \
  1100. { \
  1101. XFadeContext *s = ctx->priv; \
  1102. const int width = out->width; \
  1103. const float w = width; \
  1104. \
  1105. for (int y = slice_start; y < slice_end; y++) { \
  1106. for (int x = 0; x < width; x++) { \
  1107. const float smooth = smoothstep(-0.5f, 0.f, x / w - progress * 1.5f); \
  1108. const float ss = smooth <= fract(10.f * x / w) ? 0.f : 1.f; \
  1109. \
  1110. for (int p = 0; p < s->nb_planes; p++) { \
  1111. const type *xf0 = (const type *)(a->data[p] + y * a->linesize[p]); \
  1112. const type *xf1 = (const type *)(b->data[p] + y * b->linesize[p]); \
  1113. type *dst = (type *)(out->data[p] + y * out->linesize[p]); \
  1114. \
  1115. dst[x] = mix(xf1[x], xf0[x], ss); \
  1116. } \
  1117. } \
  1118. } \
  1119. }
  1120. HLSLICE_TRANSITION(8, uint8_t, 1)
  1121. HLSLICE_TRANSITION(16, uint16_t, 2)
  1122. #define HRSLICE_TRANSITION(name, type, div) \
  1123. static void hrslice##name##_transition(AVFilterContext *ctx, \
  1124. const AVFrame *a, const AVFrame *b, AVFrame *out, \
  1125. float progress, \
  1126. int slice_start, int slice_end, int jobnr) \
  1127. { \
  1128. XFadeContext *s = ctx->priv; \
  1129. const int width = out->width; \
  1130. const float w = width; \
  1131. \
  1132. for (int y = slice_start; y < slice_end; y++) { \
  1133. for (int x = 0; x < width; x++) { \
  1134. const float xx = (w - 1 - x) / w; \
  1135. const float smooth = smoothstep(-0.5f, 0.f, xx - progress * 1.5f); \
  1136. const float ss = smooth <= fract(10.f * xx) ? 0.f : 1.f; \
  1137. \
  1138. for (int p = 0; p < s->nb_planes; p++) { \
  1139. const type *xf0 = (const type *)(a->data[p] + y * a->linesize[p]); \
  1140. const type *xf1 = (const type *)(b->data[p] + y * b->linesize[p]); \
  1141. type *dst = (type *)(out->data[p] + y * out->linesize[p]); \
  1142. \
  1143. dst[x] = mix(xf1[x], xf0[x], ss); \
  1144. } \
  1145. } \
  1146. } \
  1147. }
  1148. HRSLICE_TRANSITION(8, uint8_t, 1)
  1149. HRSLICE_TRANSITION(16, uint16_t, 2)
  1150. #define VUSLICE_TRANSITION(name, type, div) \
  1151. static void vuslice##name##_transition(AVFilterContext *ctx, \
  1152. const AVFrame *a, const AVFrame *b, AVFrame *out, \
  1153. float progress, \
  1154. int slice_start, int slice_end, int jobnr) \
  1155. { \
  1156. XFadeContext *s = ctx->priv; \
  1157. const int width = out->width; \
  1158. const float h = out->height; \
  1159. \
  1160. for (int y = slice_start; y < slice_end; y++) { \
  1161. const float smooth = smoothstep(-0.5f, 0.f, y / h - progress * 1.5f); \
  1162. const float ss = smooth <= fract(10.f * y / h) ? 0.f : 1.f; \
  1163. \
  1164. for (int x = 0; x < width; x++) { \
  1165. for (int p = 0; p < s->nb_planes; p++) { \
  1166. const type *xf0 = (const type *)(a->data[p] + y * a->linesize[p]); \
  1167. const type *xf1 = (const type *)(b->data[p] + y * b->linesize[p]); \
  1168. type *dst = (type *)(out->data[p] + y * out->linesize[p]); \
  1169. \
  1170. dst[x] = mix(xf1[x], xf0[x], ss); \
  1171. } \
  1172. } \
  1173. } \
  1174. }
  1175. VUSLICE_TRANSITION(8, uint8_t, 1)
  1176. VUSLICE_TRANSITION(16, uint16_t, 2)
  1177. #define VDSLICE_TRANSITION(name, type, div) \
  1178. static void vdslice##name##_transition(AVFilterContext *ctx, \
  1179. const AVFrame *a, const AVFrame *b, AVFrame *out, \
  1180. float progress, \
  1181. int slice_start, int slice_end, int jobnr) \
  1182. { \
  1183. XFadeContext *s = ctx->priv; \
  1184. const int width = out->width; \
  1185. const float h = out->height; \
  1186. \
  1187. for (int y = slice_start; y < slice_end; y++) { \
  1188. const float yy = (h - 1 - y) / h; \
  1189. const float smooth = smoothstep(-0.5f, 0.f, yy - progress * 1.5f); \
  1190. const float ss = smooth <= fract(10.f * yy) ? 0.f : 1.f; \
  1191. \
  1192. for (int x = 0; x < width; x++) { \
  1193. for (int p = 0; p < s->nb_planes; p++) { \
  1194. const type *xf0 = (const type *)(a->data[p] + y * a->linesize[p]); \
  1195. const type *xf1 = (const type *)(b->data[p] + y * b->linesize[p]); \
  1196. type *dst = (type *)(out->data[p] + y * out->linesize[p]); \
  1197. \
  1198. dst[x] = mix(xf1[x], xf0[x], ss); \
  1199. } \
  1200. } \
  1201. } \
  1202. }
  1203. VDSLICE_TRANSITION(8, uint8_t, 1)
  1204. VDSLICE_TRANSITION(16, uint16_t, 2)
  1205. static inline double getpix(void *priv, double x, double y, int plane, int nb)
  1206. {
  1207. XFadeContext *s = priv;
  1208. AVFrame *in = s->xf[nb];
  1209. const uint8_t *src = in->data[FFMIN(plane, s->nb_planes - 1)];
  1210. int linesize = in->linesize[FFMIN(plane, s->nb_planes - 1)];
  1211. const int w = in->width;
  1212. const int h = in->height;
  1213. int xi, yi;
  1214. xi = av_clipd(x, 0, w - 1);
  1215. yi = av_clipd(y, 0, h - 1);
  1216. if (s->depth > 8) {
  1217. const uint16_t *src16 = (const uint16_t*)src;
  1218. linesize /= 2;
  1219. return src16[xi + yi * linesize];
  1220. } else {
  1221. return src[xi + yi * linesize];
  1222. }
  1223. }
  1224. static double a0(void *priv, double x, double y) { return getpix(priv, x, y, 0, 0); }
  1225. static double a1(void *priv, double x, double y) { return getpix(priv, x, y, 1, 0); }
  1226. static double a2(void *priv, double x, double y) { return getpix(priv, x, y, 2, 0); }
  1227. static double a3(void *priv, double x, double y) { return getpix(priv, x, y, 3, 0); }
  1228. static double b0(void *priv, double x, double y) { return getpix(priv, x, y, 0, 1); }
  1229. static double b1(void *priv, double x, double y) { return getpix(priv, x, y, 1, 1); }
  1230. static double b2(void *priv, double x, double y) { return getpix(priv, x, y, 2, 1); }
  1231. static double b3(void *priv, double x, double y) { return getpix(priv, x, y, 3, 1); }
  1232. static int config_output(AVFilterLink *outlink)
  1233. {
  1234. AVFilterContext *ctx = outlink->src;
  1235. AVFilterLink *inlink0 = ctx->inputs[0];
  1236. AVFilterLink *inlink1 = ctx->inputs[1];
  1237. XFadeContext *s = ctx->priv;
  1238. const AVPixFmtDescriptor *pix_desc = av_pix_fmt_desc_get(inlink0->format);
  1239. int is_rgb;
  1240. if (inlink0->format != inlink1->format) {
  1241. av_log(ctx, AV_LOG_ERROR, "inputs must be of same pixel format\n");
  1242. return AVERROR(EINVAL);
  1243. }
  1244. if (inlink0->w != inlink1->w || inlink0->h != inlink1->h) {
  1245. av_log(ctx, AV_LOG_ERROR, "First input link %s parameters "
  1246. "(size %dx%d) do not match the corresponding "
  1247. "second input link %s parameters (size %dx%d)\n",
  1248. ctx->input_pads[0].name, inlink0->w, inlink0->h,
  1249. ctx->input_pads[1].name, inlink1->w, inlink1->h);
  1250. return AVERROR(EINVAL);
  1251. }
  1252. if (inlink0->time_base.num != inlink1->time_base.num ||
  1253. inlink0->time_base.den != inlink1->time_base.den) {
  1254. av_log(ctx, AV_LOG_ERROR, "First input link %s timebase "
  1255. "(%d/%d) do not match the corresponding "
  1256. "second input link %s timebase (%d/%d)\n",
  1257. ctx->input_pads[0].name, inlink0->time_base.num, inlink0->time_base.den,
  1258. ctx->input_pads[1].name, inlink1->time_base.num, inlink1->time_base.den);
  1259. return AVERROR(EINVAL);
  1260. }
  1261. outlink->w = inlink0->w;
  1262. outlink->h = inlink0->h;
  1263. outlink->time_base = inlink0->time_base;
  1264. outlink->sample_aspect_ratio = inlink0->sample_aspect_ratio;
  1265. outlink->frame_rate = inlink0->frame_rate;
  1266. s->depth = pix_desc->comp[0].depth;
  1267. is_rgb = !!(pix_desc->flags & AV_PIX_FMT_FLAG_RGB);
  1268. s->nb_planes = av_pix_fmt_count_planes(inlink0->format);
  1269. s->max_value = (1 << s->depth) - 1;
  1270. s->black[0] = 0;
  1271. s->black[1] = s->black[2] = is_rgb ? 0 : s->max_value / 2;
  1272. s->black[3] = s->max_value;
  1273. s->white[0] = s->white[3] = s->max_value;
  1274. s->white[1] = s->white[2] = is_rgb ? s->max_value : s->max_value / 2;
  1275. s->first_pts = s->last_pts = s->pts = AV_NOPTS_VALUE;
  1276. if (s->duration)
  1277. s->duration_pts = av_rescale_q(s->duration, AV_TIME_BASE_Q, outlink->time_base);
  1278. if (s->offset)
  1279. s->offset_pts = av_rescale_q(s->offset, AV_TIME_BASE_Q, outlink->time_base);
  1280. switch (s->transition) {
  1281. case CUSTOM: s->transitionf = s->depth <= 8 ? custom8_transition : custom16_transition; break;
  1282. case FADE: s->transitionf = s->depth <= 8 ? fade8_transition : fade16_transition; break;
  1283. case WIPELEFT: s->transitionf = s->depth <= 8 ? wipeleft8_transition : wipeleft16_transition; break;
  1284. case WIPERIGHT: s->transitionf = s->depth <= 8 ? wiperight8_transition : wiperight16_transition; break;
  1285. case WIPEUP: s->transitionf = s->depth <= 8 ? wipeup8_transition : wipeup16_transition; break;
  1286. case WIPEDOWN: s->transitionf = s->depth <= 8 ? wipedown8_transition : wipedown16_transition; break;
  1287. case SLIDELEFT: s->transitionf = s->depth <= 8 ? slideleft8_transition : slideleft16_transition; break;
  1288. case SLIDERIGHT: s->transitionf = s->depth <= 8 ? slideright8_transition : slideright16_transition; break;
  1289. case SLIDEUP: s->transitionf = s->depth <= 8 ? slideup8_transition : slideup16_transition; break;
  1290. case SLIDEDOWN: s->transitionf = s->depth <= 8 ? slidedown8_transition : slidedown16_transition; break;
  1291. case CIRCLECROP: s->transitionf = s->depth <= 8 ? circlecrop8_transition : circlecrop16_transition; break;
  1292. case RECTCROP: s->transitionf = s->depth <= 8 ? rectcrop8_transition : rectcrop16_transition; break;
  1293. case DISTANCE: s->transitionf = s->depth <= 8 ? distance8_transition : distance16_transition; break;
  1294. case FADEBLACK: s->transitionf = s->depth <= 8 ? fadeblack8_transition : fadeblack16_transition; break;
  1295. case FADEWHITE: s->transitionf = s->depth <= 8 ? fadewhite8_transition : fadewhite16_transition; break;
  1296. case RADIAL: s->transitionf = s->depth <= 8 ? radial8_transition : radial16_transition; break;
  1297. case SMOOTHLEFT: s->transitionf = s->depth <= 8 ? smoothleft8_transition : smoothleft16_transition; break;
  1298. case SMOOTHRIGHT:s->transitionf = s->depth <= 8 ? smoothright8_transition: smoothright16_transition;break;
  1299. case SMOOTHUP: s->transitionf = s->depth <= 8 ? smoothup8_transition : smoothup16_transition; break;
  1300. case SMOOTHDOWN: s->transitionf = s->depth <= 8 ? smoothdown8_transition : smoothdown16_transition; break;
  1301. case CIRCLEOPEN: s->transitionf = s->depth <= 8 ? circleopen8_transition : circleopen16_transition; break;
  1302. case CIRCLECLOSE:s->transitionf = s->depth <= 8 ? circleclose8_transition: circleclose16_transition;break;
  1303. case VERTOPEN: s->transitionf = s->depth <= 8 ? vertopen8_transition : vertopen16_transition; break;
  1304. case VERTCLOSE: s->transitionf = s->depth <= 8 ? vertclose8_transition : vertclose16_transition; break;
  1305. case HORZOPEN: s->transitionf = s->depth <= 8 ? horzopen8_transition : horzopen16_transition; break;
  1306. case HORZCLOSE: s->transitionf = s->depth <= 8 ? horzclose8_transition : horzclose16_transition; break;
  1307. case DISSOLVE: s->transitionf = s->depth <= 8 ? dissolve8_transition : dissolve16_transition; break;
  1308. case PIXELIZE: s->transitionf = s->depth <= 8 ? pixelize8_transition : pixelize16_transition; break;
  1309. case DIAGTL: s->transitionf = s->depth <= 8 ? diagtl8_transition : diagtl16_transition; break;
  1310. case DIAGTR: s->transitionf = s->depth <= 8 ? diagtr8_transition : diagtr16_transition; break;
  1311. case DIAGBL: s->transitionf = s->depth <= 8 ? diagbl8_transition : diagbl16_transition; break;
  1312. case DIAGBR: s->transitionf = s->depth <= 8 ? diagbr8_transition : diagbr16_transition; break;
  1313. case HLSLICE: s->transitionf = s->depth <= 8 ? hlslice8_transition : hlslice16_transition; break;
  1314. case HRSLICE: s->transitionf = s->depth <= 8 ? hrslice8_transition : hrslice16_transition; break;
  1315. case VUSLICE: s->transitionf = s->depth <= 8 ? vuslice8_transition : vuslice16_transition; break;
  1316. case VDSLICE: s->transitionf = s->depth <= 8 ? vdslice8_transition : vdslice16_transition; break;
  1317. }
  1318. if (s->transition == CUSTOM) {
  1319. static const char *const func2_names[] = {
  1320. "a0", "a1", "a2", "a3",
  1321. "b0", "b1", "b2", "b3",
  1322. NULL
  1323. };
  1324. double (*func2[])(void *, double, double) = {
  1325. a0, a1, a2, a3,
  1326. b0, b1, b2, b3,
  1327. NULL };
  1328. int ret;
  1329. if (!s->custom_str)
  1330. return AVERROR(EINVAL);
  1331. ret = av_expr_parse(&s->e, s->custom_str, var_names,
  1332. NULL, NULL, func2_names, func2, 0, ctx);
  1333. if (ret < 0)
  1334. return ret;
  1335. }
  1336. return 0;
  1337. }
  1338. static int xfade_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
  1339. {
  1340. XFadeContext *s = ctx->priv;
  1341. AVFilterLink *outlink = ctx->outputs[0];
  1342. ThreadData *td = arg;
  1343. int slice_start = (outlink->h * jobnr ) / nb_jobs;
  1344. int slice_end = (outlink->h * (jobnr+1)) / nb_jobs;
  1345. s->transitionf(ctx, td->xf[0], td->xf[1], td->out, td->progress, slice_start, slice_end, jobnr);
  1346. return 0;
  1347. }
  1348. static int xfade_frame(AVFilterContext *ctx, AVFrame *a, AVFrame *b)
  1349. {
  1350. XFadeContext *s = ctx->priv;
  1351. AVFilterLink *outlink = ctx->outputs[0];
  1352. float progress = av_clipf(1.f - ((float)(s->pts - s->first_pts - s->offset_pts) / s->duration_pts), 0.f, 1.f);
  1353. ThreadData td;
  1354. AVFrame *out;
  1355. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  1356. if (!out)
  1357. return AVERROR(ENOMEM);
  1358. td.xf[0] = a, td.xf[1] = b, td.out = out, td.progress = progress;
  1359. ctx->internal->execute(ctx, xfade_slice, &td, NULL, FFMIN(outlink->h, ff_filter_get_nb_threads(ctx)));
  1360. out->pts = s->pts;
  1361. return ff_filter_frame(outlink, out);
  1362. }
  1363. static int xfade_activate(AVFilterContext *ctx)
  1364. {
  1365. XFadeContext *s = ctx->priv;
  1366. AVFilterLink *outlink = ctx->outputs[0];
  1367. AVFrame *in = NULL;
  1368. int ret = 0, status;
  1369. int64_t pts;
  1370. FF_FILTER_FORWARD_STATUS_BACK_ALL(outlink, ctx);
  1371. if (s->xfade_is_over) {
  1372. ret = ff_inlink_consume_frame(ctx->inputs[1], &in);
  1373. if (ret < 0) {
  1374. return ret;
  1375. } else if (ret > 0) {
  1376. in->pts = (in->pts - s->last_pts) + s->pts;
  1377. return ff_filter_frame(outlink, in);
  1378. } else if (ff_inlink_acknowledge_status(ctx->inputs[1], &status, &pts)) {
  1379. ff_outlink_set_status(outlink, status, s->pts);
  1380. return 0;
  1381. } else if (!ret) {
  1382. if (ff_outlink_frame_wanted(outlink)) {
  1383. ff_inlink_request_frame(ctx->inputs[1]);
  1384. return 0;
  1385. }
  1386. }
  1387. }
  1388. if (ff_inlink_queued_frames(ctx->inputs[0]) > 0) {
  1389. s->xf[0] = ff_inlink_peek_frame(ctx->inputs[0], 0);
  1390. if (s->xf[0]) {
  1391. if (s->first_pts == AV_NOPTS_VALUE) {
  1392. s->first_pts = s->xf[0]->pts;
  1393. }
  1394. s->pts = s->xf[0]->pts;
  1395. if (s->first_pts + s->offset_pts > s->xf[0]->pts) {
  1396. s->xf[0] = NULL;
  1397. s->need_second = 0;
  1398. ff_inlink_consume_frame(ctx->inputs[0], &in);
  1399. return ff_filter_frame(outlink, in);
  1400. }
  1401. s->need_second = 1;
  1402. }
  1403. }
  1404. if (s->xf[0] && ff_inlink_queued_frames(ctx->inputs[1]) > 0) {
  1405. ff_inlink_consume_frame(ctx->inputs[0], &s->xf[0]);
  1406. ff_inlink_consume_frame(ctx->inputs[1], &s->xf[1]);
  1407. s->last_pts = s->xf[1]->pts;
  1408. s->pts = s->xf[0]->pts;
  1409. if (s->xf[0]->pts - (s->first_pts + s->offset_pts) > s->duration_pts)
  1410. s->xfade_is_over = 1;
  1411. ret = xfade_frame(ctx, s->xf[0], s->xf[1]);
  1412. av_frame_free(&s->xf[0]);
  1413. av_frame_free(&s->xf[1]);
  1414. return ret;
  1415. }
  1416. if (ff_inlink_queued_frames(ctx->inputs[0]) > 0 &&
  1417. ff_inlink_queued_frames(ctx->inputs[1]) > 0) {
  1418. ff_filter_set_ready(ctx, 100);
  1419. return 0;
  1420. }
  1421. if (ff_outlink_frame_wanted(outlink)) {
  1422. if (!s->eof[0] && ff_outlink_get_status(ctx->inputs[0])) {
  1423. s->eof[0] = 1;
  1424. s->xfade_is_over = 1;
  1425. }
  1426. if (!s->eof[1] && ff_outlink_get_status(ctx->inputs[1])) {
  1427. s->eof[1] = 1;
  1428. }
  1429. if (!s->eof[0] && !s->xf[0])
  1430. ff_inlink_request_frame(ctx->inputs[0]);
  1431. if (!s->eof[1] && (s->need_second || s->eof[0]))
  1432. ff_inlink_request_frame(ctx->inputs[1]);
  1433. if (s->eof[0] && s->eof[1] && (
  1434. ff_inlink_queued_frames(ctx->inputs[0]) <= 0 ||
  1435. ff_inlink_queued_frames(ctx->inputs[1]) <= 0))
  1436. ff_outlink_set_status(outlink, AVERROR_EOF, AV_NOPTS_VALUE);
  1437. return 0;
  1438. }
  1439. return FFERROR_NOT_READY;
  1440. }
  1441. static const AVFilterPad xfade_inputs[] = {
  1442. {
  1443. .name = "main",
  1444. .type = AVMEDIA_TYPE_VIDEO,
  1445. },
  1446. {
  1447. .name = "xfade",
  1448. .type = AVMEDIA_TYPE_VIDEO,
  1449. },
  1450. { NULL }
  1451. };
  1452. static const AVFilterPad xfade_outputs[] = {
  1453. {
  1454. .name = "default",
  1455. .type = AVMEDIA_TYPE_VIDEO,
  1456. .config_props = config_output,
  1457. },
  1458. { NULL }
  1459. };
  1460. AVFilter ff_vf_xfade = {
  1461. .name = "xfade",
  1462. .description = NULL_IF_CONFIG_SMALL("Cross fade one video with another video."),
  1463. .priv_size = sizeof(XFadeContext),
  1464. .priv_class = &xfade_class,
  1465. .query_formats = query_formats,
  1466. .activate = xfade_activate,
  1467. .uninit = uninit,
  1468. .inputs = xfade_inputs,
  1469. .outputs = xfade_outputs,
  1470. .flags = AVFILTER_FLAG_SLICE_THREADS,
  1471. };