Wrap palette code inside CONFIG_PALETTE flag.

This flag was already added to aomedia/master, so bringing it back to
webm/nextgenv2, as part of an effort to get the two codebases in sync.

Change-Id: I2b933a6a160e4210d1411a9e7978149eb8553205
diff --git a/av1/av1_cx.mk b/av1/av1_cx.mk
index 6461e4c..3f85a5f 100644
--- a/av1/av1_cx.mk
+++ b/av1/av1_cx.mk
@@ -66,8 +66,10 @@
 AV1_CX_SRCS-yes += encoder/treewriter.h
 AV1_CX_SRCS-yes += encoder/mcomp.c
 AV1_CX_SRCS-yes += encoder/encoder.c
+ifeq ($(CONFIG_PALETTE),yes)
 AV1_CX_SRCS-yes += encoder/palette.h
 AV1_CX_SRCS-yes += encoder/palette.c
+endif
 AV1_CX_SRCS-yes += encoder/picklpf.c
 AV1_CX_SRCS-yes += encoder/picklpf.h
 AV1_CX_SRCS-$(CONFIG_LOOP_RESTORATION) += encoder/pickrst.c
diff --git a/av1/common/blockd.h b/av1/common/blockd.h
index a541378..b16f512 100644
--- a/av1/common/blockd.h
+++ b/av1/common/blockd.h
@@ -155,6 +155,7 @@
 
 typedef int8_t MV_REFERENCE_FRAME;
 
+#if CONFIG_PALETTE
 typedef struct {
   // Number of base colors for Y (0) and UV (1)
   uint8_t palette_size[2];
@@ -168,6 +169,7 @@
   // TODO(huisu): move this to encoder
   uint8_t palette_first_color_idx[2];
 } PALETTE_MODE_INFO;
+#endif  // CONFIG_PALETTE
 
 #if CONFIG_EXT_INTRA
 typedef struct {
@@ -199,7 +201,9 @@
 
   // Only for INTRA blocks
   PREDICTION_MODE uv_mode;
+#if CONFIG_PALETTE
   PALETTE_MODE_INFO palette_mode_info;
+#endif  // CONFIG_PALETTE
 
 // Only for INTER blocks
 #if CONFIG_DUAL_FILTER
@@ -291,7 +295,9 @@
   dequant_val_type_nuq seg_dequant_nuq[MAX_SEGMENTS][QUANT_PROFILES]
                                       [COEF_BANDS];
 #endif
+#if CONFIG_PALETTE
   uint8_t *color_index_map;
+#endif  // CONFIG_PALETTE
 
   // number of 4x4s in current block
   uint16_t n4_w, n4_h;
diff --git a/av1/common/entropymode.c b/av1/common/entropymode.c
index 4daac84..84843d8 100644
--- a/av1/common/entropymode.c
+++ b/av1/common/entropymode.c
@@ -474,6 +474,7 @@
 #endif  // CONFIG_EXT_REFS
 };
 
+#if CONFIG_PALETTE
 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,
@@ -832,6 +833,7 @@
   // (7, 3, 0, 0), (8, 0, 0, 0), (8, 2, 0, 0), (10, 0, 0, 0)
   9680, 10648, 10890, 13310
 };
+#endif  // CONFIG_PALETTE
 
 const aom_tree_index av1_tx_size_tree[TX_SIZES - 1][TREE_SIZE(TX_SIZES)] = {
   {
@@ -883,6 +885,7 @@
 static const aom_prob default_rect_tx_prob[TX_SIZES - 1] = { 192, 192, 192 };
 #endif  // CONFIG_EXT_TX && CONFIG_RECT_TX && CONFIG_VAR_TX
 
+#if CONFIG_PALETTE
 int av1_get_palette_color_context(const uint8_t *color_map, int cols, int r,
                                   int c, int n, int *color_order) {
   int i, j, max, max_idx, temp;
@@ -951,6 +954,7 @@
 
   return color_ctx;
 }
+#endif  // CONFIG_PALETTE
 
 #if CONFIG_VAR_TX
 static const aom_prob default_txfm_partition_probs[TXFM_PARTITION_CONTEXTS] = {
diff --git a/av1/common/entropymode.h b/av1/common/entropymode.h
index 4f33bba..cfe279e 100644
--- a/av1/common/entropymode.h
+++ b/av1/common/entropymode.h
@@ -31,11 +31,13 @@
 #define INTER_COMPOUND_OFFSET(mode) ((mode)-NEAREST_NEARESTMV)
 #endif  // CONFIG_EXT_INTER
 
+#if CONFIG_PALETTE
 #define PALETTE_COLOR_CONTEXTS 16
 #define PALETTE_MAX_SIZE 8
 #define PALETTE_BLOCK_SIZES (BLOCK_LARGEST - BLOCK_8X8 + 1)
 #define PALETTE_Y_MODE_CONTEXTS 3
 #define PALETTE_MAX_BLOCK_SIZE (64 * 64)
+#endif  // CONFIG_PALETTE
 
 struct AV1Common;
 
@@ -218,6 +220,7 @@
 
 extern const aom_prob av1_kf_y_mode_prob[INTRA_MODES][INTRA_MODES]
                                         [INTRA_MODES - 1];
+#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];
@@ -231,6 +234,7 @@
 extern const aom_prob av1_default_palette_uv_color_prob[PALETTE_MAX_SIZE - 1]
                                                        [PALETTE_COLOR_CONTEXTS]
                                                        [PALETTE_COLORS - 1];
+#endif  // CONFIG_PALETTE
 
 extern const aom_tree_index av1_intra_mode_tree[TREE_SIZE(INTRA_MODES)];
 extern const aom_tree_index av1_inter_mode_tree[TREE_SIZE(INTER_MODES)];
@@ -247,9 +251,11 @@
 #endif
 extern const aom_tree_index
     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)];
+#endif  // CONFIG_PALETTE
 extern const aom_tree_index av1_tx_size_tree[TX_SIZES - 1][TREE_SIZE(TX_SIZES)];
 #if CONFIG_EXT_INTRA
 extern const aom_tree_index av1_intra_filter_tree[TREE_SIZE(INTRA_FILTERS)];
@@ -287,8 +293,10 @@
   return i;
 }
 
+#if CONFIG_PALETTE
 int av1_get_palette_color_context(const uint8_t *color_map, int cols, int r,
                                   int c, int n, int *color_order);
+#endif  // CONFIG_PALETTE
 
 #ifdef __cplusplus
 }  // extern "C"
diff --git a/av1/common/enums.h b/av1/common/enums.h
index 7ea6776..79c0f18 100644
--- a/av1/common/enums.h
+++ b/av1/common/enums.h
@@ -223,6 +223,7 @@
 
 typedef enum { PLANE_TYPE_Y = 0, PLANE_TYPE_UV = 1, PLANE_TYPES } PLANE_TYPE;
 
+#if CONFIG_PALETTE
 typedef enum {
   TWO_COLORS,
   THREE_COLORS,
@@ -245,6 +246,7 @@
   PALETTE_COLOR_EIGHT,
   PALETTE_COLORS
 } PALETTE_COLOR;
+#endif  // CONFIG_PALETTE
 
 #ifdef CONFIG_CLPF
 #define CLPF_NOFLAG -1
diff --git a/av1/common/onyxc_int.h b/av1/common/onyxc_int.h
index 3a2203a..ee8cb47 100644
--- a/av1/common/onyxc_int.h
+++ b/av1/common/onyxc_int.h
@@ -218,7 +218,9 @@
 
   int allow_high_precision_mv;
 
+#if CONFIG_PALETTE
   int allow_screen_content_tools;
+#endif  // CONFIG_PALETTE
 
   // Flag signaling which frame contexts should be reset to default values.
   RESET_FRAME_CONTEXT_MODE reset_frame_context;
diff --git a/av1/common/reconintra.c b/av1/common/reconintra.c
index 719b788..d6bd87d 100644
--- a/av1/common/reconintra.c
+++ b/av1/common/reconintra.c
@@ -1526,6 +1526,7 @@
   const int have_bottom = av1_has_bottom(bsize, mi_row, mi_col, yd > 0, tx_size,
                                          row_off, col_off, pd->subsampling_y);
 
+#if CONFIG_PALETTE
   if (xd->mi[0]->mbmi.palette_mode_info.palette_size[plane != 0] > 0) {
     const int bs = 4 * num_4x4_blocks_wide_txsize_lookup[tx_size];
     const int stride = 4 * (1 << bwl_in);
@@ -1560,6 +1561,7 @@
 #endif  // CONFIG_AOM_HIGHBITDEPTH
     return;
   }
+#endif  // CONFIG_PALETTE
 
 #if CONFIG_AOM_HIGHBITDEPTH
   if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
diff --git a/av1/decoder/decodeframe.c b/av1/decoder/decodeframe.c
index 65a162b..000757f 100644
--- a/av1/decoder/decodeframe.c
+++ b/av1/decoder/decodeframe.c
@@ -1209,10 +1209,12 @@
   }
   if (!is_inter_block(mbmi)) {
     int plane;
+#if CONFIG_PALETTE
     for (plane = 0; plane <= 1; ++plane) {
       if (mbmi->palette_mode_info.palette_size[plane])
         av1_decode_palette_tokens(xd, plane, r);
     }
+#endif  // CONFIG_PALETTE
     for (plane = 0; plane < MAX_MB_PLANE; ++plane) {
       const struct macroblockd_plane *const pd = &xd->plane[plane];
       const TX_SIZE tx_size = plane ? get_uv_tx_size(mbmi, pd) : mbmi->tx_size;
@@ -2785,8 +2787,10 @@
                           &td->bit_reader, pbi->decrypt_cb, pbi->decrypt_state);
 #endif
       av1_init_macroblockd(cm, &td->xd, td->dqcoeff);
+#if CONFIG_PALETTE
       td->xd.plane[0].color_index_map = td->color_index_map[0];
       td->xd.plane[1].color_index_map = td->color_index_map[1];
+#endif  // CONFIG_PALETTE
     }
   }
 
@@ -3095,8 +3099,10 @@
                             pbi->decrypt_state);
 #endif  // CONFIG_ANS
         av1_init_macroblockd(cm, &twd->xd, twd->dqcoeff);
+#if CONFIG_PALETTE
         twd->xd.plane[0].color_index_map = twd->color_index_map[0];
         twd->xd.plane[1].color_index_map = twd->color_index_map[1];
+#endif  // CONFIG_PALETTE
 
         worker->had_error = 0;
         if (i == num_workers - 1 || tile_col == tile_cols_end - 1) {
@@ -3283,10 +3289,14 @@
       memset(&cm->ref_frame_map, -1, sizeof(cm->ref_frame_map));
       pbi->need_resync = 0;
     }
+#if CONFIG_PALETTE
     cm->allow_screen_content_tools = aom_rb_read_bit(rb);
+#endif  // CONFIG_PALETTE
   } else {
     cm->intra_only = cm->show_frame ? 0 : aom_rb_read_bit(rb);
+#if CONFIG_PALETTE
     if (cm->intra_only) cm->allow_screen_content_tools = aom_rb_read_bit(rb);
+#endif  // CONFIG_PALETTE
     if (cm->error_resilient_mode) {
       cm->reset_frame_context = RESET_FRAME_CONTEXT_ALL;
     } else {
diff --git a/av1/decoder/decodemv.c b/av1/decoder/decodemv.c
index 3f9ab23..3ede7e0 100644
--- a/av1/decoder/decodemv.c
+++ b/av1/decoder/decodemv.c
@@ -25,6 +25,7 @@
 
 #include "aom_dsp/aom_dsp_common.h"
 
+#if CONFIG_EXT_INTRA || CONFIG_PALETTE
 static INLINE int read_uniform(aom_reader *r, int n) {
   int l = get_unsigned_bits(n);
   int m = (1 << l) - n;
@@ -37,6 +38,7 @@
   else
     return (v << 1) - m + aom_read_literal(r, 1);
 }
+#endif  // CONFIG_EXT_INTRA || CONFIG_PALETTE
 
 static PREDICTION_MODE read_intra_mode(aom_reader *r, const aom_prob *p) {
   return (PREDICTION_MODE)aom_read_tree(r, av1_intra_mode_tree, p);
@@ -443,6 +445,7 @@
   }
 }
 
+#if CONFIG_PALETTE
 static void read_palette_mode_info(AV1_COMMON *const cm, MACROBLOCKD *const xd,
                                    aom_reader *r) {
   MODE_INFO *const mi = xd->mi[0];
@@ -492,6 +495,7 @@
     }
   }
 }
+#endif  // CONFIG_PALETTE
 
 #if CONFIG_EXT_INTRA
 static void read_ext_intra_mode_info(AV1_COMMON *const cm,
@@ -503,7 +507,11 @@
 #if !ALLOW_FILTER_INTRA_MODES
   return;
 #endif
-  if (mbmi->mode == DC_PRED && mbmi->palette_mode_info.palette_size[0] == 0) {
+  if (mbmi->mode == DC_PRED
+#if CONFIG_PALETTE
+      && mbmi->palette_mode_info.palette_size[0] == 0
+#endif  // CONFIG_PALETTE
+      ) {
     mbmi->ext_intra_mode_info.use_ext_intra_mode[0] =
         aom_read(r, cm->fc->ext_intra_probs[0]);
     if (mbmi->ext_intra_mode_info.use_ext_intra_mode[0]) {
@@ -513,8 +521,11 @@
     if (counts)
       ++counts->ext_intra[0][mbmi->ext_intra_mode_info.use_ext_intra_mode[0]];
   }
-  if (mbmi->uv_mode == DC_PRED &&
-      mbmi->palette_mode_info.palette_size[1] == 0) {
+  if (mbmi->uv_mode == DC_PRED
+#if CONFIG_PALETTE
+      && mbmi->palette_mode_info.palette_size[1] == 0
+#endif  // CONFIG_PALETTE
+      ) {
     mbmi->ext_intra_mode_info.use_ext_intra_mode[1] =
         aom_read(r, cm->fc->ext_intra_probs[1]);
     if (mbmi->ext_intra_mode_info.use_ext_intra_mode[1]) {
@@ -607,10 +618,12 @@
 #if CONFIG_EXT_INTRA
   read_intra_angle_info(cm, xd, r);
 #endif  // CONFIG_EXT_INTRA
+#if CONFIG_PALETTE
   mbmi->palette_mode_info.palette_size[0] = 0;
   mbmi->palette_mode_info.palette_size[1] = 0;
   if (bsize >= BLOCK_8X8 && cm->allow_screen_content_tools)
     read_palette_mode_info(cm, xd, r);
+#endif  // CONFIG_PALETTE
 #if CONFIG_EXT_INTRA
   mbmi->ext_intra_mode_info.use_ext_intra_mode[0] = 0;
   mbmi->ext_intra_mode_info.use_ext_intra_mode[1] = 0;
@@ -895,10 +908,12 @@
 #if CONFIG_EXT_INTRA
   read_intra_angle_info(cm, xd, r);
 #endif  // CONFIG_EXT_INTRA
+#if CONFIG_PALETTE
   mbmi->palette_mode_info.palette_size[0] = 0;
   mbmi->palette_mode_info.palette_size[1] = 0;
   if (bsize >= BLOCK_8X8 && cm->allow_screen_content_tools)
     read_palette_mode_info(cm, xd, r);
+#endif  // CONFIG_PALETTE
 #if CONFIG_EXT_INTRA
   mbmi->ext_intra_mode_info.use_ext_intra_mode[0] = 0;
   mbmi->ext_intra_mode_info.use_ext_intra_mode[1] = 0;
@@ -1187,8 +1202,10 @@
   int16_t mode_ctx = 0;
   MV_REFERENCE_FRAME ref_frame;
 
+#if CONFIG_PALETTE
   mbmi->palette_mode_info.palette_size[0] = 0;
   mbmi->palette_mode_info.palette_size[1] = 0;
+#endif  // CONFIG_PALETTE
 
   read_ref_frames(cm, xd, r, mbmi->segment_id, mbmi->ref_frame);
   is_compound = has_second_ref(mbmi);
diff --git a/av1/decoder/decoder.h b/av1/decoder/decoder.h
index 3900b44..919e7b8 100644
--- a/av1/decoder/decoder.h
+++ b/av1/decoder/decoder.h
@@ -34,7 +34,9 @@
   DECLARE_ALIGNED(16, MACROBLOCKD, xd);
   /* dqcoeff are shared by all the planes. So planes must be decoded serially */
   DECLARE_ALIGNED(16, tran_low_t, dqcoeff[MAX_TX_SQUARE]);
+#if CONFIG_PALETTE
   DECLARE_ALIGNED(16, uint8_t, color_index_map[2][MAX_SB_SQUARE]);
+#endif  // CONFIG_PALETTE
 } TileData;
 
 typedef struct TileWorkerData {
@@ -44,7 +46,9 @@
   DECLARE_ALIGNED(16, MACROBLOCKD, xd);
   /* dqcoeff are shared by all the planes. So planes must be decoded serially */
   DECLARE_ALIGNED(16, tran_low_t, dqcoeff[MAX_TX_SQUARE]);
+#if CONFIG_PALETTE
   DECLARE_ALIGNED(16, uint8_t, color_index_map[2][MAX_SB_SQUARE]);
+#endif  // CONFIG_PALETTE
   struct aom_internal_error_info error_info;
 } TileWorkerData;
 
diff --git a/av1/decoder/detokenize.c b/av1/decoder/detokenize.c
index 9a40f69..22d90db 100644
--- a/av1/decoder/detokenize.c
+++ b/av1/decoder/detokenize.c
@@ -332,6 +332,7 @@
   }
 }
 
+#if CONFIG_PALETTE
 void av1_decode_palette_tokens(MACROBLOCKD *const xd, int plane,
                                aom_reader *r) {
   MODE_INFO *const mi = xd->mi[0];
@@ -360,6 +361,7 @@
     }
   }
 }
+#endif  // CONFIG_PALETTE
 
 int av1_decode_block_tokens(MACROBLOCKD *const xd, int plane,
                             const scan_order *sc, int x, int y, TX_SIZE tx_size,
diff --git a/av1/decoder/detokenize.h b/av1/decoder/detokenize.h
index 69d6fab..dc96cf5 100644
--- a/av1/decoder/detokenize.h
+++ b/av1/decoder/detokenize.h
@@ -22,7 +22,10 @@
 extern "C" {
 #endif
 
+#if CONFIG_PALETTE
 void av1_decode_palette_tokens(MACROBLOCKD *const xd, int plane, aom_reader *r);
+#endif  // CONFIG_PALETTE
+
 int av1_decode_block_tokens(MACROBLOCKD *const xd, int plane,
                             const scan_order *sc, int x, int y, TX_SIZE tx_size,
                             TX_TYPE tx_type,
diff --git a/av1/encoder/bitstream.c b/av1/encoder/bitstream.c
index 80da661..e31fa67 100644
--- a/av1/encoder/bitstream.c
+++ b/av1/encoder/bitstream.c
@@ -84,6 +84,7 @@
       { 53, 6 }, { 54, 6 }, { 55, 6 }, { 0, 1 },  { 7, 3 }
     };
 #endif  // CONFIG_EXT_INTER
+#if CONFIG_PALETTE
 static const struct av1_token palette_size_encodings[] = {
   { 0, 1 }, { 2, 2 }, { 6, 3 }, { 14, 4 }, { 30, 5 }, { 62, 6 }, { 63, 6 },
 };
@@ -115,6 +116,7 @@
         { 126, 7 },
         { 127, 7 } },  // 8 colors
     };
+#endif  // CONFIG_PALETTE
 
 static const struct av1_token tx_size_encodings[TX_SIZES - 1][TX_SIZES] = {
   { { 0, 1 }, { 1, 1 } },                      // Max tx_size is 8X8
@@ -122,6 +124,7 @@
   { { 0, 1 }, { 2, 2 }, { 6, 3 }, { 7, 3 } },  // Max tx_size is 32X32
 };
 
+#if CONFIG_EXT_INTRA || CONFIG_PALETTE
 static INLINE void write_uniform(aom_writer *w, int n, int v) {
   int l = get_unsigned_bits(n);
   int m = (1 << l) - n;
@@ -133,6 +136,7 @@
     aom_write_literal(w, (v - m) & 1, 1);
   }
 }
+#endif  // CONFIG_EXT_INTRA || CONFIG_PALETTE
 
 #if CONFIG_EXT_TX
 static struct av1_token ext_tx_inter_encodings[EXT_TX_SETS_INTER][TX_TYPES];
@@ -580,6 +584,7 @@
 }
 #endif  // CONFIG_EXT_TX
 
+#if CONFIG_PALETTE
 static void pack_palette_tokens(aom_writer *w, const TOKENEXTRA **tp, int n,
                                 int num) {
   int i;
@@ -593,6 +598,7 @@
 
   *tp = p;
 }
+#endif  // CONFIG_PALETTE
 
 #if CONFIG_SUPERTX
 static void update_supertx_probs(AV1_COMMON *cm, aom_writer *w) {
@@ -862,7 +868,11 @@
 #if !ALLOW_FILTER_INTRA_MODES
   return;
 #endif
-  if (mbmi->mode == DC_PRED && mbmi->palette_mode_info.palette_size[0] == 0) {
+  if (mbmi->mode == DC_PRED
+#if CONFIG_PALETTE
+      && mbmi->palette_mode_info.palette_size[0] == 0
+#endif  // CONFIG_PALETTE
+      ) {
     aom_write(w, mbmi->ext_intra_mode_info.use_ext_intra_mode[0],
               cm->fc->ext_intra_probs[0]);
     if (mbmi->ext_intra_mode_info.use_ext_intra_mode[0]) {
@@ -871,8 +881,11 @@
     }
   }
 
-  if (mbmi->uv_mode == DC_PRED &&
-      mbmi->palette_mode_info.palette_size[1] == 0) {
+  if (mbmi->uv_mode == DC_PRED
+#if CONFIG_PALETTE
+      && mbmi->palette_mode_info.palette_size[1] == 0
+#endif  // CONFIG_PALETTE
+      ) {
     aom_write(w, mbmi->ext_intra_mode_info.use_ext_intra_mode[1],
               cm->fc->ext_intra_probs[1]);
     if (mbmi->ext_intra_mode_info.use_ext_intra_mode[1]) {
@@ -959,6 +972,7 @@
   }
 }
 
+#if CONFIG_PALETTE
 static void write_palette_mode_info(const AV1_COMMON *cm, const MACROBLOCKD *xd,
                                     const MODE_INFO *const mi, aom_writer *w) {
   const MB_MODE_INFO *const mbmi = &mi->mbmi;
@@ -1005,6 +1019,7 @@
     }
   }
 }
+#endif  // CONFIG_PALETTE
 
 static void pack_inter_mode_mvs(AV1_COMP *cpi, const MODE_INFO *mi,
 #if CONFIG_SUPERTX
@@ -1116,8 +1131,10 @@
 #if CONFIG_EXT_INTRA
     write_intra_angle_info(cm, xd, w);
 #endif  // CONFIG_EXT_INTRA
+#if CONFIG_PALETTE
     if (bsize >= BLOCK_8X8 && cm->allow_screen_content_tools)
       write_palette_mode_info(cm, xd, mi, w);
+#endif  // CONFIG_PALETTE
 #if CONFIG_EXT_INTRA
     if (bsize >= BLOCK_8X8) write_ext_intra_mode_info(cm, mbmi, w);
 #endif  // CONFIG_EXT_INTRA
@@ -1473,8 +1490,10 @@
 #if CONFIG_EXT_INTRA
   write_intra_angle_info(cm, xd, w);
 #endif  // CONFIG_EXT_INTRA
+#if CONFIG_PALETTE
   if (bsize >= BLOCK_8X8 && cm->allow_screen_content_tools)
     write_palette_mode_info(cm, xd, mi, w);
+#endif  // CONFIG_PALETTE
 #if CONFIG_EXT_INTRA
   if (bsize >= BLOCK_8X8) write_ext_intra_mode_info(cm, mbmi, w);
 #endif  // CONFIG_EXT_INTRA
@@ -1589,6 +1608,7 @@
                         w);
   }
 
+#if CONFIG_PALETTE
   for (plane = 0; plane <= 1; ++plane) {
     if (m->mbmi.palette_mode_info.palette_size[plane] > 0) {
       const int rows = (4 * num_4x4_blocks_high_lookup[m->mbmi.sb_type]) >>
@@ -1601,6 +1621,7 @@
       assert(*tok < tok_end + m->mbmi.skip);
     }
   }
+#endif  // CONFIG_PALETTE
 
 #if CONFIG_SUPERTX
   if (supertx_enabled) return;
@@ -3156,10 +3177,14 @@
     write_sync_code(wb);
     write_bitdepth_colorspace_sampling(cm, wb);
     write_frame_size(cm, wb);
+#if CONFIG_PALETTE
     aom_wb_write_bit(wb, cm->allow_screen_content_tools);
+#endif  // CONFIG_PALETTE
   } else {
     if (!cm->show_frame) aom_wb_write_bit(wb, cm->intra_only);
+#if CONFIG_PALETTE
     if (cm->intra_only) aom_wb_write_bit(wb, cm->allow_screen_content_tools);
+#endif  // CONFIG_PALETTE
     if (!cm->error_resilient_mode) {
       if (cm->intra_only) {
         aom_wb_write_bit(wb,
diff --git a/av1/encoder/block.h b/av1/encoder/block.h
index 8a6e134..3b3f93a 100644
--- a/av1/encoder/block.h
+++ b/av1/encoder/block.h
@@ -66,10 +66,12 @@
 #endif
 } MB_MODE_INFO_EXT;
 
+#if CONFIG_PALETTE
 typedef struct {
   uint8_t best_palette_color_map[MAX_SB_SQUARE];
   float kmeans_data_buf[2 * MAX_SB_SQUARE];
 } PALETTE_BUFFER;
+#endif  // CONFIG_PALETTE
 
 typedef struct macroblock MACROBLOCK;
 struct macroblock {
@@ -128,7 +130,9 @@
   int *nmvsadcost_hp[2];
   int **mvsadcost;
 
+#if CONFIG_PALETTE
   PALETTE_BUFFER *palette_buffer;
+#endif  // CONFIG_PALETTE
 
   // These define limits to motion vector components to prevent them
   // from extending outside the UMV borders
diff --git a/av1/encoder/context_tree.c b/av1/encoder/context_tree.c
index 66e1aca..6d1cb39 100644
--- a/av1/encoder/context_tree.c
+++ b/av1/encoder/context_tree.c
@@ -48,6 +48,7 @@
     }
   }
 
+#if CONFIG_PALETTE
   if (cm->allow_screen_content_tools) {
     for (i = 0; i < 2; ++i) {
       CHECK_MEM_ERROR(
@@ -55,6 +56,7 @@
           aom_memalign(32, num_pix * sizeof(*ctx->color_index_map[i])));
     }
   }
+#endif  // CONFIG_PALETTE
 }
 
 static void free_mode_context(PICK_MODE_CONTEXT *ctx) {
@@ -76,10 +78,12 @@
     }
   }
 
+#if CONFIG_PALETTE
   for (i = 0; i < 2; ++i) {
     aom_free(ctx->color_index_map[i]);
     ctx->color_index_map[i] = 0;
   }
+#endif  // CONFIG_PALETTE
 }
 
 static void alloc_tree_contexts(AV1_COMMON *cm, PC_TREE *tree,
diff --git a/av1/encoder/context_tree.h b/av1/encoder/context_tree.h
index c798cdf..59be673 100644
--- a/av1/encoder/context_tree.h
+++ b/av1/encoder/context_tree.h
@@ -27,7 +27,9 @@
 typedef struct {
   MODE_INFO mic;
   MB_MODE_INFO_EXT mbmi_ext;
+#if CONFIG_PALETTE
   uint8_t *color_index_map[2];
+#endif  // CONFIG_PALETTE
 #if CONFIG_VAR_TX
   uint8_t *blk_skip[MAX_MB_PLANE];
 #endif
diff --git a/av1/encoder/encodeframe.c b/av1/encoder/encodeframe.c
index 2d9a892..653e4c1 100644
--- a/av1/encoder/encodeframe.c
+++ b/av1/encoder/encodeframe.c
@@ -1100,7 +1100,9 @@
     p[i].eobs = ctx->eobs[i][2];
   }
 
+#if CONFIG_PALETTE
   for (i = 0; i < 2; ++i) pd[i].color_index_map = ctx->color_index_map[i];
+#endif  // CONFIG_PALETTE
 
   // Restore the coding context of the MB to that that was in place
   // when the mode was picked for it
@@ -1640,7 +1642,9 @@
     p[i].eobs = ctx->eobs[i][0];
   }
 
+#if CONFIG_PALETTE
   for (i = 0; i < 2; ++i) pd[i].color_index_map = ctx->color_index_map[i];
+#endif  // CONFIG_PALETTE
 
   ctx->is_coded = 0;
   ctx->skippable = 0;
@@ -5031,10 +5035,17 @@
 #if CONFIG_EXT_INTRA
     if (!dry_run && bsize >= BLOCK_8X8) {
       FRAME_COUNTS *counts = td->counts;
-      if (mbmi->mode == DC_PRED && mbmi->palette_mode_info.palette_size[0] == 0)
+      if (mbmi->mode == DC_PRED
+#if CONFIG_PALETTE
+          && mbmi->palette_mode_info.palette_size[0] == 0
+#endif  // CONFIG_PALETTE
+          )
         ++counts->ext_intra[0][mbmi->ext_intra_mode_info.use_ext_intra_mode[0]];
-      if (mbmi->uv_mode == DC_PRED &&
-          mbmi->palette_mode_info.palette_size[1] == 0)
+      if (mbmi->uv_mode == DC_PRED
+#if CONFIG_PALETTE
+          && mbmi->palette_mode_info.palette_size[1] == 0
+#endif  // CONFIG_PALETTE
+          )
         ++counts->ext_intra[1][mbmi->ext_intra_mode_info.use_ext_intra_mode[1]];
       if (mbmi->mode != DC_PRED && mbmi->mode != TM_PRED) {
         int p_angle;
@@ -5047,6 +5058,7 @@
     }
 #endif  // CONFIG_EXT_INTRA
 
+#if CONFIG_PALETTE
     if (bsize >= BLOCK_8X8 && !dry_run) {
       for (plane = 0; plane <= 1; ++plane) {
         if (mbmi->palette_mode_info.palette_size[plane] > 0) {
@@ -5058,6 +5070,7 @@
         }
       }
     }
+#endif  // CONFIG_PALETTE
     av1_tokenize_sb(cpi, td, t, dry_run, AOMMAX(bsize, BLOCK_8X8), rate);
   } else {
     int ref;
diff --git a/av1/encoder/encoder.c b/av1/encoder/encoder.c
index acb5498..82b75dc 100644
--- a/av1/encoder/encoder.c
+++ b/av1/encoder/encoder.c
@@ -481,8 +481,10 @@
   av1_free_pc_tree(&cpi->td);
   av1_free_var_tree(&cpi->td);
 
+#if CONFIG_PALETTE
   if (cpi->common.allow_screen_content_tools)
     aom_free(cpi->td.mb.palette_buffer);
+#endif  // CONFIG_PALETTE
 
   if (cpi->source_diff_var != NULL) {
     aom_free(cpi->source_diff_var);
@@ -1941,6 +1943,7 @@
           : REFRESH_FRAME_CONTEXT_BACKWARD;
   cm->reset_frame_context = RESET_FRAME_CONTEXT_NONE;
 
+#if CONFIG_PALETTE
   cm->allow_screen_content_tools = (cpi->oxcf.content == AOM_CONTENT_SCREEN);
   if (cm->allow_screen_content_tools) {
     MACROBLOCK *x = &cpi->td.mb;
@@ -1953,6 +1956,7 @@
     av1_free_pc_tree(&cpi->td);
     av1_setup_pc_tree(&cpi->common, &cpi->td);
   }
+#endif  // CONFIG_PALETTE
 
   av1_reset_segment_features(cm);
   av1_set_high_precision_mv(cpi, 0);
@@ -2561,8 +2565,10 @@
 
     // Deallocate allocated thread data.
     if (t < cpi->num_workers - 1) {
+#if CONFIG_PALETTE
       if (cpi->common.allow_screen_content_tools)
         aom_free(thread_data->td->mb.palette_buffer);
+#endif  // CONFIG_PALETTE
       aom_free(thread_data->td->counts);
       av1_free_pc_tree(thread_data->td);
       av1_free_var_tree(thread_data->td);
diff --git a/av1/encoder/encoder.h b/av1/encoder/encoder.h
index f854874..a99a5cb 100644
--- a/av1/encoder/encoder.h
+++ b/av1/encoder/encoder.h
@@ -554,12 +554,14 @@
 #else
   int partition_cost[PARTITION_CONTEXTS][PARTITION_TYPES];
 #endif
+#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]
                           [PALETTE_COLORS];
   int palette_uv_color_cost[PALETTE_MAX_SIZE - 1][PALETTE_COLOR_CONTEXTS]
                            [PALETTE_COLORS];
+#endif  // CONFIG_PALETTE
   int tx_size_cost[TX_SIZES - 1][TX_SIZE_CONTEXTS][TX_SIZES];
 #if CONFIG_EXT_TX
   int inter_tx_type_costs[EXT_TX_SETS_INTER][EXT_TX_SIZES][TX_TYPES];
diff --git a/av1/encoder/ethread.c b/av1/encoder/ethread.c
index d4c0a7a..117d0ed 100644
--- a/av1/encoder/ethread.c
+++ b/av1/encoder/ethread.c
@@ -130,12 +130,14 @@
              sizeof(cpi->common.counts));
     }
 
+#if CONFIG_PALETTE
     // Allocate buffers used by palette coding mode.
     if (cpi->common.allow_screen_content_tools && i < num_workers - 1) {
       MACROBLOCK *x = &thread_data->td->mb;
       CHECK_MEM_ERROR(cm, x->palette_buffer,
                       aom_memalign(16, sizeof(*x->palette_buffer)));
     }
+#endif  // CONFIG_PALETTE
   }
 
   // Encode a frame
diff --git a/av1/encoder/rd.c b/av1/encoder/rd.c
index ff96714..2b9171f 100644
--- a/av1/encoder/rd.c
+++ b/av1/encoder/rd.c
@@ -90,6 +90,7 @@
     av1_cost_tokens(cpi->switchable_interp_costs[i],
                     fc->switchable_interp_prob[i], av1_switchable_interp_tree);
 
+#if CONFIG_PALETTE
   for (i = 0; i < PALETTE_BLOCK_SIZES; ++i) {
     av1_cost_tokens(cpi->palette_y_size_cost[i],
                     av1_default_palette_y_size_prob[i], av1_palette_size_tree);
@@ -97,7 +98,7 @@
                     av1_default_palette_uv_size_prob[i], av1_palette_size_tree);
   }
 
-  for (i = 0; i < PALETTE_MAX_SIZE - 1; ++i)
+  for (i = 0; i < PALETTE_MAX_SIZE - 1; ++i) {
     for (j = 0; j < PALETTE_COLOR_CONTEXTS; ++j) {
       av1_cost_tokens(cpi->palette_y_color_cost[i][j],
                       av1_default_palette_y_color_prob[i][j],
@@ -106,6 +107,8 @@
                       av1_default_palette_uv_color_prob[i][j],
                       av1_palette_color_tree[i]);
     }
+  }
+#endif  // CONFIG_PALETTE
 
   for (i = 0; i < TX_SIZES - 1; ++i)
     for (j = 0; j < TX_SIZE_CONTEXTS; ++j)
diff --git a/av1/encoder/rdopt.c b/av1/encoder/rdopt.c
index 8707061..e594a32 100644
--- a/av1/encoder/rdopt.c
+++ b/av1/encoder/rdopt.c
@@ -41,7 +41,9 @@
 #include "av1/encoder/encoder.h"
 #include "av1/encoder/hybrid_fwd_txfm.h"
 #include "av1/encoder/mcomp.h"
+#if CONFIG_PALETTE
 #include "av1/encoder/palette.h"
+#endif  // CONFIG_PALETTE
 #include "av1/encoder/quantize.h"
 #include "av1/encoder/ratectrl.h"
 #include "av1/encoder/rd.h"
@@ -415,6 +417,7 @@
   { { INTRA_FRAME, NONE } },
 };
 
+#if CONFIG_EXT_INTRA || CONFIG_PALETTE
 static INLINE int write_uniform_cost(int n, int v) {
   int l = get_unsigned_bits(n), m = (1 << l) - n;
   if (l == 0) return 0;
@@ -423,6 +426,7 @@
   else
     return l * av1_cost_bit(128, 0);
 }
+#endif  // CONFIG_EXT_INTRA || CONFIG_PALETTE
 
 // constants for prune 1 and prune 2 decision boundaries
 #define FAST_EXT_TX_CORR_MID 0.0
@@ -1727,6 +1731,7 @@
   return 0;
 }
 
+#if CONFIG_PALETTE
 static int rd_pick_palette_intra_sby(
     AV1_COMP *cpi, MACROBLOCK *x, BLOCK_SIZE bsize, int palette_ctx,
     int dc_mode_cost, PALETTE_MODE_INFO *palette_mode_info,
@@ -1871,6 +1876,7 @@
   }
   return rate_overhead;
 }
+#endif  // CONFIG_PALETTE
 
 static int64_t rd_pick_intra4x4block(AV1_COMP *cpi, MACROBLOCK *x, int row,
                                      int col, PREDICTION_MODE *best_mode,
@@ -1902,7 +1908,9 @@
   memcpy(ta, a, num_4x4_blocks_wide * sizeof(a[0]));
   memcpy(tl, l, num_4x4_blocks_high * sizeof(l[0]));
   xd->mi[0]->mbmi.tx_size = TX_4X4;
+#if CONFIG_PALETTE
   xd->mi[0]->mbmi.palette_mode_info.palette_size[0] = 0;
+#endif  // CONFIG_PALETTE
 
 #if CONFIG_AOM_HIGHBITDEPTH
   if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
@@ -2258,7 +2266,9 @@
   av1_zero(ext_intra_mode_info);
   mbmi->ext_intra_mode_info.use_ext_intra_mode[0] = 1;
   mbmi->mode = DC_PRED;
+#if CONFIG_PALETTE
   mbmi->palette_mode_info.palette_size[0] = 0;
+#endif  // CONFIG_PALETTE
 
   for (mode = 0; mode < FILTER_INTRA_MODES; ++mode) {
     if (skip_mask & (1 << mode)) continue;
@@ -2599,6 +2609,10 @@
   int this_rate, this_rate_tokenonly, s;
   int64_t this_distortion, this_rd;
   TX_SIZE best_tx = TX_4X4;
+#if CONFIG_EXT_INTRA || CONFIG_PALETTE
+  const int rows = 4 * num_4x4_blocks_high_lookup[bsize];
+  const int cols = 4 * num_4x4_blocks_wide_lookup[bsize];
+#endif  // CONFIG_EXT_INTRA || CONFIG_PALETTE
 #if CONFIG_EXT_INTRA
   const int intra_filter_ctx = av1_get_pred_context_intra_interp(xd);
   EXT_INTRA_MODE_INFO ext_intra_mode_info;
@@ -2612,15 +2626,15 @@
 #endif  // CONFIG_EXT_INTRA
   TX_TYPE best_tx_type = DCT_DCT;
   int *bmode_costs;
+#if CONFIG_PALETTE
   PALETTE_MODE_INFO palette_mode_info;
   PALETTE_MODE_INFO *const pmi = &mic->mbmi.palette_mode_info;
   uint8_t *best_palette_color_map =
       cpi->common.allow_screen_content_tools
           ? x->palette_buffer->best_palette_color_map
           : NULL;
-  const int rows = 4 * num_4x4_blocks_high_lookup[bsize];
-  const int cols = 4 * num_4x4_blocks_wide_lookup[bsize];
   int palette_ctx = 0;
+#endif  // CONFIG_PALETTE
   const MODE_INFO *above_mi = xd->above_mi;
   const MODE_INFO *left_mi = xd->left_mi;
   const PREDICTION_MODE A = av1_above_block_mode(mic, above_mi, 0);
@@ -2643,12 +2657,14 @@
 #endif
     angle_estimation(src, src_stride, rows, cols, directional_mode_skip_mask);
 #endif  // CONFIG_EXT_INTRA
+#if CONFIG_PALETTE
   palette_mode_info.palette_size[0] = 0;
   pmi->palette_size[0] = 0;
   if (above_mi)
     palette_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);
+#endif  // CONFIG_PALETTE
 
   if (cpi->sf.tx_type_search.fast_intra_tx_type_search)
     x->use_default_intra_tx_type = 1;
@@ -2699,9 +2715,11 @@
           cpi->tx_size_cost[max_tx_size - TX_8X8][get_tx_size_context(xd)]
                            [mic->mbmi.tx_size];
     }
+#if CONFIG_PALETTE
     if (cpi->common.allow_screen_content_tools && mic->mbmi.mode == DC_PRED)
       this_rate += av1_cost_bit(
           av1_default_palette_y_mode_prob[bsize - BLOCK_8X8][palette_ctx], 0);
+#endif  // CONFIG_PALETTE
 #if CONFIG_EXT_INTRA
     if (mic->mbmi.mode == DC_PRED && ALLOW_FILTER_INTRA_MODES)
       this_rate += av1_cost_bit(cpi->common.fc->ext_intra_probs[0], 0);
@@ -2741,11 +2759,13 @@
     }
   }
 
+#if CONFIG_PALETTE
   if (cpi->common.allow_screen_content_tools)
     rd_pick_palette_intra_sby(cpi, x, bsize, palette_ctx, bmode_costs[DC_PRED],
                               &palette_mode_info, best_palette_color_map,
                               &best_tx, &best_tx_type, &mode_selected,
                               &best_rd);
+#endif  // CONFIG_PALETTE
 
 #if CONFIG_EXT_INTRA
   if (ALLOW_FILTER_INTRA_MODES && beat_best_rd) {
@@ -2764,7 +2784,9 @@
   if (ext_intra_mode_info.use_ext_intra_mode[0]) {
     mic->mbmi.ext_intra_mode_info.ext_intra_mode[0] =
         ext_intra_mode_info.ext_intra_mode[0];
+#if CONFIG_PALETTE
     palette_mode_info.palette_size[0] = 0;
+#endif  // CONFIG_PALETTE
   }
 #endif  // CONFIG_EXT_INTRA
 
@@ -2775,6 +2797,7 @@
   mic->mbmi.intra_filter = best_filter;
 #endif  // CONFIG_EXT_INTRA
   mic->mbmi.tx_type = best_tx_type;
+#if CONFIG_PALETTE
   pmi->palette_size[0] = palette_mode_info.palette_size[0];
   if (palette_mode_info.palette_size[0] > 0) {
     memcpy(pmi->palette_colors, palette_mode_info.palette_colors,
@@ -2782,6 +2805,7 @@
     memcpy(xd->plane[0].color_index_map, best_palette_color_map,
            rows * cols * sizeof(best_palette_color_map[0]));
   }
+#endif  // CONFIG_PALETTE
 
   return best_rd;
 }
@@ -3565,6 +3589,7 @@
 }
 #endif  // CONFIG_VAR_TX
 
+#if CONFIG_PALETTE
 static void rd_pick_palette_intra_sbuv(
     AV1_COMP *cpi, MACROBLOCK *x, int dc_mode_cost,
     PALETTE_MODE_INFO *palette_mode_info, uint8_t *best_palette_color_map,
@@ -3729,6 +3754,7 @@
     }
   }
 }
+#endif  // CONFIG_PALETTE
 
 #if CONFIG_EXT_INTRA
 // Return 1 if an ext intra mode is selected; return 0 otherwise.
@@ -3747,7 +3773,9 @@
   av1_zero(ext_intra_mode_info);
   mbmi->ext_intra_mode_info.use_ext_intra_mode[1] = 1;
   mbmi->uv_mode = DC_PRED;
+#if CONFIG_PALETTE
   mbmi->palette_mode_info.palette_size[1] = 0;
+#endif  // CONFIG_PALETTE
 
   for (mode = 0; mode < FILTER_INTRA_MODES; ++mode) {
     mbmi->ext_intra_mode_info.ext_intra_mode[1] = mode;
@@ -3890,6 +3918,7 @@
   int64_t best_rd = INT64_MAX, this_rd;
   int this_rate_tokenonly, this_rate, s;
   int64_t this_distortion, this_sse;
+#if CONFIG_PALETTE
   const int rows =
       (4 * num_4x4_blocks_high_lookup[bsize]) >> (xd->plane[1].subsampling_y);
   const int cols =
@@ -3897,6 +3926,7 @@
   PALETTE_MODE_INFO palette_mode_info;
   PALETTE_MODE_INFO *const pmi = &xd->mi[0]->mbmi.palette_mode_info;
   uint8_t *best_palette_color_map = NULL;
+#endif  // CONFIG_PALETTE
 #if CONFIG_EXT_INTRA
   int is_directional_mode, rate_overhead, best_angle_delta = 0;
   EXT_INTRA_MODE_INFO ext_intra_mode_info;
@@ -3904,8 +3934,10 @@
   ext_intra_mode_info.use_ext_intra_mode[1] = 0;
   mbmi->ext_intra_mode_info.use_ext_intra_mode[1] = 0;
 #endif  // CONFIG_EXT_INTRA
+#if CONFIG_PALETTE
   palette_mode_info.palette_size[1] = 0;
   pmi->palette_size[1] = 0;
+#endif  // CONFIG_PALETTE
   for (mode = DC_PRED; mode <= TM_PRED; ++mode) {
     if (!(cpi->sf.intra_uv_mode_mask[max_tx_size] & (1 << mode))) continue;
 
@@ -3938,10 +3970,12 @@
       continue;
     this_rate = this_rate_tokenonly + cpi->intra_uv_mode_cost[mbmi->mode][mode];
 #endif  // CONFIG_EXT_INTRA
+#if CONFIG_PALETTE
     if (cpi->common.allow_screen_content_tools && mbmi->sb_type >= BLOCK_8X8 &&
         mode == DC_PRED)
       this_rate += av1_cost_bit(
           av1_default_palette_uv_mode_prob[pmi->palette_size[0] > 0], 0);
+#endif  // CONFIG_PALETTE
 
     this_rd = RDCOST(x->rdmult, x->rddiv, this_rate, this_distortion);
 
@@ -3958,6 +3992,7 @@
     }
   }
 
+#if CONFIG_PALETTE
   if (cpi->common.allow_screen_content_tools && mbmi->sb_type >= BLOCK_8X8) {
     best_palette_color_map = x->palette_buffer->best_palette_color_map;
     rd_pick_palette_intra_sbuv(
@@ -3965,6 +4000,7 @@
         &palette_mode_info, best_palette_color_map, &mode_selected, &best_rd,
         rate, rate_tokenonly, distortion, skippable);
   }
+#endif  // CONFIG_PALETTE
 
 #if CONFIG_EXT_INTRA
   if (mbmi->sb_type >= BLOCK_8X8 && ALLOW_FILTER_INTRA_MODES) {
@@ -3980,11 +4016,14 @@
   if (ext_intra_mode_info.use_ext_intra_mode[1]) {
     mbmi->ext_intra_mode_info.ext_intra_mode[1] =
         ext_intra_mode_info.ext_intra_mode[1];
+#if CONFIG_PALETTE
     palette_mode_info.palette_size[1] = 0;
+#endif  // CONFIG_PALETTE
   }
   mbmi->angle_delta[1] = best_angle_delta;
 #endif  // CONFIG_EXT_INTRA
   mbmi->uv_mode = mode_selected;
+#if CONFIG_PALETTE
   pmi->palette_size[1] = palette_mode_info.palette_size[1];
   if (palette_mode_info.palette_size[1] > 0) {
     memcpy(pmi->palette_colors + PALETTE_MAX_SIZE,
@@ -3993,6 +4032,7 @@
     memcpy(xd->plane[1].color_index_map, best_palette_color_map,
            rows * cols * sizeof(best_palette_color_map[0]));
   }
+#endif  // CONFIG_PALETTE
 
   return best_rd;
 }
@@ -7972,6 +8012,7 @@
          av1_active_v_edge(cpi, mi_col, cpi->common.mib_size);
 }
 
+#if CONFIG_PALETTE
 static void restore_uv_color_map(AV1_COMP *cpi, MACROBLOCK *x) {
   MACROBLOCKD *const xd = &x->e_mbd;
   MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi;
@@ -8019,13 +8060,17 @@
   av1_calc_indices(data, centroids, color_map, rows * cols,
                    pmi->palette_size[1], 2);
 }
+#endif  // CONFIG_PALETTE
 
 #if CONFIG_EXT_INTRA
 static void pick_ext_intra_interframe(
     AV1_COMP *cpi, MACROBLOCK *x, PICK_MODE_CONTEXT *ctx, BLOCK_SIZE bsize,
     int *rate_uv_intra, int *rate_uv_tokenonly, int64_t *dist_uv, int *skip_uv,
     PREDICTION_MODE *mode_uv, EXT_INTRA_MODE_INFO *ext_intra_mode_info_uv,
-    PALETTE_MODE_INFO *pmi_uv, int8_t *uv_angle_delta, int palette_ctx,
+    int8_t *uv_angle_delta,
+#if CONFIG_PALETTE
+    PALETTE_MODE_INFO *pmi_uv, int palette_ctx,
+#endif  // CONFIG_PALETTE
     int skip_mask, unsigned int *ref_costs_single, int64_t *best_rd,
     int64_t *best_intra_rd, PREDICTION_MODE *best_intra_mode,
     int *best_mode_index, int *best_skip2, int *best_mode_skippable,
@@ -8036,7 +8081,9 @@
   AV1_COMMON *const cm = &cpi->common;
   MACROBLOCKD *const xd = &x->e_mbd;
   MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi;
+#if CONFIG_PALETTE
   PALETTE_MODE_INFO *const pmi = &mbmi->palette_mode_info;
+#endif  // CONFIG_PALETTE
   const TX_SIZE max_tx_size = max_txsize_lookup[bsize];
   int rate2 = 0, rate_y = INT_MAX, skippable = 0, rate_uv, rate_dummy, i;
   int dc_mode_index;
@@ -8069,7 +8116,9 @@
     choose_intra_uv_mode(cpi, x, ctx, bsize, uv_tx, &rate_uv_intra[uv_tx],
                          &rate_uv_tokenonly[uv_tx], &dist_uv[uv_tx],
                          &skip_uv[uv_tx], &mode_uv[uv_tx]);
+#if CONFIG_PALETTE
     if (cm->allow_screen_content_tools) pmi_uv[uv_tx] = *pmi;
+#endif  // CONFIG_PALETTE
     ext_intra_mode_info_uv[uv_tx] = mbmi->ext_intra_mode_info;
     uv_angle_delta[uv_tx] = mbmi->angle_delta[1];
   }
@@ -8078,12 +8127,14 @@
   distortion_uv = dist_uv[uv_tx];
   skippable = skippable && skip_uv[uv_tx];
   mbmi->uv_mode = mode_uv[uv_tx];
+#if CONFIG_PALETTE
   if (cm->allow_screen_content_tools) {
     pmi->palette_size[1] = pmi_uv[uv_tx].palette_size[1];
     memcpy(pmi->palette_colors + PALETTE_MAX_SIZE,
            pmi_uv[uv_tx].palette_colors + PALETTE_MAX_SIZE,
            2 * PALETTE_MAX_SIZE * sizeof(pmi->palette_colors[0]));
   }
+#endif  // CONFIG_PALETTE
   mbmi->angle_delta[1] = uv_angle_delta[uv_tx];
   mbmi->ext_intra_mode_info.use_ext_intra_mode[1] =
       ext_intra_mode_info_uv[uv_tx].use_ext_intra_mode[1];
@@ -8094,9 +8145,11 @@
 
   rate2 = rate_y + intra_mode_cost[mbmi->mode] + rate_uv +
           cpi->intra_uv_mode_cost[mbmi->mode][mbmi->uv_mode];
+#if CONFIG_PALETTE
   if (cpi->common.allow_screen_content_tools && mbmi->mode == DC_PRED)
     rate2 += av1_cost_bit(
         av1_default_palette_y_mode_prob[bsize - BLOCK_8X8][palette_ctx], 0);
+#endif  // CONFIG_PALETTE
 
   if (!xd->lossless[mbmi->segment_id]) {
     // super_block_yrd above includes the cost of the tx_size in the
@@ -8202,7 +8255,9 @@
   SPEED_FEATURES *const sf = &cpi->sf;
   MACROBLOCKD *const xd = &x->e_mbd;
   MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi;
+#if CONFIG_PALETTE
   PALETTE_MODE_INFO *const pmi = &mbmi->palette_mode_info;
+#endif  // CONFIG_PALETTE
   MB_MODE_INFO_EXT *const mbmi_ext = x->mbmi_ext;
   const struct segmentation *const seg = &cm->seg;
   PREDICTION_MODE this_mode;
@@ -8254,7 +8309,9 @@
   int64_t dist_uv[TX_SIZES];
   int skip_uv[TX_SIZES];
   PREDICTION_MODE mode_uv[TX_SIZES];
+#if CONFIG_PALETTE
   PALETTE_MODE_INFO pmi_uv[TX_SIZES];
+#endif  // CONFIG_PALETTE
 #if CONFIG_EXT_INTRA
   EXT_INTRA_MODE_INFO ext_intra_mode_info_uv[TX_SIZES];
   int8_t uv_angle_delta[TX_SIZES], dc_skipped = 1;
@@ -8281,11 +8338,15 @@
   int *mode_map = tile_data->mode_map[bsize];
   const int mode_search_skip_flags = sf->mode_search_skip_flags;
   const TX_SIZE max_tx_size = max_txsize_lookup[bsize];
-  int palette_ctx = 0;
+#if CONFIG_PALETTE || CONFIG_EXT_INTRA
   const int rows = 4 * num_4x4_blocks_high_lookup[bsize];
   const int cols = 4 * num_4x4_blocks_wide_lookup[bsize];
+#endif  // CONFIG_PALETTE || CONFIG_EXT_INTRA
+#if CONFIG_PALETTE
+  int palette_ctx = 0;
   const MODE_INFO *above_mi = xd->above_mi;
   const MODE_INFO *left_mi = xd->left_mi;
+#endif  // CONFIG_PALETTE
 #if CONFIG_MOTION_VAR
 #if CONFIG_AOM_HIGHBITDEPTH
   DECLARE_ALIGNED(16, uint8_t, tmp_buf1[2 * MAX_MB_PLANE * MAX_SB_SQUARE]);
@@ -8327,14 +8388,16 @@
 #endif  // CONFIG_MOTION_VAR
 
   av1_zero(best_mbmode);
-  av1_zero(pmi_uv);
 
+#if CONFIG_PALETTE
+  av1_zero(pmi_uv);
   if (cm->allow_screen_content_tools) {
     if (above_mi)
       palette_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);
   }
+#endif  // CONFIG_PALETTE
 
 #if CONFIG_EXT_INTRA
   memset(directional_mode_skip_mask, 0,
@@ -8713,8 +8776,10 @@
     mbmi->uv_mode = DC_PRED;
     mbmi->ref_frame[0] = ref_frame;
     mbmi->ref_frame[1] = second_ref_frame;
+#if CONFIG_PALETTE
     pmi->palette_size[0] = 0;
     pmi->palette_size[1] = 0;
+#endif  // CONFIG_PALETTE
 #if CONFIG_EXT_INTRA
     mbmi->ext_intra_mode_info.use_ext_intra_mode[0] = 0;
     mbmi->ext_intra_mode_info.use_ext_intra_mode[1] = 0;
@@ -8756,8 +8821,6 @@
         if (!angle_stats_ready) {
           const int src_stride = x->plane[0].src.stride;
           const uint8_t *src = x->plane[0].src.buf;
-          const int rows = 4 * num_4x4_blocks_high_lookup[bsize];
-          const int cols = 4 * num_4x4_blocks_wide_lookup[bsize];
 #if CONFIG_AOM_HIGHBITDEPTH
           if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH)
             highbd_angle_estimation(src, src_stride, rows, cols,
@@ -8797,7 +8860,10 @@
         choose_intra_uv_mode(cpi, x, ctx, bsize, uv_tx, &rate_uv_intra[uv_tx],
                              &rate_uv_tokenonly[uv_tx], &dist_uv[uv_tx],
                              &skip_uv[uv_tx], &mode_uv[uv_tx]);
+#if CONFIG_PALETTE
         if (cm->allow_screen_content_tools) pmi_uv[uv_tx] = *pmi;
+#endif  // CONFIG_PALETTE
+
 #if CONFIG_EXT_INTRA
         ext_intra_mode_info_uv[uv_tx] = mbmi->ext_intra_mode_info;
         uv_angle_delta[uv_tx] = mbmi->angle_delta[1];
@@ -8808,12 +8874,15 @@
       distortion_uv = dist_uv[uv_tx];
       skippable = skippable && skip_uv[uv_tx];
       mbmi->uv_mode = mode_uv[uv_tx];
+#if CONFIG_PALETTE
       if (cm->allow_screen_content_tools) {
         pmi->palette_size[1] = pmi_uv[uv_tx].palette_size[1];
         memcpy(pmi->palette_colors + PALETTE_MAX_SIZE,
                pmi_uv[uv_tx].palette_colors + PALETTE_MAX_SIZE,
                2 * PALETTE_MAX_SIZE * sizeof(pmi->palette_colors[0]));
       }
+#endif  // CONFIG_PALETTE
+
 #if CONFIG_EXT_INTRA
       mbmi->angle_delta[1] = uv_angle_delta[uv_tx];
       mbmi->ext_intra_mode_info.use_ext_intra_mode[1] =
@@ -8826,9 +8895,11 @@
 
       rate2 = rate_y + intra_mode_cost[mbmi->mode] + rate_uv +
               cpi->intra_uv_mode_cost[mbmi->mode][mbmi->uv_mode];
+#if CONFIG_PALETTE
       if (cpi->common.allow_screen_content_tools && mbmi->mode == DC_PRED)
         rate2 += av1_cost_bit(
             av1_default_palette_y_mode_prob[bsize - BLOCK_8X8][palette_ctx], 0);
+#endif  // CONFIG_PALETTE
 
       if (!xd->lossless[mbmi->segment_id]) {
         // super_block_yrd above includes the cost of the tx_size in the
@@ -9421,6 +9492,7 @@
     }
   }
 
+#if CONFIG_PALETTE
   // Only try palette mode when the best mode so far is an intra mode.
   if (cm->allow_screen_content_tools && !is_inter_mode(best_mbmode.mode)) {
     PREDICTION_MODE mode_selected;
@@ -9519,17 +9591,24 @@
     }
   }
 PALETTE_EXIT:
+#endif  // CONFIG_PALETTE
 
 #if CONFIG_EXT_INTRA
   // TODO(huisu): ext-intra is turned off in lossless mode for now to
   // avoid a unit test failure
   if (!xd->lossless[mbmi->segment_id] && ALLOW_FILTER_INTRA_MODES &&
-      mbmi->palette_mode_info.palette_size[0] == 0 && !dc_skipped &&
-      best_mode_index >= 0 && best_intra_rd < (best_rd + (best_rd >> 3))) {
+#if CONFIG_PALETTE
+      mbmi->palette_mode_info.palette_size[0] == 0 &&
+#endif  // CONFIG_PALETTE
+      !dc_skipped && best_mode_index >= 0 &&
+      best_intra_rd < (best_rd + (best_rd >> 3))) {
     pick_ext_intra_interframe(
         cpi, x, ctx, bsize, rate_uv_intra, rate_uv_tokenonly, dist_uv, skip_uv,
-        mode_uv, ext_intra_mode_info_uv, pmi_uv, uv_angle_delta, palette_ctx, 0,
-        ref_costs_single, &best_rd, &best_intra_rd, &best_intra_mode,
+        mode_uv, ext_intra_mode_info_uv, uv_angle_delta,
+#if CONFIG_PALETTE
+        pmi_uv, palette_ctx,
+#endif  // CONFIG_PALETTE
+        0, ref_costs_single, &best_rd, &best_intra_rd, &best_intra_mode,
         &best_mode_index, &best_skip2, &best_mode_skippable,
 #if CONFIG_SUPERTX
         returnrate_nocoef,
@@ -9781,9 +9860,11 @@
   store_coding_context(x, ctx, best_mode_index, best_pred_diff,
                        best_mode_skippable);
 
+#if CONFIG_PALETTE
   if (cm->allow_screen_content_tools && pmi->palette_size[1] > 0) {
     restore_uv_color_map(cpi, x);
   }
+#endif  // CONFIG_PALETTE
 }
 
 void av1_rd_pick_inter_mode_sb_seg_skip(AV1_COMP *cpi, TileDataEnc *tile_data,
@@ -9817,8 +9898,11 @@
 
   assert(segfeature_active(&cm->seg, segment_id, SEG_LVL_SKIP));
 
+#if CONFIG_PALETTE
   mbmi->palette_mode_info.palette_size[0] = 0;
   mbmi->palette_mode_info.palette_size[1] = 0;
+#endif  // CONFIG_PALETTE
+
 #if CONFIG_EXT_INTRA
   mbmi->ext_intra_mode_info.use_ext_intra_mode[0] = 0;
   mbmi->ext_intra_mode_info.use_ext_intra_mode[1] = 0;
@@ -10043,8 +10127,10 @@
     frame_mv[ZEROMV][ref_frame].as_int = 0;
   }
 
+#if CONFIG_PALETTE
   mbmi->palette_mode_info.palette_size[0] = 0;
   mbmi->palette_mode_info.palette_size[1] = 0;
+#endif  // CONFIG_PALETTE
 
   for (ref_index = 0; ref_index < MAX_REFS; ++ref_index) {
     int mode_excluded = 0;
diff --git a/av1/encoder/tokenize.c b/av1/encoder/tokenize.c
index 8095681..aa67f6c 100644
--- a/av1/encoder/tokenize.c
+++ b/av1/encoder/tokenize.c
@@ -408,6 +408,7 @@
   return segfeature_active(seg, segment_id, SEG_LVL_SKIP) ? 0 : eob_max;
 }
 
+#if CONFIG_PALETTE
 void av1_tokenize_palette_sb(AV1_COMP *cpi, struct ThreadData *const td,
                              int plane, TOKENEXTRA **t, RUN_TYPE dry_run,
                              BLOCK_SIZE bsize, int *rate) {
@@ -448,6 +449,7 @@
   }
   if (rate) *rate += this_rate;
 }
+#endif  // CONFIG_PALETTE
 
 static void tokenize_b(int plane, int block, int blk_row, int blk_col,
                        BLOCK_SIZE plane_bsize, TX_SIZE tx_size, void *arg) {
diff --git a/av1/encoder/tokenize.h b/av1/encoder/tokenize.h
index f20848a..7f1bc74 100644
--- a/av1/encoder/tokenize.h
+++ b/av1/encoder/tokenize.h
@@ -70,10 +70,11 @@
                            TOKENEXTRA **t, RUN_TYPE dry_run, int mi_row,
                            int mi_col, BLOCK_SIZE bsize, int *rate);
 #endif
-
+#if CONFIG_PALETTE
 void av1_tokenize_palette_sb(struct AV1_COMP *cpi, struct ThreadData *const td,
                              int plane, TOKENEXTRA **t, RUN_TYPE dry_run,
                              BLOCK_SIZE bsize, int *rate);
+#endif  // CONFIG_PALETTE
 void av1_tokenize_sb(struct AV1_COMP *cpi, struct ThreadData *td,
                      TOKENEXTRA **t, RUN_TYPE dry_run, BLOCK_SIZE bsize,
                      int *rate);
diff --git a/configure b/configure
index 0e33876..e84ccca 100755
--- a/configure
+++ b/configure
@@ -278,6 +278,7 @@
     bidir_pred
     bitstream_debug
     alt_intra
+    palette
 "
 CONFIG_LIST="
     dependency_tracking
diff --git a/test/end_to_end_test.cc b/test/end_to_end_test.cc
index ce44feb..a88106f 100644
--- a/test/end_to_end_test.cc
+++ b/test/end_to_end_test.cc
@@ -129,11 +129,13 @@
       encoder->Control(AV1E_SET_FRAME_PARALLEL_DECODING, 1);
       encoder->Control(AV1E_SET_TILE_COLUMNS, 4);
       encoder->Control(AOME_SET_CPUUSED, cpu_used_);
+#if CONFIG_PALETTE
       // Test screen coding tools at cpu_used = 1 && encoding mode is two-pass.
       if (cpu_used_ == 1 && encoding_mode_ == ::libaom_test::kTwoPassGood)
         encoder->Control(AV1E_SET_TUNE_CONTENT, AOM_CONTENT_SCREEN);
       else
         encoder->Control(AV1E_SET_TUNE_CONTENT, AOM_CONTENT_DEFAULT);
+#endif  // CONFIG_PALETTE
       if (encoding_mode_ != ::libaom_test::kRealTime) {
         encoder->Control(AOME_SET_ENABLEAUTOALTREF, 1);
         encoder->Control(AOME_SET_ARNR_MAXFRAMES, 7);