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.

873 lines
30KB

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