diff --git a/crates/ec-h264/src/decoder.rs b/crates/ec-h264/src/decoder.rs index ae083e85..f563804c 100644 --- a/crates/ec-h264/src/decoder.rs +++ b/crates/ec-h264/src/decoder.rs @@ -19,6 +19,7 @@ //! caller that wants pictures as they are decoded. use std::collections::VecDeque; +use std::sync::Arc; use ec_core::BitReader; use ec_core::error::{Error, Result}; @@ -105,8 +106,8 @@ pub enum OutputOrder { /// The decode core, generic over the sample type of the pictures it holds. pub(crate) struct Core { - sps_map: Vec>, - pps_map: Vec>, + sps_map: Vec>>, + pps_map: Vec>>, rbsp: Vec, /// The picture being decoded, held out of the buffer pool. cur: Picture, @@ -635,7 +636,7 @@ impl Core { let sps = sps?; let id = sps.id; self.dpb.configure(&sps); - self.sps_map[id as usize] = Some(sps); + self.sps_map[id as usize] = Some(Arc::new(sps)); self.active_pps = None; self.last_sps = Some(id); Ok(NalOutcome::ParameterSet) @@ -643,11 +644,11 @@ impl Core { NalUnitType::Pps => { let mut rbsp = core::mem::take(&mut self.rbsp); unescape_rbsp(payload, &mut rbsp); - let pps = Pps::parse(&rbsp, |id| self.sps_map[id as usize].as_ref()); + let pps = Pps::parse(&rbsp, |id| self.sps_map[id as usize].as_deref()); self.rbsp = rbsp; let pps = pps?; let id = pps.id as usize; - self.pps_map[id] = Some(pps); + self.pps_map[id] = Some(Arc::new(pps)); // A re-sent PPS may carry other scaling lists: rebuild the level scales. self.active_pps = None; Ok(NalOutcome::ParameterSet) @@ -691,7 +692,7 @@ impl Core { let output_ok = !(self.cur_ref_padded && self.cur.poc < recovery_poc); let sps_id = self.cur.sps_id as usize; let sps = self.sps_map[sps_id] - .take() + .clone() .ok_or_else(|| Error::corrupt("active SPS vanished"))?; let pic = core::mem::take(&mut self.cur); let marking = self.has_marking.then_some(&self.cur_marking); @@ -702,7 +703,6 @@ impl Core { self.dpb .store(pic, &sps, &self.cur_info, marking, output_ok) }; - self.sps_map[sps_id] = Some(sps); stored?; if self.dpb.interlaced { // The picture a caller asks for is the frame store, which a field @@ -869,14 +869,13 @@ impl Core { return Err(Error::corrupt("pic_parameter_set_id > 255")); } let pps = self.pps_map[pps_id as usize] - .as_ref() + .clone() .ok_or_else(|| Error::corrupt("slice refers to an unknown PPS"))?; let sps = self.sps_map[pps.sps_id as usize] - .as_ref() + .clone() .ok_or_else(|| Error::corrupt("PPS refers to an unknown SPS"))?; - - check_supported::(sps, pps, slice_type_code, self.wide)?; - let sh = SliceHeader::parse(rbsp, header, sps, pps)?; + check_supported::(&sps, &pps, slice_type_code, self.wide)?; + let sh = SliceHeader::parse(rbsp, header, &sps, &pps)?; if self.has_picture && !self.cur.complete { let same_picture = self.cur_key == Some(PicKey::of(&sh, header)); // A picture starts again at macroblock 0 once macroblock 0 is @@ -892,16 +891,7 @@ impl Core { // Redundant coded slices (7.4.3) repeat primary data: ignored. return Ok(NalOutcome::Skipped); } - // Borrow the parameter sets out of their slots for the duration of the - // slice: cloning them per slice would allocate, and a decode loop that - // allocates per slice is the thing the whole layout is built to avoid. - let sps_slot = pps.sps_id as usize; - let sps = self.sps_map[sps_slot].take().expect("checked above"); - let pps = self.pps_map[pps_id as usize].take().expect("checked above"); - let outcome = self.decode_slice_data(&sps, &pps, &sh, header, rbsp, first_mb); - self.sps_map[sps_slot] = Some(sps); - self.pps_map[pps_id as usize] = Some(pps); - outcome + self.decode_slice_data(&sps, &pps, &sh, header, rbsp, first_mb) } #[allow(clippy::too_many_arguments)]