vf_overlay.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660
  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, {.i64=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. int ret;
  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. ret = ff_start_frame(ctx->outputs[0], avfilter_ref_buffer(outpicref, ~0));
  417. over->frame_requested = 0;
  418. return ret;
  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. int ret;
  425. if (!next_mainpic)
  426. return AVERROR(EAGAIN);
  427. if ((ret = try_start_frame(ctx, next_mainpic)) == AVERROR(EAGAIN))
  428. return ret;
  429. avfilter_unref_buffer(ff_bufqueue_get(&over->queue_main));
  430. return ret;
  431. }
  432. static int try_push_frame(AVFilterContext *ctx)
  433. {
  434. OverlayContext *over = ctx->priv;
  435. AVFilterLink *outlink = ctx->outputs[0];
  436. AVFilterBufferRef *outpicref;
  437. int ret;
  438. if ((ret = try_start_next_frame(ctx)) < 0)
  439. return ret;
  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. if ((ret = ff_draw_slice(outlink, 0, outpicref->video->h, +1)) < 0 ||
  446. (ret = ff_end_frame(outlink)) < 0)
  447. return ret;
  448. return 0;
  449. }
  450. static int flush_frames(AVFilterContext *ctx)
  451. {
  452. int ret;
  453. while (!(ret = try_push_frame(ctx)));
  454. return ret == AVERROR(EAGAIN) ? 0 : ret;
  455. }
  456. static int start_frame_main(AVFilterLink *inlink, AVFilterBufferRef *inpicref)
  457. {
  458. AVFilterContext *ctx = inlink->dst;
  459. OverlayContext *over = ctx->priv;
  460. int ret;
  461. if ((ret = flush_frames(ctx)) < 0)
  462. return ret;
  463. inpicref->pts = av_rescale_q(inpicref->pts, ctx->inputs[MAIN]->time_base,
  464. ctx->outputs[0]->time_base);
  465. if ((ret = try_start_frame(ctx, inpicref)) < 0) {
  466. if (ret != AVERROR(EAGAIN))
  467. return ret;
  468. ff_bufqueue_add(ctx, &over->queue_main, inpicref);
  469. av_assert1(inpicref == inlink->cur_buf);
  470. inlink->cur_buf = NULL;
  471. }
  472. return 0;
  473. }
  474. static int draw_slice_main(AVFilterLink *inlink, int y, int h, int slice_dir)
  475. {
  476. AVFilterContext *ctx = inlink->dst;
  477. OverlayContext *over = ctx->priv;
  478. AVFilterLink *outlink = ctx->outputs[0];
  479. AVFilterBufferRef *outpicref = outlink->out_buf;
  480. if (!outpicref)
  481. return 0;
  482. if (over->overpicref &&
  483. y + h > over->y && y < over->y + over->overpicref->video->h) {
  484. blend_slice(ctx, outpicref, over->overpicref, over->x, over->y,
  485. over->overpicref->video->w, over->overpicref->video->h,
  486. y, outpicref->video->w, h);
  487. }
  488. return ff_draw_slice(outlink, y, h, slice_dir);
  489. }
  490. static int end_frame_main(AVFilterLink *inlink)
  491. {
  492. AVFilterContext *ctx = inlink->dst;
  493. AVFilterLink *outlink = ctx->outputs[0];
  494. AVFilterBufferRef *outpicref = outlink->out_buf;
  495. flush_frames(ctx);
  496. if (!outpicref)
  497. return 0;
  498. return ff_end_frame(ctx->outputs[0]);
  499. }
  500. static int start_frame_over(AVFilterLink *inlink, AVFilterBufferRef *inpicref)
  501. {
  502. return 0;
  503. }
  504. static int end_frame_over(AVFilterLink *inlink)
  505. {
  506. AVFilterContext *ctx = inlink->dst;
  507. OverlayContext *over = ctx->priv;
  508. AVFilterBufferRef *inpicref = inlink->cur_buf;
  509. int ret;
  510. inlink->cur_buf = NULL;
  511. if ((ret = flush_frames(ctx)) < 0)
  512. return ret;
  513. inpicref->pts = av_rescale_q(inpicref->pts, ctx->inputs[OVERLAY]->time_base,
  514. ctx->outputs[0]->time_base);
  515. ff_bufqueue_add(ctx, &over->queue_over, inpicref);
  516. ret = try_push_frame(ctx);
  517. return ret == AVERROR(EAGAIN) ? 0 : ret;
  518. }
  519. static int request_frame(AVFilterLink *outlink)
  520. {
  521. AVFilterContext *ctx = outlink->src;
  522. OverlayContext *over = ctx->priv;
  523. int input, ret;
  524. if (!try_push_frame(ctx))
  525. return 0;
  526. over->frame_requested = 1;
  527. while (over->frame_requested) {
  528. /* TODO if we had a frame duration, we could guess more accurately */
  529. input = !over->overlay_eof && (over->queue_main.available ||
  530. over->queue_over.available < 2) ?
  531. OVERLAY : MAIN;
  532. ret = ff_request_frame(ctx->inputs[input]);
  533. /* EOF on main is reported immediately */
  534. if (ret == AVERROR_EOF && input == OVERLAY) {
  535. over->overlay_eof = 1;
  536. if ((ret = try_start_next_frame(ctx)) != AVERROR(EAGAIN))
  537. return ret;
  538. ret = 0; /* continue requesting frames on main */
  539. }
  540. if (ret < 0)
  541. return ret;
  542. }
  543. return 0;
  544. }
  545. static int null_draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir)
  546. {
  547. return 0;
  548. }
  549. AVFilter avfilter_vf_overlay = {
  550. .name = "overlay",
  551. .description = NULL_IF_CONFIG_SMALL("Overlay a video source on top of the input."),
  552. .init = init,
  553. .uninit = uninit,
  554. .priv_size = sizeof(OverlayContext),
  555. .query_formats = query_formats,
  556. .inputs = (const AVFilterPad[]) {{ .name = "main",
  557. .type = AVMEDIA_TYPE_VIDEO,
  558. .get_video_buffer= get_video_buffer,
  559. .config_props = config_input_main,
  560. .start_frame = start_frame_main,
  561. .draw_slice = draw_slice_main,
  562. .end_frame = end_frame_main,
  563. .min_perms = AV_PERM_READ | AV_PERM_WRITE | AV_PERM_PRESERVE },
  564. { .name = "overlay",
  565. .type = AVMEDIA_TYPE_VIDEO,
  566. .config_props = config_input_overlay,
  567. .start_frame = start_frame_over,
  568. .draw_slice = null_draw_slice,
  569. .end_frame = end_frame_over,
  570. .min_perms = AV_PERM_READ | AV_PERM_PRESERVE },
  571. { .name = NULL}},
  572. .outputs = (const AVFilterPad[]) {{ .name = "default",
  573. .type = AVMEDIA_TYPE_VIDEO,
  574. .rej_perms = AV_PERM_WRITE,
  575. .config_props = config_output,
  576. .request_frame = request_frame, },
  577. { .name = NULL}},
  578. .priv_class = &overlay_class,
  579. };