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.

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