• 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 #ifndef AOM_AV1_COMMON_CDEF_BLOCK_SIMD_H_
13 #define AOM_AV1_COMMON_CDEF_BLOCK_SIMD_H_
14 
15 #include "config/av1_rtcd.h"
16 
17 #include "av1/common/cdef_block.h"
18 
19 /* partial A is a 16-bit vector of the form:
20    [x8 x7 x6 x5 x4 x3 x2 x1] and partial B has the form:
21    [0  y1 y2 y3 y4 y5 y6 y7].
22    This function computes (x1^2+y1^2)*C1 + (x2^2+y2^2)*C2 + ...
23    (x7^2+y2^7)*C7 + (x8^2+0^2)*C8 where the C1..C8 constants are in const1
24    and const2. */
fold_mul_and_sum(v128 partiala,v128 partialb,v128 const1,v128 const2)25 static INLINE v128 fold_mul_and_sum(v128 partiala, v128 partialb, v128 const1,
26                                     v128 const2) {
27   v128 tmp;
28   /* Reverse partial B. */
29   partialb = v128_shuffle_8(
30       partialb, v128_from_32(0x0f0e0100, 0x03020504, 0x07060908, 0x0b0a0d0c));
31   /* Interleave the x and y values of identical indices and pair x8 with 0. */
32   tmp = partiala;
33   partiala = v128_ziplo_16(partialb, partiala);
34   partialb = v128_ziphi_16(partialb, tmp);
35   /* Square and add the corresponding x and y values. */
36   partiala = v128_madd_s16(partiala, partiala);
37   partialb = v128_madd_s16(partialb, partialb);
38   /* Multiply by constant. */
39   partiala = v128_mullo_s32(partiala, const1);
40   partialb = v128_mullo_s32(partialb, const2);
41   /* Sum all results. */
42   partiala = v128_add_32(partiala, partialb);
43   return partiala;
44 }
45 
hsum4(v128 x0,v128 x1,v128 x2,v128 x3)46 static INLINE v128 hsum4(v128 x0, v128 x1, v128 x2, v128 x3) {
47   v128 t0, t1, t2, t3;
48   t0 = v128_ziplo_32(x1, x0);
49   t1 = v128_ziplo_32(x3, x2);
50   t2 = v128_ziphi_32(x1, x0);
51   t3 = v128_ziphi_32(x3, x2);
52   x0 = v128_ziplo_64(t1, t0);
53   x1 = v128_ziphi_64(t1, t0);
54   x2 = v128_ziplo_64(t3, t2);
55   x3 = v128_ziphi_64(t3, t2);
56   return v128_add_32(v128_add_32(x0, x1), v128_add_32(x2, x3));
57 }
58 
59 /* Computes cost for directions 0, 5, 6 and 7. We can call this function again
60    to compute the remaining directions. */
compute_directions(v128 lines[8],int32_t tmp_cost1[4])61 static INLINE v128 compute_directions(v128 lines[8], int32_t tmp_cost1[4]) {
62   v128 partial4a, partial4b, partial5a, partial5b, partial7a, partial7b;
63   v128 partial6;
64   v128 tmp;
65   /* Partial sums for lines 0 and 1. */
66   partial4a = v128_shl_n_byte(lines[0], 14);
67   partial4b = v128_shr_n_byte(lines[0], 2);
68   partial4a = v128_add_16(partial4a, v128_shl_n_byte(lines[1], 12));
69   partial4b = v128_add_16(partial4b, v128_shr_n_byte(lines[1], 4));
70   tmp = v128_add_16(lines[0], lines[1]);
71   partial5a = v128_shl_n_byte(tmp, 10);
72   partial5b = v128_shr_n_byte(tmp, 6);
73   partial7a = v128_shl_n_byte(tmp, 4);
74   partial7b = v128_shr_n_byte(tmp, 12);
75   partial6 = tmp;
76 
77   /* Partial sums for lines 2 and 3. */
78   partial4a = v128_add_16(partial4a, v128_shl_n_byte(lines[2], 10));
79   partial4b = v128_add_16(partial4b, v128_shr_n_byte(lines[2], 6));
80   partial4a = v128_add_16(partial4a, v128_shl_n_byte(lines[3], 8));
81   partial4b = v128_add_16(partial4b, v128_shr_n_byte(lines[3], 8));
82   tmp = v128_add_16(lines[2], lines[3]);
83   partial5a = v128_add_16(partial5a, v128_shl_n_byte(tmp, 8));
84   partial5b = v128_add_16(partial5b, v128_shr_n_byte(tmp, 8));
85   partial7a = v128_add_16(partial7a, v128_shl_n_byte(tmp, 6));
86   partial7b = v128_add_16(partial7b, v128_shr_n_byte(tmp, 10));
87   partial6 = v128_add_16(partial6, tmp);
88 
89   /* Partial sums for lines 4 and 5. */
90   partial4a = v128_add_16(partial4a, v128_shl_n_byte(lines[4], 6));
91   partial4b = v128_add_16(partial4b, v128_shr_n_byte(lines[4], 10));
92   partial4a = v128_add_16(partial4a, v128_shl_n_byte(lines[5], 4));
93   partial4b = v128_add_16(partial4b, v128_shr_n_byte(lines[5], 12));
94   tmp = v128_add_16(lines[4], lines[5]);
95   partial5a = v128_add_16(partial5a, v128_shl_n_byte(tmp, 6));
96   partial5b = v128_add_16(partial5b, v128_shr_n_byte(tmp, 10));
97   partial7a = v128_add_16(partial7a, v128_shl_n_byte(tmp, 8));
98   partial7b = v128_add_16(partial7b, v128_shr_n_byte(tmp, 8));
99   partial6 = v128_add_16(partial6, tmp);
100 
101   /* Partial sums for lines 6 and 7. */
102   partial4a = v128_add_16(partial4a, v128_shl_n_byte(lines[6], 2));
103   partial4b = v128_add_16(partial4b, v128_shr_n_byte(lines[6], 14));
104   partial4a = v128_add_16(partial4a, lines[7]);
105   tmp = v128_add_16(lines[6], lines[7]);
106   partial5a = v128_add_16(partial5a, v128_shl_n_byte(tmp, 4));
107   partial5b = v128_add_16(partial5b, v128_shr_n_byte(tmp, 12));
108   partial7a = v128_add_16(partial7a, v128_shl_n_byte(tmp, 10));
109   partial7b = v128_add_16(partial7b, v128_shr_n_byte(tmp, 6));
110   partial6 = v128_add_16(partial6, tmp);
111 
112   /* Compute costs in terms of partial sums. */
113   partial4a =
114       fold_mul_and_sum(partial4a, partial4b, v128_from_32(210, 280, 420, 840),
115                        v128_from_32(105, 120, 140, 168));
116   partial7a =
117       fold_mul_and_sum(partial7a, partial7b, v128_from_32(210, 420, 0, 0),
118                        v128_from_32(105, 105, 105, 140));
119   partial5a =
120       fold_mul_and_sum(partial5a, partial5b, v128_from_32(210, 420, 0, 0),
121                        v128_from_32(105, 105, 105, 140));
122   partial6 = v128_madd_s16(partial6, partial6);
123   partial6 = v128_mullo_s32(partial6, v128_dup_32(105));
124 
125   partial4a = hsum4(partial4a, partial5a, partial6, partial7a);
126   v128_store_unaligned(tmp_cost1, partial4a);
127   return partial4a;
128 }
129 
130 /* transpose and reverse the order of the lines -- equivalent to a 90-degree
131    counter-clockwise rotation of the pixels. */
array_reverse_transpose_8x8(v128 * in,v128 * res)132 static INLINE void array_reverse_transpose_8x8(v128 *in, v128 *res) {
133   const v128 tr0_0 = v128_ziplo_16(in[1], in[0]);
134   const v128 tr0_1 = v128_ziplo_16(in[3], in[2]);
135   const v128 tr0_2 = v128_ziphi_16(in[1], in[0]);
136   const v128 tr0_3 = v128_ziphi_16(in[3], in[2]);
137   const v128 tr0_4 = v128_ziplo_16(in[5], in[4]);
138   const v128 tr0_5 = v128_ziplo_16(in[7], in[6]);
139   const v128 tr0_6 = v128_ziphi_16(in[5], in[4]);
140   const v128 tr0_7 = v128_ziphi_16(in[7], in[6]);
141 
142   const v128 tr1_0 = v128_ziplo_32(tr0_1, tr0_0);
143   const v128 tr1_1 = v128_ziplo_32(tr0_5, tr0_4);
144   const v128 tr1_2 = v128_ziphi_32(tr0_1, tr0_0);
145   const v128 tr1_3 = v128_ziphi_32(tr0_5, tr0_4);
146   const v128 tr1_4 = v128_ziplo_32(tr0_3, tr0_2);
147   const v128 tr1_5 = v128_ziplo_32(tr0_7, tr0_6);
148   const v128 tr1_6 = v128_ziphi_32(tr0_3, tr0_2);
149   const v128 tr1_7 = v128_ziphi_32(tr0_7, tr0_6);
150 
151   res[7] = v128_ziplo_64(tr1_1, tr1_0);
152   res[6] = v128_ziphi_64(tr1_1, tr1_0);
153   res[5] = v128_ziplo_64(tr1_3, tr1_2);
154   res[4] = v128_ziphi_64(tr1_3, tr1_2);
155   res[3] = v128_ziplo_64(tr1_5, tr1_4);
156   res[2] = v128_ziphi_64(tr1_5, tr1_4);
157   res[1] = v128_ziplo_64(tr1_7, tr1_6);
158   res[0] = v128_ziphi_64(tr1_7, tr1_6);
159 }
160 
SIMD_FUNC(cdef_find_dir)161 int SIMD_FUNC(cdef_find_dir)(const uint16_t *img, int stride, int32_t *var,
162                              int coeff_shift) {
163   int i;
164   int32_t cost[8];
165   int32_t best_cost = 0;
166   int best_dir = 0;
167   v128 lines[8];
168   for (i = 0; i < 8; i++) {
169     lines[i] = v128_load_unaligned(&img[i * stride]);
170     lines[i] =
171         v128_sub_16(v128_shr_s16(lines[i], coeff_shift), v128_dup_16(128));
172   }
173 
174   /* Compute "mostly vertical" directions. */
175   v128 dir47 = compute_directions(lines, cost + 4);
176 
177   array_reverse_transpose_8x8(lines, lines);
178 
179   /* Compute "mostly horizontal" directions. */
180   v128 dir03 = compute_directions(lines, cost);
181 
182   v128 max = v128_max_s32(dir03, dir47);
183   max = v128_max_s32(max, v128_align(max, max, 8));
184   max = v128_max_s32(max, v128_align(max, max, 4));
185   best_cost = v128_low_u32(max);
186   v128 t =
187       v128_pack_s32_s16(v128_cmpeq_32(max, dir47), v128_cmpeq_32(max, dir03));
188   best_dir = v128_movemask_8(v128_pack_s16_s8(t, t));
189   best_dir = get_msb(best_dir ^ (best_dir - 1));  // Count trailing zeros
190 
191   /* Difference between the optimal variance and the variance along the
192      orthogonal direction. Again, the sum(x^2) terms cancel out. */
193   *var = best_cost - cost[(best_dir + 4) & 7];
194   /* We'd normally divide by 840, but dividing by 1024 is close enough
195      for what we're going to do with this. */
196   *var >>= 10;
197   return best_dir;
198 }
199 
200 // Work around compiler out of memory issues with Win32 builds. This issue has
201 // been observed with Visual Studio 2017, 2019, and 2022 (version 17.4).
202 #if defined(_MSC_VER) && defined(_M_IX86) && _MSC_VER < 1940
203 #define CDEF_INLINE static INLINE
204 #else
205 #define CDEF_INLINE SIMD_INLINE
206 #endif
207 
208 // sign(a-b) * min(abs(a-b), max(0, threshold - (abs(a-b) >> adjdamp)))
constrain16(v256 a,v256 b,unsigned int threshold,unsigned int adjdamp)209 CDEF_INLINE v256 constrain16(v256 a, v256 b, unsigned int threshold,
210                              unsigned int adjdamp) {
211   v256 diff = v256_sub_16(a, b);
212   const v256 sign = v256_shr_n_s16(diff, 15);
213   diff = v256_abs_s16(diff);
214   const v256 s =
215       v256_ssub_u16(v256_dup_16(threshold), v256_shr_u16(diff, adjdamp));
216   return v256_xor(v256_add_16(sign, v256_min_s16(diff, s)), sign);
217 }
218 
get_max_primary(const int is_lowbd,v256 * tap,v256 max,v256 cdef_large_value_mask)219 SIMD_INLINE v256 get_max_primary(const int is_lowbd, v256 *tap, v256 max,
220                                  v256 cdef_large_value_mask) {
221   if (is_lowbd) {
222     v256 max_u8;
223     max_u8 = tap[0];
224     max_u8 = v256_max_u8(max_u8, tap[1]);
225     max_u8 = v256_max_u8(max_u8, tap[2]);
226     max_u8 = v256_max_u8(max_u8, tap[3]);
227     /* The source is 16 bits, however, we only really care about the lower
228     8 bits.  The upper 8 bits contain the "large" flag.  After the final
229     primary max has been calculated, zero out the upper 8 bits.  Use this
230     to find the "16 bit" max. */
231     max = v256_max_s16(max, v256_and(max_u8, cdef_large_value_mask));
232   } else {
233     /* Convert CDEF_VERY_LARGE to 0 before calculating max. */
234     max = v256_max_s16(max, v256_and(tap[0], cdef_large_value_mask));
235     max = v256_max_s16(max, v256_and(tap[1], cdef_large_value_mask));
236     max = v256_max_s16(max, v256_and(tap[2], cdef_large_value_mask));
237     max = v256_max_s16(max, v256_and(tap[3], cdef_large_value_mask));
238   }
239   return max;
240 }
241 
get_max_secondary(const int is_lowbd,v256 * tap,v256 max,v256 cdef_large_value_mask)242 SIMD_INLINE v256 get_max_secondary(const int is_lowbd, v256 *tap, v256 max,
243                                    v256 cdef_large_value_mask) {
244   if (is_lowbd) {
245     v256 max_u8;
246     max_u8 = tap[0];
247     max_u8 = v256_max_u8(max_u8, tap[1]);
248     max_u8 = v256_max_u8(max_u8, tap[2]);
249     max_u8 = v256_max_u8(max_u8, tap[3]);
250     max_u8 = v256_max_u8(max_u8, tap[4]);
251     max_u8 = v256_max_u8(max_u8, tap[5]);
252     max_u8 = v256_max_u8(max_u8, tap[6]);
253     max_u8 = v256_max_u8(max_u8, tap[7]);
254     /* The source is 16 bits, however, we only really care about the lower
255     8 bits.  The upper 8 bits contain the "large" flag.  After the final
256     primary max has been calculated, zero out the upper 8 bits.  Use this
257     to find the "16 bit" max. */
258     max = v256_max_s16(max, v256_and(max_u8, cdef_large_value_mask));
259   } else {
260     /* Convert CDEF_VERY_LARGE to 0 before calculating max. */
261     max = v256_max_s16(max, v256_and(tap[0], cdef_large_value_mask));
262     max = v256_max_s16(max, v256_and(tap[1], cdef_large_value_mask));
263     max = v256_max_s16(max, v256_and(tap[2], cdef_large_value_mask));
264     max = v256_max_s16(max, v256_and(tap[3], cdef_large_value_mask));
265     max = v256_max_s16(max, v256_and(tap[4], cdef_large_value_mask));
266     max = v256_max_s16(max, v256_and(tap[5], cdef_large_value_mask));
267     max = v256_max_s16(max, v256_and(tap[6], cdef_large_value_mask));
268     max = v256_max_s16(max, v256_and(tap[7], cdef_large_value_mask));
269   }
270   return max;
271 }
272 
273 // MSVC takes far too much time optimizing these.
274 // https://bugs.chromium.org/p/aomedia/issues/detail?id=3395
275 #if defined(_MSC_VER) && !defined(__clang__)
276 #pragma optimize("", off)
277 #endif
278 
filter_block_4x4(const int is_lowbd,void * dest,int dstride,const uint16_t * in,int pri_strength,int sec_strength,int dir,int pri_damping,int sec_damping,int coeff_shift,int height,int enable_primary,int enable_secondary)279 CDEF_INLINE void filter_block_4x4(const int is_lowbd, void *dest, int dstride,
280                                   const uint16_t *in, int pri_strength,
281                                   int sec_strength, int dir, int pri_damping,
282                                   int sec_damping, int coeff_shift, int height,
283                                   int enable_primary, int enable_secondary) {
284   uint8_t *dst8 = (uint8_t *)dest;
285   uint16_t *dst16 = (uint16_t *)dest;
286   const int clipping_required = enable_primary && enable_secondary;
287   v256 p0, p1, p2, p3;
288   v256 sum, row, res;
289   v256 max, min;
290   const v256 cdef_large_value_mask = v256_dup_16((uint16_t)~CDEF_VERY_LARGE);
291   const int po1 = cdef_directions[dir][0];
292   const int po2 = cdef_directions[dir][1];
293   const int s1o1 = cdef_directions[dir + 2][0];
294   const int s1o2 = cdef_directions[dir + 2][1];
295   const int s2o1 = cdef_directions[dir - 2][0];
296   const int s2o2 = cdef_directions[dir - 2][1];
297   const int *pri_taps = cdef_pri_taps[(pri_strength >> coeff_shift) & 1];
298   const int *sec_taps = cdef_sec_taps;
299   int i;
300 
301   if (enable_primary && pri_strength)
302     pri_damping = AOMMAX(0, pri_damping - get_msb(pri_strength));
303   if (enable_secondary && sec_strength)
304     sec_damping = AOMMAX(0, sec_damping - get_msb(sec_strength));
305 
306   for (i = 0; i < height; i += 4) {
307     sum = v256_zero();
308     row = v256_from_v64(v64_load_aligned(&in[(i + 0) * CDEF_BSTRIDE]),
309                         v64_load_aligned(&in[(i + 1) * CDEF_BSTRIDE]),
310                         v64_load_aligned(&in[(i + 2) * CDEF_BSTRIDE]),
311                         v64_load_aligned(&in[(i + 3) * CDEF_BSTRIDE]));
312     max = min = row;
313 
314     if (enable_primary) {
315       v256 tap[4];
316       // Primary near taps
317       tap[0] =
318           v256_from_v64(v64_load_unaligned(&in[(i + 0) * CDEF_BSTRIDE + po1]),
319                         v64_load_unaligned(&in[(i + 1) * CDEF_BSTRIDE + po1]),
320                         v64_load_unaligned(&in[(i + 2) * CDEF_BSTRIDE + po1]),
321                         v64_load_unaligned(&in[(i + 3) * CDEF_BSTRIDE + po1]));
322       p0 = constrain16(tap[0], row, pri_strength, pri_damping);
323       tap[1] =
324           v256_from_v64(v64_load_unaligned(&in[(i + 0) * CDEF_BSTRIDE - po1]),
325                         v64_load_unaligned(&in[(i + 1) * CDEF_BSTRIDE - po1]),
326                         v64_load_unaligned(&in[(i + 2) * CDEF_BSTRIDE - po1]),
327                         v64_load_unaligned(&in[(i + 3) * CDEF_BSTRIDE - po1]));
328       p1 = constrain16(tap[1], row, pri_strength, pri_damping);
329 
330       // sum += pri_taps[0] * (p0 + p1)
331       sum = v256_add_16(
332           sum, v256_mullo_s16(v256_dup_16(pri_taps[0]), v256_add_16(p0, p1)));
333 
334       // Primary far taps
335       tap[2] =
336           v256_from_v64(v64_load_unaligned(&in[(i + 0) * CDEF_BSTRIDE + po2]),
337                         v64_load_unaligned(&in[(i + 1) * CDEF_BSTRIDE + po2]),
338                         v64_load_unaligned(&in[(i + 2) * CDEF_BSTRIDE + po2]),
339                         v64_load_unaligned(&in[(i + 3) * CDEF_BSTRIDE + po2]));
340       p0 = constrain16(tap[2], row, pri_strength, pri_damping);
341       tap[3] =
342           v256_from_v64(v64_load_unaligned(&in[(i + 0) * CDEF_BSTRIDE - po2]),
343                         v64_load_unaligned(&in[(i + 1) * CDEF_BSTRIDE - po2]),
344                         v64_load_unaligned(&in[(i + 2) * CDEF_BSTRIDE - po2]),
345                         v64_load_unaligned(&in[(i + 3) * CDEF_BSTRIDE - po2]));
346       p1 = constrain16(tap[3], row, pri_strength, pri_damping);
347 
348       // sum += pri_taps[1] * (p0 + p1)
349       sum = v256_add_16(
350           sum, v256_mullo_s16(v256_dup_16(pri_taps[1]), v256_add_16(p0, p1)));
351       if (clipping_required) {
352         max = get_max_primary(is_lowbd, tap, max, cdef_large_value_mask);
353 
354         min = v256_min_s16(min, tap[0]);
355         min = v256_min_s16(min, tap[1]);
356         min = v256_min_s16(min, tap[2]);
357         min = v256_min_s16(min, tap[3]);
358       }
359     }
360 
361     if (enable_secondary) {
362       v256 tap[8];
363       // Secondary near taps
364       tap[0] =
365           v256_from_v64(v64_load_unaligned(&in[(i + 0) * CDEF_BSTRIDE + s1o1]),
366                         v64_load_unaligned(&in[(i + 1) * CDEF_BSTRIDE + s1o1]),
367                         v64_load_unaligned(&in[(i + 2) * CDEF_BSTRIDE + s1o1]),
368                         v64_load_unaligned(&in[(i + 3) * CDEF_BSTRIDE + s1o1]));
369       p0 = constrain16(tap[0], row, sec_strength, sec_damping);
370       tap[1] =
371           v256_from_v64(v64_load_unaligned(&in[(i + 0) * CDEF_BSTRIDE - s1o1]),
372                         v64_load_unaligned(&in[(i + 1) * CDEF_BSTRIDE - s1o1]),
373                         v64_load_unaligned(&in[(i + 2) * CDEF_BSTRIDE - s1o1]),
374                         v64_load_unaligned(&in[(i + 3) * CDEF_BSTRIDE - s1o1]));
375       p1 = constrain16(tap[1], row, sec_strength, sec_damping);
376       tap[2] =
377           v256_from_v64(v64_load_unaligned(&in[(i + 0) * CDEF_BSTRIDE + s2o1]),
378                         v64_load_unaligned(&in[(i + 1) * CDEF_BSTRIDE + s2o1]),
379                         v64_load_unaligned(&in[(i + 2) * CDEF_BSTRIDE + s2o1]),
380                         v64_load_unaligned(&in[(i + 3) * CDEF_BSTRIDE + s2o1]));
381       p2 = constrain16(tap[2], row, sec_strength, sec_damping);
382       tap[3] =
383           v256_from_v64(v64_load_unaligned(&in[(i + 0) * CDEF_BSTRIDE - s2o1]),
384                         v64_load_unaligned(&in[(i + 1) * CDEF_BSTRIDE - s2o1]),
385                         v64_load_unaligned(&in[(i + 2) * CDEF_BSTRIDE - s2o1]),
386                         v64_load_unaligned(&in[(i + 3) * CDEF_BSTRIDE - s2o1]));
387       p3 = constrain16(tap[3], row, sec_strength, sec_damping);
388 
389       // sum += sec_taps[0] * (p0 + p1 + p2 + p3)
390       sum = v256_add_16(sum, v256_mullo_s16(v256_dup_16(sec_taps[0]),
391                                             v256_add_16(v256_add_16(p0, p1),
392                                                         v256_add_16(p2, p3))));
393 
394       // Secondary far taps
395       tap[4] =
396           v256_from_v64(v64_load_unaligned(&in[(i + 0) * CDEF_BSTRIDE + s1o2]),
397                         v64_load_unaligned(&in[(i + 1) * CDEF_BSTRIDE + s1o2]),
398                         v64_load_unaligned(&in[(i + 2) * CDEF_BSTRIDE + s1o2]),
399                         v64_load_unaligned(&in[(i + 3) * CDEF_BSTRIDE + s1o2]));
400       p0 = constrain16(tap[4], row, sec_strength, sec_damping);
401       tap[5] =
402           v256_from_v64(v64_load_unaligned(&in[(i + 0) * CDEF_BSTRIDE - s1o2]),
403                         v64_load_unaligned(&in[(i + 1) * CDEF_BSTRIDE - s1o2]),
404                         v64_load_unaligned(&in[(i + 2) * CDEF_BSTRIDE - s1o2]),
405                         v64_load_unaligned(&in[(i + 3) * CDEF_BSTRIDE - s1o2]));
406       p1 = constrain16(tap[5], row, sec_strength, sec_damping);
407       tap[6] =
408           v256_from_v64(v64_load_unaligned(&in[(i + 0) * CDEF_BSTRIDE + s2o2]),
409                         v64_load_unaligned(&in[(i + 1) * CDEF_BSTRIDE + s2o2]),
410                         v64_load_unaligned(&in[(i + 2) * CDEF_BSTRIDE + s2o2]),
411                         v64_load_unaligned(&in[(i + 3) * CDEF_BSTRIDE + s2o2]));
412       p2 = constrain16(tap[6], row, sec_strength, sec_damping);
413       tap[7] =
414           v256_from_v64(v64_load_unaligned(&in[(i + 0) * CDEF_BSTRIDE - s2o2]),
415                         v64_load_unaligned(&in[(i + 1) * CDEF_BSTRIDE - s2o2]),
416                         v64_load_unaligned(&in[(i + 2) * CDEF_BSTRIDE - s2o2]),
417                         v64_load_unaligned(&in[(i + 3) * CDEF_BSTRIDE - s2o2]));
418       p3 = constrain16(tap[7], row, sec_strength, sec_damping);
419 
420       // sum += sec_taps[1] * (p0 + p1 + p2 + p3)
421       sum = v256_add_16(sum, v256_mullo_s16(v256_dup_16(sec_taps[1]),
422                                             v256_add_16(v256_add_16(p0, p1),
423                                                         v256_add_16(p2, p3))));
424 
425       if (clipping_required) {
426         max = get_max_secondary(is_lowbd, tap, max, cdef_large_value_mask);
427 
428         min = v256_min_s16(min, tap[0]);
429         min = v256_min_s16(min, tap[1]);
430         min = v256_min_s16(min, tap[2]);
431         min = v256_min_s16(min, tap[3]);
432         min = v256_min_s16(min, tap[4]);
433         min = v256_min_s16(min, tap[5]);
434         min = v256_min_s16(min, tap[6]);
435         min = v256_min_s16(min, tap[7]);
436       }
437     }
438 
439     // res = row + ((sum - (sum < 0) + 8) >> 4)
440     sum = v256_add_16(sum, v256_cmplt_s16(sum, v256_zero()));
441     res = v256_add_16(sum, v256_dup_16(8));
442     res = v256_shr_n_s16(res, 4);
443     res = v256_add_16(row, res);
444     if (clipping_required) {
445       res = v256_min_s16(v256_max_s16(res, min), max);
446     }
447 
448     if (is_lowbd) {
449       const v128 res_128 = v256_low_v128(v256_pack_s16_u8(res, res));
450       u32_store_aligned(&dst8[(i + 0) * dstride],
451                         v64_high_u32(v128_high_v64(res_128)));
452       u32_store_aligned(&dst8[(i + 1) * dstride],
453                         v64_low_u32(v128_high_v64(res_128)));
454       u32_store_aligned(&dst8[(i + 2) * dstride],
455                         v64_high_u32(v128_low_v64(res_128)));
456       u32_store_aligned(&dst8[(i + 3) * dstride],
457                         v64_low_u32(v128_low_v64(res_128)));
458     } else {
459       v64_store_aligned(&dst16[(i + 0) * dstride],
460                         v128_high_v64(v256_high_v128(res)));
461       v64_store_aligned(&dst16[(i + 1) * dstride],
462                         v128_low_v64(v256_high_v128(res)));
463       v64_store_aligned(&dst16[(i + 2) * dstride],
464                         v128_high_v64(v256_low_v128(res)));
465       v64_store_aligned(&dst16[(i + 3) * dstride],
466                         v128_low_v64(v256_low_v128(res)));
467     }
468   }
469 }
470 
filter_block_8x8(const int is_lowbd,void * dest,int dstride,const uint16_t * in,int pri_strength,int sec_strength,int dir,int pri_damping,int sec_damping,int coeff_shift,int height,int enable_primary,int enable_secondary)471 CDEF_INLINE void filter_block_8x8(const int is_lowbd, void *dest, int dstride,
472                                   const uint16_t *in, int pri_strength,
473                                   int sec_strength, int dir, int pri_damping,
474                                   int sec_damping, int coeff_shift, int height,
475                                   int enable_primary, int enable_secondary) {
476   uint8_t *dst8 = (uint8_t *)dest;
477   uint16_t *dst16 = (uint16_t *)dest;
478   const int clipping_required = enable_primary && enable_secondary;
479   int i;
480   v256 sum, p0, p1, p2, p3, row, res;
481   const v256 cdef_large_value_mask = v256_dup_16((uint16_t)~CDEF_VERY_LARGE);
482   v256 max, min;
483   const int po1 = cdef_directions[dir][0];
484   const int po2 = cdef_directions[dir][1];
485   const int s1o1 = cdef_directions[dir + 2][0];
486   const int s1o2 = cdef_directions[dir + 2][1];
487   const int s2o1 = cdef_directions[dir - 2][0];
488   const int s2o2 = cdef_directions[dir - 2][1];
489   const int *pri_taps = cdef_pri_taps[(pri_strength >> coeff_shift) & 1];
490   const int *sec_taps = cdef_sec_taps;
491 
492   if (enable_primary && pri_strength)
493     pri_damping = AOMMAX(0, pri_damping - get_msb(pri_strength));
494   if (enable_secondary && sec_strength)
495     sec_damping = AOMMAX(0, sec_damping - get_msb(sec_strength));
496 
497   for (i = 0; i < height; i += 2) {
498     v256 tap[8];
499     sum = v256_zero();
500     row = v256_from_v128(v128_load_aligned(&in[i * CDEF_BSTRIDE]),
501                          v128_load_aligned(&in[(i + 1) * CDEF_BSTRIDE]));
502 
503     min = max = row;
504     if (enable_primary) {
505       // Primary near taps
506       tap[0] = v256_from_v128(
507           v128_load_unaligned(&in[i * CDEF_BSTRIDE + po1]),
508           v128_load_unaligned(&in[(i + 1) * CDEF_BSTRIDE + po1]));
509       tap[1] = v256_from_v128(
510           v128_load_unaligned(&in[i * CDEF_BSTRIDE - po1]),
511           v128_load_unaligned(&in[(i + 1) * CDEF_BSTRIDE - po1]));
512       p0 = constrain16(tap[0], row, pri_strength, pri_damping);
513       p1 = constrain16(tap[1], row, pri_strength, pri_damping);
514 
515       // sum += pri_taps[0] * (p0 + p1)
516       sum = v256_add_16(
517           sum, v256_mullo_s16(v256_dup_16(pri_taps[0]), v256_add_16(p0, p1)));
518 
519       // Primary far taps
520       tap[2] = v256_from_v128(
521           v128_load_unaligned(&in[i * CDEF_BSTRIDE + po2]),
522           v128_load_unaligned(&in[(i + 1) * CDEF_BSTRIDE + po2]));
523       tap[3] = v256_from_v128(
524           v128_load_unaligned(&in[i * CDEF_BSTRIDE - po2]),
525           v128_load_unaligned(&in[(i + 1) * CDEF_BSTRIDE - po2]));
526       p0 = constrain16(tap[2], row, pri_strength, pri_damping);
527       p1 = constrain16(tap[3], row, pri_strength, pri_damping);
528 
529       // sum += pri_taps[1] * (p0 + p1)
530       sum = v256_add_16(
531           sum, v256_mullo_s16(v256_dup_16(pri_taps[1]), v256_add_16(p0, p1)));
532 
533       if (clipping_required) {
534         max = get_max_primary(is_lowbd, tap, max, cdef_large_value_mask);
535 
536         min = v256_min_s16(min, tap[0]);
537         min = v256_min_s16(min, tap[1]);
538         min = v256_min_s16(min, tap[2]);
539         min = v256_min_s16(min, tap[3]);
540       }
541       // End primary
542     }
543 
544     if (enable_secondary) {
545       // Secondary near taps
546       tap[0] = v256_from_v128(
547           v128_load_unaligned(&in[i * CDEF_BSTRIDE + s1o1]),
548           v128_load_unaligned(&in[(i + 1) * CDEF_BSTRIDE + s1o1]));
549       tap[1] = v256_from_v128(
550           v128_load_unaligned(&in[i * CDEF_BSTRIDE - s1o1]),
551           v128_load_unaligned(&in[(i + 1) * CDEF_BSTRIDE - s1o1]));
552       tap[2] = v256_from_v128(
553           v128_load_unaligned(&in[i * CDEF_BSTRIDE + s2o1]),
554           v128_load_unaligned(&in[(i + 1) * CDEF_BSTRIDE + s2o1]));
555       tap[3] = v256_from_v128(
556           v128_load_unaligned(&in[i * CDEF_BSTRIDE - s2o1]),
557           v128_load_unaligned(&in[(i + 1) * CDEF_BSTRIDE - s2o1]));
558       p0 = constrain16(tap[0], row, sec_strength, sec_damping);
559       p1 = constrain16(tap[1], row, sec_strength, sec_damping);
560       p2 = constrain16(tap[2], row, sec_strength, sec_damping);
561       p3 = constrain16(tap[3], row, sec_strength, sec_damping);
562 
563       // sum += sec_taps[0] * (p0 + p1 + p2 + p3)
564       sum = v256_add_16(sum, v256_mullo_s16(v256_dup_16(sec_taps[0]),
565                                             v256_add_16(v256_add_16(p0, p1),
566                                                         v256_add_16(p2, p3))));
567 
568       // Secondary far taps
569       tap[4] = v256_from_v128(
570           v128_load_unaligned(&in[i * CDEF_BSTRIDE + s1o2]),
571           v128_load_unaligned(&in[(i + 1) * CDEF_BSTRIDE + s1o2]));
572       tap[5] = v256_from_v128(
573           v128_load_unaligned(&in[i * CDEF_BSTRIDE - s1o2]),
574           v128_load_unaligned(&in[(i + 1) * CDEF_BSTRIDE - s1o2]));
575       tap[6] = v256_from_v128(
576           v128_load_unaligned(&in[i * CDEF_BSTRIDE + s2o2]),
577           v128_load_unaligned(&in[(i + 1) * CDEF_BSTRIDE + s2o2]));
578       tap[7] = v256_from_v128(
579           v128_load_unaligned(&in[i * CDEF_BSTRIDE - s2o2]),
580           v128_load_unaligned(&in[(i + 1) * CDEF_BSTRIDE - s2o2]));
581       p0 = constrain16(tap[4], row, sec_strength, sec_damping);
582       p1 = constrain16(tap[5], row, sec_strength, sec_damping);
583       p2 = constrain16(tap[6], row, sec_strength, sec_damping);
584       p3 = constrain16(tap[7], row, sec_strength, sec_damping);
585 
586       // sum += sec_taps[1] * (p0 + p1 + p2 + p3)
587       sum = v256_add_16(sum, v256_mullo_s16(v256_dup_16(sec_taps[1]),
588                                             v256_add_16(v256_add_16(p0, p1),
589                                                         v256_add_16(p2, p3))));
590 
591       if (clipping_required) {
592         max = get_max_secondary(is_lowbd, tap, max, cdef_large_value_mask);
593 
594         min = v256_min_s16(min, tap[0]);
595         min = v256_min_s16(min, tap[1]);
596         min = v256_min_s16(min, tap[2]);
597         min = v256_min_s16(min, tap[3]);
598         min = v256_min_s16(min, tap[4]);
599         min = v256_min_s16(min, tap[5]);
600         min = v256_min_s16(min, tap[6]);
601         min = v256_min_s16(min, tap[7]);
602       }
603       // End secondary
604     }
605 
606     // res = row + ((sum - (sum < 0) + 8) >> 4)
607     sum = v256_add_16(sum, v256_cmplt_s16(sum, v256_zero()));
608     res = v256_add_16(sum, v256_dup_16(8));
609     res = v256_shr_n_s16(res, 4);
610     res = v256_add_16(row, res);
611     if (clipping_required) {
612       res = v256_min_s16(v256_max_s16(res, min), max);
613     }
614 
615     if (is_lowbd) {
616       const v128 res_128 = v256_low_v128(v256_pack_s16_u8(res, res));
617       v64_store_aligned(&dst8[i * dstride], v128_high_v64(res_128));
618       v64_store_aligned(&dst8[(i + 1) * dstride], v128_low_v64(res_128));
619     } else {
620       v128_store_unaligned(&dst16[i * dstride], v256_high_v128(res));
621       v128_store_unaligned(&dst16[(i + 1) * dstride], v256_low_v128(res));
622     }
623   }
624 }
625 
626 #if defined(_MSC_VER) && !defined(__clang__)
627 #pragma optimize("", on)
628 #endif
629 
copy_block_4xh(const int is_lowbd,void * dest,int dstride,const uint16_t * in,int height)630 SIMD_INLINE void copy_block_4xh(const int is_lowbd, void *dest, int dstride,
631                                 const uint16_t *in, int height) {
632   uint8_t *dst8 = (uint8_t *)dest;
633   uint16_t *dst16 = (uint16_t *)dest;
634   int i;
635   for (i = 0; i < height; i += 4) {
636     const v128 row0 =
637         v128_from_v64(v64_load_aligned(&in[(i + 0) * CDEF_BSTRIDE]),
638                       v64_load_aligned(&in[(i + 1) * CDEF_BSTRIDE]));
639     const v128 row1 =
640         v128_from_v64(v64_load_aligned(&in[(i + 2) * CDEF_BSTRIDE]),
641                       v64_load_aligned(&in[(i + 3) * CDEF_BSTRIDE]));
642     if (is_lowbd) {
643       /* Note: v128_pack_s16_u8(). The parameter order is swapped internally */
644       const v128 res_128 = v128_pack_s16_u8(row1, row0);
645       u32_store_aligned(&dst8[(i + 0) * dstride],
646                         v64_high_u32(v128_low_v64(res_128)));
647       u32_store_aligned(&dst8[(i + 1) * dstride],
648                         v64_low_u32(v128_low_v64(res_128)));
649       u32_store_aligned(&dst8[(i + 2) * dstride],
650                         v64_high_u32(v128_high_v64(res_128)));
651       u32_store_aligned(&dst8[(i + 3) * dstride],
652                         v64_low_u32(v128_high_v64(res_128)));
653     } else {
654       v64_store_aligned(&dst16[(i + 0) * dstride], v128_high_v64(row0));
655       v64_store_aligned(&dst16[(i + 1) * dstride], v128_low_v64(row0));
656       v64_store_aligned(&dst16[(i + 2) * dstride], v128_high_v64(row1));
657       v64_store_aligned(&dst16[(i + 3) * dstride], v128_low_v64(row1));
658     }
659   }
660 }
661 
copy_block_8xh(const int is_lowbd,void * dest,int dstride,const uint16_t * in,int height)662 SIMD_INLINE void copy_block_8xh(const int is_lowbd, void *dest, int dstride,
663                                 const uint16_t *in, int height) {
664   uint8_t *dst8 = (uint8_t *)dest;
665   uint16_t *dst16 = (uint16_t *)dest;
666   int i;
667   for (i = 0; i < height; i += 2) {
668     const v128 row0 = v128_load_aligned(&in[i * CDEF_BSTRIDE]);
669     const v128 row1 = v128_load_aligned(&in[(i + 1) * CDEF_BSTRIDE]);
670     if (is_lowbd) {
671       /* Note: v128_pack_s16_u8(). The parameter order is swapped internally */
672       const v128 res_128 = v128_pack_s16_u8(row1, row0);
673       v64_store_aligned(&dst8[i * dstride], v128_low_v64(res_128));
674       v64_store_aligned(&dst8[(i + 1) * dstride], v128_high_v64(res_128));
675     } else {
676       v128_store_unaligned(&dst16[i * dstride], row0);
677       v128_store_unaligned(&dst16[(i + 1) * dstride], row1);
678     }
679   }
680 }
681 
SIMD_FUNC(cdef_filter_8_0)682 void SIMD_FUNC(cdef_filter_8_0)(void *dest, int dstride, const uint16_t *in,
683                                 int pri_strength, int sec_strength, int dir,
684                                 int pri_damping, int sec_damping,
685                                 int coeff_shift, int block_width,
686                                 int block_height) {
687   if (block_width == 8) {
688     filter_block_8x8(/*is_lowbd=*/1, dest, dstride, in, pri_strength,
689                      sec_strength, dir, pri_damping, sec_damping, coeff_shift,
690                      block_height, /*enable_primary=*/1,
691                      /*enable_secondary=*/1);
692   } else {
693     filter_block_4x4(/*is_lowbd=*/1, dest, dstride, in, pri_strength,
694                      sec_strength, dir, pri_damping, sec_damping, coeff_shift,
695                      block_height, /*enable_primary=*/1,
696                      /*enable_secondary=*/1);
697   }
698 }
699 
SIMD_FUNC(cdef_filter_8_1)700 void SIMD_FUNC(cdef_filter_8_1)(void *dest, int dstride, const uint16_t *in,
701                                 int pri_strength, int sec_strength, int dir,
702                                 int pri_damping, int sec_damping,
703                                 int coeff_shift, int block_width,
704                                 int block_height) {
705   if (block_width == 8) {
706     filter_block_8x8(/*is_lowbd=*/1, dest, dstride, in, pri_strength,
707                      sec_strength, dir, pri_damping, sec_damping, coeff_shift,
708                      block_height, /*enable_primary=*/1,
709                      /*enable_secondary=*/0);
710   } else {
711     filter_block_4x4(/*is_lowbd=*/1, dest, dstride, in, pri_strength,
712                      sec_strength, dir, pri_damping, sec_damping, coeff_shift,
713                      block_height, /*enable_primary=*/1,
714                      /*enable_secondary=*/0);
715   }
716 }
SIMD_FUNC(cdef_filter_8_2)717 void SIMD_FUNC(cdef_filter_8_2)(void *dest, int dstride, const uint16_t *in,
718                                 int pri_strength, int sec_strength, int dir,
719                                 int pri_damping, int sec_damping,
720                                 int coeff_shift, int block_width,
721                                 int block_height) {
722   if (block_width == 8) {
723     filter_block_8x8(/*is_lowbd=*/1, dest, dstride, in, pri_strength,
724                      sec_strength, dir, pri_damping, sec_damping, coeff_shift,
725                      block_height, /*enable_primary=*/0,
726                      /*enable_secondary=*/1);
727   } else {
728     filter_block_4x4(/*is_lowbd=*/1, dest, dstride, in, pri_strength,
729                      sec_strength, dir, pri_damping, sec_damping, coeff_shift,
730                      block_height, /*enable_primary=*/0,
731                      /*enable_secondary=*/1);
732   }
733 }
734 
SIMD_FUNC(cdef_filter_8_3)735 void SIMD_FUNC(cdef_filter_8_3)(void *dest, int dstride, const uint16_t *in,
736                                 int pri_strength, int sec_strength, int dir,
737                                 int pri_damping, int sec_damping,
738                                 int coeff_shift, int block_width,
739                                 int block_height) {
740   (void)pri_strength;
741   (void)sec_strength;
742   (void)dir;
743   (void)pri_damping;
744   (void)sec_damping;
745   (void)coeff_shift;
746   (void)block_width;
747 
748   if (block_width == 8) {
749     copy_block_8xh(/*is_lowbd=*/1, dest, dstride, in, block_height);
750   } else {
751     copy_block_4xh(/*is_lowbd=*/1, dest, dstride, in, block_height);
752   }
753 }
754 
SIMD_FUNC(cdef_filter_16_0)755 void SIMD_FUNC(cdef_filter_16_0)(void *dest, int dstride, const uint16_t *in,
756                                  int pri_strength, int sec_strength, int dir,
757                                  int pri_damping, int sec_damping,
758                                  int coeff_shift, int block_width,
759                                  int block_height) {
760   if (block_width == 8) {
761     filter_block_8x8(/*is_lowbd=*/0, dest, dstride, in, pri_strength,
762                      sec_strength, dir, pri_damping, sec_damping, coeff_shift,
763                      block_height, /*enable_primary=*/1,
764                      /*enable_secondary=*/1);
765   } else {
766     filter_block_4x4(/*is_lowbd=*/0, dest, dstride, in, pri_strength,
767                      sec_strength, dir, pri_damping, sec_damping, coeff_shift,
768                      block_height, /*enable_primary=*/1,
769                      /*enable_secondary=*/1);
770   }
771 }
772 
SIMD_FUNC(cdef_filter_16_1)773 void SIMD_FUNC(cdef_filter_16_1)(void *dest, int dstride, const uint16_t *in,
774                                  int pri_strength, int sec_strength, int dir,
775                                  int pri_damping, int sec_damping,
776                                  int coeff_shift, int block_width,
777                                  int block_height) {
778   if (block_width == 8) {
779     filter_block_8x8(/*is_lowbd=*/0, dest, dstride, in, pri_strength,
780                      sec_strength, dir, pri_damping, sec_damping, coeff_shift,
781                      block_height, /*enable_primary=*/1,
782                      /*enable_secondary=*/0);
783   } else {
784     filter_block_4x4(/*is_lowbd=*/0, dest, dstride, in, pri_strength,
785                      sec_strength, dir, pri_damping, sec_damping, coeff_shift,
786                      block_height, /*enable_primary=*/1,
787                      /*enable_secondary=*/0);
788   }
789 }
SIMD_FUNC(cdef_filter_16_2)790 void SIMD_FUNC(cdef_filter_16_2)(void *dest, int dstride, const uint16_t *in,
791                                  int pri_strength, int sec_strength, int dir,
792                                  int pri_damping, int sec_damping,
793                                  int coeff_shift, int block_width,
794                                  int block_height) {
795   if (block_width == 8) {
796     filter_block_8x8(/*is_lowbd=*/0, dest, dstride, in, pri_strength,
797                      sec_strength, dir, pri_damping, sec_damping, coeff_shift,
798                      block_height, /*enable_primary=*/0,
799                      /*enable_secondary=*/1);
800   } else {
801     filter_block_4x4(/*is_lowbd=*/0, dest, dstride, in, pri_strength,
802                      sec_strength, dir, pri_damping, sec_damping, coeff_shift,
803                      block_height, /*enable_primary=*/0,
804                      /*enable_secondary=*/1);
805   }
806 }
807 
SIMD_FUNC(cdef_filter_16_3)808 void SIMD_FUNC(cdef_filter_16_3)(void *dest, int dstride, const uint16_t *in,
809                                  int pri_strength, int sec_strength, int dir,
810                                  int pri_damping, int sec_damping,
811                                  int coeff_shift, int block_width,
812                                  int block_height) {
813   (void)pri_strength;
814   (void)sec_strength;
815   (void)dir;
816   (void)pri_damping;
817   (void)sec_damping;
818   (void)coeff_shift;
819   (void)block_width;
820   if (block_width == 8) {
821     copy_block_8xh(/*is_lowbd=*/0, dest, dstride, in, block_height);
822   } else {
823     copy_block_4xh(/*is_lowbd=*/0, dest, dstride, in, block_height);
824   }
825 }
826 
SIMD_FUNC(cdef_copy_rect8_16bit_to_16bit)827 void SIMD_FUNC(cdef_copy_rect8_16bit_to_16bit)(uint16_t *dst, int dstride,
828                                                const uint16_t *src, int sstride,
829                                                int width, int height) {
830   int i, j;
831   for (i = 0; i < height; i++) {
832     for (j = 0; j < (width & ~0x7); j += 8) {
833       v128 row = v128_load_unaligned(&src[i * sstride + j]);
834       v128_store_unaligned(&dst[i * dstride + j], row);
835     }
836     for (; j < width; j++) {
837       dst[i * dstride + j] = src[i * sstride + j];
838     }
839   }
840 }
841 
842 #undef CDEF_INLINE
843 
844 #endif  // AOM_AV1_COMMON_CDEF_BLOCK_SIMD_H_
845