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.

643 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. 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, {.dbl=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 PixelFormat main_pix_fmts_yuv[] = { PIX_FMT_YUV420P, PIX_FMT_NONE };
  135. const enum PixelFormat overlay_pix_fmts_yuv[] = { PIX_FMT_YUVA420P, PIX_FMT_NONE };
  136. const enum PixelFormat main_pix_fmts_rgb[] = {
  137. PIX_FMT_ARGB, PIX_FMT_RGBA,
  138. PIX_FMT_ABGR, PIX_FMT_BGRA,
  139. PIX_FMT_RGB24, PIX_FMT_BGR24,
  140. PIX_FMT_NONE
  141. };
  142. const enum PixelFormat overlay_pix_fmts_rgb[] = {
  143. PIX_FMT_ARGB, PIX_FMT_RGBA,
  144. PIX_FMT_ABGR, PIX_FMT_BGRA,
  145. 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 PixelFormat alpha_pix_fmts[] = {
  162. PIX_FMT_YUVA420P, PIX_FMT_ARGB, PIX_FMT_ABGR, PIX_FMT_RGBA,
  163. PIX_FMT_BGRA, 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_descriptors[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_pix_fmt_descriptors[ctx->inputs[MAIN]->format].name,
  212. over->x, over->y,
  213. ctx->inputs[OVERLAY]->w, ctx->inputs[OVERLAY]->h,
  214. av_pix_fmt_descriptors[ctx->inputs[OVERLAY]->format].name);
  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. int exact;
  236. // common timebase computation:
  237. AVRational tb1 = ctx->inputs[MAIN ]->time_base;
  238. AVRational tb2 = ctx->inputs[OVERLAY]->time_base;
  239. AVRational *tb = &ctx->outputs[0]->time_base;
  240. exact = av_reduce(&tb->num, &tb->den,
  241. av_gcd((int64_t)tb1.num * tb2.den,
  242. (int64_t)tb2.num * tb1.den),
  243. (int64_t)tb1.den * tb2.den, INT_MAX);
  244. av_log(ctx, AV_LOG_VERBOSE,
  245. "main_tb:%d/%d overlay_tb:%d/%d -> tb:%d/%d exact:%d\n",
  246. tb1.num, tb1.den, tb2.num, tb2.den, tb->num, tb->den, exact);
  247. if (!exact)
  248. av_log(ctx, AV_LOG_WARNING,
  249. "Timestamp conversion inexact, timestamp information loss may occurr\n");
  250. outlink->w = ctx->inputs[MAIN]->w;
  251. outlink->h = ctx->inputs[MAIN]->h;
  252. return 0;
  253. }
  254. static AVFilterBufferRef *get_video_buffer(AVFilterLink *link, int perms, int w, int h)
  255. {
  256. return ff_get_video_buffer(link->dst->outputs[0], perms, w, h);
  257. }
  258. // divide by 255 and round to nearest
  259. // apply a fast variant: (X+127)/255 = ((X+127)*257+257)>>16 = ((X+128)*257)>>16
  260. #define FAST_DIV255(x) ((((x) + 128) * 257) >> 16)
  261. static void blend_slice(AVFilterContext *ctx,
  262. AVFilterBufferRef *dst, AVFilterBufferRef *src,
  263. int x, int y, int w, int h,
  264. int slice_y, int slice_w, int slice_h)
  265. {
  266. OverlayContext *over = ctx->priv;
  267. int i, j, k;
  268. int width, height;
  269. int overlay_end_y = y+h;
  270. int slice_end_y = slice_y+slice_h;
  271. int end_y, start_y;
  272. width = FFMIN(slice_w - x, w);
  273. end_y = FFMIN(slice_end_y, overlay_end_y);
  274. start_y = FFMAX(y, slice_y);
  275. height = end_y - start_y;
  276. if (over->main_is_packed_rgb) {
  277. uint8_t *dp = dst->data[0] + x * over->main_pix_step[0] +
  278. start_y * dst->linesize[0];
  279. uint8_t *sp = src->data[0];
  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. if (slice_y > y)
  293. sp += (slice_y - y) * src->linesize[0];
  294. for (i = 0; i < height; i++) {
  295. uint8_t *d = dp, *s = sp;
  296. for (j = 0; j < width; j++) {
  297. alpha = s[sa];
  298. // if the main channel has an alpha channel, alpha has to be calculated
  299. // to create an un-premultiplied (straight) alpha value
  300. if (main_has_alpha && alpha != 0 && alpha != 255) {
  301. // apply the general equation:
  302. // alpha = alpha_overlay / ( (alpha_main + alpha_overlay) - (alpha_main * alpha_overlay) )
  303. alpha =
  304. // the next line is a faster version of: 255 * 255 * alpha
  305. ( (alpha << 16) - (alpha << 9) + alpha )
  306. /
  307. // the next line is a faster version of: 255 * (alpha + d[da])
  308. ( ((alpha + d[da]) << 8 ) - (alpha + d[da])
  309. - d[da] * alpha );
  310. }
  311. switch (alpha) {
  312. case 0:
  313. break;
  314. case 255:
  315. d[dr] = s[sr];
  316. d[dg] = s[sg];
  317. d[db] = s[sb];
  318. break;
  319. default:
  320. // main_value = main_value * (1 - alpha) + overlay_value * alpha
  321. // since alpha is in the range 0-255, the result must divided by 255
  322. d[dr] = FAST_DIV255(d[dr] * (255 - alpha) + s[sr] * alpha);
  323. d[dg] = FAST_DIV255(d[dg] * (255 - alpha) + s[sg] * alpha);
  324. d[db] = FAST_DIV255(d[db] * (255 - alpha) + s[sb] * alpha);
  325. }
  326. if (main_has_alpha) {
  327. switch (alpha) {
  328. case 0:
  329. break;
  330. case 255:
  331. d[da] = s[sa];
  332. break;
  333. default:
  334. // apply alpha compositing: main_alpha += (1-main_alpha) * overlay_alpha
  335. d[da] += FAST_DIV255((255 - d[da]) * s[sa]);
  336. }
  337. }
  338. d += dstep;
  339. s += sstep;
  340. }
  341. dp += dst->linesize[0];
  342. sp += src->linesize[0];
  343. }
  344. } else {
  345. for (i = 0; i < 3; i++) {
  346. int hsub = i ? over->hsub : 0;
  347. int vsub = i ? over->vsub : 0;
  348. uint8_t *dp = dst->data[i] + (x >> hsub) +
  349. (start_y >> vsub) * dst->linesize[i];
  350. uint8_t *sp = src->data[i];
  351. uint8_t *ap = src->data[3];
  352. int wp = FFALIGN(width, 1<<hsub) >> hsub;
  353. int hp = FFALIGN(height, 1<<vsub) >> vsub;
  354. if (slice_y > y) {
  355. sp += ((slice_y - y) >> vsub) * src->linesize[i];
  356. ap += (slice_y - y) * src->linesize[3];
  357. }
  358. for (j = 0; j < hp; j++) {
  359. uint8_t *d = dp, *s = sp, *a = ap;
  360. for (k = 0; k < wp; k++) {
  361. // average alpha for color components, improve quality
  362. int alpha_v, alpha_h, alpha;
  363. if (hsub && vsub && j+1 < hp && k+1 < wp) {
  364. alpha = (a[0] + a[src->linesize[3]] +
  365. a[1] + a[src->linesize[3]+1]) >> 2;
  366. } else if (hsub || vsub) {
  367. alpha_h = hsub && k+1 < wp ?
  368. (a[0] + a[1]) >> 1 : a[0];
  369. alpha_v = vsub && j+1 < hp ?
  370. (a[0] + a[src->linesize[3]]) >> 1 : a[0];
  371. alpha = (alpha_v + alpha_h) >> 1;
  372. } else
  373. alpha = a[0];
  374. *d = FAST_DIV255(*d * (255 - alpha) + *s * alpha);
  375. s++;
  376. d++;
  377. a += 1 << hsub;
  378. }
  379. dp += dst->linesize[i];
  380. sp += src->linesize[i];
  381. ap += (1 << vsub) * src->linesize[3];
  382. }
  383. }
  384. }
  385. }
  386. static int try_start_frame(AVFilterContext *ctx, AVFilterBufferRef *mainpic)
  387. {
  388. OverlayContext *over = ctx->priv;
  389. AVFilterLink *outlink = ctx->outputs[0];
  390. AVFilterBufferRef *next_overpic, *outpicref;
  391. /* Discard obsolete overlay frames: if there is a next frame with pts is
  392. * before the main frame, we can drop the current overlay. */
  393. while (1) {
  394. next_overpic = ff_bufqueue_peek(&over->queue_over, 0);
  395. if (!next_overpic || next_overpic->pts > mainpic->pts)
  396. break;
  397. ff_bufqueue_get(&over->queue_over);
  398. avfilter_unref_buffer(over->overpicref);
  399. over->overpicref = next_overpic;
  400. }
  401. /* If there is no next frame and no EOF and the overlay frame is before
  402. * the main frame, we can not know yet if it will be superseded. */
  403. if (!over->queue_over.available && !over->overlay_eof &&
  404. (!over->overpicref || over->overpicref->pts < mainpic->pts))
  405. return AVERROR(EAGAIN);
  406. /* At this point, we know that the current overlay frame extends to the
  407. * time of the main frame. */
  408. outlink->out_buf = outpicref = avfilter_ref_buffer(mainpic, ~0);
  409. av_dlog(ctx, "main_pts:%s main_pts_time:%s",
  410. av_ts2str(outpicref->pts), av_ts2timestr(outpicref->pts, &outlink->time_base));
  411. if (over->overpicref)
  412. av_dlog(ctx, " over_pts:%s over_pts_time:%s",
  413. av_ts2str(over->overpicref->pts), av_ts2timestr(over->overpicref->pts, &outlink->time_base));
  414. av_dlog(ctx, "\n");
  415. ff_start_frame(ctx->outputs[0], avfilter_ref_buffer(outpicref, ~0));
  416. over->frame_requested = 0;
  417. return 0;
  418. }
  419. static int try_start_next_frame(AVFilterContext *ctx)
  420. {
  421. OverlayContext *over = ctx->priv;
  422. AVFilterBufferRef *next_mainpic = ff_bufqueue_peek(&over->queue_main, 0);
  423. if (!next_mainpic || try_start_frame(ctx, next_mainpic) < 0)
  424. return AVERROR(EAGAIN);
  425. avfilter_unref_buffer(ff_bufqueue_get(&over->queue_main));
  426. return 0;
  427. }
  428. static int try_push_frame(AVFilterContext *ctx)
  429. {
  430. OverlayContext *over = ctx->priv;
  431. AVFilterLink *outlink = ctx->outputs[0];
  432. AVFilterBufferRef *outpicref;
  433. if (try_start_next_frame(ctx) < 0)
  434. return AVERROR(EAGAIN);
  435. outpicref = outlink->out_buf;
  436. if (over->overpicref)
  437. blend_slice(ctx, outpicref, over->overpicref, over->x, over->y,
  438. over->overpicref->video->w, over->overpicref->video->h,
  439. 0, outpicref->video->w, outpicref->video->h);
  440. ff_draw_slice(outlink, 0, outpicref->video->h, +1);
  441. ff_end_frame(outlink);
  442. return 0;
  443. }
  444. static void flush_frames(AVFilterContext *ctx)
  445. {
  446. while (!try_push_frame(ctx));
  447. }
  448. static int start_frame_main(AVFilterLink *inlink, AVFilterBufferRef *inpicref)
  449. {
  450. AVFilterContext *ctx = inlink->dst;
  451. OverlayContext *over = ctx->priv;
  452. flush_frames(ctx);
  453. inpicref->pts = av_rescale_q(inpicref->pts, ctx->inputs[MAIN]->time_base,
  454. ctx->outputs[0]->time_base);
  455. if (try_start_frame(ctx, inpicref) < 0) {
  456. ff_bufqueue_add(ctx, &over->queue_main, inpicref);
  457. av_assert1(inpicref == inlink->cur_buf);
  458. inlink->cur_buf = NULL;
  459. }
  460. return 0;
  461. }
  462. static int draw_slice_main(AVFilterLink *inlink, int y, int h, int slice_dir)
  463. {
  464. AVFilterContext *ctx = inlink->dst;
  465. OverlayContext *over = ctx->priv;
  466. AVFilterLink *outlink = ctx->outputs[0];
  467. AVFilterBufferRef *outpicref = outlink->out_buf;
  468. if (!outpicref)
  469. return 0;
  470. if (over->overpicref &&
  471. y + h > over->y && y < over->y + over->overpicref->video->h) {
  472. blend_slice(ctx, outpicref, over->overpicref, over->x, over->y,
  473. over->overpicref->video->w, over->overpicref->video->h,
  474. y, outpicref->video->w, h);
  475. }
  476. return ff_draw_slice(outlink, y, h, slice_dir);
  477. }
  478. static int end_frame_main(AVFilterLink *inlink)
  479. {
  480. AVFilterContext *ctx = inlink->dst;
  481. AVFilterLink *outlink = ctx->outputs[0];
  482. AVFilterBufferRef *outpicref = outlink->out_buf;
  483. flush_frames(ctx);
  484. if (!outpicref)
  485. return 0;
  486. return ff_end_frame(ctx->outputs[0]);
  487. }
  488. static int start_frame_over(AVFilterLink *inlink, AVFilterBufferRef *inpicref)
  489. {
  490. return 0;
  491. }
  492. static int end_frame_over(AVFilterLink *inlink)
  493. {
  494. AVFilterContext *ctx = inlink->dst;
  495. OverlayContext *over = ctx->priv;
  496. AVFilterBufferRef *inpicref = inlink->cur_buf;
  497. inlink->cur_buf = NULL;
  498. flush_frames(ctx);
  499. inpicref->pts = av_rescale_q(inpicref->pts, ctx->inputs[OVERLAY]->time_base,
  500. ctx->outputs[0]->time_base);
  501. ff_bufqueue_add(ctx, &over->queue_over, inpicref);
  502. return try_push_frame(ctx);
  503. }
  504. static int request_frame(AVFilterLink *outlink)
  505. {
  506. AVFilterContext *ctx = outlink->src;
  507. OverlayContext *over = ctx->priv;
  508. int input, ret;
  509. if (!try_push_frame(ctx))
  510. return 0;
  511. over->frame_requested = 1;
  512. while (over->frame_requested) {
  513. /* TODO if we had a frame duration, we could guess more accurately */
  514. input = !over->overlay_eof && (over->queue_main.available ||
  515. over->queue_over.available < 2) ?
  516. OVERLAY : MAIN;
  517. ret = ff_request_frame(ctx->inputs[input]);
  518. /* EOF on main is reported immediately */
  519. if (ret == AVERROR_EOF && input == OVERLAY) {
  520. over->overlay_eof = 1;
  521. if (!try_start_next_frame(ctx))
  522. return 0;
  523. ret = 0; /* continue requesting frames on main */
  524. }
  525. if (ret < 0)
  526. return ret;
  527. }
  528. return 0;
  529. }
  530. static int null_draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir)
  531. {
  532. return 0;
  533. }
  534. AVFilter avfilter_vf_overlay = {
  535. .name = "overlay",
  536. .description = NULL_IF_CONFIG_SMALL("Overlay a video source on top of the input."),
  537. .init = init,
  538. .uninit = uninit,
  539. .priv_size = sizeof(OverlayContext),
  540. .query_formats = query_formats,
  541. .inputs = (const AVFilterPad[]) {{ .name = "main",
  542. .type = AVMEDIA_TYPE_VIDEO,
  543. .get_video_buffer= get_video_buffer,
  544. .config_props = config_input_main,
  545. .start_frame = start_frame_main,
  546. .draw_slice = draw_slice_main,
  547. .end_frame = end_frame_main,
  548. .min_perms = AV_PERM_READ | AV_PERM_WRITE | AV_PERM_PRESERVE },
  549. { .name = "overlay",
  550. .type = AVMEDIA_TYPE_VIDEO,
  551. .config_props = config_input_overlay,
  552. .start_frame = start_frame_over,
  553. .draw_slice = null_draw_slice,
  554. .end_frame = end_frame_over,
  555. .min_perms = AV_PERM_READ | AV_PERM_PRESERVE },
  556. { .name = NULL}},
  557. .outputs = (const AVFilterPad[]) {{ .name = "default",
  558. .type = AVMEDIA_TYPE_VIDEO,
  559. .rej_perms = AV_PERM_WRITE,
  560. .config_props = config_output,
  561. .request_frame = request_frame, },
  562. { .name = NULL}},
  563. .priv_class = &overlay_class,
  564. };