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.

1125 lines
54KB

  1. /*
  2. * Copyright (c) 2010 Stefano Sabatini
  3. * Copyright (c) 2010 Baptiste Coudurier
  4. * Copyright (c) 2007 Bobby Bingham
  5. *
  6. * This file is part of FFmpeg.
  7. *
  8. * FFmpeg is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * FFmpeg is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with FFmpeg; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. /**
  23. * @file
  24. * overlay one video on top of another
  25. */
  26. #include "avfilter.h"
  27. #include "formats.h"
  28. #include "libavutil/common.h"
  29. #include "libavutil/eval.h"
  30. #include "libavutil/avstring.h"
  31. #include "libavutil/pixdesc.h"
  32. #include "libavutil/imgutils.h"
  33. #include "libavutil/mathematics.h"
  34. #include "libavutil/opt.h"
  35. #include "libavutil/timestamp.h"
  36. #include "internal.h"
  37. #include "drawutils.h"
  38. #include "framesync.h"
  39. #include "video.h"
  40. #include "vf_overlay.h"
  41. typedef struct ThreadData {
  42. AVFrame *dst, *src;
  43. } ThreadData;
  44. static const char *const var_names[] = {
  45. "main_w", "W", ///< width of the main video
  46. "main_h", "H", ///< height of the main video
  47. "overlay_w", "w", ///< width of the overlay video
  48. "overlay_h", "h", ///< height of the overlay video
  49. "hsub",
  50. "vsub",
  51. "x",
  52. "y",
  53. "n", ///< number of frame
  54. "pos", ///< position in the file
  55. "t", ///< timestamp expressed in seconds
  56. NULL
  57. };
  58. #define MAIN 0
  59. #define OVERLAY 1
  60. #define R 0
  61. #define G 1
  62. #define B 2
  63. #define A 3
  64. #define Y 0
  65. #define U 1
  66. #define V 2
  67. enum EvalMode {
  68. EVAL_MODE_INIT,
  69. EVAL_MODE_FRAME,
  70. EVAL_MODE_NB
  71. };
  72. static av_cold void uninit(AVFilterContext *ctx)
  73. {
  74. OverlayContext *s = ctx->priv;
  75. ff_framesync_uninit(&s->fs);
  76. av_expr_free(s->x_pexpr); s->x_pexpr = NULL;
  77. av_expr_free(s->y_pexpr); s->y_pexpr = NULL;
  78. }
  79. static inline int normalize_xy(double d, int chroma_sub)
  80. {
  81. if (isnan(d))
  82. return INT_MAX;
  83. return (int)d & ~((1 << chroma_sub) - 1);
  84. }
  85. static void eval_expr(AVFilterContext *ctx)
  86. {
  87. OverlayContext *s = ctx->priv;
  88. s->var_values[VAR_X] = av_expr_eval(s->x_pexpr, s->var_values, NULL);
  89. s->var_values[VAR_Y] = av_expr_eval(s->y_pexpr, s->var_values, NULL);
  90. /* It is necessary if x is expressed from y */
  91. s->var_values[VAR_X] = av_expr_eval(s->x_pexpr, s->var_values, NULL);
  92. s->x = normalize_xy(s->var_values[VAR_X], s->hsub);
  93. s->y = normalize_xy(s->var_values[VAR_Y], s->vsub);
  94. }
  95. static int set_expr(AVExpr **pexpr, const char *expr, const char *option, void *log_ctx)
  96. {
  97. int ret;
  98. AVExpr *old = NULL;
  99. if (*pexpr)
  100. old = *pexpr;
  101. ret = av_expr_parse(pexpr, expr, var_names,
  102. NULL, NULL, NULL, NULL, 0, log_ctx);
  103. if (ret < 0) {
  104. av_log(log_ctx, AV_LOG_ERROR,
  105. "Error when evaluating the expression '%s' for %s\n",
  106. expr, option);
  107. *pexpr = old;
  108. return ret;
  109. }
  110. av_expr_free(old);
  111. return 0;
  112. }
  113. static int process_command(AVFilterContext *ctx, const char *cmd, const char *args,
  114. char *res, int res_len, int flags)
  115. {
  116. OverlayContext *s = ctx->priv;
  117. int ret;
  118. if (!strcmp(cmd, "x"))
  119. ret = set_expr(&s->x_pexpr, args, cmd, ctx);
  120. else if (!strcmp(cmd, "y"))
  121. ret = set_expr(&s->y_pexpr, args, cmd, ctx);
  122. else
  123. ret = AVERROR(ENOSYS);
  124. if (ret < 0)
  125. return ret;
  126. if (s->eval_mode == EVAL_MODE_INIT) {
  127. eval_expr(ctx);
  128. av_log(ctx, AV_LOG_VERBOSE, "x:%f xi:%d y:%f yi:%d\n",
  129. s->var_values[VAR_X], s->x,
  130. s->var_values[VAR_Y], s->y);
  131. }
  132. return ret;
  133. }
  134. static const enum AVPixelFormat alpha_pix_fmts[] = {
  135. AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA444P,
  136. AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA422P10,
  137. AV_PIX_FMT_ARGB, AV_PIX_FMT_ABGR, AV_PIX_FMT_RGBA,
  138. AV_PIX_FMT_BGRA, AV_PIX_FMT_GBRAP, AV_PIX_FMT_NONE
  139. };
  140. static int query_formats(AVFilterContext *ctx)
  141. {
  142. OverlayContext *s = ctx->priv;
  143. /* overlay formats contains alpha, for avoiding conversion with alpha information loss */
  144. static const enum AVPixelFormat main_pix_fmts_yuv420[] = {
  145. AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVA420P,
  146. AV_PIX_FMT_NV12, AV_PIX_FMT_NV21,
  147. AV_PIX_FMT_NONE
  148. };
  149. static const enum AVPixelFormat overlay_pix_fmts_yuv420[] = {
  150. AV_PIX_FMT_YUVA420P, AV_PIX_FMT_NONE
  151. };
  152. static const enum AVPixelFormat main_pix_fmts_yuv420p10[] = {
  153. AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUVA420P10,
  154. AV_PIX_FMT_NONE
  155. };
  156. static const enum AVPixelFormat overlay_pix_fmts_yuv420p10[] = {
  157. AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_NONE
  158. };
  159. static const enum AVPixelFormat main_pix_fmts_yuv422[] = {
  160. AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_NONE
  161. };
  162. static const enum AVPixelFormat overlay_pix_fmts_yuv422[] = {
  163. AV_PIX_FMT_YUVA422P, AV_PIX_FMT_NONE
  164. };
  165. static const enum AVPixelFormat main_pix_fmts_yuv422p10[] = {
  166. AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_NONE
  167. };
  168. static const enum AVPixelFormat overlay_pix_fmts_yuv422p10[] = {
  169. AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_NONE
  170. };
  171. static const enum AVPixelFormat main_pix_fmts_yuv444[] = {
  172. AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVA444P, AV_PIX_FMT_NONE
  173. };
  174. static const enum AVPixelFormat overlay_pix_fmts_yuv444[] = {
  175. AV_PIX_FMT_YUVA444P, AV_PIX_FMT_NONE
  176. };
  177. static const enum AVPixelFormat main_pix_fmts_gbrp[] = {
  178. AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRAP, AV_PIX_FMT_NONE
  179. };
  180. static const enum AVPixelFormat overlay_pix_fmts_gbrp[] = {
  181. AV_PIX_FMT_GBRAP, AV_PIX_FMT_NONE
  182. };
  183. static const enum AVPixelFormat main_pix_fmts_rgb[] = {
  184. AV_PIX_FMT_ARGB, AV_PIX_FMT_RGBA,
  185. AV_PIX_FMT_ABGR, AV_PIX_FMT_BGRA,
  186. AV_PIX_FMT_RGB24, AV_PIX_FMT_BGR24,
  187. AV_PIX_FMT_NONE
  188. };
  189. static const enum AVPixelFormat overlay_pix_fmts_rgb[] = {
  190. AV_PIX_FMT_ARGB, AV_PIX_FMT_RGBA,
  191. AV_PIX_FMT_ABGR, AV_PIX_FMT_BGRA,
  192. AV_PIX_FMT_NONE
  193. };
  194. const enum AVPixelFormat *main_formats, *overlay_formats;
  195. AVFilterFormats *formats;
  196. int ret;
  197. switch (s->format) {
  198. case OVERLAY_FORMAT_YUV420:
  199. main_formats = main_pix_fmts_yuv420;
  200. overlay_formats = overlay_pix_fmts_yuv420;
  201. break;
  202. case OVERLAY_FORMAT_YUV420P10:
  203. main_formats = main_pix_fmts_yuv420p10;
  204. overlay_formats = overlay_pix_fmts_yuv420p10;
  205. break;
  206. case OVERLAY_FORMAT_YUV422:
  207. main_formats = main_pix_fmts_yuv422;
  208. overlay_formats = overlay_pix_fmts_yuv422;
  209. break;
  210. case OVERLAY_FORMAT_YUV422P10:
  211. main_formats = main_pix_fmts_yuv422p10;
  212. overlay_formats = overlay_pix_fmts_yuv422p10;
  213. break;
  214. case OVERLAY_FORMAT_YUV444:
  215. main_formats = main_pix_fmts_yuv444;
  216. overlay_formats = overlay_pix_fmts_yuv444;
  217. break;
  218. case OVERLAY_FORMAT_RGB:
  219. main_formats = main_pix_fmts_rgb;
  220. overlay_formats = overlay_pix_fmts_rgb;
  221. break;
  222. case OVERLAY_FORMAT_GBRP:
  223. main_formats = main_pix_fmts_gbrp;
  224. overlay_formats = overlay_pix_fmts_gbrp;
  225. break;
  226. case OVERLAY_FORMAT_AUTO:
  227. return ff_set_common_formats(ctx, ff_make_format_list(alpha_pix_fmts));
  228. default:
  229. av_assert0(0);
  230. }
  231. formats = ff_make_format_list(main_formats);
  232. if ((ret = ff_formats_ref(formats, &ctx->inputs[MAIN]->outcfg.formats)) < 0 ||
  233. (ret = ff_formats_ref(formats, &ctx->outputs[MAIN]->incfg.formats)) < 0)
  234. return ret;
  235. return ff_formats_ref(ff_make_format_list(overlay_formats),
  236. &ctx->inputs[OVERLAY]->outcfg.formats);
  237. }
  238. static int config_input_overlay(AVFilterLink *inlink)
  239. {
  240. AVFilterContext *ctx = inlink->dst;
  241. OverlayContext *s = inlink->dst->priv;
  242. int ret;
  243. const AVPixFmtDescriptor *pix_desc = av_pix_fmt_desc_get(inlink->format);
  244. av_image_fill_max_pixsteps(s->overlay_pix_step, NULL, pix_desc);
  245. /* Finish the configuration by evaluating the expressions
  246. now when both inputs are configured. */
  247. s->var_values[VAR_MAIN_W ] = s->var_values[VAR_MW] = ctx->inputs[MAIN ]->w;
  248. s->var_values[VAR_MAIN_H ] = s->var_values[VAR_MH] = ctx->inputs[MAIN ]->h;
  249. s->var_values[VAR_OVERLAY_W] = s->var_values[VAR_OW] = ctx->inputs[OVERLAY]->w;
  250. s->var_values[VAR_OVERLAY_H] = s->var_values[VAR_OH] = ctx->inputs[OVERLAY]->h;
  251. s->var_values[VAR_HSUB] = 1<<pix_desc->log2_chroma_w;
  252. s->var_values[VAR_VSUB] = 1<<pix_desc->log2_chroma_h;
  253. s->var_values[VAR_X] = NAN;
  254. s->var_values[VAR_Y] = NAN;
  255. s->var_values[VAR_N] = 0;
  256. s->var_values[VAR_T] = NAN;
  257. s->var_values[VAR_POS] = NAN;
  258. if ((ret = set_expr(&s->x_pexpr, s->x_expr, "x", ctx)) < 0 ||
  259. (ret = set_expr(&s->y_pexpr, s->y_expr, "y", ctx)) < 0)
  260. return ret;
  261. s->overlay_is_packed_rgb =
  262. ff_fill_rgba_map(s->overlay_rgba_map, inlink->format) >= 0;
  263. s->overlay_has_alpha = ff_fmt_is_in(inlink->format, alpha_pix_fmts);
  264. if (s->eval_mode == EVAL_MODE_INIT) {
  265. eval_expr(ctx);
  266. av_log(ctx, AV_LOG_VERBOSE, "x:%f xi:%d y:%f yi:%d\n",
  267. s->var_values[VAR_X], s->x,
  268. s->var_values[VAR_Y], s->y);
  269. }
  270. av_log(ctx, AV_LOG_VERBOSE,
  271. "main w:%d h:%d fmt:%s overlay w:%d h:%d fmt:%s\n",
  272. ctx->inputs[MAIN]->w, ctx->inputs[MAIN]->h,
  273. av_get_pix_fmt_name(ctx->inputs[MAIN]->format),
  274. ctx->inputs[OVERLAY]->w, ctx->inputs[OVERLAY]->h,
  275. av_get_pix_fmt_name(ctx->inputs[OVERLAY]->format));
  276. return 0;
  277. }
  278. static int config_output(AVFilterLink *outlink)
  279. {
  280. AVFilterContext *ctx = outlink->src;
  281. OverlayContext *s = ctx->priv;
  282. int ret;
  283. if ((ret = ff_framesync_init_dualinput(&s->fs, ctx)) < 0)
  284. return ret;
  285. outlink->w = ctx->inputs[MAIN]->w;
  286. outlink->h = ctx->inputs[MAIN]->h;
  287. outlink->time_base = ctx->inputs[MAIN]->time_base;
  288. return ff_framesync_configure(&s->fs);
  289. }
  290. // divide by 255 and round to nearest
  291. // apply a fast variant: (X+127)/255 = ((X+127)*257+257)>>16 = ((X+128)*257)>>16
  292. #define FAST_DIV255(x) ((((x) + 128) * 257) >> 16)
  293. // calculate the unpremultiplied alpha, applying the general equation:
  294. // alpha = alpha_overlay / ( (alpha_main + alpha_overlay) - (alpha_main * alpha_overlay) )
  295. // (((x) << 16) - ((x) << 9) + (x)) is a faster version of: 255 * 255 * x
  296. // ((((x) + (y)) << 8) - ((x) + (y)) - (y) * (x)) is a faster version of: 255 * (x + y)
  297. #define UNPREMULTIPLY_ALPHA(x, y) ((((x) << 16) - ((x) << 9) + (x)) / ((((x) + (y)) << 8) - ((x) + (y)) - (y) * (x)))
  298. /**
  299. * Blend image in src to destination buffer dst at position (x, y).
  300. */
  301. static av_always_inline void blend_slice_packed_rgb(AVFilterContext *ctx,
  302. AVFrame *dst, const AVFrame *src,
  303. int main_has_alpha, int x, int y,
  304. int is_straight, int jobnr, int nb_jobs)
  305. {
  306. OverlayContext *s = ctx->priv;
  307. int i, imax, j, jmax;
  308. const int src_w = src->width;
  309. const int src_h = src->height;
  310. const int dst_w = dst->width;
  311. const int dst_h = dst->height;
  312. uint8_t alpha; ///< the amount of overlay to blend on to main
  313. const int dr = s->main_rgba_map[R];
  314. const int dg = s->main_rgba_map[G];
  315. const int db = s->main_rgba_map[B];
  316. const int da = s->main_rgba_map[A];
  317. const int dstep = s->main_pix_step[0];
  318. const int sr = s->overlay_rgba_map[R];
  319. const int sg = s->overlay_rgba_map[G];
  320. const int sb = s->overlay_rgba_map[B];
  321. const int sa = s->overlay_rgba_map[A];
  322. const int sstep = s->overlay_pix_step[0];
  323. int slice_start, slice_end;
  324. uint8_t *S, *sp, *d, *dp;
  325. i = FFMAX(-y, 0);
  326. imax = FFMIN3(-y + dst_h, FFMIN(src_h, dst_h), y + src_h);
  327. slice_start = i + (imax * jobnr) / nb_jobs;
  328. slice_end = i + (imax * (jobnr+1)) / nb_jobs;
  329. sp = src->data[0] + (slice_start) * src->linesize[0];
  330. dp = dst->data[0] + (y + slice_start) * dst->linesize[0];
  331. for (i = slice_start; i < slice_end; i++) {
  332. j = FFMAX(-x, 0);
  333. S = sp + j * sstep;
  334. d = dp + (x+j) * dstep;
  335. for (jmax = FFMIN(-x + dst_w, src_w); j < jmax; j++) {
  336. alpha = S[sa];
  337. // if the main channel has an alpha channel, alpha has to be calculated
  338. // to create an un-premultiplied (straight) alpha value
  339. if (main_has_alpha && alpha != 0 && alpha != 255) {
  340. uint8_t alpha_d = d[da];
  341. alpha = UNPREMULTIPLY_ALPHA(alpha, alpha_d);
  342. }
  343. switch (alpha) {
  344. case 0:
  345. break;
  346. case 255:
  347. d[dr] = S[sr];
  348. d[dg] = S[sg];
  349. d[db] = S[sb];
  350. break;
  351. default:
  352. // main_value = main_value * (1 - alpha) + overlay_value * alpha
  353. // since alpha is in the range 0-255, the result must divided by 255
  354. d[dr] = is_straight ? FAST_DIV255(d[dr] * (255 - alpha) + S[sr] * alpha) :
  355. FFMIN(FAST_DIV255(d[dr] * (255 - alpha)) + S[sr], 255);
  356. d[dg] = is_straight ? FAST_DIV255(d[dg] * (255 - alpha) + S[sg] * alpha) :
  357. FFMIN(FAST_DIV255(d[dg] * (255 - alpha)) + S[sg], 255);
  358. d[db] = is_straight ? FAST_DIV255(d[db] * (255 - alpha) + S[sb] * alpha) :
  359. FFMIN(FAST_DIV255(d[db] * (255 - alpha)) + S[sb], 255);
  360. }
  361. if (main_has_alpha) {
  362. switch (alpha) {
  363. case 0:
  364. break;
  365. case 255:
  366. d[da] = S[sa];
  367. break;
  368. default:
  369. // apply alpha compositing: main_alpha += (1-main_alpha) * overlay_alpha
  370. d[da] += FAST_DIV255((255 - d[da]) * S[sa]);
  371. }
  372. }
  373. d += dstep;
  374. S += sstep;
  375. }
  376. dp += dst->linesize[0];
  377. sp += src->linesize[0];
  378. }
  379. }
  380. #define DEFINE_BLEND_PLANE(depth, nbits) \
  381. static av_always_inline void blend_plane_##depth##_##nbits##bits(AVFilterContext *ctx, \
  382. AVFrame *dst, const AVFrame *src, \
  383. int src_w, int src_h, \
  384. int dst_w, int dst_h, \
  385. int i, int hsub, int vsub, \
  386. int x, int y, \
  387. int main_has_alpha, \
  388. int dst_plane, \
  389. int dst_offset, \
  390. int dst_step, \
  391. int straight, \
  392. int yuv, \
  393. int jobnr, \
  394. int nb_jobs) \
  395. { \
  396. OverlayContext *octx = ctx->priv; \
  397. int src_wp = AV_CEIL_RSHIFT(src_w, hsub); \
  398. int src_hp = AV_CEIL_RSHIFT(src_h, vsub); \
  399. int dst_wp = AV_CEIL_RSHIFT(dst_w, hsub); \
  400. int dst_hp = AV_CEIL_RSHIFT(dst_h, vsub); \
  401. int yp = y>>vsub; \
  402. int xp = x>>hsub; \
  403. uint##depth##_t *s, *sp, *d, *dp, *dap, *a, *da, *ap; \
  404. int jmax, j, k, kmax; \
  405. int slice_start, slice_end; \
  406. const uint##depth##_t max = (1 << nbits) - 1; \
  407. const uint##depth##_t mid = (1 << (nbits -1)) ; \
  408. int bytes = depth / 8; \
  409. \
  410. dst_step /= bytes; \
  411. j = FFMAX(-yp, 0); \
  412. jmax = FFMIN3(-yp + dst_hp, FFMIN(src_hp, dst_hp), yp + src_hp); \
  413. \
  414. slice_start = j + (jmax * jobnr) / nb_jobs; \
  415. slice_end = j + (jmax * (jobnr+1)) / nb_jobs; \
  416. \
  417. sp = (uint##depth##_t *)(src->data[i] + (slice_start) * src->linesize[i]); \
  418. dp = (uint##depth##_t *)(dst->data[dst_plane] \
  419. + (yp + slice_start) * dst->linesize[dst_plane] \
  420. + dst_offset); \
  421. ap = (uint##depth##_t *)(src->data[3] + (slice_start << vsub) * src->linesize[3]); \
  422. dap = (uint##depth##_t *)(dst->data[3] + ((yp + slice_start) << vsub) * dst->linesize[3]); \
  423. \
  424. for (j = slice_start; j < slice_end; j++) { \
  425. k = FFMAX(-xp, 0); \
  426. d = dp + (xp+k) * dst_step; \
  427. s = sp + k; \
  428. a = ap + (k<<hsub); \
  429. da = dap + ((xp+k) << hsub); \
  430. kmax = FFMIN(-xp + dst_wp, src_wp); \
  431. \
  432. if (nbits == 8 && ((vsub && j+1 < src_hp) || !vsub) && octx->blend_row[i]) { \
  433. int c = octx->blend_row[i]((uint8_t*)d, (uint8_t*)da, (uint8_t*)s, \
  434. (uint8_t*)a, kmax - k, src->linesize[3]); \
  435. \
  436. s += c; \
  437. d += dst_step * c; \
  438. da += (1 << hsub) * c; \
  439. a += (1 << hsub) * c; \
  440. k += c; \
  441. } \
  442. for (; k < kmax; k++) { \
  443. int alpha_v, alpha_h, alpha; \
  444. \
  445. /* average alpha for color components, improve quality */ \
  446. if (hsub && vsub && j+1 < src_hp && k+1 < src_wp) { \
  447. alpha = (a[0] + a[src->linesize[3]] + \
  448. a[1] + a[src->linesize[3]+1]) >> 2; \
  449. } else if (hsub || vsub) { \
  450. alpha_h = hsub && k+1 < src_wp ? \
  451. (a[0] + a[1]) >> 1 : a[0]; \
  452. alpha_v = vsub && j+1 < src_hp ? \
  453. (a[0] + a[src->linesize[3]]) >> 1 : a[0]; \
  454. alpha = (alpha_v + alpha_h) >> 1; \
  455. } else \
  456. alpha = a[0]; \
  457. /* if the main channel has an alpha channel, alpha has to be calculated */ \
  458. /* to create an un-premultiplied (straight) alpha value */ \
  459. if (main_has_alpha && alpha != 0 && alpha != max) { \
  460. /* average alpha for color components, improve quality */ \
  461. uint8_t alpha_d; \
  462. if (hsub && vsub && j+1 < src_hp && k+1 < src_wp) { \
  463. alpha_d = (da[0] + da[dst->linesize[3]] + \
  464. da[1] + da[dst->linesize[3]+1]) >> 2; \
  465. } else if (hsub || vsub) { \
  466. alpha_h = hsub && k+1 < src_wp ? \
  467. (da[0] + da[1]) >> 1 : da[0]; \
  468. alpha_v = vsub && j+1 < src_hp ? \
  469. (da[0] + da[dst->linesize[3]]) >> 1 : da[0]; \
  470. alpha_d = (alpha_v + alpha_h) >> 1; \
  471. } else \
  472. alpha_d = da[0]; \
  473. alpha = UNPREMULTIPLY_ALPHA(alpha, alpha_d); \
  474. } \
  475. if (straight) { \
  476. if (nbits > 8) \
  477. *d = (*d * (max - alpha) + *s * alpha) / max; \
  478. else \
  479. *d = FAST_DIV255(*d * (255 - alpha) + *s * alpha); \
  480. } else { \
  481. if (nbits > 8) { \
  482. if (i && yuv) \
  483. *d = av_clip((*d * (max - alpha) + *s * alpha) / max + *s - mid, -mid, mid) + mid; \
  484. else \
  485. *d = FFMIN((*d * (max - alpha) + *s * alpha) / max + *s, max); \
  486. } else { \
  487. if (i && yuv) \
  488. *d = av_clip(FAST_DIV255((*d - mid) * (max - alpha)) + *s - mid, -mid, mid) + mid; \
  489. else \
  490. *d = FFMIN(FAST_DIV255(*d * (max - alpha)) + *s, max); \
  491. } \
  492. } \
  493. s++; \
  494. d += dst_step; \
  495. da += 1 << hsub; \
  496. a += 1 << hsub; \
  497. } \
  498. dp += dst->linesize[dst_plane] / bytes; \
  499. sp += src->linesize[i] / bytes; \
  500. ap += (1 << vsub) * src->linesize[3] / bytes; \
  501. dap += (1 << vsub) * dst->linesize[3] / bytes; \
  502. } \
  503. }
  504. DEFINE_BLEND_PLANE(8, 8)
  505. DEFINE_BLEND_PLANE(16, 10)
  506. #define DEFINE_ALPHA_COMPOSITE(depth, nbits) \
  507. static inline void alpha_composite_##depth##_##nbits##bits(const AVFrame *src, const AVFrame *dst, \
  508. int src_w, int src_h, \
  509. int dst_w, int dst_h, \
  510. int x, int y, \
  511. int jobnr, int nb_jobs) \
  512. { \
  513. uint##depth##_t alpha; /* the amount of overlay to blend on to main */ \
  514. uint##depth##_t *s, *sa, *d, *da; \
  515. int i, imax, j, jmax; \
  516. int slice_start, slice_end; \
  517. const uint##depth##_t max = (1 << nbits) - 1; \
  518. int bytes = depth / 8; \
  519. \
  520. imax = FFMIN(-y + dst_h, src_h); \
  521. slice_start = (imax * jobnr) / nb_jobs; \
  522. slice_end = ((imax * (jobnr+1)) / nb_jobs); \
  523. \
  524. i = FFMAX(-y, 0); \
  525. sa = (uint##depth##_t *)(src->data[3] + (i + slice_start) * src->linesize[3]); \
  526. da = (uint##depth##_t *)(dst->data[3] + (y + i + slice_start) * dst->linesize[3]); \
  527. \
  528. for (i = i + slice_start; i < slice_end; i++) { \
  529. j = FFMAX(-x, 0); \
  530. s = sa + j; \
  531. d = da + x+j; \
  532. \
  533. for (jmax = FFMIN(-x + dst_w, src_w); j < jmax; j++) { \
  534. alpha = *s; \
  535. if (alpha != 0 && alpha != max) { \
  536. uint8_t alpha_d = *d; \
  537. alpha = UNPREMULTIPLY_ALPHA(alpha, alpha_d); \
  538. } \
  539. if (alpha == max) \
  540. *d = *s; \
  541. else if (alpha > 0) { \
  542. /* apply alpha compositing: main_alpha += (1-main_alpha) * overlay_alpha */ \
  543. if (nbits > 8) \
  544. *d += (max - *d) * *s / max; \
  545. else \
  546. *d += FAST_DIV255((max - *d) * *s); \
  547. } \
  548. d += 1; \
  549. s += 1; \
  550. } \
  551. da += dst->linesize[3] / bytes; \
  552. sa += src->linesize[3] / bytes; \
  553. } \
  554. }
  555. DEFINE_ALPHA_COMPOSITE(8, 8)
  556. DEFINE_ALPHA_COMPOSITE(16, 10)
  557. #define DEFINE_BLEND_SLICE_YUV(depth, nbits) \
  558. static av_always_inline void blend_slice_yuv_##depth##_##nbits##bits(AVFilterContext *ctx, \
  559. AVFrame *dst, const AVFrame *src, \
  560. int hsub, int vsub, \
  561. int main_has_alpha, \
  562. int x, int y, \
  563. int is_straight, \
  564. int jobnr, int nb_jobs) \
  565. { \
  566. OverlayContext *s = ctx->priv; \
  567. const int src_w = src->width; \
  568. const int src_h = src->height; \
  569. const int dst_w = dst->width; \
  570. const int dst_h = dst->height; \
  571. \
  572. blend_plane_##depth##_##nbits##bits(ctx, dst, src, src_w, src_h, dst_w, dst_h, 0, 0, 0, \
  573. x, y, main_has_alpha, s->main_desc->comp[0].plane, s->main_desc->comp[0].offset, \
  574. s->main_desc->comp[0].step, is_straight, 1, jobnr, nb_jobs); \
  575. blend_plane_##depth##_##nbits##bits(ctx, dst, src, src_w, src_h, dst_w, dst_h, 1, hsub, vsub, \
  576. x, y, main_has_alpha, s->main_desc->comp[1].plane, s->main_desc->comp[1].offset, \
  577. s->main_desc->comp[1].step, is_straight, 1, jobnr, nb_jobs); \
  578. blend_plane_##depth##_##nbits##bits(ctx, dst, src, src_w, src_h, dst_w, dst_h, 2, hsub, vsub, \
  579. x, y, main_has_alpha, s->main_desc->comp[2].plane, s->main_desc->comp[2].offset, \
  580. s->main_desc->comp[2].step, is_straight, 1, jobnr, nb_jobs); \
  581. \
  582. if (main_has_alpha) \
  583. alpha_composite_##depth##_##nbits##bits(src, dst, src_w, src_h, dst_w, dst_h, x, y, \
  584. jobnr, nb_jobs); \
  585. }
  586. DEFINE_BLEND_SLICE_YUV(8, 8)
  587. DEFINE_BLEND_SLICE_YUV(16, 10)
  588. static av_always_inline void blend_slice_planar_rgb(AVFilterContext *ctx,
  589. AVFrame *dst, const AVFrame *src,
  590. int hsub, int vsub,
  591. int main_has_alpha,
  592. int x, int y,
  593. int is_straight,
  594. int jobnr,
  595. int nb_jobs)
  596. {
  597. OverlayContext *s = ctx->priv;
  598. const int src_w = src->width;
  599. const int src_h = src->height;
  600. const int dst_w = dst->width;
  601. const int dst_h = dst->height;
  602. blend_plane_8_8bits(ctx, dst, src, src_w, src_h, dst_w, dst_h, 0, 0, 0, x, y, main_has_alpha,
  603. s->main_desc->comp[1].plane, s->main_desc->comp[1].offset, s->main_desc->comp[1].step, is_straight, 0,
  604. jobnr, nb_jobs);
  605. blend_plane_8_8bits(ctx, dst, src, src_w, src_h, dst_w, dst_h, 1, hsub, vsub, x, y, main_has_alpha,
  606. s->main_desc->comp[2].plane, s->main_desc->comp[2].offset, s->main_desc->comp[2].step, is_straight, 0,
  607. jobnr, nb_jobs);
  608. blend_plane_8_8bits(ctx, dst, src, src_w, src_h, dst_w, dst_h, 2, hsub, vsub, x, y, main_has_alpha,
  609. s->main_desc->comp[0].plane, s->main_desc->comp[0].offset, s->main_desc->comp[0].step, is_straight, 0,
  610. jobnr, nb_jobs);
  611. if (main_has_alpha)
  612. alpha_composite_8_8bits(src, dst, src_w, src_h, dst_w, dst_h, x, y, jobnr, nb_jobs);
  613. }
  614. static int blend_slice_yuv420(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
  615. {
  616. OverlayContext *s = ctx->priv;
  617. ThreadData *td = arg;
  618. blend_slice_yuv_8_8bits(ctx, td->dst, td->src, 1, 1, 0, s->x, s->y, 1, jobnr, nb_jobs);
  619. return 0;
  620. }
  621. static int blend_slice_yuva420(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
  622. {
  623. OverlayContext *s = ctx->priv;
  624. ThreadData *td = arg;
  625. blend_slice_yuv_8_8bits(ctx, td->dst, td->src, 1, 1, 1, s->x, s->y, 1, jobnr, nb_jobs);
  626. return 0;
  627. }
  628. static int blend_slice_yuv420p10(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
  629. {
  630. OverlayContext *s = ctx->priv;
  631. ThreadData *td = arg;
  632. blend_slice_yuv_16_10bits(ctx, td->dst, td->src, 1, 1, 0, s->x, s->y, 1, jobnr, nb_jobs);
  633. return 0;
  634. }
  635. static int blend_slice_yuva420p10(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
  636. {
  637. OverlayContext *s = ctx->priv;
  638. ThreadData *td = arg;
  639. blend_slice_yuv_16_10bits(ctx, td->dst, td->src, 1, 1, 1, s->x, s->y, 1, jobnr, nb_jobs);
  640. return 0;
  641. }
  642. static int blend_slice_yuv422p10(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
  643. {
  644. OverlayContext *s = ctx->priv;
  645. ThreadData *td = arg;
  646. blend_slice_yuv_16_10bits(ctx, td->dst, td->src, 1, 0, 0, s->x, s->y, 1, jobnr, nb_jobs);
  647. return 0;
  648. }
  649. static int blend_slice_yuva422p10(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
  650. {
  651. OverlayContext *s = ctx->priv;
  652. ThreadData *td = arg;
  653. blend_slice_yuv_16_10bits(ctx, td->dst, td->src, 1, 0, 1, s->x, s->y, 1, jobnr, nb_jobs);
  654. return 0;
  655. }
  656. static int blend_slice_yuv422(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
  657. {
  658. OverlayContext *s = ctx->priv;
  659. ThreadData *td = arg;
  660. blend_slice_yuv_8_8bits(ctx, td->dst, td->src, 1, 0, 0, s->x, s->y, 1, jobnr, nb_jobs);
  661. return 0;
  662. }
  663. static int blend_slice_yuva422(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
  664. {
  665. OverlayContext *s = ctx->priv;
  666. ThreadData *td = arg;
  667. blend_slice_yuv_8_8bits(ctx, td->dst, td->src, 1, 0, 1, s->x, s->y, 1, jobnr, nb_jobs);
  668. return 0;
  669. }
  670. static int blend_slice_yuv444(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
  671. {
  672. OverlayContext *s = ctx->priv;
  673. ThreadData *td = arg;
  674. blend_slice_yuv_8_8bits(ctx, td->dst, td->src, 0, 0, 0, s->x, s->y, 1, jobnr, nb_jobs);
  675. return 0;
  676. }
  677. static int blend_slice_yuva444(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
  678. {
  679. OverlayContext *s = ctx->priv;
  680. ThreadData *td = arg;
  681. blend_slice_yuv_8_8bits(ctx, td->dst, td->src, 0, 0, 1, s->x, s->y, 1, jobnr, nb_jobs);
  682. return 0;
  683. }
  684. static int blend_slice_gbrp(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
  685. {
  686. OverlayContext *s = ctx->priv;
  687. ThreadData *td = arg;
  688. blend_slice_planar_rgb(ctx, td->dst, td->src, 0, 0, 0, s->x, s->y, 1, jobnr, nb_jobs);
  689. return 0;
  690. }
  691. static int blend_slice_gbrap(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
  692. {
  693. OverlayContext *s = ctx->priv;
  694. ThreadData *td = arg;
  695. blend_slice_planar_rgb(ctx, td->dst, td->src, 0, 0, 1, s->x, s->y, 1, jobnr, nb_jobs);
  696. return 0;
  697. }
  698. static int blend_slice_yuv420_pm(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
  699. {
  700. OverlayContext *s = ctx->priv;
  701. ThreadData *td = arg;
  702. blend_slice_yuv_8_8bits(ctx, td->dst, td->src, 1, 1, 0, s->x, s->y, 0, jobnr, nb_jobs);
  703. return 0;
  704. }
  705. static int blend_slice_yuva420_pm(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
  706. {
  707. OverlayContext *s = ctx->priv;
  708. ThreadData *td = arg;
  709. blend_slice_yuv_8_8bits(ctx, td->dst, td->src, 1, 1, 1, s->x, s->y, 0, jobnr, nb_jobs);
  710. return 0;
  711. }
  712. static int blend_slice_yuv422_pm(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
  713. {
  714. OverlayContext *s = ctx->priv;
  715. ThreadData *td = arg;
  716. blend_slice_yuv_8_8bits(ctx, td->dst, td->src, 1, 0, 0, s->x, s->y, 0, jobnr, nb_jobs);
  717. return 0;
  718. }
  719. static int blend_slice_yuva422_pm(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
  720. {
  721. OverlayContext *s = ctx->priv;
  722. ThreadData *td = arg;
  723. blend_slice_yuv_8_8bits(ctx, td->dst, td->src, 1, 0, 1, s->x, s->y, 0, jobnr, nb_jobs);
  724. return 0;
  725. }
  726. static int blend_slice_yuv444_pm(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
  727. {
  728. OverlayContext *s = ctx->priv;
  729. ThreadData *td = arg;
  730. blend_slice_yuv_8_8bits(ctx, td->dst, td->src, 0, 0, 0, s->x, s->y, 0, jobnr, nb_jobs);
  731. return 0;
  732. }
  733. static int blend_slice_yuva444_pm(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
  734. {
  735. OverlayContext *s = ctx->priv;
  736. ThreadData *td = arg;
  737. blend_slice_yuv_8_8bits(ctx, td->dst, td->src, 0, 0, 1, s->x, s->y, 0, jobnr, nb_jobs);
  738. return 0;
  739. }
  740. static int blend_slice_gbrp_pm(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
  741. {
  742. OverlayContext *s = ctx->priv;
  743. ThreadData *td = arg;
  744. blend_slice_planar_rgb(ctx, td->dst, td->src, 0, 0, 0, s->x, s->y, 0, jobnr, nb_jobs);
  745. return 0;
  746. }
  747. static int blend_slice_gbrap_pm(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
  748. {
  749. OverlayContext *s = ctx->priv;
  750. ThreadData *td = arg;
  751. blend_slice_planar_rgb(ctx, td->dst, td->src, 0, 0, 1, s->x, s->y, 0, jobnr, nb_jobs);
  752. return 0;
  753. }
  754. static int blend_slice_rgb(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
  755. {
  756. OverlayContext *s = ctx->priv;
  757. ThreadData *td = arg;
  758. blend_slice_packed_rgb(ctx, td->dst, td->src, 0, s->x, s->y, 1, jobnr, nb_jobs);
  759. return 0;
  760. }
  761. static int blend_slice_rgba(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
  762. {
  763. OverlayContext *s = ctx->priv;
  764. ThreadData *td = arg;
  765. blend_slice_packed_rgb(ctx, td->dst, td->src, 1, s->x, s->y, 1, jobnr, nb_jobs);
  766. return 0;
  767. }
  768. static int blend_slice_rgb_pm(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
  769. {
  770. OverlayContext *s = ctx->priv;
  771. ThreadData *td = arg;
  772. blend_slice_packed_rgb(ctx, td->dst, td->src, 0, s->x, s->y, 0, jobnr, nb_jobs);
  773. return 0;
  774. }
  775. static int blend_slice_rgba_pm(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
  776. {
  777. OverlayContext *s = ctx->priv;
  778. ThreadData *td = arg;
  779. blend_slice_packed_rgb(ctx, td->dst, td->src, 1, s->x, s->y, 0, jobnr, nb_jobs);
  780. return 0;
  781. }
  782. static int config_input_main(AVFilterLink *inlink)
  783. {
  784. OverlayContext *s = inlink->dst->priv;
  785. const AVPixFmtDescriptor *pix_desc = av_pix_fmt_desc_get(inlink->format);
  786. av_image_fill_max_pixsteps(s->main_pix_step, NULL, pix_desc);
  787. s->hsub = pix_desc->log2_chroma_w;
  788. s->vsub = pix_desc->log2_chroma_h;
  789. s->main_desc = pix_desc;
  790. s->main_is_packed_rgb =
  791. ff_fill_rgba_map(s->main_rgba_map, inlink->format) >= 0;
  792. s->main_has_alpha = ff_fmt_is_in(inlink->format, alpha_pix_fmts);
  793. switch (s->format) {
  794. case OVERLAY_FORMAT_YUV420:
  795. s->blend_slice = s->main_has_alpha ? blend_slice_yuva420 : blend_slice_yuv420;
  796. break;
  797. case OVERLAY_FORMAT_YUV420P10:
  798. s->blend_slice = s->main_has_alpha ? blend_slice_yuva420p10 : blend_slice_yuv420p10;
  799. break;
  800. case OVERLAY_FORMAT_YUV422:
  801. s->blend_slice = s->main_has_alpha ? blend_slice_yuva422 : blend_slice_yuv422;
  802. break;
  803. case OVERLAY_FORMAT_YUV422P10:
  804. s->blend_slice = s->main_has_alpha ? blend_slice_yuva422p10 : blend_slice_yuv422p10;
  805. break;
  806. case OVERLAY_FORMAT_YUV444:
  807. s->blend_slice = s->main_has_alpha ? blend_slice_yuva444 : blend_slice_yuv444;
  808. break;
  809. case OVERLAY_FORMAT_RGB:
  810. s->blend_slice = s->main_has_alpha ? blend_slice_rgba : blend_slice_rgb;
  811. break;
  812. case OVERLAY_FORMAT_GBRP:
  813. s->blend_slice = s->main_has_alpha ? blend_slice_gbrap : blend_slice_gbrp;
  814. break;
  815. case OVERLAY_FORMAT_AUTO:
  816. switch (inlink->format) {
  817. case AV_PIX_FMT_YUVA420P:
  818. s->blend_slice = blend_slice_yuva420;
  819. break;
  820. case AV_PIX_FMT_YUVA420P10:
  821. s->blend_slice = blend_slice_yuva420p10;
  822. break;
  823. case AV_PIX_FMT_YUVA422P:
  824. s->blend_slice = blend_slice_yuva422;
  825. break;
  826. case AV_PIX_FMT_YUVA422P10:
  827. s->blend_slice = blend_slice_yuva422p10;
  828. break;
  829. case AV_PIX_FMT_YUVA444P:
  830. s->blend_slice = blend_slice_yuva444;
  831. break;
  832. case AV_PIX_FMT_ARGB:
  833. case AV_PIX_FMT_RGBA:
  834. case AV_PIX_FMT_BGRA:
  835. case AV_PIX_FMT_ABGR:
  836. s->blend_slice = blend_slice_rgba;
  837. break;
  838. case AV_PIX_FMT_GBRAP:
  839. s->blend_slice = blend_slice_gbrap;
  840. break;
  841. default:
  842. av_assert0(0);
  843. break;
  844. }
  845. break;
  846. }
  847. if (!s->alpha_format)
  848. goto end;
  849. switch (s->format) {
  850. case OVERLAY_FORMAT_YUV420:
  851. s->blend_slice = s->main_has_alpha ? blend_slice_yuva420_pm : blend_slice_yuv420_pm;
  852. break;
  853. case OVERLAY_FORMAT_YUV422:
  854. s->blend_slice = s->main_has_alpha ? blend_slice_yuva422_pm : blend_slice_yuv422_pm;
  855. break;
  856. case OVERLAY_FORMAT_YUV444:
  857. s->blend_slice = s->main_has_alpha ? blend_slice_yuva444_pm : blend_slice_yuv444_pm;
  858. break;
  859. case OVERLAY_FORMAT_RGB:
  860. s->blend_slice = s->main_has_alpha ? blend_slice_rgba_pm : blend_slice_rgb_pm;
  861. break;
  862. case OVERLAY_FORMAT_GBRP:
  863. s->blend_slice = s->main_has_alpha ? blend_slice_gbrap_pm : blend_slice_gbrp_pm;
  864. break;
  865. case OVERLAY_FORMAT_AUTO:
  866. switch (inlink->format) {
  867. case AV_PIX_FMT_YUVA420P:
  868. s->blend_slice = blend_slice_yuva420_pm;
  869. break;
  870. case AV_PIX_FMT_YUVA422P:
  871. s->blend_slice = blend_slice_yuva422_pm;
  872. break;
  873. case AV_PIX_FMT_YUVA444P:
  874. s->blend_slice = blend_slice_yuva444_pm;
  875. break;
  876. case AV_PIX_FMT_ARGB:
  877. case AV_PIX_FMT_RGBA:
  878. case AV_PIX_FMT_BGRA:
  879. case AV_PIX_FMT_ABGR:
  880. s->blend_slice = blend_slice_rgba_pm;
  881. break;
  882. case AV_PIX_FMT_GBRAP:
  883. s->blend_slice = blend_slice_gbrap_pm;
  884. break;
  885. default:
  886. av_assert0(0);
  887. break;
  888. }
  889. break;
  890. }
  891. end:
  892. if (ARCH_X86)
  893. ff_overlay_init_x86(s, s->format, inlink->format,
  894. s->alpha_format, s->main_has_alpha);
  895. return 0;
  896. }
  897. static int do_blend(FFFrameSync *fs)
  898. {
  899. AVFilterContext *ctx = fs->parent;
  900. AVFrame *mainpic, *second;
  901. OverlayContext *s = ctx->priv;
  902. AVFilterLink *inlink = ctx->inputs[0];
  903. int ret;
  904. ret = ff_framesync_dualinput_get_writable(fs, &mainpic, &second);
  905. if (ret < 0)
  906. return ret;
  907. if (!second)
  908. return ff_filter_frame(ctx->outputs[0], mainpic);
  909. if (s->eval_mode == EVAL_MODE_FRAME) {
  910. int64_t pos = mainpic->pkt_pos;
  911. s->var_values[VAR_N] = inlink->frame_count_out;
  912. s->var_values[VAR_T] = mainpic->pts == AV_NOPTS_VALUE ?
  913. NAN : mainpic->pts * av_q2d(inlink->time_base);
  914. s->var_values[VAR_POS] = pos == -1 ? NAN : pos;
  915. s->var_values[VAR_OVERLAY_W] = s->var_values[VAR_OW] = second->width;
  916. s->var_values[VAR_OVERLAY_H] = s->var_values[VAR_OH] = second->height;
  917. s->var_values[VAR_MAIN_W ] = s->var_values[VAR_MW] = mainpic->width;
  918. s->var_values[VAR_MAIN_H ] = s->var_values[VAR_MH] = mainpic->height;
  919. eval_expr(ctx);
  920. av_log(ctx, AV_LOG_DEBUG, "n:%f t:%f pos:%f x:%f xi:%d y:%f yi:%d\n",
  921. s->var_values[VAR_N], s->var_values[VAR_T], s->var_values[VAR_POS],
  922. s->var_values[VAR_X], s->x,
  923. s->var_values[VAR_Y], s->y);
  924. }
  925. if (s->x < mainpic->width && s->x + second->width >= 0 &&
  926. s->y < mainpic->height && s->y + second->height >= 0) {
  927. ThreadData td;
  928. td.dst = mainpic;
  929. td.src = second;
  930. ctx->internal->execute(ctx, s->blend_slice, &td, NULL, FFMIN(FFMAX(1, FFMIN3(s->y + second->height, FFMIN(second->height, mainpic->height), mainpic->height - s->y)),
  931. ff_filter_get_nb_threads(ctx)));
  932. }
  933. return ff_filter_frame(ctx->outputs[0], mainpic);
  934. }
  935. static av_cold int init(AVFilterContext *ctx)
  936. {
  937. OverlayContext *s = ctx->priv;
  938. s->fs.on_event = do_blend;
  939. return 0;
  940. }
  941. static int activate(AVFilterContext *ctx)
  942. {
  943. OverlayContext *s = ctx->priv;
  944. return ff_framesync_activate(&s->fs);
  945. }
  946. #define OFFSET(x) offsetof(OverlayContext, x)
  947. #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
  948. static const AVOption overlay_options[] = {
  949. { "x", "set the x expression", OFFSET(x_expr), AV_OPT_TYPE_STRING, {.str = "0"}, 0, 0, FLAGS },
  950. { "y", "set the y expression", OFFSET(y_expr), AV_OPT_TYPE_STRING, {.str = "0"}, 0, 0, FLAGS },
  951. { "eof_action", "Action to take when encountering EOF from secondary input ",
  952. OFFSET(fs.opt_eof_action), AV_OPT_TYPE_INT, { .i64 = EOF_ACTION_REPEAT },
  953. EOF_ACTION_REPEAT, EOF_ACTION_PASS, .flags = FLAGS, "eof_action" },
  954. { "repeat", "Repeat the previous frame.", 0, AV_OPT_TYPE_CONST, { .i64 = EOF_ACTION_REPEAT }, .flags = FLAGS, "eof_action" },
  955. { "endall", "End both streams.", 0, AV_OPT_TYPE_CONST, { .i64 = EOF_ACTION_ENDALL }, .flags = FLAGS, "eof_action" },
  956. { "pass", "Pass through the main input.", 0, AV_OPT_TYPE_CONST, { .i64 = EOF_ACTION_PASS }, .flags = FLAGS, "eof_action" },
  957. { "eval", "specify when to evaluate expressions", OFFSET(eval_mode), AV_OPT_TYPE_INT, {.i64 = EVAL_MODE_FRAME}, 0, EVAL_MODE_NB-1, FLAGS, "eval" },
  958. { "init", "eval expressions once during initialization", 0, AV_OPT_TYPE_CONST, {.i64=EVAL_MODE_INIT}, .flags = FLAGS, .unit = "eval" },
  959. { "frame", "eval expressions per-frame", 0, AV_OPT_TYPE_CONST, {.i64=EVAL_MODE_FRAME}, .flags = FLAGS, .unit = "eval" },
  960. { "shortest", "force termination when the shortest input terminates", OFFSET(fs.opt_shortest), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, FLAGS },
  961. { "format", "set output format", OFFSET(format), AV_OPT_TYPE_INT, {.i64=OVERLAY_FORMAT_YUV420}, 0, OVERLAY_FORMAT_NB-1, FLAGS, "format" },
  962. { "yuv420", "", 0, AV_OPT_TYPE_CONST, {.i64=OVERLAY_FORMAT_YUV420}, .flags = FLAGS, .unit = "format" },
  963. { "yuv420p10", "", 0, AV_OPT_TYPE_CONST, {.i64=OVERLAY_FORMAT_YUV420P10}, .flags = FLAGS, .unit = "format" },
  964. { "yuv422", "", 0, AV_OPT_TYPE_CONST, {.i64=OVERLAY_FORMAT_YUV422}, .flags = FLAGS, .unit = "format" },
  965. { "yuv422p10", "", 0, AV_OPT_TYPE_CONST, {.i64=OVERLAY_FORMAT_YUV422P10}, .flags = FLAGS, .unit = "format" },
  966. { "yuv444", "", 0, AV_OPT_TYPE_CONST, {.i64=OVERLAY_FORMAT_YUV444}, .flags = FLAGS, .unit = "format" },
  967. { "rgb", "", 0, AV_OPT_TYPE_CONST, {.i64=OVERLAY_FORMAT_RGB}, .flags = FLAGS, .unit = "format" },
  968. { "gbrp", "", 0, AV_OPT_TYPE_CONST, {.i64=OVERLAY_FORMAT_GBRP}, .flags = FLAGS, .unit = "format" },
  969. { "auto", "", 0, AV_OPT_TYPE_CONST, {.i64=OVERLAY_FORMAT_AUTO}, .flags = FLAGS, .unit = "format" },
  970. { "repeatlast", "repeat overlay of the last overlay frame", OFFSET(fs.opt_repeatlast), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1, FLAGS },
  971. { "alpha", "alpha format", OFFSET(alpha_format), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, FLAGS, "alpha_format" },
  972. { "straight", "", 0, AV_OPT_TYPE_CONST, {.i64=0}, .flags = FLAGS, .unit = "alpha_format" },
  973. { "premultiplied", "", 0, AV_OPT_TYPE_CONST, {.i64=1}, .flags = FLAGS, .unit = "alpha_format" },
  974. { NULL }
  975. };
  976. FRAMESYNC_DEFINE_CLASS(overlay, OverlayContext, fs);
  977. static const AVFilterPad avfilter_vf_overlay_inputs[] = {
  978. {
  979. .name = "main",
  980. .type = AVMEDIA_TYPE_VIDEO,
  981. .config_props = config_input_main,
  982. },
  983. {
  984. .name = "overlay",
  985. .type = AVMEDIA_TYPE_VIDEO,
  986. .config_props = config_input_overlay,
  987. },
  988. { NULL }
  989. };
  990. static const AVFilterPad avfilter_vf_overlay_outputs[] = {
  991. {
  992. .name = "default",
  993. .type = AVMEDIA_TYPE_VIDEO,
  994. .config_props = config_output,
  995. },
  996. { NULL }
  997. };
  998. AVFilter ff_vf_overlay = {
  999. .name = "overlay",
  1000. .description = NULL_IF_CONFIG_SMALL("Overlay a video source on top of the input."),
  1001. .preinit = overlay_framesync_preinit,
  1002. .init = init,
  1003. .uninit = uninit,
  1004. .priv_size = sizeof(OverlayContext),
  1005. .priv_class = &overlay_class,
  1006. .query_formats = query_formats,
  1007. .activate = activate,
  1008. .process_command = process_command,
  1009. .inputs = avfilter_vf_overlay_inputs,
  1010. .outputs = avfilter_vf_overlay_outputs,
  1011. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL |
  1012. AVFILTER_FLAG_SLICE_THREADS,
  1013. };