FFmpeg coverage


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