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.

886 lines
31KB

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