Browse code

avfilter: add convolution filter

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

Paul B Mahol authored on 2016/01/22 19:52:58
Showing 6 changed files
... ...
@@ -57,6 +57,7 @@ version <next>:
57 57
 - only seek with the right mouse button in ffplay
58 58
 - toggle full screen when double-clicking with the left mouse button in ffplay
59 59
 - afftfilt filter
60
+- convolution filter
60 61
 
61 62
 
62 63
 version 2.8:
... ...
@@ -4662,6 +4662,68 @@ For example to convert from BT.601 to SMPTE-240M, use the command:
4662 4662
 colormatrix=bt601:smpte240m
4663 4663
 @end example
4664 4664
 
4665
+@section convolution
4666
+
4667
+Apply convolution 3x3 or 5x5 filter.
4668
+
4669
+The filter accepts the following options:
4670
+
4671
+@table @option
4672
+@item 0m
4673
+@item 1m
4674
+@item 2m
4675
+@item 3m
4676
+Set matrix for each plane.
4677
+Matrix is sequence of 9 or 25 signed integers.
4678
+
4679
+@item 0rdiv
4680
+@item 1rdiv
4681
+@item 2rdiv
4682
+@item 3rdiv
4683
+Set multiplier for calculated value for each plane.
4684
+
4685
+@item 0bias
4686
+@item 1bias
4687
+@item 2bias
4688
+@item 3bias
4689
+Set bias for each plane. This value is added to the result of the multiplication.
4690
+Useful for making the overall image brighter or darker. Default is 0.0.
4691
+@end table
4692
+
4693
+@subsection Examples
4694
+
4695
+@itemize
4696
+@item
4697
+Apply sharpen:
4698
+@example
4699
+convolution="0 -1 0 -1 5 -1 0 -1 0:0 -1 0 -1 5 -1 0 -1 0:0 -1 0 -1 5 -1 0 -1 0:0 -1 0 -1 5 -1 0 -1 0"
4700
+@end example
4701
+
4702
+@item
4703
+Apply blur:
4704
+@example
4705
+convolution="1 1 1 1 1 1 1 1 1:1 1 1 1 1 1 1 1 1:1 1 1 1 1 1 1 1 1:1 1 1 1 1 1 1 1 1:1/9:1/9:1/9:1/9"
4706
+@end example
4707
+
4708
+@item
4709
+Apply edge enhance:
4710
+@example
4711
+convolution="0 0 0 -1 1 0 0 0 0:0 0 0 -1 1 0 0 0 0:0 0 0 -1 1 0 0 0 0:0 0 0 -1 1 0 0 0 0:5:1:1:1:0:128:128:128"
4712
+@end example
4713
+
4714
+@item
4715
+Apply edge detect:
4716
+@example
4717
+convolution="0 1 0 1 -4 1 0 1 0:0 1 0 1 -4 1 0 1 0:0 1 0 1 -4 1 0 1 0:0 1 0 1 -4 1 0 1 0:5:5:5:1:0:128:128:128"
4718
+@end example
4719
+
4720
+@item
4721
+Apply emboss:
4722
+@example
4723
+convolution="-2 -1 0 -1 1 1 0 1 2:-2 -1 0 -1 1 1 0 1 2:-2 -1 0 -1 1 1 0 1 2:-2 -1 0 -1 1 1 0 1 2"
4724
+@end example
4725
+@end itemize
4726
+
4665 4727
 @section copy
4666 4728
 
4667 4729
 Copy the input source unchanged to the output. This is mainly useful for
... ...
@@ -123,6 +123,7 @@ OBJS-$(CONFIG_COLORCHANNELMIXER_FILTER)      += vf_colorchannelmixer.o
123 123
 OBJS-$(CONFIG_COLORKEY_FILTER)               += vf_colorkey.o
124 124
 OBJS-$(CONFIG_COLORLEVELS_FILTER)            += vf_colorlevels.o
125 125
 OBJS-$(CONFIG_COLORMATRIX_FILTER)            += vf_colormatrix.o
126
+OBJS-$(CONFIG_CONVOLUTION_FILTER)            += vf_convolution.o
126 127
 OBJS-$(CONFIG_COPY_FILTER)                   += vf_copy.o
127 128
 OBJS-$(CONFIG_COVER_RECT_FILTER)             += vf_cover_rect.o lavfutils.o
128 129
 OBJS-$(CONFIG_CROP_FILTER)                   += vf_crop.o
... ...
@@ -144,6 +144,7 @@ void avfilter_register_all(void)
144 144
     REGISTER_FILTER(COLORKEY,       colorkey,       vf);
145 145
     REGISTER_FILTER(COLORLEVELS,    colorlevels,    vf);
146 146
     REGISTER_FILTER(COLORMATRIX,    colormatrix,    vf);
147
+    REGISTER_FILTER(CONVOLUTION,    convolution,    vf);
147 148
     REGISTER_FILTER(COPY,           copy,           vf);
148 149
     REGISTER_FILTER(COVER_RECT,     cover_rect,     vf);
149 150
     REGISTER_FILTER(CROP,           crop,           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  26
33
+#define LIBAVFILTER_VERSION_MINOR  27
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,335 @@
0
+/*
1
+ * Copyright (c) 2012-2013 Oka Motofumi (chikuzen.mo at gmail dot com)
2
+ * Copyright (c) 2015 Paul B Mahol
3
+ *
4
+ * This file is part of FFmpeg.
5
+ *
6
+ * FFmpeg is free software; you can redistribute it and/or
7
+ * modify it under the terms of the GNU Lesser General Public
8
+ * License as published by the Free Software Foundation; either
9
+ * version 2.1 of the License, or (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 GNU
14
+ * Lesser General Public License for more details.
15
+ *
16
+ * You should have received a copy of the GNU Lesser General Public
17
+ * License along with FFmpeg; if not, write to the Free Software
18
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
+ */
20
+
21
+#include "libavutil/avstring.h"
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 ConvolutionContext {
31
+    const AVClass *class;
32
+
33
+    char *matrix_str[4];
34
+    float rdiv[4];
35
+    float bias[4];
36
+
37
+    int bstride;
38
+    uint8_t *buffer;
39
+    int nb_planes;
40
+    int planewidth[4];
41
+    int planeheight[4];
42
+    int matrix[4][25];
43
+    int matrix_length[4];
44
+    int copy[4];
45
+
46
+    void (*filter[4])(struct ConvolutionContext *s, AVFrame *in, AVFrame *out, int plane);
47
+} ConvolutionContext;
48
+
49
+#define OFFSET(x) offsetof(ConvolutionContext, x)
50
+#define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
51
+
52
+static const AVOption convolution_options[] = {
53
+    { "0m", "set matrix for 1st plane", OFFSET(matrix_str[0]), AV_OPT_TYPE_STRING, {.str="0 0 0 0 1 0 0 0 0"}, 0, 0, FLAGS },
54
+    { "1m", "set matrix for 2nd plane", OFFSET(matrix_str[1]), AV_OPT_TYPE_STRING, {.str="0 0 0 0 1 0 0 0 0"}, 0, 0, FLAGS },
55
+    { "2m", "set matrix for 3rd plane", OFFSET(matrix_str[2]), AV_OPT_TYPE_STRING, {.str="0 0 0 0 1 0 0 0 0"}, 0, 0, FLAGS },
56
+    { "3m", "set matrix for 4th plane", OFFSET(matrix_str[3]), AV_OPT_TYPE_STRING, {.str="0 0 0 0 1 0 0 0 0"}, 0, 0, FLAGS },
57
+    { "0rdiv", "set rdiv for 1st plane", OFFSET(rdiv[0]), AV_OPT_TYPE_FLOAT, {.dbl=1.0}, 0.0, INT_MAX, FLAGS},
58
+    { "1rdiv", "set rdiv for 2nd plane", OFFSET(rdiv[1]), AV_OPT_TYPE_FLOAT, {.dbl=1.0}, 0.0, INT_MAX, FLAGS},
59
+    { "2rdiv", "set rdiv for 3rd plane", OFFSET(rdiv[2]), AV_OPT_TYPE_FLOAT, {.dbl=1.0}, 0.0, INT_MAX, FLAGS},
60
+    { "3rdiv", "set rdiv for 4th plane", OFFSET(rdiv[3]), AV_OPT_TYPE_FLOAT, {.dbl=1.0}, 0.0, INT_MAX, FLAGS},
61
+    { "0bias", "set bias for 1st plane", OFFSET(bias[0]), AV_OPT_TYPE_FLOAT, {.dbl=0.0}, 0.0, INT_MAX, FLAGS},
62
+    { "1bias", "set bias for 2nd plane", OFFSET(bias[1]), AV_OPT_TYPE_FLOAT, {.dbl=0.0}, 0.0, INT_MAX, FLAGS},
63
+    { "2bias", "set bias for 3rd plane", OFFSET(bias[2]), AV_OPT_TYPE_FLOAT, {.dbl=0.0}, 0.0, INT_MAX, FLAGS},
64
+    { "3bias", "set bias for 4th plane", OFFSET(bias[3]), AV_OPT_TYPE_FLOAT, {.dbl=0.0}, 0.0, INT_MAX, FLAGS},
65
+    { NULL }
66
+};
67
+
68
+AVFILTER_DEFINE_CLASS(convolution);
69
+
70
+static const int same3x3[9] = {0, 0, 0,
71
+                               0, 1, 0,
72
+                               0, 0, 0};
73
+
74
+static const int same5x5[25] = {0, 0, 0, 0, 0,
75
+                                0, 0, 0, 0, 0,
76
+                                0, 0, 1, 0, 0,
77
+                                0, 0, 0, 0, 0,
78
+                                0, 0, 0, 0, 0};
79
+
80
+static int query_formats(AVFilterContext *ctx)
81
+{
82
+    static const enum AVPixelFormat pix_fmts[] = {
83
+        AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV422P,
84
+        AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV411P,
85
+        AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P,
86
+        AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ411P,
87
+        AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV440P,
88
+        AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRAP,
89
+        AV_PIX_FMT_GRAY8,
90
+        AV_PIX_FMT_NONE
91
+    };
92
+
93
+    return ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
94
+}
95
+
96
+static int config_input(AVFilterLink *inlink)
97
+{
98
+    ConvolutionContext *s = inlink->dst->priv;
99
+    const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
100
+    int ret;
101
+
102
+    if ((ret = av_image_fill_linesizes(s->planewidth, inlink->format, inlink->w)) < 0)
103
+        return ret;
104
+
105
+    s->planeheight[1] = s->planeheight[2] = FF_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h);
106
+    s->planeheight[0] = s->planeheight[3] = inlink->h;
107
+
108
+    s->nb_planes = av_pix_fmt_count_planes(inlink->format);
109
+
110
+    s->bstride = s->planewidth[0] + 32;
111
+    s->buffer = av_malloc(5 * s->bstride);
112
+    if (!s->buffer)
113
+        return AVERROR(ENOMEM);
114
+
115
+    return 0;
116
+}
117
+
118
+static inline void line_copy8(uint8_t *line, const uint8_t *srcp, int width, int mergin)
119
+{
120
+    int i;
121
+
122
+    memcpy(line, srcp, width);
123
+
124
+    for (i = mergin; i > 0; i--) {
125
+        line[-i] = line[i];
126
+        line[width - 1 + i] = line[width - 1 - i];
127
+    }
128
+}
129
+
130
+static void filter_3x3(ConvolutionContext *s, AVFrame *in, AVFrame *out, int plane)
131
+{
132
+    const uint8_t *src = in->data[plane];
133
+    uint8_t *dst = out->data[plane];
134
+    const int stride = in->linesize[plane];
135
+    const int bstride = s->bstride;
136
+    const int height = s->planeheight[plane];
137
+    const int width  = s->planewidth[plane];
138
+    uint8_t *p0 = s->buffer + 16;
139
+    uint8_t *p1 = p0 + bstride;
140
+    uint8_t *p2 = p1 + bstride;
141
+    uint8_t *orig = p0, *end = p2;
142
+    const int *matrix = s->matrix[plane];
143
+    const float rdiv = s->rdiv[plane];
144
+    const float bias = s->bias[plane];
145
+    int y, x;
146
+
147
+    line_copy8(p0, src + stride, width, 1);
148
+    line_copy8(p1, src, width, 1);
149
+
150
+    for (y = 0; y < height; y++) {
151
+        src += stride * (y < height - 1 ? 1 : -1);
152
+        line_copy8(p2, src, width, 1);
153
+
154
+        for (x = 0; x < width; x++) {
155
+            int sum = p0[x - 1] * matrix[0] +
156
+                      p0[x] *     matrix[1] +
157
+                      p0[x + 1] * matrix[2] +
158
+                      p1[x - 1] * matrix[3] +
159
+                      p1[x] *     matrix[4] +
160
+                      p1[x + 1] * matrix[5] +
161
+                      p2[x - 1] * matrix[6] +
162
+                      p2[x] *     matrix[7] +
163
+                      p2[x + 1] * matrix[8];
164
+            sum = (int)(sum * rdiv + bias + 0.5f);
165
+            dst[x] = av_clip(sum, 0, 255);
166
+        }
167
+
168
+        p0 = p1;
169
+        p1 = p2;
170
+        p2 = (p2 == end) ? orig: p2 + bstride;
171
+        dst += out->linesize[plane];
172
+    }
173
+}
174
+
175
+static void filter_5x5(ConvolutionContext *s, AVFrame *in, AVFrame *out, int plane)
176
+{
177
+    const uint8_t *src = in->data[plane];
178
+    uint8_t *dst = out->data[plane];
179
+    const int stride = in->linesize[plane];
180
+    const int bstride = s->bstride;
181
+    const int height = s->planeheight[plane];
182
+    const int width  = s->planewidth[plane];
183
+    uint8_t *p0 = s->buffer + 16;
184
+    uint8_t *p1 = p0 + bstride;
185
+    uint8_t *p2 = p1 + bstride;
186
+    uint8_t *p3 = p2 + bstride;
187
+    uint8_t *p4 = p3 + bstride;
188
+    uint8_t *orig = p0, *end = p4;
189
+    const int *matrix = s->matrix[plane];
190
+    float rdiv = s->rdiv[plane];
191
+    float bias = s->bias[plane];
192
+    int y, x, i;
193
+
194
+    line_copy8(p0, src + 2 * stride, width, 2);
195
+    line_copy8(p1, src + stride, width, 2);
196
+    line_copy8(p2, src, width, 2);
197
+    src += stride;
198
+    line_copy8(p3, src, width, 2);
199
+
200
+
201
+    for (y = 0; y < height; y++) {
202
+        uint8_t *array[] = {
203
+            p0 - 2, p0 - 1, p0, p0 + 1, p0 + 2,
204
+            p1 - 2, p1 - 1, p1, p1 + 1, p1 + 2,
205
+            p2 - 2, p2 - 1, p2, p2 + 1, p2 + 2,
206
+            p3 - 2, p3 - 1, p3, p3 + 1, p3 + 2,
207
+            p4 - 2, p4 - 1, p4, p4 + 1, p4 + 2
208
+        };
209
+
210
+        src += stride * (y < height - 2 ? 1 : -1);
211
+        line_copy8(p4, src, width, 2);
212
+
213
+        for (x = 0; x < width; x++) {
214
+            int sum = 0;
215
+
216
+            for (i = 0; i < 25; i++) {
217
+                sum += *(array[i] + x) * matrix[i];
218
+            }
219
+            sum = (int)(sum * rdiv + bias + 0.5f);
220
+            dst[x] = av_clip(sum, 0, 255);
221
+        }
222
+
223
+        p0 = p1;
224
+        p1 = p2;
225
+        p2 = p3;
226
+        p3 = p4;
227
+        p4 = (p4 == end) ? orig: p4 + bstride;
228
+        dst += out->linesize[plane];
229
+    }
230
+}
231
+
232
+static int filter_frame(AVFilterLink *inlink, AVFrame *in)
233
+{
234
+    ConvolutionContext *s = inlink->dst->priv;
235
+    AVFilterLink *outlink = inlink->dst->outputs[0];
236
+    AVFrame *out;
237
+    int plane;
238
+
239
+    out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
240
+    if (!out) {
241
+        av_frame_free(&in);
242
+        return AVERROR(ENOMEM);
243
+    }
244
+    av_frame_copy_props(out, in);
245
+
246
+    for (plane = 0; plane < s->nb_planes; plane++) {
247
+        if (s->copy[plane]) {
248
+            av_image_copy_plane(out->data[plane], out->linesize[plane],
249
+                                in->data[plane], in->linesize[plane],
250
+                                s->planewidth[plane],
251
+                                s->planeheight[plane]);
252
+            continue;
253
+        }
254
+
255
+        s->filter[plane](s, in, out, plane);
256
+    }
257
+
258
+    av_frame_free(&in);
259
+    return ff_filter_frame(outlink, out);
260
+}
261
+
262
+static av_cold int init(AVFilterContext *ctx)
263
+{
264
+    ConvolutionContext *s = ctx->priv;
265
+    int i;
266
+
267
+    for (i = 0; i < 4; i++) {
268
+        int *matrix = (int *)s->matrix[i];
269
+        char *p, *arg, *saveptr = NULL;
270
+
271
+        p = s->matrix_str[i];
272
+        while (s->matrix_length[i] < 25) {
273
+            if (!(arg = av_strtok(p, " ", &saveptr)))
274
+                break;
275
+
276
+            p = NULL;
277
+            sscanf(arg, "%d", &matrix[s->matrix_length[i]]);
278
+            s->matrix_length[i]++;
279
+        }
280
+
281
+        if (s->matrix_length[i] == 9) {
282
+            if (!memcmp(matrix, same3x3, sizeof(same3x3)))
283
+                s->copy[i] = 1;
284
+            else
285
+                s->filter[i] = filter_3x3;
286
+        } else if (s->matrix_length[i] == 25) {
287
+            if (!memcmp(matrix, same5x5, sizeof(same5x5)))
288
+                s->copy[i] = 1;
289
+            else
290
+                s->filter[i] = filter_5x5;
291
+        } else {
292
+            return AVERROR(EINVAL);
293
+        }
294
+    }
295
+
296
+    return 0;
297
+}
298
+
299
+static av_cold void uninit(AVFilterContext *ctx)
300
+{
301
+    ConvolutionContext *s = ctx->priv;
302
+
303
+    av_freep(&s->buffer);
304
+}
305
+
306
+static const AVFilterPad convolution_inputs[] = {
307
+    {
308
+        .name         = "default",
309
+        .type         = AVMEDIA_TYPE_VIDEO,
310
+        .config_props = config_input,
311
+        .filter_frame = filter_frame,
312
+    },
313
+    { NULL }
314
+};
315
+
316
+static const AVFilterPad convolution_outputs[] = {
317
+    {
318
+        .name = "default",
319
+        .type = AVMEDIA_TYPE_VIDEO,
320
+    },
321
+    { NULL }
322
+};
323
+
324
+AVFilter ff_vf_convolution = {
325
+    .name          = "convolution",
326
+    .description   = NULL_IF_CONFIG_SMALL("Apply convolution filter."),
327
+    .priv_size     = sizeof(ConvolutionContext),
328
+    .priv_class    = &convolution_class,
329
+    .init          = init,
330
+    .uninit        = uninit,
331
+    .query_formats = query_formats,
332
+    .inputs        = convolution_inputs,
333
+    .outputs       = convolution_outputs,
334
+};