Libav
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 "libavutil/opt.h"
38 #include "internal.h"
39 #include "video.h"
40 
41 static const char *const var_names[] = {
42  "E",
43  "PHI",
44  "PI",
45  "main_w", "W",
46  "main_h", "H",
47  "overlay_w", "w",
48  "overlay_h", "h",
49  NULL
50 };
51 
52 enum var_name {
61 };
62 
63 enum EOFAction {
67 };
68 
69 static const char *eof_action_str[] = {
70  "repeat", "endall", "pass"
71 };
72 
73 #define MAIN 0
74 #define OVERLAY 1
75 
76 typedef struct {
77  const AVClass *class;
78  int x, y;
79 
80  int max_plane_step[4];
81  int hsub, vsub;
82 
83  char *x_expr, *y_expr;
84 
85  enum EOFAction eof_action;
86 
88  AVFrame *over_prev, *over_next;
90 
91 static av_cold void uninit(AVFilterContext *ctx)
92 {
93  OverlayContext *s = ctx->priv;
94 
95  av_frame_free(&s->main);
98 }
99 
101 {
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);
106 
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 );
110 
111  return 0;
112 }
113 
114 static int config_input_main(AVFilterLink *inlink)
115 {
116  OverlayContext *s = inlink->dst->priv;
117  const AVPixFmtDescriptor *pix_desc = av_pix_fmt_desc_get(inlink->format);
118 
120  s->hsub = pix_desc->log2_chroma_w;
121  s->vsub = pix_desc->log2_chroma_h;
122 
123  return 0;
124 }
125 
127 {
128  AVFilterContext *ctx = inlink->dst;
129  OverlayContext *s = inlink->dst->priv;
130  char *expr;
131  double var_values[VAR_VARS_NB], res;
132  int ret;
133 
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;
139 
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;
144 
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)
147  goto fail;
148  s->x = res;
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)))
151  goto fail;
152  s->y = res;
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)
156  goto fail;
157  s->x = res;
158 
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,
163  s->x, s->y,
164  ctx->inputs[OVERLAY]->w, ctx->inputs[OVERLAY]->h,
167 
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",
173  s->x, s->y,
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);
178  }
179  return 0;
180 
181 fail:
183  "Error when evaluating the expression '%s'\n", expr);
184  return ret;
185 }
186 
187 static int config_output(AVFilterLink *outlink)
188 {
189  AVFilterContext *ctx = outlink->src;
190 
191  outlink->w = ctx->inputs[MAIN]->w;
192  outlink->h = ctx->inputs[MAIN]->h;
193  outlink->time_base = ctx->inputs[MAIN]->time_base;
194 
195  return 0;
196 }
197 
198 static void blend_frame(AVFilterContext *ctx,
199  AVFrame *dst, AVFrame *src,
200  int x, int y)
201 {
202  OverlayContext *s = ctx->priv;
203  int i, j, k;
204  int width, height;
205  int overlay_end_y = y + src->height;
206  int end_y, start_y;
207 
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;
212 
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;
218  if (y < 0)
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;
226  d += 3;
227  s += 4;
228  }
229  dp += dst->linesize[0];
230  sp += src->linesize[0];
231  }
232  } else {
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;
242  if (y < 0) {
243  sp += ((-y) >> vsub) * src->linesize[i];
244  ap += -y * src->linesize[3];
245  }
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;
260  } else
261  alpha = a[0];
262  *d = (*d * (0xff - alpha) + *s++ * alpha + 128) >> 8;
263  d++;
264  a += 1 << hsub;
265  }
266  dp += dst->linesize[i];
267  sp += src->linesize[i];
268  ap += (1 << vsub) * src->linesize[3];
269  }
270  }
271  }
272 }
273 
274 static int filter_frame_main(AVFilterLink *inlink, AVFrame *frame)
275 {
276  OverlayContext *s = inlink->dst->priv;
277 
278  av_assert0(!s->main);
279  s->main = frame;
280 
281  return 0;
282 }
283 
284 static int filter_frame_overlay(AVFilterLink *inlink, AVFrame *frame)
285 {
286  OverlayContext *s = inlink->dst->priv;
287 
288  av_assert0(!s->over_next);
289  s->over_next = frame;
290 
291  return 0;
292 }
293 
295 {
296  OverlayContext *s = ctx->priv;
297  AVFilterLink *outlink = ctx->outputs[0];
298  int ret = ff_filter_frame(outlink, s->main);
299  s->main = NULL;
300 
301  return ret;
302 }
303 
305 {
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)
312  return AVERROR_EOF;
313  return output_frame(ctx);
314 }
315 
316 static int request_frame(AVFilterLink *outlink)
317 {
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;
322  int ret = 0;
323 
324  /* get a frame on the main input */
325  if (!s->main) {
326  ret = ff_request_frame(ctx->inputs[MAIN]);
327  if (ret < 0)
328  return ret;
329  }
330 
331  /* get a new frame on the overlay input, on EOF check setting 'eof_action' */
332  if (!s->over_next) {
333  ret = ff_request_frame(ctx->inputs[OVERLAY]);
334  if (ret == AVERROR_EOF)
335  return handle_overlay_eof(ctx);
336  else if (ret < 0)
337  return ret;
338  }
339 
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) {
344  FFSWAP(AVFrame*, s->over_prev, s->over_next);
345 
346  ret = ff_request_frame(ctx->inputs[OVERLAY]);
347  if (ret == AVERROR_EOF)
348  return handle_overlay_eof(ctx);
349  else if (ret < 0)
350  return ret;
351  }
352 
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);
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);
361  }
362 
363  return output_frame(ctx);
364 }
365 
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" },
379  { NULL },
380 };
381 
382 static const AVClass overlay_class = {
383  .class_name = "overlay",
384  .item_name = av_default_item_name,
385  .option = options,
386  .version = LIBAVUTIL_VERSION_INT,
387 };
388 
390  {
391  .name = "main",
392  .type = AVMEDIA_TYPE_VIDEO,
393  .config_props = config_input_main,
394  .filter_frame = filter_frame_main,
395  .needs_writable = 1,
396  .needs_fifo = 1,
397  },
398  {
399  .name = "overlay",
400  .type = AVMEDIA_TYPE_VIDEO,
401  .config_props = config_input_overlay,
402  .filter_frame = filter_frame_overlay,
403  .needs_fifo = 1,
404  },
405  { NULL }
406 };
407 
409  {
410  .name = "default",
411  .type = AVMEDIA_TYPE_VIDEO,
412  .config_props = config_output,
413  .request_frame = request_frame,
414  },
415  { NULL }
416 };
417 
419  .name = "overlay",
420  .description = NULL_IF_CONFIG_SMALL("Overlay a video source on top of the input."),
421 
422  .uninit = uninit,
423 
424  .priv_size = sizeof(OverlayContext),
425  .priv_class = &overlay_class,
426 
428 
429  .inputs = avfilter_vf_overlay_inputs,
430  .outputs = avfilter_vf_overlay_outputs,
431 };
static void blend_frame(AVFilterContext *ctx, AVFrame *dst, AVFrame *src, int x, int y)
Definition: vf_overlay.c:198
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:1507
This structure describes decoded (raw) audio or video data.
Definition: frame.h:107
AVOption.
Definition: opt.h:233
static int request_frame(AVFilterLink *outlink)
Definition: vf_overlay.c:316
misc image utilities
static const AVFilterPad outputs[]
Definition: af_ashowinfo.c:122
Main libavfilter public API header.
packed RGB 8:8:8, 24bpp, RGBRGB...
Definition: pixfmt.h:67
static int handle_overlay_eof(AVFilterContext *ctx)
Definition: vf_overlay.c:304
static const AVFilterPad avfilter_vf_overlay_inputs[]
Definition: vf_overlay.c:389
AVFrame * main
Definition: vf_overlay.c:87
uint8_t log2_chroma_w
Amount to shift the luma width right to find the chroma width.
Definition: pixdesc.h:68
#define FFALIGN(x, a)
Definition: common.h:62
static int filter_frame_main(AVFilterLink *inlink, AVFrame *frame)
Definition: vf_overlay.c:274
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
const char * name
Pad name.
Definition: internal.h:42
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:38
AVFilterLink ** inputs
array of pointers to input links
Definition: avfilter.h:571
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:728
char * x_expr
Definition: vf_overlay.c:83
planar YUV 4:2:0, 20bpp, (1 Cr & Cb sample per 2x2 Y & A samples)
Definition: pixfmt.h:104
uint8_t
#define av_cold
Definition: attributes.h:66
AVOptions.
static const char *const var_names[]
Definition: vf_overlay.c:41
#define b
Definition: input.c:52
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:183
#define AVERROR_EOF
End of file.
Definition: error.h:51
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:139
#define OVERLAY
Definition: vf_overlay.c:74
#define r
Definition: input.c:51
A filter pad used for either input or output.
Definition: internal.h:36
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:551
int width
width and height of the video frame
Definition: frame.h:146
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:123
uint8_t log2_chroma_h
Amount to shift the luma height right to find the chroma height.
Definition: pixdesc.h:77
#define AVERROR(e)
Definition: error.h:43
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:55
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:142
void * priv
private data for use by the filter
Definition: avfilter.h:584
static int config_input_overlay(AVFilterLink *inlink)
Definition: vf_overlay.c:126
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:148
#define FFMAX(a, b)
Definition: common.h:55
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:294
AVFrame * over_next
Definition: vf_overlay.c:88
static const AVFilterPad avfilter_vf_overlay_outputs[]
Definition: vf_overlay.c:408
#define FFMIN(a, b)
Definition: common.h:57
#define M_E
Definition: ratecontrol.c:38
packed RGB 8:8:8, 24bpp, BGRBGR...
Definition: pixfmt.h:68
static const AVClass overlay_class
Definition: vf_overlay.c:382
LIBAVUTIL_VERSION_INT
Definition: eval.c:55
char * y_expr
Definition: vf_overlay.c:83
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames...
Definition: frame.h:158
NULL
Definition: eval.c:55
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:91
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:125
av_default_item_name
Definition: dnxhdenc.c:45
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:57
int max_plane_step[4]
steps per pixel for each plane
Definition: vf_overlay.c:80
#define OFFSET(x)
Definition: vf_overlay.c:366
int y
position of overlayed picture
Definition: vf_overlay.c:78
Describe the class of an AVClass context structure.
Definition: log.h:33
Filter definition.
Definition: avfilter.h:421
static const AVFilterPad inputs[]
Definition: af_ashowinfo.c:111
#define FLAGS
Definition: vf_overlay.c:367
rational number numerator/denominator
Definition: rational.h:43
static const char * eof_action_str[]
Definition: vf_overlay.c:69
#define M_PHI
Definition: mathematics.h:34
const char * name
Filter name.
Definition: avfilter.h:425
static int query_formats(AVFilterContext *ctx)
Definition: vf_overlay.c:100
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:578
static const AVOption options[]
Definition: vf_overlay.c:368
AVFrame * over_prev
Definition: vf_overlay.c:88
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:113
int height
Definition: gxfenc.c:72
static int config_input_main(AVFilterLink *inlink)
Definition: vf_overlay.c:114
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:81
AVFilter ff_vf_overlay
Definition: vf_overlay.c:418
static int config_output(AVFilterLink *outlink)
Definition: vf_overlay.c:187
static int filter_frame_overlay(AVFilterLink *inlink, AVFrame *frame)
Definition: vf_overlay.c:284
A list of supported formats for one end of a filter link.
Definition: formats.h:64
An instance of a filter.
Definition: avfilter.h:563
int height
Definition: frame.h:146
#define MAIN
Definition: vf_overlay.c:73
int ff_request_frame(AVFilterLink *link)
Request an input frame from the filter at the other end of the link.
Definition: avfilter.c:244
#define FFSWAP(type, a, b)
Definition: common.h:60
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:1449
internal API functions
EOFAction
Definition: vf_overlay.c:63
AVPixelFormat
Pixel format.
Definition: pixfmt.h:63
var_name
Definition: setpts.c:58
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:228
simple arithmetic expression evaluator
enum EOFAction eof_action
action to take on EOF from source
Definition: vf_overlay.c:85