]> git.sesse.net Git - ffmpeg/blob - libavfilter/vf_threshold.c
Merge commit 'e16b20782a597e36a9c7488487c3179375a25b97'
[ffmpeg] / libavfilter / vf_threshold.c
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
36 #define OFFSET(x) offsetof(ThresholdContext, x)
37 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
38
39 static const AVOption threshold_options[] = {
40     { "planes", "set planes to filter", OFFSET(planes), AV_OPT_TYPE_INT,  {.i64=15}, 0, 15, FLAGS},
41     { NULL }
42 };
43
44 AVFILTER_DEFINE_CLASS(threshold);
45
46 static int query_formats(AVFilterContext *ctx)
47 {
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,
65         AV_PIX_FMT_NONE
66     };
67
68     return ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
69 }
70
71 static int process_frame(FFFrameSync *fs)
72 {
73     AVFilterContext *ctx = fs->parent;
74     ThresholdContext *s = fs->opaque;
75     AVFilterLink *outlink = ctx->outputs[0];
76     AVFrame *out, *in, *threshold, *min, *max;
77     int ret;
78
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)
83         return ret;
84
85     if (ctx->is_disabled) {
86         out = av_frame_clone(in);
87         if (!out)
88             return AVERROR(ENOMEM);
89     } else {
90         int p;
91
92         out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
93         if (!out)
94             return AVERROR(ENOMEM);
95         av_frame_copy_props(out, in);
96
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,
102                                     s->height[p]);
103                 continue;
104             }
105             s->threshold(in->data[p], threshold->data[p],
106                          min->data[p], max->data[p],
107                          out->data[p],
108                          in->linesize[p], threshold->linesize[p],
109                          min->linesize[p], max->linesize[p],
110                          out->linesize[p],
111                          s->width[p], s->height[p]);
112         }
113     }
114
115     out->pts = av_rescale_q(s->fs.pts, s->fs.time_base, outlink->time_base);
116
117     return ff_filter_frame(outlink, out);
118 }
119
120 static void threshold8(const uint8_t *in, const uint8_t *threshold,
121                        const uint8_t *min, const uint8_t *max,
122                        uint8_t *out,
123                        ptrdiff_t ilinesize, ptrdiff_t tlinesize,
124                        ptrdiff_t flinesize, ptrdiff_t slinesize,
125                        ptrdiff_t olinesize,
126                        int w, int h)
127 {
128     int x, y;
129
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];
133         }
134
135         in        += ilinesize;
136         threshold += tlinesize;
137         min       += flinesize;
138         max       += slinesize;
139         out       += olinesize;
140     }
141 }
142
143 static void threshold16(const uint8_t *iin, const uint8_t *tthreshold,
144                         const uint8_t *ffirst, const uint8_t *ssecond,
145                         uint8_t *oout,
146                         ptrdiff_t ilinesize, ptrdiff_t tlinesize,
147                         ptrdiff_t flinesize, ptrdiff_t slinesize,
148                         ptrdiff_t olinesize,
149                         int w, int h)
150 {
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;
156     int x, y;
157
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];
161         }
162
163         in        += ilinesize / 2;
164         threshold += tlinesize / 2;
165         min       += flinesize / 2;
166         max       += slinesize / 2;
167         out       += olinesize / 2;
168     }
169 }
170
171 static int config_input(AVFilterLink *inlink)
172 {
173     AVFilterContext *ctx = inlink->dst;
174     ThresholdContext *s = ctx->priv;
175     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
176     int vsub, hsub;
177
178     s->nb_planes = av_pix_fmt_count_planes(inlink->format);
179
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;
187
188     ff_threshold_init(s);
189
190     return 0;
191 }
192
193 void ff_threshold_init(ThresholdContext *s)
194 {
195     if (s->depth == 8) {
196         s->threshold = threshold8;
197         s->bpc = 1;
198     } else {
199         s->threshold = threshold16;
200         s->bpc = 2;
201     }
202
203     if (ARCH_X86)
204         ff_threshold_init_x86(s);
205 }
206
207 static int config_output(AVFilterLink *outlink)
208 {
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];
215     FFFrameSyncIn *in;
216     int ret;
217
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);
223     }
224     if (base->w                       != threshold->w ||
225         base->h                       != threshold->h ||
226         base->w                       != min->w ||
227         base->h                       != min->h ||
228         base->w                       != max->w ||
229         base->h                       != max->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);
240     }
241
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;
247
248     if ((ret = ff_framesync_init(&s->fs, ctx, 4)) < 0)
249         return ret;
250
251     in = s->fs.in;
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;
256     in[0].sync   = 1;
257     in[0].before = EXT_STOP;
258     in[0].after  = EXT_STOP;
259     in[1].sync   = 1;
260     in[1].before = EXT_STOP;
261     in[1].after  = EXT_STOP;
262     in[2].sync   = 1;
263     in[2].before = EXT_STOP;
264     in[2].after  = EXT_STOP;
265     in[3].sync   = 1;
266     in[3].before = EXT_STOP;
267     in[3].after  = EXT_STOP;
268     s->fs.opaque   = s;
269     s->fs.on_event = process_frame;
270
271     return ff_framesync_configure(&s->fs);
272 }
273
274 static int activate(AVFilterContext *ctx)
275 {
276     ThresholdContext *s = ctx->priv;
277     return ff_framesync_activate(&s->fs);
278 }
279
280 static av_cold void uninit(AVFilterContext *ctx)
281 {
282     ThresholdContext *s = ctx->priv;
283
284     ff_framesync_uninit(&s->fs);
285 }
286
287 static const AVFilterPad inputs[] = {
288     {
289         .name         = "default",
290         .type         = AVMEDIA_TYPE_VIDEO,
291         .config_props = config_input,
292     },
293     {
294         .name         = "threshold",
295         .type         = AVMEDIA_TYPE_VIDEO,
296     },
297     {
298         .name         = "min",
299         .type         = AVMEDIA_TYPE_VIDEO,
300     },
301     {
302         .name         = "max",
303         .type         = AVMEDIA_TYPE_VIDEO,
304     },
305     { NULL }
306 };
307
308 static const AVFilterPad outputs[] = {
309     {
310         .name          = "default",
311         .type          = AVMEDIA_TYPE_VIDEO,
312         .config_props  = config_output,
313     },
314     { NULL }
315 };
316
317 AVFilter ff_vf_threshold = {
318     .name          = "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,
322     .uninit        = uninit,
323     .query_formats = query_formats,
324     .activate      = activate,
325     .inputs        = inputs,
326     .outputs       = outputs,
327     .flags         = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL,
328 };