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.

414 lines
15KB

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