Fix divide by zero bug in tpl_model.

BUG=aomedia:2915

Change-Id: Id1a6d773fff4ced9e465e4e6053df7807c45afe4
diff --git a/av1/encoder/encode_strategy.c b/av1/encoder/encode_strategy.c
index 051c3b2..3f40825 100644
--- a/av1/encoder/encode_strategy.c
+++ b/av1/encoder/encode_strategy.c
@@ -941,9 +941,10 @@
                   !is_stat_generation_stage(cpi) &&
                   oxcf->algo_cfg.enable_tpl_model;
   if (frame_params->frame_type == KEY_FRAME) {
-    // Don't do tpl for fwd key frames
+    // Don't do tpl for fwd key frames or fwd key frame overlays
     allow_tpl = allow_tpl && !cpi->sf.tpl_sf.disable_filtered_key_tpl &&
-                !cpi->no_show_fwd_kf;
+                !cpi->no_show_fwd_kf &&
+                gf_group->update_type[gf_group->index] != OVERLAY_UPDATE;
   } else {
     // Do tpl after ARF is filtered, or if no ARF, at the second frame of GF
     // group.
diff --git a/av1/encoder/tpl_model.c b/av1/encoder/tpl_model.c
index c980905..8da310a 100644
--- a/av1/encoder/tpl_model.c
+++ b/av1/encoder/tpl_model.c
@@ -1478,6 +1478,7 @@
 
   if (cpi->common.tiles.large_scale) return 0;
   if (gf_group->max_layer_depth_allowed == 0) return 1;
+  if (!gop_eval) return 0;
   assert(gf_group->arf_index >= 0);
 
   double beta[2] = { 0.0 };
@@ -1507,8 +1508,14 @@
             (this_stats->recrf_dist << RDDIV_BITS) + mc_dep_delta;
       }
     }
-    beta[frame_idx - gf_group->arf_index] =
-        (double)mc_dep_cost_base / intra_cost_base;
+    if (intra_cost_base == 0) {
+      // This should happen very rarely and if it happens, assign a dummy value
+      // to it since it probably wouldn't influence things much
+      beta[frame_idx - gf_group->arf_index] = 0;
+    } else {
+      beta[frame_idx - gf_group->arf_index] =
+          (double)mc_dep_cost_base / intra_cost_base;
+    }
   }
 
 #if CONFIG_COLLECT_COMPONENT_TIMING
diff --git a/test/kf_test.cc b/test/kf_test.cc
index 80c2aec..0b36881 100644
--- a/test/kf_test.cc
+++ b/test/kf_test.cc
@@ -230,14 +230,6 @@
 }
 
 TEST_P(ForcedKeyTestLarge, ForcedFrameIsKeyCornerCases) {
-  // TODO(aomedia:2915): Remove this if statement.
-  if (encoding_mode_ == ::libaom_test::kTwoPassGood && auto_alt_ref_ == 1 &&
-      fwd_kf_enabled_ == 1) {
-    std::cerr << "The test will divide by zero (crbug.com/aomedia/2915). Skip "
-                 "the test."
-              << std::endl;
-    return;
-  }
   const aom_rational timebase = { 1, 30 };
   const int kf_offsets[] = { -2, -1, 1, 2, 0 };
   cfg_.g_lag_in_frames = 35;