2 * Copyright (c) 2020 Paul B Mahol
4 * This file is part of FFmpeg.
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.
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.
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
21 #include "libavutil/imgutils.h"
22 #include "libavutil/pixdesc.h"
23 #include "libavutil/opt.h"
28 #include "framesync.h"
30 typedef struct MaskedThresholdContext {
37 int planewidth[4], planeheight[4];
42 void (*maskedthreshold)(const uint8_t *src, const uint8_t *ref, uint8_t *dst, int threshold, int w);
43 } MaskedThresholdContext;
45 #define OFFSET(x) offsetof(MaskedThresholdContext, x)
46 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
48 typedef struct ThreadData {
49 AVFrame *src, *ref, *dst;
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 },
58 static int query_formats(AVFilterContext *ctx)
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,
82 return ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
85 static void threshold8(const uint8_t *src, const uint8_t *ref, uint8_t *dst, int threshold, int w)
87 for (int x = 0; x < w; x++)
88 dst[x] = FFABS(src[x] - ref[x]) <= threshold ? src[x] : ref[x];
91 static void threshold16(const uint8_t *ssrc, const uint8_t *rref, uint8_t *ddst, int threshold, int w)
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;
97 for (int x = 0; x < w; x++)
98 dst[x] = FFABS(src[x] - ref[x]) <= threshold ? src[x] : ref[x];
101 static int config_input(AVFilterLink *inlink)
103 AVFilterContext *ctx = inlink->dst;
104 MaskedThresholdContext *s = ctx->priv;
105 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
108 s->nb_planes = av_pix_fmt_count_planes(inlink->format);
110 if ((ret = av_image_fill_linesizes(s->linesize, inlink->format, inlink->w)) < 0)
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;
120 s->depth = desc->comp[0].depth;
122 if (desc->comp[0].depth == 8)
123 s->maskedthreshold = threshold8;
125 s->maskedthreshold = threshold16;
130 static int threshold_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
132 MaskedThresholdContext *s = ctx->priv;
133 const int threshold = s->threshold;
134 ThreadData *td = arg;
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;
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);
154 for (int y = slice_start; y < slice_end; y++) {
155 s->maskedthreshold(src, ref, dst, threshold, w);
166 static int process_frame(FFFrameSync *fs)
168 AVFilterContext *ctx = fs->parent;
169 MaskedThresholdContext *s = fs->opaque;
170 AVFilterLink *outlink = ctx->outputs[0];
171 AVFrame *out, *src, *ref;
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)
178 if (ctx->is_disabled) {
179 out = av_frame_clone(src);
181 return AVERROR(ENOMEM);
185 out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
187 return AVERROR(ENOMEM);
188 av_frame_copy_props(out, src);
194 ctx->internal->execute(ctx, threshold_slice, &td, NULL, FFMIN(s->planeheight[2],
195 ff_filter_get_nb_threads(ctx)));
197 out->pts = av_rescale_q(s->fs.pts, s->fs.time_base, outlink->time_base);
199 return ff_filter_frame(outlink, out);
202 static int config_output(AVFilterLink *outlink)
204 AVFilterContext *ctx = outlink->src;
205 MaskedThresholdContext *s = ctx->priv;
206 AVFilterLink *source = ctx->inputs[0];
207 AVFilterLink *ref = ctx->inputs[1];
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);
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);
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;
229 if ((ret = ff_framesync_init(&s->fs, ctx, 2)) < 0)
233 in[0].time_base = source->time_base;
234 in[1].time_base = ref->time_base;
236 in[0].before = EXT_STOP;
237 in[0].after = EXT_INFINITY;
239 in[1].before = EXT_STOP;
240 in[1].after = EXT_INFINITY;
242 s->fs.on_event = process_frame;
244 ret = ff_framesync_configure(&s->fs);
245 outlink->time_base = s->fs.time_base;
250 static int activate(AVFilterContext *ctx)
252 MaskedThresholdContext *s = ctx->priv;
253 return ff_framesync_activate(&s->fs);
256 static av_cold void uninit(AVFilterContext *ctx)
258 MaskedThresholdContext *s = ctx->priv;
260 ff_framesync_uninit(&s->fs);
263 static const AVFilterPad maskedthreshold_inputs[] = {
266 .type = AVMEDIA_TYPE_VIDEO,
267 .config_props = config_input,
271 .type = AVMEDIA_TYPE_VIDEO,
276 static const AVFilterPad maskedthreshold_outputs[] = {
279 .type = AVMEDIA_TYPE_VIDEO,
280 .config_props = config_output,
285 AVFILTER_DEFINE_CLASS(maskedthreshold);
287 const 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),
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 .process_command = ff_filter_process_command,