use std::ffi::c_void; use vulkanalia::prelude::v1_3::*; use vulkanalia::vk::KhrVideoQueueExtensionDeviceCommands; #[cfg(target_os = "linux")] use crate::dmabuf::DmaBuf; use crate::rgb_convert::{Input, check_rgb}; use crate::session::{ quality_level, DPB_SLOTS, Encoder, VideoSession, component_bit_depth, picture_resource, validate_rate, video_caps, vk, zeroed, }; use crate::{Colour, Error, Gpu, RateControl}; const AV1_SLOTS: usize = 8; const CBR_Q_INDEX: u32 = 128; /// Settings fixed for the life of an AV1 session. #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub struct Av1EncodeConfig { /// Width in luma samples, multiple of 8. pub width: u32, /// Height in luma samples, multiple of 8. pub height: u32, /// Frame rate as numerator and denominator. pub fps: (u32, u32), /// Rate control; ConstantQp qp is mapped to AV1 base_q_idx as qp * 5. pub rate: RateControl, /// Luma and chroma bit depth: 8 or 10 (Main profile, NV12 or P010 input). pub bit_depth: u8, /// `seq_level_idx` (0 to 23), chosen by the caller and clamped to the driver maximum. pub level: u8, /// Colour config of the sequence header, or none (unspecified). pub colour: Option, /// Speed/quality trade-off as in ec-hw's `EncoderConfig::quality`: 0 for the default level, 1 for the driver's best, each step up one level faster. pub quality: u32, } /// One picture to encode. #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub struct Av1Picture { /// Encode as KEY frame. pub key: bool, /// order_hint of this frame. pub order_hint: u8, /// DPB slot this picture is written to, below `DPB_SLOTS`. pub slot: u32, /// Reference for an INTER frame, None for KEY. pub reference: Option, } /// A picture held in the DPB. #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub struct Av1Ref { /// DPB slot, below `DPB_SLOTS`. pub slot: u32, /// order_hint of the reference. pub order_hint: u8, } struct Caps { std_header_version: vk::ExtensionProperties, max_extent: vk::Extent2D, size_align: u64, rc_modes: vk::VideoEncodeRateControlModeFlagsKHR, quality_levels: u32, single_ref: bool, sb64: bool, max_level: vk::video::StdVideoAV1Level, } struct Profile { info: vk::VideoProfileInfoKHR, codec: vk::VideoEncodeAV1ProfileInfoKHR, usage: vk::VideoEncodeUsageInfoKHR, } impl Profile { fn new(bit_depth: u8) -> Box { let component = component_bit_depth(bit_depth); let mut p = Box::new(Profile { info: vk::VideoProfileInfoKHR::builder() .video_codec_operation(vk::VideoCodecOperationFlagsKHR::ENCODE_AV1) .chroma_subsampling(vk::VideoChromaSubsamplingFlagsKHR::_420) .luma_bit_depth(component) .chroma_bit_depth(component) .build(), codec: vk::VideoEncodeAV1ProfileInfoKHR::builder() .std_profile(vk::video::STD_VIDEO_AV1_PROFILE_MAIN) .build(), usage: vk::VideoEncodeUsageInfoKHR::builder() .video_usage_hints(vk::VideoEncodeUsageFlagsKHR::STREAMING) .video_content_hints(vk::VideoEncodeContentFlagsKHR::DESKTOP) .tuning_mode(vk::VideoEncodeTuningModeKHR::LOW_LATENCY) .build(), }); p.codec.next = &p.usage as *const _ as *const c_void; p.info.next = &p.codec as *const _ as *const c_void; p } } struct RateChain { rc: vk::VideoEncodeRateControlInfoKHR, rc_av1: vk::VideoEncodeAV1RateControlInfoKHR, layer: vk::VideoEncodeRateControlLayerInfoKHR, layer_av1: vk::VideoEncodeAV1RateControlLayerInfoKHR, } impl RateChain { fn new(rate: RateControl, fps: (u32, u32)) -> Box { let mode = rate.mode(); let layers = u32::from(rate.layer().is_some()); let (bitrate, max_bitrate, buffer_ms) = rate.layer().unwrap_or_default(); let mut c = Box::new(RateChain { rc: vk::VideoEncodeRateControlInfoKHR::builder() .rate_control_mode(mode) .virtual_buffer_size_in_ms(buffer_ms) .initial_virtual_buffer_size_in_ms(buffer_ms) .build(), rc_av1: vk::VideoEncodeAV1RateControlInfoKHR::builder() .flags(vk::VideoEncodeAV1RateControlFlagsKHR::VIDEO_ENCODE_AV1_RATE_CONTROL_REGULAR_GOP | vk::VideoEncodeAV1RateControlFlagsKHR::VIDEO_ENCODE_AV1_RATE_CONTROL_REFERENCE_PATTERN_FLAT) .gop_frame_count(u32::MAX) .key_frame_period(u32::MAX) .consecutive_bipredictive_frame_count(0) .temporal_layer_count(1) .build(), layer: vk::VideoEncodeRateControlLayerInfoKHR::builder() .average_bitrate(bitrate as u64) .max_bitrate(max_bitrate as u64) .frame_rate_numerator(fps.0) .frame_rate_denominator(fps.1) .build(), layer_av1: { let mut layer = vk::VideoEncodeAV1RateControlLayerInfoKHR::builder(); if let Some(q) = rate.min_qp() { layer = layer.use_min_q_index(true).min_q_index(vk::VideoEncodeAV1QIndexKHR { intra_q_index: q as u32 * 5, predictive_q_index: q as u32 * 5, bipredictive_q_index: q as u32 * 5 }); } if let Some(m) = rate.max_frame_bytes() { layer = layer.use_max_frame_size(true).max_frame_size(vk::VideoEncodeAV1FrameSizeKHR { intra_frame_size: m, predictive_frame_size: m, bipredictive_frame_size: m }); } layer.build() }, }); c.rc.next = &c.rc_av1 as *const _ as *const c_void; c.rc.layer_count = layers; c.rc.layers = &c.layer; c.layer.next = &c.layer_av1 as *const _ as *const c_void; c } } /// An AV1 Vulkan Video encode session: KEY and INTER frames, LAST reference, NV12 uploaded from CPU. pub struct Av1EncodeSession { enc: Encoder, seq: Vec, slot_key: [bool; DPB_SLOTS], ref_order_hint: [u8; AV1_SLOTS], } impl Av1EncodeSession { /// Creates a session with its DPB, buffers and sequence header. pub fn new(gpu: &Gpu, cfg: &Av1EncodeConfig) -> Result { validate(cfg)?; let family = gpu .encode_family() .ok_or_else(|| Error::Unsupported("AV1 encode queue family".into()))?; if !gpu .device() .extensions() .contains(&vk::KHR_VIDEO_ENCODE_AV1_EXTENSION.name) { return Err(Error::Unsupported("AV1 encode".into())); } let profile = Profile::new(cfg.bit_depth); let caps = query_caps(gpu, &profile.info).map_err(|e| match e { Error::Vk(..) if cfg.bit_depth == 10 => Error::Unsupported("10-bit AV1 encode".into()), e => e, })?; if cfg.width > caps.max_extent.width || cfg.height > caps.max_extent.height { return Err(Error::Unsupported(format!( "AV1 encode {}x{}", cfg.width, cfg.height ))); } if !caps.single_ref { return Err(Error::Unsupported("AV1 single LAST reference".into())); } let level = av1_level(cfg.level, caps.max_level)?; let mode = cfg.rate.mode(); if !caps.rc_modes.contains(mode) { return Err(Error::Unsupported(format!("rate control {mode:?}"))); } let upload_family = gpu .graphics_family() .ok_or_else(|| Error::Unsupported("graphics queue family for upload".into()))?; let sb128 = !caps.sb64; let extent = vk::Extent2D { width: cfg.width, height: cfg.height, }; let vs = VideoSession::new( gpu, family, &profile.info, &caps.std_header_version, extent, DPB_SLOTS as u32, 1, )?; let mut enc = Encoder::new( vs, &profile.info, upload_family, (cfg.width, cfg.height), extent, (cfg.rate, cfg.fps), caps.size_align, )?; enc.vs.quality_level = quality_level(cfg.quality, caps.quality_levels); let params = create_parameters(&enc.vs, extent, sb128, cfg, level)?; enc.vs.set_params(params); Ok(Av1EncodeSession { enc, seq: sequence_header_obu(cfg, sb128, level), slot_key: [false; DPB_SLOTS], ref_order_hint: [0; AV1_SLOTS], }) } /// OBU_SEQUENCE_HEADER bytes of this session, with OBU header. pub fn sequence_header(&self) -> Vec { self.seq.clone() } /// Submits one NV12 picture (y: width*height, uv: interleaved CbCr width*height/2) without waiting; returns the oldest picture once two are in flight. pub fn submit(&mut self, y: &[u8], uv: &[u8], pic: &Av1Picture) -> Result>, Error> { if y.len() != self.enc.input.0 * self.enc.input.1 || uv.len() != self.enc.input.0 * self.enc.input.1 / 2 { return Err(Error::Invalid("NV12 plane size".into())); } self.check(pic)?; self.enc.upload(Input::Nv12 { y, uv })?; self.encode_uploaded(pic) } /// Submits one 10-bit picture (y: width*height, uv: interleaved CbCr width*height/2, samples in the low 10 bits) of a 10-bit session without waiting; returns the oldest picture once two are in flight. pub fn submit_16(&mut self, y: &[u16], uv: &[u16], pic: &Av1Picture) -> Result>, Error> { if y.len() != self.enc.input.0 * self.enc.input.1 || uv.len() != self.enc.input.0 * self.enc.input.1 / 2 { return Err(Error::Invalid("P010 plane size".into())); } self.check(pic)?; self.enc.upload(Input::P010 { y, uv })?; self.encode_uploaded(pic) } /// Encodes one packed RGB picture (src_width x src_height, row stride in bytes, BGRx or RGBx byte order), box-downscaled to the encoder size on the GPU. pub fn submit_rgb( &mut self, rgb: &[u8], src_width: u32, src_height: u32, stride_bytes: u32, bgr: bool, pic: &Av1Picture, ) -> Result>, Error> { self.check(pic)?; let src = (src_width as usize, src_height as usize); check_rgb( (self.enc.input.0, self.enc.input.1), src, rgb, stride_bytes as usize, )?; self.enc.upload(Input::Rgb { data: rgb, width: src.0, height: src.1, stride: stride_bytes as usize, bgr, })?; self.encode_uploaded(pic) } /// Encodes one picture from a DMA-BUF: BGRx or RGBx box-downscaled to the encoder size, or NV12 or P010 of the encoder size. #[cfg(target_os = "linux")] pub fn submit_dmabuf(&mut self, src: &DmaBuf<'_>, pic: &Av1Picture) -> Result>, Error> { self.check(pic)?; self.enc.upload_dmabuf(src)?; self.encode_uploaded(pic) } /// Encodes one picture from a shared D3D11 texture of BGRA or RGBA pixels, downscaled to the encoder size. #[cfg(windows)] pub fn submit_d3d11(&mut self, src: &crate::D3d11Texture<'_>, pic: &Av1Picture) -> Result>, Error> { self.check(pic)?; self.enc.upload_d3d11(src)?; self.encode_uploaded(pic) } fn encode_uploaded(&mut self, pic: &Av1Picture) -> Result>, Error> { let extent = self.enc.extent; let refresh: u8 = if pic.key { 0xff } else { 1 << pic.slot }; let dpb_pr = self.enc.dpb.each_ref().map(|d| picture_resource(d.view, extent)); let src_pr = picture_resource(self.enc.src_view(), extent); let key_of = self.slot_key; let mut new_std = zeroed::(); new_std.frame_type = frame_type(pic.key); new_std.OrderHint = pic.order_hint; let mut new_dpb = vk::VideoEncodeAV1DpbSlotInfoKHR::builder() .std_reference_info(&new_std) .build(); let ref_std = pic.reference.map(|r| { let mut s = zeroed::(); s.frame_type = frame_type(key_of[r.slot as usize]); s.OrderHint = r.order_hint; s }); let mut ref_dpb = ref_std.as_ref().map(|s| { vk::VideoEncodeAV1DpbSlotInfoKHR::builder() .std_reference_info(s) .build() }); let mut refs = Vec::new(); if let (Some(r), Some(dpb)) = (pic.reference, ref_dpb.as_mut()) { refs.push( vk::VideoReferenceSlotInfoKHR::builder() .slot_index(r.slot as i32) .picture_resource(&dpb_pr[r.slot as usize]) .push_next(dpb) .build(), ); } let setup = vk::VideoReferenceSlotInfoKHR::builder() .slot_index(pic.slot as i32) .picture_resource(&dpb_pr[pic.slot as usize]) .push_next(&mut new_dpb) .build(); let begin_slots: Vec = (0..DPB_SLOTS) .map(|i| { let active = pic .reference .filter(|r| r.slot as usize == i) .map_or(-1, |_| i as i32); vk::VideoReferenceSlotInfoKHR::builder() .slot_index(active) .picture_resource(&dpb_pr[i]) .build() }) .collect(); let ref_slot = pic.reference.map_or(0, |r| r.slot as i8); let mut std_pic = zeroed::(); std_pic.flags.set_show_frame(1); std_pic.flags.set_error_resilient_mode(pic.key as u32); std_pic.frame_type = frame_type(pic.key); std_pic.order_hint = pic.order_hint; std_pic.primary_ref_frame = if pic.key { vk::video::STD_VIDEO_AV1_PRIMARY_REF_NONE as u8 } else { 0 }; std_pic.refresh_frame_flags = refresh; std_pic.TxMode = vk::video::STD_VIDEO_AV1_TX_MODE_SELECT; std_pic.interpolation_filter = vk::video::STD_VIDEO_AV1_INTERPOLATION_FILTER_SWITCHABLE; std_pic.ref_order_hint = self.ref_order_hint; std_pic.ref_frame_idx = [ref_slot; 7]; let mut tile = zeroed::(); tile.flags.set_uniform_tile_spacing_flag(1); tile.TileCols = 1; tile.TileRows = 1; let mut quant = zeroed::(); quant.base_q_idx = q_index(self.enc.rate) as u8; let mut loop_filter = zeroed::(); loop_filter.loop_filter_level = [1; 4]; let cdef = cdef_params(); let restoration = zeroed::(); let motion = zeroed::(); std_pic.pTileInfo = &tile; std_pic.pQuantization = &quant; std_pic.pSegmentation = std::ptr::null(); std_pic.pLoopFilter = &loop_filter; std_pic.pCDEF = &cdef; std_pic.pLoopRestoration = &restoration; std_pic.pGlobalMotion = &motion; let mut ref_names = [-1i32; 7]; if let Some(r) = pic.reference { ref_names[0] = r.slot as i32; } let mut av1_pic = vk::VideoEncodeAV1PictureInfoKHR { prediction_mode: if pic.key { vk::VideoEncodeAV1PredictionModeKHR::VIDEO_ENCODE_AV1_PREDICTION_MODE_INTRA_ONLY } else { vk::VideoEncodeAV1PredictionModeKHR::VIDEO_ENCODE_AV1_PREDICTION_MODE_SINGLE_REFERENCE }, rate_control_group: if pic.key { vk::VideoEncodeAV1RateControlGroupKHR::VIDEO_ENCODE_AV1_RATE_CONTROL_GROUP_INTRA } else { vk::VideoEncodeAV1RateControlGroupKHR::VIDEO_ENCODE_AV1_RATE_CONTROL_GROUP_PREDICTIVE }, constant_q_index: constant_q_index(self.enc.rate), std_picture_info: &std_pic, reference_name_slot_indices: ref_names, ..Default::default() }; let encode = vk::VideoEncodeInfoKHR::builder() .dst_buffer(self.enc.bitstream_buffer()) .dst_buffer_offset(0) .dst_buffer_range(self.enc.bitstream_len) .src_picture_resource(src_pr) .setup_reference_slot(&setup) .reference_slots(&refs) .push_next(&mut av1_pic) .build(); let mut applied = RateChain::new(self.enc.applied.0, self.enc.applied.1); let mut chain = RateChain::new(self.enc.rate, self.enc.fps); let out = self .enc .submit(&begin_slots, &mut applied.rc, &mut chain.rc, &encode)?; for i in 0..DPB_SLOTS { if refresh >> i & 1 == 1 { self.slot_key[i] = pic.key; } } for i in 0..AV1_SLOTS { if refresh >> i & 1 == 1 { self.ref_order_hint[i] = pic.order_hint; } } Ok(out) } /// Waits for queued pictures and returns their coded bytes in order. pub fn flush(&mut self) -> Result>, Error> { self.enc.flush() } /// Changes bitrate or frame rate; the next encode applies it. The rate control mode cannot change. pub fn set_rate(&mut self, rate: RateControl, fps: (u32, u32)) -> Result<(), Error> { self.enc.set_rate(rate, fps) } fn check(&self, pic: &Av1Picture) -> Result<(), Error> { if pic.slot as usize >= DPB_SLOTS { return Err(Error::Invalid("DPB slot".into())); } match pic.reference { None if pic.key => Ok(()), Some(r) if !pic.key && self.enc.coded && (r.slot as usize) < DPB_SLOTS && r.slot != pic.slot => { Ok(()) } _ => Err(Error::Invalid("picture and reference do not match".into())), } } } fn create_parameters( vs: &VideoSession, extent: vk::Extent2D, sb128: bool, cfg: &Av1EncodeConfig, level: vk::video::StdVideoAV1Level, ) -> Result { let mut color = zeroed::(); color.BitDepth = cfg.bit_depth; color.subsampling_x = 1; color.subsampling_y = 1; color.color_primaries = vk::video::STD_VIDEO_AV1_COLOR_PRIMARIES_UNSPECIFIED; color.transfer_characteristics = vk::video::STD_VIDEO_AV1_TRANSFER_CHARACTERISTICS_UNSPECIFIED; color.matrix_coefficients = vk::video::STD_VIDEO_AV1_MATRIX_COEFFICIENTS_UNSPECIFIED; color.chroma_sample_position = vk::video::STD_VIDEO_AV1_CHROMA_SAMPLE_POSITION_UNKNOWN; if let Some(c) = cfg.colour { color.flags.set_color_description_present_flag(1); color.flags.set_color_range(c.full_range as u32); color.color_primaries = vk::video::StdVideoAV1ColorPrimaries(i32::from(c.primaries)); color.transfer_characteristics = vk::video::StdVideoAV1TransferCharacteristics(i32::from(c.transfer)); color.matrix_coefficients = vk::video::StdVideoAV1MatrixCoefficients(i32::from(c.matrix)); } let mut timing = zeroed::(); timing.flags.set_equal_picture_interval(1); timing.num_units_in_display_tick = cfg.fps.1; timing.time_scale = cfg.fps.0; let mut seq = zeroed::(); seq.flags.set_enable_order_hint(1); seq.flags.set_use_128x128_superblock(sb128 as u32); seq.flags.set_timing_info_present_flag(1); seq.pTimingInfo = &timing; seq.seq_profile = vk::video::STD_VIDEO_AV1_PROFILE_MAIN; seq.frame_width_bits_minus_1 = (bits(extent.width) - 1) as u8; seq.frame_height_bits_minus_1 = (bits(extent.height) - 1) as u8; seq.max_frame_width_minus_1 = (extent.width - 1) as u16; seq.max_frame_height_minus_1 = (extent.height - 1) as u16; seq.order_hint_bits_minus_1 = 7; seq.seq_force_integer_mv = vk::video::STD_VIDEO_AV1_SELECT_INTEGER_MV as u8; seq.flags.set_enable_cdef(1); seq.pColorConfig = &color; let mut op = zeroed::(); op.seq_level_idx = level.0 as u8; let mut av1 = vk::VideoEncodeAV1SessionParametersCreateInfoKHR { std_sequence_header: &seq, std_operating_point_count: 1, std_operating_points: &op, ..Default::default() }; let mut quality = vk::VideoEncodeQualityLevelInfoKHR::builder() .quality_level(vs.quality_level) .build(); let info = vk::VideoSessionParametersCreateInfoKHR::builder() .video_session(vs.session) .push_next(&mut quality) .push_next(&mut av1) .build(); unsafe { vk( vs.gpu .device() .create_video_session_parameters_khr(&info, None), "vkCreateVideoSessionParametersKHR", ) } } struct Bits(Vec); impl Bits { fn put(&mut self, value: u32, n: u32) { self.0 .extend((0..n).rev().map(|i| ((value >> i) & 1) as u8)); } fn finish(mut self) -> Vec { self.0.push(1); while self.0.len() % 8 != 0 { self.0.push(0); } self.0 .chunks(8) .map(|c| c.iter().fold(0u8, |acc, &b| (acc << 1) | b)) .collect() } } fn bits(value: u32) -> u32 { (32 - value.leading_zeros()).max(1) } fn obu(kind: u8, payload: &[u8]) -> Vec { let mut out = vec![(kind << 3) | 2]; let mut len = payload.len(); loop { let byte = (len & 0x7f) as u8; len >>= 7; if len == 0 { out.push(byte); break; } out.push(byte | 0x80); } out.extend_from_slice(payload); out } fn sequence_header_obu( cfg: &Av1EncodeConfig, sb128: bool, level: vk::video::StdVideoAV1Level, ) -> Vec { let (width, height, bit_depth) = (cfg.width, cfg.height, cfg.bit_depth); let mut b = Bits(Vec::new()); b.put(0, 3); b.put(0, 1); b.put(0, 1); b.put(1, 1); b.put(cfg.fps.1, 32); b.put(cfg.fps.0, 32); b.put(1, 1); b.put(1, 1); b.put(0, 1); b.put(0, 1); b.put(0, 5); b.put(0, 12); b.put(level.0 as u32, 5); if level.0 > 7 { b.put(0, 1); } b.put(bits(width) - 1, 4); b.put(bits(height) - 1, 4); b.put(width - 1, bits(width)); b.put(height - 1, bits(height)); b.put(0, 1); b.put(sb128 as u32, 1); b.put(0, 1); b.put(0, 1); b.put(0, 1); b.put(0, 1); b.put(0, 1); b.put(0, 1); b.put(1, 1); b.put(0, 1); b.put(0, 1); b.put(0, 1); b.put(0, 1); b.put(7, 3); b.put(0, 1); b.put(1, 1); b.put(0, 1); b.put((bit_depth == 10) as u32, 1); b.put(0, 1); match cfg.colour { Some(c) => { b.put(1, 1); b.put(u32::from(c.primaries), 8); b.put(u32::from(c.transfer), 8); b.put(u32::from(c.matrix), 8); b.put(c.full_range as u32, 1); } None => { b.put(0, 1); b.put(0, 1); } } b.put(0, 2); b.put(0, 1); b.put(0, 1); obu(1, &b.finish()) } fn validate(cfg: &Av1EncodeConfig) -> Result<(), Error> { if cfg.bit_depth != 8 && cfg.bit_depth != 10 { return Err(Error::Invalid("AV1 bit depth".into())); } if cfg.width == 0 || cfg.height == 0 || cfg.width % 8 != 0 || cfg.height % 8 != 0 { return Err(Error::Invalid( "size must be a non-zero multiple of 8".into(), )); } validate_rate(cfg.fps, cfg.rate) } fn q_index(rate: RateControl) -> u32 { match rate { RateControl::ConstantQp { qp } => qp as u32 * 5, RateControl::Cbr { .. } | RateControl::Vbr { .. } => CBR_Q_INDEX, } } fn constant_q_index(rate: RateControl) -> u32 { match rate { RateControl::ConstantQp { qp } => qp as u32 * 5, RateControl::Cbr { .. } | RateControl::Vbr { .. } => 0, } } fn frame_type(key: bool) -> vk::video::StdVideoAV1FrameType { if key { vk::video::STD_VIDEO_AV1_FRAME_TYPE_KEY } else { vk::video::STD_VIDEO_AV1_FRAME_TYPE_INTER } } fn query_caps(gpu: &Gpu, profile: &vk::VideoProfileInfoKHR) -> Result { let mut av1 = vk::VideoEncodeAV1CapabilitiesKHR::builder(); let mut encode = vk::VideoEncodeCapabilitiesKHR::builder(); let mut caps = vk::VideoCapabilitiesKHR::builder() .push_next(&mut av1) .push_next(&mut encode); video_caps(gpu, profile, &mut caps)?; Ok(Caps { std_header_version: caps.std_header_version.clone(), max_extent: caps.max_coded_extent, size_align: caps.min_bitstream_buffer_size_alignment, rc_modes: encode.rate_control_modes, quality_levels: encode.max_quality_levels, single_ref: av1.max_single_reference_count > 0 && av1.single_reference_name_mask & 1 != 0, sb64: av1.superblock_sizes.contains( vk::VideoEncodeAV1SuperblockSizeFlagsKHR::VIDEO_ENCODE_AV1_SUPERBLOCK_SIZE_64, ), max_level: av1.max_level, }) } fn av1_level( idx: u8, driver_max: vk::video::StdVideoAV1Level, ) -> Result { if idx > 23 { return Err(Error::Invalid(format!("AV1 seq_level_idx {idx}"))); } let level = vk::video::StdVideoAV1Level(i32::from(idx)); Ok(if level.0 > driver_max.0 { driver_max } else { level }) } /// CDEF candidates the encoder picks from per 64x64 block: a main strength, off for fine texture, half and double. fn cdef_params() -> vk::video::StdVideoAV1CDEF { let mut cdef = zeroed::(); cdef.cdef_damping_minus_3 = 2; cdef.cdef_bits = 2; for (i, (pri, sec)) in [(4, 1), (0, 0), (2, 1), (8, 2)].into_iter().enumerate() { cdef.cdef_y_pri_strength[i] = pri; cdef.cdef_uv_pri_strength[i] = pri; cdef.cdef_y_sec_strength[i] = sec; cdef.cdef_uv_sec_strength[i] = sec; } cdef }