2 * Copyright (c) 2002 Michael Niedermayer <michaelni@gmx.at>
3 * Copyright (c) 2011 Stefano Sabatini
5 * This file is part of Libav.
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.
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.
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.
24 * Apply a boxblur filter to the input video.
25 * Ported from MPlayer libmpcodecs/vf_boxblur.c.
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"
38 static const char *const var_names[] = {
65 FilterParam luma_param;
66 FilterParam chroma_param;
67 FilterParam alpha_param;
68 char *luma_radius_expr;
69 char *chroma_radius_expr;
70 char *alpha_radius_expr;
75 uint8_t *temp[2]; ///< temporary buffer used in blur_power()
83 static av_cold int init(AVFilterContext *ctx)
85 BoxBlurContext *s = ctx->priv;
87 if (!s->luma_radius_expr) {
88 av_log(ctx, AV_LOG_ERROR, "Luma radius expression is not set.\n");
89 return AVERROR(EINVAL);
92 if (!s->chroma_radius_expr) {
93 s->chroma_radius_expr = av_strdup(s->luma_radius_expr);
94 if (!s->chroma_radius_expr)
95 return AVERROR(ENOMEM);
96 s->chroma_param.power = s->luma_param.power;
98 if (!s->alpha_radius_expr) {
99 s->alpha_radius_expr = av_strdup(s->luma_radius_expr);
100 if (!s->alpha_radius_expr)
101 return AVERROR(ENOMEM);
102 s->alpha_param.power = s->luma_param.power;
108 static av_cold void uninit(AVFilterContext *ctx)
110 BoxBlurContext *s = ctx->priv;
112 av_freep(&s->temp[0]);
113 av_freep(&s->temp[1]);
116 static int query_formats(AVFilterContext *ctx)
118 enum AVPixelFormat pix_fmts[] = {
119 AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV420P,
120 AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUVA420P,
121 AV_PIX_FMT_YUV440P, AV_PIX_FMT_GRAY8,
122 AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ420P,
127 ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
131 static int config_input(AVFilterLink *inlink)
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;
138 double var_values[VARS_NB], res;
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);
151 s->hsub = desc->log2_chroma_w;
152 s->vsub = desc->log2_chroma_h;
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;
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; \
167 av_log(NULL, AV_LOG_ERROR, \
168 "Error when evaluating " #comp " radius expression '%s'\n", expr); \
171 EVAL_RADIUS_EXPR(luma);
172 EVAL_RADIUS_EXPR(chroma);
173 EVAL_RADIUS_EXPR(alpha);
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",
180 s->luma_param .radius, s->luma_param .power,
181 s->chroma_param.radius, s->chroma_param.power,
182 s->alpha_param .radius, s->alpha_param .power,
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); \
193 CHECK_RADIUS_VAL(w, h, luma);
194 CHECK_RADIUS_VAL(cw, ch, chroma);
195 CHECK_RADIUS_VAL(w, h, alpha);
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;
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;
208 static inline void blur(uint8_t *dst, int dst_step, const uint8_t *src, int src_step,
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:
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.
225 const int length = radius*2 + 1;
226 const int inv = ((1<<16) + length/2)/length;
229 for (x = 0; x < radius; x++)
230 sum += src[x*src_step]<<1;
231 sum += src[radius*src_step];
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;
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;
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;
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])
252 uint8_t *a = temp[0], *b = temp[1];
254 if (radius && power) {
255 blur(a, 1, src, src_step, len, radius);
256 for (; power > 2; power--) {
258 blur(b, 1, a, 1, len, radius);
262 blur(dst, dst_step, a, 1, len, radius);
265 for (i = 0; i < len; i++)
266 dst[i*dst_step] = a[i];
270 for (i = 0; i < len; i++)
271 dst[i*dst_step] = src[i*src_step];
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])
280 if (radius == 0 && dst == src)
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);
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])
293 if (radius == 0 && dst == src)
296 for (x = 0; x < w; x++)
297 blur_power(dst + x, dst_linesize, src + x, src_linesize,
298 h, radius, power, temp);
301 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
303 AVFilterContext *ctx = inlink->dst;
304 BoxBlurContext *s = ctx->priv;
305 AVFilterLink *outlink = inlink->dst->outputs[0];
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 };
312 out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
315 return AVERROR(ENOMEM);
317 av_frame_copy_props(out, in);
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],
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],
333 return ff_filter_frame(outlink, out);
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 },
351 static const AVClass boxblur_class = {
352 .class_name = "boxblur",
353 .item_name = av_default_item_name,
355 .version = LIBAVUTIL_VERSION_INT,
358 static const AVFilterPad avfilter_vf_boxblur_inputs[] = {
361 .type = AVMEDIA_TYPE_VIDEO,
362 .config_props = config_input,
363 .filter_frame = filter_frame,
368 static const AVFilterPad avfilter_vf_boxblur_outputs[] = {
371 .type = AVMEDIA_TYPE_VIDEO,
376 AVFilter avfilter_vf_boxblur = {
378 .description = NULL_IF_CONFIG_SMALL("Blur the input."),
379 .priv_size = sizeof(BoxBlurContext),
380 .priv_class = &boxblur_class,
383 .query_formats = query_formats,
385 .inputs = avfilter_vf_boxblur_inputs,
386 .outputs = avfilter_vf_boxblur_outputs,