[feat] use original resolution ratio when upgrade/downgrade source frame

This commit is contained in:
dijunkun
2025-04-16 17:22:04 +08:00
parent df6f4321e8
commit 38c88e27e0
5 changed files with 33 additions and 3 deletions

View File

@@ -15,8 +15,22 @@ int ResolutionAdapter::GetResolution(int target_bitrate, int current_width,
for (auto& resolution : GetBitrateLimits()) {
if (target_bitrate >= resolution.min_start_bitrate_bps &&
target_bitrate < resolution.max_bitrate_bps) {
*target_width = resolution.width;
*target_height = resolution.height;
// Adjust the resolution to maintain the same aspect ratio as the current
// resolution
float aspect_ratio = static_cast<float>(current_width) / current_height;
if (static_cast<float>(resolution.width) / resolution.height !=
aspect_ratio) {
if (aspect_ratio > 1.0f) {
*target_width = resolution.width;
*target_height = static_cast<int>(resolution.width / aspect_ratio);
} else {
*target_height = resolution.height;
*target_width = static_cast<int>(resolution.height * aspect_ratio);
}
} else {
*target_width = resolution.width;
*target_height = resolution.height;
}
return 0;
}
}

View File

@@ -46,6 +46,10 @@ class AomAv1Encoder : public VideoEncoder {
int SetTargetBitrate(int bitrate);
int GetResolution(int* width, int* height) {
if (seq_ == 0) {
return -1;
}
*width = frame_width_;
*height = frame_height_;
return 0;

View File

@@ -23,6 +23,10 @@ class NvidiaVideoEncoder : public VideoEncoder {
int SetTargetBitrate(int bitrate);
int GetResolution(int* width, int* height) {
if (seq_ == 0) {
return -1;
}
*width = frame_width_;
*height = frame_height_;
return 0;

View File

@@ -33,6 +33,10 @@ class OpenH264Encoder : public VideoEncoder {
int SetTargetBitrate(int bitrate);
int GetResolution(int* width, int* height) {
if (seq_ == 0) {
return -1;
}
*width = frame_width_;
*height = frame_height_;
return 0;

View File

@@ -600,7 +600,11 @@ void IceTransportController::PostUpdates(webrtc::NetworkControlUpdate update) {
if (target_bitrate != target_bitrate_ && video_encoder_) {
target_bitrate_ = target_bitrate;
int width, height, target_width, target_height;
video_encoder_->GetResolution(&width, &height);
int valid = video_encoder_->GetResolution(&width, &height);
if (valid != 0) {
width = source_width_;
height = source_height_;
}
if (0 == resolution_adapter_->GetResolution(target_bitrate_, width,
height, &target_width,
&target_height)) {