crates/format/video/tests/video.rs 2.2 KiB 72 lines Raw
1 //! The rational, the clip range, and the mp4 header.2 3 use std::time::Duration;4 5 use inf3r_video::{Clip, Rational, Timebase, box_of, empty, ftyp};6 7 #[test]8 fn rational_reduces() {9     assert_eq!(Rational::new(30, 60).unwrap(), Rational { num: 1, den: 2 });10 }11 12 #[test]13 fn timebase_converts_both_ways() {14     let base = Timebase::new(1, 1000).unwrap();15     let at = Duration::from_millis(1500);16     assert_eq!(base.ticks(at), 1500);17     assert_eq!(base.time(1500), at);18 }19 20 #[test]21 fn clip_duration_is_frames_over_rate() {22     let clip = Clip::new(640, 360, Rational::fps(30, 1).unwrap(), 90).unwrap();23     assert_eq!(clip.duration(), Duration::from_secs(3));24 }25 26 #[test]27 fn range_rounds_down_to_the_frame() {28     let clip = Clip::new(640, 360, Rational::fps(30, 1).unwrap(), 90).unwrap();29     assert_eq!(30         clip.range(Duration::from_secs(1), Duration::from_secs(2)),31         (30, 60)32     );33     assert_eq!(clip.range(Duration::ZERO, Duration::from_secs(10)), (0, 90));34 }35 36 #[test]37 fn empty_box_is_the_header() {38     assert_eq!(box_of(b"free", &[]), b"\0\0\0\x08free");39 }40 41 #[test]42 fn ftyp_counts_its_compatible_brands() {43     let boxed = ftyp(b"isom", &[b"isom", b"mp41"]);44     assert_eq!(boxed.len(), 8 + 4 + 4 + 8);45     let len = u32::from_be_bytes(boxed[0..4].try_into().unwrap()) as usize;46     assert_eq!(len, boxed.len());47 }48 49 #[test]50 fn empty_mp4_points_at_its_moov() {51     let clip = Clip::new(640, 360, Rational::fps(30, 1).unwrap(), 90).unwrap();52     let mp4 = empty(&clip).unwrap();53     assert_eq!(&mp4[4..8], b"ftyp");54     assert!(mp4.windows(4).any(|w| w == b"moov"));55     assert!(mp4.windows(4).any(|w| w == b"mvhd"));56     let first = u32::from_be_bytes(mp4[0..4].try_into().unwrap()) as usize;57     assert_eq!(&mp4[first + 4..first + 8], b"moov");58 }59 60 #[test]61 fn mvhd_duration_is_counted_in_timescale_ticks() {62     let clip = Clip::new(640, 360, Rational::fps(24, 1).unwrap(), 24).unwrap();63     let mp4 = empty(&clip).unwrap();64     let mvhd = mp4.windows(4).position(|w| w == b"mvhd").unwrap();65     let payload = mvhd + 4;66 67     let timescale = u32::from_be_bytes(mp4[payload + 20..payload + 24].try_into().unwrap());68     let duration = u64::from_be_bytes(mp4[payload + 24..payload + 32].try_into().unwrap());69 70     assert_eq!(timescale, 24);71     assert_eq!(duration, 24);72 }