diff --git a/crates/ec-hw/src/level.rs b/crates/ec-hw/src/level.rs index 48e92de9..fb3c03a0 100644 --- a/crates/ec-hw/src/level.rs +++ b/crates/ec-hw/src/level.rs @@ -12,9 +12,6 @@ const DPB_FRAMES: u64 = 1; /// H.264 luma macroblock size; the coded picture is a whole number of them. const H264_BLOCK: u32 = 16; -/// H.265 coded picture size is a whole number of 32-sample blocks here. -const H265_BLOCK: u32 = 32; - /// `(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), @@ -56,19 +53,21 @@ const HEVC_LEVELS: [(u8, u64, u64, u64); 13] = [ ]; /// `(seq_level_idx, MaxPicSize, MaxHSize, MaxVSize, MaxDisplayRate, MainMbps kbit/s)`, Annex A. -const AV1_LEVELS: [(u8, u64, u32, u32, u64, u64); 12] = [ +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, 5_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, 20_000), - (9, 2_359_296, 6_144, 3_456, 141_557_760, 30_000), - (12, 8_912_896, 8_192, 4_352, 267_386_880, 60_000), - (13, 8_912_896, 8_192, 4_352, 534_773_760, 100_000), - (14, 8_912_896, 8_192, 4_352, 1_069_547_520, 160_000), - (16, 35_651_584, 16_384, 8_704, 1_069_547_520, 240_000), - (17, 35_651_584, 16_384, 8_704, 2_139_095_040, 480_000), - (18, 35_651_584, 16_384, 8_704, 4_278_190_080, 800_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; @@ -96,10 +95,11 @@ fn bitrate(config: &EncoderConfig) -> Option { /// `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 scale = if config.h264_profile == H264Profile::High { - 5 + 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 { - 4 + 1200 }; let bitrate = bitrate(config); H264_LEVELS @@ -107,20 +107,25 @@ pub(crate) fn h264(config: &EncoderConfig) -> u8 { .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 * 4 <= br * 1000 * scale) + && 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 samples = aligned(config.width, H265_BLOCK) * aligned(config.height, H265_BLOCK); + 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) })