Merge "Palette code cleanup:" into nextgenv2
diff --git a/vp10/encoder/block.h b/vp10/encoder/block.h
index 538beaa..7da76cc 100644
--- a/vp10/encoder/block.h
+++ b/vp10/encoder/block.h
@@ -68,8 +68,6 @@
 typedef struct {
   uint8_t best_palette_color_map[MAX_SB_SQUARE];
   float kmeans_data_buf[2 * MAX_SB_SQUARE];
-  uint8_t kmeans_indices_buf[MAX_SB_SQUARE];
-  uint8_t kmeans_pre_indices_buf[MAX_SB_SQUARE];
 } PALETTE_BUFFER;
 
 typedef struct macroblock MACROBLOCK;
diff --git a/vp10/encoder/palette.c b/vp10/encoder/palette.c
index cbc3582..94e0c87 100644
--- a/vp10/encoder/palette.c
+++ b/vp10/encoder/palette.c
@@ -13,10 +13,9 @@
 
 static float calc_dist(const float *p1, const float *p2, int dim) {
   float dist = 0;
-  int i = 0;
-
+  int i;
   for (i = 0; i < dim; ++i) {
-    float diff = p1[i] - roundf(p2[i]);
+    const float diff = p1[i] - roundf(p2[i]);
     dist += diff * diff;
   }
   return dist;
@@ -25,13 +24,12 @@
 void vp10_calc_indices(const float *data, const float *centroids,
                        uint8_t *indices, int n, int k, int dim) {
   int i, j;
-  float min_dist, this_dist;
-
   for (i = 0; i < n; ++i) {
-    min_dist = calc_dist(data + i * dim, centroids, dim);
+    float 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);
+      const float this_dist =
+          calc_dist(data + i * dim, centroids + j * dim, dim);
       if (this_dist < min_dist) {
         min_dist = this_dist;
         indices[i] = j;
@@ -90,17 +88,21 @@
   return dist;
 }
 
-int vp10_k_means(const float *data, float *centroids, uint8_t *indices,
-                 uint8_t *pre_indices, int n, int k, int dim, int max_itr) {
-  int i = 0;
-  float pre_dist, this_dist;
+void vp10_k_means(const float *data, float *centroids, uint8_t *indices, int n,
+                  int k, int dim, int max_itr) {
+  int i;
+  float this_dist;
   float pre_centroids[2 * PALETTE_MAX_SIZE];
+  uint8_t pre_indices[MAX_SB_SQUARE];
 
   vp10_calc_indices(data, centroids, indices, n, k, dim);
-  pre_dist = calc_total_dist(data, centroids, indices, n, k, dim);
-  memcpy(pre_centroids, centroids, sizeof(pre_centroids[0]) * k * dim);
-  memcpy(pre_indices, indices, sizeof(pre_indices[0]) * n);
-  while (i < max_itr) {
+  this_dist = calc_total_dist(data, centroids, indices, n, k, dim);
+
+  for (i = 0; i < max_itr; ++i) {
+    const float pre_dist = this_dist;
+    memcpy(pre_centroids, centroids, sizeof(pre_centroids[0]) * k * dim);
+    memcpy(pre_indices, indices, sizeof(pre_indices[0]) * n);
+
     calc_centroids(data, centroids, indices, n, k, dim);
     vp10_calc_indices(data, centroids, indices, n, k, dim);
     this_dist = calc_total_dist(data, centroids, indices, n, k, dim);
@@ -112,14 +114,7 @@
     }
     if (!memcmp(centroids, pre_centroids, sizeof(pre_centroids[0]) * k * dim))
       break;
-
-    memcpy(pre_centroids, centroids, sizeof(pre_centroids[0]) * k * dim);
-    memcpy(pre_indices, indices, sizeof(pre_indices[0]) * n);
-    pre_dist = this_dist;
-    ++i;
   }
-
-  return i;
 }
 
 void vp10_insertion_sort(float *data, int n) {
diff --git a/vp10/encoder/palette.h b/vp10/encoder/palette.h
index 40d9ef9..3ec0f73 100644
--- a/vp10/encoder/palette.h
+++ b/vp10/encoder/palette.h
@@ -20,8 +20,8 @@
 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);
-int vp10_k_means(const float *data, float *centroids, uint8_t *indices,
-                 uint8_t *pre_indices, int n, int k, int dim, int max_itr);
+void vp10_k_means(const float *data, float *centroids, uint8_t *indices, int n,
+                  int k, int dim, int max_itr);
 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,
diff --git a/vp10/encoder/rdopt.c b/vp10/encoder/rdopt.c
index d580bb0..ea2e821 100644
--- a/vp10/encoder/rdopt.c
+++ b/vp10/encoder/rdopt.c
@@ -1860,8 +1860,6 @@
     int color_ctx, color_idx = 0;
     int color_order[PALETTE_MAX_SIZE];
     float *const data = x->palette_buffer->kmeans_data_buf;
-    uint8_t *const indices = x->palette_buffer->kmeans_indices_buf;
-    uint8_t *const pre_indices = x->palette_buffer->kmeans_pre_indices_buf;
     float centroids[PALETTE_MAX_SIZE];
     uint8_t *const color_map = xd->plane[0].color_index_map;
     float lb, ub, val;
@@ -1915,8 +1913,7 @@
         n >= 2; --n) {
       for (i = 0; i < n; ++i)
         centroids[i] = lb + (2 * i + 1) * (ub - lb) / n / 2;
-      vp10_k_means(data, centroids, indices, pre_indices, rows * cols,
-                   n, 1, max_itr);
+      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]);
@@ -1933,16 +1930,15 @@
 #if CONFIG_VP9_HIGHBITDEPTH
       if (cpi->common.use_highbitdepth)
         for (i = 0; i < k; ++i)
-          pmi->palette_colors[i] = clip_pixel_highbd((int)lroundf(centroids[i]),
-                                                     cpi->common.bit_depth);
+          pmi->palette_colors[i] =
+              clip_pixel_highbd((int)centroids[i], cpi->common.bit_depth);
       else
 #endif  // CONFIG_VP9_HIGHBITDEPTH
         for (i = 0; i < k; ++i)
-          pmi->palette_colors[i] = clip_pixel((int)lroundf(centroids[i]));
+          pmi->palette_colors[i] = clip_pixel((int)centroids[i]);
       pmi->palette_size[0] = k;
 
-      vp10_calc_indices(data, centroids, indices, rows * cols, k, 1);
-      memcpy(color_map, indices, rows * cols * sizeof(*color_map));
+      vp10_calc_indices(data, centroids, color_map, rows * cols, k, 1);
 
       super_block_yrd(cpi, x, &this_rate_tokenonly, &this_distortion,
                       &s, NULL, bsize, *best_rd);
@@ -3743,8 +3739,6 @@
     float lb_u, ub_u, val_u;
     float lb_v, ub_v, val_v;
     float *const data = x->palette_buffer->kmeans_data_buf;
-    uint8_t *const indices = x->palette_buffer->kmeans_indices_buf;
-    uint8_t *const pre_indices = x->palette_buffer->kmeans_pre_indices_buf;
     float centroids[2 * PALETTE_MAX_SIZE];
     uint8_t *const color_map = xd->plane[1].color_index_map;
     PALETTE_MODE_INFO *const pmi = &mbmi->palette_mode_info;
@@ -3806,8 +3800,7 @@
         centroids[i * 2 + 1] =
             lb_v + (2 * i + 1) * (ub_v - lb_v) / n / 2;;
       }
-      r = vp10_k_means(data, centroids, indices, pre_indices, rows * cols, n,
-                       2, max_itr);
+      vp10_k_means(data, centroids, color_map, rows * cols, n, 2, max_itr);
       pmi->palette_size[1] = n;
       for (i = 1; i < 3; ++i) {
         for (j = 0; j < n; ++j) {
@@ -3822,7 +3815,6 @@
                 clip_pixel((int)lroundf(centroids[j * 2 + i - 1]));
         }
       }
-      memcpy(color_map, indices, rows * cols * sizeof(*color_map));
 
       super_block_uvrd(cpi, x, &this_rate_tokenonly,
                        &this_distortion, &s, &this_sse, bsize, *best_rd);
@@ -3854,7 +3846,7 @@
       if (this_rd < *best_rd) {
         *best_rd = this_rd;
         *palette_mode_info = *pmi;
-        memcpy(best_palette_color_map, xd->plane[1].color_index_map,
+        memcpy(best_palette_color_map, color_map,
                rows * cols * sizeof(best_palette_color_map[0]));
         *mode_selected = DC_PRED;
         *rate = this_rate;
@@ -8287,7 +8279,6 @@
   const uint8_t *const src_u = x->plane[1].src.buf;
   const uint8_t *const src_v = x->plane[2].src.buf;
   float *const data = x->palette_buffer->kmeans_data_buf;
-  uint8_t *const indices = x->palette_buffer->kmeans_indices_buf;
   float centroids[2 * PALETTE_MAX_SIZE];
   uint8_t *const color_map = xd->plane[1].color_index_map;
   int r, c;
@@ -8323,9 +8314,8 @@
     }
   }
 
-  vp10_calc_indices(data, centroids, indices, rows * cols,
+  vp10_calc_indices(data, centroids, color_map, rows * cols,
                     pmi->palette_size[1], 2);
-  memcpy(color_map, indices, rows * cols * sizeof(*color_map));
 }
 
 #if CONFIG_EXT_INTRA