Commit 78b0bd0a authored by hui su's avatar hui su

Complete (mostly) migration of palette mode

Coding gain on screen_content is 12.2% (was 6.6%).

Some features such as frame-level color buffer, adaptive
entropy coding, are coming in future patches.

Change-Id: I2658cf5ec0cbb02cff685475759f3b68c9807697
parent 09368fcf
......@@ -383,7 +383,7 @@ vp10_default_palette_y_mode_prob[PALETTE_BLOCK_SIZES][PALETTE_Y_MODE_CONTEXTS]
};
const vpx_prob default_uv_palette_mode_prob[2] = {
const vpx_prob vp10_default_palette_uv_mode_prob[2] = {
253, 229
};
......
......@@ -176,6 +176,7 @@ extern const vpx_prob vp10_kf_y_mode_prob[INTRA_MODES][INTRA_MODES]
[INTRA_MODES - 1];
extern const vpx_prob
vp10_default_palette_y_mode_prob[PALETTE_BLOCK_SIZES][PALETTE_Y_MODE_CONTEXTS];
extern const vpx_prob vp10_default_palette_uv_mode_prob[2];
extern const vpx_prob
vp10_default_palette_y_size_prob[PALETTE_BLOCK_SIZES][PALETTE_SIZES - 1];
extern const vpx_prob
......
......@@ -1978,6 +1978,10 @@ static void decode_block(VP10Decoder *const pbi, MACROBLOCKD *const xd,
}
if (!is_inter_block(mbmi)) {
int plane;
for (plane = 0; plane <= 1; ++plane) {
if (mbmi->palette_mode_info.palette_size[plane])
vp10_decode_palette_tokens(xd, plane, r);
}
for (plane = 0; plane < MAX_MB_PLANE; ++plane) {
const struct macroblockd_plane *const pd = &xd->plane[plane];
const TX_SIZE tx_size =
......@@ -1994,9 +1998,6 @@ static void decode_block(VP10Decoder *const pbi, MACROBLOCKD *const xd,
(xd->mb_to_bottom_edge >= 0 ?
0 : xd->mb_to_bottom_edge >> (5 + pd->subsampling_y));
if (plane <= 1 && mbmi->palette_mode_info.palette_size[plane])
vp10_decode_palette_tokens(xd, plane, r);
for (row = 0; row < max_blocks_high; row += step)
for (col = 0; col < max_blocks_wide; col += step)
predict_and_reconstruct_intra_block(xd,
......
......@@ -419,30 +419,48 @@ static void read_palette_mode_info(VP10_COMMON *const cm,
vpx_reader *r) {
MODE_INFO *const mi = xd->mi[0];
MB_MODE_INFO *const mbmi = &mi->mbmi;
const MODE_INFO *above_mi = xd->above_mi;
const MODE_INFO *left_mi = xd->left_mi;
const MODE_INFO *const above_mi = xd->above_mi;
const MODE_INFO *const left_mi = xd->left_mi;
const BLOCK_SIZE bsize = mbmi->sb_type;
int i, palette_ctx = 0;
if (above_mi)
palette_ctx += (above_mi->mbmi.palette_mode_info.palette_size[0] > 0);
if (left_mi)
palette_ctx += (left_mi->mbmi.palette_mode_info.palette_size[0] > 0);
if (vpx_read(r, vp10_default_palette_y_mode_prob[bsize - BLOCK_8X8]
[palette_ctx])) {
int n;
PALETTE_MODE_INFO *pmi = &mbmi->palette_mode_info;
pmi->palette_size[0] =
int i, n, palette_ctx = 0;
PALETTE_MODE_INFO *const pmi = &mbmi->palette_mode_info;
if (mbmi->mode == DC_PRED) {
if (above_mi)
palette_ctx += (above_mi->mbmi.palette_mode_info.palette_size[0] > 0);
if (left_mi)
palette_ctx += (left_mi->mbmi.palette_mode_info.palette_size[0] > 0);
if (vpx_read(r, vp10_default_palette_y_mode_prob[bsize - BLOCK_8X8]
[palette_ctx])) {
pmi->palette_size[0] =
vpx_read_tree(r, vp10_palette_size_tree,
vp10_default_palette_y_size_prob[bsize - BLOCK_8X8]) + 2;
n = pmi->palette_size[0];
n = pmi->palette_size[0];
for (i = 0; i < n; ++i)
pmi->palette_colors[i] = vpx_read_literal(r, cm->bit_depth);
for (i = 0; i < n; ++i)
pmi->palette_colors[i] = vpx_read_literal(r, cm->bit_depth);
xd->plane[0].color_index_map[0] = read_uniform(r, n);
assert(xd->plane[0].color_index_map[0] < n);
}
}
xd->plane[0].color_index_map[0] = read_uniform(r, n);
assert(xd->plane[0].color_index_map[0] < n);
if (mbmi->uv_mode == DC_PRED) {
if (vpx_read(r,
vp10_default_palette_uv_mode_prob[pmi->palette_size[0] > 0])) {
pmi->palette_size[1] =
vpx_read_tree(r, vp10_palette_size_tree,
vp10_default_palette_uv_size_prob[bsize - BLOCK_8X8])
+ 2;
n = pmi->palette_size[1];
for (i = 0; i < n; ++i) {
pmi->palette_colors[PALETTE_MAX_SIZE + i] =
vpx_read_literal(r, cm->bit_depth);
pmi->palette_colors[2 * PALETTE_MAX_SIZE + i] =
vpx_read_literal(r, cm->bit_depth);
}
xd->plane[1].color_index_map[0] = read_uniform(r, n);
assert(xd->plane[1].color_index_map[0] < n);
}
}
}
......@@ -456,7 +474,8 @@ static void read_ext_intra_mode_info(VP10_COMMON *const cm,
#if !ALLOW_FILTER_INTRA_MODES
return;
#endif
if (mbmi->mode == DC_PRED) {
if (mbmi->mode == DC_PRED &&
mbmi->palette_mode_info.palette_size[0] == 0) {
mbmi->ext_intra_mode_info.use_ext_intra_mode[0] =
vpx_read(r, cm->fc->ext_intra_probs[0]);
if (mbmi->ext_intra_mode_info.use_ext_intra_mode[0]) {
......@@ -466,7 +485,8 @@ static void read_ext_intra_mode_info(VP10_COMMON *const cm,
if (counts)
++counts->ext_intra[0][mbmi->ext_intra_mode_info.use_ext_intra_mode[0]];
}
if (mbmi->uv_mode == DC_PRED) {
if (mbmi->uv_mode == DC_PRED &&
mbmi->palette_mode_info.palette_size[1] == 0) {
mbmi->ext_intra_mode_info.use_ext_intra_mode[1] =
vpx_read(r, cm->fc->ext_intra_probs[1]);
if (mbmi->ext_intra_mode_info.use_ext_intra_mode[1]) {
......@@ -555,8 +575,7 @@ static void read_intra_frame_mode_info(VP10_COMMON *const cm,
mbmi->palette_mode_info.palette_size[0] = 0;
mbmi->palette_mode_info.palette_size[1] = 0;
if (bsize >= BLOCK_8X8 && cm->allow_screen_content_tools &&
mbmi->mode == DC_PRED)
if (bsize >= BLOCK_8X8 && cm->allow_screen_content_tools)
read_palette_mode_info(cm, xd, r);
if (!FIXED_TX_TYPE) {
......@@ -868,9 +887,10 @@ static void read_intra_block_mode_info(VP10_COMMON *const cm,
mbmi->angle_delta[1] =
read_uniform(r, 2 * MAX_ANGLE_DELTAS + 1) - MAX_ANGLE_DELTAS;
#endif // CONFIG_EXT_INTRA
mbmi->palette_mode_info.palette_size[0] = 0;
mbmi->palette_mode_info.palette_size[1] = 0;
if (bsize >= BLOCK_8X8 && cm->allow_screen_content_tools)
read_palette_mode_info(cm, xd, r);
#if CONFIG_EXT_INTRA
mbmi->ext_intra_mode_info.use_ext_intra_mode[0] = 0;
mbmi->ext_intra_mode_info.use_ext_intra_mode[1] = 0;
......@@ -1151,6 +1171,9 @@ static void read_inter_block_mode_info(VP10Decoder *const pbi,
int16_t mode_ctx = 0;
MV_REFERENCE_FRAME ref_frame;
mbmi->palette_mode_info.palette_size[0] = 0;
mbmi->palette_mode_info.palette_size[1] = 0;
read_ref_frames(cm, xd, r, mbmi->segment_id, mbmi->ref_frame);
is_compound = has_second_ref(mbmi);
......
......@@ -429,13 +429,15 @@ void vp10_decode_palette_tokens(MACROBLOCKD *const xd, int plane,
MODE_INFO *const mi = xd->mi[0];
MB_MODE_INFO *const mbmi = &mi->mbmi;
const BLOCK_SIZE bsize = mbmi->sb_type;
int rows = 4 * num_4x4_blocks_high_lookup[bsize];
int cols = 4 * num_4x4_blocks_wide_lookup[bsize];
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];
int i, j;
uint8_t *color_map = xd->plane[plane].color_index_map;
const vpx_prob (* prob)[PALETTE_COLOR_CONTEXTS][PALETTE_COLORS - 1] =
uint8_t *color_map = xd->plane[plane != 0].color_index_map;
const vpx_prob (* const prob)[PALETTE_COLOR_CONTEXTS][PALETTE_COLORS - 1] =
plane ? vp10_default_palette_uv_color_prob :
vp10_default_palette_y_color_prob;
......
......@@ -509,13 +509,11 @@ static void update_ext_tx_probs(VP10_COMMON *cm, vpx_writer *w) {
#endif // CONFIG_EXT_TX
static void pack_palette_tokens(vpx_writer *w, TOKENEXTRA **tp,
BLOCK_SIZE bsize, int n) {
int rows = 4 * num_4x4_blocks_high_lookup[bsize];
int cols = 4 * num_4x4_blocks_wide_lookup[bsize];
int n, int num) {
int i;
TOKENEXTRA *p = *tp;
for (i = 0; i < rows * cols -1; ++i) {
for (i = 0; i < num; ++i) {
vp10_write_token(w, vp10_palette_color_tree[n - 2], p->context_tree,
&palette_color_encodings[n - 2][p->token]);
++p;
......@@ -842,7 +840,8 @@ static void write_ext_intra_mode_info(const VP10_COMMON *const cm,
#if !ALLOW_FILTER_INTRA_MODES
return;
#endif
if (mbmi->mode == DC_PRED) {
if (mbmi->mode == DC_PRED &&
mbmi->palette_mode_info.palette_size[0] == 0) {
vpx_write(w, mbmi->ext_intra_mode_info.use_ext_intra_mode[0],
cm->fc->ext_intra_probs[0]);
if (mbmi->ext_intra_mode_info.use_ext_intra_mode[0]) {
......@@ -850,7 +849,8 @@ static void write_ext_intra_mode_info(const VP10_COMMON *const cm,
write_uniform(w, FILTER_INTRA_MODES, mode);
}
}
if (mbmi->uv_mode == DC_PRED) {
if (mbmi->uv_mode == DC_PRED &&
mbmi->palette_mode_info.palette_size[1] == 0) {
vpx_write(w, mbmi->ext_intra_mode_info.use_ext_intra_mode[1],
cm->fc->ext_intra_probs[1]);
if (mbmi->ext_intra_mode_info.use_ext_intra_mode[1]) {
......@@ -881,6 +881,55 @@ static void write_switchable_interp_filter(VP10_COMP *cpi,
}
}
static void write_palette_mode_info(const VP10_COMMON *cm,
const MACROBLOCKD *xd,
const MODE_INFO *const mi,
vpx_writer *w) {
const MB_MODE_INFO *const mbmi = &mi->mbmi;
const MODE_INFO *const above_mi = xd->above_mi;
const MODE_INFO *const left_mi = xd->left_mi;
const BLOCK_SIZE bsize = mbmi->sb_type;
const PALETTE_MODE_INFO *const pmi = &mbmi->palette_mode_info;
int palette_ctx = 0;
int n, i;
if (mbmi->mode == DC_PRED) {
n = pmi->palette_size[0];
if (above_mi)
palette_ctx += (above_mi->mbmi.palette_mode_info.palette_size[0] > 0);
if (left_mi)
palette_ctx += (left_mi->mbmi.palette_mode_info.palette_size[0] > 0);
vpx_write(w, n > 0,
vp10_default_palette_y_mode_prob[bsize - BLOCK_8X8][palette_ctx]);
if (n > 0) {
vp10_write_token(w, vp10_palette_size_tree,
vp10_default_palette_y_size_prob[bsize - BLOCK_8X8],
&palette_size_encodings[n - 2]);
for (i = 0; i < n; ++i)
vpx_write_literal(w, pmi->palette_colors[i], cm->bit_depth);
write_uniform(w, n, pmi->palette_first_color_idx[0]);
}
}
if (mbmi->uv_mode == DC_PRED) {
n = pmi->palette_size[1];
vpx_write(w, n > 0,
vp10_default_palette_uv_mode_prob[pmi->palette_size[0] > 0]);
if (n > 0) {
vp10_write_token(w, vp10_palette_size_tree,
vp10_default_palette_uv_size_prob[bsize - BLOCK_8X8],
&palette_size_encodings[n - 2]);
for (i = 0; i < n; ++i) {
vpx_write_literal(w, pmi->palette_colors[PALETTE_MAX_SIZE + i],
cm->bit_depth);
vpx_write_literal(w, pmi->palette_colors[2 * PALETTE_MAX_SIZE + i],
cm->bit_depth);
}
write_uniform(w, n, pmi->palette_first_color_idx[1]);
}
}
}
static void pack_inter_mode_mvs(VP10_COMP *cpi, const MODE_INFO *mi,
#if CONFIG_SUPERTX
int supertx_enabled,
......@@ -995,7 +1044,10 @@ static void pack_inter_mode_mvs(VP10_COMP *cpi, const MODE_INFO *mi,
bsize >= BLOCK_8X8)
write_uniform(w, 2 * MAX_ANGLE_DELTAS + 1,
MAX_ANGLE_DELTAS + mbmi->angle_delta[1]);
#endif // CONFIG_EXT_INTRA
if (bsize >= BLOCK_8X8 && cm->allow_screen_content_tools)
write_palette_mode_info(cm, xd, mi, w);
#if CONFIG_EXT_INTRA
if (bsize >= BLOCK_8X8)
write_ext_intra_mode_info(cm, mbmi, w);
#endif // CONFIG_EXT_INTRA
......@@ -1263,36 +1315,6 @@ static void pack_inter_mode_mvs(VP10_COMP *cpi, const MODE_INFO *mi,
}
}
static void write_palette_mode_info(const VP10_COMMON *cm,
const MACROBLOCKD *xd,
const MODE_INFO *const mi,
vpx_writer *w) {
const MB_MODE_INFO *const mbmi = &mi->mbmi;
const MODE_INFO *const above_mi = xd->above_mi;
const MODE_INFO *const left_mi = xd->left_mi;
const BLOCK_SIZE bsize = mbmi->sb_type;
const PALETTE_MODE_INFO *pmi = &mbmi->palette_mode_info;
int palette_ctx = 0;
int n, i;
n = pmi->palette_size[0];
if (above_mi)
palette_ctx += (above_mi->mbmi.palette_mode_info.palette_size[0] > 0);
if (left_mi)
palette_ctx += (left_mi->mbmi.palette_mode_info.palette_size[0] > 0);
vpx_write(w, n > 0,
vp10_default_palette_y_mode_prob[bsize - BLOCK_8X8][palette_ctx]);
if (n > 0) {
vp10_write_token(w, vp10_palette_size_tree,
vp10_default_palette_y_size_prob[bsize - BLOCK_8X8],
&palette_size_encodings[n - 2]);
for (i = 0; i < n; ++i)
vpx_write_literal(w, pmi->palette_colors[i],
cm->bit_depth);
write_uniform(w, n, pmi->palette_first_color_idx[0]);
}
}
static void write_mb_modes_kf(const VP10_COMMON *cm, const MACROBLOCKD *xd,
MODE_INFO **mi_8x8, vpx_writer *w) {
const struct segmentation *const seg = &cm->seg;
......@@ -1351,9 +1373,7 @@ static void write_mb_modes_kf(const VP10_COMMON *cm, const MACROBLOCKD *xd,
write_uniform(w, 2 * MAX_ANGLE_DELTAS + 1,
MAX_ANGLE_DELTAS + mbmi->angle_delta[1]);
#endif // CONFIG_EXT_INTRA
if (bsize >= BLOCK_8X8 && cm->allow_screen_content_tools &&
mbmi->mode == DC_PRED)
if (bsize >= BLOCK_8X8 && cm->allow_screen_content_tools)
write_palette_mode_info(cm, xd, mi, w);
if (!FIXED_TX_TYPE) {
......@@ -1428,11 +1448,17 @@ static void write_modes_b(VP10_COMP *cpi, const TileInfo *const tile,
w);
}
if (m->mbmi.palette_mode_info.palette_size[0] > 0) {
assert(*tok < tok_end);
pack_palette_tokens(w, tok, m->mbmi.sb_type,
m->mbmi.palette_mode_info.palette_size[0]);
assert(*tok < tok_end);
for (plane = 0; plane <= 1; ++plane) {
if (m->mbmi.palette_mode_info.palette_size[plane] > 0) {
const int rows = (4 * num_4x4_blocks_high_lookup[m->mbmi.sb_type]) >>
(xd->plane[plane].subsampling_y);
const int cols = (4 * num_4x4_blocks_wide_lookup[m->mbmi.sb_type]) >>
(xd->plane[plane].subsampling_x);
assert(*tok < tok_end);
pack_palette_tokens(w, tok, m->mbmi.palette_mode_info.palette_size[plane],
rows * cols - 1);
assert(*tok < tok_end);
}
}
#if CONFIG_SUPERTX
......
......@@ -4432,9 +4432,11 @@ static void encode_superblock(VP10_COMP *cpi, ThreadData *td,
#if CONFIG_EXT_INTRA
if (output_enabled && bsize >= BLOCK_8X8) {
FRAME_COUNTS *counts = td->counts;
if (mbmi->mode == DC_PRED)
if (mbmi->mode == DC_PRED &&
mbmi->palette_mode_info.palette_size[0] == 0)
++counts->ext_intra[0][mbmi->ext_intra_mode_info.use_ext_intra_mode[0]];
if (mbmi->uv_mode == DC_PRED)
if (mbmi->uv_mode == DC_PRED &&
mbmi->palette_mode_info.palette_size[1] == 0)
++counts->ext_intra[1][mbmi->ext_intra_mode_info.use_ext_intra_mode[1]];
if (mbmi->mode != DC_PRED && mbmi->mode != TM_PRED) {
int p_angle;
......@@ -4448,12 +4450,14 @@ static void encode_superblock(VP10_COMP *cpi, ThreadData *td,
#endif // CONFIG_EXT_INTRA
if (bsize >= BLOCK_8X8 && output_enabled) {
if (mbmi->palette_mode_info.palette_size[0] > 0) {
mbmi->palette_mode_info.palette_first_color_idx[0] =
xd->plane[0].color_index_map[0];
// TODO(huisu): this increases the use of token buffer. Needs stretch
// test to verify.
vp10_tokenize_palette_sb(td, bsize, 0, t);
for (plane = 0; plane <= 1; ++plane) {
if (mbmi->palette_mode_info.palette_size[plane] > 0) {
mbmi->palette_mode_info.palette_first_color_idx[plane] =
xd->plane[plane].color_index_map[0];
// TODO(huisu): this increases the use of token buffer. Needs stretch
// test to verify.
vp10_tokenize_palette_sb(td, bsize, plane, t);
}
}
}
vp10_tokenize_sb(cpi, td, t, !output_enabled, VPXMAX(bsize, BLOCK_8X8));
......
......@@ -93,7 +93,7 @@ int vp10_k_means(const double *data, double *centroids, uint8_t *indices,
uint8_t *pre_indices, int n, int k, int dim, int max_itr) {
int i = 0;
double pre_dist, this_dist;
double pre_centroids[PALETTE_MAX_SIZE];
double pre_centroids[2 * PALETTE_MAX_SIZE];
vp10_calc_indices(data, centroids, indices, n, k, dim);
pre_dist = calc_total_dist(data, centroids, indices, n, k, dim);
......
This diff is collapsed.
......@@ -398,13 +398,18 @@ void vp10_tokenize_palette_sb(struct ThreadData *const td,
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[0].color_index_map;
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;
int color_new_idx = -1, color_ctx, color_order[PALETTE_MAX_SIZE];
int rows = 4 * num_4x4_blocks_high_lookup[bsize];
int cols = 4 * num_4x4_blocks_wide_lookup[bsize];
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);
const vpx_prob (* const probs)[PALETTE_COLOR_CONTEXTS][PALETTE_COLORS - 1] =
plane == 0 ? vp10_default_palette_y_color_prob :
vp10_default_palette_uv_color_prob;
for (i = 0; i < rows; ++i) {
for (j = (i == 0 ? 1 : 0); j < cols; ++j) {
......@@ -416,9 +421,8 @@ void vp10_tokenize_palette_sb(struct ThreadData *const td,
break;
}
assert(color_new_idx >= 0 && color_new_idx < n);
(*t)->token = color_new_idx;
(*t)->context_tree = vp10_default_palette_y_color_prob[n - 2][color_ctx];
(*t)->context_tree = probs[n - 2][color_ctx];
(*t)->skip_eob_node = 0;
++(*t);
}
......
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