FFmpeg
vf_backgroundkey.c
Go to the documentation of this file.
1 /*
2  * This file is part of FFmpeg.
3  *
4  * FFmpeg is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * FFmpeg is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with FFmpeg; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18 
19 #include "libavutil/mem.h"
20 #include "libavutil/opt.h"
21 #include "libavutil/pixdesc.h"
22 #include "avfilter.h"
23 #include "filters.h"
24 #include "video.h"
25 
26 typedef struct BackgroundkeyContext {
27  const AVClass *class;
28 
29  float threshold;
30  float similarity;
31  float blend;
32  int max;
33 
35  int hsub_log2;
36  int vsub_log2;
37 
40 
42 
43  int (*do_slice)(AVFilterContext *avctx, void *arg,
44  int jobnr, int nb_jobs);
46 
47 static int do_backgroundkey_slice(AVFilterContext *avctx, void *arg, int jobnr, int nb_jobs)
48 {
49  BackgroundkeyContext *s = avctx->priv;
50  AVFrame *frame = arg;
51  const int slice_start = (frame->height * jobnr) / nb_jobs;
52  const int slice_end = (frame->height * (jobnr + 1)) / nb_jobs;
53  const int min_diff = (255 + 255 + 255) * s->similarity;
54  const float blend = s->blend;
55  const int hsub = s->hsub_log2;
56  const int vsub = s->vsub_log2;
57  int64_t sum = 0;
58 
59  for (int y = slice_start; y < slice_end; y++) {
60  const uint8_t *srcy = frame->data[0] + frame->linesize[0] * y;
61  const uint8_t *srcu = frame->data[1] + frame->linesize[1] * (y >> vsub);
62  const uint8_t *srcv = frame->data[2] + frame->linesize[2] * (y >> vsub);
63  const uint8_t *bsrcy = s->background->data[0] + s->background->linesize[0] * y;
64  const uint8_t *bsrcu = s->background->data[1] + s->background->linesize[1] * (y >> vsub);
65  const uint8_t *bsrcv = s->background->data[2] + s->background->linesize[2] * (y >> vsub);
66  uint8_t *dst = frame->data[3] + frame->linesize[3] * y;
67  for (int x = 0; x < frame->width; x++) {
68  const int xx = x >> hsub;
69  const int diff = FFABS(srcy[x] - bsrcy[x]) +
70  FFABS(srcu[xx] - bsrcu[xx]) +
71  FFABS(srcv[xx] - bsrcv[xx]);
72  int A;
73 
74  sum += diff;
75  if (blend > 0.f) {
76  A = 255 - av_clipf((min_diff - diff) / blend, 0.f, 255.f);
77  } else {
78  A = (diff > min_diff) ? 255 : 0;
79  }
80 
81  dst[x] = A;
82  }
83  }
84 
85  s->sums[jobnr] = sum;
86 
87  return 0;
88 }
89 
90 static int do_backgroundkey16_slice(AVFilterContext *avctx, void *arg, int jobnr, int nb_jobs)
91 {
92  BackgroundkeyContext *s = avctx->priv;
93  AVFrame *frame = arg;
94  const int slice_start = (frame->height * jobnr) / nb_jobs;
95  const int slice_end = (frame->height * (jobnr + 1)) / nb_jobs;
96  const int hsub = s->hsub_log2;
97  const int vsub = s->vsub_log2;
98  const int max = s->max;
99  const int min_diff = s->similarity * (s->max + s->max + s->max);
100  const float blend = s->blend;
101  int64_t sum = 0;
102 
103  for (int y = slice_start; y < slice_end; y++) {
104  const uint16_t *srcy = (const uint16_t *)(frame->data[0] + frame->linesize[0] * y);
105  const uint16_t *srcu = (const uint16_t *)(frame->data[1] + frame->linesize[1] * (y >> vsub));
106  const uint16_t *srcv = (const uint16_t *)(frame->data[2] + frame->linesize[2] * (y >> vsub));
107  const uint16_t *bsrcy = (const uint16_t *)(s->background->data[0] + s->background->linesize[0] * y);
108  const uint16_t *bsrcu = (const uint16_t *)(s->background->data[1] + s->background->linesize[1] * (y >> vsub));
109  const uint16_t *bsrcv = (const uint16_t *)(s->background->data[2] + s->background->linesize[2] * (y >> vsub));
110  uint16_t *dst = (uint16_t *)(frame->data[3] + frame->linesize[3] * y);
111  for (int x = 0; x < frame->width; x++) {
112  const int xx = x >> hsub;
113  const int diff = FFABS(srcy[x] - bsrcy[x] ) +
114  FFABS(srcu[xx] - bsrcu[xx]) +
115  FFABS(srcv[xx] - bsrcv[xx]);
116  int A;
117 
118  sum += diff;
119  if (blend > 0.f) {
120  A = max - av_clipf((min_diff - diff) / blend, 0.f, max);
121  } else {
122  A = (diff > min_diff) ? max : 0;
123  }
124 
125  dst[x] = A;
126  }
127  }
128 
129  s->sums[jobnr] = sum;
130 
131  return 0;
132 }
133 
135 {
136  AVFilterContext *avctx = link->dst;
137  BackgroundkeyContext *s = avctx->priv;
138  int64_t sum = 0;
139  int ret = 0;
140 
141  if (!s->background) {
142  s->background = ff_get_video_buffer(link, frame->width, frame->height);
143  if (!s->background) {
144  ret = AVERROR(ENOMEM);
145  goto fail;
146  }
147  ret = av_frame_copy(s->background, frame);
148  if (ret < 0)
149  goto fail;
150  }
151 
152  if (ret = ff_filter_execute(avctx, s->do_slice, frame, NULL,
153  FFMIN(frame->height, s->nb_threads)))
154  goto fail;
155 
156  for (int n = 0; n < s->nb_threads; n++)
157  sum += s->sums[n];
158  if (s->max_sum * s->threshold < sum) {
159  ret = av_frame_copy(s->background, frame);
160  if (ret < 0)
161  goto fail;
162  }
163 
164  return ff_filter_frame(avctx->outputs[0], frame);
165 fail:
167  return ret;
168 }
169 
170 static av_cold int config_output(AVFilterLink *outlink)
171 {
173  AVFilterContext *avctx = outlink->src;
174  AVFilterLink *inlink = avctx->inputs[0];
175  BackgroundkeyContext *s = avctx->priv;
176  int depth;
177 
178  s->nb_threads = ff_filter_get_nb_threads(avctx);
179  depth = desc->comp[0].depth;
180  s->do_slice = depth <= 8 ? do_backgroundkey_slice : do_backgroundkey16_slice;
181  s->max = (1 << depth) - 1;
182  s->hsub_log2 = desc->log2_chroma_w;
183  s->vsub_log2 = desc->log2_chroma_h;
184  s->max_sum = (int64_t)(inlink->w) * inlink->h * s->max;
185  s->max_sum += 2LL * (inlink->w >> s->hsub_log2) * (inlink->h >> s->vsub_log2) * s->max;
186 
187  s->sums = av_calloc(s->nb_threads, sizeof(*s->sums));
188  if (!s->sums)
189  return AVERROR(ENOMEM);
190 
191  return 0;
192 }
193 
195 {
196  BackgroundkeyContext *s = ctx->priv;
197 
198  av_frame_free(&s->background);
199  av_freep(&s->sums);
200 }
201 
203  {
204  .name = "default",
205  .type = AVMEDIA_TYPE_VIDEO,
207  .filter_frame = filter_frame,
208  },
209 };
210 
212  {
213  .name = "default",
214  .type = AVMEDIA_TYPE_VIDEO,
215  .config_props = config_output,
216  },
217 };
218 
219 #define OFFSET(x) offsetof(BackgroundkeyContext, x)
220 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
221 
222 static const AVOption backgroundkey_options[] = {
223  { "threshold", "set the scene change threshold", OFFSET(threshold), AV_OPT_TYPE_FLOAT, { .dbl = 0.08}, 0.0, 1.0, FLAGS },
224  { "similarity", "set the similarity", OFFSET(similarity), AV_OPT_TYPE_FLOAT, { .dbl = 0.1 }, 0.0, 1.0, FLAGS },
225  { "blend", "set the blend value", OFFSET(blend), AV_OPT_TYPE_FLOAT, { .dbl = 0.0 }, 0.0, 1.0, FLAGS },
226  { NULL }
227 };
228 
229 static const enum AVPixelFormat backgroundkey_fmts[] = {
240 };
241 
242 AVFILTER_DEFINE_CLASS(backgroundkey);
243 
245  .name = "backgroundkey",
246  .description = NULL_IF_CONFIG_SMALL("Turns a static background into transparency."),
247  .priv_size = sizeof(BackgroundkeyContext),
248  .priv_class = &backgroundkey_class,
249  .uninit = uninit,
254  .process_command = ff_filter_process_command,
255 };
ff_get_video_buffer
AVFrame * ff_get_video_buffer(AVFilterLink *link, int w, int h)
Request a picture buffer with a specific set of permissions.
Definition: video.c:116
AV_PIX_FMT_YUVA422P16
#define AV_PIX_FMT_YUVA422P16
Definition: pixfmt.h:546
BackgroundkeyContext::max
int max
Definition: vf_backgroundkey.c:32
A
#define A(x)
Definition: vpx_arith.h:28
AV_PIX_FMT_GBRAP16
#define AV_PIX_FMT_GBRAP16
Definition: pixfmt.h:525
AVPixelFormat
AVPixelFormat
Pixel format.
Definition: pixfmt.h:71
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
FILTER_PIXFMTS_ARRAY
#define FILTER_PIXFMTS_ARRAY(array)
Definition: filters.h:242
backgroundkey_fmts
static enum AVPixelFormat backgroundkey_fmts[]
Definition: vf_backgroundkey.c:229
ff_filter_frame
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1062
av_pix_fmt_desc_get
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:3170
int64_t
long long int64_t
Definition: coverity.c:34
inlink
The exact code depends on how similar the blocks are and how related they are to the and needs to apply these operations to the correct inlink or outlink if there are several Macros are available to factor that when no extra processing is inlink
Definition: filter_design.txt:212
av_frame_free
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:162
AV_PIX_FMT_YUVA422P9
#define AV_PIX_FMT_YUVA422P9
Definition: pixfmt.h:538
FILTER_INPUTS
#define FILTER_INPUTS(array)
Definition: filters.h:262
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:389
pixdesc.h
AV_PIX_FMT_YUVA420P16
#define AV_PIX_FMT_YUVA420P16
Definition: pixfmt.h:545
AV_PIX_FMT_YUVA420P10
#define AV_PIX_FMT_YUVA420P10
Definition: pixfmt.h:540
BackgroundkeyContext::nb_threads
int nb_threads
Definition: vf_backgroundkey.c:34
AVOption
AVOption.
Definition: opt.h:429
max
#define max(a, b)
Definition: cuda_runtime.h:33
AVFilter::name
const char * name
Filter name.
Definition: avfilter.h:205
backgroundkey_options
static const AVOption backgroundkey_options[]
Definition: vf_backgroundkey.c:222
video.h
AV_PIX_FMT_YUVA422P10
#define AV_PIX_FMT_YUVA422P10
Definition: pixfmt.h:541
BackgroundkeyContext::similarity
float similarity
Definition: vf_backgroundkey.c:30
hsub
static void hsub(htype *dst, const htype *src, int bins)
Definition: vf_median.c:74
uninit
static av_cold void uninit(AVFilterContext *ctx)
Definition: vf_backgroundkey.c:194
AV_PIX_FMT_YUVA420P9
#define AV_PIX_FMT_YUVA420P9
Definition: pixfmt.h:537
slice_end
static int slice_end(AVCodecContext *avctx, AVFrame *pict, int *got_output)
Handle slice ends.
Definition: mpeg12dec.c:1719
AV_PIX_FMT_GBRAP
@ AV_PIX_FMT_GBRAP
planar GBRA 4:4:4:4 32bpp
Definition: pixfmt.h:212
AVFilterContext::priv
void * priv
private data for use by the filter
Definition: avfilter.h:472
fail
#define fail()
Definition: checkasm.h:188
BackgroundkeyContext::blend
float blend
Definition: vf_backgroundkey.c:31
AV_PIX_FMT_YUVA444P16
#define AV_PIX_FMT_YUVA444P16
Definition: pixfmt.h:547
filter_frame
static int filter_frame(AVFilterLink *link, AVFrame *frame)
Definition: vf_backgroundkey.c:134
AVFilterPad
A filter pad used for either input or output.
Definition: filters.h:38
do_backgroundkey16_slice
static int do_backgroundkey16_slice(AVFilterContext *avctx, void *arg, int jobnr, int nb_jobs)
Definition: vf_backgroundkey.c:90
av_cold
#define av_cold
Definition: attributes.h:90
AV_PIX_FMT_GBRAP10
#define AV_PIX_FMT_GBRAP10
Definition: pixfmt.h:522
backgroundkey_outputs
static const AVFilterPad backgroundkey_outputs[]
Definition: vf_backgroundkey.c:211
BackgroundkeyContext::max_sum
int64_t max_sum
Definition: vf_backgroundkey.c:38
backgroundkey_inputs
static const AVFilterPad backgroundkey_inputs[]
Definition: vf_backgroundkey.c:202
s
#define s(width, name)
Definition: cbs_vp9.c:198
AV_PIX_FMT_GBRAP12
#define AV_PIX_FMT_GBRAP12
Definition: pixfmt.h:523
AV_PIX_FMT_YUVA420P
@ AV_PIX_FMT_YUVA420P
planar YUV 4:2:0, 20bpp, (1 Cr & Cb sample per 2x2 Y & A samples)
Definition: pixfmt.h:108
filters.h
OFFSET
#define OFFSET(x)
Definition: vf_backgroundkey.c:219
AV_PIX_FMT_YUVA444P12
#define AV_PIX_FMT_YUVA444P12
Definition: pixfmt.h:544
ctx
AVFormatContext * ctx
Definition: movenc.c:49
AVFILTER_DEFINE_CLASS
AVFILTER_DEFINE_CLASS(backgroundkey)
FILTER_OUTPUTS
#define FILTER_OUTPUTS(array)
Definition: filters.h:263
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
do_backgroundkey_slice
static int do_backgroundkey_slice(AVFilterContext *avctx, void *arg, int jobnr, int nb_jobs)
Definition: vf_backgroundkey.c:47
arg
const char * arg
Definition: jacosubdec.c:67
FFABS
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
Definition: common.h:74
if
if(ret)
Definition: filter_design.txt:179
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:75
NULL
#define NULL
Definition: coverity.c:32
AVFilterContext::inputs
AVFilterLink ** inputs
array of pointers to input links
Definition: avfilter.h:465
FLAGS
#define FLAGS
Definition: vf_backgroundkey.c:220
av_clipf
av_clipf
Definition: af_crystalizer.c:122
ff_vf_backgroundkey
const AVFilter ff_vf_backgroundkey
Definition: vf_backgroundkey.c:244
BackgroundkeyContext
Definition: vf_backgroundkey.c:26
BackgroundkeyContext::sums
int64_t * sums
Definition: vf_backgroundkey.c:39
BackgroundkeyContext::vsub_log2
int vsub_log2
Definition: vf_backgroundkey.c:36
AVFILTERPAD_FLAG_NEEDS_WRITABLE
#define AVFILTERPAD_FLAG_NEEDS_WRITABLE
The filter expects writable frames from its input link, duplicating data buffers if needed.
Definition: filters.h:57
f
f
Definition: af_crystalizer.c:122
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:94
dst
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
Definition: dsp.h:83
av_frame_copy
int av_frame_copy(AVFrame *dst, const AVFrame *src)
Copy the frame data from src to dst.
Definition: frame.c:1003
for
for(k=2;k<=8;++k)
Definition: h264pred_template.c:425
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:901
diff
static av_always_inline int diff(const struct color_info *a, const struct color_info *b, const int trans_thresh)
Definition: vf_paletteuse.c:166
AV_PIX_FMT_YUVA444P
@ 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_YUVA444P10
#define AV_PIX_FMT_YUVA444P10
Definition: pixfmt.h:542
BackgroundkeyContext::hsub_log2
int hsub_log2
Definition: vf_backgroundkey.c:35
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:182
AV_OPT_TYPE_FLOAT
@ AV_OPT_TYPE_FLOAT
Underlying C type is float.
Definition: opt.h:271
ff_filter_get_nb_threads
int ff_filter_get_nb_threads(AVFilterContext *ctx)
Get number of threads for current filter instance.
Definition: avfilter.c:841
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
config_output
static av_cold int config_output(AVFilterLink *outlink)
Definition: vf_backgroundkey.c:170
AVFilterPad::name
const char * name
Pad name.
Definition: filters.h:44
av_calloc
void * av_calloc(size_t nmemb, size_t size)
Definition: mem.c:264
slice_start
static int slice_start(SliceContext *sc, VVCContext *s, VVCFrameContext *fc, const CodedBitstreamUnit *unit, const int is_first_slice)
Definition: dec.c:738
AVFilter
Filter definition.
Definition: avfilter.h:201
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
AV_PIX_FMT_YUVA444P9
#define AV_PIX_FMT_YUVA444P9
Definition: pixfmt.h:539
ff_filter_execute
int ff_filter_execute(AVFilterContext *ctx, avfilter_action_func *func, void *arg, int *ret, int nb_jobs)
Definition: avfilter.c:1667
AV_PIX_FMT_NONE
@ AV_PIX_FMT_NONE
Definition: pixfmt.h:72
AV_PIX_FMT_YUVA422P12
#define AV_PIX_FMT_YUVA422P12
Definition: pixfmt.h:543
avfilter.h
BackgroundkeyContext::background
AVFrame * background
Definition: vf_backgroundkey.c:41
BackgroundkeyContext::threshold
float threshold
Definition: vf_backgroundkey.c:29
AVFilterContext
An instance of a filter.
Definition: avfilter.h:457
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:152
desc
const char * desc
Definition: libsvtav1.c:79
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
mem.h
AVPixFmtDescriptor
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:69
BackgroundkeyContext::do_slice
int(* do_slice)(AVFilterContext *avctx, void *arg, int jobnr, int nb_jobs)
Definition: vf_backgroundkey.c:43
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:34
AV_PIX_FMT_YUVA422P
@ AV_PIX_FMT_YUVA422P
planar YUV 4:2:2 24bpp, (1 Cr & Cb sample per 2x1 Y & A samples)
Definition: pixfmt.h:173
AVFilterContext::outputs
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:469