forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiB9b67700Tahinli3 days ago4021 commits

vpp.rs118 lines, 3.8 KiBHistoryRawDownload
1//! [`Vpp`] — a `VAProfileNone` / `VAEntrypointVideoProc` video-processing
2//! pipeline: GPU-resident surface conversion with no system-memory round trip.
3//!
4//! This is the same `Config`/`Context`/`Buffer`/`Picture` machinery decode and
5//! encode use: VPP is another entrypoint on the same display.
6//!
7//! There is no filter query. cros-libva does not wrap
8//! `vaQueryVideoProcFilters`, so a caller that wants a filter asks for it and
9//! handles the error from the pipeline call instead.
10
11use std::rc::Rc;
12
13use cros_libva::{
14 ProcColorProperties, ProcPipelineParameterBuffer, SurfaceMemoryDescriptor, VARectangle,
15};
16
17use crate::caps::{Entrypoint, Profile};
18use crate::config::{Config, ConfigAttrib};
19use crate::display::Display;
20use crate::error::{Result, va_ok};
21use crate::picture::{Context, buffer, create_context, submit};
22
23/// A video-processing pipeline bound to one display.
24pub struct Vpp {
25 context: Rc<Context>,
26}
27
28/// A rectangle as libva takes it: `(x, y, width, height)` in pixels.
29pub type Region = (u32, u32, u32, u32);
30
31fn rect(r: Region) -> VARectangle {
32 VARectangle {
33 x: r.0 as i16,
34 y: r.1 as i16,
35 width: r.2 as u16,
36 height: r.3 as u16,
37 }
38}
39
40impl Vpp {
41 /// Build a pipeline whose config accepts `rt_formats` (the OR of every
42 /// `VA_RT_FORMAT_*` a surface fed through it will use). The context is
43 /// sized `width` x `height`, which is informational: each submission
44 /// carries its own source and destination.
45 pub fn new(display: &Rc<Display>, rt_formats: u32, width: u32, height: u32) -> Result<Vpp> {
46 let config = Config::new(
47 display,
48 Profile::None,
49 Entrypoint::VideoProc,
50 &[ConfigAttrib::rt_format(rt_formats)],
51 )?;
52 let context = create_context::<()>(display, &config, width, height, None, true)?;
53 Ok(Vpp { context })
54 }
55
56 /// Convert `source` into `dest`: one pipeline-parameter buffer, one
57 /// `begin`/`render`/`end` submission, whole-surface, driver-chosen colour
58 /// handling. Returns `dest`, synced and ready to read or hand to an encoder.
59 pub fn convert<D, E>(
60 &self,
61 source: &cros_libva::Surface<D>,
62 dest: Rc<cros_libva::Surface<E>>,
63 ) -> Result<Rc<cros_libva::Surface<E>>>
64 where
65 D: SurfaceMemoryDescriptor,
66 E: SurfaceMemoryDescriptor,
67 {
68 self.convert_region(source, (0, 0, 0, 0), dest, (0, 0, 0, 0))
69 }
70
71 /// Convert `source_region` of `source` into `dest_region` of `dest`,
72 /// scaling when the regions differ in size and leaving the rest of `dest`
73 /// as it was. A zero-sized region means "the whole surface".
74 pub fn convert_region<D, E>(
75 &self,
76 source: &cros_libva::Surface<D>,
77 source_region: Region,
78 dest: Rc<cros_libva::Surface<E>>,
79 dest_region: Region,
80 ) -> Result<Rc<cros_libva::Surface<E>>>
81 where
82 D: SurfaceMemoryDescriptor,
83 E: SurfaceMemoryDescriptor,
84 {
85 let region = |r: Region| (r.2 != 0 && r.3 != 0).then(|| rect(r));
86 let param = ProcPipelineParameterBuffer::new(
87 source.id(),
88 region(source_region),
89 0,
90 region(dest_region),
91 0,
92 0,
93 0,
94 0,
95 None,
96 None,
97 None,
98 0,
99 None,
100 0,
101 None,
102 0,
103 0,
104 ProcColorProperties::default(),
105 ProcColorProperties::default(),
106 0,
107 None,
108 );
109 let buf = buffer(
110 &self.context,
111 cros_libva::BufferType::ProcPipelineParameter(param),
112 )?;
113 let synced = submit(&self.context, Rc::clone(&dest), vec![buf])?;
114 drop(synced);
115 va_ok("vaSyncSurface", dest.sync())?;
116 Ok(dest)
117 }
118}