Turn on the max_tile experiment

max_tile was provisionally adopted at the working group meeting 2017-Oct-10

This patch also enables support for 64x64 and 128x128 superblock size for max tile
(rather than assuming 128). There is also one fix for max_tile in combination of
loop restoration where the width/height was in the wrong units for max-tile specific code.

Change-Id: Icb862a2738fea5fc6215819396e1afa4eb86e461
diff --git a/av1/common/onyxc_int.h b/av1/common/onyxc_int.h
index 24686c2..6a2fc4e 100644
--- a/av1/common/onyxc_int.h
+++ b/av1/common/onyxc_int.h
@@ -439,6 +439,9 @@
 #if CONFIG_DEPENDENT_HORZTILES
   int tile_row_independent[MAX_TILE_ROWS];  // valid for 0 <= i <  tile_rows
 #endif
+#if CONFIG_EXT_TILE
+  int tile_width, tile_height;  // In MI units
+#endif
 #else
   int log2_tile_cols, log2_tile_rows;  // Used in non-large_scale_tile_coding.
   int tile_width, tile_height;         // In MI units
diff --git a/av1/common/restoration.c b/av1/common/restoration.c
index 0373f2b..938dd96 100644
--- a/av1/common/restoration.c
+++ b/av1/common/restoration.c
@@ -48,26 +48,6 @@
 #endif
 };
 
-#if CONFIG_MAX_TILE
-static void tile_width_and_height(const AV1_COMMON *cm, int is_uv, int sb_w,
-                                  int sb_h, int *px_w, int *px_h) {
-  const int scaled_sb_w = sb_w << MAX_MIB_SIZE_LOG2;
-  const int scaled_sb_h = sb_h << MAX_MIB_SIZE_LOG2;
-
-  const int ss_x = is_uv && cm->subsampling_x;
-  const int ss_y = is_uv && cm->subsampling_y;
-
-  *px_w = (scaled_sb_w + ss_x) >> ss_x;
-  *px_h = (scaled_sb_h + ss_y) >> ss_y;
-#if CONFIG_FRAME_SUPERRES
-  if (!av1_superres_unscaled(cm)) {
-    av1_calculate_unscaled_superres_size(px_w, px_h,
-                                         cm->superres_scale_denominator);
-  }
-#endif  // CONFIG_FRAME_SUPERRES
-}
-#endif  // CONFIG_MAX_TILE
-
 // Count horizontal or vertical units per tile (use a width or height for
 // tile_size, respectively). We basically want to divide the tile size by the
 // size of a restoration unit. Rather than rounding up unconditionally as you
@@ -87,30 +67,37 @@
   // top-left and we can use av1_get_tile_rect. With CONFIG_MAX_TILE, we have
   // to do the computation ourselves, iterating over the tiles and keeping
   // track of the largest width and height, then upscaling.
-  int max_sb_w = 0;
-  int max_sb_h = 0;
+  TileInfo tile;
+  int max_mi_w = 0;
+  int max_mi_h = 0;
+  int tile_col = 0;
+  int tile_row = 0;
   for (int i = 0; i < cm->tile_cols; ++i) {
-    const int sb_w = cm->tile_col_start_sb[i + 1] - cm->tile_col_start_sb[i];
-    max_sb_w = AOMMAX(max_sb_w, sb_w);
+    av1_tile_set_col(&tile, cm, i);
+    if (tile.mi_col_end - tile.mi_col_start > max_mi_w) {
+      max_mi_w = tile.mi_col_end - tile.mi_col_start;
+      tile_col = i;
+    }
   }
   for (int i = 0; i < cm->tile_rows; ++i) {
-    const int sb_h = cm->tile_row_start_sb[i + 1] - cm->tile_row_start_sb[i];
-    max_sb_h = AOMMAX(max_sb_h, sb_h);
+    av1_tile_set_row(&tile, cm, i);
+    if (tile.mi_row_end - tile.mi_row_start > max_mi_h) {
+      max_mi_h = tile.mi_row_end - tile.mi_row_start;
+      tile_row = i;
+    }
   }
-
-  int max_tile_w, max_tile_h;
-  tile_width_and_height(cm, is_uv, max_sb_w, max_sb_h, &max_tile_w,
-                        &max_tile_h);
+  TileInfo tile_info;
+  av1_tile_init(&tile_info, cm, tile_row, tile_col);
 #else
   TileInfo tile_info;
   av1_tile_init(&tile_info, cm, 0, 0);
+#endif  // CONFIG_MAX_TILE
 
   const AV1PixelRect tile_rect = av1_get_tile_rect(&tile_info, cm, is_uv);
   assert(tile_rect.left == 0 && tile_rect.top == 0);
 
   const int max_tile_w = tile_rect.right;
   const int max_tile_h = tile_rect.bottom;
-#endif  // CONFIG_MAX_TILE
 
   // To calculate hpertile and vpertile (horizontal and vertical units per
   // tile), we basically want to divide the largest tile width or height by the
@@ -1553,8 +1540,8 @@
 // For a vertical index, mi_x should be the block's top row and tile_x_start_sb
 // should be cm->tile_row_start_sb. The return value will be "tile_row" for the
 // tile containing the block.
-static int get_tile_idx(const int *tile_x_start_sb, int mi_x) {
-  int sb_x = mi_x << MAX_MIB_SIZE_LOG2;
+static int get_tile_idx(const int *tile_x_start_sb, int mi_x, int mib_log2) {
+  int sb_x = mi_x >> mib_log2;
 
   for (int i = 0; i < MAX_TILE_COLS; ++i) {
     if (tile_x_start_sb[i + 1] > sb_x) return i;
@@ -1580,22 +1567,13 @@
 // Which tile contains the superblock? Find that tile's top-left in mi-units,
 // together with the tile's size in pixels.
 #if CONFIG_MAX_TILE
-  const int tile_row = get_tile_idx(cm->tile_row_start_sb, mi_row);
-  const int tile_col = get_tile_idx(cm->tile_col_start_sb, mi_col);
-
-  const int sb_t = cm->tile_row_start_sb[tile_row];
-  const int sb_l = cm->tile_col_start_sb[tile_col];
-  const int sb_b = cm->tile_row_start_sb[tile_row + 1];
-  const int sb_r = cm->tile_col_start_sb[tile_col + 1];
-
-  int tile_w, tile_h;
-  tile_width_and_height(cm, is_uv, sb_r - sb_l, sb_t - sb_b, &tile_w, &tile_h);
-
-  const int mi_top = sb_t << MAX_MIB_SIZE_LOG2;
-  const int mi_left = sb_l << MAX_MIB_SIZE_LOG2;
+  const int mib_log2 = cm->mib_size_log2;
+  const int tile_row = get_tile_idx(cm->tile_row_start_sb, mi_row, mib_log2);
+  const int tile_col = get_tile_idx(cm->tile_col_start_sb, mi_col, mib_log2);
 #else
   const int tile_row = mi_row / cm->tile_height;
   const int tile_col = mi_col / cm->tile_width;
+#endif  // CONFIG_MAX_TILE
 
   TileInfo tile_info;
   av1_tile_init(&tile_info, cm, tile_row, tile_col);
@@ -1606,7 +1584,6 @@
 
   const int mi_top = tile_info.mi_row_start;
   const int mi_left = tile_info.mi_col_start;
-#endif  // CONFIG_MAX_TILE
 
   // Compute the mi-unit corners of the superblock relative to the top-left of
   // the tile
diff --git a/av1/common/tile_common.c b/av1/common/tile_common.c
index 64e9ee6..60b8e39 100644
--- a/av1/common/tile_common.c
+++ b/av1/common/tile_common.c
@@ -49,10 +49,10 @@
 }
 
 void av1_get_tile_limits(AV1_COMMON *const cm) {
-  int mi_cols = ALIGN_POWER_OF_TWO(cm->mi_cols, MAX_MIB_SIZE_LOG2);
-  int mi_rows = ALIGN_POWER_OF_TWO(cm->mi_rows, MAX_MIB_SIZE_LOG2);
-  int sb_cols = mi_cols >> MAX_MIB_SIZE_LOG2;
-  int sb_rows = mi_rows >> MAX_MIB_SIZE_LOG2;
+  int mi_cols = ALIGN_POWER_OF_TWO(cm->mi_cols, cm->mib_size_log2);
+  int mi_rows = ALIGN_POWER_OF_TWO(cm->mi_rows, cm->mib_size_log2);
+  int sb_cols = mi_cols >> cm->mib_size_log2;
+  int sb_rows = mi_rows >> cm->mib_size_log2;
 
   cm->min_log2_tile_cols = tile_log2(MAX_TILE_WIDTH_SB, sb_cols);
   cm->max_log2_tile_cols = tile_log2(1, AOMMIN(sb_cols, MAX_TILE_COLS));
@@ -64,10 +64,10 @@
 }
 
 void av1_calculate_tile_cols(AV1_COMMON *const cm) {
-  int mi_cols = ALIGN_POWER_OF_TWO(cm->mi_cols, MAX_MIB_SIZE_LOG2);
-  int mi_rows = ALIGN_POWER_OF_TWO(cm->mi_rows, MAX_MIB_SIZE_LOG2);
-  int sb_cols = mi_cols >> MAX_MIB_SIZE_LOG2;
-  int sb_rows = mi_rows >> MAX_MIB_SIZE_LOG2;
+  int mi_cols = ALIGN_POWER_OF_TWO(cm->mi_cols, cm->mib_size_log2);
+  int mi_rows = ALIGN_POWER_OF_TWO(cm->mi_rows, cm->mib_size_log2);
+  int sb_cols = mi_cols >> cm->mib_size_log2;
+  int sb_rows = mi_rows >> cm->mib_size_log2;
   int i;
 
   if (cm->uniform_tile_spacing_flag) {
@@ -85,7 +85,7 @@
     cm->max_tile_height_sb = sb_rows >> cm->min_log2_tile_rows;
   } else {
     int max_tile_area_sb = (sb_rows * sb_cols);
-    int max_tile_width_sb = 0;
+    int max_tile_width_sb = 1;
     cm->log2_tile_cols = tile_log2(1, cm->tile_cols);
     for (i = 0; i < cm->tile_cols; i++) {
       int size_sb = cm->tile_col_start_sb[i + 1] - cm->tile_col_start_sb[i];
@@ -99,8 +99,8 @@
 }
 
 void av1_calculate_tile_rows(AV1_COMMON *const cm) {
-  int mi_rows = ALIGN_POWER_OF_TWO(cm->mi_rows, MAX_MIB_SIZE_LOG2);
-  int sb_rows = mi_rows >> MAX_MIB_SIZE_LOG2;
+  int mi_rows = ALIGN_POWER_OF_TWO(cm->mi_rows, cm->mib_size_log2);
+  int sb_rows = mi_rows >> cm->mib_size_log2;
   int start_sb, size_sb, i;
 
   if (cm->uniform_tile_spacing_flag) {
@@ -131,16 +131,16 @@
 
 void av1_tile_set_row(TileInfo *tile, const AV1_COMMON *cm, int row) {
   assert(row < cm->tile_rows);
-  int mi_row_start = cm->tile_row_start_sb[row] << MAX_MIB_SIZE_LOG2;
-  int mi_row_end = cm->tile_row_start_sb[row + 1] << MAX_MIB_SIZE_LOG2;
+  int mi_row_start = cm->tile_row_start_sb[row] << cm->mib_size_log2;
+  int mi_row_end = cm->tile_row_start_sb[row + 1] << cm->mib_size_log2;
   tile->mi_row_start = mi_row_start;
   tile->mi_row_end = AOMMIN(mi_row_end, cm->mi_rows);
 }
 
 void av1_tile_set_col(TileInfo *tile, const AV1_COMMON *cm, int col) {
   assert(col < cm->tile_cols);
-  int mi_col_start = cm->tile_col_start_sb[col] << MAX_MIB_SIZE_LOG2;
-  int mi_col_end = cm->tile_col_start_sb[col + 1] << MAX_MIB_SIZE_LOG2;
+  int mi_col_start = cm->tile_col_start_sb[col] << cm->mib_size_log2;
+  int mi_col_end = cm->tile_col_start_sb[col + 1] << cm->mib_size_log2;
   tile->mi_col_start = mi_col_start;
   tile->mi_col_end = AOMMIN(mi_col_end, cm->mi_cols);
 }
diff --git a/av1/decoder/decodeframe.c b/av1/decoder/decodeframe.c
index de589fc..022b9b4 100644
--- a/av1/decoder/decodeframe.c
+++ b/av1/decoder/decodeframe.c
@@ -1673,10 +1673,10 @@
 
 static void read_tile_info_max_tile(AV1_COMMON *const cm,
                                     struct aom_read_bit_buffer *const rb) {
-  int width_mi = ALIGN_POWER_OF_TWO(cm->mi_cols, MAX_MIB_SIZE_LOG2);
-  int height_mi = ALIGN_POWER_OF_TWO(cm->mi_rows, MAX_MIB_SIZE_LOG2);
-  int width_sb = width_mi >> MAX_MIB_SIZE_LOG2;
-  int height_sb = height_mi >> MAX_MIB_SIZE_LOG2;
+  int width_mi = ALIGN_POWER_OF_TWO(cm->mi_cols, cm->mib_size_log2);
+  int height_mi = ALIGN_POWER_OF_TWO(cm->mi_rows, cm->mib_size_log2);
+  int width_sb = width_mi >> cm->mib_size_log2;
+  int height_sb = height_mi >> cm->mib_size_log2;
   int start_sb, size_sb, i;
 
   av1_get_tile_limits(cm);
diff --git a/av1/decoder/inspection.c b/av1/decoder/inspection.c
index 56e0c28..beb6db7 100644
--- a/av1/decoder/inspection.c
+++ b/av1/decoder/inspection.c
@@ -48,8 +48,10 @@
   fd->show_frame = cm->show_frame;
   fd->frame_type = cm->frame_type;
   fd->base_qindex = cm->base_qindex;
+#if !CONFIG_MAX_TILE
   fd->tile_mi_cols = cm->tile_width;
   fd->tile_mi_rows = cm->tile_height;
+#endif
   fd->delta_q_present_flag = cm->delta_q_present_flag;
   fd->delta_q_res = cm->delta_q_res;
 #if CONFIG_ACCOUNTING
diff --git a/av1/decoder/inspection.h b/av1/decoder/inspection.h
index bd9d87a..c9e5c19 100644
--- a/av1/decoder/inspection.h
+++ b/av1/decoder/inspection.h
@@ -72,8 +72,10 @@
   int base_qindex;
   int mi_rows;
   int mi_cols;
+#if !CONFIG_MAX_TILE
   int tile_mi_rows;
   int tile_mi_cols;
+#endif
   int16_t y_dequant[MAX_SEGMENTS][2];
   int16_t uv_dequant[MAX_SEGMENTS][2];
 #if CONFIG_CDEF
diff --git a/av1/encoder/bitstream.c b/av1/encoder/bitstream.c
index 804c249..e180030 100644
--- a/av1/encoder/bitstream.c
+++ b/av1/encoder/bitstream.c
@@ -2826,10 +2826,10 @@
 
 static void write_tile_info_max_tile(const AV1_COMMON *const cm,
                                      struct aom_write_bit_buffer *wb) {
-  int width_mi = ALIGN_POWER_OF_TWO(cm->mi_cols, MAX_MIB_SIZE_LOG2);
-  int height_mi = ALIGN_POWER_OF_TWO(cm->mi_rows, MAX_MIB_SIZE_LOG2);
-  int width_sb = width_mi >> MAX_MIB_SIZE_LOG2;
-  int height_sb = height_mi >> MAX_MIB_SIZE_LOG2;
+  int width_mi = ALIGN_POWER_OF_TWO(cm->mi_cols, cm->mib_size_log2);
+  int height_mi = ALIGN_POWER_OF_TWO(cm->mi_rows, cm->mib_size_log2);
+  int width_sb = width_mi >> cm->mib_size_log2;
+  int height_sb = height_mi >> cm->mib_size_log2;
   int size_sb, i;
 
   aom_wb_write_bit(wb, cm->uniform_tile_spacing_flag);
diff --git a/av1/encoder/encoder.c b/av1/encoder/encoder.c
index 5d6ab41..1ba3096 100644
--- a/av1/encoder/encoder.c
+++ b/av1/encoder/encoder.c
@@ -310,10 +310,14 @@
 
   assert(cpi->oxcf.superblock_size == AOM_SUPERBLOCK_SIZE_DYNAMIC);
 
+#if !CONFIG_MAX_TILE
+  // for the max_tile experiment there is no common tile_width, tile_height
+  // max_tile assumes tile dimensions are in superblocks (not 64x64 units)
   assert(IMPLIES(cpi->common.tile_cols > 1,
                  cpi->common.tile_width % MAX_MIB_SIZE == 0));
   assert(IMPLIES(cpi->common.tile_rows > 1,
                  cpi->common.tile_height % MAX_MIB_SIZE == 0));
+#endif
 
   // TODO(any): Possibly could improve this with a heuristic.
   return BLOCK_128X128;
@@ -878,6 +882,11 @@
   AV1_COMMON *const cm = &cpi->common;
   int i, start_sb;
 
+  // Ensure superblock size is set before we read tile size in superblocks
+  if (cm->frame_type == KEY_FRAME) {
+    set_sb_size(cm, select_sb_size(cpi));
+  }
+
   av1_get_tile_limits(cm);
 
   // configure tile columns
@@ -886,8 +895,8 @@
     cm->log2_tile_cols = AOMMAX(cpi->oxcf.tile_columns, cm->min_log2_tile_cols);
     cm->log2_tile_cols = AOMMIN(cm->log2_tile_cols, cm->max_log2_tile_cols);
   } else {
-    int mi_cols = ALIGN_POWER_OF_TWO(cm->mi_cols, MAX_MIB_SIZE_LOG2);
-    int sb_cols = mi_cols >> MAX_MIB_SIZE_LOG2;
+    int mi_cols = ALIGN_POWER_OF_TWO(cm->mi_cols, cm->mib_size_log2);
+    int sb_cols = mi_cols >> cm->mib_size_log2;
     int size_sb, j = 0;
     cm->uniform_tile_spacing_flag = 0;
     for (i = 0, start_sb = 0; start_sb < sb_cols && i < MAX_TILE_COLS; i++) {
@@ -906,8 +915,8 @@
     cm->log2_tile_rows = AOMMAX(cpi->oxcf.tile_rows, cm->min_log2_tile_rows);
     cm->log2_tile_rows = AOMMIN(cm->log2_tile_rows, cm->max_log2_tile_rows);
   } else {
-    int mi_rows = ALIGN_POWER_OF_TWO(cm->mi_rows, MAX_MIB_SIZE_LOG2);
-    int sb_rows = mi_rows >> MAX_MIB_SIZE_LOG2;
+    int mi_rows = ALIGN_POWER_OF_TWO(cm->mi_rows, cm->mib_size_log2);
+    int sb_rows = mi_rows >> cm->mib_size_log2;
     int size_sb, j = 0;
     for (i = 0, start_sb = 0; start_sb < sb_rows && i < MAX_TILE_ROWS; i++) {
       cm->tile_row_start_sb[i] = start_sb;
diff --git a/build/cmake/aom_config_defaults.cmake b/build/cmake/aom_config_defaults.cmake
index 28a5e63..a7d5164 100644
--- a/build/cmake/aom_config_defaults.cmake
+++ b/build/cmake/aom_config_defaults.cmake
@@ -164,7 +164,7 @@
 set(CONFIG_LPF_SB 0 CACHE NUMBER "AV1 experiment flag.")
 set(CONFIG_LV_MAP 0 CACHE NUMBER "AV1 experiment flag.")
 set(CONFIG_MASKED_TX 0 CACHE NUMBER "AV1 experiment flag.")
-set(CONFIG_MAX_TILE 0 CACHE NUMBER "AV1 experiment flag.")
+set(CONFIG_MAX_TILE 1 CACHE NUMBER "AV1 experiment flag.")
 set(CONFIG_MFMV 0 CACHE NUMBER "AV1 experiment flag.")
 set(CONFIG_MRC_TX 0 CACHE NUMBER "AV1 experiment flag.")
 set(CONFIG_MV_COMPRESS 1 CACHE NUMBER "AV1 experiment flag.")
diff --git a/configure b/configure
index d6f5a51..509bb76 100755
--- a/configure
+++ b/configure
@@ -520,6 +520,7 @@
       soft_enable ext_delta_q
       soft_enable parallel_deblocking
       soft_enable simple_bwd_adapt
+      soft_enable max_tile
       soft_enable loopfilter_level
       soft_enable cfl
       soft_enable deblock_13tap
diff --git a/examples/inspect.c b/examples/inspect.c
index 1f5abfa..8d6bdb7 100644
--- a/examples/inspect.c
+++ b/examples/inspect.c
@@ -618,10 +618,12 @@
                   frame_data.frame_type);
   buf += snprintf(buf, MAX_BUFFER, "  \"baseQIndex\": %d,\n",
                   frame_data.base_qindex);
+#if !CONFIG_MAX_TILE
   buf += snprintf(buf, MAX_BUFFER, "  \"tileCols\": %d,\n",
                   frame_data.tile_mi_cols);
   buf += snprintf(buf, MAX_BUFFER, "  \"tileRows\": %d,\n",
                   frame_data.tile_mi_rows);
+#endif
   buf += snprintf(buf, MAX_BUFFER, "  \"deltaQPresentFlag\": %d,\n",
                   frame_data.delta_q_present_flag);
   buf += snprintf(buf, MAX_BUFFER, "  \"deltaQRes\": %d,\n",