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