2015-06-13 13:09:07 +00:00
|
|
|
/*
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
2022-02-23 12:56:49 +00:00
|
|
|
#include "config_components.h"
|
|
|
|
|
2015-06-13 13:09:07 +00:00
|
|
|
#include "libavutil/opt.h"
|
|
|
|
#include "libavutil/imgutils.h"
|
|
|
|
#include "avfilter.h"
|
2022-04-07 09:32:05 +00:00
|
|
|
#include "drawutils.h"
|
2015-06-13 13:09:07 +00:00
|
|
|
#include "formats.h"
|
|
|
|
#include "internal.h"
|
|
|
|
#include "video.h"
|
|
|
|
|
|
|
|
typedef struct ColorkeyContext {
|
|
|
|
const AVClass *class;
|
|
|
|
|
|
|
|
/* color offsets rgba */
|
2022-04-07 09:32:05 +00:00
|
|
|
uint8_t co[4];
|
2015-06-13 13:09:07 +00:00
|
|
|
|
|
|
|
uint8_t colorkey_rgba[4];
|
|
|
|
float similarity;
|
|
|
|
float blend;
|
2022-04-07 09:32:05 +00:00
|
|
|
double scale;
|
|
|
|
int depth;
|
|
|
|
int max;
|
2019-01-13 18:02:52 +00:00
|
|
|
|
|
|
|
int (*do_slice)(AVFilterContext *ctx, void *arg,
|
|
|
|
int jobnr, int nb_jobs);
|
2015-06-13 13:09:07 +00:00
|
|
|
} ColorkeyContext;
|
|
|
|
|
2022-04-07 09:32:05 +00:00
|
|
|
static int do_colorkey_pixel(const uint8_t *colorkey_rgba, int r, int g, int b,
|
2022-04-08 08:13:23 +00:00
|
|
|
float similarity, float iblend, int max, double scale)
|
2015-06-13 13:09:07 +00:00
|
|
|
{
|
2022-04-07 09:32:05 +00:00
|
|
|
double dr, dg, db, diff;
|
2015-06-13 13:09:07 +00:00
|
|
|
|
2022-04-07 09:32:05 +00:00
|
|
|
dr = r * scale - colorkey_rgba[0];
|
|
|
|
dg = g * scale - colorkey_rgba[1];
|
|
|
|
db = b * scale - colorkey_rgba[2];
|
2015-06-13 13:09:07 +00:00
|
|
|
|
2022-04-07 09:32:05 +00:00
|
|
|
diff = sqrt((dr * dr + dg * dg + db * db) / (255.0 * 255.0 * 3.0));
|
|
|
|
|
2022-04-08 08:13:23 +00:00
|
|
|
if (iblend < 10000.0) {
|
|
|
|
return av_clipd((diff - similarity) * iblend, 0.0, 1.0) * max;
|
2015-06-13 13:09:07 +00:00
|
|
|
} else {
|
2022-04-07 09:32:05 +00:00
|
|
|
return (diff > similarity) ? max : 0;
|
2015-06-13 13:09:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-07 09:32:05 +00:00
|
|
|
#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; \
|
2022-04-08 08:13:23 +00:00
|
|
|
const float iblend = 1.f / ctx->blend; \
|
2022-04-07 09:32:05 +00:00
|
|
|
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]], \
|
2022-04-08 08:13:23 +00:00
|
|
|
similarity, iblend, max, scale); \
|
2022-04-07 09:32:05 +00:00
|
|
|
} \
|
|
|
|
} \
|
|
|
|
\
|
|
|
|
return 0; \
|
2015-06-13 13:09:07 +00:00
|
|
|
}
|
|
|
|
|
2022-04-07 09:32:05 +00:00
|
|
|
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; \
|
2022-04-08 08:13:23 +00:00
|
|
|
const float iblend = 1.f / ctx->blend; \
|
2022-04-07 09:32:05 +00:00
|
|
|
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, \
|
2022-04-08 08:13:23 +00:00
|
|
|
similarity, iblend, max, scale); \
|
2022-04-07 09:32:05 +00:00
|
|
|
\
|
|
|
|
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; \
|
2019-01-13 18:02:52 +00:00
|
|
|
}
|
|
|
|
|
2022-04-07 09:32:05 +00:00
|
|
|
COLORHOLD_SLICE(8, uint8_t, int)
|
|
|
|
COLORHOLD_SLICE(16, uint16_t, int64_t)
|
2019-01-13 18:02:52 +00:00
|
|
|
|
2015-06-13 13:09:07 +00:00
|
|
|
static int filter_frame(AVFilterLink *link, AVFrame *frame)
|
|
|
|
{
|
|
|
|
AVFilterContext *avctx = link->dst;
|
2019-01-13 18:02:52 +00:00
|
|
|
ColorkeyContext *ctx = avctx->priv;
|
2015-06-13 13:09:07 +00:00
|
|
|
int res;
|
|
|
|
|
2021-08-15 19:33:25 +00:00
|
|
|
if (res = ff_filter_execute(avctx, ctx->do_slice, frame, NULL,
|
|
|
|
FFMIN(frame->height, ff_filter_get_nb_threads(avctx))))
|
2015-06-13 13:09:07 +00:00
|
|
|
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;
|
2022-04-07 09:32:05 +00:00
|
|
|
const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(avctx->inputs[0]->format);
|
2015-06-13 13:09:07 +00:00
|
|
|
|
2022-04-07 09:32:05 +00:00
|
|
|
ctx->depth = desc->comp[0].depth;
|
|
|
|
ctx->max = (1 << ctx->depth) - 1;
|
|
|
|
ctx->scale = 255.0 / ctx->max;
|
2015-06-13 13:09:07 +00:00
|
|
|
outlink->w = avctx->inputs[0]->w;
|
|
|
|
outlink->h = avctx->inputs[0]->h;
|
|
|
|
outlink->time_base = avctx->inputs[0]->time_base;
|
2022-04-07 09:32:05 +00:00
|
|
|
ff_fill_rgba_map(ctx->co, outlink->format);
|
2015-06-13 13:09:07 +00:00
|
|
|
|
2022-04-07 09:32:05 +00:00
|
|
|
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;
|
|
|
|
}
|
2015-06-13 13:09:07 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-09-29 13:57:43 +00:00
|
|
|
static const enum AVPixelFormat pixel_fmts[] = {
|
|
|
|
AV_PIX_FMT_ARGB,
|
|
|
|
AV_PIX_FMT_RGBA,
|
|
|
|
AV_PIX_FMT_ABGR,
|
|
|
|
AV_PIX_FMT_BGRA,
|
2022-04-07 09:32:05 +00:00
|
|
|
AV_PIX_FMT_RGBA64,
|
|
|
|
AV_PIX_FMT_BGRA64,
|
2021-09-29 13:57:43 +00:00
|
|
|
AV_PIX_FMT_NONE
|
|
|
|
};
|
2015-06-13 13:09:07 +00:00
|
|
|
|
|
|
|
static const AVFilterPad colorkey_inputs[] = {
|
|
|
|
{
|
|
|
|
.name = "default",
|
|
|
|
.type = AVMEDIA_TYPE_VIDEO,
|
2021-08-15 22:07:41 +00:00
|
|
|
.flags = AVFILTERPAD_FLAG_NEEDS_WRITABLE,
|
2015-06-13 13:09:07 +00:00
|
|
|
.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)
|
2020-02-11 12:57:43 +00:00
|
|
|
#define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
|
2015-06-13 13:09:07 +00:00
|
|
|
|
2019-01-13 18:02:52 +00:00
|
|
|
#if CONFIG_COLORKEY_FILTER
|
|
|
|
|
2015-06-13 13:09:07 +00:00
|
|
|
static const AVOption colorkey_options[] = {
|
2020-03-17 21:46:36 +00:00
|
|
|
{ "color", "set the colorkey key color", OFFSET(colorkey_rgba), AV_OPT_TYPE_COLOR, { .str = "black" }, 0, 0, FLAGS },
|
2015-06-13 13:09:07 +00:00
|
|
|
{ "similarity", "set the colorkey similarity value", OFFSET(similarity), AV_OPT_TYPE_FLOAT, { .dbl = 0.01 }, 0.01, 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);
|
|
|
|
|
2021-04-19 16:33:56 +00:00
|
|
|
const AVFilter ff_vf_colorkey = {
|
2015-06-13 13:09:07 +00:00
|
|
|
.name = "colorkey",
|
2015-09-18 12:47:59 +00:00
|
|
|
.description = NULL_IF_CONFIG_SMALL("Turns a certain color into transparency. Operates on RGB colors."),
|
2015-06-13 13:09:07 +00:00
|
|
|
.priv_size = sizeof(ColorkeyContext),
|
|
|
|
.priv_class = &colorkey_class,
|
2021-08-12 11:05:31 +00:00
|
|
|
FILTER_INPUTS(colorkey_inputs),
|
|
|
|
FILTER_OUTPUTS(colorkey_outputs),
|
2021-09-27 13:13:06 +00:00
|
|
|
FILTER_PIXFMTS_ARRAY(pixel_fmts),
|
2015-06-13 13:09:07 +00:00
|
|
|
.flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS,
|
2020-02-11 12:57:43 +00:00
|
|
|
.process_command = ff_filter_process_command,
|
2015-06-13 13:09:07 +00:00
|
|
|
};
|
2019-01-13 18:02:52 +00:00
|
|
|
|
|
|
|
#endif /* CONFIG_COLORKEY_FILTER */
|
|
|
|
#if CONFIG_COLORHOLD_FILTER
|
|
|
|
|
|
|
|
static const AVOption colorhold_options[] = {
|
2020-03-17 21:46:36 +00:00
|
|
|
{ "color", "set the colorhold key color", OFFSET(colorkey_rgba), AV_OPT_TYPE_COLOR, { .str = "black" }, 0, 0, FLAGS },
|
2019-01-13 18:02:52 +00:00
|
|
|
{ "similarity", "set the colorhold similarity value", OFFSET(similarity), AV_OPT_TYPE_FLOAT, { .dbl = 0.01 }, 0.01, 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);
|
|
|
|
|
2021-04-19 16:33:56 +00:00
|
|
|
const AVFilter ff_vf_colorhold = {
|
2019-01-13 18:02:52 +00:00
|
|
|
.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,
|
2021-08-12 11:05:31 +00:00
|
|
|
FILTER_INPUTS(colorkey_inputs),
|
|
|
|
FILTER_OUTPUTS(colorkey_outputs),
|
2021-09-27 13:13:06 +00:00
|
|
|
FILTER_PIXFMTS_ARRAY(pixel_fmts),
|
2019-01-13 18:02:52 +00:00
|
|
|
.flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS,
|
2020-02-11 12:57:43 +00:00
|
|
|
.process_command = ff_filter_process_command,
|
2019-01-13 18:02:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif /* CONFIG_COLORHOLD_FILTER */
|