21#include "config_components.h"
43 int jobnr,
int nb_jobs);
47 float similarity,
float iblend,
int max,
double scale)
49 double dr, dg, db,
diff;
51 dr =
r *
scale - colorkey_rgba[0];
52 dg =
g *
scale - colorkey_rgba[1];
53 db =
b *
scale - colorkey_rgba[2];
55 diff = sqrt((dr * dr + dg * dg + db * db) / (255.0 * 255.0 * 3.0));
57 if (iblend < 10000.0) {
60 return (
diff > similarity) ?
max : 0;
64#define COLORKEY_SLICE(name, type) \
65static int do_colorkey_slice##name(AVFilterContext *avctx, \
67 int jobnr, int nb_jobs) \
69 AVFrame *frame = arg; \
70 const int slice_start = ff_slice_pos(frame->height, jobnr, nb_jobs); \
71 const int slice_end = ff_slice_pos(frame->height, jobnr + 1, nb_jobs); \
72 ColorkeyContext *ctx = avctx->priv; \
73 const float similarity = ctx->similarity; \
74 const float iblend = 1.f / ctx->blend; \
75 const uint8_t *colorkey_rgba = ctx->colorkey_rgba; \
76 const uint8_t *co = ctx->co; \
77 const double scale = ctx->scale; \
78 const int max = ctx->max; \
80 for (int y = slice_start; y < slice_end; y++) { \
81 type *dst = (type *)(frame->data[0] + y * frame->linesize[0]);\
83 for (int x = 0; x < frame->width; x++) { \
84 const int o = x * 4; \
86 dst[o + co[3]] = do_colorkey_pixel(colorkey_rgba, \
90 similarity, iblend, max, scale); \
100#define COLORHOLD_SLICE(name, type, htype) \
101static int do_colorhold_slice##name(AVFilterContext *avctx, void *arg, \
102 int jobnr, int nb_jobs) \
104 AVFrame *frame = arg; \
105 const int slice_start = ff_slice_pos(frame->height, jobnr, nb_jobs); \
106 const int slice_end = ff_slice_pos(frame->height, jobnr + 1, nb_jobs); \
107 ColorkeyContext *ctx = avctx->priv; \
108 const int depth = ctx->depth; \
109 const int max = ctx->max; \
110 const int half = max / 2; \
111 const uint8_t *co = ctx->co; \
112 const uint8_t *colorkey_rgba = ctx->colorkey_rgba; \
113 const float similarity = ctx->similarity; \
114 const float iblend = 1.f / ctx->blend; \
115 const double scale = ctx->scale; \
117 for (int y = slice_start; y < slice_end; ++y) { \
118 type *dst = (type *)(frame->data[0] + y * frame->linesize[0]); \
120 for (int x = 0; x < frame->width; ++x) { \
124 r = dst[o + co[0]]; \
125 g = dst[o + co[1]]; \
126 b = dst[o + co[2]]; \
128 t = do_colorkey_pixel(colorkey_rgba, r, g, b, \
129 similarity, iblend, max, scale); \
132 htype a = (r + g + b) / 3; \
133 htype rt = max - t; \
135 dst[o + co[0]] = (a * t + r * rt + half) >> depth; \
136 dst[o + co[1]] = (a * t + g * rt + half) >> depth; \
137 dst[o + co[2]] = (a * t + b * rt + half) >> depth; \
167 ctx->depth =
desc->comp[0].depth;
168 ctx->max = (1 <<
ctx->depth) - 1;
169 ctx->scale = 255.0 /
ctx->max;
175 if (!strcmp(avctx->
filter->
name,
"colorkey")) {
176 ctx->do_slice =
ctx->max == 255 ? do_colorkey_slice8 : do_colorkey_slice16;
178 ctx->do_slice =
ctx->max == 255 ? do_colorhold_slice8 : do_colorhold_slice16;
211#define OFFSET(x) offsetof(ColorkeyContext, x)
212#define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
214#if CONFIG_COLORKEY_FILTER
216static const AVOption colorkey_options[] = {
226 .p.name =
"colorkey",
227 .p.description =
NULL_IF_CONFIG_SMALL(
"Turns a certain color into transparency. Operates on RGB colors."),
228 .p.priv_class = &colorkey_class,
238#if CONFIG_COLORHOLD_FILTER
240static const AVOption colorhold_options[] = {
250 .p.name =
"colorhold",
251 .p.description =
NULL_IF_CONFIG_SMALL(
"Turns a certain color range into gray. Operates on RGB colors."),
252 .p.priv_class = &colorhold_class,
const FFFilter ff_vf_colorhold
const FFFilter ff_vf_colorkey
static AVFormatContext * ctx
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
int ff_filter_process_command(AVFilterContext *ctx, const char *cmd, const char *arg, char *res, int res_len, int flags)
Generic processing of user supplied commands that are set in the same way as the filter options.
int ff_filter_execute(AVFilterContext *ctx, avfilter_action_func *func, void *arg, int *ret, int nb_jobs)
int ff_filter_get_nb_threads(AVFilterContext *ctx)
Get number of threads for current filter instance.
Main libavfilter public API header.
static int filter_frame(DBEDecodeContext *s, AVFrame *frame)
int ff_fill_rgba_map(uint8_t *rgba_map, enum AVPixelFormat pix_fmt)
@ AV_OPT_TYPE_FLOAT
Underlying C type is float.
@ AV_OPT_TYPE_COLOR
Underlying C type is uint8_t[4].
#define AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC
Some filters support a generic "enable" expression option that can be used to enable or disable a fil...
#define AVFILTER_FLAG_SLICE_THREADS
The filter supports multithreading by splitting frames into multiple parts and processing them concur...
static void scale(int *out, const int *in, const int w, const int h, const int shift)
static int config_output(AVBitStreamFilterLink *outlink)
#define FILTER_INPUTS(array)
#define FILTER_OUTPUTS(array)
#define AVFILTERPAD_FLAG_NEEDS_WRITABLE
The filter expects writable frames from its input link, duplicating data buffers if needed.
#define FILTER_PIXFMTS_ARRAY(array)
#define AVFILTER_DEFINE_CLASS(fname)
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
#define AV_PIX_FMT_RGBA64
AVPixelFormat
Pixel format.
@ AV_PIX_FMT_ARGB
packed ARGB 8:8:8:8, 32bpp, ARGBARGB...
@ AV_PIX_FMT_BGRA
packed BGRA 8:8:8:8, 32bpp, BGRABGRA...
@ AV_PIX_FMT_ABGR
packed ABGR 8:8:8:8, 32bpp, ABGRABGR...
@ AV_PIX_FMT_RGBA
packed RGBA 8:8:8:8, 32bpp, RGBARGBA...
#define AV_PIX_FMT_BGRA64
Describe the class of an AVClass context structure.
AVFilterLink ** inputs
array of pointers to input links
const AVFilter * filter
the AVFilter of which this is an instance
void * priv
private data for use by the filter
AVFilterLink ** outputs
array of pointers to output links
A link between two filters.
int w
agreed upon image width
int h
agreed upon image height
AVFilterContext * src
source filter
AVRational time_base
Define the time base used by the PTS of the frames/samples which will pass through this link.
int format
agreed upon media format
A filter pad used for either input or output.
const char * name
Filter name.
This structure describes decoded (raw) audio or video data.
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
int(* do_slice)(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
static enum AVPixelFormat pixel_fmts[]
static const AVFilterPad colorkey_outputs[]
static int do_colorkey_pixel(const uint8_t *colorkey_rgba, int r, int g, int b, float similarity, float iblend, int max, double scale)
static const AVFilterPad colorkey_inputs[]
static av_cold int config_output(AVFilterLink *outlink)
#define COLORKEY_SLICE(name, type)
#define COLORHOLD_SLICE(name, type, htype)
static av_always_inline int diff(const struct color_info *a, const struct color_info *b, const int trans_thresh)