• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "framesync.h"
32 #include "internal.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 
filter_slice(AVFilterContext * ctx,void * arg,int jobnr,int nb_jobs)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 
process_frame(FFFrameSync * fs)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 
config_input(AVFilterLink * inlink)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 
config_output(AVFilterLink * outlink)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     FFFrameSyncIn *in;
185     int ret;
186 
187     if (base->w                       != threshold->w ||
188         base->h                       != threshold->h ||
189         base->w                       != min->w ||
190         base->h                       != min->h ||
191         base->w                       != max->w ||
192         base->h                       != max->h) {
193         av_log(ctx, AV_LOG_ERROR, "First input link %s parameters "
194                "(size %dx%d) do not match the corresponding "
195                "second input link %s parameters (%dx%d) "
196                "and/or third input link %s parameters (%dx%d) "
197                "and/or fourth input link %s parameters (%dx%d)\n",
198                ctx->input_pads[0].name, base->w, base->h,
199                ctx->input_pads[1].name, threshold->w, threshold->h,
200                ctx->input_pads[2].name, min->w, min->h,
201                ctx->input_pads[3].name, max->w, max->h);
202         return AVERROR(EINVAL);
203     }
204 
205     outlink->w = base->w;
206     outlink->h = base->h;
207     outlink->sample_aspect_ratio = base->sample_aspect_ratio;
208     outlink->frame_rate = base->frame_rate;
209 
210     if ((ret = ff_framesync_init(&s->fs, ctx, 4)) < 0)
211         return ret;
212 
213     in = s->fs.in;
214     in[0].time_base = base->time_base;
215     in[1].time_base = threshold->time_base;
216     in[2].time_base = min->time_base;
217     in[3].time_base = max->time_base;
218     in[0].sync   = 1;
219     in[0].before = EXT_STOP;
220     in[0].after  = EXT_STOP;
221     in[1].sync   = 1;
222     in[1].before = EXT_STOP;
223     in[1].after  = EXT_STOP;
224     in[2].sync   = 1;
225     in[2].before = EXT_STOP;
226     in[2].after  = EXT_STOP;
227     in[3].sync   = 1;
228     in[3].before = EXT_STOP;
229     in[3].after  = EXT_STOP;
230     s->fs.opaque   = s;
231     s->fs.on_event = process_frame;
232 
233     ret = ff_framesync_configure(&s->fs);
234     outlink->time_base = s->fs.time_base;
235 
236     return ret;
237 }
238 
activate(AVFilterContext * ctx)239 static int activate(AVFilterContext *ctx)
240 {
241     ThresholdContext *s = ctx->priv;
242     return ff_framesync_activate(&s->fs);
243 }
244 
uninit(AVFilterContext * ctx)245 static av_cold void uninit(AVFilterContext *ctx)
246 {
247     ThresholdContext *s = ctx->priv;
248 
249     ff_framesync_uninit(&s->fs);
250 }
251 
252 static const AVFilterPad inputs[] = {
253     {
254         .name         = "default",
255         .type         = AVMEDIA_TYPE_VIDEO,
256         .config_props = config_input,
257     },
258     {
259         .name         = "threshold",
260         .type         = AVMEDIA_TYPE_VIDEO,
261     },
262     {
263         .name         = "min",
264         .type         = AVMEDIA_TYPE_VIDEO,
265     },
266     {
267         .name         = "max",
268         .type         = AVMEDIA_TYPE_VIDEO,
269     },
270 };
271 
272 static const AVFilterPad outputs[] = {
273     {
274         .name          = "default",
275         .type          = AVMEDIA_TYPE_VIDEO,
276         .config_props  = config_output,
277     },
278 };
279 
280 const AVFilter ff_vf_threshold = {
281     .name          = "threshold",
282     .description   = NULL_IF_CONFIG_SMALL("Threshold first video stream using other video streams."),
283     .priv_size     = sizeof(ThresholdContext),
284     .priv_class    = &threshold_class,
285     .uninit        = uninit,
286     .activate      = activate,
287     FILTER_INPUTS(inputs),
288     FILTER_OUTPUTS(outputs),
289     FILTER_PIXFMTS_ARRAY(pix_fmts),
290     .flags         = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL | AVFILTER_FLAG_SLICE_THREADS,
291     .process_command = ff_filter_process_command,
292 };
293