Fix encoder thread unit test failure

When we copy TPL stats and use them in encoding stage, for partial
super blocks(i.e. part of SB is outside the image), no valid TPL
data are available. Encoding stage might use left over data from
previous SB. In multi-thread case, this causes mismatch encoding
results. This CL sets values for every 16x16 block to make it
right.

BUG=aomedia:2636

STATS_CHANGED

Change-Id: I79dca119f28154dd428a7a7ee259a5e9fc8a5276
diff --git a/av1/encoder/encodeframe.c b/av1/encoder/encodeframe.c
index 8043991..fb0cea7 100644
--- a/av1/encoder/encodeframe.c
+++ b/av1/encoder/encodeframe.c
@@ -3993,6 +3993,7 @@
   if (gf_group_index >= MAX_LAG_BUFFERS) return 0;
 
   int mi_count = 0;
+  int count = 0;
   const int mi_col_sr =
       coded_to_superres_mi(mi_col, cm->superres_scale_denominator);
   const int mi_col_end_sr =
@@ -4007,20 +4008,30 @@
   const int step = mi_size_wide[tpl_bsize];
   assert(mi_size_wide[tpl_bsize] == mi_size_high[tpl_bsize]);
 
-  const int str = (mi_col_end_sr > mi_cols_sr)
-                      ? (mi_cols_sr - mi_col_sr) / step
-                      : (mi_col_end_sr - mi_col_sr) / step;
-  *stride = str;
+  // Stride is only based on SB size, and we fill in values for every 16x16
+  // block in a SB.
+  *stride = (mi_col_end_sr - mi_col_sr) / step;
 
   for (int row = mi_row; row < mi_row + mi_high; row += step) {
     for (int col = mi_col_sr; col < mi_col_end_sr; col += step) {
-      if (row >= cm->mi_params.mi_rows || col >= mi_cols_sr) continue;
+      // Handle partial SB, so that no invalid values are used later.
+      if (row >= cm->mi_params.mi_rows || col >= mi_cols_sr) {
+        inter_cost_b[count] = INT64_MAX;
+        intra_cost_b[count] = INT64_MAX;
+        for (int i = 0; i < INTER_REFS_PER_FRAME; ++i) {
+          mv_b[count][i].as_int = INVALID_MV;
+        }
+        count++;
+        continue;
+      }
+
       TplDepStats *this_stats = &tpl_stats[av1_tpl_ptr_pos(
           row, col, tpl_stride, tpl_data->tpl_stats_block_mis_log2)];
-      inter_cost_b[mi_count] = this_stats->inter_cost;
-      intra_cost_b[mi_count] = this_stats->intra_cost;
-      memcpy(mv_b[mi_count], this_stats->mv, sizeof(this_stats->mv));
+      inter_cost_b[count] = this_stats->inter_cost;
+      intra_cost_b[count] = this_stats->intra_cost;
+      memcpy(mv_b[count], this_stats->mv, sizeof(this_stats->mv));
       mi_count++;
+      count++;
     }
   }