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 <emmintrin.h>
13 : #include <xmmintrin.h>
14 :
15 : #include "./av1_rtcd.h"
16 : #include "aom/aom_integer.h"
17 :
18 0 : static INLINE void read_coeff(const tran_low_t *coeff, intptr_t offset,
19 : __m128i *c0, __m128i *c1) {
20 0 : const tran_low_t *addr = coeff + offset;
21 : #if CONFIG_HIGHBITDEPTH
22 0 : const __m128i x0 = _mm_load_si128((const __m128i *)addr);
23 0 : const __m128i x1 = _mm_load_si128((const __m128i *)addr + 1);
24 0 : const __m128i x2 = _mm_load_si128((const __m128i *)addr + 2);
25 0 : const __m128i x3 = _mm_load_si128((const __m128i *)addr + 3);
26 0 : *c0 = _mm_packs_epi32(x0, x1);
27 0 : *c1 = _mm_packs_epi32(x2, x3);
28 : #else
29 : *c0 = _mm_load_si128((const __m128i *)addr);
30 : *c1 = _mm_load_si128((const __m128i *)addr + 1);
31 : #endif
32 0 : }
33 :
34 0 : static INLINE void write_qcoeff(const __m128i *qc0, const __m128i *qc1,
35 : tran_low_t *qcoeff, intptr_t offset) {
36 0 : tran_low_t *addr = qcoeff + offset;
37 : #if CONFIG_HIGHBITDEPTH
38 0 : const __m128i zero = _mm_setzero_si128();
39 0 : __m128i sign_bits = _mm_cmplt_epi16(*qc0, zero);
40 0 : __m128i y0 = _mm_unpacklo_epi16(*qc0, sign_bits);
41 0 : __m128i y1 = _mm_unpackhi_epi16(*qc0, sign_bits);
42 : _mm_store_si128((__m128i *)addr, y0);
43 0 : _mm_store_si128((__m128i *)addr + 1, y1);
44 :
45 0 : sign_bits = _mm_cmplt_epi16(*qc1, zero);
46 0 : y0 = _mm_unpacklo_epi16(*qc1, sign_bits);
47 0 : y1 = _mm_unpackhi_epi16(*qc1, sign_bits);
48 0 : _mm_store_si128((__m128i *)addr + 2, y0);
49 0 : _mm_store_si128((__m128i *)addr + 3, y1);
50 : #else
51 : _mm_store_si128((__m128i *)addr, *qc0);
52 : _mm_store_si128((__m128i *)addr + 1, *qc1);
53 : #endif
54 0 : }
55 :
56 0 : static INLINE void write_zero(tran_low_t *qcoeff, intptr_t offset) {
57 0 : const __m128i zero = _mm_setzero_si128();
58 0 : tran_low_t *addr = qcoeff + offset;
59 : #if CONFIG_HIGHBITDEPTH
60 : _mm_store_si128((__m128i *)addr, zero);
61 0 : _mm_store_si128((__m128i *)addr + 1, zero);
62 0 : _mm_store_si128((__m128i *)addr + 2, zero);
63 0 : _mm_store_si128((__m128i *)addr + 3, zero);
64 : #else
65 : _mm_store_si128((__m128i *)addr, zero);
66 : _mm_store_si128((__m128i *)addr + 1, zero);
67 : #endif
68 0 : }
69 :
70 0 : void av1_quantize_fp_sse2(const tran_low_t *coeff_ptr, intptr_t n_coeffs,
71 : int skip_block, const int16_t *zbin_ptr,
72 : const int16_t *round_ptr, const int16_t *quant_ptr,
73 : const int16_t *quant_shift_ptr,
74 : tran_low_t *qcoeff_ptr, tran_low_t *dqcoeff_ptr,
75 : const int16_t *dequant_ptr, uint16_t *eob_ptr,
76 : const int16_t *scan_ptr, const int16_t *iscan_ptr) {
77 : __m128i zero;
78 : __m128i thr;
79 : int16_t nzflag;
80 : (void)scan_ptr;
81 : (void)zbin_ptr;
82 : (void)quant_shift_ptr;
83 :
84 0 : coeff_ptr += n_coeffs;
85 0 : iscan_ptr += n_coeffs;
86 0 : qcoeff_ptr += n_coeffs;
87 0 : dqcoeff_ptr += n_coeffs;
88 0 : n_coeffs = -n_coeffs;
89 0 : zero = _mm_setzero_si128();
90 :
91 0 : if (!skip_block) {
92 : __m128i eob;
93 : __m128i round, quant, dequant;
94 : {
95 : __m128i coeff0, coeff1;
96 :
97 : // Setup global values
98 : {
99 0 : round = _mm_load_si128((const __m128i *)round_ptr);
100 0 : quant = _mm_load_si128((const __m128i *)quant_ptr);
101 0 : dequant = _mm_load_si128((const __m128i *)dequant_ptr);
102 : }
103 :
104 : {
105 : __m128i coeff0_sign, coeff1_sign;
106 : __m128i qcoeff0, qcoeff1;
107 : __m128i qtmp0, qtmp1;
108 : // Do DC and first 15 AC
109 0 : read_coeff(coeff_ptr, n_coeffs, &coeff0, &coeff1);
110 :
111 : // Poor man's sign extract
112 0 : coeff0_sign = _mm_srai_epi16(coeff0, 15);
113 0 : coeff1_sign = _mm_srai_epi16(coeff1, 15);
114 0 : qcoeff0 = _mm_xor_si128(coeff0, coeff0_sign);
115 0 : qcoeff1 = _mm_xor_si128(coeff1, coeff1_sign);
116 0 : qcoeff0 = _mm_sub_epi16(qcoeff0, coeff0_sign);
117 0 : qcoeff1 = _mm_sub_epi16(qcoeff1, coeff1_sign);
118 :
119 0 : qcoeff0 = _mm_adds_epi16(qcoeff0, round);
120 0 : round = _mm_unpackhi_epi64(round, round);
121 0 : qcoeff1 = _mm_adds_epi16(qcoeff1, round);
122 0 : qtmp0 = _mm_mulhi_epi16(qcoeff0, quant);
123 0 : quant = _mm_unpackhi_epi64(quant, quant);
124 0 : qtmp1 = _mm_mulhi_epi16(qcoeff1, quant);
125 :
126 : // Reinsert signs
127 0 : qcoeff0 = _mm_xor_si128(qtmp0, coeff0_sign);
128 0 : qcoeff1 = _mm_xor_si128(qtmp1, coeff1_sign);
129 0 : qcoeff0 = _mm_sub_epi16(qcoeff0, coeff0_sign);
130 0 : qcoeff1 = _mm_sub_epi16(qcoeff1, coeff1_sign);
131 :
132 0 : write_qcoeff(&qcoeff0, &qcoeff1, qcoeff_ptr, n_coeffs);
133 :
134 0 : coeff0 = _mm_mullo_epi16(qcoeff0, dequant);
135 0 : dequant = _mm_unpackhi_epi64(dequant, dequant);
136 0 : coeff1 = _mm_mullo_epi16(qcoeff1, dequant);
137 :
138 0 : write_qcoeff(&coeff0, &coeff1, dqcoeff_ptr, n_coeffs);
139 : }
140 :
141 : {
142 : // Scan for eob
143 : __m128i zero_coeff0, zero_coeff1;
144 : __m128i nzero_coeff0, nzero_coeff1;
145 : __m128i iscan0, iscan1;
146 : __m128i eob1;
147 0 : zero_coeff0 = _mm_cmpeq_epi16(coeff0, zero);
148 0 : zero_coeff1 = _mm_cmpeq_epi16(coeff1, zero);
149 0 : nzero_coeff0 = _mm_cmpeq_epi16(zero_coeff0, zero);
150 0 : nzero_coeff1 = _mm_cmpeq_epi16(zero_coeff1, zero);
151 0 : iscan0 = _mm_load_si128((const __m128i *)(iscan_ptr + n_coeffs));
152 0 : iscan1 = _mm_load_si128((const __m128i *)(iscan_ptr + n_coeffs) + 1);
153 : // Add one to convert from indices to counts
154 0 : iscan0 = _mm_sub_epi16(iscan0, nzero_coeff0);
155 0 : iscan1 = _mm_sub_epi16(iscan1, nzero_coeff1);
156 0 : eob = _mm_and_si128(iscan0, nzero_coeff0);
157 0 : eob1 = _mm_and_si128(iscan1, nzero_coeff1);
158 0 : eob = _mm_max_epi16(eob, eob1);
159 : }
160 0 : n_coeffs += 8 * 2;
161 : }
162 :
163 0 : thr = _mm_srai_epi16(dequant, 1);
164 :
165 : // AC only loop
166 0 : while (n_coeffs < 0) {
167 : __m128i coeff0, coeff1;
168 : {
169 : __m128i coeff0_sign, coeff1_sign;
170 : __m128i qcoeff0, qcoeff1;
171 : __m128i qtmp0, qtmp1;
172 :
173 0 : read_coeff(coeff_ptr, n_coeffs, &coeff0, &coeff1);
174 :
175 : // Poor man's sign extract
176 0 : coeff0_sign = _mm_srai_epi16(coeff0, 15);
177 0 : coeff1_sign = _mm_srai_epi16(coeff1, 15);
178 0 : qcoeff0 = _mm_xor_si128(coeff0, coeff0_sign);
179 0 : qcoeff1 = _mm_xor_si128(coeff1, coeff1_sign);
180 0 : qcoeff0 = _mm_sub_epi16(qcoeff0, coeff0_sign);
181 0 : qcoeff1 = _mm_sub_epi16(qcoeff1, coeff1_sign);
182 :
183 0 : nzflag = _mm_movemask_epi8(_mm_cmpgt_epi16(qcoeff0, thr)) |
184 0 : _mm_movemask_epi8(_mm_cmpgt_epi16(qcoeff1, thr));
185 :
186 0 : if (nzflag) {
187 0 : qcoeff0 = _mm_adds_epi16(qcoeff0, round);
188 0 : qcoeff1 = _mm_adds_epi16(qcoeff1, round);
189 0 : qtmp0 = _mm_mulhi_epi16(qcoeff0, quant);
190 0 : qtmp1 = _mm_mulhi_epi16(qcoeff1, quant);
191 :
192 : // Reinsert signs
193 0 : qcoeff0 = _mm_xor_si128(qtmp0, coeff0_sign);
194 0 : qcoeff1 = _mm_xor_si128(qtmp1, coeff1_sign);
195 0 : qcoeff0 = _mm_sub_epi16(qcoeff0, coeff0_sign);
196 0 : qcoeff1 = _mm_sub_epi16(qcoeff1, coeff1_sign);
197 :
198 0 : write_qcoeff(&qcoeff0, &qcoeff1, qcoeff_ptr, n_coeffs);
199 :
200 0 : coeff0 = _mm_mullo_epi16(qcoeff0, dequant);
201 0 : coeff1 = _mm_mullo_epi16(qcoeff1, dequant);
202 :
203 0 : write_qcoeff(&coeff0, &coeff1, dqcoeff_ptr, n_coeffs);
204 : } else {
205 0 : write_zero(qcoeff_ptr, n_coeffs);
206 0 : write_zero(dqcoeff_ptr, n_coeffs);
207 : }
208 : }
209 :
210 0 : if (nzflag) {
211 : // Scan for eob
212 : __m128i zero_coeff0, zero_coeff1;
213 : __m128i nzero_coeff0, nzero_coeff1;
214 : __m128i iscan0, iscan1;
215 : __m128i eob0, eob1;
216 0 : zero_coeff0 = _mm_cmpeq_epi16(coeff0, zero);
217 0 : zero_coeff1 = _mm_cmpeq_epi16(coeff1, zero);
218 0 : nzero_coeff0 = _mm_cmpeq_epi16(zero_coeff0, zero);
219 0 : nzero_coeff1 = _mm_cmpeq_epi16(zero_coeff1, zero);
220 0 : iscan0 = _mm_load_si128((const __m128i *)(iscan_ptr + n_coeffs));
221 0 : iscan1 = _mm_load_si128((const __m128i *)(iscan_ptr + n_coeffs) + 1);
222 : // Add one to convert from indices to counts
223 0 : iscan0 = _mm_sub_epi16(iscan0, nzero_coeff0);
224 0 : iscan1 = _mm_sub_epi16(iscan1, nzero_coeff1);
225 0 : eob0 = _mm_and_si128(iscan0, nzero_coeff0);
226 0 : eob1 = _mm_and_si128(iscan1, nzero_coeff1);
227 0 : eob0 = _mm_max_epi16(eob0, eob1);
228 0 : eob = _mm_max_epi16(eob, eob0);
229 : }
230 0 : n_coeffs += 8 * 2;
231 : }
232 :
233 : // Accumulate EOB
234 : {
235 : __m128i eob_shuffled;
236 0 : eob_shuffled = _mm_shuffle_epi32(eob, 0xe);
237 0 : eob = _mm_max_epi16(eob, eob_shuffled);
238 0 : eob_shuffled = _mm_shufflelo_epi16(eob, 0xe);
239 0 : eob = _mm_max_epi16(eob, eob_shuffled);
240 0 : eob_shuffled = _mm_shufflelo_epi16(eob, 0x1);
241 0 : eob = _mm_max_epi16(eob, eob_shuffled);
242 0 : *eob_ptr = _mm_extract_epi16(eob, 1);
243 : }
244 : } else {
245 : do {
246 0 : write_zero(dqcoeff_ptr, n_coeffs);
247 0 : write_zero(qcoeff_ptr, n_coeffs);
248 0 : n_coeffs += 8 * 2;
249 0 : } while (n_coeffs < 0);
250 0 : *eob_ptr = 0;
251 : }
252 0 : }
|