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.

1472 lines
91KB

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