From 8c693a296e816e8f885dcb20492a915da359370d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Wed, 2 Dec 2026 15:16:02 +0300 Subject: [PATCH] S2: packed header kind is a PackedKind enum, not u32 constants; the unknown-kind error arm is gone with it --- crates/ec-hw/src/enc/headers.rs | 42 ++++++++++++++++----------------- crates/ec-hw/src/enc/mod.rs | 9 +++---- 2 files changed, 23 insertions(+), 28 deletions(-) diff --git a/crates/ec-hw/src/enc/headers.rs b/crates/ec-hw/src/enc/headers.rs index a9023700..ef1d7d19 100644 --- a/crates/ec-hw/src/enc/headers.rs +++ b/crates/ec-hw/src/enc/headers.rs @@ -10,12 +10,12 @@ //! driver was given, or the header describes a picture other than the one the //! hardware coded. -/// `VAEncPackedHeaderSequence`: packed sequence-level header bits. -pub const PACKED_HEADER_SEQUENCE: u32 = 1; -/// `VAEncPackedHeaderPicture`: packed picture-level header bits. -pub const PACKED_HEADER_PICTURE: u32 = 2; -/// `VAEncPackedHeaderSlice`: packed slice-level header bits. -pub const PACKED_HEADER_SLICE: u32 = 3; +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub(crate) enum PackedKind { + Sequence, + Picture, + Slice, +} use ec_core::BitWriter; @@ -26,13 +26,13 @@ use super::Colour; /// The bit length matters for slice headers, which do not end on a byte /// boundary — the driver continues the bitstream from exactly that bit. pub(crate) struct Packed { - pub(crate) kind: u32, + pub(crate) kind: PackedKind, pub(crate) bytes: Vec, pub(crate) bits: u32, } /// Wrap a complete NAL: start code, header byte(s), escaped payload. -fn nal(kind: u32, header: &[u8], rbsp: &[u8], padding_bits: u32) -> Packed { +fn nal(kind: PackedKind, header: &[u8], rbsp: &[u8], padding_bits: u32) -> Packed { let mut bytes = vec![0, 0, 0, 1]; bytes.extend_from_slice(header); let mut escaped = Vec::with_capacity(rbsp.len() + 8); @@ -133,7 +133,7 @@ pub(super) fn h264_sps(p: &H264Params) -> Packed { w.write_ue(0); // max_num_reorder_frames w.write_ue(1); // max_dec_frame_buffering = max_num_ref_frames trailing(&mut w); - nal(PACKED_HEADER_SEQUENCE, &[0x67], &w.into_bytes(), 0) + nal(PackedKind::Sequence, &[0x67], &w.into_bytes(), 0) } /// The picture parameter set (7.3.2.2). @@ -160,7 +160,7 @@ pub(super) fn h264_pps(p: &H264Params) -> Packed { w.write_se(0); // second_chroma_qp_index_offset } trailing(&mut w); - nal(PACKED_HEADER_PICTURE, &[0x68], &w.into_bytes(), 0) + nal(PackedKind::Picture, &[0x68], &w.into_bytes(), 0) } /// The slice header (7.3.3), up to but *not* including the slice data. @@ -202,7 +202,7 @@ pub(super) fn h264_slice_header(idr: bool, frame_num: u32, idr_pic_id: u32) -> P let padding = (8 - bits % 8) % 8; w.align_to_byte(); let header = if idr { 0x65 } else { 0x61 }; - nal(PACKED_HEADER_SLICE, &[header], &w.into_bytes(), padding) + nal(PackedKind::Slice, &[header], &w.into_bytes(), padding) } /// `rbsp_trailing_bits` (7.3.2.11). @@ -220,8 +220,7 @@ fn trailing(w: &mut BitWriter) { /// The syntax crate writes all four structures, so the encoder and the software /// HEVC path in this family share one definition of what a parameter set is. pub(super) mod hevc { - #[allow(unused_imports)] - use super::{PACKED_HEADER_PICTURE, PACKED_HEADER_SEQUENCE, PACKED_HEADER_SLICE}; + use super::PackedKind; use ec_h265_syntax::{ ConformanceWindow, NalHeader, NalUnitType, Pps, ProfileTierLevel, ShortTermRefPicSet, SliceHeader, SliceType, Sps, Vps, @@ -350,7 +349,7 @@ pub(super) mod hevc { pub(in crate::enc) fn packed_vps(vps: &Vps) -> Packed { nal( - PACKED_HEADER_SEQUENCE, + PackedKind::Sequence, &NalHeader::new(NalUnitType::Vps).to_bytes(), &vps.to_rbsp(), 0, @@ -359,7 +358,7 @@ pub(super) mod hevc { pub(in crate::enc) fn packed_sps(sps: &Sps) -> Packed { nal( - PACKED_HEADER_SEQUENCE, + PackedKind::Sequence, &NalHeader::new(NalUnitType::Sps).to_bytes(), &sps.to_rbsp(), 0, @@ -368,7 +367,7 @@ pub(super) mod hevc { pub(in crate::enc) fn packed_pps(pps: &Pps) -> Packed { nal( - PACKED_HEADER_PICTURE, + PackedKind::Picture, &NalHeader::new(NalUnitType::Pps).to_bytes(), &pps.to_rbsp(), 0, @@ -399,7 +398,7 @@ pub(super) mod hevc { // alignment bit has to be in what it is given. header.write(&mut w, sps, pps, nal_type); nal( - PACKED_HEADER_SLICE, + PackedKind::Slice, &NalHeader::new(nal_type).to_bytes(), &w.into_bytes(), 0, @@ -422,8 +421,7 @@ pub(super) mod hevc { /// syntax: a field written here that disagrees with the parameter buffer is a /// picture described two ways, and the driver believes this one. pub(super) mod av1 { - #[allow(unused_imports)] - use super::{PACKED_HEADER_PICTURE, PACKED_HEADER_SEQUENCE, PACKED_HEADER_SLICE}; + use super::PackedKind; use ec_core::BitWriter; use super::{Colour, Packed}; @@ -438,7 +436,7 @@ pub(super) mod av1 { const OBU_SIZE_BYTES: usize = 2; /// Wrap a payload as one OBU: header byte, fixed-width `obu_size`, payload. - fn obu(kind: u32, obu_type: u8, payload: Vec) -> Packed { + fn obu(kind: PackedKind, obu_type: u8, payload: Vec) -> Packed { let mut bytes = Vec::with_capacity(payload.len() + 1 + OBU_SIZE_BYTES); // forbidden(0) type(4) extension(0) has_size(1) reserved(0) bytes.push(obu_type << 3 | 0b010); @@ -538,7 +536,7 @@ pub(super) mod av1 { w.write_bit(false); // separate_uv_delta_q w.write_bit(false); // film_grain_params_present trailing(&mut w); - obu(PACKED_HEADER_SEQUENCE, 1, w.into_bytes()) + obu(PackedKind::Sequence, 1, w.into_bytes()) } /// What one picture's uncompressed header says. @@ -649,7 +647,7 @@ pub(super) mod av1 { } } trailing(&mut w); - let packed = obu(PACKED_HEADER_PICTURE, 3, w.into_bytes()); + let packed = obu(PackedKind::Picture, 3, w.into_bytes()); let offsets = FrameHeaderOffsets { qindex, segmentation, diff --git a/crates/ec-hw/src/enc/mod.rs b/crates/ec-hw/src/enc/mod.rs index eb2e9aa2..9f2ed5b9 100644 --- a/crates/ec-hw/src/enc/mod.rs +++ b/crates/ec-hw/src/enc/mod.rs @@ -593,12 +593,9 @@ impl Encoder { out: &mut Vec, ) -> Result<()> { let kind = match packed.kind { - headers::PACKED_HEADER_SEQUENCE => EncPackedHeaderType::Sequence, - headers::PACKED_HEADER_PICTURE => EncPackedHeaderType::Picture, - headers::PACKED_HEADER_SLICE => EncPackedHeaderType::Slice, - other => { - return Err(Error::config(format!("unknown packed header kind {other}"))); - } + headers::PackedKind::Sequence => EncPackedHeaderType::Sequence, + headers::PackedKind::Picture => EncPackedHeaderType::Picture, + headers::PackedKind::Slice => EncPackedHeaderType::Slice, }; out.push(BufferType::EncPackedHeaderParameter( EncPackedHeaderParameter::new(kind, packed.bits, true),