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