vf_overlay.c
Go to the documentation of this file.
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 Libav.
7  *
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.
12  *
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.
17  *
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
21  */
22 
28 #include "avfilter.h"
29 #include "formats.h"
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 "internal.h"
38 #include "video.h"
39 
40 static const char *const var_names[] = {
41  "E",
42  "PHI",
43  "PI",
44  "main_w", "W",
45  "main_h", "H",
46  "overlay_w", "w",
47  "overlay_h", "h",
48  NULL
49 };
50 
51 enum var_name {
60 };
61 
62 #define MAIN 0
63 #define OVERLAY 1
64 
65 typedef struct {
66  int x, y;
67 
68  int max_plane_step[4];
69  int hsub, vsub;
70 
71  char x_expr[256], y_expr[256];
72 
76 
77 static av_cold int init(AVFilterContext *ctx, const char *args)
78 {
79  OverlayContext *over = ctx->priv;
80 
81  av_strlcpy(over->x_expr, "0", sizeof(over->x_expr));
82  av_strlcpy(over->y_expr, "0", sizeof(over->y_expr));
83 
84  if (args)
85  sscanf(args, "%255[^:]:%255[^:]", over->x_expr, over->y_expr);
86 
87  return 0;
88 }
89 
90 static av_cold void uninit(AVFilterContext *ctx)
91 {
92  OverlayContext *s = ctx->priv;
93 
97 }
98 
100 {
101  const enum AVPixelFormat inout_pix_fmts[] = { AV_PIX_FMT_YUV420P, AV_PIX_FMT_NONE };
102  const enum AVPixelFormat blend_pix_fmts[] = { AV_PIX_FMT_YUVA420P, AV_PIX_FMT_NONE };
103  AVFilterFormats *inout_formats = ff_make_format_list(inout_pix_fmts);
104  AVFilterFormats *blend_formats = ff_make_format_list(blend_pix_fmts);
105 
106  ff_formats_ref(inout_formats, &ctx->inputs [MAIN ]->out_formats);
107  ff_formats_ref(blend_formats, &ctx->inputs [OVERLAY]->out_formats);
108  ff_formats_ref(inout_formats, &ctx->outputs[MAIN ]->in_formats );
109 
110  return 0;
111 }
112 
113 static int config_input_main(AVFilterLink *inlink)
114 {
115  OverlayContext *over = inlink->dst->priv;
116  const AVPixFmtDescriptor *pix_desc = av_pix_fmt_desc_get(inlink->format);
117 
119  over->hsub = pix_desc->log2_chroma_w;
120  over->vsub = pix_desc->log2_chroma_h;
121 
122  return 0;
123 }
124 
126 {
127  AVFilterContext *ctx = inlink->dst;
128  OverlayContext *over = inlink->dst->priv;
129  char *expr;
130  double var_values[VAR_VARS_NB], res;
131  int ret;
132 
133  /* Finish the configuration by evaluating the expressions
134  now when both inputs are configured. */
135  var_values[VAR_E ] = M_E;
136  var_values[VAR_PHI] = M_PHI;
137  var_values[VAR_PI ] = M_PI;
138 
139  var_values[VAR_MAIN_W ] = var_values[VAR_MW] = ctx->inputs[MAIN ]->w;
140  var_values[VAR_MAIN_H ] = var_values[VAR_MH] = ctx->inputs[MAIN ]->h;
141  var_values[VAR_OVERLAY_W] = var_values[VAR_OW] = ctx->inputs[OVERLAY]->w;
142  var_values[VAR_OVERLAY_H] = var_values[VAR_OH] = ctx->inputs[OVERLAY]->h;
143 
144  if ((ret = av_expr_parse_and_eval(&res, (expr = over->x_expr), var_names, var_values,
145  NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
146  goto fail;
147  over->x = res;
148  if ((ret = av_expr_parse_and_eval(&res, (expr = over->y_expr), var_names, var_values,
149  NULL, NULL, NULL, NULL, NULL, 0, ctx)))
150  goto fail;
151  over->y = res;
152  /* x may depend on y */
153  if ((ret = av_expr_parse_and_eval(&res, (expr = over->x_expr), var_names, var_values,
154  NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
155  goto fail;
156  over->x = res;
157 
158  av_log(ctx, AV_LOG_VERBOSE,
159  "main w:%d h:%d fmt:%s overlay x:%d y:%d w:%d h:%d fmt:%s\n",
160  ctx->inputs[MAIN]->w, ctx->inputs[MAIN]->h,
162  over->x, over->y,
163  ctx->inputs[OVERLAY]->w, ctx->inputs[OVERLAY]->h,
165 
166  if (over->x < 0 || over->y < 0 ||
167  over->x + var_values[VAR_OVERLAY_W] > var_values[VAR_MAIN_W] ||
168  over->y + var_values[VAR_OVERLAY_H] > var_values[VAR_MAIN_H]) {
169  av_log(ctx, AV_LOG_ERROR,
170  "Overlay area (%d,%d)<->(%d,%d) not within the main area (0,0)<->(%d,%d) or zero-sized\n",
171  over->x, over->y,
172  (int)(over->x + var_values[VAR_OVERLAY_W]),
173  (int)(over->y + var_values[VAR_OVERLAY_H]),
174  (int)var_values[VAR_MAIN_W], (int)var_values[VAR_MAIN_H]);
175  return AVERROR(EINVAL);
176  }
177  return 0;
178 
179 fail:
181  "Error when evaluating the expression '%s'\n", expr);
182  return ret;
183 }
184 
185 static int config_output(AVFilterLink *outlink)
186 {
187  AVFilterContext *ctx = outlink->src;
188 
189  outlink->w = ctx->inputs[MAIN]->w;
190  outlink->h = ctx->inputs[MAIN]->h;
191  outlink->time_base = ctx->inputs[MAIN]->time_base;
192 
193  return 0;
194 }
195 
196 static void blend_frame(AVFilterContext *ctx,
198  int x, int y)
199 {
200  OverlayContext *over = ctx->priv;
201  int i, j, k;
202  int width, height;
203  int overlay_end_y = y + src->video->h;
204  int end_y, start_y;
205 
206  width = FFMIN(dst->video->w - x, src->video->w);
207  end_y = FFMIN(dst->video->h, overlay_end_y);
208  start_y = FFMAX(y, 0);
209  height = end_y - start_y;
210 
211  if (dst->format == AV_PIX_FMT_BGR24 || dst->format == AV_PIX_FMT_RGB24) {
212  uint8_t *dp = dst->data[0] + x * 3 + start_y * dst->linesize[0];
213  uint8_t *sp = src->data[0];
214  int b = dst->format == AV_PIX_FMT_BGR24 ? 2 : 0;
215  int r = dst->format == AV_PIX_FMT_BGR24 ? 0 : 2;
216  if (y < 0)
217  sp += -y * src->linesize[0];
218  for (i = 0; i < height; i++) {
219  uint8_t *d = dp, *s = sp;
220  for (j = 0; j < width; j++) {
221  d[r] = (d[r] * (0xff - s[3]) + s[0] * s[3] + 128) >> 8;
222  d[1] = (d[1] * (0xff - s[3]) + s[1] * s[3] + 128) >> 8;
223  d[b] = (d[b] * (0xff - s[3]) + s[2] * s[3] + 128) >> 8;
224  d += 3;
225  s += 4;
226  }
227  dp += dst->linesize[0];
228  sp += src->linesize[0];
229  }
230  } else {
231  for (i = 0; i < 3; i++) {
232  int hsub = i ? over->hsub : 0;
233  int vsub = i ? over->vsub : 0;
234  uint8_t *dp = dst->data[i] + (x >> hsub) +
235  (start_y >> vsub) * dst->linesize[i];
236  uint8_t *sp = src->data[i];
237  uint8_t *ap = src->data[3];
238  int wp = FFALIGN(width, 1<<hsub) >> hsub;
239  int hp = FFALIGN(height, 1<<vsub) >> vsub;
240  if (y < 0) {
241  sp += ((-y) >> vsub) * src->linesize[i];
242  ap += -y * src->linesize[3];
243  }
244  for (j = 0; j < hp; j++) {
245  uint8_t *d = dp, *s = sp, *a = ap;
246  for (k = 0; k < wp; k++) {
247  // average alpha for color components, improve quality
248  int alpha_v, alpha_h, alpha;
249  if (hsub && vsub && j+1 < hp && k+1 < wp) {
250  alpha = (a[0] + a[src->linesize[3]] +
251  a[1] + a[src->linesize[3]+1]) >> 2;
252  } else if (hsub || vsub) {
253  alpha_h = hsub && k+1 < wp ?
254  (a[0] + a[1]) >> 1 : a[0];
255  alpha_v = vsub && j+1 < hp ?
256  (a[0] + a[src->linesize[3]]) >> 1 : a[0];
257  alpha = (alpha_v + alpha_h) >> 1;
258  } else
259  alpha = a[0];
260  *d = (*d * (0xff - alpha) + *s++ * alpha + 128) >> 8;
261  d++;
262  a += 1 << hsub;
263  }
264  dp += dst->linesize[i];
265  sp += src->linesize[i];
266  ap += (1 << vsub) * src->linesize[3];
267  }
268  }
269  }
270 }
271 
273 {
274  OverlayContext *s = inlink->dst->priv;
275 
276  av_assert0(!s->main);
277  s->main = frame;
278 
279  return 0;
280 }
281 
283 {
284  OverlayContext *s = inlink->dst->priv;
285 
286  av_assert0(!s->over_next);
287  s->over_next = frame;
288 
289  return 0;
290 }
291 
293 {
294  OverlayContext *s = ctx->priv;
295  AVFilterLink *outlink = ctx->outputs[0];
296  int ret = ff_filter_frame(outlink, s->main);
297  s->main = NULL;
298 
299  return ret;
300 }
301 
303 {
304  OverlayContext *s = ctx->priv;
305  if (s->over_prev)
306  blend_frame(ctx, s->main, s->over_prev, s->x, s->y);
307  return output_frame(ctx);
308 }
309 
310 static int request_frame(AVFilterLink *outlink)
311 {
312  AVFilterContext *ctx = outlink->src;
313  OverlayContext *s = ctx->priv;
314  AVRational tb_main = ctx->inputs[MAIN]->time_base;
315  AVRational tb_over = ctx->inputs[OVERLAY]->time_base;
316  int ret = 0;
317 
318  /* get a frame on the main input */
319  if (!s->main) {
320  ret = ff_request_frame(ctx->inputs[MAIN]);
321  if (ret < 0)
322  return ret;
323  }
324 
325  /* get a new frame on the overlay input, on EOF
326  * reuse previous */
327  if (!s->over_next) {
328  ret = ff_request_frame(ctx->inputs[OVERLAY]);
329  if (ret == AVERROR_EOF)
330  return handle_overlay_eof(ctx);
331  else if (ret < 0)
332  return ret;
333  }
334 
335  while (s->main->pts != AV_NOPTS_VALUE &&
336  s->over_next->pts != AV_NOPTS_VALUE &&
337  av_compare_ts(s->over_next->pts, tb_over, s->main->pts, tb_main) < 0) {
340 
341  ret = ff_request_frame(ctx->inputs[OVERLAY]);
342  if (ret == AVERROR_EOF)
343  return handle_overlay_eof(ctx);
344  else if (ret < 0)
345  return ret;
346  }
347 
348  if (s->main->pts == AV_NOPTS_VALUE ||
349  s->over_next->pts == AV_NOPTS_VALUE ||
350  !av_compare_ts(s->over_next->pts, tb_over, s->main->pts, tb_main)) {
351  blend_frame(ctx, s->main, s->over_next, s->x, s->y);
354  } else if (s->over_prev) {
355  blend_frame(ctx, s->main, s->over_prev, s->x, s->y);
356  }
357 
358  return output_frame(ctx);
359 }
360 
362  {
363  .name = "main",
364  .type = AVMEDIA_TYPE_VIDEO,
365  .config_props = config_input_main,
366  .filter_frame = filter_frame_main,
367  .min_perms = AV_PERM_READ,
368  .rej_perms = AV_PERM_REUSE2 | AV_PERM_PRESERVE,
369  .needs_fifo = 1,
370  },
371  {
372  .name = "overlay",
373  .type = AVMEDIA_TYPE_VIDEO,
374  .config_props = config_input_overlay,
375  .filter_frame = filter_frame_overlay,
376  .min_perms = AV_PERM_READ,
377  .rej_perms = AV_PERM_REUSE2,
378  .needs_fifo = 1,
379  },
380  { NULL }
381 };
382 
384  {
385  .name = "default",
386  .type = AVMEDIA_TYPE_VIDEO,
387  .config_props = config_output,
388  .request_frame = request_frame,
389  },
390  { NULL }
391 };
392 
394  .name = "overlay",
395  .description = NULL_IF_CONFIG_SMALL("Overlay a video source on top of the input."),
396 
397  .init = init,
398  .uninit = uninit,
399 
400  .priv_size = sizeof(OverlayContext),
401 
403 
404  .inputs = avfilter_vf_overlay_inputs,
405  .outputs = avfilter_vf_overlay_outputs,
406 };
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:1435
char y_expr[256]
Definition: vf_overlay.c:71
static int request_frame(AVFilterLink *outlink)
Definition: vf_overlay.c:310
AVFilterBufferRefVideoProps * video
video buffer specific properties
Definition: avfilter.h:159
int linesize[8]
number of bytes per line
Definition: avfilter.h:157
static int filter_frame_main(AVFilterLink *inlink, AVFilterBufferRef *frame)
Definition: vf_overlay.c:272
misc image utilities
static const AVFilterPad outputs[]
Definition: af_ashowinfo.c:122
packed RGB 8:8:8, 24bpp, RGBRGB...
Definition: pixfmt.h:67
int ff_filter_frame(AVFilterLink *link, AVFilterBufferRef *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:459
static int handle_overlay_eof(AVFilterContext *ctx)
Definition: vf_overlay.c:302
static const AVFilterPad avfilter_vf_overlay_inputs[]
Definition: vf_overlay.c:361
uint8_t log2_chroma_w
Amount to shift the luma width right to find the chroma width.
Definition: pixdesc.h:66
char x_expr[256]
Definition: vf_overlay.c:71
AVFilterFormats * ff_make_format_list(const int *fmts)
Create a list of supported formats.
Definition: formats.c:165
void av_image_fill_max_pixsteps(int max_pixsteps[4], int max_pixstep_comps[4], const AVPixFmtDescriptor *pixdesc)
Compute the max pixel step for each plane of an image with a format described by pixdesc.
Definition: imgutils.c:30
#define AV_PERM_READ
can read from the buffer
Definition: avfilter.h:97
const char * name
Pad name.
Definition: internal.h:39
AVFilterLink ** inputs
array of pointers to input links
Definition: avfilter.h:426
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
AVFilterBufferRef * main
Definition: vf_overlay.c:73
planar YUV 4:2:0, 20bpp, (1 Cr & Cb sample per 2x2 Y & A samples)
Definition: pixfmt.h:102
uint8_t
static const char *const var_names[]
Definition: vf_overlay.c:40
AVFilterBufferRef * over_prev
Definition: vf_overlay.c:74
#define b
Definition: input.c:52
void avfilter_unref_bufferp(AVFilterBufferRef **ref)
Remove a reference to a buffer and set the pointer to NULL.
Definition: buffer.c:88
#define sp
Definition: regdef.h:63
var_name
Definition: vf_boxblur.c:47
#define OVERLAY
Definition: vf_overlay.c:63
#define r
Definition: input.c:51
int64_t pts
presentation timestamp.
Definition: avfilter.h:167
A filter pad used for either input or output.
Definition: internal.h:33
int av_expr_parse_and_eval(double *d, const char *s, const char *const *const_names, const double *const_values, const char *const *func1_names, double(*const *funcs1)(void *, double), const char *const *func2_names, double(*const *funcs2)(void *, double, double), void *opaque, int log_offset, void *log_ctx)
Parse and evaluate an expression.
Definition: eval.c:548
int h
image height
Definition: avfilter.h:123
uint8_t log2_chroma_h
Amount to shift the luma height right to find the chroma height.
Definition: pixdesc.h:75
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:88
void * priv
private data for use by the filter
Definition: avfilter.h:439
static int config_input_overlay(AVFilterLink *inlink)
Definition: vf_overlay.c:125
simple assert() macros that are a bit more flexible than ISO C assert().
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:146
AVFilterBufferRef * over_next
Definition: vf_overlay.c:74
size_t av_strlcpy(char *dst, const char *src, size_t size)
Copy the string src to dst, but no more than size - 1 bytes, and null-terminate dst.
Definition: avstring.c:67
int av_compare_ts(int64_t ts_a, AVRational tb_a, int64_t ts_b, AVRational tb_b)
Compare 2 timestamps each in its own timebases.
Definition: mathematics.c:127
static int output_frame(AVFilterContext *ctx)
Definition: vf_overlay.c:292
AVFilter avfilter_vf_overlay
Definition: vf_overlay.c:393
static const AVFilterPad avfilter_vf_overlay_outputs[]
Definition: vf_overlay.c:383
static av_cold int init(AVFilterContext *ctx, const char *args)
Definition: vf_overlay.c:77
#define M_E
Definition: ratecontrol.c:39
packed RGB 8:8:8, 24bpp, BGRBGR...
Definition: pixfmt.h:68
A reference to an AVFilterBuffer.
Definition: avfilter.h:139
NULL
Definition: eval.c:52
static int width
Definition: utils.c:156
void ff_formats_ref(AVFilterFormats *f, AVFilterFormats **ref)
Add *ref as a new reference to formats.
Definition: formats.c:262
static av_cold void uninit(AVFilterContext *ctx)
Definition: vf_overlay.c:90
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:55
int max_plane_step[4]
steps per pixel for each plane
Definition: vf_overlay.c:68
int y
position of overlayed picture
Definition: vf_overlay.c:66
Filter definition.
Definition: avfilter.h:371
static const AVFilterPad inputs[]
Definition: af_ashowinfo.c:110
rational number numerator/denominator
Definition: rational.h:43
#define M_PHI
Definition: mathematics.h:34
const char * name
filter name
Definition: avfilter.h:372
static int query_formats(AVFilterContext *ctx)
Definition: vf_overlay.c:99
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:433
#define AV_PERM_PRESERVE
nobody else can overwrite the buffer
Definition: avfilter.h:99
int height
Definition: gxfenc.c:72
static int config_input_main(AVFilterLink *inlink)
Definition: vf_overlay.c:113
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:65
common internal and external API header
int vsub
chroma subsampling values
Definition: vf_overlay.c:69
#define AV_PERM_REUSE2
can output the buffer multiple times, modified each time
Definition: avfilter.h:101
static void blend_frame(AVFilterContext *ctx, AVFilterBufferRef *dst, AVFilterBufferRef *src, int x, int y)
Definition: vf_overlay.c:196
static int config_output(AVFilterLink *outlink)
Definition: vf_overlay.c:185
uint8_t * data[8]
picture/audio data for each plane
Definition: avfilter.h:141
int format
media format
Definition: avfilter.h:170
A list of supported formats for one end of a filter link.
Definition: formats.h:64
An instance of a filter.
Definition: avfilter.h:418
#define MAIN
Definition: vf_overlay.c:62
int ff_request_frame(AVFilterLink *link)
Request an input frame from the filter at the other end of the link.
Definition: avfilter.c:239
const char * av_get_pix_fmt_name(enum AVPixelFormat pix_fmt)
Return the short name for a pixel format, NULL in case pix_fmt is unknown.
Definition: pixdesc.c:1377
internal API functions
static int filter_frame_overlay(AVFilterLink *inlink, AVFilterBufferRef *frame)
Definition: vf_overlay.c:282
AVPixelFormat
Pixel format.
Definition: pixfmt.h:63
simple arithmetic expression evaluator