threers-codec 0.0.12

Pure-Rust media codecs: H.264/HEVC/VP9 encoders, H.264/HEVC/VP9/AV1 decoders, AAC/MP3/Opus/Vorbis/FLAC/WAV decoders, AAC-LC and MP3 encoders, MP4/WebM/Matroska/Ogg, GIF/APNG, captions, and VideoToolbox acceleration on macOS
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
//! H.264 on VAAPI: the native decoder's slices, reference lists and DPB
//! translated into `VAPictureParameterBufferH264`, `VAIQMatrixBufferH264`
//! and `VASliceParameterBufferH264` (the layout of libva's `va.h`).

use super::{
    Device, Id, Session, BUF_IQ_MATRIX, BUF_PICTURE_PARAMETER, BUF_SLICE_DATA, BUF_SLICE_PARAMETER,
    INVALID_ID, PROFILE_H264_HIGH, RT_FORMAT_YUV420,
};
use crate::decode::h264::params::{ScalingLists, SliceType, Sps};
use crate::decode::h264::types::RefPic;
use crate::decode::h264::{AccelSlice, H264Accel, Result};
use crate::decode::{ColorInfo, DecodedFrame};

const PIC_INVALID: u32 = 0x01;
const PIC_TOP_FIELD: u32 = 0x02;
const PIC_BOTTOM_FIELD: u32 = 0x04;
const PIC_SHORT_TERM: u32 = 0x08;
const PIC_LONG_TERM: u32 = 0x10;

/// `VAPictureH264`.
#[repr(C)]
#[derive(Clone, Copy)]
struct VaPicture {
    picture_id: Id,
    frame_idx: u32,
    flags: u32,
    top_field_order_cnt: i32,
    bottom_field_order_cnt: i32,
    reserved: [u32; 4],
}
const _: () = assert!(std::mem::size_of::<VaPicture>() == 36);

impl VaPicture {
    const INVALID: VaPicture = VaPicture {
        picture_id: INVALID_ID,
        frame_idx: 0,
        flags: PIC_INVALID,
        top_field_order_cnt: 0,
        bottom_field_order_cnt: 0,
        reserved: [0; 4],
    };
}

/// `VAPictureParameterBufferH264`.
#[repr(C)]
struct PictureParams {
    curr_pic: VaPicture,
    reference_frames: [VaPicture; 16],
    picture_width_in_mbs_minus1: u16,
    picture_height_in_mbs_minus1: u16,
    bit_depth_luma_minus8: u8,
    bit_depth_chroma_minus8: u8,
    num_ref_frames: u8,
    seq_fields: u32,
    num_slice_groups_minus1: u8,
    slice_group_map_type: u8,
    slice_group_change_rate_minus1: u16,
    pic_init_qp_minus26: i8,
    pic_init_qs_minus26: i8,
    chroma_qp_index_offset: i8,
    second_chroma_qp_index_offset: i8,
    pic_fields: u32,
    frame_num: u16,
    reserved: [u32; 8],
}
const _: () = assert!(std::mem::size_of::<PictureParams>() == 672);
const _: () = assert!(std::mem::offset_of!(PictureParams, seq_fields) == 620);
const _: () = assert!(std::mem::offset_of!(PictureParams, pic_fields) == 632);
const _: () = assert!(std::mem::offset_of!(PictureParams, frame_num) == 636);

/// `VAIQMatrixBufferH264`.
#[repr(C)]
struct IqMatrix {
    scaling_list_4x4: [[u8; 16]; 6],
    scaling_list_8x8: [[u8; 64]; 2],
    reserved: [u32; 4],
}
const _: () = assert!(std::mem::size_of::<IqMatrix>() == 240);

/// `VASliceParameterBufferH264`.
#[repr(C)]
struct SliceParams {
    slice_data_size: u32,
    slice_data_offset: u32,
    slice_data_flag: u32,
    slice_data_bit_offset: u16,
    first_mb_in_slice: u16,
    slice_type: u8,
    direct_spatial_mv_pred_flag: u8,
    num_ref_idx_l0_active_minus1: u8,
    num_ref_idx_l1_active_minus1: u8,
    cabac_init_idc: u8,
    slice_qp_delta: i8,
    disable_deblocking_filter_idc: u8,
    slice_alpha_c0_offset_div2: i8,
    slice_beta_offset_div2: i8,
    ref_pic_list: [[VaPicture; 32]; 2],
    luma_log2_weight_denom: u8,
    chroma_log2_weight_denom: u8,
    luma_weight_l0_flag: u8,
    luma_weight_l0: [i16; 32],
    luma_offset_l0: [i16; 32],
    chroma_weight_l0_flag: u8,
    chroma_weight_l0: [[i16; 2]; 32],
    chroma_offset_l0: [[i16; 2]; 32],
    luma_weight_l1_flag: u8,
    luma_weight_l1: [i16; 32],
    luma_offset_l1: [i16; 32],
    chroma_weight_l1_flag: u8,
    chroma_weight_l1: [[i16; 2]; 32],
    chroma_offset_l1: [[i16; 2]; 32],
    reserved: [u32; 4],
}
const _: () = assert!(std::mem::size_of::<SliceParams>() == 3128);
const _: () = assert!(std::mem::offset_of!(SliceParams, ref_pic_list) == 28);
const _: () = assert!(std::mem::offset_of!(SliceParams, luma_weight_l0) == 2336);
const _: () = assert!(std::mem::offset_of!(SliceParams, chroma_weight_l0) == 2466);
const _: () = assert!(std::mem::offset_of!(SliceParams, luma_weight_l1_flag) == 2722);
const _: () = assert!(std::mem::offset_of!(SliceParams, chroma_offset_l1) == 2982);

/// The [`H264Accel`] that drives a VA context.
pub(crate) struct VaH264 {
    dev: Device,
    session: Option<Session>,
    /// Surface of the picture being decoded.
    target: Id,
}

impl VaH264 {
    pub(crate) fn new(dev: Device) -> Self {
        VaH264 {
            dev,
            session: None,
            target: INVALID_ID,
        }
    }

    /// A context for the SPS's coded size, created (or recreated) on demand.
    fn session(&mut self, sps: &Sps) -> Result<()> {
        if sps.chroma_format_idc != 1 || sps.bit_depth_luma != 8 || sps.bit_depth_chroma != 8 {
            return Err("the VAAPI H.264 path decodes 8-bit 4:2:0 only".into());
        }
        let (w, h) = (sps.width_mbs * 16, sps.height_mbs * 16);
        let key = (w, h, PROFILE_H264_HIGH, RT_FORMAT_YUV420);
        if self.session.as_ref().is_some_and(|s| s.key == key) {
            return Ok(());
        }
        if let Some(mut s) = self.session.take() {
            s.destroy(&self.dev);
        }
        // References plus the picture being decoded and a field waiting for
        // its pair, with room for the driver to pipeline.
        let n = (sps.max_num_ref_frames as usize + 4).clamp(6, 24);
        self.session = Some(Session::new(
            &self.dev,
            PROFILE_H264_HIGH,
            RT_FORMAT_YUV420,
            w,
            h,
            n,
        )?);
        Ok(())
    }

    fn surface_of(&self, store_id: u32) -> Option<Id> {
        self.session.as_ref()?.surface_of(store_id as i64)
    }

    /// A DPB entry, or a list entry of the given structure.
    fn picture(&self, p: &RefPic, structure: u8, long: bool, reference: bool) -> VaPicture {
        let Some(id) = self.surface_of(p.id) else {
            return VaPicture::INVALID;
        };
        let mut flags = match structure {
            1 => PIC_TOP_FIELD,
            2 => PIC_BOTTOM_FIELD,
            _ => 0,
        };
        if reference {
            flags |= if long { PIC_LONG_TERM } else { PIC_SHORT_TERM };
        }
        VaPicture {
            picture_id: id,
            frame_idx: if long { p.lt_idx } else { p.frame_num },
            flags,
            top_field_order_cnt: if structure & 1 != 0 {
                p.field_poc[0]
            } else {
                0
            },
            bottom_field_order_cnt: if structure & 2 != 0 {
                p.field_poc[1]
            } else {
                0
            },
            reserved: [0; 4],
        }
    }

    fn begin(&mut self, s: &AccelSlice<'_>) -> Result<()> {
        let (sps, pps, hdr) = (s.sps, s.pps, s.hdr);
        self.session(sps)?;
        let dpb = s.dpb;
        self.target = self
            .session
            .as_mut()
            .unwrap()
            .claim(s.store_id as i64, |k| dpb.iter().any(|p| p.id as i64 == k))?;
        let mut pp: PictureParams = unsafe { std::mem::zeroed() };
        let long_cur = hdr.long_term_reference;
        pp.curr_pic = VaPicture {
            picture_id: self.target,
            frame_idx: hdr.frame_num,
            flags: match s.structure {
                1 => PIC_TOP_FIELD,
                2 => PIC_BOTTOM_FIELD,
                _ => 0,
            } | if hdr.nal_ref_idc != 0 {
                if long_cur {
                    PIC_LONG_TERM
                } else {
                    PIC_SHORT_TERM
                }
            } else {
                0
            },
            top_field_order_cnt: if s.structure & 1 != 0 {
                s.field_poc[0]
            } else {
                0
            },
            bottom_field_order_cnt: if s.structure & 2 != 0 {
                s.field_poc[1]
            } else {
                0
            },
            reserved: [0; 4],
        };
        pp.reference_frames = [VaPicture::INVALID; 16];
        let mut n = 0;
        for p in s.dpb.iter().filter(|p| p.is_ref()) {
            if n == 16 {
                break;
            }
            let long = p.long != 0;
            let marked = if long { p.long } else { p.short };
            let v = self.picture(p, marked, long, true);
            if v.picture_id != INVALID_ID {
                pp.reference_frames[n] = v;
                n += 1;
            }
        }
        pp.picture_width_in_mbs_minus1 = (sps.width_mbs - 1) as u16;
        // The frame's height in macroblocks, field-coded or not, as the
        // drivers read it (ffmpeg's convention; Intel depends on it).
        pp.picture_height_in_mbs_minus1 = (sps.height_mbs - 1) as u16;
        pp.bit_depth_luma_minus8 = (sps.bit_depth_luma - 8) as u8;
        pp.bit_depth_chroma_minus8 = (sps.bit_depth_chroma - 8) as u8;
        pp.num_ref_frames = sps.max_num_ref_frames as u8;
        pp.seq_fields = sps.chroma_format_idc
            | (sps.gaps_allowed as u32) << 3
            | (sps.frame_mbs_only as u32) << 4
            | (sps.mbaff as u32) << 5
            | (sps.direct_8x8_inference as u32) << 6
            | (sps.log2_max_frame_num - 4) << 8
            | sps.poc_type << 12
            | (if sps.poc_type == 0 {
                sps.log2_max_poc_lsb - 4
            } else {
                0
            }) << 14
            | (sps.delta_pic_order_always_zero as u32) << 18;
        pp.pic_init_qp_minus26 = (pps.pic_init_qp - 26) as i8;
        pp.chroma_qp_index_offset = pps.chroma_qp_offset[0] as i8;
        pp.second_chroma_qp_index_offset = pps.chroma_qp_offset[1] as i8;
        pp.pic_fields = pps.cabac as u32
            | (pps.weighted_pred as u32) << 1
            | pps.weighted_bipred_idc << 2
            | (pps.transform_8x8_mode as u32) << 4
            | (hdr.field_pic as u32) << 5
            | (pps.constrained_intra_pred as u32) << 6
            | (pps.bottom_field_pic_order_in_frame_present as u32) << 7
            | (pps.deblocking_filter_control_present as u32) << 8
            | (pps.redundant_pic_cnt_present as u32) << 9
            | ((hdr.nal_ref_idc != 0) as u32) << 10;
        pp.frame_num = hdr.frame_num as u16;

        let flat = ScalingLists::flat();
        let lists = pps
            .scaling
            .as_ref()
            .or(sps.scaling.as_ref())
            .unwrap_or(&flat);
        let iq = IqMatrix {
            scaling_list_4x4: lists.m4,
            scaling_list_8x8: [lists.m8[0], lists.m8[1]],
            reserved: [0; 4],
        };

        let (dev, ses) = (&self.dev, self.session.as_mut().unwrap());
        ses.begin(dev, self.target)?;
        let mut ids = [
            ses.buffer(dev, BUF_PICTURE_PARAMETER, &pp)?,
            ses.buffer(dev, BUF_IQ_MATRIX, &iq)?,
        ];
        ses.render(dev, &mut ids)
    }
}

/// Bit offset of RBSP bit `rbsp_bits` (counted after the header byte) within
/// the coded NAL unit, emulation prevention bytes included.
fn coded_bit_offset(nal: &[u8], rbsp_bits: usize) -> usize {
    super::coded_offset(nal, 1, rbsp_bits / 8).0 * 8 + rbsp_bits % 8
}

impl H264Accel for VaH264 {
    fn slice(&mut self, s: &AccelSlice<'_>) -> Result<()> {
        if s.first {
            self.begin(s)?;
        }
        let hdr = s.hdr;
        let mut sp: SliceParams = unsafe { std::mem::zeroed() };
        sp.slice_data_size = s.nal.len() as u32;
        sp.slice_data_bit_offset = coded_bit_offset(s.nal, hdr.data_bit_offset) as u16;
        sp.first_mb_in_slice = hdr.first_mb as u16;
        sp.slice_type = match hdr.slice_type {
            SliceType::P => 0,
            SliceType::B => 1,
            SliceType::I => 2,
        };
        sp.direct_spatial_mv_pred_flag = hdr.direct_spatial as u8;
        sp.num_ref_idx_l0_active_minus1 = hdr.num_ref_idx[0].saturating_sub(1) as u8;
        sp.num_ref_idx_l1_active_minus1 = hdr.num_ref_idx[1].saturating_sub(1) as u8;
        sp.cabac_init_idc = hdr.cabac_init_idc as u8;
        sp.slice_qp_delta = (hdr.qp - s.pps.pic_init_qp) as i8;
        sp.disable_deblocking_filter_idc = hdr.disable_deblocking as u8;
        sp.slice_alpha_c0_offset_div2 = (hdr.alpha_offset / 2) as i8;
        sp.slice_beta_offset_div2 = (hdr.beta_offset / 2) as i8;
        sp.ref_pic_list = [[VaPicture::INVALID; 32]; 2];
        for l in 0..2 {
            for (i, e) in s.refs[l].iter().take(32).enumerate() {
                sp.ref_pic_list[l][i] = self.picture(&s.dpb[e.dpb], e.structure, e.long_term, true);
            }
        }
        // Default tables where the slice codes none, as other clients send
        // them: weight 1 << denom, offset 0, denominator 5 for implicit
        // bi-prediction. The flags stay 0, so they change nothing.
        if hdr.weights.is_none() {
            let implicit = hdr.slice_type == SliceType::B && s.pps.weighted_bipred_idc == 2;
            let denom = if implicit { 5 } else { 0 };
            sp.luma_log2_weight_denom = denom;
            sp.chroma_log2_weight_denom = denom;
            let one = 1i16 << denom;
            for i in 0..32 {
                sp.luma_weight_l0[i] = one;
                sp.luma_weight_l1[i] = one;
                sp.chroma_weight_l0[i] = [one; 2];
                sp.chroma_weight_l1[i] = [one; 2];
            }
        }
        if let Some(w) = &hdr.weights {
            sp.luma_log2_weight_denom = w.luma_log2_denom as u8;
            sp.chroma_log2_weight_denom = w.chroma_log2_denom as u8;
            let lists = if hdr.slice_type == SliceType::B { 2 } else { 1 };
            for l in 0..lists {
                for (i, wc) in w.weights[l].iter().take(32).enumerate() {
                    let (luma_w, luma_o) = wc[0];
                    let chroma = [
                        [wc[1].0 as i16, wc[2].0 as i16],
                        [wc[1].1 as i16, wc[2].1 as i16],
                    ];
                    if l == 0 {
                        sp.luma_weight_l0_flag = 1;
                        sp.chroma_weight_l0_flag = 1;
                        sp.luma_weight_l0[i] = luma_w as i16;
                        sp.luma_offset_l0[i] = luma_o as i16;
                        sp.chroma_weight_l0[i] = chroma[0];
                        sp.chroma_offset_l0[i] = chroma[1];
                    } else {
                        sp.luma_weight_l1_flag = 1;
                        sp.chroma_weight_l1_flag = 1;
                        sp.luma_weight_l1[i] = luma_w as i16;
                        sp.luma_offset_l1[i] = luma_o as i16;
                        sp.chroma_weight_l1[i] = chroma[0];
                        sp.chroma_offset_l1[i] = chroma[1];
                    }
                }
            }
        }
        let (dev, ses) = (&self.dev, self.session.as_mut().ok_or("no VA context")?);
        let mut ids = [
            ses.buffer(dev, BUF_SLICE_PARAMETER, &sp)?,
            ses.raw_buffer(dev, BUF_SLICE_DATA, s.nal.len(), s.nal.as_ptr().cast())?,
        ];
        ses.render(dev, &mut ids)
    }

    fn end_picture(&mut self) -> Result<()> {
        self.session.as_mut().ok_or("no VA context")?.end(&self.dev)
    }

    fn frame(&mut self, store_id: u32, sps: &Sps) -> Result<DecodedFrame> {
        let surface = self
            .surface_of(store_id)
            .ok_or("no VA surface holds that picture")?;
        let (w, h, ..) = self.session.as_ref().unwrap().key;
        let crop = sps.crop.map(|v| v as usize);
        let color = ColorInfo::from_vui(
            sps.matrix_coefficients,
            sps.full_range,
            h - sps.crop[2] - sps.crop[3],
        );
        self.dev
            .read_frame(surface, w as usize, h as usize, false, crop, color)
    }
}

impl Drop for VaH264 {
    fn drop(&mut self) {
        if let Some(mut s) = self.session.take() {
            s.destroy(&self.dev);
        }
    }
}