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 <emmintrin.h>
14 #include "aom_dsp/x86/mem_sse2.h"
15 #include "aom_dsp/x86/synonyms.h"
16
17 #include "config/av1_rtcd.h"
18 #include "av1/common/restoration.h"
19 #include "av1/encoder/pickrst.h"
20
acc_stat_sse41(int32_t * dst,const uint8_t * src,const __m128i * shuffle,const __m128i * kl)21 static INLINE void acc_stat_sse41(int32_t *dst, const uint8_t *src,
22 const __m128i *shuffle, const __m128i *kl) {
23 const __m128i s = _mm_shuffle_epi8(xx_loadu_128(src), *shuffle);
24 const __m128i d0 = _mm_madd_epi16(*kl, _mm_cvtepu8_epi16(s));
25 const __m128i d1 =
26 _mm_madd_epi16(*kl, _mm_cvtepu8_epi16(_mm_srli_si128(s, 8)));
27 const __m128i dst0 = xx_loadu_128(dst);
28 const __m128i dst1 = xx_loadu_128(dst + 4);
29 const __m128i r0 = _mm_add_epi32(dst0, d0);
30 const __m128i r1 = _mm_add_epi32(dst1, d1);
31 xx_storeu_128(dst, r0);
32 xx_storeu_128(dst + 4, r1);
33 }
34
acc_stat_win7_one_line_sse4_1(const uint8_t * dgd,const uint8_t * src,int h_start,int h_end,int dgd_stride,const __m128i * shuffle,int32_t * sumX,int32_t sumY[WIENER_WIN][WIENER_WIN],int32_t M_int[WIENER_WIN][WIENER_WIN],int32_t H_int[WIENER_WIN2][WIENER_WIN * 8])35 static INLINE void acc_stat_win7_one_line_sse4_1(
36 const uint8_t *dgd, const uint8_t *src, int h_start, int h_end,
37 int dgd_stride, const __m128i *shuffle, int32_t *sumX,
38 int32_t sumY[WIENER_WIN][WIENER_WIN], int32_t M_int[WIENER_WIN][WIENER_WIN],
39 int32_t H_int[WIENER_WIN2][WIENER_WIN * 8]) {
40 const int wiener_win = 7;
41 int j, k, l;
42 // Main loop handles two pixels at a time
43 // We can assume that h_start is even, since it will always be aligned to
44 // a tile edge + some number of restoration units, and both of those will
45 // be 64-pixel aligned.
46 // However, at the edge of the image, h_end may be odd, so we need to handle
47 // that case correctly.
48 assert(h_start % 2 == 0);
49 const int h_end_even = h_end & ~1;
50 const int has_odd_pixel = h_end & 1;
51 for (j = h_start; j < h_end_even; j += 2) {
52 const uint8_t *dgd_ij = dgd + j;
53 const uint8_t X1 = src[j];
54 const uint8_t X2 = src[j + 1];
55 *sumX += X1 + X2;
56 for (k = 0; k < wiener_win; k++) {
57 const uint8_t *dgd_ijk = dgd_ij + k * dgd_stride;
58 for (l = 0; l < wiener_win; l++) {
59 int32_t *H_ = &H_int[(l * wiener_win + k)][0];
60 const uint8_t D1 = dgd_ijk[l];
61 const uint8_t D2 = dgd_ijk[l + 1];
62 sumY[k][l] += D1 + D2;
63 M_int[k][l] += D1 * X1 + D2 * X2;
64
65 const __m128i kl =
66 _mm_cvtepu8_epi16(_mm_set1_epi16(loadu_int16(dgd_ijk + l)));
67 acc_stat_sse41(H_ + 0 * 8, dgd_ij + 0 * dgd_stride, shuffle, &kl);
68 acc_stat_sse41(H_ + 1 * 8, dgd_ij + 1 * dgd_stride, shuffle, &kl);
69 acc_stat_sse41(H_ + 2 * 8, dgd_ij + 2 * dgd_stride, shuffle, &kl);
70 acc_stat_sse41(H_ + 3 * 8, dgd_ij + 3 * dgd_stride, shuffle, &kl);
71 acc_stat_sse41(H_ + 4 * 8, dgd_ij + 4 * dgd_stride, shuffle, &kl);
72 acc_stat_sse41(H_ + 5 * 8, dgd_ij + 5 * dgd_stride, shuffle, &kl);
73 acc_stat_sse41(H_ + 6 * 8, dgd_ij + 6 * dgd_stride, shuffle, &kl);
74 }
75 }
76 }
77 // If the width is odd, add in the final pixel
78 if (has_odd_pixel) {
79 const uint8_t *dgd_ij = dgd + j;
80 const uint8_t X1 = src[j];
81 *sumX += X1;
82 for (k = 0; k < wiener_win; k++) {
83 const uint8_t *dgd_ijk = dgd_ij + k * dgd_stride;
84 for (l = 0; l < wiener_win; l++) {
85 int32_t *H_ = &H_int[(l * wiener_win + k)][0];
86 const uint8_t D1 = dgd_ijk[l];
87 sumY[k][l] += D1;
88 M_int[k][l] += D1 * X1;
89
90 // The `acc_stat_sse41` function wants its input to have interleaved
91 // copies of two pixels, but we only have one. However, the pixels
92 // are (effectively) used as inputs to a multiply-accumulate.
93 // So if we set the extra pixel slot to 0, then it is effectively
94 // ignored.
95 const __m128i kl = _mm_cvtepu8_epi16(_mm_set1_epi16((int16_t)D1));
96 acc_stat_sse41(H_ + 0 * 8, dgd_ij + 0 * dgd_stride, shuffle, &kl);
97 acc_stat_sse41(H_ + 1 * 8, dgd_ij + 1 * dgd_stride, shuffle, &kl);
98 acc_stat_sse41(H_ + 2 * 8, dgd_ij + 2 * dgd_stride, shuffle, &kl);
99 acc_stat_sse41(H_ + 3 * 8, dgd_ij + 3 * dgd_stride, shuffle, &kl);
100 acc_stat_sse41(H_ + 4 * 8, dgd_ij + 4 * dgd_stride, shuffle, &kl);
101 acc_stat_sse41(H_ + 5 * 8, dgd_ij + 5 * dgd_stride, shuffle, &kl);
102 acc_stat_sse41(H_ + 6 * 8, dgd_ij + 6 * dgd_stride, shuffle, &kl);
103 }
104 }
105 }
106 }
107
compute_stats_win7_opt_sse4_1(const uint8_t * dgd,const uint8_t * src,int h_start,int h_end,int v_start,int v_end,int dgd_stride,int src_stride,int64_t * M,int64_t * H,int use_downsampled_wiener_stats)108 static INLINE void compute_stats_win7_opt_sse4_1(
109 const uint8_t *dgd, const uint8_t *src, int h_start, int h_end, int v_start,
110 int v_end, int dgd_stride, int src_stride, int64_t *M, int64_t *H,
111 int use_downsampled_wiener_stats) {
112 int i, j, k, l, m, n;
113 const int wiener_win = WIENER_WIN;
114 const int pixel_count = (h_end - h_start) * (v_end - v_start);
115 const int wiener_win2 = wiener_win * wiener_win;
116 const int wiener_halfwin = (wiener_win >> 1);
117 const uint8_t avg =
118 find_average(dgd, h_start, h_end, v_start, v_end, dgd_stride);
119
120 int32_t M_int32[WIENER_WIN][WIENER_WIN] = { { 0 } };
121 int32_t M_int32_row[WIENER_WIN][WIENER_WIN] = { { 0 } };
122 int64_t M_int64[WIENER_WIN][WIENER_WIN] = { { 0 } };
123 int32_t H_int32[WIENER_WIN2][WIENER_WIN * 8] = { { 0 } };
124 int32_t H_int32_row[WIENER_WIN2][WIENER_WIN * 8] = { { 0 } };
125 int64_t H_int64[WIENER_WIN2][WIENER_WIN * 8] = { { 0 } };
126 int32_t sumY[WIENER_WIN][WIENER_WIN] = { { 0 } };
127 int32_t sumX = 0;
128 const uint8_t *dgd_win = dgd - wiener_halfwin * dgd_stride - wiener_halfwin;
129 int downsample_factor =
130 use_downsampled_wiener_stats ? WIENER_STATS_DOWNSAMPLE_FACTOR : 1;
131 int32_t sumX_row = 0;
132 int32_t sumY_row[WIENER_WIN][WIENER_WIN] = { { 0 } };
133
134 const __m128i shuffle = xx_loadu_128(g_shuffle_stats_data);
135 for (j = v_start; j < v_end; j += 64) {
136 const int vert_end = AOMMIN(64, v_end - j) + j;
137 for (i = j; i < vert_end; i = i + downsample_factor) {
138 if (use_downsampled_wiener_stats &&
139 (vert_end - i < WIENER_STATS_DOWNSAMPLE_FACTOR)) {
140 downsample_factor = vert_end - i;
141 }
142 sumX_row = 0;
143 memset(sumY_row, 0, sizeof(int32_t) * WIENER_WIN * WIENER_WIN);
144 memset(M_int32_row, 0, sizeof(int32_t) * WIENER_WIN * WIENER_WIN);
145 memset(H_int32_row, 0, sizeof(int32_t) * WIENER_WIN2 * (WIENER_WIN * 8));
146 acc_stat_win7_one_line_sse4_1(
147 dgd_win + i * dgd_stride, src + i * src_stride, h_start, h_end,
148 dgd_stride, &shuffle, &sumX_row, sumY_row, M_int32_row, H_int32_row);
149 sumX += sumX_row * downsample_factor;
150 // Scale M matrix based on the downsampling factor
151 for (k = 0; k < wiener_win; ++k) {
152 for (l = 0; l < wiener_win; ++l) {
153 sumY[k][l] += (sumY_row[k][l] * downsample_factor);
154 M_int32[k][l] += (M_int32_row[k][l] * downsample_factor);
155 }
156 }
157 // Scale H matrix based on the downsampling factor
158 for (k = 0; k < WIENER_WIN2; ++k) {
159 for (l = 0; l < WIENER_WIN * 8; ++l) {
160 H_int32[k][l] += (H_int32_row[k][l] * downsample_factor);
161 }
162 }
163 }
164 for (k = 0; k < wiener_win; ++k) {
165 for (l = 0; l < wiener_win; ++l) {
166 M_int64[k][l] += M_int32[k][l];
167 M_int32[k][l] = 0;
168 }
169 }
170 for (k = 0; k < WIENER_WIN2; ++k) {
171 for (l = 0; l < WIENER_WIN * 8; ++l) {
172 H_int64[k][l] += H_int32[k][l];
173 H_int32[k][l] = 0;
174 }
175 }
176 }
177
178 const int64_t avg_square_sum = (int64_t)avg * (int64_t)avg * pixel_count;
179 for (k = 0; k < wiener_win; k++) {
180 for (l = 0; l < wiener_win; l++) {
181 const int32_t idx0 = l * wiener_win + k;
182 M[idx0] =
183 M_int64[k][l] + (avg_square_sum - (int64_t)avg * (sumX + sumY[k][l]));
184 int64_t *H_ = H + idx0 * wiener_win2;
185 int64_t *H_int_ = &H_int64[idx0][0];
186 for (m = 0; m < wiener_win; m++) {
187 for (n = 0; n < wiener_win; n++) {
188 H_[m * wiener_win + n] = H_int_[n * 8 + m] + avg_square_sum -
189 (int64_t)avg * (sumY[k][l] + sumY[n][m]);
190 }
191 }
192 }
193 }
194 }
195
196 #if CONFIG_AV1_HIGHBITDEPTH
acc_stat_highbd_sse41(int64_t * dst,const uint16_t * dgd,const __m128i * shuffle,const __m128i * dgd_ijkl)197 static INLINE void acc_stat_highbd_sse41(int64_t *dst, const uint16_t *dgd,
198 const __m128i *shuffle,
199 const __m128i *dgd_ijkl) {
200 // Load 256 bits from dgd in two chunks
201 const __m128i s0l = xx_loadu_128(dgd);
202 const __m128i s0h = xx_loadu_128(dgd + 4);
203 // s0l = [7 6 5 4 3 2 1 0] as u16 values (dgd indices)
204 // s0h = [11 10 9 8 7 6 5 4] as u16 values (dgd indices)
205 // (Slightly strange order so we can apply the same shuffle to both halves)
206
207 // Shuffle the u16 values in each half (actually using 8-bit shuffle mask)
208 const __m128i s1l = _mm_shuffle_epi8(s0l, *shuffle);
209 const __m128i s1h = _mm_shuffle_epi8(s0h, *shuffle);
210 // s1l = [4 3 3 2 2 1 1 0] as u16 values (dgd indices)
211 // s1h = [8 7 7 6 6 5 5 4] as u16 values (dgd indices)
212
213 // Multiply s1 by dgd_ijkl resulting in 8x u32 values
214 // Horizontally add pairs of u32 resulting in 4x u32
215 const __m128i dl = _mm_madd_epi16(*dgd_ijkl, s1l);
216 const __m128i dh = _mm_madd_epi16(*dgd_ijkl, s1h);
217 // dl = [d c b a] as u32 values
218 // dh = [h g f e] as u32 values
219
220 // Add these 8x u32 results on to dst in four parts
221 const __m128i dll = _mm_cvtepu32_epi64(dl);
222 const __m128i dlh = _mm_cvtepu32_epi64(_mm_srli_si128(dl, 8));
223 const __m128i dhl = _mm_cvtepu32_epi64(dh);
224 const __m128i dhh = _mm_cvtepu32_epi64(_mm_srli_si128(dh, 8));
225 // dll = [b a] as u64 values, etc.
226
227 const __m128i rll = _mm_add_epi64(xx_loadu_128(dst), dll);
228 xx_storeu_128(dst, rll);
229 const __m128i rlh = _mm_add_epi64(xx_loadu_128(dst + 2), dlh);
230 xx_storeu_128(dst + 2, rlh);
231 const __m128i rhl = _mm_add_epi64(xx_loadu_128(dst + 4), dhl);
232 xx_storeu_128(dst + 4, rhl);
233 const __m128i rhh = _mm_add_epi64(xx_loadu_128(dst + 6), dhh);
234 xx_storeu_128(dst + 6, rhh);
235 }
236
acc_stat_highbd_win7_one_line_sse4_1(const uint16_t * dgd,const uint16_t * src,int h_start,int h_end,int dgd_stride,const __m128i * shuffle,int32_t * sumX,int32_t sumY[WIENER_WIN][WIENER_WIN],int64_t M_int[WIENER_WIN][WIENER_WIN],int64_t H_int[WIENER_WIN2][WIENER_WIN * 8])237 static INLINE void acc_stat_highbd_win7_one_line_sse4_1(
238 const uint16_t *dgd, const uint16_t *src, int h_start, int h_end,
239 int dgd_stride, const __m128i *shuffle, int32_t *sumX,
240 int32_t sumY[WIENER_WIN][WIENER_WIN], int64_t M_int[WIENER_WIN][WIENER_WIN],
241 int64_t H_int[WIENER_WIN2][WIENER_WIN * 8]) {
242 int j, k, l;
243 const int wiener_win = WIENER_WIN;
244 // Main loop handles two pixels at a time
245 // We can assume that h_start is even, since it will always be aligned to
246 // a tile edge + some number of restoration units, and both of those will
247 // be 64-pixel aligned.
248 // However, at the edge of the image, h_end may be odd, so we need to handle
249 // that case correctly.
250 assert(h_start % 2 == 0);
251 const int h_end_even = h_end & ~1;
252 const int has_odd_pixel = h_end & 1;
253 for (j = h_start; j < h_end_even; j += 2) {
254 const uint16_t X1 = src[j];
255 const uint16_t X2 = src[j + 1];
256 *sumX += X1 + X2;
257 const uint16_t *dgd_ij = dgd + j;
258 for (k = 0; k < wiener_win; k++) {
259 const uint16_t *dgd_ijk = dgd_ij + k * dgd_stride;
260 for (l = 0; l < wiener_win; l++) {
261 int64_t *H_ = &H_int[(l * wiener_win + k)][0];
262 const uint16_t D1 = dgd_ijk[l];
263 const uint16_t D2 = dgd_ijk[l + 1];
264 sumY[k][l] += D1 + D2;
265 M_int[k][l] += D1 * X1 + D2 * X2;
266
267 // Load two u16 values from dgd as a single u32
268 // Then broadcast to 4x u32 slots of a 128
269 const __m128i dgd_ijkl = _mm_set1_epi32(loadu_int32(dgd_ijk + l));
270 // dgd_ijkl = [y x y x y x y x] as u16
271
272 acc_stat_highbd_sse41(H_ + 0 * 8, dgd_ij + 0 * dgd_stride, shuffle,
273 &dgd_ijkl);
274 acc_stat_highbd_sse41(H_ + 1 * 8, dgd_ij + 1 * dgd_stride, shuffle,
275 &dgd_ijkl);
276 acc_stat_highbd_sse41(H_ + 2 * 8, dgd_ij + 2 * dgd_stride, shuffle,
277 &dgd_ijkl);
278 acc_stat_highbd_sse41(H_ + 3 * 8, dgd_ij + 3 * dgd_stride, shuffle,
279 &dgd_ijkl);
280 acc_stat_highbd_sse41(H_ + 4 * 8, dgd_ij + 4 * dgd_stride, shuffle,
281 &dgd_ijkl);
282 acc_stat_highbd_sse41(H_ + 5 * 8, dgd_ij + 5 * dgd_stride, shuffle,
283 &dgd_ijkl);
284 acc_stat_highbd_sse41(H_ + 6 * 8, dgd_ij + 6 * dgd_stride, shuffle,
285 &dgd_ijkl);
286 }
287 }
288 }
289 // If the width is odd, add in the final pixel
290 if (has_odd_pixel) {
291 const uint16_t X1 = src[j];
292 *sumX += X1;
293 const uint16_t *dgd_ij = dgd + j;
294 for (k = 0; k < wiener_win; k++) {
295 const uint16_t *dgd_ijk = dgd_ij + k * dgd_stride;
296 for (l = 0; l < wiener_win; l++) {
297 int64_t *H_ = &H_int[(l * wiener_win + k)][0];
298 const uint16_t D1 = dgd_ijk[l];
299 sumY[k][l] += D1;
300 M_int[k][l] += D1 * X1;
301
302 // The `acc_stat_highbd_sse41` function wants its input to have
303 // interleaved copies of two pixels, but we only have one. However, the
304 // pixels are (effectively) used as inputs to a multiply-accumulate. So
305 // if we set the extra pixel slot to 0, then it is effectively ignored.
306 const __m128i dgd_ijkl = _mm_set1_epi32((int)D1);
307
308 acc_stat_highbd_sse41(H_ + 0 * 8, dgd_ij + 0 * dgd_stride, shuffle,
309 &dgd_ijkl);
310 acc_stat_highbd_sse41(H_ + 1 * 8, dgd_ij + 1 * dgd_stride, shuffle,
311 &dgd_ijkl);
312 acc_stat_highbd_sse41(H_ + 2 * 8, dgd_ij + 2 * dgd_stride, shuffle,
313 &dgd_ijkl);
314 acc_stat_highbd_sse41(H_ + 3 * 8, dgd_ij + 3 * dgd_stride, shuffle,
315 &dgd_ijkl);
316 acc_stat_highbd_sse41(H_ + 4 * 8, dgd_ij + 4 * dgd_stride, shuffle,
317 &dgd_ijkl);
318 acc_stat_highbd_sse41(H_ + 5 * 8, dgd_ij + 5 * dgd_stride, shuffle,
319 &dgd_ijkl);
320 acc_stat_highbd_sse41(H_ + 6 * 8, dgd_ij + 6 * dgd_stride, shuffle,
321 &dgd_ijkl);
322 }
323 }
324 }
325 }
326
compute_stats_highbd_win7_opt_sse4_1(const uint8_t * dgd8,const uint8_t * src8,int h_start,int h_end,int v_start,int v_end,int dgd_stride,int src_stride,int64_t * M,int64_t * H,aom_bit_depth_t bit_depth)327 static INLINE void compute_stats_highbd_win7_opt_sse4_1(
328 const uint8_t *dgd8, const uint8_t *src8, int h_start, int h_end,
329 int v_start, int v_end, int dgd_stride, int src_stride, int64_t *M,
330 int64_t *H, aom_bit_depth_t bit_depth) {
331 int i, j, k, l, m, n;
332 const int wiener_win = WIENER_WIN;
333 const int pixel_count = (h_end - h_start) * (v_end - v_start);
334 const int wiener_win2 = wiener_win * wiener_win;
335 const int wiener_halfwin = (wiener_win >> 1);
336 const uint16_t *src = CONVERT_TO_SHORTPTR(src8);
337 const uint16_t *dgd = CONVERT_TO_SHORTPTR(dgd8);
338 const uint16_t avg =
339 find_average_highbd(dgd, h_start, h_end, v_start, v_end, dgd_stride);
340
341 int64_t M_int[WIENER_WIN][WIENER_WIN] = { { 0 } };
342 int64_t H_int[WIENER_WIN2][WIENER_WIN * 8] = { { 0 } };
343 int32_t sumY[WIENER_WIN][WIENER_WIN] = { { 0 } };
344 int32_t sumX = 0;
345 const uint16_t *dgd_win = dgd - wiener_halfwin * dgd_stride - wiener_halfwin;
346
347 // Load just half of the 256-bit shuffle control used for the AVX2 version
348 const __m128i shuffle = xx_loadu_128(g_shuffle_stats_highbd_data);
349 for (j = v_start; j < v_end; j += 64) {
350 const int vert_end = AOMMIN(64, v_end - j) + j;
351 for (i = j; i < vert_end; i++) {
352 acc_stat_highbd_win7_one_line_sse4_1(
353 dgd_win + i * dgd_stride, src + i * src_stride, h_start, h_end,
354 dgd_stride, &shuffle, &sumX, sumY, M_int, H_int);
355 }
356 }
357
358 uint8_t bit_depth_divider = 1;
359 if (bit_depth == AOM_BITS_12)
360 bit_depth_divider = 16;
361 else if (bit_depth == AOM_BITS_10)
362 bit_depth_divider = 4;
363
364 const int64_t avg_square_sum = (int64_t)avg * (int64_t)avg * pixel_count;
365 for (k = 0; k < wiener_win; k++) {
366 for (l = 0; l < wiener_win; l++) {
367 const int32_t idx0 = l * wiener_win + k;
368 M[idx0] = (M_int[k][l] +
369 (avg_square_sum - (int64_t)avg * (sumX + sumY[k][l]))) /
370 bit_depth_divider;
371 int64_t *H_ = H + idx0 * wiener_win2;
372 int64_t *H_int_ = &H_int[idx0][0];
373 for (m = 0; m < wiener_win; m++) {
374 for (n = 0; n < wiener_win; n++) {
375 H_[m * wiener_win + n] =
376 (H_int_[n * 8 + m] +
377 (avg_square_sum - (int64_t)avg * (sumY[k][l] + sumY[n][m]))) /
378 bit_depth_divider;
379 }
380 }
381 }
382 }
383 }
384
acc_stat_highbd_win5_one_line_sse4_1(const uint16_t * dgd,const uint16_t * src,int h_start,int h_end,int dgd_stride,const __m128i * shuffle,int32_t * sumX,int32_t sumY[WIENER_WIN_CHROMA][WIENER_WIN_CHROMA],int64_t M_int[WIENER_WIN_CHROMA][WIENER_WIN_CHROMA],int64_t H_int[WIENER_WIN2_CHROMA][WIENER_WIN_CHROMA * 8])385 static INLINE void acc_stat_highbd_win5_one_line_sse4_1(
386 const uint16_t *dgd, const uint16_t *src, int h_start, int h_end,
387 int dgd_stride, const __m128i *shuffle, int32_t *sumX,
388 int32_t sumY[WIENER_WIN_CHROMA][WIENER_WIN_CHROMA],
389 int64_t M_int[WIENER_WIN_CHROMA][WIENER_WIN_CHROMA],
390 int64_t H_int[WIENER_WIN2_CHROMA][WIENER_WIN_CHROMA * 8]) {
391 int j, k, l;
392 const int wiener_win = WIENER_WIN_CHROMA;
393 // Main loop handles two pixels at a time
394 // We can assume that h_start is even, since it will always be aligned to
395 // a tile edge + some number of restoration units, and both of those will
396 // be 64-pixel aligned.
397 // However, at the edge of the image, h_end may be odd, so we need to handle
398 // that case correctly.
399 assert(h_start % 2 == 0);
400 const int h_end_even = h_end & ~1;
401 const int has_odd_pixel = h_end & 1;
402 for (j = h_start; j < h_end_even; j += 2) {
403 const uint16_t X1 = src[j];
404 const uint16_t X2 = src[j + 1];
405 *sumX += X1 + X2;
406 const uint16_t *dgd_ij = dgd + j;
407 for (k = 0; k < wiener_win; k++) {
408 const uint16_t *dgd_ijk = dgd_ij + k * dgd_stride;
409 for (l = 0; l < wiener_win; l++) {
410 int64_t *H_ = &H_int[(l * wiener_win + k)][0];
411 const uint16_t D1 = dgd_ijk[l];
412 const uint16_t D2 = dgd_ijk[l + 1];
413 sumY[k][l] += D1 + D2;
414 M_int[k][l] += D1 * X1 + D2 * X2;
415
416 // Load two u16 values from dgd as a single u32
417 // then broadcast to 4x u32 slots of a 128
418 const __m128i dgd_ijkl = _mm_set1_epi32(loadu_int32(dgd_ijk + l));
419 // dgd_ijkl = [y x y x y x y x] as u16
420
421 acc_stat_highbd_sse41(H_ + 0 * 8, dgd_ij + 0 * dgd_stride, shuffle,
422 &dgd_ijkl);
423 acc_stat_highbd_sse41(H_ + 1 * 8, dgd_ij + 1 * dgd_stride, shuffle,
424 &dgd_ijkl);
425 acc_stat_highbd_sse41(H_ + 2 * 8, dgd_ij + 2 * dgd_stride, shuffle,
426 &dgd_ijkl);
427 acc_stat_highbd_sse41(H_ + 3 * 8, dgd_ij + 3 * dgd_stride, shuffle,
428 &dgd_ijkl);
429 acc_stat_highbd_sse41(H_ + 4 * 8, dgd_ij + 4 * dgd_stride, shuffle,
430 &dgd_ijkl);
431 }
432 }
433 }
434 // If the width is odd, add in the final pixel
435 if (has_odd_pixel) {
436 const uint16_t X1 = src[j];
437 *sumX += X1;
438 const uint16_t *dgd_ij = dgd + j;
439 for (k = 0; k < wiener_win; k++) {
440 const uint16_t *dgd_ijk = dgd_ij + k * dgd_stride;
441 for (l = 0; l < wiener_win; l++) {
442 int64_t *H_ = &H_int[(l * wiener_win + k)][0];
443 const uint16_t D1 = dgd_ijk[l];
444 sumY[k][l] += D1;
445 M_int[k][l] += D1 * X1;
446
447 // The `acc_stat_highbd_sse41` function wants its input to have
448 // interleaved copies of two pixels, but we only have one. However, the
449 // pixels are (effectively) used as inputs to a multiply-accumulate. So
450 // if we set the extra pixel slot to 0, then it is effectively ignored.
451 const __m128i dgd_ijkl = _mm_set1_epi32((int)D1);
452
453 acc_stat_highbd_sse41(H_ + 0 * 8, dgd_ij + 0 * dgd_stride, shuffle,
454 &dgd_ijkl);
455 acc_stat_highbd_sse41(H_ + 1 * 8, dgd_ij + 1 * dgd_stride, shuffle,
456 &dgd_ijkl);
457 acc_stat_highbd_sse41(H_ + 2 * 8, dgd_ij + 2 * dgd_stride, shuffle,
458 &dgd_ijkl);
459 acc_stat_highbd_sse41(H_ + 3 * 8, dgd_ij + 3 * dgd_stride, shuffle,
460 &dgd_ijkl);
461 acc_stat_highbd_sse41(H_ + 4 * 8, dgd_ij + 4 * dgd_stride, shuffle,
462 &dgd_ijkl);
463 }
464 }
465 }
466 }
467
compute_stats_highbd_win5_opt_sse4_1(const uint8_t * dgd8,const uint8_t * src8,int h_start,int h_end,int v_start,int v_end,int dgd_stride,int src_stride,int64_t * M,int64_t * H,aom_bit_depth_t bit_depth)468 static INLINE void compute_stats_highbd_win5_opt_sse4_1(
469 const uint8_t *dgd8, const uint8_t *src8, int h_start, int h_end,
470 int v_start, int v_end, int dgd_stride, int src_stride, int64_t *M,
471 int64_t *H, aom_bit_depth_t bit_depth) {
472 int i, j, k, l, m, n;
473 const int wiener_win = WIENER_WIN_CHROMA;
474 const int pixel_count = (h_end - h_start) * (v_end - v_start);
475 const int wiener_win2 = wiener_win * wiener_win;
476 const int wiener_halfwin = (wiener_win >> 1);
477 const uint16_t *src = CONVERT_TO_SHORTPTR(src8);
478 const uint16_t *dgd = CONVERT_TO_SHORTPTR(dgd8);
479 const uint16_t avg =
480 find_average_highbd(dgd, h_start, h_end, v_start, v_end, dgd_stride);
481
482 int64_t M_int[WIENER_WIN_CHROMA][WIENER_WIN_CHROMA] = { { 0 } };
483 int64_t H_int[WIENER_WIN2_CHROMA][WIENER_WIN_CHROMA * 8] = { { 0 } };
484 int32_t sumY[WIENER_WIN_CHROMA][WIENER_WIN_CHROMA] = { { 0 } };
485 int32_t sumX = 0;
486 const uint16_t *dgd_win = dgd - wiener_halfwin * dgd_stride - wiener_halfwin;
487
488 // Load just half of the 256-bit shuffle control used for the AVX2 version
489 const __m128i shuffle = xx_loadu_128(g_shuffle_stats_highbd_data);
490 for (j = v_start; j < v_end; j += 64) {
491 const int vert_end = AOMMIN(64, v_end - j) + j;
492 for (i = j; i < vert_end; i++) {
493 acc_stat_highbd_win5_one_line_sse4_1(
494 dgd_win + i * dgd_stride, src + i * src_stride, h_start, h_end,
495 dgd_stride, &shuffle, &sumX, sumY, M_int, H_int);
496 }
497 }
498
499 uint8_t bit_depth_divider = 1;
500 if (bit_depth == AOM_BITS_12)
501 bit_depth_divider = 16;
502 else if (bit_depth == AOM_BITS_10)
503 bit_depth_divider = 4;
504
505 const int64_t avg_square_sum = (int64_t)avg * (int64_t)avg * pixel_count;
506 for (k = 0; k < wiener_win; k++) {
507 for (l = 0; l < wiener_win; l++) {
508 const int32_t idx0 = l * wiener_win + k;
509 M[idx0] = (M_int[k][l] +
510 (avg_square_sum - (int64_t)avg * (sumX + sumY[k][l]))) /
511 bit_depth_divider;
512 int64_t *H_ = H + idx0 * wiener_win2;
513 int64_t *H_int_ = &H_int[idx0][0];
514 for (m = 0; m < wiener_win; m++) {
515 for (n = 0; n < wiener_win; n++) {
516 H_[m * wiener_win + n] =
517 (H_int_[n * 8 + m] +
518 (avg_square_sum - (int64_t)avg * (sumY[k][l] + sumY[n][m]))) /
519 bit_depth_divider;
520 }
521 }
522 }
523 }
524 }
525
av1_compute_stats_highbd_sse4_1(int wiener_win,const uint8_t * dgd8,const uint8_t * src8,int h_start,int h_end,int v_start,int v_end,int dgd_stride,int src_stride,int64_t * M,int64_t * H,aom_bit_depth_t bit_depth)526 void av1_compute_stats_highbd_sse4_1(int wiener_win, const uint8_t *dgd8,
527 const uint8_t *src8, int h_start,
528 int h_end, int v_start, int v_end,
529 int dgd_stride, int src_stride, int64_t *M,
530 int64_t *H, aom_bit_depth_t bit_depth) {
531 if (wiener_win == WIENER_WIN) {
532 compute_stats_highbd_win7_opt_sse4_1(dgd8, src8, h_start, h_end, v_start,
533 v_end, dgd_stride, src_stride, M, H,
534 bit_depth);
535 } else if (wiener_win == WIENER_WIN_CHROMA) {
536 compute_stats_highbd_win5_opt_sse4_1(dgd8, src8, h_start, h_end, v_start,
537 v_end, dgd_stride, src_stride, M, H,
538 bit_depth);
539 } else {
540 av1_compute_stats_highbd_c(wiener_win, dgd8, src8, h_start, h_end, v_start,
541 v_end, dgd_stride, src_stride, M, H, bit_depth);
542 }
543 }
544 #endif // CONFIG_AV1_HIGHBITDEPTH
545
acc_stat_win5_one_line_sse4_1(const uint8_t * dgd,const uint8_t * src,int h_start,int h_end,int dgd_stride,const __m128i * shuffle,int32_t * sumX,int32_t sumY[WIENER_WIN_CHROMA][WIENER_WIN_CHROMA],int32_t M_int[WIENER_WIN_CHROMA][WIENER_WIN_CHROMA],int32_t H_int[WIENER_WIN2_CHROMA][WIENER_WIN_CHROMA * 8])546 static INLINE void acc_stat_win5_one_line_sse4_1(
547 const uint8_t *dgd, const uint8_t *src, int h_start, int h_end,
548 int dgd_stride, const __m128i *shuffle, int32_t *sumX,
549 int32_t sumY[WIENER_WIN_CHROMA][WIENER_WIN_CHROMA],
550 int32_t M_int[WIENER_WIN_CHROMA][WIENER_WIN_CHROMA],
551 int32_t H_int[WIENER_WIN2_CHROMA][WIENER_WIN_CHROMA * 8]) {
552 const int wiener_win = WIENER_WIN_CHROMA;
553 int j, k, l;
554 // Main loop handles two pixels at a time
555 // We can assume that h_start is even, since it will always be aligned to
556 // a tile edge + some number of restoration units, and both of those will
557 // be 64-pixel aligned.
558 // However, at the edge of the image, h_end may be odd, so we need to handle
559 // that case correctly.
560 assert(h_start % 2 == 0);
561 const int h_end_even = h_end & ~1;
562 const int has_odd_pixel = h_end & 1;
563 for (j = h_start; j < h_end_even; j += 2) {
564 const uint8_t *dgd_ij = dgd + j;
565 const uint8_t X1 = src[j];
566 const uint8_t X2 = src[j + 1];
567 *sumX += X1 + X2;
568 for (k = 0; k < wiener_win; k++) {
569 const uint8_t *dgd_ijk = dgd_ij + k * dgd_stride;
570 for (l = 0; l < wiener_win; l++) {
571 int32_t *H_ = &H_int[(l * wiener_win + k)][0];
572 const uint8_t D1 = dgd_ijk[l];
573 const uint8_t D2 = dgd_ijk[l + 1];
574 sumY[k][l] += D1 + D2;
575 M_int[k][l] += D1 * X1 + D2 * X2;
576
577 const __m128i kl =
578 _mm_cvtepu8_epi16(_mm_set1_epi16(loadu_int16(dgd_ijk + l)));
579 acc_stat_sse41(H_ + 0 * 8, dgd_ij + 0 * dgd_stride, shuffle, &kl);
580 acc_stat_sse41(H_ + 1 * 8, dgd_ij + 1 * dgd_stride, shuffle, &kl);
581 acc_stat_sse41(H_ + 2 * 8, dgd_ij + 2 * dgd_stride, shuffle, &kl);
582 acc_stat_sse41(H_ + 3 * 8, dgd_ij + 3 * dgd_stride, shuffle, &kl);
583 acc_stat_sse41(H_ + 4 * 8, dgd_ij + 4 * dgd_stride, shuffle, &kl);
584 }
585 }
586 }
587 // If the width is odd, add in the final pixel
588 if (has_odd_pixel) {
589 const uint8_t *dgd_ij = dgd + j;
590 const uint8_t X1 = src[j];
591 *sumX += X1;
592 for (k = 0; k < wiener_win; k++) {
593 const uint8_t *dgd_ijk = dgd_ij + k * dgd_stride;
594 for (l = 0; l < wiener_win; l++) {
595 int32_t *H_ = &H_int[(l * wiener_win + k)][0];
596 const uint8_t D1 = dgd_ijk[l];
597 sumY[k][l] += D1;
598 M_int[k][l] += D1 * X1;
599
600 // The `acc_stat_sse41` function wants its input to have interleaved
601 // copies of two pixels, but we only have one. However, the pixels
602 // are (effectively) used as inputs to a multiply-accumulate.
603 // So if we set the extra pixel slot to 0, then it is effectively
604 // ignored.
605 const __m128i kl = _mm_cvtepu8_epi16(_mm_set1_epi16((int16_t)D1));
606 acc_stat_sse41(H_ + 0 * 8, dgd_ij + 0 * dgd_stride, shuffle, &kl);
607 acc_stat_sse41(H_ + 1 * 8, dgd_ij + 1 * dgd_stride, shuffle, &kl);
608 acc_stat_sse41(H_ + 2 * 8, dgd_ij + 2 * dgd_stride, shuffle, &kl);
609 acc_stat_sse41(H_ + 3 * 8, dgd_ij + 3 * dgd_stride, shuffle, &kl);
610 acc_stat_sse41(H_ + 4 * 8, dgd_ij + 4 * dgd_stride, shuffle, &kl);
611 }
612 }
613 }
614 }
615
compute_stats_win5_opt_sse4_1(const uint8_t * dgd,const uint8_t * src,int h_start,int h_end,int v_start,int v_end,int dgd_stride,int src_stride,int64_t * M,int64_t * H,int use_downsampled_wiener_stats)616 static INLINE void compute_stats_win5_opt_sse4_1(
617 const uint8_t *dgd, const uint8_t *src, int h_start, int h_end, int v_start,
618 int v_end, int dgd_stride, int src_stride, int64_t *M, int64_t *H,
619 int use_downsampled_wiener_stats) {
620 int i, j, k, l, m, n;
621 const int wiener_win = WIENER_WIN_CHROMA;
622 const int pixel_count = (h_end - h_start) * (v_end - v_start);
623 const int wiener_win2 = wiener_win * wiener_win;
624 const int wiener_halfwin = (wiener_win >> 1);
625 const uint8_t avg =
626 find_average(dgd, h_start, h_end, v_start, v_end, dgd_stride);
627
628 int32_t M_int32[WIENER_WIN_CHROMA][WIENER_WIN_CHROMA] = { { 0 } };
629 int32_t M_int32_row[WIENER_WIN_CHROMA][WIENER_WIN_CHROMA] = { { 0 } };
630 int64_t M_int64[WIENER_WIN_CHROMA][WIENER_WIN_CHROMA] = { { 0 } };
631 int32_t H_int32[WIENER_WIN2_CHROMA][WIENER_WIN_CHROMA * 8] = { { 0 } };
632 int32_t H_int32_row[WIENER_WIN2_CHROMA][WIENER_WIN_CHROMA * 8] = { { 0 } };
633 int64_t H_int64[WIENER_WIN2_CHROMA][WIENER_WIN_CHROMA * 8] = { { 0 } };
634 int32_t sumY[WIENER_WIN_CHROMA][WIENER_WIN_CHROMA] = { { 0 } };
635 int32_t sumX = 0;
636 const uint8_t *dgd_win = dgd - wiener_halfwin * dgd_stride - wiener_halfwin;
637 int downsample_factor =
638 use_downsampled_wiener_stats ? WIENER_STATS_DOWNSAMPLE_FACTOR : 1;
639 int32_t sumX_row = 0;
640 int32_t sumY_row[WIENER_WIN_CHROMA][WIENER_WIN_CHROMA] = { { 0 } };
641
642 const __m128i shuffle = xx_loadu_128(g_shuffle_stats_data);
643 for (j = v_start; j < v_end; j += 64) {
644 const int vert_end = AOMMIN(64, v_end - j) + j;
645 for (i = j; i < vert_end; i = i + downsample_factor) {
646 if (use_downsampled_wiener_stats &&
647 (vert_end - i < WIENER_STATS_DOWNSAMPLE_FACTOR)) {
648 downsample_factor = vert_end - i;
649 }
650 sumX_row = 0;
651 memset(sumY_row, 0,
652 sizeof(int32_t) * WIENER_WIN_CHROMA * WIENER_WIN_CHROMA);
653 memset(M_int32_row, 0,
654 sizeof(int32_t) * WIENER_WIN_CHROMA * WIENER_WIN_CHROMA);
655 memset(H_int32_row, 0,
656 sizeof(int32_t) * WIENER_WIN2_CHROMA * (WIENER_WIN_CHROMA * 8));
657 acc_stat_win5_one_line_sse4_1(
658 dgd_win + i * dgd_stride, src + i * src_stride, h_start, h_end,
659 dgd_stride, &shuffle, &sumX_row, sumY_row, M_int32_row, H_int32_row);
660 sumX += sumX_row * downsample_factor;
661 // Scale M matrix based on the downsampling factor
662 for (k = 0; k < wiener_win; ++k) {
663 for (l = 0; l < wiener_win; ++l) {
664 sumY[k][l] += (sumY_row[k][l] * downsample_factor);
665 M_int32[k][l] += (M_int32_row[k][l] * downsample_factor);
666 }
667 }
668 // Scale H matrix based on the downsampling factor
669 for (k = 0; k < WIENER_WIN_CHROMA * WIENER_WIN_CHROMA; ++k) {
670 for (l = 0; l < WIENER_WIN_CHROMA * 8; ++l) {
671 H_int32[k][l] += (H_int32_row[k][l] * downsample_factor);
672 }
673 }
674 }
675 for (k = 0; k < wiener_win; ++k) {
676 for (l = 0; l < wiener_win; ++l) {
677 M_int64[k][l] += M_int32[k][l];
678 M_int32[k][l] = 0;
679 }
680 }
681 for (k = 0; k < WIENER_WIN_CHROMA * WIENER_WIN_CHROMA; ++k) {
682 for (l = 0; l < WIENER_WIN_CHROMA * 8; ++l) {
683 H_int64[k][l] += H_int32[k][l];
684 H_int32[k][l] = 0;
685 }
686 }
687 }
688
689 const int64_t avg_square_sum = (int64_t)avg * (int64_t)avg * pixel_count;
690 for (k = 0; k < wiener_win; k++) {
691 for (l = 0; l < wiener_win; l++) {
692 const int32_t idx0 = l * wiener_win + k;
693 M[idx0] =
694 M_int64[k][l] + (avg_square_sum - (int64_t)avg * (sumX + sumY[k][l]));
695 int64_t *H_ = H + idx0 * wiener_win2;
696 int64_t *H_int_ = &H_int64[idx0][0];
697 for (m = 0; m < wiener_win; m++) {
698 for (n = 0; n < wiener_win; n++) {
699 H_[m * wiener_win + n] = H_int_[n * 8 + m] + avg_square_sum -
700 (int64_t)avg * (sumY[k][l] + sumY[n][m]);
701 }
702 }
703 }
704 }
705 }
av1_compute_stats_sse4_1(int wiener_win,const uint8_t * dgd,const uint8_t * src,int16_t * dgd_avg,int16_t * src_avg,int h_start,int h_end,int v_start,int v_end,int dgd_stride,int src_stride,int64_t * M,int64_t * H,int use_downsampled_wiener_stats)706 void av1_compute_stats_sse4_1(int wiener_win, const uint8_t *dgd,
707 const uint8_t *src, int16_t *dgd_avg,
708 int16_t *src_avg, int h_start, int h_end,
709 int v_start, int v_end, int dgd_stride,
710 int src_stride, int64_t *M, int64_t *H,
711 int use_downsampled_wiener_stats) {
712 if (wiener_win == WIENER_WIN) {
713 compute_stats_win7_opt_sse4_1(dgd, src, h_start, h_end, v_start, v_end,
714 dgd_stride, src_stride, M, H,
715 use_downsampled_wiener_stats);
716 } else if (wiener_win == WIENER_WIN_CHROMA) {
717 compute_stats_win5_opt_sse4_1(dgd, src, h_start, h_end, v_start, v_end,
718 dgd_stride, src_stride, M, H,
719 use_downsampled_wiener_stats);
720 } else {
721 av1_compute_stats_c(wiener_win, dgd, src, dgd_avg, src_avg, h_start, h_end,
722 v_start, v_end, dgd_stride, src_stride, M, H,
723 use_downsampled_wiener_stats);
724 }
725 }
726
pair_set_epi16(int a,int b)727 static INLINE __m128i pair_set_epi16(int a, int b) {
728 return _mm_set1_epi32((int32_t)(((uint16_t)(a)) | (((uint32_t)(b)) << 16)));
729 }
730
av1_lowbd_pixel_proj_error_sse4_1(const uint8_t * src8,int width,int height,int src_stride,const uint8_t * dat8,int dat_stride,int32_t * flt0,int flt0_stride,int32_t * flt1,int flt1_stride,int xq[2],const sgr_params_type * params)731 int64_t av1_lowbd_pixel_proj_error_sse4_1(
732 const uint8_t *src8, int width, int height, int src_stride,
733 const uint8_t *dat8, int dat_stride, int32_t *flt0, int flt0_stride,
734 int32_t *flt1, int flt1_stride, int xq[2], const sgr_params_type *params) {
735 int i, j, k;
736 const int32_t shift = SGRPROJ_RST_BITS + SGRPROJ_PRJ_BITS;
737 const __m128i rounding = _mm_set1_epi32(1 << (shift - 1));
738 __m128i sum64 = _mm_setzero_si128();
739 const uint8_t *src = src8;
740 const uint8_t *dat = dat8;
741 int64_t err = 0;
742 if (params->r[0] > 0 && params->r[1] > 0) {
743 __m128i xq_coeff = pair_set_epi16(xq[0], xq[1]);
744 for (i = 0; i < height; ++i) {
745 __m128i sum32 = _mm_setzero_si128();
746 for (j = 0; j <= width - 8; j += 8) {
747 const __m128i d0 = _mm_cvtepu8_epi16(xx_loadl_64(dat + j));
748 const __m128i s0 = _mm_cvtepu8_epi16(xx_loadl_64(src + j));
749 const __m128i flt0_16b =
750 _mm_packs_epi32(xx_loadu_128(flt0 + j), xx_loadu_128(flt0 + j + 4));
751 const __m128i flt1_16b =
752 _mm_packs_epi32(xx_loadu_128(flt1 + j), xx_loadu_128(flt1 + j + 4));
753 const __m128i u0 = _mm_slli_epi16(d0, SGRPROJ_RST_BITS);
754 const __m128i flt0_0_sub_u = _mm_sub_epi16(flt0_16b, u0);
755 const __m128i flt1_0_sub_u = _mm_sub_epi16(flt1_16b, u0);
756 const __m128i v0 = _mm_madd_epi16(
757 xq_coeff, _mm_unpacklo_epi16(flt0_0_sub_u, flt1_0_sub_u));
758 const __m128i v1 = _mm_madd_epi16(
759 xq_coeff, _mm_unpackhi_epi16(flt0_0_sub_u, flt1_0_sub_u));
760 const __m128i vr0 = _mm_srai_epi32(_mm_add_epi32(v0, rounding), shift);
761 const __m128i vr1 = _mm_srai_epi32(_mm_add_epi32(v1, rounding), shift);
762 const __m128i e0 =
763 _mm_sub_epi16(_mm_add_epi16(_mm_packs_epi32(vr0, vr1), d0), s0);
764 const __m128i err0 = _mm_madd_epi16(e0, e0);
765 sum32 = _mm_add_epi32(sum32, err0);
766 }
767 for (k = j; k < width; ++k) {
768 const int32_t u = (int32_t)(dat[k] << SGRPROJ_RST_BITS);
769 int32_t v = xq[0] * (flt0[k] - u) + xq[1] * (flt1[k] - u);
770 const int32_t e = ROUND_POWER_OF_TWO(v, shift) + dat[k] - src[k];
771 err += ((int64_t)e * e);
772 }
773 dat += dat_stride;
774 src += src_stride;
775 flt0 += flt0_stride;
776 flt1 += flt1_stride;
777 const __m128i sum64_0 = _mm_cvtepi32_epi64(sum32);
778 const __m128i sum64_1 = _mm_cvtepi32_epi64(_mm_srli_si128(sum32, 8));
779 sum64 = _mm_add_epi64(sum64, sum64_0);
780 sum64 = _mm_add_epi64(sum64, sum64_1);
781 }
782 } else if (params->r[0] > 0 || params->r[1] > 0) {
783 const int xq_active = (params->r[0] > 0) ? xq[0] : xq[1];
784 const __m128i xq_coeff =
785 pair_set_epi16(xq_active, -xq_active * (1 << SGRPROJ_RST_BITS));
786 const int32_t *flt = (params->r[0] > 0) ? flt0 : flt1;
787 const int flt_stride = (params->r[0] > 0) ? flt0_stride : flt1_stride;
788 for (i = 0; i < height; ++i) {
789 __m128i sum32 = _mm_setzero_si128();
790 for (j = 0; j <= width - 8; j += 8) {
791 const __m128i d0 = _mm_cvtepu8_epi16(xx_loadl_64(dat + j));
792 const __m128i s0 = _mm_cvtepu8_epi16(xx_loadl_64(src + j));
793 const __m128i flt_16b =
794 _mm_packs_epi32(xx_loadu_128(flt + j), xx_loadu_128(flt + j + 4));
795 const __m128i v0 =
796 _mm_madd_epi16(xq_coeff, _mm_unpacklo_epi16(flt_16b, d0));
797 const __m128i v1 =
798 _mm_madd_epi16(xq_coeff, _mm_unpackhi_epi16(flt_16b, d0));
799 const __m128i vr0 = _mm_srai_epi32(_mm_add_epi32(v0, rounding), shift);
800 const __m128i vr1 = _mm_srai_epi32(_mm_add_epi32(v1, rounding), shift);
801 const __m128i e0 =
802 _mm_sub_epi16(_mm_add_epi16(_mm_packs_epi32(vr0, vr1), d0), s0);
803 const __m128i err0 = _mm_madd_epi16(e0, e0);
804 sum32 = _mm_add_epi32(sum32, err0);
805 }
806 for (k = j; k < width; ++k) {
807 const int32_t u = (int32_t)(dat[k] << SGRPROJ_RST_BITS);
808 int32_t v = xq_active * (flt[k] - u);
809 const int32_t e = ROUND_POWER_OF_TWO(v, shift) + dat[k] - src[k];
810 err += ((int64_t)e * e);
811 }
812 dat += dat_stride;
813 src += src_stride;
814 flt += flt_stride;
815 const __m128i sum64_0 = _mm_cvtepi32_epi64(sum32);
816 const __m128i sum64_1 = _mm_cvtepi32_epi64(_mm_srli_si128(sum32, 8));
817 sum64 = _mm_add_epi64(sum64, sum64_0);
818 sum64 = _mm_add_epi64(sum64, sum64_1);
819 }
820 } else {
821 __m128i sum32 = _mm_setzero_si128();
822 for (i = 0; i < height; ++i) {
823 for (j = 0; j <= width - 16; j += 16) {
824 const __m128i d = xx_loadu_128(dat + j);
825 const __m128i s = xx_loadu_128(src + j);
826 const __m128i d0 = _mm_cvtepu8_epi16(d);
827 const __m128i d1 = _mm_cvtepu8_epi16(_mm_srli_si128(d, 8));
828 const __m128i s0 = _mm_cvtepu8_epi16(s);
829 const __m128i s1 = _mm_cvtepu8_epi16(_mm_srli_si128(s, 8));
830 const __m128i diff0 = _mm_sub_epi16(d0, s0);
831 const __m128i diff1 = _mm_sub_epi16(d1, s1);
832 const __m128i err0 = _mm_madd_epi16(diff0, diff0);
833 const __m128i err1 = _mm_madd_epi16(diff1, diff1);
834 sum32 = _mm_add_epi32(sum32, err0);
835 sum32 = _mm_add_epi32(sum32, err1);
836 }
837 for (k = j; k < width; ++k) {
838 const int32_t e = (int32_t)(dat[k]) - src[k];
839 err += ((int64_t)e * e);
840 }
841 dat += dat_stride;
842 src += src_stride;
843 }
844 const __m128i sum64_0 = _mm_cvtepi32_epi64(sum32);
845 const __m128i sum64_1 = _mm_cvtepi32_epi64(_mm_srli_si128(sum32, 8));
846 sum64 = _mm_add_epi64(sum64_0, sum64_1);
847 }
848 int64_t sum[2];
849 xx_storeu_128(sum, sum64);
850 err += sum[0] + sum[1];
851 return err;
852 }
853
854 // When params->r[0] > 0 and params->r[1] > 0. In this case all elements of
855 // C and H need to be computed.
calc_proj_params_r0_r1_sse4_1(const uint8_t * src8,int width,int height,int src_stride,const uint8_t * dat8,int dat_stride,int32_t * flt0,int flt0_stride,int32_t * flt1,int flt1_stride,int64_t H[2][2],int64_t C[2])856 static AOM_INLINE void calc_proj_params_r0_r1_sse4_1(
857 const uint8_t *src8, int width, int height, int src_stride,
858 const uint8_t *dat8, int dat_stride, int32_t *flt0, int flt0_stride,
859 int32_t *flt1, int flt1_stride, int64_t H[2][2], int64_t C[2]) {
860 const int size = width * height;
861 const uint8_t *src = src8;
862 const uint8_t *dat = dat8;
863 __m128i h00, h01, h11, c0, c1;
864 const __m128i zero = _mm_setzero_si128();
865 h01 = h11 = c0 = c1 = h00 = zero;
866
867 for (int i = 0; i < height; ++i) {
868 for (int j = 0; j < width; j += 4) {
869 const __m128i u_load = _mm_cvtepu8_epi32(
870 _mm_cvtsi32_si128(*((int *)(dat + i * dat_stride + j))));
871 const __m128i s_load = _mm_cvtepu8_epi32(
872 _mm_cvtsi32_si128(*((int *)(src + i * src_stride + j))));
873 __m128i f1 = _mm_loadu_si128((__m128i *)(flt0 + i * flt0_stride + j));
874 __m128i f2 = _mm_loadu_si128((__m128i *)(flt1 + i * flt1_stride + j));
875 __m128i d = _mm_slli_epi32(u_load, SGRPROJ_RST_BITS);
876 __m128i s = _mm_slli_epi32(s_load, SGRPROJ_RST_BITS);
877 s = _mm_sub_epi32(s, d);
878 f1 = _mm_sub_epi32(f1, d);
879 f2 = _mm_sub_epi32(f2, d);
880
881 const __m128i h00_even = _mm_mul_epi32(f1, f1);
882 const __m128i h00_odd =
883 _mm_mul_epi32(_mm_srli_epi64(f1, 32), _mm_srli_epi64(f1, 32));
884 h00 = _mm_add_epi64(h00, h00_even);
885 h00 = _mm_add_epi64(h00, h00_odd);
886
887 const __m128i h01_even = _mm_mul_epi32(f1, f2);
888 const __m128i h01_odd =
889 _mm_mul_epi32(_mm_srli_epi64(f1, 32), _mm_srli_epi64(f2, 32));
890 h01 = _mm_add_epi64(h01, h01_even);
891 h01 = _mm_add_epi64(h01, h01_odd);
892
893 const __m128i h11_even = _mm_mul_epi32(f2, f2);
894 const __m128i h11_odd =
895 _mm_mul_epi32(_mm_srli_epi64(f2, 32), _mm_srli_epi64(f2, 32));
896 h11 = _mm_add_epi64(h11, h11_even);
897 h11 = _mm_add_epi64(h11, h11_odd);
898
899 const __m128i c0_even = _mm_mul_epi32(f1, s);
900 const __m128i c0_odd =
901 _mm_mul_epi32(_mm_srli_epi64(f1, 32), _mm_srli_epi64(s, 32));
902 c0 = _mm_add_epi64(c0, c0_even);
903 c0 = _mm_add_epi64(c0, c0_odd);
904
905 const __m128i c1_even = _mm_mul_epi32(f2, s);
906 const __m128i c1_odd =
907 _mm_mul_epi32(_mm_srli_epi64(f2, 32), _mm_srli_epi64(s, 32));
908 c1 = _mm_add_epi64(c1, c1_even);
909 c1 = _mm_add_epi64(c1, c1_odd);
910 }
911 }
912
913 __m128i c_low = _mm_unpacklo_epi64(c0, c1);
914 const __m128i c_high = _mm_unpackhi_epi64(c0, c1);
915 c_low = _mm_add_epi64(c_low, c_high);
916
917 __m128i h0x_low = _mm_unpacklo_epi64(h00, h01);
918 const __m128i h0x_high = _mm_unpackhi_epi64(h00, h01);
919 h0x_low = _mm_add_epi64(h0x_low, h0x_high);
920
921 // Using the symmetric properties of H, calculations of H[1][0] are not
922 // needed.
923 __m128i h1x_low = _mm_unpacklo_epi64(zero, h11);
924 const __m128i h1x_high = _mm_unpackhi_epi64(zero, h11);
925 h1x_low = _mm_add_epi64(h1x_low, h1x_high);
926
927 xx_storeu_128(C, c_low);
928 xx_storeu_128(H[0], h0x_low);
929 xx_storeu_128(H[1], h1x_low);
930
931 H[0][0] /= size;
932 H[0][1] /= size;
933 H[1][1] /= size;
934
935 // Since H is a symmetric matrix
936 H[1][0] = H[0][1];
937 C[0] /= size;
938 C[1] /= size;
939 }
940
941 // When only params->r[0] > 0. In this case only H[0][0] and C[0] are
942 // non-zero and need to be computed.
calc_proj_params_r0_sse4_1(const uint8_t * src8,int width,int height,int src_stride,const uint8_t * dat8,int dat_stride,int32_t * flt0,int flt0_stride,int64_t H[2][2],int64_t C[2])943 static AOM_INLINE void calc_proj_params_r0_sse4_1(
944 const uint8_t *src8, int width, int height, int src_stride,
945 const uint8_t *dat8, int dat_stride, int32_t *flt0, int flt0_stride,
946 int64_t H[2][2], int64_t C[2]) {
947 const int size = width * height;
948 const uint8_t *src = src8;
949 const uint8_t *dat = dat8;
950 __m128i h00, c0;
951 const __m128i zero = _mm_setzero_si128();
952 c0 = h00 = zero;
953
954 for (int i = 0; i < height; ++i) {
955 for (int j = 0; j < width; j += 4) {
956 const __m128i u_load = _mm_cvtepu8_epi32(
957 _mm_cvtsi32_si128(*((int *)(dat + i * dat_stride + j))));
958 const __m128i s_load = _mm_cvtepu8_epi32(
959 _mm_cvtsi32_si128(*((int *)(src + i * src_stride + j))));
960 __m128i f1 = _mm_loadu_si128((__m128i *)(flt0 + i * flt0_stride + j));
961 __m128i d = _mm_slli_epi32(u_load, SGRPROJ_RST_BITS);
962 __m128i s = _mm_slli_epi32(s_load, SGRPROJ_RST_BITS);
963 s = _mm_sub_epi32(s, d);
964 f1 = _mm_sub_epi32(f1, d);
965
966 const __m128i h00_even = _mm_mul_epi32(f1, f1);
967 const __m128i h00_odd =
968 _mm_mul_epi32(_mm_srli_epi64(f1, 32), _mm_srli_epi64(f1, 32));
969 h00 = _mm_add_epi64(h00, h00_even);
970 h00 = _mm_add_epi64(h00, h00_odd);
971
972 const __m128i c0_even = _mm_mul_epi32(f1, s);
973 const __m128i c0_odd =
974 _mm_mul_epi32(_mm_srli_epi64(f1, 32), _mm_srli_epi64(s, 32));
975 c0 = _mm_add_epi64(c0, c0_even);
976 c0 = _mm_add_epi64(c0, c0_odd);
977 }
978 }
979 const __m128i h00_val = _mm_add_epi64(h00, _mm_srli_si128(h00, 8));
980
981 const __m128i c0_val = _mm_add_epi64(c0, _mm_srli_si128(c0, 8));
982
983 const __m128i c = _mm_unpacklo_epi64(c0_val, zero);
984 const __m128i h0x = _mm_unpacklo_epi64(h00_val, zero);
985
986 xx_storeu_128(C, c);
987 xx_storeu_128(H[0], h0x);
988
989 H[0][0] /= size;
990 C[0] /= size;
991 }
992
993 // When only params->r[1] > 0. In this case only H[1][1] and C[1] are
994 // non-zero and need to be computed.
calc_proj_params_r1_sse4_1(const uint8_t * src8,int width,int height,int src_stride,const uint8_t * dat8,int dat_stride,int32_t * flt1,int flt1_stride,int64_t H[2][2],int64_t C[2])995 static AOM_INLINE void calc_proj_params_r1_sse4_1(
996 const uint8_t *src8, int width, int height, int src_stride,
997 const uint8_t *dat8, int dat_stride, int32_t *flt1, int flt1_stride,
998 int64_t H[2][2], int64_t C[2]) {
999 const int size = width * height;
1000 const uint8_t *src = src8;
1001 const uint8_t *dat = dat8;
1002 __m128i h11, c1;
1003 const __m128i zero = _mm_setzero_si128();
1004 c1 = h11 = zero;
1005
1006 for (int i = 0; i < height; ++i) {
1007 for (int j = 0; j < width; j += 4) {
1008 const __m128i u_load = _mm_cvtepu8_epi32(
1009 _mm_cvtsi32_si128(*((int *)(dat + i * dat_stride + j))));
1010 const __m128i s_load = _mm_cvtepu8_epi32(
1011 _mm_cvtsi32_si128(*((int *)(src + i * src_stride + j))));
1012 __m128i f2 = _mm_loadu_si128((__m128i *)(flt1 + i * flt1_stride + j));
1013 __m128i d = _mm_slli_epi32(u_load, SGRPROJ_RST_BITS);
1014 __m128i s = _mm_slli_epi32(s_load, SGRPROJ_RST_BITS);
1015 s = _mm_sub_epi32(s, d);
1016 f2 = _mm_sub_epi32(f2, d);
1017
1018 const __m128i h11_even = _mm_mul_epi32(f2, f2);
1019 const __m128i h11_odd =
1020 _mm_mul_epi32(_mm_srli_epi64(f2, 32), _mm_srli_epi64(f2, 32));
1021 h11 = _mm_add_epi64(h11, h11_even);
1022 h11 = _mm_add_epi64(h11, h11_odd);
1023
1024 const __m128i c1_even = _mm_mul_epi32(f2, s);
1025 const __m128i c1_odd =
1026 _mm_mul_epi32(_mm_srli_epi64(f2, 32), _mm_srli_epi64(s, 32));
1027 c1 = _mm_add_epi64(c1, c1_even);
1028 c1 = _mm_add_epi64(c1, c1_odd);
1029 }
1030 }
1031
1032 const __m128i h11_val = _mm_add_epi64(h11, _mm_srli_si128(h11, 8));
1033
1034 const __m128i c1_val = _mm_add_epi64(c1, _mm_srli_si128(c1, 8));
1035
1036 const __m128i c = _mm_unpacklo_epi64(zero, c1_val);
1037 const __m128i h1x = _mm_unpacklo_epi64(zero, h11_val);
1038
1039 xx_storeu_128(C, c);
1040 xx_storeu_128(H[1], h1x);
1041
1042 H[1][1] /= size;
1043 C[1] /= size;
1044 }
1045
1046 // SSE4.1 variant of av1_calc_proj_params_c.
av1_calc_proj_params_sse4_1(const uint8_t * src8,int width,int height,int src_stride,const uint8_t * dat8,int dat_stride,int32_t * flt0,int flt0_stride,int32_t * flt1,int flt1_stride,int64_t H[2][2],int64_t C[2],const sgr_params_type * params)1047 void av1_calc_proj_params_sse4_1(const uint8_t *src8, int width, int height,
1048 int src_stride, const uint8_t *dat8,
1049 int dat_stride, int32_t *flt0, int flt0_stride,
1050 int32_t *flt1, int flt1_stride,
1051 int64_t H[2][2], int64_t C[2],
1052 const sgr_params_type *params) {
1053 if ((params->r[0] > 0) && (params->r[1] > 0)) {
1054 calc_proj_params_r0_r1_sse4_1(src8, width, height, src_stride, dat8,
1055 dat_stride, flt0, flt0_stride, flt1,
1056 flt1_stride, H, C);
1057 } else if (params->r[0] > 0) {
1058 calc_proj_params_r0_sse4_1(src8, width, height, src_stride, dat8,
1059 dat_stride, flt0, flt0_stride, H, C);
1060 } else if (params->r[1] > 0) {
1061 calc_proj_params_r1_sse4_1(src8, width, height, src_stride, dat8,
1062 dat_stride, flt1, flt1_stride, H, C);
1063 }
1064 }
1065
calc_proj_params_r0_r1_high_bd_sse4_1(const uint8_t * src8,int width,int height,int src_stride,const uint8_t * dat8,int dat_stride,int32_t * flt0,int flt0_stride,int32_t * flt1,int flt1_stride,int64_t H[2][2],int64_t C[2])1066 static AOM_INLINE void calc_proj_params_r0_r1_high_bd_sse4_1(
1067 const uint8_t *src8, int width, int height, int src_stride,
1068 const uint8_t *dat8, int dat_stride, int32_t *flt0, int flt0_stride,
1069 int32_t *flt1, int flt1_stride, int64_t H[2][2], int64_t C[2]) {
1070 const int size = width * height;
1071 const uint16_t *src = CONVERT_TO_SHORTPTR(src8);
1072 const uint16_t *dat = CONVERT_TO_SHORTPTR(dat8);
1073 __m128i h00, h01, h11, c0, c1;
1074 const __m128i zero = _mm_setzero_si128();
1075 h01 = h11 = c0 = c1 = h00 = zero;
1076
1077 for (int i = 0; i < height; ++i) {
1078 for (int j = 0; j < width; j += 4) {
1079 const __m128i u_load = _mm_cvtepu16_epi32(
1080 _mm_loadl_epi64((__m128i *)(dat + i * dat_stride + j)));
1081 const __m128i s_load = _mm_cvtepu16_epi32(
1082 _mm_loadl_epi64((__m128i *)(src + i * src_stride + j)));
1083 __m128i f1 = _mm_loadu_si128((__m128i *)(flt0 + i * flt0_stride + j));
1084 __m128i f2 = _mm_loadu_si128((__m128i *)(flt1 + i * flt1_stride + j));
1085 __m128i d = _mm_slli_epi32(u_load, SGRPROJ_RST_BITS);
1086 __m128i s = _mm_slli_epi32(s_load, SGRPROJ_RST_BITS);
1087 s = _mm_sub_epi32(s, d);
1088 f1 = _mm_sub_epi32(f1, d);
1089 f2 = _mm_sub_epi32(f2, d);
1090
1091 const __m128i h00_even = _mm_mul_epi32(f1, f1);
1092 const __m128i h00_odd =
1093 _mm_mul_epi32(_mm_srli_epi64(f1, 32), _mm_srli_epi64(f1, 32));
1094 h00 = _mm_add_epi64(h00, h00_even);
1095 h00 = _mm_add_epi64(h00, h00_odd);
1096
1097 const __m128i h01_even = _mm_mul_epi32(f1, f2);
1098 const __m128i h01_odd =
1099 _mm_mul_epi32(_mm_srli_epi64(f1, 32), _mm_srli_epi64(f2, 32));
1100 h01 = _mm_add_epi64(h01, h01_even);
1101 h01 = _mm_add_epi64(h01, h01_odd);
1102
1103 const __m128i h11_even = _mm_mul_epi32(f2, f2);
1104 const __m128i h11_odd =
1105 _mm_mul_epi32(_mm_srli_epi64(f2, 32), _mm_srli_epi64(f2, 32));
1106 h11 = _mm_add_epi64(h11, h11_even);
1107 h11 = _mm_add_epi64(h11, h11_odd);
1108
1109 const __m128i c0_even = _mm_mul_epi32(f1, s);
1110 const __m128i c0_odd =
1111 _mm_mul_epi32(_mm_srli_epi64(f1, 32), _mm_srli_epi64(s, 32));
1112 c0 = _mm_add_epi64(c0, c0_even);
1113 c0 = _mm_add_epi64(c0, c0_odd);
1114
1115 const __m128i c1_even = _mm_mul_epi32(f2, s);
1116 const __m128i c1_odd =
1117 _mm_mul_epi32(_mm_srli_epi64(f2, 32), _mm_srli_epi64(s, 32));
1118 c1 = _mm_add_epi64(c1, c1_even);
1119 c1 = _mm_add_epi64(c1, c1_odd);
1120 }
1121 }
1122
1123 __m128i c_low = _mm_unpacklo_epi64(c0, c1);
1124 const __m128i c_high = _mm_unpackhi_epi64(c0, c1);
1125 c_low = _mm_add_epi64(c_low, c_high);
1126
1127 __m128i h0x_low = _mm_unpacklo_epi64(h00, h01);
1128 const __m128i h0x_high = _mm_unpackhi_epi64(h00, h01);
1129 h0x_low = _mm_add_epi64(h0x_low, h0x_high);
1130
1131 // Using the symmetric properties of H, calculations of H[1][0] are not
1132 // needed.
1133 __m128i h1x_low = _mm_unpacklo_epi64(zero, h11);
1134 const __m128i h1x_high = _mm_unpackhi_epi64(zero, h11);
1135 h1x_low = _mm_add_epi64(h1x_low, h1x_high);
1136
1137 xx_storeu_128(C, c_low);
1138 xx_storeu_128(H[0], h0x_low);
1139 xx_storeu_128(H[1], h1x_low);
1140
1141 H[0][0] /= size;
1142 H[0][1] /= size;
1143 H[1][1] /= size;
1144
1145 // Since H is a symmetric matrix
1146 H[1][0] = H[0][1];
1147 C[0] /= size;
1148 C[1] /= size;
1149 }
1150
1151 // When only params->r[0] > 0. In this case only H[0][0] and C[0] are
1152 // non-zero and need to be computed.
calc_proj_params_r0_high_bd_sse4_1(const uint8_t * src8,int width,int height,int src_stride,const uint8_t * dat8,int dat_stride,int32_t * flt0,int flt0_stride,int64_t H[2][2],int64_t C[2])1153 static AOM_INLINE void calc_proj_params_r0_high_bd_sse4_1(
1154 const uint8_t *src8, int width, int height, int src_stride,
1155 const uint8_t *dat8, int dat_stride, int32_t *flt0, int flt0_stride,
1156 int64_t H[2][2], int64_t C[2]) {
1157 const int size = width * height;
1158 const uint16_t *src = CONVERT_TO_SHORTPTR(src8);
1159 const uint16_t *dat = CONVERT_TO_SHORTPTR(dat8);
1160 __m128i h00, c0;
1161 const __m128i zero = _mm_setzero_si128();
1162 c0 = h00 = zero;
1163
1164 for (int i = 0; i < height; ++i) {
1165 for (int j = 0; j < width; j += 4) {
1166 const __m128i u_load = _mm_cvtepu16_epi32(
1167 _mm_loadl_epi64((__m128i *)(dat + i * dat_stride + j)));
1168 const __m128i s_load = _mm_cvtepu16_epi32(
1169 _mm_loadl_epi64((__m128i *)(src + i * src_stride + j)));
1170 __m128i f1 = _mm_loadu_si128((__m128i *)(flt0 + i * flt0_stride + j));
1171 __m128i d = _mm_slli_epi32(u_load, SGRPROJ_RST_BITS);
1172 __m128i s = _mm_slli_epi32(s_load, SGRPROJ_RST_BITS);
1173 s = _mm_sub_epi32(s, d);
1174 f1 = _mm_sub_epi32(f1, d);
1175
1176 const __m128i h00_even = _mm_mul_epi32(f1, f1);
1177 const __m128i h00_odd =
1178 _mm_mul_epi32(_mm_srli_epi64(f1, 32), _mm_srli_epi64(f1, 32));
1179 h00 = _mm_add_epi64(h00, h00_even);
1180 h00 = _mm_add_epi64(h00, h00_odd);
1181
1182 const __m128i c0_even = _mm_mul_epi32(f1, s);
1183 const __m128i c0_odd =
1184 _mm_mul_epi32(_mm_srli_epi64(f1, 32), _mm_srli_epi64(s, 32));
1185 c0 = _mm_add_epi64(c0, c0_even);
1186 c0 = _mm_add_epi64(c0, c0_odd);
1187 }
1188 }
1189 const __m128i h00_val = _mm_add_epi64(h00, _mm_srli_si128(h00, 8));
1190
1191 const __m128i c0_val = _mm_add_epi64(c0, _mm_srli_si128(c0, 8));
1192
1193 const __m128i c = _mm_unpacklo_epi64(c0_val, zero);
1194 const __m128i h0x = _mm_unpacklo_epi64(h00_val, zero);
1195
1196 xx_storeu_128(C, c);
1197 xx_storeu_128(H[0], h0x);
1198
1199 H[0][0] /= size;
1200 C[0] /= size;
1201 }
1202
1203 // When only params->r[1] > 0. In this case only H[1][1] and C[1] are
1204 // non-zero and need to be computed.
calc_proj_params_r1_high_bd_sse4_1(const uint8_t * src8,int width,int height,int src_stride,const uint8_t * dat8,int dat_stride,int32_t * flt1,int flt1_stride,int64_t H[2][2],int64_t C[2])1205 static AOM_INLINE void calc_proj_params_r1_high_bd_sse4_1(
1206 const uint8_t *src8, int width, int height, int src_stride,
1207 const uint8_t *dat8, int dat_stride, int32_t *flt1, int flt1_stride,
1208 int64_t H[2][2], int64_t C[2]) {
1209 const int size = width * height;
1210 const uint16_t *src = CONVERT_TO_SHORTPTR(src8);
1211 const uint16_t *dat = CONVERT_TO_SHORTPTR(dat8);
1212 __m128i h11, c1;
1213 const __m128i zero = _mm_setzero_si128();
1214 c1 = h11 = zero;
1215
1216 for (int i = 0; i < height; ++i) {
1217 for (int j = 0; j < width; j += 4) {
1218 const __m128i u_load = _mm_cvtepu16_epi32(
1219 _mm_loadl_epi64((__m128i *)(dat + i * dat_stride + j)));
1220 const __m128i s_load = _mm_cvtepu16_epi32(
1221 _mm_loadl_epi64((__m128i *)(src + i * src_stride + j)));
1222 __m128i f2 = _mm_loadu_si128((__m128i *)(flt1 + i * flt1_stride + j));
1223 __m128i d = _mm_slli_epi32(u_load, SGRPROJ_RST_BITS);
1224 __m128i s = _mm_slli_epi32(s_load, SGRPROJ_RST_BITS);
1225 s = _mm_sub_epi32(s, d);
1226 f2 = _mm_sub_epi32(f2, d);
1227
1228 const __m128i h11_even = _mm_mul_epi32(f2, f2);
1229 const __m128i h11_odd =
1230 _mm_mul_epi32(_mm_srli_epi64(f2, 32), _mm_srli_epi64(f2, 32));
1231 h11 = _mm_add_epi64(h11, h11_even);
1232 h11 = _mm_add_epi64(h11, h11_odd);
1233
1234 const __m128i c1_even = _mm_mul_epi32(f2, s);
1235 const __m128i c1_odd =
1236 _mm_mul_epi32(_mm_srli_epi64(f2, 32), _mm_srli_epi64(s, 32));
1237 c1 = _mm_add_epi64(c1, c1_even);
1238 c1 = _mm_add_epi64(c1, c1_odd);
1239 }
1240 }
1241
1242 const __m128i h11_val = _mm_add_epi64(h11, _mm_srli_si128(h11, 8));
1243
1244 const __m128i c1_val = _mm_add_epi64(c1, _mm_srli_si128(c1, 8));
1245
1246 const __m128i c = _mm_unpacklo_epi64(zero, c1_val);
1247 const __m128i h1x = _mm_unpacklo_epi64(zero, h11_val);
1248
1249 xx_storeu_128(C, c);
1250 xx_storeu_128(H[1], h1x);
1251
1252 H[1][1] /= size;
1253 C[1] /= size;
1254 }
1255
1256 // SSE4.1 variant of av1_calc_proj_params_high_bd_c.
av1_calc_proj_params_high_bd_sse4_1(const uint8_t * src8,int width,int height,int src_stride,const uint8_t * dat8,int dat_stride,int32_t * flt0,int flt0_stride,int32_t * flt1,int flt1_stride,int64_t H[2][2],int64_t C[2],const sgr_params_type * params)1257 void av1_calc_proj_params_high_bd_sse4_1(const uint8_t *src8, int width,
1258 int height, int src_stride,
1259 const uint8_t *dat8, int dat_stride,
1260 int32_t *flt0, int flt0_stride,
1261 int32_t *flt1, int flt1_stride,
1262 int64_t H[2][2], int64_t C[2],
1263 const sgr_params_type *params) {
1264 if ((params->r[0] > 0) && (params->r[1] > 0)) {
1265 calc_proj_params_r0_r1_high_bd_sse4_1(src8, width, height, src_stride, dat8,
1266 dat_stride, flt0, flt0_stride, flt1,
1267 flt1_stride, H, C);
1268 } else if (params->r[0] > 0) {
1269 calc_proj_params_r0_high_bd_sse4_1(src8, width, height, src_stride, dat8,
1270 dat_stride, flt0, flt0_stride, H, C);
1271 } else if (params->r[1] > 0) {
1272 calc_proj_params_r1_high_bd_sse4_1(src8, width, height, src_stride, dat8,
1273 dat_stride, flt1, flt1_stride, H, C);
1274 }
1275 }
1276
1277 #if CONFIG_AV1_HIGHBITDEPTH
av1_highbd_pixel_proj_error_sse4_1(const uint8_t * src8,int width,int height,int src_stride,const uint8_t * dat8,int dat_stride,int32_t * flt0,int flt0_stride,int32_t * flt1,int flt1_stride,int xq[2],const sgr_params_type * params)1278 int64_t av1_highbd_pixel_proj_error_sse4_1(
1279 const uint8_t *src8, int width, int height, int src_stride,
1280 const uint8_t *dat8, int dat_stride, int32_t *flt0, int flt0_stride,
1281 int32_t *flt1, int flt1_stride, int xq[2], const sgr_params_type *params) {
1282 int i, j, k;
1283 const int32_t shift = SGRPROJ_RST_BITS + SGRPROJ_PRJ_BITS;
1284 const __m128i rounding = _mm_set1_epi32(1 << (shift - 1));
1285 __m128i sum64 = _mm_setzero_si128();
1286 const uint16_t *src = CONVERT_TO_SHORTPTR(src8);
1287 const uint16_t *dat = CONVERT_TO_SHORTPTR(dat8);
1288 int64_t err = 0;
1289 if (params->r[0] > 0 && params->r[1] > 0) { // Both filters are enabled
1290 const __m128i xq0 = _mm_set1_epi32(xq[0]);
1291 const __m128i xq1 = _mm_set1_epi32(xq[1]);
1292
1293 for (i = 0; i < height; ++i) {
1294 __m128i sum32 = _mm_setzero_si128();
1295 for (j = 0; j <= width - 8; j += 8) {
1296 // Load 8x pixels from source image
1297 const __m128i s0 = xx_loadu_128(src + j);
1298 // s0 = [7 6 5 4 3 2 1 0] as i16 (indices of src[])
1299
1300 // Load 8x pixels from corrupted image
1301 const __m128i d0 = xx_loadu_128(dat + j);
1302 // d0 = [7 6 5 4 3 2 1 0] as i16 (indices of dat[])
1303
1304 // Shift each pixel value up by SGRPROJ_RST_BITS
1305 const __m128i u0 = _mm_slli_epi16(d0, SGRPROJ_RST_BITS);
1306
1307 // Split u0 into two halves and pad each from u16 to i32
1308 const __m128i u0l = _mm_cvtepu16_epi32(u0);
1309 const __m128i u0h = _mm_cvtepu16_epi32(_mm_srli_si128(u0, 8));
1310 // u0h = [7 6 5 4] as i32, u0l = [3 2 1 0] as i32, all dat[] indices
1311
1312 // Load 8 pixels from first and second filtered images
1313 const __m128i flt0l = xx_loadu_128(flt0 + j);
1314 const __m128i flt0h = xx_loadu_128(flt0 + j + 4);
1315 const __m128i flt1l = xx_loadu_128(flt1 + j);
1316 const __m128i flt1h = xx_loadu_128(flt1 + j + 4);
1317 // flt0 = [7 6 5 4] [3 2 1 0] as i32 (indices of flt0+j)
1318 // flt1 = [7 6 5 4] [3 2 1 0] as i32 (indices of flt1+j)
1319
1320 // Subtract shifted corrupt image from each filtered image
1321 // This gives our two basis vectors for the projection
1322 const __m128i flt0l_subu = _mm_sub_epi32(flt0l, u0l);
1323 const __m128i flt0h_subu = _mm_sub_epi32(flt0h, u0h);
1324 const __m128i flt1l_subu = _mm_sub_epi32(flt1l, u0l);
1325 const __m128i flt1h_subu = _mm_sub_epi32(flt1h, u0h);
1326 // flt?h_subu = [ f[7]-u[7] f[6]-u[6] f[5]-u[5] f[4]-u[4] ] as i32
1327 // flt?l_subu = [ f[3]-u[3] f[2]-u[2] f[1]-u[1] f[0]-u[0] ] as i32
1328
1329 // Multiply each basis vector by the corresponding coefficient
1330 const __m128i v0l = _mm_mullo_epi32(flt0l_subu, xq0);
1331 const __m128i v0h = _mm_mullo_epi32(flt0h_subu, xq0);
1332 const __m128i v1l = _mm_mullo_epi32(flt1l_subu, xq1);
1333 const __m128i v1h = _mm_mullo_epi32(flt1h_subu, xq1);
1334
1335 // Add together the contribution from each scaled basis vector
1336 const __m128i vl = _mm_add_epi32(v0l, v1l);
1337 const __m128i vh = _mm_add_epi32(v0h, v1h);
1338
1339 // Right-shift v with appropriate rounding
1340 const __m128i vrl = _mm_srai_epi32(_mm_add_epi32(vl, rounding), shift);
1341 const __m128i vrh = _mm_srai_epi32(_mm_add_epi32(vh, rounding), shift);
1342
1343 // Saturate each i32 value to i16 and combine lower and upper halves
1344 const __m128i vr = _mm_packs_epi32(vrl, vrh);
1345
1346 // Add twin-subspace-sgr-filter to corrupt image then subtract source
1347 const __m128i e0 = _mm_sub_epi16(_mm_add_epi16(vr, d0), s0);
1348
1349 // Calculate squared error and add adjacent values
1350 const __m128i err0 = _mm_madd_epi16(e0, e0);
1351
1352 sum32 = _mm_add_epi32(sum32, err0);
1353 }
1354
1355 const __m128i sum32l = _mm_cvtepu32_epi64(sum32);
1356 sum64 = _mm_add_epi64(sum64, sum32l);
1357 const __m128i sum32h = _mm_cvtepu32_epi64(_mm_srli_si128(sum32, 8));
1358 sum64 = _mm_add_epi64(sum64, sum32h);
1359
1360 // Process remaining pixels in this row (modulo 8)
1361 for (k = j; k < width; ++k) {
1362 const int32_t u = (int32_t)(dat[k] << SGRPROJ_RST_BITS);
1363 int32_t v = xq[0] * (flt0[k] - u) + xq[1] * (flt1[k] - u);
1364 const int32_t e = ROUND_POWER_OF_TWO(v, shift) + dat[k] - src[k];
1365 err += ((int64_t)e * e);
1366 }
1367 dat += dat_stride;
1368 src += src_stride;
1369 flt0 += flt0_stride;
1370 flt1 += flt1_stride;
1371 }
1372 } else if (params->r[0] > 0 || params->r[1] > 0) { // Only one filter enabled
1373 const int32_t xq_on = (params->r[0] > 0) ? xq[0] : xq[1];
1374 const __m128i xq_active = _mm_set1_epi32(xq_on);
1375 const __m128i xq_inactive =
1376 _mm_set1_epi32(-xq_on * (1 << SGRPROJ_RST_BITS));
1377 const int32_t *flt = (params->r[0] > 0) ? flt0 : flt1;
1378 const int flt_stride = (params->r[0] > 0) ? flt0_stride : flt1_stride;
1379 for (i = 0; i < height; ++i) {
1380 __m128i sum32 = _mm_setzero_si128();
1381 for (j = 0; j <= width - 8; j += 8) {
1382 // Load 8x pixels from source image
1383 const __m128i s0 = xx_loadu_128(src + j);
1384 // s0 = [7 6 5 4 3 2 1 0] as u16 (indices of src[])
1385
1386 // Load 8x pixels from corrupted image and pad each u16 to i32
1387 const __m128i d0 = xx_loadu_128(dat + j);
1388 const __m128i d0h = _mm_cvtepu16_epi32(_mm_srli_si128(d0, 8));
1389 const __m128i d0l = _mm_cvtepu16_epi32(d0);
1390 // d0h, d0l = [7 6 5 4], [3 2 1 0] as u32 (indices of dat[])
1391
1392 // Load 8 pixels from the filtered image
1393 const __m128i flth = xx_loadu_128(flt + j + 4);
1394 const __m128i fltl = xx_loadu_128(flt + j);
1395 // flth, fltl = [7 6 5 4], [3 2 1 0] as i32 (indices of flt+j)
1396
1397 const __m128i flth_xq = _mm_mullo_epi32(flth, xq_active);
1398 const __m128i fltl_xq = _mm_mullo_epi32(fltl, xq_active);
1399 const __m128i d0h_xq = _mm_mullo_epi32(d0h, xq_inactive);
1400 const __m128i d0l_xq = _mm_mullo_epi32(d0l, xq_inactive);
1401
1402 const __m128i vh = _mm_add_epi32(flth_xq, d0h_xq);
1403 const __m128i vl = _mm_add_epi32(fltl_xq, d0l_xq);
1404 // vh = [ xq0(f[7]-d[7]) xq0(f[6]-d[6]) xq0(f[5]-d[5]) xq0(f[4]-d[4]) ]
1405 // vl = [ xq0(f[3]-d[3]) xq0(f[2]-d[2]) xq0(f[1]-d[1]) xq0(f[0]-d[0]) ]
1406
1407 // Shift this down with appropriate rounding
1408 const __m128i vrh = _mm_srai_epi32(_mm_add_epi32(vh, rounding), shift);
1409 const __m128i vrl = _mm_srai_epi32(_mm_add_epi32(vl, rounding), shift);
1410
1411 // Saturate vr0 and vr1 from i32 to i16 then pack together
1412 const __m128i vr = _mm_packs_epi32(vrl, vrh);
1413
1414 // Subtract twin-subspace-sgr filtered from source image to get error
1415 const __m128i e0 = _mm_sub_epi16(_mm_add_epi16(vr, d0), s0);
1416
1417 // Calculate squared error and add adjacent values
1418 const __m128i err0 = _mm_madd_epi16(e0, e0);
1419
1420 sum32 = _mm_add_epi32(sum32, err0);
1421 }
1422
1423 const __m128i sum32l = _mm_cvtepu32_epi64(sum32);
1424 sum64 = _mm_add_epi64(sum64, sum32l);
1425 const __m128i sum32h = _mm_cvtepu32_epi64(_mm_srli_si128(sum32, 8));
1426 sum64 = _mm_add_epi64(sum64, sum32h);
1427
1428 // Process remaining pixels in this row (modulo 8)
1429 for (k = j; k < width; ++k) {
1430 const int32_t u = (int32_t)(dat[k] << SGRPROJ_RST_BITS);
1431 int32_t v = xq_on * (flt[k] - u);
1432 const int32_t e = ROUND_POWER_OF_TWO(v, shift) + dat[k] - src[k];
1433 err += ((int64_t)e * e);
1434 }
1435 dat += dat_stride;
1436 src += src_stride;
1437 flt += flt_stride;
1438 }
1439 } else { // Neither filter is enabled
1440 for (i = 0; i < height; ++i) {
1441 __m128i sum32 = _mm_setzero_si128();
1442 for (j = 0; j <= width - 16; j += 16) {
1443 // Load 2x8 u16 from source image
1444 const __m128i s0 = xx_loadu_128(src + j);
1445 const __m128i s1 = xx_loadu_128(src + j + 8);
1446 // Load 2x8 u16 from corrupted image
1447 const __m128i d0 = xx_loadu_128(dat + j);
1448 const __m128i d1 = xx_loadu_128(dat + j + 8);
1449
1450 // Subtract corrupted image from source image
1451 const __m128i diff0 = _mm_sub_epi16(d0, s0);
1452 const __m128i diff1 = _mm_sub_epi16(d1, s1);
1453
1454 // Square error and add adjacent values
1455 const __m128i err0 = _mm_madd_epi16(diff0, diff0);
1456 const __m128i err1 = _mm_madd_epi16(diff1, diff1);
1457
1458 sum32 = _mm_add_epi32(sum32, err0);
1459 sum32 = _mm_add_epi32(sum32, err1);
1460 }
1461
1462 const __m128i sum32l = _mm_cvtepu32_epi64(sum32);
1463 sum64 = _mm_add_epi64(sum64, sum32l);
1464 const __m128i sum32h = _mm_cvtepu32_epi64(_mm_srli_si128(sum32, 8));
1465 sum64 = _mm_add_epi64(sum64, sum32h);
1466
1467 // Process remaining pixels (modulu 8)
1468 for (k = j; k < width; ++k) {
1469 const int32_t e = (int32_t)(dat[k]) - src[k];
1470 err += ((int64_t)e * e);
1471 }
1472 dat += dat_stride;
1473 src += src_stride;
1474 }
1475 }
1476
1477 // Sum 4 values from sum64l and sum64h into err
1478 int64_t sum[2];
1479 xx_storeu_128(sum, sum64);
1480 err += sum[0] + sum[1];
1481 return err;
1482 }
1483 #endif // CONFIG_AV1_HIGHBITDEPTH
1484