FPMT: Move frame level updates to postencode stage

This patch moves mv_stat, max_mv_magnitude and loop_filter_level
udpates in ppi to postencode function.

Change-Id: I4e85a1394a300f28911aebca4b1ef4fb965478d9
diff --git a/av1/encoder/encode_strategy.c b/av1/encoder/encode_strategy.c
index 0891c70..1d89a72 100644
--- a/av1/encoder/encode_strategy.c
+++ b/av1/encoder/encode_strategy.c
@@ -1513,6 +1513,8 @@
         cpi->rc.frame_level_rate_correction_factors[i] =
             cpi->ppi->p_rc.temp_rate_correction_factors[i];
     }
+    // copy mv_stats from ppi to frame_level cpi.
+    cpi->mv_stats = cpi->ppi->mv_stats;
 #endif
     av1_get_second_pass_params(cpi, &frame_params, &frame_input, *frame_flags);
 #if CONFIG_COLLECT_COMPONENT_TIMING
@@ -1822,12 +1824,6 @@
   }
 #endif  // CONFIG_REALTIME_ONLY
 
-#if CONFIG_FRAME_PARALLEL_ENCODE
-  // Store current frame's largest MV component in ppi.
-  if (!is_stat_generation_stage(cpi) && cpi->do_frame_data_update)
-    cpi->ppi->max_mv_magnitude = cpi->mv_search_params.max_mv_magnitude;
-#endif
-
   if (!is_stat_generation_stage(cpi)) {
     // First pass doesn't modify reference buffer assignment or produce frame
     // flags
diff --git a/av1/encoder/encoder.c b/av1/encoder/encoder.c
index 0f092f5..fab5039 100644
--- a/av1/encoder/encoder.c
+++ b/av1/encoder/encoder.c
@@ -2646,7 +2646,11 @@
     // Reset the mv_stats in case we are interrupted by an intraframe or an
     // overlay frame.
     if (cpi->ppi->mv_stats.valid && do_mv_stats_collection) {
+#if CONFIG_FRAME_PARALLEL_ENCODE
+      av1_zero(cpi->mv_stats);
+#else
       av1_zero(cpi->ppi->mv_stats);
+#endif
     }
     // Gather the mv_stats for the next frame
     if (cpi->sf.hl_sf.high_precision_mv_usage == LAST_MV_DATA &&
@@ -3973,6 +3977,29 @@
   update_gf_group_index(cpi);
 }
 
+#if CONFIG_FRAME_PARALLEL_ENCODE
+static void update_end_of_frame_stats(AV1_COMP *cpi) {
+  if (cpi->do_frame_data_update) {
+    // Store current frame loopfilter levels in ppi, if update flag is set.
+    if (!cpi->common.show_existing_frame) {
+      AV1_COMMON *const cm = &cpi->common;
+      struct loopfilter *const lf = &cm->lf;
+      cpi->ppi->filter_level[0] = lf->filter_level[0];
+      cpi->ppi->filter_level[1] = lf->filter_level[1];
+      cpi->ppi->filter_level_u = lf->filter_level_u;
+      cpi->ppi->filter_level_v = lf->filter_level_v;
+    }
+
+    // Store current frame max_mv_magnitude in ppi, if update flag is set.
+    if (!is_stat_generation_stage(cpi))
+      cpi->ppi->max_mv_magnitude = cpi->mv_search_params.max_mv_magnitude;
+  }
+
+  // Store frame level mv_stats from cpi to ppi.
+  cpi->ppi->mv_stats = cpi->mv_stats;
+}
+#endif
+
 void av1_post_encode_updates(AV1_COMP *const cpi,
                              const AV1_COMP_DATA *const cpi_data) {
   AV1_PRIMARY *const ppi = cpi->ppi;
@@ -4031,6 +4058,9 @@
 #endif
     update_fb_of_context_type(cpi, ppi->fb_of_context_type);
     update_rc_counts(cpi);
+#if CONFIG_FRAME_PARALLEL_ENCODE
+    update_end_of_frame_stats(cpi);
+#endif
   }
 
   if (ppi->use_svc) av1_save_layer_context(cpi);
diff --git a/av1/encoder/encoder.h b/av1/encoder/encoder.h
index 8876f5f..059bb55 100644
--- a/av1/encoder/encoder.h
+++ b/av1/encoder/encoder.h
@@ -3057,6 +3057,12 @@
    * last frame in encode order in a given parallel encode set.
    */
   bool do_frame_data_update;
+
+  /*!
+   * Motion vector stats of the current encoded frame, used to update the
+   * ppi->mv_stats during postencode.
+   */
+  MV_STATS mv_stats;
 #if CONFIG_FRAME_PARALLEL_ENCODE_2
   /*!
    * Stores the reference refresh index for the current frame.
diff --git a/av1/encoder/mv_prec.c b/av1/encoder/mv_prec.c
index 84a2ac4..3ff80c8 100644
--- a/av1/encoder/mv_prec.c
+++ b/av1/encoder/mv_prec.c
@@ -346,7 +346,12 @@
 }
 
 void av1_collect_mv_stats(AV1_COMP *cpi, int current_q) {
-  MV_STATS *mv_stats = &cpi->ppi->mv_stats;
+  MV_STATS *mv_stats;
+#if CONFIG_FRAME_PARALLEL_ENCODE
+  mv_stats = &cpi->mv_stats;
+#else
+  mv_stats = &cpi->ppi->mv_stats;
+#endif
   const AV1_COMMON *cm = &cpi->common;
   const int tile_cols = cm->tiles.cols;
   const int tile_rows = cm->tiles.rows;
@@ -417,7 +422,11 @@
 #if !CONFIG_REALTIME_ONLY
   else if (cpi->sf.hl_sf.high_precision_mv_usage == LAST_MV_DATA &&
            av1_frame_allows_smart_mv(cpi) && cpi->ppi->mv_stats.valid) {
+#if CONFIG_FRAME_PARALLEL_ENCODE
+    use_hp = get_smart_mv_prec(cpi, &cpi->mv_stats, qindex);
+#else
     use_hp = get_smart_mv_prec(cpi, &cpi->ppi->mv_stats, qindex);
+#endif
   }
 #endif  // !CONFIG_REALTIME_ONLY
 
diff --git a/av1/encoder/picklpf.c b/av1/encoder/picklpf.c
index 57841fe..c44038c 100644
--- a/av1/encoder/picklpf.c
+++ b/av1/encoder/picklpf.c
@@ -300,14 +300,5 @@
           search_filter_level(sd, cpi, method == LPF_PICK_FROM_SUBIMAGE,
                               last_frame_filter_level, NULL, 2, 0);
     }
-#if CONFIG_FRAME_PARALLEL_ENCODE
-    // Store current frame loopfilter levels if update flag is set.
-    if (cpi->do_frame_data_update) {
-      cpi->ppi->filter_level[0] = lf->filter_level[0];
-      cpi->ppi->filter_level[1] = lf->filter_level[1];
-      cpi->ppi->filter_level_u = lf->filter_level_u;
-      cpi->ppi->filter_level_v = lf->filter_level_v;
-    }
-#endif
   }
 }