/src/aom/av1/common/x86/wiener_convolve_avx2.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright (c) 2018, Alliance for Open Media. All rights reserved. |
3 | | * |
4 | | * This source code is subject to the terms of the BSD 2 Clause License and |
5 | | * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License |
6 | | * was not distributed with this source code in the LICENSE file, you can |
7 | | * obtain it at www.aomedia.org/license/software. If the Alliance for Open |
8 | | * Media Patent License 1.0 was not distributed with this source code in the |
9 | | * PATENTS file, you can obtain it at www.aomedia.org/license/patent. |
10 | | */ |
11 | | |
12 | | #include <immintrin.h> |
13 | | #include <assert.h> |
14 | | |
15 | | #include "config/av1_rtcd.h" |
16 | | |
17 | | #include "av1/common/convolve.h" |
18 | | #include "aom_dsp/aom_dsp_common.h" |
19 | | #include "aom_dsp/aom_filter.h" |
20 | | #include "aom_dsp/x86/convolve_avx2.h" |
21 | | #include "aom_dsp/x86/synonyms.h" |
22 | | #include "aom_dsp/x86/synonyms_avx2.h" |
23 | | |
24 | | // 128-bit xmmwords are written as [ ... ] with the MSB on the left. |
25 | | // 256-bit ymmwords are written as two xmmwords, [ ... ][ ... ] with the MSB |
26 | | // on the left. |
27 | | // A row of, say, 8-bit pixels with values p0, p1, p2, ..., p30, p31 will be |
28 | | // loaded and stored as [ p31 ... p17 p16 ][ p15 ... p1 p0 ]. |
29 | | |
30 | | // Exploiting the range of wiener filter coefficients, |
31 | | // horizontal filtering can be done in 16 bit intermediate precision. |
32 | | // The details are as follows : |
33 | | // Consider the horizontal wiener filter coefficients of the following form : |
34 | | // [C0, C1, C2, 2^(FILTER_BITS) -2 * (C0 + C1 + C2), C2, C1, C0] |
35 | | // Subtracting 2^(FILTER_BITS) from the centre tap we get the following : |
36 | | // [C0, C1, C2, -2 * (C0 + C1 + C2), C2, C1, C0] |
37 | | // The sum of the product "C0 * p0 + C1 * p1 + C2 * p2 -2 * (C0 + C1 + C2) * p3 |
38 | | // + C2 * p4 + C1 * p5 + C0 * p6" would be in the range of signed 16 bit |
39 | | // precision. Finally, after rounding the above result by round_0, we multiply |
40 | | // the centre pixel by 2^(FILTER_BITS - round_0) and add it to get the |
41 | | // horizontal filter output. |
42 | | |
43 | | void av1_wiener_convolve_add_src_avx2(const uint8_t *src, ptrdiff_t src_stride, |
44 | | uint8_t *dst, ptrdiff_t dst_stride, |
45 | | const int16_t *filter_x, int x_step_q4, |
46 | | const int16_t *filter_y, int y_step_q4, |
47 | | int w, int h, |
48 | 304k | const WienerConvolveParams *conv_params) { |
49 | 304k | const int bd = 8; |
50 | 304k | assert(x_step_q4 == 16 && y_step_q4 == 16); |
51 | 304k | assert(!(w & 7)); |
52 | 304k | (void)x_step_q4; |
53 | 304k | (void)y_step_q4; |
54 | | |
55 | 304k | DECLARE_ALIGNED(32, int16_t, im_block[(MAX_SB_SIZE + SUBPEL_TAPS) * 8]); |
56 | 304k | int im_h = h + SUBPEL_TAPS - 2; |
57 | 304k | int im_stride = 8; |
58 | 304k | memset(im_block + (im_h * im_stride), 0, MAX_SB_SIZE); |
59 | 304k | int i, j; |
60 | 304k | const int center_tap = (SUBPEL_TAPS - 1) / 2; |
61 | 304k | const uint8_t *const src_ptr = src - center_tap * src_stride - center_tap; |
62 | | |
63 | 304k | __m256i filt[4], coeffs_h[4], coeffs_v[4], filt_center; |
64 | | |
65 | 304k | assert(conv_params->round_0 > 0); |
66 | | |
67 | 304k | filt[0] = _mm256_load_si256((__m256i const *)filt1_global_avx2); |
68 | 304k | filt[1] = _mm256_load_si256((__m256i const *)filt2_global_avx2); |
69 | 304k | filt[2] = _mm256_load_si256((__m256i const *)filt3_global_avx2); |
70 | 304k | filt[3] = _mm256_load_si256((__m256i const *)filt4_global_avx2); |
71 | | |
72 | 304k | filt_center = _mm256_load_si256((__m256i const *)filt_center_global_avx2); |
73 | | |
74 | 304k | const __m128i coeffs_x = _mm_loadu_si128((__m128i *)filter_x); |
75 | 304k | const __m256i filter_coeffs_x = _mm256_broadcastsi128_si256(coeffs_x); |
76 | | |
77 | | // coeffs 0 1 0 1 0 1 0 1 |
78 | 304k | coeffs_h[0] = |
79 | 304k | _mm256_shuffle_epi8(filter_coeffs_x, _mm256_set1_epi16(0x0200u)); |
80 | | // coeffs 2 3 2 3 2 3 2 3 |
81 | 304k | coeffs_h[1] = |
82 | 304k | _mm256_shuffle_epi8(filter_coeffs_x, _mm256_set1_epi16(0x0604u)); |
83 | | // coeffs 4 5 4 5 4 5 4 5 |
84 | 304k | coeffs_h[2] = |
85 | 304k | _mm256_shuffle_epi8(filter_coeffs_x, _mm256_set1_epi16(0x0a08u)); |
86 | | // coeffs 6 7 6 7 6 7 6 7 |
87 | 304k | coeffs_h[3] = |
88 | 304k | _mm256_shuffle_epi8(filter_coeffs_x, _mm256_set1_epi16(0x0e0cu)); |
89 | | |
90 | 304k | const __m256i round_const_h = |
91 | 304k | _mm256_set1_epi16((1 << (conv_params->round_0 - 1))); |
92 | 304k | const __m256i round_const_horz = |
93 | 304k | _mm256_set1_epi16((1 << (bd + FILTER_BITS - conv_params->round_0 - 1))); |
94 | 304k | const __m256i clamp_low = _mm256_setzero_si256(); |
95 | 304k | const __m256i clamp_high = |
96 | 304k | _mm256_set1_epi16(WIENER_CLAMP_LIMIT(conv_params->round_0, bd) - 1); |
97 | 304k | const __m128i round_shift_h = _mm_cvtsi32_si128(conv_params->round_0); |
98 | | |
99 | | // Add an offset to account for the "add_src" part of the convolve function. |
100 | 304k | const __m128i zero_128 = _mm_setzero_si128(); |
101 | 304k | const __m128i offset_0 = _mm_insert_epi16(zero_128, 1 << FILTER_BITS, 3); |
102 | 304k | const __m128i coeffs_y = _mm_add_epi16(xx_loadu_128(filter_y), offset_0); |
103 | | |
104 | 304k | const __m256i filter_coeffs_y = _mm256_broadcastsi128_si256(coeffs_y); |
105 | | |
106 | | // coeffs 0 1 0 1 0 1 0 1 |
107 | 304k | coeffs_v[0] = _mm256_shuffle_epi32(filter_coeffs_y, 0x00); |
108 | | // coeffs 2 3 2 3 2 3 2 3 |
109 | 304k | coeffs_v[1] = _mm256_shuffle_epi32(filter_coeffs_y, 0x55); |
110 | | // coeffs 4 5 4 5 4 5 4 5 |
111 | 304k | coeffs_v[2] = _mm256_shuffle_epi32(filter_coeffs_y, 0xaa); |
112 | | // coeffs 6 7 6 7 6 7 6 7 |
113 | 304k | coeffs_v[3] = _mm256_shuffle_epi32(filter_coeffs_y, 0xff); |
114 | | |
115 | 304k | const __m256i round_const_v = |
116 | 304k | _mm256_set1_epi32((1 << (conv_params->round_1 - 1)) - |
117 | 304k | (1 << (bd + conv_params->round_1 - 1))); |
118 | 304k | const __m128i round_shift_v = _mm_cvtsi32_si128(conv_params->round_1); |
119 | | |
120 | 1.57M | for (j = 0; j < w; j += 8) { |
121 | 16.8M | for (i = 0; i < im_h; i += 2) { |
122 | 15.5M | __m256i data = _mm256_castsi128_si256( |
123 | 15.5M | _mm_loadu_si128((__m128i *)&src_ptr[(i * src_stride) + j])); |
124 | | |
125 | | // Load the next line |
126 | 15.5M | if (i + 1 < im_h) |
127 | 16.3M | data = _mm256_inserti128_si256( |
128 | 15.5M | data, |
129 | 15.5M | _mm_loadu_si128( |
130 | 15.5M | (__m128i *)&src_ptr[(i * src_stride) + j + src_stride]), |
131 | 15.5M | 1); |
132 | | |
133 | 15.5M | __m256i res = convolve_lowbd_x(data, coeffs_h, filt); |
134 | | |
135 | 15.5M | res = |
136 | 15.5M | _mm256_sra_epi16(_mm256_add_epi16(res, round_const_h), round_shift_h); |
137 | | |
138 | 15.5M | __m256i data_0 = _mm256_shuffle_epi8(data, filt_center); |
139 | | |
140 | | // multiply the center pixel by 2^(FILTER_BITS - round_0) and add it to |
141 | | // the result |
142 | 15.5M | data_0 = _mm256_slli_epi16(data_0, FILTER_BITS - conv_params->round_0); |
143 | 15.5M | res = _mm256_add_epi16(res, data_0); |
144 | 15.5M | res = _mm256_add_epi16(res, round_const_horz); |
145 | 15.5M | const __m256i res_clamped = |
146 | 15.5M | _mm256_min_epi16(_mm256_max_epi16(res, clamp_low), clamp_high); |
147 | 15.5M | _mm256_store_si256((__m256i *)&im_block[i * im_stride], res_clamped); |
148 | 15.5M | } |
149 | | |
150 | | /* Vertical filter */ |
151 | 1.27M | { |
152 | 1.27M | __m256i src_0 = _mm256_loadu_si256((__m256i *)(im_block + 0 * im_stride)); |
153 | 1.27M | __m256i src_1 = _mm256_loadu_si256((__m256i *)(im_block + 1 * im_stride)); |
154 | 1.27M | __m256i src_2 = _mm256_loadu_si256((__m256i *)(im_block + 2 * im_stride)); |
155 | 1.27M | __m256i src_3 = _mm256_loadu_si256((__m256i *)(im_block + 3 * im_stride)); |
156 | 1.27M | __m256i src_4 = _mm256_loadu_si256((__m256i *)(im_block + 4 * im_stride)); |
157 | 1.27M | __m256i src_5 = _mm256_loadu_si256((__m256i *)(im_block + 5 * im_stride)); |
158 | | |
159 | 1.27M | __m256i s[8]; |
160 | 1.27M | s[0] = _mm256_unpacklo_epi16(src_0, src_1); |
161 | 1.27M | s[1] = _mm256_unpacklo_epi16(src_2, src_3); |
162 | 1.27M | s[2] = _mm256_unpacklo_epi16(src_4, src_5); |
163 | | |
164 | 1.27M | s[4] = _mm256_unpackhi_epi16(src_0, src_1); |
165 | 1.27M | s[5] = _mm256_unpackhi_epi16(src_2, src_3); |
166 | 1.27M | s[6] = _mm256_unpackhi_epi16(src_4, src_5); |
167 | | |
168 | 20.3M | for (i = 0; i < h - 1; i += 2) { |
169 | 19.1M | const int16_t *data = &im_block[i * im_stride]; |
170 | | |
171 | 19.1M | const __m256i s6 = |
172 | 19.1M | _mm256_loadu_si256((__m256i *)(data + 6 * im_stride)); |
173 | 19.1M | const __m256i s7 = |
174 | 19.1M | _mm256_loadu_si256((__m256i *)(data + 7 * im_stride)); |
175 | | |
176 | 19.1M | s[3] = _mm256_unpacklo_epi16(s6, s7); |
177 | 19.1M | s[7] = _mm256_unpackhi_epi16(s6, s7); |
178 | | |
179 | 19.1M | __m256i res_a = convolve(s, coeffs_v); |
180 | 19.1M | __m256i res_b = convolve(s + 4, coeffs_v); |
181 | | |
182 | 19.1M | const __m256i res_a_round = _mm256_sra_epi32( |
183 | 19.1M | _mm256_add_epi32(res_a, round_const_v), round_shift_v); |
184 | 19.1M | const __m256i res_b_round = _mm256_sra_epi32( |
185 | 19.1M | _mm256_add_epi32(res_b, round_const_v), round_shift_v); |
186 | | |
187 | | /* rounding code */ |
188 | | // 16 bit conversion |
189 | 19.1M | const __m256i res_16bit = _mm256_packs_epi32(res_a_round, res_b_round); |
190 | | // 8 bit conversion and saturation to uint8 |
191 | 19.1M | const __m256i res_8b = _mm256_packus_epi16(res_16bit, res_16bit); |
192 | | |
193 | 19.1M | const __m128i res_0 = _mm256_castsi256_si128(res_8b); |
194 | 19.1M | const __m128i res_1 = _mm256_extracti128_si256(res_8b, 1); |
195 | | |
196 | | // Store values into the destination buffer |
197 | 19.1M | __m128i *const p_0 = (__m128i *)&dst[i * dst_stride + j]; |
198 | 19.1M | __m128i *const p_1 = (__m128i *)&dst[i * dst_stride + j + dst_stride]; |
199 | | |
200 | 19.1M | _mm_storel_epi64(p_0, res_0); |
201 | 19.1M | _mm_storel_epi64(p_1, res_1); |
202 | | |
203 | 19.1M | s[0] = s[1]; |
204 | 19.1M | s[1] = s[2]; |
205 | 19.1M | s[2] = s[3]; |
206 | | |
207 | 19.1M | s[4] = s[5]; |
208 | 19.1M | s[5] = s[6]; |
209 | 19.1M | s[6] = s[7]; |
210 | 19.1M | } |
211 | 1.27M | if (h - i) { |
212 | 2.48k | s[0] = _mm256_permute2x128_si256(s[0], s[4], 0x20); |
213 | 2.48k | s[1] = _mm256_permute2x128_si256(s[1], s[5], 0x20); |
214 | 2.48k | s[2] = _mm256_permute2x128_si256(s[2], s[6], 0x20); |
215 | | |
216 | 2.48k | const int16_t *data = &im_block[i * im_stride]; |
217 | 2.48k | const __m128i s6_ = _mm_loadu_si128((__m128i *)(data + 6 * im_stride)); |
218 | 2.48k | const __m128i s7_ = _mm_loadu_si128((__m128i *)(data + 7 * im_stride)); |
219 | | |
220 | 2.48k | __m128i s3 = _mm_unpacklo_epi16(s6_, s7_); |
221 | 2.48k | __m128i s7 = _mm_unpackhi_epi16(s6_, s7_); |
222 | | |
223 | 2.48k | s[3] = _mm256_inserti128_si256(_mm256_castsi128_si256(s3), s7, 1); |
224 | 2.48k | __m256i convolveres = convolve(s, coeffs_v); |
225 | | |
226 | 2.48k | const __m256i res_round = _mm256_sra_epi32( |
227 | 2.48k | _mm256_add_epi32(convolveres, round_const_v), round_shift_v); |
228 | | |
229 | | /* rounding code */ |
230 | | // 16 bit conversion |
231 | 2.48k | __m128i reslo = _mm256_castsi256_si128(res_round); |
232 | 2.48k | __m128i reshi = _mm256_extracti128_si256(res_round, 1); |
233 | 2.48k | const __m128i res_16bit = _mm_packus_epi32(reslo, reshi); |
234 | | |
235 | | // 8 bit conversion and saturation to uint8 |
236 | 2.48k | const __m128i res_8b = _mm_packus_epi16(res_16bit, res_16bit); |
237 | 2.48k | __m128i *const p_0 = (__m128i *)&dst[i * dst_stride + j]; |
238 | 2.48k | _mm_storel_epi64(p_0, res_8b); |
239 | 2.48k | } |
240 | 1.27M | } |
241 | 1.27M | } |
242 | 304k | } |