2 * Copyright (c) 2018 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
21 #include "libavutil/imgutils.h"
22 #include "libavutil/pixdesc.h"
23 #include "libavutil/opt.h"
29 typedef struct MaskFunContext {
38 int width[4], height[4];
45 int (*getsum)(AVFilterContext *ctx, AVFrame *out);
46 int (*maskfun)(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs);
49 #define OFFSET(x) offsetof(MaskFunContext, x)
50 #define VFT AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
52 static const AVOption maskfun_options[] = {
53 { "low", "set low threshold", OFFSET(low), AV_OPT_TYPE_INT, {.i64=10}, 0, UINT16_MAX, VFT },
54 { "high", "set high threshold", OFFSET(high), AV_OPT_TYPE_INT, {.i64=10}, 0, UINT16_MAX, VFT },
55 { "planes", "set planes", OFFSET(planes), AV_OPT_TYPE_INT, {.i64=0xF}, 0, 0xF, VFT },
56 { "fill", "set fill value", OFFSET(fill), AV_OPT_TYPE_INT, {.i64=0}, 0, UINT16_MAX, VFT },
57 { "sum", "set sum value", OFFSET(sum), AV_OPT_TYPE_INT, {.i64=10}, 0, UINT16_MAX, VFT },
61 AVFILTER_DEFINE_CLASS(maskfun);
63 static int query_formats(AVFilterContext *ctx)
65 static const enum AVPixelFormat pix_fmts[] = {
66 AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV440P,
67 AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P,
68 AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUV420P,
69 AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ420P,
70 AV_PIX_FMT_YUVJ411P, AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P,
71 AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV444P9,
72 AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10,
73 AV_PIX_FMT_YUV420P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV444P12, AV_PIX_FMT_YUV440P12,
74 AV_PIX_FMT_YUV420P14, AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV444P14,
75 AV_PIX_FMT_YUV420P16, AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV444P16,
76 AV_PIX_FMT_YUVA420P9, AV_PIX_FMT_YUVA422P9, AV_PIX_FMT_YUVA444P9,
77 AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA444P10,
78 AV_PIX_FMT_YUVA420P16, AV_PIX_FMT_YUVA422P16, AV_PIX_FMT_YUVA444P16,
79 AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRP9, AV_PIX_FMT_GBRP10,
80 AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRP14, AV_PIX_FMT_GBRP16,
81 AV_PIX_FMT_GBRAP, AV_PIX_FMT_GBRAP10, AV_PIX_FMT_GBRAP12, AV_PIX_FMT_GBRAP16,
82 AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY9, AV_PIX_FMT_GRAY10, AV_PIX_FMT_GRAY12, AV_PIX_FMT_GRAY14, AV_PIX_FMT_GRAY16,
86 return ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
89 static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
91 AVFilterContext *ctx = inlink->dst;
92 MaskFunContext *s = ctx->priv;
93 AVFilterLink *outlink = ctx->outputs[0];
95 if (s->getsum(ctx, frame)) {
96 AVFrame *out = av_frame_clone(s->empty);
99 av_frame_free(&frame);
100 return AVERROR(ENOMEM);
102 out->pts = frame->pts;
103 av_frame_free(&frame);
105 return ff_filter_frame(outlink, out);
108 ctx->internal->execute(ctx, s->maskfun, frame, NULL,
109 FFMIN(s->height[1], ff_filter_get_nb_threads(ctx)));
111 return ff_filter_frame(outlink, frame);
114 #define GETSUM(name, type, div) \
115 static int getsum##name(AVFilterContext *ctx, AVFrame *out) \
117 MaskFunContext *s = ctx->priv; \
121 for (p = 0; p < s->nb_planes; p++) { \
122 const int linesize = out->linesize[p] / div; \
123 const int w = s->width[p]; \
124 const int h = s->height[p]; \
125 type *dst = (type *)out->data[p]; \
127 if (!((1 << p) & s->planes)) \
130 for (int y = 0; y < h; y++) { \
131 for (int x = 0; x < w; x++) \
133 if (sum >= s->max_sum) \
142 GETSUM(8, uint8_t, 1)
143 GETSUM(16, uint16_t, 2)
145 #define MASKFUN(name, type, div) \
146 static int maskfun##name(AVFilterContext *ctx, void *arg, \
147 int jobnr, int nb_jobs) \
149 MaskFunContext *s = ctx->priv; \
150 AVFrame *out = arg; \
151 const int low = s->low; \
152 const int high = s->high; \
153 const int max = s->max; \
156 for (p = 0; p < s->nb_planes; p++) { \
157 const int linesize = out->linesize[p] / div; \
158 const int w = s->width[p]; \
159 const int h = s->height[p]; \
160 const int slice_start = (h * jobnr) / nb_jobs; \
161 const int slice_end = (h * (jobnr+1)) / nb_jobs; \
162 type *dst = (type *)out->data[p] + slice_start * linesize; \
164 if (!((1 << p) & s->planes)) \
167 for (int y = slice_start; y < slice_end; y++) { \
168 for (int x = 0; x < w; x++) { \
171 else if (dst[x] > high) \
182 MASKFUN(8, uint8_t, 1)
183 MASKFUN(16, uint16_t, 2)
185 static void fill_frame(AVFilterContext *ctx)
187 MaskFunContext *s = ctx->priv;
189 s->fill = FFMIN(s->fill, s->max);
191 for (int p = 0; p < s->nb_planes; p++) {
192 uint8_t *dst = s->empty->data[p];
194 for (int y = 0; y < s->height[p]; y++) {
195 memset(dst, s->fill, s->width[p]);
196 dst += s->empty->linesize[p];
200 for (int p = 0; p < s->nb_planes; p++) {
201 uint16_t *dst = (uint16_t *)s->empty->data[p];
203 for (int y = 0; y < s->height[p]; y++) {
204 for (int x = 0; x < s->width[p]; x++)
206 dst += s->empty->linesize[p] / 2;
212 static void set_max_sum(AVFilterContext *ctx)
214 MaskFunContext *s = ctx->priv;
217 for (int p = 0; p < s->nb_planes; p++) {
218 if (!((1 << p) & s->planes))
220 s->max_sum += (uint64_t)s->sum * s->width[p] * s->height[p];
224 static int config_input(AVFilterLink *inlink)
226 AVFilterContext *ctx = inlink->dst;
227 MaskFunContext *s = ctx->priv;
228 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
231 s->nb_planes = av_pix_fmt_count_planes(inlink->format);
233 if ((ret = av_image_fill_linesizes(s->linesize, inlink->format, inlink->w)) < 0)
236 hsub = desc->log2_chroma_w;
237 vsub = desc->log2_chroma_h;
238 s->height[1] = s->height[2] = AV_CEIL_RSHIFT(inlink->h, vsub);
239 s->height[0] = s->height[3] = inlink->h;
240 s->width[1] = s->width[2] = AV_CEIL_RSHIFT(inlink->w, hsub);
241 s->width[0] = s->width[3] = inlink->w;
243 s->depth = desc->comp[0].depth;
244 s->max = (1 << s->depth) - 1;
247 s->maskfun = maskfun8;
250 s->maskfun = maskfun16;
251 s->getsum = getsum16;
254 s->empty = ff_get_video_buffer(inlink, inlink->w, inlink->h);
256 return AVERROR(ENOMEM);
265 static int process_command(AVFilterContext *ctx, const char *cmd, const char *args,
266 char *res, int res_len, int flags)
268 MaskFunContext *s = ctx->priv;
273 ret = ff_filter_process_command(ctx, cmd, args, res, res_len, flags);
286 static av_cold void uninit(AVFilterContext *ctx)
288 MaskFunContext *s = ctx->priv;
290 av_frame_free(&s->empty);
293 static const AVFilterPad maskfun_inputs[] = {
296 .type = AVMEDIA_TYPE_VIDEO,
297 .filter_frame = filter_frame,
298 .config_props = config_input,
304 static const AVFilterPad maskfun_outputs[] = {
307 .type = AVMEDIA_TYPE_VIDEO,
312 const AVFilter ff_vf_maskfun = {
314 .description = NULL_IF_CONFIG_SMALL("Create Mask."),
315 .priv_size = sizeof(MaskFunContext),
316 .query_formats = query_formats,
318 .inputs = maskfun_inputs,
319 .outputs = maskfun_outputs,
320 .priv_class = &maskfun_class,
321 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS,
322 .process_command = process_command,