summaryrefslogtreecommitdiff
path: root/libavfilter/vf_colorkey.c
blob: 9979f4ad0da853fc17b36d1b7bd3f3b5bde8a4d4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
/*
 * Copyright (c) 2015 Timo Rothenpieler <timo@rothenpieler.org>
 *
 * This file is part of FFmpeg.
 *
 * FFmpeg is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * FFmpeg is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with FFmpeg; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

#include "config_components.h"

#include "libavutil/opt.h"
#include "libavutil/imgutils.h"
#include "avfilter.h"
#include "drawutils.h"
#include "formats.h"
#include "internal.h"
#include "video.h"

typedef struct ColorkeyContext {
    const AVClass *class;

    /* color offsets rgba */
    uint8_t co[4];

    uint8_t colorkey_rgba[4];
    float similarity;
    float blend;
    double scale;
    int depth;
    int max;

    int (*do_slice)(AVFilterContext *ctx, void *arg,
                    int jobnr, int nb_jobs);
} ColorkeyContext;

static int do_colorkey_pixel(const uint8_t *colorkey_rgba, int r, int g, int b,
                             float similarity, float iblend, int max, double scale)
{
    double dr, dg, db, diff;

    dr = r * scale - colorkey_rgba[0];
    dg = g * scale - colorkey_rgba[1];
    db = b * scale - colorkey_rgba[2];

    diff = sqrt((dr * dr + dg * dg + db * db) / (255.0 * 255.0 * 3.0));

    if (iblend < 10000.0) {
        return av_clipd((diff - similarity) * iblend, 0.0, 1.0) * max;
    } else {
        return (diff > similarity) ? max : 0;
    }
}

#define COLORKEY_SLICE(name, type)                                 \
static int do_colorkey_slice##name(AVFilterContext *avctx,         \
                                   void *arg,                      \
                                   int jobnr, int nb_jobs)         \
{                                                                  \
    AVFrame *frame = arg;                                          \
    const int slice_start = (frame->height * jobnr) / nb_jobs;     \
    const int slice_end = (frame->height * (jobnr + 1)) / nb_jobs; \
    ColorkeyContext *ctx = avctx->priv;                            \
    const float similarity = ctx->similarity;                      \
    const float iblend = 1.f / ctx->blend;                         \
    const uint8_t *colorkey_rgba = ctx->colorkey_rgba;             \
    const uint8_t *co = ctx->co;                                   \
    const double scale = ctx->scale;                               \
    const int max = ctx->max;                                      \
                                                                   \
    for (int y = slice_start; y < slice_end; y++) {                \
        type *dst = (type *)(frame->data[0] + y * frame->linesize[0]);\
                                                                   \
        for (int x = 0; x < frame->width; x++) {                   \
            const int o = x * 4;                                   \
                                                                   \
            dst[o + co[3]] = do_colorkey_pixel(colorkey_rgba,      \
                             dst[o + co[0]],                       \
                             dst[o + co[1]],                       \
                             dst[o + co[2]],                       \
                             similarity, iblend, max, scale);      \
        }                                                          \
    }                                                              \
                                                                   \
    return 0;                                                      \
}

COLORKEY_SLICE(8, uint8_t)
COLORKEY_SLICE(16, uint16_t)

#define COLORHOLD_SLICE(name, type, htype)                             \
static int do_colorhold_slice##name(AVFilterContext *avctx, void *arg, \
                              int jobnr, int nb_jobs)                  \
{                                                                      \
    AVFrame *frame = arg;                                              \
    const int slice_start = (frame->height * jobnr) / nb_jobs;         \
    const int slice_end = (frame->height * (jobnr + 1)) / nb_jobs;     \
    ColorkeyContext *ctx = avctx->priv;                                \
    const int depth = ctx->depth;                                      \
    const int max = ctx->max;                                          \
    const int half = max / 2;                                          \
    const uint8_t *co = ctx->co;                                       \
    const uint8_t *colorkey_rgba = ctx->colorkey_rgba;                 \
    const float similarity = ctx->similarity;                          \
    const float iblend = 1.f / ctx->blend;                             \
    const double scale = ctx->scale;                                   \
                                                                       \
    for (int y = slice_start; y < slice_end; ++y) {                    \
        type *dst = (type *)(frame->data[0] + y * frame->linesize[0]); \
                                                                       \
        for (int x = 0; x < frame->width; ++x) {                       \
            int o, t, r, g, b;                                         \
                                                                       \
            o = x * 4;                                                 \
            r = dst[o + co[0]];                                        \
            g = dst[o + co[1]];                                        \
            b = dst[o + co[2]];                                        \
                                                                       \
            t = do_colorkey_pixel(colorkey_rgba, r, g, b,              \
                                  similarity, iblend, max, scale);     \
                                                                       \
            if (t > 0) {                                               \
                htype a = (r + g + b) / 3;                             \
                htype rt = max - t;                                    \
                                                                       \
                dst[o + co[0]] = (a * t + r * rt + half) >> depth;     \
                dst[o + co[1]] = (a * t + g * rt + half) >> depth;     \
                dst[o + co[2]] = (a * t + b * rt + half) >> depth;     \
            }                                                          \
        }                                                              \
    }                                                                  \
                                                                       \
    return 0;                                                          \
}

COLORHOLD_SLICE(8, uint8_t, int)
COLORHOLD_SLICE(16, uint16_t, int64_t)

static int filter_frame(AVFilterLink *link, AVFrame *frame)
{
    AVFilterContext *avctx = link->dst;
    ColorkeyContext *ctx = avctx->priv;
    int res;

    if (res = ff_filter_execute(avctx, ctx->do_slice, frame, NULL,
                                FFMIN(frame->height, ff_filter_get_nb_threads(avctx))))
        return res;

    return ff_filter_frame(avctx->outputs[0], frame);
}

static av_cold int config_output(AVFilterLink *outlink)
{
    AVFilterContext *avctx = outlink->src;
    ColorkeyContext *ctx = avctx->priv;
    const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(avctx->inputs[0]->format);

    ctx->depth = desc->comp[0].depth;
    ctx->max   = (1 << ctx->depth) - 1;
    ctx->scale = 255.0 / ctx->max;
    outlink->w = avctx->inputs[0]->w;
    outlink->h = avctx->inputs[0]->h;
    outlink->time_base = avctx->inputs[0]->time_base;
    ff_fill_rgba_map(ctx->co, outlink->format);

    if (!strcmp(avctx->filter->name, "colorkey")) {
        ctx->do_slice = ctx->max == 255 ? do_colorkey_slice8  : do_colorkey_slice16;
    } else {
        ctx->do_slice = ctx->max == 255 ? do_colorhold_slice8 : do_colorhold_slice16;
    }

    return 0;
}

static const enum AVPixelFormat pixel_fmts[] = {
    AV_PIX_FMT_ARGB,
    AV_PIX_FMT_RGBA,
    AV_PIX_FMT_ABGR,
    AV_PIX_FMT_BGRA,
    AV_PIX_FMT_RGBA64,
    AV_PIX_FMT_BGRA64,
    AV_PIX_FMT_NONE
};

static const AVFilterPad colorkey_inputs[] = {
    {
        .name = "default",
        .type = AVMEDIA_TYPE_VIDEO,
        .flags        = AVFILTERPAD_FLAG_NEEDS_WRITABLE,
        .filter_frame = filter_frame,
    },
};

static const AVFilterPad colorkey_outputs[] = {
    {
        .name = "default",
        .type = AVMEDIA_TYPE_VIDEO,
        .config_props  = config_output,
    },
};

#define OFFSET(x) offsetof(ColorkeyContext, x)
#define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_RUNTIME_PARAM

#if CONFIG_COLORKEY_FILTER

static const AVOption colorkey_options[] = {
    { "color", "set the colorkey key color", OFFSET(colorkey_rgba), AV_OPT_TYPE_COLOR, { .str = "black" }, 0, 0, FLAGS },
    { "similarity", "set the colorkey similarity value", OFFSET(similarity), AV_OPT_TYPE_FLOAT, { .dbl = 0.01 }, 0.00001, 1.0, FLAGS },
    { "blend", "set the colorkey key blend value", OFFSET(blend), AV_OPT_TYPE_FLOAT, { .dbl = 0.0 }, 0.0, 1.0, FLAGS },
    { NULL }
};

AVFILTER_DEFINE_CLASS(colorkey);

const AVFilter ff_vf_colorkey = {
    .name          = "colorkey",
    .description   = NULL_IF_CONFIG_SMALL("Turns a certain color into transparency. Operates on RGB colors."),
    .priv_size     = sizeof(ColorkeyContext),
    .priv_class    = &colorkey_class,
    FILTER_INPUTS(colorkey_inputs),
    FILTER_OUTPUTS(colorkey_outputs),
    FILTER_PIXFMTS_ARRAY(pixel_fmts),
    .flags         = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS,
    .process_command = ff_filter_process_command,
};

#endif /* CONFIG_COLORKEY_FILTER */
#if CONFIG_COLORHOLD_FILTER

static const AVOption colorhold_options[] = {
    { "color", "set the colorhold key color", OFFSET(colorkey_rgba), AV_OPT_TYPE_COLOR, { .str = "black" }, 0, 0, FLAGS },
    { "similarity", "set the colorhold similarity value", OFFSET(similarity), AV_OPT_TYPE_FLOAT, { .dbl = 0.01 }, 0.00001, 1.0, FLAGS },
    { "blend", "set the colorhold blend value", OFFSET(blend), AV_OPT_TYPE_FLOAT, { .dbl = 0.0 }, 0.0, 1.0, FLAGS },
    { NULL }
};

AVFILTER_DEFINE_CLASS(colorhold);

const AVFilter ff_vf_colorhold = {
    .name          = "colorhold",
    .description   = NULL_IF_CONFIG_SMALL("Turns a certain color range into gray. Operates on RGB colors."),
    .priv_size     = sizeof(ColorkeyContext),
    .priv_class    = &colorhold_class,
    FILTER_INPUTS(colorkey_inputs),
    FILTER_OUTPUTS(colorkey_outputs),
    FILTER_PIXFMTS_ARRAY(pixel_fmts),
    .flags         = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS,
    .process_command = ff_filter_process_command,
};

#endif /* CONFIG_COLORHOLD_FILTER */