Libav
vf_boxblur.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2002 Michael Niedermayer <michaelni@gmx.at>
3  * Copyright (c) 2011 Stefano Sabatini
4  *
5  * This file is part of Libav.
6  *
7  * Libav is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (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
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along
18  * with Libav; if not, write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20  */
21 
28 #include "libavutil/avstring.h"
29 #include "libavutil/common.h"
30 #include "libavutil/eval.h"
31 #include "libavutil/opt.h"
32 #include "libavutil/pixdesc.h"
33 #include "avfilter.h"
34 #include "formats.h"
35 #include "internal.h"
36 #include "video.h"
37 
38 static const char *const var_names[] = {
39  "w",
40  "h",
41  "cw",
42  "ch",
43  "hsub",
44  "vsub",
45  NULL
46 };
47 
48 enum var_name {
56 };
57 
58 typedef struct {
59  int radius;
60  int power;
61 } FilterParam;
62 
63 typedef struct {
64  const AVClass *class;
71 
72  int hsub, vsub;
73  int radius[4];
74  int power[4];
75  uint8_t *temp[2];
77 
78 #define Y 0
79 #define U 1
80 #define V 2
81 #define A 3
82 
83 static av_cold int init(AVFilterContext *ctx)
84 {
85  BoxBlurContext *s = ctx->priv;
86 
87  if (!s->luma_radius_expr) {
88  av_log(ctx, AV_LOG_ERROR, "Luma radius expression is not set.\n");
89  return AVERROR(EINVAL);
90  }
91 
92  if (!s->chroma_radius_expr) {
94  if (!s->chroma_radius_expr)
95  return AVERROR(ENOMEM);
97  }
98  if (!s->alpha_radius_expr) {
100  if (!s->alpha_radius_expr)
101  return AVERROR(ENOMEM);
103  }
104 
105  return 0;
106 }
107 
108 static av_cold void uninit(AVFilterContext *ctx)
109 {
110  BoxBlurContext *s = ctx->priv;
111 
112  av_freep(&s->temp[0]);
113  av_freep(&s->temp[1]);
114 }
115 
117 {
118  enum AVPixelFormat pix_fmts[] = {
125  };
126 
128  return 0;
129 }
130 
131 static int config_input(AVFilterLink *inlink)
132 {
133  const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
134  AVFilterContext *ctx = inlink->dst;
135  BoxBlurContext *s = ctx->priv;
136  int w = inlink->w, h = inlink->h;
137  int cw, ch;
138  double var_values[VARS_NB], res;
139  char *expr;
140  int ret;
141 
142  av_freep(&s->temp[0]);
143  av_freep(&s->temp[1]);
144  if (!(s->temp[0] = av_malloc(FFMAX(w, h))))
145  return AVERROR(ENOMEM);
146  if (!(s->temp[1] = av_malloc(FFMAX(w, h)))) {
147  av_freep(&s->temp[0]);
148  return AVERROR(ENOMEM);
149  }
150 
151  s->hsub = desc->log2_chroma_w;
152  s->vsub = desc->log2_chroma_h;
153 
154  var_values[VAR_W] = inlink->w;
155  var_values[VAR_H] = inlink->h;
156  var_values[VAR_CW] = cw = w>>s->hsub;
157  var_values[VAR_CH] = ch = h>>s->vsub;
158  var_values[VAR_HSUB] = 1<<s->hsub;
159  var_values[VAR_VSUB] = 1<<s->vsub;
160 
161 #define EVAL_RADIUS_EXPR(comp) \
162  expr = s->comp##_radius_expr; \
163  ret = av_expr_parse_and_eval(&res, expr, var_names, var_values, \
164  NULL, NULL, NULL, NULL, NULL, 0, ctx); \
165  s->comp##_param.radius = res; \
166  if (ret < 0) { \
167  av_log(NULL, AV_LOG_ERROR, \
168  "Error when evaluating " #comp " radius expression '%s'\n", expr); \
169  return ret; \
170  }
171  EVAL_RADIUS_EXPR(luma);
172  EVAL_RADIUS_EXPR(chroma);
173  EVAL_RADIUS_EXPR(alpha);
174 
175  av_log(ctx, AV_LOG_DEBUG,
176  "luma_radius:%d luma_power:%d "
177  "chroma_radius:%d chroma_power:%d "
178  "alpha_radius:%d alpha_power:%d "
179  "w:%d chroma_w:%d h:%d chroma_h:%d\n",
183  w, cw, h, ch);
184 
185 #define CHECK_RADIUS_VAL(w_, h_, comp) \
186  if (s->comp##_param.radius < 0 || \
187  2*s->comp##_param.radius > FFMIN(w_, h_)) { \
188  av_log(ctx, AV_LOG_ERROR, \
189  "Invalid " #comp " radius value %d, must be >= 0 and <= %d\n", \
190  s->comp##_param.radius, FFMIN(w_, h_)/2); \
191  return AVERROR(EINVAL); \
192  }
193  CHECK_RADIUS_VAL(w, h, luma);
194  CHECK_RADIUS_VAL(cw, ch, chroma);
195  CHECK_RADIUS_VAL(w, h, alpha);
196 
197  s->radius[Y] = s->luma_param.radius;
198  s->radius[U] = s->radius[V] = s->chroma_param.radius;
199  s->radius[A] = s->alpha_param.radius;
200 
201  s->power[Y] = s->luma_param.power;
202  s->power[U] = s->power[V] = s->chroma_param.power;
203  s->power[A] = s->alpha_param.power;
204 
205  return 0;
206 }
207 
208 static inline void blur(uint8_t *dst, int dst_step, const uint8_t *src, int src_step,
209  int len, int radius)
210 {
211  /* Naive boxblur would sum source pixels from x-radius .. x+radius
212  * for destination pixel x. That would be O(radius*width).
213  * If you now look at what source pixels represent 2 consecutive
214  * output pixels, then you see they are almost identical and only
215  * differ by 2 pixels, like:
216  * src0 111111111
217  * dst0 1
218  * src1 111111111
219  * dst1 1
220  * src0-src1 1 -1
221  * so when you know one output pixel you can find the next by just adding
222  * and subtracting 1 input pixel.
223  * The following code adopts this faster variant.
224  */
225  const int length = radius*2 + 1;
226  const int inv = ((1<<16) + length/2)/length;
227  int x, sum = 0;
228 
229  for (x = 0; x < radius; x++)
230  sum += src[x*src_step]<<1;
231  sum += src[radius*src_step];
232 
233  for (x = 0; x <= radius; x++) {
234  sum += src[(radius+x)*src_step] - src[(radius-x)*src_step];
235  dst[x*dst_step] = (sum*inv + (1<<15))>>16;
236  }
237 
238  for (; x < len-radius; x++) {
239  sum += src[(radius+x)*src_step] - src[(x-radius-1)*src_step];
240  dst[x*dst_step] = (sum*inv + (1<<15))>>16;
241  }
242 
243  for (; x < len; x++) {
244  sum += src[(2*len-radius-x-1)*src_step] - src[(x-radius-1)*src_step];
245  dst[x*dst_step] = (sum*inv + (1<<15))>>16;
246  }
247 }
248 
249 static inline void blur_power(uint8_t *dst, int dst_step, const uint8_t *src, int src_step,
250  int len, int radius, int power, uint8_t *temp[2])
251 {
252  uint8_t *a = temp[0], *b = temp[1];
253 
254  if (radius && power) {
255  blur(a, 1, src, src_step, len, radius);
256  for (; power > 2; power--) {
257  uint8_t *c;
258  blur(b, 1, a, 1, len, radius);
259  c = a; a = b; b = c;
260  }
261  if (power > 1) {
262  blur(dst, dst_step, a, 1, len, radius);
263  } else {
264  int i;
265  for (i = 0; i < len; i++)
266  dst[i*dst_step] = a[i];
267  }
268  } else {
269  int i;
270  for (i = 0; i < len; i++)
271  dst[i*dst_step] = src[i*src_step];
272  }
273 }
274 
275 static void hblur(uint8_t *dst, int dst_linesize, const uint8_t *src, int src_linesize,
276  int w, int h, int radius, int power, uint8_t *temp[2])
277 {
278  int y;
279 
280  if (radius == 0 && dst == src)
281  return;
282 
283  for (y = 0; y < h; y++)
284  blur_power(dst + y*dst_linesize, 1, src + y*src_linesize, 1,
285  w, radius, power, temp);
286 }
287 
288 static void vblur(uint8_t *dst, int dst_linesize, const uint8_t *src, int src_linesize,
289  int w, int h, int radius, int power, uint8_t *temp[2])
290 {
291  int x;
292 
293  if (radius == 0 && dst == src)
294  return;
295 
296  for (x = 0; x < w; x++)
297  blur_power(dst + x, dst_linesize, src + x, src_linesize,
298  h, radius, power, temp);
299 }
300 
301 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
302 {
303  AVFilterContext *ctx = inlink->dst;
304  BoxBlurContext *s = ctx->priv;
305  AVFilterLink *outlink = inlink->dst->outputs[0];
306  AVFrame *out;
307  int plane;
308  int cw = inlink->w >> s->hsub, ch = in->height >> s->vsub;
309  int w[4] = { inlink->w, cw, cw, inlink->w };
310  int h[4] = { in->height, ch, ch, in->height };
311 
312  out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
313  if (!out) {
314  av_frame_free(&in);
315  return AVERROR(ENOMEM);
316  }
317  av_frame_copy_props(out, in);
318 
319  for (plane = 0; in->data[plane] && plane < 4; plane++)
320  hblur(out->data[plane], out->linesize[plane],
321  in ->data[plane], in ->linesize[plane],
322  w[plane], h[plane], s->radius[plane], s->power[plane],
323  s->temp);
324 
325  for (plane = 0; in->data[plane] && plane < 4; plane++)
326  vblur(out->data[plane], out->linesize[plane],
327  out->data[plane], out->linesize[plane],
328  w[plane], h[plane], s->radius[plane], s->power[plane],
329  s->temp);
330 
331  av_frame_free(&in);
332 
333  return ff_filter_frame(outlink, out);
334 }
335 
336 #define OFFSET(x) offsetof(BoxBlurContext, x)
337 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM
338 static const AVOption options[] = {
339  { "luma_radius", "Radius of the luma blurring box", OFFSET(luma_radius_expr), AV_OPT_TYPE_STRING, .flags = FLAGS },
340  { "luma_power", "How many times should the boxblur be applied to luma",
341  OFFSET(luma_param.power), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, INT_MAX, FLAGS },
342  { "chroma_radius", "Radius of the chroma blurring box", OFFSET(chroma_radius_expr), AV_OPT_TYPE_STRING, .flags = FLAGS },
343  { "chroma_power", "How many times should the boxblur be applied to chroma",
344  OFFSET(chroma_param.power), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, INT_MAX, FLAGS },
345  { "alpha_radius", "Radius of the alpha blurring box", OFFSET(alpha_radius_expr), AV_OPT_TYPE_STRING, .flags = FLAGS },
346  { "alpha_power", "How many times should the boxblur be applied to alpha",
347  OFFSET(alpha_param.power), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, INT_MAX, FLAGS },
348  { NULL },
349 };
350 
351 static const AVClass boxblur_class = {
352  .class_name = "boxblur",
353  .item_name = av_default_item_name,
354  .option = options,
355  .version = LIBAVUTIL_VERSION_INT,
356 };
357 
359  {
360  .name = "default",
361  .type = AVMEDIA_TYPE_VIDEO,
362  .config_props = config_input,
363  .filter_frame = filter_frame,
364  },
365  { NULL }
366 };
367 
369  {
370  .name = "default",
371  .type = AVMEDIA_TYPE_VIDEO,
372  },
373  { NULL }
374 };
375 
377  .name = "boxblur",
378  .description = NULL_IF_CONFIG_SMALL("Blur the input."),
379  .priv_size = sizeof(BoxBlurContext),
380  .priv_class = &boxblur_class,
381  .init = init,
382  .uninit = uninit,
384 
385  .inputs = avfilter_vf_boxblur_inputs,
386  .outputs = avfilter_vf_boxblur_outputs,
387 };