Browse code

avfilter: add avgblur filter

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

Paul B Mahol authored on 2016/09/05 22:28:09
Showing 6 changed files
... ...
@@ -26,6 +26,7 @@ version <next>:
26 26
 - added threads option per filter instance
27 27
 - weave filter
28 28
 - gblur filter
29
+- avgblur filter
29 30
 
30 31
 
31 32
 version 3.1:
... ...
@@ -4402,6 +4402,24 @@ number in range [5, 129].
4402 4402
 Set what planes of frame filter will use for averaging. Default is all.
4403 4403
 @end table
4404 4404
 
4405
+@section avgblur
4406
+
4407
+Apply average blur filter.
4408
+
4409
+The filter accepts the following options:
4410
+
4411
+@table @option
4412
+@item sizeX
4413
+Set horizontal kernel size.
4414
+
4415
+@item planes
4416
+Set which planes to filter. By default all planes are filtered.
4417
+
4418
+@item sizeY
4419
+Set vertical kernel size, if zero it will be same as @code{sizeX}.
4420
+Default is @code{0}.
4421
+@end table
4422
+
4405 4423
 @section bbox
4406 4424
 
4407 4425
 Compute the bounding box for the non-black pixels in the input frame
... ...
@@ -124,6 +124,7 @@ OBJS-$(CONFIG_ALPHAEXTRACT_FILTER)           += vf_extractplanes.o
124 124
 OBJS-$(CONFIG_ALPHAMERGE_FILTER)             += vf_alphamerge.o
125 125
 OBJS-$(CONFIG_ASS_FILTER)                    += vf_subtitles.o
126 126
 OBJS-$(CONFIG_ATADENOISE_FILTER)             += vf_atadenoise.o
127
+OBJS-$(CONFIG_AVGBLUR_FILTER)                += vf_avgblur.o
127 128
 OBJS-$(CONFIG_BBOX_FILTER)                   += bbox.o vf_bbox.o
128 129
 OBJS-$(CONFIG_BENCH_FILTER)                  += f_bench.o
129 130
 OBJS-$(CONFIG_BITPLANENOISE_FILTER)          += vf_bitplanenoise.o
... ...
@@ -141,6 +141,7 @@ void avfilter_register_all(void)
141 141
     REGISTER_FILTER(ALPHAMERGE,     alphamerge,     vf);
142 142
     REGISTER_FILTER(ASS,            ass,            vf);
143 143
     REGISTER_FILTER(ATADENOISE,     atadenoise,     vf);
144
+    REGISTER_FILTER(AVGBLUR,        avgblur,        vf);
144 145
     REGISTER_FILTER(BBOX,           bbox,           vf);
145 146
     REGISTER_FILTER(BENCH,          bench,          vf);
146 147
     REGISTER_FILTER(BITPLANENOISE,  bitplanenoise,  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  60
33
+#define LIBAVFILTER_VERSION_MINOR  61
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,326 @@
0
+/*
1
+ * Copyright (c) 2016 Paul B Mahol
2
+ *
3
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ * of this software and associated documentation files (the "Software"), to deal
5
+ * in the Software without restriction, including without limitation the rights
6
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ * copies of the Software, and to permit persons to whom the Software is
8
+ * furnished to do so, subject to the following conditions:
9
+ *
10
+ * The above copyright notice and this permission notice shall be included in
11
+ * all copies or substantial portions of the Software.
12
+ *
13
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19
+ * SOFTWARE.
20
+ */
21
+
22
+#include "libavutil/imgutils.h"
23
+#include "libavutil/opt.h"
24
+#include "libavutil/pixdesc.h"
25
+#include "avfilter.h"
26
+#include "formats.h"
27
+#include "internal.h"
28
+#include "video.h"
29
+
30
+typedef struct AverageBlurContext {
31
+    const AVClass *class;
32
+
33
+    int radius;
34
+    int radiusV;
35
+    int planes;
36
+
37
+    int depth;
38
+    int planewidth[4];
39
+    int planeheight[4];
40
+    float *buffer;
41
+    int nb_planes;
42
+
43
+    int (*filter_horizontally)(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs);
44
+    int (*filter_vertically)(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs);
45
+} AverageBlurContext;
46
+
47
+#define OFFSET(x) offsetof(AverageBlurContext, x)
48
+#define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
49
+
50
+static const AVOption avgblur_options[] = {
51
+    { "sizeX",  "set horizontal size",  OFFSET(radius),  AV_OPT_TYPE_INT, {.i64=1},   1, 1024, FLAGS },
52
+    { "planes", "set planes to filter", OFFSET(planes),  AV_OPT_TYPE_INT, {.i64=0xF}, 0,  0xF, FLAGS },
53
+    { "sizeY",  "set vertical size",    OFFSET(radiusV), AV_OPT_TYPE_INT, {.i64=0},   0, 1024, FLAGS },
54
+    { NULL }
55
+};
56
+
57
+AVFILTER_DEFINE_CLASS(avgblur);
58
+
59
+typedef struct ThreadData {
60
+    int height;
61
+    int width;
62
+    uint8_t *ptr;
63
+    int linesize;
64
+} ThreadData;
65
+
66
+#define HORIZONTAL_FILTER(name, type)                                                         \
67
+static int filter_horizontally_##name(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)\
68
+{                                                                                             \
69
+    AverageBlurContext *s = ctx->priv;                                                        \
70
+    ThreadData *td = arg;                                                                     \
71
+    const int height = td->height;                                                            \
72
+    const int width = td->width;                                                              \
73
+    const int slice_start = (height *  jobnr   ) / nb_jobs;                                   \
74
+    const int slice_end   = (height * (jobnr+1)) / nb_jobs;                                   \
75
+    const int radius = FFMIN(s->radius, width / 2);                                           \
76
+    const int linesize = td->linesize / sizeof(type);                                         \
77
+    float *buffer = s->buffer;                                                                \
78
+    const type *src;                                                                          \
79
+    float *ptr;                                                                               \
80
+    int y, x;                                                                                 \
81
+                                                                                              \
82
+    /* Filter horizontally along each row */                                                  \
83
+    for (y = slice_start; y < slice_end; y++) {                                               \
84
+        float acc = 0;                                                                        \
85
+        int count = 0;                                                                        \
86
+                                                                                              \
87
+        src = (const type *)td->ptr + linesize * y;                                           \
88
+        ptr = buffer + width * y;                                                             \
89
+                                                                                              \
90
+        for (x = 0; x < radius; x++) {                                                        \
91
+            acc += src[x];                                                                    \
92
+        }                                                                                     \
93
+        count += radius;                                                                      \
94
+                                                                                              \
95
+        for (x = 0; x <= radius; x++) {                                                       \
96
+            acc += src[x + radius];                                                           \
97
+            count++;                                                                          \
98
+            ptr[x] = acc / count;                                                             \
99
+        }                                                                                     \
100
+                                                                                              \
101
+        for (; x < width - radius; x++) {                                                     \
102
+            acc += src[x + radius] - src[x - radius - 1];                                     \
103
+            ptr[x] = acc / count;                                                             \
104
+        }                                                                                     \
105
+                                                                                              \
106
+        for (; x < width; x++) {                                                              \
107
+            acc -= src[x - radius];                                                           \
108
+            count--;                                                                          \
109
+            ptr[x] = acc / count;                                                             \
110
+        }                                                                                     \
111
+    }                                                                                         \
112
+                                                                                              \
113
+    return 0;                                                                                 \
114
+}
115
+
116
+HORIZONTAL_FILTER(8, uint8_t)
117
+HORIZONTAL_FILTER(16, uint16_t)
118
+
119
+#define VERTICAL_FILTER(name, type)                                                           \
120
+static int filter_vertically_##name(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)  \
121
+{                                                                                             \
122
+    AverageBlurContext *s = ctx->priv;                                                        \
123
+    ThreadData *td = arg;                                                                     \
124
+    const int height = td->height;                                                            \
125
+    const int width = td->width;                                                              \
126
+    const int slice_start = (width *  jobnr   ) / nb_jobs;                                    \
127
+    const int slice_end   = (width * (jobnr+1)) / nb_jobs;                                    \
128
+    const int radius = FFMIN(s->radiusV, height / 2);                                         \
129
+    const int linesize = td->linesize / sizeof(type);                                         \
130
+    type *buffer = (type *)td->ptr;                                                           \
131
+    const float *src;                                                                         \
132
+    type *ptr;                                                                                \
133
+    int i, x;                                                                                 \
134
+                                                                                              \
135
+    /* Filter vertically along each column */                                                 \
136
+    for (x = slice_start; x < slice_end; x++) {                                               \
137
+        float acc = 0;                                                                        \
138
+        int count = 0;                                                                        \
139
+                                                                                              \
140
+        ptr = buffer + x;                                                                     \
141
+        src = s->buffer + x;                                                                  \
142
+                                                                                              \
143
+        for (i = 0; i < radius; i++) {                                                        \
144
+            acc += src[0];                                                                    \
145
+            src += width;                                                                     \
146
+        }                                                                                     \
147
+        count += radius;                                                                      \
148
+                                                                                              \
149
+        src = s->buffer + x;                                                                  \
150
+        ptr = buffer + x;                                                                     \
151
+        for (i = 0; i <= radius; i++) {                                                       \
152
+            acc += src[(i + radius) * width];                                                 \
153
+            count++;                                                                          \
154
+            ptr[i * linesize] = acc / count;                                                  \
155
+        }                                                                                     \
156
+                                                                                              \
157
+        for (; i < height - radius; i++) {                                                    \
158
+            acc += src[(i + radius) * width] - src[(i - radius - 1) * width];                 \
159
+            ptr[i * linesize] = acc / count;                                                  \
160
+        }                                                                                     \
161
+                                                                                              \
162
+        for (; i < height; i++) {                                                             \
163
+            acc -= src[(i - radius) * width];                                                 \
164
+            count--;                                                                          \
165
+            ptr[i * linesize] = acc / count;                                                  \
166
+        }                                                                                     \
167
+    }                                                                                         \
168
+                                                                                              \
169
+    return 0;                                                                                 \
170
+}
171
+
172
+VERTICAL_FILTER(8, uint8_t)
173
+VERTICAL_FILTER(16, uint16_t)
174
+
175
+static int config_input(AVFilterLink *inlink)
176
+{
177
+    const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
178
+    AverageBlurContext *s = inlink->dst->priv;
179
+
180
+    s->depth = desc->comp[0].depth;
181
+    s->planewidth[1] = s->planewidth[2] = AV_CEIL_RSHIFT(inlink->w, desc->log2_chroma_w);
182
+    s->planewidth[0] = s->planewidth[3] = inlink->w;
183
+    s->planeheight[1] = s->planeheight[2] = AV_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h);
184
+    s->planeheight[0] = s->planeheight[3] = inlink->h;
185
+
186
+    s->nb_planes = av_pix_fmt_count_planes(inlink->format);
187
+
188
+    s->buffer = av_malloc_array(inlink->w, inlink->h * sizeof(*s->buffer));
189
+    if (!s->buffer)
190
+        return AVERROR(ENOMEM);
191
+
192
+    if (s->radiusV <= 0) {
193
+        s->radiusV = s->radius;
194
+    }
195
+
196
+    if (s->depth == 8) {
197
+        s->filter_horizontally = filter_horizontally_8;
198
+        s->filter_vertically = filter_vertically_8;
199
+    } else {
200
+        s->filter_horizontally = filter_horizontally_16;
201
+        s->filter_vertically = filter_vertically_16;
202
+    }
203
+
204
+    return 0;
205
+}
206
+
207
+static void averageiir2d(AVFilterContext *ctx, AVFrame *in, AVFrame *out, int plane)
208
+{
209
+    AverageBlurContext *s = ctx->priv;
210
+    const int width = s->planewidth[plane];
211
+    const int height = s->planeheight[plane];
212
+    const int nb_threads = ff_filter_get_nb_threads(ctx);
213
+    ThreadData td;
214
+
215
+    td.width = width;
216
+    td.height = height;
217
+    td.ptr = in->data[plane];
218
+    td.linesize = in->linesize[plane];
219
+    ctx->internal->execute(ctx, s->filter_horizontally, &td, NULL, FFMIN(height, nb_threads));
220
+    td.ptr = out->data[plane];
221
+    td.linesize = out->linesize[plane];
222
+    ctx->internal->execute(ctx, s->filter_vertically, &td, NULL, FFMIN(width, nb_threads));
223
+}
224
+
225
+static int query_formats(AVFilterContext *ctx)
226
+{
227
+    static const enum AVPixelFormat pix_fmts[] = {
228
+        AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV440P,
229
+        AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P,
230
+        AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUV420P,
231
+        AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ420P,
232
+        AV_PIX_FMT_YUVJ411P, AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P,
233
+        AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV444P9,
234
+        AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10,
235
+        AV_PIX_FMT_YUV420P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV444P12, AV_PIX_FMT_YUV440P12,
236
+        AV_PIX_FMT_YUV420P14, AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV444P14,
237
+        AV_PIX_FMT_YUV420P16, AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV444P16,
238
+        AV_PIX_FMT_YUVA420P9, AV_PIX_FMT_YUVA422P9, AV_PIX_FMT_YUVA444P9,
239
+        AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA444P10,
240
+        AV_PIX_FMT_YUVA420P16, AV_PIX_FMT_YUVA422P16, AV_PIX_FMT_YUVA444P16,
241
+        AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRP9, AV_PIX_FMT_GBRP10,
242
+        AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRP14, AV_PIX_FMT_GBRP16,
243
+        AV_PIX_FMT_GBRAP, AV_PIX_FMT_GBRAP12, AV_PIX_FMT_GBRAP16,
244
+        AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY16,
245
+        AV_PIX_FMT_NONE
246
+    };
247
+
248
+    return ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
249
+}
250
+
251
+static int filter_frame(AVFilterLink *inlink, AVFrame *in)
252
+{
253
+    AVFilterContext *ctx = inlink->dst;
254
+    AverageBlurContext *s = ctx->priv;
255
+    AVFilterLink *outlink = ctx->outputs[0];
256
+    AVFrame *out;
257
+    int plane;
258
+
259
+    if (av_frame_is_writable(in)) {
260
+        out = in;
261
+    } else {
262
+        out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
263
+        if (!out) {
264
+            av_frame_free(&in);
265
+            return AVERROR(ENOMEM);
266
+        }
267
+        av_frame_copy_props(out, in);
268
+    }
269
+
270
+    for (plane = 0; plane < s->nb_planes; plane++) {
271
+        const int height = s->planeheight[plane];
272
+        const int width = s->planewidth[plane];
273
+
274
+        if (!(s->planes & (1 << plane))) {
275
+            if (out != in)
276
+                av_image_copy_plane(out->data[plane], out->linesize[plane],
277
+                                    in->data[plane], in->linesize[plane],
278
+                                    width * ((s->depth + 7) / 8), height);
279
+            continue;
280
+        }
281
+
282
+        averageiir2d(ctx, in, out, plane);
283
+    }
284
+
285
+    if (out != in)
286
+        av_frame_free(&in);
287
+    return ff_filter_frame(outlink, out);
288
+}
289
+
290
+static av_cold void uninit(AVFilterContext *ctx)
291
+{
292
+    AverageBlurContext *s = ctx->priv;
293
+
294
+    av_freep(&s->buffer);
295
+}
296
+
297
+static const AVFilterPad avgblur_inputs[] = {
298
+    {
299
+        .name         = "default",
300
+        .type         = AVMEDIA_TYPE_VIDEO,
301
+        .config_props = config_input,
302
+        .filter_frame = filter_frame,
303
+    },
304
+    { NULL }
305
+};
306
+
307
+static const AVFilterPad avgblur_outputs[] = {
308
+    {
309
+        .name = "default",
310
+        .type = AVMEDIA_TYPE_VIDEO,
311
+    },
312
+    { NULL }
313
+};
314
+
315
+AVFilter ff_vf_avgblur = {
316
+    .name          = "avgblur",
317
+    .description   = NULL_IF_CONFIG_SMALL("Apply Average Blur filter."),
318
+    .priv_size     = sizeof(AverageBlurContext),
319
+    .priv_class    = &avgblur_class,
320
+    .uninit        = uninit,
321
+    .query_formats = query_formats,
322
+    .inputs        = avgblur_inputs,
323
+    .outputs       = avgblur_outputs,
324
+    .flags         = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS,
325
+};