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 
check_intra_pred(Dav1dIntraPredDSPContext * const c)68 static void check_intra_pred(Dav1dIntraPredDSPContext *const c) {
69     PIXEL_RECT(c_dst, 64, 64);
70     PIXEL_RECT(a_dst, 64, 64);
71     ALIGN_STK_64(pixel, topleft_buf, 257,);
72     pixel *const topleft = topleft_buf + 128;
73 
74     declare_func(void, pixel *dst, ptrdiff_t stride, const pixel *topleft,
75                  int width, int height, int angle, int max_width, int max_height
76                  HIGHBD_DECL_SUFFIX);
77 
78     for (int mode = 0; mode < N_IMPL_INTRA_PRED_MODES; mode++) {
79         int bpc_min = BITDEPTH, bpc_max = BITDEPTH;
80         if (mode == FILTER_PRED && BITDEPTH == 16) {
81             bpc_min = 10;
82             bpc_max = 12;
83         }
84         for (int bpc = bpc_min; bpc <= bpc_max; bpc += 2)
85             for (int w = 4; w <= (mode == FILTER_PRED ? 32 : 64); w <<= 1)
86                 if (check_func(c->intra_pred[mode], "intra_pred_%s_w%d_%dbpc",
87                     intra_pred_mode_names[mode], w, bpc))
88                 {
89                     for (int h = imax(w / 4, 4); h <= imin(w * 4,
90                         (mode == FILTER_PRED ? 32 : 64)); h <<= 1)
91                     {
92                         const ptrdiff_t stride = c_dst_stride;
93 
94                         int a = 0, maxw = 0, maxh = 0;
95                         if (mode >= Z1_PRED && mode <= Z3_PRED) { /* angle */
96                             a = (90 * (mode - Z1_PRED) + z_angles[rnd() % 27]) |
97                                 (rnd() & 0x600);
98                             if (mode == Z2_PRED) {
99                                 maxw = rnd(), maxh = rnd();
100                                 maxw = 1 + (maxw & (maxw & 4096 ? 4095 : w - 1));
101                                 maxh = 1 + (maxh & (maxh & 4096 ? 4095 : h - 1));
102                             }
103                         } else if (mode == FILTER_PRED) /* filter_idx */
104                             a = (rnd() % 5) | (rnd() & ~511);
105 
106                         int bitdepth_max;
107                         if (bpc == 16)
108                             bitdepth_max = rnd() & 1 ? 0x3ff : 0xfff;
109                         else
110                             bitdepth_max = (1 << bpc) - 1;
111 
112                         for (int i = -h * 2; i <= w * 2; i++)
113                             topleft[i] = rnd() & bitdepth_max;
114 
115                         CLEAR_PIXEL_RECT(c_dst);
116                         CLEAR_PIXEL_RECT(a_dst);
117                         call_ref(c_dst, stride, topleft, w, h, a, maxw, maxh
118                                  HIGHBD_TAIL_SUFFIX);
119                         call_new(a_dst, stride, topleft, w, h, a, maxw, maxh
120                                  HIGHBD_TAIL_SUFFIX);
121                         if (checkasm_check_pixel_padded(c_dst, stride,
122                                                         a_dst, stride,
123                                                         w, h, "dst"))
124                         {
125                             if (mode == Z1_PRED || mode == Z3_PRED)
126                                 fprintf(stderr, "angle = %d (0x%03x)\n",
127                                         a & 0x1ff, a & 0x600);
128                             else if (mode == Z2_PRED)
129                                 fprintf(stderr, "angle = %d (0x%03x), "
130                                         "max_width = %d, max_height = %d\n",
131                                         a & 0x1ff, a & 0x600, maxw, maxh);
132                             else if (mode == FILTER_PRED)
133                                 fprintf(stderr, "filter_idx = %d\n", a & 0x1ff);
134                         }
135 
136                         bench_new(a_dst, stride, topleft, w, h, a, 128, 128
137                                   HIGHBD_TAIL_SUFFIX);
138                     }
139                 }
140     }
141     report("intra_pred");
142 }
143 
check_cfl_ac(Dav1dIntraPredDSPContext * const c)144 static void check_cfl_ac(Dav1dIntraPredDSPContext *const c) {
145     ALIGN_STK_64(int16_t, c_dst, 32 * 32,);
146     ALIGN_STK_64(int16_t, a_dst, 32 * 32,);
147     ALIGN_STK_64(pixel, luma, 32 * 32,);
148 
149     declare_func(void, int16_t *ac, const pixel *y, ptrdiff_t stride,
150                  int w_pad, int h_pad, int cw, int ch);
151 
152     for (int layout = 1; layout <= DAV1D_PIXEL_LAYOUT_I444; layout++) {
153         const int ss_ver = layout == DAV1D_PIXEL_LAYOUT_I420;
154         const int ss_hor = layout != DAV1D_PIXEL_LAYOUT_I444;
155         const int h_step = 2 >> ss_hor, v_step = 2 >> ss_ver;
156         for (int w = 4; w <= (32 >> ss_hor); w <<= 1)
157             if (check_func(c->cfl_ac[layout - 1], "cfl_ac_%s_w%d_%dbpc",
158                 cfl_ac_names[layout - 1], w, BITDEPTH))
159             {
160                 for (int h = imax(w / 4, 4);
161                      h <= imin(w * 4, (32 >> ss_ver)); h <<= 1)
162                 {
163                     const ptrdiff_t stride = 32 * sizeof(pixel);
164                     for (int w_pad = imax((w >> 2) - h_step, 0);
165                          w_pad >= 0; w_pad -= h_step)
166                     {
167                         for (int h_pad = imax((h >> 2) - v_step, 0);
168                              h_pad >= 0; h_pad -= v_step)
169                         {
170 #if BITDEPTH == 16
171                             const int bitdepth_max = rnd() & 1 ? 0x3ff : 0xfff;
172 #else
173                             const int bitdepth_max = 0xff;
174 #endif
175                             for (int y = 0; y < (h << ss_ver); y++)
176                                 for (int x = 0; x < (w << ss_hor); x++)
177                                     luma[y * 32 + x] = rnd() & bitdepth_max;
178 
179                             call_ref(c_dst, luma, stride, w_pad, h_pad, w, h);
180                             call_new(a_dst, luma, stride, w_pad, h_pad, w, h);
181                             checkasm_check(int16_t, c_dst, w * sizeof(*c_dst),
182                                                     a_dst, w * sizeof(*a_dst),
183                                                     w, h, "dst");
184                         }
185                     }
186 
187                     bench_new(a_dst, luma, stride, 0, 0, w, h);
188                 }
189             }
190     }
191     report("cfl_ac");
192 }
193 
check_cfl_pred(Dav1dIntraPredDSPContext * const c)194 static void check_cfl_pred(Dav1dIntraPredDSPContext *const c) {
195     ALIGN_STK_64(pixel, c_dst, 32 * 32,);
196     ALIGN_STK_64(pixel, a_dst, 32 * 32,);
197     ALIGN_STK_64(int16_t, ac, 32 * 32,);
198     ALIGN_STK_64(pixel, topleft_buf, 257,);
199     pixel *const topleft = topleft_buf + 128;
200 
201     declare_func(void, pixel *dst, ptrdiff_t stride, const pixel *topleft,
202                  int width, int height, const int16_t *ac, int alpha
203                  HIGHBD_DECL_SUFFIX);
204 
205     for (int mode = 0; mode <= DC_128_PRED; mode += 1 + 2 * !mode)
206         for (int w = 4; w <= 32; w <<= 1)
207             if (check_func(c->cfl_pred[mode], "cfl_pred_%s_w%d_%dbpc",
208                 cfl_pred_mode_names[mode], w, BITDEPTH))
209             {
210                 for (int h = imax(w / 4, 4); h <= imin(w * 4, 32); h <<= 1)
211                 {
212 #if BITDEPTH == 16
213                     const int bitdepth_max = rnd() & 1 ? 0x3ff : 0xfff;
214 #else
215                     const int bitdepth_max = 0xff;
216 #endif
217 
218                     const ptrdiff_t stride = w * sizeof(pixel);
219 
220                     int alpha = ((rnd() & 15) + 1) * (1 - (rnd() & 2));
221 
222                     for (int i = -h * 2; i <= w * 2; i++)
223                         topleft[i] = rnd() & bitdepth_max;
224 
225                     int luma_avg = w * h >> 1;
226                     for (int i = 0; i < w * h; i++)
227                         luma_avg += ac[i] = rnd() & (bitdepth_max << 3);
228                     luma_avg /= w * h;
229                     for (int i = 0; i < w * h; i++)
230                         ac[i] -= luma_avg;
231 
232                     call_ref(c_dst, stride, topleft, w, h, ac, alpha
233                              HIGHBD_TAIL_SUFFIX);
234                     call_new(a_dst, stride, topleft, w, h, ac, alpha
235                              HIGHBD_TAIL_SUFFIX);
236                     checkasm_check_pixel(c_dst, stride, a_dst, stride,
237                                          w, h, "dst");
238 
239                     bench_new(a_dst, stride, topleft, w, h, ac, alpha
240                               HIGHBD_TAIL_SUFFIX);
241                 }
242             }
243     report("cfl_pred");
244 }
245 
check_pal_pred(Dav1dIntraPredDSPContext * const c)246 static void check_pal_pred(Dav1dIntraPredDSPContext *const c) {
247     ALIGN_STK_64(pixel, c_dst, 64 * 64,);
248     ALIGN_STK_64(pixel, a_dst, 64 * 64,);
249     ALIGN_STK_64(uint8_t, idx, 64 * 64,);
250     ALIGN_STK_16(uint16_t, pal, 8,);
251 
252     declare_func(void, pixel *dst, ptrdiff_t stride, const uint16_t *pal,
253                  const uint8_t *idx, int w, int h);
254 
255     for (int w = 4; w <= 64; w <<= 1)
256         if (check_func(c->pal_pred, "pal_pred_w%d_%dbpc", w, BITDEPTH))
257             for (int h = imax(w / 4, 4); h <= imin(w * 4, 64); h <<= 1)
258             {
259 #if BITDEPTH == 16
260                 const int bitdepth_max = rnd() & 1 ? 0x3ff : 0xfff;
261 #else
262                 const int bitdepth_max = 0xff;
263 #endif
264                 const ptrdiff_t stride = w * sizeof(pixel);
265 
266                 for (int i = 0; i < 8; i++)
267                     pal[i] = rnd() & bitdepth_max;
268 
269                 for (int i = 0; i < w * h; i++)
270                     idx[i] = rnd() & 7;
271 
272                 call_ref(c_dst, stride, pal, idx, w, h);
273                 call_new(a_dst, stride, pal, idx, w, h);
274                 checkasm_check_pixel(c_dst, stride, a_dst, stride, w, h, "dst");
275 
276                 bench_new(a_dst, stride, pal, idx, w, h);
277             }
278     report("pal_pred");
279 }
280 
bitfn(checkasm_check_ipred)281 void bitfn(checkasm_check_ipred)(void) {
282     Dav1dIntraPredDSPContext c;
283     bitfn(dav1d_intra_pred_dsp_init)(&c);
284 
285     check_intra_pred(&c);
286     check_cfl_ac(&c);
287     check_cfl_pred(&c);
288     check_pal_pred(&c);
289 }
290