FFmpeg  4.4.8
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 "formats.h"
27 #include "internal.h"
28 #include "video.h"
29 
30 typedef struct MaskFunContext {
31  const AVClass *class;
32 
33  int low, high;
34  int planes;
35  int fill;
36  int sum;
37 
38  int linesize[4];
39  int width[4], height[4];
40  int nb_planes;
41  int depth;
42  int max;
43  uint64_t max_sum;
44 
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 
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 
63 
65 {
66  static const enum AVPixelFormat pix_fmts[] = {
85  };
86 
88 }
89 
90 static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
91 {
92  AVFilterContext *ctx = inlink->dst;
93  MaskFunContext *s = ctx->priv;
94  AVFilterLink *outlink = ctx->outputs[0];
95 
96  if (s->getsum(ctx, frame)) {
97  AVFrame *out = av_frame_clone(s->empty);
98 
99  if (!out) {
101  return AVERROR(ENOMEM);
102  }
103  out->pts = frame->pts;
105 
106  return ff_filter_frame(outlink, out);
107  }
108 
109  ctx->internal->execute(ctx, s->maskfun, frame, NULL,
110  FFMIN(s->height[1], ff_filter_get_nb_threads(ctx)));
111 
112  return ff_filter_frame(outlink, frame);
113 }
114 
115 #define GETSUM(name, type, div) \
116 static int getsum##name(AVFilterContext *ctx, AVFrame *out) \
117 { \
118  MaskFunContext *s = ctx->priv; \
119  uint64_t sum = 0; \
120  int p; \
121  \
122  for (p = 0; p < s->nb_planes; p++) { \
123  const int linesize = out->linesize[p] / div; \
124  const int w = s->width[p]; \
125  const int h = s->height[p]; \
126  type *dst = (type *)out->data[p]; \
127  \
128  if (!((1 << p) & s->planes)) \
129  continue; \
130  \
131  for (int y = 0; y < h; y++) { \
132  for (int x = 0; x < w; x++) \
133  sum += dst[x]; \
134  if (sum >= s->max_sum) \
135  return 1; \
136  dst += linesize; \
137  } \
138  } \
139  \
140  return 0; \
141 }
142 
143 GETSUM(8, uint8_t, 1)
144 GETSUM(16, uint16_t, 2)
145 
146 #define MASKFUN(name, type, div) \
147 static int maskfun##name(AVFilterContext *ctx, void *arg, \
148  int jobnr, int nb_jobs) \
149 { \
150  MaskFunContext *s = ctx->priv; \
151  AVFrame *out = arg; \
152  const int low = s->low; \
153  const int high = s->high; \
154  const int max = s->max; \
155  int p; \
156  \
157  for (p = 0; p < s->nb_planes; p++) { \
158  const int linesize = out->linesize[p] / div; \
159  const int w = s->width[p]; \
160  const int h = s->height[p]; \
161  const int slice_start = ff_slice_pos(h, jobnr, nb_jobs); \
162  const int slice_end = ff_slice_pos(h, jobnr + 1, nb_jobs); \
163  type *dst = (type *)out->data[p] + slice_start * linesize; \
164  \
165  if (!((1 << p) & s->planes)) \
166  continue; \
167  \
168  for (int y = slice_start; y < slice_end; y++) { \
169  for (int x = 0; x < w; x++) { \
170  if (dst[x] <= low) \
171  dst[x] = 0; \
172  else if (dst[x] > high) \
173  dst[x] = max; \
174  } \
175  \
176  dst += linesize; \
177  } \
178  } \
179  \
180  return 0; \
181 }
182 
183 MASKFUN(8, uint8_t, 1)
184 MASKFUN(16, uint16_t, 2)
185 
187 {
188  MaskFunContext *s = ctx->priv;
189 
190  s->fill = FFMIN(s->fill, s->max);
191  if (s->depth == 8) {
192  for (int p = 0; p < s->nb_planes; p++) {
193  uint8_t *dst = s->empty->data[p];
194 
195  for (int y = 0; y < s->height[p]; y++) {
196  memset(dst, s->fill, s->width[p]);
197  dst += s->empty->linesize[p];
198  }
199  }
200  } else {
201  for (int p = 0; p < s->nb_planes; p++) {
202  uint16_t *dst = (uint16_t *)s->empty->data[p];
203 
204  for (int y = 0; y < s->height[p]; y++) {
205  for (int x = 0; x < s->width[p]; x++)
206  dst[x] = s->fill;
207  dst += s->empty->linesize[p] / 2;
208  }
209  }
210  }
211 }
212 
214 {
215  MaskFunContext *s = ctx->priv;
216 
217  s->max_sum = 0;
218  for (int p = 0; p < s->nb_planes; p++) {
219  if (!((1 << p) & s->planes))
220  continue;
221  s->max_sum += (uint64_t)s->sum * s->width[p] * s->height[p];
222  }
223 }
224 
225 static int config_input(AVFilterLink *inlink)
226 {
227  AVFilterContext *ctx = inlink->dst;
228  MaskFunContext *s = ctx->priv;
230  int vsub, hsub, ret;
231 
232  s->nb_planes = av_pix_fmt_count_planes(inlink->format);
233 
234  if ((ret = av_image_fill_linesizes(s->linesize, inlink->format, inlink->w)) < 0)
235  return ret;
236 
237  hsub = desc->log2_chroma_w;
238  vsub = desc->log2_chroma_h;
239  s->height[1] = s->height[2] = AV_CEIL_RSHIFT(inlink->h, vsub);
240  s->height[0] = s->height[3] = inlink->h;
241  s->width[1] = s->width[2] = AV_CEIL_RSHIFT(inlink->w, hsub);
242  s->width[0] = s->width[3] = inlink->w;
243 
244  s->depth = desc->comp[0].depth;
245  s->max = (1 << s->depth) - 1;
246 
247  if (s->depth == 8) {
248  s->maskfun = maskfun8;
249  s->getsum = getsum8;
250  } else {
251  s->maskfun = maskfun16;
252  s->getsum = getsum16;
253  }
254 
255  s->empty = ff_get_video_buffer(inlink, inlink->w, inlink->h);
256  if (!s->empty)
257  return AVERROR(ENOMEM);
258 
259  fill_frame(ctx);
260 
261  set_max_sum(ctx);
262 
263  return 0;
264 }
265 
266 static int process_command(AVFilterContext *ctx, const char *cmd, const char *args,
267  char *res, int res_len, int flags)
268 {
269  MaskFunContext *s = ctx->priv;
270  int fill = s->fill;
271  int sum = s->sum;
272  int ret;
273 
274  ret = ff_filter_process_command(ctx, cmd, args, res, res_len, flags);
275  if (ret < 0)
276  return ret;
277 
278  if (sum != s->sum)
279  set_max_sum(ctx);
280 
281  if (fill != s->fill)
282  fill_frame(ctx);
283 
284  return 0;
285 }
286 
288 {
289  MaskFunContext *s = ctx->priv;
290 
291  av_frame_free(&s->empty);
292 }
293 
294 static const AVFilterPad maskfun_inputs[] = {
295  {
296  .name = "default",
297  .type = AVMEDIA_TYPE_VIDEO,
298  .filter_frame = filter_frame,
299  .config_props = config_input,
300  .needs_writable = 1,
301  },
302  { NULL }
303 };
304 
305 static const AVFilterPad maskfun_outputs[] = {
306  {
307  .name = "default",
308  .type = AVMEDIA_TYPE_VIDEO,
309  },
310  { NULL }
311 };
312 
314  .name = "maskfun",
315  .description = NULL_IF_CONFIG_SMALL("Create Mask."),
316  .priv_size = sizeof(MaskFunContext),
318  .uninit = uninit,
321  .priv_class = &maskfun_class,
324 };
static const AVFilterPad inputs[]
Definition: af_acontrast.c:193
static const AVFilterPad outputs[]
Definition: af_acontrast.c:203
#define av_cold
Definition: attributes.h:88
uint8_t
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1096
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:882
int ff_filter_get_nb_threads(AVFilterContext *ctx)
Get number of threads for current filter instance.
Definition: avfilter.c:802
Main libavfilter public API header.
#define flags(name, subs,...)
Definition: cbs_av1.c:572
#define s(width, name)
Definition: cbs_vp9.c:257
#define FFMIN(a, b)
Definition: common.h:105
#define AV_CEIL_RSHIFT(a, b)
Definition: common.h:58
#define NULL
Definition: coverity.c:32
static AVFrame * frame
int
int ff_set_common_formats(AVFilterContext *ctx, AVFilterFormats *formats)
A helper for query_formats() which sets all links to the same list of formats.
Definition: formats.c:587
AVFilterFormats * ff_make_format_list(const int *fmts)
Create a list of supported formats.
Definition: formats.c:286
@ AV_OPT_TYPE_INT
Definition: opt.h:225
#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:126
#define AVFILTER_FLAG_SLICE_THREADS
The filter supports multithreading by splitting frames into multiple parts and processing them concur...
Definition: avfilter.h:117
#define AVERROR(e)
Definition: error.h:43
AVFrame * av_frame_clone(const AVFrame *src)
Create a new frame that references the same data as src.
Definition: frame.c:540
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:203
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
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
for(j=16;j >0;--j)
misc image utilities
const char * arg
Definition: jacosubdec.c:66
common internal API header
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:117
static enum AVPixelFormat pix_fmts[]
Definition: libkvazaar.c:309
const char * desc
Definition: libsvtav1.c:79
static const struct @322 planes[]
AVOptions.
int av_pix_fmt_count_planes(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2613
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2573
#define AV_PIX_FMT_GBRAP12
Definition: pixfmt.h:420
#define AV_PIX_FMT_YUV420P16
Definition: pixfmt.h:410
#define AV_PIX_FMT_YUV444P12
Definition: pixfmt.h:406
#define AV_PIX_FMT_YUV444P9
Definition: pixfmt.h:398
#define AV_PIX_FMT_YUV420P10
Definition: pixfmt.h:399
#define AV_PIX_FMT_YUV440P12
Definition: pixfmt.h:405
#define AV_PIX_FMT_GRAY9
Definition: pixfmt.h:379
#define AV_PIX_FMT_GBRAP16
Definition: pixfmt.h:421
#define AV_PIX_FMT_GBRP9
Definition: pixfmt.h:414
#define AV_PIX_FMT_YUV422P9
Definition: pixfmt.h:397
#define AV_PIX_FMT_YUVA444P10
Definition: pixfmt.h:438
#define AV_PIX_FMT_YUVA420P16
Definition: pixfmt.h:441
#define AV_PIX_FMT_YUV420P12
Definition: pixfmt.h:403
#define AV_PIX_FMT_YUVA420P10
Definition: pixfmt.h:436
#define AV_PIX_FMT_YUVA422P9
Definition: pixfmt.h:434
#define AV_PIX_FMT_YUV422P12
Definition: pixfmt.h:404
#define AV_PIX_FMT_GBRP10
Definition: pixfmt.h:415
#define AV_PIX_FMT_YUV422P10
Definition: pixfmt.h:400
#define AV_PIX_FMT_GRAY12
Definition: pixfmt.h:381
#define AV_PIX_FMT_GBRP12
Definition: pixfmt.h:416
#define AV_PIX_FMT_YUV420P9
Definition: pixfmt.h:396
#define AV_PIX_FMT_YUVA420P9
Definition: pixfmt.h:433
#define AV_PIX_FMT_YUVA422P10
Definition: pixfmt.h:437
#define AV_PIX_FMT_YUV420P14
Definition: pixfmt.h:407
AVPixelFormat
Pixel format.
Definition: pixfmt.h:64
@ AV_PIX_FMT_NONE
Definition: pixfmt.h:65
@ AV_PIX_FMT_YUV420P
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:66
@ AV_PIX_FMT_YUV440P
planar YUV 4:4:0 (1 Cr & Cb sample per 1x2 Y samples)
Definition: pixfmt.h:99
@ AV_PIX_FMT_YUV422P
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition: pixfmt.h:70
@ AV_PIX_FMT_GRAY8
Y , 8bpp.
Definition: pixfmt.h:74
@ AV_PIX_FMT_YUVA420P
planar YUV 4:2:0, 20bpp, (1 Cr & Cb sample per 2x2 Y & A samples)
Definition: pixfmt.h:101
@ 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:100
@ AV_PIX_FMT_YUV410P
planar YUV 4:1:0, 9bpp, (1 Cr & Cb sample per 4x4 Y samples)
Definition: pixfmt.h:72
@ AV_PIX_FMT_YUV411P
planar YUV 4:1:1, 12bpp, (1 Cr & Cb sample per 4x1 Y samples)
Definition: pixfmt.h:73
@ AV_PIX_FMT_YUV444P
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition: pixfmt.h:71
@ AV_PIX_FMT_YUVA444P
planar YUV 4:4:4 32bpp, (1 Cr & Cb sample per 1x1 Y & A samples)
Definition: pixfmt.h:177
@ 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:258
@ AV_PIX_FMT_GBRAP
planar GBRA 4:4:4:4 32bpp
Definition: pixfmt.h:215
@ 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:79
@ AV_PIX_FMT_YUVA422P
planar YUV 4:2:2 24bpp, (1 Cr & Cb sample per 2x1 Y & A samples)
Definition: pixfmt.h:176
@ AV_PIX_FMT_GBRP
planar GBR 4:4:4 24bpp
Definition: pixfmt.h:168
@ 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:80
@ 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:78
#define AV_PIX_FMT_YUV422P14
Definition: pixfmt.h:408
#define AV_PIX_FMT_GRAY10
Definition: pixfmt.h:380
#define AV_PIX_FMT_GRAY14
Definition: pixfmt.h:382
#define AV_PIX_FMT_YUV422P16
Definition: pixfmt.h:411
#define AV_PIX_FMT_GRAY16
Definition: pixfmt.h:383
#define AV_PIX_FMT_GBRAP10
Definition: pixfmt.h:419
#define AV_PIX_FMT_YUVA444P16
Definition: pixfmt.h:443
#define AV_PIX_FMT_YUVA422P16
Definition: pixfmt.h:442
#define AV_PIX_FMT_GBRP16
Definition: pixfmt.h:418
#define AV_PIX_FMT_YUV444P14
Definition: pixfmt.h:409
#define AV_PIX_FMT_YUVA444P9
Definition: pixfmt.h:435
#define AV_PIX_FMT_GBRP14
Definition: pixfmt.h:417
#define AV_PIX_FMT_YUV444P16
Definition: pixfmt.h:412
#define AV_PIX_FMT_YUV444P10
Definition: pixfmt.h:402
Describe the class of an AVClass context structure.
Definition: log.h:67
An instance of a filter.
Definition: avfilter.h:341
A filter pad used for either input or output.
Definition: internal.h:54
const char * name
Pad name.
Definition: internal.h:60
Filter definition.
Definition: avfilter.h:145
const char * name
Filter name.
Definition: avfilter.h:149
AVFormatInternal * internal
An opaque field for libavformat internal usage.
Definition: avformat.h:1699
This structure describes decoded (raw) audio or video data.
Definition: frame.h:318
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:411
AVOption.
Definition: opt.h:248
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:81
int height[4]
Definition: vf_maskfun.c:39
int(* maskfun)(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
Definition: vf_maskfun.c:47
int width[4]
Definition: vf_maskfun.c:39
uint64_t max_sum
Definition: vf_maskfun.c:43
AVFrame * empty
Definition: vf_maskfun.c:45
int(* getsum)(AVFilterContext *ctx, AVFrame *out)
Definition: vf_maskfun.c:46
int linesize[4]
Definition: vf_maskfun.c:38
FILE * out
Definition: movenc.c:54
AVFormatContext * ctx
Definition: movenc.c:48
#define VFT
Definition: vf_maskfun.c:51
static const AVFilterPad maskfun_outputs[]
Definition: vf_maskfun.c:305
static const AVOption maskfun_options[]
Definition: vf_maskfun.c:53
static int query_formats(AVFilterContext *ctx)
Definition: vf_maskfun.c:64
static const AVFilterPad maskfun_inputs[]
Definition: vf_maskfun.c:294
static int config_input(AVFilterLink *inlink)
Definition: vf_maskfun.c:225
static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
Definition: vf_maskfun.c:90
static void fill_frame(AVFilterContext *ctx)
Definition: vf_maskfun.c:186
AVFILTER_DEFINE_CLASS(maskfun)
static void set_max_sum(AVFilterContext *ctx)
Definition: vf_maskfun.c:213
static int process_command(AVFilterContext *ctx, const char *cmd, const char *args, char *res, int res_len, int flags)
Definition: vf_maskfun.c:266
static av_cold void uninit(AVFilterContext *ctx)
Definition: vf_maskfun.c:287
AVFilter ff_vf_maskfun
Definition: vf_maskfun.c:313
#define OFFSET(x)
Definition: vf_maskfun.c:50
#define GETSUM(name, type, div)
Definition: vf_maskfun.c:115
#define MASKFUN(name, type, div)
Definition: vf_maskfun.c:146
static void hsub(htype *dst, const htype *src, int bins)
Definition: vf_median.c:76
AVFrame * ff_get_video_buffer(AVFilterLink *link, int w, int h)
Request a picture buffer with a specific set of permissions.
Definition: video.c:104