2 * Copyright (c) 2017 Paul B Mahol
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 LumakeyContext {
40 int (*do_lumakey_slice)(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs);
43 static int do_lumakey_slice8(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
45 LumakeyContext *s = ctx->priv;
47 const int slice_start = (frame->height * jobnr) / nb_jobs;
48 const int slice_end = (frame->height * (jobnr + 1)) / nb_jobs;
49 uint8_t *alpha = frame->data[3] + slice_start * frame->linesize[3];
50 const uint8_t *luma = frame->data[0] + slice_start * frame->linesize[0];
52 const int w = s->white;
53 const int b = s->black;
56 for (y = slice_start; y < slice_end; y++) {
57 for (x = 0; x < frame->width; x++) {
58 if (luma[x] >= b && luma[x] <= w) {
60 } else if (luma[x] > b - so && luma[x] < w + so) {
62 alpha[x] = 255 - (luma[x] - b + so) * 255 / so;
64 alpha[x] = (luma[x] - w) * 255 / so;
68 luma += frame->linesize[0];
69 alpha += frame->linesize[3];
75 static int do_lumakey_slice16(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
77 LumakeyContext *s = ctx->priv;
79 const int slice_start = (frame->height * jobnr) / nb_jobs;
80 const int slice_end = (frame->height * (jobnr + 1)) / nb_jobs;
81 uint16_t *alpha = (uint16_t *)(frame->data[3] + slice_start * frame->linesize[3]);
82 const uint16_t *luma = (const uint16_t *)(frame->data[0] + slice_start * frame->linesize[0]);
84 const int w = s->white;
85 const int b = s->black;
89 for (y = slice_start; y < slice_end; y++) {
90 for (x = 0; x < frame->width; x++) {
91 if (luma[x] >= b && luma[x] <= w) {
93 } else if (luma[x] > b - so && luma[x] < w + so) {
95 alpha[x] = m - (luma[x] - b + so) * m / so;
97 alpha[x] = (luma[x] - w) * m / so;
101 luma += frame->linesize[0] / 2;
102 alpha += frame->linesize[3] / 2;
108 static int config_input(AVFilterLink *inlink)
110 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
111 AVFilterContext *ctx = inlink->dst;
112 LumakeyContext *s = ctx->priv;
115 depth = desc->comp[0].depth;
117 s->white = av_clip_uint8((s->threshold + s->tolerance) * 255);
118 s->black = av_clip_uint8((s->threshold - s->tolerance) * 255);
119 s->do_lumakey_slice = do_lumakey_slice8;
120 s->so = s->softness * 255;
122 s->max = (1 << depth) - 1;
123 s->white = av_clip((s->threshold + s->tolerance) * s->max, 0, s->max);
124 s->black = av_clip((s->threshold - s->tolerance) * s->max, 0, s->max);
125 s->do_lumakey_slice = do_lumakey_slice16;
126 s->so = s->softness * s->max;
132 static int filter_frame(AVFilterLink *link, AVFrame *frame)
134 AVFilterContext *ctx = link->dst;
135 LumakeyContext *s = ctx->priv;
138 if (ret = av_frame_make_writable(frame))
141 if (ret = ctx->internal->execute(ctx, s->do_lumakey_slice, frame, NULL, FFMIN(frame->height, ff_filter_get_nb_threads(ctx))))
144 return ff_filter_frame(ctx->outputs[0], frame);
147 static av_cold int query_formats(AVFilterContext *ctx)
149 static const enum AVPixelFormat pixel_fmts[] = {
150 AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA420P,
151 AV_PIX_FMT_YUVA444P9, AV_PIX_FMT_YUVA422P9, AV_PIX_FMT_YUVA420P9,
152 AV_PIX_FMT_YUVA444P10, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA420P10,
153 AV_PIX_FMT_YUVA444P12, AV_PIX_FMT_YUVA422P12,
154 AV_PIX_FMT_YUVA444P16, AV_PIX_FMT_YUVA422P16, AV_PIX_FMT_YUVA420P16,
157 AVFilterFormats *formats;
159 formats = ff_make_format_list(pixel_fmts);
161 return AVERROR(ENOMEM);
163 return ff_set_common_formats(ctx, formats);
166 static const AVFilterPad lumakey_inputs[] = {
169 .type = AVMEDIA_TYPE_VIDEO,
170 .filter_frame = filter_frame,
171 .config_props = config_input,
176 static const AVFilterPad lumakey_outputs[] = {
179 .type = AVMEDIA_TYPE_VIDEO,
184 #define OFFSET(x) offsetof(LumakeyContext, x)
185 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
187 static const AVOption lumakey_options[] = {
188 { "threshold", "set the threshold value", OFFSET(threshold), AV_OPT_TYPE_DOUBLE, {.dbl=0}, 0, 1, FLAGS },
189 { "tolerance", "set the tolerance value", OFFSET(tolerance), AV_OPT_TYPE_DOUBLE, {.dbl=0.01}, 0, 1, FLAGS },
190 { "softness", "set the softness value", OFFSET(softness), AV_OPT_TYPE_DOUBLE, {.dbl=0}, 0, 1, FLAGS },
194 AVFILTER_DEFINE_CLASS(lumakey);
196 AVFilter ff_vf_lumakey = {
198 .description = NULL_IF_CONFIG_SMALL("Turns a certain luma into transparency."),
199 .priv_size = sizeof(LumakeyContext),
200 .priv_class = &lumakey_class,
201 .query_formats = query_formats,
202 .inputs = lumakey_inputs,
203 .outputs = lumakey_outputs,
204 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS,