Allow tile_rows/tile_cols to not be a power of 2

The example bitstream in bug 710 shows what might go on. This has a
frame with mi_cols=540 and log2_tile_cols=3. But 540/8 = 67.5, which
gets rounded up to give a tile_width of 5*16=80. However, 540/80 =
6.75 which means that the frame finishes somewhere in the middle of
tile 7 (and tile 8 is completely empty).

This patch clamps tile_rows/tile_cols to ensure that can't happen. The
bitstream still signals ceil(log2(tile_<rows/cols>)) so there's no
change there.

BUG=aomedia:710

Change-Id: Idfad658ef59cf71674100f5e74bd53dd192c6a84
diff --git a/av1/decoder/decodeframe.c b/av1/decoder/decodeframe.c
index ce49ef99..c7b013b 100644
--- a/av1/decoder/decodeframe.c
+++ b/av1/decoder/decodeframe.c
@@ -3207,9 +3207,6 @@
     cm->loop_filter_across_tiles_enabled = aom_rb_read_bit(rb);
 #endif  // CONFIG_LOOPFILTERING_ACROSS_TILES
 
-    cm->tile_cols = 1 << cm->log2_tile_cols;
-    cm->tile_rows = 1 << cm->log2_tile_rows;
-
     cm->tile_width = ALIGN_POWER_OF_TWO(cm->mi_cols, MAX_MIB_SIZE_LOG2);
     cm->tile_width >>= cm->log2_tile_cols;
     cm->tile_height = ALIGN_POWER_OF_TWO(cm->mi_rows, MAX_MIB_SIZE_LOG2);
@@ -3219,6 +3216,12 @@
     cm->tile_width = ALIGN_POWER_OF_TWO(cm->tile_width, MAX_MIB_SIZE_LOG2);
     cm->tile_height = ALIGN_POWER_OF_TWO(cm->tile_height, MAX_MIB_SIZE_LOG2);
 
+    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);
+
     // tile size magnitude
     pbi->tile_size_bytes = aom_rb_read_literal(rb, 2) + 1;
 #if CONFIG_EXT_TILE
diff --git a/av1/encoder/encoder.c b/av1/encoder/encoder.c
index d2b5e61..b1bb0f1 100644
--- a/av1/encoder/encoder.c
+++ b/av1/encoder/encoder.c
@@ -896,9 +896,6 @@
         clamp(cpi->oxcf.tile_columns, min_log2_tile_cols, max_log2_tile_cols);
     cm->log2_tile_rows = cpi->oxcf.tile_rows;
 
-    cm->tile_cols = 1 << cm->log2_tile_cols;
-    cm->tile_rows = 1 << cm->log2_tile_rows;
-
     cm->tile_width = ALIGN_POWER_OF_TWO(cm->mi_cols, MAX_MIB_SIZE_LOG2);
     cm->tile_width >>= cm->log2_tile_cols;
     cm->tile_height = ALIGN_POWER_OF_TWO(cm->mi_rows, MAX_MIB_SIZE_LOG2);
@@ -907,6 +904,12 @@
     // round to integer multiples of max superblock size
     cm->tile_width = ALIGN_POWER_OF_TWO(cm->tile_width, MAX_MIB_SIZE_LOG2);
     cm->tile_height = ALIGN_POWER_OF_TWO(cm->tile_height, MAX_MIB_SIZE_LOG2);
+
+    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);
 #if CONFIG_EXT_TILE
   }
 #endif  // CONFIG_EXT_TILE