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.

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