vk-video-ffi: H265DecodeSession (Main 8-bit, distinct DPB/output, resolved short-term RPS); smoke example bit-exact vs ffmpeg
Browse files at this commit6f8f2a3fff601d5dc714593d00d4d1efb7e74970Parent: 18fc8577 changed files +1604 -15
- Cargo.lock +1 -0
- crates/ec-vk/src/lib.rs +3 -1
- shims/vk-video-ffi/Cargo.toml +1 -0
- shims/vk-video-ffi/examples/h265_decode_smoke.rs +282 -0
- shims/vk-video-ffi/src/h264_decode.rs +14 -14
- shims/vk-video-ffi/src/h265_decode.rs +1297 -0
- shims/vk-video-ffi/src/lib.rs +6 -0
Cargo.lockmodified+1 -0
| @@ -1896,6 +1896,7 @@ name = "vk-video-ffi" | |||
| 1896 | version = "0.1.0" | 1896 | version = "0.1.0" |
| 1897 | dependencies = [ | 1897 | dependencies = [ |
| 1898 | "ec-h264-syntax", | 1898 | "ec-h264-syntax", |
| 1899 | + "ec-h265-syntax", | ||
| 1899 | "vulkanalia", | 1900 | "vulkanalia", |
| 1900 | ] | 1901 | ] |
| 1901 | 1902 | ||
crates/ec-vk/src/lib.rsmodified+3 -1
| @@ -11,7 +11,9 @@ pub use av1::{Av1Encoder, Av1EncoderConfig}; | |||
| 11 | pub use h265::{H265Encoder, H265EncoderConfig}; | 11 | pub use h265::{H265Encoder, H265EncoderConfig}; |
| 12 | pub use vk_video_ffi::{ | 12 | pub use vk_video_ffi::{ |
| 13 | Codec, Direction, Error, Gpu, H264DecodeConfig, H264DecodePicture, H264DecodeSession, H264Pps, | 13 | Codec, Direction, Error, Gpu, H264DecodeConfig, H264DecodePicture, H264DecodeSession, H264Pps, |
| 14 | - H264RefSlot, H264Sps, RateControl, ScalingLists, | 14 | + H264RefSlot, H264Sps, H265DecPicBufMgr, H265DecodeConfig, H265DecodePicture, H265DecodeSession, |
| 15 | + H265LongTermRefPicSps, H265Pcm, H265Pps, H265ProfileTierLevel, H265RefSlot, H265ScalingLists, | ||
| 16 | + H265ShortTermRefPicSet, H265Sps, H265Vps, RateControl, ScalingLists, | ||
| 15 | }; | 17 | }; |
| 16 | 18 | ||
| 17 | /// Encoder settings. | 19 | /// Encoder settings. |
shims/vk-video-ffi/Cargo.tomlmodified+1 -0
| @@ -10,3 +10,4 @@ vulkanalia = { version = "0.35", features = ["libloading"] } | |||
| 10 | 10 | ||
| 11 | [dev-dependencies] | 11 | [dev-dependencies] |
| 12 | ec-h264-syntax.workspace = true | 12 | ec-h264-syntax.workspace = true |
| 13 | +ec-h265-syntax.workspace = true | ||
shims/vk-video-ffi/examples/h265_decode_smoke.rsadded+282 -0
| @@ -0,0 +1,282 @@ | |||
| 1 | +use std::fs; | ||
| 2 | +use std::path::PathBuf; | ||
| 3 | +use std::process::Command; | ||
| 4 | + | ||
| 5 | +use ec_h265_syntax::{ | ||
| 6 | + NalUnitType, Pps, ProfileTierLevel, ScalingList, ShortTermRefPicSet, SliceHeader, Sps, Vps, | ||
| 7 | + split_annex_b, | ||
| 8 | +}; | ||
| 9 | +use vk_video_ffi::{ | ||
| 10 | + Gpu, H265DecPicBufMgr, H265DecodeConfig, H265DecodePicture, H265DecodeSession, | ||
| 11 | + H265LongTermRefPicSps, H265Pcm, H265Pps, H265ProfileTierLevel, H265ScalingLists, | ||
| 12 | + H265ShortTermRefPicSet, H265Sps, H265Vps, | ||
| 13 | +}; | ||
| 14 | + | ||
| 15 | +fn ptl(p: &ProfileTierLevel) -> H265ProfileTierLevel { | ||
| 16 | + H265ProfileTierLevel { | ||
| 17 | + tier_flag: p.tier_flag, | ||
| 18 | + progressive_source: p.progressive_source, | ||
| 19 | + interlaced_source: p.interlaced_source, | ||
| 20 | + non_packed_constraint: p.non_packed_constraint, | ||
| 21 | + frame_only_constraint: p.frame_only_constraint, | ||
| 22 | + profile_idc: p.profile_idc, | ||
| 23 | + level_idc: p.level_idc, | ||
| 24 | + } | ||
| 25 | +} | ||
| 26 | + | ||
| 27 | +fn dpbm( | ||
| 28 | + max_latency_increase_plus1: u32, | ||
| 29 | + max_dec_pic_buffering_minus1: u32, | ||
| 30 | + reorder: u32, | ||
| 31 | +) -> H265DecPicBufMgr { | ||
| 32 | + H265DecPicBufMgr { | ||
| 33 | + max_latency_increase_plus1: [max_latency_increase_plus1; 7], | ||
| 34 | + max_dec_pic_buffering_minus1: [max_dec_pic_buffering_minus1 as u8; 7], | ||
| 35 | + max_num_reorder_pics: [reorder as u8; 7], | ||
| 36 | + } | ||
| 37 | +} | ||
| 38 | + | ||
| 39 | +fn lists(l: &ScalingList) -> H265ScalingLists { | ||
| 40 | + let c = &l.coefficients; | ||
| 41 | + H265ScalingLists { | ||
| 42 | + list_4x4: std::array::from_fn(|i| c[0][i][..16].try_into().unwrap()), | ||
| 43 | + list_8x8: std::array::from_fn(|i| c[1][i]), | ||
| 44 | + list_16x16: std::array::from_fn(|i| c[2][i]), | ||
| 45 | + list_32x32: std::array::from_fn(|i| c[3][3 * i]), | ||
| 46 | + dc_16x16: std::array::from_fn(|i| l.dc[2][i]), | ||
| 47 | + dc_32x32: std::array::from_fn(|i| l.dc[3][3 * i]), | ||
| 48 | + } | ||
| 49 | +} | ||
| 50 | + | ||
| 51 | +fn rps(r: &ShortTermRefPicSet) -> H265ShortTermRefPicSet { | ||
| 52 | + let (n0, n1) = (r.num_negative as usize, r.num_positive as usize); | ||
| 53 | + H265ShortTermRefPicSet { | ||
| 54 | + delta_poc_s0: r.delta_poc_s0[..n0].to_vec(), | ||
| 55 | + used_by_curr_pic_s0: r.used_s0[..n0].to_vec(), | ||
| 56 | + delta_poc_s1: r.delta_poc_s1[..n1].to_vec(), | ||
| 57 | + used_by_curr_pic_s1: r.used_s1[..n1].to_vec(), | ||
| 58 | + } | ||
| 59 | +} | ||
| 60 | + | ||
| 61 | +fn main() { | ||
| 62 | + let dir = PathBuf::from(std::env::args().nth(1).expect("scratch dir argument")); | ||
| 63 | + let stream = dir.join("idr.hevc"); | ||
| 64 | + let reference = dir.join("ref.yuv"); | ||
| 65 | + let status = Command::new("ffmpeg") | ||
| 66 | + .args(["-y", "-loglevel", "error", "-f", "lavfi", "-i"]) | ||
| 67 | + .arg("testsrc2=size=320x240:rate=30") | ||
| 68 | + .args([ | ||
| 69 | + "-frames:v", | ||
| 70 | + "1", | ||
| 71 | + "-c:v", | ||
| 72 | + "libx265", | ||
| 73 | + "-x265-params", | ||
| 74 | + "log-level=error", | ||
| 75 | + ]) | ||
| 76 | + .args(["-f", "hevc"]) | ||
| 77 | + .arg(&stream) | ||
| 78 | + .status() | ||
| 79 | + .expect("run ffmpeg"); | ||
| 80 | + assert!(status.success()); | ||
| 81 | + let status = Command::new("ffmpeg") | ||
| 82 | + .args(["-y", "-loglevel", "error", "-i"]) | ||
| 83 | + .arg(&stream) | ||
| 84 | + .args(["-f", "rawvideo", "-pix_fmt", "yuv420p"]) | ||
| 85 | + .arg(&reference) | ||
| 86 | + .status() | ||
| 87 | + .expect("run ffmpeg"); | ||
| 88 | + assert!(status.success()); | ||
| 89 | + | ||
| 90 | + let data = fs::read(&stream).expect("read stream"); | ||
| 91 | + let nals = split_annex_b(&data); | ||
| 92 | + let mut vps_list: Vec<Vps> = Vec::new(); | ||
| 93 | + let mut sps_list: Vec<Sps> = Vec::new(); | ||
| 94 | + let mut pps_list: Vec<Pps> = Vec::new(); | ||
| 95 | + let mut slice_nals: Vec<Vec<u8>> = Vec::new(); | ||
| 96 | + for nal in &nals { | ||
| 97 | + match nal.header.nal_type { | ||
| 98 | + NalUnitType::Vps => vps_list.push(Vps::parse(&nal.rbsp()).expect("VPS")), | ||
| 99 | + NalUnitType::Sps => sps_list.push(Sps::parse(&nal.rbsp()).expect("SPS")), | ||
| 100 | + NalUnitType::Pps => pps_list.push(Pps::parse(&nal.rbsp()).expect("PPS")), | ||
| 101 | + t if t.is_vcl() => { | ||
| 102 | + let mut raw = nal.header.to_bytes().to_vec(); | ||
| 103 | + raw.extend_from_slice(nal.payload); | ||
| 104 | + slice_nals.push(raw); | ||
| 105 | + } | ||
| 106 | + _ => {} | ||
| 107 | + } | ||
| 108 | + } | ||
| 109 | + let first = nals | ||
| 110 | + .iter() | ||
| 111 | + .find(|n| n.header.nal_type.is_vcl()) | ||
| 112 | + .expect("slice"); | ||
| 113 | + let vps = &vps_list[0]; | ||
| 114 | + let pps = &pps_list[0]; | ||
| 115 | + let sps = sps_list | ||
| 116 | + .iter() | ||
| 117 | + .find(|s| s.id == pps.sps_id) | ||
| 118 | + .expect("SPS for PPS"); | ||
| 119 | + let (sh, pos) = | ||
| 120 | + SliceHeader::parse(&first.rbsp(), sps, pps, first.header.nal_type).expect("slice header"); | ||
| 121 | + | ||
| 122 | + let h265_vps = H265Vps { | ||
| 123 | + id: vps.id, | ||
| 124 | + max_sub_layers_minus1: 0, | ||
| 125 | + profile_tier_level: ptl(&vps.ptl), | ||
| 126 | + dec_pic_buf_mgr: dpbm( | ||
| 127 | + 0, | ||
| 128 | + vps.max_dec_pic_buffering_minus1, | ||
| 129 | + vps.max_num_reorder_pics, | ||
| 130 | + ), | ||
| 131 | + }; | ||
| 132 | + let c = &sps.conf_win; | ||
| 133 | + let h265_sps = H265Sps { | ||
| 134 | + id: sps.id as u8, | ||
| 135 | + vps_id: sps.vps_id, | ||
| 136 | + max_sub_layers_minus1: 0, | ||
| 137 | + profile_tier_level: ptl(&sps.ptl), | ||
| 138 | + chroma_format_idc: sps.chroma_format_idc as u8, | ||
| 139 | + pic_width_in_luma_samples: sps.pic_width_in_luma_samples, | ||
| 140 | + pic_height_in_luma_samples: sps.pic_height_in_luma_samples, | ||
| 141 | + conformance_window: [c.left, c.right, c.top, c.bottom], | ||
| 142 | + bit_depth_luma_minus8: sps.bit_depth_luma_minus8 as u8, | ||
| 143 | + bit_depth_chroma_minus8: sps.bit_depth_chroma_minus8 as u8, | ||
| 144 | + log2_max_pic_order_cnt_lsb_minus4: sps.log2_max_poc_lsb_minus4 as u8, | ||
| 145 | + log2_min_luma_coding_block_size_minus3: sps.log2_min_cb_size_minus3 as u8, | ||
| 146 | + log2_diff_max_min_luma_coding_block_size: sps.log2_diff_max_min_cb_size as u8, | ||
| 147 | + log2_min_luma_transform_block_size_minus2: sps.log2_min_tb_size_minus2 as u8, | ||
| 148 | + log2_diff_max_min_luma_transform_block_size: sps.log2_diff_max_min_tb_size as u8, | ||
| 149 | + max_transform_hierarchy_depth_inter: sps.max_transform_hierarchy_depth_inter as u8, | ||
| 150 | + max_transform_hierarchy_depth_intra: sps.max_transform_hierarchy_depth_intra as u8, | ||
| 151 | + pcm: sps.pcm.map(|(a, b, c, d, e)| H265Pcm { | ||
| 152 | + bit_depth_luma_minus1: a as u8, | ||
| 153 | + bit_depth_chroma_minus1: b as u8, | ||
| 154 | + log2_min_luma_coding_block_size_minus3: c as u8, | ||
| 155 | + log2_diff_max_min_luma_coding_block_size: d as u8, | ||
| 156 | + loop_filter_disabled: e, | ||
| 157 | + }), | ||
| 158 | + dec_pic_buf_mgr: dpbm( | ||
| 159 | + sps.max_latency_increase_plus1, | ||
| 160 | + sps.max_dec_pic_buffering_minus1, | ||
| 161 | + sps.max_num_reorder_pics, | ||
| 162 | + ), | ||
| 163 | + scaling_list_enabled: sps.scaling_list_enabled, | ||
| 164 | + scaling_lists: sps.scaling_list.as_ref().map(lists), | ||
| 165 | + amp_enabled: sps.amp_enabled, | ||
| 166 | + sample_adaptive_offset_enabled: sps.sao_enabled, | ||
| 167 | + long_term_ref_pics_present: sps.long_term_ref_pics_present, | ||
| 168 | + long_term_ref_pics_sps: sps | ||
| 169 | + .long_term_ref_pics_sps | ||
| 170 | + .iter() | ||
| 171 | + .map(|&(poc_lsb, used_by_curr_pic)| H265LongTermRefPicSps { | ||
| 172 | + poc_lsb, | ||
| 173 | + used_by_curr_pic, | ||
| 174 | + }) | ||
| 175 | + .collect(), | ||
| 176 | + temporal_mvp_enabled: sps.temporal_mvp_enabled, | ||
| 177 | + strong_intra_smoothing_enabled: sps.strong_intra_smoothing, | ||
| 178 | + short_term_ref_pic_sets: sps.short_term_ref_pic_sets.iter().map(rps).collect(), | ||
| 179 | + }; | ||
| 180 | + let h265_pps = H265Pps { | ||
| 181 | + id: pps.id as u8, | ||
| 182 | + sps_id: pps.sps_id as u8, | ||
| 183 | + dependent_slice_segments_enabled: pps.dependent_slice_segments_enabled, | ||
| 184 | + output_flag_present: pps.output_flag_present, | ||
| 185 | + num_extra_slice_header_bits: pps.num_extra_slice_header_bits as u8, | ||
| 186 | + sign_data_hiding_enabled: pps.sign_data_hiding_enabled, | ||
| 187 | + cabac_init_present: pps.cabac_init_present, | ||
| 188 | + num_ref_idx_l0_default_active_minus1: pps.num_ref_idx_l0_default_active_minus1 as u8, | ||
| 189 | + num_ref_idx_l1_default_active_minus1: pps.num_ref_idx_l1_default_active_minus1 as u8, | ||
| 190 | + init_qp_minus26: pps.init_qp_minus26 as i8, | ||
| 191 | + constrained_intra_pred: pps.constrained_intra_pred, | ||
| 192 | + transform_skip_enabled: pps.transform_skip_enabled, | ||
| 193 | + cu_qp_delta_enabled: pps.cu_qp_delta_enabled, | ||
| 194 | + diff_cu_qp_delta_depth: pps.diff_cu_qp_delta_depth as u8, | ||
| 195 | + cb_qp_offset: pps.cb_qp_offset as i8, | ||
| 196 | + cr_qp_offset: pps.cr_qp_offset as i8, | ||
| 197 | + slice_chroma_qp_offsets_present: pps.slice_chroma_qp_offsets_present, | ||
| 198 | + weighted_pred: pps.weighted_pred, | ||
| 199 | + weighted_bipred: pps.weighted_bipred, | ||
| 200 | + transquant_bypass_enabled: pps.transquant_bypass_enabled, | ||
| 201 | + tiles_enabled: pps.tiles_enabled, | ||
| 202 | + entropy_coding_sync_enabled: pps.entropy_coding_sync_enabled, | ||
| 203 | + num_tile_columns_minus1: pps.num_tile_columns_minus1 as u8, | ||
| 204 | + num_tile_rows_minus1: pps.num_tile_rows_minus1 as u8, | ||
| 205 | + uniform_spacing: pps.uniform_spacing, | ||
| 206 | + column_width_minus1: pps.column_width_minus1.iter().map(|&w| w as u16).collect(), | ||
| 207 | + row_height_minus1: pps.row_height_minus1.iter().map(|&h| h as u16).collect(), | ||
| 208 | + loop_filter_across_tiles_enabled: pps.loop_filter_across_tiles_enabled, | ||
| 209 | + loop_filter_across_slices_enabled: pps.loop_filter_across_slices_enabled, | ||
| 210 | + deblocking_filter_control_present: pps.deblocking_filter_control_present, | ||
| 211 | + deblocking_filter_override_enabled: pps.deblocking_filter_override_enabled, | ||
| 212 | + deblocking_filter_disabled: pps.deblocking_filter_disabled, | ||
| 213 | + beta_offset_div2: pps.beta_offset_div2 as i8, | ||
| 214 | + tc_offset_div2: pps.tc_offset_div2 as i8, | ||
| 215 | + scaling_lists: pps.scaling_list.as_ref().map(lists), | ||
| 216 | + lists_modification_present: pps.lists_modification_present, | ||
| 217 | + log2_parallel_merge_level_minus2: pps.log2_parallel_merge_level_minus2 as u8, | ||
| 218 | + slice_segment_header_extension_present: pps.slice_segment_header_extension_present, | ||
| 219 | + }; | ||
| 220 | + | ||
| 221 | + let (w, h) = ( | ||
| 222 | + h265_sps.pic_width_in_luma_samples, | ||
| 223 | + h265_sps.pic_height_in_luma_samples, | ||
| 224 | + ); | ||
| 225 | + let gpu = Gpu::open().expect("open GPU"); | ||
| 226 | + println!("device: {}", gpu.device_name()); | ||
| 227 | + let cfg = H265DecodeConfig { | ||
| 228 | + max_width: w, | ||
| 229 | + max_height: h, | ||
| 230 | + max_dpb_slots: 2, | ||
| 231 | + }; | ||
| 232 | + let mut session = H265DecodeSession::new(&gpu, &cfg).expect("decode session"); | ||
| 233 | + session | ||
| 234 | + .set_parameters( | ||
| 235 | + std::slice::from_ref(&h265_vps), | ||
| 236 | + std::slice::from_ref(&h265_sps), | ||
| 237 | + std::slice::from_ref(&h265_pps), | ||
| 238 | + ) | ||
| 239 | + .expect("parameters"); | ||
| 240 | + let slices: Vec<&[u8]> = slice_nals.iter().map(|v| v.as_slice()).collect(); | ||
| 241 | + let pic = H265DecodePicture { | ||
| 242 | + vps_id: sps.vps_id, | ||
| 243 | + sps_id: sps.id as u8, | ||
| 244 | + pps_id: pps.id as u8, | ||
| 245 | + irap: first.header.nal_type.is_irap(), | ||
| 246 | + idr: first.header.nal_type.is_idr(), | ||
| 247 | + reference: true, | ||
| 248 | + poc: 0, | ||
| 249 | + setup_slot: 0, | ||
| 250 | + short_term_ref_pic_set_sps: sh.short_term_ref_pic_set_sps_flag, | ||
| 251 | + num_delta_pocs_of_ref_rps_idx: 0, | ||
| 252 | + st_rps_bits: pos.st_rps_bits, | ||
| 253 | + st_curr_before: &[], | ||
| 254 | + st_curr_after: &[], | ||
| 255 | + lt_curr: &[], | ||
| 256 | + refs: &[], | ||
| 257 | + slices: &slices, | ||
| 258 | + }; | ||
| 259 | + session.decode(&pic).expect("decode"); | ||
| 260 | + let (y, uv) = session.read_output(w, h).expect("read slot"); | ||
| 261 | + | ||
| 262 | + let ref_data = fs::read(&reference).expect("read reference"); | ||
| 263 | + let luma = (w * h) as usize; | ||
| 264 | + let chroma = luma / 4; | ||
| 265 | + let ref_y = &ref_data[..luma]; | ||
| 266 | + let ref_u = &ref_data[luma..luma + chroma]; | ||
| 267 | + let ref_v = &ref_data[luma + chroma..luma + 2 * chroma]; | ||
| 268 | + let mad = |a: &[u8], b: &[u8]| { | ||
| 269 | + a.iter() | ||
| 270 | + .zip(b) | ||
| 271 | + .map(|(&x, &y)| (x as f64 - y as f64).abs()) | ||
| 272 | + .sum::<f64>() | ||
| 273 | + / a.len() as f64 | ||
| 274 | + }; | ||
| 275 | + let ref_uv: Vec<u8> = ref_u | ||
| 276 | + .iter() | ||
| 277 | + .zip(ref_v) | ||
| 278 | + .flat_map(|(&u, &v)| [u, v]) | ||
| 279 | + .collect(); | ||
| 280 | + println!("mean abs diff Y: {:.6}", mad(&y, ref_y)); | ||
| 281 | + println!("mean abs diff UV: {:.6}", mad(&uv, &ref_uv)); | ||
| 282 | +} | ||
shims/vk-video-ffi/src/h264_decode.rsmodified+14 -14
| @@ -172,18 +172,18 @@ struct Caps { | |||
| 172 | } | 172 | } |
| 173 | 173 | ||
| 174 | #[derive(Default)] | 174 | #[derive(Default)] |
| 175 | -struct Image { | 175 | +pub(crate) struct Image { |
| 176 | - image: vk::Image, | 176 | + pub(crate) image: vk::Image, |
| 177 | - view: vk::ImageView, | 177 | + pub(crate) view: vk::ImageView, |
| 178 | - memory: vk::DeviceMemory, | 178 | + pub(crate) memory: vk::DeviceMemory, |
| 179 | } | 179 | } |
| 180 | 180 | ||
| 181 | #[derive(Default)] | 181 | #[derive(Default)] |
| 182 | -struct Buffer { | 182 | +pub(crate) struct Buffer { |
| 183 | - buffer: vk::Buffer, | 183 | + pub(crate) buffer: vk::Buffer, |
| 184 | - memory: vk::DeviceMemory, | 184 | + pub(crate) memory: vk::DeviceMemory, |
| 185 | - ptr: Option<*mut u8>, | 185 | + pub(crate) ptr: Option<*mut u8>, |
| 186 | - size: u64, | 186 | + pub(crate) size: u64, |
| 187 | } | 187 | } |
| 188 | 188 | ||
| 189 | struct Profile { | 189 | struct Profile { |
| @@ -798,7 +798,7 @@ fn query_caps(gpu: &Gpu, profile: &vk::VideoProfileInfoKHR) -> Result<Caps, Erro | |||
| 798 | }) | 798 | }) |
| 799 | } | 799 | } |
| 800 | 800 | ||
| 801 | -fn submit( | 801 | +pub(crate) fn submit( |
| 802 | d: &Device, | 802 | d: &Device, |
| 803 | fence: vk::Fence, | 803 | fence: vk::Fence, |
| 804 | queue: vk::Queue, | 804 | queue: vk::Queue, |
| @@ -817,7 +817,7 @@ fn submit( | |||
| 817 | Ok(()) | 817 | Ok(()) |
| 818 | } | 818 | } |
| 819 | 819 | ||
| 820 | -fn allocate( | 820 | +pub(crate) fn allocate( |
| 821 | gpu: &Gpu, | 821 | gpu: &Gpu, |
| 822 | req: vk::MemoryRequirements, | 822 | req: vk::MemoryRequirements, |
| 823 | flags: vk::MemoryPropertyFlags, | 823 | flags: vk::MemoryPropertyFlags, |
| @@ -835,7 +835,7 @@ fn allocate( | |||
| 835 | } | 835 | } |
| 836 | } | 836 | } |
| 837 | 837 | ||
| 838 | -fn new_image( | 838 | +pub(crate) fn new_image( |
| 839 | gpu: &Gpu, | 839 | gpu: &Gpu, |
| 840 | width: u32, | 840 | width: u32, |
| 841 | height: u32, | 841 | height: u32, |
| @@ -888,7 +888,7 @@ fn new_image( | |||
| 888 | }) | 888 | }) |
| 889 | } | 889 | } |
| 890 | 890 | ||
| 891 | -fn new_buffer( | 891 | +pub(crate) fn new_buffer( |
| 892 | gpu: &Gpu, | 892 | gpu: &Gpu, |
| 893 | size: u64, | 893 | size: u64, |
| 894 | usage: vk::BufferUsageFlags, | 894 | usage: vk::BufferUsageFlags, |
| @@ -922,7 +922,7 @@ fn new_buffer( | |||
| 922 | }) | 922 | }) |
| 923 | } | 923 | } |
| 924 | 924 | ||
| 925 | -fn align_u64(value: u64, to: u64) -> u64 { | 925 | +pub(crate) fn align_u64(value: u64, to: u64) -> u64 { |
| 926 | let to = to.max(1); | 926 | let to = to.max(1); |
| 927 | value.div_ceil(to) * to | 927 | value.div_ceil(to) * to |
| 928 | } | 928 | } |
shims/vk-video-ffi/src/h265_decode.rsadded+1297 -0
| @@ -0,0 +1,1297 @@ | |||
| 1 | +use std::ffi::c_void; | ||
| 2 | + | ||
| 3 | +use vulkanalia::prelude::v1_3::*; | ||
| 4 | +use vulkanalia::vk::{ | ||
| 5 | + KhrVideoDecodeQueueExtensionDeviceCommands, KhrVideoQueueExtensionDeviceCommands, | ||
| 6 | + KhrVideoQueueExtensionInstanceCommands, | ||
| 7 | +}; | ||
| 8 | + | ||
| 9 | +use crate::h264_decode::{Buffer, Image, align_u64, allocate, new_buffer, new_image, submit}; | ||
| 10 | +use crate::h264_encode::{ | ||
| 11 | + NV12, PLANE_0, PLANE_1, align, alloc_cb, barrier, copy_region, create_pool, picture_resource, | ||
| 12 | + vk, zeroed, | ||
| 13 | +}; | ||
| 14 | +use crate::{Error, Gpu}; | ||
| 15 | + | ||
| 16 | +/// Settings fixed for the life of a decode session. | ||
| 17 | +#[derive(Debug, Clone, Copy, PartialEq, Eq)] | ||
| 18 | +pub struct H265DecodeConfig { | ||
| 19 | + /// Largest coded width in luma samples. | ||
| 20 | + pub max_width: u32, | ||
| 21 | + /// Largest coded height in luma samples. | ||
| 22 | + pub max_height: u32, | ||
| 23 | + /// DPB slots, including the slot the picture is decoded into. | ||
| 24 | + pub max_dpb_slots: u32, | ||
| 25 | +} | ||
| 26 | + | ||
| 27 | +/// Profile, tier and level of a parameter set. | ||
| 28 | +#[derive(Debug, Clone, Copy, PartialEq, Eq)] | ||
| 29 | +pub struct H265ProfileTierLevel { | ||
| 30 | + /// general_tier_flag. | ||
| 31 | + pub tier_flag: bool, | ||
| 32 | + /// general_progressive_source_flag. | ||
| 33 | + pub progressive_source: bool, | ||
| 34 | + /// general_interlaced_source_flag. | ||
| 35 | + pub interlaced_source: bool, | ||
| 36 | + /// general_non_packed_constraint_flag. | ||
| 37 | + pub non_packed_constraint: bool, | ||
| 38 | + /// general_frame_only_constraint_flag. | ||
| 39 | + pub frame_only_constraint: bool, | ||
| 40 | + /// general_profile_idc. | ||
| 41 | + pub profile_idc: u8, | ||
| 42 | + /// general_level_idc, 30 x the level number. | ||
| 43 | + pub level_idc: u8, | ||
| 44 | +} | ||
| 45 | + | ||
| 46 | +/// Decoded picture buffer limits, one entry per sub-layer. | ||
| 47 | +#[derive(Debug, Clone, Copy, PartialEq, Eq)] | ||
| 48 | +pub struct H265DecPicBufMgr { | ||
| 49 | + /// sps_max_latency_increase_plus1. | ||
| 50 | + pub max_latency_increase_plus1: [u32; 7], | ||
| 51 | + /// sps_max_dec_pic_buffering_minus1. | ||
| 52 | + pub max_dec_pic_buffering_minus1: [u8; 7], | ||
| 53 | + /// sps_max_num_reorder_pics. | ||
| 54 | + pub max_num_reorder_pics: [u8; 7], | ||
| 55 | +} | ||
| 56 | + | ||
| 57 | +/// Video parameter set fields a decoder needs (names follow the spec syntax). | ||
| 58 | +#[derive(Debug, Clone, Copy, PartialEq, Eq)] | ||
| 59 | +pub struct H265Vps { | ||
| 60 | + /// vps_video_parameter_set_id. | ||
| 61 | + pub id: u8, | ||
| 62 | + /// vps_max_sub_layers_minus1. | ||
| 63 | + pub max_sub_layers_minus1: u8, | ||
| 64 | + /// Profile, tier and level of the base layer. | ||
| 65 | + pub profile_tier_level: H265ProfileTierLevel, | ||
| 66 | + /// Decoded picture buffer limits. | ||
| 67 | + pub dec_pic_buf_mgr: H265DecPicBufMgr, | ||
| 68 | +} | ||
| 69 | + | ||
| 70 | +/// Scaling lists in coded (up-right diagonal) order, fall-back rules already applied. | ||
| 71 | +#[derive(Debug, Clone, PartialEq, Eq)] | ||
| 72 | +pub struct H265ScalingLists { | ||
| 73 | + /// ScalingList4x4 for matrixId 0..6. | ||
| 74 | + pub list_4x4: [[u8; 16]; 6], | ||
| 75 | + /// ScalingList8x8 for matrixId 0..6. | ||
| 76 | + pub list_8x8: [[u8; 64]; 6], | ||
| 77 | + /// ScalingList16x16 for matrixId 0..6. | ||
| 78 | + pub list_16x16: [[u8; 64]; 6], | ||
| 79 | + /// ScalingList32x32 for matrixId 0 and 3. | ||
| 80 | + pub list_32x32: [[u8; 64]; 2], | ||
| 81 | + /// scaling_list_dc_coef_minus8 + 8 for 16x16. | ||
| 82 | + pub dc_16x16: [u8; 6], | ||
| 83 | + /// scaling_list_dc_coef_minus8 + 8 for 32x32. | ||
| 84 | + pub dc_32x32: [u8; 2], | ||
| 85 | +} | ||
| 86 | + | ||
| 87 | +/// PCM parameters, present when pcm_enabled_flag is set. | ||
| 88 | +#[derive(Debug, Clone, Copy, PartialEq, Eq)] | ||
| 89 | +pub struct H265Pcm { | ||
| 90 | + /// pcm_sample_bit_depth_luma_minus1. | ||
| 91 | + pub bit_depth_luma_minus1: u8, | ||
| 92 | + /// pcm_sample_bit_depth_chroma_minus1. | ||
| 93 | + pub bit_depth_chroma_minus1: u8, | ||
| 94 | + /// log2_min_pcm_luma_coding_block_size_minus3. | ||
| 95 | + pub log2_min_luma_coding_block_size_minus3: u8, | ||
| 96 | + /// log2_diff_max_min_pcm_luma_coding_block_size. | ||
| 97 | + pub log2_diff_max_min_luma_coding_block_size: u8, | ||
| 98 | + /// pcm_loop_filter_disabled_flag. | ||
| 99 | + pub loop_filter_disabled: bool, | ||
| 100 | +} | ||
| 101 | + | ||
| 102 | +/// One SPS long-term reference picture. | ||
| 103 | +#[derive(Debug, Clone, Copy, PartialEq, Eq)] | ||
| 104 | +pub struct H265LongTermRefPicSps { | ||
| 105 | + /// lt_ref_pic_poc_lsb_sps. | ||
| 106 | + pub poc_lsb: u32, | ||
| 107 | + /// used_by_curr_pic_lt_sps_flag. | ||
| 108 | + pub used_by_curr_pic: bool, | ||
| 109 | +} | ||
| 110 | + | ||
| 111 | +/// An explicit short-term reference picture set, resolved to POC deltas. | ||
| 112 | +#[derive(Debug, Clone, PartialEq, Eq)] | ||
| 113 | +pub struct H265ShortTermRefPicSet { | ||
| 114 | + /// DeltaPocS0, negative and decreasing. | ||
| 115 | + pub delta_poc_s0: Vec<i32>, | ||
| 116 | + /// UsedByCurrPicS0. | ||
| 117 | + pub used_by_curr_pic_s0: Vec<bool>, | ||
| 118 | + /// DeltaPocS1, positive and increasing. | ||
| 119 | + pub delta_poc_s1: Vec<i32>, | ||
| 120 | + /// UsedByCurrPicS1. | ||
| 121 | + pub used_by_curr_pic_s1: Vec<bool>, | ||
| 122 | +} | ||
| 123 | + | ||
| 124 | +/// Sequence parameter set fields a decoder needs (names follow the spec syntax). | ||
| 125 | +#[derive(Debug, Clone, PartialEq, Eq)] | ||
| 126 | +pub struct H265Sps { | ||
| 127 | + /// seq_parameter_set_id. | ||
| 128 | + pub id: u8, | ||
| 129 | + /// sps_video_parameter_set_id. | ||
| 130 | + pub vps_id: u8, | ||
| 131 | + /// sps_max_sub_layers_minus1. | ||
| 132 | + pub max_sub_layers_minus1: u8, | ||
| 133 | + /// Profile, tier and level of the base layer. | ||
| 134 | + pub profile_tier_level: H265ProfileTierLevel, | ||
| 135 | + /// chroma_format_idc, 1 for 4:2:0. | ||
| 136 | + pub chroma_format_idc: u8, | ||
| 137 | + /// pic_width_in_luma_samples. | ||
| 138 | + pub pic_width_in_luma_samples: u32, | ||
| 139 | + /// pic_height_in_luma_samples. | ||
| 140 | + pub pic_height_in_luma_samples: u32, | ||
| 141 | + /// Conformance window left, right, top and bottom offsets in chroma units. | ||
| 142 | + pub conformance_window: [u32; 4], | ||
| 143 | + /// bit_depth_luma_minus8. | ||
| 144 | + pub bit_depth_luma_minus8: u8, | ||
| 145 | + /// bit_depth_chroma_minus8. | ||
| 146 | + pub bit_depth_chroma_minus8: u8, | ||
| 147 | + /// log2_max_pic_order_cnt_lsb_minus4. | ||
| 148 | + pub log2_max_pic_order_cnt_lsb_minus4: u8, | ||
| 149 | + /// log2_min_luma_coding_block_size_minus3. | ||
| 150 | + pub log2_min_luma_coding_block_size_minus3: u8, | ||
| 151 | + /// log2_diff_max_min_luma_coding_block_size. | ||
| 152 | + pub log2_diff_max_min_luma_coding_block_size: u8, | ||
| 153 | + /// log2_min_luma_transform_block_size_minus2. | ||
| 154 | + pub log2_min_luma_transform_block_size_minus2: u8, | ||
| 155 | + /// log2_diff_max_min_luma_transform_block_size. | ||
| 156 | + pub log2_diff_max_min_luma_transform_block_size: u8, | ||
| 157 | + /// max_transform_hierarchy_depth_inter. | ||
| 158 | + pub max_transform_hierarchy_depth_inter: u8, | ||
| 159 | + /// max_transform_hierarchy_depth_intra. | ||
| 160 | + pub max_transform_hierarchy_depth_intra: u8, | ||
| 161 | + /// PCM parameters when pcm_enabled_flag is set. | ||
| 162 | + pub pcm: Option<H265Pcm>, | ||
| 163 | + /// Decoded picture buffer limits. | ||
| 164 | + pub dec_pic_buf_mgr: H265DecPicBufMgr, | ||
| 165 | + /// scaling_list_enabled_flag. | ||
| 166 | + pub scaling_list_enabled: bool, | ||
| 167 | + /// Explicit scaling lists when sps_scaling_list_data_present_flag is set. | ||
| 168 | + pub scaling_lists: Option<H265ScalingLists>, | ||
| 169 | + /// amp_enabled_flag. | ||
| 170 | + pub amp_enabled: bool, | ||
| 171 | + /// sample_adaptive_offset_enabled_flag. | ||
| 172 | + pub sample_adaptive_offset_enabled: bool, | ||
| 173 | + /// long_term_ref_pics_present_flag. | ||
| 174 | + pub long_term_ref_pics_present: bool, | ||
| 175 | + /// SPS long-term reference pictures. | ||
| 176 | + pub long_term_ref_pics_sps: Vec<H265LongTermRefPicSps>, | ||
| 177 | + /// sps_temporal_mvp_enabled_flag. | ||
| 178 | + pub temporal_mvp_enabled: bool, | ||
| 179 | + /// strong_intra_smoothing_enabled_flag. | ||
| 180 | + pub strong_intra_smoothing_enabled: bool, | ||
| 181 | + /// Short-term reference picture sets in the SPS. | ||
| 182 | + pub short_term_ref_pic_sets: Vec<H265ShortTermRefPicSet>, | ||
| 183 | +} | ||
| 184 | + | ||
| 185 | +/// Picture parameter set fields a decoder needs (names follow the spec syntax). | ||
| 186 | +#[derive(Debug, Clone, PartialEq, Eq)] | ||
| 187 | +pub struct H265Pps { | ||
| 188 | + /// pic_parameter_set_id. | ||
| 189 | + pub id: u8, | ||
| 190 | + /// seq_parameter_set_id. | ||
| 191 | + pub sps_id: u8, | ||
| 192 | + /// dependent_slice_segments_enabled_flag. | ||
| 193 | + pub dependent_slice_segments_enabled: bool, | ||
| 194 | + /// output_flag_present_flag. | ||
| 195 | + pub output_flag_present: bool, | ||
| 196 | + /// num_extra_slice_header_bits. | ||
| 197 | + pub num_extra_slice_header_bits: u8, | ||
| 198 | + /// sign_data_hiding_enabled_flag. | ||
| 199 | + pub sign_data_hiding_enabled: bool, | ||
| 200 | + /// cabac_init_present_flag. | ||
| 201 | + pub cabac_init_present: bool, | ||
| 202 | + /// num_ref_idx_l0_default_active_minus1. | ||
| 203 | + pub num_ref_idx_l0_default_active_minus1: u8, | ||
| 204 | + /// num_ref_idx_l1_default_active_minus1. | ||
| 205 | + pub num_ref_idx_l1_default_active_minus1: u8, | ||
| 206 | + /// init_qp_minus26. | ||
| 207 | + pub init_qp_minus26: i8, | ||
| 208 | + /// constrained_intra_pred_flag. | ||
| 209 | + pub constrained_intra_pred: bool, | ||
| 210 | + /// transform_skip_enabled_flag. | ||
| 211 | + pub transform_skip_enabled: bool, | ||
| 212 | + /// cu_qp_delta_enabled_flag. | ||
| 213 | + pub cu_qp_delta_enabled: bool, | ||
| 214 | + /// diff_cu_qp_delta_depth. | ||
| 215 | + pub diff_cu_qp_delta_depth: u8, | ||
| 216 | + /// pps_cb_qp_offset. | ||
| 217 | + pub cb_qp_offset: i8, | ||
| 218 | + /// pps_cr_qp_offset. | ||
| 219 | + pub cr_qp_offset: i8, | ||
| 220 | + /// pps_slice_chroma_qp_offsets_present_flag. | ||
| 221 | + pub slice_chroma_qp_offsets_present: bool, | ||
| 222 | + /// weighted_pred_flag. | ||
| 223 | + pub weighted_pred: bool, | ||
| 224 | + /// weighted_bipred_flag. | ||
| 225 | + pub weighted_bipred: bool, | ||
| 226 | + /// transquant_bypass_enabled_flag. | ||
| 227 | + pub transquant_bypass_enabled: bool, | ||
| 228 | + /// tiles_enabled_flag. | ||
| 229 | + pub tiles_enabled: bool, | ||
| 230 | + /// entropy_coding_sync_enabled_flag. | ||
| 231 | + pub entropy_coding_sync_enabled: bool, | ||
| 232 | + /// num_tile_columns_minus1. | ||
| 233 | + pub num_tile_columns_minus1: u8, | ||
| 234 | + /// num_tile_rows_minus1. | ||
| 235 | + pub num_tile_rows_minus1: u8, | ||
| 236 | + /// uniform_spacing_flag. | ||
| 237 | + pub uniform_spacing: bool, | ||
| 238 | + /// column_width_minus1 when the spacing is not uniform. | ||
| 239 | + pub column_width_minus1: Vec<u16>, | ||
| 240 | + /// row_height_minus1 when the spacing is not uniform. | ||
| 241 | + pub row_height_minus1: Vec<u16>, | ||
| 242 | + /// loop_filter_across_tiles_enabled_flag. | ||
| 243 | + pub loop_filter_across_tiles_enabled: bool, | ||
| 244 | + /// pps_loop_filter_across_slices_enabled_flag. | ||
| 245 | + pub loop_filter_across_slices_enabled: bool, | ||
| 246 | + /// deblocking_filter_control_present_flag. | ||
| 247 | + pub deblocking_filter_control_present: bool, | ||
| 248 | + /// deblocking_filter_override_enabled_flag. | ||
| 249 | + pub deblocking_filter_override_enabled: bool, | ||
| 250 | + /// pps_deblocking_filter_disabled_flag. | ||
| 251 | + pub deblocking_filter_disabled: bool, | ||
| 252 | + /// pps_beta_offset_div2. | ||
| 253 | + pub beta_offset_div2: i8, | ||
| 254 | + /// pps_tc_offset_div2. | ||
| 255 | + pub tc_offset_div2: i8, | ||
| 256 | + /// Explicit scaling lists when pps_scaling_list_data_present_flag is set. | ||
| 257 | + pub scaling_lists: Option<H265ScalingLists>, | ||
| 258 | + /// lists_modification_present_flag. | ||
| 259 | + pub lists_modification_present: bool, | ||
| 260 | + /// log2_parallel_merge_level_minus2. | ||
| 261 | + pub log2_parallel_merge_level_minus2: u8, | ||
| 262 | + /// slice_segment_header_extension_present_flag. | ||
| 263 | + pub slice_segment_header_extension_present: bool, | ||
| 264 | +} | ||
| 265 | + | ||
| 266 | +/// A picture held in a DPB slot, used as reference. | ||
| 267 | +#[derive(Debug, Clone, Copy, PartialEq, Eq)] | ||
| 268 | +pub struct H265RefSlot { | ||
| 269 | + /// DPB slot index. | ||
| 270 | + pub slot: u32, | ||
| 271 | + /// PicOrderCntVal of the reference. | ||
| 272 | + pub poc: i32, | ||
| 273 | + /// Marked as long-term reference. | ||
| 274 | + pub long_term: bool, | ||
| 275 | +} | ||
| 276 | + | ||
| 277 | +/// One coded H.265 picture to decode. Slices are complete NAL units with their two-byte header, no start code. | ||
| 278 | +#[derive(Debug, Clone, Copy)] | ||
| 279 | +pub struct H265DecodePicture<'a> { | ||
| 280 | + /// video_parameter_set_id of the active VPS. | ||
| 281 | + pub vps_id: u8, | ||
| 282 | + /// seq_parameter_set_id of the active SPS. | ||
| 283 | + pub sps_id: u8, | ||
| 284 | + /// pic_parameter_set_id of the active PPS. | ||
| 285 | + pub pps_id: u8, | ||
| 286 | + /// IRAP picture. | ||
| 287 | + pub irap: bool, | ||
| 288 | + /// IDR picture. | ||
| 289 | + pub idr: bool, | ||
| 290 | + /// Reference picture. | ||
| 291 | + pub reference: bool, | ||
| 292 | + /// PicOrderCntVal. | ||
| 293 | + pub poc: i32, | ||
| 294 | + /// DPB slot the picture is decoded into. | ||
| 295 | + pub setup_slot: u32, | ||
| 296 | + /// short_term_ref_pic_set_sps_flag of the slice header. | ||
| 297 | + pub short_term_ref_pic_set_sps: bool, | ||
| 298 | + /// NumDeltaPocsOfRefRpsIdx, 0 unless the slice header set is inter predicted. | ||
| 299 | + pub num_delta_pocs_of_ref_rps_idx: u8, | ||
| 300 | + /// NumBitsForSTRefPicSetInSlice, 0 when the set comes from the SPS. | ||
| 301 | + pub st_rps_bits: u32, | ||
| 302 | + /// DPB slots of RefPicSetStCurrBefore. | ||
| 303 | + pub st_curr_before: &'a [u32], | ||
| 304 | + /// DPB slots of RefPicSetStCurrAfter. | ||
| 305 | + pub st_curr_after: &'a [u32], | ||
| 306 | + /// DPB slots of RefPicSetLtCurr. | ||
| 307 | + pub lt_curr: &'a [u32], | ||
| 308 | + /// Every DPB picture in use as a reference, all in slots other than setup_slot. | ||
| 309 | + pub refs: &'a [H265RefSlot], | ||
| 310 | + /// Slice segment NAL units in decoding order. | ||
| 311 | + pub slices: &'a [&'a [u8]], | ||
| 312 | +} | ||
| 313 | + | ||
| 314 | +struct Caps { | ||
| 315 | + std_header_version: vk::ExtensionProperties, | ||
| 316 | + max_extent: vk::Extent2D, | ||
| 317 | + size_align: u64, | ||
| 318 | + max_dpb_slots: u32, | ||
| 319 | + max_active_refs: u32, | ||
| 320 | + distinct: bool, | ||
| 321 | +} | ||
| 322 | + | ||
| 323 | +struct Profile { | ||
| 324 | + info: vk::VideoProfileInfoKHR, | ||
| 325 | + h265: vk::VideoDecodeH265ProfileInfoKHR, | ||
| 326 | +} | ||
| 327 | + | ||
| 328 | +impl Profile { | ||
| 329 | + fn new() -> Box<Self> { | ||
| 330 | + let mut p = Box::new(Profile { | ||
| 331 | + info: vk::VideoProfileInfoKHR::builder() | ||
| 332 | + .video_codec_operation(vk::VideoCodecOperationFlagsKHR::DECODE_H265) | ||
| 333 | + .chroma_subsampling(vk::VideoChromaSubsamplingFlagsKHR::_420) | ||
| 334 | + .luma_bit_depth(vk::VideoComponentBitDepthFlagsKHR::_8) | ||
| 335 | + .chroma_bit_depth(vk::VideoComponentBitDepthFlagsKHR::_8) | ||
| 336 | + .build(), | ||
| 337 | + h265: vk::VideoDecodeH265ProfileInfoKHR::builder() | ||
| 338 | + .std_profile_idc(vk::video::STD_VIDEO_H265_PROFILE_IDC_MAIN) | ||
| 339 | + .build(), | ||
| 340 | + }); | ||
| 341 | + p.info.next = &p.h265 as *const _ as *const c_void; | ||
| 342 | + p | ||
| 343 | + } | ||
| 344 | +} | ||
| 345 | + | ||
| 346 | +/// An H.265 Main Vulkan Video decode session: NV12 DPB images per slot and a separate NV12 output image. | ||
| 347 | +pub struct H265DecodeSession { | ||
| 348 | + gpu: Gpu, | ||
| 349 | + family: u32, | ||
| 350 | + queue: vk::Queue, | ||
| 351 | + graphics: u32, | ||
| 352 | + graphics_queue: vk::Queue, | ||
| 353 | + video_session: vk::VideoSessionKHR, | ||
| 354 | + session_memories: Vec<vk::DeviceMemory>, | ||
| 355 | + params: Option<vk::VideoSessionParametersKHR>, | ||
| 356 | + sps: Vec<H265Sps>, | ||
| 357 | + max_extent: vk::Extent2D, | ||
| 358 | + max_dpb_slots: u32, | ||
| 359 | + max_active_refs: u32, | ||
| 360 | + size_align: u64, | ||
| 361 | + dpb: Vec<Image>, | ||
| 362 | + output: Image, | ||
| 363 | + bitstream: Buffer, | ||
| 364 | + readback: Buffer, | ||
| 365 | + decode_pool: vk::CommandPool, | ||
| 366 | + decode_cb: vk::CommandBuffer, | ||
| 367 | + graphics_pool: vk::CommandPool, | ||
| 368 | + graphics_cb: vk::CommandBuffer, | ||
| 369 | + fence: vk::Fence, | ||
| 370 | + coded: bool, | ||
| 371 | +} | ||
| 372 | + | ||
| 373 | +impl H265DecodeSession { | ||
| 374 | + /// Creates a session with its DPB, bitstream and readback buffers. | ||
| 375 | + pub fn new(gpu: &Gpu, cfg: &H265DecodeConfig) -> Result<Self, Error> { | ||
| 376 | + if cfg.max_width == 0 || cfg.max_height == 0 || cfg.max_dpb_slots == 0 { | ||
| 377 | + return Err(Error::Invalid( | ||
| 378 | + "decode size or DPB slot count is zero".into(), | ||
| 379 | + )); | ||
| 380 | + } | ||
| 381 | + let family = gpu | ||
| 382 | + .decode_family() | ||
| 383 | + .ok_or_else(|| Error::Unsupported("H.265 decode queue family".into()))?; | ||
| 384 | + let graphics = gpu | ||
| 385 | + .graphics_family() | ||
| 386 | + .ok_or_else(|| Error::Unsupported("graphics queue family for readback".into()))?; | ||
| 387 | + if !gpu | ||
| 388 | + .device() | ||
| 389 | + .extensions() | ||
| 390 | + .contains(&vk::KHR_VIDEO_DECODE_H265_EXTENSION.name) | ||
| 391 | + { | ||
| 392 | + return Err(Error::Unsupported("H.265 decode".into())); | ||
| 393 | + } | ||
| 394 | + let width = align(cfg.max_width, 16); | ||
| 395 | + let height = align(cfg.max_height, 16); | ||
| 396 | + let profile = Profile::new(); | ||
| 397 | + let caps = query_caps(gpu, &profile.info)?; | ||
| 398 | + if width > caps.max_extent.width | ||
| 399 | + || height > caps.max_extent.height | ||
| 400 | + || cfg.max_dpb_slots > caps.max_dpb_slots | ||
| 401 | + { | ||
| 402 | + return Err(Error::Unsupported(format!("H.265 decode {width}x{height}"))); | ||
| 403 | + } | ||
| 404 | + if !caps.distinct { | ||
| 405 | + return Err(Error::Unsupported( | ||
| 406 | + "H.265 decode with DPB and output in one image".into(), | ||
| 407 | + )); | ||
| 408 | + } | ||
| 409 | + let max_active = caps.max_active_refs.min(cfg.max_dpb_slots); | ||
| 410 | + | ||
| 411 | + let queue = unsafe { gpu.device().get_device_queue(family, 0) }; | ||
| 412 | + let graphics_queue = unsafe { gpu.device().get_device_queue(graphics, 0) }; | ||
| 413 | + let mut s = H265DecodeSession { | ||
| 414 | + gpu: gpu.clone(), | ||
| 415 | + family, | ||
| 416 | + queue, | ||
| 417 | + graphics, | ||
| 418 | + graphics_queue, | ||
| 419 | + video_session: Default::default(), | ||
| 420 | + session_memories: Vec::new(), | ||
| 421 | + params: None, | ||
| 422 | + sps: Vec::new(), | ||
| 423 | + max_extent: vk::Extent2D { width, height }, | ||
| 424 | + max_dpb_slots: cfg.max_dpb_slots, | ||
| 425 | + max_active_refs: max_active, | ||
| 426 | + size_align: caps.size_align, | ||
| 427 | + dpb: Vec::new(), | ||
| 428 | + output: Default::default(), | ||
| 429 | + bitstream: Default::default(), | ||
| 430 | + readback: Default::default(), | ||
| 431 | + decode_pool: Default::default(), | ||
| 432 | + decode_cb: Default::default(), | ||
| 433 | + graphics_pool: Default::default(), | ||
| 434 | + graphics_cb: Default::default(), | ||
| 435 | + fence: Default::default(), | ||
| 436 | + coded: false, | ||
| 437 | + }; | ||
| 438 | + s.create_session(&profile, &caps, max_active)?; | ||
| 439 | + s.create_resources(&profile, &caps)?; | ||
| 440 | + s.init_dpb()?; | ||
| 441 | + Ok(s) | ||
| 442 | + } | ||
| 443 | + | ||
| 444 | + /// Replaces the session parameters with the given VPS, SPS and PPS sets. | ||
| 445 | + pub fn set_parameters( | ||
| 446 | + &mut self, | ||
| 447 | + vps: &[H265Vps], | ||
| 448 | + sps: &[H265Sps], | ||
| 449 | + pps: &[H265Pps], | ||
| 450 | + ) -> Result<(), Error> { | ||
| 451 | + if sps.iter().any(|s| { | ||
| 452 | + s.chroma_format_idc != 1 | ||
| 453 | + || s.bit_depth_luma_minus8 != 0 | ||
| 454 | + || s.bit_depth_chroma_minus8 != 0 | ||
| 455 | + }) { | ||
| 456 | + return Err(Error::Unsupported("H.265 decode beyond 8-bit 4:2:0".into())); | ||
| 457 | + } | ||
| 458 | + let gpu = self.gpu.clone(); | ||
| 459 | + let d = gpu.device(); | ||
| 460 | + | ||
| 461 | + let vps_ptl = vps | ||
| 462 | + .iter() | ||
| 463 | + .map(|v| std_ptl(&v.profile_tier_level).map(Box::new)) | ||
| 464 | + .collect::<Result<Vec<_>, Error>>()?; | ||
| 465 | + let vps_dpbm: Vec<_> = vps | ||
| 466 | + .iter() | ||
| 467 | + .map(|v| Box::new(std_dpbm(&v.dec_pic_buf_mgr))) | ||
| 468 | + .collect(); | ||
| 469 | + let std_vps: Vec<vk::video::StdVideoH265VideoParameterSet> = vps | ||
| 470 | + .iter() | ||
| 471 | + .zip(&vps_ptl) | ||
| 472 | + .zip(&vps_dpbm) | ||
| 473 | + .map(|((v, ptl), dpbm)| std_vps(v, ptl, dpbm)) | ||
| 474 | + .collect(); | ||
| 475 | + | ||
| 476 | + let sps_ptl = sps | ||
| 477 | + .iter() | ||
| 478 | + .map(|s| std_ptl(&s.profile_tier_level).map(Box::new)) | ||
| 479 | + .collect::<Result<Vec<_>, Error>>()?; | ||
| 480 | + let sps_dpbm: Vec<_> = sps | ||
| 481 | + .iter() | ||
| 482 | + .map(|s| Box::new(std_dpbm(&s.dec_pic_buf_mgr))) | ||
| 483 | + .collect(); | ||
| 484 | + let sps_lists: Vec<_> = sps | ||
| 485 | + .iter() | ||
| 486 | + .map(|s| s.scaling_lists.as_ref().map(|l| Box::new(std_lists(l)))) | ||
| 487 | + .collect(); | ||
| 488 | + let sps_rps = sps | ||
| 489 | + .iter() | ||
| 490 | + .map(|s| { | ||
| 491 | + s.short_term_ref_pic_sets | ||
| 492 | + .iter() | ||
| 493 | + .map(std_rps) | ||
| 494 | + .collect::<Result<Vec<_>, Error>>() | ||
| 495 | + }) | ||
| 496 | + .collect::<Result<Vec<_>, Error>>()?; | ||
| 497 | + let sps_lt = sps | ||
| 498 | + .iter() | ||
| 499 | + .map(|s| std_lt(&s.long_term_ref_pics_sps).map(Box::new)) | ||
| 500 | + .collect::<Result<Vec<_>, Error>>()?; | ||
| 501 | + let std_sps = sps | ||
| 502 | + .iter() | ||
| 503 | + .enumerate() | ||
| 504 | + .map(|(i, s)| { | ||
| 505 | + std_sps( | ||
| 506 | + s, | ||
| 507 | + &sps_ptl[i], | ||
| 508 | + &sps_dpbm[i], | ||
| 509 | + sps_lists[i].as_deref(), | ||
| 510 | + &sps_rps[i], | ||
| 511 | + &sps_lt[i], | ||
| 512 | + ) | ||
| 513 | + }) | ||
| 514 | + .collect::<Result<Vec<_>, Error>>()?; | ||
| 515 | + | ||
| 516 | + let pps_lists: Vec<_> = pps | ||
| 517 | + .iter() | ||
| 518 | + .map(|p| p.scaling_lists.as_ref().map(|l| Box::new(std_lists(l)))) | ||
| 519 | + .collect(); | ||
| 520 | + let std_pps = pps | ||
| 521 | + .iter() | ||
| 522 | + .zip(&pps_lists) | ||
| 523 | + .map(|(p, lists)| { | ||
| 524 | + let vps_id = sps | ||
| 525 | + .iter() | ||
| 526 | + .find(|s| s.id == p.sps_id) | ||
| 527 | + .map(|s| s.vps_id) | ||
| 528 | + .ok_or_else(|| Error::Invalid(format!("PPS names unknown SPS {}", p.sps_id)))?; | ||
| 529 | + std_pps(p, vps_id, lists.as_deref()) | ||
| 530 | + }) | ||
| 531 | + .collect::<Result<Vec<_>, Error>>()?; | ||
| 532 | + | ||
| 533 | + let add = vk::VideoDecodeH265SessionParametersAddInfoKHR::builder() | ||
| 534 | + .std_vp_ss(&std_vps) | ||
| 535 | + .std_sp_ss(&std_sps) | ||
| 536 | + .std_pp_ss(&std_pps) | ||
| 537 | + .build(); | ||
| 538 | + let mut h265 = vk::VideoDecodeH265SessionParametersCreateInfoKHR::builder() | ||
| 539 | + .max_std_vps_count(vps.len().max(1) as u32) | ||
| 540 | + .max_std_sps_count(sps.len().max(1) as u32) | ||
| 541 | + .max_std_pps_count(pps.len().max(1) as u32) | ||
| 542 | + .parameters_add_info(&add) | ||
| 543 | + .build(); | ||
| 544 | + let info = vk::VideoSessionParametersCreateInfoKHR::builder() | ||
| 545 | + .video_session(self.video_session) | ||
| 546 | + .push_next(&mut h265) | ||
| 547 | + .build(); | ||
| 548 | + let params = unsafe { | ||
| 549 | + vk( | ||
| 550 | + d.create_video_session_parameters_khr(&info, None), | ||
| 551 | + "vkCreateVideoSessionParametersKHR", | ||
| 552 | + )? | ||
| 553 | + }; | ||
| 554 | + if let Some(old) = self.params.replace(params) { | ||
| 555 | + unsafe { d.destroy_video_session_parameters_khr(old, None) }; | ||
| 556 | + } | ||
| 557 | + self.sps = sps.to_vec(); | ||
| 558 | + Ok(()) | ||
| 559 | + } | ||
| 560 | + | ||
| 561 | + /// Decodes one picture into its setup slot, referencing the given DPB slots. | ||
| 562 | + pub fn decode(&mut self, pic: &H265DecodePicture) -> Result<(), Error> { | ||
| 563 | + let params = self | ||
| 564 | + .params | ||
| 565 | + .ok_or_else(|| Error::Invalid("session parameters not set".into()))?; | ||
| 566 | + let extent = { | ||
| 567 | + let sps = self | ||
| 568 | + .sps | ||
| 569 | + .iter() | ||
| 570 | + .find(|s| s.id == pic.sps_id) | ||
| 571 | + .ok_or_else(|| Error::Invalid(format!("unknown SPS {}", pic.sps_id)))?; | ||
| 572 | + vk::Extent2D { | ||
| 573 | + width: sps.pic_width_in_luma_samples, | ||
| 574 | + height: sps.pic_height_in_luma_samples, | ||
| 575 | + } | ||
| 576 | + }; | ||
| 577 | + if extent.width > self.max_extent.width || extent.height > self.max_extent.height { | ||
| 578 | + return Err(Error::Invalid("picture larger than session".into())); | ||
| 579 | + } | ||
| 580 | + if pic.setup_slot >= self.max_dpb_slots { | ||
| 581 | + return Err(Error::Invalid("setup DPB slot".into())); | ||
| 582 | + } | ||
| 583 | + if pic.refs.len() > self.max_active_refs as usize { | ||
| 584 | + return Err(Error::Invalid("too many reference pictures".into())); | ||
| 585 | + } | ||
| 586 | + if pic | ||
| 587 | + .refs | ||
| 588 | + .iter() | ||
| 589 | + .any(|r| r.slot >= self.max_dpb_slots || r.slot == pic.setup_slot) | ||
| 590 | + { | ||
| 591 | + return Err(Error::Invalid("reference DPB slot".into())); | ||
| 592 | + } | ||
| 593 | + for list in [pic.st_curr_before, pic.st_curr_after, pic.lt_curr] { | ||
| 594 | + if list.len() > 8 { | ||
| 595 | + return Err(Error::Invalid("too many RPS entries".into())); | ||
| 596 | + } | ||
| 597 | + if list.iter().any(|s| !pic.refs.iter().any(|r| r.slot == *s)) { | ||
| 598 | + return Err(Error::Invalid("RPS entry is not a listed reference".into())); | ||
| 599 | + } | ||
| 600 | + } | ||
| 601 | + if pic.slices.is_empty() { | ||
| 602 | + return Err(Error::Invalid("no slices".into())); | ||
| 603 | + } | ||
| 604 | + | ||
| 605 | + let total: usize = pic.slices.iter().map(|s| 3 + s.len()).sum(); | ||
| 606 | + if total as u64 > self.bitstream.size { | ||
| 607 | + return Err(Error::Invalid("slices larger than bitstream buffer".into())); | ||
| 608 | + } | ||
| 609 | + let ptr = self | ||
| 610 | + .bitstream | ||
| 611 | + .ptr | ||
| 612 | + .ok_or_else(|| Error::Invalid("bitstream buffer not mapped".into()))?; | ||
| 613 | + let buf = unsafe { std::slice::from_raw_parts_mut(ptr, self.bitstream.size as usize) }; | ||
| 614 | + let mut offsets = Vec::with_capacity(pic.slices.len()); | ||
| 615 | + let mut at = 0usize; | ||
| 616 | + for slice in pic.slices { | ||
| 617 | + offsets.push(at as u32); | ||
| 618 | + buf[at..at + 3].copy_from_slice(&[0, 0, 1]); | ||
| 619 | + buf[at + 3..at + 3 + slice.len()].copy_from_slice(slice); | ||
| 620 | + at += 3 + slice.len(); | ||
| 621 | + } | ||
| 622 | + let range = align_u64(at as u64, self.size_align); | ||
| 623 | + | ||
| 624 | + let gpu = self.gpu.clone(); | ||
| 625 | + let d = gpu.device(); | ||
| 626 | + let cb = self.decode_cb; | ||
| 627 | + let pr: Vec<vk::VideoPictureResourceInfoKHR> = self | ||
| 628 | + .dpb | ||
| 629 | + .iter() | ||
| 630 | + .map(|i| picture_resource(i.view, extent)) | ||
| 631 | + .collect(); | ||
| 632 | + | ||
| 633 | + let setup_std = ref_info(pic.poc, false); | ||
| 634 | + let mut setup_dpb = vk::VideoDecodeH265DpbSlotInfoKHR::builder() | ||
| 635 | + .std_reference_info(&setup_std) | ||
| 636 | + .build(); | ||
| 637 | + let setup = vk::VideoReferenceSlotInfoKHR::builder() | ||
| 638 | + .slot_index(pic.setup_slot as i32) | ||
| 639 | + .picture_resource(&pr[pic.setup_slot as usize]) | ||
| 640 | + .push_next(&mut setup_dpb) | ||
| 641 | + .build(); | ||
| 642 | + | ||
| 643 | + let ref_std: Vec<vk::video::StdVideoDecodeH265ReferenceInfo> = pic | ||
| 644 | + .refs | ||
| 645 | + .iter() | ||
| 646 | + .map(|r| ref_info(r.poc, r.long_term)) | ||
| 647 | + .collect(); | ||
| 648 | + let mut ref_dpb: Vec<vk::VideoDecodeH265DpbSlotInfoKHR> = ref_std | ||
| 649 | + .iter() | ||
| 650 | + .map(|s| { | ||
| 651 | + vk::VideoDecodeH265DpbSlotInfoKHR::builder() | ||
| 652 | + .std_reference_info(s) | ||
| 653 | + .build() | ||
| 654 | + }) | ||
| 655 | + .collect(); | ||
| 656 | + let refs: Vec<vk::VideoReferenceSlotInfoKHR> = pic | ||
| 657 | + .refs | ||
| 658 | + .iter() | ||
| 659 | + .zip(ref_dpb.iter_mut()) | ||
| 660 | + .map(|(r, dpb)| { | ||
| 661 | + vk::VideoReferenceSlotInfoKHR::builder() | ||
| 662 | + .slot_index(r.slot as i32) | ||
| 663 | + .picture_resource(&pr[r.slot as usize]) | ||
| 664 | + .push_next(dpb) | ||
| 665 | + .build() | ||
| 666 | + }) | ||
| 667 | + .collect(); | ||
| 668 | + | ||
| 669 | + let mut begin: Vec<vk::VideoReferenceSlotInfoKHR> = vec![ | ||
| 670 | + vk::VideoReferenceSlotInfoKHR::builder() | ||
| 671 | + .slot_index(-1) | ||
| 672 | + .picture_resource(&pr[pic.setup_slot as usize]) | ||
| 673 | + .build(), | ||
| 674 | + ]; | ||
| 675 | + begin.extend(pic.refs.iter().map(|r| { | ||
| 676 | + vk::VideoReferenceSlotInfoKHR::builder() | ||
| 677 | + .slot_index(r.slot as i32) | ||
| 678 | + .picture_resource(&pr[r.slot as usize]) | ||
| 679 | + .build() | ||
| 680 | + })); | ||
| 681 | + | ||
| 682 | + let mut std_pic = zeroed::<vk::video::StdVideoDecodeH265PictureInfo>(); | ||
| 683 | + std_pic.flags.set_IrapPicFlag(pic.irap as u32); | ||
| 684 | + std_pic.flags.set_IdrPicFlag(pic.idr as u32); | ||
| 685 | + std_pic.flags.set_IsReference(pic.reference as u32); | ||
| 686 | + std_pic | ||
| 687 | + .flags | ||
| 688 | + .set_short_term_ref_pic_set_sps_flag(pic.short_term_ref_pic_set_sps as u32); | ||
| 689 | + std_pic.sps_video_parameter_set_id = pic.vps_id; | ||
| 690 | + std_pic.pps_seq_parameter_set_id = pic.sps_id; | ||
| 691 | + std_pic.pps_pic_parameter_set_id = pic.pps_id; | ||
| 692 | + std_pic.NumDeltaPocsOfRefRpsIdx = pic.num_delta_pocs_of_ref_rps_idx; | ||
| 693 | + std_pic.PicOrderCntVal = pic.poc; | ||
| 694 | + std_pic.NumBitsForSTRefPicSetInSlice = pic.st_rps_bits as u16; | ||
| 695 | + std_pic.RefPicSetStCurrBefore = rps_slots(pic.st_curr_before); | ||
| 696 | + std_pic.RefPicSetStCurrAfter = rps_slots(pic.st_curr_after); | ||
| 697 | + std_pic.RefPicSetLtCurr = rps_slots(pic.lt_curr); | ||
| 698 | + | ||
| 699 | + let mut h265 = vk::VideoDecodeH265PictureInfoKHR::builder() | ||
| 700 | + .std_picture_info(&std_pic) | ||
| 701 | + .slice_segment_offsets(&offsets) | ||
| 702 | + .build(); | ||
| 703 | + let decode = vk::VideoDecodeInfoKHR::builder() | ||
| 704 | + .src_buffer(self.bitstream.buffer) | ||
| 705 | + .src_buffer_offset(0) | ||
| 706 | + .src_buffer_range(range) | ||
| 707 | + .dst_picture_resource(picture_resource(self.output.view, extent)) | ||
| 708 | + .setup_reference_slot(&setup) | ||
| 709 | + .reference_slots(&refs) | ||
| 710 | + .push_next(&mut h265) | ||
| 711 | + .build(); | ||
| 712 | + let begin_info = vk::VideoBeginCodingInfoKHR::builder() | ||
| 713 | + .video_session(self.video_session) | ||
| 714 | + .video_session_parameters(params) | ||
| 715 | + .reference_slots(&begin) | ||
| 716 | + .build(); | ||
| 717 | + let reset = !self.coded; | ||
| 718 | + | ||
| 719 | + unsafe { | ||
| 720 | + let info = vk::CommandBufferBeginInfo::builder() | ||
| 721 | + .flags(vk::CommandBufferUsageFlags::ONE_TIME_SUBMIT); | ||
| 722 | + vk(d.begin_command_buffer(cb, &info), "vkBeginCommandBuffer")?; | ||
| 723 | + barrier( | ||
| 724 | + d, | ||
| 725 | + cb, | ||
| 726 | + self.output.image, | ||
| 727 | + ( | ||
| 728 | + vk::ImageLayout::UNDEFINED, | ||
| 729 | + vk::PipelineStageFlags2::NONE, | ||
| 730 | + vk::AccessFlags2::NONE, | ||
| 731 | + ), | ||
| 732 | + ( | ||
| 733 | + vk::ImageLayout::VIDEO_DECODE_DST_KHR, | ||
| 734 | + vk::PipelineStageFlags2::ALL_COMMANDS, | ||
| 735 | + vk::AccessFlags2::NONE, | ||
| 736 | + ), | ||
| 737 | + ); | ||
| 738 | + d.cmd_begin_video_coding_khr(cb, &begin_info); | ||
| 739 | + if reset { | ||
| 740 | + let control = vk::VideoCodingControlInfoKHR::builder() | ||
| 741 | + .flags(vk::VideoCodingControlFlagsKHR::RESET) | ||
| 742 | + .build(); | ||
| 743 | + d.cmd_control_video_coding_khr(cb, &control); | ||
| 744 | + } | ||
| 745 | + d.cmd_decode_video_khr(cb, &decode); | ||
| 746 | + let end = vk::VideoEndCodingInfoKHR::builder().build(); | ||
| 747 | + d.cmd_end_video_coding_khr(cb, &end); | ||
| 748 | + vk(d.end_command_buffer(cb), "vkEndCommandBuffer")?; | ||
| 749 | + } | ||
| 750 | + submit(d, self.fence, self.queue, cb)?; | ||
| 751 | + self.coded = true; | ||
| 752 | + Ok(()) | ||
| 753 | + } | ||
| 754 | + | ||
| 755 | + /// Copies the top-left width x height of the last decoded picture out as NV12 planes (Y, interleaved UV). | ||
| 756 | + pub fn read_output(&mut self, width: u32, height: u32) -> Result<(Vec<u8>, Vec<u8>), Error> { | ||
| 757 | + if !self.coded { | ||
| 758 | + return Err(Error::Invalid("nothing decoded".into())); | ||
| 759 | + } | ||
| 760 | + if width == 0 | ||
| 761 | + || height == 0 | ||
| 762 | + || width % 2 != 0 | ||
| 763 | + || height % 2 != 0 | ||
| 764 | + || width > self.max_extent.width | ||
| 765 | + || height > self.max_extent.height | ||
| 766 | + { | ||
| 767 | + return Err(Error::Invalid("readback size".into())); | ||
| 768 | + } | ||
| 769 | + let gpu = self.gpu.clone(); | ||
| 770 | + let d = gpu.device(); | ||
| 771 | + let cb = self.graphics_cb; | ||
| 772 | + let image = self.output.image; | ||
| 773 | + let luma = width as usize * height as usize; | ||
| 774 | + let copies = [ | ||
| 775 | + copy_region(0, PLANE_0, width, height), | ||
| 776 | + copy_region(luma as u64, PLANE_1, width / 2, height / 2), | ||
| 777 | + ]; | ||
| 778 | + unsafe { | ||
| 779 | + let info = vk::CommandBufferBeginInfo::builder() | ||
| 780 | + .flags(vk::CommandBufferUsageFlags::ONE_TIME_SUBMIT); | ||
| 781 | + vk(d.begin_command_buffer(cb, &info), "vkBeginCommandBuffer")?; | ||
| 782 | + barrier( | ||
| 783 | + d, | ||
| 784 | + cb, | ||
| 785 | + image, | ||
| 786 | + ( | ||
| 787 | + vk::ImageLayout::VIDEO_DECODE_DST_KHR, | ||
| 788 | + vk::PipelineStageFlags2::ALL_COMMANDS, | ||
| 789 | + vk::AccessFlags2::NONE, | ||
| 790 | + ), | ||
| 791 | + ( | ||
| 792 | + vk::ImageLayout::TRANSFER_SRC_OPTIMAL, | ||
| 793 | + vk::PipelineStageFlags2::COPY, | ||
| 794 | + vk::AccessFlags2::TRANSFER_READ, | ||
| 795 | + ), | ||
| 796 | + ); | ||
| 797 | + d.cmd_copy_image_to_buffer( | ||
| 798 | + cb, | ||
| 799 | + image, | ||
| 800 | + vk::ImageLayout::TRANSFER_SRC_OPTIMAL, | ||
| 801 | + self.readback.buffer, | ||
| 802 | + &copies, | ||
| 803 | + ); | ||
| 804 | + barrier( | ||
| 805 | + d, | ||
| 806 | + cb, | ||
| 807 | + image, | ||
| 808 | + ( | ||
| 809 | + vk::ImageLayout::TRANSFER_SRC_OPTIMAL, | ||
| 810 | + vk::PipelineStageFlags2::COPY, | ||
| 811 | + vk::AccessFlags2::TRANSFER_READ, | ||
| 812 | + ), | ||
| 813 | + ( | ||
| 814 | + vk::ImageLayout::VIDEO_DECODE_DST_KHR, | ||
| 815 | + vk::PipelineStageFlags2::ALL_COMMANDS, | ||
| 816 | + vk::AccessFlags2::NONE, | ||
| 817 | + ), | ||
| 818 | + ); | ||
| 819 | + vk(d.end_command_buffer(cb), "vkEndCommandBuffer")?; | ||
| 820 | + } | ||
| 821 | + submit(d, self.fence, self.graphics_queue, cb)?; | ||
| 822 | + let ptr = self | ||
| 823 | + .readback | ||
| 824 | + .ptr | ||
| 825 | + .ok_or_else(|| Error::Invalid("readback buffer not mapped".into()))?; | ||
| 826 | + let bytes = unsafe { std::slice::from_raw_parts(ptr, luma * 3 / 2) }; | ||
| 827 | + Ok((bytes[..luma].to_vec(), bytes[luma..].to_vec())) | ||
| 828 | + } | ||
| 829 | + | ||
| 830 | + fn create_session(&mut self, profile: &Profile, caps: &Caps, active: u32) -> Result<(), Error> { | ||
| 831 | + let gpu = self.gpu.clone(); | ||
| 832 | + let d = gpu.device(); | ||
| 833 | + let info = vk::VideoSessionCreateInfoKHR::builder() | ||
| 834 | + .queue_family_index(self.family) | ||
| 835 | + .video_profile(&profile.info) | ||
| 836 | + .picture_format(NV12) | ||
| 837 | + .max_coded_extent(self.max_extent) | ||
| 838 | + .reference_picture_format(NV12) | ||
| 839 | + .max_dpb_slots(self.max_dpb_slots) | ||
| 840 | + .max_active_reference_pictures(active) | ||
| 841 | + .std_header_version(&caps.std_header_version) | ||
| 842 | + .build(); | ||
| 843 | + self.video_session = unsafe { | ||
| 844 | + vk( | ||
| 845 | + d.create_video_session_khr(&info, None), | ||
| 846 | + "vkCreateVideoSessionKHR", | ||
| 847 | + )? | ||
| 848 | + }; | ||
| 849 | + let reqs = unsafe { | ||
| 850 | + vk( | ||
| 851 | + d.get_video_session_memory_requirements_khr(self.video_session), | ||
| 852 | + "vkGetVideoSessionMemoryRequirementsKHR", | ||
| 853 | + )? | ||
| 854 | + }; | ||
| 855 | + let mut binds = Vec::new(); | ||
| 856 | + for r in &reqs { | ||
| 857 | + let mem = allocate( | ||
| 858 | + &gpu, | ||
| 859 | + r.memory_requirements, | ||
| 860 | + vk::MemoryPropertyFlags::DEVICE_LOCAL, | ||
| 861 | + )?; | ||
| 862 | + self.session_memories.push(mem); | ||
| 863 | + binds.push( | ||
| 864 | + vk::BindVideoSessionMemoryInfoKHR::builder() | ||
| 865 | + .memory_bind_index(r.memory_bind_index) | ||
| 866 | + .memory(mem) | ||
| 867 | + .memory_size(r.memory_requirements.size) | ||
| 868 | + .build(), | ||
| 869 | + ); | ||
| 870 | + } | ||
| 871 | + unsafe { | ||
| 872 | + vk( | ||
| 873 | + d.bind_video_session_memory_khr(self.video_session, &binds), | ||
| 874 | + "vkBindVideoSessionMemoryKHR", | ||
| 875 | + ) | ||
| 876 | + } | ||
| 877 | + } | ||
| 878 | + | ||
| 879 | + fn create_resources(&mut self, profile: &Profile, caps: &Caps) -> Result<(), Error> { | ||
| 880 | + let gpu = self.gpu.clone(); | ||
| 881 | + let d = gpu.device(); | ||
| 882 | + let mut list = vk::VideoProfileListInfoKHR::builder() | ||
| 883 | + .profiles(std::slice::from_ref(&profile.info)) | ||
| 884 | + .build(); | ||
| 885 | + let width = self.max_extent.width; | ||
| 886 | + let height = self.max_extent.height; | ||
| 887 | + let families = if self.family == self.graphics { | ||
| 888 | + vec![self.family] | ||
| 889 | + } else { | ||
| 890 | + vec![self.family, self.graphics] | ||
| 891 | + }; | ||
| 892 | + let dpb = vk::ImageUsageFlags::VIDEO_DECODE_DPB_KHR; | ||
| 893 | + for _ in 0..self.max_dpb_slots { | ||
| 894 | + let image = new_image(&gpu, width, height, dpb, dpb, &[self.family], &mut list)?; | ||
| 895 | + self.dpb.push(image); | ||
| 896 | + } | ||
| 897 | + let output = vk::ImageUsageFlags::VIDEO_DECODE_DST_KHR | vk::ImageUsageFlags::TRANSFER_SRC; | ||
| 898 | + self.output = new_image( | ||
| 899 | + &gpu, | ||
| 900 | + width, | ||
| 901 | + height, | ||
| 902 | + output, | ||
| 903 | + vk::ImageUsageFlags::VIDEO_DECODE_DST_KHR, | ||
| 904 | + &families, | ||
| 905 | + &mut list, | ||
| 906 | + )?; | ||
| 907 | + | ||
| 908 | + let bitstream_size = align_u64(width as u64 * height as u64 * 3 + 65536, caps.size_align); | ||
| 909 | + self.bitstream = new_buffer( | ||
| 910 | + &gpu, | ||
| 911 | + bitstream_size, | ||
| 912 | + vk::BufferUsageFlags::VIDEO_DECODE_SRC_KHR, | ||
| 913 | + vk::MemoryPropertyFlags::HOST_VISIBLE | vk::MemoryPropertyFlags::HOST_COHERENT, | ||
| 914 | + Some(&mut list), | ||
| 915 | + )?; | ||
| 916 | + self.readback = new_buffer( | ||
| 917 | + &gpu, | ||
| 918 | + width as u64 * height as u64 * 3 / 2, | ||
| 919 | + vk::BufferUsageFlags::TRANSFER_DST, | ||
| 920 | + vk::MemoryPropertyFlags::HOST_VISIBLE | vk::MemoryPropertyFlags::HOST_COHERENT, | ||
| 921 | + None, | ||
| 922 | + )?; | ||
| 923 | + | ||
| 924 | + self.decode_pool = create_pool(d, self.family)?; | ||
| 925 | + self.decode_cb = alloc_cb(d, self.decode_pool)?; | ||
| 926 | + self.graphics_pool = create_pool(d, self.graphics)?; | ||
| 927 | + self.graphics_cb = alloc_cb(d, self.graphics_pool)?; | ||
| 928 | + self.fence = unsafe { | ||
| 929 | + vk( | ||
| 930 | + d.create_fence(&vk::FenceCreateInfo::builder().build(), None), | ||
| 931 | + "vkCreateFence", | ||
| 932 | + )? | ||
| 933 | + }; | ||
| 934 | + Ok(()) | ||
| 935 | + } | ||
| 936 | + | ||
| 937 | + fn init_dpb(&mut self) -> Result<(), Error> { | ||
| 938 | + let gpu = self.gpu.clone(); | ||
| 939 | + let d = gpu.device(); | ||
| 940 | + let cb = self.decode_cb; | ||
| 941 | + unsafe { | ||
| 942 | + let info = vk::CommandBufferBeginInfo::builder() | ||
| 943 | + .flags(vk::CommandBufferUsageFlags::ONE_TIME_SUBMIT); | ||
| 944 | + vk(d.begin_command_buffer(cb, &info), "vkBeginCommandBuffer")?; | ||
| 945 | + for img in &self.dpb { | ||
| 946 | + barrier( | ||
| 947 | + d, | ||
| 948 | + cb, | ||
| 949 | + img.image, | ||
| 950 | + ( | ||
| 951 | + vk::ImageLayout::UNDEFINED, | ||
| 952 | + vk::PipelineStageFlags2::NONE, | ||
| 953 | + vk::AccessFlags2::NONE, | ||
| 954 | + ), | ||
| 955 | + ( | ||
| 956 | + vk::ImageLayout::VIDEO_DECODE_DPB_KHR, | ||
| 957 | + vk::PipelineStageFlags2::ALL_COMMANDS, | ||
| 958 | + vk::AccessFlags2::NONE, | ||
| 959 | + ), | ||
| 960 | + ); | ||
| 961 | + } | ||
| 962 | + vk(d.end_command_buffer(cb), "vkEndCommandBuffer")?; | ||
| 963 | + } | ||
| 964 | + submit(d, self.fence, self.queue, cb) | ||
| 965 | + } | ||
| 966 | +} | ||
| 967 | + | ||
| 968 | +impl Drop for H265DecodeSession { | ||
| 969 | + fn drop(&mut self) { | ||
| 970 | + let gpu = self.gpu.clone(); | ||
| 971 | + let d = gpu.device(); | ||
| 972 | + unsafe { | ||
| 973 | + let _ = d.device_wait_idle(); | ||
| 974 | + d.destroy_fence(self.fence, None); | ||
| 975 | + d.destroy_command_pool(self.decode_pool, None); | ||
| 976 | + d.destroy_command_pool(self.graphics_pool, None); | ||
| 977 | + for img in self.dpb.iter().chain(std::iter::once(&self.output)) { | ||
| 978 | + d.destroy_image_view(img.view, None); | ||
| 979 | + d.destroy_image(img.image, None); | ||
| 980 | + d.free_memory(img.memory, None); | ||
| 981 | + } | ||
| 982 | + for buf in [&self.bitstream, &self.readback] { | ||
| 983 | + d.destroy_buffer(buf.buffer, None); | ||
| 984 | + d.free_memory(buf.memory, None); | ||
| 985 | + } | ||
| 986 | + if let Some(params) = self.params { | ||
| 987 | + d.destroy_video_session_parameters_khr(params, None); | ||
| 988 | + } | ||
| 989 | + d.destroy_video_session_khr(self.video_session, None); | ||
| 990 | + for m in &self.session_memories { | ||
| 991 | + d.free_memory(*m, None); | ||
| 992 | + } | ||
| 993 | + } | ||
| 994 | + } | ||
| 995 | +} | ||
| 996 | + | ||
| 997 | +fn query_caps(gpu: &Gpu, profile: &vk::VideoProfileInfoKHR) -> Result<Caps, Error> { | ||
| 998 | + let mut h265 = vk::VideoDecodeH265CapabilitiesKHR::builder(); | ||
| 999 | + let mut decode = vk::VideoDecodeCapabilitiesKHR::builder(); | ||
| 1000 | + let mut caps = vk::VideoCapabilitiesKHR::builder() | ||
| 1001 | + .push_next(&mut h265) | ||
| 1002 | + .push_next(&mut decode); | ||
| 1003 | + unsafe { | ||
| 1004 | + vk( | ||
| 1005 | + gpu.instance().get_physical_device_video_capabilities_khr( | ||
| 1006 | + gpu.physical(), | ||
| 1007 | + profile, | ||
| 1008 | + &mut caps, | ||
| 1009 | + ), | ||
| 1010 | + "vkGetPhysicalDeviceVideoCapabilitiesKHR", | ||
| 1011 | + )?; | ||
| 1012 | + } | ||
| 1013 | + let caps = caps.build(); | ||
| 1014 | + let decode = decode.build(); | ||
| 1015 | + Ok(Caps { | ||
| 1016 | + std_header_version: caps.std_header_version, | ||
| 1017 | + max_extent: caps.max_coded_extent, | ||
| 1018 | + size_align: caps.min_bitstream_buffer_size_alignment, | ||
| 1019 | + max_dpb_slots: caps.max_dpb_slots, | ||
| 1020 | + max_active_refs: caps.max_active_reference_pictures, | ||
| 1021 | + distinct: decode | ||
| 1022 | + .flags | ||
| 1023 | + .contains(vk::VideoDecodeCapabilityFlagsKHR::DPB_AND_OUTPUT_DISTINCT), | ||
| 1024 | + }) | ||
| 1025 | +} | ||
| 1026 | + | ||
| 1027 | +fn rps_slots(slots: &[u32]) -> [u8; 8] { | ||
| 1028 | + let mut out = [0xff; 8]; | ||
| 1029 | + for (o, &s) in out.iter_mut().zip(slots) { | ||
| 1030 | + *o = s as u8; | ||
| 1031 | + } | ||
| 1032 | + out | ||
| 1033 | +} | ||
| 1034 | + | ||
| 1035 | +fn ref_info(poc: i32, long_term: bool) -> vk::video::StdVideoDecodeH265ReferenceInfo { | ||
| 1036 | + let mut info = zeroed::<vk::video::StdVideoDecodeH265ReferenceInfo>(); | ||
| 1037 | + info.flags | ||
| 1038 | + .set_used_for_long_term_reference(long_term as u32); | ||
| 1039 | + info.PicOrderCntVal = poc; | ||
| 1040 | + info | ||
| 1041 | +} | ||
| 1042 | + | ||
| 1043 | +fn level(idc: u8) -> Result<vk::video::StdVideoH265LevelIdc, Error> { | ||
| 1044 | + use vk::video::*; | ||
| 1045 | + let table = [ | ||
| 1046 | + (30, STD_VIDEO_H265_LEVEL_IDC_1_0), | ||
| 1047 | + (60, STD_VIDEO_H265_LEVEL_IDC_2_0), | ||
| 1048 | + (63, STD_VIDEO_H265_LEVEL_IDC_2_1), | ||
| 1049 | + (90, STD_VIDEO_H265_LEVEL_IDC_3_0), | ||
| 1050 | + (93, STD_VIDEO_H265_LEVEL_IDC_3_1), | ||
| 1051 | + (120, STD_VIDEO_H265_LEVEL_IDC_4_0), | ||
| 1052 | + (123, STD_VIDEO_H265_LEVEL_IDC_4_1), | ||
| 1053 | + (150, STD_VIDEO_H265_LEVEL_IDC_5_0), | ||
| 1054 | + (153, STD_VIDEO_H265_LEVEL_IDC_5_1), | ||
| 1055 | + (156, STD_VIDEO_H265_LEVEL_IDC_5_2), | ||
| 1056 | + (180, STD_VIDEO_H265_LEVEL_IDC_6_0), | ||
| 1057 | + (183, STD_VIDEO_H265_LEVEL_IDC_6_1), | ||
| 1058 | + (186, STD_VIDEO_H265_LEVEL_IDC_6_2), | ||
| 1059 | + ]; | ||
| 1060 | + table | ||
| 1061 | + .into_iter() | ||
| 1062 | + .find(|&(v, _)| v == idc) | ||
| 1063 | + .map(|(_, level)| level) | ||
| 1064 | + .ok_or_else(|| Error::Invalid(format!("H.265 level_idc {idc}"))) | ||
| 1065 | +} | ||
| 1066 | + | ||
| 1067 | +fn std_ptl(p: &H265ProfileTierLevel) -> Result<vk::video::StdVideoH265ProfileTierLevel, Error> { | ||
| 1068 | + let mut out = zeroed::<vk::video::StdVideoH265ProfileTierLevel>(); | ||
| 1069 | + out.flags.set_general_tier_flag(p.tier_flag as u32); | ||
| 1070 | + out.flags | ||
| 1071 | + .set_general_progressive_source_flag(p.progressive_source as u32); | ||
| 1072 | + out.flags | ||
| 1073 | + .set_general_interlaced_source_flag(p.interlaced_source as u32); | ||
| 1074 | + out.flags | ||
| 1075 | + .set_general_non_packed_constraint_flag(p.non_packed_constraint as u32); | ||
| 1076 | + out.flags | ||
| 1077 | + .set_general_frame_only_constraint_flag(p.frame_only_constraint as u32); | ||
| 1078 | + out.general_profile_idc = vk::video::StdVideoH265ProfileIdc(p.profile_idc as i32); | ||
| 1079 | + out.general_level_idc = level(p.level_idc)?; | ||
| 1080 | + Ok(out) | ||
| 1081 | +} | ||
| 1082 | + | ||
| 1083 | +fn std_dpbm(d: &H265DecPicBufMgr) -> vk::video::StdVideoH265DecPicBufMgr { | ||
| 1084 | + vk::video::StdVideoH265DecPicBufMgr { | ||
| 1085 | + max_latency_increase_plus1: d.max_latency_increase_plus1, | ||
| 1086 | + max_dec_pic_buffering_minus1: d.max_dec_pic_buffering_minus1, | ||
| 1087 | + max_num_reorder_pics: d.max_num_reorder_pics, | ||
| 1088 | + } | ||
| 1089 | +} | ||
| 1090 | + | ||
| 1091 | +fn std_lists(l: &H265ScalingLists) -> vk::video::StdVideoH265ScalingLists { | ||
| 1092 | + vk::video::StdVideoH265ScalingLists { | ||
| 1093 | + ScalingList4x4: l.list_4x4, | ||
| 1094 | + ScalingList8x8: l.list_8x8, | ||
| 1095 | + ScalingList16x16: l.list_16x16, | ||
| 1096 | + ScalingList32x32: l.list_32x32, | ||
| 1097 | + ScalingListDCCoef16x16: l.dc_16x16, | ||
| 1098 | + ScalingListDCCoef32x32: l.dc_32x32, | ||
| 1099 | + } | ||
| 1100 | +} | ||
| 1101 | + | ||
| 1102 | +fn std_vps( | ||
| 1103 | + v: &H265Vps, | ||
| 1104 | + ptl: &vk::video::StdVideoH265ProfileTierLevel, | ||
| 1105 | + dpbm: &vk::video::StdVideoH265DecPicBufMgr, | ||
| 1106 | +) -> vk::video::StdVideoH265VideoParameterSet { | ||
| 1107 | + let mut out = zeroed::<vk::video::StdVideoH265VideoParameterSet>(); | ||
| 1108 | + out.vps_video_parameter_set_id = v.id; | ||
| 1109 | + out.vps_max_sub_layers_minus1 = v.max_sub_layers_minus1; | ||
| 1110 | + out.pProfileTierLevel = ptl as *const _; | ||
| 1111 | + out.pDecPicBufMgr = dpbm as *const _; | ||
| 1112 | + out | ||
| 1113 | +} | ||
| 1114 | + | ||
| 1115 | +fn std_lt( | ||
| 1116 | + lt: &[H265LongTermRefPicSps], | ||
| 1117 | +) -> Result<vk::video::StdVideoH265LongTermRefPicsSps, Error> { | ||
| 1118 | + if lt.len() > 32 { | ||
| 1119 | + return Err(Error::Invalid("too many SPS long-term pictures".into())); | ||
| 1120 | + } | ||
| 1121 | + let mut out = zeroed::<vk::video::StdVideoH265LongTermRefPicsSps>(); | ||
| 1122 | + for (i, l) in lt.iter().enumerate() { | ||
| 1123 | + out.lt_ref_pic_poc_lsb_sps[i] = l.poc_lsb; | ||
| 1124 | + out.used_by_curr_pic_lt_sps_flag |= (l.used_by_curr_pic as u32) << i; | ||
| 1125 | + } | ||
| 1126 | + Ok(out) | ||
| 1127 | +} | ||
| 1128 | + | ||
| 1129 | +fn deltas(d: &[i32], negative: bool) -> Result<[u16; 16], Error> { | ||
| 1130 | + let mut out = [0u16; 16]; | ||
| 1131 | + let mut prev = 0i32; | ||
| 1132 | + for (o, &cur) in out.iter_mut().zip(d) { | ||
| 1133 | + let step = if negative { prev - cur } else { cur - prev }; | ||
| 1134 | + *o = u16::try_from(step - 1) | ||
| 1135 | + .map_err(|_| Error::Invalid("short-term RPS deltas not monotonic".into()))?; | ||
| 1136 | + prev = cur; | ||
| 1137 | + } | ||
| 1138 | + Ok(out) | ||
| 1139 | +} | ||
| 1140 | + | ||
| 1141 | +fn std_rps(r: &H265ShortTermRefPicSet) -> Result<vk::video::StdVideoH265ShortTermRefPicSet, Error> { | ||
| 1142 | + let (n0, n1) = (r.delta_poc_s0.len(), r.delta_poc_s1.len()); | ||
| 1143 | + if n0 + n1 > 16 || r.used_by_curr_pic_s0.len() != n0 || r.used_by_curr_pic_s1.len() != n1 { | ||
| 1144 | + return Err(Error::Invalid("short-term RPS shape".into())); | ||
| 1145 | + } | ||
| 1146 | + let mask = |flags: &[bool]| { | ||
| 1147 | + flags | ||
| 1148 | + .iter() | ||
| 1149 | + .enumerate() | ||
| 1150 | + .fold(0u32, |m, (i, &f)| m | ((f as u32) << i)) | ||
| 1151 | + }; | ||
| 1152 | + let used0 = mask(&r.used_by_curr_pic_s0); | ||
| 1153 | + let used1 = mask(&r.used_by_curr_pic_s1); | ||
| 1154 | + let mut out = zeroed::<vk::video::StdVideoH265ShortTermRefPicSet>(); | ||
| 1155 | + out.num_negative_pics = n0 as u8; | ||
| 1156 | + out.num_positive_pics = n1 as u8; | ||
| 1157 | + out.used_by_curr_pic_s0_flag = used0 as u16; | ||
| 1158 | + out.used_by_curr_pic_s1_flag = used1 as u16; | ||
| 1159 | + out.used_by_curr_pic_flag = (used0 | (used1 << n0)) as u16; | ||
| 1160 | + out.delta_poc_s0_minus1 = deltas(&r.delta_poc_s0, true)?; | ||
| 1161 | + out.delta_poc_s1_minus1 = deltas(&r.delta_poc_s1, false)?; | ||
| 1162 | + Ok(out) | ||
| 1163 | +} | ||
| 1164 | + | ||
| 1165 | +fn std_sps( | ||
| 1166 | + s: &H265Sps, | ||
| 1167 | + ptl: &vk::video::StdVideoH265ProfileTierLevel, | ||
| 1168 | + dpbm: &vk::video::StdVideoH265DecPicBufMgr, | ||
| 1169 | + lists: Option<&vk::video::StdVideoH265ScalingLists>, | ||
| 1170 | + rps: &[vk::video::StdVideoH265ShortTermRefPicSet], | ||
| 1171 | + lt: &vk::video::StdVideoH265LongTermRefPicsSps, | ||
| 1172 | +) -> Result<vk::video::StdVideoH265SequenceParameterSet, Error> { | ||
| 1173 | + let mut out = zeroed::<vk::video::StdVideoH265SequenceParameterSet>(); | ||
| 1174 | + out.flags | ||
| 1175 | + .set_conformance_window_flag((s.conformance_window != [0; 4]) as u32); | ||
| 1176 | + out.flags | ||
| 1177 | + .set_scaling_list_enabled_flag(s.scaling_list_enabled as u32); | ||
| 1178 | + out.flags | ||
| 1179 | + .set_sps_scaling_list_data_present_flag(lists.is_some() as u32); | ||
| 1180 | + out.flags.set_amp_enabled_flag(s.amp_enabled as u32); | ||
| 1181 | + out.flags | ||
| 1182 | + .set_sample_adaptive_offset_enabled_flag(s.sample_adaptive_offset_enabled as u32); | ||
| 1183 | + out.flags.set_pcm_enabled_flag(s.pcm.is_some() as u32); | ||
| 1184 | + out.flags | ||
| 1185 | + .set_long_term_ref_pics_present_flag(s.long_term_ref_pics_present as u32); | ||
| 1186 | + out.flags | ||
| 1187 | + .set_sps_temporal_mvp_enabled_flag(s.temporal_mvp_enabled as u32); | ||
| 1188 | + out.flags | ||
| 1189 | + .set_strong_intra_smoothing_enabled_flag(s.strong_intra_smoothing_enabled as u32); | ||
| 1190 | + if let Some(p) = s.pcm { | ||
| 1191 | + out.flags | ||
| 1192 | + .set_pcm_loop_filter_disabled_flag(p.loop_filter_disabled as u32); | ||
| 1193 | + out.pcm_sample_bit_depth_luma_minus1 = p.bit_depth_luma_minus1; | ||
| 1194 | + out.pcm_sample_bit_depth_chroma_minus1 = p.bit_depth_chroma_minus1; | ||
| 1195 | + out.log2_min_pcm_luma_coding_block_size_minus3 = p.log2_min_luma_coding_block_size_minus3; | ||
| 1196 | + out.log2_diff_max_min_pcm_luma_coding_block_size = | ||
| 1197 | + p.log2_diff_max_min_luma_coding_block_size; | ||
| 1198 | + } | ||
| 1199 | + out.chroma_format_idc = vk::video::StdVideoH265ChromaFormatIdc(s.chroma_format_idc as i32); | ||
| 1200 | + out.pic_width_in_luma_samples = s.pic_width_in_luma_samples; | ||
| 1201 | + out.pic_height_in_luma_samples = s.pic_height_in_luma_samples; | ||
| 1202 | + out.sps_video_parameter_set_id = s.vps_id; | ||
| 1203 | + out.sps_max_sub_layers_minus1 = s.max_sub_layers_minus1; | ||
| 1204 | + out.sps_seq_parameter_set_id = s.id; | ||
| 1205 | + out.bit_depth_luma_minus8 = s.bit_depth_luma_minus8; | ||
| 1206 | + out.bit_depth_chroma_minus8 = s.bit_depth_chroma_minus8; | ||
| 1207 | + out.log2_max_pic_order_cnt_lsb_minus4 = s.log2_max_pic_order_cnt_lsb_minus4; | ||
| 1208 | + out.log2_min_luma_coding_block_size_minus3 = s.log2_min_luma_coding_block_size_minus3; | ||
| 1209 | + out.log2_diff_max_min_luma_coding_block_size = s.log2_diff_max_min_luma_coding_block_size; | ||
| 1210 | + out.log2_min_luma_transform_block_size_minus2 = s.log2_min_luma_transform_block_size_minus2; | ||
| 1211 | + out.log2_diff_max_min_luma_transform_block_size = s.log2_diff_max_min_luma_transform_block_size; | ||
| 1212 | + out.max_transform_hierarchy_depth_inter = s.max_transform_hierarchy_depth_inter; | ||
| 1213 | + out.max_transform_hierarchy_depth_intra = s.max_transform_hierarchy_depth_intra; | ||
| 1214 | + out.num_short_term_ref_pic_sets = rps.len() as u8; | ||
| 1215 | + out.num_long_term_ref_pics_sps = s.long_term_ref_pics_sps.len() as u8; | ||
| 1216 | + out.conf_win_left_offset = s.conformance_window[0]; | ||
| 1217 | + out.conf_win_right_offset = s.conformance_window[1]; | ||
| 1218 | + out.conf_win_top_offset = s.conformance_window[2]; | ||
| 1219 | + out.conf_win_bottom_offset = s.conformance_window[3]; | ||
| 1220 | + out.pProfileTierLevel = ptl as *const _; | ||
| 1221 | + out.pDecPicBufMgr = dpbm as *const _; | ||
| 1222 | + out.pScalingLists = lists.map_or(std::ptr::null(), |l| l as *const _); | ||
| 1223 | + out.pShortTermRefPicSet = rps.as_ptr(); | ||
| 1224 | + out.pLongTermRefPicsSps = lt as *const _; | ||
| 1225 | + Ok(out) | ||
| 1226 | +} | ||
| 1227 | + | ||
| 1228 | +fn std_pps( | ||
| 1229 | + p: &H265Pps, | ||
| 1230 | + vps_id: u8, | ||
| 1231 | + lists: Option<&vk::video::StdVideoH265ScalingLists>, | ||
| 1232 | +) -> Result<vk::video::StdVideoH265PictureParameterSet, Error> { | ||
| 1233 | + if p.column_width_minus1.len() > 19 || p.row_height_minus1.len() > 21 { | ||
| 1234 | + return Err(Error::Invalid("too many tile columns or rows".into())); | ||
| 1235 | + } | ||
| 1236 | + let mut out = zeroed::<vk::video::StdVideoH265PictureParameterSet>(); | ||
| 1237 | + out.flags | ||
| 1238 | + .set_dependent_slice_segments_enabled_flag(p.dependent_slice_segments_enabled as u32); | ||
| 1239 | + out.flags | ||
| 1240 | + .set_output_flag_present_flag(p.output_flag_present as u32); | ||
| 1241 | + out.flags | ||
| 1242 | + .set_sign_data_hiding_enabled_flag(p.sign_data_hiding_enabled as u32); | ||
| 1243 | + out.flags | ||
| 1244 | + .set_cabac_init_present_flag(p.cabac_init_present as u32); | ||
| 1245 | + out.flags | ||
| 1246 | + .set_constrained_intra_pred_flag(p.constrained_intra_pred as u32); | ||
| 1247 | + out.flags | ||
| 1248 | + .set_transform_skip_enabled_flag(p.transform_skip_enabled as u32); | ||
| 1249 | + out.flags | ||
| 1250 | + .set_cu_qp_delta_enabled_flag(p.cu_qp_delta_enabled as u32); | ||
| 1251 | + out.flags | ||
| 1252 | + .set_pps_slice_chroma_qp_offsets_present_flag(p.slice_chroma_qp_offsets_present as u32); | ||
| 1253 | + out.flags.set_weighted_pred_flag(p.weighted_pred as u32); | ||
| 1254 | + out.flags.set_weighted_bipred_flag(p.weighted_bipred as u32); | ||
| 1255 | + out.flags | ||
| 1256 | + .set_transquant_bypass_enabled_flag(p.transquant_bypass_enabled as u32); | ||
| 1257 | + out.flags.set_tiles_enabled_flag(p.tiles_enabled as u32); | ||
| 1258 | + out.flags | ||
| 1259 | + .set_entropy_coding_sync_enabled_flag(p.entropy_coding_sync_enabled as u32); | ||
| 1260 | + out.flags.set_uniform_spacing_flag(p.uniform_spacing as u32); | ||
| 1261 | + out.flags | ||
| 1262 | + .set_loop_filter_across_tiles_enabled_flag(p.loop_filter_across_tiles_enabled as u32); | ||
| 1263 | + out.flags | ||
| 1264 | + .set_pps_loop_filter_across_slices_enabled_flag(p.loop_filter_across_slices_enabled as u32); | ||
| 1265 | + out.flags | ||
| 1266 | + .set_deblocking_filter_control_present_flag(p.deblocking_filter_control_present as u32); | ||
| 1267 | + out.flags | ||
| 1268 | + .set_deblocking_filter_override_enabled_flag(p.deblocking_filter_override_enabled as u32); | ||
| 1269 | + out.flags | ||
| 1270 | + .set_pps_deblocking_filter_disabled_flag(p.deblocking_filter_disabled as u32); | ||
| 1271 | + out.flags | ||
| 1272 | + .set_pps_scaling_list_data_present_flag(lists.is_some() as u32); | ||
| 1273 | + out.flags | ||
| 1274 | + .set_lists_modification_present_flag(p.lists_modification_present as u32); | ||
| 1275 | + out.flags.set_slice_segment_header_extension_present_flag( | ||
| 1276 | + p.slice_segment_header_extension_present as u32, | ||
| 1277 | + ); | ||
| 1278 | + out.pps_pic_parameter_set_id = p.id; | ||
| 1279 | + out.pps_seq_parameter_set_id = p.sps_id; | ||
| 1280 | + out.sps_video_parameter_set_id = vps_id; | ||
| 1281 | + out.num_extra_slice_header_bits = p.num_extra_slice_header_bits; | ||
| 1282 | + out.num_ref_idx_l0_default_active_minus1 = p.num_ref_idx_l0_default_active_minus1; | ||
| 1283 | + out.num_ref_idx_l1_default_active_minus1 = p.num_ref_idx_l1_default_active_minus1; | ||
| 1284 | + out.init_qp_minus26 = p.init_qp_minus26; | ||
| 1285 | + out.diff_cu_qp_delta_depth = p.diff_cu_qp_delta_depth; | ||
| 1286 | + out.pps_cb_qp_offset = p.cb_qp_offset; | ||
| 1287 | + out.pps_cr_qp_offset = p.cr_qp_offset; | ||
| 1288 | + out.pps_beta_offset_div2 = p.beta_offset_div2; | ||
| 1289 | + out.pps_tc_offset_div2 = p.tc_offset_div2; | ||
| 1290 | + out.log2_parallel_merge_level_minus2 = p.log2_parallel_merge_level_minus2; | ||
| 1291 | + out.num_tile_columns_minus1 = p.num_tile_columns_minus1; | ||
| 1292 | + out.num_tile_rows_minus1 = p.num_tile_rows_minus1; | ||
| 1293 | + out.column_width_minus1[..p.column_width_minus1.len()].copy_from_slice(&p.column_width_minus1); | ||
| 1294 | + out.row_height_minus1[..p.row_height_minus1.len()].copy_from_slice(&p.row_height_minus1); | ||
| 1295 | + out.pScalingLists = lists.map_or(std::ptr::null(), |l| l as *const _); | ||
| 1296 | + Ok(out) | ||
| 1297 | +} | ||
shims/vk-video-ffi/src/lib.rsmodified+6 -0
| @@ -9,6 +9,7 @@ use std::sync::Arc; | |||
| 9 | mod av1_encode; | 9 | mod av1_encode; |
| 10 | mod h264_decode; | 10 | mod h264_decode; |
| 11 | mod h264_encode; | 11 | mod h264_encode; |
| 12 | +mod h265_decode; | ||
| 12 | mod h265_encode; | 13 | mod h265_encode; |
| 13 | mod rgb_convert; | 14 | mod rgb_convert; |
| 14 | 15 | ||
| @@ -18,6 +19,11 @@ pub use h264_decode::{ | |||
| 18 | ScalingLists, | 19 | ScalingLists, |
| 19 | }; | 20 | }; |
| 20 | pub use h264_encode::{H264EncodeConfig, H264EncodeSession, H264Picture, H264Ref, RateControl}; | 21 | pub use h264_encode::{H264EncodeConfig, H264EncodeSession, H264Picture, H264Ref, RateControl}; |
| 22 | +pub use h265_decode::{ | ||
| 23 | + H265DecPicBufMgr, H265DecodeConfig, H265DecodePicture, H265DecodeSession, H265LongTermRefPicSps, | ||
| 24 | + H265Pcm, H265Pps, H265ProfileTierLevel, H265RefSlot, H265ScalingLists, H265ShortTermRefPicSet, | ||
| 25 | + H265Sps, H265Vps, | ||
| 26 | +}; | ||
| 21 | pub use h265_encode::{H265EncodeConfig, H265EncodeSession, H265Picture, H265Ref}; | 27 | pub use h265_encode::{H265EncodeConfig, H265EncodeSession, H265Picture, H265Ref}; |
| 22 | 28 | ||
| 23 | use vk::KhrVideoQueueExtensionInstanceCommands; | 29 | use vk::KhrVideoQueueExtensionInstanceCommands; |