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/pixdesc.h"
37 static const char *const var_names[] = {
63 FilterParam luma_param;
64 FilterParam chroma_param;
65 FilterParam alpha_param;
66 char luma_radius_expr [256];
67 char chroma_radius_expr[256];
68 char alpha_radius_expr [256];
73 uint8_t *temp[2]; ///< temporary buffer used in blur_power()
81 static av_cold int init(AVFilterContext *ctx, const char *args)
83 BoxBlurContext *boxblur = ctx->priv;
87 av_log(ctx, AV_LOG_ERROR,
88 "Filter expects 2 or 4 or 6 arguments, none provided\n");
89 return AVERROR(EINVAL);
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);
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);
104 boxblur->chroma_param.power = boxblur->luma_param.power;
105 av_strlcpy(boxblur->chroma_radius_expr, boxblur->luma_radius_expr,
106 sizeof(boxblur->chroma_radius_expr));
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));
117 static av_cold void uninit(AVFilterContext *ctx)
119 BoxBlurContext *boxblur = ctx->priv;
121 av_freep(&boxblur->temp[0]);
122 av_freep(&boxblur->temp[1]);
125 static int query_formats(AVFilterContext *ctx)
127 enum AVPixelFormat pix_fmts[] = {
128 AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV420P,
129 AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUVA420P,
130 AV_PIX_FMT_YUV440P, AV_PIX_FMT_GRAY8,
131 AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ420P,
136 ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
140 static int config_input(AVFilterLink *inlink)
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;
147 double var_values[VARS_NB], res;
151 if (!(boxblur->temp[0] = av_malloc(FFMAX(w, h))) ||
152 !(boxblur->temp[1] = av_malloc(FFMAX(w, h))))
153 return AVERROR(ENOMEM);
155 boxblur->hsub = desc->log2_chroma_w;
156 boxblur->vsub = desc->log2_chroma_h;
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;
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; \
171 av_log(NULL, AV_LOG_ERROR, \
172 "Error when evaluating " #comp " radius expression '%s'\n", expr); \
175 EVAL_RADIUS_EXPR(luma);
176 EVAL_RADIUS_EXPR(chroma);
177 EVAL_RADIUS_EXPR(alpha);
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,
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); \
197 CHECK_RADIUS_VAL(w, h, luma);
198 CHECK_RADIUS_VAL(cw, ch, chroma);
199 CHECK_RADIUS_VAL(w, h, alpha);
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;
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;
212 static inline void blur(uint8_t *dst, int dst_step, const uint8_t *src, int src_step,
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:
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.
230 const int length = radius*2 + 1;
231 const int inv = ((1<<16) + length/2)/length;
233 for (x = 0; x < radius; x++)
234 sum += src[x*src_step]<<1;
235 sum += src[radius*src_step];
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;
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;
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;
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])
256 uint8_t *a = temp[0], *b = temp[1];
258 if (radius && power) {
259 blur(a, 1, src, src_step, len, radius);
260 for (; power > 2; power--) {
262 blur(b, 1, a, 1, len, radius);
266 blur(dst, dst_step, a, 1, len, radius);
269 for (i = 0; i < len; i++)
270 dst[i*dst_step] = a[i];
274 for (i = 0; i < len; i++)
275 dst[i*dst_step] = src[i*src_step];
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])
284 if (radius == 0 && dst == src)
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);
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])
297 if (radius == 0 && dst == src)
300 for (x = 0; x < w; x++)
301 blur_power(dst + x, dst_linesize, src + x, src_linesize,
302 h, radius, power, temp);
305 static int null_draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir) { return 0; }
307 static int end_frame(AVFilterLink *inlink)
309 AVFilterContext *ctx = inlink->dst;
310 BoxBlurContext *boxblur = ctx->priv;
311 AVFilterLink *outlink = inlink->dst->outputs[0];
312 AVFilterBufferRef *inpicref = inlink ->cur_buf;
313 AVFilterBufferRef *outpicref = outlink->out_buf;
315 int cw = inlink->w >> boxblur->hsub, ch = inlink->h >> boxblur->vsub;
316 int w[4] = { inlink->w, cw, cw, inlink->w };
317 int h[4] = { inlink->h, ch, ch, inlink->h };
319 for (plane = 0; inpicref->data[plane] && plane < 4; plane++)
320 hblur(outpicref->data[plane], outpicref->linesize[plane],
321 inpicref ->data[plane], inpicref ->linesize[plane],
322 w[plane], h[plane], boxblur->radius[plane], boxblur->power[plane],
325 for (plane = 0; inpicref->data[plane] && plane < 4; plane++)
326 vblur(outpicref->data[plane], outpicref->linesize[plane],
327 outpicref->data[plane], outpicref->linesize[plane],
328 w[plane], h[plane], boxblur->radius[plane], boxblur->power[plane],
331 ff_draw_slice(outlink, 0, inlink->h, 1);
332 return ff_end_frame(outlink);
335 static const AVFilterPad avfilter_vf_boxblur_inputs[] = {
338 .type = AVMEDIA_TYPE_VIDEO,
339 .config_props = config_input,
340 .draw_slice = null_draw_slice,
341 .end_frame = end_frame,
342 .min_perms = AV_PERM_READ
347 static const AVFilterPad avfilter_vf_boxblur_outputs[] = {
350 .type = AVMEDIA_TYPE_VIDEO,
355 AVFilter avfilter_vf_boxblur = {
357 .description = NULL_IF_CONFIG_SMALL("Blur the input."),
358 .priv_size = sizeof(BoxBlurContext),
361 .query_formats = query_formats,
363 .inputs = avfilter_vf_boxblur_inputs,
364 .outputs = avfilter_vf_boxblur_outputs,