//! Level selection shared by both encoder backends. //! //! Each function returns the smallest level whose picture size, sample rate, //! DPB and bitrate limits hold for the configuration. The Vulkan shim clamps //! the result to the driver's maximum. use crate::types::{EncoderConfig, H264Profile, RateControlMode}; /// `max_dec_pic_buffering` the encoders signal: one reference, no reordering. const DPB_FRAMES: u64 = 1; /// H.264 luma macroblock size; the coded picture is a whole number of them. const H264_BLOCK: u32 = 16; /// `(level_idc, MaxMBPS, MaxFS, MaxDpbMbs, MaxBR kbit/s)`, Table A-1. const H264_LEVELS: [(u8, u64, u64, u64, u64); 19] = [ (10, 1485, 99, 396, 64), (11, 3000, 396, 900, 192), (12, 6000, 396, 2376, 384), (13, 11880, 396, 2376, 768), (20, 11880, 396, 2376, 2000), (21, 19800, 792, 4752, 4000), (22, 20250, 1620, 8100, 4000), (30, 40500, 1620, 8100, 10000), (31, 108000, 3600, 18000, 14000), (32, 216000, 5120, 20480, 20000), (40, 245760, 8192, 32768, 20000), (41, 245760, 8192, 32768, 50000), (42, 522240, 8704, 34816, 50000), (50, 589824, 22080, 110400, 135000), (51, 983040, 36864, 184320, 240000), (52, 2073600, 36864, 184320, 240000), (60, 4177920, 139264, 696320, 240000), (61, 8355840, 139264, 696320, 480000), (62, 16711680, 139264, 696320, 800000), ]; /// `(general_level_idc, MaxLumaPs, MaxLumaSr, Main tier MaxBR kbit/s)`, Table A.8. const HEVC_LEVELS: [(u8, u64, u64, u64); 13] = [ (30, 36864, 552960, 128), (60, 122880, 3686400, 1500), (63, 245760, 7372800, 3000), (90, 552960, 16588800, 6000), (93, 983040, 33177600, 10000), (120, 2228224, 66846720, 12000), (123, 2228224, 133693440, 20000), (150, 8912896, 267386880, 25000), (153, 8912896, 534773760, 40000), (156, 8912896, 1069547520, 60000), (180, 35651584, 1069547520, 60000), (183, 35651584, 2139095040, 120000), (186, 35651584, 4278190080, 240000), ]; /// `(seq_level_idx, MaxPicSize, MaxHSize, MaxVSize, MaxDisplayRate, MainMbps kbit/s)`, Annex A. const AV1_LEVELS: [(u8, u64, u32, u32, u64, u64); 14] = [ (0, 147_456, 2_048, 1_152, 4_423_680, 1_500), (1, 278_784, 2_816, 1_584, 8_363_520, 3_000), (4, 665_856, 4_352, 2_448, 19_975_680, 6_000), (5, 1_065_024, 5_504, 3_096, 31_950_720, 10_000), (8, 2_359_296, 6_144, 3_456, 70_778_880, 12_000), (9, 2_359_296, 6_144, 3_456, 141_557_760, 20_000), (12, 8_912_896, 8_192, 4_352, 267_386_880, 30_000), (13, 8_912_896, 8_192, 4_352, 534_773_760, 40_000), (14, 8_912_896, 8_192, 4_352, 1_069_547_520, 60_000), (15, 8_912_896, 8_192, 4_352, 1_069_547_520, 60_000), (16, 35_651_584, 16_384, 8_704, 1_069_547_520, 60_000), (17, 35_651_584, 16_384, 8_704, 2_139_095_040, 100_000), (18, 35_651_584, 16_384, 8_704, 4_278_190_080, 160_000), (19, 35_651_584, 16_384, 8_704, 4_278_190_080, 160_000), ]; const H264_MAX_LEVEL: u8 = 62; const HEVC_MAX_LEVEL: u8 = 186; const AV1_MAX_LEVEL: u8 = 31; fn aligned(value: u32, block: u32) -> u64 { u64::from(value.next_multiple_of(block)) } /// Whether `samples` per picture at the frame rate stay within `limit` per second. fn rate_fits(samples: u64, framerate: (u32, u32), limit: u64) -> bool { let (num, den) = (u64::from(framerate.0.max(1)), u64::from(framerate.1.max(1))); samples * num <= limit * den } /// The bitrate the level must carry, or `None` when the quantiser is fixed. fn bitrate(config: &EncoderConfig) -> Option { match config.rate_control { RateControlMode::ConstantQp { .. } => None, _ => Some(u64::from(config.bitrate)), } } /// `level_idc` of the lowest H.264 level that fits `config`. pub(crate) fn h264(config: &EncoderConfig) -> u8 { let mbs = (aligned(config.width, H264_BLOCK) / 16) * (aligned(config.height, H264_BLOCK) / 16); let (w, h) = (aligned(config.width, H264_BLOCK) / 16, aligned(config.height, H264_BLOCK) / 16); let nal_factor = if config.h264_profile == H264Profile::High { 1500 } else { 1200 }; let bitrate = bitrate(config); H264_LEVELS .iter() .find(|&&(_, mbps, fs, dpb, br)| { rate_fits(mbs, config.framerate, mbps) && mbs <= fs && w * w <= 8 * fs && h * h <= 8 * fs && mbs * DPB_FRAMES <= dpb && bitrate.is_none_or(|b| b <= br * nal_factor) }) .map_or(H264_MAX_LEVEL, |level| level.0) } /// `general_level_idc` of the lowest Main-tier H.265 level that fits `config`. pub(crate) fn hevc(config: &EncoderConfig) -> u8 { let (w, h) = (u64::from(config.width), u64::from(config.height)); let samples = w * h; let bitrate = bitrate(config); HEVC_LEVELS .iter() .find(|&&(_, max_ps, max_sr, br)| { samples <= max_ps && w * w <= 8 * max_ps && h * h <= 8 * max_ps && rate_fits(samples, config.framerate, max_sr) && bitrate.is_none_or(|b| b * 10 <= br * 1000 * 11) }) .map_or(HEVC_MAX_LEVEL, |level| level.0) } /// `seq_level_idx` of the lowest AV1 level that fits `config`. pub(crate) fn av1(config: &EncoderConfig) -> u8 { let (width, height) = (config.width.max(1), config.height.max(1)); let samples = u64::from(width) * u64::from(height); let bitrate = bitrate(config); AV1_LEVELS .iter() .find(|&&(_, max_size, max_w, max_h, max_rate, main_kbps)| { samples <= max_size && width <= max_w && height <= max_h && rate_fits(samples, config.framerate, max_rate) && bitrate.is_none_or(|b| b <= main_kbps * 1000) }) .map_or(AV1_MAX_LEVEL, |level| level.0) }