aom: rtc: Add logic for setting sb_size for MT

From 20465886b01a04ddc5ac7fed4ac690b125c7838e Mon Sep 17 00:00:00 2001
From: Marco Paniconi <[EMAIL REDACTED]>
Date: Wed, 12 Jun 2024 15:18:16 -0700
Subject: [PATCH] rtc: Add logic for setting sb_size for MT

This was removed here, as fix to psnr regression:
https://aomedia-review.googlesource.com/c/aom/+/190624

But the underlying issue was resolved in:
https://aomedia-review.googlesource.com/c/aom/+/191005

So adding the logic back in (with small tuning), as we
measured ~10% speedup for 4 and 8 threads across some
~1080p screen clips.

Change-Id: I4e4ca9342544bfcc27635a0ac9569a26e81e9ff8
---
 av1/encoder/encoder_utils.c | 16 +++++++++++++++-
 1 file changed, 15 insertions(+), 1 deletion(-)

diff --git a/av1/encoder/encoder_utils.c b/av1/encoder/encoder_utils.c
index 2a549f894..3419c09ca 100644
--- a/av1/encoder/encoder_utils.c
+++ b/av1/encoder/encoder_utils.c
@@ -837,7 +837,21 @@ BLOCK_SIZE av1_select_sb_size(const AV1EncoderConfig *const oxcf, int width,
                ? BLOCK_128X128
                : BLOCK_64X64;
   } else if (oxcf->mode == REALTIME) {
-    return AOMMIN(width, height) > 720 ? BLOCK_128X128 : BLOCK_64X64;
+    if (oxcf->tune_cfg.content == AOM_CONTENT_SCREEN) {
+      const TileConfig *const tile_cfg = &oxcf->tile_cfg;
+      const int num_tiles =
+          (1 << tile_cfg->tile_columns) * (1 << tile_cfg->tile_rows);
+      // For multi-thread encode: if the number of (128x128) superblocks
+      // per tile is low use 64X64 superblock.
+      if (oxcf->row_mt == 1 && oxcf->max_threads >= 4 &&
+          oxcf->max_threads >= num_tiles && AOMMIN(width, height) >= 720 &&
+          (width * height) / (128 * 128 * num_tiles) < 40)
+        return BLOCK_64X64;
+      else
+        return AOMMIN(width, height) >= 720 ? BLOCK_128X128 : BLOCK_64X64;
+    } else {
+      return AOMMIN(width, height) > 720 ? BLOCK_128X128 : BLOCK_64X64;
+    }
   }
 
   // TODO(any): Possibly could improve this with a heuristic.