2 * Copyright (c) 2015 The WebM project authors. All Rights Reserved.
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.
14 #include "./vpx_dsp_rtcd.h"
15 #include "./vpx_config.h"
17 #include "vpx/vpx_integer.h"
18 #include "vpx_dsp/arm/idct_neon.h"
20 static INLINE unsigned int horizontal_add_u16x8(const uint16x8_t v_16x8) {
21 const uint32x4_t a = vpaddlq_u16(v_16x8);
22 const uint64x2_t b = vpaddlq_u32(a);
23 const uint32x2_t c = vadd_u32(vreinterpret_u32_u64(vget_low_u64(b)),
24 vreinterpret_u32_u64(vget_high_u64(b)));
25 return vget_lane_u32(c, 0);
28 unsigned int vpx_avg_4x4_neon(const uint8_t *s, int p) {
30 uint32x2_t v_s0 = vdup_n_u32(0);
31 uint32x2_t v_s1 = vdup_n_u32(0);
32 v_s0 = vld1_lane_u32((const uint32_t *)s, v_s0, 0);
33 v_s0 = vld1_lane_u32((const uint32_t *)(s + p), v_s0, 1);
34 v_s1 = vld1_lane_u32((const uint32_t *)(s + 2 * p), v_s1, 0);
35 v_s1 = vld1_lane_u32((const uint32_t *)(s + 3 * p), v_s1, 1);
36 v_sum = vaddl_u8(vreinterpret_u8_u32(v_s0), vreinterpret_u8_u32(v_s1));
37 return (horizontal_add_u16x8(v_sum) + 8) >> 4;
40 unsigned int vpx_avg_8x8_neon(const uint8_t *s, int p) {
41 uint8x8_t v_s0 = vld1_u8(s);
42 const uint8x8_t v_s1 = vld1_u8(s + p);
43 uint16x8_t v_sum = vaddl_u8(v_s0, v_s1);
45 v_s0 = vld1_u8(s + 2 * p);
46 v_sum = vaddw_u8(v_sum, v_s0);
48 v_s0 = vld1_u8(s + 3 * p);
49 v_sum = vaddw_u8(v_sum, v_s0);
51 v_s0 = vld1_u8(s + 4 * p);
52 v_sum = vaddw_u8(v_sum, v_s0);
54 v_s0 = vld1_u8(s + 5 * p);
55 v_sum = vaddw_u8(v_sum, v_s0);
57 v_s0 = vld1_u8(s + 6 * p);
58 v_sum = vaddw_u8(v_sum, v_s0);
60 v_s0 = vld1_u8(s + 7 * p);
61 v_sum = vaddw_u8(v_sum, v_s0);
63 return (horizontal_add_u16x8(v_sum) + 32) >> 6;
66 // coeff: 16 bits, dynamic range [-32640, 32640].
67 // length: value range {16, 64, 256, 1024}.
68 int vpx_satd_neon(const tran_low_t *coeff, int length) {
69 const int16x4_t zero = vdup_n_s16(0);
70 int32x4_t accum = vdupq_n_s32(0);
73 const int16x8_t src0 = load_tran_low_to_s16q(coeff);
74 const int16x8_t src8 = load_tran_low_to_s16q(coeff + 8);
75 accum = vabal_s16(accum, vget_low_s16(src0), zero);
76 accum = vabal_s16(accum, vget_high_s16(src0), zero);
77 accum = vabal_s16(accum, vget_low_s16(src8), zero);
78 accum = vabal_s16(accum, vget_high_s16(src8), zero);
81 } while (length != 0);
84 // satd: 26 bits, dynamic range [-32640 * 1024, 32640 * 1024]
85 const int64x2_t s0 = vpaddlq_s32(accum); // cascading summation of 'accum'.
86 const int32x2_t s1 = vadd_s32(vreinterpret_s32_s64(vget_low_s64(s0)),
87 vreinterpret_s32_s64(vget_high_s64(s0)));
88 const int satd = vget_lane_s32(s1, 0);
93 void vpx_int_pro_row_neon(int16_t hbuf[16], uint8_t const *ref,
94 const int ref_stride, const int height) {
96 uint16x8_t vec_sum_lo = vdupq_n_u16(0);
97 uint16x8_t vec_sum_hi = vdupq_n_u16(0);
98 const int shift_factor = ((height >> 5) + 3) * -1;
99 const int16x8_t vec_shift = vdupq_n_s16(shift_factor);
101 for (i = 0; i < height; i += 8) {
102 const uint8x16_t vec_row1 = vld1q_u8(ref);
103 const uint8x16_t vec_row2 = vld1q_u8(ref + ref_stride);
104 const uint8x16_t vec_row3 = vld1q_u8(ref + ref_stride * 2);
105 const uint8x16_t vec_row4 = vld1q_u8(ref + ref_stride * 3);
106 const uint8x16_t vec_row5 = vld1q_u8(ref + ref_stride * 4);
107 const uint8x16_t vec_row6 = vld1q_u8(ref + ref_stride * 5);
108 const uint8x16_t vec_row7 = vld1q_u8(ref + ref_stride * 6);
109 const uint8x16_t vec_row8 = vld1q_u8(ref + ref_stride * 7);
111 vec_sum_lo = vaddw_u8(vec_sum_lo, vget_low_u8(vec_row1));
112 vec_sum_hi = vaddw_u8(vec_sum_hi, vget_high_u8(vec_row1));
114 vec_sum_lo = vaddw_u8(vec_sum_lo, vget_low_u8(vec_row2));
115 vec_sum_hi = vaddw_u8(vec_sum_hi, vget_high_u8(vec_row2));
117 vec_sum_lo = vaddw_u8(vec_sum_lo, vget_low_u8(vec_row3));
118 vec_sum_hi = vaddw_u8(vec_sum_hi, vget_high_u8(vec_row3));
120 vec_sum_lo = vaddw_u8(vec_sum_lo, vget_low_u8(vec_row4));
121 vec_sum_hi = vaddw_u8(vec_sum_hi, vget_high_u8(vec_row4));
123 vec_sum_lo = vaddw_u8(vec_sum_lo, vget_low_u8(vec_row5));
124 vec_sum_hi = vaddw_u8(vec_sum_hi, vget_high_u8(vec_row5));
126 vec_sum_lo = vaddw_u8(vec_sum_lo, vget_low_u8(vec_row6));
127 vec_sum_hi = vaddw_u8(vec_sum_hi, vget_high_u8(vec_row6));
129 vec_sum_lo = vaddw_u8(vec_sum_lo, vget_low_u8(vec_row7));
130 vec_sum_hi = vaddw_u8(vec_sum_hi, vget_high_u8(vec_row7));
132 vec_sum_lo = vaddw_u8(vec_sum_lo, vget_low_u8(vec_row8));
133 vec_sum_hi = vaddw_u8(vec_sum_hi, vget_high_u8(vec_row8));
135 ref += ref_stride * 8;
138 vec_sum_lo = vshlq_u16(vec_sum_lo, vec_shift);
139 vec_sum_hi = vshlq_u16(vec_sum_hi, vec_shift);
141 vst1q_s16(hbuf, vreinterpretq_s16_u16(vec_sum_lo));
143 vst1q_s16(hbuf, vreinterpretq_s16_u16(vec_sum_hi));
146 int16_t vpx_int_pro_col_neon(uint8_t const *ref, const int width) {
148 uint16x8_t vec_sum = vdupq_n_u16(0);
150 for (i = 0; i < width; i += 16) {
151 const uint8x16_t vec_row = vld1q_u8(ref);
152 vec_sum = vaddw_u8(vec_sum, vget_low_u8(vec_row));
153 vec_sum = vaddw_u8(vec_sum, vget_high_u8(vec_row));
157 return horizontal_add_u16x8(vec_sum);
160 // ref, src = [0, 510] - max diff = 16-bits
161 // bwl = {2, 3, 4}, width = {16, 32, 64}
162 int vpx_vector_var_neon(int16_t const *ref, int16_t const *src, const int bwl) {
163 int width = 4 << bwl;
164 int32x4_t sse = vdupq_n_s32(0);
165 int16x8_t total = vdupq_n_s16(0);
168 assert((width % 8) == 0);
171 const int16x8_t r = vld1q_s16(ref);
172 const int16x8_t s = vld1q_s16(src);
173 const int16x8_t diff = vsubq_s16(r, s); // [-510, 510], 10 bits.
174 const int16x4_t diff_lo = vget_low_s16(diff);
175 const int16x4_t diff_hi = vget_high_s16(diff);
176 sse = vmlal_s16(sse, diff_lo, diff_lo); // dynamic range 26 bits.
177 sse = vmlal_s16(sse, diff_hi, diff_hi);
178 total = vaddq_s16(total, diff); // dynamic range 16 bits.
183 } while (width != 0);
186 // Note: 'total''s pairwise addition could be implemented similarly to
187 // horizontal_add_u16x8(), but one less vpaddl with 'total' when paired
188 // with the summation of 'sse' performed better on a Cortex-A15.
189 const int32x4_t t0 = vpaddlq_s16(total); // cascading summation of 'total'
190 const int32x2_t t1 = vadd_s32(vget_low_s32(t0), vget_high_s32(t0));
191 const int32x2_t t2 = vpadd_s32(t1, t1);
192 const int t = vget_lane_s32(t2, 0);
193 const int64x2_t s0 = vpaddlq_s32(sse); // cascading summation of 'sse'.
194 const int32x2_t s1 = vadd_s32(vreinterpret_s32_s64(vget_low_s64(s0)),
195 vreinterpret_s32_s64(vget_high_s64(s0)));
196 const int s = vget_lane_s32(s1, 0);
197 const int shift_factor = bwl + 2;
198 return s - ((t * t) >> shift_factor);
202 void vpx_minmax_8x8_neon(const uint8_t *a, int a_stride, const uint8_t *b,
203 int b_stride, int *min, int *max) {
204 // Load and concatenate.
205 const uint8x16_t a01 = vcombine_u8(vld1_u8(a), vld1_u8(a + a_stride));
206 const uint8x16_t a23 =
207 vcombine_u8(vld1_u8(a + 2 * a_stride), vld1_u8(a + 3 * a_stride));
208 const uint8x16_t a45 =
209 vcombine_u8(vld1_u8(a + 4 * a_stride), vld1_u8(a + 5 * a_stride));
210 const uint8x16_t a67 =
211 vcombine_u8(vld1_u8(a + 6 * a_stride), vld1_u8(a + 7 * a_stride));
213 const uint8x16_t b01 = vcombine_u8(vld1_u8(b), vld1_u8(b + b_stride));
214 const uint8x16_t b23 =
215 vcombine_u8(vld1_u8(b + 2 * b_stride), vld1_u8(b + 3 * b_stride));
216 const uint8x16_t b45 =
217 vcombine_u8(vld1_u8(b + 4 * b_stride), vld1_u8(b + 5 * b_stride));
218 const uint8x16_t b67 =
219 vcombine_u8(vld1_u8(b + 6 * b_stride), vld1_u8(b + 7 * b_stride));
221 // Absolute difference.
222 const uint8x16_t ab01_diff = vabdq_u8(a01, b01);
223 const uint8x16_t ab23_diff = vabdq_u8(a23, b23);
224 const uint8x16_t ab45_diff = vabdq_u8(a45, b45);
225 const uint8x16_t ab67_diff = vabdq_u8(a67, b67);
227 // Max values between the Q vectors.
228 const uint8x16_t ab0123_max = vmaxq_u8(ab01_diff, ab23_diff);
229 const uint8x16_t ab4567_max = vmaxq_u8(ab45_diff, ab67_diff);
230 const uint8x16_t ab0123_min = vminq_u8(ab01_diff, ab23_diff);
231 const uint8x16_t ab4567_min = vminq_u8(ab45_diff, ab67_diff);
233 const uint8x16_t ab07_max = vmaxq_u8(ab0123_max, ab4567_max);
234 const uint8x16_t ab07_min = vminq_u8(ab0123_min, ab4567_min);
236 // Split to D and start doing pairwise.
237 uint8x8_t ab_max = vmax_u8(vget_high_u8(ab07_max), vget_low_u8(ab07_max));
238 uint8x8_t ab_min = vmin_u8(vget_high_u8(ab07_min), vget_low_u8(ab07_min));
240 // Enough runs of vpmax/min propogate the max/min values to every position.
241 ab_max = vpmax_u8(ab_max, ab_max);
242 ab_min = vpmin_u8(ab_min, ab_min);
244 ab_max = vpmax_u8(ab_max, ab_max);
245 ab_min = vpmin_u8(ab_min, ab_min);
247 ab_max = vpmax_u8(ab_max, ab_max);
248 ab_min = vpmin_u8(ab_min, ab_min);
250 *min = *max = 0; // Clear high bits
251 // Store directly to avoid costly neon->gpr transfer.
252 vst1_lane_u8((uint8_t *)max, ab_max, 0);
253 vst1_lane_u8((uint8_t *)min, ab_min, 0);