Line data Source code
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 :
14 : #include "./av1_rtcd.h"
15 : #include "av1/common/warped_motion.h"
16 :
17 0 : void av1_highbd_warp_affine_ssse3(const int32_t *mat, const uint16_t *ref,
18 : int width, int height, int stride,
19 : uint16_t *pred, int p_col, int p_row,
20 : int p_width, int p_height, int p_stride,
21 : int subsampling_x, int subsampling_y, int bd,
22 : int comp_avg, int16_t alpha, int16_t beta,
23 : int16_t gamma, int16_t delta) {
24 : #if HORSHEAR_REDUCE_PREC_BITS >= 5
25 : __m128i tmp[15];
26 : #else
27 : #error "HORSHEAR_REDUCE_PREC_BITS < 5 not currently supported by SSSE3 filter"
28 : #endif
29 : int i, j, k;
30 :
31 : /* Note: For this code to work, the left/right frame borders need to be
32 : extended by at least 13 pixels each. By the time we get here, other
33 : code will have set up this border, but we allow an explicit check
34 : for debugging purposes.
35 : */
36 : /*for (i = 0; i < height; ++i) {
37 : for (j = 0; j < 13; ++j) {
38 : assert(ref[i * stride - 13 + j] == ref[i * stride]);
39 : assert(ref[i * stride + width + j] == ref[i * stride + (width - 1)]);
40 : }
41 : }*/
42 :
43 0 : for (i = 0; i < p_height; i += 8) {
44 0 : for (j = 0; j < p_width; j += 8) {
45 : // (x, y) coordinates of the center of this block in the destination
46 : // image
47 0 : const int32_t dst_x = p_col + j + 4;
48 0 : const int32_t dst_y = p_row + i + 4;
49 :
50 : int32_t x4, y4, ix4, sx4, iy4, sy4;
51 0 : if (subsampling_x)
52 0 : x4 = (mat[2] * 4 * dst_x + mat[3] * 4 * dst_y + mat[0] * 2 +
53 0 : (mat[2] + mat[3] - (1 << WARPEDMODEL_PREC_BITS))) /
54 : 4;
55 : else
56 0 : x4 = mat[2] * dst_x + mat[3] * dst_y + mat[0];
57 :
58 0 : if (subsampling_y)
59 0 : y4 = (mat[4] * 4 * dst_x + mat[5] * 4 * dst_y + mat[1] * 2 +
60 0 : (mat[4] + mat[5] - (1 << WARPEDMODEL_PREC_BITS))) /
61 : 4;
62 : else
63 0 : y4 = mat[4] * dst_x + mat[5] * dst_y + mat[1];
64 :
65 0 : ix4 = x4 >> WARPEDMODEL_PREC_BITS;
66 0 : sx4 = x4 & ((1 << WARPEDMODEL_PREC_BITS) - 1);
67 0 : iy4 = y4 >> WARPEDMODEL_PREC_BITS;
68 0 : sy4 = y4 & ((1 << WARPEDMODEL_PREC_BITS) - 1);
69 :
70 : // Add in all the constant terms, including rounding and offset
71 0 : sx4 += alpha * (-4) + beta * (-4) + (1 << (WARPEDDIFF_PREC_BITS - 1)) +
72 : (WARPEDPIXEL_PREC_SHIFTS << WARPEDDIFF_PREC_BITS);
73 0 : sy4 += gamma * (-4) + delta * (-4) + (1 << (WARPEDDIFF_PREC_BITS - 1)) +
74 : (WARPEDPIXEL_PREC_SHIFTS << WARPEDDIFF_PREC_BITS);
75 :
76 0 : sx4 &= ~((1 << WARP_PARAM_REDUCE_BITS) - 1);
77 0 : sy4 &= ~((1 << WARP_PARAM_REDUCE_BITS) - 1);
78 :
79 : // Horizontal filter
80 : // If the block is aligned such that, after clamping, every sample
81 : // would be taken from the leftmost/rightmost column, then we can
82 : // skip the expensive horizontal filter.
83 0 : if (ix4 <= -7) {
84 0 : for (k = -7; k < AOMMIN(8, p_height - i); ++k) {
85 0 : int iy = iy4 + k;
86 0 : if (iy < 0)
87 0 : iy = 0;
88 0 : else if (iy > height - 1)
89 0 : iy = height - 1;
90 0 : tmp[k + 7] = _mm_set1_epi16(
91 0 : (1 << (bd + WARPEDPIXEL_FILTER_BITS - HORSHEAR_REDUCE_PREC_BITS -
92 0 : 1)) +
93 0 : ref[iy * stride] *
94 : (1 << (WARPEDPIXEL_FILTER_BITS - HORSHEAR_REDUCE_PREC_BITS)));
95 : }
96 0 : } else if (ix4 >= width + 6) {
97 0 : for (k = -7; k < AOMMIN(8, p_height - i); ++k) {
98 0 : int iy = iy4 + k;
99 0 : if (iy < 0)
100 0 : iy = 0;
101 0 : else if (iy > height - 1)
102 0 : iy = height - 1;
103 0 : tmp[k + 7] = _mm_set1_epi16(
104 0 : (1 << (bd + WARPEDPIXEL_FILTER_BITS - HORSHEAR_REDUCE_PREC_BITS -
105 0 : 1)) +
106 0 : ref[iy * stride + (width - 1)] *
107 : (1 << (WARPEDPIXEL_FILTER_BITS - HORSHEAR_REDUCE_PREC_BITS)));
108 : }
109 : } else {
110 0 : for (k = -7; k < AOMMIN(8, p_height - i); ++k) {
111 0 : int iy = iy4 + k;
112 0 : if (iy < 0)
113 0 : iy = 0;
114 0 : else if (iy > height - 1)
115 0 : iy = height - 1;
116 0 : int sx = sx4 + beta * (k + 4);
117 :
118 : // Load source pixels
119 0 : const __m128i src =
120 0 : _mm_loadu_si128((__m128i *)(ref + iy * stride + ix4 - 7));
121 0 : const __m128i src2 =
122 0 : _mm_loadu_si128((__m128i *)(ref + iy * stride + ix4 + 1));
123 :
124 : // Filter even-index pixels
125 0 : const __m128i tmp_0 = _mm_loadu_si128(
126 0 : (__m128i *)(warped_filter +
127 0 : ((sx + 0 * alpha) >> WARPEDDIFF_PREC_BITS)));
128 0 : const __m128i tmp_2 = _mm_loadu_si128(
129 0 : (__m128i *)(warped_filter +
130 0 : ((sx + 2 * alpha) >> WARPEDDIFF_PREC_BITS)));
131 0 : const __m128i tmp_4 = _mm_loadu_si128(
132 0 : (__m128i *)(warped_filter +
133 0 : ((sx + 4 * alpha) >> WARPEDDIFF_PREC_BITS)));
134 0 : const __m128i tmp_6 = _mm_loadu_si128(
135 0 : (__m128i *)(warped_filter +
136 0 : ((sx + 6 * alpha) >> WARPEDDIFF_PREC_BITS)));
137 :
138 : // coeffs 0 1 0 1 2 3 2 3 for pixels 0, 2
139 0 : const __m128i tmp_8 = _mm_unpacklo_epi32(tmp_0, tmp_2);
140 : // coeffs 0 1 0 1 2 3 2 3 for pixels 4, 6
141 0 : const __m128i tmp_10 = _mm_unpacklo_epi32(tmp_4, tmp_6);
142 : // coeffs 4 5 4 5 6 7 6 7 for pixels 0, 2
143 0 : const __m128i tmp_12 = _mm_unpackhi_epi32(tmp_0, tmp_2);
144 : // coeffs 4 5 4 5 6 7 6 7 for pixels 4, 6
145 0 : const __m128i tmp_14 = _mm_unpackhi_epi32(tmp_4, tmp_6);
146 :
147 : // coeffs 0 1 0 1 0 1 0 1 for pixels 0, 2, 4, 6
148 0 : const __m128i coeff_0 = _mm_unpacklo_epi64(tmp_8, tmp_10);
149 : // coeffs 2 3 2 3 2 3 2 3 for pixels 0, 2, 4, 6
150 0 : const __m128i coeff_2 = _mm_unpackhi_epi64(tmp_8, tmp_10);
151 : // coeffs 4 5 4 5 4 5 4 5 for pixels 0, 2, 4, 6
152 0 : const __m128i coeff_4 = _mm_unpacklo_epi64(tmp_12, tmp_14);
153 : // coeffs 6 7 6 7 6 7 6 7 for pixels 0, 2, 4, 6
154 0 : const __m128i coeff_6 = _mm_unpackhi_epi64(tmp_12, tmp_14);
155 :
156 0 : const __m128i round_const =
157 0 : _mm_set1_epi32((1 << (bd + WARPEDPIXEL_FILTER_BITS - 1)) +
158 : ((1 << HORSHEAR_REDUCE_PREC_BITS) >> 1));
159 :
160 : // Calculate filtered results
161 0 : const __m128i res_0 = _mm_madd_epi16(src, coeff_0);
162 0 : const __m128i res_2 =
163 0 : _mm_madd_epi16(_mm_alignr_epi8(src2, src, 4), coeff_2);
164 0 : const __m128i res_4 =
165 0 : _mm_madd_epi16(_mm_alignr_epi8(src2, src, 8), coeff_4);
166 0 : const __m128i res_6 =
167 0 : _mm_madd_epi16(_mm_alignr_epi8(src2, src, 12), coeff_6);
168 :
169 0 : __m128i res_even = _mm_add_epi32(_mm_add_epi32(res_0, res_4),
170 : _mm_add_epi32(res_2, res_6));
171 0 : res_even = _mm_srai_epi32(_mm_add_epi32(res_even, round_const),
172 : HORSHEAR_REDUCE_PREC_BITS);
173 :
174 : // Filter odd-index pixels
175 0 : const __m128i tmp_1 = _mm_loadu_si128(
176 0 : (__m128i *)(warped_filter +
177 0 : ((sx + 1 * alpha) >> WARPEDDIFF_PREC_BITS)));
178 0 : const __m128i tmp_3 = _mm_loadu_si128(
179 0 : (__m128i *)(warped_filter +
180 0 : ((sx + 3 * alpha) >> WARPEDDIFF_PREC_BITS)));
181 0 : const __m128i tmp_5 = _mm_loadu_si128(
182 0 : (__m128i *)(warped_filter +
183 0 : ((sx + 5 * alpha) >> WARPEDDIFF_PREC_BITS)));
184 0 : const __m128i tmp_7 = _mm_loadu_si128(
185 0 : (__m128i *)(warped_filter +
186 0 : ((sx + 7 * alpha) >> WARPEDDIFF_PREC_BITS)));
187 :
188 0 : const __m128i tmp_9 = _mm_unpacklo_epi32(tmp_1, tmp_3);
189 0 : const __m128i tmp_11 = _mm_unpacklo_epi32(tmp_5, tmp_7);
190 0 : const __m128i tmp_13 = _mm_unpackhi_epi32(tmp_1, tmp_3);
191 0 : const __m128i tmp_15 = _mm_unpackhi_epi32(tmp_5, tmp_7);
192 :
193 0 : const __m128i coeff_1 = _mm_unpacklo_epi64(tmp_9, tmp_11);
194 0 : const __m128i coeff_3 = _mm_unpackhi_epi64(tmp_9, tmp_11);
195 0 : const __m128i coeff_5 = _mm_unpacklo_epi64(tmp_13, tmp_15);
196 0 : const __m128i coeff_7 = _mm_unpackhi_epi64(tmp_13, tmp_15);
197 :
198 0 : const __m128i res_1 =
199 0 : _mm_madd_epi16(_mm_alignr_epi8(src2, src, 2), coeff_1);
200 0 : const __m128i res_3 =
201 0 : _mm_madd_epi16(_mm_alignr_epi8(src2, src, 6), coeff_3);
202 0 : const __m128i res_5 =
203 0 : _mm_madd_epi16(_mm_alignr_epi8(src2, src, 10), coeff_5);
204 0 : const __m128i res_7 =
205 0 : _mm_madd_epi16(_mm_alignr_epi8(src2, src, 14), coeff_7);
206 :
207 0 : __m128i res_odd = _mm_add_epi32(_mm_add_epi32(res_1, res_5),
208 : _mm_add_epi32(res_3, res_7));
209 0 : res_odd = _mm_srai_epi32(_mm_add_epi32(res_odd, round_const),
210 : HORSHEAR_REDUCE_PREC_BITS);
211 :
212 : // Combine results into one register.
213 : // We store the columns in the order 0, 2, 4, 6, 1, 3, 5, 7
214 : // as this order helps with the vertical filter.
215 0 : tmp[k + 7] = _mm_packs_epi32(res_even, res_odd);
216 : }
217 : }
218 :
219 : // Vertical filter
220 0 : for (k = -4; k < AOMMIN(4, p_height - i - 4); ++k) {
221 0 : int sy = sy4 + delta * (k + 4);
222 :
223 : // Load from tmp and rearrange pairs of consecutive rows into the
224 : // column order 0 0 2 2 4 4 6 6; 1 1 3 3 5 5 7 7
225 0 : const __m128i *src = tmp + (k + 4);
226 0 : const __m128i src_0 = _mm_unpacklo_epi16(src[0], src[1]);
227 0 : const __m128i src_2 = _mm_unpacklo_epi16(src[2], src[3]);
228 0 : const __m128i src_4 = _mm_unpacklo_epi16(src[4], src[5]);
229 0 : const __m128i src_6 = _mm_unpacklo_epi16(src[6], src[7]);
230 :
231 : // Filter even-index pixels
232 0 : const __m128i tmp_0 = _mm_loadu_si128(
233 0 : (__m128i *)(warped_filter +
234 0 : ((sy + 0 * gamma) >> WARPEDDIFF_PREC_BITS)));
235 0 : const __m128i tmp_2 = _mm_loadu_si128(
236 0 : (__m128i *)(warped_filter +
237 0 : ((sy + 2 * gamma) >> WARPEDDIFF_PREC_BITS)));
238 0 : const __m128i tmp_4 = _mm_loadu_si128(
239 0 : (__m128i *)(warped_filter +
240 0 : ((sy + 4 * gamma) >> WARPEDDIFF_PREC_BITS)));
241 0 : const __m128i tmp_6 = _mm_loadu_si128(
242 0 : (__m128i *)(warped_filter +
243 0 : ((sy + 6 * gamma) >> WARPEDDIFF_PREC_BITS)));
244 :
245 0 : const __m128i tmp_8 = _mm_unpacklo_epi32(tmp_0, tmp_2);
246 0 : const __m128i tmp_10 = _mm_unpacklo_epi32(tmp_4, tmp_6);
247 0 : const __m128i tmp_12 = _mm_unpackhi_epi32(tmp_0, tmp_2);
248 0 : const __m128i tmp_14 = _mm_unpackhi_epi32(tmp_4, tmp_6);
249 :
250 0 : const __m128i coeff_0 = _mm_unpacklo_epi64(tmp_8, tmp_10);
251 0 : const __m128i coeff_2 = _mm_unpackhi_epi64(tmp_8, tmp_10);
252 0 : const __m128i coeff_4 = _mm_unpacklo_epi64(tmp_12, tmp_14);
253 0 : const __m128i coeff_6 = _mm_unpackhi_epi64(tmp_12, tmp_14);
254 :
255 0 : const __m128i res_0 = _mm_madd_epi16(src_0, coeff_0);
256 0 : const __m128i res_2 = _mm_madd_epi16(src_2, coeff_2);
257 0 : const __m128i res_4 = _mm_madd_epi16(src_4, coeff_4);
258 0 : const __m128i res_6 = _mm_madd_epi16(src_6, coeff_6);
259 :
260 0 : const __m128i res_even = _mm_add_epi32(_mm_add_epi32(res_0, res_2),
261 : _mm_add_epi32(res_4, res_6));
262 :
263 : // Filter odd-index pixels
264 0 : const __m128i src_1 = _mm_unpackhi_epi16(src[0], src[1]);
265 0 : const __m128i src_3 = _mm_unpackhi_epi16(src[2], src[3]);
266 0 : const __m128i src_5 = _mm_unpackhi_epi16(src[4], src[5]);
267 0 : const __m128i src_7 = _mm_unpackhi_epi16(src[6], src[7]);
268 :
269 0 : const __m128i tmp_1 = _mm_loadu_si128(
270 0 : (__m128i *)(warped_filter +
271 0 : ((sy + 1 * gamma) >> WARPEDDIFF_PREC_BITS)));
272 0 : const __m128i tmp_3 = _mm_loadu_si128(
273 0 : (__m128i *)(warped_filter +
274 0 : ((sy + 3 * gamma) >> WARPEDDIFF_PREC_BITS)));
275 0 : const __m128i tmp_5 = _mm_loadu_si128(
276 0 : (__m128i *)(warped_filter +
277 0 : ((sy + 5 * gamma) >> WARPEDDIFF_PREC_BITS)));
278 0 : const __m128i tmp_7 = _mm_loadu_si128(
279 0 : (__m128i *)(warped_filter +
280 0 : ((sy + 7 * gamma) >> WARPEDDIFF_PREC_BITS)));
281 :
282 0 : const __m128i tmp_9 = _mm_unpacklo_epi32(tmp_1, tmp_3);
283 0 : const __m128i tmp_11 = _mm_unpacklo_epi32(tmp_5, tmp_7);
284 0 : const __m128i tmp_13 = _mm_unpackhi_epi32(tmp_1, tmp_3);
285 0 : const __m128i tmp_15 = _mm_unpackhi_epi32(tmp_5, tmp_7);
286 :
287 0 : const __m128i coeff_1 = _mm_unpacklo_epi64(tmp_9, tmp_11);
288 0 : const __m128i coeff_3 = _mm_unpackhi_epi64(tmp_9, tmp_11);
289 0 : const __m128i coeff_5 = _mm_unpacklo_epi64(tmp_13, tmp_15);
290 0 : const __m128i coeff_7 = _mm_unpackhi_epi64(tmp_13, tmp_15);
291 :
292 0 : const __m128i res_1 = _mm_madd_epi16(src_1, coeff_1);
293 0 : const __m128i res_3 = _mm_madd_epi16(src_3, coeff_3);
294 0 : const __m128i res_5 = _mm_madd_epi16(src_5, coeff_5);
295 0 : const __m128i res_7 = _mm_madd_epi16(src_7, coeff_7);
296 :
297 0 : const __m128i res_odd = _mm_add_epi32(_mm_add_epi32(res_1, res_3),
298 : _mm_add_epi32(res_5, res_7));
299 :
300 : // Rearrange pixels back into the order 0 ... 7
301 0 : const __m128i res_lo = _mm_unpacklo_epi32(res_even, res_odd);
302 0 : const __m128i res_hi = _mm_unpackhi_epi32(res_even, res_odd);
303 :
304 : // Round and pack into 8 bits
305 0 : const __m128i round_const =
306 0 : _mm_set1_epi32(-(1 << (bd + VERSHEAR_REDUCE_PREC_BITS - 1)) +
307 : ((1 << VERSHEAR_REDUCE_PREC_BITS) >> 1));
308 :
309 0 : const __m128i res_lo_round = _mm_srai_epi32(
310 : _mm_add_epi32(res_lo, round_const), VERSHEAR_REDUCE_PREC_BITS);
311 0 : const __m128i res_hi_round = _mm_srai_epi32(
312 : _mm_add_epi32(res_hi, round_const), VERSHEAR_REDUCE_PREC_BITS);
313 :
314 0 : __m128i res_16bit = _mm_packs_epi32(res_lo_round, res_hi_round);
315 : // Clamp res_16bit to the range [0, 2^bd - 1]
316 0 : const __m128i max_val = _mm_set1_epi16((1 << bd) - 1);
317 0 : const __m128i zero = _mm_setzero_si128();
318 0 : res_16bit = _mm_max_epi16(_mm_min_epi16(res_16bit, max_val), zero);
319 :
320 : // Store, blending with 'pred' if needed
321 0 : __m128i *const p = (__m128i *)&pred[(i + k + 4) * p_stride + j];
322 :
323 : // Note: If we're outputting a 4x4 block, we need to be very careful
324 : // to only output 4 pixels at this point, to avoid encode/decode
325 : // mismatches when encoding with multiple threads.
326 0 : if (p_width == 4) {
327 0 : if (comp_avg)
328 0 : res_16bit = _mm_avg_epu16(res_16bit, _mm_loadl_epi64(p));
329 : _mm_storel_epi64(p, res_16bit);
330 : } else {
331 0 : if (comp_avg)
332 0 : res_16bit = _mm_avg_epu16(res_16bit, _mm_loadu_si128(p));
333 : _mm_storeu_si128(p, res_16bit);
334 : }
335 : }
336 : }
337 : }
338 0 : }
|