Update the resolution check for VP8 simulcast.

To support non-default values of scale_resolution_down_by.

Bug: webrtc:10069
Change-Id: I7efb39cc06a895986f9583acc2180245c009a7fa
Reviewed-on: https://webrtc-review.googlesource.com/c/121650
Commit-Queue: Mirta Dvornicic <mirtad@webrtc.org>
Reviewed-by: Rasmus Brandt <brandtr@webrtc.org>
Reviewed-by: Ilya Nikolaevskiy <ilnik@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#26677}
diff --git a/modules/video_coding/codecs/vp8/test/vp8_impl_unittest.cc b/modules/video_coding/codecs/vp8/test/vp8_impl_unittest.cc
index 06d9448..555f978 100644
--- a/modules/video_coding/codecs/vp8/test/vp8_impl_unittest.cc
+++ b/modules/video_coding/codecs/vp8/test/vp8_impl_unittest.cc
@@ -242,30 +242,30 @@
 
 TEST_F(TestVp8Impl, ChecksSimulcastSettings) {
   codec_settings_.numberOfSimulcastStreams = 2;
-  // Reslutions are not scaled by 2, temporal layers do not match.
+  // Resolutions are not in ascending order, temporal layers do not match.
   codec_settings_.simulcastStream[0] = {kWidth, kHeight, kFramerateFps, 2,
                                         4000,   3000,    2000,          80};
-  codec_settings_.simulcastStream[1] = {kWidth, kHeight, 30,   3,
-                                        4000,   3000,    2000, 80};
+  codec_settings_.simulcastStream[1] = {kWidth / 2, kHeight / 2, 30,   3,
+                                        4000,       3000,        2000, 80};
   EXPECT_EQ(WEBRTC_VIDEO_CODEC_ERR_SIMULCAST_PARAMETERS_NOT_SUPPORTED,
             encoder_->InitEncode(&codec_settings_, kNumCores, kMaxPayloadSize));
   codec_settings_.numberOfSimulcastStreams = 3;
-  // Reslutions are not scaled by 2.
+  // Resolutions are not in ascending order.
   codec_settings_.simulcastStream[0] = {
       kWidth / 2, kHeight / 2, kFramerateFps, 1, 4000, 3000, 2000, 80};
   codec_settings_.simulcastStream[1] = {
-      kWidth / 2, kHeight / 2, kFramerateFps, 1, 4000, 3000, 2000, 80};
+      kWidth / 2 - 1, kHeight / 2 - 1, kFramerateFps, 1, 4000, 3000, 2000, 80};
   codec_settings_.simulcastStream[2] = {kWidth, kHeight, 30,   1,
                                         4000,   3000,    2000, 80};
   EXPECT_EQ(WEBRTC_VIDEO_CODEC_ERR_SIMULCAST_PARAMETERS_NOT_SUPPORTED,
             encoder_->InitEncode(&codec_settings_, kNumCores, kMaxPayloadSize));
-  // Reslutions are not scaled by 2.
+  // Resolutions are not in ascending order.
   codec_settings_.simulcastStream[0] = {kWidth, kHeight, kFramerateFps, 1,
                                         4000,   3000,    2000,          80};
   codec_settings_.simulcastStream[1] = {kWidth, kHeight, kFramerateFps, 1,
                                         4000,   3000,    2000,          80};
-  codec_settings_.simulcastStream[2] = {kWidth, kHeight, kFramerateFps, 1,
-                                        4000,   3000,    2000,          80};
+  codec_settings_.simulcastStream[2] = {
+      kWidth - 1, kHeight - 1, kFramerateFps, 1, 4000, 3000, 2000, 80};
   EXPECT_EQ(WEBRTC_VIDEO_CODEC_ERR_SIMULCAST_PARAMETERS_NOT_SUPPORTED,
             encoder_->InitEncode(&codec_settings_, kNumCores, kMaxPayloadSize));
   // Temporal layers do not match.
@@ -296,6 +296,16 @@
                                         4000,   3000,    2000,          80};
   EXPECT_EQ(WEBRTC_VIDEO_CODEC_OK,
             encoder_->InitEncode(&codec_settings_, kNumCores, kMaxPayloadSize));
+  // Everything fine: custom scaling, top resolution matches video, temporal
+  // settings are the same for all layers.
+  codec_settings_.simulcastStream[0] = {
+      kWidth / 4, kHeight / 4, kFramerateFps, 1, 4000, 3000, 2000, 80};
+  codec_settings_.simulcastStream[1] = {kWidth, kHeight, kFramerateFps, 1,
+                                        4000,   3000,    2000,          80};
+  codec_settings_.simulcastStream[2] = {kWidth, kHeight, kFramerateFps, 1,
+                                        4000,   3000,    2000,          80};
+  EXPECT_EQ(WEBRTC_VIDEO_CODEC_OK,
+            encoder_->InitEncode(&codec_settings_, kNumCores, kMaxPayloadSize));
 }
 
 #if defined(WEBRTC_ANDROID)
diff --git a/modules/video_coding/utility/simulcast_utility.cc b/modules/video_coding/utility/simulcast_utility.cc
index 9966682..b517455 100644
--- a/modules/video_coding/utility/simulcast_utility.cc
+++ b/modules/video_coding/utility/simulcast_utility.cc
@@ -47,10 +47,20 @@
       return false;
     }
   }
-  for (int i = 1; i < num_streams; ++i) {
-    if (codec.simulcastStream[i].width !=
-        codec.simulcastStream[i - 1].width * 2) {
-      return false;
+  if (codec.codecType == webrtc::kVideoCodecVP8) {
+    for (int i = 1; i < num_streams; ++i) {
+      if (codec.simulcastStream[i].width < codec.simulcastStream[i - 1].width) {
+        return false;
+      }
+    }
+  } else {
+    // TODO(mirtad): H264 encoder implementation still assumes the default
+    // resolution downscaling is used.
+    for (int i = 1; i < num_streams; ++i) {
+      if (codec.simulcastStream[i].width !=
+          codec.simulcastStream[i - 1].width * 2) {
+        return false;
+      }
     }
   }
   return true;