FFmpeg
Loading...
Searching...
No Matches
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
26typedef struct BackgroundkeyContext {
27 const AVClass *class;
28
29 float threshold;
31 float blend;
32 int max;
33
37
40
42
43 int (*do_slice)(AVFilterContext *avctx, void *arg,
44 int jobnr, int nb_jobs);
46
47static 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 = ff_slice_pos(frame->height, jobnr, nb_jobs);
52 const int slice_end = ff_slice_pos(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
90static 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 = ff_slice_pos(frame->height, jobnr, nb_jobs);
95 const int slice_end = ff_slice_pos(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);
165fail:
167 return ret;
168}
169
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
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
241
243
245 .p.name = "backgroundkey",
246 .p.description = NULL_IF_CONFIG_SMALL("Turns a static background into transparency."),
247 .p.priv_class = &backgroundkey_class,
249 .priv_size = sizeof(BackgroundkeyContext),
250 .uninit = uninit,
254 .process_command = ff_filter_process_command,
255};
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_backgroundkey
static AVFormatContext * ctx
#define A(x)
Definition vpx_arith.h:28
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 s(width, name)
Definition cbs_vp9.c:198
#define FLAGS
Definition cmdutils.c:598
#define av_clipf
Definition common.h:145
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
Definition common.h:74
#define NULL
Definition coverity.c:32
long long int64_t
Definition coverity.c:34
#define max(a, b)
static AVFrame * frame
static int filter_frame(DBEDecodeContext *s, AVFrame *frame)
Definition dolby_e.c:1067
#define fail
Definition test.h:479
@ AV_OPT_TYPE_FLOAT
Underlying C type is float.
Definition opt.h:270
#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
#define AVERROR(e)
Definition error.h:45
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition frame.c:64
int av_frame_copy(AVFrame *dst, const AVFrame *src)
Copy the frame data from src to dst.
Definition frame.c:711
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
static av_cold void uninit(AVBitStreamFilterContext *ctx)
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
#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
void * av_calloc(size_t nmemb, size_t size)
Definition mem.c:264
Memory handling functions.
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_GBRAP12
Definition pixfmt.h:569
#define AV_PIX_FMT_GBRAP16
Definition pixfmt.h:571
#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_GBRAP
planar GBRA 4:4:4:4 32bpp
Definition pixfmt.h:212
@ 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_GBRAP10
Definition pixfmt.h:568
#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
AVFilterLink ** inputs
array of pointers to input links
Definition avfilter.h:281
void * priv
private data for use by the filter
Definition avfilter.h:288
AVFilterLink ** outputs
array of pointers to output links
Definition avfilter.h:285
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
int(* do_slice)(AVFilterContext *avctx, void *arg, int jobnr, int nb_jobs)
#define av_freep(p)
static const AVOption backgroundkey_options[]
static enum AVPixelFormat backgroundkey_fmts[]
static av_cold int config_output(AVFilterLink *outlink)
static const AVFilterPad backgroundkey_inputs[]
static int filter_frame(AVFilterLink *link, AVFrame *frame)
static av_cold void uninit(AVFilterContext *ctx)
static const AVFilterPad backgroundkey_outputs[]
static int do_backgroundkey_slice(AVFilterContext *avctx, void *arg, int jobnr, int nb_jobs)
#define OFFSET(x)
static int do_backgroundkey16_slice(AVFilterContext *avctx, void *arg, int jobnr, int nb_jobs)
static void hsub(htype *dst, const htype *src, int bins)
Definition vf_median.c:74
static av_always_inline int diff(const struct color_info *a, const struct color_info *b, const int trans_thresh)
AVFrame * ff_get_video_buffer(AVFilterLink *link, int w, int h)
Request a picture buffer with a specific set of permissions.
Definition video.c:89
static int slice_start(SliceContext *sc, VVCContext *s, VVCFrameContext *fc, const CodedBitstreamUnit *unit, const int is_first_slice)
Definition dec.c:844