Determine screen content type in the first pass

Allow the function to determine screen content type to be called
for both the first and the second pass.

Change-Id: I226bfcc5a7c293f0c7760f64b0b4a5d4d7c8fc78
diff --git a/av1/encoder/encode_strategy.c b/av1/encoder/encode_strategy.c
index 3bba79a..7cd170b 100644
--- a/av1/encoder/encode_strategy.c
+++ b/av1/encoder/encode_strategy.c
@@ -1111,6 +1111,8 @@
   frame_input.source = code_arf ? &cpi->alt_ref_buffer : &source->img;
   frame_input.last_source = last_source != NULL ? &last_source->img : NULL;
   frame_input.ts_duration = source->ts_end - source->ts_start;
+  // Save unfiltered source. It is used in av1_get_second_pass_params().
+  cpi->unfiltered_source = frame_input.source;
 
   *time_stamp = source->ts_start;
   *time_end = source->ts_end;
@@ -1266,8 +1268,6 @@
 #endif
   }
 
-  // Save unfiltered source.
-  cpi->unfiltered_source = frame_input.source;
 #if CONFIG_REALTIME_ONLY
   if (av1_encode(cpi, dest, &frame_input, &frame_params, &frame_results) !=
       AOM_CODEC_OK) {
diff --git a/av1/encoder/encoder.c b/av1/encoder/encoder.c
index 53f376b..ac0c915 100644
--- a/av1/encoder/encoder.c
+++ b/av1/encoder/encoder.c
@@ -3977,8 +3977,8 @@
   }
 }
 
-static void set_screen_content_options(const AV1_COMP *const cpi,
-                                       FeatureFlags *const features) {
+void av1_set_screen_content_options(const AV1_COMP *const cpi,
+                                    FeatureFlags *const features) {
   const AV1_COMMON *const cm = &cpi->common;
 
   if (cm->seq_params.force_screen_content_tools != 2) {
@@ -4046,15 +4046,11 @@
 static void set_size_independent_vars(AV1_COMP *cpi) {
   int i;
   AV1_COMMON *const cm = &cpi->common;
-  FeatureFlags *const features = &cm->features;
   for (i = LAST_FRAME; i <= ALTREF_FRAME; ++i) {
     cm->global_motion[i] = default_warp_params;
   }
   cpi->gm_info.search_done = 0;
 
-  if (frame_is_intra_only(cm)) set_screen_content_options(cpi, features);
-  cpi->is_screen_content_type = (features->allow_screen_content_tools != 0);
-
   av1_set_speed_features_framesize_independent(cpi, cpi->speed);
   av1_set_rd_speed_thresholds(cpi);
   cm->features.interp_filter = SWITCHABLE;
diff --git a/av1/encoder/encoder.h b/av1/encoder/encoder.h
index 826e9a6..4274aec 100644
--- a/av1/encoder/encoder.h
+++ b/av1/encoder/encoder.h
@@ -1358,6 +1358,18 @@
                                         CompoundTypeRdBuffers *const bufs);
 void av1_release_compound_type_rd_buffers(CompoundTypeRdBuffers *const bufs);
 
+// Set screen content options.
+// This function estimates whether to use screen content tools, by counting
+// the portion of blocks that have few luma colors.
+// Modifies:
+//   cpi->commom.allow_screen_content_tools
+//   cpi->common.allow_intrabc
+// However, the estimation is not accurate and may misclassify videos.
+// A slower but more accurate approach that determines whether to use screen
+// content tools is employed later. See determine_sc_tools_with_encoding().
+void av1_set_screen_content_options(const struct AV1_COMP *cpi,
+                                    FeatureFlags *features);
+
 // TODO(jingning): Move these functions as primitive members for the new cpi
 // class.
 static INLINE void stack_push(int *stack, int *stack_size, int item) {
diff --git a/av1/encoder/firstpass.c b/av1/encoder/firstpass.c
index d0cd939..8894397 100644
--- a/av1/encoder/firstpass.c
+++ b/av1/encoder/firstpass.c
@@ -864,6 +864,12 @@
   const PICK_MODE_CONTEXT *ctx = &cpi->td.pc_root->none;
   MV last_mv = kZeroMv;
   const int qindex = find_fp_qindex(seq_params->bit_depth);
+  // Detect if the key frame is screen content type.
+  if (frame_is_intra_only(cm)) {
+    FeatureFlags *const features = &cm->features;
+    av1_set_screen_content_options(cpi, features);
+    cpi->is_screen_content_type = features->allow_screen_content_tools;
+  }
   // First pass coding proceeds in raster scan order with unit size of 16x16.
   const BLOCK_SIZE fp_block_size = BLOCK_16X16;
   const int fp_block_size_width = block_size_high[fp_block_size];
diff --git a/av1/encoder/pass2_strategy.c b/av1/encoder/pass2_strategy.c
index 56c4d1a..d55b69e 100644
--- a/av1/encoder/pass2_strategy.c
+++ b/av1/encoder/pass2_strategy.c
@@ -2489,6 +2489,13 @@
   RATE_CONTROL *const rc = &cpi->rc;
   TWO_PASS *const twopass = &cpi->twopass;
   GF_GROUP *const gf_group = &cpi->gf_group;
+  AV1_COMMON *cm = &cpi->common;
+
+  if (frame_is_intra_only(cm)) {
+    FeatureFlags *const features = &cm->features;
+    av1_set_screen_content_options(cpi, features);
+    cpi->is_screen_content_type = features->allow_screen_content_tools;
+  }
 
   if (is_stat_consumption_stage(cpi) && !twopass->stats_in) return;