mp4box 0.10.0

MP4/ISOBMFF parser and editor: box trees, typed decoding, sample tables, fragmented MP4, and non-destructive editing
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
use anyhow::Context;
use serde::Serialize;
use std::fs::File;
use std::io::{Read, Seek, SeekFrom};
use std::path::Path;

#[derive(Debug, Clone, Serialize)]
pub struct SampleInfo {
    /// 0-based sample index
    pub index: u32,

    /// Decode time (DTS) in track timescale units
    pub dts: u64,

    /// Presentation time (PTS) in track timescale units (DTS + composition offset)
    pub pts: u64,

    /// Start time in seconds (pts / timescale as f64)
    pub start_time: f64,

    /// Duration in track timescale units (from stts)
    pub duration: u32,

    /// Composition/rendered offset in track timescale units (from ctts, may be 0)
    pub rendered_offset: i64,

    /// Byte offset in the file (from stsc + stco/co64)
    pub file_offset: u64,

    /// Sample size in bytes (from stsz)
    pub size: u32,

    /// Whether this sample is a sync sample / keyframe (from stss)
    pub is_sync: bool,
}

/// Complete sample information and metadata for a single track in an MP4 file.
///
/// This structure represents all the sample-level information extracted from an MP4 track,
/// combining metadata from the track header and media information with detailed sample
/// data parsed from the sample table boxes (stbl). It provides a complete view of a
/// track's temporal structure, timing information, and individual sample properties.
///
/// The struct is designed for media analysis, debugging, and applications that need
/// detailed insight into MP4 file structure and sample organization.
///
/// # Fields
///
/// * `track_id` - Unique identifier for this track within the MP4 file (from tkhd box).
///   Track IDs are typically sequential starting from 1, but can have gaps.
///
/// * `handler_type` - Four-character code indicating the media type (from hdlr box):
///   - `"vide"` - Video track
///   - `"soun"` - Audio track
///   - `"hint"` - Hint track
///   - `"meta"` - Metadata track
///   - `"subt"` - Subtitle track
///   - And other standardized or custom handler types
///
/// * `timescale` - Time coordinate system for this track (from mdhd box).
///   Defines the number of time units per second. For example:
///   - Video tracks often use 90000 (90kHz) or frame rate multiples
///   - Audio tracks commonly use the sample rate (e.g., 48000 for 48kHz)
///
/// * `duration` - Total track duration in track timescale units (from mdhd box).
///   To get duration in seconds: `duration as f64 / timescale as f64`
///
/// * `sample_count` - Total number of samples/frames in this track.
///   Should equal `samples.len()` when all samples are successfully parsed.
///
/// * `samples` - Detailed information for each individual sample in the track.
///   Ordered chronologically by decode time (DTS). Each `SampleInfo` contains
///   timing, size, sync status, and file offset information.
///
/// # Example
///
/// ```rust,no_run
/// use mp4box::track_samples_from_path;
///
/// let track_samples = track_samples_from_path("video.mp4").unwrap();
///
/// for track in track_samples {
///     println!("Track {}: {} ({} samples)",
///              track.track_id,
///              track.handler_type,
///              track.sample_count);
///
///     let duration_sec = track.duration as f64 / track.timescale as f64;
///     println!("Duration: {:.2} seconds", duration_sec);
///
///     if track.handler_type == "vide" {
///         let keyframes = track.samples.iter()
///             .filter(|s| s.is_sync)
///             .count();
///         println!("Keyframes: {}", keyframes);
///     }
/// }
/// ```
#[derive(Debug, Clone, Serialize)]
pub struct TrackSamples {
    pub track_id: u32,
    pub handler_type: String, // "vide", "soun", etc.
    pub timescale: u32,
    pub duration: u64, // in track timescale units
    pub sample_count: u32,
    pub samples: Vec<SampleInfo>,
}

/// Extracts sample information from all tracks in an MP4 file using a generic reader.
///
/// This function reads an MP4 file from any source that implements `Read + Seek` (such as
/// a file, buffer, or network stream) and extracts detailed sample information from all
/// video and audio tracks found in the file.
///
/// # Parameters
///
/// * `reader` - A mutable reference to any type implementing `Read + Seek` traits.
///   The reader will be used to parse the MP4 box structure and extract sample data.
///
/// # Returns
///
/// Returns `Ok(Vec<TrackSamples>)` containing sample information for each track found,
/// or an `Err` if the file cannot be parsed or is not a valid MP4 file.
///
/// Each `TrackSamples` contains:
/// - Track metadata (ID, handler type, timescale, duration)
/// - Individual sample information (timing, size, sync status, file offsets)
///
/// # Errors
///
/// This function may return an error in the following cases:
/// - I/O errors when reading from the source
/// - Invalid or corrupted MP4 file structure
/// - Missing required MP4 boxes (moov, trak, etc.)
/// - Memory allocation failures for large files
///
/// # Example
///
/// ```rust,no_run
/// use std::fs::File;
/// use mp4box::track_samples_from_reader;
///
/// let file = File::open("video.mp4").unwrap();
/// let track_samples = track_samples_from_reader(file).unwrap();
///
/// for track in track_samples {
///     println!("Track {}: {} samples", track.track_id, track.sample_count);
/// }
/// ```
pub fn track_samples_from_reader<R: Read + Seek>(
    mut reader: R,
) -> anyhow::Result<Vec<TrackSamples>> {
    let file_size = reader.seek(SeekFrom::End(0))?;
    reader.seek(SeekFrom::Start(0))?;

    let boxes = crate::get_boxes(&mut reader, file_size, /*decode=*/ true)
        .context("getting boxes from reader")?;

    let mut result = Vec::new();

    for moov_box in boxes.iter().filter(|b| b.typ == "moov") {
        if let Some(children) = &moov_box.children {
            for trak_box in children.iter().filter(|b| b.typ == "trak") {
                if let Some(track_samples) =
                    crate::samples::extract_track_samples(trak_box, &mut reader)?
                {
                    result.push(track_samples);
                }
            }
        }
    }

    // Fragmented MP4: samples live in moof/traf/trun rather than stbl.
    extract_fragment_samples(&boxes, &mut result);

    Ok(result)
}

/// Append samples described by movie fragments (`moof`) to their tracks.
///
/// Implements the ISO 14496-12 §8.8 defaulting chain: per-sample trun values,
/// then trun first_sample_flags (first sample only), then tfhd defaults, then
/// trex defaults. Byte offsets use tfhd base_data_offset when present and the
/// enclosing moof's start offset otherwise (the default-base-is-moof rule,
/// which is also the practical fallback for legacy files).
fn extract_fragment_samples(boxes: &[crate::Box], tracks: &mut [TrackSamples]) {
    use crate::registry::{StructuredData, TrexData};
    use std::collections::HashMap;

    // trex defaults from moov > mvex > trex
    let mut trex_map: HashMap<u32, TrexData> = HashMap::new();
    for moov in boxes.iter().filter(|b| b.typ == "moov") {
        let Some(kids) = &moov.children else { continue };
        for mvex in kids.iter().filter(|b| b.typ == "mvex") {
            let Some(mkids) = &mvex.children else {
                continue;
            };
            for trex in mkids.iter().filter(|b| b.typ == "trex") {
                if let Some(StructuredData::TrackExtends(d)) = &trex.structured_data {
                    trex_map.insert(d.track_id, d.clone());
                }
            }
        }
    }

    // Running decode time per track, for trafs without a tfdt.
    let mut next_dts: HashMap<u32, u64> = tracks
        .iter()
        .map(|t| {
            let end = t
                .samples
                .last()
                .map(|s| s.dts + s.duration as u64)
                .unwrap_or(0);
            (t.track_id, end)
        })
        .collect();

    let original_counts: Vec<usize> = tracks.iter().map(|t| t.samples.len()).collect();

    for moof in boxes.iter().filter(|b| b.typ == "moof") {
        let moof_start = moof.offset;
        let Some(kids) = &moof.children else { continue };

        for traf in kids.iter().filter(|b| b.typ == "traf") {
            let Some(tkids) = &traf.children else {
                continue;
            };
            let Some(tfhd) = tkids.iter().find_map(|b| match &b.structured_data {
                Some(StructuredData::TrackFragmentHeader(d)) => Some(d),
                _ => None,
            }) else {
                continue;
            };
            let tfdt = tkids.iter().find_map(|b| match &b.structured_data {
                Some(StructuredData::TrackFragmentDecodeTime(d)) => Some(d),
                _ => None,
            });
            let Some(track) = tracks.iter_mut().find(|t| t.track_id == tfhd.track_id) else {
                continue;
            };
            let trex = trex_map.get(&tfhd.track_id);

            let mut dts = tfdt
                .map(|d| d.base_media_decode_time)
                .unwrap_or_else(|| next_dts.get(&tfhd.track_id).copied().unwrap_or(0));

            let base = tfhd.base_data_offset.unwrap_or(moof_start);
            // Where the previous trun's data ended, for truns without a
            // data_offset of their own.
            let mut cursor: Option<u64> = None;

            for trun_box in tkids.iter().filter(|b| b.typ == "trun") {
                let Some(StructuredData::TrackFragmentRun(trun)) = &trun_box.structured_data else {
                    continue;
                };

                let mut offset = match trun.data_offset {
                    Some(d) => base.saturating_add_signed(d as i64),
                    None => cursor.unwrap_or(base),
                };

                for (j, s) in trun.samples.iter().enumerate() {
                    let duration = s
                        .duration
                        .or(tfhd.default_sample_duration)
                        .or(trex.map(|t| t.default_sample_duration))
                        .unwrap_or(0);
                    let size = s
                        .size
                        .or(tfhd.default_sample_size)
                        .or(trex.map(|t| t.default_sample_size))
                        .unwrap_or(0);
                    let flags = s
                        .flags
                        .or(if j == 0 {
                            trun.first_sample_flags
                        } else {
                            None
                        })
                        .or(tfhd.default_sample_flags)
                        .or(trex.map(|t| t.default_sample_flags))
                        .unwrap_or(0);
                    let cto = s.composition_time_offset.unwrap_or(0);
                    let pts = dts.saturating_add_signed(cto as i64);

                    track.samples.push(SampleInfo {
                        index: track.samples.len() as u32,
                        dts,
                        pts,
                        start_time: if track.timescale > 0 {
                            pts as f64 / track.timescale as f64
                        } else {
                            0.0
                        },
                        duration,
                        rendered_offset: cto as i64,
                        file_offset: offset,
                        size,
                        // sample_flags bit 16: sample_is_non_sync_sample
                        is_sync: flags & 0x0001_0000 == 0,
                    });

                    offset += size as u64;
                    dts += duration as u64;
                }
                cursor = Some(offset);
            }
            next_dts.insert(tfhd.track_id, dts);
        }
    }

    // Fragmented files usually declare duration 0 in mdhd; fill in the
    // observed duration for tracks that gained fragment samples.
    for (track, original) in tracks.iter_mut().zip(original_counts) {
        if track.samples.len() == original {
            continue;
        }
        track.sample_count = track.samples.len() as u32;
        let end = track
            .samples
            .last()
            .map(|s| s.dts + s.duration as u64)
            .unwrap_or(0);
        if track.duration < end {
            track.duration = end;
        }
    }
}

/// Extracts sample information from all tracks in an MP4 file specified by file path.
///
/// This is a convenience function that opens a file from the filesystem and delegates
/// to `track_samples_from_reader()` to perform the actual parsing. It's the most common
/// way to extract sample information when working with MP4 files on disk.
///
/// # Parameters
///
/// * `path` - A path-like type (anything implementing `AsRef<Path>`) pointing to the
///   MP4 file to analyze. This includes `String`, `&str`, `PathBuf`, and `&Path`.
///
/// # Returns
///
/// Returns `Ok(Vec<TrackSamples>)` containing sample information for each track found,
/// or an `Err` if the file cannot be opened, read, or parsed.
///
/// # Errors
///
/// This function may return an error in the following cases:
/// - File not found or insufficient permissions to read the file
/// - All errors that can occur in `track_samples_from_reader()`
/// - Invalid or corrupted MP4 file structure
/// - Missing required MP4 boxes (moov, trak, etc.)
///
/// # Example
///
/// ```rust,no_run
/// use mp4box::track_samples_from_path;
/// use std::path::Path;
///
/// fn main() -> Result<(), Box<dyn std::error::Error>> {
///     // Using string literal
///     let samples = track_samples_from_path("video.mp4")?;
///
///     // Using Path
///     let path = Path::new("/path/to/video.mp4");
///     let samples = track_samples_from_path(path)?;
///
///     for track in samples {
///         println!("Track {} has {} samples of type {}",
///                  track.track_id, track.sample_count, track.handler_type);
///     }
///     Ok(())
/// }
/// ```
pub fn track_samples_from_path(path: impl AsRef<Path>) -> anyhow::Result<Vec<TrackSamples>> {
    let file = File::open(path)?;
    track_samples_from_reader(file)
}

/// Extracts sample information from a single track box (trak) in an MP4 file.
///
/// This function processes a specific track box from an already-parsed MP4 file structure
/// and extracts all sample-related information from its sample table boxes (stbl).
/// It's a lower-level function typically used internally by `track_samples_from_reader()`.
///
/// The function navigates through the MP4 box hierarchy (trak → mdia → minf → stbl) to
/// locate and parse the various sample table boxes (stts, stsc, stsz, stco, etc.) that
/// contain the sample metadata.
///
/// # Parameters
///
/// * `trak_box` - A reference to a parsed track box (`trak`) from an MP4 file. This box
///   should contain the complete track structure including media information and sample tables.
/// * `reader` - A mutable reference to the file reader, used for seeking to specific
///   byte offsets when calculating sample file positions.
///
/// # Returns
///
/// Returns:
/// - `Ok(Some(TrackSamples))` - Successfully extracted sample information from the track
/// - `Ok(None)` - Track box is valid but contains no usable sample information
/// - `Err(anyhow::Error)` - Failed to parse the track due to structural issues
///
/// The returned `TrackSamples` contains:
/// - Track metadata (ID, media handler type, timescale, duration)
/// - Complete sample information (timing, sizes, sync points, file offsets)
///
/// # Errors
///
/// This function may return an error in the following cases:
/// - Missing required child boxes (mdia, minf, stbl)
/// - Corrupted or invalid sample table data
/// - Inconsistent sample counts between different sample tables
/// - I/O errors when calculating file offsets
/// - Memory allocation failures for tracks with many samples
///
/// # Example
///
/// ```rust,no_run
/// use mp4box::get_boxes;
/// use mp4box::samples::extract_track_samples;
/// use std::fs::File;
///
/// fn main() -> Result<(), Box<dyn std::error::Error>> {
///     let mut file = File::open("video.mp4")?;
///     let file_size = file.metadata()?.len();
///     let boxes = get_boxes(&mut file, file_size, true)?;
///
///     // Find moov box and extract samples from each track
///     for moov_box in boxes.iter().filter(|b| b.typ == "moov") {
///         if let Some(children) = &moov_box.children {
///             for trak_box in children.iter().filter(|b| b.typ == "trak") {
///                 if let Some(samples) = extract_track_samples(trak_box, &mut file)? {
///                     println!("Found track with {} samples", samples.sample_count);
///                 }
///             }
///         }
///     }
///     Ok(())
/// }
/// ```
pub fn extract_track_samples<R: Read + Seek>(
    trak_box: &crate::Box,
    reader: &mut R,
) -> anyhow::Result<Option<TrackSamples>> {
    // use crate::{BoxValue, StructuredData}; // Will be used when we implement proper parsing

    // Find track ID from tkhd
    let track_id = find_track_id(trak_box)?;

    // Find handler type from mdhd
    let (handler_type, timescale, duration) = find_media_info(trak_box)?;

    // Find sample table (stbl) box
    let stbl_box = find_stbl_box(trak_box)?;

    // Extract sample table data
    let sample_tables = extract_sample_tables(stbl_box)?;

    // Build sample information from the tables
    let _ = reader;
    let samples = build_sample_info(&sample_tables, timescale)?;
    let sample_count = samples.len() as u32;

    Ok(Some(TrackSamples {
        track_id,
        handler_type,
        timescale,
        duration,
        sample_count,
        samples,
    }))
}

fn find_track_id(trak_box: &crate::Box) -> anyhow::Result<u32> {
    use crate::registry::StructuredData;

    // Look for tkhd box to get track ID
    if let Some(children) = &trak_box.children {
        for child in children {
            if child.typ == "tkhd" {
                // Extract track ID from structured data
                if let Some(StructuredData::TrackHeader(tkhd_data)) = &child.structured_data {
                    return Ok(tkhd_data.track_id);
                }
            }
        }
    }
    anyhow::bail!("No tkhd box found or track ID could not be parsed")
}

fn find_media_info(trak_box: &crate::Box) -> anyhow::Result<(String, u32, u64)> {
    use crate::registry::StructuredData;

    // Look for mdia/mdhd and mdia/hdlr boxes
    if let Some(children) = &trak_box.children {
        for child in children {
            if child.typ == "mdia"
                && let Some(mdia_children) = &child.children
            {
                let mut timescale = 1000; // Default
                let mut duration = 0; // Default
                let mut handler_type = String::from("vide"); // Default

                for mdia_child in mdia_children {
                    if mdia_child.typ == "mdhd" {
                        // Parse timescale and duration from mdhd
                        if let Some(StructuredData::MediaHeader(mdhd_data)) =
                            &mdia_child.structured_data
                        {
                            timescale = mdhd_data.timescale;
                            duration = mdhd_data.duration;
                        }
                    }
                    if mdia_child.typ == "hdlr" {
                        // Parse handler type from hdlr
                        if let Some(StructuredData::HandlerReference(hdlr_data)) =
                            &mdia_child.structured_data
                        {
                            handler_type = hdlr_data.handler_type.clone();
                        }
                    }
                }

                return Ok((handler_type, timescale, duration));
            }
        }
    }
    Ok((String::from("vide"), 1000, 0))
}

fn find_stbl_box(trak_box: &crate::Box) -> anyhow::Result<&crate::Box> {
    // Navigate to mdia/minf/stbl
    if let Some(children) = &trak_box.children {
        for child in children {
            if child.typ == "mdia"
                && let Some(mdia_children) = &child.children
            {
                for mdia_child in mdia_children {
                    if mdia_child.typ == "minf"
                        && let Some(minf_children) = &mdia_child.children
                    {
                        for minf_child in minf_children {
                            if minf_child.typ == "stbl" {
                                return Ok(minf_child);
                            }
                        }
                    }
                }
            }
        }
    }
    anyhow::bail!("stbl box not found")
}

#[derive(Debug)]
struct SampleTables {
    stsd: Option<crate::registry::StsdData>,
    stts: Option<crate::registry::SttsData>,
    ctts: Option<crate::registry::CttsData>,
    stsc: Option<crate::registry::StscData>,
    stsz: Option<crate::registry::StszData>,
    stss: Option<crate::registry::StssData>,
    stco: Option<crate::registry::StcoData>,
    co64: Option<crate::registry::Co64Data>,
}

fn extract_sample_tables(stbl_box: &crate::Box) -> anyhow::Result<SampleTables> {
    let mut tables = SampleTables {
        stsd: None,
        stts: None,
        ctts: None,
        stsc: None,
        stsz: None,
        stss: None,
        stco: None,
        co64: None,
    };

    // Extract structured data directly from child boxes
    if let Some(children) = &stbl_box.children {
        for child in children {
            if let Some(structured_data) = &child.structured_data {
                match structured_data {
                    crate::registry::StructuredData::SampleDescription(data) => {
                        tables.stsd = Some(data.clone());
                    }
                    crate::registry::StructuredData::DecodingTimeToSample(data) => {
                        tables.stts = Some(data.clone());
                    }
                    crate::registry::StructuredData::CompositionTimeToSample(data) => {
                        tables.ctts = Some(data.clone());
                    }
                    crate::registry::StructuredData::SampleToChunk(data) => {
                        tables.stsc = Some(data.clone());
                    }
                    crate::registry::StructuredData::SampleSize(data) => {
                        tables.stsz = Some(data.clone());
                    }
                    crate::registry::StructuredData::SyncSample(data) => {
                        tables.stss = Some(data.clone());
                    }
                    crate::registry::StructuredData::ChunkOffset(data) => {
                        tables.stco = Some(data.clone());
                    }
                    crate::registry::StructuredData::ChunkOffset64(data) => {
                        tables.co64 = Some(data.clone());
                    }
                    // Headers, fragment boxes, etc. are not sample table data
                    _ => {}
                }
            }
        }
    }

    Ok(tables)
}

/// Sequential cursor over run-length encoded table entries (stts/ctts).
/// Yields one value per sample in a single forward pass.
struct RunCursor<'a, T> {
    entries: &'a [T],
    idx: usize,
    used: u32,
}

impl<'a, T> RunCursor<'a, T> {
    fn new(entries: &'a [T]) -> Self {
        Self {
            entries,
            idx: 0,
            used: 0,
        }
    }

    /// Advance one sample; returns the entry covering it, or `None` once the
    /// table is exhausted.
    fn next(&mut self, count_of: impl Fn(&T) -> u32) -> Option<&'a T> {
        while let Some(e) = self.entries.get(self.idx) {
            if self.used < count_of(e) {
                self.used += 1;
                return Some(e);
            }
            self.idx += 1;
            self.used = 0;
        }
        None
    }
}

fn build_sample_info(tables: &SampleTables, timescale: u32) -> anyhow::Result<Vec<SampleInfo>> {
    // Sample count and sizes come from stsz (or stz2, which decodes to the
    // same shape).
    let Some(stsz) = &tables.stsz else {
        return Ok(Vec::new());
    };
    let sample_count = stsz.sample_count;

    let size_of = |i: u32| -> u32 {
        if stsz.sample_size > 0 {
            stsz.sample_size
        } else {
            stsz.sample_sizes.get(i as usize).copied().unwrap_or(0)
        }
    };

    // ---- File offsets: walk chunks once via stsc + stco/co64 ----
    let chunk_offset_at = |index: usize| -> Option<u64> {
        if let Some(co64) = &tables.co64 {
            co64.chunk_offsets.get(index).copied()
        } else if let Some(stco) = &tables.stco {
            stco.chunk_offsets.get(index).copied().map(u64::from)
        } else {
            None
        }
    };
    let chunk_count = if let Some(co64) = &tables.co64 {
        co64.chunk_offsets.len()
    } else if let Some(stco) = &tables.stco {
        stco.chunk_offsets.len()
    } else {
        0
    };

    // Samples not covered by the tables keep offset 0 (unknown).
    let mut file_offsets = vec![0u64; sample_count as usize];
    if let Some(stsc) = &tables.stsc {
        let mut sample_idx = 0u32;
        'chunks: for (i, entry) in stsc.entries.iter().enumerate() {
            // Chunk range covered by this stsc entry (1-based first_chunk).
            let first = (entry.first_chunk.max(1) - 1) as usize;
            let next_first = stsc
                .entries
                .get(i + 1)
                .map(|e| (e.first_chunk.max(1) - 1) as usize)
                .unwrap_or(chunk_count);

            for chunk in first..next_first.min(chunk_count) {
                let Some(base) = chunk_offset_at(chunk) else {
                    break 'chunks;
                };
                let mut offset = base;
                for _ in 0..entry.samples_per_chunk {
                    if sample_idx >= sample_count {
                        break 'chunks;
                    }
                    file_offsets[sample_idx as usize] = offset;
                    offset += size_of(sample_idx) as u64;
                    sample_idx += 1;
                }
            }
        }
    }

    // ---- Timing and sync tables, one forward pass each ----
    let empty_stts = Vec::new();
    let mut stts_cursor = RunCursor::new(
        tables
            .stts
            .as_ref()
            .map(|t| t.entries.as_slice())
            .unwrap_or(&empty_stts),
    );
    let empty_ctts = Vec::new();
    let mut ctts_cursor = RunCursor::new(
        tables
            .ctts
            .as_ref()
            .map(|t| t.entries.as_slice())
            .unwrap_or(&empty_ctts),
    );
    let stss_set: Option<std::collections::HashSet<u32>> = tables
        .stss
        .as_ref()
        .map(|s| s.sample_numbers.iter().copied().collect());

    // If stts runs out before sample_count (non-compliant but seen in the
    // wild), keep using its last delta; a missing/empty table yields 0
    // rather than a fabricated frame rate.
    let last_stts_delta = tables
        .stts
        .as_ref()
        .and_then(|t| t.entries.last())
        .map(|e| e.sample_delta)
        .unwrap_or(0);

    let mut samples = Vec::with_capacity(sample_count as usize);
    let mut current_dts = 0u64;

    for i in 0..sample_count {
        let duration = stts_cursor
            .next(|e| e.sample_count)
            .map(|e| e.sample_delta)
            .unwrap_or(last_stts_delta);

        // A ctts that doesn't cover this sample means no composition offset.
        let composition_offset = ctts_cursor
            .next(|e| e.sample_count)
            .map(|e| e.sample_offset)
            .unwrap_or(0);

        let pts = current_dts.saturating_add_signed(composition_offset as i64);

        samples.push(SampleInfo {
            index: i,
            dts: current_dts,
            pts,
            start_time: if timescale > 0 {
                pts as f64 / timescale as f64
            } else {
                0.0
            },
            duration,
            rendered_offset: composition_offset as i64,
            file_offset: file_offsets[i as usize],
            size: size_of(i),
            // stss uses 1-based sample numbers; no stss box = all sync.
            is_sync: stss_set.as_ref().is_none_or(|set| set.contains(&(i + 1))),
        });

        current_dts += duration as u64;
    }

    Ok(samples)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::registry::{StructuredData, TkhdData};

    #[test]
    fn test_find_track_id_from_structured_data() {
        // Create a mock tkhd box with structured data
        let tkhd_data = TkhdData {
            version: 0,
            flags: 0,
            track_id: 42,
            duration: 48000,
            width: 1920.0,
            height: 1080.0,
        };

        let tkhd_box = crate::Box {
            offset: 0,
            size: 0,
            header_size: 0,
            payload_offset: None,
            payload_size: None,
            typ: "tkhd".to_string(),
            uuid: None,
            version: Some(0),
            flags: Some(0),
            kind: "full".to_string(),
            full_name: "Track Header Box".to_string(),
            decoded: None,
            structured_data: Some(StructuredData::TrackHeader(tkhd_data)),
            children: None,
        };

        let trak_box = crate::Box {
            offset: 0,
            size: 0,
            header_size: 0,
            payload_offset: None,
            payload_size: None,
            typ: "trak".to_string(),
            uuid: None,
            version: None,
            flags: None,
            kind: "container".to_string(),
            full_name: "Track Box".to_string(),
            decoded: None,
            structured_data: None,
            children: Some(vec![tkhd_box]),
        };

        // Test that we can extract the correct track ID
        let track_id = find_track_id(&trak_box).unwrap();
        assert_eq!(track_id, 42);
    }

    #[test]
    fn test_find_track_id_multiple_tracks() {
        // Test with different track IDs to ensure each gets the right one
        for expected_id in [1, 3, 7, 255] {
            let tkhd_data = TkhdData {
                version: 0,
                flags: 0,
                track_id: expected_id,
                duration: 24000,
                width: 0.0,
                height: 0.0,
            };

            let tkhd_box = crate::Box {
                offset: 0,
                size: 0,
                header_size: 0,
                payload_offset: None,
                payload_size: None,
                typ: "tkhd".to_string(),
                uuid: None,
                version: Some(0),
                flags: Some(0),
                kind: "full".to_string(),
                full_name: "Track Header Box".to_string(),
                decoded: None,
                structured_data: Some(StructuredData::TrackHeader(tkhd_data)),
                children: None,
            };

            let trak_box = crate::Box {
                offset: 0,
                size: 0,
                header_size: 0,
                payload_offset: None,
                payload_size: None,
                typ: "trak".to_string(),
                uuid: None,
                version: None,
                flags: None,
                kind: "container".to_string(),
                full_name: "Track Box".to_string(),
                decoded: None,
                structured_data: None,
                children: Some(vec![tkhd_box]),
            };

            let track_id = find_track_id(&trak_box).unwrap();
            assert_eq!(track_id, expected_id);
        }
    }

    #[test]
    fn test_find_track_id_no_tkhd_box() {
        // Test error case when no tkhd box is present
        let trak_box = crate::Box {
            offset: 0,
            size: 0,
            header_size: 0,
            payload_offset: None,
            payload_size: None,
            typ: "trak".to_string(),
            uuid: None,
            version: None,
            flags: None,
            kind: "container".to_string(),
            full_name: "Track Box".to_string(),
            decoded: None,
            structured_data: None,
            children: Some(vec![]),
        };

        let result = find_track_id(&trak_box);
        assert!(result.is_err());
        assert!(
            result
                .unwrap_err()
                .to_string()
                .contains("No tkhd box found")
        );
    }
}