Line |
Branch |
Exec |
Source |
1 |
|
|
/* |
2 |
|
|
* Copyright (c) 2016 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 |
|
|
/** |
22 |
|
|
* @file |
23 |
|
|
* threshold video filter |
24 |
|
|
*/ |
25 |
|
|
|
26 |
|
|
#include "libavutil/imgutils.h" |
27 |
|
|
#include "libavutil/internal.h" |
28 |
|
|
#include "libavutil/opt.h" |
29 |
|
|
#include "libavutil/pixdesc.h" |
30 |
|
|
#include "avfilter.h" |
31 |
|
|
#include "filters.h" |
32 |
|
|
#include "framesync.h" |
33 |
|
|
#include "video.h" |
34 |
|
|
#include "threshold.h" |
35 |
|
|
#include "vf_threshold_init.h" |
36 |
|
|
|
37 |
|
|
#define OFFSET(x) offsetof(ThresholdContext, x) |
38 |
|
|
#define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_RUNTIME_PARAM |
39 |
|
|
|
40 |
|
|
static const AVOption threshold_options[] = { |
41 |
|
|
{ "planes", "set planes to filter", OFFSET(planes), AV_OPT_TYPE_INT, {.i64=15}, 0, 15, FLAGS}, |
42 |
|
|
{ NULL } |
43 |
|
|
}; |
44 |
|
|
|
45 |
|
|
AVFILTER_DEFINE_CLASS(threshold); |
46 |
|
|
|
47 |
|
|
static const enum AVPixelFormat pix_fmts[] = { |
48 |
|
|
AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV440P, |
49 |
|
|
AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P, |
50 |
|
|
AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUV420P, |
51 |
|
|
AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ420P, |
52 |
|
|
AV_PIX_FMT_YUVJ411P, AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P, |
53 |
|
|
AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV444P9, |
54 |
|
|
AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV440P10, AV_PIX_FMT_YUV444P10, |
55 |
|
|
AV_PIX_FMT_YUV420P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV440P12, AV_PIX_FMT_YUV444P12, |
56 |
|
|
AV_PIX_FMT_YUV420P16, AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV444P16, |
57 |
|
|
AV_PIX_FMT_YUVA420P9, AV_PIX_FMT_YUVA422P9, AV_PIX_FMT_YUVA444P9, |
58 |
|
|
AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA444P10, |
59 |
|
|
AV_PIX_FMT_YUVA422P12, AV_PIX_FMT_YUVA444P12, |
60 |
|
|
AV_PIX_FMT_YUVA420P16, AV_PIX_FMT_YUVA422P16, AV_PIX_FMT_YUVA444P16, |
61 |
|
|
AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRP9, AV_PIX_FMT_GBRP10, |
62 |
|
|
AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRP14, AV_PIX_FMT_GBRP16, |
63 |
|
|
AV_PIX_FMT_GBRAP, AV_PIX_FMT_GBRAP10, AV_PIX_FMT_GBRAP12 , AV_PIX_FMT_GBRAP16, |
64 |
|
|
AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY9, AV_PIX_FMT_GRAY10, |
65 |
|
|
AV_PIX_FMT_GRAY12, AV_PIX_FMT_GRAY14, AV_PIX_FMT_GRAY16, |
66 |
|
|
AV_PIX_FMT_NONE |
67 |
|
|
}; |
68 |
|
|
|
69 |
|
|
typedef struct ThreadData { |
70 |
|
|
AVFrame *in; |
71 |
|
|
AVFrame *threshold; |
72 |
|
|
AVFrame *min; |
73 |
|
|
AVFrame *max; |
74 |
|
|
AVFrame *out; |
75 |
|
|
} ThreadData; |
76 |
|
|
|
77 |
|
✗ |
static int filter_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs) |
78 |
|
|
{ |
79 |
|
✗ |
ThresholdContext *s = ctx->priv; |
80 |
|
✗ |
ThreadData *td = arg; |
81 |
|
✗ |
AVFrame *min = td->min; |
82 |
|
✗ |
AVFrame *max = td->max; |
83 |
|
✗ |
AVFrame *threshold = td->threshold; |
84 |
|
✗ |
AVFrame *in = td->in; |
85 |
|
✗ |
AVFrame *out = td->out; |
86 |
|
|
|
87 |
|
✗ |
for (int p = 0; p < s->nb_planes; p++) { |
88 |
|
✗ |
const int h = s->height[p]; |
89 |
|
✗ |
const int slice_start = (h * jobnr) / nb_jobs; |
90 |
|
✗ |
const int slice_end = (h * (jobnr+1)) / nb_jobs; |
91 |
|
|
|
92 |
|
✗ |
if (!(s->planes & (1 << p))) { |
93 |
|
✗ |
av_image_copy_plane(out->data[p] + slice_start * out->linesize[p], |
94 |
|
|
out->linesize[p], |
95 |
|
✗ |
in->data[p] + slice_start * in->linesize[p], |
96 |
|
|
in->linesize[p], |
97 |
|
✗ |
s->width[p] * s->bpc, |
98 |
|
|
slice_end - slice_start); |
99 |
|
✗ |
continue; |
100 |
|
|
} |
101 |
|
✗ |
s->threshold(in->data[p] + slice_start * in->linesize[p], |
102 |
|
✗ |
threshold->data[p] + slice_start * threshold->linesize[p], |
103 |
|
✗ |
min->data[p] + slice_start * min->linesize[p], |
104 |
|
✗ |
max->data[p] + slice_start * max->linesize[p], |
105 |
|
✗ |
out->data[p] + slice_start * out->linesize[p], |
106 |
|
✗ |
in->linesize[p], threshold->linesize[p], |
107 |
|
✗ |
min->linesize[p], max->linesize[p], |
108 |
|
✗ |
out->linesize[p], |
109 |
|
|
s->width[p], slice_end - slice_start); |
110 |
|
|
} |
111 |
|
|
|
112 |
|
✗ |
return 0; |
113 |
|
|
} |
114 |
|
|
|
115 |
|
✗ |
static int process_frame(FFFrameSync *fs) |
116 |
|
|
{ |
117 |
|
✗ |
AVFilterContext *ctx = fs->parent; |
118 |
|
✗ |
ThresholdContext *s = fs->opaque; |
119 |
|
✗ |
AVFilterLink *outlink = ctx->outputs[0]; |
120 |
|
|
AVFrame *out, *in, *threshold, *min, *max; |
121 |
|
|
ThreadData td; |
122 |
|
|
int ret; |
123 |
|
|
|
124 |
|
✗ |
if ((ret = ff_framesync_get_frame(&s->fs, 0, &in, 0)) < 0 || |
125 |
|
✗ |
(ret = ff_framesync_get_frame(&s->fs, 1, &threshold, 0)) < 0 || |
126 |
|
✗ |
(ret = ff_framesync_get_frame(&s->fs, 2, &min, 0)) < 0 || |
127 |
|
✗ |
(ret = ff_framesync_get_frame(&s->fs, 3, &max, 0)) < 0) |
128 |
|
✗ |
return ret; |
129 |
|
|
|
130 |
|
✗ |
if (ctx->is_disabled) { |
131 |
|
✗ |
out = av_frame_clone(in); |
132 |
|
✗ |
if (!out) |
133 |
|
✗ |
return AVERROR(ENOMEM); |
134 |
|
|
} else { |
135 |
|
✗ |
out = ff_get_video_buffer(outlink, outlink->w, outlink->h); |
136 |
|
✗ |
if (!out) |
137 |
|
✗ |
return AVERROR(ENOMEM); |
138 |
|
✗ |
av_frame_copy_props(out, in); |
139 |
|
|
|
140 |
|
✗ |
td.out = out; |
141 |
|
✗ |
td.in = in; |
142 |
|
✗ |
td.threshold = threshold; |
143 |
|
✗ |
td.min = min; |
144 |
|
✗ |
td.max = max; |
145 |
|
✗ |
ff_filter_execute(ctx, filter_slice, &td, NULL, |
146 |
|
✗ |
FFMIN(s->height[2], ff_filter_get_nb_threads(ctx))); |
147 |
|
|
} |
148 |
|
|
|
149 |
|
✗ |
out->pts = av_rescale_q(s->fs.pts, s->fs.time_base, outlink->time_base); |
150 |
|
|
|
151 |
|
✗ |
return ff_filter_frame(outlink, out); |
152 |
|
|
} |
153 |
|
|
|
154 |
|
✗ |
static int config_input(AVFilterLink *inlink) |
155 |
|
|
{ |
156 |
|
✗ |
AVFilterContext *ctx = inlink->dst; |
157 |
|
✗ |
ThresholdContext *s = ctx->priv; |
158 |
|
✗ |
const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format); |
159 |
|
|
int vsub, hsub; |
160 |
|
|
|
161 |
|
✗ |
s->nb_planes = av_pix_fmt_count_planes(inlink->format); |
162 |
|
|
|
163 |
|
✗ |
hsub = desc->log2_chroma_w; |
164 |
|
✗ |
vsub = desc->log2_chroma_h; |
165 |
|
✗ |
s->height[1] = s->height[2] = AV_CEIL_RSHIFT(inlink->h, vsub); |
166 |
|
✗ |
s->height[0] = s->height[3] = inlink->h; |
167 |
|
✗ |
s->width[1] = s->width[2] = AV_CEIL_RSHIFT(inlink->w, hsub); |
168 |
|
✗ |
s->width[0] = s->width[3] = inlink->w; |
169 |
|
✗ |
s->depth = desc->comp[0].depth; |
170 |
|
|
|
171 |
|
✗ |
ff_threshold_init(s); |
172 |
|
|
|
173 |
|
✗ |
return 0; |
174 |
|
|
} |
175 |
|
|
|
176 |
|
✗ |
static int config_output(AVFilterLink *outlink) |
177 |
|
|
{ |
178 |
|
✗ |
AVFilterContext *ctx = outlink->src; |
179 |
|
✗ |
ThresholdContext *s = ctx->priv; |
180 |
|
✗ |
AVFilterLink *base = ctx->inputs[0]; |
181 |
|
✗ |
AVFilterLink *threshold = ctx->inputs[1]; |
182 |
|
✗ |
AVFilterLink *min = ctx->inputs[2]; |
183 |
|
✗ |
AVFilterLink *max = ctx->inputs[3]; |
184 |
|
✗ |
FilterLink *il = ff_filter_link(base); |
185 |
|
✗ |
FilterLink *ol = ff_filter_link(outlink); |
186 |
|
|
FFFrameSyncIn *in; |
187 |
|
|
int ret; |
188 |
|
|
|
189 |
|
✗ |
if (base->w != threshold->w || |
190 |
|
✗ |
base->h != threshold->h || |
191 |
|
✗ |
base->w != min->w || |
192 |
|
✗ |
base->h != min->h || |
193 |
|
✗ |
base->w != max->w || |
194 |
|
✗ |
base->h != max->h) { |
195 |
|
✗ |
av_log(ctx, AV_LOG_ERROR, "First input link %s parameters " |
196 |
|
|
"(size %dx%d) do not match the corresponding " |
197 |
|
|
"second input link %s parameters (%dx%d) " |
198 |
|
|
"and/or third input link %s parameters (%dx%d) " |
199 |
|
|
"and/or fourth input link %s parameters (%dx%d)\n", |
200 |
|
✗ |
ctx->input_pads[0].name, base->w, base->h, |
201 |
|
✗ |
ctx->input_pads[1].name, threshold->w, threshold->h, |
202 |
|
✗ |
ctx->input_pads[2].name, min->w, min->h, |
203 |
|
✗ |
ctx->input_pads[3].name, max->w, max->h); |
204 |
|
✗ |
return AVERROR(EINVAL); |
205 |
|
|
} |
206 |
|
|
|
207 |
|
✗ |
outlink->w = base->w; |
208 |
|
✗ |
outlink->h = base->h; |
209 |
|
✗ |
outlink->sample_aspect_ratio = base->sample_aspect_ratio; |
210 |
|
✗ |
ol->frame_rate = il->frame_rate; |
211 |
|
|
|
212 |
|
✗ |
if ((ret = ff_framesync_init(&s->fs, ctx, 4)) < 0) |
213 |
|
✗ |
return ret; |
214 |
|
|
|
215 |
|
✗ |
in = s->fs.in; |
216 |
|
✗ |
in[0].time_base = base->time_base; |
217 |
|
✗ |
in[1].time_base = threshold->time_base; |
218 |
|
✗ |
in[2].time_base = min->time_base; |
219 |
|
✗ |
in[3].time_base = max->time_base; |
220 |
|
✗ |
in[0].sync = 1; |
221 |
|
✗ |
in[0].before = EXT_STOP; |
222 |
|
✗ |
in[0].after = EXT_STOP; |
223 |
|
✗ |
in[1].sync = 1; |
224 |
|
✗ |
in[1].before = EXT_STOP; |
225 |
|
✗ |
in[1].after = EXT_STOP; |
226 |
|
✗ |
in[2].sync = 1; |
227 |
|
✗ |
in[2].before = EXT_STOP; |
228 |
|
✗ |
in[2].after = EXT_STOP; |
229 |
|
✗ |
in[3].sync = 1; |
230 |
|
✗ |
in[3].before = EXT_STOP; |
231 |
|
✗ |
in[3].after = EXT_STOP; |
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 |
|
|
|
241 |
|
✗ |
static int activate(AVFilterContext *ctx) |
242 |
|
|
{ |
243 |
|
✗ |
ThresholdContext *s = ctx->priv; |
244 |
|
✗ |
return ff_framesync_activate(&s->fs); |
245 |
|
|
} |
246 |
|
|
|
247 |
|
✗ |
static av_cold void uninit(AVFilterContext *ctx) |
248 |
|
|
{ |
249 |
|
✗ |
ThresholdContext *s = ctx->priv; |
250 |
|
|
|
251 |
|
✗ |
ff_framesync_uninit(&s->fs); |
252 |
|
✗ |
} |
253 |
|
|
|
254 |
|
|
static const AVFilterPad inputs[] = { |
255 |
|
|
{ |
256 |
|
|
.name = "default", |
257 |
|
|
.type = AVMEDIA_TYPE_VIDEO, |
258 |
|
|
.config_props = config_input, |
259 |
|
|
}, |
260 |
|
|
{ |
261 |
|
|
.name = "threshold", |
262 |
|
|
.type = AVMEDIA_TYPE_VIDEO, |
263 |
|
|
}, |
264 |
|
|
{ |
265 |
|
|
.name = "min", |
266 |
|
|
.type = AVMEDIA_TYPE_VIDEO, |
267 |
|
|
}, |
268 |
|
|
{ |
269 |
|
|
.name = "max", |
270 |
|
|
.type = AVMEDIA_TYPE_VIDEO, |
271 |
|
|
}, |
272 |
|
|
}; |
273 |
|
|
|
274 |
|
|
static const AVFilterPad outputs[] = { |
275 |
|
|
{ |
276 |
|
|
.name = "default", |
277 |
|
|
.type = AVMEDIA_TYPE_VIDEO, |
278 |
|
|
.config_props = config_output, |
279 |
|
|
}, |
280 |
|
|
}; |
281 |
|
|
|
282 |
|
|
const AVFilter ff_vf_threshold = { |
283 |
|
|
.name = "threshold", |
284 |
|
|
.description = NULL_IF_CONFIG_SMALL("Threshold first video stream using other video streams."), |
285 |
|
|
.priv_size = sizeof(ThresholdContext), |
286 |
|
|
.priv_class = &threshold_class, |
287 |
|
|
.uninit = uninit, |
288 |
|
|
.activate = activate, |
289 |
|
|
FILTER_INPUTS(inputs), |
290 |
|
|
FILTER_OUTPUTS(outputs), |
291 |
|
|
FILTER_PIXFMTS_ARRAY(pix_fmts), |
292 |
|
|
.flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL | AVFILTER_FLAG_SLICE_THREADS, |
293 |
|
|
.process_command = ff_filter_process_command, |
294 |
|
|
}; |
295 |
|
|
|