summaryrefslogtreecommitdiffstats
path: root/third_party/rust/mp4parse_capi/tests
diff options
context:
space:
mode:
Diffstat (limited to 'third_party/rust/mp4parse_capi/tests')
-rw-r--r--third_party/rust/mp4parse_capi/tests/test_chunk_out_of_range.rs45
-rw-r--r--third_party/rust/mp4parse_capi/tests/test_encryption.rs296
-rw-r--r--third_party/rust/mp4parse_capi/tests/test_fragment.rs117
-rw-r--r--third_party/rust/mp4parse_capi/tests/test_rotation.rs41
-rw-r--r--third_party/rust/mp4parse_capi/tests/test_sample_table.rs280
-rw-r--r--third_party/rust/mp4parse_capi/tests/test_workaround_stsc.rs46
6 files changed, 825 insertions, 0 deletions
diff --git a/third_party/rust/mp4parse_capi/tests/test_chunk_out_of_range.rs b/third_party/rust/mp4parse_capi/tests/test_chunk_out_of_range.rs
new file mode 100644
index 0000000000..b3a9543b40
--- /dev/null
+++ b/third_party/rust/mp4parse_capi/tests/test_chunk_out_of_range.rs
@@ -0,0 +1,45 @@
+extern crate mp4parse_capi;
+use mp4parse_capi::*;
+use std::io::Read;
+
+extern "C" fn buf_read(buf: *mut u8, size: usize, userdata: *mut std::os::raw::c_void) -> isize {
+ let input: &mut std::fs::File = unsafe { &mut *(userdata as *mut _) };
+ let buf = unsafe { std::slice::from_raw_parts_mut(buf, size) };
+ match input.read(buf) {
+ Ok(n) => n as isize,
+ Err(_) => -1,
+ }
+}
+
+#[test]
+fn parse_out_of_chunk_range() {
+ let mut file = std::fs::File::open("tests/chunk_out_of_range.mp4").expect("Unknown file");
+ let io = Mp4parseIo {
+ read: Some(buf_read),
+ userdata: &mut file as *mut _ as *mut std::os::raw::c_void,
+ };
+
+ unsafe {
+ let mut parser = std::ptr::null_mut();
+ let mut rv = mp4parse_new(&io, &mut parser);
+ assert_eq!(rv, Mp4parseStatus::Ok);
+ assert!(!parser.is_null());
+
+ let mut counts: u32 = 0;
+ rv = mp4parse_get_track_count(parser, &mut counts);
+ assert_eq!(rv, Mp4parseStatus::Ok);
+ assert_eq!(counts, 1);
+
+ // its first chunk is out of range.
+ // <SampleToChunkBox EntryCount="1">
+ // <BoxInfo Size="28" Type="stsc"/>
+ // <FullBoxInfo Version="0" Flags="0x0"/>
+ // <SampleToChunkEntry FirstChunk="16777217" SamplesPerChunk="17" SampleDescriptionIndex="1"/>
+ //
+ let mut indice = Mp4parseByteData::default();
+ let rv = mp4parse_get_indice_table(parser, 1, &mut indice);
+ assert_eq!(rv, Mp4parseStatus::Invalid);
+
+ mp4parse_free(parser);
+ }
+}
diff --git a/third_party/rust/mp4parse_capi/tests/test_encryption.rs b/third_party/rust/mp4parse_capi/tests/test_encryption.rs
new file mode 100644
index 0000000000..1a47e329fc
--- /dev/null
+++ b/third_party/rust/mp4parse_capi/tests/test_encryption.rs
@@ -0,0 +1,296 @@
+extern crate mp4parse_capi;
+use mp4parse_capi::*;
+use std::io::Read;
+
+extern "C" fn buf_read(buf: *mut u8, size: usize, userdata: *mut std::os::raw::c_void) -> isize {
+ let input: &mut std::fs::File = unsafe { &mut *(userdata as *mut _) };
+ let buf = unsafe { std::slice::from_raw_parts_mut(buf, size) };
+ match input.read(buf) {
+ Ok(n) => n as isize,
+ Err(_) => -1,
+ }
+}
+
+#[test]
+fn parse_cenc() {
+ let mut file = std::fs::File::open("tests/short-cenc.mp4").expect("Unknown file");
+ let io = Mp4parseIo {
+ read: Some(buf_read),
+ userdata: &mut file as *mut _ as *mut std::os::raw::c_void,
+ };
+
+ unsafe {
+ let mut parser = std::ptr::null_mut();
+ let mut rv = mp4parse_new(&io, &mut parser);
+ assert_eq!(rv, Mp4parseStatus::Ok);
+ assert!(!parser.is_null());
+ let mut counts: u32 = 0;
+ rv = mp4parse_get_track_count(parser, &mut counts);
+ assert_eq!(rv, Mp4parseStatus::Ok);
+ assert_eq!(counts, 2);
+
+ // Make sure we have a video track and it's at index 0
+ let mut video_track_info = Mp4parseTrackInfo::default();
+ rv = mp4parse_get_track_info(parser, 0, &mut video_track_info);
+ assert_eq!(rv, Mp4parseStatus::Ok);
+ assert_eq!(video_track_info.track_type, Mp4parseTrackType::Video);
+
+ // Make sure we have a audio track and it's at index 1
+ let mut audio_track_info = Mp4parseTrackInfo::default();
+ rv = mp4parse_get_track_info(parser, 1, &mut audio_track_info);
+ assert_eq!(rv, Mp4parseStatus::Ok);
+ assert_eq!(audio_track_info.track_type, Mp4parseTrackType::Audio);
+
+ // Verify video track and crypto information
+ let mut video = Mp4parseTrackVideoInfo::default();
+ rv = mp4parse_get_track_video_info(parser, 0, &mut video);
+ assert_eq!(rv, Mp4parseStatus::Ok);
+ assert_eq!(video.sample_info_count, 1);
+ assert_eq!((*video.sample_info).codec_type, Mp4parseCodec::Avc);
+ assert_eq!((*video.sample_info).image_width, 320);
+ assert_eq!((*video.sample_info).image_height, 240);
+ let protected_data = &(*video.sample_info).protected_data;
+ assert_eq!(
+ protected_data.original_format,
+ OptionalFourCc::Some(*b"avc1")
+ );
+ assert_eq!(
+ protected_data.scheme_type,
+ Mp4ParseEncryptionSchemeType::Cenc
+ );
+ assert_eq!(protected_data.is_encrypted, 0x01);
+ assert_eq!(protected_data.iv_size, 16);
+ assert_eq!(protected_data.kid.length, 16);
+ let expected_kid = [
+ 0x7e, 0x57, 0x1d, 0x01, 0x7e, 0x57, 0x1d, 0x01, 0x7e, 0x57, 0x1d, 0x01, 0x7e, 0x57,
+ 0x1d, 0x01,
+ ];
+ for (i, expected_byte) in expected_kid.iter().enumerate() {
+ assert_eq!(&(*protected_data.kid.data.add(i)), expected_byte);
+ }
+ assert_eq!(protected_data.crypt_byte_block, 0);
+ assert_eq!(protected_data.skip_byte_block, 0);
+ assert_eq!(protected_data.constant_iv.length, 0);
+
+ // Verify audio track and crypto information
+ let mut audio = Mp4parseTrackAudioInfo::default();
+ rv = mp4parse_get_track_audio_info(parser, 1, &mut audio);
+ assert_eq!(rv, Mp4parseStatus::Ok);
+ assert_eq!(audio.sample_info_count, 1);
+ assert_eq!((*audio.sample_info).codec_type, Mp4parseCodec::Aac);
+ assert_eq!((*audio.sample_info).channels, 2);
+ assert_eq!((*audio.sample_info).bit_depth, 16);
+ assert_eq!((*audio.sample_info).sample_rate, 44100);
+ let protected_data = &(*audio.sample_info).protected_data;
+ assert_eq!(
+ protected_data.original_format,
+ OptionalFourCc::Some(*b"mp4a")
+ );
+ assert_eq!(protected_data.is_encrypted, 0x01);
+ assert_eq!(protected_data.iv_size, 16);
+ assert_eq!(protected_data.kid.length, 16);
+ let expected_kid = [
+ 0x7e, 0x57, 0x1d, 0x02, 0x7e, 0x57, 0x1d, 0x02, 0x7e, 0x57, 0x1d, 0x02, 0x7e, 0x57,
+ 0x1d, 0x02,
+ ];
+ for (i, expected_byte) in expected_kid.iter().enumerate() {
+ assert_eq!(&(*protected_data.kid.data.add(i)), expected_byte);
+ }
+ assert_eq!(protected_data.crypt_byte_block, 0);
+ assert_eq!(protected_data.skip_byte_block, 0);
+ assert_eq!(protected_data.constant_iv.length, 0);
+ }
+}
+
+#[test]
+fn parse_cbcs() {
+ let mut file = std::fs::File::open("tests/bipbop_cbcs_video_init.mp4").expect("Unknown file");
+ let io = Mp4parseIo {
+ read: Some(buf_read),
+ userdata: &mut file as *mut _ as *mut std::os::raw::c_void,
+ };
+
+ unsafe {
+ let mut parser = std::ptr::null_mut();
+ let mut rv = mp4parse_new(&io, &mut parser);
+ assert_eq!(rv, Mp4parseStatus::Ok);
+ assert!(!parser.is_null());
+ let mut counts: u32 = 0;
+ rv = mp4parse_get_track_count(parser, &mut counts);
+ assert_eq!(rv, Mp4parseStatus::Ok);
+ assert_eq!(counts, 1);
+
+ // Make sure we have a video track
+ let mut video_track_info = Mp4parseTrackInfo::default();
+ rv = mp4parse_get_track_info(parser, 0, &mut video_track_info);
+ assert_eq!(rv, Mp4parseStatus::Ok);
+ assert_eq!(video_track_info.track_type, Mp4parseTrackType::Video);
+
+ // Verify video track and crypto information
+ let mut video = Mp4parseTrackVideoInfo::default();
+ rv = mp4parse_get_track_video_info(parser, 0, &mut video);
+ assert_eq!(rv, Mp4parseStatus::Ok);
+ assert_eq!(video.sample_info_count, 2);
+ assert_eq!((*video.sample_info).codec_type, Mp4parseCodec::Avc);
+ assert_eq!((*video.sample_info).image_width, 400);
+ assert_eq!((*video.sample_info).image_height, 300);
+ let protected_data = &(*video.sample_info).protected_data;
+ assert_eq!(
+ protected_data.original_format,
+ OptionalFourCc::Some(*b"avc1")
+ );
+ assert_eq!(
+ protected_data.scheme_type,
+ Mp4ParseEncryptionSchemeType::Cbcs
+ );
+ assert_eq!(protected_data.is_encrypted, 0x01);
+ assert_eq!(protected_data.iv_size, 0);
+ assert_eq!(protected_data.kid.length, 16);
+ let expected_kid = [
+ 0x7e, 0x57, 0x1d, 0x04, 0x7e, 0x57, 0x1d, 0x04, 0x7e, 0x57, 0x1d, 0x04, 0x7e, 0x57,
+ 0x1d, 0x21,
+ ];
+ for (i, expected_byte) in expected_kid.iter().enumerate() {
+ assert_eq!(&(*protected_data.kid.data.add(i)), expected_byte);
+ }
+ assert_eq!(protected_data.crypt_byte_block, 1);
+ assert_eq!(protected_data.skip_byte_block, 9);
+ assert_eq!(protected_data.constant_iv.length, 16);
+ let expected_iv = [
+ 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0x00, 0x11, 0x22, 0x33, 0x44,
+ 0x55, 0x66,
+ ];
+ for (i, expected_byte) in expected_iv.iter().enumerate() {
+ assert_eq!(&(*protected_data.constant_iv.data.add(i)), expected_byte);
+ }
+ }
+}
+
+#[test]
+fn parse_unencrypted() {
+ // Ensure the encryption related data is not populated for files without
+ // encryption metadata.
+ let mut file = std::fs::File::open("tests/opus_audioinit.mp4").expect("Unknown file");
+ let io = Mp4parseIo {
+ read: Some(buf_read),
+ userdata: &mut file as *mut _ as *mut std::os::raw::c_void,
+ };
+
+ unsafe {
+ let mut parser = std::ptr::null_mut();
+ let mut rv = mp4parse_new(&io, &mut parser);
+ assert_eq!(rv, Mp4parseStatus::Ok);
+ assert!(!parser.is_null());
+
+ let mut counts: u32 = 0;
+ rv = mp4parse_get_track_count(parser, &mut counts);
+ assert_eq!(rv, Mp4parseStatus::Ok);
+ assert_eq!(counts, 1);
+
+ let mut track_info = Mp4parseTrackInfo::default();
+ rv = mp4parse_get_track_info(parser, 0, &mut track_info);
+ assert_eq!(rv, Mp4parseStatus::Ok);
+ assert_eq!(track_info.track_type, Mp4parseTrackType::Audio);
+
+ let mut audio = Mp4parseTrackAudioInfo::default();
+ rv = mp4parse_get_track_audio_info(parser, 0, &mut audio);
+ assert_eq!(rv, Mp4parseStatus::Ok);
+ assert_eq!(audio.sample_info_count, 1);
+ let protected_data = &(*audio.sample_info).protected_data;
+ assert_eq!(protected_data.original_format, OptionalFourCc::None);
+ assert_eq!(
+ protected_data.scheme_type,
+ Mp4ParseEncryptionSchemeType::None
+ );
+ assert_eq!(protected_data.is_encrypted, 0x00);
+ assert_eq!(protected_data.iv_size, 0);
+ assert_eq!(protected_data.kid.length, 0);
+ assert_eq!(protected_data.crypt_byte_block, 0);
+ assert_eq!(protected_data.skip_byte_block, 0);
+ assert_eq!(protected_data.constant_iv.length, 0);
+ }
+}
+
+#[test]
+fn parse_encrypted_av1() {
+ // For reference, this file was created from the av1.mp4 in mozilla's media tests using
+ // shaka-packager. The following command was used to produce the file:
+ // ```
+ // packager-win.exe in=av1.mp4,stream=video,output=av1-clearkey-cbcs-video.mp4
+ // --protection_scheme cbcs --enable_raw_key_encryption
+ // --keys label=:key_id=00112233445566778899AABBCCDDEEFF:key=00112233445566778899AABBCCDDEEFF
+ // --iv 11223344556677889900112233445566
+ // ```
+ let mut file = std::fs::File::open("tests/av1-clearkey-cbcs-video.mp4").expect("Unknown file");
+ let io = Mp4parseIo {
+ read: Some(buf_read),
+ userdata: &mut file as *mut _ as *mut std::os::raw::c_void,
+ };
+
+ unsafe {
+ let mut parser = std::ptr::null_mut();
+ let mut rv = mp4parse_new(&io, &mut parser);
+ assert_eq!(rv, Mp4parseStatus::Ok);
+ assert!(!parser.is_null());
+ let mut counts: u32 = 0;
+ rv = mp4parse_get_track_count(parser, &mut counts);
+ assert_eq!(rv, Mp4parseStatus::Ok);
+ assert_eq!(counts, 1);
+
+ // Make sure we have a video track
+ let mut video_track_info = Mp4parseTrackInfo::default();
+ rv = mp4parse_get_track_info(parser, 0, &mut video_track_info);
+ assert_eq!(rv, Mp4parseStatus::Ok);
+ assert_eq!(video_track_info.track_type, Mp4parseTrackType::Video);
+
+ // Verify video track and crypto information
+ let mut video = Mp4parseTrackVideoInfo::default();
+ rv = mp4parse_get_track_video_info(parser, 0, &mut video);
+ assert_eq!(rv, Mp4parseStatus::Ok);
+ assert_eq!(video.sample_info_count, 2);
+ assert_eq!((*video.sample_info).codec_type, Mp4parseCodec::Av1);
+ assert_eq!((*video.sample_info).image_width, 160);
+ assert_eq!((*video.sample_info).image_height, 90);
+
+ // Check that extra data binary blob.
+ let expected_extra_data = [
+ 0x81, 0x00, 0x0c, 0x00, 0x0a, 0x0a, 0x00, 0x00, 0x00, 0x03, 0xb4, 0xfd, 0x97, 0xff,
+ 0xe6, 0x01,
+ ];
+ let extra_data = &(*video.sample_info).extra_data;
+ assert_eq!(extra_data.length, 16);
+ for (i, expected_byte) in expected_extra_data.iter().enumerate() {
+ assert_eq!(&(*extra_data.data.add(i)), expected_byte);
+ }
+
+ let protected_data = &(*video.sample_info).protected_data;
+ assert_eq!(
+ protected_data.original_format,
+ OptionalFourCc::Some(*b"av01")
+ );
+ assert_eq!(
+ protected_data.scheme_type,
+ Mp4ParseEncryptionSchemeType::Cbcs
+ );
+ assert_eq!(protected_data.is_encrypted, 0x01);
+ assert_eq!(protected_data.iv_size, 0);
+ assert_eq!(protected_data.kid.length, 16);
+ let expected_kid = [
+ 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd,
+ 0xee, 0xff,
+ ];
+ for (i, expected_byte) in expected_kid.iter().enumerate() {
+ assert_eq!(&(*protected_data.kid.data.add(i)), expected_byte);
+ }
+ assert_eq!(protected_data.crypt_byte_block, 1);
+ assert_eq!(protected_data.skip_byte_block, 9);
+ assert_eq!(protected_data.constant_iv.length, 16);
+ let expected_iv = [
+ 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0x00, 0x11, 0x22, 0x33, 0x44,
+ 0x55, 0x66,
+ ];
+ for (i, expected_byte) in expected_iv.iter().enumerate() {
+ assert_eq!(&(*protected_data.constant_iv.data.add(i)), expected_byte);
+ }
+ }
+}
diff --git a/third_party/rust/mp4parse_capi/tests/test_fragment.rs b/third_party/rust/mp4parse_capi/tests/test_fragment.rs
new file mode 100644
index 0000000000..d6ebe1f177
--- /dev/null
+++ b/third_party/rust/mp4parse_capi/tests/test_fragment.rs
@@ -0,0 +1,117 @@
+extern crate mp4parse_capi;
+use mp4parse_capi::*;
+use std::io::Read;
+
+extern "C" fn buf_read(buf: *mut u8, size: usize, userdata: *mut std::os::raw::c_void) -> isize {
+ let input: &mut std::fs::File = unsafe { &mut *(userdata as *mut _) };
+ let buf = unsafe { std::slice::from_raw_parts_mut(buf, size) };
+ match input.read(buf) {
+ Ok(n) => n as isize,
+ Err(_) => -1,
+ }
+}
+
+#[test]
+fn parse_fragment() {
+ let mut file = std::fs::File::open("tests/bipbop_audioinit.mp4").expect("Unknown file");
+ let io = Mp4parseIo {
+ read: Some(buf_read),
+ userdata: &mut file as *mut _ as *mut std::os::raw::c_void,
+ };
+
+ unsafe {
+ let mut parser = std::ptr::null_mut();
+ let mut rv = mp4parse_new(&io, &mut parser);
+ assert_eq!(rv, Mp4parseStatus::Ok);
+ assert!(!parser.is_null());
+
+ let mut counts: u32 = 0;
+ rv = mp4parse_get_track_count(parser, &mut counts);
+ assert_eq!(rv, Mp4parseStatus::Ok);
+ assert_eq!(counts, 1);
+
+ let mut track_info = Mp4parseTrackInfo::default();
+ rv = mp4parse_get_track_info(parser, 0, &mut track_info);
+ assert_eq!(rv, Mp4parseStatus::Ok);
+ assert_eq!(track_info.track_type, Mp4parseTrackType::Audio);
+ assert_eq!(track_info.track_id, 1);
+ assert_eq!(track_info.duration, 0);
+ assert_eq!(track_info.media_time, 0);
+
+ let mut audio = Default::default();
+ rv = mp4parse_get_track_audio_info(parser, 0, &mut audio);
+ assert_eq!(rv, Mp4parseStatus::Ok);
+ assert_eq!(audio.sample_info_count, 1);
+
+ assert_eq!((*audio.sample_info).codec_type, Mp4parseCodec::Aac);
+ assert_eq!((*audio.sample_info).channels, 2);
+ assert_eq!((*audio.sample_info).bit_depth, 16);
+ assert_eq!((*audio.sample_info).sample_rate, 22050);
+ assert_eq!((*audio.sample_info).extra_data.length, 27);
+ assert_eq!((*audio.sample_info).codec_specific_config.length, 2);
+
+ let mut is_fragmented_file: u8 = 0;
+ rv = mp4parse_is_fragmented(parser, track_info.track_id, &mut is_fragmented_file);
+ assert_eq!(rv, Mp4parseStatus::Ok);
+ assert_eq!(is_fragmented_file, 1);
+
+ let mut fragment_info = Mp4parseFragmentInfo::default();
+ rv = mp4parse_get_fragment_info(parser, &mut fragment_info);
+ assert_eq!(rv, Mp4parseStatus::Ok);
+ assert_eq!(fragment_info.fragment_duration, 10_032_000);
+
+ mp4parse_free(parser);
+ }
+}
+
+#[test]
+fn parse_opus_fragment() {
+ let mut file = std::fs::File::open("tests/opus_audioinit.mp4").expect("Unknown file");
+ let io = Mp4parseIo {
+ read: Some(buf_read),
+ userdata: &mut file as *mut _ as *mut std::os::raw::c_void,
+ };
+
+ unsafe {
+ let mut parser = std::ptr::null_mut();
+ let mut rv = mp4parse_new(&io, &mut parser);
+ assert_eq!(rv, Mp4parseStatus::Ok);
+ assert!(!parser.is_null());
+
+ let mut counts: u32 = 0;
+ rv = mp4parse_get_track_count(parser, &mut counts);
+ assert_eq!(rv, Mp4parseStatus::Ok);
+ assert_eq!(counts, 1);
+
+ let mut track_info = Mp4parseTrackInfo::default();
+ rv = mp4parse_get_track_info(parser, 0, &mut track_info);
+ assert_eq!(rv, Mp4parseStatus::Ok);
+ assert_eq!(track_info.track_type, Mp4parseTrackType::Audio);
+ assert_eq!(track_info.track_id, 1);
+ assert_eq!(track_info.duration, 0);
+ assert_eq!(track_info.media_time, 0);
+
+ let mut audio = Default::default();
+ rv = mp4parse_get_track_audio_info(parser, 0, &mut audio);
+ assert_eq!(rv, Mp4parseStatus::Ok);
+ assert_eq!(audio.sample_info_count, 1);
+
+ assert_eq!((*audio.sample_info).codec_type, Mp4parseCodec::Opus);
+ assert_eq!((*audio.sample_info).channels, 1);
+ assert_eq!((*audio.sample_info).bit_depth, 16);
+ assert_eq!((*audio.sample_info).sample_rate, 48000);
+ assert_eq!((*audio.sample_info).extra_data.length, 0);
+ assert_eq!((*audio.sample_info).codec_specific_config.length, 19);
+
+ let mut is_fragmented_file: u8 = 0;
+ rv = mp4parse_is_fragmented(parser, track_info.track_id, &mut is_fragmented_file);
+ assert_eq!(rv, Mp4parseStatus::Ok);
+ assert_eq!(is_fragmented_file, 1);
+
+ let mut fragment_info = Mp4parseFragmentInfo::default();
+ rv = mp4parse_get_fragment_info(parser, &mut fragment_info);
+ assert_eq!(rv, Mp4parseStatus::Ok);
+
+ mp4parse_free(parser);
+ }
+}
diff --git a/third_party/rust/mp4parse_capi/tests/test_rotation.rs b/third_party/rust/mp4parse_capi/tests/test_rotation.rs
new file mode 100644
index 0000000000..918c9fbeae
--- /dev/null
+++ b/third_party/rust/mp4parse_capi/tests/test_rotation.rs
@@ -0,0 +1,41 @@
+extern crate mp4parse_capi;
+use mp4parse_capi::*;
+use std::io::Read;
+
+extern "C" fn buf_read(buf: *mut u8, size: usize, userdata: *mut std::os::raw::c_void) -> isize {
+ let input: &mut std::fs::File = unsafe { &mut *(userdata as *mut _) };
+ let buf = unsafe { std::slice::from_raw_parts_mut(buf, size) };
+ match input.read(buf) {
+ Ok(n) => n as isize,
+ Err(_) => -1,
+ }
+}
+
+#[test]
+fn parse_rotation() {
+ let mut file = std::fs::File::open("tests/video_rotation_90.mp4").expect("Unknown file");
+ let io = Mp4parseIo {
+ read: Some(buf_read),
+ userdata: &mut file as *mut _ as *mut std::os::raw::c_void,
+ };
+
+ unsafe {
+ let mut parser = std::ptr::null_mut();
+ let mut rv = mp4parse_new(&io, &mut parser);
+ assert_eq!(rv, Mp4parseStatus::Ok);
+ assert!(!parser.is_null());
+
+ let mut counts: u32 = 0;
+ rv = mp4parse_get_track_count(parser, &mut counts);
+ assert_eq!(rv, Mp4parseStatus::Ok);
+ assert_eq!(counts, 1);
+
+ let mut video = Mp4parseTrackVideoInfo::default();
+
+ let rv = mp4parse_get_track_video_info(parser, 0, &mut video);
+ assert_eq!(rv, Mp4parseStatus::Ok);
+ assert_eq!(video.rotation, 90);
+
+ mp4parse_free(parser);
+ }
+}
diff --git a/third_party/rust/mp4parse_capi/tests/test_sample_table.rs b/third_party/rust/mp4parse_capi/tests/test_sample_table.rs
new file mode 100644
index 0000000000..ad030a94c5
--- /dev/null
+++ b/third_party/rust/mp4parse_capi/tests/test_sample_table.rs
@@ -0,0 +1,280 @@
+extern crate mp4parse;
+extern crate mp4parse_capi;
+use mp4parse::unstable::Indice;
+use mp4parse_capi::*;
+use std::io::Read;
+
+extern "C" fn buf_read(buf: *mut u8, size: usize, userdata: *mut std::os::raw::c_void) -> isize {
+ let input: &mut std::fs::File = unsafe { &mut *(userdata as *mut _) };
+ let buf = unsafe { std::slice::from_raw_parts_mut(buf, size) };
+ match input.read(buf) {
+ Ok(n) => n as isize,
+ Err(_) => -1,
+ }
+}
+
+#[test]
+fn parse_sample_table() {
+ let mut file =
+ std::fs::File::open("tests/bipbop_nonfragment_header.mp4").expect("Unknown file");
+ let io = Mp4parseIo {
+ read: Some(buf_read),
+ userdata: &mut file as *mut _ as *mut std::os::raw::c_void,
+ };
+
+ unsafe {
+ let mut parser = std::ptr::null_mut();
+ let mut rv = mp4parse_new(&io, &mut parser);
+ assert_eq!(rv, Mp4parseStatus::Ok);
+ assert!(!parser.is_null());
+
+ let mut counts: u32 = 0;
+ rv = mp4parse_get_track_count(parser, &mut counts);
+ assert_eq!(rv, Mp4parseStatus::Ok);
+ assert_eq!(counts, 2);
+
+ let mut track_info = Mp4parseTrackInfo::default();
+ rv = mp4parse_get_track_info(parser, 1, &mut track_info);
+ assert_eq!(rv, Mp4parseStatus::Ok);
+ assert_eq!(track_info.track_type, Mp4parseTrackType::Audio);
+
+ // Check audio smaple table
+ let mut is_fragmented_file: u8 = 0;
+ rv = mp4parse_is_fragmented(parser, track_info.track_id, &mut is_fragmented_file);
+ assert_eq!(rv, Mp4parseStatus::Ok);
+ assert_eq!(is_fragmented_file, 0);
+
+ let mut indice = Mp4parseByteData::default();
+ rv = mp4parse_get_indice_table(parser, track_info.track_id, &mut indice);
+ assert_eq!(rv, Mp4parseStatus::Ok);
+
+ // Compare the value from stagefright.
+ let audio_indice_0 = Indice {
+ start_offset: 27_046.into(),
+ end_offset: 27_052.into(),
+ start_composition: 0.into(),
+ end_composition: 46_439.into(),
+ start_decode: 0.into(),
+ sync: true,
+ };
+ let audio_indice_215 = Indice {
+ start_offset: 283_550.into(),
+ end_offset: 283_556.into(),
+ start_composition: 9_984_580.into(),
+ end_composition: 10_031_020.into(),
+ start_decode: 9_984_580.into(),
+ sync: true,
+ };
+ assert_eq!(indice.length, 216);
+ assert_eq!(*indice.indices.offset(0), audio_indice_0);
+ assert_eq!(*indice.indices.offset(215), audio_indice_215);
+
+ // Check video smaple table
+ rv = mp4parse_get_track_info(parser, 0, &mut track_info);
+ assert_eq!(rv, Mp4parseStatus::Ok);
+ assert_eq!(track_info.track_type, Mp4parseTrackType::Video);
+
+ let mut is_fragmented_file: u8 = 0;
+ rv = mp4parse_is_fragmented(parser, track_info.track_id, &mut is_fragmented_file);
+ assert_eq!(rv, Mp4parseStatus::Ok);
+ assert_eq!(is_fragmented_file, 0);
+
+ let mut indice = Mp4parseByteData::default();
+ rv = mp4parse_get_indice_table(parser, track_info.track_id, &mut indice);
+ assert_eq!(rv, Mp4parseStatus::Ok);
+
+ // Compare the last few data from stagefright.
+ let video_indice_291 = Indice {
+ start_offset: 280_226.into(),
+ end_offset: 280_855.into(),
+ start_composition: 9_838_333.into(),
+ end_composition: 9_871_677.into(),
+ start_decode: 9_710_000.into(),
+ sync: false,
+ };
+ let video_indice_292 = Indice {
+ start_offset: 280_855.into(),
+ end_offset: 281_297.into(),
+ start_composition: 9_805_011.into(),
+ end_composition: 9_838_333.into(),
+ start_decode: 9_710_011.into(),
+ sync: false,
+ };
+ // TODO: start_composition time in stagefright is 9905000, but it is 9904999 in parser, it
+ // could be rounding error.
+ //let video_indice_293 = Indice { start_offset: 281_297, end_offset: 281_919, start_composition: 9_905_000, end_composition: 9_938_344, start_decode: 9_776_666, sync: false };
+ //let video_indice_294 = Indice { start_offset: 281_919, end_offset: 282_391, start_composition: 9_871_677, end_composition: 9_905_000, start_decode: 9_776_677, sync: false };
+ let video_indice_295 = Indice {
+ start_offset: 282_391.into(),
+ end_offset: 283_032.into(),
+ start_composition: 9_971_666.into(),
+ end_composition: 9_971_677.into(),
+ start_decode: 9_843_333.into(),
+ sync: false,
+ };
+ let video_indice_296 = Indice {
+ start_offset: 283_092.into(),
+ end_offset: 283_526.into(),
+ start_composition: 9_938_344.into(),
+ end_composition: 9_971_666.into(),
+ start_decode: 9_843_344.into(),
+ sync: false,
+ };
+
+ assert_eq!(indice.length, 297);
+ assert_eq!(*indice.indices.offset(291), video_indice_291);
+ assert_eq!(*indice.indices.offset(292), video_indice_292);
+ //assert_eq!(*indice.indices.offset(293), video_indice_293);
+ //assert_eq!(*indice.indices.offset(294), video_indice_294);
+ assert_eq!(*indice.indices.offset(295), video_indice_295);
+ assert_eq!(*indice.indices.offset(296), video_indice_296);
+
+ mp4parse_free(parser);
+ }
+}
+
+#[test]
+fn parse_sample_table_with_elst() {
+ let mut file = std::fs::File::open("tests/short-cenc.mp4").expect("Unknown file");
+ let io = Mp4parseIo {
+ read: Some(buf_read),
+ userdata: &mut file as *mut _ as *mut std::os::raw::c_void,
+ };
+
+ unsafe {
+ let mut parser = std::ptr::null_mut();
+ let mut rv = mp4parse_new(&io, &mut parser);
+ assert_eq!(rv, Mp4parseStatus::Ok);
+ assert!(!parser.is_null());
+
+ let mut counts: u32 = 0;
+ rv = mp4parse_get_track_count(parser, &mut counts);
+ assert_eq!(rv, Mp4parseStatus::Ok);
+ assert_eq!(counts, 2);
+
+ let mut track_info = Mp4parseTrackInfo::default();
+ rv = mp4parse_get_track_info(parser, 1, &mut track_info);
+ assert_eq!(rv, Mp4parseStatus::Ok);
+ assert_eq!(track_info.track_type, Mp4parseTrackType::Audio);
+
+ // Check audio sample table
+ let mut is_fragmented_file: u8 = std::u8::MAX;
+ rv = mp4parse_is_fragmented(parser, track_info.track_id, &mut is_fragmented_file);
+ assert_eq!(rv, Mp4parseStatus::Ok);
+ assert_eq!(is_fragmented_file, 0);
+
+ let mut indice = Mp4parseByteData::default();
+ rv = mp4parse_get_indice_table(parser, track_info.track_id, &mut indice);
+ assert_eq!(rv, Mp4parseStatus::Ok);
+
+ // Compare the value from stagefright.
+ // Due to 'elst', the start_composition and end_composition are negative
+ // at first two samples.
+ let audio_indice_0 = Indice {
+ start_offset: 6992.into(),
+ end_offset: 7363.into(),
+ start_composition: (-36281).into(),
+ end_composition: (-13062).into(),
+ start_decode: 0.into(),
+ sync: true,
+ };
+ let audio_indice_1 = Indice {
+ start_offset: 7363.into(),
+ end_offset: 7735.into(),
+ start_composition: (-13062).into(),
+ end_composition: 10158.into(),
+ start_decode: 23219.into(),
+ sync: true,
+ };
+ let audio_indice_2 = Indice {
+ start_offset: 7735.into(),
+ end_offset: 8106.into(),
+ start_composition: 10158.into(),
+ end_composition: 33378.into(),
+ start_decode: 46439.into(),
+ sync: true,
+ };
+ assert_eq!(indice.length, 21);
+ assert_eq!(*indice.indices.offset(0), audio_indice_0);
+ assert_eq!(*indice.indices.offset(1), audio_indice_1);
+ assert_eq!(*indice.indices.offset(2), audio_indice_2);
+
+ mp4parse_free(parser);
+ }
+}
+
+#[test]
+fn parse_sample_table_with_negative_ctts() {
+ let mut file = std::fs::File::open("tests/white.mp4").expect("Unknown file");
+ let io = Mp4parseIo {
+ read: Some(buf_read),
+ userdata: &mut file as *mut _ as *mut std::os::raw::c_void,
+ };
+
+ unsafe {
+ let mut parser = std::ptr::null_mut();
+ let mut rv = mp4parse_new(&io, &mut parser);
+ assert_eq!(rv, Mp4parseStatus::Ok);
+ assert!(!parser.is_null());
+
+ let mut counts: u32 = 0;
+ rv = mp4parse_get_track_count(parser, &mut counts);
+ assert_eq!(rv, Mp4parseStatus::Ok);
+ assert_eq!(counts, 1);
+
+ let mut track_info = Mp4parseTrackInfo::default();
+ rv = mp4parse_get_track_info(parser, 0, &mut track_info);
+ assert_eq!(rv, Mp4parseStatus::Ok);
+ assert_eq!(track_info.track_type, Mp4parseTrackType::Video);
+
+ let mut is_fragmented_file: u8 = std::u8::MAX;
+ rv = mp4parse_is_fragmented(parser, track_info.track_id, &mut is_fragmented_file);
+ assert_eq!(rv, Mp4parseStatus::Ok);
+ assert_eq!(is_fragmented_file, 0);
+
+ let mut indice = Mp4parseByteData::default();
+ rv = mp4parse_get_indice_table(parser, track_info.track_id, &mut indice);
+ assert_eq!(rv, Mp4parseStatus::Ok);
+
+ // There are negative value in 'ctts' table.
+ let video_indice_0 = Indice {
+ start_offset: 48.into(),
+ end_offset: 890.into(),
+ start_composition: 0.into(),
+ end_composition: 33_333.into(),
+ start_decode: 0.into(),
+ sync: true,
+ };
+ let video_indice_1 = Indice {
+ start_offset: 890.into(),
+ end_offset: 913.into(),
+ start_composition: 133_333.into(),
+ end_composition: 166_666.into(),
+ start_decode: 33_333.into(),
+ sync: false,
+ };
+ let video_indice_2 = Indice {
+ start_offset: 913.into(),
+ end_offset: 934.into(),
+ start_composition: 66_666.into(),
+ end_composition: 100_000.into(),
+ start_decode: 66_666.into(),
+ sync: false,
+ };
+ let video_indice_3 = Indice {
+ start_offset: 934.into(),
+ end_offset: 955.into(),
+ start_composition: 33_333.into(),
+ end_composition: 66_666.into(),
+ start_decode: 100_000.into(),
+ sync: false,
+ };
+ assert_eq!(indice.length, 300);
+ assert_eq!(*indice.indices.offset(0), video_indice_0);
+ assert_eq!(*indice.indices.offset(1), video_indice_1);
+ assert_eq!(*indice.indices.offset(2), video_indice_2);
+ assert_eq!(*indice.indices.offset(3), video_indice_3);
+
+ mp4parse_free(parser);
+ }
+}
diff --git a/third_party/rust/mp4parse_capi/tests/test_workaround_stsc.rs b/third_party/rust/mp4parse_capi/tests/test_workaround_stsc.rs
new file mode 100644
index 0000000000..99c92a7fef
--- /dev/null
+++ b/third_party/rust/mp4parse_capi/tests/test_workaround_stsc.rs
@@ -0,0 +1,46 @@
+extern crate mp4parse_capi;
+use mp4parse_capi::*;
+use std::io::Read;
+
+extern "C" fn buf_read(buf: *mut u8, size: usize, userdata: *mut std::os::raw::c_void) -> isize {
+ let input: &mut std::fs::File = unsafe { &mut *(userdata as *mut _) };
+ let buf = unsafe { std::slice::from_raw_parts_mut(buf, size) };
+ match input.read(buf) {
+ Ok(n) => n as isize,
+ Err(_) => -1,
+ }
+}
+
+#[test]
+fn parse_invalid_stsc_table() {
+ let mut file = std::fs::File::open("tests/zero_empty_stsc.mp4").expect("Unknown file");
+ let io = Mp4parseIo {
+ read: Some(buf_read),
+ userdata: &mut file as *mut _ as *mut std::os::raw::c_void,
+ };
+
+ unsafe {
+ let mut parser = std::ptr::null_mut();
+ let rv = mp4parse_new(&io, &mut parser);
+
+ assert_eq!(rv, Mp4parseStatus::Ok);
+ assert!(!parser.is_null());
+
+ let mut counts: u32 = 0;
+ let rv = mp4parse_get_track_count(parser, &mut counts);
+ assert_eq!(rv, Mp4parseStatus::Ok);
+ assert_eq!(counts, 2);
+
+ let mut indice_video = Mp4parseByteData::default();
+ let rv = mp4parse_get_indice_table(parser, 1, &mut indice_video);
+ assert_eq!(rv, Mp4parseStatus::Ok);
+ assert_eq!(indice_video.length, 1040);
+
+ let mut indice_audio = Mp4parseByteData::default();
+ let rv = mp4parse_get_indice_table(parser, 2, &mut indice_audio);
+ assert_eq!(rv, Mp4parseStatus::Ok);
+ assert_eq!(indice_audio.length, 1952);
+
+ mp4parse_free(parser);
+ }
+}