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.

650 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/pixdesc.h"
  32. #include "libavutil/imgutils.h"
  33. #include "libavutil/mathematics.h"
  34. #include "libavutil/opt.h"
  35. #include "internal.h"
  36. #include "dualinput.h"
  37. #include "drawutils.h"
  38. #include "video.h"
  39. static const char *const var_names[] = {
  40. "main_w", "W", ///< width of the main video
  41. "main_h", "H", ///< height of the main video
  42. "overlay_w", "w", ///< width of the overlay video
  43. "overlay_h", "h", ///< height of the overlay video
  44. "hsub",
  45. "vsub",
  46. "x",
  47. "y",
  48. "n", ///< number of frame
  49. "pos", ///< position in the file
  50. "t", ///< timestamp expressed in seconds
  51. NULL
  52. };
  53. enum var_name {
  54. VAR_MAIN_W, VAR_MW,
  55. VAR_MAIN_H, VAR_MH,
  56. VAR_OVERLAY_W, VAR_OW,
  57. VAR_OVERLAY_H, VAR_OH,
  58. VAR_HSUB,
  59. VAR_VSUB,
  60. VAR_X,
  61. VAR_Y,
  62. VAR_N,
  63. VAR_POS,
  64. VAR_T,
  65. VAR_VARS_NB
  66. };
  67. #define MAIN 0
  68. #define OVERLAY 1
  69. #define R 0
  70. #define G 1
  71. #define B 2
  72. #define A 3
  73. #define Y 0
  74. #define U 1
  75. #define V 2
  76. typedef struct {
  77. const AVClass *class;
  78. int x, y; ///< position of overlayed picture
  79. int allow_packed_rgb;
  80. uint8_t main_is_packed_rgb;
  81. uint8_t main_rgba_map[4];
  82. uint8_t main_has_alpha;
  83. uint8_t overlay_is_packed_rgb;
  84. uint8_t overlay_rgba_map[4];
  85. uint8_t overlay_has_alpha;
  86. enum OverlayFormat { OVERLAY_FORMAT_YUV420, OVERLAY_FORMAT_YUV422, OVERLAY_FORMAT_YUV444, OVERLAY_FORMAT_RGB, OVERLAY_FORMAT_NB} format;
  87. enum EvalMode { EVAL_MODE_INIT, EVAL_MODE_FRAME, EVAL_MODE_NB } eval_mode;
  88. FFDualInputContext dinput;
  89. int main_pix_step[4]; ///< steps per pixel for each plane of the main output
  90. int overlay_pix_step[4]; ///< steps per pixel for each plane of the overlay
  91. int hsub, vsub; ///< chroma subsampling values
  92. double var_values[VAR_VARS_NB];
  93. char *x_expr, *y_expr;
  94. AVExpr *x_pexpr, *y_pexpr;
  95. } OverlayContext;
  96. static av_cold void uninit(AVFilterContext *ctx)
  97. {
  98. OverlayContext *s = ctx->priv;
  99. ff_dualinput_uninit(&s->dinput);
  100. av_expr_free(s->x_pexpr); s->x_pexpr = NULL;
  101. av_expr_free(s->y_pexpr); s->y_pexpr = NULL;
  102. }
  103. static inline int normalize_xy(double d, int chroma_sub)
  104. {
  105. if (isnan(d))
  106. return INT_MAX;
  107. return (int)d & ~((1 << chroma_sub) - 1);
  108. }
  109. static void eval_expr(AVFilterContext *ctx)
  110. {
  111. OverlayContext *s = ctx->priv;
  112. s->var_values[VAR_X] = av_expr_eval(s->x_pexpr, s->var_values, NULL);
  113. s->var_values[VAR_Y] = av_expr_eval(s->y_pexpr, s->var_values, NULL);
  114. s->var_values[VAR_X] = av_expr_eval(s->x_pexpr, s->var_values, NULL);
  115. s->x = normalize_xy(s->var_values[VAR_X], s->hsub);
  116. s->y = normalize_xy(s->var_values[VAR_Y], s->vsub);
  117. }
  118. static int set_expr(AVExpr **pexpr, const char *expr, const char *option, void *log_ctx)
  119. {
  120. int ret;
  121. AVExpr *old = NULL;
  122. if (*pexpr)
  123. old = *pexpr;
  124. ret = av_expr_parse(pexpr, expr, var_names,
  125. NULL, NULL, NULL, NULL, 0, log_ctx);
  126. if (ret < 0) {
  127. av_log(log_ctx, AV_LOG_ERROR,
  128. "Error when evaluating the expression '%s' for %s\n",
  129. expr, option);
  130. *pexpr = old;
  131. return ret;
  132. }
  133. av_expr_free(old);
  134. return 0;
  135. }
  136. static int process_command(AVFilterContext *ctx, const char *cmd, const char *args,
  137. char *res, int res_len, int flags)
  138. {
  139. OverlayContext *s = ctx->priv;
  140. int ret;
  141. if (!strcmp(cmd, "x"))
  142. ret = set_expr(&s->x_pexpr, args, cmd, ctx);
  143. else if (!strcmp(cmd, "y"))
  144. ret = set_expr(&s->y_pexpr, args, cmd, ctx);
  145. else
  146. ret = AVERROR(ENOSYS);
  147. if (ret < 0)
  148. return ret;
  149. if (s->eval_mode == EVAL_MODE_INIT) {
  150. eval_expr(ctx);
  151. av_log(ctx, AV_LOG_VERBOSE, "x:%f xi:%d y:%f yi:%d\n",
  152. s->var_values[VAR_X], s->x,
  153. s->var_values[VAR_Y], s->y);
  154. }
  155. return ret;
  156. }
  157. static int query_formats(AVFilterContext *ctx)
  158. {
  159. OverlayContext *s = ctx->priv;
  160. /* overlay formats contains alpha, for avoiding conversion with alpha information loss */
  161. static const enum AVPixelFormat main_pix_fmts_yuv420[] = {
  162. AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUVA420P, AV_PIX_FMT_NONE
  163. };
  164. static const enum AVPixelFormat overlay_pix_fmts_yuv420[] = {
  165. AV_PIX_FMT_YUVA420P, AV_PIX_FMT_NONE
  166. };
  167. static const enum AVPixelFormat main_pix_fmts_yuv422[] = {
  168. AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_NONE
  169. };
  170. static const enum AVPixelFormat overlay_pix_fmts_yuv422[] = {
  171. AV_PIX_FMT_YUVA422P, AV_PIX_FMT_NONE
  172. };
  173. static const enum AVPixelFormat main_pix_fmts_yuv444[] = {
  174. AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUVA444P, AV_PIX_FMT_NONE
  175. };
  176. static const enum AVPixelFormat overlay_pix_fmts_yuv444[] = {
  177. AV_PIX_FMT_YUVA444P, AV_PIX_FMT_NONE
  178. };
  179. static const enum AVPixelFormat main_pix_fmts_rgb[] = {
  180. AV_PIX_FMT_ARGB, AV_PIX_FMT_RGBA,
  181. AV_PIX_FMT_ABGR, AV_PIX_FMT_BGRA,
  182. AV_PIX_FMT_RGB24, AV_PIX_FMT_BGR24,
  183. AV_PIX_FMT_NONE
  184. };
  185. static const enum AVPixelFormat overlay_pix_fmts_rgb[] = {
  186. AV_PIX_FMT_ARGB, AV_PIX_FMT_RGBA,
  187. AV_PIX_FMT_ABGR, AV_PIX_FMT_BGRA,
  188. AV_PIX_FMT_NONE
  189. };
  190. AVFilterFormats *main_formats;
  191. AVFilterFormats *overlay_formats;
  192. switch (s->format) {
  193. case OVERLAY_FORMAT_YUV420:
  194. main_formats = ff_make_format_list(main_pix_fmts_yuv420);
  195. overlay_formats = ff_make_format_list(overlay_pix_fmts_yuv420);
  196. break;
  197. case OVERLAY_FORMAT_YUV422:
  198. main_formats = ff_make_format_list(main_pix_fmts_yuv422);
  199. overlay_formats = ff_make_format_list(overlay_pix_fmts_yuv422);
  200. break;
  201. case OVERLAY_FORMAT_YUV444:
  202. main_formats = ff_make_format_list(main_pix_fmts_yuv444);
  203. overlay_formats = ff_make_format_list(overlay_pix_fmts_yuv444);
  204. break;
  205. case OVERLAY_FORMAT_RGB:
  206. main_formats = ff_make_format_list(main_pix_fmts_rgb);
  207. overlay_formats = ff_make_format_list(overlay_pix_fmts_rgb);
  208. break;
  209. default:
  210. av_assert0(0);
  211. }
  212. ff_formats_ref(main_formats, &ctx->inputs [MAIN ]->out_formats);
  213. ff_formats_ref(overlay_formats, &ctx->inputs [OVERLAY]->out_formats);
  214. ff_formats_ref(main_formats, &ctx->outputs[MAIN ]->in_formats );
  215. return 0;
  216. }
  217. static const enum AVPixelFormat alpha_pix_fmts[] = {
  218. AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUVA444P,
  219. AV_PIX_FMT_ARGB, AV_PIX_FMT_ABGR, AV_PIX_FMT_RGBA,
  220. AV_PIX_FMT_BGRA, AV_PIX_FMT_NONE
  221. };
  222. static int config_input_main(AVFilterLink *inlink)
  223. {
  224. OverlayContext *s = inlink->dst->priv;
  225. const AVPixFmtDescriptor *pix_desc = av_pix_fmt_desc_get(inlink->format);
  226. av_image_fill_max_pixsteps(s->main_pix_step, NULL, pix_desc);
  227. s->hsub = pix_desc->log2_chroma_w;
  228. s->vsub = pix_desc->log2_chroma_h;
  229. s->main_is_packed_rgb =
  230. ff_fill_rgba_map(s->main_rgba_map, inlink->format) >= 0;
  231. s->main_has_alpha = ff_fmt_is_in(inlink->format, alpha_pix_fmts);
  232. return 0;
  233. }
  234. static int config_input_overlay(AVFilterLink *inlink)
  235. {
  236. AVFilterContext *ctx = inlink->dst;
  237. OverlayContext *s = inlink->dst->priv;
  238. int ret;
  239. const AVPixFmtDescriptor *pix_desc = av_pix_fmt_desc_get(inlink->format);
  240. av_image_fill_max_pixsteps(s->overlay_pix_step, NULL, pix_desc);
  241. /* Finish the configuration by evaluating the expressions
  242. now when both inputs are configured. */
  243. s->var_values[VAR_MAIN_W ] = s->var_values[VAR_MW] = ctx->inputs[MAIN ]->w;
  244. s->var_values[VAR_MAIN_H ] = s->var_values[VAR_MH] = ctx->inputs[MAIN ]->h;
  245. s->var_values[VAR_OVERLAY_W] = s->var_values[VAR_OW] = ctx->inputs[OVERLAY]->w;
  246. s->var_values[VAR_OVERLAY_H] = s->var_values[VAR_OH] = ctx->inputs[OVERLAY]->h;
  247. s->var_values[VAR_HSUB] = 1<<pix_desc->log2_chroma_w;
  248. s->var_values[VAR_VSUB] = 1<<pix_desc->log2_chroma_h;
  249. s->var_values[VAR_X] = NAN;
  250. s->var_values[VAR_Y] = NAN;
  251. s->var_values[VAR_N] = 0;
  252. s->var_values[VAR_T] = NAN;
  253. s->var_values[VAR_POS] = NAN;
  254. if ((ret = set_expr(&s->x_pexpr, s->x_expr, "x", ctx)) < 0 ||
  255. (ret = set_expr(&s->y_pexpr, s->y_expr, "y", ctx)) < 0)
  256. return ret;
  257. s->overlay_is_packed_rgb =
  258. ff_fill_rgba_map(s->overlay_rgba_map, inlink->format) >= 0;
  259. s->overlay_has_alpha = ff_fmt_is_in(inlink->format, alpha_pix_fmts);
  260. if (s->eval_mode == EVAL_MODE_INIT) {
  261. eval_expr(ctx);
  262. av_log(ctx, AV_LOG_VERBOSE, "x:%f xi:%d y:%f yi:%d\n",
  263. s->var_values[VAR_X], s->x,
  264. s->var_values[VAR_Y], s->y);
  265. }
  266. av_log(ctx, AV_LOG_VERBOSE,
  267. "main w:%d h:%d fmt:%s overlay w:%d h:%d fmt:%s\n",
  268. ctx->inputs[MAIN]->w, ctx->inputs[MAIN]->h,
  269. av_get_pix_fmt_name(ctx->inputs[MAIN]->format),
  270. ctx->inputs[OVERLAY]->w, ctx->inputs[OVERLAY]->h,
  271. av_get_pix_fmt_name(ctx->inputs[OVERLAY]->format));
  272. return 0;
  273. }
  274. static int config_output(AVFilterLink *outlink)
  275. {
  276. AVFilterContext *ctx = outlink->src;
  277. OverlayContext *s = ctx->priv;
  278. int ret;
  279. if ((ret = ff_dualinput_init(ctx, &s->dinput)) < 0)
  280. return ret;
  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+src_w < 0 ||
  308. y >= dst_h || y+src_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(AVFilterLink *inlink, AVFrame *inpicref)
  492. {
  493. OverlayContext *s = inlink->dst->priv;
  494. return ff_dualinput_filter_frame(&s->dinput, inlink, inpicref);
  495. }
  496. static int request_frame(AVFilterLink *outlink)
  497. {
  498. OverlayContext *s = outlink->src->priv;
  499. return ff_dualinput_request_frame(&s->dinput, outlink);
  500. }
  501. static av_cold int init(AVFilterContext *ctx)
  502. {
  503. OverlayContext *s = ctx->priv;
  504. if (s->allow_packed_rgb) {
  505. av_log(ctx, AV_LOG_WARNING,
  506. "The rgb option is deprecated and is overriding the format option, use format instead\n");
  507. s->format = OVERLAY_FORMAT_RGB;
  508. }
  509. s->dinput.process = do_blend;
  510. return 0;
  511. }
  512. #define OFFSET(x) offsetof(OverlayContext, x)
  513. #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
  514. static const AVOption overlay_options[] = {
  515. { "x", "set the x expression", OFFSET(x_expr), AV_OPT_TYPE_STRING, {.str = "0"}, CHAR_MIN, CHAR_MAX, FLAGS },
  516. { "y", "set the y expression", OFFSET(y_expr), AV_OPT_TYPE_STRING, {.str = "0"}, CHAR_MIN, CHAR_MAX, FLAGS },
  517. { "eval", "specify when to evaluate expressions", OFFSET(eval_mode), AV_OPT_TYPE_INT, {.i64 = EVAL_MODE_FRAME}, 0, EVAL_MODE_NB-1, FLAGS, "eval" },
  518. { "init", "eval expressions once during initialization", 0, AV_OPT_TYPE_CONST, {.i64=EVAL_MODE_INIT}, .flags = FLAGS, .unit = "eval" },
  519. { "frame", "eval expressions per-frame", 0, AV_OPT_TYPE_CONST, {.i64=EVAL_MODE_FRAME}, .flags = FLAGS, .unit = "eval" },
  520. { "rgb", "force packed RGB in input and output (deprecated)", OFFSET(allow_packed_rgb), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, FLAGS },
  521. { "shortest", "force termination when the shortest input terminates", OFFSET(dinput.shortest), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, FLAGS },
  522. { "format", "set output format", OFFSET(format), AV_OPT_TYPE_INT, {.i64=OVERLAY_FORMAT_YUV420}, 0, OVERLAY_FORMAT_NB-1, FLAGS, "format" },
  523. { "yuv420", "", 0, AV_OPT_TYPE_CONST, {.i64=OVERLAY_FORMAT_YUV420}, .flags = FLAGS, .unit = "format" },
  524. { "yuv422", "", 0, AV_OPT_TYPE_CONST, {.i64=OVERLAY_FORMAT_YUV422}, .flags = FLAGS, .unit = "format" },
  525. { "yuv444", "", 0, AV_OPT_TYPE_CONST, {.i64=OVERLAY_FORMAT_YUV444}, .flags = FLAGS, .unit = "format" },
  526. { "rgb", "", 0, AV_OPT_TYPE_CONST, {.i64=OVERLAY_FORMAT_RGB}, .flags = FLAGS, .unit = "format" },
  527. { "repeatlast", "repeat overlay of the last overlay frame", OFFSET(dinput.repeatlast), AV_OPT_TYPE_INT, {.i64=1}, 0, 1, FLAGS },
  528. { NULL }
  529. };
  530. AVFILTER_DEFINE_CLASS(overlay);
  531. static const AVFilterPad avfilter_vf_overlay_inputs[] = {
  532. {
  533. .name = "main",
  534. .type = AVMEDIA_TYPE_VIDEO,
  535. .config_props = config_input_main,
  536. .filter_frame = filter_frame,
  537. .needs_writable = 1,
  538. },
  539. {
  540. .name = "overlay",
  541. .type = AVMEDIA_TYPE_VIDEO,
  542. .config_props = config_input_overlay,
  543. .filter_frame = filter_frame,
  544. },
  545. { NULL }
  546. };
  547. static const AVFilterPad avfilter_vf_overlay_outputs[] = {
  548. {
  549. .name = "default",
  550. .type = AVMEDIA_TYPE_VIDEO,
  551. .config_props = config_output,
  552. .request_frame = request_frame,
  553. },
  554. { NULL }
  555. };
  556. AVFilter ff_vf_overlay = {
  557. .name = "overlay",
  558. .description = NULL_IF_CONFIG_SMALL("Overlay a video source on top of the input."),
  559. .init = init,
  560. .uninit = uninit,
  561. .priv_size = sizeof(OverlayContext),
  562. .priv_class = &overlay_class,
  563. .query_formats = query_formats,
  564. .process_command = process_command,
  565. .inputs = avfilter_vf_overlay_inputs,
  566. .outputs = avfilter_vf_overlay_outputs,
  567. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL,
  568. };