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.

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