| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | |
| 16 | |
| 17 | |
| 18 | |
| 19 | |
| 20 | use std::collections::VecDeque; |
| 21 | use std::rc::Rc; |
| 22 | use std::sync::atomic::Ordering; |
| 23 | use std::time::{Duration, Instant}; |
| 24 | |
| 25 | use ec_va::caps::{Entrypoint, Profile}; |
| 26 | use ec_va::va; |
| 27 | use ec_va::{CapReport, Config, ConfigAttrib, Context, Display, Surface, SurfaceSpec}; |
| 28 | |
| 29 | use crate::error::{Error, Result}; |
| 30 | use crate::frame::Frame; |
| 31 | use crate::pool::{PooledSurface, SurfacePool}; |
| 32 | |
| 33 | pub use crate::types::Codec; |
| 34 | |
| 35 | mod av1; |
| 36 | mod h264; |
| 37 | mod vp9; |
| 38 | |
| 39 | #[doc(hidden)] |
| 40 | pub use av1::Av1Decoder; |
| 41 | #[doc(hidden)] |
| 42 | pub use av1::fuzz_build_parameters as av1_fuzz_build_parameters; |
| 43 | mod hevc; |
| 44 | |
| 45 | #[doc(hidden)] |
| 46 | pub use h264::H264Decoder; |
| 47 | #[doc(hidden)] |
| 48 | pub use h264::fuzz_build_parameters as h264_fuzz_build_parameters; |
| 49 | #[doc(hidden)] |
| 50 | pub use vp9::Vp9Decoder; |
| 51 | #[doc(hidden)] |
| 52 | pub use vp9::fuzz_build_parameters as vp9_fuzz_build_parameters; |
| 53 | pub use hevc::HevcDecoder; |
| 54 | #[doc(hidden)] |
| 55 | pub use hevc::fuzz_build_parameters as hevc_fuzz_build_parameters; |
| 56 | |
| 57 | |
| 58 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 59 | pub struct StreamInfo { |
| 60 | |
| 61 | pub coded_size: (u32, u32), |
| 62 | |
| 63 | pub display_size: (u32, u32), |
| 64 | |
| 65 | pub bit_depth: u8, |
| 66 | |
| 67 | pub profile: Profile, |
| 68 | |
| 69 | pub num_surfaces: usize, |
| 70 | } |
| 71 | |
| 72 | |
| 73 | |
| 74 | |
| 75 | pub(crate) fn units(data: &[u8]) -> Vec<&[u8]> { |
| 76 | let mut out = Vec::new(); |
| 77 | let mut rest = data; |
| 78 | while let Some((len, tail)) = rest.split_first_chunk::<4>() { |
| 79 | let take = (u32::from_le_bytes(*len) as usize).min(tail.len()); |
| 80 | out.push(&tail[..take]); |
| 81 | rest = &tail[take..]; |
| 82 | } |
| 83 | out |
| 84 | } |
| 85 | |
| 86 | |
| 87 | |
| 88 | |
| 89 | pub(crate) fn rt_format_for_bit_depth(bit_depth: u8) -> u32 { |
| 90 | if bit_depth > 8 { |
| 91 | va::VA_RT_FORMAT_YUV420_10 |
| 92 | } else { |
| 93 | va::VA_RT_FORMAT_YUV420 |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | |
| 98 | pub struct Decoder { |
| 99 | inner: Inner, |
| 100 | } |
| 101 | |
| 102 | enum Inner { |
| 103 | H264(Box<H264Decoder>), |
| 104 | Vp9(Box<Vp9Decoder>), |
| 105 | Av1(Box<Av1Decoder>), |
| 106 | H265(Box<HevcDecoder>), |
| 107 | } |
| 108 | |
| 109 | impl Decoder { |
| 110 | |
| 111 | |
| 112 | |
| 113 | |
| 114 | |
| 115 | pub fn new(display: &Rc<Display>, codec: Codec) -> Result<Decoder> { |
| 116 | let inner = match codec { |
| 117 | Codec::H264 => Inner::H264(Box::new(H264Decoder::new(display))), |
| 118 | Codec::Vp9 => Inner::Vp9(Box::new(Vp9Decoder::new(display))), |
| 119 | Codec::Av1 => Inner::Av1(Box::new(Av1Decoder::new(display))), |
| 120 | Codec::H265 => Inner::H265(Box::new(HevcDecoder::new(display))), |
| 121 | }; |
| 122 | Ok(Decoder { inner }) |
| 123 | } |
| 124 | |
| 125 | |
| 126 | |
| 127 | pub fn decode(&mut self, data: &[u8], timestamp: i64) -> Result<()> { |
| 128 | match &mut self.inner { |
| 129 | Inner::H264(d) => d.decode(data, timestamp), |
| 130 | Inner::Vp9(d) => d.decode(data, timestamp), |
| 131 | Inner::Av1(d) => d.decode(data, timestamp), |
| 132 | Inner::H265(d) => d.decode(data, timestamp), |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | |
| 137 | pub fn next_frame(&mut self) -> Option<Frame> { |
| 138 | match &mut self.inner { |
| 139 | Inner::H264(d) => d.next_frame(), |
| 140 | Inner::Vp9(d) => d.next_frame(), |
| 141 | Inner::Av1(d) => d.next_frame(), |
| 142 | Inner::H265(d) => d.next_frame(), |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | |
| 147 | pub fn flush(&mut self) -> Result<()> { |
| 148 | match &mut self.inner { |
| 149 | Inner::H264(d) => d.flush(), |
| 150 | Inner::Vp9(d) => d.flush(), |
| 151 | Inner::Av1(d) => d.flush(), |
| 152 | Inner::H265(d) => d.flush(), |
| 153 | } |
| 154 | Ok(()) |
| 155 | } |
| 156 | |
| 157 | |
| 158 | pub fn reset(&mut self) { |
| 159 | match &mut self.inner { |
| 160 | Inner::H264(d) => d.reset(), |
| 161 | Inner::Vp9(d) => d.reset(), |
| 162 | Inner::Av1(d) => d.reset(), |
| 163 | Inner::H265(d) => d.reset(), |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | |
| 168 | |
| 169 | pub fn gap_frames_synthesized(&self) -> u64 { |
| 170 | match &self.inner { |
| 171 | Inner::H264(d) => d.gap_frames_synthesized(), |
| 172 | Inner::Vp9(_) | Inner::Av1(_) => 0, |
| 173 | Inner::H265(_) => 0, |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | |
| 178 | pub fn stream_info(&self) -> Option<StreamInfo> { |
| 179 | match &self.inner { |
| 180 | Inner::H264(d) => d.stream_info(), |
| 181 | Inner::Vp9(d) => d.stream_info(), |
| 182 | Inner::Av1(d) => d.stream_info(), |
| 183 | Inner::H265(d) => d.stream_info(), |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | |
| 188 | |
| 189 | |
| 190 | pub fn colour(&self) -> Option<crate::frame::Colour> { |
| 191 | match &self.inner { |
| 192 | Inner::H264(d) => d.colour(), |
| 193 | Inner::Vp9(_) | Inner::Av1(_) => None, |
| 194 | Inner::H265(d) => d.colour(), |
| 195 | } |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | |
| 200 | |
| 201 | |
| 202 | |
| 203 | |
| 204 | |
| 205 | |
| 206 | #[derive(Debug, Default)] |
| 207 | pub(crate) struct ReadyFrames { |
| 208 | queue: VecDeque<Frame>, |
| 209 | } |
| 210 | |
| 211 | impl ReadyFrames { |
| 212 | pub(crate) fn push(&mut self, frame: Frame) { |
| 213 | self.queue.push_back(frame); |
| 214 | } |
| 215 | |
| 216 | pub(crate) fn pop(&mut self) -> Option<Frame> { |
| 217 | self.queue.pop_front() |
| 218 | } |
| 219 | |
| 220 | pub(crate) fn clear(&mut self) { |
| 221 | self.queue.clear(); |
| 222 | } |
| 223 | |
| 224 | pub(crate) fn len(&self) -> usize { |
| 225 | self.queue.len() |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | |
| 230 | |
| 231 | |
| 232 | const IDLE_SETTLE: Duration = Duration::from_millis(250); |
| 233 | |
| 234 | |
| 235 | |
| 236 | fn context_gone(status: i32) -> bool { |
| 237 | status_is( |
| 238 | status, |
| 239 | &[ |
| 240 | va::VA_STATUS_ERROR_INVALID_CONTEXT, |
| 241 | va::VA_STATUS_ERROR_OPERATION_FAILED, |
| 242 | va::VA_STATUS_ERROR_HW_BUSY, |
| 243 | va::VA_STATUS_ERROR_TIMEDOUT, |
| 244 | va::VA_STATUS_ERROR_DECODING_ERROR, |
| 245 | ], |
| 246 | ) |
| 247 | } |
| 248 | |
| 249 | |
| 250 | |
| 251 | fn status_is(status: i32, codes: &[u32]) -> bool { |
| 252 | codes.iter().any(|&c| c as i32 == status) |
| 253 | } |
| 254 | |
| 255 | |
| 256 | struct Pending { |
| 257 | surface: Rc<Surface>, |
| 258 | since: Instant, |
| 259 | } |
| 260 | |
| 261 | pub(crate) struct Session { |
| 262 | |
| 263 | |
| 264 | born_epoch: u64, |
| 265 | |
| 266 | |
| 267 | pending: std::sync::Mutex<Option<Pending>>, |
| 268 | |
| 269 | |
| 270 | |
| 271 | |
| 272 | pub(crate) poisoned: std::sync::atomic::AtomicBool, |
| 273 | pub(crate) context: Rc<Context>, |
| 274 | display: Rc<Display>, |
| 275 | |
| 276 | pub(crate) pool: SurfacePool, |
| 277 | pub(crate) coded_size: (u32, u32), |
| 278 | pub(crate) display_size: (u32, u32), |
| 279 | pub(crate) bit_depth: u8, |
| 280 | pub(crate) profile: Profile, |
| 281 | } |
| 282 | |
| 283 | impl Session { |
| 284 | |
| 285 | |
| 286 | |
| 287 | |
| 288 | |
| 289 | |
| 290 | |
| 291 | pub(crate) fn new( |
| 292 | display: &Rc<Display>, |
| 293 | profile: Profile, |
| 294 | coded_size: (u32, u32), |
| 295 | display_size: (u32, u32), |
| 296 | bit_depth: u8, |
| 297 | num_surfaces: usize, |
| 298 | ) -> Result<Session> { |
| 299 | let born_epoch = crate::reset::gpu_reset_epoch(); |
| 300 | if crate::reset::gpu_recently_reset() { |
| 301 | return Err(Error::hw_reset( |
| 302 | "the GPU was reset moments ago; new hardware sessions wait out the grace period", |
| 303 | )); |
| 304 | } |
| 305 | let caps = CapReport::probe(display)?; |
| 306 | let entry = caps.entry(profile, Entrypoint::VLD).ok_or_else(|| { |
| 307 | Error::unsupported( |
| 308 | format!("{profile:?} decode"), |
| 309 | "the driver advertises no VLD entrypoint for this profile", |
| 310 | ) |
| 311 | })?; |
| 312 | |
| 313 | let rt_format = rt_format_for_bit_depth(bit_depth); |
| 314 | if !entry.supports_rt_format(rt_format) { |
| 315 | return Err(Error::unsupported( |
| 316 | format!("{bit_depth}-bit {profile:?} decode"), |
| 317 | format!( |
| 318 | "the driver's RT format mask for this profile is {:#x}", |
| 319 | entry.rt_formats |
| 320 | ), |
| 321 | )); |
| 322 | } |
| 323 | |
| 324 | let (w, h) = coded_size; |
| 325 | let (dw, dh) = display_size; |
| 326 | if let Some(surfaces) = &entry.surfaces |
| 327 | && !(surfaces.allows(w, h) && surfaces.allows(dw.max(1), dh.max(1))) |
| 328 | { |
| 329 | return Err(Error::unsupported( |
| 330 | format!("{dw}x{dh} {profile:?} decode"), |
| 331 | format!( |
| 332 | "the driver accepts {:?}..{:?} x {:?}..{:?}", |
| 333 | surfaces.min_width, |
| 334 | surfaces.max_width, |
| 335 | surfaces.min_height, |
| 336 | surfaces.max_height |
| 337 | ), |
| 338 | )); |
| 339 | } |
| 340 | |
| 341 | let config = Config::new( |
| 342 | display, |
| 343 | profile, |
| 344 | Entrypoint::VLD, |
| 345 | &[ConfigAttrib::rt_format(rt_format)], |
| 346 | )?; |
| 347 | |
| 348 | let spec = if bit_depth > 8 { |
| 349 | SurfaceSpec::p010(w, h) |
| 350 | } else { |
| 351 | SurfaceSpec::nv12(w, h) |
| 352 | } |
| 353 | .with_usage_hint(va::VA_SURFACE_ATTRIB_USAGE_HINT_DECODER); |
| 354 | let surfaces = ec_va::create_pool(display, &spec, num_surfaces)?; |
| 355 | let context = |
| 356 | ec_va::picture::create_context(display, &config, w, h, Some(&surfaces), true)?; |
| 357 | let pool = SurfacePool::from_surfaces(surfaces, &spec); |
| 358 | |
| 359 | if crate::reset::gpu_reset_epoch() != born_epoch { |
| 360 | return Err(Error::hw_reset( |
| 361 | "the GPU was reset while the session was created", |
| 362 | )); |
| 363 | } |
| 364 | Ok(Session { |
| 365 | born_epoch, |
| 366 | pending: std::sync::Mutex::new(None), |
| 367 | poisoned: std::sync::atomic::AtomicBool::new(false), |
| 368 | context, |
| 369 | display: Rc::clone(display), |
| 370 | pool, |
| 371 | coded_size, |
| 372 | display_size, |
| 373 | bit_depth, |
| 374 | profile, |
| 375 | }) |
| 376 | } |
| 377 | |
| 378 | |
| 379 | |
| 380 | |
| 381 | pub(crate) fn acquire(&self) -> Result<Rc<PooledSurface>> { |
| 382 | if let Some(surface) = self.pool.acquire() { |
| 383 | return Ok(surface); |
| 384 | } |
| 385 | let spec = *self.pool.spec(); |
| 386 | self.pool.grow(ec_va::create_pool(&self.display, &spec, 1)?); |
| 387 | self.pool |
| 388 | .acquire() |
| 389 | .ok_or_else(|| Error::config("surface pool empty after growing")) |
| 390 | } |
| 391 | |
| 392 | |
| 393 | |
| 394 | |
| 395 | |
| 396 | |
| 397 | |
| 398 | |
| 399 | |
| 400 | |
| 401 | |
| 402 | |
| 403 | |
| 404 | |
| 405 | |
| 406 | pub(crate) fn submit(&self, target: &Rc<Surface>, buffers: Vec<ec_va::Buffer>) -> Result<()> { |
| 407 | if buffers.is_empty() { |
| 408 | |
| 409 | |
| 410 | |
| 411 | return Err(Error::config( |
| 412 | "refusing to submit a picture with no parameter buffers", |
| 413 | )); |
| 414 | } |
| 415 | self.check_alive()?; |
| 416 | |
| 417 | |
| 418 | let stale = self |
| 419 | .pending_slot() |
| 420 | .as_ref() |
| 421 | .filter(|p| p.since.elapsed() > IDLE_SETTLE) |
| 422 | .map(|p| Rc::clone(&p.surface)); |
| 423 | if let Some(old) = stale { |
| 424 | self.settle(&old)?; |
| 425 | self.check_alive()?; |
| 426 | } |
| 427 | let outcome = ec_va::picture::submit(&self.context, Rc::clone(target), buffers); |
| 428 | if let Err(e) = outcome { |
| 429 | match e.status() { |
| 430 | Some(status) |
| 431 | if status_is( |
| 432 | status, |
| 433 | &[ |
| 434 | va::VA_STATUS_ERROR_OPERATION_FAILED, |
| 435 | va::VA_STATUS_ERROR_DECODING_ERROR, |
| 436 | ], |
| 437 | ) => |
| 438 | { |
| 439 | self.poison() |
| 440 | } |
| 441 | Some(status) if context_gone(status) => crate::reset::report_gpu_reset(), |
| 442 | _ => {} |
| 443 | } |
| 444 | return Err(e.into()); |
| 445 | } |
| 446 | let previous = self.pending_slot().replace(Pending { |
| 447 | surface: Rc::clone(target), |
| 448 | since: Instant::now(), |
| 449 | }); |
| 450 | if let Some(previous) = previous { |
| 451 | self.settle(&previous.surface)?; |
| 452 | } |
| 453 | Ok(()) |
| 454 | } |
| 455 | |
| 456 | fn pending_slot(&self) -> std::sync::MutexGuard<'_, Option<Pending>> { |
| 457 | match self.pending.lock() { |
| 458 | Ok(slot) => slot, |
| 459 | Err(poisoned) => poisoned.into_inner(), |
| 460 | } |
| 461 | } |
| 462 | |
| 463 | fn check_alive(&self) -> Result<()> { |
| 464 | if crate::reset::gpu_reset_epoch() != self.born_epoch { |
| 465 | return Err(Error::hw_reset( |
| 466 | "the GPU was reset after this session was created; its context is lost", |
| 467 | )); |
| 468 | } |
| 469 | if self.poisoned.load(Ordering::Acquire) { |
| 470 | return Err(Error::poisoned("an earlier picture failed on the GPU")); |
| 471 | } |
| 472 | Ok(()) |
| 473 | } |
| 474 | |
| 475 | fn poison(&self) { |
| 476 | self.poisoned.store(true, Ordering::Release); |
| 477 | } |
| 478 | |
| 479 | |
| 480 | fn settle(&self, surface: &Surface) -> Result<()> { |
| 481 | crate::reset::sync_watched(surface) |
| 482 | } |
| 483 | |
| 484 | pub(crate) fn info(&self) -> StreamInfo { |
| 485 | StreamInfo { |
| 486 | coded_size: self.coded_size, |
| 487 | display_size: self.display_size, |
| 488 | bit_depth: self.bit_depth, |
| 489 | profile: self.profile, |
| 490 | num_surfaces: self.pool.len(), |
| 491 | } |
| 492 | } |
| 493 | } |
| 494 | |
| 495 | #[cfg(test)] |
| 496 | mod tests { |
| 497 | use super::*; |
| 498 | |
| 499 | |
| 500 | |
| 501 | |
| 502 | #[test] |
| 503 | fn decode_context_rt_format_follows_the_bit_depth() { |
| 504 | assert_eq!(rt_format_for_bit_depth(8), va::VA_RT_FORMAT_YUV420); |
| 505 | assert_eq!(rt_format_for_bit_depth(10), va::VA_RT_FORMAT_YUV420_10); |
| 506 | assert_ne!(rt_format_for_bit_depth(10), rt_format_for_bit_depth(8)); |
| 507 | } |
| 508 | } |