xref: /aosp_15_r20/external/libdav1d/tests/checkasm/ipred.c (revision c09093415860a1c2373dacd84c4fde00c507cdfd)
1 /*
2  * Copyright © 2018, VideoLAN and dav1d authors
3  * Copyright © 2018, Two Orioles, LLC
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright notice, this
10  *    list of conditions and the following disclaimer.
11  *
12  * 2. Redistributions in binary form must reproduce the above copyright notice,
13  *    this list of conditions and the following disclaimer in the documentation
14  *    and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
20  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #include "tests/checkasm/checkasm.h"
29 #include "src/ipred.h"
30 #include "src/levels.h"
31 
32 #include <stdio.h>
33 
34 static const char *const intra_pred_mode_names[N_IMPL_INTRA_PRED_MODES] = {
35     [DC_PRED]       = "dc",
36     [DC_128_PRED]   = "dc_128",
37     [TOP_DC_PRED]   = "dc_top",
38     [LEFT_DC_PRED]  = "dc_left",
39     [HOR_PRED]      = "h",
40     [VERT_PRED]     = "v",
41     [PAETH_PRED]    = "paeth",
42     [SMOOTH_PRED]   = "smooth",
43     [SMOOTH_V_PRED] = "smooth_v",
44     [SMOOTH_H_PRED] = "smooth_h",
45     [Z1_PRED]       = "z1",
46     [Z2_PRED]       = "z2",
47     [Z3_PRED]       = "z3",
48     [FILTER_PRED]   = "filter"
49 };
50 
51 static const char *const cfl_ac_names[3] = { "420", "422", "444" };
52 
53 static const char *const cfl_pred_mode_names[DC_128_PRED + 1] = {
54     [DC_PRED]       = "cfl",
55     [DC_128_PRED]   = "cfl_128",
56     [TOP_DC_PRED]   = "cfl_top",
57     [LEFT_DC_PRED]  = "cfl_left",
58 };
59 
60 static const uint8_t z_angles[27] = {
61      3,  6,  9,
62     14, 17, 20, 23, 26, 29, 32,
63     36, 39, 42, 45, 48, 51, 54,
64     58, 61, 64, 67, 70, 73, 76,
65     81, 84, 87
66 };
67 
68 /* Generate max_width/max_height values that covers all edge cases */
gen_z2_max_wh(const int sz)69 static int gen_z2_max_wh(const int sz) {
70     const int n = rnd();
71     if (n & (1 << 17)) /* edge block */
72         return (n & (sz - 1)) + 1;
73     if (n & (1 << 16)) /* max size, exceeds uint16_t */
74         return 65536;
75     return (n & 65535) + 1;
76 }
77 
check_intra_pred(Dav1dIntraPredDSPContext * const c)78 static void check_intra_pred(Dav1dIntraPredDSPContext *const c) {
79     PIXEL_RECT(c_dst, 64, 64);
80     PIXEL_RECT(a_dst, 64, 64);
81     ALIGN_STK_64(pixel, topleft_buf, 257,);
82     pixel *const topleft = topleft_buf + 128;
83 
84     declare_func(void, pixel *dst, ptrdiff_t stride, const pixel *topleft,
85                  int width, int height, int angle, int max_width, int max_height
86                  HIGHBD_DECL_SUFFIX);
87 
88     for (int mode = 0; mode < N_IMPL_INTRA_PRED_MODES; mode++) {
89         int bpc_min = BITDEPTH, bpc_max = BITDEPTH;
90         if (mode == FILTER_PRED && BITDEPTH == 16) {
91             bpc_min = 10;
92             bpc_max = 12;
93         }
94         for (int bpc = bpc_min; bpc <= bpc_max; bpc += 2)
95             for (int w = 4; w <= (mode == FILTER_PRED ? 32 : 64); w <<= 1)
96                 if (check_func(c->intra_pred[mode], "intra_pred_%s_w%d_%dbpc",
97                     intra_pred_mode_names[mode], w, bpc))
98                 {
99                     for (int h = imax(w / 4, 4); h <= imin(w * 4,
100                         (mode == FILTER_PRED ? 32 : 64)); h <<= 1)
101                     {
102                         const ptrdiff_t stride = c_dst_stride;
103                         int nb_iters = (mode >= Z1_PRED && mode <= Z3_PRED) ? 5 : 1;
104 
105                         for (int iter = 0; iter < nb_iters; iter++) {
106                             int a = 0, maxw = 0, maxh = 0;
107                             if (mode >= Z1_PRED && mode <= Z3_PRED) { /* angle */
108                                 a = (90 * (mode - Z1_PRED) + z_angles[rnd() % 27]) |
109                                     (rnd() & 0x600);
110                                 if (mode == Z2_PRED) {
111                                     maxw = gen_z2_max_wh(w);
112                                     maxh = gen_z2_max_wh(h);
113                                 }
114                             } else if (mode == FILTER_PRED) /* filter_idx */
115                                 a = (rnd() % 5) | (rnd() & ~511);
116 
117                             int bitdepth_max;
118                             if (bpc == 16)
119                                 bitdepth_max = rnd() & 1 ? 0x3ff : 0xfff;
120                             else
121                                 bitdepth_max = (1 << bpc) - 1;
122 
123                             for (int i = -h * 2; i <= w * 2; i++)
124                                 topleft[i] = rnd() & bitdepth_max;
125 
126                             CLEAR_PIXEL_RECT(c_dst);
127                             CLEAR_PIXEL_RECT(a_dst);
128                             call_ref(c_dst, stride, topleft, w, h, a, maxw, maxh
129                                      HIGHBD_TAIL_SUFFIX);
130                             call_new(a_dst, stride, topleft, w, h, a, maxw, maxh
131                                      HIGHBD_TAIL_SUFFIX);
132                             if (checkasm_check_pixel_padded(c_dst, stride,
133                                                             a_dst, stride,
134                                                             w, h, "dst"))
135                             {
136                                 if (mode == Z1_PRED || mode == Z3_PRED)
137                                     fprintf(stderr, "angle = %d (0x%03x)\n",
138                                             a & 0x1ff, a & 0x600);
139                                 else if (mode == Z2_PRED)
140                                     fprintf(stderr, "angle = %d (0x%03x), "
141                                             "max_width = %d, max_height = %d\n",
142                                             a & 0x1ff, a & 0x600, maxw, maxh);
143                                 else if (mode == FILTER_PRED)
144                                     fprintf(stderr, "filter_idx = %d\n", a & 0x1ff);
145                                 break;
146                             }
147 
148                             bench_new(a_dst, stride, topleft, w, h, a, 128, 128
149                                       HIGHBD_TAIL_SUFFIX);
150                         }
151                     }
152                 }
153     }
154     report("intra_pred");
155 }
156 
check_cfl_ac(Dav1dIntraPredDSPContext * const c)157 static void check_cfl_ac(Dav1dIntraPredDSPContext *const c) {
158     ALIGN_STK_64(int16_t, c_dst, 32 * 32,);
159     ALIGN_STK_64(int16_t, a_dst, 32 * 32,);
160     ALIGN_STK_64(pixel, luma, 32 * 32,);
161 
162     declare_func(void, int16_t *ac, const pixel *y, ptrdiff_t stride,
163                  int w_pad, int h_pad, int cw, int ch);
164 
165     for (int layout = 1; layout <= DAV1D_PIXEL_LAYOUT_I444; layout++) {
166         const int ss_ver = layout == DAV1D_PIXEL_LAYOUT_I420;
167         const int ss_hor = layout != DAV1D_PIXEL_LAYOUT_I444;
168         const int h_step = 2 >> ss_hor, v_step = 2 >> ss_ver;
169         for (int w = 4; w <= (32 >> ss_hor); w <<= 1)
170             if (check_func(c->cfl_ac[layout - 1], "cfl_ac_%s_w%d_%dbpc",
171                 cfl_ac_names[layout - 1], w, BITDEPTH))
172             {
173                 for (int h = imax(w / 4, 4);
174                      h <= imin(w * 4, (32 >> ss_ver)); h <<= 1)
175                 {
176                     const ptrdiff_t stride = 32 * sizeof(pixel);
177                     for (int w_pad = imax((w >> 2) - h_step, 0);
178                          w_pad >= 0; w_pad -= h_step)
179                     {
180                         for (int h_pad = imax((h >> 2) - v_step, 0);
181                              h_pad >= 0; h_pad -= v_step)
182                         {
183 #if BITDEPTH == 16
184                             const int bitdepth_max = rnd() & 1 ? 0x3ff : 0xfff;
185 #else
186                             const int bitdepth_max = 0xff;
187 #endif
188                             for (int y = 0; y < (h << ss_ver); y++)
189                                 for (int x = 0; x < (w << ss_hor); x++)
190                                     luma[y * 32 + x] = rnd() & bitdepth_max;
191 
192                             call_ref(c_dst, luma, stride, w_pad, h_pad, w, h);
193                             call_new(a_dst, luma, stride, w_pad, h_pad, w, h);
194                             checkasm_check(int16_t, c_dst, w * sizeof(*c_dst),
195                                                     a_dst, w * sizeof(*a_dst),
196                                                     w, h, "dst");
197                         }
198                     }
199 
200                     bench_new(a_dst, luma, stride, 0, 0, w, h);
201                 }
202             }
203     }
204     report("cfl_ac");
205 }
206 
check_cfl_pred(Dav1dIntraPredDSPContext * const c)207 static void check_cfl_pred(Dav1dIntraPredDSPContext *const c) {
208     PIXEL_RECT(c_dst, 32, 32);
209     PIXEL_RECT(a_dst, 32, 32);
210     ALIGN_STK_64(int16_t, ac, 32 * 32,);
211     ALIGN_STK_64(pixel, topleft_buf, 257,);
212     pixel *const topleft = topleft_buf + 128;
213 
214     declare_func(void, pixel *dst, ptrdiff_t stride, const pixel *topleft,
215                  int width, int height, const int16_t *ac, int alpha
216                  HIGHBD_DECL_SUFFIX);
217 
218     for (int mode = 0; mode <= DC_128_PRED; mode += 1 + 2 * !mode)
219         for (int w = 4; w <= 32; w <<= 1)
220             if (check_func(c->cfl_pred[mode], "cfl_pred_%s_w%d_%dbpc",
221                 cfl_pred_mode_names[mode], w, BITDEPTH))
222             {
223                 for (int h = imax(w / 4, 4); h <= imin(w * 4, 32); h <<= 1)
224                 {
225 #if BITDEPTH == 16
226                     const int bitdepth_max = rnd() & 1 ? 0x3ff : 0xfff;
227 #else
228                     const int bitdepth_max = 0xff;
229 #endif
230 
231                     int alpha = ((rnd() & 15) + 1) * (1 - (rnd() & 2));
232 
233                     for (int i = -h * 2; i <= w * 2; i++)
234                         topleft[i] = rnd() & bitdepth_max;
235 
236                     int luma_avg = w * h >> 1;
237                     for (int i = 0; i < w * h; i++)
238                         luma_avg += ac[i] = rnd() & (bitdepth_max << 3);
239                     luma_avg /= w * h;
240                     for (int i = 0; i < w * h; i++)
241                         ac[i] -= luma_avg;
242 
243                     CLEAR_PIXEL_RECT(c_dst);
244                     CLEAR_PIXEL_RECT(a_dst);
245 
246                     call_ref(c_dst, c_dst_stride, topleft, w, h, ac, alpha
247                              HIGHBD_TAIL_SUFFIX);
248                     call_new(a_dst, a_dst_stride, topleft, w, h, ac, alpha
249                              HIGHBD_TAIL_SUFFIX);
250                     checkasm_check_pixel_padded(c_dst, c_dst_stride, a_dst, a_dst_stride,
251                                                 w, h, "dst");
252 
253                     bench_new(a_dst, a_dst_stride, topleft, w, h, ac, alpha
254                               HIGHBD_TAIL_SUFFIX);
255                 }
256             }
257     report("cfl_pred");
258 }
259 
check_pal_pred(Dav1dIntraPredDSPContext * const c)260 static void check_pal_pred(Dav1dIntraPredDSPContext *const c) {
261     PIXEL_RECT(c_dst, 64, 64);
262     PIXEL_RECT(a_dst, 64, 64);
263     ALIGN_STK_64(uint8_t, idx, 32 * 64,);
264     ALIGN_STK_16(pixel, pal, 8,);
265 
266     declare_func(void, pixel *dst, ptrdiff_t stride, const pixel *pal,
267                  const uint8_t *idx, int w, int h);
268 
269     for (int w = 4; w <= 64; w <<= 1)
270         if (check_func(c->pal_pred, "pal_pred_w%d_%dbpc", w, BITDEPTH))
271             for (int h = imax(w / 4, 4); h <= imin(w * 4, 64); h <<= 1)
272             {
273 #if BITDEPTH == 16
274                 const int bitdepth_max = rnd() & 1 ? 0x3ff : 0xfff;
275 #else
276                 const int bitdepth_max = 0xff;
277 #endif
278 
279                 for (int i = 0; i < 8; i++)
280                     pal[i] = rnd() & bitdepth_max;
281 
282                 for (int i = 0; i < w * h / 2; i++)
283                     idx[i] = rnd() & 0x77;
284 
285                 CLEAR_PIXEL_RECT(c_dst);
286                 CLEAR_PIXEL_RECT(a_dst);
287 
288                 call_ref(c_dst, c_dst_stride, pal, idx, w, h);
289                 call_new(a_dst, a_dst_stride, pal, idx, w, h);
290                 checkasm_check_pixel_padded(c_dst, c_dst_stride,
291                                             a_dst, a_dst_stride, w, h, "dst");
292 
293                 bench_new(a_dst, a_dst_stride, pal, idx, w, h);
294             }
295     report("pal_pred");
296 }
297 
bitfn(checkasm_check_ipred)298 void bitfn(checkasm_check_ipred)(void) {
299     Dav1dIntraPredDSPContext c;
300     bitfn(dav1d_intra_pred_dsp_init)(&c);
301 
302     check_intra_pred(&c);
303     check_cfl_ac(&c);
304     check_cfl_pred(&c);
305     check_pal_pred(&c);
306 }
307