forge
Explore
Log in

Don't you dare go Hollow

edith

Clone
★ 0

0 watching0 forksRust18.3 MiBfbc29d1Tahinli2 months ago972 commits

vaapi.rs648 lines, 23.6 KiBHistoryRawDownload
1// Copyright 2023 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 anyhow::anyhow;
8use anyhow::Context;
9use libva::Display;
10
11use crate::backend::vaapi::decoder::va_surface_id;
12use crate::backend::vaapi::decoder::DecodedHandle as VADecodedHandle;
13use crate::backend::vaapi::decoder::VaStreamInfo;
14use crate::backend::vaapi::decoder::VaapiBackend;
15use crate::backend::vaapi::decoder::VaapiPicture;
16use crate::codec::av1::parser::BitDepth;
17use crate::codec::av1::parser::FrameHeaderObu;
18use crate::codec::av1::parser::Profile;
19use crate::codec::av1::parser::StreamInfo;
20use crate::codec::av1::parser::TileGroupObu;
21use crate::codec::av1::parser::WarpModelType;
22use crate::codec::av1::parser::MAX_SEGMENTS;
23use crate::codec::av1::parser::MAX_TILE_COLS;
24use crate::codec::av1::parser::MAX_TILE_ROWS;
25use crate::codec::av1::parser::NUM_REF_FRAMES;
26use crate::codec::av1::parser::SEG_LVL_MAX;
27use crate::decoder::stateless::av1::Av1;
28use crate::decoder::stateless::av1::StatelessAV1DecoderBackend;
29use crate::decoder::stateless::NewPictureError;
30use crate::decoder::stateless::NewPictureResult;
31use crate::decoder::stateless::NewStatelessDecoderError;
32use crate::decoder::stateless::StatelessBackendResult;
33use crate::decoder::stateless::StatelessDecoder;
34use crate::decoder::stateless::StatelessDecoderBackendPicture;
35use crate::decoder::BlockingMode;
36use crate::video_frame::VideoFrame;
37use crate::Rect;
38use crate::Resolution;
39
40/// The number of surfaces to allocate for this codec.
41const NUM_SURFACES: usize = 16;
42
43impl VaStreamInfo for &StreamInfo {
44 fn va_profile(&self) -> anyhow::Result<i32> {
45 match self.seq_header.seq_profile {
46 Profile::Profile0 => Ok(libva::VAProfile::VAProfileAV1Profile0),
47 Profile::Profile1 => Ok(libva::VAProfile::VAProfileAV1Profile1),
48 Profile::Profile2 => {
49 Err(anyhow!("Profile {:?} is not supported by VAAPI", self.seq_header.seq_profile))
50 }
51 }
52 }
53
54 fn rt_format(&self) -> anyhow::Result<u32> {
55 // See table 6.4.1.
56 match self.seq_header.seq_profile {
57 Profile::Profile0 => {
58 if self.seq_header.bit_depth == BitDepth::Depth8 {
59 Ok(libva::VA_RT_FORMAT_YUV420)
60 } else if self.seq_header.bit_depth == BitDepth::Depth10 {
61 Ok(libva::VA_RT_FORMAT_YUV420_10)
62 } else {
63 Err(anyhow!(
64 "Unsupported bit depth {:?} for profile {:?}",
65 self.seq_header.bit_depth,
66 self.seq_header.seq_profile
67 ))
68 }
69 }
70 Profile::Profile1 => {
71 if self.seq_header.bit_depth == BitDepth::Depth8 {
72 Ok(libva::VA_RT_FORMAT_YUV444)
73 } else if self.seq_header.bit_depth == BitDepth::Depth10 {
74 Ok(libva::VA_RT_FORMAT_YUV444_10)
75 } else {
76 Err(anyhow!(
77 "Unsupported bit depth {:?} for profile {:?}",
78 self.seq_header.bit_depth,
79 self.seq_header.seq_profile
80 ))
81 }
82 }
83 Profile::Profile2 => {
84 Err(anyhow!("Profile {:?} is not supported by VAAPI", self.seq_header.seq_profile))
85 }
86 }
87 }
88
89 fn min_num_surfaces(&self) -> usize {
90 // LOCAL PATCH (see /Cargo.toml [patch.crates-io]): a sequence that may
91 // synthesize film grain displays every grainy picture from a second
92 // surface (`VaapiPicture::set_display_frame`), so every one of the frames
93 // in flight -- the eight of the reference map, the one being decoded, the
94 // ones handed out but not yet read back -- can cost two. A sequence whose
95 // header says grain never happens is left at the count it always had.
96 match self.seq_header.film_grain_params_present {
97 true => 2 * NUM_SURFACES,
98 false => NUM_SURFACES,
99 }
100 }
101
102 fn coded_size(&self) -> Resolution {
103 Resolution::from((
104 self.seq_header.max_frame_width_minus_1 as u32 + 1,
105 self.seq_header.max_frame_height_minus_1 as u32 + 1,
106 ))
107 }
108
109 fn visible_rect(&self) -> Rect {
110 Rect::from(((0, 0), (self.render_width, self.render_height)))
111 }
112}
113
114impl From<&FrameHeaderObu> for libva::AV1FilmGrain {
115 fn from(hdr: &FrameHeaderObu) -> Self {
116 let fg = &hdr.film_grain_params;
117
118 let film_grain_fields = libva::AV1FilmGrainFields::new(
119 u32::from(fg.apply_grain),
120 u32::from(fg.chroma_scaling_from_luma),
121 u32::from(fg.grain_scaling_minus_8),
122 fg.ar_coeff_lag,
123 fg.ar_coeff_shift_minus_6 as u32,
124 fg.grain_scale_shift as u32,
125 u32::from(fg.overlap_flag),
126 u32::from(fg.clip_to_restricted_range),
127 );
128
129 const NUM_POINT_Y: usize = 14;
130 let fg_point_y_value = {
131 let mut fg_point_y_value = [0u8; NUM_POINT_Y];
132 fg_point_y_value.copy_from_slice(&fg.point_y_value[0..NUM_POINT_Y]);
133 fg_point_y_value
134 };
135 let fg_point_y_scaling = {
136 let mut fg_point_y_scaling = [0u8; NUM_POINT_Y];
137 fg_point_y_scaling.copy_from_slice(&fg.point_y_scaling[0..NUM_POINT_Y]);
138 fg_point_y_scaling
139 };
140
141 const NUM_POINT_CB: usize = 10;
142 let fg_point_cb_value = {
143 let mut fg_point_cb_value = [0u8; NUM_POINT_CB];
144 fg_point_cb_value.copy_from_slice(&fg.point_cb_value[0..NUM_POINT_CB]);
145 fg_point_cb_value
146 };
147 let fg_point_cb_scaling = {
148 let mut fg_point_cb_scaling = [0u8; NUM_POINT_CB];
149 fg_point_cb_scaling.copy_from_slice(&fg.point_cb_scaling[0..NUM_POINT_CB]);
150 fg_point_cb_scaling
151 };
152
153 const NUM_POINT_CR: usize = 10;
154 let fg_point_cr_value = {
155 let mut fg_point_cr_value = [0u8; NUM_POINT_CR];
156 fg_point_cr_value.copy_from_slice(&fg.point_cr_value[0..NUM_POINT_CR]);
157 fg_point_cr_value
158 };
159 let fg_point_cr_scaling = {
160 let mut fg_point_cr_scaling = [0u8; NUM_POINT_CR];
161 fg_point_cr_scaling.copy_from_slice(&fg.point_cr_scaling[0..NUM_POINT_CR]);
162 fg_point_cr_scaling
163 };
164
165 let fg_ar_coeffs_y = {
166 let mut fg_ar_coeffs_y = [0i8; 24];
167 fg_ar_coeffs_y
168 .iter_mut()
169 .zip(fg.ar_coeffs_y_plus_128.iter().copied())
170 .for_each(|(dest, src)| *dest = ((src as i16) - 128) as i8);
171 fg_ar_coeffs_y
172 };
173 let fg_ar_coeffs_cb = {
174 let mut fg_ar_coeffs_cb = [0i8; 25];
175 fg_ar_coeffs_cb
176 .iter_mut()
177 .zip(fg.ar_coeffs_cb_plus_128.iter().copied())
178 .for_each(|(dest, src)| *dest = ((src as i16) - 128) as i8);
179 fg_ar_coeffs_cb
180 };
181 let fg_ar_coeffs_cr = {
182 let mut fg_ar_coeffs_cr = [0i8; 25];
183 fg_ar_coeffs_cr
184 .iter_mut()
185 .zip(fg.ar_coeffs_cr_plus_128.iter().copied())
186 .for_each(|(dest, src)| *dest = ((src as i16) - 128) as i8);
187 fg_ar_coeffs_cr
188 };
189
190 libva::AV1FilmGrain::new(
191 &film_grain_fields,
192 fg.grain_seed,
193 fg.num_y_points,
194 fg_point_y_value,
195 fg_point_y_scaling,
196 fg.num_cb_points,
197 fg_point_cb_value,
198 fg_point_cb_scaling,
199 fg.num_cr_points,
200 fg_point_cr_value,
201 fg_point_cr_scaling,
202 fg_ar_coeffs_y,
203 fg_ar_coeffs_cb,
204 fg_ar_coeffs_cr,
205 fg.cb_mult,
206 fg.cb_luma_mult,
207 fg.cb_offset,
208 fg.cr_mult,
209 fg.cr_luma_mult,
210 fg.cr_offset,
211 )
212 }
213}
214
215fn build_wm_info(hdr: &FrameHeaderObu) -> [libva::AV1WarpedMotionParams; 7] {
216 let mut wm = vec![];
217 let gm = &hdr.global_motion_params;
218 for i in 1..=7 {
219 let wm_type = match gm.gm_type[i] {
220 /* TODO: these were not exported in cros-libva */
221 WarpModelType::Identity => 0,
222 WarpModelType::Translation => 1,
223 WarpModelType::RotZoom => 2,
224 WarpModelType::Affine => 3,
225 };
226
227 let params = {
228 let mut params = [0; 8];
229 params[0..6].copy_from_slice(&gm.gm_params[i][0..6]);
230 params
231 };
232
233 wm.push(libva::AV1WarpedMotionParams::new(wm_type, params, u8::from(!gm.warp_valid[i])));
234 }
235
236 match wm.try_into() {
237 Ok(wm) => wm,
238 Err(_) => unreachable!("The Vec should have the right size"),
239 }
240}
241
242fn build_pic_param<V: VideoFrame>(
243 hdr: &FrameHeaderObu,
244 stream_info: &StreamInfo,
245 current_frame: libva::VASurfaceID,
246 current_display_picture: libva::VASurfaceID,
247 reference_frames: &[Option<VADecodedHandle<V>>; NUM_REF_FRAMES],
248) -> anyhow::Result<libva::BufferType> {
249 let seq = stream_info.seq_header.clone();
250
251 let seq_info_fields = libva::AV1SeqFields::new(
252 u32::from(seq.still_picture),
253 u32::from(seq.use_128x128_superblock),
254 u32::from(seq.enable_filter_intra),
255 u32::from(seq.enable_intra_edge_filter),
256 u32::from(seq.enable_interintra_compound),
257 u32::from(seq.enable_masked_compound),
258 u32::from(seq.enable_dual_filter),
259 u32::from(seq.enable_order_hint),
260 u32::from(seq.enable_jnt_comp),
261 u32::from(seq.enable_cdef),
262 u32::from(seq.color_config.mono_chrome),
263 u32::from(seq.color_config.color_range),
264 u32::from(seq.color_config.subsampling_x),
265 u32::from(seq.color_config.subsampling_y),
266 seq.color_config.chroma_sample_position as u32,
267 u32::from(seq.film_grain_params_present),
268 );
269
270 let seg = &hdr.segmentation_params;
271 let seg_info_fields = libva::AV1SegmentInfoFields::new(
272 u32::from(seg.segmentation_enabled),
273 u32::from(seg.segmentation_update_map),
274 u32::from(seg.segmentation_temporal_update),
275 u32::from(seg.segmentation_update_data),
276 );
277
278 let seg_feature_mask = {
279 let mut seg_feature_mask = [0u8; MAX_SEGMENTS];
280 #[allow(clippy::needless_range_loop)]
281 for i in 0..MAX_SEGMENTS {
282 let mut mask = 0;
283 for j in 0..SEG_LVL_MAX {
284 if seg.feature_enabled[i][j] {
285 mask |= 1 << j;
286 }
287 }
288 seg_feature_mask[i] = mask;
289 }
290 seg_feature_mask
291 };
292
293 let seg_info =
294 libva::AV1Segmentation::new(&seg_info_fields, seg.feature_data, seg_feature_mask);
295
296 let pic_info_fields = libva::AV1PicInfoFields::new(
297 hdr.frame_type as u32,
298 u32::from(hdr.show_frame),
299 u32::from(hdr.showable_frame),
300 u32::from(hdr.error_resilient_mode),
301 u32::from(hdr.disable_cdf_update),
302 hdr.allow_screen_content_tools,
303 hdr.force_integer_mv,
304 u32::from(hdr.allow_intrabc),
305 u32::from(hdr.use_superres),
306 u32::from(hdr.allow_high_precision_mv),
307 u32::from(hdr.is_motion_mode_switchable),
308 u32::from(hdr.use_ref_frame_mvs),
309 u32::from(hdr.disable_frame_end_update_cdf),
310 u32::from(hdr.tile_info.uniform_tile_spacing_flag),
311 u32::from(hdr.allow_warped_motion),
312 0, /* large_scale_tile */
313 );
314
315 let bit_depth_idx = match seq.bit_depth {
316 BitDepth::Depth8 => 0,
317 BitDepth::Depth10 => 1,
318 BitDepth::Depth12 => 2,
319 };
320
321 let ref_frame_map: [libva::VASurfaceID; NUM_REF_FRAMES] =
322 reference_frames.iter().map(va_surface_id).collect::<Vec<_>>().try_into().unwrap();
323
324 let width_in_sbs_minus_1 = {
325 let mut width_in_sbs_minus_1 = [0; MAX_TILE_COLS - 1];
326 #[allow(clippy::needless_range_loop)]
327 for i in 0..width_in_sbs_minus_1.len() {
328 width_in_sbs_minus_1[i] = u16::try_from(hdr.tile_info.width_in_sbs_minus_1[i])
329 .context("Invalid width_in_sbs_minus_1")?;
330 }
331 width_in_sbs_minus_1
332 };
333
334 let height_in_sbs_minus_1 = {
335 let mut height_in_sbs_minus_1 = [0; MAX_TILE_ROWS - 1];
336 #[allow(clippy::needless_range_loop)]
337 for i in 0..height_in_sbs_minus_1.len() {
338 height_in_sbs_minus_1[i] = u16::try_from(hdr.tile_info.height_in_sbs_minus_1[i])
339 .context("Invalid height_in_sbs_minus_1")?;
340 }
341 height_in_sbs_minus_1
342 };
343
344 let lf = &hdr.loop_filter_params;
345 let filter_level = [lf.loop_filter_level[0], lf.loop_filter_level[1]];
346
347 let lf_fields = libva::AV1LoopFilterFields::new(
348 lf.loop_filter_sharpness,
349 u8::from(lf.loop_filter_delta_enabled),
350 u8::from(lf.loop_filter_delta_update),
351 );
352
353 let quant = &hdr.quantization_params;
354 let qmatrix_fields = libva::AV1QMatrixFields::new(
355 u16::from(quant.using_qmatrix),
356 u16::try_from(quant.qm_y).context("Invalid qm_y")?,
357 u16::try_from(quant.qm_u).context("Invalid qm_u")?,
358 u16::try_from(quant.qm_v).context("Invalid qm_v")?,
359 );
360
361 let mode_control_fields = libva::AV1ModeControlFields::new(
362 u32::from(quant.delta_q_present),
363 quant.delta_q_res,
364 u32::from(lf.delta_lf_present),
365 lf.delta_lf_res as u32,
366 lf.delta_lf_multi as u32,
367 hdr.tx_mode as u32,
368 u32::from(hdr.reference_select),
369 u32::from(hdr.reduced_tx_set),
370 u32::from(hdr.skip_mode_present),
371 );
372
373 let cdef = &hdr.cdef_params;
374 let (cdef_y_strengths, cdef_uv_strengths) = {
375 let num_cdef_strenghts = 1 << cdef.cdef_bits;
376 let mut cdef_y_strengths = [0; 8];
377 let mut cdef_uv_strengths = [0; 8];
378
379 #[allow(clippy::needless_range_loop)]
380 for i in 0..num_cdef_strenghts {
381 let mut sec_strength = cdef.cdef_y_sec_strength[i];
382 if sec_strength == 4 {
383 sec_strength -= 1;
384 }
385 cdef_y_strengths[i] =
386 u8::try_from(((cdef.cdef_y_pri_strength[i] & 0xf) << 2) | (sec_strength & 0x3))
387 .context("Failed to merge primary and secondary strengths")?;
388 }
389
390 #[allow(clippy::needless_range_loop)]
391 for i in 0..num_cdef_strenghts {
392 let mut sec_strength = cdef.cdef_uv_sec_strength[i];
393 if sec_strength == 4 {
394 sec_strength -= 1;
395 }
396 cdef_uv_strengths[i] =
397 u8::try_from(((cdef.cdef_uv_pri_strength[i] & 0xf) << 2) | (sec_strength & 0x3))
398 .context("Failed to merge primary and secondary strengths")?;
399 }
400
401 (cdef_y_strengths, cdef_uv_strengths)
402 };
403
404 let lr = &hdr.loop_restoration_params;
405 let loop_restoration_fields = libva::AV1LoopRestorationFields::new(
406 lr.frame_restoration_type[0] as u16,
407 lr.frame_restoration_type[1] as u16,
408 lr.frame_restoration_type[2] as u16,
409 u16::from(lr.lr_unit_shift),
410 u16::from(lr.lr_uv_shift),
411 );
412
413 let wm = build_wm_info(hdr);
414
415 let pic_param = libva::PictureParameterBufferAV1::new(
416 u8::try_from(seq.seq_profile as u32).context("Invalid profile")?,
417 u8::try_from(seq.order_hint_bits_minus_1).context("Invalid order hint bits")?,
418 bit_depth_idx,
419 u8::try_from(seq.color_config.matrix_coefficients as u32)
420 .context("Invalid matrix_coefficients")?,
421 &seq_info_fields,
422 current_frame,
423 current_display_picture,
424 vec![], /* anchor_frames_list */
425 u16::try_from(hdr.upscaled_width - 1).context("Invalid frame width")?,
426 u16::try_from(hdr.frame_height - 1).context("Invalid frame height")?,
427 0, /* output_frame_width_in_tiles_minus_1 */
428 0, /* output_frame_height_in_tiles_minus_1 */
429 ref_frame_map,
430 hdr.ref_frame_idx,
431 u8::try_from(hdr.primary_ref_frame).context("Invalid primary_ref_frame")?,
432 u8::try_from(hdr.order_hint).context("Invalid order_hint")?,
433 &seg_info,
434 &libva::AV1FilmGrain::from(hdr),
435 u8::try_from(hdr.tile_info.tile_cols).context("Invalid tile_cols")?,
436 u8::try_from(hdr.tile_info.tile_rows).context("Invalid tile_rows")?,
437 width_in_sbs_minus_1,
438 height_in_sbs_minus_1,
439 0, /* large-scale tile not supported */
440 u16::try_from(hdr.tile_info.context_update_tile_id)
441 .context("Invalid context_update_tile_id")?,
442 &pic_info_fields,
443 u8::try_from(hdr.superres_denom).context("Invalid superres_denom")?,
444 u8::try_from(hdr.interpolation_filter as u32).context("Invalid interpolation_filter")?,
445 filter_level,
446 lf.loop_filter_level[2],
447 lf.loop_filter_level[3],
448 &lf_fields,
449 lf.loop_filter_ref_deltas,
450 lf.loop_filter_mode_deltas,
451 u8::try_from(quant.base_q_idx).context("Invalid base_q_idx")?,
452 i8::try_from(quant.delta_q_y_dc).context("Invalid delta_q_y_dc")?,
453 i8::try_from(quant.delta_q_u_dc).context("Invalid delta_q_u_dc")?,
454 i8::try_from(quant.delta_q_u_ac).context("Invalid delta_q_u_ac")?,
455 i8::try_from(quant.delta_q_v_dc).context("Invalid delta_q_v_dc")?,
456 i8::try_from(quant.delta_q_v_ac).context("Invalid delta_q_v_ac")?,
457 &qmatrix_fields,
458 &mode_control_fields,
459 u8::try_from(hdr.cdef_params.cdef_damping - 3).context("Invalid cdef_damping")?,
460 u8::try_from(hdr.cdef_params.cdef_bits).context("Invalid cdef_bits")?,
461 cdef_y_strengths,
462 cdef_uv_strengths,
463 &loop_restoration_fields,
464 &wm,
465 );
466
467 Ok(libva::BufferType::PictureParameter(libva::PictureParameter::AV1(pic_param)))
468}
469
470fn build_slice_params_for_tg(tg: &TileGroupObu) -> anyhow::Result<libva::BufferType> {
471 let mut slice_params = libva::SliceParameterBufferAV1::new();
472
473 for tile in &tg.tiles {
474 /* all tiles must be submitted in the same slice parameter array */
475 slice_params.add_slice_parameter(
476 tile.tile_size,
477 tile.tile_offset,
478 0,
479 u16::try_from(tile.tile_row).context("Invalid tile_row")?,
480 u16::try_from(tile.tile_col).context("Invalid tile_col")?,
481 u16::try_from(tg.tg_start).context("Invalid tg_start")?,
482 u16::try_from(tg.tg_end).context("Invalid tg_end")?,
483 0,
484 0,
485 );
486 }
487
488 Ok(libva::BufferType::SliceParameter(libva::SliceParameter::AV1(slice_params)))
489}
490
491fn build_slice_data_for_tg(tg: TileGroupObu) -> libva::BufferType {
492 let TileGroupObu { obu, .. } = tg;
493 libva::BufferType::SliceData(Vec::from(obu.as_ref()))
494}
495
496impl<V: VideoFrame> StatelessDecoderBackendPicture<Av1> for VaapiBackend<V> {
497 type Picture = VaapiPicture<V>;
498}
499
500impl<V: VideoFrame> StatelessAV1DecoderBackend for VaapiBackend<V> {
501 fn change_stream_info(&mut self, stream_info: &StreamInfo) -> StatelessBackendResult<()> {
502 self.new_sequence(stream_info)
503 }
504
505 fn new_picture(
506 &mut self,
507 hdr: &FrameHeaderObu,
508 timestamp: u64,
509 alloc_cb: &mut dyn FnMut() -> Option<V>,
510 ) -> NewPictureResult<Self::Picture> {
511 let mut picture = VaapiPicture::new(
512 timestamp,
513 Rc::clone(&self.context),
514 alloc_cb().ok_or(NewPictureError::OutOfOutputBuffers)?,
515 );
516 // LOCAL PATCH (see /Cargo.toml [patch.crates-io]): a frame that asks for
517 // film grain is displayed from a second surface, so it costs two frames
518 // of the pool rather than one -- which is why `min_num_surfaces` above
519 // counts double for a sequence that may use grain. Failing the second
520 // allocation drops the first back into the pool with the picture, and the
521 // caller resubmits exactly as it does for the first.
522 if hdr.film_grain_params.apply_grain {
523 let frame = alloc_cb().ok_or(NewPictureError::OutOfOutputBuffers)?;
524 picture.set_display_frame(self.context.display(), frame);
525 }
526 Ok(picture)
527 }
528
529 fn begin_picture(
530 &mut self,
531 picture: &mut Self::Picture,
532 stream_info: &StreamInfo,
533 hdr: &FrameHeaderObu,
534 reference_frames: &[Option<Self::Handle>; NUM_REF_FRAMES],
535 ) -> StatelessBackendResult<()> {
536 let pic_param = build_pic_param(
537 hdr,
538 stream_info,
539 picture.surface().id(),
540 picture.display_surface_id(),
541 reference_frames,
542 )
543 .context("Failed to build picture parameter")?;
544 let pic_param = self
545 .context
546 .create_buffer(pic_param)
547 .context("Failed to create picture parameter buffer")?;
548 picture.add_buffer(pic_param);
549
550 Ok(())
551 }
552
553 fn decode_tile_group(
554 &mut self,
555 picture: &mut Self::Picture,
556 tile_group: TileGroupObu,
557 ) -> crate::decoder::stateless::StatelessBackendResult<()> {
558 let slice_params = build_slice_params_for_tg(&tile_group)?;
559 let slice_data = build_slice_data_for_tg(tile_group);
560
561 let context = &self.context;
562
563 let buffer = context
564 .create_buffer(slice_params)
565 .context("Failed to create slice parameter buffer")?;
566
567 picture.add_buffer(buffer);
568
569 let buffer =
570 context.create_buffer(slice_data).context("Failed to create slice data buffer")?;
571
572 picture.add_buffer(buffer);
573
574 Ok(())
575 }
576
577 fn submit_picture(&mut self, picture: Self::Picture) -> StatelessBackendResult<Self::Handle> {
578 self.process_picture::<Av1>(picture)
579 }
580}
581
582impl<V: VideoFrame> StatelessDecoder<Av1, VaapiBackend<V>> {
583 // Creates a new instance of the decoder using the VAAPI backend.
584 pub fn new_vaapi(
585 display: Rc<Display>,
586 blocking_mode: BlockingMode,
587 ) -> Result<Self, NewStatelessDecoderError> {
588 Self::new(VaapiBackend::new(display, true), blocking_mode)
589 }
590}
591
592#[cfg(test)]
593mod tests {
594 use libva::Display;
595
596 use crate::bitstream_utils::IvfIterator;
597 use crate::decoder::stateless::av1::Av1;
598 use crate::decoder::stateless::tests::test_decode_stream;
599 use crate::decoder::stateless::tests::TestStream;
600 use crate::decoder::stateless::StatelessDecoder;
601 use crate::decoder::BlockingMode;
602 use crate::utils::simple_playback_loop;
603 use crate::utils::simple_playback_loop_owned_frames;
604 use crate::DecodedFormat;
605
606 /// Run `test` using the vaapi decoder, in both blocking and non-blocking modes.
607 fn test_decoder_vaapi(
608 test: &TestStream,
609 output_format: DecodedFormat,
610 blocking_mode: BlockingMode,
611 ) {
612 let display = Display::open().unwrap();
613 let decoder = StatelessDecoder::<Av1, _>::new_vaapi::<()>(display, blocking_mode).unwrap();
614
615 test_decode_stream(
616 |d, s, f| {
617 simple_playback_loop(
618 d,
619 IvfIterator::new(s),
620 f,
621 &mut simple_playback_loop_owned_frames,
622 output_format,
623 blocking_mode,
624 )
625 },
626 decoder,
627 test,
628 true,
629 false,
630 );
631 }
632
633 #[test]
634 // Ignore this test by default as it requires libva-compatible hardware.
635 #[ignore]
636 fn test_25fps_av1_blocking() {
637 use crate::decoder::stateless::av1::tests::DECODE_TEST_25FPS;
638 test_decoder_vaapi(&DECODE_TEST_25FPS, DecodedFormat::NV12, BlockingMode::Blocking);
639 }
640
641 #[test]
642 // Ignore this test by default as it requires libva-compatible hardware.
643 #[ignore]
644 fn test_25fps_av1_non_blocking() {
645 use crate::decoder::stateless::av1::tests::DECODE_TEST_25FPS;
646 test_decoder_vaapi(&DECODE_TEST_25FPS, DecodedFormat::NV12, BlockingMode::NonBlocking);
647 }
648}