FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/tests/checkasm/vf_nlmeans.c
Date: 2026-09-24 20:08:24
Exec Total Coverage
Lines: 86 89 96.6%
Functions: 1 1 100.0%
Branches: 41 90 45.6%

Line Branch Exec Source
1 /*
2 * Copyright (c) 2018 Clément Bœsch <u pkh me>
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 <math.h>
22 #include <string.h>
23
24 #include "checkasm.h"
25 #include "libavfilter/vf_nlmeans_init.h"
26 #include "libavutil/avassert.h"
27 #include "libavutil/mem.h"
28 #include "libavutil/mem_internal.h"
29
30 #define randomize_buffer(buf, size) do { \
31 int i; \
32 for (i = 0; i < size / 4; i++) \
33 ((uint32_t *)buf)[i] = rnd(); \
34 } while (0)
35
36 14 void checkasm_check_nlmeans(void)
37 {
38 14 NLMeansDSPContext dsp = {0};
39
40 14 const int w = 123; // source width
41 14 const int h = 45; // source height
42 14 const int p = 3; // patch half size
43 14 const int r = 2; // research window half size
44
45 14 ff_nlmeans_init(&dsp);
46
47 /* See the filter's code for the explanations on the variables */
48
2/2
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 13 times.
14 if (check_func(dsp.compute_safe_ssd_integral_image, "ssd_integral_image")) {
49 int offx, offy;
50 1 const int e = p + r;
51 1 const int ii_w = w + e*2;
52 1 const int ii_h = h + e*2;
53 1 const int ii_lz_32 = FFALIGN(ii_w + 1, 4);
54 1 uint32_t *ii_orig_ref = av_calloc(ii_h + 1, ii_lz_32 * sizeof(*ii_orig_ref));
55 1 uint32_t *ii_ref = ii_orig_ref + ii_lz_32 + 1;
56 1 uint32_t *ii_orig_new = av_calloc(ii_h + 1, ii_lz_32 * sizeof(*ii_orig_new));
57 1 uint32_t *ii_new = ii_orig_new + ii_lz_32 + 1;
58 1 const int src_lz = FFALIGN(w, 16);
59 1 uint8_t *src = av_calloc(h, src_lz);
60
61 1 declare_func(void, uint32_t *dst, ptrdiff_t dst_linesize_32,
62 const uint8_t *s1, ptrdiff_t linesize1,
63 const uint8_t *s2, ptrdiff_t linesize2,
64 int w, int h);
65
66
2/2
✓ Branch 1 taken 1440 times.
✓ Branch 2 taken 1 times.
1441 randomize_buffer(src, h * src_lz);
67
68
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 1 times.
6 for (offy = -r; offy <= r; offy++) {
69
2/2
✓ Branch 0 taken 25 times.
✓ Branch 1 taken 5 times.
30 for (offx = -r; offx <= r; offx++) {
70
4/4
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 20 times.
✓ Branch 2 taken 4 times.
✓ Branch 3 taken 1 times.
25 if (offx || offy) {
71 24 const int s1x = e;
72 24 const int s1y = e;
73 24 const int s2x = e + offx;
74 24 const int s2y = e + offy;
75 24 const int startx_safe = FFMAX(s1x, s2x);
76 24 const int starty_safe = FFMAX(s1y, s2y);
77
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 14 times.
24 const int u_endx_safe = FFMIN(s1x + w, s2x + w);
78
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 14 times.
24 const int endy_safe = FFMIN(s1y + h, s2y + h);
79 24 const int safe_pw = (u_endx_safe - startx_safe) & ~0xf;
80 24 const int safe_ph = endy_safe - starty_safe;
81
82
2/4
✓ Branch 0 taken 24 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 24 times.
24 av_assert0(safe_pw && safe_ph);
83
2/4
✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 24 times.
24 av_assert0(startx_safe - s1x >= 0); av_assert0(startx_safe - s1x < w);
84
2/4
✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 24 times.
24 av_assert0(starty_safe - s1y >= 0); av_assert0(starty_safe - s1y < h);
85
2/4
✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 24 times.
24 av_assert0(startx_safe - s2x >= 0); av_assert0(startx_safe - s2x < w);
86
2/4
✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 24 times.
24 av_assert0(starty_safe - s2y >= 0); av_assert0(starty_safe - s2y < h);
87
88 24 memset(ii_ref, 0, (ii_lz_32 * ii_h - 1) * sizeof(*ii_ref));
89 24 memset(ii_new, 0, (ii_lz_32 * ii_h - 1) * sizeof(*ii_new));
90
91
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 24 times.
24 call_ref(ii_ref + starty_safe*ii_lz_32 + startx_safe, ii_lz_32,
92 src + (starty_safe - s1y) * src_lz + (startx_safe - s1x), src_lz,
93 src + (starty_safe - s2y) * src_lz + (startx_safe - s2x), src_lz,
94 safe_pw, safe_ph);
95 24 call_new(ii_new + starty_safe*ii_lz_32 + startx_safe, ii_lz_32,
96 src + (starty_safe - s1y) * src_lz + (startx_safe - s1x), src_lz,
97 src + (starty_safe - s2y) * src_lz + (startx_safe - s2x), src_lz,
98 safe_pw, safe_ph);
99
100
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
24 if (memcmp(ii_ref, ii_new, (ii_lz_32 * ii_h - 1) * sizeof(*ii_ref)))
101 ✗ fail();
102
103 24 memset(ii_new, 0, (ii_lz_32 * ii_h - 1) * sizeof(*ii_new));
104
1/18
✗ Branch 1 not taken.
✓ Branch 2 taken 24 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 41 not taken.
✗ Branch 42 not taken.
✗ Branch 43 not taken.
✗ Branch 44 not taken.
✗ Branch 45 not taken.
✗ Branch 46 not taken.
✗ Branch 47 not taken.
✗ Branch 48 not taken.
✗ Branch 51 not taken.
✗ Branch 52 not taken.
✗ Branch 54 not taken.
✗ Branch 55 not taken.
✗ Branch 59 not taken.
✗ Branch 60 not taken.
24 bench_new(ii_new + starty_safe*ii_lz_32 + startx_safe, ii_lz_32,
105 src + (starty_safe - s1y) * src_lz + (startx_safe - s1x), src_lz,
106 src + (starty_safe - s2y) * src_lz + (startx_safe - s2x), src_lz,
107 safe_pw, safe_ph);
108 }
109 }
110 }
111
112 1 av_freep(&ii_orig_ref);
113 1 av_freep(&ii_orig_new);
114 1 av_freep(&src);
115 }
116
117
2/2
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 12 times.
14 if (check_func(dsp.compute_weights_line, "compute_weights_line")) {
118 #define TEST_W 256
119 #define MAX_MEANINGFUL_DIFF 255
120 2 const int startx = 10;
121 2 const int endx = 200;
122
123 // Allocate aligned buffers on stack
124 2 LOCAL_ALIGNED_32(uint32_t, iia, [TEST_W + 16]);
125 2 LOCAL_ALIGNED_32(uint32_t, iib, [TEST_W + 16]);
126 2 LOCAL_ALIGNED_32(uint32_t, iid, [TEST_W + 16]);
127 2 LOCAL_ALIGNED_32(uint32_t, iie, [TEST_W + 16]);
128 2 LOCAL_ALIGNED_32(uint8_t, src, [TEST_W + 16]);
129 2 LOCAL_ALIGNED_32(float, tw_ref, [TEST_W + 16]);
130 2 LOCAL_ALIGNED_32(float, tw_new, [TEST_W + 16]);
131 2 LOCAL_ALIGNED_32(float, sum_ref, [TEST_W + 16]);
132 2 LOCAL_ALIGNED_32(float, sum_new, [TEST_W + 16]);
133 2 LOCAL_ALIGNED_32(float, lut, [MAX_MEANINGFUL_DIFF + 1]);
134
135 2 declare_func(void, const uint32_t *const iia,
136 const uint32_t *const iib,
137 const uint32_t *const iid,
138 const uint32_t *const iie,
139 const uint8_t *const src,
140 float *total_weight,
141 float *sum,
142 const float *const weight_lut,
143 ptrdiff_t max_meaningful_diff,
144 ptrdiff_t startx, ptrdiff_t endx);
145
146 // Initialize LUT: weight = exp(-diff * scale)
147 // Using scale = 0.01 for testing
148
2/2
✓ Branch 0 taken 512 times.
✓ Branch 1 taken 2 times.
514 for (int i = 0; i <= MAX_MEANINGFUL_DIFF; i++)
149 512 lut[i] = expf(-i * 0.01f);
150
151 // Initialize source pixels
152
2/2
✓ Branch 0 taken 512 times.
✓ Branch 1 taken 2 times.
514 for (int i = 0; i < TEST_W; i++)
153 512 src[i] = rnd() & 0xff;
154
155 // Initialize integral images
156 // We need to ensure diff = e - d - b + a is non-negative and within range
157 // Set up as if computing real integral image values
158
2/2
✓ Branch 0 taken 512 times.
✓ Branch 1 taken 2 times.
514 for (int i = 0; i < TEST_W; i++) {
159 512 uint32_t base = rnd() % 1000;
160 512 iia[i] = base;
161 512 iib[i] = base + (rnd() % 100);
162 512 iid[i] = base + (rnd() % 100);
163 // e = a + (b - a) + (d - a) + diff
164 // So diff = e - d - b + a will be in range [0, max_meaningful_diff]
165 512 uint32_t diff = rnd() % (MAX_MEANINGFUL_DIFF + 1);
166 512 iie[i] = iia[i] + (iib[i] - iia[i]) + (iid[i] - iia[i]) + diff;
167 }
168
169 // Clear output buffers
170 2 memset(tw_ref, 0, (TEST_W + 16) * sizeof(float));
171 2 memset(tw_new, 0, (TEST_W + 16) * sizeof(float));
172 2 memset(sum_ref, 0, (TEST_W + 16) * sizeof(float));
173 2 memset(sum_new, 0, (TEST_W + 16) * sizeof(float));
174
175
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
2 call_ref(iia, iib, iid, iie, src, tw_ref, sum_ref, lut,
176 MAX_MEANINGFUL_DIFF, startx, endx);
177 2 call_new(iia, iib, iid, iie, src, tw_new, sum_new, lut,
178 MAX_MEANINGFUL_DIFF, startx, endx);
179
180 // Compare results with small tolerance for floating point
181
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 if (!float_near_abs_eps_array(tw_ref + startx, tw_new + startx, 1e-5f, endx - startx))
182 ✗ fail();
183
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 if (!float_near_abs_eps_array(sum_ref + startx, sum_new + startx, 1e-4f, endx - startx))
184 ✗ fail();
185
186 // Benchmark
187 2 memset(tw_new, 0, (TEST_W + 16) * sizeof(float));
188 2 memset(sum_new, 0, (TEST_W + 16) * sizeof(float));
189
1/18
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 41 not taken.
✗ Branch 42 not taken.
✗ Branch 43 not taken.
✗ Branch 44 not taken.
✗ Branch 45 not taken.
✗ Branch 46 not taken.
✗ Branch 47 not taken.
✗ Branch 48 not taken.
✗ Branch 51 not taken.
✗ Branch 52 not taken.
✗ Branch 54 not taken.
✗ Branch 55 not taken.
✗ Branch 59 not taken.
✗ Branch 60 not taken.
2 bench_new(iia, iib, iid, iie, src, tw_new, sum_new, lut,
190 MAX_MEANINGFUL_DIFF, startx, endx);
191 }
192
193 14 report("dsp");
194 14 }
195