rtc-.screen: Rework the logic for setting the high motion flag

Move the reset of rc->num_col/row_roll_blscroll_last_tl0 to after
the frame dropping, so it corresponds to the last encoded frame
when used in the scene_detection. And add condition on the last
frame where the source_sad was low. This improves the setting of
rc->high_motion_screen_content_rtc.

This fixes the encode_time regression noted in the issue below,
and still has the reduced quality dips that was acheived in:
https://aomedia-review.googlesource.com/c/aom/+/203801

Bug: b:444116924
Change-Id: Ie63e54228b1616341a802e525b78601dc7dc92da
diff --git a/av1/encoder/encoder.c b/av1/encoder/encoder.c
index 4dc4258..5d47c1c 100644
--- a/av1/encoder/encoder.c
+++ b/av1/encoder/encoder.c
@@ -3175,6 +3175,11 @@
   // frame.
   if (!frame_is_intra_only(cm)) av1_pick_and_set_high_precision_mv(cpi, q);
 
+  if (svc->temporal_layer_id == 0) {
+    cpi->rc.num_col_blscroll_last_tl0 = 0;
+    cpi->rc.num_row_blscroll_last_tl0 = 0;
+  }
+
   // transform / motion compensation build reconstruction frame
   av1_encode_frame(cpi);
 
diff --git a/av1/encoder/ratectrl.c b/av1/encoder/ratectrl.c
index 7a2ea67..161527d 100644
--- a/av1/encoder/ratectrl.c
+++ b/av1/encoder/ratectrl.c
@@ -499,6 +499,7 @@
   rc->force_max_q = 0;
   rc->postencode_drop = 0;
   rc->frames_since_scene_change = 0;
+  rc->last_frame_low_source_sad = 0;
 }
 
 static bool check_buffer_below_thresh(AV1_COMP *cpi, int64_t buffer_level,
@@ -2485,6 +2486,8 @@
   rc->frame_number_encoded++;
   rc->prev_frame_is_dropped = 0;
   rc->drop_count_consec = 0;
+  if (rc->frame_source_sad < 10000)
+    rc->last_frame_low_source_sad = current_frame->frame_number;
 }
 
 void av1_rc_postencode_update_drop_frame(AV1_COMP *cpi) {
@@ -3348,12 +3351,12 @@
   }
   // Update the high_motion_content_screen_rtc flag on TL0. Avoid the update
   // if too many consecutive frame drops occurred.
-  // The threshold_high_motion is set to a large value, to account for
+  // The threshold_high_motion is kept to a large value, to account for
   // mis-detection for scroll for scaled input (where scroll motion can be
-  // subpel and not detected below). The threshold may be reduced when better
+  // subpel and not detected below). The threshold may be improved when better
   // scroll detection (for subpel) is added.
   const int scale =
-      (unscaled_src->y_width * unscaled_src->y_height >= 1920 * 1080) ? 40 : 16;
+      (unscaled_src->y_width * unscaled_src->y_height > 1920 * 1080) ? 30 : 10;
   const uint64_t thresh_high_motion = scale * 64 * 64;
   if (cpi->svc.temporal_layer_id == 0 && rc->drop_count_consec < 3) {
     cpi->rc.high_motion_content_screen_rtc = 0;
@@ -3364,6 +3367,7 @@
         rc->percent_blocks_with_motion > 40 &&
         rc->prev_avg_source_sad > thresh_high_motion &&
         rc->avg_source_sad > thresh_high_motion &&
+        cm->current_frame.frame_number - rc->last_frame_low_source_sad > 10 &&
         rc->avg_frame_low_motion < 60 && unscaled_src->y_width >= 1280 &&
         unscaled_src->y_height >= 720) {
       cpi->rc.high_motion_content_screen_rtc = 1;
@@ -3867,10 +3871,6 @@
     resize_reset_rc(cpi, resize_pending_params->width,
                     resize_pending_params->height, cm->width, cm->height);
   }
-  if (svc->temporal_layer_id == 0) {
-    rc->num_col_blscroll_last_tl0 = 0;
-    rc->num_row_blscroll_last_tl0 = 0;
-  }
   // Set the GF interval and update flag.
   if (!rc->rtc_external_ratectrl)
     set_gf_interval_update_onepass_rt(cpi, *frame_type);
diff --git a/av1/encoder/ratectrl.h b/av1/encoder/ratectrl.h
index 88c048f..cd376ba 100644
--- a/av1/encoder/ratectrl.h
+++ b/av1/encoder/ratectrl.h
@@ -194,6 +194,7 @@
   uint64_t avg_source_sad;
   uint64_t prev_avg_source_sad;
   uint64_t frame_source_sad;
+  unsigned int last_frame_low_source_sad;
   uint64_t frame_spatial_variance;
   int static_since_last_scene_change;
   int last_encoded_size_keyframe;