vk decode: 10-bit H.265 Main10 and AV1 on Vulkan (P010 session, bit depth from SPS/sequence header); P010 -> I420 via the shared picture conversion, bit-identical to VA
Browse files at this commitcad917bb1e7261c5f1c5887a006bea3555ea2b38Parent: ce0dca211 changed files +317 -152
- crates/ec-hw/examples/hw_decode.rs +67 -17
- crates/ec-hw/src/frame.rs +1 -74
- crates/ec-hw/src/hw.rs +1 -1
- crates/ec-hw/src/picture.rs +108 -0
- crates/ec-hw/src/vkdec265.rs +33 -20
- crates/ec-hw/src/vkdec_av1.rs +22 -9
- shims/vk-video-ffi/examples/av1_decode_smoke.rs +1 -0
- shims/vk-video-ffi/examples/h265_decode_smoke.rs +1 -0
- shims/vk-video-ffi/src/av1_decode.rs +23 -12
- shims/vk-video-ffi/src/h265_decode.rs +30 -11
- shims/vk-video-ffi/src/session.rs +30 -8
crates/ec-hw/examples/hw_decode.rsmodified+67 -17
| @@ -1,4 +1,5 @@ | ||
| 1 | 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 | +//! The 10-bit H.265 and AV1 streams run on Vulkan and are compared with the VA-API decode of the same stream. | |
| 2 | 3 | //! |
| 3 | 4 | //! ```text |
| 4 | 5 | //! cargo run -p ec-hw --example hw_decode -- <scratch dir> |
| @@ -103,34 +104,83 @@ fn main() { | ||
| 103 | 104 | for case in CASES { |
| 104 | 105 | run(&dir, case); |
| 105 | 106 | } |
| 106 | - ten_bit_falls_back(&dir); | |
| 107 | + ten_bit(&dir); | |
| 107 | 108 | own_av1(&dir); |
| 108 | 109 | } |
| 109 | 110 | |
| 110 | -fn ten_bit_falls_back(dir: &std::path::Path) { | |
| 111 | - let path = dir.join("ten.hevc"); | |
| 112 | - let p = path.to_str().expect("utf-8 path"); | |
| 113 | - ffmpeg(&["-f", "lavfi", "-i", "testsrc2=size=640x360:rate=30", "-frames:v", "20", "-pix_fmt", "yuv420p10le", "-c:v", "libx265", "-x265-params", "log-level=error", p]); | |
| 114 | - let data = std::fs::read(&path).expect("read 10-bit stream"); | |
| 115 | - let mut dec = ec_hw::HwDecoder::open(ec_hw::Codec::H265).expect("open"); | |
| 116 | - let opened = dec.backend(); | |
| 111 | +fn ten_bit(dir: &Path) { | |
| 112 | + let hevc = dir.join("ten.hevc"); | |
| 113 | + let ivf = dir.join("ten_av1.ivf"); | |
| 114 | + ffmpeg(&["-f", "lavfi", "-i", "testsrc2=size=640x360:rate=30", "-frames:v", "20", "-pix_fmt", "yuv420p10le", "-c:v", "libx265", "-x265-params", "log-level=error", hevc.to_str().expect("utf-8 path")]); | |
| 115 | + ffmpeg(&["-f", "lavfi", "-i", "testsrc2=size=640x360:rate=30", "-frames:v", "30", "-pix_fmt", "yuv420p10le", "-c:v", "libsvtav1", "-f", "ivf", ivf.to_str().expect("utf-8 path")]); | |
| 116 | + let hevc_data = fs::read(&hevc).expect("read 10-bit H.265"); | |
| 117 | + ten_bit_case("ten.hevc", Codec::H265, &nal_units(&hevc_data)); | |
| 118 | + let av1_data = fs::read(&ivf).expect("read 10-bit AV1"); | |
| 119 | + ten_bit_case("ten_av1.ivf", Codec::Av1, &ivf_units(&av1_data)); | |
| 120 | +} | |
| 121 | + | |
| 122 | +fn nal_units(data: &[u8]) -> Vec<&[u8]> { | |
| 117 | 123 | let starts: Vec<usize> = (0..data.len().saturating_sub(3)) |
| 118 | 124 | .filter(|&i| data[i..i + 3] == [0, 0, 1]) |
| 119 | 125 | .map(|i| if i > 0 && data[i - 1] == 0 { i - 1 } else { i }) |
| 120 | 126 | .chain([data.len()]) |
| 121 | 127 | .collect(); |
| 122 | - let mut n = 0; | |
| 123 | - for w in starts.windows(2) { | |
| 124 | - dec.decode(&data[w[0]..w[1]], 0).expect("decode 10-bit"); | |
| 125 | - while dec.next_frame().expect("frame").is_some() { | |
| 126 | - n += 1; | |
| 128 | + starts.windows(2).map(|w| &data[w[0]..w[1]]).collect() | |
| 129 | +} | |
| 130 | + | |
| 131 | +fn ten_bit_case(label: &str, codec: Codec, units: &[&[u8]]) { | |
| 132 | + let mut decoder = HwDecoder::open(codec).expect("open decoder"); | |
| 133 | + let backend = decoder.backend(); | |
| 134 | + assert_eq!(backend, Backend::Vk, "{label}: 10-bit must decode on Vulkan"); | |
| 135 | + let mut ours: Vec<I420> = Vec::new(); | |
| 136 | + for (pts, unit) in units.iter().enumerate() { | |
| 137 | + decoder.decode(unit, pts as i64).expect("decode 10-bit"); | |
| 138 | + drain(&mut decoder, &mut ours); | |
| 139 | + } | |
| 140 | + decoder.flush().expect("flush"); | |
| 141 | + drain(&mut decoder, &mut ours); | |
| 142 | + let theirs = va_frames(codec, units); | |
| 143 | + compare_i420(label, codec, backend, &ours, &theirs); | |
| 144 | +} | |
| 145 | + | |
| 146 | +fn va_frames(codec: Codec, units: &[&[u8]]) -> Vec<I420> { | |
| 147 | + let display = ec_va::open().expect("VA display"); | |
| 148 | + let mut decoder = ec_hw::Decoder::new(&display, codec).expect("VA decoder"); | |
| 149 | + let mut out = Vec::new(); | |
| 150 | + for (pts, unit) in units.iter().enumerate() { | |
| 151 | + decoder.decode(unit, pts as i64).expect("VA decode"); | |
| 152 | + while let Some(frame) = decoder.next_frame() { | |
| 153 | + out.push(frame.to_i420().expect("VA readback")); | |
| 127 | 154 | } |
| 128 | 155 | } |
| 129 | - dec.flush().expect("flush"); | |
| 130 | - while dec.next_frame().expect("frame").is_some() { | |
| 131 | - n += 1; | |
| 156 | + decoder.flush().expect("VA flush"); | |
| 157 | + while let Some(frame) = decoder.next_frame() { | |
| 158 | + out.push(frame.to_i420().expect("VA readback")); | |
| 132 | 159 | } |
| 133 | - println!("ten.hevc: opened {opened:?}, decoded on {:?}, {n} frames", dec.backend()); | |
| 160 | + out | |
| 161 | +} | |
| 162 | + | |
| 163 | +fn compare_i420(label: &str, codec: Codec, backend: Backend, ours: &[I420], theirs: &[I420]) { | |
| 164 | + let worst = ours | |
| 165 | + .iter() | |
| 166 | + .zip(theirs) | |
| 167 | + .map(|(a, b)| { | |
| 168 | + assert_eq!((a.width, a.height), (b.width, b.height), "{label}: frame size"); | |
| 169 | + let sum: f64 = a | |
| 170 | + .y | |
| 171 | + .iter() | |
| 172 | + .zip(&b.y) | |
| 173 | + .map(|(&x, &y)| (x as f64 - y as f64).abs()) | |
| 174 | + .sum(); | |
| 175 | + sum / a.y.len() as f64 | |
| 176 | + }) | |
| 177 | + .fold(0.0f64, f64::max); | |
| 178 | + println!( | |
| 179 | + "{label} {codec:?} {backend:?}: decoded {} frames, VA {} frames, max mean |diff| Y {worst:.6}", | |
| 180 | + ours.len(), | |
| 181 | + theirs.len(), | |
| 182 | + ); | |
| 183 | + assert_eq!(ours.len(), theirs.len(), "{label}: frame count"); | |
| 134 | 184 | } |
| 135 | 185 | |
| 136 | 186 | fn ffmpeg(args: &[&str]) { |
crates/ec-hw/src/frame.rsmodified+1 -74
| @@ -7,6 +7,7 @@ use ec_va::va::{VA_FOURCC_NV12, VA_FOURCC_P010}; | ||
| 7 | 7 | use ec_va::{Display, Image, PrimeSurface, Surface, SurfaceExt}; |
| 8 | 8 | |
| 9 | 9 | use crate::error::{Error, Result}; |
| 10 | +use crate::picture::{check_plane, fill_i420}; | |
| 10 | 11 | use crate::pool::PooledSurface; |
| 11 | 12 | |
| 12 | 13 | pub use crate::picture::{Colour, I420, I420_16, i420_to_nv12, nv12_to_i420}; |
| @@ -191,62 +192,6 @@ impl std::fmt::Debug for Frame { | ||
| 191 | 192 | } |
| 192 | 193 | } |
| 193 | 194 | |
| 194 | -/// Copy NV12/P010 planes into `out`, after validating every plane against it. | |
| 195 | -fn fill_i420( | |
| 196 | - out: &mut I420, | |
| 197 | - bit_depth: u8, | |
| 198 | - y_plane: &[u8], | |
| 199 | - y_pitch: usize, | |
| 200 | - uv_plane: &[u8], | |
| 201 | - uv_pitch: usize, | |
| 202 | -) -> Result<()> { | |
| 203 | - let (w, h) = (out.width as usize, out.height as usize); | |
| 204 | - let (cw, ch) = (w.div_ceil(2), h.div_ceil(2)); | |
| 205 | - let bytes = if bit_depth == 8 { 1 } else { 2 }; | |
| 206 | - check_plane("luma", y_plane.len(), y_pitch, h, w * bytes)?; | |
| 207 | - check_plane("chroma", uv_plane.len(), uv_pitch, ch, cw * 2 * bytes)?; | |
| 208 | - let mut row_buf = vec![0u8; uv_pitch.max(y_pitch).max(w * 2)]; | |
| 209 | - match bit_depth { | |
| 210 | - 8 => { | |
| 211 | - for row in 0..h { | |
| 212 | - let src = row * y_pitch; | |
| 213 | - out.y[row * w..(row + 1) * w].copy_from_slice(&y_plane[src..src + w]); | |
| 214 | - } | |
| 215 | - for row in 0..ch { | |
| 216 | - let src = row * uv_pitch; | |
| 217 | - let line = &mut row_buf[..cw * 2]; | |
| 218 | - line.copy_from_slice(&uv_plane[src..src + cw * 2]); | |
| 219 | - for col in 0..cw { | |
| 220 | - out.u[row * cw + col] = line[col * 2]; | |
| 221 | - out.v[row * cw + col] = line[col * 2 + 1]; | |
| 222 | - } | |
| 223 | - } | |
| 224 | - } | |
| 225 | - _ => { | |
| 226 | - // P010 keeps the sample in the *high* bits of each u16, so the | |
| 227 | - // 8-bit value is the high byte — a shift by 8, not by 2. | |
| 228 | - for row in 0..h { | |
| 229 | - let src = row * y_pitch; | |
| 230 | - let line = &mut row_buf[..w * 2]; | |
| 231 | - line.copy_from_slice(&y_plane[src..src + w * 2]); | |
| 232 | - for col in 0..w { | |
| 233 | - out.y[row * w + col] = line[col * 2 + 1]; | |
| 234 | - } | |
| 235 | - } | |
| 236 | - for row in 0..ch { | |
| 237 | - let src = row * uv_pitch; | |
| 238 | - let line = &mut row_buf[..cw * 4]; | |
| 239 | - line.copy_from_slice(&uv_plane[src..src + cw * 4]); | |
| 240 | - for col in 0..cw { | |
| 241 | - out.u[row * cw + col] = line[col * 4 + 1]; | |
| 242 | - out.v[row * cw + col] = line[col * 4 + 3]; | |
| 243 | - } | |
| 244 | - } | |
| 245 | - } | |
| 246 | - } | |
| 247 | - Ok(()) | |
| 248 | -} | |
| 249 | - | |
| 250 | 195 | /// Copy NV12/P010 planes into `out`, after validating every plane against it. |
| 251 | 196 | fn fill_i420_16( |
| 252 | 197 | out: &mut I420_16, |
| @@ -328,24 +273,6 @@ fn planes_of<'a>(image: &'a Image<'_>) -> Result<(&'a [u8], usize, &'a [u8], usi | ||
| 328 | 273 | Ok((y_plane, y_pitch, uv_plane, uv_pitch)) |
| 329 | 274 | } |
| 330 | 275 | |
| 331 | -/// Check that `rows` rows of `row_bytes` bytes at `pitch` fit in a plane of | |
| 332 | -/// `len` bytes. A lost or half-initialised GPU context can hand back a short | |
| 333 | -/// or odd mapping; this turns what would be a slice panic into an error. | |
| 334 | -fn check_plane(name: &str, len: usize, pitch: usize, rows: usize, row_bytes: usize) -> Result<()> { | |
| 335 | - if rows == 0 || row_bytes == 0 { | |
| 336 | - return Ok(()); | |
| 337 | - } | |
| 338 | - let need = (rows - 1) | |
| 339 | - .checked_mul(pitch) | |
| 340 | - .and_then(|n| n.checked_add(row_bytes)); | |
| 341 | - match need { | |
| 342 | - Some(need) if pitch >= row_bytes && need <= len => Ok(()), | |
| 343 | - _ => Err(Error::config(format!( | |
| 344 | - "VA image {name} plane does not cover the frame: {rows} rows x {row_bytes} bytes at pitch {pitch}, mapped {len} bytes" | |
| 345 | - ))), | |
| 346 | - } | |
| 347 | -} | |
| 348 | - | |
| 349 | 276 | #[cfg(test)] |
| 350 | 277 | mod tests { |
| 351 | 278 | use super::*; |
crates/ec-hw/src/hw.rsmodified+1 -1
| @@ -561,7 +561,7 @@ impl HwDecoder { | ||
| 561 | 561 | } |
| 562 | 562 | } |
| 563 | 563 | |
| 564 | - /// Feed one access unit. A stream Vulkan refuses (e.g. 10-bit) before its first | |
| 564 | + /// Feed one access unit. A stream Vulkan refuses (an unsupported profile) before its first | |
| 565 | 565 | /// picture comes out moves to VA-API on Linux, replaying what was fed so far. |
| 566 | 566 | pub fn decode(&mut self, data: &[u8], timestamp: i64) -> Result<()> { |
| 567 | 567 | let result = match &mut self.inner { |
crates/ec-hw/src/picture.rsmodified+108 -0
| @@ -3,6 +3,8 @@ | ||
| 3 | 3 | |
| 4 | 4 | use ec_core::color::{ColorDescription, ContentLight}; |
| 5 | 5 | |
| 6 | +use crate::error::{Error, Result}; | |
| 7 | + | |
| 6 | 8 | /// A stream's colour metadata, resolved from its own headers: VUI tags for the |
| 7 | 9 | /// primaries/transfer/matrix triplet, and — for HEVC — the two HDR prefix SEI |
| 8 | 10 | /// messages for the peak the mastering grade and the content itself declared. |
| @@ -90,6 +92,112 @@ pub fn nv12_to_i420( | ||
| 90 | 92 | out |
| 91 | 93 | } |
| 92 | 94 | |
| 95 | +pub(crate) fn semi_planar_to_i420( | |
| 96 | + bit_depth: u8, | |
| 97 | + y: &[u8], | |
| 98 | + uv: &[u8], | |
| 99 | + pitch: usize, | |
| 100 | + origin: (usize, usize), | |
| 101 | + size: (u32, u32), | |
| 102 | +) -> Result<I420> { | |
| 103 | + let bytes = if bit_depth == 8 { 1 } else { 2 }; | |
| 104 | + let pitch = pitch * bytes; | |
| 105 | + let (x, top) = (origin.0 * bytes, origin.1); | |
| 106 | + let y = y.get(top * pitch + x..).unwrap_or(&[]); | |
| 107 | + let uv = uv.get((top / 2) * pitch + x..).unwrap_or(&[]); | |
| 108 | + let (width, height) = size; | |
| 109 | + let (cw, ch) = ((width as usize).div_ceil(2), (height as usize).div_ceil(2)); | |
| 110 | + let mut out = I420 { | |
| 111 | + y: vec![0; width as usize * height as usize], | |
| 112 | + u: vec![0; cw * ch], | |
| 113 | + v: vec![0; cw * ch], | |
| 114 | + width, | |
| 115 | + height, | |
| 116 | + }; | |
| 117 | + fill_i420(&mut out, bit_depth, y, pitch, uv, pitch)?; | |
| 118 | + Ok(out) | |
| 119 | +} | |
| 120 | + | |
| 121 | +/// Copy NV12/P010 planes into `out`, after validating every plane against it. | |
| 122 | +pub(crate) fn fill_i420( | |
| 123 | + out: &mut I420, | |
| 124 | + bit_depth: u8, | |
| 125 | + y_plane: &[u8], | |
| 126 | + y_pitch: usize, | |
| 127 | + uv_plane: &[u8], | |
| 128 | + uv_pitch: usize, | |
| 129 | +) -> Result<()> { | |
| 130 | + let (w, h) = (out.width as usize, out.height as usize); | |
| 131 | + let (cw, ch) = (w.div_ceil(2), h.div_ceil(2)); | |
| 132 | + let bytes = if bit_depth == 8 { 1 } else { 2 }; | |
| 133 | + check_plane("luma", y_plane.len(), y_pitch, h, w * bytes)?; | |
| 134 | + check_plane("chroma", uv_plane.len(), uv_pitch, ch, cw * 2 * bytes)?; | |
| 135 | + let mut row_buf = vec![0u8; uv_pitch.max(y_pitch).max(w * 2)]; | |
| 136 | + match bit_depth { | |
| 137 | + 8 => { | |
| 138 | + for row in 0..h { | |
| 139 | + let src = row * y_pitch; | |
| 140 | + out.y[row * w..(row + 1) * w].copy_from_slice(&y_plane[src..src + w]); | |
| 141 | + } | |
| 142 | + for row in 0..ch { | |
| 143 | + let src = row * uv_pitch; | |
| 144 | + let line = &mut row_buf[..cw * 2]; | |
| 145 | + line.copy_from_slice(&uv_plane[src..src + cw * 2]); | |
| 146 | + for col in 0..cw { | |
| 147 | + out.u[row * cw + col] = line[col * 2]; | |
| 148 | + out.v[row * cw + col] = line[col * 2 + 1]; | |
| 149 | + } | |
| 150 | + } | |
| 151 | + } | |
| 152 | + _ => { | |
| 153 | + // P010 keeps the sample in the *high* bits of each u16, so the | |
| 154 | + // 8-bit value is the high byte — a shift by 8, not by 2. | |
| 155 | + for row in 0..h { | |
| 156 | + let src = row * y_pitch; | |
| 157 | + let line = &mut row_buf[..w * 2]; | |
| 158 | + line.copy_from_slice(&y_plane[src..src + w * 2]); | |
| 159 | + for col in 0..w { | |
| 160 | + out.y[row * w + col] = line[col * 2 + 1]; | |
| 161 | + } | |
| 162 | + } | |
| 163 | + for row in 0..ch { | |
| 164 | + let src = row * uv_pitch; | |
| 165 | + let line = &mut row_buf[..cw * 4]; | |
| 166 | + line.copy_from_slice(&uv_plane[src..src + cw * 4]); | |
| 167 | + for col in 0..cw { | |
| 168 | + out.u[row * cw + col] = line[col * 4 + 1]; | |
| 169 | + out.v[row * cw + col] = line[col * 4 + 3]; | |
| 170 | + } | |
| 171 | + } | |
| 172 | + } | |
| 173 | + } | |
| 174 | + Ok(()) | |
| 175 | +} | |
| 176 | + | |
| 177 | +/// Check that `rows` rows of `row_bytes` bytes at `pitch` fit in a plane of | |
| 178 | +/// `len` bytes. A lost or half-initialised GPU context can hand back a short | |
| 179 | +/// or odd mapping; this turns what would be a slice panic into an error. | |
| 180 | +pub(crate) fn check_plane( | |
| 181 | + name: &str, | |
| 182 | + len: usize, | |
| 183 | + pitch: usize, | |
| 184 | + rows: usize, | |
| 185 | + row_bytes: usize, | |
| 186 | +) -> Result<()> { | |
| 187 | + if rows == 0 || row_bytes == 0 { | |
| 188 | + return Ok(()); | |
| 189 | + } | |
| 190 | + let need = (rows - 1) | |
| 191 | + .checked_mul(pitch) | |
| 192 | + .and_then(|n| n.checked_add(row_bytes)); | |
| 193 | + match need { | |
| 194 | + Some(need) if pitch >= row_bytes && need <= len => Ok(()), | |
| 195 | + _ => Err(Error::config(format!( | |
| 196 | + "VA image {name} plane does not cover the frame: {rows} rows x {row_bytes} bytes at pitch {pitch}, mapped {len} bytes" | |
| 197 | + ))), | |
| 198 | + } | |
| 199 | +} | |
| 200 | + | |
| 93 | 201 | /// Interleave I420 chroma into an NV12 buffer, for the encoder input path. |
| 94 | 202 | pub fn i420_to_nv12( |
| 95 | 203 | src: &I420, |
crates/ec-hw/src/vkdec265.rsmodified+33 -20
| @@ -16,12 +16,13 @@ use crate::hevc_dpb::{ | ||
| 16 | 16 | self, Dpb, Mark, is_bla_picture, is_leading_picture, is_rasl_picture, is_reference_picture, |
| 17 | 17 | peek_pps_id, |
| 18 | 18 | }; |
| 19 | -use crate::picture::{I420, nv12_to_i420}; | |
| 19 | +use crate::picture::{I420, semi_planar_to_i420}; | |
| 20 | 20 | |
| 21 | 21 | const MAX_SLOTS: u32 = 17; |
| 22 | 22 | |
| 23 | 23 | struct Session { |
| 24 | 24 | h265: H265DecodeSession, |
| 25 | + bit_depth: u8, | |
| 25 | 26 | max_width: u32, |
| 26 | 27 | max_height: u32, |
| 27 | 28 | slots: u32, |
| @@ -205,23 +206,33 @@ impl VkH265Decoder { | ||
| 205 | 206 | |
| 206 | 207 | fn ensure_session(&mut self, sps: &Sps) -> Result<()> { |
| 207 | 208 | if sps.chroma_format_idc != 1 |
| 208 | - || sps.bit_depth_luma_minus8 != 0 | |
| 209 | - || sps.bit_depth_chroma_minus8 != 0 | |
| 209 | + || sps.bit_depth_luma_minus8 != sps.bit_depth_chroma_minus8 | |
| 210 | + || !matches!(sps.bit_depth_luma_minus8, 0 | 2) | |
| 210 | 211 | { |
| 211 | 212 | return Err(Error::unsupported( |
| 212 | 213 | "Vulkan H.265 decode", |
| 213 | - "only 8-bit 4:2:0", | |
| 214 | + "only 8- and 10-bit 4:2:0", | |
| 214 | 215 | )); |
| 215 | 216 | } |
| 217 | + let bit_depth = (8 + sps.bit_depth_luma_minus8) as u8; | |
| 216 | 218 | let slots = (sps.max_dec_pic_buffering_minus1 + 2).min(MAX_SLOTS); |
| 217 | 219 | let (w, h) = ( |
| 218 | 220 | sps.pic_width_in_luma_samples, |
| 219 | 221 | sps.pic_height_in_luma_samples, |
| 220 | 222 | ); |
| 221 | 223 | let (max_width, max_height, slots) = match &self.session { |
| 222 | - Some(s) if w <= s.max_width && h <= s.max_height && slots <= s.slots => return Ok(()), | |
| 223 | - Some(s) => (w.max(s.max_width), h.max(s.max_height), slots.max(s.slots)), | |
| 224 | - None => (w, h, slots), | |
| 224 | + Some(s) | |
| 225 | + if s.bit_depth == bit_depth | |
| 226 | + && w <= s.max_width | |
| 227 | + && h <= s.max_height | |
| 228 | + && slots <= s.slots => | |
| 229 | + { | |
| 230 | + return Ok(()); | |
| 231 | + } | |
| 232 | + Some(s) if s.bit_depth == bit_depth => { | |
| 233 | + (w.max(s.max_width), h.max(s.max_height), slots.max(s.slots)) | |
| 234 | + } | |
| 235 | + _ => (w, h, slots), | |
| 225 | 236 | }; |
| 226 | 237 | self.current = None; |
| 227 | 238 | self.dpb.pictures.clear(); |
| @@ -233,10 +244,12 @@ impl VkH265Decoder { | ||
| 233 | 244 | max_width, |
| 234 | 245 | max_height, |
| 235 | 246 | max_dpb_slots: slots, |
| 247 | + bit_depth, | |
| 236 | 248 | }, |
| 237 | 249 | )?; |
| 238 | 250 | self.session = Some(Session { |
| 239 | 251 | h265, |
| 252 | + bit_depth, | |
| 240 | 253 | max_width, |
| 241 | 254 | max_height, |
| 242 | 255 | slots, |
| @@ -328,7 +341,8 @@ impl VkH265Decoder { | ||
| 328 | 341 | sps.pic_height_in_luma_samples, |
| 329 | 342 | )?; |
| 330 | 343 | if current.output { |
| 331 | - self.outputs.insert(current.slot, crop(&sps, &y, &uv)); | |
| 344 | + let frame = crop(&sps, &y, &uv)?; | |
| 345 | + self.outputs.insert(current.slot, frame); | |
| 332 | 346 | } |
| 333 | 347 | |
| 334 | 348 | self.dpb.pictures.push(hevc_dpb::StoredPicture { |
| @@ -369,18 +383,17 @@ fn store<T: PartialEq>(slot: &mut Option<T>, value: T) -> bool { | ||
| 369 | 383 | true |
| 370 | 384 | } |
| 371 | 385 | |
| 372 | -fn crop(sps: &Sps, y: &[u8], uv: &[u8]) -> I420 { | |
| 373 | - let (width, height) = sps.display_size(); | |
| 374 | - let pitch = sps.pic_width_in_luma_samples as usize; | |
| 375 | - let left = 2 * sps.conf_win.left as usize; | |
| 376 | - let top = 2 * sps.conf_win.top as usize; | |
| 377 | - nv12_to_i420( | |
| 378 | - &y[top * pitch + left..], | |
| 379 | - pitch, | |
| 380 | - &uv[(top / 2) * pitch + left..], | |
| 381 | - pitch, | |
| 382 | - width as usize, | |
| 383 | - height as usize, | |
| 386 | +fn crop(sps: &Sps, y: &[u8], uv: &[u8]) -> Result<I420> { | |
| 387 | + semi_planar_to_i420( | |
| 388 | + (8 + sps.bit_depth_luma_minus8) as u8, | |
| 389 | + y, | |
| 390 | + uv, | |
| 391 | + sps.pic_width_in_luma_samples as usize, | |
| 392 | + ( | |
| 393 | + 2 * sps.conf_win.left as usize, | |
| 394 | + 2 * sps.conf_win.top as usize, | |
| 395 | + ), | |
| 396 | + sps.display_size(), | |
| 384 | 397 | ) |
| 385 | 398 | } |
| 386 | 399 |
crates/ec-hw/src/vkdec_av1.rsmodified+22 -9
| @@ -17,12 +17,13 @@ use ec_vk::{ | ||
| 17 | 17 | |
| 18 | 18 | use crate::av1_refs::RefSlots; |
| 19 | 19 | use crate::error::{Error, Result}; |
| 20 | -use crate::picture::{I420, nv12_to_i420}; | |
| 20 | +use crate::picture::{I420, semi_planar_to_i420}; | |
| 21 | 21 | |
| 22 | 22 | const DPB_SLOTS: u32 = NUM_REF_FRAMES as u32 + 1; |
| 23 | 23 | |
| 24 | 24 | struct Session { |
| 25 | 25 | av1: Av1DecodeSession, |
| 26 | + bit_depth: u8, | |
| 26 | 27 | width: u32, |
| 27 | 28 | height: u32, |
| 28 | 29 | sequence: Option<SequenceHeader>, |
| @@ -161,8 +162,14 @@ impl VkAv1Decoder { | ||
| 161 | 162 | return Ok(()); |
| 162 | 163 | } |
| 163 | 164 | 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); | |
| 165 | + let decoded = semi_planar_to_i420( | |
| 166 | + session.bit_depth, | |
| 167 | + &y, | |
| 168 | + &uv, | |
| 169 | + width as usize, | |
| 170 | + (0, 0), | |
| 171 | + (width, height), | |
| 172 | + )?; | |
| 166 | 173 | let state = Av1RefSlot { |
| 167 | 174 | slot: setup_slot, |
| 168 | 175 | frame_type: av1_frame_type(header.frame_type), |
| @@ -187,12 +194,16 @@ impl VkAv1Decoder { | ||
| 187 | 194 | } |
| 188 | 195 | |
| 189 | 196 | fn ensure_session(&mut self, seq: &SequenceHeader, width: u32, height: u32) -> Result<()> { |
| 197 | + let bit_depth = seq.color_config.bit_depth; | |
| 190 | 198 | let mut session = match self.session.take() { |
| 191 | - Some(s) if width <= s.width && height <= s.height => s, | |
| 199 | + Some(s) if s.bit_depth == bit_depth && width <= s.width && height <= s.height => s, | |
| 192 | 200 | old => { |
| 193 | - let (width, height) = old.map_or((width, height), |s| { | |
| 194 | - (width.max(s.width), height.max(s.height)) | |
| 195 | - }); | |
| 201 | + let (width, height) = match &old { | |
| 202 | + Some(s) if s.bit_depth == bit_depth => { | |
| 203 | + (width.max(s.width), height.max(s.height)) | |
| 204 | + } | |
| 205 | + _ => (width, height), | |
| 206 | + }; | |
| 196 | 207 | self.refs = RefSlots::new(); |
| 197 | 208 | let av1 = Av1DecodeSession::new( |
| 198 | 209 | &self.gpu, |
| @@ -200,10 +211,12 @@ impl VkAv1Decoder { | ||
| 200 | 211 | max_width: width, |
| 201 | 212 | max_height: height, |
| 202 | 213 | max_dpb_slots: DPB_SLOTS, |
| 214 | + bit_depth, | |
| 203 | 215 | }, |
| 204 | 216 | )?; |
| 205 | 217 | Session { |
| 206 | 218 | av1, |
| 219 | + bit_depth, | |
| 207 | 220 | width, |
| 208 | 221 | height, |
| 209 | 222 | sequence: None, |
| @@ -228,14 +241,14 @@ impl VkAv1Decoder { | ||
| 228 | 241 | fn check_supported(seq: &SequenceHeader, header: &FrameHeader) -> Result<()> { |
| 229 | 242 | let c = &seq.color_config; |
| 230 | 243 | if seq.seq_profile != 0 |
| 231 | - || c.bit_depth != 8 | |
| 244 | + || !matches!(c.bit_depth, 8 | 10) | |
| 232 | 245 | || c.mono_chrome |
| 233 | 246 | || c.subsampling_x != 1 |
| 234 | 247 | || c.subsampling_y != 1 |
| 235 | 248 | { |
| 236 | 249 | return Err(Error::unsupported( |
| 237 | 250 | "Vulkan AV1 decode", |
| 238 | - "only Main 8-bit 4:2:0", | |
| 251 | + "only Main 8- and 10-bit 4:2:0", | |
| 239 | 252 | )); |
| 240 | 253 | } |
| 241 | 254 | if header.film_grain.apply_grain { |
shims/vk-video-ffi/examples/av1_decode_smoke.rsmodified+1 -0
| @@ -285,6 +285,7 @@ fn main() { | ||
| 285 | 285 | max_width: w, |
| 286 | 286 | max_height: h, |
| 287 | 287 | max_dpb_slots: 9, |
| 288 | + bit_depth: 8, | |
| 288 | 289 | }; |
| 289 | 290 | let mut session = Av1DecodeSession::new(&gpu, &cfg).expect("decode session"); |
| 290 | 291 | session |
shims/vk-video-ffi/examples/h265_decode_smoke.rsmodified+1 -0
| @@ -228,6 +228,7 @@ fn main() { | ||
| 228 | 228 | max_width: w, |
| 229 | 229 | max_height: h, |
| 230 | 230 | max_dpb_slots: 2, |
| 231 | + bit_depth: 8, | |
| 231 | 232 | }; |
| 232 | 233 | let mut session = H265DecodeSession::new(&gpu, &cfg).expect("decode session"); |
| 233 | 234 | session |
shims/vk-video-ffi/src/av1_decode.rsmodified+23 -12
| @@ -4,7 +4,8 @@ use vulkanalia::prelude::v1_3::*; | ||
| 4 | 4 | use vulkanalia::vk::KhrVideoQueueExtensionDeviceCommands; |
| 5 | 5 | |
| 6 | 6 | use crate::session::{ |
| 7 | - Decoder, VideoSession, align, align_u64, picture_resource, video_caps, vk, zeroed, | |
| 7 | + Decoder, VideoSession, align, align_u64, component_bit_depth, picture_resource, video_caps, vk, | |
| 8 | + zeroed, | |
| 8 | 9 | }; |
| 9 | 10 | use crate::{Error, Gpu}; |
| 10 | 11 | |
| @@ -17,6 +18,8 @@ pub struct Av1DecodeConfig { | ||
| 17 | 18 | pub max_height: u32, |
| 18 | 19 | /// DPB slots, including the slot the picture is decoded into. |
| 19 | 20 | pub max_dpb_slots: u32, |
| 21 | + /// Luma and chroma bit depth: 8 or 10. | |
| 22 | + pub bit_depth: u8, | |
| 20 | 23 | } |
| 21 | 24 | |
| 22 | 25 | /// Frame type, values as in the spec and `StdVideoAV1FrameType`. |
| @@ -471,13 +474,14 @@ struct Profile { | ||
| 471 | 474 | } |
| 472 | 475 | |
| 473 | 476 | impl Profile { |
| 474 | - fn new() -> Box<Self> { | |
| 477 | + fn new(bit_depth: u8) -> Box<Self> { | |
| 478 | + let component = component_bit_depth(bit_depth); | |
| 475 | 479 | let mut p = Box::new(Profile { |
| 476 | 480 | info: vk::VideoProfileInfoKHR::builder() |
| 477 | 481 | .video_codec_operation(vk::VideoCodecOperationFlagsKHR::DECODE_AV1) |
| 478 | 482 | .chroma_subsampling(vk::VideoChromaSubsamplingFlagsKHR::_420) |
| 479 | - .luma_bit_depth(vk::VideoComponentBitDepthFlagsKHR::_8) | |
| 480 | - .chroma_bit_depth(vk::VideoComponentBitDepthFlagsKHR::_8) | |
| 483 | + .luma_bit_depth(component) | |
| 484 | + .chroma_bit_depth(component) | |
| 481 | 485 | .build(), |
| 482 | 486 | av1: vk::VideoDecodeAV1ProfileInfoKHR::builder() |
| 483 | 487 | .std_profile(vk::video::STD_VIDEO_AV1_PROFILE_MAIN) |
| @@ -489,10 +493,11 @@ impl Profile { | ||
| 489 | 493 | } |
| 490 | 494 | } |
| 491 | 495 | |
| 492 | -/// An AV1 Main 8-bit 4:2:0 Vulkan Video decode session: NV12 DPB images per slot and a separate NV12 output image. | |
| 496 | +/// An AV1 Main 8- or 10-bit 4:2:0 Vulkan Video decode session: DPB images per slot and a separate output image. | |
| 493 | 497 | pub struct Av1DecodeSession { |
| 494 | 498 | dec: Decoder, |
| 495 | 499 | sequence: Option<Av1SequenceHeader>, |
| 500 | + bit_depth: u8, | |
| 496 | 501 | } |
| 497 | 502 | |
| 498 | 503 | impl Av1DecodeSession { |
| @@ -503,6 +508,9 @@ impl Av1DecodeSession { | ||
| 503 | 508 | "decode size or DPB slot count is zero".into(), |
| 504 | 509 | )); |
| 505 | 510 | } |
| 511 | + if !matches!(cfg.bit_depth, 8 | 10) { | |
| 512 | + return Err(Error::Invalid("bit depth is not 8 or 10".into())); | |
| 513 | + } | |
| 506 | 514 | let family = gpu |
| 507 | 515 | .decode_family() |
| 508 | 516 | .ok_or_else(|| Error::Unsupported("AV1 decode queue family".into()))?; |
| @@ -518,7 +526,7 @@ impl Av1DecodeSession { | ||
| 518 | 526 | } |
| 519 | 527 | let width = align(cfg.max_width, 16); |
| 520 | 528 | let height = align(cfg.max_height, 16); |
| 521 | - let profile = Profile::new(); | |
| 529 | + let profile = Profile::new(cfg.bit_depth); | |
| 522 | 530 | let caps = query_caps(gpu, &profile.info)?; |
| 523 | 531 | if width > caps.max_extent.width |
| 524 | 532 | || height > caps.max_extent.height |
| @@ -555,6 +563,7 @@ impl Av1DecodeSession { | ||
| 555 | 563 | Ok(Av1DecodeSession { |
| 556 | 564 | dec, |
| 557 | 565 | sequence: None, |
| 566 | + bit_depth: cfg.bit_depth, | |
| 558 | 567 | }) |
| 559 | 568 | } |
| 560 | 569 | |
| @@ -562,14 +571,15 @@ impl Av1DecodeSession { | ||
| 562 | 571 | pub fn set_sequence_header(&mut self, seq: &Av1SequenceHeader) -> Result<(), Error> { |
| 563 | 572 | let c = &seq.color_config; |
| 564 | 573 | if seq.seq_profile != 0 |
| 565 | - || c.bit_depth != 8 | |
| 574 | + || c.bit_depth != self.bit_depth | |
| 566 | 575 | || c.mono_chrome |
| 567 | 576 | || c.subsampling_x != 1 |
| 568 | 577 | || c.subsampling_y != 1 |
| 569 | 578 | { |
| 570 | - return Err(Error::Unsupported( | |
| 571 | - "AV1 sequence other than Main 8-bit 4:2:0".into(), | |
| 572 | - )); | |
| 579 | + return Err(Error::Unsupported(format!( | |
| 580 | + "AV1 sequence other than Main {}-bit 4:2:0", | |
| 581 | + self.bit_depth | |
| 582 | + ))); | |
| 573 | 583 | } |
| 574 | 584 | let gpu = self.dec.vs.gpu.clone(); |
| 575 | 585 | let d = gpu.device(); |
| @@ -842,7 +852,7 @@ impl Av1DecodeSession { | ||
| 842 | 852 | self.dec.run(&begin, &decode) |
| 843 | 853 | } |
| 844 | 854 | |
| 845 | - /// Copies the top-left width x height of the last decoded picture out as NV12 planes (Y, interleaved UV). | |
| 855 | + /// Copies the top-left width x height of the last decoded picture out as NV12 or P010 planes (Y, interleaved UV). | |
| 846 | 856 | pub fn read_output(&mut self, width: u32, height: u32) -> Result<(Vec<u8>, Vec<u8>), Error> { |
| 847 | 857 | self.dec.read_nv12(width, height) |
| 848 | 858 | } |
| @@ -854,7 +864,8 @@ fn query_caps(gpu: &Gpu, profile: &vk::VideoProfileInfoKHR) -> Result<Caps, Erro | ||
| 854 | 864 | let mut caps = vk::VideoCapabilitiesKHR::builder() |
| 855 | 865 | .push_next(&mut av1) |
| 856 | 866 | .push_next(&mut decode); |
| 857 | - video_caps(gpu, profile, &mut caps)?; | |
| 867 | + video_caps(gpu, profile, &mut caps) | |
| 868 | + .map_err(|_| Error::Unsupported("AV1 decode profile".into()))?; | |
| 858 | 869 | let caps = caps.build(); |
| 859 | 870 | let decode = decode.build(); |
| 860 | 871 | Ok(Caps { |
shims/vk-video-ffi/src/h265_decode.rsmodified+30 -11
| @@ -4,7 +4,8 @@ use vulkanalia::prelude::v1_3::*; | ||
| 4 | 4 | use vulkanalia::vk::KhrVideoQueueExtensionDeviceCommands; |
| 5 | 5 | |
| 6 | 6 | use crate::session::{ |
| 7 | - Decoder, VideoSession, align, align_u64, picture_resource, video_caps, vk, zeroed, | |
| 7 | + Decoder, VideoSession, align, align_u64, component_bit_depth, picture_resource, video_caps, vk, | |
| 8 | + zeroed, | |
| 8 | 9 | }; |
| 9 | 10 | use crate::{Error, Gpu}; |
| 10 | 11 | |
| @@ -17,6 +18,8 @@ pub struct H265DecodeConfig { | ||
| 17 | 18 | pub max_height: u32, |
| 18 | 19 | /// DPB slots, including the slot the picture is decoded into. |
| 19 | 20 | pub max_dpb_slots: u32, |
| 21 | + /// Luma and chroma bit depth: 8 or 10. | |
| 22 | + pub bit_depth: u8, | |
| 20 | 23 | } |
| 21 | 24 | |
| 22 | 25 | /// Profile, tier and level of a parameter set. |
| @@ -321,16 +324,22 @@ struct Profile { | ||
| 321 | 324 | } |
| 322 | 325 | |
| 323 | 326 | impl Profile { |
| 324 | - fn new() -> Box<Self> { | |
| 327 | + fn new(bit_depth: u8) -> Box<Self> { | |
| 328 | + let component = component_bit_depth(bit_depth); | |
| 329 | + let profile_idc = if bit_depth == 10 { | |
| 330 | + vk::video::STD_VIDEO_H265_PROFILE_IDC_MAIN_10 | |
| 331 | + } else { | |
| 332 | + vk::video::STD_VIDEO_H265_PROFILE_IDC_MAIN | |
| 333 | + }; | |
| 325 | 334 | let mut p = Box::new(Profile { |
| 326 | 335 | info: vk::VideoProfileInfoKHR::builder() |
| 327 | 336 | .video_codec_operation(vk::VideoCodecOperationFlagsKHR::DECODE_H265) |
| 328 | 337 | .chroma_subsampling(vk::VideoChromaSubsamplingFlagsKHR::_420) |
| 329 | - .luma_bit_depth(vk::VideoComponentBitDepthFlagsKHR::_8) | |
| 330 | - .chroma_bit_depth(vk::VideoComponentBitDepthFlagsKHR::_8) | |
| 338 | + .luma_bit_depth(component) | |
| 339 | + .chroma_bit_depth(component) | |
| 331 | 340 | .build(), |
| 332 | 341 | h265: vk::VideoDecodeH265ProfileInfoKHR::builder() |
| 333 | - .std_profile_idc(vk::video::STD_VIDEO_H265_PROFILE_IDC_MAIN) | |
| 342 | + .std_profile_idc(profile_idc) | |
| 334 | 343 | .build(), |
| 335 | 344 | }); |
| 336 | 345 | p.info.next = &p.h265 as *const _ as *const c_void; |
| @@ -342,6 +351,7 @@ impl Profile { | ||
| 342 | 351 | pub struct H265DecodeSession { |
| 343 | 352 | dec: Decoder, |
| 344 | 353 | sps: Vec<H265Sps>, |
| 354 | + bit_depth: u8, | |
| 345 | 355 | } |
| 346 | 356 | |
| 347 | 357 | impl H265DecodeSession { |
| @@ -352,6 +362,9 @@ impl H265DecodeSession { | ||
| 352 | 362 | "decode size or DPB slot count is zero".into(), |
| 353 | 363 | )); |
| 354 | 364 | } |
| 365 | + if !matches!(cfg.bit_depth, 8 | 10) { | |
| 366 | + return Err(Error::Invalid("bit depth is not 8 or 10".into())); | |
| 367 | + } | |
| 355 | 368 | let family = gpu |
| 356 | 369 | .decode_family() |
| 357 | 370 | .ok_or_else(|| Error::Unsupported("H.265 decode queue family".into()))?; |
| @@ -367,7 +380,7 @@ impl H265DecodeSession { | ||
| 367 | 380 | } |
| 368 | 381 | let width = align(cfg.max_width, 16); |
| 369 | 382 | let height = align(cfg.max_height, 16); |
| 370 | - let profile = Profile::new(); | |
| 383 | + let profile = Profile::new(cfg.bit_depth); | |
| 371 | 384 | let caps = query_caps(gpu, &profile.info)?; |
| 372 | 385 | if width > caps.max_extent.width |
| 373 | 386 | || height > caps.max_extent.height |
| @@ -404,6 +417,7 @@ impl H265DecodeSession { | ||
| 404 | 417 | Ok(H265DecodeSession { |
| 405 | 418 | dec, |
| 406 | 419 | sps: Vec::new(), |
| 420 | + bit_depth: cfg.bit_depth, | |
| 407 | 421 | }) |
| 408 | 422 | } |
| 409 | 423 | |
| @@ -414,12 +428,16 @@ impl H265DecodeSession { | ||
| 414 | 428 | sps: &[H265Sps], |
| 415 | 429 | pps: &[H265Pps], |
| 416 | 430 | ) -> Result<(), Error> { |
| 431 | + let minus8 = self.bit_depth - 8; | |
| 417 | 432 | if sps.iter().any(|s| { |
| 418 | 433 | s.chroma_format_idc != 1 |
| 419 | - || s.bit_depth_luma_minus8 != 0 | |
| 420 | - || s.bit_depth_chroma_minus8 != 0 | |
| 434 | + || s.bit_depth_luma_minus8 != minus8 | |
| 435 | + || s.bit_depth_chroma_minus8 != minus8 | |
| 421 | 436 | }) { |
| 422 | - return Err(Error::Unsupported("H.265 decode beyond 8-bit 4:2:0".into())); | |
| 437 | + return Err(Error::Unsupported(format!( | |
| 438 | + "H.265 decode other than {}-bit 4:2:0", | |
| 439 | + self.bit_depth | |
| 440 | + ))); | |
| 423 | 441 | } |
| 424 | 442 | let gpu = self.dec.vs.gpu.clone(); |
| 425 | 443 | let d = gpu.device(); |
| @@ -675,7 +693,7 @@ impl H265DecodeSession { | ||
| 675 | 693 | self.dec.run(&begin, &decode) |
| 676 | 694 | } |
| 677 | 695 | |
| 678 | - /// Copies the top-left width x height of the last decoded picture out as NV12 planes (Y, interleaved UV). | |
| 696 | + /// Copies the top-left width x height of the last decoded picture out as NV12 or P010 planes (Y, interleaved UV). | |
| 679 | 697 | pub fn read_output(&mut self, width: u32, height: u32) -> Result<(Vec<u8>, Vec<u8>), Error> { |
| 680 | 698 | self.dec.read_nv12(width, height) |
| 681 | 699 | } |
| @@ -687,7 +705,8 @@ fn query_caps(gpu: &Gpu, profile: &vk::VideoProfileInfoKHR) -> Result<Caps, Erro | ||
| 687 | 705 | let mut caps = vk::VideoCapabilitiesKHR::builder() |
| 688 | 706 | .push_next(&mut h265) |
| 689 | 707 | .push_next(&mut decode); |
| 690 | - video_caps(gpu, profile, &mut caps)?; | |
| 708 | + video_caps(gpu, profile, &mut caps) | |
| 709 | + .map_err(|_| Error::Unsupported("H.265 decode profile".into()))?; | |
| 691 | 710 | let caps = caps.build(); |
| 692 | 711 | let decode = decode.build(); |
| 693 | 712 | Ok(Caps { |
shims/vk-video-ffi/src/session.rsmodified+30 -8
| @@ -8,6 +8,7 @@ use crate::rgb_convert::{Input, RgbConverter}; | ||
| 8 | 8 | use crate::{Error, Gpu, RateControl}; |
| 9 | 9 | |
| 10 | 10 | pub(crate) const NV12: vk::Format = vk::Format::G8_B8R8_2PLANE_420_UNORM; |
| 11 | +pub(crate) const P010: vk::Format = vk::Format::G10X6_B10X6R10X6_2PLANE_420_UNORM_3PACK16; | |
| 11 | 12 | pub(crate) const DPB_SLOTS: usize = 2; |
| 12 | 13 | pub(crate) const PLANE_0: vk::ImageAspectFlags = vk::ImageAspectFlags::PLANE_0; |
| 13 | 14 | pub(crate) const PLANE_1: vk::ImageAspectFlags = vk::ImageAspectFlags::PLANE_1; |
| @@ -22,6 +23,18 @@ pub(crate) fn zeroed<T>() -> T { | ||
| 22 | 23 | unsafe { std::mem::zeroed() } |
| 23 | 24 | } |
| 24 | 25 | |
| 26 | +pub(crate) fn bytes_per_sample(format: vk::Format) -> usize { | |
| 27 | + if format == P010 { 2 } else { 1 } | |
| 28 | +} | |
| 29 | + | |
| 30 | +pub(crate) fn component_bit_depth(bit_depth: u8) -> vk::VideoComponentBitDepthFlagsKHR { | |
| 31 | + if bit_depth == 10 { | |
| 32 | + vk::VideoComponentBitDepthFlagsKHR::_10 | |
| 33 | + } else { | |
| 34 | + vk::VideoComponentBitDepthFlagsKHR::_8 | |
| 35 | + } | |
| 36 | +} | |
| 37 | + | |
| 25 | 38 | pub(crate) fn align(value: u32, to: u32) -> u32 { |
| 26 | 39 | value.div_ceil(to) * to |
| 27 | 40 | } |
| @@ -224,6 +237,7 @@ impl Buffer { | ||
| 224 | 237 | pub(crate) struct VideoSession { |
| 225 | 238 | pub(crate) gpu: Gpu, |
| 226 | 239 | pub(crate) family: u32, |
| 240 | + pub(crate) format: vk::Format, | |
| 227 | 241 | pub(crate) session: vk::VideoSessionKHR, |
| 228 | 242 | pub(crate) params: vk::VideoSessionParametersKHR, |
| 229 | 243 | memories: Vec<vk::DeviceMemory>, |
| @@ -239,9 +253,15 @@ impl VideoSession { | ||
| 239 | 253 | max_dpb_slots: u32, |
| 240 | 254 | max_active_refs: u32, |
| 241 | 255 | ) -> Result<Self, Error> { |
| 256 | + let format = if profile.luma_bit_depth == vk::VideoComponentBitDepthFlagsKHR::_10 { | |
| 257 | + P010 | |
| 258 | + } else { | |
| 259 | + NV12 | |
| 260 | + }; | |
| 242 | 261 | let mut s = VideoSession { |
| 243 | 262 | gpu: gpu.clone(), |
| 244 | 263 | family, |
| 264 | + format, | |
| 245 | 265 | session: Default::default(), |
| 246 | 266 | params: Default::default(), |
| 247 | 267 | memories: Vec::new(), |
| @@ -250,9 +270,9 @@ impl VideoSession { | ||
| 250 | 270 | let info = vk::VideoSessionCreateInfoKHR::builder() |
| 251 | 271 | .queue_family_index(family) |
| 252 | 272 | .video_profile(profile) |
| 253 | - .picture_format(NV12) | |
| 273 | + .picture_format(format) | |
| 254 | 274 | .max_coded_extent(extent) |
| 255 | - .reference_picture_format(NV12) | |
| 275 | + .reference_picture_format(format) | |
| 256 | 276 | .max_dpb_slots(max_dpb_slots) |
| 257 | 277 | .max_active_reference_pictures(max_active_refs) |
| 258 | 278 | .std_header_version(std_header_version) |
| @@ -331,7 +351,7 @@ impl VideoSession { | ||
| 331 | 351 | ) -> Result<Image, Error> { |
| 332 | 352 | let info = vk::ImageCreateInfo::builder() |
| 333 | 353 | .image_type(vk::ImageType::_2D) |
| 334 | - .format(NV12) | |
| 354 | + .format(self.format) | |
| 335 | 355 | .extent(vk::Extent3D { |
| 336 | 356 | width: extent.width, |
| 337 | 357 | height: extent.height, |
| @@ -362,7 +382,7 @@ impl VideoSession { | ||
| 362 | 382 | let view_info = vk::ImageViewCreateInfo::builder() |
| 363 | 383 | .image(image) |
| 364 | 384 | .view_type(vk::ImageViewType::_2D) |
| 365 | - .format(NV12) | |
| 385 | + .format(self.format) | |
| 366 | 386 | .subresource_range(color_range()) |
| 367 | 387 | .push_next(&mut usage_info) |
| 368 | 388 | .build(); |
| @@ -912,8 +932,9 @@ impl Decoder { | ||
| 912 | 932 | &families, |
| 913 | 933 | &mut list, |
| 914 | 934 | )?; |
| 935 | + let bytes = bytes_per_sample(dec.vs.format) as u64; | |
| 915 | 936 | let bitstream_size = align_u64( |
| 916 | - max_extent.width as u64 * max_extent.height as u64 * 3 + 65536, | |
| 937 | + max_extent.width as u64 * max_extent.height as u64 * 3 * bytes / 2 + 65536, | |
| 917 | 938 | size_align, |
| 918 | 939 | ); |
| 919 | 940 | dec.bitstream = dec.vs.buffer( |
| @@ -923,7 +944,7 @@ impl Decoder { | ||
| 923 | 944 | Some(&mut list), |
| 924 | 945 | )?; |
| 925 | 946 | dec.readback = dec.vs.buffer( |
| 926 | - max_extent.width as u64 * max_extent.height as u64 * 3 / 2, | |
| 947 | + max_extent.width as u64 * max_extent.height as u64 * 3 / 2 * bytes, | |
| 927 | 948 | vk::BufferUsageFlags::TRANSFER_DST, |
| 928 | 949 | vk::MemoryPropertyFlags::HOST_VISIBLE | vk::MemoryPropertyFlags::HOST_COHERENT, |
| 929 | 950 | None, |
| @@ -980,7 +1001,7 @@ impl Decoder { | ||
| 980 | 1001 | Ok(()) |
| 981 | 1002 | } |
| 982 | 1003 | |
| 983 | - /// Copies the top-left width x height of the last decoded picture out as NV12 planes (Y, interleaved UV). | |
| 1004 | + /// Copies the top-left width x height of the last decoded picture out as NV12 or P010 planes (Y, interleaved UV). | |
| 984 | 1005 | pub(crate) fn read_nv12( |
| 985 | 1006 | &mut self, |
| 986 | 1007 | width: u32, |
| @@ -1002,7 +1023,8 @@ impl Decoder { | ||
| 1002 | 1023 | let d = gpu.device(); |
| 1003 | 1024 | let cb = self.graphics.cb; |
| 1004 | 1025 | let image = self.output.image; |
| 1005 | - let luma = width as usize * height as usize; | |
| 1026 | + let bytes = bytes_per_sample(self.vs.format); | |
| 1027 | + let luma = width as usize * height as usize * bytes; | |
| 1006 | 1028 | let copies = [ |
| 1007 | 1029 | copy_region(0, PLANE_0, width, height), |
| 1008 | 1030 | copy_region(luma as u64, PLANE_1, width / 2, height / 2), |