Skip to content

Commit

Permalink
Entropy coding option (alvr-org#1318)
Browse files Browse the repository at this point in the history
* Add entropy coding option

* Use CAVLC as default option

* Update description for entropy options

* Add entropy coding option to Linux

* Fix indentation

* Add missing break statements
  • Loading branch information
deiteris authored and korejan committed Sep 29, 2024
1 parent bbfac23 commit 9d53d77
Show file tree
Hide file tree
Showing 12 changed files with 90 additions and 16 deletions.
5 changes: 5 additions & 0 deletions alvr/dashboard/js/app/nls/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,11 @@ define({
"Specifies the bitrate control method (Windows with AMD and nVidia cards only). CBR - constant bitrate, VBR - variable bitrate.",
"_root_video_rateControlMode_cbr-choice-.name": "CBR",
"_root_video_rateControlMode_vbr-choice-.name": "VBR",
"_root_video_entropyCoding-choice-.name": "Entropy coding method",
"_root_video_entropyCoding-choice-.description":
"Specifies the entropy coding method (h264 only). CABAC - provides better quality at lower bitrate, but may increase the encoder/decoder latency, CAVLC - provides worse quality that can be compensated with higher bitrate, and may significantly decrease encoder/decoder latency.",
"_root_video_entropyCoding_cabac-choice-.name": "CABAC",
"_root_video_entropyCoding_cavlc-choice-.name": "CAVLC",
"_root_video_advancedCodecOptions_amfControls_usePreproc.name":
"Enable Pre-Processor component",
"_root_video_advancedCodecOptions_amfControls_usePreproc.description":
Expand Down
5 changes: 5 additions & 0 deletions alvr/server/cpp/ALVR-common/packet_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ enum ALVR_RATE_CONTROL_METHOD {
ALVR_VBR = 1,
};

enum ALVR_ENTROPY_CODING {
ALVR_CABAC = 0,
ALVR_CAVLC = 1,
};

enum ALVR_INPUT {
ALVR_INPUT_SYSTEM_CLICK,
ALVR_INPUT_APPLICATION_MENU_CLICK,
Expand Down
1 change: 1 addition & 0 deletions alvr/server/cpp/alvr_server/Settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ void Settings::Load()

m_codec = (int32_t)config.get("codec").get<int64_t>();
m_rateControlMode = (uint32_t)config.get("rate_control_mode").get<int64_t>();
m_entropyCoding = (uint32_t)config.get("entropy_coding").get<int64_t>();
m_refreshRate = (int)config.get("refresh_rate").get<int64_t>();
mEncodeBitrateMBs = (int)config.get("encode_bitrate_mbs").get<int64_t>();
m_enableAdaptiveBitrate = config.get("enable_adaptive_bitrate").get<bool>();
Expand Down
1 change: 1 addition & 0 deletions alvr/server/cpp/alvr_server/Settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ class Settings
uint32_t m_preProcTor;
uint32_t m_encoderQualityPreset;
uint32_t m_rateControlMode;
uint32_t m_entropyCoding;
bool m_force_sw_encoding;
uint32_t m_swThreadCount;

Expand Down
10 changes: 10 additions & 0 deletions alvr/server/cpp/platform/linux/EncodePipelineNvEnc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,16 @@ alvr::EncodePipelineNvEnc::EncodePipelineNvEnc(std::vector<VkFrame> &input_frame
case ALVR_CODEC_H264:
AVUTIL.av_opt_set(encoder_ctx, "preset", "llhq", 0);
AVUTIL.av_opt_set(encoder_ctx, "zerolatency", "1", 0);

switch (settings.m_entropyCoding) {
case ALVR_CABAC:
AVUTIL.av_opt_set(encoder_ctx->priv_data, "coder", "ac", 0);
break;
case ALVR_CAVLC:
AVUTIL.av_opt_set(encoder_ctx->priv_data, "coder", "vlc", 0);
break;
}

break;
case ALVR_CODEC_H265:
AVUTIL.av_opt_set(encoder_ctx, "preset", "llhq", 0);
Expand Down
11 changes: 10 additions & 1 deletion alvr/server/cpp/platform/linux/EncodePipelineVAAPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,16 @@ alvr::EncodePipelineVAAPI::EncodePipelineVAAPI(std::vector<VkFrame>& input_frame
{
case ALVR_CODEC_H264:
encoder_ctx->profile = FF_PROFILE_H264_MAIN;
AVUTIL.av_opt_set(encoder_ctx, "rc_mode", "2", 0); //CBR

switch (settings.m_entropyCoding) {
case ALVR_CABAC:
AVUTIL.av_opt_set(encoder_ctx->priv_data, "coder", "ac", 0);
break;
case ALVR_CAVLC:
AVUTIL.av_opt_set(encoder_ctx->priv_data, "coder", "vlc", 0);
break;
}

break;
case ALVR_CODEC_H265:
encoder_ctx->profile = FF_PROFILE_HEVC_MAIN;
Expand Down
9 changes: 9 additions & 0 deletions alvr/server/cpp/platform/win32/VideoEncoderNVENC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,15 @@ void VideoEncoderNVENC::FillEncodeConfig(NV_ENC_INITIALIZE_PARAMS &initializePar
config.intraRefreshCnt = Settings::Instance().m_nvencIntraRefreshCount;
}

switch (Settings::Instance().m_entropyCoding) {
case ALVR_CABAC:
config.entropyCodingMode = NV_ENC_H264_ENTROPY_CODING_MODE_CABAC;
break;
case ALVR_CAVLC:
config.entropyCodingMode = NV_ENC_H264_ENTROPY_CODING_MODE_CAVLC;
break;
}

config.maxNumRefFrames = maxNumRefFrames;
config.idrPeriod = gopLength;
}
Expand Down
10 changes: 9 additions & 1 deletion alvr/server/cpp/platform/win32/VideoEncoderSW.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,15 @@ void VideoEncoderSW::Initialize() {
av_dict_set(&opt, "tune", "zerolatency", 0);
switch (m_codec) {
case ALVR_CODEC_H264:
m_codecContext->profile = Settings::Instance().m_use10bitEncoder ? FF_PROFILE_H264_HIGH_10 : FF_PROFILE_H264_HIGH;
m_codecContext->profile = Settings::Instance().m_use10bitEncoder ? FF_PROFILE_H264_HIGH_10_INTRA : (FF_PROFILE_H264_HIGH | FF_PROFILE_H264_INTRA);
switch (Settings::Instance().m_entropyCoding) {
case ALVR_CABAC:
av_dict_set(&opt, "coder", "ac", 0);
break;
case ALVR_CAVLC:
av_dict_set(&opt, "coder", "vlc", 0);
break;
}
break;
case ALVR_CODEC_H265:
m_codecContext->profile = Settings::Instance().m_use10bitEncoder ? FF_PROFILE_HEVC_MAIN_10 : FF_PROFILE_HEVC_MAIN;
Expand Down
38 changes: 24 additions & 14 deletions alvr/server/cpp/platform/win32/VideoEncoderVCE.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,16 @@ amf::AMFComponentPtr VideoEncoderVCE::MakeEncoder(
amfEncoder->SetProperty(AMF_VIDEO_ENCODER_RATE_CONTROL_METHOD, AMF_VIDEO_ENCODER_RATE_CONTROL_METHOD_LATENCY_CONSTRAINED_VBR);
break;
}

switch (Settings::Instance().m_entropyCoding) {
case ALVR_CABAC:
amfEncoder->SetProperty(AMF_VIDEO_ENCODER_CABAC_ENABLE, AMF_VIDEO_ENCODER_CABAC);
break;
case ALVR_CAVLC:
amfEncoder->SetProperty(AMF_VIDEO_ENCODER_CABAC_ENABLE, AMF_VIDEO_ENCODER_CALV);
break;
}

amfEncoder->SetProperty(AMF_VIDEO_ENCODER_TARGET_BITRATE, bitRateIn);
amfEncoder->SetProperty(AMF_VIDEO_ENCODER_PEAK_BITRATE, bitRateIn);
amfEncoder->SetProperty(AMF_VIDEO_ENCODER_FRAMESIZE, ::AMFConstructSize(width, height));
Expand Down Expand Up @@ -198,13 +208,13 @@ amf::AMFComponentPtr VideoEncoderVCE::MakeEncoder(

amfEncoder->SetProperty(AMF_VIDEO_ENCODER_MAX_NUM_REFRAMES, 0);

amf::AMFCapsPtr caps;
if (amfEncoder->GetCaps(&caps) == AMF_OK) {
caps->GetProperty(AMF_VIDEO_ENCODER_CAPS_QUERY_TIMEOUT_SUPPORT, &m_hasQueryTimeout);
}
if (m_hasQueryTimeout) {
amfEncoder->SetProperty(AMF_VIDEO_ENCODER_QUERY_TIMEOUT, 1000); // 1s timeout
}
amf::AMFCapsPtr caps;
if (amfEncoder->GetCaps(&caps) == AMF_OK) {
caps->GetProperty(AMF_VIDEO_ENCODER_CAPS_QUERY_TIMEOUT_SUPPORT, &m_hasQueryTimeout);
}
if (m_hasQueryTimeout) {
amfEncoder->SetProperty(AMF_VIDEO_ENCODER_QUERY_TIMEOUT, 1000); // 1s timeout
}
}
else
{
Expand Down Expand Up @@ -261,13 +271,13 @@ amf::AMFComponentPtr VideoEncoderVCE::MakeEncoder(

amfEncoder->SetProperty(AMF_VIDEO_ENCODER_HEVC_MAX_NUM_REFRAMES, 0);

amf::AMFCapsPtr caps;
if (amfEncoder->GetCaps(&caps) == AMF_OK) {
caps->GetProperty(AMF_VIDEO_ENCODER_CAPS_HEVC_QUERY_TIMEOUT_SUPPORT, &m_hasQueryTimeout);
}
if (m_hasQueryTimeout) {
amfEncoder->SetProperty(AMF_VIDEO_ENCODER_HEVC_QUERY_TIMEOUT, 1000); // 1s timeout
}
amf::AMFCapsPtr caps;
if (amfEncoder->GetCaps(&caps) == AMF_OK) {
caps->GetProperty(AMF_VIDEO_ENCODER_CAPS_HEVC_QUERY_TIMEOUT_SUPPORT, &m_hasQueryTimeout);
}
if (m_hasQueryTimeout) {
amfEncoder->SetProperty(AMF_VIDEO_ENCODER_HEVC_QUERY_TIMEOUT, 1000); // 1s timeout
}
}

Debug("Configured %s.\n", pCodec);
Expand Down
1 change: 1 addition & 0 deletions alvr/server/src/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,7 @@ async fn client_handshake(
adapter_index: settings.video.adapter_index,
codec: matches!(settings.video.codec, CodecType::HEVC) as _,
rate_control_mode: settings.video.rate_control_mode as u32,
entropy_coding: settings.video.entropy_coding as u32,
refresh_rate: fps as _,
use_10bit_encoder: settings.video.use_10bit_encoder,
use_preproc: amf_controls.use_preproc,
Expand Down
1 change: 1 addition & 0 deletions alvr/session/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ pub struct OpenvrConfig {
pub preproc_tor: u32,
pub encoder_quality_preset: u32,
pub rate_control_mode: u32,
pub entropy_coding: u32,
pub force_sw_encoding: bool,
pub sw_thread_count: u32,
pub encode_bitrate_mbs: u64,
Expand Down
14 changes: 14 additions & 0 deletions alvr/session/src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,14 @@ pub enum RateControlMode {
VBR = 1,
}

#[repr(u8)]
#[derive(SettingsSchema, Serialize, Deserialize, Clone)]
#[serde(tag = "type", content = "content")]
pub enum EntropyCoding {
CABAC = 0,
CAVLC = 1,
}

/// Except for preset, the value of these fields is not applied if == -1 (flag)
#[derive(SettingsSchema, Serialize, Deserialize, Clone)]
#[serde(rename_all = "camelCase")]
Expand Down Expand Up @@ -226,6 +234,9 @@ pub struct VideoDesc {
#[schema(advanced)]
pub rate_control_mode: RateControlMode,

#[schema(advanced)]
pub entropy_coding: EntropyCoding,

// #[schema(advanced)]
// pub video_coding: VideoCoding,
#[schema(advanced)]
Expand Down Expand Up @@ -629,6 +640,9 @@ pub fn session_settings_default() -> SettingsDefault {
variant: RateControlModeDefaultVariant::CBR,
},
client_request_realtime_decoder: true,
entropy_coding: EntropyCodingDefault {
variant: EntropyCodingDefaultVariant::CAVLC,
},
use_10bit_encoder: false,
sw_thread_count: 0,
encode_bitrate_mbs: 30,
Expand Down

0 comments on commit 9d53d77

Please sign in to comment.