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.

784 lines
27KB

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