Compute mv_bits in av1_vbr_rc_update_q_index_list

Only compute mv_bits when tpl_stats is ready

Change-Id: Ie15f9bda417ac5e08f470eebbab1963c6c8c8c14
diff --git a/av1/encoder/encode_strategy.c b/av1/encoder/encode_strategy.c
index 40ada30..89194c3 100644
--- a/av1/encoder/encode_strategy.c
+++ b/av1/encoder/encode_strategy.c
@@ -1030,8 +1030,6 @@
       av1_tpl_preload_rc_estimate(cpi, frame_params);
       av1_tpl_setup_stats(cpi, 0, frame_params, frame_input);
 #if CONFIG_BITRATE_ACCURACY
-      av1_vbr_estimate_mv_and_update(&cpi->ppi->tpl_data, gf_group->size,
-                                     cpi->gf_frame_index, &cpi->vbr_rc_info);
       av1_vbr_rc_update_q_index_list(&cpi->vbr_rc_info, &cpi->ppi->tpl_data,
                                      gf_group, cpi->gf_frame_index,
                                      cm->seq_params->bit_depth);
diff --git a/av1/encoder/pass2_strategy.c b/av1/encoder/pass2_strategy.c
index 994e202..ae7da76 100644
--- a/av1/encoder/pass2_strategy.c
+++ b/av1/encoder/pass2_strategy.c
@@ -1027,8 +1027,6 @@
       if (is_temporal_filter_enabled && !shorten_gf_interval) {
         cpi->skip_tpl_setup_stats = 1;
 #if CONFIG_BITRATE_ACCURACY
-        av1_vbr_estimate_mv_and_update(&cpi->ppi->tpl_data, gf_group->size,
-                                       cpi->gf_frame_index, &cpi->vbr_rc_info);
         av1_vbr_rc_update_q_index_list(&cpi->vbr_rc_info, &cpi->ppi->tpl_data,
                                        gf_group, cpi->gf_frame_index,
                                        cpi->common.seq_params->bit_depth);
diff --git a/av1/encoder/tpl_model.c b/av1/encoder/tpl_model.c
index d2c1b7d..c8121f6 100644
--- a/av1/encoder/tpl_model.c
+++ b/av1/encoder/tpl_model.c
@@ -2081,6 +2081,10 @@
     // rather than gf_group->q_val to avoid conflicts with the existing code.
     int stats_valid_list[MAX_LENGTH_TPL_FRAME_STATS] = { 0 };
     get_tpl_stats_valid_list(tpl_data, gf_group->size, stats_valid_list);
+
+    double mv_bits = av1_tpl_compute_mv_bits(tpl_data, gf_group->size,
+                                             gf_frame_index, vbr_rc_info);
+    gop_bit_budget -= mv_bits;
     av1_q_mode_estimate_base_q(
         gf_group, tpl_data->txfm_stats_list, stats_valid_list, gop_bit_budget,
         gf_frame_index, arf_qstep_ratio, bit_depth, vbr_rc_info->scale_factor,
@@ -2088,16 +2092,6 @@
   }
 }
 
-/* Estimate the entropy of motion vectors and update vbr_rc_info. */
-void av1_vbr_estimate_mv_and_update(const TplParams *tpl_data,
-                                    int gf_group_size, int gf_frame_index,
-                                    VBR_RATECTRL_INFO *vbr_rc_info) {
-  double mv_bits = av1_tpl_compute_mv_bits(tpl_data, gf_group_size,
-                                           gf_frame_index, vbr_rc_info);
-  // Subtract the motion vector entropy from the bit budget.
-  vbr_rc_info->gop_bit_budget -= mv_bits;
-}
-
 /* For a GOP, calculate the bits used by motion vectors. */
 double av1_tpl_compute_mv_bits(const TplParams *tpl_data, int gf_group_size,
                                int gf_frame_index,
@@ -2106,11 +2100,15 @@
 
   // Loop through each frame.
   for (int i = gf_frame_index; i < gf_group_size; i++) {
-    TplDepFrame *tpl_frame = &tpl_data->tpl_frame[i];
-    double frame_mv_bits = av1_tpl_compute_frame_mv_entropy(
-        tpl_frame, tpl_data->tpl_stats_block_mis_log2);
-    total_mv_bits += frame_mv_bits;
-    vbr_rc_info->estimated_mv_bitrate_byframe[i] = frame_mv_bits;
+    if (av1_tpl_stats_ready(tpl_data, i)) {
+      TplDepFrame *tpl_frame = &tpl_data->tpl_frame[i];
+      double frame_mv_bits = av1_tpl_compute_frame_mv_entropy(
+          tpl_frame, tpl_data->tpl_stats_block_mis_log2);
+      total_mv_bits += frame_mv_bits;
+      vbr_rc_info->estimated_mv_bitrate_byframe[i] = frame_mv_bits;
+    } else {
+      vbr_rc_info->estimated_mv_bitrate_byframe[i] = 0;
+    }
   }
 
   // Scale the final result by the scale factor.
diff --git a/av1/encoder/tpl_model.h b/av1/encoder/tpl_model.h
index 922a6e5..f698503 100644
--- a/av1/encoder/tpl_model.h
+++ b/av1/encoder/tpl_model.h
@@ -586,17 +586,6 @@
                                     int gf_frame_index,
                                     aom_bit_depth_t bit_depth);
 
-/*!\brief Estimate the entropy of motion vectors and update vbr_rc_info.
- *
- * \param[in]       tpl_data          TPL struct
- * \param[in]       gf_group_size     Number of frames in the gf_group
- * \param[in]       gf_frame_index    Current frame index
- * \param[in]       vbr_rc_info       Rate control info struct
- */
-void av1_vbr_estimate_mv_and_update(const TplParams *tpl_data,
-                                    int gf_group_size, int gf_frame_index,
-                                    VBR_RATECTRL_INFO *vbr_rc_info);
-
 /*!\brief For a GOP, calculate the bits used by motion vectors.
  *
  * \param[in]       tpl_data          TPL struct