RC: calc tpl stats for gop starting with leaf

When using external rate control, in rare cases, the sequence ends
with a GOP that has only 1 frame, which is also leaf frame. TPL stats
needs to be calculated for those cases too.

Change-Id: I777cb09eb972e3da78d64dd1046c589ac86defca
diff --git a/av1/encoder/encode_strategy.c b/av1/encoder/encode_strategy.c
index 3b7973a..f067e02 100644
--- a/av1/encoder/encode_strategy.c
+++ b/av1/encoder/encode_strategy.c
@@ -869,11 +869,18 @@
     if (frame_params->frame_type != KEY_FRAME) {
       // In rare case, it's possible to have non ARF/GF update_type here.
       // We should set allow_tpl to zero in the situation
-      allow_tpl =
-          allow_tpl && (update_type == ARF_UPDATE || update_type == GF_UPDATE ||
-                        (cpi->use_ducky_encode &&
-                         cpi->ducky_encode_info.frame_info.gop_mode ==
-                             DUCKY_ENCODE_GOP_MODE_RCL));
+      bool frame_type_allow_tpl =
+          update_type == ARF_UPDATE || update_type == GF_UPDATE;
+      // When external rate control is used, sometimes the sequence ends with
+      // a GOP with only 1 frame which is a leaf frame. TPL stats needs to be
+      // calculated for it as well.
+      if (av1_use_tpl_for_extrc(&cpi->ext_ratectrl)) {
+        frame_type_allow_tpl |= update_type == LF_UPDATE;
+      }
+      allow_tpl = allow_tpl && (frame_type_allow_tpl ||
+                                (cpi->use_ducky_encode &&
+                                 cpi->ducky_encode_info.frame_info.gop_mode ==
+                                     DUCKY_ENCODE_GOP_MODE_RCL));
     }
 
     if (allow_tpl) {
diff --git a/av1/encoder/tpl_model.c b/av1/encoder/tpl_model.c
index 8380646..d3699c9 100644
--- a/av1/encoder/tpl_model.c
+++ b/av1/encoder/tpl_model.c
@@ -1463,10 +1463,6 @@
       gf_group->layer_depth[frame_idx] >= layer_depth_th;
 }
 
-static inline bool use_tpl_for_extrc(AOM_EXT_RATECTRL const *ext_rc) {
-  return ext_rc->ready && ext_rc->funcs.send_tpl_gop_stats != NULL;
-}
-
 static void tpl_store_before_propagation(AomTplBlockStats *tpl_block_stats,
                                          TplDepStats *src_stats, int mi_row,
                                          int mi_col) {
@@ -1555,7 +1551,7 @@
     tpl_model_store(tpl_frame->tpl_stats_ptr, mi_row, mi_col, tpl_frame->stride,
                     &tpl_stats, tpl_data->tpl_stats_block_mis_log2);
 
-    if (use_tpl_for_extrc(&cpi->ext_ratectrl)) {
+    if (av1_use_tpl_for_extrc(&cpi->ext_ratectrl)) {
       AomTplFrameStats *tpl_frame_stats_before_propagation =
           &cpi->extrc_tpl_gop_stats.frame_stats_list[tpl_data->frame_idx];
       AomTplBlockStats *block_stats =
@@ -2064,7 +2060,7 @@
 
   av1_init_tpl_stats(tpl_data);
 
-  if (use_tpl_for_extrc(&cpi->ext_ratectrl)) {
+  if (av1_use_tpl_for_extrc(&cpi->ext_ratectrl)) {
     init_tpl_stats_before_propagation(
         cpi->common.error, &cpi->extrc_tpl_gop_stats, tpl_data,
         tpl_gf_group_frames, cpi->common.width, cpi->common.height);
@@ -2135,7 +2131,7 @@
                              num_planes);
   }
 
-  if (use_tpl_for_extrc(&cpi->ext_ratectrl)) {
+  if (av1_use_tpl_for_extrc(&cpi->ext_ratectrl)) {
     // TPL stats has extra frames from next GOP. Trim those extra frames for
     // external RC.
     trim_tpl_stats(cpi->common.error, &cpi->extrc_tpl_gop_stats,
diff --git a/av1/encoder/tpl_model.h b/av1/encoder/tpl_model.h
index 8fba0cf..ba6fd9c 100644
--- a/av1/encoder/tpl_model.h
+++ b/av1/encoder/tpl_model.h
@@ -35,6 +35,7 @@
 
 #include "av1/common/mv.h"
 #include "av1/common/scale.h"
+#include "av1/encoder/av1_ext_ratectrl.h"
 #include "av1/encoder/block.h"
 #include "av1/encoder/lookahead.h"
 #include "av1/encoder/ratectrl.h"
@@ -415,6 +416,10 @@
 void av1_read_rd_command(const char *filepath, RD_COMMAND *rd_command);
 #endif  // CONFIG_RD_COMMAND
 
+static inline bool av1_use_tpl_for_extrc(AOM_EXT_RATECTRL const *ext_rc) {
+  return ext_rc->ready && ext_rc->funcs.send_tpl_gop_stats != NULL;
+}
+
 /*!\brief Allocate buffers used by tpl model
  *
  * \param[in]    Top-level encode/decode structure