1 /*
2 * Copyright (c) 2022 The WebM project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11 #include <immintrin.h>
12
13 #include "./vpx_dsp_rtcd.h"
14 #include "vp9/common/vp9_scan.h"
15 #include "vp9/encoder/vp9_block.h"
16
init_one_qp(const __m128i * p,__m256i * qp)17 static VPX_FORCE_INLINE void init_one_qp(const __m128i *p, __m256i *qp) {
18 const __m128i sign = _mm_srai_epi16(*p, 15);
19 const __m128i dc = _mm_unpacklo_epi16(*p, sign);
20 const __m128i ac = _mm_unpackhi_epi16(*p, sign);
21 *qp = _mm256_insertf128_si256(_mm256_castsi128_si256(dc), ac, 1);
22 }
23
update_qp(__m256i * qp)24 static VPX_FORCE_INLINE void update_qp(__m256i *qp) {
25 int i;
26 for (i = 0; i < 5; ++i) {
27 qp[i] = _mm256_permute2x128_si256(qp[i], qp[i], 0x11);
28 }
29 }
30
init_qp(const struct macroblock_plane * const mb_plane,const int16_t * dequant_ptr,__m256i * qp,int log_scale)31 static VPX_FORCE_INLINE void init_qp(
32 const struct macroblock_plane *const mb_plane, const int16_t *dequant_ptr,
33 __m256i *qp, int log_scale) {
34 const __m128i zbin = _mm_loadu_si128((const __m128i *)mb_plane->zbin);
35 const __m128i round = _mm_loadu_si128((const __m128i *)mb_plane->round);
36 const __m128i quant = _mm_loadu_si128((const __m128i *)mb_plane->quant);
37 const __m128i dequant = _mm_loadu_si128((const __m128i *)dequant_ptr);
38 const __m128i quant_shift =
39 _mm_loadu_si128((const __m128i *)mb_plane->quant_shift);
40 init_one_qp(&zbin, &qp[0]);
41 init_one_qp(&round, &qp[1]);
42 init_one_qp(&quant, &qp[2]);
43 init_one_qp(&dequant, &qp[3]);
44 init_one_qp(&quant_shift, &qp[4]);
45 if (log_scale > 0) {
46 const __m256i rnd = _mm256_set1_epi32((int16_t)(1 << (log_scale - 1)));
47 qp[0] = _mm256_add_epi32(qp[0], rnd);
48 qp[0] = _mm256_srai_epi32(qp[0], log_scale);
49
50 qp[1] = _mm256_add_epi32(qp[1], rnd);
51 qp[1] = _mm256_srai_epi32(qp[1], log_scale);
52 }
53 // Subtracting 1 here eliminates a _mm256_cmpeq_epi32() instruction when
54 // calculating the zbin mask.
55 qp[0] = _mm256_sub_epi32(qp[0], _mm256_set1_epi32(1));
56 }
57
58 // Note:
59 // *x is vector multiplied by *y which is 16 int32_t parallel multiplication
60 // and right shift 16. The output, 16 int32_t is save in *p.
mm256_mul_shift_epi32(const __m256i * x,const __m256i * y)61 static VPX_FORCE_INLINE __m256i mm256_mul_shift_epi32(const __m256i *x,
62 const __m256i *y) {
63 __m256i prod_lo = _mm256_mul_epi32(*x, *y);
64 __m256i prod_hi = _mm256_srli_epi64(*x, 32);
65 const __m256i mult_hi = _mm256_srli_epi64(*y, 32);
66 const __m256i mask = _mm256_set_epi32(0, -1, 0, -1, 0, -1, 0, -1);
67 prod_hi = _mm256_mul_epi32(prod_hi, mult_hi);
68 prod_lo = _mm256_srli_epi64(prod_lo, 16);
69 prod_lo = _mm256_and_si256(prod_lo, mask);
70 prod_hi = _mm256_srli_epi64(prod_hi, 16);
71 prod_hi = _mm256_slli_epi64(prod_hi, 32);
72 return _mm256_or_si256(prod_lo, prod_hi);
73 }
74
get_max_lane_eob(const int16_t * iscan_ptr,__m256i eobmax,__m256i nz_mask)75 static VPX_FORCE_INLINE __m256i get_max_lane_eob(const int16_t *iscan_ptr,
76 __m256i eobmax,
77 __m256i nz_mask) {
78 const __m256i packed_nz_mask = _mm256_packs_epi32(nz_mask, nz_mask);
79 const __m256i packed_nz_mask_perm =
80 _mm256_permute4x64_epi64(packed_nz_mask, 0xD8);
81 const __m256i iscan =
82 _mm256_castsi128_si256(_mm_loadu_si128((const __m128i *)iscan_ptr));
83 const __m256i nz_iscan = _mm256_and_si256(iscan, packed_nz_mask_perm);
84 return _mm256_max_epi16(eobmax, nz_iscan);
85 }
86
87 // Get the max eob from the lower 128 bits.
get_max_eob(__m256i eob)88 static VPX_FORCE_INLINE uint16_t get_max_eob(__m256i eob) {
89 __m256i eob_s;
90 eob_s = _mm256_shuffle_epi32(eob, 0xe);
91 eob = _mm256_max_epi16(eob, eob_s);
92 eob_s = _mm256_shufflelo_epi16(eob, 0xe);
93 eob = _mm256_max_epi16(eob, eob_s);
94 eob_s = _mm256_shufflelo_epi16(eob, 1);
95 eob = _mm256_max_epi16(eob, eob_s);
96 #if defined(_MSC_VER) && (_MSC_VER < 1910)
97 return _mm_cvtsi128_si32(_mm256_extracti128_si256(eob, 0)) & 0xffff;
98 #else
99 return (uint16_t)_mm256_extract_epi16(eob, 0);
100 #endif
101 }
102
quantize(const __m256i * qp,const tran_low_t * coeff_ptr,const int16_t * iscan_ptr,tran_low_t * qcoeff,tran_low_t * dqcoeff,__m256i * eob)103 static VPX_FORCE_INLINE void quantize(const __m256i *qp,
104 const tran_low_t *coeff_ptr,
105 const int16_t *iscan_ptr,
106 tran_low_t *qcoeff, tran_low_t *dqcoeff,
107 __m256i *eob) {
108 const __m256i coeff = _mm256_loadu_si256((const __m256i *)coeff_ptr);
109 const __m256i abs_coeff = _mm256_abs_epi32(coeff);
110 const __m256i zbin_mask = _mm256_cmpgt_epi32(abs_coeff, qp[0]);
111
112 if (_mm256_movemask_epi8(zbin_mask) == 0) {
113 const __m256i zero = _mm256_setzero_si256();
114 _mm256_storeu_si256((__m256i *)qcoeff, zero);
115 _mm256_storeu_si256((__m256i *)dqcoeff, zero);
116 return;
117 }
118 {
119 const __m256i tmp_rnd =
120 _mm256_and_si256(_mm256_add_epi32(abs_coeff, qp[1]), zbin_mask);
121 const __m256i tmp = mm256_mul_shift_epi32(&tmp_rnd, &qp[2]);
122 const __m256i tmp2 = _mm256_add_epi32(tmp, tmp_rnd);
123 const __m256i abs_q = mm256_mul_shift_epi32(&tmp2, &qp[4]);
124 const __m256i abs_dq = _mm256_mullo_epi32(abs_q, qp[3]);
125 const __m256i nz_mask = _mm256_cmpgt_epi32(abs_q, _mm256_setzero_si256());
126 const __m256i q = _mm256_sign_epi32(abs_q, coeff);
127 const __m256i dq = _mm256_sign_epi32(abs_dq, coeff);
128
129 _mm256_storeu_si256((__m256i *)qcoeff, q);
130 _mm256_storeu_si256((__m256i *)dqcoeff, dq);
131
132 *eob = get_max_lane_eob(iscan_ptr, *eob, nz_mask);
133 }
134 }
135
vpx_highbd_quantize_b_avx2(const tran_low_t * coeff_ptr,intptr_t n_coeffs,const struct macroblock_plane * const mb_plane,tran_low_t * qcoeff_ptr,tran_low_t * dqcoeff_ptr,const int16_t * dequant_ptr,uint16_t * eob_ptr,const struct ScanOrder * const scan_order)136 void vpx_highbd_quantize_b_avx2(const tran_low_t *coeff_ptr, intptr_t n_coeffs,
137 const struct macroblock_plane *const mb_plane,
138 tran_low_t *qcoeff_ptr, tran_low_t *dqcoeff_ptr,
139 const int16_t *dequant_ptr, uint16_t *eob_ptr,
140 const struct ScanOrder *const scan_order) {
141 const int step = 8;
142 __m256i eob = _mm256_setzero_si256();
143 __m256i qp[5];
144 const int16_t *iscan = scan_order->iscan;
145
146 init_qp(mb_plane, dequant_ptr, qp, 0);
147
148 quantize(qp, coeff_ptr, iscan, qcoeff_ptr, dqcoeff_ptr, &eob);
149
150 coeff_ptr += step;
151 qcoeff_ptr += step;
152 dqcoeff_ptr += step;
153 iscan += step;
154 n_coeffs -= step;
155
156 update_qp(qp);
157
158 while (n_coeffs > 0) {
159 quantize(qp, coeff_ptr, iscan, qcoeff_ptr, dqcoeff_ptr, &eob);
160
161 coeff_ptr += step;
162 qcoeff_ptr += step;
163 dqcoeff_ptr += step;
164 iscan += step;
165 n_coeffs -= step;
166 }
167
168 *eob_ptr = get_max_eob(eob);
169 }
170
mm256_mul_shift_epi32_logscale(const __m256i * x,const __m256i * y,int log_scale)171 static VPX_FORCE_INLINE __m256i mm256_mul_shift_epi32_logscale(const __m256i *x,
172 const __m256i *y,
173 int log_scale) {
174 __m256i prod_lo = _mm256_mul_epi32(*x, *y);
175 __m256i prod_hi = _mm256_srli_epi64(*x, 32);
176 const __m256i mult_hi = _mm256_srli_epi64(*y, 32);
177 const __m256i mask = _mm256_set_epi32(0, -1, 0, -1, 0, -1, 0, -1);
178 prod_hi = _mm256_mul_epi32(prod_hi, mult_hi);
179 prod_lo = _mm256_srli_epi64(prod_lo, 16 - log_scale);
180 prod_lo = _mm256_and_si256(prod_lo, mask);
181 prod_hi = _mm256_srli_epi64(prod_hi, 16 - log_scale);
182 prod_hi = _mm256_slli_epi64(prod_hi, 32);
183 return _mm256_or_si256(prod_lo, prod_hi);
184 }
185
quantize_b_32x32(const __m256i * qp,const tran_low_t * coeff_ptr,const int16_t * iscan_ptr,tran_low_t * qcoeff,tran_low_t * dqcoeff,__m256i * eob)186 static VPX_FORCE_INLINE void quantize_b_32x32(
187 const __m256i *qp, const tran_low_t *coeff_ptr, const int16_t *iscan_ptr,
188 tran_low_t *qcoeff, tran_low_t *dqcoeff, __m256i *eob) {
189 const __m256i coeff = _mm256_loadu_si256((const __m256i *)coeff_ptr);
190 const __m256i abs_coeff = _mm256_abs_epi32(coeff);
191 const __m256i zbin_mask = _mm256_cmpgt_epi32(abs_coeff, qp[0]);
192
193 if (_mm256_movemask_epi8(zbin_mask) == 0) {
194 const __m256i zero = _mm256_setzero_si256();
195 _mm256_storeu_si256((__m256i *)qcoeff, zero);
196 _mm256_storeu_si256((__m256i *)dqcoeff, zero);
197 return;
198 }
199
200 {
201 const __m256i tmp_rnd =
202 _mm256_and_si256(_mm256_add_epi32(abs_coeff, qp[1]), zbin_mask);
203 // const int64_t tmp2 = ((tmpw * quant_ptr[rc != 0]) >> 16) + tmpw;
204 const __m256i tmp = mm256_mul_shift_epi32_logscale(&tmp_rnd, &qp[2], 0);
205 const __m256i tmp2 = _mm256_add_epi32(tmp, tmp_rnd);
206 // const int abs_qcoeff = (int)((tmp2 * quant_shift_ptr[rc != 0]) >> 15);
207 const __m256i abs_q = mm256_mul_shift_epi32_logscale(&tmp2, &qp[4], 1);
208 const __m256i abs_dq =
209 _mm256_srli_epi32(_mm256_mullo_epi32(abs_q, qp[3]), 1);
210 const __m256i nz_mask = _mm256_cmpgt_epi32(abs_q, _mm256_setzero_si256());
211 const __m256i q = _mm256_sign_epi32(abs_q, coeff);
212 const __m256i dq = _mm256_sign_epi32(abs_dq, coeff);
213
214 _mm256_storeu_si256((__m256i *)qcoeff, q);
215 _mm256_storeu_si256((__m256i *)dqcoeff, dq);
216
217 *eob = get_max_lane_eob(iscan_ptr, *eob, nz_mask);
218 }
219 }
220
vpx_highbd_quantize_b_32x32_avx2(const tran_low_t * coeff_ptr,const struct macroblock_plane * const mb_plane,tran_low_t * qcoeff_ptr,tran_low_t * dqcoeff_ptr,const int16_t * dequant_ptr,uint16_t * eob_ptr,const struct ScanOrder * const scan_order)221 void vpx_highbd_quantize_b_32x32_avx2(
222 const tran_low_t *coeff_ptr, const struct macroblock_plane *const mb_plane,
223 tran_low_t *qcoeff_ptr, tran_low_t *dqcoeff_ptr, const int16_t *dequant_ptr,
224 uint16_t *eob_ptr, const struct ScanOrder *const scan_order) {
225 const unsigned int step = 8;
226 intptr_t n_coeffs = 32 * 32;
227 const int16_t *iscan = scan_order->iscan;
228 __m256i eob = _mm256_setzero_si256();
229 __m256i qp[5];
230
231 init_qp(mb_plane, dequant_ptr, qp, 1);
232
233 quantize_b_32x32(qp, coeff_ptr, iscan, qcoeff_ptr, dqcoeff_ptr, &eob);
234
235 coeff_ptr += step;
236 qcoeff_ptr += step;
237 dqcoeff_ptr += step;
238 iscan += step;
239 n_coeffs -= step;
240
241 update_qp(qp);
242
243 while (n_coeffs > 0) {
244 quantize_b_32x32(qp, coeff_ptr, iscan, qcoeff_ptr, dqcoeff_ptr, &eob);
245
246 coeff_ptr += step;
247 qcoeff_ptr += step;
248 dqcoeff_ptr += step;
249 iscan += step;
250 n_coeffs -= step;
251 }
252
253 *eob_ptr = get_max_eob(eob);
254 }
255