FFmpeg coverage


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