forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiB9b67700Tahinli3 days ago4021 commits

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