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.

723 lines
26KB

  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. /* #define DEBUG */
  27. #include "avfilter.h"
  28. #include "formats.h"
  29. #include "libavutil/common.h"
  30. #include "libavutil/eval.h"
  31. #include "libavutil/avstring.h"
  32. #include "libavutil/opt.h"
  33. #include "libavutil/pixdesc.h"
  34. #include "libavutil/imgutils.h"
  35. #include "libavutil/mathematics.h"
  36. #include "libavutil/timestamp.h"
  37. #include "internal.h"
  38. #include "bufferqueue.h"
  39. #include "drawutils.h"
  40. #include "video.h"
  41. static const char *const var_names[] = {
  42. "main_w", "W", ///< width of the main video
  43. "main_h", "H", ///< height of the main video
  44. "overlay_w", "w", ///< width of the overlay video
  45. "overlay_h", "h", ///< height of the overlay video
  46. "hsub",
  47. "vsub",
  48. "x",
  49. "y",
  50. "n", ///< number of frame
  51. "pos", ///< position in the file
  52. "t", ///< timestamp expressed in seconds
  53. NULL
  54. };
  55. enum var_name {
  56. VAR_MAIN_W, VAR_MW,
  57. VAR_MAIN_H, VAR_MH,
  58. VAR_OVERLAY_W, VAR_OW,
  59. VAR_OVERLAY_H, VAR_OH,
  60. VAR_HSUB,
  61. VAR_VSUB,
  62. VAR_X,
  63. VAR_Y,
  64. VAR_N,
  65. VAR_POS,
  66. VAR_T,
  67. VAR_VARS_NB
  68. };
  69. #define MAIN 0
  70. #define OVERLAY 1
  71. #define R 0
  72. #define G 1
  73. #define B 2
  74. #define A 3
  75. #define Y 0
  76. #define U 1
  77. #define V 2
  78. typedef struct {
  79. const AVClass *class;
  80. int x, y; ///< position of overlayed picture
  81. int allow_packed_rgb;
  82. uint8_t frame_requested;
  83. uint8_t overlay_eof;
  84. uint8_t main_is_packed_rgb;
  85. uint8_t main_rgba_map[4];
  86. uint8_t main_has_alpha;
  87. uint8_t overlay_is_packed_rgb;
  88. uint8_t overlay_rgba_map[4];
  89. uint8_t overlay_has_alpha;
  90. enum OverlayFormat { OVERLAY_FORMAT_YUV420, OVERLAY_FORMAT_YUV444, OVERLAY_FORMAT_RGB, OVERLAY_FORMAT_NB} format;
  91. enum EvalMode { EVAL_MODE_INIT, EVAL_MODE_FRAME, EVAL_MODE_NB } eval_mode;
  92. AVFrame *overpicref;
  93. struct FFBufQueue queue_main;
  94. struct FFBufQueue queue_over;
  95. int main_pix_step[4]; ///< steps per pixel for each plane of the main output
  96. int overlay_pix_step[4]; ///< steps per pixel for each plane of the overlay
  97. int hsub, vsub; ///< chroma subsampling values
  98. int shortest; ///< terminate stream when the shortest input terminates
  99. double var_values[VAR_VARS_NB];
  100. char *x_expr, *y_expr;
  101. AVExpr *x_pexpr, *y_pexpr;
  102. } OverlayContext;
  103. #define OFFSET(x) offsetof(OverlayContext, x)
  104. #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
  105. static const AVOption overlay_options[] = {
  106. { "x", "set the x expression", OFFSET(x_expr), AV_OPT_TYPE_STRING, {.str = "0"}, CHAR_MIN, CHAR_MAX, FLAGS },
  107. { "y", "set the y expression", OFFSET(y_expr), AV_OPT_TYPE_STRING, {.str = "0"}, CHAR_MIN, CHAR_MAX, FLAGS },
  108. { "eval", "specify when to evaluate expressions", OFFSET(eval_mode), AV_OPT_TYPE_INT, {.i64 = EVAL_MODE_FRAME}, 0, EVAL_MODE_NB-1, FLAGS, "eval" },
  109. { "init", "eval expressions once during initialization", 0, AV_OPT_TYPE_CONST, {.i64=EVAL_MODE_INIT}, .flags = FLAGS, .unit = "eval" },
  110. { "frame", "eval expressions per-frame", 0, AV_OPT_TYPE_CONST, {.i64=EVAL_MODE_FRAME}, .flags = FLAGS, .unit = "eval" },
  111. { "rgb", "force packed RGB in input and output (deprecated)", OFFSET(allow_packed_rgb), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, FLAGS },
  112. { "shortest", "force termination when the shortest input terminates", OFFSET(shortest), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, FLAGS },
  113. { "format", "set output format", OFFSET(format), AV_OPT_TYPE_INT, {.i64=OVERLAY_FORMAT_YUV420}, 0, OVERLAY_FORMAT_NB-1, FLAGS, "format" },
  114. { "yuv420", "", 0, AV_OPT_TYPE_CONST, {.i64=OVERLAY_FORMAT_YUV420}, .flags = FLAGS, .unit = "format" },
  115. { "yuv444", "", 0, AV_OPT_TYPE_CONST, {.i64=OVERLAY_FORMAT_YUV444}, .flags = FLAGS, .unit = "format" },
  116. { "rgb", "", 0, AV_OPT_TYPE_CONST, {.i64=OVERLAY_FORMAT_RGB}, .flags = FLAGS, .unit = "format" },
  117. { NULL }
  118. };
  119. AVFILTER_DEFINE_CLASS(overlay);
  120. static av_cold int init(AVFilterContext *ctx, const char *args)
  121. {
  122. OverlayContext *over = ctx->priv;
  123. if (over->allow_packed_rgb) {
  124. av_log(ctx, AV_LOG_WARNING,
  125. "The rgb option is deprecated and is overriding the format option, use format instead\n");
  126. over->format = OVERLAY_FORMAT_RGB;
  127. }
  128. return 0;
  129. }
  130. static av_cold void uninit(AVFilterContext *ctx)
  131. {
  132. OverlayContext *over = ctx->priv;
  133. av_frame_free(&over->overpicref);
  134. ff_bufqueue_discard_all(&over->queue_main);
  135. ff_bufqueue_discard_all(&over->queue_over);
  136. av_expr_free(over->x_pexpr); over->x_pexpr = NULL;
  137. av_expr_free(over->y_pexpr); over->y_pexpr = NULL;
  138. }
  139. static int query_formats(AVFilterContext *ctx)
  140. {
  141. OverlayContext *over = ctx->priv;
  142. /* overlay formats contains alpha, for avoiding conversion with alpha information loss */
  143. static const enum AVPixelFormat main_pix_fmts_yuv420[] = {
  144. AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUVA420P, AV_PIX_FMT_NONE
  145. };
  146. static const enum AVPixelFormat overlay_pix_fmts_yuv420[] = {
  147. AV_PIX_FMT_YUVA420P, AV_PIX_FMT_NONE
  148. };
  149. static const enum AVPixelFormat main_pix_fmts_yuv444[] = {
  150. AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUVA444P, AV_PIX_FMT_NONE
  151. };
  152. static const enum AVPixelFormat overlay_pix_fmts_yuv444[] = {
  153. AV_PIX_FMT_YUVA444P, AV_PIX_FMT_NONE
  154. };
  155. static const enum AVPixelFormat main_pix_fmts_rgb[] = {
  156. AV_PIX_FMT_ARGB, AV_PIX_FMT_RGBA,
  157. AV_PIX_FMT_ABGR, AV_PIX_FMT_BGRA,
  158. AV_PIX_FMT_RGB24, AV_PIX_FMT_BGR24,
  159. AV_PIX_FMT_NONE
  160. };
  161. static const enum AVPixelFormat overlay_pix_fmts_rgb[] = {
  162. AV_PIX_FMT_ARGB, AV_PIX_FMT_RGBA,
  163. AV_PIX_FMT_ABGR, AV_PIX_FMT_BGRA,
  164. AV_PIX_FMT_NONE
  165. };
  166. AVFilterFormats *main_formats;
  167. AVFilterFormats *overlay_formats;
  168. switch (over->format) {
  169. case OVERLAY_FORMAT_YUV420:
  170. main_formats = ff_make_format_list(main_pix_fmts_yuv420);
  171. overlay_formats = ff_make_format_list(overlay_pix_fmts_yuv420);
  172. break;
  173. case OVERLAY_FORMAT_YUV444:
  174. main_formats = ff_make_format_list(main_pix_fmts_yuv444);
  175. overlay_formats = ff_make_format_list(overlay_pix_fmts_yuv444);
  176. break;
  177. case OVERLAY_FORMAT_RGB:
  178. main_formats = ff_make_format_list(main_pix_fmts_rgb);
  179. overlay_formats = ff_make_format_list(overlay_pix_fmts_rgb);
  180. break;
  181. default:
  182. av_assert0(0);
  183. }
  184. ff_formats_ref(main_formats, &ctx->inputs [MAIN ]->out_formats);
  185. ff_formats_ref(overlay_formats, &ctx->inputs [OVERLAY]->out_formats);
  186. ff_formats_ref(main_formats, &ctx->outputs[MAIN ]->in_formats );
  187. return 0;
  188. }
  189. static const enum AVPixelFormat alpha_pix_fmts[] = {
  190. AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUVA444P,
  191. AV_PIX_FMT_ARGB, AV_PIX_FMT_ABGR, AV_PIX_FMT_RGBA,
  192. AV_PIX_FMT_BGRA, AV_PIX_FMT_NONE
  193. };
  194. static int config_input_main(AVFilterLink *inlink)
  195. {
  196. OverlayContext *over = inlink->dst->priv;
  197. const AVPixFmtDescriptor *pix_desc = av_pix_fmt_desc_get(inlink->format);
  198. av_image_fill_max_pixsteps(over->main_pix_step, NULL, pix_desc);
  199. over->hsub = pix_desc->log2_chroma_w;
  200. over->vsub = pix_desc->log2_chroma_h;
  201. over->main_is_packed_rgb =
  202. ff_fill_rgba_map(over->main_rgba_map, inlink->format) >= 0;
  203. over->main_has_alpha = ff_fmt_is_in(inlink->format, alpha_pix_fmts);
  204. return 0;
  205. }
  206. static inline int normalize_xy(double d, int chroma_sub)
  207. {
  208. if (isnan(d))
  209. return INT_MAX;
  210. return (int)d & ~((1 << chroma_sub) - 1);
  211. }
  212. static void eval_expr(AVFilterContext *ctx)
  213. {
  214. OverlayContext *over = ctx->priv;
  215. over->var_values[VAR_X] = av_expr_eval(over->x_pexpr, over->var_values, NULL);
  216. over->var_values[VAR_Y] = av_expr_eval(over->y_pexpr, over->var_values, NULL);
  217. over->var_values[VAR_X] = av_expr_eval(over->x_pexpr, over->var_values, NULL);
  218. over->x = normalize_xy(over->var_values[VAR_X], over->hsub);
  219. over->y = normalize_xy(over->var_values[VAR_Y], over->vsub);
  220. }
  221. static int config_input_overlay(AVFilterLink *inlink)
  222. {
  223. AVFilterContext *ctx = inlink->dst;
  224. OverlayContext *over = inlink->dst->priv;
  225. char *expr;
  226. int ret;
  227. const AVPixFmtDescriptor *pix_desc = av_pix_fmt_desc_get(inlink->format);
  228. av_image_fill_max_pixsteps(over->overlay_pix_step, NULL, pix_desc);
  229. /* Finish the configuration by evaluating the expressions
  230. now when both inputs are configured. */
  231. over->var_values[VAR_MAIN_W ] = over->var_values[VAR_MW] = ctx->inputs[MAIN ]->w;
  232. over->var_values[VAR_MAIN_H ] = over->var_values[VAR_MH] = ctx->inputs[MAIN ]->h;
  233. over->var_values[VAR_OVERLAY_W] = over->var_values[VAR_OW] = ctx->inputs[OVERLAY]->w;
  234. over->var_values[VAR_OVERLAY_H] = over->var_values[VAR_OH] = ctx->inputs[OVERLAY]->h;
  235. over->var_values[VAR_HSUB] = 1<<pix_desc->log2_chroma_w;
  236. over->var_values[VAR_VSUB] = 1<<pix_desc->log2_chroma_h;
  237. over->var_values[VAR_X] = NAN;
  238. over->var_values[VAR_Y] = NAN;
  239. over->var_values[VAR_N] = 0;
  240. over->var_values[VAR_T] = NAN;
  241. over->var_values[VAR_POS] = NAN;
  242. expr = over->x_expr;
  243. if ((ret = av_expr_parse(&over->x_pexpr, expr, var_names,
  244. NULL, NULL, NULL, NULL, 0, ctx)) < 0)
  245. goto fail;
  246. expr = over->y_expr;
  247. if ((ret = av_expr_parse(&over->y_pexpr, expr, var_names,
  248. NULL, NULL, NULL, NULL, 0, ctx)) < 0)
  249. goto fail;
  250. over->overlay_is_packed_rgb =
  251. ff_fill_rgba_map(over->overlay_rgba_map, inlink->format) >= 0;
  252. over->overlay_has_alpha = ff_fmt_is_in(inlink->format, alpha_pix_fmts);
  253. if (over->eval_mode == EVAL_MODE_INIT) {
  254. eval_expr(ctx);
  255. av_log(ctx, AV_LOG_VERBOSE, "x:%f xi:%d y:%f yi:%d\n",
  256. over->var_values[VAR_X], over->x,
  257. over->var_values[VAR_Y], over->y);
  258. }
  259. av_log(ctx, AV_LOG_VERBOSE,
  260. "main w:%d h:%d fmt:%s overlay w:%d h:%d fmt:%s\n",
  261. ctx->inputs[MAIN]->w, ctx->inputs[MAIN]->h,
  262. av_get_pix_fmt_name(ctx->inputs[MAIN]->format),
  263. ctx->inputs[OVERLAY]->w, ctx->inputs[OVERLAY]->h,
  264. av_get_pix_fmt_name(ctx->inputs[OVERLAY]->format));
  265. return 0;
  266. fail:
  267. av_log(NULL, AV_LOG_ERROR,
  268. "Error when parsing the expression '%s'\n", expr);
  269. return ret;
  270. }
  271. static int config_output(AVFilterLink *outlink)
  272. {
  273. AVFilterContext *ctx = outlink->src;
  274. outlink->w = ctx->inputs[MAIN]->w;
  275. outlink->h = ctx->inputs[MAIN]->h;
  276. outlink->time_base = ctx->inputs[MAIN]->time_base;
  277. return 0;
  278. }
  279. // divide by 255 and round to nearest
  280. // apply a fast variant: (X+127)/255 = ((X+127)*257+257)>>16 = ((X+128)*257)>>16
  281. #define FAST_DIV255(x) ((((x) + 128) * 257) >> 16)
  282. // calculate the unpremultiplied alpha, applying the general equation:
  283. // alpha = alpha_overlay / ( (alpha_main + alpha_overlay) - (alpha_main * alpha_overlay) )
  284. // (((x) << 16) - ((x) << 9) + (x)) is a faster version of: 255 * 255 * x
  285. // ((((x) + (y)) << 8) - ((x) + (y)) - (y) * (x)) is a faster version of: 255 * (x + y)
  286. #define UNPREMULTIPLY_ALPHA(x, y) ((((x) << 16) - ((x) << 9) + (x)) / ((((x) + (y)) << 8) - ((x) + (y)) - (y) * (x)))
  287. /**
  288. * Blend image in src to destination buffer dst at position (x, y).
  289. */
  290. static void blend_image(AVFilterContext *ctx,
  291. AVFrame *dst, AVFrame *src,
  292. int x, int y)
  293. {
  294. OverlayContext *over = ctx->priv;
  295. int i, imax, j, jmax, k, kmax;
  296. const int src_w = src->width;
  297. const int src_h = src->height;
  298. const int dst_w = dst->width;
  299. const int dst_h = dst->height;
  300. if (x >= dst_w || x+dst_w < 0 ||
  301. y >= dst_h || y+dst_h < 0)
  302. return; /* no intersection */
  303. if (over->main_is_packed_rgb) {
  304. uint8_t alpha; ///< the amount of overlay to blend on to main
  305. const int dr = over->main_rgba_map[R];
  306. const int dg = over->main_rgba_map[G];
  307. const int db = over->main_rgba_map[B];
  308. const int da = over->main_rgba_map[A];
  309. const int dstep = over->main_pix_step[0];
  310. const int sr = over->overlay_rgba_map[R];
  311. const int sg = over->overlay_rgba_map[G];
  312. const int sb = over->overlay_rgba_map[B];
  313. const int sa = over->overlay_rgba_map[A];
  314. const int sstep = over->overlay_pix_step[0];
  315. const int main_has_alpha = over->main_has_alpha;
  316. uint8_t *s, *sp, *d, *dp;
  317. i = FFMAX(-y, 0);
  318. sp = src->data[0] + i * src->linesize[0];
  319. dp = dst->data[0] + (y+i) * dst->linesize[0];
  320. for (imax = FFMIN(-y + dst_h, src_h); i < imax; i++) {
  321. j = FFMAX(-x, 0);
  322. s = sp + j * sstep;
  323. d = dp + (x+j) * dstep;
  324. for (jmax = FFMIN(-x + dst_w, src_w); j < jmax; j++) {
  325. alpha = s[sa];
  326. // if the main channel has an alpha channel, alpha has to be calculated
  327. // to create an un-premultiplied (straight) alpha value
  328. if (main_has_alpha && alpha != 0 && alpha != 255) {
  329. uint8_t alpha_d = d[da];
  330. alpha = UNPREMULTIPLY_ALPHA(alpha, alpha_d);
  331. }
  332. switch (alpha) {
  333. case 0:
  334. break;
  335. case 255:
  336. d[dr] = s[sr];
  337. d[dg] = s[sg];
  338. d[db] = s[sb];
  339. break;
  340. default:
  341. // main_value = main_value * (1 - alpha) + overlay_value * alpha
  342. // since alpha is in the range 0-255, the result must divided by 255
  343. d[dr] = FAST_DIV255(d[dr] * (255 - alpha) + s[sr] * alpha);
  344. d[dg] = FAST_DIV255(d[dg] * (255 - alpha) + s[sg] * alpha);
  345. d[db] = FAST_DIV255(d[db] * (255 - alpha) + s[sb] * alpha);
  346. }
  347. if (main_has_alpha) {
  348. switch (alpha) {
  349. case 0:
  350. break;
  351. case 255:
  352. d[da] = s[sa];
  353. break;
  354. default:
  355. // apply alpha compositing: main_alpha += (1-main_alpha) * overlay_alpha
  356. d[da] += FAST_DIV255((255 - d[da]) * s[sa]);
  357. }
  358. }
  359. d += dstep;
  360. s += sstep;
  361. }
  362. dp += dst->linesize[0];
  363. sp += src->linesize[0];
  364. }
  365. } else {
  366. const int main_has_alpha = over->main_has_alpha;
  367. if (main_has_alpha) {
  368. uint8_t alpha; ///< the amount of overlay to blend on to main
  369. uint8_t *s, *sa, *d, *da;
  370. i = FFMAX(-y, 0);
  371. sa = src->data[3] + i * src->linesize[3];
  372. da = dst->data[3] + (y+i) * dst->linesize[3];
  373. for (imax = FFMIN(-y + dst_h, src_h); i < imax; i++) {
  374. j = FFMAX(-x, 0);
  375. s = sa + j;
  376. d = da + x+j;
  377. for (jmax = FFMIN(-x + dst_w, src_w); j < jmax; j++) {
  378. alpha = *s;
  379. if (alpha != 0 && alpha != 255) {
  380. uint8_t alpha_d = *d;
  381. alpha = UNPREMULTIPLY_ALPHA(alpha, alpha_d);
  382. }
  383. switch (alpha) {
  384. case 0:
  385. break;
  386. case 255:
  387. *d = *s;
  388. break;
  389. default:
  390. // apply alpha compositing: main_alpha += (1-main_alpha) * overlay_alpha
  391. *d += FAST_DIV255((255 - *d) * *s);
  392. }
  393. d += 1;
  394. s += 1;
  395. }
  396. da += dst->linesize[3];
  397. sa += src->linesize[3];
  398. }
  399. }
  400. for (i = 0; i < 3; i++) {
  401. int hsub = i ? over->hsub : 0;
  402. int vsub = i ? over->vsub : 0;
  403. int src_wp = FFALIGN(src_w, 1<<hsub) >> hsub;
  404. int src_hp = FFALIGN(src_h, 1<<vsub) >> vsub;
  405. int dst_wp = FFALIGN(dst_w, 1<<hsub) >> hsub;
  406. int dst_hp = FFALIGN(dst_h, 1<<vsub) >> vsub;
  407. int yp = y>>vsub;
  408. int xp = x>>hsub;
  409. uint8_t *s, *sp, *d, *dp, *a, *ap;
  410. j = FFMAX(-yp, 0);
  411. sp = src->data[i] + j * src->linesize[i];
  412. dp = dst->data[i] + (yp+j) * dst->linesize[i];
  413. ap = src->data[3] + (j<<vsub) * src->linesize[3];
  414. for (jmax = FFMIN(-yp + dst_hp, src_hp); j < jmax; j++) {
  415. k = FFMAX(-xp, 0);
  416. d = dp + xp+k;
  417. s = sp + k;
  418. a = ap + (k<<hsub);
  419. for (kmax = FFMIN(-xp + dst_wp, src_wp); k < kmax; k++) {
  420. int alpha_v, alpha_h, alpha;
  421. // average alpha for color components, improve quality
  422. if (hsub && vsub && j+1 < src_hp && k+1 < src_wp) {
  423. alpha = (a[0] + a[src->linesize[3]] +
  424. a[1] + a[src->linesize[3]+1]) >> 2;
  425. } else if (hsub || vsub) {
  426. alpha_h = hsub && k+1 < src_wp ?
  427. (a[0] + a[1]) >> 1 : a[0];
  428. alpha_v = vsub && j+1 < src_hp ?
  429. (a[0] + a[src->linesize[3]]) >> 1 : a[0];
  430. alpha = (alpha_v + alpha_h) >> 1;
  431. } else
  432. alpha = a[0];
  433. // if the main channel has an alpha channel, alpha has to be calculated
  434. // to create an un-premultiplied (straight) alpha value
  435. if (main_has_alpha && alpha != 0 && alpha != 255) {
  436. // average alpha for color components, improve quality
  437. uint8_t alpha_d;
  438. if (hsub && vsub && j+1 < src_hp && k+1 < src_wp) {
  439. alpha_d = (d[0] + d[src->linesize[3]] +
  440. d[1] + d[src->linesize[3]+1]) >> 2;
  441. } else if (hsub || vsub) {
  442. alpha_h = hsub && k+1 < src_wp ?
  443. (d[0] + d[1]) >> 1 : d[0];
  444. alpha_v = vsub && j+1 < src_hp ?
  445. (d[0] + d[src->linesize[3]]) >> 1 : d[0];
  446. alpha_d = (alpha_v + alpha_h) >> 1;
  447. } else
  448. alpha_d = d[0];
  449. alpha = UNPREMULTIPLY_ALPHA(alpha, alpha_d);
  450. }
  451. *d = FAST_DIV255(*d * (255 - alpha) + *s * alpha);
  452. s++;
  453. d++;
  454. a += 1 << hsub;
  455. }
  456. dp += dst->linesize[i];
  457. sp += src->linesize[i];
  458. ap += (1 << vsub) * src->linesize[3];
  459. }
  460. }
  461. }
  462. }
  463. static int try_filter_frame(AVFilterContext *ctx, AVFrame *mainpic)
  464. {
  465. OverlayContext *over = ctx->priv;
  466. AVFilterLink *inlink = ctx->inputs[0];
  467. AVFrame *next_overpic;
  468. int ret;
  469. /* Discard obsolete overlay frames: if there is a next overlay frame with pts
  470. * before the main frame, we can drop the current overlay. */
  471. while (1) {
  472. next_overpic = ff_bufqueue_peek(&over->queue_over, 0);
  473. if (!next_overpic || av_compare_ts(next_overpic->pts, ctx->inputs[OVERLAY]->time_base,
  474. mainpic->pts , ctx->inputs[MAIN]->time_base) > 0)
  475. break;
  476. ff_bufqueue_get(&over->queue_over);
  477. av_frame_free(&over->overpicref);
  478. over->overpicref = next_overpic;
  479. }
  480. /* If there is no next frame and no EOF and the overlay frame is before
  481. * the main frame, we can not know yet if it will be superseded. */
  482. if (!over->queue_over.available && !over->overlay_eof &&
  483. (!over->overpicref || av_compare_ts(over->overpicref->pts, ctx->inputs[OVERLAY]->time_base,
  484. mainpic->pts , ctx->inputs[MAIN]->time_base) < 0))
  485. return AVERROR(EAGAIN);
  486. /* At this point, we know that the current overlay frame extends to the
  487. * time of the main frame. */
  488. av_dlog(ctx, "main_pts:%s main_pts_time:%s",
  489. av_ts2str(mainpic->pts), av_ts2timestr(mainpic->pts, &ctx->inputs[MAIN]->time_base));
  490. if (over->overpicref)
  491. av_dlog(ctx, " over_pts:%s over_pts_time:%s",
  492. av_ts2str(over->overpicref->pts), av_ts2timestr(over->overpicref->pts, &ctx->inputs[OVERLAY]->time_base));
  493. av_dlog(ctx, "\n");
  494. if (over->overpicref) {
  495. if (over->eval_mode == EVAL_MODE_FRAME) {
  496. int64_t pos = av_frame_get_pkt_pos(mainpic);
  497. over->var_values[VAR_T] = mainpic->pts == AV_NOPTS_VALUE ?
  498. NAN : mainpic->pts * av_q2d(inlink->time_base);
  499. over->var_values[VAR_POS] = pos == -1 ? NAN : pos;
  500. eval_expr(ctx);
  501. av_log(ctx, AV_LOG_DEBUG, "n:%f t:%f pos:%f x:%f xi:%d y:%f yi:%d\n",
  502. over->var_values[VAR_N], over->var_values[VAR_T], over->var_values[VAR_POS],
  503. over->var_values[VAR_X], over->x,
  504. over->var_values[VAR_Y], over->y);
  505. }
  506. blend_image(ctx, mainpic, over->overpicref, over->x, over->y);
  507. over->var_values[VAR_N] += 1.0;
  508. }
  509. ret = ff_filter_frame(ctx->outputs[0], mainpic);
  510. av_assert1(ret != AVERROR(EAGAIN));
  511. over->frame_requested = 0;
  512. return ret;
  513. }
  514. static int try_filter_next_frame(AVFilterContext *ctx)
  515. {
  516. OverlayContext *over = ctx->priv;
  517. AVFrame *next_mainpic = ff_bufqueue_peek(&over->queue_main, 0);
  518. int ret;
  519. if (!next_mainpic)
  520. return AVERROR(EAGAIN);
  521. if ((ret = try_filter_frame(ctx, next_mainpic)) == AVERROR(EAGAIN))
  522. return ret;
  523. ff_bufqueue_get(&over->queue_main);
  524. return ret;
  525. }
  526. static int flush_frames(AVFilterContext *ctx)
  527. {
  528. int ret;
  529. while (!(ret = try_filter_next_frame(ctx)));
  530. return ret == AVERROR(EAGAIN) ? 0 : ret;
  531. }
  532. static int filter_frame_main(AVFilterLink *inlink, AVFrame *inpicref)
  533. {
  534. AVFilterContext *ctx = inlink->dst;
  535. OverlayContext *over = ctx->priv;
  536. int ret;
  537. if ((ret = flush_frames(ctx)) < 0)
  538. return ret;
  539. if ((ret = try_filter_frame(ctx, inpicref)) < 0) {
  540. if (ret != AVERROR(EAGAIN))
  541. return ret;
  542. ff_bufqueue_add(ctx, &over->queue_main, inpicref);
  543. }
  544. if (!over->overpicref)
  545. return 0;
  546. flush_frames(ctx);
  547. return 0;
  548. }
  549. static int filter_frame_over(AVFilterLink *inlink, AVFrame *inpicref)
  550. {
  551. AVFilterContext *ctx = inlink->dst;
  552. OverlayContext *over = ctx->priv;
  553. int ret;
  554. if ((ret = flush_frames(ctx)) < 0)
  555. return ret;
  556. ff_bufqueue_add(ctx, &over->queue_over, inpicref);
  557. ret = try_filter_next_frame(ctx);
  558. return ret == AVERROR(EAGAIN) ? 0 : ret;
  559. }
  560. static int request_frame(AVFilterLink *outlink)
  561. {
  562. AVFilterContext *ctx = outlink->src;
  563. OverlayContext *over = ctx->priv;
  564. int input, ret;
  565. if (!try_filter_next_frame(ctx))
  566. return 0;
  567. over->frame_requested = 1;
  568. while (over->frame_requested) {
  569. /* TODO if we had a frame duration, we could guess more accurately */
  570. input = !over->overlay_eof && (over->queue_main.available ||
  571. over->queue_over.available < 2) ?
  572. OVERLAY : MAIN;
  573. ret = ff_request_frame(ctx->inputs[input]);
  574. /* EOF on main is reported immediately */
  575. if (ret == AVERROR_EOF && input == OVERLAY) {
  576. over->overlay_eof = 1;
  577. if (over->shortest)
  578. return ret;
  579. if ((ret = try_filter_next_frame(ctx)) != AVERROR(EAGAIN))
  580. return ret;
  581. ret = 0; /* continue requesting frames on main */
  582. }
  583. if (ret < 0)
  584. return ret;
  585. }
  586. return 0;
  587. }
  588. static const AVFilterPad avfilter_vf_overlay_inputs[] = {
  589. {
  590. .name = "main",
  591. .type = AVMEDIA_TYPE_VIDEO,
  592. .get_video_buffer = ff_null_get_video_buffer,
  593. .config_props = config_input_main,
  594. .filter_frame = filter_frame_main,
  595. .needs_writable = 1,
  596. },
  597. {
  598. .name = "overlay",
  599. .type = AVMEDIA_TYPE_VIDEO,
  600. .config_props = config_input_overlay,
  601. .filter_frame = filter_frame_over,
  602. },
  603. { NULL }
  604. };
  605. static const AVFilterPad avfilter_vf_overlay_outputs[] = {
  606. {
  607. .name = "default",
  608. .type = AVMEDIA_TYPE_VIDEO,
  609. .config_props = config_output,
  610. .request_frame = request_frame,
  611. },
  612. { NULL }
  613. };
  614. static const char *const shorthand[] = { "x", "y", NULL };
  615. AVFilter avfilter_vf_overlay = {
  616. .name = "overlay",
  617. .description = NULL_IF_CONFIG_SMALL("Overlay a video source on top of the input."),
  618. .init = init,
  619. .uninit = uninit,
  620. .priv_size = sizeof(OverlayContext),
  621. .query_formats = query_formats,
  622. .inputs = avfilter_vf_overlay_inputs,
  623. .outputs = avfilter_vf_overlay_outputs,
  624. .priv_class = &overlay_class,
  625. .shorthand = shorthand,
  626. };