You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

410 lines
13KB

  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 Libav.
  7. *
  8. * Libav 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. * Libav 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 Libav; 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/avassert.h"
  32. #include "libavutil/pixdesc.h"
  33. #include "libavutil/imgutils.h"
  34. #include "libavutil/mathematics.h"
  35. #include "libavutil/opt.h"
  36. #include "internal.h"
  37. #include "video.h"
  38. static const char *const var_names[] = {
  39. "E",
  40. "PHI",
  41. "PI",
  42. "main_w", "W", ///< width of the main video
  43. "main_h", "H", ///< height of the main video
  44. "overlay_w", "w", ///< width of the overlay video
  45. "overlay_h", "h", ///< height of the overlay video
  46. NULL
  47. };
  48. enum var_name {
  49. VAR_E,
  50. VAR_PHI,
  51. VAR_PI,
  52. VAR_MAIN_W, VAR_MW,
  53. VAR_MAIN_H, VAR_MH,
  54. VAR_OVERLAY_W, VAR_OW,
  55. VAR_OVERLAY_H, VAR_OH,
  56. VAR_VARS_NB
  57. };
  58. #define MAIN 0
  59. #define OVERLAY 1
  60. typedef struct {
  61. const AVClass *class;
  62. int x, y; ///< position of overlayed picture
  63. int max_plane_step[4]; ///< steps per pixel for each plane
  64. int hsub, vsub; ///< chroma subsampling values
  65. char *x_expr, *y_expr;
  66. AVFrame *main;
  67. AVFrame *over_prev, *over_next;
  68. } OverlayContext;
  69. static av_cold void uninit(AVFilterContext *ctx)
  70. {
  71. OverlayContext *s = ctx->priv;
  72. av_frame_free(&s->main);
  73. av_frame_free(&s->over_prev);
  74. av_frame_free(&s->over_next);
  75. }
  76. static int query_formats(AVFilterContext *ctx)
  77. {
  78. const enum AVPixelFormat inout_pix_fmts[] = { AV_PIX_FMT_YUV420P, AV_PIX_FMT_NONE };
  79. const enum AVPixelFormat blend_pix_fmts[] = { AV_PIX_FMT_YUVA420P, AV_PIX_FMT_NONE };
  80. AVFilterFormats *inout_formats = ff_make_format_list(inout_pix_fmts);
  81. AVFilterFormats *blend_formats = ff_make_format_list(blend_pix_fmts);
  82. ff_formats_ref(inout_formats, &ctx->inputs [MAIN ]->out_formats);
  83. ff_formats_ref(blend_formats, &ctx->inputs [OVERLAY]->out_formats);
  84. ff_formats_ref(inout_formats, &ctx->outputs[MAIN ]->in_formats );
  85. return 0;
  86. }
  87. static int config_input_main(AVFilterLink *inlink)
  88. {
  89. OverlayContext *s = inlink->dst->priv;
  90. const AVPixFmtDescriptor *pix_desc = av_pix_fmt_desc_get(inlink->format);
  91. av_image_fill_max_pixsteps(s->max_plane_step, NULL, pix_desc);
  92. s->hsub = pix_desc->log2_chroma_w;
  93. s->vsub = pix_desc->log2_chroma_h;
  94. return 0;
  95. }
  96. static int config_input_overlay(AVFilterLink *inlink)
  97. {
  98. AVFilterContext *ctx = inlink->dst;
  99. OverlayContext *s = inlink->dst->priv;
  100. char *expr;
  101. double var_values[VAR_VARS_NB], res;
  102. int ret;
  103. /* Finish the configuration by evaluating the expressions
  104. now when both inputs are configured. */
  105. var_values[VAR_E ] = M_E;
  106. var_values[VAR_PHI] = M_PHI;
  107. var_values[VAR_PI ] = M_PI;
  108. var_values[VAR_MAIN_W ] = var_values[VAR_MW] = ctx->inputs[MAIN ]->w;
  109. var_values[VAR_MAIN_H ] = var_values[VAR_MH] = ctx->inputs[MAIN ]->h;
  110. var_values[VAR_OVERLAY_W] = var_values[VAR_OW] = ctx->inputs[OVERLAY]->w;
  111. var_values[VAR_OVERLAY_H] = var_values[VAR_OH] = ctx->inputs[OVERLAY]->h;
  112. if ((ret = av_expr_parse_and_eval(&res, (expr = s->x_expr), var_names, var_values,
  113. NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
  114. goto fail;
  115. s->x = res;
  116. if ((ret = av_expr_parse_and_eval(&res, (expr = s->y_expr), var_names, var_values,
  117. NULL, NULL, NULL, NULL, NULL, 0, ctx)))
  118. goto fail;
  119. s->y = res;
  120. /* x may depend on y */
  121. if ((ret = av_expr_parse_and_eval(&res, (expr = s->x_expr), var_names, var_values,
  122. NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
  123. goto fail;
  124. s->x = res;
  125. av_log(ctx, AV_LOG_VERBOSE,
  126. "main w:%d h:%d fmt:%s overlay x:%d y:%d w:%d h:%d fmt:%s\n",
  127. ctx->inputs[MAIN]->w, ctx->inputs[MAIN]->h,
  128. av_get_pix_fmt_name(ctx->inputs[MAIN]->format),
  129. s->x, s->y,
  130. ctx->inputs[OVERLAY]->w, ctx->inputs[OVERLAY]->h,
  131. av_get_pix_fmt_name(ctx->inputs[OVERLAY]->format));
  132. if (s->x < 0 || s->y < 0 ||
  133. s->x + var_values[VAR_OVERLAY_W] > var_values[VAR_MAIN_W] ||
  134. s->y + var_values[VAR_OVERLAY_H] > var_values[VAR_MAIN_H]) {
  135. av_log(ctx, AV_LOG_ERROR,
  136. "Overlay area (%d,%d)<->(%d,%d) not within the main area (0,0)<->(%d,%d) or zero-sized\n",
  137. s->x, s->y,
  138. (int)(s->x + var_values[VAR_OVERLAY_W]),
  139. (int)(s->y + var_values[VAR_OVERLAY_H]),
  140. (int)var_values[VAR_MAIN_W], (int)var_values[VAR_MAIN_H]);
  141. return AVERROR(EINVAL);
  142. }
  143. return 0;
  144. fail:
  145. av_log(NULL, AV_LOG_ERROR,
  146. "Error when evaluating the expression '%s'\n", expr);
  147. return ret;
  148. }
  149. static int config_output(AVFilterLink *outlink)
  150. {
  151. AVFilterContext *ctx = outlink->src;
  152. outlink->w = ctx->inputs[MAIN]->w;
  153. outlink->h = ctx->inputs[MAIN]->h;
  154. outlink->time_base = ctx->inputs[MAIN]->time_base;
  155. return 0;
  156. }
  157. static void blend_frame(AVFilterContext *ctx,
  158. AVFrame *dst, AVFrame *src,
  159. int x, int y)
  160. {
  161. OverlayContext *s = ctx->priv;
  162. int i, j, k;
  163. int width, height;
  164. int overlay_end_y = y + src->height;
  165. int end_y, start_y;
  166. width = FFMIN(dst->width - x, src->width);
  167. end_y = FFMIN(dst->height, overlay_end_y);
  168. start_y = FFMAX(y, 0);
  169. height = end_y - start_y;
  170. if (dst->format == AV_PIX_FMT_BGR24 || dst->format == AV_PIX_FMT_RGB24) {
  171. uint8_t *dp = dst->data[0] + x * 3 + start_y * dst->linesize[0];
  172. uint8_t *sp = src->data[0];
  173. int b = dst->format == AV_PIX_FMT_BGR24 ? 2 : 0;
  174. int r = dst->format == AV_PIX_FMT_BGR24 ? 0 : 2;
  175. if (y < 0)
  176. sp += -y * src->linesize[0];
  177. for (i = 0; i < height; i++) {
  178. uint8_t *d = dp, *s = sp;
  179. for (j = 0; j < width; j++) {
  180. d[r] = (d[r] * (0xff - s[3]) + s[0] * s[3] + 128) >> 8;
  181. d[1] = (d[1] * (0xff - s[3]) + s[1] * s[3] + 128) >> 8;
  182. d[b] = (d[b] * (0xff - s[3]) + s[2] * s[3] + 128) >> 8;
  183. d += 3;
  184. s += 4;
  185. }
  186. dp += dst->linesize[0];
  187. sp += src->linesize[0];
  188. }
  189. } else {
  190. for (i = 0; i < 3; i++) {
  191. int hsub = i ? s->hsub : 0;
  192. int vsub = i ? s->vsub : 0;
  193. uint8_t *dp = dst->data[i] + (x >> hsub) +
  194. (start_y >> vsub) * dst->linesize[i];
  195. uint8_t *sp = src->data[i];
  196. uint8_t *ap = src->data[3];
  197. int wp = FFALIGN(width, 1<<hsub) >> hsub;
  198. int hp = FFALIGN(height, 1<<vsub) >> vsub;
  199. if (y < 0) {
  200. sp += ((-y) >> vsub) * src->linesize[i];
  201. ap += -y * src->linesize[3];
  202. }
  203. for (j = 0; j < hp; j++) {
  204. uint8_t *d = dp, *s = sp, *a = ap;
  205. for (k = 0; k < wp; k++) {
  206. // average alpha for color components, improve quality
  207. int alpha_v, alpha_h, alpha;
  208. if (hsub && vsub && j+1 < hp && k+1 < wp) {
  209. alpha = (a[0] + a[src->linesize[3]] +
  210. a[1] + a[src->linesize[3]+1]) >> 2;
  211. } else if (hsub || vsub) {
  212. alpha_h = hsub && k+1 < wp ?
  213. (a[0] + a[1]) >> 1 : a[0];
  214. alpha_v = vsub && j+1 < hp ?
  215. (a[0] + a[src->linesize[3]]) >> 1 : a[0];
  216. alpha = (alpha_v + alpha_h) >> 1;
  217. } else
  218. alpha = a[0];
  219. *d = (*d * (0xff - alpha) + *s++ * alpha + 128) >> 8;
  220. d++;
  221. a += 1 << hsub;
  222. }
  223. dp += dst->linesize[i];
  224. sp += src->linesize[i];
  225. ap += (1 << vsub) * src->linesize[3];
  226. }
  227. }
  228. }
  229. }
  230. static int filter_frame_main(AVFilterLink *inlink, AVFrame *frame)
  231. {
  232. OverlayContext *s = inlink->dst->priv;
  233. av_assert0(!s->main);
  234. s->main = frame;
  235. return 0;
  236. }
  237. static int filter_frame_overlay(AVFilterLink *inlink, AVFrame *frame)
  238. {
  239. OverlayContext *s = inlink->dst->priv;
  240. av_assert0(!s->over_next);
  241. s->over_next = frame;
  242. return 0;
  243. }
  244. static int output_frame(AVFilterContext *ctx)
  245. {
  246. OverlayContext *s = ctx->priv;
  247. AVFilterLink *outlink = ctx->outputs[0];
  248. int ret = ff_filter_frame(outlink, s->main);
  249. s->main = NULL;
  250. return ret;
  251. }
  252. static int handle_overlay_eof(AVFilterContext *ctx)
  253. {
  254. OverlayContext *s = ctx->priv;
  255. if (s->over_prev)
  256. blend_frame(ctx, s->main, s->over_prev, s->x, s->y);
  257. return output_frame(ctx);
  258. }
  259. static int request_frame(AVFilterLink *outlink)
  260. {
  261. AVFilterContext *ctx = outlink->src;
  262. OverlayContext *s = ctx->priv;
  263. AVRational tb_main = ctx->inputs[MAIN]->time_base;
  264. AVRational tb_over = ctx->inputs[OVERLAY]->time_base;
  265. int ret = 0;
  266. /* get a frame on the main input */
  267. if (!s->main) {
  268. ret = ff_request_frame(ctx->inputs[MAIN]);
  269. if (ret < 0)
  270. return ret;
  271. }
  272. /* get a new frame on the overlay input, on EOF
  273. * reuse previous */
  274. if (!s->over_next) {
  275. ret = ff_request_frame(ctx->inputs[OVERLAY]);
  276. if (ret == AVERROR_EOF)
  277. return handle_overlay_eof(ctx);
  278. else if (ret < 0)
  279. return ret;
  280. }
  281. while (s->main->pts != AV_NOPTS_VALUE &&
  282. s->over_next->pts != AV_NOPTS_VALUE &&
  283. av_compare_ts(s->over_next->pts, tb_over, s->main->pts, tb_main) < 0) {
  284. av_frame_free(&s->over_prev);
  285. FFSWAP(AVFrame*, s->over_prev, s->over_next);
  286. ret = ff_request_frame(ctx->inputs[OVERLAY]);
  287. if (ret == AVERROR_EOF)
  288. return handle_overlay_eof(ctx);
  289. else if (ret < 0)
  290. return ret;
  291. }
  292. if (s->main->pts == AV_NOPTS_VALUE ||
  293. s->over_next->pts == AV_NOPTS_VALUE ||
  294. !av_compare_ts(s->over_next->pts, tb_over, s->main->pts, tb_main)) {
  295. blend_frame(ctx, s->main, s->over_next, s->x, s->y);
  296. av_frame_free(&s->over_prev);
  297. FFSWAP(AVFrame*, s->over_prev, s->over_next);
  298. } else if (s->over_prev) {
  299. blend_frame(ctx, s->main, s->over_prev, s->x, s->y);
  300. }
  301. return output_frame(ctx);
  302. }
  303. #define OFFSET(x) offsetof(OverlayContext, x)
  304. #define FLAGS AV_OPT_FLAG_VIDEO_PARAM
  305. static const AVOption options[] = {
  306. { "x", "Horizontal position of the left edge of the overlaid video on the "
  307. "main video.", OFFSET(x_expr), AV_OPT_TYPE_STRING, { .str = "0" }, .flags = FLAGS },
  308. { "y", "Vertical position of the top edge of the overlaid video on the "
  309. "main video.", OFFSET(y_expr), AV_OPT_TYPE_STRING, { .str = "0" }, .flags = FLAGS },
  310. { NULL },
  311. };
  312. static const AVClass overlay_class = {
  313. .class_name = "overlay",
  314. .item_name = av_default_item_name,
  315. .option = options,
  316. .version = LIBAVUTIL_VERSION_INT,
  317. };
  318. static const AVFilterPad avfilter_vf_overlay_inputs[] = {
  319. {
  320. .name = "main",
  321. .type = AVMEDIA_TYPE_VIDEO,
  322. .config_props = config_input_main,
  323. .filter_frame = filter_frame_main,
  324. .needs_writable = 1,
  325. .needs_fifo = 1,
  326. },
  327. {
  328. .name = "overlay",
  329. .type = AVMEDIA_TYPE_VIDEO,
  330. .config_props = config_input_overlay,
  331. .filter_frame = filter_frame_overlay,
  332. .needs_fifo = 1,
  333. },
  334. { NULL }
  335. };
  336. static const AVFilterPad avfilter_vf_overlay_outputs[] = {
  337. {
  338. .name = "default",
  339. .type = AVMEDIA_TYPE_VIDEO,
  340. .config_props = config_output,
  341. .request_frame = request_frame,
  342. },
  343. { NULL }
  344. };
  345. AVFilter ff_vf_overlay = {
  346. .name = "overlay",
  347. .description = NULL_IF_CONFIG_SMALL("Overlay a video source on top of the input."),
  348. .uninit = uninit,
  349. .priv_size = sizeof(OverlayContext),
  350. .priv_class = &overlay_class,
  351. .query_formats = query_formats,
  352. .inputs = avfilter_vf_overlay_inputs,
  353. .outputs = avfilter_vf_overlay_outputs,
  354. };