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 @@
19 //! caller that wants pictures as they are decoded.19 //! caller that wants pictures as they are decoded.
20 20
21 use std::collections::VecDeque;21 use std::collections::VecDeque;
22+use std::sync::Arc;
22 23
23 use ec_core::BitReader;24 use ec_core::BitReader;
24 use ec_core::error::{Error, Result};25 use ec_core::error::{Error, Result};
@@ -105,8 +106,8 @@ pub enum OutputOrder {
105 106
106 /// The decode core, generic over the sample type of the pictures it holds.107 /// The decode core, generic over the sample type of the pictures it holds.
107 pub(crate) struct Core<S: Sample> {108 pub(crate) struct Core<S: Sample> {
108- sps_map: Vec<Option<Sps>>,109+ sps_map: Vec<Option<Arc<Sps>>>,
109- pps_map: Vec<Option<Pps>>,110+ pps_map: Vec<Option<Arc<Pps>>>,
110 rbsp: Vec<u8>,111 rbsp: Vec<u8>,
111 /// The picture being decoded, held out of the buffer pool.112 /// The picture being decoded, held out of the buffer pool.
112 cur: Picture<S>,113 cur: Picture<S>,
@@ -635,7 +636,7 @@ impl<S: Sample> Core<S> {
635 let sps = sps?;636 let sps = sps?;
636 let id = sps.id;637 let id = sps.id;
637 self.dpb.configure(&sps);638 self.dpb.configure(&sps);
638- self.sps_map[id as usize] = Some(sps);639+ self.sps_map[id as usize] = Some(Arc::new(sps));
639 self.active_pps = None;640 self.active_pps = None;
640 self.last_sps = Some(id);641 self.last_sps = Some(id);
641 Ok(NalOutcome::ParameterSet)642 Ok(NalOutcome::ParameterSet)
@@ -643,11 +644,11 @@ impl<S: Sample> Core<S> {
643 NalUnitType::Pps => {644 NalUnitType::Pps => {
644 let mut rbsp = core::mem::take(&mut self.rbsp);645 let mut rbsp = core::mem::take(&mut self.rbsp);
645 unescape_rbsp(payload, &mut rbsp);646 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());
647 self.rbsp = rbsp;648 self.rbsp = rbsp;
648 let pps = pps?;649 let pps = pps?;
649 let id = pps.id as usize;650 let id = pps.id as usize;
650- self.pps_map[id] = Some(pps);651+ self.pps_map[id] = Some(Arc::new(pps));
651 // A re-sent PPS may carry other scaling lists: rebuild the level scales.652 // A re-sent PPS may carry other scaling lists: rebuild the level scales.
652 self.active_pps = None;653 self.active_pps = None;
653 Ok(NalOutcome::ParameterSet)654 Ok(NalOutcome::ParameterSet)
@@ -691,7 +692,7 @@ impl<S: Sample> Core<S> {
691 let output_ok = !(self.cur_ref_padded && self.cur.poc < recovery_poc);692 let output_ok = !(self.cur_ref_padded && self.cur.poc < recovery_poc);
692 let sps_id = self.cur.sps_id as usize;693 let sps_id = self.cur.sps_id as usize;
693 let sps = self.sps_map[sps_id]694 let sps = self.sps_map[sps_id]
694- .take()695+ .clone()
695 .ok_or_else(|| Error::corrupt("active SPS vanished"))?;696 .ok_or_else(|| Error::corrupt("active SPS vanished"))?;
696 let pic = core::mem::take(&mut self.cur);697 let pic = core::mem::take(&mut self.cur);
697 let marking = self.has_marking.then_some(&self.cur_marking);698 let marking = self.has_marking.then_some(&self.cur_marking);
@@ -702,7 +703,6 @@ impl<S: Sample> Core<S> {
702 self.dpb703 self.dpb
703 .store(pic, &sps, &self.cur_info, marking, output_ok)704 .store(pic, &sps, &self.cur_info, marking, output_ok)
704 };705 };
705- self.sps_map[sps_id] = Some(sps);
706 stored?;706 stored?;
707 if self.dpb.interlaced {707 if self.dpb.interlaced {
708 // The picture a caller asks for is the frame store, which a field708 // The picture a caller asks for is the frame store, which a field
@@ -869,14 +869,13 @@ impl<S: Sample> Core<S> {
869 return Err(Error::corrupt("pic_parameter_set_id > 255"));869 return Err(Error::corrupt("pic_parameter_set_id > 255"));
870 }870 }
871 let pps = self.pps_map[pps_id as usize]871 let pps = self.pps_map[pps_id as usize]
872- .as_ref()872+ .clone()
873 .ok_or_else(|| Error::corrupt("slice refers to an unknown PPS"))?;873 .ok_or_else(|| Error::corrupt("slice refers to an unknown PPS"))?;
874 let sps = self.sps_map[pps.sps_id as usize]874 let sps = self.sps_map[pps.sps_id as usize]
875- .as_ref()875+ .clone()
876 .ok_or_else(|| Error::corrupt("PPS refers to an unknown SPS"))?;876 .ok_or_else(|| Error::corrupt("PPS refers to an unknown SPS"))?;
877-877+ check_supported::<S>(&sps, &pps, slice_type_code, self.wide)?;
878- check_supported::<S>(sps, pps, slice_type_code, self.wide)?;878+ let sh = SliceHeader::parse(rbsp, header, &sps, &pps)?;
879- let sh = SliceHeader::parse(rbsp, header, sps, pps)?;
880 if self.has_picture && !self.cur.complete {879 if self.has_picture && !self.cur.complete {
881 let same_picture = self.cur_key == Some(PicKey::of(&sh, header));880 let same_picture = self.cur_key == Some(PicKey::of(&sh, header));
882 // A picture starts again at macroblock 0 once macroblock 0 is881 // A picture starts again at macroblock 0 once macroblock 0 is
@@ -892,16 +891,7 @@ impl<S: Sample> Core<S> {
892 // Redundant coded slices (7.4.3) repeat primary data: ignored.891 // Redundant coded slices (7.4.3) repeat primary data: ignored.
893 return Ok(NalOutcome::Skipped);892 return Ok(NalOutcome::Skipped);
894 }893 }
895- // Borrow the parameter sets out of their slots for the duration of the894+ self.decode_slice_data(&sps, &pps, &sh, header, rbsp, first_mb)
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
905 }895 }
906 896
907 #[allow(clippy::too_many_arguments)]897 #[allow(clippy::too_many_arguments)]