FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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 
35  float similarity;
36  float blend;
38 
40 {
41  int dr = (int)r - ctx->colorkey_rgba[0];
42  int dg = (int)g - ctx->colorkey_rgba[1];
43  int db = (int)b - ctx->colorkey_rgba[2];
44 
45  double diff = sqrt((dr * dr + dg * dg + db * db) / (255.0 * 255.0));
46 
47  if (ctx->blend > 0.0001) {
48  return av_clipd((diff - ctx->similarity) / ctx->blend, 0.0, 1.0) * 255.0;
49  } else {
50  return (diff > ctx->similarity) ? 255 : 0;
51  }
52 }
53 
54 static int do_colorkey_slice(AVFilterContext *avctx, void *arg, int jobnr, int nb_jobs)
55 {
56  AVFrame *frame = arg;
57 
58  const int slice_start = (frame->height * jobnr) / nb_jobs;
59  const int slice_end = (frame->height * (jobnr + 1)) / nb_jobs;
60 
61  ColorkeyContext *ctx = avctx->priv;
62 
63  int o, x, y;
64 
65  for (y = slice_start; y < slice_end; ++y) {
66  for (x = 0; x < frame->width; ++x) {
67  o = frame->linesize[0] * y + x * 4;
68 
69  frame->data[0][o + ctx->co[3]] =
71  frame->data[0][o + ctx->co[0]],
72  frame->data[0][o + ctx->co[1]],
73  frame->data[0][o + ctx->co[2]]);
74  }
75  }
76 
77  return 0;
78 }
79 
81 {
82  AVFilterContext *avctx = link->dst;
83  int res;
84 
85  if (res = av_frame_make_writable(frame))
86  return res;
87 
88  if (res = avctx->internal->execute(avctx, do_colorkey_slice, frame, NULL, FFMIN(frame->height, ff_filter_get_nb_threads(avctx))))
89  return res;
90 
91  return ff_filter_frame(avctx->outputs[0], frame);
92 }
93 
94 static av_cold int config_output(AVFilterLink *outlink)
95 {
96  AVFilterContext *avctx = outlink->src;
97  ColorkeyContext *ctx = avctx->priv;
99  int i;
100 
101  outlink->w = avctx->inputs[0]->w;
102  outlink->h = avctx->inputs[0]->h;
103  outlink->time_base = avctx->inputs[0]->time_base;
104 
105  for (i = 0; i < 4; ++i)
106  ctx->co[i] = desc->comp[i].offset;
107 
108  return 0;
109 }
110 
112 {
113  static const enum AVPixelFormat pixel_fmts[] = {
119  };
120 
122 
123  formats = ff_make_format_list(pixel_fmts);
124  if (!formats)
125  return AVERROR(ENOMEM);
126 
127  return ff_set_common_formats(avctx, formats);
128 }
129 
130 static const AVFilterPad colorkey_inputs[] = {
131  {
132  .name = "default",
133  .type = AVMEDIA_TYPE_VIDEO,
134  .filter_frame = filter_frame,
135  },
136  { NULL }
137 };
138 
139 static const AVFilterPad colorkey_outputs[] = {
140  {
141  .name = "default",
142  .type = AVMEDIA_TYPE_VIDEO,
143  .config_props = config_output,
144  },
145  { NULL }
146 };
147 
148 #define OFFSET(x) offsetof(ColorkeyContext, x)
149 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
150 
151 static const AVOption colorkey_options[] = {
152  { "color", "set the colorkey key color", OFFSET(colorkey_rgba), AV_OPT_TYPE_COLOR, { .str = "black" }, CHAR_MIN, CHAR_MAX, FLAGS },
153  { "similarity", "set the colorkey similarity value", OFFSET(similarity), AV_OPT_TYPE_FLOAT, { .dbl = 0.01 }, 0.01, 1.0, FLAGS },
154  { "blend", "set the colorkey key blend value", OFFSET(blend), AV_OPT_TYPE_FLOAT, { .dbl = 0.0 }, 0.0, 1.0, FLAGS },
155  { NULL }
156 };
157 
158 AVFILTER_DEFINE_CLASS(colorkey);
159 
161  .name = "colorkey",
162  .description = NULL_IF_CONFIG_SMALL("Turns a certain color into transparency. Operates on RGB colors."),
163  .priv_size = sizeof(ColorkeyContext),
164  .priv_class = &colorkey_class,
166  .inputs = colorkey_inputs,
167  .outputs = colorkey_outputs,
169 };
AVFilter ff_vf_colorkey
Definition: vf_colorkey.c:160
#define NULL
Definition: coverity.c:32
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2266
This structure describes decoded (raw) audio or video data.
Definition: frame.h:184
AVOption.
Definition: opt.h:245
uint8_t colorkey_rgba[4]
Definition: vf_colorkey.c:34
misc image utilities
Main libavfilter public API header.
const char * g
Definition: vf_curves.c:112
const char * desc
Definition: nvenc.c:101
const char * b
Definition: vf_curves.c:113
static const AVFilterPad colorkey_outputs[]
Definition: vf_colorkey.c:139
static enum AVSampleFormat formats[]
Definition: avresample.c:163
AVFilterFormats * ff_make_format_list(const int *fmts)
Create a list of supported formats.
Definition: formats.c:283
static av_cold int config_output(AVFilterLink *outlink)
Definition: vf_colorkey.c:94
#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:59
AVFilterLink ** inputs
array of pointers to input links
Definition: avfilter.h:315
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1189
AVComponentDescriptor comp[4]
Parameters that describe how pixels are packed.
Definition: pixdesc.h:117
uint8_t
#define av_cold
Definition: attributes.h:82
AVOptions.
static uint8_t do_colorkey_pixel(ColorkeyContext *ctx, uint8_t r, uint8_t g, uint8_t b)
Definition: vf_colorkey.c:39
packed ABGR 8:8:8:8, 32bpp, ABGRABGR...
Definition: pixfmt.h:95
static AVFrame * frame
A filter pad used for either input or output.
Definition: internal.h:53
int width
width and height of the video frame
Definition: frame.h:236
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
static const AVOption colorkey_options[]
Definition: vf_colorkey.c:151
#define AVERROR(e)
Definition: error.h:43
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:176
packed BGRA 8:8:8:8, 32bpp, BGRABGRA...
Definition: pixfmt.h:96
const char * r
Definition: vf_curves.c:111
void * priv
private data for use by the filter
Definition: avfilter.h:322
#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
packed ARGB 8:8:8:8, 32bpp, ARGBARGB...
Definition: pixfmt.h:93
static int filter_frame(AVFilterLink *link, AVFrame *frame)
Definition: vf_colorkey.c:80
packed RGBA 8:8:8:8, 32bpp, RGBARGBA...
Definition: pixfmt.h:94
static int do_colorkey_slice(AVFilterContext *avctx, void *arg, int jobnr, int nb_jobs)
Definition: vf_colorkey.c:54
int ff_filter_get_nb_threads(AVFilterContext *ctx)
Get number of threads for current filter instance.
Definition: avfilter.c:786
#define FFMIN(a, b)
Definition: common.h:96
AVFormatContext * ctx
Definition: movenc.c:48
static const AVFilterPad colorkey_inputs[]
Definition: vf_colorkey.c:130
static const AVFilterPad outputs[]
Definition: af_afftfilt.c:386
#define FLAGS
Definition: vf_colorkey.c:149
AVFILTER_DEFINE_CLASS(colorkey)
static const AVFilterPad inputs[]
Definition: af_afftfilt.c:376
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:215
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:81
#define OFFSET(x)
Definition: vf_colorkey.c:148
Describe the class of an AVClass context structure.
Definition: log.h:67
Filter definition.
Definition: avfilter.h:144
const char * name
Filter name.
Definition: avfilter.h:148
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:319
int offset
Number of elements before the component of the first pixel.
Definition: pixdesc.h:47
int av_frame_make_writable(AVFrame *frame)
Ensure that the frame data is writable, avoiding data copy if possible.
Definition: frame.c:546
AVFilterInternal * internal
An opaque struct for libavfilter internal use.
Definition: avfilter.h:347
static int flags
Definition: cpu.c:47
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:198
avfilter_execute_func * execute
Definition: internal.h:153
static int slice_end(AVCodecContext *avctx, AVFrame *pict)
Handle slice ends.
Definition: mpeg12dec.c:2051
static av_always_inline int diff(const uint32_t a, const uint32_t b)
static av_cold int query_formats(AVFilterContext *avctx)
Definition: vf_colorkey.c:111
A list of supported formats for one end of a filter link.
Definition: formats.h:64
An instance of a filter.
Definition: avfilter.h:307
int height
Definition: frame.h:236
internal API functions
AVPixelFormat
Pixel format.
Definition: pixfmt.h:60