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.

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