forge
Explore
Log in

Don't you dare go Hollow

ec-hw: level tables and checks match ffmpeg (AV1 MainMbps, aspect bounds, NAL factors)

Browse files at this commit

5b6d3298b1cd4fa5921f119a3f0db41c0cfa5236Ahmet Kaan GümüşParent: ab12e5b
crates/ec-hw/src/level.rsmodified+23 -18
@@ -12,9 +12,6 @@ const DPB_FRAMES: u64 = 1;
1212 /// H.264 luma macroblock size; the coded picture is a whole number of them.
1313 const H264_BLOCK: u32 = 16;
1414
15-/// H.265 coded picture size is a whole number of 32-sample blocks here.
16-const H265_BLOCK: u32 = 32;
17-
1815 /// `(level_idc, MaxMBPS, MaxFS, MaxDpbMbs, MaxBR kbit/s)`, Table A-1.
1916 const H264_LEVELS: [(u8, u64, u64, u64, u64); 19] = [
2017 (10, 1485, 99, 396, 64),
@@ -56,19 +53,21 @@ const HEVC_LEVELS: [(u8, u64, u64, u64); 13] = [
5653 ];
5754
5855 /// `(seq_level_idx, MaxPicSize, MaxHSize, MaxVSize, MaxDisplayRate, MainMbps kbit/s)`, Annex A.
59-const AV1_LEVELS: [(u8, u64, u32, u32, u64, u64); 12] = [
56+const AV1_LEVELS: [(u8, u64, u32, u32, u64, u64); 14] = [
6057 (0, 147_456, 2_048, 1_152, 4_423_680, 1_500),
6158 (1, 278_784, 2_816, 1_584, 8_363_520, 3_000),
62- (4, 665_856, 4_352, 2_448, 19_975_680, 5_000),
59+ (4, 665_856, 4_352, 2_448, 19_975_680, 6_000),
6360 (5, 1_065_024, 5_504, 3_096, 31_950_720, 10_000),
64- (8, 2_359_296, 6_144, 3_456, 70_778_880, 20_000),
65- (9, 2_359_296, 6_144, 3_456, 141_557_760, 30_000),
66- (12, 8_912_896, 8_192, 4_352, 267_386_880, 60_000),
67- (13, 8_912_896, 8_192, 4_352, 534_773_760, 100_000),
68- (14, 8_912_896, 8_192, 4_352, 1_069_547_520, 160_000),
69- (16, 35_651_584, 16_384, 8_704, 1_069_547_520, 240_000),
70- (17, 35_651_584, 16_384, 8_704, 2_139_095_040, 480_000),
71- (18, 35_651_584, 16_384, 8_704, 4_278_190_080, 800_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),
7271 ];
7372
7473 const H264_MAX_LEVEL: u8 = 62;
@@ -96,10 +95,11 @@ fn bitrate(config: &EncoderConfig) -> Option<u64> {
9695 /// `level_idc` of the lowest H.264 level that fits `config`.
9796 pub(crate) fn h264(config: &EncoderConfig) -> u8 {
9897 let mbs = (aligned(config.width, H264_BLOCK) / 16) * (aligned(config.height, H264_BLOCK) / 16);
99- let scale = if config.h264_profile == H264Profile::High {
100- 5
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
101101 } else {
102- 4
102+ 1200
103103 };
104104 let bitrate = bitrate(config);
105105 H264_LEVELS
@@ -107,20 +107,25 @@ pub(crate) fn h264(config: &EncoderConfig) -> u8 {
107107 .find(|&&(_, mbps, fs, dpb, br)| {
108108 rate_fits(mbs, config.framerate, mbps)
109109 && mbs <= fs
110+ && w * w <= 8 * fs
111+ && h * h <= 8 * fs
110112 && mbs * DPB_FRAMES <= dpb
111- && bitrate.is_none_or(|b| b * 4 <= br * 1000 * scale)
113+ && bitrate.is_none_or(|b| b <= br * nal_factor)
112114 })
113115 .map_or(H264_MAX_LEVEL, |level| level.0)
114116 }
115117
116118 /// `general_level_idc` of the lowest Main-tier H.265 level that fits `config`.
117119 pub(crate) fn hevc(config: &EncoderConfig) -> u8 {
118- let samples = aligned(config.width, H265_BLOCK) * aligned(config.height, H265_BLOCK);
120+ let (w, h) = (u64::from(config.width), u64::from(config.height));
121+ let samples = w * h;
119122 let bitrate = bitrate(config);
120123 HEVC_LEVELS
121124 .iter()
122125 .find(|&&(_, max_ps, max_sr, br)| {
123126 samples <= max_ps
127+ && w * w <= 8 * max_ps
128+ && h * h <= 8 * max_ps
124129 && rate_fits(samples, config.framerate, max_sr)
125130 && bitrate.is_none_or(|b| b * 10 <= br * 1000 * 11)
126131 })