History

avfilter: add maskedthreshold filter

Paul B Mahol committed 6 years ago

Commit 4457f751 parent 9987f6d6 files · +323 −1Browse files

Showing the changes to libavfilter (4 of 6 files). Show all

Unified · Split

4 files
  1. libavfilter/Makefile +1 −0
  2. libavfilter/allfilters.c +1 −0
  3. libavfilter/version.h +1 −1
  4. libavfilter/vf_maskedthreshold.c +298 −0
libavfilter/Makefile+1−0
@@ -302,6 +302,7 @@ OBJS-$(CONFIG_MASKEDCLAMP_FILTER) += vf_maskedclamp.o framesync.o · 301 unmodified lines
302302 OBJS-$(CONFIG_MASKEDMAX_FILTER) += vf_maskedminmax.o framesync.o
303303 OBJS-$(CONFIG_MASKEDMERGE_FILTER) += vf_maskedmerge.o framesync.o
304304 OBJS-$(CONFIG_MASKEDMIN_FILTER) += vf_maskedminmax.o framesync.o
305+OBJS-$(CONFIG_MASKEDTHRESHOLD_FILTER) += vf_maskedthreshold.o framesync.o
305306 OBJS-$(CONFIG_MASKFUN_FILTER) += vf_maskfun.o
306307 OBJS-$(CONFIG_MCDEINT_FILTER) += vf_mcdeint.o
307308 OBJS-$(CONFIG_MEDIAN_FILTER) += vf_median.o
libavfilter/allfilters.c+1−0
@@ -287,6 +287,7 @@ extern AVFilter ff_vf_maskedclamp; · 286 unmodified lines
287287 extern AVFilter ff_vf_maskedmax;
288288 extern AVFilter ff_vf_maskedmerge;
289289 extern AVFilter ff_vf_maskedmin;
290+extern AVFilter ff_vf_maskedthreshold;
290291 extern AVFilter ff_vf_maskfun;
291292 extern AVFilter ff_vf_mcdeint;
292293 extern AVFilter ff_vf_median;
libavfilter/version.h+1−1
@@ -30,7 +30,7 @@ · 29 unmodified lines
3030 #include "libavutil/version.h"
3131
3232 #define LIBAVFILTER_VERSION_MAJOR 7
33−#define LIBAVFILTER_VERSION_MINOR 78
33+#define LIBAVFILTER_VERSION_MINOR 79
3434 #define LIBAVFILTER_VERSION_MICRO 100
3535
3636
libavfilter/vf_maskedthreshold.c+298−0
@@ -0,0 +1,298 @@
1+/*
2+ * Copyright (c) 2020 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+typedef struct MaskedThresholdContext {
31+ const AVClass *class;
32+
33+ int threshold;
34+ int planes;
35+
36+ int linesize[4];
37+ int planewidth[4], planeheight[4];
38+ int nb_planes;
39+ int depth;
40+ FFFrameSync fs;
41+
42+ void (*maskedthreshold)(const uint8_t *src, const uint8_t *ref, uint8_t *dst, int threshold, int w);
43+} MaskedThresholdContext;
44+
45+#define OFFSET(x) offsetof(MaskedThresholdContext, x)
46+#define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
47+
48+typedef struct ThreadData {
49+ AVFrame *src, *ref, *dst;
50+} ThreadData;
51+
52+static const AVOption maskedthreshold_options[] = {
53+ { "threshold", "set threshold", OFFSET(threshold), AV_OPT_TYPE_INT, {.i64=1}, 0, UINT16_MAX, FLAGS },
54+ { "planes", "set planes", OFFSET(planes), AV_OPT_TYPE_INT, {.i64=0xF}, 0, 0xF, FLAGS },
55+ { NULL }
56+};
57+
58+static int query_formats(AVFilterContext *ctx)
59+{
60+ static const enum AVPixelFormat pix_fmts[] = {
61+ AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV440P,
62+ AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P,
63+ AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUV420P,
64+ AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ420P,
65+ AV_PIX_FMT_YUVJ411P, AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P,
66+ AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV444P9,
67+ AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10,
68+ AV_PIX_FMT_YUV420P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV444P12, AV_PIX_FMT_YUV440P12,
69+ AV_PIX_FMT_YUV420P14, AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV444P14,
70+ AV_PIX_FMT_YUV420P16, AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV444P16,
71+ AV_PIX_FMT_YUVA420P9, AV_PIX_FMT_YUVA422P9, AV_PIX_FMT_YUVA444P9,
72+ AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA444P10,
73+ AV_PIX_FMT_YUVA422P12, AV_PIX_FMT_YUVA444P12,
74+ AV_PIX_FMT_YUVA420P16, AV_PIX_FMT_YUVA422P16, AV_PIX_FMT_YUVA444P16,
75+ AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRP9, AV_PIX_FMT_GBRP10,
76+ AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRP14, AV_PIX_FMT_GBRP16,
77+ AV_PIX_FMT_GBRAP, AV_PIX_FMT_GBRAP10, AV_PIX_FMT_GBRAP12, AV_PIX_FMT_GBRAP16,
78+ AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY9, AV_PIX_FMT_GRAY10, AV_PIX_FMT_GRAY12, AV_PIX_FMT_GRAY14, AV_PIX_FMT_GRAY16,
79+ AV_PIX_FMT_NONE
80+ };
81+
82+ return ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
83+}
84+
85+static void threshold8(const uint8_t *src, const uint8_t *ref, uint8_t *dst, int threshold, int w)
86+{
87+ for (int x = 0; x < w; x++)
88+ dst[x] = FFABS(src[x] - ref[x]) <= threshold ? src[x] : ref[x];
89+}
90+
91+static void threshold16(const uint8_t *ssrc, const uint8_t *rref, uint8_t *ddst, int threshold, int w)
92+{
93+ const uint16_t *src = (const uint16_t *)ssrc;
94+ const uint16_t *ref = (const uint16_t *)rref;
95+ uint16_t *dst = (uint16_t *)ddst;
96+
97+ for (int x = 0; x < w; x++)
98+ dst[x] = FFABS(src[x] - ref[x]) <= threshold ? src[x] : ref[x];
99+}
100+
101+static int config_input(AVFilterLink *inlink)
102+{
103+ AVFilterContext *ctx = inlink->dst;
104+ MaskedThresholdContext *s = ctx->priv;
105+ const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
106+ int vsub, hsub, ret;
107+
108+ s->nb_planes = av_pix_fmt_count_planes(inlink->format);
109+
110+ if ((ret = av_image_fill_linesizes(s->linesize, inlink->format, inlink->w)) < 0)
111+ return ret;
112+
113+ hsub = desc->log2_chroma_w;
114+ vsub = desc->log2_chroma_h;
115+ s->planeheight[1] = s->planeheight[2] = AV_CEIL_RSHIFT(inlink->h, vsub);
116+ s->planeheight[0] = s->planeheight[3] = inlink->h;
117+ s->planewidth[1] = s->planewidth[2] = AV_CEIL_RSHIFT(inlink->w, hsub);
118+ s->planewidth[0] = s->planewidth[3] = inlink->w;
119+
120+ s->depth = desc->comp[0].depth;
121+
122+ if (desc->comp[0].depth == 8)
123+ s->maskedthreshold = threshold8;
124+ else
125+ s->maskedthreshold = threshold16;
126+
127+ return 0;
128+}
129+
130+static int threshold_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
131+{
132+ MaskedThresholdContext *s = ctx->priv;
133+ const int threshold = s->threshold;
134+ ThreadData *td = arg;
135+
136+ for (int p = 0; p < s->nb_planes; p++) {
137+ const ptrdiff_t src_linesize = td->src->linesize[p];
138+ const ptrdiff_t ref_linesize = td->ref->linesize[p];
139+ const ptrdiff_t dst_linesize = td->dst->linesize[p];
140+ const int w = s->planewidth[p];
141+ const int h = s->planeheight[p];
142+ const int slice_start = (h * jobnr) / nb_jobs;
143+ const int slice_end = (h * (jobnr+1)) / nb_jobs;
144+ const uint8_t *src = td->src->data[p] + slice_start * src_linesize;
145+ const uint8_t *ref = td->ref->data[p] + slice_start * ref_linesize;
146+ uint8_t *dst = td->dst->data[p] + slice_start * dst_linesize;
147+
148+ if (!((1 << p) & s->planes)) {
149+ av_image_copy_plane(dst, dst_linesize, ref, ref_linesize,
150+ s->linesize[p], slice_end - slice_start);
151+ continue;
152+ }
153+
154+ for (int y = slice_start; y < slice_end; y++) {
155+ s->maskedthreshold(src, ref, dst, threshold, w);
156+
157+ dst += dst_linesize;
158+ src += src_linesize;
159+ ref += ref_linesize;
160+ }
161+ }
162+
163+ return 0;
164+}
165+
166+static int process_frame(FFFrameSync *fs)
167+{
168+ AVFilterContext *ctx = fs->parent;
169+ MaskedThresholdContext *s = fs->opaque;
170+ AVFilterLink *outlink = ctx->outputs[0];
171+ AVFrame *out, *src, *ref;
172+ int ret;
173+
174+ if ((ret = ff_framesync_get_frame(&s->fs, 0, &src, 0)) < 0 ||
175+ (ret = ff_framesync_get_frame(&s->fs, 1, &ref, 0)) < 0)
176+ return ret;
177+
178+ if (ctx->is_disabled) {
179+ out = av_frame_clone(src);
180+ if (!out)
181+ return AVERROR(ENOMEM);
182+ } else {
183+ ThreadData td;
184+
185+ out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
186+ if (!out)
187+ return AVERROR(ENOMEM);
188+ av_frame_copy_props(out, src);
189+
190+ td.src = src;
191+ td.ref = ref;
192+ td.dst = out;
193+
194+ ctx->internal->execute(ctx, threshold_slice, &td, NULL, FFMIN(s->planeheight[2],
195+ ff_filter_get_nb_threads(ctx)));
196+ }
197+ out->pts = av_rescale_q(s->fs.pts, s->fs.time_base, outlink->time_base);
198+
199+ return ff_filter_frame(outlink, out);
200+}
201+
202+static int config_output(AVFilterLink *outlink)
203+{
204+ AVFilterContext *ctx = outlink->src;
205+ MaskedThresholdContext *s = ctx->priv;
206+ AVFilterLink *source = ctx->inputs[0];
207+ AVFilterLink *ref = ctx->inputs[1];
208+ FFFrameSyncIn *in;
209+ int ret;
210+
211+ if (source->format != ref->format) {
212+ av_log(ctx, AV_LOG_ERROR, "inputs must be of same pixel format\n");
213+ return AVERROR(EINVAL);
214+ }
215+ if (source->w != ref->w || source->h != ref->h) {
216+ av_log(ctx, AV_LOG_ERROR, "First input link %s parameters "
217+ "(size %dx%d) do not match the corresponding "
218+ "second input link %s parameters (%dx%d)\n",
219+ ctx->input_pads[0].name, source->w, source->h,
220+ ctx->input_pads[1].name, ref->w, ref->h);
221+ return AVERROR(EINVAL);
222+ }
223+
224+ outlink->w = source->w;
225+ outlink->h = source->h;
226+ outlink->sample_aspect_ratio = source->sample_aspect_ratio;
227+ outlink->frame_rate = source->frame_rate;
228+
229+ if ((ret = ff_framesync_init(&s->fs, ctx, 2)) < 0)
230+ return ret;
231+
232+ in = s->fs.in;
233+ in[0].time_base = source->time_base;
234+ in[1].time_base = ref->time_base;
235+ in[0].sync = 1;
236+ in[0].before = EXT_STOP;
237+ in[0].after = EXT_INFINITY;
238+ in[1].sync = 1;
239+ in[1].before = EXT_STOP;
240+ in[1].after = EXT_INFINITY;
241+ s->fs.opaque = s;
242+ s->fs.on_event = process_frame;
243+
244+ ret = ff_framesync_configure(&s->fs);
245+ outlink->time_base = s->fs.time_base;
246+
247+ return ret;
248+}
249+
250+static int activate(AVFilterContext *ctx)
251+{
252+ MaskedThresholdContext *s = ctx->priv;
253+ return ff_framesync_activate(&s->fs);
254+}
255+
256+static av_cold void uninit(AVFilterContext *ctx)
257+{
258+ MaskedThresholdContext *s = ctx->priv;
259+
260+ ff_framesync_uninit(&s->fs);
261+}
262+
263+static const AVFilterPad maskedthreshold_inputs[] = {
264+ {
265+ .name = "source",
266+ .type = AVMEDIA_TYPE_VIDEO,
267+ .config_props = config_input,
268+ },
269+ {
270+ .name = "reference",
271+ .type = AVMEDIA_TYPE_VIDEO,
272+ },
273+ { NULL }
274+};
275+
276+static const AVFilterPad maskedthreshold_outputs[] = {
277+ {
278+ .name = "default",
279+ .type = AVMEDIA_TYPE_VIDEO,
280+ .config_props = config_output,
281+ },
282+ { NULL }
283+};
284+
285+AVFILTER_DEFINE_CLASS(maskedthreshold);
286+
287+AVFilter ff_vf_maskedthreshold = {
288+ .name = "maskedthreshold",
289+ .description = NULL_IF_CONFIG_SMALL("Pick pixels comparing absolute difference of two streams with threshold."),
290+ .priv_class = &maskedthreshold_class,
291+ .priv_size = sizeof(MaskedThresholdContext),
292+ .uninit = uninit,
293+ .activate = activate,
294+ .query_formats = query_formats,
295+ .inputs = maskedthreshold_inputs,
296+ .outputs = maskedthreshold_outputs,
297+ .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL | AVFILTER_FLAG_SLICE_THREADS,
298+};

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t