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 "aom_dsp/quantize.h"
13 #include "aom_mem/aom_mem.h"
14
quantize_b_helper_c(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,const qm_val_t * qm_ptr,const qm_val_t * iqm_ptr,const int log_scale)15 void quantize_b_helper_c(const tran_low_t *coeff_ptr, intptr_t n_coeffs,
16 const int16_t *zbin_ptr, const int16_t *round_ptr,
17 const int16_t *quant_ptr,
18 const int16_t *quant_shift_ptr, tran_low_t *qcoeff_ptr,
19 tran_low_t *dqcoeff_ptr, const int16_t *dequant_ptr,
20 uint16_t *eob_ptr, const int16_t *scan,
21 const int16_t *iscan, const qm_val_t *qm_ptr,
22 const qm_val_t *iqm_ptr, const int log_scale) {
23 const int zbins[2] = { ROUND_POWER_OF_TWO(zbin_ptr[0], log_scale),
24 ROUND_POWER_OF_TWO(zbin_ptr[1], log_scale) };
25 const int nzbins[2] = { zbins[0] * -1, zbins[1] * -1 };
26 int i, non_zero_count = (int)n_coeffs, eob = -1;
27 (void)iscan;
28
29 memset(qcoeff_ptr, 0, n_coeffs * sizeof(*qcoeff_ptr));
30 memset(dqcoeff_ptr, 0, n_coeffs * sizeof(*dqcoeff_ptr));
31
32 // Pre-scan pass
33 for (i = (int)n_coeffs - 1; i >= 0; i--) {
34 const int rc = scan[i];
35 const qm_val_t wt = qm_ptr != NULL ? qm_ptr[rc] : (1 << AOM_QM_BITS);
36 const int coeff = coeff_ptr[rc] * wt;
37
38 if (coeff < (zbins[rc != 0] * (1 << AOM_QM_BITS)) &&
39 coeff > (nzbins[rc != 0] * (1 << AOM_QM_BITS)))
40 non_zero_count--;
41 else
42 break;
43 }
44
45 // Quantization pass: All coefficients with index >= zero_flag are
46 // skippable. Note: zero_flag can be zero.
47 for (i = 0; i < non_zero_count; i++) {
48 const int rc = scan[i];
49 const int coeff = coeff_ptr[rc];
50 const int coeff_sign = (coeff >> 31);
51 const int abs_coeff = (coeff ^ coeff_sign) - coeff_sign;
52 int tmp32;
53
54 const qm_val_t wt = qm_ptr != NULL ? qm_ptr[rc] : (1 << AOM_QM_BITS);
55 if (abs_coeff * wt >= (zbins[rc != 0] << AOM_QM_BITS)) {
56 int64_t tmp =
57 clamp(abs_coeff + ROUND_POWER_OF_TWO(round_ptr[rc != 0], log_scale),
58 INT16_MIN, INT16_MAX);
59 tmp *= wt;
60 tmp32 = (int)(((((tmp * quant_ptr[rc != 0]) >> 16) + tmp) *
61 quant_shift_ptr[rc != 0]) >>
62 (16 - log_scale + AOM_QM_BITS)); // quantization
63 qcoeff_ptr[rc] = (tmp32 ^ coeff_sign) - coeff_sign;
64 const int iwt = iqm_ptr != NULL ? iqm_ptr[rc] : (1 << AOM_QM_BITS);
65 const int dequant =
66 (dequant_ptr[rc != 0] * iwt + (1 << (AOM_QM_BITS - 1))) >>
67 AOM_QM_BITS;
68 const tran_low_t abs_dqcoeff = (tmp32 * dequant) >> log_scale;
69 dqcoeff_ptr[rc] = (tran_low_t)((abs_dqcoeff ^ coeff_sign) - coeff_sign);
70
71 if (tmp32) eob = i;
72 }
73 }
74 *eob_ptr = eob + 1;
75 }
76
highbd_quantize_b_helper_c(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,const qm_val_t * qm_ptr,const qm_val_t * iqm_ptr,const int log_scale)77 void highbd_quantize_b_helper_c(
78 const tran_low_t *coeff_ptr, intptr_t n_coeffs, const int16_t *zbin_ptr,
79 const int16_t *round_ptr, const int16_t *quant_ptr,
80 const int16_t *quant_shift_ptr, tran_low_t *qcoeff_ptr,
81 tran_low_t *dqcoeff_ptr, const int16_t *dequant_ptr, uint16_t *eob_ptr,
82 const int16_t *scan, const int16_t *iscan, const qm_val_t *qm_ptr,
83 const qm_val_t *iqm_ptr, const int log_scale) {
84 int i, eob = -1;
85 const int zbins[2] = { ROUND_POWER_OF_TWO(zbin_ptr[0], log_scale),
86 ROUND_POWER_OF_TWO(zbin_ptr[1], log_scale) };
87 const int nzbins[2] = { zbins[0] * -1, zbins[1] * -1 };
88 int dequant;
89 int idx_arr[4096];
90 (void)iscan;
91 int idx = 0;
92
93 memset(qcoeff_ptr, 0, n_coeffs * sizeof(*qcoeff_ptr));
94 memset(dqcoeff_ptr, 0, n_coeffs * sizeof(*dqcoeff_ptr));
95
96 // Pre-scan pass
97 for (i = 0; i < n_coeffs; i++) {
98 const int rc = scan[i];
99 const qm_val_t wt = qm_ptr != NULL ? qm_ptr[rc] : (1 << AOM_QM_BITS);
100 const int coeff = coeff_ptr[rc] * wt;
101
102 // If the coefficient is out of the base ZBIN range, keep it for
103 // quantization.
104 if (coeff >= (zbins[rc != 0] * (1 << AOM_QM_BITS)) ||
105 coeff <= (nzbins[rc != 0] * (1 << AOM_QM_BITS)))
106 idx_arr[idx++] = i;
107 }
108
109 // Quantization pass: only process the coefficients selected in
110 // pre-scan pass. Note: idx can be zero.
111 for (i = 0; i < idx; i++) {
112 const int rc = scan[idx_arr[i]];
113 const int coeff = coeff_ptr[rc];
114 const int coeff_sign = (coeff >> 31);
115 const qm_val_t wt = qm_ptr != NULL ? qm_ptr[rc] : (1 << AOM_QM_BITS);
116 const qm_val_t iwt = iqm_ptr != NULL ? iqm_ptr[rc] : (1 << AOM_QM_BITS);
117 const int abs_coeff = (coeff ^ coeff_sign) - coeff_sign;
118 const int64_t tmp1 =
119 abs_coeff + ROUND_POWER_OF_TWO(round_ptr[rc != 0], log_scale);
120 const int64_t tmpw = tmp1 * wt;
121 const int64_t tmp2 = ((tmpw * quant_ptr[rc != 0]) >> 16) + tmpw;
122 const int abs_qcoeff = (int)((tmp2 * quant_shift_ptr[rc != 0]) >>
123 (16 - log_scale + AOM_QM_BITS));
124 qcoeff_ptr[rc] = (tran_low_t)((abs_qcoeff ^ coeff_sign) - coeff_sign);
125 dequant =
126 (dequant_ptr[rc != 0] * iwt + (1 << (AOM_QM_BITS - 1))) >> AOM_QM_BITS;
127 const tran_low_t abs_dqcoeff = (abs_qcoeff * dequant) >> log_scale;
128 dqcoeff_ptr[rc] = (tran_low_t)((abs_dqcoeff ^ coeff_sign) - coeff_sign);
129 if (abs_qcoeff) eob = idx_arr[i];
130 }
131 *eob_ptr = eob + 1;
132 }
133
134 /* These functions should only be called when quantisation matrices
135 are not used. */
aom_quantize_b_c(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)136 void aom_quantize_b_c(const tran_low_t *coeff_ptr, intptr_t n_coeffs,
137 const int16_t *zbin_ptr, const int16_t *round_ptr,
138 const int16_t *quant_ptr, const int16_t *quant_shift_ptr,
139 tran_low_t *qcoeff_ptr, tran_low_t *dqcoeff_ptr,
140 const int16_t *dequant_ptr, uint16_t *eob_ptr,
141 const int16_t *scan, const int16_t *iscan) {
142 quantize_b_helper_c(coeff_ptr, n_coeffs, zbin_ptr, round_ptr, quant_ptr,
143 quant_shift_ptr, qcoeff_ptr, dqcoeff_ptr, dequant_ptr,
144 eob_ptr, scan, iscan, NULL, NULL, 0);
145 }
146
aom_quantize_b_32x32_c(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)147 void aom_quantize_b_32x32_c(const tran_low_t *coeff_ptr, intptr_t n_coeffs,
148 const int16_t *zbin_ptr, const int16_t *round_ptr,
149 const int16_t *quant_ptr,
150 const int16_t *quant_shift_ptr,
151 tran_low_t *qcoeff_ptr, tran_low_t *dqcoeff_ptr,
152 const int16_t *dequant_ptr, uint16_t *eob_ptr,
153 const int16_t *scan, const int16_t *iscan) {
154 quantize_b_helper_c(coeff_ptr, n_coeffs, zbin_ptr, round_ptr, quant_ptr,
155 quant_shift_ptr, qcoeff_ptr, dqcoeff_ptr, dequant_ptr,
156 eob_ptr, scan, iscan, NULL, NULL, 1);
157 }
158
aom_quantize_b_64x64_c(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)159 void aom_quantize_b_64x64_c(const tran_low_t *coeff_ptr, intptr_t n_coeffs,
160 const int16_t *zbin_ptr, const int16_t *round_ptr,
161 const int16_t *quant_ptr,
162 const int16_t *quant_shift_ptr,
163 tran_low_t *qcoeff_ptr, tran_low_t *dqcoeff_ptr,
164 const int16_t *dequant_ptr, uint16_t *eob_ptr,
165 const int16_t *scan, const int16_t *iscan) {
166 quantize_b_helper_c(coeff_ptr, n_coeffs, zbin_ptr, round_ptr, quant_ptr,
167 quant_shift_ptr, qcoeff_ptr, dqcoeff_ptr, dequant_ptr,
168 eob_ptr, scan, iscan, NULL, NULL, 2);
169 }
170
aom_highbd_quantize_b_c(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)171 void aom_highbd_quantize_b_c(const tran_low_t *coeff_ptr, intptr_t n_coeffs,
172 const int16_t *zbin_ptr, const int16_t *round_ptr,
173 const int16_t *quant_ptr,
174 const int16_t *quant_shift_ptr,
175 tran_low_t *qcoeff_ptr, tran_low_t *dqcoeff_ptr,
176 const int16_t *dequant_ptr, uint16_t *eob_ptr,
177 const int16_t *scan, const int16_t *iscan) {
178 highbd_quantize_b_helper_c(coeff_ptr, n_coeffs, zbin_ptr, round_ptr,
179 quant_ptr, quant_shift_ptr, qcoeff_ptr,
180 dqcoeff_ptr, dequant_ptr, eob_ptr, scan, iscan,
181 NULL, NULL, 0);
182 }
183
aom_highbd_quantize_b_32x32_c(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)184 void aom_highbd_quantize_b_32x32_c(
185 const tran_low_t *coeff_ptr, intptr_t n_coeffs, const int16_t *zbin_ptr,
186 const int16_t *round_ptr, const int16_t *quant_ptr,
187 const int16_t *quant_shift_ptr, tran_low_t *qcoeff_ptr,
188 tran_low_t *dqcoeff_ptr, const int16_t *dequant_ptr, uint16_t *eob_ptr,
189 const int16_t *scan, const int16_t *iscan) {
190 highbd_quantize_b_helper_c(coeff_ptr, n_coeffs, zbin_ptr, round_ptr,
191 quant_ptr, quant_shift_ptr, qcoeff_ptr,
192 dqcoeff_ptr, dequant_ptr, eob_ptr, scan, iscan,
193 NULL, NULL, 1);
194 }
195
aom_highbd_quantize_b_64x64_c(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)196 void aom_highbd_quantize_b_64x64_c(
197 const tran_low_t *coeff_ptr, intptr_t n_coeffs, const int16_t *zbin_ptr,
198 const int16_t *round_ptr, const int16_t *quant_ptr,
199 const int16_t *quant_shift_ptr, tran_low_t *qcoeff_ptr,
200 tran_low_t *dqcoeff_ptr, const int16_t *dequant_ptr, uint16_t *eob_ptr,
201 const int16_t *scan, const int16_t *iscan) {
202 highbd_quantize_b_helper_c(coeff_ptr, n_coeffs, zbin_ptr, round_ptr,
203 quant_ptr, quant_shift_ptr, qcoeff_ptr,
204 dqcoeff_ptr, dequant_ptr, eob_ptr, scan, iscan,
205 NULL, NULL, 2);
206 }
207