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 ChromakeyContext {
31 uint8_t chromakey_rgba[4];
32 uint8_t chromakey_uv[2];
42 int (*do_slice)(AVFilterContext *ctx, void *arg,
43 int jobnr, int nb_jobs);
46 static uint8_t do_chromakey_pixel(ChromakeyContext *ctx, uint8_t u[9], uint8_t v[9])
51 for (i = 0; i < 9; ++i) {
52 du = (int)u[i] - ctx->chromakey_uv[0];
53 dv = (int)v[i] - ctx->chromakey_uv[1];
55 diff += sqrt((du * du + dv * dv) / (255.0 * 255.0));
60 if (ctx->blend > 0.0001) {
61 return av_clipd((diff - ctx->similarity) / ctx->blend, 0.0, 1.0) * 255.0;
63 return (diff > ctx->similarity) ? 255 : 0;
67 static av_always_inline void get_pixel_uv(AVFrame *frame, int hsub_log2, int vsub_log2, int x, int y, uint8_t *u, uint8_t *v)
69 if (x < 0 || x >= frame->width || y < 0 || y >= frame->height)
75 *u = frame->data[1][frame->linesize[1] * y + x];
76 *v = frame->data[2][frame->linesize[2] * y + x];
79 static int do_chromakey_slice(AVFilterContext *avctx, void *arg, int jobnr, int nb_jobs)
83 const int slice_start = (frame->height * jobnr) / nb_jobs;
84 const int slice_end = (frame->height * (jobnr + 1)) / nb_jobs;
86 ChromakeyContext *ctx = avctx->priv;
91 memset(u, ctx->chromakey_uv[0], sizeof(u));
92 memset(v, ctx->chromakey_uv[1], sizeof(v));
94 for (y = slice_start; y < slice_end; ++y) {
95 for (x = 0; x < frame->width; ++x) {
96 for (yo = 0; yo < 3; ++yo) {
97 for (xo = 0; xo < 3; ++xo) {
98 get_pixel_uv(frame, ctx->hsub_log2, ctx->vsub_log2, x + xo - 1, y + yo - 1, &u[yo * 3 + xo], &v[yo * 3 + xo]);
102 frame->data[3][frame->linesize[3] * y + x] = do_chromakey_pixel(ctx, u, v);
109 static int do_chromahold_slice(AVFilterContext *avctx, void *arg, int jobnr, int nb_jobs)
111 ChromakeyContext *ctx = avctx->priv;
112 AVFrame *frame = arg;
113 const int slice_start = ((frame->height >> ctx->vsub_log2) * jobnr) / nb_jobs;
114 const int slice_end = ((frame->height >> ctx->vsub_log2) * (jobnr + 1)) / nb_jobs;
118 for (y = slice_start; y < slice_end; ++y) {
119 for (x = 0; x < frame->width >> ctx->hsub_log2; ++x) {
120 int u = frame->data[1][frame->linesize[1] * y + x];
121 int v = frame->data[2][frame->linesize[2] * y + x];
125 du = u - ctx->chromakey_uv[0];
126 dv = v - ctx->chromakey_uv[1];
128 diff = sqrt((du * du + dv * dv) / (255.0 * 255.0));
130 alpha = diff > ctx->similarity;
132 frame->data[1][frame->linesize[1] * y + x] = 128;
133 frame->data[2][frame->linesize[2] * y + x] = 128;
141 static int filter_frame(AVFilterLink *link, AVFrame *frame)
143 AVFilterContext *avctx = link->dst;
144 ChromakeyContext *ctx = avctx->priv;
147 if (res = avctx->internal->execute(avctx, ctx->do_slice, frame, NULL, FFMIN(frame->height, ff_filter_get_nb_threads(avctx))))
150 return ff_filter_frame(avctx->outputs[0], frame);
153 #define FIXNUM(x) lrint((x) * (1 << 10))
154 #define RGB_TO_U(rgb) (((- FIXNUM(0.16874) * rgb[0] - FIXNUM(0.33126) * rgb[1] + FIXNUM(0.50000) * rgb[2] + (1 << 9) - 1) >> 10) + 128)
155 #define RGB_TO_V(rgb) ((( FIXNUM(0.50000) * rgb[0] - FIXNUM(0.41869) * rgb[1] - FIXNUM(0.08131) * rgb[2] + (1 << 9) - 1) >> 10) + 128)
157 static av_cold int initialize_chromakey(AVFilterContext *avctx)
159 ChromakeyContext *ctx = avctx->priv;
162 ctx->chromakey_uv[0] = ctx->chromakey_rgba[1];
163 ctx->chromakey_uv[1] = ctx->chromakey_rgba[2];
165 ctx->chromakey_uv[0] = RGB_TO_U(ctx->chromakey_rgba);
166 ctx->chromakey_uv[1] = RGB_TO_V(ctx->chromakey_rgba);
169 if (!strcmp(avctx->filter->name, "chromakey")) {
170 ctx->do_slice = do_chromakey_slice;
172 ctx->do_slice = do_chromahold_slice;
178 static av_cold int query_formats(AVFilterContext *avctx)
180 static const enum AVPixelFormat pixel_fmts[] = {
187 static const enum AVPixelFormat hold_pixel_fmts[] = {
197 AVFilterFormats *formats = NULL;
199 formats = ff_make_format_list(!strcmp(avctx->filter->name, "chromahold") ? hold_pixel_fmts : pixel_fmts);
201 return AVERROR(ENOMEM);
203 return ff_set_common_formats(avctx, formats);
206 static av_cold int config_input(AVFilterLink *inlink)
208 AVFilterContext *avctx = inlink->dst;
209 ChromakeyContext *ctx = avctx->priv;
210 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
212 ctx->hsub_log2 = desc->log2_chroma_w;
213 ctx->vsub_log2 = desc->log2_chroma_h;
218 static const AVFilterPad chromakey_inputs[] = {
221 .type = AVMEDIA_TYPE_VIDEO,
223 .filter_frame = filter_frame,
224 .config_props = config_input,
229 static const AVFilterPad chromakey_outputs[] = {
232 .type = AVMEDIA_TYPE_VIDEO,
237 #define OFFSET(x) offsetof(ChromakeyContext, x)
238 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
240 static const AVOption chromakey_options[] = {
241 { "color", "set the chromakey key color", OFFSET(chromakey_rgba), AV_OPT_TYPE_COLOR, { .str = "black" }, CHAR_MIN, CHAR_MAX, FLAGS },
242 { "similarity", "set the chromakey similarity value", OFFSET(similarity), AV_OPT_TYPE_FLOAT, { .dbl = 0.01 }, 0.01, 1.0, FLAGS },
243 { "blend", "set the chromakey key blend value", OFFSET(blend), AV_OPT_TYPE_FLOAT, { .dbl = 0.0 }, 0.0, 1.0, FLAGS },
244 { "yuv", "color parameter is in yuv instead of rgb", OFFSET(is_yuv), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, FLAGS },
248 AVFILTER_DEFINE_CLASS(chromakey);
250 AVFilter ff_vf_chromakey = {
252 .description = NULL_IF_CONFIG_SMALL("Turns a certain color into transparency. Operates on YUV colors."),
253 .priv_size = sizeof(ChromakeyContext),
254 .priv_class = &chromakey_class,
255 .init = initialize_chromakey,
256 .query_formats = query_formats,
257 .inputs = chromakey_inputs,
258 .outputs = chromakey_outputs,
259 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS,
262 static const AVOption chromahold_options[] = {
263 { "color", "set the chromahold key color", OFFSET(chromakey_rgba), AV_OPT_TYPE_COLOR, { .str = "black" }, CHAR_MIN, CHAR_MAX, FLAGS },
264 { "similarity", "set the chromahold similarity value", OFFSET(similarity), AV_OPT_TYPE_FLOAT, { .dbl = 0.01 }, 0.01, 1.0, FLAGS },
265 { "yuv", "color parameter is in yuv instead of rgb", OFFSET(is_yuv), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, FLAGS },
269 static const AVFilterPad chromahold_inputs[] = {
272 .type = AVMEDIA_TYPE_VIDEO,
274 .filter_frame = filter_frame,
275 .config_props = config_input,
280 static const AVFilterPad chromahold_outputs[] = {
283 .type = AVMEDIA_TYPE_VIDEO,
288 AVFILTER_DEFINE_CLASS(chromahold);
290 AVFilter ff_vf_chromahold = {
291 .name = "chromahold",
292 .description = NULL_IF_CONFIG_SMALL("Turns a certain color range into gray."),
293 .priv_size = sizeof(ChromakeyContext),
294 .priv_class = &chromahold_class,
295 .init = initialize_chromakey,
296 .query_formats = query_formats,
297 .inputs = chromahold_inputs,
298 .outputs = chromahold_outputs,
299 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS,