2 * Copyright (c) 2015 Timo Rothenpieler <timo@rothenpieler.org>
4 * This file is part of FFmpeg.
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.
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.
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
21 #include "libavutil/opt.h"
22 #include "libavutil/imgutils.h"
28 typedef struct ColorkeyContext {
31 /* color offsets rgba */
34 uint8_t colorkey_rgba[4];
38 int (*do_slice)(AVFilterContext *ctx, void *arg,
39 int jobnr, int nb_jobs);
42 static uint8_t do_colorkey_pixel(ColorkeyContext *ctx, uint8_t r, uint8_t g, uint8_t b)
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];
48 double diff = sqrt((dr * dr + dg * dg + db * db) / (255.0 * 255.0 * 3.0));
50 if (ctx->blend > 0.0001) {
51 return av_clipd((diff - ctx->similarity) / ctx->blend, 0.0, 1.0) * 255.0;
53 return (diff > ctx->similarity) ? 255 : 0;
57 static int do_colorkey_slice(AVFilterContext *avctx, void *arg, int jobnr, int nb_jobs)
61 const int slice_start = (frame->height * jobnr) / nb_jobs;
62 const int slice_end = (frame->height * (jobnr + 1)) / nb_jobs;
64 ColorkeyContext *ctx = avctx->priv;
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;
72 frame->data[0][o + ctx->co[3]] =
73 do_colorkey_pixel(ctx,
74 frame->data[0][o + ctx->co[0]],
75 frame->data[0][o + ctx->co[1]],
76 frame->data[0][o + ctx->co[2]]);
83 static int do_colorhold_slice(AVFilterContext *avctx, void *arg, int jobnr, int nb_jobs)
87 const int slice_start = (frame->height * jobnr) / nb_jobs;
88 const int slice_end = (frame->height * (jobnr + 1)) / nb_jobs;
90 ColorkeyContext *ctx = avctx->priv;
94 for (y = slice_start; y < slice_end; ++y) {
95 for (x = 0; x < frame->width; ++x) {
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]];
103 t = do_colorkey_pixel(ctx, r, g, b);
106 int a = (r + g + b) / 3;
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;
119 static av_cold int init_filter(AVFilterContext *avctx)
121 ColorkeyContext *ctx = avctx->priv;
123 if (!strcmp(avctx->filter->name, "colorkey")) {
124 ctx->do_slice = do_colorkey_slice;
126 ctx->do_slice = do_colorhold_slice;
132 static int filter_frame(AVFilterLink *link, AVFrame *frame)
134 AVFilterContext *avctx = link->dst;
135 ColorkeyContext *ctx = avctx->priv;
138 if (res = av_frame_make_writable(frame))
141 if (res = avctx->internal->execute(avctx, ctx->do_slice, frame, NULL, FFMIN(frame->height, ff_filter_get_nb_threads(avctx))))
144 return ff_filter_frame(avctx->outputs[0], frame);
147 static av_cold int config_output(AVFilterLink *outlink)
149 AVFilterContext *avctx = outlink->src;
150 ColorkeyContext *ctx = avctx->priv;
151 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(outlink->format);
154 outlink->w = avctx->inputs[0]->w;
155 outlink->h = avctx->inputs[0]->h;
156 outlink->time_base = avctx->inputs[0]->time_base;
158 for (i = 0; i < 4; ++i)
159 ctx->co[i] = desc->comp[i].offset;
164 static av_cold int query_formats(AVFilterContext *avctx)
166 static const enum AVPixelFormat pixel_fmts[] = {
174 AVFilterFormats *formats = NULL;
176 formats = ff_make_format_list(pixel_fmts);
178 return AVERROR(ENOMEM);
180 return ff_set_common_formats(avctx, formats);
183 static const AVFilterPad colorkey_inputs[] = {
186 .type = AVMEDIA_TYPE_VIDEO,
187 .filter_frame = filter_frame,
192 static const AVFilterPad colorkey_outputs[] = {
195 .type = AVMEDIA_TYPE_VIDEO,
196 .config_props = config_output,
201 #define OFFSET(x) offsetof(ColorkeyContext, x)
202 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
204 #if CONFIG_COLORKEY_FILTER
206 static const AVOption colorkey_options[] = {
207 { "color", "set the colorkey key color", OFFSET(colorkey_rgba), AV_OPT_TYPE_COLOR, { .str = "black" }, 0, 0, FLAGS },
208 { "similarity", "set the colorkey similarity value", OFFSET(similarity), AV_OPT_TYPE_FLOAT, { .dbl = 0.01 }, 0.01, 1.0, FLAGS },
209 { "blend", "set the colorkey key blend value", OFFSET(blend), AV_OPT_TYPE_FLOAT, { .dbl = 0.0 }, 0.0, 1.0, FLAGS },
213 AVFILTER_DEFINE_CLASS(colorkey);
215 const AVFilter ff_vf_colorkey = {
217 .description = NULL_IF_CONFIG_SMALL("Turns a certain color into transparency. Operates on RGB colors."),
218 .priv_size = sizeof(ColorkeyContext),
219 .priv_class = &colorkey_class,
220 .query_formats = query_formats,
222 .inputs = colorkey_inputs,
223 .outputs = colorkey_outputs,
224 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS,
225 .process_command = ff_filter_process_command,
228 #endif /* CONFIG_COLORKEY_FILTER */
229 #if CONFIG_COLORHOLD_FILTER
231 static const AVOption colorhold_options[] = {
232 { "color", "set the colorhold key color", OFFSET(colorkey_rgba), AV_OPT_TYPE_COLOR, { .str = "black" }, 0, 0, FLAGS },
233 { "similarity", "set the colorhold similarity value", OFFSET(similarity), AV_OPT_TYPE_FLOAT, { .dbl = 0.01 }, 0.01, 1.0, FLAGS },
234 { "blend", "set the colorhold blend value", OFFSET(blend), AV_OPT_TYPE_FLOAT, { .dbl = 0.0 }, 0.0, 1.0, FLAGS },
238 AVFILTER_DEFINE_CLASS(colorhold);
240 const AVFilter ff_vf_colorhold = {
242 .description = NULL_IF_CONFIG_SMALL("Turns a certain color range into gray. Operates on RGB colors."),
243 .priv_size = sizeof(ColorkeyContext),
244 .priv_class = &colorhold_class,
245 .query_formats = query_formats,
247 .inputs = colorkey_inputs,
248 .outputs = colorkey_outputs,
249 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS,
250 .process_command = ff_filter_process_command,
253 #endif /* CONFIG_COLORHOLD_FILTER */