2 * Copyright (c) 2016 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
23 * threshold video filter
26 #include "libavutil/imgutils.h"
27 #include "libavutil/internal.h"
28 #include "libavutil/opt.h"
29 #include "libavutil/pixdesc.h"
31 #include "framesync.h"
34 #include "threshold.h"
36 #define OFFSET(x) offsetof(ThresholdContext, x)
37 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
39 static const AVOption threshold_options[] = {
40 { "planes", "set planes to filter", OFFSET(planes), AV_OPT_TYPE_INT, {.i64=15}, 0, 15, FLAGS},
44 AVFILTER_DEFINE_CLASS(threshold);
46 static int query_formats(AVFilterContext *ctx)
48 static const enum AVPixelFormat pix_fmts[] = {
49 AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV440P,
50 AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P,
51 AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUV420P,
52 AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ420P,
53 AV_PIX_FMT_YUVJ411P, AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P,
54 AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV444P9,
55 AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10,
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_YUVA420P16, AV_PIX_FMT_YUVA422P16, AV_PIX_FMT_YUVA444P16,
60 AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRP9, AV_PIX_FMT_GBRP10,
61 AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRP14, AV_PIX_FMT_GBRP16,
62 AV_PIX_FMT_GBRAP, AV_PIX_FMT_GBRAP10, AV_PIX_FMT_GBRAP12 , AV_PIX_FMT_GBRAP16,
63 AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY9, AV_PIX_FMT_GRAY10,
64 AV_PIX_FMT_GRAY12, AV_PIX_FMT_GRAY14, AV_PIX_FMT_GRAY16,
68 return ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
71 static int process_frame(FFFrameSync *fs)
73 AVFilterContext *ctx = fs->parent;
74 ThresholdContext *s = fs->opaque;
75 AVFilterLink *outlink = ctx->outputs[0];
76 AVFrame *out, *in, *threshold, *min, *max;
79 if ((ret = ff_framesync_get_frame(&s->fs, 0, &in, 0)) < 0 ||
80 (ret = ff_framesync_get_frame(&s->fs, 1, &threshold, 0)) < 0 ||
81 (ret = ff_framesync_get_frame(&s->fs, 2, &min, 0)) < 0 ||
82 (ret = ff_framesync_get_frame(&s->fs, 3, &max, 0)) < 0)
85 if (ctx->is_disabled) {
86 out = av_frame_clone(in);
88 return AVERROR(ENOMEM);
92 out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
94 return AVERROR(ENOMEM);
95 av_frame_copy_props(out, in);
97 for (p = 0; p < s->nb_planes; p++) {
98 if (!(s->planes & (1 << p))) {
99 av_image_copy_plane(out->data[p], out->linesize[p],
100 in->data[p], in->linesize[p],
101 s->width[p] * s->bpc,
105 s->threshold(in->data[p], threshold->data[p],
106 min->data[p], max->data[p],
108 in->linesize[p], threshold->linesize[p],
109 min->linesize[p], max->linesize[p],
111 s->width[p], s->height[p]);
115 out->pts = av_rescale_q(s->fs.pts, s->fs.time_base, outlink->time_base);
117 return ff_filter_frame(outlink, out);
120 static void threshold8(const uint8_t *in, const uint8_t *threshold,
121 const uint8_t *min, const uint8_t *max,
123 ptrdiff_t ilinesize, ptrdiff_t tlinesize,
124 ptrdiff_t flinesize, ptrdiff_t slinesize,
130 for (y = 0; y < h; y++) {
131 for (x = 0; x < w; x++) {
132 out[x] = in[x] < threshold[x] ? min[x] : max[x];
136 threshold += tlinesize;
143 static void threshold16(const uint8_t *iin, const uint8_t *tthreshold,
144 const uint8_t *ffirst, const uint8_t *ssecond,
146 ptrdiff_t ilinesize, ptrdiff_t tlinesize,
147 ptrdiff_t flinesize, ptrdiff_t slinesize,
151 const uint16_t *in = (const uint16_t *)iin;
152 const uint16_t *threshold = (const uint16_t *)tthreshold;
153 const uint16_t *min = (const uint16_t *)ffirst;
154 const uint16_t *max = (const uint16_t *)ssecond;
155 uint16_t *out = (uint16_t *)oout;
158 for (y = 0; y < h; y++) {
159 for (x = 0; x < w; x++) {
160 out[x] = in[x] < threshold[x] ? min[x] : max[x];
164 threshold += tlinesize / 2;
165 min += flinesize / 2;
166 max += slinesize / 2;
167 out += olinesize / 2;
171 static int config_input(AVFilterLink *inlink)
173 AVFilterContext *ctx = inlink->dst;
174 ThresholdContext *s = ctx->priv;
175 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
178 s->nb_planes = av_pix_fmt_count_planes(inlink->format);
180 hsub = desc->log2_chroma_w;
181 vsub = desc->log2_chroma_h;
182 s->height[1] = s->height[2] = AV_CEIL_RSHIFT(inlink->h, vsub);
183 s->height[0] = s->height[3] = inlink->h;
184 s->width[1] = s->width[2] = AV_CEIL_RSHIFT(inlink->w, hsub);
185 s->width[0] = s->width[3] = inlink->w;
186 s->depth = desc->comp[0].depth;
188 ff_threshold_init(s);
193 void ff_threshold_init(ThresholdContext *s)
196 s->threshold = threshold8;
199 s->threshold = threshold16;
204 ff_threshold_init_x86(s);
207 static int config_output(AVFilterLink *outlink)
209 AVFilterContext *ctx = outlink->src;
210 ThresholdContext *s = ctx->priv;
211 AVFilterLink *base = ctx->inputs[0];
212 AVFilterLink *threshold = ctx->inputs[1];
213 AVFilterLink *min = ctx->inputs[2];
214 AVFilterLink *max = ctx->inputs[3];
218 if (base->format != threshold->format ||
219 base->format != min->format ||
220 base->format != max->format) {
221 av_log(ctx, AV_LOG_ERROR, "inputs must be of same pixel format\n");
222 return AVERROR(EINVAL);
224 if (base->w != threshold->w ||
225 base->h != threshold->h ||
230 av_log(ctx, AV_LOG_ERROR, "First input link %s parameters "
231 "(size %dx%d) do not match the corresponding "
232 "second input link %s parameters (%dx%d) "
233 "and/or third input link %s parameters (%dx%d) "
234 "and/or fourth input link %s parameters (%dx%d)\n",
235 ctx->input_pads[0].name, base->w, base->h,
236 ctx->input_pads[1].name, threshold->w, threshold->h,
237 ctx->input_pads[2].name, min->w, min->h,
238 ctx->input_pads[3].name, max->w, max->h);
239 return AVERROR(EINVAL);
242 outlink->w = base->w;
243 outlink->h = base->h;
244 outlink->time_base = base->time_base;
245 outlink->sample_aspect_ratio = base->sample_aspect_ratio;
246 outlink->frame_rate = base->frame_rate;
248 if ((ret = ff_framesync_init(&s->fs, ctx, 4)) < 0)
252 in[0].time_base = base->time_base;
253 in[1].time_base = threshold->time_base;
254 in[2].time_base = min->time_base;
255 in[3].time_base = max->time_base;
257 in[0].before = EXT_STOP;
258 in[0].after = EXT_STOP;
260 in[1].before = EXT_STOP;
261 in[1].after = EXT_STOP;
263 in[2].before = EXT_STOP;
264 in[2].after = EXT_STOP;
266 in[3].before = EXT_STOP;
267 in[3].after = EXT_STOP;
269 s->fs.on_event = process_frame;
271 return ff_framesync_configure(&s->fs);
274 static int activate(AVFilterContext *ctx)
276 ThresholdContext *s = ctx->priv;
277 return ff_framesync_activate(&s->fs);
280 static av_cold void uninit(AVFilterContext *ctx)
282 ThresholdContext *s = ctx->priv;
284 ff_framesync_uninit(&s->fs);
287 static const AVFilterPad inputs[] = {
290 .type = AVMEDIA_TYPE_VIDEO,
291 .config_props = config_input,
295 .type = AVMEDIA_TYPE_VIDEO,
299 .type = AVMEDIA_TYPE_VIDEO,
303 .type = AVMEDIA_TYPE_VIDEO,
308 static const AVFilterPad outputs[] = {
311 .type = AVMEDIA_TYPE_VIDEO,
312 .config_props = config_output,
317 AVFilter ff_vf_threshold = {
319 .description = NULL_IF_CONFIG_SMALL("Threshold first video stream using other video streams."),
320 .priv_size = sizeof(ThresholdContext),
321 .priv_class = &threshold_class,
323 .query_formats = query_formats,
324 .activate = activate,
327 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL,