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.

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