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.

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