FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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 (alpha) {
132  frame->data[1][frame->linesize[1] * y + x] = 128;
133  frame->data[2][frame->linesize[2] * y + x] = 128;
134  }
135  }
136  }
137 
138  return 0;
139 }
140 
142 {
143  AVFilterContext *avctx = link->dst;
144  ChromakeyContext *ctx = avctx->priv;
145  int res;
146 
147  if (res = avctx->internal->execute(avctx, ctx->do_slice, frame, NULL, FFMIN(frame->height, ff_filter_get_nb_threads(avctx))))
148  return res;
149 
150  return ff_filter_frame(avctx->outputs[0], frame);
151 }
152 
153 #define FIXNUM(x) lrint((x) * (1 << 10))
154 #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)
155 #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)
156 
158 {
159  ChromakeyContext *ctx = avctx->priv;
160 
161  if (ctx->is_yuv) {
162  ctx->chromakey_uv[0] = ctx->chromakey_rgba[1];
163  ctx->chromakey_uv[1] = ctx->chromakey_rgba[2];
164  } else {
165  ctx->chromakey_uv[0] = RGB_TO_U(ctx->chromakey_rgba);
166  ctx->chromakey_uv[1] = RGB_TO_V(ctx->chromakey_rgba);
167  }
168 
169  if (!strcmp(avctx->filter->name, "chromakey")) {
171  } else {
173  }
174 
175  return 0;
176 }
177 
179 {
180  static const enum AVPixelFormat pixel_fmts[] = {
185  };
186 
187  static const enum AVPixelFormat hold_pixel_fmts[] = {
195  };
196 
198 
199  formats = ff_make_format_list(!strcmp(avctx->filter->name, "chromahold") ? hold_pixel_fmts : pixel_fmts);
200  if (!formats)
201  return AVERROR(ENOMEM);
202 
203  return ff_set_common_formats(avctx, formats);
204 }
205 
206 static av_cold int config_input(AVFilterLink *inlink)
207 {
208  AVFilterContext *avctx = inlink->dst;
209  ChromakeyContext *ctx = avctx->priv;
211 
212  ctx->hsub_log2 = desc->log2_chroma_w;
213  ctx->vsub_log2 = desc->log2_chroma_h;
214 
215  return 0;
216 }
217 
218 static const AVFilterPad chromakey_inputs[] = {
219  {
220  .name = "default",
221  .type = AVMEDIA_TYPE_VIDEO,
222  .needs_writable = 1,
223  .filter_frame = filter_frame,
224  .config_props = config_input,
225  },
226  { NULL }
227 };
228 
229 static const AVFilterPad chromakey_outputs[] = {
230  {
231  .name = "default",
232  .type = AVMEDIA_TYPE_VIDEO,
233  },
234  { NULL }
235 };
236 
237 #define OFFSET(x) offsetof(ChromakeyContext, x)
238 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
239 
240 static const AVOption chromakey_options[] = {
241  { "color", "set the chromakey key color", OFFSET(chromakey_rgba), AV_OPT_TYPE_COLOR, { .str = "black" }, CHAR_MIN, CHAR_MAX, FLAGS },
242  { "similarity", "set the chromakey similarity value", OFFSET(similarity), AV_OPT_TYPE_FLOAT, { .dbl = 0.01 }, 0.01, 1.0, FLAGS },
243  { "blend", "set the chromakey key blend value", OFFSET(blend), AV_OPT_TYPE_FLOAT, { .dbl = 0.0 }, 0.0, 1.0, FLAGS },
244  { "yuv", "color parameter is in yuv instead of rgb", OFFSET(is_yuv), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, FLAGS },
245  { NULL }
246 };
247 
248 AVFILTER_DEFINE_CLASS(chromakey);
249 
251  .name = "chromakey",
252  .description = NULL_IF_CONFIG_SMALL("Turns a certain color into transparency. Operates on YUV colors."),
253  .priv_size = sizeof(ChromakeyContext),
254  .priv_class = &chromakey_class,
257  .inputs = chromakey_inputs,
258  .outputs = chromakey_outputs,
260 };
261 
262 static const AVOption chromahold_options[] = {
263  { "color", "set the chromahold key color", OFFSET(chromakey_rgba), AV_OPT_TYPE_COLOR, { .str = "black" }, CHAR_MIN, CHAR_MAX, FLAGS },
264  { "similarity", "set the chromahold similarity value", OFFSET(similarity), AV_OPT_TYPE_FLOAT, { .dbl = 0.01 }, 0.01, 1.0, FLAGS },
265  { "yuv", "color parameter is in yuv instead of rgb", OFFSET(is_yuv), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, FLAGS },
266  { NULL }
267 };
268 
269 static const AVFilterPad chromahold_inputs[] = {
270  {
271  .name = "default",
272  .type = AVMEDIA_TYPE_VIDEO,
273  .needs_writable = 1,
274  .filter_frame = filter_frame,
275  .config_props = config_input,
276  },
277  { NULL }
278 };
279 
280 static const AVFilterPad chromahold_outputs[] = {
281  {
282  .name = "default",
283  .type = AVMEDIA_TYPE_VIDEO,
284  },
285  { NULL }
286 };
287 
288 AVFILTER_DEFINE_CLASS(chromahold);
289 
291  .name = "chromahold",
292  .description = NULL_IF_CONFIG_SMALL("Turns a certain color range into gray."),
293  .priv_size = sizeof(ChromakeyContext),
294  .priv_class = &chromahold_class,
297  .inputs = chromahold_inputs,
298  .outputs = chromahold_outputs,
300 };
#define NULL
Definition: coverity.c:32
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2498
This structure describes decoded (raw) audio or video data.
Definition: frame.h:268
AVOption.
Definition: opt.h:246
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition: pixfmt.h:71
misc image utilities
Main libavfilter public API header.
const char * desc
Definition: nvenc.c:68
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
AVFILTER_DEFINE_CLASS(chromakey)
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
static const AVOption chromahold_options[]
Definition: vf_chromakey.c:262
#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
uint8_t chromakey_uv[2]
Definition: vf_chromakey.c:32
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1080
planar YUV 4:2:0, 20bpp, (1 Cr & Cb sample per 2x2 Y & A samples)
Definition: pixfmt.h:101
uint8_t
#define av_cold
Definition: attributes.h:82
AVOptions.
AVFilter ff_vf_chromahold
Definition: vf_chromakey.c:290
static const AVFilterPad chromahold_inputs[]
Definition: vf_chromakey.c:269
static const AVFilterPad chromakey_outputs[]
Definition: vf_chromakey.c:229
#define u(width, name, range_min, range_max)
Definition: cbs_h2645.c:253
static AVFrame * frame
static av_cold int initialize_chromakey(AVFilterContext *avctx)
Definition: vf_chromakey.c:157
A filter pad used for either input or output.
Definition: internal.h:54
static const AVOption chromakey_options[]
Definition: vf_chromakey.c:240
planar YUV 4:2:2 24bpp, (1 Cr & Cb sample per 2x1 Y & A samples)
Definition: pixfmt.h:176
int width
Definition: frame.h:326
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
static uint8_t do_chromakey_pixel(ChromakeyContext *ctx, uint8_t u[9], uint8_t v[9])
Definition: vf_chromakey.c:46
#define AVERROR(e)
Definition: error.h:43
int(* do_slice)(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
Definition: vf_chromakey.c:42
#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
const char * arg
Definition: jacosubdec.c:66
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition: pixfmt.h:70
static const AVFilterPad chromahold_outputs[]
Definition: vf_chromakey.c:280
int ff_filter_get_nb_threads(AVFilterContext *ctx)
Get number of threads for current filter instance.
Definition: avfilter.c:802
#define FFMIN(a, b)
Definition: common.h:96
AVFilter ff_vf_chromakey
Definition: vf_chromakey.c:250
uint8_t chromakey_rgba[4]
Definition: vf_chromakey.c:31
AVFormatContext * ctx
Definition: movenc.c:48
static int filter_frame(AVFilterLink *link, AVFrame *frame)
Definition: vf_chromakey.c:141
static const AVFilterPad inputs[]
Definition: af_acontrast.c:193
static const AVFilterPad outputs[]
Definition: af_acontrast.c:203
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:299
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
static const int16_t alpha[]
Definition: ilbcdata.h:55
Describe the class of an AVClass context structure.
Definition: log.h:67
Filter definition.
Definition: avfilter.h:144
#define OFFSET(x)
Definition: vf_chromakey.c:237
const char * name
Filter name.
Definition: avfilter.h:148
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:350
#define RGB_TO_U(rgb)
Definition: vf_chromakey.c:154
#define flags(name, subs,...)
Definition: cbs_av1.c:606
AVFilterInternal * internal
An opaque struct for libavfilter internal use.
Definition: avfilter.h:378
#define FLAGS
Definition: vf_chromakey.c:238
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:282
static av_cold int query_formats(AVFilterContext *avctx)
Definition: vf_chromakey.c:178
static const AVFilterPad chromakey_inputs[]
Definition: vf_chromakey.c:218
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
int
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:66
avfilter_execute_func * execute
Definition: internal.h:155
static av_cold int config_input(AVFilterLink *inlink)
Definition: vf_chromakey.c:206
static int do_chromakey_slice(AVFilterContext *avctx, void *arg, int jobnr, int nb_jobs)
Definition: vf_chromakey.c:79
static int slice_end(AVCodecContext *avctx, AVFrame *pict)
Handle slice ends.
Definition: mpeg12dec.c:2029
#define RGB_TO_V(rgb)
Definition: vf_chromakey.c:155
static av_always_inline int diff(const uint32_t a, const uint32_t b)
A list of supported formats for one end of a filter link.
Definition: formats.h:64
An instance of a filter.
Definition: avfilter.h:338
int height
Definition: frame.h:326
#define av_always_inline
Definition: attributes.h:39
static int do_chromahold_slice(AVFilterContext *avctx, void *arg, int jobnr, int nb_jobs)
Definition: vf_chromakey.c:109
formats
Definition: signature.h:48
internal API functions
AVPixelFormat
Pixel format.
Definition: pixfmt.h:64
const AVFilter * filter
the AVFilter of which this is an instance
Definition: avfilter.h:341