Remove duplicate reference frames in tpl

The duplicate reference frames used in motion
estimation in tpl has been removed.

cpu-used  Instruction Count
              Reduction
   1           0.46%
   2           0.50%
   3           0.80%
   4           0.90%

Change-Id: I29edf1a5af0bd3a8b7cb99bb24b44dec7d95e6eb
diff --git a/av1/encoder/tpl_model.c b/av1/encoder/tpl_model.c
index 7b6d369..9cb49b3 100644
--- a/av1/encoder/tpl_model.c
+++ b/av1/encoder/tpl_model.c
@@ -517,7 +517,7 @@
 
   AV1_COMMON *cm = &cpi->common;
   struct scale_factors sf;
-  int rdmult, idx;
+  int rdmult;
   ThreadData *td = &cpi->td;
   MACROBLOCK *x = &td->mb;
   MACROBLOCKD *xd = &x->e_mbd;
@@ -547,9 +547,20 @@
   uint8_t *predictor =
       is_cur_buf_hbd(xd) ? CONVERT_TO_BYTEPTR(predictor8) : predictor8;
 
-  // TODO(jingning): remove the duplicate frames.
-  for (idx = 0; idx < INTER_REFS_PER_FRAME; ++idx)
-    ref_frame[idx] = cpi->tpl_frame[tpl_frame->ref_map_index[idx]].gf_picture;
+  // Remove duplicate frames
+  for (int idx1 = 0; idx1 < INTER_REFS_PER_FRAME; ++idx1) {
+    const int rf_idx1 = tpl_frame->ref_map_index[idx1];
+    int duplicate = 0;
+    for (int idx2 = 0; idx2 < idx1; ++idx2) {
+      const int rf_idx2 = tpl_frame->ref_map_index[idx2];
+      if (cpi->tpl_frame[rf_idx1].gf_picture ==
+          cpi->tpl_frame[rf_idx2].gf_picture) {
+        duplicate = 1;
+        break;
+      }
+    }
+    if (!duplicate) ref_frame[idx1] = cpi->tpl_frame[rf_idx1].gf_picture;
+  }
 
   // Make a temporary mbmi for tpl model
   MB_MODE_INFO **backup_mi_grid = xd->mi;