FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
vf_pad.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2008 vmrsss
3  * Copyright (c) 2009 Stefano Sabatini
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
22 /**
23  * @file
24  * video padding filter
25  */
26 
27 #include "avfilter.h"
28 #include "formats.h"
29 #include "internal.h"
30 #include "video.h"
31 #include "libavutil/avstring.h"
32 #include "libavutil/common.h"
33 #include "libavutil/eval.h"
34 #include "libavutil/pixdesc.h"
35 #include "libavutil/colorspace.h"
36 #include "libavutil/avassert.h"
37 #include "libavutil/imgutils.h"
38 #include "libavutil/opt.h"
39 #include "libavutil/parseutils.h"
40 #include "libavutil/mathematics.h"
41 #include "libavutil/opt.h"
42 
43 #include "drawutils.h"
44 
45 static const char *const var_names[] = {
46  "in_w", "iw",
47  "in_h", "ih",
48  "out_w", "ow",
49  "out_h", "oh",
50  "x",
51  "y",
52  "a",
53  "sar",
54  "dar",
55  "hsub",
56  "vsub",
57  NULL
58 };
59 
60 enum var_name {
73 };
74 
76 {
78  return 0;
79 }
80 
81 typedef struct {
82  const AVClass *class;
83  int w, h; ///< output dimensions, a value of 0 will result in the input size
84  int x, y; ///< offsets of the input area with respect to the padded area
85  int in_w, in_h; ///< width and height for the padded input video, which has to be aligned to the chroma values in order to avoid chroma issues
86 
87  char *w_expr; ///< width expression string
88  char *h_expr; ///< height expression string
89  char *x_expr; ///< width expression string
90  char *y_expr; ///< height expression string
91  uint8_t rgba_color[4]; ///< color for the padding area
94 } PadContext;
95 
96 static int config_input(AVFilterLink *inlink)
97 {
98  AVFilterContext *ctx = inlink->dst;
99  PadContext *s = ctx->priv;
100  int ret;
101  double var_values[VARS_NB], res;
102  char *expr;
103 
104  ff_draw_init(&s->draw, inlink->format, 0);
105  ff_draw_color(&s->draw, &s->color, s->rgba_color);
106 
107  var_values[VAR_IN_W] = var_values[VAR_IW] = inlink->w;
108  var_values[VAR_IN_H] = var_values[VAR_IH] = inlink->h;
109  var_values[VAR_OUT_W] = var_values[VAR_OW] = NAN;
110  var_values[VAR_OUT_H] = var_values[VAR_OH] = NAN;
111  var_values[VAR_A] = (double) inlink->w / inlink->h;
112  var_values[VAR_SAR] = inlink->sample_aspect_ratio.num ?
113  (double) inlink->sample_aspect_ratio.num / inlink->sample_aspect_ratio.den : 1;
114  var_values[VAR_DAR] = var_values[VAR_A] * var_values[VAR_SAR];
115  var_values[VAR_HSUB] = 1 << s->draw.hsub_max;
116  var_values[VAR_VSUB] = 1 << s->draw.vsub_max;
117 
118  /* evaluate width and height */
119  av_expr_parse_and_eval(&res, (expr = s->w_expr),
120  var_names, var_values,
121  NULL, NULL, NULL, NULL, NULL, 0, ctx);
122  s->w = var_values[VAR_OUT_W] = var_values[VAR_OW] = res;
123  if ((ret = av_expr_parse_and_eval(&res, (expr = s->h_expr),
124  var_names, var_values,
125  NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
126  goto eval_fail;
127  s->h = var_values[VAR_OUT_H] = var_values[VAR_OH] = res;
128  /* evaluate the width again, as it may depend on the evaluated output height */
129  if ((ret = av_expr_parse_and_eval(&res, (expr = s->w_expr),
130  var_names, var_values,
131  NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
132  goto eval_fail;
133  s->w = var_values[VAR_OUT_W] = var_values[VAR_OW] = res;
134 
135  /* evaluate x and y */
136  av_expr_parse_and_eval(&res, (expr = s->x_expr),
137  var_names, var_values,
138  NULL, NULL, NULL, NULL, NULL, 0, ctx);
139  s->x = var_values[VAR_X] = res;
140  if ((ret = av_expr_parse_and_eval(&res, (expr = s->y_expr),
141  var_names, var_values,
142  NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
143  goto eval_fail;
144  s->y = var_values[VAR_Y] = res;
145  /* evaluate x again, as it may depend on the evaluated y value */
146  if ((ret = av_expr_parse_and_eval(&res, (expr = s->x_expr),
147  var_names, var_values,
148  NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
149  goto eval_fail;
150  s->x = var_values[VAR_X] = res;
151 
152  /* sanity check params */
153  if (s->w < 0 || s->h < 0 || s->x < 0 || s->y < 0) {
154  av_log(ctx, AV_LOG_ERROR, "Negative values are not acceptable.\n");
155  return AVERROR(EINVAL);
156  }
157 
158  if (!s->w)
159  s->w = inlink->w;
160  if (!s->h)
161  s->h = inlink->h;
162 
163  s->w = ff_draw_round_to_sub(&s->draw, 0, -1, s->w);
164  s->h = ff_draw_round_to_sub(&s->draw, 1, -1, s->h);
165  s->x = ff_draw_round_to_sub(&s->draw, 0, -1, s->x);
166  s->y = ff_draw_round_to_sub(&s->draw, 1, -1, s->y);
167  s->in_w = ff_draw_round_to_sub(&s->draw, 0, -1, inlink->w);
168  s->in_h = ff_draw_round_to_sub(&s->draw, 1, -1, inlink->h);
169 
170  av_log(ctx, AV_LOG_VERBOSE, "w:%d h:%d -> w:%d h:%d x:%d y:%d color:0x%02X%02X%02X%02X\n",
171  inlink->w, inlink->h, s->w, s->h, s->x, s->y,
172  s->rgba_color[0], s->rgba_color[1], s->rgba_color[2], s->rgba_color[3]);
173 
174  if (s->x < 0 || s->y < 0 ||
175  s->w <= 0 || s->h <= 0 ||
176  (unsigned)s->x + (unsigned)inlink->w > s->w ||
177  (unsigned)s->y + (unsigned)inlink->h > s->h) {
178  av_log(ctx, AV_LOG_ERROR,
179  "Input area %d:%d:%d:%d not within the padded area 0:0:%d:%d or zero-sized\n",
180  s->x, s->y, s->x + inlink->w, s->y + inlink->h, s->w, s->h);
181  return AVERROR(EINVAL);
182  }
183 
184  return 0;
185 
186 eval_fail:
188  "Error when evaluating the expression '%s'\n", expr);
189  return ret;
190 
191 }
192 
193 static int config_output(AVFilterLink *outlink)
194 {
195  PadContext *s = outlink->src->priv;
196 
197  outlink->w = s->w;
198  outlink->h = s->h;
199  return 0;
200 }
201 
202 static AVFrame *get_video_buffer(AVFilterLink *inlink, int w, int h)
203 {
204  PadContext *s = inlink->dst->priv;
205 
207  w + (s->w - s->in_w),
208  h + (s->h - s->in_h));
209  int plane;
210 
211  if (!frame)
212  return NULL;
213 
214  frame->width = w;
215  frame->height = h;
216 
217  for (plane = 0; plane < 4 && frame->data[plane]; plane++) {
218  int hsub = s->draw.hsub[plane];
219  int vsub = s->draw.vsub[plane];
220  frame->data[plane] += (s->x >> hsub) * s->draw.pixelstep[plane] +
221  (s->y >> vsub) * frame->linesize[plane];
222  }
223 
224  return frame;
225 }
226 
227 /* check whether each plane in this buffer can be padded without copying */
229 {
230  int planes[4] = { -1, -1, -1, -1}, *p = planes;
231  int i, j;
232 
233  /* get all planes in this buffer */
234  for (i = 0; i < FF_ARRAY_ELEMS(planes) && frame->data[i]; i++) {
235  if (av_frame_get_plane_buffer(frame, i) == buf)
236  *p++ = i;
237  }
238 
239  /* for each plane in this buffer, check that it can be padded without
240  * going over buffer bounds or other planes */
241  for (i = 0; i < FF_ARRAY_ELEMS(planes) && planes[i] >= 0; i++) {
242  int hsub = s->draw.hsub[planes[i]];
243  int vsub = s->draw.vsub[planes[i]];
244 
245  uint8_t *start = frame->data[planes[i]];
246  uint8_t *end = start + (frame->height >> vsub) *
247  frame->linesize[planes[i]];
248 
249  /* amount of free space needed before the start and after the end
250  * of the plane */
251  ptrdiff_t req_start = (s->x >> hsub) * s->draw.pixelstep[planes[i]] +
252  (s->y >> vsub) * frame->linesize[planes[i]];
253  ptrdiff_t req_end = ((s->w - s->x - frame->width) >> hsub) *
254  s->draw.pixelstep[planes[i]] +
255  (s->y >> vsub) * frame->linesize[planes[i]];
256 
257  if (frame->linesize[planes[i]] < (s->w >> hsub) * s->draw.pixelstep[planes[i]])
258  return 1;
259  if (start - buf->data < req_start ||
260  (buf->data + buf->size) - end < req_end)
261  return 1;
262 
263 #define SIGN(x) ((x) > 0 ? 1 : -1)
264  for (j = 0; j < FF_ARRAY_ELEMS(planes) && planes[j] >= 0; j++) {
265  int vsub1 = s->draw.vsub[planes[j]];
266  uint8_t *start1 = frame->data[planes[j]];
267  uint8_t *end1 = start1 + (frame->height >> vsub1) *
268  frame->linesize[planes[j]];
269  if (i == j)
270  continue;
271 
272  if (SIGN(start - end1) != SIGN(start - end1 - req_start) ||
273  SIGN(end - start1) != SIGN(end - start1 + req_end))
274  return 1;
275  }
276  }
277 
278  return 0;
279 }
280 
282 {
283  int i;
284 
285  if (!av_frame_is_writable(frame))
286  return 1;
287 
288  for (i = 0; i < 4 && frame->buf[i]; i++)
289  if (buffer_needs_copy(s, frame, frame->buf[i]))
290  return 1;
291  return 0;
292 }
293 
294 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
295 {
296  PadContext *s = inlink->dst->priv;
297  AVFrame *out;
298  int needs_copy = frame_needs_copy(s, in);
299 
300  if (needs_copy) {
301  av_log(inlink->dst, AV_LOG_DEBUG, "Direct padding impossible allocating new frame\n");
302  out = ff_get_video_buffer(inlink->dst->outputs[0],
303  FFMAX(inlink->w, s->w),
304  FFMAX(inlink->h, s->h));
305  if (!out) {
306  av_frame_free(&in);
307  return AVERROR(ENOMEM);
308  }
309 
310  av_frame_copy_props(out, in);
311  } else {
312  int i;
313 
314  out = in;
315  for (i = 0; i < 4 && out->data[i]; i++) {
316  int hsub = s->draw.hsub[i];
317  int vsub = s->draw.vsub[i];
318  out->data[i] -= (s->x >> hsub) * s->draw.pixelstep[i] +
319  (s->y >> vsub) * out->linesize[i];
320  }
321  }
322 
323  /* top bar */
324  if (s->y) {
325  ff_fill_rectangle(&s->draw, &s->color,
326  out->data, out->linesize,
327  0, 0, s->w, s->y);
328  }
329 
330  /* bottom bar */
331  if (s->h > s->y + s->in_h) {
332  ff_fill_rectangle(&s->draw, &s->color,
333  out->data, out->linesize,
334  0, s->y + s->in_h, s->w, s->h - s->y - s->in_h);
335  }
336 
337  /* left border */
338  ff_fill_rectangle(&s->draw, &s->color, out->data, out->linesize,
339  0, s->y, s->x, in->height);
340 
341  if (needs_copy) {
343  out->data, out->linesize, in->data, in->linesize,
344  s->x, s->y, 0, 0, in->width, in->height);
345  }
346 
347  /* right border */
348  ff_fill_rectangle(&s->draw, &s->color, out->data, out->linesize,
349  s->x + s->in_w, s->y, s->w - s->x - s->in_w,
350  in->height);
351 
352  out->width = s->w;
353  out->height = s->h;
354 
355  if (in != out)
356  av_frame_free(&in);
357  return ff_filter_frame(inlink->dst->outputs[0], out);
358 }
359 
360 #define OFFSET(x) offsetof(PadContext, x)
361 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
362 
363 static const AVOption pad_options[] = {
364  { "width", "set the pad area width expression", OFFSET(w_expr), AV_OPT_TYPE_STRING, {.str = "iw"}, CHAR_MIN, CHAR_MAX, FLAGS },
365  { "w", "set the pad area width expression", OFFSET(w_expr), AV_OPT_TYPE_STRING, {.str = "iw"}, CHAR_MIN, CHAR_MAX, FLAGS },
366  { "height", "set the pad area height expression", OFFSET(h_expr), AV_OPT_TYPE_STRING, {.str = "ih"}, CHAR_MIN, CHAR_MAX, FLAGS },
367  { "h", "set the pad area height expression", OFFSET(h_expr), AV_OPT_TYPE_STRING, {.str = "ih"}, CHAR_MIN, CHAR_MAX, FLAGS },
368  { "x", "set the x offset expression for the input image position", OFFSET(x_expr), AV_OPT_TYPE_STRING, {.str = "0"}, CHAR_MIN, CHAR_MAX, FLAGS },
369  { "y", "set the y offset expression for the input image position", OFFSET(y_expr), AV_OPT_TYPE_STRING, {.str = "0"}, CHAR_MIN, CHAR_MAX, FLAGS },
370  { "color", "set the color of the padded area border", OFFSET(rgba_color), AV_OPT_TYPE_COLOR, {.str = "black"}, .flags = FLAGS },
371  { NULL },
372 };
373 
375 
377  {
378  .name = "default",
379  .type = AVMEDIA_TYPE_VIDEO,
380  .config_props = config_input,
381  .get_video_buffer = get_video_buffer,
382  .filter_frame = filter_frame,
383  },
384  { NULL }
385 };
386 
388  {
389  .name = "default",
390  .type = AVMEDIA_TYPE_VIDEO,
391  .config_props = config_output,
392  },
393  { NULL }
394 };
395 
397  .name = "pad",
398  .description = NULL_IF_CONFIG_SMALL("Pad input image to width:height[:x:y[:color]] (default x and y: 0, default color: black)."),
399 
400  .priv_size = sizeof(PadContext),
401  .priv_class = &pad_class,
403 
404  .inputs = avfilter_vf_pad_inputs,
405 
406  .outputs = avfilter_vf_pad_outputs,
407 };