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/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];
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  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  av_freep(&boxblur->temp[0]);
152  av_freep(&boxblur->temp[1]);
153  if (!(boxblur->temp[0] = av_malloc(FFMAX(w, h))))
154  return AVERROR(ENOMEM);
155  if (!(boxblur->temp[1] = av_malloc(FFMAX(w, h)))) {
156  av_freep(&boxblur->temp[0]);
157  return AVERROR(ENOMEM);
158  }
159 
160  boxblur->hsub = desc->log2_chroma_w;
161  boxblur->vsub = desc->log2_chroma_h;
162 
163  var_values[VAR_W] = inlink->w;
164  var_values[VAR_H] = inlink->h;
165  var_values[VAR_CW] = cw = w>>boxblur->hsub;
166  var_values[VAR_CH] = ch = h>>boxblur->vsub;
167  var_values[VAR_HSUB] = 1<<boxblur->hsub;
168  var_values[VAR_VSUB] = 1<<boxblur->vsub;
169 
170 #define EVAL_RADIUS_EXPR(comp) \
171  expr = boxblur->comp##_radius_expr; \
172  ret = av_expr_parse_and_eval(&res, expr, var_names, var_values, \
173  NULL, NULL, NULL, NULL, NULL, 0, ctx); \
174  boxblur->comp##_param.radius = res; \
175  if (ret < 0) { \
176  av_log(NULL, AV_LOG_ERROR, \
177  "Error when evaluating " #comp " radius expression '%s'\n", expr); \
178  return ret; \
179  }
180  EVAL_RADIUS_EXPR(luma);
181  EVAL_RADIUS_EXPR(chroma);
182  EVAL_RADIUS_EXPR(alpha);
183 
184  av_log(ctx, AV_LOG_DEBUG,
185  "luma_radius:%d luma_power:%d "
186  "chroma_radius:%d chroma_power:%d "
187  "alpha_radius:%d alpha_power:%d "
188  "w:%d chroma_w:%d h:%d chroma_h:%d\n",
189  boxblur->luma_param .radius, boxblur->luma_param .power,
190  boxblur->chroma_param.radius, boxblur->chroma_param.power,
191  boxblur->alpha_param .radius, boxblur->alpha_param .power,
192  w, cw, h, ch);
193 
194 #define CHECK_RADIUS_VAL(w_, h_, comp) \
195  if (boxblur->comp##_param.radius < 0 || \
196  2*boxblur->comp##_param.radius > FFMIN(w_, h_)) { \
197  av_log(ctx, AV_LOG_ERROR, \
198  "Invalid " #comp " radius value %d, must be >= 0 and <= %d\n", \
199  boxblur->comp##_param.radius, FFMIN(w_, h_)/2); \
200  return AVERROR(EINVAL); \
201  }
202  CHECK_RADIUS_VAL(w, h, luma);
203  CHECK_RADIUS_VAL(cw, ch, chroma);
204  CHECK_RADIUS_VAL(w, h, alpha);
205 
206  boxblur->radius[Y] = boxblur->luma_param.radius;
207  boxblur->radius[U] = boxblur->radius[V] = boxblur->chroma_param.radius;
208  boxblur->radius[A] = boxblur->alpha_param.radius;
209 
210  boxblur->power[Y] = boxblur->luma_param.power;
211  boxblur->power[U] = boxblur->power[V] = boxblur->chroma_param.power;
212  boxblur->power[A] = boxblur->alpha_param.power;
213 
214  return 0;
215 }
216 
217 static inline void blur(uint8_t *dst, int dst_step, const uint8_t *src, int src_step,
218  int len, int radius)
219 {
220  /* Naive boxblur would sum source pixels from x-radius .. x+radius
221  * for destination pixel x. That would be O(radius*width).
222  * If you now look at what source pixels represent 2 consecutive
223  * output pixels, then you see they are almost identical and only
224  * differ by 2 pixels, like:
225  * src0 111111111
226  * dst0 1
227  * src1 111111111
228  * dst1 1
229  * src0-src1 1 -1
230  * so when you know one output pixel you can find the next by just adding
231  * and subtracting 1 input pixel.
232  * The following code adopts this faster variant.
233  */
234  const int length = radius*2 + 1;
235  const int inv = ((1<<16) + length/2)/length;
236  int x, sum = 0;
237 
238  for (x = 0; x < radius; x++)
239  sum += src[x*src_step]<<1;
240  sum += src[radius*src_step];
241 
242  for (x = 0; x <= radius; x++) {
243  sum += src[(radius+x)*src_step] - src[(radius-x)*src_step];
244  dst[x*dst_step] = (sum*inv + (1<<15))>>16;
245  }
246 
247  for (; x < len-radius; x++) {
248  sum += src[(radius+x)*src_step] - src[(x-radius-1)*src_step];
249  dst[x*dst_step] = (sum*inv + (1<<15))>>16;
250  }
251 
252  for (; x < len; x++) {
253  sum += src[(2*len-radius-x-1)*src_step] - src[(x-radius-1)*src_step];
254  dst[x*dst_step] = (sum*inv + (1<<15))>>16;
255  }
256 }
257 
258 static inline void blur_power(uint8_t *dst, int dst_step, const uint8_t *src, int src_step,
259  int len, int radius, int power, uint8_t *temp[2])
260 {
261  uint8_t *a = temp[0], *b = temp[1];
262 
263  if (radius && power) {
264  blur(a, 1, src, src_step, len, radius);
265  for (; power > 2; power--) {
266  uint8_t *c;
267  blur(b, 1, a, 1, len, radius);
268  c = a; a = b; b = c;
269  }
270  if (power > 1) {
271  blur(dst, dst_step, a, 1, len, radius);
272  } else {
273  int i;
274  for (i = 0; i < len; i++)
275  dst[i*dst_step] = a[i];
276  }
277  } else {
278  int i;
279  for (i = 0; i < len; i++)
280  dst[i*dst_step] = src[i*src_step];
281  }
282 }
283 
284 static void hblur(uint8_t *dst, int dst_linesize, const uint8_t *src, int src_linesize,
285  int w, int h, int radius, int power, uint8_t *temp[2])
286 {
287  int y;
288 
289  if (radius == 0 && dst == src)
290  return;
291 
292  for (y = 0; y < h; y++)
293  blur_power(dst + y*dst_linesize, 1, src + y*src_linesize, 1,
294  w, radius, power, temp);
295 }
296 
297 static void vblur(uint8_t *dst, int dst_linesize, const uint8_t *src, int src_linesize,
298  int w, int h, int radius, int power, uint8_t *temp[2])
299 {
300  int x;
301 
302  if (radius == 0 && dst == src)
303  return;
304 
305  for (x = 0; x < w; x++)
306  blur_power(dst + x, dst_linesize, src + x, src_linesize,
307  h, radius, power, temp);
308 }
309 
311 {
312  AVFilterContext *ctx = inlink->dst;
313  BoxBlurContext *boxblur = ctx->priv;
314  AVFilterLink *outlink = inlink->dst->outputs[0];
315  AVFilterBufferRef *out;
316  int plane;
317  int cw = inlink->w >> boxblur->hsub, ch = in->video->h >> boxblur->vsub;
318  int w[4] = { inlink->w, cw, cw, inlink->w };
319  int h[4] = { in->video->h, ch, ch, in->video->h };
320 
321  out = ff_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
322  if (!out) {
324  return AVERROR(ENOMEM);
325  }
327 
328  for (plane = 0; in->data[plane] && plane < 4; plane++)
329  hblur(out->data[plane], out->linesize[plane],
330  in ->data[plane], in ->linesize[plane],
331  w[plane], h[plane], boxblur->radius[plane], boxblur->power[plane],
332  boxblur->temp);
333 
334  for (plane = 0; in->data[plane] && plane < 4; plane++)
335  vblur(out->data[plane], out->linesize[plane],
336  out->data[plane], out->linesize[plane],
337  w[plane], h[plane], boxblur->radius[plane], boxblur->power[plane],
338  boxblur->temp);
339 
341 
342  return ff_filter_frame(outlink, out);
343 }
344 
346  {
347  .name = "default",
348  .type = AVMEDIA_TYPE_VIDEO,
349  .config_props = config_input,
350  .filter_frame = filter_frame,
351  .min_perms = AV_PERM_READ
352  },
353  { NULL }
354 };
355 
357  {
358  .name = "default",
359  .type = AVMEDIA_TYPE_VIDEO,
360  },
361  { NULL }
362 };
363 
365  .name = "boxblur",
366  .description = NULL_IF_CONFIG_SMALL("Blur the input."),
367  .priv_size = sizeof(BoxBlurContext),
368  .init = init,
369  .uninit = uninit,
371 
372  .inputs = avfilter_vf_boxblur_inputs,
373  .outputs = avfilter_vf_boxblur_outputs,
374 };
void * av_malloc(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:61
#define U
Definition: vf_boxblur.c:77
AVFilter avfilter_vf_boxblur
Definition: vf_boxblur.c:364
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:1435
AVFilterBufferRefVideoProps * video
video buffer specific properties
Definition: avfilter.h:159
static av_cold int init(AVFilterContext *ctx, const char *args)
Definition: vf_boxblur.c:81
int linesize[8]
number of bytes per line
Definition: avfilter.h:157
static void hblur(uint8_t *dst, int dst_linesize, const uint8_t *src, int src_linesize, int w, int h, int radius, int power, uint8_t *temp[2])
Definition: vf_boxblur.c:284
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition: pixfmt.h:70
static const AVFilterPad outputs[]
Definition: af_ashowinfo.c:122
static int config_input(AVFilterLink *inlink)
Definition: vf_boxblur.c:140
char chroma_radius_expr[256]
Definition: vf_boxblur.c:67
int ff_filter_frame(AVFilterLink *link, AVFilterBufferRef *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:459
uint8_t log2_chroma_w
Amount to shift the luma width right to find the chroma width.
Definition: pixdesc.h:66
void av_freep(void *arg)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
Definition: mem.c:151
AVFilterFormats * ff_make_format_list(const int *fmts)
Create a list of supported formats.
Definition: formats.c:165
static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *in)
Definition: vf_boxblur.c:310
#define AV_PERM_READ
can read from the buffer
Definition: avfilter.h:97
const char * name
Pad name.
Definition: internal.h:39
planar YUV 4:2:0, 20bpp, (1 Cr & Cb sample per 2x2 Y & A samples)
Definition: pixfmt.h:102
uint8_t
uint8_t * temp[2]
temporary buffer used in blur_power()
Definition: vf_boxblur.c:73
#define Y
Definition: vf_boxblur.c:76
struct FilterParam FilterParam
#define b
Definition: input.c:52
void avfilter_unref_bufferp(AVFilterBufferRef **ref)
Remove a reference to a buffer and set the pointer to NULL.
Definition: buffer.c:88
#define A
Definition: vf_boxblur.c:79
static const AVFilterPad avfilter_vf_boxblur_outputs[]
Definition: vf_boxblur.c:356
static void vblur(uint8_t *dst, int dst_linesize, const uint8_t *src, int src_linesize, int w, int h, int radius, int power, uint8_t *temp[2])
Definition: vf_boxblur.c:297
planar YUV 4:4:0 full scale (JPEG), deprecated in favor of PIX_FMT_YUV440P and setting color_range ...
Definition: pixfmt.h:101
planar YUV 4:2:2, 16bpp, full scale (JPEG), deprecated in favor of PIX_FMT_YUV422P and setting color_...
Definition: pixfmt.h:78
FilterParam alpha_param
Definition: vf_boxblur.c:65
var_name
Definition: vf_boxblur.c:47
void ff_set_common_formats(AVFilterContext *ctx, AVFilterFormats *formats)
A helper for query_formats() which sets all links to the same list of formats.
Definition: formats.c:375
A filter pad used for either input or output.
Definition: internal.h:33
AVFilterBufferRef * ff_get_video_buffer(AVFilterLink *link, int perms, int w, int h)
Request a picture buffer with a specific set of permissions.
Definition: video.c:145
int h
image height
Definition: avfilter.h:123
uint8_t log2_chroma_h
Amount to shift the luma height right to find the chroma height.
Definition: pixdesc.h:75
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:88
void * priv
private data for use by the filter
Definition: avfilter.h:439
static int query_formats(AVFilterContext *ctx)
Definition: vf_boxblur.c:125
int radius[4]
Definition: vf_boxblur.c:71
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:146
static const AVFilterPad avfilter_vf_boxblur_inputs[]
Definition: vf_boxblur.c:345
#define V
Definition: vf_boxblur.c:78
size_t av_strlcpy(char *dst, const char *src, size_t size)
Copy the string src to dst, but no more than size - 1 bytes, and null-terminate dst.
Definition: avstring.c:67
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition: pixfmt.h:69
planar YUV 4:2:0, 12bpp, full scale (JPEG), deprecated in favor of PIX_FMT_YUV420P and setting color_...
Definition: pixfmt.h:77
FilterParam luma_param
Definition: vf_boxblur.c:63
char alpha_radius_expr[256]
Definition: vf_boxblur.c:68
static const char *const var_names[]
Definition: vf_boxblur.c:37
int power[4]
Definition: vf_boxblur.c:72
#define EVAL_RADIUS_EXPR(comp)
A reference to an AVFilterBuffer.
Definition: avfilter.h:139
NULL
Definition: eval.c:52
static void blur(uint8_t *dst, int dst_step, const uint8_t *src, int src_step, int len, int radius)
Definition: vf_boxblur.c:217
static void blur_power(uint8_t *dst, int dst_step, const uint8_t *src, int src_step, int len, int radius, int power, uint8_t *temp[2])
Definition: vf_boxblur.c:258
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:55
planar YUV 4:1:0, 9bpp, (1 Cr & Cb sample per 4x4 Y samples)
Definition: pixfmt.h:71
Filter definition.
Definition: avfilter.h:371
static const AVFilterPad inputs[]
Definition: af_ashowinfo.c:110
static av_cold void uninit(AVFilterContext *ctx)
Definition: vf_boxblur.c:117
const char * name
filter name
Definition: avfilter.h:372
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:433
char luma_radius_expr[256]
Definition: vf_boxblur.c:66
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:65
Y , 8bpp.
Definition: pixfmt.h:73
common internal and external API header
#define CHECK_RADIUS_VAL(w_, h_, comp)
void avfilter_copy_buffer_ref_props(AVFilterBufferRef *dst, AVFilterBufferRef *src)
Copy properties of src to dst, without copying the actual data.
Definition: buffer.c:164
planar YUV 4:4:4, 24bpp, full scale (JPEG), deprecated in favor of PIX_FMT_YUV444P and setting color_...
Definition: pixfmt.h:79
planar YUV 4:1:1, 12bpp, (1 Cr & Cb sample per 4x1 Y samples)
Definition: pixfmt.h:72
FilterParam chroma_param
Definition: vf_boxblur.c:64
#define AV_PERM_WRITE
can write to the buffer
Definition: avfilter.h:98
int len
uint8_t * data[8]
picture/audio data for each plane
Definition: avfilter.h:141
An instance of a filter.
Definition: avfilter.h:418
planar YUV 4:4:0 (1 Cr & Cb sample per 1x2 Y samples)
Definition: pixfmt.h:100
internal API functions
AVPixelFormat
Pixel format.
Definition: pixfmt.h:63
simple arithmetic expression evaluator