FFmpeg
vf_lumakey.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017 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/opt.h"
22 #include "libavutil/imgutils.h"
23 #include "avfilter.h"
24 #include "formats.h"
25 #include "internal.h"
26 #include "video.h"
27 
28 typedef struct LumakeyContext {
29  const AVClass *class;
30 
31  double threshold;
32  double tolerance;
33  double softness;
34 
35  int white;
36  int black;
37  int so;
38  int max;
39 
40  int (*do_lumakey_slice)(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs);
42 
43 static int do_lumakey_slice8(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
44 {
45  LumakeyContext *s = ctx->priv;
46  AVFrame *frame = arg;
47  const int slice_start = (frame->height * jobnr) / nb_jobs;
48  const int slice_end = (frame->height * (jobnr + 1)) / nb_jobs;
49  uint8_t *alpha = frame->data[3] + slice_start * frame->linesize[3];
50  const uint8_t *luma = frame->data[0] + slice_start * frame->linesize[0];
51  const int so = s->so;
52  const int w = s->white;
53  const int b = s->black;
54  int x, y;
55 
56  for (y = slice_start; y < slice_end; y++) {
57  for (x = 0; x < frame->width; x++) {
58  if (luma[x] >= b && luma[x] <= w) {
59  alpha[x] = 0;
60  } else if (luma[x] > b - so && luma[x] < w + so) {
61  if (luma[x] < b) {
62  alpha[x] = 255 - (luma[x] - b + so) * 255 / so;
63  } else {
64  alpha[x] = (luma[x] - w) * 255 / so;
65  }
66  }
67  }
68  luma += frame->linesize[0];
69  alpha += frame->linesize[3];
70  }
71 
72  return 0;
73 }
74 
75 static int do_lumakey_slice16(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
76 {
77  LumakeyContext *s = ctx->priv;
78  AVFrame *frame = arg;
79  const int slice_start = (frame->height * jobnr) / nb_jobs;
80  const int slice_end = (frame->height * (jobnr + 1)) / nb_jobs;
81  uint16_t *alpha = (uint16_t *)(frame->data[3] + slice_start * frame->linesize[3]);
82  const uint16_t *luma = (const uint16_t *)(frame->data[0] + slice_start * frame->linesize[0]);
83  const int so = s->so;
84  const int w = s->white;
85  const int b = s->black;
86  const int m = s->max;
87  int x, y;
88 
89  for (y = slice_start; y < slice_end; y++) {
90  for (x = 0; x < frame->width; x++) {
91  if (luma[x] >= b && luma[x] <= w) {
92  alpha[x] = 0;
93  } else if (luma[x] > b - so && luma[x] < w + so) {
94  if (luma[x] < b) {
95  alpha[x] = m - (luma[x] - b + so) * m / so;
96  } else {
97  alpha[x] = (luma[x] - w) * m / so;
98  }
99  }
100  }
101  luma += frame->linesize[0] / 2;
102  alpha += frame->linesize[3] / 2;
103  }
104 
105  return 0;
106 }
107 
109 {
111  AVFilterContext *ctx = inlink->dst;
112  LumakeyContext *s = ctx->priv;
113  int depth;
114 
115  depth = desc->comp[0].depth;
116  if (depth == 8) {
117  s->white = av_clip_uint8((s->threshold + s->tolerance) * 255);
118  s->black = av_clip_uint8((s->threshold - s->tolerance) * 255);
119  s->do_lumakey_slice = do_lumakey_slice8;
120  s->so = s->softness * 255;
121  } else {
122  s->max = (1 << depth) - 1;
123  s->white = av_clip((s->threshold + s->tolerance) * s->max, 0, s->max);
124  s->black = av_clip((s->threshold - s->tolerance) * s->max, 0, s->max);
125  s->do_lumakey_slice = do_lumakey_slice16;
126  s->so = s->softness * s->max;
127  }
128 
129  return 0;
130 }
131 
133 {
134  AVFilterContext *ctx = link->dst;
135  LumakeyContext *s = ctx->priv;
136  int ret;
137 
139  return ret;
140 
141  if (ret = ctx->internal->execute(ctx, s->do_lumakey_slice, frame, NULL, FFMIN(frame->height, ff_filter_get_nb_threads(ctx))))
142  return ret;
143 
144  return ff_filter_frame(ctx->outputs[0], frame);
145 }
146 
148 {
149  static const enum AVPixelFormat pixel_fmts[] = {
156  };
158 
159  formats = ff_make_format_list(pixel_fmts);
160  if (!formats)
161  return AVERROR(ENOMEM);
162 
164 }
165 
166 static int process_command(AVFilterContext *ctx, const char *cmd, const char *args,
167  char *res, int res_len, int flags)
168 {
169  int ret;
170 
171  ret = ff_filter_process_command(ctx, cmd, args, res, res_len, flags);
172  if (ret < 0)
173  return ret;
174 
175  return config_input(ctx->inputs[0]);
176 }
177 
178 static const AVFilterPad lumakey_inputs[] = {
179  {
180  .name = "default",
181  .type = AVMEDIA_TYPE_VIDEO,
182  .filter_frame = filter_frame,
183  .config_props = config_input,
184  },
185  { NULL }
186 };
187 
188 static const AVFilterPad lumakey_outputs[] = {
189  {
190  .name = "default",
191  .type = AVMEDIA_TYPE_VIDEO,
192  },
193  { NULL }
194 };
195 
196 #define OFFSET(x) offsetof(LumakeyContext, x)
197 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
198 
199 static const AVOption lumakey_options[] = {
200  { "threshold", "set the threshold value", OFFSET(threshold), AV_OPT_TYPE_DOUBLE, {.dbl=0}, 0, 1, FLAGS },
201  { "tolerance", "set the tolerance value", OFFSET(tolerance), AV_OPT_TYPE_DOUBLE, {.dbl=0.01}, 0, 1, FLAGS },
202  { "softness", "set the softness value", OFFSET(softness), AV_OPT_TYPE_DOUBLE, {.dbl=0}, 0, 1, FLAGS },
203  { NULL }
204 };
205 
206 AVFILTER_DEFINE_CLASS(lumakey);
207 
209  .name = "lumakey",
210  .description = NULL_IF_CONFIG_SMALL("Turns a certain luma into transparency."),
211  .priv_size = sizeof(LumakeyContext),
212  .priv_class = &lumakey_class,
218 };
LumakeyContext::max
int max
Definition: vf_lumakey.c:38
formats
formats
Definition: signature.h:48
AV_PIX_FMT_YUVA422P16
#define AV_PIX_FMT_YUVA422P16
Definition: pixfmt.h:442
AVPixelFormat
AVPixelFormat
Pixel format.
Definition: pixfmt.h:64
av_clip
#define av_clip
Definition: common.h:122
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
opt.h
ff_make_format_list
AVFilterFormats * ff_make_format_list(const int *fmts)
Create a list of supported formats.
Definition: formats.c:286
do_lumakey_slice8
static int do_lumakey_slice8(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
Definition: vf_lumakey.c:43
ff_filter_frame
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1096
av_pix_fmt_desc_get
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2573
LumakeyContext::so
int so
Definition: vf_lumakey.c:37
inlink
The exact code depends on how similar the blocks are and how related they are to the and needs to apply these operations to the correct inlink or outlink if there are several Macros are available to factor that when no extra processing is inlink
Definition: filter_design.txt:212
AV_PIX_FMT_YUVA422P9
#define AV_PIX_FMT_YUVA422P9
Definition: pixfmt.h:434
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:318
av_frame_make_writable
int av_frame_make_writable(AVFrame *frame)
Ensure that the frame data is writable, avoiding data copy if possible.
Definition: frame.c:611
AV_PIX_FMT_YUVA420P16
#define AV_PIX_FMT_YUVA420P16
Definition: pixfmt.h:441
w
uint8_t w
Definition: llviddspenc.c:39
LumakeyContext::softness
double softness
Definition: vf_lumakey.c:33
AV_PIX_FMT_YUVA420P10
#define AV_PIX_FMT_YUVA420P10
Definition: pixfmt.h:436
AVOption
AVOption.
Definition: opt.h:248
b
#define b
Definition: input.c:41
filter_frame
static int filter_frame(AVFilterLink *link, AVFrame *frame)
Definition: vf_lumakey.c:132
LumakeyContext
Definition: vf_lumakey.c:28
AVFilter::name
const char * name
Filter name.
Definition: avfilter.h:149
video.h
AVFormatContext::internal
AVFormatInternal * internal
An opaque field for libavformat internal usage.
Definition: avformat.h:1699
AV_PIX_FMT_YUVA422P10
#define AV_PIX_FMT_YUVA422P10
Definition: pixfmt.h:437
AVFilterFormats
A list of supported formats for one end of a filter link.
Definition: formats.h:65
formats.h
AV_PIX_FMT_YUVA420P9
#define AV_PIX_FMT_YUVA420P9
Definition: pixfmt.h:433
AV_PIX_FMT_YUVA444P16
#define AV_PIX_FMT_YUVA444P16
Definition: pixfmt.h:443
AVFilterPad
A filter pad used for either input or output.
Definition: internal.h:54
av_cold
#define av_cold
Definition: attributes.h:90
ff_set_common_formats
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
s
#define s(width, name)
Definition: cbs_vp9.c:257
AV_PIX_FMT_YUVA420P
@ AV_PIX_FMT_YUVA420P
planar YUV 4:2:0, 20bpp, (1 Cr & Cb sample per 2x2 Y & A samples)
Definition: pixfmt.h:101
AV_OPT_TYPE_DOUBLE
@ AV_OPT_TYPE_DOUBLE
Definition: opt.h:227
slice_end
static int slice_end(AVCodecContext *avctx, AVFrame *pict)
Handle slice ends.
Definition: mpeg12dec.c:2033
lumakey_inputs
static const AVFilterPad lumakey_inputs[]
Definition: vf_lumakey.c:178
outputs
static const AVFilterPad outputs[]
Definition: af_acontrast.c:203
AV_PIX_FMT_YUVA444P12
#define AV_PIX_FMT_YUVA444P12
Definition: pixfmt.h:440
ctx
AVFormatContext * ctx
Definition: movenc.c:48
LumakeyContext::black
int black
Definition: vf_lumakey.c:36
link
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a link
Definition: filter_design.txt:23
arg
const char * arg
Definition: jacosubdec.c:66
LumakeyContext::white
int white
Definition: vf_lumakey.c:35
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:67
NULL
#define NULL
Definition: coverity.c:32
LumakeyContext::threshold
double threshold
Definition: vf_lumakey.c:31
LumakeyContext::do_lumakey_slice
int(* do_lumakey_slice)(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
Definition: vf_lumakey.c:40
inputs
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several inputs
Definition: filter_design.txt:243
OFFSET
#define OFFSET(x)
Definition: vf_lumakey.c:196
lumakey_outputs
static const AVFilterPad lumakey_outputs[]
Definition: vf_lumakey.c:188
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:117
ff_vf_lumakey
AVFilter ff_vf_lumakey
Definition: vf_lumakey.c:208
do_lumakey_slice16
static int do_lumakey_slice16(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
Definition: vf_lumakey.c:75
ff_filter_process_command
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
FFMIN
#define FFMIN(a, b)
Definition: common.h:105
AV_PIX_FMT_YUVA444P
@ 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_YUVA444P10
#define AV_PIX_FMT_YUVA444P10
Definition: pixfmt.h:438
internal.h
AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC
#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
LumakeyContext::tolerance
double tolerance
Definition: vf_lumakey.c:32
ff_filter_get_nb_threads
int ff_filter_get_nb_threads(AVFilterContext *ctx)
Get number of threads for current filter instance.
Definition: avfilter.c:802
uint8_t
uint8_t
Definition: audio_convert.c:194
AVFilterPad::name
const char * name
Pad name.
Definition: internal.h:60
AVFilter
Filter definition.
Definition: avfilter.h:145
ret
ret
Definition: filter_design.txt:187
frame
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return or at least make progress towards producing a frame
Definition: filter_design.txt:264
AV_PIX_FMT_YUVA444P9
#define AV_PIX_FMT_YUVA444P9
Definition: pixfmt.h:435
config_input
static int config_input(AVFilterLink *inlink)
Definition: vf_lumakey.c:108
AV_PIX_FMT_NONE
@ AV_PIX_FMT_NONE
Definition: pixfmt.h:65
AV_PIX_FMT_YUVA422P12
#define AV_PIX_FMT_YUVA422P12
Definition: pixfmt.h:439
avfilter.h
query_formats
static av_cold int query_formats(AVFilterContext *ctx)
Definition: vf_lumakey.c:147
lumakey_options
static const AVOption lumakey_options[]
Definition: vf_lumakey.c:199
av_clip_uint8
#define av_clip_uint8
Definition: common.h:128
AVFilterContext
An instance of a filter.
Definition: avfilter.h:341
AVFILTER_FLAG_SLICE_THREADS
#define AVFILTER_FLAG_SLICE_THREADS
The filter supports multithreading by splitting frames into multiple parts and processing them concur...
Definition: avfilter.h:117
desc
const char * desc
Definition: libsvtav1.c:79
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
AVPixFmtDescriptor
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:81
alpha
static const int16_t alpha[]
Definition: ilbcdata.h:55
process_command
static int process_command(AVFilterContext *ctx, const char *cmd, const char *args, char *res, int res_len, int flags)
Definition: vf_lumakey.c:166
FLAGS
#define FLAGS
Definition: vf_lumakey.c:197
imgutils.h
flags
#define flags(name, subs,...)
Definition: cbs_av1.c:561
AVFILTER_DEFINE_CLASS
AVFILTER_DEFINE_CLASS(lumakey)
int
int
Definition: ffmpeg_filter.c:170
AV_PIX_FMT_YUVA422P
@ AV_PIX_FMT_YUVA422P
planar YUV 4:2:2 24bpp, (1 Cr & Cb sample per 2x1 Y & A samples)
Definition: pixfmt.h:176