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.

1654 lines
105KB

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