]> git.sesse.net Git - ffmpeg/blob - libavfilter/vf_boxblur.c
Merge remote-tracking branch 'qatar/master'
[ffmpeg] / libavfilter / vf_boxblur.c
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/eval.h"
30 #include "libavutil/pixdesc.h"
31 #include "avfilter.h"
32 #include "formats.h"
33 #include "video.h"
34
35 static const char *const var_names[] = {
36     "w",
37     "h",
38     "cw",
39     "ch",
40     "hsub",
41     "vsub",
42     NULL
43 };
44
45 enum var_name {
46     VAR_W,
47     VAR_H,
48     VAR_CW,
49     VAR_CH,
50     VAR_HSUB,
51     VAR_VSUB,
52     VARS_NB
53 };
54
55 typedef struct {
56     int radius;
57     int power;
58 } FilterParam;
59
60 typedef struct {
61     FilterParam luma_param;
62     FilterParam chroma_param;
63     FilterParam alpha_param;
64     char luma_radius_expr  [256];
65     char chroma_radius_expr[256];
66     char alpha_radius_expr [256];
67
68     int hsub, vsub;
69     int radius[4];
70     int power[4];
71     uint8_t *temp[2]; ///< temporary buffer used in blur_power()
72 } BoxBlurContext;
73
74 #define Y 0
75 #define U 1
76 #define V 2
77 #define A 3
78
79 static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
80 {
81     BoxBlurContext *boxblur = ctx->priv;
82     int e;
83
84     if (!args) {
85         av_log(ctx, AV_LOG_ERROR,
86                "Filter expects 2 or 4 or 6 arguments, none provided\n");
87         return AVERROR(EINVAL);
88     }
89
90     e = sscanf(args, "%255[^:]:%d:%255[^:]:%d:%255[^:]:%d",
91                boxblur->luma_radius_expr,   &boxblur->luma_param  .power,
92                boxblur->chroma_radius_expr, &boxblur->chroma_param.power,
93                boxblur->alpha_radius_expr,  &boxblur->alpha_param .power);
94
95     if (e != 2 && e != 4 && e != 6) {
96         av_log(ctx, AV_LOG_ERROR,
97                "Filter expects 2 or 4 or 6 params, provided %d\n", e);
98         return AVERROR(EINVAL);
99     }
100
101     if (e < 4) {
102         boxblur->chroma_param.power = boxblur->luma_param.power;
103         av_strlcpy(boxblur->chroma_radius_expr, boxblur->luma_radius_expr,
104                    sizeof(boxblur->chroma_radius_expr));
105     }
106     if (e < 6) {
107         boxblur->alpha_param.power = boxblur->luma_param.power;
108         av_strlcpy(boxblur->alpha_radius_expr, boxblur->luma_radius_expr,
109                    sizeof(boxblur->alpha_radius_expr));
110     }
111
112     return 0;
113 }
114
115 static av_cold void uninit(AVFilterContext *ctx)
116 {
117     BoxBlurContext *boxblur = ctx->priv;
118
119     av_freep(&boxblur->temp[0]);
120     av_freep(&boxblur->temp[1]);
121 }
122
123 static int query_formats(AVFilterContext *ctx)
124 {
125     enum PixelFormat pix_fmts[] = {
126         PIX_FMT_YUV444P,  PIX_FMT_YUV422P,  PIX_FMT_YUV420P,
127         PIX_FMT_YUV411P,  PIX_FMT_YUV410P,  PIX_FMT_YUVA420P,
128         PIX_FMT_YUV440P,  PIX_FMT_GRAY8,
129         PIX_FMT_YUVJ444P, PIX_FMT_YUVJ422P, PIX_FMT_YUVJ420P,
130         PIX_FMT_YUVJ440P,
131         PIX_FMT_NONE
132     };
133
134     ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
135     return 0;
136 }
137
138 static int config_input(AVFilterLink *inlink)
139 {
140     AVFilterContext *ctx = inlink->dst;
141     BoxBlurContext *boxblur = ctx->priv;
142     const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[inlink->format];
143     int w = inlink->w, h = inlink->h;
144     int cw, ch;
145     double var_values[VARS_NB], res;
146     char *expr;
147     int ret;
148
149     if (!(boxblur->temp[0] = av_malloc(FFMAX(w, h))) ||
150         !(boxblur->temp[1] = av_malloc(FFMAX(w, h))))
151         return AVERROR(ENOMEM);
152
153     boxblur->hsub = desc->log2_chroma_w;
154     boxblur->vsub = desc->log2_chroma_h;
155
156     var_values[VAR_W]  = inlink->w;
157     var_values[VAR_H]  = inlink->h;
158     var_values[VAR_CW] = cw = w>>boxblur->hsub;
159     var_values[VAR_CH] = ch = h>>boxblur->vsub;
160     var_values[VAR_HSUB] = 1<<boxblur->hsub;
161     var_values[VAR_VSUB] = 1<<boxblur->vsub;
162
163 #define EVAL_RADIUS_EXPR(comp)                                          \
164     expr = boxblur->comp##_radius_expr;                                 \
165     ret = av_expr_parse_and_eval(&res, expr, var_names, var_values,     \
166                                  NULL, NULL, NULL, NULL, NULL, 0, ctx); \
167     boxblur->comp##_param.radius = res;                                 \
168     if (ret < 0) {                                                      \
169         av_log(NULL, AV_LOG_ERROR,                                      \
170                "Error when evaluating " #comp " radius expression '%s'\n", expr); \
171         return ret;                                                     \
172     }
173     EVAL_RADIUS_EXPR(luma);
174     EVAL_RADIUS_EXPR(chroma);
175     EVAL_RADIUS_EXPR(alpha);
176
177     av_log(ctx, AV_LOG_INFO,
178            "luma_radius:%d luma_power:%d "
179            "chroma_radius:%d chroma_power:%d "
180            "alpha_radius:%d alpha_power:%d "
181            "w:%d chroma_w:%d h:%d chroma_h:%d\n",
182            boxblur->luma_param  .radius, boxblur->luma_param  .power,
183            boxblur->chroma_param.radius, boxblur->chroma_param.power,
184            boxblur->alpha_param .radius, boxblur->alpha_param .power,
185            w, cw, h, ch);
186
187 #define CHECK_RADIUS_VAL(w_, h_, comp)                                  \
188     if (boxblur->comp##_param.radius < 0 ||                             \
189         2*boxblur->comp##_param.radius > FFMIN(w_, h_)) {               \
190         av_log(ctx, AV_LOG_ERROR,                                       \
191                "Invalid " #comp " radius value %d, must be >= 0 and <= %d\n", \
192                boxblur->comp##_param.radius, FFMIN(w_, h_)/2);          \
193         return AVERROR(EINVAL);                                         \
194     }
195     CHECK_RADIUS_VAL(w,  h,  luma);
196     CHECK_RADIUS_VAL(cw, ch, chroma);
197     CHECK_RADIUS_VAL(w,  h,  alpha);
198
199     boxblur->radius[Y] = boxblur->luma_param.radius;
200     boxblur->radius[U] = boxblur->radius[V] = boxblur->chroma_param.radius;
201     boxblur->radius[A] = boxblur->alpha_param.radius;
202
203     boxblur->power[Y] = boxblur->luma_param.power;
204     boxblur->power[U] = boxblur->power[V] = boxblur->chroma_param.power;
205     boxblur->power[A] = boxblur->alpha_param.power;
206
207     return 0;
208 }
209
210 static inline void blur(uint8_t *dst, int dst_step, const uint8_t *src, int src_step,
211                         int len, int radius)
212 {
213     /* Naive boxblur would sum source pixels from x-radius .. x+radius
214      * for destination pixel x. That would be O(radius*width).
215      * If you now look at what source pixels represent 2 consecutive
216      * output pixels, then you see they are almost identical and only
217      * differ by 2 pixels, like:
218      * src0       111111111
219      * dst0           1
220      * src1        111111111
221      * dst1            1
222      * src0-src1  1       -1
223      * so when you know one output pixel you can find the next by just adding
224      * and subtracting 1 input pixel.
225      * The following code adopts this faster variant.
226      */
227     int x, sum = 0;
228     const int length = radius*2 + 1;
229     const int inv = ((1<<16) + length/2)/length;
230
231     for (x = 0; x < radius; x++)
232         sum += src[x*src_step]<<1;
233     sum += src[radius*src_step];
234
235     for (x = 0; x <= radius; x++) {
236         sum += src[(radius+x)*src_step] - src[(radius-x)*src_step];
237         dst[x*dst_step] = (sum*inv + (1<<15))>>16;
238     }
239
240     for (; x < len-radius; x++) {
241         sum += src[(radius+x)*src_step] - src[(x-radius-1)*src_step];
242         dst[x*dst_step] = (sum*inv + (1<<15))>>16;
243     }
244
245     for (; x < len; x++) {
246         sum += src[(2*len-radius-x-1)*src_step] - src[(x-radius-1)*src_step];
247         dst[x*dst_step] = (sum*inv + (1<<15))>>16;
248     }
249 }
250
251 static inline void blur_power(uint8_t *dst, int dst_step, const uint8_t *src, int src_step,
252                               int len, int radius, int power, uint8_t *temp[2])
253 {
254     uint8_t *a = temp[0], *b = temp[1];
255
256     if (radius && power) {
257         blur(a, 1, src, src_step, len, radius);
258         for (; power > 2; power--) {
259             uint8_t *c;
260             blur(b, 1, a, 1, len, radius);
261             c = a; a = b; b = c;
262         }
263         if (power > 1) {
264             blur(dst, dst_step, a, 1, len, radius);
265         } else {
266             int i;
267             for (i = 0; i < len; i++)
268                 dst[i*dst_step] = a[i];
269         }
270     } else {
271         int i;
272         for (i = 0; i < len; i++)
273             dst[i*dst_step] = src[i*src_step];
274     }
275 }
276
277 static void hblur(uint8_t *dst, int dst_linesize, const uint8_t *src, int src_linesize,
278                   int w, int h, int radius, int power, uint8_t *temp[2])
279 {
280     int y;
281
282     if (radius == 0 && dst == src)
283         return;
284
285     for (y = 0; y < h; y++)
286         blur_power(dst + y*dst_linesize, 1, src + y*src_linesize, 1,
287                    w, radius, power, temp);
288 }
289
290 static void vblur(uint8_t *dst, int dst_linesize, const uint8_t *src, int src_linesize,
291                   int w, int h, int radius, int power, uint8_t *temp[2])
292 {
293     int x;
294
295     if (radius == 0 && dst == src)
296         return;
297
298     for (x = 0; x < w; x++)
299         blur_power(dst + x, dst_linesize, src + x, src_linesize,
300                    h, radius, power, temp);
301 }
302
303 static void null_draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir) { }
304
305 static void end_frame(AVFilterLink *inlink)
306 {
307     AVFilterContext *ctx = inlink->dst;
308     BoxBlurContext *boxblur = ctx->priv;
309     AVFilterLink *outlink = inlink->dst->outputs[0];
310     AVFilterBufferRef *inpicref  = inlink ->cur_buf;
311     AVFilterBufferRef *outpicref = outlink->out_buf;
312     int plane;
313     int cw = inlink->w >> boxblur->hsub, ch = inlink->h >> boxblur->vsub;
314     int w[4] = { inlink->w, cw, cw, inlink->w };
315     int h[4] = { inlink->h, ch, ch, inlink->h };
316
317     for (plane = 0; inpicref->data[plane] && plane < 4; plane++)
318         hblur(outpicref->data[plane], outpicref->linesize[plane],
319               inpicref ->data[plane], inpicref ->linesize[plane],
320               w[plane], h[plane], boxblur->radius[plane], boxblur->power[plane],
321               boxblur->temp);
322
323     for (plane = 0; inpicref->data[plane] && plane < 4; plane++)
324         vblur(outpicref->data[plane], outpicref->linesize[plane],
325               outpicref->data[plane], outpicref->linesize[plane],
326               w[plane], h[plane], boxblur->radius[plane], boxblur->power[plane],
327               boxblur->temp);
328
329     ff_draw_slice(outlink, 0, inlink->h, 1);
330     avfilter_default_end_frame(inlink);
331 }
332
333 AVFilter avfilter_vf_boxblur = {
334     .name          = "boxblur",
335     .description   = NULL_IF_CONFIG_SMALL("Blur the input."),
336     .priv_size     = sizeof(BoxBlurContext),
337     .init          = init,
338     .uninit        = uninit,
339     .query_formats = query_formats,
340
341     .inputs    = (const AVFilterPad[]) {{ .name       = "default",
342                                     .type             = AVMEDIA_TYPE_VIDEO,
343                                     .config_props     = config_input,
344                                     .draw_slice       = null_draw_slice,
345                                     .end_frame        = end_frame,
346                                     .min_perms        = AV_PERM_READ },
347                                   { .name = NULL}},
348     .outputs   = (const AVFilterPad[]) {{ .name       = "default",
349                                     .type             = AVMEDIA_TYPE_VIDEO, },
350                                   { .name = NULL}},
351 };