forge
Explore
Log in

Don't you dare go Hollow

Merge branch 'worktree-agent-a1660121caf3252ff'

Browse files at this commit

c256206f87f3aa9b0bc9e6586fa4011fae05625aAhmet Kaan GümüşParent: 5d368625b6d329
crates/ec-h265-syntax/src/ps.rsmodified+23 -1
@@ -186,6 +186,8 @@ pub struct Vps {
186 pub max_dec_pic_buffering_minus1: u32,186 pub max_dec_pic_buffering_minus1: u32,
187 /// `vps_max_num_reorder_pics[0]`; 0 for an intra-only stream.187 /// `vps_max_num_reorder_pics[0]`; 0 for an intra-only stream.
188 pub max_num_reorder_pics: u32,188 pub max_num_reorder_pics: u32,
189+ /// `(vps_num_units_in_tick, vps_time_scale)` when `vps_timing_info_present_flag` is set.
190+ pub timing: Option<(u32, u32)>,
189 }191 }
190 192
191 impl Vps {193 impl Vps {
@@ -206,7 +208,16 @@ impl Vps {
206 w.write_ue(0); // vps_max_latency_increase_plus1208 w.write_ue(0); // vps_max_latency_increase_plus1
207 w.write_bits(0, 6); // vps_max_layer_id209 w.write_bits(0, 6); // vps_max_layer_id
208 w.write_ue(0); // vps_num_layer_sets_minus1210 w.write_ue(0); // vps_num_layer_sets_minus1
209- w.write_bit(false); // vps_timing_info_present_flag211+ match self.timing {
212+ Some((num_units_in_tick, time_scale)) => {
213+ w.write_bit(true); // vps_timing_info_present_flag
214+ w.write_bits(num_units_in_tick, 32);
215+ w.write_bits(time_scale, 32);
216+ w.write_bit(false); // vps_poc_proportional_to_timing_flag
217+ w.write_ue(0); // vps_num_hrd_parameters
218+ }
219+ None => w.write_bit(false), // vps_timing_info_present_flag
220+ }
210 w.write_bit(false); // vps_extension_flag221 w.write_bit(false); // vps_extension_flag
211 rbsp_trailing_bits(&mut w);222 rbsp_trailing_bits(&mut w);
212 w.into_bytes()223 w.into_bytes()
@@ -250,11 +261,19 @@ impl Vps {
250 max_num_reorder_pics = r.read_ue()?;261 max_num_reorder_pics = r.read_ue()?;
251 r.read_ue()?;262 r.read_ue()?;
252 }263 }
264+ r.read_bits(6)?; // vps_max_layer_id
265+ let layer_sets_minus1 = r.read_ue()?;
266+ let timing = if layer_sets_minus1 == 0 && r.read_bit()? {
267+ Some((r.read_bits(32)?, r.read_bits(32)?))
268+ } else {
269+ None
270+ };
253 Ok(Vps {271 Ok(Vps {
254 id,272 id,
255 ptl,273 ptl,
256 max_dec_pic_buffering_minus1,274 max_dec_pic_buffering_minus1,
257 max_num_reorder_pics,275 max_num_reorder_pics,
276+ timing,
258 })277 })
259 }278 }
260 }279 }
@@ -1491,6 +1510,7 @@ mod tests {
1491 ptl: ProfileTierLevel::main(120),1510 ptl: ProfileTierLevel::main(120),
1492 max_dec_pic_buffering_minus1: 0,1511 max_dec_pic_buffering_minus1: 0,
1493 max_num_reorder_pics: 0,1512 max_num_reorder_pics: 0,
1513+ timing: None,
1494 };1514 };
1495 assert_eq!(Vps::parse(&vps.to_rbsp()).unwrap(), vps);1515 assert_eq!(Vps::parse(&vps.to_rbsp()).unwrap(), vps);
1496 1516
@@ -1572,6 +1592,7 @@ mod tests {
1572 ptl: ProfileTierLevel::main(120),1592 ptl: ProfileTierLevel::main(120),
1573 max_dec_pic_buffering_minus1: 0,1593 max_dec_pic_buffering_minus1: 0,
1574 max_num_reorder_pics: 0,1594 max_num_reorder_pics: 0,
1595+ timing: None,
1575 }1596 }
1576 .to_rbsp();1597 .to_rbsp();
1577 vps[1] |= 0x0e;1598 vps[1] |= 0x0e;
@@ -1678,6 +1699,7 @@ mod tests {
1678 ptl: ProfileTierLevel::main(120),1699 ptl: ProfileTierLevel::main(120),
1679 max_dec_pic_buffering_minus1: 16,1700 max_dec_pic_buffering_minus1: 16,
1680 max_num_reorder_pics: 0,1701 max_num_reorder_pics: 0,
1702+ timing: None,
1681 };1703 };
1682 refused_naming(Vps::parse(&vps.to_rbsp()), "vps_max_dec_pic_buffering_minus1");1704 refused_naming(Vps::parse(&vps.to_rbsp()), "vps_max_dec_pic_buffering_minus1");
1683 }1705 }
crates/ec-h265-syntax/tests/fuzz.rsmodified+1 -0
@@ -109,6 +109,7 @@ fn ten_thousand_malformed_inputs_never_panic() {
109 ptl: ProfileTierLevel::main(120),109 ptl: ProfileTierLevel::main(120),
110 max_dec_pic_buffering_minus1: 1,110 max_dec_pic_buffering_minus1: 1,
111 max_num_reorder_pics: 0,111 max_num_reorder_pics: 0,
112+ timing: None,
112 }113 }
113 .to_rbsp(),114 .to_rbsp(),
114 ];115 ];
crates/ec-h265/src/encoder.rsmodified+1 -0
@@ -486,6 +486,7 @@ impl Encoder {
486 ptl,486 ptl,
487 max_dec_pic_buffering_minus1: dpb_minus1,487 max_dec_pic_buffering_minus1: dpb_minus1,
488 max_num_reorder_pics: 0,488 max_num_reorder_pics: 0,
489+ timing: None,
489 };490 };
490 let log2_ctb = cfg.ctb_size.trailing_zeros();491 let log2_ctb = cfg.ctb_size.trailing_zeros();
491 let sps = Sps {492 let sps = Sps {
crates/ec-hw/src/enc/av1.rsmodified+2 -44
@@ -52,7 +52,7 @@ pub(super) fn parameters(
52 let (width, height) = (config.width.max(1), config.height.max(1));52 let (width, height) = (config.width.max(1), config.height.max(1));
53 let cqp = config.rate_control.is_cqp();53 let cqp = config.rate_control.is_cqp();
54 let ten_bit = config.bit_depth == 10;54 let ten_bit = config.bit_depth == 10;
55- let level_idx = seq_level_idx(coded_w, coded_h, config.framerate);55+ let level_idx = crate::level::av1(&config);
56 56
57 // enable_order_hint, the bit depth and 4:2:0 subsampling; everything else off.57 // enable_order_hint, the bit depth and 4:2:0 subsampling; everything else off.
58 let seq_fields = AV1EncSeqFields::new(58 let seq_fields = AV1EncSeqFields::new(
@@ -260,6 +260,7 @@ pub(super) fn parameters(
260 height,260 height,
261 seq_level_idx: level_idx,261 seq_level_idx: level_idx,
262 seq_tier: 0,262 seq_tier: 0,
263+ framerate: config.framerate,
263 high_bitdepth: ten_bit,264 high_bitdepth: ten_bit,
264 colour: config.colour,265 colour: config.colour,
265 }),266 }),
@@ -274,46 +275,3 @@ pub(super) fn parameters(
274 )));275 )));
275 Ok(())276 Ok(())
276 }277 }
277-
278-/// The lowest AV1 level whose picture size and rate fit (Annex A), as a
279-/// `seq_level_idx`. Levels below 2.0 do not exist, and anything past 6.3 is
280-/// signalled as the maximum-parameters level.
281-fn seq_level_idx(width: u32, height: u32, framerate: (u32, u32)) -> u8 {
282- // (seq_level_idx, MaxPicSize, MaxHSize, MaxVSize, MaxDisplayRate)
283- const LEVELS: [(u8, u64, u32, u32, u64); 12] = [
284- (0, 147_456, 2_048, 1_152, 4_423_680), // 2.0
285- (1, 278_784, 2_816, 1_584, 8_363_520), // 2.1
286- (4, 665_856, 4_352, 2_448, 19_975_680), // 3.0
287- (5, 1_065_024, 5_504, 3_096, 31_950_720), // 3.1
288- (8, 2_359_296, 6_144, 3_456, 70_778_880), // 4.0
289- (9, 2_359_296, 6_144, 3_456, 141_557_760), // 4.1
290- (12, 8_912_896, 8_192, 4_352, 267_386_880), // 5.0
291- (13, 8_912_896, 8_192, 4_352, 534_773_760), // 5.1
292- (14, 8_912_896, 8_192, 4_352, 1_069_547_520), // 5.2
293- (16, 35_651_584, 16_384, 8_704, 1_069_547_520), // 6.0
294- (17, 35_651_584, 16_384, 8_704, 2_139_095_040), // 6.1
295- (18, 35_651_584, 16_384, 8_704, 4_278_190_080), // 6.2
296- ];
297- let samples = u64::from(width) * u64::from(height);
298- let fps = f64::from(framerate.0.max(1)) / f64::from(framerate.1.max(1));
299- let rate = (samples as f64 * fps).ceil() as u64;
300- for &(idx, max_size, max_h, max_v, max_rate) in &LEVELS {
301- if samples <= max_size && width <= max_h && height <= max_v && rate <= max_rate {
302- return idx;
303- }
304- }
305- 31 // maximum parameters
306-}
307-
308-#[cfg(test)]
309-mod tests {
310- use super::*;
311-
312- #[test]
313- fn levels_follow_size_and_rate() {
314- // 1080p30 is 4.0 (idx 8), 2160p30 5.0 (12), 2160p60 5.1 (13).
315- assert_eq!(seq_level_idx(1920, 1088, (30, 1)), 8);
316- assert_eq!(seq_level_idx(3840, 2176, (30, 1)), 12);
317- assert_eq!(seq_level_idx(3840, 2176, (60, 1)), 13);
318- }
319-}
crates/ec-hw/src/enc/h264.rsmodified+4 -46
@@ -14,7 +14,7 @@ use ec_va::va::{
14 VABufferID, VA_INVALID_ID,14 VABufferID, VA_INVALID_ID,
15 };15 };
16 16
17-use super::{Encoder, RateControlMode, headers};17+use super::{Encoder, H264Profile, RateControlMode, headers};
18 use crate::error::Result;18 use crate::error::Result;
19 use crate::params::h264::{PICTURE_INVALID, PICTURE_SHORT_TERM_REFERENCE};19 use crate::params::h264::{PICTURE_INVALID, PICTURE_SHORT_TERM_REFERENCE};
20 use crate::params::INVALID_SURFACE;20 use crate::params::INVALID_SURFACE;
@@ -75,7 +75,7 @@ pub(super) fn parameters(
75 let position = encoder.gop_position();75 let position = encoder.gop_position();
76 (frame_num_at(position), 2 * position as i32)76 (frame_num_at(position), 2 * position as i32)
77 };77 };
78- let level_idc = level_idc(coded_w, coded_h, config.framerate);78+ let level_idc = crate::level::h264(&config);
79 let intra_period = config.gop_size.max(1);79 let intra_period = config.gop_size.max(1);
80 let bits_per_second = match config.rate_control {80 let bits_per_second = match config.rate_control {
81 RateControlMode::ConstantQp { .. } => 0,81 RateControlMode::ConstantQp { .. } => 0,
@@ -136,6 +136,7 @@ pub(super) fn parameters(
136 );136 );
137 }137 }
138 let cabac = config.h264_profile.cabac();138 let cabac = config.h264_profile.cabac();
139+ let transform_8x8 = config.h264_profile == H264Profile::High;
139 // reference_pic_flag = 1; CABAC in Main and High (10% smaller), CAVLC in140 // reference_pic_flag = 1; CABAC in Main and High (10% smaller), CAVLC in
140 // Constrained Baseline; deblocking_filter_control_present_flag = 1.141 // Constrained Baseline; deblocking_filter_control_present_flag = 1.
141 let pic_fields = H264EncPicFields::new(142 let pic_fields = H264EncPicFields::new(
@@ -145,7 +146,7 @@ pub(super) fn parameters(
145 0,146 0,
146 0,147 0,
147 0,148 0,
148- 0,149+ u32::from(transform_8x8),
149 1,150 1,
150 0,151 0,
151 0,152 0,
@@ -242,46 +243,3 @@ pub(super) fn parameters(
242 out.push(BufferType::EncSliceParameter(EncSliceParameter::H264(slice)));243 out.push(BufferType::EncSliceParameter(EncSliceParameter::H264(slice)));
243 Ok(())244 Ok(())
244 }245 }
245-
246-/// The lowest Annex A level that fits the picture size and rate.
247-///
248-/// Advertising level 6.2 for a 640x480 clip is legal and lazy; a player that
249-/// checks levels against its decoder budget deserves the real one.
250-fn level_idc(width: u32, height: u32, framerate: (u32, u32)) -> u8 {
251- const LEVELS: [(u8, u64, u64); 12] = [
252- (10, 1485, 99),
253- (11, 3000, 396),
254- (20, 11880, 396),
255- (21, 19800, 792),
256- (22, 20250, 1620),
257- (30, 40500, 1620),
258- (31, 108000, 3600),
259- (32, 216000, 5120),
260- (40, 245760, 8192),
261- (50, 589824, 22080),
262- (51, 983040, 36864),
263- (52, 2073600, 36864),
264- ];
265- let frame_mbs = u64::from(width / 16) * u64::from(height / 16);
266- let fps = f64::from(framerate.0.max(1)) / f64::from(framerate.1.max(1));
267- let mbps = (frame_mbs as f64 * fps).ceil() as u64;
268- for &(idc, max_mbps, max_fs) in &LEVELS {
269- if mbps <= max_mbps && frame_mbs <= max_fs {
270- return idc;
271- }
272- }
273- 62
274-}
275-
276-#[cfg(test)]
277-mod tests {
278- use super::*;
279-
280- #[test]
281- fn levels_follow_size_and_rate() {
282- assert_eq!(level_idc(1920, 1088, (30, 1)), 40);
283- assert_eq!(level_idc(1920, 1088, (60, 1)), 50);
284- assert_eq!(level_idc(3840, 2160, (30, 1)), 51);
285- assert_eq!(level_idc(640, 480, (30, 1)), 30);
286- }
287-}
crates/ec-hw/src/enc/headers.rsmodified+13 -1
@@ -155,6 +155,11 @@ pub(super) fn h264_pps(p: &H264Params) -> Packed {
155 w.write_bit(true); // deblocking_filter_control_present_flag155 w.write_bit(true); // deblocking_filter_control_present_flag
156 w.write_bit(false); // constrained_intra_pred_flag156 w.write_bit(false); // constrained_intra_pred_flag
157 w.write_bit(false); // redundant_pic_cnt_present_flag157 w.write_bit(false); // redundant_pic_cnt_present_flag
158+ if p.profile == super::H264Profile::High {
159+ w.write_bit(true); // transform_8x8_mode_flag
160+ w.write_bit(false); // pic_scaling_matrix_present_flag
161+ w.write_se(0); // second_chroma_qp_index_offset
162+ }
158 trailing(&mut w);163 trailing(&mut w);
159 nal(PACKED_HEADER_PICTURE, &[0x68], &w.into_bytes(), 0)164 nal(PACKED_HEADER_PICTURE, &[0x68], &w.into_bytes(), 0)
160 }165 }
@@ -251,6 +256,7 @@ pub(super) mod hevc {
251 ptl,256 ptl,
252 max_dec_pic_buffering_minus1: 1,257 max_dec_pic_buffering_minus1: 1,
253 max_num_reorder_pics: 0,258 max_num_reorder_pics: 0,
259+ timing: Some((p.num_units_in_tick, p.time_scale)),
254 };260 };
255 let sps = Sps {261 let sps = Sps {
256 vps_id: 0,262 vps_id: 0,
@@ -460,6 +466,7 @@ pub(super) mod av1 {
460 pub(in crate::enc) height: u32,466 pub(in crate::enc) height: u32,
461 pub(in crate::enc) seq_level_idx: u8,467 pub(in crate::enc) seq_level_idx: u8,
462 pub(in crate::enc) seq_tier: u8,468 pub(in crate::enc) seq_tier: u8,
469+ pub(in crate::enc) framerate: (u32, u32),
463 /// 10-bit (`high_bitdepth`); profile 0 has no `twelve_bit`.470 /// 10-bit (`high_bitdepth`); profile 0 has no `twelve_bit`.
464 pub(in crate::enc) high_bitdepth: bool,471 pub(in crate::enc) high_bitdepth: bool,
465 pub(in crate::enc) colour: Option<Colour>,472 pub(in crate::enc) colour: Option<Colour>,
@@ -476,7 +483,12 @@ pub(super) mod av1 {
476 w.write_bits(0, 3); // seq_profile483 w.write_bits(0, 3); // seq_profile
477 w.write_bit(false); // still_picture484 w.write_bit(false); // still_picture
478 w.write_bit(false); // reduced_still_picture_header485 w.write_bit(false); // reduced_still_picture_header
479- w.write_bit(false); // timing_info_present_flag486+ w.write_bit(true); // timing_info_present_flag
487+ w.write_bits(p.framerate.1.max(1), 32); // num_units_in_display_tick
488+ w.write_bits(p.framerate.0.max(1), 32); // time_scale
489+ w.write_bit(true); // equal_picture_interval
490+ w.write_bit(true); // num_ticks_per_picture_minus_1 = 0, as uvlc
491+ w.write_bit(false); // decoder_model_info_present_flag
480 w.write_bit(false); // initial_display_delay_present_flag492 w.write_bit(false); // initial_display_delay_present_flag
481 w.write_bits(0, 5); // operating_points_cnt_minus_1493 w.write_bits(0, 5); // operating_points_cnt_minus_1
482 w.write_bits(0, 12); // operating_point_idc[0]494 w.write_bits(0, 12); // operating_point_idc[0]
crates/ec-hw/src/enc/hevc.rsmodified+1 -39
@@ -48,7 +48,7 @@ pub(super) fn parameters(
48 } else {48 } else {
49 encoder.gop_position() as i3249 encoder.gop_position() as i32
50 };50 };
51- let general_level_idc = level_idc(coded_w, coded_h, config.framerate);51+ let general_level_idc = crate::level::hevc(&config);
52 let intra_period = config.gop_size.max(1);52 let intra_period = config.gop_size.max(1);
53 let bits_per_second = match config.rate_control {53 let bits_per_second = match config.rate_control {
54 RateControlMode::ConstantQp { .. } => 0,54 RateControlMode::ConstantQp { .. } => 0,
@@ -242,41 +242,3 @@ pub(super) fn parameters(
242 )));242 )));
243 Ok(())243 Ok(())
244 }244 }
245-
246-/// The lowest level whose luma sample rate and picture size fit (Annex A).
247-fn level_idc(width: u32, height: u32, framerate: (u32, u32)) -> u8 {
248- // (general_level_idc = level * 30, MaxLumaPs, MaxLumaSr)
249- const LEVELS: [(u8, u64, u64); 9] = [
250- (30, 36_864, 552_960),
251- (60, 122_880, 3_686_400),
252- (63, 245_760, 7_372_800),
253- (90, 552_960, 16_588_800),
254- (93, 983_040, 33_177_600),
255- (120, 2_228_224, 66_846_720),
256- (123, 2_228_224, 133_693_440),
257- (150, 8_912_896, 267_386_880),
258- (153, 8_912_896, 534_773_760),
259- ];
260- let samples = u64::from(width) * u64::from(height);
261- let fps = f64::from(framerate.0.max(1)) / f64::from(framerate.1.max(1));
262- let rate = (samples as f64 * fps).ceil() as u64;
263- for &(idc, max_ps, max_sr) in &LEVELS {
264- if samples <= max_ps && rate <= max_sr {
265- return idc;
266- }
267- }
268- 186
269-}
270-
271-#[cfg(test)]
272-mod tests {
273- use super::*;
274-
275- #[test]
276- fn levels_follow_size_and_rate() {
277- // 1080p30 is level 4.0 (120), 1080p60 level 4.1 (123), 2160p30 5.0 (150).
278- assert_eq!(level_idc(1920, 1088, (30, 1)), 120);
279- assert_eq!(level_idc(1920, 1088, (60, 1)), 123);
280- assert_eq!(level_idc(3840, 2160, (30, 1)), 150);
281- }
282-}
crates/ec-hw/src/hw.rsmodified+8 -0
@@ -855,6 +855,11 @@ fn open_vk(config: EncoderConfig) -> Result<VkEncoder> {
855 let gpu = vk_gpu()855 let gpu = vk_gpu()
856 .ok_or_else(|| Error::unsupported("Vulkan Video encode", "no Vulkan Video device"))?;856 .ok_or_else(|| Error::unsupported("Vulkan Video encode", "no Vulkan Video device"))?;
857 let rate = vk_rate(&config, config.bitrate);857 let rate = vk_rate(&config, config.bitrate);
858+ let level = match config.codec {
859+ EncCodec::H264 => crate::level::h264(&config),
860+ EncCodec::H265 => crate::level::hevc(&config),
861+ EncCodec::Av1 => crate::level::av1(&config),
862+ };
858 let (width, height, fps, gop_size) = (863 let (width, height, fps, gop_size) = (
859 config.width,864 config.width,
860 config.height,865 config.height,
@@ -877,6 +882,7 @@ fn open_vk(config: EncoderConfig) -> Result<VkEncoder> {
877 gop_size,882 gop_size,
878 rate,883 rate,
879 profile: vk_h264_profile(config.h264_profile),884 profile: vk_h264_profile(config.h264_profile),
885+ level,
880 colour,886 colour,
881 },887 },
882 )?),888 )?),
@@ -889,6 +895,7 @@ fn open_vk(config: EncoderConfig) -> Result<VkEncoder> {
889 gop_size,895 gop_size,
890 rate,896 rate,
891 bit_depth: config.bit_depth,897 bit_depth: config.bit_depth,
898+ level,
892 colour,899 colour,
893 },900 },
894 )?),901 )?),
@@ -901,6 +908,7 @@ fn open_vk(config: EncoderConfig) -> Result<VkEncoder> {
901 gop_size,908 gop_size,
902 rate,909 rate,
903 bit_depth: config.bit_depth,910 bit_depth: config.bit_depth,
911+ level,
904 colour,912 colour,
905 },913 },
906 )?),914 )?),
crates/ec-hw/src/level.rsadded+150 -0
@@ -0,0 +1,150 @@
1+//! Level selection shared by both encoder backends.
2+//!
3+//! Each function returns the smallest level whose picture size, sample rate,
4+//! DPB and bitrate limits hold for the configuration. The Vulkan shim clamps
5+//! the result to the driver's maximum.
6+
7+use crate::types::{EncoderConfig, H264Profile, RateControlMode};
8+
9+/// `max_dec_pic_buffering` the encoders signal: one reference, no reordering.
10+const DPB_FRAMES: u64 = 1;
11+
12+/// H.264 luma macroblock size; the coded picture is a whole number of them.
13+const H264_BLOCK: u32 = 16;
14+
15+/// `(level_idc, MaxMBPS, MaxFS, MaxDpbMbs, MaxBR kbit/s)`, Table A-1.
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+/// `(general_level_idc, MaxLumaPs, MaxLumaSr, Main tier MaxBR kbit/s)`, Table A.8.
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+/// `(seq_level_idx, MaxPicSize, MaxHSize, MaxVSize, MaxDisplayRate, MainMbps kbit/s)`, Annex A.
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+/// Whether `samples` per picture at the frame rate stay within `limit` per second.
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+/// The bitrate the level must carry, or `None` when the quantiser is fixed.
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+/// `level_idc` of the lowest H.264 level that fits `config`.
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+/// `general_level_idc` of the lowest Main-tier H.265 level that fits `config`.
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+/// `seq_level_idx` of the lowest AV1 level that fits `config`.
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+}
crates/ec-hw/src/lib.rsmodified+1 -0
@@ -63,6 +63,7 @@ pub mod error;
63 pub(crate) mod dpb264;63 pub(crate) mod dpb264;
64 #[cfg_attr(not(target_os = "linux"), allow(dead_code))]64 #[cfg_attr(not(target_os = "linux"), allow(dead_code))]
65 pub(crate) mod h264_common;65 pub(crate) mod h264_common;
66+pub(crate) mod level;
66 pub(crate) mod rate;67 pub(crate) mod rate;
67 pub(crate) mod ref_slots;68 pub(crate) mod ref_slots;
68 pub(crate) mod vkdec_vp9;69 pub(crate) mod vkdec_vp9;
crates/ec-vk/examples/av1_encode.rsmodified+1 -0
@@ -27,6 +27,7 @@ fn main() {
27 width: W,27 width: W,
28 height: H,28 height: H,
29 fps: (FPS, 1),29 fps: (FPS, 1),
30+ level: 23,
30 gop_size: 30,31 gop_size: 30,
31 rate: match std::env::args().nth(2) {32 rate: match std::env::args().nth(2) {
32 Some(b) => RateControl::Cbr {33 Some(b) => RateControl::Cbr {
crates/ec-vk/examples/h264_encode.rsmodified+1 -0
@@ -26,6 +26,7 @@ fn main() {
26 width: W,26 width: W,
27 height: H,27 height: H,
28 fps: (30, 1),28 fps: (30, 1),
29+ level: 62,
29 gop_size: 30,30 gop_size: 30,
30 profile: H264Profile::High,31 profile: H264Profile::High,
31 colour: None,32 colour: None,
crates/ec-vk/examples/h265_encode.rsmodified+1 -0
@@ -26,6 +26,7 @@ fn main() {
26 width: W,26 width: W,
27 height: H,27 height: H,
28 fps: (30, 1),28 fps: (30, 1),
29+ level: 186,
29 gop_size: 30,30 gop_size: 30,
30 rate: match std::env::args().nth(2) {31 rate: match std::env::args().nth(2) {
31 Some(b) => RateControl::Cbr {32 Some(b) => RateControl::Cbr {
crates/ec-vk/src/av1.rsmodified+3 -0
@@ -21,6 +21,8 @@ pub struct Av1EncoderConfig {
21 pub rate: RateControl,21 pub rate: RateControl,
22 /// Luma and chroma bit depth: 8, or 10 for Main 10 (`encode_16`).22 /// Luma and chroma bit depth: 8, or 10 for Main 10 (`encode_16`).
23 pub bit_depth: u8,23 pub bit_depth: u8,
24+ /// `seq_level_idx`, clamped to the driver maximum.
25+ pub level: u8,
24 /// Colour config of the sequence header, or none.26 /// Colour config of the sequence header, or none.
25 pub colour: Option<Colour>,27 pub colour: Option<Colour>,
26 }28 }
@@ -54,6 +56,7 @@ impl Av1Encoder {
54 fps: cfg.fps,56 fps: cfg.fps,
55 rate: cfg.rate,57 rate: cfg.rate,
56 bit_depth: cfg.bit_depth,58 bit_depth: cfg.bit_depth,
59+ level: cfg.level,
57 colour: cfg.colour,60 colour: cfg.colour,
58 },61 },
59 )?;62 )?;
crates/ec-vk/src/h265.rsmodified+3 -0
@@ -19,6 +19,8 @@ pub struct H265EncoderConfig {
19 pub rate: RateControl,19 pub rate: RateControl,
20 /// Luma and chroma bit depth: 8, or 10 for Main 10 (`encode_16`).20 /// Luma and chroma bit depth: 8, or 10 for Main 10 (`encode_16`).
21 pub bit_depth: u8,21 pub bit_depth: u8,
22+ /// `general_level_idc`, clamped to the driver maximum.
23+ pub level: u8,
22 /// Colour description for the VUI, or none.24 /// Colour description for the VUI, or none.
23 pub colour: Option<Colour>,25 pub colour: Option<Colour>,
24 }26 }
@@ -52,6 +54,7 @@ impl H265Encoder {
52 fps: cfg.fps,54 fps: cfg.fps,
53 rate: cfg.rate,55 rate: cfg.rate,
54 bit_depth: cfg.bit_depth,56 bit_depth: cfg.bit_depth,
57+ level: cfg.level,
55 colour: cfg.colour,58 colour: cfg.colour,
56 },59 },
57 )?;60 )?;
crates/ec-vk/src/lib.rsmodified+3 -0
@@ -41,6 +41,8 @@ pub struct H264EncoderConfig {
41 pub rate: RateControl,41 pub rate: RateControl,
42 /// H.264 profile.42 /// H.264 profile.
43 pub profile: H264Profile,43 pub profile: H264Profile,
44+ /// `level_idc`, clamped to the driver maximum.
45+ pub level: u8,
44 /// Colour description for the VUI, or none.46 /// Colour description for the VUI, or none.
45 pub colour: Option<Colour>,47 pub colour: Option<Colour>,
46 }48 }
@@ -107,6 +109,7 @@ impl H264Encoder {
107 fps: cfg.fps,109 fps: cfg.fps,
108 rate: cfg.rate,110 rate: cfg.rate,
109 profile: cfg.profile,111 profile: cfg.profile,
112+ level: cfg.level,
110 colour: cfg.colour,113 colour: cfg.colour,
111 },114 },
112 )?;115 )?;
shims/vk-video-ffi/examples/dmabuf_smoke.rsmodified+3 -0
@@ -88,6 +88,7 @@ impl Encode for H264EncodeSession {
88 fps: FPS,88 fps: FPS,
89 rate: RATE,89 rate: RATE,
90 profile: H264Profile::High,90 profile: H264Profile::High,
91+ level: 62,
91 colour: None,92 colour: None,
92 };93 };
93 H264EncodeSession::new(gpu, &cfg)94 H264EncodeSession::new(gpu, &cfg)
@@ -130,6 +131,7 @@ impl Encode for H265EncodeSession {
130 fps: FPS,131 fps: FPS,
131 rate: RATE,132 rate: RATE,
132 bit_depth: bits,133 bit_depth: bits,
134+ level: 186,
133 colour: None,135 colour: None,
134 };136 };
135 H265EncodeSession::new(gpu, &cfg)137 H265EncodeSession::new(gpu, &cfg)
@@ -174,6 +176,7 @@ impl Encode for Av1EncodeSession {
174 fps: FPS,176 fps: FPS,
175 rate: RATE,177 rate: RATE,
176 bit_depth: bits,178 bit_depth: bits,
179+ level: 23,
177 colour: None,180 colour: None,
178 };181 };
179 Av1EncodeSession::new(gpu, &cfg)182 Av1EncodeSession::new(gpu, &cfg)
shims/vk-video-ffi/src/av1_encode.rsmodified+39 -6
@@ -13,7 +13,6 @@ use crate::session::{
13 use crate::{Colour, Error, Gpu, RateControl};13 use crate::{Colour, Error, Gpu, RateControl};
14 14
15 const AV1_SLOTS: usize = 8;15 const AV1_SLOTS: usize = 8;
16-const LEVEL: vk::video::StdVideoAV1Level = vk::video::STD_VIDEO_AV1_LEVEL_5_1;
17 const CBR_Q_INDEX: u32 = 128;16 const CBR_Q_INDEX: u32 = 128;
18 17
19 /// Settings fixed for the life of an AV1 session.18 /// Settings fixed for the life of an AV1 session.
@@ -29,6 +28,8 @@ pub struct Av1EncodeConfig {
29 pub rate: RateControl,28 pub rate: RateControl,
30 /// Luma and chroma bit depth: 8 or 10 (Main profile, NV12 or P010 input).29 /// Luma and chroma bit depth: 8 or 10 (Main profile, NV12 or P010 input).
31 pub bit_depth: u8,30 pub bit_depth: u8,
31+ /// `seq_level_idx` (0 to 23), chosen by the caller and clamped to the driver maximum.
32+ pub level: u8,
32 /// Colour config of the sequence header, or none (unspecified).33 /// Colour config of the sequence header, or none (unspecified).
33 pub colour: Option<Colour>,34 pub colour: Option<Colour>,
34 }35 }
@@ -62,6 +63,7 @@ struct Caps {
62 rc_modes: vk::VideoEncodeRateControlModeFlagsKHR,63 rc_modes: vk::VideoEncodeRateControlModeFlagsKHR,
63 single_ref: bool,64 single_ref: bool,
64 sb64: bool,65 sb64: bool,
66+ max_level: vk::video::StdVideoAV1Level,
65 }67 }
66 68
67 struct Profile {69 struct Profile {
@@ -180,6 +182,7 @@ impl Av1EncodeSession {
180 if !caps.single_ref {182 if !caps.single_ref {
181 return Err(Error::Unsupported("AV1 single LAST reference".into()));183 return Err(Error::Unsupported("AV1 single LAST reference".into()));
182 }184 }
185+ let level = av1_level(cfg.level, caps.max_level)?;
183 let mode = match cfg.rate {186 let mode = match cfg.rate {
184 RateControl::ConstantQp { .. } => vk::VideoEncodeRateControlModeFlagsKHR::DISABLED,187 RateControl::ConstantQp { .. } => vk::VideoEncodeRateControlModeFlagsKHR::DISABLED,
185 RateControl::Cbr { .. } => vk::VideoEncodeRateControlModeFlagsKHR::CBR,188 RateControl::Cbr { .. } => vk::VideoEncodeRateControlModeFlagsKHR::CBR,
@@ -214,11 +217,11 @@ impl Av1EncodeSession {
214 (cfg.rate, cfg.fps),217 (cfg.rate, cfg.fps),
215 caps.size_align,218 caps.size_align,
216 )?;219 )?;
217- let params = create_parameters(&enc.vs, extent, sb128, cfg)?;220+ let params = create_parameters(&enc.vs, extent, sb128, cfg, level)?;
218 enc.vs.set_params(params);221 enc.vs.set_params(params);
219 Ok(Av1EncodeSession {222 Ok(Av1EncodeSession {
220 enc,223 enc,
221- seq: sequence_header_obu(cfg, sb128),224+ seq: sequence_header_obu(cfg, sb128, level),
222 slot_key: [false; DPB_SLOTS],225 slot_key: [false; DPB_SLOTS],
223 ref_order_hint: [0; AV1_SLOTS],226 ref_order_hint: [0; AV1_SLOTS],
224 })227 })
@@ -503,6 +506,7 @@ fn create_parameters(
503 extent: vk::Extent2D,506 extent: vk::Extent2D,
504 sb128: bool,507 sb128: bool,
505 cfg: &Av1EncodeConfig,508 cfg: &Av1EncodeConfig,
509+ level: vk::video::StdVideoAV1Level,
506 ) -> Result<vk::VideoSessionParametersKHR, Error> {510 ) -> Result<vk::VideoSessionParametersKHR, Error> {
507 let mut color = zeroed::<vk::video::StdVideoAV1ColorConfig>();511 let mut color = zeroed::<vk::video::StdVideoAV1ColorConfig>();
508 color.BitDepth = cfg.bit_depth;512 color.BitDepth = cfg.bit_depth;
@@ -521,9 +525,15 @@ fn create_parameters(
521 color.matrix_coefficients = vk::video::StdVideoAV1MatrixCoefficients(i32::from(c.matrix));525 color.matrix_coefficients = vk::video::StdVideoAV1MatrixCoefficients(i32::from(c.matrix));
522 }526 }
523 527
528+ let mut timing = zeroed::<vk::video::StdVideoAV1TimingInfo>();
529+ timing.flags.set_equal_picture_interval(1);
530+ timing.num_units_in_display_tick = cfg.fps.1;
531+ timing.time_scale = cfg.fps.0;
524 let mut seq = zeroed::<vk::video::StdVideoAV1SequenceHeader>();532 let mut seq = zeroed::<vk::video::StdVideoAV1SequenceHeader>();
525 seq.flags.set_enable_order_hint(1);533 seq.flags.set_enable_order_hint(1);
526 seq.flags.set_use_128x128_superblock(sb128 as u32);534 seq.flags.set_use_128x128_superblock(sb128 as u32);
535+ seq.flags.set_timing_info_present_flag(1);
536+ seq.pTimingInfo = &timing;
527 seq.seq_profile = vk::video::STD_VIDEO_AV1_PROFILE_MAIN;537 seq.seq_profile = vk::video::STD_VIDEO_AV1_PROFILE_MAIN;
528 seq.frame_width_bits_minus_1 = (bits(extent.width) - 1) as u8;538 seq.frame_width_bits_minus_1 = (bits(extent.width) - 1) as u8;
529 seq.frame_height_bits_minus_1 = (bits(extent.height) - 1) as u8;539 seq.frame_height_bits_minus_1 = (bits(extent.height) - 1) as u8;
@@ -534,7 +544,7 @@ fn create_parameters(
534 seq.pColorConfig = &color;544 seq.pColorConfig = &color;
535 545
536 let mut op = zeroed::<vk::video::StdVideoEncodeAV1OperatingPointInfo>();546 let mut op = zeroed::<vk::video::StdVideoEncodeAV1OperatingPointInfo>();
537- op.seq_level_idx = LEVEL.0 as u8;547+ op.seq_level_idx = level.0 as u8;
538 548
539 let mut av1 = vk::VideoEncodeAV1SessionParametersCreateInfoKHR {549 let mut av1 = vk::VideoEncodeAV1SessionParametersCreateInfoKHR {
540 std_sequence_header: &seq,550 std_sequence_header: &seq,
@@ -596,18 +606,29 @@ fn obu(kind: u8, payload: &[u8]) -> Vec<u8> {
596 out606 out
597 }607 }
598 608
599-fn sequence_header_obu(cfg: &Av1EncodeConfig, sb128: bool) -> Vec<u8> {609+fn sequence_header_obu(
610+ cfg: &Av1EncodeConfig,
611+ sb128: bool,
612+ level: vk::video::StdVideoAV1Level,
613+) -> Vec<u8> {
600 let (width, height, bit_depth) = (cfg.width, cfg.height, cfg.bit_depth);614 let (width, height, bit_depth) = (cfg.width, cfg.height, cfg.bit_depth);
601 let mut b = Bits(Vec::new());615 let mut b = Bits(Vec::new());
602 b.put(0, 3);616 b.put(0, 3);
603 b.put(0, 1);617 b.put(0, 1);
604 b.put(0, 1);618 b.put(0, 1);
619+ b.put(1, 1);
620+ b.put(cfg.fps.1, 32);
621+ b.put(cfg.fps.0, 32);
622+ b.put(1, 1);
623+ b.put(1, 1);
605 b.put(0, 1);624 b.put(0, 1);
606 b.put(0, 1);625 b.put(0, 1);
607 b.put(0, 5);626 b.put(0, 5);
608 b.put(0, 12);627 b.put(0, 12);
609- b.put(LEVEL.0 as u32, 5);628+ b.put(level.0 as u32, 5);
629+ if level.0 > 7 {
610 b.put(0, 1);630 b.put(0, 1);
631+ }
611 b.put(bits(width) - 1, 4);632 b.put(bits(width) - 1, 4);
612 b.put(bits(height) - 1, 4);633 b.put(bits(height) - 1, 4);
613 b.put(width - 1, bits(width));634 b.put(width - 1, bits(width));
@@ -700,5 +721,17 @@ fn query_caps(gpu: &Gpu, profile: &vk::VideoProfileInfoKHR) -> Result<Caps, Erro
700 sb64: av1.superblock_sizes.contains(721 sb64: av1.superblock_sizes.contains(
701 vk::VideoEncodeAV1SuperblockSizeFlagsKHR::VIDEO_ENCODE_AV1_SUPERBLOCK_SIZE_64,722 vk::VideoEncodeAV1SuperblockSizeFlagsKHR::VIDEO_ENCODE_AV1_SUPERBLOCK_SIZE_64,
702 ),723 ),
724+ max_level: av1.max_level,
703 })725 })
704 }726 }
727+
728+fn av1_level(
729+ idx: u8,
730+ driver_max: vk::video::StdVideoAV1Level,
731+) -> Result<vk::video::StdVideoAV1Level, Error> {
732+ if idx > 23 {
733+ return Err(Error::Invalid(format!("AV1 seq_level_idx {idx}")));
734+ }
735+ let level = vk::video::StdVideoAV1Level(i32::from(idx));
736+ Ok(if level.0 > driver_max.0 { driver_max } else { level })
737+}
shims/vk-video-ffi/src/h264_encode.rsmodified+37 -20
@@ -55,6 +55,8 @@ pub struct H264EncodeConfig {
55 pub rate: RateControl,55 pub rate: RateControl,
56 /// H.264 profile.56 /// H.264 profile.
57 pub profile: H264Profile,57 pub profile: H264Profile,
58+ /// `level_idc` (10 to 62), chosen by the caller and clamped to the driver maximum.
59+ pub level: u8,
58 /// Colour description for the VUI, or none.60 /// Colour description for the VUI, or none.
59 pub colour: Option<Colour>,61 pub colour: Option<Colour>,
60 }62 }
@@ -93,6 +95,7 @@ struct Caps {
93 size_align: u64,95 size_align: u64,
94 rc_modes: vk::VideoEncodeRateControlModeFlagsKHR,96 rc_modes: vk::VideoEncodeRateControlModeFlagsKHR,
95 max_refs: u32,97 max_refs: u32,
98+ max_level: vk::video::StdVideoH264LevelIdc,
96 }99 }
97 100
98 struct Profile {101 struct Profile {
@@ -203,12 +206,12 @@ impl H264EncodeSession {
203 }206 }
204 let width = align(cfg.width, 16);207 let width = align(cfg.width, 16);
205 let height = align(cfg.height, 16);208 let height = align(cfg.height, 16);
206- let level = level_for(width, height, cfg.fps)?;
207 let profile = Profile::new(cfg.profile);209 let profile = Profile::new(cfg.profile);
208 let caps = query_caps(gpu, &profile.info).map_err(|e| match e {210 let caps = query_caps(gpu, &profile.info).map_err(|e| match e {
209 Error::Vk(..) => Error::Unsupported(format!("H.264 {:?} encode", cfg.profile)),211 Error::Vk(..) => Error::Unsupported(format!("H.264 {:?} encode", cfg.profile)),
210 other => other,212 other => other,
211 })?;213 })?;
214+ let level = h264_level(cfg.level, caps.max_level)?;
212 if width > caps.max_extent.width || height > caps.max_extent.height {215 if width > caps.max_extent.width || height > caps.max_extent.height {
213 return Err(Error::Unsupported(format!("H.264 encode {width}x{height}")));216 return Err(Error::Unsupported(format!("H.264 encode {width}x{height}")));
214 }217 }
@@ -579,25 +582,6 @@ pub(crate) fn validate(
579 validate_rate(fps, rate)582 validate_rate(fps, rate)
580 }583 }
581 584
582-fn level_for(
583- width: u32,
584- height: u32,
585- fps: (u32, u32),
586-) -> Result<vk::video::StdVideoH264LevelIdc, Error> {
587- let mbs = (width / 16) as u64 * (height / 16) as u64;
588- let table = [
589- (8160u64, 245760u64, vk::video::STD_VIDEO_H264_LEVEL_IDC_4_1),
590- (8704, 522240, vk::video::STD_VIDEO_H264_LEVEL_IDC_4_2),
591- (36864, 983040, vk::video::STD_VIDEO_H264_LEVEL_IDC_5_1),
592- (36864, 2073600, vk::video::STD_VIDEO_H264_LEVEL_IDC_5_2),
593- ];
594- table
595- .into_iter()
596- .find(|&(frame, mbps, _)| mbs <= frame && mbs * fps.0 as u64 <= mbps * fps.1 as u64)
597- .map(|(_, _, level)| level)
598- .ok_or_else(|| Error::Unsupported(format!("H.264 level for {width}x{height} at {fps:?}")))
599-}
600-
601 fn query_caps(gpu: &Gpu, profile: &vk::VideoProfileInfoKHR) -> Result<Caps, Error> {585 fn query_caps(gpu: &Gpu, profile: &vk::VideoProfileInfoKHR) -> Result<Caps, Error> {
602 let mut h264 = vk::VideoEncodeH264CapabilitiesKHR::builder();586 let mut h264 = vk::VideoEncodeH264CapabilitiesKHR::builder();
603 let mut encode = vk::VideoEncodeCapabilitiesKHR::builder();587 let mut encode = vk::VideoEncodeCapabilitiesKHR::builder();
@@ -611,6 +595,7 @@ fn query_caps(gpu: &Gpu, profile: &vk::VideoProfileInfoKHR) -> Result<Caps, Erro
611 size_align: caps.min_bitstream_buffer_size_alignment,595 size_align: caps.min_bitstream_buffer_size_alignment,
612 rc_modes: encode.rate_control_modes,596 rc_modes: encode.rate_control_modes,
613 max_refs: h264.max_p_picture_l0_reference_count,597 max_refs: h264.max_p_picture_l0_reference_count,
598+ max_level: h264.max_level_idc,
614 })599 })
615 }600 }
616 601
@@ -625,3 +610,35 @@ fn reference_info(
625 info.PicOrderCnt = poc;610 info.PicOrderCnt = poc;
626 info611 info
627 }612 }
613+
614+fn h264_level(
615+ idc: u8,
616+ driver_max: vk::video::StdVideoH264LevelIdc,
617+) -> Result<vk::video::StdVideoH264LevelIdc, Error> {
618+ const LEVELS: [(u8, vk::video::StdVideoH264LevelIdc); 19] = [
619+ (10, vk::video::STD_VIDEO_H264_LEVEL_IDC_1_0),
620+ (11, vk::video::STD_VIDEO_H264_LEVEL_IDC_1_1),
621+ (12, vk::video::STD_VIDEO_H264_LEVEL_IDC_1_2),
622+ (13, vk::video::STD_VIDEO_H264_LEVEL_IDC_1_3),
623+ (20, vk::video::STD_VIDEO_H264_LEVEL_IDC_2_0),
624+ (21, vk::video::STD_VIDEO_H264_LEVEL_IDC_2_1),
625+ (22, vk::video::STD_VIDEO_H264_LEVEL_IDC_2_2),
626+ (30, vk::video::STD_VIDEO_H264_LEVEL_IDC_3_0),
627+ (31, vk::video::STD_VIDEO_H264_LEVEL_IDC_3_1),
628+ (32, vk::video::STD_VIDEO_H264_LEVEL_IDC_3_2),
629+ (40, vk::video::STD_VIDEO_H264_LEVEL_IDC_4_0),
630+ (41, vk::video::STD_VIDEO_H264_LEVEL_IDC_4_1),
631+ (42, vk::video::STD_VIDEO_H264_LEVEL_IDC_4_2),
632+ (50, vk::video::STD_VIDEO_H264_LEVEL_IDC_5_0),
633+ (51, vk::video::STD_VIDEO_H264_LEVEL_IDC_5_1),
634+ (52, vk::video::STD_VIDEO_H264_LEVEL_IDC_5_2),
635+ (60, vk::video::STD_VIDEO_H264_LEVEL_IDC_6_0),
636+ (61, vk::video::STD_VIDEO_H264_LEVEL_IDC_6_1),
637+ (62, vk::video::STD_VIDEO_H264_LEVEL_IDC_6_2),
638+ ];
639+ let (_, level) = LEVELS
640+ .into_iter()
641+ .find(|(value, _)| *value == idc)
642+ .ok_or_else(|| Error::Invalid(format!("H.264 level_idc {idc}")))?;
643+ Ok(if level.0 > driver_max.0 { driver_max } else { level })
644+}
shims/vk-video-ffi/src/h265_encode.rsmodified+26 -19
@@ -28,6 +28,8 @@ pub struct H265EncodeConfig {
28 pub rate: RateControl,28 pub rate: RateControl,
29 /// Luma and chroma bit depth: 8 (Main, NV12 input) or 10 (Main 10, P010 input).29 /// Luma and chroma bit depth: 8 (Main, NV12 input) or 10 (Main 10, P010 input).
30 pub bit_depth: u8,30 pub bit_depth: u8,
31+ /// `general_level_idc` (30 to 186), chosen by the caller and clamped to the driver maximum.
32+ pub level: u8,
31 /// Colour description for the VUI, or none.33 /// Colour description for the VUI, or none.
32 pub colour: Option<Colour>,34 pub colour: Option<Colour>,
33 }35 }
@@ -60,6 +62,7 @@ struct Caps {
60 size_align: u64,62 size_align: u64,
61 rc_modes: vk::VideoEncodeRateControlModeFlagsKHR,63 rc_modes: vk::VideoEncodeRateControlModeFlagsKHR,
62 max_refs: u32,64 max_refs: u32,
65+ max_level: vk::video::StdVideoH265LevelIdc,
63 ctb_sizes: vk::VideoEncodeH265CtbSizeFlagsKHR,66 ctb_sizes: vk::VideoEncodeH265CtbSizeFlagsKHR,
64 transform_sizes: vk::VideoEncodeH265TransformBlockSizeFlagsKHR,67 transform_sizes: vk::VideoEncodeH265TransformBlockSizeFlagsKHR,
65 std_flags: vk::VideoEncodeH265StdFlagsKHR,68 std_flags: vk::VideoEncodeH265StdFlagsKHR,
@@ -647,7 +650,7 @@ fn coding(caps: &Caps, cfg: &H265EncodeConfig) -> Result<Coding, Error> {
647 transform_skip: caps650 transform_skip: caps
648 .std_flags651 .std_flags
649 .contains(vk::VideoEncodeH265StdFlagsKHR::TRANSFORM_SKIP_ENABLED_FLAG_SET),652 .contains(vk::VideoEncodeH265StdFlagsKHR::TRANSFORM_SKIP_ENABLED_FLAG_SET),
650- level: level_for(width, height, cfg.fps)?,653+ level: h265_level(cfg.level, caps.max_level)?,
651 })654 })
652 }655 }
653 656
@@ -659,27 +662,30 @@ fn profile_idc(bit_depth: u8) -> vk::video::StdVideoH265ProfileIdc {
659 }662 }
660 }663 }
661 664
662-fn level_for(665+fn h265_level(
663- width: u32,666+ idc: u8,
664- height: u32,667+ driver_max: vk::video::StdVideoH265LevelIdc,
665- fps: (u32, u32),
666 ) -> Result<vk::video::StdVideoH265LevelIdc, Error> {668 ) -> Result<vk::video::StdVideoH265LevelIdc, Error> {
667- let luma = width as u64 * height as u64;669+ const LEVELS: [(u8, vk::video::StdVideoH265LevelIdc); 13] = [
668- let table = [670+ (30, vk::video::STD_VIDEO_H265_LEVEL_IDC_1_0),
669- (671+ (60, vk::video::STD_VIDEO_H265_LEVEL_IDC_2_0),
670- 983040u64,672+ (63, vk::video::STD_VIDEO_H265_LEVEL_IDC_2_1),
671- 33177600u64,673+ (90, vk::video::STD_VIDEO_H265_LEVEL_IDC_3_0),
672- vk::video::STD_VIDEO_H265_LEVEL_IDC_3_1,674+ (93, vk::video::STD_VIDEO_H265_LEVEL_IDC_3_1),
673- ),675+ (120, vk::video::STD_VIDEO_H265_LEVEL_IDC_4_0),
674- (2228224, 66846720, vk::video::STD_VIDEO_H265_LEVEL_IDC_4_0),676+ (123, vk::video::STD_VIDEO_H265_LEVEL_IDC_4_1),
675- (2228224, 133693440, vk::video::STD_VIDEO_H265_LEVEL_IDC_4_1),677+ (150, vk::video::STD_VIDEO_H265_LEVEL_IDC_5_0),
676- (8912896, 267386880, vk::video::STD_VIDEO_H265_LEVEL_IDC_5_0),678+ (153, vk::video::STD_VIDEO_H265_LEVEL_IDC_5_1),
679+ (156, vk::video::STD_VIDEO_H265_LEVEL_IDC_5_2),
680+ (180, vk::video::STD_VIDEO_H265_LEVEL_IDC_6_0),
681+ (183, vk::video::STD_VIDEO_H265_LEVEL_IDC_6_1),
682+ (186, vk::video::STD_VIDEO_H265_LEVEL_IDC_6_2),
677 ];683 ];
678- table684+ let (_, level) = LEVELS
679 .into_iter()685 .into_iter()
680- .find(|&(ps, sr, _)| luma <= ps && luma * fps.0 as u64 <= sr * fps.1 as u64)686+ .find(|(value, _)| *value == idc)
681- .map(|(_, _, level)| level)687+ .ok_or_else(|| Error::Invalid(format!("H.265 general_level_idc {idc}")))?;
682- .ok_or_else(|| Error::Unsupported(format!("H.265 level for {width}x{height} at {fps:?}")))688+ Ok(if level.0 > driver_max.0 { driver_max } else { level })
683 }689 }
684 690
685 fn query_caps(gpu: &Gpu, profile: &vk::VideoProfileInfoKHR) -> Result<Caps, Error> {691 fn query_caps(gpu: &Gpu, profile: &vk::VideoProfileInfoKHR) -> Result<Caps, Error> {
@@ -695,6 +701,7 @@ fn query_caps(gpu: &Gpu, profile: &vk::VideoProfileInfoKHR) -> Result<Caps, Erro
695 size_align: caps.min_bitstream_buffer_size_alignment,701 size_align: caps.min_bitstream_buffer_size_alignment,
696 rc_modes: encode.rate_control_modes,702 rc_modes: encode.rate_control_modes,
697 max_refs: h265.max_p_picture_l0_reference_count,703 max_refs: h265.max_p_picture_l0_reference_count,
704+ max_level: h265.max_level_idc,
698 ctb_sizes: h265.ctb_sizes,705 ctb_sizes: h265.ctb_sizes,
699 transform_sizes: h265.transform_block_sizes,706 transform_sizes: h265.transform_block_sizes,
700 std_flags: h265.std_syntax_flags,707 std_flags: h265.std_syntax_flags,