xref: /aosp_15_r20/external/libaom/av1/encoder/x86/av1_highbd_quantize_avx2.c (revision 77c1e3ccc04c968bd2bc212e87364f250e820521)
1 /*
2  * Copyright (c) 2017, 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 
14 #include "config/av1_rtcd.h"
15 
16 #include "aom/aom_integer.h"
17 #include "aom_dsp/aom_dsp_common.h"
18 
init_one_qp(const __m128i * p,__m256i * qp)19 static inline void init_one_qp(const __m128i *p, __m256i *qp) {
20   const __m128i zero = _mm_setzero_si128();
21   const __m128i dc = _mm_unpacklo_epi16(*p, zero);
22   const __m128i ac = _mm_unpackhi_epi16(*p, zero);
23   *qp = _mm256_insertf128_si256(_mm256_castsi128_si256(dc), ac, 1);
24 }
25 
update_qp(__m256i * qp)26 static inline void update_qp(__m256i *qp) {
27   qp[0] = _mm256_permute2x128_si256(qp[0], qp[0], 0x11);
28   qp[1] = _mm256_permute2x128_si256(qp[1], qp[1], 0x11);
29   qp[2] = _mm256_permute2x128_si256(qp[2], qp[2], 0x11);
30 }
31 
init_qp(const int16_t * round_ptr,const int16_t * quant_ptr,const int16_t * dequant_ptr,int log_scale,__m256i * qp)32 static inline void init_qp(const int16_t *round_ptr, const int16_t *quant_ptr,
33                            const int16_t *dequant_ptr, int log_scale,
34                            __m256i *qp) {
35   __m128i round = _mm_loadu_si128((const __m128i *)round_ptr);
36   if (log_scale) {
37     const __m128i round_scale = _mm_set1_epi16(1 << (15 - log_scale));
38     round = _mm_mulhrs_epi16(round, round_scale);
39   }
40   const __m128i quant = _mm_loadu_si128((const __m128i *)quant_ptr);
41   const __m128i dequant = _mm_loadu_si128((const __m128i *)dequant_ptr);
42 
43   init_one_qp(&round, &qp[0]);
44   init_one_qp(&quant, &qp[1]);
45   init_one_qp(&dequant, &qp[2]);
46 }
47 
quantize(const __m256i * qp,__m256i * c,const int16_t * iscan_ptr,int log_scale,tran_low_t * qcoeff,tran_low_t * dqcoeff,__m256i * eob)48 static inline void quantize(const __m256i *qp, __m256i *c,
49                             const int16_t *iscan_ptr, int log_scale,
50                             tran_low_t *qcoeff, tran_low_t *dqcoeff,
51                             __m256i *eob) {
52   const __m256i abs_coeff = _mm256_abs_epi32(*c);
53   __m256i q = _mm256_add_epi32(abs_coeff, qp[0]);
54 
55   __m256i q_lo = _mm256_mul_epi32(q, qp[1]);
56   __m256i q_hi = _mm256_srli_epi64(q, 32);
57   const __m256i qp_hi = _mm256_srli_epi64(qp[1], 32);
58   q_hi = _mm256_mul_epi32(q_hi, qp_hi);
59   q_lo = _mm256_srli_epi64(q_lo, 16 - log_scale);
60   q_hi = _mm256_srli_epi64(q_hi, 16 - log_scale);
61   q_hi = _mm256_slli_epi64(q_hi, 32);
62   q = _mm256_or_si256(q_lo, q_hi);
63   const __m256i abs_s = _mm256_slli_epi32(abs_coeff, 1 + log_scale);
64   const __m256i mask = _mm256_cmpgt_epi32(qp[2], abs_s);
65   q = _mm256_andnot_si256(mask, q);
66 
67   __m256i dq = _mm256_mullo_epi32(q, qp[2]);
68   dq = _mm256_srai_epi32(dq, log_scale);
69   q = _mm256_sign_epi32(q, *c);
70   dq = _mm256_sign_epi32(dq, *c);
71 
72   _mm256_storeu_si256((__m256i *)qcoeff, q);
73   _mm256_storeu_si256((__m256i *)dqcoeff, dq);
74 
75   const __m128i isc = _mm_loadu_si128((const __m128i *)iscan_ptr);
76   const __m128i zr = _mm_setzero_si128();
77   const __m128i lo = _mm_unpacklo_epi16(isc, zr);
78   const __m128i hi = _mm_unpackhi_epi16(isc, zr);
79   const __m256i iscan =
80       _mm256_insertf128_si256(_mm256_castsi128_si256(lo), hi, 1);
81 
82   const __m256i zero = _mm256_setzero_si256();
83   const __m256i zc = _mm256_cmpeq_epi32(dq, zero);
84   const __m256i nz = _mm256_cmpeq_epi32(zc, zero);
85   __m256i cur_eob = _mm256_sub_epi32(iscan, nz);
86   cur_eob = _mm256_and_si256(cur_eob, nz);
87   *eob = _mm256_max_epi32(cur_eob, *eob);
88 }
89 
av1_highbd_quantize_fp_avx2(const tran_low_t * coeff_ptr,intptr_t n_coeffs,const int16_t * zbin_ptr,const int16_t * round_ptr,const int16_t * quant_ptr,const int16_t * quant_shift_ptr,tran_low_t * qcoeff_ptr,tran_low_t * dqcoeff_ptr,const int16_t * dequant_ptr,uint16_t * eob_ptr,const int16_t * scan,const int16_t * iscan,int log_scale)90 void av1_highbd_quantize_fp_avx2(
91     const tran_low_t *coeff_ptr, intptr_t n_coeffs, const int16_t *zbin_ptr,
92     const int16_t *round_ptr, const int16_t *quant_ptr,
93     const int16_t *quant_shift_ptr, tran_low_t *qcoeff_ptr,
94     tran_low_t *dqcoeff_ptr, const int16_t *dequant_ptr, uint16_t *eob_ptr,
95     const int16_t *scan, const int16_t *iscan, int log_scale) {
96   (void)scan;
97   (void)zbin_ptr;
98   (void)quant_shift_ptr;
99   const unsigned int step = 8;
100   __m256i qp[3], coeff;
101 
102   init_qp(round_ptr, quant_ptr, dequant_ptr, log_scale, qp);
103   coeff = _mm256_loadu_si256((const __m256i *)coeff_ptr);
104 
105   __m256i eob = _mm256_setzero_si256();
106   quantize(qp, &coeff, iscan, log_scale, qcoeff_ptr, dqcoeff_ptr, &eob);
107 
108   coeff_ptr += step;
109   qcoeff_ptr += step;
110   dqcoeff_ptr += step;
111   iscan += step;
112   n_coeffs -= step;
113 
114   update_qp(qp);
115   while (n_coeffs > 0) {
116     coeff = _mm256_loadu_si256((const __m256i *)coeff_ptr);
117     quantize(qp, &coeff, iscan, log_scale, qcoeff_ptr, dqcoeff_ptr, &eob);
118 
119     coeff_ptr += step;
120     qcoeff_ptr += step;
121     dqcoeff_ptr += step;
122     iscan += step;
123     n_coeffs -= step;
124   }
125   {
126     __m256i eob_s;
127     eob_s = _mm256_shuffle_epi32(eob, 0xe);
128     eob = _mm256_max_epi16(eob, eob_s);
129     eob_s = _mm256_shufflelo_epi16(eob, 0xe);
130     eob = _mm256_max_epi16(eob, eob_s);
131     eob_s = _mm256_shufflelo_epi16(eob, 1);
132     eob = _mm256_max_epi16(eob, eob_s);
133     const __m128i final_eob = _mm_max_epi16(_mm256_castsi256_si128(eob),
134                                             _mm256_extractf128_si256(eob, 1));
135     *eob_ptr = _mm_extract_epi16(final_eob, 0);
136   }
137 }
138