Signed-off-by: Timo Rothenpieler <timo@rothenpieler.org>
Timo Rothenpieler authored on 2015/09/24 01:06:46... | ... |
@@ -359,6 +359,7 @@ Filters: |
359 | 359 |
avf_avectorscope.c Paul B Mahol |
360 | 360 |
avf_showcqt.c Muhammad Faiz |
361 | 361 |
vf_blend.c Paul B Mahol |
362 |
+ vf_chromakey.c Timo Rothenpieler |
|
362 | 363 |
vf_colorchannelmixer.c Paul B Mahol |
363 | 364 |
vf_colorbalance.c Paul B Mahol |
364 | 365 |
vf_colorkey.c Timo Rothenpieler |
... | ... |
@@ -3707,6 +3707,51 @@ boxblur=luma_radius=min(h\,w)/10:luma_power=1:chroma_radius=min(cw\,ch)/10:chrom |
3707 | 3707 |
@end example |
3708 | 3708 |
@end itemize |
3709 | 3709 |
|
3710 |
+@section chromakey |
|
3711 |
+YUV colorspace color/chroma keying. |
|
3712 |
+ |
|
3713 |
+The filter accepts the following options: |
|
3714 |
+ |
|
3715 |
+@table @option |
|
3716 |
+@item color |
|
3717 |
+The color which will be replaced with transparency. |
|
3718 |
+ |
|
3719 |
+@item similarity |
|
3720 |
+Similarity percentage with the key color. |
|
3721 |
+ |
|
3722 |
+0.01 matches only the exact key color, while 1.0 matches everything. |
|
3723 |
+ |
|
3724 |
+@item blend |
|
3725 |
+Blend percentage. |
|
3726 |
+ |
|
3727 |
+0.0 makes pixels either fully transparent, or not transparent at all. |
|
3728 |
+ |
|
3729 |
+Higher values result in semi-transparent pixels, with a higher transparency |
|
3730 |
+the more similar the pixels color is to the key color. |
|
3731 |
+ |
|
3732 |
+@item yuv |
|
3733 |
+Signals that the color passed is already in YUV instead of RGB. |
|
3734 |
+ |
|
3735 |
+Litteral colors like "green" or "red" don't make sense with this enabled anymore. |
|
3736 |
+This can be used to pass exact YUV values as hexadecimal numbers. |
|
3737 |
+@end table |
|
3738 |
+ |
|
3739 |
+@subsection Examples |
|
3740 |
+ |
|
3741 |
+@itemize |
|
3742 |
+@item |
|
3743 |
+Make every green pixel in the input image transparent: |
|
3744 |
+@example |
|
3745 |
+ffmpeg -i input.png -vf chromakey=green out.png |
|
3746 |
+@end example |
|
3747 |
+ |
|
3748 |
+@item |
|
3749 |
+Overlay a greenscreen-video on top of a static black background. |
|
3750 |
+@example |
|
3751 |
+ffmpeg -f lavfi -i color=c=black:s=1280x720 -i video.mp4 -shortest -filter_complex "[1:v]chromakey=0x70de77:0.1:0.2[ckout];[0:v][ckout]overlay[out]" -map "[out]" output.mkv |
|
3752 |
+@end example |
|
3753 |
+@end itemize |
|
3754 |
+ |
|
3710 | 3755 |
@section codecview |
3711 | 3756 |
|
3712 | 3757 |
Visualize information exported by some codecs. |
... | ... |
@@ -105,6 +105,7 @@ OBJS-$(CONFIG_BLACKDETECT_FILTER) += vf_blackdetect.o |
105 | 105 |
OBJS-$(CONFIG_BLACKFRAME_FILTER) += vf_blackframe.o |
106 | 106 |
OBJS-$(CONFIG_BLEND_FILTER) += vf_blend.o dualinput.o framesync.o |
107 | 107 |
OBJS-$(CONFIG_BOXBLUR_FILTER) += vf_boxblur.o |
108 |
+OBJS-$(CONFIG_CHROMAKEY_FILTER) += vf_chromakey.o |
|
108 | 109 |
OBJS-$(CONFIG_CODECVIEW_FILTER) += vf_codecview.o |
109 | 110 |
OBJS-$(CONFIG_COLORBALANCE_FILTER) += vf_colorbalance.o |
110 | 111 |
OBJS-$(CONFIG_COLORCHANNELMIXER_FILTER) += vf_colorchannelmixer.o |
... | ... |
@@ -127,6 +127,7 @@ void avfilter_register_all(void) |
127 | 127 |
REGISTER_FILTER(BLACKFRAME, blackframe, vf); |
128 | 128 |
REGISTER_FILTER(BLEND, blend, vf); |
129 | 129 |
REGISTER_FILTER(BOXBLUR, boxblur, vf); |
130 |
+ REGISTER_FILTER(CHROMAKEY, chromakey, vf); |
|
130 | 131 |
REGISTER_FILTER(CODECVIEW, codecview, vf); |
131 | 132 |
REGISTER_FILTER(COLORBALANCE, colorbalance, vf); |
132 | 133 |
REGISTER_FILTER(COLORCHANNELMIXER, colorchannelmixer, vf); |
133 | 134 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,199 @@ |
0 |
+/* |
|
1 |
+ * Copyright (c) 2015 Timo Rothenpieler <timo@rothenpieler.org> |
|
2 |
+ * |
|
3 |
+ * This file is part of FFmpeg. |
|
4 |
+ * |
|
5 |
+ * FFmpeg is free software; you can redistribute it and/or |
|
6 |
+ * modify it under the terms of the GNU Lesser General Public |
|
7 |
+ * License as published by the Free Software Foundation; either |
|
8 |
+ * version 2.1 of the License, or (at your option) any later version. |
|
9 |
+ * |
|
10 |
+ * FFmpeg is distributed in the hope that it will be useful, |
|
11 |
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 |
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
13 |
+ * Lesser General Public License for more details. |
|
14 |
+ * |
|
15 |
+ * You should have received a copy of the GNU Lesser General Public |
|
16 |
+ * License along with FFmpeg; if not, write to the Free Software |
|
17 |
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
|
18 |
+ */ |
|
19 |
+ |
|
20 |
+#include "libavutil/opt.h" |
|
21 |
+#include "libavutil/imgutils.h" |
|
22 |
+#include "avfilter.h" |
|
23 |
+#include "formats.h" |
|
24 |
+#include "internal.h" |
|
25 |
+#include "video.h" |
|
26 |
+ |
|
27 |
+typedef struct ChromakeyContext { |
|
28 |
+ const AVClass *class; |
|
29 |
+ |
|
30 |
+ uint8_t chromakey_rgba[4]; |
|
31 |
+ uint8_t chromakey_uv[2]; |
|
32 |
+ |
|
33 |
+ float similarity; |
|
34 |
+ float blend; |
|
35 |
+ |
|
36 |
+ int is_yuv; |
|
37 |
+} ChromakeyContext; |
|
38 |
+ |
|
39 |
+static uint8_t do_chromakey_pixel(ChromakeyContext *ctx, uint8_t u[9], uint8_t v[9]) |
|
40 |
+{ |
|
41 |
+ double diff = 0.0; |
|
42 |
+ int du, dv, i; |
|
43 |
+ |
|
44 |
+ for (i = 0; i < 9; ++i) { |
|
45 |
+ du = (int)u[i] - ctx->chromakey_uv[0]; |
|
46 |
+ dv = (int)v[i] - ctx->chromakey_uv[1]; |
|
47 |
+ |
|
48 |
+ diff += sqrt((du * du + dv * dv) / (255.0 * 255.0)); |
|
49 |
+ } |
|
50 |
+ |
|
51 |
+ diff /= 9.0; |
|
52 |
+ |
|
53 |
+ if (ctx->blend > 0.0001) { |
|
54 |
+ return av_clipd((diff - ctx->similarity) / ctx->blend, 0.0, 1.0) * 255.0; |
|
55 |
+ } else { |
|
56 |
+ return (diff > ctx->similarity) ? 255 : 0; |
|
57 |
+ } |
|
58 |
+} |
|
59 |
+ |
|
60 |
+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) |
|
61 |
+{ |
|
62 |
+ if (x < 0 || x >= frame->width || y < 0 || y >= frame->height) |
|
63 |
+ return; |
|
64 |
+ |
|
65 |
+ x >>= hsub_log2; |
|
66 |
+ y >>= vsub_log2; |
|
67 |
+ |
|
68 |
+ *u = frame->data[1][frame->linesize[1] * y + x]; |
|
69 |
+ *v = frame->data[2][frame->linesize[2] * y + x]; |
|
70 |
+} |
|
71 |
+ |
|
72 |
+static int do_chromakey_slice(AVFilterContext *avctx, void *arg, int jobnr, int nb_jobs) |
|
73 |
+{ |
|
74 |
+ AVFrame *frame = arg; |
|
75 |
+ |
|
76 |
+ const int slice_start = (frame->height * jobnr) / nb_jobs; |
|
77 |
+ const int slice_end = (frame->height * (jobnr + 1)) / nb_jobs; |
|
78 |
+ |
|
79 |
+ ChromakeyContext *ctx = avctx->priv; |
|
80 |
+ |
|
81 |
+ int hsub_log2 = 0, vsub_log2 = 0; |
|
82 |
+ int x, y, xo, yo; |
|
83 |
+ uint8_t u[9], v[9]; |
|
84 |
+ |
|
85 |
+ memset(u, ctx->chromakey_uv[0], sizeof(u)); |
|
86 |
+ memset(v, ctx->chromakey_uv[1], sizeof(v)); |
|
87 |
+ |
|
88 |
+ if (frame->format == AV_PIX_FMT_YUVA420P || frame->format == AV_PIX_FMT_YUVA422P) |
|
89 |
+ hsub_log2 = 1; |
|
90 |
+ |
|
91 |
+ if (frame->format == AV_PIX_FMT_YUVA420P) |
|
92 |
+ vsub_log2 = 1; |
|
93 |
+ |
|
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, hsub_log2, vsub_log2, x + xo - 1, y + yo - 1, &u[yo * 3 + xo], &v[yo * 3 + xo]); |
|
99 |
+ } |
|
100 |
+ } |
|
101 |
+ |
|
102 |
+ frame->data[3][frame->linesize[3] * y + x] = do_chromakey_pixel(ctx, u, v); |
|
103 |
+ } |
|
104 |
+ } |
|
105 |
+ |
|
106 |
+ return 0; |
|
107 |
+} |
|
108 |
+ |
|
109 |
+static int filter_frame(AVFilterLink *link, AVFrame *frame) |
|
110 |
+{ |
|
111 |
+ AVFilterContext *avctx = link->dst; |
|
112 |
+ int res; |
|
113 |
+ |
|
114 |
+ if (res = avctx->internal->execute(avctx, do_chromakey_slice, frame, NULL, FFMIN(frame->height, avctx->graph->nb_threads))) |
|
115 |
+ return res; |
|
116 |
+ |
|
117 |
+ return ff_filter_frame(avctx->outputs[0], frame); |
|
118 |
+} |
|
119 |
+ |
|
120 |
+#define FIXNUM(x) lrint((x) * (1 << 10)) |
|
121 |
+#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) |
|
122 |
+#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) |
|
123 |
+ |
|
124 |
+static av_cold int initialize_chromakey(AVFilterContext *avctx) |
|
125 |
+{ |
|
126 |
+ ChromakeyContext *ctx = avctx->priv; |
|
127 |
+ |
|
128 |
+ if (ctx->is_yuv) { |
|
129 |
+ ctx->chromakey_uv[0] = ctx->chromakey_rgba[1]; |
|
130 |
+ ctx->chromakey_uv[1] = ctx->chromakey_rgba[2]; |
|
131 |
+ } else { |
|
132 |
+ ctx->chromakey_uv[0] = RGB_TO_U(ctx->chromakey_rgba); |
|
133 |
+ ctx->chromakey_uv[1] = RGB_TO_V(ctx->chromakey_rgba); |
|
134 |
+ } |
|
135 |
+ |
|
136 |
+ return 0; |
|
137 |
+} |
|
138 |
+ |
|
139 |
+static av_cold int query_formats(AVFilterContext *avctx) |
|
140 |
+{ |
|
141 |
+ static const enum AVPixelFormat pixel_fmts[] = { |
|
142 |
+ AV_PIX_FMT_YUVA420P, |
|
143 |
+ AV_PIX_FMT_YUVA422P, |
|
144 |
+ AV_PIX_FMT_YUVA444P, |
|
145 |
+ AV_PIX_FMT_NONE |
|
146 |
+ }; |
|
147 |
+ |
|
148 |
+ AVFilterFormats *formats = NULL; |
|
149 |
+ |
|
150 |
+ formats = ff_make_format_list(pixel_fmts); |
|
151 |
+ if (!formats) |
|
152 |
+ return AVERROR(ENOMEM); |
|
153 |
+ |
|
154 |
+ return ff_set_common_formats(avctx, formats); |
|
155 |
+} |
|
156 |
+ |
|
157 |
+static const AVFilterPad chromakey_inputs[] = { |
|
158 |
+ { |
|
159 |
+ .name = "default", |
|
160 |
+ .type = AVMEDIA_TYPE_VIDEO, |
|
161 |
+ .needs_writable = 1, |
|
162 |
+ .filter_frame = filter_frame, |
|
163 |
+ }, |
|
164 |
+ { NULL } |
|
165 |
+}; |
|
166 |
+ |
|
167 |
+static const AVFilterPad chromakey_outputs[] = { |
|
168 |
+ { |
|
169 |
+ .name = "default", |
|
170 |
+ .type = AVMEDIA_TYPE_VIDEO, |
|
171 |
+ }, |
|
172 |
+ { NULL } |
|
173 |
+}; |
|
174 |
+ |
|
175 |
+#define OFFSET(x) offsetof(ChromakeyContext, x) |
|
176 |
+#define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM |
|
177 |
+ |
|
178 |
+static const AVOption chromakey_options[] = { |
|
179 |
+ { "color", "set the chromakey key color", OFFSET(chromakey_rgba), AV_OPT_TYPE_COLOR, { .str = "black" }, CHAR_MIN, CHAR_MAX, FLAGS }, |
|
180 |
+ { "similarity", "set the chromakey similarity value", OFFSET(similarity), AV_OPT_TYPE_FLOAT, { .dbl = 0.01 }, 0.01, 1.0, FLAGS }, |
|
181 |
+ { "blend", "set the chromakey key blend value", OFFSET(blend), AV_OPT_TYPE_FLOAT, { .dbl = 0.0 }, 0.0, 1.0, FLAGS }, |
|
182 |
+ { "yuv", "color parameter is in yuv instead of rgb", OFFSET(is_yuv), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, FLAGS }, |
|
183 |
+ { NULL } |
|
184 |
+}; |
|
185 |
+ |
|
186 |
+AVFILTER_DEFINE_CLASS(chromakey); |
|
187 |
+ |
|
188 |
+AVFilter ff_vf_chromakey = { |
|
189 |
+ .name = "chromakey", |
|
190 |
+ .description = NULL_IF_CONFIG_SMALL("Turns a certain color into transparency. Operates on YUV colors."), |
|
191 |
+ .priv_size = sizeof(ChromakeyContext), |
|
192 |
+ .priv_class = &chromakey_class, |
|
193 |
+ .init = initialize_chromakey, |
|
194 |
+ .query_formats = query_formats, |
|
195 |
+ .inputs = chromakey_inputs, |
|
196 |
+ .outputs = chromakey_outputs, |
|
197 |
+ .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS, |
|
198 |
+}; |