]> git.sesse.net Git - ffmpeg/blob - libavfilter/vf_chromakey.c
lavfi/vf_w3fdif: reindent after last commit.
[ffmpeg] / libavfilter / vf_chromakey.c
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 ChromakeyContext {
29     const AVClass *class;
30
31     uint8_t chromakey_rgba[4];
32     uint8_t chromakey_uv[2];
33
34     float similarity;
35     float blend;
36
37     int is_yuv;
38 } ChromakeyContext;
39
40 static uint8_t do_chromakey_pixel(ChromakeyContext *ctx, uint8_t u[9], uint8_t v[9])
41 {
42     double diff = 0.0;
43     int du, dv, i;
44
45     for (i = 0; i < 9; ++i) {
46         du = (int)u[i] - ctx->chromakey_uv[0];
47         dv = (int)v[i] - ctx->chromakey_uv[1];
48
49         diff += sqrt((du * du + dv * dv) / (255.0 * 255.0));
50     }
51
52     diff /= 9.0;
53
54     if (ctx->blend > 0.0001) {
55         return av_clipd((diff - ctx->similarity) / ctx->blend, 0.0, 1.0) * 255.0;
56     } else {
57         return (diff > ctx->similarity) ? 255 : 0;
58     }
59 }
60
61 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)
62 {
63     if (x < 0 || x >= frame->width || y < 0 || y >= frame->height)
64         return;
65
66     x >>= hsub_log2;
67     y >>= vsub_log2;
68
69     *u = frame->data[1][frame->linesize[1] * y + x];
70     *v = frame->data[2][frame->linesize[2] * y + x];
71 }
72
73 static int do_chromakey_slice(AVFilterContext *avctx, void *arg, int jobnr, int nb_jobs)
74 {
75     AVFrame *frame = arg;
76
77     const int slice_start = (frame->height * jobnr) / nb_jobs;
78     const int slice_end = (frame->height * (jobnr + 1)) / nb_jobs;
79
80     ChromakeyContext *ctx = avctx->priv;
81
82     int hsub_log2 = 0, vsub_log2 = 0;
83     int x, y, xo, yo;
84     uint8_t u[9], v[9];
85
86     memset(u, ctx->chromakey_uv[0], sizeof(u));
87     memset(v, ctx->chromakey_uv[1], sizeof(v));
88
89     if (frame->format == AV_PIX_FMT_YUVA420P || frame->format == AV_PIX_FMT_YUVA422P)
90         hsub_log2 = 1;
91
92     if (frame->format == AV_PIX_FMT_YUVA420P)
93         vsub_log2 = 1;
94
95     for (y = slice_start; y < slice_end; ++y) {
96         for (x = 0; x < frame->width; ++x) {
97             for (yo = 0; yo < 3; ++yo) {
98                 for (xo = 0; xo < 3; ++xo) {
99                     get_pixel_uv(frame, hsub_log2, vsub_log2, x + xo - 1, y + yo - 1, &u[yo * 3 + xo], &v[yo * 3 + xo]);
100                 }
101             }
102
103             frame->data[3][frame->linesize[3] * y + x] = do_chromakey_pixel(ctx, u, v);
104         }
105     }
106
107     return 0;
108 }
109
110 static int filter_frame(AVFilterLink *link, AVFrame *frame)
111 {
112     AVFilterContext *avctx = link->dst;
113     int res;
114
115     if (res = avctx->internal->execute(avctx, do_chromakey_slice, frame, NULL, FFMIN(frame->height, avctx->graph->nb_threads)))
116         return res;
117
118     return ff_filter_frame(avctx->outputs[0], frame);
119 }
120
121 #define FIXNUM(x) lrint((x) * (1 << 10))
122 #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)
123 #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)
124
125 static av_cold int initialize_chromakey(AVFilterContext *avctx)
126 {
127     ChromakeyContext *ctx = avctx->priv;
128
129     if (ctx->is_yuv) {
130         ctx->chromakey_uv[0] = ctx->chromakey_rgba[1];
131         ctx->chromakey_uv[1] = ctx->chromakey_rgba[2];
132     } else {
133         ctx->chromakey_uv[0] = RGB_TO_U(ctx->chromakey_rgba);
134         ctx->chromakey_uv[1] = RGB_TO_V(ctx->chromakey_rgba);
135     }
136
137     return 0;
138 }
139
140 static av_cold int query_formats(AVFilterContext *avctx)
141 {
142     static const enum AVPixelFormat pixel_fmts[] = {
143         AV_PIX_FMT_YUVA420P,
144         AV_PIX_FMT_YUVA422P,
145         AV_PIX_FMT_YUVA444P,
146         AV_PIX_FMT_NONE
147     };
148
149     AVFilterFormats *formats = NULL;
150
151     formats = ff_make_format_list(pixel_fmts);
152     if (!formats)
153         return AVERROR(ENOMEM);
154
155     return ff_set_common_formats(avctx, formats);
156 }
157
158 static const AVFilterPad chromakey_inputs[] = {
159     {
160         .name           = "default",
161         .type           = AVMEDIA_TYPE_VIDEO,
162         .needs_writable = 1,
163         .filter_frame   = filter_frame,
164     },
165     { NULL }
166 };
167
168 static const AVFilterPad chromakey_outputs[] = {
169     {
170         .name           = "default",
171         .type           = AVMEDIA_TYPE_VIDEO,
172     },
173     { NULL }
174 };
175
176 #define OFFSET(x) offsetof(ChromakeyContext, x)
177 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
178
179 static const AVOption chromakey_options[] = {
180     { "color", "set the chromakey key color", OFFSET(chromakey_rgba), AV_OPT_TYPE_COLOR, { .str = "black" }, CHAR_MIN, CHAR_MAX, FLAGS },
181     { "similarity", "set the chromakey similarity value", OFFSET(similarity), AV_OPT_TYPE_FLOAT, { .dbl = 0.01 }, 0.01, 1.0, FLAGS },
182     { "blend", "set the chromakey key blend value", OFFSET(blend), AV_OPT_TYPE_FLOAT, { .dbl = 0.0 }, 0.0, 1.0, FLAGS },
183     { "yuv", "color parameter is in yuv instead of rgb", OFFSET(is_yuv), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, FLAGS },
184     { NULL }
185 };
186
187 AVFILTER_DEFINE_CLASS(chromakey);
188
189 AVFilter ff_vf_chromakey = {
190     .name          = "chromakey",
191     .description   = NULL_IF_CONFIG_SMALL("Turns a certain color into transparency. Operates on YUV colors."),
192     .priv_size     = sizeof(ChromakeyContext),
193     .priv_class    = &chromakey_class,
194     .init          = initialize_chromakey,
195     .query_formats = query_formats,
196     .inputs        = chromakey_inputs,
197     .outputs       = chromakey_outputs,
198     .flags         = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS,
199 };