Angie Chiang | 80b8226 | 2017-02-24 11:39:47 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2017, Alliance for Open Media. All rights reserved |
| 3 | * |
| 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. |
| 10 | */ |
| 11 | |
Angie Chiang | 41220ab | 2018-02-14 17:48:23 -0800 | [diff] [blame] | 12 | #include "av1/encoder/encodetxb.h" |
| 13 | |
Linfeng Zhang | ae7b2f3 | 2017-11-08 15:46:57 -0800 | [diff] [blame] | 14 | #include "aom_ports/mem.h" |
Angie Chiang | 0397eda | 2017-03-15 16:57:14 -0700 | [diff] [blame] | 15 | #include "av1/common/blockd.h" |
Angie Chiang | e50f3ec | 2017-04-10 15:50:33 -0700 | [diff] [blame] | 16 | #include "av1/common/idct.h" |
Angie Chiang | 0397eda | 2017-03-15 16:57:14 -0700 | [diff] [blame] | 17 | #include "av1/common/pred_common.h" |
Angie Chiang | 41220ab | 2018-02-14 17:48:23 -0800 | [diff] [blame] | 18 | #include "av1/common/scan.h" |
Angie Chiang | 1628fcc | 2017-04-13 16:30:30 -0700 | [diff] [blame] | 19 | #include "av1/encoder/bitstream.h" |
Angie Chiang | 47c7218 | 2017-02-27 14:30:38 -0800 | [diff] [blame] | 20 | #include "av1/encoder/cost.h" |
Angie Chiang | 41220ab | 2018-02-14 17:48:23 -0800 | [diff] [blame] | 21 | #include "av1/encoder/encodeframe.h" |
Michelle Findlay-Olynyk | fbab062 | 2017-12-13 14:10:56 -0800 | [diff] [blame] | 22 | #include "av1/encoder/hash.h" |
Angie Chiang | 808d859 | 2017-04-06 18:36:55 -0700 | [diff] [blame] | 23 | #include "av1/encoder/rdopt.h" |
Angie Chiang | 0397eda | 2017-03-15 16:57:14 -0700 | [diff] [blame] | 24 | #include "av1/encoder/tokenize.h" |
Angie Chiang | 80b8226 | 2017-02-24 11:39:47 -0800 | [diff] [blame] | 25 | |
Yaowu Xu | 74e6335 | 2019-05-06 09:21:33 -0700 | [diff] [blame] | 26 | #if CONFIG_HTB_TRELLIS |
Michelle Findlay-Olynyk | dea531d | 2017-12-13 14:10:56 -0800 | [diff] [blame] | 27 | static int hbt_needs_init = 1; |
Peng Bin | 8a204cd | 2018-04-08 13:07:35 +0800 | [diff] [blame] | 28 | static CRC32C crc_calculator; |
Michelle Findlay-Olynyk | dea531d | 2017-12-13 14:10:56 -0800 | [diff] [blame] | 29 | static const int HBT_EOB = 16; // also the length in opt_qcoeff |
| 30 | static const int HBT_TABLE_SIZE = 65536; // 16 bit: holds 65536 'arrays' |
| 31 | static const int HBT_ARRAY_LENGTH = 256; // 8 bit: 256 entries |
| 32 | // If removed in hbt_create_hashes or increased beyond int8_t, widen deltas type |
| 33 | static const int HBT_KICKOUT = 3; |
Michelle Findlay-Olynyk | fbab062 | 2017-12-13 14:10:56 -0800 | [diff] [blame] | 34 | |
| 35 | typedef struct OptTxbQcoeff { |
Michelle Findlay-Olynyk | dea531d | 2017-12-13 14:10:56 -0800 | [diff] [blame] | 36 | // Use larger type if larger/no kickout value is used in hbt_create_hashes |
| 37 | int8_t deltas[16]; |
| 38 | uint32_t hbt_qc_hash; |
| 39 | uint32_t hbt_ctx_hash; |
| 40 | int init; |
| 41 | int rate_cost; |
Michelle Findlay-Olynyk | fbab062 | 2017-12-13 14:10:56 -0800 | [diff] [blame] | 42 | } OptTxbQcoeff; |
| 43 | |
Michelle Findlay-Olynyk | dea531d | 2017-12-13 14:10:56 -0800 | [diff] [blame] | 44 | OptTxbQcoeff *hbt_hash_table; |
Yaowu Xu | 74e6335 | 2019-05-06 09:21:33 -0700 | [diff] [blame] | 45 | #endif // CONFIG_HTB_TRELLIS |
Michelle Findlay-Olynyk | fbab062 | 2017-12-13 14:10:56 -0800 | [diff] [blame] | 46 | |
Dake He | a47cd6c | 2017-10-13 18:09:58 -0700 | [diff] [blame] | 47 | typedef struct LevelDownStats { |
| 48 | int update; |
| 49 | tran_low_t low_qc; |
| 50 | tran_low_t low_dqc; |
Dake He | a47cd6c | 2017-10-13 18:09:58 -0700 | [diff] [blame] | 51 | int64_t dist0; |
| 52 | int rate; |
| 53 | int rate_low; |
| 54 | int64_t dist; |
| 55 | int64_t dist_low; |
| 56 | int64_t rd; |
| 57 | int64_t rd_low; |
Dake He | 4d44769 | 2017-12-15 09:10:06 -0800 | [diff] [blame] | 58 | int64_t nz_rd; |
Dake He | a47cd6c | 2017-10-13 18:09:58 -0700 | [diff] [blame] | 59 | int64_t rd_diff; |
| 60 | int cost_diff; |
| 61 | int64_t dist_diff; |
| 62 | int new_eob; |
| 63 | } LevelDownStats; |
| 64 | |
Jingning Han | 37f5354 | 2019-06-06 14:22:17 -0700 | [diff] [blame] | 65 | static INLINE int get_dqv(const int16_t *dequant, int coeff_idx, |
| 66 | const qm_val_t *iqmatrix) { |
| 67 | int dqv = dequant[!!coeff_idx]; |
| 68 | if (iqmatrix != NULL) |
| 69 | dqv = |
| 70 | ((iqmatrix[coeff_idx] * dqv) + (1 << (AOM_QM_BITS - 1))) >> AOM_QM_BITS; |
| 71 | return dqv; |
| 72 | } |
| 73 | |
Angie Chiang | f0fbf9d | 2017-03-15 15:01:22 -0700 | [diff] [blame] | 74 | void av1_alloc_txb_buf(AV1_COMP *cpi) { |
Jingning Han | f5a4d3b | 2017-08-27 23:01:19 -0700 | [diff] [blame] | 75 | AV1_COMMON *cm = &cpi->common; |
Urvang Joshi | 9dc909d | 2020-03-23 16:07:02 -0700 | [diff] [blame] | 76 | int size = ((cm->mi_params.mi_rows >> cm->seq_params.mib_size_log2) + 1) * |
| 77 | ((cm->mi_params.mi_cols >> cm->seq_params.mib_size_log2) + 1); |
Jingning Han | f5a4d3b | 2017-08-27 23:01:19 -0700 | [diff] [blame] | 78 | |
Angie Chiang | 9367e3e | 2017-10-02 16:28:11 -0700 | [diff] [blame] | 79 | av1_free_txb_buf(cpi); |
Jingning Han | f5a4d3b | 2017-08-27 23:01:19 -0700 | [diff] [blame] | 80 | // TODO(jingning): This should be further reduced. |
| 81 | CHECK_MEM_ERROR(cm, cpi->coeff_buffer_base, |
Peng Bin | 27d7ca9 | 2018-03-22 22:25:56 +0800 | [diff] [blame] | 82 | aom_memalign(32, sizeof(*cpi->coeff_buffer_base) * size)); |
Angie Chiang | f0fbf9d | 2017-03-15 15:01:22 -0700 | [diff] [blame] | 83 | } |
| 84 | |
Angie Chiang | ed9667e | 2018-03-02 15:10:50 -0800 | [diff] [blame] | 85 | void av1_free_txb_buf(AV1_COMP *cpi) { aom_free(cpi->coeff_buffer_base); } |
Angie Chiang | f0fbf9d | 2017-03-15 15:01:22 -0700 | [diff] [blame] | 86 | |
Angie Chiang | 80b8226 | 2017-02-24 11:39:47 -0800 | [diff] [blame] | 87 | static void write_golomb(aom_writer *w, int level) { |
| 88 | int x = level + 1; |
| 89 | int i = x; |
| 90 | int length = 0; |
| 91 | |
| 92 | while (i) { |
| 93 | i >>= 1; |
| 94 | ++length; |
| 95 | } |
| 96 | assert(length > 0); |
| 97 | |
| 98 | for (i = 0; i < length - 1; ++i) aom_write_bit(w, 0); |
| 99 | |
| 100 | for (i = length - 1; i >= 0; --i) aom_write_bit(w, (x >> i) & 0x01); |
| 101 | } |
| 102 | |
Dake He | a47cd6c | 2017-10-13 18:09:58 -0700 | [diff] [blame] | 103 | static INLINE tran_low_t get_lower_coeff(tran_low_t qc) { |
| 104 | if (qc == 0) { |
| 105 | return 0; |
| 106 | } |
| 107 | return qc > 0 ? qc - 1 : qc + 1; |
| 108 | } |
| 109 | |
Angie Chiang | b3167a6 | 2018-01-30 19:37:57 -0800 | [diff] [blame] | 110 | static INLINE tran_low_t qcoeff_to_dqcoeff(tran_low_t qc, int coeff_idx, |
Angie Chiang | b3167a6 | 2018-01-30 19:37:57 -0800 | [diff] [blame] | 111 | int dqv, int shift, |
| 112 | const qm_val_t *iqmatrix) { |
Angie Chiang | 40b0731 | 2018-03-30 10:42:55 -0700 | [diff] [blame] | 113 | int sign = qc < 0 ? -1 : 1; |
Angie Chiang | b3167a6 | 2018-01-30 19:37:57 -0800 | [diff] [blame] | 114 | if (iqmatrix != NULL) |
| 115 | dqv = |
| 116 | ((iqmatrix[coeff_idx] * dqv) + (1 << (AOM_QM_BITS - 1))) >> AOM_QM_BITS; |
Angie Chiang | 40b0731 | 2018-03-30 10:42:55 -0700 | [diff] [blame] | 117 | return sign * ((abs(qc) * dqv) >> shift); |
Dake He | a47cd6c | 2017-10-13 18:09:58 -0700 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | static INLINE int64_t get_coeff_dist(tran_low_t tcoeff, tran_low_t dqcoeff, |
| 121 | int shift) { |
| 122 | const int64_t diff = (tcoeff - dqcoeff) * (1 << shift); |
| 123 | const int64_t error = diff * diff; |
| 124 | return error; |
| 125 | } |
| 126 | |
Urvang Joshi | ac97d27 | 2018-07-31 15:16:52 -0700 | [diff] [blame] | 127 | static const int8_t eob_to_pos_small[33] = { |
| 128 | 0, 1, 2, // 0-2 |
| 129 | 3, 3, // 3-4 |
| 130 | 4, 4, 4, 4, // 5-8 |
| 131 | 5, 5, 5, 5, 5, 5, 5, 5, // 9-16 |
| 132 | 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6 // 17-32 |
| 133 | }; |
| 134 | |
| 135 | static const int8_t eob_to_pos_large[17] = { |
| 136 | 6, // place holder |
| 137 | 7, // 33-64 |
| 138 | 8, 8, // 65-128 |
| 139 | 9, 9, 9, 9, // 129-256 |
| 140 | 10, 10, 10, 10, 10, 10, 10, 10, // 257-512 |
| 141 | 11 // 513- |
| 142 | }; |
| 143 | |
| 144 | static INLINE int get_eob_pos_token(const int eob, int *const extra) { |
| 145 | int t; |
| 146 | |
| 147 | if (eob < 33) { |
| 148 | t = eob_to_pos_small[eob]; |
| 149 | } else { |
| 150 | const int e = AOMMIN((eob - 1) >> 5, 16); |
| 151 | t = eob_to_pos_large[e]; |
| 152 | } |
| 153 | |
Yaowu Xu | 3a19b8a | 2019-05-01 08:40:42 -0700 | [diff] [blame] | 154 | *extra = eob - av1_eob_group_start[t]; |
Urvang Joshi | ac97d27 | 2018-07-31 15:16:52 -0700 | [diff] [blame] | 155 | |
| 156 | return t; |
| 157 | } |
| 158 | |
Yue Chen | 198738d | 2018-03-08 17:26:01 -0800 | [diff] [blame] | 159 | #if CONFIG_ENTROPY_STATS |
Yaowu Xu | 49abec8 | 2018-03-13 16:09:01 -0700 | [diff] [blame] | 160 | void av1_update_eob_context(int cdf_idx, int eob, TX_SIZE tx_size, |
Katsuhisa Yuasa | 55c95f8 | 2018-04-03 00:51:22 +0900 | [diff] [blame] | 161 | TX_CLASS tx_class, PLANE_TYPE plane, |
Yunqing Wang | 0e141b5 | 2017-11-02 15:08:58 -0700 | [diff] [blame] | 162 | FRAME_CONTEXT *ec_ctx, FRAME_COUNTS *counts, |
| 163 | uint8_t allow_update_cdf) { |
Yue Chen | 198738d | 2018-03-08 17:26:01 -0800 | [diff] [blame] | 164 | #else |
Katsuhisa Yuasa | 55c95f8 | 2018-04-03 00:51:22 +0900 | [diff] [blame] | 165 | void av1_update_eob_context(int eob, TX_SIZE tx_size, TX_CLASS tx_class, |
Yaowu Xu | 49abec8 | 2018-03-13 16:09:01 -0700 | [diff] [blame] | 166 | PLANE_TYPE plane, FRAME_CONTEXT *ec_ctx, |
| 167 | uint8_t allow_update_cdf) { |
Yue Chen | 198738d | 2018-03-08 17:26:01 -0800 | [diff] [blame] | 168 | #endif |
Yaowu Xu | 49abec8 | 2018-03-13 16:09:01 -0700 | [diff] [blame] | 169 | int eob_extra; |
Linfeng Zhang | 0c72b2f | 2017-12-04 10:59:28 -0800 | [diff] [blame] | 170 | const int eob_pt = get_eob_pos_token(eob, &eob_extra); |
Debargha Mukherjee | b3eda2f | 2017-11-28 16:00:20 -0800 | [diff] [blame] | 171 | TX_SIZE txs_ctx = get_txsize_entropy_ctx(tx_size); |
Dake He | a47cd6c | 2017-10-13 18:09:58 -0700 | [diff] [blame] | 172 | |
Dake He | 0db7d0e | 2017-12-21 15:23:20 -0800 | [diff] [blame] | 173 | const int eob_multi_size = txsize_log2_minus4[tx_size]; |
Katsuhisa Yuasa | 55c95f8 | 2018-04-03 00:51:22 +0900 | [diff] [blame] | 174 | const int eob_multi_ctx = (tx_class == TX_CLASS_2D) ? 0 : 1; |
Dake He | 0db7d0e | 2017-12-21 15:23:20 -0800 | [diff] [blame] | 175 | |
| 176 | switch (eob_multi_size) { |
| 177 | case 0: |
Yue Chen | 198738d | 2018-03-08 17:26:01 -0800 | [diff] [blame] | 178 | #if CONFIG_ENTROPY_STATS |
| 179 | ++counts->eob_multi16[cdf_idx][plane][eob_multi_ctx][eob_pt - 1]; |
| 180 | #endif |
Dake He | 0db7d0e | 2017-12-21 15:23:20 -0800 | [diff] [blame] | 181 | if (allow_update_cdf) |
| 182 | update_cdf(ec_ctx->eob_flag_cdf16[plane][eob_multi_ctx], eob_pt - 1, 5); |
| 183 | break; |
| 184 | case 1: |
Yue Chen | 198738d | 2018-03-08 17:26:01 -0800 | [diff] [blame] | 185 | #if CONFIG_ENTROPY_STATS |
| 186 | ++counts->eob_multi32[cdf_idx][plane][eob_multi_ctx][eob_pt - 1]; |
| 187 | #endif |
Dake He | 0db7d0e | 2017-12-21 15:23:20 -0800 | [diff] [blame] | 188 | if (allow_update_cdf) |
| 189 | update_cdf(ec_ctx->eob_flag_cdf32[plane][eob_multi_ctx], eob_pt - 1, 6); |
| 190 | break; |
| 191 | case 2: |
Yue Chen | 198738d | 2018-03-08 17:26:01 -0800 | [diff] [blame] | 192 | #if CONFIG_ENTROPY_STATS |
| 193 | ++counts->eob_multi64[cdf_idx][plane][eob_multi_ctx][eob_pt - 1]; |
| 194 | #endif |
Dake He | 0db7d0e | 2017-12-21 15:23:20 -0800 | [diff] [blame] | 195 | if (allow_update_cdf) |
| 196 | update_cdf(ec_ctx->eob_flag_cdf64[plane][eob_multi_ctx], eob_pt - 1, 7); |
| 197 | break; |
| 198 | case 3: |
Yue Chen | 198738d | 2018-03-08 17:26:01 -0800 | [diff] [blame] | 199 | #if CONFIG_ENTROPY_STATS |
| 200 | ++counts->eob_multi128[cdf_idx][plane][eob_multi_ctx][eob_pt - 1]; |
| 201 | #endif |
Hui Su | 1cb1c00 | 2018-02-05 18:21:20 -0800 | [diff] [blame] | 202 | if (allow_update_cdf) { |
Dake He | 0db7d0e | 2017-12-21 15:23:20 -0800 | [diff] [blame] | 203 | update_cdf(ec_ctx->eob_flag_cdf128[plane][eob_multi_ctx], eob_pt - 1, |
| 204 | 8); |
Hui Su | 1cb1c00 | 2018-02-05 18:21:20 -0800 | [diff] [blame] | 205 | } |
Dake He | 0db7d0e | 2017-12-21 15:23:20 -0800 | [diff] [blame] | 206 | break; |
| 207 | case 4: |
Yue Chen | 198738d | 2018-03-08 17:26:01 -0800 | [diff] [blame] | 208 | #if CONFIG_ENTROPY_STATS |
| 209 | ++counts->eob_multi256[cdf_idx][plane][eob_multi_ctx][eob_pt - 1]; |
| 210 | #endif |
Hui Su | 1cb1c00 | 2018-02-05 18:21:20 -0800 | [diff] [blame] | 211 | if (allow_update_cdf) { |
Dake He | 0db7d0e | 2017-12-21 15:23:20 -0800 | [diff] [blame] | 212 | update_cdf(ec_ctx->eob_flag_cdf256[plane][eob_multi_ctx], eob_pt - 1, |
| 213 | 9); |
Hui Su | 1cb1c00 | 2018-02-05 18:21:20 -0800 | [diff] [blame] | 214 | } |
Dake He | 0db7d0e | 2017-12-21 15:23:20 -0800 | [diff] [blame] | 215 | break; |
| 216 | case 5: |
Yue Chen | 198738d | 2018-03-08 17:26:01 -0800 | [diff] [blame] | 217 | #if CONFIG_ENTROPY_STATS |
| 218 | ++counts->eob_multi512[cdf_idx][plane][eob_multi_ctx][eob_pt - 1]; |
| 219 | #endif |
Hui Su | 1cb1c00 | 2018-02-05 18:21:20 -0800 | [diff] [blame] | 220 | if (allow_update_cdf) { |
Dake He | 0db7d0e | 2017-12-21 15:23:20 -0800 | [diff] [blame] | 221 | update_cdf(ec_ctx->eob_flag_cdf512[plane][eob_multi_ctx], eob_pt - 1, |
| 222 | 10); |
Hui Su | 1cb1c00 | 2018-02-05 18:21:20 -0800 | [diff] [blame] | 223 | } |
Dake He | 0db7d0e | 2017-12-21 15:23:20 -0800 | [diff] [blame] | 224 | break; |
| 225 | case 6: |
| 226 | default: |
Yue Chen | 198738d | 2018-03-08 17:26:01 -0800 | [diff] [blame] | 227 | #if CONFIG_ENTROPY_STATS |
| 228 | ++counts->eob_multi1024[cdf_idx][plane][eob_multi_ctx][eob_pt - 1]; |
| 229 | #endif |
Hui Su | 1cb1c00 | 2018-02-05 18:21:20 -0800 | [diff] [blame] | 230 | if (allow_update_cdf) { |
Dake He | 0db7d0e | 2017-12-21 15:23:20 -0800 | [diff] [blame] | 231 | update_cdf(ec_ctx->eob_flag_cdf1024[plane][eob_multi_ctx], eob_pt - 1, |
| 232 | 11); |
Hui Su | 1cb1c00 | 2018-02-05 18:21:20 -0800 | [diff] [blame] | 233 | } |
Dake He | 0db7d0e | 2017-12-21 15:23:20 -0800 | [diff] [blame] | 234 | break; |
| 235 | } |
Jingning Han | 00803a7 | 2017-10-25 16:04:34 -0700 | [diff] [blame] | 236 | |
Yaowu Xu | 3a19b8a | 2019-05-01 08:40:42 -0700 | [diff] [blame] | 237 | if (av1_eob_offset_bits[eob_pt] > 0) { |
Jingning Han | ff4a9f8 | 2018-06-08 10:48:45 -0700 | [diff] [blame] | 238 | int eob_ctx = eob_pt - 3; |
Yaowu Xu | 3a19b8a | 2019-05-01 08:40:42 -0700 | [diff] [blame] | 239 | int eob_shift = av1_eob_offset_bits[eob_pt] - 1; |
Angie Chiang | 7ab884e | 2017-10-18 15:57:12 -0700 | [diff] [blame] | 240 | int bit = (eob_extra & (1 << eob_shift)) ? 1 : 0; |
Hui Su | 1e95989 | 2018-01-22 12:14:43 -0800 | [diff] [blame] | 241 | #if CONFIG_ENTROPY_STATS |
Yue Chen | 198738d | 2018-03-08 17:26:01 -0800 | [diff] [blame] | 242 | counts->eob_extra[cdf_idx][txs_ctx][plane][eob_pt][bit]++; |
Hui Su | 1e95989 | 2018-01-22 12:14:43 -0800 | [diff] [blame] | 243 | #endif // CONFIG_ENTROPY_STATS |
Yunqing Wang | 0e141b5 | 2017-11-02 15:08:58 -0700 | [diff] [blame] | 244 | if (allow_update_cdf) |
Jingning Han | ff4a9f8 | 2018-06-08 10:48:45 -0700 | [diff] [blame] | 245 | update_cdf(ec_ctx->eob_extra_cdf[txs_ctx][plane][eob_ctx], bit, 2); |
Angie Chiang | 7ab884e | 2017-10-18 15:57:12 -0700 | [diff] [blame] | 246 | } |
Dake He | a47cd6c | 2017-10-13 18:09:58 -0700 | [diff] [blame] | 247 | } |
| 248 | |
Yaowu Xu | 49abec8 | 2018-03-13 16:09:01 -0700 | [diff] [blame] | 249 | static int get_eob_cost(int eob, const LV_MAP_EOB_COST *txb_eob_costs, |
Katsuhisa Yuasa | 55c95f8 | 2018-04-03 00:51:22 +0900 | [diff] [blame] | 250 | const LV_MAP_COEFF_COST *txb_costs, TX_CLASS tx_class) { |
Yaowu Xu | 49abec8 | 2018-03-13 16:09:01 -0700 | [diff] [blame] | 251 | int eob_extra; |
Linfeng Zhang | 0c72b2f | 2017-12-04 10:59:28 -0800 | [diff] [blame] | 252 | const int eob_pt = get_eob_pos_token(eob, &eob_extra); |
Dake He | a47cd6c | 2017-10-13 18:09:58 -0700 | [diff] [blame] | 253 | int eob_cost = 0; |
Katsuhisa Yuasa | 55c95f8 | 2018-04-03 00:51:22 +0900 | [diff] [blame] | 254 | const int eob_multi_ctx = (tx_class == TX_CLASS_2D) ? 0 : 1; |
Dake He | 0db7d0e | 2017-12-21 15:23:20 -0800 | [diff] [blame] | 255 | eob_cost = txb_eob_costs->eob_cost[eob_multi_ctx][eob_pt - 1]; |
Dake He | 0db7d0e | 2017-12-21 15:23:20 -0800 | [diff] [blame] | 256 | |
Yaowu Xu | 3a19b8a | 2019-05-01 08:40:42 -0700 | [diff] [blame] | 257 | if (av1_eob_offset_bits[eob_pt] > 0) { |
Jingning Han | ff4a9f8 | 2018-06-08 10:48:45 -0700 | [diff] [blame] | 258 | const int eob_ctx = eob_pt - 3; |
Yaowu Xu | 3a19b8a | 2019-05-01 08:40:42 -0700 | [diff] [blame] | 259 | const int eob_shift = av1_eob_offset_bits[eob_pt] - 1; |
Hui Su | 751a233 | 2018-01-23 11:35:03 -0800 | [diff] [blame] | 260 | const int bit = (eob_extra & (1 << eob_shift)) ? 1 : 0; |
Jingning Han | ff4a9f8 | 2018-06-08 10:48:45 -0700 | [diff] [blame] | 261 | eob_cost += txb_costs->eob_extra_cost[eob_ctx][bit]; |
Yaowu Xu | 3a19b8a | 2019-05-01 08:40:42 -0700 | [diff] [blame] | 262 | const int offset_bits = av1_eob_offset_bits[eob_pt]; |
Hui Su | 751a233 | 2018-01-23 11:35:03 -0800 | [diff] [blame] | 263 | if (offset_bits > 1) eob_cost += av1_cost_literal(offset_bits - 1); |
Dake He | a47cd6c | 2017-10-13 18:09:58 -0700 | [diff] [blame] | 264 | } |
| 265 | return eob_cost; |
| 266 | } |
| 267 | |
Angie Chiang | e4ea748 | 2018-03-15 11:36:41 -0700 | [diff] [blame] | 268 | static INLINE int get_sign_bit_cost(tran_low_t qc, int coeff_idx, |
| 269 | const int (*dc_sign_cost)[2], |
| 270 | int dc_sign_ctx) { |
Angie Chiang | e4ea748 | 2018-03-15 11:36:41 -0700 | [diff] [blame] | 271 | if (coeff_idx == 0) { |
Angie Chiang | 2f94a45 | 2018-04-03 20:21:06 -0700 | [diff] [blame] | 272 | const int sign = (qc < 0) ? 1 : 0; |
Angie Chiang | e4ea748 | 2018-03-15 11:36:41 -0700 | [diff] [blame] | 273 | return dc_sign_cost[dc_sign_ctx][sign]; |
Angie Chiang | e4ea748 | 2018-03-15 11:36:41 -0700 | [diff] [blame] | 274 | } |
Angie Chiang | 40b0731 | 2018-03-30 10:42:55 -0700 | [diff] [blame] | 275 | return av1_cost_literal(1); |
Angie Chiang | e4ea748 | 2018-03-15 11:36:41 -0700 | [diff] [blame] | 276 | } |
| 277 | |
Wenyao Liu | f7e5375 | 2019-01-22 17:34:44 +0800 | [diff] [blame] | 278 | static const int golomb_bits_cost[32] = { |
| 279 | 0, 512, 512 * 3, 512 * 3, 512 * 5, 512 * 5, 512 * 5, 512 * 5, |
| 280 | 512 * 7, 512 * 7, 512 * 7, 512 * 7, 512 * 7, 512 * 7, 512 * 7, 512 * 7, |
| 281 | 512 * 9, 512 * 9, 512 * 9, 512 * 9, 512 * 9, 512 * 9, 512 * 9, 512 * 9, |
| 282 | 512 * 9, 512 * 9, 512 * 9, 512 * 9, 512 * 9, 512 * 9, 512 * 9, 512 * 9 |
| 283 | }; |
| 284 | static const int golomb_cost_diff[32] = { |
| 285 | 0, 512, 512 * 2, 0, 512 * 2, 0, 0, 0, 512 * 2, 0, 0, 0, 0, 0, 0, 0, |
| 286 | 512 * 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 |
| 287 | }; |
| 288 | |
Angie Chiang | e4ea748 | 2018-03-15 11:36:41 -0700 | [diff] [blame] | 289 | static INLINE int get_golomb_cost(int abs_qc) { |
| 290 | if (abs_qc >= 1 + NUM_BASE_LEVELS + COEFF_BASE_RANGE) { |
| 291 | const int r = abs_qc - COEFF_BASE_RANGE - NUM_BASE_LEVELS; |
| 292 | const int length = get_msb(r) + 1; |
| 293 | return av1_cost_literal(2 * length - 1); |
| 294 | } |
| 295 | return 0; |
| 296 | } |
| 297 | |
Wenyao Liu | f7e5375 | 2019-01-22 17:34:44 +0800 | [diff] [blame] | 298 | static INLINE int get_br_cost_with_diff(tran_low_t level, const int *coeff_lps, |
| 299 | int *diff) { |
| 300 | const int base_range = AOMMIN(level - 1 - NUM_BASE_LEVELS, COEFF_BASE_RANGE); |
| 301 | int golomb_bits = 0; |
| 302 | if (level <= COEFF_BASE_RANGE + 1 + NUM_BASE_LEVELS) |
| 303 | *diff += coeff_lps[base_range + COEFF_BASE_RANGE + 1]; |
| 304 | |
| 305 | if (level >= COEFF_BASE_RANGE + 1 + NUM_BASE_LEVELS) { |
| 306 | int r = level - COEFF_BASE_RANGE - NUM_BASE_LEVELS; |
| 307 | if (r < 32) { |
| 308 | golomb_bits = golomb_bits_cost[r]; |
| 309 | *diff += golomb_cost_diff[r]; |
| 310 | } else { |
| 311 | golomb_bits = get_golomb_cost(level); |
| 312 | *diff += (r & (r - 1)) == 0 ? 1024 : 0; |
| 313 | } |
| 314 | } |
| 315 | |
| 316 | return coeff_lps[base_range] + golomb_bits; |
| 317 | } |
| 318 | |
Jim Bankoski | 24b30d7 | 2019-01-14 09:07:43 -0800 | [diff] [blame] | 319 | static INLINE int get_br_cost(tran_low_t level, const int *coeff_lps) { |
| 320 | const int base_range = AOMMIN(level - 1 - NUM_BASE_LEVELS, COEFF_BASE_RANGE); |
| 321 | return coeff_lps[base_range] + get_golomb_cost(level); |
| 322 | } |
| 323 | |
Linfeng Zhang | 1015a34 | 2017-10-24 16:20:41 -0700 | [diff] [blame] | 324 | static int get_coeff_cost(const tran_low_t qc, const int scan_idx, |
Sebastien Alaiwan | 78f7bb9 | 2018-01-11 11:02:43 +0100 | [diff] [blame] | 325 | const int is_eob, const TxbInfo *const txb_info, |
Linfeng Zhang | 5f1b8ce | 2017-12-11 15:53:10 -0800 | [diff] [blame] | 326 | const LV_MAP_COEFF_COST *const txb_costs, |
Katsuhisa Yuasa | 55c95f8 | 2018-04-03 00:51:22 +0900 | [diff] [blame] | 327 | const int coeff_ctx, const TX_CLASS tx_class) { |
Xing Jin | bd91e94 | 2018-06-21 13:43:17 +0800 | [diff] [blame] | 328 | const TXB_CTX *const txb_ctx = txb_info->txb_ctx; |
Angie Chiang | e4ea748 | 2018-03-15 11:36:41 -0700 | [diff] [blame] | 329 | const int is_nz = (qc != 0); |
| 330 | const tran_low_t abs_qc = abs(qc); |
| 331 | int cost = 0; |
| 332 | const int16_t *const scan = txb_info->scan_order->scan; |
| 333 | const int pos = scan[scan_idx]; |
| 334 | |
| 335 | if (is_eob) { |
| 336 | cost += txb_costs->base_eob_cost[coeff_ctx][AOMMIN(abs_qc, 3) - 1]; |
| 337 | } else { |
| 338 | cost += txb_costs->base_cost[coeff_ctx][AOMMIN(abs_qc, 3)]; |
| 339 | } |
| 340 | if (is_nz) { |
| 341 | cost += get_sign_bit_cost(qc, scan_idx, txb_costs->dc_sign_cost, |
| 342 | txb_ctx->dc_sign_ctx); |
| 343 | |
| 344 | if (abs_qc > NUM_BASE_LEVELS) { |
| 345 | const int ctx = |
Katsuhisa Yuasa | 55c95f8 | 2018-04-03 00:51:22 +0900 | [diff] [blame] | 346 | get_br_ctx(txb_info->levels, pos, txb_info->bwl, tx_class); |
Jim Bankoski | fe17122 | 2019-01-14 08:41:57 -0800 | [diff] [blame] | 347 | cost += get_br_cost(abs_qc, txb_costs->lps_cost[ctx]); |
Angie Chiang | e4ea748 | 2018-03-15 11:36:41 -0700 | [diff] [blame] | 348 | } |
| 349 | } |
| 350 | return cost; |
| 351 | } |
Dake He | a47cd6c | 2017-10-13 18:09:58 -0700 | [diff] [blame] | 352 | |
Frederic Barbier | e8c6752 | 2018-03-27 15:09:57 +0200 | [diff] [blame] | 353 | static INLINE int get_nz_map_ctx(const uint8_t *const levels, |
| 354 | const int coeff_idx, const int bwl, |
| 355 | const int height, const int scan_idx, |
| 356 | const int is_eob, const TX_SIZE tx_size, |
Katsuhisa Yuasa | 55c95f8 | 2018-04-03 00:51:22 +0900 | [diff] [blame] | 357 | const TX_CLASS tx_class) { |
Frederic Barbier | e8c6752 | 2018-03-27 15:09:57 +0200 | [diff] [blame] | 358 | if (is_eob) { |
| 359 | if (scan_idx == 0) return 0; |
| 360 | if (scan_idx <= (height << bwl) / 8) return 1; |
| 361 | if (scan_idx <= (height << bwl) / 4) return 2; |
| 362 | return 3; |
| 363 | } |
Frederic Barbier | e8c6752 | 2018-03-27 15:09:57 +0200 | [diff] [blame] | 364 | const int stats = |
| 365 | get_nz_mag(levels + get_padded_idx(coeff_idx, bwl), bwl, tx_class); |
| 366 | return get_nz_map_ctx_from_stats(stats, coeff_idx, bwl, tx_size, tx_class); |
| 367 | } |
| 368 | |
Linfeng Zhang | db41d1e | 2017-12-05 11:06:20 -0800 | [diff] [blame] | 369 | static void get_dist_cost_stats(LevelDownStats *const stats, const int scan_idx, |
Ola Hugosson | 1389210 | 2017-11-06 08:01:44 +0100 | [diff] [blame] | 370 | const int is_eob, |
Linfeng Zhang | db41d1e | 2017-12-05 11:06:20 -0800 | [diff] [blame] | 371 | const LV_MAP_COEFF_COST *const txb_costs, |
Katsuhisa Yuasa | 55c95f8 | 2018-04-03 00:51:22 +0900 | [diff] [blame] | 372 | const TxbInfo *const txb_info, |
| 373 | const TX_CLASS tx_class) { |
Linfeng Zhang | db41d1e | 2017-12-05 11:06:20 -0800 | [diff] [blame] | 374 | const int16_t *const scan = txb_info->scan_order->scan; |
Dake He | a47cd6c | 2017-10-13 18:09:58 -0700 | [diff] [blame] | 375 | const int coeff_idx = scan[scan_idx]; |
| 376 | const tran_low_t qc = txb_info->qcoeff[coeff_idx]; |
Linfeng Zhang | 1015a34 | 2017-10-24 16:20:41 -0700 | [diff] [blame] | 377 | const uint8_t *const levels = txb_info->levels; |
Dake He | a47cd6c | 2017-10-13 18:09:58 -0700 | [diff] [blame] | 378 | stats->new_eob = -1; |
| 379 | stats->update = 0; |
Debargha Mukherjee | e2f6b16 | 2018-01-04 17:23:05 -0800 | [diff] [blame] | 380 | stats->rd_low = 0; |
| 381 | stats->rd = 0; |
Michelle Findlay-Olynyk | fbab062 | 2017-12-13 14:10:56 -0800 | [diff] [blame] | 382 | stats->nz_rd = 0; |
Michelle Findlay-Olynyk | fbab062 | 2017-12-13 14:10:56 -0800 | [diff] [blame] | 383 | stats->dist_low = 0; |
| 384 | stats->rate_low = 0; |
| 385 | stats->low_qc = 0; |
Dake He | a47cd6c | 2017-10-13 18:09:58 -0700 | [diff] [blame] | 386 | |
| 387 | const tran_low_t tqc = txb_info->tcoeff[coeff_idx]; |
| 388 | const int dqv = txb_info->dequant[coeff_idx != 0]; |
Sebastien Alaiwan | 78f7bb9 | 2018-01-11 11:02:43 +0100 | [diff] [blame] | 389 | const int coeff_ctx = |
| 390 | get_nz_map_ctx(levels, coeff_idx, txb_info->bwl, txb_info->height, |
Katsuhisa Yuasa | 55c95f8 | 2018-04-03 00:51:22 +0900 | [diff] [blame] | 391 | scan_idx, is_eob, txb_info->tx_size, tx_class); |
| 392 | const int qc_cost = get_coeff_cost(qc, scan_idx, is_eob, txb_info, txb_costs, |
| 393 | coeff_ctx, tx_class); |
Angie Chiang | 99b1242 | 2018-03-02 17:15:00 -0800 | [diff] [blame] | 394 | assert(qc != 0); |
| 395 | const tran_low_t dqc = qcoeff_to_dqcoeff(qc, coeff_idx, dqv, txb_info->shift, |
| 396 | txb_info->iqmatrix); |
| 397 | const int64_t dqc_dist = get_coeff_dist(tqc, dqc, txb_info->shift); |
Cheng Chen | 7964120 | 2018-01-04 18:52:52 -0800 | [diff] [blame] | 398 | |
Angie Chiang | 99b1242 | 2018-03-02 17:15:00 -0800 | [diff] [blame] | 399 | // distortion difference when coefficient is quantized to 0 |
| 400 | const tran_low_t dqc0 = |
| 401 | qcoeff_to_dqcoeff(0, coeff_idx, dqv, txb_info->shift, txb_info->iqmatrix); |
Cheng Chen | 7964120 | 2018-01-04 18:52:52 -0800 | [diff] [blame] | 402 | |
Angie Chiang | 99b1242 | 2018-03-02 17:15:00 -0800 | [diff] [blame] | 403 | stats->dist0 = get_coeff_dist(tqc, dqc0, txb_info->shift); |
| 404 | stats->dist = dqc_dist - stats->dist0; |
| 405 | stats->rate = qc_cost; |
Cheng Chen | 7964120 | 2018-01-04 18:52:52 -0800 | [diff] [blame] | 406 | |
Dake He | a47cd6c | 2017-10-13 18:09:58 -0700 | [diff] [blame] | 407 | stats->rd = RDCOST(txb_info->rdmult, stats->rate, stats->dist); |
| 408 | |
| 409 | stats->low_qc = get_lower_coeff(qc); |
Dake He | a47cd6c | 2017-10-13 18:09:58 -0700 | [diff] [blame] | 410 | |
Dake He | 4d44769 | 2017-12-15 09:10:06 -0800 | [diff] [blame] | 411 | if (is_eob && stats->low_qc == 0) { |
Dake He | 4d44769 | 2017-12-15 09:10:06 -0800 | [diff] [blame] | 412 | stats->rd_low = stats->rd; // disable selection of low_qc in this case. |
Ola Hugosson | 1389210 | 2017-11-06 08:01:44 +0100 | [diff] [blame] | 413 | } else { |
Cheng Chen | 37d8873 | 2018-01-09 14:02:41 -0800 | [diff] [blame] | 414 | if (stats->low_qc == 0) { |
| 415 | stats->dist_low = 0; |
| 416 | } else { |
Frederic Barbier | e111cba | 2018-02-20 16:11:28 +0100 | [diff] [blame] | 417 | stats->low_dqc = qcoeff_to_dqcoeff(stats->low_qc, coeff_idx, dqv, |
| 418 | txb_info->shift, txb_info->iqmatrix); |
Cheng Chen | 37d8873 | 2018-01-09 14:02:41 -0800 | [diff] [blame] | 419 | const int64_t low_dqc_dist = |
| 420 | get_coeff_dist(tqc, stats->low_dqc, txb_info->shift); |
| 421 | stats->dist_low = low_dqc_dist - stats->dist0; |
| 422 | } |
Katsuhisa Yuasa | 55c95f8 | 2018-04-03 00:51:22 +0900 | [diff] [blame] | 423 | const int low_qc_cost = |
| 424 | get_coeff_cost(stats->low_qc, scan_idx, is_eob, txb_info, txb_costs, |
| 425 | coeff_ctx, tx_class); |
Dake He | 4d44769 | 2017-12-15 09:10:06 -0800 | [diff] [blame] | 426 | stats->rate_low = low_qc_cost; |
| 427 | stats->rd_low = RDCOST(txb_info->rdmult, stats->rate_low, stats->dist_low); |
Ola Hugosson | 1389210 | 2017-11-06 08:01:44 +0100 | [diff] [blame] | 428 | } |
Angie Chiang | cbbf4f0 | 2018-03-02 18:34:38 -0800 | [diff] [blame] | 429 | } |
Cheng Chen | 37d8873 | 2018-01-09 14:02:41 -0800 | [diff] [blame] | 430 | |
Angie Chiang | cbbf4f0 | 2018-03-02 18:34:38 -0800 | [diff] [blame] | 431 | static void get_dist_cost_stats_with_eob( |
| 432 | LevelDownStats *const stats, const int scan_idx, |
Katsuhisa Yuasa | 55c95f8 | 2018-04-03 00:51:22 +0900 | [diff] [blame] | 433 | const LV_MAP_COEFF_COST *const txb_costs, const TxbInfo *const txb_info, |
| 434 | const TX_CLASS tx_class) { |
Angie Chiang | cbbf4f0 | 2018-03-02 18:34:38 -0800 | [diff] [blame] | 435 | const int is_eob = 0; |
Katsuhisa Yuasa | 55c95f8 | 2018-04-03 00:51:22 +0900 | [diff] [blame] | 436 | get_dist_cost_stats(stats, scan_idx, is_eob, txb_costs, txb_info, tx_class); |
Angie Chiang | cbbf4f0 | 2018-03-02 18:34:38 -0800 | [diff] [blame] | 437 | |
| 438 | const int16_t *const scan = txb_info->scan_order->scan; |
| 439 | const int coeff_idx = scan[scan_idx]; |
| 440 | const tran_low_t qc = txb_info->qcoeff[coeff_idx]; |
| 441 | const int coeff_ctx_temp = get_nz_map_ctx( |
| 442 | txb_info->levels, coeff_idx, txb_info->bwl, txb_info->height, scan_idx, 1, |
Katsuhisa Yuasa | 55c95f8 | 2018-04-03 00:51:22 +0900 | [diff] [blame] | 443 | txb_info->tx_size, tx_class); |
| 444 | const int qc_eob_cost = get_coeff_cost(qc, scan_idx, 1, txb_info, txb_costs, |
| 445 | coeff_ctx_temp, tx_class); |
Angie Chiang | cbbf4f0 | 2018-03-02 18:34:38 -0800 | [diff] [blame] | 446 | int64_t rd_eob = RDCOST(txb_info->rdmult, qc_eob_cost, stats->dist); |
| 447 | if (stats->low_qc != 0) { |
Katsuhisa Yuasa | 55c95f8 | 2018-04-03 00:51:22 +0900 | [diff] [blame] | 448 | const int low_qc_eob_cost = |
| 449 | get_coeff_cost(stats->low_qc, scan_idx, 1, txb_info, txb_costs, |
| 450 | coeff_ctx_temp, tx_class); |
Angie Chiang | cbbf4f0 | 2018-03-02 18:34:38 -0800 | [diff] [blame] | 451 | int64_t rd_eob_low = |
| 452 | RDCOST(txb_info->rdmult, low_qc_eob_cost, stats->dist_low); |
| 453 | rd_eob = (rd_eob > rd_eob_low) ? rd_eob_low : rd_eob; |
Cheng Chen | 37d8873 | 2018-01-09 14:02:41 -0800 | [diff] [blame] | 454 | } |
Angie Chiang | cbbf4f0 | 2018-03-02 18:34:38 -0800 | [diff] [blame] | 455 | |
| 456 | stats->nz_rd = AOMMIN(stats->rd_low, stats->rd) - rd_eob; |
Dake He | a47cd6c | 2017-10-13 18:09:58 -0700 | [diff] [blame] | 457 | } |
| 458 | |
Linfeng Zhang | 1015a34 | 2017-10-24 16:20:41 -0700 | [diff] [blame] | 459 | static INLINE void update_qcoeff(const int coeff_idx, const tran_low_t qc, |
| 460 | const TxbInfo *const txb_info) { |
Dake He | a47cd6c | 2017-10-13 18:09:58 -0700 | [diff] [blame] | 461 | txb_info->qcoeff[coeff_idx] = qc; |
Linfeng Zhang | d564737 | 2017-12-05 17:06:07 -0800 | [diff] [blame] | 462 | txb_info->levels[get_padded_idx(coeff_idx, txb_info->bwl)] = |
Jingning Han | 5cb408e | 2017-11-17 14:43:39 -0800 | [diff] [blame] | 463 | (uint8_t)clamp(abs(qc), 0, INT8_MAX); |
Linfeng Zhang | 1015a34 | 2017-10-24 16:20:41 -0700 | [diff] [blame] | 464 | } |
| 465 | |
| 466 | static INLINE void update_coeff(const int coeff_idx, const tran_low_t qc, |
| 467 | const TxbInfo *const txb_info) { |
| 468 | update_qcoeff(coeff_idx, qc, txb_info); |
Dake He | a47cd6c | 2017-10-13 18:09:58 -0700 | [diff] [blame] | 469 | const int dqv = txb_info->dequant[coeff_idx != 0]; |
Frederic Barbier | e111cba | 2018-02-20 16:11:28 +0100 | [diff] [blame] | 470 | txb_info->dqcoeff[coeff_idx] = qcoeff_to_dqcoeff( |
| 471 | qc, coeff_idx, dqv, txb_info->shift, txb_info->iqmatrix); |
Dake He | a47cd6c | 2017-10-13 18:09:58 -0700 | [diff] [blame] | 472 | } |
| 473 | |
Peng Bin | 27d7ca9 | 2018-03-22 22:25:56 +0800 | [diff] [blame] | 474 | void av1_txb_init_levels_c(const tran_low_t *const coeff, const int width, |
| 475 | const int height, uint8_t *const levels) { |
Linfeng Zhang | 679d81e | 2017-10-31 15:27:42 -0700 | [diff] [blame] | 476 | const int stride = width + TX_PAD_HOR; |
Linfeng Zhang | 1122d7d | 2017-10-31 15:30:28 -0700 | [diff] [blame] | 477 | uint8_t *ls = levels; |
Linfeng Zhang | 679d81e | 2017-10-31 15:27:42 -0700 | [diff] [blame] | 478 | |
Linfeng Zhang | 1122d7d | 2017-10-31 15:30:28 -0700 | [diff] [blame] | 479 | memset(levels + stride * height, 0, |
| 480 | sizeof(*levels) * (TX_PAD_BOTTOM * stride + TX_PAD_END)); |
Linfeng Zhang | 679d81e | 2017-10-31 15:27:42 -0700 | [diff] [blame] | 481 | |
Linfeng Zhang | 1122d7d | 2017-10-31 15:30:28 -0700 | [diff] [blame] | 482 | for (int i = 0; i < height; i++) { |
| 483 | for (int j = 0; j < width; j++) { |
Jingning Han | 5cb408e | 2017-11-17 14:43:39 -0800 | [diff] [blame] | 484 | *ls++ = (uint8_t)clamp(abs(coeff[i * width + j]), 0, INT8_MAX); |
Linfeng Zhang | 1122d7d | 2017-10-31 15:30:28 -0700 | [diff] [blame] | 485 | } |
| 486 | for (int j = 0; j < TX_PAD_HOR; j++) { |
| 487 | *ls++ = 0; |
| 488 | } |
Linfeng Zhang | 1015a34 | 2017-10-24 16:20:41 -0700 | [diff] [blame] | 489 | } |
| 490 | } |
| 491 | |
Linfeng Zhang | 0ba23e8 | 2017-12-20 16:27:28 -0800 | [diff] [blame] | 492 | void av1_get_nz_map_contexts_c(const uint8_t *const levels, |
| 493 | const int16_t *const scan, const uint16_t eob, |
Katsuhisa Yuasa | 55c95f8 | 2018-04-03 00:51:22 +0900 | [diff] [blame] | 494 | const TX_SIZE tx_size, const TX_CLASS tx_class, |
Linfeng Zhang | 0ba23e8 | 2017-12-20 16:27:28 -0800 | [diff] [blame] | 495 | int8_t *const coeff_contexts) { |
Linfeng Zhang | d67c13f | 2017-12-11 11:49:12 -0800 | [diff] [blame] | 496 | const int bwl = get_txb_bwl(tx_size); |
Linfeng Zhang | d67c13f | 2017-12-11 11:49:12 -0800 | [diff] [blame] | 497 | const int height = get_txb_high(tx_size); |
Linfeng Zhang | d67c13f | 2017-12-11 11:49:12 -0800 | [diff] [blame] | 498 | for (int i = 0; i < eob; ++i) { |
| 499 | const int pos = scan[i]; |
Sebastien Alaiwan | 78f7bb9 | 2018-01-11 11:02:43 +0100 | [diff] [blame] | 500 | coeff_contexts[pos] = get_nz_map_ctx(levels, pos, bwl, height, i, |
Katsuhisa Yuasa | 55c95f8 | 2018-04-03 00:51:22 +0900 | [diff] [blame] | 501 | i == eob - 1, tx_size, tx_class); |
Linfeng Zhang | d67c13f | 2017-12-11 11:49:12 -0800 | [diff] [blame] | 502 | } |
| 503 | } |
| 504 | |
Hui Su | 179fbfa | 2019-10-07 15:13:06 -0700 | [diff] [blame] | 505 | void av1_write_coeffs_txb(const AV1_COMMON *const cm, MACROBLOCK *const x, |
Luc Trudeau | 2eb9b84 | 2017-12-13 11:19:16 -0500 | [diff] [blame] | 506 | aom_writer *w, int blk_row, int blk_col, int plane, |
Hui Su | 179fbfa | 2019-10-07 15:13:06 -0700 | [diff] [blame] | 507 | int block, TX_SIZE tx_size) { |
| 508 | MACROBLOCKD *xd = &x->e_mbd; |
| 509 | const CB_COEFF_BUFFER *cb_coef_buff = x->cb_coef_buff; |
| 510 | const int txb_offset = |
| 511 | x->mbmi_ext_frame->cb_offset / (TX_SIZE_W_MIN * TX_SIZE_H_MIN); |
| 512 | const uint16_t *eob_txb = cb_coef_buff->eobs[plane] + txb_offset; |
| 513 | const uint16_t eob = eob_txb[block]; |
| 514 | const uint8_t *entropy_ctx = cb_coef_buff->entropy_ctx[plane] + txb_offset; |
| 515 | const int txb_skip_ctx = entropy_ctx[block] & TXB_SKIP_CTX_MASK; |
Debargha Mukherjee | b3eda2f | 2017-11-28 16:00:20 -0800 | [diff] [blame] | 516 | const TX_SIZE txs_ctx = get_txsize_entropy_ctx(tx_size); |
Peng Bin | 66205b7 | 2018-09-04 15:11:08 +0800 | [diff] [blame] | 517 | FRAME_CONTEXT *ec_ctx = xd->tile_ctx; |
Hui Su | 179fbfa | 2019-10-07 15:13:06 -0700 | [diff] [blame] | 518 | aom_write_symbol(w, eob == 0, ec_ctx->txb_skip_cdf[txs_ctx][txb_skip_ctx], 2); |
Peng Bin | 66205b7 | 2018-09-04 15:11:08 +0800 | [diff] [blame] | 519 | if (eob == 0) return; |
Hui Su | 179fbfa | 2019-10-07 15:13:06 -0700 | [diff] [blame] | 520 | |
Peng Bin | 66205b7 | 2018-09-04 15:11:08 +0800 | [diff] [blame] | 521 | const PLANE_TYPE plane_type = get_plane_type(plane); |
Urvang Joshi | b6409e9 | 2020-03-23 11:23:27 -0700 | [diff] [blame] | 522 | const TX_TYPE tx_type = |
| 523 | av1_get_tx_type(xd, plane_type, blk_row, blk_col, tx_size, |
| 524 | cm->features.reduced_tx_set_used); |
Hui Su | 179fbfa | 2019-10-07 15:13:06 -0700 | [diff] [blame] | 525 | // Only y plane's tx_type is transmitted |
| 526 | if (plane == 0) { |
| 527 | av1_write_tx_type(cm, xd, tx_type, tx_size, w); |
| 528 | } |
Angie Chiang | 80b8226 | 2017-02-24 11:39:47 -0800 | [diff] [blame] | 529 | |
Yaowu Xu | 49abec8 | 2018-03-13 16:09:01 -0700 | [diff] [blame] | 530 | int eob_extra; |
Linfeng Zhang | 0c72b2f | 2017-12-04 10:59:28 -0800 | [diff] [blame] | 531 | const int eob_pt = get_eob_pos_token(eob, &eob_extra); |
Dake He | 0db7d0e | 2017-12-21 15:23:20 -0800 | [diff] [blame] | 532 | const int eob_multi_size = txsize_log2_minus4[tx_size]; |
Hui Su | 179fbfa | 2019-10-07 15:13:06 -0700 | [diff] [blame] | 533 | const TX_CLASS tx_class = tx_type_to_class[tx_type]; |
Katsuhisa Yuasa | 55c95f8 | 2018-04-03 00:51:22 +0900 | [diff] [blame] | 534 | const int eob_multi_ctx = (tx_class == TX_CLASS_2D) ? 0 : 1; |
Dake He | 0db7d0e | 2017-12-21 15:23:20 -0800 | [diff] [blame] | 535 | switch (eob_multi_size) { |
| 536 | case 0: |
| 537 | aom_write_symbol(w, eob_pt - 1, |
| 538 | ec_ctx->eob_flag_cdf16[plane_type][eob_multi_ctx], 5); |
| 539 | break; |
| 540 | case 1: |
| 541 | aom_write_symbol(w, eob_pt - 1, |
| 542 | ec_ctx->eob_flag_cdf32[plane_type][eob_multi_ctx], 6); |
| 543 | break; |
| 544 | case 2: |
| 545 | aom_write_symbol(w, eob_pt - 1, |
| 546 | ec_ctx->eob_flag_cdf64[plane_type][eob_multi_ctx], 7); |
| 547 | break; |
| 548 | case 3: |
| 549 | aom_write_symbol(w, eob_pt - 1, |
| 550 | ec_ctx->eob_flag_cdf128[plane_type][eob_multi_ctx], 8); |
| 551 | break; |
| 552 | case 4: |
| 553 | aom_write_symbol(w, eob_pt - 1, |
| 554 | ec_ctx->eob_flag_cdf256[plane_type][eob_multi_ctx], 9); |
| 555 | break; |
| 556 | case 5: |
| 557 | aom_write_symbol(w, eob_pt - 1, |
| 558 | ec_ctx->eob_flag_cdf512[plane_type][eob_multi_ctx], 10); |
| 559 | break; |
| 560 | default: |
| 561 | aom_write_symbol(w, eob_pt - 1, |
| 562 | ec_ctx->eob_flag_cdf1024[plane_type][eob_multi_ctx], 11); |
| 563 | break; |
| 564 | } |
| 565 | |
Yaowu Xu | 3a19b8a | 2019-05-01 08:40:42 -0700 | [diff] [blame] | 566 | const int eob_offset_bits = av1_eob_offset_bits[eob_pt]; |
wenyao.liu | 0be5901 | 2018-12-24 10:27:31 +0800 | [diff] [blame] | 567 | if (eob_offset_bits > 0) { |
Jingning Han | ff4a9f8 | 2018-06-08 10:48:45 -0700 | [diff] [blame] | 568 | const int eob_ctx = eob_pt - 3; |
wenyao.liu | 0be5901 | 2018-12-24 10:27:31 +0800 | [diff] [blame] | 569 | int eob_shift = eob_offset_bits - 1; |
Angie Chiang | 7ab884e | 2017-10-18 15:57:12 -0700 | [diff] [blame] | 570 | int bit = (eob_extra & (1 << eob_shift)) ? 1 : 0; |
Jingning Han | ff4a9f8 | 2018-06-08 10:48:45 -0700 | [diff] [blame] | 571 | aom_write_symbol(w, bit, |
| 572 | ec_ctx->eob_extra_cdf[txs_ctx][plane_type][eob_ctx], 2); |
wenyao.liu | 0be5901 | 2018-12-24 10:27:31 +0800 | [diff] [blame] | 573 | for (int i = 1; i < eob_offset_bits; i++) { |
| 574 | eob_shift = eob_offset_bits - 1 - i; |
Angie Chiang | 7ab884e | 2017-10-18 15:57:12 -0700 | [diff] [blame] | 575 | bit = (eob_extra & (1 << eob_shift)) ? 1 : 0; |
Dake He | a47cd6c | 2017-10-13 18:09:58 -0700 | [diff] [blame] | 576 | aom_write_bit(w, bit); |
Dake He | a47cd6c | 2017-10-13 18:09:58 -0700 | [diff] [blame] | 577 | } |
| 578 | } |
Dake He | 03a3292 | 2017-10-31 08:06:45 -0700 | [diff] [blame] | 579 | |
Hui Su | 179fbfa | 2019-10-07 15:13:06 -0700 | [diff] [blame] | 580 | const int width = get_txb_wide(tx_size); |
| 581 | const int height = get_txb_high(tx_size); |
| 582 | uint8_t levels_buf[TX_PAD_2D]; |
| 583 | uint8_t *const levels = set_levels(levels_buf, width); |
| 584 | const tran_low_t *tcoeff_txb = |
| 585 | cb_coef_buff->tcoeff[plane] + x->mbmi_ext_frame->cb_offset; |
Yaowu Xu | 18a4996 | 2019-10-16 12:47:04 -0700 | [diff] [blame] | 586 | const tran_low_t *tcoeff = tcoeff_txb + BLOCK_OFFSET(block); |
Hui Su | 179fbfa | 2019-10-07 15:13:06 -0700 | [diff] [blame] | 587 | av1_txb_init_levels(tcoeff, width, height, levels); |
| 588 | const SCAN_ORDER *const scan_order = get_scan(tx_size, tx_type); |
| 589 | const int16_t *const scan = scan_order->scan; |
| 590 | DECLARE_ALIGNED(16, int8_t, coeff_contexts[MAX_TX_SQUARE]); |
Katsuhisa Yuasa | 55c95f8 | 2018-04-03 00:51:22 +0900 | [diff] [blame] | 591 | av1_get_nz_map_contexts(levels, scan, eob, tx_size, tx_class, coeff_contexts); |
Linfeng Zhang | d67c13f | 2017-12-11 11:49:12 -0800 | [diff] [blame] | 592 | |
Hui Su | 179fbfa | 2019-10-07 15:13:06 -0700 | [diff] [blame] | 593 | const int bwl = get_txb_bwl(tx_size); |
| 594 | for (int c = eob - 1; c >= 0; --c) { |
Linfeng Zhang | db41d1e | 2017-12-05 11:06:20 -0800 | [diff] [blame] | 595 | const int pos = scan[c]; |
Linfeng Zhang | d67c13f | 2017-12-11 11:49:12 -0800 | [diff] [blame] | 596 | const int coeff_ctx = coeff_contexts[pos]; |
| 597 | const tran_low_t v = tcoeff[pos]; |
Angie Chiang | 41220ab | 2018-02-14 17:48:23 -0800 | [diff] [blame] | 598 | const tran_low_t level = abs(v); |
Dake He | 03a3292 | 2017-10-31 08:06:45 -0700 | [diff] [blame] | 599 | |
Dake He | 3fe369c | 2017-11-16 17:56:44 -0800 | [diff] [blame] | 600 | if (c == eob - 1) { |
| 601 | aom_write_symbol( |
Angie Chiang | 41220ab | 2018-02-14 17:48:23 -0800 | [diff] [blame] | 602 | w, AOMMIN(level, 3) - 1, |
Dake He | 4d44769 | 2017-12-15 09:10:06 -0800 | [diff] [blame] | 603 | ec_ctx->coeff_base_eob_cdf[txs_ctx][plane_type][coeff_ctx], 3); |
Dake He | 3fe369c | 2017-11-16 17:56:44 -0800 | [diff] [blame] | 604 | } else { |
Angie Chiang | 41220ab | 2018-02-14 17:48:23 -0800 | [diff] [blame] | 605 | aom_write_symbol(w, AOMMIN(level, 3), |
Dake He | 3fe369c | 2017-11-16 17:56:44 -0800 | [diff] [blame] | 606 | ec_ctx->coeff_base_cdf[txs_ctx][plane_type][coeff_ctx], |
| 607 | 4); |
| 608 | } |
Angie Chiang | 41220ab | 2018-02-14 17:48:23 -0800 | [diff] [blame] | 609 | if (level > NUM_BASE_LEVELS) { |
Sebastien Alaiwan | 78f7bb9 | 2018-01-11 11:02:43 +0100 | [diff] [blame] | 610 | // level is above 1. |
Linfeng Zhang | db41d1e | 2017-12-05 11:06:20 -0800 | [diff] [blame] | 611 | const int base_range = level - 1 - NUM_BASE_LEVELS; |
Katsuhisa Yuasa | 55c95f8 | 2018-04-03 00:51:22 +0900 | [diff] [blame] | 612 | const int br_ctx = get_br_ctx(levels, pos, bwl, tx_class); |
wenyao.liu | 0be5901 | 2018-12-24 10:27:31 +0800 | [diff] [blame] | 613 | aom_cdf_prob *cdf = |
| 614 | ec_ctx->coeff_br_cdf[AOMMIN(txs_ctx, TX_32X32)][plane_type][br_ctx]; |
Angie Chiang | 41220ab | 2018-02-14 17:48:23 -0800 | [diff] [blame] | 615 | for (int idx = 0; idx < COEFF_BASE_RANGE; idx += BR_CDF_SIZE - 1) { |
Linfeng Zhang | db41d1e | 2017-12-05 11:06:20 -0800 | [diff] [blame] | 616 | const int k = AOMMIN(base_range - idx, BR_CDF_SIZE - 1); |
wenyao.liu | 0be5901 | 2018-12-24 10:27:31 +0800 | [diff] [blame] | 617 | aom_write_symbol(w, k, cdf, BR_CDF_SIZE); |
Ola Hugosson | e72a209 | 2017-11-12 09:11:53 +0100 | [diff] [blame] | 618 | if (k < BR_CDF_SIZE - 1) break; |
| 619 | } |
Angie Chiang | 6d5419c | 2018-02-20 15:12:15 -0800 | [diff] [blame] | 620 | } |
| 621 | } |
| 622 | |
| 623 | // Loop to code all signs in the transform block, |
| 624 | // starting with the sign of DC (if applicable) |
Hui Su | 179fbfa | 2019-10-07 15:13:06 -0700 | [diff] [blame] | 625 | for (int c = 0; c < eob; ++c) { |
Angie Chiang | 6d5419c | 2018-02-20 15:12:15 -0800 | [diff] [blame] | 626 | const tran_low_t v = tcoeff[scan[c]]; |
| 627 | const tran_low_t level = abs(v); |
| 628 | const int sign = (v < 0) ? 1 : 0; |
| 629 | if (level) { |
| 630 | if (c == 0) { |
Hui Su | 179fbfa | 2019-10-07 15:13:06 -0700 | [diff] [blame] | 631 | const int dc_sign_ctx = |
| 632 | (entropy_ctx[block] >> DC_SIGN_CTX_SHIFT) & DC_SIGN_CTX_MASK; |
| 633 | aom_write_symbol(w, sign, ec_ctx->dc_sign_cdf[plane_type][dc_sign_ctx], |
| 634 | 2); |
Angie Chiang | 6d5419c | 2018-02-20 15:12:15 -0800 | [diff] [blame] | 635 | } else { |
| 636 | aom_write_bit(w, sign); |
| 637 | } |
Yushin Cho | 73711d5 | 2018-05-04 13:59:46 -0700 | [diff] [blame] | 638 | if (level > COEFF_BASE_RANGE + NUM_BASE_LEVELS) |
| 639 | write_golomb(w, level - COEFF_BASE_RANGE - 1 - NUM_BASE_LEVELS); |
Angie Chiang | 41220ab | 2018-02-14 17:48:23 -0800 | [diff] [blame] | 640 | } |
| 641 | } |
Angie Chiang | 80b8226 | 2017-02-24 11:39:47 -0800 | [diff] [blame] | 642 | } |
Angie Chiang | 47c7218 | 2017-02-27 14:30:38 -0800 | [diff] [blame] | 643 | |
Angie Chiang | 140b333 | 2017-12-12 17:29:25 -0800 | [diff] [blame] | 644 | typedef struct encode_txb_args { |
| 645 | const AV1_COMMON *cm; |
| 646 | MACROBLOCK *x; |
| 647 | aom_writer *w; |
| 648 | } ENCODE_TXB_ARGS; |
| 649 | |
Hui Su | d62a63a | 2020-02-27 16:59:54 -0800 | [diff] [blame] | 650 | void av1_write_coeffs_mb(const AV1_COMMON *const cm, MACROBLOCK *x, |
| 651 | aom_writer *w, BLOCK_SIZE bsize) { |
Angie Chiang | 140b333 | 2017-12-12 17:29:25 -0800 | [diff] [blame] | 652 | MACROBLOCKD *xd = &x->e_mbd; |
Jingning Han | ad54a98 | 2018-01-12 14:40:29 -0800 | [diff] [blame] | 653 | const int num_planes = av1_num_planes(cm); |
| 654 | int block[MAX_MB_PLANE] = { 0 }; |
Jingning Han | 4b48cd1 | 2018-01-11 15:56:42 -0800 | [diff] [blame] | 655 | int row, col; |
Cheng Chen | 8ab1f44 | 2018-04-27 18:01:52 -0700 | [diff] [blame] | 656 | assert(bsize == get_plane_block_size(bsize, xd->plane[0].subsampling_x, |
| 657 | xd->plane[0].subsampling_y)); |
Yushin Cho | ca63a8b | 2018-04-20 16:46:36 -0700 | [diff] [blame] | 658 | const int max_blocks_wide = max_block_wide(xd, bsize, 0); |
| 659 | const int max_blocks_high = max_block_high(xd, bsize, 0); |
| 660 | const BLOCK_SIZE max_unit_bsize = BLOCK_64X64; |
Hui Su | adda587 | 2019-12-09 10:08:49 -0800 | [diff] [blame] | 661 | int mu_blocks_wide = mi_size_wide[max_unit_bsize]; |
| 662 | int mu_blocks_high = mi_size_high[max_unit_bsize]; |
Jingning Han | 4b48cd1 | 2018-01-11 15:56:42 -0800 | [diff] [blame] | 663 | mu_blocks_wide = AOMMIN(max_blocks_wide, mu_blocks_wide); |
| 664 | mu_blocks_high = AOMMIN(max_blocks_high, mu_blocks_high); |
| 665 | |
| 666 | for (row = 0; row < max_blocks_high; row += mu_blocks_high) { |
Jingning Han | 4b48cd1 | 2018-01-11 15:56:42 -0800 | [diff] [blame] | 667 | for (col = 0; col < max_blocks_wide; col += mu_blocks_wide) { |
Jingning Han | ad54a98 | 2018-01-12 14:40:29 -0800 | [diff] [blame] | 668 | for (int plane = 0; plane < num_planes; ++plane) { |
Hui Su | 474e1e1 | 2020-02-27 15:46:36 -0800 | [diff] [blame] | 669 | if (plane && !xd->is_chroma_ref) break; |
Jingning Han | ad54a98 | 2018-01-12 14:40:29 -0800 | [diff] [blame] | 670 | const TX_SIZE tx_size = av1_get_tx_size(plane, xd); |
| 671 | const int stepr = tx_size_high_unit[tx_size]; |
| 672 | const int stepc = tx_size_wide_unit[tx_size]; |
| 673 | const int step = stepr * stepc; |
Hui Su | 474e1e1 | 2020-02-27 15:46:36 -0800 | [diff] [blame] | 674 | const struct macroblockd_plane *const pd = &xd->plane[plane]; |
Jingning Han | ad54a98 | 2018-01-12 14:40:29 -0800 | [diff] [blame] | 675 | const int unit_height = ROUND_POWER_OF_TWO( |
| 676 | AOMMIN(mu_blocks_high + row, max_blocks_high), pd->subsampling_y); |
| 677 | const int unit_width = ROUND_POWER_OF_TWO( |
| 678 | AOMMIN(mu_blocks_wide + col, max_blocks_wide), pd->subsampling_x); |
| 679 | for (int blk_row = row >> pd->subsampling_y; blk_row < unit_height; |
| 680 | blk_row += stepr) { |
| 681 | for (int blk_col = col >> pd->subsampling_x; blk_col < unit_width; |
| 682 | blk_col += stepc) { |
Hui Su | 179fbfa | 2019-10-07 15:13:06 -0700 | [diff] [blame] | 683 | av1_write_coeffs_txb(cm, x, w, blk_row, blk_col, plane, |
| 684 | block[plane], tx_size); |
Jingning Han | ad54a98 | 2018-01-12 14:40:29 -0800 | [diff] [blame] | 685 | block[plane] += step; |
| 686 | } |
Jingning Han | 4b48cd1 | 2018-01-11 15:56:42 -0800 | [diff] [blame] | 687 | } |
| 688 | } |
| 689 | } |
| 690 | } |
Angie Chiang | c8af611 | 2017-03-16 16:11:22 -0700 | [diff] [blame] | 691 | } |
| 692 | |
Urvang Joshi | 553096a | 2018-05-10 15:27:14 -0700 | [diff] [blame] | 693 | // TODO(angiebird): use this function whenever it's possible |
Yaowu Xu | 33fd0a0 | 2019-10-15 18:23:29 -0700 | [diff] [blame] | 694 | static int get_tx_type_cost(const MACROBLOCK *x, const MACROBLOCKD *xd, |
| 695 | int plane, TX_SIZE tx_size, TX_TYPE tx_type, |
| 696 | int reduced_tx_set_used) { |
Urvang Joshi | 553096a | 2018-05-10 15:27:14 -0700 | [diff] [blame] | 697 | if (plane > 0) return 0; |
| 698 | |
| 699 | const TX_SIZE square_tx_size = txsize_sqr_map[tx_size]; |
| 700 | |
| 701 | const MB_MODE_INFO *mbmi = xd->mi[0]; |
| 702 | const int is_inter = is_inter_block(mbmi); |
Yaowu Xu | 33fd0a0 | 2019-10-15 18:23:29 -0700 | [diff] [blame] | 703 | if (get_ext_tx_types(tx_size, is_inter, reduced_tx_set_used) > 1 && |
Urvang Joshi | 553096a | 2018-05-10 15:27:14 -0700 | [diff] [blame] | 704 | !xd->lossless[xd->mi[0]->segment_id]) { |
| 705 | const int ext_tx_set = |
Yaowu Xu | 33fd0a0 | 2019-10-15 18:23:29 -0700 | [diff] [blame] | 706 | get_ext_tx_set(tx_size, is_inter, reduced_tx_set_used); |
Urvang Joshi | 553096a | 2018-05-10 15:27:14 -0700 | [diff] [blame] | 707 | if (is_inter) { |
| 708 | if (ext_tx_set > 0) |
chiyotsai | 9a06d18 | 2020-05-01 17:12:12 -0700 | [diff] [blame] | 709 | return x->mode_costs |
| 710 | .inter_tx_type_costs[ext_tx_set][square_tx_size][tx_type]; |
Urvang Joshi | 553096a | 2018-05-10 15:27:14 -0700 | [diff] [blame] | 711 | } else { |
| 712 | if (ext_tx_set > 0) { |
| 713 | PREDICTION_MODE intra_dir; |
| 714 | if (mbmi->filter_intra_mode_info.use_filter_intra) |
| 715 | intra_dir = fimode_to_intradir[mbmi->filter_intra_mode_info |
| 716 | .filter_intra_mode]; |
| 717 | else |
| 718 | intra_dir = mbmi->mode; |
chiyotsai | 9a06d18 | 2020-05-01 17:12:12 -0700 | [diff] [blame] | 719 | return x->mode_costs.intra_tx_type_costs[ext_tx_set][square_tx_size] |
| 720 | [intra_dir][tx_type]; |
Urvang Joshi | 553096a | 2018-05-10 15:27:14 -0700 | [diff] [blame] | 721 | } |
| 722 | } |
| 723 | } |
| 724 | return 0; |
| 725 | } |
| 726 | |
bohanli | 0bff42b | 2020-01-09 15:21:11 -0800 | [diff] [blame] | 727 | static INLINE void update_coeff_eob_fast(int *eob, int shift, |
| 728 | const int16_t *dequant_ptr, |
| 729 | const int16_t *scan, |
| 730 | const tran_low_t *coeff_ptr, |
| 731 | tran_low_t *qcoeff_ptr, |
| 732 | tran_low_t *dqcoeff_ptr) { |
| 733 | // TODO(sarahparker) make this work for aomqm |
| 734 | int eob_out = *eob; |
| 735 | int zbin[2] = { dequant_ptr[0] + ROUND_POWER_OF_TWO(dequant_ptr[0] * 70, 7), |
| 736 | dequant_ptr[1] + ROUND_POWER_OF_TWO(dequant_ptr[1] * 70, 7) }; |
| 737 | |
| 738 | for (int i = *eob - 1; i >= 0; i--) { |
| 739 | const int rc = scan[i]; |
| 740 | const int qcoeff = qcoeff_ptr[rc]; |
| 741 | const int coeff = coeff_ptr[rc]; |
Yaowu Xu | 9db5b8d | 2020-04-10 08:58:15 -0700 | [diff] [blame] | 742 | const int coeff_sign = AOMSIGN(coeff); |
bohanli | 0bff42b | 2020-01-09 15:21:11 -0800 | [diff] [blame] | 743 | int64_t abs_coeff = (coeff ^ coeff_sign) - coeff_sign; |
| 744 | |
| 745 | if (((abs_coeff << (1 + shift)) < zbin[rc != 0]) || (qcoeff == 0)) { |
| 746 | eob_out--; |
| 747 | qcoeff_ptr[rc] = 0; |
| 748 | dqcoeff_ptr[rc] = 0; |
| 749 | } else { |
| 750 | break; |
| 751 | } |
| 752 | } |
| 753 | |
| 754 | *eob = eob_out; |
| 755 | } |
| 756 | |
Katsuhisa Yuasa | 5146703 | 2018-04-08 16:16:10 +0900 | [diff] [blame] | 757 | static AOM_FORCE_INLINE int warehouse_efficients_txb( |
bohanli | 3613e5d | 2019-10-25 13:10:11 -0700 | [diff] [blame] | 758 | const MACROBLOCK *x, const int plane, const int block, |
| 759 | const TX_SIZE tx_size, const TXB_CTX *const txb_ctx, |
Katsuhisa Yuasa | 5146703 | 2018-04-08 16:16:10 +0900 | [diff] [blame] | 760 | const struct macroblock_plane *p, const int eob, |
| 761 | const PLANE_TYPE plane_type, const LV_MAP_COEFF_COST *const coeff_costs, |
bohanli | 3613e5d | 2019-10-25 13:10:11 -0700 | [diff] [blame] | 762 | const MACROBLOCKD *const xd, const TX_TYPE tx_type, const TX_CLASS tx_class, |
| 763 | int reduced_tx_set_used) { |
Yaowu Xu | 18a4996 | 2019-10-16 12:47:04 -0700 | [diff] [blame] | 764 | const tran_low_t *const qcoeff = p->qcoeff + BLOCK_OFFSET(block); |
Linfeng Zhang | c02b411 | 2017-12-21 13:11:36 -0800 | [diff] [blame] | 765 | const int txb_skip_ctx = txb_ctx->txb_skip_ctx; |
Angie Chiang | a9ba58e | 2017-12-01 19:22:43 -0800 | [diff] [blame] | 766 | const int bwl = get_txb_bwl(tx_size); |
| 767 | const int width = get_txb_wide(tx_size); |
| 768 | const int height = get_txb_high(tx_size); |
Yaowu Xu | f1e1293 | 2018-02-26 15:50:09 -0800 | [diff] [blame] | 769 | const SCAN_ORDER *const scan_order = get_scan(tx_size, tx_type); |
Linfeng Zhang | db41d1e | 2017-12-05 11:06:20 -0800 | [diff] [blame] | 770 | const int16_t *const scan = scan_order->scan; |
Linfeng Zhang | 679d81e | 2017-10-31 15:27:42 -0700 | [diff] [blame] | 771 | uint8_t levels_buf[TX_PAD_2D]; |
| 772 | uint8_t *const levels = set_levels(levels_buf, width); |
Linfeng Zhang | d67c13f | 2017-12-11 11:49:12 -0800 | [diff] [blame] | 773 | DECLARE_ALIGNED(16, int8_t, coeff_contexts[MAX_TX_SQUARE]); |
Dake He | 0db7d0e | 2017-12-21 15:23:20 -0800 | [diff] [blame] | 774 | const int eob_multi_size = txsize_log2_minus4[tx_size]; |
| 775 | const LV_MAP_EOB_COST *const eob_costs = |
chiyotsai | 9a06d18 | 2020-05-01 17:12:12 -0700 | [diff] [blame] | 776 | &x->coeff_costs.eob_costs[eob_multi_size][plane_type]; |
Hui Su | 1820554 | 2018-03-30 11:37:41 -0700 | [diff] [blame] | 777 | int cost = coeff_costs->txb_skip_cost[txb_skip_ctx][0]; |
Angie Chiang | 47c7218 | 2017-02-27 14:30:38 -0800 | [diff] [blame] | 778 | |
Linfeng Zhang | 1122d7d | 2017-10-31 15:30:28 -0700 | [diff] [blame] | 779 | av1_txb_init_levels(qcoeff, width, height, levels); |
Linfeng Zhang | 1015a34 | 2017-10-24 16:20:41 -0700 | [diff] [blame] | 780 | |
bohanli | 3613e5d | 2019-10-25 13:10:11 -0700 | [diff] [blame] | 781 | cost += get_tx_type_cost(x, xd, plane, tx_size, tx_type, reduced_tx_set_used); |
Angie Chiang | 0591787 | 2017-04-15 12:28:56 -0700 | [diff] [blame] | 782 | |
Katsuhisa Yuasa | 55c95f8 | 2018-04-03 00:51:22 +0900 | [diff] [blame] | 783 | cost += get_eob_cost(eob, eob_costs, coeff_costs, tx_class); |
Linfeng Zhang | d67c13f | 2017-12-11 11:49:12 -0800 | [diff] [blame] | 784 | |
Katsuhisa Yuasa | 55c95f8 | 2018-04-03 00:51:22 +0900 | [diff] [blame] | 785 | av1_get_nz_map_contexts(levels, scan, eob, tx_size, tx_class, coeff_contexts); |
Linfeng Zhang | d67c13f | 2017-12-11 11:49:12 -0800 | [diff] [blame] | 786 | |
Wenyao Liu | f7e5375 | 2019-01-22 17:34:44 +0800 | [diff] [blame] | 787 | const int(*lps_cost)[COEFF_BASE_RANGE + 1 + COEFF_BASE_RANGE + 1] = |
| 788 | coeff_costs->lps_cost; |
Katsuhisa Yuasa | 5146703 | 2018-04-08 16:16:10 +0900 | [diff] [blame] | 789 | int c = eob - 1; |
| 790 | { |
Linfeng Zhang | db41d1e | 2017-12-05 11:06:20 -0800 | [diff] [blame] | 791 | const int pos = scan[c]; |
| 792 | const tran_low_t v = qcoeff[pos]; |
Yaowu Xu | 9db5b8d | 2020-04-10 08:58:15 -0700 | [diff] [blame] | 793 | const int sign = AOMSIGN(v); |
Katsuhisa Yuasa | 5146703 | 2018-04-08 16:16:10 +0900 | [diff] [blame] | 794 | const int level = (v ^ sign) - sign; |
Linfeng Zhang | d67c13f | 2017-12-11 11:49:12 -0800 | [diff] [blame] | 795 | const int coeff_ctx = coeff_contexts[pos]; |
Katsuhisa Yuasa | 5146703 | 2018-04-08 16:16:10 +0900 | [diff] [blame] | 796 | cost += coeff_costs->base_eob_cost[coeff_ctx][AOMMIN(level, 3) - 1]; |
Dake He | a47cd6c | 2017-10-13 18:09:58 -0700 | [diff] [blame] | 797 | |
Katsuhisa Yuasa | 5146703 | 2018-04-08 16:16:10 +0900 | [diff] [blame] | 798 | if (v) { |
Dake He | a47cd6c | 2017-10-13 18:09:58 -0700 | [diff] [blame] | 799 | // sign bit cost |
Dake He | a47cd6c | 2017-10-13 18:09:58 -0700 | [diff] [blame] | 800 | if (level > NUM_BASE_LEVELS) { |
Jim Bankoski | fe17122 | 2019-01-14 08:41:57 -0800 | [diff] [blame] | 801 | const int ctx = get_br_ctx_eob(pos, bwl, tx_class); |
| 802 | cost += get_br_cost(level, lps_cost[ctx]); |
Katsuhisa Yuasa | 5146703 | 2018-04-08 16:16:10 +0900 | [diff] [blame] | 803 | } |
| 804 | if (c) { |
| 805 | cost += av1_cost_literal(1); |
| 806 | } else { |
| 807 | const int sign01 = (sign ^ sign) - sign; |
| 808 | const int dc_sign_ctx = txb_ctx->dc_sign_ctx; |
| 809 | cost += coeff_costs->dc_sign_cost[dc_sign_ctx][sign01]; |
| 810 | return cost; |
| 811 | } |
| 812 | } |
| 813 | } |
Wenyao Liu | f7e5375 | 2019-01-22 17:34:44 +0800 | [diff] [blame] | 814 | const int(*base_cost)[8] = coeff_costs->base_cost; |
Katsuhisa Yuasa | 5146703 | 2018-04-08 16:16:10 +0900 | [diff] [blame] | 815 | for (c = eob - 2; c >= 1; --c) { |
| 816 | const int pos = scan[c]; |
| 817 | const int coeff_ctx = coeff_contexts[pos]; |
| 818 | const tran_low_t v = qcoeff[pos]; |
| 819 | const int level = abs(v); |
bohanli | 3613e5d | 2019-10-25 13:10:11 -0700 | [diff] [blame] | 820 | cost += base_cost[coeff_ctx][AOMMIN(level, 3)]; |
Katsuhisa Yuasa | 5146703 | 2018-04-08 16:16:10 +0900 | [diff] [blame] | 821 | if (v) { |
| 822 | // sign bit cost |
| 823 | cost += av1_cost_literal(1); |
| 824 | if (level > NUM_BASE_LEVELS) { |
| 825 | const int ctx = get_br_ctx(levels, pos, bwl, tx_class); |
Jim Bankoski | fe17122 | 2019-01-14 08:41:57 -0800 | [diff] [blame] | 826 | cost += get_br_cost(level, lps_cost[ctx]); |
Katsuhisa Yuasa | 5146703 | 2018-04-08 16:16:10 +0900 | [diff] [blame] | 827 | } |
| 828 | } |
Katsuhisa Yuasa | 5146703 | 2018-04-08 16:16:10 +0900 | [diff] [blame] | 829 | } |
bohanli | 3613e5d | 2019-10-25 13:10:11 -0700 | [diff] [blame] | 830 | // c == 0 after previous loop |
| 831 | { |
Katsuhisa Yuasa | 5146703 | 2018-04-08 16:16:10 +0900 | [diff] [blame] | 832 | const int pos = scan[c]; |
| 833 | const tran_low_t v = qcoeff[pos]; |
| 834 | const int coeff_ctx = coeff_contexts[pos]; |
Yaowu Xu | 9db5b8d | 2020-04-10 08:58:15 -0700 | [diff] [blame] | 835 | const int sign = AOMSIGN(v); |
Katsuhisa Yuasa | 5146703 | 2018-04-08 16:16:10 +0900 | [diff] [blame] | 836 | const int level = (v ^ sign) - sign; |
| 837 | cost += base_cost[coeff_ctx][AOMMIN(level, 3)]; |
Dake He | a47cd6c | 2017-10-13 18:09:58 -0700 | [diff] [blame] | 838 | |
Katsuhisa Yuasa | 5146703 | 2018-04-08 16:16:10 +0900 | [diff] [blame] | 839 | if (v) { |
| 840 | // sign bit cost |
| 841 | const int sign01 = (sign ^ sign) - sign; |
| 842 | const int dc_sign_ctx = txb_ctx->dc_sign_ctx; |
| 843 | cost += coeff_costs->dc_sign_cost[dc_sign_ctx][sign01]; |
| 844 | if (level > NUM_BASE_LEVELS) { |
| 845 | const int ctx = get_br_ctx(levels, pos, bwl, tx_class); |
Jim Bankoski | fe17122 | 2019-01-14 08:41:57 -0800 | [diff] [blame] | 846 | cost += get_br_cost(level, lps_cost[ctx]); |
Dake He | a47cd6c | 2017-10-13 18:09:58 -0700 | [diff] [blame] | 847 | } |
| 848 | } |
| 849 | } |
Angie Chiang | 47c7218 | 2017-02-27 14:30:38 -0800 | [diff] [blame] | 850 | return cost; |
| 851 | } |
Angie Chiang | 0397eda | 2017-03-15 16:57:14 -0700 | [diff] [blame] | 852 | |
bohanli | 0bff42b | 2020-01-09 15:21:11 -0800 | [diff] [blame] | 853 | static AOM_FORCE_INLINE int warehouse_efficients_txb_laplacian( |
| 854 | const MACROBLOCK *x, const int plane, const int block, |
| 855 | const TX_SIZE tx_size, const TXB_CTX *const txb_ctx, const int eob, |
| 856 | const PLANE_TYPE plane_type, const LV_MAP_COEFF_COST *const coeff_costs, |
| 857 | const MACROBLOCKD *const xd, const TX_TYPE tx_type, const TX_CLASS tx_class, |
| 858 | int reduced_tx_set_used) { |
| 859 | const int txb_skip_ctx = txb_ctx->txb_skip_ctx; |
| 860 | |
| 861 | const int eob_multi_size = txsize_log2_minus4[tx_size]; |
| 862 | const LV_MAP_EOB_COST *const eob_costs = |
chiyotsai | 9a06d18 | 2020-05-01 17:12:12 -0700 | [diff] [blame] | 863 | &x->coeff_costs.eob_costs[eob_multi_size][plane_type]; |
bohanli | 0bff42b | 2020-01-09 15:21:11 -0800 | [diff] [blame] | 864 | int cost = coeff_costs->txb_skip_cost[txb_skip_ctx][0]; |
| 865 | |
| 866 | cost += get_tx_type_cost(x, xd, plane, tx_size, tx_type, reduced_tx_set_used); |
| 867 | |
| 868 | cost += get_eob_cost(eob, eob_costs, coeff_costs, tx_class); |
| 869 | |
| 870 | cost += av1_cost_coeffs_txb_estimate(x, plane, block, tx_size, tx_type); |
| 871 | return cost; |
| 872 | } |
| 873 | |
| 874 | // Look up table of individual cost of coefficient by its quantization level. |
| 875 | // determined based on Laplacian distribution conditioned on estimated context |
| 876 | static const int costLUT[15] = { -1143, 53, 545, 825, 1031, |
| 877 | 1209, 1393, 1577, 1763, 1947, |
| 878 | 2132, 2317, 2501, 2686, 2871 }; |
| 879 | static const int const_term = (1 << AV1_PROB_COST_SHIFT); |
| 880 | static const int loge_par = ((14427 << AV1_PROB_COST_SHIFT) + 5000) / 10000; |
| 881 | int av1_cost_coeffs_txb_estimate(const MACROBLOCK *x, const int plane, |
| 882 | const int block, const TX_SIZE tx_size, |
| 883 | const TX_TYPE tx_type) { |
| 884 | assert(plane == 0); |
| 885 | |
| 886 | int cost = 0; |
| 887 | const struct macroblock_plane *p = &x->plane[plane]; |
| 888 | const SCAN_ORDER *scan_order = get_scan(tx_size, tx_type); |
| 889 | const int16_t *scan = scan_order->scan; |
| 890 | tran_low_t *qcoeff = p->qcoeff + BLOCK_OFFSET(block); |
| 891 | |
| 892 | int eob = p->eobs[block]; |
| 893 | |
| 894 | // coeffs |
| 895 | int c = eob - 1; |
| 896 | // eob |
| 897 | { |
| 898 | const int pos = scan[c]; |
| 899 | const tran_low_t v = abs(qcoeff[pos]) - 1; |
| 900 | cost += (v << (AV1_PROB_COST_SHIFT + 2)); |
| 901 | } |
| 902 | // other coeffs |
| 903 | for (c = eob - 2; c >= 0; c--) { |
| 904 | const int pos = scan[c]; |
| 905 | const tran_low_t v = abs(qcoeff[pos]); |
| 906 | const int idx = AOMMIN(v, 14); |
| 907 | |
| 908 | cost += costLUT[idx]; |
| 909 | } |
| 910 | |
| 911 | // const_term does not contain DC, and log(e) does not contain eob, so both |
| 912 | // (eob-1) |
| 913 | cost += (const_term + loge_par) * (eob - 1); |
| 914 | |
| 915 | return cost; |
| 916 | } |
| 917 | |
bohanli | 3613e5d | 2019-10-25 13:10:11 -0700 | [diff] [blame] | 918 | int av1_cost_coeffs_txb(const MACROBLOCK *x, const int plane, const int block, |
| 919 | const TX_SIZE tx_size, const TX_TYPE tx_type, |
| 920 | const TXB_CTX *const txb_ctx, int reduced_tx_set_used) { |
Katsuhisa Yuasa | 5146703 | 2018-04-08 16:16:10 +0900 | [diff] [blame] | 921 | const struct macroblock_plane *p = &x->plane[plane]; |
| 922 | const int eob = p->eobs[block]; |
| 923 | const TX_SIZE txs_ctx = get_txsize_entropy_ctx(tx_size); |
| 924 | const PLANE_TYPE plane_type = get_plane_type(plane); |
| 925 | const LV_MAP_COEFF_COST *const coeff_costs = |
chiyotsai | 9a06d18 | 2020-05-01 17:12:12 -0700 | [diff] [blame] | 926 | &x->coeff_costs.coeff_costs[txs_ctx][plane_type]; |
Katsuhisa Yuasa | 5146703 | 2018-04-08 16:16:10 +0900 | [diff] [blame] | 927 | if (eob == 0) { |
| 928 | return coeff_costs->txb_skip_cost[txb_ctx->txb_skip_ctx][1]; |
| 929 | } |
| 930 | |
| 931 | const MACROBLOCKD *const xd = &x->e_mbd; |
Katsuhisa Yuasa | 5146703 | 2018-04-08 16:16:10 +0900 | [diff] [blame] | 932 | const TX_CLASS tx_class = tx_type_to_class[tx_type]; |
| 933 | |
bohanli | 3613e5d | 2019-10-25 13:10:11 -0700 | [diff] [blame] | 934 | return warehouse_efficients_txb(x, plane, block, tx_size, txb_ctx, p, eob, |
Yaowu Xu | 109ec9b | 2019-10-15 17:19:24 -0700 | [diff] [blame] | 935 | plane_type, coeff_costs, xd, tx_type, |
bohanli | 3613e5d | 2019-10-25 13:10:11 -0700 | [diff] [blame] | 936 | tx_class, reduced_tx_set_used); |
Katsuhisa Yuasa | 5146703 | 2018-04-08 16:16:10 +0900 | [diff] [blame] | 937 | } |
| 938 | |
bohanli | 736644d | 2020-01-14 09:30:37 -0800 | [diff] [blame] | 939 | int av1_cost_coeffs_txb_laplacian(const MACROBLOCK *x, const int plane, |
| 940 | const int block, const TX_SIZE tx_size, |
| 941 | const TX_TYPE tx_type, |
| 942 | const TXB_CTX *const txb_ctx, |
| 943 | const int reduced_tx_set_used, |
| 944 | const int adjust_eob) { |
bohanli | 0bff42b | 2020-01-09 15:21:11 -0800 | [diff] [blame] | 945 | const struct macroblock_plane *p = &x->plane[plane]; |
| 946 | int eob = p->eobs[block]; |
| 947 | |
| 948 | if (adjust_eob) { |
| 949 | const SCAN_ORDER *scan_order = get_scan(tx_size, tx_type); |
| 950 | const int16_t *scan = scan_order->scan; |
| 951 | tran_low_t *tcoeff = p->coeff + BLOCK_OFFSET(block); |
| 952 | tran_low_t *qcoeff = p->qcoeff + BLOCK_OFFSET(block); |
Urvang Joshi | 9543ad7 | 2020-04-17 16:59:46 -0700 | [diff] [blame] | 953 | tran_low_t *dqcoeff = p->dqcoeff + BLOCK_OFFSET(block); |
bohanli | 0bff42b | 2020-01-09 15:21:11 -0800 | [diff] [blame] | 954 | update_coeff_eob_fast(&eob, av1_get_tx_scale(tx_size), p->dequant_QTX, scan, |
| 955 | tcoeff, qcoeff, dqcoeff); |
| 956 | p->eobs[block] = eob; |
| 957 | } |
| 958 | |
| 959 | const TX_SIZE txs_ctx = get_txsize_entropy_ctx(tx_size); |
| 960 | const PLANE_TYPE plane_type = get_plane_type(plane); |
| 961 | const LV_MAP_COEFF_COST *const coeff_costs = |
chiyotsai | 9a06d18 | 2020-05-01 17:12:12 -0700 | [diff] [blame] | 962 | &x->coeff_costs.coeff_costs[txs_ctx][plane_type]; |
bohanli | 0bff42b | 2020-01-09 15:21:11 -0800 | [diff] [blame] | 963 | if (eob == 0) { |
| 964 | return coeff_costs->txb_skip_cost[txb_ctx->txb_skip_ctx][1]; |
| 965 | } |
| 966 | |
| 967 | const MACROBLOCKD *const xd = &x->e_mbd; |
| 968 | const TX_CLASS tx_class = tx_type_to_class[tx_type]; |
| 969 | |
| 970 | return warehouse_efficients_txb_laplacian( |
| 971 | x, plane, block, tx_size, txb_ctx, eob, plane_type, coeff_costs, xd, |
| 972 | tx_type, tx_class, reduced_tx_set_used); |
| 973 | } |
| 974 | |
Dake He | a47cd6c | 2017-10-13 18:09:58 -0700 | [diff] [blame] | 975 | static int optimize_txb(TxbInfo *txb_info, const LV_MAP_COEFF_COST *txb_costs, |
Angie Chiang | 45385b8 | 2018-03-02 15:25:58 -0800 | [diff] [blame] | 976 | const LV_MAP_EOB_COST *txb_eob_costs, int *rate_cost) { |
Dake He | a47cd6c | 2017-10-13 18:09:58 -0700 | [diff] [blame] | 977 | int update = 0; |
| 978 | if (txb_info->eob == 0) return update; |
Linfeng Zhang | db41d1e | 2017-12-05 11:06:20 -0800 | [diff] [blame] | 979 | const int16_t *const scan = txb_info->scan_order->scan; |
Dake He | a47cd6c | 2017-10-13 18:09:58 -0700 | [diff] [blame] | 980 | // forward optimize the nz_map` |
| 981 | const int init_eob = txb_info->eob; |
Katsuhisa Yuasa | 55c95f8 | 2018-04-03 00:51:22 +0900 | [diff] [blame] | 982 | const TX_CLASS tx_class = tx_type_to_class[txb_info->tx_type]; |
Yaowu Xu | 49abec8 | 2018-03-13 16:09:01 -0700 | [diff] [blame] | 983 | const int eob_cost = |
Katsuhisa Yuasa | 55c95f8 | 2018-04-03 00:51:22 +0900 | [diff] [blame] | 984 | get_eob_cost(init_eob, txb_eob_costs, txb_costs, tx_class); |
Dake He | a47cd6c | 2017-10-13 18:09:58 -0700 | [diff] [blame] | 985 | |
| 986 | // backward optimize the level-k map |
Cheng Chen | 82775f6 | 2018-01-18 12:09:54 -0800 | [diff] [blame] | 987 | int accu_rate = eob_cost; |
Dake He | a47cd6c | 2017-10-13 18:09:58 -0700 | [diff] [blame] | 988 | int64_t accu_dist = 0; |
| 989 | int64_t prev_eob_rd_cost = INT64_MAX; |
| 990 | int64_t cur_eob_rd_cost = 0; |
| 991 | |
Angie Chiang | cbbf4f0 | 2018-03-02 18:34:38 -0800 | [diff] [blame] | 992 | { |
| 993 | const int si = init_eob - 1; |
| 994 | const int coeff_idx = scan[si]; |
| 995 | LevelDownStats stats; |
Katsuhisa Yuasa | 55c95f8 | 2018-04-03 00:51:22 +0900 | [diff] [blame] | 996 | get_dist_cost_stats(&stats, si, si == init_eob - 1, txb_costs, txb_info, |
| 997 | tx_class); |
Angie Chiang | cbbf4f0 | 2018-03-02 18:34:38 -0800 | [diff] [blame] | 998 | if ((stats.rd_low < stats.rd) && (stats.low_qc != 0)) { |
| 999 | update = 1; |
| 1000 | update_coeff(coeff_idx, stats.low_qc, txb_info); |
| 1001 | accu_rate += stats.rate_low; |
| 1002 | accu_dist += stats.dist_low; |
| 1003 | } else { |
| 1004 | accu_rate += stats.rate; |
| 1005 | accu_dist += stats.dist; |
| 1006 | } |
| 1007 | } |
| 1008 | |
| 1009 | int si = init_eob - 2; |
| 1010 | int8_t has_nz_tail = 0; |
| 1011 | // eob is not fixed |
| 1012 | for (; si >= 0 && has_nz_tail < 2; --si) { |
| 1013 | assert(si != init_eob - 1); |
Dake He | a47cd6c | 2017-10-13 18:09:58 -0700 | [diff] [blame] | 1014 | const int coeff_idx = scan[si]; |
| 1015 | tran_low_t qc = txb_info->qcoeff[coeff_idx]; |
| 1016 | |
Dake He | a47cd6c | 2017-10-13 18:09:58 -0700 | [diff] [blame] | 1017 | if (qc == 0) { |
Angie Chiang | 99b1242 | 2018-03-02 17:15:00 -0800 | [diff] [blame] | 1018 | const int coeff_ctx = |
| 1019 | get_lower_levels_ctx(txb_info->levels, coeff_idx, txb_info->bwl, |
Katsuhisa Yuasa | 55c95f8 | 2018-04-03 00:51:22 +0900 | [diff] [blame] | 1020 | txb_info->tx_size, tx_class); |
Angie Chiang | 99b1242 | 2018-03-02 17:15:00 -0800 | [diff] [blame] | 1021 | accu_rate += txb_costs->base_cost[coeff_ctx][0]; |
Dake He | a47cd6c | 2017-10-13 18:09:58 -0700 | [diff] [blame] | 1022 | } else { |
Angie Chiang | 99b1242 | 2018-03-02 17:15:00 -0800 | [diff] [blame] | 1023 | LevelDownStats stats; |
Katsuhisa Yuasa | 55c95f8 | 2018-04-03 00:51:22 +0900 | [diff] [blame] | 1024 | get_dist_cost_stats_with_eob(&stats, si, txb_costs, txb_info, tx_class); |
Angie Chiang | cbbf4f0 | 2018-03-02 18:34:38 -0800 | [diff] [blame] | 1025 | // check if it is better to make this the last significant coefficient |
Yaowu Xu | 49abec8 | 2018-03-13 16:09:01 -0700 | [diff] [blame] | 1026 | int cur_eob_rate = |
Katsuhisa Yuasa | 55c95f8 | 2018-04-03 00:51:22 +0900 | [diff] [blame] | 1027 | get_eob_cost(si + 1, txb_eob_costs, txb_costs, tx_class); |
Angie Chiang | cbbf4f0 | 2018-03-02 18:34:38 -0800 | [diff] [blame] | 1028 | cur_eob_rd_cost = RDCOST(txb_info->rdmult, cur_eob_rate, 0); |
| 1029 | prev_eob_rd_cost = |
| 1030 | RDCOST(txb_info->rdmult, accu_rate, accu_dist) + stats.nz_rd; |
| 1031 | if (cur_eob_rd_cost <= prev_eob_rd_cost) { |
| 1032 | update = 1; |
| 1033 | for (int j = si + 1; j < txb_info->eob; j++) { |
| 1034 | const int coeff_pos_j = scan[j]; |
| 1035 | update_coeff(coeff_pos_j, 0, txb_info); |
| 1036 | } |
| 1037 | txb_info->eob = si + 1; |
Angie Chiang | 99b1242 | 2018-03-02 17:15:00 -0800 | [diff] [blame] | 1038 | |
Angie Chiang | cbbf4f0 | 2018-03-02 18:34:38 -0800 | [diff] [blame] | 1039 | // rerun cost calculation due to change of eob |
| 1040 | accu_rate = cur_eob_rate; |
| 1041 | accu_dist = 0; |
Katsuhisa Yuasa | 55c95f8 | 2018-04-03 00:51:22 +0900 | [diff] [blame] | 1042 | get_dist_cost_stats(&stats, si, 1, txb_costs, txb_info, tx_class); |
Angie Chiang | cbbf4f0 | 2018-03-02 18:34:38 -0800 | [diff] [blame] | 1043 | if ((stats.rd_low < stats.rd) && (stats.low_qc != 0)) { |
| 1044 | update = 1; |
| 1045 | update_coeff(coeff_idx, stats.low_qc, txb_info); |
| 1046 | accu_rate += stats.rate_low; |
| 1047 | accu_dist += stats.dist_low; |
Cheng Chen | ec32a74 | 2018-01-12 19:09:39 -0800 | [diff] [blame] | 1048 | } else { |
Angie Chiang | cbbf4f0 | 2018-03-02 18:34:38 -0800 | [diff] [blame] | 1049 | accu_rate += stats.rate; |
| 1050 | accu_dist += stats.dist; |
| 1051 | } |
| 1052 | |
| 1053 | // reset non zero tail when new eob is found |
| 1054 | has_nz_tail = 0; |
| 1055 | } else { |
| 1056 | int bUpdCoeff = 0; |
| 1057 | if (stats.rd_low < stats.rd) { |
| 1058 | if ((si < txb_info->eob - 1)) { |
| 1059 | bUpdCoeff = 1; |
Cheng Chen | ec32a74 | 2018-01-12 19:09:39 -0800 | [diff] [blame] | 1060 | update = 1; |
Cheng Chen | ec32a74 | 2018-01-12 19:09:39 -0800 | [diff] [blame] | 1061 | } |
Angie Chiang | cbbf4f0 | 2018-03-02 18:34:38 -0800 | [diff] [blame] | 1062 | } else { |
| 1063 | ++has_nz_tail; |
| 1064 | } |
| 1065 | |
| 1066 | if (bUpdCoeff) { |
| 1067 | update_coeff(coeff_idx, stats.low_qc, txb_info); |
| 1068 | accu_rate += stats.rate_low; |
| 1069 | accu_dist += stats.dist_low; |
| 1070 | } else { |
| 1071 | accu_rate += stats.rate; |
| 1072 | accu_dist += stats.dist; |
Jingning Han | 8be58fa | 2017-12-18 09:46:13 -0800 | [diff] [blame] | 1073 | } |
Dake He | a47cd6c | 2017-10-13 18:09:58 -0700 | [diff] [blame] | 1074 | } |
Angie Chiang | cbbf4f0 | 2018-03-02 18:34:38 -0800 | [diff] [blame] | 1075 | } |
| 1076 | } // for (si) |
| 1077 | |
| 1078 | // eob is fixed |
| 1079 | for (; si >= 0; --si) { |
| 1080 | assert(si != init_eob - 1); |
| 1081 | const int coeff_idx = scan[si]; |
| 1082 | tran_low_t qc = txb_info->qcoeff[coeff_idx]; |
| 1083 | |
| 1084 | if (qc == 0) { |
| 1085 | const int coeff_ctx = |
| 1086 | get_lower_levels_ctx(txb_info->levels, coeff_idx, txb_info->bwl, |
Katsuhisa Yuasa | 55c95f8 | 2018-04-03 00:51:22 +0900 | [diff] [blame] | 1087 | txb_info->tx_size, tx_class); |
Angie Chiang | cbbf4f0 | 2018-03-02 18:34:38 -0800 | [diff] [blame] | 1088 | accu_rate += txb_costs->base_cost[coeff_ctx][0]; |
| 1089 | } else { |
| 1090 | LevelDownStats stats; |
Katsuhisa Yuasa | 55c95f8 | 2018-04-03 00:51:22 +0900 | [diff] [blame] | 1091 | get_dist_cost_stats(&stats, si, 0, txb_costs, txb_info, tx_class); |
Dake He | a47cd6c | 2017-10-13 18:09:58 -0700 | [diff] [blame] | 1092 | |
| 1093 | int bUpdCoeff = 0; |
| 1094 | if (stats.rd_low < stats.rd) { |
Dake He | 4d44769 | 2017-12-15 09:10:06 -0800 | [diff] [blame] | 1095 | if ((si < txb_info->eob - 1)) { |
Dake He | a47cd6c | 2017-10-13 18:09:58 -0700 | [diff] [blame] | 1096 | bUpdCoeff = 1; |
| 1097 | update = 1; |
| 1098 | } |
| 1099 | } |
Dake He | a47cd6c | 2017-10-13 18:09:58 -0700 | [diff] [blame] | 1100 | if (bUpdCoeff) { |
| 1101 | update_coeff(coeff_idx, stats.low_qc, txb_info); |
| 1102 | accu_rate += stats.rate_low; |
| 1103 | accu_dist += stats.dist_low; |
| 1104 | } else { |
| 1105 | accu_rate += stats.rate; |
| 1106 | accu_dist += stats.dist; |
| 1107 | } |
| 1108 | } |
| 1109 | } // for (si) |
Jingning Han | a7a6f4e | 2017-12-13 14:44:08 -0800 | [diff] [blame] | 1110 | |
Dake He | a47cd6c | 2017-10-13 18:09:58 -0700 | [diff] [blame] | 1111 | int non_zero_blk_rate = |
| 1112 | txb_costs->txb_skip_cost[txb_info->txb_ctx->txb_skip_ctx][0]; |
| 1113 | prev_eob_rd_cost = |
| 1114 | RDCOST(txb_info->rdmult, accu_rate + non_zero_blk_rate, accu_dist); |
| 1115 | |
| 1116 | int zero_blk_rate = |
| 1117 | txb_costs->txb_skip_cost[txb_info->txb_ctx->txb_skip_ctx][1]; |
| 1118 | int64_t zero_blk_rd_cost = RDCOST(txb_info->rdmult, zero_blk_rate, 0); |
| 1119 | if (zero_blk_rd_cost <= prev_eob_rd_cost) { |
| 1120 | update = 1; |
| 1121 | for (int j = 0; j < txb_info->eob; j++) { |
| 1122 | const int coeff_pos_j = scan[j]; |
| 1123 | update_coeff(coeff_pos_j, 0, txb_info); |
| 1124 | } |
| 1125 | txb_info->eob = 0; |
| 1126 | } |
| 1127 | |
Cheng Chen | 82775f6 | 2018-01-18 12:09:54 -0800 | [diff] [blame] | 1128 | // record total rate cost |
| 1129 | *rate_cost = zero_blk_rd_cost <= prev_eob_rd_cost |
| 1130 | ? zero_blk_rate |
| 1131 | : accu_rate + non_zero_blk_rate; |
Angie Chiang | 0ed5335 | 2018-03-08 12:53:43 -0800 | [diff] [blame] | 1132 | |
| 1133 | if (txb_info->eob > 0) { |
| 1134 | *rate_cost += txb_info->tx_type_cost; |
| 1135 | } |
| 1136 | |
Dake He | a47cd6c | 2017-10-13 18:09:58 -0700 | [diff] [blame] | 1137 | return update; |
| 1138 | } |
| 1139 | |
Yaowu Xu | 74e6335 | 2019-05-06 09:21:33 -0700 | [diff] [blame] | 1140 | #if CONFIG_HTB_TRELLIS |
Sarah Parker | 427e3b1 | 2018-10-12 12:28:44 -0700 | [diff] [blame] | 1141 | static void hbt_init() { |
Michelle Findlay-Olynyk | dea531d | 2017-12-13 14:10:56 -0800 | [diff] [blame] | 1142 | hbt_hash_table = |
| 1143 | aom_malloc(sizeof(OptTxbQcoeff) * HBT_TABLE_SIZE * HBT_ARRAY_LENGTH); |
| 1144 | memset(hbt_hash_table, 0, |
| 1145 | sizeof(OptTxbQcoeff) * HBT_TABLE_SIZE * HBT_ARRAY_LENGTH); |
Peng Bin | 8a204cd | 2018-04-08 13:07:35 +0800 | [diff] [blame] | 1146 | av1_crc32c_calculator_init(&crc_calculator); // 31 bit: qc & ctx |
Michelle Findlay-Olynyk | dea531d | 2017-12-13 14:10:56 -0800 | [diff] [blame] | 1147 | |
| 1148 | hbt_needs_init = 0; |
Michelle Findlay-Olynyk | fbab062 | 2017-12-13 14:10:56 -0800 | [diff] [blame] | 1149 | } |
| 1150 | |
Michelle Findlay-Olynyk | dea531d | 2017-12-13 14:10:56 -0800 | [diff] [blame] | 1151 | void hbt_destroy() { aom_free(hbt_hash_table); } |
| 1152 | |
Sarah Parker | 427e3b1 | 2018-10-12 12:28:44 -0700 | [diff] [blame] | 1153 | static int hbt_hash_miss(uint32_t hbt_ctx_hash, uint32_t hbt_qc_hash, |
| 1154 | TxbInfo *txb_info, const LV_MAP_COEFF_COST *txb_costs, |
| 1155 | const LV_MAP_EOB_COST *txb_eob_costs, |
| 1156 | const struct macroblock_plane *p, int block, |
| 1157 | int fast_mode, int *rate_cost) { |
Angie Chiang | 45385b8 | 2018-03-02 15:25:58 -0800 | [diff] [blame] | 1158 | (void)fast_mode; |
Michelle Findlay-Olynyk | fbab062 | 2017-12-13 14:10:56 -0800 | [diff] [blame] | 1159 | const int16_t *scan = txb_info->scan_order->scan; |
Michelle Findlay-Olynyk | dea531d | 2017-12-13 14:10:56 -0800 | [diff] [blame] | 1160 | int prev_eob = txb_info->eob; |
| 1161 | assert(HBT_EOB <= 16); // Lengthen array if allowing longer eob. |
| 1162 | int32_t prev_coeff[16]; |
| 1163 | for (int i = 0; i < prev_eob; i++) { |
| 1164 | prev_coeff[i] = txb_info->qcoeff[scan[i]]; |
| 1165 | } |
| 1166 | for (int i = prev_eob; i < HBT_EOB; i++) { |
| 1167 | prev_coeff[i] = 0; // For compiler piece of mind. |
| 1168 | } |
Michelle Findlay-Olynyk | fbab062 | 2017-12-13 14:10:56 -0800 | [diff] [blame] | 1169 | |
| 1170 | av1_txb_init_levels(txb_info->qcoeff, txb_info->width, txb_info->height, |
| 1171 | txb_info->levels); |
Michelle Findlay-Olynyk | dea531d | 2017-12-13 14:10:56 -0800 | [diff] [blame] | 1172 | |
Angie Chiang | 45385b8 | 2018-03-02 15:25:58 -0800 | [diff] [blame] | 1173 | const int update = |
| 1174 | optimize_txb(txb_info, txb_costs, txb_eob_costs, rate_cost); |
Michelle Findlay-Olynyk | dea531d | 2017-12-13 14:10:56 -0800 | [diff] [blame] | 1175 | |
| 1176 | // Overwrite old entry |
| 1177 | uint16_t hbt_table_index = hbt_ctx_hash % HBT_TABLE_SIZE; |
| 1178 | uint16_t hbt_array_index = hbt_qc_hash % HBT_ARRAY_LENGTH; |
| 1179 | hbt_hash_table[hbt_table_index * HBT_ARRAY_LENGTH + hbt_array_index] |
| 1180 | .rate_cost = *rate_cost; |
| 1181 | hbt_hash_table[hbt_table_index * HBT_ARRAY_LENGTH + hbt_array_index].init = 1; |
| 1182 | hbt_hash_table[hbt_table_index * HBT_ARRAY_LENGTH + hbt_array_index] |
| 1183 | .hbt_qc_hash = hbt_qc_hash; |
| 1184 | hbt_hash_table[hbt_table_index * HBT_ARRAY_LENGTH + hbt_array_index] |
| 1185 | .hbt_ctx_hash = hbt_ctx_hash; |
| 1186 | assert(prev_eob >= txb_info->eob); // eob can't get longer |
| 1187 | for (int i = 0; i < txb_info->eob; i++) { |
| 1188 | // Record how coeff changed. Convention: towards zero is negative. |
| 1189 | if (txb_info->qcoeff[scan[i]] > 0) |
| 1190 | hbt_hash_table[hbt_table_index * HBT_ARRAY_LENGTH + hbt_array_index] |
| 1191 | .deltas[i] = txb_info->qcoeff[scan[i]] - prev_coeff[i]; |
| 1192 | else |
| 1193 | hbt_hash_table[hbt_table_index * HBT_ARRAY_LENGTH + hbt_array_index] |
| 1194 | .deltas[i] = prev_coeff[i] - txb_info->qcoeff[scan[i]]; |
| 1195 | } |
| 1196 | for (int i = txb_info->eob; i < prev_eob; i++) { |
| 1197 | // If eob got shorter, record that all after it changed to zero. |
| 1198 | if (prev_coeff[i] > 0) |
| 1199 | hbt_hash_table[hbt_table_index * HBT_ARRAY_LENGTH + hbt_array_index] |
| 1200 | .deltas[i] = -prev_coeff[i]; |
| 1201 | else |
| 1202 | hbt_hash_table[hbt_table_index * HBT_ARRAY_LENGTH + hbt_array_index] |
| 1203 | .deltas[i] = prev_coeff[i]; |
| 1204 | } |
| 1205 | for (int i = prev_eob; i < HBT_EOB; i++) { |
| 1206 | // Record 'no change' after optimized coefficients run out. |
| 1207 | hbt_hash_table[hbt_table_index * HBT_ARRAY_LENGTH + hbt_array_index] |
| 1208 | .deltas[i] = 0; |
| 1209 | } |
Michelle Findlay-Olynyk | fbab062 | 2017-12-13 14:10:56 -0800 | [diff] [blame] | 1210 | |
| 1211 | if (update) { |
Michelle Findlay-Olynyk | fbab062 | 2017-12-13 14:10:56 -0800 | [diff] [blame] | 1212 | p->eobs[block] = txb_info->eob; |
| 1213 | p->txb_entropy_ctx[block] = av1_get_txb_entropy_context( |
| 1214 | txb_info->qcoeff, txb_info->scan_order, txb_info->eob); |
| 1215 | } |
| 1216 | return txb_info->eob; |
| 1217 | } |
| 1218 | |
Sarah Parker | 427e3b1 | 2018-10-12 12:28:44 -0700 | [diff] [blame] | 1219 | static int hbt_hash_hit(uint32_t hbt_table_index, int hbt_array_index, |
| 1220 | TxbInfo *txb_info, const struct macroblock_plane *p, |
| 1221 | int block, int *rate_cost) { |
Michelle Findlay-Olynyk | fbab062 | 2017-12-13 14:10:56 -0800 | [diff] [blame] | 1222 | const int16_t *scan = txb_info->scan_order->scan; |
| 1223 | int new_eob = 0; |
| 1224 | int update = 0; |
| 1225 | |
| 1226 | for (int i = 0; i < txb_info->eob; i++) { |
Michelle Findlay-Olynyk | dea531d | 2017-12-13 14:10:56 -0800 | [diff] [blame] | 1227 | // Delta convention is negatives go towards zero, so only apply those ones. |
| 1228 | if (hbt_hash_table[hbt_table_index * HBT_ARRAY_LENGTH + hbt_array_index] |
| 1229 | .deltas[i] < 0) { |
| 1230 | if (txb_info->qcoeff[scan[i]] > 0) |
| 1231 | txb_info->qcoeff[scan[i]] += |
| 1232 | hbt_hash_table[hbt_table_index * HBT_ARRAY_LENGTH + hbt_array_index] |
| 1233 | .deltas[i]; |
| 1234 | else |
| 1235 | txb_info->qcoeff[scan[i]] -= |
| 1236 | hbt_hash_table[hbt_table_index * HBT_ARRAY_LENGTH + hbt_array_index] |
| 1237 | .deltas[i]; |
| 1238 | |
Michelle Findlay-Olynyk | fbab062 | 2017-12-13 14:10:56 -0800 | [diff] [blame] | 1239 | update = 1; |
| 1240 | update_coeff(scan[i], txb_info->qcoeff[scan[i]], txb_info); |
| 1241 | } |
Michelle Findlay-Olynyk | fbab062 | 2017-12-13 14:10:56 -0800 | [diff] [blame] | 1242 | if (txb_info->qcoeff[scan[i]]) new_eob = i + 1; |
| 1243 | } |
| 1244 | |
Michelle Findlay-Olynyk | dea531d | 2017-12-13 14:10:56 -0800 | [diff] [blame] | 1245 | // Rate_cost can be calculated here instead (av1_cost_coeffs_txb), but |
| 1246 | // it is expensive and gives little benefit as long as qc_hash is high bit |
| 1247 | *rate_cost = |
| 1248 | hbt_hash_table[hbt_table_index * HBT_ARRAY_LENGTH + hbt_array_index] |
| 1249 | .rate_cost; |
| 1250 | |
Michelle Findlay-Olynyk | fbab062 | 2017-12-13 14:10:56 -0800 | [diff] [blame] | 1251 | if (update) { |
| 1252 | txb_info->eob = new_eob; |
| 1253 | p->eobs[block] = txb_info->eob; |
| 1254 | p->txb_entropy_ctx[block] = av1_get_txb_entropy_context( |
| 1255 | txb_info->qcoeff, txb_info->scan_order, txb_info->eob); |
| 1256 | } |
Michelle Findlay-Olynyk | dea531d | 2017-12-13 14:10:56 -0800 | [diff] [blame] | 1257 | |
Michelle Findlay-Olynyk | fbab062 | 2017-12-13 14:10:56 -0800 | [diff] [blame] | 1258 | return txb_info->eob; |
| 1259 | } |
| 1260 | |
Sarah Parker | 427e3b1 | 2018-10-12 12:28:44 -0700 | [diff] [blame] | 1261 | static int hbt_search_match(uint32_t hbt_ctx_hash, uint32_t hbt_qc_hash, |
| 1262 | TxbInfo *txb_info, |
| 1263 | const LV_MAP_COEFF_COST *txb_costs, |
| 1264 | const LV_MAP_EOB_COST *txb_eob_costs, |
| 1265 | const struct macroblock_plane *p, int block, |
| 1266 | int fast_mode, int *rate_cost) { |
Michelle Findlay-Olynyk | dea531d | 2017-12-13 14:10:56 -0800 | [diff] [blame] | 1267 | // Check for qcoeff match |
| 1268 | int hbt_array_index = hbt_qc_hash % HBT_ARRAY_LENGTH; |
| 1269 | int hbt_table_index = hbt_ctx_hash % HBT_TABLE_SIZE; |
Michelle Findlay-Olynyk | fbab062 | 2017-12-13 14:10:56 -0800 | [diff] [blame] | 1270 | |
Michelle Findlay-Olynyk | dea531d | 2017-12-13 14:10:56 -0800 | [diff] [blame] | 1271 | if (hbt_hash_table[hbt_table_index * HBT_ARRAY_LENGTH + hbt_array_index] |
| 1272 | .hbt_qc_hash == hbt_qc_hash && |
| 1273 | hbt_hash_table[hbt_table_index * HBT_ARRAY_LENGTH + hbt_array_index] |
| 1274 | .hbt_ctx_hash == hbt_ctx_hash && |
| 1275 | hbt_hash_table[hbt_table_index * HBT_ARRAY_LENGTH + hbt_array_index] |
| 1276 | .init) { |
| 1277 | return hbt_hash_hit(hbt_table_index, hbt_array_index, txb_info, p, block, |
| 1278 | rate_cost); |
| 1279 | } else { |
| 1280 | return hbt_hash_miss(hbt_ctx_hash, hbt_qc_hash, txb_info, txb_costs, |
| 1281 | txb_eob_costs, p, block, fast_mode, rate_cost); |
Michelle Findlay-Olynyk | fbab062 | 2017-12-13 14:10:56 -0800 | [diff] [blame] | 1282 | } |
| 1283 | } |
| 1284 | |
Sarah Parker | 427e3b1 | 2018-10-12 12:28:44 -0700 | [diff] [blame] | 1285 | static int hbt_create_hashes(TxbInfo *txb_info, |
| 1286 | const LV_MAP_COEFF_COST *txb_costs, |
| 1287 | const LV_MAP_EOB_COST *txb_eob_costs, |
| 1288 | const struct macroblock_plane *p, int block, |
| 1289 | int fast_mode, int *rate_cost) { |
Michelle Findlay-Olynyk | fbab062 | 2017-12-13 14:10:56 -0800 | [diff] [blame] | 1290 | // Initialize hash table if needed. |
Michelle Findlay-Olynyk | dea531d | 2017-12-13 14:10:56 -0800 | [diff] [blame] | 1291 | if (hbt_needs_init) { |
| 1292 | hbt_init(); |
Michelle Findlay-Olynyk | fbab062 | 2017-12-13 14:10:56 -0800 | [diff] [blame] | 1293 | } |
| 1294 | |
| 1295 | //// Hash creation |
Michelle Findlay-Olynyk | dea531d | 2017-12-13 14:10:56 -0800 | [diff] [blame] | 1296 | uint8_t txb_hash_data[256]; // Asserts below to ensure enough space. |
Michelle Findlay-Olynyk | fbab062 | 2017-12-13 14:10:56 -0800 | [diff] [blame] | 1297 | const int16_t *scan = txb_info->scan_order->scan; |
| 1298 | uint8_t chunk = 0; |
Michelle Findlay-Olynyk | fbab062 | 2017-12-13 14:10:56 -0800 | [diff] [blame] | 1299 | int hash_data_index = 0; |
Michelle Findlay-Olynyk | fbab062 | 2017-12-13 14:10:56 -0800 | [diff] [blame] | 1300 | |
Michelle Findlay-Olynyk | dea531d | 2017-12-13 14:10:56 -0800 | [diff] [blame] | 1301 | // Make qc_hash. |
| 1302 | int packing_index = 0; // needed for packing. |
Michelle Findlay-Olynyk | fbab062 | 2017-12-13 14:10:56 -0800 | [diff] [blame] | 1303 | for (int i = 0; i < txb_info->eob; i++) { |
Michelle Findlay-Olynyk | dea531d | 2017-12-13 14:10:56 -0800 | [diff] [blame] | 1304 | tran_low_t prechunk = txb_info->qcoeff[scan[i]]; |
| 1305 | |
| 1306 | // Softening: Improves speed. Aligns with signed deltas. |
| 1307 | if (prechunk < 0) prechunk *= -1; |
| 1308 | |
| 1309 | // Early kick out: Don't apply feature if there are large coeffs: |
| 1310 | // If this kickout value is removed or raised beyond int8_t, |
| 1311 | // widen deltas type in OptTxbQcoeff struct. |
| 1312 | assert((int8_t)HBT_KICKOUT == HBT_KICKOUT); // If not, widen types. |
| 1313 | if (prechunk > HBT_KICKOUT) { |
| 1314 | av1_txb_init_levels(txb_info->qcoeff, txb_info->width, txb_info->height, |
| 1315 | txb_info->levels); |
| 1316 | |
Angie Chiang | 45385b8 | 2018-03-02 15:25:58 -0800 | [diff] [blame] | 1317 | const int update = |
| 1318 | optimize_txb(txb_info, txb_costs, txb_eob_costs, rate_cost); |
Michelle Findlay-Olynyk | dea531d | 2017-12-13 14:10:56 -0800 | [diff] [blame] | 1319 | |
| 1320 | if (update) { |
| 1321 | p->eobs[block] = txb_info->eob; |
| 1322 | p->txb_entropy_ctx[block] = av1_get_txb_entropy_context( |
| 1323 | txb_info->qcoeff, txb_info->scan_order, txb_info->eob); |
| 1324 | } |
| 1325 | return txb_info->eob; |
| 1326 | } |
| 1327 | |
| 1328 | // Since coeffs are 0 to 3, only 2 bits are needed: pack into bytes |
| 1329 | if (packing_index == 0) txb_hash_data[hash_data_index] = 0; |
| 1330 | chunk = prechunk << packing_index; |
| 1331 | packing_index += 2; |
| 1332 | txb_hash_data[hash_data_index] |= chunk; |
| 1333 | |
| 1334 | // Full byte: |
| 1335 | if (packing_index == 8) { |
| 1336 | packing_index = 0; |
| 1337 | hash_data_index++; |
Michelle Findlay-Olynyk | fbab062 | 2017-12-13 14:10:56 -0800 | [diff] [blame] | 1338 | } |
| 1339 | } |
Michelle Findlay-Olynyk | dea531d | 2017-12-13 14:10:56 -0800 | [diff] [blame] | 1340 | // Needed when packing_index != 0, to include final byte. |
| 1341 | hash_data_index++; |
| 1342 | assert(hash_data_index <= 64); |
| 1343 | // 31 bit qc_hash: index to array |
| 1344 | uint32_t hbt_qc_hash = |
Peng Bin | 8a204cd | 2018-04-08 13:07:35 +0800 | [diff] [blame] | 1345 | av1_get_crc32c_value(&crc_calculator, txb_hash_data, hash_data_index); |
Michelle Findlay-Olynyk | dea531d | 2017-12-13 14:10:56 -0800 | [diff] [blame] | 1346 | |
| 1347 | // Make ctx_hash. |
| 1348 | hash_data_index = 0; |
| 1349 | tran_low_t prechunk; |
| 1350 | |
| 1351 | for (int i = 0; i < txb_info->eob; i++) { |
| 1352 | // Save as magnitudes towards or away from zero. |
| 1353 | if (txb_info->tcoeff[scan[i]] >= 0) |
| 1354 | prechunk = txb_info->tcoeff[scan[i]] - txb_info->dqcoeff[scan[i]]; |
| 1355 | else |
| 1356 | prechunk = txb_info->dqcoeff[scan[i]] - txb_info->tcoeff[scan[i]]; |
| 1357 | |
| 1358 | chunk = prechunk & 0xff; |
| 1359 | txb_hash_data[hash_data_index++] = chunk; |
| 1360 | } |
| 1361 | |
| 1362 | // Extra ctx data: |
| 1363 | // Include dequants. |
| 1364 | txb_hash_data[hash_data_index++] = txb_info->dequant[0] & 0xff; |
| 1365 | txb_hash_data[hash_data_index++] = txb_info->dequant[1] & 0xff; |
| 1366 | chunk = txb_info->txb_ctx->txb_skip_ctx & 0xff; |
| 1367 | txb_hash_data[hash_data_index++] = chunk; |
| 1368 | chunk = txb_info->txb_ctx->dc_sign_ctx & 0xff; |
| 1369 | txb_hash_data[hash_data_index++] = chunk; |
| 1370 | // eob |
| 1371 | chunk = txb_info->eob & 0xff; |
| 1372 | txb_hash_data[hash_data_index++] = chunk; |
| 1373 | // rdmult (int64) |
| 1374 | chunk = txb_info->rdmult & 0xff; |
| 1375 | txb_hash_data[hash_data_index++] = chunk; |
| 1376 | // tx_type |
| 1377 | chunk = txb_info->tx_type & 0xff; |
| 1378 | txb_hash_data[hash_data_index++] = chunk; |
Michelle Findlay-Olynyk | fbab062 | 2017-12-13 14:10:56 -0800 | [diff] [blame] | 1379 | // base_eob_cost |
| 1380 | for (int i = 1; i < 3; i++) { // i = 0 are softened away |
| 1381 | for (int j = 0; j < SIG_COEF_CONTEXTS_EOB; j++) { |
| 1382 | chunk = (txb_costs->base_eob_cost[j][i] & 0xff00) >> 8; |
| 1383 | txb_hash_data[hash_data_index++] = chunk; |
| 1384 | } |
Michelle Findlay-Olynyk | dea531d | 2017-12-13 14:10:56 -0800 | [diff] [blame] | 1385 | } |
| 1386 | // eob_cost |
| 1387 | for (int i = 0; i < 11; i++) { |
| 1388 | for (int j = 0; j < 2; j++) { |
| 1389 | chunk = (txb_eob_costs->eob_cost[j][i] & 0xff00) >> 8; |
| 1390 | txb_hash_data[hash_data_index++] = chunk; |
| 1391 | } |
| 1392 | } |
| 1393 | // dc_sign_cost |
| 1394 | for (int i = 0; i < 2; i++) { |
| 1395 | for (int j = 0; j < DC_SIGN_CONTEXTS; j++) { |
| 1396 | chunk = (txb_costs->dc_sign_cost[j][i] & 0xff00) >> 8; |
| 1397 | txb_hash_data[hash_data_index++] = chunk; |
| 1398 | } |
| 1399 | } |
Michelle Findlay-Olynyk | fbab062 | 2017-12-13 14:10:56 -0800 | [diff] [blame] | 1400 | |
Michelle Findlay-Olynyk | dea531d | 2017-12-13 14:10:56 -0800 | [diff] [blame] | 1401 | assert(hash_data_index <= 256); |
| 1402 | // 31 bit ctx_hash: used to index table |
| 1403 | uint32_t hbt_ctx_hash = |
Peng Bin | 8a204cd | 2018-04-08 13:07:35 +0800 | [diff] [blame] | 1404 | av1_get_crc32c_value(&crc_calculator, txb_hash_data, hash_data_index); |
Michelle Findlay-Olynyk | fbab062 | 2017-12-13 14:10:56 -0800 | [diff] [blame] | 1405 | //// End hash creation |
| 1406 | |
Michelle Findlay-Olynyk | dea531d | 2017-12-13 14:10:56 -0800 | [diff] [blame] | 1407 | return hbt_search_match(hbt_ctx_hash, hbt_qc_hash, txb_info, txb_costs, |
| 1408 | txb_eob_costs, p, block, fast_mode, rate_cost); |
Michelle Findlay-Olynyk | fbab062 | 2017-12-13 14:10:56 -0800 | [diff] [blame] | 1409 | } |
Yaowu Xu | 74e6335 | 2019-05-06 09:21:33 -0700 | [diff] [blame] | 1410 | #endif // CONFIG_HTB_TRELLIS |
Michelle Findlay-Olynyk | fbab062 | 2017-12-13 14:10:56 -0800 | [diff] [blame] | 1411 | |
Wenyao Liu | f7e5375 | 2019-01-22 17:34:44 +0800 | [diff] [blame] | 1412 | static AOM_FORCE_INLINE int get_two_coeff_cost_simple( |
Katsuhisa Yuasa | 9f63996 | 2018-04-08 18:00:54 +0900 | [diff] [blame] | 1413 | int ci, tran_low_t abs_qc, int coeff_ctx, |
| 1414 | const LV_MAP_COEFF_COST *txb_costs, int bwl, TX_CLASS tx_class, |
Wenyao Liu | f7e5375 | 2019-01-22 17:34:44 +0800 | [diff] [blame] | 1415 | const uint8_t *levels, int *cost_low) { |
Angie Chiang | 40b0731 | 2018-03-30 10:42:55 -0700 | [diff] [blame] | 1416 | // this simple version assumes the coeff's scan_idx is not DC (scan_idx != 0) |
Angie Chiang | e4ea748 | 2018-03-15 11:36:41 -0700 | [diff] [blame] | 1417 | // and not the last (scan_idx != eob - 1) |
| 1418 | assert(ci > 0); |
| 1419 | int cost = txb_costs->base_cost[coeff_ctx][AOMMIN(abs_qc, 3)]; |
Wenyao Liu | f7e5375 | 2019-01-22 17:34:44 +0800 | [diff] [blame] | 1420 | int diff = 0; |
| 1421 | if (abs_qc <= 3) diff = txb_costs->base_cost[coeff_ctx][abs_qc + 4]; |
Angie Chiang | e4ea748 | 2018-03-15 11:36:41 -0700 | [diff] [blame] | 1422 | if (abs_qc) { |
| 1423 | cost += av1_cost_literal(1); |
| 1424 | if (abs_qc > NUM_BASE_LEVELS) { |
Katsuhisa Yuasa | 55c95f8 | 2018-04-03 00:51:22 +0900 | [diff] [blame] | 1425 | const int br_ctx = get_br_ctx(levels, ci, bwl, tx_class); |
Wenyao Liu | f7e5375 | 2019-01-22 17:34:44 +0800 | [diff] [blame] | 1426 | int brcost_diff = 0; |
| 1427 | cost += get_br_cost_with_diff(abs_qc, txb_costs->lps_cost[br_ctx], |
| 1428 | &brcost_diff); |
| 1429 | diff += brcost_diff; |
Jim Bankoski | fe17122 | 2019-01-14 08:41:57 -0800 | [diff] [blame] | 1430 | } |
| 1431 | } |
Wenyao Liu | f7e5375 | 2019-01-22 17:34:44 +0800 | [diff] [blame] | 1432 | *cost_low = cost - diff; |
| 1433 | |
Jim Bankoski | fe17122 | 2019-01-14 08:41:57 -0800 | [diff] [blame] | 1434 | return cost; |
| 1435 | } |
| 1436 | |
| 1437 | static INLINE int get_coeff_cost_eob(int ci, tran_low_t abs_qc, int sign, |
| 1438 | int coeff_ctx, int dc_sign_ctx, |
| 1439 | const LV_MAP_COEFF_COST *txb_costs, |
| 1440 | int bwl, TX_CLASS tx_class) { |
| 1441 | int cost = 0; |
| 1442 | cost += txb_costs->base_eob_cost[coeff_ctx][AOMMIN(abs_qc, 3) - 1]; |
| 1443 | if (abs_qc != 0) { |
| 1444 | if (ci == 0) { |
| 1445 | cost += txb_costs->dc_sign_cost[dc_sign_ctx][sign]; |
| 1446 | } else { |
| 1447 | cost += av1_cost_literal(1); |
| 1448 | } |
| 1449 | if (abs_qc > NUM_BASE_LEVELS) { |
| 1450 | int br_ctx; |
| 1451 | br_ctx = get_br_ctx_eob(ci, bwl, tx_class); |
| 1452 | cost += get_br_cost(abs_qc, txb_costs->lps_cost[br_ctx]); |
Angie Chiang | e4ea748 | 2018-03-15 11:36:41 -0700 | [diff] [blame] | 1453 | } |
| 1454 | } |
| 1455 | return cost; |
| 1456 | } |
| 1457 | |
| 1458 | static INLINE int get_coeff_cost_general(int is_last, int ci, tran_low_t abs_qc, |
Angie Chiang | 40b0731 | 2018-03-30 10:42:55 -0700 | [diff] [blame] | 1459 | int sign, int coeff_ctx, |
Angie Chiang | e4ea748 | 2018-03-15 11:36:41 -0700 | [diff] [blame] | 1460 | int dc_sign_ctx, |
| 1461 | const LV_MAP_COEFF_COST *txb_costs, |
Katsuhisa Yuasa | 55c95f8 | 2018-04-03 00:51:22 +0900 | [diff] [blame] | 1462 | int bwl, TX_CLASS tx_class, |
Angie Chiang | e4ea748 | 2018-03-15 11:36:41 -0700 | [diff] [blame] | 1463 | const uint8_t *levels) { |
| 1464 | int cost = 0; |
| 1465 | if (is_last) { |
| 1466 | cost += txb_costs->base_eob_cost[coeff_ctx][AOMMIN(abs_qc, 3) - 1]; |
| 1467 | } else { |
| 1468 | cost += txb_costs->base_cost[coeff_ctx][AOMMIN(abs_qc, 3)]; |
| 1469 | } |
| 1470 | if (abs_qc != 0) { |
| 1471 | if (ci == 0) { |
Angie Chiang | 40b0731 | 2018-03-30 10:42:55 -0700 | [diff] [blame] | 1472 | cost += txb_costs->dc_sign_cost[dc_sign_ctx][sign]; |
Angie Chiang | e4ea748 | 2018-03-15 11:36:41 -0700 | [diff] [blame] | 1473 | } else { |
| 1474 | cost += av1_cost_literal(1); |
| 1475 | } |
| 1476 | if (abs_qc > NUM_BASE_LEVELS) { |
Jim Bankoski | 95cb0d7 | 2019-01-11 15:20:06 -0800 | [diff] [blame] | 1477 | int br_ctx; |
| 1478 | if (is_last) |
| 1479 | br_ctx = get_br_ctx_eob(ci, bwl, tx_class); |
| 1480 | else |
| 1481 | br_ctx = get_br_ctx(levels, ci, bwl, tx_class); |
Jim Bankoski | fe17122 | 2019-01-14 08:41:57 -0800 | [diff] [blame] | 1482 | cost += get_br_cost(abs_qc, txb_costs->lps_cost[br_ctx]); |
Angie Chiang | e4ea748 | 2018-03-15 11:36:41 -0700 | [diff] [blame] | 1483 | } |
| 1484 | } |
| 1485 | return cost; |
| 1486 | } |
| 1487 | |
Angie Chiang | 40b0731 | 2018-03-30 10:42:55 -0700 | [diff] [blame] | 1488 | static INLINE void get_qc_dqc_low(tran_low_t abs_qc, int sign, int dqv, |
Angie Chiang | e4ea748 | 2018-03-15 11:36:41 -0700 | [diff] [blame] | 1489 | int shift, tran_low_t *qc_low, |
| 1490 | tran_low_t *dqc_low) { |
| 1491 | tran_low_t abs_qc_low = abs_qc - 1; |
Angie Chiang | 40b0731 | 2018-03-30 10:42:55 -0700 | [diff] [blame] | 1492 | *qc_low = (-sign ^ abs_qc_low) + sign; |
| 1493 | assert((sign ? -abs_qc_low : abs_qc_low) == *qc_low); |
Angie Chiang | e4ea748 | 2018-03-15 11:36:41 -0700 | [diff] [blame] | 1494 | tran_low_t abs_dqc_low = (abs_qc_low * dqv) >> shift; |
Angie Chiang | 40b0731 | 2018-03-30 10:42:55 -0700 | [diff] [blame] | 1495 | *dqc_low = (-sign ^ abs_dqc_low) + sign; |
| 1496 | assert((sign ? -abs_dqc_low : abs_dqc_low) == *dqc_low); |
Angie Chiang | e4ea748 | 2018-03-15 11:36:41 -0700 | [diff] [blame] | 1497 | } |
| 1498 | |
| 1499 | static INLINE void update_coeff_general( |
| 1500 | int *accu_rate, int64_t *accu_dist, int si, int eob, TX_SIZE tx_size, |
Katsuhisa Yuasa | 55c95f8 | 2018-04-03 00:51:22 +0900 | [diff] [blame] | 1501 | TX_CLASS tx_class, int bwl, int height, int64_t rdmult, int shift, |
Angie Chiang | e4ea748 | 2018-03-15 11:36:41 -0700 | [diff] [blame] | 1502 | int dc_sign_ctx, const int16_t *dequant, const int16_t *scan, |
| 1503 | const LV_MAP_COEFF_COST *txb_costs, const tran_low_t *tcoeff, |
Jingning Han | 37f5354 | 2019-06-06 14:22:17 -0700 | [diff] [blame] | 1504 | tran_low_t *qcoeff, tran_low_t *dqcoeff, uint8_t *levels, |
| 1505 | const qm_val_t *iqmatrix) { |
| 1506 | const int dqv = get_dqv(dequant, scan[si], iqmatrix); |
Angie Chiang | e4ea748 | 2018-03-15 11:36:41 -0700 | [diff] [blame] | 1507 | const int ci = scan[si]; |
| 1508 | const tran_low_t qc = qcoeff[ci]; |
| 1509 | const int is_last = si == (eob - 1); |
| 1510 | const int coeff_ctx = get_lower_levels_ctx_general( |
Katsuhisa Yuasa | 55c95f8 | 2018-04-03 00:51:22 +0900 | [diff] [blame] | 1511 | is_last, si, bwl, height, levels, ci, tx_size, tx_class); |
Angie Chiang | e4ea748 | 2018-03-15 11:36:41 -0700 | [diff] [blame] | 1512 | if (qc == 0) { |
| 1513 | *accu_rate += txb_costs->base_cost[coeff_ctx][0]; |
| 1514 | } else { |
Angie Chiang | 40b0731 | 2018-03-30 10:42:55 -0700 | [diff] [blame] | 1515 | const int sign = (qc < 0) ? 1 : 0; |
Angie Chiang | e4ea748 | 2018-03-15 11:36:41 -0700 | [diff] [blame] | 1516 | const tran_low_t abs_qc = abs(qc); |
| 1517 | const tran_low_t tqc = tcoeff[ci]; |
| 1518 | const tran_low_t dqc = dqcoeff[ci]; |
| 1519 | const int64_t dist = get_coeff_dist(tqc, dqc, shift); |
| 1520 | const int64_t dist0 = get_coeff_dist(tqc, 0, shift); |
| 1521 | const int rate = |
Angie Chiang | 40b0731 | 2018-03-30 10:42:55 -0700 | [diff] [blame] | 1522 | get_coeff_cost_general(is_last, ci, abs_qc, sign, coeff_ctx, |
Katsuhisa Yuasa | 55c95f8 | 2018-04-03 00:51:22 +0900 | [diff] [blame] | 1523 | dc_sign_ctx, txb_costs, bwl, tx_class, levels); |
Angie Chiang | e4ea748 | 2018-03-15 11:36:41 -0700 | [diff] [blame] | 1524 | const int64_t rd = RDCOST(rdmult, rate, dist); |
| 1525 | |
| 1526 | tran_low_t qc_low, dqc_low; |
Wenyao Liu | f7e5375 | 2019-01-22 17:34:44 +0800 | [diff] [blame] | 1527 | tran_low_t abs_qc_low; |
| 1528 | int64_t dist_low, rd_low; |
| 1529 | int rate_low; |
| 1530 | if (abs_qc == 1) { |
| 1531 | abs_qc_low = qc_low = dqc_low = 0; |
| 1532 | dist_low = dist0; |
| 1533 | rate_low = txb_costs->base_cost[coeff_ctx][0]; |
| 1534 | } else { |
| 1535 | get_qc_dqc_low(abs_qc, sign, dqv, shift, &qc_low, &dqc_low); |
| 1536 | abs_qc_low = abs_qc - 1; |
| 1537 | dist_low = get_coeff_dist(tqc, dqc_low, shift); |
| 1538 | rate_low = |
| 1539 | get_coeff_cost_general(is_last, ci, abs_qc_low, sign, coeff_ctx, |
| 1540 | dc_sign_ctx, txb_costs, bwl, tx_class, levels); |
| 1541 | } |
| 1542 | |
| 1543 | rd_low = RDCOST(rdmult, rate_low, dist_low); |
Angie Chiang | e4ea748 | 2018-03-15 11:36:41 -0700 | [diff] [blame] | 1544 | if (rd_low < rd) { |
| 1545 | qcoeff[ci] = qc_low; |
| 1546 | dqcoeff[ci] = dqc_low; |
| 1547 | levels[get_padded_idx(ci, bwl)] = AOMMIN(abs_qc_low, INT8_MAX); |
| 1548 | *accu_rate += rate_low; |
| 1549 | *accu_dist += dist_low - dist0; |
| 1550 | } else { |
| 1551 | *accu_rate += rate; |
| 1552 | *accu_dist += dist - dist0; |
| 1553 | } |
| 1554 | } |
| 1555 | } |
| 1556 | |
Katsuhisa Yuasa | 9f63996 | 2018-04-08 18:00:54 +0900 | [diff] [blame] | 1557 | static AOM_FORCE_INLINE void update_coeff_simple( |
Katsuhisa Yuasa | 55c95f8 | 2018-04-03 00:51:22 +0900 | [diff] [blame] | 1558 | int *accu_rate, int si, int eob, TX_SIZE tx_size, TX_CLASS tx_class, |
| 1559 | int bwl, int64_t rdmult, int shift, const int16_t *dequant, |
| 1560 | const int16_t *scan, const LV_MAP_COEFF_COST *txb_costs, |
| 1561 | const tran_low_t *tcoeff, tran_low_t *qcoeff, tran_low_t *dqcoeff, |
Jingning Han | 37f5354 | 2019-06-06 14:22:17 -0700 | [diff] [blame] | 1562 | uint8_t *levels, const qm_val_t *iqmatrix) { |
| 1563 | const int dqv = get_dqv(dequant, scan[si], iqmatrix); |
Angie Chiang | e4ea748 | 2018-03-15 11:36:41 -0700 | [diff] [blame] | 1564 | (void)eob; |
Angie Chiang | 40b0731 | 2018-03-30 10:42:55 -0700 | [diff] [blame] | 1565 | // this simple version assumes the coeff's scan_idx is not DC (scan_idx != 0) |
Angie Chiang | e4ea748 | 2018-03-15 11:36:41 -0700 | [diff] [blame] | 1566 | // and not the last (scan_idx != eob - 1) |
| 1567 | assert(si != eob - 1); |
| 1568 | assert(si > 0); |
| 1569 | const int ci = scan[si]; |
| 1570 | const tran_low_t qc = qcoeff[ci]; |
Katsuhisa Yuasa | 55c95f8 | 2018-04-03 00:51:22 +0900 | [diff] [blame] | 1571 | const int coeff_ctx = |
| 1572 | get_lower_levels_ctx(levels, ci, bwl, tx_size, tx_class); |
Angie Chiang | e4ea748 | 2018-03-15 11:36:41 -0700 | [diff] [blame] | 1573 | if (qc == 0) { |
| 1574 | *accu_rate += txb_costs->base_cost[coeff_ctx][0]; |
| 1575 | } else { |
| 1576 | const tran_low_t abs_qc = abs(qc); |
Wenyao Liu | f7e5375 | 2019-01-22 17:34:44 +0800 | [diff] [blame] | 1577 | const tran_low_t abs_tqc = abs(tcoeff[ci]); |
| 1578 | const tran_low_t abs_dqc = abs(dqcoeff[ci]); |
| 1579 | int rate_low = 0; |
| 1580 | const int rate = get_two_coeff_cost_simple( |
| 1581 | ci, abs_qc, coeff_ctx, txb_costs, bwl, tx_class, levels, &rate_low); |
| 1582 | if (abs_dqc < abs_tqc) { |
Jingning Han | 87f5c34 | 2018-04-10 23:10:12 -0700 | [diff] [blame] | 1583 | *accu_rate += rate; |
| 1584 | return; |
| 1585 | } |
Wenyao Liu | f7e5375 | 2019-01-22 17:34:44 +0800 | [diff] [blame] | 1586 | |
| 1587 | const int64_t dist = get_coeff_dist(abs_tqc, abs_dqc, shift); |
Angie Chiang | e4ea748 | 2018-03-15 11:36:41 -0700 | [diff] [blame] | 1588 | const int64_t rd = RDCOST(rdmult, rate, dist); |
| 1589 | |
Angie Chiang | e4ea748 | 2018-03-15 11:36:41 -0700 | [diff] [blame] | 1590 | const tran_low_t abs_qc_low = abs_qc - 1; |
Wenyao Liu | f7e5375 | 2019-01-22 17:34:44 +0800 | [diff] [blame] | 1591 | const tran_low_t abs_dqc_low = (abs_qc_low * dqv) >> shift; |
| 1592 | const int64_t dist_low = get_coeff_dist(abs_tqc, abs_dqc_low, shift); |
Angie Chiang | e4ea748 | 2018-03-15 11:36:41 -0700 | [diff] [blame] | 1593 | const int64_t rd_low = RDCOST(rdmult, rate_low, dist_low); |
Wenyao Liu | f7e5375 | 2019-01-22 17:34:44 +0800 | [diff] [blame] | 1594 | |
Angie Chiang | e4ea748 | 2018-03-15 11:36:41 -0700 | [diff] [blame] | 1595 | if (rd_low < rd) { |
Wenyao Liu | f7e5375 | 2019-01-22 17:34:44 +0800 | [diff] [blame] | 1596 | const int sign = (qc < 0) ? 1 : 0; |
| 1597 | qcoeff[ci] = (-sign ^ abs_qc_low) + sign; |
| 1598 | dqcoeff[ci] = (-sign ^ abs_dqc_low) + sign; |
Angie Chiang | e4ea748 | 2018-03-15 11:36:41 -0700 | [diff] [blame] | 1599 | levels[get_padded_idx(ci, bwl)] = AOMMIN(abs_qc_low, INT8_MAX); |
| 1600 | *accu_rate += rate_low; |
Angie Chiang | e4ea748 | 2018-03-15 11:36:41 -0700 | [diff] [blame] | 1601 | } else { |
| 1602 | *accu_rate += rate; |
Angie Chiang | e4ea748 | 2018-03-15 11:36:41 -0700 | [diff] [blame] | 1603 | } |
| 1604 | } |
| 1605 | } |
| 1606 | |
Katsuhisa Yuasa | 9f63996 | 2018-04-08 18:00:54 +0900 | [diff] [blame] | 1607 | static AOM_FORCE_INLINE void update_coeff_eob( |
Angie Chiang | e4ea748 | 2018-03-15 11:36:41 -0700 | [diff] [blame] | 1608 | int *accu_rate, int64_t *accu_dist, int *eob, int *nz_num, int *nz_ci, |
Katsuhisa Yuasa | 55c95f8 | 2018-04-03 00:51:22 +0900 | [diff] [blame] | 1609 | int si, TX_SIZE tx_size, TX_CLASS tx_class, int bwl, int height, |
Angie Chiang | e4ea748 | 2018-03-15 11:36:41 -0700 | [diff] [blame] | 1610 | int dc_sign_ctx, int64_t rdmult, int shift, const int16_t *dequant, |
| 1611 | const int16_t *scan, const LV_MAP_EOB_COST *txb_eob_costs, |
| 1612 | const LV_MAP_COEFF_COST *txb_costs, const tran_low_t *tcoeff, |
Jingning Han | 37f5354 | 2019-06-06 14:22:17 -0700 | [diff] [blame] | 1613 | tran_low_t *qcoeff, tran_low_t *dqcoeff, uint8_t *levels, int sharpness, |
| 1614 | const qm_val_t *iqmatrix) { |
| 1615 | const int dqv = get_dqv(dequant, scan[si], iqmatrix); |
Angie Chiang | e4ea748 | 2018-03-15 11:36:41 -0700 | [diff] [blame] | 1616 | assert(si != *eob - 1); |
| 1617 | const int ci = scan[si]; |
| 1618 | const tran_low_t qc = qcoeff[ci]; |
Katsuhisa Yuasa | 55c95f8 | 2018-04-03 00:51:22 +0900 | [diff] [blame] | 1619 | const int coeff_ctx = |
| 1620 | get_lower_levels_ctx(levels, ci, bwl, tx_size, tx_class); |
Angie Chiang | e4ea748 | 2018-03-15 11:36:41 -0700 | [diff] [blame] | 1621 | if (qc == 0) { |
| 1622 | *accu_rate += txb_costs->base_cost[coeff_ctx][0]; |
| 1623 | } else { |
| 1624 | int lower_level = 0; |
| 1625 | const tran_low_t abs_qc = abs(qc); |
| 1626 | const tran_low_t tqc = tcoeff[ci]; |
| 1627 | const tran_low_t dqc = dqcoeff[ci]; |
Angie Chiang | 40b0731 | 2018-03-30 10:42:55 -0700 | [diff] [blame] | 1628 | const int sign = (qc < 0) ? 1 : 0; |
Angie Chiang | e4ea748 | 2018-03-15 11:36:41 -0700 | [diff] [blame] | 1629 | const int64_t dist0 = get_coeff_dist(tqc, 0, shift); |
| 1630 | int64_t dist = get_coeff_dist(tqc, dqc, shift) - dist0; |
| 1631 | int rate = |
Angie Chiang | 40b0731 | 2018-03-30 10:42:55 -0700 | [diff] [blame] | 1632 | get_coeff_cost_general(0, ci, abs_qc, sign, coeff_ctx, dc_sign_ctx, |
Katsuhisa Yuasa | 55c95f8 | 2018-04-03 00:51:22 +0900 | [diff] [blame] | 1633 | txb_costs, bwl, tx_class, levels); |
Angie Chiang | e4ea748 | 2018-03-15 11:36:41 -0700 | [diff] [blame] | 1634 | int64_t rd = RDCOST(rdmult, *accu_rate + rate, *accu_dist + dist); |
| 1635 | |
| 1636 | tran_low_t qc_low, dqc_low; |
Wenyao Liu | f7e5375 | 2019-01-22 17:34:44 +0800 | [diff] [blame] | 1637 | tran_low_t abs_qc_low; |
| 1638 | int64_t dist_low, rd_low; |
| 1639 | int rate_low; |
| 1640 | if (abs_qc == 1) { |
| 1641 | abs_qc_low = 0; |
| 1642 | dqc_low = qc_low = 0; |
| 1643 | dist_low = 0; |
| 1644 | rate_low = txb_costs->base_cost[coeff_ctx][0]; |
| 1645 | rd_low = RDCOST(rdmult, *accu_rate + rate_low, *accu_dist); |
| 1646 | } else { |
| 1647 | get_qc_dqc_low(abs_qc, sign, dqv, shift, &qc_low, &dqc_low); |
| 1648 | abs_qc_low = abs_qc - 1; |
| 1649 | dist_low = get_coeff_dist(tqc, dqc_low, shift) - dist0; |
| 1650 | rate_low = |
| 1651 | get_coeff_cost_general(0, ci, abs_qc_low, sign, coeff_ctx, |
| 1652 | dc_sign_ctx, txb_costs, bwl, tx_class, levels); |
| 1653 | rd_low = RDCOST(rdmult, *accu_rate + rate_low, *accu_dist + dist_low); |
| 1654 | } |
Angie Chiang | e4ea748 | 2018-03-15 11:36:41 -0700 | [diff] [blame] | 1655 | |
| 1656 | int lower_level_new_eob = 0; |
| 1657 | const int new_eob = si + 1; |
wenyao.liu | 0be5901 | 2018-12-24 10:27:31 +0800 | [diff] [blame] | 1658 | const int coeff_ctx_new_eob = get_lower_levels_ctx_eob(bwl, height, si); |
Angie Chiang | e4ea748 | 2018-03-15 11:36:41 -0700 | [diff] [blame] | 1659 | const int new_eob_cost = |
Katsuhisa Yuasa | 55c95f8 | 2018-04-03 00:51:22 +0900 | [diff] [blame] | 1660 | get_eob_cost(new_eob, txb_eob_costs, txb_costs, tx_class); |
Angie Chiang | e4ea748 | 2018-03-15 11:36:41 -0700 | [diff] [blame] | 1661 | int rate_coeff_eob = |
Jim Bankoski | fe17122 | 2019-01-14 08:41:57 -0800 | [diff] [blame] | 1662 | new_eob_cost + get_coeff_cost_eob(ci, abs_qc, sign, coeff_ctx_new_eob, |
| 1663 | dc_sign_ctx, txb_costs, bwl, |
| 1664 | tx_class); |
Angie Chiang | e4ea748 | 2018-03-15 11:36:41 -0700 | [diff] [blame] | 1665 | int64_t dist_new_eob = dist; |
| 1666 | int64_t rd_new_eob = RDCOST(rdmult, rate_coeff_eob, dist_new_eob); |
| 1667 | |
| 1668 | if (abs_qc_low > 0) { |
| 1669 | const int rate_coeff_eob_low = |
Jim Bankoski | fe17122 | 2019-01-14 08:41:57 -0800 | [diff] [blame] | 1670 | new_eob_cost + get_coeff_cost_eob(ci, abs_qc_low, sign, |
| 1671 | coeff_ctx_new_eob, dc_sign_ctx, |
| 1672 | txb_costs, bwl, tx_class); |
Angie Chiang | e4ea748 | 2018-03-15 11:36:41 -0700 | [diff] [blame] | 1673 | const int64_t dist_new_eob_low = dist_low; |
| 1674 | const int64_t rd_new_eob_low = |
| 1675 | RDCOST(rdmult, rate_coeff_eob_low, dist_new_eob_low); |
| 1676 | if (rd_new_eob_low < rd_new_eob) { |
| 1677 | lower_level_new_eob = 1; |
| 1678 | rd_new_eob = rd_new_eob_low; |
| 1679 | rate_coeff_eob = rate_coeff_eob_low; |
| 1680 | dist_new_eob = dist_new_eob_low; |
| 1681 | } |
| 1682 | } |
| 1683 | |
| 1684 | if (rd_low < rd) { |
| 1685 | lower_level = 1; |
| 1686 | rd = rd_low; |
| 1687 | rate = rate_low; |
| 1688 | dist = dist_low; |
| 1689 | } |
| 1690 | |
Jim Bankoski | 855ac46 | 2018-06-07 09:05:00 -0700 | [diff] [blame] | 1691 | if (sharpness == 0 && rd_new_eob < rd) { |
Angie Chiang | e4ea748 | 2018-03-15 11:36:41 -0700 | [diff] [blame] | 1692 | for (int ni = 0; ni < *nz_num; ++ni) { |
| 1693 | int last_ci = nz_ci[ni]; |
Jim Bankoski | 95cb0d7 | 2019-01-11 15:20:06 -0800 | [diff] [blame] | 1694 | levels[get_padded_idx(last_ci, bwl)] = 0; |
Angie Chiang | e4ea748 | 2018-03-15 11:36:41 -0700 | [diff] [blame] | 1695 | qcoeff[last_ci] = 0; |
| 1696 | dqcoeff[last_ci] = 0; |
| 1697 | } |
| 1698 | *eob = new_eob; |
| 1699 | *nz_num = 0; |
| 1700 | *accu_rate = rate_coeff_eob; |
| 1701 | *accu_dist = dist_new_eob; |
| 1702 | lower_level = lower_level_new_eob; |
| 1703 | } else { |
Angie Chiang | e4ea748 | 2018-03-15 11:36:41 -0700 | [diff] [blame] | 1704 | *accu_rate += rate; |
| 1705 | *accu_dist += dist; |
| 1706 | } |
| 1707 | |
| 1708 | if (lower_level) { |
| 1709 | qcoeff[ci] = qc_low; |
| 1710 | dqcoeff[ci] = dqc_low; |
| 1711 | levels[get_padded_idx(ci, bwl)] = AOMMIN(abs_qc_low, INT8_MAX); |
| 1712 | } |
| 1713 | if (qcoeff[ci]) { |
| 1714 | nz_ci[*nz_num] = ci; |
| 1715 | ++*nz_num; |
| 1716 | } |
| 1717 | } |
| 1718 | } |
| 1719 | |
Angie Chiang | d0397e1 | 2018-03-27 19:03:20 -0700 | [diff] [blame] | 1720 | static INLINE void update_skip(int *accu_rate, int64_t accu_dist, int *eob, |
| 1721 | int nz_num, int *nz_ci, int64_t rdmult, |
| 1722 | int skip_cost, int non_skip_cost, |
Jim Bankoski | 855ac46 | 2018-06-07 09:05:00 -0700 | [diff] [blame] | 1723 | tran_low_t *qcoeff, tran_low_t *dqcoeff, |
| 1724 | int sharpness) { |
Angie Chiang | d0397e1 | 2018-03-27 19:03:20 -0700 | [diff] [blame] | 1725 | const int64_t rd = RDCOST(rdmult, *accu_rate + non_skip_cost, accu_dist); |
Angie Chiang | e4ea748 | 2018-03-15 11:36:41 -0700 | [diff] [blame] | 1726 | const int64_t rd_new_eob = RDCOST(rdmult, skip_cost, 0); |
Jim Bankoski | 855ac46 | 2018-06-07 09:05:00 -0700 | [diff] [blame] | 1727 | if (sharpness == 0 && rd_new_eob < rd) { |
Angie Chiang | d0397e1 | 2018-03-27 19:03:20 -0700 | [diff] [blame] | 1728 | for (int i = 0; i < nz_num; ++i) { |
| 1729 | const int ci = nz_ci[i]; |
Angie Chiang | e4ea748 | 2018-03-15 11:36:41 -0700 | [diff] [blame] | 1730 | qcoeff[ci] = 0; |
| 1731 | dqcoeff[ci] = 0; |
Angie Chiang | d0397e1 | 2018-03-27 19:03:20 -0700 | [diff] [blame] | 1732 | // no need to set up levels because this is the last step |
| 1733 | // levels[get_padded_idx(ci, bwl)] = 0; |
Angie Chiang | e4ea748 | 2018-03-15 11:36:41 -0700 | [diff] [blame] | 1734 | } |
Angie Chiang | e4ea748 | 2018-03-15 11:36:41 -0700 | [diff] [blame] | 1735 | *accu_rate = 0; |
| 1736 | *eob = 0; |
| 1737 | } |
| 1738 | } |
| 1739 | |
| 1740 | int av1_optimize_txb_new(const struct AV1_COMP *cpi, MACROBLOCK *x, int plane, |
Xing Jin | bd91e94 | 2018-06-21 13:43:17 +0800 | [diff] [blame] | 1741 | int block, TX_SIZE tx_size, TX_TYPE tx_type, |
| 1742 | const TXB_CTX *const txb_ctx, int *rate_cost, |
Sarah Parker | 89df034 | 2019-02-07 22:23:39 -0800 | [diff] [blame] | 1743 | int sharpness, int fast_mode) { |
Angie Chiang | e4ea748 | 2018-03-15 11:36:41 -0700 | [diff] [blame] | 1744 | MACROBLOCKD *xd = &x->e_mbd; |
Sarah Parker | 89df034 | 2019-02-07 22:23:39 -0800 | [diff] [blame] | 1745 | const struct macroblock_plane *p = &x->plane[plane]; |
| 1746 | const SCAN_ORDER *scan_order = get_scan(tx_size, tx_type); |
| 1747 | const int16_t *scan = scan_order->scan; |
| 1748 | const int shift = av1_get_tx_scale(tx_size); |
| 1749 | int eob = p->eobs[block]; |
| 1750 | const int16_t *dequant = p->dequant_QTX; |
Jingning Han | 37f5354 | 2019-06-06 14:22:17 -0700 | [diff] [blame] | 1751 | const qm_val_t *iqmatrix = |
Urvang Joshi | f409a63 | 2020-03-28 01:03:50 -0700 | [diff] [blame] | 1752 | av1_get_iqmatrix(&cpi->common.quant_params, xd, plane, tx_size, tx_type); |
Yaowu Xu | 18a4996 | 2019-10-16 12:47:04 -0700 | [diff] [blame] | 1753 | const int block_offset = BLOCK_OFFSET(block); |
| 1754 | tran_low_t *qcoeff = p->qcoeff + block_offset; |
Urvang Joshi | 9543ad7 | 2020-04-17 16:59:46 -0700 | [diff] [blame] | 1755 | tran_low_t *dqcoeff = p->dqcoeff + block_offset; |
Yaowu Xu | 18a4996 | 2019-10-16 12:47:04 -0700 | [diff] [blame] | 1756 | const tran_low_t *tcoeff = p->coeff + block_offset; |
chiyotsai | 9a06d18 | 2020-05-01 17:12:12 -0700 | [diff] [blame] | 1757 | const CoeffCosts *coeff_costs = &x->coeff_costs; |
Sarah Parker | 89df034 | 2019-02-07 22:23:39 -0800 | [diff] [blame] | 1758 | |
Jingning Han | 3e4c9ed | 2019-06-07 15:03:16 -0700 | [diff] [blame] | 1759 | // This function is not called if eob = 0. |
| 1760 | assert(eob > 0); |
| 1761 | |
Sarah Parker | 89df034 | 2019-02-07 22:23:39 -0800 | [diff] [blame] | 1762 | if (fast_mode) { |
| 1763 | update_coeff_eob_fast(&eob, shift, dequant, scan, tcoeff, qcoeff, dqcoeff); |
| 1764 | p->eobs[block] = eob; |
| 1765 | if (eob == 0) { |
chiyotsai | 9a06d18 | 2020-05-01 17:12:12 -0700 | [diff] [blame] | 1766 | *rate_cost = av1_cost_skip_txb(coeff_costs, txb_ctx, plane, tx_size); |
Sarah Parker | 89df034 | 2019-02-07 22:23:39 -0800 | [diff] [blame] | 1767 | return eob; |
| 1768 | } |
| 1769 | } |
| 1770 | |
| 1771 | const AV1_COMMON *cm = &cpi->common; |
Angie Chiang | e4ea748 | 2018-03-15 11:36:41 -0700 | [diff] [blame] | 1772 | const PLANE_TYPE plane_type = get_plane_type(plane); |
| 1773 | const TX_SIZE txs_ctx = get_txsize_entropy_ctx(tx_size); |
Katsuhisa Yuasa | 55c95f8 | 2018-04-03 00:51:22 +0900 | [diff] [blame] | 1774 | const TX_CLASS tx_class = tx_type_to_class[tx_type]; |
Yue Chen | 53b53f0 | 2018-03-29 14:31:23 -0700 | [diff] [blame] | 1775 | const MB_MODE_INFO *mbmi = xd->mi[0]; |
Angie Chiang | e4ea748 | 2018-03-15 11:36:41 -0700 | [diff] [blame] | 1776 | const int bwl = get_txb_bwl(tx_size); |
| 1777 | const int width = get_txb_wide(tx_size); |
| 1778 | const int height = get_txb_high(tx_size); |
| 1779 | assert(width == (1 << bwl)); |
| 1780 | const int is_inter = is_inter_block(mbmi); |
chiyotsai | 9a06d18 | 2020-05-01 17:12:12 -0700 | [diff] [blame] | 1781 | const LV_MAP_COEFF_COST *txb_costs = |
| 1782 | &coeff_costs->coeff_costs[txs_ctx][plane_type]; |
Angie Chiang | e4ea748 | 2018-03-15 11:36:41 -0700 | [diff] [blame] | 1783 | const int eob_multi_size = txsize_log2_minus4[tx_size]; |
| 1784 | const LV_MAP_EOB_COST *txb_eob_costs = |
chiyotsai | 9a06d18 | 2020-05-01 17:12:12 -0700 | [diff] [blame] | 1785 | &coeff_costs->eob_costs[eob_multi_size][plane_type]; |
Angie Chiang | e4ea748 | 2018-03-15 11:36:41 -0700 | [diff] [blame] | 1786 | |
Debargha Mukherjee | 7f3ab7f | 2019-05-30 00:20:50 -0700 | [diff] [blame] | 1787 | const int rshift = |
| 1788 | (sharpness + |
Vishesh | 734eff9 | 2020-06-20 21:46:36 +0530 | [diff] [blame^] | 1789 | (cpi->oxcf.q_cfg.aq_mode == VARIANCE_AQ && mbmi->segment_id < 4 |
Debargha Mukherjee | 7f3ab7f | 2019-05-30 00:20:50 -0700 | [diff] [blame] | 1790 | ? 7 - mbmi->segment_id |
| 1791 | : 2) + |
Vishesh | 734eff9 | 2020-06-20 21:46:36 +0530 | [diff] [blame^] | 1792 | (cpi->oxcf.q_cfg.aq_mode != VARIANCE_AQ && |
| 1793 | cpi->oxcf.q_cfg.deltaq_mode == DELTA_Q_PERCEPTUAL && |
Debargha Mukherjee | 7f3ab7f | 2019-05-30 00:20:50 -0700 | [diff] [blame] | 1794 | cm->delta_q_info.delta_q_present_flag && x->sb_energy_level < 0 |
| 1795 | ? (3 - x->sb_energy_level) |
| 1796 | : 0)); |
Debargha Mukherjee | 53d45d9 | 2019-02-11 12:59:50 -0800 | [diff] [blame] | 1797 | const int64_t rdmult = |
| 1798 | (((int64_t)x->rdmult * |
| 1799 | (plane_rd_mult[is_inter][plane_type] << (2 * (xd->bd - 8)))) + |
| 1800 | 2) >> |
| 1801 | rshift; |
Angie Chiang | e4ea748 | 2018-03-15 11:36:41 -0700 | [diff] [blame] | 1802 | |
| 1803 | uint8_t levels_buf[TX_PAD_2D]; |
| 1804 | uint8_t *const levels = set_levels(levels_buf, width); |
Jim Bankoski | 855ac46 | 2018-06-07 09:05:00 -0700 | [diff] [blame] | 1805 | |
Jim Bankoski | 102f36b | 2019-01-14 09:37:05 -0800 | [diff] [blame] | 1806 | if (eob > 1) av1_txb_init_levels(qcoeff, width, height, levels); |
Angie Chiang | e4ea748 | 2018-03-15 11:36:41 -0700 | [diff] [blame] | 1807 | |
| 1808 | // TODO(angirbird): check iqmatrix |
| 1809 | |
| 1810 | const int non_skip_cost = txb_costs->txb_skip_cost[txb_ctx->txb_skip_ctx][0]; |
| 1811 | const int skip_cost = txb_costs->txb_skip_cost[txb_ctx->txb_skip_ctx][1]; |
Katsuhisa Yuasa | 55c95f8 | 2018-04-03 00:51:22 +0900 | [diff] [blame] | 1812 | const int eob_cost = get_eob_cost(eob, txb_eob_costs, txb_costs, tx_class); |
Angie Chiang | e4ea748 | 2018-03-15 11:36:41 -0700 | [diff] [blame] | 1813 | int accu_rate = eob_cost; |
| 1814 | int64_t accu_dist = 0; |
| 1815 | int si = eob - 1; |
| 1816 | const int ci = scan[si]; |
| 1817 | const tran_low_t qc = qcoeff[ci]; |
| 1818 | const tran_low_t abs_qc = abs(qc); |
Angie Chiang | 40b0731 | 2018-03-30 10:42:55 -0700 | [diff] [blame] | 1819 | const int sign = qc < 0; |
Angie Chiang | e4ea748 | 2018-03-15 11:36:41 -0700 | [diff] [blame] | 1820 | const int max_nz_num = 2; |
| 1821 | int nz_num = 1; |
| 1822 | int nz_ci[3] = { ci, 0, 0 }; |
| 1823 | if (abs_qc >= 2) { |
Katsuhisa Yuasa | 55c95f8 | 2018-04-03 00:51:22 +0900 | [diff] [blame] | 1824 | update_coeff_general(&accu_rate, &accu_dist, si, eob, tx_size, tx_class, |
| 1825 | bwl, height, rdmult, shift, txb_ctx->dc_sign_ctx, |
| 1826 | dequant, scan, txb_costs, tcoeff, qcoeff, dqcoeff, |
Jingning Han | 37f5354 | 2019-06-06 14:22:17 -0700 | [diff] [blame] | 1827 | levels, iqmatrix); |
Angie Chiang | e4ea748 | 2018-03-15 11:36:41 -0700 | [diff] [blame] | 1828 | --si; |
| 1829 | } else { |
| 1830 | assert(abs_qc == 1); |
wenyao.liu | 0be5901 | 2018-12-24 10:27:31 +0800 | [diff] [blame] | 1831 | const int coeff_ctx = get_lower_levels_ctx_eob(bwl, height, si); |
Jim Bankoski | 102f36b | 2019-01-14 09:37:05 -0800 | [diff] [blame] | 1832 | accu_rate += |
| 1833 | get_coeff_cost_eob(ci, abs_qc, sign, coeff_ctx, txb_ctx->dc_sign_ctx, |
| 1834 | txb_costs, bwl, tx_class); |
Angie Chiang | e4ea748 | 2018-03-15 11:36:41 -0700 | [diff] [blame] | 1835 | const tran_low_t tqc = tcoeff[ci]; |
| 1836 | const tran_low_t dqc = dqcoeff[ci]; |
| 1837 | const int64_t dist = get_coeff_dist(tqc, dqc, shift); |
| 1838 | const int64_t dist0 = get_coeff_dist(tqc, 0, shift); |
| 1839 | accu_dist += dist - dist0; |
| 1840 | --si; |
| 1841 | } |
| 1842 | |
Katsuhisa Yuasa | 9f63996 | 2018-04-08 18:00:54 +0900 | [diff] [blame] | 1843 | #define UPDATE_COEFF_EOB_CASE(tx_class_literal) \ |
| 1844 | case tx_class_literal: \ |
Sarah Parker | 89df034 | 2019-02-07 22:23:39 -0800 | [diff] [blame] | 1845 | for (; si >= 0 && nz_num <= max_nz_num && !fast_mode; --si) { \ |
Katsuhisa Yuasa | 9f63996 | 2018-04-08 18:00:54 +0900 | [diff] [blame] | 1846 | update_coeff_eob(&accu_rate, &accu_dist, &eob, &nz_num, nz_ci, si, \ |
| 1847 | tx_size, tx_class_literal, bwl, height, \ |
| 1848 | txb_ctx->dc_sign_ctx, rdmult, shift, dequant, scan, \ |
| 1849 | txb_eob_costs, txb_costs, tcoeff, qcoeff, dqcoeff, \ |
Jingning Han | 37f5354 | 2019-06-06 14:22:17 -0700 | [diff] [blame] | 1850 | levels, sharpness, iqmatrix); \ |
Katsuhisa Yuasa | 9f63996 | 2018-04-08 18:00:54 +0900 | [diff] [blame] | 1851 | } \ |
| 1852 | break; |
| 1853 | switch (tx_class) { |
| 1854 | UPDATE_COEFF_EOB_CASE(TX_CLASS_2D); |
| 1855 | UPDATE_COEFF_EOB_CASE(TX_CLASS_HORIZ); |
| 1856 | UPDATE_COEFF_EOB_CASE(TX_CLASS_VERT); |
| 1857 | #undef UPDATE_COEFF_EOB_CASE |
| 1858 | default: assert(false); |
Angie Chiang | e4ea748 | 2018-03-15 11:36:41 -0700 | [diff] [blame] | 1859 | } |
| 1860 | |
Angie Chiang | d0397e1 | 2018-03-27 19:03:20 -0700 | [diff] [blame] | 1861 | if (si == -1 && nz_num <= max_nz_num) { |
| 1862 | update_skip(&accu_rate, accu_dist, &eob, nz_num, nz_ci, rdmult, skip_cost, |
Jim Bankoski | 855ac46 | 2018-06-07 09:05:00 -0700 | [diff] [blame] | 1863 | non_skip_cost, qcoeff, dqcoeff, sharpness); |
Angie Chiang | d0397e1 | 2018-03-27 19:03:20 -0700 | [diff] [blame] | 1864 | } |
| 1865 | |
Katsuhisa Yuasa | 9f63996 | 2018-04-08 18:00:54 +0900 | [diff] [blame] | 1866 | #define UPDATE_COEFF_SIMPLE_CASE(tx_class_literal) \ |
| 1867 | case tx_class_literal: \ |
| 1868 | for (; si >= 1; --si) { \ |
| 1869 | update_coeff_simple(&accu_rate, si, eob, tx_size, tx_class_literal, bwl, \ |
| 1870 | rdmult, shift, dequant, scan, txb_costs, tcoeff, \ |
Jingning Han | 37f5354 | 2019-06-06 14:22:17 -0700 | [diff] [blame] | 1871 | qcoeff, dqcoeff, levels, iqmatrix); \ |
Katsuhisa Yuasa | 9f63996 | 2018-04-08 18:00:54 +0900 | [diff] [blame] | 1872 | } \ |
| 1873 | break; |
| 1874 | switch (tx_class) { |
| 1875 | UPDATE_COEFF_SIMPLE_CASE(TX_CLASS_2D); |
| 1876 | UPDATE_COEFF_SIMPLE_CASE(TX_CLASS_HORIZ); |
| 1877 | UPDATE_COEFF_SIMPLE_CASE(TX_CLASS_VERT); |
| 1878 | #undef UPDATE_COEFF_SIMPLE_CASE |
| 1879 | default: assert(false); |
Angie Chiang | e4ea748 | 2018-03-15 11:36:41 -0700 | [diff] [blame] | 1880 | } |
| 1881 | |
| 1882 | // DC position |
| 1883 | if (si == 0) { |
Angie Chiang | d0397e1 | 2018-03-27 19:03:20 -0700 | [diff] [blame] | 1884 | // no need to update accu_dist because it's not used after this point |
| 1885 | int64_t dummy_dist = 0; |
Katsuhisa Yuasa | 55c95f8 | 2018-04-03 00:51:22 +0900 | [diff] [blame] | 1886 | update_coeff_general(&accu_rate, &dummy_dist, si, eob, tx_size, tx_class, |
Angie Chiang | d0397e1 | 2018-03-27 19:03:20 -0700 | [diff] [blame] | 1887 | bwl, height, rdmult, shift, txb_ctx->dc_sign_ctx, |
| 1888 | dequant, scan, txb_costs, tcoeff, qcoeff, dqcoeff, |
Jingning Han | 37f5354 | 2019-06-06 14:22:17 -0700 | [diff] [blame] | 1889 | levels, iqmatrix); |
Angie Chiang | e4ea748 | 2018-03-15 11:36:41 -0700 | [diff] [blame] | 1890 | } |
| 1891 | |
Urvang Joshi | b6409e9 | 2020-03-23 11:23:27 -0700 | [diff] [blame] | 1892 | const int tx_type_cost = get_tx_type_cost(x, xd, plane, tx_size, tx_type, |
| 1893 | cm->features.reduced_tx_set_used); |
Angie Chiang | e4ea748 | 2018-03-15 11:36:41 -0700 | [diff] [blame] | 1894 | if (eob == 0) |
| 1895 | accu_rate += skip_cost; |
| 1896 | else |
| 1897 | accu_rate += non_skip_cost + tx_type_cost; |
| 1898 | |
| 1899 | p->eobs[block] = eob; |
| 1900 | p->txb_entropy_ctx[block] = |
| 1901 | av1_get_txb_entropy_context(qcoeff, scan_order, p->eobs[block]); |
| 1902 | |
| 1903 | *rate_cost = accu_rate; |
| 1904 | return eob; |
| 1905 | } |
| 1906 | |
Angie Chiang | 40b0731 | 2018-03-30 10:42:55 -0700 | [diff] [blame] | 1907 | // This function is deprecated, but we keep it here because hash trellis |
Angie Chiang | e4ea748 | 2018-03-15 11:36:41 -0700 | [diff] [blame] | 1908 | // is not integrated with av1_optimize_txb_new yet |
Yaowu Xu | efcf1e9 | 2018-01-12 17:05:46 -0800 | [diff] [blame] | 1909 | int av1_optimize_txb(const struct AV1_COMP *cpi, MACROBLOCK *x, int plane, |
Jingning Han | 7eab9ff | 2017-07-06 10:12:54 -0700 | [diff] [blame] | 1910 | int blk_row, int blk_col, int block, TX_SIZE tx_size, |
Cheng Chen | 82775f6 | 2018-01-18 12:09:54 -0800 | [diff] [blame] | 1911 | TXB_CTX *txb_ctx, int fast_mode, int *rate_cost) { |
Michelle Findlay-Olynyk | fbab062 | 2017-12-13 14:10:56 -0800 | [diff] [blame] | 1912 | const AV1_COMMON *cm = &cpi->common; |
Urvang Joshi | b6409e9 | 2020-03-23 11:23:27 -0700 | [diff] [blame] | 1913 | const int reduced_tx_set_used = cm->features.reduced_tx_set_used; |
Angie Chiang | 07c57f3 | 2017-05-30 18:18:33 -0700 | [diff] [blame] | 1914 | MACROBLOCKD *const xd = &x->e_mbd; |
| 1915 | const PLANE_TYPE plane_type = get_plane_type(plane); |
Debargha Mukherjee | b3eda2f | 2017-11-28 16:00:20 -0800 | [diff] [blame] | 1916 | const TX_SIZE txs_ctx = get_txsize_entropy_ctx(tx_size); |
Hui Su | 37980cc | 2019-10-09 10:16:35 -0700 | [diff] [blame] | 1917 | const TX_TYPE tx_type = av1_get_tx_type(xd, plane_type, blk_row, blk_col, |
Yaowu Xu | 33fd0a0 | 2019-10-15 18:23:29 -0700 | [diff] [blame] | 1918 | tx_size, reduced_tx_set_used); |
Yue Chen | 53b53f0 | 2018-03-29 14:31:23 -0700 | [diff] [blame] | 1919 | const MB_MODE_INFO *mbmi = xd->mi[0]; |
Angie Chiang | 07c57f3 | 2017-05-30 18:18:33 -0700 | [diff] [blame] | 1920 | const struct macroblock_plane *p = &x->plane[plane]; |
Angie Chiang | 07c57f3 | 2017-05-30 18:18:33 -0700 | [diff] [blame] | 1921 | const int eob = p->eobs[block]; |
Yaowu Xu | 18a4996 | 2019-10-16 12:47:04 -0700 | [diff] [blame] | 1922 | const int block_offset = BLOCK_OFFSET(block); |
| 1923 | tran_low_t *qcoeff = p->qcoeff + block_offset; |
Urvang Joshi | 9543ad7 | 2020-04-17 16:59:46 -0700 | [diff] [blame] | 1924 | tran_low_t *dqcoeff = p->dqcoeff + block_offset; |
Yaowu Xu | 18a4996 | 2019-10-16 12:47:04 -0700 | [diff] [blame] | 1925 | const tran_low_t *tcoeff = p->coeff + block_offset; |
Monty Montgomery | 125c0fc | 2017-10-26 00:44:35 -0400 | [diff] [blame] | 1926 | const int16_t *dequant = p->dequant_QTX; |
Urvang Joshi | 8089315 | 2017-10-27 11:51:14 -0700 | [diff] [blame] | 1927 | const int seg_eob = av1_get_max_eob(tx_size); |
Angie Chiang | a9ba58e | 2017-12-01 19:22:43 -0800 | [diff] [blame] | 1928 | const int bwl = get_txb_bwl(tx_size); |
| 1929 | const int width = get_txb_wide(tx_size); |
| 1930 | const int height = get_txb_high(tx_size); |
Angie Chiang | 07c57f3 | 2017-05-30 18:18:33 -0700 | [diff] [blame] | 1931 | const int is_inter = is_inter_block(mbmi); |
Yaowu Xu | f1e1293 | 2018-02-26 15:50:09 -0800 | [diff] [blame] | 1932 | const SCAN_ORDER *const scan_order = get_scan(tx_size, tx_type); |
chiyotsai | 9a06d18 | 2020-05-01 17:12:12 -0700 | [diff] [blame] | 1933 | const CoeffCosts *coeff_costs = &x->coeff_costs; |
| 1934 | const LV_MAP_COEFF_COST *txb_costs = |
| 1935 | &coeff_costs->coeff_costs[txs_ctx][plane_type]; |
Dake He | 0db7d0e | 2017-12-21 15:23:20 -0800 | [diff] [blame] | 1936 | const int eob_multi_size = txsize_log2_minus4[tx_size]; |
| 1937 | const LV_MAP_EOB_COST txb_eob_costs = |
chiyotsai | 9a06d18 | 2020-05-01 17:12:12 -0700 | [diff] [blame] | 1938 | coeff_costs->eob_costs[eob_multi_size][plane_type]; |
Angie Chiang | 07c57f3 | 2017-05-30 18:18:33 -0700 | [diff] [blame] | 1939 | |
| 1940 | const int shift = av1_get_tx_scale(tx_size); |
| 1941 | const int64_t rdmult = |
Debargha Mukherjee | 53d45d9 | 2019-02-11 12:59:50 -0800 | [diff] [blame] | 1942 | (((int64_t)x->rdmult * plane_rd_mult[is_inter][plane_type] |
| 1943 | << (2 * (xd->bd - 8))) + |
Jingning Han | b433f4c | 2017-11-17 15:43:59 -0800 | [diff] [blame] | 1944 | 2) >> |
| 1945 | 2; |
Linfeng Zhang | 679d81e | 2017-10-31 15:27:42 -0700 | [diff] [blame] | 1946 | uint8_t levels_buf[TX_PAD_2D]; |
| 1947 | uint8_t *const levels = set_levels(levels_buf, width); |
Angie Chiang | b3167a6 | 2018-01-30 19:37:57 -0800 | [diff] [blame] | 1948 | const qm_val_t *iqmatrix = |
Urvang Joshi | f409a63 | 2020-03-28 01:03:50 -0700 | [diff] [blame] | 1949 | av1_get_iqmatrix(&cpi->common.quant_params, xd, plane, tx_size, tx_type); |
Linfeng Zhang | 1122d7d | 2017-10-31 15:30:28 -0700 | [diff] [blame] | 1950 | assert(width == (1 << bwl)); |
Yaowu Xu | 33fd0a0 | 2019-10-15 18:23:29 -0700 | [diff] [blame] | 1951 | const int tx_type_cost = |
| 1952 | get_tx_type_cost(x, xd, plane, tx_size, tx_type, reduced_tx_set_used); |
Linfeng Zhang | 1015a34 | 2017-10-24 16:20:41 -0700 | [diff] [blame] | 1953 | TxbInfo txb_info = { |
David Turner | b757ce0 | 2018-11-12 15:01:28 +0000 | [diff] [blame] | 1954 | qcoeff, levels, dqcoeff, tcoeff, dequant, shift, tx_size, |
| 1955 | txs_ctx, tx_type, bwl, width, height, eob, seg_eob, |
| 1956 | scan_order, txb_ctx, rdmult, iqmatrix, tx_type_cost, |
Linfeng Zhang | 1015a34 | 2017-10-24 16:20:41 -0700 | [diff] [blame] | 1957 | }; |
| 1958 | |
Yaowu Xu | 74e6335 | 2019-05-06 09:21:33 -0700 | [diff] [blame] | 1959 | #if CONFIG_HTB_TRELLIS |
Michelle Findlay-Olynyk | fbab062 | 2017-12-13 14:10:56 -0800 | [diff] [blame] | 1960 | // Hash based trellis (hbt) speed feature: avoid expensive optimize_txb calls |
Michelle Findlay-Olynyk | dea531d | 2017-12-13 14:10:56 -0800 | [diff] [blame] | 1961 | // by storing the coefficient deltas in a hash table. |
Michelle Findlay-Olynyk | fbab062 | 2017-12-13 14:10:56 -0800 | [diff] [blame] | 1962 | // Currently disabled in speedfeatures.c |
Michelle Findlay-Olynyk | dea531d | 2017-12-13 14:10:56 -0800 | [diff] [blame] | 1963 | if (eob <= HBT_EOB && eob > 0 && cpi->sf.use_hash_based_trellis) { |
Angie Chiang | e15d2e3 | 2018-03-02 15:49:06 -0800 | [diff] [blame] | 1964 | return hbt_create_hashes(&txb_info, txb_costs, &txb_eob_costs, p, block, |
Michelle Findlay-Olynyk | dea531d | 2017-12-13 14:10:56 -0800 | [diff] [blame] | 1965 | fast_mode, rate_cost); |
Michelle Findlay-Olynyk | fbab062 | 2017-12-13 14:10:56 -0800 | [diff] [blame] | 1966 | } |
Yaowu Xu | 74e6335 | 2019-05-06 09:21:33 -0700 | [diff] [blame] | 1967 | #else |
| 1968 | (void)fast_mode; |
| 1969 | #endif // CONFIG_HTB_TRELLIS |
Linfeng Zhang | 1122d7d | 2017-10-31 15:30:28 -0700 | [diff] [blame] | 1970 | av1_txb_init_levels(qcoeff, width, height, levels); |
Urvang Joshi | 70006e4 | 2017-06-14 16:08:55 -0700 | [diff] [blame] | 1971 | |
Angie Chiang | 45385b8 | 2018-03-02 15:25:58 -0800 | [diff] [blame] | 1972 | const int update = |
Angie Chiang | e15d2e3 | 2018-03-02 15:49:06 -0800 | [diff] [blame] | 1973 | optimize_txb(&txb_info, txb_costs, &txb_eob_costs, rate_cost); |
Angie Chiang | 07c57f3 | 2017-05-30 18:18:33 -0700 | [diff] [blame] | 1974 | |
Jingning Han | d7e9911 | 2017-12-13 09:47:45 -0800 | [diff] [blame] | 1975 | if (update) { |
| 1976 | p->eobs[block] = txb_info.eob; |
| 1977 | p->txb_entropy_ctx[block] = |
| 1978 | av1_get_txb_entropy_context(qcoeff, scan_order, txb_info.eob); |
| 1979 | } |
Angie Chiang | 07c57f3 | 2017-05-30 18:18:33 -0700 | [diff] [blame] | 1980 | return txb_info.eob; |
| 1981 | } |
Jingning Han | d7e9911 | 2017-12-13 09:47:45 -0800 | [diff] [blame] | 1982 | |
Angie Chiang | 74e2307 | 2017-03-24 14:54:23 -0700 | [diff] [blame] | 1983 | int av1_get_txb_entropy_context(const tran_low_t *qcoeff, |
| 1984 | const SCAN_ORDER *scan_order, int eob) { |
Linfeng Zhang | db41d1e | 2017-12-05 11:06:20 -0800 | [diff] [blame] | 1985 | const int16_t *const scan = scan_order->scan; |
Angie Chiang | 74e2307 | 2017-03-24 14:54:23 -0700 | [diff] [blame] | 1986 | int cul_level = 0; |
| 1987 | int c; |
Jingning Han | 339cf93 | 2017-09-18 10:17:02 -0700 | [diff] [blame] | 1988 | |
| 1989 | if (eob == 0) return 0; |
Angie Chiang | 74e2307 | 2017-03-24 14:54:23 -0700 | [diff] [blame] | 1990 | for (c = 0; c < eob; ++c) { |
| 1991 | cul_level += abs(qcoeff[scan[c]]); |
Jingning Han | e313765 | 2018-04-02 11:53:47 -0700 | [diff] [blame] | 1992 | if (cul_level > COEFF_CONTEXT_MASK) break; |
Angie Chiang | 74e2307 | 2017-03-24 14:54:23 -0700 | [diff] [blame] | 1993 | } |
| 1994 | |
| 1995 | cul_level = AOMMIN(COEFF_CONTEXT_MASK, cul_level); |
| 1996 | set_dc_sign(&cul_level, qcoeff[0]); |
| 1997 | |
| 1998 | return cul_level; |
| 1999 | } |
| 2000 | |
Hui Su | 6f981af | 2019-05-21 20:49:51 -0700 | [diff] [blame] | 2001 | static void update_tx_type_count(const AV1_COMP *cpi, const AV1_COMMON *cm, |
| 2002 | MACROBLOCKD *xd, int blk_row, int blk_col, |
| 2003 | int plane, TX_SIZE tx_size, |
| 2004 | FRAME_COUNTS *counts, |
Urvang Joshi | 553096a | 2018-05-10 15:27:14 -0700 | [diff] [blame] | 2005 | uint8_t allow_update_cdf) { |
| 2006 | MB_MODE_INFO *mbmi = xd->mi[0]; |
| 2007 | int is_inter = is_inter_block(mbmi); |
Urvang Joshi | b6409e9 | 2020-03-23 11:23:27 -0700 | [diff] [blame] | 2008 | const int reduced_tx_set_used = cm->features.reduced_tx_set_used; |
Urvang Joshi | 553096a | 2018-05-10 15:27:14 -0700 | [diff] [blame] | 2009 | FRAME_CONTEXT *fc = xd->tile_ctx; |
| 2010 | #if !CONFIG_ENTROPY_STATS |
| 2011 | (void)counts; |
| 2012 | #endif // !CONFIG_ENTROPY_STATS |
| 2013 | |
| 2014 | // Only y plane's tx_type is updated |
| 2015 | if (plane > 0) return; |
Hui Su | 37980cc | 2019-10-09 10:16:35 -0700 | [diff] [blame] | 2016 | const TX_TYPE tx_type = av1_get_tx_type(xd, PLANE_TYPE_Y, blk_row, blk_col, |
Yaowu Xu | 33fd0a0 | 2019-10-15 18:23:29 -0700 | [diff] [blame] | 2017 | tx_size, reduced_tx_set_used); |
Hui Su | 6f981af | 2019-05-21 20:49:51 -0700 | [diff] [blame] | 2018 | if (is_inter) { |
Vishesh | 28709f2 | 2020-06-02 14:46:24 +0530 | [diff] [blame] | 2019 | if (cpi->oxcf.txfm_cfg.use_inter_dct_only) { |
Hui Su | 6f981af | 2019-05-21 20:49:51 -0700 | [diff] [blame] | 2020 | assert(tx_type == DCT_DCT); |
| 2021 | } |
| 2022 | } else { |
Vishesh | 28709f2 | 2020-06-02 14:46:24 +0530 | [diff] [blame] | 2023 | if (cpi->oxcf.txfm_cfg.use_intra_dct_only) { |
Hui Su | 6f981af | 2019-05-21 20:49:51 -0700 | [diff] [blame] | 2024 | assert(tx_type == DCT_DCT); |
Vishesh | 28709f2 | 2020-06-02 14:46:24 +0530 | [diff] [blame] | 2025 | } else if (cpi->oxcf.txfm_cfg.use_intra_default_tx_only) { |
Hui Su | 6f981af | 2019-05-21 20:49:51 -0700 | [diff] [blame] | 2026 | const TX_TYPE default_type = get_default_tx_type( |
| 2027 | PLANE_TYPE_Y, xd, tx_size, cpi->is_screen_content_type); |
| 2028 | (void)default_type; |
| 2029 | assert(tx_type == default_type); |
| 2030 | } |
| 2031 | } |
| 2032 | |
Yaowu Xu | 33fd0a0 | 2019-10-15 18:23:29 -0700 | [diff] [blame] | 2033 | if (get_ext_tx_types(tx_size, is_inter, reduced_tx_set_used) > 1 && |
chiyotsai | 8c004e1 | 2020-04-17 15:52:08 -0700 | [diff] [blame] | 2034 | cm->quant_params.base_qindex > 0 && !mbmi->skip_txfm && |
Urvang Joshi | 553096a | 2018-05-10 15:27:14 -0700 | [diff] [blame] | 2035 | !segfeature_active(&cm->seg, mbmi->segment_id, SEG_LVL_SKIP)) { |
Yaowu Xu | 33fd0a0 | 2019-10-15 18:23:29 -0700 | [diff] [blame] | 2036 | const int eset = get_ext_tx_set(tx_size, is_inter, reduced_tx_set_used); |
Urvang Joshi | 553096a | 2018-05-10 15:27:14 -0700 | [diff] [blame] | 2037 | if (eset > 0) { |
| 2038 | const TxSetType tx_set_type = |
Yaowu Xu | 33fd0a0 | 2019-10-15 18:23:29 -0700 | [diff] [blame] | 2039 | av1_get_ext_tx_set_type(tx_size, is_inter, reduced_tx_set_used); |
Urvang Joshi | 553096a | 2018-05-10 15:27:14 -0700 | [diff] [blame] | 2040 | if (is_inter) { |
| 2041 | if (allow_update_cdf) { |
| 2042 | update_cdf(fc->inter_ext_tx_cdf[eset][txsize_sqr_map[tx_size]], |
| 2043 | av1_ext_tx_ind[tx_set_type][tx_type], |
| 2044 | av1_num_ext_tx_set[tx_set_type]); |
| 2045 | } |
| 2046 | #if CONFIG_ENTROPY_STATS |
| 2047 | ++counts->inter_ext_tx[eset][txsize_sqr_map[tx_size]] |
| 2048 | [av1_ext_tx_ind[tx_set_type][tx_type]]; |
| 2049 | #endif // CONFIG_ENTROPY_STATS |
| 2050 | } else { |
| 2051 | PREDICTION_MODE intra_dir; |
| 2052 | if (mbmi->filter_intra_mode_info.use_filter_intra) |
| 2053 | intra_dir = fimode_to_intradir[mbmi->filter_intra_mode_info |
| 2054 | .filter_intra_mode]; |
| 2055 | else |
| 2056 | intra_dir = mbmi->mode; |
| 2057 | #if CONFIG_ENTROPY_STATS |
| 2058 | ++counts->intra_ext_tx[eset][txsize_sqr_map[tx_size]][intra_dir] |
| 2059 | [av1_ext_tx_ind[tx_set_type][tx_type]]; |
| 2060 | #endif // CONFIG_ENTROPY_STATS |
| 2061 | if (allow_update_cdf) { |
| 2062 | update_cdf( |
| 2063 | fc->intra_ext_tx_cdf[eset][txsize_sqr_map[tx_size]][intra_dir], |
| 2064 | av1_ext_tx_ind[tx_set_type][tx_type], |
| 2065 | av1_num_ext_tx_set[tx_set_type]); |
| 2066 | } |
| 2067 | } |
| 2068 | } |
| 2069 | } |
| 2070 | } |
| 2071 | |
Jingning Han | 4fe5f67 | 2017-05-19 15:46:07 -0700 | [diff] [blame] | 2072 | void av1_update_and_record_txb_context(int plane, int block, int blk_row, |
| 2073 | int blk_col, BLOCK_SIZE plane_bsize, |
| 2074 | TX_SIZE tx_size, void *arg) { |
Jingning Han | 6171ae7 | 2017-05-18 20:15:06 -0700 | [diff] [blame] | 2075 | struct tokenize_b_args *const args = arg; |
Angie Chiang | 0397eda | 2017-03-15 16:57:14 -0700 | [diff] [blame] | 2076 | const AV1_COMP *cpi = args->cpi; |
| 2077 | const AV1_COMMON *cm = &cpi->common; |
| 2078 | ThreadData *const td = args->td; |
| 2079 | MACROBLOCK *const x = &td->mb; |
| 2080 | MACROBLOCKD *const xd = &x->e_mbd; |
| 2081 | struct macroblock_plane *p = &x->plane[plane]; |
| 2082 | struct macroblockd_plane *pd = &xd->plane[plane]; |
Hui Su | 8763cd3 | 2018-04-02 12:29:05 -0700 | [diff] [blame] | 2083 | const int eob = p->eobs[block]; |
Yaowu Xu | 18a4996 | 2019-10-16 12:47:04 -0700 | [diff] [blame] | 2084 | const int block_offset = BLOCK_OFFSET(block); |
Yaowu Xu | f3dba2c | 2020-01-03 11:03:03 -0800 | [diff] [blame] | 2085 | tran_low_t *qcoeff = p->qcoeff + block_offset; |
Hui Su | 8763cd3 | 2018-04-02 12:29:05 -0700 | [diff] [blame] | 2086 | const PLANE_TYPE plane_type = pd->plane_type; |
Urvang Joshi | b6409e9 | 2020-03-23 11:23:27 -0700 | [diff] [blame] | 2087 | const TX_TYPE tx_type = |
| 2088 | av1_get_tx_type(xd, plane_type, blk_row, blk_col, tx_size, |
| 2089 | cm->features.reduced_tx_set_used); |
Hui Su | 8763cd3 | 2018-04-02 12:29:05 -0700 | [diff] [blame] | 2090 | const SCAN_ORDER *const scan_order = get_scan(tx_size, tx_type); |
Yaowu Xu | f3dba2c | 2020-01-03 11:03:03 -0800 | [diff] [blame] | 2091 | tran_low_t *tcoeff; |
| 2092 | assert(args->dry_run != DRY_RUN_COSTCOEFFS); |
| 2093 | if (args->dry_run == OUTPUT_ENABLED) { |
| 2094 | MB_MODE_INFO *mbmi = xd->mi[0]; |
| 2095 | TXB_CTX txb_ctx; |
Urvang Joshi | 46ff552 | 2020-03-30 15:27:37 -0700 | [diff] [blame] | 2096 | get_txb_ctx(plane_bsize, tx_size, plane, |
| 2097 | pd->above_entropy_context + blk_col, |
| 2098 | pd->left_entropy_context + blk_row, &txb_ctx); |
Yaowu Xu | f3dba2c | 2020-01-03 11:03:03 -0800 | [diff] [blame] | 2099 | const int bwl = get_txb_bwl(tx_size); |
| 2100 | const int width = get_txb_wide(tx_size); |
| 2101 | const int height = get_txb_high(tx_size); |
| 2102 | const uint8_t allow_update_cdf = args->allow_update_cdf; |
| 2103 | const TX_SIZE txsize_ctx = get_txsize_entropy_ctx(tx_size); |
| 2104 | FRAME_CONTEXT *ec_ctx = xd->tile_ctx; |
| 2105 | #if CONFIG_ENTROPY_STATS |
| 2106 | int cdf_idx = cm->coef_cdf_category; |
| 2107 | ++td->counts->txb_skip[cdf_idx][txsize_ctx][txb_ctx.txb_skip_ctx][eob == 0]; |
| 2108 | #endif // CONFIG_ENTROPY_STATS |
| 2109 | if (allow_update_cdf) { |
| 2110 | update_cdf(ec_ctx->txb_skip_cdf[txsize_ctx][txb_ctx.txb_skip_ctx], |
| 2111 | eob == 0, 2); |
| 2112 | } |
Yunqing Wang | db70bf4 | 2019-08-19 09:28:11 -0700 | [diff] [blame] | 2113 | |
Yaowu Xu | f3dba2c | 2020-01-03 11:03:03 -0800 | [diff] [blame] | 2114 | CB_COEFF_BUFFER *cb_coef_buff = x->cb_coef_buff; |
| 2115 | const int txb_offset = |
| 2116 | x->mbmi_ext_frame->cb_offset / (TX_SIZE_W_MIN * TX_SIZE_H_MIN); |
| 2117 | uint16_t *eob_txb = cb_coef_buff->eobs[plane] + txb_offset; |
| 2118 | uint8_t *const entropy_ctx = cb_coef_buff->entropy_ctx[plane] + txb_offset; |
| 2119 | entropy_ctx[block] = txb_ctx.txb_skip_ctx; |
| 2120 | eob_txb[block] = eob; |
| 2121 | |
| 2122 | if (eob == 0) { |
Urvang Joshi | 46ff552 | 2020-03-30 15:27:37 -0700 | [diff] [blame] | 2123 | av1_set_entropy_contexts(xd, pd, plane, plane_bsize, tx_size, 0, blk_col, |
| 2124 | blk_row); |
Yaowu Xu | f3dba2c | 2020-01-03 11:03:03 -0800 | [diff] [blame] | 2125 | return; |
| 2126 | } |
| 2127 | const int segment_id = mbmi->segment_id; |
| 2128 | const int seg_eob = av1_get_tx_eob(&cpi->common.seg, segment_id, tx_size); |
| 2129 | tran_low_t *tcoeff_txb = |
| 2130 | cb_coef_buff->tcoeff[plane] + x->mbmi_ext_frame->cb_offset; |
| 2131 | tcoeff = tcoeff_txb + block_offset; |
| 2132 | memcpy(tcoeff, qcoeff, sizeof(*tcoeff) * seg_eob); |
| 2133 | |
| 2134 | uint8_t levels_buf[TX_PAD_2D]; |
| 2135 | uint8_t *const levels = set_levels(levels_buf, width); |
| 2136 | av1_txb_init_levels(tcoeff, width, height, levels); |
| 2137 | update_tx_type_count(cpi, cm, xd, blk_row, blk_col, plane, tx_size, |
| 2138 | td->counts, allow_update_cdf); |
| 2139 | |
| 2140 | const TX_CLASS tx_class = tx_type_to_class[tx_type]; |
| 2141 | const int16_t *const scan = scan_order->scan; |
| 2142 | |
| 2143 | // record tx type usage |
| 2144 | td->rd_counts.tx_type_used[tx_size][tx_type]++; |
Yunqing Wang | db70bf4 | 2019-08-19 09:28:11 -0700 | [diff] [blame] | 2145 | |
Yue Chen | 198738d | 2018-03-08 17:26:01 -0800 | [diff] [blame] | 2146 | #if CONFIG_ENTROPY_STATS |
Yaowu Xu | f3dba2c | 2020-01-03 11:03:03 -0800 | [diff] [blame] | 2147 | av1_update_eob_context(cdf_idx, eob, tx_size, tx_class, plane_type, ec_ctx, |
| 2148 | td->counts, allow_update_cdf); |
Yue Chen | 198738d | 2018-03-08 17:26:01 -0800 | [diff] [blame] | 2149 | #else |
Yaowu Xu | f3dba2c | 2020-01-03 11:03:03 -0800 | [diff] [blame] | 2150 | av1_update_eob_context(eob, tx_size, tx_class, plane_type, ec_ctx, |
| 2151 | allow_update_cdf); |
Yue Chen | 198738d | 2018-03-08 17:26:01 -0800 | [diff] [blame] | 2152 | #endif |
Hui Su | 8763cd3 | 2018-04-02 12:29:05 -0700 | [diff] [blame] | 2153 | |
Yaowu Xu | f3dba2c | 2020-01-03 11:03:03 -0800 | [diff] [blame] | 2154 | DECLARE_ALIGNED(16, int8_t, coeff_contexts[MAX_TX_SQUARE]); |
| 2155 | av1_get_nz_map_contexts(levels, scan, eob, tx_size, tx_class, |
| 2156 | coeff_contexts); |
Linfeng Zhang | d67c13f | 2017-12-11 11:49:12 -0800 | [diff] [blame] | 2157 | |
Yaowu Xu | f3dba2c | 2020-01-03 11:03:03 -0800 | [diff] [blame] | 2158 | for (int c = eob - 1; c >= 0; --c) { |
| 2159 | const int pos = scan[c]; |
| 2160 | const int coeff_ctx = coeff_contexts[pos]; |
| 2161 | const tran_low_t v = qcoeff[pos]; |
| 2162 | const tran_low_t level = abs(v); |
Dake He | 03a3292 | 2017-10-31 08:06:45 -0700 | [diff] [blame] | 2163 | |
Yaowu Xu | f3dba2c | 2020-01-03 11:03:03 -0800 | [diff] [blame] | 2164 | if (allow_update_cdf) { |
| 2165 | if (c == eob - 1) { |
| 2166 | assert(coeff_ctx < 4); |
| 2167 | update_cdf( |
| 2168 | ec_ctx->coeff_base_eob_cdf[txsize_ctx][plane_type][coeff_ctx], |
| 2169 | AOMMIN(level, 3) - 1, 3); |
| 2170 | } else { |
| 2171 | update_cdf(ec_ctx->coeff_base_cdf[txsize_ctx][plane_type][coeff_ctx], |
| 2172 | AOMMIN(level, 3), 4); |
| 2173 | } |
Dake He | 4d44769 | 2017-12-15 09:10:06 -0800 | [diff] [blame] | 2174 | } |
Dake He | 4d44769 | 2017-12-15 09:10:06 -0800 | [diff] [blame] | 2175 | if (c == eob - 1) { |
| 2176 | assert(coeff_ctx < 4); |
Yue Chen | 198738d | 2018-03-08 17:26:01 -0800 | [diff] [blame] | 2177 | #if CONFIG_ENTROPY_STATS |
| 2178 | ++td->counts->coeff_base_eob_multi[cdf_idx][txsize_ctx][plane_type] |
| 2179 | [coeff_ctx][AOMMIN(level, 3) - 1]; |
Dake He | 4d44769 | 2017-12-15 09:10:06 -0800 | [diff] [blame] | 2180 | } else { |
Yue Chen | 198738d | 2018-03-08 17:26:01 -0800 | [diff] [blame] | 2181 | ++td->counts->coeff_base_multi[cdf_idx][txsize_ctx][plane_type] |
| 2182 | [coeff_ctx][AOMMIN(level, 3)]; |
| 2183 | #endif |
Angie Chiang | 2cc5eb3 | 2018-02-14 18:37:22 -0800 | [diff] [blame] | 2184 | } |
Yaowu Xu | f3dba2c | 2020-01-03 11:03:03 -0800 | [diff] [blame] | 2185 | if (level > NUM_BASE_LEVELS) { |
| 2186 | const int base_range = level - 1 - NUM_BASE_LEVELS; |
| 2187 | const int br_ctx = get_br_ctx(levels, pos, bwl, tx_class); |
| 2188 | for (int idx = 0; idx < COEFF_BASE_RANGE; idx += BR_CDF_SIZE - 1) { |
| 2189 | const int k = AOMMIN(base_range - idx, BR_CDF_SIZE - 1); |
| 2190 | if (allow_update_cdf) { |
| 2191 | update_cdf(ec_ctx->coeff_br_cdf[AOMMIN(txsize_ctx, TX_32X32)] |
| 2192 | [plane_type][br_ctx], |
| 2193 | k, BR_CDF_SIZE); |
| 2194 | } |
| 2195 | for (int lps = 0; lps < BR_CDF_SIZE - 1; lps++) { |
Angie Chiang | 2cc5eb3 | 2018-02-14 18:37:22 -0800 | [diff] [blame] | 2196 | #if CONFIG_ENTROPY_STATS |
Yaowu Xu | f3dba2c | 2020-01-03 11:03:03 -0800 | [diff] [blame] | 2197 | ++td->counts->coeff_lps[AOMMIN(txsize_ctx, TX_32X32)][plane_type] |
| 2198 | [lps][br_ctx][lps == k]; |
Angie Chiang | 2cc5eb3 | 2018-02-14 18:37:22 -0800 | [diff] [blame] | 2199 | #endif // CONFIG_ENTROPY_STATS |
Yaowu Xu | f3dba2c | 2020-01-03 11:03:03 -0800 | [diff] [blame] | 2200 | if (lps == k) break; |
| 2201 | } |
Yue Chen | 198738d | 2018-03-08 17:26:01 -0800 | [diff] [blame] | 2202 | #if CONFIG_ENTROPY_STATS |
Yaowu Xu | f3dba2c | 2020-01-03 11:03:03 -0800 | [diff] [blame] | 2203 | ++td->counts->coeff_lps_multi[cdf_idx][AOMMIN(txsize_ctx, TX_32X32)] |
| 2204 | [plane_type][br_ctx][k]; |
Yue Chen | 198738d | 2018-03-08 17:26:01 -0800 | [diff] [blame] | 2205 | #endif |
Yaowu Xu | f3dba2c | 2020-01-03 11:03:03 -0800 | [diff] [blame] | 2206 | if (k < BR_CDF_SIZE - 1) break; |
| 2207 | } |
Dake He | 5988177 | 2017-11-24 07:00:02 -0800 | [diff] [blame] | 2208 | } |
| 2209 | } |
Yaowu Xu | f3dba2c | 2020-01-03 11:03:03 -0800 | [diff] [blame] | 2210 | // Update the context needed to code the DC sign (if applicable) |
| 2211 | if (tcoeff[0] != 0) { |
| 2212 | const int dc_sign = (tcoeff[0] < 0) ? 1 : 0; |
| 2213 | const int dc_sign_ctx = txb_ctx.dc_sign_ctx; |
Hui Su | 1e95989 | 2018-01-22 12:14:43 -0800 | [diff] [blame] | 2214 | #if CONFIG_ENTROPY_STATS |
Yaowu Xu | f3dba2c | 2020-01-03 11:03:03 -0800 | [diff] [blame] | 2215 | ++td->counts->dc_sign[plane_type][dc_sign_ctx][dc_sign]; |
Hui Su | 1e95989 | 2018-01-22 12:14:43 -0800 | [diff] [blame] | 2216 | #endif // CONFIG_ENTROPY_STATS |
Yaowu Xu | f3dba2c | 2020-01-03 11:03:03 -0800 | [diff] [blame] | 2217 | if (allow_update_cdf) |
| 2218 | update_cdf(ec_ctx->dc_sign_cdf[plane_type][dc_sign_ctx], dc_sign, 2); |
| 2219 | entropy_ctx[block] |= dc_sign_ctx << DC_SIGN_CTX_SHIFT; |
| 2220 | } |
| 2221 | } else { |
| 2222 | tcoeff = qcoeff; |
Dake He | 43edb76 | 2017-10-26 10:29:46 -0700 | [diff] [blame] | 2223 | } |
Hui Su | 8763cd3 | 2018-04-02 12:29:05 -0700 | [diff] [blame] | 2224 | const int cul_level = av1_get_txb_entropy_context(tcoeff, scan_order, eob); |
Urvang Joshi | 46ff552 | 2020-03-30 15:27:37 -0700 | [diff] [blame] | 2225 | av1_set_entropy_contexts(xd, pd, plane, plane_bsize, tx_size, cul_level, |
| 2226 | blk_col, blk_row); |
Angie Chiang | 0397eda | 2017-03-15 16:57:14 -0700 | [diff] [blame] | 2227 | } |
| 2228 | |
| 2229 | void av1_update_txb_context(const AV1_COMP *cpi, ThreadData *td, |
Hui Su | 95e9c0c | 2019-12-09 15:21:35 -0800 | [diff] [blame] | 2230 | RUN_TYPE dry_run, BLOCK_SIZE bsize, |
| 2231 | uint8_t allow_update_cdf) { |
Imdad Sardharwalla | af8e264 | 2018-01-19 11:46:34 +0000 | [diff] [blame] | 2232 | const AV1_COMMON *const cm = &cpi->common; |
| 2233 | const int num_planes = av1_num_planes(cm); |
Angie Chiang | 0397eda | 2017-03-15 16:57:14 -0700 | [diff] [blame] | 2234 | MACROBLOCK *const x = &td->mb; |
| 2235 | MACROBLOCKD *const xd = &x->e_mbd; |
Yue Chen | 53b53f0 | 2018-03-29 14:31:23 -0700 | [diff] [blame] | 2236 | MB_MODE_INFO *const mbmi = xd->mi[0]; |
Yaowu Xu | f3dba2c | 2020-01-03 11:03:03 -0800 | [diff] [blame] | 2237 | struct tokenize_b_args arg = { cpi, td, 0, allow_update_cdf, dry_run }; |
chiyotsai | 8c004e1 | 2020-04-17 15:52:08 -0700 | [diff] [blame] | 2238 | if (mbmi->skip_txfm) { |
Urvang Joshi | 46ff552 | 2020-03-30 15:27:37 -0700 | [diff] [blame] | 2239 | av1_reset_entropy_context(xd, bsize, num_planes); |
Angie Chiang | 0397eda | 2017-03-15 16:57:14 -0700 | [diff] [blame] | 2240 | return; |
| 2241 | } |
Yaowu Xu | 88039f0 | 2020-01-03 12:09:18 -0800 | [diff] [blame] | 2242 | |
| 2243 | for (int plane = 0; plane < num_planes; ++plane) { |
Hui Su | 474e1e1 | 2020-02-27 15:46:36 -0800 | [diff] [blame] | 2244 | if (plane && !xd->is_chroma_ref) break; |
Yaowu Xu | 88039f0 | 2020-01-03 12:09:18 -0800 | [diff] [blame] | 2245 | const struct macroblockd_plane *const pd = &xd->plane[plane]; |
| 2246 | const int ss_x = pd->subsampling_x; |
| 2247 | const int ss_y = pd->subsampling_y; |
Yaowu Xu | 88039f0 | 2020-01-03 12:09:18 -0800 | [diff] [blame] | 2248 | const BLOCK_SIZE plane_bsize = get_plane_block_size(bsize, ss_x, ss_y); |
| 2249 | av1_foreach_transformed_block_in_plane( |
| 2250 | xd, plane_bsize, plane, av1_update_and_record_txb_context, &arg); |
| 2251 | } |
Angie Chiang | 0397eda | 2017-03-15 16:57:14 -0700 | [diff] [blame] | 2252 | } |
Hui Su | f1b1106 | 2019-04-17 14:18:26 -0700 | [diff] [blame] | 2253 | |
| 2254 | CB_COEFF_BUFFER *av1_get_cb_coeff_buffer(const struct AV1_COMP *cpi, int mi_row, |
| 2255 | int mi_col) { |
| 2256 | const AV1_COMMON *const cm = &cpi->common; |
| 2257 | const int mib_size_log2 = cm->seq_params.mib_size_log2; |
Urvang Joshi | 9dc909d | 2020-03-23 16:07:02 -0700 | [diff] [blame] | 2258 | const int stride = (cm->mi_params.mi_cols >> mib_size_log2) + 1; |
Hui Su | f1b1106 | 2019-04-17 14:18:26 -0700 | [diff] [blame] | 2259 | const int offset = |
| 2260 | (mi_row >> mib_size_log2) * stride + (mi_col >> mib_size_log2); |
| 2261 | return cpi->coeff_buffer_base + offset; |
| 2262 | } |