blob: a3b8a6fc735f88840f9ac36853a0f8f3ee9aaa21 [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
Michelle Findlay-Olynykdea531d2017-12-13 14:10:56 -080026static int hbt_needs_init = 1;
Peng Bin8a204cd2018-04-08 13:07:35 +080027static CRC32C crc_calculator;
Michelle Findlay-Olynykdea531d2017-12-13 14:10:56 -080028static const int HBT_EOB = 16; // also the length in opt_qcoeff
29static const int HBT_TABLE_SIZE = 65536; // 16 bit: holds 65536 'arrays'
30static const int HBT_ARRAY_LENGTH = 256; // 8 bit: 256 entries
31// If removed in hbt_create_hashes or increased beyond int8_t, widen deltas type
32static const int HBT_KICKOUT = 3;
Michelle Findlay-Olynykfbab0622017-12-13 14:10:56 -080033
34typedef struct OptTxbQcoeff {
Michelle Findlay-Olynykdea531d2017-12-13 14:10:56 -080035 // Use larger type if larger/no kickout value is used in hbt_create_hashes
36 int8_t deltas[16];
37 uint32_t hbt_qc_hash;
38 uint32_t hbt_ctx_hash;
39 int init;
40 int rate_cost;
Michelle Findlay-Olynykfbab0622017-12-13 14:10:56 -080041} OptTxbQcoeff;
42
Michelle Findlay-Olynykdea531d2017-12-13 14:10:56 -080043OptTxbQcoeff *hbt_hash_table;
Michelle Findlay-Olynykfbab0622017-12-13 14:10:56 -080044
Dake Hea47cd6c2017-10-13 18:09:58 -070045typedef struct LevelDownStats {
46 int update;
47 tran_low_t low_qc;
48 tran_low_t low_dqc;
Dake Hea47cd6c2017-10-13 18:09:58 -070049 int64_t dist0;
50 int rate;
51 int rate_low;
52 int64_t dist;
53 int64_t dist_low;
54 int64_t rd;
55 int64_t rd_low;
Dake He4d447692017-12-15 09:10:06 -080056 int64_t nz_rd;
Dake Hea47cd6c2017-10-13 18:09:58 -070057 int64_t rd_diff;
58 int cost_diff;
59 int64_t dist_diff;
60 int new_eob;
61} LevelDownStats;
62
Angie Chiangf0fbf9d2017-03-15 15:01:22 -070063void av1_alloc_txb_buf(AV1_COMP *cpi) {
Jingning Hanf5a4d3b2017-08-27 23:01:19 -070064 AV1_COMMON *cm = &cpi->common;
Imdad Sardharwalla4ec84ab2018-02-06 12:20:18 +000065 int size = ((cm->mi_rows >> cm->seq_params.mib_size_log2) + 1) *
66 ((cm->mi_cols >> cm->seq_params.mib_size_log2) + 1);
Jingning Hanf5a4d3b2017-08-27 23:01:19 -070067
Angie Chiang9367e3e2017-10-02 16:28:11 -070068 av1_free_txb_buf(cpi);
Jingning Hanf5a4d3b2017-08-27 23:01:19 -070069 // TODO(jingning): This should be further reduced.
70 CHECK_MEM_ERROR(cm, cpi->coeff_buffer_base,
Peng Bin27d7ca92018-03-22 22:25:56 +080071 aom_memalign(32, sizeof(*cpi->coeff_buffer_base) * size));
Angie Chiangf0fbf9d2017-03-15 15:01:22 -070072}
73
Angie Chianged9667e2018-03-02 15:10:50 -080074void av1_free_txb_buf(AV1_COMP *cpi) { aom_free(cpi->coeff_buffer_base); }
Angie Chiangf0fbf9d2017-03-15 15:01:22 -070075
Jingning Hanf5a4d3b2017-08-27 23:01:19 -070076void av1_set_coeff_buffer(const AV1_COMP *const cpi, MACROBLOCK *const x,
77 int mi_row, int mi_col) {
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +000078 const AV1_COMMON *const cm = &cpi->common;
79 const int num_planes = av1_num_planes(cm);
Imdad Sardharwalla4ec84ab2018-02-06 12:20:18 +000080 int mib_size_log2 = cm->seq_params.mib_size_log2;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +000081 int stride = (cm->mi_cols >> mib_size_log2) + 1;
Dominic Symes917d6c02017-10-11 18:00:52 +020082 int offset = (mi_row >> mib_size_log2) * stride + (mi_col >> mib_size_log2);
Jingning Hanf5a4d3b2017-08-27 23:01:19 -070083 CB_COEFF_BUFFER *coeff_buf = &cpi->coeff_buffer_base[offset];
84 const int txb_offset = x->cb_offset / (TX_SIZE_W_MIN * TX_SIZE_H_MIN);
Hui Suad01a472018-03-01 10:46:34 -080085 assert(x->cb_offset < (1 << num_pels_log2_lookup[cm->seq_params.sb_size]));
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +000086 for (int plane = 0; plane < num_planes; ++plane) {
Jingning Hanf5a4d3b2017-08-27 23:01:19 -070087 x->mbmi_ext->tcoeff[plane] = coeff_buf->tcoeff[plane] + x->cb_offset;
88 x->mbmi_ext->eobs[plane] = coeff_buf->eobs[plane] + txb_offset;
89 x->mbmi_ext->txb_skip_ctx[plane] =
90 coeff_buf->txb_skip_ctx[plane] + txb_offset;
91 x->mbmi_ext->dc_sign_ctx[plane] =
92 coeff_buf->dc_sign_ctx[plane] + txb_offset;
93 }
94}
95
Angie Chiang80b82262017-02-24 11:39:47 -080096static void write_golomb(aom_writer *w, int level) {
97 int x = level + 1;
98 int i = x;
99 int length = 0;
100
101 while (i) {
102 i >>= 1;
103 ++length;
104 }
105 assert(length > 0);
106
107 for (i = 0; i < length - 1; ++i) aom_write_bit(w, 0);
108
109 for (i = length - 1; i >= 0; --i) aom_write_bit(w, (x >> i) & 0x01);
110}
111
Dake Hea47cd6c2017-10-13 18:09:58 -0700112static INLINE tran_low_t get_lower_coeff(tran_low_t qc) {
113 if (qc == 0) {
114 return 0;
115 }
116 return qc > 0 ? qc - 1 : qc + 1;
117}
118
Angie Chiangb3167a62018-01-30 19:37:57 -0800119static INLINE tran_low_t qcoeff_to_dqcoeff(tran_low_t qc, int coeff_idx,
Angie Chiangb3167a62018-01-30 19:37:57 -0800120 int dqv, int shift,
121 const qm_val_t *iqmatrix) {
Angie Chiang40b07312018-03-30 10:42:55 -0700122 int sign = qc < 0 ? -1 : 1;
Angie Chiangb3167a62018-01-30 19:37:57 -0800123 if (iqmatrix != NULL)
124 dqv =
125 ((iqmatrix[coeff_idx] * dqv) + (1 << (AOM_QM_BITS - 1))) >> AOM_QM_BITS;
Angie Chiang40b07312018-03-30 10:42:55 -0700126 return sign * ((abs(qc) * dqv) >> shift);
Dake Hea47cd6c2017-10-13 18:09:58 -0700127}
128
129static INLINE int64_t get_coeff_dist(tran_low_t tcoeff, tran_low_t dqcoeff,
130 int shift) {
131 const int64_t diff = (tcoeff - dqcoeff) * (1 << shift);
132 const int64_t error = diff * diff;
133 return error;
134}
135
Yue Chen198738d2018-03-08 17:26:01 -0800136#if CONFIG_ENTROPY_STATS
Yaowu Xu49abec82018-03-13 16:09:01 -0700137void av1_update_eob_context(int cdf_idx, int eob, TX_SIZE tx_size,
Katsuhisa Yuasa55c95f82018-04-03 00:51:22 +0900138 TX_CLASS tx_class, PLANE_TYPE plane,
Yunqing Wang0e141b52017-11-02 15:08:58 -0700139 FRAME_CONTEXT *ec_ctx, FRAME_COUNTS *counts,
140 uint8_t allow_update_cdf) {
Yue Chen198738d2018-03-08 17:26:01 -0800141#else
Katsuhisa Yuasa55c95f82018-04-03 00:51:22 +0900142void av1_update_eob_context(int eob, TX_SIZE tx_size, TX_CLASS tx_class,
Yaowu Xu49abec82018-03-13 16:09:01 -0700143 PLANE_TYPE plane, FRAME_CONTEXT *ec_ctx,
144 uint8_t allow_update_cdf) {
Yue Chen198738d2018-03-08 17:26:01 -0800145#endif
Yaowu Xu49abec82018-03-13 16:09:01 -0700146 int eob_extra;
Linfeng Zhang0c72b2f2017-12-04 10:59:28 -0800147 const int eob_pt = get_eob_pos_token(eob, &eob_extra);
Debargha Mukherjeeb3eda2f2017-11-28 16:00:20 -0800148 TX_SIZE txs_ctx = get_txsize_entropy_ctx(tx_size);
Dake Hea47cd6c2017-10-13 18:09:58 -0700149
Dake He0db7d0e2017-12-21 15:23:20 -0800150 const int eob_multi_size = txsize_log2_minus4[tx_size];
Katsuhisa Yuasa55c95f82018-04-03 00:51:22 +0900151 const int eob_multi_ctx = (tx_class == TX_CLASS_2D) ? 0 : 1;
Dake He0db7d0e2017-12-21 15:23:20 -0800152
153 switch (eob_multi_size) {
154 case 0:
Yue Chen198738d2018-03-08 17:26:01 -0800155#if CONFIG_ENTROPY_STATS
156 ++counts->eob_multi16[cdf_idx][plane][eob_multi_ctx][eob_pt - 1];
157#endif
Dake He0db7d0e2017-12-21 15:23:20 -0800158 if (allow_update_cdf)
159 update_cdf(ec_ctx->eob_flag_cdf16[plane][eob_multi_ctx], eob_pt - 1, 5);
160 break;
161 case 1:
Yue Chen198738d2018-03-08 17:26:01 -0800162#if CONFIG_ENTROPY_STATS
163 ++counts->eob_multi32[cdf_idx][plane][eob_multi_ctx][eob_pt - 1];
164#endif
Dake He0db7d0e2017-12-21 15:23:20 -0800165 if (allow_update_cdf)
166 update_cdf(ec_ctx->eob_flag_cdf32[plane][eob_multi_ctx], eob_pt - 1, 6);
167 break;
168 case 2:
Yue Chen198738d2018-03-08 17:26:01 -0800169#if CONFIG_ENTROPY_STATS
170 ++counts->eob_multi64[cdf_idx][plane][eob_multi_ctx][eob_pt - 1];
171#endif
Dake He0db7d0e2017-12-21 15:23:20 -0800172 if (allow_update_cdf)
173 update_cdf(ec_ctx->eob_flag_cdf64[plane][eob_multi_ctx], eob_pt - 1, 7);
174 break;
175 case 3:
Yue Chen198738d2018-03-08 17:26:01 -0800176#if CONFIG_ENTROPY_STATS
177 ++counts->eob_multi128[cdf_idx][plane][eob_multi_ctx][eob_pt - 1];
178#endif
Hui Su1cb1c002018-02-05 18:21:20 -0800179 if (allow_update_cdf) {
Dake He0db7d0e2017-12-21 15:23:20 -0800180 update_cdf(ec_ctx->eob_flag_cdf128[plane][eob_multi_ctx], eob_pt - 1,
181 8);
Hui Su1cb1c002018-02-05 18:21:20 -0800182 }
Dake He0db7d0e2017-12-21 15:23:20 -0800183 break;
184 case 4:
Yue Chen198738d2018-03-08 17:26:01 -0800185#if CONFIG_ENTROPY_STATS
186 ++counts->eob_multi256[cdf_idx][plane][eob_multi_ctx][eob_pt - 1];
187#endif
Hui Su1cb1c002018-02-05 18:21:20 -0800188 if (allow_update_cdf) {
Dake He0db7d0e2017-12-21 15:23:20 -0800189 update_cdf(ec_ctx->eob_flag_cdf256[plane][eob_multi_ctx], eob_pt - 1,
190 9);
Hui Su1cb1c002018-02-05 18:21:20 -0800191 }
Dake He0db7d0e2017-12-21 15:23:20 -0800192 break;
193 case 5:
Yue Chen198738d2018-03-08 17:26:01 -0800194#if CONFIG_ENTROPY_STATS
195 ++counts->eob_multi512[cdf_idx][plane][eob_multi_ctx][eob_pt - 1];
196#endif
Hui Su1cb1c002018-02-05 18:21:20 -0800197 if (allow_update_cdf) {
Dake He0db7d0e2017-12-21 15:23:20 -0800198 update_cdf(ec_ctx->eob_flag_cdf512[plane][eob_multi_ctx], eob_pt - 1,
199 10);
Hui Su1cb1c002018-02-05 18:21:20 -0800200 }
Dake He0db7d0e2017-12-21 15:23:20 -0800201 break;
202 case 6:
203 default:
Yue Chen198738d2018-03-08 17:26:01 -0800204#if CONFIG_ENTROPY_STATS
205 ++counts->eob_multi1024[cdf_idx][plane][eob_multi_ctx][eob_pt - 1];
206#endif
Hui Su1cb1c002018-02-05 18:21:20 -0800207 if (allow_update_cdf) {
Dake He0db7d0e2017-12-21 15:23:20 -0800208 update_cdf(ec_ctx->eob_flag_cdf1024[plane][eob_multi_ctx], eob_pt - 1,
209 11);
Hui Su1cb1c002018-02-05 18:21:20 -0800210 }
Dake He0db7d0e2017-12-21 15:23:20 -0800211 break;
212 }
Jingning Han00803a72017-10-25 16:04:34 -0700213
Angie Chiang7ab884e2017-10-18 15:57:12 -0700214 if (k_eob_offset_bits[eob_pt] > 0) {
Jingning Hanff4a9f82018-06-08 10:48:45 -0700215 int eob_ctx = eob_pt - 3;
Angie Chiang7ab884e2017-10-18 15:57:12 -0700216 int eob_shift = k_eob_offset_bits[eob_pt] - 1;
217 int bit = (eob_extra & (1 << eob_shift)) ? 1 : 0;
Hui Su1e959892018-01-22 12:14:43 -0800218#if CONFIG_ENTROPY_STATS
Yue Chen198738d2018-03-08 17:26:01 -0800219 counts->eob_extra[cdf_idx][txs_ctx][plane][eob_pt][bit]++;
Hui Su1e959892018-01-22 12:14:43 -0800220#endif // CONFIG_ENTROPY_STATS
Yunqing Wang0e141b52017-11-02 15:08:58 -0700221 if (allow_update_cdf)
Jingning Hanff4a9f82018-06-08 10:48:45 -0700222 update_cdf(ec_ctx->eob_extra_cdf[txs_ctx][plane][eob_ctx], bit, 2);
Angie Chiang7ab884e2017-10-18 15:57:12 -0700223 }
Dake Hea47cd6c2017-10-13 18:09:58 -0700224}
225
Yaowu Xu49abec82018-03-13 16:09:01 -0700226static int get_eob_cost(int eob, const LV_MAP_EOB_COST *txb_eob_costs,
Katsuhisa Yuasa55c95f82018-04-03 00:51:22 +0900227 const LV_MAP_COEFF_COST *txb_costs, TX_CLASS tx_class) {
Yaowu Xu49abec82018-03-13 16:09:01 -0700228 int eob_extra;
Linfeng Zhang0c72b2f2017-12-04 10:59:28 -0800229 const int eob_pt = get_eob_pos_token(eob, &eob_extra);
Dake Hea47cd6c2017-10-13 18:09:58 -0700230 int eob_cost = 0;
Katsuhisa Yuasa55c95f82018-04-03 00:51:22 +0900231 const int eob_multi_ctx = (tx_class == TX_CLASS_2D) ? 0 : 1;
Dake He0db7d0e2017-12-21 15:23:20 -0800232 eob_cost = txb_eob_costs->eob_cost[eob_multi_ctx][eob_pt - 1];
Dake He0db7d0e2017-12-21 15:23:20 -0800233
Dake Hea47cd6c2017-10-13 18:09:58 -0700234 if (k_eob_offset_bits[eob_pt] > 0) {
Jingning Hanff4a9f82018-06-08 10:48:45 -0700235 const int eob_ctx = eob_pt - 3;
Hui Su751a2332018-01-23 11:35:03 -0800236 const int eob_shift = k_eob_offset_bits[eob_pt] - 1;
237 const int bit = (eob_extra & (1 << eob_shift)) ? 1 : 0;
Jingning Hanff4a9f82018-06-08 10:48:45 -0700238 eob_cost += txb_costs->eob_extra_cost[eob_ctx][bit];
Hui Su751a2332018-01-23 11:35:03 -0800239 const int offset_bits = k_eob_offset_bits[eob_pt];
240 if (offset_bits > 1) eob_cost += av1_cost_literal(offset_bits - 1);
Dake Hea47cd6c2017-10-13 18:09:58 -0700241 }
242 return eob_cost;
243}
244
Angie Chiange4ea7482018-03-15 11:36:41 -0700245static INLINE int get_sign_bit_cost(tran_low_t qc, int coeff_idx,
246 const int (*dc_sign_cost)[2],
247 int dc_sign_ctx) {
Angie Chiange4ea7482018-03-15 11:36:41 -0700248 if (coeff_idx == 0) {
Angie Chiang2f94a452018-04-03 20:21:06 -0700249 const int sign = (qc < 0) ? 1 : 0;
Angie Chiange4ea7482018-03-15 11:36:41 -0700250 return dc_sign_cost[dc_sign_ctx][sign];
Angie Chiange4ea7482018-03-15 11:36:41 -0700251 }
Angie Chiang40b07312018-03-30 10:42:55 -0700252 return av1_cost_literal(1);
Angie Chiange4ea7482018-03-15 11:36:41 -0700253}
254
255static INLINE int get_br_cost(tran_low_t abs_qc, int ctx,
256 const int *coeff_lps) {
257 const tran_low_t min_level = 1 + NUM_BASE_LEVELS;
258 const tran_low_t max_level = 1 + NUM_BASE_LEVELS + COEFF_BASE_RANGE;
259 (void)ctx;
260 if (abs_qc >= min_level) {
Angie Chiang40b07312018-03-30 10:42:55 -0700261 if (abs_qc >= max_level) {
Angie Chiange4ea7482018-03-15 11:36:41 -0700262 return coeff_lps[COEFF_BASE_RANGE]; // COEFF_BASE_RANGE * cost0;
Angie Chiang40b07312018-03-30 10:42:55 -0700263 } else {
Angie Chiange4ea7482018-03-15 11:36:41 -0700264 return coeff_lps[(abs_qc - min_level)]; // * cost0 + cost1;
Angie Chiang40b07312018-03-30 10:42:55 -0700265 }
Angie Chiange4ea7482018-03-15 11:36:41 -0700266 }
Angie Chiang40b07312018-03-30 10:42:55 -0700267 return 0;
Angie Chiange4ea7482018-03-15 11:36:41 -0700268}
269
270static INLINE int get_golomb_cost(int abs_qc) {
271 if (abs_qc >= 1 + NUM_BASE_LEVELS + COEFF_BASE_RANGE) {
272 const int r = abs_qc - COEFF_BASE_RANGE - NUM_BASE_LEVELS;
273 const int length = get_msb(r) + 1;
274 return av1_cost_literal(2 * length - 1);
275 }
276 return 0;
277}
278
Linfeng Zhang1015a342017-10-24 16:20:41 -0700279static int get_coeff_cost(const tran_low_t qc, const int scan_idx,
Sebastien Alaiwan78f7bb92018-01-11 11:02:43 +0100280 const int is_eob, const TxbInfo *const txb_info,
Linfeng Zhang5f1b8ce2017-12-11 15:53:10 -0800281 const LV_MAP_COEFF_COST *const txb_costs,
Katsuhisa Yuasa55c95f82018-04-03 00:51:22 +0900282 const int coeff_ctx, const TX_CLASS tx_class) {
Xing Jinbd91e942018-06-21 13:43:17 +0800283 const TXB_CTX *const txb_ctx = txb_info->txb_ctx;
Angie Chiange4ea7482018-03-15 11:36:41 -0700284 const int is_nz = (qc != 0);
285 const tran_low_t abs_qc = abs(qc);
286 int cost = 0;
287 const int16_t *const scan = txb_info->scan_order->scan;
288 const int pos = scan[scan_idx];
289
290 if (is_eob) {
291 cost += txb_costs->base_eob_cost[coeff_ctx][AOMMIN(abs_qc, 3) - 1];
292 } else {
293 cost += txb_costs->base_cost[coeff_ctx][AOMMIN(abs_qc, 3)];
294 }
295 if (is_nz) {
296 cost += get_sign_bit_cost(qc, scan_idx, txb_costs->dc_sign_cost,
297 txb_ctx->dc_sign_ctx);
298
299 if (abs_qc > NUM_BASE_LEVELS) {
300 const int ctx =
Katsuhisa Yuasa55c95f82018-04-03 00:51:22 +0900301 get_br_ctx(txb_info->levels, pos, txb_info->bwl, tx_class);
Angie Chiange4ea7482018-03-15 11:36:41 -0700302 cost += get_br_cost(abs_qc, ctx, txb_costs->lps_cost[ctx]);
303 cost += get_golomb_cost(abs_qc);
304 }
305 }
306 return cost;
307}
Dake Hea47cd6c2017-10-13 18:09:58 -0700308
Frederic Barbiere8c67522018-03-27 15:09:57 +0200309static INLINE int get_nz_map_ctx(const uint8_t *const levels,
310 const int coeff_idx, const int bwl,
311 const int height, const int scan_idx,
312 const int is_eob, const TX_SIZE tx_size,
Katsuhisa Yuasa55c95f82018-04-03 00:51:22 +0900313 const TX_CLASS tx_class) {
Frederic Barbiere8c67522018-03-27 15:09:57 +0200314 if (is_eob) {
315 if (scan_idx == 0) return 0;
316 if (scan_idx <= (height << bwl) / 8) return 1;
317 if (scan_idx <= (height << bwl) / 4) return 2;
318 return 3;
319 }
Frederic Barbiere8c67522018-03-27 15:09:57 +0200320 const int stats =
321 get_nz_mag(levels + get_padded_idx(coeff_idx, bwl), bwl, tx_class);
322 return get_nz_map_ctx_from_stats(stats, coeff_idx, bwl, tx_size, tx_class);
323}
324
Linfeng Zhangdb41d1e2017-12-05 11:06:20 -0800325static void get_dist_cost_stats(LevelDownStats *const stats, const int scan_idx,
Ola Hugosson13892102017-11-06 08:01:44 +0100326 const int is_eob,
Linfeng Zhangdb41d1e2017-12-05 11:06:20 -0800327 const LV_MAP_COEFF_COST *const txb_costs,
Katsuhisa Yuasa55c95f82018-04-03 00:51:22 +0900328 const TxbInfo *const txb_info,
329 const TX_CLASS tx_class) {
Linfeng Zhangdb41d1e2017-12-05 11:06:20 -0800330 const int16_t *const scan = txb_info->scan_order->scan;
Dake Hea47cd6c2017-10-13 18:09:58 -0700331 const int coeff_idx = scan[scan_idx];
332 const tran_low_t qc = txb_info->qcoeff[coeff_idx];
Linfeng Zhang1015a342017-10-24 16:20:41 -0700333 const uint8_t *const levels = txb_info->levels;
Dake Hea47cd6c2017-10-13 18:09:58 -0700334 stats->new_eob = -1;
335 stats->update = 0;
Debargha Mukherjeee2f6b162018-01-04 17:23:05 -0800336 stats->rd_low = 0;
337 stats->rd = 0;
Michelle Findlay-Olynykfbab0622017-12-13 14:10:56 -0800338 stats->nz_rd = 0;
Michelle Findlay-Olynykfbab0622017-12-13 14:10:56 -0800339 stats->dist_low = 0;
340 stats->rate_low = 0;
341 stats->low_qc = 0;
Dake Hea47cd6c2017-10-13 18:09:58 -0700342
343 const tran_low_t tqc = txb_info->tcoeff[coeff_idx];
344 const int dqv = txb_info->dequant[coeff_idx != 0];
Sebastien Alaiwan78f7bb92018-01-11 11:02:43 +0100345 const int coeff_ctx =
346 get_nz_map_ctx(levels, coeff_idx, txb_info->bwl, txb_info->height,
Katsuhisa Yuasa55c95f82018-04-03 00:51:22 +0900347 scan_idx, is_eob, txb_info->tx_size, tx_class);
348 const int qc_cost = get_coeff_cost(qc, scan_idx, is_eob, txb_info, txb_costs,
349 coeff_ctx, tx_class);
Angie Chiang99b12422018-03-02 17:15:00 -0800350 assert(qc != 0);
351 const tran_low_t dqc = qcoeff_to_dqcoeff(qc, coeff_idx, dqv, txb_info->shift,
352 txb_info->iqmatrix);
353 const int64_t dqc_dist = get_coeff_dist(tqc, dqc, txb_info->shift);
Cheng Chen79641202018-01-04 18:52:52 -0800354
Angie Chiang99b12422018-03-02 17:15:00 -0800355 // distortion difference when coefficient is quantized to 0
356 const tran_low_t dqc0 =
357 qcoeff_to_dqcoeff(0, coeff_idx, dqv, txb_info->shift, txb_info->iqmatrix);
Cheng Chen79641202018-01-04 18:52:52 -0800358
Angie Chiang99b12422018-03-02 17:15:00 -0800359 stats->dist0 = get_coeff_dist(tqc, dqc0, txb_info->shift);
360 stats->dist = dqc_dist - stats->dist0;
361 stats->rate = qc_cost;
Cheng Chen79641202018-01-04 18:52:52 -0800362
Dake Hea47cd6c2017-10-13 18:09:58 -0700363 stats->rd = RDCOST(txb_info->rdmult, stats->rate, stats->dist);
364
365 stats->low_qc = get_lower_coeff(qc);
Dake Hea47cd6c2017-10-13 18:09:58 -0700366
Dake He4d447692017-12-15 09:10:06 -0800367 if (is_eob && stats->low_qc == 0) {
Dake He4d447692017-12-15 09:10:06 -0800368 stats->rd_low = stats->rd; // disable selection of low_qc in this case.
Ola Hugosson13892102017-11-06 08:01:44 +0100369 } else {
Cheng Chen37d88732018-01-09 14:02:41 -0800370 if (stats->low_qc == 0) {
371 stats->dist_low = 0;
372 } else {
Frederic Barbiere111cba2018-02-20 16:11:28 +0100373 stats->low_dqc = qcoeff_to_dqcoeff(stats->low_qc, coeff_idx, dqv,
374 txb_info->shift, txb_info->iqmatrix);
Cheng Chen37d88732018-01-09 14:02:41 -0800375 const int64_t low_dqc_dist =
376 get_coeff_dist(tqc, stats->low_dqc, txb_info->shift);
377 stats->dist_low = low_dqc_dist - stats->dist0;
378 }
Katsuhisa Yuasa55c95f82018-04-03 00:51:22 +0900379 const int low_qc_cost =
380 get_coeff_cost(stats->low_qc, scan_idx, is_eob, txb_info, txb_costs,
381 coeff_ctx, tx_class);
Dake He4d447692017-12-15 09:10:06 -0800382 stats->rate_low = low_qc_cost;
383 stats->rd_low = RDCOST(txb_info->rdmult, stats->rate_low, stats->dist_low);
Ola Hugosson13892102017-11-06 08:01:44 +0100384 }
Angie Chiangcbbf4f02018-03-02 18:34:38 -0800385}
Cheng Chen37d88732018-01-09 14:02:41 -0800386
Angie Chiangcbbf4f02018-03-02 18:34:38 -0800387static void get_dist_cost_stats_with_eob(
388 LevelDownStats *const stats, const int scan_idx,
Katsuhisa Yuasa55c95f82018-04-03 00:51:22 +0900389 const LV_MAP_COEFF_COST *const txb_costs, const TxbInfo *const txb_info,
390 const TX_CLASS tx_class) {
Angie Chiangcbbf4f02018-03-02 18:34:38 -0800391 const int is_eob = 0;
Katsuhisa Yuasa55c95f82018-04-03 00:51:22 +0900392 get_dist_cost_stats(stats, scan_idx, is_eob, txb_costs, txb_info, tx_class);
Angie Chiangcbbf4f02018-03-02 18:34:38 -0800393
394 const int16_t *const scan = txb_info->scan_order->scan;
395 const int coeff_idx = scan[scan_idx];
396 const tran_low_t qc = txb_info->qcoeff[coeff_idx];
397 const int coeff_ctx_temp = get_nz_map_ctx(
398 txb_info->levels, coeff_idx, txb_info->bwl, txb_info->height, scan_idx, 1,
Katsuhisa Yuasa55c95f82018-04-03 00:51:22 +0900399 txb_info->tx_size, tx_class);
400 const int qc_eob_cost = get_coeff_cost(qc, scan_idx, 1, txb_info, txb_costs,
401 coeff_ctx_temp, tx_class);
Angie Chiangcbbf4f02018-03-02 18:34:38 -0800402 int64_t rd_eob = RDCOST(txb_info->rdmult, qc_eob_cost, stats->dist);
403 if (stats->low_qc != 0) {
Katsuhisa Yuasa55c95f82018-04-03 00:51:22 +0900404 const int low_qc_eob_cost =
405 get_coeff_cost(stats->low_qc, scan_idx, 1, txb_info, txb_costs,
406 coeff_ctx_temp, tx_class);
Angie Chiangcbbf4f02018-03-02 18:34:38 -0800407 int64_t rd_eob_low =
408 RDCOST(txb_info->rdmult, low_qc_eob_cost, stats->dist_low);
409 rd_eob = (rd_eob > rd_eob_low) ? rd_eob_low : rd_eob;
Cheng Chen37d88732018-01-09 14:02:41 -0800410 }
Angie Chiangcbbf4f02018-03-02 18:34:38 -0800411
412 stats->nz_rd = AOMMIN(stats->rd_low, stats->rd) - rd_eob;
Dake Hea47cd6c2017-10-13 18:09:58 -0700413}
414
Linfeng Zhang1015a342017-10-24 16:20:41 -0700415static INLINE void update_qcoeff(const int coeff_idx, const tran_low_t qc,
416 const TxbInfo *const txb_info) {
Dake Hea47cd6c2017-10-13 18:09:58 -0700417 txb_info->qcoeff[coeff_idx] = qc;
Linfeng Zhangd5647372017-12-05 17:06:07 -0800418 txb_info->levels[get_padded_idx(coeff_idx, txb_info->bwl)] =
Jingning Han5cb408e2017-11-17 14:43:39 -0800419 (uint8_t)clamp(abs(qc), 0, INT8_MAX);
Linfeng Zhang1015a342017-10-24 16:20:41 -0700420}
421
422static INLINE void update_coeff(const int coeff_idx, const tran_low_t qc,
423 const TxbInfo *const txb_info) {
424 update_qcoeff(coeff_idx, qc, txb_info);
Dake Hea47cd6c2017-10-13 18:09:58 -0700425 const int dqv = txb_info->dequant[coeff_idx != 0];
Frederic Barbiere111cba2018-02-20 16:11:28 +0100426 txb_info->dqcoeff[coeff_idx] = qcoeff_to_dqcoeff(
427 qc, coeff_idx, dqv, txb_info->shift, txb_info->iqmatrix);
Dake Hea47cd6c2017-10-13 18:09:58 -0700428}
429
Peng Bin27d7ca92018-03-22 22:25:56 +0800430void av1_txb_init_levels_c(const tran_low_t *const coeff, const int width,
431 const int height, uint8_t *const levels) {
Linfeng Zhang679d81e2017-10-31 15:27:42 -0700432 const int stride = width + TX_PAD_HOR;
Linfeng Zhang1122d7d2017-10-31 15:30:28 -0700433 uint8_t *ls = levels;
Linfeng Zhang679d81e2017-10-31 15:27:42 -0700434
435 memset(levels - TX_PAD_TOP * stride, 0,
436 sizeof(*levels) * TX_PAD_TOP * stride);
Linfeng Zhang1122d7d2017-10-31 15:30:28 -0700437 memset(levels + stride * height, 0,
438 sizeof(*levels) * (TX_PAD_BOTTOM * stride + TX_PAD_END));
Linfeng Zhang679d81e2017-10-31 15:27:42 -0700439
Linfeng Zhang1122d7d2017-10-31 15:30:28 -0700440 for (int i = 0; i < height; i++) {
441 for (int j = 0; j < width; j++) {
Jingning Han5cb408e2017-11-17 14:43:39 -0800442 *ls++ = (uint8_t)clamp(abs(coeff[i * width + j]), 0, INT8_MAX);
Linfeng Zhang1122d7d2017-10-31 15:30:28 -0700443 }
444 for (int j = 0; j < TX_PAD_HOR; j++) {
445 *ls++ = 0;
446 }
Linfeng Zhang1015a342017-10-24 16:20:41 -0700447 }
448}
449
Linfeng Zhang0ba23e82017-12-20 16:27:28 -0800450void av1_get_nz_map_contexts_c(const uint8_t *const levels,
451 const int16_t *const scan, const uint16_t eob,
Katsuhisa Yuasa55c95f82018-04-03 00:51:22 +0900452 const TX_SIZE tx_size, const TX_CLASS tx_class,
Linfeng Zhang0ba23e82017-12-20 16:27:28 -0800453 int8_t *const coeff_contexts) {
Linfeng Zhangd67c13f2017-12-11 11:49:12 -0800454 const int bwl = get_txb_bwl(tx_size);
Linfeng Zhangd67c13f2017-12-11 11:49:12 -0800455 const int height = get_txb_high(tx_size);
Linfeng Zhangd67c13f2017-12-11 11:49:12 -0800456 for (int i = 0; i < eob; ++i) {
457 const int pos = scan[i];
Sebastien Alaiwan78f7bb92018-01-11 11:02:43 +0100458 coeff_contexts[pos] = get_nz_map_ctx(levels, pos, bwl, height, i,
Katsuhisa Yuasa55c95f82018-04-03 00:51:22 +0900459 i == eob - 1, tx_size, tx_class);
Linfeng Zhangd67c13f2017-12-11 11:49:12 -0800460 }
461}
462
Angie Chiang80b82262017-02-24 11:39:47 -0800463void av1_write_coeffs_txb(const AV1_COMMON *const cm, MACROBLOCKD *xd,
Luc Trudeau2eb9b842017-12-13 11:19:16 -0500464 aom_writer *w, int blk_row, int blk_col, int plane,
465 TX_SIZE tx_size, const tran_low_t *tcoeff,
Jingning Han7eab9ff2017-07-06 10:12:54 -0700466 uint16_t eob, TXB_CTX *txb_ctx) {
Angie Chiangcea11f22017-02-24 12:30:40 -0800467 const PLANE_TYPE plane_type = get_plane_type(plane);
Debargha Mukherjeeb3eda2f2017-11-28 16:00:20 -0800468 const TX_SIZE txs_ctx = get_txsize_entropy_ctx(tx_size);
Sarah Parker7c71cc02018-01-29 12:27:58 -0800469 const TX_TYPE tx_type = av1_get_tx_type(plane_type, xd, blk_row, blk_col,
470 tx_size, cm->reduced_tx_set_used);
Katsuhisa Yuasa55c95f82018-04-03 00:51:22 +0900471 const TX_CLASS tx_class = tx_type_to_class[tx_type];
Yaowu Xuf1e12932018-02-26 15:50:09 -0800472 const SCAN_ORDER *const scan_order = get_scan(tx_size, tx_type);
Linfeng Zhangdb41d1e2017-12-05 11:06:20 -0800473 const int16_t *const scan = scan_order->scan;
Angie Chiang80b82262017-02-24 11:39:47 -0800474 int c;
Angie Chianga9ba58e2017-12-01 19:22:43 -0800475 const int bwl = get_txb_bwl(tx_size);
476 const int width = get_txb_wide(tx_size);
477 const int height = get_txb_high(tx_size);
Jingning Han41c7f442017-09-05 14:54:00 -0700478 FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
Linfeng Zhang679d81e2017-10-31 15:27:42 -0700479 uint8_t levels_buf[TX_PAD_2D];
480 uint8_t *const levels = set_levels(levels_buf, width);
Linfeng Zhangd67c13f2017-12-11 11:49:12 -0800481 DECLARE_ALIGNED(16, int8_t, coeff_contexts[MAX_TX_SQUARE]);
Angie Chiang80b82262017-02-24 11:39:47 -0800482
Hui Su41d61522018-01-23 10:47:55 -0800483 aom_write_symbol(w, eob == 0,
484 ec_ctx->txb_skip_cdf[txs_ctx][txb_ctx->txb_skip_ctx], 2);
Angie Chianga3f7d2e2017-12-07 19:51:14 -0800485 if (plane == 0 && eob == 0) {
486 assert(tx_type == DCT_DCT);
487 }
Angie Chiang80b82262017-02-24 11:39:47 -0800488 if (eob == 0) return;
Linfeng Zhangce065ca2017-10-17 16:49:30 -0700489
Linfeng Zhang1122d7d2017-10-31 15:30:28 -0700490 av1_txb_init_levels(tcoeff, width, height, levels);
Linfeng Zhangce065ca2017-10-17 16:49:30 -0700491
Debargha Mukherjee3ebb0d02017-12-14 05:05:18 -0800492 av1_write_tx_type(cm, xd, blk_row, blk_col, plane, tx_size, w);
Angie Chiang80b82262017-02-24 11:39:47 -0800493
Yaowu Xu49abec82018-03-13 16:09:01 -0700494 int eob_extra;
Linfeng Zhang0c72b2f2017-12-04 10:59:28 -0800495 const int eob_pt = get_eob_pos_token(eob, &eob_extra);
Dake He0db7d0e2017-12-21 15:23:20 -0800496 const int eob_multi_size = txsize_log2_minus4[tx_size];
Katsuhisa Yuasa55c95f82018-04-03 00:51:22 +0900497 const int eob_multi_ctx = (tx_class == TX_CLASS_2D) ? 0 : 1;
Dake He0db7d0e2017-12-21 15:23:20 -0800498 switch (eob_multi_size) {
499 case 0:
500 aom_write_symbol(w, eob_pt - 1,
501 ec_ctx->eob_flag_cdf16[plane_type][eob_multi_ctx], 5);
502 break;
503 case 1:
504 aom_write_symbol(w, eob_pt - 1,
505 ec_ctx->eob_flag_cdf32[plane_type][eob_multi_ctx], 6);
506 break;
507 case 2:
508 aom_write_symbol(w, eob_pt - 1,
509 ec_ctx->eob_flag_cdf64[plane_type][eob_multi_ctx], 7);
510 break;
511 case 3:
512 aom_write_symbol(w, eob_pt - 1,
513 ec_ctx->eob_flag_cdf128[plane_type][eob_multi_ctx], 8);
514 break;
515 case 4:
516 aom_write_symbol(w, eob_pt - 1,
517 ec_ctx->eob_flag_cdf256[plane_type][eob_multi_ctx], 9);
518 break;
519 case 5:
520 aom_write_symbol(w, eob_pt - 1,
521 ec_ctx->eob_flag_cdf512[plane_type][eob_multi_ctx], 10);
522 break;
523 default:
524 aom_write_symbol(w, eob_pt - 1,
525 ec_ctx->eob_flag_cdf1024[plane_type][eob_multi_ctx], 11);
526 break;
527 }
528
Dake Hea47cd6c2017-10-13 18:09:58 -0700529 if (k_eob_offset_bits[eob_pt] > 0) {
Jingning Hanff4a9f82018-06-08 10:48:45 -0700530 const int eob_ctx = eob_pt - 3;
Angie Chiang7ab884e2017-10-18 15:57:12 -0700531 int eob_shift = k_eob_offset_bits[eob_pt] - 1;
532 int bit = (eob_extra & (1 << eob_shift)) ? 1 : 0;
Jingning Hanff4a9f82018-06-08 10:48:45 -0700533 aom_write_symbol(w, bit,
534 ec_ctx->eob_extra_cdf[txs_ctx][plane_type][eob_ctx], 2);
Angie Chiang7ab884e2017-10-18 15:57:12 -0700535 for (int i = 1; i < k_eob_offset_bits[eob_pt]; i++) {
536 eob_shift = k_eob_offset_bits[eob_pt] - 1 - i;
537 bit = (eob_extra & (1 << eob_shift)) ? 1 : 0;
Dake Hea47cd6c2017-10-13 18:09:58 -0700538 aom_write_bit(w, bit);
Dake Hea47cd6c2017-10-13 18:09:58 -0700539 }
540 }
Dake He03a32922017-10-31 08:06:45 -0700541
Katsuhisa Yuasa55c95f82018-04-03 00:51:22 +0900542 av1_get_nz_map_contexts(levels, scan, eob, tx_size, tx_class, coeff_contexts);
Linfeng Zhangd67c13f2017-12-11 11:49:12 -0800543
544 for (c = eob - 1; c >= 0; --c) {
Linfeng Zhangdb41d1e2017-12-05 11:06:20 -0800545 const int pos = scan[c];
Linfeng Zhangd67c13f2017-12-11 11:49:12 -0800546 const int coeff_ctx = coeff_contexts[pos];
547 const tran_low_t v = tcoeff[pos];
Angie Chiang41220ab2018-02-14 17:48:23 -0800548 const tran_low_t level = abs(v);
Dake He03a32922017-10-31 08:06:45 -0700549
Dake He3fe369c2017-11-16 17:56:44 -0800550 if (c == eob - 1) {
551 aom_write_symbol(
Angie Chiang41220ab2018-02-14 17:48:23 -0800552 w, AOMMIN(level, 3) - 1,
Dake He4d447692017-12-15 09:10:06 -0800553 ec_ctx->coeff_base_eob_cdf[txs_ctx][plane_type][coeff_ctx], 3);
Dake He3fe369c2017-11-16 17:56:44 -0800554 } else {
Angie Chiang41220ab2018-02-14 17:48:23 -0800555 aom_write_symbol(w, AOMMIN(level, 3),
Dake He3fe369c2017-11-16 17:56:44 -0800556 ec_ctx->coeff_base_cdf[txs_ctx][plane_type][coeff_ctx],
557 4);
558 }
Angie Chiang41220ab2018-02-14 17:48:23 -0800559 if (level > NUM_BASE_LEVELS) {
Sebastien Alaiwan78f7bb92018-01-11 11:02:43 +0100560 // level is above 1.
Linfeng Zhangdb41d1e2017-12-05 11:06:20 -0800561 const int base_range = level - 1 - NUM_BASE_LEVELS;
Katsuhisa Yuasa55c95f82018-04-03 00:51:22 +0900562 const int br_ctx = get_br_ctx(levels, pos, bwl, tx_class);
Angie Chiang41220ab2018-02-14 17:48:23 -0800563 for (int idx = 0; idx < COEFF_BASE_RANGE; idx += BR_CDF_SIZE - 1) {
Linfeng Zhangdb41d1e2017-12-05 11:06:20 -0800564 const int k = AOMMIN(base_range - idx, BR_CDF_SIZE - 1);
Angie Chiang6d5419c2018-02-20 15:12:15 -0800565 aom_write_symbol(
566 w, k,
567 ec_ctx->coeff_br_cdf[AOMMIN(txs_ctx, TX_32X32)][plane_type][br_ctx],
568 BR_CDF_SIZE);
Ola Hugossone72a2092017-11-12 09:11:53 +0100569 if (k < BR_CDF_SIZE - 1) break;
570 }
Angie Chiang6d5419c2018-02-20 15:12:15 -0800571 }
572 }
573
574 // Loop to code all signs in the transform block,
575 // starting with the sign of DC (if applicable)
576 for (c = 0; c < eob; ++c) {
577 const tran_low_t v = tcoeff[scan[c]];
578 const tran_low_t level = abs(v);
579 const int sign = (v < 0) ? 1 : 0;
580 if (level) {
581 if (c == 0) {
582 aom_write_symbol(
583 w, sign, ec_ctx->dc_sign_cdf[plane_type][txb_ctx->dc_sign_ctx], 2);
584 } else {
585 aom_write_bit(w, sign);
586 }
Yushin Cho73711d52018-05-04 13:59:46 -0700587 if (level > COEFF_BASE_RANGE + NUM_BASE_LEVELS)
588 write_golomb(w, level - COEFF_BASE_RANGE - 1 - NUM_BASE_LEVELS);
Angie Chiang41220ab2018-02-14 17:48:23 -0800589 }
590 }
Angie Chiang80b82262017-02-24 11:39:47 -0800591}
Angie Chiang47c72182017-02-27 14:30:38 -0800592
Angie Chiang140b3332017-12-12 17:29:25 -0800593typedef struct encode_txb_args {
594 const AV1_COMMON *cm;
595 MACROBLOCK *x;
596 aom_writer *w;
597} ENCODE_TXB_ARGS;
598
Jingning Han3a32e102018-01-11 16:00:35 -0800599static void write_coeffs_txb_wrap(const AV1_COMMON *cm, MACROBLOCK *x,
600 aom_writer *w, int plane, int block,
601 int blk_row, int blk_col, TX_SIZE tx_size) {
Angie Chiangc8af6112017-03-16 16:11:22 -0700602 MACROBLOCKD *xd = &x->e_mbd;
Angie Chiang140b3332017-12-12 17:29:25 -0800603 tran_low_t *tcoeff = BLOCK_OFFSET(x->mbmi_ext->tcoeff[plane], block);
604 uint16_t eob = x->mbmi_ext->eobs[plane][block];
605 TXB_CTX txb_ctx = { x->mbmi_ext->txb_skip_ctx[plane][block],
606 x->mbmi_ext->dc_sign_ctx[plane][block] };
Luc Trudeau2eb9b842017-12-13 11:19:16 -0500607 av1_write_coeffs_txb(cm, xd, w, blk_row, blk_col, plane, tx_size, tcoeff, eob,
608 &txb_ctx);
Angie Chiang140b3332017-12-12 17:29:25 -0800609}
610
Jingning Han4b48cd12018-01-11 15:56:42 -0800611void av1_write_coeffs_mb(const AV1_COMMON *const cm, MACROBLOCK *x, int mi_row,
Jingning Hanad54a982018-01-12 14:40:29 -0800612 int mi_col, aom_writer *w, BLOCK_SIZE bsize) {
Angie Chiang140b3332017-12-12 17:29:25 -0800613 MACROBLOCKD *xd = &x->e_mbd;
Jingning Hanad54a982018-01-12 14:40:29 -0800614 const int num_planes = av1_num_planes(cm);
615 int block[MAX_MB_PLANE] = { 0 };
Jingning Han4b48cd12018-01-11 15:56:42 -0800616 int row, col;
Cheng Chen8ab1f442018-04-27 18:01:52 -0700617 assert(bsize == get_plane_block_size(bsize, xd->plane[0].subsampling_x,
618 xd->plane[0].subsampling_y));
Yushin Choca63a8b2018-04-20 16:46:36 -0700619 const int max_blocks_wide = max_block_wide(xd, bsize, 0);
620 const int max_blocks_high = max_block_high(xd, bsize, 0);
621 const BLOCK_SIZE max_unit_bsize = BLOCK_64X64;
Jingning Han4b48cd12018-01-11 15:56:42 -0800622 int mu_blocks_wide = block_size_wide[max_unit_bsize] >> tx_size_wide_log2[0];
623 int mu_blocks_high = block_size_high[max_unit_bsize] >> tx_size_high_log2[0];
624 mu_blocks_wide = AOMMIN(max_blocks_wide, mu_blocks_wide);
625 mu_blocks_high = AOMMIN(max_blocks_high, mu_blocks_high);
626
627 for (row = 0; row < max_blocks_high; row += mu_blocks_high) {
Jingning Han4b48cd12018-01-11 15:56:42 -0800628 for (col = 0; col < max_blocks_wide; col += mu_blocks_wide) {
Jingning Hanad54a982018-01-12 14:40:29 -0800629 for (int plane = 0; plane < num_planes; ++plane) {
630 const struct macroblockd_plane *const pd = &xd->plane[plane];
631 if (!is_chroma_reference(mi_row, mi_col, bsize, pd->subsampling_x,
632 pd->subsampling_y))
633 continue;
634 const TX_SIZE tx_size = av1_get_tx_size(plane, xd);
635 const int stepr = tx_size_high_unit[tx_size];
636 const int stepc = tx_size_wide_unit[tx_size];
637 const int step = stepr * stepc;
Jingning Han4b48cd12018-01-11 15:56:42 -0800638
Jingning Hanad54a982018-01-12 14:40:29 -0800639 const int unit_height = ROUND_POWER_OF_TWO(
640 AOMMIN(mu_blocks_high + row, max_blocks_high), pd->subsampling_y);
641 const int unit_width = ROUND_POWER_OF_TWO(
642 AOMMIN(mu_blocks_wide + col, max_blocks_wide), pd->subsampling_x);
643 for (int blk_row = row >> pd->subsampling_y; blk_row < unit_height;
644 blk_row += stepr) {
645 for (int blk_col = col >> pd->subsampling_x; blk_col < unit_width;
646 blk_col += stepc) {
647 write_coeffs_txb_wrap(cm, x, w, plane, block[plane], blk_row,
648 blk_col, tx_size);
649 block[plane] += step;
650 }
Jingning Han4b48cd12018-01-11 15:56:42 -0800651 }
652 }
653 }
654 }
Angie Chiangc8af6112017-03-16 16:11:22 -0700655}
656
Urvang Joshi553096a2018-05-10 15:27:14 -0700657// TODO(angiebird): use this function whenever it's possible
658static int get_tx_type_cost(const AV1_COMMON *cm, const MACROBLOCK *x,
659 const MACROBLOCKD *xd, int plane, TX_SIZE tx_size,
660 TX_TYPE tx_type) {
661 if (plane > 0) return 0;
662
663 const TX_SIZE square_tx_size = txsize_sqr_map[tx_size];
664
665 const MB_MODE_INFO *mbmi = xd->mi[0];
666 const int is_inter = is_inter_block(mbmi);
667 if (get_ext_tx_types(tx_size, is_inter, cm->reduced_tx_set_used) > 1 &&
668 !xd->lossless[xd->mi[0]->segment_id]) {
669 const int ext_tx_set =
670 get_ext_tx_set(tx_size, is_inter, cm->reduced_tx_set_used);
671 if (is_inter) {
672 if (ext_tx_set > 0)
673 return x->inter_tx_type_costs[ext_tx_set][square_tx_size][tx_type];
674 } else {
675 if (ext_tx_set > 0) {
676 PREDICTION_MODE intra_dir;
677 if (mbmi->filter_intra_mode_info.use_filter_intra)
678 intra_dir = fimode_to_intradir[mbmi->filter_intra_mode_info
679 .filter_intra_mode];
680 else
681 intra_dir = mbmi->mode;
682 return x->intra_tx_type_costs[ext_tx_set][square_tx_size][intra_dir]
683 [tx_type];
684 }
685 }
686 }
687 return 0;
688}
689
Katsuhisa Yuasa51467032018-04-08 16:16:10 +0900690static AOM_FORCE_INLINE int warehouse_efficients_txb(
691 const AV1_COMMON *const cm, const MACROBLOCK *x, const int plane,
692 const int block, const TX_SIZE tx_size, const TXB_CTX *const txb_ctx,
693 const struct macroblock_plane *p, const int eob,
694 const PLANE_TYPE plane_type, const LV_MAP_COEFF_COST *const coeff_costs,
695 const MACROBLOCKD *const xd, const TX_TYPE tx_type,
696 const TX_CLASS tx_class) {
Angie Chiang47c72182017-02-27 14:30:38 -0800697 const tran_low_t *const qcoeff = BLOCK_OFFSET(p->qcoeff, block);
Linfeng Zhangc02b4112017-12-21 13:11:36 -0800698 const int txb_skip_ctx = txb_ctx->txb_skip_ctx;
Angie Chianga9ba58e2017-12-01 19:22:43 -0800699 const int bwl = get_txb_bwl(tx_size);
700 const int width = get_txb_wide(tx_size);
701 const int height = get_txb_high(tx_size);
Yaowu Xuf1e12932018-02-26 15:50:09 -0800702 const SCAN_ORDER *const scan_order = get_scan(tx_size, tx_type);
Linfeng Zhangdb41d1e2017-12-05 11:06:20 -0800703 const int16_t *const scan = scan_order->scan;
Linfeng Zhang679d81e2017-10-31 15:27:42 -0700704 uint8_t levels_buf[TX_PAD_2D];
705 uint8_t *const levels = set_levels(levels_buf, width);
Linfeng Zhangd67c13f2017-12-11 11:49:12 -0800706 DECLARE_ALIGNED(16, int8_t, coeff_contexts[MAX_TX_SQUARE]);
Dake He0db7d0e2017-12-21 15:23:20 -0800707 const int eob_multi_size = txsize_log2_minus4[tx_size];
708 const LV_MAP_EOB_COST *const eob_costs =
709 &x->eob_costs[eob_multi_size][plane_type];
Hui Su18205542018-03-30 11:37:41 -0700710 int cost = coeff_costs->txb_skip_cost[txb_skip_ctx][0];
Angie Chiang47c72182017-02-27 14:30:38 -0800711
Linfeng Zhang1122d7d2017-10-31 15:30:28 -0700712 av1_txb_init_levels(qcoeff, width, height, levels);
Linfeng Zhang1015a342017-10-24 16:20:41 -0700713
Urvang Joshi553096a2018-05-10 15:27:14 -0700714 cost += get_tx_type_cost(cm, x, xd, plane, tx_size, tx_type);
Angie Chiang05917872017-04-15 12:28:56 -0700715
Katsuhisa Yuasa55c95f82018-04-03 00:51:22 +0900716 cost += get_eob_cost(eob, eob_costs, coeff_costs, tx_class);
Linfeng Zhangd67c13f2017-12-11 11:49:12 -0800717
Katsuhisa Yuasa55c95f82018-04-03 00:51:22 +0900718 av1_get_nz_map_contexts(levels, scan, eob, tx_size, tx_class, coeff_contexts);
Linfeng Zhangd67c13f2017-12-11 11:49:12 -0800719
Katsuhisa Yuasa51467032018-04-08 16:16:10 +0900720 const int(*lps_cost)[COEFF_BASE_RANGE + 1] = coeff_costs->lps_cost;
721 int c = eob - 1;
722 {
Linfeng Zhangdb41d1e2017-12-05 11:06:20 -0800723 const int pos = scan[c];
724 const tran_low_t v = qcoeff[pos];
Katsuhisa Yuasa51467032018-04-08 16:16:10 +0900725 const int sign = v >> 31;
726 const int level = (v ^ sign) - sign;
Linfeng Zhangd67c13f2017-12-11 11:49:12 -0800727 const int coeff_ctx = coeff_contexts[pos];
Katsuhisa Yuasa51467032018-04-08 16:16:10 +0900728 cost += coeff_costs->base_eob_cost[coeff_ctx][AOMMIN(level, 3) - 1];
Dake Hea47cd6c2017-10-13 18:09:58 -0700729
Katsuhisa Yuasa51467032018-04-08 16:16:10 +0900730 if (v) {
Dake Hea47cd6c2017-10-13 18:09:58 -0700731 // sign bit cost
Dake Hea47cd6c2017-10-13 18:09:58 -0700732 if (level > NUM_BASE_LEVELS) {
Katsuhisa Yuasa55c95f82018-04-03 00:51:22 +0900733 const int ctx = get_br_ctx(levels, pos, bwl, tx_class);
Katsuhisa Yuasa51467032018-04-08 16:16:10 +0900734 const int base_range =
735 AOMMIN(level - 1 - NUM_BASE_LEVELS, COEFF_BASE_RANGE);
736 cost += lps_cost[ctx][base_range];
737 cost += get_golomb_cost(level);
738 }
739 if (c) {
740 cost += av1_cost_literal(1);
741 } else {
742 const int sign01 = (sign ^ sign) - sign;
743 const int dc_sign_ctx = txb_ctx->dc_sign_ctx;
744 cost += coeff_costs->dc_sign_cost[dc_sign_ctx][sign01];
745 return cost;
746 }
747 }
748 }
749 const int(*base_cost)[4] = coeff_costs->base_cost;
750 for (c = eob - 2; c >= 1; --c) {
751 const int pos = scan[c];
752 const int coeff_ctx = coeff_contexts[pos];
753 const tran_low_t v = qcoeff[pos];
754 const int level = abs(v);
755 const int cost0 = base_cost[coeff_ctx][AOMMIN(level, 3)];
756 if (v) {
757 // sign bit cost
758 cost += av1_cost_literal(1);
759 if (level > NUM_BASE_LEVELS) {
760 const int ctx = get_br_ctx(levels, pos, bwl, tx_class);
761 const int base_range =
762 AOMMIN(level - 1 - NUM_BASE_LEVELS, COEFF_BASE_RANGE);
763 cost += lps_cost[ctx][base_range];
764 cost += get_golomb_cost(level);
765 }
766 }
767 cost += cost0;
768 }
769 if (c == 0) {
770 const int pos = scan[c];
771 const tran_low_t v = qcoeff[pos];
772 const int coeff_ctx = coeff_contexts[pos];
773 const int sign = v >> 31;
774 const int level = (v ^ sign) - sign;
775 cost += base_cost[coeff_ctx][AOMMIN(level, 3)];
Dake Hea47cd6c2017-10-13 18:09:58 -0700776
Katsuhisa Yuasa51467032018-04-08 16:16:10 +0900777 if (v) {
778 // sign bit cost
779 const int sign01 = (sign ^ sign) - sign;
780 const int dc_sign_ctx = txb_ctx->dc_sign_ctx;
781 cost += coeff_costs->dc_sign_cost[dc_sign_ctx][sign01];
782 if (level > NUM_BASE_LEVELS) {
783 const int ctx = get_br_ctx(levels, pos, bwl, tx_class);
784 const int base_range =
785 AOMMIN(level - 1 - NUM_BASE_LEVELS, COEFF_BASE_RANGE);
786 cost += lps_cost[ctx][base_range];
787 cost += get_golomb_cost(level);
Dake Hea47cd6c2017-10-13 18:09:58 -0700788 }
789 }
790 }
Angie Chiang47c72182017-02-27 14:30:38 -0800791 return cost;
792}
Angie Chiang0397eda2017-03-15 16:57:14 -0700793
Katsuhisa Yuasa51467032018-04-08 16:16:10 +0900794int av1_cost_coeffs_txb(const AV1_COMMON *const cm, const MACROBLOCK *x,
Xing Jin3a43b3b2018-06-25 15:41:38 +0800795 const int plane, const int block, const TX_SIZE tx_size,
796 const TX_TYPE tx_type, const TXB_CTX *const txb_ctx) {
Katsuhisa Yuasa51467032018-04-08 16:16:10 +0900797 const struct macroblock_plane *p = &x->plane[plane];
798 const int eob = p->eobs[block];
799 const TX_SIZE txs_ctx = get_txsize_entropy_ctx(tx_size);
800 const PLANE_TYPE plane_type = get_plane_type(plane);
801 const LV_MAP_COEFF_COST *const coeff_costs =
802 &x->coeff_costs[txs_ctx][plane_type];
803 if (eob == 0) {
804 return coeff_costs->txb_skip_cost[txb_ctx->txb_skip_ctx][1];
805 }
806
807 const MACROBLOCKD *const xd = &x->e_mbd;
Katsuhisa Yuasa51467032018-04-08 16:16:10 +0900808 const TX_CLASS tx_class = tx_type_to_class[tx_type];
809
810#define WAREHOUSE_EFFICIENTS_TXB_CASE(tx_class_literal) \
811 case tx_class_literal: \
812 return warehouse_efficients_txb(cm, x, plane, block, tx_size, txb_ctx, p, \
813 eob, plane_type, coeff_costs, xd, tx_type, \
814 tx_class_literal);
815 switch (tx_class) {
816 WAREHOUSE_EFFICIENTS_TXB_CASE(TX_CLASS_2D);
817 WAREHOUSE_EFFICIENTS_TXB_CASE(TX_CLASS_HORIZ);
818 WAREHOUSE_EFFICIENTS_TXB_CASE(TX_CLASS_VERT);
819#undef WAREHOUSE_EFFICIENTS_TXB_CASE
820 default: assert(false); return 0;
821 }
822}
823
Dake Hea47cd6c2017-10-13 18:09:58 -0700824static int optimize_txb(TxbInfo *txb_info, const LV_MAP_COEFF_COST *txb_costs,
Angie Chiang45385b82018-03-02 15:25:58 -0800825 const LV_MAP_EOB_COST *txb_eob_costs, int *rate_cost) {
Dake Hea47cd6c2017-10-13 18:09:58 -0700826 int update = 0;
827 if (txb_info->eob == 0) return update;
Linfeng Zhangdb41d1e2017-12-05 11:06:20 -0800828 const int16_t *const scan = txb_info->scan_order->scan;
Dake Hea47cd6c2017-10-13 18:09:58 -0700829 // forward optimize the nz_map`
830 const int init_eob = txb_info->eob;
Katsuhisa Yuasa55c95f82018-04-03 00:51:22 +0900831 const TX_CLASS tx_class = tx_type_to_class[txb_info->tx_type];
Yaowu Xu49abec82018-03-13 16:09:01 -0700832 const int eob_cost =
Katsuhisa Yuasa55c95f82018-04-03 00:51:22 +0900833 get_eob_cost(init_eob, txb_eob_costs, txb_costs, tx_class);
Dake Hea47cd6c2017-10-13 18:09:58 -0700834
835 // backward optimize the level-k map
Cheng Chen82775f62018-01-18 12:09:54 -0800836 int accu_rate = eob_cost;
Dake Hea47cd6c2017-10-13 18:09:58 -0700837 int64_t accu_dist = 0;
838 int64_t prev_eob_rd_cost = INT64_MAX;
839 int64_t cur_eob_rd_cost = 0;
840
Angie Chiangcbbf4f02018-03-02 18:34:38 -0800841 {
842 const int si = init_eob - 1;
843 const int coeff_idx = scan[si];
844 LevelDownStats stats;
Katsuhisa Yuasa55c95f82018-04-03 00:51:22 +0900845 get_dist_cost_stats(&stats, si, si == init_eob - 1, txb_costs, txb_info,
846 tx_class);
Angie Chiangcbbf4f02018-03-02 18:34:38 -0800847 if ((stats.rd_low < stats.rd) && (stats.low_qc != 0)) {
848 update = 1;
849 update_coeff(coeff_idx, stats.low_qc, txb_info);
850 accu_rate += stats.rate_low;
851 accu_dist += stats.dist_low;
852 } else {
853 accu_rate += stats.rate;
854 accu_dist += stats.dist;
855 }
856 }
857
858 int si = init_eob - 2;
859 int8_t has_nz_tail = 0;
860 // eob is not fixed
861 for (; si >= 0 && has_nz_tail < 2; --si) {
862 assert(si != init_eob - 1);
Dake Hea47cd6c2017-10-13 18:09:58 -0700863 const int coeff_idx = scan[si];
864 tran_low_t qc = txb_info->qcoeff[coeff_idx];
865
Dake Hea47cd6c2017-10-13 18:09:58 -0700866 if (qc == 0) {
Angie Chiang99b12422018-03-02 17:15:00 -0800867 const int coeff_ctx =
868 get_lower_levels_ctx(txb_info->levels, coeff_idx, txb_info->bwl,
Katsuhisa Yuasa55c95f82018-04-03 00:51:22 +0900869 txb_info->tx_size, tx_class);
Angie Chiang99b12422018-03-02 17:15:00 -0800870 accu_rate += txb_costs->base_cost[coeff_ctx][0];
Dake Hea47cd6c2017-10-13 18:09:58 -0700871 } else {
Angie Chiang99b12422018-03-02 17:15:00 -0800872 LevelDownStats stats;
Katsuhisa Yuasa55c95f82018-04-03 00:51:22 +0900873 get_dist_cost_stats_with_eob(&stats, si, txb_costs, txb_info, tx_class);
Angie Chiangcbbf4f02018-03-02 18:34:38 -0800874 // check if it is better to make this the last significant coefficient
Yaowu Xu49abec82018-03-13 16:09:01 -0700875 int cur_eob_rate =
Katsuhisa Yuasa55c95f82018-04-03 00:51:22 +0900876 get_eob_cost(si + 1, txb_eob_costs, txb_costs, tx_class);
Angie Chiangcbbf4f02018-03-02 18:34:38 -0800877 cur_eob_rd_cost = RDCOST(txb_info->rdmult, cur_eob_rate, 0);
878 prev_eob_rd_cost =
879 RDCOST(txb_info->rdmult, accu_rate, accu_dist) + stats.nz_rd;
880 if (cur_eob_rd_cost <= prev_eob_rd_cost) {
881 update = 1;
882 for (int j = si + 1; j < txb_info->eob; j++) {
883 const int coeff_pos_j = scan[j];
884 update_coeff(coeff_pos_j, 0, txb_info);
885 }
886 txb_info->eob = si + 1;
Angie Chiang99b12422018-03-02 17:15:00 -0800887
Angie Chiangcbbf4f02018-03-02 18:34:38 -0800888 // rerun cost calculation due to change of eob
889 accu_rate = cur_eob_rate;
890 accu_dist = 0;
Katsuhisa Yuasa55c95f82018-04-03 00:51:22 +0900891 get_dist_cost_stats(&stats, si, 1, txb_costs, txb_info, tx_class);
Angie Chiangcbbf4f02018-03-02 18:34:38 -0800892 if ((stats.rd_low < stats.rd) && (stats.low_qc != 0)) {
893 update = 1;
894 update_coeff(coeff_idx, stats.low_qc, txb_info);
895 accu_rate += stats.rate_low;
896 accu_dist += stats.dist_low;
Cheng Chenec32a742018-01-12 19:09:39 -0800897 } else {
Angie Chiangcbbf4f02018-03-02 18:34:38 -0800898 accu_rate += stats.rate;
899 accu_dist += stats.dist;
900 }
901
902 // reset non zero tail when new eob is found
903 has_nz_tail = 0;
904 } else {
905 int bUpdCoeff = 0;
906 if (stats.rd_low < stats.rd) {
907 if ((si < txb_info->eob - 1)) {
908 bUpdCoeff = 1;
Cheng Chenec32a742018-01-12 19:09:39 -0800909 update = 1;
Cheng Chenec32a742018-01-12 19:09:39 -0800910 }
Angie Chiangcbbf4f02018-03-02 18:34:38 -0800911 } else {
912 ++has_nz_tail;
913 }
914
915 if (bUpdCoeff) {
916 update_coeff(coeff_idx, stats.low_qc, txb_info);
917 accu_rate += stats.rate_low;
918 accu_dist += stats.dist_low;
919 } else {
920 accu_rate += stats.rate;
921 accu_dist += stats.dist;
Jingning Han8be58fa2017-12-18 09:46:13 -0800922 }
Dake Hea47cd6c2017-10-13 18:09:58 -0700923 }
Angie Chiangcbbf4f02018-03-02 18:34:38 -0800924 }
925 } // for (si)
926
927 // eob is fixed
928 for (; si >= 0; --si) {
929 assert(si != init_eob - 1);
930 const int coeff_idx = scan[si];
931 tran_low_t qc = txb_info->qcoeff[coeff_idx];
932
933 if (qc == 0) {
934 const int coeff_ctx =
935 get_lower_levels_ctx(txb_info->levels, coeff_idx, txb_info->bwl,
Katsuhisa Yuasa55c95f82018-04-03 00:51:22 +0900936 txb_info->tx_size, tx_class);
Angie Chiangcbbf4f02018-03-02 18:34:38 -0800937 accu_rate += txb_costs->base_cost[coeff_ctx][0];
938 } else {
939 LevelDownStats stats;
Katsuhisa Yuasa55c95f82018-04-03 00:51:22 +0900940 get_dist_cost_stats(&stats, si, 0, txb_costs, txb_info, tx_class);
Dake Hea47cd6c2017-10-13 18:09:58 -0700941
942 int bUpdCoeff = 0;
943 if (stats.rd_low < stats.rd) {
Dake He4d447692017-12-15 09:10:06 -0800944 if ((si < txb_info->eob - 1)) {
Dake Hea47cd6c2017-10-13 18:09:58 -0700945 bUpdCoeff = 1;
946 update = 1;
947 }
948 }
Dake Hea47cd6c2017-10-13 18:09:58 -0700949 if (bUpdCoeff) {
950 update_coeff(coeff_idx, stats.low_qc, txb_info);
951 accu_rate += stats.rate_low;
952 accu_dist += stats.dist_low;
953 } else {
954 accu_rate += stats.rate;
955 accu_dist += stats.dist;
956 }
957 }
958 } // for (si)
Jingning Hana7a6f4e2017-12-13 14:44:08 -0800959
Dake Hea47cd6c2017-10-13 18:09:58 -0700960 int non_zero_blk_rate =
961 txb_costs->txb_skip_cost[txb_info->txb_ctx->txb_skip_ctx][0];
962 prev_eob_rd_cost =
963 RDCOST(txb_info->rdmult, accu_rate + non_zero_blk_rate, accu_dist);
964
965 int zero_blk_rate =
966 txb_costs->txb_skip_cost[txb_info->txb_ctx->txb_skip_ctx][1];
967 int64_t zero_blk_rd_cost = RDCOST(txb_info->rdmult, zero_blk_rate, 0);
968 if (zero_blk_rd_cost <= prev_eob_rd_cost) {
969 update = 1;
970 for (int j = 0; j < txb_info->eob; j++) {
971 const int coeff_pos_j = scan[j];
972 update_coeff(coeff_pos_j, 0, txb_info);
973 }
974 txb_info->eob = 0;
975 }
976
Cheng Chen82775f62018-01-18 12:09:54 -0800977 // record total rate cost
978 *rate_cost = zero_blk_rd_cost <= prev_eob_rd_cost
979 ? zero_blk_rate
980 : accu_rate + non_zero_blk_rate;
Angie Chiang0ed53352018-03-08 12:53:43 -0800981
982 if (txb_info->eob > 0) {
983 *rate_cost += txb_info->tx_type_cost;
984 }
985
Dake Hea47cd6c2017-10-13 18:09:58 -0700986 return update;
987}
988
Angie Chiang07c57f32017-05-30 18:18:33 -0700989// These numbers are empirically obtained.
990static const int plane_rd_mult[REF_TYPES][PLANE_TYPES] = {
Johannb0ef6ff2018-02-08 14:32:21 -0800991 { 17, 13 },
992 { 16, 10 },
Angie Chiang07c57f32017-05-30 18:18:33 -0700993};
994
Michelle Findlay-Olynykdea531d2017-12-13 14:10:56 -0800995void hbt_init() {
996 hbt_hash_table =
997 aom_malloc(sizeof(OptTxbQcoeff) * HBT_TABLE_SIZE * HBT_ARRAY_LENGTH);
998 memset(hbt_hash_table, 0,
999 sizeof(OptTxbQcoeff) * HBT_TABLE_SIZE * HBT_ARRAY_LENGTH);
Peng Bin8a204cd2018-04-08 13:07:35 +08001000 av1_crc32c_calculator_init(&crc_calculator); // 31 bit: qc & ctx
Michelle Findlay-Olynykdea531d2017-12-13 14:10:56 -08001001
1002 hbt_needs_init = 0;
Michelle Findlay-Olynykfbab0622017-12-13 14:10:56 -08001003}
1004
Michelle Findlay-Olynykdea531d2017-12-13 14:10:56 -08001005void hbt_destroy() { aom_free(hbt_hash_table); }
1006
1007int hbt_hash_miss(uint32_t hbt_ctx_hash, uint32_t hbt_qc_hash,
1008 TxbInfo *txb_info, const LV_MAP_COEFF_COST *txb_costs,
Michelle Findlay-Olynykfbab0622017-12-13 14:10:56 -08001009 const LV_MAP_EOB_COST *txb_eob_costs,
Michelle Findlay-Olynykdea531d2017-12-13 14:10:56 -08001010 const struct macroblock_plane *p, int block, int fast_mode,
1011 int *rate_cost) {
Angie Chiang45385b82018-03-02 15:25:58 -08001012 (void)fast_mode;
Michelle Findlay-Olynykfbab0622017-12-13 14:10:56 -08001013 const int16_t *scan = txb_info->scan_order->scan;
Michelle Findlay-Olynykdea531d2017-12-13 14:10:56 -08001014 int prev_eob = txb_info->eob;
1015 assert(HBT_EOB <= 16); // Lengthen array if allowing longer eob.
1016 int32_t prev_coeff[16];
1017 for (int i = 0; i < prev_eob; i++) {
1018 prev_coeff[i] = txb_info->qcoeff[scan[i]];
1019 }
1020 for (int i = prev_eob; i < HBT_EOB; i++) {
1021 prev_coeff[i] = 0; // For compiler piece of mind.
1022 }
Michelle Findlay-Olynykfbab0622017-12-13 14:10:56 -08001023
1024 av1_txb_init_levels(txb_info->qcoeff, txb_info->width, txb_info->height,
1025 txb_info->levels);
Michelle Findlay-Olynykdea531d2017-12-13 14:10:56 -08001026
Angie Chiang45385b82018-03-02 15:25:58 -08001027 const int update =
1028 optimize_txb(txb_info, txb_costs, txb_eob_costs, rate_cost);
Michelle Findlay-Olynykdea531d2017-12-13 14:10:56 -08001029
1030 // Overwrite old entry
1031 uint16_t hbt_table_index = hbt_ctx_hash % HBT_TABLE_SIZE;
1032 uint16_t hbt_array_index = hbt_qc_hash % HBT_ARRAY_LENGTH;
1033 hbt_hash_table[hbt_table_index * HBT_ARRAY_LENGTH + hbt_array_index]
1034 .rate_cost = *rate_cost;
1035 hbt_hash_table[hbt_table_index * HBT_ARRAY_LENGTH + hbt_array_index].init = 1;
1036 hbt_hash_table[hbt_table_index * HBT_ARRAY_LENGTH + hbt_array_index]
1037 .hbt_qc_hash = hbt_qc_hash;
1038 hbt_hash_table[hbt_table_index * HBT_ARRAY_LENGTH + hbt_array_index]
1039 .hbt_ctx_hash = hbt_ctx_hash;
1040 assert(prev_eob >= txb_info->eob); // eob can't get longer
1041 for (int i = 0; i < txb_info->eob; i++) {
1042 // Record how coeff changed. Convention: towards zero is negative.
1043 if (txb_info->qcoeff[scan[i]] > 0)
1044 hbt_hash_table[hbt_table_index * HBT_ARRAY_LENGTH + hbt_array_index]
1045 .deltas[i] = txb_info->qcoeff[scan[i]] - prev_coeff[i];
1046 else
1047 hbt_hash_table[hbt_table_index * HBT_ARRAY_LENGTH + hbt_array_index]
1048 .deltas[i] = prev_coeff[i] - txb_info->qcoeff[scan[i]];
1049 }
1050 for (int i = txb_info->eob; i < prev_eob; i++) {
1051 // If eob got shorter, record that all after it changed to zero.
1052 if (prev_coeff[i] > 0)
1053 hbt_hash_table[hbt_table_index * HBT_ARRAY_LENGTH + hbt_array_index]
1054 .deltas[i] = -prev_coeff[i];
1055 else
1056 hbt_hash_table[hbt_table_index * HBT_ARRAY_LENGTH + hbt_array_index]
1057 .deltas[i] = prev_coeff[i];
1058 }
1059 for (int i = prev_eob; i < HBT_EOB; i++) {
1060 // Record 'no change' after optimized coefficients run out.
1061 hbt_hash_table[hbt_table_index * HBT_ARRAY_LENGTH + hbt_array_index]
1062 .deltas[i] = 0;
1063 }
Michelle Findlay-Olynykfbab0622017-12-13 14:10:56 -08001064
1065 if (update) {
Michelle Findlay-Olynykfbab0622017-12-13 14:10:56 -08001066 p->eobs[block] = txb_info->eob;
1067 p->txb_entropy_ctx[block] = av1_get_txb_entropy_context(
1068 txb_info->qcoeff, txb_info->scan_order, txb_info->eob);
1069 }
1070 return txb_info->eob;
1071}
1072
Michelle Findlay-Olynykdea531d2017-12-13 14:10:56 -08001073int hbt_hash_hit(uint32_t hbt_table_index, int hbt_array_index,
1074 TxbInfo *txb_info, const struct macroblock_plane *p, int block,
1075 int *rate_cost) {
Michelle Findlay-Olynykfbab0622017-12-13 14:10:56 -08001076 const int16_t *scan = txb_info->scan_order->scan;
1077 int new_eob = 0;
1078 int update = 0;
1079
1080 for (int i = 0; i < txb_info->eob; i++) {
Michelle Findlay-Olynykdea531d2017-12-13 14:10:56 -08001081 // Delta convention is negatives go towards zero, so only apply those ones.
1082 if (hbt_hash_table[hbt_table_index * HBT_ARRAY_LENGTH + hbt_array_index]
1083 .deltas[i] < 0) {
1084 if (txb_info->qcoeff[scan[i]] > 0)
1085 txb_info->qcoeff[scan[i]] +=
1086 hbt_hash_table[hbt_table_index * HBT_ARRAY_LENGTH + hbt_array_index]
1087 .deltas[i];
1088 else
1089 txb_info->qcoeff[scan[i]] -=
1090 hbt_hash_table[hbt_table_index * HBT_ARRAY_LENGTH + hbt_array_index]
1091 .deltas[i];
1092
Michelle Findlay-Olynykfbab0622017-12-13 14:10:56 -08001093 update = 1;
1094 update_coeff(scan[i], txb_info->qcoeff[scan[i]], txb_info);
1095 }
Michelle Findlay-Olynykfbab0622017-12-13 14:10:56 -08001096 if (txb_info->qcoeff[scan[i]]) new_eob = i + 1;
1097 }
1098
Michelle Findlay-Olynykdea531d2017-12-13 14:10:56 -08001099 // Rate_cost can be calculated here instead (av1_cost_coeffs_txb), but
1100 // it is expensive and gives little benefit as long as qc_hash is high bit
1101 *rate_cost =
1102 hbt_hash_table[hbt_table_index * HBT_ARRAY_LENGTH + hbt_array_index]
1103 .rate_cost;
1104
Michelle Findlay-Olynykfbab0622017-12-13 14:10:56 -08001105 if (update) {
1106 txb_info->eob = new_eob;
1107 p->eobs[block] = txb_info->eob;
1108 p->txb_entropy_ctx[block] = av1_get_txb_entropy_context(
1109 txb_info->qcoeff, txb_info->scan_order, txb_info->eob);
1110 }
Michelle Findlay-Olynykdea531d2017-12-13 14:10:56 -08001111
Michelle Findlay-Olynykfbab0622017-12-13 14:10:56 -08001112 return txb_info->eob;
1113}
1114
Michelle Findlay-Olynykdea531d2017-12-13 14:10:56 -08001115int hbt_search_match(uint32_t hbt_ctx_hash, uint32_t hbt_qc_hash,
1116 TxbInfo *txb_info, const LV_MAP_COEFF_COST *txb_costs,
1117 const LV_MAP_EOB_COST *txb_eob_costs,
1118 const struct macroblock_plane *p, int block, int fast_mode,
1119 int *rate_cost) {
1120 // Check for qcoeff match
1121 int hbt_array_index = hbt_qc_hash % HBT_ARRAY_LENGTH;
1122 int hbt_table_index = hbt_ctx_hash % HBT_TABLE_SIZE;
Michelle Findlay-Olynykfbab0622017-12-13 14:10:56 -08001123
Michelle Findlay-Olynykdea531d2017-12-13 14:10:56 -08001124 if (hbt_hash_table[hbt_table_index * HBT_ARRAY_LENGTH + hbt_array_index]
1125 .hbt_qc_hash == hbt_qc_hash &&
1126 hbt_hash_table[hbt_table_index * HBT_ARRAY_LENGTH + hbt_array_index]
1127 .hbt_ctx_hash == hbt_ctx_hash &&
1128 hbt_hash_table[hbt_table_index * HBT_ARRAY_LENGTH + hbt_array_index]
1129 .init) {
1130 return hbt_hash_hit(hbt_table_index, hbt_array_index, txb_info, p, block,
1131 rate_cost);
1132 } else {
1133 return hbt_hash_miss(hbt_ctx_hash, hbt_qc_hash, txb_info, txb_costs,
1134 txb_eob_costs, p, block, fast_mode, rate_cost);
Michelle Findlay-Olynykfbab0622017-12-13 14:10:56 -08001135 }
1136}
1137
Michelle Findlay-Olynykdea531d2017-12-13 14:10:56 -08001138int hbt_create_hashes(TxbInfo *txb_info, const LV_MAP_COEFF_COST *txb_costs,
1139 const LV_MAP_EOB_COST *txb_eob_costs,
1140 const struct macroblock_plane *p, int block,
1141 int fast_mode, int *rate_cost) {
Michelle Findlay-Olynykfbab0622017-12-13 14:10:56 -08001142 // Initialize hash table if needed.
Michelle Findlay-Olynykdea531d2017-12-13 14:10:56 -08001143 if (hbt_needs_init) {
1144 hbt_init();
Michelle Findlay-Olynykfbab0622017-12-13 14:10:56 -08001145 }
1146
1147 //// Hash creation
Michelle Findlay-Olynykdea531d2017-12-13 14:10:56 -08001148 uint8_t txb_hash_data[256]; // Asserts below to ensure enough space.
Michelle Findlay-Olynykfbab0622017-12-13 14:10:56 -08001149 const int16_t *scan = txb_info->scan_order->scan;
1150 uint8_t chunk = 0;
Michelle Findlay-Olynykfbab0622017-12-13 14:10:56 -08001151 int hash_data_index = 0;
Michelle Findlay-Olynykfbab0622017-12-13 14:10:56 -08001152
Michelle Findlay-Olynykdea531d2017-12-13 14:10:56 -08001153 // Make qc_hash.
1154 int packing_index = 0; // needed for packing.
Michelle Findlay-Olynykfbab0622017-12-13 14:10:56 -08001155 for (int i = 0; i < txb_info->eob; i++) {
Michelle Findlay-Olynykdea531d2017-12-13 14:10:56 -08001156 tran_low_t prechunk = txb_info->qcoeff[scan[i]];
1157
1158 // Softening: Improves speed. Aligns with signed deltas.
1159 if (prechunk < 0) prechunk *= -1;
1160
1161 // Early kick out: Don't apply feature if there are large coeffs:
1162 // If this kickout value is removed or raised beyond int8_t,
1163 // widen deltas type in OptTxbQcoeff struct.
1164 assert((int8_t)HBT_KICKOUT == HBT_KICKOUT); // If not, widen types.
1165 if (prechunk > HBT_KICKOUT) {
1166 av1_txb_init_levels(txb_info->qcoeff, txb_info->width, txb_info->height,
1167 txb_info->levels);
1168
Angie Chiang45385b82018-03-02 15:25:58 -08001169 const int update =
1170 optimize_txb(txb_info, txb_costs, txb_eob_costs, rate_cost);
Michelle Findlay-Olynykdea531d2017-12-13 14:10:56 -08001171
1172 if (update) {
1173 p->eobs[block] = txb_info->eob;
1174 p->txb_entropy_ctx[block] = av1_get_txb_entropy_context(
1175 txb_info->qcoeff, txb_info->scan_order, txb_info->eob);
1176 }
1177 return txb_info->eob;
1178 }
1179
1180 // Since coeffs are 0 to 3, only 2 bits are needed: pack into bytes
1181 if (packing_index == 0) txb_hash_data[hash_data_index] = 0;
1182 chunk = prechunk << packing_index;
1183 packing_index += 2;
1184 txb_hash_data[hash_data_index] |= chunk;
1185
1186 // Full byte:
1187 if (packing_index == 8) {
1188 packing_index = 0;
1189 hash_data_index++;
Michelle Findlay-Olynykfbab0622017-12-13 14:10:56 -08001190 }
1191 }
Michelle Findlay-Olynykdea531d2017-12-13 14:10:56 -08001192 // Needed when packing_index != 0, to include final byte.
1193 hash_data_index++;
1194 assert(hash_data_index <= 64);
1195 // 31 bit qc_hash: index to array
1196 uint32_t hbt_qc_hash =
Peng Bin8a204cd2018-04-08 13:07:35 +08001197 av1_get_crc32c_value(&crc_calculator, txb_hash_data, hash_data_index);
Michelle Findlay-Olynykdea531d2017-12-13 14:10:56 -08001198
1199 // Make ctx_hash.
1200 hash_data_index = 0;
1201 tran_low_t prechunk;
1202
1203 for (int i = 0; i < txb_info->eob; i++) {
1204 // Save as magnitudes towards or away from zero.
1205 if (txb_info->tcoeff[scan[i]] >= 0)
1206 prechunk = txb_info->tcoeff[scan[i]] - txb_info->dqcoeff[scan[i]];
1207 else
1208 prechunk = txb_info->dqcoeff[scan[i]] - txb_info->tcoeff[scan[i]];
1209
1210 chunk = prechunk & 0xff;
1211 txb_hash_data[hash_data_index++] = chunk;
1212 }
1213
1214 // Extra ctx data:
1215 // Include dequants.
1216 txb_hash_data[hash_data_index++] = txb_info->dequant[0] & 0xff;
1217 txb_hash_data[hash_data_index++] = txb_info->dequant[1] & 0xff;
1218 chunk = txb_info->txb_ctx->txb_skip_ctx & 0xff;
1219 txb_hash_data[hash_data_index++] = chunk;
1220 chunk = txb_info->txb_ctx->dc_sign_ctx & 0xff;
1221 txb_hash_data[hash_data_index++] = chunk;
1222 // eob
1223 chunk = txb_info->eob & 0xff;
1224 txb_hash_data[hash_data_index++] = chunk;
1225 // rdmult (int64)
1226 chunk = txb_info->rdmult & 0xff;
1227 txb_hash_data[hash_data_index++] = chunk;
1228 // tx_type
1229 chunk = txb_info->tx_type & 0xff;
1230 txb_hash_data[hash_data_index++] = chunk;
Michelle Findlay-Olynykfbab0622017-12-13 14:10:56 -08001231 // base_eob_cost
1232 for (int i = 1; i < 3; i++) { // i = 0 are softened away
1233 for (int j = 0; j < SIG_COEF_CONTEXTS_EOB; j++) {
1234 chunk = (txb_costs->base_eob_cost[j][i] & 0xff00) >> 8;
1235 txb_hash_data[hash_data_index++] = chunk;
1236 }
Michelle Findlay-Olynykdea531d2017-12-13 14:10:56 -08001237 }
1238 // eob_cost
1239 for (int i = 0; i < 11; i++) {
1240 for (int j = 0; j < 2; j++) {
1241 chunk = (txb_eob_costs->eob_cost[j][i] & 0xff00) >> 8;
1242 txb_hash_data[hash_data_index++] = chunk;
1243 }
1244 }
1245 // dc_sign_cost
1246 for (int i = 0; i < 2; i++) {
1247 for (int j = 0; j < DC_SIGN_CONTEXTS; j++) {
1248 chunk = (txb_costs->dc_sign_cost[j][i] & 0xff00) >> 8;
1249 txb_hash_data[hash_data_index++] = chunk;
1250 }
1251 }
Michelle Findlay-Olynykfbab0622017-12-13 14:10:56 -08001252
Michelle Findlay-Olynykdea531d2017-12-13 14:10:56 -08001253 assert(hash_data_index <= 256);
1254 // 31 bit ctx_hash: used to index table
1255 uint32_t hbt_ctx_hash =
Peng Bin8a204cd2018-04-08 13:07:35 +08001256 av1_get_crc32c_value(&crc_calculator, txb_hash_data, hash_data_index);
Michelle Findlay-Olynykfbab0622017-12-13 14:10:56 -08001257 //// End hash creation
1258
Michelle Findlay-Olynykdea531d2017-12-13 14:10:56 -08001259 return hbt_search_match(hbt_ctx_hash, hbt_qc_hash, txb_info, txb_costs,
1260 txb_eob_costs, p, block, fast_mode, rate_cost);
Michelle Findlay-Olynykfbab0622017-12-13 14:10:56 -08001261}
1262
Katsuhisa Yuasa9f639962018-04-08 18:00:54 +09001263static AOM_FORCE_INLINE int get_coeff_cost_simple(
1264 int ci, tran_low_t abs_qc, int coeff_ctx,
1265 const LV_MAP_COEFF_COST *txb_costs, int bwl, TX_CLASS tx_class,
1266 const uint8_t *levels) {
Angie Chiang40b07312018-03-30 10:42:55 -07001267 // this simple version assumes the coeff's scan_idx is not DC (scan_idx != 0)
Angie Chiange4ea7482018-03-15 11:36:41 -07001268 // and not the last (scan_idx != eob - 1)
1269 assert(ci > 0);
1270 int cost = txb_costs->base_cost[coeff_ctx][AOMMIN(abs_qc, 3)];
1271 if (abs_qc) {
1272 cost += av1_cost_literal(1);
1273 if (abs_qc > NUM_BASE_LEVELS) {
Katsuhisa Yuasa55c95f82018-04-03 00:51:22 +09001274 const int br_ctx = get_br_ctx(levels, ci, bwl, tx_class);
Angie Chiange4ea7482018-03-15 11:36:41 -07001275 cost += get_br_cost(abs_qc, br_ctx, txb_costs->lps_cost[br_ctx]);
1276 cost += get_golomb_cost(abs_qc);
1277 }
1278 }
1279 return cost;
1280}
1281
1282static INLINE int get_coeff_cost_general(int is_last, int ci, tran_low_t abs_qc,
Angie Chiang40b07312018-03-30 10:42:55 -07001283 int sign, int coeff_ctx,
Angie Chiange4ea7482018-03-15 11:36:41 -07001284 int dc_sign_ctx,
1285 const LV_MAP_COEFF_COST *txb_costs,
Katsuhisa Yuasa55c95f82018-04-03 00:51:22 +09001286 int bwl, TX_CLASS tx_class,
Angie Chiange4ea7482018-03-15 11:36:41 -07001287 const uint8_t *levels) {
1288 int cost = 0;
1289 if (is_last) {
1290 cost += txb_costs->base_eob_cost[coeff_ctx][AOMMIN(abs_qc, 3) - 1];
1291 } else {
1292 cost += txb_costs->base_cost[coeff_ctx][AOMMIN(abs_qc, 3)];
1293 }
1294 if (abs_qc != 0) {
1295 if (ci == 0) {
Angie Chiang40b07312018-03-30 10:42:55 -07001296 cost += txb_costs->dc_sign_cost[dc_sign_ctx][sign];
Angie Chiange4ea7482018-03-15 11:36:41 -07001297 } else {
1298 cost += av1_cost_literal(1);
1299 }
1300 if (abs_qc > NUM_BASE_LEVELS) {
Katsuhisa Yuasa55c95f82018-04-03 00:51:22 +09001301 const int br_ctx = get_br_ctx(levels, ci, bwl, tx_class);
Angie Chiange4ea7482018-03-15 11:36:41 -07001302 cost += get_br_cost(abs_qc, br_ctx, txb_costs->lps_cost[br_ctx]);
1303 cost += get_golomb_cost(abs_qc);
1304 }
1305 }
1306 return cost;
1307}
1308
Angie Chiang40b07312018-03-30 10:42:55 -07001309static INLINE void get_qc_dqc_low(tran_low_t abs_qc, int sign, int dqv,
Angie Chiange4ea7482018-03-15 11:36:41 -07001310 int shift, tran_low_t *qc_low,
1311 tran_low_t *dqc_low) {
1312 tran_low_t abs_qc_low = abs_qc - 1;
Angie Chiang40b07312018-03-30 10:42:55 -07001313 *qc_low = (-sign ^ abs_qc_low) + sign;
1314 assert((sign ? -abs_qc_low : abs_qc_low) == *qc_low);
Angie Chiange4ea7482018-03-15 11:36:41 -07001315 tran_low_t abs_dqc_low = (abs_qc_low * dqv) >> shift;
Angie Chiang40b07312018-03-30 10:42:55 -07001316 *dqc_low = (-sign ^ abs_dqc_low) + sign;
1317 assert((sign ? -abs_dqc_low : abs_dqc_low) == *dqc_low);
Angie Chiange4ea7482018-03-15 11:36:41 -07001318}
1319
1320static INLINE void update_coeff_general(
1321 int *accu_rate, int64_t *accu_dist, int si, int eob, TX_SIZE tx_size,
Katsuhisa Yuasa55c95f82018-04-03 00:51:22 +09001322 TX_CLASS tx_class, int bwl, int height, int64_t rdmult, int shift,
Angie Chiange4ea7482018-03-15 11:36:41 -07001323 int dc_sign_ctx, const int16_t *dequant, const int16_t *scan,
1324 const LV_MAP_COEFF_COST *txb_costs, const tran_low_t *tcoeff,
1325 tran_low_t *qcoeff, tran_low_t *dqcoeff, uint8_t *levels) {
1326 const int dqv = dequant[si != 0];
1327 const int ci = scan[si];
1328 const tran_low_t qc = qcoeff[ci];
1329 const int is_last = si == (eob - 1);
1330 const int coeff_ctx = get_lower_levels_ctx_general(
Katsuhisa Yuasa55c95f82018-04-03 00:51:22 +09001331 is_last, si, bwl, height, levels, ci, tx_size, tx_class);
Angie Chiange4ea7482018-03-15 11:36:41 -07001332 if (qc == 0) {
1333 *accu_rate += txb_costs->base_cost[coeff_ctx][0];
1334 } else {
Angie Chiang40b07312018-03-30 10:42:55 -07001335 const int sign = (qc < 0) ? 1 : 0;
Angie Chiange4ea7482018-03-15 11:36:41 -07001336 const tran_low_t abs_qc = abs(qc);
1337 const tran_low_t tqc = tcoeff[ci];
1338 const tran_low_t dqc = dqcoeff[ci];
1339 const int64_t dist = get_coeff_dist(tqc, dqc, shift);
1340 const int64_t dist0 = get_coeff_dist(tqc, 0, shift);
1341 const int rate =
Angie Chiang40b07312018-03-30 10:42:55 -07001342 get_coeff_cost_general(is_last, ci, abs_qc, sign, coeff_ctx,
Katsuhisa Yuasa55c95f82018-04-03 00:51:22 +09001343 dc_sign_ctx, txb_costs, bwl, tx_class, levels);
Angie Chiange4ea7482018-03-15 11:36:41 -07001344 const int64_t rd = RDCOST(rdmult, rate, dist);
1345
1346 tran_low_t qc_low, dqc_low;
Angie Chiang40b07312018-03-30 10:42:55 -07001347 get_qc_dqc_low(abs_qc, sign, dqv, shift, &qc_low, &dqc_low);
Angie Chiange4ea7482018-03-15 11:36:41 -07001348 const tran_low_t abs_qc_low = abs_qc - 1;
1349 const int64_t dist_low = get_coeff_dist(tqc, dqc_low, shift);
1350 const int rate_low =
Angie Chiang40b07312018-03-30 10:42:55 -07001351 get_coeff_cost_general(is_last, ci, abs_qc_low, sign, coeff_ctx,
Katsuhisa Yuasa55c95f82018-04-03 00:51:22 +09001352 dc_sign_ctx, txb_costs, bwl, tx_class, levels);
Angie Chiange4ea7482018-03-15 11:36:41 -07001353 const int64_t rd_low = RDCOST(rdmult, rate_low, dist_low);
1354 if (rd_low < rd) {
1355 qcoeff[ci] = qc_low;
1356 dqcoeff[ci] = dqc_low;
1357 levels[get_padded_idx(ci, bwl)] = AOMMIN(abs_qc_low, INT8_MAX);
1358 *accu_rate += rate_low;
1359 *accu_dist += dist_low - dist0;
1360 } else {
1361 *accu_rate += rate;
1362 *accu_dist += dist - dist0;
1363 }
1364 }
1365}
1366
Katsuhisa Yuasa9f639962018-04-08 18:00:54 +09001367static AOM_FORCE_INLINE void update_coeff_simple(
Katsuhisa Yuasa55c95f82018-04-03 00:51:22 +09001368 int *accu_rate, int si, int eob, TX_SIZE tx_size, TX_CLASS tx_class,
1369 int bwl, int64_t rdmult, int shift, const int16_t *dequant,
1370 const int16_t *scan, const LV_MAP_COEFF_COST *txb_costs,
1371 const tran_low_t *tcoeff, tran_low_t *qcoeff, tran_low_t *dqcoeff,
1372 uint8_t *levels) {
Angie Chiange4ea7482018-03-15 11:36:41 -07001373 const int dqv = dequant[1];
1374 (void)eob;
Angie Chiang40b07312018-03-30 10:42:55 -07001375 // this simple version assumes the coeff's scan_idx is not DC (scan_idx != 0)
Angie Chiange4ea7482018-03-15 11:36:41 -07001376 // and not the last (scan_idx != eob - 1)
1377 assert(si != eob - 1);
1378 assert(si > 0);
1379 const int ci = scan[si];
1380 const tran_low_t qc = qcoeff[ci];
Katsuhisa Yuasa55c95f82018-04-03 00:51:22 +09001381 const int coeff_ctx =
1382 get_lower_levels_ctx(levels, ci, bwl, tx_size, tx_class);
Angie Chiange4ea7482018-03-15 11:36:41 -07001383 if (qc == 0) {
1384 *accu_rate += txb_costs->base_cost[coeff_ctx][0];
1385 } else {
1386 const tran_low_t abs_qc = abs(qc);
1387 const tran_low_t tqc = tcoeff[ci];
1388 const tran_low_t dqc = dqcoeff[ci];
Angie Chiange4ea7482018-03-15 11:36:41 -07001389 const int rate = get_coeff_cost_simple(ci, abs_qc, coeff_ctx, txb_costs,
Katsuhisa Yuasa55c95f82018-04-03 00:51:22 +09001390 bwl, tx_class, levels);
Jingning Han87f5c342018-04-10 23:10:12 -07001391 if (abs(dqc) < abs(tqc)) {
1392 *accu_rate += rate;
1393 return;
1394 }
1395 const int64_t dist = get_coeff_dist(tqc, dqc, shift);
Angie Chiange4ea7482018-03-15 11:36:41 -07001396 const int64_t rd = RDCOST(rdmult, rate, dist);
1397
Angie Chiang40b07312018-03-30 10:42:55 -07001398 const int sign = (qc < 0) ? 1 : 0;
Angie Chiange4ea7482018-03-15 11:36:41 -07001399 tran_low_t qc_low, dqc_low;
Angie Chiang40b07312018-03-30 10:42:55 -07001400 get_qc_dqc_low(abs_qc, sign, dqv, shift, &qc_low, &dqc_low);
Angie Chiange4ea7482018-03-15 11:36:41 -07001401 const tran_low_t abs_qc_low = abs_qc - 1;
1402 const int64_t dist_low = get_coeff_dist(tqc, dqc_low, shift);
Katsuhisa Yuasa55c95f82018-04-03 00:51:22 +09001403 const int rate_low = get_coeff_cost_simple(
1404 ci, abs_qc_low, coeff_ctx, txb_costs, bwl, tx_class, levels);
Angie Chiange4ea7482018-03-15 11:36:41 -07001405 const int64_t rd_low = RDCOST(rdmult, rate_low, dist_low);
1406 if (rd_low < rd) {
1407 qcoeff[ci] = qc_low;
1408 dqcoeff[ci] = dqc_low;
1409 levels[get_padded_idx(ci, bwl)] = AOMMIN(abs_qc_low, INT8_MAX);
1410 *accu_rate += rate_low;
Angie Chiange4ea7482018-03-15 11:36:41 -07001411 } else {
1412 *accu_rate += rate;
Angie Chiange4ea7482018-03-15 11:36:41 -07001413 }
1414 }
1415}
1416
Katsuhisa Yuasa9f639962018-04-08 18:00:54 +09001417static AOM_FORCE_INLINE void update_coeff_eob(
Angie Chiange4ea7482018-03-15 11:36:41 -07001418 int *accu_rate, int64_t *accu_dist, int *eob, int *nz_num, int *nz_ci,
Katsuhisa Yuasa55c95f82018-04-03 00:51:22 +09001419 int si, TX_SIZE tx_size, TX_CLASS tx_class, int bwl, int height,
Angie Chiange4ea7482018-03-15 11:36:41 -07001420 int dc_sign_ctx, int64_t rdmult, int shift, const int16_t *dequant,
1421 const int16_t *scan, const LV_MAP_EOB_COST *txb_eob_costs,
1422 const LV_MAP_COEFF_COST *txb_costs, const tran_low_t *tcoeff,
Jim Bankoski855ac462018-06-07 09:05:00 -07001423 tran_low_t *qcoeff, tran_low_t *dqcoeff, uint8_t *levels, int sharpness) {
Angie Chiange4ea7482018-03-15 11:36:41 -07001424 const int dqv = dequant[si != 0];
1425 assert(si != *eob - 1);
1426 const int ci = scan[si];
1427 const tran_low_t qc = qcoeff[ci];
Katsuhisa Yuasa55c95f82018-04-03 00:51:22 +09001428 const int coeff_ctx =
1429 get_lower_levels_ctx(levels, ci, bwl, tx_size, tx_class);
Angie Chiange4ea7482018-03-15 11:36:41 -07001430 if (qc == 0) {
1431 *accu_rate += txb_costs->base_cost[coeff_ctx][0];
1432 } else {
1433 int lower_level = 0;
1434 const tran_low_t abs_qc = abs(qc);
1435 const tran_low_t tqc = tcoeff[ci];
1436 const tran_low_t dqc = dqcoeff[ci];
Angie Chiang40b07312018-03-30 10:42:55 -07001437 const int sign = (qc < 0) ? 1 : 0;
Angie Chiange4ea7482018-03-15 11:36:41 -07001438 const int64_t dist0 = get_coeff_dist(tqc, 0, shift);
1439 int64_t dist = get_coeff_dist(tqc, dqc, shift) - dist0;
1440 int rate =
Angie Chiang40b07312018-03-30 10:42:55 -07001441 get_coeff_cost_general(0, ci, abs_qc, sign, coeff_ctx, dc_sign_ctx,
Katsuhisa Yuasa55c95f82018-04-03 00:51:22 +09001442 txb_costs, bwl, tx_class, levels);
Angie Chiange4ea7482018-03-15 11:36:41 -07001443 int64_t rd = RDCOST(rdmult, *accu_rate + rate, *accu_dist + dist);
1444
1445 tran_low_t qc_low, dqc_low;
Angie Chiang40b07312018-03-30 10:42:55 -07001446 get_qc_dqc_low(abs_qc, sign, dqv, shift, &qc_low, &dqc_low);
Angie Chiange4ea7482018-03-15 11:36:41 -07001447 const tran_low_t abs_qc_low = abs_qc - 1;
1448 const int64_t dist_low = get_coeff_dist(tqc, dqc_low, shift) - dist0;
1449 const int rate_low =
Angie Chiang40b07312018-03-30 10:42:55 -07001450 get_coeff_cost_general(0, ci, abs_qc_low, sign, coeff_ctx, dc_sign_ctx,
Katsuhisa Yuasa55c95f82018-04-03 00:51:22 +09001451 txb_costs, bwl, tx_class, levels);
Angie Chiange4ea7482018-03-15 11:36:41 -07001452 const int64_t rd_low =
1453 RDCOST(rdmult, *accu_rate + rate_low, *accu_dist + dist_low);
1454
1455 int lower_level_new_eob = 0;
1456 const int new_eob = si + 1;
1457 uint8_t tmp_levels[3];
1458 for (int ni = 0; ni < *nz_num; ++ni) {
1459 const int last_ci = nz_ci[ni];
1460 tmp_levels[ni] = levels[get_padded_idx(last_ci, bwl)];
1461 levels[get_padded_idx(last_ci, bwl)] = 0;
1462 }
1463
1464 const int coeff_ctx_new_eob = get_lower_levels_ctx_general(
Katsuhisa Yuasa55c95f82018-04-03 00:51:22 +09001465 1, si, bwl, height, levels, ci, tx_size, tx_class);
Angie Chiange4ea7482018-03-15 11:36:41 -07001466 const int new_eob_cost =
Katsuhisa Yuasa55c95f82018-04-03 00:51:22 +09001467 get_eob_cost(new_eob, txb_eob_costs, txb_costs, tx_class);
Angie Chiange4ea7482018-03-15 11:36:41 -07001468 int rate_coeff_eob =
Angie Chiang40b07312018-03-30 10:42:55 -07001469 new_eob_cost + get_coeff_cost_general(1, ci, abs_qc, sign,
Angie Chiange4ea7482018-03-15 11:36:41 -07001470 coeff_ctx_new_eob, dc_sign_ctx,
Katsuhisa Yuasa55c95f82018-04-03 00:51:22 +09001471 txb_costs, bwl, tx_class, levels);
Angie Chiange4ea7482018-03-15 11:36:41 -07001472 int64_t dist_new_eob = dist;
1473 int64_t rd_new_eob = RDCOST(rdmult, rate_coeff_eob, dist_new_eob);
1474
1475 if (abs_qc_low > 0) {
1476 const int rate_coeff_eob_low =
1477 new_eob_cost +
Angie Chiang40b07312018-03-30 10:42:55 -07001478 get_coeff_cost_general(1, ci, abs_qc_low, sign, coeff_ctx_new_eob,
Katsuhisa Yuasa55c95f82018-04-03 00:51:22 +09001479 dc_sign_ctx, txb_costs, bwl, tx_class, levels);
Angie Chiange4ea7482018-03-15 11:36:41 -07001480 const int64_t dist_new_eob_low = dist_low;
1481 const int64_t rd_new_eob_low =
1482 RDCOST(rdmult, rate_coeff_eob_low, dist_new_eob_low);
1483 if (rd_new_eob_low < rd_new_eob) {
1484 lower_level_new_eob = 1;
1485 rd_new_eob = rd_new_eob_low;
1486 rate_coeff_eob = rate_coeff_eob_low;
1487 dist_new_eob = dist_new_eob_low;
1488 }
1489 }
1490
1491 if (rd_low < rd) {
1492 lower_level = 1;
1493 rd = rd_low;
1494 rate = rate_low;
1495 dist = dist_low;
1496 }
1497
Jim Bankoski855ac462018-06-07 09:05:00 -07001498 if (sharpness == 0 && rd_new_eob < rd) {
Angie Chiange4ea7482018-03-15 11:36:41 -07001499 for (int ni = 0; ni < *nz_num; ++ni) {
1500 int last_ci = nz_ci[ni];
1501 // levels[get_padded_idx(last_ci, bwl)] = 0;
1502 qcoeff[last_ci] = 0;
1503 dqcoeff[last_ci] = 0;
1504 }
1505 *eob = new_eob;
1506 *nz_num = 0;
1507 *accu_rate = rate_coeff_eob;
1508 *accu_dist = dist_new_eob;
1509 lower_level = lower_level_new_eob;
1510 } else {
1511 for (int ni = 0; ni < *nz_num; ++ni) {
1512 const int last_ci = nz_ci[ni];
1513 levels[get_padded_idx(last_ci, bwl)] = tmp_levels[ni];
1514 }
1515 *accu_rate += rate;
1516 *accu_dist += dist;
1517 }
1518
1519 if (lower_level) {
1520 qcoeff[ci] = qc_low;
1521 dqcoeff[ci] = dqc_low;
1522 levels[get_padded_idx(ci, bwl)] = AOMMIN(abs_qc_low, INT8_MAX);
1523 }
1524 if (qcoeff[ci]) {
1525 nz_ci[*nz_num] = ci;
1526 ++*nz_num;
1527 }
1528 }
1529}
1530
Angie Chiangd0397e12018-03-27 19:03:20 -07001531static INLINE void update_skip(int *accu_rate, int64_t accu_dist, int *eob,
1532 int nz_num, int *nz_ci, int64_t rdmult,
1533 int skip_cost, int non_skip_cost,
Jim Bankoski855ac462018-06-07 09:05:00 -07001534 tran_low_t *qcoeff, tran_low_t *dqcoeff,
1535 int sharpness) {
Angie Chiangd0397e12018-03-27 19:03:20 -07001536 const int64_t rd = RDCOST(rdmult, *accu_rate + non_skip_cost, accu_dist);
Angie Chiange4ea7482018-03-15 11:36:41 -07001537 const int64_t rd_new_eob = RDCOST(rdmult, skip_cost, 0);
Jim Bankoski855ac462018-06-07 09:05:00 -07001538 if (sharpness == 0 && rd_new_eob < rd) {
Angie Chiangd0397e12018-03-27 19:03:20 -07001539 for (int i = 0; i < nz_num; ++i) {
1540 const int ci = nz_ci[i];
Angie Chiange4ea7482018-03-15 11:36:41 -07001541 qcoeff[ci] = 0;
1542 dqcoeff[ci] = 0;
Angie Chiangd0397e12018-03-27 19:03:20 -07001543 // no need to set up levels because this is the last step
1544 // levels[get_padded_idx(ci, bwl)] = 0;
Angie Chiange4ea7482018-03-15 11:36:41 -07001545 }
Angie Chiange4ea7482018-03-15 11:36:41 -07001546 *accu_rate = 0;
1547 *eob = 0;
1548 }
1549}
1550
1551int av1_optimize_txb_new(const struct AV1_COMP *cpi, MACROBLOCK *x, int plane,
Xing Jinbd91e942018-06-21 13:43:17 +08001552 int block, TX_SIZE tx_size, TX_TYPE tx_type,
1553 const TXB_CTX *const txb_ctx, int *rate_cost,
1554 int sharpness) {
Angie Chiange4ea7482018-03-15 11:36:41 -07001555 const AV1_COMMON *cm = &cpi->common;
1556 MACROBLOCKD *xd = &x->e_mbd;
1557 const PLANE_TYPE plane_type = get_plane_type(plane);
1558 const TX_SIZE txs_ctx = get_txsize_entropy_ctx(tx_size);
Katsuhisa Yuasa55c95f82018-04-03 00:51:22 +09001559 const TX_CLASS tx_class = tx_type_to_class[tx_type];
Yue Chen53b53f02018-03-29 14:31:23 -07001560 const MB_MODE_INFO *mbmi = xd->mi[0];
Angie Chiange4ea7482018-03-15 11:36:41 -07001561 const struct macroblock_plane *p = &x->plane[plane];
1562 struct macroblockd_plane *pd = &xd->plane[plane];
1563 tran_low_t *qcoeff = BLOCK_OFFSET(p->qcoeff, block);
1564 tran_low_t *dqcoeff = BLOCK_OFFSET(pd->dqcoeff, block);
1565 const tran_low_t *tcoeff = BLOCK_OFFSET(p->coeff, block);
1566 const int16_t *dequant = p->dequant_QTX;
1567 const int bwl = get_txb_bwl(tx_size);
1568 const int width = get_txb_wide(tx_size);
1569 const int height = get_txb_high(tx_size);
1570 assert(width == (1 << bwl));
1571 const int is_inter = is_inter_block(mbmi);
1572 const SCAN_ORDER *scan_order = get_scan(tx_size, tx_type);
1573 const int16_t *scan = scan_order->scan;
1574 const LV_MAP_COEFF_COST *txb_costs = &x->coeff_costs[txs_ctx][plane_type];
1575 const int eob_multi_size = txsize_log2_minus4[tx_size];
1576 const LV_MAP_EOB_COST *txb_eob_costs =
1577 &x->eob_costs[eob_multi_size][plane_type];
1578
1579 const int shift = av1_get_tx_scale(tx_size);
1580 const int64_t rdmult =
1581 ((x->rdmult * plane_rd_mult[is_inter][plane_type] << (2 * (xd->bd - 8))) +
1582 2) >>
Jim Bankoski855ac462018-06-07 09:05:00 -07001583 (sharpness + (cpi->oxcf.aq_mode == VARIANCE_AQ && mbmi->segment_id < 4
1584 ? 7 - mbmi->segment_id
1585 : 2));
Angie Chiange4ea7482018-03-15 11:36:41 -07001586
1587 uint8_t levels_buf[TX_PAD_2D];
1588 uint8_t *const levels = set_levels(levels_buf, width);
Jim Bankoski855ac462018-06-07 09:05:00 -07001589
Angie Chiange4ea7482018-03-15 11:36:41 -07001590 av1_txb_init_levels(qcoeff, width, height, levels);
1591
1592 // TODO(angirbird): check iqmatrix
1593
1594 const int non_skip_cost = txb_costs->txb_skip_cost[txb_ctx->txb_skip_ctx][0];
1595 const int skip_cost = txb_costs->txb_skip_cost[txb_ctx->txb_skip_ctx][1];
1596 int eob = p->eobs[block];
Katsuhisa Yuasa55c95f82018-04-03 00:51:22 +09001597 const int eob_cost = get_eob_cost(eob, txb_eob_costs, txb_costs, tx_class);
Angie Chiange4ea7482018-03-15 11:36:41 -07001598 int accu_rate = eob_cost;
1599 int64_t accu_dist = 0;
1600 int si = eob - 1;
1601 const int ci = scan[si];
1602 const tran_low_t qc = qcoeff[ci];
1603 const tran_low_t abs_qc = abs(qc);
Angie Chiang40b07312018-03-30 10:42:55 -07001604 const int sign = qc < 0;
Angie Chiange4ea7482018-03-15 11:36:41 -07001605 const int max_nz_num = 2;
1606 int nz_num = 1;
1607 int nz_ci[3] = { ci, 0, 0 };
1608 if (abs_qc >= 2) {
Katsuhisa Yuasa55c95f82018-04-03 00:51:22 +09001609 update_coeff_general(&accu_rate, &accu_dist, si, eob, tx_size, tx_class,
1610 bwl, height, rdmult, shift, txb_ctx->dc_sign_ctx,
1611 dequant, scan, txb_costs, tcoeff, qcoeff, dqcoeff,
1612 levels);
Angie Chiange4ea7482018-03-15 11:36:41 -07001613 --si;
1614 } else {
1615 assert(abs_qc == 1);
1616 const int coeff_ctx = get_lower_levels_ctx_general(
Katsuhisa Yuasa55c95f82018-04-03 00:51:22 +09001617 1, si, bwl, height, levels, ci, tx_size, tx_class);
Angie Chiang40b07312018-03-30 10:42:55 -07001618 accu_rate += get_coeff_cost_general(1, ci, abs_qc, sign, coeff_ctx,
Angie Chiange4ea7482018-03-15 11:36:41 -07001619 txb_ctx->dc_sign_ctx, txb_costs, bwl,
Katsuhisa Yuasa55c95f82018-04-03 00:51:22 +09001620 tx_class, levels);
Angie Chiange4ea7482018-03-15 11:36:41 -07001621 const tran_low_t tqc = tcoeff[ci];
1622 const tran_low_t dqc = dqcoeff[ci];
1623 const int64_t dist = get_coeff_dist(tqc, dqc, shift);
1624 const int64_t dist0 = get_coeff_dist(tqc, 0, shift);
1625 accu_dist += dist - dist0;
1626 --si;
1627 }
1628
Katsuhisa Yuasa9f639962018-04-08 18:00:54 +09001629#define UPDATE_COEFF_EOB_CASE(tx_class_literal) \
1630 case tx_class_literal: \
1631 for (; si >= 0 && nz_num <= max_nz_num; --si) { \
1632 update_coeff_eob(&accu_rate, &accu_dist, &eob, &nz_num, nz_ci, si, \
1633 tx_size, tx_class_literal, bwl, height, \
1634 txb_ctx->dc_sign_ctx, rdmult, shift, dequant, scan, \
1635 txb_eob_costs, txb_costs, tcoeff, qcoeff, dqcoeff, \
Jim Bankoski855ac462018-06-07 09:05:00 -07001636 levels, sharpness); \
Katsuhisa Yuasa9f639962018-04-08 18:00:54 +09001637 } \
1638 break;
1639 switch (tx_class) {
1640 UPDATE_COEFF_EOB_CASE(TX_CLASS_2D);
1641 UPDATE_COEFF_EOB_CASE(TX_CLASS_HORIZ);
1642 UPDATE_COEFF_EOB_CASE(TX_CLASS_VERT);
1643#undef UPDATE_COEFF_EOB_CASE
1644 default: assert(false);
Angie Chiange4ea7482018-03-15 11:36:41 -07001645 }
1646
Angie Chiangd0397e12018-03-27 19:03:20 -07001647 if (si == -1 && nz_num <= max_nz_num) {
1648 update_skip(&accu_rate, accu_dist, &eob, nz_num, nz_ci, rdmult, skip_cost,
Jim Bankoski855ac462018-06-07 09:05:00 -07001649 non_skip_cost, qcoeff, dqcoeff, sharpness);
Angie Chiangd0397e12018-03-27 19:03:20 -07001650 }
1651
Katsuhisa Yuasa9f639962018-04-08 18:00:54 +09001652#define UPDATE_COEFF_SIMPLE_CASE(tx_class_literal) \
1653 case tx_class_literal: \
1654 for (; si >= 1; --si) { \
1655 update_coeff_simple(&accu_rate, si, eob, tx_size, tx_class_literal, bwl, \
1656 rdmult, shift, dequant, scan, txb_costs, tcoeff, \
1657 qcoeff, dqcoeff, levels); \
1658 } \
1659 break;
1660 switch (tx_class) {
1661 UPDATE_COEFF_SIMPLE_CASE(TX_CLASS_2D);
1662 UPDATE_COEFF_SIMPLE_CASE(TX_CLASS_HORIZ);
1663 UPDATE_COEFF_SIMPLE_CASE(TX_CLASS_VERT);
1664#undef UPDATE_COEFF_SIMPLE_CASE
1665 default: assert(false);
Angie Chiange4ea7482018-03-15 11:36:41 -07001666 }
1667
1668 // DC position
1669 if (si == 0) {
Angie Chiangd0397e12018-03-27 19:03:20 -07001670 // no need to update accu_dist because it's not used after this point
1671 int64_t dummy_dist = 0;
Katsuhisa Yuasa55c95f82018-04-03 00:51:22 +09001672 update_coeff_general(&accu_rate, &dummy_dist, si, eob, tx_size, tx_class,
Angie Chiangd0397e12018-03-27 19:03:20 -07001673 bwl, height, rdmult, shift, txb_ctx->dc_sign_ctx,
1674 dequant, scan, txb_costs, tcoeff, qcoeff, dqcoeff,
1675 levels);
Angie Chiange4ea7482018-03-15 11:36:41 -07001676 }
1677
Urvang Joshi553096a2018-05-10 15:27:14 -07001678 const int tx_type_cost = get_tx_type_cost(cm, x, xd, plane, tx_size, tx_type);
Angie Chiange4ea7482018-03-15 11:36:41 -07001679 if (eob == 0)
1680 accu_rate += skip_cost;
1681 else
1682 accu_rate += non_skip_cost + tx_type_cost;
1683
1684 p->eobs[block] = eob;
1685 p->txb_entropy_ctx[block] =
1686 av1_get_txb_entropy_context(qcoeff, scan_order, p->eobs[block]);
1687
1688 *rate_cost = accu_rate;
1689 return eob;
1690}
1691
Angie Chiang40b07312018-03-30 10:42:55 -07001692// This function is deprecated, but we keep it here because hash trellis
Angie Chiange4ea7482018-03-15 11:36:41 -07001693// is not integrated with av1_optimize_txb_new yet
Yaowu Xuefcf1e92018-01-12 17:05:46 -08001694int av1_optimize_txb(const struct AV1_COMP *cpi, MACROBLOCK *x, int plane,
Jingning Han7eab9ff2017-07-06 10:12:54 -07001695 int blk_row, int blk_col, int block, TX_SIZE tx_size,
Cheng Chen82775f62018-01-18 12:09:54 -08001696 TXB_CTX *txb_ctx, int fast_mode, int *rate_cost) {
Michelle Findlay-Olynykfbab0622017-12-13 14:10:56 -08001697 const AV1_COMMON *cm = &cpi->common;
Angie Chiang07c57f32017-05-30 18:18:33 -07001698 MACROBLOCKD *const xd = &x->e_mbd;
1699 const PLANE_TYPE plane_type = get_plane_type(plane);
Debargha Mukherjeeb3eda2f2017-11-28 16:00:20 -08001700 const TX_SIZE txs_ctx = get_txsize_entropy_ctx(tx_size);
Sarah Parker7c71cc02018-01-29 12:27:58 -08001701 const TX_TYPE tx_type = av1_get_tx_type(plane_type, xd, blk_row, blk_col,
1702 tx_size, cm->reduced_tx_set_used);
Yue Chen53b53f02018-03-29 14:31:23 -07001703 const MB_MODE_INFO *mbmi = xd->mi[0];
Angie Chiang07c57f32017-05-30 18:18:33 -07001704 const struct macroblock_plane *p = &x->plane[plane];
1705 struct macroblockd_plane *pd = &xd->plane[plane];
1706 const int eob = p->eobs[block];
1707 tran_low_t *qcoeff = BLOCK_OFFSET(p->qcoeff, block);
1708 tran_low_t *dqcoeff = BLOCK_OFFSET(pd->dqcoeff, block);
1709 const tran_low_t *tcoeff = BLOCK_OFFSET(p->coeff, block);
Monty Montgomery125c0fc2017-10-26 00:44:35 -04001710 const int16_t *dequant = p->dequant_QTX;
Urvang Joshi80893152017-10-27 11:51:14 -07001711 const int seg_eob = av1_get_max_eob(tx_size);
Angie Chianga9ba58e2017-12-01 19:22:43 -08001712 const int bwl = get_txb_bwl(tx_size);
1713 const int width = get_txb_wide(tx_size);
1714 const int height = get_txb_high(tx_size);
Angie Chiang07c57f32017-05-30 18:18:33 -07001715 const int is_inter = is_inter_block(mbmi);
Yaowu Xuf1e12932018-02-26 15:50:09 -08001716 const SCAN_ORDER *const scan_order = get_scan(tx_size, tx_type);
Angie Chiange15d2e32018-03-02 15:49:06 -08001717 const LV_MAP_COEFF_COST *txb_costs = &x->coeff_costs[txs_ctx][plane_type];
Dake He0db7d0e2017-12-21 15:23:20 -08001718 const int eob_multi_size = txsize_log2_minus4[tx_size];
1719 const LV_MAP_EOB_COST txb_eob_costs =
1720 x->eob_costs[eob_multi_size][plane_type];
Angie Chiang07c57f32017-05-30 18:18:33 -07001721
1722 const int shift = av1_get_tx_scale(tx_size);
1723 const int64_t rdmult =
Jingning Hanb433f4c2017-11-17 15:43:59 -08001724 ((x->rdmult * plane_rd_mult[is_inter][plane_type] << (2 * (xd->bd - 8))) +
1725 2) >>
1726 2;
Linfeng Zhang679d81e2017-10-31 15:27:42 -07001727 uint8_t levels_buf[TX_PAD_2D];
1728 uint8_t *const levels = set_levels(levels_buf, width);
Angie Chiangb3167a62018-01-30 19:37:57 -08001729 const TX_SIZE qm_tx_size = av1_get_adjusted_tx_size(tx_size);
1730 const qm_val_t *iqmatrix =
1731 IS_2D_TRANSFORM(tx_type)
1732 ? pd->seg_iqmatrix[mbmi->segment_id][qm_tx_size]
1733 : cm->giqmatrix[NUM_QM_LEVELS - 1][0][qm_tx_size];
Linfeng Zhang1122d7d2017-10-31 15:30:28 -07001734 assert(width == (1 << bwl));
Urvang Joshi553096a2018-05-10 15:27:14 -07001735 const int tx_type_cost = get_tx_type_cost(cm, x, xd, plane, tx_size, tx_type);
Linfeng Zhang1015a342017-10-24 16:20:41 -07001736 TxbInfo txb_info = {
Angie Chiang0ed53352018-03-08 12:53:43 -08001737 qcoeff, levels, dqcoeff, tcoeff, dequant, shift,
1738 tx_size, txs_ctx, tx_type, bwl, width, height,
1739 eob, seg_eob, scan_order, txb_ctx, rdmult, &cm->coeff_ctx_table,
1740 iqmatrix, tx_type_cost,
Linfeng Zhang1015a342017-10-24 16:20:41 -07001741 };
1742
Michelle Findlay-Olynykfbab0622017-12-13 14:10:56 -08001743 // Hash based trellis (hbt) speed feature: avoid expensive optimize_txb calls
Michelle Findlay-Olynykdea531d2017-12-13 14:10:56 -08001744 // by storing the coefficient deltas in a hash table.
Michelle Findlay-Olynykfbab0622017-12-13 14:10:56 -08001745 // Currently disabled in speedfeatures.c
Michelle Findlay-Olynykdea531d2017-12-13 14:10:56 -08001746 if (eob <= HBT_EOB && eob > 0 && cpi->sf.use_hash_based_trellis) {
Angie Chiange15d2e32018-03-02 15:49:06 -08001747 return hbt_create_hashes(&txb_info, txb_costs, &txb_eob_costs, p, block,
Michelle Findlay-Olynykdea531d2017-12-13 14:10:56 -08001748 fast_mode, rate_cost);
Michelle Findlay-Olynykfbab0622017-12-13 14:10:56 -08001749 }
1750
Linfeng Zhang1122d7d2017-10-31 15:30:28 -07001751 av1_txb_init_levels(qcoeff, width, height, levels);
Urvang Joshi70006e42017-06-14 16:08:55 -07001752
Angie Chiang45385b82018-03-02 15:25:58 -08001753 const int update =
Angie Chiange15d2e32018-03-02 15:49:06 -08001754 optimize_txb(&txb_info, txb_costs, &txb_eob_costs, rate_cost);
Angie Chiang07c57f32017-05-30 18:18:33 -07001755
Jingning Hand7e99112017-12-13 09:47:45 -08001756 if (update) {
1757 p->eobs[block] = txb_info.eob;
1758 p->txb_entropy_ctx[block] =
1759 av1_get_txb_entropy_context(qcoeff, scan_order, txb_info.eob);
1760 }
Angie Chiang07c57f32017-05-30 18:18:33 -07001761 return txb_info.eob;
1762}
Jingning Hand7e99112017-12-13 09:47:45 -08001763
Angie Chiang74e23072017-03-24 14:54:23 -07001764int av1_get_txb_entropy_context(const tran_low_t *qcoeff,
1765 const SCAN_ORDER *scan_order, int eob) {
Linfeng Zhangdb41d1e2017-12-05 11:06:20 -08001766 const int16_t *const scan = scan_order->scan;
Angie Chiang74e23072017-03-24 14:54:23 -07001767 int cul_level = 0;
1768 int c;
Jingning Han339cf932017-09-18 10:17:02 -07001769
1770 if (eob == 0) return 0;
Angie Chiang74e23072017-03-24 14:54:23 -07001771 for (c = 0; c < eob; ++c) {
1772 cul_level += abs(qcoeff[scan[c]]);
Jingning Hane3137652018-04-02 11:53:47 -07001773 if (cul_level > COEFF_CONTEXT_MASK) break;
Angie Chiang74e23072017-03-24 14:54:23 -07001774 }
1775
1776 cul_level = AOMMIN(COEFF_CONTEXT_MASK, cul_level);
1777 set_dc_sign(&cul_level, qcoeff[0]);
1778
1779 return cul_level;
1780}
1781
Jingning Han4fe5f672017-05-19 15:46:07 -07001782void av1_update_txb_context_b(int plane, int block, int blk_row, int blk_col,
1783 BLOCK_SIZE plane_bsize, TX_SIZE tx_size,
1784 void *arg) {
Jingning Han6171ae72017-05-18 20:15:06 -07001785 struct tokenize_b_args *const args = arg;
Angie Chiang36d616b2017-03-22 13:58:36 -07001786 const AV1_COMP *cpi = args->cpi;
1787 const AV1_COMMON *cm = &cpi->common;
Angie Chiang0397eda2017-03-15 16:57:14 -07001788 ThreadData *const td = args->td;
1789 MACROBLOCK *const x = &td->mb;
1790 MACROBLOCKD *const xd = &x->e_mbd;
1791 struct macroblock_plane *p = &x->plane[plane];
1792 struct macroblockd_plane *pd = &xd->plane[plane];
Angie Chiang36d616b2017-03-22 13:58:36 -07001793 const uint16_t eob = p->eobs[block];
1794 const tran_low_t *qcoeff = BLOCK_OFFSET(p->qcoeff, block);
1795 const PLANE_TYPE plane_type = pd->plane_type;
Sarah Parker7c71cc02018-01-29 12:27:58 -08001796 const TX_TYPE tx_type = av1_get_tx_type(plane_type, xd, blk_row, blk_col,
1797 tx_size, cm->reduced_tx_set_used);
Yaowu Xuf1e12932018-02-26 15:50:09 -08001798 const SCAN_ORDER *const scan_order = get_scan(tx_size, tx_type);
Hui Sue674dec2018-04-02 13:02:08 -07001799 const int cul_level = av1_get_txb_entropy_context(qcoeff, scan_order, eob);
1800 av1_set_contexts(xd, pd, plane, plane_bsize, tx_size, cul_level, blk_col,
1801 blk_row);
Angie Chiang0397eda2017-03-15 16:57:14 -07001802}
1803
Urvang Joshi553096a2018-05-10 15:27:14 -07001804static void update_tx_type_count(const AV1_COMMON *cm, MACROBLOCKD *xd,
1805 int blk_row, int blk_col, int plane,
1806 TX_SIZE tx_size, FRAME_COUNTS *counts,
1807 uint8_t allow_update_cdf) {
1808 MB_MODE_INFO *mbmi = xd->mi[0];
1809 int is_inter = is_inter_block(mbmi);
1810 FRAME_CONTEXT *fc = xd->tile_ctx;
1811#if !CONFIG_ENTROPY_STATS
1812 (void)counts;
1813#endif // !CONFIG_ENTROPY_STATS
1814
1815 // Only y plane's tx_type is updated
1816 if (plane > 0) return;
1817 TX_TYPE tx_type = av1_get_tx_type(PLANE_TYPE_Y, xd, blk_row, blk_col, tx_size,
1818 cm->reduced_tx_set_used);
1819 if (get_ext_tx_types(tx_size, is_inter, cm->reduced_tx_set_used) > 1 &&
1820 cm->base_qindex > 0 && !mbmi->skip &&
1821 !segfeature_active(&cm->seg, mbmi->segment_id, SEG_LVL_SKIP)) {
1822 const int eset = get_ext_tx_set(tx_size, is_inter, cm->reduced_tx_set_used);
1823 if (eset > 0) {
1824 const TxSetType tx_set_type =
1825 av1_get_ext_tx_set_type(tx_size, is_inter, cm->reduced_tx_set_used);
1826 if (is_inter) {
1827 if (allow_update_cdf) {
1828 update_cdf(fc->inter_ext_tx_cdf[eset][txsize_sqr_map[tx_size]],
1829 av1_ext_tx_ind[tx_set_type][tx_type],
1830 av1_num_ext_tx_set[tx_set_type]);
1831 }
1832#if CONFIG_ENTROPY_STATS
1833 ++counts->inter_ext_tx[eset][txsize_sqr_map[tx_size]]
1834 [av1_ext_tx_ind[tx_set_type][tx_type]];
1835#endif // CONFIG_ENTROPY_STATS
1836 } else {
1837 PREDICTION_MODE intra_dir;
1838 if (mbmi->filter_intra_mode_info.use_filter_intra)
1839 intra_dir = fimode_to_intradir[mbmi->filter_intra_mode_info
1840 .filter_intra_mode];
1841 else
1842 intra_dir = mbmi->mode;
1843#if CONFIG_ENTROPY_STATS
1844 ++counts->intra_ext_tx[eset][txsize_sqr_map[tx_size]][intra_dir]
1845 [av1_ext_tx_ind[tx_set_type][tx_type]];
1846#endif // CONFIG_ENTROPY_STATS
1847 if (allow_update_cdf) {
1848 update_cdf(
1849 fc->intra_ext_tx_cdf[eset][txsize_sqr_map[tx_size]][intra_dir],
1850 av1_ext_tx_ind[tx_set_type][tx_type],
1851 av1_num_ext_tx_set[tx_set_type]);
1852 }
1853 }
1854 }
1855 }
1856}
1857
Jingning Han4fe5f672017-05-19 15:46:07 -07001858void av1_update_and_record_txb_context(int plane, int block, int blk_row,
1859 int blk_col, BLOCK_SIZE plane_bsize,
1860 TX_SIZE tx_size, void *arg) {
Jingning Han6171ae72017-05-18 20:15:06 -07001861 struct tokenize_b_args *const args = arg;
Angie Chiang0397eda2017-03-15 16:57:14 -07001862 const AV1_COMP *cpi = args->cpi;
1863 const AV1_COMMON *cm = &cpi->common;
1864 ThreadData *const td = args->td;
1865 MACROBLOCK *const x = &td->mb;
1866 MACROBLOCKD *const xd = &x->e_mbd;
1867 struct macroblock_plane *p = &x->plane[plane];
1868 struct macroblockd_plane *pd = &xd->plane[plane];
Yue Chen53b53f02018-03-29 14:31:23 -07001869 MB_MODE_INFO *mbmi = xd->mi[0];
Hui Su8763cd32018-04-02 12:29:05 -07001870 const int eob = p->eobs[block];
Angie Chiang85901562017-03-17 12:03:27 -07001871 TXB_CTX txb_ctx;
1872 get_txb_ctx(plane_bsize, tx_size, plane, pd->above_context + blk_col,
1873 pd->left_context + blk_row, &txb_ctx);
Angie Chianga9ba58e2017-12-01 19:22:43 -08001874 const int bwl = get_txb_bwl(tx_size);
1875 const int width = get_txb_wide(tx_size);
1876 const int height = get_txb_high(tx_size);
Yunqing Wang0e141b52017-11-02 15:08:58 -07001877 const uint8_t allow_update_cdf = args->allow_update_cdf;
Hui Su8763cd32018-04-02 12:29:05 -07001878 const TX_SIZE txsize_ctx = get_txsize_entropy_ctx(tx_size);
Jingning Han8f661602017-08-19 08:16:50 -07001879 FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
Yue Chen198738d2018-03-08 17:26:01 -08001880#if CONFIG_ENTROPY_STATS
1881 int cdf_idx = cm->coef_cdf_category;
1882#endif // CONFIG_ENTROPY_STATS
Jingning Han48be0e12017-06-13 12:12:01 -07001883
Hui Su1e959892018-01-22 12:14:43 -08001884#if CONFIG_ENTROPY_STATS
Yue Chen198738d2018-03-08 17:26:01 -08001885 ++td->counts->txb_skip[cdf_idx][txsize_ctx][txb_ctx.txb_skip_ctx][eob == 0];
Hui Su1e959892018-01-22 12:14:43 -08001886#endif // CONFIG_ENTROPY_STATS
Hui Su41d61522018-01-23 10:47:55 -08001887 if (allow_update_cdf) {
1888 update_cdf(ec_ctx->txb_skip_cdf[txsize_ctx][txb_ctx.txb_skip_ctx], eob == 0,
Yunqing Wang0e141b52017-11-02 15:08:58 -07001889 2);
Hui Su41d61522018-01-23 10:47:55 -08001890 }
Angie Chiang0397eda2017-03-15 16:57:14 -07001891
Hui Su8763cd32018-04-02 12:29:05 -07001892 x->mbmi_ext->txb_skip_ctx[plane][block] = txb_ctx.txb_skip_ctx;
Angie Chiang0397eda2017-03-15 16:57:14 -07001893 x->mbmi_ext->eobs[plane][block] = eob;
1894
1895 if (eob == 0) {
Hui Sue674dec2018-04-02 13:02:08 -07001896 av1_set_contexts(xd, pd, plane, plane_bsize, tx_size, 0, blk_col, blk_row);
Angie Chiang0397eda2017-03-15 16:57:14 -07001897 return;
1898 }
1899
Hui Su8763cd32018-04-02 12:29:05 -07001900 tran_low_t *tcoeff = BLOCK_OFFSET(x->mbmi_ext->tcoeff[plane], block);
1901 const int segment_id = mbmi->segment_id;
1902 const int seg_eob = av1_get_tx_eob(&cpi->common.seg, segment_id, tx_size);
1903 const tran_low_t *qcoeff = BLOCK_OFFSET(p->qcoeff, block);
1904 memcpy(tcoeff, qcoeff, sizeof(*tcoeff) * seg_eob);
1905
1906 uint8_t levels_buf[TX_PAD_2D];
1907 uint8_t *const levels = set_levels(levels_buf, width);
Linfeng Zhang1122d7d2017-10-31 15:30:28 -07001908 av1_txb_init_levels(tcoeff, width, height, levels);
Urvang Joshi553096a2018-05-10 15:27:14 -07001909 update_tx_type_count(cm, xd, blk_row, blk_col, plane, tx_size, td->counts,
1910 allow_update_cdf);
Hui Su8763cd32018-04-02 12:29:05 -07001911
1912 const PLANE_TYPE plane_type = pd->plane_type;
1913 const TX_TYPE tx_type = av1_get_tx_type(plane_type, xd, blk_row, blk_col,
1914 tx_size, cm->reduced_tx_set_used);
Katsuhisa Yuasa55c95f82018-04-03 00:51:22 +09001915 const TX_CLASS tx_class = tx_type_to_class[tx_type];
Hui Su8763cd32018-04-02 12:29:05 -07001916 const SCAN_ORDER *const scan_order = get_scan(tx_size, tx_type);
1917 const int16_t *const scan = scan_order->scan;
Yue Chen198738d2018-03-08 17:26:01 -08001918#if CONFIG_ENTROPY_STATS
Katsuhisa Yuasa55c95f82018-04-03 00:51:22 +09001919 av1_update_eob_context(cdf_idx, eob, tx_size, tx_class, plane_type, ec_ctx,
Yaowu Xu49abec82018-03-13 16:09:01 -07001920 td->counts, allow_update_cdf);
Yue Chen198738d2018-03-08 17:26:01 -08001921#else
Katsuhisa Yuasa55c95f82018-04-03 00:51:22 +09001922 av1_update_eob_context(eob, tx_size, tx_class, plane_type, ec_ctx,
Yue Chen198738d2018-03-08 17:26:01 -08001923 allow_update_cdf);
1924#endif
Hui Su8763cd32018-04-02 12:29:05 -07001925
1926 DECLARE_ALIGNED(16, int8_t, coeff_contexts[MAX_TX_SQUARE]);
Katsuhisa Yuasa55c95f82018-04-03 00:51:22 +09001927 av1_get_nz_map_contexts(levels, scan, eob, tx_size, tx_class, coeff_contexts);
Linfeng Zhangd67c13f2017-12-11 11:49:12 -08001928
Hui Su8763cd32018-04-02 12:29:05 -07001929 for (int c = eob - 1; c >= 0; --c) {
Linfeng Zhangdb41d1e2017-12-05 11:06:20 -08001930 const int pos = scan[c];
Linfeng Zhangd67c13f2017-12-11 11:49:12 -08001931 const int coeff_ctx = coeff_contexts[pos];
Linfeng Zhangdb41d1e2017-12-05 11:06:20 -08001932 const tran_low_t v = qcoeff[pos];
Angie Chiang2cc5eb32018-02-14 18:37:22 -08001933 const tran_low_t level = abs(v);
Dake He03a32922017-10-31 08:06:45 -07001934
Dake He4d447692017-12-15 09:10:06 -08001935 if (allow_update_cdf) {
1936 if (c == eob - 1) {
1937 assert(coeff_ctx < 4);
1938 update_cdf(
1939 ec_ctx->coeff_base_eob_cdf[txsize_ctx][plane_type][coeff_ctx],
Angie Chiang2cc5eb32018-02-14 18:37:22 -08001940 AOMMIN(level, 3) - 1, 3);
Dake He4d447692017-12-15 09:10:06 -08001941 } else {
1942 update_cdf(ec_ctx->coeff_base_cdf[txsize_ctx][plane_type][coeff_ctx],
Angie Chiang2cc5eb32018-02-14 18:37:22 -08001943 AOMMIN(level, 3), 4);
Dake He4d447692017-12-15 09:10:06 -08001944 }
Dake He3fe369c2017-11-16 17:56:44 -08001945 }
Dake He59881772017-11-24 07:00:02 -08001946 {
Dake He4d447692017-12-15 09:10:06 -08001947 if (c == eob - 1) {
1948 assert(coeff_ctx < 4);
Yue Chen198738d2018-03-08 17:26:01 -08001949#if CONFIG_ENTROPY_STATS
1950 ++td->counts->coeff_base_eob_multi[cdf_idx][txsize_ctx][plane_type]
1951 [coeff_ctx][AOMMIN(level, 3) - 1];
Dake He4d447692017-12-15 09:10:06 -08001952 } else {
Yue Chen198738d2018-03-08 17:26:01 -08001953 ++td->counts->coeff_base_multi[cdf_idx][txsize_ctx][plane_type]
1954 [coeff_ctx][AOMMIN(level, 3)];
1955#endif
Angie Chiang2cc5eb32018-02-14 18:37:22 -08001956 }
1957 }
1958 if (level > NUM_BASE_LEVELS) {
1959 const int base_range = level - 1 - NUM_BASE_LEVELS;
Katsuhisa Yuasa55c95f82018-04-03 00:51:22 +09001960 const int br_ctx = get_br_ctx(levels, pos, bwl, tx_class);
Angie Chiang2cc5eb32018-02-14 18:37:22 -08001961 for (int idx = 0; idx < COEFF_BASE_RANGE; idx += BR_CDF_SIZE - 1) {
1962 const int k = AOMMIN(base_range - idx, BR_CDF_SIZE - 1);
1963 if (allow_update_cdf) {
Angie Chiang2cfb7142018-02-16 11:24:52 -08001964 update_cdf(ec_ctx->coeff_br_cdf[AOMMIN(txsize_ctx, TX_32X32)]
1965 [plane_type][br_ctx],
1966 k, BR_CDF_SIZE);
Angie Chiang2cc5eb32018-02-14 18:37:22 -08001967 }
1968 for (int lps = 0; lps < BR_CDF_SIZE - 1; lps++) {
1969#if CONFIG_ENTROPY_STATS
Angie Chiang2cc5eb32018-02-14 18:37:22 -08001970 ++td->counts->coeff_lps[AOMMIN(txsize_ctx, TX_32X32)][plane_type][lps]
1971 [br_ctx][lps == k];
Angie Chiang2cc5eb32018-02-14 18:37:22 -08001972#endif // CONFIG_ENTROPY_STATS
1973 if (lps == k) break;
1974 }
Yue Chen198738d2018-03-08 17:26:01 -08001975#if CONFIG_ENTROPY_STATS
1976 ++td->counts->coeff_lps_multi[cdf_idx][AOMMIN(txsize_ctx, TX_32X32)]
1977 [plane_type][br_ctx][k];
1978#endif
Angie Chiang2cc5eb32018-02-14 18:37:22 -08001979 if (k < BR_CDF_SIZE - 1) break;
Dake He59881772017-11-24 07:00:02 -08001980 }
1981 }
Dake Hea47cd6c2017-10-13 18:09:58 -07001982 }
Dake Hea47cd6c2017-10-13 18:09:58 -07001983
Linfeng Zhangdb41d1e2017-12-05 11:06:20 -08001984 // Update the context needed to code the DC sign (if applicable)
Linfeng Zhangdb41d1e2017-12-05 11:06:20 -08001985 if (tcoeff[0] != 0) {
Hui Su8763cd32018-04-02 12:29:05 -07001986 const int dc_sign = (tcoeff[0] < 0) ? 1 : 0;
1987 const int dc_sign_ctx = txb_ctx.dc_sign_ctx;
Hui Su1e959892018-01-22 12:14:43 -08001988#if CONFIG_ENTROPY_STATS
Hui Su8763cd32018-04-02 12:29:05 -07001989 ++td->counts->dc_sign[plane_type][dc_sign_ctx][dc_sign];
Hui Su1e959892018-01-22 12:14:43 -08001990#endif // CONFIG_ENTROPY_STATS
Yunqing Wang0e141b52017-11-02 15:08:58 -07001991 if (allow_update_cdf)
Hui Su8763cd32018-04-02 12:29:05 -07001992 update_cdf(ec_ctx->dc_sign_cdf[plane_type][dc_sign_ctx], dc_sign, 2);
Dake He43edb762017-10-26 10:29:46 -07001993 x->mbmi_ext->dc_sign_ctx[plane][block] = dc_sign_ctx;
1994 }
1995
Hui Su8763cd32018-04-02 12:29:05 -07001996 const int cul_level = av1_get_txb_entropy_context(tcoeff, scan_order, eob);
Hui Sue674dec2018-04-02 13:02:08 -07001997 av1_set_contexts(xd, pd, plane, plane_bsize, tx_size, cul_level, blk_col,
1998 blk_row);
Angie Chiang0397eda2017-03-15 16:57:14 -07001999}
2000
2001void av1_update_txb_context(const AV1_COMP *cpi, ThreadData *td,
2002 RUN_TYPE dry_run, BLOCK_SIZE bsize, int *rate,
Yunqing Wang0e141b52017-11-02 15:08:58 -07002003 int mi_row, int mi_col, uint8_t allow_update_cdf) {
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00002004 const AV1_COMMON *const cm = &cpi->common;
2005 const int num_planes = av1_num_planes(cm);
Angie Chiang0397eda2017-03-15 16:57:14 -07002006 MACROBLOCK *const x = &td->mb;
2007 MACROBLOCKD *const xd = &x->e_mbd;
Yue Chen53b53f02018-03-29 14:31:23 -07002008 MB_MODE_INFO *const mbmi = xd->mi[0];
Yunqing Wang0e141b52017-11-02 15:08:58 -07002009 struct tokenize_b_args arg = { cpi, td, NULL, 0, allow_update_cdf };
Angie Chiang0397eda2017-03-15 16:57:14 -07002010 (void)rate;
2011 (void)mi_row;
2012 (void)mi_col;
2013 if (mbmi->skip) {
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00002014 av1_reset_skip_context(xd, mi_row, mi_col, bsize, num_planes);
Angie Chiang0397eda2017-03-15 16:57:14 -07002015 return;
2016 }
2017
2018 if (!dry_run) {
Jingning Han94652b82017-04-04 09:45:02 -07002019 av1_foreach_transformed_block(xd, bsize, mi_row, mi_col,
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00002020 av1_update_and_record_txb_context, &arg,
2021 num_planes);
Angie Chiangc8af6112017-03-16 16:11:22 -07002022 } else if (dry_run == DRY_RUN_NORMAL) {
Jingning Han4fe5f672017-05-19 15:46:07 -07002023 av1_foreach_transformed_block(xd, bsize, mi_row, mi_col,
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00002024 av1_update_txb_context_b, &arg, num_planes);
Angie Chiangc8af6112017-03-16 16:11:22 -07002025 } else {
2026 printf("DRY_RUN_COSTCOEFFS is not supported yet\n");
2027 assert(0);
Angie Chiang0397eda2017-03-15 16:57:14 -07002028 }
2029}