blob: 76ed8db26d8c492111899e7b326da9f209995d7a [file]
/*
* 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 <cstdlib>
#include <optional>
#include <vector>
#include "api/scoped_refptr.h"
#include "api/video/video_frame_buffer.h"
#include "rtc_base/checks.h"
#include "third_party/libyuv/include/libyuv/scale.h"
namespace webrtc {
namespace {
// Block size used by libaom's `av1_rc_scene_detection_onepass_rt`.
constexpr int kSceneBlockSizePx = 64;
// Minimum per-64x64-block SAD before flagging a scene change (~1.95 luma/px
// for screen, ~2.44 luma/px for camera).
constexpr double kScreenMinSadPer64x64 = 8000.0;
constexpr double kCameraMinSadPer64x64 = 10000.0;
// Required jump factor over the recent smoothed SAD.
constexpr double kScreenSadJumpFactor = 5.0;
constexpr double kCameraSadJumpFactor = 6.0;
int ComputeBlockSizePx(int downscale_factor) {
RTC_CHECK_GT(downscale_factor, 0);
RTC_CHECK_EQ(kSceneBlockSizePx % downscale_factor, 0);
return kSceneBlockSizePx / downscale_factor;
}
} // namespace
SceneAnalyzer::SceneAnalyzer(const Options& options)
: options_(options),
block_size_px_(ComputeBlockSizePx(options.downscale_factor)) {}
void SceneAnalyzer::Reset() {
previous_.clear();
current_.clear();
src_width_px_ = 0;
src_height_px_ = 0;
frames_since_reset_ = 0;
avg_source_sad_per_64x64_ = 0.0;
}
std::optional<SceneAnalyzer::Stats> SceneAnalyzer::Analyze(
VideoFrameBuffer& frame,
bool is_screen) {
const uint8_t* src_y = nullptr;
int src_stride_y_px = 0;
int src_width_px = frame.width();
int src_height_px = frame.height();
scoped_refptr<I420BufferInterface> mapped_i420;
switch (frame.type()) {
case VideoFrameBuffer::Type::kNV12:
if (const NV12BufferInterface* nv12 = frame.GetNV12()) {
src_y = nv12->DataY();
src_stride_y_px = nv12->StrideY();
}
break;
case VideoFrameBuffer::Type::kI422:
if (const I422BufferInterface* i422 = frame.GetI422()) {
src_y = i422->DataY();
src_stride_y_px = i422->StrideY();
}
break;
case VideoFrameBuffer::Type::kI444:
if (const I444BufferInterface* i444 = frame.GetI444()) {
src_y = i444->DataY();
src_stride_y_px = i444->StrideY();
}
break;
default:
if (const I420BufferInterface* i420 = frame.GetI420()) {
src_y = i420->DataY();
src_stride_y_px = i420->StrideY();
} else {
mapped_i420 = frame.ToI420ForInspection();
if (!mapped_i420) {
return std::nullopt;
}
src_y = mapped_i420->DataY();
src_stride_y_px = mapped_i420->StrideY();
src_width_px = mapped_i420->width();
src_height_px = mapped_i420->height();
}
break;
}
if (src_y == nullptr || src_width_px <= 0 || src_height_px <= 0 ||
src_stride_y_px < src_width_px) {
return std::nullopt;
}
const int factor = options_.downscale_factor;
const int width_px = (src_width_px + factor - 1) / factor;
const int height_px = (src_height_px + factor - 1) / factor;
const size_t num_pixels = static_cast<size_t>(width_px) * height_px;
const libyuv::FilterMode filter =
options_.point_sample ? libyuv::kFilterNone : libyuv::kFilterBox;
if (previous_.empty() || src_width_px != src_width_px_ ||
src_height_px != src_height_px_) {
src_width_px_ = src_width_px;
src_height_px_ = src_height_px;
frames_since_reset_ = 0;
avg_source_sad_per_64x64_ = 0.0;
previous_.resize(num_pixels);
libyuv::ScalePlane(src_y, src_stride_y_px, src_width_px, src_height_px,
previous_.data(), width_px, width_px, height_px, filter);
return std::nullopt;
}
current_.resize(num_pixels);
libyuv::ScalePlane(src_y, src_stride_y_px, src_width_px, src_height_px,
current_.data(), width_px, width_px, height_px, filter);
++frames_since_reset_;
// SAD per 64x64 block of the full-resolution frame, skipping partial border
// blocks like libaom.
const int pixels_per_sample = factor * factor;
const int block_cols = width_px / block_size_px_;
const int block_rows = height_px / block_size_px_;
uint64_t total_sad = 0;
int num_blocks = 0;
int num_zero_sad_blocks = 0;
for (int by = 0; by < block_rows; ++by) {
for (int bx = 0; bx < block_cols; ++bx) {
uint64_t block_sad = 0;
for (int y = by * block_size_px_; y < (by + 1) * block_size_px_; ++y) {
for (int x = bx * block_size_px_; x < (bx + 1) * block_size_px_; ++x) {
const size_t i = static_cast<size_t>(y) * width_px + x;
block_sad += std::abs(current_[i] - previous_[i]);
}
}
block_sad *= pixels_per_sample;
total_sad += block_sad;
num_zero_sad_blocks += block_sad == 0 ? 1 : 0;
++num_blocks;
}
}
double sum = 0.0;
double sum_squares = 0.0;
for (uint8_t value : current_) {
const double sample = value;
sum += sample;
sum_squares += sample * sample;
}
const double mean = sum / current_.size();
previous_.swap(current_);
if (num_blocks == 0) {
// Frame is smaller than a single 64x64 analysis block; omit stats rather
// than reporting a misleading 0.0 SAD that looks like a static frame.
return std::nullopt;
}
Stats stats;
stats.spatial_variance =
std::max(0.0, sum_squares / previous_.size() - mean * mean);
const double avg_sad_per_64x64 = static_cast<double>(total_sad) / num_blocks;
stats.mean_abs_diff =
avg_sad_per_64x64 / (kSceneBlockSizePx * kSceneBlockSizePx);
// Flag a scene change on a sharp SAD jump over the smoothed baseline, after
// 2 warmup diffs, unless too many blocks stayed static (allowing up to 75%
// static blocks on very large jumps such as a window swap on a desktop, or
// 50% otherwise).
const double min_threshold_sad_per_64x64 =
is_screen ? kScreenMinSadPer64x64 : kCameraMinSadPer64x64;
const double jump_factor =
is_screen ? kScreenSadJumpFactor : kCameraSadJumpFactor;
const int max_zero_sad_blocks =
avg_sad_per_64x64 > 8 * min_threshold_sad_per_64x64
? std::max(1, (3 * num_blocks) / 4)
: std::max(1, num_blocks / 2);
stats.scene_change =
avg_sad_per_64x64 > std::max(min_threshold_sad_per_64x64,
avg_source_sad_per_64x64_ * jump_factor) &&
frames_since_reset_ > 2 && num_zero_sad_blocks < max_zero_sad_blocks;
avg_source_sad_per_64x64_ =
(3 * avg_source_sad_per_64x64_ + avg_sad_per_64x64) / 4;
return stats;
}
} // namespace webrtc