Distinguish use screen content tools and screen content type

Some natural videos could use screen content tools and get better
coding efficiency.
We therefore use a separate variable |use_screen_content_tools|
for it.
And |is_screen_content_type| now stands for "real" screen content
videos, and when it is true, we adjust rate control scheme for it.

This cl fixes some false positives in detecting screen content type.

STATS_CHANGED

BUG=aomedia:2785

Change-Id: I06f2f002c40f8f75dbea4b31006589effd9949d8
diff --git a/av1/common/blockd.h b/av1/common/blockd.h
index 7a05224..bd586a4 100644
--- a/av1/common/blockd.h
+++ b/av1/common/blockd.h
@@ -1058,12 +1058,12 @@
 static INLINE TX_TYPE get_default_tx_type(PLANE_TYPE plane_type,
                                           const MACROBLOCKD *xd,
                                           TX_SIZE tx_size,
-                                          int is_screen_content_type) {
+                                          int use_screen_content_tools) {
   const MB_MODE_INFO *const mbmi = xd->mi[0];
 
   if (is_inter_block(mbmi) || plane_type != PLANE_TYPE_Y ||
       xd->lossless[mbmi->segment_id] || tx_size >= TX_32X32 ||
-      is_screen_content_type)
+      use_screen_content_tools)
     return DCT_DCT;
 
   return intra_mode_to_tx_type(mbmi, plane_type);
diff --git a/av1/encoder/encoder.c b/av1/encoder/encoder.c
index ab5993f..c96f979 100644
--- a/av1/encoder/encoder.c
+++ b/av1/encoder/encoder.c
@@ -1692,8 +1692,7 @@
   }
 }
 
-void av1_set_screen_content_options(const AV1_COMP *cpi,
-                                    FeatureFlags *features) {
+void av1_set_screen_content_options(AV1_COMP *cpi, FeatureFlags *features) {
   const AV1_COMMON *const cm = &cpi->common;
 
   if (cm->seq_params.force_screen_content_tools != 2) {
@@ -1756,6 +1755,11 @@
   // requires that the block has high variance.
   features->allow_intrabc = features->allow_screen_content_tools &&
                             counts_2 * blk_h * blk_w * 12 > width * height;
+  cpi->use_screen_content_tools = features->allow_screen_content_tools;
+  cpi->is_screen_content_type =
+      features->allow_intrabc ||
+      (counts_1 * blk_h * blk_w * 10 > width * height * 4 &&
+       counts_2 * blk_h * blk_w * 30 > width * height);
 }
 
 // Function pointer to search site config initialization
@@ -2824,7 +2828,6 @@
 
   if (frame_is_intra_only(cm)) {
     av1_set_screen_content_options(cpi, features);
-    cpi->is_screen_content_type = features->allow_screen_content_tools;
   }
 
   // frame type has been decided outside of this function call
diff --git a/av1/encoder/encoder.h b/av1/encoder/encoder.h
index 641ea90..04aab64 100644
--- a/av1/encoder/encoder.h
+++ b/av1/encoder/encoder.h
@@ -2394,7 +2394,19 @@
   InterpSearchFlags interp_search_flags;
 
   /*!
-   * Set for screen contents or when screen content tools are enabled.
+   * Turn on screen content tools flag.
+   * Note that some videos are not screen content videos, but
+   * screen content tools could also improve coding efficiency.
+   * For example, videos with large flat regions, gaming videos that look
+   * like natural videos.
+   */
+  int use_screen_content_tools;
+
+  /*!
+   * A flag to indicate "real" screen content videos.
+   * For example, screen shares, screen editing.
+   * This type is true indicates |use_screen_content_tools| must be true.
+   * In addition, rate control strategy is adjusted when this flag is true.
    */
   int is_screen_content_type;
 
@@ -2710,12 +2722,14 @@
 // 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
+//   cpi->commom.features.allow_screen_content_tools
+//   cpi->common.features.allow_intrabc
+//   cpi->use_screen_content_tools
+//   cpi->is_screen_content_type
 // 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 av1_determine_sc_tools_with_encoding().
-void av1_set_screen_content_options(const struct AV1_COMP *cpi,
+void av1_set_screen_content_options(struct AV1_COMP *cpi,
                                     FeatureFlags *features);
 
 // TODO(jingning): Move these functions as primitive members for the new cpi
diff --git a/av1/encoder/encoder_utils.c b/av1/encoder/encoder_utils.c
index 7209054..9eb87f2 100644
--- a/av1/encoder/encoder_utils.c
+++ b/av1/encoder/encoder_utils.c
@@ -851,6 +851,7 @@
 static void screen_content_tools_determination(
     AV1_COMP *cpi, const int allow_screen_content_tools_orig_decision,
     const int allow_intrabc_orig_decision,
+    const int use_screen_content_tools_orig_decision,
     const int is_screen_content_type_orig_decision, const int pass,
     int *projected_size_pass, PSNR_STATS *psnr) {
   AV1_COMMON *const cm = &cpi->common;
@@ -872,12 +873,14 @@
     // Use screen content tools, if we get coding gain.
     features->allow_screen_content_tools = 1;
     features->allow_intrabc = cpi->intrabc_used;
+    cpi->use_screen_content_tools = 1;
     cpi->is_screen_content_type = 1;
   } else {
     // Use original screen content decision.
     features->allow_screen_content_tools =
         allow_screen_content_tools_orig_decision;
     features->allow_intrabc = allow_intrabc_orig_decision;
+    cpi->use_screen_content_tools = use_screen_content_tools_orig_decision;
     cpi->is_screen_content_type = is_screen_content_type_orig_decision;
   }
 }
@@ -892,7 +895,7 @@
     // Use a high q, and a fixed block size for fast encoding.
     cm->features.allow_screen_content_tools = 0;
     cm->features.allow_intrabc = 0;
-    cpi->is_screen_content_type = 0;
+    cpi->use_screen_content_tools = 0;
     cpi->sf.part_sf.partition_search_type = FIXED_PARTITION;
     cpi->sf.part_sf.fixed_partition_size = BLOCK_32X32;
     return;
@@ -903,14 +906,14 @@
   cm->features.allow_screen_content_tools = 1;
   // TODO(chengchen): turn intrabc on could lead to data race issue.
   // cm->allow_intrabc = 1;
-  cpi->is_screen_content_type = 1;
+  cpi->use_screen_content_tools = 1;
   cpi->sf.part_sf.partition_search_type = FIXED_PARTITION;
   cpi->sf.part_sf.fixed_partition_size = BLOCK_32X32;
 }
 
 // Determines whether to use screen content tools for the key frame group.
 // This function modifies "cm->features.allow_screen_content_tools",
-// "cm->features.allow_intrabc" and "cpi->is_screen_content_type".
+// "cm->features.allow_intrabc" and "cpi->use_screen_content_tools".
 void av1_determine_sc_tools_with_encoding(AV1_COMP *cpi, const int q_orig) {
   AV1_COMMON *const cm = &cpi->common;
   const AV1EncoderConfig *const oxcf = &cpi->oxcf;
@@ -922,11 +925,13 @@
   const int allow_screen_content_tools_orig_decision =
       cm->features.allow_screen_content_tools;
   const int allow_intrabc_orig_decision = cm->features.allow_intrabc;
+  const int use_screen_content_tools_orig_decision =
+      cpi->use_screen_content_tools;
   const int is_screen_content_type_orig_decision = cpi->is_screen_content_type;
   // Turn off the encoding trial for forward key frame and superres.
   if (cpi->sf.rt_sf.use_nonrd_pick_mode || oxcf->kf_cfg.fwd_kf_enabled ||
       cpi->superres_mode != AOM_SUPERRES_NONE || oxcf->mode == REALTIME ||
-      is_screen_content_type_orig_decision || !is_key_frame) {
+      use_screen_content_tools_orig_decision || !is_key_frame) {
     return;
   }
 
@@ -999,8 +1004,8 @@
     // Screen content decision
     screen_content_tools_determination(
         cpi, allow_screen_content_tools_orig_decision,
-        allow_intrabc_orig_decision, is_screen_content_type_orig_decision, pass,
-        projected_size_pass, psnr);
+        allow_intrabc_orig_decision, use_screen_content_tools_orig_decision,
+        is_screen_content_type_orig_decision, pass, projected_size_pass, psnr);
   }
 
   // Set partition speed feature back.
diff --git a/av1/encoder/encodetxb.c b/av1/encoder/encodetxb.c
index af847ee..c98bb1e 100644
--- a/av1/encoder/encodetxb.c
+++ b/av1/encoder/encodetxb.c
@@ -1320,7 +1320,7 @@
       assert(tx_type == DCT_DCT);
     } else if (cpi->oxcf.txfm_cfg.use_intra_default_tx_only) {
       const TX_TYPE default_type = get_default_tx_type(
-          PLANE_TYPE_Y, xd, tx_size, cpi->is_screen_content_type);
+          PLANE_TYPE_Y, xd, tx_size, cpi->use_screen_content_tools);
       (void)default_type;
       assert(tx_type == default_type);
     }
diff --git a/av1/encoder/firstpass.c b/av1/encoder/firstpass.c
index ec1bb0e..9eb0309 100644
--- a/av1/encoder/firstpass.c
+++ b/av1/encoder/firstpass.c
@@ -1070,7 +1070,6 @@
   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;
diff --git a/av1/encoder/partition_search.c b/av1/encoder/partition_search.c
index 1bcd142..412e761 100644
--- a/av1/encoder/partition_search.c
+++ b/av1/encoder/partition_search.c
@@ -2995,7 +2995,7 @@
   assert(part_search_state->terminate_partition_search == 0);
 
   // Set PARTITION_NONE for screen content.
-  if (cpi->is_screen_content_type)
+  if (cpi->use_screen_content_tools)
     part_search_state->partition_none_allowed =
         blk_params.has_rows && blk_params.has_cols;
 }
diff --git a/av1/encoder/partition_strategy.c b/av1/encoder/partition_strategy.c
index 8cc6b76..3f12e1f 100644
--- a/av1/encoder/partition_strategy.c
+++ b/av1/encoder/partition_strategy.c
@@ -1301,7 +1301,7 @@
   // A CNN-based speed feature pruning out either split or all non-split
   // partition in INTRA frame coding.
   const int try_intra_cnn_split =
-      !cpi->is_screen_content_type && frame_is_intra_only(cm) &&
+      !cpi->use_screen_content_tools && frame_is_intra_only(cm) &&
       cpi->sf.part_sf.intra_cnn_split &&
       cm->seq_params.sb_size >= BLOCK_64X64 && bsize <= BLOCK_64X64 &&
       bsize >= BLOCK_8X8 &&
@@ -1319,7 +1319,7 @@
   // must be done prior to PARTITION_SPLIT to propagate the initial mvs to a
   // smaller blocksize.
   const int try_split_only =
-      !cpi->is_screen_content_type &&
+      !cpi->use_screen_content_tools &&
       cpi->sf.part_sf.simple_motion_search_split && *do_square_split &&
       bsize >= BLOCK_8X8 &&
       mi_row + mi_size_high[bsize] <= mi_params->mi_rows &&
@@ -1337,7 +1337,7 @@
   // direction. The results are stored in prune_horz and prune_vert in order to
   // bypass future related pruning checks if a pruning decision has been made.
   const int try_prune_rect =
-      !cpi->is_screen_content_type &&
+      !cpi->use_screen_content_tools &&
       cpi->sf.part_sf.simple_motion_search_prune_rect &&
       !frame_is_intra_only(cm) && *do_rectangular_split &&
       (*do_square_split || *partition_none_allowed ||
diff --git a/av1/encoder/partition_strategy.h b/av1/encoder/partition_strategy.h
index 140f2c8..1c83551 100644
--- a/av1/encoder/partition_strategy.h
+++ b/av1/encoder/partition_strategy.h
@@ -270,7 +270,7 @@
   assert(IMPLIES(cpi->gf_group.size > 0,
                  cpi->gf_group.index < cpi->gf_group.size));
   const AV1_COMMON *const cm = &cpi->common;
-  return !frame_is_intra_only(cm) && !cpi->is_screen_content_type &&
+  return !frame_is_intra_only(cm) && !cpi->use_screen_content_tools &&
          cpi->sf.part_sf.auto_max_partition_based_on_simple_motion !=
              NOT_IN_USE &&
          sb_size == BLOCK_128X128 &&
diff --git a/av1/encoder/speed_features.c b/av1/encoder/speed_features.c
index 33b898b..f0cab24 100644
--- a/av1/encoder/speed_features.c
+++ b/av1/encoder/speed_features.c
@@ -370,7 +370,7 @@
   sf->rt_sf.use_real_time_ref_set = 0;
 
   if (cpi->twopass.fr_content_type == FC_GRAPHICS_ANIMATION ||
-      cpi->is_screen_content_type) {
+      cpi->use_screen_content_tools) {
     sf->mv_sf.exhaustive_searches_thresh = (1 << 20);
   } else {
     sf->mv_sf.exhaustive_searches_thresh = (1 << 25);
diff --git a/av1/encoder/tx_search.c b/av1/encoder/tx_search.c
index 5ab0bb0..df577c3 100644
--- a/av1/encoder/tx_search.c
+++ b/av1/encoder/tx_search.c
@@ -1939,7 +1939,7 @@
   if ((!is_inter && txfm_params->use_default_intra_tx_type) ||
       (is_inter && txfm_params->use_default_inter_tx_type)) {
     txk_allowed =
-        get_default_tx_type(0, xd, tx_size, cpi->is_screen_content_type);
+        get_default_tx_type(0, xd, tx_size, cpi->use_screen_content_tools);
   } else if (x->rd_model == LOW_TXFM_RD) {
     if (plane == 0) txk_allowed = DCT_DCT;
   }