Commit 6dde8018 authored by Urvang Joshi's avatar Urvang Joshi

Palette code: Use built-in qsort() method; create remove_dup() method.

Change-Id: Id816413307334336a9f473540cf9aa0e789ea9e9
parent 1da3e129
......@@ -9,6 +9,7 @@
*/
#include <math.h>
#include <stdlib.h>
#include "vp10/encoder/palette.h"
static float calc_dist(const float *p1, const float *p2, int dim) {
......@@ -117,26 +118,27 @@ void vp10_k_means(const float *data, float *centroids, uint8_t *indices, int n,
}
}
void vp10_insertion_sort(float *data, int n) {
int i, j, k;
float val;
if (n <= 1)
return;
for (i = 1; i < n; ++i) {
val = data[i];
j = 0;
while (val > data[j] && j < i)
++j;
if (j == i)
continue;
static int float_comparer(const void *a, const void *b) {
const float fa = *(const float *)a;
const float fb = *(const float *)b;
return (fa > fb) - (fb < fa);
}
for (k = i; k > j; --k)
data[k] = data[k - 1];
data[j] = val;
int vp10_remove_duplicates(float *centroids, int num_centroids) {
int num_unique; // number of unique centroids
int i;
qsort(centroids, num_centroids, sizeof(*centroids), float_comparer);
for (i = 0; i < num_centroids; ++i) {
centroids[i] = roundf(centroids[i]);
}
// Remove duplicates.
num_unique = 1;
for (i = 1; i < num_centroids; ++i) {
if (centroids[i] != centroids[i - 1]) { // found a new unique centroid
centroids[num_unique++] = centroids[i];
}
}
return num_unique;
}
int vp10_count_colors(const uint8_t *src, int stride, int rows, int cols) {
......
......@@ -17,11 +17,15 @@
extern "C" {
#endif
void vp10_insertion_sort(float *data, int n);
void vp10_calc_indices(const float *data, const float *centroids,
uint8_t *indices, int n, int k, int dim);
void vp10_k_means(const float *data, float *centroids, uint8_t *indices, int n,
int k, int dim, int max_itr);
// Given a list of centroids, returns the unique number of centroids 'k', and
// puts these unique centroids in first 'k' indices of 'centroids' array.
int vp10_remove_duplicates(float *centroids, int num_centroids);
int vp10_count_colors(const uint8_t *src, int stride, int rows, int cols);
#if CONFIG_VP9_HIGHBITDEPTH
int vp10_count_colors_highbd(const uint8_t *src8, int stride, int rows,
......
......@@ -1914,18 +1914,7 @@ static int rd_pick_palette_intra_sby(VP10_COMP *cpi, MACROBLOCK *x,
for (i = 0; i < n; ++i)
centroids[i] = lb + (2 * i + 1) * (ub - lb) / n / 2;
vp10_k_means(data, centroids, color_map, rows * cols, n, 1, max_itr);
vp10_insertion_sort(centroids, n);
for (i = 0; i < n; ++i)
centroids[i] = roundf(centroids[i]);
// remove duplicates
i = 1;
for (j = 1; j < n; ++j) {
if (centroids[j] != centroids[j - 1]) { // found a new unique centroid
centroids[i] = centroids[j];
++i;
}
}
k = i; // number of unique centroids
k = vp10_remove_duplicates(centroids, n);
#if CONFIG_VP9_HIGHBITDEPTH
if (cpi->common.use_highbitdepth)
......
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