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 <immintrin.h>
13 #include <assert.h>
14 
15 #include "config/aom_dsp_rtcd.h"
16 
17 #include "av1/common/convolve.h"
18 #include "aom_dsp/aom_dsp_common.h"
19 #include "aom_dsp/aom_filter.h"
20 #include "aom_dsp/x86/synonyms.h"
21 #include "aom_dsp/x86/synonyms_avx2.h"
22 
23 // 128-bit xmmwords are written as [ ... ] with the MSB on the left.
24 // 256-bit ymmwords are written as two xmmwords, [ ... ][ ... ] with the MSB
25 // on the left.
26 // A row of, say, 16-bit pixels with values p0, p1, p2, ..., p14, p15 will be
27 // loaded and stored as [ p15 ... p9 p8 ][ p7 ... p1 p0 ].
av1_highbd_wiener_convolve_add_src_avx2(const uint8_t * src8,ptrdiff_t src_stride,uint8_t * dst8,ptrdiff_t dst_stride,const int16_t * filter_x,int x_step_q4,const int16_t * filter_y,int y_step_q4,int w,int h,const ConvolveParams * conv_params,int bd)28 void av1_highbd_wiener_convolve_add_src_avx2(
29     const uint8_t *src8, ptrdiff_t src_stride, uint8_t *dst8,
30     ptrdiff_t dst_stride, const int16_t *filter_x, int x_step_q4,
31     const int16_t *filter_y, int y_step_q4, int w, int h,
32     const ConvolveParams *conv_params, int bd) {
33   assert(x_step_q4 == 16 && y_step_q4 == 16);
34   assert(!(w & 7));
35   assert(bd + FILTER_BITS - conv_params->round_0 + 2 <= 16);
36   (void)x_step_q4;
37   (void)y_step_q4;
38 
39   const uint16_t *const src = CONVERT_TO_SHORTPTR(src8);
40   uint16_t *const dst = CONVERT_TO_SHORTPTR(dst8);
41 
42   DECLARE_ALIGNED(32, uint16_t,
43                   temp[(MAX_SB_SIZE + SUBPEL_TAPS - 1) * MAX_SB_SIZE]);
44   int intermediate_height = h + SUBPEL_TAPS - 1;
45   const int center_tap = ((SUBPEL_TAPS - 1) / 2);
46   const uint16_t *const src_ptr = src - center_tap * src_stride - center_tap;
47 
48   const __m128i zero_128 = _mm_setzero_si128();
49   const __m256i zero_256 = _mm256_setzero_si256();
50 
51   // Add an offset to account for the "add_src" part of the convolve function.
52   const __m128i offset = _mm_insert_epi16(zero_128, 1 << FILTER_BITS, 3);
53 
54   const __m256i clamp_low = zero_256;
55 
56   /* Horizontal filter */
57   {
58     const __m256i clamp_high_ep =
59         _mm256_set1_epi16(WIENER_CLAMP_LIMIT(conv_params->round_0, bd) - 1);
60 
61     // coeffs [ f7 f6 f5 f4 f3 f2 f1 f0 ]
62     const __m128i coeffs_x = _mm_add_epi16(xx_loadu_128(filter_x), offset);
63 
64     // coeffs [ f3 f2 f3 f2 f1 f0 f1 f0 ]
65     const __m128i coeffs_0123 = _mm_unpacklo_epi32(coeffs_x, coeffs_x);
66     // coeffs [ f7 f6 f7 f6 f5 f4 f5 f4 ]
67     const __m128i coeffs_4567 = _mm_unpackhi_epi32(coeffs_x, coeffs_x);
68 
69     // coeffs [ f1 f0 f1 f0 f1 f0 f1 f0 ]
70     const __m128i coeffs_01_128 = _mm_unpacklo_epi64(coeffs_0123, coeffs_0123);
71     // coeffs [ f3 f2 f3 f2 f3 f2 f3 f2 ]
72     const __m128i coeffs_23_128 = _mm_unpackhi_epi64(coeffs_0123, coeffs_0123);
73     // coeffs [ f5 f4 f5 f4 f5 f4 f5 f4 ]
74     const __m128i coeffs_45_128 = _mm_unpacklo_epi64(coeffs_4567, coeffs_4567);
75     // coeffs [ f7 f6 f7 f6 f7 f6 f7 f6 ]
76     const __m128i coeffs_67_128 = _mm_unpackhi_epi64(coeffs_4567, coeffs_4567);
77 
78     // coeffs [ f1 f0 f1 f0 f1 f0 f1 f0 ][ f1 f0 f1 f0 f1 f0 f1 f0 ]
79     const __m256i coeffs_01 = yy_set_m128i(coeffs_01_128, coeffs_01_128);
80     // coeffs [ f3 f2 f3 f2 f3 f2 f3 f2 ][ f3 f2 f3 f2 f3 f2 f3 f2 ]
81     const __m256i coeffs_23 = yy_set_m128i(coeffs_23_128, coeffs_23_128);
82     // coeffs [ f5 f4 f5 f4 f5 f4 f5 f4 ][ f5 f4 f5 f4 f5 f4 f5 f4 ]
83     const __m256i coeffs_45 = yy_set_m128i(coeffs_45_128, coeffs_45_128);
84     // coeffs [ f7 f6 f7 f6 f7 f6 f7 f6 ][ f7 f6 f7 f6 f7 f6 f7 f6 ]
85     const __m256i coeffs_67 = yy_set_m128i(coeffs_67_128, coeffs_67_128);
86 
87     const __m256i round_const = _mm256_set1_epi32(
88         (1 << (conv_params->round_0 - 1)) + (1 << (bd + FILTER_BITS - 1)));
89 
90     for (int i = 0; i < intermediate_height; ++i) {
91       for (int j = 0; j < w; j += 16) {
92         const uint16_t *src_ij = src_ptr + i * src_stride + j;
93 
94         // Load 16-bit src data
95         const __m256i src_0 = yy_loadu_256(src_ij + 0);
96         const __m256i src_1 = yy_loadu_256(src_ij + 1);
97         const __m256i src_2 = yy_loadu_256(src_ij + 2);
98         const __m256i src_3 = yy_loadu_256(src_ij + 3);
99         const __m256i src_4 = yy_loadu_256(src_ij + 4);
100         const __m256i src_5 = yy_loadu_256(src_ij + 5);
101         const __m256i src_6 = yy_loadu_256(src_ij + 6);
102         const __m256i src_7 = yy_loadu_256(src_ij + 7);
103 
104         // Multiply src data by filter coeffs and sum pairs
105         const __m256i res_0 = _mm256_madd_epi16(src_0, coeffs_01);
106         const __m256i res_1 = _mm256_madd_epi16(src_1, coeffs_01);
107         const __m256i res_2 = _mm256_madd_epi16(src_2, coeffs_23);
108         const __m256i res_3 = _mm256_madd_epi16(src_3, coeffs_23);
109         const __m256i res_4 = _mm256_madd_epi16(src_4, coeffs_45);
110         const __m256i res_5 = _mm256_madd_epi16(src_5, coeffs_45);
111         const __m256i res_6 = _mm256_madd_epi16(src_6, coeffs_67);
112         const __m256i res_7 = _mm256_madd_epi16(src_7, coeffs_67);
113 
114         // Calculate scalar product for even- and odd-indices separately,
115         // increasing to 32-bit precision
116         const __m256i res_even_sum = _mm256_add_epi32(
117             _mm256_add_epi32(res_0, res_4), _mm256_add_epi32(res_2, res_6));
118         const __m256i res_even = _mm256_srai_epi32(
119             _mm256_add_epi32(res_even_sum, round_const), conv_params->round_0);
120 
121         const __m256i res_odd_sum = _mm256_add_epi32(
122             _mm256_add_epi32(res_1, res_5), _mm256_add_epi32(res_3, res_7));
123         const __m256i res_odd = _mm256_srai_epi32(
124             _mm256_add_epi32(res_odd_sum, round_const), conv_params->round_0);
125 
126         // Reduce to 16-bit precision and pack even- and odd-index results
127         // back into one register. The _mm256_packs_epi32 intrinsic returns
128         // a register with the pixels ordered as follows:
129         // [ 15 13 11 9 14 12 10 8 ] [ 7 5 3 1 6 4 2 0 ]
130         const __m256i res = _mm256_packs_epi32(res_even, res_odd);
131         const __m256i res_clamped =
132             _mm256_min_epi16(_mm256_max_epi16(res, clamp_low), clamp_high_ep);
133 
134         // Store in a temporary array
135         yy_storeu_256(temp + i * MAX_SB_SIZE + j, res_clamped);
136       }
137     }
138   }
139 
140   /* Vertical filter */
141   {
142     const __m256i clamp_high = _mm256_set1_epi16((1 << bd) - 1);
143 
144     // coeffs [ f7 f6 f5 f4 f3 f2 f1 f0 ]
145     const __m128i coeffs_y = _mm_add_epi16(xx_loadu_128(filter_y), offset);
146 
147     // coeffs [ f3 f2 f3 f2 f1 f0 f1 f0 ]
148     const __m128i coeffs_0123 = _mm_unpacklo_epi32(coeffs_y, coeffs_y);
149     // coeffs [ f7 f6 f7 f6 f5 f4 f5 f4 ]
150     const __m128i coeffs_4567 = _mm_unpackhi_epi32(coeffs_y, coeffs_y);
151 
152     // coeffs [ f1 f0 f1 f0 f1 f0 f1 f0 ]
153     const __m128i coeffs_01_128 = _mm_unpacklo_epi64(coeffs_0123, coeffs_0123);
154     // coeffs [ f3 f2 f3 f2 f3 f2 f3 f2 ]
155     const __m128i coeffs_23_128 = _mm_unpackhi_epi64(coeffs_0123, coeffs_0123);
156     // coeffs [ f5 f4 f5 f4 f5 f4 f5 f4 ]
157     const __m128i coeffs_45_128 = _mm_unpacklo_epi64(coeffs_4567, coeffs_4567);
158     // coeffs [ f7 f6 f7 f6 f7 f6 f7 f6 ]
159     const __m128i coeffs_67_128 = _mm_unpackhi_epi64(coeffs_4567, coeffs_4567);
160 
161     // coeffs [ f1 f0 f1 f0 f1 f0 f1 f0 ][ f1 f0 f1 f0 f1 f0 f1 f0 ]
162     const __m256i coeffs_01 = yy_set_m128i(coeffs_01_128, coeffs_01_128);
163     // coeffs [ f3 f2 f3 f2 f3 f2 f3 f2 ][ f3 f2 f3 f2 f3 f2 f3 f2 ]
164     const __m256i coeffs_23 = yy_set_m128i(coeffs_23_128, coeffs_23_128);
165     // coeffs [ f5 f4 f5 f4 f5 f4 f5 f4 ][ f5 f4 f5 f4 f5 f4 f5 f4 ]
166     const __m256i coeffs_45 = yy_set_m128i(coeffs_45_128, coeffs_45_128);
167     // coeffs [ f7 f6 f7 f6 f7 f6 f7 f6 ][ f7 f6 f7 f6 f7 f6 f7 f6 ]
168     const __m256i coeffs_67 = yy_set_m128i(coeffs_67_128, coeffs_67_128);
169 
170     const __m256i round_const =
171         _mm256_set1_epi32((1 << (conv_params->round_1 - 1)) -
172                           (1 << (bd + conv_params->round_1 - 1)));
173 
174     for (int i = 0; i < h; ++i) {
175       for (int j = 0; j < w; j += 16) {
176         const uint16_t *temp_ij = temp + i * MAX_SB_SIZE + j;
177 
178         // Load 16-bit data from the output of the horizontal filter in
179         // which the pixels are ordered as follows:
180         // [ 15 13 11 9 14 12 10 8 ] [ 7 5 3 1 6 4 2 0 ]
181         const __m256i data_0 = yy_loadu_256(temp_ij + 0 * MAX_SB_SIZE);
182         const __m256i data_1 = yy_loadu_256(temp_ij + 1 * MAX_SB_SIZE);
183         const __m256i data_2 = yy_loadu_256(temp_ij + 2 * MAX_SB_SIZE);
184         const __m256i data_3 = yy_loadu_256(temp_ij + 3 * MAX_SB_SIZE);
185         const __m256i data_4 = yy_loadu_256(temp_ij + 4 * MAX_SB_SIZE);
186         const __m256i data_5 = yy_loadu_256(temp_ij + 5 * MAX_SB_SIZE);
187         const __m256i data_6 = yy_loadu_256(temp_ij + 6 * MAX_SB_SIZE);
188         const __m256i data_7 = yy_loadu_256(temp_ij + 7 * MAX_SB_SIZE);
189 
190         // Filter the even-indices, increasing to 32-bit precision
191         const __m256i src_0 = _mm256_unpacklo_epi16(data_0, data_1);
192         const __m256i src_2 = _mm256_unpacklo_epi16(data_2, data_3);
193         const __m256i src_4 = _mm256_unpacklo_epi16(data_4, data_5);
194         const __m256i src_6 = _mm256_unpacklo_epi16(data_6, data_7);
195 
196         const __m256i res_0 = _mm256_madd_epi16(src_0, coeffs_01);
197         const __m256i res_2 = _mm256_madd_epi16(src_2, coeffs_23);
198         const __m256i res_4 = _mm256_madd_epi16(src_4, coeffs_45);
199         const __m256i res_6 = _mm256_madd_epi16(src_6, coeffs_67);
200 
201         const __m256i res_even = _mm256_add_epi32(
202             _mm256_add_epi32(res_0, res_2), _mm256_add_epi32(res_4, res_6));
203 
204         // Filter the odd-indices, increasing to 32-bit precision
205         const __m256i src_1 = _mm256_unpackhi_epi16(data_0, data_1);
206         const __m256i src_3 = _mm256_unpackhi_epi16(data_2, data_3);
207         const __m256i src_5 = _mm256_unpackhi_epi16(data_4, data_5);
208         const __m256i src_7 = _mm256_unpackhi_epi16(data_6, data_7);
209 
210         const __m256i res_1 = _mm256_madd_epi16(src_1, coeffs_01);
211         const __m256i res_3 = _mm256_madd_epi16(src_3, coeffs_23);
212         const __m256i res_5 = _mm256_madd_epi16(src_5, coeffs_45);
213         const __m256i res_7 = _mm256_madd_epi16(src_7, coeffs_67);
214 
215         const __m256i res_odd = _mm256_add_epi32(
216             _mm256_add_epi32(res_1, res_3), _mm256_add_epi32(res_5, res_7));
217 
218         // Pixels are currently in the following order:
219         // res_even order: [ 14 12 10 8 ] [ 6 4 2 0 ]
220         // res_odd order:  [ 15 13 11 9 ] [ 7 5 3 1 ]
221         //
222         // Rearrange the pixels into the following order:
223         // res_lo order: [ 11 10  9  8 ] [ 3 2 1 0 ]
224         // res_hi order: [ 15 14 13 12 ] [ 7 6 5 4 ]
225         const __m256i res_lo = _mm256_unpacklo_epi32(res_even, res_odd);
226         const __m256i res_hi = _mm256_unpackhi_epi32(res_even, res_odd);
227 
228         const __m256i res_lo_round = _mm256_srai_epi32(
229             _mm256_add_epi32(res_lo, round_const), conv_params->round_1);
230         const __m256i res_hi_round = _mm256_srai_epi32(
231             _mm256_add_epi32(res_hi, round_const), conv_params->round_1);
232 
233         // Reduce to 16-bit precision and pack into the correct order:
234         // [ 15 14 13 12 11 10 9 8 ][ 7 6 5 4 3 2 1 0 ]
235         const __m256i res_16bit =
236             _mm256_packs_epi32(res_lo_round, res_hi_round);
237         const __m256i res_16bit_clamped = _mm256_min_epi16(
238             _mm256_max_epi16(res_16bit, clamp_low), clamp_high);
239 
240         // Store in the dst array
241         yy_storeu_256(dst + i * dst_stride + j, res_16bit_clamped);
242       }
243     }
244   }
245 }
246