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 "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 ColorkeyContext {
29  const AVClass *class;
30 
31  /* color offsets rgba */
32  int co[4];
33 
34  uint8_t colorkey_rgba[4];
35  float similarity;
36  float blend;
37 
39  int jobnr, int nb_jobs);
41 
42 static uint8_t do_colorkey_pixel(ColorkeyContext *ctx, uint8_t r, uint8_t g, uint8_t b)
43 {
44  int dr = (int)r - ctx->colorkey_rgba[0];
45  int dg = (int)g - ctx->colorkey_rgba[1];
46  int db = (int)b - ctx->colorkey_rgba[2];
47 
48  double diff = sqrt((dr * dr + dg * dg + db * db) / (255.0 * 255.0 * 3.0));
49 
50  if (ctx->blend > 0.0001) {
51  return av_clipd((diff - ctx->similarity) / ctx->blend, 0.0, 1.0) * 255.0;
52  } else {
53  return (diff > ctx->similarity) ? 255 : 0;
54  }
55 }
56 
57 static int do_colorkey_slice(AVFilterContext *avctx, void *arg, int jobnr, int nb_jobs)
58 {
59  AVFrame *frame = arg;
60 
61  const int slice_start = (frame->height * jobnr) / nb_jobs;
62  const int slice_end = (frame->height * (jobnr + 1)) / nb_jobs;
63 
64  ColorkeyContext *ctx = avctx->priv;
65 
66  int o, x, y;
67 
68  for (y = slice_start; y < slice_end; ++y) {
69  for (x = 0; x < frame->width; ++x) {
70  o = frame->linesize[0] * y + x * 4;
71 
72  frame->data[0][o + ctx->co[3]] =
74  frame->data[0][o + ctx->co[0]],
75  frame->data[0][o + ctx->co[1]],
76  frame->data[0][o + ctx->co[2]]);
77  }
78  }
79 
80  return 0;
81 }
82 
83 static int do_colorhold_slice(AVFilterContext *avctx, void *arg, int jobnr, int nb_jobs)
84 {
85  AVFrame *frame = arg;
86 
87  const int slice_start = (frame->height * jobnr) / nb_jobs;
88  const int slice_end = (frame->height * (jobnr + 1)) / nb_jobs;
89 
90  ColorkeyContext *ctx = avctx->priv;
91 
92  int x, y;
93 
94  for (y = slice_start; y < slice_end; ++y) {
95  for (x = 0; x < frame->width; ++x) {
96  int o, t, r, g, b;
97 
98  o = frame->linesize[0] * y + x * 4;
99  r = frame->data[0][o + ctx->co[0]];
100  g = frame->data[0][o + ctx->co[1]];
101  b = frame->data[0][o + ctx->co[2]];
102 
103  t = do_colorkey_pixel(ctx, r, g, b);
104 
105  if (t > 0) {
106  int a = (r + g + b) / 3;
107  int rt = 255 - t;
108 
109  frame->data[0][o + ctx->co[0]] = (a * t + r * rt + 127) >> 8;
110  frame->data[0][o + ctx->co[1]] = (a * t + g * rt + 127) >> 8;
111  frame->data[0][o + ctx->co[2]] = (a * t + b * rt + 127) >> 8;
112  }
113  }
114  }
115 
116  return 0;
117 }
118 
120 {
121  ColorkeyContext *ctx = avctx->priv;
122 
123  if (!strcmp(avctx->filter->name, "colorkey")) {
124  ctx->do_slice = do_colorkey_slice;
125  } else {
126  ctx->do_slice = do_colorhold_slice;
127  }
128 
129  return 0;
130 }
131 
133 {
134  AVFilterContext *avctx = link->dst;
135  ColorkeyContext *ctx = avctx->priv;
136  int res;
137 
138  if (res = ff_filter_execute(avctx, ctx->do_slice, frame, NULL,
139  FFMIN(frame->height, ff_filter_get_nb_threads(avctx))))
140  return res;
141 
142  return ff_filter_frame(avctx->outputs[0], frame);
143 }
144 
145 static av_cold int config_output(AVFilterLink *outlink)
146 {
147  AVFilterContext *avctx = outlink->src;
148  ColorkeyContext *ctx = avctx->priv;
150  int i;
151 
152  outlink->w = avctx->inputs[0]->w;
153  outlink->h = avctx->inputs[0]->h;
154  outlink->time_base = avctx->inputs[0]->time_base;
155 
156  for (i = 0; i < 4; ++i)
157  ctx->co[i] = desc->comp[i].offset;
158 
159  return 0;
160 }
161 
162 static const enum AVPixelFormat pixel_fmts[] = {
168 };
169 
170 static const AVFilterPad colorkey_inputs[] = {
171  {
172  .name = "default",
173  .type = AVMEDIA_TYPE_VIDEO,
175  .filter_frame = filter_frame,
176  },
177 };
178 
179 static const AVFilterPad colorkey_outputs[] = {
180  {
181  .name = "default",
182  .type = AVMEDIA_TYPE_VIDEO,
183  .config_props = config_output,
184  },
185 };
186 
187 #define OFFSET(x) offsetof(ColorkeyContext, x)
188 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
189 
190 #if CONFIG_COLORKEY_FILTER
191 
192 static const AVOption colorkey_options[] = {
193  { "color", "set the colorkey key color", OFFSET(colorkey_rgba), AV_OPT_TYPE_COLOR, { .str = "black" }, 0, 0, FLAGS },
194  { "similarity", "set the colorkey similarity value", OFFSET(similarity), AV_OPT_TYPE_FLOAT, { .dbl = 0.01 }, 0.01, 1.0, FLAGS },
195  { "blend", "set the colorkey key blend value", OFFSET(blend), AV_OPT_TYPE_FLOAT, { .dbl = 0.0 }, 0.0, 1.0, FLAGS },
196  { NULL }
197 };
198 
199 AVFILTER_DEFINE_CLASS(colorkey);
200 
201 const AVFilter ff_vf_colorkey = {
202  .name = "colorkey",
203  .description = NULL_IF_CONFIG_SMALL("Turns a certain color into transparency. Operates on RGB colors."),
204  .priv_size = sizeof(ColorkeyContext),
205  .priv_class = &colorkey_class,
206  .init = init_filter,
211  .process_command = ff_filter_process_command,
212 };
213 
214 #endif /* CONFIG_COLORKEY_FILTER */
215 #if CONFIG_COLORHOLD_FILTER
216 
217 static const AVOption colorhold_options[] = {
218  { "color", "set the colorhold key color", OFFSET(colorkey_rgba), AV_OPT_TYPE_COLOR, { .str = "black" }, 0, 0, FLAGS },
219  { "similarity", "set the colorhold similarity value", OFFSET(similarity), AV_OPT_TYPE_FLOAT, { .dbl = 0.01 }, 0.01, 1.0, FLAGS },
220  { "blend", "set the colorhold blend value", OFFSET(blend), AV_OPT_TYPE_FLOAT, { .dbl = 0.0 }, 0.0, 1.0, FLAGS },
221  { NULL }
222 };
223 
224 AVFILTER_DEFINE_CLASS(colorhold);
225 
226 const AVFilter ff_vf_colorhold = {
227  .name = "colorhold",
228  .description = NULL_IF_CONFIG_SMALL("Turns a certain color range into gray. Operates on RGB colors."),
229  .priv_size = sizeof(ColorkeyContext),
230  .priv_class = &colorhold_class,
231  .init = init_filter,
236  .process_command = ff_filter_process_command,
237 };
238 
239 #endif /* CONFIG_COLORHOLD_FILTER */
init_filter
static av_cold int init_filter(AVFilterContext *avctx)
Definition: vf_colorkey.c:119
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:179
ff_filter_frame
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1018
av_pix_fmt_desc_get
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2660
ff_vf_colorhold
const AVFilter ff_vf_colorhold
FILTER_PIXFMTS_ARRAY
#define FILTER_PIXFMTS_ARRAY(array)
Definition: internal.h:171
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:317
AVOption
AVOption.
Definition: opt.h:247
b
#define b
Definition: input.c:40
AV_PIX_FMT_BGRA
@ AV_PIX_FMT_BGRA
packed BGRA 8:8:8:8, 32bpp, BGRABGRA...
Definition: pixfmt.h:95
AVFilter::name
const char * name
Filter name.
Definition: avfilter.h:169
video.h
formats.h
init
static int init
Definition: av_tx.c:47
AVFilterContext::priv
void * priv
private data for use by the filter
Definition: avfilter.h:417
ColorkeyContext::blend
float blend
Definition: vf_colorkey.c:36
AVFilterPad
A filter pad used for either input or output.
Definition: internal.h:50
ColorkeyContext::do_slice
int(* do_slice)(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
Definition: vf_colorkey.c:38
av_cold
#define av_cold
Definition: attributes.h:90
filter_frame
static int filter_frame(AVFilterLink *link, AVFrame *frame)
Definition: vf_colorkey.c:132
do_colorkey_slice
static int do_colorkey_slice(AVFilterContext *avctx, void *arg, int jobnr, int nb_jobs)
Definition: vf_colorkey.c:57
g
const char * g
Definition: vf_curves.c:117
slice_end
static int slice_end(AVCodecContext *avctx, AVFrame *pict)
Handle slice ends.
Definition: mpeg12dec.c:2042
ctx
AVFormatContext * ctx
Definition: movenc.c:48
do_colorkey_pixel
static uint8_t do_colorkey_pixel(ColorkeyContext *ctx, uint8_t r, uint8_t g, uint8_t b)
Definition: vf_colorkey.c:42
ColorkeyContext::colorkey_rgba
uint8_t colorkey_rgba[4]
Definition: vf_colorkey.c:34
FILTER_INPUTS
#define FILTER_INPUTS(array)
Definition: internal.h:191
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
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:28
AV_OPT_TYPE_COLOR
@ AV_OPT_TYPE_COLOR
Definition: opt.h:239
AVFilterContext::inputs
AVFilterLink ** inputs
array of pointers to input links
Definition: avfilter.h:410
AV_PIX_FMT_ABGR
@ AV_PIX_FMT_ABGR
packed ABGR 8:8:8:8, 32bpp, ABGRABGR...
Definition: pixfmt.h:94
do_colorhold_slice
static int do_colorhold_slice(AVFilterContext *avctx, void *arg, int jobnr, int nb_jobs)
Definition: vf_colorkey.c:83
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
av_clipd
#define av_clipd
Definition: common.h:147
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
a
The reader does not expect b to be semantically here and if the code is changed by maybe adding a a division or other the signedness will almost certainly be mistaken To avoid this confusion a new type was SUINT is the C unsigned type but it holds a signed int to use the same example SUINT a
Definition: undefined.txt:41
ColorkeyContext::co
int co[4]
Definition: vf_colorkey.c:32
ColorkeyContext::similarity
float similarity
Definition: vf_colorkey.c:35
colorkey_inputs
static const AVFilterPad colorkey_inputs[]
Definition: vf_colorkey.c:170
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:146
AVFILTER_DEFINE_CLASS
#define AVFILTER_DEFINE_CLASS(fname)
Definition: internal.h:326
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:227
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:271
FLAGS
#define FLAGS
Definition: vf_colorkey.c:188
ff_filter_get_nb_threads
int ff_filter_get_nb_threads(AVFilterContext *ctx)
Get number of threads for current filter instance.
Definition: avfilter.c:803
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
AVFilterPad::name
const char * name
Pad name.
Definition: internal.h:56
AVFilter
Filter definition.
Definition: avfilter.h:165
pixel_fmts
static enum AVPixelFormat pixel_fmts[]
Definition: vf_colorkey.c:162
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:402
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:121
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: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:192
OFFSET
#define OFFSET(x)
Definition: vf_colorkey.c:187
imgutils.h
AVFilterContext::filter
const AVFilter * filter
the AVFilter of which this is an instance
Definition: avfilter.h:405
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:143
int
int
Definition: ffmpeg_filter.c:153
config_output
static av_cold int config_output(AVFilterLink *outlink)
Definition: vf_colorkey.c:145
ff_vf_colorkey
const AVFilter ff_vf_colorkey
AVFilterContext::outputs
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:414
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:69