• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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|AV_OPT_FLAG_RUNTIME_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 const enum AVPixelFormat pix_fmts[] = {
59     AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV440P,
60     AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P,
61     AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUV420P,
62     AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ420P,
63     AV_PIX_FMT_YUVJ411P, AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P,
64     AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV444P9,
65     AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10,
66     AV_PIX_FMT_YUV420P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV444P12, AV_PIX_FMT_YUV440P12,
67     AV_PIX_FMT_YUV420P14, AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV444P14,
68     AV_PIX_FMT_YUV420P16, AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV444P16,
69     AV_PIX_FMT_YUVA420P9, AV_PIX_FMT_YUVA422P9, AV_PIX_FMT_YUVA444P9,
70     AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA444P10,
71     AV_PIX_FMT_YUVA422P12, AV_PIX_FMT_YUVA444P12,
72     AV_PIX_FMT_YUVA420P16, AV_PIX_FMT_YUVA422P16, AV_PIX_FMT_YUVA444P16,
73     AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRP9, AV_PIX_FMT_GBRP10,
74     AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRP14, AV_PIX_FMT_GBRP16,
75     AV_PIX_FMT_GBRAP, AV_PIX_FMT_GBRAP10, AV_PIX_FMT_GBRAP12, AV_PIX_FMT_GBRAP16,
76     AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY9, AV_PIX_FMT_GRAY10, AV_PIX_FMT_GRAY12, AV_PIX_FMT_GRAY14, AV_PIX_FMT_GRAY16,
77     AV_PIX_FMT_NONE
78 };
79 
threshold8(const uint8_t * src,const uint8_t * ref,uint8_t * dst,int threshold,int w)80 static void threshold8(const uint8_t *src, const uint8_t *ref, uint8_t *dst, int threshold, int w)
81 {
82     for (int x = 0; x < w; x++)
83         dst[x] = FFABS(src[x] - ref[x]) <= threshold ? src[x] : ref[x];
84 }
85 
threshold16(const uint8_t * ssrc,const uint8_t * rref,uint8_t * ddst,int threshold,int w)86 static void threshold16(const uint8_t *ssrc, const uint8_t *rref, uint8_t *ddst, int threshold, int w)
87 {
88     const uint16_t *src = (const uint16_t *)ssrc;
89     const uint16_t *ref = (const uint16_t *)rref;
90     uint16_t *dst = (uint16_t *)ddst;
91 
92     for (int x = 0; x < w; x++)
93         dst[x] = FFABS(src[x] - ref[x]) <= threshold ? src[x] : ref[x];
94 }
95 
config_input(AVFilterLink * inlink)96 static int config_input(AVFilterLink *inlink)
97 {
98     AVFilterContext *ctx = inlink->dst;
99     MaskedThresholdContext *s = ctx->priv;
100     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
101     int vsub, hsub, ret;
102 
103     s->nb_planes = av_pix_fmt_count_planes(inlink->format);
104 
105     if ((ret = av_image_fill_linesizes(s->linesize, inlink->format, inlink->w)) < 0)
106         return ret;
107 
108     hsub = desc->log2_chroma_w;
109     vsub = desc->log2_chroma_h;
110     s->planeheight[1] = s->planeheight[2] = AV_CEIL_RSHIFT(inlink->h, vsub);
111     s->planeheight[0] = s->planeheight[3] = inlink->h;
112     s->planewidth[1]  = s->planewidth[2]  = AV_CEIL_RSHIFT(inlink->w, hsub);
113     s->planewidth[0]  = s->planewidth[3]  = inlink->w;
114 
115     s->depth = desc->comp[0].depth;
116 
117     if (desc->comp[0].depth == 8)
118         s->maskedthreshold = threshold8;
119     else
120         s->maskedthreshold = threshold16;
121 
122     return 0;
123 }
124 
threshold_slice(AVFilterContext * ctx,void * arg,int jobnr,int nb_jobs)125 static int threshold_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
126 {
127     MaskedThresholdContext *s = ctx->priv;
128     const int threshold = s->threshold;
129     ThreadData *td = arg;
130 
131     for (int p = 0; p < s->nb_planes; p++) {
132         const ptrdiff_t src_linesize = td->src->linesize[p];
133         const ptrdiff_t ref_linesize = td->ref->linesize[p];
134         const ptrdiff_t dst_linesize = td->dst->linesize[p];
135         const int w = s->planewidth[p];
136         const int h = s->planeheight[p];
137         const int slice_start = (h * jobnr) / nb_jobs;
138         const int slice_end = (h * (jobnr+1)) / nb_jobs;
139         const uint8_t *src = td->src->data[p] + slice_start * src_linesize;
140         const uint8_t *ref = td->ref->data[p] + slice_start * ref_linesize;
141         uint8_t *dst = td->dst->data[p] + slice_start * dst_linesize;
142 
143         if (!((1 << p) & s->planes)) {
144             av_image_copy_plane(dst, dst_linesize, ref, ref_linesize,
145                                 s->linesize[p], slice_end - slice_start);
146             continue;
147         }
148 
149         for (int y = slice_start; y < slice_end; y++) {
150             s->maskedthreshold(src, ref, dst, threshold, w);
151 
152             dst += dst_linesize;
153             src += src_linesize;
154             ref += ref_linesize;
155         }
156     }
157 
158     return 0;
159 }
160 
process_frame(FFFrameSync * fs)161 static int process_frame(FFFrameSync *fs)
162 {
163     AVFilterContext *ctx = fs->parent;
164     MaskedThresholdContext *s = fs->opaque;
165     AVFilterLink *outlink = ctx->outputs[0];
166     AVFrame *out, *src, *ref;
167     int ret;
168 
169     if ((ret = ff_framesync_get_frame(&s->fs, 0, &src, 0)) < 0 ||
170         (ret = ff_framesync_get_frame(&s->fs, 1, &ref, 0)) < 0)
171         return ret;
172 
173     if (ctx->is_disabled) {
174         out = av_frame_clone(src);
175         if (!out)
176             return AVERROR(ENOMEM);
177     } else {
178         ThreadData td;
179 
180         out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
181         if (!out)
182             return AVERROR(ENOMEM);
183         av_frame_copy_props(out, src);
184 
185         td.src = src;
186         td.ref = ref;
187         td.dst = out;
188 
189         ff_filter_execute(ctx, threshold_slice, &td, NULL,
190                           FFMIN(s->planeheight[2], ff_filter_get_nb_threads(ctx)));
191     }
192     out->pts = av_rescale_q(s->fs.pts, s->fs.time_base, outlink->time_base);
193 
194     return ff_filter_frame(outlink, out);
195 }
196 
config_output(AVFilterLink * outlink)197 static int config_output(AVFilterLink *outlink)
198 {
199     AVFilterContext *ctx = outlink->src;
200     MaskedThresholdContext *s = ctx->priv;
201     AVFilterLink *source = ctx->inputs[0];
202     AVFilterLink *ref = ctx->inputs[1];
203     FFFrameSyncIn *in;
204     int ret;
205 
206     if (source->w != ref->w || source->h != ref->h) {
207         av_log(ctx, AV_LOG_ERROR, "First input link %s parameters "
208                "(size %dx%d) do not match the corresponding "
209                "second input link %s parameters (%dx%d)\n",
210                ctx->input_pads[0].name, source->w, source->h,
211                ctx->input_pads[1].name, ref->w, ref->h);
212         return AVERROR(EINVAL);
213     }
214 
215     outlink->w = source->w;
216     outlink->h = source->h;
217     outlink->sample_aspect_ratio = source->sample_aspect_ratio;
218     outlink->frame_rate = source->frame_rate;
219 
220     if ((ret = ff_framesync_init(&s->fs, ctx, 2)) < 0)
221         return ret;
222 
223     in = s->fs.in;
224     in[0].time_base = source->time_base;
225     in[1].time_base = ref->time_base;
226     in[0].sync   = 1;
227     in[0].before = EXT_STOP;
228     in[0].after  = EXT_INFINITY;
229     in[1].sync   = 1;
230     in[1].before = EXT_STOP;
231     in[1].after  = EXT_INFINITY;
232     s->fs.opaque   = s;
233     s->fs.on_event = process_frame;
234 
235     ret = ff_framesync_configure(&s->fs);
236     outlink->time_base = s->fs.time_base;
237 
238     return ret;
239 }
240 
activate(AVFilterContext * ctx)241 static int activate(AVFilterContext *ctx)
242 {
243     MaskedThresholdContext *s = ctx->priv;
244     return ff_framesync_activate(&s->fs);
245 }
246 
uninit(AVFilterContext * ctx)247 static av_cold void uninit(AVFilterContext *ctx)
248 {
249     MaskedThresholdContext *s = ctx->priv;
250 
251     ff_framesync_uninit(&s->fs);
252 }
253 
254 static const AVFilterPad maskedthreshold_inputs[] = {
255     {
256         .name         = "source",
257         .type         = AVMEDIA_TYPE_VIDEO,
258         .config_props = config_input,
259     },
260     {
261         .name         = "reference",
262         .type         = AVMEDIA_TYPE_VIDEO,
263     },
264 };
265 
266 static const AVFilterPad maskedthreshold_outputs[] = {
267     {
268         .name          = "default",
269         .type          = AVMEDIA_TYPE_VIDEO,
270         .config_props  = config_output,
271     },
272 };
273 
274 AVFILTER_DEFINE_CLASS(maskedthreshold);
275 
276 const AVFilter ff_vf_maskedthreshold = {
277     .name          = "maskedthreshold",
278     .description   = NULL_IF_CONFIG_SMALL("Pick pixels comparing absolute difference of two streams with threshold."),
279     .priv_class    = &maskedthreshold_class,
280     .priv_size     = sizeof(MaskedThresholdContext),
281     .uninit        = uninit,
282     .activate      = activate,
283     FILTER_INPUTS(maskedthreshold_inputs),
284     FILTER_OUTPUTS(maskedthreshold_outputs),
285     FILTER_PIXFMTS_ARRAY(pix_fmts),
286     .flags         = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL | AVFILTER_FLAG_SLICE_THREADS,
287     .process_command = ff_filter_process_command,
288 };
289