xref: /aosp_15_r20/external/libdav1d/tests/checkasm/refmvs.c (revision c09093415860a1c2373dacd84c4fde00c507cdfd)
1 /*
2  * Copyright © 2021, VideoLAN and dav1d authors
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice, this
9  *    list of conditions and the following disclaimer.
10  *
11  * 2. Redistributions in binary form must reproduce the above copyright notice,
12  *    this list of conditions and the following disclaimer in the documentation
13  *    and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
19  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26 
27 #include "tests/checkasm/checkasm.h"
28 #include "src/refmvs.h"
29 
30 #include <stdio.h>
31 
gen_mv(const int total_bits,int spel_bits)32 static inline int gen_mv(const int total_bits, int spel_bits) {
33     int bits = rnd() & ((1 << spel_bits) - 1);
34     do {
35         bits |= (rnd() & 1) << spel_bits;
36     } while (rnd() & 1 && ++spel_bits < total_bits);
37     // the do/while makes it relatively more likely to be close to zero (fpel)
38     // than far away
39     return rnd() & 1 ? -bits : bits;
40 }
41 
42 #define ARRAY_SIZE(n) (sizeof(n)/sizeof(*(n)))
43 
get_min_mv_val(const int idx)44 static inline int get_min_mv_val(const int idx) {
45     if      (idx <= 9)  return idx;
46     else if (idx <= 18) return (idx - 9) * 10;
47     else if (idx <= 27) return (idx - 18) * 100;
48     else if (idx <= 36) return (idx - 27) * 1000;
49     else                return (idx - 36) * 10000;
50 }
51 
gen_tmv(refmvs_temporal_block * const rb,const int * ref2ref)52 static inline void gen_tmv(refmvs_temporal_block *const rb, const int *ref2ref) {
53     rb->ref = rnd() % 7;
54     if (!rb->ref) return;
55     static const int x_prob[] = {
56         26447556, 6800591, 3708783,  2198592, 1635940, 1145901, 1052602, 1261759,
57          1099739,  755108, 6075404,  4355916, 3254908, 2897157, 2273676, 2154432,
58          1937436, 1694818, 1466863, 10203087, 5241546, 3328819, 2187483, 1458997,
59          1030842,  806863,  587219,   525024, 1858953,  422368,  114626,   16992
60     };
61     static const int y_prob[] = {
62         33845001, 7591218,  6425971, 4115838, 4032161, 2515962, 2614601, 2343656,
63          2898897, 1397254, 10125350, 5124449, 3232914, 2185499, 1608775, 1342585,
64           980208,  795714,   649665, 3369250, 1298716,  486002,  279588,  235990,
65           110318,   89372,    66895,   46980,  153322,   32960,    4500,     389
66     };
67     const int prob = rnd() % 100000000;
68     int acc = 0;
69     for (unsigned i = 0; i < ARRAY_SIZE(x_prob); i++) {
70         acc += x_prob[i];
71         if (prob < acc) {
72             const int min = get_min_mv_val(i);
73             const int max = get_min_mv_val(i + 1);
74             const int val = min + rnd() % (max - min);
75             rb->mv.x = iclip(val * ref2ref[rb->ref], -(1 << 15), (1 << 15) - 1);
76             break;
77         }
78     }
79     acc = 0;
80     for (unsigned i = 0; i < ARRAY_SIZE(y_prob); i++) {
81         acc += y_prob[i];
82         if (prob < acc) {
83             const int min = get_min_mv_val(i);
84             const int max = get_min_mv_val(i + 1);
85             const int val = min + rnd() % (max - min);
86             rb->mv.y = iclip(val * ref2ref[rb->ref], -(1 << 15), (1 << 15) - 1);
87             break;
88         }
89     }
90 }
91 
get_ref2cur(void)92 static inline int get_ref2cur(void) {
93     const int prob = rnd() % 100;
94     static const uint8_t ref2cur[11] = { 35, 55, 67, 73, 78, 83, 84, 87, 90, 93, 100 };
95     for (int i = 0; i < 11; i++)
96         if (prob < ref2cur[i])
97             return rnd() & 1 ? -(i + 1) : i + 1;
98     return 0;
99 }
100 
get_seqlen(void)101 static inline int get_seqlen(void) {
102     int len = 0, max_len;
103     const int prob = rnd() % 100000;
104     // =1 =2 =3 =4  <8  =8 <16 =16 <32 =32 <48 =48 <64 =64 >64 eq240
105     //  5 17 1.5 16  5  10  5   7   4   3  1.5  2   1    2   20   15   chimera blocks
106     // 25 38 2.5 19 3.5 5.5 2 1.87 .86 .4  .18 .2 .067 .165 .478 .28   chimera sequences
107 
108     if      (prob < 25000) len = 1;       // =1   5%
109     else if (prob < 63000) len = 2;       // =2   17%
110     else if (prob < 65500) len = 3;       // =3   1.5%
111     else if (prob < 84500) len = 4;       // =4   16%
112     else if (prob < 88000) max_len = 7;   // <8   5% (43.5% tot <8)
113     else if (prob < 93500) len = 8;       // =8   10%
114     else if (prob < 95500) max_len = 15;  // <16  5%
115     else if (prob < 97370) len = 16;      // =16  7%
116     else if (prob < 98230) max_len = 31;  // <32  4%
117     else if (prob < 98630) len = 32;      // =32  3%
118     else if (prob < 98810) max_len = 47;  // <48  1.5%
119     else if (prob < 99010) len = 48;      // =48  2%
120     else if (prob < 99077) max_len = 63;  // <64  1%
121     else if (prob < 99242) len = 64;      // =64  2%
122     else if (prob < 99720) max_len = 239; // <240 5%
123     else                   len = 240;     // =240 15%
124 
125     if (!len) len = 1 + rnd() % max_len;
126     return len;
127 }
128 
init_rp_ref(refmvs_frame const * const rf,const int col_start8,const int col_end8,const int row_start8,const int row_end8)129 static inline void init_rp_ref(refmvs_frame const *const rf,
130                                const int col_start8, const int col_end8,
131                                const int row_start8, const int row_end8)
132 {
133     const int col_start8i = imax(col_start8 - 8, 0);
134     const int col_end8i = imin(col_end8 + 8, rf->iw8);
135     for (int n = 0; n < rf->n_mfmvs; n++) {
136         refmvs_temporal_block *rp_ref = rf->rp_ref[rf->mfmv_ref[n]];
137         for (int i = row_start8; i < imin(row_end8, rf->ih8); i++) {
138             for (int j = col_start8i; j < col_end8i;) {
139                 refmvs_temporal_block rb;
140                 gen_tmv(&rb, rf->mfmv_ref2ref[n]);
141                 for (int k = get_seqlen(); k && j < col_end8i; k--, j++)
142                     rp_ref[i * rf->iw8 + j] = rb;
143             }
144         }
145     }
146 }
147 
check_load_tmvs(const Dav1dRefmvsDSPContext * const c)148 static void check_load_tmvs(const Dav1dRefmvsDSPContext *const c) {
149     refmvs_temporal_block *rp_ref[7] = {0};
150     refmvs_temporal_block c_rp_proj[240 * 63];
151     refmvs_temporal_block a_rp_proj[240 * 63];
152     refmvs_frame rf = {
153         .rp_ref = rp_ref,
154         .rp_stride = 240, .iw8 = 240, .ih8 = 63,
155         .n_mfmvs = 3
156     };
157     const size_t rp_ref_sz = rf.ih8 * rf.rp_stride * sizeof(refmvs_temporal_block);
158 
159     declare_func(void, const refmvs_frame *rf, int tile_row_idx,
160                  int col_start8, int col_end8, int row_start8, int row_end8);
161 
162     if (check_func(c->load_tmvs, "load_tmvs")) {
163         const int row_start8 = (rnd() & 3) << 4;
164         const int row_end8 = row_start8 + 16;
165         const int col_start8 = rnd() & 31;
166         const int col_end8 = rf.iw8 - (rnd() & 31);
167 
168         for (int n = 0; n < rf.n_mfmvs; n++) {
169             rf.mfmv_ref[n] = rnd() % 7;
170             rf.mfmv_ref2cur[n] = get_ref2cur();
171             for (int r = 0; r < 7; r++)
172                 rf.mfmv_ref2ref[n][r] = rnd() & 31;
173         }
174         for (int n = 0; n < rf.n_mfmvs; n++) {
175             refmvs_temporal_block **p_rp_ref = &rp_ref[rf.mfmv_ref[n]];
176             if (!*p_rp_ref)
177                 *p_rp_ref = malloc(rp_ref_sz);
178         }
179         init_rp_ref(&rf, 0, rf.iw8, row_start8, row_end8);
180         for (int i = 0; i < rf.iw8 * rf.ih8; i++) {
181             c_rp_proj[i].mv.n = a_rp_proj[i].mv.n = 0xdeadbeef;
182             c_rp_proj[i].ref = a_rp_proj[i].ref = 0xdd;
183         }
184 
185         rf.n_tile_threads = 1;
186 
187         rf.rp_proj = c_rp_proj;
188         call_ref(&rf, 0, col_start8, col_end8, row_start8, row_end8);
189         rf.rp_proj = a_rp_proj;
190         call_new(&rf, 0, col_start8, col_end8, row_start8, row_end8);
191 
192         for (int i = 0; i < rf.ih8; i++)
193             for (int j = 0; j < rf.iw8; j++)
194                 if (c_rp_proj[i * rf.iw8 + j].mv.n != a_rp_proj[i * rf.iw8 + j].mv.n ||
195                     (c_rp_proj[i * rf.iw8 + j].ref != a_rp_proj[i * rf.iw8 + j].ref &&
196                      c_rp_proj[i * rf.iw8 + j].mv.n != INVALID_MV))
197                 {
198                     if (fail()) {
199                         fprintf(stderr, "[%d][%d] c_rp.mv.x = 0x%x a_rp.mv.x = 0x%x\n",
200                                 i, j, c_rp_proj[i * rf.iw8 + j].mv.x, a_rp_proj[i * rf.iw8 + j].mv.x);
201                         fprintf(stderr, "[%d][%d] c_rp.mv.y = 0x%x a_rp.mv.y = 0x%x\n",
202                                 i, j, c_rp_proj[i * rf.iw8 + j].mv.y, a_rp_proj[i * rf.iw8 + j].mv.y);
203                         fprintf(stderr, "[%d][%d] c_rp.ref = %u a_rp.ref = %u\n",
204                                 i, j, c_rp_proj[i * rf.iw8 + j].ref, a_rp_proj[i * rf.iw8 + j].ref);
205                     }
206                 }
207 
208         if (checkasm_bench_func()) {
209             for (int n = 0; n < rf.n_mfmvs; n++) {
210                 rf.mfmv_ref2cur[n] = 1;
211                 for (int r = 0; r < 7; r++)
212                     rf.mfmv_ref2ref[n][r] = 1;
213             }
214             bench_new(&rf, 0, 0, rf.iw8, row_start8, row_end8);
215         }
216 
217         for (int n = 0; n < rf.n_mfmvs; n++) {
218             free(rp_ref[rf.mfmv_ref[n]]);
219             rp_ref[rf.mfmv_ref[n]] = NULL;
220         }
221     }
222 
223     report("load_tmvs");
224 }
225 
check_save_tmvs(const Dav1dRefmvsDSPContext * const c)226 static void check_save_tmvs(const Dav1dRefmvsDSPContext *const c) {
227     refmvs_block *rr[31];
228     refmvs_block r[31 * 256];
229     ALIGN_STK_64(refmvs_temporal_block, c_rp, 128 * 16,);
230     ALIGN_STK_64(refmvs_temporal_block, a_rp, 128 * 16,);
231     uint8_t ref_sign[7];
232 
233     for (int i = 0; i < 31; i++)
234         rr[i] = &r[i * 256];
235 
236     declare_func(void, refmvs_temporal_block *rp, const ptrdiff_t stride,
237                  refmvs_block *const *const rr, const uint8_t *const ref_sign,
238                  int col_end8, int row_end8, int col_start8, int row_start8);
239 
240     if (check_func(c->save_tmvs, "save_tmvs")) {
241         const int row_start8 = rnd() & 7;
242         const int row_end8 = 8 + (rnd() & 7);
243         const int col_start8 = rnd() & 31;
244         const int col_end8 = 96 + (rnd() & 31);
245 
246         for (int i = 0; i < 7; i++)
247             ref_sign[i] = rnd() & 1;
248 
249         for (int i = row_start8; i < row_end8; i++)
250             for (int j = col_start8; j < col_end8;) {
251                 int bs = rnd() % N_BS_SIZES;
252                 while (j + ((dav1d_block_dimensions[bs][0] + 1) >> 1) > col_end8)
253                     bs++;
254                 rr[i * 2][j * 2 + 1] = (refmvs_block) {
255                     .mv.mv[0].x = gen_mv(14, 10),
256                     .mv.mv[0].y = gen_mv(14, 10),
257                     .mv.mv[1].x = gen_mv(14, 10),
258                     .mv.mv[1].y = gen_mv(14, 10),
259                     .ref.ref = { (rnd() % 9) - 1, (rnd() % 9) - 1 },
260                     .bs = bs
261                 };
262                 for (int k = 0; k < (dav1d_block_dimensions[bs][0] + 1) >> 1; k++, j++) {
263                     c_rp[i * 128 + j].mv.n = 0xdeadbeef;
264                     c_rp[i * 128 + j].ref = 0xdd;
265                 }
266             }
267 
268         call_ref(c_rp + row_start8 * 128, 128, rr, ref_sign,
269                  col_end8, row_end8, col_start8, row_start8);
270         call_new(a_rp + row_start8 * 128, 128, rr, ref_sign,
271                  col_end8, row_end8, col_start8, row_start8);
272         for (int i = row_start8; i < row_end8; i++)
273             for (int j = col_start8; j < col_end8; j++)
274                 if (c_rp[i * 128 + j].mv.n != a_rp[i * 128 + j].mv.n ||
275                     c_rp[i * 128 + j].ref != a_rp[i * 128 + j].ref)
276                 {
277                     if (fail()) {
278                         fprintf(stderr, "[%d][%d] c_rp.mv.x = 0x%x a_rp.mv.x = 0x%x\n",
279                                 i, j, c_rp[i * 128 + j].mv.x, a_rp[i * 128 + j].mv.x);
280                         fprintf(stderr, "[%d][%d] c_rp.mv.y = 0x%x a_rp.mv.y = 0x%x\n",
281                                 i, j, c_rp[i * 128 + j].mv.y, a_rp[i * 128 + j].mv.y);
282                         fprintf(stderr, "[%d][%d] c_rp.ref = %u a_rp.ref = %u\n",
283                                 i, j, c_rp[i * 128 + j].ref, a_rp[i * 128 + j].ref);
284                     }
285                 }
286 
287         for (int bs = BS_4x4; bs < N_BS_SIZES; bs++) {
288             const int bw8 = (dav1d_block_dimensions[bs][0] + 1) >> 1;
289             for (int i = 0; i < 16; i++)
290                 for (int j = 0; j < 128; j += bw8) {
291                     rr[i * 2][j * 2 + 1].ref.ref[0] = (rnd() % 9) - 1;
292                     rr[i * 2][j * 2 + 1].ref.ref[1] = (rnd() % 9) - 1;
293                     rr[i * 2][j * 2 + 1].bs = bs;
294                 }
295             bench_new(alternate(c_rp, a_rp), 128, rr, ref_sign, 128, 16, 0, 0);
296         }
297     }
298 
299     report("save_tmvs");
300 }
301 
check_splat_mv(const Dav1dRefmvsDSPContext * const c)302 static void check_splat_mv(const Dav1dRefmvsDSPContext *const c) {
303     ALIGN_STK_64(refmvs_block, c_buf, 32 * 32,);
304     ALIGN_STK_64(refmvs_block, a_buf, 32 * 32,);
305     refmvs_block *c_dst[32];
306     refmvs_block *a_dst[32];
307     const size_t stride = 32 * sizeof(refmvs_block);
308 
309     for (int i = 0; i < 32; i++) {
310         c_dst[i] = c_buf + 32 * i;
311         a_dst[i] = a_buf + 32 * i;
312     }
313 
314     declare_func(void, refmvs_block **rr, const refmvs_block *rmv,
315                  int bx4, int bw4, int bh4);
316 
317     for (int w = 1; w <= 32; w *= 2) {
318         if (check_func(c->splat_mv, "splat_mv_w%d", w)) {
319             const int h_min = imax(w / 4, 1);
320             const int h_max = imin(w * 4, 32);
321             const int w_uint32 = w * sizeof(refmvs_block) / sizeof(uint32_t);
322             for (int h = h_min; h <= h_max; h *= 2) {
323                 const int offset = (int) ((unsigned) w * rnd()) & 31;
324                 union {
325                     refmvs_block rmv;
326                     uint32_t u32[3];
327                 } ALIGN(tmp, 16);
328                 tmp.u32[0] = rnd();
329                 tmp.u32[1] = rnd();
330                 tmp.u32[2] = rnd();
331 
332                 call_ref(c_dst, &tmp.rmv, offset, w, h);
333                 call_new(a_dst, &tmp.rmv, offset, w, h);
334                 checkasm_check(uint32_t, (uint32_t*)(c_buf + offset), stride,
335                                          (uint32_t*)(a_buf + offset), stride,
336                                          w_uint32, h, "dst");
337 
338                 bench_new(a_dst, &tmp.rmv, 0, w, h);
339             }
340         }
341     }
342     report("splat_mv");
343 }
344 
checkasm_check_refmvs(void)345 void checkasm_check_refmvs(void) {
346     Dav1dRefmvsDSPContext c;
347     dav1d_refmvs_dsp_init(&c);
348 
349     check_load_tmvs(&c);
350     check_save_tmvs(&c);
351     check_splat_mv(&c);
352 }
353