vf_overlay.c 24 KB

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