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