forge
Explore
Log in

Don't you dare go Hollow

S8: SPS and PPS maps hold Arc; slices clone the handles instead of take/restore and expect (Arc, not Rc: H264Decoder must stay Send)

Browse files at this commit

756640e5ad8c76dff8d63a284aa448788e263c7dAhmet Kaan GümüşParent: 292c4a4
crates/ec-h264/src/decoder.rsmodified+12 -22
@@ -19,6 +19,7 @@
1919 //! caller that wants pictures as they are decoded.
2020
2121 use std::collections::VecDeque;
22+use std::sync::Arc;
2223
2324 use ec_core::BitReader;
2425 use ec_core::error::{Error, Result};
@@ -105,8 +106,8 @@ pub enum OutputOrder {
105106
106107 /// The decode core, generic over the sample type of the pictures it holds.
107108 pub(crate) struct Core<S: Sample> {
108- sps_map: Vec<Option<Sps>>,
109- pps_map: Vec<Option<Pps>>,
109+ sps_map: Vec<Option<Arc<Sps>>>,
110+ pps_map: Vec<Option<Arc<Pps>>>,
110111 rbsp: Vec<u8>,
111112 /// The picture being decoded, held out of the buffer pool.
112113 cur: Picture<S>,
@@ -635,7 +636,7 @@ impl<S: Sample> Core<S> {
635636 let sps = sps?;
636637 let id = sps.id;
637638 self.dpb.configure(&sps);
638- self.sps_map[id as usize] = Some(sps);
639+ self.sps_map[id as usize] = Some(Arc::new(sps));
639640 self.active_pps = None;
640641 self.last_sps = Some(id);
641642 Ok(NalOutcome::ParameterSet)
@@ -643,11 +644,11 @@ impl<S: Sample> Core<S> {
643644 NalUnitType::Pps => {
644645 let mut rbsp = core::mem::take(&mut self.rbsp);
645646 unescape_rbsp(payload, &mut rbsp);
646- let pps = Pps::parse(&rbsp, |id| self.sps_map[id as usize].as_ref());
647+ let pps = Pps::parse(&rbsp, |id| self.sps_map[id as usize].as_deref());
647648 self.rbsp = rbsp;
648649 let pps = pps?;
649650 let id = pps.id as usize;
650- self.pps_map[id] = Some(pps);
651+ self.pps_map[id] = Some(Arc::new(pps));
651652 // A re-sent PPS may carry other scaling lists: rebuild the level scales.
652653 self.active_pps = None;
653654 Ok(NalOutcome::ParameterSet)
@@ -691,7 +692,7 @@ impl<S: Sample> Core<S> {
691692 let output_ok = !(self.cur_ref_padded && self.cur.poc < recovery_poc);
692693 let sps_id = self.cur.sps_id as usize;
693694 let sps = self.sps_map[sps_id]
694- .take()
695+ .clone()
695696 .ok_or_else(|| Error::corrupt("active SPS vanished"))?;
696697 let pic = core::mem::take(&mut self.cur);
697698 let marking = self.has_marking.then_some(&self.cur_marking);
@@ -702,7 +703,6 @@ impl<S: Sample> Core<S> {
702703 self.dpb
703704 .store(pic, &sps, &self.cur_info, marking, output_ok)
704705 };
705- self.sps_map[sps_id] = Some(sps);
706706 stored?;
707707 if self.dpb.interlaced {
708708 // The picture a caller asks for is the frame store, which a field
@@ -869,14 +869,13 @@ impl<S: Sample> Core<S> {
869869 return Err(Error::corrupt("pic_parameter_set_id > 255"));
870870 }
871871 let pps = self.pps_map[pps_id as usize]
872- .as_ref()
872+ .clone()
873873 .ok_or_else(|| Error::corrupt("slice refers to an unknown PPS"))?;
874874 let sps = self.sps_map[pps.sps_id as usize]
875- .as_ref()
875+ .clone()
876876 .ok_or_else(|| Error::corrupt("PPS refers to an unknown SPS"))?;
877-
878- check_supported::<S>(sps, pps, slice_type_code, self.wide)?;
879- let sh = SliceHeader::parse(rbsp, header, sps, pps)?;
877+ check_supported::<S>(&sps, &pps, slice_type_code, self.wide)?;
878+ let sh = SliceHeader::parse(rbsp, header, &sps, &pps)?;
880879 if self.has_picture && !self.cur.complete {
881880 let same_picture = self.cur_key == Some(PicKey::of(&sh, header));
882881 // A picture starts again at macroblock 0 once macroblock 0 is
@@ -892,16 +891,7 @@ impl<S: Sample> Core<S> {
892891 // Redundant coded slices (7.4.3) repeat primary data: ignored.
893892 return Ok(NalOutcome::Skipped);
894893 }
895- // Borrow the parameter sets out of their slots for the duration of the
896- // slice: cloning them per slice would allocate, and a decode loop that
897- // allocates per slice is the thing the whole layout is built to avoid.
898- let sps_slot = pps.sps_id as usize;
899- let sps = self.sps_map[sps_slot].take().expect("checked above");
900- let pps = self.pps_map[pps_id as usize].take().expect("checked above");
901- let outcome = self.decode_slice_data(&sps, &pps, &sh, header, rbsp, first_mb);
902- self.sps_map[sps_slot] = Some(sps);
903- self.pps_map[pps_id as usize] = Some(pps);
904- outcome
894+ self.decode_slice_data(&sps, &pps, &sh, header, rbsp, first_mb)
905895 }
906896
907897 #[allow(clippy::too_many_arguments)]