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 #include <stdio.h>
32
33 #include "common/dump.h"
34
35 #include "src/levels.h"
36 #include "src/cdef.h"
37
to_binary(int x)38 static int to_binary(int x) { /* 0-15 -> 0000-1111 */
39 return (x & 1) + 5 * (x & 2) + 25 * (x & 4) + 125 * (x & 8);
40 }
41
init_tmp(pixel * buf,int n,const int bitdepth_max)42 static void init_tmp(pixel *buf, int n, const int bitdepth_max) {
43 const int fill_type = rnd() & 7;
44 if (fill_type == 0)
45 while (n--) /* check for cdef_filter underflows */
46 *buf++ = rnd() & 1;
47 else if (fill_type == 1)
48 while (n--) /* check for cdef_filter overflows */
49 *buf++ = bitdepth_max - (rnd() & 1);
50 else
51 while (n--)
52 *buf++ = rnd() & bitdepth_max;
53 }
54
check_cdef_filter(const cdef_fn fn,const int w,const int h)55 static void check_cdef_filter(const cdef_fn fn, const int w, const int h) {
56 ALIGN_STK_64(pixel, c_src, 16 * 10 + 16, ), *const c_dst = c_src + 8;
57 ALIGN_STK_64(pixel, a_src, 16 * 10 + 16, ), *const a_dst = a_src + 8;
58 ALIGN_STK_64(pixel, top_buf, 16 * 2 + 16, ), *const top = top_buf + 8;
59 ALIGN_STK_64(pixel, bot_buf, 16 * 2 + 16, ), *const bot = bot_buf + 8;
60 ALIGN_STK_16(pixel, left, 8,[2]);
61 const ptrdiff_t stride = 16 * sizeof(pixel);
62
63 declare_func(void, pixel *dst, ptrdiff_t dst_stride, const pixel (*left)[2],
64 const pixel *top, const pixel *bot, int pri_strength,
65 int sec_strength, int dir, int damping,
66 enum CdefEdgeFlags edges HIGHBD_DECL_SUFFIX);
67
68 for (int s = 0x1; s <= 0x3; s++) {
69 if (check_func(fn, "cdef_filter_%dx%d_%02d_%dbpc", w, h, to_binary(s), BITDEPTH)) {
70 for (int dir = 0; dir < 8; dir++) {
71 for (enum CdefEdgeFlags edges = 0x0; edges <= 0xf; edges++) {
72 #if BITDEPTH == 16
73 const int bitdepth_max = rnd() & 1 ? 0x3ff : 0xfff;
74 #else
75 const int bitdepth_max = 0xff;
76 #endif
77 const int bitdepth_min_8 = bitdepth_from_max(bitdepth_max) - 8;
78
79 init_tmp(c_src, 16 * 10 + 16, bitdepth_max);
80 init_tmp(top_buf, 16 * 2 + 16, bitdepth_max);
81 init_tmp(bot_buf, 16 * 2 + 16, bitdepth_max);
82 init_tmp((pixel *) left, 8 * 2, bitdepth_max);
83 memcpy(a_src, c_src, (16 * 10 + 16) * sizeof(pixel));
84
85 const int pri_strength = s & 2 ? (1 + (rnd() % 15)) << bitdepth_min_8 : 0;
86 const int sec_strength = s & 1 ? 1 << ((rnd() % 3) + bitdepth_min_8) : 0;
87 const int damping = 3 + (rnd() & 3) + bitdepth_min_8 - (w == 4 || (rnd() & 1));
88 call_ref(c_dst, stride, left, top, bot, pri_strength, sec_strength,
89 dir, damping, edges HIGHBD_TAIL_SUFFIX);
90 call_new(a_dst, stride, left, top, bot, pri_strength, sec_strength,
91 dir, damping, edges HIGHBD_TAIL_SUFFIX);
92 if (checkasm_check_pixel(c_dst, stride, a_dst, stride, w, h, "dst")) {
93 fprintf(stderr, "strength = %d:%d, dir = %d, damping = %d, edges = %04d\n",
94 pri_strength, sec_strength, dir, damping, to_binary(edges));
95 return;
96 }
97 if (dir == 7 && (edges == 0x5 || edges == 0xa || edges == 0xf))
98 bench_new(alternate(c_dst, a_dst), stride, left, top, bot, pri_strength,
99 sec_strength, dir, damping, edges HIGHBD_TAIL_SUFFIX);
100 }
101 }
102 }
103 }
104 }
105
check_cdef_direction(const cdef_dir_fn fn)106 static void check_cdef_direction(const cdef_dir_fn fn) {
107 ALIGN_STK_64(pixel, src, 8 * 8,);
108
109 declare_func(int, const pixel *src, ptrdiff_t dst_stride, unsigned *var
110 HIGHBD_DECL_SUFFIX);
111
112 if (check_func(fn, "cdef_dir_%dbpc", BITDEPTH)) {
113 unsigned c_var, a_var;
114 #if BITDEPTH == 16
115 const int bitdepth_max = rnd() & 1 ? 0x3ff : 0xfff;
116 #else
117 const int bitdepth_max = 0xff;
118 #endif
119 init_tmp(src, 64, bitdepth_max);
120
121 const int c_dir = call_ref(src, 8 * sizeof(pixel), &c_var HIGHBD_TAIL_SUFFIX);
122 const int a_dir = call_new(src, 8 * sizeof(pixel), &a_var HIGHBD_TAIL_SUFFIX);
123 if (c_var != a_var || c_dir != a_dir) {
124 if (fail()) {
125 hex_fdump(stderr, src, 8 * sizeof(pixel), 8, 8, "src");
126 fprintf(stderr, "c_dir %d a_dir %d\n", c_dir, a_dir);
127 }
128 }
129 bench_new(src, 8 * sizeof(pixel), &a_var HIGHBD_TAIL_SUFFIX);
130 }
131 report("cdef_dir");
132 }
133
bitfn(checkasm_check_cdef)134 void bitfn(checkasm_check_cdef)(void) {
135 Dav1dCdefDSPContext c;
136 bitfn(dav1d_cdef_dsp_init)(&c);
137
138 check_cdef_direction(c.dir);
139
140 check_cdef_filter(c.fb[0], 8, 8);
141 check_cdef_filter(c.fb[1], 4, 8);
142 check_cdef_filter(c.fb[2], 4, 4);
143 report("cdef_filter");
144 }
145