Browse code

avfilter: add threshold filter

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

Paul B Mahol authored on 2016/02/07 01:11:07
Showing 6 changed files
... ...
@@ -19,6 +19,7 @@ version <next>:
19 19
 - Sample Dump eXchange demuxer
20 20
 - abitscope multimedia filter
21 21
 - Scenarist Closed Captions demuxer and muxer
22
+- threshold filter
22 23
 
23 24
 version 3.2:
24 25
 - libopenmpt demuxer
... ...
@@ -13192,6 +13192,63 @@ PAL output (25i):
13192 13192
 16p: 33333334
13193 13193
 @end example
13194 13194
 
13195
+@section threshold
13196
+
13197
+Apply threshold effect to video stream.
13198
+
13199
+This filter needs four video streams to perform thresholding.
13200
+First stream is stream we are filtering.
13201
+Second stream is holding threshold values, third stream is holding min values,
13202
+and last, fourth stream is holding max values.
13203
+
13204
+The filter accepts the following option:
13205
+
13206
+@table @option
13207
+@item planes
13208
+Set which planes will be processed, unprocessed planes will be copied.
13209
+By default value 0xf, all planes will be processed.
13210
+@end table
13211
+
13212
+For example if first stream pixel's component value is less then threshold value
13213
+of pixel component from 2nd threshold stream, third stream value will picked,
13214
+otherwise fourth stream pixel component value will be picked.
13215
+
13216
+Using color source filter one can perform various types of thresholding:
13217
+
13218
+@subsection Examples
13219
+
13220
+@itemize
13221
+@item
13222
+Binary threshold, using gray color as threshold:
13223
+@example
13224
+ffmpeg -i 320x240.avi -f lavfi -i color=gray -f lavfi -i color=black -f lavfi -i color=white -lavfi threshold output.avi
13225
+@end example
13226
+
13227
+@item
13228
+Inverted binary threshold, using gray color as threshold:
13229
+@example
13230
+ffmpeg -i 320x240.avi -f lavfi -i color=gray -f lavfi -i color=white -f lavfi -i color=black -lavfi threshold output.avi
13231
+@end example
13232
+
13233
+@item
13234
+Truncate binary threshold, using gray color as threshold:
13235
+@example
13236
+ffmpeg -i 320x240.avi -f lavfi -i color=gray -i 320x240.avi -f lavfi -i color=gray -lavfi threshold output.avi
13237
+@end example
13238
+
13239
+@item
13240
+Threshold to zero, using gray color as threshold:
13241
+@example
13242
+ffmpeg -i 320x240.avi -f lavfi -i color=gray -f lavfi -i color=white -i 320x240.avi -lavfi threshold output.avi
13243
+@end example
13244
+
13245
+@item
13246
+Inverted threshold to zero, using gray color as threshold:
13247
+@example
13248
+ffmpeg -i 320x240.avi -f lavfi -i color=gray -i 320x240.avi -f lavfi -i color=white -lavfi threshold output.avi
13249
+@end example
13250
+@end itemize
13251
+
13195 13252
 @section thumbnail
13196 13253
 Select the most representative frame in a given sequence of consecutive frames.
13197 13254
 
... ...
@@ -289,6 +289,7 @@ OBJS-$(CONFIG_SWAPRECT_FILTER)               += vf_swaprect.o
289 289
 OBJS-$(CONFIG_SWAPUV_FILTER)                 += vf_swapuv.o
290 290
 OBJS-$(CONFIG_TBLEND_FILTER)                 += vf_blend.o dualinput.o framesync.o
291 291
 OBJS-$(CONFIG_TELECINE_FILTER)               += vf_telecine.o
292
+OBJS-$(CONFIG_THRESHOLD_FILTER)              += vf_threshold.o
292 293
 OBJS-$(CONFIG_THUMBNAIL_FILTER)              += vf_thumbnail.o
293 294
 OBJS-$(CONFIG_TILE_FILTER)                   += vf_tile.o
294 295
 OBJS-$(CONFIG_TINTERLACE_FILTER)             += vf_tinterlace.o
... ...
@@ -304,6 +304,7 @@ void avfilter_register_all(void)
304 304
     REGISTER_FILTER(SWAPUV,         swapuv,         vf);
305 305
     REGISTER_FILTER(TBLEND,         tblend,         vf);
306 306
     REGISTER_FILTER(TELECINE,       telecine,       vf);
307
+    REGISTER_FILTER(THRESHOLD,      threshold,      vf);
307 308
     REGISTER_FILTER(THUMBNAIL,      thumbnail,      vf);
308 309
     REGISTER_FILTER(TILE,           tile,           vf);
309 310
     REGISTER_FILTER(TINTERLACE,     tinterlace,     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  71
33
+#define LIBAVFILTER_VERSION_MINOR  72
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,349 @@
0
+/*
1
+ * Copyright (c) 2016 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
+/**
21
+ * @file
22
+ * threshold video filter
23
+ */
24
+
25
+#include "libavutil/imgutils.h"
26
+#include "libavutil/internal.h"
27
+#include "libavutil/opt.h"
28
+#include "libavutil/pixdesc.h"
29
+#include "avfilter.h"
30
+#include "framesync.h"
31
+#include "internal.h"
32
+#include "video.h"
33
+
34
+typedef struct ThresholdContext {
35
+    const AVClass *class;
36
+
37
+    int planes;
38
+    int bpc;
39
+
40
+    int nb_planes;
41
+    int width[4], height[4];
42
+
43
+    void (*threshold)(const uint8_t *in, const uint8_t *threshold,
44
+                      const uint8_t *min, const uint8_t *max,
45
+                      uint8_t *out,
46
+                      ptrdiff_t ilinesize, ptrdiff_t tlinesize,
47
+                      ptrdiff_t flinesize, ptrdiff_t slinesize,
48
+                      ptrdiff_t olinesize,
49
+                      int w, int h);
50
+
51
+    AVFrame *frames[4];
52
+    FFFrameSync fs;
53
+} ThresholdContext;
54
+
55
+#define OFFSET(x) offsetof(ThresholdContext, x)
56
+#define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
57
+
58
+static const AVOption threshold_options[] = {
59
+    { "planes", "set planes to filter", OFFSET(planes), AV_OPT_TYPE_INT,  {.i64=15}, 0, 15, FLAGS},
60
+    { NULL }
61
+};
62
+
63
+AVFILTER_DEFINE_CLASS(threshold);
64
+
65
+static int query_formats(AVFilterContext *ctx)
66
+{
67
+    static const enum AVPixelFormat pix_fmts[] = {
68
+        AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV440P,
69
+        AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P,
70
+        AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUV420P,
71
+        AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ420P,
72
+        AV_PIX_FMT_YUVJ411P, AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P,
73
+        AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV444P9,
74
+        AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10,
75
+        AV_PIX_FMT_YUV420P16, AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV444P16,
76
+        AV_PIX_FMT_YUVA420P9, AV_PIX_FMT_YUVA422P9, AV_PIX_FMT_YUVA444P9,
77
+        AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA444P10,
78
+        AV_PIX_FMT_YUVA420P16, AV_PIX_FMT_YUVA422P16, AV_PIX_FMT_YUVA444P16,
79
+        AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRP9, AV_PIX_FMT_GBRP10,
80
+        AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRP14, AV_PIX_FMT_GBRP16,
81
+        AV_PIX_FMT_GBRAP, AV_PIX_FMT_GBRAP16,
82
+        AV_PIX_FMT_GRAY8,  AV_PIX_FMT_GRAY10,
83
+        AV_PIX_FMT_GRAY12, AV_PIX_FMT_GRAY16,
84
+        AV_PIX_FMT_NONE
85
+    };
86
+
87
+    return ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
88
+}
89
+
90
+static int process_frame(FFFrameSync *fs)
91
+{
92
+    AVFilterContext *ctx = fs->parent;
93
+    ThresholdContext *s = fs->opaque;
94
+    AVFilterLink *outlink = ctx->outputs[0];
95
+    AVFrame *out, *in, *threshold, *min, *max;
96
+    int ret;
97
+
98
+    if ((ret = ff_framesync_get_frame(&s->fs, 0, &in,        0)) < 0 ||
99
+        (ret = ff_framesync_get_frame(&s->fs, 1, &threshold, 0)) < 0 ||
100
+        (ret = ff_framesync_get_frame(&s->fs, 2, &min,       0)) < 0 ||
101
+        (ret = ff_framesync_get_frame(&s->fs, 3, &max,       0)) < 0)
102
+        return ret;
103
+
104
+    if (ctx->is_disabled) {
105
+        out = av_frame_clone(in);
106
+        if (!out)
107
+            return AVERROR(ENOMEM);
108
+    } else {
109
+        int p;
110
+
111
+        out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
112
+        if (!out)
113
+            return AVERROR(ENOMEM);
114
+        av_frame_copy_props(out, in);
115
+
116
+        for (p = 0; p < s->nb_planes; p++) {
117
+            if (!(s->planes & (1 << p))) {
118
+                av_image_copy_plane(out->data[p], out->linesize[p],
119
+                                    in->data[p], in->linesize[p],
120
+                                    s->width[p] * s->bpc,
121
+                                    s->height[p]);
122
+                continue;
123
+            }
124
+            s->threshold(in->data[p], threshold->data[p],
125
+                         min->data[p], max->data[p],
126
+                         out->data[p],
127
+                         in->linesize[p], threshold->linesize[p],
128
+                         min->linesize[p], max->linesize[p],
129
+                         out->linesize[p],
130
+                         s->width[p], s->height[p]);
131
+        }
132
+    }
133
+
134
+    out->pts = av_rescale_q(s->fs.pts, s->fs.time_base, outlink->time_base);
135
+
136
+    return ff_filter_frame(outlink, out);
137
+}
138
+
139
+static void threshold8(const uint8_t *in, const uint8_t *threshold,
140
+                       const uint8_t *min, const uint8_t *max,
141
+                       uint8_t *out,
142
+                       ptrdiff_t ilinesize, ptrdiff_t tlinesize,
143
+                       ptrdiff_t flinesize, ptrdiff_t slinesize,
144
+                       ptrdiff_t olinesize,
145
+                       int w, int h)
146
+{
147
+    int x, y;
148
+
149
+    for (y = 0; y < h; y++) {
150
+        for (x = 0; x < w; x++) {
151
+            out[x] = in[x] < threshold[x] ? min[x] : max[x];
152
+        }
153
+
154
+        in        += ilinesize;
155
+        threshold += tlinesize;
156
+        min       += flinesize;
157
+        max       += flinesize;
158
+        out       += olinesize;
159
+    }
160
+}
161
+
162
+static void threshold16(const uint8_t *iin, const uint8_t *tthreshold,
163
+                        const uint8_t *ffirst, const uint8_t *ssecond,
164
+                        uint8_t *oout,
165
+                        ptrdiff_t ilinesize, ptrdiff_t tlinesize,
166
+                        ptrdiff_t flinesize, ptrdiff_t slinesize,
167
+                        ptrdiff_t olinesize,
168
+                        int w, int h)
169
+{
170
+    const uint16_t *in = (const uint16_t *)iin;
171
+    const uint16_t *threshold = (const uint16_t *)tthreshold;
172
+    const uint16_t *min = (const uint16_t *)ffirst;
173
+    const uint16_t *max = (const uint16_t *)ssecond;
174
+    uint16_t *out = (uint16_t *)oout;
175
+    int x, y;
176
+
177
+    for (y = 0; y < h; y++) {
178
+        for (x = 0; x < w; x++) {
179
+            out[x] = in[x] < threshold[x] ? min[x] : max[x];
180
+        }
181
+
182
+        in        += ilinesize / 2;
183
+        threshold += tlinesize / 2;
184
+        min       += flinesize / 2;
185
+        max       += flinesize / 2;
186
+        out       += olinesize / 2;
187
+    }
188
+}
189
+
190
+static int config_input(AVFilterLink *inlink)
191
+{
192
+    AVFilterContext *ctx = inlink->dst;
193
+    ThresholdContext *s = ctx->priv;
194
+    const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
195
+    int vsub, hsub;
196
+
197
+    s->nb_planes = av_pix_fmt_count_planes(inlink->format);
198
+
199
+    hsub = desc->log2_chroma_w;
200
+    vsub = desc->log2_chroma_h;
201
+    s->height[1] = s->height[2] = AV_CEIL_RSHIFT(inlink->h, vsub);
202
+    s->height[0] = s->height[3] = inlink->h;
203
+    s->width[1]  = s->width[2]  = AV_CEIL_RSHIFT(inlink->w, hsub);
204
+    s->width[0]  = s->width[3]  = inlink->w;
205
+
206
+    if (desc->comp[0].depth == 8) {
207
+        s->threshold = threshold8;
208
+        s->bpc = 1;
209
+    } else {
210
+        s->threshold = threshold16;
211
+        s->bpc = 2;
212
+    }
213
+
214
+    return 0;
215
+}
216
+
217
+static int config_output(AVFilterLink *outlink)
218
+{
219
+    AVFilterContext *ctx = outlink->src;
220
+    ThresholdContext *s = ctx->priv;
221
+    AVFilterLink *base = ctx->inputs[0];
222
+    AVFilterLink *threshold = ctx->inputs[1];
223
+    AVFilterLink *min = ctx->inputs[2];
224
+    AVFilterLink *max = ctx->inputs[3];
225
+    FFFrameSyncIn *in;
226
+    int ret;
227
+
228
+    if (base->format != threshold->format ||
229
+        base->format != min->format ||
230
+        base->format != max->format) {
231
+        av_log(ctx, AV_LOG_ERROR, "inputs must be of same pixel format\n");
232
+        return AVERROR(EINVAL);
233
+    }
234
+    if (base->w                       != threshold->w ||
235
+        base->h                       != threshold->h ||
236
+        base->w                       != min->w ||
237
+        base->h                       != min->h ||
238
+        base->w                       != max->w ||
239
+        base->h                       != max->h) {
240
+        av_log(ctx, AV_LOG_ERROR, "First input link %s parameters "
241
+               "(size %dx%d) do not match the corresponding "
242
+               "second input link %s parameters (%dx%d) "
243
+               "and/or third input link %s parameters (%dx%d) "
244
+               "and/or fourth input link %s parameters (%dx%d)\n",
245
+               ctx->input_pads[0].name, base->w, base->h,
246
+               ctx->input_pads[1].name, threshold->w, threshold->h,
247
+               ctx->input_pads[2].name, min->w, min->h,
248
+               ctx->input_pads[3].name, max->w, max->h);
249
+        return AVERROR(EINVAL);
250
+    }
251
+
252
+    outlink->w = base->w;
253
+    outlink->h = base->h;
254
+    outlink->time_base = base->time_base;
255
+    outlink->sample_aspect_ratio = base->sample_aspect_ratio;
256
+    outlink->frame_rate = base->frame_rate;
257
+
258
+    if ((ret = ff_framesync_init(&s->fs, ctx, 4)) < 0)
259
+        return ret;
260
+
261
+    in = s->fs.in;
262
+    in[0].time_base = base->time_base;
263
+    in[1].time_base = threshold->time_base;
264
+    in[2].time_base = min->time_base;
265
+    in[3].time_base = max->time_base;
266
+    in[0].sync   = 1;
267
+    in[0].before = EXT_STOP;
268
+    in[0].after  = EXT_STOP;
269
+    in[1].sync   = 1;
270
+    in[1].before = EXT_STOP;
271
+    in[1].after  = EXT_STOP;
272
+    in[2].sync   = 1;
273
+    in[2].before = EXT_STOP;
274
+    in[2].after  = EXT_STOP;
275
+    in[3].sync   = 1;
276
+    in[3].before = EXT_STOP;
277
+    in[3].after  = EXT_STOP;
278
+    s->fs.opaque   = s;
279
+    s->fs.on_event = process_frame;
280
+
281
+    return ff_framesync_configure(&s->fs);
282
+}
283
+
284
+static int filter_frame(AVFilterLink *inlink, AVFrame *buf)
285
+{
286
+    ThresholdContext *s = inlink->dst->priv;
287
+    return ff_framesync_filter_frame(&s->fs, inlink, buf);
288
+}
289
+
290
+static int request_frame(AVFilterLink *outlink)
291
+{
292
+    ThresholdContext *s = outlink->src->priv;
293
+    return ff_framesync_request_frame(&s->fs, outlink);
294
+}
295
+
296
+static av_cold void uninit(AVFilterContext *ctx)
297
+{
298
+    ThresholdContext *s = ctx->priv;
299
+
300
+    ff_framesync_uninit(&s->fs);
301
+}
302
+
303
+static const AVFilterPad inputs[] = {
304
+    {
305
+        .name         = "default",
306
+        .type         = AVMEDIA_TYPE_VIDEO,
307
+        .filter_frame = filter_frame,
308
+        .config_props = config_input,
309
+    },
310
+    {
311
+        .name         = "threshold",
312
+        .type         = AVMEDIA_TYPE_VIDEO,
313
+        .filter_frame = filter_frame,
314
+    },
315
+    {
316
+        .name         = "min",
317
+        .type         = AVMEDIA_TYPE_VIDEO,
318
+        .filter_frame = filter_frame,
319
+    },
320
+    {
321
+        .name         = "max",
322
+        .type         = AVMEDIA_TYPE_VIDEO,
323
+        .filter_frame = filter_frame,
324
+    },
325
+    { NULL }
326
+};
327
+
328
+static const AVFilterPad outputs[] = {
329
+    {
330
+        .name          = "default",
331
+        .type          = AVMEDIA_TYPE_VIDEO,
332
+        .config_props  = config_output,
333
+        .request_frame = request_frame,
334
+    },
335
+    { NULL }
336
+};
337
+
338
+AVFilter ff_vf_threshold = {
339
+    .name          = "threshold",
340
+    .description   = NULL_IF_CONFIG_SMALL("Threshold first video stream using other video streams."),
341
+    .priv_size     = sizeof(ThresholdContext),
342
+    .priv_class    = &threshold_class,
343
+    .uninit        = uninit,
344
+    .query_formats = query_formats,
345
+    .inputs        = inputs,
346
+    .outputs       = outputs,
347
+    .flags         = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL,
348
+};