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.

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