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.

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