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/encoder/bitstream.c b/av1/encoder/bitstream.c
index bcb95d8..8704469 100644
--- a/av1/encoder/bitstream.c
+++ b/av1/encoder/bitstream.c
@@ -643,10 +643,11 @@
 #if CONFIG_PALETTE
 static void pack_palette_tokens(aom_writer *w, const TOKENEXTRA **tp, int n,
                                 int num) {
-  int i;
   const TOKENEXTRA *p = *tp;
-
-  for (i = 0; i < num; ++i) {
+  write_uniform(w, n, p->token);  // The first color index.
+  ++p;
+  --num;
+  for (int i = 0; i < num; ++i) {
 #if CONFIG_NEW_MULTISYMBOL
     aom_write_symbol(w, p->token, p->palette_cdf, n);
 #else
@@ -656,7 +657,6 @@
 #endif
     ++p;
   }
-
   *tp = p;
 }
 #endif  // CONFIG_PALETTE
@@ -1493,12 +1493,14 @@
   if (mbmi->mode == DC_PRED) {
     const int n = pmi->palette_size[0];
     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);
+    }
     aom_write(
         w, n > 0,
         av1_default_palette_y_mode_prob[bsize - BLOCK_8X8][palette_y_mode_ctx]);
@@ -1520,7 +1522,6 @@
         aom_write_literal(w, pmi->palette_colors[i], cm->bit_depth);
       }
 #endif  // CONFIG_PALETTE_DELTA_ENCODING
-      write_uniform(w, n, pmi->palette_first_color_idx[0]);
     }
   }
 
@@ -1552,7 +1553,6 @@
                           cm->bit_depth);
       }
 #endif  // CONFIG_PALETTE_DELTA_ENCODING
-      write_uniform(w, n, pmi->palette_first_color_idx[1]);
     }
   }
 }
@@ -2503,7 +2503,7 @@
       av1_get_block_dimensions(mbmi->sb_type, plane, xd, NULL, NULL, &rows,
                                &cols);
       assert(*tok < tok_end);
-      pack_palette_tokens(w, tok, palette_size_plane, rows * cols - 1);
+      pack_palette_tokens(w, tok, palette_size_plane, rows * cols);
       assert(*tok < tok_end + mbmi->skip);
     }
   }