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.

910 lines
32KB

  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 "framesync2.h"
  39. #include "video.h"
  40. static const char *const var_names[] = {
  41. "main_w", "W", ///< width of the main video
  42. "main_h", "H", ///< height of the main video
  43. "overlay_w", "w", ///< width of the overlay video
  44. "overlay_h", "h", ///< height of the overlay video
  45. "hsub",
  46. "vsub",
  47. "x",
  48. "y",
  49. "n", ///< number of frame
  50. "pos", ///< position in the file
  51. "t", ///< timestamp expressed in seconds
  52. NULL
  53. };
  54. enum var_name {
  55. VAR_MAIN_W, VAR_MW,
  56. VAR_MAIN_H, VAR_MH,
  57. VAR_OVERLAY_W, VAR_OW,
  58. VAR_OVERLAY_H, VAR_OH,
  59. VAR_HSUB,
  60. VAR_VSUB,
  61. VAR_X,
  62. VAR_Y,
  63. VAR_N,
  64. VAR_POS,
  65. VAR_T,
  66. VAR_VARS_NB
  67. };
  68. enum EOFAction {
  69. EOF_ACTION_REPEAT,
  70. EOF_ACTION_ENDALL,
  71. EOF_ACTION_PASS
  72. };
  73. static const char * const eof_action_str[] = {
  74. "repeat", "endall", "pass"
  75. };
  76. #define MAIN 0
  77. #define OVERLAY 1
  78. #define R 0
  79. #define G 1
  80. #define B 2
  81. #define A 3
  82. #define Y 0
  83. #define U 1
  84. #define V 2
  85. enum EvalMode {
  86. EVAL_MODE_INIT,
  87. EVAL_MODE_FRAME,
  88. EVAL_MODE_NB
  89. };
  90. enum OverlayFormat {
  91. OVERLAY_FORMAT_YUV420,
  92. OVERLAY_FORMAT_YUV422,
  93. OVERLAY_FORMAT_YUV444,
  94. OVERLAY_FORMAT_RGB,
  95. OVERLAY_FORMAT_GBRP,
  96. OVERLAY_FORMAT_AUTO,
  97. OVERLAY_FORMAT_NB
  98. };
  99. typedef struct OverlayContext {
  100. const AVClass *class;
  101. int x, y; ///< position of overlaid picture
  102. uint8_t main_is_packed_rgb;
  103. uint8_t main_rgba_map[4];
  104. uint8_t main_has_alpha;
  105. uint8_t overlay_is_packed_rgb;
  106. uint8_t overlay_rgba_map[4];
  107. uint8_t overlay_has_alpha;
  108. int format; ///< OverlayFormat
  109. int eval_mode; ///< EvalMode
  110. FFFrameSync fs;
  111. int main_pix_step[4]; ///< steps per pixel for each plane of the main output
  112. int overlay_pix_step[4]; ///< steps per pixel for each plane of the overlay
  113. int hsub, vsub; ///< chroma subsampling values
  114. const AVPixFmtDescriptor *main_desc; ///< format descriptor for main input
  115. double var_values[VAR_VARS_NB];
  116. char *x_expr, *y_expr;
  117. int eof_action; ///< action to take on EOF from source
  118. int opt_shortest;
  119. int opt_repeatlast;
  120. AVExpr *x_pexpr, *y_pexpr;
  121. void (*blend_image)(AVFilterContext *ctx, AVFrame *dst, const AVFrame *src, int x, int y);
  122. } OverlayContext;
  123. static av_cold void uninit(AVFilterContext *ctx)
  124. {
  125. OverlayContext *s = ctx->priv;
  126. ff_framesync2_uninit(&s->fs);
  127. av_expr_free(s->x_pexpr); s->x_pexpr = NULL;
  128. av_expr_free(s->y_pexpr); s->y_pexpr = NULL;
  129. }
  130. static inline int normalize_xy(double d, int chroma_sub)
  131. {
  132. if (isnan(d))
  133. return INT_MAX;
  134. return (int)d & ~((1 << chroma_sub) - 1);
  135. }
  136. static void eval_expr(AVFilterContext *ctx)
  137. {
  138. OverlayContext *s = ctx->priv;
  139. s->var_values[VAR_X] = av_expr_eval(s->x_pexpr, s->var_values, NULL);
  140. s->var_values[VAR_Y] = av_expr_eval(s->y_pexpr, s->var_values, NULL);
  141. s->var_values[VAR_X] = av_expr_eval(s->x_pexpr, s->var_values, NULL);
  142. s->x = normalize_xy(s->var_values[VAR_X], s->hsub);
  143. s->y = normalize_xy(s->var_values[VAR_Y], s->vsub);
  144. }
  145. static int set_expr(AVExpr **pexpr, const char *expr, const char *option, void *log_ctx)
  146. {
  147. int ret;
  148. AVExpr *old = NULL;
  149. if (*pexpr)
  150. old = *pexpr;
  151. ret = av_expr_parse(pexpr, expr, var_names,
  152. NULL, NULL, NULL, NULL, 0, log_ctx);
  153. if (ret < 0) {
  154. av_log(log_ctx, AV_LOG_ERROR,
  155. "Error when evaluating the expression '%s' for %s\n",
  156. expr, option);
  157. *pexpr = old;
  158. return ret;
  159. }
  160. av_expr_free(old);
  161. return 0;
  162. }
  163. static int process_command(AVFilterContext *ctx, const char *cmd, const char *args,
  164. char *res, int res_len, int flags)
  165. {
  166. OverlayContext *s = ctx->priv;
  167. int ret;
  168. if (!strcmp(cmd, "x"))
  169. ret = set_expr(&s->x_pexpr, args, cmd, ctx);
  170. else if (!strcmp(cmd, "y"))
  171. ret = set_expr(&s->y_pexpr, args, cmd, ctx);
  172. else
  173. ret = AVERROR(ENOSYS);
  174. if (ret < 0)
  175. return ret;
  176. if (s->eval_mode == EVAL_MODE_INIT) {
  177. eval_expr(ctx);
  178. av_log(ctx, AV_LOG_VERBOSE, "x:%f xi:%d y:%f yi:%d\n",
  179. s->var_values[VAR_X], s->x,
  180. s->var_values[VAR_Y], s->y);
  181. }
  182. return ret;
  183. }
  184. static const enum AVPixelFormat alpha_pix_fmts[] = {
  185. AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA444P,
  186. AV_PIX_FMT_ARGB, AV_PIX_FMT_ABGR, AV_PIX_FMT_RGBA,
  187. AV_PIX_FMT_BGRA, AV_PIX_FMT_GBRAP, AV_PIX_FMT_NONE
  188. };
  189. static int query_formats(AVFilterContext *ctx)
  190. {
  191. OverlayContext *s = ctx->priv;
  192. /* overlay formats contains alpha, for avoiding conversion with alpha information loss */
  193. static const enum AVPixelFormat main_pix_fmts_yuv420[] = {
  194. AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVA420P,
  195. AV_PIX_FMT_NV12, AV_PIX_FMT_NV21,
  196. AV_PIX_FMT_NONE
  197. };
  198. static const enum AVPixelFormat overlay_pix_fmts_yuv420[] = {
  199. AV_PIX_FMT_YUVA420P, AV_PIX_FMT_NONE
  200. };
  201. static const enum AVPixelFormat main_pix_fmts_yuv422[] = {
  202. AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_NONE
  203. };
  204. static const enum AVPixelFormat overlay_pix_fmts_yuv422[] = {
  205. AV_PIX_FMT_YUVA422P, AV_PIX_FMT_NONE
  206. };
  207. static const enum AVPixelFormat main_pix_fmts_yuv444[] = {
  208. AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVA444P, AV_PIX_FMT_NONE
  209. };
  210. static const enum AVPixelFormat overlay_pix_fmts_yuv444[] = {
  211. AV_PIX_FMT_YUVA444P, AV_PIX_FMT_NONE
  212. };
  213. static const enum AVPixelFormat main_pix_fmts_gbrp[] = {
  214. AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRAP, AV_PIX_FMT_NONE
  215. };
  216. static const enum AVPixelFormat overlay_pix_fmts_gbrp[] = {
  217. AV_PIX_FMT_GBRAP, AV_PIX_FMT_NONE
  218. };
  219. static const enum AVPixelFormat main_pix_fmts_rgb[] = {
  220. AV_PIX_FMT_ARGB, AV_PIX_FMT_RGBA,
  221. AV_PIX_FMT_ABGR, AV_PIX_FMT_BGRA,
  222. AV_PIX_FMT_RGB24, AV_PIX_FMT_BGR24,
  223. AV_PIX_FMT_NONE
  224. };
  225. static const enum AVPixelFormat overlay_pix_fmts_rgb[] = {
  226. AV_PIX_FMT_ARGB, AV_PIX_FMT_RGBA,
  227. AV_PIX_FMT_ABGR, AV_PIX_FMT_BGRA,
  228. AV_PIX_FMT_NONE
  229. };
  230. AVFilterFormats *main_formats = NULL;
  231. AVFilterFormats *overlay_formats = NULL;
  232. int ret;
  233. switch (s->format) {
  234. case OVERLAY_FORMAT_YUV420:
  235. if (!(main_formats = ff_make_format_list(main_pix_fmts_yuv420)) ||
  236. !(overlay_formats = ff_make_format_list(overlay_pix_fmts_yuv420))) {
  237. ret = AVERROR(ENOMEM);
  238. goto fail;
  239. }
  240. break;
  241. case OVERLAY_FORMAT_YUV422:
  242. if (!(main_formats = ff_make_format_list(main_pix_fmts_yuv422)) ||
  243. !(overlay_formats = ff_make_format_list(overlay_pix_fmts_yuv422))) {
  244. ret = AVERROR(ENOMEM);
  245. goto fail;
  246. }
  247. break;
  248. case OVERLAY_FORMAT_YUV444:
  249. if (!(main_formats = ff_make_format_list(main_pix_fmts_yuv444)) ||
  250. !(overlay_formats = ff_make_format_list(overlay_pix_fmts_yuv444))) {
  251. ret = AVERROR(ENOMEM);
  252. goto fail;
  253. }
  254. break;
  255. case OVERLAY_FORMAT_RGB:
  256. if (!(main_formats = ff_make_format_list(main_pix_fmts_rgb)) ||
  257. !(overlay_formats = ff_make_format_list(overlay_pix_fmts_rgb))) {
  258. ret = AVERROR(ENOMEM);
  259. goto fail;
  260. }
  261. break;
  262. case OVERLAY_FORMAT_GBRP:
  263. if (!(main_formats = ff_make_format_list(main_pix_fmts_gbrp)) ||
  264. !(overlay_formats = ff_make_format_list(overlay_pix_fmts_gbrp))) {
  265. ret = AVERROR(ENOMEM);
  266. goto fail;
  267. }
  268. break;
  269. case OVERLAY_FORMAT_AUTO:
  270. if (!(main_formats = ff_make_format_list(alpha_pix_fmts))) {
  271. ret = AVERROR(ENOMEM);
  272. goto fail;
  273. }
  274. break;
  275. default:
  276. av_assert0(0);
  277. }
  278. if (s->format == OVERLAY_FORMAT_AUTO) {
  279. ret = ff_set_common_formats(ctx, main_formats);
  280. if (ret < 0)
  281. goto fail;
  282. } else {
  283. if ((ret = ff_formats_ref(main_formats , &ctx->inputs[MAIN]->out_formats )) < 0 ||
  284. (ret = ff_formats_ref(overlay_formats, &ctx->inputs[OVERLAY]->out_formats)) < 0 ||
  285. (ret = ff_formats_ref(main_formats , &ctx->outputs[MAIN]->in_formats )) < 0)
  286. goto fail;
  287. }
  288. return 0;
  289. fail:
  290. if (main_formats)
  291. av_freep(&main_formats->formats);
  292. av_freep(&main_formats);
  293. if (overlay_formats)
  294. av_freep(&overlay_formats->formats);
  295. av_freep(&overlay_formats);
  296. return ret;
  297. }
  298. static int config_input_overlay(AVFilterLink *inlink)
  299. {
  300. AVFilterContext *ctx = inlink->dst;
  301. OverlayContext *s = inlink->dst->priv;
  302. int ret;
  303. const AVPixFmtDescriptor *pix_desc = av_pix_fmt_desc_get(inlink->format);
  304. av_image_fill_max_pixsteps(s->overlay_pix_step, NULL, pix_desc);
  305. /* Finish the configuration by evaluating the expressions
  306. now when both inputs are configured. */
  307. s->var_values[VAR_MAIN_W ] = s->var_values[VAR_MW] = ctx->inputs[MAIN ]->w;
  308. s->var_values[VAR_MAIN_H ] = s->var_values[VAR_MH] = ctx->inputs[MAIN ]->h;
  309. s->var_values[VAR_OVERLAY_W] = s->var_values[VAR_OW] = ctx->inputs[OVERLAY]->w;
  310. s->var_values[VAR_OVERLAY_H] = s->var_values[VAR_OH] = ctx->inputs[OVERLAY]->h;
  311. s->var_values[VAR_HSUB] = 1<<pix_desc->log2_chroma_w;
  312. s->var_values[VAR_VSUB] = 1<<pix_desc->log2_chroma_h;
  313. s->var_values[VAR_X] = NAN;
  314. s->var_values[VAR_Y] = NAN;
  315. s->var_values[VAR_N] = 0;
  316. s->var_values[VAR_T] = NAN;
  317. s->var_values[VAR_POS] = NAN;
  318. if ((ret = set_expr(&s->x_pexpr, s->x_expr, "x", ctx)) < 0 ||
  319. (ret = set_expr(&s->y_pexpr, s->y_expr, "y", ctx)) < 0)
  320. return ret;
  321. s->overlay_is_packed_rgb =
  322. ff_fill_rgba_map(s->overlay_rgba_map, inlink->format) >= 0;
  323. s->overlay_has_alpha = ff_fmt_is_in(inlink->format, alpha_pix_fmts);
  324. if (s->eval_mode == EVAL_MODE_INIT) {
  325. eval_expr(ctx);
  326. av_log(ctx, AV_LOG_VERBOSE, "x:%f xi:%d y:%f yi:%d\n",
  327. s->var_values[VAR_X], s->x,
  328. s->var_values[VAR_Y], s->y);
  329. }
  330. av_log(ctx, AV_LOG_VERBOSE,
  331. "main w:%d h:%d fmt:%s overlay w:%d h:%d fmt:%s eof_action:%s\n",
  332. ctx->inputs[MAIN]->w, ctx->inputs[MAIN]->h,
  333. av_get_pix_fmt_name(ctx->inputs[MAIN]->format),
  334. ctx->inputs[OVERLAY]->w, ctx->inputs[OVERLAY]->h,
  335. av_get_pix_fmt_name(ctx->inputs[OVERLAY]->format),
  336. eof_action_str[s->eof_action]);
  337. return 0;
  338. }
  339. static int config_output(AVFilterLink *outlink)
  340. {
  341. AVFilterContext *ctx = outlink->src;
  342. OverlayContext *s = ctx->priv;
  343. int ret;
  344. if ((ret = ff_framesync2_init_dualinput(&s->fs, ctx)) < 0)
  345. return ret;
  346. if (s->opt_shortest)
  347. s->fs.in[0].after = s->fs.in[1].after = EXT_STOP;
  348. if (!s->opt_repeatlast) {
  349. s->fs.in[1].after = EXT_NULL;
  350. s->fs.in[1].sync = 0;
  351. }
  352. outlink->w = ctx->inputs[MAIN]->w;
  353. outlink->h = ctx->inputs[MAIN]->h;
  354. outlink->time_base = ctx->inputs[MAIN]->time_base;
  355. return ff_framesync2_configure(&s->fs);
  356. }
  357. // divide by 255 and round to nearest
  358. // apply a fast variant: (X+127)/255 = ((X+127)*257+257)>>16 = ((X+128)*257)>>16
  359. #define FAST_DIV255(x) ((((x) + 128) * 257) >> 16)
  360. // calculate the unpremultiplied alpha, applying the general equation:
  361. // alpha = alpha_overlay / ( (alpha_main + alpha_overlay) - (alpha_main * alpha_overlay) )
  362. // (((x) << 16) - ((x) << 9) + (x)) is a faster version of: 255 * 255 * x
  363. // ((((x) + (y)) << 8) - ((x) + (y)) - (y) * (x)) is a faster version of: 255 * (x + y)
  364. #define UNPREMULTIPLY_ALPHA(x, y) ((((x) << 16) - ((x) << 9) + (x)) / ((((x) + (y)) << 8) - ((x) + (y)) - (y) * (x)))
  365. /**
  366. * Blend image in src to destination buffer dst at position (x, y).
  367. */
  368. static void blend_image_packed_rgb(AVFilterContext *ctx,
  369. AVFrame *dst, const AVFrame *src,
  370. int main_has_alpha, int x, int y)
  371. {
  372. OverlayContext *s = ctx->priv;
  373. int i, imax, j, jmax;
  374. const int src_w = src->width;
  375. const int src_h = src->height;
  376. const int dst_w = dst->width;
  377. const int dst_h = dst->height;
  378. uint8_t alpha; ///< the amount of overlay to blend on to main
  379. const int dr = s->main_rgba_map[R];
  380. const int dg = s->main_rgba_map[G];
  381. const int db = s->main_rgba_map[B];
  382. const int da = s->main_rgba_map[A];
  383. const int dstep = s->main_pix_step[0];
  384. const int sr = s->overlay_rgba_map[R];
  385. const int sg = s->overlay_rgba_map[G];
  386. const int sb = s->overlay_rgba_map[B];
  387. const int sa = s->overlay_rgba_map[A];
  388. const int sstep = s->overlay_pix_step[0];
  389. uint8_t *S, *sp, *d, *dp;
  390. i = FFMAX(-y, 0);
  391. sp = src->data[0] + i * src->linesize[0];
  392. dp = dst->data[0] + (y+i) * dst->linesize[0];
  393. for (imax = FFMIN(-y + dst_h, src_h); i < imax; i++) {
  394. j = FFMAX(-x, 0);
  395. S = sp + j * sstep;
  396. d = dp + (x+j) * dstep;
  397. for (jmax = FFMIN(-x + dst_w, src_w); j < jmax; j++) {
  398. alpha = S[sa];
  399. // if the main channel has an alpha channel, alpha has to be calculated
  400. // to create an un-premultiplied (straight) alpha value
  401. if (main_has_alpha && alpha != 0 && alpha != 255) {
  402. uint8_t alpha_d = d[da];
  403. alpha = UNPREMULTIPLY_ALPHA(alpha, alpha_d);
  404. }
  405. switch (alpha) {
  406. case 0:
  407. break;
  408. case 255:
  409. d[dr] = S[sr];
  410. d[dg] = S[sg];
  411. d[db] = S[sb];
  412. break;
  413. default:
  414. // main_value = main_value * (1 - alpha) + overlay_value * alpha
  415. // since alpha is in the range 0-255, the result must divided by 255
  416. d[dr] = FAST_DIV255(d[dr] * (255 - alpha) + S[sr] * alpha);
  417. d[dg] = FAST_DIV255(d[dg] * (255 - alpha) + S[sg] * alpha);
  418. d[db] = FAST_DIV255(d[db] * (255 - alpha) + S[sb] * alpha);
  419. }
  420. if (main_has_alpha) {
  421. switch (alpha) {
  422. case 0:
  423. break;
  424. case 255:
  425. d[da] = S[sa];
  426. break;
  427. default:
  428. // apply alpha compositing: main_alpha += (1-main_alpha) * overlay_alpha
  429. d[da] += FAST_DIV255((255 - d[da]) * S[sa]);
  430. }
  431. }
  432. d += dstep;
  433. S += sstep;
  434. }
  435. dp += dst->linesize[0];
  436. sp += src->linesize[0];
  437. }
  438. }
  439. static av_always_inline void blend_plane(AVFilterContext *ctx,
  440. AVFrame *dst, const AVFrame *src,
  441. int src_w, int src_h,
  442. int dst_w, int dst_h,
  443. int i, int hsub, int vsub,
  444. int x, int y,
  445. int main_has_alpha,
  446. int dst_plane,
  447. int dst_offset,
  448. int dst_step)
  449. {
  450. int src_wp = AV_CEIL_RSHIFT(src_w, hsub);
  451. int src_hp = AV_CEIL_RSHIFT(src_h, vsub);
  452. int dst_wp = AV_CEIL_RSHIFT(dst_w, hsub);
  453. int dst_hp = AV_CEIL_RSHIFT(dst_h, vsub);
  454. int yp = y>>vsub;
  455. int xp = x>>hsub;
  456. uint8_t *s, *sp, *d, *dp, *dap, *a, *da, *ap;
  457. int jmax, j, k, kmax;
  458. j = FFMAX(-yp, 0);
  459. sp = src->data[i] + j * src->linesize[i];
  460. dp = dst->data[dst_plane]
  461. + (yp+j) * dst->linesize[dst_plane]
  462. + dst_offset;
  463. ap = src->data[3] + (j<<vsub) * src->linesize[3];
  464. dap = dst->data[3] + ((yp+j) << vsub) * dst->linesize[3];
  465. for (jmax = FFMIN(-yp + dst_hp, src_hp); j < jmax; j++) {
  466. k = FFMAX(-xp, 0);
  467. d = dp + (xp+k) * dst_step;
  468. s = sp + k;
  469. a = ap + (k<<hsub);
  470. da = dap + ((xp+k) << hsub);
  471. for (kmax = FFMIN(-xp + dst_wp, src_wp); k < kmax; k++) {
  472. int alpha_v, alpha_h, alpha;
  473. // average alpha for color components, improve quality
  474. if (hsub && vsub && j+1 < src_hp && k+1 < src_wp) {
  475. alpha = (a[0] + a[src->linesize[3]] +
  476. a[1] + a[src->linesize[3]+1]) >> 2;
  477. } else if (hsub || vsub) {
  478. alpha_h = hsub && k+1 < src_wp ?
  479. (a[0] + a[1]) >> 1 : a[0];
  480. alpha_v = vsub && j+1 < src_hp ?
  481. (a[0] + a[src->linesize[3]]) >> 1 : a[0];
  482. alpha = (alpha_v + alpha_h) >> 1;
  483. } else
  484. alpha = a[0];
  485. // if the main channel has an alpha channel, alpha has to be calculated
  486. // to create an un-premultiplied (straight) alpha value
  487. if (main_has_alpha && alpha != 0 && alpha != 255) {
  488. // average alpha for color components, improve quality
  489. uint8_t alpha_d;
  490. if (hsub && vsub && j+1 < src_hp && k+1 < src_wp) {
  491. alpha_d = (da[0] + da[dst->linesize[3]] +
  492. da[1] + da[dst->linesize[3]+1]) >> 2;
  493. } else if (hsub || vsub) {
  494. alpha_h = hsub && k+1 < src_wp ?
  495. (da[0] + da[1]) >> 1 : da[0];
  496. alpha_v = vsub && j+1 < src_hp ?
  497. (da[0] + da[dst->linesize[3]]) >> 1 : da[0];
  498. alpha_d = (alpha_v + alpha_h) >> 1;
  499. } else
  500. alpha_d = da[0];
  501. alpha = UNPREMULTIPLY_ALPHA(alpha, alpha_d);
  502. }
  503. *d = FAST_DIV255(*d * (255 - alpha) + *s * alpha);
  504. s++;
  505. d += dst_step;
  506. da += 1 << hsub;
  507. a += 1 << hsub;
  508. }
  509. dp += dst->linesize[dst_plane];
  510. sp += src->linesize[i];
  511. ap += (1 << vsub) * src->linesize[3];
  512. dap += (1 << vsub) * dst->linesize[3];
  513. }
  514. }
  515. static inline void alpha_composite(const AVFrame *src, const AVFrame *dst,
  516. int src_w, int src_h,
  517. int dst_w, int dst_h,
  518. int x, int y)
  519. {
  520. uint8_t alpha; ///< the amount of overlay to blend on to main
  521. uint8_t *s, *sa, *d, *da;
  522. int i, imax, j, jmax;
  523. i = FFMAX(-y, 0);
  524. sa = src->data[3] + i * src->linesize[3];
  525. da = dst->data[3] + (y+i) * dst->linesize[3];
  526. for (imax = FFMIN(-y + dst_h, src_h); i < imax; i++) {
  527. j = FFMAX(-x, 0);
  528. s = sa + j;
  529. d = da + x+j;
  530. for (jmax = FFMIN(-x + dst_w, src_w); j < jmax; j++) {
  531. alpha = *s;
  532. if (alpha != 0 && alpha != 255) {
  533. uint8_t alpha_d = *d;
  534. alpha = UNPREMULTIPLY_ALPHA(alpha, alpha_d);
  535. }
  536. switch (alpha) {
  537. case 0:
  538. break;
  539. case 255:
  540. *d = *s;
  541. break;
  542. default:
  543. // apply alpha compositing: main_alpha += (1-main_alpha) * overlay_alpha
  544. *d += FAST_DIV255((255 - *d) * *s);
  545. }
  546. d += 1;
  547. s += 1;
  548. }
  549. da += dst->linesize[3];
  550. sa += src->linesize[3];
  551. }
  552. }
  553. static av_always_inline void blend_image_yuv(AVFilterContext *ctx,
  554. AVFrame *dst, const AVFrame *src,
  555. int hsub, int vsub,
  556. int main_has_alpha,
  557. int x, int y)
  558. {
  559. OverlayContext *s = ctx->priv;
  560. const int src_w = src->width;
  561. const int src_h = src->height;
  562. const int dst_w = dst->width;
  563. const int dst_h = dst->height;
  564. blend_plane(ctx, dst, src, src_w, src_h, dst_w, dst_h, 0, 0, 0, x, y, main_has_alpha,
  565. s->main_desc->comp[0].plane, s->main_desc->comp[0].offset, s->main_desc->comp[0].step);
  566. blend_plane(ctx, dst, src, src_w, src_h, dst_w, dst_h, 1, hsub, vsub, x, y, main_has_alpha,
  567. s->main_desc->comp[1].plane, s->main_desc->comp[1].offset, s->main_desc->comp[1].step);
  568. blend_plane(ctx, dst, src, src_w, src_h, dst_w, dst_h, 2, hsub, vsub, x, y, main_has_alpha,
  569. s->main_desc->comp[2].plane, s->main_desc->comp[2].offset, s->main_desc->comp[2].step);
  570. if (main_has_alpha)
  571. alpha_composite(src, dst, src_w, src_h, dst_w, dst_h, x, y);
  572. }
  573. static av_always_inline void blend_image_planar_rgb(AVFilterContext *ctx,
  574. AVFrame *dst, const AVFrame *src,
  575. int hsub, int vsub,
  576. int main_has_alpha,
  577. int x, int y)
  578. {
  579. OverlayContext *s = ctx->priv;
  580. const int src_w = src->width;
  581. const int src_h = src->height;
  582. const int dst_w = dst->width;
  583. const int dst_h = dst->height;
  584. blend_plane(ctx, dst, src, src_w, src_h, dst_w, dst_h, 0, 0, 0, x, y, main_has_alpha,
  585. s->main_desc->comp[1].plane, s->main_desc->comp[1].offset, s->main_desc->comp[1].step);
  586. blend_plane(ctx, dst, src, src_w, src_h, dst_w, dst_h, 1, hsub, vsub, x, y, main_has_alpha,
  587. s->main_desc->comp[2].plane, s->main_desc->comp[2].offset, s->main_desc->comp[2].step);
  588. blend_plane(ctx, dst, src, src_w, src_h, dst_w, dst_h, 2, hsub, vsub, x, y, main_has_alpha,
  589. s->main_desc->comp[0].plane, s->main_desc->comp[0].offset, s->main_desc->comp[0].step);
  590. if (main_has_alpha)
  591. alpha_composite(src, dst, src_w, src_h, dst_w, dst_h, x, y);
  592. }
  593. static void blend_image_yuv420(AVFilterContext *ctx, AVFrame *dst, const AVFrame *src, int x, int y)
  594. {
  595. blend_image_yuv(ctx, dst, src, 1, 1, 0, x, y);
  596. }
  597. static void blend_image_yuva420(AVFilterContext *ctx, AVFrame *dst, const AVFrame *src, int x, int y)
  598. {
  599. blend_image_yuv(ctx, dst, src, 1, 1, 1, x, y);
  600. }
  601. static void blend_image_yuv422(AVFilterContext *ctx, AVFrame *dst, const AVFrame *src, int x, int y)
  602. {
  603. blend_image_yuv(ctx, dst, src, 1, 0, 0, x, y);
  604. }
  605. static void blend_image_yuva422(AVFilterContext *ctx, AVFrame *dst, const AVFrame *src, int x, int y)
  606. {
  607. blend_image_yuv(ctx, dst, src, 1, 0, 1, x, y);
  608. }
  609. static void blend_image_yuv444(AVFilterContext *ctx, AVFrame *dst, const AVFrame *src, int x, int y)
  610. {
  611. blend_image_yuv(ctx, dst, src, 0, 0, 0, x, y);
  612. }
  613. static void blend_image_yuva444(AVFilterContext *ctx, AVFrame *dst, const AVFrame *src, int x, int y)
  614. {
  615. blend_image_yuv(ctx, dst, src, 0, 0, 1, x, y);
  616. }
  617. static void blend_image_gbrp(AVFilterContext *ctx, AVFrame *dst, const AVFrame *src, int x, int y)
  618. {
  619. blend_image_planar_rgb(ctx, dst, src, 0, 0, 0, x, y);
  620. }
  621. static void blend_image_gbrap(AVFilterContext *ctx, AVFrame *dst, const AVFrame *src, int x, int y)
  622. {
  623. blend_image_planar_rgb(ctx, dst, src, 0, 0, 1, x, y);
  624. }
  625. static void blend_image_rgb(AVFilterContext *ctx, AVFrame *dst, const AVFrame *src, int x, int y)
  626. {
  627. blend_image_packed_rgb(ctx, dst, src, 0, x, y);
  628. }
  629. static void blend_image_rgba(AVFilterContext *ctx, AVFrame *dst, const AVFrame *src, int x, int y)
  630. {
  631. blend_image_packed_rgb(ctx, dst, src, 1, x, y);
  632. }
  633. static int config_input_main(AVFilterLink *inlink)
  634. {
  635. OverlayContext *s = inlink->dst->priv;
  636. const AVPixFmtDescriptor *pix_desc = av_pix_fmt_desc_get(inlink->format);
  637. av_image_fill_max_pixsteps(s->main_pix_step, NULL, pix_desc);
  638. s->hsub = pix_desc->log2_chroma_w;
  639. s->vsub = pix_desc->log2_chroma_h;
  640. s->main_desc = pix_desc;
  641. s->main_is_packed_rgb =
  642. ff_fill_rgba_map(s->main_rgba_map, inlink->format) >= 0;
  643. s->main_has_alpha = ff_fmt_is_in(inlink->format, alpha_pix_fmts);
  644. switch (s->format) {
  645. case OVERLAY_FORMAT_YUV420:
  646. s->blend_image = s->main_has_alpha ? blend_image_yuva420 : blend_image_yuv420;
  647. break;
  648. case OVERLAY_FORMAT_YUV422:
  649. s->blend_image = s->main_has_alpha ? blend_image_yuva422 : blend_image_yuv422;
  650. break;
  651. case OVERLAY_FORMAT_YUV444:
  652. s->blend_image = s->main_has_alpha ? blend_image_yuva444 : blend_image_yuv444;
  653. break;
  654. case OVERLAY_FORMAT_RGB:
  655. s->blend_image = s->main_has_alpha ? blend_image_rgba : blend_image_rgb;
  656. break;
  657. case OVERLAY_FORMAT_GBRP:
  658. s->blend_image = s->main_has_alpha ? blend_image_gbrap : blend_image_gbrp;
  659. break;
  660. case OVERLAY_FORMAT_AUTO:
  661. switch (inlink->format) {
  662. case AV_PIX_FMT_YUVA420P:
  663. s->blend_image = blend_image_yuva420;
  664. break;
  665. case AV_PIX_FMT_YUVA422P:
  666. s->blend_image = blend_image_yuva422;
  667. break;
  668. case AV_PIX_FMT_YUVA444P:
  669. s->blend_image = blend_image_yuva444;
  670. break;
  671. case AV_PIX_FMT_ARGB:
  672. case AV_PIX_FMT_RGBA:
  673. case AV_PIX_FMT_BGRA:
  674. case AV_PIX_FMT_ABGR:
  675. s->blend_image = blend_image_rgba;
  676. break;
  677. case AV_PIX_FMT_GBRAP:
  678. s->blend_image = blend_image_gbrap;
  679. break;
  680. default:
  681. av_assert0(0);
  682. break;
  683. }
  684. break;
  685. }
  686. return 0;
  687. }
  688. static int do_blend(FFFrameSync *fs)
  689. {
  690. AVFilterContext *ctx = fs->parent;
  691. AVFrame *mainpic, *second;
  692. OverlayContext *s = ctx->priv;
  693. AVFilterLink *inlink = ctx->inputs[0];
  694. int ret;
  695. ret = ff_framesync2_dualinput_get_writable(fs, &mainpic, &second);
  696. if (ret < 0)
  697. return ret;
  698. if (!second)
  699. return ff_filter_frame(ctx->outputs[0], mainpic);
  700. if (s->eval_mode == EVAL_MODE_FRAME) {
  701. int64_t pos = mainpic->pkt_pos;
  702. s->var_values[VAR_N] = inlink->frame_count_out;
  703. s->var_values[VAR_T] = mainpic->pts == AV_NOPTS_VALUE ?
  704. NAN : mainpic->pts * av_q2d(inlink->time_base);
  705. s->var_values[VAR_POS] = pos == -1 ? NAN : pos;
  706. s->var_values[VAR_OVERLAY_W] = s->var_values[VAR_OW] = second->width;
  707. s->var_values[VAR_OVERLAY_H] = s->var_values[VAR_OH] = second->height;
  708. s->var_values[VAR_MAIN_W ] = s->var_values[VAR_MW] = mainpic->width;
  709. s->var_values[VAR_MAIN_H ] = s->var_values[VAR_MH] = mainpic->height;
  710. eval_expr(ctx);
  711. av_log(ctx, AV_LOG_DEBUG, "n:%f t:%f pos:%f x:%f xi:%d y:%f yi:%d\n",
  712. s->var_values[VAR_N], s->var_values[VAR_T], s->var_values[VAR_POS],
  713. s->var_values[VAR_X], s->x,
  714. s->var_values[VAR_Y], s->y);
  715. }
  716. if (s->x < mainpic->width && s->x + second->width >= 0 ||
  717. s->y < mainpic->height && s->y + second->height >= 0)
  718. s->blend_image(ctx, mainpic, second, s->x, s->y);
  719. return ff_filter_frame(ctx->outputs[0], mainpic);
  720. }
  721. static av_cold int init(AVFilterContext *ctx)
  722. {
  723. OverlayContext *s = ctx->priv;
  724. if (!s->opt_repeatlast || s->eof_action == EOF_ACTION_PASS) {
  725. s->opt_repeatlast = 0;
  726. s->eof_action = EOF_ACTION_PASS;
  727. }
  728. if (s->opt_shortest || s->eof_action == EOF_ACTION_ENDALL) {
  729. s->opt_shortest = 1;
  730. s->eof_action = EOF_ACTION_ENDALL;
  731. }
  732. s->fs.on_event = do_blend;
  733. return 0;
  734. }
  735. static int activate(AVFilterContext *ctx)
  736. {
  737. OverlayContext *s = ctx->priv;
  738. return ff_framesync2_activate(&s->fs);
  739. }
  740. #define OFFSET(x) offsetof(OverlayContext, x)
  741. #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
  742. static const AVOption overlay_options[] = {
  743. { "x", "set the x expression", OFFSET(x_expr), AV_OPT_TYPE_STRING, {.str = "0"}, CHAR_MIN, CHAR_MAX, FLAGS },
  744. { "y", "set the y expression", OFFSET(y_expr), AV_OPT_TYPE_STRING, {.str = "0"}, CHAR_MIN, CHAR_MAX, FLAGS },
  745. { "eof_action", "Action to take when encountering EOF from secondary input ",
  746. OFFSET(eof_action), AV_OPT_TYPE_INT, { .i64 = EOF_ACTION_REPEAT },
  747. EOF_ACTION_REPEAT, EOF_ACTION_PASS, .flags = FLAGS, "eof_action" },
  748. { "repeat", "Repeat the previous frame.", 0, AV_OPT_TYPE_CONST, { .i64 = EOF_ACTION_REPEAT }, .flags = FLAGS, "eof_action" },
  749. { "endall", "End both streams.", 0, AV_OPT_TYPE_CONST, { .i64 = EOF_ACTION_ENDALL }, .flags = FLAGS, "eof_action" },
  750. { "pass", "Pass through the main input.", 0, AV_OPT_TYPE_CONST, { .i64 = EOF_ACTION_PASS }, .flags = FLAGS, "eof_action" },
  751. { "eval", "specify when to evaluate expressions", OFFSET(eval_mode), AV_OPT_TYPE_INT, {.i64 = EVAL_MODE_FRAME}, 0, EVAL_MODE_NB-1, FLAGS, "eval" },
  752. { "init", "eval expressions once during initialization", 0, AV_OPT_TYPE_CONST, {.i64=EVAL_MODE_INIT}, .flags = FLAGS, .unit = "eval" },
  753. { "frame", "eval expressions per-frame", 0, AV_OPT_TYPE_CONST, {.i64=EVAL_MODE_FRAME}, .flags = FLAGS, .unit = "eval" },
  754. { "shortest", "force termination when the shortest input terminates", OFFSET(opt_shortest), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, FLAGS },
  755. { "format", "set output format", OFFSET(format), AV_OPT_TYPE_INT, {.i64=OVERLAY_FORMAT_YUV420}, 0, OVERLAY_FORMAT_NB-1, FLAGS, "format" },
  756. { "yuv420", "", 0, AV_OPT_TYPE_CONST, {.i64=OVERLAY_FORMAT_YUV420}, .flags = FLAGS, .unit = "format" },
  757. { "yuv422", "", 0, AV_OPT_TYPE_CONST, {.i64=OVERLAY_FORMAT_YUV422}, .flags = FLAGS, .unit = "format" },
  758. { "yuv444", "", 0, AV_OPT_TYPE_CONST, {.i64=OVERLAY_FORMAT_YUV444}, .flags = FLAGS, .unit = "format" },
  759. { "rgb", "", 0, AV_OPT_TYPE_CONST, {.i64=OVERLAY_FORMAT_RGB}, .flags = FLAGS, .unit = "format" },
  760. { "gbrp", "", 0, AV_OPT_TYPE_CONST, {.i64=OVERLAY_FORMAT_GBRP}, .flags = FLAGS, .unit = "format" },
  761. { "auto", "", 0, AV_OPT_TYPE_CONST, {.i64=OVERLAY_FORMAT_AUTO}, .flags = FLAGS, .unit = "format" },
  762. { "repeatlast", "repeat overlay of the last overlay frame", OFFSET(opt_repeatlast), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1, FLAGS },
  763. { NULL }
  764. };
  765. AVFILTER_DEFINE_CLASS(overlay);
  766. static const AVFilterPad avfilter_vf_overlay_inputs[] = {
  767. {
  768. .name = "main",
  769. .type = AVMEDIA_TYPE_VIDEO,
  770. .config_props = config_input_main,
  771. },
  772. {
  773. .name = "overlay",
  774. .type = AVMEDIA_TYPE_VIDEO,
  775. .config_props = config_input_overlay,
  776. },
  777. { NULL }
  778. };
  779. static const AVFilterPad avfilter_vf_overlay_outputs[] = {
  780. {
  781. .name = "default",
  782. .type = AVMEDIA_TYPE_VIDEO,
  783. .config_props = config_output,
  784. },
  785. { NULL }
  786. };
  787. AVFilter ff_vf_overlay = {
  788. .name = "overlay",
  789. .description = NULL_IF_CONFIG_SMALL("Overlay a video source on top of the input."),
  790. .init = init,
  791. .uninit = uninit,
  792. .priv_size = sizeof(OverlayContext),
  793. .priv_class = &overlay_class,
  794. .query_formats = query_formats,
  795. .activate = activate,
  796. .process_command = process_command,
  797. .inputs = avfilter_vf_overlay_inputs,
  798. .outputs = avfilter_vf_overlay_outputs,
  799. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL,
  800. };