| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | use crate::types::{EncoderConfig, H264Profile, RateControlMode}; |
| 8 | |
| 9 | |
| 10 | const DPB_FRAMES: u64 = 1; |
| 11 | |
| 12 | |
| 13 | const H264_BLOCK: u32 = 16; |
| 14 | |
| 15 | |
| 16 | const H264_LEVELS: [(u8, u64, u64, u64, u64); 19] = [ |
| 17 | (10, 1485, 99, 396, 64), |
| 18 | (11, 3000, 396, 900, 192), |
| 19 | (12, 6000, 396, 2376, 384), |
| 20 | (13, 11880, 396, 2376, 768), |
| 21 | (20, 11880, 396, 2376, 2000), |
| 22 | (21, 19800, 792, 4752, 4000), |
| 23 | (22, 20250, 1620, 8100, 4000), |
| 24 | (30, 40500, 1620, 8100, 10000), |
| 25 | (31, 108000, 3600, 18000, 14000), |
| 26 | (32, 216000, 5120, 20480, 20000), |
| 27 | (40, 245760, 8192, 32768, 20000), |
| 28 | (41, 245760, 8192, 32768, 50000), |
| 29 | (42, 522240, 8704, 34816, 50000), |
| 30 | (50, 589824, 22080, 110400, 135000), |
| 31 | (51, 983040, 36864, 184320, 240000), |
| 32 | (52, 2073600, 36864, 184320, 240000), |
| 33 | (60, 4177920, 139264, 696320, 240000), |
| 34 | (61, 8355840, 139264, 696320, 480000), |
| 35 | (62, 16711680, 139264, 696320, 800000), |
| 36 | ]; |
| 37 | |
| 38 | |
| 39 | const HEVC_LEVELS: [(u8, u64, u64, u64); 13] = [ |
| 40 | (30, 36864, 552960, 128), |
| 41 | (60, 122880, 3686400, 1500), |
| 42 | (63, 245760, 7372800, 3000), |
| 43 | (90, 552960, 16588800, 6000), |
| 44 | (93, 983040, 33177600, 10000), |
| 45 | (120, 2228224, 66846720, 12000), |
| 46 | (123, 2228224, 133693440, 20000), |
| 47 | (150, 8912896, 267386880, 25000), |
| 48 | (153, 8912896, 534773760, 40000), |
| 49 | (156, 8912896, 1069547520, 60000), |
| 50 | (180, 35651584, 1069547520, 60000), |
| 51 | (183, 35651584, 2139095040, 120000), |
| 52 | (186, 35651584, 4278190080, 240000), |
| 53 | ]; |
| 54 | |
| 55 | |
| 56 | const AV1_LEVELS: [(u8, u64, u32, u32, u64, u64); 14] = [ |
| 57 | (0, 147_456, 2_048, 1_152, 4_423_680, 1_500), |
| 58 | (1, 278_784, 2_816, 1_584, 8_363_520, 3_000), |
| 59 | (4, 665_856, 4_352, 2_448, 19_975_680, 6_000), |
| 60 | (5, 1_065_024, 5_504, 3_096, 31_950_720, 10_000), |
| 61 | (8, 2_359_296, 6_144, 3_456, 70_778_880, 12_000), |
| 62 | (9, 2_359_296, 6_144, 3_456, 141_557_760, 20_000), |
| 63 | (12, 8_912_896, 8_192, 4_352, 267_386_880, 30_000), |
| 64 | (13, 8_912_896, 8_192, 4_352, 534_773_760, 40_000), |
| 65 | (14, 8_912_896, 8_192, 4_352, 1_069_547_520, 60_000), |
| 66 | (15, 8_912_896, 8_192, 4_352, 1_069_547_520, 60_000), |
| 67 | (16, 35_651_584, 16_384, 8_704, 1_069_547_520, 60_000), |
| 68 | (17, 35_651_584, 16_384, 8_704, 2_139_095_040, 100_000), |
| 69 | (18, 35_651_584, 16_384, 8_704, 4_278_190_080, 160_000), |
| 70 | (19, 35_651_584, 16_384, 8_704, 4_278_190_080, 160_000), |
| 71 | ]; |
| 72 | |
| 73 | const H264_MAX_LEVEL: u8 = 62; |
| 74 | const HEVC_MAX_LEVEL: u8 = 186; |
| 75 | const AV1_MAX_LEVEL: u8 = 31; |
| 76 | |
| 77 | fn aligned(value: u32, block: u32) -> u64 { |
| 78 | u64::from(value.next_multiple_of(block)) |
| 79 | } |
| 80 | |
| 81 | |
| 82 | fn rate_fits(samples: u64, framerate: (u32, u32), limit: u64) -> bool { |
| 83 | let (num, den) = (u64::from(framerate.0.max(1)), u64::from(framerate.1.max(1))); |
| 84 | samples * num <= limit * den |
| 85 | } |
| 86 | |
| 87 | |
| 88 | fn bitrate(config: &EncoderConfig) -> Option<u64> { |
| 89 | match config.rate_control { |
| 90 | RateControlMode::ConstantQp { .. } => None, |
| 91 | _ => Some(u64::from(config.bitrate)), |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | |
| 96 | pub(crate) fn h264(config: &EncoderConfig) -> u8 { |
| 97 | let mbs = (aligned(config.width, H264_BLOCK) / 16) * (aligned(config.height, H264_BLOCK) / 16); |
| 98 | let (w, h) = (aligned(config.width, H264_BLOCK) / 16, aligned(config.height, H264_BLOCK) / 16); |
| 99 | let nal_factor = if config.h264_profile == H264Profile::High { |
| 100 | 1500 |
| 101 | } else { |
| 102 | 1200 |
| 103 | }; |
| 104 | let bitrate = bitrate(config); |
| 105 | H264_LEVELS |
| 106 | .iter() |
| 107 | .find(|&&(_, mbps, fs, dpb, br)| { |
| 108 | rate_fits(mbs, config.framerate, mbps) |
| 109 | && mbs <= fs |
| 110 | && w * w <= 8 * fs |
| 111 | && h * h <= 8 * fs |
| 112 | && mbs * DPB_FRAMES <= dpb |
| 113 | && bitrate.is_none_or(|b| b <= br * nal_factor) |
| 114 | }) |
| 115 | .map_or(H264_MAX_LEVEL, |level| level.0) |
| 116 | } |
| 117 | |
| 118 | |
| 119 | pub(crate) fn hevc(config: &EncoderConfig) -> u8 { |
| 120 | let (w, h) = (u64::from(config.width), u64::from(config.height)); |
| 121 | let samples = w * h; |
| 122 | let bitrate = bitrate(config); |
| 123 | HEVC_LEVELS |
| 124 | .iter() |
| 125 | .find(|&&(_, max_ps, max_sr, br)| { |
| 126 | samples <= max_ps |
| 127 | && w * w <= 8 * max_ps |
| 128 | && h * h <= 8 * max_ps |
| 129 | && rate_fits(samples, config.framerate, max_sr) |
| 130 | && bitrate.is_none_or(|b| b * 10 <= br * 1000 * 11) |
| 131 | }) |
| 132 | .map_or(HEVC_MAX_LEVEL, |level| level.0) |
| 133 | } |
| 134 | |
| 135 | |
| 136 | pub(crate) fn av1(config: &EncoderConfig) -> u8 { |
| 137 | let (width, height) = (config.width.max(1), config.height.max(1)); |
| 138 | let samples = u64::from(width) * u64::from(height); |
| 139 | let bitrate = bitrate(config); |
| 140 | AV1_LEVELS |
| 141 | .iter() |
| 142 | .find(|&&(_, max_size, max_w, max_h, max_rate, main_kbps)| { |
| 143 | samples <= max_size |
| 144 | && width <= max_w |
| 145 | && height <= max_h |
| 146 | && rate_fits(samples, config.framerate, max_rate) |
| 147 | && bitrate.is_none_or(|b| b <= main_kbps * 1000) |
| 148 | }) |
| 149 | .map_or(AV1_MAX_LEVEL, |level| level.0) |
| 150 | } |