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.

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