vf_overlay.c 24 KB

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