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.

778 lines
28KB

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