Palette: remove palette_first_color_idx[] in PALETTE_MODE_INFO

Handle the first color index in the tokenization process, along with the
other color indeices.

This patch also includes some minor refactoring changes.

Test results verify that there is no implact on compression efficiency.

Change-Id: I7de51c18a52f337320331b5e8d63dfea3cf510f0
diff --git a/av1/decoder/decodemv.c b/av1/decoder/decodemv.c
index 166721b..05fdc46 100644
--- a/av1/decoder/decodemv.c
+++ b/av1/decoder/decodemv.c
@@ -35,19 +35,6 @@
 
 #define DEC_MISMATCH_DEBUG 0
 
-#if CONFIG_EXT_INTRA || CONFIG_FILTER_INTRA || CONFIG_PALETTE
-static INLINE int read_uniform(aom_reader *r, int n) {
-  const int l = get_unsigned_bits(n);
-  const int m = (1 << l) - n;
-  const int v = aom_read_literal(r, l - 1, ACCT_STR);
-  assert(l != 0);
-  if (v < m)
-    return v;
-  else
-    return (v << 1) - m + aom_read_literal(r, 1, ACCT_STR);
-}
-#endif  // CONFIG_EXT_INTRA || CONFIG_FILTER_INTRA || CONFIG_PALETTE
-
 static PREDICTION_MODE read_intra_mode(aom_reader *r, aom_cdf_prob *cdf) {
   return (PREDICTION_MODE)
       av1_intra_mode_inv[aom_read_symbol(r, cdf, INTRA_MODES, ACCT_STR)];
@@ -809,17 +796,18 @@
   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 n;
   PALETTE_MODE_INFO *const pmi = &mbmi->palette_mode_info;
 
   if (mbmi->mode == DC_PRED) {
     int palette_y_mode_ctx = 0;
-    if (above_mi)
+    if (above_mi) {
       palette_y_mode_ctx +=
           (above_mi->mbmi.palette_mode_info.palette_size[0] > 0);
-    if (left_mi)
+    }
+    if (left_mi) {
       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)) {
@@ -835,16 +823,12 @@
                         ACCT_STR) +
           2;
 #endif
-      n = pmi->palette_size[0];
 #if CONFIG_PALETTE_DELTA_ENCODING
       read_palette_colors_y(xd, cm->bit_depth, pmi, r);
 #else
-      int i;
-      for (i = 0; i < n; ++i)
+      for (int i = 0; i < pmi->palette_size[0]; ++i)
         pmi->palette_colors[i] = aom_read_literal(r, cm->bit_depth, ACCT_STR);
 #endif  // CONFIG_PALETTE_DELTA_ENCODING
-      xd->plane[0].color_index_map[0] = read_uniform(r, n);
-      assert(xd->plane[0].color_index_map[0] < n);
     }
   }
 
@@ -864,20 +848,16 @@
                         ACCT_STR) +
           2;
 #endif
-      n = pmi->palette_size[1];
 #if CONFIG_PALETTE_DELTA_ENCODING
       read_palette_colors_uv(xd, cm->bit_depth, pmi, r);
 #else
-      int i;
-      for (i = 0; i < n; ++i) {
+      for (int i = 0; i < pmi->palette_size[1]; ++i) {
         pmi->palette_colors[PALETTE_MAX_SIZE + i] =
             aom_read_literal(r, cm->bit_depth, ACCT_STR);
         pmi->palette_colors[2 * PALETTE_MAX_SIZE + i] =
             aom_read_literal(r, cm->bit_depth, ACCT_STR);
       }
 #endif  // CONFIG_PALETTE_DELTA_ENCODING
-      xd->plane[1].color_index_map[0] = read_uniform(r, n);
-      assert(xd->plane[1].color_index_map[0] < n);
     }
   }
 }
@@ -902,7 +882,7 @@
         aom_read(r, cm->fc->filter_intra_probs[0], ACCT_STR);
     if (filter_intra_mode_info->use_filter_intra_mode[0]) {
       filter_intra_mode_info->filter_intra_mode[0] =
-          read_uniform(r, FILTER_INTRA_MODES);
+          av1_read_uniform(r, FILTER_INTRA_MODES);
     }
     if (counts) {
       ++counts
@@ -929,7 +909,7 @@
         aom_read(r, cm->fc->filter_intra_probs[1], ACCT_STR);
     if (filter_intra_mode_info->use_filter_intra_mode[1]) {
       filter_intra_mode_info->filter_intra_mode[1] =
-          read_uniform(r, FILTER_INTRA_MODES);
+          av1_read_uniform(r, FILTER_INTRA_MODES);
     }
     if (counts) {
       ++counts
@@ -959,7 +939,7 @@
 
   if (av1_is_directional_mode(mbmi->mode, bsize)) {
     mbmi->angle_delta[0] =
-        read_uniform(r, 2 * MAX_ANGLE_DELTA + 1) - MAX_ANGLE_DELTA;
+        av1_read_uniform(r, 2 * MAX_ANGLE_DELTA + 1) - MAX_ANGLE_DELTA;
 #if CONFIG_INTRA_INTERP
     p_angle = mode_to_angle_map[mbmi->mode] + mbmi->angle_delta[0] * ANGLE_STEP;
     if (av1_is_intra_filter_switchable(p_angle)) {
@@ -975,7 +955,7 @@
 
   if (av1_is_directional_mode(mbmi->uv_mode, bsize)) {
     mbmi->angle_delta[1] =
-        read_uniform(r, 2 * MAX_ANGLE_DELTA + 1) - MAX_ANGLE_DELTA;
+        av1_read_uniform(r, 2 * MAX_ANGLE_DELTA + 1) - MAX_ANGLE_DELTA;
   }
 }
 #endif  // CONFIG_EXT_INTRA
diff --git a/av1/decoder/decoder.h b/av1/decoder/decoder.h
index 6f62911..7a6e7cd 100644
--- a/av1/decoder/decoder.h
+++ b/av1/decoder/decoder.h
@@ -215,6 +215,20 @@
 }
 #endif  // CONFIG_EXT_REFS
 
+#if CONFIG_EXT_INTRA || CONFIG_FILTER_INTRA || CONFIG_PALETTE
+#define ACCT_STR __func__
+static INLINE int av1_read_uniform(aom_reader *r, int n) {
+  const int l = get_unsigned_bits(n);
+  const int m = (1 << l) - n;
+  const int v = aom_read_literal(r, l - 1, ACCT_STR);
+  assert(l != 0);
+  if (v < m)
+    return v;
+  else
+    return (v << 1) - m + aom_read_literal(r, 1, ACCT_STR);
+}
+#endif  // CONFIG_EXT_INTRA || CONFIG_FILTER_INTRA || CONFIG_PALETTE
+
 #ifdef __cplusplus
 }  // extern "C"
 #endif
diff --git a/av1/decoder/detokenize.c b/av1/decoder/detokenize.c
index ce0b70c..8e30139 100644
--- a/av1/decoder/detokenize.c
+++ b/av1/decoder/detokenize.c
@@ -16,6 +16,7 @@
 #endif  // !CONFIG_PVQ
 
 #include "av1/common/blockd.h"
+#include "av1/decoder/detokenize.h"
 
 #define ACCT_STR __func__
 
@@ -23,7 +24,6 @@
 #include "av1/common/common.h"
 #include "av1/common/entropy.h"
 #include "av1/common/idct.h"
-#include "av1/decoder/detokenize.h"
 
 #define EOB_CONTEXT_NODE 0
 #define ZERO_CONTEXT_NODE 1
@@ -225,7 +225,6 @@
   const MB_MODE_INFO *const mbmi = &mi->mbmi;
   uint8_t color_order[PALETTE_MAX_SIZE];
   const int n = mbmi->palette_mode_info.palette_size[plane];
-  int i, j;
   uint8_t *const color_map = xd->plane[plane].color_index_map;
 #if CONFIG_NEW_MULTISYMBOL
   aom_cdf_prob(
@@ -244,10 +243,14 @@
                            &plane_block_height, &rows, &cols);
   assert(plane == 0 || plane == 1);
 
+  // The first color index.
+  color_map[0] = av1_read_uniform(r, n);
+  assert(color_map[0] < n);
+
 #if CONFIG_PALETTE_THROUGHPUT
   // Run wavefront on the palette map index decoding.
-  for (i = 1; i < rows + cols - 1; ++i) {
-    for (j = AOMMIN(i, cols - 1); j >= AOMMAX(0, i - rows + 1); --j) {
+  for (int i = 1; i < rows + cols - 1; ++i) {
+    for (int j = AOMMIN(i, cols - 1); j >= AOMMAX(0, i - rows + 1); --j) {
       const int color_ctx = av1_get_palette_color_index_context(
           color_map, plane_block_width, (i - j), j, n, color_order, NULL);
 #if CONFIG_NEW_MULTISYMBOL
@@ -264,15 +267,15 @@
   }
   // Copy last column to extra columns.
   if (cols < plane_block_width) {
-    for (i = 0; i < plane_block_height; ++i) {
+    for (int i = 0; i < plane_block_height; ++i) {
       memset(color_map + i * plane_block_width + cols,
              color_map[i * plane_block_width + cols - 1],
              (plane_block_width - cols));
     }
   }
 #else
-  for (i = 0; i < rows; ++i) {
-    for (j = (i == 0 ? 1 : 0); j < cols; ++j) {
+  for (int i = 0; i < rows; ++i) {
+    for (int j = (i == 0 ? 1 : 0); j < cols; ++j) {
       const int color_ctx = av1_get_palette_color_index_context(
           color_map, plane_block_width, i, j, n, color_order, NULL);
 #if CONFIG_NEW_MULTISYMBOL
@@ -292,7 +295,7 @@
   }
 #endif  // CONFIG_PALETTE_THROUGHPUT
   // Copy last row to extra rows.
-  for (i = rows; i < plane_block_height; ++i) {
+  for (int i = rows; i < plane_block_height; ++i) {
     memcpy(color_map + i * plane_block_width,
            color_map + (rows - 1) * plane_block_width, plane_block_width);
   }
diff --git a/av1/decoder/detokenize.h b/av1/decoder/detokenize.h
index ba40666..0e58a28 100644
--- a/av1/decoder/detokenize.h
+++ b/av1/decoder/detokenize.h
@@ -14,9 +14,9 @@
 
 #include "./aom_config.h"
 #if !CONFIG_PVQ || CONFIG_VAR_TX
-#include "av1/decoder/decoder.h"
 #include "av1/common/scan.h"
-#endif  // !CONFIG_PVQ
+#endif  // !CONFIG_PVQ || CONFIG_VAR_TX
+#include "av1/decoder/decoder.h"
 
 #ifdef __cplusplus
 extern "C" {