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.

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