Move tile_cols/tile_rows calculation into common function

This is just a code cleanup, moving the calculation of cm->tile_rows
and cm->tile_cols into get_tile_size, which is already calculating
cm->tile_width and cm->tile_height. The patch also gets rid of a
spurious AOMMIN and replaces it with an assertion.

Change-Id: I46666c4a197ac26d4b3746d9ea6575dc4af0d570
diff --git a/av1/common/tile_common.c b/av1/common/tile_common.c
index c3dedbf..5d7f778 100644
--- a/av1/common/tile_common.c
+++ b/av1/common/tile_common.c
@@ -218,15 +218,27 @@
   }
 }
 
-int get_tile_size(int frame_mi_size, int log2_tile_num) {
+int get_tile_size(int mi_frame_size, int log2_tile_num, int *ntiles) {
   // Round the frame up to a whole number of max superblocks
-  frame_mi_size = ALIGN_POWER_OF_TWO(frame_mi_size, MAX_MIB_SIZE_LOG2);
-  // Divide by the number of tiles, rounding up to the multiple of the max
-  // superblock size. To do this, shift right (and round up) to get the number
-  // of super-blocks and then shift left again to convert it to mi units.
-  const int shift = log2_tile_num + MAX_MIB_SIZE_LOG2;
-  const int round = (1 << shift) - 1;
-  return ((frame_mi_size + round) >> shift) << MAX_MIB_SIZE_LOG2;
+  mi_frame_size = ALIGN_POWER_OF_TWO(mi_frame_size, MAX_MIB_SIZE_LOG2);
+
+  // Divide by the signalled number of tiles, rounding up to the multiple of
+  // the max superblock size. To do this, shift right (and round up) to get the
+  // tile size in max super-blocks and then shift left again to convert it to
+  // mi units.
+  const int max_sb_tile_size =
+      ROUND_POWER_OF_TWO(mi_frame_size, log2_tile_num + MAX_MIB_SIZE_LOG2);
+  const int mi_tile_size = max_sb_tile_size << MAX_MIB_SIZE_LOG2;
+
+  // The actual number of tiles is the ceiling of the frame size in mi units
+  // divided by mi_size. This is at most 1 << log2_tile_num but might be
+  // strictly less if max_sb_tile_size got rounded up significantly.
+  if (ntiles) {
+    *ntiles = (mi_frame_size + mi_tile_size - 1) / mi_tile_size;
+    assert(*ntiles <= (1 << log2_tile_num));
+  }
+
+  return mi_tile_size;
 }
 
 #if CONFIG_LOOPFILTERING_ACROSS_TILES
diff --git a/av1/common/tile_common.h b/av1/common/tile_common.h
index a455957..be21e14 100644
--- a/av1/common/tile_common.h
+++ b/av1/common/tile_common.h
@@ -46,7 +46,7 @@
 
 // Calculate the correct tile size (width or height) for (1 << log2_tile_num)
 // tiles horizontally or vertically in the frame.
-int get_tile_size(int frame_mi_size, int log2_tile_num);
+int get_tile_size(int mi_frame_size, int log2_tile_num, int *ntiles);
 
 #if CONFIG_LOOPFILTERING_ACROSS_TILES
 void av1_setup_across_tile_boundary_info(const struct AV1Common *const cm,
diff --git a/av1/decoder/decodeframe.c b/av1/decoder/decodeframe.c
index fec406f..b4259c7 100644
--- a/av1/decoder/decodeframe.c
+++ b/av1/decoder/decodeframe.c
@@ -3352,14 +3352,11 @@
   cm->log2_tile_rows = aom_rb_read_bit(rb);
   if (cm->log2_tile_rows) cm->log2_tile_rows += aom_rb_read_bit(rb);
 
-  cm->tile_width = get_tile_size(cm->mi_cols, cm->log2_tile_cols);
-  cm->tile_height = get_tile_size(cm->mi_rows, cm->log2_tile_rows);
+  cm->tile_width =
+      get_tile_size(cm->mi_cols, cm->log2_tile_cols, &cm->tile_cols);
+  cm->tile_height =
+      get_tile_size(cm->mi_rows, cm->log2_tile_rows, &cm->tile_rows);
 
-  const int max_cols = (cm->mi_cols + cm->tile_width - 1) / cm->tile_width;
-  const int max_rows = (cm->mi_rows + cm->tile_height - 1) / cm->tile_height;
-
-  cm->tile_cols = AOMMIN(1 << cm->log2_tile_cols, max_cols);
-  cm->tile_rows = AOMMIN(1 << cm->log2_tile_rows, max_rows);
 #endif  // CONFIG_MAX_TILE
 #if CONFIG_DEPENDENT_HORZTILES
     if (cm->tile_rows > 1)
diff --git a/av1/encoder/encoder.c b/av1/encoder/encoder.c
index 9e2f96c..db09c40 100644
--- a/av1/encoder/encoder.c
+++ b/av1/encoder/encoder.c
@@ -969,14 +969,10 @@
       clamp(cpi->oxcf.tile_columns, min_log2_tile_cols, max_log2_tile_cols);
   cm->log2_tile_rows = cpi->oxcf.tile_rows;
 
-  cm->tile_width = get_tile_size(cm->mi_cols, cm->log2_tile_cols);
-  cm->tile_height = get_tile_size(cm->mi_rows, cm->log2_tile_rows);
-
-  const int max_cols = (cm->mi_cols + cm->tile_width - 1) / cm->tile_width;
-  const int max_rows = (cm->mi_rows + cm->tile_height - 1) / cm->tile_height;
-
-  cm->tile_cols = AOMMIN(1 << cm->log2_tile_cols, max_cols);
-  cm->tile_rows = AOMMIN(1 << cm->log2_tile_rows, max_rows);
+  cm->tile_width =
+      get_tile_size(cm->mi_cols, cm->log2_tile_cols, &cm->tile_cols);
+  cm->tile_height =
+      get_tile_size(cm->mi_rows, cm->log2_tile_rows, &cm->tile_rows);
 #endif  // CONFIG_MAX_TILE
 #if CONFIG_EXT_TILE
   }