Comments for CommonModeInfoParams struct members

Also:
- Rename 'mi' to more appropriate 'mi_alloc'
- Use 'TX_TYPE' instead of 'uint8_t' for 'tx_type_map'

BUG=aomedia:2610

Change-Id: Ibbc8b7eb8c59513f769b5e9de2cf7c195ad6e8b8
diff --git a/av1/common/alloccommon.c b/av1/common/alloccommon.c
index fd66022..2addde9 100644
--- a/av1/common/alloccommon.c
+++ b/av1/common/alloccommon.c
@@ -231,8 +231,9 @@
       mi_params->mi_grid_size < mi_grid_size) {
     mi_params->free_mi(mi_params);
 
-    mi_params->mi = aom_calloc(alloc_mi_size, sizeof(*mi_params->mi));
-    if (!mi_params->mi) return 1;
+    mi_params->mi_alloc =
+        aom_calloc(alloc_mi_size, sizeof(*mi_params->mi_alloc));
+    if (!mi_params->mi_alloc) return 1;
     mi_params->mi_alloc_size = alloc_mi_size;
 
     mi_params->mi_grid_base = (MB_MODE_INFO **)aom_calloc(
diff --git a/av1/common/av1_common_int.h b/av1/common/av1_common_int.h
index 91582e7..14c5d11 100644
--- a/av1/common/av1_common_int.h
+++ b/av1/common/av1_common_int.h
@@ -362,32 +362,49 @@
 // Struct containing params related to MB_MODE_INFO arrays and related info.
 typedef struct CommonModeInfoParams CommonModeInfoParams;
 struct CommonModeInfoParams {
-  // MBs, mb_rows/cols is in 16-pixel units; mi_rows/cols is in
-  // MB_MODE_INFO (4-pixel) units.
-  int MBs;
+  // Number of rows/cols in the frame in 16 pixel units.
+  // This is computed from frame width and height aligned to a multiple of 8.
   int mb_rows;
   int mb_cols;
+  // Total MBs = mb_rows * mb_cols.
+  int MBs;
 
+  // Number of rows/cols in the frame in 4 pixel (MB_MODE_INFO) units.
+  // This is computed from frame width and height aligned to a multiple of 8.
   int mi_rows;
   int mi_cols;
 
-  // Corresponds to upper left visible macroblock
-  MB_MODE_INFO *mi;
+  // An array of MB_MODE_INFO structs for every 'mi_alloc_bsize' sized block
+  // in the frame.
+  // Note: This array should be treated like a scratch memory, and should NOT be
+  // accessed directly, in most cases. Please use 'mi_grid_base' array instead.
+  MB_MODE_INFO *mi_alloc;
+  // Number of allocated elements in 'mi_alloc'.
   int mi_alloc_size;
+  // Stride for 'mi_alloc' array.
   int mi_alloc_stride;
-  // The minimum size each allocated mi can correspond to.
+  // The minimum block size that each element in 'mi_alloc' can correspond to.
   // For decoder, this is always BLOCK_4X4.
-  // For encoder, this is currently set to BLOCK_4X4 for resolution below 4k,
-  // and BLOCK_8X8 for resolution above 4k
+  // For encoder, this is currently set to BLOCK_4X4 for resolution < 4k,
+  // and BLOCK_8X8 for resolution >= 4k.
   BLOCK_SIZE mi_alloc_bsize;
 
-  // Grid of pointers to 4x4 MB_MODE_INFO structs. Any 4x4 not in the visible
-  // area will be NULL.
+  // Grid of pointers to 4x4 MB_MODE_INFO structs allocated in 'mi_alloc'.
+  // It's possible that:
+  // - Multiple pointers in the grid point to the same element in 'mi_alloc'
+  // (for example, for all 4x4 blocks that belong to the same partition block).
+  // - Some pointers can be NULL (for example, for blocks outside visible area).
   MB_MODE_INFO **mi_grid_base;
+  // Number of allocated elements in 'mi_grid_base' (and 'tx_type_map' also).
   int mi_grid_size;
+  // Stride for 'mi_grid_base' (and 'tx_type_map' also).
   int mi_stride;
 
-  uint8_t *tx_type_map;
+  // An array of tx types for each 4x4 block in the frame.
+  // Number of allocated elements is same as 'mi_grid_size', and stride is
+  // same as 'mi_grid_size'. So, indexing into 'tx_type_map' is same as that of
+  // 'mi_grid_base'.
+  TX_TYPE *tx_type_map;
 
   // Function pointers to allow separate logic for encoder and decoder.
   void (*free_mi)(struct CommonModeInfoParams *mi_params);
@@ -1233,7 +1250,7 @@
   // 'mi_grid_base' should point to appropriate memory in 'mi'.
   const int mi_grid_idx = get_mi_grid_idx(mi_params, mi_row, mi_col);
   const int mi_alloc_idx = get_alloc_mi_idx(mi_params, mi_row, mi_col);
-  mi_params->mi_grid_base[mi_grid_idx] = &mi_params->mi[mi_alloc_idx];
+  mi_params->mi_grid_base[mi_grid_idx] = &mi_params->mi_alloc[mi_alloc_idx];
   // 'xd->mi' should point to an offset in 'mi_grid_base';
   xd->mi = mi_params->mi_grid_base + mi_grid_idx;
   // 'xd->tx_type_map' should point to an offset in 'mi_params->tx_type_map'.
diff --git a/av1/decoder/decoder.c b/av1/decoder/decoder.c
index 3240ecc..7cfe1ed 100644
--- a/av1/decoder/decoder.c
+++ b/av1/decoder/decoder.c
@@ -82,8 +82,8 @@
 }
 
 static void dec_free_mi(CommonModeInfoParams *mi_params) {
-  aom_free(mi_params->mi);
-  mi_params->mi = NULL;
+  aom_free(mi_params->mi_alloc);
+  mi_params->mi_alloc = NULL;
   aom_free(mi_params->mi_grid_base);
   mi_params->mi_grid_base = NULL;
   mi_params->mi_alloc_size = 0;
diff --git a/av1/encoder/encodeframe.c b/av1/encoder/encodeframe.c
index 9b0d0e3..e41bd5c 100644
--- a/av1/encoder/encodeframe.c
+++ b/av1/encoder/encodeframe.c
@@ -1844,7 +1844,7 @@
   const int mi_rows_remaining = tile->mi_row_end - mi_row;
   const int mi_cols_remaining = tile->mi_col_end - mi_col;
   MB_MODE_INFO *const mi_upper_left =
-      mi_params->mi + get_alloc_mi_idx(mi_params, mi_row, mi_col);
+      mi_params->mi_alloc + get_alloc_mi_idx(mi_params, mi_row, mi_col);
   int bh = mi_size_high[bsize];
   int bw = mi_size_wide[bsize];
 
@@ -4442,7 +4442,7 @@
 // Memset the mbmis at the current superblock to 0
 static INLINE void reset_mbmi(CommonModeInfoParams *const mi_params,
                               BLOCK_SIZE sb_size, int mi_row, int mi_col) {
-  // size of sb in unit of mi_grid (BLOCK_4X4)
+  // size of sb in unit of mi (BLOCK_4X4)
   const int sb_size_mi = mi_size_wide[sb_size];
   const int mi_alloc_size_1d = mi_size_wide[mi_params->mi_alloc_bsize];
   // size of sb in unit of allocated mi size
@@ -4450,7 +4450,7 @@
   assert(mi_params->mi_alloc_stride % sb_size_alloc_mi == 0 &&
          "mi is not allocated as a multiple of sb!");
   assert(mi_params->mi_stride % sb_size_mi == 0 &&
-         "mi_grid is not allocated as a multiple of sb!");
+         "mi_grid_base is not allocated as a multiple of sb!");
 
   const int mi_rows = mi_size_high[sb_size];
   for (int cur_mi_row = 0; cur_mi_row < mi_rows; cur_mi_row++) {
@@ -4465,8 +4465,8 @@
     memset(&mi_params->tx_type_map[mi_grid_idx], 0,
            sb_size_mi * sizeof(*mi_params->tx_type_map));
     if (cur_mi_row % mi_alloc_size_1d == 0) {
-      memset(&mi_params->mi[alloc_mi_idx], 0,
-             sb_size_alloc_mi * sizeof(*mi_params->mi));
+      memset(&mi_params->mi_alloc[alloc_mi_idx], 0,
+             sb_size_alloc_mi * sizeof(*mi_params->mi_alloc));
     }
   }
 }
@@ -4500,7 +4500,8 @@
          sizeof(sb_fp_stats->thresh_freq_fact));
 
   const int alloc_mi_idx = get_alloc_mi_idx(&cm->mi_params, mi_row, mi_col);
-  sb_fp_stats->current_qindex = cm->mi_params.mi[alloc_mi_idx].current_qindex;
+  sb_fp_stats->current_qindex =
+      cm->mi_params.mi_alloc[alloc_mi_idx].current_qindex;
 
 #if CONFIG_INTERNAL_STATS
   memcpy(sb_fp_stats->mode_chosen_counts, cpi->mode_chosen_counts,
@@ -4531,7 +4532,8 @@
          sizeof(sb_fp_stats->thresh_freq_fact));
 
   const int alloc_mi_idx = get_alloc_mi_idx(&cm->mi_params, mi_row, mi_col);
-  cm->mi_params.mi[alloc_mi_idx].current_qindex = sb_fp_stats->current_qindex;
+  cm->mi_params.mi_alloc[alloc_mi_idx].current_qindex =
+      sb_fp_stats->current_qindex;
 
 #if CONFIG_INTERNAL_STATS
   memcpy(cpi->mode_chosen_counts, sb_fp_stats->mode_chosen_counts,
diff --git a/av1/encoder/encoder.c b/av1/encoder/encoder.c
index 5c61e28..aeb310c 100644
--- a/av1/encoder/encoder.c
+++ b/av1/encoder/encoder.c
@@ -670,7 +670,8 @@
 static void enc_setup_mi(CommonModeInfoParams *mi_params) {
   const int mi_grid_size =
       mi_params->mi_stride * calc_mi_size(mi_params->mi_rows);
-  memset(mi_params->mi, 0, mi_params->mi_alloc_size * sizeof(*mi_params->mi));
+  memset(mi_params->mi_alloc, 0,
+         mi_params->mi_alloc_size * sizeof(*mi_params->mi_alloc));
   memset(mi_params->mi_grid_base, 0,
          mi_grid_size * sizeof(*mi_params->mi_grid_base));
   memset(mi_params->tx_type_map, 0,
@@ -678,8 +679,8 @@
 }
 
 static void enc_free_mi(CommonModeInfoParams *mi_params) {
-  aom_free(mi_params->mi);
-  mi_params->mi = NULL;
+  aom_free(mi_params->mi_alloc);
+  mi_params->mi_alloc = NULL;
   aom_free(mi_params->mi_grid_base);
   mi_params->mi_grid_base = NULL;
   mi_params->mi_alloc_size = 0;