FFmpeg
Loading...
Searching...
No Matches
vf_lumakey.c
Go to the documentation of this file.
1/*
2 * Copyright (c) 2017 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 "libavutil/opt.h"
22#include "libavutil/pixdesc.h"
23#include "avfilter.h"
24#include "filters.h"
25#include "video.h"
26
27typedef struct LumakeyContext {
28 const AVClass *class;
29
30 double threshold;
31 double tolerance;
32 double softness;
33
34 int white;
35 int black;
36 int so;
37 int max;
38
39 int (*do_lumakey_slice)(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs);
41
42static int do_lumakey_slice8(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
43{
44 LumakeyContext *s = ctx->priv;
45 AVFrame *frame = arg;
46 const int slice_start = ff_slice_pos(frame->height, jobnr, nb_jobs);
47 const int slice_end = ff_slice_pos(frame->height, jobnr + 1, nb_jobs);
48 uint8_t *alpha = frame->data[3] + slice_start * frame->linesize[3];
49 const uint8_t *luma = frame->data[0] + slice_start * frame->linesize[0];
50 const int so = s->so;
51 const int w = s->white;
52 const int b = s->black;
53 int x, y;
54
55 for (y = slice_start; y < slice_end; y++) {
56 for (x = 0; x < frame->width; x++) {
57 if (luma[x] >= b && luma[x] <= w) {
58 alpha[x] = 0;
59 } else if (luma[x] > b - so && luma[x] < w + so) {
60 if (luma[x] < b) {
61 alpha[x] = 255 - (luma[x] - b + so) * 255 / so;
62 } else {
63 alpha[x] = (luma[x] - w) * 255 / so;
64 }
65 }
66 }
67 luma += frame->linesize[0];
68 alpha += frame->linesize[3];
69 }
70
71 return 0;
72}
73
74static int do_lumakey_slice16(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
75{
76 LumakeyContext *s = ctx->priv;
77 AVFrame *frame = arg;
78 const int slice_start = ff_slice_pos(frame->height, jobnr, nb_jobs);
79 const int slice_end = ff_slice_pos(frame->height, jobnr + 1, nb_jobs);
80 uint16_t *alpha = (uint16_t *)(frame->data[3] + slice_start * frame->linesize[3]);
81 const uint16_t *luma = (const uint16_t *)(frame->data[0] + slice_start * frame->linesize[0]);
82 const int so = s->so;
83 const int w = s->white;
84 const int b = s->black;
85 const int m = s->max;
86 int x, y;
87
88 for (y = slice_start; y < slice_end; y++) {
89 for (x = 0; x < frame->width; x++) {
90 if (luma[x] >= b && luma[x] <= w) {
91 alpha[x] = 0;
92 } else if (luma[x] > b - so && luma[x] < w + so) {
93 if (luma[x] < b) {
94 alpha[x] = m - (luma[x] - b + so) * m / so;
95 } else {
96 alpha[x] = (luma[x] - w) * m / so;
97 }
98 }
99 }
100 luma += frame->linesize[0] / 2;
101 alpha += frame->linesize[3] / 2;
102 }
103
104 return 0;
105}
106
107static int config_input(AVFilterLink *inlink)
108{
110 AVFilterContext *ctx = inlink->dst;
111 LumakeyContext *s = ctx->priv;
112 int depth;
113
114 depth = desc->comp[0].depth;
115 if (depth == 8) {
116 s->white = av_clip_uint8((s->threshold + s->tolerance) * 255);
117 s->black = av_clip_uint8((s->threshold - s->tolerance) * 255);
118 s->do_lumakey_slice = do_lumakey_slice8;
119 s->so = s->softness * 255;
120 } else {
121 s->max = (1 << depth) - 1;
122 s->white = av_clip((s->threshold + s->tolerance) * s->max, 0, s->max);
123 s->black = av_clip((s->threshold - s->tolerance) * s->max, 0, s->max);
124 s->do_lumakey_slice = do_lumakey_slice16;
125 s->so = s->softness * s->max;
126 }
127
128 return 0;
129}
130
132{
133 AVFilterContext *ctx = link->dst;
134 LumakeyContext *s = ctx->priv;
135 int ret;
136
137 if (ret = ff_filter_execute(ctx, s->do_lumakey_slice, frame, NULL,
139 return ret;
140
141 return ff_filter_frame(ctx->outputs[0], frame);
142}
143
152
153static int process_command(AVFilterContext *ctx, const char *cmd, const char *args,
154 char *res, int res_len, int flags)
155{
156 int ret;
157
158 ret = ff_filter_process_command(ctx, cmd, args, res, res_len, flags);
159 if (ret < 0)
160 return ret;
161
162 return config_input(ctx->inputs[0]);
163}
164
165static const AVFilterPad lumakey_inputs[] = {
166 {
167 .name = "default",
168 .type = AVMEDIA_TYPE_VIDEO,
170 .filter_frame = filter_frame,
171 .config_props = config_input,
172 },
173};
174
175#define OFFSET(x) offsetof(LumakeyContext, x)
176#define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
177
178static const AVOption lumakey_options[] = {
179 { "threshold", "set the threshold value", OFFSET(threshold), AV_OPT_TYPE_DOUBLE, {.dbl=0}, 0, 1, FLAGS },
180 { "tolerance", "set the tolerance value", OFFSET(tolerance), AV_OPT_TYPE_DOUBLE, {.dbl=0.01}, 0, 1, FLAGS },
181 { "softness", "set the softness value", OFFSET(softness), AV_OPT_TYPE_DOUBLE, {.dbl=0}, 0, 1, FLAGS },
182 { NULL }
183};
184
186
188 .p.name = "lumakey",
189 .p.description = NULL_IF_CONFIG_SMALL("Turns a certain luma into transparency."),
190 .p.priv_class = &lumakey_class,
193 .priv_size = sizeof(LumakeyContext),
197 .process_command = process_command,
198};
static int config_input(AVFilterLink *inlink)
static int process_command(AVFilterContext *ctx, const char *cmd, const char *args, char *res, int res_len, int flags)
const FFFilter ff_vf_lumakey
Definition vf_lumakey.c:187
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 flags(name, subs,...)
Definition cbs_h264.c:74
#define s(width, name)
Definition cbs_vp9.c:198
#define FLAGS
Definition cmdutils.c:598
#define av_clip
Definition common.h:100
#define av_clip_uint8
Definition common.h:106
#define NULL
Definition coverity.c:32
static AVFrame * frame
static int filter_frame(DBEDecodeContext *s, AVFrame *frame)
Definition dolby_e.c:1067
@ AV_OPT_TYPE_DOUBLE
Underlying C type is double.
Definition opt.h:266
#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 const int16_t alpha[]
Definition ilbcdata.h:55
#define b
Definition input.c:43
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 NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:88
const char * desc
Definition libsvtav1.c:83
uint8_t w
Definition llvidencdsp.c:39
#define FFMIN(a, b)
Definition macros.h:49
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_YUVA444P10
Definition pixfmt.h:598
#define AV_PIX_FMT_YUVA420P16
Definition pixfmt.h:601
#define AV_PIX_FMT_YUVA420P10
Definition pixfmt.h:596
#define AV_PIX_FMT_YUVA422P9
Definition pixfmt.h:594
#define AV_PIX_FMT_YUVA420P9
Definition pixfmt.h:593
#define AV_PIX_FMT_YUVA422P10
Definition pixfmt.h:597
AVPixelFormat
Pixel format.
Definition pixfmt.h:71
@ AV_PIX_FMT_NONE
Definition pixfmt.h:72
@ 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_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_YUVA444P16
Definition pixfmt.h:603
#define AV_PIX_FMT_YUVA422P16
Definition pixfmt.h:602
#define AV_PIX_FMT_YUVA444P9
Definition pixfmt.h:595
#define AV_PIX_FMT_YUVA444P12
Definition pixfmt.h:600
Describe the class of an AVClass context structure.
Definition log.h:76
An instance of a filter.
Definition avfilter.h:273
A filter pad used for either input or output.
Definition filters.h:40
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
double softness
Definition vf_lumakey.c:32
double tolerance
Definition vf_lumakey.c:31
int(* do_lumakey_slice)(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
Definition vf_lumakey.c:39
double threshold
Definition vf_lumakey.c:30
static AVFormatContext * ctx
Definition movenc.c:49
static enum AVPixelFormat pixel_fmts[]
Definition vf_amplify.c:52
static const AVFilterPad lumakey_inputs[]
Definition vf_lumakey.c:165
static int do_lumakey_slice16(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
Definition vf_lumakey.c:74
static int config_input(AVFilterLink *inlink)
Definition vf_lumakey.c:107
static const AVOption lumakey_options[]
Definition vf_lumakey.c:178
static int filter_frame(AVFilterLink *link, AVFrame *frame)
Definition vf_lumakey.c:131
static int process_command(AVFilterContext *ctx, const char *cmd, const char *args, char *res, int res_len, int flags)
Definition vf_lumakey.c:153
#define OFFSET(x)
Definition vf_lumakey.c:175
static int do_lumakey_slice8(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
Definition vf_lumakey.c:42
const AVFilterPad ff_video_default_filterpad[1]
An AVFilterPad array whose only entry has name "default" and is of type AVMEDIA_TYPE_VIDEO.
Definition video.c:37
static int slice_start(SliceContext *sc, VVCContext *s, VVCFrameContext *fc, const CodedBitstreamUnit *unit, const int is_first_slice)
Definition dec.c:844