FFmpeg
Loading...
Searching...
No Matches
vf_hsvkey.c
Go to the documentation of this file.
1/*
2 * Copyright (c) 2021 Paul B Mahol
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 <float.h>
22
23#include "libavutil/opt.h"
25#include "libavutil/pixdesc.h"
26#include "avfilter.h"
27#include "filters.h"
28
29typedef struct HSVKeyContext {
30 const AVClass *class;
31
32 float hue, hue_opt, sat, val;
34 float blend;
35
36 float scale;
37
38 float half;
39
40 int depth;
41 int max;
42
45
47 int jobnr, int nb_jobs);
49
50#define SQR(x) ((x)*(x))
51
52static int do_hsvkey_pixel(HSVKeyContext *s, int y, int u, int v,
53 float hue_key, float sat_key, float val_key)
54{
55 const float similarity = s->similarity;
56 const float scale = s->scale;
57 const float blend = s->blend;
58 const int imax = s->max;
59 const float max = imax;
60 const float half = s->half;
61 const float uf = u - half;
62 const float vf = v - half;
63 const float hue = hue_key < 0.f ? -hue_key : atan2f(uf, vf) + M_PI;
64 const float sat = sat_key < 0.f ? -sat_key : sqrtf((uf * uf + vf * vf) / (half * half * 2.f));
65 const float val = val_key < 0.f ? -val_key : scale * y;
66 float diff;
67
68 hue_key = fabsf(hue_key);
69 sat_key = fabsf(sat_key);
70 val_key = fabsf(val_key);
71
72 diff = sqrtf(fmaxf(SQR(sat) * SQR(val) +
73 SQR(sat_key) * SQR(val_key) -
74 2.f * sat * val * sat_key * val_key * cosf(hue_key - hue) +
75 SQR(val - val_key), 0.f));
76 if (diff < similarity) {
77 return 0;
78 } else if (blend > FLT_MIN) {
79 return av_clipf((diff - similarity) / blend, 0.f, 1.f) * max;
80 } else {
81 return imax;
82 }
83
84 return 0;
85}
86
87static int do_hsvkey_slice(AVFilterContext *avctx, void *arg, int jobnr, int nb_jobs)
88{
89 HSVKeyContext *s = avctx->priv;
90 AVFrame *frame = arg;
91 const int slice_start = ff_slice_pos(frame->height, jobnr, nb_jobs);
92 const int slice_end = ff_slice_pos(frame->height, jobnr + 1, nb_jobs);
93 const int hsub_log2 = s->hsub_log2;
94 const int vsub_log2 = s->vsub_log2;
95 const float hue = s->hue;
96 const float sat = s->sat;
97 const float val = s->val;
98
99 for (int y = slice_start; y < slice_end; y++) {
100 for (int x = 0; x < frame->width; x++) {
101 int Y = frame->data[0][frame->linesize[0] * y + x];
102 int u = frame->data[1][frame->linesize[1] * (y >> vsub_log2) + (x >> hsub_log2)];
103 int v = frame->data[2][frame->linesize[2] * (y >> vsub_log2) + (x >> hsub_log2)];
104
105 frame->data[3][frame->linesize[3] * y + x] = do_hsvkey_pixel(s, Y, u, v, hue, sat, val);
106 }
107 }
108
109 return 0;
110}
111
112static int do_hsvkey16_slice(AVFilterContext *avctx, void *arg, int jobnr, int nb_jobs)
113{
114 HSVKeyContext *s = avctx->priv;
115 AVFrame *frame = arg;
116 const int slice_start = ff_slice_pos(frame->height, jobnr, nb_jobs);
117 const int slice_end = ff_slice_pos(frame->height, jobnr + 1, nb_jobs);
118 const int hsub_log2 = s->hsub_log2;
119 const int vsub_log2 = s->vsub_log2;
120 const float hue = s->hue;
121 const float sat = s->sat;
122 const float val = s->val;
123
124 for (int y = slice_start; y < slice_end; ++y) {
125 for (int x = 0; x < frame->width; ++x) {
126 uint16_t *dst = (uint16_t *)(frame->data[3] + frame->linesize[3] * y);
127 int Y = AV_RN16(&frame->data[0][frame->linesize[0] * y + 2 * x]);
128 int u = AV_RN16(&frame->data[1][frame->linesize[1] * (y >> vsub_log2) + 2 * (x >> hsub_log2)]);
129 int v = AV_RN16(&frame->data[2][frame->linesize[2] * (y >> vsub_log2) + 2 * (x >> hsub_log2)]);
130
131 dst[x] = do_hsvkey_pixel(s, Y, u, v, hue, sat, val);
132 }
133 }
134
135 return 0;
136}
137
138static int do_hsvhold_slice(AVFilterContext *avctx, void *arg, int jobnr, int nb_jobs)
139{
140 HSVKeyContext *s = avctx->priv;
141 AVFrame *frame = arg;
142 const int hsub_log2 = s->hsub_log2;
143 const int vsub_log2 = s->vsub_log2;
144 const int width = frame->width >> hsub_log2;
145 const int height = frame->height >> vsub_log2;
146 const int slice_start = ff_slice_pos(height, jobnr, nb_jobs);
147 const int slice_end = ff_slice_pos(height, jobnr + 1, nb_jobs);
148 const float scale = s->scale;
149 const float hue = s->hue;
150 const float sat = s->sat;
151 const float val = s->val;
152
153 for (int y = slice_start; y < slice_end; ++y) {
154 for (int x = 0; x < width; ++x) {
155 uint8_t *dstu = frame->data[1] + frame->linesize[1] * y;
156 uint8_t *dstv = frame->data[2] + frame->linesize[2] * y;
157 int Y = frame->data[0][frame->linesize[0] * (y << vsub_log2) + (x << hsub_log2)];
158 int u = frame->data[1][frame->linesize[1] * y + x];
159 int v = frame->data[2][frame->linesize[2] * y + x];
160 int t = do_hsvkey_pixel(s, Y, u, v, hue, sat, val);
161
162 if (t > 0) {
163 float f = 1.f - t * scale;
164
165 dstu[x] = 128 + (u - 128) * f;
166 dstv[x] = 128 + (v - 128) * f;
167 }
168 }
169 }
170
171 return 0;
172}
173
174static int do_hsvhold16_slice(AVFilterContext *avctx, void *arg, int jobnr, int nb_jobs)
175{
176 HSVKeyContext *s = avctx->priv;
177 AVFrame *frame = arg;
178 const int hsub_log2 = s->hsub_log2;
179 const int vsub_log2 = s->vsub_log2;
180 const int width = frame->width >> hsub_log2;
181 const int height = frame->height >> vsub_log2;
182 const int slice_start = ff_slice_pos(height, jobnr, nb_jobs);
183 const int slice_end = ff_slice_pos(height, jobnr + 1, nb_jobs);
184 const float scale = s->scale;
185 const float half = s->half;
186 const float hue = s->hue;
187 const float sat = s->sat;
188 const float val = s->val;
189
190 for (int y = slice_start; y < slice_end; ++y) {
191 for (int x = 0; x < width; ++x) {
192 uint16_t *dstu = (uint16_t *)(frame->data[1] + frame->linesize[1] * y);
193 uint16_t *dstv = (uint16_t *)(frame->data[2] + frame->linesize[2] * y);
194 int Y = AV_RN16(&frame->data[0][frame->linesize[0] * (y << vsub_log2) + 2 * (x << hsub_log2)]);
195 int u = AV_RN16(&frame->data[1][frame->linesize[1] * y + 2 * x]);
196 int v = AV_RN16(&frame->data[2][frame->linesize[2] * y + 2 * x]);
197 int t = do_hsvkey_pixel(s, Y, u, v, hue, sat, val);
198
199 if (t > 0) {
200 float f = 1.f - t * scale;
201
202 dstu[x] = half + (u - half) * f;
203 dstv[x] = half + (v - half) * f;
204 }
205 }
206 }
207
208 return 0;
209}
210
212{
213 AVFilterContext *avctx = link->dst;
214 HSVKeyContext *s = avctx->priv;
215 int res;
216
217 s->hue = FFSIGN(s->hue_opt) *M_PI * fmodf(526.f - fabsf(s->hue_opt), 360.f) / 180.f;
218 if (res = ff_filter_execute(avctx, s->do_slice, frame, NULL,
219 FFMIN(frame->height, ff_filter_get_nb_threads(avctx))))
220 return res;
221
222 return ff_filter_frame(avctx->outputs[0], frame);
223}
224
226{
228 AVFilterContext *avctx = outlink->src;
229 HSVKeyContext *s = avctx->priv;
230
231 s->depth = desc->comp[0].depth;
232 s->max = (1 << s->depth) - 1;
233 s->half = 0.5f * s->max;
234 s->scale = 1.f / s->max;
235
236 if (!strcmp(avctx->filter->name, "hsvkey")) {
237 s->do_slice = s->depth <= 8 ? do_hsvkey_slice : do_hsvkey16_slice;
238 } else {
239 s->do_slice = s->depth <= 8 ? do_hsvhold_slice: do_hsvhold16_slice;
240 }
241
242 return 0;
243}
244
246{
247 AVFilterContext *avctx = inlink->dst;
248 HSVKeyContext *s = avctx->priv;
250
251 s->hsub_log2 = desc->log2_chroma_w;
252 s->vsub_log2 = desc->log2_chroma_h;
253
254 return 0;
255}
256
267
268static const AVFilterPad inputs[] = {
269 {
270 .name = "default",
271 .type = AVMEDIA_TYPE_VIDEO,
273 .filter_frame = filter_frame,
274 .config_props = config_input,
275 },
276};
277
278static const AVFilterPad outputs[] = {
279 {
280 .name = "default",
281 .type = AVMEDIA_TYPE_VIDEO,
282 .config_props = config_output,
283 },
284};
285
286#define OFFSET(x) offsetof(HSVKeyContext, x)
287#define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
288
289static const AVOption hsvkey_options[] = {
290 { "hue", "set the hue value", OFFSET(hue_opt), AV_OPT_TYPE_FLOAT, { .dbl = 0 }, -360, 360, FLAGS },
291 { "sat", "set the saturation value", OFFSET(sat), AV_OPT_TYPE_FLOAT, { .dbl = 0 }, -1, 1, FLAGS },
292 { "val", "set the value value", OFFSET(val), AV_OPT_TYPE_FLOAT, { .dbl = 0 }, -1, 1, FLAGS },
293 { "similarity", "set the hsvkey similarity value", OFFSET(similarity), AV_OPT_TYPE_FLOAT, { .dbl = 0.01}, 0.00001, 1.0, FLAGS },
294 { "blend", "set the hsvkey blend value", OFFSET(blend), AV_OPT_TYPE_FLOAT, { .dbl = 0.0 }, 0.0, 1.0, FLAGS },
295 { NULL }
296};
297
299
301 .p.name = "hsvkey",
302 .p.description = NULL_IF_CONFIG_SMALL("Turns a certain HSV range into transparency. Operates on YUV colors."),
303 .p.priv_class = &hsvkey_class,
305 .priv_size = sizeof(HSVKeyContext),
309 .process_command = ff_filter_process_command,
310};
311
330
331static const AVOption hsvhold_options[] = {
332 { "hue", "set the hue value", OFFSET(hue_opt), AV_OPT_TYPE_FLOAT, { .dbl = 0 }, -360, 360, FLAGS },
333 { "sat", "set the saturation value", OFFSET(sat), AV_OPT_TYPE_FLOAT, { .dbl = 0 }, -1, 1, FLAGS },
334 { "val", "set the value value", OFFSET(val), AV_OPT_TYPE_FLOAT, { .dbl = 0 }, -1, 1, FLAGS },
335 { "similarity", "set the hsvhold similarity value", OFFSET(similarity), AV_OPT_TYPE_FLOAT, { .dbl = 0.01 }, 0.00001, 1.0, FLAGS },
336 { "blend", "set the hsvhold blend value", OFFSET(blend), AV_OPT_TYPE_FLOAT, { .dbl = 0.0 }, 0.0, 1.0, FLAGS },
337 { NULL }
338};
339
341
343 .p.name = "hsvhold",
344 .p.description = NULL_IF_CONFIG_SMALL("Turns a certain HSV range into gray."),
345 .p.priv_class = &hsvhold_class,
347 .priv_size = sizeof(HSVKeyContext),
351 .process_command = ff_filter_process_command,
352};
uint8_t ptrdiff_t const uint8_t ptrdiff_t int const int8_t const int8_t * vf
Definition dsp.h:262
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
Definition dsp.h:87
static double val(void *priv, double ch)
Definition aeval.c:77
static const AVFilterPad inputs[]
Definition af_aap.c:299
static const AVFilterPad outputs[]
Definition af_aap.c:310
static int config_input(AVFilterLink *inlink)
const FFFilter ff_vf_hsvhold
Definition vf_hsvkey.c:342
const FFFilter ff_vf_hsvkey
Definition vf_hsvkey.c:300
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition avfilter.c:1068
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.
Definition avfilter.c:906
int ff_filter_execute(AVFilterContext *ctx, avfilter_action_func *func, void *arg, int *ret, int nb_jobs)
Definition avfilter.c:1696
int ff_filter_get_nb_threads(AVFilterContext *ctx)
Get number of threads for current filter instance.
Definition avfilter.c:846
Main libavfilter public API header.
#define Y
Definition boxblur.h:37
#define f(width, name)
Definition cbs_vp8.c:236
#define s(width, name)
Definition cbs_vp9.c:198
#define FLAGS
Definition cmdutils.c:598
#define av_clipf
Definition common.h:145
#define FFSIGN(a)
Definition common.h:75
#define NULL
Definition coverity.c:32
static __device__ float sqrtf(float a)
static __device__ float fabsf(float a)
#define max(a, b)
static AVFrame * frame
float fmaxf(float, float)
static int filter_frame(DBEDecodeContext *s, AVFrame *frame)
Definition dolby_e.c:1067
@ AV_OPT_TYPE_FLOAT
Underlying C type is float.
Definition opt.h:270
#define AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC
Some filters support a generic "enable" expression option that can be used to enable or disable a fil...
Definition avfilter.h:196
#define AVFILTER_FLAG_SLICE_THREADS
The filter supports multithreading by splitting frames into multiple parts and processing them concur...
Definition avfilter.h:166
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
static void scale(int *out, const int *in, const int w, const int h, const int shift)
Definition intra.c:278
#define AV_RN16(p)
static int config_output(AVBitStreamFilterLink *outlink)
#define u(width, name, range_min, range_max)
Definition cbs_apv.c:68
const char * arg
Definition jacosubdec.c:65
#define FILTER_INPUTS(array)
Definition filters.h:264
#define FILTER_OUTPUTS(array)
Definition filters.h:265
static int ff_slice_pos(int total, int jobnr, int nb_jobs)
Compute the boundary index for a slice when work of size total is split into nb_jobs slices.
Definition filters.h:763
#define AVFILTERPAD_FLAG_NEEDS_WRITABLE
The filter expects writable frames from its input link, duplicating data buffers if needed.
Definition filters.h:59
#define FILTER_PIXFMTS_ARRAY(array)
Definition filters.h:244
#define AVFILTER_DEFINE_CLASS(fname)
Definition filters.h:478
#define av_cold
Definition attributes.h:117
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:88
#define cosf(x)
Definition libm.h:80
#define atan2f(y, x)
Definition libm.h:47
const char * desc
Definition libsvtav1.c:83
#define FFMIN(a, b)
Definition macros.h:49
#define M_PI
Definition mathematics.h:67
static uint8_t half(int a, int b)
Definition mobiclip.c:540
static int slice_end(AVCodecContext *avctx, AVFrame *pict, int *got_output)
Handle slice ends.
Definition mpeg12dec.c:1697
AVOptions.
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition pixdesc.c:3460
#define AV_PIX_FMT_YUV420P16
Definition pixfmt.h:556
#define AV_PIX_FMT_YUV444P12
Definition pixfmt.h:552
#define AV_PIX_FMT_YUV444P9
Definition pixfmt.h:544
#define AV_PIX_FMT_YUV420P10
Definition pixfmt.h:545
#define AV_PIX_FMT_YUV422P9
Definition pixfmt.h:543
#define AV_PIX_FMT_YUVA444P10
Definition pixfmt.h:598
#define AV_PIX_FMT_YUVA420P16
Definition pixfmt.h:601
#define AV_PIX_FMT_YUV420P12
Definition pixfmt.h:549
#define AV_PIX_FMT_YUVA420P10
Definition pixfmt.h:596
#define AV_PIX_FMT_YUVA422P9
Definition pixfmt.h:594
#define AV_PIX_FMT_YUV422P12
Definition pixfmt.h:550
#define AV_PIX_FMT_YUV422P10
Definition pixfmt.h:546
#define AV_PIX_FMT_YUV420P9
Definition pixfmt.h:542
#define AV_PIX_FMT_YUVA420P9
Definition pixfmt.h:593
#define AV_PIX_FMT_YUVA422P10
Definition pixfmt.h:597
#define AV_PIX_FMT_YUV420P14
Definition pixfmt.h:553
AVPixelFormat
Pixel format.
Definition pixfmt.h:71
@ AV_PIX_FMT_NONE
Definition pixfmt.h:72
@ AV_PIX_FMT_YUV420P
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition pixfmt.h:73
@ AV_PIX_FMT_YUV422P
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition pixfmt.h:77
@ AV_PIX_FMT_YUVA420P
planar YUV 4:2:0, 20bpp, (1 Cr & Cb sample per 2x2 Y & A samples)
Definition pixfmt.h:108
@ AV_PIX_FMT_YUV444P
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition pixfmt.h:78
@ AV_PIX_FMT_YUVA444P
planar YUV 4:4:4 32bpp, (1 Cr & Cb sample per 1x1 Y & A samples)
Definition pixfmt.h:174
@ AV_PIX_FMT_YUVA422P
planar YUV 4:2:2 24bpp, (1 Cr & Cb sample per 2x1 Y & A samples)
Definition pixfmt.h:173
#define AV_PIX_FMT_YUVA422P12
Definition pixfmt.h:599
#define AV_PIX_FMT_YUV422P14
Definition pixfmt.h:554
#define AV_PIX_FMT_YUV422P16
Definition pixfmt.h:557
#define AV_PIX_FMT_YUVA444P16
Definition pixfmt.h:603
#define AV_PIX_FMT_YUVA422P16
Definition pixfmt.h:602
#define AV_PIX_FMT_YUV444P14
Definition pixfmt.h:555
#define AV_PIX_FMT_YUVA444P9
Definition pixfmt.h:595
#define AV_PIX_FMT_YUVA444P12
Definition pixfmt.h:600
#define AV_PIX_FMT_YUV444P16
Definition pixfmt.h:558
#define AV_PIX_FMT_YUV444P10
Definition pixfmt.h:548
Describe the class of an AVClass context structure.
Definition log.h:76
An instance of a filter.
Definition avfilter.h:273
const AVFilter * filter
the AVFilter of which this is an instance
Definition avfilter.h:276
void * priv
private data for use by the filter
Definition avfilter.h:288
AVFilterLink ** outputs
array of pointers to output links
Definition avfilter.h:285
A filter pad used for either input or output.
Definition filters.h:40
const char * name
Filter name.
Definition avfilter.h:219
This structure describes decoded (raw) audio or video data.
Definition frame.h:472
AVOption.
Definition opt.h:428
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition pixdesc.h:69
float similarity
Definition vf_hsvkey.c:33
float hue_opt
Definition vf_hsvkey.c:32
int(* do_slice)(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
Definition vf_hsvkey.c:46
static int imax(const int a, const int b)
Definition internal.h:177
static AVFormatContext * ctx
Definition movenc.c:49
#define height
Definition dsp.h:89
#define width
Definition dsp.h:89
static enum AVPixelFormat hold_pixel_fmts[]
static int do_hsvhold_slice(AVFilterContext *avctx, void *arg, int jobnr, int nb_jobs)
Definition vf_hsvkey.c:138
static av_cold int config_output(AVFilterLink *outlink)
Definition vf_hsvkey.c:225
static av_cold int config_input(AVFilterLink *inlink)
Definition vf_hsvkey.c:245
#define SQR(x)
Definition vf_hsvkey.c:50
static const AVOption hsvhold_options[]
Definition vf_hsvkey.c:331
static int filter_frame(AVFilterLink *link, AVFrame *frame)
Definition vf_hsvkey.c:211
static const AVOption hsvkey_options[]
Definition vf_hsvkey.c:289
static int do_hsvkey_pixel(HSVKeyContext *s, int y, int u, int v, float hue_key, float sat_key, float val_key)
Definition vf_hsvkey.c:52
static int do_hsvhold16_slice(AVFilterContext *avctx, void *arg, int jobnr, int nb_jobs)
Definition vf_hsvkey.c:174
#define OFFSET(x)
Definition vf_hsvkey.c:286
static enum AVPixelFormat key_pixel_fmts[]
Definition vf_hsvkey.c:257
static int do_hsvkey_slice(AVFilterContext *avctx, void *arg, int jobnr, int nb_jobs)
Definition vf_hsvkey.c:87
static int do_hsvkey16_slice(AVFilterContext *avctx, void *arg, int jobnr, int nb_jobs)
Definition vf_hsvkey.c:112
static av_always_inline int diff(const struct color_info *a, const struct color_info *b, const int trans_thresh)
static int slice_start(SliceContext *sc, VVCContext *s, VVCFrameContext *fc, const CodedBitstreamUnit *unit, const int is_first_slice)
Definition dec.c:844