forge
Explore
Log in

Don't you dare go Hollow

Merge branch 'worktree-agent-af775a1ed6a046ae4'

Browse files at this commit

8352fa1d201b85fa8ede43cc6268ec5163ed00b3Ahmet Kaan GümüşParent: fd663bdcad917b
crates/ec-hw/examples/hw_decode.rsmodified+67 -17
@@ -1,4 +1,5 @@
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.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 //! ```text4 //! ```text
4 //! cargo run -p ec-hw --example hw_decode -- <scratch dir>5 //! cargo run -p ec-hw --example hw_decode -- <scratch dir>
@@ -103,34 +104,83 @@ fn main() {
103 for case in CASES {104 for case in CASES {
104 run(&dir, case);105 run(&dir, case);
105 }106 }
106- ten_bit_falls_back(&dir);107+ ten_bit(&dir);
107 own_av1(&dir);108 own_av1(&dir);
108 }109 }
109 110
110-fn ten_bit_falls_back(dir: &std::path::Path) {111+fn ten_bit(dir: &Path) {
111- let path = dir.join("ten.hevc");112+ let hevc = dir.join("ten.hevc");
112- let p = path.to_str().expect("utf-8 path");113+ let ivf = dir.join("ten_av1.ivf");
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+ 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")]);
114- let data = std::fs::read(&path).expect("read 10-bit stream");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")]);
115- let mut dec = ec_hw::HwDecoder::open(ec_hw::Codec::H265).expect("open");116+ let hevc_data = fs::read(&hevc).expect("read 10-bit H.265");
116- let opened = dec.backend();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 let starts: Vec<usize> = (0..data.len().saturating_sub(3))123 let starts: Vec<usize> = (0..data.len().saturating_sub(3))
118 .filter(|&i| data[i..i + 3] == [0, 0, 1])124 .filter(|&i| data[i..i + 3] == [0, 0, 1])
119 .map(|i| if i > 0 && data[i - 1] == 0 { i - 1 } else { i })125 .map(|i| if i > 0 && data[i - 1] == 0 { i - 1 } else { i })
120 .chain([data.len()])126 .chain([data.len()])
121 .collect();127 .collect();
122- let mut n = 0;128+ starts.windows(2).map(|w| &data[w[0]..w[1]]).collect()
123- for w in starts.windows(2) {129+}
124- dec.decode(&data[w[0]..w[1]], 0).expect("decode 10-bit");130+
125- while dec.next_frame().expect("frame").is_some() {131+fn ten_bit_case(label: &str, codec: Codec, units: &[&[u8]]) {
126- n += 1;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");156+ decoder.flush().expect("VA flush");
130- while dec.next_frame().expect("frame").is_some() {157+ while let Some(frame) = decoder.next_frame() {
131- n += 1;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 fn ffmpeg(args: &[&str]) {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 use ec_va::{Display, Image, PrimeSurface, Surface, SurfaceExt};7 use ec_va::{Display, Image, PrimeSurface, Surface, SurfaceExt};
8 8
9 use crate::error::{Error, Result};9 use crate::error::{Error, Result};
10+use crate::picture::{check_plane, fill_i420};
10 use crate::pool::PooledSurface;11 use crate::pool::PooledSurface;
11 12
12 pub use crate::picture::{Colour, I420, I420_16, i420_to_nv12, nv12_to_i420};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 /// Copy NV12/P010 planes into `out`, after validating every plane against it.195 /// Copy NV12/P010 planes into `out`, after validating every plane against it.
251 fn fill_i420_16(196 fn fill_i420_16(
252 out: &mut I420_16,197 out: &mut I420_16,
@@ -328,24 +273,6 @@ fn planes_of<'a>(image: &'a Image<'_>) -> Result<(&'a [u8], usize, &'a [u8], usi
328 Ok((y_plane, y_pitch, uv_plane, uv_pitch))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 #[cfg(test)]276 #[cfg(test)]
350 mod tests {277 mod tests {
351 use super::*;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 first564+ /// Feed one access unit. A stream Vulkan refuses (an unsupported profile) before its first
565 /// picture comes out moves to VA-API on Linux, replaying what was fed so far.565 /// picture comes out moves to VA-API on Linux, replaying what was fed so far.
566 pub fn decode(&mut self, data: &[u8], timestamp: i64) -> Result<()> {566 pub fn decode(&mut self, data: &[u8], timestamp: i64) -> Result<()> {
567 let result = match &mut self.inner {567 let result = match &mut self.inner {
crates/ec-hw/src/picture.rsmodified+108 -0
@@ -3,6 +3,8 @@
3 3
4 use ec_core::color::{ColorDescription, ContentLight};4 use ec_core::color::{ColorDescription, ContentLight};
5 5
6+use crate::error::{Error, Result};
7+
6 /// A stream's colour metadata, resolved from its own headers: VUI tags for the8 /// A stream's colour metadata, resolved from its own headers: VUI tags for the
7 /// primaries/transfer/matrix triplet, and — for HEVC — the two HDR prefix SEI9 /// primaries/transfer/matrix triplet, and — for HEVC — the two HDR prefix SEI
8 /// messages for the peak the mastering grade and the content itself declared.10 /// messages for the peak the mastering grade and the content itself declared.
@@ -90,6 +92,112 @@ pub fn nv12_to_i420(
90 out92 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 /// Interleave I420 chroma into an NV12 buffer, for the encoder input path.201 /// Interleave I420 chroma into an NV12 buffer, for the encoder input path.
94 pub fn i420_to_nv12(202 pub fn i420_to_nv12(
95 src: &I420,203 src: &I420,
crates/ec-hw/src/vkdec265.rsmodified+33 -20
@@ -16,12 +16,13 @@ use crate::hevc_dpb::{
16 self, Dpb, Mark, is_bla_picture, is_leading_picture, is_rasl_picture, is_reference_picture,16 self, Dpb, Mark, is_bla_picture, is_leading_picture, is_rasl_picture, is_reference_picture,
17 peek_pps_id,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 const MAX_SLOTS: u32 = 17;21 const MAX_SLOTS: u32 = 17;
22 22
23 struct Session {23 struct Session {
24 h265: H265DecodeSession,24 h265: H265DecodeSession,
25+ bit_depth: u8,
25 max_width: u32,26 max_width: u32,
26 max_height: u32,27 max_height: u32,
27 slots: u32,28 slots: u32,
@@ -205,23 +206,33 @@ impl VkH265Decoder {
205 206
206 fn ensure_session(&mut self, sps: &Sps) -> Result<()> {207 fn ensure_session(&mut self, sps: &Sps) -> Result<()> {
207 if sps.chroma_format_idc != 1208 if sps.chroma_format_idc != 1
208- || sps.bit_depth_luma_minus8 != 0209+ || sps.bit_depth_luma_minus8 != sps.bit_depth_chroma_minus8
209- || sps.bit_depth_chroma_minus8 != 0210+ || !matches!(sps.bit_depth_luma_minus8, 0 | 2)
210 {211 {
211 return Err(Error::unsupported(212 return Err(Error::unsupported(
212 "Vulkan H.265 decode",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 let slots = (sps.max_dec_pic_buffering_minus1 + 2).min(MAX_SLOTS);218 let slots = (sps.max_dec_pic_buffering_minus1 + 2).min(MAX_SLOTS);
217 let (w, h) = (219 let (w, h) = (
218 sps.pic_width_in_luma_samples,220 sps.pic_width_in_luma_samples,
219 sps.pic_height_in_luma_samples,221 sps.pic_height_in_luma_samples,
220 );222 );
221 let (max_width, max_height, slots) = match &self.session {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(()),224+ Some(s)
223- Some(s) => (w.max(s.max_width), h.max(s.max_height), slots.max(s.slots)),225+ if s.bit_depth == bit_depth
224- None => (w, h, slots),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 self.current = None;237 self.current = None;
227 self.dpb.pictures.clear();238 self.dpb.pictures.clear();
@@ -233,10 +244,12 @@ impl VkH265Decoder {
233 max_width,244 max_width,
234 max_height,245 max_height,
235 max_dpb_slots: slots,246 max_dpb_slots: slots,
247+ bit_depth,
236 },248 },
237 )?;249 )?;
238 self.session = Some(Session {250 self.session = Some(Session {
239 h265,251 h265,
252+ bit_depth,
240 max_width,253 max_width,
241 max_height,254 max_height,
242 slots,255 slots,
@@ -328,7 +341,8 @@ impl VkH265Decoder {
328 sps.pic_height_in_luma_samples,341 sps.pic_height_in_luma_samples,
329 )?;342 )?;
330 if current.output {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 self.dpb.pictures.push(hevc_dpb::StoredPicture {348 self.dpb.pictures.push(hevc_dpb::StoredPicture {
@@ -369,18 +383,17 @@ fn store<T: PartialEq>(slot: &mut Option<T>, value: T) -> bool {
369 true383 true
370 }384 }
371 385
372-fn crop(sps: &Sps, y: &[u8], uv: &[u8]) -> I420 {386+fn crop(sps: &Sps, y: &[u8], uv: &[u8]) -> Result<I420> {
373- let (width, height) = sps.display_size();387+ semi_planar_to_i420(
374- let pitch = sps.pic_width_in_luma_samples as usize;388+ (8 + sps.bit_depth_luma_minus8) as u8,
375- let left = 2 * sps.conf_win.left as usize;389+ y,
376- let top = 2 * sps.conf_win.top as usize;390+ uv,
377- nv12_to_i420(391+ sps.pic_width_in_luma_samples as usize,
378- &y[top * pitch + left..],392+ (
379- pitch,393+ 2 * sps.conf_win.left as usize,
380- &uv[(top / 2) * pitch + left..],394+ 2 * sps.conf_win.top as usize,
381- pitch,395+ ),
382- width as usize,396+ sps.display_size(),
383- height as usize,
384 )397 )
385 }398 }
386 399
crates/ec-hw/src/vkdec_av1.rsmodified+21 -8
@@ -17,12 +17,13 @@ use ec_vk::{
17 17
18 use crate::av1_refs::RefSlots;18 use crate::av1_refs::RefSlots;
19 use crate::error::{Error, Result};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 const DPB_SLOTS: u32 = NUM_REF_FRAMES as u32 + 1;22 const DPB_SLOTS: u32 = NUM_REF_FRAMES as u32 + 1;
23 23
24 struct Session {24 struct Session {
25 av1: Av1DecodeSession,25 av1: Av1DecodeSession,
26+ bit_depth: u8,
26 width: u32,27 width: u32,
27 height: u32,28 height: u32,
28 sequence: Option<SequenceHeader>,29 sequence: Option<SequenceHeader>,
@@ -161,8 +162,14 @@ impl VkAv1Decoder {
161 return Ok(());162 return Ok(());
162 }163 }
163 let (y, uv) = session.av1.read_output(width, height)?;164 let (y, uv) = session.av1.read_output(width, height)?;
164- let w = width as usize;165+ let decoded = semi_planar_to_i420(
165- let decoded = nv12_to_i420(&y, w, &uv, w, w, height as usize);166+ session.bit_depth,
167+ &y,
168+ &uv,
169+ width as usize,
170+ (0, 0),
171+ (width, height),
172+ )?;
166 let state = Av1RefSlot {173 let state = Av1RefSlot {
167 slot: setup_slot,174 slot: setup_slot,
168 frame_type: av1_frame_type(header.frame_type),175 frame_type: av1_frame_type(header.frame_type),
@@ -187,12 +194,16 @@ impl VkAv1Decoder {
187 }194 }
188 195
189 fn ensure_session(&mut self, seq: &SequenceHeader, width: u32, height: u32) -> Result<()> {196 fn ensure_session(&mut self, seq: &SequenceHeader, width: u32, height: u32) -> Result<()> {
197+ let bit_depth = seq.color_config.bit_depth;
190 let mut session = match self.session.take() {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 old => {200 old => {
193- let (width, height) = old.map_or((width, height), |s| {201+ let (width, height) = match &old {
202+ Some(s) if s.bit_depth == bit_depth => {
194 (width.max(s.width), height.max(s.height))203 (width.max(s.width), height.max(s.height))
195- });204+ }
205+ _ => (width, height),
206+ };
196 self.refs = RefSlots::new();207 self.refs = RefSlots::new();
197 let av1 = Av1DecodeSession::new(208 let av1 = Av1DecodeSession::new(
198 &self.gpu,209 &self.gpu,
@@ -200,10 +211,12 @@ impl VkAv1Decoder {
200 max_width: width,211 max_width: width,
201 max_height: height,212 max_height: height,
202 max_dpb_slots: DPB_SLOTS,213 max_dpb_slots: DPB_SLOTS,
214+ bit_depth,
203 },215 },
204 )?;216 )?;
205 Session {217 Session {
206 av1,218 av1,
219+ bit_depth,
207 width,220 width,
208 height,221 height,
209 sequence: None,222 sequence: None,
@@ -228,14 +241,14 @@ impl VkAv1Decoder {
228 fn check_supported(seq: &SequenceHeader, header: &FrameHeader) -> Result<()> {241 fn check_supported(seq: &SequenceHeader, header: &FrameHeader) -> Result<()> {
229 let c = &seq.color_config;242 let c = &seq.color_config;
230 if seq.seq_profile != 0243 if seq.seq_profile != 0
231- || c.bit_depth != 8244+ || !matches!(c.bit_depth, 8 | 10)
232 || c.mono_chrome245 || c.mono_chrome
233 || c.subsampling_x != 1246 || c.subsampling_x != 1
234 || c.subsampling_y != 1247 || c.subsampling_y != 1
235 {248 {
236 return Err(Error::unsupported(249 return Err(Error::unsupported(
237 "Vulkan AV1 decode",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 if header.film_grain.apply_grain {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 max_width: w,285 max_width: w,
286 max_height: h,286 max_height: h,
287 max_dpb_slots: 9,287 max_dpb_slots: 9,
288+ bit_depth: 8,
288 };289 };
289 let mut session = Av1DecodeSession::new(&gpu, &cfg).expect("decode session");290 let mut session = Av1DecodeSession::new(&gpu, &cfg).expect("decode session");
290 session291 session
shims/vk-video-ffi/examples/h265_decode_smoke.rsmodified+1 -0
@@ -228,6 +228,7 @@ fn main() {
228 max_width: w,228 max_width: w,
229 max_height: h,229 max_height: h,
230 max_dpb_slots: 2,230 max_dpb_slots: 2,
231+ bit_depth: 8,
231 };232 };
232 let mut session = H265DecodeSession::new(&gpu, &cfg).expect("decode session");233 let mut session = H265DecodeSession::new(&gpu, &cfg).expect("decode session");
233 session234 session
shims/vk-video-ffi/src/av1_decode.rsmodified+23 -12
@@ -4,7 +4,8 @@ use vulkanalia::prelude::v1_3::*;
4 use vulkanalia::vk::KhrVideoQueueExtensionDeviceCommands;4 use vulkanalia::vk::KhrVideoQueueExtensionDeviceCommands;
5 5
6 use crate::session::{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 use crate::{Error, Gpu};10 use crate::{Error, Gpu};
10 11
@@ -17,6 +18,8 @@ pub struct Av1DecodeConfig {
17 pub max_height: u32,18 pub max_height: u32,
18 /// DPB slots, including the slot the picture is decoded into.19 /// DPB slots, including the slot the picture is decoded into.
19 pub max_dpb_slots: u32,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 /// Frame type, values as in the spec and `StdVideoAV1FrameType`.25 /// Frame type, values as in the spec and `StdVideoAV1FrameType`.
@@ -471,13 +474,14 @@ struct Profile {
471 }474 }
472 475
473 impl Profile {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 let mut p = Box::new(Profile {479 let mut p = Box::new(Profile {
476 info: vk::VideoProfileInfoKHR::builder()480 info: vk::VideoProfileInfoKHR::builder()
477 .video_codec_operation(vk::VideoCodecOperationFlagsKHR::DECODE_AV1)481 .video_codec_operation(vk::VideoCodecOperationFlagsKHR::DECODE_AV1)
478 .chroma_subsampling(vk::VideoChromaSubsamplingFlagsKHR::_420)482 .chroma_subsampling(vk::VideoChromaSubsamplingFlagsKHR::_420)
479- .luma_bit_depth(vk::VideoComponentBitDepthFlagsKHR::_8)483+ .luma_bit_depth(component)
480- .chroma_bit_depth(vk::VideoComponentBitDepthFlagsKHR::_8)484+ .chroma_bit_depth(component)
481 .build(),485 .build(),
482 av1: vk::VideoDecodeAV1ProfileInfoKHR::builder()486 av1: vk::VideoDecodeAV1ProfileInfoKHR::builder()
483 .std_profile(vk::video::STD_VIDEO_AV1_PROFILE_MAIN)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 pub struct Av1DecodeSession {497 pub struct Av1DecodeSession {
494 dec: Decoder,498 dec: Decoder,
495 sequence: Option<Av1SequenceHeader>,499 sequence: Option<Av1SequenceHeader>,
500+ bit_depth: u8,
496 }501 }
497 502
498 impl Av1DecodeSession {503 impl Av1DecodeSession {
@@ -503,6 +508,9 @@ impl Av1DecodeSession {
503 "decode size or DPB slot count is zero".into(),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 let family = gpu514 let family = gpu
507 .decode_family()515 .decode_family()
508 .ok_or_else(|| Error::Unsupported("AV1 decode queue family".into()))?;516 .ok_or_else(|| Error::Unsupported("AV1 decode queue family".into()))?;
@@ -518,7 +526,7 @@ impl Av1DecodeSession {
518 }526 }
519 let width = align(cfg.max_width, 16);527 let width = align(cfg.max_width, 16);
520 let height = align(cfg.max_height, 16);528 let height = align(cfg.max_height, 16);
521- let profile = Profile::new();529+ let profile = Profile::new(cfg.bit_depth);
522 let caps = query_caps(gpu, &profile.info)?;530 let caps = query_caps(gpu, &profile.info)?;
523 if width > caps.max_extent.width531 if width > caps.max_extent.width
524 || height > caps.max_extent.height532 || height > caps.max_extent.height
@@ -555,6 +563,7 @@ impl Av1DecodeSession {
555 Ok(Av1DecodeSession {563 Ok(Av1DecodeSession {
556 dec,564 dec,
557 sequence: None,565 sequence: None,
566+ bit_depth: cfg.bit_depth,
558 })567 })
559 }568 }
560 569
@@ -562,14 +571,15 @@ impl Av1DecodeSession {
562 pub fn set_sequence_header(&mut self, seq: &Av1SequenceHeader) -> Result<(), Error> {571 pub fn set_sequence_header(&mut self, seq: &Av1SequenceHeader) -> Result<(), Error> {
563 let c = &seq.color_config;572 let c = &seq.color_config;
564 if seq.seq_profile != 0573 if seq.seq_profile != 0
565- || c.bit_depth != 8574+ || c.bit_depth != self.bit_depth
566 || c.mono_chrome575 || c.mono_chrome
567 || c.subsampling_x != 1576 || c.subsampling_x != 1
568 || c.subsampling_y != 1577 || c.subsampling_y != 1
569 {578 {
570- return Err(Error::Unsupported(579+ return Err(Error::Unsupported(format!(
571- "AV1 sequence other than Main 8-bit 4:2:0".into(),580+ "AV1 sequence other than Main {}-bit 4:2:0",
572- ));581+ self.bit_depth
582+ )));
573 }583 }
574 let gpu = self.dec.vs.gpu.clone();584 let gpu = self.dec.vs.gpu.clone();
575 let d = gpu.device();585 let d = gpu.device();
@@ -842,7 +852,7 @@ impl Av1DecodeSession {
842 self.dec.run(&begin, &decode)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 pub fn read_output(&mut self, width: u32, height: u32) -> Result<(Vec<u8>, Vec<u8>), Error> {856 pub fn read_output(&mut self, width: u32, height: u32) -> Result<(Vec<u8>, Vec<u8>), Error> {
847 self.dec.read_nv12(width, height)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 let mut caps = vk::VideoCapabilitiesKHR::builder()864 let mut caps = vk::VideoCapabilitiesKHR::builder()
855 .push_next(&mut av1)865 .push_next(&mut av1)
856 .push_next(&mut decode);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 let caps = caps.build();869 let caps = caps.build();
859 let decode = decode.build();870 let decode = decode.build();
860 Ok(Caps {871 Ok(Caps {
shims/vk-video-ffi/src/h265_decode.rsmodified+30 -11
@@ -4,7 +4,8 @@ use vulkanalia::prelude::v1_3::*;
4 use vulkanalia::vk::KhrVideoQueueExtensionDeviceCommands;4 use vulkanalia::vk::KhrVideoQueueExtensionDeviceCommands;
5 5
6 use crate::session::{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 use crate::{Error, Gpu};10 use crate::{Error, Gpu};
10 11
@@ -17,6 +18,8 @@ pub struct H265DecodeConfig {
17 pub max_height: u32,18 pub max_height: u32,
18 /// DPB slots, including the slot the picture is decoded into.19 /// DPB slots, including the slot the picture is decoded into.
19 pub max_dpb_slots: u32,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 /// Profile, tier and level of a parameter set.25 /// Profile, tier and level of a parameter set.
@@ -321,16 +324,22 @@ struct Profile {
321 }324 }
322 325
323 impl Profile {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 let mut p = Box::new(Profile {334 let mut p = Box::new(Profile {
326 info: vk::VideoProfileInfoKHR::builder()335 info: vk::VideoProfileInfoKHR::builder()
327 .video_codec_operation(vk::VideoCodecOperationFlagsKHR::DECODE_H265)336 .video_codec_operation(vk::VideoCodecOperationFlagsKHR::DECODE_H265)
328 .chroma_subsampling(vk::VideoChromaSubsamplingFlagsKHR::_420)337 .chroma_subsampling(vk::VideoChromaSubsamplingFlagsKHR::_420)
329- .luma_bit_depth(vk::VideoComponentBitDepthFlagsKHR::_8)338+ .luma_bit_depth(component)
330- .chroma_bit_depth(vk::VideoComponentBitDepthFlagsKHR::_8)339+ .chroma_bit_depth(component)
331 .build(),340 .build(),
332 h265: vk::VideoDecodeH265ProfileInfoKHR::builder()341 h265: vk::VideoDecodeH265ProfileInfoKHR::builder()
333- .std_profile_idc(vk::video::STD_VIDEO_H265_PROFILE_IDC_MAIN)342+ .std_profile_idc(profile_idc)
334 .build(),343 .build(),
335 });344 });
336 p.info.next = &p.h265 as *const _ as *const c_void;345 p.info.next = &p.h265 as *const _ as *const c_void;
@@ -342,6 +351,7 @@ impl Profile {
342 pub struct H265DecodeSession {351 pub struct H265DecodeSession {
343 dec: Decoder,352 dec: Decoder,
344 sps: Vec<H265Sps>,353 sps: Vec<H265Sps>,
354+ bit_depth: u8,
345 }355 }
346 356
347 impl H265DecodeSession {357 impl H265DecodeSession {
@@ -352,6 +362,9 @@ impl H265DecodeSession {
352 "decode size or DPB slot count is zero".into(),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 let family = gpu368 let family = gpu
356 .decode_family()369 .decode_family()
357 .ok_or_else(|| Error::Unsupported("H.265 decode queue family".into()))?;370 .ok_or_else(|| Error::Unsupported("H.265 decode queue family".into()))?;
@@ -367,7 +380,7 @@ impl H265DecodeSession {
367 }380 }
368 let width = align(cfg.max_width, 16);381 let width = align(cfg.max_width, 16);
369 let height = align(cfg.max_height, 16);382 let height = align(cfg.max_height, 16);
370- let profile = Profile::new();383+ let profile = Profile::new(cfg.bit_depth);
371 let caps = query_caps(gpu, &profile.info)?;384 let caps = query_caps(gpu, &profile.info)?;
372 if width > caps.max_extent.width385 if width > caps.max_extent.width
373 || height > caps.max_extent.height386 || height > caps.max_extent.height
@@ -404,6 +417,7 @@ impl H265DecodeSession {
404 Ok(H265DecodeSession {417 Ok(H265DecodeSession {
405 dec,418 dec,
406 sps: Vec::new(),419 sps: Vec::new(),
420+ bit_depth: cfg.bit_depth,
407 })421 })
408 }422 }
409 423
@@ -414,12 +428,16 @@ impl H265DecodeSession {
414 sps: &[H265Sps],428 sps: &[H265Sps],
415 pps: &[H265Pps],429 pps: &[H265Pps],
416 ) -> Result<(), Error> {430 ) -> Result<(), Error> {
431+ let minus8 = self.bit_depth - 8;
417 if sps.iter().any(|s| {432 if sps.iter().any(|s| {
418 s.chroma_format_idc != 1433 s.chroma_format_idc != 1
419- || s.bit_depth_luma_minus8 != 0434+ || s.bit_depth_luma_minus8 != minus8
420- || s.bit_depth_chroma_minus8 != 0435+ || 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 let gpu = self.dec.vs.gpu.clone();442 let gpu = self.dec.vs.gpu.clone();
425 let d = gpu.device();443 let d = gpu.device();
@@ -675,7 +693,7 @@ impl H265DecodeSession {
675 self.dec.run(&begin, &decode)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 pub fn read_output(&mut self, width: u32, height: u32) -> Result<(Vec<u8>, Vec<u8>), Error> {697 pub fn read_output(&mut self, width: u32, height: u32) -> Result<(Vec<u8>, Vec<u8>), Error> {
680 self.dec.read_nv12(width, height)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 let mut caps = vk::VideoCapabilitiesKHR::builder()705 let mut caps = vk::VideoCapabilitiesKHR::builder()
688 .push_next(&mut h265)706 .push_next(&mut h265)
689 .push_next(&mut decode);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 let caps = caps.build();710 let caps = caps.build();
692 let decode = decode.build();711 let decode = decode.build();
693 Ok(Caps {712 Ok(Caps {
shims/vk-video-ffi/src/session.rsmodified+30 -8
@@ -8,6 +8,7 @@ use crate::rgb_convert::{Input, RgbConverter};
8 use crate::{Error, Gpu, RateControl};8 use crate::{Error, Gpu, RateControl};
9 9
10 pub(crate) const NV12: vk::Format = vk::Format::G8_B8R8_2PLANE_420_UNORM;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 pub(crate) const DPB_SLOTS: usize = 2;12 pub(crate) const DPB_SLOTS: usize = 2;
12 pub(crate) const PLANE_0: vk::ImageAspectFlags = vk::ImageAspectFlags::PLANE_0;13 pub(crate) const PLANE_0: vk::ImageAspectFlags = vk::ImageAspectFlags::PLANE_0;
13 pub(crate) const PLANE_1: vk::ImageAspectFlags = vk::ImageAspectFlags::PLANE_1;14 pub(crate) const PLANE_1: vk::ImageAspectFlags = vk::ImageAspectFlags::PLANE_1;
@@ -24,6 +25,18 @@ pub(crate) fn zeroed<T>() -> T {
24 unsafe { std::mem::zeroed() }25 unsafe { std::mem::zeroed() }
25 }26 }
26 27
28+pub(crate) fn bytes_per_sample(format: vk::Format) -> usize {
29+ if format == P010 { 2 } else { 1 }
30+}
31+
32+pub(crate) fn component_bit_depth(bit_depth: u8) -> vk::VideoComponentBitDepthFlagsKHR {
33+ if bit_depth == 10 {
34+ vk::VideoComponentBitDepthFlagsKHR::_10
35+ } else {
36+ vk::VideoComponentBitDepthFlagsKHR::_8
37+ }
38+}
39+
27 pub(crate) fn align(value: u32, to: u32) -> u32 {40 pub(crate) fn align(value: u32, to: u32) -> u32 {
28 value.div_ceil(to) * to41 value.div_ceil(to) * to
29 }42 }
@@ -226,6 +239,7 @@ impl Buffer {
226 pub(crate) struct VideoSession {239 pub(crate) struct VideoSession {
227 pub(crate) gpu: Gpu,240 pub(crate) gpu: Gpu,
228 pub(crate) family: u32,241 pub(crate) family: u32,
242+ pub(crate) format: vk::Format,
229 pub(crate) session: vk::VideoSessionKHR,243 pub(crate) session: vk::VideoSessionKHR,
230 pub(crate) params: vk::VideoSessionParametersKHR,244 pub(crate) params: vk::VideoSessionParametersKHR,
231 memories: Vec<vk::DeviceMemory>,245 memories: Vec<vk::DeviceMemory>,
@@ -241,9 +255,15 @@ impl VideoSession {
241 max_dpb_slots: u32,255 max_dpb_slots: u32,
242 max_active_refs: u32,256 max_active_refs: u32,
243 ) -> Result<Self, Error> {257 ) -> Result<Self, Error> {
258+ let format = if profile.luma_bit_depth == vk::VideoComponentBitDepthFlagsKHR::_10 {
259+ P010
260+ } else {
261+ NV12
262+ };
244 let mut s = VideoSession {263 let mut s = VideoSession {
245 gpu: gpu.clone(),264 gpu: gpu.clone(),
246 family,265 family,
266+ format,
247 session: Default::default(),267 session: Default::default(),
248 params: Default::default(),268 params: Default::default(),
249 memories: Vec::new(),269 memories: Vec::new(),
@@ -252,9 +272,9 @@ impl VideoSession {
252 let info = vk::VideoSessionCreateInfoKHR::builder()272 let info = vk::VideoSessionCreateInfoKHR::builder()
253 .queue_family_index(family)273 .queue_family_index(family)
254 .video_profile(profile)274 .video_profile(profile)
255- .picture_format(NV12)275+ .picture_format(format)
256 .max_coded_extent(extent)276 .max_coded_extent(extent)
257- .reference_picture_format(NV12)277+ .reference_picture_format(format)
258 .max_dpb_slots(max_dpb_slots)278 .max_dpb_slots(max_dpb_slots)
259 .max_active_reference_pictures(max_active_refs)279 .max_active_reference_pictures(max_active_refs)
260 .std_header_version(std_header_version)280 .std_header_version(std_header_version)
@@ -354,7 +374,7 @@ impl VideoSession {
354 ) -> Result<Image, Error> {374 ) -> Result<Image, Error> {
355 let info = vk::ImageCreateInfo::builder()375 let info = vk::ImageCreateInfo::builder()
356 .image_type(vk::ImageType::_2D)376 .image_type(vk::ImageType::_2D)
357- .format(NV12)377+ .format(self.format)
358 .extent(vk::Extent3D {378 .extent(vk::Extent3D {
359 width: extent.width,379 width: extent.width,
360 height: extent.height,380 height: extent.height,
@@ -385,7 +405,7 @@ impl VideoSession {
385 let view_info = vk::ImageViewCreateInfo::builder()405 let view_info = vk::ImageViewCreateInfo::builder()
386 .image(image)406 .image(image)
387 .view_type(vk::ImageViewType::_2D)407 .view_type(vk::ImageViewType::_2D)
388- .format(NV12)408+ .format(self.format)
389 .subresource_range(color_range())409 .subresource_range(color_range())
390 .push_next(&mut usage_info)410 .push_next(&mut usage_info)
391 .build();411 .build();
@@ -938,8 +958,9 @@ impl Decoder {
938 &families,958 &families,
939 &mut list,959 &mut list,
940 )?;960 )?;
961+ let bytes = bytes_per_sample(dec.vs.format) as u64;
941 let bitstream_size = align_u64(962 let bitstream_size = align_u64(
942- max_extent.width as u64 * max_extent.height as u64 * 3 + 65536,963+ max_extent.width as u64 * max_extent.height as u64 * 3 * bytes / 2 + 65536,
943 size_align,964 size_align,
944 );965 );
945 dec.bitstream = dec.vs.buffer(966 dec.bitstream = dec.vs.buffer(
@@ -949,7 +970,7 @@ impl Decoder {
949 Some(&mut list),970 Some(&mut list),
950 )?;971 )?;
951 dec.readback = dec.vs.buffer(972 dec.readback = dec.vs.buffer(
952- max_extent.width as u64 * max_extent.height as u64 * 3 / 2,973+ max_extent.width as u64 * max_extent.height as u64 * 3 / 2 * bytes,
953 vk::BufferUsageFlags::TRANSFER_DST,974 vk::BufferUsageFlags::TRANSFER_DST,
954 vk::MemoryPropertyFlags::HOST_VISIBLE | vk::MemoryPropertyFlags::HOST_COHERENT,975 vk::MemoryPropertyFlags::HOST_VISIBLE | vk::MemoryPropertyFlags::HOST_COHERENT,
955 None,976 None,
@@ -1006,7 +1027,7 @@ impl Decoder {
1006 Ok(())1027 Ok(())
1007 }1028 }
1008 1029
1009- /// Copies the top-left width x height of the last decoded picture out as NV12 planes (Y, interleaved UV).1030+ /// Copies the top-left width x height of the last decoded picture out as NV12 or P010 planes (Y, interleaved UV).
1010 pub(crate) fn read_nv12(1031 pub(crate) fn read_nv12(
1011 &mut self,1032 &mut self,
1012 width: u32,1033 width: u32,
@@ -1028,7 +1049,8 @@ impl Decoder {
1028 let d = gpu.device();1049 let d = gpu.device();
1029 let cb = self.graphics.cb;1050 let cb = self.graphics.cb;
1030 let image = self.output.image;1051 let image = self.output.image;
1031- let luma = width as usize * height as usize;1052+ let bytes = bytes_per_sample(self.vs.format);
1053+ let luma = width as usize * height as usize * bytes;
1032 let copies = [1054 let copies = [
1033 copy_region(0, PLANE_0, width, height),1055 copy_region(0, PLANE_0, width, height),
1034 copy_region(luma as u64, PLANE_1, width / 2, height / 2),1056 copy_region(luma as u64, PLANE_1, width / 2, height / 2),