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 <tmmintrin.h>
13 #include <assert.h>
14 
15 #include "config/aom_dsp_rtcd.h"
16 
17 #include "aom_dsp/x86/convolve_sse2.h"
18 
av1_highbd_convolve_y_sr_ssse3(const uint16_t * src,int src_stride,uint16_t * dst,int dst_stride,int w,int h,const InterpFilterParams * filter_params_x,const InterpFilterParams * filter_params_y,const int subpel_x_q4,const int subpel_y_q4,ConvolveParams * conv_params,int bd)19 void av1_highbd_convolve_y_sr_ssse3(const uint16_t *src, int src_stride,
20                                     uint16_t *dst, int dst_stride, int w, int h,
21                                     const InterpFilterParams *filter_params_x,
22                                     const InterpFilterParams *filter_params_y,
23                                     const int subpel_x_q4,
24                                     const int subpel_y_q4,
25                                     ConvolveParams *conv_params, int bd) {
26   int i, j;
27   const int fo_vert = filter_params_y->taps / 2 - 1;
28   const uint16_t *const src_ptr = src - fo_vert * src_stride;
29   (void)filter_params_x;
30   (void)subpel_x_q4;
31   (void)conv_params;
32 
33   assert(conv_params->round_0 <= FILTER_BITS);
34   assert(((conv_params->round_0 + conv_params->round_1) <= (FILTER_BITS + 1)) ||
35          ((conv_params->round_0 + conv_params->round_1) == (2 * FILTER_BITS)));
36 
37   __m128i s[16], coeffs_y[4];
38 
39   const int bits = FILTER_BITS;
40 
41   const __m128i round_shift_bits = _mm_cvtsi32_si128(bits);
42   const __m128i round_const_bits = _mm_set1_epi32((1 << bits) >> 1);
43   const __m128i clip_pixel =
44       _mm_set1_epi16(bd == 10 ? 1023 : (bd == 12 ? 4095 : 255));
45   const __m128i zero = _mm_setzero_si128();
46 
47   prepare_coeffs(filter_params_y, subpel_y_q4, coeffs_y);
48 
49   for (j = 0; j < w; j += 8) {
50     const uint16_t *data = &src_ptr[j];
51     /* Vertical filter */
52     {
53       __m128i s0 = _mm_loadu_si128((__m128i *)(data + 0 * src_stride));
54       __m128i s1 = _mm_loadu_si128((__m128i *)(data + 1 * src_stride));
55       __m128i s2 = _mm_loadu_si128((__m128i *)(data + 2 * src_stride));
56       __m128i s3 = _mm_loadu_si128((__m128i *)(data + 3 * src_stride));
57       __m128i s4 = _mm_loadu_si128((__m128i *)(data + 4 * src_stride));
58       __m128i s5 = _mm_loadu_si128((__m128i *)(data + 5 * src_stride));
59       __m128i s6 = _mm_loadu_si128((__m128i *)(data + 6 * src_stride));
60 
61       s[0] = _mm_unpacklo_epi16(s0, s1);
62       s[1] = _mm_unpacklo_epi16(s2, s3);
63       s[2] = _mm_unpacklo_epi16(s4, s5);
64 
65       s[4] = _mm_unpackhi_epi16(s0, s1);
66       s[5] = _mm_unpackhi_epi16(s2, s3);
67       s[6] = _mm_unpackhi_epi16(s4, s5);
68 
69       s[0 + 8] = _mm_unpacklo_epi16(s1, s2);
70       s[1 + 8] = _mm_unpacklo_epi16(s3, s4);
71       s[2 + 8] = _mm_unpacklo_epi16(s5, s6);
72 
73       s[4 + 8] = _mm_unpackhi_epi16(s1, s2);
74       s[5 + 8] = _mm_unpackhi_epi16(s3, s4);
75       s[6 + 8] = _mm_unpackhi_epi16(s5, s6);
76 
77       for (i = 0; i < h; i += 2) {
78         data = &src_ptr[i * src_stride + j];
79 
80         __m128i s7 = _mm_loadu_si128((__m128i *)(data + 7 * src_stride));
81         __m128i s8 = _mm_loadu_si128((__m128i *)(data + 8 * src_stride));
82 
83         s[3] = _mm_unpacklo_epi16(s6, s7);
84         s[7] = _mm_unpackhi_epi16(s6, s7);
85 
86         s[3 + 8] = _mm_unpacklo_epi16(s7, s8);
87         s[7 + 8] = _mm_unpackhi_epi16(s7, s8);
88 
89         const __m128i res_a0 = convolve(s, coeffs_y);
90         __m128i res_a_round0 = _mm_sra_epi32(
91             _mm_add_epi32(res_a0, round_const_bits), round_shift_bits);
92 
93         const __m128i res_a1 = convolve(s + 8, coeffs_y);
94         __m128i res_a_round1 = _mm_sra_epi32(
95             _mm_add_epi32(res_a1, round_const_bits), round_shift_bits);
96 
97         if (w - j > 4) {
98           const __m128i res_b0 = convolve(s + 4, coeffs_y);
99           __m128i res_b_round0 = _mm_sra_epi32(
100               _mm_add_epi32(res_b0, round_const_bits), round_shift_bits);
101 
102           const __m128i res_b1 = convolve(s + 4 + 8, coeffs_y);
103           __m128i res_b_round1 = _mm_sra_epi32(
104               _mm_add_epi32(res_b1, round_const_bits), round_shift_bits);
105 
106           __m128i res_16bit0 = _mm_packs_epi32(res_a_round0, res_b_round0);
107           res_16bit0 = _mm_min_epi16(res_16bit0, clip_pixel);
108           res_16bit0 = _mm_max_epi16(res_16bit0, zero);
109 
110           __m128i res_16bit1 = _mm_packs_epi32(res_a_round1, res_b_round1);
111           res_16bit1 = _mm_min_epi16(res_16bit1, clip_pixel);
112           res_16bit1 = _mm_max_epi16(res_16bit1, zero);
113 
114           _mm_storeu_si128((__m128i *)&dst[i * dst_stride + j], res_16bit0);
115           _mm_storeu_si128((__m128i *)&dst[i * dst_stride + j + dst_stride],
116                            res_16bit1);
117         } else if (w == 4) {
118           res_a_round0 = _mm_packs_epi32(res_a_round0, res_a_round0);
119           res_a_round0 = _mm_min_epi16(res_a_round0, clip_pixel);
120           res_a_round0 = _mm_max_epi16(res_a_round0, zero);
121 
122           res_a_round1 = _mm_packs_epi32(res_a_round1, res_a_round1);
123           res_a_round1 = _mm_min_epi16(res_a_round1, clip_pixel);
124           res_a_round1 = _mm_max_epi16(res_a_round1, zero);
125 
126           _mm_storel_epi64((__m128i *)&dst[i * dst_stride + j], res_a_round0);
127           _mm_storel_epi64((__m128i *)&dst[i * dst_stride + j + dst_stride],
128                            res_a_round1);
129         } else {
130           res_a_round0 = _mm_packs_epi32(res_a_round0, res_a_round0);
131           res_a_round0 = _mm_min_epi16(res_a_round0, clip_pixel);
132           res_a_round0 = _mm_max_epi16(res_a_round0, zero);
133 
134           res_a_round1 = _mm_packs_epi32(res_a_round1, res_a_round1);
135           res_a_round1 = _mm_min_epi16(res_a_round1, clip_pixel);
136           res_a_round1 = _mm_max_epi16(res_a_round1, zero);
137 
138           *((uint32_t *)(&dst[i * dst_stride + j])) =
139               _mm_cvtsi128_si32(res_a_round0);
140 
141           *((uint32_t *)(&dst[i * dst_stride + j + dst_stride])) =
142               _mm_cvtsi128_si32(res_a_round1);
143         }
144 
145         s[0] = s[1];
146         s[1] = s[2];
147         s[2] = s[3];
148 
149         s[4] = s[5];
150         s[5] = s[6];
151         s[6] = s[7];
152 
153         s[0 + 8] = s[1 + 8];
154         s[1 + 8] = s[2 + 8];
155         s[2 + 8] = s[3 + 8];
156 
157         s[4 + 8] = s[5 + 8];
158         s[5 + 8] = s[6 + 8];
159         s[6 + 8] = s[7 + 8];
160 
161         s6 = s8;
162       }
163     }
164   }
165 }
166 
av1_highbd_convolve_x_sr_ssse3(const uint16_t * src,int src_stride,uint16_t * dst,int dst_stride,int w,int h,const InterpFilterParams * filter_params_x,const InterpFilterParams * filter_params_y,const int subpel_x_q4,const int subpel_y_q4,ConvolveParams * conv_params,int bd)167 void av1_highbd_convolve_x_sr_ssse3(const uint16_t *src, int src_stride,
168                                     uint16_t *dst, int dst_stride, int w, int h,
169                                     const InterpFilterParams *filter_params_x,
170                                     const InterpFilterParams *filter_params_y,
171                                     const int subpel_x_q4,
172                                     const int subpel_y_q4,
173                                     ConvolveParams *conv_params, int bd) {
174   int i, j;
175   const int fo_horiz = filter_params_x->taps / 2 - 1;
176   const uint16_t *const src_ptr = src - fo_horiz;
177   (void)subpel_y_q4;
178   (void)filter_params_y;
179 
180   // Check that, even with 12-bit input, the intermediate values will fit
181   // into an unsigned 16-bit intermediate array.
182   assert(bd + FILTER_BITS + 2 - conv_params->round_0 <= 16);
183 
184   __m128i s[4], coeffs_x[4];
185 
186   const __m128i round_const_x =
187       _mm_set1_epi32(((1 << conv_params->round_0) >> 1));
188   const __m128i round_shift_x = _mm_cvtsi32_si128(conv_params->round_0);
189 
190   const int bits = FILTER_BITS - conv_params->round_0;
191 
192   const __m128i round_shift_bits = _mm_cvtsi32_si128(bits);
193   const __m128i round_const_bits = _mm_set1_epi32((1 << bits) >> 1);
194   const __m128i clip_pixel =
195       _mm_set1_epi16(bd == 10 ? 1023 : (bd == 12 ? 4095 : 255));
196   const __m128i zero = _mm_setzero_si128();
197 
198   prepare_coeffs(filter_params_x, subpel_x_q4, coeffs_x);
199 
200   for (j = 0; j < w; j += 8) {
201     /* Horizontal filter */
202     {
203       for (i = 0; i < h; i += 1) {
204         const __m128i row00 =
205             _mm_loadu_si128((__m128i *)&src_ptr[i * src_stride + j]);
206         const __m128i row01 =
207             _mm_loadu_si128((__m128i *)&src_ptr[i * src_stride + (j + 8)]);
208 
209         // even pixels
210         s[0] = _mm_alignr_epi8(row01, row00, 0);
211         s[1] = _mm_alignr_epi8(row01, row00, 4);
212         s[2] = _mm_alignr_epi8(row01, row00, 8);
213         s[3] = _mm_alignr_epi8(row01, row00, 12);
214 
215         __m128i res_even = convolve(s, coeffs_x);
216         res_even = _mm_sra_epi32(_mm_add_epi32(res_even, round_const_x),
217                                  round_shift_x);
218 
219         // odd pixels
220         s[0] = _mm_alignr_epi8(row01, row00, 2);
221         s[1] = _mm_alignr_epi8(row01, row00, 6);
222         s[2] = _mm_alignr_epi8(row01, row00, 10);
223         s[3] = _mm_alignr_epi8(row01, row00, 14);
224 
225         __m128i res_odd = convolve(s, coeffs_x);
226         res_odd =
227             _mm_sra_epi32(_mm_add_epi32(res_odd, round_const_x), round_shift_x);
228 
229         res_even = _mm_sra_epi32(_mm_add_epi32(res_even, round_const_bits),
230                                  round_shift_bits);
231         res_odd = _mm_sra_epi32(_mm_add_epi32(res_odd, round_const_bits),
232                                 round_shift_bits);
233 
234         __m128i res_even1 = _mm_packs_epi32(res_even, res_even);
235         __m128i res_odd1 = _mm_packs_epi32(res_odd, res_odd);
236         __m128i res = _mm_unpacklo_epi16(res_even1, res_odd1);
237 
238         res = _mm_min_epi16(res, clip_pixel);
239         res = _mm_max_epi16(res, zero);
240 
241         if (w - j > 4) {
242           _mm_storeu_si128((__m128i *)&dst[i * dst_stride + j], res);
243         } else if (w == 4) {
244           _mm_storel_epi64((__m128i *)&dst[i * dst_stride + j], res);
245         } else {
246           *((uint32_t *)(&dst[i * dst_stride + j])) = _mm_cvtsi128_si32(res);
247         }
248       }
249     }
250   }
251 }
252