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