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.

397 lines
14KB

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