Skip to content

Commit 0b4b8a3

Browse files
committed
cargo fmt
1 parent 1a62648 commit 0b4b8a3

3 files changed

Lines changed: 86 additions & 51 deletions

File tree

src/bin/mp4info.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,9 @@ fn parse_trak(trak: &Box, index: usize, info: &mut MediaInfo) {
188188
// mdhd: timescale / duration / language
189189
if let Some(mdhd) = find_child(mdia, "mdhd") {
190190
// Try structured data first
191-
if let Some(mp4box::registry::StructuredData::MediaHeader(mdhd_data)) = &mdhd.structured_data {
191+
if let Some(mp4box::registry::StructuredData::MediaHeader(mdhd_data)) =
192+
&mdhd.structured_data
193+
{
192194
ti.timescale = Some(mdhd_data.timescale);
193195
ti.duration_ticks = Some(mdhd_data.duration as u64);
194196
ti.duration_seconds = Some(mdhd_data.duration as f64 / mdhd_data.timescale as f64);
@@ -214,7 +216,9 @@ fn parse_trak(trak: &Box, index: usize, info: &mut MediaInfo) {
214216
// hdlr: determine track type (video/audio/other)
215217
if let Some(hdlr) = find_child(mdia, "hdlr") {
216218
// Try structured data first
217-
if let Some(mp4box::registry::StructuredData::HandlerReference(hdlr_data)) = &hdlr.structured_data {
219+
if let Some(mp4box::registry::StructuredData::HandlerReference(hdlr_data)) =
220+
&hdlr.structured_data
221+
{
218222
let tt = match hdlr_data.handler_type.as_str() {
219223
"vide" => "video",
220224
"soun" => "audio",

src/samples.rs

Lines changed: 34 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,10 @@ pub struct SampleInfo {
7878
/// use mp4box::track_samples_from_path;
7979
///
8080
/// let track_samples = track_samples_from_path("video.mp4").unwrap();
81-
///
81+
///
8282
/// for track in track_samples {
83-
/// println!("Track {}: {} ({} samples)",
84-
/// track.track_id,
83+
/// println!("Track {}: {} ({} samples)",
84+
/// track.track_id,
8585
/// track.handler_type,
8686
/// track.sample_count);
8787
///
@@ -142,7 +142,7 @@ pub struct TrackSamples {
142142
///
143143
/// let file = File::open("video.mp4").unwrap();
144144
/// let track_samples = track_samples_from_reader(file).unwrap();
145-
///
145+
///
146146
/// for track in track_samples {
147147
/// println!("Track {}: {} samples", track.track_id, track.sample_count);
148148
/// }
@@ -212,7 +212,7 @@ pub fn track_samples_from_reader<R: Read + Seek>(
212212
/// let samples = track_samples_from_path(path)?;
213213
///
214214
/// for track in samples {
215-
/// println!("Track {} has {} samples of type {}",
215+
/// println!("Track {} has {} samples of type {}",
216216
/// track.track_id, track.sample_count, track.handler_type);
217217
/// }
218218
/// Ok(())
@@ -333,7 +333,7 @@ fn find_track_id(trak_box: &crate::Box) -> anyhow::Result<u32> {
333333

334334
fn find_media_info(trak_box: &crate::Box) -> anyhow::Result<(String, u32, u64)> {
335335
use crate::registry::StructuredData;
336-
336+
337337
// Look for mdia/mdhd and mdia/hdlr boxes
338338
if let Some(children) = &trak_box.children {
339339
for child in children {
@@ -347,14 +347,18 @@ fn find_media_info(trak_box: &crate::Box) -> anyhow::Result<(String, u32, u64)>
347347
for mdia_child in mdia_children {
348348
if mdia_child.typ == "mdhd" {
349349
// Parse timescale and duration from mdhd
350-
if let Some(StructuredData::MediaHeader(mdhd_data)) = &mdia_child.structured_data {
350+
if let Some(StructuredData::MediaHeader(mdhd_data)) =
351+
&mdia_child.structured_data
352+
{
351353
timescale = mdhd_data.timescale;
352354
duration = mdhd_data.duration as u64;
353355
}
354356
}
355357
if mdia_child.typ == "hdlr" {
356358
// Parse handler type from hdlr
357-
if let Some(StructuredData::HandlerReference(hdlr_data)) = &mdia_child.structured_data {
359+
if let Some(StructuredData::HandlerReference(hdlr_data)) =
360+
&mdia_child.structured_data
361+
{
358362
handler_type = hdlr_data.handler_type.clone();
359363
}
360364
}
@@ -445,8 +449,8 @@ fn extract_sample_tables(stbl_box: &crate::Box) -> anyhow::Result<SampleTables>
445449
tables.co64 = Some(data.clone());
446450
}
447451
// MediaHeader and HandlerReference are not sample table data, ignore them
448-
crate::registry::StructuredData::MediaHeader(_) => {},
449-
crate::registry::StructuredData::HandlerReference(_) => {},
452+
crate::registry::StructuredData::MediaHeader(_) => {}
453+
crate::registry::StructuredData::HandlerReference(_) => {}
450454
}
451455
}
452456
}
@@ -571,68 +575,72 @@ fn get_composition_offset_from_ctts(
571575

572576
fn get_sample_file_offset(tables: &SampleTables, sample_index: u32) -> u64 {
573577
// Calculate actual file offset using stsc + stco/co64 + stsz
574-
578+
575579
let stsc = match &tables.stsc {
576580
Some(data) => data,
577581
None => return 0, // No chunk mapping available
578582
};
579-
583+
580584
let stsz = match &tables.stsz {
581585
Some(data) => data,
582586
None => return 0, // No sample sizes available
583587
};
584-
588+
585589
// Get chunk offsets (prefer 64-bit if available)
586590
let chunk_offsets: Vec<u64> = if let Some(co64) = &tables.co64 {
587591
co64.chunk_offsets.clone()
588592
} else if let Some(stco) = &tables.stco {
589-
stco.chunk_offsets.iter().map(|&offset| offset as u64).collect()
593+
stco.chunk_offsets
594+
.iter()
595+
.map(|&offset| offset as u64)
596+
.collect()
590597
} else {
591598
return 0; // No chunk offsets available
592599
};
593-
600+
594601
// Find which chunk contains this sample (1-based sample indexing in MP4)
595602
let target_sample = sample_index + 1;
596603
let mut current_sample = 1u32;
597604
let mut chunk_index = 0usize;
598605
let mut samples_per_chunk = 0u32;
599-
606+
600607
for (i, entry) in stsc.entries.iter().enumerate() {
601608
// Calculate how many samples are covered by previous chunks with this entry's configuration
602609
let next_first_chunk = if i + 1 < stsc.entries.len() {
603610
stsc.entries[i + 1].first_chunk
604611
} else {
605612
chunk_offsets.len() as u32 + 1 // Beyond last chunk
606613
};
607-
614+
608615
samples_per_chunk = entry.samples_per_chunk;
609616
let chunks_with_this_config = next_first_chunk - entry.first_chunk;
610617
let samples_in_this_range = chunks_with_this_config * samples_per_chunk;
611-
618+
612619
if current_sample + samples_in_this_range > target_sample {
613620
// Target sample is in this range
614621
let sample_offset_in_range = target_sample - current_sample;
615-
chunk_index = (entry.first_chunk - 1) as usize + (sample_offset_in_range / samples_per_chunk) as usize;
622+
chunk_index = (entry.first_chunk - 1) as usize
623+
+ (sample_offset_in_range / samples_per_chunk) as usize;
616624
break;
617625
}
618-
626+
619627
current_sample += samples_in_this_range;
620628
}
621-
629+
622630
if chunk_index >= chunk_offsets.len() {
623631
return 0; // Chunk index out of bounds
624632
}
625-
633+
626634
// Get the base offset of the chunk
627635
let chunk_offset = chunk_offsets[chunk_index];
628-
636+
629637
// Calculate which sample within the chunk we want
630638
let sample_in_chunk = ((target_sample - current_sample) % samples_per_chunk) as usize;
631-
639+
632640
// Sum up the sizes of preceding samples in this chunk to get the offset within chunk
633641
let mut offset_in_chunk = 0u64;
634642
let chunk_start_sample = current_sample as usize;
635-
643+
636644
// Handle both fixed and variable sample sizes
637645
if stsz.sample_size > 0 {
638646
// Fixed sample size for all samples
@@ -646,6 +654,6 @@ fn get_sample_file_offset(tables: &SampleTables, sample_index: u32) -> u64 {
646654
}
647655
}
648656
}
649-
657+
650658
chunk_offset + offset_in_chunk
651659
}

tests/registry_tests.rs

Lines changed: 46 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,10 @@ mod tests {
2525
};
2626

2727
let registry = default_registry();
28-
let result = registry.decode(&BoxKey::FourCC(FourCC(*b"stts")), &mut cursor, &header).unwrap().unwrap();
28+
let result = registry
29+
.decode(&BoxKey::FourCC(FourCC(*b"stts")), &mut cursor, &header)
30+
.unwrap()
31+
.unwrap();
2932

3033
match result {
3134
BoxValue::Structured(StructuredData::DecodingTimeToSample(stts_data)) => {
@@ -65,7 +68,10 @@ mod tests {
6568
};
6669

6770
let registry = default_registry();
68-
let result = registry.decode(&BoxKey::FourCC(FourCC(*b"stsz")), &mut cursor, &header).unwrap().unwrap();
71+
let result = registry
72+
.decode(&BoxKey::FourCC(FourCC(*b"stsz")), &mut cursor, &header)
73+
.unwrap()
74+
.unwrap();
6975

7076
match result {
7177
BoxValue::Structured(StructuredData::SampleSize(stsz_data)) => {
@@ -106,7 +112,10 @@ mod tests {
106112
};
107113

108114
let registry = default_registry();
109-
let result = registry.decode(&BoxKey::FourCC(FourCC(*b"stsc")), &mut cursor, &header).unwrap().unwrap();
115+
let result = registry
116+
.decode(&BoxKey::FourCC(FourCC(*b"stsc")), &mut cursor, &header)
117+
.unwrap()
118+
.unwrap();
110119

111120
match result {
112121
BoxValue::Structured(StructuredData::SampleToChunk(stsc_data)) => {
@@ -132,12 +141,12 @@ mod tests {
132141
// Create mock CTTS box data (without version/flags)
133142
let mock_data = vec![
134143
0, 0, 0, 3, // entry_count = 3
135-
0, 0, 0, 5, // sample_count = 5
136-
0, 0, 1, 0, // sample_offset = 256
137-
0, 0, 0, 2, // sample_count = 2
144+
0, 0, 0, 5, // sample_count = 5
145+
0, 0, 1, 0, // sample_offset = 256
146+
0, 0, 0, 2, // sample_count = 2
138147
255, 255, 255, 0, // sample_offset = -256 (signed)
139-
0, 0, 0, 1, // sample_count = 1
140-
0, 0, 2, 0, // sample_offset = 512
148+
0, 0, 0, 1, // sample_count = 1
149+
0, 0, 2, 0, // sample_offset = 512
141150
];
142151

143152
let mut cursor = Cursor::new(mock_data);
@@ -150,7 +159,10 @@ mod tests {
150159
};
151160

152161
let registry = default_registry();
153-
let result = registry.decode(&BoxKey::FourCC(FourCC(*b"ctts")), &mut cursor, &header).unwrap().unwrap();
162+
let result = registry
163+
.decode(&BoxKey::FourCC(FourCC(*b"ctts")), &mut cursor, &header)
164+
.unwrap()
165+
.unwrap();
154166

155167
match result {
156168
BoxValue::Structured(StructuredData::CompositionTimeToSample(ctts_data)) => {
@@ -193,7 +205,10 @@ mod tests {
193205
};
194206

195207
let registry = default_registry();
196-
let result = registry.decode(&BoxKey::FourCC(FourCC(*b"stss")), &mut cursor, &header).unwrap().unwrap();
208+
let result = registry
209+
.decode(&BoxKey::FourCC(FourCC(*b"stss")), &mut cursor, &header)
210+
.unwrap()
211+
.unwrap();
197212

198213
match result {
199214
BoxValue::Structured(StructuredData::SyncSample(stss_data)) => {
@@ -231,7 +246,10 @@ mod tests {
231246
};
232247

233248
let registry = default_registry();
234-
let result = registry.decode(&BoxKey::FourCC(FourCC(*b"stco")), &mut cursor, &header).unwrap().unwrap();
249+
let result = registry
250+
.decode(&BoxKey::FourCC(FourCC(*b"stco")), &mut cursor, &header)
251+
.unwrap()
252+
.unwrap();
235253

236254
match result {
237255
BoxValue::Structured(StructuredData::ChunkOffset(stco_data)) => {
@@ -267,7 +285,10 @@ mod tests {
267285
};
268286

269287
let registry = default_registry();
270-
let result = registry.decode(&BoxKey::FourCC(FourCC(*b"co64")), &mut cursor, &header).unwrap().unwrap();
288+
let result = registry
289+
.decode(&BoxKey::FourCC(FourCC(*b"co64")), &mut cursor, &header)
290+
.unwrap()
291+
.unwrap();
271292

272293
match result {
273294
BoxValue::Structured(StructuredData::ChunkOffset64(co64_data)) => {
@@ -296,18 +317,15 @@ mod tests {
296317
0, 0, // pre_defined
297318
0, 0, // reserved
298319
0, 0, 0, 0, 0, 0, 0, 0, // pre_defined[3]
299-
0, 0, 0, 0,
300-
7, 128, // width = 1920
301-
4, 56, // height = 1080
320+
0, 0, 0, 0, 7, 128, // width = 1920
321+
4, 56, // height = 1080
302322
0, 72, 0, 0, // horizresolution = 72 dpi
303323
0, 72, 0, 0, // vertresolution = 72 dpi
304324
0, 0, 0, 0, // reserved
305325
0, 1, // frame_count = 1
306326
0, 0, 0, 0, 0, 0, 0, 0, // compressorname (32 bytes)
307-
0, 0, 0, 0, 0, 0, 0, 0,
308-
0, 0, 0, 0, 0, 0, 0, 0,
309-
0, 0, 0, 0, 0, 0, 0, 0,
310-
0, 24, // depth = 24
327+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
328+
24, // depth = 24
311329
255, 255, // pre_defined
312330
];
313331

@@ -321,7 +339,10 @@ mod tests {
321339
};
322340

323341
let registry = default_registry();
324-
let result = registry.decode(&BoxKey::FourCC(FourCC(*b"stsd")), &mut cursor, &header).unwrap().unwrap();
342+
let result = registry
343+
.decode(&BoxKey::FourCC(FourCC(*b"stsd")), &mut cursor, &header)
344+
.unwrap()
345+
.unwrap();
325346

326347
match result {
327348
BoxValue::Structured(StructuredData::SampleDescription(stsd_data)) => {
@@ -352,8 +373,7 @@ mod tests {
352373
0, 0, 0, 0, 0, 0, // reserved
353374
0, 1, // data_reference_index = 1
354375
0, 0, 0, 0, // reserved[2]
355-
0, 0, 0, 0,
356-
0, 2, // channelcount = 2 (stereo)
376+
0, 0, 0, 0, 0, 2, // channelcount = 2 (stereo)
357377
0, 16, // samplesize = 16 bits
358378
0, 0, // pre_defined
359379
0, 0, // reserved
@@ -370,7 +390,10 @@ mod tests {
370390
};
371391

372392
let registry = default_registry();
373-
let result = registry.decode(&BoxKey::FourCC(FourCC(*b"stsd")), &mut cursor, &header).unwrap().unwrap();
393+
let result = registry
394+
.decode(&BoxKey::FourCC(FourCC(*b"stsd")), &mut cursor, &header)
395+
.unwrap()
396+
.unwrap();
374397

375398
match result {
376399
BoxValue::Structured(StructuredData::SampleDescription(stsd_data)) => {

0 commit comments

Comments
 (0)