xref: /aosp_15_r20/external/libdav1d/tests/checkasm/looprestoration.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 
30 #include <stdio.h>
31 #include <string.h>
32 
33 #include "src/levels.h"
34 #include "src/looprestoration.h"
35 #include "src/tables.h"
36 
to_binary(int x)37 static int to_binary(int x) { /* 0-15 -> 0000-1111 */
38     return (x & 1) + 5 * (x & 2) + 25 * (x & 4) + 125 * (x & 8);
39 }
40 
init_tmp(pixel * buf,const ptrdiff_t stride,const int w,const int h,const int bitdepth_max)41 static void init_tmp(pixel *buf, const ptrdiff_t stride,
42                      const int w, const int h, const int bitdepth_max)
43 {
44     const int noise_mask = bitdepth_max >> 4;
45     const int x_off = rnd() & 7, y_off = rnd() & 7;
46 
47     for (int y = 0; y < h; y++) {
48         for (int x = 0; x < w; x++) {
49             buf[x] = (((x + x_off) ^ (y + y_off)) & 8 ? bitdepth_max : 0) ^
50                      (rnd() & noise_mask);
51         }
52         buf += PXSTRIDE(stride);
53     }
54 }
55 
check_wiener(Dav1dLoopRestorationDSPContext * const c,const int bpc)56 static void check_wiener(Dav1dLoopRestorationDSPContext *const c, const int bpc) {
57     ALIGN_STK_64(pixel, c_src, 448 * 64 + 64,), *const c_dst = c_src + 64;
58     ALIGN_STK_64(pixel, a_src, 448 * 64 + 64,), *const a_dst = a_src + 64;
59     ALIGN_STK_64(pixel, edge_buf, 448 * 8 + 64,), *const h_edge = edge_buf + 64;
60     pixel left[64][4];
61     LooprestorationParams params;
62     int16_t (*const filter)[8] = params.filter;
63 
64     declare_func(void, pixel *dst, ptrdiff_t dst_stride,
65                  const pixel (*const left)[4],
66                  const pixel *lpf, int w, int h,
67                  const LooprestorationParams *params,
68                  enum LrEdgeFlags edges HIGHBD_DECL_SUFFIX);
69 
70     for (int t = 0; t < 2; t++) {
71         if (check_func(c->wiener[t], "wiener_%dtap_%dbpc", t ? 5 : 7, bpc)) {
72             filter[0][0] = filter[0][6] = t ? 0 : (rnd() & 15) - 5;
73             filter[0][1] = filter[0][5] = (rnd() & 31) - 23;
74             filter[0][2] = filter[0][4] = (rnd() & 63) - 17;
75             filter[0][3] = -(filter[0][0] + filter[0][1] + filter[0][2]) * 2;
76 #if BITDEPTH != 8
77             filter[0][3] += 128;
78 #endif
79 
80             filter[1][0] = filter[1][6] = t ? 0 : (rnd() & 15) - 5;
81             filter[1][1] = filter[1][5] = (rnd() & 31) - 23;
82             filter[1][2] = filter[1][4] = (rnd() & 63) - 17;
83             filter[1][3] = 128 - (filter[1][0] + filter[1][1] + filter[1][2]) * 2;
84 
85             const int base_w = 1 + (rnd() % 384);
86             const int base_h = 1 + (rnd() & 63);
87             const int bitdepth_max = (1 << bpc) - 1;
88 
89             init_tmp(c_src, 448 * sizeof(pixel), 448, 64, bitdepth_max);
90             init_tmp(edge_buf, 448 * sizeof(pixel), 448, 8, bitdepth_max);
91             init_tmp((pixel *) left, 4 * sizeof(pixel), 4, 64, bitdepth_max);
92 
93             for (enum LrEdgeFlags edges = 0; edges <= 0xf; edges++) {
94                 const int w = edges & LR_HAVE_RIGHT ? 256 : base_w;
95                 const int h = edges & LR_HAVE_BOTTOM ? 64 : base_h;
96 
97                 memcpy(a_src, c_src, 448 * 64 * sizeof(pixel));
98 
99                 call_ref(c_dst, 448 * sizeof(pixel), left,
100                          h_edge, w, h, &params, edges HIGHBD_TAIL_SUFFIX);
101                 call_new(a_dst, 448 * sizeof(pixel), left,
102                          h_edge, w, h, &params, edges HIGHBD_TAIL_SUFFIX);
103                 if (checkasm_check_pixel(c_dst, 448 * sizeof(pixel),
104                                          a_dst, 448 * sizeof(pixel),
105                                          w, h, "dst"))
106                 {
107                     fprintf(stderr, "size = %dx%d, edges = %04d\n",
108                             w, h, to_binary(edges));
109                     break;
110                 }
111             }
112             bench_new(alternate(c_dst, a_dst), 448 * sizeof(pixel), left,
113                       h_edge, 256, 64, &params, 0xf HIGHBD_TAIL_SUFFIX);
114         }
115     }
116 }
117 
check_sgr(Dav1dLoopRestorationDSPContext * const c,const int bpc)118 static void check_sgr(Dav1dLoopRestorationDSPContext *const c, const int bpc) {
119     ALIGN_STK_64(pixel, c_src, 448 * 64 + 64,), *const c_dst = c_src + 64;
120     ALIGN_STK_64(pixel, a_src, 448 * 64 + 64,), *const a_dst = a_src + 64;
121     ALIGN_STK_64(pixel, edge_buf, 448 * 8 + 64,), *const h_edge = edge_buf + 64;
122     pixel left[64][4];
123     LooprestorationParams params;
124 
125     declare_func(void, pixel *dst, ptrdiff_t dst_stride,
126                  const pixel (*const left)[4],
127                  const pixel *lpf, int w, int h,
128                  const LooprestorationParams *params,
129                  enum LrEdgeFlags edges HIGHBD_DECL_SUFFIX);
130 
131     static const struct { char name[4]; uint8_t idx; } sgr_data[3] = {
132         { "5x5", 14 },
133         { "3x3", 10 },
134         { "mix",  0 },
135     };
136 
137     for (int i = 0; i < 3; i++) {
138         if (check_func(c->sgr[i], "sgr_%s_%dbpc", sgr_data[i].name, bpc)) {
139             const uint16_t *const sgr_params = dav1d_sgr_params[sgr_data[i].idx];
140             params.sgr.s0 = sgr_params[0];
141             params.sgr.s1 = sgr_params[1];
142             params.sgr.w0 = sgr_params[0] ? (rnd() & 127) - 96 : 0;
143             params.sgr.w1 = (sgr_params[1] ? 160 - (rnd() & 127) : 33) - params.sgr.w0;
144 
145             const int base_w = 1 + (rnd() % 384);
146             const int base_h = 1 + (rnd() & 63);
147             const int bitdepth_max = (1 << bpc) - 1;
148 
149             init_tmp(c_src, 448 * sizeof(pixel), 448, 64, bitdepth_max);
150             init_tmp(edge_buf, 448 * sizeof(pixel), 448, 8, bitdepth_max);
151             init_tmp((pixel *) left, 4 * sizeof(pixel), 4, 64, bitdepth_max);
152 
153             for (enum LrEdgeFlags edges = 0; edges <= 0xf; edges++) {
154                 const int w = edges & LR_HAVE_RIGHT ? 256 : base_w;
155                 const int h = edges & LR_HAVE_BOTTOM ? 64 : base_h;
156 
157                 memcpy(a_src, c_src, 448 * 64 * sizeof(pixel));
158 
159                 call_ref(c_dst, 448 * sizeof(pixel), left, h_edge,
160                          w, h, &params, edges HIGHBD_TAIL_SUFFIX);
161                 call_new(a_dst, 448 * sizeof(pixel), left, h_edge,
162                          w, h, &params, edges HIGHBD_TAIL_SUFFIX);
163                 if (checkasm_check_pixel(c_dst, 448 * sizeof(pixel),
164                                          a_dst, 448 * sizeof(pixel),
165                                          w, h, "dst"))
166                 {
167                     fprintf(stderr, "size = %dx%d, edges = %04d\n",
168                             w, h, to_binary(edges));
169                     break;
170                 }
171             }
172             bench_new(alternate(c_dst, a_dst), 448 * sizeof(pixel), left,
173                       h_edge, 256, 64, &params, 0xf HIGHBD_TAIL_SUFFIX);
174         }
175     }
176 }
177 
bitfn(checkasm_check_looprestoration)178 void bitfn(checkasm_check_looprestoration)(void) {
179 #if BITDEPTH == 16
180     const int bpc_min = 10, bpc_max = 12;
181 #else
182     const int bpc_min = 8, bpc_max = 8;
183 #endif
184     for (int bpc = bpc_min; bpc <= bpc_max; bpc += 2) {
185         Dav1dLoopRestorationDSPContext c;
186         bitfn(dav1d_loop_restoration_dsp_init)(&c, bpc);
187         check_wiener(&c, bpc);
188     }
189     report("wiener");
190     for (int bpc = bpc_min; bpc <= bpc_max; bpc += 2) {
191         Dav1dLoopRestorationDSPContext c;
192         bitfn(dav1d_loop_restoration_dsp_init)(&c, bpc);
193         check_sgr(&c, bpc);
194     }
195     report("sgr");
196 }
197