Setup frame/tile boundary when frame/tile geometry changes

Change-Id: I44bc9d8887526a5ee92bf79730fa3ce6c73b160b
diff --git a/av1/common/onyxc_int.h b/av1/common/onyxc_int.h
index 6d3ff56..cc0f7c8 100644
--- a/av1/common/onyxc_int.h
+++ b/av1/common/onyxc_int.h
@@ -374,6 +374,7 @@
 
   int log2_tile_cols, log2_tile_rows;  // Used in non-large_scale_tile_coding.
   int tile_cols, tile_rows;
+  int last_tile_cols, last_tile_rows;
   int tile_width, tile_height;  // In MI units
 #if CONFIG_EXT_TILE
   unsigned int large_scale_tile;
diff --git a/av1/decoder/decodeframe.c b/av1/decoder/decodeframe.c
index 93788e1..4e8d99a 100644
--- a/av1/decoder/decodeframe.c
+++ b/av1/decoder/decodeframe.c
@@ -3701,6 +3701,15 @@
 }
 #endif  // #if CONFIG_PVQ
 
+static void dec_setup_across_tile_boundary_info(
+    const AV1_COMMON *const cm, const TileInfo *const tile_info) {
+  if (cm->width != cm->last_width || cm->height != cm->last_height ||
+      cm->tile_cols != cm->last_tile_cols ||
+      cm->tile_rows != cm->last_tile_rows) {
+    av1_setup_across_tile_boundary_info(cm, tile_info);
+  }
+}
+
 static const uint8_t *decode_tiles(AV1Decoder *pbi, const uint8_t *data,
                                    const uint8_t *data_end) {
   AV1_COMMON *const cm = &pbi->common;
@@ -3869,7 +3878,7 @@
       av1_zero_above_context(cm, tile_info.mi_col_start, tile_info.mi_col_end);
 #endif
 
-      av1_setup_across_tile_boundary_info(cm, &tile_info);
+      dec_setup_across_tile_boundary_info(cm, &tile_info);
 
       for (mi_row = tile_info.mi_row_start; mi_row < tile_info.mi_row_end;
            mi_row += cm->mib_size) {
@@ -4191,7 +4200,7 @@
         av1_tile_init(tile_info, cm, tile_row, buf->col);
         av1_tile_init(&twd->xd.tile, cm, tile_row, buf->col);
 
-        av1_setup_across_tile_boundary_info(cm, tile_info);
+        dec_setup_across_tile_boundary_info(cm, tile_info);
 
         setup_bool_decoder(buf->data, data_end, buf->size, &cm->error,
                            &twd->bit_reader,
@@ -5246,6 +5255,22 @@
 }
 #endif  // CONFIG_FRAME_SUPERRES
 
+static void dec_setup_frame_boundary_info(AV1_COMMON *const cm) {
+  if (cm->width != cm->last_width || cm->height != cm->last_height ||
+      cm->tile_cols != cm->last_tile_cols ||
+      cm->tile_rows != cm->last_tile_rows) {
+    int row, col;
+    for (row = 0; row < cm->mi_rows; ++row) {
+      MODE_INFO *mi = cm->mi + row * cm->mi_stride;
+      for (col = 0; col < cm->mi_cols; ++col) {
+        mi->mbmi.boundary_info = 0;
+        mi++;
+      }
+    }
+    av1_setup_frame_boundary_info(cm);
+  }
+}
+
 void av1_decode_frame(AV1Decoder *pbi, const uint8_t *data,
                       const uint8_t *data_end, const uint8_t **p_data_end) {
   AV1_COMMON *const cm = &pbi->common;
@@ -5404,7 +5429,7 @@
     av1_frameworker_unlock_stats(worker);
   }
 
-  av1_setup_frame_boundary_info(cm);
+  dec_setup_frame_boundary_info(cm);
 
   if (pbi->max_threads > 1 && !CONFIG_CB4X4 &&
 #if CONFIG_EXT_TILE
diff --git a/av1/decoder/decoder.c b/av1/decoder/decoder.c
index 3998c20..aaea636 100644
--- a/av1/decoder/decoder.c
+++ b/av1/decoder/decoder.c
@@ -492,6 +492,8 @@
   } else {
     cm->last_width = cm->width;
     cm->last_height = cm->height;
+    cm->last_tile_cols = cm->tile_cols;
+    cm->last_tile_rows = cm->tile_rows;
     if (cm->show_frame) {
       cm->current_video_frame++;
     }
diff --git a/av1/encoder/encoder.c b/av1/encoder/encoder.c
index 6fa0264..be5e1a3 100644
--- a/av1/encoder/encoder.c
+++ b/av1/encoder/encoder.c
@@ -4981,6 +4981,9 @@
     encode_with_recode_loop(cpi, size, dest);
   }
 
+  cm->last_tile_cols = cm->tile_cols;
+  cm->last_tile_rows = cm->tile_rows;
+
 #ifdef OUTPUT_YUV_SKINMAP
   if (cpi->common.current_video_frame > 1) {
     av1_compute_skin_map(cpi, yuv_skinmap_file);
@@ -5179,6 +5182,7 @@
 #if CONFIG_EXT_REFS
   }
 #endif  // CONFIG_EXT_REFS
+
   aom_free(tile_ctxs);
   aom_free(cdf_ptrs);
 }