use std::sync::OnceLock;
use crate::rdoq::RdoqPricingMode;
pub(super) const PROFILE: &str = "BPG_STILLSEARCH_PROFILE";
pub(super) const LUMA_CHEAP: &str = "BPG_STILLSEARCH_LUMA_CHEAP";
pub(super) const LAMBDA_SCALE: &str = "BPG_STILLSEARCH_LAMBDA_SCALE";
pub(super) const SSIM_RD: &str = "BPG_STILLSEARCH_SSIM_RD";
pub(super) const RDOQ_SEARCH: &str = "BPG_STILLSEARCH_RDOQ_SEARCH";
pub(super) const RDOQ_ONE_PASS: &str = "BPG_STILLSEARCH_RDOQ_ONE_PASS";
pub(super) const RDOQ_PRICING: &str = "BPG_RDOQ_PRICING";
pub(super) const RDOQ_TRELLIS_BEAM: &str = "BPG_RDOQ_TRELLIS_BEAM";
pub(super) const FINAL_RDOQ: &str = "BPG_STILLSEARCH_FINAL_RDOQ";
pub(super) const RDOQ_AUDIT: &str = "BPG_STILLSEARCH_RDOQ_AUDIT";
pub(super) const RDOQ_AUDIT_MOD: &str = "BPG_STILLSEARCH_RDOQ_AUDIT_MOD";
pub(super) const STILL_DUMP: &str = "BPG_STILL_DUMP";
pub(super) const TU_MIN_SPLIT_LOG2: &str = "BPG_STILLSEARCH_TU_MIN_SPLIT_LOG2";
pub(super) const TU_SPLIT_BIAS_PER_PX: &str = "BPG_STILLSEARCH_TU_SPLIT_BIAS_PER_PX";
pub(super) const TU_4X4_BIAS_PER_PX: &str = "BPG_STILLSEARCH_TU_4X4_BIAS_PER_PX";
pub(super) const TU_FORCE_SPLIT_LOG2: &str = "BPG_STILLSEARCH_TU_FORCE_SPLIT_LOG2";
pub(super) const TU_TARGET_4X4: &str = "BPG_STILLSEARCH_TU_TARGET_4X4";
pub(super) const TU_TARGET_VAR_MIN: &str = "BPG_STILLSEARCH_TU_TARGET_VAR_MIN";
pub(super) const TU_TARGET_COST_MIN: &str = "BPG_STILLSEARCH_TU_TARGET_COST_MIN";
pub(super) const TU_TARGET_MEAN_RANGE_MIN: &str = "BPG_STILLSEARCH_TU_TARGET_MEAN_RANGE_MIN";
pub(super) const CU_SPLIT_BIAS_PER_PX: &str = "BPG_STILLSEARCH_CU_SPLIT_BIAS_PER_PX";
pub(super) const CU_FORCE_SPLIT_LOG2: &str = "BPG_STILLSEARCH_CU_FORCE_SPLIT_LOG2";
pub(super) const CU_SPLIT_BOUND: &str = "BPG_STILLSEARCH_CU_SPLIT_BOUND";
pub(super) const TU_SPLIT_BOUND: &str = "BPG_STILLSEARCH_TU_SPLIT_BOUND";
pub(super) const NXN_BIAS_PER_PX: &str = "BPG_STILLSEARCH_NXN_BIAS_PER_PX";
pub(super) const NXN_FORCE: &str = "BPG_STILLSEARCH_NXN_FORCE";
pub(super) const SSIM_RD_WEIGHT: &str = "BPG_STILLSEARCH_SSIM_RD_WEIGHT";
pub(in crate::encoder) const PSY_RD: &str = "BPG_PSY_RD";
pub(in crate::encoder) const PSY_RDOQ: &str = "BPG_PSY_RDOQ";
pub(super) const LUMA_CHEAP_RESIDUAL_PRICE: &str = "BPG_STILLSEARCH_LUMA_CHEAP_RESIDUAL_PRICE";
pub(super) const CHEAP_BITS_STATIC: &str = "BPG_STILLSEARCH_CHEAP_BITS_STATIC";
pub(super) const NXN_SKIP_SATD: &str = "BPG_STILLSEARCH_NXN_SKIP_SATD";
pub(super) const CTX_EVOLVE: &str = "BPG_STILLSEARCH_CTX_EVOLVE";
pub(super) const CU_HEADER_BITS: &str = "BPG_STILLSEARCH_CU_HEADER_BITS";
pub(super) const CHROMA_MODE_SEARCH: &str = "BPG_STILLSEARCH_CHROMA_MODES";
pub(super) const CHROMA_ROUGH: &str = "BPG_STILLSEARCH_CHROMA_ROUGH";
pub(super) const CHROMA_ROUGH_K: &str = "BPG_STILLSEARCH_CHROMA_ROUGH_K";
pub(super) const CHROMA_SKIP_SATD: &str = "BPG_STILLSEARCH_CHROMA_SKIP_SATD";
pub(super) const CHROMA_REUSE_DM: &str = "BPG_STILLSEARCH_CHROMA_REUSE_DM";
pub(super) const ROUGH_AUDIT: &str = "BPG_STILLSEARCH_ROUGH_AUDIT";
pub(super) const SCALAR_ANGULAR_ROUGH: &str = "BPG_STILLSEARCH_SCALAR_ANGULAR_ROUGH";
pub(super) const ROUGH_MODE_BIT_WEIGHT: &str = "BPG_STILLSEARCH_ROUGH_MODE_BIT_WEIGHT";
pub(super) const X265_RMD_WINDOW: &str = "BPG_STILLSEARCH_X265_RMD_WINDOW";
pub(super) const X265_RMD_CAP: &str = "BPG_STILLSEARCH_X265_RMD_CAP";
pub(super) const X265_RMD_AUDIT: &str = "BPG_STILLSEARCH_X265_RMD_AUDIT";
pub(super) const X265_RMD_INTEGER_COST: &str = "BPG_STILLSEARCH_X265_RMD_INTEGER_COST";
pub(super) const X265SHAPE_EXACT_TOP: &str = "BPG_STILLSEARCH_X265SHAPE_EXACT_TOP";
pub(super) const X265SHAPE_EXACT_MARGIN: &str = "BPG_STILLSEARCH_X265SHAPE_EXACT_MARGIN";
pub(super) const X265SHAPE_EXACT_VAR_MIN: &str = "BPG_STILLSEARCH_X265SHAPE_EXACT_VAR_MIN";
pub(super) const LUMA_MISS_AUDIT: &str = "BPG_STILLSEARCH_LUMA_MISS_AUDIT";
pub(super) const LUMA_ORACLE: &str = "BPG_STILLSEARCH_LUMA_ORACLE";
pub(super) const LUMA_ORACLE_MOD: &str = "BPG_STILLSEARCH_LUMA_ORACLE_MOD";
#[inline]
pub(super) fn profile_enabled() -> bool {
static ENABLED: OnceLock<bool> = OnceLock::new();
*ENABLED.get_or_init(|| std::env::var_os(PROFILE).is_some())
}
#[derive(Clone, Copy, PartialEq, Eq)]
struct CtxEvolve {
search: bool,
finalize: bool,
}
#[inline]
fn ctx_evolve() -> CtxEvolve {
static VALUE: OnceLock<CtxEvolve> = OnceLock::new();
*VALUE.get_or_init(|| {
let raw = std::env::var(CTX_EVOLVE).unwrap_or_default();
match raw.trim().to_ascii_lowercase().as_str() {
"0" | "off" | "none" => CtxEvolve {
search: false,
finalize: false,
},
"search" => CtxEvolve {
search: true,
finalize: false,
},
"finalize" => CtxEvolve {
search: false,
finalize: true,
},
_ => CtxEvolve {
search: true,
finalize: true,
},
}
})
}
#[inline]
pub(super) fn ctx_evolve_search() -> bool {
ctx_evolve().search
}
#[inline]
pub(super) fn ctx_evolve_finalize() -> bool {
ctx_evolve().finalize
}
#[inline]
pub(super) fn cu_header_bits_enabled() -> bool {
static ENABLED: OnceLock<bool> = OnceLock::new();
*ENABLED.get_or_init(|| {
std::env::var(CU_HEADER_BITS)
.map(|v| v.trim() != "0")
.unwrap_or(true)
})
}
#[inline]
pub(super) fn chroma_mode_search_enabled() -> bool {
static ENABLED: OnceLock<bool> = OnceLock::new();
*ENABLED.get_or_init(|| {
std::env::var(CHROMA_MODE_SEARCH)
.map(|v| v.trim() != "0")
.unwrap_or(true)
})
}
#[inline]
pub(super) fn chroma_rough_enabled() -> bool {
static ENABLED: OnceLock<bool> = OnceLock::new();
*ENABLED.get_or_init(|| {
std::env::var(CHROMA_ROUGH)
.map(|v| v.trim() != "0")
.unwrap_or(true)
})
}
#[inline]
pub(super) fn chroma_rough_k(template_k: u8) -> usize {
static VALUE: OnceLock<Option<usize>> = OnceLock::new();
let env_val = *VALUE.get_or_init(|| {
std::env::var(CHROMA_ROUGH_K)
.ok()
.and_then(|v| v.trim().parse::<usize>().ok())
.map(|v| v.clamp(1, 4))
});
env_val.unwrap_or(usize::from(template_k.clamp(1, 4)))
}
#[inline]
pub(super) fn chroma_skip_satd(template_threshold: f64) -> f64 {
static VALUE: OnceLock<Option<f64>> = OnceLock::new();
let env_val = *VALUE.get_or_init(|| {
std::env::var(CHROMA_SKIP_SATD)
.ok()
.and_then(|v| v.trim().parse::<f64>().ok())
.filter(|v| v.is_finite() && *v >= 0.0)
});
env_val.unwrap_or(template_threshold.max(0.0))
}
#[inline]
pub(super) fn chroma_reuse_dm_enabled() -> bool {
static ENABLED: OnceLock<bool> = OnceLock::new();
*ENABLED.get_or_init(|| {
std::env::var(CHROMA_REUSE_DM)
.map(|v| v.trim() != "0")
.unwrap_or(true)
})
}
#[inline]
pub(super) fn x265_rmd_audit_enabled() -> bool {
static ENABLED: OnceLock<bool> = OnceLock::new();
*ENABLED.get_or_init(|| std::env::var_os(X265_RMD_AUDIT).is_some())
}
#[inline]
pub(super) fn x265_rmd_integer_cost_enabled() -> bool {
static ENABLED: OnceLock<bool> = OnceLock::new();
*ENABLED.get_or_init(|| std::env::var_os(X265_RMD_INTEGER_COST).is_some())
}
#[inline]
pub(super) fn rdoq_audit_sample(x0: u32, y0: u32, log2_size: u8, c_idx: u8, mode: u8) -> bool {
static ENABLED: OnceLock<bool> = OnceLock::new();
if !*ENABLED.get_or_init(|| std::env::var_os(RDOQ_AUDIT).is_some()) {
return false;
}
static MOD: OnceLock<u64> = OnceLock::new();
let modulus = *MOD.get_or_init(|| {
std::env::var(RDOQ_AUDIT_MOD)
.ok()
.and_then(|v| v.trim().parse::<u64>().ok())
.filter(|&v| v > 0)
.unwrap_or(1024)
});
if modulus <= 1 {
return true;
}
let mut h = u64::from(x0 / 4).wrapping_mul(0x9E37_79B1);
h ^= u64::from(y0 / 4).wrapping_mul(0x85EB_CA77);
h ^= u64::from(log2_size).wrapping_mul(0xC2B2_AE3D);
h ^= u64::from(c_idx).wrapping_mul(0x27D4_EB2F);
h ^= u64::from(mode).wrapping_mul(0x1656_67B1);
h % modulus == 0
}
#[inline]
pub(super) fn x265shape_exact_top() -> usize {
static VALUE: OnceLock<usize> = OnceLock::new();
*VALUE.get_or_init(|| {
std::env::var(X265SHAPE_EXACT_TOP)
.ok()
.and_then(|v| v.trim().parse::<usize>().ok())
.filter(|&v| v >= 1)
.unwrap_or(1)
})
}
#[inline]
pub(super) fn x265shape_exact_margin() -> f64 {
static VALUE: OnceLock<f64> = OnceLock::new();
*VALUE.get_or_init(|| {
std::env::var(X265SHAPE_EXACT_MARGIN)
.ok()
.and_then(|v| v.trim().parse::<f64>().ok())
.filter(|v| v.is_finite() && *v >= 1.0)
.unwrap_or(f64::INFINITY)
})
}
#[inline]
pub(super) fn x265shape_exact_var_min() -> f64 {
static VALUE: OnceLock<f64> = OnceLock::new();
*VALUE.get_or_init(|| {
std::env::var(X265SHAPE_EXACT_VAR_MIN)
.ok()
.and_then(|v| v.trim().parse::<f64>().ok())
.filter(|v| v.is_finite() && *v >= 0.0)
.unwrap_or(0.0)
})
}
#[inline]
pub(super) fn luma_miss_audit_enabled() -> bool {
static ENABLED: OnceLock<bool> = OnceLock::new();
*ENABLED.get_or_init(|| std::env::var_os(LUMA_MISS_AUDIT).is_some())
}
#[inline]
pub(super) fn luma_cheap_enabled(template_enabled: bool) -> bool {
static ENV: OnceLock<Option<bool>> = OnceLock::new();
let env_val = *ENV.get_or_init(|| std::env::var(LUMA_CHEAP).ok().map(|v| v.trim() != "0"));
env_val.unwrap_or(template_enabled)
}
#[inline]
pub(super) fn lambda_scale() -> f64 {
static SCALE: OnceLock<f64> = OnceLock::new();
*SCALE.get_or_init(|| {
std::env::var(LAMBDA_SCALE)
.ok()
.and_then(|v| v.trim().parse::<f64>().ok())
.filter(|v| v.is_finite() && *v > 0.0)
.unwrap_or(1.0)
})
}
pub(super) fn dump_ctu_target() -> Option<(u32, u32)> {
static V: OnceLock<Option<(u32, u32)>> = OnceLock::new();
*V.get_or_init(|| {
let s = std::env::var("BPG_DUMP_CTU").ok()?;
let (a, b) = s.split_once(',')?;
Some((a.trim().parse().ok()?, b.trim().parse().ok()?))
})
}
pub(super) fn residual_bits_search_scale() -> f64 {
static SCALE: OnceLock<f64> = OnceLock::new();
*SCALE.get_or_init(|| {
std::env::var("BPG_RESIDUAL_BITS_SCALE")
.ok()
.and_then(|v| v.trim().parse::<f64>().ok())
.filter(|v| v.is_finite() && *v > 0.0)
.unwrap_or(1.0)
})
}
#[inline]
pub(super) fn dump_target() -> Option<(u32, u32, u8, u8)> {
static VALUE: OnceLock<Option<(u32, u32, u8, u8)>> = OnceLock::new();
*VALUE.get_or_init(|| {
let raw = std::env::var(STILL_DUMP).ok()?;
let mut it = raw.split(',').map(|s| s.trim());
let x = it.next()?.parse::<u32>().ok()?;
let y = it.next()?.parse::<u32>().ok()?;
let log2 = it.next()?.parse::<u8>().ok()?;
let c_idx = it.next().and_then(|s| s.parse::<u8>().ok()).unwrap_or(0);
Some((x, y, log2, c_idx))
})
}
#[inline]
pub(super) fn tu_min_split_log2_override() -> Option<u8> {
static VALUE: OnceLock<Option<u8>> = OnceLock::new();
*VALUE.get_or_init(|| {
std::env::var(TU_MIN_SPLIT_LOG2)
.ok()
.and_then(|v| v.trim().parse::<u8>().ok())
.filter(|&v| (2..=5).contains(&v))
})
}
#[inline]
pub(super) fn tu_split_bias_per_px() -> f64 {
static VALUE: OnceLock<f64> = OnceLock::new();
*VALUE.get_or_init(|| {
std::env::var(TU_SPLIT_BIAS_PER_PX)
.ok()
.and_then(|v| v.trim().parse::<f64>().ok())
.filter(|v| v.is_finite())
.unwrap_or(0.0)
})
}
#[inline]
pub(super) fn tu_4x4_bias_per_px() -> f64 {
static VALUE: OnceLock<f64> = OnceLock::new();
*VALUE.get_or_init(|| {
std::env::var(TU_4X4_BIAS_PER_PX)
.ok()
.and_then(|v| v.trim().parse::<f64>().ok())
.filter(|v| v.is_finite())
.unwrap_or(0.0)
})
}
#[inline]
pub(super) fn tu_force_split_log2() -> Option<u8> {
static VALUE: OnceLock<Option<u8>> = OnceLock::new();
*VALUE.get_or_init(|| {
std::env::var(TU_FORCE_SPLIT_LOG2)
.ok()
.and_then(|v| v.trim().parse::<u8>().ok())
.filter(|&v| (3..=6).contains(&v))
})
}
#[inline]
pub(super) fn cu_split_bias_per_px() -> f64 {
static VALUE: OnceLock<f64> = OnceLock::new();
*VALUE.get_or_init(|| {
std::env::var(CU_SPLIT_BIAS_PER_PX)
.ok()
.and_then(|v| v.trim().parse::<f64>().ok())
.filter(|v| v.is_finite())
.unwrap_or(0.0)
})
}
#[inline]
pub(super) fn cu_split_bound_enabled() -> bool {
static ENABLED: OnceLock<bool> = OnceLock::new();
*ENABLED.get_or_init(|| {
std::env::var(CU_SPLIT_BOUND)
.map(|v| v.trim() != "0")
.unwrap_or(true)
})
}
#[inline]
pub(super) fn descent_log_enabled() -> bool {
static ENABLED: OnceLock<bool> = OnceLock::new();
*ENABLED.get_or_init(|| std::env::var_os("BPG_STILLSEARCH_DESCENT_LOG").is_some())
}
#[inline]
pub(super) fn descent_gate_override() -> Option<(f64, f64)> {
static VALUE: OnceLock<Option<(f64, f64)>> = OnceLock::new();
*VALUE.get_or_init(|| {
let raw = std::env::var("BPG_STILLSEARCH_DESCENT_GATE").ok()?;
let v = raw.trim();
if matches!(v, "0" | "off" | "false" | "none" | "disable" | "disabled") {
return Some((0.0, 0.0));
}
let (a, b) = v.split_once(',')?;
let t16 = a.trim().parse::<f64>().ok().filter(|t| t.is_finite())?;
let t32 = b.trim().parse::<f64>().ok().filter(|t| t.is_finite())?;
Some((t16, t32))
})
}
#[inline]
pub(super) fn tu_split_bound_enabled() -> bool {
static ENABLED: OnceLock<bool> = OnceLock::new();
*ENABLED.get_or_init(|| {
std::env::var(TU_SPLIT_BOUND)
.map(|v| v.trim() != "0")
.unwrap_or(true)
})
}
#[inline]
pub(super) fn cu_force_split_log2(default: Option<u8>) -> Option<u8> {
static OVERRIDE: OnceLock<Option<Option<u8>>> = OnceLock::new();
match *OVERRIDE.get_or_init(|| {
let Ok(raw) = std::env::var(CU_FORCE_SPLIT_LOG2) else {
return None;
};
let v = raw.trim();
if matches!(v, "0" | "off" | "false" | "none" | "disable" | "disabled") {
return Some(None);
}
v.parse::<u8>()
.ok()
.filter(|&v| (4..=6).contains(&v))
.map(Some)
}) {
Some(v) => v,
None => default,
}
}
#[inline]
pub(super) fn nxn_bias_per_px() -> f64 {
static VALUE: OnceLock<f64> = OnceLock::new();
*VALUE.get_or_init(|| {
std::env::var(NXN_BIAS_PER_PX)
.ok()
.and_then(|v| v.trim().parse::<f64>().ok())
.filter(|v| v.is_finite())
.unwrap_or(0.0)
})
}
#[inline]
pub(super) fn nxn_force_enabled() -> bool {
static VALUE: OnceLock<bool> = OnceLock::new();
*VALUE.get_or_init(|| {
std::env::var(NXN_FORCE)
.ok()
.map(|v| {
let v = v.trim().to_ascii_lowercase();
!(v == "0" || v == "off" || v == "none" || v.is_empty())
})
.unwrap_or(false)
})
}
#[inline]
pub(super) fn tu_target_4x4_enabled() -> bool {
static VALUE: OnceLock<bool> = OnceLock::new();
*VALUE.get_or_init(|| {
std::env::var(TU_TARGET_4X4)
.ok()
.map(|v| {
let v = v.trim().to_ascii_lowercase();
!(v == "0" || v == "off" || v == "none" || v.is_empty())
})
.unwrap_or(false)
})
}
#[inline]
pub(super) fn tu_target_var_min() -> f64 {
static VALUE: OnceLock<f64> = OnceLock::new();
*VALUE.get_or_init(|| {
std::env::var(TU_TARGET_VAR_MIN)
.ok()
.and_then(|v| v.trim().parse::<f64>().ok())
.filter(|v| v.is_finite() && *v >= 0.0)
.unwrap_or(200.0)
})
}
#[inline]
pub(super) fn tu_target_cost_min() -> f64 {
static VALUE: OnceLock<f64> = OnceLock::new();
*VALUE.get_or_init(|| {
std::env::var(TU_TARGET_COST_MIN)
.ok()
.and_then(|v| v.trim().parse::<f64>().ok())
.filter(|v| v.is_finite() && *v >= 0.0)
.unwrap_or(50.0)
})
}
#[inline]
pub(super) fn tu_target_mean_range_min() -> f64 {
static VALUE: OnceLock<f64> = OnceLock::new();
*VALUE.get_or_init(|| {
std::env::var(TU_TARGET_MEAN_RANGE_MIN)
.ok()
.and_then(|v| v.trim().parse::<f64>().ok())
.filter(|v| v.is_finite() && *v >= 0.0)
.unwrap_or(16.0)
})
}
#[inline]
pub(super) fn final_rdoq_enabled() -> bool {
static ENABLED: OnceLock<bool> = OnceLock::new();
*ENABLED.get_or_init(|| {
std::env::var(FINAL_RDOQ)
.ok()
.map(|v| {
let v = v.trim().to_ascii_lowercase();
!(v == "0" || v == "off" || v == "none")
})
.unwrap_or(true)
})
}
#[inline]
pub(super) fn rdoq_one_pass_enabled() -> bool {
static ENABLED: OnceLock<bool> = OnceLock::new();
*ENABLED.get_or_init(|| {
std::env::var(RDOQ_ONE_PASS)
.ok()
.map(|v| {
let v = v.trim().to_ascii_lowercase();
!(v == "0" || v == "off" || v == "none")
})
.unwrap_or(true)
})
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(super) enum RootTuReuseMode {
Off,
On,
Verify,
}
pub(super) fn root_tu_reuse_mode() -> RootTuReuseMode {
static MODE: OnceLock<RootTuReuseMode> = OnceLock::new();
*MODE.get_or_init(|| {
match std::env::var("BPG_STILLSEARCH_ROOT_TU_REUSE")
.unwrap_or_default()
.trim()
.to_ascii_lowercase()
.as_str()
{
"0" | "off" | "none" | "disable" | "disabled" => RootTuReuseMode::Off,
"verify" | "check" => RootTuReuseMode::Verify,
_ => RootTuReuseMode::On,
}
})
}
#[inline]
pub(super) fn rdoq_search_override() -> Option<bool> {
static OVERRIDE: OnceLock<Option<bool>> = OnceLock::new();
*OVERRIDE.get_or_init(|| {
let v = std::env::var(RDOQ_SEARCH).ok()?;
let v = v.trim().to_ascii_lowercase();
if v.is_empty() || v == "effort" {
return None;
}
Some(!(v == "0" || v == "off" || v == "none"))
})
}
#[inline]
pub(super) fn rdoq_pricing_mode() -> RdoqPricingMode {
static MODE: OnceLock<RdoqPricingMode> = OnceLock::new();
*MODE.get_or_init(|| {
let raw = std::env::var(RDOQ_PRICING).ok();
match raw
.as_deref()
.map(str::trim)
.map(str::to_ascii_lowercase)
.as_deref()
{
Some("1" | "running" | "run") => RdoqPricingMode::Running,
Some("2" | "hybrid") => RdoqPricingMode::Hybrid,
Some("3" | "exact" | "exact-cleanup" | "cleanup") => RdoqPricingMode::ExactCleanup,
Some("4" | "trellis" | "bounded-trellis" | "bounded_trellis") => {
RdoqPricingMode::BoundedTrellis
}
_ => RdoqPricingMode::Frozen,
}
})
}
#[inline]
pub(super) fn rdoq_trellis_beam() -> usize {
static BEAM: OnceLock<usize> = OnceLock::new();
*BEAM.get_or_init(|| {
std::env::var(RDOQ_TRELLIS_BEAM)
.ok()
.and_then(|v| v.trim().parse::<usize>().ok())
.filter(|&v| v > 0)
.unwrap_or(4)
})
}
#[inline]
pub(in crate::encoder) fn psy_rd(config_value: f32) -> f32 {
static ENV: OnceLock<Option<f32>> = OnceLock::new();
let env_val = *ENV.get_or_init(|| {
std::env::var(PSY_RD)
.ok()
.and_then(|v| v.trim().parse::<f32>().ok())
.filter(|v| v.is_finite())
});
env_val.unwrap_or(config_value).clamp(0.0, 5.0)
}
#[inline]
pub(in crate::encoder) fn psy_rdoq(config_value: f32) -> f32 {
static ENV: OnceLock<Option<f32>> = OnceLock::new();
let env_val = *ENV.get_or_init(|| {
std::env::var(PSY_RDOQ)
.ok()
.and_then(|v| v.trim().parse::<f32>().ok())
.filter(|v| v.is_finite())
});
env_val.unwrap_or(config_value).clamp(0.0, 50.0)
}
#[inline]
pub(super) fn ssim_rd_enabled() -> bool {
static ENABLED: OnceLock<bool> = OnceLock::new();
*ENABLED.get_or_init(|| {
std::env::var(SSIM_RD)
.ok()
.map(|v| {
let v = v.trim().to_ascii_lowercase();
!(v == "0" || v == "off" || v == "none" || v.is_empty())
})
.unwrap_or(false)
})
}
#[inline]
pub(super) fn ssim_rd_weight() -> f64 {
static WEIGHT: OnceLock<f64> = OnceLock::new();
*WEIGHT.get_or_init(|| {
std::env::var(SSIM_RD_WEIGHT)
.ok()
.and_then(|v| v.trim().parse::<f64>().ok())
.filter(|v| v.is_finite() && *v >= 0.0)
.unwrap_or(1.0 / 16384.0)
})
}
#[inline]
pub(super) fn luma_cheap_residual_price_exact(template_exact: bool) -> bool {
static ENV: OnceLock<Option<bool>> = OnceLock::new();
let env_val = *ENV.get_or_init(|| {
std::env::var(LUMA_CHEAP_RESIDUAL_PRICE).ok().map(|v| {
let v = v.trim().to_ascii_lowercase();
!(v == "0" || v == "skip" || v == "none" || v == "off")
})
});
env_val.unwrap_or(template_exact)
}
pub(super) fn cheap_bits_static() -> bool {
static ENV: OnceLock<bool> = OnceLock::new();
*ENV.get_or_init(|| {
std::env::var(CHEAP_BITS_STATIC)
.ok()
.map(|v| {
let v = v.trim().to_ascii_lowercase();
!(v == "0" || v == "off" || v == "none" || v.is_empty())
})
.unwrap_or(false)
})
}
#[inline]
pub(super) fn nxn_skip_satd_threshold(template_val: f64) -> f64 {
static ENV: OnceLock<Option<f64>> = OnceLock::new();
let env_val = *ENV.get_or_init(|| {
std::env::var(NXN_SKIP_SATD)
.ok()
.and_then(|s| s.parse().ok())
});
env_val.unwrap_or(template_val)
}
#[inline]
pub(super) fn rough_audit_enabled() -> bool {
static ENABLED: OnceLock<bool> = OnceLock::new();
*ENABLED.get_or_init(|| std::env::var_os(ROUGH_AUDIT).is_some())
}
#[inline]
pub(super) fn scalar_angular_rough_enabled() -> bool {
static ENABLED: OnceLock<bool> = OnceLock::new();
*ENABLED.get_or_init(|| {
std::env::var(SCALAR_ANGULAR_ROUGH)
.ok()
.map(|v| v.trim() != "0")
.unwrap_or(false)
})
}
#[inline]
pub(super) fn rough_mode_bit_weight_override() -> Option<f64> {
static V: OnceLock<Option<f64>> = OnceLock::new();
*V.get_or_init(|| {
std::env::var(ROUGH_MODE_BIT_WEIGHT)
.ok()
.and_then(|s| s.parse().ok())
})
}
#[inline]
pub(super) fn luma_oracle_enabled() -> bool {
static ENABLED: OnceLock<bool> = OnceLock::new();
*ENABLED.get_or_init(|| std::env::var_os(LUMA_ORACLE).is_some())
}
#[inline]
pub(super) fn luma_oracle_mod() -> u64 {
static V: OnceLock<u64> = OnceLock::new();
*V.get_or_init(|| {
std::env::var(LUMA_ORACLE_MOD)
.ok()
.and_then(|s| s.parse().ok())
.unwrap_or(64)
.max(1)
})
}
#[inline]
pub(super) fn x265_rmd_window() -> f64 {
static V: OnceLock<f64> = OnceLock::new();
*V.get_or_init(|| parse_env_or(X265_RMD_WINDOW, 1.25_f64).max(1.0))
}
#[inline]
pub(super) fn x265_rmd_cap(default_cap: usize) -> usize {
static V: OnceLock<Option<usize>> = OnceLock::new();
V.get_or_init(|| {
std::env::var(X265_RMD_CAP)
.ok()
.and_then(|s| s.trim().parse::<usize>().ok())
.map(|n| n.clamp(1, 35))
})
.unwrap_or(default_cap)
}
fn parse_env_or<T>(key: &str, default: T) -> T
where
T: std::str::FromStr,
{
std::env::var(key)
.ok()
.and_then(|s| s.trim().parse().ok())
.unwrap_or(default)
}