Browse code

avfilter: add generic FFT video convolve filter

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

Paul B Mahol authored on 2017/08/31 19:07:58
Showing 6 changed files
... ...
@@ -45,6 +45,7 @@ version <next>:
45 45
 - despill video filter
46 46
 - haas audio filter
47 47
 - SUP/PGS subtitle muxer
48
+- convolve video filter
48 49
 
49 50
 version 3.3:
50 51
 - CrystalHD decoder moved to new decode API
... ...
@@ -6005,6 +6005,24 @@ 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 -
6005 6005
 @end example
6006 6006
 @end itemize
6007 6007
 
6008
+@section convolve
6009
+
6010
+Apply 2D convolution of video stream in frequency domain using second stream
6011
+as impulse.
6012
+
6013
+The filter accepts the following options:
6014
+
6015
+@table @option
6016
+@item planes
6017
+Set which planes to process.
6018
+
6019
+@item impulse
6020
+Set which impulse video frames will be processed, can be @var{first}
6021
+or @var{all}. Default is @var{all}.
6022
+@end table
6023
+
6024
+The @code{convolve} filter also supports the @ref{framesync} options.
6025
+
6008 6026
 @section copy
6009 6027
 
6010 6028
 Copy the input video source unchanged to the output. This is mainly useful for
... ...
@@ -150,6 +150,7 @@ OBJS-$(CONFIG_COLORLEVELS_FILTER)            += vf_colorlevels.o
150 150
 OBJS-$(CONFIG_COLORMATRIX_FILTER)            += vf_colormatrix.o
151 151
 OBJS-$(CONFIG_COLORSPACE_FILTER)             += vf_colorspace.o colorspacedsp.o
152 152
 OBJS-$(CONFIG_CONVOLUTION_FILTER)            += vf_convolution.o
153
+OBJS-$(CONFIG_CONVOLVE_FILTER)               += vf_convolve.o framesync2.o
153 154
 OBJS-$(CONFIG_COPY_FILTER)                   += vf_copy.o
154 155
 OBJS-$(CONFIG_COREIMAGE_FILTER)              += vf_coreimage.o
155 156
 OBJS-$(CONFIG_COVER_RECT_FILTER)             += vf_cover_rect.o lavfutils.o
... ...
@@ -162,6 +162,7 @@ static void register_all(void)
162 162
     REGISTER_FILTER(COLORMATRIX,    colormatrix,    vf);
163 163
     REGISTER_FILTER(COLORSPACE,     colorspace,     vf);
164 164
     REGISTER_FILTER(CONVOLUTION,    convolution,    vf);
165
+    REGISTER_FILTER(CONVOLVE,       convolve,       vf);
165 166
     REGISTER_FILTER(COPY,           copy,           vf);
166 167
     REGISTER_FILTER(COREIMAGE,      coreimage,      vf);
167 168
     REGISTER_FILTER(COVER_RECT,     cover_rect,     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 104
33
+#define LIBAVFILTER_VERSION_MINOR 105
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,418 @@
0
+/*
1
+ * Copyright (c) 2017 Paul B Mahol
2
+ *
3
+ * This file is part of FFmpeg.
4
+ *
5
+ * FFmpeg is free software; you can redistribute it and/or
6
+ * modify it under the terms of the GNU Lesser General Public
7
+ * License as published by the Free Software Foundation; either
8
+ * version 2.1 of the License, or (at your option) any later version.
9
+ *
10
+ * FFmpeg is distributed in the hope that it will be useful,
11
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
+ * Lesser General Public License for more details.
14
+ *
15
+ * You should have received a copy of the GNU Lesser General Public
16
+ * License along with FFmpeg; if not, write to the Free Software
17
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
+ */
19
+
20
+#include "libavutil/imgutils.h"
21
+#include "libavutil/opt.h"
22
+#include "libavutil/pixdesc.h"
23
+#include "libavcodec/avfft.h"
24
+
25
+#include "avfilter.h"
26
+#include "formats.h"
27
+#include "framesync2.h"
28
+#include "internal.h"
29
+#include "video.h"
30
+
31
+typedef struct ConvolveContext {
32
+    const AVClass *class;
33
+    FFFrameSync fs;
34
+
35
+    FFTContext *fft[4];
36
+    FFTContext *ifft[4];
37
+
38
+    int fft_bits[4];
39
+    int fft_len[4];
40
+    int planewidth[4];
41
+    int planeheight[4];
42
+
43
+    FFTComplex *fft_hdata[4];
44
+    FFTComplex *fft_vdata[4];
45
+    FFTComplex *fft_hdata_impulse[4];
46
+    FFTComplex *fft_vdata_impulse[4];
47
+
48
+    int depth;
49
+    int planes;
50
+    int impulse;
51
+    int nb_planes;
52
+    int got_impulse[4];
53
+} ConvolveContext;
54
+
55
+#define OFFSET(x) offsetof(ConvolveContext, x)
56
+#define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
57
+
58
+static const AVOption convolve_options[] = {
59
+    { "planes",  "set planes to convolve",                  OFFSET(planes),   AV_OPT_TYPE_INT,   {.i64=7}, 0, 15, FLAGS },
60
+    { "impulse", "when to process impulses",                OFFSET(impulse),  AV_OPT_TYPE_INT,   {.i64=1}, 0,  1, FLAGS, "impulse" },
61
+    {   "first", "process only first impulse, ignore rest", 0,                AV_OPT_TYPE_CONST, {.i64=0}, 0,  0, FLAGS, "impulse" },
62
+    {   "all",   "process all impulses",                    0,                AV_OPT_TYPE_CONST, {.i64=1}, 0,  0, FLAGS, "impulse" },
63
+    { NULL },
64
+};
65
+
66
+FRAMESYNC_DEFINE_CLASS(convolve, ConvolveContext, fs);
67
+
68
+static int query_formats(AVFilterContext *ctx)
69
+{
70
+    static const enum AVPixelFormat pixel_fmts_fftfilt[] = {
71
+        AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV440P,
72
+        AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P,
73
+        AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUV420P,
74
+        AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ420P,
75
+        AV_PIX_FMT_YUVJ411P, AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P,
76
+        AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV444P9,
77
+        AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10,
78
+        AV_PIX_FMT_YUV420P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV444P12, AV_PIX_FMT_YUV440P12,
79
+        AV_PIX_FMT_YUV420P14, AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV444P14,
80
+        AV_PIX_FMT_YUV420P16, AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV444P16,
81
+        AV_PIX_FMT_YUVA420P9, AV_PIX_FMT_YUVA422P9, AV_PIX_FMT_YUVA444P9,
82
+        AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA444P10,
83
+        AV_PIX_FMT_YUVA420P16, AV_PIX_FMT_YUVA422P16, AV_PIX_FMT_YUVA444P16,
84
+        AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRP9, AV_PIX_FMT_GBRP10,
85
+        AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRP14, AV_PIX_FMT_GBRP16,
86
+        AV_PIX_FMT_GBRAP, AV_PIX_FMT_GBRAP10, AV_PIX_FMT_GBRAP12, AV_PIX_FMT_GBRAP16,
87
+        AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY9, AV_PIX_FMT_GRAY10, AV_PIX_FMT_GRAY12, AV_PIX_FMT_GRAY16,
88
+        AV_PIX_FMT_NONE
89
+    };
90
+
91
+    AVFilterFormats *fmts_list = ff_make_format_list(pixel_fmts_fftfilt);
92
+    if (!fmts_list)
93
+        return AVERROR(ENOMEM);
94
+    return ff_set_common_formats(ctx, fmts_list);
95
+}
96
+
97
+static int config_input_main(AVFilterLink *inlink)
98
+{
99
+    ConvolveContext *s = inlink->dst->priv;
100
+    const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
101
+    int fft_bits, i;
102
+
103
+    s->planewidth[1] = s->planewidth[2] = AV_CEIL_RSHIFT(inlink->w, desc->log2_chroma_w);
104
+    s->planewidth[0] = s->planewidth[3] = inlink->w;
105
+    s->planeheight[1] = s->planeheight[2] = AV_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h);
106
+    s->planeheight[0] = s->planeheight[3] = inlink->h;
107
+
108
+    s->nb_planes = desc->nb_components;
109
+    s->depth = desc->comp[0].depth;
110
+
111
+    for (i = 0; i < s->nb_planes; i++) {
112
+        int w = s->planewidth[i];
113
+        int h = s->planeheight[i];
114
+        int n = FFMAX(w, h) * 10/9;
115
+
116
+        for (fft_bits = 1; 1 << fft_bits < n; fft_bits++);
117
+
118
+        s->fft_bits[i] = fft_bits;
119
+        s->fft_len[i] = 1 << s->fft_bits[i];
120
+
121
+        if (!(s->fft_hdata[i] = av_calloc(s->fft_len[i], s->fft_len[i] * sizeof(FFTComplex))))
122
+            return AVERROR(ENOMEM);
123
+
124
+        if (!(s->fft_vdata[i] = av_calloc(s->fft_len[i], s->fft_len[i] * sizeof(FFTComplex))))
125
+            return AVERROR(ENOMEM);
126
+
127
+        if (!(s->fft_hdata_impulse[i] = av_calloc(s->fft_len[i], s->fft_len[i] * sizeof(FFTComplex))))
128
+            return AVERROR(ENOMEM);
129
+
130
+        if (!(s->fft_vdata_impulse[i] = av_calloc(s->fft_len[i], s->fft_len[i] * sizeof(FFTComplex))))
131
+            return AVERROR(ENOMEM);
132
+    }
133
+
134
+    return 0;
135
+}
136
+
137
+static int config_input_impulse(AVFilterLink *inlink)
138
+{
139
+    AVFilterContext *ctx  = inlink->dst;
140
+
141
+    if (ctx->inputs[0]->w != ctx->inputs[1]->w ||
142
+        ctx->inputs[0]->h != ctx->inputs[1]->h) {
143
+        av_log(ctx, AV_LOG_ERROR, "Width and height of input videos must be same.\n");
144
+        return AVERROR(EINVAL);
145
+    }
146
+    if (ctx->inputs[0]->format != ctx->inputs[1]->format) {
147
+        av_log(ctx, AV_LOG_ERROR, "Inputs must be of same pixel format.\n");
148
+        return AVERROR(EINVAL);
149
+    }
150
+
151
+    return 0;
152
+}
153
+
154
+static void fft_horizontal(ConvolveContext *s, FFTComplex *fft_hdata,
155
+                           AVFrame *in, int w, int h, int n, int plane, float scale)
156
+{
157
+    int y, x;
158
+
159
+    for (y = 0; y < h; y++) {
160
+        if (s->depth == 8) {
161
+            const uint8_t *src = in->data[plane] + in->linesize[plane] * y;
162
+
163
+            for (x = 0; x < w; x++) {
164
+                fft_hdata[y * n + x].re = src[x] * scale;
165
+                fft_hdata[y * n + x].im = 0;
166
+            }
167
+        } else {
168
+            const uint16_t *src = (const uint16_t *)(in->data[plane] + in->linesize[plane] * y);
169
+
170
+            for (x = 0; x < w; x++) {
171
+                fft_hdata[y * n + x].re = src[x] * scale;
172
+                fft_hdata[y * n + x].im = 0;
173
+            }
174
+        }
175
+        for (; x < n; x++) {
176
+            fft_hdata[y * n + x].re = 0;
177
+            fft_hdata[y * n + x].im = 0;
178
+        }
179
+    }
180
+
181
+    for (; y < n; y++) {
182
+        for (x = 0; x < n; x++) {
183
+            fft_hdata[y * n + x].re = 0;
184
+            fft_hdata[y * n + x].im = 0;
185
+        }
186
+    }
187
+
188
+    for (y = 0; y < n; y++) {
189
+        av_fft_permute(s->fft[plane], fft_hdata + y * n);
190
+        av_fft_calc(s->fft[plane], fft_hdata + y * n);
191
+    }
192
+}
193
+
194
+static void fft_vertical(ConvolveContext *s, FFTComplex *fft_hdata, FFTComplex *fft_vdata,
195
+                         int n, int plane)
196
+{
197
+    int y, x;
198
+
199
+    for (y = 0; y < n; y++) {
200
+        for (x = 0; x < n; x++) {
201
+            fft_vdata[y * n + x].re = fft_hdata[x * n + y].re;
202
+            fft_vdata[y * n + x].im = fft_hdata[x * n + y].im;
203
+        }
204
+        for (; x < n; x++) {
205
+            fft_vdata[y * n + x].re = 0;
206
+            fft_vdata[y * n + x].im = 0;
207
+        }
208
+        av_fft_permute(s->fft[plane], fft_vdata + y * n);
209
+        av_fft_calc(s->fft[plane], fft_vdata + y * n);
210
+    }
211
+}
212
+
213
+static void ifft_vertical(ConvolveContext *s, int n, int plane)
214
+{
215
+    int y, x;
216
+
217
+    for (y = 0; y < n; y++) {
218
+        av_fft_permute(s->ifft[plane], s->fft_vdata[plane] + y * n);
219
+        av_fft_calc(s->ifft[plane], s->fft_vdata[plane] + y * n);
220
+        for (x = 0; x < n; x++) {
221
+            s->fft_hdata[plane][x * n + y].re = s->fft_vdata[plane][y * n + x].re;
222
+            s->fft_hdata[plane][x * n + y].im = s->fft_vdata[plane][y * n + x].im;
223
+        }
224
+    }
225
+}
226
+
227
+static void ifft_horizontal(ConvolveContext *s, AVFrame *out,
228
+                            int w, int h, int n, int plane)
229
+{
230
+    const float scale = 1.f / (n * n);
231
+    const int max = (1 << s->depth) - 1;
232
+    const int oh = h / 2;
233
+    const int ow = w / 2;
234
+    int y, x;
235
+
236
+    for (y = 0; y < n; y++) {
237
+        av_fft_permute(s->ifft[plane], s->fft_hdata[plane] + y * n);
238
+        av_fft_calc(s->ifft[plane], s->fft_hdata[plane] + y * n);
239
+    }
240
+
241
+    if (s->depth == 8) {
242
+        for (y = 0; y < h; y++) {
243
+            uint8_t *dst = out->data[plane] + y * out->linesize[plane];
244
+            for (x = 0; x < w; x++)
245
+                dst[x] = av_clip(s->fft_hdata[plane][(y+oh) * n + x+ow].re * scale, 0, 255);
246
+        }
247
+    } else {
248
+        for (y = 0; y < h; y++) {
249
+            uint16_t *dst = (uint16_t *)(out->data[plane] + y * out->linesize[plane]);
250
+            for (x = 0; x < w; x++)
251
+                dst[x] = av_clip(s->fft_hdata[plane][(y+oh) * n + x+ow].re * scale, 0, max);
252
+        }
253
+    }
254
+}
255
+
256
+static int do_convolve(FFFrameSync *fs)
257
+{
258
+    AVFilterContext *ctx = fs->parent;
259
+    AVFilterLink *outlink = ctx->outputs[0];
260
+    ConvolveContext *s = ctx->priv;
261
+    AVFrame *mainpic = NULL, *impulsepic = NULL;
262
+    int ret, y, x, plane;
263
+
264
+    ret = ff_framesync2_dualinput_get(fs, &mainpic, &impulsepic);
265
+    if (ret < 0)
266
+        return ret;
267
+    if (!impulsepic)
268
+        return ff_filter_frame(outlink, mainpic);
269
+
270
+    for (plane = 0; plane < s->nb_planes; plane++) {
271
+        const int n = s->fft_len[plane];
272
+        const int w = s->planewidth[plane];
273
+        const int h = s->planeheight[plane];
274
+        float total = 0;
275
+
276
+        if (!(s->planes & (1 << plane))) {
277
+            continue;
278
+        }
279
+
280
+        fft_horizontal(s, s->fft_hdata[plane], mainpic, w, h, n, plane, 1.f);
281
+        fft_vertical(s, s->fft_hdata[plane], s->fft_vdata[plane],
282
+                     n, plane);
283
+
284
+        if ((!s->impulse && !s->got_impulse[plane]) || s->impulse) {
285
+            if (s->depth == 8) {
286
+                for (y = 0; y < h; y++) {
287
+                    const uint8_t *src = (const uint8_t *)(impulsepic->data[plane] + y * impulsepic->linesize[plane]) ;
288
+                    for (x = 0; x < w; x++) {
289
+                        total += src[x];
290
+                    }
291
+                }
292
+            } else {
293
+                for (y = 0; y < h; y++) {
294
+                    const uint16_t *src = (const uint16_t *)(impulsepic->data[plane] + y * impulsepic->linesize[plane]) ;
295
+                    for (x = 0; x < w; x++) {
296
+                        total += src[x];
297
+                    }
298
+                }
299
+            }
300
+            total = FFMAX(1, total);
301
+
302
+            fft_horizontal(s, s->fft_hdata_impulse[plane], impulsepic, w, h, n, plane, 1 / total);
303
+            fft_vertical(s, s->fft_hdata_impulse[plane], s->fft_vdata_impulse[plane],
304
+                         n, plane);
305
+
306
+            s->got_impulse[plane] = 1;
307
+        }
308
+
309
+        for (y = 0; y < n; y++) {
310
+            for (x = 0; x < n; x++) {
311
+                FFTSample re, im, ire, iim;
312
+
313
+                re = s->fft_vdata[plane][y*n + x].re;
314
+                im = s->fft_vdata[plane][y*n + x].im;
315
+                ire = s->fft_vdata_impulse[plane][y*n + x].re;
316
+                iim = s->fft_vdata_impulse[plane][y*n + x].im;
317
+
318
+                s->fft_vdata[plane][y*n + x].re = ire * re - iim * im;
319
+                s->fft_vdata[plane][y*n + x].im = iim * re + ire * im;
320
+            }
321
+        }
322
+
323
+        ifft_vertical(s, n, plane);
324
+        ifft_horizontal(s, mainpic, w, h, n, plane);
325
+    }
326
+
327
+    return ff_filter_frame(outlink, mainpic);
328
+}
329
+
330
+static int config_output(AVFilterLink *outlink)
331
+{
332
+    AVFilterContext *ctx = outlink->src;
333
+    ConvolveContext *s = ctx->priv;
334
+    AVFilterLink *mainlink = ctx->inputs[0];
335
+    int ret, i;
336
+
337
+    s->fs.on_event = do_convolve;
338
+    ret = ff_framesync2_init_dualinput(&s->fs, ctx);
339
+    if (ret < 0)
340
+        return ret;
341
+    outlink->w = mainlink->w;
342
+    outlink->h = mainlink->h;
343
+    outlink->time_base = mainlink->time_base;
344
+    outlink->sample_aspect_ratio = mainlink->sample_aspect_ratio;
345
+    outlink->frame_rate = mainlink->frame_rate;
346
+
347
+    if ((ret = ff_framesync2_configure(&s->fs)) < 0)
348
+        return ret;
349
+
350
+    for (i = 0; i < s->nb_planes; i++) {
351
+        s->fft[i]  = av_fft_init(s->fft_bits[i], 0);
352
+        s->ifft[i] = av_fft_init(s->fft_bits[i], 1);
353
+        if (!s->fft[i] || !s->ifft[i])
354
+            return AVERROR(ENOMEM);
355
+    }
356
+
357
+    return 0;
358
+}
359
+
360
+static int activate(AVFilterContext *ctx)
361
+{
362
+    ConvolveContext *s = ctx->priv;
363
+    return ff_framesync2_activate(&s->fs);
364
+}
365
+
366
+static av_cold void uninit(AVFilterContext *ctx)
367
+{
368
+    ConvolveContext *s = ctx->priv;
369
+    int i;
370
+
371
+    for (i = 0; i < 4; i++) {
372
+        av_freep(&s->fft_hdata[i]);
373
+        av_freep(&s->fft_vdata[i]);
374
+        av_freep(&s->fft_hdata_impulse[i]);
375
+        av_freep(&s->fft_vdata_impulse[i]);
376
+        av_fft_end(s->fft[i]);
377
+        av_fft_end(s->ifft[i]);
378
+    }
379
+
380
+    ff_framesync2_uninit(&s->fs);
381
+}
382
+
383
+static const AVFilterPad convolve_inputs[] = {
384
+    {
385
+        .name          = "main",
386
+        .type          = AVMEDIA_TYPE_VIDEO,
387
+        .config_props  = config_input_main,
388
+    },{
389
+        .name          = "impulse",
390
+        .type          = AVMEDIA_TYPE_VIDEO,
391
+        .config_props  = config_input_impulse,
392
+    },
393
+    { NULL }
394
+};
395
+
396
+static const AVFilterPad convolve_outputs[] = {
397
+    {
398
+        .name          = "default",
399
+        .type          = AVMEDIA_TYPE_VIDEO,
400
+        .config_props  = config_output,
401
+    },
402
+    { NULL }
403
+};
404
+
405
+AVFilter ff_vf_convolve = {
406
+    .name          = "convolve",
407
+    .description   = NULL_IF_CONFIG_SMALL("Convolve first video stream with second video stream."),
408
+    .preinit       = convolve_framesync_preinit,
409
+    .uninit        = uninit,
410
+    .query_formats = query_formats,
411
+    .activate      = activate,
412
+    .priv_size     = sizeof(ConvolveContext),
413
+    .priv_class    = &convolve_class,
414
+    .inputs        = convolve_inputs,
415
+    .outputs       = convolve_outputs,
416
+    .flags         = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL,
417
+};