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.

761 lines
27KB

  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/opt.h"
  37. #include "libavutil/timestamp.h"
  38. #include "internal.h"
  39. #include "bufferqueue.h"
  40. #include "drawutils.h"
  41. #include "video.h"
  42. static const char *const var_names[] = {
  43. "main_w", "W", ///< width of the main video
  44. "main_h", "H", ///< height of the main video
  45. "overlay_w", "w", ///< width of the overlay video
  46. "overlay_h", "h", ///< height of the overlay video
  47. "hsub",
  48. "vsub",
  49. "x",
  50. "y",
  51. "n", ///< number of frame
  52. "pos", ///< position in the file
  53. "t", ///< timestamp expressed in seconds
  54. NULL
  55. };
  56. enum var_name {
  57. VAR_MAIN_W, VAR_MW,
  58. VAR_MAIN_H, VAR_MH,
  59. VAR_OVERLAY_W, VAR_OW,
  60. VAR_OVERLAY_H, VAR_OH,
  61. VAR_HSUB,
  62. VAR_VSUB,
  63. VAR_X,
  64. VAR_Y,
  65. VAR_N,
  66. VAR_POS,
  67. VAR_T,
  68. VAR_VARS_NB
  69. };
  70. #define MAIN 0
  71. #define OVERLAY 1
  72. #define R 0
  73. #define G 1
  74. #define B 2
  75. #define A 3
  76. #define Y 0
  77. #define U 1
  78. #define V 2
  79. typedef struct {
  80. const AVClass *class;
  81. int x, y; ///< position of overlayed picture
  82. int allow_packed_rgb;
  83. uint8_t frame_requested;
  84. uint8_t overlay_eof;
  85. uint8_t main_is_packed_rgb;
  86. uint8_t main_rgba_map[4];
  87. uint8_t main_has_alpha;
  88. uint8_t overlay_is_packed_rgb;
  89. uint8_t overlay_rgba_map[4];
  90. uint8_t overlay_has_alpha;
  91. enum OverlayFormat { OVERLAY_FORMAT_YUV420, OVERLAY_FORMAT_YUV444, OVERLAY_FORMAT_RGB, OVERLAY_FORMAT_NB} format;
  92. enum EvalMode { EVAL_MODE_INIT, EVAL_MODE_FRAME, EVAL_MODE_NB } eval_mode;
  93. AVFrame *overpicref;
  94. struct FFBufQueue queue_main;
  95. struct FFBufQueue queue_over;
  96. int main_pix_step[4]; ///< steps per pixel for each plane of the main output
  97. int overlay_pix_step[4]; ///< steps per pixel for each plane of the overlay
  98. int hsub, vsub; ///< chroma subsampling values
  99. int shortest; ///< terminate stream when the shortest input terminates
  100. int repeatlast; ///< repeat last overlay frame
  101. double var_values[VAR_VARS_NB];
  102. char *x_expr, *y_expr;
  103. AVExpr *x_pexpr, *y_pexpr;
  104. } OverlayContext;
  105. static av_cold int init(AVFilterContext *ctx)
  106. {
  107. OverlayContext *over = ctx->priv;
  108. if (over->allow_packed_rgb) {
  109. av_log(ctx, AV_LOG_WARNING,
  110. "The rgb option is deprecated and is overriding the format option, use format instead\n");
  111. over->format = OVERLAY_FORMAT_RGB;
  112. }
  113. return 0;
  114. }
  115. static av_cold void uninit(AVFilterContext *ctx)
  116. {
  117. OverlayContext *over = ctx->priv;
  118. av_frame_free(&over->overpicref);
  119. ff_bufqueue_discard_all(&over->queue_main);
  120. ff_bufqueue_discard_all(&over->queue_over);
  121. av_expr_free(over->x_pexpr); over->x_pexpr = NULL;
  122. av_expr_free(over->y_pexpr); over->y_pexpr = NULL;
  123. }
  124. static inline int normalize_xy(double d, int chroma_sub)
  125. {
  126. if (isnan(d))
  127. return INT_MAX;
  128. return (int)d & ~((1 << chroma_sub) - 1);
  129. }
  130. static void eval_expr(AVFilterContext *ctx)
  131. {
  132. OverlayContext *over = ctx->priv;
  133. over->var_values[VAR_X] = av_expr_eval(over->x_pexpr, over->var_values, NULL);
  134. over->var_values[VAR_Y] = av_expr_eval(over->y_pexpr, over->var_values, NULL);
  135. over->var_values[VAR_X] = av_expr_eval(over->x_pexpr, over->var_values, NULL);
  136. over->x = normalize_xy(over->var_values[VAR_X], over->hsub);
  137. over->y = normalize_xy(over->var_values[VAR_Y], over->vsub);
  138. }
  139. static int set_expr(AVExpr **pexpr, const char *expr, const char *option, void *log_ctx)
  140. {
  141. int ret;
  142. AVExpr *old = NULL;
  143. if (*pexpr)
  144. old = *pexpr;
  145. ret = av_expr_parse(pexpr, expr, var_names,
  146. NULL, NULL, NULL, NULL, 0, log_ctx);
  147. if (ret < 0) {
  148. av_log(log_ctx, AV_LOG_ERROR,
  149. "Error when evaluating the expression '%s' for %s\n",
  150. expr, option);
  151. *pexpr = old;
  152. return ret;
  153. }
  154. av_expr_free(old);
  155. return 0;
  156. }
  157. static int process_command(AVFilterContext *ctx, const char *cmd, const char *args,
  158. char *res, int res_len, int flags)
  159. {
  160. OverlayContext *over = ctx->priv;
  161. int ret;
  162. if (!strcmp(cmd, "x"))
  163. ret = set_expr(&over->x_pexpr, args, cmd, ctx);
  164. else if (!strcmp(cmd, "y"))
  165. ret = set_expr(&over->y_pexpr, args, cmd, ctx);
  166. else
  167. ret = AVERROR(ENOSYS);
  168. if (ret < 0)
  169. return ret;
  170. if (over->eval_mode == EVAL_MODE_INIT) {
  171. eval_expr(ctx);
  172. av_log(ctx, AV_LOG_VERBOSE, "x:%f xi:%d y:%f yi:%d\n",
  173. over->var_values[VAR_X], over->x,
  174. over->var_values[VAR_Y], over->y);
  175. }
  176. return ret;
  177. }
  178. static int query_formats(AVFilterContext *ctx)
  179. {
  180. OverlayContext *over = ctx->priv;
  181. /* overlay formats contains alpha, for avoiding conversion with alpha information loss */
  182. static const enum AVPixelFormat main_pix_fmts_yuv420[] = {
  183. AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUVA420P, AV_PIX_FMT_NONE
  184. };
  185. static const enum AVPixelFormat overlay_pix_fmts_yuv420[] = {
  186. AV_PIX_FMT_YUVA420P, AV_PIX_FMT_NONE
  187. };
  188. static const enum AVPixelFormat main_pix_fmts_yuv444[] = {
  189. AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUVA444P, AV_PIX_FMT_NONE
  190. };
  191. static const enum AVPixelFormat overlay_pix_fmts_yuv444[] = {
  192. AV_PIX_FMT_YUVA444P, AV_PIX_FMT_NONE
  193. };
  194. static const enum AVPixelFormat main_pix_fmts_rgb[] = {
  195. AV_PIX_FMT_ARGB, AV_PIX_FMT_RGBA,
  196. AV_PIX_FMT_ABGR, AV_PIX_FMT_BGRA,
  197. AV_PIX_FMT_RGB24, AV_PIX_FMT_BGR24,
  198. AV_PIX_FMT_NONE
  199. };
  200. static const enum AVPixelFormat overlay_pix_fmts_rgb[] = {
  201. AV_PIX_FMT_ARGB, AV_PIX_FMT_RGBA,
  202. AV_PIX_FMT_ABGR, AV_PIX_FMT_BGRA,
  203. AV_PIX_FMT_NONE
  204. };
  205. AVFilterFormats *main_formats;
  206. AVFilterFormats *overlay_formats;
  207. switch (over->format) {
  208. case OVERLAY_FORMAT_YUV420:
  209. main_formats = ff_make_format_list(main_pix_fmts_yuv420);
  210. overlay_formats = ff_make_format_list(overlay_pix_fmts_yuv420);
  211. break;
  212. case OVERLAY_FORMAT_YUV444:
  213. main_formats = ff_make_format_list(main_pix_fmts_yuv444);
  214. overlay_formats = ff_make_format_list(overlay_pix_fmts_yuv444);
  215. break;
  216. case OVERLAY_FORMAT_RGB:
  217. main_formats = ff_make_format_list(main_pix_fmts_rgb);
  218. overlay_formats = ff_make_format_list(overlay_pix_fmts_rgb);
  219. break;
  220. default:
  221. av_assert0(0);
  222. }
  223. ff_formats_ref(main_formats, &ctx->inputs [MAIN ]->out_formats);
  224. ff_formats_ref(overlay_formats, &ctx->inputs [OVERLAY]->out_formats);
  225. ff_formats_ref(main_formats, &ctx->outputs[MAIN ]->in_formats );
  226. return 0;
  227. }
  228. static const enum AVPixelFormat alpha_pix_fmts[] = {
  229. AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUVA444P,
  230. AV_PIX_FMT_ARGB, AV_PIX_FMT_ABGR, AV_PIX_FMT_RGBA,
  231. AV_PIX_FMT_BGRA, AV_PIX_FMT_NONE
  232. };
  233. static int config_input_main(AVFilterLink *inlink)
  234. {
  235. OverlayContext *over = inlink->dst->priv;
  236. const AVPixFmtDescriptor *pix_desc = av_pix_fmt_desc_get(inlink->format);
  237. av_image_fill_max_pixsteps(over->main_pix_step, NULL, pix_desc);
  238. over->hsub = pix_desc->log2_chroma_w;
  239. over->vsub = pix_desc->log2_chroma_h;
  240. over->main_is_packed_rgb =
  241. ff_fill_rgba_map(over->main_rgba_map, inlink->format) >= 0;
  242. over->main_has_alpha = ff_fmt_is_in(inlink->format, alpha_pix_fmts);
  243. return 0;
  244. }
  245. static int config_input_overlay(AVFilterLink *inlink)
  246. {
  247. AVFilterContext *ctx = inlink->dst;
  248. OverlayContext *over = inlink->dst->priv;
  249. int ret;
  250. const AVPixFmtDescriptor *pix_desc = av_pix_fmt_desc_get(inlink->format);
  251. av_image_fill_max_pixsteps(over->overlay_pix_step, NULL, pix_desc);
  252. /* Finish the configuration by evaluating the expressions
  253. now when both inputs are configured. */
  254. over->var_values[VAR_MAIN_W ] = over->var_values[VAR_MW] = ctx->inputs[MAIN ]->w;
  255. over->var_values[VAR_MAIN_H ] = over->var_values[VAR_MH] = ctx->inputs[MAIN ]->h;
  256. over->var_values[VAR_OVERLAY_W] = over->var_values[VAR_OW] = ctx->inputs[OVERLAY]->w;
  257. over->var_values[VAR_OVERLAY_H] = over->var_values[VAR_OH] = ctx->inputs[OVERLAY]->h;
  258. over->var_values[VAR_HSUB] = 1<<pix_desc->log2_chroma_w;
  259. over->var_values[VAR_VSUB] = 1<<pix_desc->log2_chroma_h;
  260. over->var_values[VAR_X] = NAN;
  261. over->var_values[VAR_Y] = NAN;
  262. over->var_values[VAR_N] = 0;
  263. over->var_values[VAR_T] = NAN;
  264. over->var_values[VAR_POS] = NAN;
  265. if ((ret = set_expr(&over->x_pexpr, over->x_expr, "x", ctx)) < 0 ||
  266. (ret = set_expr(&over->y_pexpr, over->y_expr, "y", ctx)) < 0)
  267. return ret;
  268. over->overlay_is_packed_rgb =
  269. ff_fill_rgba_map(over->overlay_rgba_map, inlink->format) >= 0;
  270. over->overlay_has_alpha = ff_fmt_is_in(inlink->format, alpha_pix_fmts);
  271. if (over->eval_mode == EVAL_MODE_INIT) {
  272. eval_expr(ctx);
  273. av_log(ctx, AV_LOG_VERBOSE, "x:%f xi:%d y:%f yi:%d\n",
  274. over->var_values[VAR_X], over->x,
  275. over->var_values[VAR_Y], over->y);
  276. }
  277. av_log(ctx, AV_LOG_VERBOSE,
  278. "main w:%d h:%d fmt:%s overlay w:%d h:%d fmt:%s\n",
  279. ctx->inputs[MAIN]->w, ctx->inputs[MAIN]->h,
  280. av_get_pix_fmt_name(ctx->inputs[MAIN]->format),
  281. ctx->inputs[OVERLAY]->w, ctx->inputs[OVERLAY]->h,
  282. av_get_pix_fmt_name(ctx->inputs[OVERLAY]->format));
  283. return 0;
  284. }
  285. static int config_output(AVFilterLink *outlink)
  286. {
  287. AVFilterContext *ctx = outlink->src;
  288. outlink->w = ctx->inputs[MAIN]->w;
  289. outlink->h = ctx->inputs[MAIN]->h;
  290. outlink->time_base = ctx->inputs[MAIN]->time_base;
  291. return 0;
  292. }
  293. // divide by 255 and round to nearest
  294. // apply a fast variant: (X+127)/255 = ((X+127)*257+257)>>16 = ((X+128)*257)>>16
  295. #define FAST_DIV255(x) ((((x) + 128) * 257) >> 16)
  296. // calculate the unpremultiplied alpha, applying the general equation:
  297. // alpha = alpha_overlay / ( (alpha_main + alpha_overlay) - (alpha_main * alpha_overlay) )
  298. // (((x) << 16) - ((x) << 9) + (x)) is a faster version of: 255 * 255 * x
  299. // ((((x) + (y)) << 8) - ((x) + (y)) - (y) * (x)) is a faster version of: 255 * (x + y)
  300. #define UNPREMULTIPLY_ALPHA(x, y) ((((x) << 16) - ((x) << 9) + (x)) / ((((x) + (y)) << 8) - ((x) + (y)) - (y) * (x)))
  301. /**
  302. * Blend image in src to destination buffer dst at position (x, y).
  303. */
  304. static void blend_image(AVFilterContext *ctx,
  305. AVFrame *dst, AVFrame *src,
  306. int x, int y)
  307. {
  308. OverlayContext *over = ctx->priv;
  309. int i, imax, j, jmax, k, kmax;
  310. const int src_w = src->width;
  311. const int src_h = src->height;
  312. const int dst_w = dst->width;
  313. const int dst_h = dst->height;
  314. if (x >= dst_w || x+dst_w < 0 ||
  315. y >= dst_h || y+dst_h < 0)
  316. return; /* no intersection */
  317. if (over->main_is_packed_rgb) {
  318. uint8_t alpha; ///< the amount of overlay to blend on to main
  319. const int dr = over->main_rgba_map[R];
  320. const int dg = over->main_rgba_map[G];
  321. const int db = over->main_rgba_map[B];
  322. const int da = over->main_rgba_map[A];
  323. const int dstep = over->main_pix_step[0];
  324. const int sr = over->overlay_rgba_map[R];
  325. const int sg = over->overlay_rgba_map[G];
  326. const int sb = over->overlay_rgba_map[B];
  327. const int sa = over->overlay_rgba_map[A];
  328. const int sstep = over->overlay_pix_step[0];
  329. const int main_has_alpha = over->main_has_alpha;
  330. uint8_t *s, *sp, *d, *dp;
  331. i = FFMAX(-y, 0);
  332. sp = src->data[0] + i * src->linesize[0];
  333. dp = dst->data[0] + (y+i) * dst->linesize[0];
  334. for (imax = FFMIN(-y + dst_h, src_h); i < imax; i++) {
  335. j = FFMAX(-x, 0);
  336. s = sp + j * sstep;
  337. d = dp + (x+j) * dstep;
  338. for (jmax = FFMIN(-x + dst_w, src_w); j < jmax; j++) {
  339. alpha = s[sa];
  340. // if the main channel has an alpha channel, alpha has to be calculated
  341. // to create an un-premultiplied (straight) alpha value
  342. if (main_has_alpha && alpha != 0 && alpha != 255) {
  343. uint8_t alpha_d = d[da];
  344. alpha = UNPREMULTIPLY_ALPHA(alpha, alpha_d);
  345. }
  346. switch (alpha) {
  347. case 0:
  348. break;
  349. case 255:
  350. d[dr] = s[sr];
  351. d[dg] = s[sg];
  352. d[db] = s[sb];
  353. break;
  354. default:
  355. // main_value = main_value * (1 - alpha) + overlay_value * alpha
  356. // since alpha is in the range 0-255, the result must divided by 255
  357. d[dr] = FAST_DIV255(d[dr] * (255 - alpha) + s[sr] * alpha);
  358. d[dg] = FAST_DIV255(d[dg] * (255 - alpha) + s[sg] * alpha);
  359. d[db] = FAST_DIV255(d[db] * (255 - alpha) + s[sb] * alpha);
  360. }
  361. if (main_has_alpha) {
  362. switch (alpha) {
  363. case 0:
  364. break;
  365. case 255:
  366. d[da] = s[sa];
  367. break;
  368. default:
  369. // apply alpha compositing: main_alpha += (1-main_alpha) * overlay_alpha
  370. d[da] += FAST_DIV255((255 - d[da]) * s[sa]);
  371. }
  372. }
  373. d += dstep;
  374. s += sstep;
  375. }
  376. dp += dst->linesize[0];
  377. sp += src->linesize[0];
  378. }
  379. } else {
  380. const int main_has_alpha = over->main_has_alpha;
  381. if (main_has_alpha) {
  382. uint8_t alpha; ///< the amount of overlay to blend on to main
  383. uint8_t *s, *sa, *d, *da;
  384. i = FFMAX(-y, 0);
  385. sa = src->data[3] + i * src->linesize[3];
  386. da = dst->data[3] + (y+i) * dst->linesize[3];
  387. for (imax = FFMIN(-y + dst_h, src_h); i < imax; i++) {
  388. j = FFMAX(-x, 0);
  389. s = sa + j;
  390. d = da + x+j;
  391. for (jmax = FFMIN(-x + dst_w, src_w); j < jmax; j++) {
  392. alpha = *s;
  393. if (alpha != 0 && alpha != 255) {
  394. uint8_t alpha_d = *d;
  395. alpha = UNPREMULTIPLY_ALPHA(alpha, alpha_d);
  396. }
  397. switch (alpha) {
  398. case 0:
  399. break;
  400. case 255:
  401. *d = *s;
  402. break;
  403. default:
  404. // apply alpha compositing: main_alpha += (1-main_alpha) * overlay_alpha
  405. *d += FAST_DIV255((255 - *d) * *s);
  406. }
  407. d += 1;
  408. s += 1;
  409. }
  410. da += dst->linesize[3];
  411. sa += src->linesize[3];
  412. }
  413. }
  414. for (i = 0; i < 3; i++) {
  415. int hsub = i ? over->hsub : 0;
  416. int vsub = i ? over->vsub : 0;
  417. int src_wp = FF_CEIL_RSHIFT(src_w, hsub);
  418. int src_hp = FF_CEIL_RSHIFT(src_h, vsub);
  419. int dst_wp = FF_CEIL_RSHIFT(dst_w, hsub);
  420. int dst_hp = FF_CEIL_RSHIFT(dst_h, vsub);
  421. int yp = y>>vsub;
  422. int xp = x>>hsub;
  423. uint8_t *s, *sp, *d, *dp, *a, *ap;
  424. j = FFMAX(-yp, 0);
  425. sp = src->data[i] + j * src->linesize[i];
  426. dp = dst->data[i] + (yp+j) * dst->linesize[i];
  427. ap = src->data[3] + (j<<vsub) * src->linesize[3];
  428. for (jmax = FFMIN(-yp + dst_hp, src_hp); j < jmax; j++) {
  429. k = FFMAX(-xp, 0);
  430. d = dp + xp+k;
  431. s = sp + k;
  432. a = ap + (k<<hsub);
  433. for (kmax = FFMIN(-xp + dst_wp, src_wp); k < kmax; k++) {
  434. int alpha_v, alpha_h, alpha;
  435. // average alpha for color components, improve quality
  436. if (hsub && vsub && j+1 < src_hp && k+1 < src_wp) {
  437. alpha = (a[0] + a[src->linesize[3]] +
  438. a[1] + a[src->linesize[3]+1]) >> 2;
  439. } else if (hsub || vsub) {
  440. alpha_h = hsub && k+1 < src_wp ?
  441. (a[0] + a[1]) >> 1 : a[0];
  442. alpha_v = vsub && j+1 < src_hp ?
  443. (a[0] + a[src->linesize[3]]) >> 1 : a[0];
  444. alpha = (alpha_v + alpha_h) >> 1;
  445. } else
  446. alpha = a[0];
  447. // if the main channel has an alpha channel, alpha has to be calculated
  448. // to create an un-premultiplied (straight) alpha value
  449. if (main_has_alpha && alpha != 0 && alpha != 255) {
  450. // average alpha for color components, improve quality
  451. uint8_t alpha_d;
  452. if (hsub && vsub && j+1 < src_hp && k+1 < src_wp) {
  453. alpha_d = (d[0] + d[src->linesize[3]] +
  454. d[1] + d[src->linesize[3]+1]) >> 2;
  455. } else if (hsub || vsub) {
  456. alpha_h = hsub && k+1 < src_wp ?
  457. (d[0] + d[1]) >> 1 : d[0];
  458. alpha_v = vsub && j+1 < src_hp ?
  459. (d[0] + d[src->linesize[3]]) >> 1 : d[0];
  460. alpha_d = (alpha_v + alpha_h) >> 1;
  461. } else
  462. alpha_d = d[0];
  463. alpha = UNPREMULTIPLY_ALPHA(alpha, alpha_d);
  464. }
  465. *d = FAST_DIV255(*d * (255 - alpha) + *s * alpha);
  466. s++;
  467. d++;
  468. a += 1 << hsub;
  469. }
  470. dp += dst->linesize[i];
  471. sp += src->linesize[i];
  472. ap += (1 << vsub) * src->linesize[3];
  473. }
  474. }
  475. }
  476. }
  477. static int try_filter_frame(AVFilterContext *ctx, AVFrame *mainpic)
  478. {
  479. OverlayContext *over = ctx->priv;
  480. AVFilterLink *inlink = ctx->inputs[0];
  481. AVFrame *next_overpic;
  482. int ret;
  483. /* Discard obsolete overlay frames: if there is a next overlay frame with pts
  484. * before the main frame, we can drop the current overlay. */
  485. while (1) {
  486. next_overpic = ff_bufqueue_peek(&over->queue_over, 0);
  487. if (!next_overpic && over->overlay_eof && !over->repeatlast) {
  488. av_frame_free(&over->overpicref);
  489. break;
  490. }
  491. if (!next_overpic || av_compare_ts(next_overpic->pts, ctx->inputs[OVERLAY]->time_base,
  492. mainpic->pts , ctx->inputs[MAIN]->time_base) > 0)
  493. break;
  494. ff_bufqueue_get(&over->queue_over);
  495. av_frame_free(&over->overpicref);
  496. over->overpicref = next_overpic;
  497. }
  498. /* If there is no next frame and no EOF and the overlay frame is before
  499. * the main frame, we can not know yet if it will be superseded. */
  500. if (!over->queue_over.available && !over->overlay_eof &&
  501. (!over->overpicref || av_compare_ts(over->overpicref->pts, ctx->inputs[OVERLAY]->time_base,
  502. mainpic->pts , ctx->inputs[MAIN]->time_base) < 0))
  503. return AVERROR(EAGAIN);
  504. /* At this point, we know that the current overlay frame extends to the
  505. * time of the main frame. */
  506. av_dlog(ctx, "main_pts:%s main_pts_time:%s",
  507. av_ts2str(mainpic->pts), av_ts2timestr(mainpic->pts, &ctx->inputs[MAIN]->time_base));
  508. if (over->overpicref)
  509. av_dlog(ctx, " over_pts:%s over_pts_time:%s",
  510. av_ts2str(over->overpicref->pts), av_ts2timestr(over->overpicref->pts, &ctx->inputs[OVERLAY]->time_base));
  511. av_dlog(ctx, "\n");
  512. if (over->overpicref) {
  513. if (over->eval_mode == EVAL_MODE_FRAME) {
  514. int64_t pos = av_frame_get_pkt_pos(mainpic);
  515. over->var_values[VAR_N] = inlink->frame_count;
  516. over->var_values[VAR_T] = mainpic->pts == AV_NOPTS_VALUE ?
  517. NAN : mainpic->pts * av_q2d(inlink->time_base);
  518. over->var_values[VAR_POS] = pos == -1 ? NAN : pos;
  519. eval_expr(ctx);
  520. av_log(ctx, AV_LOG_DEBUG, "n:%f t:%f pos:%f x:%f xi:%d y:%f yi:%d\n",
  521. over->var_values[VAR_N], over->var_values[VAR_T], over->var_values[VAR_POS],
  522. over->var_values[VAR_X], over->x,
  523. over->var_values[VAR_Y], over->y);
  524. }
  525. if (!ctx->is_disabled)
  526. blend_image(ctx, mainpic, over->overpicref, over->x, over->y);
  527. }
  528. ret = ff_filter_frame(ctx->outputs[0], mainpic);
  529. av_assert1(ret != AVERROR(EAGAIN));
  530. over->frame_requested = 0;
  531. return ret;
  532. }
  533. static int try_filter_next_frame(AVFilterContext *ctx)
  534. {
  535. OverlayContext *over = ctx->priv;
  536. AVFrame *next_mainpic = ff_bufqueue_peek(&over->queue_main, 0);
  537. int ret;
  538. if (!next_mainpic)
  539. return AVERROR(EAGAIN);
  540. if ((ret = try_filter_frame(ctx, next_mainpic)) == AVERROR(EAGAIN))
  541. return ret;
  542. ff_bufqueue_get(&over->queue_main);
  543. return ret;
  544. }
  545. static int flush_frames(AVFilterContext *ctx)
  546. {
  547. int ret;
  548. while (!(ret = try_filter_next_frame(ctx)));
  549. return ret == AVERROR(EAGAIN) ? 0 : ret;
  550. }
  551. static int filter_frame_main(AVFilterLink *inlink, AVFrame *inpicref)
  552. {
  553. AVFilterContext *ctx = inlink->dst;
  554. OverlayContext *over = ctx->priv;
  555. int ret;
  556. if ((ret = flush_frames(ctx)) < 0)
  557. return ret;
  558. if ((ret = try_filter_frame(ctx, inpicref)) < 0) {
  559. if (ret != AVERROR(EAGAIN))
  560. return ret;
  561. ff_bufqueue_add(ctx, &over->queue_main, inpicref);
  562. }
  563. if (!over->overpicref)
  564. return 0;
  565. flush_frames(ctx);
  566. return 0;
  567. }
  568. static int filter_frame_over(AVFilterLink *inlink, AVFrame *inpicref)
  569. {
  570. AVFilterContext *ctx = inlink->dst;
  571. OverlayContext *over = ctx->priv;
  572. int ret;
  573. if ((ret = flush_frames(ctx)) < 0)
  574. return ret;
  575. ff_bufqueue_add(ctx, &over->queue_over, inpicref);
  576. ret = try_filter_next_frame(ctx);
  577. return ret == AVERROR(EAGAIN) ? 0 : ret;
  578. }
  579. static int request_frame(AVFilterLink *outlink)
  580. {
  581. AVFilterContext *ctx = outlink->src;
  582. OverlayContext *over = ctx->priv;
  583. int input, ret;
  584. if (!try_filter_next_frame(ctx))
  585. return 0;
  586. over->frame_requested = 1;
  587. while (over->frame_requested) {
  588. /* TODO if we had a frame duration, we could guess more accurately */
  589. input = !over->overlay_eof && (over->queue_main.available ||
  590. over->queue_over.available < 2) ?
  591. OVERLAY : MAIN;
  592. ret = ff_request_frame(ctx->inputs[input]);
  593. /* EOF on main is reported immediately */
  594. if (ret == AVERROR_EOF && input == OVERLAY) {
  595. over->overlay_eof = 1;
  596. if (over->shortest)
  597. return ret;
  598. if ((ret = try_filter_next_frame(ctx)) != AVERROR(EAGAIN))
  599. return ret;
  600. ret = 0; /* continue requesting frames on main */
  601. }
  602. if (ret < 0)
  603. return ret;
  604. }
  605. return 0;
  606. }
  607. #define OFFSET(x) offsetof(OverlayContext, x)
  608. #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
  609. static const AVOption overlay_options[] = {
  610. { "x", "set the x expression", OFFSET(x_expr), AV_OPT_TYPE_STRING, {.str = "0"}, CHAR_MIN, CHAR_MAX, FLAGS },
  611. { "y", "set the y expression", OFFSET(y_expr), AV_OPT_TYPE_STRING, {.str = "0"}, CHAR_MIN, CHAR_MAX, FLAGS },
  612. { "eval", "specify when to evaluate expressions", OFFSET(eval_mode), AV_OPT_TYPE_INT, {.i64 = EVAL_MODE_FRAME}, 0, EVAL_MODE_NB-1, FLAGS, "eval" },
  613. { "init", "eval expressions once during initialization", 0, AV_OPT_TYPE_CONST, {.i64=EVAL_MODE_INIT}, .flags = FLAGS, .unit = "eval" },
  614. { "frame", "eval expressions per-frame", 0, AV_OPT_TYPE_CONST, {.i64=EVAL_MODE_FRAME}, .flags = FLAGS, .unit = "eval" },
  615. { "rgb", "force packed RGB in input and output (deprecated)", OFFSET(allow_packed_rgb), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, FLAGS },
  616. { "shortest", "force termination when the shortest input terminates", OFFSET(shortest), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, FLAGS },
  617. { "format", "set output format", OFFSET(format), AV_OPT_TYPE_INT, {.i64=OVERLAY_FORMAT_YUV420}, 0, OVERLAY_FORMAT_NB-1, FLAGS, "format" },
  618. { "yuv420", "", 0, AV_OPT_TYPE_CONST, {.i64=OVERLAY_FORMAT_YUV420}, .flags = FLAGS, .unit = "format" },
  619. { "yuv444", "", 0, AV_OPT_TYPE_CONST, {.i64=OVERLAY_FORMAT_YUV444}, .flags = FLAGS, .unit = "format" },
  620. { "rgb", "", 0, AV_OPT_TYPE_CONST, {.i64=OVERLAY_FORMAT_RGB}, .flags = FLAGS, .unit = "format" },
  621. { "repeatlast", "repeat overlay of the last overlay frame", OFFSET(repeatlast), AV_OPT_TYPE_INT, {.i64=1}, 0, 1, FLAGS },
  622. { NULL }
  623. };
  624. AVFILTER_DEFINE_CLASS(overlay);
  625. static const AVFilterPad avfilter_vf_overlay_inputs[] = {
  626. {
  627. .name = "main",
  628. .type = AVMEDIA_TYPE_VIDEO,
  629. .get_video_buffer = ff_null_get_video_buffer,
  630. .config_props = config_input_main,
  631. .filter_frame = filter_frame_main,
  632. .needs_writable = 1,
  633. },
  634. {
  635. .name = "overlay",
  636. .type = AVMEDIA_TYPE_VIDEO,
  637. .config_props = config_input_overlay,
  638. .filter_frame = filter_frame_over,
  639. },
  640. { NULL }
  641. };
  642. static const AVFilterPad avfilter_vf_overlay_outputs[] = {
  643. {
  644. .name = "default",
  645. .type = AVMEDIA_TYPE_VIDEO,
  646. .config_props = config_output,
  647. .request_frame = request_frame,
  648. },
  649. { NULL }
  650. };
  651. AVFilter avfilter_vf_overlay = {
  652. .name = "overlay",
  653. .description = NULL_IF_CONFIG_SMALL("Overlay a video source on top of the input."),
  654. .init = init,
  655. .uninit = uninit,
  656. .priv_size = sizeof(OverlayContext),
  657. .priv_class = &overlay_class,
  658. .query_formats = query_formats,
  659. .process_command = process_command,
  660. .inputs = avfilter_vf_overlay_inputs,
  661. .outputs = avfilter_vf_overlay_outputs,
  662. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL,
  663. };