Commit 5980accbef for aom
commit 5980accbefbc3312f3594a8528c0f1c7a3a66688
Author: Frank Galligan <fgalligan@google.com>
Date: Thu Dec 3 21:26:04 2026 -0800
context_tree: add PmcPool & fast teardown
Pool PICK_MODE_CONTEXT allocations per thread inside
PC_TREE_SHARED_BUFFERS->pmc_pool (2 x 11 intrusive freelist buckets by
[use_palette][get_msb(num_4x4_blk)], bounded to 16 nodes/bucket, ~0.9 MB
max retained per thread) to eliminate repeated calloc/free churn and
full-struct zeroing during partition search.
- zero_pmc_fast() zeroes [0, color_index_map) and the tail starting at
skippable, preserving pointer fields and num_4x4_blk, guarded by
C11 static_asserts verifying exact layout contiguity.
- Move av1_free_pmc() fast path inline for pooled teardown and add
av1_free_pc_tree_root() / av1_init_pc_tree_root() cleanup helpers.
- Add EncodeInternal.PmcPoolReuseAndZeroFast unit test.
Speedup (x86_64 -O3, setarch -R, bit-exact):
allintra 1080p: -1.43% (1T) / -0.56% (8T);
allintra 1080p screendata: -0.52% (1T) / -4.67% (8T tiled);
good 2-pass 720p: -0.84% (1T); good 1-pass: -0.52% (1T);
rt 720p: -1.66% (1T) encode time reduction.
Binary size (x86_64 -O3): libaom.a .text +4.9 KiB (+0.06%),
.rodata +0.1 KiB (+0.01%); stripped aomenc +4.0 KiB (+0.04%).
Memory (peak RSS, PR_SET_THP_DISABLE+no_aslr): 1080p allintra 1T
+0 KiB (37288 KiB), 8T +0 KiB (37224 KiB); 720p good 1T +0 KiB
(244200 KiB), rt 1T -112 KiB (-0.26%).
TAG=agy
CONV=65f7dcec-dd34-49ef-8d71-23221fddd279
Change-Id: If5d4cf62f4299dd98658338614ae3ac7bddc63c9
diff --git a/av1/encoder/context_tree.c b/av1/encoder/context_tree.c
index 0344cdc10f..a01425688e 100644
--- a/av1/encoder/context_tree.c
+++ b/av1/encoder/context_tree.c
@@ -10,12 +10,108 @@
*/
#include "av1/encoder/context_tree.h"
+
+#include <assert.h>
+#include <stdbool.h>
+#include <stddef.h>
+#include <stdint.h>
+#include <string.h>
+
+#include "aom_dsp/aom_dsp_common.h"
+#include "aom_ports/bitops.h"
#include "av1/encoder/encoder.h"
#include "av1/encoder/rd.h"
-#include <assert.h>
+
+#define MAX_PMC_POOL_BUCKET_DEPTH 16
+#define PMC_POOL_NUM_PAL 2
+#define PMC_POOL_NUM_SIZES 11 // get_msb(128 * 128 / 16) + 1
+
+// Bounded per-thread free-list pool of PICK_MODE_CONTEXT nodes indexed by
+// [use_palette][get_msb(num_4x4_blk)] (2 * 11 = 22 buckets * up to 16 nodes =
+// 352 nodes max; since 64x128/128x128 exceed MAX_PALETTE_SQUARE, at most 20
+// buckets * 16 = 320 nodes can be populated, bounding worst-case per-thread
+// retained pool memory to ~0.9 MB).
+struct PmcPool {
+ PICK_MODE_CONTEXT *pmc_free[PMC_POOL_NUM_PAL][PMC_POOL_NUM_SIZES];
+ uint8_t count[PMC_POOL_NUM_PAL][PMC_POOL_NUM_SIZES];
+};
+
+static_assert(
+ (1 << (PMC_POOL_NUM_SIZES - 1)) == MAX_SB_SQUARE / 16,
+ "PMC_POOL_NUM_SIZES must cover all block sizes up to MAX_SB_SIZE");
+static_assert(offsetof(PICK_MODE_CONTEXT, color_index_map) >=
+ sizeof(PICK_MODE_CONTEXT *),
+ "freelist next pointer must fit before color_index_map");
+static_assert(offsetof(PICK_MODE_CONTEXT, tx_type_map) >
+ offsetof(PICK_MODE_CONTEXT, color_index_map),
+ "tx_type_map must remain after color_index_map");
+static_assert(offsetof(PICK_MODE_CONTEXT, num_4x4_blk) ==
+ offsetof(PICK_MODE_CONTEXT, tx_type_map) +
+ sizeof(((PICK_MODE_CONTEXT *)0)->tx_type_map),
+ "num_4x4_blk must immediately follow tx_type_map");
+static_assert(
+ offsetof(PICK_MODE_CONTEXT, skippable) -
+ offsetof(PICK_MODE_CONTEXT, color_index_map) ==
+ sizeof(((PICK_MODE_CONTEXT *)0)->color_index_map) +
+ sizeof(((PICK_MODE_CONTEXT *)0)->coeff) +
+ sizeof(((PICK_MODE_CONTEXT *)0)->qcoeff) +
+ sizeof(((PICK_MODE_CONTEXT *)0)->dqcoeff) +
+ sizeof(((PICK_MODE_CONTEXT *)0)->eobs) +
+ sizeof(((PICK_MODE_CONTEXT *)0)->txb_entropy_ctx) +
+ sizeof(((PICK_MODE_CONTEXT *)0)->tx_type_map) +
+ sizeof(((PICK_MODE_CONTEXT *)0)->num_4x4_blk),
+ "zero_pmc_fast() preserved region must contain only pointer fields and "
+ "num_4x4_blk");
+static_assert(offsetof(PICK_MODE_CONTEXT, skippable) >
+ offsetof(PICK_MODE_CONTEXT, num_4x4_blk),
+ "tail zeroing must not clobber num_4x4_blk");
+static_assert(offsetof(PICK_MODE_CONTEXT, rd_mode_is_ready) >
+ offsetof(PICK_MODE_CONTEXT, skippable),
+ "rd_mode_is_ready must fall within tail-zeroed region");
+
+static inline int pmc_size_idx(int num_blk) {
+ assert(num_blk > 0 && (num_blk & (num_blk - 1)) == 0);
+ const int idx = get_msb(num_blk);
+ assert(idx >= 0 && idx < PMC_POOL_NUM_SIZES);
+ return idx;
+}
+
+static inline PICK_MODE_CONTEXT *pmc_get_next_free(const void *ctx) {
+ PICK_MODE_CONTEXT *next;
+ memcpy(&next, ctx, sizeof(next));
+ return next;
+}
+
+static inline void pmc_set_next_free(void *ctx, PICK_MODE_CONTEXT *next) {
+ memcpy(ctx, &next, sizeof(next));
+}
+
+static inline void zero_tx_type_map(uint8_t *p, int num_blk) {
+ assert(num_blk > 0 && (num_blk & (num_blk - 1)) == 0);
+ if (num_blk <= 2) {
+ p[0] = 0;
+ p[num_blk - 1] = 0;
+ } else if (num_blk == 4) {
+ memset(p, 0, 4);
+ } else if (num_blk == 8) {
+ memset(p, 0, 8);
+ } else if (num_blk == 16) {
+ memset(p, 0, 16);
+ } else {
+ assert((num_blk & 15) == 0);
+ memset(p, 0, (size_t)num_blk);
+ }
+}
+
+static inline void zero_pmc_fast(PICK_MODE_CONTEXT *ctx) {
+ const size_t tail_offset = offsetof(PICK_MODE_CONTEXT, skippable);
+ memset(ctx, 0, offsetof(PICK_MODE_CONTEXT, color_index_map));
+ memset((uint8_t *)ctx + tail_offset, 0, sizeof(*ctx) - tail_offset);
+}
void av1_copy_tree_context(PICK_MODE_CONTEXT *dst_ctx,
PICK_MODE_CONTEXT *src_ctx) {
+ assert(dst_ctx->num_4x4_blk == src_ctx->num_4x4_blk);
dst_ctx->mic = src_ctx->mic;
dst_ctx->mbmi_ext_best = src_ctx->mbmi_ext_best;
@@ -39,6 +135,9 @@ void av1_setup_shared_coeff_buffer(const SequenceHeader *const seq_params,
const int max_sb_square_y = 1 << num_pels_log2_lookup[seq_params->sb_size];
const int max_sb_square_uv = max_sb_square_y >> (seq_params->subsampling_x +
seq_params->subsampling_y);
+ assert(shared_bufs->pmc_pool == NULL);
+ AOM_CHECK_MEM_ERROR(error, shared_bufs->pmc_pool,
+ aom_calloc(1, sizeof(*shared_bufs->pmc_pool)));
for (int i = 0; i < num_planes; i++) {
const int max_num_pix =
(i == AOM_PLANE_Y) ? max_sb_square_y : max_sb_square_uv;
@@ -52,6 +151,21 @@ void av1_setup_shared_coeff_buffer(const SequenceHeader *const seq_params,
}
void av1_free_shared_coeff_buffer(PC_TREE_SHARED_BUFFERS *shared_bufs) {
+ if (shared_bufs->pmc_pool != NULL) {
+ struct PmcPool *const pool = shared_bufs->pmc_pool;
+ for (int pal = 0; pal < PMC_POOL_NUM_PAL; ++pal) {
+ for (int idx = 0; idx < PMC_POOL_NUM_SIZES; ++idx) {
+ PICK_MODE_CONTEXT *cur = pool->pmc_free[pal][idx];
+ while (cur != NULL) {
+ PICK_MODE_CONTEXT *next = pmc_get_next_free(cur);
+ aom_free(cur);
+ cur = next;
+ }
+ }
+ }
+ aom_free(pool);
+ shared_bufs->pmc_pool = NULL;
+ }
for (int i = 0; i < 3; i++) {
aom_free(shared_bufs->coeff_buf[i]);
aom_free(shared_bufs->qcoeff_buf[i]);
@@ -62,23 +176,40 @@ void av1_free_shared_coeff_buffer(PC_TREE_SHARED_BUFFERS *shared_bufs) {
}
}
-PICK_MODE_CONTEXT *av1_alloc_pmc(const struct AV1_COMP *const cpi,
- BLOCK_SIZE bsize,
- PC_TREE_SHARED_BUFFERS *shared_bufs) {
- const AV1_COMMON *const cm = &cpi->common;
- const int num_planes = av1_num_planes(cm);
- const int num_pix = block_size_wide[bsize] * block_size_high[bsize];
- const int num_blk = num_pix / 16;
+static inline void free_pmc_inline(PICK_MODE_CONTEXT *ctx,
+ PC_TREE_SHARED_BUFFERS *shared_bufs) {
+ assert(shared_bufs != NULL);
+ // free_thread_data() tears down shared_coeff_buf (setting pmc_pool = NULL)
+ // before av1_free_pc_tree_root(), and foreign buffers must not enter pool.
+ struct PmcPool *const pool = shared_bufs->pmc_pool;
+ if (pool == NULL ||
+ ctx->coeff[AOM_PLANE_Y] != shared_bufs->coeff_buf[AOM_PLANE_Y]) {
+ aom_free(ctx);
+ return;
+ }
+ const bool use_palette = (ctx->color_index_map[0] != NULL);
+ const int idx = pmc_size_idx(ctx->num_4x4_blk);
+ if (pool->count[use_palette][idx] >= MAX_PMC_POOL_BUCKET_DEPTH) {
+ aom_free(ctx);
+ return;
+ }
+ pmc_set_next_free(ctx, pool->pmc_free[use_palette][idx]);
+ pool->pmc_free[use_palette][idx] = ctx;
+ ++pool->count[use_palette][idx];
+}
- // Allocate PICK_MODE_CONTEXT and all sub-arrays (eobs, txb_entropy_ctx,
- // color_index_map, tx_type_map) in a single 32-byte aligned memory block to
- // avoid multiple aom_memalign/aom_free calls per partition context.
+// Keep cold PMC allocation out of the hot free-list pop path in av1_alloc_pmc.
+static AOM_NOINLINE PICK_MODE_CONTEXT *alloc_pmc_slow(
+ int num_pix, int num_blk, bool use_palette,
+ PC_TREE_SHARED_BUFFERS *shared_bufs) {
+ // Infer plane count from shared_bufs capacity (1 for monochrome, 3
+ // otherwise).
+ const int num_planes =
+ (shared_bufs->coeff_buf[AOM_PLANE_U] != NULL) ? MAX_MB_PLANE : 1;
const size_t ctx_sz = (sizeof(PICK_MODE_CONTEXT) + 31) & ~(size_t)31;
const size_t eob_sz = (num_blk * sizeof(uint16_t) + 31) & ~(size_t)31;
const size_t txb_ctx_sz =
(num_blk * sizeof(ENTROPY_CONTEXT) + 31) & ~(size_t)31;
- const bool use_palette = (num_pix <= MAX_PALETTE_SQUARE) &&
- cm->features.allow_screen_content_tools;
const size_t pal_sz =
use_palette ? ((num_pix * sizeof(uint8_t) + 31) & ~(size_t)31) : 0;
const size_t tx_type_sz = num_blk * sizeof(uint8_t);
@@ -90,7 +221,6 @@ PICK_MODE_CONTEXT *av1_alloc_pmc(const struct AV1_COMP *const cpi,
PICK_MODE_CONTEXT *ctx = (PICK_MODE_CONTEXT *)raw;
memset(ctx, 0, sizeof(*ctx));
- ctx->rd_mode_is_ready = 0;
ctx->num_4x4_blk = num_blk;
uint8_t *ptr = raw + ctx_sz;
@@ -109,21 +239,43 @@ PICK_MODE_CONTEXT *av1_alloc_pmc(const struct AV1_COMP *const cpi,
ptr += pal_sz;
ctx->color_index_map[1] = ptr;
ptr += pal_sz;
- } else {
- ctx->color_index_map[0] = NULL;
- ctx->color_index_map[1] = NULL;
}
ctx->tx_type_map = ptr;
- memset(ctx->tx_type_map, 0, tx_type_sz);
-
+ zero_tx_type_map(ctx->tx_type_map, num_blk);
av1_invalid_rd_stats(&ctx->rd_stats);
-
return ctx;
}
+PICK_MODE_CONTEXT *av1_alloc_pmc(const struct AV1_COMP *const cpi,
+ BLOCK_SIZE bsize,
+ PC_TREE_SHARED_BUFFERS *shared_bufs) {
+ assert(shared_bufs != NULL && bsize < BLOCK_SIZES_ALL);
+ const AV1_COMMON *const cm = &cpi->common;
+ const int num_pix = block_size_wide[bsize] * block_size_high[bsize];
+ const int num_blk = num_pix / 16;
+ const bool use_palette = (num_pix <= MAX_PALETTE_SQUARE) &&
+ cm->features.allow_screen_content_tools;
+ struct PmcPool *const pool = shared_bufs->pmc_pool;
+ if (pool != NULL) {
+ const int idx = pmc_size_idx(num_blk);
+ PICK_MODE_CONTEXT *ctx = pool->pmc_free[use_palette][idx];
+ if (ctx != NULL) {
+ pool->pmc_free[use_palette][idx] = pmc_get_next_free(ctx);
+ assert(pool->count[use_palette][idx] > 0);
+ --pool->count[use_palette][idx];
+ assert(ctx->num_4x4_blk == num_blk);
+ zero_pmc_fast(ctx);
+ av1_invalid_rd_stats(&ctx->rd_stats);
+ zero_tx_type_map(ctx->tx_type_map, num_blk);
+ return ctx;
+ }
+ }
+ return alloc_pmc_slow(num_pix, num_blk, use_palette, shared_bufs);
+}
+
void av1_reset_pmc(PICK_MODE_CONTEXT *ctx) {
- av1_zero_array(ctx->tx_type_map, ctx->num_4x4_blk);
+ zero_tx_type_map(ctx->tx_type_map, ctx->num_4x4_blk);
av1_invalid_rd_stats(&ctx->rd_stats);
}
@@ -143,46 +295,92 @@ PC_TREE *av1_alloc_pc_tree_node(BLOCK_SIZE bsize) {
return pc_tree;
}
-#define FREE_PMC_NODE(CTX) \
- do { \
- av1_free_pmc(CTX, num_planes); \
- CTX = NULL; \
+#define FREE_PMC_NODE(CTX) \
+ do { \
+ if (CTX != NULL) { \
+ free_pmc_inline(CTX, shared_bufs); \
+ CTX = NULL; \
+ } \
} while (0)
void av1_free_pc_tree_recursive(PC_TREE *pc_tree, int num_planes, int keep_best,
int keep_none,
- PARTITION_SEARCH_TYPE partition_search_type) {
+ PARTITION_SEARCH_TYPE partition_search_type,
+ PC_TREE_SHARED_BUFFERS *shared_bufs) {
+ (void)num_planes;
if (pc_tree == NULL) return;
- // Avoid freeing of extended partitions as they are not supported when
- // partition_search_type is VAR_BASED_PARTITION.
- if (partition_search_type == VAR_BASED_PARTITION && !keep_best &&
- !keep_none) {
- FREE_PMC_NODE(pc_tree->none);
-
- for (int i = 0; i < 2; ++i) {
- FREE_PMC_NODE(pc_tree->horizontal[i]);
- FREE_PMC_NODE(pc_tree->vertical[i]);
- }
-
-#if !defined(NDEBUG) && !CONFIG_REALTIME_ONLY
- for (int i = 0; i < 3; ++i) {
- assert(pc_tree->horizontala[i] == NULL);
- assert(pc_tree->horizontalb[i] == NULL);
- assert(pc_tree->verticala[i] == NULL);
- assert(pc_tree->verticalb[i] == NULL);
- }
- for (int i = 0; i < 4; ++i) {
- assert(pc_tree->horizontal4[i] == NULL);
- assert(pc_tree->vertical4[i] == NULL);
- }
+ const BLOCK_SIZE bsize = pc_tree->block_size;
+ if (!keep_best && !keep_none) {
+ if (pc_tree->none != NULL) free_pmc_inline(pc_tree->none, shared_bufs);
+ if (bsize != BLOCK_4X4) {
+ for (int i = 0; i < 2; ++i) {
+ if (pc_tree->horizontal[i] != NULL)
+ free_pmc_inline(pc_tree->horizontal[i], shared_bufs);
+ if (pc_tree->vertical[i] != NULL)
+ free_pmc_inline(pc_tree->vertical[i], shared_bufs);
+ }
+#if !CONFIG_REALTIME_ONLY
+ // AB partitions require bsize > BLOCK_8X8 and are skipped under
+ // VAR_BASED_PARTITION; 4-way partitions also require bsize > BLOCK_8X8.
+ if (bsize > BLOCK_8X8 && partition_search_type != VAR_BASED_PARTITION) {
+ for (int i = 0; i < 3; ++i) {
+ if (pc_tree->horizontala[i] != NULL)
+ free_pmc_inline(pc_tree->horizontala[i], shared_bufs);
+ if (pc_tree->horizontalb[i] != NULL)
+ free_pmc_inline(pc_tree->horizontalb[i], shared_bufs);
+ if (pc_tree->verticala[i] != NULL)
+ free_pmc_inline(pc_tree->verticala[i], shared_bufs);
+ if (pc_tree->verticalb[i] != NULL)
+ free_pmc_inline(pc_tree->verticalb[i], shared_bufs);
+ }
+ for (int i = 0; i < 4; ++i) {
+ if (pc_tree->horizontal4[i] != NULL)
+ free_pmc_inline(pc_tree->horizontal4[i], shared_bufs);
+ if (pc_tree->vertical4[i] != NULL)
+ free_pmc_inline(pc_tree->vertical4[i], shared_bufs);
+ }
+ }
+#if !defined(NDEBUG)
+ if (bsize <= BLOCK_8X8 || partition_search_type == VAR_BASED_PARTITION) {
+ for (int i = 0; i < 3; ++i) {
+ assert(pc_tree->horizontala[i] == NULL);
+ assert(pc_tree->horizontalb[i] == NULL);
+ assert(pc_tree->verticala[i] == NULL);
+ assert(pc_tree->verticalb[i] == NULL);
+ }
+ for (int i = 0; i < 4; ++i) {
+ assert(pc_tree->horizontal4[i] == NULL);
+ assert(pc_tree->vertical4[i] == NULL);
+ }
+ }
+#endif // !defined(NDEBUG)
+#endif // !CONFIG_REALTIME_ONLY
+#if !defined(NDEBUG)
+ } else {
+ for (int i = 0; i < 2; ++i) {
+ assert(pc_tree->horizontal[i] == NULL);
+ assert(pc_tree->vertical[i] == NULL);
+ }
+#if !CONFIG_REALTIME_ONLY
+ for (int i = 0; i < 3; ++i) {
+ assert(pc_tree->horizontala[i] == NULL);
+ assert(pc_tree->horizontalb[i] == NULL);
+ assert(pc_tree->verticala[i] == NULL);
+ assert(pc_tree->verticalb[i] == NULL);
+ }
+ for (int i = 0; i < 4; ++i) {
+ assert(pc_tree->horizontal4[i] == NULL);
+ assert(pc_tree->vertical4[i] == NULL);
+ }
#endif
-
+#endif // !defined(NDEBUG)
+ }
+ // Traverse split[0..3] defensively regardless of bsize.
for (int i = 0; i < 4; ++i) {
if (pc_tree->split[i] != NULL) {
av1_free_pc_tree_recursive(pc_tree->split[i], num_planes, 0, 0,
- partition_search_type);
- pc_tree->split[i] = NULL;
+ partition_search_type, shared_bufs);
}
}
aom_free(pc_tree);
@@ -190,46 +388,50 @@ void av1_free_pc_tree_recursive(PC_TREE *pc_tree, int num_planes, int keep_best,
}
const PARTITION_TYPE partition = pc_tree->partitioning;
-
if (!keep_none && (!keep_best || (partition != PARTITION_NONE)))
FREE_PMC_NODE(pc_tree->none);
- for (int i = 0; i < 2; ++i) {
- if (!keep_best || (partition != PARTITION_HORZ))
- FREE_PMC_NODE(pc_tree->horizontal[i]);
- if (!keep_best || (partition != PARTITION_VERT))
- FREE_PMC_NODE(pc_tree->vertical[i]);
- }
+ if (bsize != BLOCK_4X4) {
+ for (int i = 0; i < 2; ++i) {
+ if (!keep_best || (partition != PARTITION_HORZ))
+ FREE_PMC_NODE(pc_tree->horizontal[i]);
+ if (!keep_best || (partition != PARTITION_VERT))
+ FREE_PMC_NODE(pc_tree->vertical[i]);
+ }
#if !CONFIG_REALTIME_ONLY
- for (int i = 0; i < 3; ++i) {
- if (!keep_best || (partition != PARTITION_HORZ_A))
- FREE_PMC_NODE(pc_tree->horizontala[i]);
- if (!keep_best || (partition != PARTITION_HORZ_B))
- FREE_PMC_NODE(pc_tree->horizontalb[i]);
- if (!keep_best || (partition != PARTITION_VERT_A))
- FREE_PMC_NODE(pc_tree->verticala[i]);
- if (!keep_best || (partition != PARTITION_VERT_B))
- FREE_PMC_NODE(pc_tree->verticalb[i]);
- }
- for (int i = 0; i < 4; ++i) {
- if (!keep_best || (partition != PARTITION_HORZ_4))
- FREE_PMC_NODE(pc_tree->horizontal4[i]);
- if (!keep_best || (partition != PARTITION_VERT_4))
- FREE_PMC_NODE(pc_tree->vertical4[i]);
- }
+ if (bsize > BLOCK_8X8) {
+ if (!keep_best || (partition != PARTITION_HORZ_A)) {
+ for (int i = 0; i < 3; ++i) FREE_PMC_NODE(pc_tree->horizontala[i]);
+ }
+ if (!keep_best || (partition != PARTITION_HORZ_B)) {
+ for (int i = 0; i < 3; ++i) FREE_PMC_NODE(pc_tree->horizontalb[i]);
+ }
+ if (!keep_best || (partition != PARTITION_VERT_A)) {
+ for (int i = 0; i < 3; ++i) FREE_PMC_NODE(pc_tree->verticala[i]);
+ }
+ if (!keep_best || (partition != PARTITION_VERT_B)) {
+ for (int i = 0; i < 3; ++i) FREE_PMC_NODE(pc_tree->verticalb[i]);
+ }
+ if (!keep_best || (partition != PARTITION_HORZ_4)) {
+ for (int i = 0; i < 4; ++i) FREE_PMC_NODE(pc_tree->horizontal4[i]);
+ }
+ if (!keep_best || (partition != PARTITION_VERT_4)) {
+ for (int i = 0; i < 4; ++i) FREE_PMC_NODE(pc_tree->vertical4[i]);
+ }
+ }
#endif
+ }
if (!keep_best || (partition != PARTITION_SPLIT)) {
for (int i = 0; i < 4; ++i) {
if (pc_tree->split[i] != NULL) {
av1_free_pc_tree_recursive(pc_tree->split[i], num_planes, 0, 0,
- partition_search_type);
+ partition_search_type, shared_bufs);
pc_tree->split[i] = NULL;
}
}
}
-
- if (!keep_best && !keep_none) aom_free(pc_tree);
}
+#undef FREE_PMC_NODE
// Normal end-of-frame callers pass partition_search_type to keep
// VAR_BASED_PARTITION debug asserts; error/teardown paths pass
@@ -239,7 +441,7 @@ void av1_free_pc_tree_root(struct ThreadData *td, int num_planes,
assert(td != NULL);
if (td->pc_root != NULL) {
av1_free_pc_tree_recursive(td->pc_root, num_planes, 0, 0,
- partition_search_type);
+ partition_search_type, &td->shared_coeff_buf);
td->pc_root = NULL;
}
}
diff --git a/av1/encoder/context_tree.h b/av1/encoder/context_tree.h
index e6285880ff..5b772fcb7b 100644
--- a/av1/encoder/context_tree.h
+++ b/av1/encoder/context_tree.h
@@ -25,18 +25,25 @@ extern "C" {
struct AV1_PRIMARY;
struct AV1_COMP;
struct AV1Common;
+struct PmcPool;
struct ThreadData;
typedef struct {
tran_low_t *coeff_buf[MAX_MB_PLANE];
tran_low_t *qcoeff_buf[MAX_MB_PLANE];
tran_low_t *dqcoeff_buf[MAX_MB_PLANE];
+ struct PmcPool *pmc_pool;
} PC_TREE_SHARED_BUFFERS;
// Structure to hold snapshot of coding context during the mode picking process
typedef struct PICK_MODE_CONTEXT {
MB_MODE_INFO mic;
MB_MODE_INFO_EXT_FRAME mbmi_ext_best;
+ // zero_pmc_fast() clears [0, color_index_map) and [skippable, sizeof(*ctx)),
+ // preserving the allocation pointers and |num_4x4_blk| in
+ // [color_index_map, skippable). While pooled, the first sizeof(void *) bytes
+ // of |mic| store the intrusive free-list next pointer. New per-block state
+ // fields must be placed outside [color_index_map, skippable).
uint8_t *color_index_map[2];
tran_low_t *coeff[MAX_MB_PLANE];
@@ -108,7 +115,8 @@ void av1_free_shared_coeff_buffer(PC_TREE_SHARED_BUFFERS *shared_bufs);
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);
+ PARTITION_SEARCH_TYPE partition_search_type,
+ PC_TREE_SHARED_BUFFERS *shared_bufs);
// 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);
diff --git a/av1/encoder/encodeframe.c b/av1/encoder/encodeframe.c
index 646114b7fa..e0822de5b9 100644
--- a/av1/encoder/encodeframe.c
+++ b/av1/encoder/encodeframe.c
@@ -922,7 +922,8 @@ static inline void encode_rd_sb(AV1_COMP *cpi, ThreadData *td,
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);
+ sf->part_sf.partition_search_type,
+ &td->shared_coeff_buf);
td->pc_root = NULL;
} else {
// The most exhaustive recursive partition search
diff --git a/av1/encoder/partition_search.c b/av1/encoder/partition_search.c
index 6979b75aff..6393c7e3b3 100644
--- a/av1/encoder/partition_search.c
+++ b/av1/encoder/partition_search.c
@@ -5220,7 +5220,8 @@ static bool ml_partition_search_whole_tree(AV1_COMP *const cpi, ThreadData *td,
av1_ext_part_send_partition_stats(ext_part_controller, &stats);
if (!partition_decision.is_final_decision) {
av1_free_pc_tree_recursive(td->pc_root, av1_num_planes(cm), 0, 0,
- cpi->sf.part_sf.partition_search_type);
+ cpi->sf.part_sf.partition_search_type,
+ &td->shared_coeff_buf);
td->pc_root = NULL;
}
} while (!partition_decision.is_final_decision);
@@ -5230,7 +5231,8 @@ static bool ml_partition_search_whole_tree(AV1_COMP *const cpi, ThreadData *td,
encode_sb(cpi, td, tile_data, tp, mi_row, mi_col, OUTPUT_ENABLED, bsize,
td->pc_root, NULL);
av1_free_pc_tree_recursive(td->pc_root, av1_num_planes(cm), 0, 0,
- cpi->sf.part_sf.partition_search_type);
+ cpi->sf.part_sf.partition_search_type,
+ &td->shared_coeff_buf);
td->pc_root = NULL;
return true;
@@ -5507,8 +5509,8 @@ static bool recursive_partition(AV1_COMP *const cpi, ThreadData *td,
for (int i = 0; i < 4; ++i) {
if (pc_tree->split[i] != NULL) {
av1_free_pc_tree_recursive(pc_tree->split[i], av1_num_planes(cm), 0,
- 0,
- cpi->sf.part_sf.partition_search_type);
+ 0, cpi->sf.part_sf.partition_search_type,
+ &td->shared_coeff_buf);
pc_tree->split[i] = NULL;
}
}
@@ -5555,7 +5557,8 @@ static bool ml_partition_search_partial(AV1_COMP *const cpi, ThreadData *td,
encode_sb(cpi, td, tile_data, tp, mi_row, mi_col, OUTPUT_ENABLED, bsize,
td->pc_root, NULL);
av1_free_pc_tree_recursive(td->pc_root, av1_num_planes(cm), 0, 0,
- cpi->sf.part_sf.partition_search_type);
+ cpi->sf.part_sf.partition_search_type,
+ &td->shared_coeff_buf);
td->pc_root = NULL;
return true;
@@ -5603,7 +5606,8 @@ bool av1_rd_partition_search(AV1_COMP *const cpi, ThreadData *td,
num_configs = read_partition_tree(cpi, td->pc_root, xd->error_info, i);
if (num_configs <= 0) {
av1_free_pc_tree_recursive(td->pc_root, av1_num_planes(cm), 0, 0,
- cpi->sf.part_sf.partition_search_type);
+ cpi->sf.part_sf.partition_search_type,
+ &td->shared_coeff_buf);
td->pc_root = NULL;
aom_internal_error(xd->error_info, AOM_CODEC_ERROR, "Invalid configs.");
}
@@ -5623,7 +5627,8 @@ bool av1_rd_partition_search(AV1_COMP *const cpi, ThreadData *td,
*best_rd_cost = x->rdcost[i];
}
av1_free_pc_tree_recursive(td->pc_root, av1_num_planes(cm), 0, 0,
- cpi->sf.part_sf.partition_search_type);
+ cpi->sf.part_sf.partition_search_type,
+ &td->shared_coeff_buf);
td->pc_root = NULL;
++i;
} while (i < num_configs);
@@ -5642,7 +5647,8 @@ bool av1_rd_partition_search(AV1_COMP *const cpi, ThreadData *td,
encode_sb(cpi, td, tile_data, tp, mi_row, mi_col, OUTPUT_ENABLED, bsize,
td->pc_root, NULL);
av1_free_pc_tree_recursive(td->pc_root, av1_num_planes(cm), 0, 0,
- cpi->sf.part_sf.partition_search_type);
+ cpi->sf.part_sf.partition_search_type,
+ &td->shared_coeff_buf);
td->pc_root = NULL;
++cpi->sb_counter;
@@ -6109,7 +6115,8 @@ BEGIN_PARTITION_SEARCH:
assert(pc_tree == td->pc_root);
// Dealloc the whole PC_TREE after a superblock is done.
av1_free_pc_tree_recursive(pc_tree, num_planes, 0, 0,
- cpi->sf.part_sf.partition_search_type);
+ cpi->sf.part_sf.partition_search_type,
+ &td->shared_coeff_buf);
pc_tree = NULL;
td->pc_root = NULL;
pc_tree_dealloc = 1;
@@ -6129,7 +6136,8 @@ BEGIN_PARTITION_SEARCH:
// nodes for the best partition and PARTITION_NONE.
if (pc_tree_dealloc == 0)
av1_free_pc_tree_recursive(pc_tree, num_planes, 1, 1,
- cpi->sf.part_sf.partition_search_type);
+ cpi->sf.part_sf.partition_search_type,
+ &td->shared_coeff_buf);
if (bsize == cm->seq_params->sb_size) {
assert(best_rdc.rate < INT_MAX);
diff --git a/test/encode_internal_test.cc b/test/encode_internal_test.cc
index 03e2de77b9..d1ceabb944 100644
--- a/test/encode_internal_test.cc
+++ b/test/encode_internal_test.cc
@@ -9,6 +9,7 @@
* PATENTS file, you can obtain it at www.aomedia.org/license/patent.
*/
+#include <climits>
#include <csetjmp>
#include <cstddef>
#include <cstdint>
@@ -33,6 +34,8 @@
#include "av1/common/enums.h"
#include "av1/common/reconintra.h"
#include "av1/encoder/allintra_vis.h"
+#include "av1/encoder/context_tree.h"
+#include "av1/encoder/encoder.h"
#include "av1/encoder/intra_mode_search.h"
#include "test/acm_random.h"
@@ -1433,4 +1436,90 @@ TEST(EncodeInternal, CountColors) {
}
}
+TEST(EncodeInternal, PmcPoolReuseAndZeroFast) {
+ std::unique_ptr<AV1_COMP> cpi_test(new AV1_COMP());
+ SequenceHeader seq_params = {};
+ seq_params.sb_size = BLOCK_64X64;
+ seq_params.subsampling_x = 1;
+ seq_params.subsampling_y = 1;
+ cpi_test->common.seq_params = &seq_params;
+ struct aom_internal_error_info error = {};
+ if (setjmp(error.jmp)) FAIL();
+ error.setjmp = 1;
+
+ PC_TREE_SHARED_BUFFERS shared_bufs = {};
+ struct SharedBufsDeleter {
+ void operator()(PC_TREE_SHARED_BUFFERS *p) const {
+ av1_free_shared_coeff_buffer(p);
+ }
+ };
+ struct PmcDeleter {
+ void operator()(PICK_MODE_CONTEXT *p) const { av1_free_pmc(p, 3); }
+ };
+ av1_setup_shared_coeff_buffer(&seq_params, &shared_bufs, &error);
+ std::unique_ptr<PC_TREE_SHARED_BUFFERS, SharedBufsDeleter> shared_guard(
+ &shared_bufs);
+ ASSERT_NE(shared_bufs.pmc_pool, nullptr);
+
+ std::unique_ptr<PICK_MODE_CONTEXT, PmcDeleter> ctx_guard(
+ av1_alloc_pmc(cpi_test.get(), BLOCK_8X8, &shared_bufs));
+ PICK_MODE_CONTEXT *ctx = ctx_guard.get();
+ ASSERT_NE(ctx, nullptr);
+ EXPECT_EQ(ctx->num_4x4_blk, 4);
+ EXPECT_EQ(ctx->color_index_map[0], nullptr);
+ tran_low_t *const expected_coeff = ctx->coeff[0];
+ uint16_t *const expected_eobs = ctx->eobs[0];
+
+ // Poison zero_pmc_fast() head and tail regions plus tx_type_map.
+ memset(&ctx->mic, 0x5a, sizeof(ctx->mic));
+ memset(&ctx->mbmi_ext_best, 0x5a, sizeof(ctx->mbmi_ext_best));
+ ctx->skippable = 1;
+ ctx->rd_mode_is_ready = 1;
+ ctx->rd_stats.rate = 1234;
+ memset(ctx->tx_type_map, 0x7f, 4);
+
+ PC_TREE *node = av1_alloc_pc_tree_node(BLOCK_8X8);
+ ASSERT_NE(node, nullptr);
+ node->none = ctx_guard.release();
+ av1_free_pc_tree_recursive(node, 3, 0, 0, SEARCH_PARTITION, &shared_bufs);
+
+ // Palette-enabled allocation must not pop the non-palette bucket.
+ cpi_test->common.features.allow_screen_content_tools = 1;
+ std::unique_ptr<PICK_MODE_CONTEXT, PmcDeleter> pal_ctx(
+ av1_alloc_pmc(cpi_test.get(), BLOCK_8X8, &shared_bufs));
+ ASSERT_NE(pal_ctx.get(), nullptr);
+ EXPECT_NE(pal_ctx.get(), ctx);
+ EXPECT_NE(pal_ctx->color_index_map[0], nullptr);
+
+ // Non-palette allocation must pop the exact pooled node, fully zeroed.
+ cpi_test->common.features.allow_screen_content_tools = 0;
+ std::unique_ptr<PICK_MODE_CONTEXT, PmcDeleter> reused(
+ av1_alloc_pmc(cpi_test.get(), BLOCK_8X8, &shared_bufs));
+ ASSERT_EQ(reused.get(), ctx);
+ EXPECT_EQ(reused->coeff[0], expected_coeff);
+ EXPECT_EQ(reused->eobs[0], expected_eobs);
+ EXPECT_EQ(reused->color_index_map[0], nullptr);
+ EXPECT_EQ(reused->num_4x4_blk, 4);
+ EXPECT_EQ(reused->mic.mode, 0);
+ EXPECT_EQ(reused->skippable, 0);
+ EXPECT_EQ(reused->rd_mode_is_ready, 0);
+ EXPECT_EQ(reused->rd_stats.rate, INT_MAX);
+ for (int i = 0; i < 4; ++i) {
+ EXPECT_EQ(reused->tx_type_map[i], 0);
+ }
+
+ // Exceed bucket depth (16) and verify cleanup via av1_free_pc_tree_recursive.
+ std::unique_ptr<PICK_MODE_CONTEXT, PmcDeleter> extra[18];
+ for (int i = 0; i < 18; ++i) {
+ extra[i].reset(av1_alloc_pmc(cpi_test.get(), BLOCK_4X4, &shared_bufs));
+ ASSERT_NE(extra[i].get(), nullptr);
+ }
+ for (int i = 0; i < 18; ++i) {
+ PC_TREE *leaf = av1_alloc_pc_tree_node(BLOCK_4X4);
+ ASSERT_NE(leaf, nullptr);
+ leaf->none = extra[i].release();
+ av1_free_pc_tree_recursive(leaf, 3, 0, 0, SEARCH_PARTITION, &shared_bufs);
+ }
+}
+
} // namespace