Accumulate stats under CONFIG_ENTROPY_STATS in AV1_PRIMARY

Create a sequence level copy of stats under CONFIG_ENTROPY_STATS in
AV1_PRIMARY. The stats in cpi are reset and calculated for each frame.
The stats in cpi are then accumulated into stats in ppi.

Change-Id: I2f5484a85523e8c8a1904529b7ecd20f988cc9fa
diff --git a/av1/av1_cx_iface.c b/av1/av1_cx_iface.c
index fb9f6ce..064a7db 100644
--- a/av1/av1_cx_iface.c
+++ b/av1/av1_cx_iface.c
@@ -2392,6 +2392,9 @@
   if (ctx->ppi) {
     AV1_PRIMARY *ppi = ctx->ppi;
 
+#if CONFIG_ENTROPY_STATS
+    print_entropy_stats(ppi);
+#endif
 #if CONFIG_INTERNAL_STATS
     print_internal_stats(ppi);
 #endif
@@ -2688,6 +2691,10 @@
         aom_internal_error(&ppi->error, AOM_CODEC_ERROR, NULL);
       }
 
+#if CONFIG_ENTROPY_STATS
+      if (ppi->cpi->oxcf.pass != 1 && !cpi->common.show_existing_frame)
+        av1_accumulate_frame_counts(&ppi->aggregate_fc, &cpi->counts);
+#endif
 #if CONFIG_INTERNAL_STATS
       if (ppi->cpi->oxcf.pass != 1) {
         ppi->total_time_compress_data += cpi->time_compress_data;
diff --git a/av1/encoder/encoder.c b/av1/encoder/encoder.c
index 30bca96..e1b1c4c 100644
--- a/av1/encoder/encoder.c
+++ b/av1/encoder/encoder.c
@@ -81,10 +81,6 @@
 
 #define DEFAULT_EXPLICIT_ORDER_HINT_BITS 7
 
-#if CONFIG_ENTROPY_STATS
-FRAME_COUNTS aggregate_fc;
-#endif  // CONFIG_ENTROPY_STATS
-
 // #define OUTPUT_YUV_REC
 #ifdef OUTPUT_YUV_REC
 FILE *yuv_rec_file;
@@ -894,6 +890,10 @@
 
   init_config_sequence(ppi, oxcf);
 
+#if CONFIG_ENTROPY_STATS
+  av1_zero(ppi->aggregate_fc);
+#endif  // CONFIG_ENTROPY_STATS
+
   av1_primary_rc_init(oxcf, &ppi->p_rc);
 
   // For two pass and lag_in_frames > 33 in LAP.
@@ -1303,9 +1303,6 @@
 #if CONFIG_SPEED_STATS
   cpi->tx_search_count = 0;
 #endif  // CONFIG_SPEED_STATS
-#if CONFIG_ENTROPY_STATS
-  av1_zero(aggregate_fc);
-#endif  // CONFIG_ENTROPY_STATS
 
   cpi->time_stamps.first_ts_start = INT64_MAX;
 
@@ -1518,14 +1515,6 @@
 
   AV1_COMMON *cm = &cpi->common;
   if (cm->current_frame.frame_number > 0) {
-#if CONFIG_ENTROPY_STATS
-    if (!is_stat_generation_stage(cpi)) {
-      fprintf(stderr, "Writing counts.stt\n");
-      FILE *f = fopen("counts.stt", "wb");
-      fwrite(&aggregate_fc, sizeof(aggregate_fc), 1, f);
-      fclose(f);
-    }
-#endif  // CONFIG_ENTROPY_STATS
 #if CONFIG_SPEED_STATS
     if (!is_stat_generation_stage(cpi)) {
       fprintf(stdout, "tx_search_count = %d\n", cpi->tx_search_count);
@@ -3317,10 +3306,6 @@
 
   refresh_reference_frames(cpi);
 
-#if CONFIG_ENTROPY_STATS
-  av1_accumulate_frame_counts(&aggregate_fc, &cpi->counts);
-#endif  // CONFIG_ENTROPY_STATS
-
   if (features->refresh_frame_context == REFRESH_FRAME_CONTEXT_BACKWARD) {
     *cm->fc = cpi->tile_data[largest_tile_id].tctx;
     av1_reset_cdf_symbol_counters(cm->fc);
@@ -3534,6 +3519,20 @@
   return res;
 }
 
+#if CONFIG_ENTROPY_STATS
+void print_entropy_stats(AV1_PRIMARY *const ppi) {
+  if (!ppi->cpi) return;
+
+  if (ppi->cpi->oxcf.pass != 1 &&
+      ppi->cpi->common.current_frame.frame_number > 0) {
+    fprintf(stderr, "Writing counts.stt\n");
+    FILE *f = fopen("counts.stt", "wb");
+    fwrite(&ppi->aggregate_fc, sizeof(ppi->aggregate_fc), 1, f);
+    fclose(f);
+  }
+}
+#endif  // CONFIG_ENTROPY_STATS
+
 #if CONFIG_INTERNAL_STATS
 extern double av1_get_blockiness(const unsigned char *img1, int img1_pitch,
                                  const unsigned char *img2, int img2_pitch,
@@ -3794,6 +3793,11 @@
   cpi->time_compress_data = 0;
   cpi->bytes = 0;
 #endif
+#if CONFIG_ENTROPY_STATS
+  if (cpi->compressor_stage == ENCODE_STAGE) {
+    av1_zero(cpi->counts);
+  }
+#endif
 
 #if CONFIG_BITSTREAM_DEBUG
   assert(cpi->oxcf.max_threads <= 1 &&
diff --git a/av1/encoder/encoder.h b/av1/encoder/encoder.h
index 68736fb..938f930 100644
--- a/av1/encoder/encoder.h
+++ b/av1/encoder/encoder.h
@@ -2309,6 +2309,13 @@
   Metrics metrics;
   /*!\endcond */
 #endif
+
+#if CONFIG_ENTROPY_STATS
+  /*!
+   * Aggregates frame counts for the sequence.
+   */
+  FRAME_COUNTS aggregate_fc;
+#endif  // CONFIG_ENTROPY_STATS
 } AV1_PRIMARY;
 
 /*!
@@ -2942,6 +2949,9 @@
 
 void av1_remove_primary_compressor(AV1_PRIMARY *ppi);
 
+#if CONFIG_ENTROPY_STATS
+void print_entropy_stats(AV1_PRIMARY *const ppi);
+#endif
 #if CONFIG_INTERNAL_STATS
 void print_internal_stats(AV1_PRIMARY *ppi);
 #endif