FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/vf_maskfun.c
Date: 2024-11-20 23:03:26
Exec Total Coverage
Lines: 0 94 0.0%
Functions: 0 10 0.0%
Branches: 0 84 0.0%

Line Branch Exec Source
1 /*
2 * Copyright (c) 2018 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 #include "libavutil/imgutils.h"
22 #include "libavutil/pixdesc.h"
23 #include "libavutil/opt.h"
24 #include "avfilter.h"
25 #include "filters.h"
26 #include "video.h"
27
28 typedef struct MaskFunContext {
29 const AVClass *class;
30
31 int low, high;
32 int planes;
33 int fill;
34 int sum;
35
36 int linesize[4];
37 int planewidth[4], planeheight[4];
38 int nb_planes;
39 int depth;
40 int max;
41 uint64_t max_sum;
42
43 AVFrame *in;
44 AVFrame *empty;
45
46 int (*getsum)(AVFilterContext *ctx, AVFrame *out);
47 int (*maskfun)(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs);
48 } MaskFunContext;
49
50 #define OFFSET(x) offsetof(MaskFunContext, x)
51 #define VFT AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
52
53 static const AVOption maskfun_options[] = {
54 { "low", "set low threshold", OFFSET(low), AV_OPT_TYPE_INT, {.i64=10}, 0, UINT16_MAX, VFT },
55 { "high", "set high threshold", OFFSET(high), AV_OPT_TYPE_INT, {.i64=10}, 0, UINT16_MAX, VFT },
56 { "planes", "set planes", OFFSET(planes), AV_OPT_TYPE_INT, {.i64=0xF}, 0, 0xF, VFT },
57 { "fill", "set fill value", OFFSET(fill), AV_OPT_TYPE_INT, {.i64=0}, 0, UINT16_MAX, VFT },
58 { "sum", "set sum value", OFFSET(sum), AV_OPT_TYPE_INT, {.i64=10}, 0, UINT16_MAX, VFT },
59 { NULL }
60 };
61
62 AVFILTER_DEFINE_CLASS(maskfun);
63
64 static const enum AVPixelFormat pix_fmts[] = {
65 AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV440P,
66 AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P,
67 AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUV420P,
68 AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ420P,
69 AV_PIX_FMT_YUVJ411P, AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P,
70 AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV444P9,
71 AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10,
72 AV_PIX_FMT_YUV420P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV444P12, AV_PIX_FMT_YUV440P12,
73 AV_PIX_FMT_YUV420P14, AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV444P14,
74 AV_PIX_FMT_YUV420P16, AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV444P16,
75 AV_PIX_FMT_YUVA420P9, AV_PIX_FMT_YUVA422P9, AV_PIX_FMT_YUVA444P9,
76 AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA444P10,
77 AV_PIX_FMT_YUVA420P16, AV_PIX_FMT_YUVA422P16, AV_PIX_FMT_YUVA444P16,
78 AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRP9, AV_PIX_FMT_GBRP10,
79 AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRP14, AV_PIX_FMT_GBRP16,
80 AV_PIX_FMT_GBRAP, AV_PIX_FMT_GBRAP10, AV_PIX_FMT_GBRAP12, AV_PIX_FMT_GBRAP16,
81 AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY9, AV_PIX_FMT_GRAY10, AV_PIX_FMT_GRAY12, AV_PIX_FMT_GRAY14, AV_PIX_FMT_GRAY16,
82 AV_PIX_FMT_NONE
83 };
84
85 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
86 {
87 AVFilterContext *ctx = inlink->dst;
88 MaskFunContext *s = ctx->priv;
89 AVFilterLink *outlink = ctx->outputs[0];
90 AVFrame *out;
91
92 if (s->getsum(ctx, in)) {
93 AVFrame *out = av_frame_clone(s->empty);
94
95 if (!out) {
96 av_frame_free(&in);
97 return AVERROR(ENOMEM);
98 }
99 out->pts = in->pts;
100 av_frame_free(&in);
101
102 return ff_filter_frame(outlink, out);
103 }
104
105 if (av_frame_is_writable(in)) {
106 out = in;
107 } else {
108 out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
109 if (!out)
110 return AVERROR(ENOMEM);
111 av_frame_copy_props(out, in);
112 }
113
114 s->in = in;
115 ff_filter_execute(ctx, s->maskfun, out, NULL,
116 FFMIN(s->planeheight[1], ff_filter_get_nb_threads(ctx)));
117
118 if (out != in)
119 av_frame_free(&in);
120 return ff_filter_frame(outlink, out);
121 }
122
123 #define GETSUM(name, type, div) \
124 static int getsum##name(AVFilterContext *ctx, AVFrame *out) \
125 { \
126 MaskFunContext *s = ctx->priv; \
127 uint64_t sum = 0; \
128 int p; \
129 \
130 for (p = 0; p < s->nb_planes; p++) { \
131 const int linesize = out->linesize[p] / div; \
132 const int w = s->planewidth[p]; \
133 const int h = s->planeheight[p]; \
134 type *dst = (type *)out->data[p]; \
135 \
136 if (!((1 << p) & s->planes)) \
137 continue; \
138 \
139 for (int y = 0; y < h; y++) { \
140 for (int x = 0; x < w; x++) \
141 sum += dst[x]; \
142 if (sum >= s->max_sum) \
143 return 1; \
144 dst += linesize; \
145 } \
146 } \
147 \
148 return 0; \
149 }
150
151 GETSUM(8, uint8_t, 1)
152 GETSUM(16, uint16_t, 2)
153
154 #define MASKFUN(name, type, div) \
155 static int maskfun##name(AVFilterContext *ctx, void *arg, \
156 int jobnr, int nb_jobs) \
157 { \
158 MaskFunContext *s = ctx->priv; \
159 AVFrame *in = s->in; \
160 AVFrame *out = arg; \
161 const int low = s->low; \
162 const int high = s->high; \
163 const int max = s->max; \
164 int p; \
165 \
166 for (p = 0; p < s->nb_planes; p++) { \
167 const int src_linesize = in->linesize[p] / div; \
168 const int linesize = out->linesize[p] / div; \
169 const int w = s->planewidth[p]; \
170 const int h = s->planeheight[p]; \
171 const int slice_start = (h * jobnr) / nb_jobs; \
172 const int slice_end = (h * (jobnr+1)) / nb_jobs; \
173 const type *src = (type *)in->data[p] + \
174 slice_start * src_linesize; \
175 type *dst = (type *)out->data[p] + \
176 slice_start * linesize; \
177 \
178 if (!((1 << p) & s->planes)) \
179 continue; \
180 \
181 for (int y = slice_start; y < slice_end; y++) { \
182 for (int x = 0; x < w; x++) { \
183 dst[x] = src[x]; \
184 if (dst[x] <= low) \
185 dst[x] = 0; \
186 else if (dst[x] > high) \
187 dst[x] = max; \
188 } \
189 \
190 src += src_linesize; \
191 dst += linesize; \
192 } \
193 } \
194 \
195 return 0; \
196 }
197
198 MASKFUN(8, uint8_t, 1)
199 MASKFUN(16, uint16_t, 2)
200
201 static void fill_frame(AVFilterContext *ctx)
202 {
203 MaskFunContext *s = ctx->priv;
204
205 s->fill = FFMIN(s->fill, s->max);
206 if (s->depth == 8) {
207 for (int p = 0; p < s->nb_planes; p++) {
208 uint8_t *dst = s->empty->data[p];
209
210 for (int y = 0; y < s->planeheight[p]; y++) {
211 memset(dst, s->fill, s->planewidth[p]);
212 dst += s->empty->linesize[p];
213 }
214 }
215 } else {
216 for (int p = 0; p < s->nb_planes; p++) {
217 uint16_t *dst = (uint16_t *)s->empty->data[p];
218
219 for (int y = 0; y < s->planeheight[p]; y++) {
220 for (int x = 0; x < s->planewidth[p]; x++)
221 dst[x] = s->fill;
222 dst += s->empty->linesize[p] / 2;
223 }
224 }
225 }
226 }
227
228 static void set_max_sum(AVFilterContext *ctx)
229 {
230 MaskFunContext *s = ctx->priv;
231
232 s->max_sum = 0;
233 for (int p = 0; p < s->nb_planes; p++) {
234 if (!((1 << p) & s->planes))
235 continue;
236 s->max_sum += (uint64_t)s->sum * s->planewidth[p] * s->planeheight[p];
237 }
238 }
239
240 static int config_input(AVFilterLink *inlink)
241 {
242 AVFilterContext *ctx = inlink->dst;
243 MaskFunContext *s = ctx->priv;
244 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
245 int vsub, hsub, ret;
246
247 s->nb_planes = av_pix_fmt_count_planes(inlink->format);
248
249 if ((ret = av_image_fill_linesizes(s->linesize, inlink->format, inlink->w)) < 0)
250 return ret;
251
252 hsub = desc->log2_chroma_w;
253 vsub = desc->log2_chroma_h;
254 s->planeheight[1] = s->planeheight[2] = AV_CEIL_RSHIFT(inlink->h, vsub);
255 s->planeheight[0] = s->planeheight[3] = inlink->h;
256 s->planewidth[1] = s->planewidth[2] = AV_CEIL_RSHIFT(inlink->w, hsub);
257 s->planewidth[0] = s->planewidth[3] = inlink->w;
258
259 s->depth = desc->comp[0].depth;
260 s->max = (1 << s->depth) - 1;
261
262 if (s->depth == 8) {
263 s->maskfun = maskfun8;
264 s->getsum = getsum8;
265 } else {
266 s->maskfun = maskfun16;
267 s->getsum = getsum16;
268 }
269
270 s->empty = ff_get_video_buffer(inlink, inlink->w, inlink->h);
271 if (!s->empty)
272 return AVERROR(ENOMEM);
273
274 fill_frame(ctx);
275
276 set_max_sum(ctx);
277
278 return 0;
279 }
280
281 static int process_command(AVFilterContext *ctx, const char *cmd, const char *args,
282 char *res, int res_len, int flags)
283 {
284 MaskFunContext *s = ctx->priv;
285 int fill = s->fill;
286 int sum = s->sum;
287 int ret;
288
289 ret = ff_filter_process_command(ctx, cmd, args, res, res_len, flags);
290 if (ret < 0)
291 return ret;
292
293 if (sum != s->sum)
294 set_max_sum(ctx);
295
296 if (fill != s->fill)
297 fill_frame(ctx);
298
299 return 0;
300 }
301
302 static av_cold void uninit(AVFilterContext *ctx)
303 {
304 MaskFunContext *s = ctx->priv;
305
306 av_frame_free(&s->empty);
307 }
308
309 static const AVFilterPad maskfun_inputs[] = {
310 {
311 .name = "default",
312 .type = AVMEDIA_TYPE_VIDEO,
313 .flags = AVFILTERPAD_FLAG_NEEDS_WRITABLE,
314 .filter_frame = filter_frame,
315 .config_props = config_input,
316 },
317 };
318
319 const AVFilter ff_vf_maskfun = {
320 .name = "maskfun",
321 .description = NULL_IF_CONFIG_SMALL("Create Mask."),
322 .priv_size = sizeof(MaskFunContext),
323 .uninit = uninit,
324 FILTER_INPUTS(maskfun_inputs),
325 FILTER_OUTPUTS(ff_video_default_filterpad),
326 FILTER_PIXFMTS_ARRAY(pix_fmts),
327 .priv_class = &maskfun_class,
328 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS,
329 .process_command = process_command,
330 };
331