FFmpeg
Loading...
Searching...
No Matches
vf_blurdetect.c
Go to the documentation of this file.
1/*
2 * Copyright (c) 2021 Thilo Borgmann <thilo.borgmann _at_ mail.de>
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/**
22 * @file
23 * No-reference blurdetect filter
24 *
25 * Implementing:
26 * Marziliano, Pina, et al. "A no-reference perceptual blur metric." Proceedings.
27 * International conference on image processing. Vol. 3. IEEE, 2002.
28 * https://infoscience.epfl.ch/record/111802/files/14%20A%20no-reference%20perceptual%20blur%20metric.pdf
29 *
30 * @author Thilo Borgmann <thilo.borgmann _at_ mail.de>
31 */
32
33#include "libavutil/mem.h"
34#include "libavutil/opt.h"
35#include "libavutil/pixdesc.h"
36#include "libavutil/qsort.h"
37
38#include "filters.h"
39#include "edge_common.h"
40#include "video.h"
41
42static int comp(const float *a,const float *b)
43{
44 return FFDIFFSIGN(*a, *b);
45}
46
47typedef struct BLRContext {
48 const AVClass *class;
49
50 int hsub, vsub;
52
53 float low, high;
54 uint8_t low_u8, high_u8;
55 int radius; // radius during local maxima detection
56 int block_pct; // percentage of "sharpest" blocks in the image to use for bluriness calculation
57 int block_width; // width for block abbreviation
58 int block_height; // height for block abbreviation
59 int planes; // number of planes to filter
60
61 double blur_total;
62 uint64_t nb_frames;
63
64 float *blks;
65 uint8_t *filterbuf;
66 uint8_t *tmpbuf;
67 uint16_t *gradients;
68 int8_t *directions;
70
71#define OFFSET(x) offsetof(BLRContext, x)
72#define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
73static const AVOption blurdetect_options[] = {
74 { "high", "set high threshold", OFFSET(high), AV_OPT_TYPE_FLOAT, {.dbl=30/255.}, 0, 1, FLAGS },
75 { "low", "set low threshold", OFFSET(low), AV_OPT_TYPE_FLOAT, {.dbl=15/255.}, 0, 1, FLAGS },
76 { "radius", "search radius for maxima detection", OFFSET(radius), AV_OPT_TYPE_INT, {.i64=50}, 1, 100, FLAGS },
77 { "block_pct", "block pooling threshold when calculating blurriness", OFFSET(block_pct), AV_OPT_TYPE_INT, {.i64=80}, 1, 100, FLAGS },
78 { "block_width", "block size for block-based abbreviation of blurriness", OFFSET(block_width), AV_OPT_TYPE_INT, {.i64=-1}, -1, INT_MAX, FLAGS },
79 { "block_height", "block size for block-based abbreviation of blurriness", OFFSET(block_height), AV_OPT_TYPE_INT, {.i64=-1}, -1, INT_MAX, FLAGS },
80 { "planes", "set planes to filter", OFFSET(planes), AV_OPT_TYPE_INT, {.i64=1}, 0, 15, FLAGS },
81 { NULL }
82};
83
85
87{
88 BLRContext *s = ctx->priv;
89
90 s->low_u8 = s->low * 255. + .5;
91 s->high_u8 = s->high * 255. + .5;
92
93 return 0;
94}
95
97{
98 AVFilterContext *ctx = inlink->dst;
99 BLRContext *s = ctx->priv;
100 const int bufsize = inlink->w * inlink->h;
101 const AVPixFmtDescriptor *pix_desc;
102
103 pix_desc = av_pix_fmt_desc_get(inlink->format);
104 s->hsub = pix_desc->log2_chroma_w;
105 s->vsub = pix_desc->log2_chroma_h;
106 s->nb_planes = av_pix_fmt_count_planes(inlink->format);
107
108 if (s->block_width < 1 || s->block_height < 1) {
109 s->block_width = inlink->w;
110 s->block_height = inlink->h;
111 }
112
113 s->tmpbuf = av_malloc(bufsize);
114 s->filterbuf = av_malloc(bufsize);
115 s->gradients = av_calloc(bufsize, sizeof(*s->gradients));
116 s->directions = av_malloc(bufsize);
117 s->blks = av_calloc((inlink->w / s->block_width) * (inlink->h / s->block_height),
118 sizeof(*s->blks));
119
120 if (!s->tmpbuf || !s->filterbuf || !s->gradients || !s->directions || !s->blks)
121 return AVERROR(ENOMEM);
122
123 return 0;
124}
125
126// edge width is defined as the distance between surrounding maxima of the edge pixel
127static float edge_width(BLRContext *blr, int i, int j, int8_t dir, int w, int h,
128 int edge, const uint8_t *src, int src_linesize)
129{
130 float width = 0;
131 int dX, dY;
132 int sign;
133 int tmp;
134 int p1;
135 int p2;
136 int k, x, y;
137 int radius = blr->radius;
138
139 switch(dir) {
140 case DIRECTION_HORIZONTAL: dX = 1; dY = 0; break;
141 case DIRECTION_VERTICAL: dX = 0; dY = 1; break;
142 case DIRECTION_45UP: dX = 1; dY = -1; break;
143 case DIRECTION_45DOWN: dX = 1; dY = 1; break;
144 default: dX = 1; dY = 1; break;
145 }
146
147 // determines if search in direction dX/dY is looking for a maximum or minimum
148 sign = src[j * src_linesize + i] > src[(j - dY) * src_linesize + i - dX] ? 1 : -1;
149
150 // search in -(dX/dY) direction
151 for (k = 0; k < radius; k++) {
152 x = i - k*dX;
153 y = j - k*dY;
154 p1 = y * src_linesize + x;
155 x -= dX;
156 y -= dY;
157 p2 = y * src_linesize + x;
158 if (x < 0 || x >= w || y < 0 || y >= h)
159 return 0;
160
161 tmp = (src[p1] - src[p2]) * sign;
162
163 if (tmp <= 0) // local maximum found
164 break;
165 }
166 width += k;
167
168 // search in +(dX/dY) direction
169 for (k = 0; k < radius; k++) {
170 x = i + k * dX;
171 y = j + k * dY;
172 p1 = y * src_linesize + x;
173 x += dX;
174 y += dY;
175 p2 = y * src_linesize + x;
176 if (x < 0 || x >= w || y < 0 || y >= h)
177 return 0;
178
179 tmp = (src[p1] - src[p2]) * sign;
180
181 if (tmp >= 0) // local maximum found
182 break;
183 }
184 width += k;
185
186 // for 45 degree directions approximate edge width in pixel units: 0.7 ~= sqrt(2)/2
187 if (dir == DIRECTION_45UP || dir == DIRECTION_45DOWN)
188 width *= 0.7;
189
190 return width;
191}
192
193static float calculate_blur(BLRContext *s, int w, int h, int hsub, int vsub,
194 int8_t* dir, int dir_linesize,
195 uint8_t* dst, int dst_linesize,
196 uint8_t* src, int src_linesize)
197{
198 float total_width = 0.0;
199
200 int blkcnt = 0;
201
202 float *blks = s->blks;
203 float block_pool_threshold = s->block_pct / 100.0;
204
205 int block_width = AV_CEIL_RSHIFT(s->block_width, hsub);
206 int block_height = AV_CEIL_RSHIFT(s->block_height, vsub);
207 int brows = h / block_height;
208 int bcols = w / block_width;
209
210 for (int blkj = 0; blkj < brows; blkj++) {
211 for (int blki = 0; blki < bcols; blki++) {
212 double block_total_width = 0.0;
213 int block_count = 0;
214 for (int inj = 0; inj < block_height; inj++) {
215 for (int ini = 0; ini < block_width; ini++) {
216 int i = blki * block_width + ini;
217 int j = blkj * block_height + inj;
218
219 if (dst[j * dst_linesize + i] > 0) {
220 float width = edge_width(s, i, j, dir[j*dir_linesize+i],
221 w, h, dst[j*dst_linesize+i],
222 src, src_linesize);
223 if (width > 0.001) { // throw away zeros
224 block_count++;
225 block_total_width += width;
226 }
227 }
228 }
229 }
230 // if not enough edge pixels in a block, consider it smooth
231 if (block_total_width >= 2 && block_count) {
232 blks[blkcnt] = block_total_width / block_count;
233 blkcnt++;
234 }
235 }
236 }
237
238 // simple block pooling by sorting and keeping the sharper blocks
239 AV_QSORT(blks, blkcnt, float, comp);
240 blkcnt = ceil(blkcnt * block_pool_threshold);
241 for (int i = 0; i < blkcnt; i++) {
242 total_width += blks[i];
243 }
244
245 return total_width / blkcnt;
246}
247
248static void set_meta(AVDictionary **metadata, const char *key, float d)
249{
250 char value[128];
251 snprintf(value, sizeof(value), "%f", d);
253}
254
256{
257 FilterLink *inl = ff_filter_link(inlink);
258 AVFilterContext *ctx = inlink->dst;
259 BLRContext *s = ctx->priv;
260 AVFilterLink *outlink = ctx->outputs[0];
261
262 const int inw = inlink->w;
263 const int inh = inlink->h;
264
265 uint8_t *tmpbuf = s->tmpbuf;
266 uint8_t *filterbuf = s->filterbuf;
267 uint16_t *gradients = s->gradients;
268 int8_t *directions = s->directions;
269
270 float blur = 0.0f;
271 int nplanes = 0;
273 metadata = &in->metadata;
274
275 for (int plane = 0; plane < s->nb_planes; plane++) {
276 int hsub = plane == 1 || plane == 2 ? s->hsub : 0;
277 int vsub = plane == 1 || plane == 2 ? s->vsub : 0;
278 int w = AV_CEIL_RSHIFT(inw, hsub);
279 int h = AV_CEIL_RSHIFT(inh, vsub);
280
281 if (!((1 << plane) & s->planes))
282 continue;
283
284 nplanes++;
285
286 // gaussian filter to reduce noise
287 ff_gaussian_blur_8(w, h,
288 filterbuf, w,
289 in->data[plane], in->linesize[plane], 1);
290
291 // compute the 16-bits gradients and directions for the next step
292 ff_sobel_8(w, h, gradients, w, directions, w, filterbuf, w, 1);
293
294 // non_maximum_suppression() will actually keep & clip what's necessary and
295 // ignore the rest, so we need a clean output buffer
296 memset(tmpbuf, 0, inw * inh);
297 ff_non_maximum_suppression(w, h, tmpbuf, w, directions, w, gradients, w);
298
299
300 // keep high values, or low values surrounded by high values
301 ff_double_threshold(s->low_u8, s->high_u8, w, h,
302 tmpbuf, w, tmpbuf, w);
303
304 blur += calculate_blur(s, w, h, hsub, vsub, directions, w,
305 tmpbuf, w, filterbuf, w);
306 }
307
308 if (nplanes)
309 blur /= nplanes;
310
311 s->blur_total += blur;
312
313 // write stats
314 av_log(ctx, AV_LOG_VERBOSE, "blur: %.7f\n", blur);
315
316 set_meta(metadata, "lavfi.blur", blur);
317
318 s->nb_frames = inl->frame_count_in;
319
320 return ff_filter_frame(outlink, in);
321}
322
324{
325 BLRContext *s = ctx->priv;
326
327 if (s->nb_frames > 0) {
328 av_log(ctx, AV_LOG_INFO, "blur mean: %.7f\n",
329 s->blur_total / s->nb_frames);
330 }
331
332 av_freep(&s->tmpbuf);
333 av_freep(&s->filterbuf);
334 av_freep(&s->gradients);
335 av_freep(&s->directions);
336 av_freep(&s->blks);
337}
338
350
352 {
353 .name = "default",
354 .type = AVMEDIA_TYPE_VIDEO,
355 .config_props = blurdetect_config_input,
356 .filter_frame = blurdetect_filter_frame,
357 },
358};
359
361 .p.name = "blurdetect",
362 .p.description = NULL_IF_CONFIG_SMALL("Blurdetect filter."),
363 .p.priv_class = &blurdetect_class,
365 .priv_size = sizeof(BLRContext),
371};
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_blurdetect
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition avfilter.c:1068
static int FUNC metadata(CodedBitstreamContext *ctx, RWContext *rw, APVRawMetadata *current)
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define s(width, name)
Definition cbs_vp9.c:198
#define FLAGS
Definition cmdutils.c:598
#define AV_CEIL_RSHIFT(a, b)
Definition common.h:60
#define NULL
Definition coverity.c:32
static __device__ float ceil(float a)
int high
Definition dovi_rpuenc.c:39
int(* init)(AVBSFContext *ctx)
Definition dts2pts.c:608
static void comp(unsigned char *dst, ptrdiff_t dst_stride, unsigned char *src, ptrdiff_t src_stride, int add)
Definition eamad.c:79
void ff_non_maximum_suppression(int w, int h, uint8_t *dst, int dst_linesize, const int8_t *dir, int dir_linesize, const uint16_t *src, int src_linesize)
Filters rounded gradients to drop all non-maxima pixels in the magnitude image Expects gradients gene...
Definition edge_common.c:60
void ff_double_threshold(int low, int high, int w, int h, uint8_t *dst, int dst_linesize, const uint8_t *src, int src_linesize)
Filters all pixels in src to keep all pixels > high, and keep all pixels > low where all surrounding ...
Definition edge_common.c:89
common functions for edge detection
@ DIRECTION_45DOWN
Definition edge_common.h:34
@ DIRECTION_45UP
Definition edge_common.h:33
@ DIRECTION_HORIZONTAL
Definition edge_common.h:35
@ DIRECTION_VERTICAL
Definition edge_common.h:36
double value
Definition eval.c:102
const char * key
@ AV_OPT_TYPE_INT
Underlying C type is int.
Definition opt.h:258
@ AV_OPT_TYPE_FLOAT
Underlying C type is float.
Definition opt.h:270
#define AVFILTER_FLAG_METADATA_ONLY
The filter is a "metadata" filter - it does not modify the frame data in any way.
Definition avfilter.h:182
int av_dict_set(AVDictionary **pm, const char *key, const char *value, int flags)
Set the given entry in *pm, overwriting an existing entry.
Definition dict.c:86
#define AVERROR(e)
Definition error.h:45
#define AV_LOG_VERBOSE
Detailed information.
Definition log.h:226
#define AV_LOG_INFO
Standard information.
Definition log.h:221
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
int a
#define b
Definition input.c:43
static av_cold void uninit(AVBitStreamFilterContext *ctx)
#define FILTER_INPUTS(array)
Definition filters.h:264
#define FILTER_OUTPUTS(array)
Definition filters.h:265
#define FILTER_PIXFMTS_ARRAY(array)
Definition filters.h:244
static FilterLink * ff_filter_link(AVFilterLink *link)
Definition filters.h:199
#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
static enum AVPixelFormat pix_fmts[]
Definition libkvazaar.c:296
static const struct @257111027162314367033347246032313251342043035002 planes[]
uint8_t w
Definition llvidencdsp.c:39
#define FFDIFFSIGN(x, y)
Comparator.
Definition macros.h:45
void * av_calloc(size_t nmemb, size_t size)
Definition mem.c:264
Memory handling functions.
#define av_malloc(s)
Definition ops_static.c:52
AVOptions.
int av_pix_fmt_count_planes(enum AVPixelFormat pix_fmt)
Definition pixdesc.c:3500
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition pixdesc.c:3460
AVPixelFormat
Pixel format.
Definition pixfmt.h:71
@ AV_PIX_FMT_NONE
Definition pixfmt.h:72
@ AV_PIX_FMT_YUV420P
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition pixfmt.h:73
@ AV_PIX_FMT_YUV440P
planar YUV 4:4:0 (1 Cr & Cb sample per 1x2 Y samples)
Definition pixfmt.h:106
@ AV_PIX_FMT_YUV422P
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition pixfmt.h:77
@ AV_PIX_FMT_GRAY8
Y , 8bpp.
Definition pixfmt.h:81
@ 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_YUVJ440P
planar YUV 4:4:0 full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV440P and setting color_range
Definition pixfmt.h:107
@ AV_PIX_FMT_YUV410P
planar YUV 4:1:0, 9bpp, (1 Cr & Cb sample per 4x4 Y samples)
Definition pixfmt.h:79
@ AV_PIX_FMT_YUV411P
planar YUV 4:1:1, 12bpp, (1 Cr & Cb sample per 4x1 Y samples)
Definition pixfmt.h:80
@ AV_PIX_FMT_YUV444P
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition pixfmt.h:78
@ 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_YUVJ411P
planar YUV 4:1:1, 12bpp, (1 Cr & Cb sample per 4x1 Y samples) full scale (JPEG), deprecated in favor ...
Definition pixfmt.h:283
@ AV_PIX_FMT_GBRAP
planar GBRA 4:4:4:4 32bpp
Definition pixfmt.h:212
@ AV_PIX_FMT_YUVJ422P
planar YUV 4:2:2, 16bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV422P and setting col...
Definition pixfmt.h:86
@ AV_PIX_FMT_YUVA422P
planar YUV 4:2:2 24bpp, (1 Cr & Cb sample per 2x1 Y & A samples)
Definition pixfmt.h:173
@ AV_PIX_FMT_GBRP
planar GBR 4:4:4 24bpp
Definition pixfmt.h:165
@ AV_PIX_FMT_YUVJ444P
planar YUV 4:4:4, 24bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV444P and setting col...
Definition pixfmt.h:87
@ AV_PIX_FMT_YUVJ420P
planar YUV 4:2:0, 12bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV420P and setting col...
Definition pixfmt.h:85
#define AV_QSORT(p, num, type, cmp)
Quicksort This sort is fast, and fully inplace but not stable and it is possible to construct input t...
Definition qsort.h:33
#define snprintf
Definition snprintf.h:34
Describe the class of an AVClass context structure.
Definition log.h:76
An instance of a filter.
Definition avfilter.h:273
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
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition frame.h:493
AVDictionary * metadata
metadata.
Definition frame.h:750
int linesize[AV_NUM_DATA_POINTERS]
For video, a positive or negative value, which is typically indicating the size in bytes of each pict...
Definition frame.h:517
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
uint8_t log2_chroma_w
Amount to shift the luma width right to find the chroma width.
Definition pixdesc.h:80
uint8_t log2_chroma_h
Amount to shift the luma height right to find the chroma height.
Definition pixdesc.h:89
uint8_t low_u8
double blur_total
uint16_t * gradients
uint8_t * tmpbuf
int8_t * directions
uint8_t * filterbuf
uint64_t nb_frames
uint8_t high_u8
float * blks
#define av_freep(p)
#define av_log(a,...)
static uint8_t tmp[40]
Definition aes_ctr.c:52
#define src
Definition vp8dsp.c:248
static AVFormatContext * ctx
Definition movenc.c:49
#define width
Definition dsp.h:89
static av_cold int blurdetect_init(AVFilterContext *ctx)
static int blurdetect_config_input(AVFilterLink *inlink)
static const AVOption blurdetect_options[]
static int blurdetect_filter_frame(AVFilterLink *inlink, AVFrame *in)
static void set_meta(AVDictionary **metadata, const char *key, float d)
static float calculate_blur(BLRContext *s, int w, int h, int hsub, int vsub, int8_t *dir, int dir_linesize, uint8_t *dst, int dst_linesize, uint8_t *src, int src_linesize)
static const AVFilterPad blurdetect_inputs[]
#define OFFSET(x)
static av_cold void blurdetect_uninit(AVFilterContext *ctx)
static float edge_width(BLRContext *blr, int i, int j, int8_t dir, int w, int h, int edge, const uint8_t *src, int src_linesize)
static int comp(const float *a, const float *b)
static void blur(uint8_t *dst, int dst_step, const uint8_t *src, int src_step, int len, int radius, int pixsize)
Definition vf_boxblur.c:164
static void hsub(htype *dst, const htype *src, int bins)
Definition vf_median.c:74
const AVFilterPad ff_video_default_filterpad[1]
An AVFilterPad array whose only entry has name "default" and is of type AVMEDIA_TYPE_VIDEO.
Definition video.c:37