vf_overlay.c 14 KB

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