ec-hw: H.264 DPB generic over the surface type and ungated (dpb264.rs, h264_common.rs), for the Vulkan decoder
Browse files at this commitc58ee896b647127ec129183d994ae91fc18d3fe8Parent: e98bba95 changed files +53 -43
- crates/ec-hw/src/dec/h264.rs +5 -28
- crates/ec-hw/src/dec/mod.rs +0 -1
- crates/ec-hw/src/dec/dpb264.rs → crates/ec-hw/src/dpb264.rs +13 -14
- crates/ec-hw/src/h264_common.rs +31 -0
- crates/ec-hw/src/lib.rs +4 -0
crates/ec-hw/src/dec/h264.rsmodified+5 -28
| @@ -10,10 +10,11 @@ use ec_h264_syntax::{ | |||
| 10 | use ec_va::caps::Profile; | 10 | use ec_va::caps::Profile; |
| 11 | use ec_va::{Buffer, Display}; | 11 | use ec_va::{Buffer, Display}; |
| 12 | 12 | ||
| 13 | -use super::dpb264::{Dpb, Mark, PicInfo, Picture, Poc, RefList}; | ||
| 14 | use super::{ReadyFrames, Session, StreamInfo}; | 13 | use super::{ReadyFrames, Session, StreamInfo}; |
| 14 | +use crate::dpb264::{self, Mark, PicInfo, Poc, RefList}; | ||
| 15 | use crate::error::{Error, Result}; | 15 | use crate::error::{Error, Result}; |
| 16 | use crate::frame::{Colour, Frame}; | 16 | use crate::frame::{Colour, Frame}; |
| 17 | +use crate::h264_common::{peek_pps_id, vui_colour_tags}; | ||
| 17 | use crate::params::h264::{ | 18 | use crate::params::h264::{ |
| 18 | IQMatrixBufferH264, PICTURE_LONG_TERM_REFERENCE, PICTURE_SHORT_TERM_REFERENCE, PicFields, | 19 | IQMatrixBufferH264, PICTURE_LONG_TERM_REFERENCE, PICTURE_SHORT_TERM_REFERENCE, PicFields, |
| 19 | PictureParameterBufferH264, SeqFields, SliceParameterBufferH264, VAPictureH264, | 20 | PictureParameterBufferH264, SeqFields, SliceParameterBufferH264, VAPictureH264, |
| @@ -75,6 +76,9 @@ struct Current { | |||
| 75 | ref_padded: bool, | 76 | ref_padded: bool, |
| 76 | } | 77 | } |
| 77 | 78 | ||
| 79 | +type Dpb = dpb264::Dpb<Rc<PooledSurface>>; | ||
| 80 | +type Picture = dpb264::Picture<Rc<PooledSurface>>; | ||
| 81 | + | ||
| 78 | /// A stateless H.264 decoder. | 82 | /// A stateless H.264 decoder. |
| 79 | pub struct H264Decoder { | 83 | pub struct H264Decoder { |
| 80 | display: Rc<Display>, | 84 | display: Rc<Display>, |
| @@ -489,33 +493,6 @@ impl H264Decoder { | |||
| 489 | } | 493 | } |
| 490 | } | 494 | } |
| 491 | 495 | ||
| 492 | -/// An SPS's VUI `colour_description` and range flag, as [`Tags`] for | ||
| 493 | -/// [`ColorDescription::resolve`]. [`Tags::default()`] when the VUI has no | ||
| 494 | -/// `video_signal_type` at all. | ||
| 495 | -fn vui_colour_tags(sps: &Sps) -> Tags { | ||
| 496 | - let Some(vui) = &sps.vui else { | ||
| 497 | - return Tags::default(); | ||
| 498 | - }; | ||
| 499 | - if vui.video_format.is_none() { | ||
| 500 | - return Tags::default(); | ||
| 501 | - } | ||
| 502 | - let (_primaries, transfer, matrix) = vui.colour_description.unwrap_or((0, 0, 0)); | ||
| 503 | - Tags::from_codes( | ||
| 504 | - u64::from(matrix), | ||
| 505 | - u64::from(transfer), | ||
| 506 | - if vui.video_full_range { 2 } else { 1 }, | ||
| 507 | - ) | ||
| 508 | -} | ||
| 509 | - | ||
| 510 | -/// The `pic_parameter_set_id` of a slice header, which is the second syntax | ||
| 511 | -/// element and needs no parameter set to read. | ||
| 512 | -fn peek_pps_id(rbsp: &[u8]) -> Result<u32> { | ||
| 513 | - let mut r = ec_core::BitReader::new(rbsp); | ||
| 514 | - r.read_ue()?; // first_mb_in_slice | ||
| 515 | - r.read_ue()?; // slice_type | ||
| 516 | - Ok(r.read_ue()?) | ||
| 517 | -} | ||
| 518 | - | ||
| 519 | /// The VA profile for a stream, from its SPS. | 496 | /// The VA profile for a stream, from its SPS. |
| 520 | fn profile_for(sps: &Sps) -> Result<Profile> { | 497 | fn profile_for(sps: &Sps) -> Result<Profile> { |
| 521 | Ok(match sps.profile_idc { | 498 | Ok(match sps.profile_idc { |
crates/ec-hw/src/dec/mod.rsmodified+0 -1
| @@ -33,7 +33,6 @@ use crate::pool::SurfacePool; | |||
| 33 | pub use crate::types::Codec; | 33 | pub use crate::types::Codec; |
| 34 | 34 | ||
| 35 | mod av1; | 35 | mod av1; |
| 36 | -mod dpb264; | ||
| 37 | mod h264; | 36 | mod h264; |
| 38 | mod vp9; | 37 | mod vp9; |
| 39 | 38 | ||
crates/ec-hw/src/dec/dpb264.rs → crates/ec-hw/src/dpb264.rsrenamed+13 -14
| @@ -7,12 +7,9 @@ | |||
| 7 | //! told the whole buffer on every picture, so these lists are the parameter | 7 | //! told the whole buffer on every picture, so these lists are the parameter |
| 8 | //! buffer's contents rather than an internal detail. | 8 | //! buffer's contents rather than an internal detail. |
| 9 | 9 | ||
| 10 | -use std::rc::Rc; | ||
| 11 | - | ||
| 12 | use ec_h264_syntax::{DecRefPicMarking, RefPicListMod, SliceType, Sps}; | 10 | use ec_h264_syntax::{DecRefPicMarking, RefPicListMod, SliceType, Sps}; |
| 13 | 11 | ||
| 14 | use crate::error::{Error, Result}; | 12 | use crate::error::{Error, Result}; |
| 15 | -use crate::pool::PooledSurface; | ||
| 16 | 13 | ||
| 17 | /// Maximum pictures held at once (Annex A caps the DPB at 16 frames; the extra | 14 | /// Maximum pictures held at once (Annex A caps the DPB at 16 frames; the extra |
| 18 | /// slot is the picture being decoded). | 15 | /// slot is the picture being decoded). |
| @@ -30,11 +27,11 @@ pub(crate) enum Mark { | |||
| 30 | } | 27 | } |
| 31 | 28 | ||
| 32 | /// One picture in the buffer. | 29 | /// One picture in the buffer. |
| 33 | -pub(crate) struct Picture { | 30 | +pub(crate) struct Picture<S> { |
| 34 | /// Identity, so "the picture just stored" survives reordering. | 31 | /// Identity, so "the picture just stored" survives reordering. |
| 35 | pub id: i32, | 32 | pub id: i32, |
| 36 | /// The surface it decoded into. | 33 | /// The surface it decoded into. |
| 37 | - pub surface: Rc<PooledSurface>, | 34 | + pub surface: S, |
| 38 | /// Presentation timestamp of the access unit it came from. | 35 | /// Presentation timestamp of the access unit it came from. |
| 39 | pub timestamp: i64, | 36 | pub timestamp: i64, |
| 40 | /// `frame_num`. | 37 | /// `frame_num`. |
| @@ -62,7 +59,7 @@ pub(crate) struct Picture { | |||
| 62 | pub output: bool, | 59 | pub output: bool, |
| 63 | } | 60 | } |
| 64 | 61 | ||
| 65 | -impl Picture { | 62 | +impl<S> Picture<S> { |
| 66 | fn stored(&self) -> bool { | 63 | fn stored(&self) -> bool { |
| 67 | self.output || self.mark != Mark::Unused | 64 | self.output || self.mark != Mark::Unused |
| 68 | } | 65 | } |
| @@ -124,8 +121,8 @@ pub(crate) struct PicInfo { | |||
| 124 | } | 121 | } |
| 125 | 122 | ||
| 126 | /// The decoded picture buffer and the cross-picture state of clause 8.2. | 123 | /// The decoded picture buffer and the cross-picture state of clause 8.2. |
| 127 | -pub(crate) struct Dpb { | 124 | +pub(crate) struct Dpb<S> { |
| 128 | - pub frames: Vec<Picture>, | 125 | + pub frames: Vec<Picture<S>>, |
| 129 | next_id: i32, | 126 | next_id: i32, |
| 130 | max_frames: usize, | 127 | max_frames: usize, |
| 131 | max_reorder: usize, | 128 | max_reorder: usize, |
| @@ -141,8 +138,8 @@ pub(crate) struct Dpb { | |||
| 141 | last_id: i32, | 138 | last_id: i32, |
| 142 | } | 139 | } |
| 143 | 140 | ||
| 144 | -impl Default for Dpb { | 141 | +impl<S> Default for Dpb<S> { |
| 145 | - fn default() -> Dpb { | 142 | + fn default() -> Dpb<S> { |
| 146 | Dpb { | 143 | Dpb { |
| 147 | frames: Vec::new(), | 144 | frames: Vec::new(), |
| 148 | next_id: 0, | 145 | next_id: 0, |
| @@ -162,7 +159,7 @@ impl Default for Dpb { | |||
| 162 | } | 159 | } |
| 163 | } | 160 | } |
| 164 | 161 | ||
| 165 | -impl Dpb { | 162 | +impl<S: Clone> Dpb<S> { |
| 166 | /// Adopt the buffer limits of `sps` (Annex A / VUI). | 163 | /// Adopt the buffer limits of `sps` (Annex A / VUI). |
| 167 | pub(crate) fn configure(&mut self, sps: &Sps) { | 164 | pub(crate) fn configure(&mut self, sps: &Sps) { |
| 168 | let level_frames = max_dpb_frames(sps); | 165 | let level_frames = max_dpb_frames(sps); |
| @@ -335,7 +332,7 @@ impl Dpb { | |||
| 335 | .frames | 332 | .frames |
| 336 | .iter() | 333 | .iter() |
| 337 | .max_by_key(|p| p.frame_num_wrap) | 334 | .max_by_key(|p| p.frame_num_wrap) |
| 338 | - .map(|p| Rc::clone(&p.surface)) | 335 | + .map(|p| p.surface.clone()) |
| 339 | else { | 336 | else { |
| 340 | // Nothing decoded yet: there is no picture to stand in for the | 337 | // Nothing decoded yet: there is no picture to stand in for the |
| 341 | // missing ones, and the next IDR will resynchronise anyway. | 338 | // missing ones, and the next IDR will resynchronise anyway. |
| @@ -597,7 +594,7 @@ impl Dpb { | |||
| 597 | /// a reference and for bookkeeping, but must never be handed to a caller. | 594 | /// a reference and for bookkeeping, but must never be handed to a caller. |
| 598 | pub(crate) fn store( | 595 | pub(crate) fn store( |
| 599 | &mut self, | 596 | &mut self, |
| 600 | - mut pic: Picture, | 597 | + mut pic: Picture<S>, |
| 601 | sps: &Sps, | 598 | sps: &Sps, |
| 602 | info: &PicInfo, | 599 | info: &PicInfo, |
| 603 | marking: Option<&DecRefPicMarking>, | 600 | marking: Option<&DecRefPicMarking>, |
| @@ -699,7 +696,7 @@ impl Dpb { | |||
| 699 | /// Adaptive marking (8.2.5.4). | 696 | /// Adaptive marking (8.2.5.4). |
| 700 | fn apply_mmco( | 697 | fn apply_mmco( |
| 701 | &mut self, | 698 | &mut self, |
| 702 | - pic: &mut Picture, | 699 | + pic: &mut Picture<S>, |
| 703 | m: &DecRefPicMarking, | 700 | m: &DecRefPicMarking, |
| 704 | info: &PicInfo, | 701 | info: &PicInfo, |
| 705 | ) -> Result<()> { | 702 | ) -> Result<()> { |
| @@ -878,6 +875,8 @@ pub(crate) fn gap_frame_nums(prev_ref: u32, frame_num: u32, max: u32) -> impl It | |||
| 878 | mod tests { | 875 | mod tests { |
| 879 | use super::*; | 876 | use super::*; |
| 880 | 877 | ||
| 878 | + type Dpb = super::Dpb<u32>; | ||
| 879 | + | ||
| 881 | fn sps_for(poc_type: u8) -> Sps { | 880 | fn sps_for(poc_type: u8) -> Sps { |
| 882 | let mut sps = Sps { | 881 | let mut sps = Sps { |
| 883 | profile_idc: 100, | 882 | profile_idc: 100, |
crates/ec-hw/src/h264_common.rsadded+31 -0
| @@ -0,0 +1,31 @@ | |||
| 1 | +use ec_core::color::Tags; | ||
| 2 | +use ec_h264_syntax::Sps; | ||
| 3 | + | ||
| 4 | +use crate::error::Result; | ||
| 5 | + | ||
| 6 | +/// An SPS's VUI `colour_description` and range flag, as [`Tags`] for | ||
| 7 | +/// [`ec_core::color::ColorDescription::resolve`]. [`Tags::default()`] when the VUI has no | ||
| 8 | +/// `video_signal_type` at all. | ||
| 9 | +pub(crate) fn vui_colour_tags(sps: &Sps) -> Tags { | ||
| 10 | + let Some(vui) = &sps.vui else { | ||
| 11 | + return Tags::default(); | ||
| 12 | + }; | ||
| 13 | + if vui.video_format.is_none() { | ||
| 14 | + return Tags::default(); | ||
| 15 | + } | ||
| 16 | + let (_primaries, transfer, matrix) = vui.colour_description.unwrap_or((0, 0, 0)); | ||
| 17 | + Tags::from_codes( | ||
| 18 | + u64::from(matrix), | ||
| 19 | + u64::from(transfer), | ||
| 20 | + if vui.video_full_range { 2 } else { 1 }, | ||
| 21 | + ) | ||
| 22 | +} | ||
| 23 | + | ||
| 24 | +/// The `pic_parameter_set_id` of a slice header, which is the second syntax | ||
| 25 | +/// element and needs no parameter set to read. | ||
| 26 | +pub(crate) fn peek_pps_id(rbsp: &[u8]) -> Result<u32> { | ||
| 27 | + let mut r = ec_core::BitReader::new(rbsp); | ||
| 28 | + r.read_ue()?; // first_mb_in_slice | ||
| 29 | + r.read_ue()?; // slice_type | ||
| 30 | + Ok(r.read_ue()?) | ||
| 31 | +} | ||
crates/ec-hw/src/lib.rsmodified+4 -0
| @@ -59,6 +59,10 @@ pub mod dec; | |||
| 59 | #[cfg(target_os = "linux")] | 59 | #[cfg(target_os = "linux")] |
| 60 | pub mod enc; | 60 | pub mod enc; |
| 61 | pub mod error; | 61 | pub mod error; |
| 62 | +#[cfg_attr(not(target_os = "linux"), allow(dead_code))] | ||
| 63 | +pub(crate) mod dpb264; | ||
| 64 | +#[cfg_attr(not(target_os = "linux"), allow(dead_code))] | ||
| 65 | +pub(crate) mod h264_common; | ||
| 62 | pub mod hw; | 66 | pub mod hw; |
| 63 | #[cfg(target_os = "linux")] | 67 | #[cfg(target_os = "linux")] |
| 64 | pub mod frame; | 68 | pub mod frame; |