/src/aom/av1/encoder/x86/rdopt_sse4.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 <assert.h> |
13 | | #include <smmintrin.h> |
14 | | #include "aom_dsp/x86/synonyms.h" |
15 | | |
16 | | #include "config/av1_rtcd.h" |
17 | | #include "av1/encoder/rdopt.h" |
18 | | |
19 | | // Process horizontal and vertical correlations in a 4x4 block of pixels. |
20 | | // We actually use the 4x4 pixels to calculate correlations corresponding to |
21 | | // the top-left 3x3 pixels, so this function must be called with 1x1 overlap, |
22 | | // moving the window along/down by 3 pixels at a time. |
23 | | static inline void horver_correlation_4x4(const int16_t *diff, int stride, |
24 | | __m128i *xy_sum_32, |
25 | | __m128i *xz_sum_32, __m128i *x_sum_32, |
26 | 0 | __m128i *x2_sum_32) { |
27 | | // Pixels in this 4x4 [ a b c d ] |
28 | | // are referred to as: [ e f g h ] |
29 | | // [ i j k l ] |
30 | | // [ m n o p ] |
31 | |
|
32 | 0 | const __m128i pixelsa = xx_loadu_2x64(&diff[0 * stride], &diff[2 * stride]); |
33 | 0 | const __m128i pixelsb = xx_loadu_2x64(&diff[1 * stride], &diff[3 * stride]); |
34 | | // pixelsa = [d c b a l k j i] as i16 |
35 | | // pixelsb = [h g f e p o n m] as i16 |
36 | |
|
37 | 0 | const __m128i slli_a = _mm_slli_epi64(pixelsa, 16); |
38 | 0 | const __m128i slli_b = _mm_slli_epi64(pixelsb, 16); |
39 | | // slli_a = [c b a 0 k j i 0] as i16 |
40 | | // slli_b = [g f e 0 o n m 0] as i16 |
41 | |
|
42 | 0 | const __m128i xy_madd_a = _mm_madd_epi16(pixelsa, slli_a); |
43 | 0 | const __m128i xy_madd_b = _mm_madd_epi16(pixelsb, slli_b); |
44 | | // xy_madd_a = [bc+cd ab jk+kl ij] as i32 |
45 | | // xy_madd_b = [fg+gh ef no+op mn] as i32 |
46 | |
|
47 | 0 | const __m128i xy32 = _mm_hadd_epi32(xy_madd_b, xy_madd_a); |
48 | | // xy32 = [ab+bc+cd ij+jk+kl ef+fg+gh mn+no+op] as i32 |
49 | 0 | *xy_sum_32 = _mm_add_epi32(*xy_sum_32, xy32); |
50 | |
|
51 | 0 | const __m128i xz_madd_a = _mm_madd_epi16(slli_a, slli_b); |
52 | | // xz_madd_a = [bf+cg ae jn+ko im] i32 |
53 | |
|
54 | 0 | const __m128i swap_b = _mm_srli_si128(slli_b, 8); |
55 | | // swap_b = [0 0 0 0 g f e 0] as i16 |
56 | 0 | const __m128i xz_madd_b = _mm_madd_epi16(slli_a, swap_b); |
57 | | // xz_madd_b = [0 0 gk+fj ei] i32 |
58 | |
|
59 | 0 | const __m128i xz32 = _mm_hadd_epi32(xz_madd_b, xz_madd_a); |
60 | | // xz32 = [ae+bf+cg im+jn+ko 0 ei+fj+gk] i32 |
61 | 0 | *xz_sum_32 = _mm_add_epi32(*xz_sum_32, xz32); |
62 | | |
63 | | // Now calculate the straight sums, x_sum += a+b+c+e+f+g+i+j+k |
64 | | // (sum up every element in slli_a and swap_b) |
65 | 0 | const __m128i sum_slli_a = _mm_hadd_epi16(slli_a, slli_a); |
66 | 0 | const __m128i sum_slli_a32 = _mm_cvtepi16_epi32(sum_slli_a); |
67 | | // sum_slli_a32 = [c+b a k+j i] as i32 |
68 | 0 | const __m128i swap_b32 = _mm_cvtepi16_epi32(swap_b); |
69 | | // swap_b32 = [g f e 0] as i32 |
70 | 0 | *x_sum_32 = _mm_add_epi32(*x_sum_32, sum_slli_a32); |
71 | 0 | *x_sum_32 = _mm_add_epi32(*x_sum_32, swap_b32); |
72 | | // sum = [c+b+g a+f k+j+e i] as i32 |
73 | | |
74 | | // Also sum their squares |
75 | 0 | const __m128i slli_a_2 = _mm_madd_epi16(slli_a, slli_a); |
76 | 0 | const __m128i swap_b_2 = _mm_madd_epi16(swap_b, swap_b); |
77 | | // slli_a_2 = [c2+b2 a2 k2+j2 i2] |
78 | | // swap_b_2 = [0 0 g2+f2 e2] |
79 | 0 | const __m128i sum2 = _mm_hadd_epi32(slli_a_2, swap_b_2); |
80 | | // sum2 = [0 g2+f2+e2 c2+b2+a2 k2+j2+i2] |
81 | 0 | *x2_sum_32 = _mm_add_epi32(*x2_sum_32, sum2); |
82 | 0 | } |
83 | | |
84 | | void av1_get_horver_correlation_full_sse4_1(const int16_t *diff, int stride, |
85 | | int width, int height, float *hcorr, |
86 | 0 | float *vcorr) { |
87 | | // The following notation is used: |
88 | | // x - current pixel |
89 | | // y - right neighbour pixel |
90 | | // z - below neighbour pixel |
91 | | // w - down-right neighbour pixel |
92 | 0 | int64_t xy_sum = 0, xz_sum = 0; |
93 | 0 | int64_t x_sum = 0, x2_sum = 0; |
94 | | |
95 | | // Process horizontal and vertical correlations through the body in 4x4 |
96 | | // blocks. This excludes the final row and column and possibly one extra |
97 | | // column depending how 3 divides into width and height |
98 | 0 | int32_t xy_tmp[4] = { 0 }, xz_tmp[4] = { 0 }; |
99 | 0 | int32_t x_tmp[4] = { 0 }, x2_tmp[4] = { 0 }; |
100 | 0 | __m128i xy_sum_32 = _mm_setzero_si128(); |
101 | 0 | __m128i xz_sum_32 = _mm_setzero_si128(); |
102 | 0 | __m128i x_sum_32 = _mm_setzero_si128(); |
103 | 0 | __m128i x2_sum_32 = _mm_setzero_si128(); |
104 | 0 | for (int i = 0; i <= height - 4; i += 3) { |
105 | 0 | for (int j = 0; j <= width - 4; j += 3) { |
106 | 0 | horver_correlation_4x4(&diff[i * stride + j], stride, &xy_sum_32, |
107 | 0 | &xz_sum_32, &x_sum_32, &x2_sum_32); |
108 | 0 | } |
109 | 0 | xx_storeu_128(xy_tmp, xy_sum_32); |
110 | 0 | xx_storeu_128(xz_tmp, xz_sum_32); |
111 | 0 | xx_storeu_128(x_tmp, x_sum_32); |
112 | 0 | xx_storeu_128(x2_tmp, x2_sum_32); |
113 | 0 | xy_sum += (int64_t)xy_tmp[3] + xy_tmp[2] + xy_tmp[1]; |
114 | 0 | xz_sum += (int64_t)xz_tmp[3] + xz_tmp[2] + xz_tmp[0]; |
115 | 0 | x_sum += (int64_t)x_tmp[3] + x_tmp[2] + x_tmp[1] + x_tmp[0]; |
116 | 0 | x2_sum += (int64_t)x2_tmp[2] + x2_tmp[1] + x2_tmp[0]; |
117 | 0 | xy_sum_32 = _mm_setzero_si128(); |
118 | 0 | xz_sum_32 = _mm_setzero_si128(); |
119 | 0 | x_sum_32 = _mm_setzero_si128(); |
120 | 0 | x2_sum_32 = _mm_setzero_si128(); |
121 | 0 | } |
122 | | |
123 | | // x_sum now covers every pixel except the final 1-2 rows and 1-2 cols |
124 | 0 | int64_t x_finalrow = 0, x_finalcol = 0, x2_finalrow = 0, x2_finalcol = 0; |
125 | | |
126 | | // Do we have 2 rows remaining or just the one? Note that width and height |
127 | | // are powers of 2, so each modulo 3 must be 1 or 2. |
128 | 0 | if (height % 3 == 1) { // Just horiz corrs on the final row |
129 | 0 | const int16_t x0 = diff[(height - 1) * stride]; |
130 | 0 | x_sum += x0; |
131 | 0 | x_finalrow += x0; |
132 | 0 | x2_sum += x0 * x0; |
133 | 0 | x2_finalrow += x0 * x0; |
134 | 0 | for (int j = 0; j < width - 1; ++j) { |
135 | 0 | const int16_t x = diff[(height - 1) * stride + j]; |
136 | 0 | const int16_t y = diff[(height - 1) * stride + j + 1]; |
137 | 0 | xy_sum += x * y; |
138 | 0 | x_sum += y; |
139 | 0 | x2_sum += y * y; |
140 | 0 | x_finalrow += y; |
141 | 0 | x2_finalrow += y * y; |
142 | 0 | } |
143 | 0 | } else { // Two rows remaining to do |
144 | 0 | const int16_t x0 = diff[(height - 2) * stride]; |
145 | 0 | const int16_t z0 = diff[(height - 1) * stride]; |
146 | 0 | x_sum += x0 + z0; |
147 | 0 | x2_sum += x0 * x0 + z0 * z0; |
148 | 0 | x_finalrow += z0; |
149 | 0 | x2_finalrow += z0 * z0; |
150 | 0 | for (int j = 0; j < width - 1; ++j) { |
151 | 0 | const int16_t x = diff[(height - 2) * stride + j]; |
152 | 0 | const int16_t y = diff[(height - 2) * stride + j + 1]; |
153 | 0 | const int16_t z = diff[(height - 1) * stride + j]; |
154 | 0 | const int16_t w = diff[(height - 1) * stride + j + 1]; |
155 | | |
156 | | // Horizontal and vertical correlations for the penultimate row: |
157 | 0 | xy_sum += x * y; |
158 | 0 | xz_sum += x * z; |
159 | | |
160 | | // Now just horizontal correlations for the final row: |
161 | 0 | xy_sum += z * w; |
162 | |
|
163 | 0 | x_sum += y + w; |
164 | 0 | x2_sum += y * y + w * w; |
165 | 0 | x_finalrow += w; |
166 | 0 | x2_finalrow += w * w; |
167 | 0 | } |
168 | 0 | } |
169 | | |
170 | | // Do we have 2 columns remaining or just the one? |
171 | 0 | if (width % 3 == 1) { // Just vert corrs on the final col |
172 | 0 | const int16_t x0 = diff[width - 1]; |
173 | 0 | x_sum += x0; |
174 | 0 | x_finalcol += x0; |
175 | 0 | x2_sum += x0 * x0; |
176 | 0 | x2_finalcol += x0 * x0; |
177 | 0 | for (int i = 0; i < height - 1; ++i) { |
178 | 0 | const int16_t x = diff[i * stride + width - 1]; |
179 | 0 | const int16_t z = diff[(i + 1) * stride + width - 1]; |
180 | 0 | xz_sum += x * z; |
181 | 0 | x_finalcol += z; |
182 | 0 | x2_finalcol += z * z; |
183 | | // So the bottom-right elements don't get counted twice: |
184 | 0 | if (i < height - (height % 3 == 1 ? 2 : 3)) { |
185 | 0 | x_sum += z; |
186 | 0 | x2_sum += z * z; |
187 | 0 | } |
188 | 0 | } |
189 | 0 | } else { // Two cols remaining |
190 | 0 | const int16_t x0 = diff[width - 2]; |
191 | 0 | const int16_t y0 = diff[width - 1]; |
192 | 0 | x_sum += x0 + y0; |
193 | 0 | x2_sum += x0 * x0 + y0 * y0; |
194 | 0 | x_finalcol += y0; |
195 | 0 | x2_finalcol += y0 * y0; |
196 | 0 | for (int i = 0; i < height - 1; ++i) { |
197 | 0 | const int16_t x = diff[i * stride + width - 2]; |
198 | 0 | const int16_t y = diff[i * stride + width - 1]; |
199 | 0 | const int16_t z = diff[(i + 1) * stride + width - 2]; |
200 | 0 | const int16_t w = diff[(i + 1) * stride + width - 1]; |
201 | | |
202 | | // Horizontal and vertical correlations for the penultimate col: |
203 | | // Skip these on the last iteration of this loop if we also had two |
204 | | // rows remaining, otherwise the final horizontal and vertical correlation |
205 | | // get erroneously processed twice |
206 | 0 | if (i < height - 2 || height % 3 == 1) { |
207 | 0 | xy_sum += x * y; |
208 | 0 | xz_sum += x * z; |
209 | 0 | } |
210 | |
|
211 | 0 | x_finalcol += w; |
212 | 0 | x2_finalcol += w * w; |
213 | | // So the bottom-right elements don't get counted twice: |
214 | 0 | if (i < height - (height % 3 == 1 ? 2 : 3)) { |
215 | 0 | x_sum += z + w; |
216 | 0 | x2_sum += z * z + w * w; |
217 | 0 | } |
218 | | |
219 | | // Now just vertical correlations for the final column: |
220 | 0 | xz_sum += y * w; |
221 | 0 | } |
222 | 0 | } |
223 | | |
224 | | // Calculate the simple sums and squared-sums |
225 | 0 | int64_t x_firstrow = 0, x_firstcol = 0; |
226 | 0 | int64_t x2_firstrow = 0, x2_firstcol = 0; |
227 | |
|
228 | 0 | for (int j = 0; j < width; ++j) { |
229 | 0 | x_firstrow += diff[j]; |
230 | 0 | x2_firstrow += diff[j] * diff[j]; |
231 | 0 | } |
232 | 0 | for (int i = 0; i < height; ++i) { |
233 | 0 | x_firstcol += diff[i * stride]; |
234 | 0 | x2_firstcol += diff[i * stride] * diff[i * stride]; |
235 | 0 | } |
236 | |
|
237 | 0 | int64_t xhor_sum = x_sum - x_finalcol; |
238 | 0 | int64_t xver_sum = x_sum - x_finalrow; |
239 | 0 | int64_t y_sum = x_sum - x_firstcol; |
240 | 0 | int64_t z_sum = x_sum - x_firstrow; |
241 | 0 | int64_t x2hor_sum = x2_sum - x2_finalcol; |
242 | 0 | int64_t x2ver_sum = x2_sum - x2_finalrow; |
243 | 0 | int64_t y2_sum = x2_sum - x2_firstcol; |
244 | 0 | int64_t z2_sum = x2_sum - x2_firstrow; |
245 | |
|
246 | 0 | const float num_hor = (float)(height * (width - 1)); |
247 | 0 | const float num_ver = (float)((height - 1) * width); |
248 | |
|
249 | 0 | const float xhor_var_n = x2hor_sum - (xhor_sum * xhor_sum) / num_hor; |
250 | 0 | const float xver_var_n = x2ver_sum - (xver_sum * xver_sum) / num_ver; |
251 | |
|
252 | 0 | const float y_var_n = y2_sum - (y_sum * y_sum) / num_hor; |
253 | 0 | const float z_var_n = z2_sum - (z_sum * z_sum) / num_ver; |
254 | |
|
255 | 0 | const float xy_var_n = xy_sum - (xhor_sum * y_sum) / num_hor; |
256 | 0 | const float xz_var_n = xz_sum - (xver_sum * z_sum) / num_ver; |
257 | |
|
258 | 0 | if (xhor_var_n > 0 && y_var_n > 0) { |
259 | 0 | *hcorr = xy_var_n / sqrtf(xhor_var_n * y_var_n); |
260 | 0 | *hcorr = *hcorr < 0 ? 0 : *hcorr; |
261 | 0 | } else { |
262 | 0 | *hcorr = 1.0; |
263 | 0 | } |
264 | 0 | if (xver_var_n > 0 && z_var_n > 0) { |
265 | 0 | *vcorr = xz_var_n / sqrtf(xver_var_n * z_var_n); |
266 | 0 | *vcorr = *vcorr < 0 ? 0 : *vcorr; |
267 | 0 | } else { |
268 | 0 | *vcorr = 1.0; |
269 | 0 | } |
270 | 0 | } |