FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/tests/checkasm/hevc_deblock.c
Date: 2024-05-03 15:42:48
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/hevcdsp.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, int32_t *tc, uint8_t *no_p, uint8_t *no_q);
61
62
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,
63 "hevc_h_loop_filter_chroma%d%s", bit_depth, c ? "_full" : ""))
64 {
65
2/2
✓ Branch 1 taken 9216 times.
✓ Branch 2 taken 12 times.
9228 randomize_buffers(buf0, buf1, BUF_SIZE);
66
67 12 call_ref(buf0 + BUF_OFFSET, BUF_STRIDE, tc, no_p, no_q);
68 12 call_new(buf1 + BUF_OFFSET, BUF_STRIDE, tc, no_p, no_q);
69
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
12 if (memcmp(buf0, buf1, BUF_SIZE))
70 fail();
71
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);
72 }
73
74
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,
75 "hevc_v_loop_filter_chroma%d%s", bit_depth, c ? "_full" : ""))
76 {
77
2/2
✓ Branch 1 taken 9216 times.
✓ Branch 2 taken 12 times.
9228 randomize_buffers(buf0, buf1, BUF_SIZE);
78
79 12 call_ref(buf0 + BUF_OFFSET, BUF_STRIDE, tc, no_p, no_q);
80 12 call_new(buf1 + BUF_OFFSET, BUF_STRIDE, tc, no_p, no_q);
81
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
12 if (memcmp(buf0, buf1, BUF_SIZE))
82 fail();
83
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);
84 }
85 78 }
86
87 #define P3 buf[-4 * xstride]
88 #define P2 buf[-3 * xstride]
89 #define P1 buf[-2 * xstride]
90 #define P0 buf[-1 * xstride]
91 #define Q0 buf[0 * xstride]
92 #define Q1 buf[1 * xstride]
93 #define Q2 buf[2 * xstride]
94 #define Q3 buf[3 * xstride]
95
96 #define TC25(x) ((tc[x] * 5 + 1) >> 1)
97 #define MASK(x) (uint16_t)(x & ((1 << (bit_depth)) - 1))
98 #define GET(x) ((SIZEOF_PIXEL == 1) ? *(uint8_t*)(&x) : *(uint16_t*)(&x))
99 #define SET(x, y) do { \
100 uint16_t z = MASK(y); \
101 if (SIZEOF_PIXEL == 1) \
102 *(uint8_t*)(&x) = z; \
103 else \
104 *(uint16_t*)(&x) = z; \
105 } while (0)
106 #define RANDCLIP(x, diff) av_clip(GET(x) - (diff), 0, \
107 (1 << (bit_depth)) - 1) + rnd() % FFMAX(2 * (diff), 1)
108
109 // NOTE: this function doesn't work 'correctly' in that it won't always choose
110 // strong/strong or weak/weak, in most cases it tends to but will sometimes mix
111 // weak/strong or even skip sometimes. This is more useful to test correctness
112 // for these functions, though it does make benching them difficult. The easiest
113 // way to bench these functions is to check an overall decode since there are too
114 // many paths and ways to trigger the deblock: we would have to bench all
115 // permutations of weak/strong/skip/nd_q/nd_p/no_q/no_p and it quickly becomes
116 // too much.
117 90 static void randomize_luma_buffers(int type, int *beta, int32_t tc[2],
118 uint8_t *buf, ptrdiff_t xstride, ptrdiff_t ystride, int bit_depth)
119 {
120 int i, j, b3, tc25, tc25diff, b3diff;
121 // both tc & beta are unscaled inputs
122 // minimum useful value is 1, full range 0-24
123 90 tc[0] = (rnd() % 25) + 1;
124 90 tc[1] = (rnd() % 25) + 1;
125 // minimum useful value for 8bit is 8
126 90 *beta = (rnd() % 57) + 8;
127
128
3/4
✓ Branch 0 taken 30 times.
✓ Branch 1 taken 30 times.
✓ Branch 2 taken 30 times.
✗ Branch 3 not taken.
90 switch (type) {
129 30 case 0: // strong
130
2/2
✓ Branch 0 taken 60 times.
✓ Branch 1 taken 30 times.
90 for (j = 0; j < 2; j++) {
131 60 tc25 = TC25(j) << (bit_depth - 8);
132 60 tc25diff = FFMAX(tc25 - 1, 0);
133 // 4 lines per tc
134
2/2
✓ Branch 0 taken 240 times.
✓ Branch 1 taken 60 times.
300 for (i = 0; i < 4; i++) {
135 240 b3 = (*beta << (bit_depth - 8)) >> 3;
136
137
2/2
✓ Branch 1 taken 80 times.
✓ Branch 2 taken 160 times.
240 SET(P0, rnd() % (1 << bit_depth));
138
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));
139
140 // p3 - p0 up to beta3 budget
141 240 b3diff = rnd() % b3;
142
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));
143 // q3 - q0, reduced budget
144 240 b3diff = rnd() % FFMAX(b3 - b3diff, 1);
145
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));
146
147 // same concept, budget across 4 pixels
148 240 b3 -= b3diff = rnd() % FFMAX(b3, 1);
149
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));
150 240 b3 -= b3diff = rnd() % FFMAX(b3, 1);
151
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));
152
153 // extra reduced budget for weighted pixels
154 240 b3 -= b3diff = rnd() % FFMAX(b3 - (1 << (bit_depth - 8)), 1);
155
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));
156 240 b3 -= b3diff = rnd() % FFMAX(b3 - (1 << (bit_depth - 8)), 1);
157
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));
158
159 240 buf += ystride;
160 }
161 }
162 30 break;
163 30 case 1: // weak
164
2/2
✓ Branch 0 taken 60 times.
✓ Branch 1 taken 30 times.
90 for (j = 0; j < 2; j++) {
165 60 tc25 = TC25(j) << (bit_depth - 8);
166 60 tc25diff = FFMAX(tc25 - 1, 0);
167 // 4 lines per tc
168
2/2
✓ Branch 0 taken 240 times.
✓ Branch 1 taken 60 times.
300 for (i = 0; i < 4; i++) {
169 // Weak filtering is signficantly simpler to activate as
170 // we only need to satisfy d0 + d3 < beta, which
171 // can be simplified to d0 + d0 < beta. Using the above
172 // derivations but substiuting b3 for b1 and ensuring
173 // that P0/Q0 are at least 1/2 tc25diff apart (tending
174 // towards 1/2 range).
175 240 b3 = (*beta << (bit_depth - 8)) >> 1;
176
177
2/2
✓ Branch 1 taken 80 times.
✓ Branch 2 taken 160 times.
240 SET(P0, rnd() % (1 << bit_depth));
178
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) +
179 (tc25diff >> 1) * (P0 < (1 << (bit_depth - 1))) ? 1 : -1);
180
181 // p3 - p0 up to beta3 budget
182 240 b3diff = rnd() % b3;
183
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));
184 // q3 - q0, reduced budget
185 240 b3diff = rnd() % FFMAX(b3 - b3diff, 1);
186
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));
187
188 // same concept, budget across 4 pixels
189 240 b3 -= b3diff = rnd() % FFMAX(b3, 1);
190
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));
191 240 b3 -= b3diff = rnd() % FFMAX(b3, 1);
192
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));
193
194 // extra reduced budget for weighted pixels
195 240 b3 -= b3diff = rnd() % FFMAX(b3 - (1 << (bit_depth - 8)), 1);
196
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));
197 240 b3 -= b3diff = rnd() % FFMAX(b3 - (1 << (bit_depth - 8)), 1);
198
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));
199
200 240 buf += ystride;
201 }
202 }
203 30 break;
204 30 case 2: // none
205 30 *beta = 0; // ensure skip
206
2/2
✓ Branch 0 taken 240 times.
✓ Branch 1 taken 30 times.
270 for (i = 0; i < 8; i++) {
207 // we can just fill with completely random data, nothing should be touched.
208
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());
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(Q0, rnd()); SET(Q1, rnd()); SET(Q2, rnd()); SET(Q3, rnd());
210 240 buf += ystride;
211 }
212 30 break;
213 }
214 90 }
215
216 78 static void check_deblock_luma(HEVCDSPContext *h, int bit_depth, int c)
217 {
218 const char *type;
219 78 const char *types[3] = { "strong", "weak", "skip" };
220 int beta;
221 78 int32_t tc[2] = {0};
222 78 uint8_t no_p[2] = { rnd() & c, rnd() & c };
223 78 uint8_t no_q[2] = { rnd() & c, rnd() & c };
224 78 LOCAL_ALIGNED_32(uint8_t, buf0, [BUF_SIZE]);
225 78 LOCAL_ALIGNED_32(uint8_t, buf1, [BUF_SIZE]);
226 78 uint8_t *ptr0 = buf0 + BUF_OFFSET,
227 78 *ptr1 = buf1 + BUF_OFFSET;
228
229 78 declare_func(void, uint8_t *pix, ptrdiff_t stride, int beta, int32_t *tc, uint8_t *no_p, uint8_t *no_q);
230 78 memset(buf0, 0, BUF_SIZE);
231
232
2/2
✓ Branch 0 taken 234 times.
✓ Branch 1 taken 78 times.
312 for (int j = 0; j < 3; j++) {
233 234 type = types[j];
234
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,
235 "hevc_h_loop_filter_luma%d_%s%s", bit_depth, type, c ? "_full" : ""))
236 {
237 45 randomize_luma_buffers(j, &beta, tc, buf0 + BUF_OFFSET, 16 * SIZEOF_PIXEL, SIZEOF_PIXEL, bit_depth);
238 45 memcpy(buf1, buf0, BUF_SIZE);
239
240 45 call_ref(ptr0, 16 * SIZEOF_PIXEL, beta, tc, no_p, no_q);
241 45 call_new(ptr1, 16 * SIZEOF_PIXEL, beta, tc, no_p, no_q);
242
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 45 times.
45 if (memcmp(buf0, buf1, BUF_SIZE))
243 fail();
244
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);
245 }
246
247
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,
248 "hevc_v_loop_filter_luma%d_%s%s", bit_depth, type, c ? "_full" : ""))
249 {
250 45 randomize_luma_buffers(j, &beta, tc, buf0 + BUF_OFFSET, SIZEOF_PIXEL, 16 * SIZEOF_PIXEL, bit_depth);
251 45 memcpy(buf1, buf0, BUF_SIZE);
252
253 45 call_ref(ptr0, 16 * SIZEOF_PIXEL, beta, tc, no_p, no_q);
254 45 call_new(ptr1, 16 * SIZEOF_PIXEL, beta, tc, no_p, no_q);
255
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 45 times.
45 if (memcmp(buf0, buf1, BUF_SIZE))
256 fail();
257
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);
258 }
259 }
260 78 }
261
262 13 void checkasm_check_hevc_deblock(void)
263 {
264 HEVCDSPContext h;
265 int bit_depth;
266
2/2
✓ Branch 0 taken 39 times.
✓ Branch 1 taken 13 times.
52 for (bit_depth = 8; bit_depth <= 12; bit_depth += 2) {
267 39 ff_hevc_dsp_init(&h, bit_depth);
268 39 check_deblock_chroma(&h, bit_depth, 0);
269 }
270 13 report("chroma");
271
2/2
✓ Branch 0 taken 39 times.
✓ Branch 1 taken 13 times.
52 for (bit_depth = 8; bit_depth <= 12; bit_depth += 2) {
272 39 ff_hevc_dsp_init(&h, bit_depth);
273 39 check_deblock_chroma(&h, bit_depth, 1);
274 }
275 13 report("chroma_full");
276
2/2
✓ Branch 0 taken 39 times.
✓ Branch 1 taken 13 times.
52 for (bit_depth = 8; bit_depth <= 12; bit_depth += 2) {
277 39 ff_hevc_dsp_init(&h, bit_depth);
278 39 check_deblock_luma(&h, bit_depth, 0);
279 }
280 13 report("luma");
281
2/2
✓ Branch 0 taken 39 times.
✓ Branch 1 taken 13 times.
52 for (bit_depth = 8; bit_depth <= 12; bit_depth += 2) {
282 39 ff_hevc_dsp_init(&h, bit_depth);
283 39 check_deblock_luma(&h, bit_depth, 1);
284 }
285 13 report("luma_full");
286 13 }
287