aom: Add a speed feature for screen content type in rtc

From 407b9866d40a58da32ed8de4f9102fe93eef6ee2 Mon Sep 17 00:00:00 2001
From: Cheng Chen <[EMAIL REDACTED]>
Date: Wed, 8 May 2024 15:25:17 -0700
Subject: [PATCH] Add a speed feature for screen content type in rtc

This speed feature filters blocks so that less blocks will go
through the palette search path.

Disabling this feature leads to better coding gain but higher
key frame encoding time.

For speed 10 and 11, the feature is turned off for faster
key frame encoding.

For rtc_screen test set, if disabling the feature, the coding
performance is
              psnr        ssim        vmaf        encoding_time
Speed 10    -1.334%     -2.233%     -1.420%        -0.261%
Speed 11    -2.610%     -4.344%     -2.601%        0.207%

Change-Id: I0d2a729c806f7b2780c749fa6df169fbeb2758b8
---
 av1/encoder/nonrd_pickmode.c | 11 ++++++++---
 av1/encoder/speed_features.c |  2 ++
 av1/encoder/speed_features.h |  6 ++++++
 3 files changed, 16 insertions(+), 3 deletions(-)

diff --git a/av1/encoder/nonrd_pickmode.c b/av1/encoder/nonrd_pickmode.c
index ed64056e4..d08e9c0ac 100644
--- a/av1/encoder/nonrd_pickmode.c
+++ b/av1/encoder/nonrd_pickmode.c
@@ -23,6 +23,7 @@
 #include "av1/encoder/model_rd.h"
 #include "av1/encoder/motion_search_facade.h"
 #include "av1/encoder/nonrd_opt.h"
+#include "av1/encoder/palette.h"
 #include "av1/encoder/reconinter_enc.h"
 #include "av1/encoder/var_based_part.h"
 
@@ -1655,11 +1656,15 @@ void av1_nonrd_pick_intra_mode(AV1_COMP *cpi, MACROBLOCK *x, RD_STATS *rd_cost,
 
   // Try palette if it's enabled.
   bool try_palette =
-      (!args.prune_mode_based_on_sad || best_sad_norm > thresh_sad) &&
-      cpi->oxcf.tool_cfg.enable_palette && bsize <= BLOCK_16X16 &&
-      x->source_variance > 200 &&
+      cpi->oxcf.tool_cfg.enable_palette &&
       av1_allow_palette(cpi->common.features.allow_screen_content_tools,
                         mi->bsize);
+  if (cpi->sf.rt_sf.prune_screen_palette_search) {
+    bool prune =
+        (!args.prune_mode_based_on_sad || best_sad_norm > thresh_sad) &&
+        bsize <= BLOCK_16X16 && x->source_variance > 200;
+    try_palette &= prune;
+  }
   if (try_palette) {
     const TxfmSearchInfo *txfm_info = &x->txfm_search_info;
     const unsigned int intra_ref_frame_cost = 0;
diff --git a/av1/encoder/speed_features.c b/av1/encoder/speed_features.c
index 671986600..6fd01d8fc 100644
--- a/av1/encoder/speed_features.c
+++ b/av1/encoder/speed_features.c
@@ -1579,6 +1579,7 @@ static void set_rt_speed_feature_framesize_dependent(const AV1_COMP *const cpi,
       sf->rt_sf.nonrd_aggressive_skip = 1;
       sf->rt_sf.thresh_active_maps_skip_lf_cdef = 90;
       sf->rt_sf.hybrid_intra_pickmode = 0;
+      sf->rt_sf.prune_screen_palette_search = true;
       sf->rt_sf.prune_intra_mode_using_best_sad_so_far = true;
     }
     if (speed >= 11) {
@@ -2253,6 +2254,7 @@ static AOM_INLINE void init_rt_sf(REAL_TIME_SPEED_FEATURES *rt_sf) {
   rt_sf->use_nonrd_filter_search = 0;
   rt_sf->use_simple_rd_model = 0;
   rt_sf->hybrid_intra_pickmode = 0;
+  rt_sf->prune_screen_palette_search = false;
   rt_sf->source_metrics_sb_nonrd = 0;
   rt_sf->overshoot_detection_cbr = NO_DETECTION;
   rt_sf->check_scene_detection = 0;
diff --git a/av1/encoder/speed_features.h b/av1/encoder/speed_features.h
index d6b294927..300d7e190 100644
--- a/av1/encoder/speed_features.h
+++ b/av1/encoder/speed_features.h
@@ -1629,6 +1629,12 @@ typedef struct REAL_TIME_SPEED_FEATURES {
   // 2 : use rd for bsize < 16x16 and src var >= 101, nonrd otherwise
   int hybrid_intra_pickmode;
 
+  // Filter blocks by certain criteria such as SAD, source variance, such that
+  // fewer blocks will go through the palette search.
+  // For screen content types, enable this feature reduces key frame encoding
+  // time. Disabling it leads to better compression efficiency.
+  bool prune_screen_palette_search;
+
   // Compute variance/sse on source difference, prior to encoding superblock.
   int source_metrics_sb_nonrd;