Skip to main content

threers_codec/vaapi/
encode.rs

1//! HEVC encoding on a VA driver: RGBA frames in, an `hvc1` MP4 out.
2//!
3//! IDR and P frames, each P frame predicted from the one before, so every
4//! frame pushed comes back as one access unit before `push_rgba` returns.
5//! The driver writes the parameter sets and slice headers itself; they are
6//! lifted out of the stream into `hvcC`, as the NVENC path does.
7//!
8//! The struct layouts are libva's `va_enc_hevc.h`, written as zeroed blocks
9//! of the C size with fields at the offsets the C compiler reported
10//! ([`super::enc_layout`]).
11
12use std::ffi::{c_int, c_uint, c_void};
13
14use super::enc_layout::*;
15use super::{
16    ConfigAttrib, Device, Id, Image, ImageFormat, ATTRIB_RT_FORMAT, ENTRYPOINT_ENC_SLICE,
17    ENTRYPOINT_ENC_SLICE_LP, INVALID_ID, PROFILE_HEVC_MAIN, PROGRESSIVE, RT_FORMAT_YUV420,
18};
19use crate::hevc::hvcc::{build_hvcc, HvccArray, HvccProfile};
20
21const ATTRIB_RATE_CONTROL: c_int = 5;
22const ATTRIB_ENC_PACKED_HEADERS: c_int = 10;
23const ATTRIB_PREDICTION_DIRECTION: c_int = 39;
24const ATTRIB_ENC_HEVC_FEATURES: c_int = 50;
25const ATTRIB_ENC_HEVC_BLOCK_SIZES: c_int = 51;
26const ATTRIB_NOT_SUPPORTED: c_uint = 0x8000_0000;
27/// `VA_PREDICTION_DIRECTION_BI_NOT_EMPTY`: B slices must have a list 1.
28const PREDICTION_BI_NOT_EMPTY: c_uint = 4;
29const RC_CQP: c_uint = 0x10;
30const RC_VBR: c_uint = 0x4;
31const BUF_ENC_CODED: c_int = 21;
32const BUF_ENC_SEQUENCE: c_int = 22;
33const BUF_ENC_PICTURE: c_int = 23;
34const BUF_ENC_SLICE: c_int = 24;
35const BUF_ENC_MISC: c_int = 27;
36const BUF_PACKED_HEADER_PARAMS: c_int = 25;
37const BUF_PACKED_HEADER_DATA: c_int = 26;
38const PACKED_SEQUENCE: u32 = 1;
39const PACKED_SLICE: u32 = 3;
40/// `VA_ENC_PACKED_HEADER_SEQUENCE | VA_ENC_PACKED_HEADER_SLICE`.
41const PACKED_HEADERS: c_uint = 0x1 | 0x4;
42/// `sizeof(VAEncPackedHeaderParameterBuffer)`.
43const PACKED_PARAM_SIZE: usize = 28;
44const PIC_INVALID: u32 = 0x01;
45/// The largest CTB AMD's and Intel's encoders take, and the one they want.
46const LOG2_CTB: u32 = 6;
47
48/// How a [`VaapiEncoder`] spends bits.
49#[derive(Debug, Clone, Copy, PartialEq)]
50pub enum VaapiRate {
51    /// A fixed quantizer for every frame (0–51, lower is better).
52    ConstQp(u32),
53    /// Variable bitrate around this many bits per second.
54    Bitrate(u32),
55}
56
57/// A zeroed C struct of a known size with fields written at known offsets.
58struct Blob(Vec<u64>);
59
60impl Blob {
61    fn new(size: usize) -> Blob {
62        Blob(vec![0; size.div_ceil(8)])
63    }
64    fn bytes(&mut self) -> &mut [u8] {
65        let n = self.0.len() * 8;
66        unsafe { std::slice::from_raw_parts_mut(self.0.as_mut_ptr().cast(), n) }
67    }
68    fn u8(&mut self, at: usize, v: u8) {
69        self.bytes()[at] = v;
70    }
71    fn u16(&mut self, at: usize, v: u16) {
72        self.bytes()[at..at + 2].copy_from_slice(&v.to_ne_bytes());
73    }
74    fn u32(&mut self, at: usize, v: u32) {
75        self.bytes()[at..at + 4].copy_from_slice(&v.to_ne_bytes());
76    }
77    fn i32(&mut self, at: usize, v: i32) {
78        self.u32(at, v as u32);
79    }
80    /// A `VAPictureHEVC`: surface, POC, flags.
81    fn picture(&mut self, at: usize, surface: Id, poc: i32, flags: u32) {
82        self.u32(at, surface);
83        self.i32(at + 4, poc);
84        self.u32(at + 8, flags);
85    }
86    fn invalid_pictures(&mut self, at: usize, n: usize) {
87        for i in 0..n {
88            self.picture(at + 28 * i, INVALID_ID, 0, PIC_INVALID);
89        }
90    }
91    fn ptr(&self) -> *const c_void {
92        self.0.as_ptr().cast()
93    }
94}
95
96/// A device with an HEVC encode entry point, and which one: the full
97/// encoder first, Intel's low-power one (the only one on recent Intel GPUs)
98/// after.
99pub(crate) fn open_device() -> Result<(Device, c_int), String> {
100    Device::open_for(PROFILE_HEVC_MAIN, ENTRYPOINT_ENC_SLICE)
101        .map(|d| (d, ENTRYPOINT_ENC_SLICE))
102        .or_else(|e| {
103            Device::open_for(PROFILE_HEVC_MAIN, ENTRYPOINT_ENC_SLICE_LP)
104                .map(|d| (d, ENTRYPOINT_ENC_SLICE_LP))
105                .map_err(|_| e)
106        })
107}
108
109/// What the driver's encoder requires of the stream, which our headers and
110/// the VA parameters must both state.
111#[derive(Clone, Copy, Debug)]
112struct Requirements {
113    /// Asymmetric motion partitions are always on (Intel's VDEnc).
114    amp: bool,
115    /// `cu_qp_delta_enabled_flag` must be set (Intel's VDEnc).
116    cu_qp_delta: bool,
117    /// P frames go as B slices whose list 1 repeats list 0 ("low-delay B"),
118    /// for encoders without P slices.
119    low_delay_b: bool,
120    /// `max_transform_hierarchy_depth_inter` / `_intra`: the largest the
121    /// encoder allows, which is what it uses.
122    depth_inter: u32,
123    depth_intra: u32,
124}
125
126impl Requirements {
127    fn query(dev: &Device, entrypoint: c_int) -> Requirements {
128        let mut a = [
129            ConfigAttrib {
130                kind: ATTRIB_ENC_HEVC_FEATURES,
131                value: 0,
132            },
133            ConfigAttrib {
134                kind: ATTRIB_PREDICTION_DIRECTION,
135                value: 0,
136            },
137            ConfigAttrib {
138                kind: ATTRIB_ENC_HEVC_BLOCK_SIZES,
139                value: 0,
140            },
141        ];
142        let ok = unsafe {
143            (dev.va.get_config_attributes)(
144                dev.dpy,
145                PROFILE_HEVC_MAIN,
146                entrypoint,
147                a.as_mut_ptr(),
148                3,
149            )
150        } == 0;
151        let known = |v: c_uint| ok && v != ATTRIB_NOT_SUPPORTED;
152        // VAConfigAttribValEncHEVCFeatures: two bits a feature, 2 = required.
153        let feature = |i: u32| {
154            if known(a[0].value) {
155                (a[0].value >> (2 * i)) & 3
156            } else {
157                0
158            }
159        };
160        Requirements {
161            amp: feature(2) == 2,
162            cu_qp_delta: feature(11) == 2,
163            low_delay_b: known(a[1].value) && a[1].value & PREDICTION_BI_NOT_EMPTY != 0,
164            // VAConfigAttribValEncHEVCBlockSizes, two bits a field:
165            // max_max_transform_hierarchy_depth_inter is field 5, _intra 7.
166            depth_inter: if known(a[2].value) {
167                (a[2].value >> 10) & 3
168            } else {
169                0
170            },
171            depth_intra: if known(a[2].value) {
172                (a[2].value >> 14) & 3
173            } else {
174                0
175            },
176        }
177    }
178}
179
180/// A hardware HEVC encoder on a VA driver (AMD, Intel): RGBA frames in, an
181/// `hvc1` MP4 out.
182pub struct VaapiEncoder {
183    dev: Device,
184    req: Requirements,
185    config: Id,
186    context: Id,
187    input: Id,
188    /// Reconstructed pictures: the one being coded and its reference.
189    recon: [Id; 2],
190    coded: Id,
191    image: Image,
192    width: u32,
193    height: u32,
194    fps: u32,
195    gop: u32,
196    rate: VaapiRate,
197    frame: u32,
198    vps: Vec<u8>,
199    sps: Vec<u8>,
200    pps: Vec<u8>,
201    samples: Vec<Vec<u8>>,
202}
203
204impl VaapiEncoder {
205    /// Open an encoder for `width × height` frames (even sizes) at `fps`,
206    /// rate-controlled by `rate`, an IDR frame every `keyframe_secs` seconds.
207    pub fn new(
208        width: u32,
209        height: u32,
210        fps: u32,
211        rate: VaapiRate,
212        keyframe_secs: u32,
213    ) -> Result<Self, String> {
214        if width == 0 || height == 0 || !width.is_multiple_of(2) || !height.is_multiple_of(2) {
215            return Err(format!("VAAPI encodes even sizes; {width}x{height} is not"));
216        }
217        let (dev, entrypoint) = open_device()?;
218        let req = Requirements::query(&dev, entrypoint);
219        let (va, dpy) = (dev.va, dev.dpy);
220        let fps = fps.max(1);
221        unsafe {
222            let mut attribs = [
223                ConfigAttrib {
224                    kind: ATTRIB_RT_FORMAT,
225                    value: RT_FORMAT_YUV420,
226                },
227                ConfigAttrib {
228                    kind: ATTRIB_RATE_CONTROL,
229                    value: match rate {
230                        VaapiRate::ConstQp(_) => RC_CQP,
231                        VaapiRate::Bitrate(_) => RC_VBR,
232                    },
233                },
234                // Parameter sets and slice headers come from us ([`headers`]):
235                // drivers differ on whether they write their own (Mesa writes
236                // none), and ours carry the colour description.
237                ConfigAttrib {
238                    kind: ATTRIB_ENC_PACKED_HEADERS,
239                    value: PACKED_HEADERS,
240                },
241            ];
242            let mut config = 0;
243            va.check(
244                "vaCreateConfig",
245                (va.create_config)(
246                    dpy,
247                    PROFILE_HEVC_MAIN,
248                    entrypoint,
249                    attribs.as_mut_ptr(),
250                    attribs.len() as c_int,
251                    &mut config,
252                ),
253            )?;
254            // Surfaces padded to 16, as other VA clients allocate them; the
255            // SPS crops to the picture.
256            let (sw, sh) = (width.next_multiple_of(16), height.next_multiple_of(16));
257            let mut surfaces = [INVALID_ID; 3];
258            let r = (va.create_surfaces)(
259                dpy,
260                RT_FORMAT_YUV420,
261                sw,
262                sh,
263                surfaces.as_mut_ptr(),
264                3,
265                std::ptr::null_mut(),
266                0,
267            );
268            if let Err(e) = va.check("vaCreateSurfaces", r) {
269                (va.destroy_config)(dpy, config);
270                return Err(e);
271            }
272            let mut recon = [surfaces[1], surfaces[2]];
273            let mut context = 0;
274            let r = (va.create_context)(
275                dpy,
276                config,
277                sw as c_int,
278                sh as c_int,
279                PROGRESSIVE,
280                recon.as_mut_ptr(),
281                2,
282                &mut context,
283            );
284            if let Err(e) = va.check("vaCreateContext", r) {
285                (va.destroy_surfaces)(dpy, surfaces.as_mut_ptr(), 3);
286                (va.destroy_config)(dpy, config);
287                return Err(e);
288            }
289            let mut coded = INVALID_ID;
290            let size = (sw * sh * 3 / 2 + (1 << 16)) as c_uint;
291            let r = (va.create_buffer)(
292                dpy,
293                context,
294                BUF_ENC_CODED,
295                size,
296                1,
297                std::ptr::null_mut(),
298                &mut coded,
299            );
300            let mut fmt: ImageFormat = std::mem::zeroed();
301            fmt.fourcc = super::FOURCC_NV12;
302            fmt.byte_order = 1;
303            fmt.bits_per_pixel = 12;
304            let mut image: Image = std::mem::zeroed();
305            let r2 = if r == 0 {
306                (va.create_image)(dpy, &mut fmt, width as c_int, height as c_int, &mut image)
307            } else {
308                r
309            };
310            if let Err(e) = va.check("vaCreateBuffer/vaCreateImage", r2) {
311                if coded != INVALID_ID {
312                    (va.destroy_buffer)(dpy, coded);
313                }
314                (va.destroy_context)(dpy, context);
315                (va.destroy_surfaces)(dpy, surfaces.as_mut_ptr(), 3);
316                (va.destroy_config)(dpy, config);
317                return Err(e);
318            }
319            Ok(VaapiEncoder {
320                dev,
321                req,
322                config,
323                context,
324                input: surfaces[0],
325                recon,
326                coded,
327                image,
328                width,
329                height,
330                fps,
331                gop: (fps * keyframe_secs.max(1)).max(1),
332                rate,
333                frame: 0,
334                vps: Vec::new(),
335                sps: Vec::new(),
336                pps: Vec::new(),
337                samples: Vec::new(),
338            })
339        }
340    }
341
342    /// RGBA into the input surface as NV12, BT.709 limited range for HD and
343    /// BT.601 below it: what decoders assume of a stream that does not say,
344    /// and the driver's SPS does not.
345    fn upload(&mut self, rgba: &[u8]) -> Result<(), String> {
346        let (w, h) = (self.width as usize, self.height as usize);
347        // Y = (kr·R + kg·G + kb·B) in 8.8 fixed point, limited range.
348        let (yr, yg, yb, ur, ug, ub, vr, vg, vb) = if h >= 720 {
349            (47, 157, 16, -26, -87, 112, 112, -102, -10)
350        } else {
351            (66, 129, 25, -38, -74, 112, 112, -94, -18)
352        };
353        let (va, dpy) = (self.dev.va, self.dev.dpy);
354        unsafe {
355            let mut p: *mut c_void = std::ptr::null_mut();
356            va.check("vaMapBuffer", (va.map_buffer)(dpy, self.image.buf, &mut p))?;
357            let data = std::slice::from_raw_parts_mut(p as *mut u8, self.image.data_size as usize);
358            let (yp, up) = (
359                self.image.pitches[0] as usize,
360                self.image.pitches[1] as usize,
361            );
362            let (yo, uo) = (
363                self.image.offsets[0] as usize,
364                self.image.offsets[1] as usize,
365            );
366            for y in 0..h {
367                let src = &rgba[y * w * 4..][..w * 4];
368                let dst = &mut data[yo + y * yp..][..w];
369                for (d, px) in dst.iter_mut().zip(src.as_chunks::<4>().0) {
370                    let (r, g, b) = (px[0] as i32, px[1] as i32, px[2] as i32);
371                    *d = (((yr * r + yg * g + yb * b + 128) >> 8) + 16).clamp(0, 255) as u8;
372                }
373            }
374            for cy in 0..h / 2 {
375                let (r0, r1) = (
376                    &rgba[2 * cy * w * 4..][..w * 4],
377                    &rgba[(2 * cy + 1) * w * 4..][..w * 4],
378                );
379                let dst = &mut data[uo + cy * up..][..w];
380                for (cx, d) in dst.as_chunks_mut::<2>().0.iter_mut().enumerate() {
381                    let mut s = [0i32; 3];
382                    for row in [r0, r1] {
383                        for px in row[cx * 8..cx * 8 + 8].as_chunks::<4>().0 {
384                            s[0] += px[0] as i32;
385                            s[1] += px[1] as i32;
386                            s[2] += px[2] as i32;
387                        }
388                    }
389                    let (r, g, b) = ((s[0] + 2) >> 2, (s[1] + 2) >> 2, (s[2] + 2) >> 2);
390                    d[0] = (((ur * r + ug * g + ub * b + 128) >> 8) + 128).clamp(0, 255) as u8;
391                    d[1] = (((vr * r + vg * g + vb * b + 128) >> 8) + 128).clamp(0, 255) as u8;
392                }
393            }
394            (va.unmap_buffer)(dpy, self.image.buf);
395            let r = (va.put_image)(
396                dpy,
397                self.input,
398                self.image.image_id,
399                0,
400                0,
401                w as c_uint,
402                h as c_uint,
403                0,
404                0,
405                w as c_uint,
406                h as c_uint,
407            );
408            va.check("vaPutImage", r)
409        }
410    }
411
412    fn buffer(&self, kind: c_int, blob: &Blob, size: usize) -> Result<Id, String> {
413        let mut id = INVALID_ID;
414        let r = unsafe {
415            (self.dev.va.create_buffer)(
416                self.dev.dpy,
417                self.context,
418                kind,
419                size as c_uint,
420                1,
421                blob.ptr() as *mut c_void,
422                &mut id,
423            )
424        };
425        self.dev.va.check("vaCreateBuffer", r)?;
426        Ok(id)
427    }
428
429    /// A packed header: its parameter buffer and its data buffer.
430    fn packed(&self, kind: u32, nal: &[u8], ids: &mut Vec<Id>) -> Result<(), String> {
431        let mut p = Blob::new(PACKED_PARAM_SIZE);
432        p.u32(0, kind);
433        p.u32(4, (nal.len() * 8) as u32);
434        p.u8(8, 1); // has_emulation_bytes
435        ids.push(self.buffer(BUF_PACKED_HEADER_PARAMS, &p, PACKED_PARAM_SIZE)?);
436        let mut id = INVALID_ID;
437        let r = unsafe {
438            (self.dev.va.create_buffer)(
439                self.dev.dpy,
440                self.context,
441                BUF_PACKED_HEADER_DATA,
442                nal.len() as c_uint,
443                1,
444                nal.as_ptr() as *mut c_void,
445                &mut id,
446            )
447        };
448        self.dev.va.check("vaCreateBuffer", r)?;
449        ids.push(id);
450        Ok(())
451    }
452
453    fn qp(&self) -> u32 {
454        match self.rate {
455            VaapiRate::ConstQp(qp) => qp.min(51),
456            VaapiRate::Bitrate(_) => 26,
457        }
458    }
459
460    /// Encode one frame of tightly packed RGBA8, `width * height * 4` bytes.
461    pub fn push_rgba(&mut self, rgba: &[u8]) -> Result<(), String> {
462        let (w, h) = (self.width as usize, self.height as usize);
463        if rgba.len() < w * h * 4 {
464            return Err(format!("frame is {} bytes, want {}", rgba.len(), w * h * 4));
465        }
466        self.upload(&rgba[..w * h * 4])?;
467        let poc_in_gop = self.frame % self.gop;
468        let idr = poc_in_gop == 0;
469        let cur = self.recon[(self.frame % 2) as usize];
470        let reference = self.recon[((self.frame + 1) % 2) as usize];
471        let qp = self.qp();
472        let ctb = 1u32 << LOG2_CTB;
473        let ctus = self.width.div_ceil(ctb) * self.height.div_ceil(ctb);
474        let bitrate = match self.rate {
475            VaapiRate::Bitrate(b) => b,
476            VaapiRate::ConstQp(_) => 0,
477        };
478
479        let mut ids = Vec::new();
480        if idr {
481            let mut seq = Blob::new(SEQ_SIZE);
482            seq.u8(SEQ_PROFILE, 1);
483            seq.u8(SEQ_LEVEL, level_idc(self.width, self.height, self.fps));
484            seq.u32(SEQ_INTRA_PERIOD, self.gop);
485            seq.u32(SEQ_IDR_PERIOD, self.gop);
486            seq.u32(SEQ_IP_PERIOD, 1);
487            seq.u32(SEQ_BPS, bitrate);
488            seq.u16(SEQ_WIDTH, self.width.next_multiple_of(16) as u16);
489            seq.u16(SEQ_HEIGHT, self.height.next_multiple_of(16) as u16);
490            seq.u32(
491                SEQ_FIELDS,
492                1 << SEQ_BIT_CHROMA_FORMAT
493                    | 1 << SEQ_BIT_LOW_DELAY
494                    | (self.req.amp as u32) << SEQ_BIT_AMP,
495            );
496            seq.u8(SEQ_LOG2_MIN_CB, 0);
497            seq.u8(SEQ_LOG2_DIFF_CB, (LOG2_CTB - 3) as u8);
498            seq.u8(SEQ_LOG2_MIN_TB, 0);
499            seq.u8(SEQ_LOG2_DIFF_TB, 3);
500            seq.u8(SEQ_DEPTH_INTER, self.req.depth_inter as u8);
501            seq.u8(SEQ_DEPTH_INTRA, self.req.depth_intra as u8);
502            ids.push(self.buffer(BUF_ENC_SEQUENCE, &seq, SEQ_SIZE)?);
503            if let VaapiRate::Bitrate(bps) = self.rate {
504                let mut rc = Blob::new(MISC_HEADER_SIZE + MISC_RC_SIZE);
505                rc.u32(0, MISC_TYPE_RATE_CONTROL);
506                rc.u32(MISC_HEADER_SIZE + RC_BPS, bps);
507                rc.u32(MISC_HEADER_SIZE + RC_TARGET_PCT, 90);
508                rc.u32(MISC_HEADER_SIZE + RC_WINDOW, 1000);
509                ids.push(self.buffer(BUF_ENC_MISC, &rc, MISC_HEADER_SIZE + MISC_RC_SIZE)?);
510                let mut fr = Blob::new(MISC_HEADER_SIZE + MISC_FRAMERATE_SIZE);
511                fr.u32(0, MISC_TYPE_FRAME_RATE);
512                fr.u32(MISC_HEADER_SIZE, self.fps | (1 << 16));
513                ids.push(self.buffer(BUF_ENC_MISC, &fr, MISC_HEADER_SIZE + MISC_FRAMERATE_SIZE)?);
514            }
515        }
516
517        let hdr = headers::Params {
518            width: self.width,
519            height: self.height,
520            level_idc: level_idc(self.width, self.height, self.fps),
521            init_qp: qp as i32,
522            cu_qp_delta: self.req.cu_qp_delta || matches!(self.rate, VaapiRate::Bitrate(_)),
523            amp: self.req.amp,
524            low_delay_b: self.req.low_delay_b,
525            depth_inter: self.req.depth_inter,
526            depth_intra: self.req.depth_intra,
527            matrix: if self.height >= 720 { 1 } else { 6 },
528        };
529        let mut pic = Blob::new(PIC_SIZE);
530        pic.picture(PIC_CURR, cur, poc_in_gop as i32, 0);
531        pic.invalid_pictures(PIC_REFS, 15);
532        if !idr {
533            pic.picture(PIC_REFS, reference, poc_in_gop as i32 - 1, 0);
534        }
535        pic.u32(PIC_CODED_BUF, self.coded);
536        pic.u8(PIC_COLLOCATED, 0xff);
537        pic.u8(PIC_INIT_QP, qp as u8);
538        // QP can change in every 8×8 quantization group, as rate control
539        // needs (and as other clients set it).
540        if hdr.cu_qp_delta {
541            pic.u8(PIC_DIFF_CU_QP, (LOG2_CTB - 3) as u8);
542        }
543        pic.u8(PIC_NAL_TYPE, if idr { 19 } else { 1 });
544        let coding_type = if idr { 1 } else { 2 };
545        let cu_qp = hdr.cu_qp_delta as u32;
546        pic.u32(
547            PIC_FIELDS,
548            (idr as u32) << PIC_BIT_IDR
549                | coding_type << PIC_BIT_CODING_TYPE
550                | 1 << PIC_BIT_REFERENCE
551                | cu_qp << PIC_BIT_CU_QP_DELTA
552                | 1 << PIC_BIT_LF_ACROSS_SLICES,
553        );
554        ids.push(self.buffer(BUF_ENC_PICTURE, &pic, PIC_SIZE)?);
555
556        // The order other VA clients use, which drivers expect: sequence,
557        // picture, then the packed sequence headers, then each slice's packed
558        // header before its parameters.
559        if idr {
560            let mut seq = headers::vps(&hdr);
561            seq.extend(headers::sps(&hdr));
562            seq.extend(headers::pps(&hdr));
563            self.packed(PACKED_SEQUENCE, &seq, &mut ids)?;
564        }
565        self.packed(
566            PACKED_SLICE,
567            &headers::slice(&hdr, idr, poc_in_gop),
568            &mut ids,
569        )?;
570        let mut slice = Blob::new(SLICE_SIZE);
571        slice.u32(SLICE_ADDRESS, 0);
572        slice.u32(SLICE_NUM_CTU, ctus);
573        // I, P, or for encoders without P slices B with both lists the
574        // previous frame.
575        slice.u8(
576            SLICE_TYPE,
577            if idr {
578                2
579            } else if self.req.low_delay_b {
580                0
581            } else {
582                1
583            },
584        );
585        slice.invalid_pictures(SLICE_REF_L0, 15);
586        slice.invalid_pictures(SLICE_REF_L1, 15);
587        if !idr {
588            slice.picture(SLICE_REF_L0, reference, poc_in_gop as i32 - 1, 0);
589            if self.req.low_delay_b {
590                slice.picture(SLICE_REF_L1, reference, poc_in_gop as i32 - 1, 0);
591            }
592        }
593        slice.u8(SLICE_MAX_MERGE, 5);
594        slice.u32(
595            SLICE_FIELDS,
596            1 << SLICE_BIT_LAST | 1 << SLICE_BIT_LF_ACROSS_SLICES,
597        );
598        ids.push(self.buffer(BUF_ENC_SLICE, &slice, SLICE_SIZE)?);
599
600        let (va, dpy) = (self.dev.va, self.dev.dpy);
601        let r = unsafe {
602            va.check(
603                "vaBeginPicture",
604                (va.begin_picture)(dpy, self.context, self.input),
605            )
606            .and_then(|_| {
607                va.check(
608                    "vaRenderPicture",
609                    (va.render_picture)(dpy, self.context, ids.as_mut_ptr(), ids.len() as c_int),
610                )
611            })
612            .and_then(|_| va.check("vaEndPicture", (va.end_picture)(dpy, self.context)))
613            .and_then(|_| va.check("vaSyncSurface", (va.sync_surface)(dpy, self.input)))
614        };
615        for id in ids {
616            unsafe { (va.destroy_buffer)(dpy, id) };
617        }
618        r?;
619        let au = self.read_coded()?;
620        self.collect(&au);
621        self.frame += 1;
622        Ok(())
623    }
624
625    /// The bytes in the coded buffer: a list of segments.
626    fn read_coded(&self) -> Result<Vec<u8>, String> {
627        let (va, dpy) = (self.dev.va, self.dev.dpy);
628        let mut out = Vec::new();
629        unsafe {
630            let mut p: *mut c_void = std::ptr::null_mut();
631            va.check("vaMapBuffer", (va.map_buffer)(dpy, self.coded, &mut p))?;
632            let mut seg = p as *const u8;
633            while !seg.is_null() {
634                let size = *(seg.add(SEG_SIZE) as *const u32) as usize;
635                let buf = *(seg.add(SEG_BUF) as *const *const u8);
636                if !buf.is_null() && size > 0 {
637                    out.extend_from_slice(std::slice::from_raw_parts(buf, size));
638                }
639                seg = *(seg.add(SEG_NEXT) as *const *const u8);
640            }
641            (va.unmap_buffer)(dpy, self.coded);
642        }
643        Ok(out)
644    }
645
646    /// Parameter sets kept for `hvcC`; everything else becomes the sample.
647    fn collect(&mut self, au: &[u8]) {
648        let mut sample = Vec::with_capacity(au.len());
649        for nal in crate::h264::nal::split_annexb(au) {
650            match nal.first().map(|b| (b >> 1) & 0x3f) {
651                Some(32) if self.vps.is_empty() => self.vps = nal,
652                Some(33) if self.sps.is_empty() => self.sps = nal,
653                Some(34) if self.pps.is_empty() => self.pps = nal,
654                Some(32..=34) => {}
655                _ => {
656                    sample.extend_from_slice(&(nal.len() as u32).to_be_bytes());
657                    sample.extend_from_slice(&nal);
658                }
659            }
660        }
661        if !sample.is_empty() {
662            self.samples.push(sample);
663        }
664    }
665
666    /// Finish and return the MP4 file.
667    pub fn finish(self) -> Result<Vec<u8>, String> {
668        if self.samples.is_empty() {
669            return Err("no frames were pushed".into());
670        }
671        if self.vps.is_empty() || self.sps.is_empty() || self.pps.is_empty() {
672            return Err("the VA driver did not emit HEVC parameter sets".into());
673        }
674        let profile = HvccProfile::from_sps(&self.sps).ok_or("the driver's SPS did not parse")?;
675        let (vps, sps, pps) = ([self.vps.clone()], [self.sps.clone()], [self.pps.clone()]);
676        let hvcc = build_hvcc(
677            &profile,
678            &[
679                HvccArray {
680                    nal_type: 32,
681                    complete: true,
682                    nals: &vps,
683                },
684                HvccArray {
685                    nal_type: 33,
686                    complete: true,
687                    nals: &sps,
688                },
689                HvccArray {
690                    nal_type: 34,
691                    complete: true,
692                    nals: &pps,
693                },
694            ],
695        );
696        Ok(crate::mp4::mux_hevc(&crate::mp4::Mp4Params {
697            width: self.width,
698            height: self.height,
699            timescale: self.fps * 1000,
700            frame_duration: 1000,
701            hvcc_payload: &hvcc,
702            almo_payload: None,
703            samples: &self.samples,
704        }))
705    }
706}
707
708impl Drop for VaapiEncoder {
709    fn drop(&mut self) {
710        let (va, dpy) = (self.dev.va, self.dev.dpy);
711        unsafe {
712            (va.destroy_image)(dpy, self.image.image_id);
713            (va.destroy_buffer)(dpy, self.coded);
714            (va.destroy_context)(dpy, self.context);
715            let mut s = [self.input, self.recon[0], self.recon[1]];
716            (va.destroy_surfaces)(dpy, s.as_mut_ptr(), 3);
717            (va.destroy_config)(dpy, self.config);
718        }
719    }
720}
721
722/// `general_level_idc` (30 × level) for a picture size and rate (Table A.8,
723/// Main tier).
724fn level_idc(w: u32, h: u32, fps: u32) -> u8 {
725    let samples = w as u64 * h as u64;
726    let rate = samples * fps as u64;
727    let levels: [(u64, u64, u8); 8] = [
728        (552_960, 16_588_800, 93),       // 3.1
729        (2_228_224, 66_846_720, 120),    // 4
730        (2_228_224, 133_693_440, 123),   // 4.1
731        (8_912_896, 267_386_880, 150),   // 5
732        (8_912_896, 534_773_760, 153),   // 5.1
733        (8_912_896, 1_069_547_520, 156), // 5.2
734        (35_651_584, 1_069_547_520, 180),
735        (35_651_584, 2_139_095_040, 183),
736    ];
737    levels
738        .iter()
739        .find(|(s, r, _)| samples <= *s && rate <= *r)
740        .map_or(186, |l| l.2)
741}
742
743/// The VPS, SPS, PPS and slice headers, written to match exactly what the VA
744/// parameters set up (a 64×64 CTB, 4–32 transforms of depth 0, no SAO, AMP
745/// or temporal MV prediction; one reference), with a conformance window to
746/// the picture's size and the colour description in the VUI.
747mod headers {
748    use crate::bitstream::{emulation_prevention, rbsp_trailing_bits, BitWriter};
749
750    pub(super) struct Params {
751        pub width: u32,
752        pub height: u32,
753        pub level_idc: u8,
754        pub init_qp: i32,
755        pub cu_qp_delta: bool,
756        pub amp: bool,
757        /// P frames coded as B slices with list 1 = list 0.
758        pub low_delay_b: bool,
759        pub depth_inter: u32,
760        pub depth_intra: u32,
761        /// `matrix_coefficients` (and primaries / transfer): 1 BT.709, 6 BT.601.
762        pub matrix: u32,
763    }
764
765    /// `00 00 00 01`, the two-byte header, the payload with emulation
766    /// prevention.
767    fn nal(nal_type: u8, w: BitWriter) -> Vec<u8> {
768        let mut out = vec![0, 0, 0, 1, nal_type << 1, 1];
769        out.extend(emulation_prevention(&w.finish()));
770        out
771    }
772
773    fn profile_tier_level(w: &mut BitWriter, level_idc: u8) {
774        w.write_bits(0, 2); // general_profile_space
775        w.flag(false); // general_tier_flag
776        w.write_bits(1, 5); // general_profile_idc: Main
777        w.write_bits(0x6000_0000, 32); // compatible with Main and Main 10
778        w.flag(true); // progressive_source
779        w.flag(false); // interlaced_source
780        w.flag(false); // non_packed_constraint
781        w.flag(true); // frame_only_constraint
782        w.write_bits64(0, 44);
783        w.write_bits(level_idc as u32, 8);
784    }
785
786    pub(super) fn vps(p: &Params) -> Vec<u8> {
787        let mut w = BitWriter::new();
788        w.write_bits(0, 4); // vps_video_parameter_set_id
789        w.flag(true); // vps_base_layer_internal_flag
790        w.flag(true); // vps_base_layer_available_flag
791        w.write_bits(0, 6); // vps_max_layers_minus1
792        w.write_bits(0, 3); // vps_max_sub_layers_minus1
793        w.flag(true); // vps_temporal_id_nesting_flag
794        w.write_bits(0xffff, 16);
795        profile_tier_level(&mut w, p.level_idc);
796        w.flag(true); // vps_sub_layer_ordering_info_present_flag
797        w.write_ue(1); // vps_max_dec_pic_buffering_minus1
798        w.write_ue(0); // vps_max_num_reorder_pics
799        w.write_ue(0); // vps_max_latency_increase_plus1
800        w.write_bits(0, 6); // vps_max_layer_id
801        w.write_ue(0); // vps_num_layer_sets_minus1
802        w.flag(false); // vps_timing_info_present_flag
803        w.flag(false); // vps_extension_flag
804        rbsp_trailing_bits(&mut w);
805        nal(32, w)
806    }
807
808    pub(super) fn sps(p: &Params) -> Vec<u8> {
809        let mut w = BitWriter::new();
810        w.write_bits(0, 4); // sps_video_parameter_set_id
811        w.write_bits(0, 3); // sps_max_sub_layers_minus1
812        w.flag(true); // sps_temporal_id_nesting_flag
813        profile_tier_level(&mut w, p.level_idc);
814        w.write_ue(0); // sps_seq_parameter_set_id
815        w.write_ue(1); // chroma_format_idc: 4:2:0
816                       // The coded size is the surface's (16-aligned); the window crops to
817                       // the picture. Mesa copies the window from here into the SPS it
818                       // writes, and always codes the surface's size.
819        let (cw, ch) = (p.width.next_multiple_of(16), p.height.next_multiple_of(16));
820        w.write_ue(cw);
821        w.write_ue(ch);
822        let crop = cw != p.width || ch != p.height;
823        w.flag(crop); // conformance_window_flag
824        if crop {
825            // In chroma samples for 4:2:0.
826            w.write_ue(0);
827            w.write_ue((cw - p.width) / 2);
828            w.write_ue(0);
829            w.write_ue((ch - p.height) / 2);
830        }
831        w.write_ue(0); // bit_depth_luma_minus8
832        w.write_ue(0); // bit_depth_chroma_minus8
833        w.write_ue(4); // log2_max_pic_order_cnt_lsb_minus4: 8-bit POC LSB
834        w.flag(true); // sps_sub_layer_ordering_info_present_flag
835        w.write_ue(1); // sps_max_dec_pic_buffering_minus1
836        w.write_ue(0); // sps_max_num_reorder_pics
837        w.write_ue(0); // sps_max_latency_increase_plus1
838        w.write_ue(0); // log2_min_luma_coding_block_size_minus3: 8×8
839        w.write_ue(3); // log2_diff_max_min_luma_coding_block_size: 64×64 CTB
840        w.write_ue(0); // log2_min_luma_transform_block_size_minus2: 4×4
841        w.write_ue(3); // log2_diff_max_min_luma_transform_block_size: 32×32
842        w.write_ue(p.depth_inter); // max_transform_hierarchy_depth_inter
843        w.write_ue(p.depth_intra); // max_transform_hierarchy_depth_intra
844        w.flag(false); // scaling_list_enabled_flag
845        w.flag(p.amp); // amp_enabled_flag
846        w.flag(false); // sample_adaptive_offset_enabled_flag
847        w.flag(false); // pcm_enabled_flag
848        w.write_ue(0); // num_short_term_ref_pic_sets: each slice codes its own
849        w.flag(false); // long_term_ref_pics_present_flag
850        w.flag(false); // sps_temporal_mvp_enabled_flag
851        w.flag(false); // strong_intra_smoothing_enabled_flag
852        w.flag(true); // vui_parameters_present_flag
853        w.flag(false); // aspect_ratio_info_present_flag
854        w.flag(false); // overscan_info_present_flag
855        w.flag(true); // video_signal_type_present_flag
856        w.write_bits(5, 3); // video_format: unspecified
857        w.flag(false); // video_full_range_flag
858        w.flag(true); // colour_description_present_flag
859        w.write_bits(p.matrix, 8); // colour_primaries
860        w.write_bits(p.matrix, 8); // transfer_characteristics
861        w.write_bits(p.matrix, 8); // matrix_coeffs
862        w.flag(false); // chroma_loc_info_present_flag
863        w.flag(false); // neutral_chroma_indication_flag
864        w.flag(false); // field_seq_flag
865        w.flag(false); // frame_field_info_present_flag
866        w.flag(false); // default_display_window_flag
867        w.flag(false); // vui_timing_info_present_flag
868        w.flag(false); // bitstream_restriction_flag
869        w.flag(false); // sps_extension_present_flag
870        rbsp_trailing_bits(&mut w);
871        nal(33, w)
872    }
873
874    pub(super) fn pps(p: &Params) -> Vec<u8> {
875        let mut w = BitWriter::new();
876        w.write_ue(0); // pps_pic_parameter_set_id
877        w.write_ue(0); // pps_seq_parameter_set_id
878        w.flag(false); // dependent_slice_segments_enabled_flag
879        w.flag(false); // output_flag_present_flag
880        w.write_bits(0, 3); // num_extra_slice_header_bits
881        w.flag(false); // sign_data_hiding_enabled_flag
882        w.flag(false); // cabac_init_present_flag
883        w.write_ue(0); // num_ref_idx_l0_default_active_minus1
884        w.write_ue(0); // num_ref_idx_l1_default_active_minus1
885        w.write_se(p.init_qp - 26);
886        w.flag(false); // constrained_intra_pred_flag
887        w.flag(false); // transform_skip_enabled_flag
888        w.flag(p.cu_qp_delta);
889        if p.cu_qp_delta {
890            w.write_ue(super::LOG2_CTB - 3); // diff_cu_qp_delta_depth: 8×8 groups
891        }
892        w.write_se(0); // pps_cb_qp_offset
893        w.write_se(0); // pps_cr_qp_offset
894        w.flag(false); // pps_slice_chroma_qp_offsets_present_flag
895        w.flag(false); // weighted_pred_flag
896        w.flag(false); // weighted_bipred_flag
897        w.flag(false); // transquant_bypass_enabled_flag
898        w.flag(false); // tiles_enabled_flag
899        w.flag(false); // entropy_coding_sync_enabled_flag
900        w.flag(true); // pps_loop_filter_across_slices_enabled_flag
901        w.flag(false); // deblocking_filter_control_present_flag
902        w.flag(false); // pps_scaling_list_data_present_flag
903        w.flag(false); // lists_modification_present_flag
904        w.write_ue(0); // log2_parallel_merge_level_minus2
905        w.flag(false); // slice_segment_header_extension_present_flag
906        w.flag(false); // pps_extension_present_flag
907        rbsp_trailing_bits(&mut w);
908        nal(34, w)
909    }
910
911    /// The slice segment header of a one-slice picture, through
912    /// `byte_alignment()`; the encoder appends the slice data.
913    pub(super) fn slice(p: &Params, idr: bool, poc: u32) -> Vec<u8> {
914        let mut w = BitWriter::new();
915        w.flag(true); // first_slice_segment_in_pic_flag
916        if idr {
917            w.flag(false); // no_output_of_prior_pics_flag
918        }
919        w.write_ue(0); // slice_pic_parameter_set_id
920        w.write_ue(if idr {
921            2
922        } else if p.low_delay_b {
923            0
924        } else {
925            1
926        }); // slice_type: I, P or B
927        if !idr {
928            w.write_bits(poc & 0xff, 8); // slice_pic_order_cnt_lsb
929            w.flag(false); // short_term_ref_pic_set_sps_flag
930                           // st_ref_pic_set(0): the previous picture, used.
931            w.write_ue(1); // num_negative_pics
932            w.write_ue(0); // num_positive_pics
933            w.write_ue(0); // delta_poc_s0_minus1
934            w.flag(true); // used_by_curr_pic_s0_flag
935            w.flag(false); // num_ref_idx_active_override_flag: one in each list
936            if p.low_delay_b {
937                w.flag(false); // mvd_l1_zero_flag
938            }
939            w.write_ue(0); // five_minus_max_num_merge_cand
940        }
941        w.write_se(0); // slice_qp_delta
942                       // pps_loop_filter_across_slices_enabled_flag, deblocking on:
943        w.flag(true); // slice_loop_filter_across_slices_enabled_flag
944                      // byte_alignment()
945        w.write_bit(1);
946        w.align_zero();
947        nal(if idr { 19 } else { 1 }, w)
948    }
949}