Skip to main content

moq_vaapi/buffer/
enc_misc.rs

1// Copyright 2024 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
5//! Wrappers around `VAEncMisc*` types.
6
7use crate::bindings;
8
9#[repr(C)]
10#[derive(Default)]
11pub struct MiscEncParamBuffer<T> {
12	hdr: bindings::VAEncMiscParameterBuffer,
13	value: T,
14}
15
16impl<T> MiscEncParamBuffer<T> {
17	fn new(type_: bindings::VAEncMiscParameterType::Type, value: T) -> Self {
18		Self {
19			hdr: bindings::_VAEncMiscParameterBuffer {
20				type_,
21				data: Default::default(),
22			},
23			value,
24		}
25	}
26
27	fn new_boxed(type_: bindings::VAEncMiscParameterType::Type, value: T) -> Box<Self> {
28		Box::new(Self::new(type_, value))
29	}
30}
31
32#[derive(Default)]
33pub struct EncMiscParameterFrameRate(Box<MiscEncParamBuffer<bindings::VAEncMiscParameterFrameRate>>);
34
35impl EncMiscParameterFrameRate {
36	pub fn new(framerate: u32, temporal_id: u32) -> Self {
37		// libva expects `(denominator << 16) | numerator`, and we set framerate denominator to 1.
38		let framerate = (1 << 16) | (framerate & 0xFFFF);
39
40		let _bitfield_1 = bindings::_VAEncMiscParameterFrameRate__bindgen_ty_1__bindgen_ty_1::new_bitfield_1(
41			temporal_id,
42			Default::default(),
43		);
44
45		Self(MiscEncParamBuffer::new_boxed(
46			bindings::VAEncMiscParameterType::VAEncMiscParameterTypeFrameRate,
47			bindings::_VAEncMiscParameterFrameRate {
48				framerate,
49				framerate_flags: bindings::_VAEncMiscParameterFrameRate__bindgen_ty_1 {
50					bits: bindings::_VAEncMiscParameterFrameRate__bindgen_ty_1__bindgen_ty_1 {
51						_bitfield_align_1: Default::default(),
52						_bitfield_1,
53					},
54				},
55				..Default::default()
56			},
57		))
58	}
59
60	pub fn inner(&self) -> &MiscEncParamBuffer<bindings::VAEncMiscParameterFrameRate> {
61		&self.0
62	}
63
64	pub(crate) fn inner_mut(&mut self) -> &mut MiscEncParamBuffer<bindings::VAEncMiscParameterFrameRate> {
65		&mut self.0
66	}
67}
68
69#[derive(Default)]
70pub struct RcFlags(bindings::_VAEncMiscParameterRateControl__bindgen_ty_1);
71
72impl RcFlags {
73	pub fn new(
74		reset: u32,
75		disable_frame_skip: u32,
76		disable_bit_stuffing: u32,
77		mb_rate_control: u32,
78		temporal_id: u32,
79		cfs_i_frames: u32,
80		enable_parallel_brc: u32,
81		enable_dynamic_scaling: u32,
82		frame_tolerance_mode: u32,
83	) -> Self {
84		let _bitfield_1 = bindings::_VAEncMiscParameterRateControl__bindgen_ty_1__bindgen_ty_1::new_bitfield_1(
85			reset,
86			disable_frame_skip,
87			disable_bit_stuffing,
88			mb_rate_control,
89			temporal_id,
90			cfs_i_frames,
91			enable_parallel_brc,
92			enable_dynamic_scaling,
93			frame_tolerance_mode,
94			Default::default(),
95		);
96
97		Self(bindings::_VAEncMiscParameterRateControl__bindgen_ty_1 {
98			bits: bindings::_VAEncMiscParameterRateControl__bindgen_ty_1__bindgen_ty_1 {
99				_bitfield_align_1: Default::default(),
100				_bitfield_1,
101			},
102		})
103	}
104}
105
106#[derive(Default)]
107pub struct EncMiscParameterRateControl(Box<MiscEncParamBuffer<bindings::VAEncMiscParameterRateControl>>);
108
109impl EncMiscParameterRateControl {
110	pub fn new(
111		bits_per_second: u32,
112		target_percentage: u32,
113		window_size: u32,
114		initial_qp: u32,
115		min_qp: u32,
116		basic_unit_size: u32,
117		rc_flags: RcFlags,
118		icq_quality_factor: u32,
119		max_qp: u32,
120		quality_factor: u32,
121		target_frame_size: u32,
122	) -> Self {
123		Self(MiscEncParamBuffer::new_boxed(
124			bindings::VAEncMiscParameterType::VAEncMiscParameterTypeRateControl,
125			bindings::VAEncMiscParameterRateControl {
126				bits_per_second,
127				target_percentage,
128				window_size,
129				initial_qp,
130				min_qp,
131				basic_unit_size,
132				rc_flags: rc_flags.0,
133				ICQ_quality_factor: icq_quality_factor,
134				max_qp,
135				quality_factor,
136				#[cfg(libva_1_10_or_higher)]
137				target_frame_size,
138				..Default::default()
139			},
140		))
141	}
142
143	pub fn inner(&self) -> &MiscEncParamBuffer<bindings::VAEncMiscParameterRateControl> {
144		&self.0
145	}
146
147	pub(crate) fn inner_mut(&mut self) -> &mut MiscEncParamBuffer<bindings::VAEncMiscParameterRateControl> {
148		&mut self.0
149	}
150}
151
152#[derive(Default)]
153pub struct EncMiscParameterMaxSliceSize(Box<MiscEncParamBuffer<bindings::VAEncMiscParameterMaxSliceSize>>);
154
155impl EncMiscParameterMaxSliceSize {
156	pub fn new(max_slice_size: u32) -> Self {
157		Self(MiscEncParamBuffer::new_boxed(
158			bindings::VAEncMiscParameterType::VAEncMiscParameterTypeMaxFrameSize,
159			bindings::VAEncMiscParameterMaxSliceSize {
160				max_slice_size,
161				..Default::default()
162			},
163		))
164	}
165
166	pub fn inner(&self) -> &MiscEncParamBuffer<bindings::VAEncMiscParameterMaxSliceSize> {
167		&self.0
168	}
169
170	pub(crate) fn inner_mut(&mut self) -> &mut MiscEncParamBuffer<bindings::VAEncMiscParameterMaxSliceSize> {
171		&mut self.0
172	}
173}
174
175#[derive(Default)]
176pub struct EncMiscParameterBufferMaxFrameSize(Box<MiscEncParamBuffer<bindings::VAEncMiscParameterBufferMaxFrameSize>>);
177
178impl EncMiscParameterBufferMaxFrameSize {
179	pub fn new(max_frame_size: u32) -> Self {
180		Self(MiscEncParamBuffer::new_boxed(
181			bindings::VAEncMiscParameterType::VAEncMiscParameterTypeMaxFrameSize,
182			bindings::VAEncMiscParameterBufferMaxFrameSize {
183				type_: bindings::VAEncMiscParameterType::VAEncMiscParameterTypeMaxFrameSize,
184				max_frame_size,
185				..Default::default()
186			},
187		))
188	}
189
190	pub fn inner(&self) -> &MiscEncParamBuffer<bindings::VAEncMiscParameterBufferMaxFrameSize> {
191		&self.0
192	}
193
194	pub(crate) fn inner_mut(&mut self) -> &mut MiscEncParamBuffer<bindings::VAEncMiscParameterBufferMaxFrameSize> {
195		&mut self.0
196	}
197}
198
199#[derive(Default)]
200pub struct EncMiscParameterSkipFrame(Box<MiscEncParamBuffer<bindings::VAEncMiscParameterSkipFrame>>);
201
202impl EncMiscParameterSkipFrame {
203	pub fn new(skip_frame_flag: u8, num_skip_frames: u8, size_skip_frames: u32) -> Self {
204		Self(MiscEncParamBuffer::new_boxed(
205			bindings::VAEncMiscParameterType::VAEncMiscParameterTypeSkipFrame,
206			bindings::VAEncMiscParameterSkipFrame {
207				skip_frame_flag,
208				num_skip_frames,
209				size_skip_frames,
210				..Default::default()
211			},
212		))
213	}
214
215	pub fn inner(&self) -> &MiscEncParamBuffer<bindings::VAEncMiscParameterSkipFrame> {
216		&self.0
217	}
218
219	pub(crate) fn inner_mut(&mut self) -> &mut MiscEncParamBuffer<bindings::VAEncMiscParameterSkipFrame> {
220		&mut self.0
221	}
222}
223
224#[derive(Default)]
225pub struct EncMiscParameterHRD(Box<MiscEncParamBuffer<bindings::VAEncMiscParameterHRD>>);
226
227impl EncMiscParameterHRD {
228	pub fn new(initial_buffer_fullness: u32, buffer_size: u32) -> Self {
229		Self(MiscEncParamBuffer::new_boxed(
230			bindings::VAEncMiscParameterType::VAEncMiscParameterTypeHRD,
231			bindings::VAEncMiscParameterHRD {
232				initial_buffer_fullness,
233				buffer_size,
234				..Default::default()
235			},
236		))
237	}
238
239	pub fn inner(&self) -> &MiscEncParamBuffer<bindings::VAEncMiscParameterHRD> {
240		&self.0
241	}
242
243	pub(crate) fn inner_mut(&mut self) -> &mut MiscEncParamBuffer<bindings::VAEncMiscParameterHRD> {
244		&mut self.0
245	}
246}
247
248#[derive(Default)]
249pub struct EncMiscParameterBufferQualityLevel(Box<MiscEncParamBuffer<bindings::VAEncMiscParameterBufferQualityLevel>>);
250
251impl EncMiscParameterBufferQualityLevel {
252	pub fn new(quality_level: u32) -> Self {
253		Self(MiscEncParamBuffer::new_boxed(
254			bindings::VAEncMiscParameterType::VAEncMiscParameterTypeQualityLevel,
255			bindings::VAEncMiscParameterBufferQualityLevel {
256				quality_level,
257				..Default::default()
258			},
259		))
260	}
261
262	pub fn inner(&self) -> &MiscEncParamBuffer<bindings::VAEncMiscParameterBufferQualityLevel> {
263		&self.0
264	}
265
266	pub(crate) fn inner_mut(&mut self) -> &mut MiscEncParamBuffer<bindings::VAEncMiscParameterBufferQualityLevel> {
267		&mut self.0
268	}
269}
270
271#[derive(Default)]
272pub struct EncMiscParameterQuantization(Box<MiscEncParamBuffer<bindings::VAEncMiscParameterQuantization>>);
273
274impl EncMiscParameterQuantization {
275	pub fn new(disable_trellis: bool, enable_trellis_i: bool, enable_trellis_p: bool, enable_trellis_b: bool) -> Self {
276		let _bitfield_1 = bindings::_VAEncMiscParameterQuantization__bindgen_ty_1__bindgen_ty_1::new_bitfield_1(
277			disable_trellis as u32,
278			enable_trellis_i as u32,
279			enable_trellis_p as u32,
280			enable_trellis_b as u32,
281			Default::default(),
282		);
283
284		Self(MiscEncParamBuffer::new_boxed(
285			bindings::VAEncMiscParameterType::VAEncMiscParameterTypeQuantization,
286			bindings::VAEncMiscParameterQuantization {
287				quantization_flags: bindings::_VAEncMiscParameterQuantization__bindgen_ty_1 {
288					bits: bindings::_VAEncMiscParameterQuantization__bindgen_ty_1__bindgen_ty_1 {
289						_bitfield_align_1: Default::default(),
290						_bitfield_1,
291					},
292				},
293				..Default::default()
294			},
295		))
296	}
297
298	pub fn inner(&self) -> &MiscEncParamBuffer<bindings::VAEncMiscParameterQuantization> {
299		&self.0
300	}
301
302	pub(crate) fn inner_mut(&mut self) -> &mut MiscEncParamBuffer<bindings::VAEncMiscParameterQuantization> {
303		&mut self.0
304	}
305}