From 4c171e84c35d3e6342cf8a4d1bc7a77f429918d2 Mon Sep 17 00:00:00 2001 From: Emil Vardar Date: Wed, 13 Nov 2024 09:25:18 +0000 Subject: [PATCH] Prevent upscaling when calculating sample values. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Bug: webrtc:358039777 Change-Id: I33edc12f312d0d37eac0c39a913313a1aa6f1de5 Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/366942 Reviewed-by: Erik Språng Commit-Queue: Emil Vardar (xWF) Reviewed-by: Sergey Silkin Cr-Commit-Position: refs/heads/main@{#43394} --- .../halton_frame_sampler.cc | 7 ++++++ .../halton_frame_sampler_unittest.cc | 25 ++++++++++++++----- 2 files changed, 26 insertions(+), 6 deletions(-) diff --git a/video/corruption_detection/halton_frame_sampler.cc b/video/corruption_detection/halton_frame_sampler.cc index ea11217d9a..cd88310a10 100644 --- a/video/corruption_detection/halton_frame_sampler.cc +++ b/video/corruption_detection/halton_frame_sampler.cc @@ -173,6 +173,13 @@ std::vector GetSampleValuesForFrame( << std_dev_gaussian_blur << ".\n"; return {}; } + if (scaled_width > i420_frame_buffer->width() || + scaled_height > i420_frame_buffer->height()) { + RTC_LOG(LS_WARNING) + << "Upscaling causes corruption. Therefore, only down-scaling is " + "permissible."; + return {}; + } // Scale the frame to the desired resolution: // 1. Create a new buffer with the desired resolution. diff --git a/video/corruption_detection/halton_frame_sampler_unittest.cc b/video/corruption_detection/halton_frame_sampler_unittest.cc index 747884b9b5..3475c67bf4 100644 --- a/video/corruption_detection/halton_frame_sampler_unittest.cc +++ b/video/corruption_detection/halton_frame_sampler_unittest.cc @@ -157,6 +157,7 @@ TEST(HaltonFrameSamplerTest, FrameIsNotSampledWhenTimestampsAreEqual) { /*is_key_frame=*/false, /*rtp_timestamp=*/0, /*num_samples=*/1), _); } + #endif // GTEST_HAS_DEATH_TEST TEST(HaltonFrameSamplerGaussianFilteringTest, @@ -236,6 +237,18 @@ TEST(HaltonFrameSamplerGaussianFilteringTest, IsEmpty()); } +TEST(HaltonFrameSamplerGaussianFilteringTest, + ShouldReturnEmptyListWhenUpscaling) { + const scoped_refptr kDefaultI420Buffer = + MakeDefaultI420FrameBuffer(); + + EXPECT_THAT(GetSampleValuesForFrame(kDefaultI420Buffer, + MakeDefaultSampleCoordinates(), + /*scaled_width=*/8, /*scaled_height=*/8, + kDefaultStdDevGaussianBlur), + IsEmpty()); +} + TEST(HaltonFrameSamplerGaussianFilteringTest, ShouldReturnGivenValueWhenStdDevZero) { // 4x4 i420 frame data. @@ -337,8 +350,8 @@ TEST(HaltonFrameSamplerGaussianFilteringTest, {.row = 0.8, .column = 0.4}}; // With scaling. - const int kScaledWidth = 10; - const int kScaledHeight = 10; + const int kScaledWidth = 2; + const int kScaledHeight = 2; // No filtering. const double kStdDevGaussianBlur = 0.02; @@ -346,13 +359,13 @@ TEST(HaltonFrameSamplerGaussianFilteringTest, EXPECT_THAT( GetSampleValuesForFrame(kI420Buffer, kSampleCoordinates, kScaledWidth, kScaledHeight, kStdDevGaussianBlur), - ElementsAre(AllOf(Field(&FilteredSample::value, DoubleEq(96.0)), + ElementsAre(AllOf(Field(&FilteredSample::value, DoubleEq(131.0)), Field(&FilteredSample::plane, ImagePlane::kChroma)), - AllOf(Field(&FilteredSample::value, DoubleEq(30.0)), + AllOf(Field(&FilteredSample::value, DoubleEq(35.0)), Field(&FilteredSample::plane, ImagePlane::kChroma)), - AllOf(Field(&FilteredSample::value, DoubleEq(66.0)), + AllOf(Field(&FilteredSample::value, DoubleEq(131.0)), Field(&FilteredSample::plane, ImagePlane::kChroma)), - AllOf(Field(&FilteredSample::value, DoubleNear(127.0, 1.0)), + AllOf(Field(&FilteredSample::value, DoubleEq(98.0)), Field(&FilteredSample::plane, ImagePlane::kLuma)))); }