FFmpeg
Loading...
Searching...
No Matches
vf_despill.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
26typedef struct DespillContext {
27 const AVClass *class;
28
29 int co[4]; /* color offsets rgba */
30
31 int alpha;
32 int type;
33 float spillmix;
35 float redscale;
37 float bluescale;
40
41static int do_despill_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
42{
43 DespillContext *s = ctx->priv;
44 AVFrame *frame = arg;
45 const int ro = s->co[0], go = s->co[1], bo = s->co[2], ao = s->co[3];
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 const float brightness = s->brightness;
49 const float redscale = s->redscale;
50 const float greenscale = s->greenscale;
51 const float bluescale = s->bluescale;
52 const float spillmix = s->spillmix;
53 const float factor = (1.f - spillmix) * (1.f - s->spillexpand);
54 float red, green, blue;
55 int x, y;
56
57 for (y = slice_start; y < slice_end; y++) {
58 uint8_t *dst = frame->data[0] + y * frame->linesize[0];
59
60 for (x = 0; x < frame->width; x++) {
61 float spillmap;
62
63 red = dst[x * 4 + ro] / 255.f;
64 green = dst[x * 4 + go] / 255.f;
65 blue = dst[x * 4 + bo] / 255.f;
66
67 if (s->type) {
68 spillmap = FFMAX(blue - (red * spillmix + green * factor), 0.f);
69 } else {
70 spillmap = FFMAX(green - (red * spillmix + blue * factor), 0.f);
71 }
72
73 red = FFMAX(red + spillmap * redscale + brightness * spillmap, 0.f);
74 green = FFMAX(green + spillmap * greenscale + brightness * spillmap, 0.f);
75 blue = FFMAX(blue + spillmap * bluescale + brightness * spillmap, 0.f);
76
77 dst[x * 4 + ro] = av_clip_uint8(red * 255);
78 dst[x * 4 + go] = av_clip_uint8(green * 255);
79 dst[x * 4 + bo] = av_clip_uint8(blue * 255);
80 if (s->alpha) {
81 spillmap = 1.f - spillmap;
82 dst[x * 4 + ao] = av_clip_uint8(spillmap * 255);
83 }
84 }
85 }
86
87 return 0;
88}
89
91{
92 AVFilterContext *ctx = link->dst;
93 int ret;
94
97 return ret;
98
99 return ff_filter_frame(ctx->outputs[0], frame);
100}
101
103{
104 AVFilterContext *ctx = outlink->src;
105 DespillContext *s = ctx->priv;
107 int i;
108
109 for (i = 0; i < 4; ++i)
110 s->co[i] = desc->comp[i].offset;
111
112 return 0;
113}
114
122
123static const AVFilterPad despill_inputs[] = {
124 {
125 .name = "default",
126 .type = AVMEDIA_TYPE_VIDEO,
128 .filter_frame = filter_frame,
129 },
130};
131
132static const AVFilterPad despill_outputs[] = {
133 {
134 .name = "default",
135 .type = AVMEDIA_TYPE_VIDEO,
136 .config_props = config_output,
137 },
138};
139
140#define OFFSET(x) offsetof(DespillContext, x)
141#define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
142
143static const AVOption despill_options[] = {
144 { "type", "set the screen type", OFFSET(type), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, FLAGS, .unit = "type" },
145 { "green", "greenscreen", 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, FLAGS, .unit = "type" },
146 { "blue", "bluescreen", 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, FLAGS, .unit = "type" },
147 { "mix", "set the spillmap mix", OFFSET(spillmix), AV_OPT_TYPE_FLOAT, {.dbl=0.5}, 0, 1, FLAGS },
148 { "expand", "set the spillmap expand", OFFSET(spillexpand), AV_OPT_TYPE_FLOAT, {.dbl=0}, 0, 1, FLAGS },
149 { "red", "set red scale", OFFSET(redscale), AV_OPT_TYPE_FLOAT, {.dbl=0}, -100, 100, FLAGS },
150 { "green", "set green scale", OFFSET(greenscale), AV_OPT_TYPE_FLOAT, {.dbl=-1}, -100, 100, FLAGS },
151 { "blue", "set blue scale", OFFSET(bluescale), AV_OPT_TYPE_FLOAT, {.dbl=0}, -100, 100, FLAGS },
152 { "brightness", "set brightness", OFFSET(brightness), AV_OPT_TYPE_FLOAT, {.dbl=0}, -10, 10, FLAGS },
153 { "alpha", "change alpha component", OFFSET(alpha), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS },
154 { NULL }
155};
156
158
160 .p.name = "despill",
161 .p.description = NULL_IF_CONFIG_SMALL("Despill video."),
162 .p.priv_class = &despill_class,
164 .priv_size = sizeof(DespillContext),
168 .process_command = ff_filter_process_command,
169};
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
Definition dsp.h:87
const FFFilter ff_vf_despill
Definition vf_despill.c:159
static AVFormatContext * ctx
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 i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define s(width, name)
Definition cbs_vp9.c:198
#define FLAGS
Definition cmdutils.c:598
#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_CONST
Special option type for declaring named constants.
Definition opt.h:298
@ AV_OPT_TYPE_INT
Underlying C type is int.
Definition opt.h:258
@ AV_OPT_TYPE_FLOAT
Underlying C type is float.
Definition opt.h:270
@ AV_OPT_TYPE_BOOL
Underlying C type is int.
Definition opt.h:326
#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
cl_device_type type
static const int16_t alpha[]
Definition ilbcdata.h:55
static int config_output(AVBitStreamFilterLink *outlink)
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
static const int factor[16]
Definition vf_pp7.c:98
#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
const char * desc
Definition libsvtav1.c:83
#define FFMIN(a, b)
Definition macros.h:49
#define FFMAX(a, b)
Definition macros.h:47
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
AVPixelFormat
Pixel format.
Definition pixfmt.h:71
@ AV_PIX_FMT_NONE
Definition pixfmt.h:72
@ AV_PIX_FMT_ARGB
packed ARGB 8:8:8:8, 32bpp, ARGBARGB...
Definition pixfmt.h:99
@ AV_PIX_FMT_BGRA
packed BGRA 8:8:8:8, 32bpp, BGRABGRA...
Definition pixfmt.h:102
@ AV_PIX_FMT_ABGR
packed ABGR 8:8:8:8, 32bpp, ABGRABGR...
Definition pixfmt.h:101
@ AV_PIX_FMT_RGBA
packed RGBA 8:8:8:8, 32bpp, RGBARGBA...
Definition pixfmt.h:100
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
float spillexpand
Definition vf_despill.c:34
static enum AVPixelFormat pixel_fmts[]
Definition vf_amplify.c:52
static const AVOption despill_options[]
Definition vf_despill.c:143
static const AVFilterPad despill_inputs[]
Definition vf_despill.c:123
static const AVFilterPad despill_outputs[]
Definition vf_despill.c:132
static int do_despill_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
Definition vf_despill.c:41
static av_cold int config_output(AVFilterLink *outlink)
Definition vf_despill.c:102
static int filter_frame(AVFilterLink *link, AVFrame *frame)
Definition vf_despill.c:90
#define OFFSET(x)
Definition vf_despill.c:140
static int slice_start(SliceContext *sc, VVCContext *s, VVCFrameContext *fc, const CodedBitstreamUnit *unit, const int is_first_slice)
Definition dec.c:844