Skip to main content

threers_codec/vaapi/
mod.rs

1//! VAAPI: hardware decoding and encoding on Linux GPUs with a VA driver —
2//! AMD (Mesa `radeonsi`), Intel (`iHD`, `i965`) and others.
3//!
4//! [`VaapiDecoder`] decodes H.264, HEVC, VP9 and AV1; [`VaapiEncoder`]
5//! encodes HEVC to MP4.
6//!
7//! `libva` and `libva-drm` are opened at run time, not linked, so a binary
8//! built with the `vaapi` feature runs unchanged where there is no driver,
9//! and [`available`] says so. Struct layouts are those of libva 2.x (VA-API
10//! 1.x), checked against the C compiler's.
11//!
12//! VAAPI does not parse the bitstream: the caller supplies every picture's
13//! parameters, reference frames and slice headers. So the native decoders do
14//! that part — parameter sets, reference marking and lists, output order —
15//! and hand each slice to the GPU through their accelerator hooks
16//! ([`H264Accel`](crate::decode::h264::H264Accel) and its HEVC, VP9 and AV1
17//! counterparts). The pictures that come back are the native decoder's,
18//! sample for sample; only who computes them changes.
19//!
20//! [`decode::Decoder`](crate::decode::Decoder) uses this when it is available;
21//! [`VaapiDecoder`] is the backend on its own.
22
23use std::ffi::{c_char, c_int, c_uint, c_void, CStr, CString};
24use std::sync::OnceLock;
25
26mod av1;
27mod av1_layout;
28mod enc_layout;
29mod encode;
30mod h264;
31mod hevc;
32mod vp9;
33
34pub use encode::{VaapiEncoder, VaapiRate};
35
36use crate::decode::{ChromaFormat, ColorInfo, DecodedFrame, PackedFrame, Picture};
37use crate::mp4_demux::{TrackCodec, VideoTrack};
38
39pub(crate) type Display = *mut c_void;
40pub(crate) type Status = c_int;
41pub(crate) type Id = c_uint;
42
43pub(crate) const INVALID_ID: Id = 0xffff_ffff;
44pub(crate) const PROFILE_H264_HIGH: c_int = 7;
45pub(crate) const PROFILE_HEVC_MAIN: c_int = 17;
46pub(crate) const PROFILE_HEVC_MAIN10: c_int = 18;
47pub(crate) const ENTRYPOINT_VLD: c_int = 1;
48pub(crate) const ENTRYPOINT_ENC_SLICE: c_int = 6;
49/// Intel's low-power (VDEnc) encoder.
50pub(crate) const ENTRYPOINT_ENC_SLICE_LP: c_int = 8;
51pub(crate) const ATTRIB_RT_FORMAT: c_int = 0;
52pub(crate) const RT_FORMAT_YUV420: c_uint = 1;
53pub(crate) const RT_FORMAT_YUV420_10: c_uint = 0x100;
54pub(crate) const BUF_PICTURE_PARAMETER: c_int = 0;
55pub(crate) const BUF_IQ_MATRIX: c_int = 1;
56pub(crate) const BUF_SLICE_PARAMETER: c_int = 4;
57pub(crate) const BUF_SLICE_DATA: c_int = 5;
58pub(crate) const PROGRESSIVE: c_int = 1;
59pub(crate) const FOURCC_NV12: u32 = 0x3231_564e;
60const FOURCC_P010: u32 = 0x3031_3050;
61
62/// `VAConfigAttrib`.
63#[repr(C)]
64pub(crate) struct ConfigAttrib {
65    pub kind: c_int,
66    pub value: c_uint,
67}
68
69/// `VAImageFormat`.
70#[repr(C)]
71#[derive(Clone, Copy)]
72pub(crate) struct ImageFormat {
73    pub(crate) fourcc: u32,
74    pub(crate) byte_order: u32,
75    pub(crate) bits_per_pixel: u32,
76    pub(crate) depth: u32,
77    pub(crate) masks: [u32; 4],
78    pub(crate) reserved: [u32; 4],
79}
80const _: () = assert!(std::mem::size_of::<ImageFormat>() == 48);
81
82/// `VAImage`.
83#[repr(C)]
84pub(crate) struct Image {
85    pub(crate) image_id: Id,
86    pub(crate) format: ImageFormat,
87    pub(crate) buf: Id,
88    pub(crate) width: u16,
89    pub(crate) height: u16,
90    pub(crate) data_size: u32,
91    pub(crate) num_planes: u32,
92    pub(crate) pitches: [u32; 3],
93    pub(crate) offsets: [u32; 3],
94    pub(crate) num_palette_entries: i32,
95    pub(crate) entry_bytes: i32,
96    pub(crate) component_order: [i8; 4],
97    pub(crate) reserved: [u32; 4],
98}
99const _: () = assert!(std::mem::size_of::<Image>() == 120);
100
101/// The libva entry points used here.
102pub(crate) struct Va {
103    _lib: Lib,
104    _drm: Lib,
105    get_display_drm: unsafe extern "C" fn(c_int) -> Display,
106    initialize: unsafe extern "C" fn(Display, *mut c_int, *mut c_int) -> Status,
107    terminate: unsafe extern "C" fn(Display) -> Status,
108    error_str: unsafe extern "C" fn(Status) -> *const c_char,
109    set_info_callback:
110        Option<unsafe extern "C" fn(Display, *mut c_void, *mut c_void) -> *mut c_void>,
111    pub create_config:
112        unsafe extern "C" fn(Display, c_int, c_int, *mut ConfigAttrib, c_int, *mut Id) -> Status,
113    pub get_config_attributes:
114        unsafe extern "C" fn(Display, c_int, c_int, *mut ConfigAttrib, c_int) -> Status,
115    pub destroy_config: unsafe extern "C" fn(Display, Id) -> Status,
116    pub create_surfaces: unsafe extern "C" fn(
117        Display,
118        c_uint,
119        c_uint,
120        c_uint,
121        *mut Id,
122        c_uint,
123        *mut c_void,
124        c_uint,
125    ) -> Status,
126    pub destroy_surfaces: unsafe extern "C" fn(Display, *mut Id, c_int) -> Status,
127    pub create_context:
128        unsafe extern "C" fn(Display, Id, c_int, c_int, c_int, *mut Id, c_int, *mut Id) -> Status,
129    pub destroy_context: unsafe extern "C" fn(Display, Id) -> Status,
130    pub create_buffer:
131        unsafe extern "C" fn(Display, Id, c_int, c_uint, c_uint, *mut c_void, *mut Id) -> Status,
132    pub destroy_buffer: unsafe extern "C" fn(Display, Id) -> Status,
133    pub begin_picture: unsafe extern "C" fn(Display, Id, Id) -> Status,
134    pub render_picture: unsafe extern "C" fn(Display, Id, *mut Id, c_int) -> Status,
135    pub end_picture: unsafe extern "C" fn(Display, Id) -> Status,
136    pub sync_surface: unsafe extern "C" fn(Display, Id) -> Status,
137    derive_image: unsafe extern "C" fn(Display, Id, *mut Image) -> Status,
138    pub create_image:
139        unsafe extern "C" fn(Display, *mut ImageFormat, c_int, c_int, *mut Image) -> Status,
140    get_image: unsafe extern "C" fn(Display, Id, c_int, c_int, c_uint, c_uint, Id) -> Status,
141    pub put_image: unsafe extern "C" fn(
142        Display,
143        Id,
144        Id,
145        c_int,
146        c_int,
147        c_uint,
148        c_uint,
149        c_int,
150        c_int,
151        c_uint,
152        c_uint,
153    ) -> Status,
154    pub destroy_image: unsafe extern "C" fn(Display, Id) -> Status,
155    pub map_buffer: unsafe extern "C" fn(Display, Id, *mut *mut c_void) -> Status,
156    pub unmap_buffer: unsafe extern "C" fn(Display, Id) -> Status,
157}
158
159/// A shared library opened at run time.
160pub(crate) struct Lib(*mut c_void);
161unsafe impl Send for Lib {}
162unsafe impl Sync for Lib {}
163
164#[link(name = "dl")]
165unsafe extern "C" {
166    fn dlopen(filename: *const c_char, flag: c_int) -> *mut c_void;
167    fn dlsym(handle: *mut c_void, symbol: *const c_char) -> *mut c_void;
168}
169
170impl Lib {
171    fn open(names: &[&str]) -> Option<Lib> {
172        names.iter().find_map(|n| {
173            let c = CString::new(*n).ok()?;
174            // RTLD_NOW
175            let h = unsafe { dlopen(c.as_ptr(), 2) };
176            (!h.is_null()).then_some(Lib(h))
177        })
178    }
179
180    /// # Safety
181    /// `T` must be the symbol's true signature.
182    unsafe fn sym<T: Copy>(&self, name: &str) -> Option<T> {
183        let c = CString::new(name).ok()?;
184        let p = unsafe { dlsym(self.0, c.as_ptr()) };
185        (!p.is_null()).then(|| unsafe { std::mem::transmute_copy(&p) })
186    }
187}
188
189impl Va {
190    fn load() -> Option<Va> {
191        let lib = Lib::open(&["libva.so.2", "libva.so"])?;
192        let drm = Lib::open(&["libva-drm.so.2", "libva-drm.so"])?;
193        unsafe {
194            Some(Va {
195                get_display_drm: drm.sym("vaGetDisplayDRM")?,
196                initialize: lib.sym("vaInitialize")?,
197                terminate: lib.sym("vaTerminate")?,
198                error_str: lib.sym("vaErrorStr")?,
199                set_info_callback: lib.sym("vaSetInfoCallback"),
200                create_config: lib.sym("vaCreateConfig")?,
201                get_config_attributes: lib.sym("vaGetConfigAttributes")?,
202                destroy_config: lib.sym("vaDestroyConfig")?,
203                create_surfaces: lib.sym("vaCreateSurfaces")?,
204                destroy_surfaces: lib.sym("vaDestroySurfaces")?,
205                create_context: lib.sym("vaCreateContext")?,
206                destroy_context: lib.sym("vaDestroyContext")?,
207                create_buffer: lib.sym("vaCreateBuffer")?,
208                destroy_buffer: lib.sym("vaDestroyBuffer")?,
209                begin_picture: lib.sym("vaBeginPicture")?,
210                render_picture: lib.sym("vaRenderPicture")?,
211                end_picture: lib.sym("vaEndPicture")?,
212                sync_surface: lib.sym("vaSyncSurface")?,
213                derive_image: lib.sym("vaDeriveImage")?,
214                create_image: lib.sym("vaCreateImage")?,
215                get_image: lib.sym("vaGetImage")?,
216                put_image: lib.sym("vaPutImage")?,
217                destroy_image: lib.sym("vaDestroyImage")?,
218                map_buffer: lib.sym("vaMapBuffer")?,
219                unmap_buffer: lib.sym("vaUnmapBuffer")?,
220                _lib: lib,
221                _drm: drm,
222            })
223        }
224    }
225
226    pub(crate) fn check(&self, what: &str, s: Status) -> Result<(), String> {
227        if s == 0 {
228            return Ok(());
229        }
230        let msg = unsafe {
231            CStr::from_ptr((self.error_str)(s))
232                .to_string_lossy()
233                .into_owned()
234        };
235        Err(format!("{what} failed: {msg} (VA status {s})"))
236    }
237}
238
239/// libva, loaded once, or `None` where it is not installed.
240fn va() -> Option<&'static Va> {
241    static VA: OnceLock<Option<Va>> = OnceLock::new();
242    VA.get_or_init(Va::load).as_ref()
243}
244
245/// An initialised VA display on a DRM render node, terminated on drop.
246pub(crate) struct Device {
247    pub va: &'static Va,
248    pub dpy: Display,
249    _node: std::fs::File,
250    /// The image frames are copied into for reading, with its size and
251    /// format: created once, not per frame (each is megabytes of GPU memory
252    /// and a round trip into the kernel).
253    readback: std::cell::Cell<Option<(Image, usize, usize, u32)>>,
254    /// Where [`read_frame`](Self::read_frame) leaves pictures as the
255    /// hardware made them (NV12, P010) when set, handing the decoder an
256    /// empty stand-in: see [`VaapiDecoder::take_picture`].
257    pub(crate) outbox: Option<std::sync::Arc<Outbox>>,
258}
259
260/// Packed pictures read back during one [`VaapiDecoder::push`], in the order
261/// they were read, and the pool their buffers come from.
262#[derive(Default)]
263pub(crate) struct Outbox {
264    frames: std::sync::Mutex<Vec<PackedFrame>>,
265    pool: std::sync::Mutex<crate::bytes::FramePool>,
266}
267
268impl Device {
269    /// The first render node whose driver initialises and decodes `profile`.
270    pub(crate) fn open(profile: c_int) -> Result<Device, String> {
271        Self::open_for(profile, ENTRYPOINT_VLD)
272    }
273
274    /// The first render node whose driver has `entrypoint` for `profile`.
275    pub(crate) fn open_for(profile: c_int, entrypoint: c_int) -> Result<Device, String> {
276        let va = va().ok_or("libva is not installed")?;
277        let mut last = String::from("no DRM render node (/dev/dri/renderD*)");
278        for n in 128..136 {
279            let path = format!("/dev/dri/renderD{n}");
280            let Ok(node) = std::fs::OpenOptions::new()
281                .read(true)
282                .write(true)
283                .open(&path)
284            else {
285                continue;
286            };
287            use std::os::fd::AsRawFd;
288            let dpy = unsafe { (va.get_display_drm)(node.as_raw_fd()) };
289            if dpy.is_null() {
290                continue;
291            }
292            // libva prints which driver it loads to stderr unless told not to;
293            // a library has no business writing there.
294            if let Some(f) = va.set_info_callback {
295                unsafe { f(dpy, std::ptr::null_mut(), std::ptr::null_mut()) };
296            }
297            let (mut major, mut minor) = (0, 0);
298            if let Err(e) = va.check(&format!("vaInitialize on {path}"), unsafe {
299                (va.initialize)(dpy, &mut major, &mut minor)
300            }) {
301                last = e;
302                continue;
303            }
304            let dev = Device {
305                va,
306                dpy,
307                _node: node,
308                readback: std::cell::Cell::new(None),
309                outbox: None,
310            };
311            // Does this driver decode the profile at all?
312            let mut attrib = ConfigAttrib {
313                kind: ATTRIB_RT_FORMAT,
314                value: RT_FORMAT_YUV420,
315            };
316            let mut cfg = 0;
317            match va.check("vaCreateConfig", unsafe {
318                (va.create_config)(dpy, profile, entrypoint, &mut attrib, 1, &mut cfg)
319            }) {
320                Ok(()) => {
321                    unsafe { (va.destroy_config)(dpy, cfg) };
322                    return Ok(dev);
323                }
324                Err(e) => last = format!("{path}: {e}"),
325            }
326        }
327        Err(last)
328    }
329
330    /// `vaCreateImage` + `vaGetImage` into `img`; false (and nothing held)
331    /// when the driver cannot.
332    unsafe fn get_image(
333        &self,
334        surface: Id,
335        w: usize,
336        h: usize,
337        fourcc: u32,
338        deep: bool,
339        img: &mut Image,
340    ) -> bool {
341        let va = self.va;
342        match self.readback.take() {
343            Some((cached, cw, ch, cf)) if (cw, ch, cf) == (w, h, fourcc) => {
344                if unsafe {
345                    (va.get_image)(
346                        self.dpy,
347                        surface,
348                        0,
349                        0,
350                        w as c_uint,
351                        h as c_uint,
352                        cached.image_id,
353                    )
354                } == 0
355                {
356                    *img = cached;
357                    return true;
358                }
359                unsafe { (va.destroy_image)(self.dpy, cached.image_id) };
360            }
361            Some((stale, ..)) => {
362                unsafe { (va.destroy_image)(self.dpy, stale.image_id) };
363            }
364            None => {}
365        }
366        let mut fmt: ImageFormat = unsafe { std::mem::zeroed() };
367        fmt.fourcc = fourcc;
368        fmt.byte_order = 1;
369        fmt.bits_per_pixel = if deep { 24 } else { 12 };
370        *img = unsafe { std::mem::zeroed() };
371        if unsafe { (va.create_image)(self.dpy, &mut fmt, w as c_int, h as c_int, img) } != 0 {
372            return false;
373        }
374        if unsafe {
375            (va.get_image)(
376                self.dpy,
377                surface,
378                0,
379                0,
380                w as c_uint,
381                h as c_uint,
382                img.image_id,
383            )
384        } != 0
385        {
386            unsafe { (va.destroy_image)(self.dpy, img.image_id) };
387            *img = unsafe { std::mem::zeroed() };
388            return false;
389        }
390        true
391    }
392
393    /// The decoded picture in `surface` (`w × h` coded, NV12, or P010 when
394    /// `deep`), cropped by `crop` (`[left, right, top, bottom]`, luma
395    /// samples) straight out of the mapped image, after waiting for it.
396    pub(crate) fn read_frame(
397        &self,
398        surface: Id,
399        w: usize,
400        h: usize,
401        deep: bool,
402        crop: [usize; 4],
403        color: ColorInfo,
404    ) -> Result<DecodedFrame, String> {
405        let fourcc = if deep { FOURCC_P010 } else { FOURCC_NV12 };
406        let va = self.va;
407        unsafe {
408            va.check("vaSyncSurface", (va.sync_surface)(self.dpy, surface))?;
409            // Copy the surface into an image first: mapping the surface itself
410            // (vaDeriveImage) reads uncached video memory, which on Mesa costs
411            // more than decoding the frame did. Derive only when the copy fails.
412            let mut img: Image = std::mem::zeroed();
413            let copied = self.get_image(surface, w, h, fourcc, deep, &mut img);
414            if !copied {
415                let derived = (va.derive_image)(self.dpy, surface, &mut img) == 0
416                    && img.format.fourcc == fourcc;
417                if !derived {
418                    if img.image_id != 0 && img.image_id != INVALID_ID {
419                        (va.destroy_image)(self.dpy, img.image_id);
420                    }
421                    return Err("the driver neither copies nor exposes the decoded surface".into());
422                }
423            }
424            let mut p: *mut c_void = std::ptr::null_mut();
425            let r = va.check("vaMapBuffer", (va.map_buffer)(self.dpy, img.buf, &mut p));
426            if let Err(e) = r {
427                (va.destroy_image)(self.dpy, img.image_id);
428                return Err(e);
429            }
430            let data = std::slice::from_raw_parts(p as *const u8, img.data_size as usize);
431            let (yp, up) = (img.pitches[0] as usize, img.pitches[1] as usize);
432            let (yo, uo) = (img.offsets[0] as usize, img.offsets[1] as usize);
433            let [left, right, top, bottom] = crop;
434            let (fw, fh) = (
435                w.saturating_sub(left + right),
436                h.saturating_sub(top + bottom),
437            );
438            let (cw, ch) = (fw.div_ceil(2), fh.div_ceil(2));
439            let (cx, cy) = (left / 2, top / 2);
440            let bps = if deep { 2 } else { 1 };
441            if let Some(outbox) = &self.outbox {
442                // As the hardware made it: rows copied, nothing converted.
443                // The GPU reads NV12 and P010 as they are.
444                let pool = outbox.pool.lock().unwrap().clone();
445                let mut py = pool.take(fw * fh * bps);
446                for r in 0..fh {
447                    py.extend_from_slice(&data[yo + (top + r) * yp + left * bps..][..fw * bps]);
448                }
449                let mut uv = pool.take(cw * ch * 2 * bps);
450                for r in 0..ch {
451                    uv.extend_from_slice(
452                        &data[uo + (cy + r) * up + cx * 2 * bps..][..cw * 2 * bps],
453                    );
454                }
455                (va.unmap_buffer)(self.dpy, img.buf);
456                if copied {
457                    self.readback.set(Some((img, w, h, fourcc)));
458                } else {
459                    (va.destroy_image)(self.dpy, img.image_id);
460                }
461                outbox.frames.lock().unwrap().push(PackedFrame {
462                    width: fw as u32,
463                    height: fh as u32,
464                    bit_depth: if deep { 10 } else { 8 },
465                    y: py,
466                    uv,
467                    color,
468                });
469                // The stand-in: the picture's size and format, no samples.
470                return Ok(DecodedFrame {
471                    width: fw as u32,
472                    height: fh as u32,
473                    bit_depth: if deep { 10 } else { 8 },
474                    chroma: ChromaFormat::Yuv420,
475                    y: Vec::new(),
476                    u: Vec::new(),
477                    v: Vec::new(),
478                    color,
479                });
480            }
481            // Straight loops per format, so they vectorise: this copy runs
482            // once per frame over the whole picture.
483            let mut y = vec![0u16; fw * fh];
484            let mut u = vec![0u16; cw * ch];
485            let mut v = vec![0u16; cw * ch];
486            for r in 0..fh {
487                let (src, dst) = (
488                    &data[yo + (top + r) * yp + left * bps..][..fw * bps],
489                    &mut y[r * fw..][..fw],
490                );
491                if deep {
492                    // P010: 10-bit samples in the high bits of little-endian u16s.
493                    for (d, s) in dst.iter_mut().zip(src.as_chunks::<2>().0) {
494                        *d = u16::from_le_bytes([s[0], s[1]]) >> 6;
495                    }
496                } else {
497                    for (d, &s) in dst.iter_mut().zip(src) {
498                        *d = s as u16;
499                    }
500                }
501            }
502            for r in 0..ch {
503                let src = &data[uo + (cy + r) * up + cx * 2 * bps..][..cw * 2 * bps];
504                let (du, dv) = (&mut u[r * cw..][..cw], &mut v[r * cw..][..cw]);
505                if deep {
506                    for ((a, b), s) in du.iter_mut().zip(dv.iter_mut()).zip(src.as_chunks::<4>().0)
507                    {
508                        *a = u16::from_le_bytes([s[0], s[1]]) >> 6;
509                        *b = u16::from_le_bytes([s[2], s[3]]) >> 6;
510                    }
511                } else {
512                    for ((a, b), s) in du.iter_mut().zip(dv.iter_mut()).zip(src.as_chunks::<2>().0)
513                    {
514                        *a = s[0] as u16;
515                        *b = s[1] as u16;
516                    }
517                }
518            }
519            let frame = DecodedFrame {
520                width: fw as u32,
521                height: fh as u32,
522                bit_depth: if deep { 10 } else { 8 },
523                chroma: ChromaFormat::Yuv420,
524                y,
525                u,
526                v,
527                color,
528            };
529            (va.unmap_buffer)(self.dpy, img.buf);
530            if copied {
531                self.readback.set(Some((img, w, h, fourcc)));
532            } else {
533                (va.destroy_image)(self.dpy, img.image_id);
534            }
535            Ok(frame)
536        }
537    }
538}
539
540impl Drop for Device {
541    fn drop(&mut self) {
542        if let Some((img, ..)) = self.readback.take() {
543            unsafe { (self.va.destroy_image)(self.dpy, img.image_id) };
544        }
545        unsafe { (self.va.terminate)(self.dpy) };
546    }
547}
548
549/// Whether this machine has a VA driver that encodes HEVC.
550pub fn encode_available() -> bool {
551    static AVAILABLE: OnceLock<bool> = OnceLock::new();
552    *AVAILABLE.get_or_init(|| encode::open_device().is_ok())
553}
554
555/// Whether this machine has a VA driver that decodes H.264.
556pub fn available() -> bool {
557    static AVAILABLE: OnceLock<bool> = OnceLock::new();
558    *AVAILABLE.get_or_init(|| Device::open(PROFILE_H264_HIGH).is_ok())
559}
560
561/// A VA decoding context: config, surfaces and which picture each surface
562/// holds (`owner`, a key the codec chooses: a frame store id, a POC).
563pub(crate) struct Session {
564    pub config: Id,
565    pub context: Id,
566    surfaces: Vec<Id>,
567    owner: Vec<Option<i64>>,
568    /// Coded size in samples, profile and render-target format.
569    pub key: (u32, u32, c_int, c_uint),
570    /// Buffers of the picture being decoded, destroyed after `vaEndPicture`.
571    buffers: Vec<Id>,
572}
573
574impl Session {
575    pub(crate) fn new(
576        dev: &Device,
577        profile: c_int,
578        rt: c_uint,
579        w: u32,
580        h: u32,
581        n: usize,
582    ) -> Result<Session, String> {
583        let (va, dpy) = (dev.va, dev.dpy);
584        unsafe {
585            let mut attrib = ConfigAttrib {
586                kind: ATTRIB_RT_FORMAT,
587                value: rt,
588            };
589            let mut config = 0;
590            va.check(
591                "vaCreateConfig",
592                (va.create_config)(dpy, profile, ENTRYPOINT_VLD, &mut attrib, 1, &mut config),
593            )?;
594            let mut surfaces = vec![INVALID_ID; n];
595            let r = (va.create_surfaces)(
596                dpy,
597                rt,
598                w,
599                h,
600                surfaces.as_mut_ptr(),
601                n as c_uint,
602                std::ptr::null_mut(),
603                0,
604            );
605            if let Err(e) = va.check("vaCreateSurfaces", r) {
606                (va.destroy_config)(dpy, config);
607                return Err(e);
608            }
609            let mut context = 0;
610            let r = (va.create_context)(
611                dpy,
612                config,
613                w as c_int,
614                h as c_int,
615                PROGRESSIVE,
616                surfaces.as_mut_ptr(),
617                n as c_int,
618                &mut context,
619            );
620            if let Err(e) = va.check("vaCreateContext", r) {
621                (va.destroy_surfaces)(dpy, surfaces.as_mut_ptr(), n as c_int);
622                (va.destroy_config)(dpy, config);
623                return Err(e);
624            }
625            Ok(Session {
626                config,
627                context,
628                surfaces,
629                owner: vec![None; n],
630                key: (w, h, profile, rt),
631                buffers: Vec::new(),
632            })
633        }
634    }
635
636    pub(crate) fn destroy(&mut self, dev: &Device) {
637        let (va, dpy) = (dev.va, dev.dpy);
638        unsafe {
639            for id in self.buffers.drain(..) {
640                (va.destroy_buffer)(dpy, id);
641            }
642            (va.destroy_context)(dpy, self.context);
643            (va.destroy_surfaces)(
644                dpy,
645                self.surfaces.as_mut_ptr(),
646                self.surfaces.len() as c_int,
647            );
648            (va.destroy_config)(dpy, self.config);
649        }
650    }
651
652    /// The surface holding picture `key`.
653    pub(crate) fn surface_of(&self, key: i64) -> Option<Id> {
654        self.owner
655            .iter()
656            .position(|o| *o == Some(key))
657            .map(|i| self.surfaces[i])
658    }
659
660    /// The surface for picture `key`: its own if it has one, else one whose
661    /// picture is no longer `live`.
662    pub(crate) fn claim(&mut self, key: i64, live: impl Fn(i64) -> bool) -> Result<Id, String> {
663        if let Some(s) = self.surface_of(key) {
664            return Ok(s);
665        }
666        let i = self
667            .owner
668            .iter()
669            .position(|o| o.is_none_or(|k| !live(k)))
670            .ok_or("every VA surface holds a reference")?;
671        self.owner[i] = Some(key);
672        Ok(self.surfaces[i])
673    }
674
675    pub(crate) fn buffer<T>(&mut self, dev: &Device, kind: c_int, data: &T) -> Result<Id, String> {
676        self.raw_buffer(
677            dev,
678            kind,
679            std::mem::size_of::<T>(),
680            (data as *const T).cast(),
681        )
682    }
683
684    pub(crate) fn raw_buffer(
685        &mut self,
686        dev: &Device,
687        kind: c_int,
688        size: usize,
689        data: *const c_void,
690    ) -> Result<Id, String> {
691        self.raw_buffer_n(dev, kind, size, 1, data)
692    }
693
694    /// A buffer of `n` elements of `size` bytes each.
695    pub(crate) fn raw_buffer_n(
696        &mut self,
697        dev: &Device,
698        kind: c_int,
699        size: usize,
700        n: usize,
701        data: *const c_void,
702    ) -> Result<Id, String> {
703        let mut id = INVALID_ID;
704        let r = unsafe {
705            (dev.va.create_buffer)(
706                dev.dpy,
707                self.context,
708                kind,
709                size as c_uint,
710                n as c_uint,
711                data as *mut c_void,
712                &mut id,
713            )
714        };
715        dev.va.check("vaCreateBuffer", r)?;
716        self.buffers.push(id);
717        Ok(id)
718    }
719
720    pub(crate) fn begin(&mut self, dev: &Device, target: Id) -> Result<(), String> {
721        dev.va.check("vaBeginPicture", unsafe {
722            (dev.va.begin_picture)(dev.dpy, self.context, target)
723        })
724    }
725
726    pub(crate) fn render(&mut self, dev: &Device, ids: &mut [Id]) -> Result<(), String> {
727        dev.va.check("vaRenderPicture", unsafe {
728            (dev.va.render_picture)(dev.dpy, self.context, ids.as_mut_ptr(), ids.len() as c_int)
729        })
730    }
731
732    /// `vaEndPicture`, then free the picture's buffers.
733    pub(crate) fn end(&mut self, dev: &Device) -> Result<(), String> {
734        let r = dev.va.check("vaEndPicture", unsafe {
735            (dev.va.end_picture)(dev.dpy, self.context)
736        });
737        for id in self.buffers.drain(..) {
738            unsafe { (dev.va.destroy_buffer)(dev.dpy, id) };
739        }
740        r
741    }
742}
743
744/// Bytes of the coded NAL unit (header included) up to RBSP byte `rbsp`
745/// counted from `header` bytes in, and the emulation prevention bytes among
746/// them.
747pub(crate) fn coded_offset(nal: &[u8], header: usize, rbsp: usize) -> (usize, usize) {
748    let (mut seen, mut zeros, mut i, mut epb) = (0usize, 0usize, header, 0usize);
749    while i < nal.len() && seen < rbsp {
750        let b = nal[i];
751        if zeros >= 2 && b == 3 {
752            zeros = 0;
753            epb += 1;
754            i += 1;
755            continue;
756        }
757        zeros = if b == 0 { zeros + 1 } else { 0 };
758        seen += 1;
759        i += 1;
760    }
761    // An emulation prevention byte sitting exactly at the target.
762    if zeros >= 2 && i < nal.len() && nal[i] == 3 {
763        epb += 1;
764        i += 1;
765    }
766    (i, epb)
767}
768
769/// The native decoder doing the parsing for a [`VaapiDecoder`].
770enum Inner {
771    H264(Box<crate::decode::h264::H264Decoder>),
772    Hevc(Box<crate::decode::hevc::HevcDecoder>),
773    Vp9(Box<crate::decode::vp9::Vp9Decoder>),
774    Av1(Box<crate::decode::av1::Av1Decoder>),
775}
776
777/// A hardware decoder on a VA driver, with the interface of
778/// [`NativeDecoder`](crate::decode::NativeDecoder): open it from a demuxed
779/// track, push samples in decode order, take pictures by presentation time.
780///
781/// H.264 (progressive, MBAFF and field-coded, 8-bit 4:2:0), HEVC (Main
782/// and Main 10), VP9 (profiles 0 and 2) and AV1 (Main, 8- and 10-bit 4:2:0). Other streams are refused at [`new`](Self::new), and
783/// [`Decoder`](crate::decode::Decoder) then decodes them natively.
784pub struct VaapiDecoder {
785    dec: Inner,
786    nal_len: usize,
787    width: u32,
788    height: u32,
789    ready: Vec<(i64, Picture)>,
790    /// Pictures as the hardware made them, read back during a push.
791    outbox: std::sync::Arc<Outbox>,
792}
793
794impl VaapiDecoder {
795    /// Open a session for `track`.
796    pub fn new(track: &VideoTrack) -> Result<Self, String> {
797        let outbox = std::sync::Arc::new(Outbox::default());
798        let open = |profile| -> Result<Device, String> {
799            let mut device = Device::open(profile)?;
800            device.outbox = Some(outbox.clone());
801            Ok(device)
802        };
803        let (dec, sets, nal_len) = match track.codec {
804            TrackCodec::H264 => {
805                let device = open(PROFILE_H264_HIGH)?;
806                let (sets, nal_len) = crate::decode::split_avcc(&track.config)?;
807                let mut d = crate::decode::h264::H264Decoder::new();
808                d.set_accel(Box::new(h264::VaH264::new(device)));
809                (Inner::H264(Box::new(d)), sets, nal_len)
810            }
811            TrackCodec::Hevc => {
812                let device = open(PROFILE_HEVC_MAIN)?;
813                let (sets, nal_len) = crate::decode::split_hvcc(&track.config)?;
814                let mut d = crate::decode::hevc::HevcDecoder::new();
815                d.set_accel(Box::new(hevc::VaHevc::new(device)));
816                (Inner::Hevc(Box::new(d)), sets, nal_len)
817            }
818            TrackCodec::Vp9 => {
819                let device = open(vp9::PROFILE_VP9_0)?;
820                let mut d = crate::decode::vp9::Vp9Decoder::new();
821                d.set_accel(Box::new(vp9::VaVp9::new(device)));
822                (Inner::Vp9(Box::new(d)), Vec::new(), 0)
823            }
824            TrackCodec::Av1 => {
825                let device = open(av1_layout::PROFILE_AV1_0)?;
826                let mut d = crate::decode::av1::Av1Decoder::new();
827                d.set_accel(Box::new(av1::VaAv1::new(device)));
828                (Inner::Av1(Box::new(d)), Vec::new(), 0)
829            }
830        };
831        // Drivers have size limits (AMD's VCN will not go below 64 lines,
832        // for one) that only show when a context is created, which would
833        // otherwise be at the first frame. Try one now, so a stream the GPU
834        // will not take is refused here and `Decoder` decodes it natively.
835        {
836            let (profile, align) = match track.codec {
837                TrackCodec::H264 => (PROFILE_H264_HIGH, 16),
838                TrackCodec::Vp9 => (vp9::PROFILE_VP9_0, 8),
839                TrackCodec::Av1 => (av1_layout::PROFILE_AV1_0, 8),
840                TrackCodec::Hevc => (PROFILE_HEVC_MAIN, 8),
841            };
842            let probe = Device::open(profile)?;
843            let (w, h) = (
844                track.width.next_multiple_of(align),
845                track.height.next_multiple_of(align),
846            );
847            Session::new(&probe, profile, RT_FORMAT_YUV420, w.max(1), h.max(1), 1)
848                .map_err(|e| format!("{}x{}: {e}", track.width, track.height))?
849                .destroy(&probe);
850        }
851        let mut me = VaapiDecoder {
852            dec,
853            nal_len,
854            width: track.width,
855            height: track.height,
856            ready: Vec::new(),
857            outbox,
858        };
859        for s in sets {
860            me.nal(s)?;
861        }
862        Ok(me)
863    }
864
865    fn nal(&mut self, nal: &[u8]) -> Result<(), String> {
866        match &mut self.dec {
867            Inner::H264(d) => d.decode_nal(nal),
868            Inner::Hevc(d) => d.decode_nal(nal),
869            Inner::Vp9(_) | Inner::Av1(_) => Err("VP9 and AV1 have no NAL units".into()),
870        }
871    }
872
873    /// Size of the track, as the container states it.
874    pub fn size(&self) -> (u32, u32) {
875        (self.width, self.height)
876    }
877
878    /// Hand one coded frame (length-prefixed NAL units) to the decoder, in
879    /// decode order. Its picture is read back before this returns.
880    pub fn push(
881        &mut self,
882        sample: &[u8],
883        pts: i64,
884        _dts: i64,
885        _duration: i64,
886    ) -> Result<(), String> {
887        self.outbox.frames.lock().unwrap().clear();
888        let r = self.decode(sample, pts);
889        self.outbox.frames.lock().unwrap().clear();
890        r
891    }
892
893    fn decode(&mut self, sample: &[u8], pts: i64) -> Result<(), String> {
894        // Every picture is read back as it is shown, in this order, into the
895        // outbox; the decoder keeps an empty stand-in.
896        let take = |outbox: &Outbox| -> Result<PackedFrame, String> {
897            let mut packed = outbox.frames.lock().unwrap();
898            if packed.is_empty() {
899                return Err("a picture was shown but not read back".into());
900            }
901            Ok(packed.remove(0))
902        };
903        if let Inner::Av1(d) = &mut self.dec {
904            // A temporal unit shows at most one picture. With film grain the
905            // decoder has the samples, grain added; without, the outbox has
906            // the picture.
907            for (i, f) in d.decode(sample)?.into_iter().enumerate() {
908                let picture = if f.hdr.film_grain_present {
909                    Picture::Planes(f.to_decoded())
910                } else {
911                    let mut p = take(&self.outbox)?;
912                    p.color =
913                        ColorInfo::from_vui(Some(f.seq.mtrx as u32), f.seq.color_range, p.height);
914                    Picture::Packed(p)
915                };
916                self.ready.push((pts + i as i64, picture));
917            }
918            return Ok(());
919        }
920        if let Inner::Vp9(d) = &mut self.dec {
921            // A packet (superframe) shows at most one picture.
922            for (i, f) in d.decode(sample)?.into_iter().enumerate() {
923                d.accel_read(&f)?;
924                self.ready
925                    .push((pts + i as i64, Picture::Packed(take(&self.outbox)?)));
926            }
927            return Ok(());
928        }
929        for nal in crate::decode::bits::split_length_prefixed(sample, self.nal_len)? {
930            self.nal(nal)?;
931        }
932        let (shown, error) = match &mut self.dec {
933            Inner::H264(d) => {
934                d.finish_picture();
935                (d.take_pictures().len(), d.take_accel_error())
936            }
937            Inner::Hevc(d) => {
938                d.finish_picture();
939                (d.take_pictures().len(), d.take_accel_error())
940            }
941            Inner::Vp9(_) | Inner::Av1(_) => unreachable!("VP9 and AV1 returned above"),
942        };
943        if let Some(e) = error {
944            return Err(e);
945        }
946        for i in 0..shown {
947            self.ready
948                .push((pts + i as i64, Picture::Packed(take(&self.outbox)?)));
949        }
950        Ok(())
951    }
952
953    /// Copy pictures into buffers from `pool` (see
954    /// [`FramePool`](crate::bytes::FramePool)), to be given back there.
955    pub fn set_pool(&mut self, pool: crate::bytes::FramePool) {
956        *self.outbox.pool.lock().unwrap() = pool;
957    }
958
959    /// The picture for presentation time `pts` as tightly packed BGRA.
960    pub fn take(&mut self, pts: i64) -> Option<Vec<u8>> {
961        self.take_frame(pts).map(|f| f.to_bgra())
962    }
963
964    /// [`take`](Self::take), but the planes.
965    pub fn take_frame(&mut self, pts: i64) -> Option<DecodedFrame> {
966        self.take_picture(pts).map(Picture::into_decoded)
967    }
968
969    /// [`take`](Self::take), as the hardware left the picture where it can:
970    /// NV12, or P010 above 8 bits, rows copied out of the surface and
971    /// nothing converted, for a caller converting on the GPU. AV1 with film
972    /// grain comes as planes, the grain added by the CPU.
973    pub fn take_picture(&mut self, pts: i64) -> Option<Picture> {
974        let hit = self.ready.iter().position(|(p, _)| *p == pts)?;
975        let (_, picture) = self.ready.remove(hit);
976        self.ready.retain(|(p, _)| *p > pts);
977        Some(picture)
978    }
979}