Commit 5d011cb2 authored by hui su's avatar hui su

VP10: Add palette mode part 1

Add palette mode for keyframe luma channel. Palette mode is enabled
when using "--tune-content=screen" in encoding config parameters.

on screen_content testset:  +6.89%
on derlr                 :  +0.00%

Design doc (WIP):
https://goo.gl/lD4yJw

Change-Id: Ib368b216bfd3ea21c6c27436934ad87afdaa6f88
parent ba7ea445
......@@ -63,6 +63,20 @@ typedef struct {
#define MAX_REF_FRAMES 4
typedef int8_t MV_REFERENCE_FRAME;
typedef struct {
// Number of base colors for Y (0) and UV (1)
uint8_t palette_size[2];
// Value of base colors for Y, U, and V
#if CONFIG_VP9_HIGHBITDEPTH
uint16_t palette_colors[3 * PALETTE_MAX_SIZE];
#else
uint8_t palette_colors[3 * PALETTE_MAX_SIZE];
#endif // CONFIG_VP9_HIGHBITDEPTH
// Only used by encoder to store the color index of the top left pixel.
// TODO(huisu): move this to encoder
uint8_t palette_first_color_idx[2];
} PALETTE_MODE_INFO;
// This structure now relates to 8x8 block regions.
typedef struct {
// Common for both INTER and INTRA blocks
......@@ -78,6 +92,7 @@ typedef struct {
// Only for INTRA blocks
PREDICTION_MODE uv_mode;
PALETTE_MODE_INFO palette_mode_info;
// Only for INTER blocks
INTERP_FILTER interp_filter;
......@@ -131,6 +146,7 @@ struct macroblockd_plane {
ENTROPY_CONTEXT *above_context;
ENTROPY_CONTEXT *left_context;
int16_t seg_dequant[MAX_SEGMENTS][2];
uint8_t *color_index_map;
// number of 4x4s in current block
uint16_t n4_w, n4_h;
......
This diff is collapsed.
......@@ -26,6 +26,12 @@ extern "C" {
#define INTER_OFFSET(mode) ((mode) - NEARESTMV)
#define PALETTE_COLOR_CONTEXTS 16
#define PALETTE_MAX_SIZE 8
#define PALETTE_BLOCK_SIZES (BLOCK_64X64 - BLOCK_8X8 + 1)
#define PALETTE_Y_MODE_CONTEXTS 3
struct VP10Common;
struct tx_probs {
......@@ -83,11 +89,26 @@ extern const vpx_prob vp10_kf_y_mode_prob[INTRA_MODES][INTRA_MODES]
[INTRA_MODES - 1];
extern const vpx_prob vp10_kf_partition_probs[PARTITION_CONTEXTS]
[PARTITION_TYPES - 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_y_size_prob[PALETTE_BLOCK_SIZES][PALETTE_SIZES - 1];
extern const vpx_prob
vp10_default_palette_uv_size_prob[PALETTE_BLOCK_SIZES][PALETTE_SIZES - 1];
extern const vpx_prob vp10_default_palette_y_color_prob
[PALETTE_MAX_SIZE - 1][PALETTE_COLOR_CONTEXTS][PALETTE_COLORS - 1];
extern const vpx_prob vp10_default_palette_uv_color_prob
[PALETTE_MAX_SIZE - 1][PALETTE_COLOR_CONTEXTS][PALETTE_COLORS - 1];
extern const vpx_tree_index vp10_intra_mode_tree[TREE_SIZE(INTRA_MODES)];
extern const vpx_tree_index vp10_inter_mode_tree[TREE_SIZE(INTER_MODES)];
extern const vpx_tree_index vp10_partition_tree[TREE_SIZE(PARTITION_TYPES)];
extern const vpx_tree_index vp10_switchable_interp_tree
[TREE_SIZE(SWITCHABLE_FILTERS)];
extern const vpx_tree_index vp10_palette_size_tree[TREE_SIZE(PALETTE_SIZES)];
extern const vpx_tree_index
vp10_palette_color_tree[PALETTE_MAX_SIZE - 1][TREE_SIZE(PALETTE_COLORS)];
void vp10_setup_past_independence(struct VP10Common *cm);
......@@ -100,6 +121,18 @@ void vp10_tx_counts_to_branch_counts_16x16(const unsigned int *tx_count_16x16p,
void vp10_tx_counts_to_branch_counts_8x8(const unsigned int *tx_count_8x8p,
unsigned int (*ct_8x8p)[2]);
static INLINE int vp10_ceil_log2(int n) {
int i = 1, p = 2;
while (p < n) {
i++;
p = p << 1;
}
return i;
}
int vp10_get_palette_color_context(const uint8_t *color_map, int cols,
int r, int c, int n, int *color_order);
#ifdef __cplusplus
} // extern "C"
#endif
......
......@@ -109,6 +109,29 @@ typedef enum {
PLANE_TYPES
} PLANE_TYPE;
typedef enum {
TWO_COLORS,
THREE_COLORS,
FOUR_COLORS,
FIVE_COLORS,
SIX_COLORS,
SEVEN_COLORS,
EIGHT_COLORS,
PALETTE_SIZES
} PALETTE_SIZE;
typedef enum {
PALETTE_COLOR_ONE,
PALETTE_COLOR_TWO,
PALETTE_COLOR_THREE,
PALETTE_COLOR_FOUR,
PALETTE_COLOR_FIVE,
PALETTE_COLOR_SIX,
PALETTE_COLOR_SEVEN,
PALETTE_COLOR_EIGHT,
PALETTE_COLORS
} PALETTE_COLOR;
#define DC_PRED 0 // Average of above and left pixels
#define V_PRED 1 // Vertical
#define H_PRED 2 // Horizontal
......
......@@ -185,6 +185,8 @@ typedef struct VP10Common {
int allow_high_precision_mv;
int allow_screen_content_tools;
// Flag signaling which frame contexts should be reset to default values.
RESET_FRAME_CONTEXT_MODE reset_frame_context;
......
......@@ -397,6 +397,40 @@ void vp10_predict_intra_block(const MACROBLOCKD *xd, int bwl_in,
const int x = aoff * 4;
const int y = loff * 4;
if (xd->mi[0]->mbmi.palette_mode_info.palette_size[plane != 0] > 0) {
const int bs = 4 * (1 << tx_size);
const int stride = 4 * (1 << bwl_in);
int r, c;
uint8_t *map = NULL;
#if CONFIG_VP9_HIGHBITDEPTH
uint16_t *palette = xd->mi[0]->mbmi.palette_mode_info.palette_colors +
plane * PALETTE_MAX_SIZE;
#else
uint8_t *palette = xd->mi[0]->mbmi.palette_mode_info.palette_colors +
plane * PALETTE_MAX_SIZE;
#endif // CONFIG_VP9_HIGHBITDEPTH
map = xd->plane[plane != 0].color_index_map;
#if CONFIG_VP9_HIGHBITDEPTH
if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
uint16_t *dst16 = CONVERT_TO_SHORTPTR(dst);
for (r = 0; r < bs; ++r)
for (c = 0; c < bs; ++c)
dst16[r * dst_stride + c] =
palette[map[(r + y) * stride + c + x]];
} else {
#endif // CONFIG_VP9_HIGHBITDEPTH
for (r = 0; r < bs; ++r)
for (c = 0; c < bs; ++c)
dst[r * dst_stride + c] = palette[map[(r + y) * stride + c + x]];
#if CONFIG_VP9_HIGHBITDEPTH
}
#endif // CONFIG_VP9_HIGHBITDEPTH
return;
}
#if CONFIG_VP9_HIGHBITDEPTH
if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
build_intra_predictors_high(xd, ref, ref_stride, dst, dst_stride, mode,
......
......@@ -847,6 +847,9 @@ static void decode_block(VP10Decoder *const pbi, MACROBLOCKD *const xd,
const int max_blocks_high = num_4x4_h + (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, r, mbmi, plane,
......@@ -1535,6 +1538,8 @@ static const uint8_t *decode_tiles(VP10Decoder *pbi,
&tile_data->bit_reader, pbi->decrypt_cb,
pbi->decrypt_state);
vp10_init_macroblockd(cm, &tile_data->xd, tile_data->dqcoeff);
tile_data->xd.plane[0].color_index_map = tile_data->color_index_map[0];
tile_data->xd.plane[1].color_index_map = tile_data->color_index_map[1];
}
}
......@@ -1757,6 +1762,8 @@ static const uint8_t *decode_tiles_mt(VP10Decoder *pbi,
&tile_data->bit_reader, pbi->decrypt_cb,
pbi->decrypt_state);
vp10_init_macroblockd(cm, &tile_data->xd, tile_data->dqcoeff);
tile_data->xd.plane[0].color_index_map = tile_data->color_index_map[0];
tile_data->xd.plane[1].color_index_map = tile_data->color_index_map[1];
worker->had_error = 0;
if (i == num_workers - 1 || n == tile_cols - 1) {
......@@ -1926,6 +1933,8 @@ static size_t read_uncompressed_header(VP10Decoder *pbi,
memset(&cm->ref_frame_map, -1, sizeof(cm->ref_frame_map));
pbi->need_resync = 0;
}
if (cm->current_video_frame == 0)
cm->allow_screen_content_tools = vpx_rb_read_bit(rb);
} else {
cm->intra_only = cm->show_frame ? 0 : vpx_rb_read_bit(rb);
......
......@@ -24,6 +24,19 @@
#include "vpx_dsp/vpx_dsp_common.h"
static INLINE int read_uniform(vpx_reader *r, int n) {
int l = get_unsigned_bits(n);
int m = (1 << l) - n;
int v = vpx_read_literal(r, l-1);
assert(l != 0);
if (v < m)
return v;
else
return (v << 1) - m + vpx_read_literal(r, 1);
}
static PREDICTION_MODE read_intra_mode(vpx_reader *r, const vpx_prob *p) {
return (PREDICTION_MODE)vpx_read_tree(r, vp10_intra_mode_tree, p);
}
......@@ -196,6 +209,38 @@ static int read_skip(VP10_COMMON *cm, const MACROBLOCKD *xd,
}
}
static void read_palette_mode_info(VP10_COMMON *const cm,
MACROBLOCKD *const xd,
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 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] =
vpx_read_tree(r, vp10_palette_size_tree,
vp10_default_palette_y_size_prob[bsize - BLOCK_8X8]) + 2;
n = pmi->palette_size[0];
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);
}
}
static void read_intra_frame_mode_info(VP10_COMMON *const cm,
MACROBLOCKD *const xd,
int mi_row, int mi_col, vpx_reader *r) {
......@@ -244,6 +289,12 @@ static void read_intra_frame_mode_info(VP10_COMMON *const cm,
}
mbmi->uv_mode = read_intra_mode(r, vp10_kf_uv_mode_prob[mbmi->mode]);
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)
read_palette_mode_info(cm, xd, r);
}
static int read_mv_component(vpx_reader *r,
......@@ -407,6 +458,9 @@ static void read_intra_block_mode_info(VP10_COMMON *const cm,
}
mbmi->uv_mode = read_intra_mode_uv(cm, xd, r, mbmi->mode);
mbmi->palette_mode_info.palette_size[0] = 0;
mbmi->palette_mode_info.palette_size[1] = 0;
}
static INLINE int is_mv_valid(const MV *mv) {
......
......@@ -34,6 +34,7 @@ typedef struct TileData {
DECLARE_ALIGNED(16, MACROBLOCKD, xd);
/* dqcoeff are shared by all the planes. So planes must be decoded serially */
DECLARE_ALIGNED(16, tran_low_t, dqcoeff[32 * 32]);
DECLARE_ALIGNED(16, uint8_t, color_index_map[2][64 * 64]);
} TileData;
typedef struct TileWorkerData {
......@@ -43,6 +44,7 @@ typedef struct TileWorkerData {
DECLARE_ALIGNED(16, MACROBLOCKD, xd);
/* dqcoeff are shared by all the planes. So planes must be decoded serially */
DECLARE_ALIGNED(16, tran_low_t, dqcoeff[32 * 32]);
DECLARE_ALIGNED(16, uint8_t, color_index_map[2][64 * 64]);
struct vpx_internal_error_info error_info;
} TileWorkerData;
......
......@@ -257,6 +257,33 @@ void dec_set_contexts(const MACROBLOCKD *xd, struct macroblockd_plane *pd,
}
}
void vp10_decode_palette_tokens(MACROBLOCKD *const xd, int plane,
vpx_reader *r) {
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];
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] =
plane ? vp10_default_palette_uv_color_prob :
vp10_default_palette_y_color_prob;
for (i = 0; i < rows; ++i) {
for (j = (i == 0 ? 1 : 0); j < cols; ++j) {
color_ctx = vp10_get_palette_color_context(color_map, cols, i, j, n,
color_order);
color_idx = vpx_read_tree(r, vp10_palette_color_tree[n - 2],
prob[n - 2][color_ctx]);
assert(color_idx >= 0 && color_idx < n);
color_map[i * cols + j] = color_order[color_idx];
}
}
}
int vp10_decode_block_tokens(MACROBLOCKD *xd,
int plane, const scan_order *sc,
int x, int y,
......
......@@ -20,6 +20,8 @@
extern "C" {
#endif
void vp10_decode_palette_tokens(MACROBLOCKD *const xd, int plane,
vpx_reader *r);
int vp10_decode_block_tokens(MACROBLOCKD *xd,
int plane, const scan_order *sc,
int x, int y,
......
......@@ -44,6 +44,33 @@ static const struct vp10_token partition_encodings[PARTITION_TYPES] =
{{0, 1}, {2, 2}, {6, 3}, {7, 3}};
static const struct vp10_token inter_mode_encodings[INTER_MODES] =
{{2, 2}, {6, 3}, {0, 1}, {7, 3}};
static const struct vp10_token palette_size_encodings[] = {
{0, 1}, {2, 2}, {6, 3}, {14, 4}, {30, 5}, {62, 6}, {63, 6},
};
static const struct vp10_token
palette_color_encodings[PALETTE_MAX_SIZE - 1][8] = {
{{0, 1}, {1, 1}}, // 2 colors
{{0, 1}, {2, 2}, {3, 2}}, // 3 colors
{{0, 1}, {2, 2}, {6, 3}, {7, 3}}, // 4 colors
{{0, 1}, {2, 2}, {6, 3}, {14, 4}, {15, 4}}, // 5 colors
{{0, 1}, {2, 2}, {6, 3}, {14, 4}, {30, 5}, {31, 5}}, // 6 colors
{{0, 1}, {2, 2}, {6, 3}, {14, 4}, {30, 5}, {62, 6}, {63, 6}}, // 7 colors
{{0, 1}, {2, 2}, {6, 3}, {14, 4},
{30, 5}, {62, 6}, {126, 7}, {127, 7}}, // 8 colors
};
static INLINE void write_uniform(vpx_writer *w, int n, int v) {
int l = get_unsigned_bits(n);
int m = (1 << l) - n;
if (l == 0)
return;
if (v < m) {
vpx_write_literal(w, v, l - 1);
} else {
vpx_write_literal(w, m + ((v - m) >> 1), l - 1);
vpx_write_literal(w, (v - m) & 1, 1);
}
}
static void write_intra_mode(vpx_writer *w, PREDICTION_MODE mode,
const vpx_prob *probs) {
......@@ -120,6 +147,22 @@ static void update_switchable_interp_probs(VP10_COMMON *cm, vpx_writer *w,
counts->switchable_interp[j], SWITCHABLE_FILTERS, w);
}
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 i;
TOKENEXTRA *p = *tp;
for (i = 0; i < rows * cols -1; ++i) {
vp10_write_token(w, vp10_palette_color_tree[n - 2], p->context_tree,
&palette_color_encodings[n - 2][p->token]);
++p;
}
*tp = p;
}
static void pack_mb_tokens(vpx_writer *w,
TOKENEXTRA **tp, const TOKENEXTRA *const stop,
vpx_bit_depth_t bit_depth, const TX_SIZE tx) {
......@@ -353,6 +396,36 @@ 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;
......@@ -387,6 +460,10 @@ static void write_mb_modes_kf(const VP10_COMMON *cm, const MACROBLOCKD *xd,
}
write_intra_mode(w, mbmi->uv_mode, vp10_kf_uv_mode_prob[mbmi->mode]);
if (bsize >= BLOCK_8X8 && cm->allow_screen_content_tools &&
mbmi->mode == DC_PRED)
write_palette_mode_info(cm, xd, mi, w);
}
static void write_modes_b(VP10_COMP *cpi, const TileInfo *const tile,
......@@ -413,6 +490,13 @@ static void write_modes_b(VP10_COMP *cpi, const TileInfo *const tile,
pack_inter_mode_mvs(cpi, m, 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);
}
if (!m->mbmi.skip) {
assert(*tok < tok_end);
for (plane = 0; plane < MAX_MB_PLANE; ++plane) {
......@@ -1133,6 +1217,8 @@ static void write_uncompressed_header(VP10_COMP *cpi,
write_sync_code(wb);
write_bitdepth_colorspace_sampling(cm, wb);
write_frame_size(cm, wb);
if (cm->current_video_frame == 0)
vpx_wb_write_bit(wb, cm->allow_screen_content_tools);
} else {
if (!cm->show_frame)
vpx_wb_write_bit(wb, cm->intra_only);
......
......@@ -52,6 +52,13 @@ typedef struct {
uint8_t mode_context[MAX_REF_FRAMES];
} MB_MODE_INFO_EXT;
typedef struct {
uint8_t best_palette_color_map[4096];
double kmeans_data_buf[4096];
uint8_t kmeans_indices_buf[4096];
uint8_t kmeans_pre_indices_buf[4096];
} PALETTE_BUFFER;
typedef struct macroblock MACROBLOCK;
struct macroblock {
struct macroblock_plane plane[MAX_MB_PLANE];
......@@ -92,6 +99,8 @@ struct macroblock {
int *nmvsadcost_hp[2];
int **mvsadcost;
PALETTE_BUFFER *palette_buffer;
// These define limits to motion vector components to prevent them
// from extending outside the UMV borders
int mv_col_min;
......
......@@ -61,6 +61,11 @@ static void free_mode_context(PICK_MODE_CONTEXT *ctx) {
ctx->eobs[i][k] = 0;
}
}
for (i = 0; i < 2; ++i) {
vpx_free(ctx->color_index_map[i]);
ctx->color_index_map[i] = 0;
}
}
static void alloc_tree_contexts(VP10_COMMON *cm, PC_TREE *tree,
......
......@@ -27,6 +27,7 @@ typedef struct {
MODE_INFO mic;
MB_MODE_INFO_EXT mbmi_ext;
uint8_t *zcoeff_blk;
uint8_t *color_index_map[2];
tran_low_t *coeff[MAX_MB_PLANE][3];
tran_low_t *qcoeff[MAX_MB_PLANE][3];
tran_low_t *dqcoeff[MAX_MB_PLANE][3];
......
......@@ -1005,6 +1005,9 @@ static void update_state(VP10_COMP *cpi, ThreadData *td,
p[i].eobs = ctx->eobs_pbuf[i][2];
}
for (i = 0; i < 2; ++i)
pd[i].color_index_map = ctx->color_index_map[i];
// Restore the coding context of the MB to that that was in place
// when the mode was picked for it
for (y = 0; y < mi_height; y++)
......@@ -1136,6 +1139,19 @@ static void rd_pick_sb_modes(VP10_COMP *cpi,
pd[i].dqcoeff = ctx->dqcoeff_pbuf[i][0];
p[i].eobs = ctx->eobs_pbuf[i][0];
}
if (cm->current_video_frame == 0 && cm->allow_screen_content_tools) {
for (i = 0; i < 2; ++i) {
if (ctx->color_index_map[i] == 0) {
CHECK_MEM_ERROR(cm, ctx->color_index_map[i],
vpx_memalign(16, (ctx->num_4x4_blk << 4) *
sizeof(*ctx->color_index_map[i])));
}
}
}
for (i = 0; i < 2; ++i)
pd[i].color_index_map = ctx->color_index_map[i];
ctx->is_coded = 0;
ctx->skippable = 0;
ctx->pred_pixel_ready = 0;
......@@ -2938,6 +2954,16 @@ static void encode_superblock(VP10_COMP *cpi, ThreadData *td,
vp10_encode_intra_block_plane(x, VPXMAX(bsize, BLOCK_8X8), plane);
if (output_enabled)
sum_intra_stats(td->counts, mi);
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);
}
}
vp10_tokenize_sb(cpi, td, t, !output_enabled, VPXMAX(bsize, BLOCK_8X8));
} else {
int ref;
......
......@@ -390,6 +390,9 @@ static void dealloc_compressor_data(VP10_COMP *cpi) {
vp10_free_pc_tree(&cpi->td);
if (cpi->common.allow_screen_content_tools)
vpx_free(cpi->td.mb.palette_buffer);
if (cpi->source_diff_var != NULL) {
vpx_free(cpi->source_diff_var);
cpi->source_diff_var = NULL;
......@@ -1428,6 +1431,15 @@ void vp10_change_config(struct VP10_COMP *cpi, const VP10EncoderConfig *oxcf) {
: REFRESH_FRAME_CONTEXT_BACKWARD;
cm->reset_frame_context = RESET_FRAME_CONTEXT_NONE;
cm->allow_screen_content_tools = (cpi->oxcf.content == VP9E_CONTENT_SCREEN);
if (cm->allow_screen_content_tools) {
MACROBLOCK *x = &cpi->td.mb;
if (x->palette_buffer == 0) {
CHECK_MEM_ERROR(cm, x->palette_buffer,
vpx_memalign(16, sizeof(*x->palette_buffer)));
}
}
vp10_reset_segment_features(&cm->seg);
vp10_set_high_precision_mv(cpi, 0);
......@@ -1936,6 +1948,8 @@ void vp10_remove_compressor(VP10_COMP *cpi) {
vpx_free(thread_data->td->counts);
vp10_free_pc_tree(thread_data->td);
vpx_free(thread_data->td);
if (cpi->common.allow_screen_content_tools)
vpx_free(thread_data->td->mb.palette_buffer);
}
}
vpx_free(cpi->tile_thr_data);
......
......@@ -458,6 +458,12 @@ typedef struct VP10_COMP {
int y_mode_costs[INTRA_MODES][INTRA_MODES][INTRA_MODES];
int switchable_interp_costs[SWITCHABLE_FILTER_CONTEXTS][SWITCHABLE_FILTERS];
int partition_cost[PARTITION_CONTEXTS][PARTITION_TYPES];
int palette_y_size_cost[10][PALETTE_SIZES];
int palette_uv_size_cost[10][PALETTE_SIZES];
int palette_y_color_cost[PALETTE_MAX_SIZE - 1][PALETTE_COLOR_CONTEXTS]
[PALETTE_COLORS];
int palette_uv_color_cost[PALETTE_MAX_SIZE - 1][PALETTE_COLOR_CONTEXTS]
[PALETTE_COLORS];
int multi_arf_allowed;
int multi_arf_enabled;
......
......@@ -96,6 +96,13 @@ void vp10_encode_tiles_mt(VP10_COMP *cpi) {
CHECK_MEM_ERROR(cm, thread_data->td->counts,
vpx_calloc(1, sizeof(*thread_data->td->counts)));
// Allocate buffers used by palette coding mode.
if (cpi->common.allow_screen_content_tools) {
MACROBLOCK *x = &thread_data->td->mb;
CHECK_MEM_ERROR(cm, x->palette_buffer,
vpx_memalign(16, sizeof(*x->palette_buffer)));
}
// Create threads
if (!winterface->reset(worker))
vpx_internal_error(&cm->error, VPX_CODEC_ERROR,
......
/*
* Copyright (c) 2015 The WebM project authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
#include "vp10/encoder/palette.h"
static double calc_dist(const double *p1, const double *p2, int dim) {
double dist = 0;
int i = 0;
for (i = 0; i < dim; i++) {
dist = dist + (p1[i] - p2[i]) * (p1[i] - p2[i]);
}
return dist;
}
void vp10_calc_indices(const double *data, const double *centroids,
uint8_t *indices, int n, int k, int dim) {
int i, j;
double min_dist, this_dist;
for (i = 0; i < n; i++) {
min_dist = calc_dist(data + i * dim, centroids, dim);
indices[i] = 0;
for (j = 1; j < k; j++) {
this_dist = calc_dist(data + i * dim, centroids + j * dim, dim);
if (this_dist < min_dist) {
min_dist = this_dist;
indices[i] = j;
}
}
}
}
static void calc_centroids(const double *data, double *centroids,
const uint8_t *indices, int n, int k, int dim) {
int i, j, index;
int count[PALETTE_MAX_SIZE];
srand((unsigned int) data[0]);
memset(count, 0, sizeof(count[0]) * k);
memset(centroids, 0, sizeof(centroids[0]) * k * dim);
for (i = 0; i < n; i++) {
index = indices[i];
assert(index < k);
count[index]++;
for (j = 0; j < dim; j++) {
centroids[index * dim + j] += data[i * dim + j];
}
}
for (i = 0; i < k; i++) {
if (count[i] == 0) {
// TODO(huisu): replace rand() with something else.
memcpy(centroids + i * dim, data + (rand() % n) * dim,
sizeof(centroids[0]) * dim);
} else {
const double norm = 1.0 / count[i];
for (j = 0; j < dim; j++)
centroids[i * dim + j] *= norm;
}
}
}
static double calc_total_dist(const double *data, const double *centroids,