Commit 8f32c9a833 for aom
commit 8f32c9a833bba5f8be221f87112c1f630e29e4e1
Author: Frank Galligan <fgalligan@google.com>
Date: Fri Nov 13 10:43:59 2026 -0800
av1: Add eob == 2 txb cost and pack paths
Skip 2D levels init and full-block tcoeff memcpy for 2D eob == 2
blocks with |qcoeff[0]| <= NUM_BASE_LEVELS, and use unsigned index
math and simplified context lookup in eob == 2 coeff costing.
Speedup: -0.79% (1T) / -0.58% (8T) encode time reduction (bit-exact).
TAG=agy
CONV=b929a630-a6cd-45a0-979c-1767c30178ee
Change-Id: Ibde364712f8abf889cccc581c090b8b30b830b9b
diff --git a/av1/encoder/encodetxb.c b/av1/encoder/encodetxb.c
index c1a4e70344..ed745865e7 100644
--- a/av1/encoder/encodetxb.c
+++ b/av1/encoder/encodetxb.c
@@ -328,13 +328,19 @@ void av1_write_coeffs_txb(const AV1_COMMON *const cm, MACROBLOCK *const x,
const tran_low_t *tcoeff = tcoeff_txb + BLOCK_OFFSET(block);
const SCAN_ORDER *const scan_order = get_scan(tx_size, tx_type);
const int16_t *const scan = scan_order->scan;
+ assert(scan[0] == 0);
DECLARE_ALIGNED(16, int8_t, coeff_contexts[MAX_TX_SQUARE]);
- if (eob > 1) {
+ if (eob == 1) {
+ coeff_contexts[0] = 0;
+ } else if (eob == 2 && tx_class == TX_CLASS_2D &&
+ (unsigned int)(tcoeff[0] + NUM_BASE_LEVELS) <=
+ 2 * NUM_BASE_LEVELS) {
+ coeff_contexts[0] = 0;
+ coeff_contexts[scan[1]] = 1;
+ } else {
av1_txb_init_levels(tcoeff, width, height, levels);
av1_get_nz_map_contexts(levels, scan, eob, tx_size, tx_class,
coeff_contexts);
- } else {
- coeff_contexts[0] = 0;
}
// The rest of the block only writes symbols, so keep the range coder state
@@ -413,7 +419,7 @@ void av1_write_coeffs_txb(const AV1_COMMON *const cm, MACROBLOCK *const x,
write_coeff_br(w, &st, br_cdf[get_br_ctx_eob(pos, bhl, tx_class)], level);
}
}
- for (int c = eob - 2; c >= 0; --c) {
+ for (int c = eob - 2; c >= 1; --c) {
const int pos = scan[c];
const tran_low_t v = tcoeff[pos];
const tran_low_t level = abs(v);
@@ -427,6 +433,19 @@ void av1_write_coeffs_txb(const AV1_COMMON *const cm, MACROBLOCK *const x,
level);
}
}
+ if (eob > 1) {
+ const tran_low_t v = tcoeff[0];
+ const tran_low_t level = abs(v);
+ nz_coeffs[num_nz] = v;
+ num_nz += (v != 0);
+ aom_write_symbol_state(w, &st, AOMMIN(level, 3),
+ base_cdf[coeff_contexts[0]], 4);
+ if (UNLIKELY(level > NUM_BASE_LEVELS)) {
+ write_coeff_br(
+ w, &st, br_cdf[get_br_ctx_stride(levels, 0, bhl, stride, tx_class)],
+ level);
+ }
+ }
// Code all signs in the transform block, starting with the sign of DC (if
// applicable). nz_coeffs holds them in the reverse order.
@@ -634,20 +653,36 @@ static void update_and_record_txb_context_output(
tran_low_t *tcoeff_txb =
cb_coef_buff->tcoeff[plane] + x->mbmi_ext_frame->cb_offset[plane_type];
tran_low_t *tcoeff = tcoeff_txb + block_offset;
+ const TX_CLASS tx_class = tx_type_to_class[tx_type];
+ const int16_t *const scan = scan_order->scan;
+ const int fast_eob2 =
+ eob == 2 && tx_class == TX_CLASS_2D &&
+ (unsigned int)(qcoeff[0] + NUM_BASE_LEVELS) <= 2 * NUM_BASE_LEVELS;
+#if !CONFIG_ENTROPY_STATS
if (eob == 1) {
+ assert(scan[0] == 0);
tcoeff[0] = qcoeff[0];
+ } else if (fast_eob2) {
+ assert(scan[0] == 0);
+ tcoeff[0] = qcoeff[0];
+ tcoeff[scan[1]] = qcoeff[scan[1]];
} else {
memcpy(tcoeff, qcoeff, sizeof(*tcoeff) * seg_eob);
}
+#else
+ if (eob == 1) {
+ assert(scan[0] == 0);
+ tcoeff[0] = qcoeff[0];
+ } else {
+ memcpy(tcoeff, qcoeff, sizeof(*tcoeff) * seg_eob);
+ }
+#endif
uint8_t levels_buf[TX_PAD_2D];
uint8_t *const levels = set_levels(levels_buf, height);
update_tx_type_count(cpi, cm, xd, blk_row, blk_col, plane, tx_size,
td->counts, allow_update_cdf);
- const TX_CLASS tx_class = tx_type_to_class[tx_type];
- const int16_t *const scan = scan_order->scan;
-
// record tx type usage
td->rd_counts.tx_type_used[tx_size][tx_type]++;
@@ -660,12 +695,17 @@ static void update_and_record_txb_context_output(
#endif
DECLARE_ALIGNED(16, int8_t, coeff_contexts[MAX_TX_SQUARE]);
- if (eob > 1) {
+ if (eob == 1) {
+ assert(scan[0] == 0);
+ coeff_contexts[0] = 0;
+ } else if (fast_eob2) {
+ assert(scan[0] == 0);
+ coeff_contexts[0] = 0;
+ coeff_contexts[scan[1]] = 1;
+ } else {
av1_txb_init_levels(tcoeff, width, height, levels);
av1_get_nz_map_contexts(levels, scan, eob, tx_size, tx_class,
coeff_contexts);
- } else {
- coeff_contexts[0] = 0;
}
const intptr_t stride = height + TX_PAD_HOR;
@@ -824,9 +864,17 @@ static void record_txb_context_output(int plane, int block, int blk_row,
tran_low_t *tcoeff_txb =
cb_coef_buff->tcoeff[plane] + x->mbmi_ext_frame->cb_offset[plane_type];
tran_low_t *tcoeff = tcoeff_txb + block_offset;
+ const int16_t *const scan = scan_order->scan;
#if !CONFIG_ENTROPY_STATS
if (eob == 1) {
+ assert(scan[0] == 0);
tcoeff[0] = qcoeff[0];
+ } else if (eob == 2 && tx_type_to_class[tx_type] == TX_CLASS_2D &&
+ (unsigned int)(qcoeff[0] + NUM_BASE_LEVELS) <=
+ 2 * NUM_BASE_LEVELS) {
+ assert(scan[0] == 0);
+ tcoeff[0] = qcoeff[0];
+ tcoeff[scan[1]] = qcoeff[scan[1]];
} else {
memcpy(tcoeff, qcoeff, sizeof(*tcoeff) * seg_eob);
}
@@ -846,7 +894,6 @@ static void record_txb_context_output(int plane, int block, int blk_row,
#else
const bool do_coeff_scan = cpi->mt_info.pack_bs_mt_enabled;
#endif
- const int16_t *const scan = scan_order->scan;
// record tx type usage
td->rd_counts.tx_type_used[tx_size][tx_type]++;
diff --git a/av1/encoder/txb_rdopt.c b/av1/encoder/txb_rdopt.c
index cce0927e2e..a2526a3e5a 100644
--- a/av1/encoder/txb_rdopt.c
+++ b/av1/encoder/txb_rdopt.c
@@ -974,9 +974,10 @@ static AOM_FORCE_INLINE int warehouse_efficients_txb_loop(
const tran_low_t v = qcoeff[pos];
if (v) {
- const int level = abs(v);
+ const unsigned int level = (unsigned int)abs(v);
const int coeff_ctx = get_lower_levels_ctx_eob(bhl, width, c);
- cost += coeff_costs->base_eob_cost[coeff_ctx][AOMMIN(level, 3) - 1] +
+ const uintptr_t min_lvl = level < 3 ? level : 3;
+ cost += coeff_costs->base_eob_cost[coeff_ctx][min_lvl - 1] +
av1_cost_literal(1);
if (UNLIKELY(level > NUM_BASE_LEVELS)) {
const int ctx = get_br_ctx_eob(pos, bhl, tx_class);
@@ -1026,8 +1027,9 @@ static AOM_FORCE_INLINE int warehouse_efficients_txb_loop(
for (c = eob - 2; c >= 1; --c) {
const int pos = scan[c];
const int coeff_ctx = coeff_contexts[pos];
- const int level = abs(qcoeff[pos]);
- cost += base_cost[coeff_ctx][4 + AOMMIN(level, 3)];
+ const unsigned int level = (unsigned int)abs(qcoeff[pos]);
+ const uintptr_t min_lvl = level < 3 ? level : 3;
+ cost += base_cost[coeff_ctx][4 + min_lvl];
if (UNLIKELY(level > NUM_BASE_LEVELS)) {
const int ctx =
get_br_ctx_simple_stride(levels, pos, bhl, stride, tx_class);
@@ -1041,10 +1043,11 @@ static AOM_FORCE_INLINE int warehouse_efficients_txb_loop(
if (!v) {
cost += base_cost[coeff_ctx][0];
} else {
- const int level = abs(v);
+ const unsigned int level = (unsigned int)abs(v);
const int sign01 = (v < 0) ? 1 : 0;
const int dc_sign_ctx = txb_ctx->dc_sign_ctx;
- cost += base_cost[coeff_ctx][AOMMIN(level, 3)] +
+ const uintptr_t min_lvl = level < 3 ? level : 3;
+ cost += base_cost[coeff_ctx][min_lvl] +
coeff_costs->dc_sign_cost[dc_sign_ctx][sign01];
if (UNLIKELY(level > NUM_BASE_LEVELS)) {
const int ctx = get_br_ctx_stride(levels, 0, bhl, stride, tx_class);
@@ -1063,10 +1066,8 @@ static int warehouse_efficients_txb_multi(
const TXB_CTX *const txb_ctx, const int eob,
const LV_MAP_COEFF_COST *const coeff_costs, const TX_TYPE tx_type,
const TX_CLASS tx_class, int cost) {
- const int bhl = get_txb_bhl(tx_size);
const int width = get_txb_wide(tx_size);
const int height = get_txb_high(tx_size);
- const intptr_t stride = height + TX_PAD_HOR;
const SCAN_ORDER *const scan_order = get_scan(tx_size, tx_type);
const int16_t *const scan = scan_order->scan;
uint8_t levels_buf[TX_PAD_2D];
@@ -1074,6 +1075,8 @@ static int warehouse_efficients_txb_multi(
av1_txb_init_levels(qcoeff, width, height, levels);
+ const int bhl = get_txb_bhl(tx_size);
+ const intptr_t stride = height + TX_PAD_HOR;
switch (tx_class) {
case TX_CLASS_2D:
return warehouse_efficients_txb_loop(qcoeff, scan, eob, levels, tx_size,
@@ -1218,41 +1221,33 @@ int av1_cost_coeffs_txb(const MACROBLOCK *x, const int plane, const int block,
}
if (eob == 2 && tx_class == TX_CLASS_2D) {
- const int bhl = get_txb_bhl(tx_size);
- const int width = get_txb_wide(tx_size);
- const SCAN_ORDER *const scan_order = get_scan(tx_size, tx_type);
- const int (*lps_cost)[16] = coeff_costs->lps_cost;
- const int (*base_cost)[8] = coeff_costs->base_cost;
- const int pos1 = scan_order->scan[1];
+ const int16_t *const scan = get_scan(tx_size, tx_type)->scan;
+ const int pos1 = scan[1];
+ assert(pos1 == 1 || pos1 == (1 << get_txb_bhl(tx_size)));
+ assert(get_lower_levels_ctx_eob(get_txb_bhl(tx_size), get_txb_wide(tx_size),
+ 1) == 1);
const tran_low_t v1 = qcoeff[pos1];
- int level1 = 0;
+ unsigned int level1 = 0;
if (v1) {
- const int sign1 = AOMSIGN(v1);
- level1 = (v1 ^ sign1) - sign1;
- const int coeff_ctx1 = get_lower_levels_ctx_eob(bhl, width, 1);
- cost += coeff_costs->base_eob_cost[coeff_ctx1][AOMMIN(level1, 3) - 1];
- if (level1 > NUM_BASE_LEVELS) {
- const int ctx1 = get_br_ctx_eob(pos1, bhl, TX_CLASS_2D);
- cost += get_br_cost(level1, lps_cost[ctx1]);
+ level1 = (unsigned int)abs(v1);
+ const uintptr_t min_lvl1 = level1 < 3 ? level1 : 3;
+ cost += coeff_costs->base_eob_cost[1][min_lvl1 - 1] + av1_cost_literal(1);
+ if (UNLIKELY(level1 > NUM_BASE_LEVELS)) {
+ cost += get_br_cost(level1, coeff_costs->lps_cost[7]);
}
- cost += av1_cost_literal(1);
}
const tran_low_t v0 = qcoeff[0];
if (!v0) {
- cost += base_cost[0][0];
+ cost += coeff_costs->base_cost[0][0];
} else {
- const int sign0 = AOMSIGN(v0);
- const int level0 = (v0 ^ sign0) - sign0;
- cost += base_cost[0][AOMMIN(level0, 3)];
- const int sign01 = -sign0;
- cost += coeff_costs->dc_sign_cost[txb_ctx->dc_sign_ctx][sign01];
- if (level0 > NUM_BASE_LEVELS) {
- const int mag =
- (pos1 == 1 || pos1 == (1 << bhl) || pos1 == (1 << bhl) + 1)
- ? AOMMIN(level1, INT8_MAX)
- : 0;
- const int ctx0 = AOMMIN((mag + 1) >> 1, 6);
- cost += get_br_cost(level0, lps_cost[ctx0]);
+ const unsigned int level0 = (unsigned int)abs(v0);
+ const int sign01 = (v0 < 0) ? 1 : 0;
+ const uintptr_t min_lvl0 = level0 < 3 ? level0 : 3;
+ cost += coeff_costs->base_cost[0][min_lvl0] +
+ coeff_costs->dc_sign_cost[txb_ctx->dc_sign_ctx][sign01];
+ if (UNLIKELY(level0 > NUM_BASE_LEVELS)) {
+ const unsigned int ctx0 = AOMMIN((level1 + 1) >> 1, 6);
+ cost += get_br_cost(level0, coeff_costs->lps_cost[ctx0]);
}
}
return cost;