2 * Copyright (c) 2010 Stefano Sabatini
3 * Copyright (c) 2010 Baptiste Coudurier
4 * Copyright (c) 2007 Bobby Bingham
6 * This file is part of Libav.
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.
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.
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
25 * overlay one video on top of another
30 #include "libavutil/common.h"
31 #include "libavutil/eval.h"
32 #include "libavutil/avstring.h"
33 #include "libavutil/avassert.h"
34 #include "libavutil/pixdesc.h"
35 #include "libavutil/imgutils.h"
36 #include "libavutil/mathematics.h"
37 #include "libavutil/opt.h"
41 static const char *const var_names[] = {
45 "main_w", "W", ///< width of the main video
46 "main_h", "H", ///< height of the main video
47 "overlay_w", "w", ///< width of the overlay video
48 "overlay_h", "h", ///< height of the overlay video
58 VAR_OVERLAY_W, VAR_OW,
59 VAR_OVERLAY_H, VAR_OH,
69 static const char *eof_action_str[] = {
70 "repeat", "endall", "pass"
76 typedef struct OverlayContext {
78 int x, y; ///< position of overlayed picture
80 int max_plane_step[4]; ///< steps per pixel for each plane
81 int hsub, vsub; ///< chroma subsampling values
83 char *x_expr, *y_expr;
85 enum EOFAction eof_action; ///< action to take on EOF from source
88 AVFrame *over_prev, *over_next;
91 static av_cold void uninit(AVFilterContext *ctx)
93 OverlayContext *s = ctx->priv;
95 av_frame_free(&s->main);
96 av_frame_free(&s->over_prev);
97 av_frame_free(&s->over_next);
100 static int query_formats(AVFilterContext *ctx)
102 const enum AVPixelFormat inout_pix_fmts[] = { AV_PIX_FMT_YUV420P, AV_PIX_FMT_NONE };
103 const enum AVPixelFormat blend_pix_fmts[] = { AV_PIX_FMT_YUVA420P, AV_PIX_FMT_NONE };
104 AVFilterFormats *inout_formats = ff_make_format_list(inout_pix_fmts);
105 AVFilterFormats *blend_formats = ff_make_format_list(blend_pix_fmts);
107 ff_formats_ref(inout_formats, &ctx->inputs [MAIN ]->out_formats);
108 ff_formats_ref(blend_formats, &ctx->inputs [OVERLAY]->out_formats);
109 ff_formats_ref(inout_formats, &ctx->outputs[MAIN ]->in_formats );
114 static int config_input_main(AVFilterLink *inlink)
116 OverlayContext *s = inlink->dst->priv;
117 const AVPixFmtDescriptor *pix_desc = av_pix_fmt_desc_get(inlink->format);
119 av_image_fill_max_pixsteps(s->max_plane_step, NULL, pix_desc);
120 s->hsub = pix_desc->log2_chroma_w;
121 s->vsub = pix_desc->log2_chroma_h;
126 static int config_input_overlay(AVFilterLink *inlink)
128 AVFilterContext *ctx = inlink->dst;
129 OverlayContext *s = inlink->dst->priv;
131 double var_values[VAR_VARS_NB], res;
134 /* Finish the configuration by evaluating the expressions
135 now when both inputs are configured. */
136 var_values[VAR_E ] = M_E;
137 var_values[VAR_PHI] = M_PHI;
138 var_values[VAR_PI ] = M_PI;
140 var_values[VAR_MAIN_W ] = var_values[VAR_MW] = ctx->inputs[MAIN ]->w;
141 var_values[VAR_MAIN_H ] = var_values[VAR_MH] = ctx->inputs[MAIN ]->h;
142 var_values[VAR_OVERLAY_W] = var_values[VAR_OW] = ctx->inputs[OVERLAY]->w;
143 var_values[VAR_OVERLAY_H] = var_values[VAR_OH] = ctx->inputs[OVERLAY]->h;
145 if ((ret = av_expr_parse_and_eval(&res, (expr = s->x_expr), var_names, var_values,
146 NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
149 if ((ret = av_expr_parse_and_eval(&res, (expr = s->y_expr), var_names, var_values,
150 NULL, NULL, NULL, NULL, NULL, 0, ctx)))
153 /* x may depend on y */
154 if ((ret = av_expr_parse_and_eval(&res, (expr = s->x_expr), var_names, var_values,
155 NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
159 av_log(ctx, AV_LOG_VERBOSE,
160 "main w:%d h:%d fmt:%s overlay x:%d y:%d w:%d h:%d fmt:%s eof_action:%s\n",
161 ctx->inputs[MAIN]->w, ctx->inputs[MAIN]->h,
162 av_get_pix_fmt_name(ctx->inputs[MAIN]->format),
164 ctx->inputs[OVERLAY]->w, ctx->inputs[OVERLAY]->h,
165 av_get_pix_fmt_name(ctx->inputs[OVERLAY]->format),
166 eof_action_str[s->eof_action]);
168 if (s->x < 0 || s->y < 0 ||
169 s->x + var_values[VAR_OVERLAY_W] > var_values[VAR_MAIN_W] ||
170 s->y + var_values[VAR_OVERLAY_H] > var_values[VAR_MAIN_H]) {
171 av_log(ctx, AV_LOG_ERROR,
172 "Overlay area (%d,%d)<->(%d,%d) not within the main area (0,0)<->(%d,%d) or zero-sized\n",
174 (int)(s->x + var_values[VAR_OVERLAY_W]),
175 (int)(s->y + var_values[VAR_OVERLAY_H]),
176 (int)var_values[VAR_MAIN_W], (int)var_values[VAR_MAIN_H]);
177 return AVERROR(EINVAL);
182 av_log(NULL, AV_LOG_ERROR,
183 "Error when evaluating the expression '%s'\n", expr);
187 static int config_output(AVFilterLink *outlink)
189 AVFilterContext *ctx = outlink->src;
191 outlink->w = ctx->inputs[MAIN]->w;
192 outlink->h = ctx->inputs[MAIN]->h;
193 outlink->time_base = ctx->inputs[MAIN]->time_base;
198 static void blend_frame(AVFilterContext *ctx,
199 AVFrame *dst, AVFrame *src,
202 OverlayContext *s = ctx->priv;
205 int overlay_end_y = y + src->height;
208 width = FFMIN(dst->width - x, src->width);
209 end_y = FFMIN(dst->height, overlay_end_y);
210 start_y = FFMAX(y, 0);
211 height = end_y - start_y;
213 if (dst->format == AV_PIX_FMT_BGR24 || dst->format == AV_PIX_FMT_RGB24) {
214 uint8_t *dp = dst->data[0] + x * 3 + start_y * dst->linesize[0];
215 uint8_t *sp = src->data[0];
216 int b = dst->format == AV_PIX_FMT_BGR24 ? 2 : 0;
217 int r = dst->format == AV_PIX_FMT_BGR24 ? 0 : 2;
219 sp += -y * src->linesize[0];
220 for (i = 0; i < height; i++) {
221 uint8_t *d = dp, *s = sp;
222 for (j = 0; j < width; j++) {
223 d[r] = (d[r] * (0xff - s[3]) + s[0] * s[3] + 128) >> 8;
224 d[1] = (d[1] * (0xff - s[3]) + s[1] * s[3] + 128) >> 8;
225 d[b] = (d[b] * (0xff - s[3]) + s[2] * s[3] + 128) >> 8;
229 dp += dst->linesize[0];
230 sp += src->linesize[0];
233 for (i = 0; i < 3; i++) {
234 int hsub = i ? s->hsub : 0;
235 int vsub = i ? s->vsub : 0;
236 uint8_t *dp = dst->data[i] + (x >> hsub) +
237 (start_y >> vsub) * dst->linesize[i];
238 uint8_t *sp = src->data[i];
239 uint8_t *ap = src->data[3];
240 int wp = FFALIGN(width, 1<<hsub) >> hsub;
241 int hp = FFALIGN(height, 1<<vsub) >> vsub;
243 sp += ((-y) >> vsub) * src->linesize[i];
244 ap += -y * src->linesize[3];
246 for (j = 0; j < hp; j++) {
247 uint8_t *d = dp, *s = sp, *a = ap;
248 for (k = 0; k < wp; k++) {
249 // average alpha for color components, improve quality
250 int alpha_v, alpha_h, alpha;
251 if (hsub && vsub && j+1 < hp && k+1 < wp) {
252 alpha = (a[0] + a[src->linesize[3]] +
253 a[1] + a[src->linesize[3]+1]) >> 2;
254 } else if (hsub || vsub) {
255 alpha_h = hsub && k+1 < wp ?
256 (a[0] + a[1]) >> 1 : a[0];
257 alpha_v = vsub && j+1 < hp ?
258 (a[0] + a[src->linesize[3]]) >> 1 : a[0];
259 alpha = (alpha_v + alpha_h) >> 1;
262 *d = (*d * (0xff - alpha) + *s++ * alpha + 128) >> 8;
266 dp += dst->linesize[i];
267 sp += src->linesize[i];
268 ap += (1 << vsub) * src->linesize[3];
274 static int filter_frame_main(AVFilterLink *inlink, AVFrame *frame)
276 OverlayContext *s = inlink->dst->priv;
278 av_assert0(!s->main);
284 static int filter_frame_overlay(AVFilterLink *inlink, AVFrame *frame)
286 OverlayContext *s = inlink->dst->priv;
288 av_assert0(!s->over_next);
289 s->over_next = frame;
294 static int output_frame(AVFilterContext *ctx)
296 OverlayContext *s = ctx->priv;
297 AVFilterLink *outlink = ctx->outputs[0];
298 int ret = ff_filter_frame(outlink, s->main);
304 static int handle_overlay_eof(AVFilterContext *ctx)
306 OverlayContext *s = ctx->priv;
307 /* Repeat previous frame on secondary input */
308 if (s->over_prev && s->eof_action == EOF_ACTION_REPEAT)
309 blend_frame(ctx, s->main, s->over_prev, s->x, s->y);
310 /* End both streams */
311 else if (s->eof_action == EOF_ACTION_ENDALL)
313 return output_frame(ctx);
316 static int request_frame(AVFilterLink *outlink)
318 AVFilterContext *ctx = outlink->src;
319 OverlayContext *s = ctx->priv;
320 AVRational tb_main = ctx->inputs[MAIN]->time_base;
321 AVRational tb_over = ctx->inputs[OVERLAY]->time_base;
324 /* get a frame on the main input */
326 ret = ff_request_frame(ctx->inputs[MAIN]);
331 /* get a new frame on the overlay input, on EOF check setting 'eof_action' */
333 ret = ff_request_frame(ctx->inputs[OVERLAY]);
334 if (ret == AVERROR_EOF)
335 return handle_overlay_eof(ctx);
340 while (s->main->pts != AV_NOPTS_VALUE &&
341 s->over_next->pts != AV_NOPTS_VALUE &&
342 av_compare_ts(s->over_next->pts, tb_over, s->main->pts, tb_main) < 0) {
343 av_frame_free(&s->over_prev);
344 FFSWAP(AVFrame*, s->over_prev, s->over_next);
346 ret = ff_request_frame(ctx->inputs[OVERLAY]);
347 if (ret == AVERROR_EOF)
348 return handle_overlay_eof(ctx);
353 if (s->main->pts == AV_NOPTS_VALUE ||
354 s->over_next->pts == AV_NOPTS_VALUE ||
355 !av_compare_ts(s->over_next->pts, tb_over, s->main->pts, tb_main)) {
356 blend_frame(ctx, s->main, s->over_next, s->x, s->y);
357 av_frame_free(&s->over_prev);
358 FFSWAP(AVFrame*, s->over_prev, s->over_next);
359 } else if (s->over_prev) {
360 blend_frame(ctx, s->main, s->over_prev, s->x, s->y);
363 return output_frame(ctx);
366 #define OFFSET(x) offsetof(OverlayContext, x)
367 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM
368 static const AVOption options[] = {
369 { "x", "Horizontal position of the left edge of the overlaid video on the "
370 "main video.", OFFSET(x_expr), AV_OPT_TYPE_STRING, { .str = "0" }, .flags = FLAGS },
371 { "y", "Vertical position of the top edge of the overlaid video on the "
372 "main video.", OFFSET(y_expr), AV_OPT_TYPE_STRING, { .str = "0" }, .flags = FLAGS },
373 { "eof_action", "Action to take when encountering EOF from secondary input ",
374 OFFSET(eof_action), AV_OPT_TYPE_INT, { .i64 = EOF_ACTION_REPEAT },
375 EOF_ACTION_REPEAT, EOF_ACTION_PASS, .flags = FLAGS, "eof_action" },
376 { "repeat", "Repeat the previous frame.", 0, AV_OPT_TYPE_CONST, { .i64 = EOF_ACTION_REPEAT }, .flags = FLAGS, "eof_action" },
377 { "endall", "End both streams.", 0, AV_OPT_TYPE_CONST, { .i64 = EOF_ACTION_ENDALL }, .flags = FLAGS, "eof_action" },
378 { "pass", "Pass through the main input.", 0, AV_OPT_TYPE_CONST, { .i64 = EOF_ACTION_PASS }, .flags = FLAGS, "eof_action" },
382 static const AVClass overlay_class = {
383 .class_name = "overlay",
384 .item_name = av_default_item_name,
386 .version = LIBAVUTIL_VERSION_INT,
389 static const AVFilterPad avfilter_vf_overlay_inputs[] = {
392 .type = AVMEDIA_TYPE_VIDEO,
393 .config_props = config_input_main,
394 .filter_frame = filter_frame_main,
400 .type = AVMEDIA_TYPE_VIDEO,
401 .config_props = config_input_overlay,
402 .filter_frame = filter_frame_overlay,
408 static const AVFilterPad avfilter_vf_overlay_outputs[] = {
411 .type = AVMEDIA_TYPE_VIDEO,
412 .config_props = config_output,
413 .request_frame = request_frame,
418 AVFilter ff_vf_overlay = {
420 .description = NULL_IF_CONFIG_SMALL("Overlay a video source on top of the input."),
424 .priv_size = sizeof(OverlayContext),
425 .priv_class = &overlay_class,
427 .query_formats = query_formats,
429 .inputs = avfilter_vf_overlay_inputs,
430 .outputs = avfilter_vf_overlay_outputs,