FFmpeg
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/imgutils.h"
23 #include "avfilter.h"
24 #include "formats.h"
25 #include "internal.h"
26 #include "video.h"
27 
28 typedef struct DespillContext {
29  const AVClass *class;
30 
31  int co[4]; /* color offsets rgba */
32 
33  int alpha;
34  int type;
35  float spillmix;
36  float spillexpand;
37  float redscale;
38  float greenscale;
39  float bluescale;
40  float brightness;
42 
43 static int do_despill_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
44 {
45  DespillContext *s = ctx->priv;
46  AVFrame *frame = arg;
47  const int ro = s->co[0], go = s->co[1], bo = s->co[2], ao = s->co[3];
48  const int slice_start = (frame->height * jobnr) / nb_jobs;
49  const int slice_end = (frame->height * (jobnr + 1)) / nb_jobs;
50  const float brightness = s->brightness;
51  const float redscale = s->redscale;
52  const float greenscale = s->greenscale;
53  const float bluescale = s->bluescale;
54  const float spillmix = s->spillmix;
55  const float factor = (1.f - spillmix) * (1.f - s->spillexpand);
56  float red, green, blue;
57  int x, y;
58 
59  for (y = slice_start; y < slice_end; y++) {
60  uint8_t *dst = frame->data[0] + y * frame->linesize[0];
61 
62  for (x = 0; x < frame->width; x++) {
63  float spillmap;
64 
65  red = dst[x * 4 + ro] / 255.f;
66  green = dst[x * 4 + go] / 255.f;
67  blue = dst[x * 4 + bo] / 255.f;
68 
69  if (s->type) {
70  spillmap = FFMAX(blue - (red * spillmix + green * factor), 0.f);
71  } else {
72  spillmap = FFMAX(green - (red * spillmix + blue * factor), 0.f);
73  }
74 
75  red = FFMAX(red + spillmap * redscale + brightness * spillmap, 0.f);
76  green = FFMAX(green + spillmap * greenscale + brightness * spillmap, 0.f);
77  blue = FFMAX(blue + spillmap * bluescale + brightness * spillmap, 0.f);
78 
79  dst[x * 4 + ro] = av_clip_uint8(red * 255);
80  dst[x * 4 + go] = av_clip_uint8(green * 255);
81  dst[x * 4 + bo] = av_clip_uint8(blue * 255);
82  if (s->alpha) {
83  spillmap = 1.f - spillmap;
84  dst[x * 4 + ao] = av_clip_uint8(spillmap * 255);
85  }
86  }
87  }
88 
89  return 0;
90 }
91 
93 {
94  AVFilterContext *ctx = link->dst;
95  int ret;
96 
98  return ret;
99 
101  return ret;
102 
103  return ff_filter_frame(ctx->outputs[0], frame);
104 }
105 
106 static av_cold int config_output(AVFilterLink *outlink)
107 {
108  AVFilterContext *ctx = outlink->src;
109  DespillContext *s = ctx->priv;
111  int i;
112 
113  for (i = 0; i < 4; ++i)
114  s->co[i] = desc->comp[i].offset;
115 
116  return 0;
117 }
118 
120 {
121  static const enum AVPixelFormat pixel_fmts[] = {
127  };
129 
130  formats = ff_make_format_list(pixel_fmts);
131  if (!formats)
132  return AVERROR(ENOMEM);
133 
135 }
136 
137 static const AVFilterPad despill_inputs[] = {
138  {
139  .name = "default",
140  .type = AVMEDIA_TYPE_VIDEO,
141  .filter_frame = filter_frame,
142  },
143  { NULL }
144 };
145 
146 static const AVFilterPad despill_outputs[] = {
147  {
148  .name = "default",
149  .type = AVMEDIA_TYPE_VIDEO,
150  .config_props = config_output,
151  },
152  { NULL }
153 };
154 
155 #define OFFSET(x) offsetof(DespillContext, x)
156 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
157 
158 static const AVOption despill_options[] = {
159  { "type", "set the screen type", OFFSET(type), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, FLAGS, "type" },
160  { "green", "greenscreen", 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, FLAGS, "type" },
161  { "blue", "bluescreen", 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, FLAGS, "type" },
162  { "mix", "set the spillmap mix", OFFSET(spillmix), AV_OPT_TYPE_FLOAT, {.dbl=0.5}, 0, 1, FLAGS },
163  { "expand", "set the spillmap expand", OFFSET(spillexpand), AV_OPT_TYPE_FLOAT, {.dbl=0}, 0, 1, FLAGS },
164  { "red", "set red scale", OFFSET(redscale), AV_OPT_TYPE_FLOAT, {.dbl=0}, -100, 100, FLAGS },
165  { "green", "set green scale", OFFSET(greenscale), AV_OPT_TYPE_FLOAT, {.dbl=-1}, -100, 100, FLAGS },
166  { "blue", "set blue scale", OFFSET(bluescale), AV_OPT_TYPE_FLOAT, {.dbl=0}, -100, 100, FLAGS },
167  { "brightness", "set brightness", OFFSET(brightness), AV_OPT_TYPE_FLOAT, {.dbl=0}, -10, 10, FLAGS },
168  { "alpha", "change alpha component", OFFSET(alpha), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS },
169  { NULL }
170 };
171 
172 AVFILTER_DEFINE_CLASS(despill);
173 
175  .name = "despill",
176  .description = NULL_IF_CONFIG_SMALL("Despill video."),
177  .priv_size = sizeof(DespillContext),
178  .priv_class = &despill_class,
184 };
DespillContext::co
int co[4]
Definition: vf_despill.c:31
formats
formats
Definition: signature.h:48
AVPixelFormat
AVPixelFormat
Pixel format.
Definition: pixfmt.h:64
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
opt.h
ff_make_format_list
AVFilterFormats * ff_make_format_list(const int *fmts)
Create a list of supported formats.
Definition: formats.c:286
ff_filter_frame
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1096
av_pix_fmt_desc_get
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2573
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:318
av_frame_make_writable
int av_frame_make_writable(AVFrame *frame)
Ensure that the frame data is writable, avoiding data copy if possible.
Definition: frame.c:611
AVOption
AVOption.
Definition: opt.h:248
AV_PIX_FMT_BGRA
@ AV_PIX_FMT_BGRA
packed BGRA 8:8:8:8, 32bpp, BGRABGRA...
Definition: pixfmt.h:95
AVFilter::name
const char * name
Filter name.
Definition: avfilter.h:149
filter_frame
static int filter_frame(AVFilterLink *link, AVFrame *frame)
Definition: vf_despill.c:92
video.h
AVFormatContext::internal
AVFormatInternal * internal
An opaque field for libavformat internal usage.
Definition: avformat.h:1699
OFFSET
#define OFFSET(x)
Definition: vf_despill.c:155
AVFilterFormats
A list of supported formats for one end of a filter link.
Definition: formats.h:65
formats.h
FLAGS
#define FLAGS
Definition: vf_despill.c:156
do_despill_slice
static int do_despill_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
Definition: vf_despill.c:43
config_output
static av_cold int config_output(AVFilterLink *outlink)
Definition: vf_despill.c:106
type
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf type
Definition: writing_filters.txt:86
AVFilterPad
A filter pad used for either input or output.
Definition: internal.h:54
av_cold
#define av_cold
Definition: attributes.h:90
ff_set_common_formats
int ff_set_common_formats(AVFilterContext *ctx, AVFilterFormats *formats)
A helper for query_formats() which sets all links to the same list of formats.
Definition: formats.c:587
s
#define s(width, name)
Definition: cbs_vp9.c:257
slice_end
static int slice_end(AVCodecContext *avctx, AVFrame *pict)
Handle slice ends.
Definition: mpeg12dec.c:2033
outputs
static const AVFilterPad outputs[]
Definition: af_acontrast.c:203
ctx
AVFormatContext * ctx
Definition: movenc.c:48
DespillContext::redscale
float redscale
Definition: vf_despill.c:37
f
#define f(width, name)
Definition: cbs_vp9.c:255
link
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a link
Definition: filter_design.txt:23
AV_PIX_FMT_RGBA
@ AV_PIX_FMT_RGBA
packed RGBA 8:8:8:8, 32bpp, RGBARGBA...
Definition: pixfmt.h:93
arg
const char * arg
Definition: jacosubdec.c:66
AVFILTER_DEFINE_CLASS
AVFILTER_DEFINE_CLASS(despill)
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:67
NULL
#define NULL
Definition: coverity.c:32
query_formats
static av_cold int query_formats(AVFilterContext *ctx)
Definition: vf_despill.c:119
DespillContext::greenscale
float greenscale
Definition: vf_despill.c:38
inputs
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several inputs
Definition: filter_design.txt:243
AV_PIX_FMT_ABGR
@ AV_PIX_FMT_ABGR
packed ABGR 8:8:8:8, 32bpp, ABGRABGR...
Definition: pixfmt.h:94
DespillContext::spillmix
float spillmix
Definition: vf_despill.c:35
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:117
DespillContext::spillexpand
float spillexpand
Definition: vf_despill.c:36
DespillContext::alpha
int alpha
Definition: vf_despill.c:33
process_command
static int process_command(AVFilterContext *ctx, const char *cmd, const char *args, char *res, int res_len, int flags)
Definition: af_acrusher.c:336
FFMAX
#define FFMAX(a, b)
Definition: common.h:103
despill_options
static const AVOption despill_options[]
Definition: vf_despill.c:158
ff_filter_process_command
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:882
FFMIN
#define FFMIN(a, b)
Definition: common.h:105
internal.h
AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC
#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:126
AV_PIX_FMT_ARGB
@ AV_PIX_FMT_ARGB
packed ARGB 8:8:8:8, 32bpp, ARGBARGB...
Definition: pixfmt.h:92
AV_OPT_TYPE_FLOAT
@ AV_OPT_TYPE_FLOAT
Definition: opt.h:228
DespillContext::brightness
float brightness
Definition: vf_despill.c:40
i
int i
Definition: input.c:407
ff_filter_get_nb_threads
int ff_filter_get_nb_threads(AVFilterContext *ctx)
Get number of threads for current filter instance.
Definition: avfilter.c:802
uint8_t
uint8_t
Definition: audio_convert.c:194
AVFilterPad::name
const char * name
Pad name.
Definition: internal.h:60
AVFilter
Filter definition.
Definition: avfilter.h:145
ret
ret
Definition: filter_design.txt:187
frame
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return or at least make progress towards producing a frame
Definition: filter_design.txt:264
despill_inputs
static const AVFilterPad despill_inputs[]
Definition: vf_despill.c:137
AV_PIX_FMT_NONE
@ AV_PIX_FMT_NONE
Definition: pixfmt.h:65
DespillContext
Definition: vf_despill.c:28
AV_OPT_TYPE_INT
@ AV_OPT_TYPE_INT
Definition: opt.h:225
avfilter.h
av_clip_uint8
#define av_clip_uint8
Definition: common.h:128
despill_outputs
static const AVFilterPad despill_outputs[]
Definition: vf_despill.c:146
AVFilterContext
An instance of a filter.
Definition: avfilter.h:341
factor
static const int factor[16]
Definition: vf_pp7.c:77
AVFILTER_FLAG_SLICE_THREADS
#define AVFILTER_FLAG_SLICE_THREADS
The filter supports multithreading by splitting frames into multiple parts and processing them concur...
Definition: avfilter.h:117
desc
const char * desc
Definition: libsvtav1.c:79
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
AVPixFmtDescriptor
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:81
alpha
static const int16_t alpha[]
Definition: ilbcdata.h:55
AV_OPT_TYPE_BOOL
@ AV_OPT_TYPE_BOOL
Definition: opt.h:242
imgutils.h
flags
#define flags(name, subs,...)
Definition: cbs_av1.c:561
ff_vf_despill
AVFilter ff_vf_despill
Definition: vf_despill.c:174
AV_OPT_TYPE_CONST
@ AV_OPT_TYPE_CONST
Definition: opt.h:234
DespillContext::type
int type
Definition: vf_despill.c:34
DespillContext::bluescale
float bluescale
Definition: vf_despill.c:39