FFmpeg  4.4.8
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 "filters.h"
25 #include "formats.h"
26 #include "internal.h"
27 #include "video.h"
28 
29 typedef struct ColorkeyContext {
30  const AVClass *class;
31 
32  /* color offsets rgba */
33  int co[4];
34 
36  float similarity;
37  float blend;
38 
40  int jobnr, int nb_jobs);
42 
44 {
45  int dr = (int)r - ctx->colorkey_rgba[0];
46  int dg = (int)g - ctx->colorkey_rgba[1];
47  int db = (int)b - ctx->colorkey_rgba[2];
48 
49  double diff = sqrt((dr * dr + dg * dg + db * db) / (255.0 * 255.0 * 3.0));
50 
51  if (ctx->blend > 0.0001) {
52  return av_clipd((diff - ctx->similarity) / ctx->blend, 0.0, 1.0) * 255.0;
53  } else {
54  return (diff > ctx->similarity) ? 255 : 0;
55  }
56 }
57 
58 static int do_colorkey_slice(AVFilterContext *avctx, void *arg, int jobnr, int nb_jobs)
59 {
60  AVFrame *frame = arg;
61 
62  const int slice_start = ff_slice_pos(frame->height, jobnr, nb_jobs);
63  const int slice_end = ff_slice_pos(frame->height, jobnr + 1, nb_jobs);
64 
65  ColorkeyContext *ctx = avctx->priv;
66 
67  int o, x, y;
68 
69  for (y = slice_start; y < slice_end; ++y) {
70  for (x = 0; x < frame->width; ++x) {
71  o = frame->linesize[0] * y + x * 4;
72 
73  frame->data[0][o + ctx->co[3]] =
75  frame->data[0][o + ctx->co[0]],
76  frame->data[0][o + ctx->co[1]],
77  frame->data[0][o + ctx->co[2]]);
78  }
79  }
80 
81  return 0;
82 }
83 
84 static int do_colorhold_slice(AVFilterContext *avctx, void *arg, int jobnr, int nb_jobs)
85 {
86  AVFrame *frame = arg;
87 
88  const int slice_start = ff_slice_pos(frame->height, jobnr, nb_jobs);
89  const int slice_end = ff_slice_pos(frame->height, jobnr + 1, nb_jobs);
90 
91  ColorkeyContext *ctx = avctx->priv;
92 
93  int x, y;
94 
95  for (y = slice_start; y < slice_end; ++y) {
96  for (x = 0; x < frame->width; ++x) {
97  int o, t, r, g, b;
98 
99  o = frame->linesize[0] * y + x * 4;
100  r = frame->data[0][o + ctx->co[0]];
101  g = frame->data[0][o + ctx->co[1]];
102  b = frame->data[0][o + ctx->co[2]];
103 
104  t = do_colorkey_pixel(ctx, r, g, b);
105 
106  if (t > 0) {
107  int a = (r + g + b) / 3;
108  int rt = 255 - t;
109 
110  frame->data[0][o + ctx->co[0]] = (a * t + r * rt + 127) >> 8;
111  frame->data[0][o + ctx->co[1]] = (a * t + g * rt + 127) >> 8;
112  frame->data[0][o + ctx->co[2]] = (a * t + b * rt + 127) >> 8;
113  }
114  }
115  }
116 
117  return 0;
118 }
119 
121 {
122  ColorkeyContext *ctx = avctx->priv;
123 
124  if (!strcmp(avctx->filter->name, "colorkey")) {
125  ctx->do_slice = do_colorkey_slice;
126  } else {
127  ctx->do_slice = do_colorhold_slice;
128  }
129 
130  return 0;
131 }
132 
134 {
135  AVFilterContext *avctx = link->dst;
136  ColorkeyContext *ctx = avctx->priv;
137  int res;
138 
139  if (res = av_frame_make_writable(frame))
140  return res;
141 
142  if (res = avctx->internal->execute(avctx, ctx->do_slice, frame, NULL, FFMIN(frame->height, ff_filter_get_nb_threads(avctx))))
143  return res;
144 
145  return ff_filter_frame(avctx->outputs[0], frame);
146 }
147 
148 static av_cold int config_output(AVFilterLink *outlink)
149 {
150  AVFilterContext *avctx = outlink->src;
151  ColorkeyContext *ctx = avctx->priv;
153  int i;
154 
155  outlink->w = avctx->inputs[0]->w;
156  outlink->h = avctx->inputs[0]->h;
157  outlink->time_base = avctx->inputs[0]->time_base;
158 
159  for (i = 0; i < 4; ++i)
160  ctx->co[i] = desc->comp[i].offset;
161 
162  return 0;
163 }
164 
166 {
167  static const enum AVPixelFormat pixel_fmts[] = {
173  };
174 
176 
177  formats = ff_make_format_list(pixel_fmts);
178  if (!formats)
179  return AVERROR(ENOMEM);
180 
181  return ff_set_common_formats(avctx, formats);
182 }
183 
184 static const AVFilterPad colorkey_inputs[] = {
185  {
186  .name = "default",
187  .type = AVMEDIA_TYPE_VIDEO,
188  .filter_frame = filter_frame,
189  },
190  { NULL }
191 };
192 
193 static const AVFilterPad colorkey_outputs[] = {
194  {
195  .name = "default",
196  .type = AVMEDIA_TYPE_VIDEO,
197  .config_props = config_output,
198  },
199  { NULL }
200 };
201 
202 #define OFFSET(x) offsetof(ColorkeyContext, x)
203 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
204 
205 #if CONFIG_COLORKEY_FILTER
206 
207 static const AVOption colorkey_options[] = {
208  { "color", "set the colorkey key color", OFFSET(colorkey_rgba), AV_OPT_TYPE_COLOR, { .str = "black" }, 0, 0, FLAGS },
209  { "similarity", "set the colorkey similarity value", OFFSET(similarity), AV_OPT_TYPE_FLOAT, { .dbl = 0.01 }, 0.01, 1.0, FLAGS },
210  { "blend", "set the colorkey key blend value", OFFSET(blend), AV_OPT_TYPE_FLOAT, { .dbl = 0.0 }, 0.0, 1.0, FLAGS },
211  { NULL }
212 };
213 
214 AVFILTER_DEFINE_CLASS(colorkey);
215 
217  .name = "colorkey",
218  .description = NULL_IF_CONFIG_SMALL("Turns a certain color into transparency. Operates on RGB colors."),
219  .priv_size = sizeof(ColorkeyContext),
220  .priv_class = &colorkey_class,
222  .init = init_filter,
227 };
228 
229 #endif /* CONFIG_COLORKEY_FILTER */
230 #if CONFIG_COLORHOLD_FILTER
231 
232 static const AVOption colorhold_options[] = {
233  { "color", "set the colorhold key color", OFFSET(colorkey_rgba), AV_OPT_TYPE_COLOR, { .str = "black" }, 0, 0, FLAGS },
234  { "similarity", "set the colorhold similarity value", OFFSET(similarity), AV_OPT_TYPE_FLOAT, { .dbl = 0.01 }, 0.01, 1.0, FLAGS },
235  { "blend", "set the colorhold blend value", OFFSET(blend), AV_OPT_TYPE_FLOAT, { .dbl = 0.0 }, 0.0, 1.0, FLAGS },
236  { NULL }
237 };
238 
239 AVFILTER_DEFINE_CLASS(colorhold);
240 
242  .name = "colorhold",
243  .description = NULL_IF_CONFIG_SMALL("Turns a certain color range into gray. Operates on RGB colors."),
244  .priv_size = sizeof(ColorkeyContext),
245  .priv_class = &colorhold_class,
247  .init = init_filter,
252 };
253 
254 #endif /* CONFIG_COLORHOLD_FILTER */
static const AVFilterPad inputs[]
Definition: af_acontrast.c:193
static const AVFilterPad outputs[]
Definition: af_acontrast.c:203
static int process_command(AVFilterContext *ctx, const char *cmd, const char *args, char *res, int res_len, int flags)
Definition: af_acrusher.c:336
AVFilter ff_vf_colorhold
AVFilter ff_vf_colorkey
#define av_cold
Definition: attributes.h:88
uint8_t
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1096
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
int ff_filter_get_nb_threads(AVFilterContext *ctx)
Get number of threads for current filter instance.
Definition: avfilter.c:802
Main libavfilter public API header.
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:31
#define flags(name, subs,...)
Definition: cbs_av1.c:572
#define FFMIN(a, b)
Definition: common.h:105
#define av_clipd
Definition: common.h:173
#define NULL
Definition: coverity.c:32
static AVFrame * frame
int
static int ff_slice_pos(int total, int jobnr, int nb_jobs)
Compute the boundary index for a slice when work of size total is split into nb_jobs slices.
Definition: filters.h:271
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
AVFilterFormats * ff_make_format_list(const int *fmts)
Create a list of supported formats.
Definition: formats.c:286
@ AV_OPT_TYPE_FLOAT
Definition: opt.h:228
@ AV_OPT_TYPE_COLOR
Definition: opt.h:240
#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
#define AVFILTER_FLAG_SLICE_THREADS
The filter supports multithreading by splitting frames into multiple parts and processing them concur...
Definition: avfilter.h:117
#define AVERROR(e)
Definition: error.h:43
int av_frame_make_writable(AVFrame *frame)
Ensure that the frame data is writable, avoiding data copy if possible.
Definition: frame.c:611
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
misc image utilities
int i
Definition: input.c:407
const char * arg
Definition: jacosubdec.c:66
#define AVFILTER_DEFINE_CLASS(fname)
Definition: internal.h:288
common internal API header
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:117
const char * desc
Definition: libsvtav1.c:79
static int slice_end(AVCodecContext *avctx, AVFrame *pict)
Handle slice ends.
Definition: mpeg12dec.c:2033
AVOptions.
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2573
AVPixelFormat
Pixel format.
Definition: pixfmt.h:64
@ AV_PIX_FMT_NONE
Definition: pixfmt.h:65
@ AV_PIX_FMT_ARGB
packed ARGB 8:8:8:8, 32bpp, ARGBARGB...
Definition: pixfmt.h:92
@ AV_PIX_FMT_BGRA
packed BGRA 8:8:8:8, 32bpp, BGRABGRA...
Definition: pixfmt.h:95
@ AV_PIX_FMT_ABGR
packed ABGR 8:8:8:8, 32bpp, ABGRABGR...
Definition: pixfmt.h:94
@ AV_PIX_FMT_RGBA
packed RGBA 8:8:8:8, 32bpp, RGBARGBA...
Definition: pixfmt.h:93
formats
Definition: signature.h:48
Describe the class of an AVClass context structure.
Definition: log.h:67
An instance of a filter.
Definition: avfilter.h:341
AVFilterLink ** inputs
array of pointers to input links
Definition: avfilter.h:349
const AVFilter * filter
the AVFilter of which this is an instance
Definition: avfilter.h:344
AVFilterInternal * internal
An opaque struct for libavfilter internal use.
Definition: avfilter.h:381
void * priv
private data for use by the filter
Definition: avfilter.h:356
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:353
A list of supported formats for one end of a filter link.
Definition: formats.h:65
avfilter_execute_func * execute
Definition: internal.h:136
A filter pad used for either input or output.
Definition: internal.h:54
const char * name
Pad name.
Definition: internal.h:60
Filter definition.
Definition: avfilter.h:145
const char * name
Filter name.
Definition: avfilter.h:149
This structure describes decoded (raw) audio or video data.
Definition: frame.h:318
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:332
int width
Definition: frame.h:376
int height
Definition: frame.h:376
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:349
AVOption.
Definition: opt.h:248
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:81
uint8_t colorkey_rgba[4]
Definition: vf_colorkey.c:35
int(* do_slice)(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
Definition: vf_colorkey.c:39
AVFormatContext * ctx
Definition: movenc.c:48
static const AVFilterPad colorkey_outputs[]
Definition: vf_colorkey.c:193
static av_cold int query_formats(AVFilterContext *avctx)
Definition: vf_colorkey.c:165
static const AVFilterPad colorkey_inputs[]
Definition: vf_colorkey.c:184
#define FLAGS
Definition: vf_colorkey.c:203
static av_cold int config_output(AVFilterLink *outlink)
Definition: vf_colorkey.c:148
static int do_colorkey_slice(AVFilterContext *avctx, void *arg, int jobnr, int nb_jobs)
Definition: vf_colorkey.c:58
static int filter_frame(AVFilterLink *link, AVFrame *frame)
Definition: vf_colorkey.c:133
static uint8_t do_colorkey_pixel(ColorkeyContext *ctx, uint8_t r, uint8_t g, uint8_t b)
Definition: vf_colorkey.c:43
static av_cold int init_filter(AVFilterContext *avctx)
Definition: vf_colorkey.c:120
#define OFFSET(x)
Definition: vf_colorkey.c:202
static int do_colorhold_slice(AVFilterContext *avctx, void *arg, int jobnr, int nb_jobs)
Definition: vf_colorkey.c:84
const char * b
Definition: vf_curves.c:119
const char * g
Definition: vf_curves.c:118
const char * r
Definition: vf_curves.c:117
static av_always_inline int diff(const uint32_t a, const uint32_t b)