forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiB00c1b3fTahinli2 months ago1999 commits

h264.rs376 lines, 14.2 KiBHistoryRawDownload
1//! H.264 decode parameter buffers (`va.h:3572-3716`).
2
3use ec_va::sys::{
4 VAIQMatrixBufferType, VAPictureParameterBufferType, VASliceParameterBufferType, VASurfaceID,
5};
6
7use super::{INVALID_SURFACE, VaParam};
8use crate::va_bits;
9
10/// `VA_PICTURE_H264_INVALID` (`va.h:3583`).
11pub const PICTURE_INVALID: u32 = 0x0000_0001;
12/// `VA_PICTURE_H264_TOP_FIELD`.
13pub const PICTURE_TOP_FIELD: u32 = 0x0000_0002;
14/// `VA_PICTURE_H264_BOTTOM_FIELD`.
15pub const PICTURE_BOTTOM_FIELD: u32 = 0x0000_0004;
16/// `VA_PICTURE_H264_SHORT_TERM_REFERENCE`.
17pub const PICTURE_SHORT_TERM_REFERENCE: u32 = 0x0000_0008;
18/// `VA_PICTURE_H264_LONG_TERM_REFERENCE`.
19pub const PICTURE_LONG_TERM_REFERENCE: u32 = 0x0000_0010;
20
21/// `VAPictureH264`, `va.h:3572`.
22#[repr(C)]
23#[derive(Debug, Clone, Copy, PartialEq, Eq)]
24pub struct VAPictureH264 {
25 /// Surface holding the picture, or [`INVALID_SURFACE`].
26 pub picture_id: VASurfaceID,
27 /// `FrameNum` (short term) or `LongTermFrameIdx` (long term).
28 pub frame_idx: u32,
29 /// `VA_PICTURE_H264_*` bitmask.
30 pub flags: u32,
31 /// `TopFieldOrderCnt`.
32 pub top_field_order_cnt: i32,
33 /// `BottomFieldOrderCnt`.
34 pub bottom_field_order_cnt: i32,
35 pub(crate) va_reserved: [u32; 4],
36}
37
38impl VAPictureH264 {
39 /// The "no picture here" entry every unused list slot must carry.
40 ///
41 /// Not `Default::default()` by accident: a zeroed entry names surface 0,
42 /// which is a real surface id, and drivers have been known to read it.
43 pub const INVALID: VAPictureH264 = VAPictureH264 {
44 picture_id: INVALID_SURFACE,
45 frame_idx: 0,
46 flags: PICTURE_INVALID,
47 top_field_order_cnt: 0,
48 bottom_field_order_cnt: 0,
49 va_reserved: [0; 4],
50 };
51
52 /// A frame picture with both field order counts set to its POC.
53 pub fn frame(picture_id: VASurfaceID, frame_idx: u32, poc: i32, flags: u32) -> VAPictureH264 {
54 VAPictureH264 {
55 picture_id,
56 frame_idx,
57 flags,
58 top_field_order_cnt: poc,
59 bottom_field_order_cnt: poc,
60 va_reserved: [0; 4],
61 }
62 }
63}
64
65impl Default for VAPictureH264 {
66 fn default() -> Self {
67 VAPictureH264::INVALID
68 }
69}
70
71va_bits! {
72 /// `VAPictureParameterBufferH264::seq_fields`.
73 SeqFields: u32 {
74 chroma_format_idc: 2,
75 residual_colour_transform_flag: 1,
76 gaps_in_frame_num_value_allowed_flag: 1,
77 frame_mbs_only_flag: 1,
78 mb_adaptive_frame_field_flag: 1,
79 direct_8x8_inference_flag: 1,
80 min_luma_bipred_size8x8: 1,
81 log2_max_frame_num_minus4: 4,
82 pic_order_cnt_type: 2,
83 log2_max_pic_order_cnt_lsb_minus4: 4,
84 delta_pic_order_always_zero_flag: 1,
85 }
86}
87
88va_bits! {
89 /// `VAPictureParameterBufferH264::pic_fields`.
90 PicFields: u32 {
91 entropy_coding_mode_flag: 1,
92 weighted_pred_flag: 1,
93 weighted_bipred_idc: 2,
94 transform_8x8_mode_flag: 1,
95 field_pic_flag: 1,
96 constrained_intra_pred_flag: 1,
97 pic_order_present_flag: 1,
98 deblocking_filter_control_present_flag: 1,
99 redundant_pic_cnt_present_flag: 1,
100 reference_pic_flag: 1,
101 }
102}
103
104/// `VAPictureParameterBufferH264`, `va.h:3594`.
105#[repr(C)]
106#[derive(Debug, Clone, Copy)]
107pub struct PictureParameterBufferH264 {
108 /// The picture being decoded.
109 pub curr_pic: VAPictureH264,
110 /// Every picture in the DPB; unused slots are [`VAPictureH264::INVALID`].
111 pub reference_frames: [VAPictureH264; 16],
112 /// `PicWidthInMbs - 1`.
113 pub picture_width_in_mbs_minus1: u16,
114 /// `PicHeightInMapUnits - 1` for a frame-only stream.
115 pub picture_height_in_mbs_minus1: u16,
116 /// `bit_depth_luma_minus8`.
117 pub bit_depth_luma_minus8: u8,
118 /// `bit_depth_chroma_minus8`.
119 pub bit_depth_chroma_minus8: u8,
120 /// `max_num_ref_frames`.
121 pub num_ref_frames: u8,
122 /// Sequence flags.
123 pub seq_fields: SeqFields,
124 /// Deprecated FMO fields; libva ignores them, we write zero.
125 pub(crate) num_slice_groups_minus1: u8,
126 pub(crate) slice_group_map_type: u8,
127 pub(crate) slice_group_change_rate_minus1: u16,
128 /// `pic_init_qp_minus26`.
129 pub pic_init_qp_minus26: i8,
130 /// `pic_init_qs_minus26`.
131 pub pic_init_qs_minus26: i8,
132 /// `chroma_qp_index_offset`.
133 pub chroma_qp_index_offset: i8,
134 /// `second_chroma_qp_index_offset`.
135 pub second_chroma_qp_index_offset: i8,
136 /// Picture flags.
137 pub pic_fields: PicFields,
138 /// `frame_num` of the current picture.
139 pub frame_num: u16,
140 pub(crate) va_reserved: [u32; 8],
141}
142
143impl Default for PictureParameterBufferH264 {
144 fn default() -> Self {
145 PictureParameterBufferH264 {
146 curr_pic: VAPictureH264::INVALID,
147 reference_frames: [VAPictureH264::INVALID; 16],
148 picture_width_in_mbs_minus1: 0,
149 picture_height_in_mbs_minus1: 0,
150 bit_depth_luma_minus8: 0,
151 bit_depth_chroma_minus8: 0,
152 num_ref_frames: 0,
153 seq_fields: SeqFields::default(),
154 num_slice_groups_minus1: 0,
155 slice_group_map_type: 0,
156 slice_group_change_rate_minus1: 0,
157 pic_init_qp_minus26: 0,
158 pic_init_qs_minus26: 0,
159 chroma_qp_index_offset: 0,
160 second_chroma_qp_index_offset: 0,
161 pic_fields: PicFields::default(),
162 frame_num: 0,
163 va_reserved: [0; 8],
164 }
165 }
166}
167
168// SAFETY: `#[repr(C)]` transcription of `VAPictureParameterBufferH264`, checked
169// field for field by the assertion block below; every field is an integer or an
170// array of integers, so any bit pattern is valid.
171unsafe impl VaParam for PictureParameterBufferH264 {
172 const TYPE: i32 = VAPictureParameterBufferType;
173}
174
175/// `VAIQMatrixBufferH264`, `va.h:3648`.
176#[repr(C)]
177#[derive(Debug, Clone, Copy)]
178pub struct IQMatrixBufferH264 {
179 /// 4x4 scaling lists in raster scan order.
180 pub scaling_list_4x4: [[u8; 16]; 6],
181 /// 8x8 scaling lists in raster scan order.
182 pub scaling_list_8x8: [[u8; 64]; 2],
183 pub(crate) va_reserved: [u32; 4],
184}
185
186impl Default for IQMatrixBufferH264 {
187 fn default() -> Self {
188 IQMatrixBufferH264 {
189 scaling_list_4x4: [[16; 16]; 6],
190 scaling_list_8x8: [[16; 64]; 2],
191 va_reserved: [0; 4],
192 }
193 }
194}
195
196// SAFETY: as `PictureParameterBufferH264`; plain `u8` arrays plus padding.
197unsafe impl VaParam for IQMatrixBufferH264 {
198 const TYPE: i32 = VAIQMatrixBufferType;
199}
200
201/// `VASliceParameterBufferH264`, `va.h:3659`.
202///
203/// The weight tables are the bulk of it: 3 KB per slice, which is why the
204/// decoder builds one on the stack and hands it straight to `vaCreateBuffer`
205/// rather than keeping a queue of them.
206#[repr(C)]
207#[derive(Debug, Clone, Copy)]
208pub struct SliceParameterBufferH264 {
209 /// Bytes of slice data for this slice.
210 pub slice_data_size: u32,
211 /// Offset of this slice's NAL unit within the data buffer.
212 pub slice_data_offset: u32,
213 /// `VA_SLICE_DATA_FLAG_*`.
214 pub slice_data_flag: u32,
215 /// Bits from the start of the NAL unit byte to `slice_data()`, counted in
216 /// the *unescaped* RBSP.
217 pub slice_data_bit_offset: u16,
218 /// `first_mb_in_slice`.
219 pub first_mb_in_slice: u16,
220 /// `slice_type` (0..=4, i.e. the modulo-5 value).
221 pub slice_type: u8,
222 /// `direct_spatial_mv_pred_flag`.
223 pub direct_spatial_mv_pred_flag: u8,
224 /// `num_ref_idx_l0_active_minus1`, override applied.
225 pub num_ref_idx_l0_active_minus1: u8,
226 /// `num_ref_idx_l1_active_minus1`, override applied.
227 pub num_ref_idx_l1_active_minus1: u8,
228 /// `cabac_init_idc`.
229 pub cabac_init_idc: u8,
230 /// `slice_qp_delta`.
231 pub slice_qp_delta: i8,
232 /// `disable_deblocking_filter_idc`.
233 pub disable_deblocking_filter_idc: u8,
234 /// `slice_alpha_c0_offset_div2`.
235 pub slice_alpha_c0_offset_div2: i8,
236 /// `slice_beta_offset_div2`.
237 pub slice_beta_offset_div2: i8,
238 /// `RefPicList0` after initialisation and modification (8.2.4).
239 pub ref_pic_list0: [VAPictureH264; 32],
240 /// `RefPicList1`.
241 pub ref_pic_list1: [VAPictureH264; 32],
242 /// `luma_log2_weight_denom`.
243 pub luma_log2_weight_denom: u8,
244 /// `chroma_log2_weight_denom`.
245 pub chroma_log2_weight_denom: u8,
246 /// Whether any `luma_weight_l0_flag` was set.
247 pub luma_weight_l0_flag: u8,
248 /// `luma_weight_l0`.
249 pub luma_weight_l0: [i16; 32],
250 /// `luma_offset_l0`.
251 pub luma_offset_l0: [i16; 32],
252 /// Whether any `chroma_weight_l0_flag` was set.
253 pub chroma_weight_l0_flag: u8,
254 /// `chroma_weight_l0`.
255 pub chroma_weight_l0: [[i16; 2]; 32],
256 /// `chroma_offset_l0`.
257 pub chroma_offset_l0: [[i16; 2]; 32],
258 /// Whether any `luma_weight_l1_flag` was set.
259 pub luma_weight_l1_flag: u8,
260 /// `luma_weight_l1`.
261 pub luma_weight_l1: [i16; 32],
262 /// `luma_offset_l1`.
263 pub luma_offset_l1: [i16; 32],
264 /// Whether any `chroma_weight_l1_flag` was set.
265 pub chroma_weight_l1_flag: u8,
266 /// `chroma_weight_l1`.
267 pub chroma_weight_l1: [[i16; 2]; 32],
268 /// `chroma_offset_l1`.
269 pub chroma_offset_l1: [[i16; 2]; 32],
270 pub(crate) va_reserved: [u32; 4],
271}
272
273impl Default for SliceParameterBufferH264 {
274 fn default() -> Self {
275 SliceParameterBufferH264 {
276 slice_data_size: 0,
277 slice_data_offset: 0,
278 slice_data_flag: 0,
279 slice_data_bit_offset: 0,
280 first_mb_in_slice: 0,
281 slice_type: 0,
282 direct_spatial_mv_pred_flag: 0,
283 num_ref_idx_l0_active_minus1: 0,
284 num_ref_idx_l1_active_minus1: 0,
285 cabac_init_idc: 0,
286 slice_qp_delta: 0,
287 disable_deblocking_filter_idc: 0,
288 slice_alpha_c0_offset_div2: 0,
289 slice_beta_offset_div2: 0,
290 ref_pic_list0: [VAPictureH264::INVALID; 32],
291 ref_pic_list1: [VAPictureH264::INVALID; 32],
292 luma_log2_weight_denom: 0,
293 chroma_log2_weight_denom: 0,
294 luma_weight_l0_flag: 0,
295 luma_weight_l0: [0; 32],
296 luma_offset_l0: [0; 32],
297 chroma_weight_l0_flag: 0,
298 chroma_weight_l0: [[0; 2]; 32],
299 chroma_offset_l0: [[0; 2]; 32],
300 luma_weight_l1_flag: 0,
301 luma_weight_l1: [0; 32],
302 luma_offset_l1: [0; 32],
303 chroma_weight_l1_flag: 0,
304 chroma_weight_l1: [[0; 2]; 32],
305 chroma_offset_l1: [[0; 2]; 32],
306 va_reserved: [0; 4],
307 }
308 }
309}
310
311// SAFETY: as `PictureParameterBufferH264`.
312unsafe impl VaParam for SliceParameterBufferH264 {
313 const TYPE: i32 = VASliceParameterBufferType;
314}
315
316// ---------------------------------------------------------------------------
317// ABI transcription check — `crates/ec-hw/abi-probe.c`, libva 1.23.0, x86_64:
318//
319// VAPictureH264 size=36 align=4 frame_idx=4 flags=8
320// TopFieldOrderCnt=12
321// BottomFieldOrderCnt=16
322// VAPictureParameterBufferH264 size=672 align=4 ReferenceFrames=36
323// picture_width_in_mbs_minus1=612
324// bit_depth_luma_minus8=616
325// num_ref_frames=618 seq_fields=620
326// pic_init_qp_minus26=628
327// pic_fields=632 frame_num=636
328// VAIQMatrixBufferH264 size=240 align=4 ScalingList8x8=96
329// VASliceParameterBufferH264 size=3128 align=4 slice_data_bit_offset=12
330// slice_type=16 cabac_init_idc=20
331// RefPicList0=28 RefPicList1=1180
332// luma_log2_weight_denom=2332
333// luma_weight_l0=2336
334// chroma_weight_l0=2466
335// luma_weight_l1_flag=2722
336// chroma_offset_l1=2982
337const _: () = {
338 use std::mem::{align_of, offset_of, size_of};
339
340 assert!(size_of::<VAPictureH264>() == 36 && align_of::<VAPictureH264>() == 4);
341 assert!(offset_of!(VAPictureH264, frame_idx) == 4);
342 assert!(offset_of!(VAPictureH264, flags) == 8);
343 assert!(offset_of!(VAPictureH264, top_field_order_cnt) == 12);
344 assert!(offset_of!(VAPictureH264, bottom_field_order_cnt) == 16);
345
346 assert!(
347 size_of::<PictureParameterBufferH264>() == 672
348 && align_of::<PictureParameterBufferH264>() == 4
349 );
350 assert!(offset_of!(PictureParameterBufferH264, reference_frames) == 36);
351 assert!(offset_of!(PictureParameterBufferH264, picture_width_in_mbs_minus1) == 612);
352 assert!(offset_of!(PictureParameterBufferH264, bit_depth_luma_minus8) == 616);
353 assert!(offset_of!(PictureParameterBufferH264, num_ref_frames) == 618);
354 assert!(offset_of!(PictureParameterBufferH264, seq_fields) == 620);
355 assert!(offset_of!(PictureParameterBufferH264, pic_init_qp_minus26) == 628);
356 assert!(offset_of!(PictureParameterBufferH264, pic_fields) == 632);
357 assert!(offset_of!(PictureParameterBufferH264, frame_num) == 636);
358
359 assert!(size_of::<IQMatrixBufferH264>() == 240 && align_of::<IQMatrixBufferH264>() == 4);
360 assert!(offset_of!(IQMatrixBufferH264, scaling_list_8x8) == 96);
361
362 assert!(
363 size_of::<SliceParameterBufferH264>() == 3128
364 && align_of::<SliceParameterBufferH264>() == 4
365 );
366 assert!(offset_of!(SliceParameterBufferH264, slice_data_bit_offset) == 12);
367 assert!(offset_of!(SliceParameterBufferH264, slice_type) == 16);
368 assert!(offset_of!(SliceParameterBufferH264, cabac_init_idc) == 20);
369 assert!(offset_of!(SliceParameterBufferH264, ref_pic_list0) == 28);
370 assert!(offset_of!(SliceParameterBufferH264, ref_pic_list1) == 1180);
371 assert!(offset_of!(SliceParameterBufferH264, luma_log2_weight_denom) == 2332);
372 assert!(offset_of!(SliceParameterBufferH264, luma_weight_l0) == 2336);
373 assert!(offset_of!(SliceParameterBufferH264, chroma_weight_l0) == 2466);
374 assert!(offset_of!(SliceParameterBufferH264, luma_weight_l1_flag) == 2722);
375 assert!(offset_of!(SliceParameterBufferH264, chroma_offset_l1) == 2982);
376};