History

avfilter: add varblur video filter

Paul B Mahol committed 5 years ago

Commit 407acc01 parent 66f80556 files · +421 −1Browse files

Unified · Split

6 files
  1. Changelog +1 −0
  2. doc/filters.texi +16 −0
  3. libavfilter/Makefile +1 −0
  4. libavfilter/allfilters.c +1 −0
  5. libavfilter/version.h +1 −1
  6. libavfilter/vf_varblur.c +401 −0
Changelog+1−0
@@ -28,6 +28,7 @@ version <next>: · 27 unmodified lines
2828 - speex decoder
2929 - limitdiff video filter
3030 - xcorrelate video filter
31+- varblur video filter
3132
3233
3334 version 4.4:
doc/filters.texi+16−0
@@ -21704,6 +21704,22 @@ Threshold used depends also on each decomposition coefficients. · 21703 unmodified lines
2170421704 Default is universal.
2170521705 @end table
2170621706
21707+@section varblur
21708+Apply variable blur filter by using 2nd video stream to set blur radius.
21709+
21710+This filter accepts the following options:
21711+
21712+@table @option
21713+@item min_r
21714+Set min allowed radius. By default is 0. Allowed range is from 0 to 254.
21715+@item max_r
21716+Set max allowed radius. By default is 8. Allowed range is from 1 to 255.
21717+@item planes
21718+Set which planes to process. By default all are used.
21719+@end table
21720+
21721+The @code{varblur} filter also supports the @ref{framesync} options.
21722+
2170721723 @section vectorscope
2170821724
2170921725 Display 2 color component values in the two dimensional graph (which is called
libavfilter/Makefile+1−0
@@ -485,6 +485,7 @@ OBJS-$(CONFIG_UNTILE_FILTER) += vf_untile.o · 484 unmodified lines
485485 OBJS-$(CONFIG_USPP_FILTER) += vf_uspp.o qp_table.o
486486 OBJS-$(CONFIG_V360_FILTER) += vf_v360.o
487487 OBJS-$(CONFIG_VAGUEDENOISER_FILTER) += vf_vaguedenoiser.o
488+OBJS-$(CONFIG_VARBLUR_FILTER) += vf_varblur.o framesync.o
488489 OBJS-$(CONFIG_VECTORSCOPE_FILTER) += vf_vectorscope.o
489490 OBJS-$(CONFIG_VFLIP_FILTER) += vf_vflip.o
490491 OBJS-$(CONFIG_VFRDET_FILTER) += vf_vfrdet.o
libavfilter/allfilters.c+1−0
@@ -462,6 +462,7 @@ extern const AVFilter ff_vf_untile; · 461 unmodified lines
462462 extern const AVFilter ff_vf_uspp;
463463 extern const AVFilter ff_vf_v360;
464464 extern const AVFilter ff_vf_vaguedenoiser;
465+extern const AVFilter ff_vf_varblur;
465466 extern const AVFilter ff_vf_vectorscope;
466467 extern const AVFilter ff_vf_vflip;
467468 extern const AVFilter ff_vf_vfrdet;
libavfilter/version.h+1−1
@@ -30,7 +30,7 @@ · 29 unmodified lines
3030 #include "libavutil/version.h"
3131
3232 #define LIBAVFILTER_VERSION_MAJOR 8
33−#define LIBAVFILTER_VERSION_MINOR 14
33+#define LIBAVFILTER_VERSION_MINOR 15
3434 #define LIBAVFILTER_VERSION_MICRO 100
3535
3636
libavfilter/vf_varblur.c+401−0
@@ -0,0 +1,401 @@
1+/*
2+ * Copyright (c) 2021 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/imgutils.h"
22+#include "libavutil/opt.h"
23+#include "libavutil/pixdesc.h"
24+#include "avfilter.h"
25+#include "formats.h"
26+#include "framesync.h"
27+#include "internal.h"
28+#include "video.h"
29+
30+typedef struct VarBlurContext {
31+ const AVClass *class;
32+ FFFrameSync fs;
33+
34+ int min_radius;
35+ int max_radius;
36+ int planes;
37+
38+ int depth;
39+ int planewidth[4];
40+ int planeheight[4];
41+
42+ AVFrame *sat;
43+ int nb_planes;
44+
45+ void (*compute_sat)(const uint8_t *ssrc,
46+ int linesize,
47+ int w, int h,
48+ const uint8_t *dstp,
49+ int dst_linesize);
50+
51+ int (*blur_plane)(AVFilterContext *ctx,
52+ uint8_t *ddst,
53+ int ddst_linesize,
54+ const uint8_t *rrptr,
55+ int rrptr_linesize,
56+ int w, int h,
57+ const uint8_t *pptr,
58+ int pptr_linesize,
59+ int slice_start, int slice_end);
60+} VarBlurContext;
61+
62+#define OFFSET(x) offsetof(VarBlurContext, x)
63+#define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
64+
65+static const AVOption varblur_options[] = {
66+ { "min_r", "set min blur radius", OFFSET(min_radius), AV_OPT_TYPE_INT, {.i64=0}, 0, 254, FLAGS },
67+ { "max_r", "set max blur radius", OFFSET(max_radius), AV_OPT_TYPE_INT, {.i64=8}, 1, 255, FLAGS },
68+ { "planes", "set planes to filter", OFFSET(planes), AV_OPT_TYPE_INT, {.i64=0xF}, 0, 0xF, FLAGS },
69+ { NULL }
70+};
71+
72+FRAMESYNC_DEFINE_CLASS(varblur, VarBlurContext, fs);
73+
74+static const enum AVPixelFormat pix_fmts[] = {
75+ AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV440P,
76+ AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P,
77+ AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUV420P,
78+ AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ420P,
79+ AV_PIX_FMT_YUVJ411P, AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P,
80+ AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV444P9,
81+ AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10,
82+ AV_PIX_FMT_YUV420P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV444P12, AV_PIX_FMT_YUV440P12,
83+ AV_PIX_FMT_YUV420P14, AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV444P14,
84+ AV_PIX_FMT_YUV420P16, AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV444P16,
85+ AV_PIX_FMT_YUVA420P9, AV_PIX_FMT_YUVA422P9, AV_PIX_FMT_YUVA444P9,
86+ AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA444P10,
87+ AV_PIX_FMT_YUVA422P12, AV_PIX_FMT_YUVA444P12,
88+ AV_PIX_FMT_YUVA420P16, AV_PIX_FMT_YUVA422P16, AV_PIX_FMT_YUVA444P16,
89+ AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRP9, AV_PIX_FMT_GBRP10,
90+ AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRP14, AV_PIX_FMT_GBRP16,
91+ AV_PIX_FMT_GBRAP, AV_PIX_FMT_GBRAP10, AV_PIX_FMT_GBRAP12, AV_PIX_FMT_GBRAP16,
92+ AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY9, AV_PIX_FMT_GRAY10, AV_PIX_FMT_GRAY12, AV_PIX_FMT_GRAY14, AV_PIX_FMT_GRAY16,
93+ AV_PIX_FMT_NONE
94+};
95+
96+#define COMPUTE_SAT(type, stype, depth) \
97+static void compute_sat##depth(const uint8_t *ssrc, \
98+ int linesize, \
99+ int w, int h, \
100+ const uint8_t *dstp, \
101+ int dst_linesize) \
102+{ \
103+ const type *src = (const type *)ssrc; \
104+ stype *dst = (stype *)dstp; \
105+ \
106+ linesize /= (depth / 8); \
107+ dst_linesize /= (depth / 2); \
108+ dst += dst_linesize; \
109+ \
110+ for (int y = 0; y < h; y++) { \
111+ stype sum = 0; \
112+ \
113+ for (int x = 1; x < w; x++) { \
114+ sum += src[x - 1]; \
115+ dst[x] = sum + dst[x - dst_linesize]; \
116+ } \
117+ \
118+ src += linesize; \
119+ dst += dst_linesize; \
120+ } \
121+}
122+
123+COMPUTE_SAT(uint8_t, uint32_t, 8)
124+COMPUTE_SAT(uint16_t, uint64_t, 16)
125+
126+typedef struct ThreadData {
127+ AVFrame *in, *out, *radius;
128+} ThreadData;
129+
130+static float lerpf(float v0, float v1, float f)
131+{
132+ return v0 + (v1 - v0) * f;
133+}
134+
135+#define BLUR_PLANE(type, stype, bits) \
136+static int blur_plane##bits(AVFilterContext *ctx, \
137+ uint8_t *ddst, \
138+ int ddst_linesize, \
139+ const uint8_t *rrptr, \
140+ int rrptr_linesize, \
141+ int w, int h, \
142+ const uint8_t *pptr, \
143+ int pptr_linesize, \
144+ int slice_start, int slice_end) \
145+{ \
146+ VarBlurContext *s = ctx->priv; \
147+ const int ddepth = s->depth; \
148+ const int dst_linesize = ddst_linesize / (bits / 8); \
149+ const int ptr_linesize = pptr_linesize / (bits / 2); \
150+ const int rptr_linesize = rrptr_linesize / (bits / 8); \
151+ const type *rptr = (const type *)rrptr + slice_start * rptr_linesize; \
152+ type *dst = (type *)ddst + slice_start * dst_linesize; \
153+ const stype *ptr = (stype *)pptr; \
154+ const float minr = 2.f * s->min_radius + 1.f; \
155+ const float maxr = 2.f * s->max_radius + 1.f; \
156+ const float scaler = (maxr - minr) / ((1 << ddepth) - 1); \
157+ \
158+ for (int y = slice_start; y < slice_end; y++) { \
159+ for (int x = 0; x < w; x++) { \
160+ const float radiusf = minr + (FFMAX(0.f, 2 * rptr[x] + 1 - minr)) * scaler; \
161+ const int radius = floorf(radiusf); \
162+ const float factor = radiusf - radius; \
163+ const int nradius = radius + 1; \
164+ const int l = FFMIN(radius, x); \
165+ const int r = FFMIN(radius, w - x - 1); \
166+ const int t = FFMIN(radius, y); \
167+ const int b = FFMIN(radius, h - y - 1); \
168+ const int nl = FFMIN(nradius, x); \
169+ const int nr = FFMIN(nradius, w - x - 1); \
170+ const int nt = FFMIN(nradius, y); \
171+ const int nb = FFMIN(nradius, h - y - 1); \
172+ stype tl = ptr[(y - t) * ptr_linesize + x - l]; \
173+ stype tr = ptr[(y - t) * ptr_linesize + x + r]; \
174+ stype bl = ptr[(y + b) * ptr_linesize + x - l]; \
175+ stype br = ptr[(y + b) * ptr_linesize + x + r]; \
176+ stype ntl = ptr[(y - nt) * ptr_linesize + x - nl]; \
177+ stype ntr = ptr[(y - nt) * ptr_linesize + x + nr]; \
178+ stype nbl = ptr[(y + nb) * ptr_linesize + x - nl]; \
179+ stype nbr = ptr[(y + nb) * ptr_linesize + x + nr]; \
180+ stype div = (l + r) * (t + b); \
181+ stype ndiv = (nl + nr) * (nt + nb); \
182+ stype p0 = (br + tl - bl - tr) / div; \
183+ stype n0 = (nbr + ntl - nbl - ntr) / ndiv; \
184+ \
185+ dst[x] = av_clip_uintp2_c(lrintf( \
186+ lerpf(p0, n0, factor)), \
187+ ddepth); \
188+ } \
189+ \
190+ rptr += rptr_linesize; \
191+ dst += dst_linesize; \
192+ } \
193+ \
194+ return 0; \
195+}
196+
197+BLUR_PLANE(uint8_t, uint32_t, 8)
198+BLUR_PLANE(uint16_t, uint64_t, 16)
199+
200+static int blur_planes(AVFilterContext *ctx, void *arg,
201+ int jobnr, int nb_jobs)
202+{
203+ VarBlurContext *s = ctx->priv;
204+ ThreadData *td = arg;
205+ AVFrame *radius = td->radius;
206+ AVFrame *out = td->out;
207+ AVFrame *in = td->in;
208+
209+ for (int plane = 0; plane < s->nb_planes; plane++) {
210+ const int height = s->planeheight[plane];
211+ const int slice_start = (height * jobnr) / nb_jobs;
212+ const int slice_end = (height * (jobnr+1)) / nb_jobs;
213+ const int width = s->planewidth[plane];
214+ const int linesize = in->linesize[plane];
215+ const int dst_linesize = out->linesize[plane];
216+ const uint8_t *rptr = radius->data[plane];
217+ const int rptr_linesize = radius->linesize[plane];
218+ uint8_t *ptr = s->sat->data[plane];
219+ const int ptr_linesize = s->sat->linesize[plane];
220+ const uint8_t *src = in->data[plane];
221+ uint8_t *dst = out->data[plane];
222+
223+ if (!(s->planes & (1 << plane))) {
224+ if (out != in)
225+ av_image_copy_plane(dst + slice_start * dst_linesize,
226+ dst_linesize,
227+ src + slice_start * linesize,
228+ linesize,
229+ width * ((s->depth + 7) / 8),
230+ slice_end - slice_start);
231+ continue;
232+ }
233+
234+ s->blur_plane(ctx, dst, dst_linesize,
235+ rptr, rptr_linesize,
236+ width, height,
237+ ptr, ptr_linesize,
238+ slice_start, slice_end);
239+ }
240+
241+ return 0;
242+}
243+
244+static int blur_frame(AVFilterContext *ctx, AVFrame *in, AVFrame *radius)
245+{
246+ VarBlurContext *s = ctx->priv;
247+ AVFilterLink *outlink = ctx->outputs[0];
248+ ThreadData td;
249+ AVFrame *out;
250+
251+ if (av_frame_is_writable(in)) {
252+ out = in;
253+ } else {
254+ out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
255+ if (!out) {
256+ av_frame_free(&in);
257+ return AVERROR(ENOMEM);
258+ }
259+ av_frame_copy_props(out, in);
260+ }
261+
262+ for (int plane = 0; plane < s->nb_planes; plane++) {
263+ const int height = s->planeheight[plane];
264+ const int width = s->planewidth[plane];
265+ const int linesize = in->linesize[plane];
266+ uint8_t *ptr = s->sat->data[plane];
267+ const int ptr_linesize = s->sat->linesize[plane];
268+ const uint8_t *src = in->data[plane];
269+
270+ if (!(s->planes & (1 << plane)))
271+ continue;
272+
273+ s->compute_sat(src, linesize, width, height, ptr, ptr_linesize);
274+ }
275+
276+ td.in = in;
277+ td.out = out;
278+ td.radius = radius;
279+ ff_filter_execute(ctx, blur_planes, &td, NULL,
280+ FFMIN(s->planeheight[1], ff_filter_get_nb_threads(ctx)));
281+
282+ if (out != in)
283+ av_frame_free(&in);
284+ return ff_filter_frame(outlink, out);
285+}
286+
287+static int activate(AVFilterContext *ctx)
288+{
289+ VarBlurContext *s = ctx->priv;
290+ return ff_framesync_activate(&s->fs);
291+}
292+
293+static int varblur_frame(FFFrameSync *fs)
294+{
295+ AVFilterContext *ctx = fs->parent;
296+ VarBlurContext *s = ctx->priv;
297+ AVFrame *in, *radius;
298+ int ret;
299+
300+ if (s->max_radius <= s->min_radius)
301+ s->max_radius = s->min_radius + 1;
302+
303+ ret = ff_framesync_dualinput_get(fs, &in, &radius);
304+ if (ret < 0)
305+ return ret;
306+ if (!radius)
307+ return ff_filter_frame(ctx->outputs[0], in);
308+ return blur_frame(ctx, in, radius);
309+}
310+
311+static int config_output(AVFilterLink *outlink)
312+{
313+ AVFilterContext *ctx = outlink->src;
314+ AVFilterLink *inlink = ctx->inputs[0];
315+ AVFilterLink *radiuslink = ctx->inputs[1];
316+ VarBlurContext *s = ctx->priv;
317+ const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(outlink->format);
318+ int ret;
319+
320+ if (inlink->w != radiuslink->w || inlink->h != radiuslink->h) {
321+ av_log(ctx, AV_LOG_ERROR, "First input link %s parameters "
322+ "(size %dx%d) do not match the corresponding "
323+ "second input link %s parameters (size %dx%d)\n",
324+ ctx->input_pads[0].name, inlink->w, inlink->h,
325+ ctx->input_pads[1].name, radiuslink->w, radiuslink->h);
326+ return AVERROR(EINVAL);
327+ }
328+
329+ outlink->w = inlink->w;
330+ outlink->h = inlink->h;
331+ outlink->time_base = inlink->time_base;
332+ outlink->sample_aspect_ratio = inlink->sample_aspect_ratio;
333+ outlink->frame_rate = inlink->frame_rate;
334+
335+ s->depth = desc->comp[0].depth;
336+ s->blur_plane = s->depth <= 8 ? blur_plane8 : blur_plane16;
337+ s->compute_sat = s->depth <= 8 ? compute_sat8 : compute_sat16;
338+
339+ s->planewidth[1] = s->planewidth[2] = AV_CEIL_RSHIFT(outlink->w, desc->log2_chroma_w);
340+ s->planewidth[0] = s->planewidth[3] = outlink->w;
341+ s->planeheight[1] = s->planeheight[2] = AV_CEIL_RSHIFT(outlink->h, desc->log2_chroma_h);
342+ s->planeheight[0] = s->planeheight[3] = outlink->h;
343+
344+ s->nb_planes = av_pix_fmt_count_planes(outlink->format);
345+
346+ s->sat = ff_get_video_buffer(outlink, (outlink->w + 1) * 4 * ((s->depth + 7) / 8), outlink->h + 1);
347+ if (!s->sat)
348+ return AVERROR(ENOMEM);
349+
350+ s->fs.on_event = varblur_frame;
351+ if ((ret = ff_framesync_init_dualinput(&s->fs, ctx)) < 0)
352+ return ret;
353+
354+ ret = ff_framesync_configure(&s->fs);
355+ outlink->time_base = s->fs.time_base;
356+
357+ return ret;
358+}
359+
360+static av_cold void uninit(AVFilterContext *ctx)
361+{
362+ VarBlurContext *s = ctx->priv;
363+
364+ ff_framesync_uninit(&s->fs);
365+ av_frame_free(&s->sat);
366+}
367+
368+static const AVFilterPad varblur_inputs[] = {
369+ {
370+ .name = "default",
371+ .type = AVMEDIA_TYPE_VIDEO,
372+ },
373+ {
374+ .name = "radius",
375+ .type = AVMEDIA_TYPE_VIDEO,
376+ },
377+};
378+
379+static const AVFilterPad varblur_outputs[] = {
380+ {
381+ .name = "default",
382+ .type = AVMEDIA_TYPE_VIDEO,
383+ .config_props = config_output,
384+ },
385+};
386+
387+const AVFilter ff_vf_varblur = {
388+ .name = "varblur",
389+ .description = NULL_IF_CONFIG_SMALL("Apply Variable Blur filter."),
390+ .priv_size = sizeof(VarBlurContext),
391+ .priv_class = &varblur_class,
392+ .activate = activate,
393+ .preinit = varblur_framesync_preinit,
394+ .uninit = uninit,
395+ FILTER_INPUTS(varblur_inputs),
396+ FILTER_OUTPUTS(varblur_outputs),
397+ FILTER_PIXFMTS_ARRAY(pix_fmts),
398+ .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL |
399+ AVFILTER_FLAG_SLICE_THREADS,
400+ .process_command = ff_filter_process_command,
401+};

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t