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"
23 #include "libavutil/intreadwrite.h"
29 typedef struct ChromakeyContext {
32 uint8_t chromakey_rgba[4];
33 uint16_t chromakey_uv[2];
46 int (*do_slice)(AVFilterContext *ctx, void *arg,
47 int jobnr, int nb_jobs);
50 static uint8_t do_chromakey_pixel(ChromakeyContext *ctx, uint8_t u[9], uint8_t v[9])
55 for (i = 0; i < 9; ++i) {
56 du = (int)u[i] - ctx->chromakey_uv[0];
57 dv = (int)v[i] - ctx->chromakey_uv[1];
59 diff += sqrt((du * du + dv * dv) / (255.0 * 255.0 * 2));
64 if (ctx->blend > 0.0001) {
65 return av_clipd((diff - ctx->similarity) / ctx->blend, 0.0, 1.0) * 255.0;
67 return (diff > ctx->similarity) ? 255 : 0;
71 static uint16_t do_chromakey_pixel16(ChromakeyContext *ctx, uint16_t u[9], uint16_t v[9])
73 double max = ctx->max;
77 for (i = 0; i < 9; ++i) {
78 du = (int)u[i] - ctx->chromakey_uv[0];
79 dv = (int)v[i] - ctx->chromakey_uv[1];
81 diff += sqrt((du * du + dv * dv) / (max * max * 2));
86 if (ctx->blend > 0.0001) {
87 return av_clipd((diff - ctx->similarity) / ctx->blend, 0.0, 1.0) * max;
89 return (diff > ctx->similarity) ? max : 0;
93 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)
95 if (x < 0 || x >= frame->width || y < 0 || y >= frame->height)
101 *u = frame->data[1][frame->linesize[1] * y + x];
102 *v = frame->data[2][frame->linesize[2] * y + x];
105 static av_always_inline void get_pixel16_uv(AVFrame *frame, int hsub_log2, int vsub_log2, int x, int y, uint16_t *u, uint16_t *v)
107 if (x < 0 || x >= frame->width || y < 0 || y >= frame->height)
113 *u = AV_RN16(&frame->data[1][frame->linesize[1] * y + 2 * x]);
114 *v = AV_RN16(&frame->data[2][frame->linesize[2] * y + 2 * x]);
117 static int do_chromakey_slice(AVFilterContext *avctx, void *arg, int jobnr, int nb_jobs)
119 AVFrame *frame = arg;
121 const int slice_start = (frame->height * jobnr) / nb_jobs;
122 const int slice_end = (frame->height * (jobnr + 1)) / nb_jobs;
124 ChromakeyContext *ctx = avctx->priv;
129 memset(u, ctx->chromakey_uv[0], sizeof(u));
130 memset(v, ctx->chromakey_uv[1], sizeof(v));
132 for (y = slice_start; y < slice_end; ++y) {
133 for (x = 0; x < frame->width; ++x) {
134 for (yo = 0; yo < 3; ++yo) {
135 for (xo = 0; xo < 3; ++xo) {
136 get_pixel_uv(frame, ctx->hsub_log2, ctx->vsub_log2, x + xo - 1, y + yo - 1, &u[yo * 3 + xo], &v[yo * 3 + xo]);
140 frame->data[3][frame->linesize[3] * y + x] = do_chromakey_pixel(ctx, u, v);
147 static int do_chromakey16_slice(AVFilterContext *avctx, void *arg, int jobnr, int nb_jobs)
149 AVFrame *frame = arg;
151 const int slice_start = (frame->height * jobnr) / nb_jobs;
152 const int slice_end = (frame->height * (jobnr + 1)) / nb_jobs;
154 ChromakeyContext *ctx = avctx->priv;
159 for (int i = 0; i < 9; i++) {
160 u[i] = ctx->chromakey_uv[0];
161 v[i] = ctx->chromakey_uv[1];
164 for (y = slice_start; y < slice_end; ++y) {
165 for (x = 0; x < frame->width; ++x) {
166 uint16_t *dst = (uint16_t *)(frame->data[3] + frame->linesize[3] * y);
168 for (yo = 0; yo < 3; ++yo) {
169 for (xo = 0; xo < 3; ++xo) {
170 get_pixel16_uv(frame, ctx->hsub_log2, ctx->vsub_log2, x + xo - 1, y + yo - 1, &u[yo * 3 + xo], &v[yo * 3 + xo]);
174 dst[x] = do_chromakey_pixel16(ctx, u, v);
181 static int do_chromahold_slice(AVFilterContext *avctx, void *arg, int jobnr, int nb_jobs)
183 ChromakeyContext *ctx = avctx->priv;
184 AVFrame *frame = arg;
185 const int slice_start = ((frame->height >> ctx->vsub_log2) * jobnr) / nb_jobs;
186 const int slice_end = ((frame->height >> ctx->vsub_log2) * (jobnr + 1)) / nb_jobs;
190 for (y = slice_start; y < slice_end; ++y) {
191 for (x = 0; x < frame->width >> ctx->hsub_log2; ++x) {
192 int u = frame->data[1][frame->linesize[1] * y + x];
193 int v = frame->data[2][frame->linesize[2] * y + x];
197 du = u - ctx->chromakey_uv[0];
198 dv = v - ctx->chromakey_uv[1];
200 diff = sqrt((du * du + dv * dv) / (255.0 * 255.0));
202 alpha = diff > ctx->similarity;
203 if (ctx->blend > 0.0001) {
204 double f = 1. - av_clipd((diff - ctx->similarity) / ctx->blend, 0.0, 1.0);
206 frame->data[1][frame->linesize[1] * y + x] = 128 + (u - 128) * f;
207 frame->data[2][frame->linesize[2] * y + x] = 128 + (v - 128) * f;
209 frame->data[1][frame->linesize[1] * y + x] = 128;
210 frame->data[2][frame->linesize[2] * y + x] = 128;
218 static int do_chromahold16_slice(AVFilterContext *avctx, void *arg, int jobnr, int nb_jobs)
220 ChromakeyContext *ctx = avctx->priv;
221 AVFrame *frame = arg;
222 const int slice_start = ((frame->height >> ctx->vsub_log2) * jobnr) / nb_jobs;
223 const int slice_end = ((frame->height >> ctx->vsub_log2) * (jobnr + 1)) / nb_jobs;
224 const int mid = ctx->mid;
225 double max = ctx->max;
229 for (y = slice_start; y < slice_end; ++y) {
230 for (x = 0; x < frame->width >> ctx->hsub_log2; ++x) {
231 int u = AV_RN16(&frame->data[1][frame->linesize[1] * y + 2 * x]);
232 int v = AV_RN16(&frame->data[2][frame->linesize[2] * y + 2 * x]);
236 du = u - ctx->chromakey_uv[0];
237 dv = v - ctx->chromakey_uv[1];
239 diff = sqrt((du * du + dv * dv) / (max * max));
241 alpha = diff > ctx->similarity;
242 if (ctx->blend > 0.0001) {
243 double f = 1. - av_clipd((diff - ctx->similarity) / ctx->blend, 0.0, 1.0);
245 AV_WN16(&frame->data[1][frame->linesize[1] * y + 2 * x], mid + (u - mid) * f);
246 AV_WN16(&frame->data[2][frame->linesize[2] * y + 2 * x], mid + (v - mid) * f);
248 AV_WN16(&frame->data[1][frame->linesize[1] * y + 2 * x], mid);
249 AV_WN16(&frame->data[2][frame->linesize[2] * y + 2 * x], mid);
257 static int filter_frame(AVFilterLink *link, AVFrame *frame)
259 AVFilterContext *avctx = link->dst;
260 ChromakeyContext *ctx = avctx->priv;
263 if (res = avctx->internal->execute(avctx, ctx->do_slice, frame, NULL, FFMIN(frame->height, ff_filter_get_nb_threads(avctx))))
266 return ff_filter_frame(avctx->outputs[0], frame);
269 #define FIXNUM(x) lrint((x) * (1 << 10))
270 #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)
271 #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)
273 static av_cold int config_output(AVFilterLink *outlink)
275 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(outlink->format);
276 AVFilterContext *avctx = outlink->src;
277 ChromakeyContext *ctx = avctx->priv;
280 ctx->depth = desc->comp[0].depth;
281 ctx->mid = 1 << (ctx->depth - 1);
282 ctx->max = (1 << ctx->depth) - 1;
284 factor = 1 << (ctx->depth - 8);
287 ctx->chromakey_uv[0] = ctx->chromakey_rgba[1] * factor;
288 ctx->chromakey_uv[1] = ctx->chromakey_rgba[2] * factor;
290 ctx->chromakey_uv[0] = RGB_TO_U(ctx->chromakey_rgba) * factor;
291 ctx->chromakey_uv[1] = RGB_TO_V(ctx->chromakey_rgba) * factor;
294 if (!strcmp(avctx->filter->name, "chromakey")) {
295 ctx->do_slice = ctx->depth <= 8 ? do_chromakey_slice : do_chromakey16_slice;
297 ctx->do_slice = ctx->depth <= 8 ? do_chromahold_slice: do_chromahold16_slice;
303 static av_cold int query_formats(AVFilterContext *avctx)
305 static const enum AVPixelFormat pixel_fmts[] = {
309 AV_PIX_FMT_YUVA420P9, AV_PIX_FMT_YUVA422P9, AV_PIX_FMT_YUVA444P9,
310 AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA444P10,
311 AV_PIX_FMT_YUVA422P12, AV_PIX_FMT_YUVA444P12,
312 AV_PIX_FMT_YUVA420P16, AV_PIX_FMT_YUVA422P16, AV_PIX_FMT_YUVA444P16,
316 static const enum AVPixelFormat hold_pixel_fmts[] = {
323 AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV444P9,
324 AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10,
325 AV_PIX_FMT_YUV444P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV420P12,
326 AV_PIX_FMT_YUV444P14, AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV420P14,
327 AV_PIX_FMT_YUV420P16, AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV444P16,
328 AV_PIX_FMT_YUVA420P9, AV_PIX_FMT_YUVA422P9, AV_PIX_FMT_YUVA444P9,
329 AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA444P10,
330 AV_PIX_FMT_YUVA422P12, AV_PIX_FMT_YUVA444P12,
331 AV_PIX_FMT_YUVA420P16, AV_PIX_FMT_YUVA422P16, AV_PIX_FMT_YUVA444P16,
335 AVFilterFormats *formats = NULL;
337 formats = ff_make_format_list(!strcmp(avctx->filter->name, "chromahold") ? hold_pixel_fmts : pixel_fmts);
339 return AVERROR(ENOMEM);
341 return ff_set_common_formats(avctx, formats);
344 static av_cold int config_input(AVFilterLink *inlink)
346 AVFilterContext *avctx = inlink->dst;
347 ChromakeyContext *ctx = avctx->priv;
348 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
350 ctx->hsub_log2 = desc->log2_chroma_w;
351 ctx->vsub_log2 = desc->log2_chroma_h;
356 static int process_command(AVFilterContext *ctx, const char *cmd, const char *args,
357 char *res, int res_len, int flags)
361 ret = ff_filter_process_command(ctx, cmd, args, res, res_len, flags);
365 return config_output(ctx->outputs[0]);
368 static const AVFilterPad chromakey_inputs[] = {
371 .type = AVMEDIA_TYPE_VIDEO,
373 .filter_frame = filter_frame,
374 .config_props = config_input,
379 static const AVFilterPad chromakey_outputs[] = {
382 .type = AVMEDIA_TYPE_VIDEO,
383 .config_props = config_output,
388 #define OFFSET(x) offsetof(ChromakeyContext, x)
389 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
391 static const AVOption chromakey_options[] = {
392 { "color", "set the chromakey key color", OFFSET(chromakey_rgba), AV_OPT_TYPE_COLOR, { .str = "black" }, 0, 0, FLAGS },
393 { "similarity", "set the chromakey similarity value", OFFSET(similarity), AV_OPT_TYPE_FLOAT, { .dbl = 0.01 }, 0.01, 1.0, FLAGS },
394 { "blend", "set the chromakey key blend value", OFFSET(blend), AV_OPT_TYPE_FLOAT, { .dbl = 0.0 }, 0.0, 1.0, FLAGS },
395 { "yuv", "color parameter is in yuv instead of rgb", OFFSET(is_yuv), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, FLAGS },
399 AVFILTER_DEFINE_CLASS(chromakey);
401 AVFilter ff_vf_chromakey = {
403 .description = NULL_IF_CONFIG_SMALL("Turns a certain color into transparency. Operates on YUV colors."),
404 .priv_size = sizeof(ChromakeyContext),
405 .priv_class = &chromakey_class,
406 .query_formats = query_formats,
407 .inputs = chromakey_inputs,
408 .outputs = chromakey_outputs,
409 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS,
410 .process_command = process_command,
413 static const AVOption chromahold_options[] = {
414 { "color", "set the chromahold key color", OFFSET(chromakey_rgba), AV_OPT_TYPE_COLOR, { .str = "black" }, 0, 0, FLAGS },
415 { "similarity", "set the chromahold similarity value", OFFSET(similarity), AV_OPT_TYPE_FLOAT, { .dbl = 0.01 }, 0.01, 1.0, FLAGS },
416 { "blend", "set the chromahold blend value", OFFSET(blend), AV_OPT_TYPE_FLOAT, { .dbl = 0.0 }, 0.0, 1.0, FLAGS },
417 { "yuv", "color parameter is in yuv instead of rgb", OFFSET(is_yuv), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, FLAGS },
421 static const AVFilterPad chromahold_inputs[] = {
424 .type = AVMEDIA_TYPE_VIDEO,
426 .filter_frame = filter_frame,
427 .config_props = config_input,
432 static const AVFilterPad chromahold_outputs[] = {
435 .type = AVMEDIA_TYPE_VIDEO,
436 .config_props = config_output,
441 AVFILTER_DEFINE_CLASS(chromahold);
443 AVFilter ff_vf_chromahold = {
444 .name = "chromahold",
445 .description = NULL_IF_CONFIG_SMALL("Turns a certain color range into gray."),
446 .priv_size = sizeof(ChromakeyContext),
447 .priv_class = &chromahold_class,
448 .query_formats = query_formats,
449 .inputs = chromahold_inputs,
450 .outputs = chromahold_outputs,
451 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS,
452 .process_command = process_command,