FFmpeg  4.2.1
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 "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 width[4], height[4];
39  int nb_planes;
40  int depth;
41  int max;
42  uint64_t max_sum;
43 
46  int (*maskfun)(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs);
48 
49 #define OFFSET(x) offsetof(MaskFunContext, x)
50 #define VF AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
51 
52 static const AVOption maskfun_options[] = {
53  { "low", "set low threshold", OFFSET(low), AV_OPT_TYPE_INT, {.i64=10}, 0, UINT16_MAX, VF },
54  { "high", "set high threshold", OFFSET(high), AV_OPT_TYPE_INT, {.i64=10}, 0, UINT16_MAX, VF },
55  { "planes", "set planes", OFFSET(planes), AV_OPT_TYPE_INT, {.i64=0xF}, 0, 0xF, VF },
56  { "fill", "set fill value", OFFSET(fill), AV_OPT_TYPE_INT, {.i64=0}, 0, UINT16_MAX, VF },
57  { "sum", "set sum value", OFFSET(sum), AV_OPT_TYPE_INT, {.i64=10}, 0, UINT16_MAX, VF },
58  { NULL }
59 };
60 
62 
64 {
65  static const enum AVPixelFormat pix_fmts[] = {
84  };
85 
86  return ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
87 }
88 
89 static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
90 {
91  AVFilterContext *ctx = inlink->dst;
92  MaskFunContext *s = ctx->priv;
93  AVFilterLink *outlink = ctx->outputs[0];
94 
95  if (s->getsum(ctx, frame)) {
97 
98  if (!out) {
99  av_frame_free(&frame);
100  return AVERROR(ENOMEM);
101  }
102  out->pts = frame->pts;
103  av_frame_free(&frame);
104 
105  return ff_filter_frame(outlink, out);
106  }
107 
108  ctx->internal->execute(ctx, s->maskfun, frame, NULL,
109  FFMIN(s->height[1], ff_filter_get_nb_threads(ctx)));
110 
111  return ff_filter_frame(outlink, frame);
112 }
113 
114 #define GETSUM(name, type, div) \
115 static int getsum##name(AVFilterContext *ctx, AVFrame *out) \
116 { \
117  MaskFunContext *s = ctx->priv; \
118  uint64_t sum = 0; \
119  int p; \
120  \
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]; \
126  \
127  if (!((1 << p) & s->planes)) \
128  continue; \
129  \
130  for (int y = 0; y < h; y++) { \
131  for (int x = 0; x < w; x++) \
132  sum += dst[x]; \
133  if (sum >= s->max_sum) \
134  return 1; \
135  dst += linesize; \
136  } \
137  } \
138  \
139  return 0; \
140 }
141 
142 GETSUM(8, uint8_t, 1)
143 GETSUM(16, uint16_t, 2)
144 
145 #define MASKFUN(name, type, div) \
146 static int maskfun##name(AVFilterContext *ctx, void *arg, \
147  int jobnr, int nb_jobs) \
148 { \
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; \
154  int p; \
155  \
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; \
163  \
164  if (!((1 << p) & s->planes)) \
165  continue; \
166  \
167  for (int y = slice_start; y < slice_end; y++) { \
168  for (int x = 0; x < w; x++) { \
169  if (dst[x] <= low) \
170  dst[x] = 0; \
171  else if (dst[x] > high) \
172  dst[x] = max; \
173  } \
174  \
175  dst += linesize; \
176  } \
177  } \
178  \
179  return 0; \
180 }
181 
182 MASKFUN(8, uint8_t, 1)
183 MASKFUN(16, uint16_t, 2)
184 
185 static int config_input(AVFilterLink *inlink)
186 {
187  AVFilterContext *ctx = inlink->dst;
188  MaskFunContext *s = ctx->priv;
189  const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
190  int vsub, hsub, ret;
191 
192  s->nb_planes = av_pix_fmt_count_planes(inlink->format);
193 
194  if ((ret = av_image_fill_linesizes(s->linesize, inlink->format, inlink->w)) < 0)
195  return ret;
196 
197  hsub = desc->log2_chroma_w;
198  vsub = desc->log2_chroma_h;
199  s->height[1] = s->height[2] = AV_CEIL_RSHIFT(inlink->h, vsub);
200  s->height[0] = s->height[3] = inlink->h;
201  s->width[1] = s->width[2] = AV_CEIL_RSHIFT(inlink->w, hsub);
202  s->width[0] = s->width[3] = inlink->w;
203 
204  s->depth = desc->comp[0].depth;
205  s->max = (1 << s->depth) - 1;
206  s->fill = FFMIN(s->fill, s->max);
207 
208  if (s->depth == 8) {
209  s->maskfun = maskfun8;
210  s->getsum = getsum8;
211  } else {
212  s->maskfun = maskfun16;
213  s->getsum = getsum16;
214  }
215 
216  s->empty = ff_get_video_buffer(inlink, inlink->w, inlink->h);
217  if (!s->empty)
218  return AVERROR(ENOMEM);
219 
220  if (s->depth == 8) {
221  for (int p = 0; p < s->nb_planes; p++) {
222  uint8_t *dst = s->empty->data[p];
223 
224  for (int y = 0; y < s->height[p]; y++) {
225  memset(dst, s->fill, s->width[p]);
226  dst += s->empty->linesize[p];
227  }
228  }
229  } else {
230  for (int p = 0; p < s->nb_planes; p++) {
231  uint16_t *dst = (uint16_t *)s->empty->data[p];
232 
233  for (int y = 0; y < s->height[p]; y++) {
234  for (int x = 0; x < s->width[p]; x++)
235  dst[x] = s->fill;
236  dst += s->empty->linesize[p] / 2;
237  }
238  }
239  }
240 
241  s->max_sum = 0;
242  for (int p = 0; p < s->nb_planes; p++) {
243  if (!((1 << p) & s->planes))
244  continue;
245  s->max_sum += (uint64_t)s->sum * s->width[p] * s->height[p];
246  }
247 
248  return 0;
249 }
250 
251 static const AVFilterPad maskfun_inputs[] = {
252  {
253  .name = "default",
254  .type = AVMEDIA_TYPE_VIDEO,
255  .filter_frame = filter_frame,
256  .config_props = config_input,
257  .needs_writable = 1,
258  },
259  { NULL }
260 };
261 
262 static const AVFilterPad maskfun_outputs[] = {
263  {
264  .name = "default",
265  .type = AVMEDIA_TYPE_VIDEO,
266  },
267  { NULL }
268 };
269 
271  .name = "maskfun",
272  .description = NULL_IF_CONFIG_SMALL("Create Mask."),
273  .priv_size = sizeof(MaskFunContext),
275  .inputs = maskfun_inputs,
276  .outputs = maskfun_outputs,
277  .priv_class = &maskfun_class,
279 };
#define NULL
Definition: coverity.c:32
#define AV_PIX_FMT_YUVA422P16
Definition: pixfmt.h:430
#define AV_PIX_FMT_YUVA422P9
Definition: pixfmt.h:422
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2522
This structure describes decoded (raw) audio or video data.
Definition: frame.h:295
AVOption.
Definition: opt.h:246
#define AV_PIX_FMT_YUVA420P10
Definition: pixfmt.h:424
#define AV_PIX_FMT_YUV444P14
Definition: pixfmt.h:397
#define AV_PIX_FMT_GBRAP10
Definition: pixfmt.h:407
#define AV_PIX_FMT_YUVA422P10
Definition: pixfmt.h:425
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition: pixfmt.h:71
misc image utilities
int av_pix_fmt_count_planes(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2562
Main libavfilter public API header.
const char * desc
Definition: nvenc.c:68
int(* maskfun)(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
Definition: vf_maskfun.c:46
planar GBR 4:4:4 24bpp
Definition: pixfmt.h:168
#define AV_PIX_FMT_GBRP10
Definition: pixfmt.h:403
#define AV_PIX_FMT_GRAY9
Definition: pixfmt.h:367
#define AV_PIX_FMT_YUV420P12
Definition: pixfmt.h:391
AVFrame * ff_get_video_buffer(AVFilterLink *link, int w, int h)
Request a picture buffer with a specific set of permissions.
Definition: video.c:99
uint8_t log2_chroma_w
Amount to shift the luma width right to find the chroma width.
Definition: pixdesc.h:92
AVFilterFormats * ff_make_format_list(const int *fmts)
Create a list of supported formats.
Definition: formats.c:283
#define AV_PIX_FMT_GRAY10
Definition: pixfmt.h:368
AVFrame * empty
Definition: vf_maskfun.c:44
#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:125
const char * name
Pad name.
Definition: internal.h:60
#define AV_PIX_FMT_GRAY12
Definition: pixfmt.h:369
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1080
static const AVFilterPad maskfun_inputs[]
Definition: vf_maskfun.c:251
planar YUV 4:2:0, 20bpp, (1 Cr & Cb sample per 2x2 Y & A samples)
Definition: pixfmt.h:101
AVComponentDescriptor comp[4]
Parameters that describe how pixels are packed.
Definition: pixdesc.h:117
uint8_t
int width[4]
Definition: vf_maskfun.c:38
AVOptions.
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:388
#define AV_PIX_FMT_YUVA420P9
Definition: pixfmt.h:421
#define AV_PIX_FMT_GBRP9
Definition: pixfmt.h:402
static AVFrame * frame
planar YUV 4:4:0 full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV440P and setting color_range...
Definition: pixfmt.h:100
planar YUV 4:2:2, 16bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV422P and setting col...
Definition: pixfmt.h:79
#define AV_PIX_FMT_YUV444P16
Definition: pixfmt.h:400
#define AV_PIX_FMT_YUV422P12
Definition: pixfmt.h:392
#define AV_PIX_FMT_YUVA420P16
Definition: pixfmt.h:429
A filter pad used for either input or output.
Definition: internal.h:54
uint64_t max_sum
Definition: vf_maskfun.c:42
planar YUV 4:2:2 24bpp, (1 Cr & Cb sample per 2x1 Y & A samples)
Definition: pixfmt.h:176
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:568
uint8_t log2_chroma_h
Amount to shift the luma height right to find the chroma height.
Definition: pixdesc.h:101
int height[4]
Definition: vf_maskfun.c:38
#define AVERROR(e)
Definition: error.h:43
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:202
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:186
void * priv
private data for use by the filter
Definition: avfilter.h:353
#define AVFILTER_FLAG_SLICE_THREADS
The filter supports multithreading by splitting frames into multiple parts and processing them concur...
Definition: avfilter.h:116
#define AV_PIX_FMT_YUVA444P16
Definition: pixfmt.h:431
const char * arg
Definition: jacosubdec.c:66
#define AV_PIX_FMT_GBRAP12
Definition: pixfmt.h:408
#define AV_PIX_FMT_YUV444P10
Definition: pixfmt.h:390
#define AV_PIX_FMT_GBRAP16
Definition: pixfmt.h:409
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition: pixfmt.h:70
static int query_formats(AVFilterContext *ctx)
Definition: vf_maskfun.c:63
#define AV_PIX_FMT_YUV422P9
Definition: pixfmt.h:385
int linesize[4]
Definition: vf_maskfun.c:37
#define AV_PIX_FMT_GBRP16
Definition: pixfmt.h:406
int ff_filter_get_nb_threads(AVFilterContext *ctx)
Get number of threads for current filter instance.
Definition: avfilter.c:802
#define AV_PIX_FMT_GRAY16
Definition: pixfmt.h:371
#define FFMIN(a, b)
Definition: common.h:96
planar YUV 4:2:0, 12bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV420P and setting col...
Definition: pixfmt.h:78
AVFormatContext * ctx
Definition: movenc.c:48
AVFilter ff_vf_maskfun
Definition: vf_maskfun.c:270
#define s(width, name)
Definition: cbs_vp9.c:257
#define AV_PIX_FMT_YUVA444P10
Definition: pixfmt.h:426
static const AVFilterPad inputs[]
Definition: af_acontrast.c:193
#define AV_PIX_FMT_YUV444P9
Definition: pixfmt.h:386
#define AV_PIX_FMT_GBRP14
Definition: pixfmt.h:405
AVFrame * av_frame_clone(const AVFrame *src)
Create a new frame that references the same data as src.
Definition: frame.c:540
static const AVFilterPad outputs[]
Definition: af_acontrast.c:203
#define AV_PIX_FMT_YUV420P16
Definition: pixfmt.h:398
#define AV_PIX_FMT_YUV420P14
Definition: pixfmt.h:395
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:326
planar YUV 4:4:4 32bpp, (1 Cr & Cb sample per 1x1 Y & A samples)
Definition: pixfmt.h:177
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:81
#define AV_PIX_FMT_GRAY14
Definition: pixfmt.h:370
#define VF
Definition: vf_maskfun.c:50
#define AV_PIX_FMT_YUV420P10
Definition: pixfmt.h:387
planar YUV 4:1:0, 9bpp, (1 Cr & Cb sample per 4x4 Y samples)
Definition: pixfmt.h:72
Describe the class of an AVClass context structure.
Definition: log.h:67
Filter definition.
Definition: avfilter.h:144
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
#define GETSUM(name, type, div)
Definition: vf_maskfun.c:114
#define MASKFUN(name, type, div)
Definition: vf_maskfun.c:145
AVFILTER_DEFINE_CLASS(maskfun)
const char * name
Filter name.
Definition: avfilter.h:148
#define AV_PIX_FMT_YUV440P12
Definition: pixfmt.h:393
#define OFFSET(x)
Definition: vf_maskfun.c:49
#define AV_PIX_FMT_YUV420P9
Definition: pixfmt.h:384
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:350
static enum AVPixelFormat pix_fmts[]
Definition: libkvazaar.c:275
#define AV_PIX_FMT_YUV422P14
Definition: pixfmt.h:396
#define AV_PIX_FMT_GBRP12
Definition: pixfmt.h:404
#define flags(name, subs,...)
Definition: cbs_av1.c:561
AVFilterInternal * internal
An opaque struct for libavfilter internal use.
Definition: avfilter.h:378
#define AV_PIX_FMT_YUV422P10
Definition: pixfmt.h:388
#define AV_PIX_FMT_YUV444P12
Definition: pixfmt.h:394
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:309
static int config_input(AVFilterLink *inlink)
Definition: vf_maskfun.c:185
int
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:66
Y , 8bpp.
Definition: pixfmt.h:74
planar GBRA 4:4:4:4 32bpp
Definition: pixfmt.h:215
#define AV_PIX_FMT_YUVA444P9
Definition: pixfmt.h:423
planar YUV 4:4:4, 24bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV444P and setting col...
Definition: pixfmt.h:80
planar YUV 4:1:1, 12bpp, (1 Cr & Cb sample per 4x1 Y samples)
Definition: pixfmt.h:73
avfilter_execute_func * execute
Definition: internal.h:155
int(* getsum)(AVFilterContext *ctx, AVFrame *out)
Definition: vf_maskfun.c:45
static const AVOption maskfun_options[]
Definition: vf_maskfun.c:52
static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
Definition: vf_maskfun.c:89
planar YUV 4:1:1, 12bpp, (1 Cr & Cb sample per 4x1 Y samples) full scale (JPEG), deprecated in favor ...
Definition: pixfmt.h:258
An instance of a filter.
Definition: avfilter.h:338
FILE * out
Definition: movenc.c:54
planar YUV 4:4:0 (1 Cr & Cb sample per 1x2 Y samples)
Definition: pixfmt.h:99
internal API functions
int depth
Number of bits in the component.
Definition: pixdesc.h:58
AVPixelFormat
Pixel format.
Definition: pixfmt.h:64
static const AVFilterPad maskfun_outputs[]
Definition: vf_maskfun.c:262
#define AV_PIX_FMT_YUV422P16
Definition: pixfmt.h:399
for(j=16;j >0;--j)
#define AV_CEIL_RSHIFT(a, b)
Definition: common.h:58