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/opt.h"
20 #include "libavutil/pixdesc.h"
21 #include "avfilter.h"
22 #include "internal.h"
23 #include "video.h"
24 
25 typedef struct BackgroundkeyContext {
26  const AVClass *class;
27 
28  float threshold;
29  float similarity;
30  float blend;
31  int max;
32 
34  int hsub_log2;
35  int vsub_log2;
36 
37  int64_t max_sum;
38  int64_t *sums;
39 
41 
42  int (*do_slice)(AVFilterContext *avctx, void *arg,
43  int jobnr, int nb_jobs);
45 
46 static int do_backgroundkey_slice(AVFilterContext *avctx, void *arg, int jobnr, int nb_jobs)
47 {
48  BackgroundkeyContext *s = avctx->priv;
49  AVFrame *frame = arg;
50  const int slice_start = (frame->height * jobnr) / nb_jobs;
51  const int slice_end = (frame->height * (jobnr + 1)) / nb_jobs;
52  const int min_diff = (255 + 255 + 255) * s->similarity;
53  const float blend = s->blend;
54  const int hsub = s->hsub_log2;
55  const int vsub = s->vsub_log2;
56  int64_t sum = 0;
57 
58  for (int y = slice_start; y < slice_end; y++) {
59  const uint8_t *srcy = frame->data[0] + frame->linesize[0] * y;
60  const uint8_t *srcu = frame->data[1] + frame->linesize[1] * (y >> vsub);
61  const uint8_t *srcv = frame->data[2] + frame->linesize[2] * (y >> vsub);
62  const uint8_t *bsrcy = s->background->data[0] + s->background->linesize[0] * y;
63  const uint8_t *bsrcu = s->background->data[1] + s->background->linesize[1] * (y >> vsub);
64  const uint8_t *bsrcv = s->background->data[2] + s->background->linesize[2] * (y >> vsub);
65  uint8_t *dst = frame->data[3] + frame->linesize[3] * y;
66  for (int x = 0; x < frame->width; x++) {
67  const int xx = x >> hsub;
68  const int diff = FFABS(srcy[x] - bsrcy[x]) +
69  FFABS(srcu[xx] - bsrcu[xx]) +
70  FFABS(srcv[xx] - bsrcv[xx]);
71  int A;
72 
73  sum += diff;
74  if (blend > 0.f) {
75  A = 255 - av_clipf((min_diff - diff) / blend, 0.f, 255.f);
76  } else {
77  A = (diff > min_diff) ? 255 : 0;
78  }
79 
80  dst[x] = A;
81  }
82  }
83 
84  s->sums[jobnr] = sum;
85 
86  return 0;
87 }
88 
89 static int do_backgroundkey16_slice(AVFilterContext *avctx, void *arg, int jobnr, int nb_jobs)
90 {
91  BackgroundkeyContext *s = avctx->priv;
92  AVFrame *frame = arg;
93  const int slice_start = (frame->height * jobnr) / nb_jobs;
94  const int slice_end = (frame->height * (jobnr + 1)) / nb_jobs;
95  const int hsub = s->hsub_log2;
96  const int vsub = s->vsub_log2;
97  const int max = s->max;
98  const int min_diff = s->similarity * (s->max + s->max + s->max);
99  const float blend = s->blend;
100  int64_t sum = 0;
101 
102  for (int y = slice_start; y < slice_end; y++) {
103  const uint16_t *srcy = (const uint16_t *)(frame->data[0] + frame->linesize[0] * y);
104  const uint16_t *srcu = (const uint16_t *)(frame->data[1] + frame->linesize[1] * (y >> vsub));
105  const uint16_t *srcv = (const uint16_t *)(frame->data[2] + frame->linesize[2] * (y >> vsub));
106  const uint16_t *bsrcy = (const uint16_t *)(s->background->data[0] + s->background->linesize[0] * y);
107  const uint16_t *bsrcu = (const uint16_t *)(s->background->data[1] + s->background->linesize[1] * (y >> vsub));
108  const uint16_t *bsrcv = (const uint16_t *)(s->background->data[2] + s->background->linesize[2] * (y >> vsub));
109  uint16_t *dst = (uint16_t *)(frame->data[3] + frame->linesize[3] * y);
110  for (int x = 0; x < frame->width; x++) {
111  const int xx = x >> hsub;
112  const int diff = FFABS(srcy[x] - bsrcy[x] ) +
113  FFABS(srcu[xx] - bsrcu[xx]) +
114  FFABS(srcv[xx] - bsrcv[xx]);
115  int A;
116 
117  sum += diff;
118  if (blend > 0.f) {
119  A = max - av_clipf((min_diff - diff) / blend, 0.f, max);
120  } else {
121  A = (diff > min_diff) ? max : 0;
122  }
123 
124  dst[x] = A;
125  }
126  }
127 
128  s->sums[jobnr] = sum;
129 
130  return 0;
131 }
132 
134 {
135  AVFilterContext *avctx = link->dst;
136  BackgroundkeyContext *s = avctx->priv;
137  int64_t sum = 0;
138  int ret = 0;
139 
140  if (!s->background) {
141  s->background = ff_get_video_buffer(link, frame->width, frame->height);
142  if (!s->background) {
143  ret = AVERROR(ENOMEM);
144  goto fail;
145  }
146  ret = av_frame_copy(s->background, frame);
147  if (ret < 0)
148  goto fail;
149  }
150 
151  if (ret = ff_filter_execute(avctx, s->do_slice, frame, NULL,
152  FFMIN(frame->height, s->nb_threads)))
153  goto fail;
154 
155  for (int n = 0; n < s->nb_threads; n++)
156  sum += s->sums[n];
157  if (s->max_sum * s->threshold < sum) {
158  ret = av_frame_copy(s->background, frame);
159  if (ret < 0)
160  goto fail;
161  }
162 
163  return ff_filter_frame(avctx->outputs[0], frame);
164 fail:
166  return ret;
167 }
168 
169 static av_cold int config_output(AVFilterLink *outlink)
170 {
172  AVFilterContext *avctx = outlink->src;
173  AVFilterLink *inlink = avctx->inputs[0];
174  BackgroundkeyContext *s = avctx->priv;
175  int depth;
176 
177  s->nb_threads = ff_filter_get_nb_threads(avctx);
178  depth = desc->comp[0].depth;
179  s->do_slice = depth <= 8 ? do_backgroundkey_slice : do_backgroundkey16_slice;
180  s->max = (1 << depth) - 1;
181  s->hsub_log2 = desc->log2_chroma_w;
182  s->vsub_log2 = desc->log2_chroma_h;
183  s->max_sum = (int64_t)(inlink->w) * inlink->h * s->max;
184  s->max_sum += 2LL * (inlink->w >> s->hsub_log2) * (inlink->h >> s->vsub_log2) * s->max;
185 
186  s->sums = av_calloc(s->nb_threads, sizeof(*s->sums));
187  if (!s->sums)
188  return AVERROR(ENOMEM);
189 
190  return 0;
191 }
192 
194 {
195  BackgroundkeyContext *s = ctx->priv;
196 
197  av_frame_free(&s->background);
198  av_freep(&s->sums);
199 }
200 
202  {
203  .name = "default",
204  .type = AVMEDIA_TYPE_VIDEO,
206  .filter_frame = filter_frame,
207  },
208 };
209 
211  {
212  .name = "default",
213  .type = AVMEDIA_TYPE_VIDEO,
214  .config_props = config_output,
215  },
216 };
217 
218 #define OFFSET(x) offsetof(BackgroundkeyContext, x)
219 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
220 
221 static const AVOption backgroundkey_options[] = {
222  { "threshold", "set the scene change threshold", OFFSET(threshold), AV_OPT_TYPE_FLOAT, { .dbl = 0.08}, 0.0, 1.0, FLAGS },
223  { "similarity", "set the similarity", OFFSET(similarity), AV_OPT_TYPE_FLOAT, { .dbl = 0.1 }, 0.0, 1.0, FLAGS },
224  { "blend", "set the blend value", OFFSET(blend), AV_OPT_TYPE_FLOAT, { .dbl = 0.0 }, 0.0, 1.0, FLAGS },
225  { NULL }
226 };
227 
228 static const enum AVPixelFormat backgroundkey_fmts[] = {
239 };
240 
241 AVFILTER_DEFINE_CLASS(backgroundkey);
242 
244  .name = "backgroundkey",
245  .description = NULL_IF_CONFIG_SMALL("Turns a static background into transparency."),
246  .priv_size = sizeof(BackgroundkeyContext),
247  .priv_class = &backgroundkey_class,
248  .uninit = uninit,
253  .process_command = ff_filter_process_command,
254 };
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:112
AV_PIX_FMT_YUVA422P16
#define AV_PIX_FMT_YUVA422P16
Definition: pixfmt.h:522
BackgroundkeyContext::max
int max
Definition: vf_backgroundkey.c:31
A
#define A(x)
Definition: vpx_arith.h:28
AV_PIX_FMT_GBRAP16
#define AV_PIX_FMT_GBRAP16
Definition: pixfmt.h:501
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
backgroundkey_fmts
static enum AVPixelFormat backgroundkey_fmts[]
Definition: vf_backgroundkey.c:228
ff_filter_frame
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1018
av_pix_fmt_desc_get
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2962
FILTER_PIXFMTS_ARRAY
#define FILTER_PIXFMTS_ARRAY(array)
Definition: internal.h:162
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:160
AV_PIX_FMT_YUVA422P9
#define AV_PIX_FMT_YUVA422P9
Definition: pixfmt.h:514
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:375
pixdesc.h
AV_PIX_FMT_YUVA420P16
#define AV_PIX_FMT_YUVA420P16
Definition: pixfmt.h:521
AV_PIX_FMT_YUVA420P10
#define AV_PIX_FMT_YUVA420P10
Definition: pixfmt.h:516
BackgroundkeyContext::nb_threads
int nb_threads
Definition: vf_backgroundkey.c:33
AVOption
AVOption.
Definition: opt.h:346
max
#define max(a, b)
Definition: cuda_runtime.h:33
AVFilter::name
const char * name
Filter name.
Definition: avfilter.h:170
backgroundkey_options
static const AVOption backgroundkey_options[]
Definition: vf_backgroundkey.c:221
video.h
AV_PIX_FMT_YUVA422P10
#define AV_PIX_FMT_YUVA422P10
Definition: pixfmt.h:517
BackgroundkeyContext::similarity
float similarity
Definition: vf_backgroundkey.c:29
hsub
static void hsub(htype *dst, const htype *src, int bins)
Definition: vf_median.c:73
uninit
static av_cold void uninit(AVFilterContext *ctx)
Definition: vf_backgroundkey.c:193
AV_PIX_FMT_YUVA420P9
#define AV_PIX_FMT_YUVA420P9
Definition: pixfmt.h:513
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:422
fail
#define fail()
Definition: checkasm.h:179
BackgroundkeyContext::blend
float blend
Definition: vf_backgroundkey.c:30
AV_PIX_FMT_YUVA444P16
#define AV_PIX_FMT_YUVA444P16
Definition: pixfmt.h:523
filter_frame
static int filter_frame(AVFilterLink *link, AVFrame *frame)
Definition: vf_backgroundkey.c:133
AVFilterPad
A filter pad used for either input or output.
Definition: internal.h:33
slice_start
static int slice_start(SliceContext *sc, VVCContext *s, VVCFrameContext *fc, const CodedBitstreamUnit *unit, const int is_first_slice)
Definition: vvcdec.c:685
do_backgroundkey16_slice
static int do_backgroundkey16_slice(AVFilterContext *avctx, void *arg, int jobnr, int nb_jobs)
Definition: vf_backgroundkey.c:89
av_cold
#define av_cold
Definition: attributes.h:90
AV_PIX_FMT_GBRAP10
#define AV_PIX_FMT_GBRAP10
Definition: pixfmt.h:498
backgroundkey_outputs
static const AVFilterPad backgroundkey_outputs[]
Definition: vf_backgroundkey.c:210
BackgroundkeyContext::max_sum
int64_t max_sum
Definition: vf_backgroundkey.c:37
backgroundkey_inputs
static const AVFilterPad backgroundkey_inputs[]
Definition: vf_backgroundkey.c:201
s
#define s(width, name)
Definition: cbs_vp9.c:198
AV_PIX_FMT_GBRAP12
#define AV_PIX_FMT_GBRAP12
Definition: pixfmt.h:499
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
slice_end
static int slice_end(AVCodecContext *avctx, AVFrame *pict)
Handle slice ends.
Definition: mpeg12dec.c:1725
OFFSET
#define OFFSET(x)
Definition: vf_backgroundkey.c:218
AV_PIX_FMT_YUVA444P12
#define AV_PIX_FMT_YUVA444P12
Definition: pixfmt.h:520
ctx
AVFormatContext * ctx
Definition: movenc.c:48
AVFILTER_DEFINE_CLASS
AVFILTER_DEFINE_CLASS(backgroundkey)
FILTER_INPUTS
#define FILTER_INPUTS(array)
Definition: internal.h:182
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:46
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:72
if
if(ret)
Definition: filter_design.txt:179
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:66
NULL
#define NULL
Definition: coverity.c:32
AVFilterContext::inputs
AVFilterLink ** inputs
array of pointers to input links
Definition: avfilter.h:415
FLAGS
#define FLAGS
Definition: vf_backgroundkey.c:219
av_clipf
av_clipf
Definition: af_crystalizer.c:121
ff_vf_backgroundkey
const AVFilter ff_vf_backgroundkey
Definition: vf_backgroundkey.c:243
BackgroundkeyContext
Definition: vf_backgroundkey.c:25
BackgroundkeyContext::sums
int64_t * sums
Definition: vf_backgroundkey.c:38
BackgroundkeyContext::vsub_log2
int vsub_log2
Definition: vf_backgroundkey.c:35
f
f
Definition: af_crystalizer.c:121
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:106
av_frame_copy
int av_frame_copy(AVFrame *dst, const AVFrame *src)
Copy the frame data from src to dst.
Definition: frame.c:910
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:890
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:164
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:518
BackgroundkeyContext::hsub_log2
int hsub_log2
Definition: vf_backgroundkey.c:34
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:147
AV_OPT_TYPE_FLOAT
@ AV_OPT_TYPE_FLOAT
Definition: opt.h:238
ff_filter_get_nb_threads
int ff_filter_get_nb_threads(AVFilterContext *ctx)
Get number of threads for current filter instance.
Definition: avfilter.c:825
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
config_output
static av_cold int config_output(AVFilterLink *outlink)
Definition: vf_backgroundkey.c:169
AVFilterPad::name
const char * name
Pad name.
Definition: internal.h:39
av_calloc
void * av_calloc(size_t nmemb, size_t size)
Definition: mem.c:262
AVFilter
Filter definition.
Definition: avfilter.h:166
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:515
AV_PIX_FMT_NONE
@ AV_PIX_FMT_NONE
Definition: pixfmt.h:72
AV_PIX_FMT_YUVA422P12
#define AV_PIX_FMT_YUVA422P12
Definition: pixfmt.h:519
avfilter.h
BackgroundkeyContext::background
AVFrame * background
Definition: vf_backgroundkey.c:40
BackgroundkeyContext::threshold
float threshold
Definition: vf_backgroundkey.c:28
AVFilterContext
An instance of a filter.
Definition: avfilter.h:407
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:75
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:69
FILTER_OUTPUTS
#define FILTER_OUTPUTS(array)
Definition: internal.h:183
BackgroundkeyContext::do_slice
int(* do_slice)(AVFilterContext *avctx, void *arg, int jobnr, int nb_jobs)
Definition: vf_backgroundkey.c:42
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:34
ff_filter_execute
static av_always_inline int ff_filter_execute(AVFilterContext *ctx, avfilter_action_func *func, void *arg, int *ret, int nb_jobs)
Definition: internal.h:134
int
int
Definition: ffmpeg_filter.c:410
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:419
AVFILTERPAD_FLAG_NEEDS_WRITABLE
#define AVFILTERPAD_FLAG_NEEDS_WRITABLE
The filter expects writable frames from its input link, duplicating data buffers if needed.
Definition: internal.h:52