FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/vf_blockdetect.c
Date: 2022-12-09 07:38:14
Exec Total Coverage
Lines: 112 113 99.1%
Functions: 5 5 100.0%
Branches: 59 66 89.4%

Line Branch Exec Source
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 blockdetect filter
24 *
25 * Implementing:
26 * Remco Muijs and Ihor Kirenko: "A no-reference blocking artifact measure for adaptive video processing." 2005 13th European signal processing conference. IEEE, 2005.
27 * http://www.eurasip.org/Proceedings/Eusipco/Eusipco2005/defevent/papers/cr1042.pdf
28 *
29 * @author Thilo Borgmann <thilo.borgmann _at_ mail.de>
30 */
31
32 #include "libavutil/imgutils.h"
33 #include "libavutil/opt.h"
34 #include "internal.h"
35
36 typedef struct BLKContext {
37 const AVClass *class;
38
39 int hsub, vsub;
40 int nb_planes;
41
42 int period_min; // minimum period to search for
43 int period_max; // maximum period to search for
44 int planes; // number of planes to filter
45
46 double block_total;
47 uint64_t nb_frames;
48
49 float *gradients;
50 } BLKContext;
51
52 #define OFFSET(x) offsetof(BLKContext, x)
53 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
54 static const AVOption blockdetect_options[] = {
55 { "period_min", "Minimum period to search for", OFFSET(period_min), AV_OPT_TYPE_INT, {.i64=3}, 2, 32, FLAGS},
56 { "period_max", "Maximum period to search for", OFFSET(period_max), AV_OPT_TYPE_INT, {.i64=24}, 2, 64, FLAGS},
57 { "planes", "set planes to filter", OFFSET(planes), AV_OPT_TYPE_INT, {.i64=1}, 0, 15, FLAGS },
58 { NULL }
59 };
60
61 AVFILTER_DEFINE_CLASS(blockdetect);
62
63 1 static int blockdetect_config_input(AVFilterLink *inlink)
64 {
65 1 AVFilterContext *ctx = inlink->dst;
66 1 BLKContext *s = ctx->priv;
67 1 const int bufsize = inlink->w * inlink->h;
68 const AVPixFmtDescriptor *pix_desc;
69
70 1 pix_desc = av_pix_fmt_desc_get(inlink->format);
71 1 s->hsub = pix_desc->log2_chroma_w;
72 1 s->vsub = pix_desc->log2_chroma_h;
73 1 s->nb_planes = av_pix_fmt_count_planes(inlink->format);
74
75 1 s->gradients = av_calloc(bufsize, sizeof(*s->gradients));
76
77
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!s->gradients)
78 return AVERROR(ENOMEM);
79
80 1 return 0;
81 }
82
83 5 static float calculate_blockiness(BLKContext *s, int w, int h,
84 float *grad, int grad_linesize,
85 uint8_t* src, int src_linesize)
86 {
87 5 float block = 0.0f;
88 5 float nonblock = 0.0f;
89 5 int block_count = 0;
90 5 int nonblock_count = 0;
91 5 float ret = 0;
92
93 // Calculate BS in horizontal and vertical directions according to (1)(2)(3).
94 // Also try to find integer pixel periods (grids) even for scaled images.
95 // In case of fractional periods, FFMAX of current and neighbor pixels
96 // can help improve the correlation with MQS.
97 // Skip linear correction term (4)(5), as it appears only valid for their own test samples.
98
99 // horizontal blockiness (fixed width)
100
2/2
✓ Branch 0 taken 995 times.
✓ Branch 1 taken 5 times.
1000 for (int j = 1; j < h; j++) {
101
2/2
✓ Branch 0 taken 291535 times.
✓ Branch 1 taken 995 times.
292530 for (int i = 3; i < w - 4; i++) {
102 291535 float temp = 0.0f;
103 291535 grad[j * grad_linesize + i] =
104 291535 abs(src[j * src_linesize + i + 0] - src[j * src_linesize + i + 1]);
105 291535 temp += abs(src[j * src_linesize + i + 1] - src[j * src_linesize + i + 2]);
106 291535 temp += abs(src[j * src_linesize + i + 2] - src[j * src_linesize + i + 3]);
107 291535 temp += abs(src[j * src_linesize + i + 3] - src[j * src_linesize + i + 4]);
108 291535 temp += abs(src[j * src_linesize + i - 0] - src[j * src_linesize + i - 1]);
109 291535 temp += abs(src[j * src_linesize + i - 1] - src[j * src_linesize + i - 2]);
110 291535 temp += abs(src[j * src_linesize + i - 2] - src[j * src_linesize + i - 3]);
111
2/2
✓ Branch 0 taken 220354 times.
✓ Branch 1 taken 71181 times.
291535 temp = FFMAX(1, temp);
112 291535 grad[j * grad_linesize + i] /= temp;
113
114 // use first row to store acculated results
115 291535 grad[i] += grad[j * grad_linesize + i];
116 }
117 }
118
119 // find horizontal period
120
2/2
✓ Branch 0 taken 110 times.
✓ Branch 1 taken 5 times.
115 for (int period = s->period_min; period < s->period_max + 1; period++) {
121 float temp;
122 110 block = 0;
123 110 nonblock = 0;
124 110 block_count = 0;
125 110 nonblock_count = 0;
126
2/2
✓ Branch 0 taken 32230 times.
✓ Branch 1 taken 110 times.
32340 for (int i = 3; i < w - 4; i++) {
127
2/2
✓ Branch 0 taken 3310 times.
✓ Branch 1 taken 28920 times.
32230 if ((i % period) == (period - 1)) {
128
6/6
✓ Branch 0 taken 2395 times.
✓ Branch 1 taken 915 times.
✓ Branch 2 taken 2751 times.
✓ Branch 3 taken 559 times.
✓ Branch 4 taken 2385 times.
✓ Branch 5 taken 366 times.
3310 block += FFMAX(FFMAX(grad[i + 0], grad[i + 1]), grad[i - 1]);
129 3310 block_count++;
130 } else {
131 28920 nonblock += grad[i];
132 28920 nonblock_count++;
133 }
134 }
135
2/4
✓ Branch 0 taken 110 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 110 times.
✗ Branch 3 not taken.
110 if (block_count && nonblock_count) {
136 110 temp = (block / block_count) / (nonblock / nonblock_count);
137
2/2
✓ Branch 0 taken 95 times.
✓ Branch 1 taken 15 times.
110 ret = FFMAX(ret, temp);
138 }
139 }
140
141 // vertical blockiness (fixed height)
142 5 block_count = 0;
143
2/2
✓ Branch 0 taken 965 times.
✓ Branch 1 taken 5 times.
970 for (int j = 3; j < h - 4; j++) {
144
2/2
✓ Branch 0 taken 288535 times.
✓ Branch 1 taken 965 times.
289500 for (int i = 1; i < w; i++) {
145 288535 float temp = 0.0f;
146 288535 grad[j * grad_linesize + i] =
147 288535 abs(src[(j + 0) * src_linesize + i] - src[(j + 1) * src_linesize + i]);
148 288535 temp += abs(src[(j + 1) * src_linesize + i] - src[(j + 2) * src_linesize + i]);
149 288535 temp += abs(src[(j + 2) * src_linesize + i] - src[(j + 3) * src_linesize + i]);
150 288535 temp += abs(src[(j + 3) * src_linesize + i] - src[(j + 4) * src_linesize + i]);
151 288535 temp += abs(src[(j - 0) * src_linesize + i] - src[(j - 1) * src_linesize + i]);
152 288535 temp += abs(src[(j - 1) * src_linesize + i] - src[(j - 2) * src_linesize + i]);
153 288535 temp += abs(src[(j - 2) * src_linesize + i] - src[(j - 3) * src_linesize + i]);
154
2/2
✓ Branch 0 taken 246901 times.
✓ Branch 1 taken 41634 times.
288535 temp = FFMAX(1, temp);
155 288535 grad[j * grad_linesize + i] /= temp;
156
157 // use first column to store accumulated results
158 288535 grad[j * grad_linesize] += grad[j * grad_linesize + i];
159 }
160 }
161
162 // find vertical period
163
2/2
✓ Branch 0 taken 110 times.
✓ Branch 1 taken 5 times.
115 for (int period = s->period_min; period < s->period_max + 1; period++) {
164 float temp;
165 110 block = 0;
166 110 nonblock = 0;
167 110 block_count = 0;
168 110 nonblock_count = 0;
169
2/2
✓ Branch 0 taken 21230 times.
✓ Branch 1 taken 110 times.
21340 for (int j = 3; j < h - 4; j++) {
170
2/2
✓ Branch 0 taken 2180 times.
✓ Branch 1 taken 19050 times.
21230 if ((j % period) == (period - 1)) {
171
6/6
✓ Branch 0 taken 1496 times.
✓ Branch 1 taken 684 times.
✓ Branch 2 taken 1758 times.
✓ Branch 3 taken 422 times.
✓ Branch 4 taken 1441 times.
✓ Branch 5 taken 317 times.
2180 block += FFMAX(FFMAX(grad[(j + 0) * grad_linesize],
172 grad[(j + 1) * grad_linesize]),
173 grad[(j - 1) * grad_linesize]);
174 2180 block_count++;
175 } else {
176 19050 nonblock += grad[j * grad_linesize];
177 19050 nonblock_count++;
178 }
179 }
180
2/4
✓ Branch 0 taken 110 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 110 times.
✗ Branch 3 not taken.
110 if (block_count && nonblock_count) {
181 110 temp = (block / block_count) / (nonblock / nonblock_count);
182
1/2
✓ Branch 0 taken 110 times.
✗ Branch 1 not taken.
110 ret = FFMAX(ret, temp);
183 }
184 }
185
186 // return highest value of horz||vert
187 5 return ret;
188 }
189
190 5 static void set_meta(AVDictionary **metadata, const char *key, float d)
191 {
192 char value[128];
193 5 snprintf(value, sizeof(value), "%f", d);
194 5 av_dict_set(metadata, key, value, 0);
195 5 }
196
197 5 static int blockdetect_filter_frame(AVFilterLink *inlink, AVFrame *in)
198 {
199 5 AVFilterContext *ctx = inlink->dst;
200 5 BLKContext *s = ctx->priv;
201 5 AVFilterLink *outlink = ctx->outputs[0];
202
203 5 const int inw = inlink->w;
204 5 const int inh = inlink->h;
205
206 5 float *gradients = s->gradients;
207
208 5 float block = 0.0f;
209 5 int nplanes = 0;
210 AVDictionary **metadata;
211 5 metadata = &in->metadata;
212
213
2/2
✓ Branch 0 taken 15 times.
✓ Branch 1 taken 5 times.
20 for (int plane = 0; plane < s->nb_planes; plane++) {
214
4/4
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 5 times.
✓ Branch 2 taken 5 times.
✓ Branch 3 taken 5 times.
15 int hsub = plane == 1 || plane == 2 ? s->hsub : 0;
215
4/4
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 5 times.
✓ Branch 2 taken 5 times.
✓ Branch 3 taken 5 times.
15 int vsub = plane == 1 || plane == 2 ? s->vsub : 0;
216 15 int w = AV_CEIL_RSHIFT(inw, hsub);
217 15 int h = AV_CEIL_RSHIFT(inh, vsub);
218
219
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 5 times.
15 if (!((1 << plane) & s->planes))
220 10 continue;
221
222 5 nplanes++;
223
224 5 block += calculate_blockiness(s, w, h, gradients, w, in->data[plane], in->linesize[plane]);
225 }
226
227
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 if (nplanes)
228 5 block /= nplanes;
229
230 5 s->block_total += block;
231
232 // write stats
233 5 av_log(ctx, AV_LOG_VERBOSE, "block: %.7f\n", block);
234
235 5 set_meta(metadata, "lavfi.block", block);
236
237 5 s->nb_frames = inlink->frame_count_in;
238
239 5 return ff_filter_frame(outlink, in);
240 }
241
242 2 static av_cold void blockdetect_uninit(AVFilterContext *ctx)
243 {
244 2 BLKContext *s = ctx->priv;
245
246
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 if (s->nb_frames > 0) {
247 1 av_log(ctx, AV_LOG_INFO, "block mean: %.7f\n",
248 1 s->block_total / s->nb_frames);
249 }
250
251 2 av_freep(&s->gradients);
252 2 }
253
254 static const enum AVPixelFormat pix_fmts[] = {
255 AV_PIX_FMT_GRAY8,
256 AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRAP,
257 AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV420P,
258 AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV440P,
259 AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P,
260 AV_PIX_FMT_YUVJ440P, AV_PIX_FMT_YUVJ411P, AV_PIX_FMT_YUVJ420P,
261 AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ444P,
262 AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA420P,
263 AV_PIX_FMT_NONE
264 };
265
266 static const AVFilterPad blockdetect_inputs[] = {
267 {
268 .name = "default",
269 .type = AVMEDIA_TYPE_VIDEO,
270 .config_props = blockdetect_config_input,
271 .filter_frame = blockdetect_filter_frame,
272 },
273 };
274
275 static const AVFilterPad blockdetect_outputs[] = {
276 {
277 .name = "default",
278 .type = AVMEDIA_TYPE_VIDEO,
279 },
280 };
281
282 const AVFilter ff_vf_blockdetect = {
283 .name = "blockdetect",
284 .description = NULL_IF_CONFIG_SMALL("Blockdetect filter."),
285 .priv_size = sizeof(BLKContext),
286 .uninit = blockdetect_uninit,
287 FILTER_PIXFMTS_ARRAY(pix_fmts),
288 FILTER_INPUTS(blockdetect_inputs),
289 FILTER_OUTPUTS(blockdetect_outputs),
290 .priv_class = &blockdetect_class,
291 .flags = AVFILTER_FLAG_METADATA_ONLY,
292 };
293