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
|
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: 1024.into(),
start_decode: 0.into(),
sync: true,
};
let audio_indice_215 = Indice {
start_offset: 283_550.into(),
end_offset: 283_556.into(),
start_composition: 220160.into(),
end_composition: 221184.into(),
start_decode: 220160.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: 876995.into(),
end_composition: 879996.into(),
start_decode: 873900.into(),
sync: false,
};
let video_indice_292 = Indice {
start_offset: 280_855.into(),
end_offset: 281_297.into(),
start_composition: 873996.into(),
end_composition: 876995.into(),
start_decode: 873901.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: 888995.into(),
end_composition: 888996.into(),
start_decode: 885900.into(),
sync: false,
};
let video_indice_296 = Indice {
start_offset: 283_092.into(),
end_offset: 283_526.into(),
start_composition: 885996.into(),
end_composition: 888995.into(),
start_decode: 885901.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 = 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: (-1600).into(),
end_composition: (-576).into(),
start_decode: 0.into(),
sync: true,
};
let audio_indice_1 = Indice {
start_offset: 7363.into(),
end_offset: 7735.into(),
start_composition: (-576).into(),
end_composition: 448.into(),
start_decode: 1024.into(),
sync: true,
};
let audio_indice_2 = Indice {
start_offset: 7735.into(),
end_offset: 8106.into(),
start_composition: 448.into(),
end_composition: 1472.into(),
start_decode: 2048.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 = 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: 100.into(),
start_decode: 0.into(),
sync: true,
};
let video_indice_1 = Indice {
start_offset: 890.into(),
end_offset: 913.into(),
start_composition: 400.into(),
end_composition: 500.into(),
start_decode: 100.into(),
sync: false,
};
let video_indice_2 = Indice {
start_offset: 913.into(),
end_offset: 934.into(),
start_composition: 200.into(),
end_composition: 300.into(),
start_decode: 200.into(),
sync: false,
};
let video_indice_3 = Indice {
start_offset: 934.into(),
end_offset: 955.into(),
start_composition: 100.into(),
end_composition: 200.into(),
start_decode: 300.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);
}
}
|