Handle centroid rounding inside palette.c itself.

Mostly refactoring, but a very tiny functional change:
Do all rounding in calc_centroids() itself, instead of rounding in two
places inside palette.c

This gives a slight performance improvement for screen content:
0.078% on average.

Change-Id: I7a0e007d30ebf4e59839483a167123f31a222dd4
This commit is contained in:
Urvang Joshi
2016-08-10 15:56:47 -07:00
parent 32c92c97ea
commit f746c103a7
3 changed files with 15 additions and 6 deletions

View File

@@ -16,7 +16,7 @@ static float calc_dist(const float *p1, const float *p2, int dim) {
float dist = 0; float dist = 0;
int i; int i;
for (i = 0; i < dim; ++i) { for (i = 0; i < dim; ++i) {
const float diff = p1[i] - roundf(p2[i]); const float diff = p1[i] - p2[i];
dist += diff * diff; dist += diff * diff;
} }
return dist; return dist;
@@ -74,6 +74,11 @@ static void calc_centroids(const float *data, float *centroids,
for (j = 0; j < dim; ++j) centroids[i * dim + j] *= norm; for (j = 0; j < dim; ++j) centroids[i * dim + j] *= norm;
} }
} }
// Round to nearest integers.
for (i = 0; i < k * dim; ++i) {
centroids[i] = roundf(centroids[i]);
}
} }
static float calc_total_dist(const float *data, const float *centroids, static float calc_total_dist(const float *data, const float *centroids,
@@ -127,9 +132,6 @@ int vp10_remove_duplicates(float *centroids, int num_centroids) {
int num_unique; // number of unique centroids int num_unique; // number of unique centroids
int i; int i;
qsort(centroids, num_centroids, sizeof(*centroids), float_comparer); qsort(centroids, num_centroids, sizeof(*centroids), float_comparer);
for (i = 0; i < num_centroids; ++i) {
centroids[i] = roundf(centroids[i]);
}
// Remove duplicates. // Remove duplicates.
num_unique = 1; num_unique = 1;
for (i = 1; i < num_centroids; ++i) { for (i = 1; i < num_centroids; ++i) {

View File

@@ -19,11 +19,18 @@ extern "C" {
void vp10_calc_indices(const float *data, const float *centroids, void vp10_calc_indices(const float *data, const float *centroids,
uint8_t *indices, int n, int k, int dim); uint8_t *indices, int n, int k, int dim);
// Given 'data' of size 'n' and initial guess of 'centroids' of size 'k x dim',
// runs up to 'max_itr' iterations of k-means algorithm to get updated
// 'centroids' and the centroid 'indices' for elements in 'data'.
// Note: the output centroids are rounded off to nearest integers.
void vp10_k_means(const float *data, float *centroids, uint8_t *indices, int n, void vp10_k_means(const float *data, float *centroids, uint8_t *indices, int n,
int k, int dim, int max_itr); int k, int dim, int max_itr);
// Given a list of centroids, returns the unique number of centroids 'k', and // Given a list of centroids, returns the unique number of centroids 'k', and
// puts these unique centroids in first 'k' indices of 'centroids' array. // puts these unique centroids in first 'k' indices of 'centroids' array.
// Ideally, the centroids should be rounded to integers before calling this
// method.
int vp10_remove_duplicates(float *centroids, int num_centroids); int vp10_remove_duplicates(float *centroids, int num_centroids);
int vp10_count_colors(const uint8_t *src, int stride, int rows, int cols); int vp10_count_colors(const uint8_t *src, int stride, int rows, int cols);

View File

@@ -3616,11 +3616,11 @@ static void rd_pick_palette_intra_sbuv(
#if CONFIG_VP9_HIGHBITDEPTH #if CONFIG_VP9_HIGHBITDEPTH
if (cpi->common.use_highbitdepth) if (cpi->common.use_highbitdepth)
pmi->palette_colors[i * PALETTE_MAX_SIZE + j] = clip_pixel_highbd( pmi->palette_colors[i * PALETTE_MAX_SIZE + j] = clip_pixel_highbd(
(int)lroundf(centroids[j * 2 + i - 1]), cpi->common.bit_depth); (int)centroids[j * 2 + i - 1], cpi->common.bit_depth);
else else
#endif // CONFIG_VP9_HIGHBITDEPTH #endif // CONFIG_VP9_HIGHBITDEPTH
pmi->palette_colors[i * PALETTE_MAX_SIZE + j] = pmi->palette_colors[i * PALETTE_MAX_SIZE + j] =
clip_pixel((int)lroundf(centroids[j * 2 + i - 1])); clip_pixel((int)centroids[j * 2 + i - 1]);
} }
} }