From 1dcf202ffe88a6de007f1331808680576fcc955d Mon Sep 17 00:00:00 2001 From: Guillaume Petit Date: Thu, 14 Nov 2024 15:28:03 +0100 Subject: [PATCH] Fixes a linear interpolation bad access MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Bug: webrtc:353425611 Change-Id: I9c38428d2463d9d76047ad5be84ed57cba9ebb72 Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/367981 Commit-Queue: Harald Alvestrand Reviewed-by: Harald Alvestrand Reviewed-by: Erik Språng Cr-Commit-Position: refs/heads/main@{#43408} --- AUTHORS | 1 + rtc_base/experiments/encoder_info_settings.cc | 6 +++ .../encoder_info_settings_unittest.cc | 45 +++++++++++++++++++ 3 files changed, 52 insertions(+) diff --git a/AUTHORS b/AUTHORS index 9424a61de1..f4dd64134d 100644 --- a/AUTHORS +++ b/AUTHORS @@ -56,6 +56,7 @@ Frederik Riedel, Frogg GmbH Gao Chun Giji Gangadharan Graham Yoakum +Guillaume Petit Gustavo Garcia Hans Knoechel Helmut Januschka diff --git a/rtc_base/experiments/encoder_info_settings.cc b/rtc_base/experiments/encoder_info_settings.cc index 82480ea298..eabf8b182d 100644 --- a/rtc_base/experiments/encoder_info_settings.cc +++ b/rtc_base/experiments/encoder_info_settings.cc @@ -144,6 +144,12 @@ EncoderInfoSettings::GetSinglecastBitrateLimitForResolutionWhenQpIsUntrusted( return bitrate_limits[interpolation_index]; } + // 0 means our resolution is smaller than the smallest resolution in the list, + // we will select smallest data as the return result. + if (interpolation_index == 0) { + return *bitrate_limits.begin(); + } + // No matching resolution, do a linear interpolate. int lower_pixel_count = bitrate_limits[interpolation_index - 1].frame_size_pixels; diff --git a/rtc_base/experiments/encoder_info_settings_unittest.cc b/rtc_base/experiments/encoder_info_settings_unittest.cc index 916f980ebe..70c0b7f0ba 100644 --- a/rtc_base/experiments/encoder_info_settings_unittest.cc +++ b/rtc_base/experiments/encoder_info_settings_unittest.cc @@ -103,4 +103,49 @@ TEST(EncoderSettingsTest, CommonSettingsUsedIfEncoderNameUnspecified) { EXPECT_EQ(3u, vp9_settings.requested_resolution_alignment()); } +TEST(GetSinglecastBitrateLimitForResolutionWhenQpIsUntrustedTests, + LinearInterpolationUnderflow) { + std::optional frame_size_pixels = 480 * 360; + std::vector resolution_bitrate_limits( + {{1280 * 720, 1500000, 30000, 2500000}, + {1920 * 1080, 2500000, 30000, 4000000}}); + + const auto resolutionBitrateLimit = EncoderInfoSettings:: + GetSinglecastBitrateLimitForResolutionWhenQpIsUntrusted( + frame_size_pixels, resolution_bitrate_limits); + EXPECT_TRUE(resolutionBitrateLimit.has_value()); + EXPECT_EQ(resolutionBitrateLimit.value(), resolution_bitrate_limits.front()); +} + +TEST(GetSinglecastBitrateLimitForResolutionWhenQpIsUntrustedTests, + LinearInterpolationOverflow) { + std::optional frame_size_pixels = 4096 * 2160; + std::vector resolution_bitrate_limits( + {{1280 * 720, 1500000, 30000, 2500000}, + {1920 * 1080, 2500000, 30000, 4000000}}); + + const auto resolutionBitrateLimit = EncoderInfoSettings:: + GetSinglecastBitrateLimitForResolutionWhenQpIsUntrusted( + frame_size_pixels, resolution_bitrate_limits); + EXPECT_TRUE(resolutionBitrateLimit.has_value()); + EXPECT_EQ(resolutionBitrateLimit.value(), resolution_bitrate_limits.back()); +} + +TEST(GetSinglecastBitrateLimitForResolutionWhenQpIsUntrustedTests, + LinearInterpolationExactMatch) { + std::optional frame_size_pixels = 1920 * 1080; + VideoEncoder::ResolutionBitrateLimits expected_match{1920 * 1080, 2500000, + 30000, 4000000}; + std::vector resolution_bitrate_limits( + {{1280 * 720, 1500000, 30000, 2500000}, + expected_match, + {4096 * 2160, 4000000, 30000, 8000000}}); + + const auto resolutionBitrateLimit = EncoderInfoSettings:: + GetSinglecastBitrateLimitForResolutionWhenQpIsUntrusted( + frame_size_pixels, resolution_bitrate_limits); + EXPECT_TRUE(resolutionBitrateLimit.has_value()); + EXPECT_EQ(resolutionBitrateLimit.value(), expected_match); +} + } // namespace webrtc