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.

669 lines
24KB

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