rtc: Fix stale data access of seg_map

When segmentation is disabled for a frame, 'cur_frame->seg_map'
was accessed in av1_cyclic_reset_segment_skip() without
initialization. Hence, when segmentation is disabled, do not
call the function av1_cyclic_reset_segment_skip().

Also, when 'cyclic_refresh->skip_over4x4' is set in rt speed
10, 'cur_frame->seg_map' is updated every other 4x4 block.
But the function av1_get_spatial_seg_pred() assumed
'cur_frame->seg_map' to be set for every 4x4 block. Hence,
modify the function av1_get_spatial_seg_pred() to access
data at 8x8 block when 'cyclic_refresh->skip_over4x4' is set.

Change-Id: I29ca120e32c9c6247de1df02f47291320eacdce8
diff --git a/av1/common/pred_common.h b/av1/common/pred_common.h
index 3db9dd6..6ad7ba3 100644
--- a/av1/common/pred_common.h
+++ b/av1/common/pred_common.h
@@ -44,7 +44,8 @@
 
 static INLINE int av1_get_spatial_seg_pred(const AV1_COMMON *const cm,
                                            const MACROBLOCKD *const xd,
-                                           int *cdf_index) {
+                                           int *cdf_index, int skip_over4x4) {
+  const int step_size = skip_over4x4 ? 2 : 1;
   int prev_ul = -1;  // top left segment_id
   int prev_l = -1;   // left segment_id
   int prev_u = -1;   // top segment_id
@@ -53,16 +54,16 @@
   const CommonModeInfoParams *const mi_params = &cm->mi_params;
   const uint8_t *seg_map = cm->cur_frame->seg_map;
   if ((xd->up_available) && (xd->left_available)) {
-    prev_ul =
-        get_segment_id(mi_params, seg_map, BLOCK_4X4, mi_row - 1, mi_col - 1);
+    prev_ul = get_segment_id(mi_params, seg_map, BLOCK_4X4, mi_row - step_size,
+                             mi_col - step_size);
   }
   if (xd->up_available) {
-    prev_u =
-        get_segment_id(mi_params, seg_map, BLOCK_4X4, mi_row - 1, mi_col - 0);
+    prev_u = get_segment_id(mi_params, seg_map, BLOCK_4X4, mi_row - step_size,
+                            mi_col - 0);
   }
   if (xd->left_available) {
-    prev_l =
-        get_segment_id(mi_params, seg_map, BLOCK_4X4, mi_row - 0, mi_col - 1);
+    prev_l = get_segment_id(mi_params, seg_map, BLOCK_4X4, mi_row - 0,
+                            mi_col - step_size);
   }
   // This property follows from the fact that get_segment_id() returns a
   // nonnegative value. This allows us to test for all edge cases with a simple
diff --git a/av1/decoder/decodemv.c b/av1/decoder/decodemv.c
index d6743b3..6e7291d 100644
--- a/av1/decoder/decodemv.c
+++ b/av1/decoder/decodemv.c
@@ -279,7 +279,7 @@
 static int read_segment_id(AV1_COMMON *const cm, const MACROBLOCKD *const xd,
                            aom_reader *r, int skip) {
   int cdf_num;
-  const int pred = av1_get_spatial_seg_pred(cm, xd, &cdf_num);
+  const int pred = av1_get_spatial_seg_pred(cm, xd, &cdf_num, 0);
   if (skip) return pred;
 
   FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
diff --git a/av1/encoder/aq_cyclicrefresh.c b/av1/encoder/aq_cyclicrefresh.c
index bbab748..89c4ac3 100644
--- a/av1/encoder/aq_cyclicrefresh.c
+++ b/av1/encoder/aq_cyclicrefresh.c
@@ -166,8 +166,12 @@
   const int bh = mi_size_high[bsize];
   const int xmis = AOMMIN(cm->mi_params.mi_cols - mi_col, bw);
   const int ymis = AOMMIN(cm->mi_params.mi_rows - mi_row, bh);
+
+  assert(cm->seg.enabled);
+
   if (!cr->skip_over4x4) {
-    mbmi->segment_id = av1_get_spatial_seg_pred(cm, xd, &cdf_num);
+    mbmi->segment_id =
+        av1_get_spatial_seg_pred(cm, xd, &cdf_num, cr->skip_over4x4);
     if (prev_segment_id != mbmi->segment_id) {
       const int block_index = mi_row * cm->mi_params.mi_cols + mi_col;
       for (int mi_y = 0; mi_y < ymis; mi_y++) {
diff --git a/av1/encoder/bitstream.c b/av1/encoder/bitstream.c
index 38ef8c9..6e4af81 100644
--- a/av1/encoder/bitstream.c
+++ b/av1/encoder/bitstream.c
@@ -462,7 +462,8 @@
 
   AV1_COMMON *const cm = &cpi->common;
   int cdf_num;
-  const int pred = av1_get_spatial_seg_pred(cm, xd, &cdf_num);
+  const int pred = av1_get_spatial_seg_pred(cm, xd, &cdf_num,
+                                            cpi->cyclic_refresh->skip_over4x4);
   const int mi_row = xd->mi_row;
   const int mi_col = xd->mi_col;
 
diff --git a/av1/encoder/encodeframe_utils.c b/av1/encoder/encodeframe_utils.c
index dd45586..fdfddcb 100644
--- a/av1/encoder/encodeframe_utils.c
+++ b/av1/encoder/encodeframe_utils.c
@@ -324,7 +324,8 @@
 
     if (!dry_run && !mi_addr->skip_txfm) {
       int cdf_num;
-      const int spatial_pred = av1_get_spatial_seg_pred(cm, xd, &cdf_num);
+      const int spatial_pred = av1_get_spatial_seg_pred(
+          cm, xd, &cdf_num, cpi->cyclic_refresh->skip_over4x4);
       const int coded_id = av1_neg_interleave(mi_addr->segment_id, spatial_pred,
                                               seg->last_active_segid + 1);
       int64_t spatial_cost = x->mode_costs.spatial_pred_cost[cdf_num][coded_id];
diff --git a/av1/encoder/partition_search.c b/av1/encoder/partition_search.c
index 67733eb..4dd47e0 100644
--- a/av1/encoder/partition_search.c
+++ b/av1/encoder/partition_search.c
@@ -2126,7 +2126,7 @@
     if (tile_data->allow_update_cdf) update_stats(&cpi->common, td);
   }
   if (cpi->oxcf.q_cfg.aq_mode == CYCLIC_REFRESH_AQ && mbmi->skip_txfm &&
-      !cpi->rc.rtc_external_ratectrl)
+      !cpi->rc.rtc_external_ratectrl && cm->seg.enabled)
     av1_cyclic_reset_segment_skip(cpi, x, mi_row, mi_col, bsize);
   // TODO(Ravi/Remya): Move this copy function to a better logical place
   // This function will copy the best mode information from block