]> granicus.if.org Git - libvpx/commitdiff
Palette high bit depth functionality
authorJulia Robson <juliamrobson@gmail.com>
Tue, 2 Jun 2015 13:31:38 +0000 (14:31 +0100)
committerPeter de Rivaz <peter.derivaz@gmail.com>
Mon, 22 Jun 2015 17:53:34 +0000 (18:53 +0100)
Changes to allow high bit depth and palette to be enabled at the
same time by using a 16 bit (instead of 8bit) palette when high
bit depth is enabled and modifying related functions accordingly.

Change-Id: I97d30b4d9338d3a51db02c94bc568eba60a8905d

vp9/common/vp9_blockd.h
vp9/common/vp9_onyxc_int.h
vp9/common/vp9_palette.c
vp9/common/vp9_palette.h
vp9/common/vp9_reconintra.c
vp9/decoder/vp9_decodemv.c
vp9/encoder/vp9_bitstream.c
vp9/encoder/vp9_context_tree.h
vp9/encoder/vp9_encodeframe.c
vp9/encoder/vp9_rdopt.c

index 5002af261d6c6d422cbe6575afeab82905ce3999..e32f054957c751d90e92f96c168d39bd12ab8550 100644 (file)
@@ -248,12 +248,17 @@ typedef struct {
   int palette_literal_size;
   int current_palette_size;
   int palette_delta_bitdepth;
-  uint8_t palette_colors[3 * PALETTE_MAX_SIZE];
   uint8_t palette_indexed_colors[PALETTE_MAX_SIZE];
   int8_t palette_color_delta[PALETTE_MAX_SIZE];
-  uint8_t palette_literal_colors[PALETTE_MAX_SIZE];
   uint8_t *palette_color_map;
   uint8_t *palette_uv_color_map;
+#if CONFIG_VP9_HIGHBITDEPTH
+  uint16_t palette_colors[3 * PALETTE_MAX_SIZE];
+  uint16_t palette_literal_colors[PALETTE_MAX_SIZE];
+#else
+  uint8_t palette_colors[3 * PALETTE_MAX_SIZE];
+  uint8_t palette_literal_colors[PALETTE_MAX_SIZE];
+#endif  // CONFIG_VP9_HIGHBITDEPTH
 #endif  // CONFIG_PALETTE
 } MB_MODE_INFO;
 
index 64be5955b0b5ab4ce790e5bec3d09b214668fe2e..515f2f72e06885ec8d27411589647c985fb7cda6 100644 (file)
@@ -223,7 +223,11 @@ typedef struct VP9Common {
   ENTROPY_CONTEXT *above_context;
 
 #if CONFIG_PALETTE
+#if CONFIG_VP9_HIGHBITDEPTH
+  uint16_t current_palette_colors[PALETTE_BUF_SIZE];
+#else
   uint8_t current_palette_colors[PALETTE_BUF_SIZE];
+#endif
   int current_palette_size;
   int current_palette_count[PALETTE_BUF_SIZE];
   int allow_palette_mode;
index d5c6d7bbf5dfcb59411adf25701ea7395b3cb848..e7ee8f347a531139c35f4e3549031437a144a181 100644 (file)
@@ -59,12 +59,47 @@ int vp9_count_colors(const uint8_t *src, int stride, int rows, int cols) {
     return n;
 }
 
+#if CONFIG_VP9_HIGHBITDEPTH
+int vp9_count_colors_highbd(const uint8_t *src8, int stride, int rows, int cols,
+                            int bit_depth) {
+  int n = 0, r, c, i;
+  uint16_t val;
+  uint16_t *src = CONVERT_TO_SHORTPTR(src8);
+  int* val_count = vpx_calloc(1 << bit_depth, sizeof(*val_count));
+
+  for (r = 0; r < rows; r++) {
+      for (c = 0; c < cols; c++) {
+        val = src[r * stride + c];
+        val_count[val]++;
+      }
+    }
+
+    for (i = 0; i < (1 << bit_depth); i++) {
+      if (val_count[i]) {
+        n++;
+      }
+    }
+
+    vpx_free(val_count);
+
+    return n;
+}
+#endif  // CONFIG_VP9_HIGHBITDEPTH
+
+
+#if CONFIG_VP9_HIGHBITDEPTH
+void vp9_palette_color_insertion(uint16_t *old_colors, int *m, int *count,
+                                 const MB_MODE_INFO *mbmi) {
+  const uint16_t *new_colors = mbmi->palette_literal_colors;
+  uint16_t val;
+#else
 void vp9_palette_color_insertion(uint8_t *old_colors, int *m, int *count,
                                  const MB_MODE_INFO *mbmi) {
-  int k = *m, n = mbmi->palette_literal_size;
-  int i, j, l, min_idx = -1;
   const uint8_t *new_colors = mbmi->palette_literal_colors;
   uint8_t val;
+#endif  // CONFIG_VP9_HIGHBITDEPTH
+  int k = *m, n = mbmi->palette_literal_size;
+  int i, j, l, min_idx = -1;
 
   if (mbmi->palette_indexed_size > 0) {
     for (i = 0; i < mbmi->palette_indexed_size; i++)
@@ -108,7 +143,11 @@ void vp9_palette_color_insertion(uint8_t *old_colors, int *m, int *count,
   *m = k;
 }
 
+#if CONFIG_VP9_HIGHBITDEPTH
+int vp9_palette_color_lookup(uint16_t *dic, int n, uint16_t val, int bits) {
+#else
 int vp9_palette_color_lookup(uint8_t *dic, int n, uint8_t val, int bits) {
+#endif  // CONFIG_VP9_HIGHBITDEPTH
   int j, min, arg_min = 0, i = 1;
 
   if (n < 1)
index fa62bd9b7cb58ae8dae2a924731e4f7d9877c3ab..bc7187e754a7696245b1193a199f3ff236ab7f1e 100644 (file)
 
 #if CONFIG_PALETTE
 int vp9_count_colors(const uint8_t *src, int stride, int rows, int cols);
-void vp9_insertion_sort(double *data, int n);
+#if CONFIG_VP9_HIGHBITDEPTH
+int vp9_count_colors_highbd(const uint8_t *src8, int stride, int rows, int cols,
+                            int bit_depth);
+void vp9_palette_color_insertion(uint16_t *old_colors, int *m, int *count,
+                                 const MB_MODE_INFO *mbmi);
+int vp9_palette_color_lookup(uint16_t *dic, int n, uint16_t val, int bits);
+#else
 void vp9_palette_color_insertion(uint8_t *old_colors, int *m, int *count,
                                  const MB_MODE_INFO *mbmi);
 int vp9_palette_color_lookup(uint8_t *dic, int n, uint8_t val, int bits);
+#endif  // CONFIG_VP9_HIGHBITDEPTH
+void vp9_insertion_sort(double *data, int n);
 int vp9_ceil_log2(int n);
 int vp9_k_means(const double *data, double *centroids, int *indices,
                 int n, int k, int dim, int max_itr);
index 25948609efd08665bc0c6dfe545eddc36680bf54..f1128682c35a8ff3606f8b1fd6e1bcefe2aba98c 100644 (file)
@@ -1359,37 +1359,22 @@ void vp9_predict_intra_block(const MACROBLOCKD *xd, int block_idx, int bwl_in,
 #endif  // CONFIG_FILTERINTRA
 
   assert(bwl >= 0);
-#if CONFIG_VP9_HIGHBITDEPTH
-  if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
-#if CONFIG_FILTERINTRA
-    if (!filterflag) {
-#endif
-      build_intra_predictors_highbd(xd, ref, ref_stride, dst, dst_stride,
-                                    mode, tx_size, have_top,
-                                    have_left, have_right, x, y,
-                                    plane, xd->bd);
-#if CONFIG_FILTERINTRA
-    } else {
-      build_filter_intra_predictors_highbd(xd, ref, ref_stride, dst, dst_stride,
-                                           mode, tx_size, have_top,
-                                           have_left, have_right, x, y,
-                                           plane, xd->bd);
-    }
-#endif
-    return;
-  }
-#endif  // CONFIG_VP9_HIGHBITDEPTH
 #if CONFIG_FILTERINTRA
   if (!filterflag) {
 #endif  // CONFIG_FILTERINTRA
 #if CONFIG_PALETTE
     if (xd->mi[0].src_mi->mbmi.palette_enabled[plane !=0]) {
-      uint8_t *palette = xd->mi[0].src_mi->mbmi.palette_colors +
-          plane * PALETTE_MAX_SIZE;
       int bs = 4 * (1 << tx_size);
       int stride = 4 * (1 << bwl_in);
       int r, c;
       uint8_t *map = NULL;
+#if CONFIG_VP9_HIGHBITDEPTH
+      uint16_t *palette = xd->mi[0].src_mi->mbmi.palette_colors +
+          plane * PALETTE_MAX_SIZE;
+#else
+      uint8_t *palette = xd->mi[0].src_mi->mbmi.palette_colors +
+          plane * PALETTE_MAX_SIZE;
+#endif  // CONFIG_VP9_HIGHBITDEPTH
 
       if (xd->plane[1].subsampling_x || xd->plane[1].subsampling_y)
         map = xd->plane[plane != 0].color_index_map;
@@ -1398,16 +1383,43 @@ void vp9_predict_intra_block(const MACROBLOCKD *xd, int block_idx, int bwl_in,
 
       for (r = 0; r < bs; r++) {
         for (c = 0; c < bs; c++) {
+#if CONFIG_VP9_HIGHBITDEPTH
+          if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
+            uint16_t *dst16 = CONVERT_TO_SHORTPTR(dst);
+            dst16[r * dst_stride + c] = palette[map[(r + y) * stride + c + x]];
+          } else {
+#endif  // CONFIG_VP9_HIGHBITDEPTH
           dst[r * dst_stride + c] = palette[map[(r + y) * stride + c + x]];
+#if CONFIG_VP9_HIGHBITDEPTH
+          }
+#endif  // CONFIG_VP9_HIGHBITDEPTH
         }
       }
       return;
     }
 #endif  // CONFIG_PALETTE
+#if CONFIG_VP9_HIGHBITDEPTH
+    if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
+      build_intra_predictors_highbd(xd, ref, ref_stride, dst, dst_stride,
+                                    mode, tx_size, have_top,
+                                    have_left, have_right, x, y,
+                                    plane, xd->bd);
+      return;
+    }
+#endif  // CONFIG_VP9_HIGHBITDEPTH
     build_intra_predictors(xd, ref, ref_stride, dst, dst_stride, mode, tx_size,
                            have_top, have_left, have_right, x, y, plane);
 #if CONFIG_FILTERINTRA
   } else {
+#if CONFIG_VP9_HIGHBITDEPTH
+    if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
+      build_filter_intra_predictors_highbd(xd, ref, ref_stride, dst, dst_stride,
+                                           mode, tx_size, have_top,
+                                           have_left, have_right, x, y,
+                                           plane, xd->bd);
+      return;
+    }
+#endif  // CONFIG_VP9_HIGHBITDEPTH
     build_filter_intra_predictors(xd, ref, ref_stride, dst, dst_stride, mode,
                                   tx_size, have_top, have_left, have_right,
                                   x, y, plane);
index 24d7cf20f0de8874f4c9b74a6285617205c33f9b..95f89a4b0f674dd08c0da70220fd04b978317e2b 100644 (file)
@@ -363,7 +363,7 @@ static void read_intra_frame_mode_info(VP9_COMMON *const cm,
     }
     if (m2 > 0) {
       for (i = 0; i < m2; i++) {
-        mbmi->palette_literal_colors[i] = vp9_read_literal(r, 8);
+        mbmi->palette_literal_colors[i] = vp9_read_literal(r, cm->bit_depth);
         mbmi->palette_colors[m1 + i] = mbmi->palette_literal_colors[i];
       }
     }
@@ -407,9 +407,11 @@ static void read_intra_frame_mode_info(VP9_COMMON *const cm,
     }
 
     for (i = 0; i < mbmi->palette_size[1]; i++)
-      mbmi->palette_colors[PALETTE_MAX_SIZE + i] = vp9_read_literal(r, 8);
+      mbmi->palette_colors[PALETTE_MAX_SIZE + i] =
+          vp9_read_literal(r, cm->bit_depth);
     for (i = 0; i < mbmi->palette_size[1]; i++)
-      mbmi->palette_colors[2 * PALETTE_MAX_SIZE + i] = vp9_read_literal(r, 8);
+      mbmi->palette_colors[2 * PALETTE_MAX_SIZE + i] =
+          vp9_read_literal(r, cm->bit_depth);
 
     if (xd->plane[1].subsampling_x && xd->plane[1].subsampling_y) {
       int color_idx = 0, color_ctx = 0;
@@ -1406,7 +1408,7 @@ static void read_inter_frame_mode_info(VP9_COMMON *const cm,
       n = mbmi->palette_size[0];
 
       for (i = 0; i < mbmi->palette_size[0]; i++)
-        mbmi->palette_colors[i] = vp9_read_literal(r, 8);
+        mbmi->palette_colors[i] = vp9_read_literal(r, cm->bit_depth);
 
       color_map[0] = vp9_read_literal(r,
                                       vp9_ceil_log2(mbmi->palette_size[0]));
@@ -1448,9 +1450,11 @@ static void read_inter_frame_mode_info(VP9_COMMON *const cm,
       }
 
       for (i = 0; i < mbmi->palette_size[1]; i++)
-        mbmi->palette_colors[PALETTE_MAX_SIZE + i] = vp9_read_literal(r, 8);
+        mbmi->palette_colors[PALETTE_MAX_SIZE + i] =
+            vp9_read_literal(r, cm->bit_depth);
       for (i = 0; i < mbmi->palette_size[1]; i++)
-        mbmi->palette_colors[2 * PALETTE_MAX_SIZE + i] = vp9_read_literal(r, 8);
+        mbmi->palette_colors[2 * PALETTE_MAX_SIZE + i] =
+            vp9_read_literal(r, cm->bit_depth);
 
       if (xd->plane[1].subsampling_x && xd->plane[1].subsampling_y) {
         int color_idx = 0, color_ctx = 0;
index 9e7f0da78efc7cd9e92ebb9ae8b2ec81276d4058..459e5699538ff5352a8cea35d0ca88d745f55342 100644 (file)
@@ -531,7 +531,7 @@ static void pack_inter_mode_mvs(VP9_COMP *cpi, const MODE_INFO *mi,
                       cm->fc.palette_size_prob[bsize - BLOCK_8X8],
                       &palette_size_encodings[n - 2]);
       for (i = 0; i < n; i++)
-        vp9_write_literal(w, mbmi->palette_colors[i], 8);
+        vp9_write_literal(w, mbmi->palette_colors[i], cm->bit_depth);
 
       memcpy(buffer, mbmi->palette_color_map,
              rows * cols * sizeof(buffer[0]));
@@ -566,9 +566,11 @@ static void pack_inter_mode_mvs(VP9_COMP *cpi, const MODE_INFO *mi,
       }
 
       for (i = 0; i < n; i++)
-        vp9_write_literal(w, mbmi->palette_colors[PALETTE_MAX_SIZE + i], 8);
+        vp9_write_literal(w, mbmi->palette_colors[PALETTE_MAX_SIZE + i],
+                          cm->bit_depth);
       for (i = 0; i < n; i++)
-        vp9_write_literal(w, mbmi->palette_colors[2 * PALETTE_MAX_SIZE + i], 8);
+        vp9_write_literal(w, mbmi->palette_colors[2 * PALETTE_MAX_SIZE + i],
+                          cm->bit_depth);
 
       if (xd->plane[1].subsampling_x && xd->plane[1].subsampling_y) {
         memcpy(buffer, mbmi->palette_uv_color_map,
@@ -945,7 +947,7 @@ static void write_mb_modes_kf(const VP9_COMMON *cm,
       }
       if (m2 > 0) {
         for (i = 0; i < m2; i++)
-          vp9_write_literal(w, mbmi->palette_literal_colors[i], 8);
+          vp9_write_literal(w, mbmi->palette_literal_colors[i], cm->bit_depth);
       }
 
       memcpy(buffer, mbmi->palette_color_map,
@@ -981,9 +983,11 @@ static void write_mb_modes_kf(const VP9_COMMON *cm,
       }
 
       for (i = 0; i < n; i++)
-        vp9_write_literal(w, mbmi->palette_colors[PALETTE_MAX_SIZE + i], 8);
+        vp9_write_literal(w, mbmi->palette_colors[PALETTE_MAX_SIZE + i],
+                          cm->bit_depth);
       for (i = 0; i < n; i++)
-        vp9_write_literal(w, mbmi->palette_colors[2 * PALETTE_MAX_SIZE + i], 8);
+        vp9_write_literal(w, mbmi->palette_colors[2 * PALETTE_MAX_SIZE + i],
+                          cm->bit_depth);
 
       if (xd->plane[1].subsampling_x && xd->plane[1].subsampling_y) {
         memcpy(buffer, mbmi->palette_uv_color_map,
index 21d75ed5d18a88ffdfa32ab30615d36ebfd73448..d26e9f2ae25914f1ffd563f51e10b26ff0d03de4 100644 (file)
@@ -32,10 +32,14 @@ typedef struct {
   uint16_t *eobs_pbuf[MAX_MB_PLANE][3];
 #if CONFIG_PALETTE
   uint8_t *color_index_map[2];
+#if CONFIG_VP9_HIGHBITDEPTH
+  uint16_t palette_colors_buf[PALETTE_BUF_SIZE];
+#else
   uint8_t palette_colors_buf[PALETTE_BUF_SIZE];
+#endif  // CONFIG_VP9_HIGHBITDEPTH
   int palette_buf_size;
   int palette_count_buf[PALETTE_BUF_SIZE];
-#endif
+#endif  // CONFIG_PALETTE
 
   int is_coded;
   int num_4x4_blk;
@@ -92,4 +96,4 @@ typedef struct PC_TREE {
 void vp9_setup_pc_tree(struct VP9Common *cm, struct VP9_COMP *cpi);
 void vp9_free_pc_tree(struct VP9_COMP *cpi);
 
-#endif /* VP9_ENCODER_VP9_CONTEXT_TREE_H_ */
+#endif  // VP9_ENCODER_VP9_CONTEXT_TREE_H_
index d6cba2ef2245c5982d708288667733bf713ab0f5..0d5beaebd809eae627bf1796bcdfba66a68c44ec 100644 (file)
@@ -1439,8 +1439,12 @@ static void rd_pick_sb_modes(VP9_COMP *cpi, const TileInfo *const tile,
   if (frame_is_intra_only(cm)) {
 #if CONFIG_PALETTE
     int n = cpi->common.current_palette_size;
-    uint8_t palette[PALETTE_BUF_SIZE];
     int count[PALETTE_BUF_SIZE];
+#if CONFIG_VP9_HIGHBITDEPTH
+    uint16_t palette[PALETTE_BUF_SIZE];
+#else
+    uint8_t palette[PALETTE_BUF_SIZE];
+#endif  // CONFIG_VP9_HIGHBITDEPTH
 
     vpx_memcpy(palette, cpi->common.current_palette_colors,
                n * sizeof(palette[0]));
@@ -1540,7 +1544,6 @@ static void update_stats(VP9_COMMON *cm, const MACROBLOCK *x) {
     const int seg_ref_active = vp9_segfeature_active(&cm->seg, mbmi->segment_id,
                                                      SEG_LVL_REF_FRAME);
     if (!seg_ref_active) {
-
       counts->intra_inter[vp9_get_intra_inter_context(xd)][inter_block]++;
 
       // If the segment reference feature is enabled we have only a single
@@ -3211,8 +3214,12 @@ static void rd_pick_partition(VP9_COMP *cpi, const TileInfo *const tile,
 #if CONFIG_PALETTE
   PICK_MODE_CONTEXT *c, *p;
   int previous_size, previous_count[PALETTE_BUF_SIZE];
+#if CONFIG_VP9_HIGHBITDEPTH
+  uint16_t previous_colors[PALETTE_BUF_SIZE];
+#else
   uint8_t previous_colors[PALETTE_BUF_SIZE];
-#endif
+#endif  // CONFIG_VP9_HIGHBITDEPTH
+#endif  // CONFIG_PALETTE
   (void) *tp_orig;
 
   assert(num_8x8_blocks_wide_lookup[bsize] ==
@@ -5539,7 +5546,7 @@ static void sum_intra_stats(FRAME_COUNTS *counts,
 #if CONFIG_FILTERINTRA
   const MACROBLOCKD* xd,
 #endif
- const MODE_INFO *mi) {
 const MODE_INFO *mi) {
   const PREDICTION_MODE y_mode = mi->mbmi.mode;
   const PREDICTION_MODE uv_mode = mi->mbmi.uv_mode;
   const BLOCK_SIZE bsize = mi->mbmi.sb_type;
index a201e35dc22379871528647271d51da549768e28..ff95931ef00f1cca59cb77570e8e8fbf060e7066 100644 (file)
@@ -1784,9 +1784,15 @@ static int64_t rd_pick_intra_sby_mode(VP9_COMP *cpi, MACROBLOCK *x,
   int cols = 4 * num_4x4_blocks_wide_lookup[bsize];
   int src_stride = x->plane[0].src.stride;
   uint8_t *src = x->plane[0].src.buf;
-  uint8_t best_palette[PALETTE_MAX_SIZE];
-  uint8_t best_index[PALETTE_MAX_SIZE], best_literal[PALETTE_MAX_SIZE];
   int8_t palette_color_delta[PALETTE_MAX_SIZE];
+  uint8_t best_index[PALETTE_MAX_SIZE];
+#if CONFIG_VP9_HIGHBITDEPTH
+  uint16_t best_palette[PALETTE_MAX_SIZE];
+  uint16_t best_literal[PALETTE_MAX_SIZE];
+#else
+  uint8_t best_palette[PALETTE_MAX_SIZE];
+  uint8_t best_literal[PALETTE_MAX_SIZE];
+#endif  // CONFIG_VP9_HIGHBITDEPTH
 #endif  // CONFIG_PALETTE
 #if CONFIG_INTRABC
   if (is_intrabc_mode(A)) A = DC_PRED;
@@ -1939,6 +1945,12 @@ static int64_t rd_pick_intra_sby_mode(VP9_COMP *cpi, MACROBLOCK *x,
 
 #if CONFIG_PALETTE
   mic->mbmi.current_palette_size = cpi->common.current_palette_size;
+#if CONFIG_VP9_HIGHBITDEPTH
+  if (cpi->common.use_highbitdepth)
+    colors = vp9_count_colors_highbd(src, src_stride, rows, cols,
+                                     cpi->common.bit_depth);
+  else
+#endif  // CONFIG_VP9_HIGHBITDEPTH
   colors = vp9_count_colors(src, src_stride, rows, cols);
   if (colors > 1 && colors <= 64 && cpi->common.allow_palette_mode) {
     int n, r, c, i, j, temp, max_itr = 200, k;
@@ -1948,13 +1960,25 @@ static int64_t rd_pick_intra_sby_mode(VP9_COMP *cpi, MACROBLOCK *x,
     int color_order[PALETTE_MAX_SIZE];
     int palette_size_cost[PALETTE_SIZES];
     double centroids[PALETTE_MAX_SIZE];
-    double lb = src[0], ub = src[0], val;
     int64_t local_tx_cache[TX_MODES];
     uint8_t *color_map;
 #if CONFIG_TX_SKIP
     int this_rate_tokenonly_s, s_s;
     int64_t this_distortion_s;
 #endif  // CONFIG_TX_SKIP
+    double lb, ub, val;
+#if CONFIG_VP9_HIGHBITDEPTH
+    uint16_t *src16 = CONVERT_TO_SHORTPTR(src);
+    if (cpi->common.use_highbitdepth) {
+      lb = src16[0];
+      ub = src16[0];
+    } else {
+#endif  // CONFIG_VP9_HIGHBITDEPTH
+    lb = src[0];
+    ub = src[0];
+#if CONFIG_VP9_HIGHBITDEPTH
+    }
+#endif  // CONFIG_VP9_HIGHBITDEPTH
 
     vpx_memset(x->kmeans_data_buffer, 0,
                sizeof(x->kmeans_data_buffer[0] * 4096));
@@ -1970,6 +1994,12 @@ static int64_t rd_pick_intra_sby_mode(VP9_COMP *cpi, MACROBLOCK *x,
     mic->mbmi.mode = DC_PRED;
     for (r = 0; r < rows; r++) {
       for (c = 0; c < cols; c++) {
+#if CONFIG_VP9_HIGHBITDEPTH
+        if (cpi->common.use_highbitdepth)
+          val = src16[r * src_stride + c];
+        else
+#endif  // CONFIG_VP9_HIGHBITDEPTH
+
         val = src[r * src_stride + c];
         x->kmeans_data_buffer[r * cols + c] = val;
         if (val < lb)
@@ -2001,8 +2031,15 @@ static int64_t rd_pick_intra_sby_mode(VP9_COMP *cpi, MACROBLOCK *x,
         }
       }
 
-      for (i = 0; i < k; i++)
+      for (i = 0; i < k; i++) {
+#if CONFIG_VP9_HIGHBITDEPTH
+        if (cpi->common.use_highbitdepth)
+            mic->mbmi.palette_colors[i] = clip_pixel_highbd(round(centroids[i]),
+                                                         cpi->common.bit_depth);
+        else
+#endif  // CONFIG_VP9_HIGHBITDEPTH
         mic->mbmi.palette_colors[i] = clip_pixel(round(centroids[i]));
+      }
 
       best_total_bits = INT_MAX;
       for (bits = 0; bits < 1 << PALETTE_DELTA_BIT; bits++) {
@@ -2025,7 +2062,7 @@ static int64_t rd_pick_intra_sby_mode(VP9_COMP *cpi, MACROBLOCK *x,
           }
         }
         total_bits = m1 * vp9_ceil_log2(cpi->common.current_palette_size) +
-            m1 * (bits == 0 ? 0 : bits + 1) + m2 * 8;
+            m1 * (bits == 0 ? 0 : bits + 1) + m2 * cpi->common.bit_depth;
         if (total_bits <= best_total_bits) {
           best_total_bits = total_bits;
           best_bits = bits;
@@ -2115,7 +2152,8 @@ static int64_t rd_pick_intra_sby_mode(VP9_COMP *cpi, MACROBLOCK *x,
       this_rate = this_rate_tokenonly +
           (1 + vp9_encode_uniform_cost(MIN(k + 1, 8), m1) + PALETTE_DELTA_BIT
               + vp9_ceil_log2(mic->mbmi.current_palette_size) * m1 +
-              best_bits * m1 + 8 * m2) * vp9_cost_bit(128, 0) +
+              best_bits * m1 +
+              cpi->common.bit_depth * m2) * vp9_cost_bit(128, 0) +
               palette_size_cost[k - 2];
       color_map = xd->plane[0].color_index_map;
       this_rate +=  vp9_ceil_log2(k) * vp9_cost_bit(128, 0);
@@ -2169,7 +2207,7 @@ static int64_t rd_pick_intra_sby_mode(VP9_COMP *cpi, MACROBLOCK *x,
       }
     }
   }
-#endif
+#endif  // CONFIG_PALETTE
 
   mic->mbmi.mode = mode_selected;
 #if CONFIG_FILTERINTRA
@@ -2375,7 +2413,11 @@ static int64_t rd_pick_intra_sbuv_mode(VP9_COMP *cpi, MACROBLOCK *x,
   int cols = (4 * num_4x4_blocks_wide_lookup[bsize]) >>
       (xd->plane[1].subsampling_y);
   int src_stride = x->plane[1].src.stride;
+#if CONFIG_VP9_HIGHBITDEPTH
+  uint16_t best_palette[2 * PALETTE_MAX_SIZE];
+#else
   uint8_t best_palette[2 * PALETTE_MAX_SIZE];
+#endif
   uint8_t *src_u = x->plane[1].src.buf;
   uint8_t *src_v = x->plane[2].src.buf;
   MB_MODE_INFO *mbmi = &xd->mi[0].src_mi->mbmi;
@@ -2513,9 +2555,22 @@ static int64_t rd_pick_intra_sbuv_mode(VP9_COMP *cpi, MACROBLOCK *x,
   if (xd->mi[0].src_mi->mbmi.sb_type >= BLOCK_8X8 &&
       (xd->plane[1].subsampling_x || xd->plane[1].subsampling_y) &&
       cpi->common.allow_palette_mode) {
-    int colors_u = vp9_count_colors(src_u, src_stride, rows, cols);
-    int colors_v = vp9_count_colors(src_v, src_stride, rows, cols);
-    int colors = colors_u > colors_v ? colors_u : colors_v;
+    int colors_u, colors_v, colors;
+#if CONFIG_VP9_HIGHBITDEPTH
+    if (cpi->common.use_highbitdepth) {
+      colors_u = vp9_count_colors_highbd(src_u, src_stride, rows, cols,
+                                         cpi->common.bit_depth);
+      colors_v = vp9_count_colors_highbd(src_v, src_stride, rows, cols,
+                                         cpi->common.bit_depth);
+    } else {
+#endif  // CONFIG_VP9_HIGHBITDEPTH
+    colors_u = vp9_count_colors(src_u, src_stride, rows, cols);
+    colors_v = vp9_count_colors(src_v, src_stride, rows, cols);
+#if CONFIG_VP9_HIGHBITDEPTH
+    }
+#endif  // CONFIG_VP9_HIGHBITDEPTH
+
+    colors = colors_u > colors_v ? colors_u : colors_v;
 
     if (colors > 1 && colors <= 64) {
       int n, r, c, i, j, max_itr = 200;
@@ -2523,14 +2578,32 @@ static int64_t rd_pick_intra_sbuv_mode(VP9_COMP *cpi, MACROBLOCK *x,
       int color_order[PALETTE_MAX_SIZE];
       int palette_size_cost[PALETTE_SIZES];
       double centroids[2 * PALETTE_MAX_SIZE];
-      double lb_u = src_u[0], ub_u = src_u[0];
-      double lb_v = src_v[0], ub_v = src_v[0], val;
       BLOCK_SIZE uv_bsize = get_plane_block_size(bsize, &xd->plane[1]);
       uint8_t *color_map;
 #if CONFIG_TX_SKIP
       int this_rate_tokenonly_s, s_s;
       int64_t this_distortion_s;
 #endif  // CONFIG_TX_SKIP
+      double lb_u, ub_u, val_u;
+      double lb_v, ub_v, val_v;
+#if CONFIG_VP9_HIGHBITDEPTH
+      uint16_t *src_u16 = CONVERT_TO_SHORTPTR(src_u);
+      uint16_t *src_v16 = CONVERT_TO_SHORTPTR(src_v);
+      if (cpi->common.use_highbitdepth) {
+        lb_u = src_u16[0];
+        ub_u = src_u16[0];
+        lb_v = src_v16[0];
+        ub_v = src_v16[0];
+      } else {
+#endif  // CONFIG_VP9_HIGHBITDEPTH
+      lb_u = src_u[0];
+      ub_u = src_u[0];
+      lb_v = src_v[0];
+      ub_v = src_v[0];
+#if CONFIG_VP9_HIGHBITDEPTH
+      }
+#endif  // CONFIG_VP9_HIGHBITDEPTH
+
 
       i = uv_bsize - BLOCK_4X4;
       vp9_cost_tokens(palette_size_cost,
@@ -2544,22 +2617,35 @@ static int64_t rd_pick_intra_sbuv_mode(VP9_COMP *cpi, MACROBLOCK *x,
 
       for (r = 0; r < rows; r++) {
         for (c = 0; c < cols; c++) {
-          x->kmeans_data_buffer[(r * cols + c) * 2 ] =
-              src_u[r * src_stride + c];
-          x->kmeans_data_buffer[(r * cols + c) * 2 + 1] =
-              src_v[r * src_stride + c];
-          val = src_u[r * src_stride + c];
-          if (val < lb_u)
-            lb_u = val;
-          else if (val > ub_u)
-            ub_u = val;
-          val = src_v[r * src_stride + c];
-          if (val < lb_v)
-            lb_v = val;
-          else if (val > ub_v)
-            ub_v = val;
-        }
-      }
+#if CONFIG_VP9_HIGHBITDEPTH
+          if (cpi->common.use_highbitdepth) {
+            x->kmeans_data_buffer[(r * cols + c) * 2 ] =
+                src_u16[r * src_stride + c];
+            x->kmeans_data_buffer[(r * cols + c) * 2 + 1] =
+                src_v16[r * src_stride + c];
+            val_u = src_u16[r * src_stride + c];
+            val_v = src_v16[r * src_stride + c];
+          } else {
+#endif  // CONFIG_VP9_HIGHBITDEPTH
+           x->kmeans_data_buffer[(r * cols + c) * 2 ] =
+               src_u[r * src_stride + c];
+           x->kmeans_data_buffer[(r * cols + c) * 2 + 1] =
+               src_v[r * src_stride + c];
+          val_u = src_u[r * src_stride + c];
+          val_v = src_v[r * src_stride + c];
+#if CONFIG_VP9_HIGHBITDEPTH
+          }
+#endif  // CONFIG_VP9_HIGHBITDEPTH
+          if (val_u < lb_u)
+            lb_u = val_u;
+          else if (val_u > ub_u)
+            ub_u = val_u;
+          if (val_v < lb_v)
+            lb_v = val_v;
+          else if (val_v > ub_v)
+            ub_v = val_v;
+         }
+       }
 
       for (n = colors > PALETTE_MAX_SIZE ? PALETTE_MAX_SIZE : colors;
           n >= 2; n--) {
@@ -2573,9 +2659,17 @@ static int64_t rd_pick_intra_sbuv_mode(VP9_COMP *cpi, MACROBLOCK *x,
 
         mbmi->palette_size[1] = n;
         for (i = 1; i < 3; i++) {
-          for (j = 0; j < n; j++)
+          for (j = 0; j < n; j++) {
+#if CONFIG_VP9_HIGHBITDEPTH
+            if (cpi->common.use_highbitdepth)
+              mbmi->palette_colors[i * PALETTE_MAX_SIZE + j] =
+                  clip_pixel_highbd(round(centroids[j * 2 + i - 1]),
+                                cpi->common.bit_depth);
+            else
+#endif  // CONFIG_VP9_HIGHBITDEPTH
             mbmi->palette_colors[i * PALETTE_MAX_SIZE + j] =
                 clip_pixel(round(centroids[j * 2 + i - 1]));
+          }
         }
         for (r = 0; r < rows; r++)
           for (c = 0; c < cols; c++) {
@@ -2624,7 +2718,7 @@ static int64_t rd_pick_intra_sbuv_mode(VP9_COMP *cpi, MACROBLOCK *x,
 
         color_map = xd->plane[1].color_index_map;
         this_rate = this_rate_tokenonly +
-            (1 + 2 * 8 * n) * vp9_cost_bit(128, 0) +
+            (1 + 2 * cpi->common.bit_depth * n) * vp9_cost_bit(128, 0) +
             palette_size_cost[n - 2];
         this_rate +=  vp9_ceil_log2(n) * vp9_cost_bit(128, 0);
         for (i = 0; i < rows; i++) {
@@ -5462,12 +5556,25 @@ static void rd_pick_palette_444(VP9_COMP *cpi, MACROBLOCK *x, RD_COST *rd_cost,
   int cols = 4 * num_4x4_blocks_wide_lookup[bsize];
   int src_stride_y = x->plane[0].src.stride;
   int src_stride_uv = x->plane[1].src.stride;
-  int colors = vp9_count_colors(src_y, src_stride_y, rows, cols);
+  int colors;
+#if CONFIG_VP9_HIGHBITDEPTH
+  if (cpi->common.use_highbitdepth)
+    colors = vp9_count_colors_highbd(src_y, src_stride_y, rows, cols,
+                                     cpi->common.bit_depth);
+  else
+#endif  // CONFIG_VP9_HIGHBITDEPTH
+  colors = vp9_count_colors(src_y, src_stride_y, rows, cols);
 
   if (colors >= 2 && colors <= 64 && cm->allow_palette_mode) {
+#if CONFIG_VP9_HIGHBITDEPTH
+    uint16_t best_palette[PALETTE_MAX_SIZE * 3];
+    uint16_t best_literal[PALETTE_MAX_SIZE];
+#else
     uint8_t best_palette[PALETTE_MAX_SIZE * 3];
-    uint8_t best_index[PALETTE_MAX_SIZE], best_literal[PALETTE_MAX_SIZE];
+    uint8_t best_literal[PALETTE_MAX_SIZE];
+#endif  // CONFIG_VP9_HIGHBITDEPTH
     int8_t palette_color_delta[PALETTE_MAX_SIZE];
+    uint8_t best_index[PALETTE_MAX_SIZE];
     int64_t local_tx_cache[TX_MODES], sse;
     int m1, m2, n, best_bits, best_n = 0;
     int r, c, i, j, max_itr = 200;
@@ -5476,7 +5583,6 @@ static void rd_pick_palette_444(VP9_COMP *cpi, MACROBLOCK *x, RD_COST *rd_cost,
     int color_ctx = 0, color_idx = 0;
     int color_order[PALETTE_MAX_SIZE];
     double centroids[3 * PALETTE_MAX_SIZE];
-    double lb = src_y[0], ub = src_y[0];
     MB_MODE_INFO *mbmi = &xd->mi[0].src_mi->mbmi;
     MB_MODE_INFO mbmi_copy;
     RD_COST palette_rd, palette_best_rd;
@@ -5489,6 +5595,22 @@ static void rd_pick_palette_444(VP9_COMP *cpi, MACROBLOCK *x, RD_COST *rd_cost,
     int tx_skipped = 0, tx_skipped_uv = 0;
     int64_t this_distortion_s;
 #endif  // CONFIG_TX_SKIP
+    double lb = src_y[0], ub = src_y[0];
+#if CONFIG_VP9_HIGHBITDEPTH
+    uint16_t *src_y16 = CONVERT_TO_SHORTPTR(src_y);
+    uint16_t *src_u16 = CONVERT_TO_SHORTPTR(src_u);
+    uint16_t *src_v16 = CONVERT_TO_SHORTPTR(src_v);
+    if (cpi->common.use_highbitdepth) {
+      lb = src_y16[0];
+      ub = src_y16[0];
+    } else {
+#endif  // CONFIG_VP9_HIGHBITDEPTH
+    lb = src_y[0];
+    ub = src_y[0];
+#if CONFIG_VP9_HIGHBITDEPTH
+    }
+#endif  // CONFIG_VP9_HIGHBITDEPTH
+
 
     palette_best_rd.rate = INT_MAX;
     palette_best_rd.dist = INT64_MAX;
@@ -5512,11 +5634,25 @@ static void rd_pick_palette_444(VP9_COMP *cpi, MACROBLOCK *x, RD_COST *rd_cost,
 #endif  // CONFIG_FILTERINTRA
     for (r = 0; r < rows; r++) {
       for (c = 0; c < cols; c++) {
+#if CONFIG_VP9_HIGHBITDEPTH
+        if (cpi->common.use_highbitdepth) {
+          x->kmeans_data_buffer[(r * cols + c) * 3] =
+              src_y16[r * src_stride_y + c];
+          x->kmeans_data_buffer[(r * cols + c) * 3 + 1] =
+              src_u16[r * src_stride_uv + c];
+          x->kmeans_data_buffer[(r * cols + c) * 3 + 2] =
+              src_v16[r * src_stride_uv + c];
+        } else {
+#endif  // CONFIG_VP9_HIGHBITDEPTH
+
         x->kmeans_data_buffer[(r * cols + c) * 3] = src_y[r * src_stride_y + c];
         x->kmeans_data_buffer[(r * cols + c) * 3 + 1] =
             src_u[r * src_stride_uv + c];
         x->kmeans_data_buffer[(r * cols + c) * 3 + 2] =
             src_v[r * src_stride_uv + c];
+#if CONFIG_VP9_HIGHBITDEPTH
+        }
+#endif  // CONFIG_VP9_HIGHBITDEPTH
       }
     }
 
@@ -5530,9 +5666,17 @@ static void rd_pick_palette_444(VP9_COMP *cpi, MACROBLOCK *x, RD_COST *rd_cost,
       r = vp9_k_means(x->kmeans_data_buffer, centroids,
                       x->kmeans_indices_buffer, rows * cols, n, 3, max_itr);
       for (i = 0; i < 3; i++) {
-        for (j = 0; j < n; j++)
+        for (j = 0; j < n; j++) {
+#if CONFIG_VP9_HIGHBITDEPTH
+          if (cpi->common.use_highbitdepth)
+          mbmi->palette_colors[i * PALETTE_MAX_SIZE + j] =
+              clip_pixel_highbd(round(centroids[j * 3 + i]),
+                                cpi->common.bit_depth);
+          else
+#endif  // CONFIG_VP9_HIGHBITDEPTH
           mbmi->palette_colors[i * PALETTE_MAX_SIZE + j] =
               clip_pixel(round(centroids[j * 3 + i]));
+        }
       }
       for (r = 0; r < rows; r++)
         for (c = 0; c < cols; c++)
@@ -5609,7 +5753,8 @@ static void rd_pick_palette_444(VP9_COMP *cpi, MACROBLOCK *x, RD_COST *rd_cost,
         continue;
 
       rate_y = rate_y_tokenonly +
-               (1 + PALETTE_DELTA_BIT + n * m2) * vp9_cost_bit(128, 0) +
+               (1 + PALETTE_DELTA_BIT + cpi->common.bit_depth * m2) *
+               vp9_cost_bit(128, 0) +
                palette_size_cost[n - 2];
       color_map = xd->plane[0].color_index_map;
       rate_y += vp9_ceil_log2(n) * vp9_cost_bit(128, 0);
@@ -5626,7 +5771,8 @@ static void rd_pick_palette_444(VP9_COMP *cpi, MACROBLOCK *x, RD_COST *rd_cost,
           rate_y += cpi->palette_color_costs[n - 2][color_ctx][color_idx];
         }
       }
-      rate_uv = rate_uv_tokenonly + (1 + 8 * 2 * n) * vp9_cost_bit(128, 0);
+      rate_uv = rate_uv_tokenonly +
+          (1 + cpi->common.bit_depth * 2 * n) * vp9_cost_bit(128, 0);
 #if CONFIG_INTRABC
       if (cm->allow_intrabc_mode)
         rate_y += vp9_cost_bit(INTRABC_PROB, 0);
@@ -5929,9 +6075,15 @@ void vp9_rd_pick_inter_mode_sb(VP9_COMP *cpi, MACROBLOCK *x,
   int palette_enabled_uv[TX_SIZES];
   int palette_size_uv[TX_SIZES];
   uint8_t *src = x->plane[0].src.buf;
+#if CONFIG_VP9_HIGHBITDEPTH
+  uint16_t best_palette[PALETTE_MAX_SIZE];
+  uint16_t palette_colors_uv[TX_SIZES][2 * PALETTE_MAX_SIZE];
+  uint16_t palette_color_map_uv[TX_SIZES][4096];
+#else
   uint8_t best_palette[PALETTE_MAX_SIZE];
   uint8_t palette_colors_uv[TX_SIZES][2 * PALETTE_MAX_SIZE];
   uint8_t palette_color_map_uv[TX_SIZES][4096];
+#endif  // CONFIG_VP9_HIGHBITDEPTH
   const MODE_INFO *above_mi = xd->up_available ?
       xd->mi[-xd->mi_stride].src_mi : NULL;
   const MODE_INFO *left_mi = xd->left_available ?
@@ -7140,6 +7292,12 @@ void vp9_rd_pick_inter_mode_sb(VP9_COMP *cpi, MACROBLOCK *x,
       !is_inter_block(mbmi)) {
     MB_MODE_INFO mbmi_copy = *mbmi;
 
+#if CONFIG_VP9_HIGHBITDEPTH
+    if (cpi->common.use_highbitdepth)
+      colors = vp9_count_colors_highbd(src, src_stride, rows, cols,
+                                       cpi->common.bit_depth);
+    else
+#endif  // CONFIG_VP9_HIGHBITDEPTH
     colors = vp9_count_colors(src, src_stride, rows, cols);
     x->skip = 0;
     if (colors > 1 && colors <= 64) {
@@ -7148,7 +7306,6 @@ void vp9_rd_pick_inter_mode_sb(VP9_COMP *cpi, MACROBLOCK *x,
       int color_order[PALETTE_MAX_SIZE];
       int palette_size_cost[PALETTE_SIZES];
       double centroids[PALETTE_MAX_SIZE];
-      double lb = src[0], ub = src[0], val;
 
       int64_t this_rd = INT64_MAX, this_rd_y, best_rd_y;
       int rate2, rate_y , rate_uv, best_token_rate_y = INT_MAX;
@@ -7165,7 +7322,19 @@ void vp9_rd_pick_inter_mode_sb(VP9_COMP *cpi, MACROBLOCK *x,
       int64_t tx_cache_s[TX_MODES];
       int tx_skipped_y = 0;
 #endif  // CONFIG_TX_SKIP
-
+      double lb, ub, val;
+#if CONFIG_VP9_HIGHBITDEPTH
+      uint16_t *src16 = CONVERT_TO_SHORTPTR(src);
+      if (cpi->common.use_highbitdepth) {
+        lb = src16[0];
+        ub = src16[0];
+      } else {
+#endif
+      lb = src[0];
+      ub = src[0];
+#if CONFIG_VP9_HIGHBITDEPTH
+      }
+#endif
       vpx_memset(x->kmeans_data_buffer, 0,
                  sizeof(x->kmeans_data_buffer[0] * 4096));
       vpx_memset(x->kmeans_indices_buffer, 0,
@@ -7178,6 +7347,11 @@ void vp9_rd_pick_inter_mode_sb(VP9_COMP *cpi, MACROBLOCK *x,
       mbmi->mode = DC_PRED;
       for (r = 0; r < rows; r++) {
         for (c = 0; c < cols; c++) {
+#if CONFIG_VP9_HIGHBITDEPTH
+          if (cpi->common.use_highbitdepth)
+            val = src16[r * src_stride + c];
+          else
+#endif
           val = src[r * src_stride + c];
           x->kmeans_data_buffer[r * cols + c] = val;
           if (val < lb)
@@ -7218,6 +7392,12 @@ void vp9_rd_pick_inter_mode_sb(VP9_COMP *cpi, MACROBLOCK *x,
 
         mbmi->palette_size[0] = k;
         for (i = 0; i < k; i++) {
+#if CONFIG_VP9_HIGHBITDEPTH
+          if (cpi->common.use_highbitdepth)
+            mbmi->palette_colors[i] = clip_pixel_highbd(round(centroids[i]),
+                                                         cpi->common.bit_depth);
+          else
+#endif  // CONFIG_VP9_HIGHBITDEPTH
           mbmi->palette_colors[i] = clip_pixel(round(centroids[i]));
           centroids[i] = (double) mbmi->palette_colors[i];
         }
@@ -7263,7 +7443,7 @@ void vp9_rd_pick_inter_mode_sb(VP9_COMP *cpi, MACROBLOCK *x,
         }
 
         total_rate_y = rate_y + palette_size_cost[k - 2] +
-            8 * k * vp9_cost_bit(128, 0) +
+            cpi->common.bit_depth * k * vp9_cost_bit(128, 0) +
             vp9_cost_bit(cm->fc.palette_enabled_prob
                          [bsize - BLOCK_8X8][palette_ctx], 1);
         color_map = xd->plane[0].color_index_map;