vf_overlay.c 14 KB

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