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.

648 lines
23KB

  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/opt.h"
  32. #include "libavutil/pixdesc.h"
  33. #include "libavutil/imgutils.h"
  34. #include "libavutil/mathematics.h"
  35. #include "libavutil/opt.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. #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. typedef struct {
  78. const AVClass *class;
  79. int x, y; ///< position of overlayed picture
  80. int allow_packed_rgb;
  81. uint8_t main_is_packed_rgb;
  82. uint8_t main_rgba_map[4];
  83. uint8_t main_has_alpha;
  84. uint8_t overlay_is_packed_rgb;
  85. uint8_t overlay_rgba_map[4];
  86. uint8_t overlay_has_alpha;
  87. enum OverlayFormat { OVERLAY_FORMAT_YUV420, OVERLAY_FORMAT_YUV444, OVERLAY_FORMAT_RGB, OVERLAY_FORMAT_NB} format;
  88. enum EvalMode { EVAL_MODE_INIT, EVAL_MODE_FRAME, EVAL_MODE_NB } eval_mode;
  89. FFDualInputContext dinput;
  90. int main_pix_step[4]; ///< steps per pixel for each plane of the main output
  91. int overlay_pix_step[4]; ///< steps per pixel for each plane of the overlay
  92. int hsub, vsub; ///< chroma subsampling values
  93. double var_values[VAR_VARS_NB];
  94. char *x_expr, *y_expr;
  95. AVExpr *x_pexpr, *y_pexpr;
  96. } OverlayContext;
  97. // TODO: remove forward declaration
  98. static AVFrame *do_blend(AVFilterContext *ctx, AVFrame *mainpic, const AVFrame *second);
  99. static av_cold int init(AVFilterContext *ctx)
  100. {
  101. OverlayContext *s = ctx->priv;
  102. if (s->allow_packed_rgb) {
  103. av_log(ctx, AV_LOG_WARNING,
  104. "The rgb option is deprecated and is overriding the format option, use format instead\n");
  105. s->format = OVERLAY_FORMAT_RGB;
  106. }
  107. s->dinput.process = do_blend;
  108. return 0;
  109. }
  110. static av_cold void uninit(AVFilterContext *ctx)
  111. {
  112. OverlayContext *s = ctx->priv;
  113. ff_dualinput_uninit(&s->dinput);
  114. av_expr_free(s->x_pexpr); s->x_pexpr = NULL;
  115. av_expr_free(s->y_pexpr); s->y_pexpr = NULL;
  116. }
  117. static inline int normalize_xy(double d, int chroma_sub)
  118. {
  119. if (isnan(d))
  120. return INT_MAX;
  121. return (int)d & ~((1 << chroma_sub) - 1);
  122. }
  123. static void eval_expr(AVFilterContext *ctx)
  124. {
  125. OverlayContext *s = ctx->priv;
  126. s->var_values[VAR_X] = av_expr_eval(s->x_pexpr, s->var_values, NULL);
  127. s->var_values[VAR_Y] = av_expr_eval(s->y_pexpr, s->var_values, NULL);
  128. s->var_values[VAR_X] = av_expr_eval(s->x_pexpr, s->var_values, NULL);
  129. s->x = normalize_xy(s->var_values[VAR_X], s->hsub);
  130. s->y = normalize_xy(s->var_values[VAR_Y], s->vsub);
  131. }
  132. static int set_expr(AVExpr **pexpr, const char *expr, const char *option, void *log_ctx)
  133. {
  134. int ret;
  135. AVExpr *old = NULL;
  136. if (*pexpr)
  137. old = *pexpr;
  138. ret = av_expr_parse(pexpr, expr, var_names,
  139. NULL, NULL, NULL, NULL, 0, log_ctx);
  140. if (ret < 0) {
  141. av_log(log_ctx, AV_LOG_ERROR,
  142. "Error when evaluating the expression '%s' for %s\n",
  143. expr, option);
  144. *pexpr = old;
  145. return ret;
  146. }
  147. av_expr_free(old);
  148. return 0;
  149. }
  150. static int process_command(AVFilterContext *ctx, const char *cmd, const char *args,
  151. char *res, int res_len, int flags)
  152. {
  153. OverlayContext *s = ctx->priv;
  154. int ret;
  155. if (!strcmp(cmd, "x"))
  156. ret = set_expr(&s->x_pexpr, args, cmd, ctx);
  157. else if (!strcmp(cmd, "y"))
  158. ret = set_expr(&s->y_pexpr, args, cmd, ctx);
  159. else
  160. ret = AVERROR(ENOSYS);
  161. if (ret < 0)
  162. return ret;
  163. if (s->eval_mode == EVAL_MODE_INIT) {
  164. eval_expr(ctx);
  165. av_log(ctx, AV_LOG_VERBOSE, "x:%f xi:%d y:%f yi:%d\n",
  166. s->var_values[VAR_X], s->x,
  167. s->var_values[VAR_Y], s->y);
  168. }
  169. return ret;
  170. }
  171. static int query_formats(AVFilterContext *ctx)
  172. {
  173. OverlayContext *s = ctx->priv;
  174. /* overlay formats contains alpha, for avoiding conversion with alpha information loss */
  175. static const enum AVPixelFormat main_pix_fmts_yuv420[] = {
  176. AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUVA420P, AV_PIX_FMT_NONE
  177. };
  178. static const enum AVPixelFormat overlay_pix_fmts_yuv420[] = {
  179. AV_PIX_FMT_YUVA420P, AV_PIX_FMT_NONE
  180. };
  181. static const enum AVPixelFormat main_pix_fmts_yuv444[] = {
  182. AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUVA444P, AV_PIX_FMT_NONE
  183. };
  184. static const enum AVPixelFormat overlay_pix_fmts_yuv444[] = {
  185. AV_PIX_FMT_YUVA444P, AV_PIX_FMT_NONE
  186. };
  187. static const enum AVPixelFormat main_pix_fmts_rgb[] = {
  188. AV_PIX_FMT_ARGB, AV_PIX_FMT_RGBA,
  189. AV_PIX_FMT_ABGR, AV_PIX_FMT_BGRA,
  190. AV_PIX_FMT_RGB24, AV_PIX_FMT_BGR24,
  191. AV_PIX_FMT_NONE
  192. };
  193. static const enum AVPixelFormat overlay_pix_fmts_rgb[] = {
  194. AV_PIX_FMT_ARGB, AV_PIX_FMT_RGBA,
  195. AV_PIX_FMT_ABGR, AV_PIX_FMT_BGRA,
  196. AV_PIX_FMT_NONE
  197. };
  198. AVFilterFormats *main_formats;
  199. AVFilterFormats *overlay_formats;
  200. switch (s->format) {
  201. case OVERLAY_FORMAT_YUV420:
  202. main_formats = ff_make_format_list(main_pix_fmts_yuv420);
  203. overlay_formats = ff_make_format_list(overlay_pix_fmts_yuv420);
  204. break;
  205. case OVERLAY_FORMAT_YUV444:
  206. main_formats = ff_make_format_list(main_pix_fmts_yuv444);
  207. overlay_formats = ff_make_format_list(overlay_pix_fmts_yuv444);
  208. break;
  209. case OVERLAY_FORMAT_RGB:
  210. main_formats = ff_make_format_list(main_pix_fmts_rgb);
  211. overlay_formats = ff_make_format_list(overlay_pix_fmts_rgb);
  212. break;
  213. default:
  214. av_assert0(0);
  215. }
  216. ff_formats_ref(main_formats, &ctx->inputs [MAIN ]->out_formats);
  217. ff_formats_ref(overlay_formats, &ctx->inputs [OVERLAY]->out_formats);
  218. ff_formats_ref(main_formats, &ctx->outputs[MAIN ]->in_formats );
  219. return 0;
  220. }
  221. static const enum AVPixelFormat alpha_pix_fmts[] = {
  222. AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUVA444P,
  223. AV_PIX_FMT_ARGB, AV_PIX_FMT_ABGR, AV_PIX_FMT_RGBA,
  224. AV_PIX_FMT_BGRA, AV_PIX_FMT_NONE
  225. };
  226. static int config_input_main(AVFilterLink *inlink)
  227. {
  228. OverlayContext *s = inlink->dst->priv;
  229. const AVPixFmtDescriptor *pix_desc = av_pix_fmt_desc_get(inlink->format);
  230. av_image_fill_max_pixsteps(s->main_pix_step, NULL, pix_desc);
  231. s->hsub = pix_desc->log2_chroma_w;
  232. s->vsub = pix_desc->log2_chroma_h;
  233. s->main_is_packed_rgb =
  234. ff_fill_rgba_map(s->main_rgba_map, inlink->format) >= 0;
  235. s->main_has_alpha = ff_fmt_is_in(inlink->format, alpha_pix_fmts);
  236. return 0;
  237. }
  238. static int config_input_overlay(AVFilterLink *inlink)
  239. {
  240. AVFilterContext *ctx = inlink->dst;
  241. OverlayContext *s = inlink->dst->priv;
  242. int ret;
  243. const AVPixFmtDescriptor *pix_desc = av_pix_fmt_desc_get(inlink->format);
  244. av_image_fill_max_pixsteps(s->overlay_pix_step, NULL, pix_desc);
  245. /* Finish the configuration by evaluating the expressions
  246. now when both inputs are configured. */
  247. s->var_values[VAR_MAIN_W ] = s->var_values[VAR_MW] = ctx->inputs[MAIN ]->w;
  248. s->var_values[VAR_MAIN_H ] = s->var_values[VAR_MH] = ctx->inputs[MAIN ]->h;
  249. s->var_values[VAR_OVERLAY_W] = s->var_values[VAR_OW] = ctx->inputs[OVERLAY]->w;
  250. s->var_values[VAR_OVERLAY_H] = s->var_values[VAR_OH] = ctx->inputs[OVERLAY]->h;
  251. s->var_values[VAR_HSUB] = 1<<pix_desc->log2_chroma_w;
  252. s->var_values[VAR_VSUB] = 1<<pix_desc->log2_chroma_h;
  253. s->var_values[VAR_X] = NAN;
  254. s->var_values[VAR_Y] = NAN;
  255. s->var_values[VAR_N] = 0;
  256. s->var_values[VAR_T] = NAN;
  257. s->var_values[VAR_POS] = NAN;
  258. if ((ret = set_expr(&s->x_pexpr, s->x_expr, "x", ctx)) < 0 ||
  259. (ret = set_expr(&s->y_pexpr, s->y_expr, "y", ctx)) < 0)
  260. return ret;
  261. s->overlay_is_packed_rgb =
  262. ff_fill_rgba_map(s->overlay_rgba_map, inlink->format) >= 0;
  263. s->overlay_has_alpha = ff_fmt_is_in(inlink->format, alpha_pix_fmts);
  264. if (s->eval_mode == EVAL_MODE_INIT) {
  265. eval_expr(ctx);
  266. av_log(ctx, AV_LOG_VERBOSE, "x:%f xi:%d y:%f yi:%d\n",
  267. s->var_values[VAR_X], s->x,
  268. s->var_values[VAR_Y], s->y);
  269. }
  270. av_log(ctx, AV_LOG_VERBOSE,
  271. "main w:%d h:%d fmt:%s overlay w:%d h:%d fmt:%s\n",
  272. ctx->inputs[MAIN]->w, ctx->inputs[MAIN]->h,
  273. av_get_pix_fmt_name(ctx->inputs[MAIN]->format),
  274. ctx->inputs[OVERLAY]->w, ctx->inputs[OVERLAY]->h,
  275. av_get_pix_fmt_name(ctx->inputs[OVERLAY]->format));
  276. return 0;
  277. }
  278. static int config_output(AVFilterLink *outlink)
  279. {
  280. AVFilterContext *ctx = outlink->src;
  281. outlink->w = ctx->inputs[MAIN]->w;
  282. outlink->h = ctx->inputs[MAIN]->h;
  283. outlink->time_base = ctx->inputs[MAIN]->time_base;
  284. return 0;
  285. }
  286. // divide by 255 and round to nearest
  287. // apply a fast variant: (X+127)/255 = ((X+127)*257+257)>>16 = ((X+128)*257)>>16
  288. #define FAST_DIV255(x) ((((x) + 128) * 257) >> 16)
  289. // calculate the unpremultiplied alpha, applying the general equation:
  290. // alpha = alpha_overlay / ( (alpha_main + alpha_overlay) - (alpha_main * alpha_overlay) )
  291. // (((x) << 16) - ((x) << 9) + (x)) is a faster version of: 255 * 255 * x
  292. // ((((x) + (y)) << 8) - ((x) + (y)) - (y) * (x)) is a faster version of: 255 * (x + y)
  293. #define UNPREMULTIPLY_ALPHA(x, y) ((((x) << 16) - ((x) << 9) + (x)) / ((((x) + (y)) << 8) - ((x) + (y)) - (y) * (x)))
  294. /**
  295. * Blend image in src to destination buffer dst at position (x, y).
  296. */
  297. static void blend_image(AVFilterContext *ctx,
  298. AVFrame *dst, const AVFrame *src,
  299. int x, int y)
  300. {
  301. OverlayContext *s = ctx->priv;
  302. int i, imax, j, jmax, k, kmax;
  303. const int src_w = src->width;
  304. const int src_h = src->height;
  305. const int dst_w = dst->width;
  306. const int dst_h = dst->height;
  307. if (x >= dst_w || x+dst_w < 0 ||
  308. y >= dst_h || y+dst_h < 0)
  309. return; /* no intersection */
  310. if (s->main_is_packed_rgb) {
  311. uint8_t alpha; ///< the amount of overlay to blend on to main
  312. const int dr = s->main_rgba_map[R];
  313. const int dg = s->main_rgba_map[G];
  314. const int db = s->main_rgba_map[B];
  315. const int da = s->main_rgba_map[A];
  316. const int dstep = s->main_pix_step[0];
  317. const int sr = s->overlay_rgba_map[R];
  318. const int sg = s->overlay_rgba_map[G];
  319. const int sb = s->overlay_rgba_map[B];
  320. const int sa = s->overlay_rgba_map[A];
  321. const int sstep = s->overlay_pix_step[0];
  322. const int main_has_alpha = s->main_has_alpha;
  323. uint8_t *s, *sp, *d, *dp;
  324. i = FFMAX(-y, 0);
  325. sp = src->data[0] + i * src->linesize[0];
  326. dp = dst->data[0] + (y+i) * dst->linesize[0];
  327. for (imax = FFMIN(-y + dst_h, src_h); i < imax; i++) {
  328. j = FFMAX(-x, 0);
  329. s = sp + j * sstep;
  330. d = dp + (x+j) * dstep;
  331. for (jmax = FFMIN(-x + dst_w, src_w); j < jmax; j++) {
  332. alpha = s[sa];
  333. // if the main channel has an alpha channel, alpha has to be calculated
  334. // to create an un-premultiplied (straight) alpha value
  335. if (main_has_alpha && alpha != 0 && alpha != 255) {
  336. uint8_t alpha_d = d[da];
  337. alpha = UNPREMULTIPLY_ALPHA(alpha, alpha_d);
  338. }
  339. switch (alpha) {
  340. case 0:
  341. break;
  342. case 255:
  343. d[dr] = s[sr];
  344. d[dg] = s[sg];
  345. d[db] = s[sb];
  346. break;
  347. default:
  348. // main_value = main_value * (1 - alpha) + overlay_value * alpha
  349. // since alpha is in the range 0-255, the result must divided by 255
  350. d[dr] = FAST_DIV255(d[dr] * (255 - alpha) + s[sr] * alpha);
  351. d[dg] = FAST_DIV255(d[dg] * (255 - alpha) + s[sg] * alpha);
  352. d[db] = FAST_DIV255(d[db] * (255 - alpha) + s[sb] * alpha);
  353. }
  354. if (main_has_alpha) {
  355. switch (alpha) {
  356. case 0:
  357. break;
  358. case 255:
  359. d[da] = s[sa];
  360. break;
  361. default:
  362. // apply alpha compositing: main_alpha += (1-main_alpha) * overlay_alpha
  363. d[da] += FAST_DIV255((255 - d[da]) * s[sa]);
  364. }
  365. }
  366. d += dstep;
  367. s += sstep;
  368. }
  369. dp += dst->linesize[0];
  370. sp += src->linesize[0];
  371. }
  372. } else {
  373. const int main_has_alpha = s->main_has_alpha;
  374. if (main_has_alpha) {
  375. uint8_t alpha; ///< the amount of overlay to blend on to main
  376. uint8_t *s, *sa, *d, *da;
  377. i = FFMAX(-y, 0);
  378. sa = src->data[3] + i * src->linesize[3];
  379. da = dst->data[3] + (y+i) * dst->linesize[3];
  380. for (imax = FFMIN(-y + dst_h, src_h); i < imax; i++) {
  381. j = FFMAX(-x, 0);
  382. s = sa + j;
  383. d = da + x+j;
  384. for (jmax = FFMIN(-x + dst_w, src_w); j < jmax; j++) {
  385. alpha = *s;
  386. if (alpha != 0 && alpha != 255) {
  387. uint8_t alpha_d = *d;
  388. alpha = UNPREMULTIPLY_ALPHA(alpha, alpha_d);
  389. }
  390. switch (alpha) {
  391. case 0:
  392. break;
  393. case 255:
  394. *d = *s;
  395. break;
  396. default:
  397. // apply alpha compositing: main_alpha += (1-main_alpha) * overlay_alpha
  398. *d += FAST_DIV255((255 - *d) * *s);
  399. }
  400. d += 1;
  401. s += 1;
  402. }
  403. da += dst->linesize[3];
  404. sa += src->linesize[3];
  405. }
  406. }
  407. for (i = 0; i < 3; i++) {
  408. int hsub = i ? s->hsub : 0;
  409. int vsub = i ? s->vsub : 0;
  410. int src_wp = FF_CEIL_RSHIFT(src_w, hsub);
  411. int src_hp = FF_CEIL_RSHIFT(src_h, vsub);
  412. int dst_wp = FF_CEIL_RSHIFT(dst_w, hsub);
  413. int dst_hp = FF_CEIL_RSHIFT(dst_h, vsub);
  414. int yp = y>>vsub;
  415. int xp = x>>hsub;
  416. uint8_t *s, *sp, *d, *dp, *a, *ap;
  417. j = FFMAX(-yp, 0);
  418. sp = src->data[i] + j * src->linesize[i];
  419. dp = dst->data[i] + (yp+j) * dst->linesize[i];
  420. ap = src->data[3] + (j<<vsub) * src->linesize[3];
  421. for (jmax = FFMIN(-yp + dst_hp, src_hp); j < jmax; j++) {
  422. k = FFMAX(-xp, 0);
  423. d = dp + xp+k;
  424. s = sp + k;
  425. a = ap + (k<<hsub);
  426. for (kmax = FFMIN(-xp + dst_wp, src_wp); k < kmax; k++) {
  427. int alpha_v, alpha_h, alpha;
  428. // average alpha for color components, improve quality
  429. if (hsub && vsub && j+1 < src_hp && k+1 < src_wp) {
  430. alpha = (a[0] + a[src->linesize[3]] +
  431. a[1] + a[src->linesize[3]+1]) >> 2;
  432. } else if (hsub || vsub) {
  433. alpha_h = hsub && k+1 < src_wp ?
  434. (a[0] + a[1]) >> 1 : a[0];
  435. alpha_v = vsub && j+1 < src_hp ?
  436. (a[0] + a[src->linesize[3]]) >> 1 : a[0];
  437. alpha = (alpha_v + alpha_h) >> 1;
  438. } else
  439. alpha = a[0];
  440. // if the main channel has an alpha channel, alpha has to be calculated
  441. // to create an un-premultiplied (straight) alpha value
  442. if (main_has_alpha && alpha != 0 && alpha != 255) {
  443. // average alpha for color components, improve quality
  444. uint8_t alpha_d;
  445. if (hsub && vsub && j+1 < src_hp && k+1 < src_wp) {
  446. alpha_d = (d[0] + d[src->linesize[3]] +
  447. d[1] + d[src->linesize[3]+1]) >> 2;
  448. } else if (hsub || vsub) {
  449. alpha_h = hsub && k+1 < src_wp ?
  450. (d[0] + d[1]) >> 1 : d[0];
  451. alpha_v = vsub && j+1 < src_hp ?
  452. (d[0] + d[src->linesize[3]]) >> 1 : d[0];
  453. alpha_d = (alpha_v + alpha_h) >> 1;
  454. } else
  455. alpha_d = d[0];
  456. alpha = UNPREMULTIPLY_ALPHA(alpha, alpha_d);
  457. }
  458. *d = FAST_DIV255(*d * (255 - alpha) + *s * alpha);
  459. s++;
  460. d++;
  461. a += 1 << hsub;
  462. }
  463. dp += dst->linesize[i];
  464. sp += src->linesize[i];
  465. ap += (1 << vsub) * src->linesize[3];
  466. }
  467. }
  468. }
  469. }
  470. static AVFrame *do_blend(AVFilterContext *ctx, AVFrame *mainpic,
  471. const AVFrame *second)
  472. {
  473. OverlayContext *s = ctx->priv;
  474. AVFilterLink *inlink = ctx->inputs[0];
  475. /* TODO: reindent */
  476. if (s->eval_mode == EVAL_MODE_FRAME) {
  477. int64_t pos = av_frame_get_pkt_pos(mainpic);
  478. s->var_values[VAR_N] = inlink->frame_count;
  479. s->var_values[VAR_T] = mainpic->pts == AV_NOPTS_VALUE ?
  480. NAN : mainpic->pts * av_q2d(inlink->time_base);
  481. s->var_values[VAR_POS] = pos == -1 ? NAN : pos;
  482. eval_expr(ctx);
  483. av_log(ctx, AV_LOG_DEBUG, "n:%f t:%f pos:%f x:%f xi:%d y:%f yi:%d\n",
  484. s->var_values[VAR_N], s->var_values[VAR_T], s->var_values[VAR_POS],
  485. s->var_values[VAR_X], s->x,
  486. s->var_values[VAR_Y], s->y);
  487. }
  488. blend_image(ctx, mainpic, second, s->x, s->y);
  489. return mainpic;
  490. }
  491. static int filter_frame_main(AVFilterLink *inlink, AVFrame *inpicref)
  492. {
  493. OverlayContext *s = inlink->dst->priv;
  494. return ff_dualinput_filter_frame_main(&s->dinput, inlink, inpicref);
  495. }
  496. static int filter_frame_over(AVFilterLink *inlink, AVFrame *inpicref)
  497. {
  498. OverlayContext *s = inlink->dst->priv;
  499. return ff_dualinput_filter_frame_second(&s->dinput, inlink, inpicref);
  500. }
  501. static int request_frame(AVFilterLink *outlink)
  502. {
  503. OverlayContext *s = outlink->src->priv;
  504. return ff_dualinput_request_frame(&s->dinput, outlink);
  505. }
  506. #define OFFSET(x) offsetof(OverlayContext, x)
  507. #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
  508. static const AVOption overlay_options[] = {
  509. { "x", "set the x expression", OFFSET(x_expr), AV_OPT_TYPE_STRING, {.str = "0"}, CHAR_MIN, CHAR_MAX, FLAGS },
  510. { "y", "set the y expression", OFFSET(y_expr), AV_OPT_TYPE_STRING, {.str = "0"}, CHAR_MIN, CHAR_MAX, FLAGS },
  511. { "eval", "specify when to evaluate expressions", OFFSET(eval_mode), AV_OPT_TYPE_INT, {.i64 = EVAL_MODE_FRAME}, 0, EVAL_MODE_NB-1, FLAGS, "eval" },
  512. { "init", "eval expressions once during initialization", 0, AV_OPT_TYPE_CONST, {.i64=EVAL_MODE_INIT}, .flags = FLAGS, .unit = "eval" },
  513. { "frame", "eval expressions per-frame", 0, AV_OPT_TYPE_CONST, {.i64=EVAL_MODE_FRAME}, .flags = FLAGS, .unit = "eval" },
  514. { "rgb", "force packed RGB in input and output (deprecated)", OFFSET(allow_packed_rgb), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, FLAGS },
  515. { "shortest", "force termination when the shortest input terminates", OFFSET(dinput.shortest), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, FLAGS },
  516. { "format", "set output format", OFFSET(format), AV_OPT_TYPE_INT, {.i64=OVERLAY_FORMAT_YUV420}, 0, OVERLAY_FORMAT_NB-1, FLAGS, "format" },
  517. { "yuv420", "", 0, AV_OPT_TYPE_CONST, {.i64=OVERLAY_FORMAT_YUV420}, .flags = FLAGS, .unit = "format" },
  518. { "yuv444", "", 0, AV_OPT_TYPE_CONST, {.i64=OVERLAY_FORMAT_YUV444}, .flags = FLAGS, .unit = "format" },
  519. { "rgb", "", 0, AV_OPT_TYPE_CONST, {.i64=OVERLAY_FORMAT_RGB}, .flags = FLAGS, .unit = "format" },
  520. { "repeatlast", "repeat overlay of the last overlay frame", OFFSET(dinput.repeatlast), AV_OPT_TYPE_INT, {.i64=1}, 0, 1, FLAGS },
  521. { NULL }
  522. };
  523. AVFILTER_DEFINE_CLASS(overlay);
  524. static const AVFilterPad avfilter_vf_overlay_inputs[] = {
  525. {
  526. .name = "main",
  527. .type = AVMEDIA_TYPE_VIDEO,
  528. .get_video_buffer = ff_null_get_video_buffer,
  529. .config_props = config_input_main,
  530. .filter_frame = filter_frame_main,
  531. .needs_writable = 1,
  532. },
  533. {
  534. .name = "overlay",
  535. .type = AVMEDIA_TYPE_VIDEO,
  536. .config_props = config_input_overlay,
  537. .filter_frame = filter_frame_over,
  538. },
  539. { NULL }
  540. };
  541. static const AVFilterPad avfilter_vf_overlay_outputs[] = {
  542. {
  543. .name = "default",
  544. .type = AVMEDIA_TYPE_VIDEO,
  545. .config_props = config_output,
  546. .request_frame = request_frame,
  547. },
  548. { NULL }
  549. };
  550. AVFilter avfilter_vf_overlay = {
  551. .name = "overlay",
  552. .description = NULL_IF_CONFIG_SMALL("Overlay a video source on top of the input."),
  553. .init = init,
  554. .uninit = uninit,
  555. .priv_size = sizeof(OverlayContext),
  556. .priv_class = &overlay_class,
  557. .query_formats = query_formats,
  558. .process_command = process_command,
  559. .inputs = avfilter_vf_overlay_inputs,
  560. .outputs = avfilter_vf_overlay_outputs,
  561. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL,
  562. };