Browse code

avfilter: add hysteresis filter

Paul B Mahol authored on 2016/08/22 22:45:44
Showing 6 changed files
... ...
@@ -19,6 +19,7 @@ version <next>:
19 19
 - floating point support in als decoder
20 20
 - fifo muxer
21 21
 - maskedclamp filter
22
+- hysteresis filter
22 23
 
23 24
 
24 25
 version 3.1:
... ...
@@ -8766,6 +8766,25 @@ If the specified expression is not valid, it is kept at its current
8766 8766
 value.
8767 8767
 @end table
8768 8768
 
8769
+@section hysteresis
8770
+
8771
+Grow first stream into second stream by connecting components.
8772
+This allows to build more robust edge masks.
8773
+
8774
+This filter accepts the following options:
8775
+
8776
+@table @option
8777
+@item planes
8778
+Set which planes will be processed as bitmap, unprocessed planes will be
8779
+copied from first stream.
8780
+By default value 0xf, all planes will be processed.
8781
+
8782
+@item threshold
8783
+Set threshold which is used in filtering. If pixel component value is higher than
8784
+this value filter algorithm for connecting components is activated.
8785
+By default value is 0.
8786
+@end table
8787
+
8769 8788
 @section idet
8770 8789
 
8771 8790
 Detect video interlacing type.
... ...
@@ -195,6 +195,7 @@ OBJS-$(CONFIG_HUE_FILTER)                    += vf_hue.o
195 195
 OBJS-$(CONFIG_HWDOWNLOAD_FILTER)             += vf_hwdownload.o
196 196
 OBJS-$(CONFIG_HWUPLOAD_CUDA_FILTER)          += vf_hwupload_cuda.o
197 197
 OBJS-$(CONFIG_HWUPLOAD_FILTER)               += vf_hwupload.o
198
+OBJS-$(CONFIG_HYSTERESIS_FILTER)             += vf_hysteresis.o framesync.o
198 199
 OBJS-$(CONFIG_IDET_FILTER)                   += vf_idet.o
199 200
 OBJS-$(CONFIG_IL_FILTER)                     += vf_il.o
200 201
 OBJS-$(CONFIG_INFLATE_FILTER)                += vf_neighbor.o
... ...
@@ -212,6 +212,7 @@ void avfilter_register_all(void)
212 212
     REGISTER_FILTER(HWDOWNLOAD,     hwdownload,     vf);
213 213
     REGISTER_FILTER(HWUPLOAD,       hwupload,       vf);
214 214
     REGISTER_FILTER(HWUPLOAD_CUDA,  hwupload_cuda,  vf);
215
+    REGISTER_FILTER(HYSTERESIS,     hysteresis,     vf);
215 216
     REGISTER_FILTER(IDET,           idet,           vf);
216 217
     REGISTER_FILTER(IL,             il,             vf);
217 218
     REGISTER_FILTER(INFLATE,        inflate,        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  53
33
+#define LIBAVFILTER_VERSION_MINOR  54
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,401 @@
0
+/*
1
+ * Copyright (c) 2013 Oka Motofumi (chikuzen.mo at gmail dot com)
2
+ * Copyright (c) 2016 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/imgutils.h"
22
+#include "libavutil/pixdesc.h"
23
+#include "libavutil/opt.h"
24
+#include "avfilter.h"
25
+#include "formats.h"
26
+#include "internal.h"
27
+#include "video.h"
28
+#include "framesync.h"
29
+
30
+#define OFFSET(x) offsetof(HysteresisContext, x)
31
+#define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
32
+
33
+typedef struct HysteresisContext {
34
+    const AVClass *class;
35
+
36
+    int planes;
37
+    int threshold;
38
+
39
+    int width[4], height[4];
40
+    int nb_planes;
41
+    int depth;
42
+    FFFrameSync fs;
43
+
44
+    uint8_t *map;
45
+    uint32_t *xy;
46
+    int index;
47
+
48
+    void (*hysteresis)(struct HysteresisContext *s, const uint8_t *bsrc, const uint8_t *osrc, uint8_t *dst,
49
+                       ptrdiff_t blinesize, ptrdiff_t olinesize,
50
+                       ptrdiff_t destlinesize,
51
+                       int w, int h);
52
+} HysteresisContext;
53
+
54
+static const AVOption hysteresis_options[] = {
55
+    { "planes",    "set planes",    OFFSET(planes),    AV_OPT_TYPE_INT, {.i64=0xF}, 0, 0xF, FLAGS },
56
+    { "threshold", "set threshold", OFFSET(threshold), AV_OPT_TYPE_INT, {.i64=0},   0, UINT16_MAX, FLAGS },
57
+    { NULL }
58
+};
59
+
60
+AVFILTER_DEFINE_CLASS(hysteresis);
61
+
62
+static int query_formats(AVFilterContext *ctx)
63
+{
64
+    static const enum AVPixelFormat pix_fmts[] = {
65
+        AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV440P,
66
+        AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P,
67
+        AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUV420P,
68
+        AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ420P,
69
+        AV_PIX_FMT_YUVJ411P, AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P,
70
+        AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV444P9,
71
+        AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10,
72
+        AV_PIX_FMT_YUV420P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV444P12, AV_PIX_FMT_YUV440P12,
73
+        AV_PIX_FMT_YUV420P14, AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV444P14,
74
+        AV_PIX_FMT_YUV420P16, AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV444P16,
75
+        AV_PIX_FMT_YUVA420P9, AV_PIX_FMT_YUVA422P9, AV_PIX_FMT_YUVA444P9,
76
+        AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA444P10,
77
+        AV_PIX_FMT_YUVA420P16, AV_PIX_FMT_YUVA422P16, AV_PIX_FMT_YUVA444P16,
78
+        AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRP9, AV_PIX_FMT_GBRP10,
79
+        AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRP14, AV_PIX_FMT_GBRP16,
80
+        AV_PIX_FMT_GBRAP, AV_PIX_FMT_GBRAP12, AV_PIX_FMT_GBRAP16,
81
+        AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY16,
82
+        AV_PIX_FMT_NONE
83
+    };
84
+
85
+    return ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
86
+}
87
+
88
+static int process_frame(FFFrameSync *fs)
89
+{
90
+    AVFilterContext *ctx = fs->parent;
91
+    HysteresisContext *s = fs->opaque;
92
+    AVFilterLink *outlink = ctx->outputs[0];
93
+    AVFrame *out, *base, *alt;
94
+    int ret;
95
+
96
+    if ((ret = ff_framesync_get_frame(&s->fs, 0, &base, 0)) < 0 ||
97
+        (ret = ff_framesync_get_frame(&s->fs, 1, &alt,  0)) < 0)
98
+        return ret;
99
+
100
+    if (ctx->is_disabled) {
101
+        out = av_frame_clone(base);
102
+        if (!out)
103
+            return AVERROR(ENOMEM);
104
+    } else {
105
+        int p;
106
+
107
+        out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
108
+        if (!out)
109
+            return AVERROR(ENOMEM);
110
+        av_frame_copy_props(out, base);
111
+
112
+        for (p = 0; p < s->nb_planes; p++) {
113
+            if (!((1 << p) & s->planes)) {
114
+                av_image_copy_plane(out->data[p], out->linesize[p], base->data[p], base->linesize[p],
115
+                                    s->width[p], s->height[p]);
116
+                continue;
117
+            } else {
118
+                int y;
119
+
120
+                for (y = 0; y < s->height[p]; y++) {
121
+                    memset(out->data[p] + y * out->linesize[p], 0, s->width[p]);
122
+                }
123
+            }
124
+
125
+            s->index = -1;
126
+            memset(s->map, 0, s->width[0] * s->height[0]);
127
+            memset(s->xy, 0, s->width[0] * s->height[0] * 4);
128
+
129
+            s->hysteresis(s, base->data[p], alt->data[p],
130
+                          out->data[p],
131
+                          base->linesize[p], alt->linesize[p],
132
+                          out->linesize[p],
133
+                          s->width[p], s->height[p]);
134
+        }
135
+    }
136
+    out->pts = av_rescale_q(s->fs.pts, s->fs.time_base, outlink->time_base);
137
+
138
+    return ff_filter_frame(outlink, out);
139
+}
140
+
141
+static int passed(HysteresisContext *s, int x, int y, int w)
142
+{
143
+    return s->map[x + y * w];
144
+}
145
+
146
+static void push(HysteresisContext *s, int x, int y, int w)
147
+{
148
+    s->map[x + y * w] = 0xff;
149
+    s->xy[++s->index] = (uint16_t)(x) << 16 | (uint16_t)y;
150
+}
151
+
152
+static void pop(HysteresisContext *s, int *x, int *y)
153
+{
154
+    uint32_t val = s->xy[s->index--];
155
+
156
+    *x = val >> 16;
157
+    *y = val & 0x0000FFFF;
158
+}
159
+
160
+static int is_empty(HysteresisContext *s)
161
+{
162
+    return s->index < 0;
163
+}
164
+
165
+static void hysteresis8(HysteresisContext *s, const uint8_t *bsrc, const uint8_t *asrc,
166
+                        uint8_t *dst,
167
+                        ptrdiff_t blinesize, ptrdiff_t alinesize,
168
+                        ptrdiff_t dlinesize,
169
+                        int w, int h)
170
+{
171
+    const int t = s->threshold;
172
+    int x, y;
173
+
174
+    for (y = 0; y < h; y++) {
175
+        for (x = 0; x < w; x++) {
176
+            if ((bsrc[x + y * blinesize] > t) && (asrc[x + y * alinesize] > t) && !passed(s, x, y, w)) {
177
+                int posx, posy;
178
+
179
+                dst[x + y * dlinesize] = asrc[x + y * alinesize];
180
+
181
+                push(s, x, y, w);
182
+
183
+                while (!is_empty(s)) {
184
+                    int x_min, x_max, y_min, y_max, yy, xx;
185
+
186
+                    pop(s, &posx, &posy);
187
+
188
+                    x_min = posx > 0 ? posx - 1 : 0;
189
+                    x_max = posx < w - 1 ? posx + 1 : posx;
190
+                    y_min = posy > 0 ? posy - 1 : 0;
191
+                    y_max = posy < h - 1 ? posy + 1 : posy;
192
+
193
+                    for (yy = y_min; yy <= y_max; yy++) {
194
+                        for (xx = x_min; xx <= x_max; xx++) {
195
+                            if ((asrc[xx + yy * alinesize] > t) && !passed(s, xx, yy, w)) {
196
+                                dst[xx + yy * dlinesize] = asrc[xx + yy * alinesize];
197
+                                push(s, xx, yy, w);
198
+                            }
199
+                        }
200
+                    }
201
+                }
202
+            }
203
+        }
204
+    }
205
+}
206
+
207
+static void hysteresis16(HysteresisContext *s, const uint8_t *bbsrc, const uint8_t *aasrc,
208
+                        uint8_t *ddst,
209
+                        ptrdiff_t blinesize, ptrdiff_t alinesize,
210
+                        ptrdiff_t dlinesize,
211
+                        int w, int h)
212
+{
213
+    const uint16_t *bsrc = (const uint16_t *)bbsrc;
214
+    const uint16_t *asrc = (const uint16_t *)aasrc;
215
+    uint16_t *dst = (uint16_t *)ddst;
216
+    const int t = s->threshold;
217
+    int x, y;
218
+
219
+    blinesize /= 2;
220
+    alinesize /= 2;
221
+    dlinesize /= 2;
222
+
223
+    for (y = 0; y < h; y++) {
224
+        for (x = 0; x < w; x++) {
225
+            if ((bsrc[x + y * blinesize] > t) && (asrc[x + y * alinesize] > t) && !passed(s, x, y, w)) {
226
+                int posx, posy;
227
+
228
+                dst[x + y * dlinesize] = asrc[x + y * alinesize];
229
+
230
+                push(s, x, y, w);
231
+
232
+                while (!is_empty(s)) {
233
+                    int x_min, x_max, y_min, y_max, yy, xx;
234
+
235
+                    pop(s, &posx, &posy);
236
+
237
+                    x_min = posx > 0 ? posx - 1 : 0;
238
+                    x_max = posx < w - 1 ? posx + 1 : posx;
239
+                    y_min = posy > 0 ? posy - 1 : 0;
240
+                    y_max = posy < h - 1 ? posy + 1 : posy;
241
+
242
+                    for (yy = y_min; yy <= y_max; yy++) {
243
+                        for (xx = x_min; xx <= x_max; xx++) {
244
+                            if ((asrc[xx + yy * alinesize] > t) && !passed(s, xx, yy, w)) {
245
+                                dst[xx + yy * dlinesize] = asrc[xx + yy * alinesize];
246
+                                push(s, xx, yy, w);
247
+                            }
248
+                        }
249
+                    }
250
+                }
251
+            }
252
+        }
253
+    }
254
+}
255
+
256
+static int config_input(AVFilterLink *inlink)
257
+{
258
+    AVFilterContext *ctx = inlink->dst;
259
+    HysteresisContext *s = ctx->priv;
260
+    const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
261
+    int vsub, hsub;
262
+
263
+    s->nb_planes = av_pix_fmt_count_planes(inlink->format);
264
+
265
+    hsub = desc->log2_chroma_w;
266
+    vsub = desc->log2_chroma_h;
267
+    s->height[1] = s->height[2] = AV_CEIL_RSHIFT(inlink->h, vsub);
268
+    s->height[0] = s->height[3] = inlink->h;
269
+    s->width[1]  = s->width[2]  = AV_CEIL_RSHIFT(inlink->w, hsub);
270
+    s->width[0]  = s->width[3]  = inlink->w;
271
+
272
+    s->depth = desc->comp[0].depth;
273
+
274
+    if (desc->comp[0].depth == 8)
275
+        s->hysteresis = hysteresis8;
276
+    else
277
+        s->hysteresis = hysteresis16;
278
+
279
+    s->map = av_calloc(inlink->w, inlink->h * sizeof (*s->map));
280
+    if (!s->map)
281
+        return AVERROR(ENOMEM);
282
+
283
+    s->xy = av_calloc(inlink->w, inlink->h * sizeof(*s->xy));
284
+    if (!s->xy)
285
+        return AVERROR(ENOMEM);
286
+
287
+    return 0;
288
+}
289
+
290
+static int config_output(AVFilterLink *outlink)
291
+{
292
+    AVFilterContext *ctx = outlink->src;
293
+    HysteresisContext *s = ctx->priv;
294
+    AVFilterLink *base = ctx->inputs[0];
295
+    AVFilterLink *alt = ctx->inputs[1];
296
+    FFFrameSyncIn *in;
297
+    int ret;
298
+
299
+    if (base->format != alt->format) {
300
+        av_log(ctx, AV_LOG_ERROR, "inputs must be of same pixel format\n");
301
+        return AVERROR(EINVAL);
302
+    }
303
+    if (base->w                       != alt->w ||
304
+        base->h                       != alt->h ||
305
+        base->sample_aspect_ratio.num != alt->sample_aspect_ratio.num ||
306
+        base->sample_aspect_ratio.den != alt->sample_aspect_ratio.den) {
307
+        av_log(ctx, AV_LOG_ERROR, "First input link %s parameters "
308
+               "(size %dx%d, SAR %d:%d) do not match the corresponding "
309
+               "second input link %s parameters (%dx%d, SAR %d:%d)\n",
310
+               ctx->input_pads[0].name, base->w, base->h,
311
+               base->sample_aspect_ratio.num,
312
+               base->sample_aspect_ratio.den,
313
+               ctx->input_pads[1].name,
314
+               alt->w, alt->h,
315
+               alt->sample_aspect_ratio.num,
316
+               alt->sample_aspect_ratio.den);
317
+        return AVERROR(EINVAL);
318
+    }
319
+
320
+    outlink->w = base->w;
321
+    outlink->h = base->h;
322
+    outlink->time_base = base->time_base;
323
+    outlink->sample_aspect_ratio = base->sample_aspect_ratio;
324
+    outlink->frame_rate = base->frame_rate;
325
+
326
+    if ((ret = ff_framesync_init(&s->fs, ctx, 2)) < 0)
327
+        return ret;
328
+
329
+    in = s->fs.in;
330
+    in[0].time_base = base->time_base;
331
+    in[1].time_base = alt->time_base;
332
+    in[0].sync   = 1;
333
+    in[0].before = EXT_STOP;
334
+    in[0].after  = EXT_INFINITY;
335
+    in[1].sync   = 1;
336
+    in[1].before = EXT_STOP;
337
+    in[1].after  = EXT_INFINITY;
338
+    s->fs.opaque   = s;
339
+    s->fs.on_event = process_frame;
340
+
341
+    return ff_framesync_configure(&s->fs);
342
+}
343
+
344
+static int filter_frame(AVFilterLink *inlink, AVFrame *buf)
345
+{
346
+    HysteresisContext *s = inlink->dst->priv;
347
+    return ff_framesync_filter_frame(&s->fs, inlink, buf);
348
+}
349
+
350
+static int request_frame(AVFilterLink *outlink)
351
+{
352
+    HysteresisContext *s = outlink->src->priv;
353
+    return ff_framesync_request_frame(&s->fs, outlink);
354
+}
355
+
356
+static av_cold void uninit(AVFilterContext *ctx)
357
+{
358
+    HysteresisContext *s = ctx->priv;
359
+
360
+    ff_framesync_uninit(&s->fs);
361
+    av_freep(&s->map);
362
+    av_freep(&s->xy);
363
+}
364
+
365
+static const AVFilterPad hysteresis_inputs[] = {
366
+    {
367
+        .name         = "base",
368
+        .type         = AVMEDIA_TYPE_VIDEO,
369
+        .filter_frame = filter_frame,
370
+        .config_props = config_input,
371
+    },
372
+    {
373
+        .name         = "alt",
374
+        .type         = AVMEDIA_TYPE_VIDEO,
375
+        .filter_frame = filter_frame,
376
+    },
377
+    { NULL }
378
+};
379
+
380
+static const AVFilterPad hysteresis_outputs[] = {
381
+    {
382
+        .name          = "default",
383
+        .type          = AVMEDIA_TYPE_VIDEO,
384
+        .config_props  = config_output,
385
+        .request_frame = request_frame,
386
+    },
387
+    { NULL }
388
+};
389
+
390
+AVFilter ff_vf_hysteresis = {
391
+    .name          = "hysteresis",
392
+    .description   = NULL_IF_CONFIG_SMALL("Grow first stream into second stream by connecting components."),
393
+    .priv_size     = sizeof(HysteresisContext),
394
+    .uninit        = uninit,
395
+    .query_formats = query_formats,
396
+    .inputs        = hysteresis_inputs,
397
+    .outputs       = hysteresis_outputs,
398
+    .priv_class    = &hysteresis_class,
399
+    .flags         = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL,
400
+};