Commit e255c55a30 for aom
commit e255c55a300b10f1ba0a799871bc38f8f8da128e
Author: Frank Galligan <fgalligan@google.com>
Date: Fri Nov 13 10:43:53 2026 -0800
av1: Inline plane size lookup and last coeff
Inline 4:4:4 subsampling check in get_plane_block_size using an inline
lookup table, streamline reconintra setup, and add specialized
update_coeff_last and update_coeff_dc helpers in txb_rdopt.
Speedup: -0.85% (1T) / -0.62% (8T) encode time reduction (bit-exact).
TAG=agy
CONV=b929a630-a6cd-45a0-979c-1767c30178ee
Change-Id: Icc942d830f85a837e6a738e4365ad39be701ace3
diff --git a/av1/common/blockd.h b/av1/common/blockd.h
index 629a1de81d..582b6b9c44 100644
--- a/av1/common/blockd.h
+++ b/av1/common/blockd.h
@@ -1218,10 +1218,11 @@ static inline TX_TYPE get_default_tx_type(PLANE_TYPE plane_type,
static inline BLOCK_SIZE get_plane_block_size(BLOCK_SIZE bsize,
int subsampling_x,
int subsampling_y) {
- assert(bsize < BLOCK_SIZES_ALL);
+ assert((unsigned int)bsize < BLOCK_SIZES_ALL);
assert(subsampling_x >= 0 && subsampling_x < 2);
assert(subsampling_y >= 0 && subsampling_y < 2);
- return av1_ss_size_lookup[bsize][subsampling_x][subsampling_y];
+ if ((subsampling_x | subsampling_y) == 0) return bsize;
+ return av1_ss_size_lookup_inline[bsize][subsampling_x][subsampling_y];
}
/*
diff --git a/av1/common/common_data.h b/av1/common/common_data.h
index 677c4a4ba0..190080bb07 100644
--- a/av1/common/common_data.h
+++ b/av1/common/common_data.h
@@ -375,6 +375,35 @@ static const TX_SIZE tx_mode_to_biggest_tx_size[TX_MODES] = {
// size function).
extern const BLOCK_SIZE av1_ss_size_lookup[BLOCK_SIZES_ALL][2][2];
+/* clang-format off */
+static const BLOCK_SIZE av1_ss_size_lookup_inline[BLOCK_SIZES_ALL][2][2] = {
+ // ss_x == 0 ss_x == 0 ss_x == 1 ss_x == 1
+ // ss_y == 0 ss_y == 1 ss_y == 0 ss_y == 1
+ { { BLOCK_4X4, BLOCK_4X4 }, { BLOCK_4X4, BLOCK_4X4 } },
+ { { BLOCK_4X8, BLOCK_4X4 }, { BLOCK_INVALID, BLOCK_4X4 } },
+ { { BLOCK_8X4, BLOCK_INVALID }, { BLOCK_4X4, BLOCK_4X4 } },
+ { { BLOCK_8X8, BLOCK_8X4 }, { BLOCK_4X8, BLOCK_4X4 } },
+ { { BLOCK_8X16, BLOCK_8X8 }, { BLOCK_INVALID, BLOCK_4X8 } },
+ { { BLOCK_16X8, BLOCK_INVALID }, { BLOCK_8X8, BLOCK_8X4 } },
+ { { BLOCK_16X16, BLOCK_16X8 }, { BLOCK_8X16, BLOCK_8X8 } },
+ { { BLOCK_16X32, BLOCK_16X16 }, { BLOCK_INVALID, BLOCK_8X16 } },
+ { { BLOCK_32X16, BLOCK_INVALID }, { BLOCK_16X16, BLOCK_16X8 } },
+ { { BLOCK_32X32, BLOCK_32X16 }, { BLOCK_16X32, BLOCK_16X16 } },
+ { { BLOCK_32X64, BLOCK_32X32 }, { BLOCK_INVALID, BLOCK_16X32 } },
+ { { BLOCK_64X32, BLOCK_INVALID }, { BLOCK_32X32, BLOCK_32X16 } },
+ { { BLOCK_64X64, BLOCK_64X32 }, { BLOCK_32X64, BLOCK_32X32 } },
+ { { BLOCK_64X128, BLOCK_64X64 }, { BLOCK_INVALID, BLOCK_32X64 } },
+ { { BLOCK_128X64, BLOCK_INVALID }, { BLOCK_64X64, BLOCK_64X32 } },
+ { { BLOCK_128X128, BLOCK_128X64 }, { BLOCK_64X128, BLOCK_64X64 } },
+ { { BLOCK_4X16, BLOCK_4X8 }, { BLOCK_INVALID, BLOCK_4X8 } },
+ { { BLOCK_16X4, BLOCK_INVALID }, { BLOCK_8X4, BLOCK_8X4 } },
+ { { BLOCK_8X32, BLOCK_8X16 }, { BLOCK_INVALID, BLOCK_4X16 } },
+ { { BLOCK_32X8, BLOCK_INVALID }, { BLOCK_16X8, BLOCK_16X4 } },
+ { { BLOCK_16X64, BLOCK_16X32 }, { BLOCK_INVALID, BLOCK_8X32 } },
+ { { BLOCK_64X16, BLOCK_INVALID }, { BLOCK_32X16, BLOCK_32X8 } }
+};
+/* clang-format on */
+
// Generates 5 bit field in which each bit set to 1 represents
// a blocksize partition 11111 means we split 128x128, 64x64, 32x32, 16x16
// and 8x8. 10000 means we just split the 128x128 to 64x64
diff --git a/av1/common/reconintra.c b/av1/common/reconintra.c
index 09ddc1a51e..e4fe34a540 100644
--- a/av1/common/reconintra.c
+++ b/av1/common/reconintra.c
@@ -986,7 +986,8 @@ static int get_intra_edge_filter_type(const MACROBLOCKD *xd, int plane) {
return (above && is_smooth(above, plane)) || (left && is_smooth(left, plane));
}
-static int intra_edge_filter_strength(int bs0, int bs1, int delta, int type) {
+static AOM_FORCE_INLINE int intra_edge_filter_strength(int bs0, int bs1,
+ int delta, int type) {
const int d = abs(delta);
int strength = 0;
@@ -1097,7 +1098,62 @@ static AOM_FORCE_INLINE void copy_above_row_lbd(uint8_t *above_row,
} else if (n_copy == 128) {
memcpy(above_row, above_ref, 128);
} else {
- memcpy(above_row, above_ref, n_copy);
+ int k = 0;
+ for (; k + 4 <= n_copy; k += 4) memcpy(above_row + k, above_ref + k, 4);
+ for (; k < n_copy; ++k) above_row[k] = above_ref[k];
+ }
+}
+
+static AOM_FORCE_INLINE void fill_intra_edge_lbd(uint8_t *p, uint8_t val,
+ int n) {
+ if (n == 4) {
+ memset(p, val, 4);
+ } else if (n == 8) {
+ memset(p, val, 8);
+ } else if (n == 16) {
+ memset(p, val, 16);
+ } else if (n == 32) {
+ memset(p, val, 32);
+ } else if (n == 64) {
+ memset(p, val, 64);
+ } else if (n == 128) {
+ memset(p, val, 128);
+ } else {
+ const uint32_t v32 = (uint32_t)val * 0x01010101U;
+ int k = 0;
+ for (; k + 4 <= n; k += 4) memcpy(p + k, &v32, 4);
+ for (; k < n; ++k) p[k] = val;
+ }
+}
+
+static AOM_FORCE_INLINE void fill_block_lbd(uint8_t *dst, int dst_stride,
+ uint8_t val, int txwpx, int txhpx) {
+ if (txwpx == 4) {
+ for (int i = 0; i < txhpx; ++i) {
+ memset(dst, val, 4);
+ dst += dst_stride;
+ }
+ } else if (txwpx == 8) {
+ for (int i = 0; i < txhpx; ++i) {
+ memset(dst, val, 8);
+ dst += dst_stride;
+ }
+ } else if (txwpx == 16) {
+ for (int i = 0; i < txhpx; ++i) {
+ memset(dst, val, 16);
+ dst += dst_stride;
+ }
+ } else if (txwpx == 32) {
+ for (int i = 0; i < txhpx; ++i) {
+ memset(dst, val, 32);
+ dst += dst_stride;
+ }
+ } else {
+ assert(txwpx == 64);
+ for (int i = 0; i < txhpx; ++i) {
+ memset(dst, val, 64);
+ dst += dst_stride;
+ }
}
}
@@ -1125,16 +1181,13 @@ static void build_directional_and_filter_intra_predictors(
assert(n_bottomleft_px >= -1);
if ((!need_above && n_left_px == 0) || (!need_left && n_top_px == 0)) {
- int val;
+ uint8_t val;
if (need_left) {
val = (n_top_px > 0) ? above_ref[0] : 129;
} else {
val = (n_left_px > 0) ? left_ref[0] : 127;
}
- for (i = 0; i < txhpx; ++i) {
- memset(dst, val, txwpx);
- dst += dst_stride;
- }
+ fill_block_lbd(dst, dst_stride, val, txwpx, txhpx);
return;
}
@@ -1156,10 +1209,11 @@ static void build_directional_and_filter_intra_predictors(
#endif
for (i = 0; i < n_left_copy; i++) left_col[i] = left_ref[i * ref_stride];
if (i < num_left_pixels_needed)
- memset(&left_col[i], left_col[i - 1], num_left_pixels_needed - i);
+ fill_intra_edge_lbd(&left_col[i], left_col[i - 1],
+ num_left_pixels_needed - i);
} else {
const uint8_t val = (n_top_px > 0) ? above_ref[0] : 129;
- memset(left_col, val, num_left_pixels_needed);
+ fill_intra_edge_lbd(left_col, val, num_left_pixels_needed);
}
}
@@ -1170,11 +1224,11 @@ static void build_directional_and_filter_intra_predictors(
const int n_top_copy = n_top_px + (n_topright_px > 0 ? n_topright_px : 0);
copy_above_row_lbd(above_row, above_ref, n_top_copy);
if (n_top_copy < num_top_pixels_needed)
- memset(&above_row[n_top_copy], above_row[n_top_copy - 1],
- num_top_pixels_needed - n_top_copy);
+ fill_intra_edge_lbd(&above_row[n_top_copy], above_row[n_top_copy - 1],
+ num_top_pixels_needed - n_top_copy);
} else {
const uint8_t val = (n_left_px > 0) ? left_ref[0] : 127;
- memset(above_row, val, num_top_pixels_needed);
+ fill_intra_edge_lbd(above_row, val, num_top_pixels_needed);
}
}
@@ -1293,16 +1347,13 @@ static void build_non_directional_intra_predictors(
mode == SMOOTH_H_PRED || mode == PAETH_PRED);
if ((!need_above && n_left_px == 0) || (!need_left && n_top_px == 0)) {
- int val = 0;
+ uint8_t val = 0;
if (need_left) {
val = (n_top_px > 0) ? above_ref[0] : 129;
} else {
val = (n_left_px > 0) ? left_ref[0] : 127;
}
- for (i = 0; i < txhpx; ++i) {
- memset(dst, val, txwpx);
- dst += dst_stride;
- }
+ fill_block_lbd(dst, dst_stride, val, txwpx, txhpx);
return;
}
@@ -1317,10 +1368,11 @@ static void build_non_directional_intra_predictors(
#pragma GCC novector
#endif
for (i = 0; i < n_left_px; i++) left_col[i] = left_ref[i * ref_stride];
- if (i < txhpx) memset(&left_col[i], left_col[i - 1], txhpx - i);
+ if (i < txhpx)
+ fill_intra_edge_lbd(&left_col[i], left_col[i - 1], txhpx - i);
} else {
const uint8_t val = (n_top_px > 0) ? above_ref[0] : 129;
- memset(left_col, val, txhpx);
+ fill_intra_edge_lbd(left_col, val, txhpx);
}
}
@@ -1329,10 +1381,11 @@ static void build_non_directional_intra_predictors(
copy_above_row_lbd(above_row, above_ref, txwpx);
} else if (n_top_px > 0) {
copy_above_row_lbd(above_row, above_ref, n_top_px);
- memset(&above_row[n_top_px], above_row[n_top_px - 1], txwpx - n_top_px);
+ fill_intra_edge_lbd(&above_row[n_top_px], above_row[n_top_px - 1],
+ txwpx - n_top_px);
} else {
const uint8_t val = (n_left_px > 0) ? left_ref[0] : 127;
- memset(above_row, val, txwpx);
+ fill_intra_edge_lbd(above_row, val, txwpx);
}
}
@@ -1710,8 +1763,9 @@ static void highbd_build_non_directional_intra_predictors(
}
#endif // CONFIG_AV1_HIGHBITDEPTH
-static inline BLOCK_SIZE scale_chroma_bsize(BLOCK_SIZE bsize, int subsampling_x,
- int subsampling_y) {
+static AOM_FORCE_INLINE BLOCK_SIZE scale_chroma_bsize(BLOCK_SIZE bsize,
+ int subsampling_x,
+ int subsampling_y) {
assert(subsampling_x >= 0 && subsampling_x < 2);
assert(subsampling_y >= 0 && subsampling_y < 2);
BLOCK_SIZE bs = bsize;
diff --git a/av1/encoder/txb_rdopt.c b/av1/encoder/txb_rdopt.c
index 576052d35b..8d6a71cfd2 100644
--- a/av1/encoder/txb_rdopt.c
+++ b/av1/encoder/txb_rdopt.c
@@ -16,41 +16,95 @@
#include "aom_ports/mem.h"
#include "av1/common/idct.h"
-static AOM_FORCE_INLINE void update_coeff_general(
- int *accu_rate, int64_t *accu_dist, int si, int eob, TX_SIZE tx_size,
- TX_CLASS tx_class, int bhl, int width, int64_t rdmult, int shift,
- int dc_sign_ctx, const int16_t *dequant, const int16_t *scan,
+// Specialized trellis update for the last non-zero coefficient (si == eob - 1)
+// when its magnitude is >= 2.
+static AOM_FORCE_INLINE void update_coeff_last(
+ int *accu_rate, int64_t *accu_dist, int si, int ci, tran_low_t abs_qc,
+ int sign, TX_CLASS tx_class, int bhl, int width, int64_t rdmult, int shift,
+ int dc_sign_ctx, const int16_t *dequant, const LV_MAP_COEFF_COST *txb_costs,
+ const tran_low_t *tcoeff, tran_low_t *qcoeff, tran_low_t *dqcoeff,
+ uint8_t *levels, const qm_val_t *iqmatrix, const qm_val_t *qmatrix) {
+ const int coeff_ctx = get_lower_levels_ctx_eob(bhl, width, si);
+ const tran_low_t tqc = tcoeff[ci];
+ const tran_low_t dqc = dqcoeff[ci];
+ const int rate = get_coeff_cost_eob(ci, abs_qc, sign, coeff_ctx, dc_sign_ctx,
+ txb_costs, bhl, tx_class);
+
+ tran_low_t qc_low, dqc_low;
+ const int dqv = get_dqv(dequant, ci, iqmatrix);
+ get_qc_dqc_low(abs_qc, sign, dqv, shift, &qc_low, &dqc_low);
+ const tran_low_t abs_qc_low = abs_qc - 1;
+ const int rate_low = get_coeff_cost_eob(
+ ci, abs_qc_low, sign, coeff_ctx, dc_sign_ctx, txb_costs, bhl, tx_class);
+
+ int64_t dist_diff_0, dist_diff_low_0;
+ if (qmatrix == NULL) {
+ const int64_t tqc2 = (int64_t)tqc * 2;
+ dist_diff_0 = ((int64_t)dqc * (dqc - tqc2)) * (1LL << (2 * shift));
+ dist_diff_low_0 =
+ ((int64_t)dqc_low * (dqc_low - tqc2)) * (1LL << (2 * shift));
+ } else {
+ const int64_t qm_val = qmatrix[ci];
+ const int64_t dist0 = get_coeff_dist_qm(tqc, 0, shift, qm_val);
+ dist_diff_0 = get_coeff_dist_qm(tqc, dqc, shift, qm_val) - dist0;
+ dist_diff_low_0 = get_coeff_dist_qm(tqc, dqc_low, shift, qm_val) - dist0;
+ }
+
+ const int64_t rd = RDCOST(rdmult, rate, dist_diff_0);
+ const int64_t rd_low = RDCOST(rdmult, rate_low, dist_diff_low_0);
+
+ if (rd_low < rd) {
+ qcoeff[ci] = qc_low;
+ dqcoeff[ci] = dqc_low;
+ levels[get_padded_idx(ci, bhl)] = AOMMIN(abs_qc_low, INT8_MAX);
+ *accu_rate += rate_low;
+ *accu_dist += dist_diff_low_0;
+ } else {
+ *accu_rate += rate;
+ *accu_dist += dist_diff_0;
+ }
+}
+
+// Specialized trellis update for the DC coefficient (si == 0) when it is not
+// the last coefficient in the block.
+static AOM_FORCE_INLINE void update_coeff_dc(
+ int *accu_rate, TX_SIZE tx_size, TX_CLASS tx_class, int bhl, int64_t rdmult,
+ int shift, int dc_sign_ctx, const int16_t *dequant,
const LV_MAP_COEFF_COST *txb_costs, const tran_low_t *tcoeff,
tran_low_t *qcoeff, tran_low_t *dqcoeff, uint8_t *levels,
const qm_val_t *iqmatrix, const qm_val_t *qmatrix) {
- const int ci = scan[si];
- const tran_low_t qc = qcoeff[ci];
- const int is_last = si == (eob - 1);
- const int coeff_ctx = get_lower_levels_ctx_general(
- is_last, si, bhl, width, levels, ci, tx_size, tx_class);
+ const tran_low_t qc = qcoeff[0];
+ const int coeff_ctx = get_lower_levels_ctx(levels, 0, bhl, tx_size, tx_class);
if (qc == 0) {
*accu_rate += txb_costs->base_cost[coeff_ctx][0];
} else {
const int sign = (qc < 0) ? 1 : 0;
const tran_low_t abs_qc = abs(qc);
- const tran_low_t tqc = tcoeff[ci];
- const tran_low_t dqc = dqcoeff[ci];
- const int rate =
- get_coeff_cost_general(is_last, ci, abs_qc, sign, coeff_ctx,
- dc_sign_ctx, txb_costs, bhl, tx_class, levels);
+ const tran_low_t tqc = tcoeff[0];
+ const tran_low_t dqc = dqcoeff[0];
+ const int dc_sign_cost = txb_costs->dc_sign_cost[dc_sign_ctx][sign];
+ const int *const base_cost = txb_costs->base_cost[coeff_ctx];
+ int rate = base_cost[AOMMIN(abs_qc, 3)] + dc_sign_cost;
+ const int *lps_cost = txb_costs->lps_cost[0];
+ if (abs_qc > NUM_BASE_LEVELS) {
+ const int br_ctx = get_br_ctx(levels, 0, bhl, tx_class);
+ lps_cost = txb_costs->lps_cost[br_ctx];
+ rate += get_br_cost(abs_qc, lps_cost);
+ }
tran_low_t qc_low = 0, dqc_low = 0;
tran_low_t abs_qc_low = 0;
int rate_low;
if (abs_qc == 1) {
- rate_low = txb_costs->base_cost[coeff_ctx][0];
+ rate_low = base_cost[0];
} else {
- const int dqv = get_dqv(dequant, ci, iqmatrix);
+ const int dqv = get_dqv(dequant, 0, iqmatrix);
get_qc_dqc_low(abs_qc, sign, dqv, shift, &qc_low, &dqc_low);
abs_qc_low = abs_qc - 1;
- rate_low =
- get_coeff_cost_general(is_last, ci, abs_qc_low, sign, coeff_ctx,
- dc_sign_ctx, txb_costs, bhl, tx_class, levels);
+ rate_low = base_cost[AOMMIN(abs_qc_low, 3)] + dc_sign_cost;
+ if (abs_qc_low > NUM_BASE_LEVELS) {
+ rate_low += get_br_cost(abs_qc_low, lps_cost);
+ }
}
int64_t dist_diff_0, dist_diff_low_0;
@@ -62,7 +116,7 @@ static AOM_FORCE_INLINE void update_coeff_general(
? 0
: (((int64_t)dqc_low * (dqc_low - tqc2)) * (1LL << (2 * shift)));
} else {
- const int64_t qm_val = qmatrix[ci];
+ const int64_t qm_val = qmatrix[0];
const int64_t dist0 = get_coeff_dist_qm(tqc, 0, shift, qm_val);
dist_diff_0 = get_coeff_dist_qm(tqc, dqc, shift, qm_val) - dist0;
dist_diff_low_0 =
@@ -75,14 +129,12 @@ static AOM_FORCE_INLINE void update_coeff_general(
const int64_t rd_low = RDCOST(rdmult, rate_low, dist_diff_low_0);
if (rd_low < rd) {
- qcoeff[ci] = qc_low;
- dqcoeff[ci] = dqc_low;
- levels[get_padded_idx(ci, bhl)] = AOMMIN(abs_qc_low, INT8_MAX);
+ qcoeff[0] = qc_low;
+ dqcoeff[0] = dqc_low;
+ levels[0] = AOMMIN(abs_qc_low, INT8_MAX);
*accu_rate += rate_low;
- *accu_dist += dist_diff_low_0;
} else {
*accu_rate += rate;
- *accu_dist += dist_diff_0;
}
}
}
@@ -603,10 +655,10 @@ static int av1_optimize_txb_multi(const struct AV1_COMP *cpi, MACROBLOCK *x,
int nz_num = 1;
int nz_ci[3] = { ci, 0, 0 };
if (abs_qc >= 2) {
- update_coeff_general(&accu_rate, &accu_dist, si, eob, tx_size, tx_class,
- bhl, width, rdmult, shift, txb_ctx->dc_sign_ctx,
- dequant, scan, txb_costs, tcoeff, qcoeff, dqcoeff,
- levels, iqmatrix, qmatrix);
+ update_coeff_last(&accu_rate, &accu_dist, si, ci, abs_qc, sign, tx_class,
+ bhl, width, rdmult, shift, txb_ctx->dc_sign_ctx, dequant,
+ txb_costs, tcoeff, qcoeff, dqcoeff, levels, iqmatrix,
+ qmatrix);
--si;
} else {
assert(abs_qc == 1);
@@ -668,11 +720,9 @@ static int av1_optimize_txb_multi(const struct AV1_COMP *cpi, MACROBLOCK *x,
// DC position
if (si == 0) {
// no need to update accu_dist because it's not used after this point
- int64_t dummy_dist = 0;
- update_coeff_general(&accu_rate, &dummy_dist, si, eob, tx_size, tx_class,
- bhl, width, rdmult, shift, txb_ctx->dc_sign_ctx,
- dequant, scan, txb_costs, tcoeff, qcoeff, dqcoeff,
- levels, iqmatrix, qmatrix);
+ update_coeff_dc(&accu_rate, tx_size, tx_class, bhl, rdmult, shift,
+ txb_ctx->dc_sign_ctx, dequant, txb_costs, tcoeff, qcoeff,
+ dqcoeff, levels, iqmatrix, qmatrix);
}
p->eobs[block] = eob;