code cleanup in init_ref_frame_space()

Change-Id: I6d4bf24c61f29bd68022bc5e10f8d174fd0ffd27
diff --git a/av1/encoder/block.h b/av1/encoder/block.h
index da7b50a..1a31d6e 100644
--- a/av1/encoder/block.h
+++ b/av1/encoder/block.h
@@ -478,7 +478,7 @@
   // The type of mv cost used during motion search
   MV_COST_TYPE mv_cost_type;
 
-  int search_ref_frame[REF_FRAMES];
+  uint8_t search_ref_frame[REF_FRAMES];
 
 #if CONFIG_AV1_HIGHBITDEPTH
   void (*fwd_txfm4x4)(const int16_t *input, tran_low_t *output, int stride);
diff --git a/av1/encoder/encodeframe.c b/av1/encoder/encodeframe.c
index 66ac461..31b221b 100644
--- a/av1/encoder/encodeframe.c
+++ b/av1/encoder/encodeframe.c
@@ -4491,7 +4491,7 @@
   const int frame_idx = cpi->gf_group.index;
   TplDepFrame *tpl_frame = &cpi->tpl_frame[frame_idx];
 
-  memset(x->search_ref_frame, 0, sizeof(x->search_ref_frame));
+  av1_zero(x->search_ref_frame);
 
   if (tpl_frame->is_valid == 0) return;
   if (!is_frame_tpl_eligible(cpi)) return;
@@ -4499,22 +4499,25 @@
   if (cpi->oxcf.superres_mode != SUPERRES_NONE) return;
   if (cpi->oxcf.aq_mode != NO_AQ) return;
 
+  const int is_overlay = cpi->gf_group.update_type[frame_idx] == OVERLAY_UPDATE;
+  if (is_overlay) {
+    memset(x->search_ref_frame, 1, sizeof(x->search_ref_frame));
+    return;
+  }
+
   TplDepStats *tpl_stats = tpl_frame->tpl_stats_ptr;
   const int tpl_stride = tpl_frame->stride;
   int64_t inter_cost[INTER_REFS_PER_FRAME] = { 0 };
   const int step = 1 << cpi->tpl_stats_block_mis_log2;
-
   const BLOCK_SIZE sb_size = cm->seq_params.sb_size;
   const int mi_row_end = AOMMIN(mi_size_high[sb_size] + mi_row, cm->mi_rows);
   const int mi_col_end = AOMMIN(mi_size_wide[sb_size] + mi_col, cm->mi_cols);
 
   for (int row = mi_row; row < mi_row_end; row += step) {
     for (int col = mi_col; col < mi_col_end; col += step) {
-      TplDepStats *this_stats =
+      const TplDepStats *this_stats =
           &tpl_stats[av1_tpl_ptr_pos(cpi, row, col, tpl_stride)];
-      int64_t tpl_pred_error[INTER_REFS_PER_FRAME];
-      memset(tpl_pred_error, 0, sizeof(tpl_pred_error));
-
+      int64_t tpl_pred_error[INTER_REFS_PER_FRAME] = { 0 };
       // Find the winner ref frame idx for the current block
       int64_t best_inter_cost = this_stats->pred_error[0];
       int best_rf_idx = 0;
@@ -4525,20 +4528,17 @@
           best_rf_idx = idx;
         }
       }
-      // Populate tpl_pred_error of
-      // 1. LAST_FRAME
-      // 2. best_ref w.r.t. LAST_FRAME.
-      tpl_pred_error[LAST_FRAME - 1] = this_stats->pred_error[LAST_FRAME - 1];
-      tpl_pred_error[best_rf_idx] =
-          this_stats->pred_error[best_rf_idx] - tpl_pred_error[LAST_FRAME - 1];
+      // tpl_pred_error is the pred_error reduction of best_ref w.r.t.
+      // LAST_FRAME.
+      tpl_pred_error[best_rf_idx] = this_stats->pred_error[best_rf_idx] -
+                                    this_stats->pred_error[LAST_FRAME - 1];
 
-      for (int rf_idx = 0; rf_idx < INTER_REFS_PER_FRAME; ++rf_idx)
+      for (int rf_idx = 1; rf_idx < INTER_REFS_PER_FRAME; ++rf_idx)
         inter_cost[rf_idx] += tpl_pred_error[rf_idx];
     }
   }
 
-  int rank_index[INTER_REFS_PER_FRAME - 1] = { 0 };
-
+  int rank_index[INTER_REFS_PER_FRAME - 1];
   for (int idx = 0; idx < INTER_REFS_PER_FRAME - 1; ++idx) {
     rank_index[idx] = idx + 1;
     for (int i = idx; i > 0; --i) {
@@ -4550,21 +4550,22 @@
     }
   }
 
-  const int is_overlay = cpi->gf_group.update_type[frame_idx] == OVERLAY_UPDATE;
   x->search_ref_frame[INTRA_FRAME] = 1;
   x->search_ref_frame[LAST_FRAME] = 1;
 
   int cutoff_ref = 0;
-
   for (int idx = 0; idx < INTER_REFS_PER_FRAME - 1; ++idx) {
     x->search_ref_frame[rank_index[idx] + LAST_FRAME] = 1;
-    if (idx > 2 && !is_overlay) {
-      // If the predictive coding gains are smaller than the previous more
-      // relevant frame over certain amount, discard this frame.
-      if (llabs(inter_cost[rank_index[idx]]) <
-              llabs(inter_cost[rank_index[idx - 1]]) / 8 ||
-          inter_cost[rank_index[idx]] == 0)
-        cutoff_ref = 1;
+    if (idx > 2) {
+      if (!cutoff_ref) {
+        // If the predictive coding gains are smaller than the previous more
+        // relevant frame over certain amount, discard this frame and all the
+        // frames afterwards.
+        if (llabs(inter_cost[rank_index[idx]]) <
+                llabs(inter_cost[rank_index[idx - 1]]) / 8 ||
+            inter_cost[rank_index[idx]] == 0)
+          cutoff_ref = 1;
+      }
 
       if (cutoff_ref) x->search_ref_frame[rank_index[idx] + LAST_FRAME] = 0;
     }