PageRenderTime 56ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/ffmpeg/libavfilter/vf_overlay.c

http://github.com/xbmc/xbmc
C | 679 lines | 532 code | 90 blank | 57 comment | 89 complexity | 801e587b5498618901a3f69e628d5992 MD5 | raw file
Possible License(s): GPL-3.0, CC-BY-SA-3.0, LGPL-2.0, 0BSD, Unlicense, GPL-2.0, AGPL-1.0, BSD-3-Clause, LGPL-2.1, LGPL-3.0
  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_WARNING,
  234. "Overlay area with coordinates x1:%d y1:%d x2:%d y2:%d "
  235. "is not completely contained within the output with size %dx%d\n",
  236. over->x, over->y,
  237. (int)(over->x + var_values[VAR_OVERLAY_W]),
  238. (int)(over->y + var_values[VAR_OVERLAY_H]),
  239. (int)var_values[VAR_MAIN_W], (int)var_values[VAR_MAIN_H]);
  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. static void blend_image(AVFilterContext *ctx,
  267. AVFilterBufferRef *dst, AVFilterBufferRef *src,
  268. int x, int y)
  269. {
  270. OverlayContext *over = ctx->priv;
  271. int i, imax, j, jmax, k, kmax;
  272. const int src_w = src->video->w;
  273. const int src_h = src->video->h;
  274. const int dst_w = dst->video->w;
  275. const int dst_h = dst->video->h;
  276. if (x >= dst_w || x+dst_w < 0 ||
  277. y >= dst_h || y+dst_h < 0)
  278. return; /* no intersection */
  279. if (over->main_is_packed_rgb) {
  280. uint8_t alpha; ///< the amount of overlay to blend on to main
  281. const int dr = over->main_rgba_map[R];
  282. const int dg = over->main_rgba_map[G];
  283. const int db = over->main_rgba_map[B];
  284. const int da = over->main_rgba_map[A];
  285. const int dstep = over->main_pix_step[0];
  286. const int sr = over->overlay_rgba_map[R];
  287. const int sg = over->overlay_rgba_map[G];
  288. const int sb = over->overlay_rgba_map[B];
  289. const int sa = over->overlay_rgba_map[A];
  290. const int sstep = over->overlay_pix_step[0];
  291. const int main_has_alpha = over->main_has_alpha;
  292. uint8_t *s, *sp, *d, *dp;
  293. i = FFMAX(-y, 0);
  294. sp = src->data[0] + i * src->linesize[0];
  295. dp = dst->data[0] + (y+i) * dst->linesize[0];
  296. for (imax = FFMIN(-y + dst_h, src_h); i < imax; i++) {
  297. j = FFMAX(-x, 0);
  298. s = sp + j * sstep;
  299. d = dp + (x+j) * dstep;
  300. for (jmax = FFMIN(-x + dst_w, src_w); j < jmax; j++) {
  301. alpha = s[sa];
  302. // if the main channel has an alpha channel, alpha has to be calculated
  303. // to create an un-premultiplied (straight) alpha value
  304. if (main_has_alpha && alpha != 0 && alpha != 255) {
  305. uint8_t alpha_d = d[da];
  306. alpha = UNPREMULTIPLY_ALPHA(alpha, alpha_d);
  307. }
  308. switch (alpha) {
  309. case 0:
  310. break;
  311. case 255:
  312. d[dr] = s[sr];
  313. d[dg] = s[sg];
  314. d[db] = s[sb];
  315. break;
  316. default:
  317. // main_value = main_value * (1 - alpha) + overlay_value * alpha
  318. // since alpha is in the range 0-255, the result must divided by 255
  319. d[dr] = FAST_DIV255(d[dr] * (255 - alpha) + s[sr] * alpha);
  320. d[dg] = FAST_DIV255(d[dg] * (255 - alpha) + s[sg] * alpha);
  321. d[db] = FAST_DIV255(d[db] * (255 - alpha) + s[sb] * alpha);
  322. }
  323. if (main_has_alpha) {
  324. switch (alpha) {
  325. case 0:
  326. break;
  327. case 255:
  328. d[da] = s[sa];
  329. break;
  330. default:
  331. // apply alpha compositing: main_alpha += (1-main_alpha) * overlay_alpha
  332. d[da] += FAST_DIV255((255 - d[da]) * s[sa]);
  333. }
  334. }
  335. d += dstep;
  336. s += sstep;
  337. }
  338. dp += dst->linesize[0];
  339. sp += src->linesize[0];
  340. }
  341. } else {
  342. const int main_has_alpha = over->main_has_alpha;
  343. if (main_has_alpha) {
  344. uint8_t alpha; ///< the amount of overlay to blend on to main
  345. uint8_t *s, *sa, *d, *da;
  346. i = FFMAX(-y, 0);
  347. sa = src->data[3] + i * src->linesize[3];
  348. da = dst->data[3] + (y+i) * dst->linesize[3];
  349. for (imax = FFMIN(-y + dst_h, src_h); i < imax; i++) {
  350. j = FFMAX(-x, 0);
  351. s = sa + j;
  352. d = da + x+j;
  353. for (jmax = FFMIN(-x + dst_w, src_w); j < jmax; j++) {
  354. alpha = *s;
  355. if (alpha != 0 && alpha != 255) {
  356. uint8_t alpha_d = *d;
  357. alpha = UNPREMULTIPLY_ALPHA(alpha, alpha_d);
  358. }
  359. switch (alpha) {
  360. case 0:
  361. break;
  362. case 255:
  363. *d = *s;
  364. break;
  365. default:
  366. // apply alpha compositing: main_alpha += (1-main_alpha) * overlay_alpha
  367. *d += FAST_DIV255((255 - *d) * *s);
  368. }
  369. d += 1;
  370. s += 1;
  371. }
  372. da += dst->linesize[3];
  373. sa += src->linesize[3];
  374. }
  375. }
  376. for (i = 0; i < 3; i++) {
  377. int hsub = i ? over->hsub : 0;
  378. int vsub = i ? over->vsub : 0;
  379. int src_wp = FFALIGN(src_w, 1<<hsub) >> hsub;
  380. int src_hp = FFALIGN(src_h, 1<<vsub) >> vsub;
  381. int dst_wp = FFALIGN(dst_w, 1<<hsub) >> hsub;
  382. int dst_hp = FFALIGN(dst_h, 1<<vsub) >> vsub;
  383. int yp = y>>vsub;
  384. int xp = x>>hsub;
  385. uint8_t *s, *sp, *d, *dp, *a, *ap;
  386. j = FFMAX(-yp, 0);
  387. sp = src->data[i] + j * src->linesize[i];
  388. dp = dst->data[i] + (yp+j) * dst->linesize[i];
  389. ap = src->data[3] + (j<<vsub) * src->linesize[3];
  390. for (jmax = FFMIN(-yp + dst_hp, src_hp); j < jmax; j++) {
  391. k = FFMAX(-xp, 0);
  392. d = dp + xp+k;
  393. s = sp + k;
  394. a = ap + (k<<hsub);
  395. for (kmax = FFMIN(-xp + dst_wp, src_wp); k < kmax; k++) {
  396. int alpha_v, alpha_h, alpha;
  397. // average alpha for color components, improve quality
  398. if (hsub && vsub && j+1 < src_hp && k+1 < src_wp) {
  399. alpha = (a[0] + a[src->linesize[3]] +
  400. a[1] + a[src->linesize[3]+1]) >> 2;
  401. } else if (hsub || vsub) {
  402. alpha_h = hsub && k+1 < src_wp ?
  403. (a[0] + a[1]) >> 1 : a[0];
  404. alpha_v = vsub && j+1 < src_hp ?
  405. (a[0] + a[src->linesize[3]]) >> 1 : a[0];
  406. alpha = (alpha_v + alpha_h) >> 1;
  407. } else
  408. alpha = a[0];
  409. // if the main channel has an alpha channel, alpha has to be calculated
  410. // to create an un-premultiplied (straight) alpha value
  411. if (main_has_alpha && alpha != 0 && alpha != 255) {
  412. // average alpha for color components, improve quality
  413. uint8_t alpha_d;
  414. if (hsub && vsub && j+1 < src_hp && k+1 < src_wp) {
  415. alpha_d = (d[0] + d[src->linesize[3]] +
  416. d[1] + d[src->linesize[3]+1]) >> 2;
  417. } else if (hsub || vsub) {
  418. alpha_h = hsub && k+1 < src_wp ?
  419. (d[0] + d[1]) >> 1 : d[0];
  420. alpha_v = vsub && j+1 < src_hp ?
  421. (d[0] + d[src->linesize[3]]) >> 1 : d[0];
  422. alpha_d = (alpha_v + alpha_h) >> 1;
  423. } else
  424. alpha_d = d[0];
  425. alpha = UNPREMULTIPLY_ALPHA(alpha, alpha_d);
  426. }
  427. *d = FAST_DIV255(*d * (255 - alpha) + *s * alpha);
  428. s++;
  429. d++;
  430. a += 1 << hsub;
  431. }
  432. dp += dst->linesize[i];
  433. sp += src->linesize[i];
  434. ap += (1 << vsub) * src->linesize[3];
  435. }
  436. }
  437. }
  438. }
  439. static int try_filter_frame(AVFilterContext *ctx, AVFilterBufferRef *mainpic)
  440. {
  441. OverlayContext *over = ctx->priv;
  442. AVFilterLink *outlink = ctx->outputs[0];
  443. AVFilterBufferRef *next_overpic;
  444. int ret;
  445. /* Discard obsolete overlay frames: if there is a next overlay frame with pts
  446. * before the main frame, we can drop the current overlay. */
  447. while (1) {
  448. next_overpic = ff_bufqueue_peek(&over->queue_over, 0);
  449. if (!next_overpic || av_compare_ts(next_overpic->pts, ctx->inputs[OVERLAY]->time_base,
  450. mainpic->pts , ctx->inputs[MAIN]->time_base) > 0)
  451. break;
  452. ff_bufqueue_get(&over->queue_over);
  453. avfilter_unref_buffer(over->overpicref);
  454. over->overpicref = next_overpic;
  455. }
  456. /* If there is no next frame and no EOF and the overlay frame is before
  457. * the main frame, we can not know yet if it will be superseded. */
  458. if (!over->queue_over.available && !over->overlay_eof &&
  459. (!over->overpicref || av_compare_ts(over->overpicref->pts, ctx->inputs[OVERLAY]->time_base,
  460. mainpic->pts , ctx->inputs[MAIN]->time_base) < 0))
  461. return AVERROR(EAGAIN);
  462. /* At this point, we know that the current overlay frame extends to the
  463. * time of the main frame. */
  464. av_dlog(ctx, "main_pts:%s main_pts_time:%s",
  465. av_ts2str(mainpic->pts), av_ts2timestr(mainpic->pts, &outlink->time_base));
  466. if (over->overpicref)
  467. av_dlog(ctx, " over_pts:%s over_pts_time:%s",
  468. av_ts2str(over->overpicref->pts), av_ts2timestr(over->overpicref->pts, &outlink->time_base));
  469. av_dlog(ctx, "\n");
  470. if (over->overpicref)
  471. blend_image(ctx, mainpic, over->overpicref, over->x, over->y);
  472. ret = ff_filter_frame(ctx->outputs[0], mainpic);
  473. av_assert1(ret != AVERROR(EAGAIN));
  474. over->frame_requested = 0;
  475. return ret;
  476. }
  477. static int try_filter_next_frame(AVFilterContext *ctx)
  478. {
  479. OverlayContext *over = ctx->priv;
  480. AVFilterBufferRef *next_mainpic = ff_bufqueue_peek(&over->queue_main, 0);
  481. int ret;
  482. if (!next_mainpic)
  483. return AVERROR(EAGAIN);
  484. if ((ret = try_filter_frame(ctx, next_mainpic)) == AVERROR(EAGAIN))
  485. return ret;
  486. ff_bufqueue_get(&over->queue_main);
  487. return ret;
  488. }
  489. static int flush_frames(AVFilterContext *ctx)
  490. {
  491. int ret;
  492. while (!(ret = try_filter_next_frame(ctx)));
  493. return ret == AVERROR(EAGAIN) ? 0 : ret;
  494. }
  495. static int filter_frame_main(AVFilterLink *inlink, AVFilterBufferRef *inpicref)
  496. {
  497. AVFilterContext *ctx = inlink->dst;
  498. OverlayContext *over = ctx->priv;
  499. int ret;
  500. if ((ret = flush_frames(ctx)) < 0)
  501. return ret;
  502. if ((ret = try_filter_frame(ctx, inpicref)) < 0) {
  503. if (ret != AVERROR(EAGAIN))
  504. return ret;
  505. ff_bufqueue_add(ctx, &over->queue_main, inpicref);
  506. }
  507. if (!over->overpicref)
  508. return 0;
  509. flush_frames(ctx);
  510. return 0;
  511. }
  512. static int filter_frame_over(AVFilterLink *inlink, AVFilterBufferRef *inpicref)
  513. {
  514. AVFilterContext *ctx = inlink->dst;
  515. OverlayContext *over = ctx->priv;
  516. int ret;
  517. if ((ret = flush_frames(ctx)) < 0)
  518. return ret;
  519. ff_bufqueue_add(ctx, &over->queue_over, inpicref);
  520. ret = try_filter_next_frame(ctx);
  521. return ret == AVERROR(EAGAIN) ? 0 : ret;
  522. }
  523. static int request_frame(AVFilterLink *outlink)
  524. {
  525. AVFilterContext *ctx = outlink->src;
  526. OverlayContext *over = ctx->priv;
  527. int input, ret;
  528. if (!try_filter_next_frame(ctx))
  529. return 0;
  530. over->frame_requested = 1;
  531. while (over->frame_requested) {
  532. /* TODO if we had a frame duration, we could guess more accurately */
  533. input = !over->overlay_eof && (over->queue_main.available ||
  534. over->queue_over.available < 2) ?
  535. OVERLAY : MAIN;
  536. ret = ff_request_frame(ctx->inputs[input]);
  537. /* EOF on main is reported immediately */
  538. if (ret == AVERROR_EOF && input == OVERLAY) {
  539. over->overlay_eof = 1;
  540. if (over->shortest)
  541. return ret;
  542. if ((ret = try_filter_next_frame(ctx)) != AVERROR(EAGAIN))
  543. return ret;
  544. ret = 0; /* continue requesting frames on main */
  545. }
  546. if (ret < 0)
  547. return ret;
  548. }
  549. return 0;
  550. }
  551. static const AVFilterPad avfilter_vf_overlay_inputs[] = {
  552. {
  553. .name = "main",
  554. .type = AVMEDIA_TYPE_VIDEO,
  555. .get_video_buffer = ff_null_get_video_buffer,
  556. .config_props = config_input_main,
  557. .filter_frame = filter_frame_main,
  558. .min_perms = AV_PERM_READ | AV_PERM_WRITE | AV_PERM_PRESERVE,
  559. },
  560. {
  561. .name = "overlay",
  562. .type = AVMEDIA_TYPE_VIDEO,
  563. .config_props = config_input_overlay,
  564. .filter_frame = filter_frame_over,
  565. .min_perms = AV_PERM_READ | AV_PERM_PRESERVE,
  566. },
  567. { NULL }
  568. };
  569. static const AVFilterPad avfilter_vf_overlay_outputs[] = {
  570. {
  571. .name = "default",
  572. .type = AVMEDIA_TYPE_VIDEO,
  573. .rej_perms = AV_PERM_WRITE,
  574. .config_props = config_output,
  575. .request_frame = request_frame,
  576. },
  577. { NULL }
  578. };
  579. AVFilter avfilter_vf_overlay = {
  580. .name = "overlay",
  581. .description = NULL_IF_CONFIG_SMALL("Overlay a video source on top of the input."),
  582. .init = init,
  583. .uninit = uninit,
  584. .priv_size = sizeof(OverlayContext),
  585. .query_formats = query_formats,
  586. .inputs = avfilter_vf_overlay_inputs,
  587. .outputs = avfilter_vf_overlay_outputs,
  588. .priv_class = &overlay_class,
  589. };