Commit e39b3b8b authored by Yaowu Xu's avatar Yaowu Xu

Correct token allocation size for tiles

With --enable-ext-partition, it is possible to have superblock size at
both 64x64 and 128x128. But MAX_SB_SIZE_LOG2 is defined to be 7, i.e.
128x128 size. In get_token_alloc(), the token count for a tile or a
frame is always computed based on "tokens/superblock * number of
superblocks", so it should use the actual superblock size, instead of
128x128.

The segfault in #988 is a result when every tile is defined at one
single superblock of 64x64, the token size computation for each tile
was assuming it is 128x128, resulting 4x overestimate of the token
count for every tile. This caused the pointer for many tiles to go
beyond the allocated token arrary, which is correctly allocated at
frame level.

BUG=aomedia:988

Change-Id: I2da21f181446ad7117ce3269f01a43950c379028
parent edc7346f
......@@ -3459,7 +3459,7 @@ void av1_init_tile_data(AV1_COMP *cpi) {
cpi->tile_tok[tile_row][tile_col] = pre_tok + tile_tok;
pre_tok = cpi->tile_tok[tile_row][tile_col];
tile_tok = allocated_tokens(*tile_info);
tile_tok = allocated_tokens(*tile_info, cm->mib_size_log2 + MI_SIZE_LOG2);
}
}
}
......@@ -3510,7 +3510,8 @@ void av1_encode_tile(AV1_COMP *cpi, ThreadData *td, int tile_row,
cpi->tok_count[tile_row][tile_col] =
(unsigned int)(tok - cpi->tile_tok[tile_row][tile_col]);
assert(cpi->tok_count[tile_row][tile_col] <= allocated_tokens(*tile_info));
assert(cpi->tok_count[tile_row][tile_col] <=
allocated_tokens(*tile_info, cm->mib_size_log2 + MI_SIZE_LOG2));
}
static void encode_tiles(AV1_COMP *cpi) {
......
......@@ -855,7 +855,8 @@ static void alloc_compressor_data(AV1_COMP *cpi) {
aom_free(cpi->tile_tok[0][0]);
{
unsigned int tokens = get_token_alloc(cm->mb_rows, cm->mb_cols);
unsigned int tokens =
get_token_alloc(cm->mb_rows, cm->mb_cols, MAX_SB_SIZE_LOG2);
CHECK_MEM_ERROR(cm, cpi->tile_tok[0][0],
aom_calloc(tokens, sizeof(*cpi->tile_tok[0][0])));
}
......
......@@ -692,33 +692,36 @@ static INLINE int enc_is_ref_frame_buf(AV1_COMP *cpi, RefCntBuffer *frame_buf) {
return (ref_frame <= ALTREF_FRAME);
}
static INLINE unsigned int get_token_alloc(int mb_rows, int mb_cols) {
static INLINE unsigned int get_token_alloc(int mb_rows, int mb_cols,
int sb_size_log2) {
// Calculate the maximum number of max superblocks in the image.
const int shift = MAX_SB_SIZE_LOG2 - 4;
const int shift = sb_size_log2 - 4;
const int sb_size = 1 << sb_size_log2;
const int sb_size_square = sb_size * sb_size;
const int sb_rows = ALIGN_POWER_OF_TWO(mb_rows, shift) >> shift;
const int sb_cols = ALIGN_POWER_OF_TWO(mb_cols, shift) >> shift;
// For transform coefficients, assume 3 planes with no subsampling. We assume
// up to 1 token per pixel, and then allow a head room of 1 EOSB token per
// 4x4 block per plane, plus EOSB_TOKEN per plane.
const int sb_coeff_toks = 3 * (MAX_SB_SQUARE + (MAX_SB_SQUARE / 16) + 1);
const int sb_coeff_toks = 3 * (sb_size_square + (sb_size_square / 16) + 1);
// For palette coefficients, there can be at most one palette for each 8x8
// block. If w, h are the width and height of the block, the palette has at
// most 1 + h * w tokens (65 for an 8x8 block) without (see
// cost_and_tokenize_map). At most, there can be palettes on two planes.
const int sb_palette_toks = 2 * (1 + 64) * (MAX_SB_SQUARE / 64);
const int sb_palette_toks = 2 * (1 + 64) * (sb_size_square / 64);
return sb_rows * sb_cols * (sb_coeff_toks + sb_palette_toks);
}
// Get the allocated token size for a tile. It does the same calculation as in
// the frame token allocation.
static INLINE unsigned int allocated_tokens(TileInfo tile) {
static INLINE unsigned int allocated_tokens(TileInfo tile, int sb_size_log2) {
int tile_mb_rows = (tile.mi_row_end - tile.mi_row_start + 2) >> 2;
int tile_mb_cols = (tile.mi_col_end - tile.mi_col_start + 2) >> 2;
return get_token_alloc(tile_mb_rows, tile_mb_cols);
return get_token_alloc(tile_mb_rows, tile_mb_cols, sb_size_log2);
}
#if CONFIG_TEMPMV_SIGNALING
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment