From c58ee896b647127ec129183d994ae91fc18d3fe8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Wed, 25 Nov 2026 08:45:23 +0300 Subject: [PATCH] ec-hw: H.264 DPB generic over the surface type and ungated (dpb264.rs, h264_common.rs), for the Vulkan decoder --- crates/ec-hw/src/dec/h264.rs | 33 +++++----------------------- crates/ec-hw/src/dec/mod.rs | 1 - crates/ec-hw/src/{dec => }/dpb264.rs | 27 +++++++++++------------ crates/ec-hw/src/h264_common.rs | 31 ++++++++++++++++++++++++++ crates/ec-hw/src/lib.rs | 4 ++++ 5 files changed, 53 insertions(+), 43 deletions(-) rename crates/ec-hw/src/{dec => }/dpb264.rs (99%) create mode 100644 crates/ec-hw/src/h264_common.rs diff --git a/crates/ec-hw/src/dec/h264.rs b/crates/ec-hw/src/dec/h264.rs index 5e64326b..c9870e49 100644 --- a/crates/ec-hw/src/dec/h264.rs +++ b/crates/ec-hw/src/dec/h264.rs @@ -10,10 +10,11 @@ use ec_h264_syntax::{ use ec_va::caps::Profile; use ec_va::{Buffer, Display}; -use super::dpb264::{Dpb, Mark, PicInfo, Picture, Poc, RefList}; use super::{ReadyFrames, Session, StreamInfo}; +use crate::dpb264::{self, Mark, PicInfo, Poc, RefList}; use crate::error::{Error, Result}; use crate::frame::{Colour, Frame}; +use crate::h264_common::{peek_pps_id, vui_colour_tags}; use crate::params::h264::{ IQMatrixBufferH264, PICTURE_LONG_TERM_REFERENCE, PICTURE_SHORT_TERM_REFERENCE, PicFields, PictureParameterBufferH264, SeqFields, SliceParameterBufferH264, VAPictureH264, @@ -75,6 +76,9 @@ struct Current { ref_padded: bool, } +type Dpb = dpb264::Dpb>; +type Picture = dpb264::Picture>; + /// A stateless H.264 decoder. pub struct H264Decoder { display: Rc, @@ -489,33 +493,6 @@ impl H264Decoder { } } -/// An SPS's VUI `colour_description` and range flag, as [`Tags`] for -/// [`ColorDescription::resolve`]. [`Tags::default()`] when the VUI has no -/// `video_signal_type` at all. -fn vui_colour_tags(sps: &Sps) -> Tags { - let Some(vui) = &sps.vui else { - return Tags::default(); - }; - if vui.video_format.is_none() { - return Tags::default(); - } - let (_primaries, transfer, matrix) = vui.colour_description.unwrap_or((0, 0, 0)); - Tags::from_codes( - u64::from(matrix), - u64::from(transfer), - if vui.video_full_range { 2 } else { 1 }, - ) -} - -/// The `pic_parameter_set_id` of a slice header, which is the second syntax -/// element and needs no parameter set to read. -fn peek_pps_id(rbsp: &[u8]) -> Result { - let mut r = ec_core::BitReader::new(rbsp); - r.read_ue()?; // first_mb_in_slice - r.read_ue()?; // slice_type - Ok(r.read_ue()?) -} - /// The VA profile for a stream, from its SPS. fn profile_for(sps: &Sps) -> Result { Ok(match sps.profile_idc { diff --git a/crates/ec-hw/src/dec/mod.rs b/crates/ec-hw/src/dec/mod.rs index 215fa262..ded51045 100644 --- a/crates/ec-hw/src/dec/mod.rs +++ b/crates/ec-hw/src/dec/mod.rs @@ -33,7 +33,6 @@ use crate::pool::SurfacePool; pub use crate::types::Codec; mod av1; -mod dpb264; mod h264; mod vp9; diff --git a/crates/ec-hw/src/dec/dpb264.rs b/crates/ec-hw/src/dpb264.rs similarity index 99% rename from crates/ec-hw/src/dec/dpb264.rs rename to crates/ec-hw/src/dpb264.rs index 7de0ccfe..ad51b747 100644 --- a/crates/ec-hw/src/dec/dpb264.rs +++ b/crates/ec-hw/src/dpb264.rs @@ -7,12 +7,9 @@ //! told the whole buffer on every picture, so these lists are the parameter //! buffer's contents rather than an internal detail. -use std::rc::Rc; - use ec_h264_syntax::{DecRefPicMarking, RefPicListMod, SliceType, Sps}; use crate::error::{Error, Result}; -use crate::pool::PooledSurface; /// Maximum pictures held at once (Annex A caps the DPB at 16 frames; the extra /// slot is the picture being decoded). @@ -30,11 +27,11 @@ pub(crate) enum Mark { } /// One picture in the buffer. -pub(crate) struct Picture { +pub(crate) struct Picture { /// Identity, so "the picture just stored" survives reordering. pub id: i32, /// The surface it decoded into. - pub surface: Rc, + pub surface: S, /// Presentation timestamp of the access unit it came from. pub timestamp: i64, /// `frame_num`. @@ -62,7 +59,7 @@ pub(crate) struct Picture { pub output: bool, } -impl Picture { +impl Picture { fn stored(&self) -> bool { self.output || self.mark != Mark::Unused } @@ -124,8 +121,8 @@ pub(crate) struct PicInfo { } /// The decoded picture buffer and the cross-picture state of clause 8.2. -pub(crate) struct Dpb { - pub frames: Vec, +pub(crate) struct Dpb { + pub frames: Vec>, next_id: i32, max_frames: usize, max_reorder: usize, @@ -141,8 +138,8 @@ pub(crate) struct Dpb { last_id: i32, } -impl Default for Dpb { - fn default() -> Dpb { +impl Default for Dpb { + fn default() -> Dpb { Dpb { frames: Vec::new(), next_id: 0, @@ -162,7 +159,7 @@ impl Default for Dpb { } } -impl Dpb { +impl Dpb { /// Adopt the buffer limits of `sps` (Annex A / VUI). pub(crate) fn configure(&mut self, sps: &Sps) { let level_frames = max_dpb_frames(sps); @@ -335,7 +332,7 @@ impl Dpb { .frames .iter() .max_by_key(|p| p.frame_num_wrap) - .map(|p| Rc::clone(&p.surface)) + .map(|p| p.surface.clone()) else { // Nothing decoded yet: there is no picture to stand in for the // missing ones, and the next IDR will resynchronise anyway. @@ -597,7 +594,7 @@ impl Dpb { /// a reference and for bookkeeping, but must never be handed to a caller. pub(crate) fn store( &mut self, - mut pic: Picture, + mut pic: Picture, sps: &Sps, info: &PicInfo, marking: Option<&DecRefPicMarking>, @@ -699,7 +696,7 @@ impl Dpb { /// Adaptive marking (8.2.5.4). fn apply_mmco( &mut self, - pic: &mut Picture, + pic: &mut Picture, m: &DecRefPicMarking, info: &PicInfo, ) -> Result<()> { @@ -878,6 +875,8 @@ pub(crate) fn gap_frame_nums(prev_ref: u32, frame_num: u32, max: u32) -> impl It mod tests { use super::*; + type Dpb = super::Dpb; + fn sps_for(poc_type: u8) -> Sps { let mut sps = Sps { profile_idc: 100, diff --git a/crates/ec-hw/src/h264_common.rs b/crates/ec-hw/src/h264_common.rs new file mode 100644 index 00000000..4b8cd83f --- /dev/null +++ b/crates/ec-hw/src/h264_common.rs @@ -0,0 +1,31 @@ +use ec_core::color::Tags; +use ec_h264_syntax::Sps; + +use crate::error::Result; + +/// An SPS's VUI `colour_description` and range flag, as [`Tags`] for +/// [`ec_core::color::ColorDescription::resolve`]. [`Tags::default()`] when the VUI has no +/// `video_signal_type` at all. +pub(crate) fn vui_colour_tags(sps: &Sps) -> Tags { + let Some(vui) = &sps.vui else { + return Tags::default(); + }; + if vui.video_format.is_none() { + return Tags::default(); + } + let (_primaries, transfer, matrix) = vui.colour_description.unwrap_or((0, 0, 0)); + Tags::from_codes( + u64::from(matrix), + u64::from(transfer), + if vui.video_full_range { 2 } else { 1 }, + ) +} + +/// The `pic_parameter_set_id` of a slice header, which is the second syntax +/// element and needs no parameter set to read. +pub(crate) fn peek_pps_id(rbsp: &[u8]) -> Result { + let mut r = ec_core::BitReader::new(rbsp); + r.read_ue()?; // first_mb_in_slice + r.read_ue()?; // slice_type + Ok(r.read_ue()?) +} diff --git a/crates/ec-hw/src/lib.rs b/crates/ec-hw/src/lib.rs index 2de91e34..b6e163e9 100644 --- a/crates/ec-hw/src/lib.rs +++ b/crates/ec-hw/src/lib.rs @@ -59,6 +59,10 @@ pub mod dec; #[cfg(target_os = "linux")] pub mod enc; pub mod error; +#[cfg_attr(not(target_os = "linux"), allow(dead_code))] +pub(crate) mod dpb264; +#[cfg_attr(not(target_os = "linux"), allow(dead_code))] +pub(crate) mod h264_common; pub mod hw; #[cfg(target_os = "linux")] pub mod frame;