xref: /aosp_15_r20/external/libaom/av1/common/arm/av1_convolve_horiz_rs_neon.c (revision 77c1e3ccc04c968bd2bc212e87364f250e820521)
1 /*
2  * Copyright (c) 2024, 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 <arm_neon.h>
13 #include <assert.h>
14 #include <stdint.h>
15 
16 #include "config/aom_config.h"
17 #include "config/av1_rtcd.h"
18 
19 #include "aom_dsp/arm/mem_neon.h"
20 #include "aom_dsp/arm/transpose_neon.h"
21 #include "av1/common/resize.h"
22 
convolve8_4(const int16x4_t s0,const int16x4_t s1,const int16x4_t s2,const int16x4_t s3,const int16x4_t s4,const int16x4_t s5,const int16x4_t s6,const int16x4_t s7,const int16x8_t filter)23 static inline uint8x8_t convolve8_4(const int16x4_t s0, const int16x4_t s1,
24                                     const int16x4_t s2, const int16x4_t s3,
25                                     const int16x4_t s4, const int16x4_t s5,
26                                     const int16x4_t s6, const int16x4_t s7,
27                                     const int16x8_t filter) {
28   const int16x4_t filter_lo = vget_low_s16(filter);
29   const int16x4_t filter_hi = vget_high_s16(filter);
30 
31   int16x4_t sum = vmul_lane_s16(s0, filter_lo, 0);
32   sum = vmla_lane_s16(sum, s1, filter_lo, 1);
33   sum = vmla_lane_s16(sum, s2, filter_lo, 2);
34   sum = vmla_lane_s16(sum, s5, filter_hi, 1);
35   sum = vmla_lane_s16(sum, s6, filter_hi, 2);
36   sum = vmla_lane_s16(sum, s7, filter_hi, 3);
37   sum = vqadd_s16(sum, vmul_lane_s16(s3, filter_lo, 3));
38   sum = vqadd_s16(sum, vmul_lane_s16(s4, filter_hi, 0));
39 
40   return vqrshrun_n_s16(vcombine_s16(sum, vdup_n_s16(0)), FILTER_BITS);
41 }
42 
convolve8_8(const int16x8_t s0,const int16x8_t s1,const int16x8_t s2,const int16x8_t s3,const int16x8_t s4,const int16x8_t s5,const int16x8_t s6,const int16x8_t s7,const int16x8_t filter)43 static inline uint8x8_t convolve8_8(const int16x8_t s0, const int16x8_t s1,
44                                     const int16x8_t s2, const int16x8_t s3,
45                                     const int16x8_t s4, const int16x8_t s5,
46                                     const int16x8_t s6, const int16x8_t s7,
47                                     const int16x8_t filter) {
48   const int16x4_t filter_lo = vget_low_s16(filter);
49   const int16x4_t filter_hi = vget_high_s16(filter);
50 
51   int16x8_t sum = vmulq_lane_s16(s0, filter_lo, 0);
52   sum = vmlaq_lane_s16(sum, s1, filter_lo, 1);
53   sum = vmlaq_lane_s16(sum, s2, filter_lo, 2);
54   sum = vmlaq_lane_s16(sum, s5, filter_hi, 1);
55   sum = vmlaq_lane_s16(sum, s6, filter_hi, 2);
56   sum = vmlaq_lane_s16(sum, s7, filter_hi, 3);
57   sum = vqaddq_s16(sum, vmulq_lane_s16(s3, filter_lo, 3));
58   sum = vqaddq_s16(sum, vmulq_lane_s16(s4, filter_hi, 0));
59 
60   return vqrshrun_n_s16(sum, FILTER_BITS);
61 }
62 
av1_convolve_horiz_rs_neon(const uint8_t * src,int src_stride,uint8_t * dst,int dst_stride,int w,int h,const int16_t * x_filter,int x0_qn,int x_step_qn)63 void av1_convolve_horiz_rs_neon(const uint8_t *src, int src_stride,
64                                 uint8_t *dst, int dst_stride, int w, int h,
65                                 const int16_t *x_filter, int x0_qn,
66                                 int x_step_qn) {
67   if ((w == 4 && h % 4 != 0) || (w % 8 == 0 && h % 8 != 0) || w % 8 != 0) {
68     av1_convolve_horiz_rs_c(src, src_stride, dst, dst_stride, w, h, x_filter,
69                             x0_qn, x_step_qn);
70     return;
71   }
72 
73   DECLARE_ALIGNED(16, uint8_t, temp[8 * 8]);
74 
75   src -= UPSCALE_NORMATIVE_TAPS / 2 - 1;
76 
77   if (w == 4) {
78     do {
79       int x_qn = x0_qn;
80 
81       // Process a 4x4 tile.
82       for (int r = 0; r < 4; ++r) {
83         const uint8_t *const s = &src[x_qn >> RS_SCALE_SUBPEL_BITS];
84 
85         const ptrdiff_t filter_offset =
86             UPSCALE_NORMATIVE_TAPS *
87             ((x_qn & RS_SCALE_SUBPEL_MASK) >> RS_SCALE_EXTRA_BITS);
88         const int16x8_t filter = vld1q_s16(x_filter + filter_offset);
89 
90         uint8x8_t t0, t1, t2, t3;
91         load_u8_8x4(s, src_stride, &t0, &t1, &t2, &t3);
92 
93         transpose_elems_inplace_u8_8x4(&t0, &t1, &t2, &t3);
94 
95         int16x4_t s0 = vget_low_s16(vreinterpretq_s16_u16(vmovl_u8(t0)));
96         int16x4_t s1 = vget_low_s16(vreinterpretq_s16_u16(vmovl_u8(t1)));
97         int16x4_t s2 = vget_low_s16(vreinterpretq_s16_u16(vmovl_u8(t2)));
98         int16x4_t s3 = vget_low_s16(vreinterpretq_s16_u16(vmovl_u8(t3)));
99         int16x4_t s4 = vget_high_s16(vreinterpretq_s16_u16(vmovl_u8(t0)));
100         int16x4_t s5 = vget_high_s16(vreinterpretq_s16_u16(vmovl_u8(t1)));
101         int16x4_t s6 = vget_high_s16(vreinterpretq_s16_u16(vmovl_u8(t2)));
102         int16x4_t s7 = vget_high_s16(vreinterpretq_s16_u16(vmovl_u8(t3)));
103 
104         uint8x8_t d0 = convolve8_4(s0, s1, s2, s3, s4, s5, s6, s7, filter);
105 
106         store_u8_4x1(&temp[r * 4], d0);
107 
108         x_qn += x_step_qn;
109       }
110 
111       // Transpose the 4x4 result tile and store.
112       uint8x8_t d01 = vld1_u8(temp + 0);
113       uint8x8_t d23 = vld1_u8(temp + 8);
114 
115       transpose_elems_inplace_u8_4x4(&d01, &d23);
116 
117       store_u8x4_strided_x2(dst + 0 * dst_stride, 2 * dst_stride, d01);
118       store_u8x4_strided_x2(dst + 1 * dst_stride, 2 * dst_stride, d23);
119 
120       dst += 4 * dst_stride;
121       src += 4 * src_stride;
122       h -= 4;
123     } while (h > 0);
124   } else {
125     do {
126       int x_qn = x0_qn;
127       uint8_t *d = dst;
128       int width = w;
129 
130       do {
131         // Process an 8x8 tile.
132         for (int r = 0; r < 8; ++r) {
133           const uint8_t *const s = &src[x_qn >> RS_SCALE_SUBPEL_BITS];
134 
135           const ptrdiff_t filter_offset =
136               UPSCALE_NORMATIVE_TAPS *
137               ((x_qn & RS_SCALE_SUBPEL_MASK) >> RS_SCALE_EXTRA_BITS);
138           const int16x8_t filter = vld1q_s16(x_filter + filter_offset);
139 
140           uint8x8_t t0, t1, t2, t3, t4, t5, t6, t7;
141           load_u8_8x8(s, src_stride, &t0, &t1, &t2, &t3, &t4, &t5, &t6, &t7);
142 
143           transpose_elems_u8_8x8(t0, t1, t2, t3, t4, t5, t6, t7, &t0, &t1, &t2,
144                                  &t3, &t4, &t5, &t6, &t7);
145 
146           int16x8_t s0 = vreinterpretq_s16_u16(vmovl_u8(t0));
147           int16x8_t s1 = vreinterpretq_s16_u16(vmovl_u8(t1));
148           int16x8_t s2 = vreinterpretq_s16_u16(vmovl_u8(t2));
149           int16x8_t s3 = vreinterpretq_s16_u16(vmovl_u8(t3));
150           int16x8_t s4 = vreinterpretq_s16_u16(vmovl_u8(t4));
151           int16x8_t s5 = vreinterpretq_s16_u16(vmovl_u8(t5));
152           int16x8_t s6 = vreinterpretq_s16_u16(vmovl_u8(t6));
153           int16x8_t s7 = vreinterpretq_s16_u16(vmovl_u8(t7));
154 
155           uint8x8_t d0 = convolve8_8(s0, s1, s2, s3, s4, s5, s6, s7, filter);
156 
157           vst1_u8(&temp[r * 8], d0);
158 
159           x_qn += x_step_qn;
160         }
161 
162         // Transpose the 8x8 result tile and store.
163         uint8x8_t d0, d1, d2, d3, d4, d5, d6, d7;
164         load_u8_8x8(temp, 8, &d0, &d1, &d2, &d3, &d4, &d5, &d6, &d7);
165 
166         transpose_elems_inplace_u8_8x8(&d0, &d1, &d2, &d3, &d4, &d5, &d6, &d7);
167 
168         store_u8_8x8(d, dst_stride, d0, d1, d2, d3, d4, d5, d6, d7);
169 
170         d += 8;
171         width -= 8;
172       } while (width != 0);
173 
174       dst += 8 * dst_stride;
175       src += 8 * src_stride;
176       h -= 8;
177 
178     } while (h > 0);
179   }
180 }
181