Merge changes from topic 'missing-proto'
[aom.git] / vpx_dsp / quantize.c
blob6426cccc73389cf1fb51d8d9613881e645b945d3
1 /*
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.
9 */
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) {
20 const int rc = 0;
21 const int coeff = coeff_ptr[rc];
22 const int coeff_sign = (coeff >> 31);
23 const int abs_coeff = (coeff ^ coeff_sign) - coeff_sign;
24 int tmp, eob = -1;
26 memset(qcoeff_ptr, 0, n_coeffs * sizeof(*qcoeff_ptr));
27 memset(dqcoeff_ptr, 0, n_coeffs * sizeof(*dqcoeff_ptr));
29 if (!skip_block) {
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;
34 if (tmp)
35 eob = 0;
37 *eob_ptr = eob + 1;
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) {
46 int eob = -1;
48 memset(qcoeff_ptr, 0, n_coeffs * sizeof(*qcoeff_ptr));
49 memset(dqcoeff_ptr, 0, n_coeffs * sizeof(*dqcoeff_ptr));
51 if (!skip_block) {
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;
59 if (abs_qcoeff)
60 eob = 0;
62 *eob_ptr = eob + 1;
64 #endif
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;
71 const int rc = 0;
72 const int coeff = coeff_ptr[rc];
73 const int coeff_sign = (coeff >> 31);
74 const int abs_coeff = (coeff ^ coeff_sign) - coeff_sign;
75 int tmp, eob = -1;
77 memset(qcoeff_ptr, 0, n_coeffs * sizeof(*qcoeff_ptr));
78 memset(dqcoeff_ptr, 0, n_coeffs * sizeof(*dqcoeff_ptr));
80 if (!skip_block) {
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;
86 if (tmp)
87 eob = 0;
89 *eob_ptr = eob + 1;
92 #if CONFIG_VP9_HIGHBITDEPTH
93 void vpx_highbd_quantize_dc_32x32(const tran_low_t *coeff_ptr,
94 int skip_block,
95 const int16_t *round_ptr,
96 const int16_t quant,
97 tran_low_t *qcoeff_ptr,
98 tran_low_t *dqcoeff_ptr,
99 const int16_t dequant_ptr,
100 uint16_t *eob_ptr) {
101 const int n_coeffs = 1024;
102 int eob = -1;
104 memset(qcoeff_ptr, 0, n_coeffs * sizeof(*qcoeff_ptr));
105 memset(dqcoeff_ptr, 0, n_coeffs * sizeof(*dqcoeff_ptr));
107 if (!skip_block) {
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;
115 if (abs_qcoeff)
116 eob = 0;
118 *eob_ptr = eob + 1;
120 #endif
122 void vpx_quantize_b_c(const tran_low_t *coeff_ptr, intptr_t n_coeffs,
123 int skip_block,
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,
128 uint16_t *eob_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};
133 (void)iscan;
135 memset(qcoeff_ptr, 0, n_coeffs * sizeof(*qcoeff_ptr));
136 memset(dqcoeff_ptr, 0, n_coeffs * sizeof(*dqcoeff_ptr));
138 if (!skip_block) {
139 // Pre-scan pass
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])
145 non_zero_count--;
146 else
147 break;
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];
165 if (tmp)
166 eob = i;
170 *eob_ptr = eob + 1;
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};
185 (void)iscan;
187 memset(qcoeff_ptr, 0, n_coeffs * sizeof(*qcoeff_ptr));
188 memset(dqcoeff_ptr, 0, n_coeffs * sizeof(*dqcoeff_ptr));
190 if (!skip_block) {
191 // Pre-scan pass
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])
197 non_zero_count--;
198 else
199 break;
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];
217 if (abs_qcoeff)
218 eob = i;
222 *eob_ptr = eob + 1;
224 #endif
226 void vpx_quantize_b_32x32_c(const tran_low_t *coeff_ptr, intptr_t n_coeffs,
227 int skip_block,
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,
233 uint16_t *eob_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};
239 int idx = 0;
240 int idx_arr[1024];
241 int i, eob = -1;
242 (void)iscan;
244 memset(qcoeff_ptr, 0, n_coeffs * sizeof(*qcoeff_ptr));
245 memset(dqcoeff_ptr, 0, n_coeffs * sizeof(*dqcoeff_ptr));
247 if (!skip_block) {
248 // Pre-scan pass
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
254 // quantization.
255 if (coeff >= zbins[rc != 0] || coeff <= nzbins[rc != 0])
256 idx_arr[idx++] = i;
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);
265 int tmp;
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;
275 if (tmp)
276 eob = idx_arr[i];
279 *eob_ptr = eob + 1;
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,
292 uint16_t *eob_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};
298 int idx = 0;
299 int idx_arr[1024];
300 int i, eob = -1;
301 (void)iscan;
303 memset(qcoeff_ptr, 0, n_coeffs * sizeof(*qcoeff_ptr));
304 memset(dqcoeff_ptr, 0, n_coeffs * sizeof(*dqcoeff_ptr));
306 if (!skip_block) {
307 // Pre-scan pass
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
313 // quantization.
314 if (coeff >= zbins[rc != 0] || coeff <= nzbins[rc != 0])
315 idx_arr[idx++] = i;
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;
332 if (abs_qcoeff)
333 eob = idx_arr[i];
336 *eob_ptr = eob + 1;
338 #endif