use crate::fourcc::{self, format_bpp};
use crate::modifier::BandwidthClass;
#[must_use]
const fn frame_bytes(fourcc_code: u32, width: u32, height: u32) -> u64 {
let pixels = (width as u64) * (height as u64);
let bpp = format_bpp(fourcc_code);
if bpp != 0 {
return pixels * (bpp as u64 / 8);
}
match fourcc_code {
fourcc::NV12 | fourcc::NV21 | fourcc::YUV420 | fourcc::YVU420 => pixels * 3 / 2,
fourcc::NV16 | fourcc::NV61 | fourcc::YUV422 | fourcc::YVU422 => pixels * 2,
fourcc::NV24
| fourcc::NV42
| fourcc::YUV444
| fourcc::YVU444
| fourcc::P010
| fourcc::P012
| fourcc::P016 => pixels * 3,
_ => pixels * 4,
}
}
pub const DEFAULT_COMPRESSED_RATIO: f32 = 0.6;
#[must_use]
pub fn scanout_cost_bytes(
width: u32,
height: u32,
fourcc_code: u32,
class: BandwidthClass,
compressed_ratio: f32,
) -> u64 {
let raw = frame_bytes(fourcc_code, width, height);
if class == BandwidthClass::Compression {
#[expect(
clippy::cast_precision_loss,
clippy::cast_possible_truncation,
clippy::cast_sign_loss,
reason = "parity with the C++ cost model, which scales through double; \
this is a scoring heuristic, not an exact byte count"
)]
{
(raw as f64 * f64::from(compressed_ratio)) as u64
}
} else {
raw
}
}