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