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.

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