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