| 1 |
use mp4parse::unstable::Indice; |
| 2 |
use mp4parse_capi::*; |
| 3 |
use std::io::Read; |
| 4 |
|
| 5 |
extern "C" fn buf_read(buf: *mut u8, size: usize, userdata: *mut std::os::raw::c_void) -> isize { |
| 6 |
let input: &mut std::fs::File = unsafe { &mut *(userdata as *mut _) }; |
| 7 |
let buf = unsafe { std::slice::from_raw_parts_mut(buf, size) }; |
| 8 |
match input.read(buf) { |
| 9 |
Ok(n) => n as isize, |
| 10 |
Err(_) => -1, |
| 11 |
} |
| 12 |
} |
| 13 |
|
| 14 |
#[test] |
| 15 |
fn parse_sample_table() { |
| 16 |
let mut file = |
| 17 |
std::fs::File::open("tests/bipbop_nonfragment_header.mp4").expect("Unknown file"); |
| 18 |
let io = Mp4parseIo { |
| 19 |
read: Some(buf_read), |
| 20 |
userdata: &mut file as *mut _ as *mut std::os::raw::c_void, |
| 21 |
}; |
| 22 |
|
| 23 |
unsafe { |
| 24 |
let mut parser = std::ptr::null_mut(); |
| 25 |
let mut rv = mp4parse_new(&io, &mut parser); |
| 26 |
assert_eq!(rv, Mp4parseStatus::Ok); |
| 27 |
assert!(!parser.is_null()); |
| 28 |
|
| 29 |
let mut counts: u32 = 0; |
| 30 |
rv = mp4parse_get_track_count(parser, &mut counts); |
| 31 |
assert_eq!(rv, Mp4parseStatus::Ok); |
| 32 |
assert_eq!(counts, 2); |
| 33 |
|
| 34 |
let mut track_info = Mp4parseTrackInfo::default(); |
| 35 |
rv = mp4parse_get_track_info(parser, 1, &mut track_info); |
| 36 |
assert_eq!(rv, Mp4parseStatus::Ok); |
| 37 |
assert_eq!(track_info.track_type, Mp4parseTrackType::Audio); |
| 38 |
|
| 39 |
// Check audio smaple table |
| 40 |
let mut is_fragmented_file: u8 = 0; |
| 41 |
rv = mp4parse_is_fragmented(parser, track_info.track_id, &mut is_fragmented_file); |
| 42 |
assert_eq!(rv, Mp4parseStatus::Ok); |
| 43 |
assert_eq!(is_fragmented_file, 0); |
| 44 |
|
| 45 |
let mut indice = Mp4parseByteData::default(); |
| 46 |
rv = mp4parse_get_indice_table(parser, track_info.track_id, &mut indice); |
| 47 |
assert_eq!(rv, Mp4parseStatus::Ok); |
| 48 |
|
| 49 |
// Compare the value from stagefright. |
| 50 |
let audio_indice_0 = Indice { |
| 51 |
start_offset: 27_046.into(), |
| 52 |
end_offset: 27_052.into(), |
| 53 |
start_composition: 0.into(), |
| 54 |
end_composition: 1024.into(), |
| 55 |
start_decode: 0.into(), |
| 56 |
sync: true, |
| 57 |
}; |
| 58 |
let audio_indice_215 = Indice { |
| 59 |
start_offset: 283_550.into(), |
| 60 |
end_offset: 283_556.into(), |
| 61 |
start_composition: 220160.into(), |
| 62 |
end_composition: 221184.into(), |
| 63 |
start_decode: 220160.into(), |
| 64 |
sync: true, |
| 65 |
}; |
| 66 |
assert_eq!(indice.length, 216); |
| 67 |
assert_eq!(*indice.indices.offset(0), audio_indice_0); |
| 68 |
assert_eq!(*indice.indices.offset(215), audio_indice_215); |
| 69 |
|
| 70 |
// Check video smaple table |
| 71 |
rv = mp4parse_get_track_info(parser, 0, &mut track_info); |
| 72 |
assert_eq!(rv, Mp4parseStatus::Ok); |
| 73 |
assert_eq!(track_info.track_type, Mp4parseTrackType::Video); |
| 74 |
|
| 75 |
let mut is_fragmented_file: u8 = 0; |
| 76 |
rv = mp4parse_is_fragmented(parser, track_info.track_id, &mut is_fragmented_file); |
| 77 |
assert_eq!(rv, Mp4parseStatus::Ok); |
| 78 |
assert_eq!(is_fragmented_file, 0); |
| 79 |
|
| 80 |
let mut indice = Mp4parseByteData::default(); |
| 81 |
rv = mp4parse_get_indice_table(parser, track_info.track_id, &mut indice); |
| 82 |
assert_eq!(rv, Mp4parseStatus::Ok); |
| 83 |
|
| 84 |
// Compare the last few data from stagefright. |
| 85 |
let video_indice_291 = Indice { |
| 86 |
start_offset: 280_226.into(), |
| 87 |
end_offset: 280_855.into(), |
| 88 |
start_composition: 876995.into(), |
| 89 |
end_composition: 879996.into(), |
| 90 |
start_decode: 873900.into(), |
| 91 |
sync: false, |
| 92 |
}; |
| 93 |
let video_indice_292 = Indice { |
| 94 |
start_offset: 280_855.into(), |
| 95 |
end_offset: 281_297.into(), |
| 96 |
start_composition: 873996.into(), |
| 97 |
end_composition: 876995.into(), |
| 98 |
start_decode: 873901.into(), |
| 99 |
sync: false, |
| 100 |
}; |
| 101 |
// TODO: start_composition time in stagefright is 9905000, but it is 9904999 in parser, it |
| 102 |
// could be rounding error. |
| 103 |
//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 }; |
| 104 |
//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 }; |
| 105 |
let video_indice_295 = Indice { |
| 106 |
start_offset: 282_391.into(), |
| 107 |
end_offset: 283_032.into(), |
| 108 |
start_composition: 888995.into(), |
| 109 |
end_composition: 888996.into(), |
| 110 |
start_decode: 885900.into(), |
| 111 |
sync: false, |
| 112 |
}; |
| 113 |
let video_indice_296 = Indice { |
| 114 |
start_offset: 283_092.into(), |
| 115 |
end_offset: 283_526.into(), |
| 116 |
start_composition: 885996.into(), |
| 117 |
end_composition: 888995.into(), |
| 118 |
start_decode: 885901.into(), |
| 119 |
sync: false, |
| 120 |
}; |
| 121 |
|
| 122 |
assert_eq!(indice.length, 297); |
| 123 |
assert_eq!(*indice.indices.offset(291), video_indice_291); |
| 124 |
assert_eq!(*indice.indices.offset(292), video_indice_292); |
| 125 |
//assert_eq!(*indice.indices.offset(293), video_indice_293); |
| 126 |
//assert_eq!(*indice.indices.offset(294), video_indice_294); |
| 127 |
assert_eq!(*indice.indices.offset(295), video_indice_295); |
| 128 |
assert_eq!(*indice.indices.offset(296), video_indice_296); |
| 129 |
|
| 130 |
mp4parse_free(parser); |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
#[test] |
| 135 |
fn parse_sample_table_with_elst() { |
| 136 |
let mut file = std::fs::File::open("tests/short-cenc.mp4").expect("Unknown file"); |
| 137 |
let io = Mp4parseIo { |
| 138 |
read: Some(buf_read), |
| 139 |
userdata: &mut file as *mut _ as *mut std::os::raw::c_void, |
| 140 |
}; |
| 141 |
|
| 142 |
unsafe { |
| 143 |
let mut parser = std::ptr::null_mut(); |
| 144 |
let mut rv = mp4parse_new(&io, &mut parser); |
| 145 |
assert_eq!(rv, Mp4parseStatus::Ok); |
| 146 |
assert!(!parser.is_null()); |
| 147 |
|
| 148 |
let mut counts: u32 = 0; |
| 149 |
rv = mp4parse_get_track_count(parser, &mut counts); |
| 150 |
assert_eq!(rv, Mp4parseStatus::Ok); |
| 151 |
assert_eq!(counts, 2); |
| 152 |
|
| 153 |
let mut track_info = Mp4parseTrackInfo::default(); |
| 154 |
rv = mp4parse_get_track_info(parser, 1, &mut track_info); |
| 155 |
assert_eq!(rv, Mp4parseStatus::Ok); |
| 156 |
assert_eq!(track_info.track_type, Mp4parseTrackType::Audio); |
| 157 |
|
| 158 |
// Check audio sample table |
| 159 |
let mut is_fragmented_file: u8 = u8::MAX; |
| 160 |
rv = mp4parse_is_fragmented(parser, track_info.track_id, &mut is_fragmented_file); |
| 161 |
assert_eq!(rv, Mp4parseStatus::Ok); |
| 162 |
assert_eq!(is_fragmented_file, 0); |
| 163 |
|
| 164 |
let mut indice = Mp4parseByteData::default(); |
| 165 |
rv = mp4parse_get_indice_table(parser, track_info.track_id, &mut indice); |
| 166 |
assert_eq!(rv, Mp4parseStatus::Ok); |
| 167 |
|
| 168 |
// Compare the value from stagefright. |
| 169 |
// Due to 'elst', the start_composition and end_composition are negative |
| 170 |
// at first two samples. |
| 171 |
let audio_indice_0 = Indice { |
| 172 |
start_offset: 6992.into(), |
| 173 |
end_offset: 7363.into(), |
| 174 |
start_composition: (-1600).into(), |
| 175 |
end_composition: (-576).into(), |
| 176 |
start_decode: 0.into(), |
| 177 |
sync: true, |
| 178 |
}; |
| 179 |
let audio_indice_1 = Indice { |
| 180 |
start_offset: 7363.into(), |
| 181 |
end_offset: 7735.into(), |
| 182 |
start_composition: (-576).into(), |
| 183 |
end_composition: 448.into(), |
| 184 |
start_decode: 1024.into(), |
| 185 |
sync: true, |
| 186 |
}; |
| 187 |
let audio_indice_2 = Indice { |
| 188 |
start_offset: 7735.into(), |
| 189 |
end_offset: 8106.into(), |
| 190 |
start_composition: 448.into(), |
| 191 |
end_composition: 1472.into(), |
| 192 |
start_decode: 2048.into(), |
| 193 |
sync: true, |
| 194 |
}; |
| 195 |
assert_eq!(indice.length, 21); |
| 196 |
assert_eq!(*indice.indices.offset(0), audio_indice_0); |
| 197 |
assert_eq!(*indice.indices.offset(1), audio_indice_1); |
| 198 |
assert_eq!(*indice.indices.offset(2), audio_indice_2); |
| 199 |
|
| 200 |
mp4parse_free(parser); |
| 201 |
} |
| 202 |
} |
| 203 |
|
| 204 |
#[test] |
| 205 |
fn parse_sample_table_with_negative_ctts() { |
| 206 |
let mut file = std::fs::File::open("tests/white.mp4").expect("Unknown file"); |
| 207 |
let io = Mp4parseIo { |
| 208 |
read: Some(buf_read), |
| 209 |
userdata: &mut file as *mut _ as *mut std::os::raw::c_void, |
| 210 |
}; |
| 211 |
|
| 212 |
unsafe { |
| 213 |
let mut parser = std::ptr::null_mut(); |
| 214 |
let mut rv = mp4parse_new(&io, &mut parser); |
| 215 |
assert_eq!(rv, Mp4parseStatus::Ok); |
| 216 |
assert!(!parser.is_null()); |
| 217 |
|
| 218 |
let mut counts: u32 = 0; |
| 219 |
rv = mp4parse_get_track_count(parser, &mut counts); |
| 220 |
assert_eq!(rv, Mp4parseStatus::Ok); |
| 221 |
assert_eq!(counts, 1); |
| 222 |
|
| 223 |
let mut track_info = Mp4parseTrackInfo::default(); |
| 224 |
rv = mp4parse_get_track_info(parser, 0, &mut track_info); |
| 225 |
assert_eq!(rv, Mp4parseStatus::Ok); |
| 226 |
assert_eq!(track_info.track_type, Mp4parseTrackType::Video); |
| 227 |
|
| 228 |
let mut is_fragmented_file: u8 = u8::MAX; |
| 229 |
rv = mp4parse_is_fragmented(parser, track_info.track_id, &mut is_fragmented_file); |
| 230 |
assert_eq!(rv, Mp4parseStatus::Ok); |
| 231 |
assert_eq!(is_fragmented_file, 0); |
| 232 |
|
| 233 |
let mut indice = Mp4parseByteData::default(); |
| 234 |
rv = mp4parse_get_indice_table(parser, track_info.track_id, &mut indice); |
| 235 |
assert_eq!(rv, Mp4parseStatus::Ok); |
| 236 |
|
| 237 |
// There are negative value in 'ctts' table. |
| 238 |
let video_indice_0 = Indice { |
| 239 |
start_offset: 48.into(), |
| 240 |
end_offset: 890.into(), |
| 241 |
start_composition: 0.into(), |
| 242 |
end_composition: 100.into(), |
| 243 |
start_decode: 0.into(), |
| 244 |
sync: true, |
| 245 |
}; |
| 246 |
let video_indice_1 = Indice { |
| 247 |
start_offset: 890.into(), |
| 248 |
end_offset: 913.into(), |
| 249 |
start_composition: 400.into(), |
| 250 |
end_composition: 500.into(), |
| 251 |
start_decode: 100.into(), |
| 252 |
sync: false, |
| 253 |
}; |
| 254 |
let video_indice_2 = Indice { |
| 255 |
start_offset: 913.into(), |
| 256 |
end_offset: 934.into(), |
| 257 |
start_composition: 200.into(), |
| 258 |
end_composition: 300.into(), |
| 259 |
start_decode: 200.into(), |
| 260 |
sync: false, |
| 261 |
}; |
| 262 |
let video_indice_3 = Indice { |
| 263 |
start_offset: 934.into(), |
| 264 |
end_offset: 955.into(), |
| 265 |
start_composition: 100.into(), |
| 266 |
end_composition: 200.into(), |
| 267 |
start_decode: 300.into(), |
| 268 |
sync: false, |
| 269 |
}; |
| 270 |
assert_eq!(indice.length, 300); |
| 271 |
assert_eq!(*indice.indices.offset(0), video_indice_0); |
| 272 |
assert_eq!(*indice.indices.offset(1), video_indice_1); |
| 273 |
assert_eq!(*indice.indices.offset(2), video_indice_2); |
| 274 |
assert_eq!(*indice.indices.offset(3), video_indice_3); |
| 275 |
|
| 276 |
mp4parse_free(parser); |
| 277 |
} |
| 278 |
} |