FFmpeg
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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 FFmpeg.
6  *
7  * FFmpeg 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  * 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
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 FFmpeg; if not, write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20  */
21 
22 /**
23  * @file
24  * Apply a boxblur filter to the input video.
25  * Ported from MPlayer libmpcodecs/vf_boxblur.c.
26  */
27 
28 #include "libavutil/avstring.h"
29 #include "libavutil/common.h"
30 #include "libavutil/eval.h"
31 #include "libavutil/pixdesc.h"
32 #include "avfilter.h"
33 #include "formats.h"
34 #include "internal.h"
35 #include "video.h"
36 
37 static const char *const var_names[] = {
38  "w",
39  "h",
40  "cw",
41  "ch",
42  "hsub",
43  "vsub",
44  NULL
45 };
46 
47 enum var_name {
55 };
56 
57 typedef struct {
58  int radius;
59  int power;
60 } FilterParam;
61 
62 typedef struct {
66  char luma_radius_expr [256];
67  char chroma_radius_expr[256];
68  char alpha_radius_expr [256];
69 
70  int hsub, vsub;
71  int radius[4];
72  int power[4];
73  uint8_t *temp[2]; ///< temporary buffer used in blur_power()
75 
76 #define Y 0
77 #define U 1
78 #define V 2
79 #define A 3
80 
81 static av_cold int init(AVFilterContext *ctx, const char *args)
82 {
83  BoxBlurContext *boxblur = ctx->priv;
84  int e;
85 
86  if (!args) {
87  av_log(ctx, AV_LOG_ERROR,
88  "Filter expects 2 or 4 or 6 arguments, none provided\n");
89  return AVERROR(EINVAL);
90  }
91 
92  e = sscanf(args, "%255[^:]:%d:%255[^:]:%d:%255[^:]:%d",
93  boxblur->luma_radius_expr, &boxblur->luma_param .power,
94  boxblur->chroma_radius_expr, &boxblur->chroma_param.power,
95  boxblur->alpha_radius_expr, &boxblur->alpha_param .power);
96 
97  if (e != 2 && e != 4 && e != 6) {
98  av_log(ctx, AV_LOG_ERROR,
99  "Filter expects 2 or 4 or 6 params, provided %d\n", e);
100  return AVERROR(EINVAL);
101  }
102 
103  if (e < 4) {
104  boxblur->chroma_param.power = boxblur->luma_param.power;
106  sizeof(boxblur->chroma_radius_expr));
107  }
108  if (e < 6) {
109  boxblur->alpha_param.power = boxblur->luma_param.power;
110  av_strlcpy(boxblur->alpha_radius_expr, boxblur->luma_radius_expr,
111  sizeof(boxblur->alpha_radius_expr));
112  }
113 
114  return 0;
115 }
116 
117 static av_cold void uninit(AVFilterContext *ctx)
118 {
119  BoxBlurContext *boxblur = ctx->priv;
120 
121  av_freep(&boxblur->temp[0]);
122  av_freep(&boxblur->temp[1]);
123 }
124 
126 {
127  static const enum AVPixelFormat pix_fmts[] = {
134  };
135 
137  return 0;
138 }
139 
140 static int config_input(AVFilterLink *inlink)
141 {
142  const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
143  AVFilterContext *ctx = inlink->dst;
144  BoxBlurContext *boxblur = ctx->priv;
145  int w = inlink->w, h = inlink->h;
146  int cw, ch;
147  double var_values[VARS_NB], res;
148  char *expr;
149  int ret;
150 
151  if (!(boxblur->temp[0] = av_malloc(FFMAX(w, h))) ||
152  !(boxblur->temp[1] = av_malloc(FFMAX(w, h))))
153  return AVERROR(ENOMEM);
154 
155  boxblur->hsub = desc->log2_chroma_w;
156  boxblur->vsub = desc->log2_chroma_h;
157 
158  var_values[VAR_W] = inlink->w;
159  var_values[VAR_H] = inlink->h;
160  var_values[VAR_CW] = cw = w>>boxblur->hsub;
161  var_values[VAR_CH] = ch = h>>boxblur->vsub;
162  var_values[VAR_HSUB] = 1<<boxblur->hsub;
163  var_values[VAR_VSUB] = 1<<boxblur->vsub;
164 
165 #define EVAL_RADIUS_EXPR(comp) \
166  expr = boxblur->comp##_radius_expr; \
167  ret = av_expr_parse_and_eval(&res, expr, var_names, var_values, \
168  NULL, NULL, NULL, NULL, NULL, 0, ctx); \
169  boxblur->comp##_param.radius = res; \
170  if (ret < 0) { \
171  av_log(NULL, AV_LOG_ERROR, \
172  "Error when evaluating " #comp " radius expression '%s'\n", expr); \
173  return ret; \
174  }
175  EVAL_RADIUS_EXPR(luma);
176  EVAL_RADIUS_EXPR(chroma);
177  EVAL_RADIUS_EXPR(alpha);
178 
179  av_log(ctx, AV_LOG_VERBOSE,
180  "luma_radius:%d luma_power:%d "
181  "chroma_radius:%d chroma_power:%d "
182  "alpha_radius:%d alpha_power:%d "
183  "w:%d chroma_w:%d h:%d chroma_h:%d\n",
184  boxblur->luma_param .radius, boxblur->luma_param .power,
185  boxblur->chroma_param.radius, boxblur->chroma_param.power,
186  boxblur->alpha_param .radius, boxblur->alpha_param .power,
187  w, cw, h, ch);
188 
189 #define CHECK_RADIUS_VAL(w_, h_, comp) \
190  if (boxblur->comp##_param.radius < 0 || \
191  2*boxblur->comp##_param.radius > FFMIN(w_, h_)) { \
192  av_log(ctx, AV_LOG_ERROR, \
193  "Invalid " #comp " radius value %d, must be >= 0 and <= %d\n", \
194  boxblur->comp##_param.radius, FFMIN(w_, h_)/2); \
195  return AVERROR(EINVAL); \
196  }
197  CHECK_RADIUS_VAL(w, h, luma);
198  CHECK_RADIUS_VAL(cw, ch, chroma);
199  CHECK_RADIUS_VAL(w, h, alpha);
200 
201  boxblur->radius[Y] = boxblur->luma_param.radius;
202  boxblur->radius[U] = boxblur->radius[V] = boxblur->chroma_param.radius;
203  boxblur->radius[A] = boxblur->alpha_param.radius;
204 
205  boxblur->power[Y] = boxblur->luma_param.power;
206  boxblur->power[U] = boxblur->power[V] = boxblur->chroma_param.power;
207  boxblur->power[A] = boxblur->alpha_param.power;
208 
209  return 0;
210 }
211 
212 static inline void blur(uint8_t *dst, int dst_step, const uint8_t *src, int src_step,
213  int len, int radius)
214 {
215  /* Naive boxblur would sum source pixels from x-radius .. x+radius
216  * for destination pixel x. That would be O(radius*width).
217  * If you now look at what source pixels represent 2 consecutive
218  * output pixels, then you see they are almost identical and only
219  * differ by 2 pixels, like:
220  * src0 111111111
221  * dst0 1
222  * src1 111111111
223  * dst1 1
224  * src0-src1 1 -1
225  * so when you know one output pixel you can find the next by just adding
226  * and subtracting 1 input pixel.
227  * The following code adopts this faster variant.
228  */
229  const int length = radius*2 + 1;
230  const int inv = ((1<<16) + length/2)/length;
231  int x, sum = 0;
232 
233  for (x = 0; x < radius; x++)
234  sum += src[x*src_step]<<1;
235  sum += src[radius*src_step];
236 
237  for (x = 0; x <= radius; x++) {
238  sum += src[(radius+x)*src_step] - src[(radius-x)*src_step];
239  dst[x*dst_step] = (sum*inv + (1<<15))>>16;
240  }
241 
242  for (; x < len-radius; x++) {
243  sum += src[(radius+x)*src_step] - src[(x-radius-1)*src_step];
244  dst[x*dst_step] = (sum*inv + (1<<15))>>16;
245  }
246 
247  for (; x < len; x++) {
248  sum += src[(2*len-radius-x-1)*src_step] - src[(x-radius-1)*src_step];
249  dst[x*dst_step] = (sum*inv + (1<<15))>>16;
250  }
251 }
252 
253 static inline void blur_power(uint8_t *dst, int dst_step, const uint8_t *src, int src_step,
254  int len, int radius, int power, uint8_t *temp[2])
255 {
256  uint8_t *a = temp[0], *b = temp[1];
257 
258  if (radius && power) {
259  blur(a, 1, src, src_step, len, radius);
260  for (; power > 2; power--) {
261  uint8_t *c;
262  blur(b, 1, a, 1, len, radius);
263  c = a; a = b; b = c;
264  }
265  if (power > 1) {
266  blur(dst, dst_step, a, 1, len, radius);
267  } else {
268  int i;
269  for (i = 0; i < len; i++)
270  dst[i*dst_step] = a[i];
271  }
272  } else {
273  int i;
274  for (i = 0; i < len; i++)
275  dst[i*dst_step] = src[i*src_step];
276  }
277 }
278 
279 static void hblur(uint8_t *dst, int dst_linesize, const uint8_t *src, int src_linesize,
280  int w, int h, int radius, int power, uint8_t *temp[2])
281 {
282  int y;
283 
284  if (radius == 0 && dst == src)
285  return;
286 
287  for (y = 0; y < h; y++)
288  blur_power(dst + y*dst_linesize, 1, src + y*src_linesize, 1,
289  w, radius, power, temp);
290 }
291 
292 static void vblur(uint8_t *dst, int dst_linesize, const uint8_t *src, int src_linesize,
293  int w, int h, int radius, int power, uint8_t *temp[2])
294 {
295  int x;
296 
297  if (radius == 0 && dst == src)
298  return;
299 
300  for (x = 0; x < w; x++)
301  blur_power(dst + x, dst_linesize, src + x, src_linesize,
302  h, radius, power, temp);
303 }
304 
306 {
307  AVFilterContext *ctx = inlink->dst;
308  BoxBlurContext *boxblur = ctx->priv;
309  AVFilterLink *outlink = inlink->dst->outputs[0];
310  AVFilterBufferRef *out;
311  int plane;
312  int cw = inlink->w >> boxblur->hsub, ch = in->video->h >> boxblur->vsub;
313  int w[4] = { inlink->w, cw, cw, inlink->w };
314  int h[4] = { in->video->h, ch, ch, in->video->h };
315 
316  out = ff_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
317  if (!out) {
319  return AVERROR(ENOMEM);
320  }
322 
323  for (plane = 0; in->data[plane] && plane < 4; plane++)
324  hblur(out->data[plane], out->linesize[plane],
325  in ->data[plane], in ->linesize[plane],
326  w[plane], h[plane], boxblur->radius[plane], boxblur->power[plane],
327  boxblur->temp);
328 
329  for (plane = 0; in->data[plane] && plane < 4; plane++)
330  vblur(out->data[plane], out->linesize[plane],
331  out->data[plane], out->linesize[plane],
332  w[plane], h[plane], boxblur->radius[plane], boxblur->power[plane],
333  boxblur->temp);
334 
336 
337  return ff_filter_frame(outlink, out);
338 }
339 
341  {
342  .name = "default",
343  .type = AVMEDIA_TYPE_VIDEO,
344  .config_props = config_input,
345  .filter_frame = filter_frame,
346  .min_perms = AV_PERM_READ
347  },
348  { NULL }
349 };
350 
352  {
353  .name = "default",
354  .type = AVMEDIA_TYPE_VIDEO,
355  },
356  { NULL }
357 };
358 
360  .name = "boxblur",
361  .description = NULL_IF_CONFIG_SMALL("Blur the input."),
362  .priv_size = sizeof(BoxBlurContext),
363  .init = init,
364  .uninit = uninit,
366 
367  .inputs = avfilter_vf_boxblur_inputs,
368  .outputs = avfilter_vf_boxblur_outputs,
369 };