vf_overlay.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060
  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 "drawutils.h"
  38. #include "framesync.h"
  39. #include "video.h"
  40. #include "vf_overlay.h"
  41. typedef struct ThreadData {
  42. AVFrame *dst, *src;
  43. } ThreadData;
  44. static const char *const var_names[] = {
  45. "main_w", "W", ///< width of the main video
  46. "main_h", "H", ///< height of the main video
  47. "overlay_w", "w", ///< width of the overlay video
  48. "overlay_h", "h", ///< height of the overlay video
  49. "hsub",
  50. "vsub",
  51. "x",
  52. "y",
  53. "n", ///< number of frame
  54. "pos", ///< position in the file
  55. "t", ///< timestamp expressed in seconds
  56. NULL
  57. };
  58. #define MAIN 0
  59. #define OVERLAY 1
  60. #define R 0
  61. #define G 1
  62. #define B 2
  63. #define A 3
  64. #define Y 0
  65. #define U 1
  66. #define V 2
  67. enum EvalMode {
  68. EVAL_MODE_INIT,
  69. EVAL_MODE_FRAME,
  70. EVAL_MODE_NB
  71. };
  72. static av_cold void uninit(AVFilterContext *ctx)
  73. {
  74. OverlayContext *s = ctx->priv;
  75. ff_framesync_uninit(&s->fs);
  76. av_expr_free(s->x_pexpr); s->x_pexpr = NULL;
  77. av_expr_free(s->y_pexpr); s->y_pexpr = NULL;
  78. }
  79. static inline int normalize_xy(double d, int chroma_sub)
  80. {
  81. if (isnan(d))
  82. return INT_MAX;
  83. return (int)d & ~((1 << chroma_sub) - 1);
  84. }
  85. static void eval_expr(AVFilterContext *ctx)
  86. {
  87. OverlayContext *s = ctx->priv;
  88. s->var_values[VAR_X] = av_expr_eval(s->x_pexpr, s->var_values, NULL);
  89. s->var_values[VAR_Y] = av_expr_eval(s->y_pexpr, s->var_values, NULL);
  90. /* It is necessary if x is expressed from y */
  91. s->var_values[VAR_X] = av_expr_eval(s->x_pexpr, s->var_values, NULL);
  92. s->x = normalize_xy(s->var_values[VAR_X], s->hsub);
  93. s->y = normalize_xy(s->var_values[VAR_Y], s->vsub);
  94. }
  95. static int set_expr(AVExpr **pexpr, const char *expr, const char *option, void *log_ctx)
  96. {
  97. int ret;
  98. AVExpr *old = NULL;
  99. if (*pexpr)
  100. old = *pexpr;
  101. ret = av_expr_parse(pexpr, expr, var_names,
  102. NULL, NULL, NULL, NULL, 0, log_ctx);
  103. if (ret < 0) {
  104. av_log(log_ctx, AV_LOG_ERROR,
  105. "Error when evaluating the expression '%s' for %s\n",
  106. expr, option);
  107. *pexpr = old;
  108. return ret;
  109. }
  110. av_expr_free(old);
  111. return 0;
  112. }
  113. static int process_command(AVFilterContext *ctx, const char *cmd, const char *args,
  114. char *res, int res_len, int flags)
  115. {
  116. OverlayContext *s = ctx->priv;
  117. int ret;
  118. if (!strcmp(cmd, "x"))
  119. ret = set_expr(&s->x_pexpr, args, cmd, ctx);
  120. else if (!strcmp(cmd, "y"))
  121. ret = set_expr(&s->y_pexpr, args, cmd, ctx);
  122. else
  123. ret = AVERROR(ENOSYS);
  124. if (ret < 0)
  125. return ret;
  126. if (s->eval_mode == EVAL_MODE_INIT) {
  127. eval_expr(ctx);
  128. av_log(ctx, AV_LOG_VERBOSE, "x:%f xi:%d y:%f yi:%d\n",
  129. s->var_values[VAR_X], s->x,
  130. s->var_values[VAR_Y], s->y);
  131. }
  132. return ret;
  133. }
  134. static const enum AVPixelFormat alpha_pix_fmts[] = {
  135. AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA444P,
  136. AV_PIX_FMT_ARGB, AV_PIX_FMT_ABGR, AV_PIX_FMT_RGBA,
  137. AV_PIX_FMT_BGRA, AV_PIX_FMT_GBRAP, AV_PIX_FMT_NONE
  138. };
  139. static int query_formats(AVFilterContext *ctx)
  140. {
  141. OverlayContext *s = ctx->priv;
  142. /* overlay formats contains alpha, for avoiding conversion with alpha information loss */
  143. static const enum AVPixelFormat main_pix_fmts_yuv420[] = {
  144. AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVA420P,
  145. AV_PIX_FMT_NV12, AV_PIX_FMT_NV21,
  146. AV_PIX_FMT_NONE
  147. };
  148. static const enum AVPixelFormat overlay_pix_fmts_yuv420[] = {
  149. AV_PIX_FMT_YUVA420P, AV_PIX_FMT_NONE
  150. };
  151. static const enum AVPixelFormat main_pix_fmts_yuv422[] = {
  152. AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_NONE
  153. };
  154. static const enum AVPixelFormat overlay_pix_fmts_yuv422[] = {
  155. AV_PIX_FMT_YUVA422P, AV_PIX_FMT_NONE
  156. };
  157. static const enum AVPixelFormat main_pix_fmts_yuv444[] = {
  158. AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVA444P, AV_PIX_FMT_NONE
  159. };
  160. static const enum AVPixelFormat overlay_pix_fmts_yuv444[] = {
  161. AV_PIX_FMT_YUVA444P, AV_PIX_FMT_NONE
  162. };
  163. static const enum AVPixelFormat main_pix_fmts_gbrp[] = {
  164. AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRAP, AV_PIX_FMT_NONE
  165. };
  166. static const enum AVPixelFormat overlay_pix_fmts_gbrp[] = {
  167. AV_PIX_FMT_GBRAP, AV_PIX_FMT_NONE
  168. };
  169. static const enum AVPixelFormat main_pix_fmts_rgb[] = {
  170. AV_PIX_FMT_ARGB, AV_PIX_FMT_RGBA,
  171. AV_PIX_FMT_ABGR, AV_PIX_FMT_BGRA,
  172. AV_PIX_FMT_RGB24, AV_PIX_FMT_BGR24,
  173. AV_PIX_FMT_NONE
  174. };
  175. static const enum AVPixelFormat overlay_pix_fmts_rgb[] = {
  176. AV_PIX_FMT_ARGB, AV_PIX_FMT_RGBA,
  177. AV_PIX_FMT_ABGR, AV_PIX_FMT_BGRA,
  178. AV_PIX_FMT_NONE
  179. };
  180. AVFilterFormats *main_formats = NULL;
  181. AVFilterFormats *overlay_formats = NULL;
  182. int ret;
  183. switch (s->format) {
  184. case OVERLAY_FORMAT_YUV420:
  185. if (!(main_formats = ff_make_format_list(main_pix_fmts_yuv420)) ||
  186. !(overlay_formats = ff_make_format_list(overlay_pix_fmts_yuv420))) {
  187. ret = AVERROR(ENOMEM);
  188. goto fail;
  189. }
  190. break;
  191. case OVERLAY_FORMAT_YUV422:
  192. if (!(main_formats = ff_make_format_list(main_pix_fmts_yuv422)) ||
  193. !(overlay_formats = ff_make_format_list(overlay_pix_fmts_yuv422))) {
  194. ret = AVERROR(ENOMEM);
  195. goto fail;
  196. }
  197. break;
  198. case OVERLAY_FORMAT_YUV444:
  199. if (!(main_formats = ff_make_format_list(main_pix_fmts_yuv444)) ||
  200. !(overlay_formats = ff_make_format_list(overlay_pix_fmts_yuv444))) {
  201. ret = AVERROR(ENOMEM);
  202. goto fail;
  203. }
  204. break;
  205. case OVERLAY_FORMAT_RGB:
  206. if (!(main_formats = ff_make_format_list(main_pix_fmts_rgb)) ||
  207. !(overlay_formats = ff_make_format_list(overlay_pix_fmts_rgb))) {
  208. ret = AVERROR(ENOMEM);
  209. goto fail;
  210. }
  211. break;
  212. case OVERLAY_FORMAT_GBRP:
  213. if (!(main_formats = ff_make_format_list(main_pix_fmts_gbrp)) ||
  214. !(overlay_formats = ff_make_format_list(overlay_pix_fmts_gbrp))) {
  215. ret = AVERROR(ENOMEM);
  216. goto fail;
  217. }
  218. break;
  219. case OVERLAY_FORMAT_AUTO:
  220. if (!(main_formats = ff_make_format_list(alpha_pix_fmts))) {
  221. ret = AVERROR(ENOMEM);
  222. goto fail;
  223. }
  224. break;
  225. default:
  226. av_assert0(0);
  227. }
  228. if (s->format == OVERLAY_FORMAT_AUTO) {
  229. ret = ff_set_common_formats(ctx, main_formats);
  230. if (ret < 0)
  231. goto fail;
  232. } else {
  233. if ((ret = ff_formats_ref(main_formats , &ctx->inputs[MAIN]->out_formats )) < 0 ||
  234. (ret = ff_formats_ref(overlay_formats, &ctx->inputs[OVERLAY]->out_formats)) < 0 ||
  235. (ret = ff_formats_ref(main_formats , &ctx->outputs[MAIN]->in_formats )) < 0)
  236. goto fail;
  237. }
  238. return 0;
  239. fail:
  240. if (main_formats)
  241. av_freep(&main_formats->formats);
  242. av_freep(&main_formats);
  243. if (overlay_formats)
  244. av_freep(&overlay_formats->formats);
  245. av_freep(&overlay_formats);
  246. return ret;
  247. }
  248. static int config_input_overlay(AVFilterLink *inlink)
  249. {
  250. AVFilterContext *ctx = inlink->dst;
  251. OverlayContext *s = inlink->dst->priv;
  252. int ret;
  253. const AVPixFmtDescriptor *pix_desc = av_pix_fmt_desc_get(inlink->format);
  254. av_image_fill_max_pixsteps(s->overlay_pix_step, NULL, pix_desc);
  255. /* Finish the configuration by evaluating the expressions
  256. now when both inputs are configured. */
  257. s->var_values[VAR_MAIN_W ] = s->var_values[VAR_MW] = ctx->inputs[MAIN ]->w;
  258. s->var_values[VAR_MAIN_H ] = s->var_values[VAR_MH] = ctx->inputs[MAIN ]->h;
  259. s->var_values[VAR_OVERLAY_W] = s->var_values[VAR_OW] = ctx->inputs[OVERLAY]->w;
  260. s->var_values[VAR_OVERLAY_H] = s->var_values[VAR_OH] = ctx->inputs[OVERLAY]->h;
  261. s->var_values[VAR_HSUB] = 1<<pix_desc->log2_chroma_w;
  262. s->var_values[VAR_VSUB] = 1<<pix_desc->log2_chroma_h;
  263. s->var_values[VAR_X] = NAN;
  264. s->var_values[VAR_Y] = NAN;
  265. s->var_values[VAR_N] = 0;
  266. s->var_values[VAR_T] = NAN;
  267. s->var_values[VAR_POS] = NAN;
  268. if ((ret = set_expr(&s->x_pexpr, s->x_expr, "x", ctx)) < 0 ||
  269. (ret = set_expr(&s->y_pexpr, s->y_expr, "y", ctx)) < 0)
  270. return ret;
  271. s->overlay_is_packed_rgb =
  272. ff_fill_rgba_map(s->overlay_rgba_map, inlink->format) >= 0;
  273. s->overlay_has_alpha = ff_fmt_is_in(inlink->format, alpha_pix_fmts);
  274. if (s->eval_mode == EVAL_MODE_INIT) {
  275. eval_expr(ctx);
  276. av_log(ctx, AV_LOG_VERBOSE, "x:%f xi:%d y:%f yi:%d\n",
  277. s->var_values[VAR_X], s->x,
  278. s->var_values[VAR_Y], s->y);
  279. }
  280. av_log(ctx, AV_LOG_VERBOSE,
  281. "main w:%d h:%d fmt:%s overlay w:%d h:%d fmt:%s\n",
  282. ctx->inputs[MAIN]->w, ctx->inputs[MAIN]->h,
  283. av_get_pix_fmt_name(ctx->inputs[MAIN]->format),
  284. ctx->inputs[OVERLAY]->w, ctx->inputs[OVERLAY]->h,
  285. av_get_pix_fmt_name(ctx->inputs[OVERLAY]->format));
  286. return 0;
  287. }
  288. static int config_output(AVFilterLink *outlink)
  289. {
  290. AVFilterContext *ctx = outlink->src;
  291. OverlayContext *s = ctx->priv;
  292. int ret;
  293. if ((ret = ff_framesync_init_dualinput(&s->fs, ctx)) < 0)
  294. return ret;
  295. outlink->w = ctx->inputs[MAIN]->w;
  296. outlink->h = ctx->inputs[MAIN]->h;
  297. outlink->time_base = ctx->inputs[MAIN]->time_base;
  298. return ff_framesync_configure(&s->fs);
  299. }
  300. // divide by 255 and round to nearest
  301. // apply a fast variant: (X+127)/255 = ((X+127)*257+257)>>16 = ((X+128)*257)>>16
  302. #define FAST_DIV255(x) ((((x) + 128) * 257) >> 16)
  303. // calculate the unpremultiplied alpha, applying the general equation:
  304. // alpha = alpha_overlay / ( (alpha_main + alpha_overlay) - (alpha_main * alpha_overlay) )
  305. // (((x) << 16) - ((x) << 9) + (x)) is a faster version of: 255 * 255 * x
  306. // ((((x) + (y)) << 8) - ((x) + (y)) - (y) * (x)) is a faster version of: 255 * (x + y)
  307. #define UNPREMULTIPLY_ALPHA(x, y) ((((x) << 16) - ((x) << 9) + (x)) / ((((x) + (y)) << 8) - ((x) + (y)) - (y) * (x)))
  308. /**
  309. * Blend image in src to destination buffer dst at position (x, y).
  310. */
  311. static av_always_inline void blend_slice_packed_rgb(AVFilterContext *ctx,
  312. AVFrame *dst, const AVFrame *src,
  313. int main_has_alpha, int x, int y,
  314. int is_straight, int jobnr, int nb_jobs)
  315. {
  316. OverlayContext *s = ctx->priv;
  317. int i, imax, j, jmax;
  318. const int src_w = src->width;
  319. const int src_h = src->height;
  320. const int dst_w = dst->width;
  321. const int dst_h = dst->height;
  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. int slice_start, slice_end;
  334. uint8_t *S, *sp, *d, *dp;
  335. i = FFMAX(-y, 0);
  336. imax = FFMIN3(-y + dst_h, FFMIN(src_h, dst_h), y + src_h);
  337. slice_start = i + (imax * jobnr) / nb_jobs;
  338. slice_end = i + (imax * (jobnr+1)) / nb_jobs;
  339. sp = src->data[0] + (slice_start) * src->linesize[0];
  340. dp = dst->data[0] + (y + slice_start) * dst->linesize[0];
  341. for (i = slice_start; i < slice_end; i++) {
  342. j = FFMAX(-x, 0);
  343. S = sp + j * sstep;
  344. d = dp + (x+j) * dstep;
  345. for (jmax = FFMIN(-x + dst_w, src_w); j < jmax; j++) {
  346. alpha = S[sa];
  347. // if the main channel has an alpha channel, alpha has to be calculated
  348. // to create an un-premultiplied (straight) alpha value
  349. if (main_has_alpha && alpha != 0 && alpha != 255) {
  350. uint8_t alpha_d = d[da];
  351. alpha = UNPREMULTIPLY_ALPHA(alpha, alpha_d);
  352. }
  353. switch (alpha) {
  354. case 0:
  355. break;
  356. case 255:
  357. d[dr] = S[sr];
  358. d[dg] = S[sg];
  359. d[db] = S[sb];
  360. break;
  361. default:
  362. // main_value = main_value * (1 - alpha) + overlay_value * alpha
  363. // since alpha is in the range 0-255, the result must divided by 255
  364. d[dr] = is_straight ? FAST_DIV255(d[dr] * (255 - alpha) + S[sr] * alpha) :
  365. FFMIN(FAST_DIV255(d[dr] * (255 - alpha)) + S[sr], 255);
  366. d[dg] = is_straight ? FAST_DIV255(d[dg] * (255 - alpha) + S[sg] * alpha) :
  367. FFMIN(FAST_DIV255(d[dg] * (255 - alpha)) + S[sg], 255);
  368. d[db] = is_straight ? FAST_DIV255(d[db] * (255 - alpha) + S[sb] * alpha) :
  369. FFMIN(FAST_DIV255(d[db] * (255 - alpha)) + S[sb], 255);
  370. }
  371. if (main_has_alpha) {
  372. switch (alpha) {
  373. case 0:
  374. break;
  375. case 255:
  376. d[da] = S[sa];
  377. break;
  378. default:
  379. // apply alpha compositing: main_alpha += (1-main_alpha) * overlay_alpha
  380. d[da] += FAST_DIV255((255 - d[da]) * S[sa]);
  381. }
  382. }
  383. d += dstep;
  384. S += sstep;
  385. }
  386. dp += dst->linesize[0];
  387. sp += src->linesize[0];
  388. }
  389. }
  390. static av_always_inline void blend_plane(AVFilterContext *ctx,
  391. AVFrame *dst, const AVFrame *src,
  392. int src_w, int src_h,
  393. int dst_w, int dst_h,
  394. int i, int hsub, int vsub,
  395. int x, int y,
  396. int main_has_alpha,
  397. int dst_plane,
  398. int dst_offset,
  399. int dst_step,
  400. int straight,
  401. int yuv,
  402. int jobnr,
  403. int nb_jobs)
  404. {
  405. OverlayContext *octx = ctx->priv;
  406. int src_wp = AV_CEIL_RSHIFT(src_w, hsub);
  407. int src_hp = AV_CEIL_RSHIFT(src_h, vsub);
  408. int dst_wp = AV_CEIL_RSHIFT(dst_w, hsub);
  409. int dst_hp = AV_CEIL_RSHIFT(dst_h, vsub);
  410. int yp = y>>vsub;
  411. int xp = x>>hsub;
  412. uint8_t *s, *sp, *d, *dp, *dap, *a, *da, *ap;
  413. int jmax, j, k, kmax;
  414. int slice_start, slice_end;
  415. j = FFMAX(-yp, 0);
  416. jmax = FFMIN3(-yp + dst_hp, FFMIN(src_hp, dst_hp), yp + src_hp);
  417. slice_start = j + (jmax * jobnr) / nb_jobs;
  418. slice_end = j + (jmax * (jobnr+1)) / nb_jobs;
  419. sp = src->data[i] + (slice_start) * src->linesize[i];
  420. dp = dst->data[dst_plane]
  421. + (yp + slice_start) * dst->linesize[dst_plane]
  422. + dst_offset;
  423. ap = src->data[3] + (slice_start << vsub) * src->linesize[3];
  424. dap = dst->data[3] + ((yp + slice_start) << vsub) * dst->linesize[3];
  425. for (j = slice_start; j < slice_end; j++) {
  426. k = FFMAX(-xp, 0);
  427. d = dp + (xp+k) * dst_step;
  428. s = sp + k;
  429. a = ap + (k<<hsub);
  430. da = dap + ((xp+k) << hsub);
  431. kmax = FFMIN(-xp + dst_wp, src_wp);
  432. if (((vsub && j+1 < src_hp) || !vsub) && octx->blend_row[i]) {
  433. int c = octx->blend_row[i](d, da, s, a, kmax - k, src->linesize[3]);
  434. s += c;
  435. d += dst_step * c;
  436. da += (1 << hsub) * c;
  437. a += (1 << hsub) * c;
  438. k += c;
  439. }
  440. for (; k < kmax; k++) {
  441. int alpha_v, alpha_h, alpha;
  442. // average alpha for color components, improve quality
  443. if (hsub && vsub && j+1 < src_hp && k+1 < src_wp) {
  444. alpha = (a[0] + a[src->linesize[3]] +
  445. a[1] + a[src->linesize[3]+1]) >> 2;
  446. } else if (hsub || vsub) {
  447. alpha_h = hsub && k+1 < src_wp ?
  448. (a[0] + a[1]) >> 1 : a[0];
  449. alpha_v = vsub && j+1 < src_hp ?
  450. (a[0] + a[src->linesize[3]]) >> 1 : a[0];
  451. alpha = (alpha_v + alpha_h) >> 1;
  452. } else
  453. alpha = a[0];
  454. // if the main channel has an alpha channel, alpha has to be calculated
  455. // to create an un-premultiplied (straight) alpha value
  456. if (main_has_alpha && alpha != 0 && alpha != 255) {
  457. // average alpha for color components, improve quality
  458. uint8_t alpha_d;
  459. if (hsub && vsub && j+1 < src_hp && k+1 < src_wp) {
  460. alpha_d = (da[0] + da[dst->linesize[3]] +
  461. da[1] + da[dst->linesize[3]+1]) >> 2;
  462. } else if (hsub || vsub) {
  463. alpha_h = hsub && k+1 < src_wp ?
  464. (da[0] + da[1]) >> 1 : da[0];
  465. alpha_v = vsub && j+1 < src_hp ?
  466. (da[0] + da[dst->linesize[3]]) >> 1 : da[0];
  467. alpha_d = (alpha_v + alpha_h) >> 1;
  468. } else
  469. alpha_d = da[0];
  470. alpha = UNPREMULTIPLY_ALPHA(alpha, alpha_d);
  471. }
  472. if (straight) {
  473. *d = FAST_DIV255(*d * (255 - alpha) + *s * alpha);
  474. } else {
  475. if (i && yuv)
  476. *d = av_clip(FAST_DIV255((*d - 128) * (255 - alpha)) + *s - 128, -128, 128) + 128;
  477. else
  478. *d = FFMIN(FAST_DIV255(*d * (255 - alpha)) + *s, 255);
  479. }
  480. s++;
  481. d += dst_step;
  482. da += 1 << hsub;
  483. a += 1 << hsub;
  484. }
  485. dp += dst->linesize[dst_plane];
  486. sp += src->linesize[i];
  487. ap += (1 << vsub) * src->linesize[3];
  488. dap += (1 << vsub) * dst->linesize[3];
  489. }
  490. }
  491. static inline void alpha_composite(const AVFrame *src, const AVFrame *dst,
  492. int src_w, int src_h,
  493. int dst_w, int dst_h,
  494. int x, int y,
  495. int jobnr, int nb_jobs)
  496. {
  497. uint8_t alpha; ///< the amount of overlay to blend on to main
  498. uint8_t *s, *sa, *d, *da;
  499. int i, imax, j, jmax;
  500. int slice_start, slice_end;
  501. imax = FFMIN(-y + dst_h, src_h);
  502. slice_start = (imax * jobnr) / nb_jobs;
  503. slice_end = ((imax * (jobnr+1)) / nb_jobs);
  504. i = FFMAX(-y, 0);
  505. sa = src->data[3] + (i + slice_start) * src->linesize[3];
  506. da = dst->data[3] + (y + i + slice_start) * dst->linesize[3];
  507. for (i = i + slice_start; i < slice_end; i++) {
  508. j = FFMAX(-x, 0);
  509. s = sa + j;
  510. d = da + x+j;
  511. for (jmax = FFMIN(-x + dst_w, src_w); j < jmax; j++) {
  512. alpha = *s;
  513. if (alpha != 0 && alpha != 255) {
  514. uint8_t alpha_d = *d;
  515. alpha = UNPREMULTIPLY_ALPHA(alpha, alpha_d);
  516. }
  517. switch (alpha) {
  518. case 0:
  519. break;
  520. case 255:
  521. *d = *s;
  522. break;
  523. default:
  524. // apply alpha compositing: main_alpha += (1-main_alpha) * overlay_alpha
  525. *d += FAST_DIV255((255 - *d) * *s);
  526. }
  527. d += 1;
  528. s += 1;
  529. }
  530. da += dst->linesize[3];
  531. sa += src->linesize[3];
  532. }
  533. }
  534. static av_always_inline void blend_slice_yuv(AVFilterContext *ctx,
  535. AVFrame *dst, const AVFrame *src,
  536. int hsub, int vsub,
  537. int main_has_alpha,
  538. int x, int y,
  539. int is_straight,
  540. int jobnr, int nb_jobs)
  541. {
  542. OverlayContext *s = ctx->priv;
  543. const int src_w = src->width;
  544. const int src_h = src->height;
  545. const int dst_w = dst->width;
  546. const int dst_h = dst->height;
  547. blend_plane(ctx, dst, src, src_w, src_h, dst_w, dst_h, 0, 0, 0, x, y, main_has_alpha,
  548. s->main_desc->comp[0].plane, s->main_desc->comp[0].offset, s->main_desc->comp[0].step, is_straight, 1,
  549. jobnr, nb_jobs);
  550. blend_plane(ctx, dst, src, src_w, src_h, dst_w, dst_h, 1, hsub, vsub, x, y, main_has_alpha,
  551. s->main_desc->comp[1].plane, s->main_desc->comp[1].offset, s->main_desc->comp[1].step, is_straight, 1,
  552. jobnr, nb_jobs);
  553. blend_plane(ctx, dst, src, src_w, src_h, dst_w, dst_h, 2, hsub, vsub, x, y, main_has_alpha,
  554. s->main_desc->comp[2].plane, s->main_desc->comp[2].offset, s->main_desc->comp[2].step, is_straight, 1,
  555. jobnr, nb_jobs);
  556. if (main_has_alpha)
  557. alpha_composite(src, dst, src_w, src_h, dst_w, dst_h, x, y, jobnr, nb_jobs);
  558. }
  559. static av_always_inline void blend_slice_planar_rgb(AVFilterContext *ctx,
  560. AVFrame *dst, const AVFrame *src,
  561. int hsub, int vsub,
  562. int main_has_alpha,
  563. int x, int y,
  564. int is_straight,
  565. int jobnr,
  566. int nb_jobs)
  567. {
  568. OverlayContext *s = ctx->priv;
  569. const int src_w = src->width;
  570. const int src_h = src->height;
  571. const int dst_w = dst->width;
  572. const int dst_h = dst->height;
  573. blend_plane(ctx, dst, src, src_w, src_h, dst_w, dst_h, 0, 0, 0, x, y, main_has_alpha,
  574. s->main_desc->comp[1].plane, s->main_desc->comp[1].offset, s->main_desc->comp[1].step, is_straight, 0,
  575. jobnr, nb_jobs);
  576. blend_plane(ctx, dst, src, src_w, src_h, dst_w, dst_h, 1, hsub, vsub, x, y, main_has_alpha,
  577. s->main_desc->comp[2].plane, s->main_desc->comp[2].offset, s->main_desc->comp[2].step, is_straight, 0,
  578. jobnr, nb_jobs);
  579. blend_plane(ctx, dst, src, src_w, src_h, dst_w, dst_h, 2, hsub, vsub, x, y, main_has_alpha,
  580. s->main_desc->comp[0].plane, s->main_desc->comp[0].offset, s->main_desc->comp[0].step, is_straight, 0,
  581. jobnr, nb_jobs);
  582. if (main_has_alpha)
  583. alpha_composite(src, dst, src_w, src_h, dst_w, dst_h, x, y, jobnr, nb_jobs);
  584. }
  585. static int blend_slice_yuv420(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
  586. {
  587. OverlayContext *s = ctx->priv;
  588. ThreadData *td = arg;
  589. blend_slice_yuv(ctx, td->dst, td->src, 1, 1, 0, s->x, s->y, 1, jobnr, nb_jobs);
  590. return 0;
  591. }
  592. static int blend_slice_yuva420(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
  593. {
  594. OverlayContext *s = ctx->priv;
  595. ThreadData *td = arg;
  596. blend_slice_yuv(ctx, td->dst, td->src, 1, 1, 1, s->x, s->y, 1, jobnr, nb_jobs);
  597. return 0;
  598. }
  599. static int blend_slice_yuv422(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
  600. {
  601. OverlayContext *s = ctx->priv;
  602. ThreadData *td = arg;
  603. blend_slice_yuv(ctx, td->dst, td->src, 1, 0, 0, s->x, s->y, 1, jobnr, nb_jobs);
  604. return 0;
  605. }
  606. static int blend_slice_yuva422(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
  607. {
  608. OverlayContext *s = ctx->priv;
  609. ThreadData *td = arg;
  610. blend_slice_yuv(ctx, td->dst, td->src, 1, 0, 1, s->x, s->y, 1, jobnr, nb_jobs);
  611. return 0;
  612. }
  613. static int blend_slice_yuv444(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
  614. {
  615. OverlayContext *s = ctx->priv;
  616. ThreadData *td = arg;
  617. blend_slice_yuv(ctx, td->dst, td->src, 0, 0, 0, s->x, s->y, 1, jobnr, nb_jobs);
  618. return 0;
  619. }
  620. static int blend_slice_yuva444(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
  621. {
  622. OverlayContext *s = ctx->priv;
  623. ThreadData *td = arg;
  624. blend_slice_yuv(ctx, td->dst, td->src, 0, 0, 1, s->x, s->y, 1, jobnr, nb_jobs);
  625. return 0;
  626. }
  627. static int blend_slice_gbrp(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
  628. {
  629. OverlayContext *s = ctx->priv;
  630. ThreadData *td = arg;
  631. blend_slice_planar_rgb(ctx, td->dst, td->src, 0, 0, 0, s->x, s->y, 1, jobnr, nb_jobs);
  632. return 0;
  633. }
  634. static int blend_slice_gbrap(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
  635. {
  636. OverlayContext *s = ctx->priv;
  637. ThreadData *td = arg;
  638. blend_slice_planar_rgb(ctx, td->dst, td->src, 0, 0, 1, s->x, s->y, 1, jobnr, nb_jobs);
  639. return 0;
  640. }
  641. static int blend_slice_yuv420_pm(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
  642. {
  643. OverlayContext *s = ctx->priv;
  644. ThreadData *td = arg;
  645. blend_slice_yuv(ctx, td->dst, td->src, 1, 1, 0, s->x, s->y, 0, jobnr, nb_jobs);
  646. return 0;
  647. }
  648. static int blend_slice_yuva420_pm(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
  649. {
  650. OverlayContext *s = ctx->priv;
  651. ThreadData *td = arg;
  652. blend_slice_yuv(ctx, td->dst, td->src, 1, 1, 1, s->x, s->y, 0, jobnr, nb_jobs);
  653. return 0;
  654. }
  655. static int blend_slice_yuv422_pm(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
  656. {
  657. OverlayContext *s = ctx->priv;
  658. ThreadData *td = arg;
  659. blend_slice_yuv(ctx, td->dst, td->src, 1, 0, 0, s->x, s->y, 0, jobnr, nb_jobs);
  660. return 0;
  661. }
  662. static int blend_slice_yuva422_pm(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
  663. {
  664. OverlayContext *s = ctx->priv;
  665. ThreadData *td = arg;
  666. blend_slice_yuv(ctx, td->dst, td->src, 1, 0, 1, s->x, s->y, 0, jobnr, nb_jobs);
  667. return 0;
  668. }
  669. static int blend_slice_yuv444_pm(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
  670. {
  671. OverlayContext *s = ctx->priv;
  672. ThreadData *td = arg;
  673. blend_slice_yuv(ctx, td->dst, td->src, 0, 0, 0, s->x, s->y, 0, jobnr, nb_jobs);
  674. return 0;
  675. }
  676. static int blend_slice_yuva444_pm(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
  677. {
  678. OverlayContext *s = ctx->priv;
  679. ThreadData *td = arg;
  680. blend_slice_yuv(ctx, td->dst, td->src, 0, 0, 1, s->x, s->y, 0, jobnr, nb_jobs);
  681. return 0;
  682. }
  683. static int blend_slice_gbrp_pm(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
  684. {
  685. OverlayContext *s = ctx->priv;
  686. ThreadData *td = arg;
  687. blend_slice_planar_rgb(ctx, td->dst, td->src, 0, 0, 0, s->x, s->y, 0, jobnr, nb_jobs);
  688. return 0;
  689. }
  690. static int blend_slice_gbrap_pm(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
  691. {
  692. OverlayContext *s = ctx->priv;
  693. ThreadData *td = arg;
  694. blend_slice_planar_rgb(ctx, td->dst, td->src, 0, 0, 1, s->x, s->y, 0, jobnr, nb_jobs);
  695. return 0;
  696. }
  697. static int blend_slice_rgb(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
  698. {
  699. OverlayContext *s = ctx->priv;
  700. ThreadData *td = arg;
  701. blend_slice_packed_rgb(ctx, td->dst, td->src, 0, s->x, s->y, 1, jobnr, nb_jobs);
  702. return 0;
  703. }
  704. static int blend_slice_rgba(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
  705. {
  706. OverlayContext *s = ctx->priv;
  707. ThreadData *td = arg;
  708. blend_slice_packed_rgb(ctx, td->dst, td->src, 1, s->x, s->y, 1, jobnr, nb_jobs);
  709. return 0;
  710. }
  711. static int blend_slice_rgb_pm(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
  712. {
  713. OverlayContext *s = ctx->priv;
  714. ThreadData *td = arg;
  715. blend_slice_packed_rgb(ctx, td->dst, td->src, 0, s->x, s->y, 0, jobnr, nb_jobs);
  716. return 0;
  717. }
  718. static int blend_slice_rgba_pm(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
  719. {
  720. OverlayContext *s = ctx->priv;
  721. ThreadData *td = arg;
  722. blend_slice_packed_rgb(ctx, td->dst, td->src, 1, s->x, s->y, 0, jobnr, nb_jobs);
  723. return 0;
  724. }
  725. static int config_input_main(AVFilterLink *inlink)
  726. {
  727. OverlayContext *s = inlink->dst->priv;
  728. const AVPixFmtDescriptor *pix_desc = av_pix_fmt_desc_get(inlink->format);
  729. av_image_fill_max_pixsteps(s->main_pix_step, NULL, pix_desc);
  730. s->hsub = pix_desc->log2_chroma_w;
  731. s->vsub = pix_desc->log2_chroma_h;
  732. s->main_desc = pix_desc;
  733. s->main_is_packed_rgb =
  734. ff_fill_rgba_map(s->main_rgba_map, inlink->format) >= 0;
  735. s->main_has_alpha = ff_fmt_is_in(inlink->format, alpha_pix_fmts);
  736. switch (s->format) {
  737. case OVERLAY_FORMAT_YUV420:
  738. s->blend_slice = s->main_has_alpha ? blend_slice_yuva420 : blend_slice_yuv420;
  739. break;
  740. case OVERLAY_FORMAT_YUV422:
  741. s->blend_slice = s->main_has_alpha ? blend_slice_yuva422 : blend_slice_yuv422;
  742. break;
  743. case OVERLAY_FORMAT_YUV444:
  744. s->blend_slice = s->main_has_alpha ? blend_slice_yuva444 : blend_slice_yuv444;
  745. break;
  746. case OVERLAY_FORMAT_RGB:
  747. s->blend_slice = s->main_has_alpha ? blend_slice_rgba : blend_slice_rgb;
  748. break;
  749. case OVERLAY_FORMAT_GBRP:
  750. s->blend_slice = s->main_has_alpha ? blend_slice_gbrap : blend_slice_gbrp;
  751. break;
  752. case OVERLAY_FORMAT_AUTO:
  753. switch (inlink->format) {
  754. case AV_PIX_FMT_YUVA420P:
  755. s->blend_slice = blend_slice_yuva420;
  756. break;
  757. case AV_PIX_FMT_YUVA422P:
  758. s->blend_slice = blend_slice_yuva422;
  759. break;
  760. case AV_PIX_FMT_YUVA444P:
  761. s->blend_slice = blend_slice_yuva444;
  762. break;
  763. case AV_PIX_FMT_ARGB:
  764. case AV_PIX_FMT_RGBA:
  765. case AV_PIX_FMT_BGRA:
  766. case AV_PIX_FMT_ABGR:
  767. s->blend_slice = blend_slice_rgba;
  768. break;
  769. case AV_PIX_FMT_GBRAP:
  770. s->blend_slice = blend_slice_gbrap;
  771. break;
  772. default:
  773. av_assert0(0);
  774. break;
  775. }
  776. break;
  777. }
  778. if (!s->alpha_format)
  779. goto end;
  780. switch (s->format) {
  781. case OVERLAY_FORMAT_YUV420:
  782. s->blend_slice = s->main_has_alpha ? blend_slice_yuva420_pm : blend_slice_yuv420_pm;
  783. break;
  784. case OVERLAY_FORMAT_YUV422:
  785. s->blend_slice = s->main_has_alpha ? blend_slice_yuva422_pm : blend_slice_yuv422_pm;
  786. break;
  787. case OVERLAY_FORMAT_YUV444:
  788. s->blend_slice = s->main_has_alpha ? blend_slice_yuva444_pm : blend_slice_yuv444_pm;
  789. break;
  790. case OVERLAY_FORMAT_RGB:
  791. s->blend_slice = s->main_has_alpha ? blend_slice_rgba_pm : blend_slice_rgb_pm;
  792. break;
  793. case OVERLAY_FORMAT_GBRP:
  794. s->blend_slice = s->main_has_alpha ? blend_slice_gbrap_pm : blend_slice_gbrp_pm;
  795. break;
  796. case OVERLAY_FORMAT_AUTO:
  797. switch (inlink->format) {
  798. case AV_PIX_FMT_YUVA420P:
  799. s->blend_slice = blend_slice_yuva420_pm;
  800. break;
  801. case AV_PIX_FMT_YUVA422P:
  802. s->blend_slice = blend_slice_yuva422_pm;
  803. break;
  804. case AV_PIX_FMT_YUVA444P:
  805. s->blend_slice = blend_slice_yuva444_pm;
  806. break;
  807. case AV_PIX_FMT_ARGB:
  808. case AV_PIX_FMT_RGBA:
  809. case AV_PIX_FMT_BGRA:
  810. case AV_PIX_FMT_ABGR:
  811. s->blend_slice = blend_slice_rgba_pm;
  812. break;
  813. case AV_PIX_FMT_GBRAP:
  814. s->blend_slice = blend_slice_gbrap_pm;
  815. break;
  816. default:
  817. av_assert0(0);
  818. break;
  819. }
  820. break;
  821. }
  822. end:
  823. if (ARCH_X86)
  824. ff_overlay_init_x86(s, s->format, inlink->format,
  825. s->alpha_format, s->main_has_alpha);
  826. return 0;
  827. }
  828. static int do_blend(FFFrameSync *fs)
  829. {
  830. AVFilterContext *ctx = fs->parent;
  831. AVFrame *mainpic, *second;
  832. OverlayContext *s = ctx->priv;
  833. AVFilterLink *inlink = ctx->inputs[0];
  834. int ret;
  835. ret = ff_framesync_dualinput_get_writable(fs, &mainpic, &second);
  836. if (ret < 0)
  837. return ret;
  838. if (!second)
  839. return ff_filter_frame(ctx->outputs[0], mainpic);
  840. if (s->eval_mode == EVAL_MODE_FRAME) {
  841. int64_t pos = mainpic->pkt_pos;
  842. s->var_values[VAR_N] = inlink->frame_count_out;
  843. s->var_values[VAR_T] = mainpic->pts == AV_NOPTS_VALUE ?
  844. NAN : mainpic->pts * av_q2d(inlink->time_base);
  845. s->var_values[VAR_POS] = pos == -1 ? NAN : pos;
  846. s->var_values[VAR_OVERLAY_W] = s->var_values[VAR_OW] = second->width;
  847. s->var_values[VAR_OVERLAY_H] = s->var_values[VAR_OH] = second->height;
  848. s->var_values[VAR_MAIN_W ] = s->var_values[VAR_MW] = mainpic->width;
  849. s->var_values[VAR_MAIN_H ] = s->var_values[VAR_MH] = mainpic->height;
  850. eval_expr(ctx);
  851. av_log(ctx, AV_LOG_DEBUG, "n:%f t:%f pos:%f x:%f xi:%d y:%f yi:%d\n",
  852. s->var_values[VAR_N], s->var_values[VAR_T], s->var_values[VAR_POS],
  853. s->var_values[VAR_X], s->x,
  854. s->var_values[VAR_Y], s->y);
  855. }
  856. if (s->x < mainpic->width && s->x + second->width >= 0 &&
  857. s->y < mainpic->height && s->y + second->height >= 0) {
  858. ThreadData td;
  859. td.dst = mainpic;
  860. td.src = second;
  861. ctx->internal->execute(ctx, s->blend_slice, &td, NULL, FFMIN(FFMAX(1, FFMIN3(s->y + second->height, FFMIN(second->height, mainpic->height), mainpic->height - s->y)),
  862. ff_filter_get_nb_threads(ctx)));
  863. }
  864. return ff_filter_frame(ctx->outputs[0], mainpic);
  865. }
  866. static av_cold int init(AVFilterContext *ctx)
  867. {
  868. OverlayContext *s = ctx->priv;
  869. s->fs.on_event = do_blend;
  870. return 0;
  871. }
  872. static int activate(AVFilterContext *ctx)
  873. {
  874. OverlayContext *s = ctx->priv;
  875. return ff_framesync_activate(&s->fs);
  876. }
  877. #define OFFSET(x) offsetof(OverlayContext, x)
  878. #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
  879. static const AVOption overlay_options[] = {
  880. { "x", "set the x expression", OFFSET(x_expr), AV_OPT_TYPE_STRING, {.str = "0"}, CHAR_MIN, CHAR_MAX, FLAGS },
  881. { "y", "set the y expression", OFFSET(y_expr), AV_OPT_TYPE_STRING, {.str = "0"}, CHAR_MIN, CHAR_MAX, FLAGS },
  882. { "eof_action", "Action to take when encountering EOF from secondary input ",
  883. OFFSET(fs.opt_eof_action), AV_OPT_TYPE_INT, { .i64 = EOF_ACTION_REPEAT },
  884. EOF_ACTION_REPEAT, EOF_ACTION_PASS, .flags = FLAGS, "eof_action" },
  885. { "repeat", "Repeat the previous frame.", 0, AV_OPT_TYPE_CONST, { .i64 = EOF_ACTION_REPEAT }, .flags = FLAGS, "eof_action" },
  886. { "endall", "End both streams.", 0, AV_OPT_TYPE_CONST, { .i64 = EOF_ACTION_ENDALL }, .flags = FLAGS, "eof_action" },
  887. { "pass", "Pass through the main input.", 0, AV_OPT_TYPE_CONST, { .i64 = EOF_ACTION_PASS }, .flags = FLAGS, "eof_action" },
  888. { "eval", "specify when to evaluate expressions", OFFSET(eval_mode), AV_OPT_TYPE_INT, {.i64 = EVAL_MODE_FRAME}, 0, EVAL_MODE_NB-1, FLAGS, "eval" },
  889. { "init", "eval expressions once during initialization", 0, AV_OPT_TYPE_CONST, {.i64=EVAL_MODE_INIT}, .flags = FLAGS, .unit = "eval" },
  890. { "frame", "eval expressions per-frame", 0, AV_OPT_TYPE_CONST, {.i64=EVAL_MODE_FRAME}, .flags = FLAGS, .unit = "eval" },
  891. { "shortest", "force termination when the shortest input terminates", OFFSET(fs.opt_shortest), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, FLAGS },
  892. { "format", "set output format", OFFSET(format), AV_OPT_TYPE_INT, {.i64=OVERLAY_FORMAT_YUV420}, 0, OVERLAY_FORMAT_NB-1, FLAGS, "format" },
  893. { "yuv420", "", 0, AV_OPT_TYPE_CONST, {.i64=OVERLAY_FORMAT_YUV420}, .flags = FLAGS, .unit = "format" },
  894. { "yuv422", "", 0, AV_OPT_TYPE_CONST, {.i64=OVERLAY_FORMAT_YUV422}, .flags = FLAGS, .unit = "format" },
  895. { "yuv444", "", 0, AV_OPT_TYPE_CONST, {.i64=OVERLAY_FORMAT_YUV444}, .flags = FLAGS, .unit = "format" },
  896. { "rgb", "", 0, AV_OPT_TYPE_CONST, {.i64=OVERLAY_FORMAT_RGB}, .flags = FLAGS, .unit = "format" },
  897. { "gbrp", "", 0, AV_OPT_TYPE_CONST, {.i64=OVERLAY_FORMAT_GBRP}, .flags = FLAGS, .unit = "format" },
  898. { "auto", "", 0, AV_OPT_TYPE_CONST, {.i64=OVERLAY_FORMAT_AUTO}, .flags = FLAGS, .unit = "format" },
  899. { "repeatlast", "repeat overlay of the last overlay frame", OFFSET(fs.opt_repeatlast), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1, FLAGS },
  900. { "alpha", "alpha format", OFFSET(alpha_format), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, FLAGS, "alpha_format" },
  901. { "straight", "", 0, AV_OPT_TYPE_CONST, {.i64=0}, .flags = FLAGS, .unit = "alpha_format" },
  902. { "premultiplied", "", 0, AV_OPT_TYPE_CONST, {.i64=1}, .flags = FLAGS, .unit = "alpha_format" },
  903. { NULL }
  904. };
  905. FRAMESYNC_DEFINE_CLASS(overlay, OverlayContext, fs);
  906. static const AVFilterPad avfilter_vf_overlay_inputs[] = {
  907. {
  908. .name = "main",
  909. .type = AVMEDIA_TYPE_VIDEO,
  910. .config_props = config_input_main,
  911. },
  912. {
  913. .name = "overlay",
  914. .type = AVMEDIA_TYPE_VIDEO,
  915. .config_props = config_input_overlay,
  916. },
  917. { NULL }
  918. };
  919. static const AVFilterPad avfilter_vf_overlay_outputs[] = {
  920. {
  921. .name = "default",
  922. .type = AVMEDIA_TYPE_VIDEO,
  923. .config_props = config_output,
  924. },
  925. { NULL }
  926. };
  927. AVFilter ff_vf_overlay = {
  928. .name = "overlay",
  929. .description = NULL_IF_CONFIG_SMALL("Overlay a video source on top of the input."),
  930. .preinit = overlay_framesync_preinit,
  931. .init = init,
  932. .uninit = uninit,
  933. .priv_size = sizeof(OverlayContext),
  934. .priv_class = &overlay_class,
  935. .query_formats = query_formats,
  936. .activate = activate,
  937. .process_command = process_command,
  938. .inputs = avfilter_vf_overlay_inputs,
  939. .outputs = avfilter_vf_overlay_outputs,
  940. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL |
  941. AVFILTER_FLAG_SLICE_THREADS,
  942. };