2 * Copyright (c) 2015 The WebM project authors. All Rights Reserved.
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.
11 #include "vpx_dsp/quantize.h"
12 #include "vpx_mem/vpx_mem.h"
14 void vpx_quantize_dc(const tran_low_t
*coeff_ptr
,
15 int n_coeffs
, int skip_block
,
16 const int16_t *round_ptr
, const int16_t quant
,
17 tran_low_t
*qcoeff_ptr
, tran_low_t
*dqcoeff_ptr
,
18 const int16_t dequant_ptr
, uint16_t *eob_ptr
) {
20 const int coeff
= coeff_ptr
[rc
];
21 const int coeff_sign
= (coeff
>> 31);
22 const int abs_coeff
= (coeff
^ coeff_sign
) - coeff_sign
;
25 memset(qcoeff_ptr
, 0, n_coeffs
* sizeof(*qcoeff_ptr
));
26 memset(dqcoeff_ptr
, 0, n_coeffs
* sizeof(*dqcoeff_ptr
));
29 tmp
= clamp(abs_coeff
+ round_ptr
[rc
!= 0], INT16_MIN
, INT16_MAX
);
30 tmp
= (tmp
* quant
) >> 16;
31 qcoeff_ptr
[rc
] = (tmp
^ coeff_sign
) - coeff_sign
;
32 dqcoeff_ptr
[rc
] = qcoeff_ptr
[rc
] * dequant_ptr
;
39 #if CONFIG_VP9_HIGHBITDEPTH
40 void vpx_highbd_quantize_dc(const tran_low_t
*coeff_ptr
,
41 int n_coeffs
, int skip_block
,
42 const int16_t *round_ptr
, const int16_t quant
,
43 tran_low_t
*qcoeff_ptr
, tran_low_t
*dqcoeff_ptr
,
44 const int16_t dequant_ptr
, uint16_t *eob_ptr
) {
47 memset(qcoeff_ptr
, 0, n_coeffs
* sizeof(*qcoeff_ptr
));
48 memset(dqcoeff_ptr
, 0, n_coeffs
* sizeof(*dqcoeff_ptr
));
51 const int coeff
= coeff_ptr
[0];
52 const int coeff_sign
= (coeff
>> 31);
53 const int abs_coeff
= (coeff
^ coeff_sign
) - coeff_sign
;
54 const int64_t tmp
= abs_coeff
+ round_ptr
[0];
55 const uint32_t abs_qcoeff
= (uint32_t)((tmp
* quant
) >> 16);
56 qcoeff_ptr
[0] = (tran_low_t
)((abs_qcoeff
^ coeff_sign
) - coeff_sign
);
57 dqcoeff_ptr
[0] = qcoeff_ptr
[0] * dequant_ptr
;
65 void vpx_quantize_dc_32x32(const tran_low_t
*coeff_ptr
, int skip_block
,
66 const int16_t *round_ptr
, const int16_t quant
,
67 tran_low_t
*qcoeff_ptr
, tran_low_t
*dqcoeff_ptr
,
68 const int16_t dequant_ptr
, uint16_t *eob_ptr
) {
69 const int n_coeffs
= 1024;
71 const int coeff
= coeff_ptr
[rc
];
72 const int coeff_sign
= (coeff
>> 31);
73 const int abs_coeff
= (coeff
^ coeff_sign
) - coeff_sign
;
76 memset(qcoeff_ptr
, 0, n_coeffs
* sizeof(*qcoeff_ptr
));
77 memset(dqcoeff_ptr
, 0, n_coeffs
* sizeof(*dqcoeff_ptr
));
80 tmp
= clamp(abs_coeff
+ ROUND_POWER_OF_TWO(round_ptr
[rc
!= 0], 1),
81 INT16_MIN
, INT16_MAX
);
82 tmp
= (tmp
* quant
) >> 15;
83 qcoeff_ptr
[rc
] = (tmp
^ coeff_sign
) - coeff_sign
;
84 dqcoeff_ptr
[rc
] = qcoeff_ptr
[rc
] * dequant_ptr
/ 2;
91 #if CONFIG_VP9_HIGHBITDEPTH
92 void vpx_highbd_quantize_dc_32x32(const tran_low_t
*coeff_ptr
,
94 const int16_t *round_ptr
,
96 tran_low_t
*qcoeff_ptr
,
97 tran_low_t
*dqcoeff_ptr
,
98 const int16_t dequant_ptr
,
100 const int n_coeffs
= 1024;
103 memset(qcoeff_ptr
, 0, n_coeffs
* sizeof(*qcoeff_ptr
));
104 memset(dqcoeff_ptr
, 0, n_coeffs
* sizeof(*dqcoeff_ptr
));
107 const int coeff
= coeff_ptr
[0];
108 const int coeff_sign
= (coeff
>> 31);
109 const int abs_coeff
= (coeff
^ coeff_sign
) - coeff_sign
;
110 const int64_t tmp
= abs_coeff
+ ROUND_POWER_OF_TWO(round_ptr
[0], 1);
111 const uint32_t abs_qcoeff
= (uint32_t)((tmp
* quant
) >> 15);
112 qcoeff_ptr
[0] = (tran_low_t
)((abs_qcoeff
^ coeff_sign
) - coeff_sign
);
113 dqcoeff_ptr
[0] = qcoeff_ptr
[0] * dequant_ptr
/ 2;
121 void vpx_quantize_b_c(const tran_low_t
*coeff_ptr
, intptr_t n_coeffs
,
123 const int16_t *zbin_ptr
, const int16_t *round_ptr
,
124 const int16_t *quant_ptr
, const int16_t *quant_shift_ptr
,
125 tran_low_t
*qcoeff_ptr
, tran_low_t
*dqcoeff_ptr
,
126 const int16_t *dequant_ptr
,
128 const int16_t *scan
, const int16_t *iscan
) {
129 int i
, non_zero_count
= (int)n_coeffs
, eob
= -1;
130 const int zbins
[2] = {zbin_ptr
[0], zbin_ptr
[1]};
131 const int nzbins
[2] = {zbins
[0] * -1, zbins
[1] * -1};
134 memset(qcoeff_ptr
, 0, n_coeffs
* sizeof(*qcoeff_ptr
));
135 memset(dqcoeff_ptr
, 0, n_coeffs
* sizeof(*dqcoeff_ptr
));
139 for (i
= (int)n_coeffs
- 1; i
>= 0; i
--) {
140 const int rc
= scan
[i
];
141 const int coeff
= coeff_ptr
[rc
];
143 if (coeff
< zbins
[rc
!= 0] && coeff
> nzbins
[rc
!= 0])
149 // Quantization pass: All coefficients with index >= zero_flag are
150 // skippable. Note: zero_flag can be zero.
151 for (i
= 0; i
< non_zero_count
; i
++) {
152 const int rc
= scan
[i
];
153 const int coeff
= coeff_ptr
[rc
];
154 const int coeff_sign
= (coeff
>> 31);
155 const int abs_coeff
= (coeff
^ coeff_sign
) - coeff_sign
;
157 if (abs_coeff
>= zbins
[rc
!= 0]) {
158 int tmp
= clamp(abs_coeff
+ round_ptr
[rc
!= 0], INT16_MIN
, INT16_MAX
);
159 tmp
= ((((tmp
* quant_ptr
[rc
!= 0]) >> 16) + tmp
) *
160 quant_shift_ptr
[rc
!= 0]) >> 16; // quantization
161 qcoeff_ptr
[rc
] = (tmp
^ coeff_sign
) - coeff_sign
;
162 dqcoeff_ptr
[rc
] = qcoeff_ptr
[rc
] * dequant_ptr
[rc
!= 0];
172 #if CONFIG_VP9_HIGHBITDEPTH
173 void vpx_highbd_quantize_b_c(const tran_low_t
*coeff_ptr
, intptr_t n_coeffs
,
174 int skip_block
, const int16_t *zbin_ptr
,
175 const int16_t *round_ptr
, const int16_t *quant_ptr
,
176 const int16_t *quant_shift_ptr
,
177 tran_low_t
*qcoeff_ptr
, tran_low_t
*dqcoeff_ptr
,
178 const int16_t *dequant_ptr
,
179 uint16_t *eob_ptr
, const int16_t *scan
,
180 const int16_t *iscan
) {
181 int i
, non_zero_count
= (int)n_coeffs
, eob
= -1;
182 const int zbins
[2] = {zbin_ptr
[0], zbin_ptr
[1]};
183 const int nzbins
[2] = {zbins
[0] * -1, zbins
[1] * -1};
186 memset(qcoeff_ptr
, 0, n_coeffs
* sizeof(*qcoeff_ptr
));
187 memset(dqcoeff_ptr
, 0, n_coeffs
* sizeof(*dqcoeff_ptr
));
191 for (i
= (int)n_coeffs
- 1; i
>= 0; i
--) {
192 const int rc
= scan
[i
];
193 const int coeff
= coeff_ptr
[rc
];
195 if (coeff
< zbins
[rc
!= 0] && coeff
> nzbins
[rc
!= 0])
201 // Quantization pass: All coefficients with index >= zero_flag are
202 // skippable. Note: zero_flag can be zero.
203 for (i
= 0; i
< non_zero_count
; i
++) {
204 const int rc
= scan
[i
];
205 const int coeff
= coeff_ptr
[rc
];
206 const int coeff_sign
= (coeff
>> 31);
207 const int abs_coeff
= (coeff
^ coeff_sign
) - coeff_sign
;
209 if (abs_coeff
>= zbins
[rc
!= 0]) {
210 const int64_t tmp1
= abs_coeff
+ round_ptr
[rc
!= 0];
211 const int64_t tmp2
= ((tmp1
* quant_ptr
[rc
!= 0]) >> 16) + tmp1
;
212 const uint32_t abs_qcoeff
=
213 (uint32_t)((tmp2
* quant_shift_ptr
[rc
!= 0]) >> 16);
214 qcoeff_ptr
[rc
] = (tran_low_t
)((abs_qcoeff
^ coeff_sign
) - coeff_sign
);
215 dqcoeff_ptr
[rc
] = qcoeff_ptr
[rc
] * dequant_ptr
[rc
!= 0];
225 void vpx_quantize_b_32x32_c(const tran_low_t
*coeff_ptr
, intptr_t n_coeffs
,
227 const int16_t *zbin_ptr
, const int16_t *round_ptr
,
228 const int16_t *quant_ptr
,
229 const int16_t *quant_shift_ptr
,
230 tran_low_t
*qcoeff_ptr
, tran_low_t
*dqcoeff_ptr
,
231 const int16_t *dequant_ptr
,
233 const int16_t *scan
, const int16_t *iscan
) {
234 const int zbins
[2] = {ROUND_POWER_OF_TWO(zbin_ptr
[0], 1),
235 ROUND_POWER_OF_TWO(zbin_ptr
[1], 1)};
236 const int nzbins
[2] = {zbins
[0] * -1, zbins
[1] * -1};
243 memset(qcoeff_ptr
, 0, n_coeffs
* sizeof(*qcoeff_ptr
));
244 memset(dqcoeff_ptr
, 0, n_coeffs
* sizeof(*dqcoeff_ptr
));
248 for (i
= 0; i
< n_coeffs
; i
++) {
249 const int rc
= scan
[i
];
250 const int coeff
= coeff_ptr
[rc
];
252 // If the coefficient is out of the base ZBIN range, keep it for
254 if (coeff
>= zbins
[rc
!= 0] || coeff
<= nzbins
[rc
!= 0])
258 // Quantization pass: only process the coefficients selected in
259 // pre-scan pass. Note: idx can be zero.
260 for (i
= 0; i
< idx
; i
++) {
261 const int rc
= scan
[idx_arr
[i
]];
262 const int coeff
= coeff_ptr
[rc
];
263 const int coeff_sign
= (coeff
>> 31);
265 int abs_coeff
= (coeff
^ coeff_sign
) - coeff_sign
;
266 abs_coeff
+= ROUND_POWER_OF_TWO(round_ptr
[rc
!= 0], 1);
267 abs_coeff
= clamp(abs_coeff
, INT16_MIN
, INT16_MAX
);
268 tmp
= ((((abs_coeff
* quant_ptr
[rc
!= 0]) >> 16) + abs_coeff
) *
269 quant_shift_ptr
[rc
!= 0]) >> 15;
271 qcoeff_ptr
[rc
] = (tmp
^ coeff_sign
) - coeff_sign
;
272 dqcoeff_ptr
[rc
] = qcoeff_ptr
[rc
] * dequant_ptr
[rc
!= 0] / 2;
281 #if CONFIG_VP9_HIGHBITDEPTH
282 void vpx_highbd_quantize_b_32x32_c(const tran_low_t
*coeff_ptr
,
283 intptr_t n_coeffs
, int skip_block
,
284 const int16_t *zbin_ptr
,
285 const int16_t *round_ptr
,
286 const int16_t *quant_ptr
,
287 const int16_t *quant_shift_ptr
,
288 tran_low_t
*qcoeff_ptr
,
289 tran_low_t
*dqcoeff_ptr
,
290 const int16_t *dequant_ptr
,
292 const int16_t *scan
, const int16_t *iscan
) {
293 const int zbins
[2] = {ROUND_POWER_OF_TWO(zbin_ptr
[0], 1),
294 ROUND_POWER_OF_TWO(zbin_ptr
[1], 1)};
295 const int nzbins
[2] = {zbins
[0] * -1, zbins
[1] * -1};
302 memset(qcoeff_ptr
, 0, n_coeffs
* sizeof(*qcoeff_ptr
));
303 memset(dqcoeff_ptr
, 0, n_coeffs
* sizeof(*dqcoeff_ptr
));
307 for (i
= 0; i
< n_coeffs
; i
++) {
308 const int rc
= scan
[i
];
309 const int coeff
= coeff_ptr
[rc
];
311 // If the coefficient is out of the base ZBIN range, keep it for
313 if (coeff
>= zbins
[rc
!= 0] || coeff
<= nzbins
[rc
!= 0])
317 // Quantization pass: only process the coefficients selected in
318 // pre-scan pass. Note: idx can be zero.
319 for (i
= 0; i
< idx
; i
++) {
320 const int rc
= scan
[idx_arr
[i
]];
321 const int coeff
= coeff_ptr
[rc
];
322 const int coeff_sign
= (coeff
>> 31);
323 const int abs_coeff
= (coeff
^ coeff_sign
) - coeff_sign
;
324 const int64_t tmp1
= abs_coeff
325 + ROUND_POWER_OF_TWO(round_ptr
[rc
!= 0], 1);
326 const int64_t tmp2
= ((tmp1
* quant_ptr
[rc
!= 0]) >> 16) + tmp1
;
327 const uint32_t abs_qcoeff
=
328 (uint32_t)((tmp2
* quant_shift_ptr
[rc
!= 0]) >> 15);
329 qcoeff_ptr
[rc
] = (tran_low_t
)((abs_qcoeff
^ coeff_sign
) - coeff_sign
);
330 dqcoeff_ptr
[rc
] = qcoeff_ptr
[rc
] * dequant_ptr
[rc
!= 0] / 2;