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