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