Make tpl_stats usage aware of super-res mode

Change-Id: Ie5f739588fb7379b44c419824f9a439734991ea2
diff --git a/av1/common/blockd.h b/av1/common/blockd.h
index da12066..0ca7c19 100644
--- a/av1/common/blockd.h
+++ b/av1/common/blockd.h
@@ -1158,6 +1158,10 @@
   return tx_size_2d[tx_size];
 }
 
+static INLINE int av1_pixels_to_mi(int pixels) {
+  return ALIGN_POWER_OF_TWO(pixels, 3) >> MI_SIZE_LOG2;
+}
+
 #ifdef __cplusplus
 }  // extern "C"
 #endif
diff --git a/av1/common/resize.h b/av1/common/resize.h
index 43bea58..7bfb73c 100644
--- a/av1/common/resize.h
+++ b/av1/common/resize.h
@@ -102,6 +102,12 @@
   return !(cm->width == cm->superres_upscaled_width);
 }
 
+static INLINE int av1_coded_to_superres_mi(int mi_col, int denom) {
+  const int mi_col_sr =
+      (mi_col * denom + SCALE_NUMERATOR / 2) / SCALE_NUMERATOR;
+  return mi_col_sr;
+}
+
 #define UPSCALE_NORMATIVE_TAPS 8
 extern const int16_t av1_resize_filter_normative[1 << RS_SUBPEL_BITS]
                                                 [UPSCALE_NORMATIVE_TAPS];
diff --git a/av1/encoder/encodeframe.c b/av1/encoder/encodeframe.c
index 51a999c..3c7e465 100644
--- a/av1/encoder/encodeframe.c
+++ b/av1/encoder/encodeframe.c
@@ -3480,6 +3480,7 @@
 #if !CONFIG_REALTIME_ONLY
 static int get_rdmult_delta(AV1_COMP *cpi, BLOCK_SIZE bsize, int analysis_type,
                             int mi_row, int mi_col, int orig_rdmult) {
+  AV1_COMMON *const cm = &cpi->common;
   assert(IMPLIES(cpi->gf_group.size > 0,
                  cpi->gf_group.index < cpi->gf_group.size));
   const int tpl_idx = cpi->gf_group.frame_disp_idx[cpi->gf_group.index];
@@ -3500,12 +3501,15 @@
 
   int64_t mc_count = 0, mc_saved = 0;
   int mi_count = 0;
+  const int mi_col_sr =
+      av1_coded_to_superres_mi(mi_col, cm->superres_scale_denominator);
+  const int mi_col_end_sr = av1_coded_to_superres_mi(
+      mi_col + mi_wide, cm->superres_scale_denominator);
+  const int mi_cols_sr = av1_pixels_to_mi(cm->superres_upscaled_width);
   for (row = mi_row; row < mi_row + mi_high; ++row) {
-    for (col = mi_col; col < mi_col + mi_wide; ++col) {
+    for (col = mi_col_sr; col < mi_col_end_sr; ++col) {
+      if (row >= cm->mi_rows || col >= mi_cols_sr) continue;
       TplDepStats *this_stats = &tpl_stats[row * tpl_stride + col];
-
-      if (row >= cpi->common.mi_rows || col >= cpi->common.mi_cols) continue;
-
       intra_cost += this_stats->intra_cost;
       mc_dep_cost += this_stats->intra_cost + this_stats->mc_flow;
       mc_count += this_stats->mc_count;
@@ -3579,10 +3583,15 @@
 
   int64_t mc_count = 0, mc_saved = 0;
   int mi_count = 0;
+  const int mi_col_sr =
+      av1_coded_to_superres_mi(mi_col, cm->superres_scale_denominator);
+  const int mi_col_end_sr = av1_coded_to_superres_mi(
+      mi_col + mi_wide, cm->superres_scale_denominator);
+  const int mi_cols_sr = av1_pixels_to_mi(cm->superres_upscaled_width);
   for (row = mi_row; row < mi_row + mi_high; ++row) {
-    for (col = mi_col; col < mi_col + mi_wide; ++col) {
+    for (col = mi_col_sr; col < mi_col_end_sr; ++col) {
+      if (row >= cm->mi_rows || col >= mi_cols_sr) continue;
       TplDepStats *this_stats = &tpl_stats[row * tpl_stride + col];
-      if (row >= cm->mi_rows || col >= cm->mi_cols) continue;
       intra_cost += this_stats->intra_cost;
       mc_dep_cost += this_stats->intra_cost + this_stats->mc_flow;
       mc_count += this_stats->mc_count;
diff --git a/av1/encoder/encoder.c b/av1/encoder/encoder.c
index 20367c0..d2a69fc 100644
--- a/av1/encoder/encoder.c
+++ b/av1/encoder/encoder.c
@@ -3639,8 +3639,9 @@
     int64_t mc_count_base = 0;
     int row, col;
 
+    const int mi_cols_sr = av1_pixels_to_mi(cm->superres_upscaled_width);
     for (row = 0; row < cm->mi_rows; ++row) {
-      for (col = 0; col < cm->mi_cols; ++col) {
+      for (col = 0; col < mi_cols_sr; ++col) {
         TplDepStats *this_stats = &tpl_stats[row * tpl_stride + col];
         intra_cost_base += this_stats->intra_cost;
         mc_dep_cost_base += this_stats->intra_cost + this_stats->mc_flow;