| 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::sync::Arc; |
| 22 | |
| 23 | use ec_va::caps::{Entrypoint, Profile}; |
| 24 | use ec_va::{CapReport, Config, ConfigAttrib, Context, Display, Surface, SurfaceSpec, sys}; |
| 25 | |
| 26 | use crate::error::{Error, Result}; |
| 27 | use crate::frame::Frame; |
| 28 | use crate::pool::SurfacePool; |
| 29 | |
| 30 | mod av1; |
| 31 | mod dpb264; |
| 32 | mod h264; |
| 33 | mod hevc; |
| 34 | mod vp9; |
| 35 | |
| 36 | pub use av1::Av1Decoder; |
| 37 | pub use h264::H264Decoder; |
| 38 | pub use hevc::HevcDecoder; |
| 39 | pub use vp9::Vp9Decoder; |
| 40 | |
| 41 | |
| 42 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 43 | pub enum Codec { |
| 44 | |
| 45 | H264, |
| 46 | |
| 47 | H265, |
| 48 | |
| 49 | Vp9, |
| 50 | |
| 51 | Av1, |
| 52 | } |
| 53 | |
| 54 | |
| 55 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 56 | pub struct StreamInfo { |
| 57 | |
| 58 | pub coded_size: (u32, u32), |
| 59 | |
| 60 | pub display_size: (u32, u32), |
| 61 | |
| 62 | pub bit_depth: u8, |
| 63 | |
| 64 | pub profile: Profile, |
| 65 | |
| 66 | pub num_surfaces: usize, |
| 67 | } |
| 68 | |
| 69 | |
| 70 | pub struct Decoder { |
| 71 | inner: Inner, |
| 72 | } |
| 73 | |
| 74 | enum Inner { |
| 75 | H264(Box<H264Decoder>), |
| 76 | H265(Box<HevcDecoder>), |
| 77 | Vp9(Box<Vp9Decoder>), |
| 78 | Av1(Box<Av1Decoder>), |
| 79 | } |
| 80 | |
| 81 | impl Decoder { |
| 82 | |
| 83 | |
| 84 | |
| 85 | |
| 86 | |
| 87 | pub fn new(display: &Arc<Display>, codec: Codec) -> Result<Decoder> { |
| 88 | let inner = match codec { |
| 89 | Codec::H264 => Inner::H264(Box::new(H264Decoder::new(display))), |
| 90 | Codec::H265 => Inner::H265(Box::new(HevcDecoder::new(display))), |
| 91 | Codec::Vp9 => Inner::Vp9(Box::new(Vp9Decoder::new(display))), |
| 92 | Codec::Av1 => Inner::Av1(Box::new(Av1Decoder::new(display))), |
| 93 | }; |
| 94 | Ok(Decoder { inner }) |
| 95 | } |
| 96 | |
| 97 | |
| 98 | |
| 99 | pub fn decode(&mut self, data: &[u8], timestamp: i64) -> Result<()> { |
| 100 | match &mut self.inner { |
| 101 | Inner::H264(d) => d.decode(data, timestamp), |
| 102 | Inner::H265(d) => d.decode(data, timestamp), |
| 103 | Inner::Vp9(d) => d.decode(data, timestamp), |
| 104 | Inner::Av1(d) => d.decode(data, timestamp), |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | |
| 109 | pub fn next_frame(&mut self) -> Option<Frame> { |
| 110 | match &mut self.inner { |
| 111 | Inner::H264(d) => d.next_frame(), |
| 112 | Inner::H265(d) => d.next_frame(), |
| 113 | Inner::Vp9(d) => d.next_frame(), |
| 114 | Inner::Av1(d) => d.next_frame(), |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | |
| 119 | pub fn flush(&mut self) -> Result<()> { |
| 120 | match &mut self.inner { |
| 121 | Inner::H264(d) => d.flush(), |
| 122 | Inner::H265(d) => d.flush(), |
| 123 | Inner::Vp9(d) => d.flush(), |
| 124 | Inner::Av1(d) => d.flush(), |
| 125 | } |
| 126 | Ok(()) |
| 127 | } |
| 128 | |
| 129 | |
| 130 | pub fn reset(&mut self) { |
| 131 | match &mut self.inner { |
| 132 | Inner::H264(d) => d.reset(), |
| 133 | Inner::H265(d) => d.reset(), |
| 134 | Inner::Vp9(d) => d.reset(), |
| 135 | Inner::Av1(d) => d.reset(), |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | |
| 140 | |
| 141 | pub fn gap_frames_synthesized(&self) -> u64 { |
| 142 | match &self.inner { |
| 143 | Inner::H264(d) => d.gap_frames_synthesized(), |
| 144 | _ => 0, |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | |
| 149 | pub fn stream_info(&self) -> Option<StreamInfo> { |
| 150 | match &self.inner { |
| 151 | Inner::H264(d) => d.stream_info(), |
| 152 | Inner::H265(d) => d.stream_info(), |
| 153 | Inner::Vp9(d) => d.stream_info(), |
| 154 | Inner::Av1(d) => d.stream_info(), |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | |
| 159 | |
| 160 | |
| 161 | pub fn colour(&self) -> Option<crate::frame::Colour> { |
| 162 | match &self.inner { |
| 163 | Inner::H264(d) => d.colour(), |
| 164 | Inner::H265(d) => d.colour(), |
| 165 | Inner::Vp9(_) | Inner::Av1(_) => None, |
| 166 | } |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | |
| 171 | |
| 172 | |
| 173 | |
| 174 | |
| 175 | |
| 176 | |
| 177 | #[derive(Debug, Default)] |
| 178 | pub(crate) struct ReadyFrames { |
| 179 | queue: VecDeque<Frame>, |
| 180 | } |
| 181 | |
| 182 | impl ReadyFrames { |
| 183 | pub(crate) fn push(&mut self, frame: Frame) { |
| 184 | self.queue.push_back(frame); |
| 185 | } |
| 186 | |
| 187 | pub(crate) fn pop(&mut self) -> Option<Frame> { |
| 188 | self.queue.pop_front() |
| 189 | } |
| 190 | |
| 191 | pub(crate) fn clear(&mut self) { |
| 192 | self.queue.clear(); |
| 193 | } |
| 194 | |
| 195 | pub(crate) fn len(&self) -> usize { |
| 196 | self.queue.len() |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | |
| 201 | pub(crate) struct Session { |
| 202 | pub(crate) context: Arc<Context>, |
| 203 | |
| 204 | pub(crate) images: crate::frame::ImageCache, |
| 205 | pub(crate) pool: SurfacePool, |
| 206 | pub(crate) coded_size: (u32, u32), |
| 207 | pub(crate) display_size: (u32, u32), |
| 208 | pub(crate) bit_depth: u8, |
| 209 | pub(crate) profile: Profile, |
| 210 | } |
| 211 | |
| 212 | impl Session { |
| 213 | |
| 214 | |
| 215 | |
| 216 | |
| 217 | |
| 218 | |
| 219 | |
| 220 | pub(crate) fn new( |
| 221 | display: &Arc<Display>, |
| 222 | profile: Profile, |
| 223 | coded_size: (u32, u32), |
| 224 | display_size: (u32, u32), |
| 225 | bit_depth: u8, |
| 226 | num_surfaces: usize, |
| 227 | ) -> Result<Session> { |
| 228 | let caps = CapReport::probe(display)?; |
| 229 | let entry = caps.entry(profile, Entrypoint::VLD).ok_or_else(|| { |
| 230 | Error::unsupported( |
| 231 | format!("{profile:?} decode"), |
| 232 | "the driver advertises no VLD entrypoint for this profile", |
| 233 | ) |
| 234 | })?; |
| 235 | |
| 236 | let rt_format = if bit_depth > 8 { |
| 237 | sys::VA_RT_FORMAT_YUV420_10 |
| 238 | } else { |
| 239 | sys::VA_RT_FORMAT_YUV420 |
| 240 | }; |
| 241 | if !entry.supports_rt_format(rt_format) { |
| 242 | return Err(Error::unsupported( |
| 243 | format!("{bit_depth}-bit {profile:?} decode"), |
| 244 | format!( |
| 245 | "the driver's RT format mask for this profile is {:#x}", |
| 246 | entry.rt_formats |
| 247 | ), |
| 248 | )); |
| 249 | } |
| 250 | |
| 251 | let (w, h) = coded_size; |
| 252 | if let Some(surfaces) = &entry.surfaces |
| 253 | && !surfaces.allows(w, h) |
| 254 | { |
| 255 | return Err(Error::unsupported( |
| 256 | format!("{w}x{h} {profile:?} decode"), |
| 257 | format!( |
| 258 | "the driver accepts {:?}..{:?} x {:?}..{:?}", |
| 259 | surfaces.min_width, |
| 260 | surfaces.max_width, |
| 261 | surfaces.min_height, |
| 262 | surfaces.max_height |
| 263 | ), |
| 264 | )); |
| 265 | } |
| 266 | |
| 267 | let config = Config::new( |
| 268 | display, |
| 269 | profile, |
| 270 | Entrypoint::VLD, |
| 271 | &[ConfigAttrib::rt_format(rt_format)], |
| 272 | )?; |
| 273 | |
| 274 | let spec = if bit_depth > 8 { |
| 275 | SurfaceSpec::p010(w, h) |
| 276 | } else { |
| 277 | SurfaceSpec::nv12(w, h) |
| 278 | } |
| 279 | .with_usage_hint(sys::VA_SURFACE_ATTRIB_USAGE_HINT_DECODER); |
| 280 | let pool = SurfacePool::new(display, &spec, num_surfaces)?; |
| 281 | let context = Context::new(&config, w, h, sys::VA_PROGRESSIVE, pool.targets())?; |
| 282 | |
| 283 | Ok(Session { |
| 284 | context, |
| 285 | images: crate::frame::ImageCache::default(), |
| 286 | pool, |
| 287 | coded_size, |
| 288 | display_size, |
| 289 | bit_depth, |
| 290 | profile, |
| 291 | }) |
| 292 | } |
| 293 | |
| 294 | |
| 295 | |
| 296 | |
| 297 | pub(crate) fn submit(&self, target: &Arc<Surface>, buffers: Vec<ec_va::Buffer>) -> Result<()> { |
| 298 | if buffers.is_empty() { |
| 299 | |
| 300 | |
| 301 | |
| 302 | return Err(Error::config( |
| 303 | "refusing to submit a picture with no parameter buffers", |
| 304 | )); |
| 305 | } |
| 306 | ec_va::Picture::new(&self.context, Arc::clone(target)) |
| 307 | .begin()? |
| 308 | .render_all(buffers)? |
| 309 | .end()?; |
| 310 | Ok(()) |
| 311 | } |
| 312 | |
| 313 | pub(crate) fn info(&self) -> StreamInfo { |
| 314 | StreamInfo { |
| 315 | coded_size: self.coded_size, |
| 316 | display_size: self.display_size, |
| 317 | bit_depth: self.bit_depth, |
| 318 | profile: self.profile, |
| 319 | num_surfaces: self.pool.len(), |
| 320 | } |
| 321 | } |
| 322 | } |