| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | use std::rc::Rc; |
| 6 | |
| 7 | use crate::c2_wrapper::c2_encoder::C2EncoderBackend; |
| 8 | use crate::encoder::av1::EncoderConfig as AV1EncoderConfig; |
| 9 | use crate::encoder::h264::EncoderConfig as H264EncoderConfig; |
| 10 | use crate::encoder::stateless::av1; |
| 11 | use crate::encoder::stateless::h264; |
| 12 | use crate::encoder::stateless::vp9; |
| 13 | use crate::encoder::vp9::EncoderConfig as VP9EncoderConfig; |
| 14 | use crate::encoder::VideoEncoder; |
| 15 | use crate::utils::align_up; |
| 16 | use crate::video_frame::VideoFrame; |
| 17 | use crate::BlockingMode; |
| 18 | use crate::DecodedFormat; |
| 19 | use crate::EncodedFormat; |
| 20 | use crate::Fourcc; |
| 21 | use crate::Resolution; |
| 22 | |
| 23 | #[derive(Clone, Debug)] |
| 24 | pub struct C2VaapiEncoderOptions { |
| 25 | pub low_power: bool, |
| 26 | pub visible_resolution: Resolution, |
| 27 | } |
| 28 | |
| 29 | pub struct C2VaapiEncoder { |
| 30 | display: Rc<libva::Display>, |
| 31 | low_power: bool, |
| 32 | visible_resolution: Resolution, |
| 33 | coded_resolution: Resolution, |
| 34 | } |
| 35 | |
| 36 | impl C2EncoderBackend for C2VaapiEncoder { |
| 37 | type EncoderOptions = C2VaapiEncoderOptions; |
| 38 | |
| 39 | fn new(options: C2VaapiEncoderOptions) -> Result<Self, String> { |
| 40 | const VAAPI_WIDTH_ALIGN: u32 = 16; |
| 41 | const VAAPI_HEIGHT_ALIGN: u32 = 16; |
| 42 | |
| 43 | |
| 44 | let display = libva::Display::open().ok_or("Error opening LibVA display!".to_string())?; |
| 45 | Ok(Self { |
| 46 | display: display, |
| 47 | low_power: options.low_power, |
| 48 | visible_resolution: options.visible_resolution.clone(), |
| 49 | |
| 50 | |
| 51 | coded_resolution: Resolution { |
| 52 | width: align_up(options.visible_resolution.width, VAAPI_WIDTH_ALIGN), |
| 53 | height: align_up(options.visible_resolution.height, VAAPI_HEIGHT_ALIGN), |
| 54 | }, |
| 55 | }) |
| 56 | } |
| 57 | |
| 58 | fn get_encoder<V: VideoFrame>( |
| 59 | &mut self, |
| 60 | input_format: DecodedFormat, |
| 61 | output_format: EncodedFormat, |
| 62 | ) -> Result<(Box<dyn VideoEncoder<V>>, Resolution, Resolution), String> { |
| 63 | Ok(match output_format { |
| 64 | EncodedFormat::H264 => { |
| 65 | let encoder = h264::StatelessEncoder::new_vaapi( |
| 66 | self.display.clone(), |
| 67 | H264EncoderConfig { |
| 68 | resolution: self.visible_resolution.clone(), |
| 69 | ..Default::default() |
| 70 | }, |
| 71 | Fourcc::from(input_format), |
| 72 | self.visible_resolution.clone(), |
| 73 | self.low_power, |
| 74 | BlockingMode::Blocking, |
| 75 | ) |
| 76 | .map_err(|err| format!("Error initializing encoder! {:?}", err))?; |
| 77 | (Box::new(encoder), self.visible_resolution.clone(), self.coded_resolution.clone()) |
| 78 | } |
| 79 | EncodedFormat::VP9 => { |
| 80 | let encoder = vp9::StatelessEncoder::new_vaapi( |
| 81 | self.display.clone(), |
| 82 | VP9EncoderConfig { |
| 83 | resolution: self.visible_resolution.clone(), |
| 84 | ..Default::default() |
| 85 | }, |
| 86 | Fourcc::from(input_format), |
| 87 | self.visible_resolution.clone(), |
| 88 | self.low_power, |
| 89 | BlockingMode::Blocking, |
| 90 | ) |
| 91 | .map_err(|err| format!("Error initializing encoder! {:?}", err))?; |
| 92 | (Box::new(encoder), self.visible_resolution.clone(), self.coded_resolution.clone()) |
| 93 | } |
| 94 | EncodedFormat::AV1 => { |
| 95 | let encoder = av1::StatelessEncoder::new_vaapi( |
| 96 | self.display.clone(), |
| 97 | AV1EncoderConfig { |
| 98 | resolution: self.visible_resolution.clone(), |
| 99 | ..Default::default() |
| 100 | }, |
| 101 | Fourcc::from(input_format), |
| 102 | self.visible_resolution.clone(), |
| 103 | self.low_power, |
| 104 | BlockingMode::Blocking, |
| 105 | ) |
| 106 | .map_err(|err| format!("Error initializing encoder! {:?}", err))?; |
| 107 | (Box::new(encoder), self.visible_resolution.clone(), self.coded_resolution.clone()) |
| 108 | } |
| 109 | _ => return Err(format!("Format not supported by V4L2! {:?}", output_format)), |
| 110 | }) |
| 111 | } |
| 112 | } |