| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | use std::path::PathBuf; |
| 6 | use std::rc::Rc; |
| 7 | |
| 8 | use crate::c2_wrapper::c2_decoder::C2DecoderBackend; |
| 9 | use crate::decoder::stateless::av1::Av1; |
| 10 | use crate::decoder::stateless::h264::H264; |
| 11 | use crate::decoder::stateless::h265::H265; |
| 12 | use crate::decoder::stateless::vp8::Vp8; |
| 13 | use crate::decoder::stateless::vp9::Vp9; |
| 14 | use crate::decoder::stateless::DynStatelessVideoDecoder; |
| 15 | use crate::decoder::stateless::StatelessDecoder; |
| 16 | use crate::decoder::stateless::StatelessVideoDecoder; |
| 17 | use crate::decoder::BlockingMode; |
| 18 | use crate::video_frame::VideoFrame; |
| 19 | use crate::EncodedFormat; |
| 20 | use crate::Fourcc; |
| 21 | |
| 22 | #[derive(Clone, Debug)] |
| 23 | pub struct C2VaapiDecoderOptions { |
| 24 | pub libva_device_path: Option<PathBuf>, |
| 25 | } |
| 26 | |
| 27 | pub struct C2VaapiDecoder { |
| 28 | display: Rc<libva::Display>, |
| 29 | } |
| 30 | |
| 31 | impl C2DecoderBackend for C2VaapiDecoder { |
| 32 | type DecoderOptions = C2VaapiDecoderOptions; |
| 33 | |
| 34 | fn new(options: C2VaapiDecoderOptions) -> Result<Self, String> { |
| 35 | let display = match options.libva_device_path { |
| 36 | Some(libva_device_path) => libva::Display::open_drm_display(libva_device_path.clone()) |
| 37 | .map_err(|_| format!("failed to open libva display {libva_device_path:?}"))?, |
| 38 | None => libva::Display::open().ok_or("failed to open libva display")?, |
| 39 | }; |
| 40 | |
| 41 | Ok(Self { display: display }) |
| 42 | } |
| 43 | |
| 44 | |
| 45 | fn supported_output_formats(&self) -> Vec<Fourcc> { |
| 46 | vec![Fourcc::from(b"NV12")] |
| 47 | } |
| 48 | |
| 49 | fn get_decoder<V: VideoFrame + 'static>( |
| 50 | &mut self, |
| 51 | format: EncodedFormat, |
| 52 | ) -> Result<DynStatelessVideoDecoder<V>, String> { |
| 53 | Ok(match format { |
| 54 | EncodedFormat::H264 => StatelessDecoder::<H264, _>::new_vaapi( |
| 55 | self.display.clone(), |
| 56 | BlockingMode::NonBlocking, |
| 57 | ) |
| 58 | .map_err(|_| "Failed to instantiate H264 encoder")? |
| 59 | .into_trait_object(), |
| 60 | EncodedFormat::H265 => StatelessDecoder::<H265, _>::new_vaapi( |
| 61 | self.display.clone(), |
| 62 | BlockingMode::NonBlocking, |
| 63 | ) |
| 64 | .map_err(|_| "Failed to instantiate H265 encoder")? |
| 65 | .into_trait_object(), |
| 66 | EncodedFormat::VP8 => StatelessDecoder::<Vp8, _>::new_vaapi( |
| 67 | self.display.clone(), |
| 68 | BlockingMode::NonBlocking, |
| 69 | ) |
| 70 | .map_err(|_| "Failed to instantiate VP8 encoder")? |
| 71 | .into_trait_object(), |
| 72 | EncodedFormat::VP9 => StatelessDecoder::<Vp9, _>::new_vaapi( |
| 73 | self.display.clone(), |
| 74 | BlockingMode::NonBlocking, |
| 75 | ) |
| 76 | .map_err(|_| "Failed to instantiate VP9 encoder")? |
| 77 | .into_trait_object(), |
| 78 | EncodedFormat::AV1 => StatelessDecoder::<Av1, _>::new_vaapi( |
| 79 | self.display.clone(), |
| 80 | BlockingMode::NonBlocking, |
| 81 | ) |
| 82 | .map_err(|_| "Failed to instantiate AV1 encoder")? |
| 83 | .into_trait_object(), |
| 84 | }) |
| 85 | } |
| 86 | } |