fix issues with segment_pred_last

* Proper re-allocation of seg_map buffer when resolution changes
* Remove undesired resetting of seg_map buffer

* This resolves issues raised in

   https://bugs.chromium.org/p/aomedia/issues/detail?id=1064
   https://bugs.chromium.org/p/aomedia/issues/detail?id=1091

* Disable error_resilient_flag in resize_test as this covers more cases

* Will be enabled by default in separate patch

BUG=aomedia:1064
BUG=aomedia:1091

Change-Id: Ic3f0e47922784c66d240b52d30ba082bdb46dc3b
diff --git a/av1/common/entropymode.c b/av1/common/entropymode.c
index 681b56f..0701674 100644
--- a/av1/common/entropymode.c
+++ b/av1/common/entropymode.c
@@ -3407,8 +3407,12 @@
   av1_clearall_segfeatures(&cm->seg);
 
   if (cm->last_frame_seg_map && !cm->frame_parallel_decode)
+#if !CONFIG_SEGMENT_PRED_LAST
     memset(cm->last_frame_seg_map, 0, (cm->mi_rows * cm->mi_cols));
-
+#else
+    memset(cm->last_frame_seg_map, 0,
+           (cm->prev_frame->mi_cols * cm->prev_frame->mi_rows));
+#endif
   if (cm->current_frame_seg_map)
     memset(cm->current_frame_seg_map, 0, (cm->mi_rows * cm->mi_cols));
 
diff --git a/av1/common/onyxc_int.h b/av1/common/onyxc_int.h
index 1dbaa0a..c02a5d7 100644
--- a/av1/common/onyxc_int.h
+++ b/av1/common/onyxc_int.h
@@ -685,6 +685,12 @@
         cm, buf->mvs,
         (MV_REF *)aom_calloc(cm->mi_rows * cm->mi_cols, sizeof(*buf->mvs)));
 #endif  // CONFIG_TMV
+#if CONFIG_SEGMENT_PRED_LAST
+    aom_free(buf->seg_map);
+    CHECK_MEM_ERROR(cm, buf->seg_map,
+                    (uint8_t *)aom_calloc(cm->mi_rows * cm->mi_cols,
+                                          sizeof(*buf->seg_map)));
+#endif
   }
 
 #if CONFIG_MFMV
diff --git a/av1/decoder/decodeframe.c b/av1/decoder/decodeframe.c
index 93e1893..9a89c44 100644
--- a/av1/decoder/decodeframe.c
+++ b/av1/decoder/decodeframe.c
@@ -751,7 +751,10 @@
 
   seg->enabled = aom_rb_read_bit(rb);
   if (!seg->enabled) return;
-
+#if CONFIG_SEGMENT_PRED_LAST
+  if (cm->seg.enabled && !cm->frame_parallel_decode && cm->prev_frame)
+    cm->last_frame_seg_map = cm->prev_frame->seg_map;
+#endif
   // Segmentation map update
   if (frame_is_intra_only(cm) || cm->error_resilient_mode) {
     seg->update_map = 1;
@@ -1157,16 +1160,7 @@
   }
 }
 #endif  // CONFIG_HORZONLY_FRAME_SUPERRES
-#if CONFIG_SEGMENT_PRED_LAST
-static void resize_segmap_buffer(AV1_COMMON *cm) {
-  aom_free(cm->cur_frame->seg_map);
-  cm->cur_frame->mi_rows = cm->mi_rows;
-  cm->cur_frame->mi_cols = cm->mi_cols;
-  CHECK_MEM_ERROR(cm, cm->cur_frame->seg_map,
-                  (uint8_t *)aom_calloc(cm->mi_rows * cm->mi_cols,
-                                        sizeof(*cm->cur_frame->seg_map)));
-}
-#endif
+
 static void resize_context_buffers(AV1_COMMON *cm, int width, int height) {
 #if CONFIG_SIZE_LIMIT
   if (width > DECODE_WIDTH_LIMIT || height > DECODE_HEIGHT_LIMIT)
@@ -1195,12 +1189,6 @@
   }
 
   ensure_mv_buffer(cm->cur_frame, cm);
-#if CONFIG_SEGMENT_PRED_LAST
-  if (cm->cur_frame->seg_map == NULL || cm->mi_rows > cm->cur_frame->mi_rows ||
-      cm->mi_cols > cm->cur_frame->mi_cols) {
-    resize_segmap_buffer(cm);
-  }
-#endif
   cm->cur_frame->width = cm->width;
   cm->cur_frame->height = cm->height;
 }
@@ -2808,6 +2796,10 @@
               : NULL;
       cm->use_prev_frame_mvs =
           cm->use_ref_frame_mvs && frame_can_use_prev_frame_mvs(cm);
+#if CONFIG_SEGMENT_PRED_LAST
+      if (cm->seg.enabled && !cm->frame_parallel_decode)
+        cm->last_frame_seg_map = cm->prev_frame->seg_map;
+#endif
 #endif
       for (int i = 0; i < INTER_REFS_PER_FRAME; ++i) {
         RefBuffer *const ref_buf = &cm->frame_refs[i];
@@ -3281,11 +3273,6 @@
 
 #if CONFIG_SEGMENT_PRED_LAST
   cm->current_frame_seg_map = cm->cur_frame->seg_map;
-  if (cm->current_frame_seg_map)
-    memset(cm->current_frame_seg_map, 0, (cm->mi_rows * cm->mi_cols));
-  if (cm->seg.temporal_update) {
-    cm->last_frame_seg_map = cm->prev_frame->seg_map;
-  }
 #endif
 
 #if CONFIG_MFMV
diff --git a/av1/encoder/encodeframe.c b/av1/encoder/encodeframe.c
index 8e5a9a7..d8a7b98 100644
--- a/av1/encoder/encodeframe.c
+++ b/av1/encoder/encodeframe.c
@@ -3870,8 +3870,6 @@
 #if CONFIG_SEGMENT_PRED_LAST
   if (cm->prev_frame) cm->last_frame_seg_map = cm->prev_frame->seg_map;
   cm->current_frame_seg_map = cm->cur_frame->seg_map;
-  if (cm->current_frame_seg_map)
-    memset(cm->current_frame_seg_map, 0, (cm->mi_rows * cm->mi_cols));
 #endif
 
   // Special case: set prev_mi to NULL when the previous mode info
diff --git a/av1/encoder/encoder.c b/av1/encoder/encoder.c
index db86116..29a2e4d 100644
--- a/av1/encoder/encoder.c
+++ b/av1/encoder/encoder.c
@@ -4550,21 +4550,6 @@
   new_fb_ptr->height = cm->height;
 }
 
-#if CONFIG_SEGMENT_PRED_LAST
-static INLINE void alloc_frame_segmap(AV1_COMMON *const cm, int buffer_idx) {
-  RefCntBuffer *const new_fb_ptr = &cm->buffer_pool->frame_bufs[buffer_idx];
-  if (new_fb_ptr->seg_map == NULL || new_fb_ptr->mi_rows < cm->mi_rows ||
-      new_fb_ptr->mi_cols < cm->mi_cols) {
-    aom_free(new_fb_ptr->seg_map);
-    CHECK_MEM_ERROR(cm, new_fb_ptr->seg_map,
-                    (uint8_t *)aom_calloc(cm->mi_rows * cm->mi_cols,
-                                          sizeof(*new_fb_ptr->seg_map)));
-    new_fb_ptr->mi_rows = cm->mi_rows;
-    new_fb_ptr->mi_cols = cm->mi_cols;
-  }
-}
-#endif
-
 static void scale_references(AV1_COMP *cpi) {
   AV1_COMMON *cm = &cpi->common;
   MV_REFERENCE_FRAME ref_frame;
@@ -4607,9 +4592,6 @@
                                       (int)cm->bit_depth);
           cpi->scaled_ref_idx[ref_frame - 1] = new_fb;
           alloc_frame_mvs(cm, new_fb);
-#if CONFIG_SEGMENT_PRED_LAST
-          alloc_frame_segmap(cm, new_fb);
-#endif
         }
       } else {
         const int buf_idx = get_ref_frame_buf_idx(cpi, ref_frame);
@@ -4924,9 +4906,6 @@
 #endif
 
   alloc_frame_mvs(cm, cm->new_fb_idx);
-#if CONFIG_SEGMENT_PRED_LAST
-  alloc_frame_segmap(cm, cm->new_fb_idx);
-#endif
 
   // Reset the frame pointers to the current frame size.
   if (aom_realloc_frame_buffer(get_frame_new_buffer(cm), cm->width, cm->height,
diff --git a/test/resize_test.cc b/test/resize_test.cc
index 3e78c05..0b0915e 100644
--- a/test/resize_test.cc
+++ b/test/resize_test.cc
@@ -483,8 +483,8 @@
     cfg_.kf_min_dist = cfg_.kf_max_dist = 3000;
     // Enable dropped frames.
     cfg_.rc_dropframe_thresh = 1;
-    // Enable error_resilience mode.
-    cfg_.g_error_resilient = 1;
+    // Disable error_resilience mode.
+    cfg_.g_error_resilient = 0;
     // Run at low bitrate.
     cfg_.rc_target_bitrate = 200;
   }