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 Libav.
6  *
7  * Libav 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  * Libav 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 Libav; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
27 #include "avfilter.h"
28 #include "libavutil/avstring.h"
29 #include "libavutil/eval.h"
30 #include "libavutil/pixdesc.h"
31 #include "libavutil/colorspace.h"
32 #include "libavutil/avassert.h"
33 #include "libavutil/imgutils.h"
34 #include "libavutil/parseutils.h"
35 #include "libavutil/mathematics.h"
36 #include "drawutils.h"
37 
38 static const char *var_names[] = {
39  "PI",
40  "PHI",
41  "E",
42  "in_w", "iw",
43  "in_h", "ih",
44  "out_w", "ow",
45  "out_h", "oh",
46  "x",
47  "y",
48  "a",
49  "hsub",
50  "vsub",
51  NULL
52 };
53 
54 enum var_name {
68 };
69 
71 {
72  static const enum PixelFormat pix_fmts[] = {
76 
83 
85  };
86 
88  return 0;
89 }
90 
91 typedef struct {
92  int w, h;
93  int x, y;
94  int in_w, in_h;
95 
96  char w_expr[256];
97  char h_expr[256];
98  char x_expr[256];
99  char y_expr[256];
100 
101  uint8_t color[4];
102  uint8_t *line[4];
103  int line_step[4];
104  int hsub, vsub;
106 } PadContext;
107 
108 static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
109 {
110  PadContext *pad = ctx->priv;
111  char color_string[128] = "black";
112 
113  av_strlcpy(pad->w_expr, "iw", sizeof(pad->w_expr));
114  av_strlcpy(pad->h_expr, "ih", sizeof(pad->h_expr));
115  av_strlcpy(pad->x_expr, "0" , sizeof(pad->w_expr));
116  av_strlcpy(pad->y_expr, "0" , sizeof(pad->h_expr));
117 
118  if (args)
119  sscanf(args, "%255[^:]:%255[^:]:%255[^:]:%255[^:]:%255s",
120  pad->w_expr, pad->h_expr, pad->x_expr, pad->y_expr, color_string);
121 
122  if (av_parse_color(pad->color, color_string, -1, ctx) < 0)
123  return AVERROR(EINVAL);
124 
125  return 0;
126 }
127 
128 static av_cold void uninit(AVFilterContext *ctx)
129 {
130  PadContext *pad = ctx->priv;
131  int i;
132 
133  for (i = 0; i < 4; i++) {
134  av_freep(&pad->line[i]);
135  pad->line_step[i] = 0;
136  }
137 }
138 
139 static int config_input(AVFilterLink *inlink)
140 {
141  AVFilterContext *ctx = inlink->dst;
142  PadContext *pad = ctx->priv;
143  const AVPixFmtDescriptor *pix_desc = &av_pix_fmt_descriptors[inlink->format];
144  uint8_t rgba_color[4];
145  int ret, is_packed_rgba;
146  double var_values[VARS_NB], res;
147  char *expr;
148 
149  pad->hsub = pix_desc->log2_chroma_w;
150  pad->vsub = pix_desc->log2_chroma_h;
151 
152  var_values[VAR_PI] = M_PI;
153  var_values[VAR_PHI] = M_PHI;
154  var_values[VAR_E] = M_E;
155  var_values[VAR_IN_W] = var_values[VAR_IW] = inlink->w;
156  var_values[VAR_IN_H] = var_values[VAR_IH] = inlink->h;
157  var_values[VAR_OUT_W] = var_values[VAR_OW] = NAN;
158  var_values[VAR_OUT_H] = var_values[VAR_OH] = NAN;
159  var_values[VAR_A] = (double) inlink->w / inlink->h;
160  var_values[VAR_HSUB] = 1<<pad->hsub;
161  var_values[VAR_VSUB] = 1<<pad->vsub;
162 
163  /* evaluate width and height */
164  av_expr_parse_and_eval(&res, (expr = pad->w_expr),
165  var_names, var_values,
166  NULL, NULL, NULL, NULL, NULL, 0, ctx);
167  pad->w = var_values[VAR_OUT_W] = var_values[VAR_OW] = res;
168  if ((ret = av_expr_parse_and_eval(&res, (expr = pad->h_expr),
169  var_names, var_values,
170  NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
171  goto eval_fail;
172  pad->h = var_values[VAR_OUT_H] = var_values[VAR_OH] = res;
173  /* evaluate the width again, as it may depend on the evaluated output height */
174  if ((ret = av_expr_parse_and_eval(&res, (expr = pad->w_expr),
175  var_names, var_values,
176  NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
177  goto eval_fail;
178  pad->w = var_values[VAR_OUT_W] = var_values[VAR_OW] = res;
179 
180  /* evaluate x and y */
181  av_expr_parse_and_eval(&res, (expr = pad->x_expr),
182  var_names, var_values,
183  NULL, NULL, NULL, NULL, NULL, 0, ctx);
184  pad->x = var_values[VAR_X] = res;
185  if ((ret = av_expr_parse_and_eval(&res, (expr = pad->y_expr),
186  var_names, var_values,
187  NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
188  goto eval_fail;
189  pad->y = var_values[VAR_Y] = res;
190  /* evaluate x again, as it may depend on the evaluated y value */
191  if ((ret = av_expr_parse_and_eval(&res, (expr = pad->x_expr),
192  var_names, var_values,
193  NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
194  goto eval_fail;
195  pad->x = var_values[VAR_X] = res;
196 
197  /* sanity check params */
198  if (pad->w < 0 || pad->h < 0 || pad->x < 0 || pad->y < 0) {
199  av_log(ctx, AV_LOG_ERROR, "Negative values are not acceptable.\n");
200  return AVERROR(EINVAL);
201  }
202 
203  if (!pad->w)
204  pad->w = inlink->w;
205  if (!pad->h)
206  pad->h = inlink->h;
207 
208  pad->w &= ~((1 << pad->hsub) - 1);
209  pad->h &= ~((1 << pad->vsub) - 1);
210  pad->x &= ~((1 << pad->hsub) - 1);
211  pad->y &= ~((1 << pad->vsub) - 1);
212 
213  pad->in_w = inlink->w & ~((1 << pad->hsub) - 1);
214  pad->in_h = inlink->h & ~((1 << pad->vsub) - 1);
215 
216  memcpy(rgba_color, pad->color, sizeof(rgba_color));
217  ff_fill_line_with_color(pad->line, pad->line_step, pad->w, pad->color,
218  inlink->format, rgba_color, &is_packed_rgba, NULL);
219 
220  av_log(ctx, AV_LOG_INFO, "w:%d h:%d -> w:%d h:%d x:%d y:%d color:0x%02X%02X%02X%02X[%s]\n",
221  inlink->w, inlink->h, pad->w, pad->h, pad->x, pad->y,
222  pad->color[0], pad->color[1], pad->color[2], pad->color[3],
223  is_packed_rgba ? "rgba" : "yuva");
224 
225  if (pad->x < 0 || pad->y < 0 ||
226  pad->w <= 0 || pad->h <= 0 ||
227  (unsigned)pad->x + (unsigned)inlink->w > pad->w ||
228  (unsigned)pad->y + (unsigned)inlink->h > pad->h) {
229  av_log(ctx, AV_LOG_ERROR,
230  "Input area %d:%d:%d:%d not within the padded area 0:0:%d:%d or zero-sized\n",
231  pad->x, pad->y, pad->x + inlink->w, pad->y + inlink->h, pad->w, pad->h);
232  return AVERROR(EINVAL);
233  }
234 
235  return 0;
236 
237 eval_fail:
238  av_log(NULL, AV_LOG_ERROR,
239  "Error when evaluating the expression '%s'\n", expr);
240  return ret;
241 
242 }
243 
244 static int config_output(AVFilterLink *outlink)
245 {
246  PadContext *pad = outlink->src->priv;
247 
248  outlink->w = pad->w;
249  outlink->h = pad->h;
250  return 0;
251 }
252 
253 static AVFilterBufferRef *get_video_buffer(AVFilterLink *inlink, int perms, int w, int h)
254 {
255  PadContext *pad = inlink->dst->priv;
256 
257  AVFilterBufferRef *picref = avfilter_get_video_buffer(inlink->dst->outputs[0], perms,
258  w + (pad->w - pad->in_w),
259  h + (pad->h - pad->in_h));
260  int plane;
261 
262  picref->video->w = w;
263  picref->video->h = h;
264 
265  for (plane = 0; plane < 4 && picref->data[plane]; plane++) {
266  int hsub = (plane == 1 || plane == 2) ? pad->hsub : 0;
267  int vsub = (plane == 1 || plane == 2) ? pad->vsub : 0;
268 
269  picref->data[plane] += (pad->x >> hsub) * pad->line_step[plane] +
270  (pad->y >> vsub) * picref->linesize[plane];
271  }
272 
273  return picref;
274 }
275 
276 static int does_clip(PadContext *pad, AVFilterBufferRef *outpicref, int plane, int hsub, int vsub, int x, int y)
277 {
278  int64_t x_in_buf, y_in_buf;
279 
280  x_in_buf = outpicref->data[plane] - outpicref->buf->data[plane]
281  + (x >> hsub) * pad ->line_step[plane]
282  + (y >> vsub) * outpicref->linesize [plane];
283 
284  if(x_in_buf < 0 || x_in_buf % pad->line_step[plane])
285  return 1;
286  x_in_buf /= pad->line_step[plane];
287 
288  av_assert0(outpicref->buf->linesize[plane]>0); //while reference can use negative linesize the main buffer should not
289 
290  y_in_buf = x_in_buf / outpicref->buf->linesize[plane];
291  x_in_buf %= outpicref->buf->linesize[plane];
292 
293  if( y_in_buf<<vsub >= outpicref->buf->h
294  || x_in_buf<<hsub >= outpicref->buf->w)
295  return 1;
296  return 0;
297 }
298 
299 static void start_frame(AVFilterLink *inlink, AVFilterBufferRef *inpicref)
300 {
301  PadContext *pad = inlink->dst->priv;
302  AVFilterBufferRef *outpicref = avfilter_ref_buffer(inpicref, ~0);
303  AVFilterBufferRef *for_next_filter;
304  int plane;
305 
306  for (plane = 0; plane < 4 && outpicref->data[plane]; plane++) {
307  int hsub = (plane == 1 || plane == 2) ? pad->hsub : 0;
308  int vsub = (plane == 1 || plane == 2) ? pad->vsub : 0;
309 
310  av_assert0(outpicref->buf->w>0 && outpicref->buf->h>0);
311 
312  if(outpicref->format != outpicref->buf->format) //unsupported currently
313  break;
314 
315  outpicref->data[plane] -= (pad->x >> hsub) * pad ->line_step[plane]
316  + (pad->y >> vsub) * outpicref->linesize [plane];
317 
318  if( does_clip(pad, outpicref, plane, hsub, vsub, 0, 0)
319  || does_clip(pad, outpicref, plane, hsub, vsub, 0, pad->h-1)
320  || does_clip(pad, outpicref, plane, hsub, vsub, pad->w-1, 0)
321  || does_clip(pad, outpicref, plane, hsub, vsub, pad->w-1, pad->h-1)
322  )
323  break;
324  }
325  pad->needs_copy= plane < 4 && outpicref->data[plane];
326  if(pad->needs_copy){
327  av_log(inlink->dst, AV_LOG_DEBUG, "Direct padding impossible allocating new frame\n");
328  avfilter_unref_buffer(outpicref);
330  FFMAX(inlink->w, pad->w),
331  FFMAX(inlink->h, pad->h));
332  avfilter_copy_buffer_ref_props(outpicref, inpicref);
333  }
334 
335  inlink->dst->outputs[0]->out_buf = outpicref;
336 
337  outpicref->video->w = pad->w;
338  outpicref->video->h = pad->h;
339 
340  for_next_filter = avfilter_ref_buffer(outpicref, ~0);
341  avfilter_start_frame(inlink->dst->outputs[0], for_next_filter);
342 }
343 
344 static void end_frame(AVFilterLink *link)
345 {
346  avfilter_end_frame(link->dst->outputs[0]);
349 }
350 
351 static void draw_send_bar_slice(AVFilterLink *link, int y, int h, int slice_dir, int before_slice)
352 {
353  PadContext *pad = link->dst->priv;
354  int bar_y, bar_h = 0;
355 
356  if (slice_dir * before_slice == 1 && y == pad->y) {
357  /* top bar */
358  bar_y = 0;
359  bar_h = pad->y;
360  } else if (slice_dir * before_slice == -1 && (y + h) == (pad->y + pad->in_h)) {
361  /* bottom bar */
362  bar_y = pad->y + pad->in_h;
363  bar_h = pad->h - pad->in_h - pad->y;
364  }
365 
366  if (bar_h) {
368  link->dst->outputs[0]->out_buf->linesize,
369  pad->line, pad->line_step, pad->hsub, pad->vsub,
370  0, bar_y, pad->w, bar_h);
371  avfilter_draw_slice(link->dst->outputs[0], bar_y, bar_h, slice_dir);
372  }
373 }
374 
375 static void draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
376 {
377  PadContext *pad = link->dst->priv;
378  AVFilterBufferRef *outpic = link->dst->outputs[0]->out_buf;
379  AVFilterBufferRef *inpic = link->cur_buf;
380 
381  y += pad->y;
382 
383  y &= ~((1 << pad->vsub) - 1);
384  h &= ~((1 << pad->vsub) - 1);
385 
386  if (!h)
387  return;
388  draw_send_bar_slice(link, y, h, slice_dir, 1);
389 
390  /* left border */
391  ff_draw_rectangle(outpic->data, outpic->linesize, pad->line, pad->line_step,
392  pad->hsub, pad->vsub, 0, y, pad->x, h);
393 
394  if(pad->needs_copy){
395  ff_copy_rectangle(outpic->data, outpic->linesize,
396  inpic->data, inpic->linesize, pad->line_step,
397  pad->hsub, pad->vsub,
398  pad->x, y, y-pad->y, inpic->video->w, h);
399  }
400 
401  /* right border */
402  ff_draw_rectangle(outpic->data, outpic->linesize,
403  pad->line, pad->line_step, pad->hsub, pad->vsub,
404  pad->x + pad->in_w, y, pad->w - pad->x - pad->in_w, h);
405  avfilter_draw_slice(link->dst->outputs[0], y, h, slice_dir);
406 
407  draw_send_bar_slice(link, y, h, slice_dir, -1);
408 }
409 
411  .name = "pad",
412  .description = NULL_IF_CONFIG_SMALL("Pad input image to width:height[:x:y[:color]] (default x and y: 0, default color: black)."),
413 
414  .priv_size = sizeof(PadContext),
415  .init = init,
416  .uninit = uninit,
418 
419  .inputs = (AVFilterPad[]) {{ .name = "default",
420  .type = AVMEDIA_TYPE_VIDEO,
421  .config_props = config_input,
422  .get_video_buffer = get_video_buffer,
423  .start_frame = start_frame,
424  .draw_slice = draw_slice,
425  .end_frame = end_frame, },
426  { .name = NULL}},
427 
428  .outputs = (AVFilterPad[]) {{ .name = "default",
429  .type = AVMEDIA_TYPE_VIDEO,
430  .config_props = config_output, },
431  { .name = NULL}},
432 };