blob: 1e861285c149084f7006f28eb00d9ef14801f20f [file] [log] [blame]
Angie Chiang80b82262017-02-24 11:39:47 -08001/*
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 Chiang41220ab2018-02-14 17:48:23 -080012#include "av1/encoder/encodetxb.h"
13
Linfeng Zhangae7b2f32017-11-08 15:46:57 -080014#include "aom_ports/mem.h"
Angie Chiang0397eda2017-03-15 16:57:14 -070015#include "av1/common/blockd.h"
Angie Chiange50f3ec2017-04-10 15:50:33 -070016#include "av1/common/idct.h"
Angie Chiang0397eda2017-03-15 16:57:14 -070017#include "av1/common/pred_common.h"
Angie Chiang41220ab2018-02-14 17:48:23 -080018#include "av1/common/scan.h"
Angie Chiang1628fcc2017-04-13 16:30:30 -070019#include "av1/encoder/bitstream.h"
Angie Chiang47c72182017-02-27 14:30:38 -080020#include "av1/encoder/cost.h"
Angie Chiang41220ab2018-02-14 17:48:23 -080021#include "av1/encoder/encodeframe.h"
Michelle Findlay-Olynykfbab0622017-12-13 14:10:56 -080022#include "av1/encoder/hash.h"
Angie Chiang808d8592017-04-06 18:36:55 -070023#include "av1/encoder/rdopt.h"
Angie Chiang0397eda2017-03-15 16:57:14 -070024#include "av1/encoder/tokenize.h"
Angie Chiang80b82262017-02-24 11:39:47 -080025
Yaowu Xu74e63352019-05-06 09:21:33 -070026#if CONFIG_HTB_TRELLIS
Michelle Findlay-Olynykdea531d2017-12-13 14:10:56 -080027static int hbt_needs_init = 1;
Peng Bin8a204cd2018-04-08 13:07:35 +080028static CRC32C crc_calculator;
Michelle Findlay-Olynykdea531d2017-12-13 14:10:56 -080029static const int HBT_EOB = 16; // also the length in opt_qcoeff
30static const int HBT_TABLE_SIZE = 65536; // 16 bit: holds 65536 'arrays'
31static const int HBT_ARRAY_LENGTH = 256; // 8 bit: 256 entries
32// If removed in hbt_create_hashes or increased beyond int8_t, widen deltas type
33static const int HBT_KICKOUT = 3;
Michelle Findlay-Olynykfbab0622017-12-13 14:10:56 -080034
35typedef struct OptTxbQcoeff {
Michelle Findlay-Olynykdea531d2017-12-13 14:10:56 -080036 // Use larger type if larger/no kickout value is used in hbt_create_hashes
37 int8_t deltas[16];
38 uint32_t hbt_qc_hash;
39 uint32_t hbt_ctx_hash;
40 int init;
41 int rate_cost;
Michelle Findlay-Olynykfbab0622017-12-13 14:10:56 -080042} OptTxbQcoeff;
43
Michelle Findlay-Olynykdea531d2017-12-13 14:10:56 -080044OptTxbQcoeff *hbt_hash_table;
Yaowu Xu74e63352019-05-06 09:21:33 -070045#endif // CONFIG_HTB_TRELLIS
Michelle Findlay-Olynykfbab0622017-12-13 14:10:56 -080046
Dake Hea47cd6c2017-10-13 18:09:58 -070047typedef struct LevelDownStats {
48 int update;
49 tran_low_t low_qc;
50 tran_low_t low_dqc;
Dake Hea47cd6c2017-10-13 18:09:58 -070051 int64_t dist0;
52 int rate;
53 int rate_low;
54 int64_t dist;
55 int64_t dist_low;
56 int64_t rd;
57 int64_t rd_low;
Dake He4d447692017-12-15 09:10:06 -080058 int64_t nz_rd;
Dake Hea47cd6c2017-10-13 18:09:58 -070059 int64_t rd_diff;
60 int cost_diff;
61 int64_t dist_diff;
62 int new_eob;
63} LevelDownStats;
64
Jingning Han37f53542019-06-06 14:22:17 -070065static INLINE int get_dqv(const int16_t *dequant, int coeff_idx,
66 const qm_val_t *iqmatrix) {
67 int dqv = dequant[!!coeff_idx];
68 if (iqmatrix != NULL)
69 dqv =
70 ((iqmatrix[coeff_idx] * dqv) + (1 << (AOM_QM_BITS - 1))) >> AOM_QM_BITS;
71 return dqv;
72}
73
Angie Chiangf0fbf9d2017-03-15 15:01:22 -070074void av1_alloc_txb_buf(AV1_COMP *cpi) {
Jingning Hanf5a4d3b2017-08-27 23:01:19 -070075 AV1_COMMON *cm = &cpi->common;
Urvang Joshi9dc909d2020-03-23 16:07:02 -070076 int size = ((cm->mi_params.mi_rows >> cm->seq_params.mib_size_log2) + 1) *
77 ((cm->mi_params.mi_cols >> cm->seq_params.mib_size_log2) + 1);
Jingning Hanf5a4d3b2017-08-27 23:01:19 -070078
Angie Chiang9367e3e2017-10-02 16:28:11 -070079 av1_free_txb_buf(cpi);
Jingning Hanf5a4d3b2017-08-27 23:01:19 -070080 // TODO(jingning): This should be further reduced.
81 CHECK_MEM_ERROR(cm, cpi->coeff_buffer_base,
Peng Bin27d7ca92018-03-22 22:25:56 +080082 aom_memalign(32, sizeof(*cpi->coeff_buffer_base) * size));
Angie Chiangf0fbf9d2017-03-15 15:01:22 -070083}
84
Angie Chianged9667e2018-03-02 15:10:50 -080085void av1_free_txb_buf(AV1_COMP *cpi) { aom_free(cpi->coeff_buffer_base); }
Angie Chiangf0fbf9d2017-03-15 15:01:22 -070086
Angie Chiang80b82262017-02-24 11:39:47 -080087static void write_golomb(aom_writer *w, int level) {
88 int x = level + 1;
89 int i = x;
90 int length = 0;
91
92 while (i) {
93 i >>= 1;
94 ++length;
95 }
96 assert(length > 0);
97
98 for (i = 0; i < length - 1; ++i) aom_write_bit(w, 0);
99
100 for (i = length - 1; i >= 0; --i) aom_write_bit(w, (x >> i) & 0x01);
101}
102
Dake Hea47cd6c2017-10-13 18:09:58 -0700103static INLINE tran_low_t get_lower_coeff(tran_low_t qc) {
104 if (qc == 0) {
105 return 0;
106 }
107 return qc > 0 ? qc - 1 : qc + 1;
108}
109
Angie Chiangb3167a62018-01-30 19:37:57 -0800110static INLINE tran_low_t qcoeff_to_dqcoeff(tran_low_t qc, int coeff_idx,
Angie Chiangb3167a62018-01-30 19:37:57 -0800111 int dqv, int shift,
112 const qm_val_t *iqmatrix) {
Angie Chiang40b07312018-03-30 10:42:55 -0700113 int sign = qc < 0 ? -1 : 1;
Angie Chiangb3167a62018-01-30 19:37:57 -0800114 if (iqmatrix != NULL)
115 dqv =
116 ((iqmatrix[coeff_idx] * dqv) + (1 << (AOM_QM_BITS - 1))) >> AOM_QM_BITS;
Angie Chiang40b07312018-03-30 10:42:55 -0700117 return sign * ((abs(qc) * dqv) >> shift);
Dake Hea47cd6c2017-10-13 18:09:58 -0700118}
119
120static INLINE int64_t get_coeff_dist(tran_low_t tcoeff, tran_low_t dqcoeff,
121 int shift) {
122 const int64_t diff = (tcoeff - dqcoeff) * (1 << shift);
123 const int64_t error = diff * diff;
124 return error;
125}
126
Urvang Joshiac97d272018-07-31 15:16:52 -0700127static const int8_t eob_to_pos_small[33] = {
128 0, 1, 2, // 0-2
129 3, 3, // 3-4
130 4, 4, 4, 4, // 5-8
131 5, 5, 5, 5, 5, 5, 5, 5, // 9-16
132 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6 // 17-32
133};
134
135static const int8_t eob_to_pos_large[17] = {
136 6, // place holder
137 7, // 33-64
138 8, 8, // 65-128
139 9, 9, 9, 9, // 129-256
140 10, 10, 10, 10, 10, 10, 10, 10, // 257-512
141 11 // 513-
142};
143
144static INLINE int get_eob_pos_token(const int eob, int *const extra) {
145 int t;
146
147 if (eob < 33) {
148 t = eob_to_pos_small[eob];
149 } else {
150 const int e = AOMMIN((eob - 1) >> 5, 16);
151 t = eob_to_pos_large[e];
152 }
153
Yaowu Xu3a19b8a2019-05-01 08:40:42 -0700154 *extra = eob - av1_eob_group_start[t];
Urvang Joshiac97d272018-07-31 15:16:52 -0700155
156 return t;
157}
158
Yue Chen198738d2018-03-08 17:26:01 -0800159#if CONFIG_ENTROPY_STATS
Yaowu Xu49abec82018-03-13 16:09:01 -0700160void av1_update_eob_context(int cdf_idx, int eob, TX_SIZE tx_size,
Katsuhisa Yuasa55c95f82018-04-03 00:51:22 +0900161 TX_CLASS tx_class, PLANE_TYPE plane,
Yunqing Wang0e141b52017-11-02 15:08:58 -0700162 FRAME_CONTEXT *ec_ctx, FRAME_COUNTS *counts,
163 uint8_t allow_update_cdf) {
Yue Chen198738d2018-03-08 17:26:01 -0800164#else
Katsuhisa Yuasa55c95f82018-04-03 00:51:22 +0900165void av1_update_eob_context(int eob, TX_SIZE tx_size, TX_CLASS tx_class,
Yaowu Xu49abec82018-03-13 16:09:01 -0700166 PLANE_TYPE plane, FRAME_CONTEXT *ec_ctx,
167 uint8_t allow_update_cdf) {
Yue Chen198738d2018-03-08 17:26:01 -0800168#endif
Yaowu Xu49abec82018-03-13 16:09:01 -0700169 int eob_extra;
Linfeng Zhang0c72b2f2017-12-04 10:59:28 -0800170 const int eob_pt = get_eob_pos_token(eob, &eob_extra);
Debargha Mukherjeeb3eda2f2017-11-28 16:00:20 -0800171 TX_SIZE txs_ctx = get_txsize_entropy_ctx(tx_size);
Dake Hea47cd6c2017-10-13 18:09:58 -0700172
Dake He0db7d0e2017-12-21 15:23:20 -0800173 const int eob_multi_size = txsize_log2_minus4[tx_size];
Katsuhisa Yuasa55c95f82018-04-03 00:51:22 +0900174 const int eob_multi_ctx = (tx_class == TX_CLASS_2D) ? 0 : 1;
Dake He0db7d0e2017-12-21 15:23:20 -0800175
176 switch (eob_multi_size) {
177 case 0:
Yue Chen198738d2018-03-08 17:26:01 -0800178#if CONFIG_ENTROPY_STATS
179 ++counts->eob_multi16[cdf_idx][plane][eob_multi_ctx][eob_pt - 1];
180#endif
Dake He0db7d0e2017-12-21 15:23:20 -0800181 if (allow_update_cdf)
182 update_cdf(ec_ctx->eob_flag_cdf16[plane][eob_multi_ctx], eob_pt - 1, 5);
183 break;
184 case 1:
Yue Chen198738d2018-03-08 17:26:01 -0800185#if CONFIG_ENTROPY_STATS
186 ++counts->eob_multi32[cdf_idx][plane][eob_multi_ctx][eob_pt - 1];
187#endif
Dake He0db7d0e2017-12-21 15:23:20 -0800188 if (allow_update_cdf)
189 update_cdf(ec_ctx->eob_flag_cdf32[plane][eob_multi_ctx], eob_pt - 1, 6);
190 break;
191 case 2:
Yue Chen198738d2018-03-08 17:26:01 -0800192#if CONFIG_ENTROPY_STATS
193 ++counts->eob_multi64[cdf_idx][plane][eob_multi_ctx][eob_pt - 1];
194#endif
Dake He0db7d0e2017-12-21 15:23:20 -0800195 if (allow_update_cdf)
196 update_cdf(ec_ctx->eob_flag_cdf64[plane][eob_multi_ctx], eob_pt - 1, 7);
197 break;
198 case 3:
Yue Chen198738d2018-03-08 17:26:01 -0800199#if CONFIG_ENTROPY_STATS
200 ++counts->eob_multi128[cdf_idx][plane][eob_multi_ctx][eob_pt - 1];
201#endif
Hui Su1cb1c002018-02-05 18:21:20 -0800202 if (allow_update_cdf) {
Dake He0db7d0e2017-12-21 15:23:20 -0800203 update_cdf(ec_ctx->eob_flag_cdf128[plane][eob_multi_ctx], eob_pt - 1,
204 8);
Hui Su1cb1c002018-02-05 18:21:20 -0800205 }
Dake He0db7d0e2017-12-21 15:23:20 -0800206 break;
207 case 4:
Yue Chen198738d2018-03-08 17:26:01 -0800208#if CONFIG_ENTROPY_STATS
209 ++counts->eob_multi256[cdf_idx][plane][eob_multi_ctx][eob_pt - 1];
210#endif
Hui Su1cb1c002018-02-05 18:21:20 -0800211 if (allow_update_cdf) {
Dake He0db7d0e2017-12-21 15:23:20 -0800212 update_cdf(ec_ctx->eob_flag_cdf256[plane][eob_multi_ctx], eob_pt - 1,
213 9);
Hui Su1cb1c002018-02-05 18:21:20 -0800214 }
Dake He0db7d0e2017-12-21 15:23:20 -0800215 break;
216 case 5:
Yue Chen198738d2018-03-08 17:26:01 -0800217#if CONFIG_ENTROPY_STATS
218 ++counts->eob_multi512[cdf_idx][plane][eob_multi_ctx][eob_pt - 1];
219#endif
Hui Su1cb1c002018-02-05 18:21:20 -0800220 if (allow_update_cdf) {
Dake He0db7d0e2017-12-21 15:23:20 -0800221 update_cdf(ec_ctx->eob_flag_cdf512[plane][eob_multi_ctx], eob_pt - 1,
222 10);
Hui Su1cb1c002018-02-05 18:21:20 -0800223 }
Dake He0db7d0e2017-12-21 15:23:20 -0800224 break;
225 case 6:
226 default:
Yue Chen198738d2018-03-08 17:26:01 -0800227#if CONFIG_ENTROPY_STATS
228 ++counts->eob_multi1024[cdf_idx][plane][eob_multi_ctx][eob_pt - 1];
229#endif
Hui Su1cb1c002018-02-05 18:21:20 -0800230 if (allow_update_cdf) {
Dake He0db7d0e2017-12-21 15:23:20 -0800231 update_cdf(ec_ctx->eob_flag_cdf1024[plane][eob_multi_ctx], eob_pt - 1,
232 11);
Hui Su1cb1c002018-02-05 18:21:20 -0800233 }
Dake He0db7d0e2017-12-21 15:23:20 -0800234 break;
235 }
Jingning Han00803a72017-10-25 16:04:34 -0700236
Yaowu Xu3a19b8a2019-05-01 08:40:42 -0700237 if (av1_eob_offset_bits[eob_pt] > 0) {
Jingning Hanff4a9f82018-06-08 10:48:45 -0700238 int eob_ctx = eob_pt - 3;
Yaowu Xu3a19b8a2019-05-01 08:40:42 -0700239 int eob_shift = av1_eob_offset_bits[eob_pt] - 1;
Angie Chiang7ab884e2017-10-18 15:57:12 -0700240 int bit = (eob_extra & (1 << eob_shift)) ? 1 : 0;
Hui Su1e959892018-01-22 12:14:43 -0800241#if CONFIG_ENTROPY_STATS
Yue Chen198738d2018-03-08 17:26:01 -0800242 counts->eob_extra[cdf_idx][txs_ctx][plane][eob_pt][bit]++;
Hui Su1e959892018-01-22 12:14:43 -0800243#endif // CONFIG_ENTROPY_STATS
Yunqing Wang0e141b52017-11-02 15:08:58 -0700244 if (allow_update_cdf)
Jingning Hanff4a9f82018-06-08 10:48:45 -0700245 update_cdf(ec_ctx->eob_extra_cdf[txs_ctx][plane][eob_ctx], bit, 2);
Angie Chiang7ab884e2017-10-18 15:57:12 -0700246 }
Dake Hea47cd6c2017-10-13 18:09:58 -0700247}
248
Yaowu Xu49abec82018-03-13 16:09:01 -0700249static int get_eob_cost(int eob, const LV_MAP_EOB_COST *txb_eob_costs,
Katsuhisa Yuasa55c95f82018-04-03 00:51:22 +0900250 const LV_MAP_COEFF_COST *txb_costs, TX_CLASS tx_class) {
Yaowu Xu49abec82018-03-13 16:09:01 -0700251 int eob_extra;
Linfeng Zhang0c72b2f2017-12-04 10:59:28 -0800252 const int eob_pt = get_eob_pos_token(eob, &eob_extra);
Dake Hea47cd6c2017-10-13 18:09:58 -0700253 int eob_cost = 0;
Katsuhisa Yuasa55c95f82018-04-03 00:51:22 +0900254 const int eob_multi_ctx = (tx_class == TX_CLASS_2D) ? 0 : 1;
Dake He0db7d0e2017-12-21 15:23:20 -0800255 eob_cost = txb_eob_costs->eob_cost[eob_multi_ctx][eob_pt - 1];
Dake He0db7d0e2017-12-21 15:23:20 -0800256
Yaowu Xu3a19b8a2019-05-01 08:40:42 -0700257 if (av1_eob_offset_bits[eob_pt] > 0) {
Jingning Hanff4a9f82018-06-08 10:48:45 -0700258 const int eob_ctx = eob_pt - 3;
Yaowu Xu3a19b8a2019-05-01 08:40:42 -0700259 const int eob_shift = av1_eob_offset_bits[eob_pt] - 1;
Hui Su751a2332018-01-23 11:35:03 -0800260 const int bit = (eob_extra & (1 << eob_shift)) ? 1 : 0;
Jingning Hanff4a9f82018-06-08 10:48:45 -0700261 eob_cost += txb_costs->eob_extra_cost[eob_ctx][bit];
Yaowu Xu3a19b8a2019-05-01 08:40:42 -0700262 const int offset_bits = av1_eob_offset_bits[eob_pt];
Hui Su751a2332018-01-23 11:35:03 -0800263 if (offset_bits > 1) eob_cost += av1_cost_literal(offset_bits - 1);
Dake Hea47cd6c2017-10-13 18:09:58 -0700264 }
265 return eob_cost;
266}
267
Angie Chiange4ea7482018-03-15 11:36:41 -0700268static INLINE int get_sign_bit_cost(tran_low_t qc, int coeff_idx,
269 const int (*dc_sign_cost)[2],
270 int dc_sign_ctx) {
Angie Chiange4ea7482018-03-15 11:36:41 -0700271 if (coeff_idx == 0) {
Angie Chiang2f94a452018-04-03 20:21:06 -0700272 const int sign = (qc < 0) ? 1 : 0;
Angie Chiange4ea7482018-03-15 11:36:41 -0700273 return dc_sign_cost[dc_sign_ctx][sign];
Angie Chiange4ea7482018-03-15 11:36:41 -0700274 }
Angie Chiang40b07312018-03-30 10:42:55 -0700275 return av1_cost_literal(1);
Angie Chiange4ea7482018-03-15 11:36:41 -0700276}
277
Wenyao Liuf7e53752019-01-22 17:34:44 +0800278static const int golomb_bits_cost[32] = {
279 0, 512, 512 * 3, 512 * 3, 512 * 5, 512 * 5, 512 * 5, 512 * 5,
280 512 * 7, 512 * 7, 512 * 7, 512 * 7, 512 * 7, 512 * 7, 512 * 7, 512 * 7,
281 512 * 9, 512 * 9, 512 * 9, 512 * 9, 512 * 9, 512 * 9, 512 * 9, 512 * 9,
282 512 * 9, 512 * 9, 512 * 9, 512 * 9, 512 * 9, 512 * 9, 512 * 9, 512 * 9
283};
284static const int golomb_cost_diff[32] = {
285 0, 512, 512 * 2, 0, 512 * 2, 0, 0, 0, 512 * 2, 0, 0, 0, 0, 0, 0, 0,
286 512 * 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
287};
288
Angie Chiange4ea7482018-03-15 11:36:41 -0700289static INLINE int get_golomb_cost(int abs_qc) {
290 if (abs_qc >= 1 + NUM_BASE_LEVELS + COEFF_BASE_RANGE) {
291 const int r = abs_qc - COEFF_BASE_RANGE - NUM_BASE_LEVELS;
292 const int length = get_msb(r) + 1;
293 return av1_cost_literal(2 * length - 1);
294 }
295 return 0;
296}
297
Wenyao Liuf7e53752019-01-22 17:34:44 +0800298static INLINE int get_br_cost_with_diff(tran_low_t level, const int *coeff_lps,
299 int *diff) {
300 const int base_range = AOMMIN(level - 1 - NUM_BASE_LEVELS, COEFF_BASE_RANGE);
301 int golomb_bits = 0;
302 if (level <= COEFF_BASE_RANGE + 1 + NUM_BASE_LEVELS)
303 *diff += coeff_lps[base_range + COEFF_BASE_RANGE + 1];
304
305 if (level >= COEFF_BASE_RANGE + 1 + NUM_BASE_LEVELS) {
306 int r = level - COEFF_BASE_RANGE - NUM_BASE_LEVELS;
307 if (r < 32) {
308 golomb_bits = golomb_bits_cost[r];
309 *diff += golomb_cost_diff[r];
310 } else {
311 golomb_bits = get_golomb_cost(level);
312 *diff += (r & (r - 1)) == 0 ? 1024 : 0;
313 }
314 }
315
316 return coeff_lps[base_range] + golomb_bits;
317}
318
Jim Bankoski24b30d72019-01-14 09:07:43 -0800319static INLINE int get_br_cost(tran_low_t level, const int *coeff_lps) {
320 const int base_range = AOMMIN(level - 1 - NUM_BASE_LEVELS, COEFF_BASE_RANGE);
321 return coeff_lps[base_range] + get_golomb_cost(level);
322}
323
Linfeng Zhang1015a342017-10-24 16:20:41 -0700324static int get_coeff_cost(const tran_low_t qc, const int scan_idx,
Sebastien Alaiwan78f7bb92018-01-11 11:02:43 +0100325 const int is_eob, const TxbInfo *const txb_info,
Linfeng Zhang5f1b8ce2017-12-11 15:53:10 -0800326 const LV_MAP_COEFF_COST *const txb_costs,
Katsuhisa Yuasa55c95f82018-04-03 00:51:22 +0900327 const int coeff_ctx, const TX_CLASS tx_class) {
Xing Jinbd91e942018-06-21 13:43:17 +0800328 const TXB_CTX *const txb_ctx = txb_info->txb_ctx;
Angie Chiange4ea7482018-03-15 11:36:41 -0700329 const int is_nz = (qc != 0);
330 const tran_low_t abs_qc = abs(qc);
331 int cost = 0;
332 const int16_t *const scan = txb_info->scan_order->scan;
333 const int pos = scan[scan_idx];
334
335 if (is_eob) {
336 cost += txb_costs->base_eob_cost[coeff_ctx][AOMMIN(abs_qc, 3) - 1];
337 } else {
338 cost += txb_costs->base_cost[coeff_ctx][AOMMIN(abs_qc, 3)];
339 }
340 if (is_nz) {
341 cost += get_sign_bit_cost(qc, scan_idx, txb_costs->dc_sign_cost,
342 txb_ctx->dc_sign_ctx);
343
344 if (abs_qc > NUM_BASE_LEVELS) {
345 const int ctx =
Katsuhisa Yuasa55c95f82018-04-03 00:51:22 +0900346 get_br_ctx(txb_info->levels, pos, txb_info->bwl, tx_class);
Jim Bankoskife171222019-01-14 08:41:57 -0800347 cost += get_br_cost(abs_qc, txb_costs->lps_cost[ctx]);
Angie Chiange4ea7482018-03-15 11:36:41 -0700348 }
349 }
350 return cost;
351}
Dake Hea47cd6c2017-10-13 18:09:58 -0700352
Frederic Barbiere8c67522018-03-27 15:09:57 +0200353static INLINE int get_nz_map_ctx(const uint8_t *const levels,
354 const int coeff_idx, const int bwl,
355 const int height, const int scan_idx,
356 const int is_eob, const TX_SIZE tx_size,
Katsuhisa Yuasa55c95f82018-04-03 00:51:22 +0900357 const TX_CLASS tx_class) {
Frederic Barbiere8c67522018-03-27 15:09:57 +0200358 if (is_eob) {
359 if (scan_idx == 0) return 0;
360 if (scan_idx <= (height << bwl) / 8) return 1;
361 if (scan_idx <= (height << bwl) / 4) return 2;
362 return 3;
363 }
Frederic Barbiere8c67522018-03-27 15:09:57 +0200364 const int stats =
365 get_nz_mag(levels + get_padded_idx(coeff_idx, bwl), bwl, tx_class);
366 return get_nz_map_ctx_from_stats(stats, coeff_idx, bwl, tx_size, tx_class);
367}
368
Linfeng Zhangdb41d1e2017-12-05 11:06:20 -0800369static void get_dist_cost_stats(LevelDownStats *const stats, const int scan_idx,
Ola Hugosson13892102017-11-06 08:01:44 +0100370 const int is_eob,
Linfeng Zhangdb41d1e2017-12-05 11:06:20 -0800371 const LV_MAP_COEFF_COST *const txb_costs,
Katsuhisa Yuasa55c95f82018-04-03 00:51:22 +0900372 const TxbInfo *const txb_info,
373 const TX_CLASS tx_class) {
Linfeng Zhangdb41d1e2017-12-05 11:06:20 -0800374 const int16_t *const scan = txb_info->scan_order->scan;
Dake Hea47cd6c2017-10-13 18:09:58 -0700375 const int coeff_idx = scan[scan_idx];
376 const tran_low_t qc = txb_info->qcoeff[coeff_idx];
Linfeng Zhang1015a342017-10-24 16:20:41 -0700377 const uint8_t *const levels = txb_info->levels;
Dake Hea47cd6c2017-10-13 18:09:58 -0700378 stats->new_eob = -1;
379 stats->update = 0;
Debargha Mukherjeee2f6b162018-01-04 17:23:05 -0800380 stats->rd_low = 0;
381 stats->rd = 0;
Michelle Findlay-Olynykfbab0622017-12-13 14:10:56 -0800382 stats->nz_rd = 0;
Michelle Findlay-Olynykfbab0622017-12-13 14:10:56 -0800383 stats->dist_low = 0;
384 stats->rate_low = 0;
385 stats->low_qc = 0;
Dake Hea47cd6c2017-10-13 18:09:58 -0700386
387 const tran_low_t tqc = txb_info->tcoeff[coeff_idx];
388 const int dqv = txb_info->dequant[coeff_idx != 0];
Sebastien Alaiwan78f7bb92018-01-11 11:02:43 +0100389 const int coeff_ctx =
390 get_nz_map_ctx(levels, coeff_idx, txb_info->bwl, txb_info->height,
Katsuhisa Yuasa55c95f82018-04-03 00:51:22 +0900391 scan_idx, is_eob, txb_info->tx_size, tx_class);
392 const int qc_cost = get_coeff_cost(qc, scan_idx, is_eob, txb_info, txb_costs,
393 coeff_ctx, tx_class);
Angie Chiang99b12422018-03-02 17:15:00 -0800394 assert(qc != 0);
395 const tran_low_t dqc = qcoeff_to_dqcoeff(qc, coeff_idx, dqv, txb_info->shift,
396 txb_info->iqmatrix);
397 const int64_t dqc_dist = get_coeff_dist(tqc, dqc, txb_info->shift);
Cheng Chen79641202018-01-04 18:52:52 -0800398
Angie Chiang99b12422018-03-02 17:15:00 -0800399 // distortion difference when coefficient is quantized to 0
400 const tran_low_t dqc0 =
401 qcoeff_to_dqcoeff(0, coeff_idx, dqv, txb_info->shift, txb_info->iqmatrix);
Cheng Chen79641202018-01-04 18:52:52 -0800402
Angie Chiang99b12422018-03-02 17:15:00 -0800403 stats->dist0 = get_coeff_dist(tqc, dqc0, txb_info->shift);
404 stats->dist = dqc_dist - stats->dist0;
405 stats->rate = qc_cost;
Cheng Chen79641202018-01-04 18:52:52 -0800406
Dake Hea47cd6c2017-10-13 18:09:58 -0700407 stats->rd = RDCOST(txb_info->rdmult, stats->rate, stats->dist);
408
409 stats->low_qc = get_lower_coeff(qc);
Dake Hea47cd6c2017-10-13 18:09:58 -0700410
Dake He4d447692017-12-15 09:10:06 -0800411 if (is_eob && stats->low_qc == 0) {
Dake He4d447692017-12-15 09:10:06 -0800412 stats->rd_low = stats->rd; // disable selection of low_qc in this case.
Ola Hugosson13892102017-11-06 08:01:44 +0100413 } else {
Cheng Chen37d88732018-01-09 14:02:41 -0800414 if (stats->low_qc == 0) {
415 stats->dist_low = 0;
416 } else {
Frederic Barbiere111cba2018-02-20 16:11:28 +0100417 stats->low_dqc = qcoeff_to_dqcoeff(stats->low_qc, coeff_idx, dqv,
418 txb_info->shift, txb_info->iqmatrix);
Cheng Chen37d88732018-01-09 14:02:41 -0800419 const int64_t low_dqc_dist =
420 get_coeff_dist(tqc, stats->low_dqc, txb_info->shift);
421 stats->dist_low = low_dqc_dist - stats->dist0;
422 }
Katsuhisa Yuasa55c95f82018-04-03 00:51:22 +0900423 const int low_qc_cost =
424 get_coeff_cost(stats->low_qc, scan_idx, is_eob, txb_info, txb_costs,
425 coeff_ctx, tx_class);
Dake He4d447692017-12-15 09:10:06 -0800426 stats->rate_low = low_qc_cost;
427 stats->rd_low = RDCOST(txb_info->rdmult, stats->rate_low, stats->dist_low);
Ola Hugosson13892102017-11-06 08:01:44 +0100428 }
Angie Chiangcbbf4f02018-03-02 18:34:38 -0800429}
Cheng Chen37d88732018-01-09 14:02:41 -0800430
Angie Chiangcbbf4f02018-03-02 18:34:38 -0800431static void get_dist_cost_stats_with_eob(
432 LevelDownStats *const stats, const int scan_idx,
Katsuhisa Yuasa55c95f82018-04-03 00:51:22 +0900433 const LV_MAP_COEFF_COST *const txb_costs, const TxbInfo *const txb_info,
434 const TX_CLASS tx_class) {
Angie Chiangcbbf4f02018-03-02 18:34:38 -0800435 const int is_eob = 0;
Katsuhisa Yuasa55c95f82018-04-03 00:51:22 +0900436 get_dist_cost_stats(stats, scan_idx, is_eob, txb_costs, txb_info, tx_class);
Angie Chiangcbbf4f02018-03-02 18:34:38 -0800437
438 const int16_t *const scan = txb_info->scan_order->scan;
439 const int coeff_idx = scan[scan_idx];
440 const tran_low_t qc = txb_info->qcoeff[coeff_idx];
441 const int coeff_ctx_temp = get_nz_map_ctx(
442 txb_info->levels, coeff_idx, txb_info->bwl, txb_info->height, scan_idx, 1,
Katsuhisa Yuasa55c95f82018-04-03 00:51:22 +0900443 txb_info->tx_size, tx_class);
444 const int qc_eob_cost = get_coeff_cost(qc, scan_idx, 1, txb_info, txb_costs,
445 coeff_ctx_temp, tx_class);
Angie Chiangcbbf4f02018-03-02 18:34:38 -0800446 int64_t rd_eob = RDCOST(txb_info->rdmult, qc_eob_cost, stats->dist);
447 if (stats->low_qc != 0) {
Katsuhisa Yuasa55c95f82018-04-03 00:51:22 +0900448 const int low_qc_eob_cost =
449 get_coeff_cost(stats->low_qc, scan_idx, 1, txb_info, txb_costs,
450 coeff_ctx_temp, tx_class);
Angie Chiangcbbf4f02018-03-02 18:34:38 -0800451 int64_t rd_eob_low =
452 RDCOST(txb_info->rdmult, low_qc_eob_cost, stats->dist_low);
453 rd_eob = (rd_eob > rd_eob_low) ? rd_eob_low : rd_eob;
Cheng Chen37d88732018-01-09 14:02:41 -0800454 }
Angie Chiangcbbf4f02018-03-02 18:34:38 -0800455
456 stats->nz_rd = AOMMIN(stats->rd_low, stats->rd) - rd_eob;
Dake Hea47cd6c2017-10-13 18:09:58 -0700457}
458
Linfeng Zhang1015a342017-10-24 16:20:41 -0700459static INLINE void update_qcoeff(const int coeff_idx, const tran_low_t qc,
460 const TxbInfo *const txb_info) {
Dake Hea47cd6c2017-10-13 18:09:58 -0700461 txb_info->qcoeff[coeff_idx] = qc;
Linfeng Zhangd5647372017-12-05 17:06:07 -0800462 txb_info->levels[get_padded_idx(coeff_idx, txb_info->bwl)] =
Jingning Han5cb408e2017-11-17 14:43:39 -0800463 (uint8_t)clamp(abs(qc), 0, INT8_MAX);
Linfeng Zhang1015a342017-10-24 16:20:41 -0700464}
465
466static INLINE void update_coeff(const int coeff_idx, const tran_low_t qc,
467 const TxbInfo *const txb_info) {
468 update_qcoeff(coeff_idx, qc, txb_info);
Dake Hea47cd6c2017-10-13 18:09:58 -0700469 const int dqv = txb_info->dequant[coeff_idx != 0];
Frederic Barbiere111cba2018-02-20 16:11:28 +0100470 txb_info->dqcoeff[coeff_idx] = qcoeff_to_dqcoeff(
471 qc, coeff_idx, dqv, txb_info->shift, txb_info->iqmatrix);
Dake Hea47cd6c2017-10-13 18:09:58 -0700472}
473
Peng Bin27d7ca92018-03-22 22:25:56 +0800474void av1_txb_init_levels_c(const tran_low_t *const coeff, const int width,
475 const int height, uint8_t *const levels) {
Linfeng Zhang679d81e2017-10-31 15:27:42 -0700476 const int stride = width + TX_PAD_HOR;
Linfeng Zhang1122d7d2017-10-31 15:30:28 -0700477 uint8_t *ls = levels;
Linfeng Zhang679d81e2017-10-31 15:27:42 -0700478
Linfeng Zhang1122d7d2017-10-31 15:30:28 -0700479 memset(levels + stride * height, 0,
480 sizeof(*levels) * (TX_PAD_BOTTOM * stride + TX_PAD_END));
Linfeng Zhang679d81e2017-10-31 15:27:42 -0700481
Linfeng Zhang1122d7d2017-10-31 15:30:28 -0700482 for (int i = 0; i < height; i++) {
483 for (int j = 0; j < width; j++) {
Jingning Han5cb408e2017-11-17 14:43:39 -0800484 *ls++ = (uint8_t)clamp(abs(coeff[i * width + j]), 0, INT8_MAX);
Linfeng Zhang1122d7d2017-10-31 15:30:28 -0700485 }
486 for (int j = 0; j < TX_PAD_HOR; j++) {
487 *ls++ = 0;
488 }
Linfeng Zhang1015a342017-10-24 16:20:41 -0700489 }
490}
491
Linfeng Zhang0ba23e82017-12-20 16:27:28 -0800492void av1_get_nz_map_contexts_c(const uint8_t *const levels,
493 const int16_t *const scan, const uint16_t eob,
Katsuhisa Yuasa55c95f82018-04-03 00:51:22 +0900494 const TX_SIZE tx_size, const TX_CLASS tx_class,
Linfeng Zhang0ba23e82017-12-20 16:27:28 -0800495 int8_t *const coeff_contexts) {
Linfeng Zhangd67c13f2017-12-11 11:49:12 -0800496 const int bwl = get_txb_bwl(tx_size);
Linfeng Zhangd67c13f2017-12-11 11:49:12 -0800497 const int height = get_txb_high(tx_size);
Linfeng Zhangd67c13f2017-12-11 11:49:12 -0800498 for (int i = 0; i < eob; ++i) {
499 const int pos = scan[i];
Sebastien Alaiwan78f7bb92018-01-11 11:02:43 +0100500 coeff_contexts[pos] = get_nz_map_ctx(levels, pos, bwl, height, i,
Katsuhisa Yuasa55c95f82018-04-03 00:51:22 +0900501 i == eob - 1, tx_size, tx_class);
Linfeng Zhangd67c13f2017-12-11 11:49:12 -0800502 }
503}
504
Hui Su179fbfa2019-10-07 15:13:06 -0700505void av1_write_coeffs_txb(const AV1_COMMON *const cm, MACROBLOCK *const x,
Luc Trudeau2eb9b842017-12-13 11:19:16 -0500506 aom_writer *w, int blk_row, int blk_col, int plane,
Hui Su179fbfa2019-10-07 15:13:06 -0700507 int block, TX_SIZE tx_size) {
508 MACROBLOCKD *xd = &x->e_mbd;
509 const CB_COEFF_BUFFER *cb_coef_buff = x->cb_coef_buff;
510 const int txb_offset =
511 x->mbmi_ext_frame->cb_offset / (TX_SIZE_W_MIN * TX_SIZE_H_MIN);
512 const uint16_t *eob_txb = cb_coef_buff->eobs[plane] + txb_offset;
513 const uint16_t eob = eob_txb[block];
514 const uint8_t *entropy_ctx = cb_coef_buff->entropy_ctx[plane] + txb_offset;
515 const int txb_skip_ctx = entropy_ctx[block] & TXB_SKIP_CTX_MASK;
Debargha Mukherjeeb3eda2f2017-11-28 16:00:20 -0800516 const TX_SIZE txs_ctx = get_txsize_entropy_ctx(tx_size);
Peng Bin66205b72018-09-04 15:11:08 +0800517 FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
Hui Su179fbfa2019-10-07 15:13:06 -0700518 aom_write_symbol(w, eob == 0, ec_ctx->txb_skip_cdf[txs_ctx][txb_skip_ctx], 2);
Peng Bin66205b72018-09-04 15:11:08 +0800519 if (eob == 0) return;
Hui Su179fbfa2019-10-07 15:13:06 -0700520
Peng Bin66205b72018-09-04 15:11:08 +0800521 const PLANE_TYPE plane_type = get_plane_type(plane);
Urvang Joshib6409e92020-03-23 11:23:27 -0700522 const TX_TYPE tx_type =
523 av1_get_tx_type(xd, plane_type, blk_row, blk_col, tx_size,
524 cm->features.reduced_tx_set_used);
Hui Su179fbfa2019-10-07 15:13:06 -0700525 // Only y plane's tx_type is transmitted
526 if (plane == 0) {
527 av1_write_tx_type(cm, xd, tx_type, tx_size, w);
528 }
Angie Chiang80b82262017-02-24 11:39:47 -0800529
Yaowu Xu49abec82018-03-13 16:09:01 -0700530 int eob_extra;
Linfeng Zhang0c72b2f2017-12-04 10:59:28 -0800531 const int eob_pt = get_eob_pos_token(eob, &eob_extra);
Dake He0db7d0e2017-12-21 15:23:20 -0800532 const int eob_multi_size = txsize_log2_minus4[tx_size];
Hui Su179fbfa2019-10-07 15:13:06 -0700533 const TX_CLASS tx_class = tx_type_to_class[tx_type];
Katsuhisa Yuasa55c95f82018-04-03 00:51:22 +0900534 const int eob_multi_ctx = (tx_class == TX_CLASS_2D) ? 0 : 1;
Dake He0db7d0e2017-12-21 15:23:20 -0800535 switch (eob_multi_size) {
536 case 0:
537 aom_write_symbol(w, eob_pt - 1,
538 ec_ctx->eob_flag_cdf16[plane_type][eob_multi_ctx], 5);
539 break;
540 case 1:
541 aom_write_symbol(w, eob_pt - 1,
542 ec_ctx->eob_flag_cdf32[plane_type][eob_multi_ctx], 6);
543 break;
544 case 2:
545 aom_write_symbol(w, eob_pt - 1,
546 ec_ctx->eob_flag_cdf64[plane_type][eob_multi_ctx], 7);
547 break;
548 case 3:
549 aom_write_symbol(w, eob_pt - 1,
550 ec_ctx->eob_flag_cdf128[plane_type][eob_multi_ctx], 8);
551 break;
552 case 4:
553 aom_write_symbol(w, eob_pt - 1,
554 ec_ctx->eob_flag_cdf256[plane_type][eob_multi_ctx], 9);
555 break;
556 case 5:
557 aom_write_symbol(w, eob_pt - 1,
558 ec_ctx->eob_flag_cdf512[plane_type][eob_multi_ctx], 10);
559 break;
560 default:
561 aom_write_symbol(w, eob_pt - 1,
562 ec_ctx->eob_flag_cdf1024[plane_type][eob_multi_ctx], 11);
563 break;
564 }
565
Yaowu Xu3a19b8a2019-05-01 08:40:42 -0700566 const int eob_offset_bits = av1_eob_offset_bits[eob_pt];
wenyao.liu0be59012018-12-24 10:27:31 +0800567 if (eob_offset_bits > 0) {
Jingning Hanff4a9f82018-06-08 10:48:45 -0700568 const int eob_ctx = eob_pt - 3;
wenyao.liu0be59012018-12-24 10:27:31 +0800569 int eob_shift = eob_offset_bits - 1;
Angie Chiang7ab884e2017-10-18 15:57:12 -0700570 int bit = (eob_extra & (1 << eob_shift)) ? 1 : 0;
Jingning Hanff4a9f82018-06-08 10:48:45 -0700571 aom_write_symbol(w, bit,
572 ec_ctx->eob_extra_cdf[txs_ctx][plane_type][eob_ctx], 2);
wenyao.liu0be59012018-12-24 10:27:31 +0800573 for (int i = 1; i < eob_offset_bits; i++) {
574 eob_shift = eob_offset_bits - 1 - i;
Angie Chiang7ab884e2017-10-18 15:57:12 -0700575 bit = (eob_extra & (1 << eob_shift)) ? 1 : 0;
Dake Hea47cd6c2017-10-13 18:09:58 -0700576 aom_write_bit(w, bit);
Dake Hea47cd6c2017-10-13 18:09:58 -0700577 }
578 }
Dake He03a32922017-10-31 08:06:45 -0700579
Hui Su179fbfa2019-10-07 15:13:06 -0700580 const int width = get_txb_wide(tx_size);
581 const int height = get_txb_high(tx_size);
582 uint8_t levels_buf[TX_PAD_2D];
583 uint8_t *const levels = set_levels(levels_buf, width);
584 const tran_low_t *tcoeff_txb =
585 cb_coef_buff->tcoeff[plane] + x->mbmi_ext_frame->cb_offset;
Yaowu Xu18a49962019-10-16 12:47:04 -0700586 const tran_low_t *tcoeff = tcoeff_txb + BLOCK_OFFSET(block);
Hui Su179fbfa2019-10-07 15:13:06 -0700587 av1_txb_init_levels(tcoeff, width, height, levels);
588 const SCAN_ORDER *const scan_order = get_scan(tx_size, tx_type);
589 const int16_t *const scan = scan_order->scan;
590 DECLARE_ALIGNED(16, int8_t, coeff_contexts[MAX_TX_SQUARE]);
Katsuhisa Yuasa55c95f82018-04-03 00:51:22 +0900591 av1_get_nz_map_contexts(levels, scan, eob, tx_size, tx_class, coeff_contexts);
Linfeng Zhangd67c13f2017-12-11 11:49:12 -0800592
Hui Su179fbfa2019-10-07 15:13:06 -0700593 const int bwl = get_txb_bwl(tx_size);
594 for (int c = eob - 1; c >= 0; --c) {
Linfeng Zhangdb41d1e2017-12-05 11:06:20 -0800595 const int pos = scan[c];
Linfeng Zhangd67c13f2017-12-11 11:49:12 -0800596 const int coeff_ctx = coeff_contexts[pos];
597 const tran_low_t v = tcoeff[pos];
Angie Chiang41220ab2018-02-14 17:48:23 -0800598 const tran_low_t level = abs(v);
Dake He03a32922017-10-31 08:06:45 -0700599
Dake He3fe369c2017-11-16 17:56:44 -0800600 if (c == eob - 1) {
601 aom_write_symbol(
Angie Chiang41220ab2018-02-14 17:48:23 -0800602 w, AOMMIN(level, 3) - 1,
Dake He4d447692017-12-15 09:10:06 -0800603 ec_ctx->coeff_base_eob_cdf[txs_ctx][plane_type][coeff_ctx], 3);
Dake He3fe369c2017-11-16 17:56:44 -0800604 } else {
Angie Chiang41220ab2018-02-14 17:48:23 -0800605 aom_write_symbol(w, AOMMIN(level, 3),
Dake He3fe369c2017-11-16 17:56:44 -0800606 ec_ctx->coeff_base_cdf[txs_ctx][plane_type][coeff_ctx],
607 4);
608 }
Angie Chiang41220ab2018-02-14 17:48:23 -0800609 if (level > NUM_BASE_LEVELS) {
Sebastien Alaiwan78f7bb92018-01-11 11:02:43 +0100610 // level is above 1.
Linfeng Zhangdb41d1e2017-12-05 11:06:20 -0800611 const int base_range = level - 1 - NUM_BASE_LEVELS;
Katsuhisa Yuasa55c95f82018-04-03 00:51:22 +0900612 const int br_ctx = get_br_ctx(levels, pos, bwl, tx_class);
wenyao.liu0be59012018-12-24 10:27:31 +0800613 aom_cdf_prob *cdf =
614 ec_ctx->coeff_br_cdf[AOMMIN(txs_ctx, TX_32X32)][plane_type][br_ctx];
Angie Chiang41220ab2018-02-14 17:48:23 -0800615 for (int idx = 0; idx < COEFF_BASE_RANGE; idx += BR_CDF_SIZE - 1) {
Linfeng Zhangdb41d1e2017-12-05 11:06:20 -0800616 const int k = AOMMIN(base_range - idx, BR_CDF_SIZE - 1);
wenyao.liu0be59012018-12-24 10:27:31 +0800617 aom_write_symbol(w, k, cdf, BR_CDF_SIZE);
Ola Hugossone72a2092017-11-12 09:11:53 +0100618 if (k < BR_CDF_SIZE - 1) break;
619 }
Angie Chiang6d5419c2018-02-20 15:12:15 -0800620 }
621 }
622
623 // Loop to code all signs in the transform block,
624 // starting with the sign of DC (if applicable)
Hui Su179fbfa2019-10-07 15:13:06 -0700625 for (int c = 0; c < eob; ++c) {
Angie Chiang6d5419c2018-02-20 15:12:15 -0800626 const tran_low_t v = tcoeff[scan[c]];
627 const tran_low_t level = abs(v);
628 const int sign = (v < 0) ? 1 : 0;
629 if (level) {
630 if (c == 0) {
Hui Su179fbfa2019-10-07 15:13:06 -0700631 const int dc_sign_ctx =
632 (entropy_ctx[block] >> DC_SIGN_CTX_SHIFT) & DC_SIGN_CTX_MASK;
633 aom_write_symbol(w, sign, ec_ctx->dc_sign_cdf[plane_type][dc_sign_ctx],
634 2);
Angie Chiang6d5419c2018-02-20 15:12:15 -0800635 } else {
636 aom_write_bit(w, sign);
637 }
Yushin Cho73711d52018-05-04 13:59:46 -0700638 if (level > COEFF_BASE_RANGE + NUM_BASE_LEVELS)
639 write_golomb(w, level - COEFF_BASE_RANGE - 1 - NUM_BASE_LEVELS);
Angie Chiang41220ab2018-02-14 17:48:23 -0800640 }
641 }
Angie Chiang80b82262017-02-24 11:39:47 -0800642}
Angie Chiang47c72182017-02-27 14:30:38 -0800643
Angie Chiang140b3332017-12-12 17:29:25 -0800644typedef struct encode_txb_args {
645 const AV1_COMMON *cm;
646 MACROBLOCK *x;
647 aom_writer *w;
648} ENCODE_TXB_ARGS;
649
Hui Sud62a63a2020-02-27 16:59:54 -0800650void av1_write_coeffs_mb(const AV1_COMMON *const cm, MACROBLOCK *x,
651 aom_writer *w, BLOCK_SIZE bsize) {
Angie Chiang140b3332017-12-12 17:29:25 -0800652 MACROBLOCKD *xd = &x->e_mbd;
Jingning Hanad54a982018-01-12 14:40:29 -0800653 const int num_planes = av1_num_planes(cm);
654 int block[MAX_MB_PLANE] = { 0 };
Jingning Han4b48cd12018-01-11 15:56:42 -0800655 int row, col;
Cheng Chen8ab1f442018-04-27 18:01:52 -0700656 assert(bsize == get_plane_block_size(bsize, xd->plane[0].subsampling_x,
657 xd->plane[0].subsampling_y));
Yushin Choca63a8b2018-04-20 16:46:36 -0700658 const int max_blocks_wide = max_block_wide(xd, bsize, 0);
659 const int max_blocks_high = max_block_high(xd, bsize, 0);
660 const BLOCK_SIZE max_unit_bsize = BLOCK_64X64;
Hui Suadda5872019-12-09 10:08:49 -0800661 int mu_blocks_wide = mi_size_wide[max_unit_bsize];
662 int mu_blocks_high = mi_size_high[max_unit_bsize];
Jingning Han4b48cd12018-01-11 15:56:42 -0800663 mu_blocks_wide = AOMMIN(max_blocks_wide, mu_blocks_wide);
664 mu_blocks_high = AOMMIN(max_blocks_high, mu_blocks_high);
665
666 for (row = 0; row < max_blocks_high; row += mu_blocks_high) {
Jingning Han4b48cd12018-01-11 15:56:42 -0800667 for (col = 0; col < max_blocks_wide; col += mu_blocks_wide) {
Jingning Hanad54a982018-01-12 14:40:29 -0800668 for (int plane = 0; plane < num_planes; ++plane) {
Hui Su474e1e12020-02-27 15:46:36 -0800669 if (plane && !xd->is_chroma_ref) break;
Jingning Hanad54a982018-01-12 14:40:29 -0800670 const TX_SIZE tx_size = av1_get_tx_size(plane, xd);
671 const int stepr = tx_size_high_unit[tx_size];
672 const int stepc = tx_size_wide_unit[tx_size];
673 const int step = stepr * stepc;
Hui Su474e1e12020-02-27 15:46:36 -0800674 const struct macroblockd_plane *const pd = &xd->plane[plane];
Jingning Hanad54a982018-01-12 14:40:29 -0800675 const int unit_height = ROUND_POWER_OF_TWO(
676 AOMMIN(mu_blocks_high + row, max_blocks_high), pd->subsampling_y);
677 const int unit_width = ROUND_POWER_OF_TWO(
678 AOMMIN(mu_blocks_wide + col, max_blocks_wide), pd->subsampling_x);
679 for (int blk_row = row >> pd->subsampling_y; blk_row < unit_height;
680 blk_row += stepr) {
681 for (int blk_col = col >> pd->subsampling_x; blk_col < unit_width;
682 blk_col += stepc) {
Hui Su179fbfa2019-10-07 15:13:06 -0700683 av1_write_coeffs_txb(cm, x, w, blk_row, blk_col, plane,
684 block[plane], tx_size);
Jingning Hanad54a982018-01-12 14:40:29 -0800685 block[plane] += step;
686 }
Jingning Han4b48cd12018-01-11 15:56:42 -0800687 }
688 }
689 }
690 }
Angie Chiangc8af6112017-03-16 16:11:22 -0700691}
692
Urvang Joshi553096a2018-05-10 15:27:14 -0700693// TODO(angiebird): use this function whenever it's possible
Yaowu Xu33fd0a02019-10-15 18:23:29 -0700694static int get_tx_type_cost(const MACROBLOCK *x, const MACROBLOCKD *xd,
695 int plane, TX_SIZE tx_size, TX_TYPE tx_type,
696 int reduced_tx_set_used) {
Urvang Joshi553096a2018-05-10 15:27:14 -0700697 if (plane > 0) return 0;
698
699 const TX_SIZE square_tx_size = txsize_sqr_map[tx_size];
700
701 const MB_MODE_INFO *mbmi = xd->mi[0];
702 const int is_inter = is_inter_block(mbmi);
Yaowu Xu33fd0a02019-10-15 18:23:29 -0700703 if (get_ext_tx_types(tx_size, is_inter, reduced_tx_set_used) > 1 &&
Urvang Joshi553096a2018-05-10 15:27:14 -0700704 !xd->lossless[xd->mi[0]->segment_id]) {
705 const int ext_tx_set =
Yaowu Xu33fd0a02019-10-15 18:23:29 -0700706 get_ext_tx_set(tx_size, is_inter, reduced_tx_set_used);
Urvang Joshi553096a2018-05-10 15:27:14 -0700707 if (is_inter) {
708 if (ext_tx_set > 0)
chiyotsai9a06d182020-05-01 17:12:12 -0700709 return x->mode_costs
710 .inter_tx_type_costs[ext_tx_set][square_tx_size][tx_type];
Urvang Joshi553096a2018-05-10 15:27:14 -0700711 } else {
712 if (ext_tx_set > 0) {
713 PREDICTION_MODE intra_dir;
714 if (mbmi->filter_intra_mode_info.use_filter_intra)
715 intra_dir = fimode_to_intradir[mbmi->filter_intra_mode_info
716 .filter_intra_mode];
717 else
718 intra_dir = mbmi->mode;
chiyotsai9a06d182020-05-01 17:12:12 -0700719 return x->mode_costs.intra_tx_type_costs[ext_tx_set][square_tx_size]
720 [intra_dir][tx_type];
Urvang Joshi553096a2018-05-10 15:27:14 -0700721 }
722 }
723 }
724 return 0;
725}
726
bohanli0bff42b2020-01-09 15:21:11 -0800727static INLINE void update_coeff_eob_fast(int *eob, int shift,
728 const int16_t *dequant_ptr,
729 const int16_t *scan,
730 const tran_low_t *coeff_ptr,
731 tran_low_t *qcoeff_ptr,
732 tran_low_t *dqcoeff_ptr) {
733 // TODO(sarahparker) make this work for aomqm
734 int eob_out = *eob;
735 int zbin[2] = { dequant_ptr[0] + ROUND_POWER_OF_TWO(dequant_ptr[0] * 70, 7),
736 dequant_ptr[1] + ROUND_POWER_OF_TWO(dequant_ptr[1] * 70, 7) };
737
738 for (int i = *eob - 1; i >= 0; i--) {
739 const int rc = scan[i];
740 const int qcoeff = qcoeff_ptr[rc];
741 const int coeff = coeff_ptr[rc];
Yaowu Xu9db5b8d2020-04-10 08:58:15 -0700742 const int coeff_sign = AOMSIGN(coeff);
bohanli0bff42b2020-01-09 15:21:11 -0800743 int64_t abs_coeff = (coeff ^ coeff_sign) - coeff_sign;
744
745 if (((abs_coeff << (1 + shift)) < zbin[rc != 0]) || (qcoeff == 0)) {
746 eob_out--;
747 qcoeff_ptr[rc] = 0;
748 dqcoeff_ptr[rc] = 0;
749 } else {
750 break;
751 }
752 }
753
754 *eob = eob_out;
755}
756
Katsuhisa Yuasa51467032018-04-08 16:16:10 +0900757static AOM_FORCE_INLINE int warehouse_efficients_txb(
bohanli3613e5d2019-10-25 13:10:11 -0700758 const MACROBLOCK *x, const int plane, const int block,
759 const TX_SIZE tx_size, const TXB_CTX *const txb_ctx,
Katsuhisa Yuasa51467032018-04-08 16:16:10 +0900760 const struct macroblock_plane *p, const int eob,
761 const PLANE_TYPE plane_type, const LV_MAP_COEFF_COST *const coeff_costs,
bohanli3613e5d2019-10-25 13:10:11 -0700762 const MACROBLOCKD *const xd, const TX_TYPE tx_type, const TX_CLASS tx_class,
763 int reduced_tx_set_used) {
Yaowu Xu18a49962019-10-16 12:47:04 -0700764 const tran_low_t *const qcoeff = p->qcoeff + BLOCK_OFFSET(block);
Linfeng Zhangc02b4112017-12-21 13:11:36 -0800765 const int txb_skip_ctx = txb_ctx->txb_skip_ctx;
Angie Chianga9ba58e2017-12-01 19:22:43 -0800766 const int bwl = get_txb_bwl(tx_size);
767 const int width = get_txb_wide(tx_size);
768 const int height = get_txb_high(tx_size);
Yaowu Xuf1e12932018-02-26 15:50:09 -0800769 const SCAN_ORDER *const scan_order = get_scan(tx_size, tx_type);
Linfeng Zhangdb41d1e2017-12-05 11:06:20 -0800770 const int16_t *const scan = scan_order->scan;
Linfeng Zhang679d81e2017-10-31 15:27:42 -0700771 uint8_t levels_buf[TX_PAD_2D];
772 uint8_t *const levels = set_levels(levels_buf, width);
Linfeng Zhangd67c13f2017-12-11 11:49:12 -0800773 DECLARE_ALIGNED(16, int8_t, coeff_contexts[MAX_TX_SQUARE]);
Dake He0db7d0e2017-12-21 15:23:20 -0800774 const int eob_multi_size = txsize_log2_minus4[tx_size];
775 const LV_MAP_EOB_COST *const eob_costs =
chiyotsai9a06d182020-05-01 17:12:12 -0700776 &x->coeff_costs.eob_costs[eob_multi_size][plane_type];
Hui Su18205542018-03-30 11:37:41 -0700777 int cost = coeff_costs->txb_skip_cost[txb_skip_ctx][0];
Angie Chiang47c72182017-02-27 14:30:38 -0800778
Linfeng Zhang1122d7d2017-10-31 15:30:28 -0700779 av1_txb_init_levels(qcoeff, width, height, levels);
Linfeng Zhang1015a342017-10-24 16:20:41 -0700780
bohanli3613e5d2019-10-25 13:10:11 -0700781 cost += get_tx_type_cost(x, xd, plane, tx_size, tx_type, reduced_tx_set_used);
Angie Chiang05917872017-04-15 12:28:56 -0700782
Katsuhisa Yuasa55c95f82018-04-03 00:51:22 +0900783 cost += get_eob_cost(eob, eob_costs, coeff_costs, tx_class);
Linfeng Zhangd67c13f2017-12-11 11:49:12 -0800784
Katsuhisa Yuasa55c95f82018-04-03 00:51:22 +0900785 av1_get_nz_map_contexts(levels, scan, eob, tx_size, tx_class, coeff_contexts);
Linfeng Zhangd67c13f2017-12-11 11:49:12 -0800786
Wenyao Liuf7e53752019-01-22 17:34:44 +0800787 const int(*lps_cost)[COEFF_BASE_RANGE + 1 + COEFF_BASE_RANGE + 1] =
788 coeff_costs->lps_cost;
Katsuhisa Yuasa51467032018-04-08 16:16:10 +0900789 int c = eob - 1;
790 {
Linfeng Zhangdb41d1e2017-12-05 11:06:20 -0800791 const int pos = scan[c];
792 const tran_low_t v = qcoeff[pos];
Yaowu Xu9db5b8d2020-04-10 08:58:15 -0700793 const int sign = AOMSIGN(v);
Katsuhisa Yuasa51467032018-04-08 16:16:10 +0900794 const int level = (v ^ sign) - sign;
Linfeng Zhangd67c13f2017-12-11 11:49:12 -0800795 const int coeff_ctx = coeff_contexts[pos];
Katsuhisa Yuasa51467032018-04-08 16:16:10 +0900796 cost += coeff_costs->base_eob_cost[coeff_ctx][AOMMIN(level, 3) - 1];
Dake Hea47cd6c2017-10-13 18:09:58 -0700797
Katsuhisa Yuasa51467032018-04-08 16:16:10 +0900798 if (v) {
Dake Hea47cd6c2017-10-13 18:09:58 -0700799 // sign bit cost
Dake Hea47cd6c2017-10-13 18:09:58 -0700800 if (level > NUM_BASE_LEVELS) {
Jim Bankoskife171222019-01-14 08:41:57 -0800801 const int ctx = get_br_ctx_eob(pos, bwl, tx_class);
802 cost += get_br_cost(level, lps_cost[ctx]);
Katsuhisa Yuasa51467032018-04-08 16:16:10 +0900803 }
804 if (c) {
805 cost += av1_cost_literal(1);
806 } else {
807 const int sign01 = (sign ^ sign) - sign;
808 const int dc_sign_ctx = txb_ctx->dc_sign_ctx;
809 cost += coeff_costs->dc_sign_cost[dc_sign_ctx][sign01];
810 return cost;
811 }
812 }
813 }
Wenyao Liuf7e53752019-01-22 17:34:44 +0800814 const int(*base_cost)[8] = coeff_costs->base_cost;
Katsuhisa Yuasa51467032018-04-08 16:16:10 +0900815 for (c = eob - 2; c >= 1; --c) {
816 const int pos = scan[c];
817 const int coeff_ctx = coeff_contexts[pos];
818 const tran_low_t v = qcoeff[pos];
819 const int level = abs(v);
bohanli3613e5d2019-10-25 13:10:11 -0700820 cost += base_cost[coeff_ctx][AOMMIN(level, 3)];
Katsuhisa Yuasa51467032018-04-08 16:16:10 +0900821 if (v) {
822 // sign bit cost
823 cost += av1_cost_literal(1);
824 if (level > NUM_BASE_LEVELS) {
825 const int ctx = get_br_ctx(levels, pos, bwl, tx_class);
Jim Bankoskife171222019-01-14 08:41:57 -0800826 cost += get_br_cost(level, lps_cost[ctx]);
Katsuhisa Yuasa51467032018-04-08 16:16:10 +0900827 }
828 }
Katsuhisa Yuasa51467032018-04-08 16:16:10 +0900829 }
bohanli3613e5d2019-10-25 13:10:11 -0700830 // c == 0 after previous loop
831 {
Katsuhisa Yuasa51467032018-04-08 16:16:10 +0900832 const int pos = scan[c];
833 const tran_low_t v = qcoeff[pos];
834 const int coeff_ctx = coeff_contexts[pos];
Yaowu Xu9db5b8d2020-04-10 08:58:15 -0700835 const int sign = AOMSIGN(v);
Katsuhisa Yuasa51467032018-04-08 16:16:10 +0900836 const int level = (v ^ sign) - sign;
837 cost += base_cost[coeff_ctx][AOMMIN(level, 3)];
Dake Hea47cd6c2017-10-13 18:09:58 -0700838
Katsuhisa Yuasa51467032018-04-08 16:16:10 +0900839 if (v) {
840 // sign bit cost
841 const int sign01 = (sign ^ sign) - sign;
842 const int dc_sign_ctx = txb_ctx->dc_sign_ctx;
843 cost += coeff_costs->dc_sign_cost[dc_sign_ctx][sign01];
844 if (level > NUM_BASE_LEVELS) {
845 const int ctx = get_br_ctx(levels, pos, bwl, tx_class);
Jim Bankoskife171222019-01-14 08:41:57 -0800846 cost += get_br_cost(level, lps_cost[ctx]);
Dake Hea47cd6c2017-10-13 18:09:58 -0700847 }
848 }
849 }
Angie Chiang47c72182017-02-27 14:30:38 -0800850 return cost;
851}
Angie Chiang0397eda2017-03-15 16:57:14 -0700852
bohanli0bff42b2020-01-09 15:21:11 -0800853static AOM_FORCE_INLINE int warehouse_efficients_txb_laplacian(
854 const MACROBLOCK *x, const int plane, const int block,
855 const TX_SIZE tx_size, const TXB_CTX *const txb_ctx, const int eob,
856 const PLANE_TYPE plane_type, const LV_MAP_COEFF_COST *const coeff_costs,
857 const MACROBLOCKD *const xd, const TX_TYPE tx_type, const TX_CLASS tx_class,
858 int reduced_tx_set_used) {
859 const int txb_skip_ctx = txb_ctx->txb_skip_ctx;
860
861 const int eob_multi_size = txsize_log2_minus4[tx_size];
862 const LV_MAP_EOB_COST *const eob_costs =
chiyotsai9a06d182020-05-01 17:12:12 -0700863 &x->coeff_costs.eob_costs[eob_multi_size][plane_type];
bohanli0bff42b2020-01-09 15:21:11 -0800864 int cost = coeff_costs->txb_skip_cost[txb_skip_ctx][0];
865
866 cost += get_tx_type_cost(x, xd, plane, tx_size, tx_type, reduced_tx_set_used);
867
868 cost += get_eob_cost(eob, eob_costs, coeff_costs, tx_class);
869
870 cost += av1_cost_coeffs_txb_estimate(x, plane, block, tx_size, tx_type);
871 return cost;
872}
873
874// Look up table of individual cost of coefficient by its quantization level.
875// determined based on Laplacian distribution conditioned on estimated context
876static const int costLUT[15] = { -1143, 53, 545, 825, 1031,
877 1209, 1393, 1577, 1763, 1947,
878 2132, 2317, 2501, 2686, 2871 };
879static const int const_term = (1 << AV1_PROB_COST_SHIFT);
880static const int loge_par = ((14427 << AV1_PROB_COST_SHIFT) + 5000) / 10000;
881int av1_cost_coeffs_txb_estimate(const MACROBLOCK *x, const int plane,
882 const int block, const TX_SIZE tx_size,
883 const TX_TYPE tx_type) {
884 assert(plane == 0);
885
886 int cost = 0;
887 const struct macroblock_plane *p = &x->plane[plane];
888 const SCAN_ORDER *scan_order = get_scan(tx_size, tx_type);
889 const int16_t *scan = scan_order->scan;
890 tran_low_t *qcoeff = p->qcoeff + BLOCK_OFFSET(block);
891
892 int eob = p->eobs[block];
893
894 // coeffs
895 int c = eob - 1;
896 // eob
897 {
898 const int pos = scan[c];
899 const tran_low_t v = abs(qcoeff[pos]) - 1;
900 cost += (v << (AV1_PROB_COST_SHIFT + 2));
901 }
902 // other coeffs
903 for (c = eob - 2; c >= 0; c--) {
904 const int pos = scan[c];
905 const tran_low_t v = abs(qcoeff[pos]);
906 const int idx = AOMMIN(v, 14);
907
908 cost += costLUT[idx];
909 }
910
911 // const_term does not contain DC, and log(e) does not contain eob, so both
912 // (eob-1)
913 cost += (const_term + loge_par) * (eob - 1);
914
915 return cost;
916}
917
bohanli3613e5d2019-10-25 13:10:11 -0700918int av1_cost_coeffs_txb(const MACROBLOCK *x, const int plane, const int block,
919 const TX_SIZE tx_size, const TX_TYPE tx_type,
920 const TXB_CTX *const txb_ctx, int reduced_tx_set_used) {
Katsuhisa Yuasa51467032018-04-08 16:16:10 +0900921 const struct macroblock_plane *p = &x->plane[plane];
922 const int eob = p->eobs[block];
923 const TX_SIZE txs_ctx = get_txsize_entropy_ctx(tx_size);
924 const PLANE_TYPE plane_type = get_plane_type(plane);
925 const LV_MAP_COEFF_COST *const coeff_costs =
chiyotsai9a06d182020-05-01 17:12:12 -0700926 &x->coeff_costs.coeff_costs[txs_ctx][plane_type];
Katsuhisa Yuasa51467032018-04-08 16:16:10 +0900927 if (eob == 0) {
928 return coeff_costs->txb_skip_cost[txb_ctx->txb_skip_ctx][1];
929 }
930
931 const MACROBLOCKD *const xd = &x->e_mbd;
Katsuhisa Yuasa51467032018-04-08 16:16:10 +0900932 const TX_CLASS tx_class = tx_type_to_class[tx_type];
933
bohanli3613e5d2019-10-25 13:10:11 -0700934 return warehouse_efficients_txb(x, plane, block, tx_size, txb_ctx, p, eob,
Yaowu Xu109ec9b2019-10-15 17:19:24 -0700935 plane_type, coeff_costs, xd, tx_type,
bohanli3613e5d2019-10-25 13:10:11 -0700936 tx_class, reduced_tx_set_used);
Katsuhisa Yuasa51467032018-04-08 16:16:10 +0900937}
938
bohanli736644d2020-01-14 09:30:37 -0800939int av1_cost_coeffs_txb_laplacian(const MACROBLOCK *x, const int plane,
940 const int block, const TX_SIZE tx_size,
941 const TX_TYPE tx_type,
942 const TXB_CTX *const txb_ctx,
943 const int reduced_tx_set_used,
944 const int adjust_eob) {
bohanli0bff42b2020-01-09 15:21:11 -0800945 const struct macroblock_plane *p = &x->plane[plane];
946 int eob = p->eobs[block];
947
948 if (adjust_eob) {
949 const SCAN_ORDER *scan_order = get_scan(tx_size, tx_type);
950 const int16_t *scan = scan_order->scan;
951 tran_low_t *tcoeff = p->coeff + BLOCK_OFFSET(block);
952 tran_low_t *qcoeff = p->qcoeff + BLOCK_OFFSET(block);
Urvang Joshi9543ad72020-04-17 16:59:46 -0700953 tran_low_t *dqcoeff = p->dqcoeff + BLOCK_OFFSET(block);
bohanli0bff42b2020-01-09 15:21:11 -0800954 update_coeff_eob_fast(&eob, av1_get_tx_scale(tx_size), p->dequant_QTX, scan,
955 tcoeff, qcoeff, dqcoeff);
956 p->eobs[block] = eob;
957 }
958
959 const TX_SIZE txs_ctx = get_txsize_entropy_ctx(tx_size);
960 const PLANE_TYPE plane_type = get_plane_type(plane);
961 const LV_MAP_COEFF_COST *const coeff_costs =
chiyotsai9a06d182020-05-01 17:12:12 -0700962 &x->coeff_costs.coeff_costs[txs_ctx][plane_type];
bohanli0bff42b2020-01-09 15:21:11 -0800963 if (eob == 0) {
964 return coeff_costs->txb_skip_cost[txb_ctx->txb_skip_ctx][1];
965 }
966
967 const MACROBLOCKD *const xd = &x->e_mbd;
968 const TX_CLASS tx_class = tx_type_to_class[tx_type];
969
970 return warehouse_efficients_txb_laplacian(
971 x, plane, block, tx_size, txb_ctx, eob, plane_type, coeff_costs, xd,
972 tx_type, tx_class, reduced_tx_set_used);
973}
974
Dake Hea47cd6c2017-10-13 18:09:58 -0700975static int optimize_txb(TxbInfo *txb_info, const LV_MAP_COEFF_COST *txb_costs,
Angie Chiang45385b82018-03-02 15:25:58 -0800976 const LV_MAP_EOB_COST *txb_eob_costs, int *rate_cost) {
Dake Hea47cd6c2017-10-13 18:09:58 -0700977 int update = 0;
978 if (txb_info->eob == 0) return update;
Linfeng Zhangdb41d1e2017-12-05 11:06:20 -0800979 const int16_t *const scan = txb_info->scan_order->scan;
Dake Hea47cd6c2017-10-13 18:09:58 -0700980 // forward optimize the nz_map`
981 const int init_eob = txb_info->eob;
Katsuhisa Yuasa55c95f82018-04-03 00:51:22 +0900982 const TX_CLASS tx_class = tx_type_to_class[txb_info->tx_type];
Yaowu Xu49abec82018-03-13 16:09:01 -0700983 const int eob_cost =
Katsuhisa Yuasa55c95f82018-04-03 00:51:22 +0900984 get_eob_cost(init_eob, txb_eob_costs, txb_costs, tx_class);
Dake Hea47cd6c2017-10-13 18:09:58 -0700985
986 // backward optimize the level-k map
Cheng Chen82775f62018-01-18 12:09:54 -0800987 int accu_rate = eob_cost;
Dake Hea47cd6c2017-10-13 18:09:58 -0700988 int64_t accu_dist = 0;
989 int64_t prev_eob_rd_cost = INT64_MAX;
990 int64_t cur_eob_rd_cost = 0;
991
Angie Chiangcbbf4f02018-03-02 18:34:38 -0800992 {
993 const int si = init_eob - 1;
994 const int coeff_idx = scan[si];
995 LevelDownStats stats;
Katsuhisa Yuasa55c95f82018-04-03 00:51:22 +0900996 get_dist_cost_stats(&stats, si, si == init_eob - 1, txb_costs, txb_info,
997 tx_class);
Angie Chiangcbbf4f02018-03-02 18:34:38 -0800998 if ((stats.rd_low < stats.rd) && (stats.low_qc != 0)) {
999 update = 1;
1000 update_coeff(coeff_idx, stats.low_qc, txb_info);
1001 accu_rate += stats.rate_low;
1002 accu_dist += stats.dist_low;
1003 } else {
1004 accu_rate += stats.rate;
1005 accu_dist += stats.dist;
1006 }
1007 }
1008
1009 int si = init_eob - 2;
1010 int8_t has_nz_tail = 0;
1011 // eob is not fixed
1012 for (; si >= 0 && has_nz_tail < 2; --si) {
1013 assert(si != init_eob - 1);
Dake Hea47cd6c2017-10-13 18:09:58 -07001014 const int coeff_idx = scan[si];
1015 tran_low_t qc = txb_info->qcoeff[coeff_idx];
1016
Dake Hea47cd6c2017-10-13 18:09:58 -07001017 if (qc == 0) {
Angie Chiang99b12422018-03-02 17:15:00 -08001018 const int coeff_ctx =
1019 get_lower_levels_ctx(txb_info->levels, coeff_idx, txb_info->bwl,
Katsuhisa Yuasa55c95f82018-04-03 00:51:22 +09001020 txb_info->tx_size, tx_class);
Angie Chiang99b12422018-03-02 17:15:00 -08001021 accu_rate += txb_costs->base_cost[coeff_ctx][0];
Dake Hea47cd6c2017-10-13 18:09:58 -07001022 } else {
Angie Chiang99b12422018-03-02 17:15:00 -08001023 LevelDownStats stats;
Katsuhisa Yuasa55c95f82018-04-03 00:51:22 +09001024 get_dist_cost_stats_with_eob(&stats, si, txb_costs, txb_info, tx_class);
Angie Chiangcbbf4f02018-03-02 18:34:38 -08001025 // check if it is better to make this the last significant coefficient
Yaowu Xu49abec82018-03-13 16:09:01 -07001026 int cur_eob_rate =
Katsuhisa Yuasa55c95f82018-04-03 00:51:22 +09001027 get_eob_cost(si + 1, txb_eob_costs, txb_costs, tx_class);
Angie Chiangcbbf4f02018-03-02 18:34:38 -08001028 cur_eob_rd_cost = RDCOST(txb_info->rdmult, cur_eob_rate, 0);
1029 prev_eob_rd_cost =
1030 RDCOST(txb_info->rdmult, accu_rate, accu_dist) + stats.nz_rd;
1031 if (cur_eob_rd_cost <= prev_eob_rd_cost) {
1032 update = 1;
1033 for (int j = si + 1; j < txb_info->eob; j++) {
1034 const int coeff_pos_j = scan[j];
1035 update_coeff(coeff_pos_j, 0, txb_info);
1036 }
1037 txb_info->eob = si + 1;
Angie Chiang99b12422018-03-02 17:15:00 -08001038
Angie Chiangcbbf4f02018-03-02 18:34:38 -08001039 // rerun cost calculation due to change of eob
1040 accu_rate = cur_eob_rate;
1041 accu_dist = 0;
Katsuhisa Yuasa55c95f82018-04-03 00:51:22 +09001042 get_dist_cost_stats(&stats, si, 1, txb_costs, txb_info, tx_class);
Angie Chiangcbbf4f02018-03-02 18:34:38 -08001043 if ((stats.rd_low < stats.rd) && (stats.low_qc != 0)) {
1044 update = 1;
1045 update_coeff(coeff_idx, stats.low_qc, txb_info);
1046 accu_rate += stats.rate_low;
1047 accu_dist += stats.dist_low;
Cheng Chenec32a742018-01-12 19:09:39 -08001048 } else {
Angie Chiangcbbf4f02018-03-02 18:34:38 -08001049 accu_rate += stats.rate;
1050 accu_dist += stats.dist;
1051 }
1052
1053 // reset non zero tail when new eob is found
1054 has_nz_tail = 0;
1055 } else {
1056 int bUpdCoeff = 0;
1057 if (stats.rd_low < stats.rd) {
1058 if ((si < txb_info->eob - 1)) {
1059 bUpdCoeff = 1;
Cheng Chenec32a742018-01-12 19:09:39 -08001060 update = 1;
Cheng Chenec32a742018-01-12 19:09:39 -08001061 }
Angie Chiangcbbf4f02018-03-02 18:34:38 -08001062 } else {
1063 ++has_nz_tail;
1064 }
1065
1066 if (bUpdCoeff) {
1067 update_coeff(coeff_idx, stats.low_qc, txb_info);
1068 accu_rate += stats.rate_low;
1069 accu_dist += stats.dist_low;
1070 } else {
1071 accu_rate += stats.rate;
1072 accu_dist += stats.dist;
Jingning Han8be58fa2017-12-18 09:46:13 -08001073 }
Dake Hea47cd6c2017-10-13 18:09:58 -07001074 }
Angie Chiangcbbf4f02018-03-02 18:34:38 -08001075 }
1076 } // for (si)
1077
1078 // eob is fixed
1079 for (; si >= 0; --si) {
1080 assert(si != init_eob - 1);
1081 const int coeff_idx = scan[si];
1082 tran_low_t qc = txb_info->qcoeff[coeff_idx];
1083
1084 if (qc == 0) {
1085 const int coeff_ctx =
1086 get_lower_levels_ctx(txb_info->levels, coeff_idx, txb_info->bwl,
Katsuhisa Yuasa55c95f82018-04-03 00:51:22 +09001087 txb_info->tx_size, tx_class);
Angie Chiangcbbf4f02018-03-02 18:34:38 -08001088 accu_rate += txb_costs->base_cost[coeff_ctx][0];
1089 } else {
1090 LevelDownStats stats;
Katsuhisa Yuasa55c95f82018-04-03 00:51:22 +09001091 get_dist_cost_stats(&stats, si, 0, txb_costs, txb_info, tx_class);
Dake Hea47cd6c2017-10-13 18:09:58 -07001092
1093 int bUpdCoeff = 0;
1094 if (stats.rd_low < stats.rd) {
Dake He4d447692017-12-15 09:10:06 -08001095 if ((si < txb_info->eob - 1)) {
Dake Hea47cd6c2017-10-13 18:09:58 -07001096 bUpdCoeff = 1;
1097 update = 1;
1098 }
1099 }
Dake Hea47cd6c2017-10-13 18:09:58 -07001100 if (bUpdCoeff) {
1101 update_coeff(coeff_idx, stats.low_qc, txb_info);
1102 accu_rate += stats.rate_low;
1103 accu_dist += stats.dist_low;
1104 } else {
1105 accu_rate += stats.rate;
1106 accu_dist += stats.dist;
1107 }
1108 }
1109 } // for (si)
Jingning Hana7a6f4e2017-12-13 14:44:08 -08001110
Dake Hea47cd6c2017-10-13 18:09:58 -07001111 int non_zero_blk_rate =
1112 txb_costs->txb_skip_cost[txb_info->txb_ctx->txb_skip_ctx][0];
1113 prev_eob_rd_cost =
1114 RDCOST(txb_info->rdmult, accu_rate + non_zero_blk_rate, accu_dist);
1115
1116 int zero_blk_rate =
1117 txb_costs->txb_skip_cost[txb_info->txb_ctx->txb_skip_ctx][1];
1118 int64_t zero_blk_rd_cost = RDCOST(txb_info->rdmult, zero_blk_rate, 0);
1119 if (zero_blk_rd_cost <= prev_eob_rd_cost) {
1120 update = 1;
1121 for (int j = 0; j < txb_info->eob; j++) {
1122 const int coeff_pos_j = scan[j];
1123 update_coeff(coeff_pos_j, 0, txb_info);
1124 }
1125 txb_info->eob = 0;
1126 }
1127
Cheng Chen82775f62018-01-18 12:09:54 -08001128 // record total rate cost
1129 *rate_cost = zero_blk_rd_cost <= prev_eob_rd_cost
1130 ? zero_blk_rate
1131 : accu_rate + non_zero_blk_rate;
Angie Chiang0ed53352018-03-08 12:53:43 -08001132
1133 if (txb_info->eob > 0) {
1134 *rate_cost += txb_info->tx_type_cost;
1135 }
1136
Dake Hea47cd6c2017-10-13 18:09:58 -07001137 return update;
1138}
1139
Yaowu Xu74e63352019-05-06 09:21:33 -07001140#if CONFIG_HTB_TRELLIS
Sarah Parker427e3b12018-10-12 12:28:44 -07001141static void hbt_init() {
Michelle Findlay-Olynykdea531d2017-12-13 14:10:56 -08001142 hbt_hash_table =
1143 aom_malloc(sizeof(OptTxbQcoeff) * HBT_TABLE_SIZE * HBT_ARRAY_LENGTH);
1144 memset(hbt_hash_table, 0,
1145 sizeof(OptTxbQcoeff) * HBT_TABLE_SIZE * HBT_ARRAY_LENGTH);
Peng Bin8a204cd2018-04-08 13:07:35 +08001146 av1_crc32c_calculator_init(&crc_calculator); // 31 bit: qc & ctx
Michelle Findlay-Olynykdea531d2017-12-13 14:10:56 -08001147
1148 hbt_needs_init = 0;
Michelle Findlay-Olynykfbab0622017-12-13 14:10:56 -08001149}
1150
Michelle Findlay-Olynykdea531d2017-12-13 14:10:56 -08001151void hbt_destroy() { aom_free(hbt_hash_table); }
1152
Sarah Parker427e3b12018-10-12 12:28:44 -07001153static int hbt_hash_miss(uint32_t hbt_ctx_hash, uint32_t hbt_qc_hash,
1154 TxbInfo *txb_info, const LV_MAP_COEFF_COST *txb_costs,
1155 const LV_MAP_EOB_COST *txb_eob_costs,
1156 const struct macroblock_plane *p, int block,
1157 int fast_mode, int *rate_cost) {
Angie Chiang45385b82018-03-02 15:25:58 -08001158 (void)fast_mode;
Michelle Findlay-Olynykfbab0622017-12-13 14:10:56 -08001159 const int16_t *scan = txb_info->scan_order->scan;
Michelle Findlay-Olynykdea531d2017-12-13 14:10:56 -08001160 int prev_eob = txb_info->eob;
1161 assert(HBT_EOB <= 16); // Lengthen array if allowing longer eob.
1162 int32_t prev_coeff[16];
1163 for (int i = 0; i < prev_eob; i++) {
1164 prev_coeff[i] = txb_info->qcoeff[scan[i]];
1165 }
1166 for (int i = prev_eob; i < HBT_EOB; i++) {
1167 prev_coeff[i] = 0; // For compiler piece of mind.
1168 }
Michelle Findlay-Olynykfbab0622017-12-13 14:10:56 -08001169
1170 av1_txb_init_levels(txb_info->qcoeff, txb_info->width, txb_info->height,
1171 txb_info->levels);
Michelle Findlay-Olynykdea531d2017-12-13 14:10:56 -08001172
Angie Chiang45385b82018-03-02 15:25:58 -08001173 const int update =
1174 optimize_txb(txb_info, txb_costs, txb_eob_costs, rate_cost);
Michelle Findlay-Olynykdea531d2017-12-13 14:10:56 -08001175
1176 // Overwrite old entry
1177 uint16_t hbt_table_index = hbt_ctx_hash % HBT_TABLE_SIZE;
1178 uint16_t hbt_array_index = hbt_qc_hash % HBT_ARRAY_LENGTH;
1179 hbt_hash_table[hbt_table_index * HBT_ARRAY_LENGTH + hbt_array_index]
1180 .rate_cost = *rate_cost;
1181 hbt_hash_table[hbt_table_index * HBT_ARRAY_LENGTH + hbt_array_index].init = 1;
1182 hbt_hash_table[hbt_table_index * HBT_ARRAY_LENGTH + hbt_array_index]
1183 .hbt_qc_hash = hbt_qc_hash;
1184 hbt_hash_table[hbt_table_index * HBT_ARRAY_LENGTH + hbt_array_index]
1185 .hbt_ctx_hash = hbt_ctx_hash;
1186 assert(prev_eob >= txb_info->eob); // eob can't get longer
1187 for (int i = 0; i < txb_info->eob; i++) {
1188 // Record how coeff changed. Convention: towards zero is negative.
1189 if (txb_info->qcoeff[scan[i]] > 0)
1190 hbt_hash_table[hbt_table_index * HBT_ARRAY_LENGTH + hbt_array_index]
1191 .deltas[i] = txb_info->qcoeff[scan[i]] - prev_coeff[i];
1192 else
1193 hbt_hash_table[hbt_table_index * HBT_ARRAY_LENGTH + hbt_array_index]
1194 .deltas[i] = prev_coeff[i] - txb_info->qcoeff[scan[i]];
1195 }
1196 for (int i = txb_info->eob; i < prev_eob; i++) {
1197 // If eob got shorter, record that all after it changed to zero.
1198 if (prev_coeff[i] > 0)
1199 hbt_hash_table[hbt_table_index * HBT_ARRAY_LENGTH + hbt_array_index]
1200 .deltas[i] = -prev_coeff[i];
1201 else
1202 hbt_hash_table[hbt_table_index * HBT_ARRAY_LENGTH + hbt_array_index]
1203 .deltas[i] = prev_coeff[i];
1204 }
1205 for (int i = prev_eob; i < HBT_EOB; i++) {
1206 // Record 'no change' after optimized coefficients run out.
1207 hbt_hash_table[hbt_table_index * HBT_ARRAY_LENGTH + hbt_array_index]
1208 .deltas[i] = 0;
1209 }
Michelle Findlay-Olynykfbab0622017-12-13 14:10:56 -08001210
1211 if (update) {
Michelle Findlay-Olynykfbab0622017-12-13 14:10:56 -08001212 p->eobs[block] = txb_info->eob;
1213 p->txb_entropy_ctx[block] = av1_get_txb_entropy_context(
1214 txb_info->qcoeff, txb_info->scan_order, txb_info->eob);
1215 }
1216 return txb_info->eob;
1217}
1218
Sarah Parker427e3b12018-10-12 12:28:44 -07001219static int hbt_hash_hit(uint32_t hbt_table_index, int hbt_array_index,
1220 TxbInfo *txb_info, const struct macroblock_plane *p,
1221 int block, int *rate_cost) {
Michelle Findlay-Olynykfbab0622017-12-13 14:10:56 -08001222 const int16_t *scan = txb_info->scan_order->scan;
1223 int new_eob = 0;
1224 int update = 0;
1225
1226 for (int i = 0; i < txb_info->eob; i++) {
Michelle Findlay-Olynykdea531d2017-12-13 14:10:56 -08001227 // Delta convention is negatives go towards zero, so only apply those ones.
1228 if (hbt_hash_table[hbt_table_index * HBT_ARRAY_LENGTH + hbt_array_index]
1229 .deltas[i] < 0) {
1230 if (txb_info->qcoeff[scan[i]] > 0)
1231 txb_info->qcoeff[scan[i]] +=
1232 hbt_hash_table[hbt_table_index * HBT_ARRAY_LENGTH + hbt_array_index]
1233 .deltas[i];
1234 else
1235 txb_info->qcoeff[scan[i]] -=
1236 hbt_hash_table[hbt_table_index * HBT_ARRAY_LENGTH + hbt_array_index]
1237 .deltas[i];
1238
Michelle Findlay-Olynykfbab0622017-12-13 14:10:56 -08001239 update = 1;
1240 update_coeff(scan[i], txb_info->qcoeff[scan[i]], txb_info);
1241 }
Michelle Findlay-Olynykfbab0622017-12-13 14:10:56 -08001242 if (txb_info->qcoeff[scan[i]]) new_eob = i + 1;
1243 }
1244
Michelle Findlay-Olynykdea531d2017-12-13 14:10:56 -08001245 // Rate_cost can be calculated here instead (av1_cost_coeffs_txb), but
1246 // it is expensive and gives little benefit as long as qc_hash is high bit
1247 *rate_cost =
1248 hbt_hash_table[hbt_table_index * HBT_ARRAY_LENGTH + hbt_array_index]
1249 .rate_cost;
1250
Michelle Findlay-Olynykfbab0622017-12-13 14:10:56 -08001251 if (update) {
1252 txb_info->eob = new_eob;
1253 p->eobs[block] = txb_info->eob;
1254 p->txb_entropy_ctx[block] = av1_get_txb_entropy_context(
1255 txb_info->qcoeff, txb_info->scan_order, txb_info->eob);
1256 }
Michelle Findlay-Olynykdea531d2017-12-13 14:10:56 -08001257
Michelle Findlay-Olynykfbab0622017-12-13 14:10:56 -08001258 return txb_info->eob;
1259}
1260
Sarah Parker427e3b12018-10-12 12:28:44 -07001261static int hbt_search_match(uint32_t hbt_ctx_hash, uint32_t hbt_qc_hash,
1262 TxbInfo *txb_info,
1263 const LV_MAP_COEFF_COST *txb_costs,
1264 const LV_MAP_EOB_COST *txb_eob_costs,
1265 const struct macroblock_plane *p, int block,
1266 int fast_mode, int *rate_cost) {
Michelle Findlay-Olynykdea531d2017-12-13 14:10:56 -08001267 // Check for qcoeff match
1268 int hbt_array_index = hbt_qc_hash % HBT_ARRAY_LENGTH;
1269 int hbt_table_index = hbt_ctx_hash % HBT_TABLE_SIZE;
Michelle Findlay-Olynykfbab0622017-12-13 14:10:56 -08001270
Michelle Findlay-Olynykdea531d2017-12-13 14:10:56 -08001271 if (hbt_hash_table[hbt_table_index * HBT_ARRAY_LENGTH + hbt_array_index]
1272 .hbt_qc_hash == hbt_qc_hash &&
1273 hbt_hash_table[hbt_table_index * HBT_ARRAY_LENGTH + hbt_array_index]
1274 .hbt_ctx_hash == hbt_ctx_hash &&
1275 hbt_hash_table[hbt_table_index * HBT_ARRAY_LENGTH + hbt_array_index]
1276 .init) {
1277 return hbt_hash_hit(hbt_table_index, hbt_array_index, txb_info, p, block,
1278 rate_cost);
1279 } else {
1280 return hbt_hash_miss(hbt_ctx_hash, hbt_qc_hash, txb_info, txb_costs,
1281 txb_eob_costs, p, block, fast_mode, rate_cost);
Michelle Findlay-Olynykfbab0622017-12-13 14:10:56 -08001282 }
1283}
1284
Sarah Parker427e3b12018-10-12 12:28:44 -07001285static int hbt_create_hashes(TxbInfo *txb_info,
1286 const LV_MAP_COEFF_COST *txb_costs,
1287 const LV_MAP_EOB_COST *txb_eob_costs,
1288 const struct macroblock_plane *p, int block,
1289 int fast_mode, int *rate_cost) {
Michelle Findlay-Olynykfbab0622017-12-13 14:10:56 -08001290 // Initialize hash table if needed.
Michelle Findlay-Olynykdea531d2017-12-13 14:10:56 -08001291 if (hbt_needs_init) {
1292 hbt_init();
Michelle Findlay-Olynykfbab0622017-12-13 14:10:56 -08001293 }
1294
1295 //// Hash creation
Michelle Findlay-Olynykdea531d2017-12-13 14:10:56 -08001296 uint8_t txb_hash_data[256]; // Asserts below to ensure enough space.
Michelle Findlay-Olynykfbab0622017-12-13 14:10:56 -08001297 const int16_t *scan = txb_info->scan_order->scan;
1298 uint8_t chunk = 0;
Michelle Findlay-Olynykfbab0622017-12-13 14:10:56 -08001299 int hash_data_index = 0;
Michelle Findlay-Olynykfbab0622017-12-13 14:10:56 -08001300
Michelle Findlay-Olynykdea531d2017-12-13 14:10:56 -08001301 // Make qc_hash.
1302 int packing_index = 0; // needed for packing.
Michelle Findlay-Olynykfbab0622017-12-13 14:10:56 -08001303 for (int i = 0; i < txb_info->eob; i++) {
Michelle Findlay-Olynykdea531d2017-12-13 14:10:56 -08001304 tran_low_t prechunk = txb_info->qcoeff[scan[i]];
1305
1306 // Softening: Improves speed. Aligns with signed deltas.
1307 if (prechunk < 0) prechunk *= -1;
1308
1309 // Early kick out: Don't apply feature if there are large coeffs:
1310 // If this kickout value is removed or raised beyond int8_t,
1311 // widen deltas type in OptTxbQcoeff struct.
1312 assert((int8_t)HBT_KICKOUT == HBT_KICKOUT); // If not, widen types.
1313 if (prechunk > HBT_KICKOUT) {
1314 av1_txb_init_levels(txb_info->qcoeff, txb_info->width, txb_info->height,
1315 txb_info->levels);
1316
Angie Chiang45385b82018-03-02 15:25:58 -08001317 const int update =
1318 optimize_txb(txb_info, txb_costs, txb_eob_costs, rate_cost);
Michelle Findlay-Olynykdea531d2017-12-13 14:10:56 -08001319
1320 if (update) {
1321 p->eobs[block] = txb_info->eob;
1322 p->txb_entropy_ctx[block] = av1_get_txb_entropy_context(
1323 txb_info->qcoeff, txb_info->scan_order, txb_info->eob);
1324 }
1325 return txb_info->eob;
1326 }
1327
1328 // Since coeffs are 0 to 3, only 2 bits are needed: pack into bytes
1329 if (packing_index == 0) txb_hash_data[hash_data_index] = 0;
1330 chunk = prechunk << packing_index;
1331 packing_index += 2;
1332 txb_hash_data[hash_data_index] |= chunk;
1333
1334 // Full byte:
1335 if (packing_index == 8) {
1336 packing_index = 0;
1337 hash_data_index++;
Michelle Findlay-Olynykfbab0622017-12-13 14:10:56 -08001338 }
1339 }
Michelle Findlay-Olynykdea531d2017-12-13 14:10:56 -08001340 // Needed when packing_index != 0, to include final byte.
1341 hash_data_index++;
1342 assert(hash_data_index <= 64);
1343 // 31 bit qc_hash: index to array
1344 uint32_t hbt_qc_hash =
Peng Bin8a204cd2018-04-08 13:07:35 +08001345 av1_get_crc32c_value(&crc_calculator, txb_hash_data, hash_data_index);
Michelle Findlay-Olynykdea531d2017-12-13 14:10:56 -08001346
1347 // Make ctx_hash.
1348 hash_data_index = 0;
1349 tran_low_t prechunk;
1350
1351 for (int i = 0; i < txb_info->eob; i++) {
1352 // Save as magnitudes towards or away from zero.
1353 if (txb_info->tcoeff[scan[i]] >= 0)
1354 prechunk = txb_info->tcoeff[scan[i]] - txb_info->dqcoeff[scan[i]];
1355 else
1356 prechunk = txb_info->dqcoeff[scan[i]] - txb_info->tcoeff[scan[i]];
1357
1358 chunk = prechunk & 0xff;
1359 txb_hash_data[hash_data_index++] = chunk;
1360 }
1361
1362 // Extra ctx data:
1363 // Include dequants.
1364 txb_hash_data[hash_data_index++] = txb_info->dequant[0] & 0xff;
1365 txb_hash_data[hash_data_index++] = txb_info->dequant[1] & 0xff;
1366 chunk = txb_info->txb_ctx->txb_skip_ctx & 0xff;
1367 txb_hash_data[hash_data_index++] = chunk;
1368 chunk = txb_info->txb_ctx->dc_sign_ctx & 0xff;
1369 txb_hash_data[hash_data_index++] = chunk;
1370 // eob
1371 chunk = txb_info->eob & 0xff;
1372 txb_hash_data[hash_data_index++] = chunk;
1373 // rdmult (int64)
1374 chunk = txb_info->rdmult & 0xff;
1375 txb_hash_data[hash_data_index++] = chunk;
1376 // tx_type
1377 chunk = txb_info->tx_type & 0xff;
1378 txb_hash_data[hash_data_index++] = chunk;
Michelle Findlay-Olynykfbab0622017-12-13 14:10:56 -08001379 // base_eob_cost
1380 for (int i = 1; i < 3; i++) { // i = 0 are softened away
1381 for (int j = 0; j < SIG_COEF_CONTEXTS_EOB; j++) {
1382 chunk = (txb_costs->base_eob_cost[j][i] & 0xff00) >> 8;
1383 txb_hash_data[hash_data_index++] = chunk;
1384 }
Michelle Findlay-Olynykdea531d2017-12-13 14:10:56 -08001385 }
1386 // eob_cost
1387 for (int i = 0; i < 11; i++) {
1388 for (int j = 0; j < 2; j++) {
1389 chunk = (txb_eob_costs->eob_cost[j][i] & 0xff00) >> 8;
1390 txb_hash_data[hash_data_index++] = chunk;
1391 }
1392 }
1393 // dc_sign_cost
1394 for (int i = 0; i < 2; i++) {
1395 for (int j = 0; j < DC_SIGN_CONTEXTS; j++) {
1396 chunk = (txb_costs->dc_sign_cost[j][i] & 0xff00) >> 8;
1397 txb_hash_data[hash_data_index++] = chunk;
1398 }
1399 }
Michelle Findlay-Olynykfbab0622017-12-13 14:10:56 -08001400
Michelle Findlay-Olynykdea531d2017-12-13 14:10:56 -08001401 assert(hash_data_index <= 256);
1402 // 31 bit ctx_hash: used to index table
1403 uint32_t hbt_ctx_hash =
Peng Bin8a204cd2018-04-08 13:07:35 +08001404 av1_get_crc32c_value(&crc_calculator, txb_hash_data, hash_data_index);
Michelle Findlay-Olynykfbab0622017-12-13 14:10:56 -08001405 //// End hash creation
1406
Michelle Findlay-Olynykdea531d2017-12-13 14:10:56 -08001407 return hbt_search_match(hbt_ctx_hash, hbt_qc_hash, txb_info, txb_costs,
1408 txb_eob_costs, p, block, fast_mode, rate_cost);
Michelle Findlay-Olynykfbab0622017-12-13 14:10:56 -08001409}
Yaowu Xu74e63352019-05-06 09:21:33 -07001410#endif // CONFIG_HTB_TRELLIS
Michelle Findlay-Olynykfbab0622017-12-13 14:10:56 -08001411
Wenyao Liuf7e53752019-01-22 17:34:44 +08001412static AOM_FORCE_INLINE int get_two_coeff_cost_simple(
Katsuhisa Yuasa9f639962018-04-08 18:00:54 +09001413 int ci, tran_low_t abs_qc, int coeff_ctx,
1414 const LV_MAP_COEFF_COST *txb_costs, int bwl, TX_CLASS tx_class,
Wenyao Liuf7e53752019-01-22 17:34:44 +08001415 const uint8_t *levels, int *cost_low) {
Angie Chiang40b07312018-03-30 10:42:55 -07001416 // this simple version assumes the coeff's scan_idx is not DC (scan_idx != 0)
Angie Chiange4ea7482018-03-15 11:36:41 -07001417 // and not the last (scan_idx != eob - 1)
1418 assert(ci > 0);
1419 int cost = txb_costs->base_cost[coeff_ctx][AOMMIN(abs_qc, 3)];
Wenyao Liuf7e53752019-01-22 17:34:44 +08001420 int diff = 0;
1421 if (abs_qc <= 3) diff = txb_costs->base_cost[coeff_ctx][abs_qc + 4];
Angie Chiange4ea7482018-03-15 11:36:41 -07001422 if (abs_qc) {
1423 cost += av1_cost_literal(1);
1424 if (abs_qc > NUM_BASE_LEVELS) {
Katsuhisa Yuasa55c95f82018-04-03 00:51:22 +09001425 const int br_ctx = get_br_ctx(levels, ci, bwl, tx_class);
Wenyao Liuf7e53752019-01-22 17:34:44 +08001426 int brcost_diff = 0;
1427 cost += get_br_cost_with_diff(abs_qc, txb_costs->lps_cost[br_ctx],
1428 &brcost_diff);
1429 diff += brcost_diff;
Jim Bankoskife171222019-01-14 08:41:57 -08001430 }
1431 }
Wenyao Liuf7e53752019-01-22 17:34:44 +08001432 *cost_low = cost - diff;
1433
Jim Bankoskife171222019-01-14 08:41:57 -08001434 return cost;
1435}
1436
1437static INLINE int get_coeff_cost_eob(int ci, tran_low_t abs_qc, int sign,
1438 int coeff_ctx, int dc_sign_ctx,
1439 const LV_MAP_COEFF_COST *txb_costs,
1440 int bwl, TX_CLASS tx_class) {
1441 int cost = 0;
1442 cost += txb_costs->base_eob_cost[coeff_ctx][AOMMIN(abs_qc, 3) - 1];
1443 if (abs_qc != 0) {
1444 if (ci == 0) {
1445 cost += txb_costs->dc_sign_cost[dc_sign_ctx][sign];
1446 } else {
1447 cost += av1_cost_literal(1);
1448 }
1449 if (abs_qc > NUM_BASE_LEVELS) {
1450 int br_ctx;
1451 br_ctx = get_br_ctx_eob(ci, bwl, tx_class);
1452 cost += get_br_cost(abs_qc, txb_costs->lps_cost[br_ctx]);
Angie Chiange4ea7482018-03-15 11:36:41 -07001453 }
1454 }
1455 return cost;
1456}
1457
1458static INLINE int get_coeff_cost_general(int is_last, int ci, tran_low_t abs_qc,
Angie Chiang40b07312018-03-30 10:42:55 -07001459 int sign, int coeff_ctx,
Angie Chiange4ea7482018-03-15 11:36:41 -07001460 int dc_sign_ctx,
1461 const LV_MAP_COEFF_COST *txb_costs,
Katsuhisa Yuasa55c95f82018-04-03 00:51:22 +09001462 int bwl, TX_CLASS tx_class,
Angie Chiange4ea7482018-03-15 11:36:41 -07001463 const uint8_t *levels) {
1464 int cost = 0;
1465 if (is_last) {
1466 cost += txb_costs->base_eob_cost[coeff_ctx][AOMMIN(abs_qc, 3) - 1];
1467 } else {
1468 cost += txb_costs->base_cost[coeff_ctx][AOMMIN(abs_qc, 3)];
1469 }
1470 if (abs_qc != 0) {
1471 if (ci == 0) {
Angie Chiang40b07312018-03-30 10:42:55 -07001472 cost += txb_costs->dc_sign_cost[dc_sign_ctx][sign];
Angie Chiange4ea7482018-03-15 11:36:41 -07001473 } else {
1474 cost += av1_cost_literal(1);
1475 }
1476 if (abs_qc > NUM_BASE_LEVELS) {
Jim Bankoski95cb0d72019-01-11 15:20:06 -08001477 int br_ctx;
1478 if (is_last)
1479 br_ctx = get_br_ctx_eob(ci, bwl, tx_class);
1480 else
1481 br_ctx = get_br_ctx(levels, ci, bwl, tx_class);
Jim Bankoskife171222019-01-14 08:41:57 -08001482 cost += get_br_cost(abs_qc, txb_costs->lps_cost[br_ctx]);
Angie Chiange4ea7482018-03-15 11:36:41 -07001483 }
1484 }
1485 return cost;
1486}
1487
Angie Chiang40b07312018-03-30 10:42:55 -07001488static INLINE void get_qc_dqc_low(tran_low_t abs_qc, int sign, int dqv,
Angie Chiange4ea7482018-03-15 11:36:41 -07001489 int shift, tran_low_t *qc_low,
1490 tran_low_t *dqc_low) {
1491 tran_low_t abs_qc_low = abs_qc - 1;
Angie Chiang40b07312018-03-30 10:42:55 -07001492 *qc_low = (-sign ^ abs_qc_low) + sign;
1493 assert((sign ? -abs_qc_low : abs_qc_low) == *qc_low);
Angie Chiange4ea7482018-03-15 11:36:41 -07001494 tran_low_t abs_dqc_low = (abs_qc_low * dqv) >> shift;
Angie Chiang40b07312018-03-30 10:42:55 -07001495 *dqc_low = (-sign ^ abs_dqc_low) + sign;
1496 assert((sign ? -abs_dqc_low : abs_dqc_low) == *dqc_low);
Angie Chiange4ea7482018-03-15 11:36:41 -07001497}
1498
1499static INLINE void update_coeff_general(
1500 int *accu_rate, int64_t *accu_dist, int si, int eob, TX_SIZE tx_size,
Katsuhisa Yuasa55c95f82018-04-03 00:51:22 +09001501 TX_CLASS tx_class, int bwl, int height, int64_t rdmult, int shift,
Angie Chiange4ea7482018-03-15 11:36:41 -07001502 int dc_sign_ctx, const int16_t *dequant, const int16_t *scan,
1503 const LV_MAP_COEFF_COST *txb_costs, const tran_low_t *tcoeff,
Jingning Han37f53542019-06-06 14:22:17 -07001504 tran_low_t *qcoeff, tran_low_t *dqcoeff, uint8_t *levels,
1505 const qm_val_t *iqmatrix) {
1506 const int dqv = get_dqv(dequant, scan[si], iqmatrix);
Angie Chiange4ea7482018-03-15 11:36:41 -07001507 const int ci = scan[si];
1508 const tran_low_t qc = qcoeff[ci];
1509 const int is_last = si == (eob - 1);
1510 const int coeff_ctx = get_lower_levels_ctx_general(
Katsuhisa Yuasa55c95f82018-04-03 00:51:22 +09001511 is_last, si, bwl, height, levels, ci, tx_size, tx_class);
Angie Chiange4ea7482018-03-15 11:36:41 -07001512 if (qc == 0) {
1513 *accu_rate += txb_costs->base_cost[coeff_ctx][0];
1514 } else {
Angie Chiang40b07312018-03-30 10:42:55 -07001515 const int sign = (qc < 0) ? 1 : 0;
Angie Chiange4ea7482018-03-15 11:36:41 -07001516 const tran_low_t abs_qc = abs(qc);
1517 const tran_low_t tqc = tcoeff[ci];
1518 const tran_low_t dqc = dqcoeff[ci];
1519 const int64_t dist = get_coeff_dist(tqc, dqc, shift);
1520 const int64_t dist0 = get_coeff_dist(tqc, 0, shift);
1521 const int rate =
Angie Chiang40b07312018-03-30 10:42:55 -07001522 get_coeff_cost_general(is_last, ci, abs_qc, sign, coeff_ctx,
Katsuhisa Yuasa55c95f82018-04-03 00:51:22 +09001523 dc_sign_ctx, txb_costs, bwl, tx_class, levels);
Angie Chiange4ea7482018-03-15 11:36:41 -07001524 const int64_t rd = RDCOST(rdmult, rate, dist);
1525
1526 tran_low_t qc_low, dqc_low;
Wenyao Liuf7e53752019-01-22 17:34:44 +08001527 tran_low_t abs_qc_low;
1528 int64_t dist_low, rd_low;
1529 int rate_low;
1530 if (abs_qc == 1) {
1531 abs_qc_low = qc_low = dqc_low = 0;
1532 dist_low = dist0;
1533 rate_low = txb_costs->base_cost[coeff_ctx][0];
1534 } else {
1535 get_qc_dqc_low(abs_qc, sign, dqv, shift, &qc_low, &dqc_low);
1536 abs_qc_low = abs_qc - 1;
1537 dist_low = get_coeff_dist(tqc, dqc_low, shift);
1538 rate_low =
1539 get_coeff_cost_general(is_last, ci, abs_qc_low, sign, coeff_ctx,
1540 dc_sign_ctx, txb_costs, bwl, tx_class, levels);
1541 }
1542
1543 rd_low = RDCOST(rdmult, rate_low, dist_low);
Angie Chiange4ea7482018-03-15 11:36:41 -07001544 if (rd_low < rd) {
1545 qcoeff[ci] = qc_low;
1546 dqcoeff[ci] = dqc_low;
1547 levels[get_padded_idx(ci, bwl)] = AOMMIN(abs_qc_low, INT8_MAX);
1548 *accu_rate += rate_low;
1549 *accu_dist += dist_low - dist0;
1550 } else {
1551 *accu_rate += rate;
1552 *accu_dist += dist - dist0;
1553 }
1554 }
1555}
1556
Katsuhisa Yuasa9f639962018-04-08 18:00:54 +09001557static AOM_FORCE_INLINE void update_coeff_simple(
Katsuhisa Yuasa55c95f82018-04-03 00:51:22 +09001558 int *accu_rate, int si, int eob, TX_SIZE tx_size, TX_CLASS tx_class,
1559 int bwl, int64_t rdmult, int shift, const int16_t *dequant,
1560 const int16_t *scan, const LV_MAP_COEFF_COST *txb_costs,
1561 const tran_low_t *tcoeff, tran_low_t *qcoeff, tran_low_t *dqcoeff,
Jingning Han37f53542019-06-06 14:22:17 -07001562 uint8_t *levels, const qm_val_t *iqmatrix) {
1563 const int dqv = get_dqv(dequant, scan[si], iqmatrix);
Angie Chiange4ea7482018-03-15 11:36:41 -07001564 (void)eob;
Angie Chiang40b07312018-03-30 10:42:55 -07001565 // this simple version assumes the coeff's scan_idx is not DC (scan_idx != 0)
Angie Chiange4ea7482018-03-15 11:36:41 -07001566 // and not the last (scan_idx != eob - 1)
1567 assert(si != eob - 1);
1568 assert(si > 0);
1569 const int ci = scan[si];
1570 const tran_low_t qc = qcoeff[ci];
Katsuhisa Yuasa55c95f82018-04-03 00:51:22 +09001571 const int coeff_ctx =
1572 get_lower_levels_ctx(levels, ci, bwl, tx_size, tx_class);
Angie Chiange4ea7482018-03-15 11:36:41 -07001573 if (qc == 0) {
1574 *accu_rate += txb_costs->base_cost[coeff_ctx][0];
1575 } else {
1576 const tran_low_t abs_qc = abs(qc);
Wenyao Liuf7e53752019-01-22 17:34:44 +08001577 const tran_low_t abs_tqc = abs(tcoeff[ci]);
1578 const tran_low_t abs_dqc = abs(dqcoeff[ci]);
1579 int rate_low = 0;
1580 const int rate = get_two_coeff_cost_simple(
1581 ci, abs_qc, coeff_ctx, txb_costs, bwl, tx_class, levels, &rate_low);
1582 if (abs_dqc < abs_tqc) {
Jingning Han87f5c342018-04-10 23:10:12 -07001583 *accu_rate += rate;
1584 return;
1585 }
Wenyao Liuf7e53752019-01-22 17:34:44 +08001586
1587 const int64_t dist = get_coeff_dist(abs_tqc, abs_dqc, shift);
Angie Chiange4ea7482018-03-15 11:36:41 -07001588 const int64_t rd = RDCOST(rdmult, rate, dist);
1589
Angie Chiange4ea7482018-03-15 11:36:41 -07001590 const tran_low_t abs_qc_low = abs_qc - 1;
Wenyao Liuf7e53752019-01-22 17:34:44 +08001591 const tran_low_t abs_dqc_low = (abs_qc_low * dqv) >> shift;
1592 const int64_t dist_low = get_coeff_dist(abs_tqc, abs_dqc_low, shift);
Angie Chiange4ea7482018-03-15 11:36:41 -07001593 const int64_t rd_low = RDCOST(rdmult, rate_low, dist_low);
Wenyao Liuf7e53752019-01-22 17:34:44 +08001594
Angie Chiange4ea7482018-03-15 11:36:41 -07001595 if (rd_low < rd) {
Wenyao Liuf7e53752019-01-22 17:34:44 +08001596 const int sign = (qc < 0) ? 1 : 0;
1597 qcoeff[ci] = (-sign ^ abs_qc_low) + sign;
1598 dqcoeff[ci] = (-sign ^ abs_dqc_low) + sign;
Angie Chiange4ea7482018-03-15 11:36:41 -07001599 levels[get_padded_idx(ci, bwl)] = AOMMIN(abs_qc_low, INT8_MAX);
1600 *accu_rate += rate_low;
Angie Chiange4ea7482018-03-15 11:36:41 -07001601 } else {
1602 *accu_rate += rate;
Angie Chiange4ea7482018-03-15 11:36:41 -07001603 }
1604 }
1605}
1606
Katsuhisa Yuasa9f639962018-04-08 18:00:54 +09001607static AOM_FORCE_INLINE void update_coeff_eob(
Angie Chiange4ea7482018-03-15 11:36:41 -07001608 int *accu_rate, int64_t *accu_dist, int *eob, int *nz_num, int *nz_ci,
Katsuhisa Yuasa55c95f82018-04-03 00:51:22 +09001609 int si, TX_SIZE tx_size, TX_CLASS tx_class, int bwl, int height,
Angie Chiange4ea7482018-03-15 11:36:41 -07001610 int dc_sign_ctx, int64_t rdmult, int shift, const int16_t *dequant,
1611 const int16_t *scan, const LV_MAP_EOB_COST *txb_eob_costs,
1612 const LV_MAP_COEFF_COST *txb_costs, const tran_low_t *tcoeff,
Jingning Han37f53542019-06-06 14:22:17 -07001613 tran_low_t *qcoeff, tran_low_t *dqcoeff, uint8_t *levels, int sharpness,
1614 const qm_val_t *iqmatrix) {
1615 const int dqv = get_dqv(dequant, scan[si], iqmatrix);
Angie Chiange4ea7482018-03-15 11:36:41 -07001616 assert(si != *eob - 1);
1617 const int ci = scan[si];
1618 const tran_low_t qc = qcoeff[ci];
Katsuhisa Yuasa55c95f82018-04-03 00:51:22 +09001619 const int coeff_ctx =
1620 get_lower_levels_ctx(levels, ci, bwl, tx_size, tx_class);
Angie Chiange4ea7482018-03-15 11:36:41 -07001621 if (qc == 0) {
1622 *accu_rate += txb_costs->base_cost[coeff_ctx][0];
1623 } else {
1624 int lower_level = 0;
1625 const tran_low_t abs_qc = abs(qc);
1626 const tran_low_t tqc = tcoeff[ci];
1627 const tran_low_t dqc = dqcoeff[ci];
Angie Chiang40b07312018-03-30 10:42:55 -07001628 const int sign = (qc < 0) ? 1 : 0;
Angie Chiange4ea7482018-03-15 11:36:41 -07001629 const int64_t dist0 = get_coeff_dist(tqc, 0, shift);
1630 int64_t dist = get_coeff_dist(tqc, dqc, shift) - dist0;
1631 int rate =
Angie Chiang40b07312018-03-30 10:42:55 -07001632 get_coeff_cost_general(0, ci, abs_qc, sign, coeff_ctx, dc_sign_ctx,
Katsuhisa Yuasa55c95f82018-04-03 00:51:22 +09001633 txb_costs, bwl, tx_class, levels);
Angie Chiange4ea7482018-03-15 11:36:41 -07001634 int64_t rd = RDCOST(rdmult, *accu_rate + rate, *accu_dist + dist);
1635
1636 tran_low_t qc_low, dqc_low;
Wenyao Liuf7e53752019-01-22 17:34:44 +08001637 tran_low_t abs_qc_low;
1638 int64_t dist_low, rd_low;
1639 int rate_low;
1640 if (abs_qc == 1) {
1641 abs_qc_low = 0;
1642 dqc_low = qc_low = 0;
1643 dist_low = 0;
1644 rate_low = txb_costs->base_cost[coeff_ctx][0];
1645 rd_low = RDCOST(rdmult, *accu_rate + rate_low, *accu_dist);
1646 } else {
1647 get_qc_dqc_low(abs_qc, sign, dqv, shift, &qc_low, &dqc_low);
1648 abs_qc_low = abs_qc - 1;
1649 dist_low = get_coeff_dist(tqc, dqc_low, shift) - dist0;
1650 rate_low =
1651 get_coeff_cost_general(0, ci, abs_qc_low, sign, coeff_ctx,
1652 dc_sign_ctx, txb_costs, bwl, tx_class, levels);
1653 rd_low = RDCOST(rdmult, *accu_rate + rate_low, *accu_dist + dist_low);
1654 }
Angie Chiange4ea7482018-03-15 11:36:41 -07001655
1656 int lower_level_new_eob = 0;
1657 const int new_eob = si + 1;
wenyao.liu0be59012018-12-24 10:27:31 +08001658 const int coeff_ctx_new_eob = get_lower_levels_ctx_eob(bwl, height, si);
Angie Chiange4ea7482018-03-15 11:36:41 -07001659 const int new_eob_cost =
Katsuhisa Yuasa55c95f82018-04-03 00:51:22 +09001660 get_eob_cost(new_eob, txb_eob_costs, txb_costs, tx_class);
Angie Chiange4ea7482018-03-15 11:36:41 -07001661 int rate_coeff_eob =
Jim Bankoskife171222019-01-14 08:41:57 -08001662 new_eob_cost + get_coeff_cost_eob(ci, abs_qc, sign, coeff_ctx_new_eob,
1663 dc_sign_ctx, txb_costs, bwl,
1664 tx_class);
Angie Chiange4ea7482018-03-15 11:36:41 -07001665 int64_t dist_new_eob = dist;
1666 int64_t rd_new_eob = RDCOST(rdmult, rate_coeff_eob, dist_new_eob);
1667
1668 if (abs_qc_low > 0) {
1669 const int rate_coeff_eob_low =
Jim Bankoskife171222019-01-14 08:41:57 -08001670 new_eob_cost + get_coeff_cost_eob(ci, abs_qc_low, sign,
1671 coeff_ctx_new_eob, dc_sign_ctx,
1672 txb_costs, bwl, tx_class);
Angie Chiange4ea7482018-03-15 11:36:41 -07001673 const int64_t dist_new_eob_low = dist_low;
1674 const int64_t rd_new_eob_low =
1675 RDCOST(rdmult, rate_coeff_eob_low, dist_new_eob_low);
1676 if (rd_new_eob_low < rd_new_eob) {
1677 lower_level_new_eob = 1;
1678 rd_new_eob = rd_new_eob_low;
1679 rate_coeff_eob = rate_coeff_eob_low;
1680 dist_new_eob = dist_new_eob_low;
1681 }
1682 }
1683
1684 if (rd_low < rd) {
1685 lower_level = 1;
1686 rd = rd_low;
1687 rate = rate_low;
1688 dist = dist_low;
1689 }
1690
Jim Bankoski855ac462018-06-07 09:05:00 -07001691 if (sharpness == 0 && rd_new_eob < rd) {
Angie Chiange4ea7482018-03-15 11:36:41 -07001692 for (int ni = 0; ni < *nz_num; ++ni) {
1693 int last_ci = nz_ci[ni];
Jim Bankoski95cb0d72019-01-11 15:20:06 -08001694 levels[get_padded_idx(last_ci, bwl)] = 0;
Angie Chiange4ea7482018-03-15 11:36:41 -07001695 qcoeff[last_ci] = 0;
1696 dqcoeff[last_ci] = 0;
1697 }
1698 *eob = new_eob;
1699 *nz_num = 0;
1700 *accu_rate = rate_coeff_eob;
1701 *accu_dist = dist_new_eob;
1702 lower_level = lower_level_new_eob;
1703 } else {
Angie Chiange4ea7482018-03-15 11:36:41 -07001704 *accu_rate += rate;
1705 *accu_dist += dist;
1706 }
1707
1708 if (lower_level) {
1709 qcoeff[ci] = qc_low;
1710 dqcoeff[ci] = dqc_low;
1711 levels[get_padded_idx(ci, bwl)] = AOMMIN(abs_qc_low, INT8_MAX);
1712 }
1713 if (qcoeff[ci]) {
1714 nz_ci[*nz_num] = ci;
1715 ++*nz_num;
1716 }
1717 }
1718}
1719
Angie Chiangd0397e12018-03-27 19:03:20 -07001720static INLINE void update_skip(int *accu_rate, int64_t accu_dist, int *eob,
1721 int nz_num, int *nz_ci, int64_t rdmult,
1722 int skip_cost, int non_skip_cost,
Jim Bankoski855ac462018-06-07 09:05:00 -07001723 tran_low_t *qcoeff, tran_low_t *dqcoeff,
1724 int sharpness) {
Angie Chiangd0397e12018-03-27 19:03:20 -07001725 const int64_t rd = RDCOST(rdmult, *accu_rate + non_skip_cost, accu_dist);
Angie Chiange4ea7482018-03-15 11:36:41 -07001726 const int64_t rd_new_eob = RDCOST(rdmult, skip_cost, 0);
Jim Bankoski855ac462018-06-07 09:05:00 -07001727 if (sharpness == 0 && rd_new_eob < rd) {
Angie Chiangd0397e12018-03-27 19:03:20 -07001728 for (int i = 0; i < nz_num; ++i) {
1729 const int ci = nz_ci[i];
Angie Chiange4ea7482018-03-15 11:36:41 -07001730 qcoeff[ci] = 0;
1731 dqcoeff[ci] = 0;
Angie Chiangd0397e12018-03-27 19:03:20 -07001732 // no need to set up levels because this is the last step
1733 // levels[get_padded_idx(ci, bwl)] = 0;
Angie Chiange4ea7482018-03-15 11:36:41 -07001734 }
Angie Chiange4ea7482018-03-15 11:36:41 -07001735 *accu_rate = 0;
1736 *eob = 0;
1737 }
1738}
1739
1740int av1_optimize_txb_new(const struct AV1_COMP *cpi, MACROBLOCK *x, int plane,
Xing Jinbd91e942018-06-21 13:43:17 +08001741 int block, TX_SIZE tx_size, TX_TYPE tx_type,
1742 const TXB_CTX *const txb_ctx, int *rate_cost,
Sarah Parker89df0342019-02-07 22:23:39 -08001743 int sharpness, int fast_mode) {
Angie Chiange4ea7482018-03-15 11:36:41 -07001744 MACROBLOCKD *xd = &x->e_mbd;
Sarah Parker89df0342019-02-07 22:23:39 -08001745 const struct macroblock_plane *p = &x->plane[plane];
1746 const SCAN_ORDER *scan_order = get_scan(tx_size, tx_type);
1747 const int16_t *scan = scan_order->scan;
1748 const int shift = av1_get_tx_scale(tx_size);
1749 int eob = p->eobs[block];
1750 const int16_t *dequant = p->dequant_QTX;
Jingning Han37f53542019-06-06 14:22:17 -07001751 const qm_val_t *iqmatrix =
Urvang Joshif409a632020-03-28 01:03:50 -07001752 av1_get_iqmatrix(&cpi->common.quant_params, xd, plane, tx_size, tx_type);
Yaowu Xu18a49962019-10-16 12:47:04 -07001753 const int block_offset = BLOCK_OFFSET(block);
1754 tran_low_t *qcoeff = p->qcoeff + block_offset;
Urvang Joshi9543ad72020-04-17 16:59:46 -07001755 tran_low_t *dqcoeff = p->dqcoeff + block_offset;
Yaowu Xu18a49962019-10-16 12:47:04 -07001756 const tran_low_t *tcoeff = p->coeff + block_offset;
chiyotsai9a06d182020-05-01 17:12:12 -07001757 const CoeffCosts *coeff_costs = &x->coeff_costs;
Sarah Parker89df0342019-02-07 22:23:39 -08001758
Jingning Han3e4c9ed2019-06-07 15:03:16 -07001759 // This function is not called if eob = 0.
1760 assert(eob > 0);
1761
Sarah Parker89df0342019-02-07 22:23:39 -08001762 if (fast_mode) {
1763 update_coeff_eob_fast(&eob, shift, dequant, scan, tcoeff, qcoeff, dqcoeff);
1764 p->eobs[block] = eob;
1765 if (eob == 0) {
chiyotsai9a06d182020-05-01 17:12:12 -07001766 *rate_cost = av1_cost_skip_txb(coeff_costs, txb_ctx, plane, tx_size);
Sarah Parker89df0342019-02-07 22:23:39 -08001767 return eob;
1768 }
1769 }
1770
1771 const AV1_COMMON *cm = &cpi->common;
Angie Chiange4ea7482018-03-15 11:36:41 -07001772 const PLANE_TYPE plane_type = get_plane_type(plane);
1773 const TX_SIZE txs_ctx = get_txsize_entropy_ctx(tx_size);
Katsuhisa Yuasa55c95f82018-04-03 00:51:22 +09001774 const TX_CLASS tx_class = tx_type_to_class[tx_type];
Yue Chen53b53f02018-03-29 14:31:23 -07001775 const MB_MODE_INFO *mbmi = xd->mi[0];
Angie Chiange4ea7482018-03-15 11:36:41 -07001776 const int bwl = get_txb_bwl(tx_size);
1777 const int width = get_txb_wide(tx_size);
1778 const int height = get_txb_high(tx_size);
1779 assert(width == (1 << bwl));
1780 const int is_inter = is_inter_block(mbmi);
chiyotsai9a06d182020-05-01 17:12:12 -07001781 const LV_MAP_COEFF_COST *txb_costs =
1782 &coeff_costs->coeff_costs[txs_ctx][plane_type];
Angie Chiange4ea7482018-03-15 11:36:41 -07001783 const int eob_multi_size = txsize_log2_minus4[tx_size];
1784 const LV_MAP_EOB_COST *txb_eob_costs =
chiyotsai9a06d182020-05-01 17:12:12 -07001785 &coeff_costs->eob_costs[eob_multi_size][plane_type];
Angie Chiange4ea7482018-03-15 11:36:41 -07001786
Debargha Mukherjee7f3ab7f2019-05-30 00:20:50 -07001787 const int rshift =
1788 (sharpness +
Vishesh734eff92020-06-20 21:46:36 +05301789 (cpi->oxcf.q_cfg.aq_mode == VARIANCE_AQ && mbmi->segment_id < 4
Debargha Mukherjee7f3ab7f2019-05-30 00:20:50 -07001790 ? 7 - mbmi->segment_id
1791 : 2) +
Vishesh734eff92020-06-20 21:46:36 +05301792 (cpi->oxcf.q_cfg.aq_mode != VARIANCE_AQ &&
1793 cpi->oxcf.q_cfg.deltaq_mode == DELTA_Q_PERCEPTUAL &&
Debargha Mukherjee7f3ab7f2019-05-30 00:20:50 -07001794 cm->delta_q_info.delta_q_present_flag && x->sb_energy_level < 0
1795 ? (3 - x->sb_energy_level)
1796 : 0));
Debargha Mukherjee53d45d92019-02-11 12:59:50 -08001797 const int64_t rdmult =
1798 (((int64_t)x->rdmult *
1799 (plane_rd_mult[is_inter][plane_type] << (2 * (xd->bd - 8)))) +
1800 2) >>
1801 rshift;
Angie Chiange4ea7482018-03-15 11:36:41 -07001802
1803 uint8_t levels_buf[TX_PAD_2D];
1804 uint8_t *const levels = set_levels(levels_buf, width);
Jim Bankoski855ac462018-06-07 09:05:00 -07001805
Jim Bankoski102f36b2019-01-14 09:37:05 -08001806 if (eob > 1) av1_txb_init_levels(qcoeff, width, height, levels);
Angie Chiange4ea7482018-03-15 11:36:41 -07001807
1808 // TODO(angirbird): check iqmatrix
1809
1810 const int non_skip_cost = txb_costs->txb_skip_cost[txb_ctx->txb_skip_ctx][0];
1811 const int skip_cost = txb_costs->txb_skip_cost[txb_ctx->txb_skip_ctx][1];
Katsuhisa Yuasa55c95f82018-04-03 00:51:22 +09001812 const int eob_cost = get_eob_cost(eob, txb_eob_costs, txb_costs, tx_class);
Angie Chiange4ea7482018-03-15 11:36:41 -07001813 int accu_rate = eob_cost;
1814 int64_t accu_dist = 0;
1815 int si = eob - 1;
1816 const int ci = scan[si];
1817 const tran_low_t qc = qcoeff[ci];
1818 const tran_low_t abs_qc = abs(qc);
Angie Chiang40b07312018-03-30 10:42:55 -07001819 const int sign = qc < 0;
Angie Chiange4ea7482018-03-15 11:36:41 -07001820 const int max_nz_num = 2;
1821 int nz_num = 1;
1822 int nz_ci[3] = { ci, 0, 0 };
1823 if (abs_qc >= 2) {
Katsuhisa Yuasa55c95f82018-04-03 00:51:22 +09001824 update_coeff_general(&accu_rate, &accu_dist, si, eob, tx_size, tx_class,
1825 bwl, height, rdmult, shift, txb_ctx->dc_sign_ctx,
1826 dequant, scan, txb_costs, tcoeff, qcoeff, dqcoeff,
Jingning Han37f53542019-06-06 14:22:17 -07001827 levels, iqmatrix);
Angie Chiange4ea7482018-03-15 11:36:41 -07001828 --si;
1829 } else {
1830 assert(abs_qc == 1);
wenyao.liu0be59012018-12-24 10:27:31 +08001831 const int coeff_ctx = get_lower_levels_ctx_eob(bwl, height, si);
Jim Bankoski102f36b2019-01-14 09:37:05 -08001832 accu_rate +=
1833 get_coeff_cost_eob(ci, abs_qc, sign, coeff_ctx, txb_ctx->dc_sign_ctx,
1834 txb_costs, bwl, tx_class);
Angie Chiange4ea7482018-03-15 11:36:41 -07001835 const tran_low_t tqc = tcoeff[ci];
1836 const tran_low_t dqc = dqcoeff[ci];
1837 const int64_t dist = get_coeff_dist(tqc, dqc, shift);
1838 const int64_t dist0 = get_coeff_dist(tqc, 0, shift);
1839 accu_dist += dist - dist0;
1840 --si;
1841 }
1842
Katsuhisa Yuasa9f639962018-04-08 18:00:54 +09001843#define UPDATE_COEFF_EOB_CASE(tx_class_literal) \
1844 case tx_class_literal: \
Sarah Parker89df0342019-02-07 22:23:39 -08001845 for (; si >= 0 && nz_num <= max_nz_num && !fast_mode; --si) { \
Katsuhisa Yuasa9f639962018-04-08 18:00:54 +09001846 update_coeff_eob(&accu_rate, &accu_dist, &eob, &nz_num, nz_ci, si, \
1847 tx_size, tx_class_literal, bwl, height, \
1848 txb_ctx->dc_sign_ctx, rdmult, shift, dequant, scan, \
1849 txb_eob_costs, txb_costs, tcoeff, qcoeff, dqcoeff, \
Jingning Han37f53542019-06-06 14:22:17 -07001850 levels, sharpness, iqmatrix); \
Katsuhisa Yuasa9f639962018-04-08 18:00:54 +09001851 } \
1852 break;
1853 switch (tx_class) {
1854 UPDATE_COEFF_EOB_CASE(TX_CLASS_2D);
1855 UPDATE_COEFF_EOB_CASE(TX_CLASS_HORIZ);
1856 UPDATE_COEFF_EOB_CASE(TX_CLASS_VERT);
1857#undef UPDATE_COEFF_EOB_CASE
1858 default: assert(false);
Angie Chiange4ea7482018-03-15 11:36:41 -07001859 }
1860
Angie Chiangd0397e12018-03-27 19:03:20 -07001861 if (si == -1 && nz_num <= max_nz_num) {
1862 update_skip(&accu_rate, accu_dist, &eob, nz_num, nz_ci, rdmult, skip_cost,
Jim Bankoski855ac462018-06-07 09:05:00 -07001863 non_skip_cost, qcoeff, dqcoeff, sharpness);
Angie Chiangd0397e12018-03-27 19:03:20 -07001864 }
1865
Katsuhisa Yuasa9f639962018-04-08 18:00:54 +09001866#define UPDATE_COEFF_SIMPLE_CASE(tx_class_literal) \
1867 case tx_class_literal: \
1868 for (; si >= 1; --si) { \
1869 update_coeff_simple(&accu_rate, si, eob, tx_size, tx_class_literal, bwl, \
1870 rdmult, shift, dequant, scan, txb_costs, tcoeff, \
Jingning Han37f53542019-06-06 14:22:17 -07001871 qcoeff, dqcoeff, levels, iqmatrix); \
Katsuhisa Yuasa9f639962018-04-08 18:00:54 +09001872 } \
1873 break;
1874 switch (tx_class) {
1875 UPDATE_COEFF_SIMPLE_CASE(TX_CLASS_2D);
1876 UPDATE_COEFF_SIMPLE_CASE(TX_CLASS_HORIZ);
1877 UPDATE_COEFF_SIMPLE_CASE(TX_CLASS_VERT);
1878#undef UPDATE_COEFF_SIMPLE_CASE
1879 default: assert(false);
Angie Chiange4ea7482018-03-15 11:36:41 -07001880 }
1881
1882 // DC position
1883 if (si == 0) {
Angie Chiangd0397e12018-03-27 19:03:20 -07001884 // no need to update accu_dist because it's not used after this point
1885 int64_t dummy_dist = 0;
Katsuhisa Yuasa55c95f82018-04-03 00:51:22 +09001886 update_coeff_general(&accu_rate, &dummy_dist, si, eob, tx_size, tx_class,
Angie Chiangd0397e12018-03-27 19:03:20 -07001887 bwl, height, rdmult, shift, txb_ctx->dc_sign_ctx,
1888 dequant, scan, txb_costs, tcoeff, qcoeff, dqcoeff,
Jingning Han37f53542019-06-06 14:22:17 -07001889 levels, iqmatrix);
Angie Chiange4ea7482018-03-15 11:36:41 -07001890 }
1891
Urvang Joshib6409e92020-03-23 11:23:27 -07001892 const int tx_type_cost = get_tx_type_cost(x, xd, plane, tx_size, tx_type,
1893 cm->features.reduced_tx_set_used);
Angie Chiange4ea7482018-03-15 11:36:41 -07001894 if (eob == 0)
1895 accu_rate += skip_cost;
1896 else
1897 accu_rate += non_skip_cost + tx_type_cost;
1898
1899 p->eobs[block] = eob;
1900 p->txb_entropy_ctx[block] =
1901 av1_get_txb_entropy_context(qcoeff, scan_order, p->eobs[block]);
1902
1903 *rate_cost = accu_rate;
1904 return eob;
1905}
1906
Angie Chiang40b07312018-03-30 10:42:55 -07001907// This function is deprecated, but we keep it here because hash trellis
Angie Chiange4ea7482018-03-15 11:36:41 -07001908// is not integrated with av1_optimize_txb_new yet
Yaowu Xuefcf1e92018-01-12 17:05:46 -08001909int av1_optimize_txb(const struct AV1_COMP *cpi, MACROBLOCK *x, int plane,
Jingning Han7eab9ff2017-07-06 10:12:54 -07001910 int blk_row, int blk_col, int block, TX_SIZE tx_size,
Cheng Chen82775f62018-01-18 12:09:54 -08001911 TXB_CTX *txb_ctx, int fast_mode, int *rate_cost) {
Michelle Findlay-Olynykfbab0622017-12-13 14:10:56 -08001912 const AV1_COMMON *cm = &cpi->common;
Urvang Joshib6409e92020-03-23 11:23:27 -07001913 const int reduced_tx_set_used = cm->features.reduced_tx_set_used;
Angie Chiang07c57f32017-05-30 18:18:33 -07001914 MACROBLOCKD *const xd = &x->e_mbd;
1915 const PLANE_TYPE plane_type = get_plane_type(plane);
Debargha Mukherjeeb3eda2f2017-11-28 16:00:20 -08001916 const TX_SIZE txs_ctx = get_txsize_entropy_ctx(tx_size);
Hui Su37980cc2019-10-09 10:16:35 -07001917 const TX_TYPE tx_type = av1_get_tx_type(xd, plane_type, blk_row, blk_col,
Yaowu Xu33fd0a02019-10-15 18:23:29 -07001918 tx_size, reduced_tx_set_used);
Yue Chen53b53f02018-03-29 14:31:23 -07001919 const MB_MODE_INFO *mbmi = xd->mi[0];
Angie Chiang07c57f32017-05-30 18:18:33 -07001920 const struct macroblock_plane *p = &x->plane[plane];
Angie Chiang07c57f32017-05-30 18:18:33 -07001921 const int eob = p->eobs[block];
Yaowu Xu18a49962019-10-16 12:47:04 -07001922 const int block_offset = BLOCK_OFFSET(block);
1923 tran_low_t *qcoeff = p->qcoeff + block_offset;
Urvang Joshi9543ad72020-04-17 16:59:46 -07001924 tran_low_t *dqcoeff = p->dqcoeff + block_offset;
Yaowu Xu18a49962019-10-16 12:47:04 -07001925 const tran_low_t *tcoeff = p->coeff + block_offset;
Monty Montgomery125c0fc2017-10-26 00:44:35 -04001926 const int16_t *dequant = p->dequant_QTX;
Urvang Joshi80893152017-10-27 11:51:14 -07001927 const int seg_eob = av1_get_max_eob(tx_size);
Angie Chianga9ba58e2017-12-01 19:22:43 -08001928 const int bwl = get_txb_bwl(tx_size);
1929 const int width = get_txb_wide(tx_size);
1930 const int height = get_txb_high(tx_size);
Angie Chiang07c57f32017-05-30 18:18:33 -07001931 const int is_inter = is_inter_block(mbmi);
Yaowu Xuf1e12932018-02-26 15:50:09 -08001932 const SCAN_ORDER *const scan_order = get_scan(tx_size, tx_type);
chiyotsai9a06d182020-05-01 17:12:12 -07001933 const CoeffCosts *coeff_costs = &x->coeff_costs;
1934 const LV_MAP_COEFF_COST *txb_costs =
1935 &coeff_costs->coeff_costs[txs_ctx][plane_type];
Dake He0db7d0e2017-12-21 15:23:20 -08001936 const int eob_multi_size = txsize_log2_minus4[tx_size];
1937 const LV_MAP_EOB_COST txb_eob_costs =
chiyotsai9a06d182020-05-01 17:12:12 -07001938 coeff_costs->eob_costs[eob_multi_size][plane_type];
Angie Chiang07c57f32017-05-30 18:18:33 -07001939
1940 const int shift = av1_get_tx_scale(tx_size);
1941 const int64_t rdmult =
Debargha Mukherjee53d45d92019-02-11 12:59:50 -08001942 (((int64_t)x->rdmult * plane_rd_mult[is_inter][plane_type]
1943 << (2 * (xd->bd - 8))) +
Jingning Hanb433f4c2017-11-17 15:43:59 -08001944 2) >>
1945 2;
Linfeng Zhang679d81e2017-10-31 15:27:42 -07001946 uint8_t levels_buf[TX_PAD_2D];
1947 uint8_t *const levels = set_levels(levels_buf, width);
Angie Chiangb3167a62018-01-30 19:37:57 -08001948 const qm_val_t *iqmatrix =
Urvang Joshif409a632020-03-28 01:03:50 -07001949 av1_get_iqmatrix(&cpi->common.quant_params, xd, plane, tx_size, tx_type);
Linfeng Zhang1122d7d2017-10-31 15:30:28 -07001950 assert(width == (1 << bwl));
Yaowu Xu33fd0a02019-10-15 18:23:29 -07001951 const int tx_type_cost =
1952 get_tx_type_cost(x, xd, plane, tx_size, tx_type, reduced_tx_set_used);
Linfeng Zhang1015a342017-10-24 16:20:41 -07001953 TxbInfo txb_info = {
David Turnerb757ce02018-11-12 15:01:28 +00001954 qcoeff, levels, dqcoeff, tcoeff, dequant, shift, tx_size,
1955 txs_ctx, tx_type, bwl, width, height, eob, seg_eob,
1956 scan_order, txb_ctx, rdmult, iqmatrix, tx_type_cost,
Linfeng Zhang1015a342017-10-24 16:20:41 -07001957 };
1958
Yaowu Xu74e63352019-05-06 09:21:33 -07001959#if CONFIG_HTB_TRELLIS
Michelle Findlay-Olynykfbab0622017-12-13 14:10:56 -08001960 // Hash based trellis (hbt) speed feature: avoid expensive optimize_txb calls
Michelle Findlay-Olynykdea531d2017-12-13 14:10:56 -08001961 // by storing the coefficient deltas in a hash table.
Michelle Findlay-Olynykfbab0622017-12-13 14:10:56 -08001962 // Currently disabled in speedfeatures.c
Michelle Findlay-Olynykdea531d2017-12-13 14:10:56 -08001963 if (eob <= HBT_EOB && eob > 0 && cpi->sf.use_hash_based_trellis) {
Angie Chiange15d2e32018-03-02 15:49:06 -08001964 return hbt_create_hashes(&txb_info, txb_costs, &txb_eob_costs, p, block,
Michelle Findlay-Olynykdea531d2017-12-13 14:10:56 -08001965 fast_mode, rate_cost);
Michelle Findlay-Olynykfbab0622017-12-13 14:10:56 -08001966 }
Yaowu Xu74e63352019-05-06 09:21:33 -07001967#else
1968 (void)fast_mode;
1969#endif // CONFIG_HTB_TRELLIS
Linfeng Zhang1122d7d2017-10-31 15:30:28 -07001970 av1_txb_init_levels(qcoeff, width, height, levels);
Urvang Joshi70006e42017-06-14 16:08:55 -07001971
Angie Chiang45385b82018-03-02 15:25:58 -08001972 const int update =
Angie Chiange15d2e32018-03-02 15:49:06 -08001973 optimize_txb(&txb_info, txb_costs, &txb_eob_costs, rate_cost);
Angie Chiang07c57f32017-05-30 18:18:33 -07001974
Jingning Hand7e99112017-12-13 09:47:45 -08001975 if (update) {
1976 p->eobs[block] = txb_info.eob;
1977 p->txb_entropy_ctx[block] =
1978 av1_get_txb_entropy_context(qcoeff, scan_order, txb_info.eob);
1979 }
Angie Chiang07c57f32017-05-30 18:18:33 -07001980 return txb_info.eob;
1981}
Jingning Hand7e99112017-12-13 09:47:45 -08001982
Angie Chiang74e23072017-03-24 14:54:23 -07001983int av1_get_txb_entropy_context(const tran_low_t *qcoeff,
1984 const SCAN_ORDER *scan_order, int eob) {
Linfeng Zhangdb41d1e2017-12-05 11:06:20 -08001985 const int16_t *const scan = scan_order->scan;
Angie Chiang74e23072017-03-24 14:54:23 -07001986 int cul_level = 0;
1987 int c;
Jingning Han339cf932017-09-18 10:17:02 -07001988
1989 if (eob == 0) return 0;
Angie Chiang74e23072017-03-24 14:54:23 -07001990 for (c = 0; c < eob; ++c) {
1991 cul_level += abs(qcoeff[scan[c]]);
Jingning Hane3137652018-04-02 11:53:47 -07001992 if (cul_level > COEFF_CONTEXT_MASK) break;
Angie Chiang74e23072017-03-24 14:54:23 -07001993 }
1994
1995 cul_level = AOMMIN(COEFF_CONTEXT_MASK, cul_level);
1996 set_dc_sign(&cul_level, qcoeff[0]);
1997
1998 return cul_level;
1999}
2000
Hui Su6f981af2019-05-21 20:49:51 -07002001static void update_tx_type_count(const AV1_COMP *cpi, const AV1_COMMON *cm,
2002 MACROBLOCKD *xd, int blk_row, int blk_col,
2003 int plane, TX_SIZE tx_size,
2004 FRAME_COUNTS *counts,
Urvang Joshi553096a2018-05-10 15:27:14 -07002005 uint8_t allow_update_cdf) {
2006 MB_MODE_INFO *mbmi = xd->mi[0];
2007 int is_inter = is_inter_block(mbmi);
Urvang Joshib6409e92020-03-23 11:23:27 -07002008 const int reduced_tx_set_used = cm->features.reduced_tx_set_used;
Urvang Joshi553096a2018-05-10 15:27:14 -07002009 FRAME_CONTEXT *fc = xd->tile_ctx;
2010#if !CONFIG_ENTROPY_STATS
2011 (void)counts;
2012#endif // !CONFIG_ENTROPY_STATS
2013
2014 // Only y plane's tx_type is updated
2015 if (plane > 0) return;
Hui Su37980cc2019-10-09 10:16:35 -07002016 const TX_TYPE tx_type = av1_get_tx_type(xd, PLANE_TYPE_Y, blk_row, blk_col,
Yaowu Xu33fd0a02019-10-15 18:23:29 -07002017 tx_size, reduced_tx_set_used);
Hui Su6f981af2019-05-21 20:49:51 -07002018 if (is_inter) {
Vishesh28709f22020-06-02 14:46:24 +05302019 if (cpi->oxcf.txfm_cfg.use_inter_dct_only) {
Hui Su6f981af2019-05-21 20:49:51 -07002020 assert(tx_type == DCT_DCT);
2021 }
2022 } else {
Vishesh28709f22020-06-02 14:46:24 +05302023 if (cpi->oxcf.txfm_cfg.use_intra_dct_only) {
Hui Su6f981af2019-05-21 20:49:51 -07002024 assert(tx_type == DCT_DCT);
Vishesh28709f22020-06-02 14:46:24 +05302025 } else if (cpi->oxcf.txfm_cfg.use_intra_default_tx_only) {
Hui Su6f981af2019-05-21 20:49:51 -07002026 const TX_TYPE default_type = get_default_tx_type(
2027 PLANE_TYPE_Y, xd, tx_size, cpi->is_screen_content_type);
2028 (void)default_type;
2029 assert(tx_type == default_type);
2030 }
2031 }
2032
Yaowu Xu33fd0a02019-10-15 18:23:29 -07002033 if (get_ext_tx_types(tx_size, is_inter, reduced_tx_set_used) > 1 &&
chiyotsai8c004e12020-04-17 15:52:08 -07002034 cm->quant_params.base_qindex > 0 && !mbmi->skip_txfm &&
Urvang Joshi553096a2018-05-10 15:27:14 -07002035 !segfeature_active(&cm->seg, mbmi->segment_id, SEG_LVL_SKIP)) {
Yaowu Xu33fd0a02019-10-15 18:23:29 -07002036 const int eset = get_ext_tx_set(tx_size, is_inter, reduced_tx_set_used);
Urvang Joshi553096a2018-05-10 15:27:14 -07002037 if (eset > 0) {
2038 const TxSetType tx_set_type =
Yaowu Xu33fd0a02019-10-15 18:23:29 -07002039 av1_get_ext_tx_set_type(tx_size, is_inter, reduced_tx_set_used);
Urvang Joshi553096a2018-05-10 15:27:14 -07002040 if (is_inter) {
2041 if (allow_update_cdf) {
2042 update_cdf(fc->inter_ext_tx_cdf[eset][txsize_sqr_map[tx_size]],
2043 av1_ext_tx_ind[tx_set_type][tx_type],
2044 av1_num_ext_tx_set[tx_set_type]);
2045 }
2046#if CONFIG_ENTROPY_STATS
2047 ++counts->inter_ext_tx[eset][txsize_sqr_map[tx_size]]
2048 [av1_ext_tx_ind[tx_set_type][tx_type]];
2049#endif // CONFIG_ENTROPY_STATS
2050 } else {
2051 PREDICTION_MODE intra_dir;
2052 if (mbmi->filter_intra_mode_info.use_filter_intra)
2053 intra_dir = fimode_to_intradir[mbmi->filter_intra_mode_info
2054 .filter_intra_mode];
2055 else
2056 intra_dir = mbmi->mode;
2057#if CONFIG_ENTROPY_STATS
2058 ++counts->intra_ext_tx[eset][txsize_sqr_map[tx_size]][intra_dir]
2059 [av1_ext_tx_ind[tx_set_type][tx_type]];
2060#endif // CONFIG_ENTROPY_STATS
2061 if (allow_update_cdf) {
2062 update_cdf(
2063 fc->intra_ext_tx_cdf[eset][txsize_sqr_map[tx_size]][intra_dir],
2064 av1_ext_tx_ind[tx_set_type][tx_type],
2065 av1_num_ext_tx_set[tx_set_type]);
2066 }
2067 }
2068 }
2069 }
2070}
2071
Jingning Han4fe5f672017-05-19 15:46:07 -07002072void av1_update_and_record_txb_context(int plane, int block, int blk_row,
2073 int blk_col, BLOCK_SIZE plane_bsize,
2074 TX_SIZE tx_size, void *arg) {
Jingning Han6171ae72017-05-18 20:15:06 -07002075 struct tokenize_b_args *const args = arg;
Angie Chiang0397eda2017-03-15 16:57:14 -07002076 const AV1_COMP *cpi = args->cpi;
2077 const AV1_COMMON *cm = &cpi->common;
2078 ThreadData *const td = args->td;
2079 MACROBLOCK *const x = &td->mb;
2080 MACROBLOCKD *const xd = &x->e_mbd;
2081 struct macroblock_plane *p = &x->plane[plane];
2082 struct macroblockd_plane *pd = &xd->plane[plane];
Hui Su8763cd32018-04-02 12:29:05 -07002083 const int eob = p->eobs[block];
Yaowu Xu18a49962019-10-16 12:47:04 -07002084 const int block_offset = BLOCK_OFFSET(block);
Yaowu Xuf3dba2c2020-01-03 11:03:03 -08002085 tran_low_t *qcoeff = p->qcoeff + block_offset;
Hui Su8763cd32018-04-02 12:29:05 -07002086 const PLANE_TYPE plane_type = pd->plane_type;
Urvang Joshib6409e92020-03-23 11:23:27 -07002087 const TX_TYPE tx_type =
2088 av1_get_tx_type(xd, plane_type, blk_row, blk_col, tx_size,
2089 cm->features.reduced_tx_set_used);
Hui Su8763cd32018-04-02 12:29:05 -07002090 const SCAN_ORDER *const scan_order = get_scan(tx_size, tx_type);
Yaowu Xuf3dba2c2020-01-03 11:03:03 -08002091 tran_low_t *tcoeff;
2092 assert(args->dry_run != DRY_RUN_COSTCOEFFS);
2093 if (args->dry_run == OUTPUT_ENABLED) {
2094 MB_MODE_INFO *mbmi = xd->mi[0];
2095 TXB_CTX txb_ctx;
Urvang Joshi46ff5522020-03-30 15:27:37 -07002096 get_txb_ctx(plane_bsize, tx_size, plane,
2097 pd->above_entropy_context + blk_col,
2098 pd->left_entropy_context + blk_row, &txb_ctx);
Yaowu Xuf3dba2c2020-01-03 11:03:03 -08002099 const int bwl = get_txb_bwl(tx_size);
2100 const int width = get_txb_wide(tx_size);
2101 const int height = get_txb_high(tx_size);
2102 const uint8_t allow_update_cdf = args->allow_update_cdf;
2103 const TX_SIZE txsize_ctx = get_txsize_entropy_ctx(tx_size);
2104 FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
2105#if CONFIG_ENTROPY_STATS
2106 int cdf_idx = cm->coef_cdf_category;
2107 ++td->counts->txb_skip[cdf_idx][txsize_ctx][txb_ctx.txb_skip_ctx][eob == 0];
2108#endif // CONFIG_ENTROPY_STATS
2109 if (allow_update_cdf) {
2110 update_cdf(ec_ctx->txb_skip_cdf[txsize_ctx][txb_ctx.txb_skip_ctx],
2111 eob == 0, 2);
2112 }
Yunqing Wangdb70bf42019-08-19 09:28:11 -07002113
Yaowu Xuf3dba2c2020-01-03 11:03:03 -08002114 CB_COEFF_BUFFER *cb_coef_buff = x->cb_coef_buff;
2115 const int txb_offset =
2116 x->mbmi_ext_frame->cb_offset / (TX_SIZE_W_MIN * TX_SIZE_H_MIN);
2117 uint16_t *eob_txb = cb_coef_buff->eobs[plane] + txb_offset;
2118 uint8_t *const entropy_ctx = cb_coef_buff->entropy_ctx[plane] + txb_offset;
2119 entropy_ctx[block] = txb_ctx.txb_skip_ctx;
2120 eob_txb[block] = eob;
2121
2122 if (eob == 0) {
Urvang Joshi46ff5522020-03-30 15:27:37 -07002123 av1_set_entropy_contexts(xd, pd, plane, plane_bsize, tx_size, 0, blk_col,
2124 blk_row);
Yaowu Xuf3dba2c2020-01-03 11:03:03 -08002125 return;
2126 }
2127 const int segment_id = mbmi->segment_id;
2128 const int seg_eob = av1_get_tx_eob(&cpi->common.seg, segment_id, tx_size);
2129 tran_low_t *tcoeff_txb =
2130 cb_coef_buff->tcoeff[plane] + x->mbmi_ext_frame->cb_offset;
2131 tcoeff = tcoeff_txb + block_offset;
2132 memcpy(tcoeff, qcoeff, sizeof(*tcoeff) * seg_eob);
2133
2134 uint8_t levels_buf[TX_PAD_2D];
2135 uint8_t *const levels = set_levels(levels_buf, width);
2136 av1_txb_init_levels(tcoeff, width, height, levels);
2137 update_tx_type_count(cpi, cm, xd, blk_row, blk_col, plane, tx_size,
2138 td->counts, allow_update_cdf);
2139
2140 const TX_CLASS tx_class = tx_type_to_class[tx_type];
2141 const int16_t *const scan = scan_order->scan;
2142
2143 // record tx type usage
2144 td->rd_counts.tx_type_used[tx_size][tx_type]++;
Yunqing Wangdb70bf42019-08-19 09:28:11 -07002145
Yue Chen198738d2018-03-08 17:26:01 -08002146#if CONFIG_ENTROPY_STATS
Yaowu Xuf3dba2c2020-01-03 11:03:03 -08002147 av1_update_eob_context(cdf_idx, eob, tx_size, tx_class, plane_type, ec_ctx,
2148 td->counts, allow_update_cdf);
Yue Chen198738d2018-03-08 17:26:01 -08002149#else
Yaowu Xuf3dba2c2020-01-03 11:03:03 -08002150 av1_update_eob_context(eob, tx_size, tx_class, plane_type, ec_ctx,
2151 allow_update_cdf);
Yue Chen198738d2018-03-08 17:26:01 -08002152#endif
Hui Su8763cd32018-04-02 12:29:05 -07002153
Yaowu Xuf3dba2c2020-01-03 11:03:03 -08002154 DECLARE_ALIGNED(16, int8_t, coeff_contexts[MAX_TX_SQUARE]);
2155 av1_get_nz_map_contexts(levels, scan, eob, tx_size, tx_class,
2156 coeff_contexts);
Linfeng Zhangd67c13f2017-12-11 11:49:12 -08002157
Yaowu Xuf3dba2c2020-01-03 11:03:03 -08002158 for (int c = eob - 1; c >= 0; --c) {
2159 const int pos = scan[c];
2160 const int coeff_ctx = coeff_contexts[pos];
2161 const tran_low_t v = qcoeff[pos];
2162 const tran_low_t level = abs(v);
Dake He03a32922017-10-31 08:06:45 -07002163
Yaowu Xuf3dba2c2020-01-03 11:03:03 -08002164 if (allow_update_cdf) {
2165 if (c == eob - 1) {
2166 assert(coeff_ctx < 4);
2167 update_cdf(
2168 ec_ctx->coeff_base_eob_cdf[txsize_ctx][plane_type][coeff_ctx],
2169 AOMMIN(level, 3) - 1, 3);
2170 } else {
2171 update_cdf(ec_ctx->coeff_base_cdf[txsize_ctx][plane_type][coeff_ctx],
2172 AOMMIN(level, 3), 4);
2173 }
Dake He4d447692017-12-15 09:10:06 -08002174 }
Dake He4d447692017-12-15 09:10:06 -08002175 if (c == eob - 1) {
2176 assert(coeff_ctx < 4);
Yue Chen198738d2018-03-08 17:26:01 -08002177#if CONFIG_ENTROPY_STATS
2178 ++td->counts->coeff_base_eob_multi[cdf_idx][txsize_ctx][plane_type]
2179 [coeff_ctx][AOMMIN(level, 3) - 1];
Dake He4d447692017-12-15 09:10:06 -08002180 } else {
Yue Chen198738d2018-03-08 17:26:01 -08002181 ++td->counts->coeff_base_multi[cdf_idx][txsize_ctx][plane_type]
2182 [coeff_ctx][AOMMIN(level, 3)];
2183#endif
Angie Chiang2cc5eb32018-02-14 18:37:22 -08002184 }
Yaowu Xuf3dba2c2020-01-03 11:03:03 -08002185 if (level > NUM_BASE_LEVELS) {
2186 const int base_range = level - 1 - NUM_BASE_LEVELS;
2187 const int br_ctx = get_br_ctx(levels, pos, bwl, tx_class);
2188 for (int idx = 0; idx < COEFF_BASE_RANGE; idx += BR_CDF_SIZE - 1) {
2189 const int k = AOMMIN(base_range - idx, BR_CDF_SIZE - 1);
2190 if (allow_update_cdf) {
2191 update_cdf(ec_ctx->coeff_br_cdf[AOMMIN(txsize_ctx, TX_32X32)]
2192 [plane_type][br_ctx],
2193 k, BR_CDF_SIZE);
2194 }
2195 for (int lps = 0; lps < BR_CDF_SIZE - 1; lps++) {
Angie Chiang2cc5eb32018-02-14 18:37:22 -08002196#if CONFIG_ENTROPY_STATS
Yaowu Xuf3dba2c2020-01-03 11:03:03 -08002197 ++td->counts->coeff_lps[AOMMIN(txsize_ctx, TX_32X32)][plane_type]
2198 [lps][br_ctx][lps == k];
Angie Chiang2cc5eb32018-02-14 18:37:22 -08002199#endif // CONFIG_ENTROPY_STATS
Yaowu Xuf3dba2c2020-01-03 11:03:03 -08002200 if (lps == k) break;
2201 }
Yue Chen198738d2018-03-08 17:26:01 -08002202#if CONFIG_ENTROPY_STATS
Yaowu Xuf3dba2c2020-01-03 11:03:03 -08002203 ++td->counts->coeff_lps_multi[cdf_idx][AOMMIN(txsize_ctx, TX_32X32)]
2204 [plane_type][br_ctx][k];
Yue Chen198738d2018-03-08 17:26:01 -08002205#endif
Yaowu Xuf3dba2c2020-01-03 11:03:03 -08002206 if (k < BR_CDF_SIZE - 1) break;
2207 }
Dake He59881772017-11-24 07:00:02 -08002208 }
2209 }
Yaowu Xuf3dba2c2020-01-03 11:03:03 -08002210 // Update the context needed to code the DC sign (if applicable)
2211 if (tcoeff[0] != 0) {
2212 const int dc_sign = (tcoeff[0] < 0) ? 1 : 0;
2213 const int dc_sign_ctx = txb_ctx.dc_sign_ctx;
Hui Su1e959892018-01-22 12:14:43 -08002214#if CONFIG_ENTROPY_STATS
Yaowu Xuf3dba2c2020-01-03 11:03:03 -08002215 ++td->counts->dc_sign[plane_type][dc_sign_ctx][dc_sign];
Hui Su1e959892018-01-22 12:14:43 -08002216#endif // CONFIG_ENTROPY_STATS
Yaowu Xuf3dba2c2020-01-03 11:03:03 -08002217 if (allow_update_cdf)
2218 update_cdf(ec_ctx->dc_sign_cdf[plane_type][dc_sign_ctx], dc_sign, 2);
2219 entropy_ctx[block] |= dc_sign_ctx << DC_SIGN_CTX_SHIFT;
2220 }
2221 } else {
2222 tcoeff = qcoeff;
Dake He43edb762017-10-26 10:29:46 -07002223 }
Hui Su8763cd32018-04-02 12:29:05 -07002224 const int cul_level = av1_get_txb_entropy_context(tcoeff, scan_order, eob);
Urvang Joshi46ff5522020-03-30 15:27:37 -07002225 av1_set_entropy_contexts(xd, pd, plane, plane_bsize, tx_size, cul_level,
2226 blk_col, blk_row);
Angie Chiang0397eda2017-03-15 16:57:14 -07002227}
2228
2229void av1_update_txb_context(const AV1_COMP *cpi, ThreadData *td,
Hui Su95e9c0c2019-12-09 15:21:35 -08002230 RUN_TYPE dry_run, BLOCK_SIZE bsize,
2231 uint8_t allow_update_cdf) {
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00002232 const AV1_COMMON *const cm = &cpi->common;
2233 const int num_planes = av1_num_planes(cm);
Angie Chiang0397eda2017-03-15 16:57:14 -07002234 MACROBLOCK *const x = &td->mb;
2235 MACROBLOCKD *const xd = &x->e_mbd;
Yue Chen53b53f02018-03-29 14:31:23 -07002236 MB_MODE_INFO *const mbmi = xd->mi[0];
Yaowu Xuf3dba2c2020-01-03 11:03:03 -08002237 struct tokenize_b_args arg = { cpi, td, 0, allow_update_cdf, dry_run };
chiyotsai8c004e12020-04-17 15:52:08 -07002238 if (mbmi->skip_txfm) {
Urvang Joshi46ff5522020-03-30 15:27:37 -07002239 av1_reset_entropy_context(xd, bsize, num_planes);
Angie Chiang0397eda2017-03-15 16:57:14 -07002240 return;
2241 }
Yaowu Xu88039f02020-01-03 12:09:18 -08002242
2243 for (int plane = 0; plane < num_planes; ++plane) {
Hui Su474e1e12020-02-27 15:46:36 -08002244 if (plane && !xd->is_chroma_ref) break;
Yaowu Xu88039f02020-01-03 12:09:18 -08002245 const struct macroblockd_plane *const pd = &xd->plane[plane];
2246 const int ss_x = pd->subsampling_x;
2247 const int ss_y = pd->subsampling_y;
Yaowu Xu88039f02020-01-03 12:09:18 -08002248 const BLOCK_SIZE plane_bsize = get_plane_block_size(bsize, ss_x, ss_y);
2249 av1_foreach_transformed_block_in_plane(
2250 xd, plane_bsize, plane, av1_update_and_record_txb_context, &arg);
2251 }
Angie Chiang0397eda2017-03-15 16:57:14 -07002252}
Hui Suf1b11062019-04-17 14:18:26 -07002253
2254CB_COEFF_BUFFER *av1_get_cb_coeff_buffer(const struct AV1_COMP *cpi, int mi_row,
2255 int mi_col) {
2256 const AV1_COMMON *const cm = &cpi->common;
2257 const int mib_size_log2 = cm->seq_params.mib_size_log2;
Urvang Joshi9dc909d2020-03-23 16:07:02 -07002258 const int stride = (cm->mi_params.mi_cols >> mib_size_log2) + 1;
Hui Suf1b11062019-04-17 14:18:26 -07002259 const int offset =
2260 (mi_row >> mib_size_log2) * stride + (mi_col >> mib_size_log2);
2261 return cpi->coeff_buffer_base + offset;
2262}