2 * Copyright (c) 2002 Michael Niedermayer <michaelni@gmx.at>
3 * Copyright (c) 2011 Stefano Sabatini
5 * This file is part of FFmpeg.
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.
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.
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.
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[] = {
58 typedef struct FilterParam {
64 typedef struct BoxBlurContext {
66 FilterParam luma_param;
67 FilterParam chroma_param;
68 FilterParam alpha_param;
73 uint8_t *temp[2]; ///< temporary buffer used in blur_power()
81 static av_cold int init(AVFilterContext *ctx)
83 BoxBlurContext *s = ctx->priv;
85 if (!s->luma_param.radius_expr) {
86 av_log(ctx, AV_LOG_ERROR, "Luma radius expression is not set.\n");
87 return AVERROR(EINVAL);
90 /* fill missing params */
91 if (!s->chroma_param.radius_expr) {
92 s->chroma_param.radius_expr = av_strdup(s->luma_param.radius_expr);
93 if (!s->chroma_param.radius_expr)
94 return AVERROR(ENOMEM);
96 if (s->chroma_param.power < 0)
97 s->chroma_param.power = s->luma_param.power;
99 if (!s->alpha_param.radius_expr) {
100 s->alpha_param.radius_expr = av_strdup(s->luma_param.radius_expr);
101 if (!s->alpha_param.radius_expr)
102 return AVERROR(ENOMEM);
104 if (s->alpha_param.power < 0)
105 s->alpha_param.power = s->luma_param.power;
110 static av_cold void uninit(AVFilterContext *ctx)
112 BoxBlurContext *s = ctx->priv;
114 av_freep(&s->temp[0]);
115 av_freep(&s->temp[1]);
118 static int query_formats(AVFilterContext *ctx)
120 AVFilterFormats *formats = NULL;
123 for (fmt = 0; av_pix_fmt_desc_get(fmt); fmt++) {
124 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(fmt);
125 if (!(desc->flags & (AV_PIX_FMT_FLAG_HWACCEL | AV_PIX_FMT_FLAG_BITSTREAM | AV_PIX_FMT_FLAG_PAL)) &&
126 (desc->flags & AV_PIX_FMT_FLAG_PLANAR || desc->nb_components == 1) &&
127 (!(desc->flags & AV_PIX_FMT_FLAG_BE) == !HAVE_BIGENDIAN || desc->comp[0].depth == 8) &&
128 (ret = ff_add_format(&formats, fmt)) < 0)
132 return ff_set_common_formats(ctx, formats);
135 static int config_input(AVFilterLink *inlink)
137 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
138 AVFilterContext *ctx = inlink->dst;
139 BoxBlurContext *s = ctx->priv;
140 int w = inlink->w, h = inlink->h;
142 double var_values[VARS_NB], res;
146 if (!(s->temp[0] = av_malloc(2*FFMAX(w, h))) ||
147 !(s->temp[1] = av_malloc(2*FFMAX(w, h))))
148 return AVERROR(ENOMEM);
150 s->hsub = desc->log2_chroma_w;
151 s->vsub = desc->log2_chroma_h;
153 var_values[VAR_W] = inlink->w;
154 var_values[VAR_H] = inlink->h;
155 var_values[VAR_CW] = cw = w>>s->hsub;
156 var_values[VAR_CH] = ch = h>>s->vsub;
157 var_values[VAR_HSUB] = 1<<s->hsub;
158 var_values[VAR_VSUB] = 1<<s->vsub;
160 #define EVAL_RADIUS_EXPR(comp) \
161 expr = s->comp##_param.radius_expr; \
162 ret = av_expr_parse_and_eval(&res, expr, var_names, var_values, \
163 NULL, NULL, NULL, NULL, NULL, 0, ctx); \
164 s->comp##_param.radius = res; \
166 av_log(NULL, AV_LOG_ERROR, \
167 "Error when evaluating " #comp " radius expression '%s'\n", expr); \
170 EVAL_RADIUS_EXPR(luma);
171 EVAL_RADIUS_EXPR(chroma);
172 EVAL_RADIUS_EXPR(alpha);
174 av_log(ctx, AV_LOG_VERBOSE,
175 "luma_radius:%d luma_power:%d "
176 "chroma_radius:%d chroma_power:%d "
177 "alpha_radius:%d alpha_power:%d "
178 "w:%d chroma_w:%d h:%d chroma_h:%d\n",
179 s->luma_param .radius, s->luma_param .power,
180 s->chroma_param.radius, s->chroma_param.power,
181 s->alpha_param .radius, s->alpha_param .power,
184 #define CHECK_RADIUS_VAL(w_, h_, comp) \
185 if (s->comp##_param.radius < 0 || \
186 2*s->comp##_param.radius > FFMIN(w_, h_)) { \
187 av_log(ctx, AV_LOG_ERROR, \
188 "Invalid " #comp " radius value %d, must be >= 0 and <= %d\n", \
189 s->comp##_param.radius, FFMIN(w_, h_)/2); \
190 return AVERROR(EINVAL); \
192 CHECK_RADIUS_VAL(w, h, luma);
193 CHECK_RADIUS_VAL(cw, ch, chroma);
194 CHECK_RADIUS_VAL(w, h, alpha);
196 s->radius[Y] = s->luma_param.radius;
197 s->radius[U] = s->radius[V] = s->chroma_param.radius;
198 s->radius[A] = s->alpha_param.radius;
200 s->power[Y] = s->luma_param.power;
201 s->power[U] = s->power[V] = s->chroma_param.power;
202 s->power[A] = s->alpha_param.power;
207 /* Naive boxblur would sum source pixels from x-radius .. x+radius
208 * for destination pixel x. That would be O(radius*width).
209 * If you now look at what source pixels represent 2 consecutive
210 * output pixels, then you see they are almost identical and only
211 * differ by 2 pixels, like:
217 * so when you know one output pixel you can find the next by just adding
218 * and subtracting 1 input pixel.
219 * The following code adopts this faster variant.
221 #define BLUR(type, depth) \
222 static inline void blur ## depth(type *dst, int dst_step, const type *src, \
223 int src_step, int len, int radius) \
225 const int length = radius*2 + 1; \
226 const int inv = ((1<<16) + length/2)/length; \
227 int x, sum = src[radius*src_step]; \
229 for (x = 0; x < radius; x++) \
230 sum += src[x*src_step]<<1; \
232 sum = sum*inv + (1<<15); \
234 for (x = 0; x <= radius; x++) { \
235 sum += (src[(radius+x)*src_step] - src[(radius-x)*src_step])*inv; \
236 dst[x*dst_step] = sum>>16; \
239 for (; x < len-radius; x++) { \
240 sum += (src[(radius+x)*src_step] - src[(x-radius-1)*src_step])*inv; \
241 dst[x*dst_step] = sum >>16; \
244 for (; x < len; x++) { \
245 sum += (src[(2*len-radius-x-1)*src_step] - src[(x-radius-1)*src_step])*inv; \
246 dst[x*dst_step] = sum>>16; \
255 static inline void blur(uint8_t *dst, int dst_step, const uint8_t *src, int src_step,
256 int len, int radius, int pixsize)
258 if (pixsize == 1) blur8 (dst, dst_step , src, src_step , len, radius);
259 else blur16((uint16_t*)dst, dst_step>>1, (const uint16_t*)src, src_step>>1, len, radius);
262 static inline void blur_power(uint8_t *dst, int dst_step, const uint8_t *src, int src_step,
263 int len, int radius, int power, uint8_t *temp[2], int pixsize)
265 uint8_t *a = temp[0], *b = temp[1];
267 if (radius && power) {
268 blur(a, pixsize, src, src_step, len, radius, pixsize);
269 for (; power > 2; power--) {
271 blur(b, pixsize, a, pixsize, len, radius, pixsize);
275 blur(dst, dst_step, a, pixsize, len, radius, pixsize);
279 for (i = 0; i < len; i++)
280 dst[i*dst_step] = a[i];
282 for (i = 0; i < len; i++)
283 *(uint16_t*)(dst + i*dst_step) = ((uint16_t*)a)[i];
288 for (i = 0; i < len; i++)
289 dst[i*dst_step] = src[i*src_step];
291 for (i = 0; i < len; i++)
292 *(uint16_t*)(dst + i*dst_step) = *(uint16_t*)(src + i*src_step);
296 static void hblur(uint8_t *dst, int dst_linesize, const uint8_t *src, int src_linesize,
297 int w, int h, int radius, int power, uint8_t *temp[2], int pixsize)
301 if (radius == 0 && dst == src)
304 for (y = 0; y < h; y++)
305 blur_power(dst + y*dst_linesize, pixsize, src + y*src_linesize, pixsize,
306 w, radius, power, temp, pixsize);
309 static void vblur(uint8_t *dst, int dst_linesize, const uint8_t *src, int src_linesize,
310 int w, int h, int radius, int power, uint8_t *temp[2], int pixsize)
314 if (radius == 0 && dst == src)
317 for (x = 0; x < w; x++)
318 blur_power(dst + x*pixsize, dst_linesize, src + x*pixsize, src_linesize,
319 h, radius, power, temp, pixsize);
322 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
324 AVFilterContext *ctx = inlink->dst;
325 BoxBlurContext *s = ctx->priv;
326 AVFilterLink *outlink = inlink->dst->outputs[0];
329 int cw = AV_CEIL_RSHIFT(inlink->w, s->hsub), ch = AV_CEIL_RSHIFT(in->height, s->vsub);
330 int w[4] = { inlink->w, cw, cw, inlink->w };
331 int h[4] = { in->height, ch, ch, in->height };
332 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
333 const int depth = desc->comp[0].depth;
334 const int pixsize = (depth+7)/8;
336 out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
339 return AVERROR(ENOMEM);
341 av_frame_copy_props(out, in);
343 for (plane = 0; plane < 4 && in->data[plane] && in->linesize[plane]; plane++)
344 hblur(out->data[plane], out->linesize[plane],
345 in ->data[plane], in ->linesize[plane],
346 w[plane], h[plane], s->radius[plane], s->power[plane],
349 for (plane = 0; plane < 4 && in->data[plane] && in->linesize[plane]; plane++)
350 vblur(out->data[plane], out->linesize[plane],
351 out->data[plane], out->linesize[plane],
352 w[plane], h[plane], s->radius[plane], s->power[plane],
357 return ff_filter_frame(outlink, out);
360 #define OFFSET(x) offsetof(BoxBlurContext, x)
361 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
363 static const AVOption boxblur_options[] = {
364 { "luma_radius", "Radius of the luma blurring box", OFFSET(luma_param.radius_expr), AV_OPT_TYPE_STRING, {.str="2"}, .flags = FLAGS },
365 { "lr", "Radius of the luma blurring box", OFFSET(luma_param.radius_expr), AV_OPT_TYPE_STRING, {.str="2"}, .flags = FLAGS },
366 { "luma_power", "How many times should the boxblur be applied to luma", OFFSET(luma_param.power), AV_OPT_TYPE_INT, {.i64=2}, 0, INT_MAX, .flags = FLAGS },
367 { "lp", "How many times should the boxblur be applied to luma", OFFSET(luma_param.power), AV_OPT_TYPE_INT, {.i64=2}, 0, INT_MAX, .flags = FLAGS },
369 { "chroma_radius", "Radius of the chroma blurring box", OFFSET(chroma_param.radius_expr), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
370 { "cr", "Radius of the chroma blurring box", OFFSET(chroma_param.radius_expr), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
371 { "chroma_power", "How many times should the boxblur be applied to chroma", OFFSET(chroma_param.power), AV_OPT_TYPE_INT, {.i64=-1}, -1, INT_MAX, .flags = FLAGS },
372 { "cp", "How many times should the boxblur be applied to chroma", OFFSET(chroma_param.power), AV_OPT_TYPE_INT, {.i64=-1}, -1, INT_MAX, .flags = FLAGS },
374 { "alpha_radius", "Radius of the alpha blurring box", OFFSET(alpha_param.radius_expr), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
375 { "ar", "Radius of the alpha blurring box", OFFSET(alpha_param.radius_expr), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
376 { "alpha_power", "How many times should the boxblur be applied to alpha", OFFSET(alpha_param.power), AV_OPT_TYPE_INT, {.i64=-1}, -1, INT_MAX, .flags = FLAGS },
377 { "ap", "How many times should the boxblur be applied to alpha", OFFSET(alpha_param.power), AV_OPT_TYPE_INT, {.i64=-1}, -1, INT_MAX, .flags = FLAGS },
382 AVFILTER_DEFINE_CLASS(boxblur);
384 static const AVFilterPad avfilter_vf_boxblur_inputs[] = {
387 .type = AVMEDIA_TYPE_VIDEO,
388 .config_props = config_input,
389 .filter_frame = filter_frame,
394 static const AVFilterPad avfilter_vf_boxblur_outputs[] = {
397 .type = AVMEDIA_TYPE_VIDEO,
402 AVFilter ff_vf_boxblur = {
404 .description = NULL_IF_CONFIG_SMALL("Blur the input."),
405 .priv_size = sizeof(BoxBlurContext),
406 .priv_class = &boxblur_class,
409 .query_formats = query_formats,
410 .inputs = avfilter_vf_boxblur_inputs,
411 .outputs = avfilter_vf_boxblur_outputs,
412 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,