FFmpeg
vf_chromakey.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015 Timo Rothenpieler <timo@rothenpieler.org>
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 ChromakeyContext {
29  const AVClass *class;
30 
33 
34  float similarity;
35  float blend;
36 
37  int is_yuv;
38 
39  int hsub_log2;
40  int vsub_log2;
41 
43  int jobnr, int nb_jobs);
45 
47 {
48  double diff = 0.0;
49  int du, dv, i;
50 
51  for (i = 0; i < 9; ++i) {
52  du = (int)u[i] - ctx->chromakey_uv[0];
53  dv = (int)v[i] - ctx->chromakey_uv[1];
54 
55  diff += sqrt((du * du + dv * dv) / (255.0 * 255.0));
56  }
57 
58  diff /= 9.0;
59 
60  if (ctx->blend > 0.0001) {
61  return av_clipd((diff - ctx->similarity) / ctx->blend, 0.0, 1.0) * 255.0;
62  } else {
63  return (diff > ctx->similarity) ? 255 : 0;
64  }
65 }
66 
67 static av_always_inline void get_pixel_uv(AVFrame *frame, int hsub_log2, int vsub_log2, int x, int y, uint8_t *u, uint8_t *v)
68 {
69  if (x < 0 || x >= frame->width || y < 0 || y >= frame->height)
70  return;
71 
72  x >>= hsub_log2;
73  y >>= vsub_log2;
74 
75  *u = frame->data[1][frame->linesize[1] * y + x];
76  *v = frame->data[2][frame->linesize[2] * y + x];
77 }
78 
79 static int do_chromakey_slice(AVFilterContext *avctx, void *arg, int jobnr, int nb_jobs)
80 {
81  AVFrame *frame = arg;
82 
83  const int slice_start = (frame->height * jobnr) / nb_jobs;
84  const int slice_end = (frame->height * (jobnr + 1)) / nb_jobs;
85 
86  ChromakeyContext *ctx = avctx->priv;
87 
88  int x, y, xo, yo;
89  uint8_t u[9], v[9];
90 
91  memset(u, ctx->chromakey_uv[0], sizeof(u));
92  memset(v, ctx->chromakey_uv[1], sizeof(v));
93 
94  for (y = slice_start; y < slice_end; ++y) {
95  for (x = 0; x < frame->width; ++x) {
96  for (yo = 0; yo < 3; ++yo) {
97  for (xo = 0; xo < 3; ++xo) {
98  get_pixel_uv(frame, ctx->hsub_log2, ctx->vsub_log2, x + xo - 1, y + yo - 1, &u[yo * 3 + xo], &v[yo * 3 + xo]);
99  }
100  }
101 
102  frame->data[3][frame->linesize[3] * y + x] = do_chromakey_pixel(ctx, u, v);
103  }
104  }
105 
106  return 0;
107 }
108 
109 static int do_chromahold_slice(AVFilterContext *avctx, void *arg, int jobnr, int nb_jobs)
110 {
111  ChromakeyContext *ctx = avctx->priv;
112  AVFrame *frame = arg;
113  const int slice_start = ((frame->height >> ctx->vsub_log2) * jobnr) / nb_jobs;
114  const int slice_end = ((frame->height >> ctx->vsub_log2) * (jobnr + 1)) / nb_jobs;
115 
116  int x, y, alpha;
117 
118  for (y = slice_start; y < slice_end; ++y) {
119  for (x = 0; x < frame->width >> ctx->hsub_log2; ++x) {
120  int u = frame->data[1][frame->linesize[1] * y + x];
121  int v = frame->data[2][frame->linesize[2] * y + x];
122  double diff;
123  int du, dv;
124 
125  du = u - ctx->chromakey_uv[0];
126  dv = v - ctx->chromakey_uv[1];
127 
128  diff = sqrt((du * du + dv * dv) / (255.0 * 255.0));
129 
130  alpha = diff > ctx->similarity;
131  if (ctx->blend > 0.0001) {
132  double f = 1. - av_clipd((diff - ctx->similarity) / ctx->blend, 0.0, 1.0);
133 
134  frame->data[1][frame->linesize[1] * y + x] = 128 + (u - 128) * f;
135  frame->data[2][frame->linesize[2] * y + x] = 128 + (v - 128) * f;
136  } else if (alpha) {
137  frame->data[1][frame->linesize[1] * y + x] = 128;
138  frame->data[2][frame->linesize[2] * y + x] = 128;
139  }
140  }
141  }
142 
143  return 0;
144 }
145 
147 {
148  AVFilterContext *avctx = link->dst;
149  ChromakeyContext *ctx = avctx->priv;
150  int res;
151 
152  if (res = avctx->internal->execute(avctx, ctx->do_slice, frame, NULL, FFMIN(frame->height, ff_filter_get_nb_threads(avctx))))
153  return res;
154 
155  return ff_filter_frame(avctx->outputs[0], frame);
156 }
157 
158 #define FIXNUM(x) lrint((x) * (1 << 10))
159 #define RGB_TO_U(rgb) (((- FIXNUM(0.16874) * rgb[0] - FIXNUM(0.33126) * rgb[1] + FIXNUM(0.50000) * rgb[2] + (1 << 9) - 1) >> 10) + 128)
160 #define RGB_TO_V(rgb) ((( FIXNUM(0.50000) * rgb[0] - FIXNUM(0.41869) * rgb[1] - FIXNUM(0.08131) * rgb[2] + (1 << 9) - 1) >> 10) + 128)
161 
163 {
164  ChromakeyContext *ctx = avctx->priv;
165 
166  if (ctx->is_yuv) {
167  ctx->chromakey_uv[0] = ctx->chromakey_rgba[1];
168  ctx->chromakey_uv[1] = ctx->chromakey_rgba[2];
169  } else {
170  ctx->chromakey_uv[0] = RGB_TO_U(ctx->chromakey_rgba);
171  ctx->chromakey_uv[1] = RGB_TO_V(ctx->chromakey_rgba);
172  }
173 
174  if (!strcmp(avctx->filter->name, "chromakey")) {
175  ctx->do_slice = do_chromakey_slice;
176  } else {
177  ctx->do_slice = do_chromahold_slice;
178  }
179 
180  return 0;
181 }
182 
184 {
185  static const enum AVPixelFormat pixel_fmts[] = {
190  };
191 
192  static const enum AVPixelFormat hold_pixel_fmts[] = {
200  };
201 
203 
204  formats = ff_make_format_list(!strcmp(avctx->filter->name, "chromahold") ? hold_pixel_fmts : pixel_fmts);
205  if (!formats)
206  return AVERROR(ENOMEM);
207 
208  return ff_set_common_formats(avctx, formats);
209 }
210 
212 {
213  AVFilterContext *avctx = inlink->dst;
214  ChromakeyContext *ctx = avctx->priv;
216 
217  ctx->hsub_log2 = desc->log2_chroma_w;
218  ctx->vsub_log2 = desc->log2_chroma_h;
219 
220  return 0;
221 }
222 
223 static const AVFilterPad chromakey_inputs[] = {
224  {
225  .name = "default",
226  .type = AVMEDIA_TYPE_VIDEO,
227  .needs_writable = 1,
228  .filter_frame = filter_frame,
229  .config_props = config_input,
230  },
231  { NULL }
232 };
233 
234 static const AVFilterPad chromakey_outputs[] = {
235  {
236  .name = "default",
237  .type = AVMEDIA_TYPE_VIDEO,
238  },
239  { NULL }
240 };
241 
242 #define OFFSET(x) offsetof(ChromakeyContext, x)
243 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
244 
245 static const AVOption chromakey_options[] = {
246  { "color", "set the chromakey key color", OFFSET(chromakey_rgba), AV_OPT_TYPE_COLOR, { .str = "black" }, CHAR_MIN, CHAR_MAX, FLAGS },
247  { "similarity", "set the chromakey similarity value", OFFSET(similarity), AV_OPT_TYPE_FLOAT, { .dbl = 0.01 }, 0.01, 1.0, FLAGS },
248  { "blend", "set the chromakey key blend value", OFFSET(blend), AV_OPT_TYPE_FLOAT, { .dbl = 0.0 }, 0.0, 1.0, FLAGS },
249  { "yuv", "color parameter is in yuv instead of rgb", OFFSET(is_yuv), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, FLAGS },
250  { NULL }
251 };
252 
253 AVFILTER_DEFINE_CLASS(chromakey);
254 
256  .name = "chromakey",
257  .description = NULL_IF_CONFIG_SMALL("Turns a certain color into transparency. Operates on YUV colors."),
258  .priv_size = sizeof(ChromakeyContext),
259  .priv_class = &chromakey_class,
265 };
266 
267 static const AVOption chromahold_options[] = {
268  { "color", "set the chromahold key color", OFFSET(chromakey_rgba), AV_OPT_TYPE_COLOR, { .str = "black" }, CHAR_MIN, CHAR_MAX, FLAGS },
269  { "similarity", "set the chromahold similarity value", OFFSET(similarity), AV_OPT_TYPE_FLOAT, { .dbl = 0.01 }, 0.01, 1.0, FLAGS },
270  { "blend", "set the chromahold blend value", OFFSET(blend), AV_OPT_TYPE_FLOAT, { .dbl = 0.0 }, 0.0, 1.0, FLAGS },
271  { "yuv", "color parameter is in yuv instead of rgb", OFFSET(is_yuv), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, FLAGS },
272  { NULL }
273 };
274 
275 static const AVFilterPad chromahold_inputs[] = {
276  {
277  .name = "default",
278  .type = AVMEDIA_TYPE_VIDEO,
279  .needs_writable = 1,
280  .filter_frame = filter_frame,
281  .config_props = config_input,
282  },
283  { NULL }
284 };
285 
286 static const AVFilterPad chromahold_outputs[] = {
287  {
288  .name = "default",
289  .type = AVMEDIA_TYPE_VIDEO,
290  },
291  { NULL }
292 };
293 
294 AVFILTER_DEFINE_CLASS(chromahold);
295 
297  .name = "chromahold",
298  .description = NULL_IF_CONFIG_SMALL("Turns a certain color range into gray."),
299  .priv_size = sizeof(ChromakeyContext),
300  .priv_class = &chromahold_class,
306 };
formats
formats
Definition: signature.h:48
AVPixelFormat
AVPixelFormat
Pixel format.
Definition: pixfmt.h:64
config_input
static av_cold int config_input(AVFilterLink *inlink)
Definition: vf_chromakey.c:211
ChromakeyContext::do_slice
int(* do_slice)(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
Definition: vf_chromakey.c:42
init
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
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
RGB_TO_V
#define RGB_TO_V(rgb)
Definition: vf_chromakey.c:160
ff_make_format_list
AVFilterFormats * ff_make_format_list(const int *fmts)
Create a list of supported formats.
Definition: formats.c:283
u
#define u(width, name, range_min, range_max)
Definition: cbs_h2645.c:252
ff_filter_frame
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1080
av_pix_fmt_desc_get
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2522
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
chromakey_outputs
static const AVFilterPad chromakey_outputs[]
Definition: vf_chromakey.c:234
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:295
AVOption
AVOption.
Definition: opt.h:246
chromahold_inputs
static const AVFilterPad chromahold_inputs[]
Definition: vf_chromakey.c:275
AVFilter::name
const char * name
Filter name.
Definition: avfilter.h:148
video.h
do_chromakey_pixel
static uint8_t do_chromakey_pixel(ChromakeyContext *ctx, uint8_t u[9], uint8_t v[9])
Definition: vf_chromakey.c:46
AVFilterFormats
A list of supported formats for one end of a filter link.
Definition: formats.h:64
formats.h
chromahold_options
static const AVOption chromahold_options[]
Definition: vf_chromakey.c:267
AVFilterContext::priv
void * priv
private data for use by the filter
Definition: avfilter.h:353
ChromakeyContext::chromakey_uv
uint8_t chromakey_uv[2]
Definition: vf_chromakey.c:32
AVFilterPad
A filter pad used for either input or output.
Definition: internal.h:54
AVFILTER_DEFINE_CLASS
AVFILTER_DEFINE_CLASS(chromakey)
av_cold
#define av_cold
Definition: attributes.h:84
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:568
ChromakeyContext::is_yuv
int is_yuv
Definition: vf_chromakey.c:37
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
ChromakeyContext
Definition: vf_chromakey.c:28
initialize_chromakey
static av_cold int initialize_chromakey(AVFilterContext *avctx)
Definition: vf_chromakey.c:162
slice_end
static int slice_end(AVCodecContext *avctx, AVFrame *pict)
Handle slice ends.
Definition: mpeg12dec.c:2026
outputs
static const AVFilterPad outputs[]
Definition: af_acontrast.c:203
ctx
AVFormatContext * ctx
Definition: movenc.c:48
AV_PIX_FMT_YUV420P
@ AV_PIX_FMT_YUV420P
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:66
f
#define f(width, name)
Definition: cbs_vp9.c:255
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
ff_vf_chromahold
AVFilter ff_vf_chromahold
Definition: vf_chromakey.c:296
arg
const char * arg
Definition: jacosubdec.c:66
chromakey_options
static const AVOption chromakey_options[]
Definition: vf_chromakey.c:245
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:67
NULL
#define NULL
Definition: coverity.c:32
ff_vf_chromakey
AVFilter ff_vf_chromakey
Definition: vf_chromakey.c:255
AV_OPT_TYPE_COLOR
@ AV_OPT_TYPE_COLOR
Definition: opt.h:238
ChromakeyContext::blend
float blend
Definition: vf_chromakey.c:35
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
chromahold_outputs
static const AVFilterPad chromahold_outputs[]
Definition: vf_chromakey.c:286
filter_frame
static int filter_frame(AVFilterLink *link, AVFrame *frame)
Definition: vf_chromakey.c:146
desc
const char * desc
Definition: nvenc.c:68
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:188
ChromakeyContext::chromakey_rgba
uint8_t chromakey_rgba[4]
Definition: vf_chromakey.c:31
RGB_TO_U
#define RGB_TO_U(rgb)
Definition: vf_chromakey.c:159
chromakey_inputs
static const AVFilterPad chromakey_inputs[]
Definition: vf_chromakey.c:223
FFMIN
#define FFMIN(a, b)
Definition: common.h:96
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
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:125
AV_OPT_TYPE_FLOAT
@ AV_OPT_TYPE_FLOAT
Definition: opt.h:226
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:259
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
av_always_inline
#define av_always_inline
Definition: attributes.h:43
uint8_t
uint8_t
Definition: audio_convert.c:194
AVFilterPad::name
const char * name
Pad name.
Definition: internal.h:60
get_pixel_uv
static av_always_inline void get_pixel_uv(AVFrame *frame, int hsub_log2, int vsub_log2, int x, int y, uint8_t *u, uint8_t *v)
Definition: vf_chromakey.c:67
AVFilter
Filter definition.
Definition: avfilter.h:144
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
do_chromakey_slice
static int do_chromakey_slice(AVFilterContext *avctx, void *arg, int jobnr, int nb_jobs)
Definition: vf_chromakey.c:79
AVFilterInternal::execute
avfilter_execute_func * execute
Definition: internal.h:155
AV_PIX_FMT_NONE
@ AV_PIX_FMT_NONE
Definition: pixfmt.h:65
avfilter.h
AVFilterContext::internal
AVFilterInternal * internal
An opaque struct for libavfilter internal use.
Definition: avfilter.h:378
AV_PIX_FMT_YUV444P
@ AV_PIX_FMT_YUV444P
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition: pixfmt.h:71
AVFilterContext
An instance of a filter.
Definition: avfilter.h:338
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:116
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
AV_PIX_FMT_YUV422P
@ AV_PIX_FMT_YUV422P
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition: pixfmt.h:70
OFFSET
#define OFFSET(x)
Definition: vf_chromakey.c:242
AVPixFmtDescriptor
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:81
diff
static av_always_inline int diff(const uint32_t a, const uint32_t b)
Definition: vf_palettegen.c:136
alpha
static const int16_t alpha[]
Definition: ilbcdata.h:55
AV_OPT_TYPE_BOOL
@ AV_OPT_TYPE_BOOL
Definition: opt.h:240
do_chromahold_slice
static int do_chromahold_slice(AVFilterContext *avctx, void *arg, int jobnr, int nb_jobs)
Definition: vf_chromakey.c:109
FLAGS
#define FLAGS
Definition: vf_chromakey.c:243
ChromakeyContext::vsub_log2
int vsub_log2
Definition: vf_chromakey.c:40
imgutils.h
flags
#define flags(name, subs,...)
Definition: cbs_av1.c:565
query_formats
static av_cold int query_formats(AVFilterContext *avctx)
Definition: vf_chromakey.c:183
ChromakeyContext::similarity
float similarity
Definition: vf_chromakey.c:34
AVFilterContext::filter
const AVFilter * filter
the AVFilter of which this is an instance
Definition: avfilter.h:341
ChromakeyContext::hsub_log2
int hsub_log2
Definition: vf_chromakey.c:39
int
int
Definition: ffmpeg_filter.c:191
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
AVFilterContext::outputs
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:350