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.

997 lines
36KB

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