Replace cricket::LeastCommonMultiple and cricket::GreatestCommonDivisor with std::lcm and std::gcd.

The std::lcm and std::gcd functions are part of the C++ standard
library. The existing functions are marked as deprecated rather than
deleted in the case of possible third party uses.

#rtc_cleanup

Bug: webrtc:377205743
Change-Id: I174e663f152d750c984a35dc7136bc18dc01bc8e
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/367440
Reviewed-by: Tomas Gunnarsson <tommi@webrtc.org>
Auto-Submit: Evan Shrubsole <eshr@google.com>
Reviewed-by: Rasmus Brandt <brandtr@webrtc.org>
Commit-Queue: Tomas Gunnarsson <tommi@webrtc.org>
Cr-Commit-Position: refs/heads/main@{#43368}
This commit is contained in:
Evan Shrubsole 2024-11-06 15:14:40 +00:00 committed by WebRTC LUCI CQ
parent e6f0c2fd23
commit 7589689774
11 changed files with 60 additions and 50 deletions

View File

@ -14,6 +14,7 @@
#include <cstdio>
#include <memory>
#include <numeric>
#include <optional>
#include <string>
#include <utility>
@ -30,7 +31,6 @@
#include "api/video/video_frame_type.h"
#include "api/video_codecs/video_codec.h"
#include "api/video_codecs/video_encoder.h"
#include "media/base/video_common.h"
#include "modules/video_coding/include/video_error_codes.h"
#include "modules/video_coding/include/video_error_codes_utils.h"
#include "modules/video_coding/utility/simulcast_utility.h"
@ -463,8 +463,8 @@ VideoEncoder::EncoderInfo VideoEncoderSoftwareFallbackWrapper::GetEncoderInfo()
EncoderInfo info =
IsFallbackActive() ? fallback_encoder_info : default_encoder_info;
info.requested_resolution_alignment = cricket::LeastCommonMultiple(
fallback_encoder_info.requested_resolution_alignment,
info.requested_resolution_alignment =
std::lcm(fallback_encoder_info.requested_resolution_alignment,
default_encoder_info.requested_resolution_alignment);
info.apply_alignment_to_all_simulcast_layers =
fallback_encoder_info.apply_alignment_to_all_simulcast_layers ||

View File

@ -125,6 +125,7 @@ rtc_library("rtc_media_base") {
"../rtc_base/system:rtc_export",
"../rtc_base/third_party/sigslot",
"../video/config:encoder_config",
"//third_party/abseil-cpp/absl/base:core_headers",
]
}
@ -198,6 +199,7 @@ rtc_library("video_broadcaster") {
"../api:media_stream_interface",
"../api:scoped_refptr",
"../api:sequence_checker",
"../api:video_track_source_constraints",
"../api/video:video_frame",
"../api/video:video_rtp_headers",
"../rtc_base:checks",
@ -219,6 +221,7 @@ rtc_library("video_common") {
"../rtc_base:stringutils",
"../rtc_base:timeutils",
"../rtc_base/system:rtc_export",
"//third_party/abseil-cpp/absl/base:core_headers",
]
}

View File

@ -15,13 +15,18 @@
#include <cstdint>
#include <cstdlib>
#include <limits>
#include <numeric>
#include <optional>
#include <string>
#include <utility>
#include "api/video/resolution.h"
#include "api/video/video_source_interface.h"
#include "media/base/video_common.h"
#include "rtc_base/checks.h"
#include "rtc_base/logging.h"
#include "rtc_base/strings/string_builder.h"
#include "rtc_base/synchronization/mutex.h"
#include "rtc_base/time_utils.h"
namespace {
@ -31,7 +36,7 @@ struct Fraction {
int denominator;
void DivideByGcd() {
int g = cricket::GreatestCommonDivisor(numerator, denominator);
int g = std::gcd(numerator, denominator);
numerator /= g;
denominator /= g;
}
@ -369,8 +374,8 @@ void VideoAdapter::OnSinkWants(const rtc::VideoSinkWants& sink_wants) {
sink_wants.target_pixel_count.value_or(
resolution_request_max_pixel_count_);
max_framerate_request_ = sink_wants.max_framerate_fps;
resolution_alignment_ = cricket::LeastCommonMultiple(
source_resolution_alignment_, sink_wants.resolution_alignment);
resolution_alignment_ =
std::lcm(source_resolution_alignment_, sink_wants.resolution_alignment);
// Convert from std::optional<rtc::VideoSinkWants::FrameSize> to
// std::optional<webrtc::Resolution>. Both are {int,int}.
scale_resolution_down_to_ = std::nullopt;

View File

@ -11,14 +11,22 @@
#include "media/base/video_broadcaster.h"
#include <algorithm>
#include <numeric>
#include <optional>
#include <vector>
#include "api/scoped_refptr.h"
#include "api/video/i420_buffer.h"
#include "api/video/video_frame.h"
#include "api/video/video_frame_buffer.h"
#include "api/video/video_rotation.h"
#include "media/base/video_common.h"
#include "api/video/video_sink_interface.h"
#include "api/video/video_source_interface.h"
#include "api/video_track_source_constraints.h"
#include "media/base/video_source_base.h"
#include "rtc_base/checks.h"
#include "rtc_base/logging.h"
#include "rtc_base/synchronization/mutex.h"
namespace rtc {
@ -168,8 +176,8 @@ void VideoBroadcaster::UpdateWants() {
if (sink.wants.max_framerate_fps < wants.max_framerate_fps) {
wants.max_framerate_fps = sink.wants.max_framerate_fps;
}
wants.resolution_alignment = cricket::LeastCommonMultiple(
wants.resolution_alignment, sink.wants.resolution_alignment);
wants.resolution_alignment =
std::lcm(wants.resolution_alignment, sink.wants.resolution_alignment);
// Pick MAX(requested_resolution) since the actual can be downscaled in
// encoder instead.

View File

@ -10,6 +10,10 @@
#include "media/base/video_common.h"
#include <cstdint>
#include <numeric>
#include <string>
#include "api/array_view.h"
#include "rtc_base/arraysize.h"
#include "rtc_base/checks.h"
@ -79,19 +83,13 @@ std::string VideoFormat::ToString() const {
int GreatestCommonDivisor(int a, int b) {
RTC_DCHECK_GE(a, 0);
RTC_DCHECK_GT(b, 0);
int c = a % b;
while (c != 0) {
a = b;
b = c;
c = a % b;
}
return b;
return std::gcd(a, b);
}
int LeastCommonMultiple(int a, int b) {
RTC_DCHECK_GT(a, 0);
RTC_DCHECK_GT(b, 0);
return a * (b / GreatestCommonDivisor(a, b));
return std::lcm(a, b);
}
} // namespace cricket

View File

@ -17,6 +17,7 @@
#include <string>
#include "absl/base/macros.h"
#include "rtc_base/system/rtc_export.h"
#include "rtc_base/time_utils.h"
@ -214,10 +215,10 @@ struct RTC_EXPORT VideoFormat : VideoFormatPod {
};
// Returns the largest positive integer that divides both `a` and `b`.
int GreatestCommonDivisor(int a, int b);
ABSL_DEPRECATE_AND_INLINE() int GreatestCommonDivisor(int a, int b);
// Returns the smallest positive integer that is divisible by both `a` and `b`.
int LeastCommonMultiple(int a, int b);
ABSL_DEPRECATE_AND_INLINE() int LeastCommonMultiple(int a, int b);
} // namespace cricket

View File

@ -92,17 +92,4 @@ TEST(VideoCommonTest, TestVideoFormatCompare) {
EXPECT_TRUE(format.IsPixelRateLess(format2));
}
TEST(VideoCommonTest, GreatestCommonDivisor) {
EXPECT_EQ(GreatestCommonDivisor(0, 1000), 1000);
EXPECT_EQ(GreatestCommonDivisor(1, 1), 1);
EXPECT_EQ(GreatestCommonDivisor(8, 12), 4);
EXPECT_EQ(GreatestCommonDivisor(24, 54), 6);
}
TEST(VideoCommonTest, LeastCommonMultiple) {
EXPECT_EQ(LeastCommonMultiple(1, 1), 1);
EXPECT_EQ(LeastCommonMultiple(2, 3), 6);
EXPECT_EQ(LeastCommonMultiple(16, 32), 32);
}
} // namespace cricket

View File

@ -17,6 +17,7 @@
#include <cstdint>
#include <iterator>
#include <memory>
#include <numeric>
#include <optional>
#include <string>
#include <tuple>
@ -51,7 +52,6 @@
#include "api/video_codecs/video_encoder_software_fallback_wrapper.h"
#include "common_video/framerate_controller.h"
#include "media/base/sdp_video_format_utils.h"
#include "media/base/video_common.h"
#include "modules/video_coding/include/video_error_codes.h"
#include "modules/video_coding/include/video_error_codes_utils.h"
#include "modules/video_coding/utility/simulcast_rate_allocator.h"
@ -884,8 +884,8 @@ webrtc::VideoCodec SimulcastEncoderAdapter::MakeStreamCodec(
void SimulcastEncoderAdapter::OverrideFromFieldTrial(
VideoEncoder::EncoderInfo* info) const {
if (encoder_info_override_.requested_resolution_alignment()) {
info->requested_resolution_alignment = cricket::LeastCommonMultiple(
info->requested_resolution_alignment,
info->requested_resolution_alignment =
std::lcm(info->requested_resolution_alignment,
*encoder_info_override_.requested_resolution_alignment());
info->apply_alignment_to_all_simulcast_layers =
info->apply_alignment_to_all_simulcast_layers ||
@ -931,8 +931,8 @@ VideoEncoder::EncoderInfo SimulcastEncoderAdapter::GetEncoderInfo() const {
const VideoEncoder::EncoderInfo& fallback_info =
encoder_context->FallbackInfo();
encoder_info.requested_resolution_alignment = cricket::LeastCommonMultiple(
primary_info.requested_resolution_alignment,
encoder_info.requested_resolution_alignment =
std::lcm(primary_info.requested_resolution_alignment,
fallback_info.requested_resolution_alignment);
encoder_info.apply_alignment_to_all_simulcast_layers =
@ -991,8 +991,8 @@ VideoEncoder::EncoderInfo SimulcastEncoderAdapter::GetEncoderInfo() const {
encoder_impl_info.is_qp_trusted.value_or(true);
}
encoder_info.fps_allocation[i] = encoder_impl_info.fps_allocation[0];
encoder_info.requested_resolution_alignment = cricket::LeastCommonMultiple(
encoder_info.requested_resolution_alignment,
encoder_info.requested_resolution_alignment =
std::lcm(encoder_info.requested_resolution_alignment,
encoder_impl_info.requested_resolution_alignment);
// request alignment on all layers if any of the encoders may need it, or
// if any non-top layer encoder requests a non-trivial alignment.

View File

@ -637,6 +637,8 @@ rtc_library("webrtc_vp9_helpers") {
"../../api/video:video_bitrate_allocation",
"../../api/video:video_bitrate_allocator",
"../../api/video:video_codec_constants",
"../../api/video:video_frame",
"../../api/video_codecs:scalability_mode",
"../../api/video_codecs:video_codecs_api",
"../../common_video",
"../../media:video_common",

View File

@ -12,13 +12,19 @@
#include <algorithm>
#include <cmath>
#include <memory>
#include <cstddef>
#include <numeric>
#include <optional>
#include <vector>
#include "media/base/video_common.h"
#include "api/video/video_codec_type.h"
#include "api/video_codecs/scalability_mode.h"
#include "api/video_codecs/spatial_layer.h"
#include "api/video_codecs/video_codec.h"
#include "modules/video_coding/codecs/vp9/include/vp9_globals.h"
#include "modules/video_coding/svc/create_scalability_structure.h"
#include "modules/video_coding/svc/scalability_mode_util.h"
#include "modules/video_coding/svc/scalable_video_controller.h"
#include "rtc_base/checks.h"
#include "rtc_base/logging.h"
@ -105,8 +111,8 @@ std::vector<SpatialLayer> ConfigureSvcNormalVideo(
if (config) {
required_divisiblity = 1;
for (size_t sl_idx = 0; sl_idx < num_spatial_layers; ++sl_idx) {
required_divisiblity = cricket::LeastCommonMultiple(
required_divisiblity, config->scaling_factor_den[sl_idx]);
required_divisiblity =
std::lcm(required_divisiblity, config->scaling_factor_den[sl_idx]);
}
}
input_width = input_width - input_width % required_divisiblity;

View File

@ -13,6 +13,7 @@
#include <cstring>
#include <map>
#include <memory>
#include <numeric>
#include <optional>
#include <set>
#include <string>
@ -61,7 +62,6 @@
#include "call/fake_network_pipe.h"
#include "call/video_receive_stream.h"
#include "call/video_send_stream.h"
#include "media/base/video_common.h"
#include "media/engine/internal_encoder_factory.h"
#include "media/engine/simulcast_encoder_adapter.h"
#include "media/engine/webrtc_video_engine.h"
@ -3505,8 +3505,8 @@ void VideoSendStreamTest::TestVp9NonFlexMode(
GetScalabilityConfig();
int required_divisibility = 1;
for (int sl_idx = 0; sl_idx < config.num_spatial_layers; ++sl_idx) {
required_divisibility = cricket::LeastCommonMultiple(
required_divisibility, config.scaling_factor_den[sl_idx]);
required_divisibility =
std::lcm(required_divisibility, config.scaling_factor_den[sl_idx]);
}
return required_divisibility;
}