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 <emmintrin.h>
13 
14 #include "config/aom_dsp_rtcd.h"
15 
16 #include "aom_dsp/aom_filter.h"
17 #include "aom_dsp/x86/convolve_sse2.h"
18 
av1_jnt_convolve_x_sse2(const uint8_t * src,int src_stride,uint8_t * dst0,int dst_stride0,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)19 void av1_jnt_convolve_x_sse2(const uint8_t *src, int src_stride, uint8_t *dst0,
20                              int dst_stride0, int w, int h,
21                              const InterpFilterParams *filter_params_x,
22                              const InterpFilterParams *filter_params_y,
23                              const int subpel_x_q4, const int subpel_y_q4,
24                              ConvolveParams *conv_params) {
25   const int bd = 8;
26   CONV_BUF_TYPE *dst = conv_params->dst;
27   const int dst_stride = conv_params->dst_stride;
28   const int fo_horiz = filter_params_x->taps / 2 - 1;
29   const uint8_t *src_ptr = src - fo_horiz;
30   const int bits = FILTER_BITS - conv_params->round_1;
31   const __m128i left_shift = _mm_cvtsi32_si128(bits);
32   const __m128i round_const = _mm_set1_epi32((1 << conv_params->round_0) >> 1);
33   const __m128i round_shift = _mm_cvtsi32_si128(conv_params->round_0);
34   const int w0 = conv_params->fwd_offset;
35   const int w1 = conv_params->bck_offset;
36   const __m128i wt0 = _mm_set1_epi16(w0);
37   const __m128i wt1 = _mm_set1_epi16(w1);
38   const __m128i wt = _mm_unpacklo_epi16(wt0, wt1);
39   const int do_average = conv_params->do_average;
40   const int use_jnt_comp_avg = conv_params->use_jnt_comp_avg;
41   const int offset_0 =
42       bd + 2 * FILTER_BITS - conv_params->round_0 - conv_params->round_1;
43   const int offset = (1 << offset_0) + (1 << (offset_0 - 1));
44   const __m128i offset_const = _mm_set1_epi16(offset);
45   const int rounding_shift =
46       2 * FILTER_BITS - conv_params->round_0 - conv_params->round_1;
47   const __m128i rounding_const = _mm_set1_epi16((1 << rounding_shift) >> 1);
48   __m128i coeffs[4];
49 
50   (void)filter_params_y;
51   (void)subpel_y_q4;
52 
53   prepare_coeffs(filter_params_x, subpel_x_q4, coeffs);
54 
55   if (w == 4) {
56     do {
57       const __m128i data = _mm_loadu_si128((__m128i *)src_ptr);
58       __m128i s[4];
59 
60       s[0] = _mm_unpacklo_epi8(data, _mm_srli_si128(data, 1));
61       s[1] =
62           _mm_unpacklo_epi8(_mm_srli_si128(data, 2), _mm_srli_si128(data, 3));
63       s[2] =
64           _mm_unpacklo_epi8(_mm_srli_si128(data, 4), _mm_srli_si128(data, 5));
65       s[3] =
66           _mm_unpacklo_epi8(_mm_srli_si128(data, 6), _mm_srli_si128(data, 7));
67       const __m128i res_lo = convolve_lo_x(s, coeffs);
68       const __m128i res_lo_round =
69           _mm_sra_epi32(_mm_add_epi32(res_lo, round_const), round_shift);
70       const __m128i res_lo_shift = _mm_sll_epi32(res_lo_round, left_shift);
71 
72       const __m128i res_16b = _mm_packs_epi32(res_lo_shift, res_lo_shift);
73       const __m128i res_unsigned = _mm_add_epi16(res_16b, offset_const);
74 
75       // Accumulate values into the destination buffer
76       if (do_average) {
77         const __m128i data_ref_0 = _mm_loadu_si128((__m128i *)dst);
78 
79         const __m128i comp_avg_res =
80             comp_avg(&data_ref_0, &res_unsigned, &wt, use_jnt_comp_avg);
81 
82         const __m128i round_result = convolve_rounding(
83             &comp_avg_res, &offset_const, &rounding_const, rounding_shift);
84 
85         const __m128i res_8 = _mm_packus_epi16(round_result, round_result);
86         *(uint32_t *)(&dst0[0]) = _mm_cvtsi128_si32(res_8);
87       } else {
88         _mm_store_si128((__m128i *)(&dst[0]), res_unsigned);
89       }
90       src_ptr += src_stride;
91       dst += dst_stride;
92       dst0 += dst_stride0;
93     } while (--h);
94   } else {
95     assert(!(w % 8));
96     int i = 0;
97     do {
98       int j = 0;
99       do {
100         const __m128i data =
101             _mm_loadu_si128((__m128i *)&src_ptr[i * src_stride + j]);
102         __m128i s[4];
103 
104         // Filter even-index pixels
105         s[0] = data;
106         s[1] = _mm_srli_si128(data, 2);
107         s[2] = _mm_srli_si128(data, 4);
108         s[3] = _mm_srli_si128(data, 6);
109         const __m128i res_even = convolve_lo_x(s, coeffs);
110 
111         // Filter odd-index pixels
112         s[0] = _mm_srli_si128(data, 1);
113         s[1] = _mm_srli_si128(data, 3);
114         s[2] = _mm_srli_si128(data, 5);
115         s[3] = _mm_srli_si128(data, 7);
116         const __m128i res_odd = convolve_lo_x(s, coeffs);
117 
118         // Rearrange pixels back into the order 0 ... 7
119         const __m128i res_lo = _mm_unpacklo_epi32(res_even, res_odd);
120         const __m128i res_hi = _mm_unpackhi_epi32(res_even, res_odd);
121         const __m128i res_lo_round =
122             _mm_sra_epi32(_mm_add_epi32(res_lo, round_const), round_shift);
123         const __m128i res_hi_round =
124             _mm_sra_epi32(_mm_add_epi32(res_hi, round_const), round_shift);
125         const __m128i res_lo_shift = _mm_sll_epi32(res_lo_round, left_shift);
126         const __m128i res_hi_shift = _mm_sll_epi32(res_hi_round, left_shift);
127 
128         const __m128i res_16b = _mm_packs_epi32(res_lo_shift, res_hi_shift);
129         const __m128i res_unsigned = _mm_add_epi16(res_16b, offset_const);
130 
131         // Accumulate values into the destination buffer
132         if (do_average) {
133           const __m128i data_ref_0 =
134               _mm_loadu_si128((__m128i *)(&dst[i * dst_stride + j]));
135 
136           const __m128i comp_avg_res =
137               comp_avg(&data_ref_0, &res_unsigned, &wt, use_jnt_comp_avg);
138 
139           const __m128i round_result = convolve_rounding(
140               &comp_avg_res, &offset_const, &rounding_const, rounding_shift);
141 
142           const __m128i res_8 = _mm_packus_epi16(round_result, round_result);
143           _mm_storel_epi64((__m128i *)(&dst0[i * dst_stride0 + j]), res_8);
144         } else {
145           _mm_store_si128((__m128i *)(&dst[i * dst_stride + j]), res_unsigned);
146         }
147         j += 8;
148       } while (j < w);
149     } while (++i < h);
150   }
151 }
152 
av1_jnt_convolve_y_sse2(const uint8_t * src,int src_stride,uint8_t * dst0,int dst_stride0,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)153 void av1_jnt_convolve_y_sse2(const uint8_t *src, int src_stride, uint8_t *dst0,
154                              int dst_stride0, int w, int h,
155                              const InterpFilterParams *filter_params_x,
156                              const InterpFilterParams *filter_params_y,
157                              const int subpel_x_q4, const int subpel_y_q4,
158                              ConvolveParams *conv_params) {
159   const int bd = 8;
160   CONV_BUF_TYPE *dst = conv_params->dst;
161   const int dst_stride = conv_params->dst_stride;
162   const int fo_vert = filter_params_y->taps / 2 - 1;
163   const uint8_t *src_ptr = src - fo_vert * src_stride;
164   const int bits = FILTER_BITS - conv_params->round_0;
165   const __m128i left_shift = _mm_cvtsi32_si128(bits);
166   const __m128i wt0 = _mm_set1_epi16(conv_params->fwd_offset);
167   const __m128i wt1 = _mm_set1_epi16(conv_params->bck_offset);
168   const __m128i wt = _mm_unpacklo_epi16(wt0, wt1);
169   const int do_average = conv_params->do_average;
170   const int use_jnt_comp_avg = conv_params->use_jnt_comp_avg;
171   const int offset_0 =
172       bd + 2 * FILTER_BITS - conv_params->round_0 - conv_params->round_1;
173   const int offset = (1 << offset_0) + (1 << (offset_0 - 1));
174   const __m128i offset_const = _mm_set1_epi16(offset);
175   const int rounding_shift =
176       2 * FILTER_BITS - conv_params->round_0 - conv_params->round_1;
177   const __m128i rounding_const = _mm_set1_epi16((1 << rounding_shift) >> 1);
178   const __m128i round_const = _mm_set1_epi32((1 << conv_params->round_1) >> 1);
179   const __m128i round_shift = _mm_cvtsi32_si128(conv_params->round_1);
180   __m128i coeffs[4];
181 
182   (void)filter_params_x;
183   (void)subpel_x_q4;
184 
185   prepare_coeffs(filter_params_y, subpel_y_q4, coeffs);
186 
187   if (w == 4) {
188     __m128i s[8], src6, res, res_shift;
189     src6 = _mm_cvtsi32_si128(*(uint32_t *)(src_ptr + 6 * src_stride));
190     s[0] = _mm_unpacklo_epi8(
191         _mm_cvtsi32_si128(*(uint32_t *)(src_ptr + 0 * src_stride)),
192         _mm_cvtsi32_si128(*(uint32_t *)(src_ptr + 1 * src_stride)));
193     s[1] = _mm_unpacklo_epi8(
194         _mm_cvtsi32_si128(*(uint32_t *)(src_ptr + 1 * src_stride)),
195         _mm_cvtsi32_si128(*(uint32_t *)(src_ptr + 2 * src_stride)));
196     s[2] = _mm_unpacklo_epi8(
197         _mm_cvtsi32_si128(*(uint32_t *)(src_ptr + 2 * src_stride)),
198         _mm_cvtsi32_si128(*(uint32_t *)(src_ptr + 3 * src_stride)));
199     s[3] = _mm_unpacklo_epi8(
200         _mm_cvtsi32_si128(*(uint32_t *)(src_ptr + 3 * src_stride)),
201         _mm_cvtsi32_si128(*(uint32_t *)(src_ptr + 4 * src_stride)));
202     s[4] = _mm_unpacklo_epi8(
203         _mm_cvtsi32_si128(*(uint32_t *)(src_ptr + 4 * src_stride)),
204         _mm_cvtsi32_si128(*(uint32_t *)(src_ptr + 5 * src_stride)));
205     s[5] = _mm_unpacklo_epi8(
206         _mm_cvtsi32_si128(*(uint32_t *)(src_ptr + 5 * src_stride)), src6);
207 
208     do {
209       s[6] = _mm_unpacklo_epi8(
210           src6, _mm_cvtsi32_si128(*(uint32_t *)(src_ptr + 7 * src_stride)));
211       src6 = _mm_cvtsi32_si128(*(uint32_t *)(src_ptr + 8 * src_stride));
212       s[7] = _mm_unpacklo_epi8(
213           _mm_cvtsi32_si128(*(uint32_t *)(src_ptr + 7 * src_stride)), src6);
214 
215       res = convolve_lo_y(s + 0, coeffs);
216       res_shift = _mm_sll_epi32(res, left_shift);
217       res_shift =
218           _mm_sra_epi32(_mm_add_epi32(res_shift, round_const), round_shift);
219 
220       __m128i res_16b = _mm_packs_epi32(res_shift, res_shift);
221       __m128i res_unsigned = _mm_add_epi16(res_16b, offset_const);
222 
223       // Accumulate values into the destination buffer
224       if (do_average) {
225         const __m128i data_ref_0 = _mm_loadu_si128((__m128i *)dst);
226 
227         const __m128i comp_avg_res =
228             comp_avg(&data_ref_0, &res_unsigned, &wt, use_jnt_comp_avg);
229 
230         const __m128i round_result = convolve_rounding(
231             &comp_avg_res, &offset_const, &rounding_const, rounding_shift);
232 
233         const __m128i res_8 = _mm_packus_epi16(round_result, round_result);
234         *(uint32_t *)(&dst0[0]) = _mm_cvtsi128_si32(res_8);
235 
236       } else {
237         _mm_store_si128((__m128i *)dst, res_unsigned);
238       }
239 
240       src_ptr += src_stride;
241       dst += dst_stride;
242       dst0 += dst_stride0;
243 
244       res = convolve_lo_y(s + 1, coeffs);
245       res_shift = _mm_sll_epi32(res, left_shift);
246       res_shift =
247           _mm_sra_epi32(_mm_add_epi32(res_shift, round_const), round_shift);
248 
249       res_16b = _mm_packs_epi32(res_shift, res_shift);
250       res_unsigned = _mm_add_epi16(res_16b, offset_const);
251 
252       // Accumulate values into the destination buffer
253       if (do_average) {
254         const __m128i data_ref_0 = _mm_loadu_si128((__m128i *)dst);
255 
256         const __m128i comp_avg_res =
257             comp_avg(&data_ref_0, &res_unsigned, &wt, use_jnt_comp_avg);
258 
259         const __m128i round_result = convolve_rounding(
260             &comp_avg_res, &offset_const, &rounding_const, rounding_shift);
261 
262         const __m128i res_8 = _mm_packus_epi16(round_result, round_result);
263         *(uint32_t *)(&dst0[0]) = _mm_cvtsi128_si32(res_8);
264 
265       } else {
266         _mm_store_si128((__m128i *)dst, res_unsigned);
267       }
268 
269       src_ptr += src_stride;
270       dst += dst_stride;
271       dst0 += dst_stride0;
272 
273       s[0] = s[2];
274       s[1] = s[3];
275       s[2] = s[4];
276       s[3] = s[5];
277       s[4] = s[6];
278       s[5] = s[7];
279       h -= 2;
280     } while (h);
281   } else {
282     assert(!(w % 8));
283     int j = 0;
284     do {
285       __m128i s[8], src6, res_lo, res_hi, res_lo_shift, res_hi_shift;
286       const uint8_t *data = &src_ptr[j];
287 
288       src6 = _mm_loadl_epi64((__m128i *)(data + 6 * src_stride));
289       s[0] = _mm_unpacklo_epi8(
290           _mm_loadl_epi64((__m128i *)(data + 0 * src_stride)),
291           _mm_loadl_epi64((__m128i *)(data + 1 * src_stride)));
292       s[1] = _mm_unpacklo_epi8(
293           _mm_loadl_epi64((__m128i *)(data + 1 * src_stride)),
294           _mm_loadl_epi64((__m128i *)(data + 2 * src_stride)));
295       s[2] = _mm_unpacklo_epi8(
296           _mm_loadl_epi64((__m128i *)(data + 2 * src_stride)),
297           _mm_loadl_epi64((__m128i *)(data + 3 * src_stride)));
298       s[3] = _mm_unpacklo_epi8(
299           _mm_loadl_epi64((__m128i *)(data + 3 * src_stride)),
300           _mm_loadl_epi64((__m128i *)(data + 4 * src_stride)));
301       s[4] = _mm_unpacklo_epi8(
302           _mm_loadl_epi64((__m128i *)(data + 4 * src_stride)),
303           _mm_loadl_epi64((__m128i *)(data + 5 * src_stride)));
304       s[5] = _mm_unpacklo_epi8(
305           _mm_loadl_epi64((__m128i *)(data + 5 * src_stride)), src6);
306 
307       int i = 0;
308       do {
309         data = &src_ptr[i * src_stride + j];
310         s[6] = _mm_unpacklo_epi8(
311             src6, _mm_loadl_epi64((__m128i *)(data + 7 * src_stride)));
312         src6 = _mm_loadl_epi64((__m128i *)(data + 8 * src_stride));
313         s[7] = _mm_unpacklo_epi8(
314             _mm_loadl_epi64((__m128i *)(data + 7 * src_stride)), src6);
315 
316         res_lo = convolve_lo_y(s, coeffs);  // Filter low index pixels
317         res_hi = convolve_hi_y(s, coeffs);  // Filter high index pixels
318         res_lo_shift = _mm_sll_epi32(res_lo, left_shift);
319         res_hi_shift = _mm_sll_epi32(res_hi, left_shift);
320         res_lo_shift = _mm_sra_epi32(_mm_add_epi32(res_lo_shift, round_const),
321                                      round_shift);
322         res_hi_shift = _mm_sra_epi32(_mm_add_epi32(res_hi_shift, round_const),
323                                      round_shift);
324 
325         __m128i res_16b = _mm_packs_epi32(res_lo_shift, res_hi_shift);
326         __m128i res_unsigned = _mm_add_epi16(res_16b, offset_const);
327 
328         // Accumulate values into the destination buffer
329         if (do_average) {
330           const __m128i data_ref_0 =
331               _mm_loadu_si128((__m128i *)(&dst[i * dst_stride + j]));
332 
333           const __m128i comp_avg_res =
334               comp_avg(&data_ref_0, &res_unsigned, &wt, use_jnt_comp_avg);
335 
336           const __m128i round_result = convolve_rounding(
337               &comp_avg_res, &offset_const, &rounding_const, rounding_shift);
338 
339           const __m128i res_8 = _mm_packus_epi16(round_result, round_result);
340           _mm_storel_epi64((__m128i *)(&dst0[i * dst_stride0 + j]), res_8);
341         } else {
342           _mm_store_si128((__m128i *)(&dst[i * dst_stride + j]), res_unsigned);
343         }
344         i++;
345 
346         res_lo = convolve_lo_y(s + 1, coeffs);  // Filter low index pixels
347         res_hi = convolve_hi_y(s + 1, coeffs);  // Filter high index pixels
348         res_lo_shift = _mm_sll_epi32(res_lo, left_shift);
349         res_hi_shift = _mm_sll_epi32(res_hi, left_shift);
350         res_lo_shift = _mm_sra_epi32(_mm_add_epi32(res_lo_shift, round_const),
351                                      round_shift);
352         res_hi_shift = _mm_sra_epi32(_mm_add_epi32(res_hi_shift, round_const),
353                                      round_shift);
354         res_16b = _mm_packs_epi32(res_lo_shift, res_hi_shift);
355         res_unsigned = _mm_add_epi16(res_16b, offset_const);
356 
357         // Accumulate values into the destination buffer
358         if (do_average) {
359           __m128i data_ref_0 =
360               _mm_loadu_si128((__m128i *)(&dst[i * dst_stride + j]));
361 
362           const __m128i comp_avg_res =
363               comp_avg(&data_ref_0, &res_unsigned, &wt, use_jnt_comp_avg);
364 
365           const __m128i round_result = convolve_rounding(
366               &comp_avg_res, &offset_const, &rounding_const, rounding_shift);
367 
368           const __m128i res_8 = _mm_packus_epi16(round_result, round_result);
369           _mm_storel_epi64((__m128i *)(&dst0[i * dst_stride0 + j]), res_8);
370         } else {
371           _mm_store_si128((__m128i *)(&dst[i * dst_stride + j]), res_unsigned);
372         }
373         i++;
374 
375         s[0] = s[2];
376         s[1] = s[3];
377         s[2] = s[4];
378         s[3] = s[5];
379         s[4] = s[6];
380         s[5] = s[7];
381       } while (i < h);
382       j += 8;
383     } while (j < w);
384   }
385 }
386