xref: /aosp_15_r20/external/libvpx/vpx_dsp/arm/variance_neon.c (revision fb1b10ab9aebc7c7068eedab379b749d7e3900be)
1 /*
2  *  Copyright (c) 2014 The WebM project authors. All Rights Reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10 
11 #include <arm_neon.h>
12 #include <assert.h>
13 
14 #include "./vpx_dsp_rtcd.h"
15 #include "./vpx_config.h"
16 
17 #include "vpx/vpx_integer.h"
18 #include "vpx_dsp/arm/mem_neon.h"
19 #include "vpx_dsp/arm/sum_neon.h"
20 #include "vpx_ports/mem.h"
21 
22 // Process a block of width 4 two rows at a time.
variance_4xh_neon(const uint8_t * src_ptr,int src_stride,const uint8_t * ref_ptr,int ref_stride,int h,uint32_t * sse,int * sum)23 static INLINE void variance_4xh_neon(const uint8_t *src_ptr, int src_stride,
24                                      const uint8_t *ref_ptr, int ref_stride,
25                                      int h, uint32_t *sse, int *sum) {
26   int16x8_t sum_s16 = vdupq_n_s16(0);
27   int32x4_t sse_s32 = vdupq_n_s32(0);
28   int i = h;
29 
30   // Number of rows we can process before 'sum_s16' overflows:
31   // 32767 / 255 ~= 128, but we use an 8-wide accumulator; so 256 4-wide rows.
32   assert(h <= 256);
33 
34   do {
35     const uint8x8_t s = load_unaligned_u8(src_ptr, src_stride);
36     const uint8x8_t r = load_unaligned_u8(ref_ptr, ref_stride);
37     const int16x8_t diff = vreinterpretq_s16_u16(vsubl_u8(s, r));
38 
39     sum_s16 = vaddq_s16(sum_s16, diff);
40 
41     sse_s32 = vmlal_s16(sse_s32, vget_low_s16(diff), vget_low_s16(diff));
42     sse_s32 = vmlal_s16(sse_s32, vget_high_s16(diff), vget_high_s16(diff));
43 
44     src_ptr += 2 * src_stride;
45     ref_ptr += 2 * ref_stride;
46     i -= 2;
47   } while (i != 0);
48 
49   *sum = horizontal_add_int16x8(sum_s16);
50   *sse = (uint32_t)horizontal_add_int32x4(sse_s32);
51 }
52 
53 // Process a block of width 8 one row at a time.
variance_8xh_neon(const uint8_t * src_ptr,int src_stride,const uint8_t * ref_ptr,int ref_stride,int h,uint32_t * sse,int * sum)54 static INLINE void variance_8xh_neon(const uint8_t *src_ptr, int src_stride,
55                                      const uint8_t *ref_ptr, int ref_stride,
56                                      int h, uint32_t *sse, int *sum) {
57   int16x8_t sum_s16 = vdupq_n_s16(0);
58   int32x4_t sse_s32[2] = { vdupq_n_s32(0), vdupq_n_s32(0) };
59   int i = h;
60 
61   // Number of rows we can process before 'sum_s16' overflows:
62   // 32767 / 255 ~= 128
63   assert(h <= 128);
64 
65   do {
66     const uint8x8_t s = vld1_u8(src_ptr);
67     const uint8x8_t r = vld1_u8(ref_ptr);
68     const int16x8_t diff = vreinterpretq_s16_u16(vsubl_u8(s, r));
69 
70     sum_s16 = vaddq_s16(sum_s16, diff);
71 
72     sse_s32[0] = vmlal_s16(sse_s32[0], vget_low_s16(diff), vget_low_s16(diff));
73     sse_s32[1] =
74         vmlal_s16(sse_s32[1], vget_high_s16(diff), vget_high_s16(diff));
75 
76     src_ptr += src_stride;
77     ref_ptr += ref_stride;
78   } while (--i != 0);
79 
80   *sum = horizontal_add_int16x8(sum_s16);
81   *sse = (uint32_t)horizontal_add_int32x4(vaddq_s32(sse_s32[0], sse_s32[1]));
82 }
83 
84 // Process a block of width 16 one row at a time.
variance_16xh_neon(const uint8_t * src_ptr,int src_stride,const uint8_t * ref_ptr,int ref_stride,int h,uint32_t * sse,int * sum)85 static INLINE void variance_16xh_neon(const uint8_t *src_ptr, int src_stride,
86                                       const uint8_t *ref_ptr, int ref_stride,
87                                       int h, uint32_t *sse, int *sum) {
88   int16x8_t sum_s16[2] = { vdupq_n_s16(0), vdupq_n_s16(0) };
89   int32x4_t sse_s32[2] = { vdupq_n_s32(0), vdupq_n_s32(0) };
90   int i = h;
91 
92   // Number of rows we can process before 'sum_s16' accumulators overflow:
93   // 32767 / 255 ~= 128, so 128 16-wide rows.
94   assert(h <= 128);
95 
96   do {
97     const uint8x16_t s = vld1q_u8(src_ptr);
98     const uint8x16_t r = vld1q_u8(ref_ptr);
99 
100     const int16x8_t diff_l =
101         vreinterpretq_s16_u16(vsubl_u8(vget_low_u8(s), vget_low_u8(r)));
102     const int16x8_t diff_h =
103         vreinterpretq_s16_u16(vsubl_u8(vget_high_u8(s), vget_high_u8(r)));
104 
105     sum_s16[0] = vaddq_s16(sum_s16[0], diff_l);
106     sum_s16[1] = vaddq_s16(sum_s16[1], diff_h);
107 
108     sse_s32[0] =
109         vmlal_s16(sse_s32[0], vget_low_s16(diff_l), vget_low_s16(diff_l));
110     sse_s32[1] =
111         vmlal_s16(sse_s32[1], vget_high_s16(diff_l), vget_high_s16(diff_l));
112     sse_s32[0] =
113         vmlal_s16(sse_s32[0], vget_low_s16(diff_h), vget_low_s16(diff_h));
114     sse_s32[1] =
115         vmlal_s16(sse_s32[1], vget_high_s16(diff_h), vget_high_s16(diff_h));
116 
117     src_ptr += src_stride;
118     ref_ptr += ref_stride;
119   } while (--i != 0);
120 
121   *sum = horizontal_add_int16x8(vaddq_s16(sum_s16[0], sum_s16[1]));
122   *sse = (uint32_t)horizontal_add_int32x4(vaddq_s32(sse_s32[0], sse_s32[1]));
123 }
124 
125 // Process a block of any size where the width is divisible by 16.
variance_large_neon(const uint8_t * src_ptr,int src_stride,const uint8_t * ref_ptr,int ref_stride,int w,int h,int h_limit,unsigned int * sse,int * sum)126 static INLINE void variance_large_neon(const uint8_t *src_ptr, int src_stride,
127                                        const uint8_t *ref_ptr, int ref_stride,
128                                        int w, int h, int h_limit,
129                                        unsigned int *sse, int *sum) {
130   int32x4_t sum_s32 = vdupq_n_s32(0);
131   int32x4_t sse_s32[2] = { vdupq_n_s32(0), vdupq_n_s32(0) };
132 
133   // 'h_limit' is the number of 'w'-width rows we can process before our 16-bit
134   // accumulator overflows. After hitting this limit we accumulate into 32-bit
135   // elements.
136   int h_tmp = h > h_limit ? h_limit : h;
137 
138   int i = 0;
139   do {
140     int16x8_t sum_s16[2] = { vdupq_n_s16(0), vdupq_n_s16(0) };
141     do {
142       int j = 0;
143       do {
144         const uint8x16_t s = vld1q_u8(src_ptr + j);
145         const uint8x16_t r = vld1q_u8(ref_ptr + j);
146 
147         const int16x8_t diff_l =
148             vreinterpretq_s16_u16(vsubl_u8(vget_low_u8(s), vget_low_u8(r)));
149         const int16x8_t diff_h =
150             vreinterpretq_s16_u16(vsubl_u8(vget_high_u8(s), vget_high_u8(r)));
151 
152         sum_s16[0] = vaddq_s16(sum_s16[0], diff_l);
153         sum_s16[1] = vaddq_s16(sum_s16[1], diff_h);
154 
155         sse_s32[0] =
156             vmlal_s16(sse_s32[0], vget_low_s16(diff_l), vget_low_s16(diff_l));
157         sse_s32[1] =
158             vmlal_s16(sse_s32[1], vget_high_s16(diff_l), vget_high_s16(diff_l));
159         sse_s32[0] =
160             vmlal_s16(sse_s32[0], vget_low_s16(diff_h), vget_low_s16(diff_h));
161         sse_s32[1] =
162             vmlal_s16(sse_s32[1], vget_high_s16(diff_h), vget_high_s16(diff_h));
163 
164         j += 16;
165       } while (j < w);
166 
167       src_ptr += src_stride;
168       ref_ptr += ref_stride;
169       i++;
170     } while (i < h_tmp);
171 
172     sum_s32 = vpadalq_s16(sum_s32, sum_s16[0]);
173     sum_s32 = vpadalq_s16(sum_s32, sum_s16[1]);
174 
175     h_tmp += h_limit;
176   } while (i < h);
177 
178   *sum = horizontal_add_int32x4(sum_s32);
179   *sse = (uint32_t)horizontal_add_int32x4(vaddq_s32(sse_s32[0], sse_s32[1]));
180 }
181 
variance_32xh_neon(const uint8_t * src,int src_stride,const uint8_t * ref,int ref_stride,int h,uint32_t * sse,int * sum)182 static INLINE void variance_32xh_neon(const uint8_t *src, int src_stride,
183                                       const uint8_t *ref, int ref_stride, int h,
184                                       uint32_t *sse, int *sum) {
185   variance_large_neon(src, src_stride, ref, ref_stride, 32, h, 64, sse, sum);
186 }
187 
variance_64xh_neon(const uint8_t * src,int src_stride,const uint8_t * ref,int ref_stride,int h,uint32_t * sse,int * sum)188 static INLINE void variance_64xh_neon(const uint8_t *src, int src_stride,
189                                       const uint8_t *ref, int ref_stride, int h,
190                                       uint32_t *sse, int *sum) {
191   variance_large_neon(src, src_stride, ref, ref_stride, 64, h, 32, sse, sum);
192 }
193 
vpx_get8x8var_neon(const uint8_t * src_ptr,int src_stride,const uint8_t * ref_ptr,int ref_stride,unsigned int * sse,int * sum)194 void vpx_get8x8var_neon(const uint8_t *src_ptr, int src_stride,
195                         const uint8_t *ref_ptr, int ref_stride,
196                         unsigned int *sse, int *sum) {
197   variance_8xh_neon(src_ptr, src_stride, ref_ptr, ref_stride, 8, sse, sum);
198 }
199 
vpx_get16x16var_neon(const uint8_t * src_ptr,int src_stride,const uint8_t * ref_ptr,int ref_stride,unsigned int * sse,int * sum)200 void vpx_get16x16var_neon(const uint8_t *src_ptr, int src_stride,
201                           const uint8_t *ref_ptr, int ref_stride,
202                           unsigned int *sse, int *sum) {
203   variance_16xh_neon(src_ptr, src_stride, ref_ptr, ref_stride, 16, sse, sum);
204 }
205 
206 #define VARIANCE_WXH_NEON(w, h, shift)                                        \
207   unsigned int vpx_variance##w##x##h##_neon(                                  \
208       const uint8_t *src, int src_stride, const uint8_t *ref, int ref_stride, \
209       unsigned int *sse) {                                                    \
210     int sum;                                                                  \
211     variance_##w##xh_neon(src, src_stride, ref, ref_stride, h, sse, &sum);    \
212     return *sse - (uint32_t)(((int64_t)sum * sum) >> shift);                  \
213   }
214 
215 VARIANCE_WXH_NEON(4, 4, 4)
216 VARIANCE_WXH_NEON(4, 8, 5)
217 
218 VARIANCE_WXH_NEON(8, 4, 5)
219 VARIANCE_WXH_NEON(8, 8, 6)
220 VARIANCE_WXH_NEON(8, 16, 7)
221 
222 VARIANCE_WXH_NEON(16, 8, 7)
223 VARIANCE_WXH_NEON(16, 16, 8)
224 VARIANCE_WXH_NEON(16, 32, 9)
225 
226 VARIANCE_WXH_NEON(32, 16, 9)
227 VARIANCE_WXH_NEON(32, 32, 10)
228 VARIANCE_WXH_NEON(32, 64, 11)
229 
230 VARIANCE_WXH_NEON(64, 32, 11)
231 VARIANCE_WXH_NEON(64, 64, 12)
232 
233 #undef VARIANCE_WXH_NEON
234 
vpx_mse8xh_neon(const unsigned char * src_ptr,int src_stride,const unsigned char * ref_ptr,int ref_stride,int h)235 static INLINE unsigned int vpx_mse8xh_neon(const unsigned char *src_ptr,
236                                            int src_stride,
237                                            const unsigned char *ref_ptr,
238                                            int ref_stride, int h) {
239   uint32x4_t sse_u32[2] = { vdupq_n_u32(0), vdupq_n_u32(0) };
240 
241   int i = h / 2;
242   do {
243     uint8x8_t s0, s1, r0, r1, diff0, diff1;
244     uint16x8_t sse0, sse1;
245 
246     s0 = vld1_u8(src_ptr);
247     src_ptr += src_stride;
248     s1 = vld1_u8(src_ptr);
249     src_ptr += src_stride;
250     r0 = vld1_u8(ref_ptr);
251     ref_ptr += ref_stride;
252     r1 = vld1_u8(ref_ptr);
253     ref_ptr += ref_stride;
254 
255     diff0 = vabd_u8(s0, r0);
256     diff1 = vabd_u8(s1, r1);
257 
258     sse0 = vmull_u8(diff0, diff0);
259     sse_u32[0] = vpadalq_u16(sse_u32[0], sse0);
260     sse1 = vmull_u8(diff1, diff1);
261     sse_u32[1] = vpadalq_u16(sse_u32[1], sse1);
262   } while (--i != 0);
263 
264   return horizontal_add_uint32x4(vaddq_u32(sse_u32[0], sse_u32[1]));
265 }
266 
vpx_mse16xh_neon(const unsigned char * src_ptr,int src_stride,const unsigned char * ref_ptr,int ref_stride,int h)267 static INLINE unsigned int vpx_mse16xh_neon(const unsigned char *src_ptr,
268                                             int src_stride,
269                                             const unsigned char *ref_ptr,
270                                             int ref_stride, int h) {
271   uint32x4_t sse_u32[2] = { vdupq_n_u32(0), vdupq_n_u32(0) };
272 
273   int i = h;
274   do {
275     uint8x16_t s, r, diff;
276     uint16x8_t sse0, sse1;
277 
278     s = vld1q_u8(src_ptr);
279     src_ptr += src_stride;
280     r = vld1q_u8(ref_ptr);
281     ref_ptr += ref_stride;
282 
283     diff = vabdq_u8(s, r);
284 
285     sse0 = vmull_u8(vget_low_u8(diff), vget_low_u8(diff));
286     sse_u32[0] = vpadalq_u16(sse_u32[0], sse0);
287     sse1 = vmull_u8(vget_high_u8(diff), vget_high_u8(diff));
288     sse_u32[1] = vpadalq_u16(sse_u32[1], sse1);
289   } while (--i != 0);
290 
291   return horizontal_add_uint32x4(vaddq_u32(sse_u32[0], sse_u32[1]));
292 }
293 
vpx_get4x4sse_cs_neon(const unsigned char * src_ptr,int src_stride,const unsigned char * ref_ptr,int ref_stride)294 unsigned int vpx_get4x4sse_cs_neon(const unsigned char *src_ptr, int src_stride,
295                                    const unsigned char *ref_ptr,
296                                    int ref_stride) {
297   uint8x8_t s[2], r[2];
298   uint16x8_t abs_diff[2];
299   uint32x4_t sse;
300 
301   s[0] = load_u8(src_ptr, src_stride);
302   r[0] = load_u8(ref_ptr, ref_stride);
303   src_ptr += 2 * src_stride;
304   ref_ptr += 2 * ref_stride;
305   s[1] = load_u8(src_ptr, src_stride);
306   r[1] = load_u8(ref_ptr, ref_stride);
307 
308   abs_diff[0] = vabdl_u8(s[0], r[0]);
309   abs_diff[1] = vabdl_u8(s[1], r[1]);
310 
311   sse = vmull_u16(vget_low_u16(abs_diff[0]), vget_low_u16(abs_diff[0]));
312   sse = vmlal_u16(sse, vget_high_u16(abs_diff[0]), vget_high_u16(abs_diff[0]));
313   sse = vmlal_u16(sse, vget_low_u16(abs_diff[1]), vget_low_u16(abs_diff[1]));
314   sse = vmlal_u16(sse, vget_high_u16(abs_diff[1]), vget_high_u16(abs_diff[1]));
315 
316   return horizontal_add_uint32x4(sse);
317 }
318 
319 #define VPX_MSE_WXH_NEON(w, h)                                               \
320   unsigned int vpx_mse##w##x##h##_neon(                                      \
321       const unsigned char *src_ptr, int src_stride,                          \
322       const unsigned char *ref_ptr, int ref_stride, unsigned int *sse) {     \
323     *sse = vpx_mse##w##xh_neon(src_ptr, src_stride, ref_ptr, ref_stride, h); \
324     return *sse;                                                             \
325   }
326 
327 VPX_MSE_WXH_NEON(8, 8)
328 VPX_MSE_WXH_NEON(8, 16)
329 VPX_MSE_WXH_NEON(16, 8)
330 VPX_MSE_WXH_NEON(16, 16)
331 
332 #undef VPX_MSE_WXH_NEON
333