[fix] fix resolution adapter

This commit is contained in:
dijunkun
2025-03-10 11:24:53 +08:00
parent de0386f08a
commit 8b4ff8cd1f
6 changed files with 26 additions and 37 deletions

View File

@@ -681,8 +681,8 @@ int NvDecoder::HandlePictureDisplay(CUVIDPARSERDISPINFO *pDispInfo) {
if (result == CUDA_SUCCESS && if (result == CUDA_SUCCESS &&
(DecodeStatus.decodeStatus == cuvidDecodeStatus_Error || (DecodeStatus.decodeStatus == cuvidDecodeStatus_Error ||
DecodeStatus.decodeStatus == cuvidDecodeStatus_Error_Concealed)) { DecodeStatus.decodeStatus == cuvidDecodeStatus_Error_Concealed)) {
printf("Decode Error occurred for picture %d\n", // printf("Decode Error occurred for picture %d\n",
m_nPicNumInDecodeOrder[pDispInfo->picture_index]); // m_nPicNumInDecodeOrder[pDispInfo->picture_index]);
} }
uint8_t *pDecodedFrame = nullptr; uint8_t *pDecodedFrame = nullptr;

View File

@@ -6,31 +6,24 @@
int ResolutionAdapter::GetResolution(int target_bitrate, int current_width, int ResolutionAdapter::GetResolution(int target_bitrate, int current_width,
int current_height, int* target_width, int current_height, int* target_width,
int* target_height) { int* target_height) {
for (auto& resolution : GetBitrateLimits()) { if (target_bitrate < GetBitrateLimits().front().min_start_bitrate_bps) {
if (target_bitrate >= resolution.min_start_bitrate_bps && *target_width = GetBitrateLimits().front().width;
target_bitrate <= resolution.max_bitrate_bps) { *target_height = GetBitrateLimits().front().height;
if (current_width * current_height <= resolution.frame_size_pixels) {
*target_width = current_width;
*target_height = current_height;
LOG_INFO("1 source resolution {}x{}, target resolution {}x{}",
current_width, current_height, *target_width, *target_height);
return 0;
} else {
*target_width = current_width * 3 / 5;
*target_height = current_height * 3 / 5;
LOG_INFO("2 source resolution {}x{}, target resolution {}x{}",
current_width, current_height, *target_width, *target_height);
return 0; return 0;
} }
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;
return 0;
} }
} }
*target_width = -1; *target_width = -1;
*target_height = -1; *target_height = -1;
LOG_INFO("3 source resolution {}x{}, target resolution {}x{}", current_width,
current_height, *target_width, *target_height);
return -1; return -1;
} }
@@ -41,8 +34,6 @@ int ResolutionAdapter::ResolutionDowngrade(const XVideoFrame* video_frame,
return -1; return -1;
} }
target_width = video_frame->width * 3 / 5;
target_height = video_frame->height * 3 / 5;
new_frame->width = target_width; new_frame->width = target_width;
new_frame->height = target_height; new_frame->height = target_height;
new_frame->data = new char[target_width * target_height * 3 / 2]; new_frame->data = new char[target_width * target_height * 3 / 2];

View File

@@ -26,13 +26,12 @@ class ResolutionAdapter {
public: public:
std::vector<ResolutionBitrateLimits> GetBitrateLimits() { std::vector<ResolutionBitrateLimits> GetBitrateLimits() {
return {{0 * 0, 0, 0, 0}, return {{320, 180, 0, 30000, 300000},
{320 * 180, 0, 30000, 300000}, {480, 270, 300000, 30000, 500000},
{480 * 270, 300000, 30000, 500000}, {640, 360, 500000, 30000, 800000},
{640 * 360, 500000, 30000, 800000}, {960, 540, 800000, 30000, 1500000},
{960 * 540, 800000, 30000, 1500000}, {1280, 720, 1500000, 30000, 2500000},
{1280 * 720, 1500000, 30000, 2500000}, {1920, 1080, 2500000, 30000, 4000000}};
{1920 * 1080, 2500000, 30000, 4000000}};
} }
int SetTargetBitrate(int bitrate); int SetTargetBitrate(int bitrate);

View File

@@ -8,13 +8,15 @@
#define _RESOLUTION_BITRATE_LIMITS_H_ #define _RESOLUTION_BITRATE_LIMITS_H_
struct ResolutionBitrateLimits { struct ResolutionBitrateLimits {
ResolutionBitrateLimits(int frame_size_pixels, int min_start_bitrate_bps, ResolutionBitrateLimits(int width, int height, int min_start_bitrate_bps,
int min_bitrate_bps, int max_bitrate_bps) int min_bitrate_bps, int max_bitrate_bps)
: frame_size_pixels(frame_size_pixels), : width(width),
height(height),
min_start_bitrate_bps(min_start_bitrate_bps), min_start_bitrate_bps(min_start_bitrate_bps),
min_bitrate_bps(min_bitrate_bps), min_bitrate_bps(min_bitrate_bps),
max_bitrate_bps(max_bitrate_bps) {} max_bitrate_bps(max_bitrate_bps) {}
int frame_size_pixels = 0; int width = 0;
int height = 0;
int min_start_bitrate_bps = 0; int min_start_bitrate_bps = 0;
int min_bitrate_bps = 0; int min_bitrate_bps = 0;
int max_bitrate_bps = 0; int max_bitrate_bps = 0;

View File

@@ -263,7 +263,6 @@ int NvidiaVideoEncoder::ResetEncodeResolution(unsigned int width,
frame_width_ = width; frame_width_ = width;
frame_height_ = height; frame_height_ = height;
LOG_WARN("Reset resolution to [{}x{}]", frame_width_, frame_height_);
NV_ENC_RECONFIGURE_PARAMS reconfig_params = {NV_ENC_RECONFIGURE_PARAMS_VER}; NV_ENC_RECONFIGURE_PARAMS reconfig_params = {NV_ENC_RECONFIGURE_PARAMS_VER};
NV_ENC_INITIALIZE_PARAMS init_params = {NV_ENC_INITIALIZE_PARAMS_VER}; NV_ENC_INITIALIZE_PARAMS init_params = {NV_ENC_INITIALIZE_PARAMS_VER};

View File

@@ -13,8 +13,6 @@ IceTransportController::IceTransportController(
: last_report_block_time_( : last_report_block_time_(
webrtc::Timestamp::Millis(webrtc_clock_->TimeInMilliseconds())), webrtc::Timestamp::Millis(webrtc_clock_->TimeInMilliseconds())),
b_force_i_frame_(true), b_force_i_frame_(true),
target_width_(1280),
target_height_(720),
video_codec_inited_(false), video_codec_inited_(false),
audio_codec_inited_(false), audio_codec_inited_(false),
load_nvcodec_dll_success_(false), load_nvcodec_dll_success_(false),
@@ -472,13 +470,13 @@ void IceTransportController::PostUpdates(webrtc::NetworkControlUpdate update) {
target_height_ = target_height; target_height_ = target_height;
b_force_i_frame_ = true; b_force_i_frame_ = true;
LOG_WARN("Set target resolution [{}x{}]", target_width_.value(), LOG_INFO("Set target resolution [{}x{}]", target_width_.value(),
target_height_.value()); target_height_.value());
} }
} else { } else if (target_width_.has_value() && target_height_.has_value()) {
target_width_.reset(); target_width_.reset();
target_height_.reset(); target_height_.reset();
LOG_WARN("Use original resolution [{}x{}]", source_width_, LOG_INFO("Use original resolution [{}x{}]", source_width_,
source_height_); source_height_);
} }
video_encoder_->SetTargetBitrate(target_bitrate_); video_encoder_->SetTargetBitrate(target_bitrate_);