diff --git a/src/codec/h265.rs b/src/codec/h265.rs index fa76cfe..01fa56c 100644 --- a/src/codec/h265.rs +++ b/src/codec/h265.rs @@ -473,7 +473,7 @@ impl Depacketizer { self.log_access_unit(&au, reason); } for nal in &self.nals { - let next_piece_idx = usize::try_from(nal.next_piece_idx).expect("u32 fits in usize"); + let next_piece_idx = std::cmp::min(nal.next_piece_idx as usize, self.pieces.len()); let nal_pieces = &self.pieces[piece_idx..next_piece_idx]; match nal.hdr.nal_unit_type() { UnitType::NalVps => { @@ -522,7 +522,7 @@ impl Depacketizer { let mut nals = vec![]; piece_idx = 0; for nal in &self.nals { - let next_piece_idx = usize::try_from(nal.next_piece_idx).expect("u32 fits in usize"); + let next_piece_idx = std::cmp::min(nal.next_piece_idx as usize, self.pieces.len()); let nal_pieces = &self.pieces[piece_idx..next_piece_idx]; nals.extend_from_slice(&[0, 0, 0, 1]);