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" |
Aniket Dhok | 64a5e40 | 2019-03-19 20:15:29 +0530 | [diff] [blame] | 14 | #include "av1/encoder/av1_quantize.h" |
Debargha Mukherjee | b52c5ce | 2019-03-06 11:39:31 -0800 | [diff] [blame] | 15 | |
Yaowu Xu | bbaa966 | 2019-05-06 11:17:03 -0700 | [diff] [blame] | 16 | void aom_quantize_b_adaptive_helper_c( |
Sarah Parker | eec52bb | 2019-01-17 15:04:35 -0800 | [diff] [blame] | 17 | const tran_low_t *coeff_ptr, intptr_t n_coeffs, const int16_t *zbin_ptr, |
| 18 | const int16_t *round_ptr, const int16_t *quant_ptr, |
| 19 | const int16_t *quant_shift_ptr, tran_low_t *qcoeff_ptr, |
| 20 | tran_low_t *dqcoeff_ptr, const int16_t *dequant_ptr, uint16_t *eob_ptr, |
| 21 | const int16_t *scan, 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 | |
Debargha Mukherjee | b52c5ce | 2019-03-06 11:39:31 -0800 | [diff] [blame] | 32 | int prescan_add[2]; |
| 33 | for (i = 0; i < 2; ++i) |
| 34 | prescan_add[i] = ROUND_POWER_OF_TWO(dequant_ptr[i] * EOB_FACTOR, 7); |
| 35 | |
Sarah Parker | eec52bb | 2019-01-17 15:04:35 -0800 | [diff] [blame] | 36 | // Pre-scan pass |
| 37 | for (i = (int)n_coeffs - 1; i >= 0; i--) { |
| 38 | const int rc = scan[i]; |
| 39 | const qm_val_t wt = qm_ptr != NULL ? qm_ptr[rc] : (1 << AOM_QM_BITS); |
| 40 | const int coeff = coeff_ptr[rc] * wt; |
Debargha Mukherjee | b52c5ce | 2019-03-06 11:39:31 -0800 | [diff] [blame] | 41 | const int prescan_add_val = prescan_add[rc != 0]; |
| 42 | if (coeff < (zbins[rc != 0] * (1 << AOM_QM_BITS) + prescan_add_val) && |
| 43 | coeff > (nzbins[rc != 0] * (1 << AOM_QM_BITS) - prescan_add_val)) |
Sarah Parker | eec52bb | 2019-01-17 15:04:35 -0800 | [diff] [blame] | 44 | non_zero_count--; |
| 45 | else |
| 46 | break; |
| 47 | } |
| 48 | |
| 49 | // Quantization pass: All coefficients with index >= zero_flag are |
| 50 | // skippable. Note: zero_flag can be zero. |
Debargha Mukherjee | b52c5ce | 2019-03-06 11:39:31 -0800 | [diff] [blame] | 51 | #if SKIP_EOB_FACTOR_ADJUST |
| 52 | int first = -1; |
| 53 | #endif // SKIP_EOB_FACTOR_ADJUST |
Sarah Parker | eec52bb | 2019-01-17 15:04:35 -0800 | [diff] [blame] | 54 | for (i = 0; i < non_zero_count; i++) { |
| 55 | const int rc = scan[i]; |
| 56 | const int coeff = coeff_ptr[rc]; |
| 57 | const int coeff_sign = (coeff >> 31); |
| 58 | const int abs_coeff = (coeff ^ coeff_sign) - coeff_sign; |
| 59 | int tmp32; |
| 60 | |
| 61 | const qm_val_t wt = qm_ptr != NULL ? qm_ptr[rc] : (1 << AOM_QM_BITS); |
| 62 | if (abs_coeff * wt >= (zbins[rc != 0] << AOM_QM_BITS)) { |
| 63 | int64_t tmp = |
| 64 | clamp(abs_coeff + ROUND_POWER_OF_TWO(round_ptr[rc != 0], log_scale), |
| 65 | INT16_MIN, INT16_MAX); |
| 66 | tmp *= wt; |
| 67 | tmp32 = (int)(((((tmp * quant_ptr[rc != 0]) >> 16) + tmp) * |
| 68 | quant_shift_ptr[rc != 0]) >> |
| 69 | (16 - log_scale + AOM_QM_BITS)); // quantization |
| 70 | qcoeff_ptr[rc] = (tmp32 ^ coeff_sign) - coeff_sign; |
| 71 | const int iwt = iqm_ptr != NULL ? iqm_ptr[rc] : (1 << AOM_QM_BITS); |
| 72 | const int dequant = |
| 73 | (dequant_ptr[rc != 0] * iwt + (1 << (AOM_QM_BITS - 1))) >> |
| 74 | AOM_QM_BITS; |
| 75 | const tran_low_t abs_dqcoeff = (tmp32 * dequant) >> log_scale; |
| 76 | dqcoeff_ptr[rc] = (tran_low_t)((abs_dqcoeff ^ coeff_sign) - coeff_sign); |
| 77 | |
Debargha Mukherjee | b52c5ce | 2019-03-06 11:39:31 -0800 | [diff] [blame] | 78 | if (tmp32) { |
| 79 | eob = i; |
| 80 | #if SKIP_EOB_FACTOR_ADJUST |
| 81 | if (first == -1) first = i; |
| 82 | #endif // SKIP_EOB_FACTOR_ADJUST |
| 83 | } |
Sarah Parker | eec52bb | 2019-01-17 15:04:35 -0800 | [diff] [blame] | 84 | } |
| 85 | } |
Debargha Mukherjee | b52c5ce | 2019-03-06 11:39:31 -0800 | [diff] [blame] | 86 | #if SKIP_EOB_FACTOR_ADJUST |
| 87 | if (eob >= 0 && first == eob) { |
| 88 | const int rc = scan[eob]; |
| 89 | if (qcoeff_ptr[rc] == 1 || qcoeff_ptr[rc] == -1) { |
| 90 | const qm_val_t wt = qm_ptr != NULL ? qm_ptr[rc] : (1 << AOM_QM_BITS); |
| 91 | const int coeff = coeff_ptr[rc] * wt; |
| 92 | const int factor = EOB_FACTOR + SKIP_EOB_FACTOR_ADJUST; |
| 93 | const int prescan_add_val = |
| 94 | ROUND_POWER_OF_TWO(dequant_ptr[rc != 0] * factor, 7); |
| 95 | if (coeff < (zbins[rc != 0] * (1 << AOM_QM_BITS) + prescan_add_val) && |
| 96 | coeff > (nzbins[rc != 0] * (1 << AOM_QM_BITS) - prescan_add_val)) { |
| 97 | qcoeff_ptr[rc] = 0; |
| 98 | dqcoeff_ptr[rc] = 0; |
| 99 | eob = -1; |
| 100 | } |
| 101 | } |
| 102 | } |
| 103 | #endif // SKIP_EOB_FACTOR_ADJUST |
Sarah Parker | eec52bb | 2019-01-17 15:04:35 -0800 | [diff] [blame] | 104 | *eob_ptr = eob + 1; |
| 105 | } |
Sarah Parker | e343b8c | 2019-01-22 15:11:32 -0800 | [diff] [blame] | 106 | |
Yaowu Xu | bbaa966 | 2019-05-06 11:17:03 -0700 | [diff] [blame] | 107 | void aom_quantize_b_helper_c(const tran_low_t *coeff_ptr, intptr_t n_coeffs, |
| 108 | const int16_t *zbin_ptr, const int16_t *round_ptr, |
| 109 | const int16_t *quant_ptr, |
| 110 | const int16_t *quant_shift_ptr, |
| 111 | tran_low_t *qcoeff_ptr, tran_low_t *dqcoeff_ptr, |
| 112 | const int16_t *dequant_ptr, uint16_t *eob_ptr, |
| 113 | const int16_t *scan, const int16_t *iscan, |
| 114 | const qm_val_t *qm_ptr, const qm_val_t *iqm_ptr, |
| 115 | const int log_scale) { |
Cheng Chen | ce8275c | 2017-04-26 11:16:24 -0700 | [diff] [blame] | 116 | const int zbins[2] = { ROUND_POWER_OF_TWO(zbin_ptr[0], log_scale), |
| 117 | ROUND_POWER_OF_TWO(zbin_ptr[1], log_scale) }; |
| 118 | const int nzbins[2] = { zbins[0] * -1, zbins[1] * -1 }; |
| 119 | int i, non_zero_count = (int)n_coeffs, eob = -1; |
| 120 | (void)iscan; |
| 121 | |
| 122 | memset(qcoeff_ptr, 0, n_coeffs * sizeof(*qcoeff_ptr)); |
| 123 | memset(dqcoeff_ptr, 0, n_coeffs * sizeof(*dqcoeff_ptr)); |
| 124 | |
Peng Bin | f36be56 | 2018-09-07 15:28:46 +0800 | [diff] [blame] | 125 | // Pre-scan pass |
| 126 | for (i = (int)n_coeffs - 1; i >= 0; i--) { |
| 127 | const int rc = scan[i]; |
| 128 | const qm_val_t wt = qm_ptr != NULL ? qm_ptr[rc] : (1 << AOM_QM_BITS); |
| 129 | const int coeff = coeff_ptr[rc] * wt; |
Cheng Chen | ce8275c | 2017-04-26 11:16:24 -0700 | [diff] [blame] | 130 | |
Peng Bin | f36be56 | 2018-09-07 15:28:46 +0800 | [diff] [blame] | 131 | if (coeff < (zbins[rc != 0] * (1 << AOM_QM_BITS)) && |
| 132 | coeff > (nzbins[rc != 0] * (1 << AOM_QM_BITS))) |
| 133 | non_zero_count--; |
| 134 | else |
| 135 | break; |
| 136 | } |
Cheng Chen | ce8275c | 2017-04-26 11:16:24 -0700 | [diff] [blame] | 137 | |
Peng Bin | f36be56 | 2018-09-07 15:28:46 +0800 | [diff] [blame] | 138 | // Quantization pass: All coefficients with index >= zero_flag are |
| 139 | // skippable. Note: zero_flag can be zero. |
| 140 | for (i = 0; i < non_zero_count; i++) { |
| 141 | const int rc = scan[i]; |
| 142 | const int coeff = coeff_ptr[rc]; |
| 143 | const int coeff_sign = (coeff >> 31); |
| 144 | const int abs_coeff = (coeff ^ coeff_sign) - coeff_sign; |
| 145 | int tmp32; |
Cheng Chen | ce8275c | 2017-04-26 11:16:24 -0700 | [diff] [blame] | 146 | |
Peng Bin | f36be56 | 2018-09-07 15:28:46 +0800 | [diff] [blame] | 147 | const qm_val_t wt = qm_ptr != NULL ? qm_ptr[rc] : (1 << AOM_QM_BITS); |
| 148 | if (abs_coeff * wt >= (zbins[rc != 0] << AOM_QM_BITS)) { |
| 149 | int64_t tmp = |
| 150 | clamp(abs_coeff + ROUND_POWER_OF_TWO(round_ptr[rc != 0], log_scale), |
| 151 | INT16_MIN, INT16_MAX); |
| 152 | tmp *= wt; |
| 153 | tmp32 = (int)(((((tmp * quant_ptr[rc != 0]) >> 16) + tmp) * |
| 154 | quant_shift_ptr[rc != 0]) >> |
| 155 | (16 - log_scale + AOM_QM_BITS)); // quantization |
| 156 | qcoeff_ptr[rc] = (tmp32 ^ coeff_sign) - coeff_sign; |
| 157 | const int iwt = iqm_ptr != NULL ? iqm_ptr[rc] : (1 << AOM_QM_BITS); |
| 158 | const int dequant = |
| 159 | (dequant_ptr[rc != 0] * iwt + (1 << (AOM_QM_BITS - 1))) >> |
| 160 | AOM_QM_BITS; |
| 161 | const tran_low_t abs_dqcoeff = (tmp32 * dequant) >> log_scale; |
| 162 | dqcoeff_ptr[rc] = (tran_low_t)((abs_dqcoeff ^ coeff_sign) - coeff_sign); |
Cheng Chen | ce8275c | 2017-04-26 11:16:24 -0700 | [diff] [blame] | 163 | |
Peng Bin | f36be56 | 2018-09-07 15:28:46 +0800 | [diff] [blame] | 164 | if (tmp32) eob = i; |
Cheng Chen | ce8275c | 2017-04-26 11:16:24 -0700 | [diff] [blame] | 165 | } |
| 166 | } |
| 167 | *eob_ptr = eob + 1; |
| 168 | } |
| 169 | |
Jerome Jiang | f8cc1c4 | 2019-08-02 16:34:32 -0700 | [diff] [blame] | 170 | #if CONFIG_AV1_HIGHBITDEPTH |
Yaowu Xu | bbaa966 | 2019-05-06 11:17:03 -0700 | [diff] [blame] | 171 | void aom_highbd_quantize_b_adaptive_helper_c( |
Sarah Parker | eec52bb | 2019-01-17 15:04:35 -0800 | [diff] [blame] | 172 | const tran_low_t *coeff_ptr, intptr_t n_coeffs, const int16_t *zbin_ptr, |
| 173 | const int16_t *round_ptr, const int16_t *quant_ptr, |
| 174 | const int16_t *quant_shift_ptr, tran_low_t *qcoeff_ptr, |
| 175 | tran_low_t *dqcoeff_ptr, const int16_t *dequant_ptr, uint16_t *eob_ptr, |
| 176 | const int16_t *scan, const int16_t *iscan, const qm_val_t *qm_ptr, |
| 177 | const qm_val_t *iqm_ptr, const int log_scale) { |
Sarah Parker | eec52bb | 2019-01-17 15:04:35 -0800 | [diff] [blame] | 178 | const int zbins[2] = { ROUND_POWER_OF_TWO(zbin_ptr[0], log_scale), |
| 179 | ROUND_POWER_OF_TWO(zbin_ptr[1], log_scale) }; |
| 180 | const int nzbins[2] = { zbins[0] * -1, zbins[1] * -1 }; |
Sarah Parker | eec52bb | 2019-01-17 15:04:35 -0800 | [diff] [blame] | 181 | (void)iscan; |
Remya | c356981 | 2019-05-21 18:00:18 +0530 | [diff] [blame] | 182 | int i, non_zero_count = (int)n_coeffs, eob = -1; |
Sarah Parker | eec52bb | 2019-01-17 15:04:35 -0800 | [diff] [blame] | 183 | |
| 184 | memset(qcoeff_ptr, 0, n_coeffs * sizeof(*qcoeff_ptr)); |
| 185 | memset(dqcoeff_ptr, 0, n_coeffs * sizeof(*dqcoeff_ptr)); |
| 186 | |
Debargha Mukherjee | b52c5ce | 2019-03-06 11:39:31 -0800 | [diff] [blame] | 187 | int prescan_add[2]; |
| 188 | for (i = 0; i < 2; ++i) |
| 189 | prescan_add[i] = ROUND_POWER_OF_TWO(dequant_ptr[i] * EOB_FACTOR, 7); |
| 190 | |
Sarah Parker | eec52bb | 2019-01-17 15:04:35 -0800 | [diff] [blame] | 191 | // Pre-scan pass |
Remya | c356981 | 2019-05-21 18:00:18 +0530 | [diff] [blame] | 192 | for (i = (int)n_coeffs - 1; i >= 0; i--) { |
Sarah Parker | eec52bb | 2019-01-17 15:04:35 -0800 | [diff] [blame] | 193 | const int rc = scan[i]; |
| 194 | const qm_val_t wt = qm_ptr != NULL ? qm_ptr[rc] : (1 << AOM_QM_BITS); |
| 195 | const int coeff = coeff_ptr[rc] * wt; |
Debargha Mukherjee | b52c5ce | 2019-03-06 11:39:31 -0800 | [diff] [blame] | 196 | const int prescan_add_val = prescan_add[rc != 0]; |
Remya | c356981 | 2019-05-21 18:00:18 +0530 | [diff] [blame] | 197 | if (coeff < (zbins[rc != 0] * (1 << AOM_QM_BITS) + prescan_add_val) && |
| 198 | coeff > (nzbins[rc != 0] * (1 << AOM_QM_BITS) - prescan_add_val)) |
| 199 | non_zero_count--; |
| 200 | else |
| 201 | break; |
Sarah Parker | eec52bb | 2019-01-17 15:04:35 -0800 | [diff] [blame] | 202 | } |
| 203 | |
Remya | c356981 | 2019-05-21 18:00:18 +0530 | [diff] [blame] | 204 | // Quantization pass: All coefficients with index >= zero_flag are |
| 205 | // skippable. Note: zero_flag can be zero. |
Debargha Mukherjee | b52c5ce | 2019-03-06 11:39:31 -0800 | [diff] [blame] | 206 | #if SKIP_EOB_FACTOR_ADJUST |
| 207 | int first = -1; |
| 208 | #endif // SKIP_EOB_FACTOR_ADJUST |
Remya | c356981 | 2019-05-21 18:00:18 +0530 | [diff] [blame] | 209 | for (i = 0; i < non_zero_count; i++) { |
| 210 | const int rc = scan[i]; |
Sarah Parker | eec52bb | 2019-01-17 15:04:35 -0800 | [diff] [blame] | 211 | const int coeff = coeff_ptr[rc]; |
| 212 | const int coeff_sign = (coeff >> 31); |
| 213 | const qm_val_t wt = qm_ptr != NULL ? qm_ptr[rc] : (1 << AOM_QM_BITS); |
Sarah Parker | eec52bb | 2019-01-17 15:04:35 -0800 | [diff] [blame] | 214 | const int abs_coeff = (coeff ^ coeff_sign) - coeff_sign; |
Remya | c356981 | 2019-05-21 18:00:18 +0530 | [diff] [blame] | 215 | if (abs_coeff * wt >= (zbins[rc != 0] << AOM_QM_BITS)) { |
| 216 | const int64_t tmp1 = |
| 217 | abs_coeff + ROUND_POWER_OF_TWO(round_ptr[rc != 0], log_scale); |
| 218 | const int64_t tmpw = tmp1 * wt; |
| 219 | const int64_t tmp2 = ((tmpw * quant_ptr[rc != 0]) >> 16) + tmpw; |
| 220 | const int abs_qcoeff = (int)((tmp2 * quant_shift_ptr[rc != 0]) >> |
| 221 | (16 - log_scale + AOM_QM_BITS)); |
| 222 | qcoeff_ptr[rc] = (tran_low_t)((abs_qcoeff ^ coeff_sign) - coeff_sign); |
| 223 | const qm_val_t iwt = iqm_ptr != NULL ? iqm_ptr[rc] : (1 << AOM_QM_BITS); |
| 224 | const int dequant = |
| 225 | (dequant_ptr[rc != 0] * iwt + (1 << (AOM_QM_BITS - 1))) >> |
| 226 | AOM_QM_BITS; |
| 227 | const tran_low_t abs_dqcoeff = (abs_qcoeff * dequant) >> log_scale; |
| 228 | dqcoeff_ptr[rc] = (tran_low_t)((abs_dqcoeff ^ coeff_sign) - coeff_sign); |
| 229 | if (abs_qcoeff) { |
| 230 | eob = i; |
Debargha Mukherjee | b52c5ce | 2019-03-06 11:39:31 -0800 | [diff] [blame] | 231 | #if SKIP_EOB_FACTOR_ADJUST |
Remya | c356981 | 2019-05-21 18:00:18 +0530 | [diff] [blame] | 232 | if (first == -1) first = eob; |
Debargha Mukherjee | b52c5ce | 2019-03-06 11:39:31 -0800 | [diff] [blame] | 233 | #endif // SKIP_EOB_FACTOR_ADJUST |
Remya | c356981 | 2019-05-21 18:00:18 +0530 | [diff] [blame] | 234 | } |
Debargha Mukherjee | b52c5ce | 2019-03-06 11:39:31 -0800 | [diff] [blame] | 235 | } |
Sarah Parker | eec52bb | 2019-01-17 15:04:35 -0800 | [diff] [blame] | 236 | } |
Debargha Mukherjee | b52c5ce | 2019-03-06 11:39:31 -0800 | [diff] [blame] | 237 | #if SKIP_EOB_FACTOR_ADJUST |
| 238 | if (eob >= 0 && first == eob) { |
| 239 | const int rc = scan[eob]; |
| 240 | if (qcoeff_ptr[rc] == 1 || qcoeff_ptr[rc] == -1) { |
| 241 | const qm_val_t wt = qm_ptr != NULL ? qm_ptr[rc] : (1 << AOM_QM_BITS); |
| 242 | const int coeff = coeff_ptr[rc] * wt; |
| 243 | const int factor = EOB_FACTOR + SKIP_EOB_FACTOR_ADJUST; |
| 244 | const int prescan_add_val = |
| 245 | ROUND_POWER_OF_TWO(dequant_ptr[rc != 0] * factor, 7); |
| 246 | if (coeff < (zbins[rc != 0] * (1 << AOM_QM_BITS) + prescan_add_val) && |
| 247 | coeff > (nzbins[rc != 0] * (1 << AOM_QM_BITS) - prescan_add_val)) { |
| 248 | qcoeff_ptr[rc] = 0; |
| 249 | dqcoeff_ptr[rc] = 0; |
| 250 | eob = -1; |
| 251 | } |
| 252 | } |
| 253 | } |
| 254 | #endif // SKIP_EOB_FACTOR_ADJUST |
Sarah Parker | eec52bb | 2019-01-17 15:04:35 -0800 | [diff] [blame] | 255 | *eob_ptr = eob + 1; |
| 256 | } |
| 257 | |
Yaowu Xu | bbaa966 | 2019-05-06 11:17:03 -0700 | [diff] [blame] | 258 | void aom_highbd_quantize_b_helper_c( |
Peng Bin | 6b97198 | 2018-09-07 15:21:40 +0800 | [diff] [blame] | 259 | const tran_low_t *coeff_ptr, intptr_t n_coeffs, const int16_t *zbin_ptr, |
| 260 | const int16_t *round_ptr, const int16_t *quant_ptr, |
Thomas Davies | f3b5ee1 | 2017-07-18 15:16:43 +0100 | [diff] [blame] | 261 | const int16_t *quant_shift_ptr, tran_low_t *qcoeff_ptr, |
| 262 | tran_low_t *dqcoeff_ptr, const int16_t *dequant_ptr, uint16_t *eob_ptr, |
| 263 | const int16_t *scan, const int16_t *iscan, const qm_val_t *qm_ptr, |
| 264 | const qm_val_t *iqm_ptr, const int log_scale) { |
| 265 | int i, eob = -1; |
| 266 | const int zbins[2] = { ROUND_POWER_OF_TWO(zbin_ptr[0], log_scale), |
| 267 | ROUND_POWER_OF_TWO(zbin_ptr[1], log_scale) }; |
| 268 | const int nzbins[2] = { zbins[0] * -1, zbins[1] * -1 }; |
| 269 | int dequant; |
Thomas Davies | f3b5ee1 | 2017-07-18 15:16:43 +0100 | [diff] [blame] | 270 | int idx_arr[4096]; |
Thomas Davies | f3b5ee1 | 2017-07-18 15:16:43 +0100 | [diff] [blame] | 271 | (void)iscan; |
| 272 | int idx = 0; |
| 273 | |
| 274 | memset(qcoeff_ptr, 0, n_coeffs * sizeof(*qcoeff_ptr)); |
| 275 | memset(dqcoeff_ptr, 0, n_coeffs * sizeof(*dqcoeff_ptr)); |
| 276 | |
Peng Bin | 6b97198 | 2018-09-07 15:21:40 +0800 | [diff] [blame] | 277 | // Pre-scan pass |
| 278 | for (i = 0; i < n_coeffs; i++) { |
| 279 | const int rc = scan[i]; |
| 280 | const qm_val_t wt = qm_ptr != NULL ? qm_ptr[rc] : (1 << AOM_QM_BITS); |
| 281 | const int coeff = coeff_ptr[rc] * wt; |
Thomas Davies | f3b5ee1 | 2017-07-18 15:16:43 +0100 | [diff] [blame] | 282 | |
Peng Bin | 6b97198 | 2018-09-07 15:21:40 +0800 | [diff] [blame] | 283 | // If the coefficient is out of the base ZBIN range, keep it for |
| 284 | // quantization. |
| 285 | if (coeff >= (zbins[rc != 0] * (1 << AOM_QM_BITS)) || |
| 286 | coeff <= (nzbins[rc != 0] * (1 << AOM_QM_BITS))) |
| 287 | idx_arr[idx++] = i; |
| 288 | } |
Thomas Davies | f3b5ee1 | 2017-07-18 15:16:43 +0100 | [diff] [blame] | 289 | |
Peng Bin | 6b97198 | 2018-09-07 15:21:40 +0800 | [diff] [blame] | 290 | // Quantization pass: only process the coefficients selected in |
| 291 | // pre-scan pass. Note: idx can be zero. |
| 292 | for (i = 0; i < idx; i++) { |
| 293 | const int rc = scan[idx_arr[i]]; |
| 294 | const int coeff = coeff_ptr[rc]; |
| 295 | const int coeff_sign = (coeff >> 31); |
| 296 | const qm_val_t wt = qm_ptr != NULL ? qm_ptr[rc] : (1 << AOM_QM_BITS); |
| 297 | const qm_val_t iwt = iqm_ptr != NULL ? iqm_ptr[rc] : (1 << AOM_QM_BITS); |
| 298 | const int abs_coeff = (coeff ^ coeff_sign) - coeff_sign; |
| 299 | const int64_t tmp1 = |
| 300 | abs_coeff + ROUND_POWER_OF_TWO(round_ptr[rc != 0], log_scale); |
| 301 | const int64_t tmpw = tmp1 * wt; |
| 302 | const int64_t tmp2 = ((tmpw * quant_ptr[rc != 0]) >> 16) + tmpw; |
| 303 | const int abs_qcoeff = (int)((tmp2 * quant_shift_ptr[rc != 0]) >> |
| 304 | (16 - log_scale + AOM_QM_BITS)); |
| 305 | qcoeff_ptr[rc] = (tran_low_t)((abs_qcoeff ^ coeff_sign) - coeff_sign); |
| 306 | dequant = |
| 307 | (dequant_ptr[rc != 0] * iwt + (1 << (AOM_QM_BITS - 1))) >> AOM_QM_BITS; |
| 308 | const tran_low_t abs_dqcoeff = (abs_qcoeff * dequant) >> log_scale; |
| 309 | dqcoeff_ptr[rc] = (tran_low_t)((abs_dqcoeff ^ coeff_sign) - coeff_sign); |
| 310 | if (abs_qcoeff) eob = idx_arr[i]; |
Thomas Davies | f3b5ee1 | 2017-07-18 15:16:43 +0100 | [diff] [blame] | 311 | } |
| 312 | *eob_ptr = eob + 1; |
| 313 | } |
Jerome Jiang | f8cc1c4 | 2019-08-02 16:34:32 -0700 | [diff] [blame] | 314 | #endif // CONFIG_AV1_HIGHBITDEPTH |
Thomas Davies | f3b5ee1 | 2017-07-18 15:16:43 +0100 | [diff] [blame] | 315 | |
Thomas Davies | f3b5ee1 | 2017-07-18 15:16:43 +0100 | [diff] [blame] | 316 | /* These functions should only be called when quantisation matrices |
| 317 | are not used. */ |
Sarah Parker | e343b8c | 2019-01-22 15:11:32 -0800 | [diff] [blame] | 318 | void aom_quantize_b_adaptive_c(const tran_low_t *coeff_ptr, intptr_t n_coeffs, |
| 319 | const int16_t *zbin_ptr, |
| 320 | const int16_t *round_ptr, |
| 321 | 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) { |
Yaowu Xu | bbaa966 | 2019-05-06 11:17:03 -0700 | [diff] [blame] | 326 | aom_quantize_b_adaptive_helper_c(coeff_ptr, n_coeffs, zbin_ptr, round_ptr, |
| 327 | quant_ptr, quant_shift_ptr, qcoeff_ptr, |
| 328 | dqcoeff_ptr, dequant_ptr, eob_ptr, scan, |
| 329 | iscan, NULL, NULL, 0); |
Sarah Parker | e343b8c | 2019-01-22 15:11:32 -0800 | [diff] [blame] | 330 | } |
| 331 | |
| 332 | void aom_quantize_b_32x32_adaptive_c( |
| 333 | const tran_low_t *coeff_ptr, intptr_t n_coeffs, const int16_t *zbin_ptr, |
| 334 | const int16_t *round_ptr, const int16_t *quant_ptr, |
| 335 | const int16_t *quant_shift_ptr, tran_low_t *qcoeff_ptr, |
| 336 | tran_low_t *dqcoeff_ptr, const int16_t *dequant_ptr, uint16_t *eob_ptr, |
| 337 | const int16_t *scan, const int16_t *iscan) { |
Yaowu Xu | bbaa966 | 2019-05-06 11:17:03 -0700 | [diff] [blame] | 338 | aom_quantize_b_adaptive_helper_c(coeff_ptr, n_coeffs, zbin_ptr, round_ptr, |
| 339 | quant_ptr, quant_shift_ptr, qcoeff_ptr, |
| 340 | dqcoeff_ptr, dequant_ptr, eob_ptr, scan, |
| 341 | iscan, NULL, NULL, 1); |
Sarah Parker | e343b8c | 2019-01-22 15:11:32 -0800 | [diff] [blame] | 342 | } |
| 343 | |
| 344 | void aom_quantize_b_64x64_adaptive_c( |
| 345 | const tran_low_t *coeff_ptr, intptr_t n_coeffs, const int16_t *zbin_ptr, |
| 346 | const int16_t *round_ptr, const int16_t *quant_ptr, |
| 347 | const int16_t *quant_shift_ptr, tran_low_t *qcoeff_ptr, |
| 348 | tran_low_t *dqcoeff_ptr, const int16_t *dequant_ptr, uint16_t *eob_ptr, |
| 349 | const int16_t *scan, const int16_t *iscan) { |
Yaowu Xu | bbaa966 | 2019-05-06 11:17:03 -0700 | [diff] [blame] | 350 | aom_quantize_b_adaptive_helper_c(coeff_ptr, n_coeffs, zbin_ptr, round_ptr, |
| 351 | quant_ptr, quant_shift_ptr, qcoeff_ptr, |
| 352 | dqcoeff_ptr, dequant_ptr, eob_ptr, scan, |
| 353 | iscan, NULL, NULL, 2); |
Sarah Parker | e343b8c | 2019-01-22 15:11:32 -0800 | [diff] [blame] | 354 | } |
| 355 | |
Jerome Jiang | f8cc1c4 | 2019-08-02 16:34:32 -0700 | [diff] [blame] | 356 | #if CONFIG_AV1_HIGHBITDEPTH |
Sarah Parker | e343b8c | 2019-01-22 15:11:32 -0800 | [diff] [blame] | 357 | void aom_highbd_quantize_b_adaptive_c( |
| 358 | const tran_low_t *coeff_ptr, intptr_t n_coeffs, const int16_t *zbin_ptr, |
| 359 | const int16_t *round_ptr, const int16_t *quant_ptr, |
| 360 | const int16_t *quant_shift_ptr, tran_low_t *qcoeff_ptr, |
| 361 | tran_low_t *dqcoeff_ptr, const int16_t *dequant_ptr, uint16_t *eob_ptr, |
| 362 | const int16_t *scan, const int16_t *iscan) { |
Yaowu Xu | bbaa966 | 2019-05-06 11:17:03 -0700 | [diff] [blame] | 363 | aom_highbd_quantize_b_adaptive_helper_c(coeff_ptr, n_coeffs, zbin_ptr, |
| 364 | round_ptr, quant_ptr, quant_shift_ptr, |
| 365 | qcoeff_ptr, dqcoeff_ptr, dequant_ptr, |
| 366 | eob_ptr, scan, iscan, NULL, NULL, 0); |
Sarah Parker | e343b8c | 2019-01-22 15:11:32 -0800 | [diff] [blame] | 367 | } |
| 368 | |
| 369 | void aom_highbd_quantize_b_32x32_adaptive_c( |
| 370 | const tran_low_t *coeff_ptr, intptr_t n_coeffs, const int16_t *zbin_ptr, |
| 371 | const int16_t *round_ptr, const int16_t *quant_ptr, |
| 372 | const int16_t *quant_shift_ptr, tran_low_t *qcoeff_ptr, |
| 373 | tran_low_t *dqcoeff_ptr, const int16_t *dequant_ptr, uint16_t *eob_ptr, |
| 374 | const int16_t *scan, const int16_t *iscan) { |
Yaowu Xu | bbaa966 | 2019-05-06 11:17:03 -0700 | [diff] [blame] | 375 | aom_highbd_quantize_b_adaptive_helper_c(coeff_ptr, n_coeffs, zbin_ptr, |
| 376 | round_ptr, quant_ptr, quant_shift_ptr, |
| 377 | qcoeff_ptr, dqcoeff_ptr, dequant_ptr, |
| 378 | eob_ptr, scan, iscan, NULL, NULL, 1); |
Sarah Parker | e343b8c | 2019-01-22 15:11:32 -0800 | [diff] [blame] | 379 | } |
| 380 | |
| 381 | void aom_highbd_quantize_b_64x64_adaptive_c( |
| 382 | const tran_low_t *coeff_ptr, intptr_t n_coeffs, const int16_t *zbin_ptr, |
| 383 | const int16_t *round_ptr, const int16_t *quant_ptr, |
| 384 | const int16_t *quant_shift_ptr, tran_low_t *qcoeff_ptr, |
| 385 | tran_low_t *dqcoeff_ptr, const int16_t *dequant_ptr, uint16_t *eob_ptr, |
| 386 | const int16_t *scan, const int16_t *iscan) { |
Yaowu Xu | bbaa966 | 2019-05-06 11:17:03 -0700 | [diff] [blame] | 387 | aom_highbd_quantize_b_adaptive_helper_c(coeff_ptr, n_coeffs, zbin_ptr, |
| 388 | round_ptr, quant_ptr, quant_shift_ptr, |
| 389 | qcoeff_ptr, dqcoeff_ptr, dequant_ptr, |
| 390 | eob_ptr, scan, iscan, NULL, NULL, 2); |
Sarah Parker | e343b8c | 2019-01-22 15:11:32 -0800 | [diff] [blame] | 391 | } |
Jerome Jiang | f8cc1c4 | 2019-08-02 16:34:32 -0700 | [diff] [blame] | 392 | #endif // CONFIG_AV1_HIGHBITDEPTH |
Sarah Parker | e343b8c | 2019-01-22 15:11:32 -0800 | [diff] [blame] | 393 | |
Cheng Chen | ce8275c | 2017-04-26 11:16:24 -0700 | [diff] [blame] | 394 | void aom_quantize_b_c(const tran_low_t *coeff_ptr, intptr_t n_coeffs, |
Peng Bin | f36be56 | 2018-09-07 15:28:46 +0800 | [diff] [blame] | 395 | const int16_t *zbin_ptr, const int16_t *round_ptr, |
| 396 | const int16_t *quant_ptr, const int16_t *quant_shift_ptr, |
| 397 | tran_low_t *qcoeff_ptr, tran_low_t *dqcoeff_ptr, |
| 398 | const int16_t *dequant_ptr, uint16_t *eob_ptr, |
| 399 | const int16_t *scan, const int16_t *iscan) { |
Yaowu Xu | bbaa966 | 2019-05-06 11:17:03 -0700 | [diff] [blame] | 400 | aom_quantize_b_helper_c(coeff_ptr, n_coeffs, zbin_ptr, round_ptr, quant_ptr, |
| 401 | quant_shift_ptr, qcoeff_ptr, dqcoeff_ptr, dequant_ptr, |
| 402 | eob_ptr, scan, iscan, NULL, NULL, 0); |
Cheng Chen | ce8275c | 2017-04-26 11:16:24 -0700 | [diff] [blame] | 403 | } |
| 404 | |
| 405 | void aom_quantize_b_32x32_c(const tran_low_t *coeff_ptr, intptr_t n_coeffs, |
Peng Bin | f36be56 | 2018-09-07 15:28:46 +0800 | [diff] [blame] | 406 | const int16_t *zbin_ptr, const int16_t *round_ptr, |
| 407 | const int16_t *quant_ptr, |
Cheng Chen | ce8275c | 2017-04-26 11:16:24 -0700 | [diff] [blame] | 408 | const int16_t *quant_shift_ptr, |
| 409 | tran_low_t *qcoeff_ptr, tran_low_t *dqcoeff_ptr, |
| 410 | const int16_t *dequant_ptr, uint16_t *eob_ptr, |
Thomas Davies | f3b5ee1 | 2017-07-18 15:16:43 +0100 | [diff] [blame] | 411 | const int16_t *scan, const int16_t *iscan) { |
Yaowu Xu | bbaa966 | 2019-05-06 11:17:03 -0700 | [diff] [blame] | 412 | aom_quantize_b_helper_c(coeff_ptr, n_coeffs, zbin_ptr, round_ptr, quant_ptr, |
| 413 | quant_shift_ptr, qcoeff_ptr, dqcoeff_ptr, dequant_ptr, |
| 414 | eob_ptr, scan, iscan, NULL, NULL, 1); |
Cheng Chen | ce8275c | 2017-04-26 11:16:24 -0700 | [diff] [blame] | 415 | } |
| 416 | |
Cheng Chen | ce8275c | 2017-04-26 11:16:24 -0700 | [diff] [blame] | 417 | void aom_quantize_b_64x64_c(const tran_low_t *coeff_ptr, intptr_t n_coeffs, |
Peng Bin | f36be56 | 2018-09-07 15:28:46 +0800 | [diff] [blame] | 418 | const int16_t *zbin_ptr, const int16_t *round_ptr, |
| 419 | const int16_t *quant_ptr, |
Cheng Chen | ce8275c | 2017-04-26 11:16:24 -0700 | [diff] [blame] | 420 | const int16_t *quant_shift_ptr, |
| 421 | tran_low_t *qcoeff_ptr, tran_low_t *dqcoeff_ptr, |
| 422 | const int16_t *dequant_ptr, uint16_t *eob_ptr, |
Thomas Davies | f3b5ee1 | 2017-07-18 15:16:43 +0100 | [diff] [blame] | 423 | const int16_t *scan, const int16_t *iscan) { |
Yaowu Xu | bbaa966 | 2019-05-06 11:17:03 -0700 | [diff] [blame] | 424 | aom_quantize_b_helper_c(coeff_ptr, n_coeffs, zbin_ptr, round_ptr, quant_ptr, |
| 425 | quant_shift_ptr, qcoeff_ptr, dqcoeff_ptr, dequant_ptr, |
| 426 | eob_ptr, scan, iscan, NULL, NULL, 2); |
Cheng Chen | ce8275c | 2017-04-26 11:16:24 -0700 | [diff] [blame] | 427 | } |
Cheng Chen | ce8275c | 2017-04-26 11:16:24 -0700 | [diff] [blame] | 428 | |
Jerome Jiang | f8cc1c4 | 2019-08-02 16:34:32 -0700 | [diff] [blame] | 429 | #if CONFIG_AV1_HIGHBITDEPTH |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 430 | void aom_highbd_quantize_b_c(const tran_low_t *coeff_ptr, intptr_t n_coeffs, |
Peng Bin | 6b97198 | 2018-09-07 15:21:40 +0800 | [diff] [blame] | 431 | const int16_t *zbin_ptr, const int16_t *round_ptr, |
| 432 | const int16_t *quant_ptr, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 433 | const int16_t *quant_shift_ptr, |
| 434 | tran_low_t *qcoeff_ptr, tran_low_t *dqcoeff_ptr, |
| 435 | const int16_t *dequant_ptr, uint16_t *eob_ptr, |
| 436 | const int16_t *scan, const int16_t *iscan) { |
Yaowu Xu | bbaa966 | 2019-05-06 11:17:03 -0700 | [diff] [blame] | 437 | aom_highbd_quantize_b_helper_c(coeff_ptr, n_coeffs, zbin_ptr, round_ptr, |
| 438 | quant_ptr, quant_shift_ptr, qcoeff_ptr, |
| 439 | dqcoeff_ptr, dequant_ptr, eob_ptr, scan, iscan, |
| 440 | NULL, NULL, 0); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 441 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 442 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 443 | void aom_highbd_quantize_b_32x32_c( |
Peng Bin | 6b97198 | 2018-09-07 15:21:40 +0800 | [diff] [blame] | 444 | const tran_low_t *coeff_ptr, intptr_t n_coeffs, const int16_t *zbin_ptr, |
| 445 | const int16_t *round_ptr, const int16_t *quant_ptr, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 446 | const int16_t *quant_shift_ptr, tran_low_t *qcoeff_ptr, |
| 447 | tran_low_t *dqcoeff_ptr, const int16_t *dequant_ptr, uint16_t *eob_ptr, |
| 448 | const int16_t *scan, const int16_t *iscan) { |
Yaowu Xu | bbaa966 | 2019-05-06 11:17:03 -0700 | [diff] [blame] | 449 | aom_highbd_quantize_b_helper_c(coeff_ptr, n_coeffs, zbin_ptr, round_ptr, |
| 450 | quant_ptr, quant_shift_ptr, qcoeff_ptr, |
| 451 | dqcoeff_ptr, dequant_ptr, eob_ptr, scan, iscan, |
| 452 | NULL, NULL, 1); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 453 | } |
Debargha Mukherjee | 0e11912 | 2016-11-04 12:10:23 -0700 | [diff] [blame] | 454 | |
Debargha Mukherjee | 0e11912 | 2016-11-04 12:10:23 -0700 | [diff] [blame] | 455 | void aom_highbd_quantize_b_64x64_c( |
Peng Bin | 6b97198 | 2018-09-07 15:21:40 +0800 | [diff] [blame] | 456 | const tran_low_t *coeff_ptr, intptr_t n_coeffs, const int16_t *zbin_ptr, |
| 457 | const int16_t *round_ptr, const int16_t *quant_ptr, |
Debargha Mukherjee | 0e11912 | 2016-11-04 12:10:23 -0700 | [diff] [blame] | 458 | const int16_t *quant_shift_ptr, tran_low_t *qcoeff_ptr, |
| 459 | tran_low_t *dqcoeff_ptr, const int16_t *dequant_ptr, uint16_t *eob_ptr, |
| 460 | const int16_t *scan, const int16_t *iscan) { |
Yaowu Xu | bbaa966 | 2019-05-06 11:17:03 -0700 | [diff] [blame] | 461 | aom_highbd_quantize_b_helper_c(coeff_ptr, n_coeffs, zbin_ptr, round_ptr, |
| 462 | quant_ptr, quant_shift_ptr, qcoeff_ptr, |
| 463 | dqcoeff_ptr, dequant_ptr, eob_ptr, scan, iscan, |
| 464 | NULL, NULL, 2); |
Debargha Mukherjee | 0e11912 | 2016-11-04 12:10:23 -0700 | [diff] [blame] | 465 | } |
Jerome Jiang | f8cc1c4 | 2019-08-02 16:34:32 -0700 | [diff] [blame] | 466 | #endif // CONFIG_AV1_HIGHBITDEPTH |