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
29 #include "libavutil/eval.h"
30 #include "libavutil/avstring.h"
31 #include "libavutil/pixdesc.h"
32 #include "libavutil/imgutils.h"
35 static const char *var_names[] = {
39 "main_w", "W", ///< width of the main video
40 "main_h", "H", ///< height of the main video
41 "overlay_w", "w", ///< width of the overlay video
42 "overlay_h", "h", ///< height of the overlay video
52 VAR_OVERLAY_W, VAR_OW,
53 VAR_OVERLAY_H, VAR_OH,
61 int x, y; ///< position of overlayed picture
63 AVFilterBufferRef *overpicref;
65 int max_plane_step[4]; ///< steps per pixel for each plane
66 int hsub, vsub; ///< chroma subsampling values
68 char x_expr[256], y_expr[256];
71 static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
73 OverlayContext *over = ctx->priv;
75 av_strlcpy(over->x_expr, "0", sizeof(over->x_expr));
76 av_strlcpy(over->y_expr, "0", sizeof(over->y_expr));
79 sscanf(args, "%255[^:]:%255[^:]", over->x_expr, over->y_expr);
84 static av_cold void uninit(AVFilterContext *ctx)
86 OverlayContext *over = ctx->priv;
89 avfilter_unref_buffer(over->overpicref);
92 static int query_formats(AVFilterContext *ctx)
94 const enum PixelFormat inout_pix_fmts[] = { PIX_FMT_YUV420P, PIX_FMT_NONE };
95 const enum PixelFormat blend_pix_fmts[] = { PIX_FMT_YUVA420P, PIX_FMT_NONE };
96 AVFilterFormats *inout_formats = avfilter_make_format_list(inout_pix_fmts);
97 AVFilterFormats *blend_formats = avfilter_make_format_list(blend_pix_fmts);
99 avfilter_formats_ref(inout_formats, &ctx->inputs [MAIN ]->out_formats);
100 avfilter_formats_ref(blend_formats, &ctx->inputs [OVERLAY]->out_formats);
101 avfilter_formats_ref(inout_formats, &ctx->outputs[MAIN ]->in_formats );
106 static int config_input_main(AVFilterLink *inlink)
108 OverlayContext *over = inlink->dst->priv;
109 const AVPixFmtDescriptor *pix_desc = &av_pix_fmt_descriptors[inlink->format];
111 av_image_fill_max_pixsteps(over->max_plane_step, NULL, pix_desc);
112 over->hsub = pix_desc->log2_chroma_w;
113 over->vsub = pix_desc->log2_chroma_h;
118 static int config_input_overlay(AVFilterLink *inlink)
120 AVFilterContext *ctx = inlink->dst;
121 OverlayContext *over = inlink->dst->priv;
123 double var_values[VAR_VARS_NB], res;
126 /* Finish the configuration by evaluating the expressions
127 now when both inputs are configured. */
128 var_values[VAR_E ] = M_E;
129 var_values[VAR_PHI] = M_PHI;
130 var_values[VAR_PI ] = M_PI;
132 var_values[VAR_MAIN_W ] = var_values[VAR_MW] = ctx->inputs[MAIN ]->w;
133 var_values[VAR_MAIN_H ] = var_values[VAR_MH] = ctx->inputs[MAIN ]->h;
134 var_values[VAR_OVERLAY_W] = var_values[VAR_OW] = ctx->inputs[OVERLAY]->w;
135 var_values[VAR_OVERLAY_H] = var_values[VAR_OH] = ctx->inputs[OVERLAY]->h;
137 if ((ret = av_expr_parse_and_eval(&res, (expr = over->x_expr), var_names, var_values,
138 NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
141 if ((ret = av_expr_parse_and_eval(&res, (expr = over->y_expr), var_names, var_values,
142 NULL, NULL, NULL, NULL, NULL, 0, ctx)))
145 /* x may depend on y */
146 if ((ret = av_expr_parse_and_eval(&res, (expr = over->x_expr), var_names, var_values,
147 NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
151 av_log(ctx, AV_LOG_INFO,
152 "main w:%d h:%d fmt:%s overlay x:%d y:%d w:%d h:%d fmt:%s\n",
153 ctx->inputs[MAIN]->w, ctx->inputs[MAIN]->h,
154 av_pix_fmt_descriptors[ctx->inputs[MAIN]->format].name,
156 ctx->inputs[OVERLAY]->w, ctx->inputs[OVERLAY]->h,
157 av_pix_fmt_descriptors[ctx->inputs[OVERLAY]->format].name);
159 if (over->x < 0 || over->y < 0 ||
160 over->x + var_values[VAR_OVERLAY_W] > var_values[VAR_MAIN_W] ||
161 over->y + var_values[VAR_OVERLAY_H] > var_values[VAR_MAIN_H]) {
162 av_log(ctx, AV_LOG_ERROR,
163 "Overlay area (%d,%d)<->(%d,%d) not within the main area (0,0)<->(%d,%d) or zero-sized\n",
165 (int)(over->x + var_values[VAR_OVERLAY_W]),
166 (int)(over->y + var_values[VAR_OVERLAY_H]),
167 (int)var_values[VAR_MAIN_W], (int)var_values[VAR_MAIN_H]);
168 return AVERROR(EINVAL);
173 av_log(NULL, AV_LOG_ERROR,
174 "Error when evaluating the expression '%s'\n", expr);
178 static int config_output(AVFilterLink *outlink)
180 AVFilterContext *ctx = outlink->src;
182 // common timebase computation:
183 AVRational tb1 = ctx->inputs[MAIN ]->time_base;
184 AVRational tb2 = ctx->inputs[OVERLAY]->time_base;
185 AVRational *tb = &ctx->outputs[0]->time_base;
186 exact = av_reduce(&tb->num, &tb->den,
187 av_gcd((int64_t)tb1.num * tb2.den,
188 (int64_t)tb2.num * tb1.den),
189 (int64_t)tb1.den * tb2.den, INT_MAX);
190 av_log(ctx, AV_LOG_INFO,
191 "main_tb:%d/%d overlay_tb:%d/%d -> tb:%d/%d exact:%d\n",
192 tb1.num, tb1.den, tb2.num, tb2.den, tb->num, tb->den, exact);
194 av_log(ctx, AV_LOG_WARNING,
195 "Timestamp conversion inexact, timestamp information loss may occurr\n");
197 outlink->w = ctx->inputs[MAIN]->w;
198 outlink->h = ctx->inputs[MAIN]->h;
203 static AVFilterBufferRef *get_video_buffer(AVFilterLink *link, int perms, int w, int h)
205 return avfilter_get_video_buffer(link->dst->outputs[0], perms, w, h);
208 static void start_frame(AVFilterLink *inlink, AVFilterBufferRef *inpicref)
210 AVFilterBufferRef *outpicref = avfilter_ref_buffer(inpicref, ~0);
211 AVFilterContext *ctx = inlink->dst;
212 OverlayContext *over = ctx->priv;
214 inlink->dst->outputs[0]->out_buf = outpicref;
215 outpicref->pts = av_rescale_q(outpicref->pts, ctx->inputs[MAIN]->time_base,
216 ctx->outputs[0]->time_base);
218 if (!over->overpicref || over->overpicref->pts < outpicref->pts) {
219 AVFilterBufferRef *old = over->overpicref;
220 over->overpicref = NULL;
221 avfilter_request_frame(ctx->inputs[OVERLAY]);
222 if (over->overpicref) {
224 avfilter_unref_buffer(old);
226 over->overpicref = old;
229 avfilter_start_frame(inlink->dst->outputs[0], outpicref);
232 static void start_frame_overlay(AVFilterLink *inlink, AVFilterBufferRef *inpicref)
234 AVFilterContext *ctx = inlink->dst;
235 OverlayContext *over = ctx->priv;
237 over->overpicref = inpicref;
238 over->overpicref->pts = av_rescale_q(inpicref->pts, ctx->inputs[OVERLAY]->time_base,
239 ctx->outputs[0]->time_base);
242 static void blend_slice(AVFilterContext *ctx,
243 AVFilterBufferRef *dst, AVFilterBufferRef *src,
244 int x, int y, int w, int h,
245 int slice_y, int slice_w, int slice_h)
247 OverlayContext *over = ctx->priv;
250 int overlay_end_y = y+h;
251 int slice_end_y = slice_y+slice_h;
254 width = FFMIN(slice_w - x, w);
255 end_y = FFMIN(slice_end_y, overlay_end_y);
256 start_y = FFMAX(y, slice_y);
257 height = end_y - start_y;
259 if (dst->format == PIX_FMT_BGR24 || dst->format == PIX_FMT_RGB24) {
260 uint8_t *dp = dst->data[0] + x * 3 + start_y * dst->linesize[0];
261 uint8_t *sp = src->data[0];
262 int b = dst->format == PIX_FMT_BGR24 ? 2 : 0;
263 int r = dst->format == PIX_FMT_BGR24 ? 0 : 2;
265 sp += (slice_y - y) * src->linesize[0];
266 for (i = 0; i < height; i++) {
267 uint8_t *d = dp, *s = sp;
268 for (j = 0; j < width; j++) {
269 d[r] = (d[r] * (0xff - s[3]) + s[0] * s[3] + 128) >> 8;
270 d[1] = (d[1] * (0xff - s[3]) + s[1] * s[3] + 128) >> 8;
271 d[b] = (d[b] * (0xff - s[3]) + s[2] * s[3] + 128) >> 8;
275 dp += dst->linesize[0];
276 sp += src->linesize[0];
279 for (i = 0; i < 3; i++) {
280 int hsub = i ? over->hsub : 0;
281 int vsub = i ? over->vsub : 0;
282 uint8_t *dp = dst->data[i] + (x >> hsub) +
283 (start_y >> vsub) * dst->linesize[i];
284 uint8_t *sp = src->data[i];
285 uint8_t *ap = src->data[3];
286 int wp = FFALIGN(width, 1<<hsub) >> hsub;
287 int hp = FFALIGN(height, 1<<vsub) >> vsub;
289 sp += ((slice_y - y) >> vsub) * src->linesize[i];
290 ap += (slice_y - y) * src->linesize[3];
292 for (j = 0; j < hp; j++) {
293 uint8_t *d = dp, *s = sp, *a = ap;
294 for (k = 0; k < wp; k++) {
295 // average alpha for color components, improve quality
296 int alpha_v, alpha_h, alpha;
297 if (hsub && vsub && j+1 < hp && k+1 < wp) {
298 alpha = (a[0] + a[src->linesize[3]] +
299 a[1] + a[src->linesize[3]+1]) >> 2;
300 } else if (hsub || vsub) {
301 alpha_h = hsub && k+1 < wp ?
302 (a[0] + a[1]) >> 1 : a[0];
303 alpha_v = vsub && j+1 < hp ?
304 (a[0] + a[src->linesize[3]]) >> 1 : a[0];
305 alpha = (alpha_v + alpha_h) >> 1;
308 *d = (*d * (0xff - alpha) + *s++ * alpha + 128) >> 8;
312 dp += dst->linesize[i];
313 sp += src->linesize[i];
314 ap += (1 << vsub) * src->linesize[3];
320 static void draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir)
322 AVFilterContext *ctx = inlink->dst;
323 AVFilterLink *outlink = ctx->outputs[0];
324 AVFilterBufferRef *outpicref = outlink->out_buf;
325 OverlayContext *over = ctx->priv;
327 if (over->overpicref &&
328 !(over->x >= outpicref->video->w || over->y >= outpicref->video->h ||
329 y+h < over->y || y >= over->y + over->overpicref->video->h)) {
330 blend_slice(ctx, outpicref, over->overpicref, over->x, over->y,
331 over->overpicref->video->w, over->overpicref->video->h,
332 y, outpicref->video->w, h);
334 avfilter_draw_slice(outlink, y, h, slice_dir);
337 static void end_frame(AVFilterLink *inlink)
339 avfilter_end_frame(inlink->dst->outputs[0]);
340 avfilter_unref_buffer(inlink->cur_buf);
343 static void null_draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir) { }
345 static void null_end_frame(AVFilterLink *inlink) { }
347 AVFilter avfilter_vf_overlay = {
349 .description = NULL_IF_CONFIG_SMALL("Overlay a video source on top of the input."),
354 .priv_size = sizeof(OverlayContext),
356 .query_formats = query_formats,
358 .inputs = (AVFilterPad[]) {{ .name = "main",
359 .type = AVMEDIA_TYPE_VIDEO,
360 .start_frame = start_frame,
361 .get_video_buffer= get_video_buffer,
362 .config_props = config_input_main,
363 .draw_slice = draw_slice,
364 .end_frame = end_frame,
365 .min_perms = AV_PERM_READ,
366 .rej_perms = AV_PERM_REUSE2|AV_PERM_PRESERVE, },
368 .type = AVMEDIA_TYPE_VIDEO,
369 .start_frame = start_frame_overlay,
370 .config_props = config_input_overlay,
371 .draw_slice = null_draw_slice,
372 .end_frame = null_end_frame,
373 .min_perms = AV_PERM_READ,
374 .rej_perms = AV_PERM_REUSE2, },
376 .outputs = (AVFilterPad[]) {{ .name = "default",
377 .type = AVMEDIA_TYPE_VIDEO,
378 .config_props = config_output, },