xref: /aosp_15_r20/external/libaom/av1/common/arm/convolve_sve2.c (revision 77c1e3ccc04c968bd2bc212e87364f250e820521)
1*77c1e3ccSAndroid Build Coastguard Worker /*
2*77c1e3ccSAndroid Build Coastguard Worker  * Copyright (c) 2024, Alliance for Open Media. All rights reserved.
3*77c1e3ccSAndroid Build Coastguard Worker  *
4*77c1e3ccSAndroid Build Coastguard Worker  * This source code is subject to the terms of the BSD 2 Clause License and
5*77c1e3ccSAndroid Build Coastguard Worker  * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6*77c1e3ccSAndroid Build Coastguard Worker  * was not distributed with this source code in the LICENSE file, you can
7*77c1e3ccSAndroid Build Coastguard Worker  * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8*77c1e3ccSAndroid Build Coastguard Worker  * Media Patent License 1.0 was not distributed with this source code in the
9*77c1e3ccSAndroid Build Coastguard Worker  * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
10*77c1e3ccSAndroid Build Coastguard Worker  */
11*77c1e3ccSAndroid Build Coastguard Worker 
12*77c1e3ccSAndroid Build Coastguard Worker #include <arm_neon.h>
13*77c1e3ccSAndroid Build Coastguard Worker #include <assert.h>
14*77c1e3ccSAndroid Build Coastguard Worker 
15*77c1e3ccSAndroid Build Coastguard Worker #include "config/aom_config.h"
16*77c1e3ccSAndroid Build Coastguard Worker #include "config/av1_rtcd.h"
17*77c1e3ccSAndroid Build Coastguard Worker 
18*77c1e3ccSAndroid Build Coastguard Worker #include "aom/aom_integer.h"
19*77c1e3ccSAndroid Build Coastguard Worker #include "aom_dsp/aom_dsp_common.h"
20*77c1e3ccSAndroid Build Coastguard Worker #include "aom_dsp/aom_filter.h"
21*77c1e3ccSAndroid Build Coastguard Worker #include "aom_dsp/arm/aom_filter.h"
22*77c1e3ccSAndroid Build Coastguard Worker #include "aom_dsp/arm/aom_neon_sve_bridge.h"
23*77c1e3ccSAndroid Build Coastguard Worker #include "aom_dsp/arm/mem_neon.h"
24*77c1e3ccSAndroid Build Coastguard Worker #include "aom_dsp/arm/transpose_neon.h"
25*77c1e3ccSAndroid Build Coastguard Worker #include "aom_ports/mem.h"
26*77c1e3ccSAndroid Build Coastguard Worker #include "av1/common/arm/highbd_convolve_sve2.h"
27*77c1e3ccSAndroid Build Coastguard Worker #include "av1/common/arm/convolve_neon_i8mm.h"
28*77c1e3ccSAndroid Build Coastguard Worker 
highbd_convolve12_4_2d_v(int16x8_t s0[2],int16x8_t s1[2],int16x8_t s2[2],int16x8_t filter_0_7,int16x8_t filter_4_11)29*77c1e3ccSAndroid Build Coastguard Worker static inline int32x4_t highbd_convolve12_4_2d_v(int16x8_t s0[2],
30*77c1e3ccSAndroid Build Coastguard Worker                                                  int16x8_t s1[2],
31*77c1e3ccSAndroid Build Coastguard Worker                                                  int16x8_t s2[2],
32*77c1e3ccSAndroid Build Coastguard Worker                                                  int16x8_t filter_0_7,
33*77c1e3ccSAndroid Build Coastguard Worker                                                  int16x8_t filter_4_11) {
34*77c1e3ccSAndroid Build Coastguard Worker   int64x2_t sum01 = aom_svdot_lane_s16(vdupq_n_s64(0), s0[0], filter_0_7, 0);
35*77c1e3ccSAndroid Build Coastguard Worker   sum01 = aom_svdot_lane_s16(sum01, s1[0], filter_0_7, 1);
36*77c1e3ccSAndroid Build Coastguard Worker   sum01 = aom_svdot_lane_s16(sum01, s2[0], filter_4_11, 1);
37*77c1e3ccSAndroid Build Coastguard Worker 
38*77c1e3ccSAndroid Build Coastguard Worker   int64x2_t sum23 = aom_svdot_lane_s16(vdupq_n_s64(0), s0[1], filter_0_7, 0);
39*77c1e3ccSAndroid Build Coastguard Worker   sum23 = aom_svdot_lane_s16(sum23, s1[1], filter_0_7, 1);
40*77c1e3ccSAndroid Build Coastguard Worker   sum23 = aom_svdot_lane_s16(sum23, s2[1], filter_4_11, 1);
41*77c1e3ccSAndroid Build Coastguard Worker 
42*77c1e3ccSAndroid Build Coastguard Worker   return vcombine_s32(vmovn_s64(sum01), vmovn_s64(sum23));
43*77c1e3ccSAndroid Build Coastguard Worker }
44*77c1e3ccSAndroid Build Coastguard Worker 
convolve_2d_sr_vert_12tap_sve2(const int16_t * src_ptr,int src_stride,uint8_t * dst_ptr,const int dst_stride,int w,int h,const int16x8_t y_filter_0_7,const int16x8_t y_filter_4_11)45*77c1e3ccSAndroid Build Coastguard Worker static inline void convolve_2d_sr_vert_12tap_sve2(
46*77c1e3ccSAndroid Build Coastguard Worker     const int16_t *src_ptr, int src_stride, uint8_t *dst_ptr,
47*77c1e3ccSAndroid Build Coastguard Worker     const int dst_stride, int w, int h, const int16x8_t y_filter_0_7,
48*77c1e3ccSAndroid Build Coastguard Worker     const int16x8_t y_filter_4_11) {
49*77c1e3ccSAndroid Build Coastguard Worker   // The no-op filter should never be used here.
50*77c1e3ccSAndroid Build Coastguard Worker   assert(vgetq_lane_s16(y_filter_0_7, 5) != 128);
51*77c1e3ccSAndroid Build Coastguard Worker 
52*77c1e3ccSAndroid Build Coastguard Worker   const int bd = 8;
53*77c1e3ccSAndroid Build Coastguard Worker   const int16x8_t sub_const = vdupq_n_s16(1 << (bd - 1));
54*77c1e3ccSAndroid Build Coastguard Worker 
55*77c1e3ccSAndroid Build Coastguard Worker   uint16x8x3_t merge_block_tbl = vld1q_u16_x3(kDotProdMergeBlockTbl);
56*77c1e3ccSAndroid Build Coastguard Worker   // Scale indices by size of the true vector length to avoid reading from an
57*77c1e3ccSAndroid Build Coastguard Worker   // 'undefined' portion of a vector on a system with SVE vectors > 128-bit.
58*77c1e3ccSAndroid Build Coastguard Worker   uint16x8_t correction0 =
59*77c1e3ccSAndroid Build Coastguard Worker       vreinterpretq_u16_u64(vdupq_n_u64(svcnth() * 0x0001000000000000ULL));
60*77c1e3ccSAndroid Build Coastguard Worker   merge_block_tbl.val[0] = vaddq_u16(merge_block_tbl.val[0], correction0);
61*77c1e3ccSAndroid Build Coastguard Worker 
62*77c1e3ccSAndroid Build Coastguard Worker   uint16x8_t correction1 =
63*77c1e3ccSAndroid Build Coastguard Worker       vreinterpretq_u16_u64(vdupq_n_u64(svcnth() * 0x0001000100000000ULL));
64*77c1e3ccSAndroid Build Coastguard Worker   merge_block_tbl.val[1] = vaddq_u16(merge_block_tbl.val[1], correction1);
65*77c1e3ccSAndroid Build Coastguard Worker 
66*77c1e3ccSAndroid Build Coastguard Worker   uint16x8_t correction2 =
67*77c1e3ccSAndroid Build Coastguard Worker       vreinterpretq_u16_u64(vdupq_n_u64(svcnth() * 0x0001000100010000ULL));
68*77c1e3ccSAndroid Build Coastguard Worker   merge_block_tbl.val[2] = vaddq_u16(merge_block_tbl.val[2], correction2);
69*77c1e3ccSAndroid Build Coastguard Worker 
70*77c1e3ccSAndroid Build Coastguard Worker   do {
71*77c1e3ccSAndroid Build Coastguard Worker     int16_t *s = (int16_t *)src_ptr;
72*77c1e3ccSAndroid Build Coastguard Worker     uint8_t *d = (uint8_t *)dst_ptr;
73*77c1e3ccSAndroid Build Coastguard Worker     int height = h;
74*77c1e3ccSAndroid Build Coastguard Worker 
75*77c1e3ccSAndroid Build Coastguard Worker     int16x4_t s0, s1, s2, s3, s4, s5, s6, s7, s8, s9, sA;
76*77c1e3ccSAndroid Build Coastguard Worker     load_s16_4x11(s, src_stride, &s0, &s1, &s2, &s3, &s4, &s5, &s6, &s7, &s8,
77*77c1e3ccSAndroid Build Coastguard Worker                   &s9, &sA);
78*77c1e3ccSAndroid Build Coastguard Worker     s += 11 * src_stride;
79*77c1e3ccSAndroid Build Coastguard Worker 
80*77c1e3ccSAndroid Build Coastguard Worker     int16x8_t s0123[2], s1234[2], s2345[2], s3456[2], s4567[2], s5678[2],
81*77c1e3ccSAndroid Build Coastguard Worker         s6789[2], s789A[2];
82*77c1e3ccSAndroid Build Coastguard Worker     // This operation combines a conventional transpose and the sample permute
83*77c1e3ccSAndroid Build Coastguard Worker     // required before computing the dot product.
84*77c1e3ccSAndroid Build Coastguard Worker     transpose_concat_4x4(s0, s1, s2, s3, s0123);
85*77c1e3ccSAndroid Build Coastguard Worker     transpose_concat_4x4(s1, s2, s3, s4, s1234);
86*77c1e3ccSAndroid Build Coastguard Worker     transpose_concat_4x4(s2, s3, s4, s5, s2345);
87*77c1e3ccSAndroid Build Coastguard Worker     transpose_concat_4x4(s3, s4, s5, s6, s3456);
88*77c1e3ccSAndroid Build Coastguard Worker     transpose_concat_4x4(s4, s5, s6, s7, s4567);
89*77c1e3ccSAndroid Build Coastguard Worker     transpose_concat_4x4(s5, s6, s7, s8, s5678);
90*77c1e3ccSAndroid Build Coastguard Worker     transpose_concat_4x4(s6, s7, s8, s9, s6789);
91*77c1e3ccSAndroid Build Coastguard Worker     transpose_concat_4x4(s7, s8, s9, sA, s789A);
92*77c1e3ccSAndroid Build Coastguard Worker 
93*77c1e3ccSAndroid Build Coastguard Worker     do {
94*77c1e3ccSAndroid Build Coastguard Worker       int16x4_t sB, sC, sD, sE;
95*77c1e3ccSAndroid Build Coastguard Worker       load_s16_4x4(s, src_stride, &sB, &sC, &sD, &sE);
96*77c1e3ccSAndroid Build Coastguard Worker 
97*77c1e3ccSAndroid Build Coastguard Worker       int16x8_t s89AB[2], s9ABC[2], sABCD[2], sBCDE[2];
98*77c1e3ccSAndroid Build Coastguard Worker       transpose_concat_4x4(sB, sC, sD, sE, sBCDE);
99*77c1e3ccSAndroid Build Coastguard Worker 
100*77c1e3ccSAndroid Build Coastguard Worker       // Merge new data into block from previous iteration.
101*77c1e3ccSAndroid Build Coastguard Worker       aom_tbl2x2_s16(s789A, sBCDE, merge_block_tbl.val[0], s89AB);
102*77c1e3ccSAndroid Build Coastguard Worker       aom_tbl2x2_s16(s789A, sBCDE, merge_block_tbl.val[1], s9ABC);
103*77c1e3ccSAndroid Build Coastguard Worker       aom_tbl2x2_s16(s789A, sBCDE, merge_block_tbl.val[2], sABCD);
104*77c1e3ccSAndroid Build Coastguard Worker 
105*77c1e3ccSAndroid Build Coastguard Worker       int32x4_t d0 = highbd_convolve12_4_2d_v(s0123, s4567, s89AB, y_filter_0_7,
106*77c1e3ccSAndroid Build Coastguard Worker                                               y_filter_4_11);
107*77c1e3ccSAndroid Build Coastguard Worker       int32x4_t d1 = highbd_convolve12_4_2d_v(s1234, s5678, s9ABC, y_filter_0_7,
108*77c1e3ccSAndroid Build Coastguard Worker                                               y_filter_4_11);
109*77c1e3ccSAndroid Build Coastguard Worker       int32x4_t d2 = highbd_convolve12_4_2d_v(s2345, s6789, sABCD, y_filter_0_7,
110*77c1e3ccSAndroid Build Coastguard Worker                                               y_filter_4_11);
111*77c1e3ccSAndroid Build Coastguard Worker       int32x4_t d3 = highbd_convolve12_4_2d_v(s3456, s789A, sBCDE, y_filter_0_7,
112*77c1e3ccSAndroid Build Coastguard Worker                                               y_filter_4_11);
113*77c1e3ccSAndroid Build Coastguard Worker 
114*77c1e3ccSAndroid Build Coastguard Worker       int16x8_t dd01 =
115*77c1e3ccSAndroid Build Coastguard Worker           vcombine_s16(vqrshrn_n_s32(d0, 2 * FILTER_BITS - ROUND0_BITS),
116*77c1e3ccSAndroid Build Coastguard Worker                        vqrshrn_n_s32(d1, 2 * FILTER_BITS - ROUND0_BITS));
117*77c1e3ccSAndroid Build Coastguard Worker       int16x8_t dd23 =
118*77c1e3ccSAndroid Build Coastguard Worker           vcombine_s16(vqrshrn_n_s32(d2, 2 * FILTER_BITS - ROUND0_BITS),
119*77c1e3ccSAndroid Build Coastguard Worker                        vqrshrn_n_s32(d3, 2 * FILTER_BITS - ROUND0_BITS));
120*77c1e3ccSAndroid Build Coastguard Worker 
121*77c1e3ccSAndroid Build Coastguard Worker       dd01 = vsubq_s16(dd01, sub_const);
122*77c1e3ccSAndroid Build Coastguard Worker       dd23 = vsubq_s16(dd23, sub_const);
123*77c1e3ccSAndroid Build Coastguard Worker 
124*77c1e3ccSAndroid Build Coastguard Worker       uint8x8_t d01 = vqmovun_s16(dd01);
125*77c1e3ccSAndroid Build Coastguard Worker       uint8x8_t d23 = vqmovun_s16(dd23);
126*77c1e3ccSAndroid Build Coastguard Worker 
127*77c1e3ccSAndroid Build Coastguard Worker       store_u8x4_strided_x2(d + 0 * dst_stride, dst_stride, d01);
128*77c1e3ccSAndroid Build Coastguard Worker       store_u8x4_strided_x2(d + 2 * dst_stride, dst_stride, d23);
129*77c1e3ccSAndroid Build Coastguard Worker 
130*77c1e3ccSAndroid Build Coastguard Worker       // Prepare block for next iteration - re-using as much as possible.
131*77c1e3ccSAndroid Build Coastguard Worker       // Shuffle everything up four rows.
132*77c1e3ccSAndroid Build Coastguard Worker       s0123[0] = s4567[0];
133*77c1e3ccSAndroid Build Coastguard Worker       s0123[1] = s4567[1];
134*77c1e3ccSAndroid Build Coastguard Worker       s1234[0] = s5678[0];
135*77c1e3ccSAndroid Build Coastguard Worker       s1234[1] = s5678[1];
136*77c1e3ccSAndroid Build Coastguard Worker       s2345[0] = s6789[0];
137*77c1e3ccSAndroid Build Coastguard Worker       s2345[1] = s6789[1];
138*77c1e3ccSAndroid Build Coastguard Worker       s3456[0] = s789A[0];
139*77c1e3ccSAndroid Build Coastguard Worker       s3456[1] = s789A[1];
140*77c1e3ccSAndroid Build Coastguard Worker       s4567[0] = s89AB[0];
141*77c1e3ccSAndroid Build Coastguard Worker       s4567[1] = s89AB[1];
142*77c1e3ccSAndroid Build Coastguard Worker       s5678[0] = s9ABC[0];
143*77c1e3ccSAndroid Build Coastguard Worker       s5678[1] = s9ABC[1];
144*77c1e3ccSAndroid Build Coastguard Worker       s6789[0] = sABCD[0];
145*77c1e3ccSAndroid Build Coastguard Worker       s6789[1] = sABCD[1];
146*77c1e3ccSAndroid Build Coastguard Worker       s789A[0] = sBCDE[0];
147*77c1e3ccSAndroid Build Coastguard Worker       s789A[1] = sBCDE[1];
148*77c1e3ccSAndroid Build Coastguard Worker 
149*77c1e3ccSAndroid Build Coastguard Worker       s += 4 * src_stride;
150*77c1e3ccSAndroid Build Coastguard Worker       d += 4 * dst_stride;
151*77c1e3ccSAndroid Build Coastguard Worker       height -= 4;
152*77c1e3ccSAndroid Build Coastguard Worker     } while (height != 0);
153*77c1e3ccSAndroid Build Coastguard Worker     src_ptr += 4;
154*77c1e3ccSAndroid Build Coastguard Worker     dst_ptr += 4;
155*77c1e3ccSAndroid Build Coastguard Worker     w -= 4;
156*77c1e3ccSAndroid Build Coastguard Worker   } while (w != 0);
157*77c1e3ccSAndroid Build Coastguard Worker }
158*77c1e3ccSAndroid Build Coastguard Worker 
av1_convolve_2d_sr_sve2(const uint8_t * src,int src_stride,uint8_t * dst,int dst_stride,int w,int h,const InterpFilterParams * filter_params_x,const InterpFilterParams * filter_params_y,const int subpel_x_qn,const int subpel_y_qn,ConvolveParams * conv_params)159*77c1e3ccSAndroid Build Coastguard Worker void av1_convolve_2d_sr_sve2(const uint8_t *src, int src_stride, uint8_t *dst,
160*77c1e3ccSAndroid Build Coastguard Worker                              int dst_stride, int w, int h,
161*77c1e3ccSAndroid Build Coastguard Worker                              const InterpFilterParams *filter_params_x,
162*77c1e3ccSAndroid Build Coastguard Worker                              const InterpFilterParams *filter_params_y,
163*77c1e3ccSAndroid Build Coastguard Worker                              const int subpel_x_qn, const int subpel_y_qn,
164*77c1e3ccSAndroid Build Coastguard Worker                              ConvolveParams *conv_params) {
165*77c1e3ccSAndroid Build Coastguard Worker   if (w == 2 || h == 2) {
166*77c1e3ccSAndroid Build Coastguard Worker     av1_convolve_2d_sr_c(src, src_stride, dst, dst_stride, w, h,
167*77c1e3ccSAndroid Build Coastguard Worker                          filter_params_x, filter_params_y, subpel_x_qn,
168*77c1e3ccSAndroid Build Coastguard Worker                          subpel_y_qn, conv_params);
169*77c1e3ccSAndroid Build Coastguard Worker     return;
170*77c1e3ccSAndroid Build Coastguard Worker   }
171*77c1e3ccSAndroid Build Coastguard Worker 
172*77c1e3ccSAndroid Build Coastguard Worker   if (filter_params_x->taps > 8) {
173*77c1e3ccSAndroid Build Coastguard Worker     const int im_h = h + filter_params_y->taps - 1;
174*77c1e3ccSAndroid Build Coastguard Worker     const int im_stride = MAX_SB_SIZE;
175*77c1e3ccSAndroid Build Coastguard Worker     const int vert_offset = filter_params_x->taps / 2 - 1;
176*77c1e3ccSAndroid Build Coastguard Worker     const int horiz_offset = filter_params_x->taps / 2 - 1;
177*77c1e3ccSAndroid Build Coastguard Worker     const uint8_t *src_ptr = src - vert_offset * src_stride - horiz_offset;
178*77c1e3ccSAndroid Build Coastguard Worker 
179*77c1e3ccSAndroid Build Coastguard Worker     const int16_t *x_filter_ptr = av1_get_interp_filter_subpel_kernel(
180*77c1e3ccSAndroid Build Coastguard Worker         filter_params_x, subpel_x_qn & SUBPEL_MASK);
181*77c1e3ccSAndroid Build Coastguard Worker     const int16_t *y_filter_ptr = av1_get_interp_filter_subpel_kernel(
182*77c1e3ccSAndroid Build Coastguard Worker         filter_params_y, subpel_y_qn & SUBPEL_MASK);
183*77c1e3ccSAndroid Build Coastguard Worker 
184*77c1e3ccSAndroid Build Coastguard Worker     DECLARE_ALIGNED(16, int16_t,
185*77c1e3ccSAndroid Build Coastguard Worker                     im_block[(MAX_SB_SIZE + MAX_FILTER_TAP - 1) * MAX_SB_SIZE]);
186*77c1e3ccSAndroid Build Coastguard Worker 
187*77c1e3ccSAndroid Build Coastguard Worker     const int16x8_t y_filter_0_7 = vld1q_s16(y_filter_ptr);
188*77c1e3ccSAndroid Build Coastguard Worker     const int16x8_t y_filter_4_11 = vld1q_s16(y_filter_ptr + 4);
189*77c1e3ccSAndroid Build Coastguard Worker 
190*77c1e3ccSAndroid Build Coastguard Worker     convolve_2d_sr_horiz_12tap_neon_i8mm(src_ptr, src_stride, im_block,
191*77c1e3ccSAndroid Build Coastguard Worker                                          im_stride, w, im_h, x_filter_ptr);
192*77c1e3ccSAndroid Build Coastguard Worker 
193*77c1e3ccSAndroid Build Coastguard Worker     convolve_2d_sr_vert_12tap_sve2(im_block, im_stride, dst, dst_stride, w, h,
194*77c1e3ccSAndroid Build Coastguard Worker                                    y_filter_0_7, y_filter_4_11);
195*77c1e3ccSAndroid Build Coastguard Worker   } else {
196*77c1e3ccSAndroid Build Coastguard Worker     av1_convolve_2d_sr_neon_i8mm(src, src_stride, dst, dst_stride, w, h,
197*77c1e3ccSAndroid Build Coastguard Worker                                  filter_params_x, filter_params_y, subpel_x_qn,
198*77c1e3ccSAndroid Build Coastguard Worker                                  subpel_y_qn, conv_params);
199*77c1e3ccSAndroid Build Coastguard Worker   }
200*77c1e3ccSAndroid Build Coastguard Worker }
201