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_rtcd.h"
12 #include "vpx_dsp/quantize.h"
13 #include "vpx_mem/vpx_mem.h"
15 void vpx_quantize_dc(const tran_low_t
*coeff_ptr
,
16 int n_coeffs
, int skip_block
,
17 const int16_t *round_ptr
, const int16_t quant
,
18 tran_low_t
*qcoeff_ptr
, tran_low_t
*dqcoeff_ptr
,
19 const int16_t dequant_ptr
, uint16_t *eob_ptr
) {
21 const int coeff
= coeff_ptr
[rc
];
22 const int coeff_sign
= (coeff
>> 31);
23 const int abs_coeff
= (coeff
^ coeff_sign
) - coeff_sign
;
26 memset(qcoeff_ptr
, 0, n_coeffs
* sizeof(*qcoeff_ptr
));
27 memset(dqcoeff_ptr
, 0, n_coeffs
* sizeof(*dqcoeff_ptr
));
30 tmp
= clamp(abs_coeff
+ round_ptr
[rc
!= 0], INT16_MIN
, INT16_MAX
);
31 tmp
= (tmp
* quant
) >> 16;
32 qcoeff_ptr
[rc
] = (tmp
^ coeff_sign
) - coeff_sign
;
33 dqcoeff_ptr
[rc
] = qcoeff_ptr
[rc
] * dequant_ptr
;
40 #if CONFIG_VP9_HIGHBITDEPTH
41 void vpx_highbd_quantize_dc(const tran_low_t
*coeff_ptr
,
42 int n_coeffs
, int skip_block
,
43 const int16_t *round_ptr
, const int16_t quant
,
44 tran_low_t
*qcoeff_ptr
, tran_low_t
*dqcoeff_ptr
,
45 const int16_t dequant_ptr
, uint16_t *eob_ptr
) {
48 memset(qcoeff_ptr
, 0, n_coeffs
* sizeof(*qcoeff_ptr
));
49 memset(dqcoeff_ptr
, 0, n_coeffs
* sizeof(*dqcoeff_ptr
));
52 const int coeff
= coeff_ptr
[0];
53 const int coeff_sign
= (coeff
>> 31);
54 const int abs_coeff
= (coeff
^ coeff_sign
) - coeff_sign
;
55 const int64_t tmp
= abs_coeff
+ round_ptr
[0];
56 const uint32_t abs_qcoeff
= (uint32_t)((tmp
* quant
) >> 16);
57 qcoeff_ptr
[0] = (tran_low_t
)((abs_qcoeff
^ coeff_sign
) - coeff_sign
);
58 dqcoeff_ptr
[0] = qcoeff_ptr
[0] * dequant_ptr
;
66 void vpx_quantize_dc_32x32(const tran_low_t
*coeff_ptr
, int skip_block
,
67 const int16_t *round_ptr
, const int16_t quant
,
68 tran_low_t
*qcoeff_ptr
, tran_low_t
*dqcoeff_ptr
,
69 const int16_t dequant_ptr
, uint16_t *eob_ptr
) {
70 const int n_coeffs
= 1024;
72 const int coeff
= coeff_ptr
[rc
];
73 const int coeff_sign
= (coeff
>> 31);
74 const int abs_coeff
= (coeff
^ coeff_sign
) - coeff_sign
;
77 memset(qcoeff_ptr
, 0, n_coeffs
* sizeof(*qcoeff_ptr
));
78 memset(dqcoeff_ptr
, 0, n_coeffs
* sizeof(*dqcoeff_ptr
));
81 tmp
= clamp(abs_coeff
+ ROUND_POWER_OF_TWO(round_ptr
[rc
!= 0], 1),
82 INT16_MIN
, INT16_MAX
);
83 tmp
= (tmp
* quant
) >> 15;
84 qcoeff_ptr
[rc
] = (tmp
^ coeff_sign
) - coeff_sign
;
85 dqcoeff_ptr
[rc
] = qcoeff_ptr
[rc
] * dequant_ptr
/ 2;
92 #if CONFIG_VP9_HIGHBITDEPTH
93 void vpx_highbd_quantize_dc_32x32(const tran_low_t
*coeff_ptr
,
95 const int16_t *round_ptr
,
97 tran_low_t
*qcoeff_ptr
,
98 tran_low_t
*dqcoeff_ptr
,
99 const int16_t dequant_ptr
,
101 const int n_coeffs
= 1024;
104 memset(qcoeff_ptr
, 0, n_coeffs
* sizeof(*qcoeff_ptr
));
105 memset(dqcoeff_ptr
, 0, n_coeffs
* sizeof(*dqcoeff_ptr
));
108 const int coeff
= coeff_ptr
[0];
109 const int coeff_sign
= (coeff
>> 31);
110 const int abs_coeff
= (coeff
^ coeff_sign
) - coeff_sign
;
111 const int64_t tmp
= abs_coeff
+ ROUND_POWER_OF_TWO(round_ptr
[0], 1);
112 const uint32_t abs_qcoeff
= (uint32_t)((tmp
* quant
) >> 15);
113 qcoeff_ptr
[0] = (tran_low_t
)((abs_qcoeff
^ coeff_sign
) - coeff_sign
);
114 dqcoeff_ptr
[0] = qcoeff_ptr
[0] * dequant_ptr
/ 2;
122 void vpx_quantize_b_c(const tran_low_t
*coeff_ptr
, intptr_t n_coeffs
,
124 const int16_t *zbin_ptr
, const int16_t *round_ptr
,
125 const int16_t *quant_ptr
, const int16_t *quant_shift_ptr
,
126 tran_low_t
*qcoeff_ptr
, tran_low_t
*dqcoeff_ptr
,
127 const int16_t *dequant_ptr
,
129 const int16_t *scan
, const int16_t *iscan
) {
130 int i
, non_zero_count
= (int)n_coeffs
, eob
= -1;
131 const int zbins
[2] = {zbin_ptr
[0], zbin_ptr
[1]};
132 const int nzbins
[2] = {zbins
[0] * -1, zbins
[1] * -1};
135 memset(qcoeff_ptr
, 0, n_coeffs
* sizeof(*qcoeff_ptr
));
136 memset(dqcoeff_ptr
, 0, n_coeffs
* sizeof(*dqcoeff_ptr
));
140 for (i
= (int)n_coeffs
- 1; i
>= 0; i
--) {
141 const int rc
= scan
[i
];
142 const int coeff
= coeff_ptr
[rc
];
144 if (coeff
< zbins
[rc
!= 0] && coeff
> nzbins
[rc
!= 0])
150 // Quantization pass: All coefficients with index >= zero_flag are
151 // skippable. Note: zero_flag can be zero.
152 for (i
= 0; i
< non_zero_count
; i
++) {
153 const int rc
= scan
[i
];
154 const int coeff
= coeff_ptr
[rc
];
155 const int coeff_sign
= (coeff
>> 31);
156 const int abs_coeff
= (coeff
^ coeff_sign
) - coeff_sign
;
158 if (abs_coeff
>= zbins
[rc
!= 0]) {
159 int tmp
= clamp(abs_coeff
+ round_ptr
[rc
!= 0], INT16_MIN
, INT16_MAX
);
160 tmp
= ((((tmp
* quant_ptr
[rc
!= 0]) >> 16) + tmp
) *
161 quant_shift_ptr
[rc
!= 0]) >> 16; // quantization
162 qcoeff_ptr
[rc
] = (tmp
^ coeff_sign
) - coeff_sign
;
163 dqcoeff_ptr
[rc
] = qcoeff_ptr
[rc
] * dequant_ptr
[rc
!= 0];
173 #if CONFIG_VP9_HIGHBITDEPTH
174 void vpx_highbd_quantize_b_c(const tran_low_t
*coeff_ptr
, intptr_t n_coeffs
,
175 int skip_block
, const int16_t *zbin_ptr
,
176 const int16_t *round_ptr
, const int16_t *quant_ptr
,
177 const int16_t *quant_shift_ptr
,
178 tran_low_t
*qcoeff_ptr
, tran_low_t
*dqcoeff_ptr
,
179 const int16_t *dequant_ptr
,
180 uint16_t *eob_ptr
, const int16_t *scan
,
181 const int16_t *iscan
) {
182 int i
, non_zero_count
= (int)n_coeffs
, eob
= -1;
183 const int zbins
[2] = {zbin_ptr
[0], zbin_ptr
[1]};
184 const int nzbins
[2] = {zbins
[0] * -1, zbins
[1] * -1};
187 memset(qcoeff_ptr
, 0, n_coeffs
* sizeof(*qcoeff_ptr
));
188 memset(dqcoeff_ptr
, 0, n_coeffs
* sizeof(*dqcoeff_ptr
));
192 for (i
= (int)n_coeffs
- 1; i
>= 0; i
--) {
193 const int rc
= scan
[i
];
194 const int coeff
= coeff_ptr
[rc
];
196 if (coeff
< zbins
[rc
!= 0] && coeff
> nzbins
[rc
!= 0])
202 // Quantization pass: All coefficients with index >= zero_flag are
203 // skippable. Note: zero_flag can be zero.
204 for (i
= 0; i
< non_zero_count
; i
++) {
205 const int rc
= scan
[i
];
206 const int coeff
= coeff_ptr
[rc
];
207 const int coeff_sign
= (coeff
>> 31);
208 const int abs_coeff
= (coeff
^ coeff_sign
) - coeff_sign
;
210 if (abs_coeff
>= zbins
[rc
!= 0]) {
211 const int64_t tmp1
= abs_coeff
+ round_ptr
[rc
!= 0];
212 const int64_t tmp2
= ((tmp1
* quant_ptr
[rc
!= 0]) >> 16) + tmp1
;
213 const uint32_t abs_qcoeff
=
214 (uint32_t)((tmp2
* quant_shift_ptr
[rc
!= 0]) >> 16);
215 qcoeff_ptr
[rc
] = (tran_low_t
)((abs_qcoeff
^ coeff_sign
) - coeff_sign
);
216 dqcoeff_ptr
[rc
] = qcoeff_ptr
[rc
] * dequant_ptr
[rc
!= 0];
226 void vpx_quantize_b_32x32_c(const tran_low_t
*coeff_ptr
, intptr_t n_coeffs
,
228 const int16_t *zbin_ptr
, const int16_t *round_ptr
,
229 const int16_t *quant_ptr
,
230 const int16_t *quant_shift_ptr
,
231 tran_low_t
*qcoeff_ptr
, tran_low_t
*dqcoeff_ptr
,
232 const int16_t *dequant_ptr
,
234 const int16_t *scan
, const int16_t *iscan
) {
235 const int zbins
[2] = {ROUND_POWER_OF_TWO(zbin_ptr
[0], 1),
236 ROUND_POWER_OF_TWO(zbin_ptr
[1], 1)};
237 const int nzbins
[2] = {zbins
[0] * -1, zbins
[1] * -1};
244 memset(qcoeff_ptr
, 0, n_coeffs
* sizeof(*qcoeff_ptr
));
245 memset(dqcoeff_ptr
, 0, n_coeffs
* sizeof(*dqcoeff_ptr
));
249 for (i
= 0; i
< n_coeffs
; i
++) {
250 const int rc
= scan
[i
];
251 const int coeff
= coeff_ptr
[rc
];
253 // If the coefficient is out of the base ZBIN range, keep it for
255 if (coeff
>= zbins
[rc
!= 0] || coeff
<= nzbins
[rc
!= 0])
259 // Quantization pass: only process the coefficients selected in
260 // pre-scan pass. Note: idx can be zero.
261 for (i
= 0; i
< idx
; i
++) {
262 const int rc
= scan
[idx_arr
[i
]];
263 const int coeff
= coeff_ptr
[rc
];
264 const int coeff_sign
= (coeff
>> 31);
266 int abs_coeff
= (coeff
^ coeff_sign
) - coeff_sign
;
267 abs_coeff
+= ROUND_POWER_OF_TWO(round_ptr
[rc
!= 0], 1);
268 abs_coeff
= clamp(abs_coeff
, INT16_MIN
, INT16_MAX
);
269 tmp
= ((((abs_coeff
* quant_ptr
[rc
!= 0]) >> 16) + abs_coeff
) *
270 quant_shift_ptr
[rc
!= 0]) >> 15;
272 qcoeff_ptr
[rc
] = (tmp
^ coeff_sign
) - coeff_sign
;
273 dqcoeff_ptr
[rc
] = qcoeff_ptr
[rc
] * dequant_ptr
[rc
!= 0] / 2;
282 #if CONFIG_VP9_HIGHBITDEPTH
283 void vpx_highbd_quantize_b_32x32_c(const tran_low_t
*coeff_ptr
,
284 intptr_t n_coeffs
, int skip_block
,
285 const int16_t *zbin_ptr
,
286 const int16_t *round_ptr
,
287 const int16_t *quant_ptr
,
288 const int16_t *quant_shift_ptr
,
289 tran_low_t
*qcoeff_ptr
,
290 tran_low_t
*dqcoeff_ptr
,
291 const int16_t *dequant_ptr
,
293 const int16_t *scan
, const int16_t *iscan
) {
294 const int zbins
[2] = {ROUND_POWER_OF_TWO(zbin_ptr
[0], 1),
295 ROUND_POWER_OF_TWO(zbin_ptr
[1], 1)};
296 const int nzbins
[2] = {zbins
[0] * -1, zbins
[1] * -1};
303 memset(qcoeff_ptr
, 0, n_coeffs
* sizeof(*qcoeff_ptr
));
304 memset(dqcoeff_ptr
, 0, n_coeffs
* sizeof(*dqcoeff_ptr
));
308 for (i
= 0; i
< n_coeffs
; i
++) {
309 const int rc
= scan
[i
];
310 const int coeff
= coeff_ptr
[rc
];
312 // If the coefficient is out of the base ZBIN range, keep it for
314 if (coeff
>= zbins
[rc
!= 0] || coeff
<= nzbins
[rc
!= 0])
318 // Quantization pass: only process the coefficients selected in
319 // pre-scan pass. Note: idx can be zero.
320 for (i
= 0; i
< idx
; i
++) {
321 const int rc
= scan
[idx_arr
[i
]];
322 const int coeff
= coeff_ptr
[rc
];
323 const int coeff_sign
= (coeff
>> 31);
324 const int abs_coeff
= (coeff
^ coeff_sign
) - coeff_sign
;
325 const int64_t tmp1
= abs_coeff
326 + ROUND_POWER_OF_TWO(round_ptr
[rc
!= 0], 1);
327 const int64_t tmp2
= ((tmp1
* quant_ptr
[rc
!= 0]) >> 16) + tmp1
;
328 const uint32_t abs_qcoeff
=
329 (uint32_t)((tmp2
* quant_shift_ptr
[rc
!= 0]) >> 15);
330 qcoeff_ptr
[rc
] = (tran_low_t
)((abs_qcoeff
^ coeff_sign
) - coeff_sign
);
331 dqcoeff_ptr
[rc
] = qcoeff_ptr
[rc
] * dequant_ptr
[rc
!= 0] / 2;