rtc: Reduce memory allocation for mode info

In real time mode, min_partition_size used is BLOCK_8X8. So, we
don't need to have mode info (mi) in 4x4 precision. In this CL,
we allocate mi in 8x8 precision for rtc encoder, which largely
reduces encoder memory usage for all rt speeds (speed 5 - 10).

In average, the encoder memory reduction is 15.3% for 720p, 9%
for 360p, and 4.6% for 180p. No stats change is introduced.

Change-Id: I66c27b6713a501c39799ad1d10dba366e3d91548
diff --git a/av1/common/alloccommon.c b/av1/common/alloccommon.c
index 0b2d765..d2e01ea 100644
--- a/av1/common/alloccommon.c
+++ b/av1/common/alloccommon.c
@@ -476,15 +476,15 @@
   return 0;
 }
 
-int av1_alloc_context_buffers(AV1_COMMON *cm, int width, int height) {
+int av1_alloc_context_buffers(AV1_COMMON *cm, int width, int height, int mode) {
   CommonModeInfoParams *const mi_params = &cm->mi_params;
-  mi_params->set_mb_mi(mi_params, width, height);
+  mi_params->set_mb_mi(mi_params, width, height, mode);
   if (alloc_mi(mi_params)) goto fail;
   return 0;
 
 fail:
   // clear the mi_* values to force a realloc on resync
-  mi_params->set_mb_mi(mi_params, 0, 0);
+  mi_params->set_mb_mi(mi_params, 0, 0, 0);
   av1_free_context_buffers(cm);
   return 1;
 }
diff --git a/av1/common/alloccommon.h b/av1/common/alloccommon.h
index 147320b..fbfe639 100644
--- a/av1/common/alloccommon.h
+++ b/av1/common/alloccommon.h
@@ -33,7 +33,8 @@
                                     int num_tile_rows, int num_mi_cols,
                                     int num_planes);
 void av1_free_above_context_buffers(struct CommonContexts *above_contexts);
-int av1_alloc_context_buffers(struct AV1Common *cm, int width, int height);
+int av1_alloc_context_buffers(struct AV1Common *cm, int width, int height,
+                              int mode);
 void av1_init_mi_buffers(struct CommonModeInfoParams *mi_params);
 void av1_free_context_buffers(struct AV1Common *cm);
 
diff --git a/av1/common/av1_common_int.h b/av1/common/av1_common_int.h
index 5454a00..4d4286b 100644
--- a/av1/common/av1_common_int.h
+++ b/av1/common/av1_common_int.h
@@ -597,9 +597,10 @@
    * \param[in,out]   mi_params   object containing common mode info parameters
    * \param           width       frame width
    * \param           height      frame height
+   * \param           mode        encoding mode
    */
   void (*set_mb_mi)(struct CommonModeInfoParams *mi_params, int width,
-                    int height);
+                    int height, int mode);
   /**@}*/
 };
 
diff --git a/av1/decoder/decodeframe.c b/av1/decoder/decodeframe.c
index 08f81da..0341a7e 100644
--- a/av1/decoder/decodeframe.c
+++ b/av1/decoder/decodeframe.c
@@ -1901,7 +1901,7 @@
     // dimensions as well as the overall size.
     if (new_mi_cols > cm->mi_params.mi_cols ||
         new_mi_rows > cm->mi_params.mi_rows) {
-      if (av1_alloc_context_buffers(cm, width, height)) {
+      if (av1_alloc_context_buffers(cm, width, height, 0)) {
         // The cm->mi_* values have been cleared and any existing context
         // buffers have been freed. Clear cm->width and cm->height to be
         // consistent and to force a realloc next time.
@@ -1911,7 +1911,7 @@
                            "Failed to allocate context buffers");
       }
     } else {
-      cm->mi_params.set_mb_mi(&cm->mi_params, width, height);
+      cm->mi_params.set_mb_mi(&cm->mi_params, width, height, 0);
     }
     av1_init_mi_buffers(&cm->mi_params);
     cm->width = width;
diff --git a/av1/decoder/decoder.c b/av1/decoder/decoder.c
index 0ec85ee..ca7b7e2 100644
--- a/av1/decoder/decoder.c
+++ b/av1/decoder/decoder.c
@@ -45,7 +45,8 @@
 }
 
 static void dec_set_mb_mi(CommonModeInfoParams *mi_params, int width,
-                          int height) {
+                          int height, int mode) {
+  (void)mode;
   // Ensure that the decoded width and height are both multiples of
   // 8 luma pixels (note: this may only be a multiple of 4 chroma pixels if
   // subsampling is used).
diff --git a/av1/encoder/encoder.c b/av1/encoder/encoder.c
index 5e7b9a5..8a7d8bc 100644
--- a/av1/encoder/encoder.c
+++ b/av1/encoder/encoder.c
@@ -297,7 +297,7 @@
   MACROBLOCKD *const xd = &cpi->td.mb.e_mbd;
 
   // We need to reallocate the context buffers here in case we need more mis.
-  if (av1_alloc_context_buffers(cm, cm->width, cm->height)) {
+  if (av1_alloc_context_buffers(cm, cm->width, cm->height, cpi->oxcf.mode)) {
     aom_internal_error(cm->error, AOM_CODEC_MEM_ERROR,
                        "Failed to allocate context buffers");
   }
@@ -1162,8 +1162,8 @@
     // buffers. The values in this variable are populated according to initial
     // width and height of the frame.
     CommonModeInfoParams mi_params;
-    enc_set_mb_mi(&mi_params, oxcf->frm_dim_cfg.width,
-                  oxcf->frm_dim_cfg.height);
+    enc_set_mb_mi(&mi_params, oxcf->frm_dim_cfg.width, oxcf->frm_dim_cfg.height,
+                  oxcf->mode);
 
     const int bsize = BLOCK_16X16;
     const int w = mi_size_wide[bsize];
diff --git a/av1/encoder/encoder_alloc.h b/av1/encoder/encoder_alloc.h
index 157e009..0d43a98 100644
--- a/av1/encoder/encoder_alloc.h
+++ b/av1/encoder/encoder_alloc.h
@@ -58,7 +58,7 @@
   AV1_COMMON *cm = &cpi->common;
   TokenInfo *token_info = &cpi->token_info;
 
-  if (av1_alloc_context_buffers(cm, cm->width, cm->height)) {
+  if (av1_alloc_context_buffers(cm, cm->width, cm->height, cpi->oxcf.mode)) {
     aom_internal_error(cm->error, AOM_CODEC_MEM_ERROR,
                        "Failed to allocate context buffers");
   }
diff --git a/av1/encoder/encoder_utils.h b/av1/encoder/encoder_utils.h
index 999c9bf..15b3296 100644
--- a/av1/encoder/encoder_utils.h
+++ b/av1/encoder/encoder_utils.h
@@ -86,15 +86,18 @@
 }
 
 static AOM_INLINE void enc_set_mb_mi(CommonModeInfoParams *mi_params, int width,
-                                     int height) {
+                                     int height, int mode) {
   const int is_4k_or_larger = AOMMIN(width, height) >= 2160;
-  mi_params->mi_alloc_bsize = is_4k_or_larger ? BLOCK_8X8 : BLOCK_4X4;
+  const int is_realtime_mode = (mode == REALTIME);
+  mi_params->mi_alloc_bsize =
+      (is_4k_or_larger || is_realtime_mode) ? BLOCK_8X8 : BLOCK_4X4;
 
   set_mb_mi(mi_params, width, height);
 }
 
 static AOM_INLINE void stat_stage_set_mb_mi(CommonModeInfoParams *mi_params,
-                                            int width, int height) {
+                                            int width, int height, int mode) {
+  (void)mode;
   mi_params->mi_alloc_bsize = BLOCK_16X16;
 
   set_mb_mi(mi_params, width, height);
diff --git a/av1/encoder/speed_features.c b/av1/encoder/speed_features.c
index c6be672..335650f 100644
--- a/av1/encoder/speed_features.c
+++ b/av1/encoder/speed_features.c
@@ -1311,6 +1311,8 @@
   sf->mv_sf.subpel_search_method = SUBPEL_TREE_PRUNED;
   sf->mv_sf.search_method = boosted ? DIAMOND : FAST_DIAMOND;
 
+  sf->part_sf.default_max_partition_size = BLOCK_128X128;
+  sf->part_sf.default_min_partition_size = BLOCK_8X8;
   sf->part_sf.ml_prune_partition = 1;
   sf->part_sf.reuse_prev_rd_results_for_part_ab = 1;
   sf->part_sf.use_best_rd_for_pruning = 1;
@@ -1364,8 +1366,6 @@
                   FLAG_EARLY_TERMINATE;
     sf->hl_sf.frame_parameter_update = 0;
 
-    sf->part_sf.default_max_partition_size = BLOCK_128X128;
-    sf->part_sf.default_min_partition_size = BLOCK_8X8;
     sf->part_sf.max_intra_bsize = BLOCK_32X32;
     sf->part_sf.partition_search_breakout_rate_thr = 500;
     sf->part_sf.partition_search_type = VAR_BASED_PARTITION;
@@ -1447,8 +1447,6 @@
   }
 
   if (speed >= 7) {
-    sf->part_sf.default_max_partition_size = BLOCK_128X128;
-    sf->part_sf.default_min_partition_size = BLOCK_8X8;
     sf->part_sf.partition_search_type = VAR_BASED_PARTITION;
 
     sf->gm_sf.gm_search_type = GM_DISABLE_SEARCH;