ec-hw: level tables and checks match ffmpeg (AV1 MainMbps, aspect bounds, NAL factors)
Browse files at this commit5b6d3298b1cd4fa5921f119a3f0db41c0cfa5236Parent: ab12e5b1 changed file +23 -18
- crates/ec-hw/src/level.rs +23 -18
crates/ec-hw/src/level.rsmodified+23 -18
| @@ -12,9 +12,6 @@ const DPB_FRAMES: u64 = 1; | ||
| 12 | 12 | /// H.264 luma macroblock size; the coded picture is a whole number of them. |
| 13 | 13 | const H264_BLOCK: u32 = 16; |
| 14 | 14 | |
| 15 | -/// H.265 coded picture size is a whole number of 32-sample blocks here. | |
| 16 | -const H265_BLOCK: u32 = 32; | |
| 17 | - | |
| 18 | 15 | /// `(level_idc, MaxMBPS, MaxFS, MaxDpbMbs, MaxBR kbit/s)`, Table A-1. |
| 19 | 16 | const H264_LEVELS: [(u8, u64, u64, u64, u64); 19] = [ |
| 20 | 17 | (10, 1485, 99, 396, 64), |
| @@ -56,19 +53,21 @@ const HEVC_LEVELS: [(u8, u64, u64, u64); 13] = [ | ||
| 56 | 53 | ]; |
| 57 | 54 | |
| 58 | 55 | /// `(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] = [ | |
| 60 | 57 | (0, 147_456, 2_048, 1_152, 4_423_680, 1_500), |
| 61 | 58 | (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), | |
| 63 | 60 | (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), | |
| 72 | 71 | ]; |
| 73 | 72 | |
| 74 | 73 | const H264_MAX_LEVEL: u8 = 62; |
| @@ -96,10 +95,11 @@ fn bitrate(config: &EncoderConfig) -> Option<u64> { | ||
| 96 | 95 | /// `level_idc` of the lowest H.264 level that fits `config`. |
| 97 | 96 | pub(crate) fn h264(config: &EncoderConfig) -> u8 { |
| 98 | 97 | 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 | |
| 101 | 101 | } else { |
| 102 | - 4 | |
| 102 | + 1200 | |
| 103 | 103 | }; |
| 104 | 104 | let bitrate = bitrate(config); |
| 105 | 105 | H264_LEVELS |
| @@ -107,20 +107,25 @@ pub(crate) fn h264(config: &EncoderConfig) -> u8 { | ||
| 107 | 107 | .find(|&&(_, mbps, fs, dpb, br)| { |
| 108 | 108 | rate_fits(mbs, config.framerate, mbps) |
| 109 | 109 | && mbs <= fs |
| 110 | + && w * w <= 8 * fs | |
| 111 | + && h * h <= 8 * fs | |
| 110 | 112 | && 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) | |
| 112 | 114 | }) |
| 113 | 115 | .map_or(H264_MAX_LEVEL, |level| level.0) |
| 114 | 116 | } |
| 115 | 117 | |
| 116 | 118 | /// `general_level_idc` of the lowest Main-tier H.265 level that fits `config`. |
| 117 | 119 | 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; | |
| 119 | 122 | let bitrate = bitrate(config); |
| 120 | 123 | HEVC_LEVELS |
| 121 | 124 | .iter() |
| 122 | 125 | .find(|&&(_, max_ps, max_sr, br)| { |
| 123 | 126 | samples <= max_ps |
| 127 | + && w * w <= 8 * max_ps | |
| 128 | + && h * h <= 8 * max_ps | |
| 124 | 129 | && rate_fits(samples, config.framerate, max_sr) |
| 125 | 130 | && bitrate.is_none_or(|b| b * 10 <= br * 1000 * 11) |
| 126 | 131 | }) |