FFmpeg
Loading...
Searching...
No Matches
vf_maskfun.c
Go to the documentation of this file.
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
28typedef 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];
39 int depth;
40 int max;
41 uint64_t max_sum;
42
45
47 int (*maskfun)(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs);
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
53static 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
63
84
85static 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);
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) \
124static 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
151GETSUM(8, uint8_t, 1)
152GETSUM(16, uint16_t, 2)
153
154#define MASKFUN(name, type, div) \
155static 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 = ff_slice_pos(h, jobnr, nb_jobs); \
172 const int slice_end = ff_slice_pos(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
198MASKFUN(8, uint8_t, 1)
199MASKFUN(16, uint16_t, 2)
200
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
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
240static int config_input(AVFilterLink *inlink)
241{
242 AVFilterContext *ctx = inlink->dst;
243 MaskFunContext *s = ctx->priv;
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
275
277
278 return 0;
279}
280
281static 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)
295
296 if (fill != s->fill)
298
299 return 0;
300}
301
303{
304 MaskFunContext *s = ctx->priv;
305
306 av_frame_free(&s->empty);
307}
308
309static const AVFilterPad maskfun_inputs[] = {
310 {
311 .name = "default",
312 .type = AVMEDIA_TYPE_VIDEO,
314 .filter_frame = filter_frame,
315 .config_props = config_input,
316 },
317};
318
320 .p.name = "maskfun",
321 .p.description = NULL_IF_CONFIG_SMALL("Create Mask."),
322 .p.priv_class = &maskfun_class,
324 .priv_size = sizeof(MaskFunContext),
325 .uninit = uninit,
329 .process_command = process_command,
330};
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
Definition dsp.h:87
static int config_input(AVFilterLink *inlink)
static int process_command(AVFilterContext *ctx, const char *cmd, const char *args, char *res, int res_len, int flags)
const FFFilter ff_vf_maskfun
Definition vf_maskfun.c:319
static FILE * out
static AVFormatContext * ctx
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition avfilter.c:1068
int ff_filter_process_command(AVFilterContext *ctx, const char *cmd, const char *arg, char *res, int res_len, int flags)
Generic processing of user supplied commands that are set in the same way as the filter options.
Definition avfilter.c:906
int ff_filter_execute(AVFilterContext *ctx, avfilter_action_func *func, void *arg, int *ret, int nb_jobs)
Definition avfilter.c:1696
int ff_filter_get_nb_threads(AVFilterContext *ctx)
Get number of threads for current filter instance.
Definition avfilter.c:846
Main libavfilter public API header.
#define flags(name, subs,...)
Definition cbs_h264.c:74
#define s(width, name)
Definition cbs_vp9.c:198
#define AV_CEIL_RSHIFT(a, b)
Definition common.h:60
#define NULL
Definition coverity.c:32
static int filter_frame(DBEDecodeContext *s, AVFrame *frame)
Definition dolby_e.c:1067
int high
Definition dovi_rpuenc.c:39
@ AV_OPT_TYPE_INT
Underlying C type is int.
Definition opt.h:258
#define AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC
Some filters support a generic "enable" expression option that can be used to enable or disable a fil...
Definition avfilter.h:196
#define AVFILTER_FLAG_SLICE_THREADS
The filter supports multithreading by splitting frames into multiple parts and processing them concur...
Definition avfilter.h:166
#define AVERROR(e)
Definition error.h:45
int av_frame_is_writable(AVFrame *frame)
Check if the frame data is writable.
Definition frame.c:535
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition frame.c:64
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
Definition frame.c:599
AVFrame * av_frame_clone(const AVFrame *src)
Create a new frame that references the same data as src.
Definition frame.c:483
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
int av_image_fill_linesizes(int linesizes[4], enum AVPixelFormat pix_fmt, int width)
Fill plane linesizes for an image with pixel format pix_fmt and width width.
Definition imgutils.c:89
misc image utilities
static av_cold void uninit(AVBitStreamFilterContext *ctx)
const char * arg
Definition jacosubdec.c:65
#define FILTER_INPUTS(array)
Definition filters.h:264
#define FILTER_OUTPUTS(array)
Definition filters.h:265
#define AVFILTERPAD_FLAG_NEEDS_WRITABLE
The filter expects writable frames from its input link, duplicating data buffers if needed.
Definition filters.h:59
#define FILTER_PIXFMTS_ARRAY(array)
Definition filters.h:244
#define AVFILTER_DEFINE_CLASS(fname)
Definition filters.h:478
#define av_cold
Definition attributes.h:117
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:88
static enum AVPixelFormat pix_fmts[]
Definition libkvazaar.c:296
const char * desc
Definition libsvtav1.c:83
static const struct @257111027162314367033347246032313251342043035002 planes[]
#define FFMIN(a, b)
Definition macros.h:49
AVOptions.
int av_pix_fmt_count_planes(enum AVPixelFormat pix_fmt)
Definition pixdesc.c:3500
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition pixdesc.c:3460
#define AV_PIX_FMT_GBRAP12
Definition pixfmt.h:569
#define AV_PIX_FMT_YUV420P16
Definition pixfmt.h:556
#define AV_PIX_FMT_YUV444P12
Definition pixfmt.h:552
#define AV_PIX_FMT_YUV444P9
Definition pixfmt.h:544
#define AV_PIX_FMT_YUV420P10
Definition pixfmt.h:545
#define AV_PIX_FMT_YUV440P12
Definition pixfmt.h:551
#define AV_PIX_FMT_GRAY9
Definition pixfmt.h:524
#define AV_PIX_FMT_GBRAP16
Definition pixfmt.h:571
#define AV_PIX_FMT_GBRP9
Definition pixfmt.h:563
#define AV_PIX_FMT_YUV422P9
Definition pixfmt.h:543
#define AV_PIX_FMT_YUVA444P10
Definition pixfmt.h:598
#define AV_PIX_FMT_YUVA420P16
Definition pixfmt.h:601
#define AV_PIX_FMT_YUV420P12
Definition pixfmt.h:549
#define AV_PIX_FMT_YUVA420P10
Definition pixfmt.h:596
#define AV_PIX_FMT_YUVA422P9
Definition pixfmt.h:594
#define AV_PIX_FMT_YUV422P12
Definition pixfmt.h:550
#define AV_PIX_FMT_GBRP10
Definition pixfmt.h:564
#define AV_PIX_FMT_YUV422P10
Definition pixfmt.h:546
#define AV_PIX_FMT_GRAY12
Definition pixfmt.h:526
#define AV_PIX_FMT_GBRP12
Definition pixfmt.h:565
#define AV_PIX_FMT_YUV420P9
Definition pixfmt.h:542
#define AV_PIX_FMT_YUVA420P9
Definition pixfmt.h:593
#define AV_PIX_FMT_YUVA422P10
Definition pixfmt.h:597
#define AV_PIX_FMT_YUV420P14
Definition pixfmt.h:553
AVPixelFormat
Pixel format.
Definition pixfmt.h:71
@ AV_PIX_FMT_NONE
Definition pixfmt.h:72
@ AV_PIX_FMT_YUV420P
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition pixfmt.h:73
@ AV_PIX_FMT_YUV440P
planar YUV 4:4:0 (1 Cr & Cb sample per 1x2 Y samples)
Definition pixfmt.h:106
@ AV_PIX_FMT_YUV422P
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition pixfmt.h:77
@ AV_PIX_FMT_GRAY8
Y , 8bpp.
Definition pixfmt.h:81
@ AV_PIX_FMT_YUVA420P
planar YUV 4:2:0, 20bpp, (1 Cr & Cb sample per 2x2 Y & A samples)
Definition pixfmt.h:108
@ AV_PIX_FMT_YUVJ440P
planar YUV 4:4:0 full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV440P and setting color_range
Definition pixfmt.h:107
@ AV_PIX_FMT_YUV410P
planar YUV 4:1:0, 9bpp, (1 Cr & Cb sample per 4x4 Y samples)
Definition pixfmt.h:79
@ AV_PIX_FMT_YUV411P
planar YUV 4:1:1, 12bpp, (1 Cr & Cb sample per 4x1 Y samples)
Definition pixfmt.h:80
@ AV_PIX_FMT_YUV444P
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition pixfmt.h:78
@ AV_PIX_FMT_YUVA444P
planar YUV 4:4:4 32bpp, (1 Cr & Cb sample per 1x1 Y & A samples)
Definition pixfmt.h:174
@ AV_PIX_FMT_YUVJ411P
planar YUV 4:1:1, 12bpp, (1 Cr & Cb sample per 4x1 Y samples) full scale (JPEG), deprecated in favor ...
Definition pixfmt.h:283
@ AV_PIX_FMT_GBRAP
planar GBRA 4:4:4:4 32bpp
Definition pixfmt.h:212
@ AV_PIX_FMT_YUVJ422P
planar YUV 4:2:2, 16bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV422P and setting col...
Definition pixfmt.h:86
@ AV_PIX_FMT_YUVA422P
planar YUV 4:2:2 24bpp, (1 Cr & Cb sample per 2x1 Y & A samples)
Definition pixfmt.h:173
@ AV_PIX_FMT_GBRP
planar GBR 4:4:4 24bpp
Definition pixfmt.h:165
@ AV_PIX_FMT_YUVJ444P
planar YUV 4:4:4, 24bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV444P and setting col...
Definition pixfmt.h:87
@ AV_PIX_FMT_YUVJ420P
planar YUV 4:2:0, 12bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV420P and setting col...
Definition pixfmt.h:85
#define AV_PIX_FMT_YUV422P14
Definition pixfmt.h:554
#define AV_PIX_FMT_GRAY10
Definition pixfmt.h:525
#define AV_PIX_FMT_GRAY14
Definition pixfmt.h:527
#define AV_PIX_FMT_YUV422P16
Definition pixfmt.h:557
#define AV_PIX_FMT_GRAY16
Definition pixfmt.h:528
#define AV_PIX_FMT_GBRAP10
Definition pixfmt.h:568
#define AV_PIX_FMT_YUVA444P16
Definition pixfmt.h:603
#define AV_PIX_FMT_YUVA422P16
Definition pixfmt.h:602
#define AV_PIX_FMT_GBRP16
Definition pixfmt.h:567
#define AV_PIX_FMT_YUV444P14
Definition pixfmt.h:555
#define AV_PIX_FMT_YUVA444P9
Definition pixfmt.h:595
#define AV_PIX_FMT_GBRP14
Definition pixfmt.h:566
#define AV_PIX_FMT_YUV444P16
Definition pixfmt.h:558
#define AV_PIX_FMT_YUV444P10
Definition pixfmt.h:548
static void fill_frame(uint16_t *pbuf, int buf_size, uint16_t color)
Definition sanm.c:2674
Describe the class of an AVClass context structure.
Definition log.h:76
An instance of a filter.
Definition avfilter.h:273
A filter pad used for either input or output.
Definition filters.h:40
This structure describes decoded (raw) audio or video data.
Definition frame.h:472
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition frame.h:574
AVOption.
Definition opt.h:428
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition pixdesc.h:69
int(* maskfun)(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
Definition vf_maskfun.c:47
int planeheight[4]
Definition vf_maskfun.c:37
uint64_t max_sum
Definition vf_maskfun.c:41
int planewidth[4]
Definition vf_maskfun.c:37
AVFrame * in
Definition vf_maskfun.c:43
AVFrame * empty
Definition vf_maskfun.c:44
int(* getsum)(AVFilterContext *ctx, AVFrame *out)
Definition vf_maskfun.c:46
int linesize[4]
Definition vf_maskfun.c:36
#define VFT
Definition vf_amplify.c:243
static const AVOption maskfun_options[]
Definition vf_maskfun.c:53
static const AVFilterPad maskfun_inputs[]
Definition vf_maskfun.c:309
static int config_input(AVFilterLink *inlink)
Definition vf_maskfun.c:240
static void fill_frame(AVFilterContext *ctx)
Definition vf_maskfun.c:201
static int filter_frame(AVFilterLink *inlink, AVFrame *in)
Definition vf_maskfun.c:85
static void set_max_sum(AVFilterContext *ctx)
Definition vf_maskfun.c:228
static int process_command(AVFilterContext *ctx, const char *cmd, const char *args, char *res, int res_len, int flags)
Definition vf_maskfun.c:281
static av_cold void uninit(AVFilterContext *ctx)
Definition vf_maskfun.c:302
#define OFFSET(x)
Definition vf_maskfun.c:50
#define GETSUM(name, type, div)
Definition vf_maskfun.c:123
#define MASKFUN(name, type, div)
Definition vf_maskfun.c:154
static void hsub(htype *dst, const htype *src, int bins)
Definition vf_median.c:74
const AVFilterPad ff_video_default_filterpad[1]
An AVFilterPad array whose only entry has name "default" and is of type AVMEDIA_TYPE_VIDEO.
Definition video.c:37
AVFrame * ff_get_video_buffer(AVFilterLink *link, int w, int h)
Request a picture buffer with a specific set of permissions.
Definition video.c:89