vf_overlay.c 26 KB

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