Commit d91e90d7c8 for aom
commit d91e90d7c8949f46f52ac321100f4b445837d70b
Author: Frank Galligan <fgalligan@google.com>
Date: Tue Nov 17 06:28:44 2026 -0800
av1: Reuse PC_TREE for VAR_BASED_PARTITION
Preallocate and reuse td->pc_root across superblocks when
partition_search_type == VAR_BASED_PARTITION (covering --rt speeds
<= 6 and --allintra speed 7 on the RD path). In av1_rd_use_partition(),
lazily allocate split subtrees, defer horizontal[1]/vertical[1]
PICK_MODE_CONTEXT allocation/reset past border checks, and reset
reused entries via reset_pmc_rd() without adding clearing overhead to
the nonrd av1_reset_pmc() hot path (--rt speed >= 7, --allintra >= 8).
get_or_reset_pmc_rd() also zeros eobs[0] when
skip_tx_no_split_var_based_partition and try_merge_partition are active
on inter/IntraBC frames so reused contexts do not leak stale eobs.
Check frame bounds before dereferencing pc_tree in encode_sb(), fall
back cleanly from an incomplete sub-block PARTITION_SPLIT break to
PARTITION_NONE, and report an internal error if a root superblock ever
produces invalid RD.
Speedup (Pixel 9 Tensor G4 Cortex-A720, 768x1024 intra, bit-exact,
aomenc --rt --cpu-used=6 --kf-max-dist=1 --threads=1): -4.73% encode
time.
TAG=agy
CONV=b929a630-a6cd-45a0-979c-1767c30178ee
Change-Id: Id4bc6dc9105e65ae2ca2269f3207d28880966c24
diff --git a/av1/encoder/encodeframe.c b/av1/encoder/encodeframe.c
index b15b4230a4..3429af1c3d 100644
--- a/av1/encoder/encodeframe.c
+++ b/av1/encoder/encodeframe.c
@@ -9,6 +9,7 @@
* PATENTS file, you can obtain it at www.aomedia.org/license/patent.
*/
+#include <assert.h>
#include <limits.h>
#include <float.h>
#include <math.h>
@@ -869,13 +870,11 @@ static inline void encode_rd_sb(AV1_COMP *cpi, ThreadData *td,
const int seg_skip) {
AV1_COMMON *const cm = &cpi->common;
MACROBLOCK *const x = &td->mb;
- MACROBLOCKD *const xd = &x->e_mbd;
const SPEED_FEATURES *const sf = &cpi->sf;
const TileInfo *const tile_info = &tile_data->tile_info;
MB_MODE_INFO **mi = cm->mi_params.mi_grid_base +
get_mi_grid_idx(&cm->mi_params, mi_row, mi_col);
const BLOCK_SIZE sb_size = cm->seq_params->sb_size;
- const int num_planes = av1_num_planes(cm);
int dummy_rate;
int64_t dummy_dist;
RD_STATS dummy_rdc;
@@ -883,6 +882,9 @@ static inline void encode_rd_sb(AV1_COMP *cpi, ThreadData *td,
#if CONFIG_REALTIME_ONLY
(void)seg_skip;
+#else
+ MACROBLOCKD *const xd = &x->e_mbd;
+ const int num_planes = av1_num_planes(cm);
#endif // CONFIG_REALTIME_ONLY
init_encode_rd_sb(cpi, td, tile_data, sms_root, &dummy_rdc, mi_row, mi_col,
@@ -897,15 +899,11 @@ static inline void encode_rd_sb(AV1_COMP *cpi, ThreadData *td,
#if CONFIG_COLLECT_COMPONENT_TIMING
start_timing(cpi, rd_use_partition_time);
#endif
- td->pc_root = av1_alloc_pc_tree_node(sb_size);
- if (!td->pc_root)
- aom_internal_error(xd->error_info, AOM_CODEC_MEM_ERROR,
- "Failed to allocate PC_TREE");
+ assert(td->pc_root != NULL);
+ td->pc_root->partitioning = PARTITION_NONE;
+ td->pc_root->block_size = sb_size;
av1_rd_use_partition(cpi, td, tile_data, mi, tp, mi_row, mi_col, sb_size,
&dummy_rate, &dummy_dist, 1, td->pc_root);
- av1_free_pc_tree_recursive(td->pc_root, num_planes, 0, 0,
- sf->part_sf.partition_search_type);
- td->pc_root = NULL;
#if CONFIG_COLLECT_COMPONENT_TIMING
end_timing(cpi, rd_use_partition_time);
#endif
@@ -2735,10 +2733,12 @@ static inline void encode_frame_internal(AV1_COMP *cpi) {
av1_encode_tiles_mt(cpi);
} else {
const int num_planes = av1_num_planes(cm);
- // Preallocate the pc_tree for realtime coding to reduce the cost of
- // memory allocation.
- const int use_nonrd_mode = cpi->sf.rt_sf.use_nonrd_pick_mode;
- if (use_nonrd_mode) {
+ // Preallocate the pc_tree for nonrd and variance-based partitioning to
+ // reduce the cost of memory allocation.
+ const bool reuse_pc_tree =
+ cpi->sf.rt_sf.use_nonrd_pick_mode ||
+ cpi->sf.part_sf.partition_search_type == VAR_BASED_PARTITION;
+ if (reuse_pc_tree) {
av1_init_pc_tree_root(cm, td);
} else {
av1_free_pc_tree_root(td, num_planes, SEARCH_PARTITION);
diff --git a/av1/encoder/ethread.c b/av1/encoder/ethread.c
index 48b2e393d7..978f927791 100644
--- a/av1/encoder/ethread.c
+++ b/av1/encoder/ethread.c
@@ -714,9 +714,10 @@ static int enc_row_mt_worker_hook(void *arg1, void *unused) {
const int num_planes = av1_num_planes(cm);
int cur_tile_id = enc_row_mt->thread_id_to_tile_id[thread_id];
- // Preallocate the pc_tree for realtime coding to reduce the cost of memory
- // allocation.
- if (cpi->sf.rt_sf.use_nonrd_pick_mode) {
+ // Preallocate the pc_tree for nonrd and variance-based partitioning to reduce
+ // the cost of memory allocation.
+ if (cpi->sf.rt_sf.use_nonrd_pick_mode ||
+ cpi->sf.part_sf.partition_search_type == VAR_BASED_PARTITION) {
av1_init_pc_tree_root(cm, thread_data->td);
} else {
av1_free_pc_tree_root(thread_data->td, num_planes, SEARCH_PARTITION);
@@ -851,9 +852,10 @@ static int enc_worker_hook(void *arg1, void *arg2) {
error_info->setjmp = 1;
const int num_planes = av1_num_planes(cm);
- // Preallocate the pc_tree for realtime coding to reduce the cost of memory
- // allocation.
- if (cpi->sf.rt_sf.use_nonrd_pick_mode) {
+ // Preallocate the pc_tree for nonrd and variance-based partitioning to reduce
+ // the cost of memory allocation.
+ if (cpi->sf.rt_sf.use_nonrd_pick_mode ||
+ cpi->sf.part_sf.partition_search_type == VAR_BASED_PARTITION) {
av1_init_pc_tree_root(cm, thread_data->td);
} else {
av1_free_pc_tree_root(thread_data->td, num_planes, SEARCH_PARTITION);
diff --git a/av1/encoder/partition_search.c b/av1/encoder/partition_search.c
index ac9e1735aa..d3d522ab46 100644
--- a/av1/encoder/partition_search.c
+++ b/av1/encoder/partition_search.c
@@ -9,6 +9,7 @@
* PATENTS file, you can obtain it at www.aomedia.org/license/patent.
*/
+#include <assert.h>
#include <float.h>
#include <inttypes.h>
@@ -1606,6 +1607,8 @@ static void encode_sb(const AV1_COMP *const cpi, ThreadData *td,
assert(bsize < BLOCK_SIZES_ALL);
const AV1_COMMON *const cm = &cpi->common;
const CommonModeInfoParams *const mi_params = &cm->mi_params;
+ if (mi_row >= mi_params->mi_rows || mi_col >= mi_params->mi_cols) return;
+ assert(pc_tree != NULL);
MACROBLOCK *const x = &td->mb;
MACROBLOCKD *const xd = &x->e_mbd;
assert(bsize < BLOCK_SIZES_ALL);
@@ -1622,7 +1625,6 @@ static void encode_sb(const AV1_COMP *const cpi, ThreadData *td,
BLOCK_SIZE bsize2 = get_partition_subsize(bsize, PARTITION_SPLIT);
#endif
- if (mi_row >= mi_params->mi_rows || mi_col >= mi_params->mi_cols) return;
if (subsize == BLOCK_INVALID) return;
if (!dry_run && ctx >= 0) {
@@ -1749,6 +1751,62 @@ static inline int is_adjust_var_based_part_enabled(
return 0;
}
+// Clears RD-specific fields (mic, mbmi_ext_best, skippable, rd_mode_is_ready)
+// that a fresh av1_alloc_pmc() would zero, beyond what av1_reset_pmc() resets.
+static void reset_pmc_rd(PICK_MODE_CONTEXT *ctx) {
+ av1_reset_pmc(ctx);
+ av1_zero(ctx->mic);
+ av1_zero(ctx->mbmi_ext_best);
+ ctx->skippable = 0;
+#if CONFIG_INTERNAL_STATS
+ ctx->best_mode_index = 0;
+#endif // CONFIG_INTERNAL_STATS
+ ctx->rd_mode_is_ready = 0;
+}
+
+static PICK_MODE_CONTEXT *get_or_reset_pmc_rd(const AV1_COMP *cpi,
+ ThreadData *td,
+ PICK_MODE_CONTEXT **ctx,
+ BLOCK_SIZE bsize) {
+ assert(bsize < BLOCK_SIZES_ALL);
+ if (*ctx == NULL) {
+ *ctx = av1_alloc_pmc(cpi, bsize, &td->shared_coeff_buf);
+ if (!*ctx) {
+ aom_internal_error(td->mb.e_mbd.error_info, AOM_CODEC_MEM_ERROR,
+ "Failed to allocate PICK_MODE_CONTEXT");
+ }
+ } else {
+ assert((*ctx)->num_4x4_blk ==
+ (block_size_wide[bsize] * block_size_high[bsize]) / 16);
+ reset_pmc_rd(*ctx);
+ }
+ if (cpi->sf.rt_sf.skip_tx_no_split_var_based_partition &&
+ td->mb.try_merge_partition &&
+ (!frame_is_intra_only(&cpi->common) || av1_allow_intrabc(&cpi->common))) {
+ av1_zero_array((*ctx)->eobs[0], (*ctx)->num_4x4_blk);
+ }
+ return *ctx;
+}
+
+static void get_or_reset_split_node(ThreadData *td, PC_TREE *pc_tree, int index,
+ BLOCK_SIZE bsize) {
+ assert(pc_tree != NULL);
+ assert(index >= 0 && index < SUB_PARTITIONS_SPLIT);
+ assert(bsize < BLOCK_SIZES_ALL);
+ if (pc_tree->split[index] == NULL) {
+ pc_tree->split[index] = av1_alloc_pc_tree_node(bsize);
+ if (!pc_tree->split[index]) {
+ aom_internal_error(td->mb.e_mbd.error_info, AOM_CODEC_MEM_ERROR,
+ "Failed to allocate PC_TREE");
+ }
+ pc_tree->split[index]->index = index;
+ } else {
+ assert(pc_tree->split[index]->index == index);
+ pc_tree->split[index]->partitioning = PARTITION_NONE;
+ pc_tree->split[index]->block_size = bsize;
+ }
+}
+
/*!\brief AV1 block partition search (partition estimation and partial search).
*
* \ingroup partition_search
@@ -1808,14 +1866,6 @@ void av1_rd_use_partition(AV1_COMP *cpi, ThreadData *td, TileDataEnc *tile_data,
int use_partition_none = 0;
x->try_merge_partition = 0;
- if (pc_tree->none == NULL) {
- pc_tree->none = av1_alloc_pmc(cpi, bsize, &td->shared_coeff_buf);
- if (!pc_tree->none)
- aom_internal_error(xd->error_info, AOM_CODEC_MEM_ERROR,
- "Failed to allocate PICK_MODE_CONTEXT");
- }
- PICK_MODE_CONTEXT *ctx_none = pc_tree->none;
-
if (mi_row >= mi_params->mi_rows || mi_col >= mi_params->mi_cols) return;
assert(mi_size_wide[bsize] == mi_size_high[bsize]);
@@ -1852,6 +1902,8 @@ void av1_rd_use_partition(AV1_COMP *cpi, ThreadData *td, TileDataEnc *tile_data,
mib[0]->bsize = bsize;
pc_tree->partitioning = PARTITION_NONE;
x->try_merge_partition = 1;
+ PICK_MODE_CONTEXT *const ctx_none =
+ get_or_reset_pmc_rd(cpi, td, &pc_tree->none, bsize);
pick_sb_modes(cpi, tile_data, x, mi_row, mi_col, &none_rdc, PARTITION_NONE,
bsize, ctx_none, invalid_rdc);
@@ -1871,45 +1923,35 @@ void av1_rd_use_partition(AV1_COMP *cpi, ThreadData *td, TileDataEnc *tile_data,
pc_tree->partitioning = partition;
}
- for (int i = 0; i < SUB_PARTITIONS_SPLIT; ++i) {
- pc_tree->split[i] = av1_alloc_pc_tree_node(subsize);
- if (!pc_tree->split[i])
- aom_internal_error(xd->error_info, AOM_CODEC_MEM_ERROR,
- "Failed to allocate PC_TREE");
- pc_tree->split[i]->index = i;
- }
switch (partition) {
- case PARTITION_NONE:
+ case PARTITION_NONE: {
+ PICK_MODE_CONTEXT *const ctx_none =
+ get_or_reset_pmc_rd(cpi, td, &pc_tree->none, bsize);
pick_sb_modes(cpi, tile_data, x, mi_row, mi_col, &last_part_rdc,
PARTITION_NONE, bsize, ctx_none, invalid_rdc);
break;
- case PARTITION_HORZ:
+ }
+ case PARTITION_HORZ: {
if (use_partition_none) {
av1_invalid_rd_stats(&last_part_rdc);
break;
}
- for (int i = 0; i < SUB_PARTITIONS_RECT; ++i) {
- pc_tree->horizontal[i] =
- av1_alloc_pmc(cpi, subsize, &td->shared_coeff_buf);
- if (!pc_tree->horizontal[i])
- aom_internal_error(xd->error_info, AOM_CODEC_MEM_ERROR,
- "Failed to allocate PICK_MODE_CONTEXT");
- }
+ PICK_MODE_CONTEXT *const ctx_h0 =
+ get_or_reset_pmc_rd(cpi, td, &pc_tree->horizontal[0], subsize);
pick_sb_modes(cpi, tile_data, x, mi_row, mi_col, &last_part_rdc,
- PARTITION_HORZ, subsize, pc_tree->horizontal[0],
- invalid_rdc);
+ PARTITION_HORZ, subsize, ctx_h0, invalid_rdc);
if (last_part_rdc.rate != INT_MAX && bsize >= BLOCK_8X8 &&
mi_row + hbs < mi_params->mi_rows) {
RD_STATS tmp_rdc;
- const PICK_MODE_CONTEXT *const ctx_h = pc_tree->horizontal[0];
av1_init_rd_stats(&tmp_rdc);
- av1_update_state(cpi, td, ctx_h, mi_row, mi_col, subsize, 1);
+ av1_update_state(cpi, td, ctx_h0, mi_row, mi_col, subsize, 1);
encode_superblock(cpi, tile_data, td, tp, DRY_RUN_NORMAL, subsize,
NULL);
+ PICK_MODE_CONTEXT *const ctx_h1 =
+ get_or_reset_pmc_rd(cpi, td, &pc_tree->horizontal[1], subsize);
pick_sb_modes(cpi, tile_data, x, mi_row + hbs, mi_col, &tmp_rdc,
- PARTITION_HORZ, subsize, pc_tree->horizontal[1],
- invalid_rdc);
+ PARTITION_HORZ, subsize, ctx_h1, invalid_rdc);
if (tmp_rdc.rate == INT_MAX || tmp_rdc.dist == INT64_MAX) {
av1_invalid_rd_stats(&last_part_rdc);
break;
@@ -1919,32 +1961,29 @@ void av1_rd_use_partition(AV1_COMP *cpi, ThreadData *td, TileDataEnc *tile_data,
last_part_rdc.rdcost += tmp_rdc.rdcost;
}
break;
- case PARTITION_VERT:
+ }
+ case PARTITION_VERT: {
if (use_partition_none) {
av1_invalid_rd_stats(&last_part_rdc);
break;
}
- for (int i = 0; i < SUB_PARTITIONS_RECT; ++i) {
- pc_tree->vertical[i] =
- av1_alloc_pmc(cpi, subsize, &td->shared_coeff_buf);
- if (!pc_tree->vertical[i])
- aom_internal_error(xd->error_info, AOM_CODEC_MEM_ERROR,
- "Failed to allocate PICK_MODE_CONTEXT");
- }
+ PICK_MODE_CONTEXT *const ctx_v0 =
+ get_or_reset_pmc_rd(cpi, td, &pc_tree->vertical[0], subsize);
pick_sb_modes(cpi, tile_data, x, mi_row, mi_col, &last_part_rdc,
- PARTITION_VERT, subsize, pc_tree->vertical[0], invalid_rdc);
+ PARTITION_VERT, subsize, ctx_v0, invalid_rdc);
if (last_part_rdc.rate != INT_MAX && bsize >= BLOCK_8X8 &&
mi_col + hbs < mi_params->mi_cols) {
RD_STATS tmp_rdc;
- const PICK_MODE_CONTEXT *const ctx_v = pc_tree->vertical[0];
av1_init_rd_stats(&tmp_rdc);
- av1_update_state(cpi, td, ctx_v, mi_row, mi_col, subsize, 1);
+ av1_update_state(cpi, td, ctx_v0, mi_row, mi_col, subsize, 1);
encode_superblock(cpi, tile_data, td, tp, DRY_RUN_NORMAL, subsize,
NULL);
+ PICK_MODE_CONTEXT *const ctx_v1 =
+ get_or_reset_pmc_rd(cpi, td, &pc_tree->vertical[1], subsize);
pick_sb_modes(cpi, tile_data, x, mi_row, mi_col + hbs, &tmp_rdc,
PARTITION_VERT, subsize,
- pc_tree->vertical[bsize > BLOCK_8X8], invalid_rdc);
+ (bsize > BLOCK_8X8) ? ctx_v1 : ctx_v0, invalid_rdc);
if (tmp_rdc.rate == INT_MAX || tmp_rdc.dist == INT64_MAX) {
av1_invalid_rd_stats(&last_part_rdc);
break;
@@ -1954,6 +1993,7 @@ void av1_rd_use_partition(AV1_COMP *cpi, ThreadData *td, TileDataEnc *tile_data,
last_part_rdc.rdcost += tmp_rdc.rdcost;
}
break;
+ }
case PARTITION_SPLIT:
if (use_partition_none) {
av1_invalid_rd_stats(&last_part_rdc);
@@ -1972,6 +2012,8 @@ void av1_rd_use_partition(AV1_COMP *cpi, ThreadData *td, TileDataEnc *tile_data,
(mi_col + x_idx >= mi_params->mi_cols))
continue;
+ get_or_reset_split_node(td, pc_tree, i, subsize);
+
av1_init_rd_stats(&tmp_rdc);
av1_rd_use_partition(
cpi, td, tile_data,
@@ -2027,16 +2069,11 @@ void av1_rd_use_partition(AV1_COMP *cpi, ThreadData *td, TileDataEnc *tile_data,
continue;
av1_save_context(x, &x_ctx, mi_row, mi_col, bsize, num_planes);
- pc_tree->split[i]->partitioning = PARTITION_NONE;
- if (pc_tree->split[i]->none == NULL)
- pc_tree->split[i]->none =
- av1_alloc_pmc(cpi, split_subsize, &td->shared_coeff_buf);
- if (!pc_tree->split[i]->none)
- aom_internal_error(xd->error_info, AOM_CODEC_MEM_ERROR,
- "Failed to allocate PICK_MODE_CONTEXT");
+ get_or_reset_split_node(td, pc_tree, i, split_subsize);
+ PICK_MODE_CONTEXT *const split_ctx =
+ get_or_reset_pmc_rd(cpi, td, &pc_tree->split[i]->none, split_subsize);
pick_sb_modes(cpi, tile_data, x, mi_row + y_idx, mi_col + x_idx, &tmp_rdc,
- PARTITION_SPLIT, split_subsize, pc_tree->split[i]->none,
- invalid_rdc);
+ PARTITION_SPLIT, split_subsize, split_ctx, invalid_rdc);
av1_restore_context(x, &x_ctx, mi_row, mi_col, bsize, num_planes);
if (tmp_rdc.rate == INT_MAX || tmp_rdc.dist == INT64_MAX) {
@@ -2080,11 +2117,20 @@ void av1_rd_use_partition(AV1_COMP *cpi, ThreadData *td, TileDataEnc *tile_data,
// No other opportunities for success.
if (bsize == cm->seq_params->sb_size)
assert(chosen_rdc.rate < INT_MAX && chosen_rdc.dist < INT64_MAX);
+ // If a sub-block hits invalid RD, avoid recursing into unpopulated/stale
+ // split children in encode_sb(); at root SB, fail rather than skipping recon.
+ if (chosen_rdc.rate == INT_MAX || chosen_rdc.dist == INT64_MAX) {
+ pc_tree->partitioning = PARTITION_NONE;
+ if (bsize == cm->seq_params->sb_size) {
+ aom_internal_error(xd->error_info, AOM_CODEC_ERROR,
+ "No valid partition for superblock");
+ }
+ }
#if CONFIG_COLLECT_COMPONENT_TIMING
start_timing(cpi, encode_sb_time);
#endif
- if (do_recon) {
+ if (do_recon && chosen_rdc.rate < INT_MAX && chosen_rdc.dist < INT64_MAX) {
if (bsize == cm->seq_params->sb_size) {
// NOTE: To get estimate for rate due to the tokens, use:
// int rate_coeffs = 0;