vk-video-ffi: H264DecodeSession (distinct DPB/output, plain-struct SPS/PPS/refs, NV12 readback); smoke example bit-exact vs ffmpeg
Browse files at this commit8defefb4b7d63dc45a4574e24a6eea658ec83baeParent: c58ee895 changed files +1250 -0
- Cargo.lock +1 -0
- shims/vk-video-ffi/Cargo.toml +3 -0
- shims/vk-video-ffi/examples/h264_decode_smoke.rs +180 -0
- shims/vk-video-ffi/src/h264_decode.rs +1055 -0
- shims/vk-video-ffi/src/lib.rs +11 -0
Cargo.lockmodified+1 -0
| @@ -1895,6 +1895,7 @@ checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a" | ||
| 1895 | 1895 | name = "vk-video-ffi" |
| 1896 | 1896 | version = "0.1.0" |
| 1897 | 1897 | dependencies = [ |
| 1898 | + "ec-h264-syntax", | |
| 1898 | 1899 | "vulkanalia", |
| 1899 | 1900 | ] |
| 1900 | 1901 |
shims/vk-video-ffi/Cargo.tomlmodified+3 -0
| @@ -7,3 +7,6 @@ authors.workspace = true | ||
| 7 | 7 | |
| 8 | 8 | [dependencies] |
| 9 | 9 | vulkanalia = { version = "0.35", features = ["libloading"] } |
| 10 | + | |
| 11 | +[dev-dependencies] | |
| 12 | +ec-h264-syntax.workspace = true |
shims/vk-video-ffi/examples/h264_decode_smoke.rsadded+180 -0
| @@ -0,0 +1,180 @@ | ||
| 1 | +use std::fs; | |
| 2 | +use std::path::PathBuf; | |
| 3 | +use std::process::Command; | |
| 4 | + | |
| 5 | +use ec_h264_syntax::sps::{ZIGZAG_4X4, ZIGZAG_8X8}; | |
| 6 | +use ec_h264_syntax::{ | |
| 7 | + AnnexBIter, NalHeader, NalUnitType, Pps, SliceHeader, Sps, unescape_rbsp, | |
| 8 | +}; | |
| 9 | +use vk_video_ffi::{ | |
| 10 | + Gpu, H264DecodeConfig, H264DecodePicture, H264DecodeSession, H264Pps, H264Sps, ScalingLists, | |
| 11 | +}; | |
| 12 | + | |
| 13 | +fn zigzag(raster: &[u8], scan: &[u8]) -> Vec<u8> { | |
| 14 | + scan.iter().map(|&r| raster[r as usize]).collect() | |
| 15 | +} | |
| 16 | + | |
| 17 | +fn lists(l: &ec_h264_syntax::ScalingLists) -> ScalingLists { | |
| 18 | + let mut out = ScalingLists { | |
| 19 | + l4x4: [[0; 16]; 6], | |
| 20 | + l8x8: [[0; 64]; 6], | |
| 21 | + }; | |
| 22 | + for i in 0..6 { | |
| 23 | + out.l4x4[i].copy_from_slice(&zigzag(&l.list_4x4[i], &ZIGZAG_4X4)); | |
| 24 | + out.l8x8[i].copy_from_slice(&zigzag(&l.list_8x8[i], &ZIGZAG_8X8)); | |
| 25 | + } | |
| 26 | + out | |
| 27 | +} | |
| 28 | + | |
| 29 | +fn main() { | |
| 30 | + let dir = PathBuf::from(std::env::args().nth(1).expect("scratch dir argument")); | |
| 31 | + let stream = dir.join("idr.h264"); | |
| 32 | + let reference = dir.join("ref.yuv"); | |
| 33 | + let status = Command::new("ffmpeg") | |
| 34 | + .args(["-y", "-loglevel", "error", "-f", "lavfi", "-i"]) | |
| 35 | + .arg("testsrc2=size=320x240:rate=30") | |
| 36 | + .args(["-frames:v", "1", "-c:v", "libx264", "-profile:v", "main"]) | |
| 37 | + .args(["-bsf:v", "h264_mp4toannexb", "-f", "h264"]) | |
| 38 | + .arg(&stream) | |
| 39 | + .status() | |
| 40 | + .expect("run ffmpeg"); | |
| 41 | + assert!(status.success()); | |
| 42 | + let status = Command::new("ffmpeg") | |
| 43 | + .args(["-y", "-loglevel", "error", "-i"]) | |
| 44 | + .arg(&stream) | |
| 45 | + .args(["-f", "rawvideo", "-pix_fmt", "yuv420p"]) | |
| 46 | + .arg(&reference) | |
| 47 | + .status() | |
| 48 | + .expect("run ffmpeg"); | |
| 49 | + assert!(status.success()); | |
| 50 | + | |
| 51 | + let data = fs::read(&stream).expect("read stream"); | |
| 52 | + let mut sps_list: Vec<Sps> = Vec::new(); | |
| 53 | + let mut pps_list: Vec<Pps> = Vec::new(); | |
| 54 | + let mut slices: Vec<&[u8]> = Vec::new(); | |
| 55 | + let mut slice_header = None; | |
| 56 | + let mut rbsp = Vec::new(); | |
| 57 | + for nal in AnnexBIter::new(&data) { | |
| 58 | + let header = NalHeader::parse(nal[0]).expect("NAL header"); | |
| 59 | + unescape_rbsp(&nal[1..], &mut rbsp); | |
| 60 | + match header.unit_type { | |
| 61 | + NalUnitType::Sps => sps_list.push(Sps::parse(&rbsp).expect("SPS")), | |
| 62 | + NalUnitType::Pps => { | |
| 63 | + let pps = Pps::parse(&rbsp, |id| sps_list.iter().find(|s| s.id == id)) | |
| 64 | + .expect("PPS"); | |
| 65 | + pps_list.push(pps); | |
| 66 | + } | |
| 67 | + NalUnitType::SliceIdr => { | |
| 68 | + let sps = &sps_list[0]; | |
| 69 | + let pps = &pps_list[0]; | |
| 70 | + let sh = SliceHeader::parse(&rbsp, header, sps, pps).expect("slice header"); | |
| 71 | + slice_header = Some(sh); | |
| 72 | + slices.push(nal); | |
| 73 | + } | |
| 74 | + _ => {} | |
| 75 | + } | |
| 76 | + } | |
| 77 | + let sps = &sps_list[0]; | |
| 78 | + let pps = &pps_list[0]; | |
| 79 | + let sh = slice_header.expect("IDR slice"); | |
| 80 | + | |
| 81 | + let unit_y = if sps.frame_mbs_only { 2 } else { 4 }; | |
| 82 | + let (left, right, top, bottom) = sps.crop; | |
| 83 | + let h264_sps = H264Sps { | |
| 84 | + id: sps.id, | |
| 85 | + profile_idc: sps.profile_idc, | |
| 86 | + level_idc: sps.level_idc, | |
| 87 | + chroma_format_idc: sps.chroma_format_idc, | |
| 88 | + bit_depth_luma_minus8: sps.bit_depth_luma - 8, | |
| 89 | + bit_depth_chroma_minus8: sps.bit_depth_chroma - 8, | |
| 90 | + log2_max_frame_num_minus4: sps.log2_max_frame_num - 4, | |
| 91 | + pic_order_cnt_type: sps.pic_order_cnt_type, | |
| 92 | + log2_max_pic_order_cnt_lsb_minus4: sps.log2_max_pic_order_cnt_lsb - 4, | |
| 93 | + offset_for_non_ref_pic: sps.offset_for_non_ref_pic, | |
| 94 | + offset_for_top_to_bottom_field: sps.offset_for_top_to_bottom_field, | |
| 95 | + offset_for_ref_frame: sps.offsets_for_ref_frame.clone(), | |
| 96 | + max_num_ref_frames: sps.max_num_ref_frames as u8, | |
| 97 | + pic_width_in_mbs_minus1: sps.mb_width - 1, | |
| 98 | + pic_height_in_map_units_minus1: (if sps.frame_mbs_only { | |
| 99 | + sps.mb_height | |
| 100 | + } else { | |
| 101 | + sps.mb_height / 2 | |
| 102 | + }) - 1, | |
| 103 | + frame_mbs_only: sps.frame_mbs_only, | |
| 104 | + mb_adaptive_frame_field: sps.mb_adaptive_frame_field, | |
| 105 | + direct_8x8_inference: sps.direct_8x8_inference, | |
| 106 | + delta_pic_order_always_zero: sps.delta_pic_order_always_zero, | |
| 107 | + frame_crop_offsets: [left / 2, right / 2, top / unit_y, bottom / unit_y], | |
| 108 | + qpprime_y_zero_transform_bypass: sps.transform_bypass, | |
| 109 | + seq_scaling_matrix: sps.scaling_lists.as_ref().map(lists), | |
| 110 | + }; | |
| 111 | + let h264_pps = H264Pps { | |
| 112 | + id: pps.id as u8, | |
| 113 | + sps_id: pps.sps_id, | |
| 114 | + entropy_coding_mode: pps.entropy_coding_mode, | |
| 115 | + bottom_field_pic_order_in_frame_present: pps.bottom_field_pic_order_in_frame_present, | |
| 116 | + num_ref_idx_l0_default_active_minus1: (pps.num_ref_idx_l0_default_active - 1) as u8, | |
| 117 | + num_ref_idx_l1_default_active_minus1: (pps.num_ref_idx_l1_default_active - 1) as u8, | |
| 118 | + weighted_pred: pps.weighted_pred, | |
| 119 | + weighted_bipred_idc: pps.weighted_bipred_idc, | |
| 120 | + pic_init_qp_minus26: (pps.pic_init_qp - 26) as i8, | |
| 121 | + pic_init_qs_minus26: (pps.pic_init_qs - 26) as i8, | |
| 122 | + chroma_qp_index_offset: pps.chroma_qp_index_offset as i8, | |
| 123 | + second_chroma_qp_index_offset: pps.second_chroma_qp_index_offset as i8, | |
| 124 | + deblocking_filter_control_present: pps.deblocking_filter_control_present, | |
| 125 | + constrained_intra_pred: pps.constrained_intra_pred, | |
| 126 | + redundant_pic_cnt_present: pps.redundant_pic_cnt_present, | |
| 127 | + transform_8x8_mode: pps.transform_8x8_mode, | |
| 128 | + pic_scaling_matrix: pps.scaling_lists.as_ref().map(lists), | |
| 129 | + }; | |
| 130 | + | |
| 131 | + let (w, h) = (sps.width, sps.height); | |
| 132 | + let gpu = Gpu::open().expect("open GPU"); | |
| 133 | + println!("device: {}", gpu.device_name()); | |
| 134 | + let cfg = H264DecodeConfig { | |
| 135 | + max_width: sps.coded_width, | |
| 136 | + max_height: sps.coded_height, | |
| 137 | + max_dpb_slots: 2, | |
| 138 | + profile_idc: sps.profile_idc, | |
| 139 | + }; | |
| 140 | + let mut session = H264DecodeSession::new(&gpu, &cfg).expect("decode session"); | |
| 141 | + session | |
| 142 | + .set_parameters(std::slice::from_ref(&h264_sps), std::slice::from_ref(&h264_pps)) | |
| 143 | + .expect("parameters"); | |
| 144 | + let poc = if sps.pic_order_cnt_type == 0 { | |
| 145 | + sh.pic_order_cnt_lsb as i32 | |
| 146 | + } else { | |
| 147 | + 0 | |
| 148 | + }; | |
| 149 | + let pic = H264DecodePicture { | |
| 150 | + sps_id: sps.id, | |
| 151 | + pps_id: pps.id as u8, | |
| 152 | + idr: true, | |
| 153 | + reference: true, | |
| 154 | + frame_num: sh.frame_num as u16, | |
| 155 | + idr_pic_id: sh.idr_pic_id.unwrap_or(0) as u16, | |
| 156 | + poc: [poc, poc + sh.delta_pic_order_cnt_bottom], | |
| 157 | + setup_slot: 0, | |
| 158 | + refs: &[], | |
| 159 | + slices: &slices, | |
| 160 | + }; | |
| 161 | + session.decode(&pic).expect("decode"); | |
| 162 | + let (y, uv) = session.read_output(w, h).expect("read slot"); | |
| 163 | + | |
| 164 | + let mut pgm = format!("P5\n{w} {h}\n255\n").into_bytes(); | |
| 165 | + pgm.extend_from_slice(&y); | |
| 166 | + fs::write(dir.join("out.pgm"), &pgm).expect("write pgm"); | |
| 167 | + | |
| 168 | + let ref_data = fs::read(&reference).expect("read reference"); | |
| 169 | + let luma = (w * h) as usize; | |
| 170 | + let chroma = luma / 4; | |
| 171 | + let ref_y = &ref_data[..luma]; | |
| 172 | + let ref_u = &ref_data[luma..luma + chroma]; | |
| 173 | + let ref_v = &ref_data[luma + chroma..luma + 2 * chroma]; | |
| 174 | + let mad = |a: &[u8], b: &[u8]| { | |
| 175 | + a.iter().zip(b).map(|(&x, &y)| (x as f64 - y as f64).abs()).sum::<f64>() / a.len() as f64 | |
| 176 | + }; | |
| 177 | + let ref_uv: Vec<u8> = ref_u.iter().zip(ref_v).flat_map(|(&u, &v)| [u, v]).collect(); | |
| 178 | + println!("mean abs diff Y: {:.6}", mad(&y, ref_y)); | |
| 179 | + println!("mean abs diff UV: {:.6}", mad(&uv, &ref_uv)); | |
| 180 | +} |
shims/vk-video-ffi/src/h264_decode.rsadded+1055 -0
| @@ -0,0 +1,1055 @@ | ||
| 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_encode::{ | |
| 10 | + NV12, PLANE_0, PLANE_1, alloc_cb, align, barrier, color_range, copy_region, create_pool, | |
| 11 | + memory_type, picture_resource, vk, zeroed, | |
| 12 | +}; | |
| 13 | +use crate::{Error, Gpu}; | |
| 14 | + | |
| 15 | +/// Settings fixed for the life of a decode session. | |
| 16 | +#[derive(Debug, Clone, Copy, PartialEq, Eq)] | |
| 17 | +pub struct H264DecodeConfig { | |
| 18 | + /// Largest coded width in luma samples. | |
| 19 | + pub max_width: u32, | |
| 20 | + /// Largest coded height in luma samples. | |
| 21 | + pub max_height: u32, | |
| 22 | + /// DPB slots, including the slot the picture is decoded into. | |
| 23 | + pub max_dpb_slots: u32, | |
| 24 | + /// profile_idc: 66, 77 or 100. | |
| 25 | + pub profile_idc: u8, | |
| 26 | +} | |
| 27 | + | |
| 28 | +/// Scaling lists in bitstream (zigzag) order, fall-back rules already applied. | |
| 29 | +#[derive(Debug, Clone, PartialEq, Eq)] | |
| 30 | +pub struct ScalingLists { | |
| 31 | + /// Intra Y, Cb, Cr then Inter Y, Cb, Cr. | |
| 32 | + pub l4x4: [[u8; 16]; 6], | |
| 33 | + /// Intra Y, Inter Y, then the chroma lists. | |
| 34 | + pub l8x8: [[u8; 64]; 6], | |
| 35 | +} | |
| 36 | + | |
| 37 | +/// Sequence parameter set fields a decoder needs (names follow the spec syntax). | |
| 38 | +#[derive(Debug, Clone, PartialEq, Eq)] | |
| 39 | +pub struct H264Sps { | |
| 40 | + /// seq_parameter_set_id. | |
| 41 | + pub id: u8, | |
| 42 | + /// profile_idc. | |
| 43 | + pub profile_idc: u8, | |
| 44 | + /// level_idc. | |
| 45 | + pub level_idc: u8, | |
| 46 | + /// chroma_format_idc. | |
| 47 | + pub chroma_format_idc: u8, | |
| 48 | + /// bit_depth_luma_minus8. | |
| 49 | + pub bit_depth_luma_minus8: u8, | |
| 50 | + /// bit_depth_chroma_minus8. | |
| 51 | + pub bit_depth_chroma_minus8: u8, | |
| 52 | + /// log2_max_frame_num_minus4. | |
| 53 | + pub log2_max_frame_num_minus4: u8, | |
| 54 | + /// pic_order_cnt_type. | |
| 55 | + pub pic_order_cnt_type: u8, | |
| 56 | + /// log2_max_pic_order_cnt_lsb_minus4. | |
| 57 | + pub log2_max_pic_order_cnt_lsb_minus4: u8, | |
| 58 | + /// offset_for_non_ref_pic. | |
| 59 | + pub offset_for_non_ref_pic: i32, | |
| 60 | + /// offset_for_top_to_bottom_field. | |
| 61 | + pub offset_for_top_to_bottom_field: i32, | |
| 62 | + /// offset_for_ref_frame list. | |
| 63 | + pub offset_for_ref_frame: Vec<i32>, | |
| 64 | + /// max_num_ref_frames. | |
| 65 | + pub max_num_ref_frames: u8, | |
| 66 | + /// pic_width_in_mbs_minus1. | |
| 67 | + pub pic_width_in_mbs_minus1: u32, | |
| 68 | + /// pic_height_in_map_units_minus1. | |
| 69 | + pub pic_height_in_map_units_minus1: u32, | |
| 70 | + /// frame_mbs_only_flag. | |
| 71 | + pub frame_mbs_only: bool, | |
| 72 | + /// mb_adaptive_frame_field_flag. | |
| 73 | + pub mb_adaptive_frame_field: bool, | |
| 74 | + /// direct_8x8_inference_flag. | |
| 75 | + pub direct_8x8_inference: bool, | |
| 76 | + /// delta_pic_order_always_zero_flag. | |
| 77 | + pub delta_pic_order_always_zero: bool, | |
| 78 | + /// frame_crop_left, right, top, bottom offsets in crop units; all zero when not cropped. | |
| 79 | + pub frame_crop_offsets: [u32; 4], | |
| 80 | + /// qpprime_y_zero_transform_bypass_flag. | |
| 81 | + pub qpprime_y_zero_transform_bypass: bool, | |
| 82 | + /// seq_scaling_matrix when present. | |
| 83 | + pub seq_scaling_matrix: Option<ScalingLists>, | |
| 84 | +} | |
| 85 | + | |
| 86 | +/// Picture parameter set fields a decoder needs (names follow the spec syntax). | |
| 87 | +#[derive(Debug, Clone, PartialEq, Eq)] | |
| 88 | +pub struct H264Pps { | |
| 89 | + /// pic_parameter_set_id. | |
| 90 | + pub id: u8, | |
| 91 | + /// seq_parameter_set_id. | |
| 92 | + pub sps_id: u8, | |
| 93 | + /// entropy_coding_mode_flag. | |
| 94 | + pub entropy_coding_mode: bool, | |
| 95 | + /// bottom_field_pic_order_in_frame_present_flag. | |
| 96 | + pub bottom_field_pic_order_in_frame_present: bool, | |
| 97 | + /// num_ref_idx_l0_default_active_minus1. | |
| 98 | + pub num_ref_idx_l0_default_active_minus1: u8, | |
| 99 | + /// num_ref_idx_l1_default_active_minus1. | |
| 100 | + pub num_ref_idx_l1_default_active_minus1: u8, | |
| 101 | + /// weighted_pred_flag. | |
| 102 | + pub weighted_pred: bool, | |
| 103 | + /// weighted_bipred_idc. | |
| 104 | + pub weighted_bipred_idc: u8, | |
| 105 | + /// pic_init_qp_minus26. | |
| 106 | + pub pic_init_qp_minus26: i8, | |
| 107 | + /// pic_init_qs_minus26. | |
| 108 | + pub pic_init_qs_minus26: i8, | |
| 109 | + /// chroma_qp_index_offset. | |
| 110 | + pub chroma_qp_index_offset: i8, | |
| 111 | + /// second_chroma_qp_index_offset. | |
| 112 | + pub second_chroma_qp_index_offset: i8, | |
| 113 | + /// deblocking_filter_control_present_flag. | |
| 114 | + pub deblocking_filter_control_present: bool, | |
| 115 | + /// constrained_intra_pred_flag. | |
| 116 | + pub constrained_intra_pred: bool, | |
| 117 | + /// redundant_pic_cnt_present_flag. | |
| 118 | + pub redundant_pic_cnt_present: bool, | |
| 119 | + /// transform_8x8_mode_flag. | |
| 120 | + pub transform_8x8_mode: bool, | |
| 121 | + /// pic_scaling_matrix when present. | |
| 122 | + pub pic_scaling_matrix: Option<ScalingLists>, | |
| 123 | +} | |
| 124 | + | |
| 125 | +/// A picture held in a DPB slot, used as reference. | |
| 126 | +#[derive(Debug, Clone, Copy, PartialEq, Eq)] | |
| 127 | +pub struct H264RefSlot { | |
| 128 | + /// DPB slot index. | |
| 129 | + pub slot: u32, | |
| 130 | + /// FrameNum of the reference. | |
| 131 | + pub frame_num: u16, | |
| 132 | + /// TopFieldOrderCnt and BottomFieldOrderCnt. | |
| 133 | + pub poc: [i32; 2], | |
| 134 | + /// Marked as long-term reference. | |
| 135 | + pub long_term: bool, | |
| 136 | + /// Gap frame that was never decoded. | |
| 137 | + pub non_existing: bool, | |
| 138 | +} | |
| 139 | + | |
| 140 | +/// One coded picture to decode. Slices are complete NAL units with their header byte, no start code. | |
| 141 | +#[derive(Debug, Clone, Copy)] | |
| 142 | +pub struct H264DecodePicture<'a> { | |
| 143 | + /// seq_parameter_set_id of the active SPS. | |
| 144 | + pub sps_id: u8, | |
| 145 | + /// pic_parameter_set_id of the active PPS. | |
| 146 | + pub pps_id: u8, | |
| 147 | + /// IDR picture. | |
| 148 | + pub idr: bool, | |
| 149 | + /// nal_ref_idc != 0. | |
| 150 | + pub reference: bool, | |
| 151 | + /// FrameNum. | |
| 152 | + pub frame_num: u16, | |
| 153 | + /// idr_pic_id of an IDR picture. | |
| 154 | + pub idr_pic_id: u16, | |
| 155 | + /// TopFieldOrderCnt and BottomFieldOrderCnt. | |
| 156 | + pub poc: [i32; 2], | |
| 157 | + /// DPB slot the picture is decoded into. | |
| 158 | + pub setup_slot: u32, | |
| 159 | + /// Active references, all in DPB slots other than setup_slot. | |
| 160 | + pub refs: &'a [H264RefSlot], | |
| 161 | + /// Slice NAL units in decoding order. | |
| 162 | + pub slices: &'a [&'a [u8]], | |
| 163 | +} | |
| 164 | + | |
| 165 | +struct Caps { | |
| 166 | + std_header_version: vk::ExtensionProperties, | |
| 167 | + max_extent: vk::Extent2D, | |
| 168 | + size_align: u64, | |
| 169 | + max_dpb_slots: u32, | |
| 170 | + max_active_refs: u32, | |
| 171 | + distinct: bool, | |
| 172 | +} | |
| 173 | + | |
| 174 | +#[derive(Default)] | |
| 175 | +struct Image { | |
| 176 | + image: vk::Image, | |
| 177 | + view: vk::ImageView, | |
| 178 | + memory: vk::DeviceMemory, | |
| 179 | +} | |
| 180 | + | |
| 181 | +#[derive(Default)] | |
| 182 | +struct Buffer { | |
| 183 | + buffer: vk::Buffer, | |
| 184 | + memory: vk::DeviceMemory, | |
| 185 | + ptr: Option<*mut u8>, | |
| 186 | + size: u64, | |
| 187 | +} | |
| 188 | + | |
| 189 | +struct Profile { | |
| 190 | + info: vk::VideoProfileInfoKHR, | |
| 191 | + h264: vk::VideoDecodeH264ProfileInfoKHR, | |
| 192 | +} | |
| 193 | + | |
| 194 | +impl Profile { | |
| 195 | + fn new(idc: u8) -> Box<Self> { | |
| 196 | + let mut p = Box::new(Profile { | |
| 197 | + info: vk::VideoProfileInfoKHR::builder() | |
| 198 | + .video_codec_operation(vk::VideoCodecOperationFlagsKHR::DECODE_H264) | |
| 199 | + .chroma_subsampling(vk::VideoChromaSubsamplingFlagsKHR::_420) | |
| 200 | + .luma_bit_depth(vk::VideoComponentBitDepthFlagsKHR::_8) | |
| 201 | + .chroma_bit_depth(vk::VideoComponentBitDepthFlagsKHR::_8) | |
| 202 | + .build(), | |
| 203 | + h264: vk::VideoDecodeH264ProfileInfoKHR::builder() | |
| 204 | + .std_profile_idc(vk::video::StdVideoH264ProfileIdc(idc as i32)) | |
| 205 | + .picture_layout(vk::VideoDecodeH264PictureLayoutFlagsKHR::PROGRESSIVE) | |
| 206 | + .build(), | |
| 207 | + }); | |
| 208 | + p.info.next = &p.h264 as *const _ as *const c_void; | |
| 209 | + p | |
| 210 | + } | |
| 211 | +} | |
| 212 | + | |
| 213 | +/// An H.264 Vulkan Video decode session: NV12 DPB images per slot and a separate NV12 output image. | |
| 214 | +pub struct H264DecodeSession { | |
| 215 | + gpu: Gpu, | |
| 216 | + family: u32, | |
| 217 | + queue: vk::Queue, | |
| 218 | + graphics: u32, | |
| 219 | + graphics_queue: vk::Queue, | |
| 220 | + video_session: vk::VideoSessionKHR, | |
| 221 | + session_memories: Vec<vk::DeviceMemory>, | |
| 222 | + params: Option<vk::VideoSessionParametersKHR>, | |
| 223 | + sps: Vec<H264Sps>, | |
| 224 | + max_extent: vk::Extent2D, | |
| 225 | + max_dpb_slots: u32, | |
| 226 | + max_active_refs: u32, | |
| 227 | + size_align: u64, | |
| 228 | + dpb: Vec<Image>, | |
| 229 | + output: Image, | |
| 230 | + bitstream: Buffer, | |
| 231 | + readback: Buffer, | |
| 232 | + decode_pool: vk::CommandPool, | |
| 233 | + decode_cb: vk::CommandBuffer, | |
| 234 | + graphics_pool: vk::CommandPool, | |
| 235 | + graphics_cb: vk::CommandBuffer, | |
| 236 | + fence: vk::Fence, | |
| 237 | + coded: bool, | |
| 238 | +} | |
| 239 | + | |
| 240 | +impl H264DecodeSession { | |
| 241 | + /// Creates a session with its DPB, bitstream and readback buffers. | |
| 242 | + pub fn new(gpu: &Gpu, cfg: &H264DecodeConfig) -> Result<Self, Error> { | |
| 243 | + if !matches!(cfg.profile_idc, 66 | 77 | 100) { | |
| 244 | + return Err(Error::Invalid(format!("H.264 profile_idc {}", cfg.profile_idc))); | |
| 245 | + } | |
| 246 | + if cfg.max_width == 0 || cfg.max_height == 0 || cfg.max_dpb_slots == 0 { | |
| 247 | + return Err(Error::Invalid("decode size or DPB slot count is zero".into())); | |
| 248 | + } | |
| 249 | + let family = gpu | |
| 250 | + .decode_family() | |
| 251 | + .ok_or_else(|| Error::Unsupported("H.264 decode queue family".into()))?; | |
| 252 | + let graphics = gpu | |
| 253 | + .graphics_family() | |
| 254 | + .ok_or_else(|| Error::Unsupported("graphics queue family for readback".into()))?; | |
| 255 | + if !gpu | |
| 256 | + .device() | |
| 257 | + .extensions() | |
| 258 | + .contains(&vk::KHR_VIDEO_DECODE_H264_EXTENSION.name) | |
| 259 | + { | |
| 260 | + return Err(Error::Unsupported("H.264 decode".into())); | |
| 261 | + } | |
| 262 | + let width = align(cfg.max_width, 16); | |
| 263 | + let height = align(cfg.max_height, 16); | |
| 264 | + let profile = Profile::new(cfg.profile_idc); | |
| 265 | + let caps = query_caps(gpu, &profile.info)?; | |
| 266 | + if width > caps.max_extent.width | |
| 267 | + || height > caps.max_extent.height | |
| 268 | + || cfg.max_dpb_slots > caps.max_dpb_slots | |
| 269 | + { | |
| 270 | + return Err(Error::Unsupported(format!("H.264 decode {width}x{height}"))); | |
| 271 | + } | |
| 272 | + if !caps.distinct { | |
| 273 | + return Err(Error::Unsupported( | |
| 274 | + "H.264 decode with DPB and output in one image".into(), | |
| 275 | + )); | |
| 276 | + } | |
| 277 | + let max_active = caps.max_active_refs.min(cfg.max_dpb_slots); | |
| 278 | + | |
| 279 | + let queue = unsafe { gpu.device().get_device_queue(family, 0) }; | |
| 280 | + let graphics_queue = unsafe { gpu.device().get_device_queue(graphics, 0) }; | |
| 281 | + let mut s = H264DecodeSession { | |
| 282 | + gpu: gpu.clone(), | |
| 283 | + family, | |
| 284 | + queue, | |
| 285 | + graphics, | |
| 286 | + graphics_queue, | |
| 287 | + video_session: Default::default(), | |
| 288 | + session_memories: Vec::new(), | |
| 289 | + params: None, | |
| 290 | + sps: Vec::new(), | |
| 291 | + max_extent: vk::Extent2D { width, height }, | |
| 292 | + max_dpb_slots: cfg.max_dpb_slots, | |
| 293 | + max_active_refs: max_active, | |
| 294 | + size_align: caps.size_align, | |
| 295 | + dpb: Vec::new(), | |
| 296 | + output: Default::default(), | |
| 297 | + bitstream: Default::default(), | |
| 298 | + readback: Default::default(), | |
| 299 | + decode_pool: Default::default(), | |
| 300 | + decode_cb: Default::default(), | |
| 301 | + graphics_pool: Default::default(), | |
| 302 | + graphics_cb: Default::default(), | |
| 303 | + fence: Default::default(), | |
| 304 | + coded: false, | |
| 305 | + }; | |
| 306 | + s.create_session(&profile, &caps, max_active)?; | |
| 307 | + s.create_resources(&profile, &caps)?; | |
| 308 | + s.init_dpb()?; | |
| 309 | + Ok(s) | |
| 310 | + } | |
| 311 | + | |
| 312 | + /// Replaces the session parameters with the given SPS and PPS sets. | |
| 313 | + pub fn set_parameters(&mut self, sps: &[H264Sps], pps: &[H264Pps]) -> Result<(), Error> { | |
| 314 | + let gpu = self.gpu.clone(); | |
| 315 | + let d = gpu.device(); | |
| 316 | + let offsets: Vec<Vec<i32>> = sps.iter().map(|s| s.offset_for_ref_frame.clone()).collect(); | |
| 317 | + let sps_lists: Vec<Option<Box<vk::video::StdVideoH264ScalingLists>>> = sps | |
| 318 | + .iter() | |
| 319 | + .map(|s| s.seq_scaling_matrix.as_ref().map(|l| Box::new(std_lists(l)))) | |
| 320 | + .collect(); | |
| 321 | + let pps_lists: Vec<Option<Box<vk::video::StdVideoH264ScalingLists>>> = pps | |
| 322 | + .iter() | |
| 323 | + .map(|p| p.pic_scaling_matrix.as_ref().map(|l| Box::new(std_lists(l)))) | |
| 324 | + .collect(); | |
| 325 | + let std_sps = sps | |
| 326 | + .iter() | |
| 327 | + .zip(&offsets) | |
| 328 | + .zip(&sps_lists) | |
| 329 | + .map(|((s, o), l)| std_sps(s, o, l.as_deref())) | |
| 330 | + .collect::<Result<Vec<_>, Error>>()?; | |
| 331 | + let std_pps: Vec<vk::video::StdVideoH264PictureParameterSet> = pps | |
| 332 | + .iter() | |
| 333 | + .zip(&pps_lists) | |
| 334 | + .map(|(p, l)| std_pps(p, l.as_deref())) | |
| 335 | + .collect(); | |
| 336 | + | |
| 337 | + let add = vk::VideoDecodeH264SessionParametersAddInfoKHR::builder() | |
| 338 | + .std_sp_ss(&std_sps) | |
| 339 | + .std_pp_ss(&std_pps) | |
| 340 | + .build(); | |
| 341 | + let mut h264 = vk::VideoDecodeH264SessionParametersCreateInfoKHR::builder() | |
| 342 | + .max_std_sps_count(sps.len().max(1) as u32) | |
| 343 | + .max_std_pps_count(pps.len().max(1) as u32) | |
| 344 | + .parameters_add_info(&add) | |
| 345 | + .build(); | |
| 346 | + let info = vk::VideoSessionParametersCreateInfoKHR::builder() | |
| 347 | + .video_session(self.video_session) | |
| 348 | + .push_next(&mut h264) | |
| 349 | + .build(); | |
| 350 | + let params = unsafe { | |
| 351 | + vk( | |
| 352 | + d.create_video_session_parameters_khr(&info, None), | |
| 353 | + "vkCreateVideoSessionParametersKHR", | |
| 354 | + )? | |
| 355 | + }; | |
| 356 | + if let Some(old) = self.params.replace(params) { | |
| 357 | + unsafe { d.destroy_video_session_parameters_khr(old, None) }; | |
| 358 | + } | |
| 359 | + self.sps = sps.to_vec(); | |
| 360 | + Ok(()) | |
| 361 | + } | |
| 362 | + | |
| 363 | + /// Decodes one picture into its setup slot, referencing the given DPB slots. | |
| 364 | + pub fn decode(&mut self, pic: &H264DecodePicture) -> Result<(), Error> { | |
| 365 | + let params = self | |
| 366 | + .params | |
| 367 | + .ok_or_else(|| Error::Invalid("session parameters not set".into()))?; | |
| 368 | + let sps = self | |
| 369 | + .sps | |
| 370 | + .iter() | |
| 371 | + .find(|s| s.id == pic.sps_id) | |
| 372 | + .ok_or_else(|| Error::Invalid(format!("unknown SPS {}", pic.sps_id)))?; | |
| 373 | + let extent = coded_extent(sps); | |
| 374 | + if extent.width > self.max_extent.width || extent.height > self.max_extent.height { | |
| 375 | + return Err(Error::Invalid("picture larger than session".into())); | |
| 376 | + } | |
| 377 | + if pic.setup_slot >= self.max_dpb_slots { | |
| 378 | + return Err(Error::Invalid("setup DPB slot".into())); | |
| 379 | + } | |
| 380 | + if pic.refs.len() > self.max_active_refs as usize { | |
| 381 | + return Err(Error::Invalid("too many reference pictures".into())); | |
| 382 | + } | |
| 383 | + if pic | |
| 384 | + .refs | |
| 385 | + .iter() | |
| 386 | + .any(|r| r.slot >= self.max_dpb_slots || r.slot == pic.setup_slot) | |
| 387 | + { | |
| 388 | + return Err(Error::Invalid("reference DPB slot".into())); | |
| 389 | + } | |
| 390 | + if pic.slices.is_empty() { | |
| 391 | + return Err(Error::Invalid("no slices".into())); | |
| 392 | + } | |
| 393 | + | |
| 394 | + let total: usize = pic.slices.iter().map(|s| 3 + s.len()).sum(); | |
| 395 | + if total as u64 > self.bitstream.size { | |
| 396 | + return Err(Error::Invalid("slices larger than bitstream buffer".into())); | |
| 397 | + } | |
| 398 | + let ptr = self | |
| 399 | + .bitstream | |
| 400 | + .ptr | |
| 401 | + .ok_or_else(|| Error::Invalid("bitstream buffer not mapped".into()))?; | |
| 402 | + let buf = unsafe { std::slice::from_raw_parts_mut(ptr, self.bitstream.size as usize) }; | |
| 403 | + let mut offsets = Vec::with_capacity(pic.slices.len()); | |
| 404 | + let mut at = 0usize; | |
| 405 | + for slice in pic.slices { | |
| 406 | + offsets.push(at as u32); | |
| 407 | + buf[at..at + 3].copy_from_slice(&[0, 0, 1]); | |
| 408 | + buf[at + 3..at + 3 + slice.len()].copy_from_slice(slice); | |
| 409 | + at += 3 + slice.len(); | |
| 410 | + } | |
| 411 | + let range = align_u64(at as u64, self.size_align); | |
| 412 | + | |
| 413 | + let gpu = self.gpu.clone(); | |
| 414 | + let d = gpu.device(); | |
| 415 | + let cb = self.decode_cb; | |
| 416 | + let pr: Vec<vk::VideoPictureResourceInfoKHR> = self | |
| 417 | + .dpb | |
| 418 | + .iter() | |
| 419 | + .map(|i| picture_resource(i.view, extent)) | |
| 420 | + .collect(); | |
| 421 | + | |
| 422 | + let setup_std = ref_info(pic.frame_num, pic.poc, false, false); | |
| 423 | + let mut setup_dpb = vk::VideoDecodeH264DpbSlotInfoKHR::builder() | |
| 424 | + .std_reference_info(&setup_std) | |
| 425 | + .build(); | |
| 426 | + let setup = vk::VideoReferenceSlotInfoKHR::builder() | |
| 427 | + .slot_index(pic.setup_slot as i32) | |
| 428 | + .picture_resource(&pr[pic.setup_slot as usize]) | |
| 429 | + .push_next(&mut setup_dpb) | |
| 430 | + .build(); | |
| 431 | + | |
| 432 | + let ref_std: Vec<vk::video::StdVideoDecodeH264ReferenceInfo> = pic | |
| 433 | + .refs | |
| 434 | + .iter() | |
| 435 | + .map(|r| ref_info(r.frame_num, r.poc, r.long_term, r.non_existing)) | |
| 436 | + .collect(); | |
| 437 | + let mut ref_dpb: Vec<vk::VideoDecodeH264DpbSlotInfoKHR> = ref_std | |
| 438 | + .iter() | |
| 439 | + .map(|s| vk::VideoDecodeH264DpbSlotInfoKHR::builder().std_reference_info(s).build()) | |
| 440 | + .collect(); | |
| 441 | + let refs: Vec<vk::VideoReferenceSlotInfoKHR> = pic | |
| 442 | + .refs | |
| 443 | + .iter() | |
| 444 | + .zip(ref_dpb.iter_mut()) | |
| 445 | + .map(|(r, dpb)| { | |
| 446 | + vk::VideoReferenceSlotInfoKHR::builder() | |
| 447 | + .slot_index(r.slot as i32) | |
| 448 | + .picture_resource(&pr[r.slot as usize]) | |
| 449 | + .push_next(dpb) | |
| 450 | + .build() | |
| 451 | + }) | |
| 452 | + .collect(); | |
| 453 | + | |
| 454 | + let mut begin: Vec<vk::VideoReferenceSlotInfoKHR> = vec![ | |
| 455 | + vk::VideoReferenceSlotInfoKHR::builder() | |
| 456 | + .slot_index(-1) | |
| 457 | + .picture_resource(&pr[pic.setup_slot as usize]) | |
| 458 | + .build(), | |
| 459 | + ]; | |
| 460 | + begin.extend(pic.refs.iter().map(|r| { | |
| 461 | + vk::VideoReferenceSlotInfoKHR::builder() | |
| 462 | + .slot_index(r.slot as i32) | |
| 463 | + .picture_resource(&pr[r.slot as usize]) | |
| 464 | + .build() | |
| 465 | + })); | |
| 466 | + | |
| 467 | + let mut std_pic = zeroed::<vk::video::StdVideoDecodeH264PictureInfo>(); | |
| 468 | + std_pic.flags.set_is_intra(pic.idr as u32); | |
| 469 | + std_pic.flags.set_IdrPicFlag(pic.idr as u32); | |
| 470 | + std_pic.flags.set_is_reference(pic.reference as u32); | |
| 471 | + std_pic.seq_parameter_set_id = pic.sps_id; | |
| 472 | + std_pic.pic_parameter_set_id = pic.pps_id; | |
| 473 | + std_pic.frame_num = pic.frame_num; | |
| 474 | + std_pic.idr_pic_id = pic.idr_pic_id; | |
| 475 | + std_pic.PicOrderCnt = pic.poc; | |
| 476 | + | |
| 477 | + let mut h264 = vk::VideoDecodeH264PictureInfoKHR::builder() | |
| 478 | + .std_picture_info(&std_pic) | |
| 479 | + .slice_offsets(&offsets) | |
| 480 | + .build(); | |
| 481 | + let decode = vk::VideoDecodeInfoKHR::builder() | |
| 482 | + .src_buffer(self.bitstream.buffer) | |
| 483 | + .src_buffer_offset(0) | |
| 484 | + .src_buffer_range(range) | |
| 485 | + .dst_picture_resource(picture_resource(self.output.view, extent)) | |
| 486 | + .setup_reference_slot(&setup) | |
| 487 | + .reference_slots(&refs) | |
| 488 | + .push_next(&mut h264) | |
| 489 | + .build(); | |
| 490 | + let begin_info = vk::VideoBeginCodingInfoKHR::builder() | |
| 491 | + .video_session(self.video_session) | |
| 492 | + .video_session_parameters(params) | |
| 493 | + .reference_slots(&begin) | |
| 494 | + .build(); | |
| 495 | + let reset = !self.coded; | |
| 496 | + | |
| 497 | + unsafe { | |
| 498 | + let info = vk::CommandBufferBeginInfo::builder() | |
| 499 | + .flags(vk::CommandBufferUsageFlags::ONE_TIME_SUBMIT); | |
| 500 | + vk(d.begin_command_buffer(cb, &info), "vkBeginCommandBuffer")?; | |
| 501 | + barrier( | |
| 502 | + d, | |
| 503 | + cb, | |
| 504 | + self.output.image, | |
| 505 | + ( | |
| 506 | + vk::ImageLayout::UNDEFINED, | |
| 507 | + vk::PipelineStageFlags2::NONE, | |
| 508 | + vk::AccessFlags2::NONE, | |
| 509 | + ), | |
| 510 | + ( | |
| 511 | + vk::ImageLayout::VIDEO_DECODE_DST_KHR, | |
| 512 | + vk::PipelineStageFlags2::ALL_COMMANDS, | |
| 513 | + vk::AccessFlags2::NONE, | |
| 514 | + ), | |
| 515 | + ); | |
| 516 | + d.cmd_begin_video_coding_khr(cb, &begin_info); | |
| 517 | + if reset { | |
| 518 | + let control = vk::VideoCodingControlInfoKHR::builder() | |
| 519 | + .flags(vk::VideoCodingControlFlagsKHR::RESET) | |
| 520 | + .build(); | |
| 521 | + d.cmd_control_video_coding_khr(cb, &control); | |
| 522 | + } | |
| 523 | + d.cmd_decode_video_khr(cb, &decode); | |
| 524 | + let end = vk::VideoEndCodingInfoKHR::builder().build(); | |
| 525 | + d.cmd_end_video_coding_khr(cb, &end); | |
| 526 | + vk(d.end_command_buffer(cb), "vkEndCommandBuffer")?; | |
| 527 | + } | |
| 528 | + submit(d, self.fence, self.queue, cb)?; | |
| 529 | + self.coded = true; | |
| 530 | + Ok(()) | |
| 531 | + } | |
| 532 | + | |
| 533 | + /// Copies the top-left width x height of the last decoded picture out as NV12 planes (Y, interleaved UV). | |
| 534 | + pub fn read_output(&mut self, width: u32, height: u32) -> Result<(Vec<u8>, Vec<u8>), Error> { | |
| 535 | + if !self.coded { | |
| 536 | + return Err(Error::Invalid("nothing decoded".into())); | |
| 537 | + } | |
| 538 | + if width == 0 | |
| 539 | + || height == 0 | |
| 540 | + || width % 2 != 0 | |
| 541 | + || height % 2 != 0 | |
| 542 | + || width > self.max_extent.width | |
| 543 | + || height > self.max_extent.height | |
| 544 | + { | |
| 545 | + return Err(Error::Invalid("readback size".into())); | |
| 546 | + } | |
| 547 | + let gpu = self.gpu.clone(); | |
| 548 | + let d = gpu.device(); | |
| 549 | + let cb = self.graphics_cb; | |
| 550 | + let image = self.output.image; | |
| 551 | + let luma = width as usize * height as usize; | |
| 552 | + let copies = [ | |
| 553 | + copy_region(0, PLANE_0, width, height), | |
| 554 | + copy_region(luma as u64, PLANE_1, width / 2, height / 2), | |
| 555 | + ]; | |
| 556 | + unsafe { | |
| 557 | + let info = vk::CommandBufferBeginInfo::builder() | |
| 558 | + .flags(vk::CommandBufferUsageFlags::ONE_TIME_SUBMIT); | |
| 559 | + vk(d.begin_command_buffer(cb, &info), "vkBeginCommandBuffer")?; | |
| 560 | + barrier( | |
| 561 | + d, | |
| 562 | + cb, | |
| 563 | + image, | |
| 564 | + ( | |
| 565 | + vk::ImageLayout::VIDEO_DECODE_DST_KHR, | |
| 566 | + vk::PipelineStageFlags2::ALL_COMMANDS, | |
| 567 | + vk::AccessFlags2::NONE, | |
| 568 | + ), | |
| 569 | + ( | |
| 570 | + vk::ImageLayout::TRANSFER_SRC_OPTIMAL, | |
| 571 | + vk::PipelineStageFlags2::COPY, | |
| 572 | + vk::AccessFlags2::TRANSFER_READ, | |
| 573 | + ), | |
| 574 | + ); | |
| 575 | + d.cmd_copy_image_to_buffer( | |
| 576 | + cb, | |
| 577 | + image, | |
| 578 | + vk::ImageLayout::TRANSFER_SRC_OPTIMAL, | |
| 579 | + self.readback.buffer, | |
| 580 | + &copies, | |
| 581 | + ); | |
| 582 | + barrier( | |
| 583 | + d, | |
| 584 | + cb, | |
| 585 | + image, | |
| 586 | + ( | |
| 587 | + vk::ImageLayout::TRANSFER_SRC_OPTIMAL, | |
| 588 | + vk::PipelineStageFlags2::COPY, | |
| 589 | + vk::AccessFlags2::TRANSFER_READ, | |
| 590 | + ), | |
| 591 | + ( | |
| 592 | + vk::ImageLayout::VIDEO_DECODE_DST_KHR, | |
| 593 | + vk::PipelineStageFlags2::ALL_COMMANDS, | |
| 594 | + vk::AccessFlags2::NONE, | |
| 595 | + ), | |
| 596 | + ); | |
| 597 | + vk(d.end_command_buffer(cb), "vkEndCommandBuffer")?; | |
| 598 | + } | |
| 599 | + submit(d, self.fence, self.graphics_queue, cb)?; | |
| 600 | + let ptr = self | |
| 601 | + .readback | |
| 602 | + .ptr | |
| 603 | + .ok_or_else(|| Error::Invalid("readback buffer not mapped".into()))?; | |
| 604 | + let bytes = unsafe { std::slice::from_raw_parts(ptr, luma * 3 / 2) }; | |
| 605 | + Ok((bytes[..luma].to_vec(), bytes[luma..].to_vec())) | |
| 606 | + } | |
| 607 | + | |
| 608 | + fn create_session(&mut self, profile: &Profile, caps: &Caps, active: u32) -> Result<(), Error> { | |
| 609 | + let gpu = self.gpu.clone(); | |
| 610 | + let d = gpu.device(); | |
| 611 | + let info = vk::VideoSessionCreateInfoKHR::builder() | |
| 612 | + .queue_family_index(self.family) | |
| 613 | + .video_profile(&profile.info) | |
| 614 | + .picture_format(NV12) | |
| 615 | + .max_coded_extent(self.max_extent) | |
| 616 | + .reference_picture_format(NV12) | |
| 617 | + .max_dpb_slots(self.max_dpb_slots) | |
| 618 | + .max_active_reference_pictures(active) | |
| 619 | + .std_header_version(&caps.std_header_version) | |
| 620 | + .build(); | |
| 621 | + self.video_session = unsafe { | |
| 622 | + vk( | |
| 623 | + d.create_video_session_khr(&info, None), | |
| 624 | + "vkCreateVideoSessionKHR", | |
| 625 | + )? | |
| 626 | + }; | |
| 627 | + let reqs = unsafe { | |
| 628 | + vk( | |
| 629 | + d.get_video_session_memory_requirements_khr(self.video_session), | |
| 630 | + "vkGetVideoSessionMemoryRequirementsKHR", | |
| 631 | + )? | |
| 632 | + }; | |
| 633 | + let mut binds = Vec::new(); | |
| 634 | + for r in &reqs { | |
| 635 | + let mem = allocate(&gpu, r.memory_requirements, vk::MemoryPropertyFlags::DEVICE_LOCAL)?; | |
| 636 | + self.session_memories.push(mem); | |
| 637 | + binds.push( | |
| 638 | + vk::BindVideoSessionMemoryInfoKHR::builder() | |
| 639 | + .memory_bind_index(r.memory_bind_index) | |
| 640 | + .memory(mem) | |
| 641 | + .memory_size(r.memory_requirements.size) | |
| 642 | + .build(), | |
| 643 | + ); | |
| 644 | + } | |
| 645 | + unsafe { | |
| 646 | + vk( | |
| 647 | + d.bind_video_session_memory_khr(self.video_session, &binds), | |
| 648 | + "vkBindVideoSessionMemoryKHR", | |
| 649 | + ) | |
| 650 | + } | |
| 651 | + } | |
| 652 | + | |
| 653 | + fn create_resources(&mut self, profile: &Profile, caps: &Caps) -> Result<(), Error> { | |
| 654 | + let gpu = self.gpu.clone(); | |
| 655 | + let d = gpu.device(); | |
| 656 | + let mut list = vk::VideoProfileListInfoKHR::builder() | |
| 657 | + .profiles(std::slice::from_ref(&profile.info)) | |
| 658 | + .build(); | |
| 659 | + let width = self.max_extent.width; | |
| 660 | + let height = self.max_extent.height; | |
| 661 | + let families = if self.family == self.graphics { | |
| 662 | + vec![self.family] | |
| 663 | + } else { | |
| 664 | + vec![self.family, self.graphics] | |
| 665 | + }; | |
| 666 | + let dpb = vk::ImageUsageFlags::VIDEO_DECODE_DPB_KHR; | |
| 667 | + for _ in 0..self.max_dpb_slots { | |
| 668 | + let image = new_image(&gpu, width, height, dpb, dpb, &[self.family], &mut list)?; | |
| 669 | + self.dpb.push(image); | |
| 670 | + } | |
| 671 | + let output = vk::ImageUsageFlags::VIDEO_DECODE_DST_KHR | vk::ImageUsageFlags::TRANSFER_SRC; | |
| 672 | + self.output = new_image( | |
| 673 | + &gpu, | |
| 674 | + width, | |
| 675 | + height, | |
| 676 | + output, | |
| 677 | + vk::ImageUsageFlags::VIDEO_DECODE_DST_KHR, | |
| 678 | + &families, | |
| 679 | + &mut list, | |
| 680 | + )?; | |
| 681 | + | |
| 682 | + let bitstream_size = align_u64(width as u64 * height as u64 * 3 + 65536, caps.size_align); | |
| 683 | + self.bitstream = new_buffer( | |
| 684 | + &gpu, | |
| 685 | + bitstream_size, | |
| 686 | + vk::BufferUsageFlags::VIDEO_DECODE_SRC_KHR, | |
| 687 | + vk::MemoryPropertyFlags::HOST_VISIBLE | vk::MemoryPropertyFlags::HOST_COHERENT, | |
| 688 | + Some(&mut list), | |
| 689 | + )?; | |
| 690 | + self.readback = new_buffer( | |
| 691 | + &gpu, | |
| 692 | + width as u64 * height as u64 * 3 / 2, | |
| 693 | + vk::BufferUsageFlags::TRANSFER_DST, | |
| 694 | + vk::MemoryPropertyFlags::HOST_VISIBLE | vk::MemoryPropertyFlags::HOST_COHERENT, | |
| 695 | + None, | |
| 696 | + )?; | |
| 697 | + | |
| 698 | + self.decode_pool = create_pool(d, self.family)?; | |
| 699 | + self.decode_cb = alloc_cb(d, self.decode_pool)?; | |
| 700 | + self.graphics_pool = create_pool(d, self.graphics)?; | |
| 701 | + self.graphics_cb = alloc_cb(d, self.graphics_pool)?; | |
| 702 | + self.fence = unsafe { | |
| 703 | + vk( | |
| 704 | + d.create_fence(&vk::FenceCreateInfo::builder().build(), None), | |
| 705 | + "vkCreateFence", | |
| 706 | + )? | |
| 707 | + }; | |
| 708 | + Ok(()) | |
| 709 | + } | |
| 710 | + | |
| 711 | + fn init_dpb(&mut self) -> Result<(), Error> { | |
| 712 | + let gpu = self.gpu.clone(); | |
| 713 | + let d = gpu.device(); | |
| 714 | + let cb = self.decode_cb; | |
| 715 | + unsafe { | |
| 716 | + let info = vk::CommandBufferBeginInfo::builder() | |
| 717 | + .flags(vk::CommandBufferUsageFlags::ONE_TIME_SUBMIT); | |
| 718 | + vk(d.begin_command_buffer(cb, &info), "vkBeginCommandBuffer")?; | |
| 719 | + for img in &self.dpb { | |
| 720 | + barrier( | |
| 721 | + d, | |
| 722 | + cb, | |
| 723 | + img.image, | |
| 724 | + ( | |
| 725 | + vk::ImageLayout::UNDEFINED, | |
| 726 | + vk::PipelineStageFlags2::NONE, | |
| 727 | + vk::AccessFlags2::NONE, | |
| 728 | + ), | |
| 729 | + ( | |
| 730 | + vk::ImageLayout::VIDEO_DECODE_DPB_KHR, | |
| 731 | + vk::PipelineStageFlags2::ALL_COMMANDS, | |
| 732 | + vk::AccessFlags2::NONE, | |
| 733 | + ), | |
| 734 | + ); | |
| 735 | + } | |
| 736 | + vk(d.end_command_buffer(cb), "vkEndCommandBuffer")?; | |
| 737 | + } | |
| 738 | + submit(d, self.fence, self.queue, cb) | |
| 739 | + } | |
| 740 | +} | |
| 741 | + | |
| 742 | +impl Drop for H264DecodeSession { | |
| 743 | + fn drop(&mut self) { | |
| 744 | + let gpu = self.gpu.clone(); | |
| 745 | + let d = gpu.device(); | |
| 746 | + unsafe { | |
| 747 | + let _ = d.device_wait_idle(); | |
| 748 | + d.destroy_fence(self.fence, None); | |
| 749 | + d.destroy_command_pool(self.decode_pool, None); | |
| 750 | + d.destroy_command_pool(self.graphics_pool, None); | |
| 751 | + for img in self.dpb.iter().chain(std::iter::once(&self.output)) { | |
| 752 | + d.destroy_image_view(img.view, None); | |
| 753 | + d.destroy_image(img.image, None); | |
| 754 | + d.free_memory(img.memory, None); | |
| 755 | + } | |
| 756 | + for buf in [&self.bitstream, &self.readback] { | |
| 757 | + d.destroy_buffer(buf.buffer, None); | |
| 758 | + d.free_memory(buf.memory, None); | |
| 759 | + } | |
| 760 | + if let Some(params) = self.params { | |
| 761 | + d.destroy_video_session_parameters_khr(params, None); | |
| 762 | + } | |
| 763 | + d.destroy_video_session_khr(self.video_session, None); | |
| 764 | + for m in &self.session_memories { | |
| 765 | + d.free_memory(*m, None); | |
| 766 | + } | |
| 767 | + } | |
| 768 | + } | |
| 769 | +} | |
| 770 | + | |
| 771 | +fn query_caps(gpu: &Gpu, profile: &vk::VideoProfileInfoKHR) -> Result<Caps, Error> { | |
| 772 | + let mut h264 = vk::VideoDecodeH264CapabilitiesKHR::builder(); | |
| 773 | + let mut decode = vk::VideoDecodeCapabilitiesKHR::builder(); | |
| 774 | + let mut caps = vk::VideoCapabilitiesKHR::builder() | |
| 775 | + .push_next(&mut h264) | |
| 776 | + .push_next(&mut decode); | |
| 777 | + unsafe { | |
| 778 | + vk( | |
| 779 | + gpu.instance().get_physical_device_video_capabilities_khr( | |
| 780 | + gpu.physical(), | |
| 781 | + profile, | |
| 782 | + &mut caps, | |
| 783 | + ), | |
| 784 | + "vkGetPhysicalDeviceVideoCapabilitiesKHR", | |
| 785 | + )?; | |
| 786 | + } | |
| 787 | + let caps = caps.build(); | |
| 788 | + let decode = decode.build(); | |
| 789 | + Ok(Caps { | |
| 790 | + std_header_version: caps.std_header_version, | |
| 791 | + max_extent: caps.max_coded_extent, | |
| 792 | + size_align: caps.min_bitstream_buffer_size_alignment, | |
| 793 | + max_dpb_slots: caps.max_dpb_slots, | |
| 794 | + max_active_refs: caps.max_active_reference_pictures, | |
| 795 | + distinct: decode | |
| 796 | + .flags | |
| 797 | + .contains(vk::VideoDecodeCapabilityFlagsKHR::DPB_AND_OUTPUT_DISTINCT), | |
| 798 | + }) | |
| 799 | +} | |
| 800 | + | |
| 801 | +fn submit( | |
| 802 | + d: &Device, | |
| 803 | + fence: vk::Fence, | |
| 804 | + queue: vk::Queue, | |
| 805 | + cb: vk::CommandBuffer, | |
| 806 | +) -> Result<(), Error> { | |
| 807 | + let cbs = [cb]; | |
| 808 | + let submit = vk::SubmitInfo::builder().command_buffers(&cbs).build(); | |
| 809 | + unsafe { | |
| 810 | + vk(d.reset_fences(&[fence]), "vkResetFences")?; | |
| 811 | + vk(d.queue_submit(queue, &[submit], fence), "vkQueueSubmit")?; | |
| 812 | + vk( | |
| 813 | + d.wait_for_fences(&[fence], true, u64::MAX), | |
| 814 | + "vkWaitForFences", | |
| 815 | + )?; | |
| 816 | + } | |
| 817 | + Ok(()) | |
| 818 | +} | |
| 819 | + | |
| 820 | +fn allocate( | |
| 821 | + gpu: &Gpu, | |
| 822 | + req: vk::MemoryRequirements, | |
| 823 | + flags: vk::MemoryPropertyFlags, | |
| 824 | +) -> Result<vk::DeviceMemory, Error> { | |
| 825 | + let index = memory_type(gpu, req.memory_type_bits, flags)?; | |
| 826 | + let info = vk::MemoryAllocateInfo::builder() | |
| 827 | + .allocation_size(req.size) | |
| 828 | + .memory_type_index(index) | |
| 829 | + .build(); | |
| 830 | + unsafe { | |
| 831 | + vk( | |
| 832 | + gpu.device().allocate_memory(&info, None), | |
| 833 | + "vkAllocateMemory", | |
| 834 | + ) | |
| 835 | + } | |
| 836 | +} | |
| 837 | + | |
| 838 | +fn new_image( | |
| 839 | + gpu: &Gpu, | |
| 840 | + width: u32, | |
| 841 | + height: u32, | |
| 842 | + usage: vk::ImageUsageFlags, | |
| 843 | + view_usage: vk::ImageUsageFlags, | |
| 844 | + families: &[u32], | |
| 845 | + list: &mut vk::VideoProfileListInfoKHR, | |
| 846 | +) -> Result<Image, Error> { | |
| 847 | + let info = vk::ImageCreateInfo::builder() | |
| 848 | + .image_type(vk::ImageType::_2D) | |
| 849 | + .format(NV12) | |
| 850 | + .extent(vk::Extent3D { | |
| 851 | + width, | |
| 852 | + height, | |
| 853 | + depth: 1, | |
| 854 | + }) | |
| 855 | + .mip_levels(1) | |
| 856 | + .array_layers(1) | |
| 857 | + .samples(vk::SampleCountFlags::_1) | |
| 858 | + .tiling(vk::ImageTiling::OPTIMAL) | |
| 859 | + .usage(usage) | |
| 860 | + .initial_layout(vk::ImageLayout::UNDEFINED); | |
| 861 | + let info = if families.len() > 1 { | |
| 862 | + info.sharing_mode(vk::SharingMode::CONCURRENT) | |
| 863 | + .queue_family_indices(families) | |
| 864 | + } else { | |
| 865 | + info.sharing_mode(vk::SharingMode::EXCLUSIVE) | |
| 866 | + }; | |
| 867 | + let info = info.push_next(list).build(); | |
| 868 | + let d = gpu.device(); | |
| 869 | + let image = unsafe { vk(d.create_image(&info, None), "vkCreateImage")? }; | |
| 870 | + let req = unsafe { d.get_image_memory_requirements(image) }; | |
| 871 | + let memory = allocate(gpu, req, vk::MemoryPropertyFlags::DEVICE_LOCAL)?; | |
| 872 | + unsafe { vk(d.bind_image_memory(image, memory, 0), "vkBindImageMemory")? }; | |
| 873 | + let mut usage_info = vk::ImageViewUsageCreateInfo::builder() | |
| 874 | + .usage(view_usage) | |
| 875 | + .build(); | |
| 876 | + let view_info = vk::ImageViewCreateInfo::builder() | |
| 877 | + .image(image) | |
| 878 | + .view_type(vk::ImageViewType::_2D) | |
| 879 | + .format(NV12) | |
| 880 | + .subresource_range(color_range()) | |
| 881 | + .push_next(&mut usage_info) | |
| 882 | + .build(); | |
| 883 | + let view = unsafe { vk(d.create_image_view(&view_info, None), "vkCreateImageView")? }; | |
| 884 | + Ok(Image { | |
| 885 | + image, | |
| 886 | + view, | |
| 887 | + memory, | |
| 888 | + }) | |
| 889 | +} | |
| 890 | + | |
| 891 | +fn new_buffer( | |
| 892 | + gpu: &Gpu, | |
| 893 | + size: u64, | |
| 894 | + usage: vk::BufferUsageFlags, | |
| 895 | + flags: vk::MemoryPropertyFlags, | |
| 896 | + list: Option<&mut vk::VideoProfileListInfoKHR>, | |
| 897 | +) -> Result<Buffer, Error> { | |
| 898 | + let info = vk::BufferCreateInfo::builder() | |
| 899 | + .size(size) | |
| 900 | + .usage(usage) | |
| 901 | + .sharing_mode(vk::SharingMode::EXCLUSIVE); | |
| 902 | + let info = match list { | |
| 903 | + Some(list) => info.push_next(list).build(), | |
| 904 | + None => info.build(), | |
| 905 | + }; | |
| 906 | + let d = gpu.device(); | |
| 907 | + let buffer = unsafe { vk(d.create_buffer(&info, None), "vkCreateBuffer")? }; | |
| 908 | + let req = unsafe { d.get_buffer_memory_requirements(buffer) }; | |
| 909 | + let memory = allocate(gpu, req, flags)?; | |
| 910 | + unsafe { vk(d.bind_buffer_memory(buffer, memory, 0), "vkBindBufferMemory")? }; | |
| 911 | + let ptr = unsafe { | |
| 912 | + vk( | |
| 913 | + d.map_memory(memory, 0, size, vk::MemoryMapFlags::empty()), | |
| 914 | + "vkMapMemory", | |
| 915 | + )? | |
| 916 | + }; | |
| 917 | + Ok(Buffer { | |
| 918 | + buffer, | |
| 919 | + memory, | |
| 920 | + ptr: Some(ptr as *mut u8), | |
| 921 | + size, | |
| 922 | + }) | |
| 923 | +} | |
| 924 | + | |
| 925 | +fn align_u64(value: u64, to: u64) -> u64 { | |
| 926 | + let to = to.max(1); | |
| 927 | + value.div_ceil(to) * to | |
| 928 | +} | |
| 929 | + | |
| 930 | +fn coded_extent(sps: &H264Sps) -> vk::Extent2D { | |
| 931 | + let rows = if sps.frame_mbs_only { 1 } else { 2 }; | |
| 932 | + vk::Extent2D { | |
| 933 | + width: (sps.pic_width_in_mbs_minus1 + 1) * 16, | |
| 934 | + height: (sps.pic_height_in_map_units_minus1 + 1) * 16 * rows, | |
| 935 | + } | |
| 936 | +} | |
| 937 | + | |
| 938 | +fn ref_info( | |
| 939 | + frame_num: u16, | |
| 940 | + poc: [i32; 2], | |
| 941 | + long_term: bool, | |
| 942 | + non_existing: bool, | |
| 943 | +) -> vk::video::StdVideoDecodeH264ReferenceInfo { | |
| 944 | + let mut info = zeroed::<vk::video::StdVideoDecodeH264ReferenceInfo>(); | |
| 945 | + info.flags.set_used_for_long_term_reference(long_term as u32); | |
| 946 | + info.flags.set_is_non_existing(non_existing as u32); | |
| 947 | + info.FrameNum = frame_num; | |
| 948 | + info.PicOrderCnt = poc; | |
| 949 | + info | |
| 950 | +} | |
| 951 | + | |
| 952 | +fn std_lists(l: &ScalingLists) -> vk::video::StdVideoH264ScalingLists { | |
| 953 | + vk::video::StdVideoH264ScalingLists { | |
| 954 | + scaling_list_present_mask: 0xffff, | |
| 955 | + use_default_scaling_matrix_mask: 0, | |
| 956 | + ScalingList4x4: l.l4x4, | |
| 957 | + ScalingList8x8: l.l8x8, | |
| 958 | + } | |
| 959 | +} | |
| 960 | + | |
| 961 | +fn level(idc: u8) -> Result<vk::video::StdVideoH264LevelIdc, Error> { | |
| 962 | + use vk::video::*; | |
| 963 | + let table = [ | |
| 964 | + (10, STD_VIDEO_H264_LEVEL_IDC_1_0), | |
| 965 | + (11, STD_VIDEO_H264_LEVEL_IDC_1_1), | |
| 966 | + (12, STD_VIDEO_H264_LEVEL_IDC_1_2), | |
| 967 | + (13, STD_VIDEO_H264_LEVEL_IDC_1_3), | |
| 968 | + (20, STD_VIDEO_H264_LEVEL_IDC_2_0), | |
| 969 | + (21, STD_VIDEO_H264_LEVEL_IDC_2_1), | |
| 970 | + (22, STD_VIDEO_H264_LEVEL_IDC_2_2), | |
| 971 | + (30, STD_VIDEO_H264_LEVEL_IDC_3_0), | |
| 972 | + (31, STD_VIDEO_H264_LEVEL_IDC_3_1), | |
| 973 | + (32, STD_VIDEO_H264_LEVEL_IDC_3_2), | |
| 974 | + (40, STD_VIDEO_H264_LEVEL_IDC_4_0), | |
| 975 | + (41, STD_VIDEO_H264_LEVEL_IDC_4_1), | |
| 976 | + (42, STD_VIDEO_H264_LEVEL_IDC_4_2), | |
| 977 | + (50, STD_VIDEO_H264_LEVEL_IDC_5_0), | |
| 978 | + (51, STD_VIDEO_H264_LEVEL_IDC_5_1), | |
| 979 | + (52, STD_VIDEO_H264_LEVEL_IDC_5_2), | |
| 980 | + (60, STD_VIDEO_H264_LEVEL_IDC_6_0), | |
| 981 | + (61, STD_VIDEO_H264_LEVEL_IDC_6_1), | |
| 982 | + (62, STD_VIDEO_H264_LEVEL_IDC_6_2), | |
| 983 | + ]; | |
| 984 | + table | |
| 985 | + .into_iter() | |
| 986 | + .find(|&(v, _)| v == idc) | |
| 987 | + .map(|(_, level)| level) | |
| 988 | + .ok_or_else(|| Error::Invalid(format!("H.264 level_idc {idc}"))) | |
| 989 | +} | |
| 990 | + | |
| 991 | +fn std_sps( | |
| 992 | + s: &H264Sps, | |
| 993 | + offsets: &[i32], | |
| 994 | + lists: Option<&vk::video::StdVideoH264ScalingLists>, | |
| 995 | +) -> Result<vk::video::StdVideoH264SequenceParameterSet, Error> { | |
| 996 | + let mut out = zeroed::<vk::video::StdVideoH264SequenceParameterSet>(); | |
| 997 | + out.flags.set_direct_8x8_inference_flag(s.direct_8x8_inference as u32); | |
| 998 | + out.flags.set_mb_adaptive_frame_field_flag(s.mb_adaptive_frame_field as u32); | |
| 999 | + out.flags.set_frame_mbs_only_flag(s.frame_mbs_only as u32); | |
| 1000 | + out.flags.set_delta_pic_order_always_zero_flag(s.delta_pic_order_always_zero as u32); | |
| 1001 | + out.flags.set_qpprime_y_zero_transform_bypass_flag(s.qpprime_y_zero_transform_bypass as u32); | |
| 1002 | + out.flags.set_frame_cropping_flag((s.frame_crop_offsets != [0; 4]) as u32); | |
| 1003 | + out.flags.set_seq_scaling_matrix_present_flag(lists.is_some() as u32); | |
| 1004 | + out.profile_idc = vk::video::StdVideoH264ProfileIdc(s.profile_idc as i32); | |
| 1005 | + out.level_idc = level(s.level_idc)?; | |
| 1006 | + out.chroma_format_idc = vk::video::StdVideoH264ChromaFormatIdc(s.chroma_format_idc as i32); | |
| 1007 | + out.seq_parameter_set_id = s.id; | |
| 1008 | + out.bit_depth_luma_minus8 = s.bit_depth_luma_minus8; | |
| 1009 | + out.bit_depth_chroma_minus8 = s.bit_depth_chroma_minus8; | |
| 1010 | + out.log2_max_frame_num_minus4 = s.log2_max_frame_num_minus4; | |
| 1011 | + out.pic_order_cnt_type = vk::video::StdVideoH264PocType(s.pic_order_cnt_type as i32); | |
| 1012 | + out.offset_for_non_ref_pic = s.offset_for_non_ref_pic; | |
| 1013 | + out.offset_for_top_to_bottom_field = s.offset_for_top_to_bottom_field; | |
| 1014 | + out.log2_max_pic_order_cnt_lsb_minus4 = s.log2_max_pic_order_cnt_lsb_minus4; | |
| 1015 | + out.num_ref_frames_in_pic_order_cnt_cycle = offsets.len() as u8; | |
| 1016 | + out.max_num_ref_frames = s.max_num_ref_frames; | |
| 1017 | + out.pic_width_in_mbs_minus1 = s.pic_width_in_mbs_minus1; | |
| 1018 | + out.pic_height_in_map_units_minus1 = s.pic_height_in_map_units_minus1; | |
| 1019 | + out.frame_crop_left_offset = s.frame_crop_offsets[0]; | |
| 1020 | + out.frame_crop_right_offset = s.frame_crop_offsets[1]; | |
| 1021 | + out.frame_crop_top_offset = s.frame_crop_offsets[2]; | |
| 1022 | + out.frame_crop_bottom_offset = s.frame_crop_offsets[3]; | |
| 1023 | + out.pOffsetForRefFrame = offsets.as_ptr(); | |
| 1024 | + out.pScalingLists = lists.map_or(std::ptr::null(), |l| l as *const _); | |
| 1025 | + Ok(out) | |
| 1026 | +} | |
| 1027 | + | |
| 1028 | +fn std_pps( | |
| 1029 | + p: &H264Pps, | |
| 1030 | + lists: Option<&vk::video::StdVideoH264ScalingLists>, | |
| 1031 | +) -> vk::video::StdVideoH264PictureParameterSet { | |
| 1032 | + let mut out = zeroed::<vk::video::StdVideoH264PictureParameterSet>(); | |
| 1033 | + out.flags.set_transform_8x8_mode_flag(p.transform_8x8_mode as u32); | |
| 1034 | + out.flags.set_redundant_pic_cnt_present_flag(p.redundant_pic_cnt_present as u32); | |
| 1035 | + out.flags.set_constrained_intra_pred_flag(p.constrained_intra_pred as u32); | |
| 1036 | + out.flags | |
| 1037 | + .set_deblocking_filter_control_present_flag(p.deblocking_filter_control_present as u32); | |
| 1038 | + out.flags.set_weighted_pred_flag(p.weighted_pred as u32); | |
| 1039 | + out.flags.set_bottom_field_pic_order_in_frame_present_flag( | |
| 1040 | + p.bottom_field_pic_order_in_frame_present as u32, | |
| 1041 | + ); | |
| 1042 | + out.flags.set_entropy_coding_mode_flag(p.entropy_coding_mode as u32); | |
| 1043 | + out.flags.set_pic_scaling_matrix_present_flag(lists.is_some() as u32); | |
| 1044 | + out.seq_parameter_set_id = p.sps_id; | |
| 1045 | + out.pic_parameter_set_id = p.id; | |
| 1046 | + out.num_ref_idx_l0_default_active_minus1 = p.num_ref_idx_l0_default_active_minus1; | |
| 1047 | + out.num_ref_idx_l1_default_active_minus1 = p.num_ref_idx_l1_default_active_minus1; | |
| 1048 | + out.weighted_bipred_idc = vk::video::StdVideoH264WeightedBipredIdc(p.weighted_bipred_idc as i32); | |
| 1049 | + out.pic_init_qp_minus26 = p.pic_init_qp_minus26; | |
| 1050 | + out.pic_init_qs_minus26 = p.pic_init_qs_minus26; | |
| 1051 | + out.chroma_qp_index_offset = p.chroma_qp_index_offset; | |
| 1052 | + out.second_chroma_qp_index_offset = p.second_chroma_qp_index_offset; | |
| 1053 | + out.pScalingLists = lists.map_or(std::ptr::null(), |l| l as *const _); | |
| 1054 | + out | |
| 1055 | +} |
shims/vk-video-ffi/src/lib.rsmodified+11 -0
| @@ -7,11 +7,16 @@ use std::fmt; | ||
| 7 | 7 | use std::sync::Arc; |
| 8 | 8 | |
| 9 | 9 | mod av1_encode; |
| 10 | +mod h264_decode; | |
| 10 | 11 | mod h264_encode; |
| 11 | 12 | mod h265_encode; |
| 12 | 13 | mod rgb_convert; |
| 13 | 14 | |
| 14 | 15 | pub use av1_encode::{Av1EncodeConfig, Av1EncodeSession, Av1Picture, Av1Ref}; |
| 16 | +pub use h264_decode::{ | |
| 17 | + H264DecodeConfig, H264DecodePicture, H264DecodeSession, H264Pps, H264RefSlot, H264Sps, | |
| 18 | + ScalingLists, | |
| 19 | +}; | |
| 15 | 20 | pub use h264_encode::{H264EncodeConfig, H264EncodeSession, H264Picture, H264Ref, RateControl}; |
| 16 | 21 | pub use h265_encode::{H265EncodeConfig, H265EncodeSession, H265Picture, H265Ref}; |
| 17 | 22 | |
| @@ -116,6 +121,7 @@ struct Inner { | ||
| 116 | 121 | physical: vk::PhysicalDevice, |
| 117 | 122 | name: String, |
| 118 | 123 | encode: Option<u32>, |
| 124 | + decode: Option<u32>, | |
| 119 | 125 | graphics: Option<u32>, |
| 120 | 126 | _entry: Entry, |
| 121 | 127 | } |
| @@ -231,6 +237,7 @@ impl Gpu { | ||
| 231 | 237 | physical, |
| 232 | 238 | name, |
| 233 | 239 | encode: families.encode, |
| 240 | + decode: families.decode, | |
| 234 | 241 | graphics: families.graphics, |
| 235 | 242 | _entry: entry, |
| 236 | 243 | }), |
| @@ -263,6 +270,10 @@ impl Gpu { | ||
| 263 | 270 | self.inner.encode |
| 264 | 271 | } |
| 265 | 272 | |
| 273 | + pub(crate) fn decode_family(&self) -> Option<u32> { | |
| 274 | + self.inner.decode | |
| 275 | + } | |
| 276 | + | |
| 266 | 277 | pub(crate) fn graphics_family(&self) -> Option<u32> { |
| 267 | 278 | self.inner.graphics |
| 268 | 279 | } |