Browse code

avfilter: add gblur filter

Signed-off-by: Paul B Mahol <onemda@gmail.com>

Paul B Mahol authored on 2016/09/01 08:18:45
Showing 6 changed files
... ...
@@ -25,6 +25,7 @@ version <next>:
25 25
 - vaguedenoiser filter
26 26
 - added threads option per filter instance
27 27
 - weave filter
28
+- gblur filter
28 29
 
29 30
 
30 31
 version 3.1:
... ...
@@ -8251,6 +8251,23 @@ option may cause flicker since the B-Frames have often larger QP. Default is
8251 8251
 
8252 8252
 @end table
8253 8253
 
8254
+@section gblur
8255
+
8256
+Apply Gaussian blur filter.
8257
+
8258
+The filter accepts the following options:
8259
+
8260
+@table @option
8261
+@item sigma
8262
+Set sigma, standard deviation of Gaussian blur. Default is @code{0.5}.
8263
+
8264
+@item steps
8265
+Set number of steps for Gaussian approximation. Defauls is @code{1}.
8266
+
8267
+@item planes
8268
+Set which planes to filter. By default all planes are filtered.
8269
+@end table
8270
+
8254 8271
 @section geq
8255 8272
 
8256 8273
 The filter accepts the following options:
... ...
@@ -182,6 +182,7 @@ OBJS-$(CONFIG_FRAMERATE_FILTER)              += vf_framerate.o
182 182
 OBJS-$(CONFIG_FRAMESTEP_FILTER)              += vf_framestep.o
183 183
 OBJS-$(CONFIG_FREI0R_FILTER)                 += vf_frei0r.o
184 184
 OBJS-$(CONFIG_FSPP_FILTER)                   += vf_fspp.o
185
+OBJS-$(CONFIG_GBLUR_FILTER)                  += vf_gblur.o
185 186
 OBJS-$(CONFIG_GEQ_FILTER)                    += vf_geq.o
186 187
 OBJS-$(CONFIG_GRADFUN_FILTER)                += vf_gradfun.o
187 188
 OBJS-$(CONFIG_HALDCLUT_FILTER)               += vf_lut3d.o dualinput.o framesync.o
... ...
@@ -199,6 +199,7 @@ void avfilter_register_all(void)
199 199
     REGISTER_FILTER(FRAMESTEP,      framestep,      vf);
200 200
     REGISTER_FILTER(FREI0R,         frei0r,         vf);
201 201
     REGISTER_FILTER(FSPP,           fspp,           vf);
202
+    REGISTER_FILTER(GBLUR,          gblur,          vf);
202 203
     REGISTER_FILTER(GEQ,            geq,            vf);
203 204
     REGISTER_FILTER(GRADFUN,        gradfun,        vf);
204 205
     REGISTER_FILTER(HALDCLUT,       haldclut,       vf);
... ...
@@ -30,7 +30,7 @@
30 30
 #include "libavutil/version.h"
31 31
 
32 32
 #define LIBAVFILTER_VERSION_MAJOR   6
33
-#define LIBAVFILTER_VERSION_MINOR  59
33
+#define LIBAVFILTER_VERSION_MINOR  60
34 34
 #define LIBAVFILTER_VERSION_MICRO 100
35 35
 
36 36
 #define LIBAVFILTER_VERSION_INT AV_VERSION_INT(LIBAVFILTER_VERSION_MAJOR, \
37 37
new file mode 100644
... ...
@@ -0,0 +1,352 @@
0
+/*
1
+ * Copyright (c) 2011 Pascal Getreuer
2
+ * Copyright (c) 2016 Paul B Mahol
3
+ *
4
+ * Redistribution and use in source and binary forms, with or without modification,
5
+ * are permitted provided that the following conditions are met:
6
+ *
7
+ *  * Redistributions of source code must retain the above copyright
8
+ *    notice, this list of conditions and the following disclaimer.
9
+ *  * Redistributions in binary form must reproduce the above
10
+ *    copyright notice, this list of conditions and the following
11
+ *    disclaimer in the documentation and/or other materials provided
12
+ *    with the distribution.
13
+ *
14
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
15
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
16
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
17
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
18
+ * HOLDER BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
22
+ * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
23
+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25
+ */
26
+
27
+#include "libavutil/imgutils.h"
28
+#include "libavutil/opt.h"
29
+#include "libavutil/pixdesc.h"
30
+#include "avfilter.h"
31
+#include "formats.h"
32
+#include "internal.h"
33
+#include "video.h"
34
+
35
+typedef struct GBlurContext {
36
+    const AVClass *class;
37
+
38
+    float sigma;
39
+    int steps;
40
+    int planes;
41
+
42
+    int depth;
43
+    int planewidth[4];
44
+    int planeheight[4];
45
+    float *buffer;
46
+    float boundaryscale;
47
+    float postscale;
48
+    float nu;
49
+    int nb_planes;
50
+} GBlurContext;
51
+
52
+#define OFFSET(x) offsetof(GBlurContext, x)
53
+#define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
54
+
55
+static const AVOption gblur_options[] = {
56
+    { "sigma",  "set sigma",            OFFSET(sigma),  AV_OPT_TYPE_FLOAT, {.dbl=0.5}, 0.0, 1024, FLAGS },
57
+    { "steps",  "set number of steps",  OFFSET(steps),  AV_OPT_TYPE_INT,   {.i64=1},     1,    6, FLAGS },
58
+    { "planes", "set planes to filter", OFFSET(planes), AV_OPT_TYPE_INT,   {.i64=0xF},   0,  0xF, FLAGS },
59
+    { NULL }
60
+};
61
+
62
+AVFILTER_DEFINE_CLASS(gblur);
63
+
64
+typedef struct ThreadData {
65
+    int height;
66
+    int width;
67
+} ThreadData;
68
+
69
+static int filter_horizontally(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
70
+{
71
+    GBlurContext *s = ctx->priv;
72
+    ThreadData *td = arg;
73
+    const int height = td->height;
74
+    const int width = td->width;
75
+    const int slice_start = (height *  jobnr   ) / nb_jobs;
76
+    const int slice_end   = (height * (jobnr+1)) / nb_jobs;
77
+    const float boundaryscale = s->boundaryscale;
78
+    const int steps = s->steps;
79
+    const float nu = s->nu;
80
+    float *buffer = s->buffer;
81
+    int y, x, step;
82
+    float *ptr;
83
+
84
+    /* Filter horizontally along each row */
85
+    for (y = slice_start; y < slice_end; y++) {
86
+        for (step = 0; step < steps; step++) {
87
+            ptr = buffer + width * y;
88
+            ptr[0] *= boundaryscale;
89
+
90
+            /* Filter rightwards */
91
+            for (x = 1; x < width; x++)
92
+                ptr[x] += nu * ptr[x - 1];
93
+
94
+            ptr[x = width - 1] *= boundaryscale;
95
+
96
+            /* Filter leftwards */
97
+            for (; x > 0; x--)
98
+                ptr[x - 1] += nu * ptr[x];
99
+        }
100
+    }
101
+
102
+    return 0;
103
+}
104
+
105
+static int filter_vertically(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
106
+{
107
+    GBlurContext *s = ctx->priv;
108
+    ThreadData *td = arg;
109
+    const int height = td->height;
110
+    const int width = td->width;
111
+    const int slice_start = (width *  jobnr   ) / nb_jobs;
112
+    const int slice_end   = (width * (jobnr+1)) / nb_jobs;
113
+    const float boundaryscale = s->boundaryscale;
114
+    const int numpixels = width * height;
115
+    const int steps = s->steps;
116
+    const float nu = s->nu;
117
+    float *buffer = s->buffer;
118
+    int i, x, step;
119
+    float *ptr;
120
+
121
+    /* Filter vertically along each column */
122
+    for (x = slice_start; x < slice_end; x++) {
123
+        for (step = 0; step < steps; step++) {
124
+            ptr = buffer + x;
125
+            ptr[0] *= boundaryscale;
126
+
127
+            /* Filter downwards */
128
+            for (i = width; i < numpixels; i += width)
129
+                ptr[i] += nu * ptr[i - width];
130
+
131
+            ptr[i = numpixels - width] *= boundaryscale;
132
+
133
+            /* Filter upwards */
134
+            for (; i > 0; i -= width)
135
+                ptr[i - width] += nu * ptr[i];
136
+        }
137
+    }
138
+
139
+    return 0;
140
+}
141
+
142
+
143
+static int filter_postscale(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
144
+{
145
+    GBlurContext *s = ctx->priv;
146
+    ThreadData *td = arg;
147
+    const int height = td->height;
148
+    const int width = td->width;
149
+    const int64_t numpixels = width * height;
150
+    const int slice_start = (numpixels *  jobnr   ) / nb_jobs;
151
+    const int slice_end   = (numpixels * (jobnr+1)) / nb_jobs;
152
+    const float postscale = s->postscale;
153
+    float *buffer = s->buffer;
154
+    int i;
155
+
156
+    for (i = slice_start; i < slice_end; i++)
157
+        buffer[i] *= postscale;
158
+
159
+    return 0;
160
+}
161
+
162
+static void gaussianiir2d(AVFilterContext *ctx, int plane)
163
+{
164
+    GBlurContext *s = ctx->priv;
165
+    const int width = s->planewidth[plane];
166
+    const int height = s->planeheight[plane];
167
+    const int nb_threads = ff_filter_get_nb_threads(ctx);
168
+    ThreadData td;
169
+
170
+    if (s->sigma <= 0 || s->steps < 0)
171
+        return;
172
+
173
+    td.width = width;
174
+    td.height = height;
175
+    ctx->internal->execute(ctx, filter_horizontally, &td, NULL, FFMIN(height, nb_threads));
176
+    ctx->internal->execute(ctx, filter_vertically, &td, NULL, FFMIN(width, nb_threads));
177
+    ctx->internal->execute(ctx, filter_postscale, &td, NULL, FFMIN(width * height, nb_threads));
178
+}
179
+
180
+static int query_formats(AVFilterContext *ctx)
181
+{
182
+    static const enum AVPixelFormat pix_fmts[] = {
183
+        AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV440P,
184
+        AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P,
185
+        AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUV420P,
186
+        AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ420P,
187
+        AV_PIX_FMT_YUVJ411P, AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P,
188
+        AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV444P9,
189
+        AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10,
190
+        AV_PIX_FMT_YUV420P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV444P12, AV_PIX_FMT_YUV440P12,
191
+        AV_PIX_FMT_YUV420P14, AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV444P14,
192
+        AV_PIX_FMT_YUV420P16, AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV444P16,
193
+        AV_PIX_FMT_YUVA420P9, AV_PIX_FMT_YUVA422P9, AV_PIX_FMT_YUVA444P9,
194
+        AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA444P10,
195
+        AV_PIX_FMT_YUVA420P16, AV_PIX_FMT_YUVA422P16, AV_PIX_FMT_YUVA444P16,
196
+        AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRP9, AV_PIX_FMT_GBRP10,
197
+        AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRP14, AV_PIX_FMT_GBRP16,
198
+        AV_PIX_FMT_GBRAP, AV_PIX_FMT_GBRAP12, AV_PIX_FMT_GBRAP16,
199
+        AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY16,
200
+        AV_PIX_FMT_NONE
201
+    };
202
+
203
+    return ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
204
+}
205
+
206
+static int config_input(AVFilterLink *inlink)
207
+{
208
+    const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
209
+    GBlurContext *s = inlink->dst->priv;
210
+
211
+    s->depth = desc->comp[0].depth;
212
+    s->planewidth[1] = s->planewidth[2] = AV_CEIL_RSHIFT(inlink->w, desc->log2_chroma_w);
213
+    s->planewidth[0] = s->planewidth[3] = inlink->w;
214
+    s->planeheight[1] = s->planeheight[2] = AV_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h);
215
+    s->planeheight[0] = s->planeheight[3] = inlink->h;
216
+
217
+    s->nb_planes = av_pix_fmt_count_planes(inlink->format);
218
+
219
+    s->buffer = av_malloc_array(inlink->w, inlink->h * sizeof(*s->buffer));
220
+    if (!s->buffer)
221
+        return AVERROR(ENOMEM);
222
+    return 0;
223
+}
224
+
225
+static int filter_frame(AVFilterLink *inlink, AVFrame *in)
226
+{
227
+    AVFilterContext *ctx = inlink->dst;
228
+    GBlurContext *s = ctx->priv;
229
+    AVFilterLink *outlink = ctx->outputs[0];
230
+    double dnu, lambda;
231
+    float sigma = s->sigma;
232
+    int steps = s->steps;
233
+    AVFrame *out;
234
+    int plane;
235
+
236
+    lambda = (sigma * sigma) / (2.0 * steps);
237
+    dnu = (1.0 + 2.0 * lambda - sqrt(1.0 + 4.0 * lambda)) / (2.0 * lambda);
238
+    s->postscale = pow(dnu / lambda, 2 * steps);
239
+    s->boundaryscale = 1.0 / (1.0 - dnu);
240
+    s->nu = (float)dnu;
241
+
242
+    if (av_frame_is_writable(in)) {
243
+        out = in;
244
+    } else {
245
+        out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
246
+        if (!out) {
247
+            av_frame_free(&in);
248
+            return AVERROR(ENOMEM);
249
+        }
250
+        av_frame_copy_props(out, in);
251
+    }
252
+
253
+    for (plane = 0; plane < s->nb_planes; plane++) {
254
+        const int height = s->planeheight[plane];
255
+        const int width = s->planewidth[plane];
256
+        float *bptr = s->buffer;
257
+        const uint8_t *src = in->data[plane];
258
+        const uint16_t *src16 = (const uint16_t *)in->data[plane];
259
+        uint8_t *dst = out->data[plane];
260
+        uint16_t *dst16 = (uint16_t *)out->data[plane];
261
+        int y, x;
262
+
263
+        if (!s->sigma || !(s->planes & (1 << plane))) {
264
+            if (out != in)
265
+                av_image_copy_plane(out->data[plane], out->linesize[plane],
266
+                                    in->data[plane], in->linesize[plane],
267
+                                    width * ((s->depth + 7) / 8), height);
268
+            continue;
269
+        }
270
+
271
+        if (s->depth == 8) {
272
+            for (y = 0; y < height; y++) {
273
+                for (x = 0; x < width; x++) {
274
+                    bptr[x] = src[x];
275
+                }
276
+                bptr += width;
277
+                src += in->linesize[plane];
278
+            }
279
+        } else {
280
+            for (y = 0; y < height; y++) {
281
+                for (x = 0; x < width; x++) {
282
+                    bptr[x] = src16[x];
283
+                }
284
+                bptr += width;
285
+                src16 += in->linesize[plane] / 2;
286
+            }
287
+        }
288
+
289
+        gaussianiir2d(ctx, plane);
290
+
291
+        bptr = s->buffer;
292
+        if (s->depth == 8) {
293
+            for (y = 0; y < height; y++) {
294
+                for (x = 0; x < width; x++) {
295
+                    dst[x] = bptr[x];
296
+                }
297
+                bptr += width;
298
+                dst += out->linesize[plane];
299
+            }
300
+        } else {
301
+            for (y = 0; y < height; y++) {
302
+                for (x = 0; x < width; x++) {
303
+                    dst16[x] = bptr[x];
304
+                }
305
+                bptr += width;
306
+                dst16 += out->linesize[plane] / 2;
307
+            }
308
+        }
309
+    }
310
+
311
+    if (out != in)
312
+        av_frame_free(&in);
313
+    return ff_filter_frame(outlink, out);
314
+}
315
+
316
+static av_cold void uninit(AVFilterContext *ctx)
317
+{
318
+    GBlurContext *s = ctx->priv;
319
+
320
+    av_freep(&s->buffer);
321
+}
322
+
323
+static const AVFilterPad gblur_inputs[] = {
324
+    {
325
+        .name         = "default",
326
+        .type         = AVMEDIA_TYPE_VIDEO,
327
+        .config_props = config_input,
328
+        .filter_frame = filter_frame,
329
+    },
330
+    { NULL }
331
+};
332
+
333
+static const AVFilterPad gblur_outputs[] = {
334
+    {
335
+        .name = "default",
336
+        .type = AVMEDIA_TYPE_VIDEO,
337
+    },
338
+    { NULL }
339
+};
340
+
341
+AVFilter ff_vf_gblur = {
342
+    .name          = "gblur",
343
+    .description   = NULL_IF_CONFIG_SMALL("Apply Gaussian Blur filter."),
344
+    .priv_size     = sizeof(GBlurContext),
345
+    .priv_class    = &gblur_class,
346
+    .uninit        = uninit,
347
+    .query_formats = query_formats,
348
+    .inputs        = gblur_inputs,
349
+    .outputs       = gblur_outputs,
350
+    .flags         = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS,
351
+};