Commit 3735896a0d for aom
commit 3735896a0dc6f7a8432b2a566d0963fa80669a52
Author: Frank Galligan <fgalligan@google.com>
Date: Mon Nov 23 22:54:29 2026 -0800
av1: Add shared pc_root init and free helpers
Add av1_init_pc_tree_root() and av1_free_pc_tree_root() in
context_tree.{h,c} and use them across encodeframe.c, ethread.c,
encoder.c, and encoder_alloc.h. Free td->pc_root on mid-tile setjmp
and row_mt_exit paths to prevent leaks on aborted encodes, and pass
SEARCH_PARTITION in dealloc_compressor_data() during teardown.
Speedup: perf-neutral (bit-exact on successful encodes).
TAG=agy
CONV=b929a630-a6cd-45a0-979c-1767c30178ee
Change-Id: Ife8df8735b1bfc3db08f573a845c168950352272
diff --git a/av1/encoder/context_tree.c b/av1/encoder/context_tree.c
index 0075fcc6d3..0344cdc10f 100644
--- a/av1/encoder/context_tree.c
+++ b/av1/encoder/context_tree.c
@@ -231,6 +231,30 @@ void av1_free_pc_tree_recursive(PC_TREE *pc_tree, int num_planes, int keep_best,
if (!keep_best && !keep_none) aom_free(pc_tree);
}
+// Normal end-of-frame callers pass partition_search_type to keep
+// VAR_BASED_PARTITION debug asserts; error/teardown paths pass
+// SEARCH_PARTITION.
+void av1_free_pc_tree_root(struct ThreadData *td, int num_planes,
+ PARTITION_SEARCH_TYPE partition_search_type) {
+ assert(td != NULL);
+ if (td->pc_root != NULL) {
+ av1_free_pc_tree_recursive(td->pc_root, num_planes, 0, 0,
+ partition_search_type);
+ td->pc_root = NULL;
+ }
+}
+
+void av1_init_pc_tree_root(const struct AV1Common *cm, struct ThreadData *td) {
+ assert(cm != NULL && td != NULL);
+ // Defensively free any leftover td->pc_root using SEARCH_PARTITION.
+ av1_free_pc_tree_root(td, av1_num_planes(cm), SEARCH_PARTITION);
+ td->pc_root = av1_alloc_pc_tree_node(cm->seq_params->sb_size);
+ if (td->pc_root == NULL) {
+ aom_internal_error(td->mb.e_mbd.error_info, AOM_CODEC_MEM_ERROR,
+ "Failed to allocate PC_TREE");
+ }
+}
+
int av1_setup_sms_tree(AV1_COMP *const cpi, ThreadData *td) {
// The structure 'sms_tree' is used to store the simple motion search data for
// partition pruning in inter frames. Hence, the memory allocations and
diff --git a/av1/encoder/context_tree.h b/av1/encoder/context_tree.h
index 1578793c0e..e6285880ff 100644
--- a/av1/encoder/context_tree.h
+++ b/av1/encoder/context_tree.h
@@ -109,6 +109,11 @@ PC_TREE *av1_alloc_pc_tree_node(BLOCK_SIZE bsize);
void av1_free_pc_tree_recursive(PC_TREE *tree, int num_planes, int keep_best,
int keep_none,
PARTITION_SEARCH_TYPE partition_search_type);
+// Frees td->pc_root recursively if non-NULL and clears td->pc_root to NULL.
+void av1_free_pc_tree_root(struct ThreadData *td, int num_planes,
+ PARTITION_SEARCH_TYPE partition_search_type);
+// Frees any leftover td->pc_root and allocates a fresh root node for cm.
+void av1_init_pc_tree_root(const struct AV1Common *cm, struct ThreadData *td);
PICK_MODE_CONTEXT *av1_alloc_pmc(const struct AV1_COMP *const cpi,
BLOCK_SIZE bsize,
diff --git a/av1/encoder/encodeframe.c b/av1/encoder/encodeframe.c
index 2f8f6e65dc..f45628fb82 100644
--- a/av1/encoder/encodeframe.c
+++ b/av1/encoder/encodeframe.c
@@ -50,6 +50,7 @@
#include "av1/encoder/aq_cyclicrefresh.h"
#include "av1/encoder/aq_variance.h"
#include "av1/encoder/av1_quantize.h"
+#include "av1/encoder/context_tree.h"
#include "av1/encoder/global_motion_facade.h"
#include "av1/encoder/encodeframe.h"
#include "av1/encoder/encodeframe_utils.h"
@@ -2726,22 +2727,19 @@ static inline void encode_frame_internal(AV1_COMP *cpi) {
if (num_workers > 1) {
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) {
- td->pc_root = av1_alloc_pc_tree_node(cm->seq_params->sb_size);
- if (!td->pc_root)
- aom_internal_error(xd->error_info, AOM_CODEC_MEM_ERROR,
- "Failed to allocate PC_TREE");
+ av1_init_pc_tree_root(cm, td);
} else {
- td->pc_root = NULL;
+ av1_free_pc_tree_root(td, num_planes, SEARCH_PARTITION);
}
encode_tiles(cpi);
- av1_free_pc_tree_recursive(td->pc_root, av1_num_planes(cm), 0, 0,
- cpi->sf.part_sf.partition_search_type);
- td->pc_root = NULL;
+ av1_free_pc_tree_root(td, num_planes,
+ cpi->sf.part_sf.partition_search_type);
}
}
diff --git a/av1/encoder/encoder.c b/av1/encoder/encoder.c
index d8f1d003ab..8cb756de20 100644
--- a/av1/encoder/encoder.c
+++ b/av1/encoder/encoder.c
@@ -5544,6 +5544,7 @@ int av1_get_compressed_data(AV1_COMP *cpi, AV1_COMP_DATA *const cpi_data) {
// before it returns.
if (setjmp(cm->error->jmp)) {
cm->error->setjmp = 0;
+ av1_free_pc_tree_root(&cpi->td, av1_num_planes(cm), SEARCH_PARTITION);
// Drop the source frame that failed to encode. Left in the lookahead, it
// would be retried on every later call and block the frames behind it.
// This is only safe in one pass mode without lag, where no other state
diff --git a/av1/encoder/encoder_alloc.h b/av1/encoder/encoder_alloc.h
index 1295aedd57..84c4c52fca 100644
--- a/av1/encoder/encoder_alloc.h
+++ b/av1/encoder/encoder_alloc.h
@@ -13,6 +13,7 @@
#define AOM_AV1_ENCODER_ENCODER_ALLOC_H_
#include "av1/encoder/block.h"
+#include "av1/encoder/context_tree.h"
#include "av1/encoder/encodeframe_utils.h"
#include "av1/encoder/encoder.h"
#include "av1/encoder/encoder_utils.h"
@@ -355,9 +356,7 @@ static inline void dealloc_compressor_data(AV1_COMP *cpi) {
cpi->td.mb.rdcost = NULL;
#endif
- av1_free_pc_tree_recursive(cpi->td.pc_root, num_planes, 0, 0,
- cpi->sf.part_sf.partition_search_type);
- cpi->td.pc_root = NULL;
+ av1_free_pc_tree_root(&cpi->td, num_planes, SEARCH_PARTITION);
for (int i = 0; i < 2; i++) {
aom_free(cpi->td.mb.intrabc_hash_info.hash_value_buffer[i]);
@@ -634,8 +633,7 @@ static inline void free_thread_data(AV1_PRIMARY *ppi) {
aom_free(td->mb.rdcost);
td->mb.rdcost = NULL;
#endif
- av1_free_pc_tree_recursive(td->pc_root, num_planes, 0, 0, SEARCH_PARTITION);
- td->pc_root = NULL;
+ av1_free_pc_tree_root(td, num_planes, SEARCH_PARTITION);
av1_dealloc_mb_wiener_var_pred_buf(td);
aom_free(td);
thread_data->td = NULL;
diff --git a/av1/encoder/ethread.c b/av1/encoder/ethread.c
index 5f90a06454..2500d91560 100644
--- a/av1/encoder/ethread.c
+++ b/av1/encoder/ethread.c
@@ -19,6 +19,7 @@
#include "av1/encoder/allintra_vis.h"
#include "av1/encoder/bitstream.h"
+#include "av1/encoder/context_tree.h"
#include "av1/encoder/enc_enums.h"
#include "av1/encoder/encodeframe.h"
#include "av1/encoder/encodeframe_utils.h"
@@ -632,22 +633,22 @@ static int enc_row_mt_worker_hook(void *arg1, void *unused) {
av1_set_vert_loop_filter_done(&cpi->common, lf_sync,
cpi->common.seq_params->mib_size_log2);
}
+ av1_free_pc_tree_root(thread_data->td, av1_num_planes(&cpi->common),
+ SEARCH_PARTITION);
return 0;
}
error_info->setjmp = 1;
const int mib_size_log2 = cm->seq_params->mib_size_log2;
+ 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) {
- thread_data->td->pc_root = av1_alloc_pc_tree_node(cm->seq_params->sb_size);
- if (!thread_data->td->pc_root)
- aom_internal_error(xd->error_info, AOM_CODEC_MEM_ERROR,
- "Failed to allocate PC_TREE");
+ av1_init_pc_tree_root(cm, thread_data->td);
} else {
- thread_data->td->pc_root = NULL;
+ av1_free_pc_tree_root(thread_data->td, num_planes, SEARCH_PARTITION);
}
assert(cur_tile_id != -1);
@@ -686,6 +687,7 @@ static int enc_row_mt_worker_hook(void *arg1, void *unused) {
// When row_mt_exit is set to true, other workers need not pursue any
// further jobs.
if (row_mt_exit) {
+ av1_free_pc_tree_root(thread_data->td, num_planes, SEARCH_PARTITION);
error_info->setjmp = 0;
return 1;
}
@@ -714,7 +716,7 @@ static int enc_row_mt_worker_hook(void *arg1, void *unused) {
*td->mb.e_mbd.tile_ctx = this_tile->tctx;
}
- av1_init_above_context(&cm->above_contexts, av1_num_planes(cm), tile_row,
+ av1_init_above_context(&cm->above_contexts, num_planes, tile_row,
&td->mb.e_mbd);
#if !CONFIG_REALTIME_ONLY
cfl_init(&td->mb.e_mbd.cfl, cm->seq_params);
@@ -743,9 +745,8 @@ static int enc_row_mt_worker_hook(void *arg1, void *unused) {
// encoding and loop filter stage.
launch_loop_filter_rows(cm, thread_data, enc_row_mt, mib_size_log2);
}
- av1_free_pc_tree_recursive(thread_data->td->pc_root, av1_num_planes(cm), 0, 0,
- cpi->sf.part_sf.partition_search_type);
- thread_data->td->pc_root = NULL;
+ av1_free_pc_tree_root(thread_data->td, num_planes,
+ cpi->sf.part_sf.partition_search_type);
error_info->setjmp = 0;
return 1;
}
@@ -768,19 +769,19 @@ static int enc_worker_hook(void *arg1, void *arg2) {
// before it returns.
if (setjmp(error_info->jmp)) {
error_info->setjmp = 0;
+ av1_free_pc_tree_root(thread_data->td, av1_num_planes(&cpi->common),
+ SEARCH_PARTITION);
return 0;
}
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) {
- thread_data->td->pc_root = av1_alloc_pc_tree_node(cm->seq_params->sb_size);
- if (!thread_data->td->pc_root)
- aom_internal_error(xd->error_info, AOM_CODEC_MEM_ERROR,
- "Failed to allocate PC_TREE");
+ av1_init_pc_tree_root(cm, thread_data->td);
} else {
- thread_data->td->pc_root = NULL;
+ av1_free_pc_tree_root(thread_data->td, num_planes, SEARCH_PARTITION);
}
for (t = thread_data->start; t < tile_rows * tile_cols; t += num_workers) {
@@ -794,9 +795,8 @@ static int enc_worker_hook(void *arg1, void *arg2) {
av1_encode_tile(cpi, thread_data->td, tile_row, tile_col);
}
- av1_free_pc_tree_recursive(thread_data->td->pc_root, av1_num_planes(cm), 0, 0,
- cpi->sf.part_sf.partition_search_type);
- thread_data->td->pc_root = NULL;
+ av1_free_pc_tree_root(thread_data->td, num_planes,
+ cpi->sf.part_sf.partition_search_type);
error_info->setjmp = 0;
return 1;
}