Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1 | /* |
Yaowu Xu | 9c01aa1 | 2016-09-01 14:32:49 -0700 | [diff] [blame] | 2 | * Copyright (c) 2016, Alliance for Open Media. All rights reserved |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3 | * |
Yaowu Xu | 9c01aa1 | 2016-09-01 14:32:49 -0700 | [diff] [blame] | 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. |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10 | */ |
| 11 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 12 | #include "aom_dsp/quantize.h" |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 13 | #include "aom_mem/aom_mem.h" |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 14 | |
| 15 | #if CONFIG_AOM_QM |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 16 | void aom_quantize_dc(const tran_low_t *coeff_ptr, int n_coeffs, int skip_block, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 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 qm_val_t *qm_ptr, const qm_val_t *iqm_ptr) { |
| 21 | const int rc = 0; |
| 22 | const int coeff = coeff_ptr[rc]; |
| 23 | const int coeff_sign = (coeff >> 31); |
| 24 | const int abs_coeff = (coeff ^ coeff_sign) - coeff_sign; |
| 25 | int64_t tmp, eob = -1; |
| 26 | int32_t tmp32; |
| 27 | int dequant = |
| 28 | (dequant_ptr * iqm_ptr[rc] + (1 << (AOM_QM_BITS - 1))) >> AOM_QM_BITS; |
| 29 | |
| 30 | memset(qcoeff_ptr, 0, n_coeffs * sizeof(*qcoeff_ptr)); |
| 31 | memset(dqcoeff_ptr, 0, n_coeffs * sizeof(*dqcoeff_ptr)); |
| 32 | |
| 33 | if (!skip_block) { |
| 34 | tmp = clamp(abs_coeff + round_ptr[rc != 0], INT16_MIN, INT16_MAX); |
| 35 | tmp32 = (int32_t)((tmp * qm_ptr[rc] * quant) >> (16 + AOM_QM_BITS)); |
| 36 | qcoeff_ptr[rc] = (tmp32 ^ coeff_sign) - coeff_sign; |
| 37 | dqcoeff_ptr[rc] = qcoeff_ptr[rc] * dequant; |
| 38 | if (tmp32) eob = 0; |
| 39 | } |
| 40 | *eob_ptr = eob + 1; |
| 41 | } |
| 42 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 43 | #if CONFIG_AOM_HIGHBITDEPTH |
| 44 | void aom_highbd_quantize_dc(const tran_low_t *coeff_ptr, int n_coeffs, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 45 | int skip_block, const int16_t *round_ptr, |
| 46 | const int16_t quant, tran_low_t *qcoeff_ptr, |
| 47 | tran_low_t *dqcoeff_ptr, const int16_t dequant_ptr, |
| 48 | uint16_t *eob_ptr, const qm_val_t *qm_ptr, |
| 49 | const qm_val_t *iqm_ptr) { |
| 50 | int eob = -1; |
| 51 | int dequant = |
| 52 | (dequant_ptr * iqm_ptr[0] + (1 << (AOM_QM_BITS - 1))) >> AOM_QM_BITS; |
| 53 | |
| 54 | memset(qcoeff_ptr, 0, n_coeffs * sizeof(*qcoeff_ptr)); |
| 55 | memset(dqcoeff_ptr, 0, n_coeffs * sizeof(*dqcoeff_ptr)); |
| 56 | |
| 57 | if (!skip_block) { |
| 58 | const int coeff = coeff_ptr[0]; |
| 59 | const int coeff_sign = (coeff >> 31); |
| 60 | const int abs_coeff = (coeff ^ coeff_sign) - coeff_sign; |
| 61 | const int64_t tmp = abs_coeff + round_ptr[0]; |
| 62 | const uint32_t abs_qcoeff = |
| 63 | (uint32_t)((tmp * qm_ptr[0] * quant) >> (16 + AOM_QM_BITS)); |
| 64 | qcoeff_ptr[0] = (tran_low_t)((abs_qcoeff ^ coeff_sign) - coeff_sign); |
| 65 | dqcoeff_ptr[0] = qcoeff_ptr[0] * dequant; |
| 66 | if (abs_qcoeff) eob = 0; |
| 67 | } |
| 68 | *eob_ptr = eob + 1; |
| 69 | } |
| 70 | #endif |
| 71 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 72 | void aom_quantize_dc_32x32(const tran_low_t *coeff_ptr, int skip_block, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 73 | const int16_t *round_ptr, const int16_t quant, |
| 74 | tran_low_t *qcoeff_ptr, tran_low_t *dqcoeff_ptr, |
| 75 | const int16_t dequant_ptr, uint16_t *eob_ptr, |
| 76 | const qm_val_t *qm_ptr, const qm_val_t *iqm_ptr) { |
| 77 | const int n_coeffs = 1024; |
| 78 | const int rc = 0; |
| 79 | const int coeff = coeff_ptr[rc]; |
| 80 | const int coeff_sign = (coeff >> 31); |
| 81 | const int abs_coeff = (coeff ^ coeff_sign) - coeff_sign; |
| 82 | int64_t tmp, eob = -1; |
| 83 | int32_t tmp32; |
| 84 | int dequant; |
| 85 | |
| 86 | memset(qcoeff_ptr, 0, n_coeffs * sizeof(*qcoeff_ptr)); |
| 87 | memset(dqcoeff_ptr, 0, n_coeffs * sizeof(*dqcoeff_ptr)); |
| 88 | |
| 89 | if (!skip_block) { |
| 90 | tmp = clamp(abs_coeff + ROUND_POWER_OF_TWO(round_ptr[rc != 0], 1), |
| 91 | INT16_MIN, INT16_MAX); |
| 92 | tmp32 = (int32_t)((tmp * qm_ptr[rc] * quant) >> (15 + AOM_QM_BITS)); |
| 93 | qcoeff_ptr[rc] = (tmp32 ^ coeff_sign) - coeff_sign; |
| 94 | dequant = |
| 95 | (dequant_ptr * iqm_ptr[rc] + (1 << (AOM_QM_BITS - 1))) >> AOM_QM_BITS; |
| 96 | dqcoeff_ptr[rc] = (qcoeff_ptr[rc] * dequant) / 2; |
| 97 | if (tmp32) eob = 0; |
| 98 | } |
| 99 | *eob_ptr = eob + 1; |
| 100 | } |
| 101 | |
Debargha Mukherjee | 0e11912 | 2016-11-04 12:10:23 -0700 | [diff] [blame] | 102 | #if CONFIG_TX64X64 |
| 103 | void aom_quantize_dc_64x64(const tran_low_t *coeff_ptr, int skip_block, |
| 104 | const int16_t *round_ptr, const int16_t quant, |
| 105 | tran_low_t *qcoeff_ptr, tran_low_t *dqcoeff_ptr, |
| 106 | const int16_t dequant_ptr, uint16_t *eob_ptr, |
| 107 | const qm_val_t *qm_ptr, const qm_val_t *iqm_ptr) { |
| 108 | const int n_coeffs = 1024; |
| 109 | const int rc = 0; |
| 110 | const int coeff = coeff_ptr[rc]; |
| 111 | const int coeff_sign = (coeff >> 31); |
| 112 | const int abs_coeff = (coeff ^ coeff_sign) - coeff_sign; |
| 113 | int64_t tmp, eob = -1; |
| 114 | int32_t tmp32; |
| 115 | int dequant; |
| 116 | |
| 117 | memset(qcoeff_ptr, 0, n_coeffs * sizeof(*qcoeff_ptr)); |
| 118 | memset(dqcoeff_ptr, 0, n_coeffs * sizeof(*dqcoeff_ptr)); |
| 119 | |
| 120 | if (!skip_block) { |
| 121 | tmp = clamp(abs_coeff + ROUND_POWER_OF_TWO(round_ptr[rc != 0], 2), |
| 122 | INT16_MIN, INT16_MAX); |
| 123 | tmp32 = (int32_t)((tmp * qm_ptr[rc] * quant) >> (14 + AOM_QM_BITS)); |
| 124 | qcoeff_ptr[rc] = (tmp32 ^ coeff_sign) - coeff_sign; |
| 125 | dequant = |
| 126 | (dequant_ptr * iqm_ptr[rc] + (1 << (AOM_QM_BITS - 1))) >> AOM_QM_BITS; |
| 127 | dqcoeff_ptr[rc] = (qcoeff_ptr[rc] * dequant) / 4; |
| 128 | if (tmp32) eob = 0; |
| 129 | } |
| 130 | *eob_ptr = eob + 1; |
| 131 | } |
| 132 | #endif // CONFIG_TX64X64 |
| 133 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 134 | #if CONFIG_AOM_HIGHBITDEPTH |
| 135 | void aom_highbd_quantize_dc_32x32(const tran_low_t *coeff_ptr, int skip_block, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 136 | const int16_t *round_ptr, const int16_t quant, |
| 137 | tran_low_t *qcoeff_ptr, |
| 138 | tran_low_t *dqcoeff_ptr, |
| 139 | const int16_t dequant_ptr, uint16_t *eob_ptr, |
| 140 | const qm_val_t *qm_ptr, |
| 141 | const qm_val_t *iqm_ptr) { |
| 142 | const int n_coeffs = 1024; |
| 143 | int eob = -1; |
| 144 | int dequant; |
| 145 | |
| 146 | memset(qcoeff_ptr, 0, n_coeffs * sizeof(*qcoeff_ptr)); |
| 147 | memset(dqcoeff_ptr, 0, n_coeffs * sizeof(*dqcoeff_ptr)); |
| 148 | |
| 149 | if (!skip_block) { |
| 150 | const int coeff = coeff_ptr[0]; |
| 151 | const int coeff_sign = (coeff >> 31); |
| 152 | const int abs_coeff = (coeff ^ coeff_sign) - coeff_sign; |
| 153 | const int64_t tmp = abs_coeff + ROUND_POWER_OF_TWO(round_ptr[0], 1); |
| 154 | const uint32_t abs_qcoeff = |
| 155 | (uint32_t)((tmp * qm_ptr[0] * quant) >> (15 + AOM_QM_BITS)); |
| 156 | qcoeff_ptr[0] = (tran_low_t)((abs_qcoeff ^ coeff_sign) - coeff_sign); |
| 157 | dequant = |
| 158 | (dequant_ptr * iqm_ptr[0] + (1 << (AOM_QM_BITS - 1))) >> AOM_QM_BITS; |
| 159 | dqcoeff_ptr[0] = (qcoeff_ptr[0] * dequant) / 2; |
| 160 | if (abs_qcoeff) eob = 0; |
| 161 | } |
| 162 | *eob_ptr = eob + 1; |
| 163 | } |
Debargha Mukherjee | 0e11912 | 2016-11-04 12:10:23 -0700 | [diff] [blame] | 164 | |
| 165 | #if CONFIG_TX64X64 |
| 166 | void aom_highbd_quantize_dc_64x64(const tran_low_t *coeff_ptr, int skip_block, |
| 167 | const int16_t *round_ptr, const int16_t quant, |
| 168 | tran_low_t *qcoeff_ptr, |
| 169 | tran_low_t *dqcoeff_ptr, |
| 170 | const int16_t dequant_ptr, uint16_t *eob_ptr, |
| 171 | const qm_val_t *qm_ptr, |
| 172 | const qm_val_t *iqm_ptr) { |
| 173 | const int n_coeffs = 1024; |
| 174 | int eob = -1; |
| 175 | int dequant; |
| 176 | |
| 177 | memset(qcoeff_ptr, 0, n_coeffs * sizeof(*qcoeff_ptr)); |
| 178 | memset(dqcoeff_ptr, 0, n_coeffs * sizeof(*dqcoeff_ptr)); |
| 179 | |
| 180 | if (!skip_block) { |
| 181 | const int coeff = coeff_ptr[0]; |
| 182 | const int coeff_sign = (coeff >> 31); |
| 183 | const int abs_coeff = (coeff ^ coeff_sign) - coeff_sign; |
| 184 | const int64_t tmp = abs_coeff + ROUND_POWER_OF_TWO(round_ptr[0], 2); |
| 185 | const uint32_t abs_qcoeff = |
| 186 | (uint32_t)((tmp * qm_ptr[0] * quant) >> (14 + AOM_QM_BITS)); |
| 187 | qcoeff_ptr[0] = (tran_low_t)((abs_qcoeff ^ coeff_sign) - coeff_sign); |
| 188 | dequant = |
| 189 | (dequant_ptr * iqm_ptr[0] + (1 << (AOM_QM_BITS - 1))) >> AOM_QM_BITS; |
| 190 | dqcoeff_ptr[0] = (qcoeff_ptr[0] * dequant) / 4; |
| 191 | if (abs_qcoeff) eob = 0; |
| 192 | } |
| 193 | *eob_ptr = eob + 1; |
| 194 | } |
| 195 | #endif // CONFIG_TX64X64 |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 196 | #endif |
| 197 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 198 | void aom_quantize_b_c(const tran_low_t *coeff_ptr, intptr_t n_coeffs, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 199 | int skip_block, const int16_t *zbin_ptr, |
| 200 | const int16_t *round_ptr, const int16_t *quant_ptr, |
| 201 | const int16_t *quant_shift_ptr, tran_low_t *qcoeff_ptr, |
| 202 | tran_low_t *dqcoeff_ptr, const int16_t *dequant_ptr, |
| 203 | uint16_t *eob_ptr, const int16_t *scan, |
| 204 | const int16_t *iscan, const qm_val_t *qm_ptr, |
| 205 | const qm_val_t *iqm_ptr) { |
| 206 | int i, non_zero_count = (int)n_coeffs, eob = -1; |
| 207 | const int zbins[2] = { zbin_ptr[0], zbin_ptr[1] }; |
| 208 | const int nzbins[2] = { zbins[0] * -1, zbins[1] * -1 }; |
| 209 | (void)iscan; |
| 210 | |
| 211 | memset(qcoeff_ptr, 0, n_coeffs * sizeof(*qcoeff_ptr)); |
| 212 | memset(dqcoeff_ptr, 0, n_coeffs * sizeof(*dqcoeff_ptr)); |
| 213 | |
| 214 | if (!skip_block) { |
| 215 | // Pre-scan pass |
| 216 | for (i = (int)n_coeffs - 1; i >= 0; i--) { |
| 217 | const int rc = scan[i]; |
| 218 | const qm_val_t wt = qm_ptr[rc]; |
| 219 | const int coeff = coeff_ptr[rc] * wt; |
| 220 | |
| 221 | if (coeff < (zbins[rc != 0] << AOM_QM_BITS) && |
| 222 | coeff > (nzbins[rc != 0] << AOM_QM_BITS)) |
| 223 | non_zero_count--; |
| 224 | else |
| 225 | break; |
| 226 | } |
| 227 | |
| 228 | // Quantization pass: All coefficients with index >= zero_flag are |
| 229 | // skippable. Note: zero_flag can be zero. |
| 230 | for (i = 0; i < non_zero_count; i++) { |
| 231 | const int rc = scan[i]; |
| 232 | const qm_val_t wt = qm_ptr[rc]; |
| 233 | const int coeff = coeff_ptr[rc]; |
| 234 | const int coeff_sign = (coeff >> 31); |
| 235 | const int abs_coeff = (coeff ^ coeff_sign) - coeff_sign; |
| 236 | int dequant; |
| 237 | |
| 238 | if (abs_coeff * wt >= (zbins[rc != 0] << AOM_QM_BITS)) { |
| 239 | int32_t tmp32; |
| 240 | int64_t tmp = |
| 241 | clamp(abs_coeff + round_ptr[rc != 0], INT16_MIN, INT16_MAX); |
| 242 | tmp = tmp * wt; |
| 243 | tmp32 = ((((tmp * quant_ptr[rc != 0]) >> 16) + tmp) * |
| 244 | quant_shift_ptr[rc != 0]) >> |
| 245 | (16 + AOM_QM_BITS); // quantization |
| 246 | dequant = |
| 247 | (dequant_ptr[rc != 0] * iqm_ptr[rc] + (1 << (AOM_QM_BITS - 1))) >> |
| 248 | AOM_QM_BITS; |
| 249 | qcoeff_ptr[rc] = (tmp32 ^ coeff_sign) - coeff_sign; |
| 250 | dqcoeff_ptr[rc] = qcoeff_ptr[rc] * dequant; |
| 251 | |
| 252 | if (tmp32) eob = i; |
| 253 | } |
| 254 | } |
| 255 | } |
| 256 | *eob_ptr = eob + 1; |
| 257 | } |
| 258 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 259 | #if CONFIG_AOM_HIGHBITDEPTH |
| 260 | void aom_highbd_quantize_b_c(const tran_low_t *coeff_ptr, intptr_t n_coeffs, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 261 | int skip_block, const int16_t *zbin_ptr, |
| 262 | const int16_t *round_ptr, const int16_t *quant_ptr, |
| 263 | const int16_t *quant_shift_ptr, |
| 264 | tran_low_t *qcoeff_ptr, tran_low_t *dqcoeff_ptr, |
| 265 | const int16_t *dequant_ptr, uint16_t *eob_ptr, |
| 266 | const int16_t *scan, const int16_t *iscan, |
| 267 | const qm_val_t *qm_ptr, const qm_val_t *iqm_ptr) { |
| 268 | int i, non_zero_count = (int)n_coeffs, eob = -1; |
| 269 | const int zbins[2] = { zbin_ptr[0], zbin_ptr[1] }; |
| 270 | const int nzbins[2] = { zbins[0] * -1, zbins[1] * -1 }; |
| 271 | int dequant; |
| 272 | (void)iscan; |
| 273 | |
| 274 | memset(qcoeff_ptr, 0, n_coeffs * sizeof(*qcoeff_ptr)); |
| 275 | memset(dqcoeff_ptr, 0, n_coeffs * sizeof(*dqcoeff_ptr)); |
| 276 | |
| 277 | if (!skip_block) { |
| 278 | // Pre-scan pass |
| 279 | for (i = (int)n_coeffs - 1; i >= 0; i--) { |
| 280 | const int rc = scan[i]; |
| 281 | const qm_val_t wt = qm_ptr[rc]; |
| 282 | const int coeff = coeff_ptr[rc] * wt; |
| 283 | |
| 284 | if (coeff < (zbins[rc != 0] << AOM_QM_BITS) && |
| 285 | coeff > (nzbins[rc != 0] << AOM_QM_BITS)) |
| 286 | non_zero_count--; |
| 287 | else |
| 288 | break; |
| 289 | } |
| 290 | |
| 291 | // Quantization pass: All coefficients with index >= zero_flag are |
| 292 | // skippable. Note: zero_flag can be zero. |
| 293 | for (i = 0; i < non_zero_count; i++) { |
| 294 | const int rc = scan[i]; |
| 295 | const int coeff = coeff_ptr[rc]; |
| 296 | const qm_val_t wt = qm_ptr[rc]; |
| 297 | const int coeff_sign = (coeff >> 31); |
| 298 | const int abs_coeff = (coeff ^ coeff_sign) - coeff_sign; |
| 299 | |
| 300 | if (abs_coeff * wt >= (zbins[rc != 0] << AOM_QM_BITS)) { |
| 301 | const int64_t tmp1 = abs_coeff + round_ptr[rc != 0]; |
| 302 | const int64_t tmpw = tmp1 * wt; |
| 303 | const int64_t tmp2 = ((tmpw * quant_ptr[rc != 0]) >> 16) + tmpw; |
| 304 | const uint32_t abs_qcoeff = |
| 305 | (uint32_t)((tmp2 * quant_shift_ptr[rc != 0]) >> (16 + AOM_QM_BITS)); |
| 306 | qcoeff_ptr[rc] = (tran_low_t)((abs_qcoeff ^ coeff_sign) - coeff_sign); |
| 307 | dequant = |
| 308 | (dequant_ptr[rc != 0] * iqm_ptr[rc] + (1 << (AOM_QM_BITS - 1))) >> |
| 309 | AOM_QM_BITS; |
| 310 | dqcoeff_ptr[rc] = qcoeff_ptr[rc] * dequant; |
| 311 | if (abs_qcoeff) eob = i; |
| 312 | } |
| 313 | } |
| 314 | } |
| 315 | *eob_ptr = eob + 1; |
| 316 | } |
| 317 | #endif |
| 318 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 319 | void aom_quantize_b_32x32_c(const tran_low_t *coeff_ptr, intptr_t n_coeffs, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 320 | int skip_block, const int16_t *zbin_ptr, |
| 321 | const int16_t *round_ptr, const int16_t *quant_ptr, |
| 322 | const int16_t *quant_shift_ptr, |
| 323 | tran_low_t *qcoeff_ptr, tran_low_t *dqcoeff_ptr, |
| 324 | const int16_t *dequant_ptr, uint16_t *eob_ptr, |
| 325 | const int16_t *scan, const int16_t *iscan, |
| 326 | const qm_val_t *qm_ptr, const qm_val_t *iqm_ptr) { |
| 327 | const int zbins[2] = { ROUND_POWER_OF_TWO(zbin_ptr[0], 1), |
| 328 | ROUND_POWER_OF_TWO(zbin_ptr[1], 1) }; |
| 329 | const int nzbins[2] = { zbins[0] * -1, zbins[1] * -1 }; |
| 330 | |
| 331 | int idx = 0; |
| 332 | int idx_arr[1024]; |
| 333 | int i, eob = -1; |
| 334 | int dequant; |
| 335 | (void)iscan; |
| 336 | |
| 337 | memset(qcoeff_ptr, 0, n_coeffs * sizeof(*qcoeff_ptr)); |
| 338 | memset(dqcoeff_ptr, 0, n_coeffs * sizeof(*dqcoeff_ptr)); |
| 339 | |
| 340 | if (!skip_block) { |
| 341 | // Pre-scan pass |
| 342 | for (i = 0; i < n_coeffs; i++) { |
| 343 | const int rc = scan[i]; |
| 344 | const qm_val_t wt = qm_ptr[rc]; |
| 345 | const int coeff = coeff_ptr[rc] * wt; |
| 346 | |
| 347 | // If the coefficient is out of the base ZBIN range, keep it for |
| 348 | // quantization. |
| 349 | if (coeff >= (zbins[rc != 0] << AOM_QM_BITS) || |
| 350 | coeff <= (nzbins[rc != 0] << AOM_QM_BITS)) |
| 351 | idx_arr[idx++] = i; |
| 352 | } |
| 353 | |
| 354 | // Quantization pass: only process the coefficients selected in |
| 355 | // pre-scan pass. Note: idx can be zero. |
| 356 | for (i = 0; i < idx; i++) { |
| 357 | const int rc = scan[idx_arr[i]]; |
| 358 | const int coeff = coeff_ptr[rc]; |
| 359 | const int coeff_sign = (coeff >> 31); |
| 360 | const qm_val_t wt = qm_ptr[rc]; |
| 361 | int64_t tmp; |
| 362 | int tmp32; |
| 363 | int abs_coeff = (coeff ^ coeff_sign) - coeff_sign; |
| 364 | abs_coeff += ROUND_POWER_OF_TWO(round_ptr[rc != 0], 1); |
| 365 | tmp = clamp(abs_coeff, INT16_MIN, INT16_MAX); |
| 366 | tmp = tmp * wt; |
| 367 | tmp32 = ((((tmp * quant_ptr[rc != 0]) >> 16) + tmp) * |
| 368 | quant_shift_ptr[rc != 0]) >> |
| 369 | (15 + AOM_QM_BITS); |
| 370 | |
| 371 | qcoeff_ptr[rc] = (tmp32 ^ coeff_sign) - coeff_sign; |
| 372 | dequant = |
| 373 | (dequant_ptr[rc != 0] * iqm_ptr[rc] + (1 << (AOM_QM_BITS - 1))) >> |
| 374 | AOM_QM_BITS; |
| 375 | dqcoeff_ptr[rc] = (qcoeff_ptr[rc] * dequant) / 2; |
| 376 | |
| 377 | if (tmp32) eob = idx_arr[i]; |
| 378 | } |
| 379 | } |
| 380 | *eob_ptr = eob + 1; |
| 381 | } |
| 382 | |
Debargha Mukherjee | 0e11912 | 2016-11-04 12:10:23 -0700 | [diff] [blame] | 383 | #if CONFIG_TX64X64 |
| 384 | void aom_quantize_b_64x64_c(const tran_low_t *coeff_ptr, intptr_t n_coeffs, |
| 385 | int skip_block, const int16_t *zbin_ptr, |
| 386 | const int16_t *round_ptr, const int16_t *quant_ptr, |
| 387 | const int16_t *quant_shift_ptr, |
| 388 | tran_low_t *qcoeff_ptr, tran_low_t *dqcoeff_ptr, |
| 389 | const int16_t *dequant_ptr, uint16_t *eob_ptr, |
| 390 | const int16_t *scan, const int16_t *iscan, |
| 391 | const qm_val_t *qm_ptr, const qm_val_t *iqm_ptr) { |
| 392 | const int zbins[2] = { ROUND_POWER_OF_TWO(zbin_ptr[0], 2), |
| 393 | ROUND_POWER_OF_TWO(zbin_ptr[1], 2) }; |
| 394 | const int nzbins[2] = { zbins[0] * -1, zbins[1] * -1 }; |
| 395 | |
| 396 | int idx = 0; |
| 397 | int idx_arr[4096]; |
| 398 | int i, eob = -1; |
| 399 | int dequant; |
| 400 | (void)iscan; |
| 401 | |
| 402 | memset(qcoeff_ptr, 0, n_coeffs * sizeof(*qcoeff_ptr)); |
| 403 | memset(dqcoeff_ptr, 0, n_coeffs * sizeof(*dqcoeff_ptr)); |
| 404 | |
| 405 | if (!skip_block) { |
| 406 | // Pre-scan pass |
| 407 | for (i = 0; i < n_coeffs; i++) { |
| 408 | const int rc = scan[i]; |
| 409 | const qm_val_t wt = qm_ptr[rc]; |
| 410 | const int coeff = coeff_ptr[rc] * wt; |
| 411 | |
| 412 | // If the coefficient is out of the base ZBIN range, keep it for |
| 413 | // quantization. |
| 414 | if (coeff >= (zbins[rc != 0] << AOM_QM_BITS) || |
| 415 | coeff <= (nzbins[rc != 0] << AOM_QM_BITS)) |
| 416 | idx_arr[idx++] = i; |
| 417 | } |
| 418 | |
| 419 | // Quantization pass: only process the coefficients selected in |
| 420 | // pre-scan pass. Note: idx can be zero. |
| 421 | for (i = 0; i < idx; i++) { |
| 422 | const int rc = scan[idx_arr[i]]; |
| 423 | const int coeff = coeff_ptr[rc]; |
| 424 | const int coeff_sign = (coeff >> 31); |
| 425 | const qm_val_t wt = qm_ptr[rc]; |
| 426 | int64_t tmp; |
| 427 | int tmp32; |
| 428 | int abs_coeff = (coeff ^ coeff_sign) - coeff_sign; |
| 429 | abs_coeff += ROUND_POWER_OF_TWO(round_ptr[rc != 0], 2); |
| 430 | tmp = clamp(abs_coeff, INT16_MIN, INT16_MAX); |
| 431 | tmp = tmp * wt; |
| 432 | tmp32 = ((((tmp * quant_ptr[rc != 0]) >> 16) + tmp) * |
| 433 | quant_shift_ptr[rc != 0]) >> |
| 434 | (14 + AOM_QM_BITS); |
| 435 | |
| 436 | qcoeff_ptr[rc] = (tmp32 ^ coeff_sign) - coeff_sign; |
| 437 | dequant = |
| 438 | (dequant_ptr[rc != 0] * iqm_ptr[rc] + (1 << (AOM_QM_BITS - 1))) >> |
| 439 | AOM_QM_BITS; |
| 440 | dqcoeff_ptr[rc] = (qcoeff_ptr[rc] * dequant) / 4; |
| 441 | |
| 442 | if (tmp32) eob = idx_arr[i]; |
| 443 | } |
| 444 | } |
| 445 | *eob_ptr = eob + 1; |
| 446 | } |
| 447 | #endif // CONFIG_TX64X64 |
| 448 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 449 | #if CONFIG_AOM_HIGHBITDEPTH |
| 450 | void aom_highbd_quantize_b_32x32_c( |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 451 | const tran_low_t *coeff_ptr, intptr_t n_coeffs, int skip_block, |
| 452 | const int16_t *zbin_ptr, const int16_t *round_ptr, const int16_t *quant_ptr, |
| 453 | const int16_t *quant_shift_ptr, tran_low_t *qcoeff_ptr, |
| 454 | tran_low_t *dqcoeff_ptr, const int16_t *dequant_ptr, uint16_t *eob_ptr, |
| 455 | const int16_t *scan, const int16_t *iscan, const qm_val_t *qm_ptr, |
| 456 | const qm_val_t *iqm_ptr) { |
| 457 | const int zbins[2] = { ROUND_POWER_OF_TWO(zbin_ptr[0], 1), |
| 458 | ROUND_POWER_OF_TWO(zbin_ptr[1], 1) }; |
| 459 | const int nzbins[2] = { zbins[0] * -1, zbins[1] * -1 }; |
| 460 | |
| 461 | int idx = 0; |
| 462 | int idx_arr[1024]; |
| 463 | int i, eob = -1; |
| 464 | int dequant; |
| 465 | (void)iscan; |
| 466 | |
| 467 | memset(qcoeff_ptr, 0, n_coeffs * sizeof(*qcoeff_ptr)); |
| 468 | memset(dqcoeff_ptr, 0, n_coeffs * sizeof(*dqcoeff_ptr)); |
| 469 | |
| 470 | if (!skip_block) { |
| 471 | // Pre-scan pass |
| 472 | for (i = 0; i < n_coeffs; i++) { |
| 473 | const int rc = scan[i]; |
| 474 | const qm_val_t wt = qm_ptr[rc]; |
| 475 | const int coeff = coeff_ptr[rc] * wt; |
| 476 | |
| 477 | // If the coefficient is out of the base ZBIN range, keep it for |
| 478 | // quantization. |
| 479 | if (coeff >= (zbins[rc != 0] << AOM_QM_BITS) || |
| 480 | coeff <= (nzbins[rc != 0] << AOM_QM_BITS)) |
| 481 | idx_arr[idx++] = i; |
| 482 | } |
| 483 | |
| 484 | // Quantization pass: only process the coefficients selected in |
| 485 | // pre-scan pass. Note: idx can be zero. |
| 486 | for (i = 0; i < idx; i++) { |
| 487 | const int rc = scan[idx_arr[i]]; |
| 488 | const int coeff = coeff_ptr[rc]; |
| 489 | const int coeff_sign = (coeff >> 31); |
| 490 | const qm_val_t wt = qm_ptr[rc]; |
| 491 | const int abs_coeff = (coeff ^ coeff_sign) - coeff_sign; |
| 492 | const int64_t tmp1 = |
| 493 | abs_coeff + ROUND_POWER_OF_TWO(round_ptr[rc != 0], 1); |
| 494 | const int64_t tmpw = tmp1 * wt; |
| 495 | const int64_t tmp2 = ((tmpw * quant_ptr[rc != 0]) >> 16) + tmpw; |
| 496 | const uint32_t abs_qcoeff = |
| 497 | (uint32_t)((tmp2 * quant_shift_ptr[rc != 0]) >> (15 + AOM_QM_BITS)); |
| 498 | qcoeff_ptr[rc] = (tran_low_t)((abs_qcoeff ^ coeff_sign) - coeff_sign); |
| 499 | dequant = |
| 500 | (dequant_ptr[rc != 0] * iqm_ptr[rc] + (1 << (AOM_QM_BITS - 1))) >> |
| 501 | AOM_QM_BITS; |
| 502 | dqcoeff_ptr[rc] = (qcoeff_ptr[rc] * dequant) / 2; |
| 503 | if (abs_qcoeff) eob = idx_arr[i]; |
| 504 | } |
| 505 | } |
| 506 | *eob_ptr = eob + 1; |
| 507 | } |
Debargha Mukherjee | 0e11912 | 2016-11-04 12:10:23 -0700 | [diff] [blame] | 508 | |
| 509 | #if CONFIG_TX64X64 |
| 510 | void aom_highbd_quantize_b_64x64_c( |
| 511 | const tran_low_t *coeff_ptr, intptr_t n_coeffs, int skip_block, |
| 512 | const int16_t *zbin_ptr, const int16_t *round_ptr, const int16_t *quant_ptr, |
| 513 | const int16_t *quant_shift_ptr, tran_low_t *qcoeff_ptr, |
| 514 | tran_low_t *dqcoeff_ptr, const int16_t *dequant_ptr, uint16_t *eob_ptr, |
| 515 | const int16_t *scan, const int16_t *iscan, const qm_val_t *qm_ptr, |
| 516 | const qm_val_t *iqm_ptr) { |
| 517 | const int zbins[2] = { ROUND_POWER_OF_TWO(zbin_ptr[0], 2), |
| 518 | ROUND_POWER_OF_TWO(zbin_ptr[1], 2) }; |
| 519 | const int nzbins[2] = { zbins[0] * -1, zbins[1] * -1 }; |
| 520 | |
| 521 | int idx = 0; |
| 522 | int idx_arr[4096]; |
| 523 | int i, eob = -1; |
| 524 | int dequant; |
| 525 | (void)iscan; |
| 526 | |
| 527 | memset(qcoeff_ptr, 0, n_coeffs * sizeof(*qcoeff_ptr)); |
| 528 | memset(dqcoeff_ptr, 0, n_coeffs * sizeof(*dqcoeff_ptr)); |
| 529 | |
| 530 | if (!skip_block) { |
| 531 | // Pre-scan pass |
| 532 | for (i = 0; i < n_coeffs; i++) { |
| 533 | const int rc = scan[i]; |
| 534 | const qm_val_t wt = qm_ptr[rc]; |
| 535 | const int coeff = coeff_ptr[rc] * wt; |
| 536 | |
| 537 | // If the coefficient is out of the base ZBIN range, keep it for |
| 538 | // quantization. |
| 539 | if (coeff >= (zbins[rc != 0] << AOM_QM_BITS) || |
| 540 | coeff <= (nzbins[rc != 0] << AOM_QM_BITS)) |
| 541 | idx_arr[idx++] = i; |
| 542 | } |
| 543 | |
| 544 | // Quantization pass: only process the coefficients selected in |
| 545 | // pre-scan pass. Note: idx can be zero. |
| 546 | for (i = 0; i < idx; i++) { |
| 547 | const int rc = scan[idx_arr[i]]; |
| 548 | const int coeff = coeff_ptr[rc]; |
| 549 | const int coeff_sign = (coeff >> 31); |
| 550 | const qm_val_t wt = qm_ptr[rc]; |
| 551 | const int abs_coeff = (coeff ^ coeff_sign) - coeff_sign; |
| 552 | const int64_t tmp1 = |
| 553 | abs_coeff + ROUND_POWER_OF_TWO(round_ptr[rc != 0], 2); |
| 554 | const int64_t tmpw = tmp1 * wt; |
| 555 | const int64_t tmp2 = ((tmpw * quant_ptr[rc != 0]) >> 16) + tmpw; |
| 556 | const uint32_t abs_qcoeff = |
| 557 | (uint32_t)((tmp2 * quant_shift_ptr[rc != 0]) >> (14 + AOM_QM_BITS)); |
| 558 | qcoeff_ptr[rc] = (tran_low_t)((abs_qcoeff ^ coeff_sign) - coeff_sign); |
| 559 | dequant = |
| 560 | (dequant_ptr[rc != 0] * iqm_ptr[rc] + (1 << (AOM_QM_BITS - 1))) >> |
| 561 | AOM_QM_BITS; |
| 562 | dqcoeff_ptr[rc] = (qcoeff_ptr[rc] * dequant) / 4; |
| 563 | if (abs_qcoeff) eob = idx_arr[i]; |
| 564 | } |
| 565 | } |
| 566 | *eob_ptr = eob + 1; |
| 567 | } |
| 568 | #endif // CONFIG_TX64X64 |
| 569 | #endif // CONFIG_AOM_HIGHBITDEPTH |
| 570 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 571 | #else |
Debargha Mukherjee | 0e11912 | 2016-11-04 12:10:23 -0700 | [diff] [blame] | 572 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 573 | void aom_quantize_dc(const tran_low_t *coeff_ptr, int n_coeffs, int skip_block, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 574 | const int16_t *round_ptr, const int16_t quant, |
| 575 | tran_low_t *qcoeff_ptr, tran_low_t *dqcoeff_ptr, |
| 576 | const int16_t dequant_ptr, uint16_t *eob_ptr) { |
| 577 | const int rc = 0; |
| 578 | const int coeff = coeff_ptr[rc]; |
| 579 | const int coeff_sign = (coeff >> 31); |
| 580 | const int abs_coeff = (coeff ^ coeff_sign) - coeff_sign; |
| 581 | int tmp, eob = -1; |
| 582 | |
| 583 | memset(qcoeff_ptr, 0, n_coeffs * sizeof(*qcoeff_ptr)); |
| 584 | memset(dqcoeff_ptr, 0, n_coeffs * sizeof(*dqcoeff_ptr)); |
| 585 | |
| 586 | if (!skip_block) { |
| 587 | tmp = clamp(abs_coeff + round_ptr[rc != 0], INT16_MIN, INT16_MAX); |
| 588 | tmp = (tmp * quant) >> 16; |
| 589 | qcoeff_ptr[rc] = (tmp ^ coeff_sign) - coeff_sign; |
| 590 | dqcoeff_ptr[rc] = qcoeff_ptr[rc] * dequant_ptr; |
| 591 | if (tmp) eob = 0; |
| 592 | } |
| 593 | *eob_ptr = eob + 1; |
| 594 | } |
| 595 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 596 | #if CONFIG_AOM_HIGHBITDEPTH |
| 597 | void aom_highbd_quantize_dc(const tran_low_t *coeff_ptr, int n_coeffs, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 598 | int skip_block, const int16_t *round_ptr, |
| 599 | const int16_t quant, tran_low_t *qcoeff_ptr, |
| 600 | tran_low_t *dqcoeff_ptr, const int16_t dequant_ptr, |
| 601 | uint16_t *eob_ptr) { |
| 602 | int eob = -1; |
| 603 | |
| 604 | memset(qcoeff_ptr, 0, n_coeffs * sizeof(*qcoeff_ptr)); |
| 605 | memset(dqcoeff_ptr, 0, n_coeffs * sizeof(*dqcoeff_ptr)); |
| 606 | |
| 607 | if (!skip_block) { |
| 608 | const int coeff = coeff_ptr[0]; |
| 609 | const int coeff_sign = (coeff >> 31); |
| 610 | const int abs_coeff = (coeff ^ coeff_sign) - coeff_sign; |
| 611 | const int64_t tmp = abs_coeff + round_ptr[0]; |
Yaowu Xu | 34b0ee6 | 2016-09-02 15:28:30 -0700 | [diff] [blame] | 612 | const uint32_t abs_qcoeff = (uint32_t)((tmp * quant) >> 16); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 613 | qcoeff_ptr[0] = (tran_low_t)((abs_qcoeff ^ coeff_sign) - coeff_sign); |
| 614 | dqcoeff_ptr[0] = qcoeff_ptr[0] * dequant_ptr; |
| 615 | if (abs_qcoeff) eob = 0; |
| 616 | } |
| 617 | *eob_ptr = eob + 1; |
| 618 | } |
| 619 | #endif |
| 620 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 621 | void aom_quantize_dc_32x32(const tran_low_t *coeff_ptr, int skip_block, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 622 | const int16_t *round_ptr, const int16_t quant, |
| 623 | tran_low_t *qcoeff_ptr, tran_low_t *dqcoeff_ptr, |
| 624 | const int16_t dequant_ptr, uint16_t *eob_ptr) { |
| 625 | const int n_coeffs = 1024; |
| 626 | const int rc = 0; |
| 627 | const int coeff = coeff_ptr[rc]; |
| 628 | const int coeff_sign = (coeff >> 31); |
| 629 | const int abs_coeff = (coeff ^ coeff_sign) - coeff_sign; |
| 630 | int tmp, eob = -1; |
| 631 | |
| 632 | memset(qcoeff_ptr, 0, n_coeffs * sizeof(*qcoeff_ptr)); |
| 633 | memset(dqcoeff_ptr, 0, n_coeffs * sizeof(*dqcoeff_ptr)); |
| 634 | |
| 635 | if (!skip_block) { |
| 636 | tmp = clamp(abs_coeff + ROUND_POWER_OF_TWO(round_ptr[rc != 0], 1), |
| 637 | INT16_MIN, INT16_MAX); |
| 638 | tmp = (tmp * quant) >> 15; |
| 639 | qcoeff_ptr[rc] = (tmp ^ coeff_sign) - coeff_sign; |
| 640 | dqcoeff_ptr[rc] = qcoeff_ptr[rc] * dequant_ptr / 2; |
| 641 | if (tmp) eob = 0; |
| 642 | } |
| 643 | *eob_ptr = eob + 1; |
| 644 | } |
| 645 | |
Debargha Mukherjee | 0e11912 | 2016-11-04 12:10:23 -0700 | [diff] [blame] | 646 | #if CONFIG_TX64X64 |
| 647 | void aom_quantize_dc_64x64(const tran_low_t *coeff_ptr, int skip_block, |
| 648 | const int16_t *round_ptr, const int16_t quant, |
| 649 | tran_low_t *qcoeff_ptr, tran_low_t *dqcoeff_ptr, |
| 650 | const int16_t dequant_ptr, uint16_t *eob_ptr) { |
| 651 | const int n_coeffs = 4096; |
| 652 | const int rc = 0; |
| 653 | const int coeff = coeff_ptr[rc]; |
| 654 | const int coeff_sign = (coeff >> 31); |
| 655 | const int abs_coeff = (coeff ^ coeff_sign) - coeff_sign; |
| 656 | int tmp, eob = -1; |
| 657 | |
| 658 | memset(qcoeff_ptr, 0, n_coeffs * sizeof(*qcoeff_ptr)); |
| 659 | memset(dqcoeff_ptr, 0, n_coeffs * sizeof(*dqcoeff_ptr)); |
| 660 | |
| 661 | if (!skip_block) { |
| 662 | tmp = clamp(abs_coeff + ROUND_POWER_OF_TWO(round_ptr[rc != 0], 2), |
| 663 | INT16_MIN, INT16_MAX); |
| 664 | tmp = (tmp * quant) >> 14; |
| 665 | qcoeff_ptr[rc] = (tmp ^ coeff_sign) - coeff_sign; |
| 666 | dqcoeff_ptr[rc] = qcoeff_ptr[rc] * dequant_ptr / 4; |
| 667 | if (tmp) eob = 0; |
| 668 | } |
| 669 | *eob_ptr = eob + 1; |
| 670 | } |
| 671 | #endif // CONFIG_TX64X64 |
| 672 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 673 | #if CONFIG_AOM_HIGHBITDEPTH |
| 674 | void aom_highbd_quantize_dc_32x32(const tran_low_t *coeff_ptr, int skip_block, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 675 | const int16_t *round_ptr, const int16_t quant, |
| 676 | tran_low_t *qcoeff_ptr, |
| 677 | tran_low_t *dqcoeff_ptr, |
| 678 | const int16_t dequant_ptr, |
| 679 | uint16_t *eob_ptr) { |
| 680 | const int n_coeffs = 1024; |
| 681 | int eob = -1; |
| 682 | |
| 683 | memset(qcoeff_ptr, 0, n_coeffs * sizeof(*qcoeff_ptr)); |
| 684 | memset(dqcoeff_ptr, 0, n_coeffs * sizeof(*dqcoeff_ptr)); |
| 685 | |
| 686 | if (!skip_block) { |
| 687 | const int coeff = coeff_ptr[0]; |
| 688 | const int coeff_sign = (coeff >> 31); |
| 689 | const int abs_coeff = (coeff ^ coeff_sign) - coeff_sign; |
| 690 | const int64_t tmp = abs_coeff + ROUND_POWER_OF_TWO(round_ptr[0], 1); |
Yaowu Xu | 34b0ee6 | 2016-09-02 15:28:30 -0700 | [diff] [blame] | 691 | const uint32_t abs_qcoeff = (uint32_t)((tmp * quant) >> 15); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 692 | qcoeff_ptr[0] = (tran_low_t)((abs_qcoeff ^ coeff_sign) - coeff_sign); |
| 693 | dqcoeff_ptr[0] = qcoeff_ptr[0] * dequant_ptr / 2; |
| 694 | if (abs_qcoeff) eob = 0; |
| 695 | } |
| 696 | *eob_ptr = eob + 1; |
| 697 | } |
Debargha Mukherjee | 0e11912 | 2016-11-04 12:10:23 -0700 | [diff] [blame] | 698 | |
| 699 | #if CONFIG_TX64X64 |
| 700 | void aom_highbd_quantize_dc_64x64(const tran_low_t *coeff_ptr, int skip_block, |
| 701 | const int16_t *round_ptr, const int16_t quant, |
| 702 | tran_low_t *qcoeff_ptr, |
| 703 | tran_low_t *dqcoeff_ptr, |
| 704 | const int16_t dequant_ptr, |
| 705 | uint16_t *eob_ptr) { |
| 706 | const int n_coeffs = 4096; |
| 707 | int eob = -1; |
| 708 | |
| 709 | memset(qcoeff_ptr, 0, n_coeffs * sizeof(*qcoeff_ptr)); |
| 710 | memset(dqcoeff_ptr, 0, n_coeffs * sizeof(*dqcoeff_ptr)); |
| 711 | |
| 712 | if (!skip_block) { |
| 713 | const int coeff = coeff_ptr[0]; |
| 714 | const int coeff_sign = (coeff >> 31); |
| 715 | const int abs_coeff = (coeff ^ coeff_sign) - coeff_sign; |
| 716 | const int64_t tmp = abs_coeff + ROUND_POWER_OF_TWO(round_ptr[0], 2); |
| 717 | const uint32_t abs_qcoeff = (uint32_t)((tmp * quant) >> 14); |
| 718 | qcoeff_ptr[0] = (tran_low_t)((abs_qcoeff ^ coeff_sign) - coeff_sign); |
| 719 | dqcoeff_ptr[0] = qcoeff_ptr[0] * dequant_ptr / 4; |
| 720 | if (abs_qcoeff) eob = 0; |
| 721 | } |
| 722 | *eob_ptr = eob + 1; |
| 723 | } |
| 724 | #endif // CONFIG_TX64X64 |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 725 | #endif |
| 726 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 727 | void aom_quantize_b_c(const tran_low_t *coeff_ptr, intptr_t n_coeffs, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 728 | int skip_block, const int16_t *zbin_ptr, |
| 729 | const int16_t *round_ptr, const int16_t *quant_ptr, |
| 730 | const int16_t *quant_shift_ptr, tran_low_t *qcoeff_ptr, |
| 731 | tran_low_t *dqcoeff_ptr, const int16_t *dequant_ptr, |
| 732 | uint16_t *eob_ptr, const int16_t *scan, |
| 733 | const int16_t *iscan) { |
| 734 | int i, non_zero_count = (int)n_coeffs, eob = -1; |
| 735 | const int zbins[2] = { zbin_ptr[0], zbin_ptr[1] }; |
| 736 | const int nzbins[2] = { zbins[0] * -1, zbins[1] * -1 }; |
| 737 | (void)iscan; |
| 738 | |
| 739 | memset(qcoeff_ptr, 0, n_coeffs * sizeof(*qcoeff_ptr)); |
| 740 | memset(dqcoeff_ptr, 0, n_coeffs * sizeof(*dqcoeff_ptr)); |
| 741 | |
| 742 | if (!skip_block) { |
| 743 | // Pre-scan pass |
| 744 | for (i = (int)n_coeffs - 1; i >= 0; i--) { |
| 745 | const int rc = scan[i]; |
| 746 | const int coeff = coeff_ptr[rc]; |
| 747 | |
| 748 | if (coeff < zbins[rc != 0] && coeff > nzbins[rc != 0]) |
| 749 | non_zero_count--; |
| 750 | else |
| 751 | break; |
| 752 | } |
| 753 | |
| 754 | // Quantization pass: All coefficients with index >= zero_flag are |
| 755 | // skippable. Note: zero_flag can be zero. |
| 756 | for (i = 0; i < non_zero_count; i++) { |
| 757 | const int rc = scan[i]; |
| 758 | const int coeff = coeff_ptr[rc]; |
| 759 | const int coeff_sign = (coeff >> 31); |
| 760 | const int abs_coeff = (coeff ^ coeff_sign) - coeff_sign; |
| 761 | |
| 762 | if (abs_coeff >= zbins[rc != 0]) { |
| 763 | int tmp = clamp(abs_coeff + round_ptr[rc != 0], INT16_MIN, INT16_MAX); |
| 764 | tmp = ((((tmp * quant_ptr[rc != 0]) >> 16) + tmp) * |
| 765 | quant_shift_ptr[rc != 0]) >> |
| 766 | 16; // quantization |
| 767 | qcoeff_ptr[rc] = (tmp ^ coeff_sign) - coeff_sign; |
| 768 | dqcoeff_ptr[rc] = qcoeff_ptr[rc] * dequant_ptr[rc != 0]; |
| 769 | |
| 770 | if (tmp) eob = i; |
| 771 | } |
| 772 | } |
| 773 | } |
| 774 | *eob_ptr = eob + 1; |
| 775 | } |
| 776 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 777 | #if CONFIG_AOM_HIGHBITDEPTH |
| 778 | void aom_highbd_quantize_b_c(const tran_low_t *coeff_ptr, intptr_t n_coeffs, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 779 | int skip_block, const int16_t *zbin_ptr, |
| 780 | const int16_t *round_ptr, const int16_t *quant_ptr, |
| 781 | const int16_t *quant_shift_ptr, |
| 782 | tran_low_t *qcoeff_ptr, tran_low_t *dqcoeff_ptr, |
| 783 | const int16_t *dequant_ptr, uint16_t *eob_ptr, |
| 784 | const int16_t *scan, const int16_t *iscan) { |
| 785 | int i, non_zero_count = (int)n_coeffs, eob = -1; |
| 786 | const int zbins[2] = { zbin_ptr[0], zbin_ptr[1] }; |
| 787 | const int nzbins[2] = { zbins[0] * -1, zbins[1] * -1 }; |
| 788 | (void)iscan; |
| 789 | |
| 790 | memset(qcoeff_ptr, 0, n_coeffs * sizeof(*qcoeff_ptr)); |
| 791 | memset(dqcoeff_ptr, 0, n_coeffs * sizeof(*dqcoeff_ptr)); |
| 792 | |
| 793 | if (!skip_block) { |
| 794 | // Pre-scan pass |
| 795 | for (i = (int)n_coeffs - 1; i >= 0; i--) { |
| 796 | const int rc = scan[i]; |
| 797 | const int coeff = coeff_ptr[rc]; |
| 798 | |
| 799 | if (coeff < zbins[rc != 0] && coeff > nzbins[rc != 0]) |
| 800 | non_zero_count--; |
| 801 | else |
| 802 | break; |
| 803 | } |
| 804 | |
| 805 | // Quantization pass: All coefficients with index >= zero_flag are |
| 806 | // skippable. Note: zero_flag can be zero. |
| 807 | for (i = 0; i < non_zero_count; i++) { |
| 808 | const int rc = scan[i]; |
| 809 | const int coeff = coeff_ptr[rc]; |
| 810 | const int coeff_sign = (coeff >> 31); |
| 811 | const int abs_coeff = (coeff ^ coeff_sign) - coeff_sign; |
| 812 | |
| 813 | if (abs_coeff >= zbins[rc != 0]) { |
| 814 | const int64_t tmp1 = abs_coeff + round_ptr[rc != 0]; |
| 815 | const int64_t tmp2 = ((tmp1 * quant_ptr[rc != 0]) >> 16) + tmp1; |
| 816 | const uint32_t abs_qcoeff = |
| 817 | (uint32_t)((tmp2 * quant_shift_ptr[rc != 0]) >> 16); |
| 818 | qcoeff_ptr[rc] = (tran_low_t)((abs_qcoeff ^ coeff_sign) - coeff_sign); |
| 819 | dqcoeff_ptr[rc] = qcoeff_ptr[rc] * dequant_ptr[rc != 0]; |
| 820 | if (abs_qcoeff) eob = i; |
| 821 | } |
| 822 | } |
| 823 | } |
| 824 | *eob_ptr = eob + 1; |
| 825 | } |
| 826 | #endif |
| 827 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 828 | void aom_quantize_b_32x32_c(const tran_low_t *coeff_ptr, intptr_t n_coeffs, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 829 | int skip_block, const int16_t *zbin_ptr, |
| 830 | const int16_t *round_ptr, const int16_t *quant_ptr, |
| 831 | const int16_t *quant_shift_ptr, |
| 832 | tran_low_t *qcoeff_ptr, tran_low_t *dqcoeff_ptr, |
| 833 | const int16_t *dequant_ptr, uint16_t *eob_ptr, |
| 834 | const int16_t *scan, const int16_t *iscan) { |
| 835 | const int zbins[2] = { ROUND_POWER_OF_TWO(zbin_ptr[0], 1), |
| 836 | ROUND_POWER_OF_TWO(zbin_ptr[1], 1) }; |
| 837 | const int nzbins[2] = { zbins[0] * -1, zbins[1] * -1 }; |
| 838 | |
| 839 | int idx = 0; |
| 840 | int idx_arr[1024]; |
| 841 | int i, eob = -1; |
| 842 | (void)iscan; |
| 843 | |
| 844 | memset(qcoeff_ptr, 0, n_coeffs * sizeof(*qcoeff_ptr)); |
| 845 | memset(dqcoeff_ptr, 0, n_coeffs * sizeof(*dqcoeff_ptr)); |
| 846 | |
| 847 | if (!skip_block) { |
| 848 | // Pre-scan pass |
| 849 | for (i = 0; i < n_coeffs; i++) { |
| 850 | const int rc = scan[i]; |
| 851 | const int coeff = coeff_ptr[rc]; |
| 852 | |
| 853 | // If the coefficient is out of the base ZBIN range, keep it for |
| 854 | // quantization. |
| 855 | if (coeff >= zbins[rc != 0] || coeff <= nzbins[rc != 0]) |
| 856 | idx_arr[idx++] = i; |
| 857 | } |
| 858 | |
| 859 | // Quantization pass: only process the coefficients selected in |
| 860 | // pre-scan pass. Note: idx can be zero. |
| 861 | for (i = 0; i < idx; i++) { |
| 862 | const int rc = scan[idx_arr[i]]; |
| 863 | const int coeff = coeff_ptr[rc]; |
| 864 | const int coeff_sign = (coeff >> 31); |
| 865 | int tmp; |
| 866 | int abs_coeff = (coeff ^ coeff_sign) - coeff_sign; |
| 867 | abs_coeff += ROUND_POWER_OF_TWO(round_ptr[rc != 0], 1); |
| 868 | abs_coeff = clamp(abs_coeff, INT16_MIN, INT16_MAX); |
| 869 | tmp = ((((abs_coeff * quant_ptr[rc != 0]) >> 16) + abs_coeff) * |
| 870 | quant_shift_ptr[rc != 0]) >> |
| 871 | 15; |
| 872 | |
| 873 | qcoeff_ptr[rc] = (tmp ^ coeff_sign) - coeff_sign; |
| 874 | dqcoeff_ptr[rc] = qcoeff_ptr[rc] * dequant_ptr[rc != 0] / 2; |
| 875 | |
| 876 | if (tmp) eob = idx_arr[i]; |
| 877 | } |
| 878 | } |
| 879 | *eob_ptr = eob + 1; |
| 880 | } |
| 881 | |
Debargha Mukherjee | 0e11912 | 2016-11-04 12:10:23 -0700 | [diff] [blame] | 882 | #if CONFIG_TX64X64 |
| 883 | void aom_quantize_b_64x64_c(const tran_low_t *coeff_ptr, intptr_t n_coeffs, |
| 884 | int skip_block, const int16_t *zbin_ptr, |
| 885 | const int16_t *round_ptr, const int16_t *quant_ptr, |
| 886 | const int16_t *quant_shift_ptr, |
| 887 | tran_low_t *qcoeff_ptr, tran_low_t *dqcoeff_ptr, |
| 888 | const int16_t *dequant_ptr, uint16_t *eob_ptr, |
| 889 | const int16_t *scan, const int16_t *iscan) { |
| 890 | const int zbins[2] = { ROUND_POWER_OF_TWO(zbin_ptr[0], 2), |
| 891 | ROUND_POWER_OF_TWO(zbin_ptr[1], 2) }; |
| 892 | const int nzbins[2] = { zbins[0] * -1, zbins[1] * -1 }; |
| 893 | |
| 894 | int idx = 0; |
| 895 | int idx_arr[4096]; |
| 896 | int i, eob = -1; |
| 897 | (void)iscan; |
| 898 | |
| 899 | memset(qcoeff_ptr, 0, n_coeffs * sizeof(*qcoeff_ptr)); |
| 900 | memset(dqcoeff_ptr, 0, n_coeffs * sizeof(*dqcoeff_ptr)); |
| 901 | |
| 902 | if (!skip_block) { |
| 903 | // Pre-scan pass |
| 904 | for (i = 0; i < n_coeffs; i++) { |
| 905 | const int rc = scan[i]; |
| 906 | const int coeff = coeff_ptr[rc]; |
| 907 | |
| 908 | // If the coefficient is out of the base ZBIN range, keep it for |
| 909 | // quantization. |
| 910 | if (coeff >= zbins[rc != 0] || coeff <= nzbins[rc != 0]) |
| 911 | idx_arr[idx++] = i; |
| 912 | } |
| 913 | |
| 914 | // Quantization pass: only process the coefficients selected in |
| 915 | // pre-scan pass. Note: idx can be zero. |
| 916 | for (i = 0; i < idx; i++) { |
| 917 | const int rc = scan[idx_arr[i]]; |
| 918 | const int coeff = coeff_ptr[rc]; |
| 919 | const int coeff_sign = (coeff >> 31); |
| 920 | int tmp; |
| 921 | int abs_coeff = (coeff ^ coeff_sign) - coeff_sign; |
| 922 | abs_coeff += ROUND_POWER_OF_TWO(round_ptr[rc != 0], 2); |
| 923 | abs_coeff = clamp(abs_coeff, INT16_MIN, INT16_MAX); |
| 924 | tmp = ((((abs_coeff * quant_ptr[rc != 0]) >> 16) + abs_coeff) * |
| 925 | quant_shift_ptr[rc != 0]) >> |
| 926 | 14; |
| 927 | |
| 928 | qcoeff_ptr[rc] = (tmp ^ coeff_sign) - coeff_sign; |
| 929 | dqcoeff_ptr[rc] = qcoeff_ptr[rc] * dequant_ptr[rc != 0] / 4; |
| 930 | |
| 931 | if (tmp) eob = idx_arr[i]; |
| 932 | } |
| 933 | } |
| 934 | *eob_ptr = eob + 1; |
| 935 | } |
| 936 | #endif // CONFIG_TX64X64 |
| 937 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 938 | #if CONFIG_AOM_HIGHBITDEPTH |
| 939 | void aom_highbd_quantize_b_32x32_c( |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 940 | const tran_low_t *coeff_ptr, intptr_t n_coeffs, int skip_block, |
| 941 | const int16_t *zbin_ptr, const int16_t *round_ptr, const int16_t *quant_ptr, |
| 942 | const int16_t *quant_shift_ptr, tran_low_t *qcoeff_ptr, |
| 943 | tran_low_t *dqcoeff_ptr, const int16_t *dequant_ptr, uint16_t *eob_ptr, |
| 944 | const int16_t *scan, const int16_t *iscan) { |
| 945 | const int zbins[2] = { ROUND_POWER_OF_TWO(zbin_ptr[0], 1), |
| 946 | ROUND_POWER_OF_TWO(zbin_ptr[1], 1) }; |
| 947 | const int nzbins[2] = { zbins[0] * -1, zbins[1] * -1 }; |
| 948 | |
| 949 | int idx = 0; |
| 950 | int idx_arr[1024]; |
| 951 | int i, eob = -1; |
| 952 | (void)iscan; |
| 953 | |
| 954 | memset(qcoeff_ptr, 0, n_coeffs * sizeof(*qcoeff_ptr)); |
| 955 | memset(dqcoeff_ptr, 0, n_coeffs * sizeof(*dqcoeff_ptr)); |
| 956 | |
| 957 | if (!skip_block) { |
| 958 | // Pre-scan pass |
| 959 | for (i = 0; i < n_coeffs; i++) { |
| 960 | const int rc = scan[i]; |
| 961 | const int coeff = coeff_ptr[rc]; |
| 962 | |
| 963 | // If the coefficient is out of the base ZBIN range, keep it for |
| 964 | // quantization. |
| 965 | if (coeff >= zbins[rc != 0] || coeff <= nzbins[rc != 0]) |
| 966 | idx_arr[idx++] = i; |
| 967 | } |
| 968 | |
| 969 | // Quantization pass: only process the coefficients selected in |
| 970 | // pre-scan pass. Note: idx can be zero. |
| 971 | for (i = 0; i < idx; i++) { |
| 972 | const int rc = scan[idx_arr[i]]; |
| 973 | const int coeff = coeff_ptr[rc]; |
| 974 | const int coeff_sign = (coeff >> 31); |
| 975 | const int abs_coeff = (coeff ^ coeff_sign) - coeff_sign; |
| 976 | const int64_t tmp1 = |
| 977 | abs_coeff + ROUND_POWER_OF_TWO(round_ptr[rc != 0], 1); |
| 978 | const int64_t tmp2 = ((tmp1 * quant_ptr[rc != 0]) >> 16) + tmp1; |
| 979 | const uint32_t abs_qcoeff = |
| 980 | (uint32_t)((tmp2 * quant_shift_ptr[rc != 0]) >> 15); |
| 981 | qcoeff_ptr[rc] = (tran_low_t)((abs_qcoeff ^ coeff_sign) - coeff_sign); |
| 982 | dqcoeff_ptr[rc] = qcoeff_ptr[rc] * dequant_ptr[rc != 0] / 2; |
| 983 | if (abs_qcoeff) eob = idx_arr[i]; |
| 984 | } |
| 985 | } |
| 986 | *eob_ptr = eob + 1; |
| 987 | } |
Debargha Mukherjee | 0e11912 | 2016-11-04 12:10:23 -0700 | [diff] [blame] | 988 | |
| 989 | #if CONFIG_TX64X64 |
| 990 | void aom_highbd_quantize_b_64x64_c( |
| 991 | const tran_low_t *coeff_ptr, intptr_t n_coeffs, int skip_block, |
| 992 | const int16_t *zbin_ptr, const int16_t *round_ptr, const int16_t *quant_ptr, |
| 993 | const int16_t *quant_shift_ptr, tran_low_t *qcoeff_ptr, |
| 994 | tran_low_t *dqcoeff_ptr, const int16_t *dequant_ptr, uint16_t *eob_ptr, |
| 995 | const int16_t *scan, const int16_t *iscan) { |
| 996 | const int zbins[2] = { ROUND_POWER_OF_TWO(zbin_ptr[0], 2), |
| 997 | ROUND_POWER_OF_TWO(zbin_ptr[1], 2) }; |
| 998 | const int nzbins[2] = { zbins[0] * -1, zbins[1] * -1 }; |
| 999 | |
| 1000 | int idx = 0; |
| 1001 | int idx_arr[4096]; |
| 1002 | int i, eob = -1; |
| 1003 | (void)iscan; |
| 1004 | |
| 1005 | memset(qcoeff_ptr, 0, n_coeffs * sizeof(*qcoeff_ptr)); |
| 1006 | memset(dqcoeff_ptr, 0, n_coeffs * sizeof(*dqcoeff_ptr)); |
| 1007 | |
| 1008 | if (!skip_block) { |
| 1009 | // Pre-scan pass |
| 1010 | for (i = 0; i < n_coeffs; i++) { |
| 1011 | const int rc = scan[i]; |
| 1012 | const int coeff = coeff_ptr[rc]; |
| 1013 | |
| 1014 | // If the coefficient is out of the base ZBIN range, keep it for |
| 1015 | // quantization. |
| 1016 | if (coeff >= zbins[rc != 0] || coeff <= nzbins[rc != 0]) |
| 1017 | idx_arr[idx++] = i; |
| 1018 | } |
| 1019 | |
| 1020 | // Quantization pass: only process the coefficients selected in |
| 1021 | // pre-scan pass. Note: idx can be zero. |
| 1022 | for (i = 0; i < idx; i++) { |
| 1023 | const int rc = scan[idx_arr[i]]; |
| 1024 | const int coeff = coeff_ptr[rc]; |
| 1025 | const int coeff_sign = (coeff >> 31); |
| 1026 | const int abs_coeff = (coeff ^ coeff_sign) - coeff_sign; |
| 1027 | const int64_t tmp1 = |
| 1028 | abs_coeff + ROUND_POWER_OF_TWO(round_ptr[rc != 0], 2); |
| 1029 | const int64_t tmp2 = ((tmp1 * quant_ptr[rc != 0]) >> 16) + tmp1; |
| 1030 | const uint32_t abs_qcoeff = |
| 1031 | (uint32_t)((tmp2 * quant_shift_ptr[rc != 0]) >> 14); |
| 1032 | qcoeff_ptr[rc] = (tran_low_t)((abs_qcoeff ^ coeff_sign) - coeff_sign); |
| 1033 | dqcoeff_ptr[rc] = qcoeff_ptr[rc] * dequant_ptr[rc != 0] / 4; |
| 1034 | if (abs_qcoeff) eob = idx_arr[i]; |
| 1035 | } |
| 1036 | } |
| 1037 | *eob_ptr = eob + 1; |
| 1038 | } |
| 1039 | #endif // CONFIG_TX64X64 |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1040 | #endif |
| 1041 | #endif |