Set av1 speed from resolution.

Use speed 6 for better quality for low resolution, speed 8 for HD for better speed.
This will better balance speed and quality.

Change-Id: I3d8dbd45533471ce58d53c1ac26f92c7b1106259
Bug: None
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/175281
Reviewed-by: Marco Paniconi <marpan@webrtc.org>
Reviewed-by: Danil Chapovalov <danilchap@webrtc.org>
Commit-Queue: Jerome Jiang <jianj@google.com>
Cr-Commit-Position: refs/heads/master@{#31336}
diff --git a/modules/video_coding/codecs/av1/libaom_av1_encoder.cc b/modules/video_coding/codecs/av1/libaom_av1_encoder.cc
index 6a01165..c34e0b8 100644
--- a/modules/video_coding/codecs/av1/libaom_av1_encoder.cc
+++ b/modules/video_coding/codecs/av1/libaom_av1_encoder.cc
@@ -39,7 +39,6 @@
 // Encoder configuration parameters
 constexpr int kQpMax = 56;
 constexpr int kQpMin = 10;
-constexpr int kDefaultEncSpeed = 7;  // Use values 6, 7, or 8 for RTC.
 constexpr int kUsageProfile = 1;     // 0 = good quality; 1 = real-time.
 constexpr int kMinQindex = 58;       // Min qindex threshold for QP scaling.
 constexpr int kMaxQindex = 180;      // Max qindex threshold for QP scaling.
@@ -48,6 +47,19 @@
 constexpr int kRtpTicksPerSecond = 90000;
 constexpr float kMinimumFrameRate = 1.0;
 
+// Only positive speeds, range for real-time coding currently is: 6 - 8.
+// Lower means slower/better quality, higher means fastest/lower quality.
+int GetCpuSpeed(int width, int height) {
+  // For smaller resolutions, use lower speed setting (get some coding gain at
+  // the cost of increased encoding complexity).
+  if (width * height <= 320 * 180)
+    return 6;
+  else if (width * height >= 1280 * 720)
+    return 8;
+  else
+    return 7;
+}
+
 class LibaomAv1Encoder final : public VideoEncoder {
  public:
   explicit LibaomAv1Encoder(
@@ -189,7 +201,8 @@
   inited_ = true;
 
   // Set control parameters
-  ret = aom_codec_control(&ctx_, AOME_SET_CPUUSED, kDefaultEncSpeed);
+  ret = aom_codec_control(&ctx_, AOME_SET_CPUUSED,
+                          GetCpuSpeed(cfg_.g_w, cfg_.g_h));
   if (ret != AOM_CODEC_OK) {
     RTC_LOG(LS_WARNING) << "LibaomAv1Encoder::EncodeInit returned " << ret
                         << " on control AV1E_SET_CPUUSED.";