• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022, 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 <arm_neon.h>
13 
14 #include "aom_dsp/arm/mem_neon.h"
15 
16 #include "av1/common/quant_common.h"
17 #include "av1/encoder/av1_quantize.h"
18 
quantize_4(const tran_low_t * coeff_ptr,tran_low_t * qcoeff_ptr,tran_low_t * dqcoeff_ptr,int32x4_t v_quant_s32,int32x4_t v_dequant_s32,int32x4_t v_round_s32,int log_scale)19 static INLINE uint16x4_t quantize_4(const tran_low_t *coeff_ptr,
20                                     tran_low_t *qcoeff_ptr,
21                                     tran_low_t *dqcoeff_ptr,
22                                     int32x4_t v_quant_s32,
23                                     int32x4_t v_dequant_s32,
24                                     int32x4_t v_round_s32, int log_scale) {
25   const int32x4_t v_coeff = vld1q_s32(coeff_ptr);
26   const int32x4_t v_coeff_sign =
27       vreinterpretq_s32_u32(vcltq_s32(v_coeff, vdupq_n_s32(0)));
28   const int32x4_t v_log_scale = vdupq_n_s32(log_scale);
29   const int32x4_t v_abs_coeff = vabsq_s32(v_coeff);
30   // ((abs_coeff << (1 + log_scale)) >= dequant_ptr[rc01])
31   const int32x4_t v_abs_coeff_scaled =
32       vshlq_s32(v_abs_coeff, vdupq_n_s32(1 + log_scale));
33   const uint32x4_t v_mask = vcgeq_s32(v_abs_coeff_scaled, v_dequant_s32);
34   // const int64_t tmp = vmask ? (int64_t)abs_coeff + log_scaled_round : 0
35   const int32x4_t v_tmp = vandq_s32(vaddq_s32(v_abs_coeff, v_round_s32),
36                                     vreinterpretq_s32_u32(v_mask));
37   //  const int abs_qcoeff = (int)((tmp * quant) >> (16 - log_scale));
38   const int32x4_t v_abs_qcoeff =
39       vqdmulhq_s32(vshlq_s32(v_tmp, v_log_scale), v_quant_s32);
40   //  qcoeff_ptr[rc] = (tran_low_t)((abs_qcoeff ^ coeff_sign) - coeff_sign);
41   const int32x4_t v_qcoeff =
42       vsubq_s32(veorq_s32(v_abs_qcoeff, v_coeff_sign), v_coeff_sign);
43   // vshlq_s32 will shift right if shift value is negative.
44   const int32x4_t v_abs_dqcoeff =
45       vshlq_s32(vmulq_s32(v_abs_qcoeff, v_dequant_s32), vnegq_s32(v_log_scale));
46   //  dqcoeff_ptr[rc] = (tran_low_t)((abs_dqcoeff ^ coeff_sign) - coeff_sign);
47   const int32x4_t v_dqcoeff =
48       vsubq_s32(veorq_s32(v_abs_dqcoeff, v_coeff_sign), v_coeff_sign);
49 
50   vst1q_s32(qcoeff_ptr, v_qcoeff);
51   vst1q_s32(dqcoeff_ptr, v_dqcoeff);
52 
53   // Used to find eob.
54   const uint32x4_t nz_qcoeff_mask = vcgtq_s32(v_abs_qcoeff, vdupq_n_s32(0));
55   return vmovn_u32(nz_qcoeff_mask);
56 }
57 
get_max_lane_eob(const int16_t * iscan,int16x8_t v_eobmax,uint16x8_t v_mask)58 static INLINE int16x8_t get_max_lane_eob(const int16_t *iscan,
59                                          int16x8_t v_eobmax,
60                                          uint16x8_t v_mask) {
61   const int16x8_t v_iscan = vld1q_s16(&iscan[0]);
62   const int16x8_t v_iscan_plus1 = vaddq_s16(v_iscan, vdupq_n_s16(1));
63   const int16x8_t v_nz_iscan = vbslq_s16(v_mask, v_iscan_plus1, vdupq_n_s16(0));
64   return vmaxq_s16(v_eobmax, v_nz_iscan);
65 }
66 
get_max_eob(int16x8_t v_eobmax)67 static INLINE uint16_t get_max_eob(int16x8_t v_eobmax) {
68 #ifdef __aarch64__
69   return (uint16_t)vmaxvq_s16(v_eobmax);
70 #else
71   const int16x4_t v_eobmax_3210 =
72       vmax_s16(vget_low_s16(v_eobmax), vget_high_s16(v_eobmax));
73   const int64x1_t v_eobmax_xx32 =
74       vshr_n_s64(vreinterpret_s64_s16(v_eobmax_3210), 32);
75   const int16x4_t v_eobmax_tmp =
76       vmax_s16(v_eobmax_3210, vreinterpret_s16_s64(v_eobmax_xx32));
77   const int64x1_t v_eobmax_xxx3 =
78       vshr_n_s64(vreinterpret_s64_s16(v_eobmax_tmp), 16);
79   const int16x4_t v_eobmax_final =
80       vmax_s16(v_eobmax_tmp, vreinterpret_s16_s64(v_eobmax_xxx3));
81   return (uint16_t)vget_lane_s16(v_eobmax_final, 0);
82 #endif
83 }
84 
av1_highbd_quantize_fp_neon(const tran_low_t * coeff_ptr,intptr_t count,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)85 void av1_highbd_quantize_fp_neon(
86     const tran_low_t *coeff_ptr, intptr_t count, const int16_t *zbin_ptr,
87     const int16_t *round_ptr, const int16_t *quant_ptr,
88     const int16_t *quant_shift_ptr, tran_low_t *qcoeff_ptr,
89     tran_low_t *dqcoeff_ptr, const int16_t *dequant_ptr, uint16_t *eob_ptr,
90     const int16_t *scan, const int16_t *iscan, int log_scale) {
91   (void)scan;
92   (void)zbin_ptr;
93   (void)quant_shift_ptr;
94 
95   const int16x4_t v_quant = vld1_s16(quant_ptr);
96   const int16x4_t v_dequant = vld1_s16(dequant_ptr);
97   const int16x4_t v_zero = vdup_n_s16(0);
98   const uint16x4_t v_round_select = vcgt_s16(vdup_n_s16(log_scale), v_zero);
99   const int16x4_t v_round_no_scale = vld1_s16(round_ptr);
100   const int16x4_t v_round_log_scale =
101       vqrdmulh_n_s16(v_round_no_scale, (int16_t)(1 << (15 - log_scale)));
102   const int16x4_t v_round =
103       vbsl_s16(v_round_select, v_round_log_scale, v_round_no_scale);
104   int32x4_t v_round_s32 = vaddl_s16(v_round, v_zero);
105   int32x4_t v_quant_s32 = vshlq_n_s32(vaddl_s16(v_quant, v_zero), 15);
106   int32x4_t v_dequant_s32 = vaddl_s16(v_dequant, v_zero);
107   uint16x4_t v_mask_lo, v_mask_hi;
108   int16x8_t v_eobmax = vdupq_n_s16(-1);
109 
110   // DC and first 3 AC
111   v_mask_lo = quantize_4(coeff_ptr, qcoeff_ptr, dqcoeff_ptr, v_quant_s32,
112                          v_dequant_s32, v_round_s32, log_scale);
113 
114   // overwrite the DC constants with AC constants
115   v_round_s32 = vdupq_lane_s32(vget_low_s32(v_round_s32), 1);
116   v_quant_s32 = vdupq_lane_s32(vget_low_s32(v_quant_s32), 1);
117   v_dequant_s32 = vdupq_lane_s32(vget_low_s32(v_dequant_s32), 1);
118 
119   // 4 more AC
120   v_mask_hi = quantize_4(coeff_ptr + 4, qcoeff_ptr + 4, dqcoeff_ptr + 4,
121                          v_quant_s32, v_dequant_s32, v_round_s32, log_scale);
122 
123   // Find the max lane eob for the first 8 coeffs.
124   v_eobmax =
125       get_max_lane_eob(iscan, v_eobmax, vcombine_u16(v_mask_lo, v_mask_hi));
126 
127   count -= 8;
128   do {
129     coeff_ptr += 8;
130     qcoeff_ptr += 8;
131     dqcoeff_ptr += 8;
132     iscan += 8;
133     v_mask_lo = quantize_4(coeff_ptr, qcoeff_ptr, dqcoeff_ptr, v_quant_s32,
134                            v_dequant_s32, v_round_s32, log_scale);
135     v_mask_hi = quantize_4(coeff_ptr + 4, qcoeff_ptr + 4, dqcoeff_ptr + 4,
136                            v_quant_s32, v_dequant_s32, v_round_s32, log_scale);
137     // Find the max lane eob for 8 coeffs.
138     v_eobmax =
139         get_max_lane_eob(iscan, v_eobmax, vcombine_u16(v_mask_lo, v_mask_hi));
140     count -= 8;
141   } while (count);
142 
143   *eob_ptr = get_max_eob(v_eobmax);
144 }
145