Commit 9aace06727 for aom
commit 9aace06727a7df5f4399a259a7e545c0b1359754
Author: Frank Galligan <fgalligan@google.com>
Date: Fri Nov 13 10:44:02 2026 -0800
av1: Inline entropy ctx and DC cap early exit
Inline max_block_wide, max_block_high, av1_set_entropy_contexts, and
power-of-two entropy fills into blockd.h, and add eob == 1 /
saturated DC fast paths in record_txb_context_dry_run.
Speedup: -0.55% (1T) / -0.41% (8T) encode time reduction (bit-exact).
TAG=agy
CONV=b929a630-a6cd-45a0-979c-1767c30178ee
Change-Id: I6ffc0372035445f6f5899b6adeddc33d64160494
diff --git a/av1/common/av1_common_int.h b/av1/common/av1_common_int.h
index e083c2b0d5..54c5139dfa 100644
--- a/av1/common/av1_common_int.h
+++ b/av1/common/av1_common_int.h
@@ -1564,33 +1564,6 @@ static inline int partition_cdf_length(BLOCK_SIZE bsize) {
return EXT_PARTITION_TYPES;
}
-static inline int max_block_wide(const MACROBLOCKD *xd, BLOCK_SIZE bsize,
- int plane) {
- assert(bsize < BLOCK_SIZES_ALL);
- int max_blocks_wide = block_size_wide[bsize];
-
- if (xd->mb_to_right_edge < 0) {
- const struct macroblockd_plane *const pd = &xd->plane[plane];
- max_blocks_wide += xd->mb_to_right_edge >> (3 + pd->subsampling_x);
- }
-
- // Scale the width in the transform block unit.
- return max_blocks_wide >> MI_SIZE_LOG2;
-}
-
-static inline int max_block_high(const MACROBLOCKD *xd, BLOCK_SIZE bsize,
- int plane) {
- int max_blocks_high = block_size_high[bsize];
-
- if (xd->mb_to_bottom_edge < 0) {
- const struct macroblockd_plane *const pd = &xd->plane[plane];
- max_blocks_high += xd->mb_to_bottom_edge >> (3 + pd->subsampling_y);
- }
-
- // Scale the height in the transform block unit.
- return max_blocks_high >> MI_SIZE_LOG2;
-}
-
static inline void av1_zero_above_context(AV1_COMMON *const cm,
const MACROBLOCKD *xd,
int mi_col_start, int mi_col_end,
diff --git a/av1/common/blockd.c b/av1/common/blockd.c
index 7e5f67dcda..ed75531439 100644
--- a/av1/common/blockd.c
+++ b/av1/common/blockd.c
@@ -26,35 +26,6 @@ PREDICTION_MODE av1_above_block_mode(const MB_MODE_INFO *above_mi) {
return above_mi->mode;
}
-void av1_set_entropy_contexts(const MACROBLOCKD *xd,
- struct macroblockd_plane *pd, int plane,
- BLOCK_SIZE plane_bsize, TX_SIZE tx_size,
- int has_eob, int aoff, int loff) {
- ENTROPY_CONTEXT *const a = pd->above_entropy_context + aoff;
- ENTROPY_CONTEXT *const l = pd->left_entropy_context + loff;
- const int txs_wide = tx_size_wide_unit[tx_size];
- const int txs_high = tx_size_high_unit[tx_size];
-
- // above
- if (has_eob && xd->mb_to_right_edge < 0) {
- const int blocks_wide = max_block_wide(xd, plane_bsize, plane);
- const int above_contexts = AOMMIN(txs_wide, blocks_wide - aoff);
- memset(a, has_eob, sizeof(*a) * above_contexts);
- memset(a + above_contexts, 0, sizeof(*a) * (txs_wide - above_contexts));
- } else {
- memset(a, has_eob, sizeof(*a) * txs_wide);
- }
-
- // left
- if (has_eob && xd->mb_to_bottom_edge < 0) {
- const int blocks_high = max_block_high(xd, plane_bsize, plane);
- const int left_contexts = AOMMIN(txs_high, blocks_high - loff);
- memset(l, has_eob, sizeof(*l) * left_contexts);
- memset(l + left_contexts, 0, sizeof(*l) * (txs_high - left_contexts));
- } else {
- memset(l, has_eob, sizeof(*l) * txs_high);
- }
-}
void av1_reset_entropy_context(MACROBLOCKD *xd, BLOCK_SIZE bsize,
const int num_planes) {
assert(bsize < BLOCK_SIZES_ALL);
@@ -65,8 +36,8 @@ void av1_reset_entropy_context(MACROBLOCKD *xd, BLOCK_SIZE bsize,
get_plane_block_size(bsize, pd->subsampling_x, pd->subsampling_y);
const int txs_wide = mi_size_wide[plane_bsize];
const int txs_high = mi_size_high[plane_bsize];
- memset(pd->above_entropy_context, 0, sizeof(ENTROPY_CONTEXT) * txs_wide);
- memset(pd->left_entropy_context, 0, sizeof(ENTROPY_CONTEXT) * txs_high);
+ av1_fill_entropy_ctx_pow2(pd->above_entropy_context, 0, txs_wide);
+ av1_fill_entropy_ctx_pow2(pd->left_entropy_context, 0, txs_high);
}
}
diff --git a/av1/common/blockd.h b/av1/common/blockd.h
index 582b6b9c44..b986f61b4e 100644
--- a/av1/common/blockd.h
+++ b/av1/common/blockd.h
@@ -1034,11 +1034,11 @@ static inline int block_signals_txsize(BLOCK_SIZE bsize) {
}
// Number of transform types in each set type
-static const int av1_num_ext_tx_set[EXT_TX_SET_TYPES] = {
+static const uint8_t av1_num_ext_tx_set[EXT_TX_SET_TYPES] = {
1, 2, 5, 7, 12, 16,
};
-static const int av1_ext_tx_used[EXT_TX_SET_TYPES][TX_TYPES] = {
+static const uint8_t av1_ext_tx_used[EXT_TX_SET_TYPES][TX_TYPES] = {
{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0 },
{ 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0 },
@@ -1139,7 +1139,7 @@ static inline TxSetType av1_get_ext_tx_set_type(TX_SIZE tx_size, int is_inter,
}
// Maps tx set types to the indices.
-static const int ext_tx_set_index[2][EXT_TX_SET_TYPES] = {
+static const int8_t ext_tx_set_index[2][EXT_TX_SET_TYPES] = {
{ // Intra
0, -1, 2, 1, -1, -1 },
{ // Inter
@@ -1431,10 +1431,88 @@ typedef void (*foreach_transformed_block_visitor)(int plane, int block,
BLOCK_SIZE plane_bsize,
TX_SIZE tx_size, void *arg);
-void av1_set_entropy_contexts(const MACROBLOCKD *xd,
- struct macroblockd_plane *pd, int plane,
- BLOCK_SIZE plane_bsize, TX_SIZE tx_size,
- int has_eob, int aoff, int loff);
+static inline int max_block_wide(const MACROBLOCKD *xd, BLOCK_SIZE bsize,
+ int plane) {
+ assert(bsize < BLOCK_SIZES_ALL);
+ int max_blocks_wide = block_size_wide[bsize];
+
+ if (xd->mb_to_right_edge < 0) {
+ const struct macroblockd_plane *const pd = &xd->plane[plane];
+ max_blocks_wide += xd->mb_to_right_edge >> (3 + pd->subsampling_x);
+ }
+
+ // Scale the width in the transform block unit.
+ return max_blocks_wide >> MI_SIZE_LOG2;
+}
+
+static inline int max_block_high(const MACROBLOCKD *xd, BLOCK_SIZE bsize,
+ int plane) {
+ assert(bsize < BLOCK_SIZES_ALL);
+ int max_blocks_high = block_size_high[bsize];
+
+ if (xd->mb_to_bottom_edge < 0) {
+ const struct macroblockd_plane *const pd = &xd->plane[plane];
+ max_blocks_high += xd->mb_to_bottom_edge >> (3 + pd->subsampling_y);
+ }
+
+ // Scale the height in the transform block unit.
+ return max_blocks_high >> MI_SIZE_LOG2;
+}
+
+// Fast power-of-two specialization for filling n ENTROPY_CONTEXT bytes with val
+// using fixed-width scalar stores instead of a runtime memset call.
+static inline void av1_fill_entropy_ctx_pow2(ENTROPY_CONTEXT *dst, uint8_t val,
+ int n) {
+ if (n == 1) {
+ dst[0] = val;
+ } else if (n == 2) {
+ const uint16_t v2 = (uint16_t)val * 0x0101u;
+ memcpy(dst, &v2, 2);
+ } else if (n == 4) {
+ const uint32_t v4 = (uint32_t)val * 0x01010101u;
+ memcpy(dst, &v4, 4);
+ } else if (n == 8) {
+ const uint64_t v8 = (uint64_t)val * 0x0101010101010101ULL;
+ memcpy(dst, &v8, 8);
+ } else if (n == 16) {
+ const uint64_t v8 = (uint64_t)val * 0x0101010101010101ULL;
+ memcpy(dst, &v8, 8);
+ memcpy(dst + 8, &v8, 8);
+ } else {
+ memset(dst, val, sizeof(*dst) * n);
+ }
+}
+
+static inline void av1_set_entropy_contexts(const MACROBLOCKD *xd,
+ struct macroblockd_plane *pd,
+ int plane, BLOCK_SIZE plane_bsize,
+ TX_SIZE tx_size, int has_eob,
+ int aoff, int loff) {
+ ENTROPY_CONTEXT *const a = pd->above_entropy_context + aoff;
+ ENTROPY_CONTEXT *const l = pd->left_entropy_context + loff;
+ const int txs_wide = tx_size_wide_unit[tx_size];
+ const int txs_high = tx_size_high_unit[tx_size];
+
+ // above
+ if (UNLIKELY(has_eob && xd->mb_to_right_edge < 0)) {
+ const int blocks_wide = max_block_wide(xd, plane_bsize, plane);
+ const int above_contexts = AOMMIN(txs_wide, blocks_wide - aoff);
+ memset(a, has_eob, sizeof(*a) * above_contexts);
+ memset(a + above_contexts, 0, sizeof(*a) * (txs_wide - above_contexts));
+ } else {
+ av1_fill_entropy_ctx_pow2(a, (uint8_t)has_eob, txs_wide);
+ }
+
+ // left
+ if (UNLIKELY(has_eob && xd->mb_to_bottom_edge < 0)) {
+ const int blocks_high = max_block_high(xd, plane_bsize, plane);
+ const int left_contexts = AOMMIN(txs_high, blocks_high - loff);
+ memset(l, has_eob, sizeof(*l) * left_contexts);
+ memset(l + left_contexts, 0, sizeof(*l) * (txs_high - left_contexts));
+ } else {
+ av1_fill_entropy_ctx_pow2(l, (uint8_t)has_eob, txs_high);
+ }
+}
#define MAX_INTERINTRA_SB_SQUARE 32 * 32
static inline int is_interintra_mode(const MB_MODE_INFO *mbmi) {
diff --git a/av1/encoder/encodemb.h b/av1/encoder/encodemb.h
index 0c9f1f5ba2..b230633992 100644
--- a/av1/encoder/encodemb.h
+++ b/av1/encoder/encodemb.h
@@ -319,8 +319,8 @@ static inline void av1_set_txb_context(MACROBLOCK *x, int plane, int block,
TX_SIZE tx_size, ENTROPY_CONTEXT *a,
ENTROPY_CONTEXT *l) {
const uint8_t ctx = x->plane[plane].txb_entropy_ctx[block];
- memset(a, ctx, tx_size_wide_unit[tx_size] * sizeof(*a));
- memset(l, ctx, tx_size_high_unit[tx_size] * sizeof(*l));
+ av1_fill_entropy_ctx_pow2(a, ctx, tx_size_wide_unit[tx_size]);
+ av1_fill_entropy_ctx_pow2(l, ctx, tx_size_high_unit[tx_size]);
}
void av1_encode_intra_block_plane(const struct AV1_COMP *cpi, MACROBLOCK *x,
diff --git a/av1/encoder/encodetxb.c b/av1/encoder/encodetxb.c
index 41246d6973..867768af31 100644
--- a/av1/encoder/encodetxb.c
+++ b/av1/encoder/encodetxb.c
@@ -886,13 +886,22 @@ static inline void record_txb_context_dry_run(
const int eob = p->eobs[block];
uint8_t cul_level = 0;
if (eob > 0) {
- const AV1_COMMON *cm = &args->cpi->common;
- const TX_TYPE tx_type =
- av1_get_tx_type(xd, pd->plane_type, blk_row, blk_col, tx_size,
- cm->features.reduced_tx_set_used);
- const SCAN_ORDER *const scan_order = get_scan(tx_size, tx_type);
const tran_low_t *tcoeff = p->qcoeff + BLOCK_OFFSET(block);
- cul_level = av1_get_txb_entropy_context(tcoeff, scan_order, eob);
+ const tran_low_t dc_val = tcoeff[0];
+ int cul_sum = abs(dc_val);
+ if (eob == 1 || cul_sum >= COEFF_CONTEXT_MASK) {
+ cul_sum = AOMMIN(cul_sum, COEFF_CONTEXT_MASK);
+ const int dc_sign_mask = ((dc_val < 0) + ((dc_val > 0) << 1))
+ << COEFF_CONTEXT_BITS;
+ cul_level = (uint8_t)(cul_sum | dc_sign_mask);
+ } else {
+ const AV1_COMMON *cm = &args->cpi->common;
+ const TX_TYPE tx_type =
+ av1_get_tx_type(xd, pd->plane_type, blk_row, blk_col, tx_size,
+ cm->features.reduced_tx_set_used);
+ const SCAN_ORDER *const scan_order = get_scan(tx_size, tx_type);
+ cul_level = av1_get_txb_entropy_context(tcoeff, scan_order, eob);
+ }
}
av1_set_entropy_contexts(xd, pd, plane, plane_bsize, tx_size, cul_level,
blk_col, blk_row);