| /* | |
| * Copyright (c) 2026 The WebRTC project authors. All Rights Reserved. | |
| * | |
| * Use of this source code is governed by a BSD-style license | |
| * that can be found in the LICENSE file in the root of the source | |
| * tree. An additional intellectual property rights grant can be found | |
| * in the file PATENTS. All contributing project authors may | |
| * be found in the AUTHORS file in the root of the source tree. | |
| */ | |
| #include "modules/video_coding/rate_control/scene_analyzer.h" | |
| #include <algorithm> | |
| #include <cstddef> | |
| #include <cstdint> | |
| #include <cstdio> | |
| #include <cstring> | |
| #include <memory> | |
| #include <optional> | |
| #include <utility> | |
| #include <vector> | |
| #include "api/make_ref_counted.h" | |
| #include "api/scoped_refptr.h" | |
| #include "api/test/create_frame_generator.h" | |
| #include "api/test/frame_generator_interface.h" | |
| #include "api/video/i420_buffer.h" | |
| #include "api/video/nv12_buffer.h" | |
| #include "api/video/video_frame_buffer.h" | |
| #include "rtc_base/time_utils.h" | |
| #include "test/gmock.h" | |
| #include "test/gtest.h" | |
| #include "test/testsupport/file_utils.h" | |
| namespace webrtc { | |
| namespace { | |
| using ::testing::DoubleNear; | |
| using ::testing::ElementsAre; | |
| using ::testing::Eq; | |
| using ::testing::Gt; | |
| using ::testing::Optional; | |
| scoped_refptr<I420Buffer> I420FrameFromLuma(int width, | |
| int height, | |
| const std::vector<uint8_t>& y) { | |
| const int chroma_width = (width + 1) / 2; | |
| const int chroma_height = (height + 1) / 2; | |
| const std::vector<uint8_t> chroma( | |
| static_cast<size_t>(chroma_width) * chroma_height, 128); | |
| return I420Buffer::Copy(width, height, y.data(), width, chroma.data(), | |
| chroma_width, chroma.data(), chroma_width); | |
| } | |
| scoped_refptr<I420Buffer> SolidI420Frame(int width, int height, uint8_t luma) { | |
| return I420FrameFromLuma( | |
| width, height, | |
| std::vector<uint8_t>(static_cast<size_t>(width) * height, luma)); | |
| } | |
| scoped_refptr<NV12Buffer> SolidNv12Frame(int width, int height, uint8_t luma) { | |
| scoped_refptr<NV12Buffer> buf = NV12Buffer::Create(width, height); | |
| std::memset(buf->MutableDataY(), luma, | |
| static_cast<size_t>(buf->StrideY()) * height); | |
| std::memset(buf->MutableDataUV(), 128, | |
| static_cast<size_t>(buf->StrideUV()) * ((height + 1) / 2)); | |
| return buf; | |
| } | |
| std::vector<scoped_refptr<VideoFrameBuffer>> Frames(int width, | |
| int height, | |
| int count) { | |
| std::unique_ptr<test::FrameGeneratorInterface> generator = | |
| test::CreateSquareFrameGenerator( | |
| width, height, test::FrameGeneratorInterface::OutputType::kI420, | |
| /*num_squares=*/10); | |
| std::vector<scoped_refptr<VideoFrameBuffer>> frames; | |
| for (int i = 0; i < count; ++i) { | |
| frames.push_back(generator->NextFrame().buffer); | |
| } | |
| return frames; | |
| } | |
| TEST(SceneAnalyzerTest, DetectsSlideChange) { | |
| std::unique_ptr<test::FrameGeneratorInterface> generator = | |
| test::CreateFromYuvFileFrameGenerator( | |
| {test::ResourcePath("web_screenshot_1850_1110", "yuv"), | |
| test::ResourcePath("presentation_1850_1110", "yuv")}, | |
| 1850, 1110, /*frame_repeat_count=*/10); | |
| for (SceneAnalyzer::Options options : | |
| {SceneAnalyzer::Options{.downscale_factor = 8}, | |
| SceneAnalyzer::Options{.downscale_factor = 16}, | |
| SceneAnalyzer::Options{.downscale_factor = 8, .point_sample = true}, | |
| SceneAnalyzer::Options{.downscale_factor = 16, .point_sample = true}}) { | |
| SceneAnalyzer analyzer(options); | |
| std::vector<int> scene_changes; | |
| for (int i = 0; i < 20; ++i) { | |
| std::optional<SceneAnalyzer::Stats> stats = | |
| analyzer.Analyze(*generator->NextFrame().buffer, /*is_screen=*/true); | |
| if (stats && stats->scene_change) { | |
| scene_changes.push_back(i); | |
| } | |
| } | |
| EXPECT_THAT(scene_changes, ElementsAre(10)) | |
| << options.downscale_factor << " " << options.point_sample; | |
| } | |
| } | |
| TEST(SceneAnalyzerTest, StaticFramesAndResolutionChangeReset) { | |
| SceneAnalyzer analyzer; | |
| scoped_refptr<I420Buffer> dark = SolidI420Frame(640, 360, 20); | |
| scoped_refptr<I420Buffer> bright = SolidI420Frame(640, 360, 200); | |
| // First frame primes `previous_` and returns nullopt. | |
| EXPECT_THAT(analyzer.Analyze(*dark, /*is_screen=*/true), Eq(std::nullopt)); | |
| for (int i = 0; i < 3; ++i) { | |
| std::optional<SceneAnalyzer::Stats> stats = | |
| analyzer.Analyze(*dark, /*is_screen=*/true); | |
| ASSERT_TRUE(stats.has_value()); | |
| EXPECT_DOUBLE_EQ(stats->mean_abs_diff, 0.0); | |
| EXPECT_FALSE(stats->scene_change); | |
| } | |
| // Large uniform jump after > 2 frames since reset flags a scene change. | |
| std::optional<SceneAnalyzer::Stats> jump = | |
| analyzer.Analyze(*bright, /*is_screen=*/true); | |
| ASSERT_TRUE(jump.has_value()); | |
| EXPECT_THAT(jump->mean_abs_diff, DoubleNear(180.0, 1e-6)); | |
| EXPECT_TRUE(jump->scene_change); | |
| // Resolution change resets state and returns nullopt once. | |
| scoped_refptr<NV12Buffer> hd_nv12 = SolidNv12Frame(1280, 720, 200); | |
| EXPECT_THAT(analyzer.Analyze(*hd_nv12, /*is_screen=*/true), Eq(std::nullopt)); | |
| std::optional<SceneAnalyzer::Stats> hd_repeat = | |
| analyzer.Analyze(*hd_nv12, /*is_screen=*/true); | |
| ASSERT_TRUE(hd_repeat.has_value()); | |
| EXPECT_DOUBLE_EQ(hd_repeat->mean_abs_diff, 0.0); | |
| EXPECT_FALSE(hd_repeat->scene_change); | |
| } | |
| TEST(SceneAnalyzerTest, ResetClearsHistoryAndSuppressesNextFrame) { | |
| SceneAnalyzer analyzer; | |
| scoped_refptr<I420Buffer> dark = SolidI420Frame(640, 360, 20); | |
| scoped_refptr<I420Buffer> bright = SolidI420Frame(640, 360, 200); | |
| EXPECT_THAT(analyzer.Analyze(*dark, /*is_screen=*/true), Eq(std::nullopt)); | |
| ASSERT_TRUE(analyzer.Analyze(*dark, /*is_screen=*/true).has_value()); | |
| analyzer.Reset(); | |
| // First frame after Reset() primes history and returns nullopt again. | |
| EXPECT_THAT(analyzer.Analyze(*dark, /*is_screen=*/true), Eq(std::nullopt)); | |
| // During warmup (`frames_since_reset_ <= 2`), even a large jump does not flag | |
| // a scene change. | |
| std::optional<SceneAnalyzer::Stats> during_warmup = | |
| analyzer.Analyze(*bright, /*is_screen=*/true); | |
| ASSERT_TRUE(during_warmup.has_value()); | |
| EXPECT_FALSE(during_warmup->scene_change); | |
| } | |
| TEST(SceneAnalyzerTest, ComputesSpatialVarianceAndHandlesSmallFrames) { | |
| SceneAnalyzer analyzer; | |
| scoped_refptr<I420Buffer> solid = SolidI420Frame(256, 256, 100); | |
| EXPECT_THAT(analyzer.Analyze(*solid), Eq(std::nullopt)); | |
| std::optional<SceneAnalyzer::Stats> solid_stats = analyzer.Analyze(*solid); | |
| ASSERT_TRUE(solid_stats.has_value()); | |
| EXPECT_DOUBLE_EQ(solid_stats->spatial_variance, 0.0); | |
| // Split-luma frame: top half 40, bottom half 200 => mean 120, variance 6400. | |
| std::vector<uint8_t> split_y(256 * 256, 200); | |
| std::fill(split_y.begin(), split_y.begin() + 128 * 256, 40); | |
| scoped_refptr<I420Buffer> split = I420FrameFromLuma(256, 256, split_y); | |
| std::optional<SceneAnalyzer::Stats> split_stats = analyzer.Analyze(*split); | |
| ASSERT_TRUE(split_stats.has_value()); | |
| EXPECT_THAT(split_stats->spatial_variance, DoubleNear(6400.0, 1.0)); | |
| // Frames smaller than one 64x64 block return nullopt rather than a misleading | |
| // 0.0 SAD. | |
| scoped_refptr<I420Buffer> tiny_dark = SolidI420Frame(32, 32, 40); | |
| scoped_refptr<I420Buffer> tiny_bright = SolidI420Frame(32, 32, 220); | |
| EXPECT_THAT(analyzer.Analyze(*tiny_dark), Eq(std::nullopt)); | |
| EXPECT_THAT(analyzer.Analyze(*tiny_bright), Eq(std::nullopt)); | |
| // Small frames with 1..3 64x64 blocks still detect scene changes cleanly. | |
| scoped_refptr<I420Buffer> small_dark = SolidI420Frame(128, 64, 20); | |
| scoped_refptr<I420Buffer> small_bright = SolidI420Frame(128, 64, 220); | |
| EXPECT_THAT(analyzer.Analyze(*small_dark, /*is_screen=*/true), | |
| Eq(std::nullopt)); | |
| for (int i = 0; i < 3; ++i) { | |
| ASSERT_TRUE(analyzer.Analyze(*small_dark, /*is_screen=*/true).has_value()); | |
| } | |
| std::optional<SceneAnalyzer::Stats> small_jump = | |
| analyzer.Analyze(*small_bright, /*is_screen=*/true); | |
| ASSERT_TRUE(small_jump.has_value()); | |
| EXPECT_TRUE(small_jump->scene_change); | |
| // Small source resolution changes that round to the same downscaled size | |
| // (640 -> 633 at downscale_factor = 8 both round up to 80) still reset. | |
| scoped_refptr<I420Buffer> res_640 = SolidI420Frame(640, 360, 100); | |
| scoped_refptr<I420Buffer> res_633 = SolidI420Frame(633, 360, 100); | |
| EXPECT_THAT(analyzer.Analyze(*res_640), Eq(std::nullopt)); | |
| ASSERT_TRUE(analyzer.Analyze(*res_640).has_value()); | |
| EXPECT_THAT(analyzer.Analyze(*res_633), Eq(std::nullopt)); | |
| } | |
| TEST(SceneAnalyzerTest, StaticBlockMajoritySuppressesSceneChange) { | |
| SceneAnalyzer analyzer; | |
| // 4x4 grid of 64x64 blocks (16 blocks total). | |
| scoped_refptr<I420Buffer> base = SolidI420Frame(256, 256, 30); | |
| EXPECT_THAT(analyzer.Analyze(*base, /*is_screen=*/false), Eq(std::nullopt)); | |
| for (int i = 0; i < 3; ++i) { | |
| ASSERT_TRUE(analyzer.Analyze(*base, /*is_screen=*/false).has_value()); | |
| } | |
| // Change only 2 of 16 blocks (87.5% static): even a huge local luma jump must | |
| // not trigger a full-frame scene change. | |
| std::vector<uint8_t> local_y(256 * 256, 30); | |
| for (int y = 0; y < 64; ++y) { | |
| for (int x = 0; x < 128; ++x) { | |
| local_y[static_cast<size_t>(y) * 256 + x] = 240; | |
| } | |
| } | |
| scoped_refptr<I420Buffer> local_change = I420FrameFromLuma(256, 256, local_y); | |
| std::optional<SceneAnalyzer::Stats> local_stats = | |
| analyzer.Analyze(*local_change, /*is_screen=*/false); | |
| ASSERT_TRUE(local_stats.has_value()); | |
| EXPECT_THAT(local_stats->mean_abs_diff, Gt(0.0)); | |
| EXPECT_FALSE(local_stats->scene_change); | |
| } | |
| class FakeNativeVideoFrameBuffer : public VideoFrameBuffer { | |
| public: | |
| FakeNativeVideoFrameBuffer(int width, | |
| int height, | |
| scoped_refptr<I420BufferInterface> inspection_i420) | |
| : width_(width), | |
| height_(height), | |
| inspection_i420_(std::move(inspection_i420)) {} | |
| Type type() const override { return Type::kNative; } | |
| int width() const override { return width_; } | |
| int height() const override { return height_; } | |
| scoped_refptr<I420BufferInterface> ToI420() override { | |
| ++to_i420_calls_; | |
| return nullptr; | |
| } | |
| scoped_refptr<I420BufferInterface> ToI420ForInspection() override { | |
| ++to_i420_for_inspection_calls_; | |
| return inspection_i420_; | |
| } | |
| int to_i420_calls() const { return to_i420_calls_; } | |
| int to_i420_for_inspection_calls() const { | |
| return to_i420_for_inspection_calls_; | |
| } | |
| private: | |
| const int width_; | |
| const int height_; | |
| scoped_refptr<I420BufferInterface> inspection_i420_; | |
| int to_i420_calls_ = 0; | |
| int to_i420_for_inspection_calls_ = 0; | |
| }; | |
| TEST(SceneAnalyzerTest, UsesToI420ForInspectionFallbackAndHandlesNull) { | |
| SceneAnalyzer analyzer; | |
| scoped_refptr<FakeNativeVideoFrameBuffer> unmappable = | |
| make_ref_counted<FakeNativeVideoFrameBuffer>(256, 256, nullptr); | |
| EXPECT_THAT(analyzer.Analyze(*unmappable), Eq(std::nullopt)); | |
| EXPECT_EQ(unmappable->to_i420_for_inspection_calls(), 1); | |
| EXPECT_EQ(unmappable->to_i420_calls(), 0); | |
| scoped_refptr<FakeNativeVideoFrameBuffer> frame1 = | |
| make_ref_counted<FakeNativeVideoFrameBuffer>( | |
| 256, 256, SolidI420Frame(256, 256, 50)); | |
| scoped_refptr<FakeNativeVideoFrameBuffer> frame2 = | |
| make_ref_counted<FakeNativeVideoFrameBuffer>( | |
| 256, 256, SolidI420Frame(256, 256, 150)); | |
| EXPECT_THAT(analyzer.Analyze(*frame1), Eq(std::nullopt)); | |
| std::optional<SceneAnalyzer::Stats> stats = analyzer.Analyze(*frame2); | |
| ASSERT_TRUE(stats.has_value()); | |
| EXPECT_THAT(stats->mean_abs_diff, DoubleNear(100.0, 1e-6)); | |
| EXPECT_EQ(frame1->to_i420_for_inspection_calls(), 1); | |
| EXPECT_EQ(frame2->to_i420_for_inspection_calls(), 1); | |
| EXPECT_EQ(frame2->to_i420_calls(), 0); | |
| } | |
| template <typename BaseInterface> | |
| class FakePlanarYuv8Buffer : public BaseInterface { | |
| public: | |
| FakePlanarYuv8Buffer(int width, int height, int stride_y, uint8_t luma) | |
| : width_(width), | |
| height_(height), | |
| stride_y_(stride_y), | |
| y_(static_cast<size_t>(std::max(stride_y, 0)) * std::max(height, 0), | |
| luma) {} | |
| int width() const override { return width_; } | |
| int height() const override { return height_; } | |
| int StrideY() const override { return stride_y_; } | |
| int StrideU() const override { return width_; } | |
| int StrideV() const override { return width_; } | |
| const uint8_t* DataY() const override { return y_.data(); } | |
| const uint8_t* DataU() const override { return nullptr; } | |
| const uint8_t* DataV() const override { return nullptr; } | |
| scoped_refptr<I420BufferInterface> ToI420() override { | |
| ++conversion_calls_; | |
| return nullptr; | |
| } | |
| scoped_refptr<I420BufferInterface> ToI420ForInspection() override { | |
| ++conversion_calls_; | |
| return nullptr; | |
| } | |
| int conversion_calls() const { return conversion_calls_; } | |
| private: | |
| const int width_; | |
| const int height_; | |
| const int stride_y_; | |
| std::vector<uint8_t> y_; | |
| int conversion_calls_ = 0; | |
| }; | |
| TEST(SceneAnalyzerTest, ReadsI422AndI444DirectlyAndRejectsInvalidStride) { | |
| // I422 and I444 expose their 8-bit luma plane directly without falling back | |
| // to I420 conversion. | |
| { | |
| SceneAnalyzer analyzer; | |
| scoped_refptr<FakePlanarYuv8Buffer<I422BufferInterface>> dark = | |
| make_ref_counted<FakePlanarYuv8Buffer<I422BufferInterface>>( | |
| 256, 256, /*stride_y=*/288, 30); | |
| scoped_refptr<FakePlanarYuv8Buffer<I422BufferInterface>> bright = | |
| make_ref_counted<FakePlanarYuv8Buffer<I422BufferInterface>>( | |
| 256, 256, /*stride_y=*/288, 130); | |
| EXPECT_THAT(analyzer.Analyze(*dark), Eq(std::nullopt)); | |
| std::optional<SceneAnalyzer::Stats> stats = analyzer.Analyze(*bright); | |
| ASSERT_TRUE(stats.has_value()); | |
| EXPECT_THAT(stats->mean_abs_diff, DoubleNear(100.0, 1e-6)); | |
| EXPECT_EQ(dark->conversion_calls(), 0); | |
| EXPECT_EQ(bright->conversion_calls(), 0); | |
| } | |
| { | |
| SceneAnalyzer analyzer; | |
| scoped_refptr<FakePlanarYuv8Buffer<I444BufferInterface>> dark = | |
| make_ref_counted<FakePlanarYuv8Buffer<I444BufferInterface>>( | |
| 256, 256, /*stride_y=*/256, 40); | |
| scoped_refptr<FakePlanarYuv8Buffer<I444BufferInterface>> bright = | |
| make_ref_counted<FakePlanarYuv8Buffer<I444BufferInterface>>( | |
| 256, 256, /*stride_y=*/256, 160); | |
| EXPECT_THAT(analyzer.Analyze(*dark), Eq(std::nullopt)); | |
| std::optional<SceneAnalyzer::Stats> stats = analyzer.Analyze(*bright); | |
| ASSERT_TRUE(stats.has_value()); | |
| EXPECT_THAT(stats->mean_abs_diff, DoubleNear(120.0, 1e-6)); | |
| EXPECT_EQ(dark->conversion_calls(), 0); | |
| EXPECT_EQ(bright->conversion_calls(), 0); | |
| } | |
| // Invalid stride (`StrideY() < width()`) is rejected safely. | |
| { | |
| SceneAnalyzer analyzer; | |
| scoped_refptr<FakePlanarYuv8Buffer<I422BufferInterface>> bad_stride = | |
| make_ref_counted<FakePlanarYuv8Buffer<I422BufferInterface>>( | |
| 256, 256, /*stride_y=*/128, 50); | |
| EXPECT_THAT(analyzer.Analyze(*bad_stride), Eq(std::nullopt)); | |
| EXPECT_EQ(bad_stride->conversion_calls(), 0); | |
| } | |
| } | |
| // Not a pass/fail test; prints `SCENE_ANALYZER_COST` lines. Meaningful in | |
| // release builds only. | |
| TEST(SceneAnalyzerTest, DISABLED_Cost) { | |
| constexpr int kNumFrames = 300; | |
| struct Res { | |
| const char* name; | |
| int width; | |
| int height; | |
| }; | |
| for (const Res& res : {Res{"720p", 1280, 720}, Res{"1080p", 1920, 1080}}) { | |
| const std::vector<scoped_refptr<VideoFrameBuffer>> frames = | |
| Frames(res.width, res.height, 30); | |
| for (SceneAnalyzer::Options options : | |
| {SceneAnalyzer::Options{.downscale_factor = 8}, | |
| SceneAnalyzer::Options{.downscale_factor = 16}, | |
| SceneAnalyzer::Options{.downscale_factor = 8, .point_sample = true}, | |
| SceneAnalyzer::Options{.downscale_factor = 16, | |
| .point_sample = true}}) { | |
| SceneAnalyzer analyzer(options); | |
| const int64_t start_us = TimeMicros(); | |
| for (int i = 0; i < kNumFrames; ++i) { | |
| [[maybe_unused]] std::optional<SceneAnalyzer::Stats> stats = | |
| analyzer.Analyze(*frames[i % frames.size()], /*is_screen=*/false); | |
| } | |
| const double us_per_frame = | |
| static_cast<double>(TimeMicros() - start_us) / kNumFrames; | |
| printf( | |
| "SCENE_ANALYZER_COST res=%s factor=%d point=%d us_per_frame=%.1f\n", | |
| res.name, options.downscale_factor, options.point_sample ? 1 : 0, | |
| us_per_frame); | |
| } | |
| } | |
| } | |
| } // namespace | |
| } // namespace webrtc |