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.

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