xref: /aosp_15_r20/external/libvpx/vpx_dsp/x86/quantize_sse2.c (revision fb1b10ab9aebc7c7068eedab379b749d7e3900be)
1 /*
2  *  Copyright (c) 2015 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 <assert.h>
12 #include <emmintrin.h>
13 #include <xmmintrin.h>
14 
15 #include "./vpx_dsp_rtcd.h"
16 #include "vpx/vpx_integer.h"
17 #include "vpx_dsp/x86/bitdepth_conversion_sse2.h"
18 #include "vpx_dsp/x86/quantize_sse2.h"
19 #include "vp9/common/vp9_scan.h"
20 
vpx_quantize_b_sse2(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)21 void vpx_quantize_b_sse2(const tran_low_t *coeff_ptr, intptr_t n_coeffs,
22                          const struct macroblock_plane *const mb_plane,
23                          tran_low_t *qcoeff_ptr, tran_low_t *dqcoeff_ptr,
24                          const int16_t *dequant_ptr, uint16_t *eob_ptr,
25                          const struct ScanOrder *const scan_order) {
26   const __m128i zero = _mm_setzero_si128();
27   int index = 16;
28   const int16_t *iscan = scan_order->iscan;
29 
30   __m128i zbin, round, quant, dequant, shift;
31   __m128i coeff0, coeff1, coeff0_sign, coeff1_sign;
32   __m128i qcoeff0, qcoeff1;
33   __m128i cmp_mask0, cmp_mask1;
34   __m128i eob, eob0;
35 
36   // Setup global values.
37   load_b_values(mb_plane, &zbin, &round, &quant, dequant_ptr, &dequant, &shift);
38 
39   // Do DC and first 15 AC.
40   coeff0 = load_tran_low(coeff_ptr);
41   coeff1 = load_tran_low(coeff_ptr + 8);
42 
43   // Poor man's abs().
44   coeff0_sign = _mm_srai_epi16(coeff0, 15);
45   coeff1_sign = _mm_srai_epi16(coeff1, 15);
46   qcoeff0 = invert_sign_sse2(coeff0, coeff0_sign);
47   qcoeff1 = invert_sign_sse2(coeff1, coeff1_sign);
48 
49   cmp_mask0 = _mm_cmpgt_epi16(qcoeff0, zbin);
50   zbin = _mm_unpackhi_epi64(zbin, zbin);  // Switch DC to AC
51   cmp_mask1 = _mm_cmpgt_epi16(qcoeff1, zbin);
52 
53   calculate_qcoeff(&qcoeff0, round, quant, shift);
54 
55   round = _mm_unpackhi_epi64(round, round);
56   quant = _mm_unpackhi_epi64(quant, quant);
57   shift = _mm_unpackhi_epi64(shift, shift);
58 
59   calculate_qcoeff(&qcoeff1, round, quant, shift);
60 
61   // Reinsert signs
62   qcoeff0 = invert_sign_sse2(qcoeff0, coeff0_sign);
63   qcoeff1 = invert_sign_sse2(qcoeff1, coeff1_sign);
64 
65   // Mask out zbin threshold coeffs
66   qcoeff0 = _mm_and_si128(qcoeff0, cmp_mask0);
67   qcoeff1 = _mm_and_si128(qcoeff1, cmp_mask1);
68 
69   store_tran_low(qcoeff0, qcoeff_ptr);
70   store_tran_low(qcoeff1, qcoeff_ptr + 8);
71 
72   calculate_dqcoeff_and_store(qcoeff0, dequant, dqcoeff_ptr);
73   dequant = _mm_unpackhi_epi64(dequant, dequant);
74   calculate_dqcoeff_and_store(qcoeff1, dequant, dqcoeff_ptr + 8);
75 
76   eob = scan_for_eob(&qcoeff0, &qcoeff1, iscan, 0, zero);
77 
78   // AC only loop.
79   while (index < n_coeffs) {
80     coeff0 = load_tran_low(coeff_ptr + index);
81     coeff1 = load_tran_low(coeff_ptr + index + 8);
82 
83     coeff0_sign = _mm_srai_epi16(coeff0, 15);
84     coeff1_sign = _mm_srai_epi16(coeff1, 15);
85     qcoeff0 = invert_sign_sse2(coeff0, coeff0_sign);
86     qcoeff1 = invert_sign_sse2(coeff1, coeff1_sign);
87 
88     cmp_mask0 = _mm_cmpgt_epi16(qcoeff0, zbin);
89     cmp_mask1 = _mm_cmpgt_epi16(qcoeff1, zbin);
90 
91     calculate_qcoeff(&qcoeff0, round, quant, shift);
92     calculate_qcoeff(&qcoeff1, round, quant, shift);
93 
94     qcoeff0 = invert_sign_sse2(qcoeff0, coeff0_sign);
95     qcoeff1 = invert_sign_sse2(qcoeff1, coeff1_sign);
96 
97     qcoeff0 = _mm_and_si128(qcoeff0, cmp_mask0);
98     qcoeff1 = _mm_and_si128(qcoeff1, cmp_mask1);
99 
100     store_tran_low(qcoeff0, qcoeff_ptr + index);
101     store_tran_low(qcoeff1, qcoeff_ptr + index + 8);
102 
103     calculate_dqcoeff_and_store(qcoeff0, dequant, dqcoeff_ptr + index);
104     calculate_dqcoeff_and_store(qcoeff1, dequant, dqcoeff_ptr + index + 8);
105 
106     eob0 = scan_for_eob(&qcoeff0, &qcoeff1, iscan, index, zero);
107     eob = _mm_max_epi16(eob, eob0);
108 
109     index += 16;
110   }
111 
112   *eob_ptr = accumulate_eob(eob);
113 }
114