• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/imgutils.h"
34 #include "libavutil/opt.h"
35 #include "libavutil/qsort.h"
36 #include "internal.h"
37 #include "edge_common.h"
38 
comp(const float * a,const float * b)39 static int comp(const float *a,const float *b)
40 {
41     return FFDIFFSIGN(*a, *b);
42 }
43 
44 typedef struct BLRContext {
45     const AVClass *class;
46 
47     int hsub, vsub;
48     int nb_planes;
49 
50     float   low, high;
51     uint8_t low_u8, high_u8;
52     int     radius;        // radius during local maxima detection
53     int     block_pct;     // percentage of "sharpest" blocks in the image to use for bluriness calculation
54     int     block_width;   // width for block abbreviation
55     int     block_height;  // height for block abbreviation
56     int     planes;        // number of planes to filter
57 
58     double   blur_total;
59     uint64_t nb_frames;
60 
61     float    *blks;
62     uint8_t  *filterbuf;
63     uint8_t  *tmpbuf;
64     uint16_t *gradients;
65     int8_t   *directions;
66 } BLRContext;
67 
68 #define OFFSET(x) offsetof(BLRContext, x)
69 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
70 static const AVOption blurdetect_options[] = {
71     { "high",          "set high threshold", OFFSET(high), AV_OPT_TYPE_FLOAT, {.dbl=30/255.}, 0, 1, FLAGS },
72     { "low",           "set low threshold",  OFFSET(low),  AV_OPT_TYPE_FLOAT, {.dbl=15/255.}, 0, 1, FLAGS },
73     { "radius",        "search radius for maxima detection", OFFSET(radius), AV_OPT_TYPE_INT, {.i64=50}, 1, 100, FLAGS },
74     { "block_pct",     "block pooling threshold when calculating blurriness", OFFSET(block_pct), AV_OPT_TYPE_INT, {.i64=80}, 1, 100, FLAGS },
75     { "block_width",   "block size for block-based abbreviation of blurriness", OFFSET(block_width), AV_OPT_TYPE_INT, {.i64=-1}, -1, INT_MAX, FLAGS },
76     { "block_height",  "block size for block-based abbreviation of blurriness", OFFSET(block_height), AV_OPT_TYPE_INT, {.i64=-1}, -1, INT_MAX, FLAGS },
77     { "planes",        "set planes to filter", OFFSET(planes), AV_OPT_TYPE_INT, {.i64=1}, 0, 15, FLAGS },
78     { NULL }
79 };
80 
81 AVFILTER_DEFINE_CLASS(blurdetect);
82 
blurdetect_init(AVFilterContext * ctx)83 static av_cold int blurdetect_init(AVFilterContext *ctx)
84 {
85     BLRContext *s = ctx->priv;
86 
87     s->low_u8  = s->low  * 255. + .5;
88     s->high_u8 = s->high * 255. + .5;
89 
90     return 0;
91 }
92 
blurdetect_config_input(AVFilterLink * inlink)93 static int blurdetect_config_input(AVFilterLink *inlink)
94 {
95     AVFilterContext *ctx = inlink->dst;
96     BLRContext      *s   = ctx->priv;
97     const int bufsize    = inlink->w * inlink->h;
98     const AVPixFmtDescriptor *pix_desc;
99 
100     pix_desc = av_pix_fmt_desc_get(inlink->format);
101     s->hsub = pix_desc->log2_chroma_w;
102     s->vsub = pix_desc->log2_chroma_h;
103     s->nb_planes = av_pix_fmt_count_planes(inlink->format);
104 
105     if (s->block_width  < 1 || s->block_height < 1) {
106         s->block_width  = inlink->w;
107         s->block_height = inlink->h;
108     }
109 
110     s->tmpbuf     = av_malloc(bufsize);
111     s->filterbuf  = av_malloc(bufsize);
112     s->gradients  = av_calloc(bufsize, sizeof(*s->gradients));
113     s->directions = av_malloc(bufsize);
114     s->blks       = av_calloc((inlink->w / s->block_width) * (inlink->h / s->block_height),
115                               sizeof(*s->blks));
116 
117     if (!s->tmpbuf || !s->filterbuf || !s->gradients || !s->directions || !s->blks)
118         return AVERROR(ENOMEM);
119 
120     return 0;
121 }
122 
123 // edge width is defined as the distance between surrounding maxima of the edge pixel
edge_width(BLRContext * blr,int i,int j,int8_t dir,int w,int h,int edge,const uint8_t * src,int src_linesize)124 static float edge_width(BLRContext *blr, int i, int j, int8_t dir, int w, int h,
125                         int edge, const uint8_t *src, int src_linesize)
126 {
127     float width = 0;
128     int dX, dY;
129     int sign;
130     int tmp;
131     int p1;
132     int p2;
133     int k, x, y;
134     int radius = blr->radius;
135 
136     switch(dir) {
137     case DIRECTION_HORIZONTAL: dX = 1; dY =  0; break;
138     case DIRECTION_VERTICAL:   dX = 0; dY =  1; break;
139     case DIRECTION_45UP:       dX = 1; dY = -1; break;
140     case DIRECTION_45DOWN:     dX = 1; dY =  1; break;
141     default:                   dX = 1; dY =  1; break;
142     }
143 
144     // determines if search in direction dX/dY is looking for a maximum or minimum
145     sign = src[j * src_linesize + i] > src[(j - dY) * src_linesize + i - dX] ? 1 : -1;
146 
147     // search in -(dX/dY) direction
148     for (k = 0; k < radius; k++) {
149         x = i - k*dX;
150         y = j - k*dY;
151         p1 = y * src_linesize + x;
152         x -= dX;
153         y -= dY;
154         p2 = y * src_linesize + x;
155         if (x < 0 || x >= w || y < 0 || y >= h)
156             return 0;
157 
158         tmp = (src[p1] - src[p2]) * sign;
159 
160         if (tmp <= 0) // local maximum found
161             break;
162     }
163     width += k;
164 
165     // search in +(dX/dY) direction
166     for (k = 0; k < radius; k++) {
167         x = i + k * dX;
168         y = j + k * dY;
169         p1 = y * src_linesize + x;
170         x += dX;
171         y += dY;
172         p2 = y * src_linesize + x;
173         if (x < 0 || x >= w || y < 0 || y >= h)
174             return 0;
175 
176         tmp = (src[p1] - src[p2]) * sign;
177 
178         if (tmp >= 0) // local maximum found
179             break;
180     }
181     width += k;
182 
183     // for 45 degree directions approximate edge width in pixel units: 0.7 ~= sqrt(2)/2
184     if (dir == DIRECTION_45UP || dir == DIRECTION_45DOWN)
185         width *= 0.7;
186 
187     return width;
188 }
189 
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)190 static float calculate_blur(BLRContext *s, int w, int h, int hsub, int vsub,
191                             int8_t* dir, int dir_linesize,
192                             uint8_t* dst, int dst_linesize,
193                             uint8_t* src, int src_linesize)
194 {
195     float total_width = 0.0;
196     int block_count;
197     double block_total_width;
198 
199     int i, j;
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             block_total_width = 0.0;
213             block_count = 0;
214             for (int inj = 0; inj < block_height; inj++) {
215                 for (int ini = 0; ini < block_width; ini++) {
216                     i = blki * block_width + ini;
217                     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 
set_meta(AVDictionary ** metadata,const char * key,float d)248 static void set_meta(AVDictionary **metadata, const char *key, float d)
249 {
250     char value[128];
251     snprintf(value, sizeof(value), "%f", d);
252     av_dict_set(metadata, key, value, 0);
253 }
254 
blurdetect_filter_frame(AVFilterLink * inlink,AVFrame * in)255 static int blurdetect_filter_frame(AVFilterLink *inlink, AVFrame *in)
256 {
257     AVFilterContext *ctx  = inlink->dst;
258     BLRContext *s         = ctx->priv;
259     AVFilterLink *outlink = ctx->outputs[0];
260 
261     const int inw = inlink->w;
262     const int inh = inlink->h;
263 
264     uint8_t *tmpbuf     = s->tmpbuf;
265     uint8_t *filterbuf  = s->filterbuf;
266     uint16_t *gradients = s->gradients;
267     int8_t *directions  = s->directions;
268 
269     float blur = 0.0f;
270     int nplanes = 0;
271     AVDictionary **metadata;
272     metadata = &in->metadata;
273 
274     for (int plane = 0; plane < s->nb_planes; plane++) {
275         int hsub = plane == 1 || plane == 2 ? s->hsub : 0;
276         int vsub = plane == 1 || plane == 2 ? s->vsub : 0;
277         int w = AV_CEIL_RSHIFT(inw, hsub);
278         int h = AV_CEIL_RSHIFT(inh, vsub);
279 
280         if (!((1 << plane) & s->planes))
281             continue;
282 
283         nplanes++;
284 
285         // gaussian filter to reduce noise
286         ff_gaussian_blur(w, h,
287                          filterbuf,  w,
288                          in->data[plane], in->linesize[plane]);
289 
290         // compute the 16-bits gradients and directions for the next step
291         ff_sobel(w, h, gradients, w, directions, w, filterbuf, w);
292 
293         // non_maximum_suppression() will actually keep & clip what's necessary and
294         // ignore the rest, so we need a clean output buffer
295         memset(tmpbuf, 0, inw * inh);
296         ff_non_maximum_suppression(w, h, tmpbuf, w, directions, w, gradients, w);
297 
298 
299         // keep high values, or low values surrounded by high values
300         ff_double_threshold(s->low_u8, s->high_u8, w, h,
301                             tmpbuf, w, tmpbuf, w);
302 
303         blur += calculate_blur(s, w, h, hsub, vsub, directions, w,
304                               tmpbuf, w, filterbuf, w);
305     }
306 
307     if (nplanes)
308         blur /= nplanes;
309 
310     s->blur_total += blur;
311 
312     // write stats
313     av_log(ctx, AV_LOG_VERBOSE, "blur: %.7f\n", blur);
314 
315     set_meta(metadata, "lavfi.blur", blur);
316 
317     s->nb_frames = inlink->frame_count_in;
318 
319     return ff_filter_frame(outlink, in);
320 }
321 
blurdetect_uninit(AVFilterContext * ctx)322 static av_cold void blurdetect_uninit(AVFilterContext *ctx)
323 {
324     BLRContext *s = ctx->priv;
325 
326     if (s->nb_frames > 0) {
327         av_log(ctx, AV_LOG_INFO, "blur mean: %.7f\n",
328                s->blur_total / s->nb_frames);
329     }
330 
331     av_freep(&s->tmpbuf);
332     av_freep(&s->filterbuf);
333     av_freep(&s->gradients);
334     av_freep(&s->directions);
335     av_freep(&s->blks);
336 }
337 
338 static const enum AVPixelFormat pix_fmts[] = {
339     AV_PIX_FMT_GRAY8,
340     AV_PIX_FMT_GBRP,     AV_PIX_FMT_GBRAP,
341     AV_PIX_FMT_YUV422P,  AV_PIX_FMT_YUV420P,
342     AV_PIX_FMT_YUV444P,  AV_PIX_FMT_YUV440P,
343     AV_PIX_FMT_YUV411P,  AV_PIX_FMT_YUV410P,
344     AV_PIX_FMT_YUVJ440P, AV_PIX_FMT_YUVJ411P, AV_PIX_FMT_YUVJ420P,
345     AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ444P,
346     AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA420P,
347     AV_PIX_FMT_NONE
348 };
349 
350 static const AVFilterPad blurdetect_inputs[] = {
351     {
352         .name         = "default",
353         .type         = AVMEDIA_TYPE_VIDEO,
354         .config_props = blurdetect_config_input,
355         .filter_frame = blurdetect_filter_frame,
356     },
357 };
358 
359 static const AVFilterPad blurdetect_outputs[] = {
360     {
361         .name = "default",
362         .type = AVMEDIA_TYPE_VIDEO,
363     },
364 };
365 
366 const AVFilter ff_vf_blurdetect = {
367     .name          = "blurdetect",
368     .description   = NULL_IF_CONFIG_SMALL("Blurdetect filter."),
369     .priv_size     = sizeof(BLRContext),
370     .init          = blurdetect_init,
371     .uninit        = blurdetect_uninit,
372     FILTER_PIXFMTS_ARRAY(pix_fmts),
373     FILTER_INPUTS(blurdetect_inputs),
374     FILTER_OUTPUTS(blurdetect_outputs),
375     .priv_class    = &blurdetect_class,
376     .flags         = AVFILTER_FLAG_METADATA_ONLY,
377 };
378