Change q_segmentation to spatial_segmentation

Adds spatial prediction to standard segmentation instead of a separate
segmentation feature.
When using temporal prediction skipped blocks are flagged as mispredicted.

Change-Id: I0c32281286d3fbce66d339c9247bcc6516f37a63
diff --git a/av1/common/alloccommon.c b/av1/common/alloccommon.c
index 00ff268..b60fc83 100644
--- a/av1/common/alloccommon.c
+++ b/av1/common/alloccommon.c
@@ -53,11 +53,6 @@
   int i;
   int seg_map_size = rows * cols;
 
-#if CONFIG_Q_SEGMENTATION
-  cm->q_seg_map = (uint8_t *)aom_calloc(seg_map_size, 1);
-  if (!cm->q_seg_map) return 1;
-#endif
-
   for (i = 0; i < NUM_PING_PONG_BUFFERS; ++i) {
     cm->seg_map_array[i] = (uint8_t *)aom_calloc(seg_map_size, 1);
     if (cm->seg_map_array[i] == NULL) return 1;
@@ -78,11 +73,6 @@
 static void free_seg_map(AV1_COMMON *cm) {
   int i;
 
-#if CONFIG_Q_SEGMENTATION
-  aom_free(cm->q_seg_map);
-  cm->q_seg_map = NULL;
-#endif
-
   for (i = 0; i < NUM_PING_PONG_BUFFERS; ++i) {
     aom_free(cm->seg_map_array[i]);
     cm->seg_map_array[i] = NULL;
diff --git a/av1/common/blockd.h b/av1/common/blockd.h
index 54bf921..1c41637 100644
--- a/av1/common/blockd.h
+++ b/av1/common/blockd.h
@@ -278,9 +278,6 @@
   int8_t skip_mode;
 #endif  // CONFIG_EXT_SKIP
   int8_t segment_id;
-#if CONFIG_Q_SEGMENTATION
-  int8_t q_segment_id;
-#endif
   int8_t seg_id_predicted;  // valid only when temporal_update is enabled
 
   // Only for INTRA blocks
diff --git a/av1/common/entropy.c b/av1/common/entropy.c
index 2756071..ae45300 100644
--- a/av1/common/entropy.c
+++ b/av1/common/entropy.c
@@ -1751,9 +1751,11 @@
   AVERAGE_TILE_CDFS(lpf_delta_cdf);
   AVERAGE_TILE_CDFS(lpf_sign_cdf);
 #endif  // CONFIG_LPF_SB
-#if CONFIG_Q_SEGMENTATION
+#if CONFIG_SPATIAL_SEGMENTATION
   int j;
-  for (j = 0; j < Q_SEGMENT_CDF_COUNT; j++) AVERAGE_TILE_CDFS(seg.q_seg_cdf[j]);
+  for (j = 0; j < SPATIAL_PREDICTION_PROBS; j++) {
+    AVERAGE_TILE_CDFS(seg.spatial_pred_seg_cdf[j]);
+  }
 #endif
 }
 
@@ -1807,10 +1809,6 @@
   AVERAGE_TILE_CDFS(lpf_delta_cdf);
   AVERAGE_TILE_CDFS(lpf_sign_cdf);
 #endif  // CONFIG_LPF_SB
-#if CONFIG_Q_SEGMENTATION
-  int j;
-  for (j = 0; j < Q_SEGMENT_CDF_COUNT; j++) AVERAGE_TILE_CDFS(seg.q_seg_cdf[j]);
-#endif
 #if CONFIG_JNT_COMP
   AVERAGE_TILE_CDFS(compound_index_cdf);
 #endif  // CONFIG_JNT_COMP
diff --git a/av1/common/entropymode.c b/av1/common/entropymode.c
index 0ddf6b6..c75a148 100644
--- a/av1/common/entropymode.c
+++ b/av1/common/entropymode.c
@@ -1603,9 +1603,10 @@
   AOM_CDF8(4096, 8192, 12288, 16384, 20480, 24576, 28672)
 };
 
-#if CONFIG_Q_SEGMENTATION
+#if CONFIG_SPATIAL_SEGMENTATION
 static const aom_cdf_prob
-    default_q_seg_tree_cdf[Q_SEGMENT_CDF_COUNT][CDF_SIZE(MAX_SEGMENTS)] = {
+    default_spatial_pred_seg_tree_cdf[SPATIAL_PREDICTION_PROBS][CDF_SIZE(
+        MAX_SEGMENTS)] = {
       {
           AOM_CDF8(5622, 7893, 16093, 18233, 27809, 28373, 32533),
       },
@@ -3085,9 +3086,10 @@
   av1_copy(fc->skip_cdfs, default_skip_cdfs);
   av1_copy(fc->intra_inter_cdf, default_intra_inter_cdf);
   av1_copy(fc->seg.tree_cdf, default_seg_tree_cdf);
-#if CONFIG_Q_SEGMENTATION
-  for (int i = 0; i < Q_SEGMENT_CDF_COUNT; i++)
-    av1_copy(fc->seg.q_seg_cdf[i], default_q_seg_tree_cdf[i]);
+#if CONFIG_SPATIAL_SEGMENTATION
+  for (int i = 0; i < SPATIAL_PREDICTION_PROBS; i++)
+    av1_copy(fc->seg.spatial_pred_seg_cdf[i],
+             default_spatial_pred_seg_tree_cdf[i]);
 #endif
   av1_copy(fc->tx_size_cdf, default_tx_size_cdf);
   av1_copy(fc->delta_q_cdf, default_delta_q_cdf);
diff --git a/av1/common/onyxc_int.h b/av1/common/onyxc_int.h
index 0ddfc42..7d8fa3f 100644
--- a/av1/common/onyxc_int.h
+++ b/av1/common/onyxc_int.h
@@ -398,10 +398,11 @@
 #endif
   uint8_t *last_frame_seg_map;
   uint8_t *current_frame_seg_map;
-#if CONFIG_Q_SEGMENTATION
-  uint8_t *q_seg_map;
-#endif
   int seg_map_alloc_size;
+#if CONFIG_SPATIAL_SEGMENTATION
+  int last_active_segid;
+  int preskip_segid;
+#endif
 
   InterpFilter interp_filter;
 
diff --git a/av1/common/pred_common.h b/av1/common/pred_common.h
index bf3f091..6ce442f 100644
--- a/av1/common/pred_common.h
+++ b/av1/common/pred_common.h
@@ -20,9 +20,9 @@
 extern "C" {
 #endif
 
-#if CONFIG_Q_SEGMENTATION
+#if CONFIG_SPATIAL_SEGMENTATION
 /* Picks CDFs based on number of matching segment IDs */
-static INLINE int pick_q_seg_cdf(int prev_ul, int prev_u, int prev_l) {
+static INLINE int pick_spatial_seg_cdf(int prev_ul, int prev_u, int prev_l) {
   if ((prev_ul == prev_u) && (prev_ul == prev_l))
     return 2;
   else if ((prev_ul == prev_u) || (prev_ul == prev_l) || (prev_u == prev_l))
@@ -31,14 +31,15 @@
     return 0;
 }
 
-static INLINE int pick_q_seg_pred(int prev_ul, int prev_u, int prev_l) {
+static INLINE int pick_spatial_seg_pred(int prev_ul, int prev_u, int prev_l) {
   /* If 2 or more are identical returns that as predictor, otherwise prev_l */
   return (prev_ul == prev_u) ? prev_u : prev_l;
 }
 
-static INLINE void set_q_segment_id(const AV1_COMMON *const cm,
-                                    uint8_t *segment_ids, BLOCK_SIZE bsize,
-                                    int mi_row, int mi_col, int segment_id) {
+static INLINE void set_spatial_segment_id(const AV1_COMMON *const cm,
+                                          uint8_t *segment_ids,
+                                          BLOCK_SIZE bsize, int mi_row,
+                                          int mi_col, int segment_id) {
   const int mi_offset = mi_row * cm->mi_cols + mi_col;
   const int bw = mi_size_wide[bsize];
   const int bh = mi_size_high[bsize];
diff --git a/av1/common/quant_common.c b/av1/common/quant_common.c
index 44a31ff..286a8a4 100644
--- a/av1/common/quant_common.c
+++ b/av1/common/quant_common.c
@@ -430,25 +430,14 @@
 #endif  // !CONFIG_DAALA_TX
 
 int av1_get_qindex(const struct segmentation *seg, int segment_id,
-#if CONFIG_Q_SEGMENTATION
-                   int q_segment_id, int base_qindex)
-#else
-                   int base_qindex)
-#endif
-{
+                   int base_qindex) {
   if (segfeature_active(seg, segment_id, SEG_LVL_ALT_Q)) {
     const int data = get_segdata(seg, segment_id, SEG_LVL_ALT_Q);
     const int seg_qindex = base_qindex + data;
     return clamp(seg_qindex, 0, MAXQ);
-  }
-#if CONFIG_Q_SEGMENTATION
-  else if (q_segment_id < seg->q_lvls) {
-    const int seg_qindex = base_qindex + seg->q_delta[q_segment_id];
-    return clamp(seg_qindex, 0, MAXQ);
-  }
-#endif
-  else
+  } else {
     return base_qindex;
+  }
 }
 
 #if CONFIG_AOM_QM
diff --git a/av1/common/quant_common.h b/av1/common/quant_common.h
index a8d1114..7a7db57 100644
--- a/av1/common/quant_common.h
+++ b/av1/common/quant_common.h
@@ -44,11 +44,7 @@
 int16_t av1_qindex_from_ac_Q3(int ac_Q3, aom_bit_depth_t bit_depth);
 
 int av1_get_qindex(const struct segmentation *seg, int segment_id,
-#if CONFIG_Q_SEGMENTATION
-                   int q_segment_id, int base_qindex);
-#else
                    int base_qindex);
-#endif
 #if CONFIG_AOM_QM
 // Reduce the large number of quantizers to a smaller number of levels for which
 // different matrices may be defined
diff --git a/av1/common/seg_common.h b/av1/common/seg_common.h
index 03276cb..a68fe6e 100644
--- a/av1/common/seg_common.h
+++ b/av1/common/seg_common.h
@@ -22,8 +22,8 @@
 #define SEG_TREE_PROBS (MAX_SEGMENTS - 1)
 
 #define PREDICTION_PROBS 3
-#if CONFIG_Q_SEGMENTATION
-#define Q_SEGMENT_CDF_COUNT 3
+#if CONFIG_SPATIAL_SEGMENTATION
+#define SPATIAL_PREDICTION_PROBS 3
 #endif
 
 #if CONFIG_LOOPFILTER_LEVEL
@@ -60,10 +60,6 @@
 
 struct segmentation {
   uint8_t enabled;
-#if CONFIG_Q_SEGMENTATION
-  uint8_t q_lvls;
-  int16_t q_delta[MAX_SEGMENTS];
-#endif
   uint8_t update_map;
   uint8_t update_data;
   uint8_t temporal_update;
@@ -76,8 +72,9 @@
   aom_prob tree_probs[SEG_TREE_PROBS];
   aom_cdf_prob tree_cdf[CDF_SIZE(MAX_SEGMENTS)];
   aom_cdf_prob pred_cdf[PREDICTION_PROBS][CDF_SIZE(2)];
-#if CONFIG_Q_SEGMENTATION
-  aom_cdf_prob q_seg_cdf[Q_SEGMENT_CDF_COUNT][CDF_SIZE(MAX_SEGMENTS)];
+#if CONFIG_SPATIAL_SEGMENTATION
+  aom_cdf_prob spatial_pred_seg_cdf[SPATIAL_PREDICTION_PROBS]
+                                   [CDF_SIZE(MAX_SEGMENTS)];
 #endif
 };
 
diff --git a/av1/decoder/decodeframe.c b/av1/decoder/decodeframe.c
index 08db2ad..785b99c 100644
--- a/av1/decoder/decodeframe.c
+++ b/av1/decoder/decodeframe.c
@@ -406,11 +406,7 @@
     for (int i = 0; i < MAX_SEGMENTS; i++) {
 #if CONFIG_EXT_DELTA_Q
       const int current_qindex =
-#if CONFIG_Q_SEGMENTATION
-          av1_get_qindex(&cm->seg, i, i, xd->current_qindex);
-#else
           av1_get_qindex(&cm->seg, i, xd->current_qindex);
-#endif
 #else
       const int current_qindex = xd->current_qindex;
 #endif  // CONFIG_EXT_DELTA_Q
@@ -959,6 +955,10 @@
     }
   }
 
+#if CONFIG_SPATIAL_SEGMENTATION
+  cm->preskip_segid = 0;
+#endif
+
   // Segmentation data update
   seg->update_data = aom_rb_read_bit(rb);
   if (seg->update_data) {
@@ -969,6 +969,10 @@
         int data = 0;
         const int feature_enabled = aom_rb_read_bit(rb);
         if (feature_enabled) {
+#if CONFIG_SPATIAL_SEGMENTATION
+          cm->preskip_segid |= j >= SEG_LVL_REF_FRAME;
+          cm->last_active_segid = i;
+#endif
           av1_enable_segfeature(seg, i, j);
           data = decode_unsigned_max(rb, av1_seg_feature_data_max(j));
           if (av1_is_segfeature_signed(j))
@@ -980,33 +984,6 @@
   }
 }
 
-#if CONFIG_Q_SEGMENTATION
-static void setup_q_segmentation(AV1_COMMON *const cm,
-                                 struct aom_read_bit_buffer *rb) {
-  struct segmentation *const seg = &cm->seg;
-
-  for (int i = 0; i < MAX_SEGMENTS; i++) {
-    if (segfeature_active(seg, i, SEG_LVL_ALT_Q)) {
-      seg->q_lvls = 0;
-      return;
-    }
-  }
-
-  if (!aom_rb_read_bit(rb)) {
-    seg->q_lvls = 0;
-    return;
-  }
-
-  seg->q_lvls = decode_unsigned_max(rb, MAX_SEGMENTS);
-
-  for (int i = 0; i < seg->q_lvls; i++) {
-    int val = decode_unsigned_max(rb, MAXQ);
-    val *= 1 - 2 * aom_rb_read_bit(rb);
-    seg->q_delta[i] = val;
-  }
-}
-#endif
-
 #if CONFIG_LOOP_RESTORATION
 static void decode_restoration_mode(AV1_COMMON *cm,
                                     struct aom_read_bit_buffer *rb) {
@@ -1279,11 +1256,7 @@
   // remaining are don't cares.
   const int max_segments = cm->seg.enabled ? MAX_SEGMENTS : 1;
   for (int i = 0; i < max_segments; ++i) {
-#if CONFIG_Q_SEGMENTATION
-    const int qindex = av1_get_qindex(&cm->seg, i, i, cm->base_qindex);
-#else
     const int qindex = av1_get_qindex(&cm->seg, i, cm->base_qindex);
-#endif
     cm->y_dequant_QTX[i][0] =
         av1_dc_quant_QTX(qindex, cm->y_dc_delta_q, cm->bit_depth);
     cm->y_dequant_QTX[i][1] = av1_ac_quant_QTX(qindex, 0, cm->bit_depth);
@@ -3119,9 +3092,6 @@
 #endif  // CONFIG_Q_ADAPT_PROBS
 
   setup_segmentation(cm, rb);
-#if CONFIG_Q_SEGMENTATION
-  setup_q_segmentation(cm, rb);
-#endif
 
   {
     int delta_q_allowed = 1;
@@ -3132,9 +3102,6 @@
       if (segfeature_active(seg, i, SEG_LVL_ALT_Q)) {
         segment_quantizer_active = 1;
       }
-#if CONFIG_Q_SEGMENTATION
-      if (seg->q_lvls) segment_quantizer_active = 1;
-#endif
     }
     delta_q_allowed = !segment_quantizer_active;
 #endif
@@ -3175,11 +3142,7 @@
 
   for (int i = 0; i < MAX_SEGMENTS; ++i) {
     const int qindex = cm->seg.enabled
-#if CONFIG_Q_SEGMENTATION
-                           ? av1_get_qindex(&cm->seg, i, i, cm->base_qindex)
-#else
                            ? av1_get_qindex(&cm->seg, i, cm->base_qindex)
-#endif
                            : cm->base_qindex;
     xd->lossless[i] = qindex == 0 && cm->y_dc_delta_q == 0 &&
                       cm->u_dc_delta_q == 0 && cm->u_ac_delta_q == 0 &&
diff --git a/av1/decoder/decodemv.c b/av1/decoder/decodemv.c
index 4a7729b..4d7da60 100644
--- a/av1/decoder/decodemv.c
+++ b/av1/decoder/decodemv.c
@@ -344,11 +344,7 @@
   return NEAREST_NEARESTMV + mode;
 }
 
-static int read_segment_id(aom_reader *r, struct segmentation_probs *segp) {
-  return aom_read_symbol(r, segp->tree_cdf, MAX_SEGMENTS, ACCT_STR);
-}
-
-#if CONFIG_Q_SEGMENTATION
+#if CONFIG_SPATIAL_SEGMENTATION
 static int neg_deinterleave(int diff, int ref, int max) {
   if (!ref) return diff;
   if (ref >= (max - 1)) return max - diff - 1;
@@ -371,52 +367,49 @@
   }
 }
 
-static int read_q_segment_id(AV1_COMMON *const cm, MACROBLOCKD *const xd,
-                             int mi_row, int mi_col, aom_reader *r) {
-  struct segmentation *const seg = &cm->seg;
+static int read_segment_id(AV1_COMMON *const cm, MACROBLOCKD *const xd,
+                           int mi_row, int mi_col, aom_reader *r, int skip) {
   FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
   struct segmentation_probs *const segp = &ec_ctx->seg;
-  MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi;
   int prev_ul = 0; /* Top left segment_id */
   int prev_l = 0;  /* Current left segment_id */
   int prev_u = 0;  /* Current top segment_id */
 
-  if (!seg->q_lvls) return 0;
-
   MODE_INFO *const mi = cm->mi + mi_row * cm->mi_stride + mi_col;
   int tinfo = mi->mbmi.boundary_info;
   int above = (!(tinfo & TILE_ABOVE_BOUNDARY)) && ((mi_row - 1) >= 0);
   int left = (!(tinfo & TILE_LEFT_BOUNDARY)) && ((mi_col - 1) >= 0);
 
   if (above && left)
-    prev_ul =
-        get_segment_id(cm, cm->q_seg_map, BLOCK_4X4, mi_row - 1, mi_col - 1);
+    prev_ul = get_segment_id(cm, cm->current_frame_seg_map, BLOCK_4X4,
+                             mi_row - 1, mi_col - 1);
 
   if (above)
-    prev_u = get_segment_id(cm, cm->q_seg_map, BLOCK_4X4, mi_row - 1, mi_col);
+    prev_u = get_segment_id(cm, cm->current_frame_seg_map, BLOCK_4X4,
+                            mi_row - 1, mi_col - 0);
 
   if (left)
-    prev_l = get_segment_id(cm, cm->q_seg_map, BLOCK_4X4, mi_row, mi_col - 1);
+    prev_l = get_segment_id(cm, cm->current_frame_seg_map, BLOCK_4X4,
+                            mi_row - 0, mi_col - 1);
 
-  int cdf_num = pick_q_seg_cdf(prev_ul, prev_u, prev_l);
-  int pred = pick_q_seg_pred(prev_ul, prev_u, prev_l);
+  int cdf_num = pick_spatial_seg_cdf(prev_ul, prev_u, prev_l);
+  int pred = pick_spatial_seg_pred(prev_ul, prev_u, prev_l);
 
-  if (mbmi->skip) {
-    set_q_segment_id(cm, cm->q_seg_map, mbmi->sb_type, mi_row, mi_col, pred);
-    return 0;
-  }
+  if (skip) return pred;
 
-  aom_cdf_prob *pred_cdf = segp->q_seg_cdf[cdf_num];
+  aom_cdf_prob *pred_cdf = segp->spatial_pred_seg_cdf[cdf_num];
   int coded_id = aom_read_symbol(r, pred_cdf, 8, ACCT_STR);
 
-  int segment_id = neg_deinterleave(coded_id, pred, seg->q_lvls);
+  int segment_id = neg_deinterleave(coded_id, pred, cm->last_active_segid + 1);
 
-  assert(segment_id >= 0 && segment_id < seg->q_lvls);
-  set_q_segment_id(cm, cm->q_seg_map, mbmi->sb_type, mi_row, mi_col,
-                   segment_id);
+  assert(segment_id >= 0 && segment_id <= cm->last_active_segid);
 
   return segment_id;
 }
+#else
+static int read_segment_id(aom_reader *r, struct segmentation_probs *segp) {
+  return aom_read_symbol(r, segp->tree_cdf, MAX_SEGMENTS, ACCT_STR);
+}
 #endif
 
 static void read_tx_size_vartx(AV1_COMMON *cm, MACROBLOCKD *xd,
@@ -558,19 +551,36 @@
 }
 
 static int read_intra_segment_id(AV1_COMMON *const cm, MACROBLOCKD *const xd,
-                                 int mi_offset, int x_mis, int y_mis,
+                                 MB_MODE_INFO *const mbmi, int mi_row,
+                                 int mi_col, int bsize, int preskip,
                                  aom_reader *r) {
   struct segmentation *const seg = &cm->seg;
+  const int mi_offset = mi_row * cm->mi_cols + mi_col;
+  const int bw = mi_size_wide[bsize];
+  const int bh = mi_size_high[bsize];
+  const int x_mis = AOMMIN(cm->mi_cols - mi_col, bw);
+  const int y_mis = AOMMIN(cm->mi_rows - mi_row, bh);
   FRAME_COUNTS *counts = xd->counts;
-  FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
-  struct segmentation_probs *const segp = &ec_ctx->seg;
   int segment_id;
 
   if (!seg->enabled) return 0;  // Default for disabled segmentation
 
   assert(seg->update_map && !seg->temporal_update);
 
-  segment_id = read_segment_id(r, segp);
+#if CONFIG_SPATIAL_SEGMENTATION
+  if (preskip) {
+    if (!cm->preskip_segid) return 0;
+  } else {
+    if (cm->preskip_segid) return mbmi->segment_id;
+  }
+  segment_id =
+      read_segment_id(cm, xd, mi_row, mi_col, r, preskip ? 0 : mbmi->skip);
+#else
+  FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
+  (void)preskip;
+  (void)mbmi;
+  segment_id = read_segment_id(r, &ec_ctx->seg);
+#endif
   if (counts) ++counts->seg.tree_total[segment_id];
   set_segment_id(cm, mi_offset, x_mis, y_mis, segment_id);
   return segment_id;
@@ -588,7 +598,8 @@
 }
 
 static int read_inter_segment_id(AV1_COMMON *const cm, MACROBLOCKD *const xd,
-                                 int mi_row, int mi_col, aom_reader *r) {
+                                 int mi_row, int mi_col, int preskip,
+                                 aom_reader *r) {
   struct segmentation *const seg = &cm->seg;
   FRAME_COUNTS *counts = xd->counts;
   FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
@@ -617,6 +628,25 @@
     return predicted_segment_id;
   }
 
+#if CONFIG_SPATIAL_SEGMENTATION
+  if (preskip) {
+    if (!cm->preskip_segid) return 0;
+  } else {
+    if (cm->preskip_segid) return mbmi->segment_id;
+    if (mbmi->skip) {
+      if (seg->temporal_update) {
+        const int ctx = av1_get_pred_context_seg_id(xd);
+        mbmi->seg_id_predicted = 0;
+        if (counts) ++counts->seg.pred[ctx][mbmi->seg_id_predicted];
+      }
+      segment_id = read_segment_id(cm, xd, mi_row, mi_col, r, 0);
+      if (counts) ++counts->seg.tree_total[segment_id];
+      set_segment_id(cm, mi_offset, x_mis, y_mis, segment_id);
+      return segment_id;
+    }
+  }
+#endif
+  (void)preskip;
   if (seg->temporal_update) {
     const int ctx = av1_get_pred_context_seg_id(xd);
     aom_cdf_prob *pred_cdf = segp->pred_cdf[ctx];
@@ -625,11 +655,19 @@
     if (mbmi->seg_id_predicted) {
       segment_id = predicted_segment_id;
     } else {
+#if CONFIG_SPATIAL_SEGMENTATION
+      segment_id = read_segment_id(cm, xd, mi_row, mi_col, r, 0);
+#else
       segment_id = read_segment_id(r, segp);
+#endif
       if (counts) ++counts->seg.tree_mispred[segment_id];
     }
   } else {
+#if CONFIG_SPATIAL_SEGMENTATION
+    segment_id = read_segment_id(cm, xd, mi_row, mi_col, r, 0);
+#else
     segment_id = read_segment_id(r, segp);
+#endif
     if (counts) ++counts->seg.tree_total[segment_id];
   }
   set_segment_id(cm, mi_offset, x_mis, y_mis, segment_id);
@@ -1077,20 +1115,16 @@
   const MODE_INFO *left_mi = xd->left_mi;
   const BLOCK_SIZE bsize = mbmi->sb_type;
   int i;
-  const int mi_offset = mi_row * cm->mi_cols + mi_col;
-  const int bw = mi_size_wide[bsize];
-  const int bh = mi_size_high[bsize];
 
-  // TODO(slavarnway): move x_mis, y_mis into xd ?????
-  const int x_mis = AOMMIN(cm->mi_cols - mi_col, bw);
-  const int y_mis = AOMMIN(cm->mi_rows - mi_row, bh);
   FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
 
-  mbmi->segment_id = read_intra_segment_id(cm, xd, mi_offset, x_mis, y_mis, r);
+  mbmi->segment_id =
+      read_intra_segment_id(cm, xd, mbmi, mi_row, mi_col, bsize, 1, r);
   mbmi->skip = read_skip(cm, xd, mbmi->segment_id, r);
 
-#if CONFIG_Q_SEGMENTATION
-  mbmi->q_segment_id = read_q_segment_id(cm, xd, mi_row, mi_col, r);
+#if CONFIG_SPATIAL_SEGMENTATION
+  mbmi->segment_id =
+      read_intra_segment_id(cm, xd, mbmi, mi_row, mi_col, bsize, 0, r);
 #endif
 
   read_cdef(cm, r, mbmi, mi_col, mi_row);
@@ -2334,7 +2368,7 @@
 
   mbmi->mv[0].as_int = 0;
   mbmi->mv[1].as_int = 0;
-  mbmi->segment_id = read_inter_segment_id(cm, xd, mi_row, mi_col, r);
+  mbmi->segment_id = read_inter_segment_id(cm, xd, mi_row, mi_col, 1, r);
 
 #if CONFIG_EXT_SKIP
   mbmi->skip_mode = read_skip_mode(cm, xd, mbmi->segment_id, r);
@@ -2351,9 +2385,9 @@
 #endif  // CONFIG_EXT_SKIP
     mbmi->skip = read_skip(cm, xd, mbmi->segment_id, r);
 
-#if CONFIG_Q_SEGMENTATION
-  mbmi->q_segment_id = read_q_segment_id(cm, xd, mi_row, mi_col, r);
-#endif  // CONFIG_Q_SEGMENTATION
+#if CONFIG_SPATIAL_SEGMENTATION
+  mbmi->segment_id = read_inter_segment_id(cm, xd, mi_row, mi_col, 0, r);
+#endif
 
   read_cdef(cm, r, mbmi, mi_col, mi_row);
 
diff --git a/av1/encoder/aq_cyclicrefresh.c b/av1/encoder/aq_cyclicrefresh.c
index 22247a2..12bdc80 100644
--- a/av1/encoder/aq_cyclicrefresh.c
+++ b/av1/encoder/aq_cyclicrefresh.c
@@ -410,11 +410,7 @@
     int mi_col = sb_col_index * cm->mib_size;
     int qindex_thresh =
         cpi->oxcf.content == AOM_CONTENT_SCREEN
-#if CONFIG_Q_SEGMENTATION
-            ? av1_get_qindex(&cm->seg, CR_SEGMENT_ID_BOOST2, 0, cm->base_qindex)
-#else
             ? av1_get_qindex(&cm->seg, CR_SEGMENT_ID_BOOST2, cm->base_qindex)
-#endif
             : 0;
     assert(mi_row >= 0 && mi_row < cm->mi_rows);
     assert(mi_col >= 0 && mi_col < cm->mi_cols);
diff --git a/av1/encoder/av1_quantize.c b/av1/encoder/av1_quantize.c
index 18b226c..d0ec6ec 100644
--- a/av1/encoder/av1_quantize.c
+++ b/av1/encoder/av1_quantize.c
@@ -1642,11 +1642,7 @@
 }
 
 void av1_init_plane_quantizers(const AV1_COMP *cpi, MACROBLOCK *x,
-#if CONFIG_Q_SEGMENTATION
-                               int segment_id, int q_segment_id) {
-#else
                                int segment_id) {
-#endif
   const AV1_COMMON *const cm = &cpi->common;
   MACROBLOCKD *const xd = &x->e_mbd;
   const QUANTS *const quants = &cpi->quants;
@@ -1663,12 +1659,7 @@
                 cm->delta_q_present_flag ? cm->base_qindex + xd->delta_qindex
                                          : cm->base_qindex));
 #endif
-#if CONFIG_Q_SEGMENTATION
-  const int qindex =
-      av1_get_qindex(&cm->seg, segment_id, q_segment_id, current_q_index);
-#else
   const int qindex = av1_get_qindex(&cm->seg, segment_id, current_q_index);
-#endif
   const int rdmult = av1_compute_rd_mult(cpi, qindex + cm->y_dc_delta_q);
 #if CONFIG_AOM_QM
   int minqm = cm->min_qmlevel;
@@ -1766,12 +1757,7 @@
 void av1_frame_init_quantizer(AV1_COMP *cpi) {
   MACROBLOCK *const x = &cpi->td.mb;
   MACROBLOCKD *const xd = &x->e_mbd;
-#if CONFIG_Q_SEGMENTATION
-  av1_init_plane_quantizers(cpi, x, xd->mi[0]->mbmi.segment_id,
-                            xd->mi[0]->mbmi.q_segment_id);
-#else
   av1_init_plane_quantizers(cpi, x, xd->mi[0]->mbmi.segment_id);
-#endif
 }
 
 void av1_set_quantizer(AV1_COMMON *cm, int q) {
diff --git a/av1/encoder/av1_quantize.h b/av1/encoder/av1_quantize.h
index 94156b5..f08022f 100644
--- a/av1/encoder/av1_quantize.h
+++ b/av1/encoder/av1_quantize.h
@@ -107,11 +107,7 @@
 void av1_frame_init_quantizer(struct AV1_COMP *cpi);
 
 void av1_init_plane_quantizers(const struct AV1_COMP *cpi, MACROBLOCK *x,
-#if CONFIG_Q_SEGMENTATION
-                               int segment_id, int q_segment_id);
-#else
                                int segment_id);
-#endif
 
 void av1_build_quantizer(aom_bit_depth_t bit_depth, int y_dc_delta_q,
                          int u_dc_delta_q, int u_ac_delta_q, int v_dc_delta_q,
diff --git a/av1/encoder/bitstream.c b/av1/encoder/bitstream.c
index d914fc3..1b75dc9 100644
--- a/av1/encoder/bitstream.c
+++ b/av1/encoder/bitstream.c
@@ -615,7 +615,7 @@
 }
 #endif  // CONFIG_LV_MAP
 
-#if CONFIG_Q_SEGMENTATION
+#if CONFIG_SPATIAL_SEGMENTATION
 static int neg_interleave(int x, int ref, int max) {
   const int diff = x - ref;
   if (!ref) return x;
@@ -639,16 +639,16 @@
   }
 }
 
-static void write_q_segment_id(const AV1_COMMON *cm, int skip,
-                               const MB_MODE_INFO *const mbmi, aom_writer *w,
-                               const struct segmentation *seg,
-                               struct segmentation_probs *segp,
-                               BLOCK_SIZE bsize, int mi_row, int mi_col) {
+static void write_segment_id(AV1_COMP *cpi, const MB_MODE_INFO *const mbmi,
+                             aom_writer *w, const struct segmentation *seg,
+                             struct segmentation_probs *segp, int mi_row,
+                             int mi_col, int skip) {
+  AV1_COMMON *const cm = &cpi->common;
   int prev_ul = 0; /* Top left segment_id */
   int prev_l = 0;  /* Current left segment_id */
   int prev_u = 0;  /* Current top segment_id */
 
-  if (!seg->q_lvls) return;
+  if (!seg->enabled || !seg->update_map) return;
 
   MODE_INFO *const mi = cm->mi + mi_row * cm->mi_stride + mi_col;
   int tinfo = mi->mbmi.boundary_info;
@@ -656,39 +656,47 @@
   int left = (!(tinfo & TILE_LEFT_BOUNDARY)) && ((mi_col - 1) >= 0);
 
   if (above && left)
-    prev_ul =
-        get_segment_id(cm, cm->q_seg_map, BLOCK_4X4, mi_row - 1, mi_col - 1);
+    prev_ul = get_segment_id(cm, cm->current_frame_seg_map, BLOCK_4X4,
+                             mi_row - 1, mi_col - 1);
 
   if (above)
-    prev_u = get_segment_id(cm, cm->q_seg_map, BLOCK_4X4, mi_row - 1, mi_col);
+    prev_u = get_segment_id(cm, cm->current_frame_seg_map, BLOCK_4X4,
+                            mi_row - 1, mi_col - 0);
 
   if (left)
-    prev_l = get_segment_id(cm, cm->q_seg_map, BLOCK_4X4, mi_row, mi_col - 1);
+    prev_l = get_segment_id(cm, cm->current_frame_seg_map, BLOCK_4X4,
+                            mi_row - 0, mi_col - 1);
 
-  int cdf_num = pick_q_seg_cdf(prev_ul, prev_u, prev_l);
-  int pred = pick_q_seg_pred(prev_ul, prev_u, prev_l);
+  int cdf_num = pick_spatial_seg_cdf(prev_ul, prev_u, prev_l);
+  int pred = pick_spatial_seg_pred(prev_ul, prev_u, prev_l);
 
   if (skip) {
-    set_q_segment_id(cm, cm->q_seg_map, mbmi->sb_type, mi_row, mi_col, pred);
+    set_spatial_segment_id(cm, cm->current_frame_seg_map, mbmi->sb_type, mi_row,
+                           mi_col, pred);
+    set_spatial_segment_id(cm, cpi->segmentation_map, mbmi->sb_type, mi_row,
+                           mi_col, pred);
+    /* mbmi is read only but we need to update segment_id */
+    ((MB_MODE_INFO *)mbmi)->segment_id = pred;
     return;
   }
 
-  int coded_id = neg_interleave(mbmi->q_segment_id, pred, seg->q_lvls);
+  int coded_id =
+      neg_interleave(mbmi->segment_id, pred, cm->last_active_segid + 1);
 
-  aom_cdf_prob *pred_cdf = segp->q_seg_cdf[cdf_num];
+  aom_cdf_prob *pred_cdf = segp->spatial_pred_seg_cdf[cdf_num];
   aom_write_symbol(w, coded_id, pred_cdf, 8);
 
-  set_q_segment_id(cm, cm->q_seg_map, bsize, mi_row, mi_col,
-                   mbmi->q_segment_id);
+  set_spatial_segment_id(cm, cm->current_frame_seg_map, mbmi->sb_type, mi_row,
+                         mi_col, mbmi->segment_id);
 }
-#endif
-
+#else
 static void write_segment_id(aom_writer *w, const struct segmentation *seg,
                              struct segmentation_probs *segp, int segment_id) {
   if (seg->enabled && seg->update_map) {
     aom_write_symbol(w, segment_id, segp->tree_cdf, MAX_SEGMENTS);
   }
 }
+#endif
 
 #define WRITE_REF_BIT(bname, pname) \
   aom_write_symbol(w, bname, av1_get_pred_cdf_##pname(cm, xd), 2)
@@ -1229,6 +1237,77 @@
 #endif
 }
 
+static void write_inter_segment_id(AV1_COMP *cpi, aom_writer *w,
+                                   const struct segmentation *const seg,
+                                   struct segmentation_probs *const segp,
+                                   int mi_row, int mi_col, int skip,
+                                   int preskip) {
+  MACROBLOCKD *const xd = &cpi->td.mb.e_mbd;
+  const MODE_INFO *mi = xd->mi[0];
+  const MB_MODE_INFO *const mbmi = &mi->mbmi;
+#if CONFIG_SPATIAL_SEGMENTATION
+  AV1_COMMON *const cm = &cpi->common;
+#else
+  (void)mi_row;
+  (void)mi_col;
+  (void)skip;
+  (void)preskip;
+#endif
+
+  if (seg->update_map) {
+#if CONFIG_SPATIAL_SEGMENTATION
+    if (preskip) {
+      if (!cm->preskip_segid) return;
+    } else {
+      if (cm->preskip_segid) return;
+      if (skip) {
+        int prev_segid = mbmi->segment_id;
+        write_segment_id(cpi, mbmi, w, seg, segp, mi_row, mi_col, 0);
+
+        if (seg->temporal_update) {
+          const int pred_flag = mbmi->seg_id_predicted;
+          const int pred_context = av1_get_pred_context_seg_id(xd);
+          unsigned(*temporal_predictor_count)[2] = cm->counts.seg.pred;
+          unsigned *t_unpred_seg_counts = cm->counts.seg.tree_mispred;
+
+          temporal_predictor_count[pred_context][pred_flag]--;
+          if (!pred_flag) t_unpred_seg_counts[prev_segid]--;
+
+          ((MB_MODE_INFO *)mbmi)->seg_id_predicted = 0;
+          temporal_predictor_count[pred_context][0]--;
+          t_unpred_seg_counts[mbmi->segment_id]--;
+        }
+        return;
+      }
+    }
+#endif
+    if (seg->temporal_update) {
+      const int pred_flag = mbmi->seg_id_predicted;
+      aom_cdf_prob *pred_cdf = av1_get_pred_cdf_seg_id(segp, xd);
+      aom_write_symbol(w, pred_flag, pred_cdf, 2);
+      if (!pred_flag) {
+#if CONFIG_SPATIAL_SEGMENTATION
+        write_segment_id(cpi, mbmi, w, seg, segp, mi_row, mi_col, 0);
+#else
+        write_segment_id(w, seg, segp, mbmi->segment_id);
+#endif
+      }
+#if CONFIG_SPATIAL_SEGMENTATION
+      if (pred_flag) {
+        set_spatial_segment_id(cm, cm->current_frame_seg_map, mbmi->sb_type,
+                               mi_row, mi_col, mbmi->segment_id);
+      }
+#endif
+    } else {
+#if CONFIG_SPATIAL_SEGMENTATION
+      write_segment_id(cpi, mbmi, w, seg, segp, mi_row, mi_col, 0);
+#else
+      write_segment_id(w, seg, segp, mbmi->segment_id);
+#endif
+    }
+  }
+}
+
 static void pack_inter_mode_mvs(AV1_COMP *cpi, const int mi_row,
                                 const int mi_col, aom_writer *w) {
   AV1_COMMON *const cm = &cpi->common;
@@ -1251,16 +1330,7 @@
   (void)mi_row;
   (void)mi_col;
 
-  if (seg->update_map) {
-    if (seg->temporal_update) {
-      const int pred_flag = mbmi->seg_id_predicted;
-      aom_cdf_prob *pred_cdf = av1_get_pred_cdf_seg_id(segp, xd);
-      aom_write_symbol(w, pred_flag, pred_cdf, 2);
-      if (!pred_flag) write_segment_id(w, seg, segp, segment_id);
-    } else {
-      write_segment_id(w, seg, segp, segment_id);
-    }
-  }
+  write_inter_segment_id(cpi, w, seg, segp, mi_row, mi_col, 0, 1);
 
 #if CONFIG_EXT_SKIP
   write_skip_mode(cm, xd, segment_id, mi, w);
@@ -1275,9 +1345,9 @@
   }
 #endif  // CONFIG_EXT_SKIP
 
-#if CONFIG_Q_SEGMENTATION
-  write_q_segment_id(cm, skip, mbmi, w, seg, segp, bsize, mi_row, mi_col);
-#endif  // CONFIG_Q_SEGMENTATION
+#if CONFIG_SPATIAL_SEGMENTATION
+  write_inter_segment_id(cpi, w, seg, segp, mi_row, mi_col, skip, 0);
+#endif
 
   write_cdef(cm, w, skip, mi_col, mi_row);
 
@@ -1570,12 +1640,13 @@
 }
 #endif  // CONFIG_INTRABC
 
-static void write_mb_modes_kf(AV1_COMMON *cm, MACROBLOCKD *xd,
+static void write_mb_modes_kf(AV1_COMP *cpi, MACROBLOCKD *xd,
 #if CONFIG_INTRABC
                               const MB_MODE_INFO_EXT *mbmi_ext,
 #endif  // CONFIG_INTRABC
                               const int mi_row, const int mi_col,
                               aom_writer *w) {
+  AV1_COMMON *const cm = &cpi->common;
   FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
   const struct segmentation *const seg = &cm->seg;
   struct segmentation_probs *const segp = &ec_ctx->seg;
@@ -1587,12 +1658,18 @@
   (void)mi_row;
   (void)mi_col;
 
+#if CONFIG_SPATIAL_SEGMENTATION
+  if (cm->preskip_segid && seg->update_map)
+    write_segment_id(cpi, mbmi, w, seg, segp, mi_row, mi_col, 0);
+#else
   if (seg->update_map) write_segment_id(w, seg, segp, mbmi->segment_id);
+#endif
 
   const int skip = write_skip(cm, xd, mbmi->segment_id, mi, w);
 
-#if CONFIG_Q_SEGMENTATION
-  write_q_segment_id(cm, skip, mbmi, w, seg, segp, bsize, mi_row, mi_col);
+#if CONFIG_SPATIAL_SEGMENTATION
+  if (!cm->preskip_segid && seg->update_map)
+    write_segment_id(cpi, mbmi, w, seg, segp, mi_row, mi_col, skip);
 #endif
 
   write_cdef(cm, w, skip, mi_col, mi_row);
@@ -1837,7 +1914,7 @@
                               ((mi_row & MAX_MIB_MASK) << TX_UNIT_HIGH_LOG2);
     }
 #endif  // CONFIG_INTRABC
-    write_mb_modes_kf(cm, xd,
+    write_mb_modes_kf(cpi, xd,
 #if CONFIG_INTRABC
                       cpi->td.mb.mbmi_ext,
 #endif  // CONFIG_INTRABC
@@ -2709,6 +2786,10 @@
     }
   }
 
+#if CONFIG_SPATIAL_SEGMENTATION
+  cm->preskip_segid = 0;
+#endif
+
   // Segmentation data
   aom_wb_write_bit(wb, seg->update_data);
   if (seg->update_data) {
@@ -2719,6 +2800,10 @@
         if (active) {
           const int data = get_segdata(seg, i, j);
           const int data_max = av1_seg_feature_data_max(j);
+#if CONFIG_SPATIAL_SEGMENTATION
+          cm->preskip_segid |= j >= SEG_LVL_REF_FRAME;
+          cm->last_active_segid = i;
+#endif
 
           if (av1_is_segfeature_signed(j)) {
             encode_unsigned_max(wb, abs(data), data_max);
@@ -2732,32 +2817,6 @@
   }
 }
 
-#if CONFIG_Q_SEGMENTATION
-static void encode_q_segmentation(AV1_COMMON *cm,
-                                  struct aom_write_bit_buffer *wb) {
-  int i;
-  struct segmentation *seg = &cm->seg;
-
-  for (i = 0; i < MAX_SEGMENTS; i++) {
-    if (segfeature_active(seg, i, SEG_LVL_ALT_Q)) {
-      seg->q_lvls = 0;
-      return;
-    }
-  }
-
-  aom_wb_write_bit(wb, !!seg->q_lvls);
-  if (!seg->q_lvls) return;
-
-  encode_unsigned_max(wb, seg->q_lvls, MAX_SEGMENTS);
-
-  for (i = 0; i < seg->q_lvls; i++) {
-    const int val = seg->q_delta[i];
-    encode_unsigned_max(wb, abs(val), MAXQ);
-    aom_wb_write_bit(wb, val < 0);
-  }
-}
-#endif
-
 static void write_tx_mode(AV1_COMMON *cm, TX_MODE *mode,
                           struct aom_write_bit_buffer *wb) {
   if (cm->all_lossless) {
@@ -3931,9 +3990,6 @@
   encode_loopfilter(cm, wb);
   encode_quantization(cm, wb);
   encode_segmentation(cm, xd, wb);
-#if CONFIG_Q_SEGMENTATION
-  encode_q_segmentation(cm, wb);
-#endif
   {
     int delta_q_allowed = 1;
 #if !CONFIG_EXT_DELTA_Q
@@ -3945,9 +4001,6 @@
         segment_quantizer_active = 1;
       }
     }
-#if CONFIG_Q_SEGMENTATION
-    segment_quantizer_active |= !!seg->q_lvls;
-#endif
     delta_q_allowed = !segment_quantizer_active;
 #endif
 
@@ -4283,9 +4336,6 @@
   encode_loopfilter(cm, wb);
   encode_quantization(cm, wb);
   encode_segmentation(cm, xd, wb);
-#if CONFIG_Q_SEGMENTATION
-  encode_q_segmentation(cm, wb);
-#endif
   {
     int delta_q_allowed = 1;
 #if !CONFIG_EXT_DELTA_Q
@@ -4297,9 +4347,6 @@
         segment_quantizer_active = 1;
       }
     }
-#if CONFIG_Q_SEGMENTATION
-    segment_quantizer_active |= !!seg->q_lvls;
-#endif
     delta_q_allowed = !segment_quantizer_active;
 #endif
 
diff --git a/av1/encoder/encodeframe.c b/av1/encoder/encodeframe.c
index 918eea1..e930bf4 100644
--- a/av1/encoder/encodeframe.c
+++ b/av1/encoder/encodeframe.c
@@ -348,29 +348,15 @@
 #endif
 
   mbmi->segment_id = 0;
-#if CONFIG_Q_SEGMENTATION
-  mbmi->q_segment_id = 0;
-#endif
 
-// Setup segment ID.
-#if CONFIG_Q_SEGMENTATION
-  if (seg->enabled || seg->q_lvls) {
-#else
+  // Setup segment ID.
   if (seg->enabled) {
-#endif
     if (seg->enabled && !cpi->vaq_refresh) {
       const uint8_t *const map =
           seg->update_map ? cpi->segmentation_map : cm->last_frame_seg_map;
       mbmi->segment_id = get_segment_id(cm, map, bsize, mi_row, mi_col);
     }
-#if CONFIG_Q_SEGMENTATION
-    if (seg->q_lvls)
-      mbmi->q_segment_id =
-          get_segment_id(cm, cpi->q_seg_encoding_map, bsize, mi_row, mi_col);
-    av1_init_plane_quantizers(cpi, x, mbmi->segment_id, mbmi->q_segment_id);
-#else
     av1_init_plane_quantizers(cpi, x, mbmi->segment_id);
-#endif
   }
 }
 
@@ -557,21 +543,11 @@
 
 #if !CONFIG_EXT_DELTA_Q
   if (cpi->oxcf.aq_mode > NO_AQ && cpi->oxcf.aq_mode < DELTA_AQ)
-#if CONFIG_Q_SEGMENTATION
-    av1_init_plane_quantizers(cpi, x, xd->mi[0]->mbmi.segment_id,
-                              xd->mi[0]->mbmi.q_segment_id);
-#else
     av1_init_plane_quantizers(cpi, x, xd->mi[0]->mbmi.segment_id);
-#endif
 #else
   if (cpi->oxcf.aq_mode)
-#if CONFIG_Q_SEGMENTATION
-    av1_init_plane_quantizers(cpi, x, xd->mi[0]->mbmi.segment_id,
-                              xd->mi[0]->mbmi.q_segment_id);
-#else
     av1_init_plane_quantizers(cpi, x, xd->mi[0]->mbmi.segment_id);
 #endif
-#endif
 
   x->skip = ctx->skip;
 
@@ -788,25 +764,12 @@
 }
 
 static int set_segment_rdmult(const AV1_COMP *const cpi, MACROBLOCK *const x,
-#if CONFIG_Q_SEGMENTATION
-                              int8_t segment_id, int8_t q_segment_id) {
-#else
                               int8_t segment_id) {
-#endif
   int segment_qindex;
   const AV1_COMMON *const cm = &cpi->common;
-#if CONFIG_Q_SEGMENTATION
-  av1_init_plane_quantizers(cpi, x, segment_id, q_segment_id);
-#else
   av1_init_plane_quantizers(cpi, x, segment_id);
-#endif
   aom_clear_system_state();
-#if CONFIG_Q_SEGMENTATION
-  segment_qindex =
-      av1_get_qindex(&cm->seg, segment_id, q_segment_id, cm->base_qindex);
-#else
   segment_qindex = av1_get_qindex(&cm->seg, segment_id, cm->base_qindex);
-#endif
   return av1_compute_rd_mult(cpi, segment_qindex + cm->y_dc_delta_q);
 }
 
@@ -917,24 +880,12 @@
       const int energy =
           bsize <= BLOCK_16X16 ? x->mb_energy : av1_block_energy(cpi, x, bsize);
       mbmi->segment_id = av1_vaq_segment_id(energy);
-// Re-initialise quantiser
-#if CONFIG_Q_SEGMENTATION
-      av1_init_plane_quantizers(cpi, x, mbmi->segment_id, mbmi->q_segment_id);
-#else
+      // Re-initialise quantiser
       av1_init_plane_quantizers(cpi, x, mbmi->segment_id);
-#endif
     }
-#if CONFIG_Q_SEGMENTATION
-    x->rdmult =
-        set_segment_rdmult(cpi, x, mbmi->segment_id, mbmi->q_segment_id);
-  } else if (aq_mode == COMPLEXITY_AQ) {
-    x->rdmult =
-        set_segment_rdmult(cpi, x, mbmi->segment_id, mbmi->q_segment_id);
-#else
     x->rdmult = set_segment_rdmult(cpi, x, mbmi->segment_id);
   } else if (aq_mode == COMPLEXITY_AQ) {
     x->rdmult = set_segment_rdmult(cpi, x, mbmi->segment_id);
-#endif
   } else if (aq_mode == CYCLIC_REFRESH_AQ) {
     // If segment is boosted, use rdmult for that segment.
     if (cyclic_refresh_segment_id_boosted(mbmi->segment_id))
@@ -2269,66 +2220,24 @@
 
 #if CONFIG_FP_MB_STATS
 const int qindex_skip_threshold_lookup[BLOCK_SIZES] = {
-  0,
-  10,
-  10,
-  30,
-  40,
-  40,
-  60,
-  80,
-  80,
-  90,
-  100,
-  100,
-  120,
+  0, 10, 10, 30, 40, 40, 60, 80, 80, 90, 100, 100, 120,
 #if CONFIG_EXT_PARTITION
   // TODO(debargha): What are the correct numbers here?
-  130,
-  130,
-  150
+  130, 130, 150
 #endif  // CONFIG_EXT_PARTITION
 };
 const int qindex_split_threshold_lookup[BLOCK_SIZES] = {
-  0,
-  3,
-  3,
-  7,
-  15,
-  15,
-  30,
-  40,
-  40,
-  60,
-  80,
-  80,
-  120,
+  0, 3, 3, 7, 15, 15, 30, 40, 40, 60, 80, 80, 120,
 #if CONFIG_EXT_PARTITION
   // TODO(debargha): What are the correct numbers here?
-  160,
-  160,
-  240
+  160, 160, 240
 #endif  // CONFIG_EXT_PARTITION
 };
 const int complexity_16x16_blocks_threshold[BLOCK_SIZES] = {
-  1,
-  1,
-  1,
-  1,
-  1,
-  1,
-  1,
-  1,
-  1,
-  1,
-  4,
-  4,
-  6,
+  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 4, 4, 6,
 #if CONFIG_EXT_PARTITION
   // TODO(debargha): What are the correct numbers here?
-  8,
-  8,
-  10
+  8, 8, 10
 #endif  // CONFIG_EXT_PARTITION
 };
 
@@ -3439,16 +3348,8 @@
       xd->mi[0]->mbmi.current_q_index = current_qindex;
 #if !CONFIG_EXT_DELTA_Q
       xd->mi[0]->mbmi.segment_id = 0;
-#if CONFIG_Q_SEGMENTATION
-      xd->mi[0]->mbmi.q_segment_id = 0;
-#endif
 #endif  // CONFIG_EXT_DELTA_Q
-#if CONFIG_Q_SEGMENTATION
-      av1_init_plane_quantizers(cpi, x, xd->mi[0]->mbmi.segment_id,
-                                xd->mi[0]->mbmi.q_segment_id);
-#else
       av1_init_plane_quantizers(cpi, x, xd->mi[0]->mbmi.segment_id);
-#endif
 #if CONFIG_EXT_DELTA_Q
       if (cpi->oxcf.deltaq_mode == DELTA_Q_LF) {
         int j, k;
@@ -4181,11 +4082,7 @@
 
   for (i = 0; i < MAX_SEGMENTS; ++i) {
     const int qindex = cm->seg.enabled
-#if CONFIG_Q_SEGMENTATION
-                           ? av1_get_qindex(&cm->seg, i, i, cm->base_qindex)
-#else
                            ? av1_get_qindex(&cm->seg, i, cm->base_qindex)
-#endif
                            : cm->base_qindex;
     xd->lossless[i] = qindex == 0 && cm->y_dc_delta_q == 0 &&
                       cm->u_dc_delta_q == 0 && cm->u_ac_delta_q == 0 &&
diff --git a/av1/encoder/encoder.h b/av1/encoder/encoder.h
index 623a5f7..d1edaf9 100644
--- a/av1/encoder/encoder.h
+++ b/av1/encoder/encoder.h
@@ -491,9 +491,6 @@
 #endif  // CONFIG_FARME_MARKER
 
   uint8_t *segmentation_map;
-#if CONFIG_Q_SEGMENTATION
-  uint8_t *q_seg_encoding_map;  // Must be allocated and set by AQs
-#endif
 
   CYCLIC_REFRESH *cyclic_refresh;
   ActiveMap active_map;
diff --git a/av1/encoder/rd.c b/av1/encoder/rd.c
index 3bd3b42..483a1e5 100644
--- a/av1/encoder/rd.c
+++ b/av1/encoder/rd.c
@@ -415,14 +415,9 @@
 
   for (segment_id = 0; segment_id < MAX_SEGMENTS; ++segment_id) {
     const int qindex =
-#if CONFIG_Q_SEGMENTATION
-        clamp(
-            av1_get_qindex(&cm->seg, segment_id, segment_id, cm->base_qindex) +
-#else
         clamp(av1_get_qindex(&cm->seg, segment_id, cm->base_qindex) +
-#endif
-                cm->y_dc_delta_q,
-            0, MAXQ);
+                  cm->y_dc_delta_q,
+              0, MAXQ);
     const int q = compute_rd_thresh_factor(qindex, cm->bit_depth);
 
     for (bsize = 0; bsize < BLOCK_SIZES_ALL; ++bsize) {