FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/tests/checkasm/hevc_deblock.c
Date: 2024-11-20 23:03:26
Exec Total Coverage
Lines: 123 127 96.9%
Functions: 4 4 100.0%
Branches: 136 170 80.0%

Line Branch Exec Source
1 /*
2 * This file is part of FFmpeg.
3 *
4 * FFmpeg is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * FFmpeg is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with FFmpeg; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17 */
18
19 #include <string.h>
20
21 #include "libavutil/intreadwrite.h"
22 #include "libavutil/macros.h"
23 #include "libavutil/mem_internal.h"
24
25 #include "libavcodec/hevc/dsp.h"
26
27 #include "checkasm.h"
28
29 static const uint32_t pixel_mask[3] = { 0xffffffff, 0x03ff03ff, 0x0fff0fff };
30
31 #define SIZEOF_PIXEL ((bit_depth + 7) / 8)
32 #define BUF_STRIDE (16 * 2)
33 #define BUF_LINES (16)
34 // large buffer sizes based on high bit depth
35 #define BUF_OFFSET (2 * BUF_STRIDE * BUF_LINES)
36 #define BUF_SIZE (2 * BUF_STRIDE * BUF_LINES + BUF_OFFSET * 2)
37
38 #define randomize_buffers(buf0, buf1, size) \
39 do { \
40 uint32_t mask = pixel_mask[(bit_depth - 8) >> 1]; \
41 int k; \
42 for (k = 0; k < size; k += 4) { \
43 uint32_t r = rnd() & mask; \
44 AV_WN32A(buf0 + k, r); \
45 AV_WN32A(buf1 + k, r); \
46 } \
47 } while (0)
48
49 78 static void check_deblock_chroma(HEVCDSPContext *h, int bit_depth, int c)
50 {
51 // see tctable[] in hevc_filter.c, we check full range
52 78 int32_t tc[2] = { rnd() % 25, rnd() % 25 };
53 // no_p, no_q can only be { 0,0 } for the simpler assembly (non *_c
54 // variant) functions, see deblocking_filter_CTB() in hevc_filter.c
55 78 uint8_t no_p[2] = { rnd() & c, rnd() & c };
56 78 uint8_t no_q[2] = { rnd() & c, rnd() & c };
57 78 LOCAL_ALIGNED_32(uint8_t, buf0, [BUF_SIZE]);
58 78 LOCAL_ALIGNED_32(uint8_t, buf1, [BUF_SIZE]);
59
60 78 declare_func(void, uint8_t *pix, ptrdiff_t stride,
61 const int32_t *tc, const uint8_t *no_p, const uint8_t *no_q);
62
63
6/6
✓ Branch 2 taken 39 times.
✓ Branch 3 taken 39 times.
✓ Branch 4 taken 39 times.
✓ Branch 5 taken 39 times.
✓ Branch 7 taken 12 times.
✓ Branch 8 taken 66 times.
78 if (check_func(c ? h->hevc_h_loop_filter_chroma_c : h->hevc_h_loop_filter_chroma,
64 "hevc_h_loop_filter_chroma%d%s", bit_depth, c ? "_full" : ""))
65 {
66
2/2
✓ Branch 1 taken 9216 times.
✓ Branch 2 taken 12 times.
9228 randomize_buffers(buf0, buf1, BUF_SIZE);
67
68 12 call_ref(buf0 + BUF_OFFSET, BUF_STRIDE, tc, no_p, no_q);
69 12 call_new(buf1 + BUF_OFFSET, BUF_STRIDE, tc, no_p, no_q);
70
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
12 if (memcmp(buf0, buf1, BUF_SIZE))
71 fail();
72
1/8
✗ Branch 1 not taken.
✓ Branch 2 taken 12 times.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
12 bench_new(buf1 + BUF_OFFSET, BUF_STRIDE, tc, no_p, no_q);
73 }
74
75
6/6
✓ Branch 2 taken 39 times.
✓ Branch 3 taken 39 times.
✓ Branch 4 taken 39 times.
✓ Branch 5 taken 39 times.
✓ Branch 7 taken 12 times.
✓ Branch 8 taken 66 times.
78 if (check_func(c ? h->hevc_v_loop_filter_chroma_c : h->hevc_v_loop_filter_chroma,
76 "hevc_v_loop_filter_chroma%d%s", bit_depth, c ? "_full" : ""))
77 {
78
2/2
✓ Branch 1 taken 9216 times.
✓ Branch 2 taken 12 times.
9228 randomize_buffers(buf0, buf1, BUF_SIZE);
79
80 12 call_ref(buf0 + BUF_OFFSET, BUF_STRIDE, tc, no_p, no_q);
81 12 call_new(buf1 + BUF_OFFSET, BUF_STRIDE, tc, no_p, no_q);
82
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
12 if (memcmp(buf0, buf1, BUF_SIZE))
83 fail();
84
1/8
✗ Branch 1 not taken.
✓ Branch 2 taken 12 times.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
12 bench_new(buf1 + BUF_OFFSET, BUF_STRIDE, tc, no_p, no_q);
85 }
86 78 }
87
88 #define P3 buf[-4 * xstride]
89 #define P2 buf[-3 * xstride]
90 #define P1 buf[-2 * xstride]
91 #define P0 buf[-1 * xstride]
92 #define Q0 buf[0 * xstride]
93 #define Q1 buf[1 * xstride]
94 #define Q2 buf[2 * xstride]
95 #define Q3 buf[3 * xstride]
96
97 #define TC25(x) ((tc[x] * 5 + 1) >> 1)
98 #define MASK(x) (uint16_t)(x & ((1 << (bit_depth)) - 1))
99 #define GET(x) ((SIZEOF_PIXEL == 1) ? *(uint8_t*)(&x) : *(uint16_t*)(&x))
100 #define SET(x, y) do { \
101 uint16_t z = MASK(y); \
102 if (SIZEOF_PIXEL == 1) \
103 *(uint8_t*)(&x) = z; \
104 else \
105 *(uint16_t*)(&x) = z; \
106 } while (0)
107 #define RANDCLIP(x, diff) av_clip(GET(x) - (diff), 0, \
108 (1 << (bit_depth)) - 1) + rnd() % FFMAX(2 * (diff), 1)
109
110 // NOTE: this function doesn't work 'correctly' in that it won't always choose
111 // strong/strong or weak/weak, in most cases it tends to but will sometimes mix
112 // weak/strong or even skip sometimes. This is more useful to test correctness
113 // for these functions, though it does make benching them difficult. The easiest
114 // way to bench these functions is to check an overall decode since there are too
115 // many paths and ways to trigger the deblock: we would have to bench all
116 // permutations of weak/strong/skip/nd_q/nd_p/no_q/no_p and it quickly becomes
117 // too much.
118 90 static void randomize_luma_buffers(int type, int *beta, int32_t tc[2],
119 uint8_t *buf, ptrdiff_t xstride, ptrdiff_t ystride, int bit_depth)
120 {
121 int i, j, b3, tc25, tc25diff, b3diff;
122 // both tc & beta are unscaled inputs
123 // minimum useful value is 1, full range 0-24
124 90 tc[0] = (rnd() % 25) + 1;
125 90 tc[1] = (rnd() % 25) + 1;
126 // minimum useful value for 8bit is 8
127 90 *beta = (rnd() % 57) + 8;
128
129
3/4
✓ Branch 0 taken 30 times.
✓ Branch 1 taken 30 times.
✓ Branch 2 taken 30 times.
✗ Branch 3 not taken.
90 switch (type) {
130 30 case 0: // strong
131
2/2
✓ Branch 0 taken 60 times.
✓ Branch 1 taken 30 times.
90 for (j = 0; j < 2; j++) {
132 60 tc25 = TC25(j) << (bit_depth - 8);
133 60 tc25diff = FFMAX(tc25 - 1, 0);
134 // 4 lines per tc
135
2/2
✓ Branch 0 taken 240 times.
✓ Branch 1 taken 60 times.
300 for (i = 0; i < 4; i++) {
136 240 b3 = (*beta << (bit_depth - 8)) >> 3;
137
138
2/2
✓ Branch 1 taken 80 times.
✓ Branch 2 taken 160 times.
240 SET(P0, rnd() % (1 << bit_depth));
139
4/4
✓ Branch 0 taken 80 times.
✓ Branch 1 taken 160 times.
✓ Branch 3 taken 80 times.
✓ Branch 4 taken 160 times.
240 SET(Q0, RANDCLIP(P0, tc25diff));
140
141 // p3 - p0 up to beta3 budget
142 240 b3diff = rnd() % b3;
143
4/4
✓ Branch 0 taken 80 times.
✓ Branch 1 taken 160 times.
✓ Branch 3 taken 80 times.
✓ Branch 4 taken 160 times.
240 SET(P3, RANDCLIP(P0, b3diff));
144 // q3 - q0, reduced budget
145 240 b3diff = rnd() % FFMAX(b3 - b3diff, 1);
146
4/4
✓ Branch 0 taken 80 times.
✓ Branch 1 taken 160 times.
✓ Branch 3 taken 80 times.
✓ Branch 4 taken 160 times.
240 SET(Q3, RANDCLIP(Q0, b3diff));
147
148 // same concept, budget across 4 pixels
149 240 b3 -= b3diff = rnd() % FFMAX(b3, 1);
150
4/4
✓ Branch 0 taken 80 times.
✓ Branch 1 taken 160 times.
✓ Branch 3 taken 80 times.
✓ Branch 4 taken 160 times.
240 SET(P2, RANDCLIP(P0, b3diff));
151 240 b3 -= b3diff = rnd() % FFMAX(b3, 1);
152
4/4
✓ Branch 0 taken 80 times.
✓ Branch 1 taken 160 times.
✓ Branch 3 taken 80 times.
✓ Branch 4 taken 160 times.
240 SET(Q2, RANDCLIP(Q0, b3diff));
153
154 // extra reduced budget for weighted pixels
155 240 b3 -= b3diff = rnd() % FFMAX(b3 - (1 << (bit_depth - 8)), 1);
156
4/4
✓ Branch 0 taken 80 times.
✓ Branch 1 taken 160 times.
✓ Branch 3 taken 80 times.
✓ Branch 4 taken 160 times.
240 SET(P1, RANDCLIP(P0, b3diff));
157 240 b3 -= b3diff = rnd() % FFMAX(b3 - (1 << (bit_depth - 8)), 1);
158
4/4
✓ Branch 0 taken 80 times.
✓ Branch 1 taken 160 times.
✓ Branch 3 taken 80 times.
✓ Branch 4 taken 160 times.
240 SET(Q1, RANDCLIP(Q0, b3diff));
159
160 240 buf += ystride;
161 }
162 }
163 30 break;
164 30 case 1: // weak
165
2/2
✓ Branch 0 taken 60 times.
✓ Branch 1 taken 30 times.
90 for (j = 0; j < 2; j++) {
166 60 tc25 = TC25(j) << (bit_depth - 8);
167 60 tc25diff = FFMAX(tc25 - 1, 0);
168 // 4 lines per tc
169
2/2
✓ Branch 0 taken 240 times.
✓ Branch 1 taken 60 times.
300 for (i = 0; i < 4; i++) {
170 // Weak filtering is signficantly simpler to activate as
171 // we only need to satisfy d0 + d3 < beta, which
172 // can be simplified to d0 + d0 < beta. Using the above
173 // derivations but substiuting b3 for b1 and ensuring
174 // that P0/Q0 are at least 1/2 tc25diff apart (tending
175 // towards 1/2 range).
176 240 b3 = (*beta << (bit_depth - 8)) >> 1;
177
178
2/2
✓ Branch 1 taken 80 times.
✓ Branch 2 taken 160 times.
240 SET(P0, rnd() % (1 << bit_depth));
179
5/6
✓ Branch 0 taken 80 times.
✓ Branch 1 taken 160 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 240 times.
✓ Branch 5 taken 80 times.
✓ Branch 6 taken 160 times.
240 SET(Q0, RANDCLIP(P0, tc25diff >> 1) +
180 (tc25diff >> 1) * (P0 < (1 << (bit_depth - 1))) ? 1 : -1);
181
182 // p3 - p0 up to beta3 budget
183 240 b3diff = rnd() % b3;
184
4/4
✓ Branch 0 taken 80 times.
✓ Branch 1 taken 160 times.
✓ Branch 3 taken 80 times.
✓ Branch 4 taken 160 times.
240 SET(P3, RANDCLIP(P0, b3diff));
185 // q3 - q0, reduced budget
186 240 b3diff = rnd() % FFMAX(b3 - b3diff, 1);
187
4/4
✓ Branch 0 taken 80 times.
✓ Branch 1 taken 160 times.
✓ Branch 3 taken 80 times.
✓ Branch 4 taken 160 times.
240 SET(Q3, RANDCLIP(Q0, b3diff));
188
189 // same concept, budget across 4 pixels
190 240 b3 -= b3diff = rnd() % FFMAX(b3, 1);
191
4/4
✓ Branch 0 taken 80 times.
✓ Branch 1 taken 160 times.
✓ Branch 3 taken 80 times.
✓ Branch 4 taken 160 times.
240 SET(P2, RANDCLIP(P0, b3diff));
192 240 b3 -= b3diff = rnd() % FFMAX(b3, 1);
193
4/4
✓ Branch 0 taken 80 times.
✓ Branch 1 taken 160 times.
✓ Branch 3 taken 80 times.
✓ Branch 4 taken 160 times.
240 SET(Q2, RANDCLIP(Q0, b3diff));
194
195 // extra reduced budget for weighted pixels
196 240 b3 -= b3diff = rnd() % FFMAX(b3 - (1 << (bit_depth - 8)), 1);
197
4/4
✓ Branch 0 taken 80 times.
✓ Branch 1 taken 160 times.
✓ Branch 3 taken 80 times.
✓ Branch 4 taken 160 times.
240 SET(P1, RANDCLIP(P0, b3diff));
198 240 b3 -= b3diff = rnd() % FFMAX(b3 - (1 << (bit_depth - 8)), 1);
199
4/4
✓ Branch 0 taken 80 times.
✓ Branch 1 taken 160 times.
✓ Branch 3 taken 80 times.
✓ Branch 4 taken 160 times.
240 SET(Q1, RANDCLIP(Q0, b3diff));
200
201 240 buf += ystride;
202 }
203 }
204 30 break;
205 30 case 2: // none
206 30 *beta = 0; // ensure skip
207
2/2
✓ Branch 0 taken 240 times.
✓ Branch 1 taken 30 times.
270 for (i = 0; i < 8; i++) {
208 // we can just fill with completely random data, nothing should be touched.
209
8/8
✓ Branch 1 taken 80 times.
✓ Branch 2 taken 160 times.
✓ Branch 4 taken 80 times.
✓ Branch 5 taken 160 times.
✓ Branch 7 taken 80 times.
✓ Branch 8 taken 160 times.
✓ Branch 10 taken 80 times.
✓ Branch 11 taken 160 times.
240 SET(P3, rnd()); SET(P2, rnd()); SET(P1, rnd()); SET(P0, rnd());
210
8/8
✓ Branch 1 taken 80 times.
✓ Branch 2 taken 160 times.
✓ Branch 4 taken 80 times.
✓ Branch 5 taken 160 times.
✓ Branch 7 taken 80 times.
✓ Branch 8 taken 160 times.
✓ Branch 10 taken 80 times.
✓ Branch 11 taken 160 times.
240 SET(Q0, rnd()); SET(Q1, rnd()); SET(Q2, rnd()); SET(Q3, rnd());
211 240 buf += ystride;
212 }
213 30 break;
214 }
215 90 }
216
217 78 static void check_deblock_luma(HEVCDSPContext *h, int bit_depth, int c)
218 {
219 const char *type;
220 78 const char *types[3] = { "strong", "weak", "skip" };
221 int beta;
222 78 int32_t tc[2] = {0};
223 78 uint8_t no_p[2] = { rnd() & c, rnd() & c };
224 78 uint8_t no_q[2] = { rnd() & c, rnd() & c };
225 78 LOCAL_ALIGNED_32(uint8_t, buf0, [BUF_SIZE]);
226 78 LOCAL_ALIGNED_32(uint8_t, buf1, [BUF_SIZE]);
227 78 uint8_t *ptr0 = buf0 + BUF_OFFSET,
228 78 *ptr1 = buf1 + BUF_OFFSET;
229
230 78 declare_func(void, uint8_t *pix, ptrdiff_t stride, int beta,
231 const int32_t *tc, const uint8_t *no_p, const uint8_t *no_q);
232 78 memset(buf0, 0, BUF_SIZE);
233
234
2/2
✓ Branch 0 taken 234 times.
✓ Branch 1 taken 78 times.
312 for (int j = 0; j < 3; j++) {
235 234 type = types[j];
236
6/6
✓ Branch 2 taken 117 times.
✓ Branch 3 taken 117 times.
✓ Branch 4 taken 117 times.
✓ Branch 5 taken 117 times.
✓ Branch 7 taken 45 times.
✓ Branch 8 taken 189 times.
234 if (check_func(c ? h->hevc_h_loop_filter_luma_c : h->hevc_h_loop_filter_luma,
237 "hevc_h_loop_filter_luma%d_%s%s", bit_depth, type, c ? "_full" : ""))
238 {
239 45 randomize_luma_buffers(j, &beta, tc, buf0 + BUF_OFFSET, 16 * SIZEOF_PIXEL, SIZEOF_PIXEL, bit_depth);
240 45 memcpy(buf1, buf0, BUF_SIZE);
241
242 45 call_ref(ptr0, 16 * SIZEOF_PIXEL, beta, tc, no_p, no_q);
243 45 call_new(ptr1, 16 * SIZEOF_PIXEL, beta, tc, no_p, no_q);
244
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 45 times.
45 if (memcmp(buf0, buf1, BUF_SIZE))
245 fail();
246
1/8
✗ Branch 1 not taken.
✓ Branch 2 taken 45 times.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
45 bench_new(ptr1, 16 * SIZEOF_PIXEL, beta, tc, no_p, no_q);
247 }
248
249
6/6
✓ Branch 2 taken 117 times.
✓ Branch 3 taken 117 times.
✓ Branch 4 taken 117 times.
✓ Branch 5 taken 117 times.
✓ Branch 7 taken 45 times.
✓ Branch 8 taken 189 times.
234 if (check_func(c ? h->hevc_v_loop_filter_luma_c : h->hevc_v_loop_filter_luma,
250 "hevc_v_loop_filter_luma%d_%s%s", bit_depth, type, c ? "_full" : ""))
251 {
252 45 randomize_luma_buffers(j, &beta, tc, buf0 + BUF_OFFSET, SIZEOF_PIXEL, 16 * SIZEOF_PIXEL, bit_depth);
253 45 memcpy(buf1, buf0, BUF_SIZE);
254
255 45 call_ref(ptr0, 16 * SIZEOF_PIXEL, beta, tc, no_p, no_q);
256 45 call_new(ptr1, 16 * SIZEOF_PIXEL, beta, tc, no_p, no_q);
257
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 45 times.
45 if (memcmp(buf0, buf1, BUF_SIZE))
258 fail();
259
1/8
✗ Branch 1 not taken.
✓ Branch 2 taken 45 times.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
45 bench_new(ptr1, 16 * SIZEOF_PIXEL, beta, tc, no_p, no_q);
260 }
261 }
262 78 }
263
264 13 void checkasm_check_hevc_deblock(void)
265 {
266 HEVCDSPContext h;
267 int bit_depth;
268
2/2
✓ Branch 0 taken 39 times.
✓ Branch 1 taken 13 times.
52 for (bit_depth = 8; bit_depth <= 12; bit_depth += 2) {
269 39 ff_hevc_dsp_init(&h, bit_depth);
270 39 check_deblock_chroma(&h, bit_depth, 0);
271 }
272 13 report("chroma");
273
2/2
✓ Branch 0 taken 39 times.
✓ Branch 1 taken 13 times.
52 for (bit_depth = 8; bit_depth <= 12; bit_depth += 2) {
274 39 ff_hevc_dsp_init(&h, bit_depth);
275 39 check_deblock_chroma(&h, bit_depth, 1);
276 }
277 13 report("chroma_full");
278
2/2
✓ Branch 0 taken 39 times.
✓ Branch 1 taken 13 times.
52 for (bit_depth = 8; bit_depth <= 12; bit_depth += 2) {
279 39 ff_hevc_dsp_init(&h, bit_depth);
280 39 check_deblock_luma(&h, bit_depth, 0);
281 }
282 13 report("luma");
283
2/2
✓ Branch 0 taken 39 times.
✓ Branch 1 taken 13 times.
52 for (bit_depth = 8; bit_depth <= 12; bit_depth += 2) {
284 39 ff_hevc_dsp_init(&h, bit_depth);
285 39 check_deblock_luma(&h, bit_depth, 1);
286 }
287 13 report("luma_full");
288 13 }
289