Palette code: add comments and rename some variables.

- Added comments for some tables and #defines for clarity.
- Renamed some variables to ensure we use "color_index" instead of
"color" for palette color index related variables.

Change-Id: Ica95a26e0f171a41a3259c8e6b3b891b8cd10151
diff --git a/av1/common/entropymode.c b/av1/common/entropymode.c
index 108e43d..688fc72 100644
--- a/av1/common/entropymode.c
+++ b/av1/common/entropymode.c
@@ -776,6 +776,9 @@
 };
 
 #if CONFIG_PALETTE
+
+// Tree to code palette size (number of colors in a palette) and the
+// corresponding probabilities for Y and UV planes.
 const aom_tree_index av1_palette_size_tree[TREE_SIZE(PALETTE_SIZES)] = {
   -TWO_COLORS,  2, -THREE_COLORS, 4,  -FOUR_COLORS,  6,
   -FIVE_COLORS, 8, -SIX_COLORS,   10, -SEVEN_COLORS, -EIGHT_COLORS,
@@ -808,6 +811,9 @@
 #endif  // CONFIG_EXT_PARTITION
     };
 
+// When palette mode is enabled, following probability tables indicate the
+// probabilities to code the "is_palette" bit (i.e. the bit that indicates
+// if this block uses palette mode or DC_PRED mode).
 const aom_prob
     av1_default_palette_y_mode_prob[PALETTE_BLOCK_SIZES]
                                    [PALETTE_Y_MODE_CONTEXTS] = {
@@ -822,10 +828,15 @@
 #endif  // CONFIG_EXT_PARTITION
                                    };
 
-const aom_prob av1_default_palette_uv_mode_prob[2] = { 253, 229 };
+const aom_prob av1_default_palette_uv_mode_prob[PALETTE_UV_MODE_CONTEXTS] = {
+  253, 229
+};
 
+// Trees to code palette color indices (for various palette sizes), and the
+// corresponding probability tables for Y and UV planes.
 const aom_tree_index
-    av1_palette_color_tree[PALETTE_MAX_SIZE - 1][TREE_SIZE(PALETTE_COLORS)] = {
+    av1_palette_color_index_tree[PALETTE_MAX_SIZE -
+                                 1][TREE_SIZE(PALETTE_COLORS)] = {
       { // 2 colors
         -PALETTE_COLOR_ONE, -PALETTE_COLOR_TWO },
       { // 3 colors
@@ -852,8 +863,8 @@
 // Note: Has to be non-zero to avoid any asserts triggering.
 #define UNUSED_PROB 128
 
-const aom_prob av1_default_palette_y_color_prob
-    [PALETTE_MAX_SIZE - 1][PALETTE_COLOR_CONTEXTS][PALETTE_COLORS - 1] = {
+const aom_prob av1_default_palette_y_color_index_prob
+    [PALETTE_MAX_SIZE - 1][PALETTE_COLOR_INDEX_CONTEXTS][PALETTE_COLORS - 1] = {
       {
           // 2 colors
           { 231, UNUSED_PROB, UNUSED_PROB, UNUSED_PROB, UNUSED_PROB,
@@ -922,8 +933,8 @@
       },
     };
 
-const aom_prob av1_default_palette_uv_color_prob
-    [PALETTE_MAX_SIZE - 1][PALETTE_COLOR_CONTEXTS][PALETTE_COLORS - 1] = {
+const aom_prob av1_default_palette_uv_color_index_prob
+    [PALETTE_MAX_SIZE - 1][PALETTE_COLOR_INDEX_CONTEXTS][PALETTE_COLORS - 1] = {
       {
           // 2 colors
           { 233, UNUSED_PROB, UNUSED_PROB, UNUSED_PROB, UNUSED_PROB,
@@ -996,9 +1007,9 @@
 
 #define MAX_COLOR_CONTEXT_HASH 8
 // Negative values are invalid
-static const int palette_color_context_lookup[MAX_COLOR_CONTEXT_HASH + 1] = {
-  -1, -1, 0, -1, -1, 4, 3, 2, 1
-};
+static const int palette_color_index_context_lookup[MAX_COLOR_CONTEXT_HASH +
+                                                    1] = { -1, -1, 0, -1, -1,
+                                                           4,  3,  2, 1 };
 
 #endif  // CONFIG_PALETTE
 
@@ -1064,10 +1075,10 @@
 #endif  // CONFIG_LOOP_RESTORATION
 
 #if CONFIG_PALETTE
-#define NUM_PALETTE_NEIGHBORS 3
-int av1_get_palette_color_context(const uint8_t *color_map, int stride, int r,
-                                  int c, int palette_size, uint8_t *color_order,
-                                  int *color_idx) {
+#define NUM_PALETTE_NEIGHBORS 3  // left, top-left and top.
+int av1_get_palette_color_index_context(const uint8_t *color_map, int stride,
+                                        int r, int c, int palette_size,
+                                        uint8_t *color_order, int *color_idx) {
   int i;
   // The +10 below should not be needed. But we get a warning "array subscript
   // is above array bounds [-Werror=array-bounds]" without it, possibly due to
@@ -1075,8 +1086,8 @@
   int scores[PALETTE_MAX_SIZE + 10];
   const int weights[NUM_PALETTE_NEIGHBORS] = { 2, 1, 2 };
   const int hash_multipliers[NUM_PALETTE_NEIGHBORS] = { 1, 2, 2 };
-  int color_ctx_hash;
-  int color_ctx;
+  int color_index_ctx_hash;
+  int color_index_ctx;
   int color_neighbors[NUM_PALETTE_NEIGHBORS];
   int inverse_color_order[PALETTE_MAX_SIZE];
   assert(palette_size <= PALETTE_MAX_SIZE);
@@ -1128,22 +1139,22 @@
   }
 
   // Get hash value of context.
-  color_ctx_hash = 0;
+  color_index_ctx_hash = 0;
   for (i = 0; i < NUM_PALETTE_NEIGHBORS; ++i) {
-    color_ctx_hash += scores[i] * hash_multipliers[i];
+    color_index_ctx_hash += scores[i] * hash_multipliers[i];
   }
-  assert(color_ctx_hash > 0);
-  assert(color_ctx_hash <= MAX_COLOR_CONTEXT_HASH);
+  assert(color_index_ctx_hash > 0);
+  assert(color_index_ctx_hash <= MAX_COLOR_CONTEXT_HASH);
 
   // Lookup context from hash.
-  color_ctx = palette_color_context_lookup[color_ctx_hash];
-  assert(color_ctx >= 0);
-  assert(color_ctx < PALETTE_COLOR_CONTEXTS);
+  color_index_ctx = palette_color_index_context_lookup[color_index_ctx_hash];
+  assert(color_index_ctx >= 0);
+  assert(color_index_ctx < PALETTE_COLOR_INDEX_CONTEXTS);
 
   if (color_idx != NULL) {
     *color_idx = inverse_color_order[color_map[r * stride + c]];
   }
-  return color_ctx;
+  return color_index_ctx;
 }
 #undef NUM_PALETTE_NEIGHBORS
 #undef MAX_COLOR_CONTEXT_HASH
diff --git a/av1/common/entropymode.h b/av1/common/entropymode.h
index 625eddd..ca84256 100644
--- a/av1/common/entropymode.h
+++ b/av1/common/entropymode.h
@@ -32,10 +32,32 @@
 #endif  // CONFIG_EXT_INTER
 
 #if CONFIG_PALETTE
-#define PALETTE_COLOR_CONTEXTS 5
+// Number of possible contexts for a color index.
+// As can be seen from av1_get_palette_color_index_context(), the possible
+// contexts are (2,0,0), (2,2,1), (3,2,0), (4,1,0), (5,0,0). These are mapped to
+// a value from 0 to 4 using 'palette_color_index_context_lookup' table.
+#define PALETTE_COLOR_INDEX_CONTEXTS 5
+
+// Maximum number of colors in a palette.
 #define PALETTE_MAX_SIZE 8
+
+// Palette mode is available for block sizes >= 8x8.
 #define PALETTE_BLOCK_SIZES (BLOCK_LARGEST - BLOCK_8X8 + 1)
+
+// Palette Y mode context for a block is determined by number of neighboring
+// blocks (top and/or left) using a palette for Y plane. So, possible Y mode'
+// context values are:
+// 0 if neither left nor top block uses palette for Y plane,
+// 1 if exactly one of left or top block uses palette for Y plane, and
+// 2 if both left and top blocks use palette for Y plane.
 #define PALETTE_Y_MODE_CONTEXTS 3
+
+// Palette UV mode context for a block is determined by whether this block uses
+// palette for the Y plane. So, possible values are:
+// 0 if this block doesn't use palette for Y plane.
+// 1 if this block uses palette for Y plane (i.e. Y palette size > 0).
+#define PALETTE_UV_MODE_CONTEXTS 2
+
 #define PALETTE_MAX_BLOCK_SIZE (64 * 64)
 #endif  // CONFIG_PALETTE
 
@@ -344,17 +366,20 @@
 #if CONFIG_PALETTE
 extern const aom_prob av1_default_palette_y_mode_prob[PALETTE_BLOCK_SIZES]
                                                      [PALETTE_Y_MODE_CONTEXTS];
-extern const aom_prob av1_default_palette_uv_mode_prob[2];
+extern const aom_prob
+    av1_default_palette_uv_mode_prob[PALETTE_UV_MODE_CONTEXTS];
 extern const aom_prob av1_default_palette_y_size_prob[PALETTE_BLOCK_SIZES]
                                                      [PALETTE_SIZES - 1];
 extern const aom_prob av1_default_palette_uv_size_prob[PALETTE_BLOCK_SIZES]
                                                       [PALETTE_SIZES - 1];
-extern const aom_prob av1_default_palette_y_color_prob[PALETTE_MAX_SIZE - 1]
-                                                      [PALETTE_COLOR_CONTEXTS]
-                                                      [PALETTE_COLORS - 1];
-extern const aom_prob av1_default_palette_uv_color_prob[PALETTE_MAX_SIZE - 1]
-                                                       [PALETTE_COLOR_CONTEXTS]
-                                                       [PALETTE_COLORS - 1];
+extern const aom_prob
+    av1_default_palette_y_color_index_prob[PALETTE_MAX_SIZE - 1]
+                                          [PALETTE_COLOR_INDEX_CONTEXTS]
+                                          [PALETTE_COLORS - 1];
+extern const aom_prob
+    av1_default_palette_uv_color_index_prob[PALETTE_MAX_SIZE - 1]
+                                           [PALETTE_COLOR_INDEX_CONTEXTS]
+                                           [PALETTE_COLORS - 1];
 #endif  // CONFIG_PALETTE
 
 extern const aom_tree_index av1_intra_mode_tree[TREE_SIZE(INTRA_MODES)];
@@ -381,8 +406,9 @@
     av1_switchable_interp_tree[TREE_SIZE(SWITCHABLE_FILTERS)];
 #if CONFIG_PALETTE
 extern const aom_tree_index av1_palette_size_tree[TREE_SIZE(PALETTE_SIZES)];
-extern const aom_tree_index av1_palette_color_tree[PALETTE_MAX_SIZE - 1]
-                                                  [TREE_SIZE(PALETTE_COLORS)];
+extern const aom_tree_index
+    av1_palette_color_index_tree[PALETTE_MAX_SIZE - 1]
+                                [TREE_SIZE(PALETTE_COLORS)];
 #endif  // CONFIG_PALETTE
 extern const aom_tree_index av1_tx_size_tree[MAX_TX_DEPTH][TREE_SIZE(TX_SIZES)];
 #if CONFIG_EXT_INTRA
@@ -439,9 +465,9 @@
 // Returns the context for palette color index at row 'r' and column 'c',
 // along with the 'color_order' of neighbors and the 'color_idx'.
 // The 'color_map' is a 2D array with the given 'stride'.
-int av1_get_palette_color_context(const uint8_t *color_map, int stride, int r,
-                                  int c, int palette_size, uint8_t *color_order,
-                                  int *color_idx);
+int av1_get_palette_color_index_context(const uint8_t *color_map, int stride,
+                                        int r, int c, int palette_size,
+                                        uint8_t *color_order, int *color_idx);
 #endif  // CONFIG_PALETTE
 
 #ifdef __cplusplus
diff --git a/av1/decoder/decodemv.c b/av1/decoder/decodemv.c
index f5dd86f..6751434 100644
--- a/av1/decoder/decodemv.c
+++ b/av1/decoder/decodemv.c
@@ -574,17 +574,20 @@
   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;
-  int i, n, palette_ctx = 0;
+  int i, n;
   PALETTE_MODE_INFO *const pmi = &mbmi->palette_mode_info;
 
   if (mbmi->mode == DC_PRED) {
+    int palette_y_mode_ctx = 0;
     if (above_mi)
-      palette_ctx += (above_mi->mbmi.palette_mode_info.palette_size[0] > 0);
+      palette_y_mode_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 (aom_read(
-            r, av1_default_palette_y_mode_prob[bsize - BLOCK_8X8][palette_ctx],
-            ACCT_STR)) {
+      palette_y_mode_ctx +=
+          (left_mi->mbmi.palette_mode_info.palette_size[0] > 0);
+    if (aom_read(r, av1_default_palette_y_mode_prob[bsize - BLOCK_8X8]
+                                                   [palette_y_mode_ctx],
+                 ACCT_STR)) {
       pmi->palette_size[0] =
           aom_read_tree(r, av1_palette_size_tree,
                         av1_default_palette_y_size_prob[bsize - BLOCK_8X8],
@@ -600,7 +603,8 @@
   }
 
   if (mbmi->uv_mode == DC_PRED) {
-    if (aom_read(r, av1_default_palette_uv_mode_prob[pmi->palette_size[0] > 0],
+    const int palette_uv_mode_ctx = (pmi->palette_size[0] > 0);
+    if (aom_read(r, av1_default_palette_uv_mode_prob[palette_uv_mode_ctx],
                  ACCT_STR)) {
       pmi->palette_size[1] =
           aom_read_tree(r, av1_palette_size_tree,
diff --git a/av1/decoder/detokenize.c b/av1/decoder/detokenize.c
index dd1964b..0003c86 100644
--- a/av1/decoder/detokenize.c
+++ b/av1/decoder/detokenize.c
@@ -318,9 +318,10 @@
   const int n = mbmi->palette_mode_info.palette_size[plane];
   int i, j;
   uint8_t *const color_map = xd->plane[plane].color_index_map;
-  const aom_prob(*const prob)[PALETTE_COLOR_CONTEXTS][PALETTE_COLORS - 1] =
-      plane ? av1_default_palette_uv_color_prob
-            : av1_default_palette_y_color_prob;
+  const aom_prob(*const prob)[PALETTE_COLOR_INDEX_CONTEXTS]
+                             [PALETTE_COLORS - 1] =
+                                 plane ? av1_default_palette_uv_color_index_prob
+                                       : av1_default_palette_y_color_index_prob;
   int plane_block_width, plane_block_height, rows, cols;
   av1_get_block_dimensions(mbmi->sb_type, plane, xd, &plane_block_width,
                            &plane_block_height, &rows, &cols);
@@ -328,10 +329,11 @@
 
   for (i = 0; i < rows; ++i) {
     for (j = (i == 0 ? 1 : 0); j < cols; ++j) {
-      const int color_ctx = av1_get_palette_color_context(
+      const int color_ctx = av1_get_palette_color_index_context(
           color_map, plane_block_width, i, j, n, color_order, NULL);
-      const int color_idx = aom_read_tree(r, av1_palette_color_tree[n - 2],
-                                          prob[n - 2][color_ctx], ACCT_STR);
+      const int color_idx =
+          aom_read_tree(r, av1_palette_color_index_tree[n - 2],
+                        prob[n - 2][color_ctx], ACCT_STR);
       assert(color_idx >= 0 && color_idx < n);
       color_map[i * plane_block_width + j] = color_order[color_idx];
     }
diff --git a/av1/encoder/bitstream.c b/av1/encoder/bitstream.c
index 7275ef3..b4545fb 100644
--- a/av1/encoder/bitstream.c
+++ b/av1/encoder/bitstream.c
@@ -77,8 +77,8 @@
 #endif  // CONFIG_EXT_INTER
 #if CONFIG_PALETTE
 static struct av1_token palette_size_encodings[PALETTE_MAX_SIZE - 1];
-static struct av1_token palette_color_encodings[PALETTE_MAX_SIZE - 1]
-                                               [PALETTE_MAX_SIZE];
+static struct av1_token palette_color_index_encodings[PALETTE_MAX_SIZE - 1]
+                                                     [PALETTE_MAX_SIZE];
 #endif  // CONFIG_PALETTE
 static const struct av1_token tx_size_encodings[MAX_TX_DEPTH][TX_SIZES] = {
   { { 0, 1 }, { 1, 1 } },                      // Max tx_size is 8X8
@@ -160,7 +160,8 @@
 #if CONFIG_PALETTE
   av1_tokens_from_tree(palette_size_encodings, av1_palette_size_tree);
   for (s = 0; s < PALETTE_MAX_SIZE - 1; ++s) {
-    av1_tokens_from_tree(palette_color_encodings[s], av1_palette_color_tree[s]);
+    av1_tokens_from_tree(palette_color_index_encodings[s],
+                         av1_palette_color_index_tree[s]);
   }
 #endif  // CONFIG_PALETTE
 
@@ -726,8 +727,8 @@
   const TOKENEXTRA *p = *tp;
 
   for (i = 0; i < num; ++i) {
-    av1_write_token(w, av1_palette_color_tree[n - 2], p->context_tree,
-                    &palette_color_encodings[n - 2][p->token]);
+    av1_write_token(w, av1_palette_color_index_tree[n - 2], p->context_tree,
+                    &palette_color_index_encodings[n - 2][p->token]);
     ++p;
   }
 
@@ -1137,17 +1138,19 @@
   const MODE_INFO *const left_mi = xd->left_mi;
   const BLOCK_SIZE bsize = mbmi->sb_type;
   const PALETTE_MODE_INFO *const pmi = &mbmi->palette_mode_info;
-  int palette_ctx = 0;
-  int n, i;
+  int i;
 
   if (mbmi->mode == DC_PRED) {
-    n = pmi->palette_size[0];
+    const int n = pmi->palette_size[0];
+    int palette_y_mode_ctx = 0;
     if (above_mi)
-      palette_ctx += (above_mi->mbmi.palette_mode_info.palette_size[0] > 0);
+      palette_y_mode_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);
-    aom_write(w, n > 0,
-              av1_default_palette_y_mode_prob[bsize - BLOCK_8X8][palette_ctx]);
+      palette_y_mode_ctx +=
+          (left_mi->mbmi.palette_mode_info.palette_size[0] > 0);
+    aom_write(w, n > 0, av1_default_palette_y_mode_prob[bsize - BLOCK_8X8]
+                                                       [palette_y_mode_ctx]);
     if (n > 0) {
       av1_write_token(w, av1_palette_size_tree,
                       av1_default_palette_y_size_prob[bsize - BLOCK_8X8],
@@ -1159,9 +1162,9 @@
   }
 
   if (mbmi->uv_mode == DC_PRED) {
-    n = pmi->palette_size[1];
-    aom_write(w, n > 0,
-              av1_default_palette_uv_mode_prob[pmi->palette_size[0] > 0]);
+    const int n = pmi->palette_size[1];
+    const int palette_uv_mode_ctx = (pmi->palette_size[0] > 0);
+    aom_write(w, n > 0, av1_default_palette_uv_mode_prob[palette_uv_mode_ctx]);
     if (n > 0) {
       av1_write_token(w, av1_palette_size_tree,
                       av1_default_palette_uv_size_prob[bsize - BLOCK_8X8],
diff --git a/av1/encoder/encoder.h b/av1/encoder/encoder.h
index 81eba9f..1bcb7b8 100644
--- a/av1/encoder/encoder.h
+++ b/av1/encoder/encoder.h
@@ -568,9 +568,9 @@
 #if CONFIG_PALETTE
   int palette_y_size_cost[PALETTE_BLOCK_SIZES][PALETTE_SIZES];
   int palette_uv_size_cost[PALETTE_BLOCK_SIZES][PALETTE_SIZES];
-  int palette_y_color_cost[PALETTE_MAX_SIZE - 1][PALETTE_COLOR_CONTEXTS]
+  int palette_y_color_cost[PALETTE_MAX_SIZE - 1][PALETTE_COLOR_INDEX_CONTEXTS]
                           [PALETTE_COLORS];
-  int palette_uv_color_cost[PALETTE_MAX_SIZE - 1][PALETTE_COLOR_CONTEXTS]
+  int palette_uv_color_cost[PALETTE_MAX_SIZE - 1][PALETTE_COLOR_INDEX_CONTEXTS]
                            [PALETTE_COLORS];
 #endif  // CONFIG_PALETTE
   int tx_size_cost[TX_SIZES - 1][TX_SIZE_CONTEXTS][TX_SIZES];
diff --git a/av1/encoder/rd.c b/av1/encoder/rd.c
index e13f009..724912a 100644
--- a/av1/encoder/rd.c
+++ b/av1/encoder/rd.c
@@ -102,13 +102,13 @@
   }
 
   for (i = 0; i < PALETTE_MAX_SIZE - 1; ++i) {
-    for (j = 0; j < PALETTE_COLOR_CONTEXTS; ++j) {
+    for (j = 0; j < PALETTE_COLOR_INDEX_CONTEXTS; ++j) {
       av1_cost_tokens(cpi->palette_y_color_cost[i][j],
-                      av1_default_palette_y_color_prob[i][j],
-                      av1_palette_color_tree[i]);
+                      av1_default_palette_y_color_index_prob[i][j],
+                      av1_palette_color_index_tree[i]);
       av1_cost_tokens(cpi->palette_uv_color_cost[i][j],
-                      av1_default_palette_uv_color_prob[i][j],
-                      av1_palette_color_tree[i]);
+                      av1_default_palette_uv_color_index_prob[i][j],
+                      av1_palette_color_index_tree[i]);
     }
   }
 #endif  // CONFIG_PALETTE
diff --git a/av1/encoder/rdopt.c b/av1/encoder/rdopt.c
index 13230e0..1574422 100644
--- a/av1/encoder/rdopt.c
+++ b/av1/encoder/rdopt.c
@@ -2428,7 +2428,7 @@
       for (i = 0; i < rows; ++i) {
         for (j = (i == 0 ? 1 : 0); j < cols; ++j) {
           int color_idx;
-          const int color_ctx = av1_get_palette_color_context(
+          const int color_ctx = av1_get_palette_color_index_context(
               color_map, block_width, i, j, k, color_order, &color_idx);
           assert(color_idx >= 0 && color_idx < k);
           palette_mode_cost +=
@@ -3511,7 +3511,7 @@
       cpi->common.allow_screen_content_tools
           ? x->palette_buffer->best_palette_color_map
           : NULL;
-  int palette_ctx = 0;
+  int palette_y_mode_ctx = 0;
 #endif  // CONFIG_PALETTE
   const MODE_INFO *above_mi = xd->above_mi;
   const MODE_INFO *left_mi = xd->left_mi;
@@ -3544,9 +3544,10 @@
 #if CONFIG_PALETTE
   pmi->palette_size[0] = 0;
   if (above_mi)
-    palette_ctx += (above_mi->mbmi.palette_mode_info.palette_size[0] > 0);
+    palette_y_mode_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);
+    palette_y_mode_ctx += (left_mi->mbmi.palette_mode_info.palette_size[0] > 0);
 #endif  // CONFIG_PALETTE
 
   if (cpi->sf.tx_type_search.fast_intra_tx_type_search)
@@ -3608,8 +3609,10 @@
     }
 #if CONFIG_PALETTE
     if (cpi->common.allow_screen_content_tools && mbmi->mode == DC_PRED)
-      this_rate += av1_cost_bit(
-          av1_default_palette_y_mode_prob[bsize - BLOCK_8X8][palette_ctx], 0);
+      this_rate +=
+          av1_cost_bit(av1_default_palette_y_mode_prob[bsize - BLOCK_8X8]
+                                                      [palette_y_mode_ctx],
+                       0);
 #endif  // CONFIG_PALETTE
 #if CONFIG_FILTER_INTRA
     if (mbmi->mode == DC_PRED)
@@ -3658,10 +3661,10 @@
 
 #if CONFIG_PALETTE
   if (cpi->common.allow_screen_content_tools) {
-    rd_pick_palette_intra_sby(cpi, x, bsize, palette_ctx, bmode_costs[DC_PRED],
-                              &best_mbmi, best_palette_color_map, &best_rd,
-                              &best_model_rd, rate, rate_tokenonly, distortion,
-                              skippable);
+    rd_pick_palette_intra_sby(cpi, x, bsize, palette_y_mode_ctx,
+                              bmode_costs[DC_PRED], &best_mbmi,
+                              best_palette_color_map, &best_rd, &best_model_rd,
+                              rate, rate_tokenonly, distortion, skippable);
   }
 #endif  // CONFIG_PALETTE
 
@@ -4510,7 +4513,7 @@
       for (i = 0; i < rows; ++i) {
         for (j = (i == 0 ? 1 : 0); j < cols; ++j) {
           int color_idx;
-          const int color_ctx = av1_get_palette_color_context(
+          const int color_ctx = av1_get_palette_color_index_context(
               color_map, plane_block_width, i, j, n, color_order, &color_idx);
           assert(color_idx >= 0 && color_idx < n);
           this_rate += cpi->palette_uv_color_cost[n - 2][color_ctx][color_idx];
diff --git a/av1/encoder/tokenize.c b/av1/encoder/tokenize.c
index b7ac41d..9823f84 100644
--- a/av1/encoder/tokenize.c
+++ b/av1/encoder/tokenize.c
@@ -397,9 +397,11 @@
   int i, j;
   int this_rate = 0;
   uint8_t color_order[PALETTE_MAX_SIZE];
-  const aom_prob(*const probs)[PALETTE_COLOR_CONTEXTS][PALETTE_COLORS - 1] =
-      plane == 0 ? av1_default_palette_y_color_prob
-                 : av1_default_palette_uv_color_prob;
+  const aom_prob(*const probs)[PALETTE_COLOR_INDEX_CONTEXTS]
+                              [PALETTE_COLORS - 1] =
+                                  plane == 0
+                                      ? av1_default_palette_y_color_index_prob
+                                      : av1_default_palette_uv_color_index_prob;
   int plane_block_width, rows, cols;
   av1_get_block_dimensions(bsize, plane, xd, &plane_block_width, NULL, &rows,
                            &cols);
@@ -408,7 +410,7 @@
   for (i = 0; i < rows; ++i) {
     for (j = (i == 0 ? 1 : 0); j < cols; ++j) {
       int color_new_idx;
-      const int color_ctx = av1_get_palette_color_context(
+      const int color_ctx = av1_get_palette_color_index_context(
           color_map, plane_block_width, i, j, n, color_order, &color_new_idx);
       assert(color_new_idx >= 0 && color_new_idx < n);
       if (dry_run == DRY_RUN_COSTCOEFFS)