ec-hw: Vulkan AV1 decode behind HwDecoder (shared generic av1_refs), bit-exact vs ffmpeg on SVT, libaom (alt-ref/show_existing) and own Vk streams; shim converts LoopRestorationSize from samples to log2(size)-5
Browse files at this commite5afa4e865ec6b0eaac27ac9972c9f6247d76b24Parent: a11df787 changed files +708 -65
- crates/ec-hw/examples/hw_decode.rs +149 -31
- crates/ec-hw/src/av1_refs.rs +39 -0
- crates/ec-hw/src/dec/av1.rs +24 -29
- crates/ec-hw/src/hw.rs +13 -4
- crates/ec-hw/src/lib.rs +2 -0
- crates/ec-hw/src/vkdec_av1.rs +480 -0
- shims/vk-video-ffi/src/av1_decode.rs +1 -1
crates/ec-hw/examples/hw_decode.rsmodified+149 -31
| @@ -1,4 +1,4 @@ | ||
| 1 | -//! Decodes ffmpeg libx264 and libx265 streams through HwDecoder and compares luma with ffmpeg's decode, frame by frame. | |
| 1 | +//! Decodes ffmpeg libx264, libx265, SVT-AV1 and libaom AV1 streams, plus our own Vulkan AV1 encoder's, through HwDecoder and compares luma with ffmpeg's decode, frame by frame. | |
| 2 | 2 | //! |
| 3 | 3 | //! ```text |
| 4 | 4 | //! cargo run -p ec-hw --example hw_decode -- <scratch dir> |
| @@ -8,7 +8,7 @@ use std::fs; | ||
| 8 | 8 | use std::path::{Path, PathBuf}; |
| 9 | 9 | use std::process::Command; |
| 10 | 10 | |
| 11 | -use ec_hw::{Backend, Codec, HwDecoder, I420}; | |
| 11 | +use ec_hw::{Backend, Codec, EncCodec, EncoderConfig, FrameMetadata, HwDecoder, HwEncoder, I420, RateControlMode}; | |
| 12 | 12 | |
| 13 | 13 | struct Case { |
| 14 | 14 | name: &'static str, |
| @@ -43,6 +43,9 @@ const H265_KEYINT: &[&str] = &[ | ||
| 43 | 43 | "log-level=error:bframes=3:ref=3:keyint=20", |
| 44 | 44 | ]; |
| 45 | 45 | const HEVC: &[&str] = &["-f", "hevc"]; |
| 46 | +const AV1_SVT: &[&str] = &["-c:v", "libsvtav1", "-g", "30"]; | |
| 47 | +const AV1_AOM: &[&str] = &["-c:v", "libaom-av1", "-cpu-used", "8", "-lag-in-frames", "10"]; | |
| 48 | +const IVF: &[&str] = &["-f", "ivf"]; | |
| 46 | 49 | |
| 47 | 50 | const CASES: &[Case] = &[ |
| 48 | 51 | Case { |
| @@ -77,6 +80,22 @@ const CASES: &[Case] = &[ | ||
| 77 | 80 | encoder: H265_KEYINT, |
| 78 | 81 | container: HEVC, |
| 79 | 82 | }, |
| 83 | + Case { | |
| 84 | + name: "av1svt640", | |
| 85 | + codec: Codec::Av1, | |
| 86 | + size: "640x360", | |
| 87 | + frames: 60, | |
| 88 | + encoder: AV1_SVT, | |
| 89 | + container: IVF, | |
| 90 | + }, | |
| 91 | + Case { | |
| 92 | + name: "av1aom720", | |
| 93 | + codec: Codec::Av1, | |
| 94 | + size: "1280x720", | |
| 95 | + frames: 30, | |
| 96 | + encoder: AV1_AOM, | |
| 97 | + container: IVF, | |
| 98 | + }, | |
| 80 | 99 | ]; |
| 81 | 100 | |
| 82 | 101 | fn main() { |
| @@ -85,6 +104,7 @@ fn main() { | ||
| 85 | 104 | run(&dir, case); |
| 86 | 105 | } |
| 87 | 106 | ten_bit_falls_back(&dir); |
| 107 | + own_av1(&dir); | |
| 88 | 108 | } |
| 89 | 109 | |
| 90 | 110 | fn ten_bit_falls_back(dir: &std::path::Path) { |
| @@ -123,10 +143,10 @@ fn ffmpeg(args: &[&str]) { | ||
| 123 | 143 | } |
| 124 | 144 | |
| 125 | 145 | fn run(dir: &Path, case: &Case) { |
| 126 | - let ext = if case.codec == Codec::H264 { | |
| 127 | - "h264" | |
| 128 | - } else { | |
| 129 | - "hevc" | |
| 146 | + let ext = match case.codec { | |
| 147 | + Codec::H264 => "h264", | |
| 148 | + Codec::Av1 => "ivf", | |
| 149 | + _ => "hevc", | |
| 130 | 150 | }; |
| 131 | 151 | let stream = dir.join(format!("{}.{ext}", case.name)); |
| 132 | 152 | let reference = dir.join(format!("{}.yuv", case.name)); |
| @@ -162,24 +182,132 @@ fn run(dir: &Path, case: &Case) { | ||
| 162 | 182 | if case.codec == Codec::H265 { |
| 163 | 183 | assert_eq!(backend, Backend::Vk, "H.265 must decode on Vulkan"); |
| 164 | 184 | } |
| 165 | - decoder.decode(&data, 0).expect("decode"); | |
| 185 | + let mut decoded: Vec<I420> = Vec::new(); | |
| 186 | + if case.codec == Codec::Av1 { | |
| 187 | + for (pts, unit) in ivf_units(&data).into_iter().enumerate() { | |
| 188 | + decoder.decode(unit, pts as i64).expect("decode"); | |
| 189 | + drain(&mut decoder, &mut decoded); | |
| 190 | + } | |
| 191 | + } else { | |
| 192 | + decoder.decode(&data, 0).expect("decode"); | |
| 193 | + } | |
| 166 | 194 | decoder.flush().expect("flush"); |
| 195 | + drain(&mut decoder, &mut decoded); | |
| 196 | + | |
| 197 | + let raw = fs::read(&reference).expect("read reference"); | |
| 198 | + compare(case.name, case.codec, backend, dims(case.size), &decoded, &raw); | |
| 199 | +} | |
| 200 | + | |
| 201 | +fn own_av1(dir: &Path) { | |
| 202 | + const FRAMES: u32 = 30; | |
| 203 | + const WIDTH: u32 = 1280; | |
| 204 | + const HEIGHT: u32 = 720; | |
| 205 | + let config = EncoderConfig { | |
| 206 | + bitrate: 2_000_000, | |
| 207 | + rate_control: RateControlMode::ConstantBitrate, | |
| 208 | + ..EncoderConfig::new(EncCodec::Av1, WIDTH, HEIGHT) | |
| 209 | + }; | |
| 210 | + let mut encoder = HwEncoder::open(config, None).expect("open AV1 encoder"); | |
| 211 | + let encoder_backend = encoder.backend(); | |
| 212 | + let mut units = Vec::new(); | |
| 213 | + for index in 0..FRAMES { | |
| 214 | + let meta = FrameMetadata { | |
| 215 | + timestamp: index as i64, | |
| 216 | + force_keyframe: index == 0, | |
| 217 | + }; | |
| 218 | + units.push(encoder.encode(&gradient(index, WIDTH, HEIGHT), meta).expect("encode").data); | |
| 219 | + } | |
| 220 | + let stream = dir.join("own_av1.ivf"); | |
| 221 | + let reference = dir.join("own_av1.yuv"); | |
| 222 | + fs::write(&stream, ivf(WIDTH, HEIGHT, &units)).expect("write ivf"); | |
| 223 | + | |
| 224 | + let mut decoder = HwDecoder::open(Codec::Av1).expect("open decoder"); | |
| 225 | + let backend = decoder.backend(); | |
| 167 | 226 | let mut decoded: Vec<I420> = Vec::new(); |
| 227 | + for (pts, unit) in units.iter().enumerate() { | |
| 228 | + decoder.decode(unit, pts as i64).expect("decode"); | |
| 229 | + drain(&mut decoder, &mut decoded); | |
| 230 | + } | |
| 231 | + decoder.flush().expect("flush"); | |
| 232 | + drain(&mut decoder, &mut decoded); | |
| 233 | + | |
| 234 | + let status = Command::new("ffmpeg") | |
| 235 | + .args(["-y", "-loglevel", "error", "-i"]) | |
| 236 | + .arg(&stream) | |
| 237 | + .args(["-f", "rawvideo", "-pix_fmt", "yuv420p"]) | |
| 238 | + .arg(&reference) | |
| 239 | + .status(); | |
| 240 | + match status { | |
| 241 | + Ok(status) if status.success() => { | |
| 242 | + let raw = fs::read(&reference).expect("read reference"); | |
| 243 | + println!("own_av1 encoder {encoder_backend:?}: {} bytes", units.iter().map(Vec::len).sum::<usize>()); | |
| 244 | + compare("own_av1", Codec::Av1, backend, (WIDTH as usize, HEIGHT as usize), &decoded, &raw); | |
| 245 | + } | |
| 246 | + _ => { | |
| 247 | + println!("own_av1 encoder {encoder_backend:?}: ffmpeg no reference, decoded {} of {FRAMES} frames on {backend:?}", decoded.len()); | |
| 248 | + assert_eq!(decoded.len(), FRAMES as usize, "own_av1: frame count"); | |
| 249 | + } | |
| 250 | + } | |
| 251 | +} | |
| 252 | + | |
| 253 | +fn gradient(index: u32, width: u32, height: u32) -> I420 { | |
| 254 | + let (w, h) = (width as usize, height as usize); | |
| 255 | + let shift = 4 * index as usize; | |
| 256 | + let y = (0..h) | |
| 257 | + .flat_map(|row| (0..w).map(move |col| ((col + row + shift) & 0xff) as u8)) | |
| 258 | + .collect(); | |
| 259 | + let chroma = w.div_ceil(2) * h.div_ceil(2); | |
| 260 | + I420 { | |
| 261 | + y, | |
| 262 | + u: vec![128; chroma], | |
| 263 | + v: vec![128; chroma], | |
| 264 | + width, | |
| 265 | + height, | |
| 266 | + } | |
| 267 | +} | |
| 268 | + | |
| 269 | +fn ivf(width: u32, height: u32, units: &[Vec<u8>]) -> Vec<u8> { | |
| 270 | + let mut out = b"DKIF".to_vec(); | |
| 271 | + out.extend_from_slice(&0u16.to_le_bytes()); | |
| 272 | + out.extend_from_slice(&32u16.to_le_bytes()); | |
| 273 | + out.extend_from_slice(b"AV01"); | |
| 274 | + out.extend_from_slice(&(width as u16).to_le_bytes()); | |
| 275 | + out.extend_from_slice(&(height as u16).to_le_bytes()); | |
| 276 | + out.extend_from_slice(&30u32.to_le_bytes()); | |
| 277 | + out.extend_from_slice(&1u32.to_le_bytes()); | |
| 278 | + out.extend_from_slice(&(units.len() as u32).to_le_bytes()); | |
| 279 | + out.extend_from_slice(&0u32.to_le_bytes()); | |
| 280 | + for (pts, unit) in units.iter().enumerate() { | |
| 281 | + out.extend_from_slice(&(unit.len() as u32).to_le_bytes()); | |
| 282 | + out.extend_from_slice(&(pts as u64).to_le_bytes()); | |
| 283 | + out.extend_from_slice(unit); | |
| 284 | + } | |
| 285 | + out | |
| 286 | +} | |
| 287 | + | |
| 288 | +fn ivf_units(ivf: &[u8]) -> Vec<&[u8]> { | |
| 289 | + let mut pos = usize::from(u16::from_le_bytes([ivf[6], ivf[7]])); | |
| 290 | + let mut units = Vec::new(); | |
| 291 | + while pos + 12 <= ivf.len() { | |
| 292 | + let len = u32::from_le_bytes(ivf[pos..pos + 4].try_into().expect("frame size")) as usize; | |
| 293 | + units.push(&ivf[pos + 12..pos + 12 + len]); | |
| 294 | + pos += 12 + len; | |
| 295 | + } | |
| 296 | + units | |
| 297 | +} | |
| 298 | + | |
| 299 | +fn drain(decoder: &mut HwDecoder, out: &mut Vec<I420>) { | |
| 168 | 300 | while let Some((_, frame)) = decoder.next_frame().expect("next frame") { |
| 169 | - decoded.push(frame); | |
| 301 | + out.push(frame); | |
| 170 | 302 | } |
| 303 | +} | |
| 171 | 304 | |
| 172 | - let raw = fs::read(&reference).expect("read reference"); | |
| 173 | - let (w, h) = case | |
| 174 | - .size | |
| 175 | - .split_once('x') | |
| 176 | - .map(|(w, h)| { | |
| 177 | - ( | |
| 178 | - w.parse::<usize>().expect("width"), | |
| 179 | - h.parse::<usize>().expect("height"), | |
| 180 | - ) | |
| 181 | - }) | |
| 182 | - .expect("size"); | |
| 305 | +fn dims(size: &str) -> (usize, usize) { | |
| 306 | + let (w, h) = size.split_once('x').expect("size"); | |
| 307 | + (w.parse().expect("width"), h.parse().expect("height")) | |
| 308 | +} | |
| 309 | + | |
| 310 | +fn compare(label: &str, codec: Codec, backend: Backend, (w, h): (usize, usize), decoded: &[I420], raw: &[u8]) { | |
| 183 | 311 | let frame_len = w * h * 3 / 2; |
| 184 | 312 | let reference_frames = raw.len() / frame_len; |
| 185 | 313 | let worst = decoded |
| @@ -196,18 +324,8 @@ fn run(dir: &Path, case: &Case) { | ||
| 196 | 324 | }) |
| 197 | 325 | .fold(0.0f64, f64::max); |
| 198 | 326 | println!( |
| 199 | - "{} {:?} {:?} {}: decoded {} frames, ffmpeg {} frames, max mean |diff| Y {worst:.6}", | |
| 200 | - case.name, | |
| 201 | - case.codec, | |
| 202 | - backend, | |
| 203 | - case.size, | |
| 204 | - decoded.len(), | |
| 205 | - reference_frames | |
| 206 | - ); | |
| 207 | - assert_eq!( | |
| 327 | + "{label} {codec:?} {backend:?} {w}x{h}: decoded {} frames, ffmpeg {reference_frames} frames, max mean |diff| Y {worst:.6}", | |
| 208 | 328 | decoded.len(), |
| 209 | - reference_frames, | |
| 210 | - "{}: frame count", | |
| 211 | - case.name | |
| 212 | 329 | ); |
| 330 | + assert_eq!(decoded.len(), reference_frames, "{label}: frame count"); | |
| 213 | 331 | } |
crates/ec-hw/src/av1_refs.rsadded+39 -0
| @@ -0,0 +1,39 @@ | ||
| 1 | +//! The eight AV1 reference slots (spec 7.20), shared by the VA-API and Vulkan decoders. | |
| 2 | + | |
| 3 | +use ec_av1_syntax::NUM_REF_FRAMES; | |
| 4 | + | |
| 5 | +/// What each reference slot holds, generic over the decoder's picture type. | |
| 6 | +#[derive(Debug, Clone)] | |
| 7 | +pub(crate) struct RefSlots<S> { | |
| 8 | + slots: [Option<S>; NUM_REF_FRAMES], | |
| 9 | +} | |
| 10 | + | |
| 11 | +impl<S: Clone> RefSlots<S> { | |
| 12 | + pub(crate) fn new() -> Self { | |
| 13 | + RefSlots { | |
| 14 | + slots: std::array::from_fn(|_| None), | |
| 15 | + } | |
| 16 | + } | |
| 17 | + | |
| 18 | + pub(crate) fn get(&self, slot: usize) -> Option<&S> { | |
| 19 | + self.slots.get(slot)?.as_ref() | |
| 20 | + } | |
| 21 | + | |
| 22 | + #[cfg_attr(not(target_os = "linux"), allow(dead_code))] | |
| 23 | + pub(crate) fn as_array(&self) -> &[Option<S>; NUM_REF_FRAMES] { | |
| 24 | + &self.slots | |
| 25 | + } | |
| 26 | + | |
| 27 | + pub(crate) fn filled(&self) -> impl Iterator<Item = &S> { | |
| 28 | + self.slots.iter().flatten() | |
| 29 | + } | |
| 30 | + | |
| 31 | + /// Store `value` in every slot named by the `refresh_frame_flags` bit mask. | |
| 32 | + pub(crate) fn refresh(&mut self, flags: u8, value: Option<S>) { | |
| 33 | + for (slot, entry) in self.slots.iter_mut().enumerate() { | |
| 34 | + if flags & (1 << slot) != 0 { | |
| 35 | + entry.clone_from(&value); | |
| 36 | + } | |
| 37 | + } | |
| 38 | + } | |
| 39 | +} |
crates/ec-hw/src/dec/av1.rsmodified+24 -29
| @@ -11,6 +11,7 @@ use ec_va::va::BufferType; | ||
| 11 | 11 | use ec_va::{Display, picture}; |
| 12 | 12 | |
| 13 | 13 | use super::{ReadyFrames, Session, StreamInfo}; |
| 14 | +use crate::av1_refs::RefSlots; | |
| 14 | 15 | use crate::error::{Error, Result}; |
| 15 | 16 | use crate::frame::Frame; |
| 16 | 17 | use crate::params::av1::{ |
| @@ -34,12 +35,12 @@ pub struct Av1Decoder { | ||
| 34 | 35 | parser: Av1Parser, |
| 35 | 36 | session: Option<Session>, |
| 36 | 37 | /// Each slot's grain-free reconstruction: what later frames predict from. |
| 37 | - refs: [Option<Rc<PooledSurface>>; NUM_REF_FRAMES], | |
| 38 | + refs: RefSlots<Rc<PooledSurface>>, | |
| 38 | 39 | /// Each slot's grained copy, when the frame in it was decoded with film |
| 39 | 40 | /// grain. A `show_existing_frame` outputs this copy (spec 7.21 reloads the |
| 40 | 41 | /// grain parameters of the frame it shows), so the driver's grain pass runs |
| 41 | 42 | /// once, at decode time, and the slot keeps the result. |
| 42 | - grained: [Option<Rc<PooledSurface>>; NUM_REF_FRAMES], | |
| 43 | + grained: RefSlots<Rc<PooledSurface>>, | |
| 43 | 44 | ready: ReadyFrames, |
| 44 | 45 | } |
| 45 | 46 | |
| @@ -50,8 +51,8 @@ impl Av1Decoder { | ||
| 50 | 51 | display: Rc::clone(display), |
| 51 | 52 | parser: Av1Parser::new(), |
| 52 | 53 | session: None, |
| 53 | - refs: [const { None }; NUM_REF_FRAMES], | |
| 54 | - grained: [const { None }; NUM_REF_FRAMES], | |
| 54 | + refs: RefSlots::new(), | |
| 55 | + grained: RefSlots::new(), | |
| 55 | 56 | ready: ReadyFrames::default(), |
| 56 | 57 | } |
| 57 | 58 | } |
| @@ -103,8 +104,8 @@ impl Av1Decoder { | ||
| 103 | 104 | /// Drop reference state after a seek. |
| 104 | 105 | pub fn reset(&mut self) { |
| 105 | 106 | self.parser = Av1Parser::new(); |
| 106 | - self.refs = [const { None }; NUM_REF_FRAMES]; | |
| 107 | - self.grained = [const { None }; NUM_REF_FRAMES]; | |
| 107 | + self.refs = RefSlots::new(); | |
| 108 | + self.grained = RefSlots::new(); | |
| 108 | 109 | self.ready.clear(); |
| 109 | 110 | } |
| 110 | 111 | |
| @@ -115,18 +116,18 @@ impl Av1Decoder { | ||
| 115 | 116 | |
| 116 | 117 | fn show_existing(&mut self, header: &FrameHeader, timestamp: i64) -> Result<()> { |
| 117 | 118 | let slot = usize::from(header.frame_to_show_map_idx); |
| 118 | - let (Some(surface), Some(session)) = ( | |
| 119 | - self.refs.get(slot).and_then(|s| s.clone()), | |
| 120 | - self.session.as_ref(), | |
| 121 | - ) else { | |
| 119 | + let (Some(surface), Some(session)) = (self.refs.get(slot).cloned(), self.session.as_ref()) | |
| 120 | + else { | |
| 122 | 121 | return Err(Error::Stream(ec_core::Error::corrupt( |
| 123 | 122 | "AV1 show_existing_frame names an empty reference slot", |
| 124 | 123 | ))); |
| 125 | 124 | }; |
| 126 | 125 | // The grained copy when the shown frame carries grain, else the |
| 127 | 126 | // reconstruction itself. |
| 128 | - let out = self.grained[slot] | |
| 129 | - .clone() | |
| 127 | + let out = self | |
| 128 | + .grained | |
| 129 | + .get(slot) | |
| 130 | + .cloned() | |
| 130 | 131 | .unwrap_or_else(|| Rc::clone(&surface)); |
| 131 | 132 | self.ready.push(Frame::new( |
| 132 | 133 | out, |
| @@ -140,13 +141,9 @@ impl Av1Decoder { | ||
| 140 | 141 | // 7.21: showing a KEY frame again refreshes every reference slot with |
| 141 | 142 | // it, which the reference *surfaces* have to follow too. |
| 142 | 143 | if header.frame_type == FrameType::Key { |
| 143 | - let grained = self.grained[slot].clone(); | |
| 144 | - for entry in &mut self.refs { | |
| 145 | - *entry = Some(Rc::clone(&surface)); | |
| 146 | - } | |
| 147 | - for entry in &mut self.grained { | |
| 148 | - entry.clone_from(&grained); | |
| 149 | - } | |
| 144 | + let grained = self.grained.get(slot).cloned(); | |
| 145 | + self.refs.refresh(u8::MAX, Some(Rc::clone(&surface))); | |
| 146 | + self.grained.refresh(u8::MAX, grained); | |
| 150 | 147 | } |
| 151 | 148 | Ok(()) |
| 152 | 149 | } |
| @@ -172,7 +169,7 @@ impl Av1Decoder { | ||
| 172 | 169 | // rather than hand it VA_INVALID_SURFACE and output what it makes of it. |
| 173 | 170 | if matches!(header.frame_type, FrameType::Inter | FrameType::Switch) { |
| 174 | 171 | for (ref_index, &slot) in header.ref_frame_idx.iter().enumerate() { |
| 175 | - if self.refs[usize::from(slot)].is_none() { | |
| 172 | + if self.refs.get(usize::from(slot)).is_none() { | |
| 176 | 173 | return Err(Error::Stream(ec_core::Error::corrupt(format!( |
| 177 | 174 | "AV1 inter frame names reference slot {slot} (ref {ref_index}), \ |
| 178 | 175 | which no decoded frame has filled" |
| @@ -205,7 +202,7 @@ impl Av1Decoder { | ||
| 205 | 202 | let pic_param = picture_parameters( |
| 206 | 203 | &sequence, |
| 207 | 204 | header, |
| 208 | - &slot_ids(&self.refs), | |
| 205 | + &slot_ids(self.refs.as_array()), | |
| 209 | 206 | surface.id(), |
| 210 | 207 | display_id, |
| 211 | 208 | ); |
| @@ -221,12 +218,10 @@ impl Av1Decoder { | ||
| 221 | 218 | )?); |
| 222 | 219 | session.submit(&surface, buffers)?; |
| 223 | 220 | |
| 224 | - for slot in 0..NUM_REF_FRAMES { | |
| 225 | - if header.refresh_frame_flags & (1 << slot) != 0 { | |
| 226 | - self.refs[slot] = Some(Rc::clone(&surface)); | |
| 227 | - self.grained[slot].clone_from(&grained); | |
| 228 | - } | |
| 229 | - } | |
| 221 | + self.refs | |
| 222 | + .refresh(header.refresh_frame_flags, Some(Rc::clone(&surface))); | |
| 223 | + self.grained | |
| 224 | + .refresh(header.refresh_frame_flags, grained.clone()); | |
| 230 | 225 | |
| 231 | 226 | if header.show_frame { |
| 232 | 227 | let out = grained.unwrap_or_else(|| Rc::clone(&surface)); |
| @@ -269,8 +264,8 @@ impl Av1Decoder { | ||
| 269 | 264 | { |
| 270 | 265 | return Ok(()); |
| 271 | 266 | } |
| 272 | - self.refs = [const { None }; NUM_REF_FRAMES]; | |
| 273 | - self.grained = [const { None }; NUM_REF_FRAMES]; | |
| 267 | + self.refs = RefSlots::new(); | |
| 268 | + self.grained = RefSlots::new(); | |
| 274 | 269 | let grain_surfaces = if seq.film_grain_params_present { |
| 275 | 270 | GRAIN_SURFACES |
| 276 | 271 | } else { |
crates/ec-hw/src/hw.rsmodified+13 -4
| @@ -5,6 +5,7 @@ use crate::picture::I420; | ||
| 5 | 5 | use crate::types::{Codec, CodedFrame, EncCodec, EncoderConfig, FrameMetadata, RateControlMode}; |
| 6 | 6 | use crate::vkdec264::VkH264Decoder; |
| 7 | 7 | use crate::vkdec265::VkH265Decoder; |
| 8 | +use crate::vkdec_av1::VkAv1Decoder; | |
| 8 | 9 | |
| 9 | 10 | #[cfg(target_os = "linux")] |
| 10 | 11 | use crate::dec::Decoder as VaDecoder; |
| @@ -59,7 +60,10 @@ pub fn capabilities() -> Vec<Capability> { | ||
| 59 | 60 | for caps in gpu.caps().into_iter().filter(|c| { |
| 60 | 61 | c.direction == ec_vk::Direction::Encode |
| 61 | 62 | || (c.direction == ec_vk::Direction::Decode |
| 62 | - && matches!(c.codec, ec_vk::Codec::H264 | ec_vk::Codec::H265)) | |
| 63 | + && matches!( | |
| 64 | + c.codec, | |
| 65 | + ec_vk::Codec::H264 | ec_vk::Codec::H265 | ec_vk::Codec::Av1 | |
| 66 | + )) | |
| 63 | 67 | }) { |
| 64 | 68 | let codec = match caps.codec { |
| 65 | 69 | ec_vk::Codec::H264 => Codec::H264, |
| @@ -423,6 +427,7 @@ fn open_va(config: EncoderConfig) -> Result<EncInner> { | ||
| 423 | 427 | enum VkDecoder { |
| 424 | 428 | H264(VkH264Decoder), |
| 425 | 429 | H265(VkH265Decoder), |
| 430 | + Av1(VkAv1Decoder), | |
| 426 | 431 | } |
| 427 | 432 | |
| 428 | 433 | impl VkDecoder { |
| @@ -430,6 +435,7 @@ impl VkDecoder { | ||
| 430 | 435 | match self { |
| 431 | 436 | VkDecoder::H264(d) => d.decode(data, timestamp), |
| 432 | 437 | VkDecoder::H265(d) => d.decode(data, timestamp), |
| 438 | + VkDecoder::Av1(d) => d.decode(data, timestamp), | |
| 433 | 439 | } |
| 434 | 440 | } |
| 435 | 441 | |
| @@ -437,6 +443,7 @@ impl VkDecoder { | ||
| 437 | 443 | match self { |
| 438 | 444 | VkDecoder::H264(d) => d.next_frame(), |
| 439 | 445 | VkDecoder::H265(d) => d.next_frame(), |
| 446 | + VkDecoder::Av1(d) => d.next_frame(), | |
| 440 | 447 | } |
| 441 | 448 | } |
| 442 | 449 | |
| @@ -444,6 +451,7 @@ impl VkDecoder { | ||
| 444 | 451 | match self { |
| 445 | 452 | VkDecoder::H264(d) => d.flush(), |
| 446 | 453 | VkDecoder::H265(d) => d.flush(), |
| 454 | + VkDecoder::Av1(d) => d.flush(), | |
| 447 | 455 | } |
| 448 | 456 | } |
| 449 | 457 | } |
| @@ -466,13 +474,14 @@ pub struct HwDecoder { | ||
| 466 | 474 | } |
| 467 | 475 | |
| 468 | 476 | impl HwDecoder { |
| 469 | - /// Open a decoder for `codec`. H.264 and H.265 try Vulkan Video first, then VA-API (Linux). | |
| 477 | + /// Open a decoder for `codec`. H.264, H.265 and AV1 try Vulkan Video first, then VA-API (Linux). | |
| 470 | 478 | pub fn open(codec: Codec) -> Result<HwDecoder> { |
| 471 | 479 | let mut reasons = Vec::new(); |
| 472 | 480 | let vk = match codec { |
| 473 | 481 | Codec::H264 => VkH264Decoder::new().map(VkDecoder::H264), |
| 474 | 482 | Codec::H265 => VkH265Decoder::new().map(VkDecoder::H265), |
| 475 | - _ => Err(Error::unsupported("Vulkan Video decode", "H.264 and H.265 only")), | |
| 483 | + Codec::Av1 => VkAv1Decoder::new().map(VkDecoder::Av1), | |
| 484 | + _ => Err(Error::unsupported("Vulkan Video decode", "H.264, H.265 and AV1 only")), | |
| 476 | 485 | }; |
| 477 | 486 | match vk { |
| 478 | 487 | Ok(inner) => { |
| @@ -482,7 +491,7 @@ impl HwDecoder { | ||
| 482 | 491 | replay: Some(Vec::new()), |
| 483 | 492 | }); |
| 484 | 493 | } |
| 485 | - Err(e) if matches!(codec, Codec::H264 | Codec::H265) => { | |
| 494 | + Err(e) if matches!(codec, Codec::H264 | Codec::H265 | Codec::Av1) => { | |
| 486 | 495 | reasons.push(format!("Vk: {e}")); |
| 487 | 496 | } |
| 488 | 497 | Err(_) => {} |
crates/ec-hw/src/lib.rsmodified+2 -0
| @@ -63,6 +63,7 @@ pub mod error; | ||
| 63 | 63 | pub(crate) mod dpb264; |
| 64 | 64 | #[cfg_attr(not(target_os = "linux"), allow(dead_code))] |
| 65 | 65 | pub(crate) mod h264_common; |
| 66 | +pub(crate) mod av1_refs; | |
| 66 | 67 | pub(crate) mod hevc_dpb; |
| 67 | 68 | pub mod hw; |
| 68 | 69 | #[cfg(target_os = "linux")] |
| @@ -76,6 +77,7 @@ pub mod reset; | ||
| 76 | 77 | pub mod types; |
| 77 | 78 | pub(crate) mod vkdec264; |
| 78 | 79 | pub(crate) mod vkdec265; |
| 80 | +pub(crate) mod vkdec_av1; | |
| 79 | 81 | |
| 80 | 82 | #[cfg(target_os = "linux")] |
| 81 | 83 | pub use dec::{Decoder, StreamInfo}; |
crates/ec-hw/src/vkdec_av1.rsadded+480 -0
| @@ -0,0 +1,480 @@ | ||
| 1 | +//! AV1 decoding over Vulkan Video: the reference slots over DPB slot indices. | |
| 2 | + | |
| 3 | +use std::collections::VecDeque; | |
| 4 | +use std::rc::Rc; | |
| 5 | + | |
| 6 | +use ec_av1_syntax::{ | |
| 7 | + Av1Parser, ChromaSamplePosition, ColorConfig, FrameHeader, FrameType, InterpolationFilter, | |
| 8 | + NUM_REF_FRAMES, ObuKind, REFS_PER_FRAME, RestorationType, SequenceHeader, Tile, TxMode, | |
| 9 | + WarpModel, | |
| 10 | +}; | |
| 11 | +use ec_vk::{ | |
| 12 | + Av1Cdef, Av1ColorConfig, Av1DecodeConfig, Av1DecodePicture, Av1DecodeSession, Av1DeltaParams, | |
| 13 | + Av1FrameHeader, Av1FrameType, Av1GlobalMotion, Av1InterpolationFilter, Av1LoopFilter, | |
| 14 | + Av1LoopRestoration, Av1Quantization, Av1RefSlot, Av1Restoration, Av1Segmentation, | |
| 15 | + Av1SequenceHeader, Av1TileInfo, Av1TimingInfo, Av1TxMode, Av1WarpModel, | |
| 16 | +}; | |
| 17 | + | |
| 18 | +use crate::av1_refs::RefSlots; | |
| 19 | +use crate::error::{Error, Result}; | |
| 20 | +use crate::picture::{I420, nv12_to_i420}; | |
| 21 | + | |
| 22 | +const DPB_SLOTS: u32 = NUM_REF_FRAMES as u32 + 1; | |
| 23 | + | |
| 24 | +struct Session { | |
| 25 | + av1: Av1DecodeSession, | |
| 26 | + width: u32, | |
| 27 | + height: u32, | |
| 28 | + sequence: Option<SequenceHeader>, | |
| 29 | +} | |
| 30 | + | |
| 31 | +struct Reference { | |
| 32 | + state: Av1RefSlot, | |
| 33 | + picture: I420, | |
| 34 | +} | |
| 35 | + | |
| 36 | +/// AV1 Vulkan Video decoder: reference bookkeeping in Rust, frames decoded into DPB slots. | |
| 37 | +pub(crate) struct VkAv1Decoder { | |
| 38 | + gpu: ec_vk::Gpu, | |
| 39 | + parser: Av1Parser, | |
| 40 | + session: Option<Session>, | |
| 41 | + refs: RefSlots<Rc<Reference>>, | |
| 42 | + ready: VecDeque<(i64, I420)>, | |
| 43 | +} | |
| 44 | + | |
| 45 | +impl VkAv1Decoder { | |
| 46 | + pub(crate) fn new() -> Result<Self> { | |
| 47 | + let gpu = crate::hw::vk_gpu() | |
| 48 | + .ok_or_else(|| Error::unsupported("Vulkan Video AV1 decode", "no Vulkan device"))?; | |
| 49 | + Ok(VkAv1Decoder { | |
| 50 | + gpu, | |
| 51 | + parser: Av1Parser::new(), | |
| 52 | + session: None, | |
| 53 | + refs: RefSlots::new(), | |
| 54 | + ready: VecDeque::new(), | |
| 55 | + }) | |
| 56 | + } | |
| 57 | + | |
| 58 | + pub(crate) fn decode(&mut self, data: &[u8], timestamp: i64) -> Result<()> { | |
| 59 | + let mut pending: Option<Box<FrameHeader>> = None; | |
| 60 | + for obu in self.parser.parse_temporal_unit(data)? { | |
| 61 | + match obu.kind { | |
| 62 | + ObuKind::FrameHeader(header) if header.show_existing_frame => { | |
| 63 | + self.show_existing(&header, timestamp)?; | |
| 64 | + } | |
| 65 | + ObuKind::FrameHeader(header) => pending = Some(header), | |
| 66 | + ObuKind::TileGroup(tiles) => { | |
| 67 | + if let Some(header) = pending.take() { | |
| 68 | + self.decode_frame(&header, &tiles, data, timestamp)?; | |
| 69 | + } | |
| 70 | + } | |
| 71 | + ObuKind::Frame(header, _) if header.show_existing_frame => { | |
| 72 | + self.show_existing(&header, timestamp)?; | |
| 73 | + } | |
| 74 | + ObuKind::Frame(header, tiles) => { | |
| 75 | + self.decode_frame(&header, &tiles, data, timestamp)?; | |
| 76 | + } | |
| 77 | + _ => {} | |
| 78 | + } | |
| 79 | + } | |
| 80 | + Ok(()) | |
| 81 | + } | |
| 82 | + | |
| 83 | + pub(crate) fn next_frame(&mut self) -> Option<(i64, I420)> { | |
| 84 | + self.ready.pop_front() | |
| 85 | + } | |
| 86 | + | |
| 87 | + pub(crate) fn flush(&mut self) -> Result<()> { | |
| 88 | + Ok(()) | |
| 89 | + } | |
| 90 | + | |
| 91 | + fn show_existing(&mut self, header: &FrameHeader, timestamp: i64) -> Result<()> { | |
| 92 | + let reference = self | |
| 93 | + .refs | |
| 94 | + .get(usize::from(header.frame_to_show_map_idx)) | |
| 95 | + .cloned() | |
| 96 | + .ok_or_else(|| { | |
| 97 | + Error::Stream(ec_core::Error::corrupt( | |
| 98 | + "AV1 show_existing_frame names an empty reference slot", | |
| 99 | + )) | |
| 100 | + })?; | |
| 101 | + self.ready.push_back((timestamp, reference.picture.clone())); | |
| 102 | + if header.frame_type == FrameType::Key { | |
| 103 | + self.refs.refresh(u8::MAX, Some(reference)); | |
| 104 | + } | |
| 105 | + Ok(()) | |
| 106 | + } | |
| 107 | + | |
| 108 | + fn decode_frame( | |
| 109 | + &mut self, | |
| 110 | + header: &FrameHeader, | |
| 111 | + tiles: &[Tile], | |
| 112 | + data: &[u8], | |
| 113 | + timestamp: i64, | |
| 114 | + ) -> Result<()> { | |
| 115 | + let seq = self | |
| 116 | + .parser | |
| 117 | + .sequence_header() | |
| 118 | + .cloned() | |
| 119 | + .ok_or_else(|| Error::config("AV1 frame before any sequence header"))?; | |
| 120 | + check_supported(&seq, header)?; | |
| 121 | + let (width, height) = (header.upscaled_width, header.frame_height); | |
| 122 | + self.ensure_session(&seq, width, height)?; | |
| 123 | + | |
| 124 | + let mut ref_frame_slot = [-1i32; REFS_PER_FRAME]; | |
| 125 | + let mut refs: Vec<Av1RefSlot> = Vec::new(); | |
| 126 | + if matches!(header.frame_type, FrameType::Inter | FrameType::Switch) { | |
| 127 | + for (name, &index) in header.ref_frame_idx.iter().enumerate() { | |
| 128 | + let reference = self.refs.get(usize::from(index)).ok_or_else(|| { | |
| 129 | + Error::Stream(ec_core::Error::corrupt(format!( | |
| 130 | + "AV1 inter frame names reference slot {index} (ref {name}), \ | |
| 131 | + which no decoded frame has filled" | |
| 132 | + ))) | |
| 133 | + })?; | |
| 134 | + ref_frame_slot[name] = reference.state.slot as i32; | |
| 135 | + if !refs.iter().any(|r| r.slot == reference.state.slot) { | |
| 136 | + refs.push(reference.state); | |
| 137 | + } | |
| 138 | + } | |
| 139 | + } | |
| 140 | + | |
| 141 | + let setup_slot = self.free_slot()?; | |
| 142 | + let frame = frame_header(header, &seq); | |
| 143 | + let offsets: Vec<u32> = tiles.iter().map(|t| t.offset as u32).collect(); | |
| 144 | + let sizes: Vec<u32> = tiles.iter().map(|t| t.size as u32).collect(); | |
| 145 | + let picture = Av1DecodePicture { | |
| 146 | + header: &frame, | |
| 147 | + setup_slot, | |
| 148 | + ref_frame_slot, | |
| 149 | + refs: &refs, | |
| 150 | + tile_data: data, | |
| 151 | + tile_offsets: &offsets, | |
| 152 | + tile_sizes: &sizes, | |
| 153 | + }; | |
| 154 | + let session = self | |
| 155 | + .session | |
| 156 | + .as_mut() | |
| 157 | + .ok_or_else(|| Error::config("AV1 picture before any session"))?; | |
| 158 | + session.av1.decode(&picture)?; | |
| 159 | + | |
| 160 | + if !(header.show_frame || header.refresh_frame_flags != 0) { | |
| 161 | + return Ok(()); | |
| 162 | + } | |
| 163 | + let (y, uv) = session.av1.read_output(width, height)?; | |
| 164 | + let w = width as usize; | |
| 165 | + let decoded = nv12_to_i420(&y, w, &uv, w, w, height as usize); | |
| 166 | + let state = Av1RefSlot { | |
| 167 | + slot: setup_slot, | |
| 168 | + frame_type: av1_frame_type(header.frame_type), | |
| 169 | + order_hint: header.order_hint as u8, | |
| 170 | + ref_order_hints: saved_order_hints(&header.order_hints), | |
| 171 | + sign_bias: sign_bias(&header.ref_frame_sign_bias), | |
| 172 | + disable_frame_end_update_cdf: header.disable_frame_end_update_cdf, | |
| 173 | + segmentation_enabled: header.segmentation.enabled, | |
| 174 | + }; | |
| 175 | + if header.show_frame { | |
| 176 | + self.ready.push_back((timestamp, decoded.clone())); | |
| 177 | + } | |
| 178 | + if header.refresh_frame_flags != 0 { | |
| 179 | + let reference = Rc::new(Reference { | |
| 180 | + state, | |
| 181 | + picture: decoded, | |
| 182 | + }); | |
| 183 | + self.refs | |
| 184 | + .refresh(header.refresh_frame_flags, Some(reference)); | |
| 185 | + } | |
| 186 | + Ok(()) | |
| 187 | + } | |
| 188 | + | |
| 189 | + fn ensure_session(&mut self, seq: &SequenceHeader, width: u32, height: u32) -> Result<()> { | |
| 190 | + let mut session = match self.session.take() { | |
| 191 | + Some(s) if width <= s.width && height <= s.height => s, | |
| 192 | + old => { | |
| 193 | + let (width, height) = old.map_or((width, height), |s| { | |
| 194 | + (width.max(s.width), height.max(s.height)) | |
| 195 | + }); | |
| 196 | + self.refs = RefSlots::new(); | |
| 197 | + let av1 = Av1DecodeSession::new( | |
| 198 | + &self.gpu, | |
| 199 | + &Av1DecodeConfig { | |
| 200 | + max_width: width, | |
| 201 | + max_height: height, | |
| 202 | + max_dpb_slots: DPB_SLOTS, | |
| 203 | + }, | |
| 204 | + )?; | |
| 205 | + Session { | |
| 206 | + av1, | |
| 207 | + width, | |
| 208 | + height, | |
| 209 | + sequence: None, | |
| 210 | + } | |
| 211 | + } | |
| 212 | + }; | |
| 213 | + if session.sequence.as_ref() != Some(seq) { | |
| 214 | + session.av1.set_sequence_header(&av1_sequence(seq))?; | |
| 215 | + session.sequence = Some(seq.clone()); | |
| 216 | + } | |
| 217 | + self.session = Some(session); | |
| 218 | + Ok(()) | |
| 219 | + } | |
| 220 | + | |
| 221 | + fn free_slot(&self) -> Result<u32> { | |
| 222 | + (0..DPB_SLOTS) | |
| 223 | + .find(|slot| self.refs.filled().all(|r| r.state.slot != *slot)) | |
| 224 | + .ok_or_else(|| Error::config("AV1 Vulkan decode: every DPB slot is in use")) | |
| 225 | + } | |
| 226 | +} | |
| 227 | + | |
| 228 | +fn check_supported(seq: &SequenceHeader, header: &FrameHeader) -> Result<()> { | |
| 229 | + let c = &seq.color_config; | |
| 230 | + if seq.seq_profile != 0 | |
| 231 | + || c.bit_depth != 8 | |
| 232 | + || c.mono_chrome | |
| 233 | + || c.subsampling_x != 1 | |
| 234 | + || c.subsampling_y != 1 | |
| 235 | + { | |
| 236 | + return Err(Error::unsupported( | |
| 237 | + "Vulkan AV1 decode", | |
| 238 | + "only Main 8-bit 4:2:0", | |
| 239 | + )); | |
| 240 | + } | |
| 241 | + if header.film_grain.apply_grain { | |
| 242 | + return Err(Error::unsupported("Vulkan AV1 decode", "film grain")); | |
| 243 | + } | |
| 244 | + if header.upscaled_width % 2 != 0 || header.frame_height % 2 != 0 { | |
| 245 | + return Err(Error::unsupported("Vulkan AV1 decode", "odd frame size")); | |
| 246 | + } | |
| 247 | + Ok(()) | |
| 248 | +} | |
| 249 | + | |
| 250 | +fn av1_frame_type(t: FrameType) -> Av1FrameType { | |
| 251 | + match t { | |
| 252 | + FrameType::Key => Av1FrameType::Key, | |
| 253 | + FrameType::Inter => Av1FrameType::Inter, | |
| 254 | + FrameType::IntraOnly => Av1FrameType::IntraOnly, | |
| 255 | + FrameType::Switch => Av1FrameType::Switch, | |
| 256 | + } | |
| 257 | +} | |
| 258 | + | |
| 259 | +/// `RefFrameSignBias` as a mask: bit `n` set when reference name `n` (LAST = 1) is a future frame. | |
| 260 | +fn sign_bias(bias: &[bool; REFS_PER_FRAME]) -> u8 { | |
| 261 | + bias.iter() | |
| 262 | + .enumerate() | |
| 263 | + .filter(|(_, set)| **set) | |
| 264 | + .fold(0u8, |mask, (name, _)| mask | 1 << (name + 1)) | |
| 265 | +} | |
| 266 | + | |
| 267 | +/// `SavedOrderHints` indexed by reference name, index 0 (INTRA_FRAME) unused. | |
| 268 | +fn saved_order_hints(hints: &[u32; REFS_PER_FRAME]) -> [u8; NUM_REF_FRAMES] { | |
| 269 | + let mut out = [0u8; NUM_REF_FRAMES]; | |
| 270 | + for (name, &hint) in hints.iter().enumerate() { | |
| 271 | + out[name + 1] = hint as u8; | |
| 272 | + } | |
| 273 | + out | |
| 274 | +} | |
| 275 | + | |
| 276 | +fn color(c: &ColorConfig) -> Av1ColorConfig { | |
| 277 | + Av1ColorConfig { | |
| 278 | + bit_depth: c.bit_depth, | |
| 279 | + mono_chrome: c.mono_chrome, | |
| 280 | + color_range: c.color_range, | |
| 281 | + separate_uv_delta_q: c.separate_uv_delta_q, | |
| 282 | + subsampling_x: c.subsampling_x, | |
| 283 | + subsampling_y: c.subsampling_y, | |
| 284 | + color_primaries: c.color_primaries, | |
| 285 | + transfer_characteristics: c.transfer_characteristics, | |
| 286 | + matrix_coefficients: c.matrix_coefficients, | |
| 287 | + chroma_sample_position: match c.chroma_sample_position { | |
| 288 | + ChromaSamplePosition::Unknown => 0, | |
| 289 | + ChromaSamplePosition::Vertical => 1, | |
| 290 | + ChromaSamplePosition::Colocated => 2, | |
| 291 | + ChromaSamplePosition::Reserved => 3, | |
| 292 | + }, | |
| 293 | + } | |
| 294 | +} | |
| 295 | + | |
| 296 | +fn av1_sequence(s: &SequenceHeader) -> Av1SequenceHeader { | |
| 297 | + Av1SequenceHeader { | |
| 298 | + seq_profile: s.seq_profile, | |
| 299 | + still_picture: s.still_picture, | |
| 300 | + reduced_still_picture_header: s.reduced_still_picture_header, | |
| 301 | + timing_info: s.timing_info.map(|t| Av1TimingInfo { | |
| 302 | + equal_picture_interval: t.equal_picture_interval, | |
| 303 | + num_units_in_display_tick: t.num_units_in_display_tick, | |
| 304 | + time_scale: t.time_scale, | |
| 305 | + num_ticks_per_picture_minus_1: t.num_ticks_per_picture.saturating_sub(1), | |
| 306 | + }), | |
| 307 | + initial_display_delay_present: s.initial_display_delay_present_flag, | |
| 308 | + frame_width_bits_minus_1: (s.frame_width_bits - 1) as u8, | |
| 309 | + frame_height_bits_minus_1: (s.frame_height_bits - 1) as u8, | |
| 310 | + max_frame_width_minus_1: (s.max_frame_width - 1) as u16, | |
| 311 | + max_frame_height_minus_1: (s.max_frame_height - 1) as u16, | |
| 312 | + frame_id_numbers_present: s.frame_id_numbers_present_flag, | |
| 313 | + delta_frame_id_length_minus_2: s.delta_frame_id_length.saturating_sub(2) as u8, | |
| 314 | + additional_frame_id_length_minus_1: s.additional_frame_id_length.saturating_sub(1) as u8, | |
| 315 | + order_hint_bits_minus_1: s.order_hint_bits.saturating_sub(1) as u8, | |
| 316 | + seq_force_integer_mv: s.seq_force_integer_mv, | |
| 317 | + seq_force_screen_content_tools: s.seq_force_screen_content_tools, | |
| 318 | + use_128x128_superblock: s.use_128x128_superblock, | |
| 319 | + enable_filter_intra: s.enable_filter_intra, | |
| 320 | + enable_intra_edge_filter: s.enable_intra_edge_filter, | |
| 321 | + enable_interintra_compound: s.enable_interintra_compound, | |
| 322 | + enable_masked_compound: s.enable_masked_compound, | |
| 323 | + enable_warped_motion: s.enable_warped_motion, | |
| 324 | + enable_dual_filter: s.enable_dual_filter, | |
| 325 | + enable_order_hint: s.enable_order_hint, | |
| 326 | + enable_jnt_comp: s.enable_jnt_comp, | |
| 327 | + enable_ref_frame_mvs: s.enable_ref_frame_mvs, | |
| 328 | + enable_superres: s.enable_superres, | |
| 329 | + enable_cdef: s.enable_cdef, | |
| 330 | + enable_restoration: s.enable_restoration, | |
| 331 | + color_config: color(&s.color_config), | |
| 332 | + film_grain_params_present: s.film_grain_params_present, | |
| 333 | + } | |
| 334 | +} | |
| 335 | + | |
| 336 | +fn restoration(r: RestorationType) -> Av1Restoration { | |
| 337 | + match r { | |
| 338 | + RestorationType::None => Av1Restoration::None, | |
| 339 | + RestorationType::Wiener => Av1Restoration::Wiener, | |
| 340 | + RestorationType::Sgrproj => Av1Restoration::Sgrproj, | |
| 341 | + RestorationType::Switchable => Av1Restoration::Switchable, | |
| 342 | + } | |
| 343 | +} | |
| 344 | + | |
| 345 | +fn frame_header(h: &FrameHeader, s: &SequenceHeader) -> Av1FrameHeader { | |
| 346 | + let sb_shift = s.sb_shift(); | |
| 347 | + let mut global_motion = [Av1GlobalMotion::default(); 8]; | |
| 348 | + let mut order_hints = [0u8; 8]; | |
| 349 | + for i in 0..REFS_PER_FRAME { | |
| 350 | + global_motion[i + 1] = Av1GlobalMotion { | |
| 351 | + model: match h.global_motion[i].model { | |
| 352 | + WarpModel::Identity => Av1WarpModel::Identity, | |
| 353 | + WarpModel::Translation => Av1WarpModel::Translation, | |
| 354 | + WarpModel::Rotzoom => Av1WarpModel::Rotzoom, | |
| 355 | + WarpModel::Affine => Av1WarpModel::Affine, | |
| 356 | + }, | |
| 357 | + params: h.global_motion[i].params, | |
| 358 | + }; | |
| 359 | + order_hints[i + 1] = h.order_hints[i] as u8; | |
| 360 | + } | |
| 361 | + Av1FrameHeader { | |
| 362 | + frame_type: av1_frame_type(h.frame_type), | |
| 363 | + show_frame: h.show_frame, | |
| 364 | + showable_frame: h.showable_frame, | |
| 365 | + error_resilient_mode: h.error_resilient_mode, | |
| 366 | + disable_cdf_update: h.disable_cdf_update, | |
| 367 | + allow_screen_content_tools: h.allow_screen_content_tools, | |
| 368 | + force_integer_mv: h.force_integer_mv, | |
| 369 | + current_frame_id: h.current_frame_id, | |
| 370 | + frame_size_override_flag: h.frame_size_override_flag, | |
| 371 | + order_hint: h.order_hint as u8, | |
| 372 | + primary_ref_frame: h.primary_ref_frame, | |
| 373 | + refresh_frame_flags: h.refresh_frame_flags, | |
| 374 | + frame_width: h.frame_width, | |
| 375 | + frame_height: h.frame_height, | |
| 376 | + upscaled_width: h.upscaled_width, | |
| 377 | + render_width: h.render_width, | |
| 378 | + render_height: h.render_height, | |
| 379 | + use_superres: h.use_superres, | |
| 380 | + superres_denom: h.superres_denom, | |
| 381 | + allow_intrabc: h.allow_intrabc, | |
| 382 | + allow_high_precision_mv: h.allow_high_precision_mv, | |
| 383 | + is_motion_mode_switchable: h.is_motion_mode_switchable, | |
| 384 | + use_ref_frame_mvs: h.use_ref_frame_mvs, | |
| 385 | + frame_refs_short_signaling: h.frame_refs_short_signaling, | |
| 386 | + disable_frame_end_update_cdf: h.disable_frame_end_update_cdf, | |
| 387 | + reference_select: h.reference_select, | |
| 388 | + skip_mode_present: h.skip_mode_present, | |
| 389 | + allow_warped_motion: h.allow_warped_motion, | |
| 390 | + reduced_tx_set: h.reduced_tx_set, | |
| 391 | + interpolation_filter: match h.interpolation_filter { | |
| 392 | + InterpolationFilter::Eighttap => Av1InterpolationFilter::EightTap, | |
| 393 | + InterpolationFilter::EighttapSmooth => Av1InterpolationFilter::EightTapSmooth, | |
| 394 | + InterpolationFilter::EighttapSharp => Av1InterpolationFilter::EightTapSharp, | |
| 395 | + InterpolationFilter::Bilinear => Av1InterpolationFilter::Bilinear, | |
| 396 | + InterpolationFilter::Switchable => Av1InterpolationFilter::Switchable, | |
| 397 | + }, | |
| 398 | + tx_mode: match h.tx_mode { | |
| 399 | + TxMode::Only4x4 => Av1TxMode::Only4x4, | |
| 400 | + TxMode::Largest => Av1TxMode::Largest, | |
| 401 | + TxMode::Select => Av1TxMode::Select, | |
| 402 | + }, | |
| 403 | + tile_info: Av1TileInfo { | |
| 404 | + uniform_spacing: h.tile_info.uniform_spacing, | |
| 405 | + cols: h.tile_info.cols as u8, | |
| 406 | + rows: h.tile_info.rows as u8, | |
| 407 | + mi_col_starts: h | |
| 408 | + .tile_info | |
| 409 | + .mi_col_starts | |
| 410 | + .iter() | |
| 411 | + .map(|&v| v as u16) | |
| 412 | + .collect(), | |
| 413 | + mi_row_starts: h | |
| 414 | + .tile_info | |
| 415 | + .mi_row_starts | |
| 416 | + .iter() | |
| 417 | + .map(|&v| v as u16) | |
| 418 | + .collect(), | |
| 419 | + width_in_sbs_minus_1: h.tile_info.width_in_sbs_minus_1(sb_shift), | |
| 420 | + height_in_sbs_minus_1: h.tile_info.height_in_sbs_minus_1(sb_shift), | |
| 421 | + context_update_tile_id: h.tile_info.context_update_tile_id as u16, | |
| 422 | + tile_size_bytes_minus_1: h.tile_info.tile_size_bytes.saturating_sub(1) as u8, | |
| 423 | + }, | |
| 424 | + quantization: Av1Quantization { | |
| 425 | + base_q_idx: h.quantization.base_q_idx, | |
| 426 | + delta_q_y_dc: h.quantization.delta_q_y_dc, | |
| 427 | + delta_q_u_dc: h.quantization.delta_q_u_dc, | |
| 428 | + delta_q_u_ac: h.quantization.delta_q_u_ac, | |
| 429 | + delta_q_v_dc: h.quantization.delta_q_v_dc, | |
| 430 | + delta_q_v_ac: h.quantization.delta_q_v_ac, | |
| 431 | + using_qmatrix: h.quantization.using_qmatrix, | |
| 432 | + qm_y: h.quantization.qm_y, | |
| 433 | + qm_u: h.quantization.qm_u, | |
| 434 | + qm_v: h.quantization.qm_v, | |
| 435 | + }, | |
| 436 | + segmentation: Av1Segmentation { | |
| 437 | + enabled: h.segmentation.enabled, | |
| 438 | + update_map: h.segmentation.update_map, | |
| 439 | + temporal_update: h.segmentation.temporal_update, | |
| 440 | + update_data: h.segmentation.update_data, | |
| 441 | + feature_enabled: h.segmentation.feature_enabled, | |
| 442 | + feature_data: h.segmentation.feature_data, | |
| 443 | + }, | |
| 444 | + delta: Av1DeltaParams { | |
| 445 | + q_present: h.delta.q_present, | |
| 446 | + q_res: h.delta.q_res, | |
| 447 | + lf_present: h.delta.lf_present, | |
| 448 | + lf_res: h.delta.lf_res, | |
| 449 | + lf_multi: h.delta.lf_multi, | |
| 450 | + }, | |
| 451 | + loop_filter: Av1LoopFilter { | |
| 452 | + level: h.loop_filter.level, | |
| 453 | + sharpness: h.loop_filter.sharpness, | |
| 454 | + delta_enabled: h.loop_filter.delta_enabled, | |
| 455 | + delta_update: h.loop_filter.delta_update, | |
| 456 | + ref_deltas: h.loop_filter.ref_deltas, | |
| 457 | + mode_deltas: h.loop_filter.mode_deltas, | |
| 458 | + }, | |
| 459 | + cdef: Av1Cdef { | |
| 460 | + damping_minus_3: h.cdef.damping.saturating_sub(3), | |
| 461 | + bits: h.cdef.bits, | |
| 462 | + y_pri: h.cdef.y_pri_strength, | |
| 463 | + y_sec: h.cdef.y_sec_strength, | |
| 464 | + uv_pri: h.cdef.uv_pri_strength, | |
| 465 | + uv_sec: h.cdef.uv_sec_strength, | |
| 466 | + }, | |
| 467 | + loop_restoration: Av1LoopRestoration { | |
| 468 | + frame_restoration_type: h.loop_restoration.frame_restoration_type.map(restoration), | |
| 469 | + loop_restoration_size: h | |
| 470 | + .loop_restoration | |
| 471 | + .loop_restoration_size | |
| 472 | + .map(|v| v as u16), | |
| 473 | + }, | |
| 474 | + global_motion, | |
| 475 | + order_hints, | |
| 476 | + skip_mode_frame: h.skip_mode_frame, | |
| 477 | + expected_frame_id: [0; 8], | |
| 478 | + apply_grain: h.film_grain.apply_grain, | |
| 479 | + } | |
| 480 | +} |
shims/vk-video-ffi/src/av1_decode.rsmodified+1 -1
| @@ -1338,7 +1338,7 @@ fn std_cdef(c: &Av1Cdef) -> vk::video::StdVideoAV1CDEF { | ||
| 1338 | 1338 | fn std_loop_restoration(l: &Av1LoopRestoration) -> vk::video::StdVideoAV1LoopRestoration { |
| 1339 | 1339 | vk::video::StdVideoAV1LoopRestoration { |
| 1340 | 1340 | FrameRestorationType: l.frame_restoration_type.map(std_restoration), |
| 1341 | - LoopRestorationSize: l.loop_restoration_size, | |
| 1341 | + LoopRestorationSize: l.loop_restoration_size.map(|v| (v.max(32).trailing_zeros() - 5) as u16), | |
| 1342 | 1342 | } |
| 1343 | 1343 | } |
| 1344 | 1344 |