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.

639 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. outlink->w = ctx->inputs[MAIN]->w;
  268. outlink->h = ctx->inputs[MAIN]->h;
  269. outlink->time_base = ctx->inputs[MAIN]->time_base;
  270. return 0;
  271. }
  272. // divide by 255 and round to nearest
  273. // apply a fast variant: (X+127)/255 = ((X+127)*257+257)>>16 = ((X+128)*257)>>16
  274. #define FAST_DIV255(x) ((((x) + 128) * 257) >> 16)
  275. // calculate the unpremultiplied alpha, applying the general equation:
  276. // alpha = alpha_overlay / ( (alpha_main + alpha_overlay) - (alpha_main * alpha_overlay) )
  277. // (((x) << 16) - ((x) << 9) + (x)) is a faster version of: 255 * 255 * x
  278. // ((((x) + (y)) << 8) - ((x) + (y)) - (y) * (x)) is a faster version of: 255 * (x + y)
  279. #define UNPREMULTIPLY_ALPHA(x, y) ((((x) << 16) - ((x) << 9) + (x)) / ((((x) + (y)) << 8) - ((x) + (y)) - (y) * (x)))
  280. /**
  281. * Blend image in src to destination buffer dst at position (x, y).
  282. */
  283. static void blend_image(AVFilterContext *ctx,
  284. AVFrame *dst, const AVFrame *src,
  285. int x, int y)
  286. {
  287. OverlayContext *s = ctx->priv;
  288. int i, imax, j, jmax, k, kmax;
  289. const int src_w = src->width;
  290. const int src_h = src->height;
  291. const int dst_w = dst->width;
  292. const int dst_h = dst->height;
  293. if (x >= dst_w || x+dst_w < 0 ||
  294. y >= dst_h || y+dst_h < 0)
  295. return; /* no intersection */
  296. if (s->main_is_packed_rgb) {
  297. uint8_t alpha; ///< the amount of overlay to blend on to main
  298. const int dr = s->main_rgba_map[R];
  299. const int dg = s->main_rgba_map[G];
  300. const int db = s->main_rgba_map[B];
  301. const int da = s->main_rgba_map[A];
  302. const int dstep = s->main_pix_step[0];
  303. const int sr = s->overlay_rgba_map[R];
  304. const int sg = s->overlay_rgba_map[G];
  305. const int sb = s->overlay_rgba_map[B];
  306. const int sa = s->overlay_rgba_map[A];
  307. const int sstep = s->overlay_pix_step[0];
  308. const int main_has_alpha = s->main_has_alpha;
  309. uint8_t *s, *sp, *d, *dp;
  310. i = FFMAX(-y, 0);
  311. sp = src->data[0] + i * src->linesize[0];
  312. dp = dst->data[0] + (y+i) * dst->linesize[0];
  313. for (imax = FFMIN(-y + dst_h, src_h); i < imax; i++) {
  314. j = FFMAX(-x, 0);
  315. s = sp + j * sstep;
  316. d = dp + (x+j) * dstep;
  317. for (jmax = FFMIN(-x + dst_w, src_w); j < jmax; j++) {
  318. alpha = s[sa];
  319. // if the main channel has an alpha channel, alpha has to be calculated
  320. // to create an un-premultiplied (straight) alpha value
  321. if (main_has_alpha && alpha != 0 && alpha != 255) {
  322. uint8_t alpha_d = d[da];
  323. alpha = UNPREMULTIPLY_ALPHA(alpha, alpha_d);
  324. }
  325. switch (alpha) {
  326. case 0:
  327. break;
  328. case 255:
  329. d[dr] = s[sr];
  330. d[dg] = s[sg];
  331. d[db] = s[sb];
  332. break;
  333. default:
  334. // main_value = main_value * (1 - alpha) + overlay_value * alpha
  335. // since alpha is in the range 0-255, the result must divided by 255
  336. d[dr] = FAST_DIV255(d[dr] * (255 - alpha) + s[sr] * alpha);
  337. d[dg] = FAST_DIV255(d[dg] * (255 - alpha) + s[sg] * alpha);
  338. d[db] = FAST_DIV255(d[db] * (255 - alpha) + s[sb] * alpha);
  339. }
  340. if (main_has_alpha) {
  341. switch (alpha) {
  342. case 0:
  343. break;
  344. case 255:
  345. d[da] = s[sa];
  346. break;
  347. default:
  348. // apply alpha compositing: main_alpha += (1-main_alpha) * overlay_alpha
  349. d[da] += FAST_DIV255((255 - d[da]) * s[sa]);
  350. }
  351. }
  352. d += dstep;
  353. s += sstep;
  354. }
  355. dp += dst->linesize[0];
  356. sp += src->linesize[0];
  357. }
  358. } else {
  359. const int main_has_alpha = s->main_has_alpha;
  360. if (main_has_alpha) {
  361. uint8_t alpha; ///< the amount of overlay to blend on to main
  362. uint8_t *s, *sa, *d, *da;
  363. i = FFMAX(-y, 0);
  364. sa = src->data[3] + i * src->linesize[3];
  365. da = dst->data[3] + (y+i) * dst->linesize[3];
  366. for (imax = FFMIN(-y + dst_h, src_h); i < imax; i++) {
  367. j = FFMAX(-x, 0);
  368. s = sa + j;
  369. d = da + x+j;
  370. for (jmax = FFMIN(-x + dst_w, src_w); j < jmax; j++) {
  371. alpha = *s;
  372. if (alpha != 0 && alpha != 255) {
  373. uint8_t alpha_d = *d;
  374. alpha = UNPREMULTIPLY_ALPHA(alpha, alpha_d);
  375. }
  376. switch (alpha) {
  377. case 0:
  378. break;
  379. case 255:
  380. *d = *s;
  381. break;
  382. default:
  383. // apply alpha compositing: main_alpha += (1-main_alpha) * overlay_alpha
  384. *d += FAST_DIV255((255 - *d) * *s);
  385. }
  386. d += 1;
  387. s += 1;
  388. }
  389. da += dst->linesize[3];
  390. sa += src->linesize[3];
  391. }
  392. }
  393. for (i = 0; i < 3; i++) {
  394. int hsub = i ? s->hsub : 0;
  395. int vsub = i ? s->vsub : 0;
  396. int src_wp = FF_CEIL_RSHIFT(src_w, hsub);
  397. int src_hp = FF_CEIL_RSHIFT(src_h, vsub);
  398. int dst_wp = FF_CEIL_RSHIFT(dst_w, hsub);
  399. int dst_hp = FF_CEIL_RSHIFT(dst_h, vsub);
  400. int yp = y>>vsub;
  401. int xp = x>>hsub;
  402. uint8_t *s, *sp, *d, *dp, *a, *ap;
  403. j = FFMAX(-yp, 0);
  404. sp = src->data[i] + j * src->linesize[i];
  405. dp = dst->data[i] + (yp+j) * dst->linesize[i];
  406. ap = src->data[3] + (j<<vsub) * src->linesize[3];
  407. for (jmax = FFMIN(-yp + dst_hp, src_hp); j < jmax; j++) {
  408. k = FFMAX(-xp, 0);
  409. d = dp + xp+k;
  410. s = sp + k;
  411. a = ap + (k<<hsub);
  412. for (kmax = FFMIN(-xp + dst_wp, src_wp); k < kmax; k++) {
  413. int alpha_v, alpha_h, alpha;
  414. // average alpha for color components, improve quality
  415. if (hsub && vsub && j+1 < src_hp && k+1 < src_wp) {
  416. alpha = (a[0] + a[src->linesize[3]] +
  417. a[1] + a[src->linesize[3]+1]) >> 2;
  418. } else if (hsub || vsub) {
  419. alpha_h = hsub && k+1 < src_wp ?
  420. (a[0] + a[1]) >> 1 : a[0];
  421. alpha_v = vsub && j+1 < src_hp ?
  422. (a[0] + a[src->linesize[3]]) >> 1 : a[0];
  423. alpha = (alpha_v + alpha_h) >> 1;
  424. } else
  425. alpha = a[0];
  426. // if the main channel has an alpha channel, alpha has to be calculated
  427. // to create an un-premultiplied (straight) alpha value
  428. if (main_has_alpha && alpha != 0 && alpha != 255) {
  429. // average alpha for color components, improve quality
  430. uint8_t alpha_d;
  431. if (hsub && vsub && j+1 < src_hp && k+1 < src_wp) {
  432. alpha_d = (d[0] + d[src->linesize[3]] +
  433. d[1] + d[src->linesize[3]+1]) >> 2;
  434. } else if (hsub || vsub) {
  435. alpha_h = hsub && k+1 < src_wp ?
  436. (d[0] + d[1]) >> 1 : d[0];
  437. alpha_v = vsub && j+1 < src_hp ?
  438. (d[0] + d[src->linesize[3]]) >> 1 : d[0];
  439. alpha_d = (alpha_v + alpha_h) >> 1;
  440. } else
  441. alpha_d = d[0];
  442. alpha = UNPREMULTIPLY_ALPHA(alpha, alpha_d);
  443. }
  444. *d = FAST_DIV255(*d * (255 - alpha) + *s * alpha);
  445. s++;
  446. d++;
  447. a += 1 << hsub;
  448. }
  449. dp += dst->linesize[i];
  450. sp += src->linesize[i];
  451. ap += (1 << vsub) * src->linesize[3];
  452. }
  453. }
  454. }
  455. }
  456. static AVFrame *do_blend(AVFilterContext *ctx, AVFrame *mainpic,
  457. const AVFrame *second)
  458. {
  459. OverlayContext *s = ctx->priv;
  460. AVFilterLink *inlink = ctx->inputs[0];
  461. /* TODO: reindent */
  462. if (s->eval_mode == EVAL_MODE_FRAME) {
  463. int64_t pos = av_frame_get_pkt_pos(mainpic);
  464. s->var_values[VAR_N] = inlink->frame_count;
  465. s->var_values[VAR_T] = mainpic->pts == AV_NOPTS_VALUE ?
  466. NAN : mainpic->pts * av_q2d(inlink->time_base);
  467. s->var_values[VAR_POS] = pos == -1 ? NAN : pos;
  468. eval_expr(ctx);
  469. av_log(ctx, AV_LOG_DEBUG, "n:%f t:%f pos:%f x:%f xi:%d y:%f yi:%d\n",
  470. s->var_values[VAR_N], s->var_values[VAR_T], s->var_values[VAR_POS],
  471. s->var_values[VAR_X], s->x,
  472. s->var_values[VAR_Y], s->y);
  473. }
  474. blend_image(ctx, mainpic, second, s->x, s->y);
  475. return mainpic;
  476. }
  477. static int filter_frame_main(AVFilterLink *inlink, AVFrame *inpicref)
  478. {
  479. OverlayContext *s = inlink->dst->priv;
  480. return ff_dualinput_filter_frame_main(&s->dinput, inlink, inpicref);
  481. }
  482. static int filter_frame_over(AVFilterLink *inlink, AVFrame *inpicref)
  483. {
  484. OverlayContext *s = inlink->dst->priv;
  485. return ff_dualinput_filter_frame_second(&s->dinput, inlink, inpicref);
  486. }
  487. static int request_frame(AVFilterLink *outlink)
  488. {
  489. OverlayContext *s = outlink->src->priv;
  490. return ff_dualinput_request_frame(&s->dinput, outlink);
  491. }
  492. static av_cold int init(AVFilterContext *ctx)
  493. {
  494. OverlayContext *s = ctx->priv;
  495. if (s->allow_packed_rgb) {
  496. av_log(ctx, AV_LOG_WARNING,
  497. "The rgb option is deprecated and is overriding the format option, use format instead\n");
  498. s->format = OVERLAY_FORMAT_RGB;
  499. }
  500. s->dinput.process = do_blend;
  501. return 0;
  502. }
  503. #define OFFSET(x) offsetof(OverlayContext, x)
  504. #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
  505. static const AVOption overlay_options[] = {
  506. { "x", "set the x expression", OFFSET(x_expr), AV_OPT_TYPE_STRING, {.str = "0"}, CHAR_MIN, CHAR_MAX, FLAGS },
  507. { "y", "set the y expression", OFFSET(y_expr), AV_OPT_TYPE_STRING, {.str = "0"}, CHAR_MIN, CHAR_MAX, FLAGS },
  508. { "eval", "specify when to evaluate expressions", OFFSET(eval_mode), AV_OPT_TYPE_INT, {.i64 = EVAL_MODE_FRAME}, 0, EVAL_MODE_NB-1, FLAGS, "eval" },
  509. { "init", "eval expressions once during initialization", 0, AV_OPT_TYPE_CONST, {.i64=EVAL_MODE_INIT}, .flags = FLAGS, .unit = "eval" },
  510. { "frame", "eval expressions per-frame", 0, AV_OPT_TYPE_CONST, {.i64=EVAL_MODE_FRAME}, .flags = FLAGS, .unit = "eval" },
  511. { "rgb", "force packed RGB in input and output (deprecated)", OFFSET(allow_packed_rgb), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, FLAGS },
  512. { "shortest", "force termination when the shortest input terminates", OFFSET(dinput.shortest), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, FLAGS },
  513. { "format", "set output format", OFFSET(format), AV_OPT_TYPE_INT, {.i64=OVERLAY_FORMAT_YUV420}, 0, OVERLAY_FORMAT_NB-1, FLAGS, "format" },
  514. { "yuv420", "", 0, AV_OPT_TYPE_CONST, {.i64=OVERLAY_FORMAT_YUV420}, .flags = FLAGS, .unit = "format" },
  515. { "yuv444", "", 0, AV_OPT_TYPE_CONST, {.i64=OVERLAY_FORMAT_YUV444}, .flags = FLAGS, .unit = "format" },
  516. { "rgb", "", 0, AV_OPT_TYPE_CONST, {.i64=OVERLAY_FORMAT_RGB}, .flags = FLAGS, .unit = "format" },
  517. { "repeatlast", "repeat overlay of the last overlay frame", OFFSET(dinput.repeatlast), AV_OPT_TYPE_INT, {.i64=1}, 0, 1, FLAGS },
  518. { NULL }
  519. };
  520. AVFILTER_DEFINE_CLASS(overlay);
  521. static const AVFilterPad avfilter_vf_overlay_inputs[] = {
  522. {
  523. .name = "main",
  524. .type = AVMEDIA_TYPE_VIDEO,
  525. .config_props = config_input_main,
  526. .filter_frame = filter_frame_main,
  527. .needs_writable = 1,
  528. },
  529. {
  530. .name = "overlay",
  531. .type = AVMEDIA_TYPE_VIDEO,
  532. .config_props = config_input_overlay,
  533. .filter_frame = filter_frame_over,
  534. },
  535. { NULL }
  536. };
  537. static const AVFilterPad avfilter_vf_overlay_outputs[] = {
  538. {
  539. .name = "default",
  540. .type = AVMEDIA_TYPE_VIDEO,
  541. .config_props = config_output,
  542. .request_frame = request_frame,
  543. },
  544. { NULL }
  545. };
  546. AVFilter avfilter_vf_overlay = {
  547. .name = "overlay",
  548. .description = NULL_IF_CONFIG_SMALL("Overlay a video source on top of the input."),
  549. .init = init,
  550. .uninit = uninit,
  551. .priv_size = sizeof(OverlayContext),
  552. .priv_class = &overlay_class,
  553. .query_formats = query_formats,
  554. .process_command = process_command,
  555. .inputs = avfilter_vf_overlay_inputs,
  556. .outputs = avfilter_vf_overlay_outputs,
  557. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL,
  558. };