• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <string.h>
31 
32 #include "src/levels.h"
33 #include "src/loopfilter.h"
34 
init_lpf_border(pixel * const dst,const ptrdiff_t stride,int E,int I,const int bitdepth_max)35 static void init_lpf_border(pixel *const dst, const ptrdiff_t stride,
36                             int E, int I, const int bitdepth_max)
37 {
38     const int bitdepth_min_8 = bitdepth_from_max(bitdepth_max) - 8;
39     const int F = 1 << bitdepth_min_8;
40     E <<= bitdepth_min_8;
41     I <<= bitdepth_min_8;
42 
43     const int filter_type = rnd() % 4;
44     const int edge_diff = rnd() % ((E + 2) * 4) - 2 * (E + 2);
45     switch (filter_type) {
46     case 0: // random, unfiltered
47         for (int i = -8; i < 8; i++)
48             dst[i * stride] = rnd() & bitdepth_max;
49         break;
50     case 1: // long flat
51         dst[-8 * stride] = rnd() & bitdepth_max;
52         dst[+7 * stride] = rnd() & bitdepth_max;
53         dst[+0 * stride] = rnd() & bitdepth_max;
54         dst[-1 * stride] = iclip_pixel(dst[+0 * stride] + edge_diff);
55         for (int i = 1; i < 7; i++) {
56             dst[-(1 + i) * stride] = iclip_pixel(dst[-1 * stride] +
57                                                  rnd() % (2 * (F + 1)) - (F + 1));
58             dst[+(0 + i) * stride] = iclip_pixel(dst[+0 * stride] +
59                                                  rnd() % (2 * (F + 1)) - (F + 1));
60         }
61         break;
62     case 2: // short flat
63         for (int i = 4; i < 8; i++) {
64             dst[-(1 + i) * stride] = rnd() & bitdepth_max;
65             dst[+(0 + i) * stride] = rnd() & bitdepth_max;
66         }
67         dst[+0 * stride] = rnd() & bitdepth_max;
68         dst[-1 * stride] = iclip_pixel(dst[+0 * stride] + edge_diff);
69         for (int i = 1; i < 4; i++) {
70             dst[-(1 + i) * stride] = iclip_pixel(dst[-1 * stride] +
71                                                  rnd() % (2 * (F + 1)) - (F + 1));
72             dst[+(0 + i) * stride] = iclip_pixel(dst[+0 * stride] +
73                                                  rnd() % (2 * (F + 1)) - (F + 1));
74         }
75         break;
76     case 3: // normal or hev
77         for (int i = 4; i < 8; i++) {
78             dst[-(1 + i) * stride] = rnd() & bitdepth_max;
79             dst[+(0 + i) * stride] = rnd() & bitdepth_max;
80         }
81         dst[+0 * stride] = rnd() & bitdepth_max;
82         dst[-1 * stride] = iclip_pixel(dst[+0 * stride] + edge_diff);
83         for (int i = 1; i < 4; i++) {
84             dst[-(1 + i) * stride] = iclip_pixel(dst[-(0 + i) * stride] +
85                                                  rnd() % (2 * (I + 1)) - (I + 1));
86             dst[+(0 + i) * stride] = iclip_pixel(dst[+(i - 1) * stride] +
87                                                  rnd() % (2 * (I + 1)) - (I + 1));
88         }
89         break;
90     }
91 }
92 
check_lpf_sb(loopfilter_sb_fn fn,const char * const name,const int n_blks,const int lf_idx,const int is_chroma,const int dir)93 static void check_lpf_sb(loopfilter_sb_fn fn, const char *const name,
94                          const int n_blks, const int lf_idx,
95                          const int is_chroma, const int dir)
96 {
97     ALIGN_STK_64(pixel, c_dst_mem, 128 * 16,);
98     ALIGN_STK_64(pixel, a_dst_mem, 128 * 16,);
99 
100     declare_func(void, pixel *dst, ptrdiff_t dst_stride, const uint32_t *mask,
101                  const uint8_t (*l)[4], ptrdiff_t b4_stride,
102                  const Av1FilterLUT *lut, int w HIGHBD_DECL_SUFFIX);
103 
104     pixel *a_dst, *c_dst;
105     ptrdiff_t stride, b4_stride;
106     int w, h;
107     if (dir) {
108         a_dst = a_dst_mem + n_blks * 4 * 8;
109         c_dst = c_dst_mem + n_blks * 4 * 8;
110         w = n_blks * 4;
111         h = 16;
112         b4_stride = 32;
113     } else {
114         a_dst = a_dst_mem + 8;
115         c_dst = c_dst_mem + 8;
116         w = 16;
117         h = n_blks * 4;
118         b4_stride = 2;
119     }
120     stride = w * sizeof(pixel);
121 
122     Av1FilterLUT lut;
123     const int sharp = rnd() & 7;
124     for (int level = 0; level < 64; level++) {
125         int limit = level;
126 
127         if (sharp > 0) {
128             limit >>= (sharp + 3) >> 2;
129             limit = imin(limit, 9 - sharp);
130         }
131         limit = imax(limit, 1);
132 
133         lut.i[level] = limit;
134         lut.e[level] = 2 * (level + 2) + limit;
135     }
136     lut.sharp[0] = (sharp + 3) >> 2;
137     lut.sharp[1] = sharp ? 9 - sharp : 0xff;
138 
139     const int n_strengths = is_chroma ? 2 : 3;
140     for (int i = 0; i < n_strengths; i++) {
141         if (check_func(fn, "%s_w%d_%dbpc", name,
142                        is_chroma ? 4 + 2 * i : 4 << i, BITDEPTH))
143         {
144             uint32_t vmask[4] = { 0 };
145             uint8_t l[32 * 2][4];
146 
147             for (int j = 0; j < n_blks; j++) {
148                 const int idx = rnd() % (i + 2);
149                 if (idx) vmask[idx - 1] |= 1U << j;
150                 if (dir) {
151                     l[j][lf_idx] = rnd() & 63;
152                     l[j + 32][lf_idx] = rnd() & 63;
153                 } else {
154                     l[j * 2][lf_idx] = rnd() & 63;
155                     l[j * 2 + 1][lf_idx] = rnd() & 63;
156                 }
157             }
158 #if BITDEPTH == 16
159             const int bitdepth_max = rnd() & 1 ? 0x3ff : 0xfff;
160 #else
161             const int bitdepth_max = 0xff;
162 #endif
163 
164             for (int i = 0; i < 4 * n_blks; i++) {
165                 const int x = i >> 2;
166                 int L;
167                 if (dir) {
168                     L = l[32 + x][lf_idx] ? l[32 + x][lf_idx] : l[x][lf_idx];
169                 } else {
170                     L = l[2 * x + 1][lf_idx] ? l[2 * x + 1][lf_idx] : l[2 * x][lf_idx];
171                 }
172                 init_lpf_border(c_dst + i * (dir ? 1 : 16), dir ? n_blks * 4 : 1,
173                                 lut.e[L], lut.i[L], bitdepth_max);
174             }
175             memcpy(a_dst_mem, c_dst_mem, 128 * sizeof(pixel) * 16);
176 
177             call_ref(c_dst, stride, vmask,
178                      (const uint8_t(*)[4]) &l[dir ? 32 : 1][lf_idx],
179                      b4_stride, &lut, n_blks HIGHBD_TAIL_SUFFIX);
180             call_new(a_dst, stride, vmask,
181                      (const uint8_t(*)[4]) &l[dir ? 32 : 1][lf_idx],
182                      b4_stride, &lut, n_blks HIGHBD_TAIL_SUFFIX);
183 
184             checkasm_check_pixel(c_dst_mem, stride, a_dst_mem, stride,
185                                  w, h, "dst");
186             bench_new(alternate(c_dst, a_dst), stride, vmask,
187                       (const uint8_t(*)[4]) &l[dir ? 32 : 1][lf_idx],
188                       b4_stride, &lut, n_blks HIGHBD_TAIL_SUFFIX);
189         }
190     }
191     report(name);
192 }
193 
bitfn(checkasm_check_loopfilter)194 void bitfn(checkasm_check_loopfilter)(void) {
195     Dav1dLoopFilterDSPContext c;
196 
197     bitfn(dav1d_loop_filter_dsp_init)(&c);
198 
199     check_lpf_sb(c.loop_filter_sb[0][0], "lpf_h_sb_y", 32, 0, 0, 0);
200     check_lpf_sb(c.loop_filter_sb[0][1], "lpf_v_sb_y", 32, 1, 0, 1);
201     check_lpf_sb(c.loop_filter_sb[1][0], "lpf_h_sb_uv", 16, 2, 1, 0);
202     check_lpf_sb(c.loop_filter_sb[1][1], "lpf_v_sb_uv", 16, 2, 1, 1);
203 }
204