Browse code

lavfi: port boxblur filter from libmpcodecs

With the following additions:
* support to gray format
* support to yuva420p format
* parametric luma/chroma/alpha radius
* consistency check on the radius values, avoid crashes with invalid values

Stefano Sabatini authored on 2011/07/10 01:13:10
Showing 7 changed files
... ...
@@ -4,6 +4,7 @@ releases are sorted from youngest to oldest.
4 4
 version next:
5 5
 
6 6
 - openal input device added
7
+- boxblur filter added
7 8
 
8 9
 
9 10
 version 0.8:
... ...
@@ -1497,6 +1497,7 @@ udp_protocol_deps="network"
1497 1497
 
1498 1498
 # filters
1499 1499
 blackframe_filter_deps="gpl"
1500
+boxblur_filter_deps="gpl"
1500 1501
 cropdetect_filter_deps="gpl"
1501 1502
 drawtext_filter_deps="libfreetype"
1502 1503
 frei0r_filter_deps="frei0r dlopen strtok_r"
... ...
@@ -183,6 +183,66 @@ threshold, and defaults to 98.
183 183
 @var{threshold} is the threshold below which a pixel value is
184 184
 considered black, and defaults to 32.
185 185
 
186
+@section boxblur
187
+
188
+Apply boxblur algorithm to the input video.
189
+
190
+This filter accepts the parameters:
191
+@var{luma_power}:@var{luma_radius}:@var{chroma_radius}:@var{chroma_power}:@var{alpha_radius}:@var{alpha_power}
192
+
193
+Chroma and alpha parameters are optional, if not specified they default
194
+to the corresponding values set for @var{luma_radius} and
195
+@var{luma_power}.
196
+
197
+@var{luma_radius}, @var{chroma_radius}, and @var{alpha_radius} represent
198
+the radius in pixels of the box used for blurring the corresponding
199
+input plane. They are expressions, and can contain the following
200
+constants:
201
+@table @option
202
+@item w, h
203
+the input width and heigth in pixels
204
+
205
+@item cw, ch
206
+the input chroma image width and height in pixels
207
+
208
+@item hsub, vsub
209
+horizontal and vertical chroma subsample values. For example for the
210
+pixel format "yuv422p" @var{hsub} is 2 and @var{vsub} is 1.
211
+@end table
212
+
213
+The radius must be a non-negative number, and must be not greater than
214
+the value of the expression @code{min(w,h)/2} for the luma and alpha planes,
215
+and of @code{min(cw,ch)/2} for the chroma planes.
216
+
217
+@var{luma_power}, @var{chroma_power}, and @var{alpha_power} represent
218
+how many times the boxblur filter is applied to the corresponding
219
+plane.
220
+
221
+Some examples follow:
222
+
223
+@itemize
224
+
225
+@item
226
+Apply a boxblur filter with luma, chroma, and alpha radius
227
+set to 2:
228
+@example
229
+boxblur=2:1
230
+@end example
231
+
232
+@item
233
+Set luma radius to 2, alpha and chroma radius to 0
234
+@example
235
+boxblur=2:1:0:0:0:0
236
+@end example
237
+
238
+@item
239
+Set luma and chroma radius to a fraction of the video dimension
240
+@example
241
+boxblur=min(h\,w)/10:1:min(cw\,ch)/10:1
242
+@end example
243
+
244
+@end itemize
245
+
186 246
 @section copy
187 247
 
188 248
 Copy the input source unchanged to the output. Mainly useful for
... ...
@@ -25,6 +25,7 @@ OBJS-$(CONFIG_ANULLSRC_FILTER)               += asrc_anullsrc.o
25 25
 OBJS-$(CONFIG_ANULLSINK_FILTER)              += asink_anullsink.o
26 26
 
27 27
 OBJS-$(CONFIG_BLACKFRAME_FILTER)             += vf_blackframe.o
28
+OBJS-$(CONFIG_BOXBLUR_FILTER)                += vf_boxblur.o
28 29
 OBJS-$(CONFIG_COPY_FILTER)                   += vf_copy.o
29 30
 OBJS-$(CONFIG_CROP_FILTER)                   += vf_crop.o
30 31
 OBJS-$(CONFIG_CROPDETECT_FILTER)             += vf_cropdetect.o
... ...
@@ -41,6 +41,7 @@ void avfilter_register_all(void)
41 41
     REGISTER_FILTER (ANULLSINK,   anullsink,   asink);
42 42
 
43 43
     REGISTER_FILTER (BLACKFRAME,  blackframe,  vf);
44
+    REGISTER_FILTER (BOXBLUR,     boxblur,     vf);
44 45
     REGISTER_FILTER (COPY,        copy,        vf);
45 46
     REGISTER_FILTER (CROP,        crop,        vf);
46 47
     REGISTER_FILTER (CROPDETECT,  cropdetect,  vf);
... ...
@@ -29,8 +29,8 @@
29 29
 #include "libavutil/rational.h"
30 30
 
31 31
 #define LIBAVFILTER_VERSION_MAJOR  2
32
-#define LIBAVFILTER_VERSION_MINOR 24
33
-#define LIBAVFILTER_VERSION_MICRO  4
32
+#define LIBAVFILTER_VERSION_MINOR 25
33
+#define LIBAVFILTER_VERSION_MICRO  0
34 34
 
35 35
 #define LIBAVFILTER_VERSION_INT AV_VERSION_INT(LIBAVFILTER_VERSION_MAJOR, \
36 36
                                                LIBAVFILTER_VERSION_MINOR, \
37 37
new file mode 100644
... ...
@@ -0,0 +1,350 @@
0
+/*
1
+ * Copyright (c) 2002 Michael Niedermayer <michaelni@gmx.at>
2
+ * Copyright (c) 2011 Stefano Sabatini
3
+ *
4
+ * This file is part of FFmpeg.
5
+ *
6
+ * FFmpeg is free software; you can redistribute it and/or modify
7
+ * it under the terms of the GNU General Public License as published by
8
+ * the Free Software Foundation; either version 2 of the License, or
9
+ * (at your option) any later version.
10
+ *
11
+ * FFmpeg is distributed in the hope that it will be useful,
12
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
+ * GNU General Public License for more details.
15
+ *
16
+ * You should have received a copy of the GNU General Public License along
17
+ * with FFmpeg; if not, write to the Free Software Foundation, Inc.,
18
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
+ */
20
+
21
+/**
22
+ * @file
23
+ * Apply a boxblur filter to the input video.
24
+ * Ported from MPlayer libmpcodecs/vf_boxblur.c.
25
+ */
26
+
27
+#include "libavutil/avstring.h"
28
+#include "libavutil/eval.h"
29
+#include "libavutil/pixdesc.h"
30
+#include "avfilter.h"
31
+
32
+static const char *var_names[] = {
33
+    "w",
34
+    "h",
35
+    "cw",
36
+    "ch",
37
+    "hsub",
38
+    "vsub",
39
+    NULL
40
+};
41
+
42
+enum var_name {
43
+    VAR_W,
44
+    VAR_H,
45
+    VAR_CW,
46
+    VAR_CH,
47
+    VAR_HSUB,
48
+    VAR_VSUB,
49
+    VARS_NB
50
+};
51
+
52
+typedef struct {
53
+    int radius;
54
+    int power;
55
+} FilterParam;
56
+
57
+typedef struct {
58
+    FilterParam luma_param;
59
+    FilterParam chroma_param;
60
+    FilterParam alpha_param;
61
+    char luma_radius_expr  [256];
62
+    char chroma_radius_expr[256];
63
+    char alpha_radius_expr [256];
64
+
65
+    int hsub, vsub;
66
+    int radius[4];
67
+    int power[4];
68
+    uint8_t *temp[2]; ///< temporary buffer used in blur_power()
69
+} BoxBlurContext;
70
+
71
+#define Y 0
72
+#define U 1
73
+#define V 2
74
+#define A 3
75
+
76
+static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
77
+{
78
+    BoxBlurContext *boxblur = ctx->priv;
79
+    int e;
80
+
81
+    if (!args) {
82
+        av_log(ctx, AV_LOG_ERROR,
83
+               "Filter expects 2 or 4 arguments, none provided\n");
84
+        return AVERROR(EINVAL);
85
+    }
86
+
87
+    e = sscanf(args, "%255[^:]:%d:%255[^:]:%d:%255[^:]:%d",
88
+               boxblur->luma_radius_expr,   &boxblur->luma_param  .power,
89
+               boxblur->chroma_radius_expr, &boxblur->chroma_param.power,
90
+               boxblur->alpha_radius_expr,  &boxblur->alpha_param .power);
91
+
92
+    if (e != 2 && e != 4 && e != 6) {
93
+        av_log(ctx, AV_LOG_ERROR,
94
+               "Filter expects 2 or 4 or 6 params, provided %d\n", e);
95
+        return AVERROR(EINVAL);
96
+    }
97
+
98
+    if (e < 4) {
99
+        boxblur->chroma_param.power = boxblur->luma_param.power;
100
+        av_strlcpy(boxblur->chroma_radius_expr, boxblur->luma_radius_expr,
101
+                   sizeof(boxblur->chroma_radius_expr));
102
+    }
103
+    if (e < 6) {
104
+        boxblur->alpha_param.power = boxblur->luma_param.power;
105
+        av_strlcpy(boxblur->alpha_radius_expr, boxblur->luma_radius_expr,
106
+                   sizeof(boxblur->alpha_radius_expr));
107
+    }
108
+
109
+    return 0;
110
+}
111
+
112
+static av_cold void uninit(AVFilterContext *ctx)
113
+{
114
+    BoxBlurContext *boxblur = ctx->priv;
115
+
116
+    av_freep(&boxblur->temp[0]);
117
+    av_freep(&boxblur->temp[1]);
118
+}
119
+
120
+static int query_formats(AVFilterContext *ctx)
121
+{
122
+    enum PixelFormat pix_fmts[] = {
123
+        PIX_FMT_YUV444P,  PIX_FMT_YUV422P,  PIX_FMT_YUV420P,
124
+        PIX_FMT_YUV411P,  PIX_FMT_YUV410P,  PIX_FMT_YUVA420P,
125
+        PIX_FMT_YUV440P,  PIX_FMT_GRAY8,
126
+        PIX_FMT_YUVJ444P, PIX_FMT_YUVJ422P, PIX_FMT_YUVJ420P,
127
+        PIX_FMT_YUVJ440P,
128
+        PIX_FMT_NONE
129
+    };
130
+
131
+    avfilter_set_common_pixel_formats(ctx, avfilter_make_format_list(pix_fmts));
132
+    return 0;
133
+}
134
+
135
+static int config_input(AVFilterLink *inlink)
136
+{
137
+    AVFilterContext *ctx = inlink->dst;
138
+    BoxBlurContext *boxblur = ctx->priv;
139
+    const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[inlink->format];
140
+    int w = inlink->w, h = inlink->h;
141
+    int cw, ch;
142
+    double var_values[VARS_NB], res;
143
+    char *expr;
144
+    int ret;
145
+
146
+    if (!(boxblur->temp[0] = av_malloc(w)) ||
147
+        !(boxblur->temp[1] = av_malloc(w)))
148
+        return AVERROR(ENOMEM);
149
+
150
+    boxblur->hsub = desc->log2_chroma_w;
151
+    boxblur->vsub = desc->log2_chroma_h;
152
+
153
+    var_values[VAR_W]  = inlink->w;
154
+    var_values[VAR_H]  = inlink->h;
155
+    var_values[VAR_CW] = cw = w>>boxblur->hsub;
156
+    var_values[VAR_CH] = ch = h>>boxblur->vsub;
157
+    var_values[VAR_HSUB] = 1<<boxblur->hsub;
158
+    var_values[VAR_VSUB] = 1<<boxblur->vsub;
159
+
160
+#define EVAL_RADIUS_EXPR(comp)                                          \
161
+    expr = boxblur->comp##_radius_expr;                                 \
162
+    ret = av_expr_parse_and_eval(&res, expr, var_names, var_values,     \
163
+                                 NULL, NULL, NULL, NULL, NULL, 0, ctx); \
164
+    boxblur->comp##_param.radius = res;                                 \
165
+    if (ret < 0) {                                                      \
166
+        av_log(NULL, AV_LOG_ERROR,                                      \
167
+               "Error when evaluating " #comp " radius expression '%s'\n", expr); \
168
+        return ret;                                                     \
169
+    }
170
+    EVAL_RADIUS_EXPR(luma);
171
+    EVAL_RADIUS_EXPR(chroma);
172
+    EVAL_RADIUS_EXPR(alpha);
173
+
174
+    av_log(ctx, AV_LOG_INFO,
175
+           "luma_radius:%d luma_power:%d "
176
+           "chroma_radius:%d chroma_power:%d "
177
+           "alpha_radius:%d alpha_power:%d "
178
+           "w:%d chroma_w:%d h:%d chroma_h:%d\n",
179
+           boxblur->luma_param  .radius, boxblur->luma_param  .power,
180
+           boxblur->chroma_param.radius, boxblur->chroma_param.power,
181
+           boxblur->alpha_param .radius, boxblur->alpha_param .power,
182
+           w, cw, h, ch);
183
+
184
+#define CHECK_RADIUS_VAL(w_, h_, comp)                                  \
185
+    if (boxblur->comp##_param.radius < 0 ||                             \
186
+        2*boxblur->comp##_param.radius > FFMIN(w_, h_)) {               \
187
+        av_log(ctx, AV_LOG_ERROR,                                       \
188
+               "Invalid " #comp " radius value %d, must be >= 0 and <= %d\n", \
189
+               boxblur->comp##_param.radius, FFMIN(w_, h_)/2);          \
190
+        return AVERROR(EINVAL);                                         \
191
+    }
192
+    CHECK_RADIUS_VAL(w,  h,  luma);
193
+    CHECK_RADIUS_VAL(cw, ch, chroma);
194
+    CHECK_RADIUS_VAL(w,  h,  alpha);
195
+
196
+    boxblur->radius[Y] = boxblur->luma_param.radius;
197
+    boxblur->radius[U] = boxblur->radius[V] = boxblur->chroma_param.radius;
198
+    boxblur->radius[A] = boxblur->alpha_param.radius;
199
+
200
+    boxblur->power[Y] = boxblur->luma_param.power;
201
+    boxblur->power[U] = boxblur->power[V] = boxblur->chroma_param.power;
202
+    boxblur->power[A] = boxblur->alpha_param.power;
203
+
204
+    return 0;
205
+}
206
+
207
+static inline void blur(uint8_t *dst, int dst_step, const uint8_t *src, int src_step,
208
+                        int w, int radius)
209
+{
210
+    /* Naive boxblur would sum source pixels from x-radius .. x+radius
211
+     * for destination pixel x. That would be O(radius*width).
212
+     * If you now look at what source pixels represent 2 consecutive
213
+     * output pixels, then you see they are almost identical and only
214
+     * differ by 2 pixels, like:
215
+     * src0       111111111
216
+     * dst0           1
217
+     * src1        111111111
218
+     * dst1            1
219
+     * src0-src1  1       -1
220
+     * so when you know one output pixel you can find the next by just adding
221
+     * and subtracting 1 input pixel.
222
+     * The following code adopts this faster variant.
223
+     */
224
+    int x, sum = 0;
225
+    const int length = radius*2 + 1;
226
+    const int inv = ((1<<16) + length/2)/length;
227
+
228
+    for (x = 0; x < radius; x++)
229
+        sum += src[x*src_step]<<1;
230
+    sum += src[radius*src_step];
231
+
232
+    for (x = 0; x <= radius; x++) {
233
+        sum += src[(radius+x)*src_step] - src[(radius-x)*src_step];
234
+        dst[x*dst_step] = (sum*inv + (1<<15))>>16;
235
+    }
236
+
237
+    for (; x < w-radius; x++) {
238
+        sum += src[(radius+x)*src_step] - src[(x-radius-1)*src_step];
239
+        dst[x*dst_step] = (sum*inv + (1<<15))>>16;
240
+    }
241
+
242
+    for (; x < w; x++) {
243
+        sum += src[(2*w-radius-x-1)*src_step] - src[(x-radius-1)*src_step];
244
+        dst[x*dst_step] = (sum*inv + (1<<15))>>16;
245
+    }
246
+}
247
+
248
+static inline void blur_power(uint8_t *dst, int dst_step, const uint8_t *src, int src_step,
249
+                              int w, int radius, int power, uint8_t *temp[2])
250
+{
251
+    uint8_t *a = temp[0], *b = temp[1];
252
+
253
+    if (radius && power) {
254
+        blur(a, 1, src, src_step, w, radius);
255
+        for (; power > 2; power--) {
256
+            uint8_t *c;
257
+            blur(b, 1, a, 1, w, radius);
258
+            c = a; a = b; b = c;
259
+        }
260
+        if (power > 1) {
261
+            blur(dst, dst_step, a, 1, w, radius);
262
+        } else {
263
+            int i;
264
+            for (i = 0; i < w; i++)
265
+                dst[i*dst_step] = a[i];
266
+        }
267
+    } else {
268
+        int i;
269
+        for (i = 0; i < w; i++)
270
+            dst[i*dst_step] = src[i*src_step];
271
+    }
272
+}
273
+
274
+static void hblur(uint8_t *dst, int dst_linesize, const uint8_t *src, int src_linesize,
275
+                  int w, int h, int radius, int power, uint8_t *temp[2])
276
+{
277
+    int y;
278
+
279
+    if (radius == 0 && dst == src)
280
+        return;
281
+
282
+    for (y = 0; y < h; y++)
283
+        blur_power(dst + y*dst_linesize, 1, src + y*src_linesize, 1,
284
+                   w, radius, power, temp);
285
+}
286
+
287
+static void vblur(uint8_t *dst, int dst_linesize, const uint8_t *src, int src_linesize,
288
+                  int w, int h, int radius, int power, uint8_t *temp[2])
289
+{
290
+    int x;
291
+
292
+    if (radius == 0 && dst == src)
293
+        return;
294
+
295
+    for (x = 0; x < w; x++)
296
+        blur_power(dst + x, dst_linesize, src + x, src_linesize,
297
+                   h, radius, power, temp);
298
+}
299
+
300
+static void draw_slice(AVFilterLink *inlink, int y0, int h0, int slice_dir)
301
+{
302
+    AVFilterContext *ctx = inlink->dst;
303
+    BoxBlurContext *boxblur = ctx->priv;
304
+    AVFilterLink *outlink = inlink->dst->outputs[0];
305
+    AVFilterBufferRef *inpicref  = inlink ->cur_buf;
306
+    AVFilterBufferRef *outpicref = outlink->out_buf;
307
+    int plane;
308
+    int cw = inlink->w >> boxblur->hsub, ch = h0 >> boxblur->vsub;
309
+    int w[4] = { inlink->w, cw, cw, inlink->w };
310
+    int h[4] = { h0, ch, ch, h0 };
311
+    uint8_t *dst[4], *src[4];
312
+
313
+    for (plane = 0; inpicref->data[plane] && plane < 4; plane++) {
314
+        int y = plane == 1 || plane == 2 ? y0 >> boxblur->vsub : y0;
315
+        src[plane] = inpicref ->data[plane] + inpicref ->linesize[plane] * y;
316
+        dst[plane] = outpicref->data[plane] + outpicref->linesize[plane] * y;
317
+    }
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
+
332
+AVFilter avfilter_vf_boxblur = {
333
+    .name          = "boxblur",
334
+    .description   = NULL_IF_CONFIG_SMALL("Blur the input."),
335
+    .priv_size     = sizeof(BoxBlurContext),
336
+    .init          = init,
337
+    .uninit        = uninit,
338
+    .query_formats = query_formats,
339
+
340
+    .inputs    = (AVFilterPad[]) {{ .name             = "default",
341
+                                    .type             = AVMEDIA_TYPE_VIDEO,
342
+                                    .config_props     = config_input,
343
+                                    .draw_slice       = draw_slice,
344
+                                    .min_perms        = AV_PERM_READ },
345
+                                  { .name = NULL}},
346
+    .outputs   = (AVFilterPad[]) {{ .name             = "default",
347
+                                    .type             = AVMEDIA_TYPE_VIDEO, },
348
+                                  { .name = NULL}},
349
+};