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.

618 lines
22KB

  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. NULL
  47. };
  48. enum var_name {
  49. VAR_MAIN_W, VAR_MW,
  50. VAR_MAIN_H, VAR_MH,
  51. VAR_OVERLAY_W, VAR_OW,
  52. VAR_OVERLAY_H, VAR_OH,
  53. VAR_VARS_NB
  54. };
  55. #define MAIN 0
  56. #define OVERLAY 1
  57. #define R 0
  58. #define G 1
  59. #define B 2
  60. #define A 3
  61. #define Y 0
  62. #define U 1
  63. #define V 2
  64. typedef struct {
  65. const AVClass *class;
  66. int x, y; ///< position of overlayed picture
  67. int allow_packed_rgb;
  68. uint8_t frame_requested;
  69. uint8_t overlay_eof;
  70. uint8_t main_is_packed_rgb;
  71. uint8_t main_rgba_map[4];
  72. uint8_t main_has_alpha;
  73. uint8_t overlay_is_packed_rgb;
  74. uint8_t overlay_rgba_map[4];
  75. uint8_t overlay_has_alpha;
  76. AVFilterBufferRef *overpicref;
  77. struct FFBufQueue queue_main;
  78. struct FFBufQueue queue_over;
  79. int main_pix_step[4]; ///< steps per pixel for each plane of the main output
  80. int overlay_pix_step[4]; ///< steps per pixel for each plane of the overlay
  81. int hsub, vsub; ///< chroma subsampling values
  82. char *x_expr, *y_expr;
  83. } OverlayContext;
  84. #define OFFSET(x) offsetof(OverlayContext, x)
  85. #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
  86. static const AVOption overlay_options[] = {
  87. { "x", "set the x expression", OFFSET(x_expr), AV_OPT_TYPE_STRING, {.str = "0"}, CHAR_MIN, CHAR_MAX, FLAGS },
  88. { "y", "set the y expression", OFFSET(y_expr), AV_OPT_TYPE_STRING, {.str = "0"}, CHAR_MIN, CHAR_MAX, FLAGS },
  89. {"rgb", "force packed RGB in input and output", OFFSET(allow_packed_rgb), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, FLAGS },
  90. {NULL},
  91. };
  92. AVFILTER_DEFINE_CLASS(overlay);
  93. static av_cold int init(AVFilterContext *ctx, const char *args)
  94. {
  95. OverlayContext *over = ctx->priv;
  96. static const char *shorthand[] = { "x", "y", NULL };
  97. over->class = &overlay_class;
  98. av_opt_set_defaults(over);
  99. return av_opt_set_from_string(over, args, shorthand, "=", ":");
  100. }
  101. static av_cold void uninit(AVFilterContext *ctx)
  102. {
  103. OverlayContext *over = ctx->priv;
  104. av_opt_free(over);
  105. avfilter_unref_bufferp(&over->overpicref);
  106. ff_bufqueue_discard_all(&over->queue_main);
  107. ff_bufqueue_discard_all(&over->queue_over);
  108. }
  109. static int query_formats(AVFilterContext *ctx)
  110. {
  111. OverlayContext *over = ctx->priv;
  112. /* overlay formats contains alpha, for avoiding conversion with alpha information loss */
  113. static const enum AVPixelFormat main_pix_fmts_yuv[] = {
  114. AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUVA420P,
  115. AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUVA444P,
  116. AV_PIX_FMT_NONE
  117. };
  118. static const enum AVPixelFormat overlay_pix_fmts_yuv[] = {
  119. AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUVA444P,
  120. AV_PIX_FMT_NONE
  121. };
  122. static const enum AVPixelFormat main_pix_fmts_rgb[] = {
  123. AV_PIX_FMT_ARGB, AV_PIX_FMT_RGBA,
  124. AV_PIX_FMT_ABGR, AV_PIX_FMT_BGRA,
  125. AV_PIX_FMT_RGB24, AV_PIX_FMT_BGR24,
  126. AV_PIX_FMT_NONE
  127. };
  128. static const enum AVPixelFormat overlay_pix_fmts_rgb[] = {
  129. AV_PIX_FMT_ARGB, AV_PIX_FMT_RGBA,
  130. AV_PIX_FMT_ABGR, AV_PIX_FMT_BGRA,
  131. AV_PIX_FMT_NONE
  132. };
  133. AVFilterFormats *main_formats;
  134. AVFilterFormats *overlay_formats;
  135. if (over->allow_packed_rgb) {
  136. main_formats = ff_make_format_list(main_pix_fmts_rgb);
  137. overlay_formats = ff_make_format_list(overlay_pix_fmts_rgb);
  138. } else {
  139. main_formats = ff_make_format_list(main_pix_fmts_yuv);
  140. overlay_formats = ff_make_format_list(overlay_pix_fmts_yuv);
  141. }
  142. ff_formats_ref(main_formats, &ctx->inputs [MAIN ]->out_formats);
  143. ff_formats_ref(overlay_formats, &ctx->inputs [OVERLAY]->out_formats);
  144. ff_formats_ref(main_formats, &ctx->outputs[MAIN ]->in_formats );
  145. return 0;
  146. }
  147. static const enum AVPixelFormat alpha_pix_fmts[] = {
  148. AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUVA444P,
  149. AV_PIX_FMT_ARGB, AV_PIX_FMT_ABGR, AV_PIX_FMT_RGBA,
  150. AV_PIX_FMT_BGRA, AV_PIX_FMT_NONE
  151. };
  152. static int config_input_main(AVFilterLink *inlink)
  153. {
  154. OverlayContext *over = inlink->dst->priv;
  155. const AVPixFmtDescriptor *pix_desc = av_pix_fmt_desc_get(inlink->format);
  156. av_image_fill_max_pixsteps(over->main_pix_step, NULL, pix_desc);
  157. over->hsub = pix_desc->log2_chroma_w;
  158. over->vsub = pix_desc->log2_chroma_h;
  159. over->main_is_packed_rgb =
  160. ff_fill_rgba_map(over->main_rgba_map, inlink->format) >= 0;
  161. over->main_has_alpha = ff_fmt_is_in(inlink->format, alpha_pix_fmts);
  162. return 0;
  163. }
  164. static int config_input_overlay(AVFilterLink *inlink)
  165. {
  166. AVFilterContext *ctx = inlink->dst;
  167. OverlayContext *over = inlink->dst->priv;
  168. char *expr;
  169. double var_values[VAR_VARS_NB], res;
  170. int ret;
  171. const AVPixFmtDescriptor *pix_desc = av_pix_fmt_desc_get(inlink->format);
  172. av_image_fill_max_pixsteps(over->overlay_pix_step, NULL, pix_desc);
  173. /* Finish the configuration by evaluating the expressions
  174. now when both inputs are configured. */
  175. var_values[VAR_MAIN_W ] = var_values[VAR_MW] = ctx->inputs[MAIN ]->w;
  176. var_values[VAR_MAIN_H ] = var_values[VAR_MH] = ctx->inputs[MAIN ]->h;
  177. var_values[VAR_OVERLAY_W] = var_values[VAR_OW] = ctx->inputs[OVERLAY]->w;
  178. var_values[VAR_OVERLAY_H] = var_values[VAR_OH] = ctx->inputs[OVERLAY]->h;
  179. if ((ret = av_expr_parse_and_eval(&res, (expr = over->x_expr), var_names, var_values,
  180. NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
  181. goto fail;
  182. over->x = res;
  183. if ((ret = av_expr_parse_and_eval(&res, (expr = over->y_expr), var_names, var_values,
  184. NULL, NULL, NULL, NULL, NULL, 0, ctx)))
  185. goto fail;
  186. over->y = res;
  187. /* x may depend on y */
  188. if ((ret = av_expr_parse_and_eval(&res, (expr = over->x_expr), var_names, var_values,
  189. NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
  190. goto fail;
  191. over->x = res;
  192. over->overlay_is_packed_rgb =
  193. ff_fill_rgba_map(over->overlay_rgba_map, inlink->format) >= 0;
  194. over->overlay_has_alpha = ff_fmt_is_in(inlink->format, alpha_pix_fmts);
  195. av_log(ctx, AV_LOG_VERBOSE,
  196. "main w:%d h:%d fmt:%s overlay x:%d y:%d w:%d h:%d fmt:%s\n",
  197. ctx->inputs[MAIN]->w, ctx->inputs[MAIN]->h,
  198. av_get_pix_fmt_name(ctx->inputs[MAIN]->format),
  199. over->x, over->y,
  200. ctx->inputs[OVERLAY]->w, ctx->inputs[OVERLAY]->h,
  201. av_get_pix_fmt_name(ctx->inputs[OVERLAY]->format));
  202. if (over->x < 0 || over->y < 0 ||
  203. over->x + var_values[VAR_OVERLAY_W] > var_values[VAR_MAIN_W] ||
  204. over->y + var_values[VAR_OVERLAY_H] > var_values[VAR_MAIN_H]) {
  205. av_log(ctx, AV_LOG_ERROR,
  206. "Overlay area (%d,%d)<->(%d,%d) not within the main area (0,0)<->(%d,%d) or zero-sized\n",
  207. over->x, over->y,
  208. (int)(over->x + var_values[VAR_OVERLAY_W]),
  209. (int)(over->y + var_values[VAR_OVERLAY_H]),
  210. (int)var_values[VAR_MAIN_W], (int)var_values[VAR_MAIN_H]);
  211. return AVERROR(EINVAL);
  212. }
  213. return 0;
  214. fail:
  215. av_log(NULL, AV_LOG_ERROR,
  216. "Error when evaluating the expression '%s'\n", expr);
  217. return ret;
  218. }
  219. static int config_output(AVFilterLink *outlink)
  220. {
  221. AVFilterContext *ctx = outlink->src;
  222. outlink->w = ctx->inputs[MAIN]->w;
  223. outlink->h = ctx->inputs[MAIN]->h;
  224. outlink->time_base = ctx->inputs[MAIN]->time_base;
  225. return 0;
  226. }
  227. // divide by 255 and round to nearest
  228. // apply a fast variant: (X+127)/255 = ((X+127)*257+257)>>16 = ((X+128)*257)>>16
  229. #define FAST_DIV255(x) ((((x) + 128) * 257) >> 16)
  230. // calculate the unpremultiplied alpha, applying the general equation:
  231. // alpha = alpha_overlay / ( (alpha_main + alpha_overlay) - (alpha_main * alpha_overlay) )
  232. // (((x) << 16) - ((x) << 9) + (x)) is a faster version of: 255 * 255 * x
  233. // ((((x) + (y)) << 8) - ((x) + (y)) - (y) * (x)) is a faster version of: 255 * (x + y)
  234. #define UNPREMULTIPLY_ALPHA(x, y) ((((x) << 16) - ((x) << 9) + (x)) / ((((x) + (y)) << 8) - ((x) + (y)) - (y) * (x)))
  235. /**
  236. * Blend image in src to destination buffer dst at position (x, y).
  237. *
  238. * It is assumed that the src image at position (x, y) is contained in
  239. * dst.
  240. */
  241. static void blend_image(AVFilterContext *ctx,
  242. AVFilterBufferRef *dst, AVFilterBufferRef *src,
  243. int x, int y)
  244. {
  245. OverlayContext *over = ctx->priv;
  246. int i, j, k;
  247. int width = src->video->w;
  248. int height = src->video->h;
  249. if (over->main_is_packed_rgb) {
  250. uint8_t *dp = dst->data[0] + x * over->main_pix_step[0] +
  251. y * dst->linesize[0];
  252. uint8_t *sp = src->data[0];
  253. uint8_t alpha; ///< the amount of overlay to blend on to main
  254. const int dr = over->main_rgba_map[R];
  255. const int dg = over->main_rgba_map[G];
  256. const int db = over->main_rgba_map[B];
  257. const int da = over->main_rgba_map[A];
  258. const int dstep = over->main_pix_step[0];
  259. const int sr = over->overlay_rgba_map[R];
  260. const int sg = over->overlay_rgba_map[G];
  261. const int sb = over->overlay_rgba_map[B];
  262. const int sa = over->overlay_rgba_map[A];
  263. const int sstep = over->overlay_pix_step[0];
  264. const int main_has_alpha = over->main_has_alpha;
  265. for (i = 0; i < height; i++) {
  266. uint8_t *d = dp, *s = sp;
  267. for (j = 0; j < width; j++) {
  268. alpha = s[sa];
  269. // if the main channel has an alpha channel, alpha has to be calculated
  270. // to create an un-premultiplied (straight) alpha value
  271. if (main_has_alpha && alpha != 0 && alpha != 255) {
  272. uint8_t alpha_d = d[da];
  273. alpha = UNPREMULTIPLY_ALPHA(alpha, alpha_d);
  274. }
  275. switch (alpha) {
  276. case 0:
  277. break;
  278. case 255:
  279. d[dr] = s[sr];
  280. d[dg] = s[sg];
  281. d[db] = s[sb];
  282. break;
  283. default:
  284. // main_value = main_value * (1 - alpha) + overlay_value * alpha
  285. // since alpha is in the range 0-255, the result must divided by 255
  286. d[dr] = FAST_DIV255(d[dr] * (255 - alpha) + s[sr] * alpha);
  287. d[dg] = FAST_DIV255(d[dg] * (255 - alpha) + s[sg] * alpha);
  288. d[db] = FAST_DIV255(d[db] * (255 - alpha) + s[sb] * alpha);
  289. }
  290. if (main_has_alpha) {
  291. switch (alpha) {
  292. case 0:
  293. break;
  294. case 255:
  295. d[da] = s[sa];
  296. break;
  297. default:
  298. // apply alpha compositing: main_alpha += (1-main_alpha) * overlay_alpha
  299. d[da] += FAST_DIV255((255 - d[da]) * s[sa]);
  300. }
  301. }
  302. d += dstep;
  303. s += sstep;
  304. }
  305. dp += dst->linesize[0];
  306. sp += src->linesize[0];
  307. }
  308. } else {
  309. const int main_has_alpha = over->main_has_alpha;
  310. if (main_has_alpha) {
  311. uint8_t *da = dst->data[3] + x * over->main_pix_step[3] +
  312. y * dst->linesize[3];
  313. uint8_t *sa = src->data[3];
  314. uint8_t alpha; ///< the amount of overlay to blend on to main
  315. for (i = 0; i < height; i++) {
  316. uint8_t *d = da, *s = sa;
  317. for (j = 0; j < width; j++) {
  318. alpha = *s;
  319. if (alpha != 0 && alpha != 255) {
  320. uint8_t alpha_d = *d;
  321. alpha = UNPREMULTIPLY_ALPHA(alpha, alpha_d);
  322. }
  323. switch (alpha) {
  324. case 0:
  325. break;
  326. case 255:
  327. *d = *s;
  328. break;
  329. default:
  330. // apply alpha compositing: main_alpha += (1-main_alpha) * overlay_alpha
  331. *d += FAST_DIV255((255 - *d) * *s);
  332. }
  333. d += 1;
  334. s += 1;
  335. }
  336. da += dst->linesize[3];
  337. sa += src->linesize[3];
  338. }
  339. }
  340. for (i = 0; i < 3; i++) {
  341. int hsub = i ? over->hsub : 0;
  342. int vsub = i ? over->vsub : 0;
  343. uint8_t *dp = dst->data[i] + (x >> hsub) +
  344. (y >> vsub) * dst->linesize[i];
  345. uint8_t *sp = src->data[i];
  346. uint8_t *ap = src->data[3];
  347. int wp = FFALIGN(width, 1<<hsub) >> hsub;
  348. int hp = FFALIGN(height, 1<<vsub) >> vsub;
  349. for (j = 0; j < hp; j++) {
  350. uint8_t *d = dp, *s = sp, *a = ap;
  351. for (k = 0; k < wp; k++) {
  352. // average alpha for color components, improve quality
  353. int alpha_v, alpha_h, alpha;
  354. if (hsub && vsub && j+1 < hp && k+1 < wp) {
  355. alpha = (a[0] + a[src->linesize[3]] +
  356. a[1] + a[src->linesize[3]+1]) >> 2;
  357. } else if (hsub || vsub) {
  358. alpha_h = hsub && k+1 < wp ?
  359. (a[0] + a[1]) >> 1 : a[0];
  360. alpha_v = vsub && j+1 < hp ?
  361. (a[0] + a[src->linesize[3]]) >> 1 : a[0];
  362. alpha = (alpha_v + alpha_h) >> 1;
  363. } else
  364. alpha = a[0];
  365. // if the main channel has an alpha channel, alpha has to be calculated
  366. // to create an un-premultiplied (straight) alpha value
  367. if (main_has_alpha && alpha != 0 && alpha != 255) {
  368. // average alpha for color components, improve quality
  369. uint8_t alpha_d;
  370. if (hsub && vsub && j+1 < hp && k+1 < wp) {
  371. alpha_d = (d[0] + d[src->linesize[3]] +
  372. d[1] + d[src->linesize[3]+1]) >> 2;
  373. } else if (hsub || vsub) {
  374. alpha_h = hsub && k+1 < wp ?
  375. (d[0] + d[1]) >> 1 : d[0];
  376. alpha_v = vsub && j+1 < hp ?
  377. (d[0] + d[src->linesize[3]]) >> 1 : d[0];
  378. alpha_d = (alpha_v + alpha_h) >> 1;
  379. } else
  380. alpha_d = d[0];
  381. alpha = UNPREMULTIPLY_ALPHA(alpha, alpha_d);
  382. }
  383. *d = FAST_DIV255(*d * (255 - alpha) + *s * alpha);
  384. s++;
  385. d++;
  386. a += 1 << hsub;
  387. }
  388. dp += dst->linesize[i];
  389. sp += src->linesize[i];
  390. ap += (1 << vsub) * src->linesize[3];
  391. }
  392. }
  393. }
  394. }
  395. static int try_filter_frame(AVFilterContext *ctx, AVFilterBufferRef *mainpic)
  396. {
  397. OverlayContext *over = ctx->priv;
  398. AVFilterLink *outlink = ctx->outputs[0];
  399. AVFilterBufferRef *next_overpic;
  400. int ret;
  401. /* Discard obsolete overlay frames: if there is a next overlay frame with pts
  402. * before the main frame, we can drop the current overlay. */
  403. while (1) {
  404. next_overpic = ff_bufqueue_peek(&over->queue_over, 0);
  405. if (!next_overpic || av_compare_ts(next_overpic->pts, ctx->inputs[OVERLAY]->time_base,
  406. mainpic->pts , ctx->inputs[MAIN]->time_base) > 0)
  407. break;
  408. ff_bufqueue_get(&over->queue_over);
  409. avfilter_unref_buffer(over->overpicref);
  410. over->overpicref = next_overpic;
  411. }
  412. /* If there is no next frame and no EOF and the overlay frame is before
  413. * the main frame, we can not know yet if it will be superseded. */
  414. if (!over->queue_over.available && !over->overlay_eof &&
  415. (!over->overpicref || av_compare_ts(over->overpicref->pts, ctx->inputs[OVERLAY]->time_base,
  416. mainpic->pts , ctx->inputs[MAIN]->time_base) < 0))
  417. return AVERROR(EAGAIN);
  418. /* At this point, we know that the current overlay frame extends to the
  419. * time of the main frame. */
  420. av_dlog(ctx, "main_pts:%s main_pts_time:%s",
  421. av_ts2str(mainpic->pts), av_ts2timestr(mainpic->pts, &outlink->time_base));
  422. if (over->overpicref)
  423. av_dlog(ctx, " over_pts:%s over_pts_time:%s",
  424. av_ts2str(over->overpicref->pts), av_ts2timestr(over->overpicref->pts, &outlink->time_base));
  425. av_dlog(ctx, "\n");
  426. if (over->overpicref)
  427. blend_image(ctx, mainpic, over->overpicref, over->x, over->y);
  428. ret = ff_filter_frame(ctx->outputs[0], mainpic);
  429. av_assert1(ret != AVERROR(EAGAIN));
  430. over->frame_requested = 0;
  431. return ret;
  432. }
  433. static int try_filter_next_frame(AVFilterContext *ctx)
  434. {
  435. OverlayContext *over = ctx->priv;
  436. AVFilterBufferRef *next_mainpic = ff_bufqueue_peek(&over->queue_main, 0);
  437. int ret;
  438. if (!next_mainpic)
  439. return AVERROR(EAGAIN);
  440. if ((ret = try_filter_frame(ctx, next_mainpic)) == AVERROR(EAGAIN))
  441. return ret;
  442. ff_bufqueue_get(&over->queue_main);
  443. return ret;
  444. }
  445. static int flush_frames(AVFilterContext *ctx)
  446. {
  447. int ret;
  448. while (!(ret = try_filter_next_frame(ctx)));
  449. return ret == AVERROR(EAGAIN) ? 0 : ret;
  450. }
  451. static int filter_frame_main(AVFilterLink *inlink, AVFilterBufferRef *inpicref)
  452. {
  453. AVFilterContext *ctx = inlink->dst;
  454. OverlayContext *over = ctx->priv;
  455. int ret;
  456. if ((ret = flush_frames(ctx)) < 0)
  457. return ret;
  458. if ((ret = try_filter_frame(ctx, inpicref)) < 0) {
  459. if (ret != AVERROR(EAGAIN))
  460. return ret;
  461. ff_bufqueue_add(ctx, &over->queue_main, inpicref);
  462. }
  463. if (!over->overpicref)
  464. return 0;
  465. flush_frames(ctx);
  466. return 0;
  467. }
  468. static int filter_frame_over(AVFilterLink *inlink, AVFilterBufferRef *inpicref)
  469. {
  470. AVFilterContext *ctx = inlink->dst;
  471. OverlayContext *over = ctx->priv;
  472. int ret;
  473. if ((ret = flush_frames(ctx)) < 0)
  474. return ret;
  475. ff_bufqueue_add(ctx, &over->queue_over, inpicref);
  476. ret = try_filter_next_frame(ctx);
  477. return ret == AVERROR(EAGAIN) ? 0 : ret;
  478. }
  479. static int request_frame(AVFilterLink *outlink)
  480. {
  481. AVFilterContext *ctx = outlink->src;
  482. OverlayContext *over = ctx->priv;
  483. int input, ret;
  484. if (!try_filter_next_frame(ctx))
  485. return 0;
  486. over->frame_requested = 1;
  487. while (over->frame_requested) {
  488. /* TODO if we had a frame duration, we could guess more accurately */
  489. input = !over->overlay_eof && (over->queue_main.available ||
  490. over->queue_over.available < 2) ?
  491. OVERLAY : MAIN;
  492. ret = ff_request_frame(ctx->inputs[input]);
  493. /* EOF on main is reported immediately */
  494. if (ret == AVERROR_EOF && input == OVERLAY) {
  495. over->overlay_eof = 1;
  496. if ((ret = try_filter_next_frame(ctx)) != AVERROR(EAGAIN))
  497. return ret;
  498. ret = 0; /* continue requesting frames on main */
  499. }
  500. if (ret < 0)
  501. return ret;
  502. }
  503. return 0;
  504. }
  505. static const AVFilterPad avfilter_vf_overlay_inputs[] = {
  506. {
  507. .name = "main",
  508. .type = AVMEDIA_TYPE_VIDEO,
  509. .get_video_buffer = ff_null_get_video_buffer,
  510. .config_props = config_input_main,
  511. .filter_frame = filter_frame_main,
  512. .min_perms = AV_PERM_READ | AV_PERM_WRITE | AV_PERM_PRESERVE,
  513. },
  514. {
  515. .name = "overlay",
  516. .type = AVMEDIA_TYPE_VIDEO,
  517. .config_props = config_input_overlay,
  518. .filter_frame = filter_frame_over,
  519. .min_perms = AV_PERM_READ | AV_PERM_PRESERVE,
  520. },
  521. { NULL }
  522. };
  523. static const AVFilterPad avfilter_vf_overlay_outputs[] = {
  524. {
  525. .name = "default",
  526. .type = AVMEDIA_TYPE_VIDEO,
  527. .rej_perms = AV_PERM_WRITE,
  528. .config_props = config_output,
  529. .request_frame = request_frame,
  530. },
  531. { NULL }
  532. };
  533. AVFilter avfilter_vf_overlay = {
  534. .name = "overlay",
  535. .description = NULL_IF_CONFIG_SMALL("Overlay a video source on top of the input."),
  536. .init = init,
  537. .uninit = uninit,
  538. .priv_size = sizeof(OverlayContext),
  539. .query_formats = query_formats,
  540. .inputs = avfilter_vf_overlay_inputs,
  541. .outputs = avfilter_vf_overlay_outputs,
  542. .priv_class = &overlay_class,
  543. };