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