forge
Explore
Log in

Don't you dare go Hollow

edith

Clone
★ 0

0 watching0 forksRust18.3 MiB7f487c1Tahinli3 months ago659 commits

c2_vaapi_encoder.rs112 lines, 4.4 KiBHistoryRawDownload
1// Copyright 2025 The ChromiumOS Authors
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5use std::rc::Rc;
6
7use crate::c2_wrapper::c2_encoder::C2EncoderBackend;
8use crate::encoder::av1::EncoderConfig as AV1EncoderConfig;
9use crate::encoder::h264::EncoderConfig as H264EncoderConfig;
10use crate::encoder::stateless::av1;
11use crate::encoder::stateless::h264;
12use crate::encoder::stateless::vp9;
13use crate::encoder::vp9::EncoderConfig as VP9EncoderConfig;
14use crate::encoder::VideoEncoder;
15use crate::utils::align_up;
16use crate::video_frame::VideoFrame;
17use crate::BlockingMode;
18use crate::DecodedFormat;
19use crate::EncodedFormat;
20use crate::Fourcc;
21use crate::Resolution;
22
23#[derive(Clone, Debug)]
24pub struct C2VaapiEncoderOptions {
25 pub low_power: bool,
26 pub visible_resolution: Resolution,
27}
28
29pub struct C2VaapiEncoder {
30 display: Rc<libva::Display>,
31 low_power: bool,
32 visible_resolution: Resolution,
33 coded_resolution: Resolution,
34}
35
36impl 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 // TODO: Support alternative display paths
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 // TODO: This really shouldn't be necessary, but for some reason minigbm gets the
50 // vertical alignment wrong when we omit it :(
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}