FFmpeg
vf_colorkey.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 "config_components.h"
22 
23 #include "libavutil/opt.h"
24 #include "libavutil/imgutils.h"
25 #include "avfilter.h"
26 #include "drawutils.h"
27 #include "formats.h"
28 #include "internal.h"
29 #include "video.h"
30 
31 typedef struct ColorkeyContext {
32  const AVClass *class;
33 
34  /* color offsets rgba */
35  uint8_t co[4];
36 
37  uint8_t colorkey_rgba[4];
38  float similarity;
39  float blend;
40  double scale;
41  int depth;
42  int max;
43 
45  int jobnr, int nb_jobs);
47 
48 static int do_colorkey_pixel(const uint8_t *colorkey_rgba, int r, int g, int b,
49  float similarity, float iblend, int max, double scale)
50 {
51  double dr, dg, db, diff;
52 
53  dr = r * scale - colorkey_rgba[0];
54  dg = g * scale - colorkey_rgba[1];
55  db = b * scale - colorkey_rgba[2];
56 
57  diff = sqrt((dr * dr + dg * dg + db * db) / (255.0 * 255.0 * 3.0));
58 
59  if (iblend < 10000.0) {
60  return av_clipd((diff - similarity) * iblend, 0.0, 1.0) * max;
61  } else {
62  return (diff > similarity) ? max : 0;
63  }
64 }
65 
66 #define COLORKEY_SLICE(name, type) \
67 static int do_colorkey_slice##name(AVFilterContext *avctx, \
68  void *arg, \
69  int jobnr, int nb_jobs) \
70 { \
71  AVFrame *frame = arg; \
72  const int slice_start = (frame->height * jobnr) / nb_jobs; \
73  const int slice_end = (frame->height * (jobnr + 1)) / nb_jobs; \
74  ColorkeyContext *ctx = avctx->priv; \
75  const float similarity = ctx->similarity; \
76  const float iblend = 1.f / ctx->blend; \
77  const uint8_t *colorkey_rgba = ctx->colorkey_rgba; \
78  const uint8_t *co = ctx->co; \
79  const double scale = ctx->scale; \
80  const int max = ctx->max; \
81  \
82  for (int y = slice_start; y < slice_end; y++) { \
83  type *dst = (type *)(frame->data[0] + y * frame->linesize[0]);\
84  \
85  for (int x = 0; x < frame->width; x++) { \
86  const int o = x * 4; \
87  \
88  dst[o + co[3]] = do_colorkey_pixel(colorkey_rgba, \
89  dst[o + co[0]], \
90  dst[o + co[1]], \
91  dst[o + co[2]], \
92  similarity, iblend, max, scale); \
93  } \
94  } \
95  \
96  return 0; \
97 }
98 
99 COLORKEY_SLICE(8, uint8_t)
100 COLORKEY_SLICE(16, uint16_t)
101 
102 #define COLORHOLD_SLICE(name, type, htype) \
103 static int do_colorhold_slice##name(AVFilterContext *avctx, void *arg, \
104  int jobnr, int nb_jobs) \
105 { \
106  AVFrame *frame = arg; \
107  const int slice_start = (frame->height * jobnr) / nb_jobs; \
108  const int slice_end = (frame->height * (jobnr + 1)) / nb_jobs; \
109  ColorkeyContext *ctx = avctx->priv; \
110  const int depth = ctx->depth; \
111  const int max = ctx->max; \
112  const int half = max / 2; \
113  const uint8_t *co = ctx->co; \
114  const uint8_t *colorkey_rgba = ctx->colorkey_rgba; \
115  const float similarity = ctx->similarity; \
116  const float iblend = 1.f / ctx->blend; \
117  const double scale = ctx->scale; \
118  \
119  for (int y = slice_start; y < slice_end; ++y) { \
120  type *dst = (type *)(frame->data[0] + y * frame->linesize[0]); \
121  \
122  for (int x = 0; x < frame->width; ++x) { \
123  int o, t, r, g, b; \
124  \
125  o = x * 4; \
126  r = dst[o + co[0]]; \
127  g = dst[o + co[1]]; \
128  b = dst[o + co[2]]; \
129  \
130  t = do_colorkey_pixel(colorkey_rgba, r, g, b, \
131  similarity, iblend, max, scale); \
132  \
133  if (t > 0) { \
134  htype a = (r + g + b) / 3; \
135  htype rt = max - t; \
136  \
137  dst[o + co[0]] = (a * t + r * rt + half) >> depth; \
138  dst[o + co[1]] = (a * t + g * rt + half) >> depth; \
139  dst[o + co[2]] = (a * t + b * rt + half) >> depth; \
140  } \
141  } \
142  } \
143  \
144  return 0; \
145 }
146 
147 COLORHOLD_SLICE(8, uint8_t, int)
148 COLORHOLD_SLICE(16, uint16_t, int64_t)
149 
151 {
152  AVFilterContext *avctx = link->dst;
153  ColorkeyContext *ctx = avctx->priv;
154  int res;
155 
156  if (res = ff_filter_execute(avctx, ctx->do_slice, frame, NULL,
157  FFMIN(frame->height, ff_filter_get_nb_threads(avctx))))
158  return res;
159 
160  return ff_filter_frame(avctx->outputs[0], frame);
161 }
162 
163 static av_cold int config_output(AVFilterLink *outlink)
164 {
165  AVFilterContext *avctx = outlink->src;
166  ColorkeyContext *ctx = avctx->priv;
168 
169  ctx->depth = desc->comp[0].depth;
170  ctx->max = (1 << ctx->depth) - 1;
171  ctx->scale = 255.0 / ctx->max;
172  outlink->w = avctx->inputs[0]->w;
173  outlink->h = avctx->inputs[0]->h;
174  outlink->time_base = avctx->inputs[0]->time_base;
175  ff_fill_rgba_map(ctx->co, outlink->format);
176 
177  if (!strcmp(avctx->filter->name, "colorkey")) {
178  ctx->do_slice = ctx->max == 255 ? do_colorkey_slice8 : do_colorkey_slice16;
179  } else {
180  ctx->do_slice = ctx->max == 255 ? do_colorhold_slice8 : do_colorhold_slice16;
181  }
182 
183  return 0;
184 }
185 
186 static const enum AVPixelFormat pixel_fmts[] = {
194 };
195 
196 static const AVFilterPad colorkey_inputs[] = {
197  {
198  .name = "default",
199  .type = AVMEDIA_TYPE_VIDEO,
201  .filter_frame = filter_frame,
202  },
203 };
204 
205 static const AVFilterPad colorkey_outputs[] = {
206  {
207  .name = "default",
208  .type = AVMEDIA_TYPE_VIDEO,
209  .config_props = config_output,
210  },
211 };
212 
213 #define OFFSET(x) offsetof(ColorkeyContext, x)
214 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
215 
216 #if CONFIG_COLORKEY_FILTER
217 
218 static const AVOption colorkey_options[] = {
219  { "color", "set the colorkey key color", OFFSET(colorkey_rgba), AV_OPT_TYPE_COLOR, { .str = "black" }, 0, 0, FLAGS },
220  { "similarity", "set the colorkey similarity value", OFFSET(similarity), AV_OPT_TYPE_FLOAT, { .dbl = 0.01 }, 0.01, 1.0, FLAGS },
221  { "blend", "set the colorkey key blend value", OFFSET(blend), AV_OPT_TYPE_FLOAT, { .dbl = 0.0 }, 0.0, 1.0, FLAGS },
222  { NULL }
223 };
224 
225 AVFILTER_DEFINE_CLASS(colorkey);
226 
227 const AVFilter ff_vf_colorkey = {
228  .name = "colorkey",
229  .description = NULL_IF_CONFIG_SMALL("Turns a certain color into transparency. Operates on RGB colors."),
230  .priv_size = sizeof(ColorkeyContext),
231  .priv_class = &colorkey_class,
236  .process_command = ff_filter_process_command,
237 };
238 
239 #endif /* CONFIG_COLORKEY_FILTER */
240 #if CONFIG_COLORHOLD_FILTER
241 
242 static const AVOption colorhold_options[] = {
243  { "color", "set the colorhold key color", OFFSET(colorkey_rgba), AV_OPT_TYPE_COLOR, { .str = "black" }, 0, 0, FLAGS },
244  { "similarity", "set the colorhold similarity value", OFFSET(similarity), AV_OPT_TYPE_FLOAT, { .dbl = 0.01 }, 0.01, 1.0, FLAGS },
245  { "blend", "set the colorhold blend value", OFFSET(blend), AV_OPT_TYPE_FLOAT, { .dbl = 0.0 }, 0.0, 1.0, FLAGS },
246  { NULL }
247 };
248 
249 AVFILTER_DEFINE_CLASS(colorhold);
250 
251 const AVFilter ff_vf_colorhold = {
252  .name = "colorhold",
253  .description = NULL_IF_CONFIG_SMALL("Turns a certain color range into gray. Operates on RGB colors."),
254  .priv_size = sizeof(ColorkeyContext),
255  .priv_class = &colorhold_class,
260  .process_command = ff_filter_process_command,
261 };
262 
263 #endif /* CONFIG_COLORHOLD_FILTER */
AVPixelFormat
AVPixelFormat
Pixel format.
Definition: pixfmt.h:64
r
const char * r
Definition: vf_curves.c:116
opt.h
colorkey_outputs
static const AVFilterPad colorkey_outputs[]
Definition: vf_colorkey.c:205
ff_filter_frame
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:999
av_pix_fmt_desc_get
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2662
ff_vf_colorhold
const AVFilter ff_vf_colorhold
FILTER_PIXFMTS_ARRAY
#define FILTER_PIXFMTS_ARRAY(array)
Definition: internal.h:170
ColorkeyContext::scale
double scale
Definition: vf_colorkey.c:40
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:325
ColorkeyContext::max
int max
Definition: vf_colorkey.c:42
AVOption
AVOption.
Definition: opt.h:251
b
#define b
Definition: input.c:34
AV_PIX_FMT_BGRA
@ AV_PIX_FMT_BGRA
packed BGRA 8:8:8:8, 32bpp, BGRABGRA...
Definition: pixfmt.h:95
max
#define max(a, b)
Definition: cuda_runtime.h:33
AVFilter::name
const char * name
Filter name.
Definition: avfilter.h:175
ColorkeyContext::co
uint8_t co[4]
Definition: vf_colorkey.c:35
COLORKEY_SLICE
#define COLORKEY_SLICE(name, type)
Definition: vf_colorkey.c:66
video.h
formats.h
COLORHOLD_SLICE
#define COLORHOLD_SLICE(name, type, htype)
Definition: vf_colorkey.c:102
AVFilterContext::priv
void * priv
private data for use by the filter
Definition: avfilter.h:423
ColorkeyContext::blend
float blend
Definition: vf_colorkey.c:39
scale
static av_always_inline float scale(float x, float s)
Definition: vf_v360.c:1389
AVFilterPad
A filter pad used for either input or output.
Definition: internal.h:49
ColorkeyContext::do_slice
int(* do_slice)(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
Definition: vf_colorkey.c:44
av_cold
#define av_cold
Definition: attributes.h:90
filter_frame
static int filter_frame(AVFilterLink *link, AVFrame *frame)
Definition: vf_colorkey.c:150
g
const char * g
Definition: vf_curves.c:117
ctx
AVFormatContext * ctx
Definition: movenc.c:48
ColorkeyContext::colorkey_rgba
uint8_t colorkey_rgba[4]
Definition: vf_colorkey.c:37
FILTER_INPUTS
#define FILTER_INPUTS(array)
Definition: internal.h:190
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
AV_PIX_FMT_RGBA
@ AV_PIX_FMT_RGBA
packed RGBA 8:8:8:8, 32bpp, RGBARGBA...
Definition: pixfmt.h:93
arg
const char * arg
Definition: jacosubdec.c:67
AV_PIX_FMT_RGBA64
#define AV_PIX_FMT_RGBA64
Definition: pixfmt.h:396
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:66
NULL
#define NULL
Definition: coverity.c:32
ColorkeyContext
Definition: vf_colorkey.c:31
AV_OPT_TYPE_COLOR
@ AV_OPT_TYPE_COLOR
Definition: opt.h:240
AVFilterContext::inputs
AVFilterLink ** inputs
array of pointers to input links
Definition: avfilter.h:416
AV_PIX_FMT_ABGR
@ AV_PIX_FMT_ABGR
packed ABGR 8:8:8:8, 32bpp, ABGRABGR...
Definition: pixfmt.h:94
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_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:863
ColorkeyContext::similarity
float similarity
Definition: vf_colorkey.c:38
colorkey_inputs
static const AVFilterPad colorkey_inputs[]
Definition: vf_colorkey.c:196
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:152
AVFILTER_DEFINE_CLASS
#define AVFILTER_DEFINE_CLASS(fname)
Definition: internal.h:325
AV_PIX_FMT_ARGB
@ AV_PIX_FMT_ARGB
packed ARGB 8:8:8:8, 32bpp, ARGBARGB...
Definition: pixfmt.h:92
AV_OPT_TYPE_FLOAT
@ AV_OPT_TYPE_FLOAT
Definition: opt.h:228
AV_PIX_FMT_BGRA64
#define AV_PIX_FMT_BGRA64
Definition: pixfmt.h:401
FLAGS
#define FLAGS
Definition: vf_colorkey.c:214
ff_filter_get_nb_threads
int ff_filter_get_nb_threads(AVFilterContext *ctx)
Get number of threads for current filter instance.
Definition: avfilter.c:783
ColorkeyContext::depth
int depth
Definition: vf_colorkey.c:41
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
AVFilterPad::name
const char * name
Pad name.
Definition: internal.h:55
AVFilter
Filter definition.
Definition: avfilter.h:171
pixel_fmts
static enum AVPixelFormat pixel_fmts[]
Definition: vf_colorkey.c:186
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_NONE
@ AV_PIX_FMT_NONE
Definition: pixfmt.h:65
avfilter.h
AVFilterContext
An instance of a filter.
Definition: avfilter.h:408
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:127
desc
const char * desc
Definition: libsvtav1.c:83
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:69
diff
static av_always_inline int diff(const uint32_t a, const uint32_t b)
Definition: vf_palettegen.c:139
FILTER_OUTPUTS
#define FILTER_OUTPUTS(array)
Definition: internal.h:191
ff_fill_rgba_map
int ff_fill_rgba_map(uint8_t *rgba_map, enum AVPixelFormat pix_fmt)
Definition: drawutils.c:35
OFFSET
#define OFFSET(x)
Definition: vf_colorkey.c:213
imgutils.h
AVFilterContext::filter
const AVFilter * filter
the AVFilter of which this is an instance
Definition: avfilter.h:411
drawutils.h
ff_filter_execute
static av_always_inline int ff_filter_execute(AVFilterContext *ctx, avfilter_action_func *func, void *arg, int *ret, int nb_jobs)
Definition: internal.h:142
do_colorkey_pixel
static int do_colorkey_pixel(const uint8_t *colorkey_rgba, int r, int g, int b, float similarity, float iblend, int max, double scale)
Definition: vf_colorkey.c:48
int
int
Definition: ffmpeg_filter.c:153
av_clipd
av_clipd
Definition: af_crystalizer.c:132
config_output
static av_cold int config_output(AVFilterLink *outlink)
Definition: vf_colorkey.c:163
ff_vf_colorkey
const AVFilter ff_vf_colorkey
AVFilterContext::outputs
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:420
AVFILTERPAD_FLAG_NEEDS_WRITABLE
#define AVFILTERPAD_FLAG_NEEDS_WRITABLE
The filter expects writable frames from its input link, duplicating data buffers if needed.
Definition: internal.h:68