vf_overlay.c 23 KB

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