Skip to content

Commit 6ebb531

Browse files
authored
Merge pull request #235 from mozilla/misc-prealloc
Preallocate vector storage when the size is known
2 parents b715d07 + 547617a commit 6ebb531

File tree

3 files changed

+32
-15
lines changed

3 files changed

+32
-15
lines changed

mp4parse/src/fallible.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ impl<T> TryVec<T> {
256256
Ok(())
257257
}
258258

259-
fn reserve(&mut self, additional: usize) -> Result<()> {
259+
pub fn reserve(&mut self, additional: usize) -> Result<()> {
260260
#[cfg(feature = "mp4parse_fallible")]
261261
{
262262
let available = self

mp4parse/src/lib.rs

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2059,10 +2059,11 @@ fn read_tkhd<T: Read>(src: &mut BMFFBox<T>) -> Result<TrackHeaderBox> {
20592059
}
20602060

20612061
/// Parse a elst box.
2062+
/// See ISO 14496-12:2015 § 8.6.6
20622063
fn read_elst<T: Read>(src: &mut BMFFBox<T>) -> Result<EditListBox> {
20632064
let (version, _) = read_fullbox_extra(src)?;
20642065
let edit_count = be_u32_with_limit(src)?;
2065-
let mut edits = TryVec::new();
2066+
let mut edits = TryVec::with_capacity(edit_count.to_usize())?;
20662067
for _ in 0..edit_count {
20672068
let (segment_duration, media_time) = match version {
20682069
1 => {
@@ -2134,10 +2135,11 @@ fn read_mdhd<T: Read>(src: &mut BMFFBox<T>) -> Result<MediaHeaderBox> {
21342135
}
21352136

21362137
/// Parse a stco box.
2138+
/// See ISO 14496-12:2015 § 8.7.5
21372139
fn read_stco<T: Read>(src: &mut BMFFBox<T>) -> Result<ChunkOffsetBox> {
21382140
let (_, _) = read_fullbox_extra(src)?;
21392141
let offset_count = be_u32_with_limit(src)?;
2140-
let mut offsets = TryVec::new();
2142+
let mut offsets = TryVec::with_capacity(offset_count.to_usize())?;
21412143
for _ in 0..offset_count {
21422144
offsets.push(be_u32(src)?.into())?;
21432145
}
@@ -2149,10 +2151,11 @@ fn read_stco<T: Read>(src: &mut BMFFBox<T>) -> Result<ChunkOffsetBox> {
21492151
}
21502152

21512153
/// Parse a co64 box.
2154+
/// See ISO 14496-12:2015 § 8.7.5
21522155
fn read_co64<T: Read>(src: &mut BMFFBox<T>) -> Result<ChunkOffsetBox> {
21532156
let (_, _) = read_fullbox_extra(src)?;
21542157
let offset_count = be_u32_with_limit(src)?;
2155-
let mut offsets = TryVec::new();
2158+
let mut offsets = TryVec::with_capacity(offset_count.to_usize())?;
21562159
for _ in 0..offset_count {
21572160
offsets.push(be_u64(src)?)?;
21582161
}
@@ -2164,10 +2167,11 @@ fn read_co64<T: Read>(src: &mut BMFFBox<T>) -> Result<ChunkOffsetBox> {
21642167
}
21652168

21662169
/// Parse a stss box.
2170+
/// See ISO 14496-12:2015 § 8.6.2
21672171
fn read_stss<T: Read>(src: &mut BMFFBox<T>) -> Result<SyncSampleBox> {
21682172
let (_, _) = read_fullbox_extra(src)?;
21692173
let sample_count = be_u32_with_limit(src)?;
2170-
let mut samples = TryVec::new();
2174+
let mut samples = TryVec::with_capacity(sample_count.to_usize())?;
21712175
for _ in 0..sample_count {
21722176
samples.push(be_u32(src)?)?;
21732177
}
@@ -2179,10 +2183,11 @@ fn read_stss<T: Read>(src: &mut BMFFBox<T>) -> Result<SyncSampleBox> {
21792183
}
21802184

21812185
/// Parse a stsc box.
2186+
/// See ISO 14496-12:2015 § 8.7.4
21822187
fn read_stsc<T: Read>(src: &mut BMFFBox<T>) -> Result<SampleToChunkBox> {
21832188
let (_, _) = read_fullbox_extra(src)?;
21842189
let sample_count = be_u32_with_limit(src)?;
2185-
let mut samples = TryVec::new();
2190+
let mut samples = TryVec::with_capacity(sample_count.to_usize())?;
21862191
for _ in 0..sample_count {
21872192
let first_chunk = be_u32(src)?;
21882193
let samples_per_chunk = be_u32_with_limit(src)?;
@@ -2200,16 +2205,23 @@ fn read_stsc<T: Read>(src: &mut BMFFBox<T>) -> Result<SampleToChunkBox> {
22002205
Ok(SampleToChunkBox { samples })
22012206
}
22022207

2208+
/// Parse a Composition Time to Sample Box
2209+
/// See ISO 14496-12:2015 § 8.6.1.3
22032210
fn read_ctts<T: Read>(src: &mut BMFFBox<T>) -> Result<CompositionOffsetBox> {
22042211
let (version, _) = read_fullbox_extra(src)?;
22052212

2206-
let counts = u64::from(be_u32_with_limit(src)?);
2213+
let counts = be_u32_with_limit(src)?;
22072214

2208-
if src.bytes_left() < counts.checked_mul(8).expect("counts -> bytes overflow") {
2215+
if src.bytes_left()
2216+
< counts
2217+
.checked_mul(8)
2218+
.expect("counts -> bytes overflow")
2219+
.into()
2220+
{
22092221
return Err(Error::InvalidData("insufficient data in 'ctts' box"));
22102222
}
22112223

2212-
let mut offsets = TryVec::new();
2224+
let mut offsets = TryVec::with_capacity(counts.to_usize())?;
22132225
for _ in 0..counts {
22142226
let (sample_count, time_offset) = match version {
22152227
// According to spec, Version0 shoule be used when version == 0;
@@ -2236,12 +2248,14 @@ fn read_ctts<T: Read>(src: &mut BMFFBox<T>) -> Result<CompositionOffsetBox> {
22362248
}
22372249

22382250
/// Parse a stsz box.
2251+
/// See ISO 14496-12:2015 § 8.7.3.2
22392252
fn read_stsz<T: Read>(src: &mut BMFFBox<T>) -> Result<SampleSizeBox> {
22402253
let (_, _) = read_fullbox_extra(src)?;
22412254
let sample_size = be_u32(src)?;
22422255
let sample_count = be_u32_with_limit(src)?;
22432256
let mut sample_sizes = TryVec::new();
22442257
if sample_size == 0 {
2258+
sample_sizes.reserve(sample_count.to_usize())?;
22452259
for _ in 0..sample_count {
22462260
sample_sizes.push(be_u32(src)?)?;
22472261
}
@@ -2257,10 +2271,11 @@ fn read_stsz<T: Read>(src: &mut BMFFBox<T>) -> Result<SampleSizeBox> {
22572271
}
22582272

22592273
/// Parse a stts box.
2274+
/// See ISO 14496-12:2015 § 8.6.1.2
22602275
fn read_stts<T: Read>(src: &mut BMFFBox<T>) -> Result<TimeToSampleBox> {
22612276
let (_, _) = read_fullbox_extra(src)?;
22622277
let sample_count = be_u32_with_limit(src)?;
2263-
let mut samples = TryVec::new();
2278+
let mut samples = TryVec::with_capacity(sample_count.to_usize())?;
22642279
for _ in 0..sample_count {
22652280
let sample_count = be_u32_with_limit(src)?;
22662281
let sample_delta = be_u32(src)?;
@@ -2715,6 +2730,7 @@ fn read_esds<T: Read>(src: &mut BMFFBox<T>) -> Result<ES_Descriptor> {
27152730
}
27162731

27172732
/// Parse `FLACSpecificBox`.
2733+
/// See https://github.com/xiph/flac/blob/master/doc/isoflac.txt § 3.3.2
27182734
fn read_dfla<T: Read>(src: &mut BMFFBox<T>) -> Result<FLACSpecificBox> {
27192735
let (version, flags) = read_fullbox_extra(src)?;
27202736
if version != 0 {

mp4parse_capi/src/lib.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1736,13 +1736,14 @@ fn get_pssh_info(
17361736

17371737
pssh_data.clear();
17381738
for pssh in &context.psshs {
1739-
let content_len = pssh.box_content.len();
1740-
if content_len > std::u32::MAX as usize {
1741-
return Err(Mp4parseStatus::Invalid);
1742-
}
1739+
let content_len = pssh
1740+
.box_content
1741+
.len()
1742+
.try_into()
1743+
.map_err(|_| Mp4parseStatus::Invalid)?;
17431744
let mut data_len = TryVec::new();
17441745
if data_len
1745-
.write_u32::<byteorder::NativeEndian>(content_len as u32)
1746+
.write_u32::<byteorder::NativeEndian>(content_len)
17461747
.is_err()
17471748
{
17481749
return Err(Mp4parseStatus::Io);

0 commit comments

Comments
 (0)