forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiBa5441e8Tahinli3 months ago1014 commits

vpp.rs153 lines, 6.3 KiBHistoryRawDownload
1//! [`Vpp`] — a `VAProfileNone` / `VAEntrypointVideoProc` video-processing
2//! pipeline: GPU-resident surface conversion (format, and any filter the
3//! driver advertises) with no system-memory round trip.
4//!
5//! This is the same `Config`/`Context`/`Buffer`/`Picture` machinery decode and
6//! encode use — VPP is just another entrypoint on the same display, and
7//! `Picture`'s typestate protocol applies unchanged.
8
9use std::sync::Arc;
10
11use crate::caps::{Entrypoint, Profile};
12use crate::config::{Config, ConfigAttrib};
13use crate::display::Display;
14use crate::error::Result;
15use crate::picture::{Buffer, Context, Picture};
16use crate::surface::Surface;
17use crate::sys;
18
19/// A video-processing pipeline bound to one display.
20pub struct Vpp {
21 context: Arc<Context>,
22}
23
24impl Vpp {
25 /// Build a pipeline whose config accepts `rt_formats` (the OR of every
26 /// `VA_RT_FORMAT_*` a surface fed through it will use, source or
27 /// destination — one config covers both ends of a conversion) and whose
28 /// context targets `targets` (every surface this pipeline will render
29 /// into; VA-API contexts, like decode/encode ones, are created with the
30 /// full set of render targets up front).
31 pub fn new(display: &Arc<Display>, rt_formats: u32, targets: &[Arc<Surface>]) -> Result<Vpp> {
32 let config = Config::new(
33 display,
34 Profile::None,
35 Entrypoint::VideoProc,
36 &[ConfigAttrib::rt_format(rt_formats)],
37 )?;
38 // The size a VPP context is created with is informational only — each
39 // submission carries its own source and destination surfaces, which
40 // may be smaller or larger — so the first target's size is as good a
41 // hint as any.
42 let (width, height) = targets
43 .first()
44 .map(|s| s.size())
45 .unwrap_or((1, 1));
46 let context = Context::new(&config, width, height, 0, targets)?;
47 Ok(Vpp { context })
48 }
49
50 /// `VAProcFilterType` values this pipeline's driver advertises
51 /// (`vaQueryVideoProcFilters`), e.g. to check for
52 /// `VAProcFilterHighDynamicRangeToneMapping` before asking for it.
53 pub fn filters(&self) -> Result<Vec<i32>> {
54 let mut filters = vec![0i32; sys::VAProcFilterCount as usize];
55 let mut count = filters.len() as u32;
56 // SAFETY: `filters` has room for exactly `count` entries and `count`
57 // is passed alongside; libva writes only within that bound and
58 // rewrites `count` to the number actually filled in (va_vpp.h:1518).
59 let status = unsafe {
60 sys::vaQueryVideoProcFilters(
61 self.context.display().handle(),
62 self.context_id(),
63 filters.as_mut_ptr(),
64 &mut count,
65 )
66 };
67 crate::error::check("vaQueryVideoProcFilters", status)?;
68 filters.truncate(count as usize);
69 Ok(filters)
70 }
71
72 /// Convert `source` into `dest`: one pipeline-parameter buffer, one
73 /// `begin`/`render`/`end`/`sync` submission, whole-surface, no filters,
74 /// driver-chosen colour handling — the plain format conversion this
75 /// crate exists for. Returns `dest` back, synced and ready to read or
76 /// hand to an encoder.
77 pub fn convert(&self, source: &Arc<Surface>, dest: Arc<Surface>) -> Result<Arc<Surface>> {
78 let param = sys::VAProcPipelineParameterBuffer {
79 surface: source.id(),
80 ..Default::default()
81 };
82 // SAFETY: `VAProcPipelineParameterBuffer` is the exact repr(C)
83 // parameter struct for `VAProcPipelineParameterBufferType`
84 // (va_vpp.h:886); it has no padding-sensitive invariants beyond the
85 // zeroed reserved tail `Default` already establishes.
86 let buffer = unsafe {
87 Buffer::from_param(&self.context, sys::VAProcPipelineParameterBufferType, &param)?
88 };
89 let surface = Picture::new(&self.context, dest)
90 .begin()?
91 .render(buffer)?
92 .end()?
93 .sync()?
94 .into_surface();
95 Ok(surface)
96 }
97
98 /// Convert `source` into `dest` like [`Vpp::convert`], but reading only
99 /// `source_region` of `source` and writing only `dest_region` of `dest` —
100 /// no scaling (both regions are the same size in every caller today),
101 /// just a placement: the pixels outside `dest_region` are left exactly as
102 /// `dest` already had them. This is how a visible picture smaller than
103 /// its block-aligned coded surface (e.g. a 2160-tall HEVC source, whose
104 /// coded height rounds to 2176) lands inside a differently-padded
105 /// destination without a resize.
106 pub fn convert_region(
107 &self,
108 source: &Arc<Surface>,
109 source_region: (u32, u32, u32, u32),
110 dest: Arc<Surface>,
111 dest_region: (u32, u32, u32, u32),
112 ) -> Result<Arc<Surface>> {
113 let sr = sys::VARectangle {
114 x: source_region.0 as i16,
115 y: source_region.1 as i16,
116 width: source_region.2 as u16,
117 height: source_region.3 as u16,
118 };
119 let dr = sys::VARectangle {
120 x: dest_region.0 as i16,
121 y: dest_region.1 as i16,
122 width: dest_region.2 as u16,
123 height: dest_region.3 as u16,
124 };
125 let param = sys::VAProcPipelineParameterBuffer {
126 surface: source.id(),
127 surface_region: &sr,
128 output_region: &dr,
129 ..Default::default()
130 };
131 // SAFETY: `surface_region`/`output_region` point at `sr`/`dr`, which
132 // outlive this call (built here, read only while `Buffer::from_param`
133 // constructs the driver buffer below); otherwise identical to
134 // `convert`'s safety argument.
135 let buffer = unsafe {
136 Buffer::from_param(&self.context, sys::VAProcPipelineParameterBufferType, &param)?
137 };
138 let surface = Picture::new(&self.context, dest)
139 .begin()?
140 .render(buffer)?
141 .end()?
142 .sync()?
143 .into_surface();
144 Ok(surface)
145 }
146
147 fn context_id(&self) -> sys::VAContextID {
148 // `Context` does not expose its raw id outside the crate; VPP needs
149 // it for `vaQueryVideoProcFilters`, which has no typestate-protected
150 // equivalent to route through `Picture`.
151 self.context.raw_id()
152 }
153}