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.

659 lines
22KB

  1. /*
  2. * Copyright (c) 2010 Stefano Sabatini
  3. * Copyright (c) 2010 Baptiste Coudurier
  4. * Copyright (c) 2007 Bobby Bingham
  5. *
  6. * This file is part of FFmpeg.
  7. *
  8. * FFmpeg is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * FFmpeg is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with FFmpeg; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. /**
  23. * @file
  24. * overlay one video on top of another
  25. */
  26. /* #define DEBUG */
  27. #include "avfilter.h"
  28. #include "formats.h"
  29. #include "libavutil/common.h"
  30. #include "libavutil/eval.h"
  31. #include "libavutil/avstring.h"
  32. #include "libavutil/opt.h"
  33. #include "libavutil/pixdesc.h"
  34. #include "libavutil/imgutils.h"
  35. #include "libavutil/mathematics.h"
  36. #include "libavutil/timestamp.h"
  37. #include "internal.h"
  38. #include "bufferqueue.h"
  39. #include "drawutils.h"
  40. #include "video.h"
  41. static const char *const var_names[] = {
  42. "main_w", "W", ///< width of the main video
  43. "main_h", "H", ///< height of the main video
  44. "overlay_w", "w", ///< width of the overlay video
  45. "overlay_h", "h", ///< height of the overlay video
  46. NULL
  47. };
  48. enum var_name {
  49. VAR_MAIN_W, VAR_MW,
  50. VAR_MAIN_H, VAR_MH,
  51. VAR_OVERLAY_W, VAR_OW,
  52. VAR_OVERLAY_H, VAR_OH,
  53. VAR_VARS_NB
  54. };
  55. #define MAIN 0
  56. #define OVERLAY 1
  57. #define R 0
  58. #define G 1
  59. #define B 2
  60. #define A 3
  61. #define Y 0
  62. #define U 1
  63. #define V 2
  64. typedef struct {
  65. const AVClass *class;
  66. int x, y; ///< position of overlayed picture
  67. int allow_packed_rgb;
  68. uint8_t frame_requested;
  69. uint8_t overlay_eof;
  70. uint8_t main_is_packed_rgb;
  71. uint8_t main_rgba_map[4];
  72. uint8_t main_has_alpha;
  73. uint8_t overlay_is_packed_rgb;
  74. uint8_t overlay_rgba_map[4];
  75. uint8_t overlay_has_alpha;
  76. AVFilterBufferRef *overpicref;
  77. struct FFBufQueue queue_main;
  78. struct FFBufQueue queue_over;
  79. int main_pix_step[4]; ///< steps per pixel for each plane of the main output
  80. int overlay_pix_step[4]; ///< steps per pixel for each plane of the overlay
  81. int hsub, vsub; ///< chroma subsampling values
  82. char *x_expr, *y_expr;
  83. } OverlayContext;
  84. #define OFFSET(x) offsetof(OverlayContext, x)
  85. #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
  86. static const AVOption overlay_options[] = {
  87. { "x", "set the x expression", OFFSET(x_expr), AV_OPT_TYPE_STRING, {.str = "0"}, CHAR_MIN, CHAR_MAX, FLAGS },
  88. { "y", "set the y expression", OFFSET(y_expr), AV_OPT_TYPE_STRING, {.str = "0"}, CHAR_MIN, CHAR_MAX, FLAGS },
  89. {"rgb", "force packed RGB in input and output", OFFSET(allow_packed_rgb), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, FLAGS },
  90. {NULL},
  91. };
  92. AVFILTER_DEFINE_CLASS(overlay);
  93. static av_cold int init(AVFilterContext *ctx, const char *args)
  94. {
  95. OverlayContext *over = ctx->priv;
  96. char *args1 = av_strdup(args);
  97. char *expr, *bufptr = NULL;
  98. int ret = 0;
  99. over->class = &overlay_class;
  100. av_opt_set_defaults(over);
  101. if (expr = av_strtok(args1, ":", &bufptr)) {
  102. av_free(over->x_expr);
  103. if (!(over->x_expr = av_strdup(expr))) {
  104. ret = AVERROR(ENOMEM);
  105. goto end;
  106. }
  107. }
  108. if (expr = av_strtok(NULL, ":", &bufptr)) {
  109. av_free(over->y_expr);
  110. if (!(over->y_expr = av_strdup(expr))) {
  111. ret = AVERROR(ENOMEM);
  112. goto end;
  113. }
  114. }
  115. if (bufptr && (ret = av_set_options_string(over, bufptr, "=", ":")) < 0)
  116. goto end;
  117. end:
  118. av_free(args1);
  119. return ret;
  120. }
  121. static av_cold void uninit(AVFilterContext *ctx)
  122. {
  123. OverlayContext *over = ctx->priv;
  124. av_freep(&over->x_expr);
  125. av_freep(&over->y_expr);
  126. avfilter_unref_bufferp(&over->overpicref);
  127. ff_bufqueue_discard_all(&over->queue_main);
  128. ff_bufqueue_discard_all(&over->queue_over);
  129. }
  130. static int query_formats(AVFilterContext *ctx)
  131. {
  132. OverlayContext *over = ctx->priv;
  133. /* overlay formats contains alpha, for avoiding conversion with alpha information loss */
  134. const enum AVPixelFormat main_pix_fmts_yuv[] = { AV_PIX_FMT_YUV420P, AV_PIX_FMT_NONE };
  135. const enum AVPixelFormat overlay_pix_fmts_yuv[] = { AV_PIX_FMT_YUVA420P, AV_PIX_FMT_NONE };
  136. const enum AVPixelFormat main_pix_fmts_rgb[] = {
  137. AV_PIX_FMT_ARGB, AV_PIX_FMT_RGBA,
  138. AV_PIX_FMT_ABGR, AV_PIX_FMT_BGRA,
  139. AV_PIX_FMT_RGB24, AV_PIX_FMT_BGR24,
  140. AV_PIX_FMT_NONE
  141. };
  142. const enum AVPixelFormat overlay_pix_fmts_rgb[] = {
  143. AV_PIX_FMT_ARGB, AV_PIX_FMT_RGBA,
  144. AV_PIX_FMT_ABGR, AV_PIX_FMT_BGRA,
  145. AV_PIX_FMT_NONE
  146. };
  147. AVFilterFormats *main_formats;
  148. AVFilterFormats *overlay_formats;
  149. if (over->allow_packed_rgb) {
  150. main_formats = ff_make_format_list(main_pix_fmts_rgb);
  151. overlay_formats = ff_make_format_list(overlay_pix_fmts_rgb);
  152. } else {
  153. main_formats = ff_make_format_list(main_pix_fmts_yuv);
  154. overlay_formats = ff_make_format_list(overlay_pix_fmts_yuv);
  155. }
  156. ff_formats_ref(main_formats, &ctx->inputs [MAIN ]->out_formats);
  157. ff_formats_ref(overlay_formats, &ctx->inputs [OVERLAY]->out_formats);
  158. ff_formats_ref(main_formats, &ctx->outputs[MAIN ]->in_formats );
  159. return 0;
  160. }
  161. static const enum AVPixelFormat alpha_pix_fmts[] = {
  162. AV_PIX_FMT_YUVA420P, AV_PIX_FMT_ARGB, AV_PIX_FMT_ABGR, AV_PIX_FMT_RGBA,
  163. AV_PIX_FMT_BGRA, AV_PIX_FMT_NONE
  164. };
  165. static int config_input_main(AVFilterLink *inlink)
  166. {
  167. OverlayContext *over = inlink->dst->priv;
  168. const AVPixFmtDescriptor *pix_desc = av_pix_fmt_desc_get(inlink->format);
  169. av_image_fill_max_pixsteps(over->main_pix_step, NULL, pix_desc);
  170. over->hsub = pix_desc->log2_chroma_w;
  171. over->vsub = pix_desc->log2_chroma_h;
  172. over->main_is_packed_rgb =
  173. ff_fill_rgba_map(over->main_rgba_map, inlink->format) >= 0;
  174. over->main_has_alpha = ff_fmt_is_in(inlink->format, alpha_pix_fmts);
  175. return 0;
  176. }
  177. static int config_input_overlay(AVFilterLink *inlink)
  178. {
  179. AVFilterContext *ctx = inlink->dst;
  180. OverlayContext *over = inlink->dst->priv;
  181. char *expr;
  182. double var_values[VAR_VARS_NB], res;
  183. int ret;
  184. const AVPixFmtDescriptor *pix_desc = &av_pix_fmt_descriptors[inlink->format];
  185. av_image_fill_max_pixsteps(over->overlay_pix_step, NULL, pix_desc);
  186. /* Finish the configuration by evaluating the expressions
  187. now when both inputs are configured. */
  188. var_values[VAR_MAIN_W ] = var_values[VAR_MW] = ctx->inputs[MAIN ]->w;
  189. var_values[VAR_MAIN_H ] = var_values[VAR_MH] = ctx->inputs[MAIN ]->h;
  190. var_values[VAR_OVERLAY_W] = var_values[VAR_OW] = ctx->inputs[OVERLAY]->w;
  191. var_values[VAR_OVERLAY_H] = var_values[VAR_OH] = ctx->inputs[OVERLAY]->h;
  192. if ((ret = av_expr_parse_and_eval(&res, (expr = over->x_expr), var_names, var_values,
  193. NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
  194. goto fail;
  195. over->x = res;
  196. if ((ret = av_expr_parse_and_eval(&res, (expr = over->y_expr), var_names, var_values,
  197. NULL, NULL, NULL, NULL, NULL, 0, ctx)))
  198. goto fail;
  199. over->y = res;
  200. /* x may depend on y */
  201. if ((ret = av_expr_parse_and_eval(&res, (expr = over->x_expr), var_names, var_values,
  202. NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
  203. goto fail;
  204. over->x = res;
  205. over->overlay_is_packed_rgb =
  206. ff_fill_rgba_map(over->overlay_rgba_map, inlink->format) >= 0;
  207. over->overlay_has_alpha = ff_fmt_is_in(inlink->format, alpha_pix_fmts);
  208. av_log(ctx, AV_LOG_VERBOSE,
  209. "main w:%d h:%d fmt:%s overlay x:%d y:%d w:%d h:%d fmt:%s\n",
  210. ctx->inputs[MAIN]->w, ctx->inputs[MAIN]->h,
  211. av_get_pix_fmt_name(ctx->inputs[MAIN]->format),
  212. over->x, over->y,
  213. ctx->inputs[OVERLAY]->w, ctx->inputs[OVERLAY]->h,
  214. av_get_pix_fmt_name(ctx->inputs[OVERLAY]->format));
  215. if (over->x < 0 || over->y < 0 ||
  216. over->x + var_values[VAR_OVERLAY_W] > var_values[VAR_MAIN_W] ||
  217. over->y + var_values[VAR_OVERLAY_H] > var_values[VAR_MAIN_H]) {
  218. av_log(ctx, AV_LOG_ERROR,
  219. "Overlay area (%d,%d)<->(%d,%d) not within the main area (0,0)<->(%d,%d) or zero-sized\n",
  220. over->x, over->y,
  221. (int)(over->x + var_values[VAR_OVERLAY_W]),
  222. (int)(over->y + var_values[VAR_OVERLAY_H]),
  223. (int)var_values[VAR_MAIN_W], (int)var_values[VAR_MAIN_H]);
  224. return AVERROR(EINVAL);
  225. }
  226. return 0;
  227. fail:
  228. av_log(NULL, AV_LOG_ERROR,
  229. "Error when evaluating the expression '%s'\n", expr);
  230. return ret;
  231. }
  232. static int config_output(AVFilterLink *outlink)
  233. {
  234. AVFilterContext *ctx = outlink->src;
  235. outlink->w = ctx->inputs[MAIN]->w;
  236. outlink->h = ctx->inputs[MAIN]->h;
  237. outlink->time_base = ctx->inputs[MAIN]->time_base;
  238. return 0;
  239. }
  240. static AVFilterBufferRef *get_video_buffer(AVFilterLink *link, int perms, int w, int h)
  241. {
  242. return ff_get_video_buffer(link->dst->outputs[0], perms, w, h);
  243. }
  244. // divide by 255 and round to nearest
  245. // apply a fast variant: (X+127)/255 = ((X+127)*257+257)>>16 = ((X+128)*257)>>16
  246. #define FAST_DIV255(x) ((((x) + 128) * 257) >> 16)
  247. static void blend_slice(AVFilterContext *ctx,
  248. AVFilterBufferRef *dst, AVFilterBufferRef *src,
  249. int x, int y, int w, int h,
  250. int slice_y, int slice_w, int slice_h)
  251. {
  252. OverlayContext *over = ctx->priv;
  253. int i, j, k;
  254. int width, height;
  255. int overlay_end_y = y+h;
  256. int slice_end_y = slice_y+slice_h;
  257. int end_y, start_y;
  258. width = FFMIN(slice_w - x, w);
  259. end_y = FFMIN(slice_end_y, overlay_end_y);
  260. start_y = FFMAX(y, slice_y);
  261. height = end_y - start_y;
  262. if (over->main_is_packed_rgb) {
  263. uint8_t *dp = dst->data[0] + x * over->main_pix_step[0] +
  264. start_y * dst->linesize[0];
  265. uint8_t *sp = src->data[0];
  266. uint8_t alpha; ///< the amount of overlay to blend on to main
  267. const int dr = over->main_rgba_map[R];
  268. const int dg = over->main_rgba_map[G];
  269. const int db = over->main_rgba_map[B];
  270. const int da = over->main_rgba_map[A];
  271. const int dstep = over->main_pix_step[0];
  272. const int sr = over->overlay_rgba_map[R];
  273. const int sg = over->overlay_rgba_map[G];
  274. const int sb = over->overlay_rgba_map[B];
  275. const int sa = over->overlay_rgba_map[A];
  276. const int sstep = over->overlay_pix_step[0];
  277. const int main_has_alpha = over->main_has_alpha;
  278. if (slice_y > y)
  279. sp += (slice_y - y) * src->linesize[0];
  280. for (i = 0; i < height; i++) {
  281. uint8_t *d = dp, *s = sp;
  282. for (j = 0; j < width; j++) {
  283. alpha = s[sa];
  284. // if the main channel has an alpha channel, alpha has to be calculated
  285. // to create an un-premultiplied (straight) alpha value
  286. if (main_has_alpha && alpha != 0 && alpha != 255) {
  287. // apply the general equation:
  288. // alpha = alpha_overlay / ( (alpha_main + alpha_overlay) - (alpha_main * alpha_overlay) )
  289. alpha =
  290. // the next line is a faster version of: 255 * 255 * alpha
  291. ( (alpha << 16) - (alpha << 9) + alpha )
  292. /
  293. // the next line is a faster version of: 255 * (alpha + d[da])
  294. ( ((alpha + d[da]) << 8 ) - (alpha + d[da])
  295. - d[da] * alpha );
  296. }
  297. switch (alpha) {
  298. case 0:
  299. break;
  300. case 255:
  301. d[dr] = s[sr];
  302. d[dg] = s[sg];
  303. d[db] = s[sb];
  304. break;
  305. default:
  306. // main_value = main_value * (1 - alpha) + overlay_value * alpha
  307. // since alpha is in the range 0-255, the result must divided by 255
  308. d[dr] = FAST_DIV255(d[dr] * (255 - alpha) + s[sr] * alpha);
  309. d[dg] = FAST_DIV255(d[dg] * (255 - alpha) + s[sg] * alpha);
  310. d[db] = FAST_DIV255(d[db] * (255 - alpha) + s[sb] * alpha);
  311. }
  312. if (main_has_alpha) {
  313. switch (alpha) {
  314. case 0:
  315. break;
  316. case 255:
  317. d[da] = s[sa];
  318. break;
  319. default:
  320. // apply alpha compositing: main_alpha += (1-main_alpha) * overlay_alpha
  321. d[da] += FAST_DIV255((255 - d[da]) * s[sa]);
  322. }
  323. }
  324. d += dstep;
  325. s += sstep;
  326. }
  327. dp += dst->linesize[0];
  328. sp += src->linesize[0];
  329. }
  330. } else {
  331. for (i = 0; i < 3; i++) {
  332. int hsub = i ? over->hsub : 0;
  333. int vsub = i ? over->vsub : 0;
  334. uint8_t *dp = dst->data[i] + (x >> hsub) +
  335. (start_y >> vsub) * dst->linesize[i];
  336. uint8_t *sp = src->data[i];
  337. uint8_t *ap = src->data[3];
  338. int wp = FFALIGN(width, 1<<hsub) >> hsub;
  339. int hp = FFALIGN(height, 1<<vsub) >> vsub;
  340. if (slice_y > y) {
  341. sp += ((slice_y - y) >> vsub) * src->linesize[i];
  342. ap += (slice_y - y) * src->linesize[3];
  343. }
  344. for (j = 0; j < hp; j++) {
  345. uint8_t *d = dp, *s = sp, *a = ap;
  346. for (k = 0; k < wp; k++) {
  347. // average alpha for color components, improve quality
  348. int alpha_v, alpha_h, alpha;
  349. if (hsub && vsub && j+1 < hp && k+1 < wp) {
  350. alpha = (a[0] + a[src->linesize[3]] +
  351. a[1] + a[src->linesize[3]+1]) >> 2;
  352. } else if (hsub || vsub) {
  353. alpha_h = hsub && k+1 < wp ?
  354. (a[0] + a[1]) >> 1 : a[0];
  355. alpha_v = vsub && j+1 < hp ?
  356. (a[0] + a[src->linesize[3]]) >> 1 : a[0];
  357. alpha = (alpha_v + alpha_h) >> 1;
  358. } else
  359. alpha = a[0];
  360. *d = FAST_DIV255(*d * (255 - alpha) + *s * alpha);
  361. s++;
  362. d++;
  363. a += 1 << hsub;
  364. }
  365. dp += dst->linesize[i];
  366. sp += src->linesize[i];
  367. ap += (1 << vsub) * src->linesize[3];
  368. }
  369. }
  370. }
  371. }
  372. static int try_start_frame(AVFilterContext *ctx, AVFilterBufferRef *mainpic)
  373. {
  374. OverlayContext *over = ctx->priv;
  375. AVFilterLink *outlink = ctx->outputs[0];
  376. AVFilterBufferRef *next_overpic, *outpicref;
  377. int ret;
  378. /* Discard obsolete overlay frames: if there is a next frame with pts is
  379. * before the main frame, we can drop the current overlay. */
  380. while (1) {
  381. next_overpic = ff_bufqueue_peek(&over->queue_over, 0);
  382. if (!next_overpic || av_compare_ts(next_overpic->pts, ctx->inputs[OVERLAY]->time_base,
  383. mainpic->pts , ctx->inputs[MAIN]->time_base) > 0)
  384. break;
  385. ff_bufqueue_get(&over->queue_over);
  386. avfilter_unref_buffer(over->overpicref);
  387. over->overpicref = next_overpic;
  388. }
  389. /* If there is no next frame and no EOF and the overlay frame is before
  390. * the main frame, we can not know yet if it will be superseded. */
  391. if (!over->queue_over.available && !over->overlay_eof &&
  392. (!over->overpicref || av_compare_ts(over->overpicref->pts, ctx->inputs[OVERLAY]->time_base,
  393. mainpic->pts , ctx->inputs[MAIN]->time_base) < 0))
  394. return AVERROR(EAGAIN);
  395. /* At this point, we know that the current overlay frame extends to the
  396. * time of the main frame. */
  397. outlink->out_buf = outpicref = avfilter_ref_buffer(mainpic, ~0);
  398. av_dlog(ctx, "main_pts:%s main_pts_time:%s",
  399. av_ts2str(outpicref->pts), av_ts2timestr(outpicref->pts, &outlink->time_base));
  400. if (over->overpicref)
  401. av_dlog(ctx, " over_pts:%s over_pts_time:%s",
  402. av_ts2str(over->overpicref->pts), av_ts2timestr(over->overpicref->pts, &outlink->time_base));
  403. av_dlog(ctx, "\n");
  404. ret = ff_start_frame(ctx->outputs[0], avfilter_ref_buffer(outpicref, ~0));
  405. over->frame_requested = 0;
  406. return ret;
  407. }
  408. static int try_start_next_frame(AVFilterContext *ctx)
  409. {
  410. OverlayContext *over = ctx->priv;
  411. AVFilterBufferRef *next_mainpic = ff_bufqueue_peek(&over->queue_main, 0);
  412. int ret;
  413. if (!next_mainpic)
  414. return AVERROR(EAGAIN);
  415. if ((ret = try_start_frame(ctx, next_mainpic)) == AVERROR(EAGAIN))
  416. return ret;
  417. avfilter_unref_buffer(ff_bufqueue_get(&over->queue_main));
  418. return ret;
  419. }
  420. static int try_push_frame(AVFilterContext *ctx)
  421. {
  422. OverlayContext *over = ctx->priv;
  423. AVFilterLink *outlink = ctx->outputs[0];
  424. AVFilterBufferRef *outpicref;
  425. int ret;
  426. if ((ret = try_start_next_frame(ctx)) < 0)
  427. return ret;
  428. outpicref = outlink->out_buf;
  429. if (over->overpicref)
  430. blend_slice(ctx, outpicref, over->overpicref, over->x, over->y,
  431. over->overpicref->video->w, over->overpicref->video->h,
  432. 0, outpicref->video->w, outpicref->video->h);
  433. if ((ret = ff_draw_slice(outlink, 0, outpicref->video->h, +1)) < 0 ||
  434. (ret = ff_end_frame(outlink)) < 0)
  435. return ret;
  436. return 0;
  437. }
  438. static int flush_frames(AVFilterContext *ctx)
  439. {
  440. int ret;
  441. while (!(ret = try_push_frame(ctx)));
  442. return ret == AVERROR(EAGAIN) ? 0 : ret;
  443. }
  444. static int start_frame_main(AVFilterLink *inlink, AVFilterBufferRef *inpicref)
  445. {
  446. AVFilterContext *ctx = inlink->dst;
  447. OverlayContext *over = ctx->priv;
  448. int ret;
  449. if ((ret = flush_frames(ctx)) < 0)
  450. return ret;
  451. if ((ret = try_start_frame(ctx, inpicref)) < 0) {
  452. if (ret != AVERROR(EAGAIN))
  453. return ret;
  454. ff_bufqueue_add(ctx, &over->queue_main, inpicref);
  455. av_assert1(inpicref == inlink->cur_buf);
  456. inlink->cur_buf = NULL;
  457. }
  458. return 0;
  459. }
  460. static int draw_slice_main(AVFilterLink *inlink, int y, int h, int slice_dir)
  461. {
  462. AVFilterContext *ctx = inlink->dst;
  463. OverlayContext *over = ctx->priv;
  464. AVFilterLink *outlink = ctx->outputs[0];
  465. AVFilterBufferRef *outpicref = outlink->out_buf;
  466. if (!outpicref)
  467. return 0;
  468. if (over->overpicref &&
  469. y + h > over->y && y < over->y + over->overpicref->video->h) {
  470. blend_slice(ctx, outpicref, over->overpicref, over->x, over->y,
  471. over->overpicref->video->w, over->overpicref->video->h,
  472. y, outpicref->video->w, h);
  473. }
  474. return ff_draw_slice(outlink, y, h, slice_dir);
  475. }
  476. static int end_frame_main(AVFilterLink *inlink)
  477. {
  478. AVFilterContext *ctx = inlink->dst;
  479. AVFilterLink *outlink = ctx->outputs[0];
  480. AVFilterBufferRef *outpicref = outlink->out_buf;
  481. flush_frames(ctx);
  482. if (!outpicref)
  483. return 0;
  484. return ff_end_frame(ctx->outputs[0]);
  485. }
  486. static int start_frame_over(AVFilterLink *inlink, AVFilterBufferRef *inpicref)
  487. {
  488. return 0;
  489. }
  490. static int end_frame_over(AVFilterLink *inlink)
  491. {
  492. AVFilterContext *ctx = inlink->dst;
  493. OverlayContext *over = ctx->priv;
  494. AVFilterBufferRef *inpicref = inlink->cur_buf;
  495. int ret;
  496. inlink->cur_buf = NULL;
  497. if ((ret = flush_frames(ctx)) < 0)
  498. return ret;
  499. ff_bufqueue_add(ctx, &over->queue_over, inpicref);
  500. ret = try_push_frame(ctx);
  501. return ret == AVERROR(EAGAIN) ? 0 : ret;
  502. }
  503. static int request_frame(AVFilterLink *outlink)
  504. {
  505. AVFilterContext *ctx = outlink->src;
  506. OverlayContext *over = ctx->priv;
  507. int input, ret;
  508. if (!try_push_frame(ctx))
  509. return 0;
  510. over->frame_requested = 1;
  511. while (over->frame_requested) {
  512. /* TODO if we had a frame duration, we could guess more accurately */
  513. input = !over->overlay_eof && (over->queue_main.available ||
  514. over->queue_over.available < 2) ?
  515. OVERLAY : MAIN;
  516. ret = ff_request_frame(ctx->inputs[input]);
  517. /* EOF on main is reported immediately */
  518. if (ret == AVERROR_EOF && input == OVERLAY) {
  519. over->overlay_eof = 1;
  520. if ((ret = try_start_next_frame(ctx)) != AVERROR(EAGAIN))
  521. return ret;
  522. ret = 0; /* continue requesting frames on main */
  523. }
  524. if (ret < 0)
  525. return ret;
  526. }
  527. return 0;
  528. }
  529. static int null_draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir)
  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= get_video_buffer,
  538. .config_props = config_input_main,
  539. .start_frame = start_frame_main,
  540. .draw_slice = draw_slice_main,
  541. .end_frame = end_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. .start_frame = start_frame_over,
  549. .draw_slice = null_draw_slice,
  550. .end_frame = end_frame_over,
  551. .min_perms = AV_PERM_READ | AV_PERM_PRESERVE,
  552. },
  553. { NULL }
  554. };
  555. static const AVFilterPad avfilter_vf_overlay_outputs[] = {
  556. {
  557. .name = "default",
  558. .type = AVMEDIA_TYPE_VIDEO,
  559. .rej_perms = AV_PERM_WRITE,
  560. .config_props = config_output,
  561. .request_frame = request_frame,
  562. },
  563. { NULL }
  564. };
  565. AVFilter avfilter_vf_overlay = {
  566. .name = "overlay",
  567. .description = NULL_IF_CONFIG_SMALL("Overlay a video source on top of the input."),
  568. .init = init,
  569. .uninit = uninit,
  570. .priv_size = sizeof(OverlayContext),
  571. .query_formats = query_formats,
  572. .inputs = avfilter_vf_overlay_inputs,
  573. .outputs = avfilter_vf_overlay_outputs,
  574. .priv_class = &overlay_class,
  575. };