1/*
2 *  Copyright (c) 2014 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 <arm_neon.h>
12
13#include <math.h>
14
15#include "vpx_mem/vpx_mem.h"
16
17#include "vp9/common/vp9_quant_common.h"
18#include "vp9/common/vp9_seg_common.h"
19
20#include "vp9/encoder/vp9_encoder.h"
21#include "vp9/encoder/vp9_quantize.h"
22#include "vp9/encoder/vp9_rd.h"
23
24#include "vpx_dsp/arm/idct_neon.h"
25#include "vpx_dsp/arm/mem_neon.h"
26#include "vpx_dsp/vpx_dsp_common.h"
27
28void vp9_quantize_fp_neon(const tran_low_t *coeff_ptr, intptr_t count,
29                          int skip_block, const int16_t *round_ptr,
30                          const int16_t *quant_ptr, tran_low_t *qcoeff_ptr,
31                          tran_low_t *dqcoeff_ptr, const int16_t *dequant_ptr,
32                          uint16_t *eob_ptr, const int16_t *scan,
33                          const int16_t *iscan) {
34  (void)scan;
35
36  if (!skip_block) {
37    // Quantization pass: All coefficients with index >= zero_flag are
38    // skippable. Note: zero_flag can be zero.
39    int i;
40    const int16x8_t v_zero = vdupq_n_s16(0);
41    const int16x8_t v_one = vdupq_n_s16(1);
42    int16x8_t v_eobmax_76543210 = vdupq_n_s16(-1);
43    int16x8_t v_round = vmovq_n_s16(round_ptr[1]);
44    int16x8_t v_quant = vmovq_n_s16(quant_ptr[1]);
45    int16x8_t v_dequant = vmovq_n_s16(dequant_ptr[1]);
46    // adjust for dc
47    v_round = vsetq_lane_s16(round_ptr[0], v_round, 0);
48    v_quant = vsetq_lane_s16(quant_ptr[0], v_quant, 0);
49    v_dequant = vsetq_lane_s16(dequant_ptr[0], v_dequant, 0);
50    // process dc and the first seven ac coeffs
51    {
52      const int16x8_t v_iscan = vld1q_s16(&iscan[0]);
53      const int16x8_t v_coeff = load_tran_low_to_s16q(coeff_ptr);
54      const int16x8_t v_coeff_sign = vshrq_n_s16(v_coeff, 15);
55      const int16x8_t v_tmp = vabaq_s16(v_round, v_coeff, v_zero);
56      const int32x4_t v_tmp_lo =
57          vmull_s16(vget_low_s16(v_tmp), vget_low_s16(v_quant));
58      const int32x4_t v_tmp_hi =
59          vmull_s16(vget_high_s16(v_tmp), vget_high_s16(v_quant));
60      const int16x8_t v_tmp2 =
61          vcombine_s16(vshrn_n_s32(v_tmp_lo, 16), vshrn_n_s32(v_tmp_hi, 16));
62      const uint16x8_t v_nz_mask = vceqq_s16(v_tmp2, v_zero);
63      const int16x8_t v_iscan_plus1 = vaddq_s16(v_iscan, v_one);
64      const int16x8_t v_nz_iscan = vbslq_s16(v_nz_mask, v_zero, v_iscan_plus1);
65      const int16x8_t v_qcoeff_a = veorq_s16(v_tmp2, v_coeff_sign);
66      const int16x8_t v_qcoeff = vsubq_s16(v_qcoeff_a, v_coeff_sign);
67      const int16x8_t v_dqcoeff = vmulq_s16(v_qcoeff, v_dequant);
68      v_eobmax_76543210 = vmaxq_s16(v_eobmax_76543210, v_nz_iscan);
69      store_s16q_to_tran_low(qcoeff_ptr, v_qcoeff);
70      store_s16q_to_tran_low(dqcoeff_ptr, v_dqcoeff);
71      v_round = vmovq_n_s16(round_ptr[1]);
72      v_quant = vmovq_n_s16(quant_ptr[1]);
73      v_dequant = vmovq_n_s16(dequant_ptr[1]);
74    }
75    // now process the rest of the ac coeffs
76    for (i = 8; i < count; i += 8) {
77      const int16x8_t v_iscan = vld1q_s16(&iscan[i]);
78      const int16x8_t v_coeff = load_tran_low_to_s16q(coeff_ptr + i);
79      const int16x8_t v_coeff_sign = vshrq_n_s16(v_coeff, 15);
80      const int16x8_t v_tmp = vabaq_s16(v_round, v_coeff, v_zero);
81      const int32x4_t v_tmp_lo =
82          vmull_s16(vget_low_s16(v_tmp), vget_low_s16(v_quant));
83      const int32x4_t v_tmp_hi =
84          vmull_s16(vget_high_s16(v_tmp), vget_high_s16(v_quant));
85      const int16x8_t v_tmp2 =
86          vcombine_s16(vshrn_n_s32(v_tmp_lo, 16), vshrn_n_s32(v_tmp_hi, 16));
87      const uint16x8_t v_nz_mask = vceqq_s16(v_tmp2, v_zero);
88      const int16x8_t v_iscan_plus1 = vaddq_s16(v_iscan, v_one);
89      const int16x8_t v_nz_iscan = vbslq_s16(v_nz_mask, v_zero, v_iscan_plus1);
90      const int16x8_t v_qcoeff_a = veorq_s16(v_tmp2, v_coeff_sign);
91      const int16x8_t v_qcoeff = vsubq_s16(v_qcoeff_a, v_coeff_sign);
92      const int16x8_t v_dqcoeff = vmulq_s16(v_qcoeff, v_dequant);
93      v_eobmax_76543210 = vmaxq_s16(v_eobmax_76543210, v_nz_iscan);
94      store_s16q_to_tran_low(qcoeff_ptr + i, v_qcoeff);
95      store_s16q_to_tran_low(dqcoeff_ptr + i, v_dqcoeff);
96    }
97    {
98      const int16x4_t v_eobmax_3210 = vmax_s16(
99          vget_low_s16(v_eobmax_76543210), vget_high_s16(v_eobmax_76543210));
100      const int64x1_t v_eobmax_xx32 =
101          vshr_n_s64(vreinterpret_s64_s16(v_eobmax_3210), 32);
102      const int16x4_t v_eobmax_tmp =
103          vmax_s16(v_eobmax_3210, vreinterpret_s16_s64(v_eobmax_xx32));
104      const int64x1_t v_eobmax_xxx3 =
105          vshr_n_s64(vreinterpret_s64_s16(v_eobmax_tmp), 16);
106      const int16x4_t v_eobmax_final =
107          vmax_s16(v_eobmax_tmp, vreinterpret_s16_s64(v_eobmax_xxx3));
108
109      *eob_ptr = (uint16_t)vget_lane_s16(v_eobmax_final, 0);
110    }
111  } else {
112    memset(qcoeff_ptr, 0, count * sizeof(*qcoeff_ptr));
113    memset(dqcoeff_ptr, 0, count * sizeof(*dqcoeff_ptr));
114    *eob_ptr = 0;
115  }
116}
117