Commit 967ff395 authored by Urvang Joshi's avatar Urvang Joshi

Palette: Use inverse_color_order to find color index faster.

Cherry-picked from aomedia/master: b1c3bb57

Change-Id: Icfc16070160fd9763abb1dbf5545103e62b4b9ff
parent b42827f6
......@@ -892,13 +892,14 @@ static const aom_prob default_rect_tx_prob[TX_SIZES - 1] = { 192, 192, 192 };
#if CONFIG_PALETTE
int av1_get_palette_color_context(const uint8_t *color_map, int cols, int r,
int c, int n, int *color_order) {
int c, int n, uint8_t *color_order,
int *color_idx) {
int i, j, max, max_idx, temp;
int scores[PALETTE_MAX_SIZE + 10];
int weights[4] = { 3, 2, 3, 2 };
int color_ctx = 0;
int color_neighbors[4];
int inverse_color_order[PALETTE_MAX_SIZE];
assert(n <= PALETTE_MAX_SIZE);
if (c - 1 >= 0)
......@@ -918,7 +919,10 @@ int av1_get_palette_color_context(const uint8_t *color_map, int cols, int r,
else
color_neighbors[3] = -1;
for (i = 0; i < PALETTE_MAX_SIZE; ++i) color_order[i] = i;
for (i = 0; i < PALETTE_MAX_SIZE; ++i) {
color_order[i] = i;
inverse_color_order[i] = i;
}
memset(scores, 0, PALETTE_MAX_SIZE * sizeof(scores[0]));
for (i = 0; i < 4; ++i) {
if (color_neighbors[i] >= 0) scores[color_neighbors[i]] += weights[i];
......@@ -944,6 +948,8 @@ int av1_get_palette_color_context(const uint8_t *color_map, int cols, int r,
temp = color_order[i];
color_order[i] = color_order[max_idx];
color_order[max_idx] = temp;
inverse_color_order[color_order[i]] = i;
inverse_color_order[color_order[max_idx]] = max_idx;
}
}
......@@ -956,7 +962,9 @@ int av1_get_palette_color_context(const uint8_t *color_map, int cols, int r,
}
if (color_ctx >= PALETTE_COLOR_CONTEXTS) color_ctx = 0;
if (color_idx != NULL) {
*color_idx = inverse_color_order[color_map[r * cols + c]];
}
return color_ctx;
}
#endif // CONFIG_PALETTE
......
......@@ -320,7 +320,8 @@ static INLINE int av1_ceil_log2(int n) {
#if CONFIG_PALETTE
int av1_get_palette_color_context(const uint8_t *color_map, int cols, int r,
int c, int n, int *color_order);
int c, int n, uint8_t *color_order,
int *color_idx);
#endif // CONFIG_PALETTE
#ifdef __cplusplus
......
......@@ -338,15 +338,15 @@ static void dec_set_contexts(const MACROBLOCKD *xd,
#if CONFIG_PALETTE
void av1_decode_palette_tokens(MACROBLOCKD *const xd, int plane,
aom_reader *r) {
MODE_INFO *const mi = xd->mi[0];
MB_MODE_INFO *const mbmi = &mi->mbmi;
const MODE_INFO *const mi = xd->mi[0];
const MB_MODE_INFO *const mbmi = &mi->mbmi;
const BLOCK_SIZE bsize = mbmi->sb_type;
const int rows = (4 * num_4x4_blocks_high_lookup[bsize]) >>
(xd->plane[plane != 0].subsampling_y);
const int cols = (4 * num_4x4_blocks_wide_lookup[bsize]) >>
(xd->plane[plane != 0].subsampling_x);
int color_idx, color_ctx, color_order[PALETTE_MAX_SIZE];
int n = mbmi->palette_mode_info.palette_size[plane != 0];
uint8_t color_order[PALETTE_MAX_SIZE];
const int n = mbmi->palette_mode_info.palette_size[plane != 0];
int i, j;
uint8_t *color_map = xd->plane[plane != 0].color_index_map;
const aom_prob(*const prob)[PALETTE_COLOR_CONTEXTS][PALETTE_COLORS - 1] =
......@@ -355,10 +355,10 @@ void av1_decode_palette_tokens(MACROBLOCKD *const xd, int plane,
for (i = 0; i < rows; ++i) {
for (j = (i == 0 ? 1 : 0); j < cols; ++j) {
color_ctx =
av1_get_palette_color_context(color_map, cols, i, j, n, color_order);
color_idx = aom_read_tree(r, av1_palette_color_tree[n - 2],
prob[n - 2][color_ctx], ACCT_STR);
const int color_ctx = av1_get_palette_color_context(color_map, cols, i, j,
n, color_order, NULL);
const int color_idx = aom_read_tree(r, av1_palette_color_tree[n - 2],
prob[n - 2][color_ctx], ACCT_STR);
assert(color_idx >= 0 && color_idx < n);
color_map[i * cols + j] = color_order[color_idx];
}
......
......@@ -1768,8 +1768,7 @@ static int rd_pick_palette_intra_sby(
if (colors > 1 && colors <= 64) {
int r, c, i, j, k;
const int max_itr = 50;
int color_ctx, color_idx = 0;
int color_order[PALETTE_MAX_SIZE];
uint8_t color_order[PALETTE_MAX_SIZE];
float *const data = x->palette_buffer->kmeans_data_buf;
float centroids[PALETTE_MAX_SIZE];
uint8_t *const color_map = xd->plane[0].color_index_map;
......@@ -1853,13 +1852,9 @@ static int rd_pick_palette_intra_sby(
1);
for (i = 0; i < rows; ++i) {
for (j = (i == 0 ? 1 : 0); j < cols; ++j) {
color_ctx = av1_get_palette_color_context(color_map, cols, i, j, k,
color_order);
for (r = 0; r < k; ++r)
if (color_map[i * cols + j] == color_order[r]) {
color_idx = r;
break;
}
int color_idx;
const int color_ctx = av1_get_palette_color_context(
color_map, cols, i, j, k, color_order, &color_idx);
assert(color_idx >= 0 && color_idx < k);
this_rate += cpi->palette_y_color_cost[k - 2][color_ctx][color_idx];
}
......@@ -3647,8 +3642,7 @@ static void rd_pick_palette_intra_sbuv(
if (colors > 1 && colors <= 64) {
int r, c, n, i, j;
const int max_itr = 50;
int color_ctx, color_idx = 0;
int color_order[PALETTE_MAX_SIZE];
uint8_t color_order[PALETTE_MAX_SIZE];
int64_t this_sse;
float lb_u, ub_u, val_u;
float lb_v, ub_v, val_v;
......@@ -3741,13 +3735,9 @@ static void rd_pick_palette_intra_sbuv(
for (i = 0; i < rows; ++i) {
for (j = (i == 0 ? 1 : 0); j < cols; ++j) {
color_ctx = av1_get_palette_color_context(color_map, cols, i, j, n,
color_order);
for (r = 0; r < n; ++r)
if (color_map[i * cols + j] == color_order[r]) {
color_idx = r;
break;
}
int color_idx;
const int color_ctx = av1_get_palette_color_context(
color_map, cols, i, j, n, color_order, &color_idx);
assert(color_idx >= 0 && color_idx < n);
this_rate += cpi->palette_uv_color_cost[n - 2][color_ctx][color_idx];
}
......@@ -9377,7 +9367,7 @@ void av1_rd_pick_inter_mode_sb(const AV1_COMP *cpi, TileDataEnc *tile_data,
int best_rate_nocoef;
#endif
int64_t distortion2 = 0, distortion_y = 0, dummy_rd = best_rd, this_rd;
int skippable = 0;
int skippable = 0, rate_overhead = 0;
TX_SIZE best_tx_size, uv_tx;
TX_TYPE best_tx_type;
PALETTE_MODE_INFO palette_mode_info;
......
......@@ -409,18 +409,19 @@ static INLINE int get_tx_eob(const struct segmentation *seg, int segment_id,
}
#if CONFIG_PALETTE
void av1_tokenize_palette_sb(const AV1_COMP *cpi, struct ThreadData *const td,
int plane, TOKENEXTRA **t, RUN_TYPE dry_run,
BLOCK_SIZE bsize, int *rate) {
MACROBLOCK *const x = &td->mb;
MACROBLOCKD *const xd = &x->e_mbd;
MB_MODE_INFO *mbmi = &xd->mi[0]->mbmi;
uint8_t *color_map = xd->plane[plane != 0].color_index_map;
PALETTE_MODE_INFO *pmi = &mbmi->palette_mode_info;
int n = pmi->palette_size[plane != 0];
int i, j, k;
void av1_tokenize_palette_sb(const AV1_COMP *cpi,
const struct ThreadData *const td, int plane,
TOKENEXTRA **t, RUN_TYPE dry_run, BLOCK_SIZE bsize,
int *rate) {
const MACROBLOCK *const x = &td->mb;
const MACROBLOCKD *const xd = &x->e_mbd;
const MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi;
const uint8_t *const color_map = xd->plane[plane != 0].color_index_map;
const PALETTE_MODE_INFO *const pmi = &mbmi->palette_mode_info;
const int n = pmi->palette_size[plane != 0];
int i, j;
int this_rate = 0;
int color_idx = -1, color_ctx, color_order[PALETTE_MAX_SIZE];
uint8_t color_order[PALETTE_MAX_SIZE];
const int rows = (4 * num_4x4_blocks_high_lookup[bsize]) >>
(xd->plane[plane != 0].subsampling_y);
const int cols = (4 * num_4x4_blocks_wide_lookup[bsize]) >>
......@@ -431,17 +432,13 @@ void av1_tokenize_palette_sb(const AV1_COMP *cpi, struct ThreadData *const td,
for (i = 0; i < rows; ++i) {
for (j = (i == 0 ? 1 : 0); j < cols; ++j) {
color_ctx =
av1_get_palette_color_context(color_map, cols, i, j, n, color_order);
for (k = 0; k < n; ++k)
if (color_map[i * cols + j] == color_order[k]) {
color_idx = k;
break;
}
assert(color_idx >= 0 && color_idx < n);
int color_new_idx;
const int color_ctx = av1_get_palette_color_context(
color_map, cols, i, j, n, color_order, &color_new_idx);
assert(color_new_idx >= 0 && color_new_idx < n);
if (dry_run == DRY_RUN_COSTCOEFFS)
this_rate += cpi->palette_y_color_cost[n - 2][color_ctx][color_idx];
(*t)->token = color_idx;
this_rate += cpi->palette_y_color_cost[n - 2][color_ctx][color_new_idx];
(*t)->token = color_new_idx;
(*t)->context_tree = probs[n - 2][color_ctx];
(*t)->skip_eob_node = 0;
++(*t);
......
......@@ -72,7 +72,7 @@ void av1_tokenize_sb_vartx(const struct AV1_COMP *cpi, struct ThreadData *td,
#endif
#if CONFIG_PALETTE
void av1_tokenize_palette_sb(const struct AV1_COMP *cpi,
struct ThreadData *const td, int plane,
const struct ThreadData *const td, int plane,
TOKENEXTRA **t, RUN_TYPE dry_run, BLOCK_SIZE bsize,
int *rate);
#endif // CONFIG_PALETTE
......
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