]> git.nihav.org Git - nihav.git/blame - nihav-commonfmt/src/demuxers/mov.rs
mpegaudio: make packetiser checks less verbose
[nihav.git] / nihav-commonfmt / src / demuxers / mov.rs
CommitLineData
58309c73
KS
1use nihav_core::demuxers::*;
2use nihav_registry::register::*;
fbf1f900 3use nihav_core::compr::deflate::*;
58309c73
KS
4
5macro_rules! mktag {
6 ($a:expr, $b:expr, $c:expr, $d:expr) => ({
7 (($a as u32) << 24) | (($b as u32) << 16) | (($c as u32) << 8) | ($d as u32)
8 });
9 ($arr:expr) => ({
10 (($arr[0] as u32) << 24) | (($arr[1] as u32) << 16) | (($arr[2] as u32) << 8) | ($arr[3] as u32)
11 });
12}
13
14trait Skip64 {
15 fn skip64(&mut self, size: u64) -> ByteIOResult<()>;
16}
17
18impl<'a> Skip64 for ByteReader<'a> {
19 fn skip64(&mut self, size: u64) -> ByteIOResult<()> {
20 if (size as usize as u64) != size {
21 self.seek(SeekFrom::Current(size as i64))?;
22 } else {
23 self.read_skip(size as usize)?;
24 }
25 Ok(())
26 }
27}
28
29fn read_chunk_header(br: &mut ByteReader) -> DemuxerResult<(u32, u64)> {
30 let size = br.read_u32be()?;
31 let ctype = br.read_u32be()?;
32 if size == 0 {
33 Ok((ctype, br.left() as u64))
34 } else if size == 1 {
35 let size64 = br.read_u64be()?;
36 validate!(size64 >= 16);
37 Ok((ctype, size64 - 16))
38 } else {
39 validate!(size >= 8);
40 Ok((ctype, (size as u64) - 8))
41 }
42}
43
58cd51fa 44fn read_palette(br: &mut ByteReader, size: u64, pal: &mut [u8; 1024]) -> DemuxerResult<u64> {
00333698 45 let _seed = br.read_u32be()?;
0ad1f05e 46 let flags = br.read_u16be()?;
00333698
KS
47 let palsize = (br.read_u16be()? as usize) + 1;
48 validate!(palsize <= 256);
0ad1f05e
KS
49 validate!(flags == 0 || flags == 0x4000 || flags == 0x8000);
50 validate!((palsize as u64) * 8 + 8 <= size);
00333698
KS
51 for i in 0..palsize {
52 let a = br.read_u16be()?;
53 let r = br.read_u16be()?;
54 let g = br.read_u16be()?;
55 let b = br.read_u16be()?;
58cd51fa
KS
56 pal[i * 4] = (r >> 8) as u8;
57 pal[i * 4 + 1] = (g >> 8) as u8;
58 pal[i * 4 + 2] = (b >> 8) as u8;
0ad1f05e
KS
59 if flags == 0x8000 {
60 pal[i * 4 + 3] = (a >> 8) as u8;
61 }
62 }
63 if flags == 0x4000 {
64 br.read_skip(8)?;
00333698
KS
65 }
66 Ok(size)
67}
68
58309c73
KS
69struct RootChunkHandler {
70 ctype: u32,
71 parse: fn(dmx: &mut MOVDemuxer, strmgr: &mut StreamManager, size: u64) -> DemuxerResult<u64>,
72}
73
74struct TrackChunkHandler {
75 ctype: u32,
76 parse: fn(track: &mut Track, br: &mut ByteReader, size: u64) -> DemuxerResult<u64>,
77}
78
79const IGNORED_CHUNKS: &[u32] = &[
80 mktag!(b"free"), mktag!(b"skip"), mktag!(b"udta"), mktag!(b"wide")
81];
82
83const ROOT_CHUNK_HANDLERS: &[RootChunkHandler] = &[
7e19285f 84 RootChunkHandler { ctype: mktag!(b"ftyp"), parse: read_ftyp },
58309c73
KS
85 RootChunkHandler { ctype: mktag!(b"mdat"), parse: read_mdat },
86 RootChunkHandler { ctype: mktag!(b"moov"), parse: read_moov },
0a0456a8
KS
87 RootChunkHandler { ctype: mktag!(b"moof"), parse: read_moof },
88 RootChunkHandler { ctype: mktag!(b"sidx"), parse: read_sidx },
58309c73
KS
89];
90
650d7bfb
KS
91fn print_cname(ctype: u32, size: u64, off: u64, depth: u8) {
92 for _ in 0..depth { print!(" "); }
93 let tag = [(ctype >> 24) as u8, (ctype >> 16) as u8, (ctype >> 8) as u8, ctype as u8];
94 let mut printable = true;
95 for &ch in tag.iter() {
96 if ch < 0x20 || ch > 0x7F {
97 printable = false;
98 break;
99 }
100 }
101 if printable {
102 print!(" '{}{}{}{}'", tag[0] as char, tag[1] as char, tag[2] as char, tag[3] as char);
103 } else {
104 print!(" {:08X}", ctype);
105 }
106 println!(" size {} @ {:X}", size, off);
107}
108
58309c73
KS
109macro_rules! read_chunk_list {
110 (root; $name: expr, $fname: ident, $handlers: ident) => {
111 fn $fname(&mut self, strmgr: &mut StreamManager, size: u64) -> DemuxerResult<()> {
112 self.depth += 1;
113 validate!(self.depth < 32);
114 let list_end = self.src.tell() + size;
115 while self.src.tell() < list_end {
116 let ret = read_chunk_header(&mut self.src);
117 if ret.is_err() { break; }
118 let (ctype, size) = ret.unwrap();
650d7bfb
KS
119 if self.print_chunks {
120 print_cname(ctype, size, self.src.tell(), self.depth as u8);
121 }
58309c73
KS
122 if self.src.tell() + size > list_end {
123 break;
124 }
125 if IGNORED_CHUNKS.contains(&ctype) {
126 self.src.skip64(size)?;
127 continue;
128 }
129 let handler = $handlers.iter().find(|x| x.ctype == ctype);
130 let read_size;
131 if let Some(ref handler) = handler {
132 read_size = (handler.parse)(self, strmgr, size)?;
133 } else {
134 println!("skipping unknown chunk {:08X} size {}", ctype, size);
135 read_size = 0;
136 }
137 validate!(read_size <= size);
138 self.src.skip64(size - read_size)?;
139 }
140 self.depth -= 1;
141 validate!(self.src.tell() == list_end);
142 Ok(())
143 }
144 };
145 (track; $name: expr, $fname: ident, $handlers: ident) => {
146 fn $fname(&mut self, br: &mut ByteReader, size: u64) -> DemuxerResult<()> {
147 self.depth += 1;
148 validate!(self.depth < 32);
149 let list_end = br.tell() + size;
150 while br.tell() < list_end {
151 let ret = read_chunk_header(br);
152 if ret.is_err() { break; }
153 let (ctype, size) = ret.unwrap();
650d7bfb
KS
154 if self.print_chunks {
155 print_cname(ctype, size, br.tell(), self.depth + 1);
156 }
58309c73
KS
157 if br.tell() + size > list_end {
158 break;
159 }
160 if IGNORED_CHUNKS.contains(&ctype) {
161 br.skip64(size)?;
162 continue;
163 }
164 let handler = $handlers.iter().find(|x| x.ctype == ctype);
165 let read_size;
166 if let Some(ref handler) = handler {
167 read_size = (handler.parse)(self, br, size)?;
168 } else {
169 read_size = 0;
170 }
171 validate!(read_size <= size);
172 br.skip64(size - read_size)?;
173 }
174 self.depth -= 1;
175 validate!(br.tell() == list_end);
176 Ok(())
177 }
178 }
179}
180
181fn skip_chunk(_track: &mut Track, br: &mut ByteReader, size: u64) -> DemuxerResult<u64> {
182 br.skip64(size)?;
183 Ok(size)
184}
185
7e19285f
KS
186fn read_ftyp(dmx: &mut MOVDemuxer, _strmgr: &mut StreamManager, size: u64) -> DemuxerResult<u64> {
187 dmx.src.skip64(size)?;
188 Ok(size)
189}
190
58309c73
KS
191fn read_mdat(dmx: &mut MOVDemuxer, _strmgr: &mut StreamManager, size: u64) -> DemuxerResult<u64> {
192 dmx.mdat_pos = dmx.src.tell();
193 dmx.mdat_size = size;
194 dmx.src.skip64(size)?;
195 Ok(size)
196}
197
0a0456a8
KS
198fn read_sidx(_dmx: &mut MOVDemuxer, _strmgr: &mut StreamManager, _size: u64) -> DemuxerResult<u64> {
199 Ok(0)
200}
201
58309c73
KS
202fn read_moov(dmx: &mut MOVDemuxer, strmgr: &mut StreamManager, size: u64) -> DemuxerResult<u64> {
203 dmx.read_moov(strmgr, size)?;
204 Ok(size)
205}
206
207const MOOV_CHUNK_HANDLERS: &[RootChunkHandler] = &[
208 RootChunkHandler { ctype: mktag!(b"mvhd"), parse: read_mvhd },
fbf1f900 209 RootChunkHandler { ctype: mktag!(b"cmov"), parse: read_cmov },
00333698 210 RootChunkHandler { ctype: mktag!(b"ctab"), parse: read_ctab },
58309c73 211 RootChunkHandler { ctype: mktag!(b"trak"), parse: read_trak },
7e19285f 212 RootChunkHandler { ctype: mktag!(b"meta"), parse: read_meta },
0a0456a8 213 RootChunkHandler { ctype: mktag!(b"mvex"), parse: read_mvex },
554caf5d 214 RootChunkHandler { ctype: mktag!(b"iods"), parse: skip_chunk_mov },
58309c73
KS
215];
216
217fn read_mvhd(dmx: &mut MOVDemuxer, _strmgr: &mut StreamManager, size: u64) -> DemuxerResult<u64> {
218 const KNOWN_MVHD_SIZE: u64 = 100;
219 let br = &mut dmx.src;
220 validate!(size >= KNOWN_MVHD_SIZE);
221 let version = br.read_byte()?;
222 validate!(version == 0);
223 let _flags = br.read_u24be()?;
224 let _ctime = br.read_u32be()?;
225 let _mtime = br.read_u32be()?;
226 let tscale = br.read_u32be()?;
227 let duration = br.read_u32be()?;
228 let _pref_rate = br.read_u32be()?;
229 let _pref_volume = br.read_u16be()?;
230 br.read_skip(10)?;
231 br.read_skip(36)?; // matrix
232 let _preview_time = br.read_u32be()?;
233 let _preview_duration = br.read_u32be()?;
234 let _poster_time = br.read_u32be()?;
235 let _sel_time = br.read_u32be()?;
236 let _sel_duration = br.read_u32be()?;
237 let _cur_time = br.read_u32be()?;
238 let _next_track_id = br.read_u32be()?;
239 dmx.duration = duration;
240 dmx.tb_den = tscale;
241
242 Ok(KNOWN_MVHD_SIZE)
243}
244
fbf1f900
KS
245fn read_cmov(dmx: &mut MOVDemuxer, strmgr: &mut StreamManager, size: u64) -> DemuxerResult<u64> {
246 let br = &mut dmx.src;
247 validate!(size > 24);
248 let dcom_size = br.read_u32be()?;
249 let dcom_tag = br.read_tag()?;
250 let compr_type = br.read_tag()?;
251 validate!(&dcom_tag == b"dcom" && dcom_size == 12);
252 if &compr_type != b"zlib" {
253 return Err(DemuxerError::NotImplemented);
254 }
255 let cmvd_size = u64::from(br.read_u32be()?);
256 let cmvd_tag = br.read_tag()?;
257 validate!(&cmvd_tag == b"cmvd" && cmvd_size > 14 && cmvd_size == size - 12);
258 let comp_size = (cmvd_size - 12) as usize;
259 let uncomp_size = br.read_u32be()? as usize;
260 validate!(uncomp_size > 8);
261 let mut sbuf = vec![0; comp_size];
262 let mut dbuf = vec![0; uncomp_size];
263 br.read_buf(sbuf.as_mut_slice())?;
61a228ff 264 let ret = Inflate::uncompress(sbuf.as_slice(), dbuf.as_mut_slice());
fbf1f900
KS
265 if ret.is_err() {
266 return Err(DemuxerError::InvalidData);
267 }
268 let len = ret.unwrap();
269 validate!(len == uncomp_size);
270 let mut mr = MemoryReader::new_read(dbuf.as_slice());
271 let mut br = ByteReader::new(&mut mr);
272 let (ctype, csize) = read_chunk_header(&mut br)?;
273 validate!(ctype == mktag!(b"moov"));
274 let mut ddmx = MOVDemuxer::new(&mut br);
650d7bfb 275 ddmx.print_chunks = dmx.print_chunks;
fbf1f900
KS
276 ddmx.read_moov(strmgr, csize)?;
277 std::mem::swap(&mut dmx.tracks, &mut ddmx.tracks);
278 dmx.duration = ddmx.duration;
279 dmx.tb_den = ddmx.tb_den;
280 std::mem::swap(&mut dmx.pal, &mut ddmx.pal);
237cc1f9 281
fbf1f900
KS
282 Ok(size)
283}
284
00333698 285fn read_ctab(dmx: &mut MOVDemuxer, _strmgr: &mut StreamManager, size: u64) -> DemuxerResult<u64> {
58cd51fa
KS
286 let mut pal = [0; 1024];
287 let size = read_palette(&mut dmx.src, size, &mut pal)?;
288 dmx.pal = Some(Arc::new(pal));
289 Ok(size)
00333698
KS
290}
291
7e19285f
KS
292fn read_meta(dmx: &mut MOVDemuxer, _strmgr: &mut StreamManager, size: u64) -> DemuxerResult<u64> {
293 dmx.src.skip64(size)?;
294 Ok(size)
295}
296
0a0456a8
KS
297fn read_mvex(_dmx: &mut MOVDemuxer, _strmgr: &mut StreamManager, _size: u64) -> DemuxerResult<u64> {
298 Ok(0)
299}
300
554caf5d
KS
301fn skip_chunk_mov(_dmx: &mut MOVDemuxer, _strmgr: &mut StreamManager, _size: u64) -> DemuxerResult<u64> {
302 Ok(0)
303}
304
58309c73
KS
305fn read_trak(dmx: &mut MOVDemuxer, strmgr: &mut StreamManager, size: u64) -> DemuxerResult<u64> {
306 let mut track = Track::new(dmx.cur_track as u32, dmx.tb_den);
650d7bfb 307 track.print_chunks = dmx.print_chunks;
58309c73
KS
308 track.read_trak(&mut dmx.src, size)?;
309 validate!(track.tkhd_found && track.stsd_found);
310 validate!(strmgr.get_stream_by_id(track.track_id).is_none());
311 dmx.cur_track += 1;
58309c73
KS
312 dmx.tracks.push(track);
313 Ok(size)
314}
315
0a0456a8
KS
316fn read_moof(dmx: &mut MOVDemuxer, strmgr: &mut StreamManager, size: u64) -> DemuxerResult<u64> {
317 dmx.moof_off = dmx.src.tell() - 8;
318 dmx.read_moof(strmgr, size)?;
319 Ok(size)
320}
321
322const MOOF_CHUNK_HANDLERS: &[RootChunkHandler] = &[
323 RootChunkHandler { ctype: mktag!(b"mfhd"), parse: read_mfhd },
324 RootChunkHandler { ctype: mktag!(b"traf"), parse: read_traf },
325 RootChunkHandler { ctype: mktag!(b"meta"), parse: read_meta },
326];
327
328fn read_mfhd(dmx: &mut MOVDemuxer, _strmgr: &mut StreamManager, size: u64) -> DemuxerResult<u64> {
329 const KNOWN_MFHD_SIZE: u64 = 8;
330 validate!(size >= KNOWN_MFHD_SIZE);
331 let version = dmx.src.read_byte()?;
332 validate!(version == 0);
333 let flags = dmx.src.read_u24be()?;
334 validate!(flags == 0);
335 let _seq_no = dmx.src.read_u32be()?;
336
337 Ok(KNOWN_MFHD_SIZE)
338}
339
340fn read_traf(dmx: &mut MOVDemuxer, _strmgr: &mut StreamManager, size: u64) -> DemuxerResult<u64> {
341 let mut buf = [0u8; 16];
342 dmx.src.peek_buf(&mut buf)?;
343 validate!(&buf[4..8] == b"tfhd");
344 let track_id = read_u32be(&buf[12..16])?;
345 let mut track = None;
346 for trk in dmx.tracks.iter_mut() {
347 if trk.track_id == track_id {
348 track = Some(trk);
349 break;
350 }
351 }
352 if let Some(track) = track {
353 track.moof_off = dmx.moof_off;
354 track.read_traf(&mut dmx.src, size)?;
355 Ok(size)
356 } else {
357 Ok(0)
358 }
359}
360
58309c73
KS
361const TRAK_CHUNK_HANDLERS: &[TrackChunkHandler] = &[
362 TrackChunkHandler { ctype: mktag!(b"clip"), parse: skip_chunk },
363 TrackChunkHandler { ctype: mktag!(b"matt"), parse: skip_chunk },
364 TrackChunkHandler { ctype: mktag!(b"edts"), parse: skip_chunk },
365 TrackChunkHandler { ctype: mktag!(b"tref"), parse: skip_chunk },
366 TrackChunkHandler { ctype: mktag!(b"load"), parse: skip_chunk },
367 TrackChunkHandler { ctype: mktag!(b"imap"), parse: skip_chunk },
368 TrackChunkHandler { ctype: mktag!(b"tkhd"), parse: read_tkhd },
369 TrackChunkHandler { ctype: mktag!(b"mdia"), parse: read_mdia },
370];
371
372fn read_tkhd(track: &mut Track, br: &mut ByteReader, size: u64) -> DemuxerResult<u64> {
373 const KNOWN_TKHD_SIZE: u64 = 84;
374 validate!(size >= KNOWN_TKHD_SIZE);
375 let version = br.read_byte()?;
376 validate!(version == 0);
377 let _flags = br.read_u24be()?;
378 let _ctime = br.read_u32be()?;
379 let _mtime = br.read_u32be()?;
380 let track_id = br.read_u32be()?;
381 br.read_skip(4)?;
a480a0de 382 let duration = br.read_u32be()?;
58309c73
KS
383 br.read_skip(8)?;
384 let _layer = br.read_u16be()?;
385 let _alt_group = br.read_u16be()?;
386 let _volume = br.read_u16be()?;
387 br.read_skip(2)?;
388 br.read_skip(36)?; // matrix
389 let width = br.read_u32be()? as usize;
390 let height = br.read_u32be()? as usize;
391 track.width = width >> 16;
392 track.height = height >> 16;
393 track.track_id = track_id;
a480a0de 394 track.duration = duration;
58309c73
KS
395
396 track.tkhd_found = true;
397 Ok(KNOWN_TKHD_SIZE)
398}
399
400fn read_mdia(track: &mut Track, br: &mut ByteReader, size: u64) -> DemuxerResult<u64> {
401 track.stream_type = StreamType::None;
402 track.read_mdia(br, size)?;
403 Ok(size)
404}
405
406const MDIA_CHUNK_HANDLERS: &[TrackChunkHandler] = &[
079fff1d 407 TrackChunkHandler { ctype: mktag!(b"mdhd"), parse: read_mdhd },
58309c73
KS
408 TrackChunkHandler { ctype: mktag!(b"hdlr"), parse: read_hdlr },
409 TrackChunkHandler { ctype: mktag!(b"minf"), parse: read_minf },
410];
411
079fff1d
KS
412fn read_mdhd(track: &mut Track, br: &mut ByteReader, size: u64) -> DemuxerResult<u64> {
413 const KNOWN_MDHD_SIZE: u64 = 24;
414 validate!(size >= KNOWN_MDHD_SIZE);
415 let version = br.read_byte()?;
416 validate!(version == 0);
417 let flags = br.read_u24be()?;
418 validate!(flags == 0);
419 let _ctime = br.read_u32be()?;
420 let _mtime = br.read_u32be()?;
421 track.tb_den = br.read_u32be()?;
422 validate!(track.tb_den != 0);
423 track.duration = br.read_u32be()?;
424 let _language = br.read_u16be()?;
425 let _quality = br.read_u16be()?;
426
427 Ok(KNOWN_MDHD_SIZE)
428}
429
58309c73
KS
430fn read_hdlr(track: &mut Track, br: &mut ByteReader, size: u64) -> DemuxerResult<u64> {
431 const KNOWN_HDLR_SIZE: u64 = 24;
432 validate!(size >= KNOWN_HDLR_SIZE);
433 let version = br.read_byte()?;
434 validate!(version == 0);
435 let flags = br.read_u24be()?;
436 validate!(flags == 0);
437 let comp_type = br.read_u32be()?;
438 let comp_subtype = br.read_u32be()?;
439 let _comp_manufacturer = br.read_u32be()?;
440 let _comp_flags = br.read_u32be()?;
441 let _comp_flags_mask = br.read_u32be()?;
442
d4809976 443 if comp_type == mktag!(b"mhlr") || comp_type == 0 {
58309c73
KS
444 if comp_subtype == mktag!(b"vide") {
445 track.stream_type = StreamType::Video;
446 } else if comp_subtype == mktag!(b"soun") {
447 track.stream_type = StreamType::Audio;
448 } else {
449 track.stream_type = StreamType::Data;
450 }
451 } else if comp_type == mktag!(b"dhlr") {
452 track.stream_type = StreamType::Data;
453 } else {
454 println!("Unknown stream type");
455 track.stream_type = StreamType::Data;
456 }
c8db9313 457
58309c73
KS
458 Ok(KNOWN_HDLR_SIZE)
459}
460
461fn read_minf(track: &mut Track, br: &mut ByteReader, size: u64) -> DemuxerResult<u64> {
462 track.read_minf(br, size)?;
463 Ok(size)
464}
465
466const MINF_CHUNK_HANDLERS: &[TrackChunkHandler] = &[
467 TrackChunkHandler { ctype: mktag!(b"hdlr"), parse: skip_chunk },
468 TrackChunkHandler { ctype: mktag!(b"dinf"), parse: skip_chunk },
469 TrackChunkHandler { ctype: mktag!(b"vmhd"), parse: read_vmhd },
470 TrackChunkHandler { ctype: mktag!(b"smhd"), parse: read_smhd },
471 TrackChunkHandler { ctype: mktag!(b"gmhd"), parse: read_gmhd },
472 TrackChunkHandler { ctype: mktag!(b"gmin"), parse: read_gmin },
473 TrackChunkHandler { ctype: mktag!(b"stbl"), parse: read_stbl },
474];
475
476fn read_vmhd(track: &mut Track, br: &mut ByteReader, size: u64) -> DemuxerResult<u64> {
477 const KNOWN_VMHD_SIZE: u64 = 12;
478 validate!(track.stream_type == StreamType::Video);
479 validate!(size >= KNOWN_VMHD_SIZE);
480 let version = br.read_byte()?;
481 validate!(version == 0);
482 let _flags = br.read_u24be()?;
483 br.read_skip(2)?; // graphics mode
484 br.read_skip(6)?; // opcolor
485 Ok(KNOWN_VMHD_SIZE)
486}
487
488fn read_smhd(track: &mut Track, br: &mut ByteReader, size: u64) -> DemuxerResult<u64> {
489 const KNOWN_SMHD_SIZE: u64 = 8;
490 validate!(track.stream_type == StreamType::Audio);
491 validate!(size >= KNOWN_SMHD_SIZE);
492 let version = br.read_byte()?;
493 validate!(version == 0);
494 let _flags = br.read_u24be()?;
495 br.read_skip(2)?; // balance
496 br.read_skip(2)?;
497 Ok(KNOWN_SMHD_SIZE)
498}
499
500fn read_gmhd(track: &mut Track, _br: &mut ByteReader, _size: u64) -> DemuxerResult<u64> {
501 validate!(track.stream_type == StreamType::Data);
502 Ok(0)
503}
504
505fn read_gmin(track: &mut Track, br: &mut ByteReader, size: u64) -> DemuxerResult<u64> {
506 validate!(track.stream_type == StreamType::Data);
507 const KNOWN_GMIN_SIZE: u64 = 16;
508 validate!(size >= KNOWN_GMIN_SIZE);
509 let version = br.read_byte()?;
510 validate!(version == 0);
511 let _flags = br.read_u24be()?;
512 br.read_skip(2)?; // graphics mode
513 br.read_skip(6)?; // opcolor
514 br.read_skip(2)?; // balance
515 br.read_skip(2)?;
516 Ok(KNOWN_GMIN_SIZE)
517}
518
519fn read_stbl(track: &mut Track, br: &mut ByteReader, size: u64) -> DemuxerResult<u64> {
520 track.read_stbl(br, size)?;
521 Ok(size)
522}
523
524const STBL_CHUNK_HANDLERS: &[TrackChunkHandler] = &[
525 TrackChunkHandler { ctype: mktag!(b"stsd"), parse: read_stsd },
bbbf86dd 526 TrackChunkHandler { ctype: mktag!(b"stts"), parse: read_stts },
58309c73
KS
527 TrackChunkHandler { ctype: mktag!(b"stss"), parse: read_stss },
528 TrackChunkHandler { ctype: mktag!(b"stsc"), parse: read_stsc },
529 TrackChunkHandler { ctype: mktag!(b"stsz"), parse: read_stsz },
530 TrackChunkHandler { ctype: mktag!(b"stco"), parse: read_stco },
531 TrackChunkHandler { ctype: mktag!(b"stsh"), parse: skip_chunk },
4e7deeda 532 TrackChunkHandler { ctype: mktag!(b"ctts"), parse: read_ctts },
58309c73
KS
533];
534
89b16ac8
KS
535fn parse_audio_edata(br: &mut ByteReader, start_pos: u64, size: u64) -> DemuxerResult<Option<Vec<u8>>> {
536 let read_part = br.tell() - start_pos;
537 if read_part + 8 < size {
9aa783a0
KS
538 let mut buf = [0; 8];
539 br.peek_buf(&mut buf)?;
540 if &buf[4..8] != b"wave" {
541 let mut buf = vec![0; (size - read_part) as usize];
542 br.read_buf(&mut buf)?;
543 return Ok(Some(buf));
544 }
545
89b16ac8
KS
546 let csize = br.read_u32be()? as u64;
547 let ctag = br.read_u32be()?;
548 validate!(read_part + csize <= size);
549 validate!(ctag == mktag!(b"wave"));
550 if csize == 8 {
551 return Ok(None);
552 }
553 let mut buf = [0; 8];
554 br.peek_buf(&mut buf)?;
555 if &buf[4..8] == b"frma" {
556 br.read_skip(12)?;
557 if csize > 20 {
558 let mut buf = vec![0; (csize - 20) as usize];
559 br.read_buf(&mut buf)?;
560 Ok(Some(buf))
561 } else {
562 Ok(None)
563 }
564 } else if csize > 8 {
565 let mut buf = vec![0; (csize as usize) - 8];
566 br.read_buf(&mut buf)?;
567 Ok(Some(buf))
568 } else {
569 Ok(None)
570 }
571 } else {
572 Ok(None)
573 }
574}
575
58309c73
KS
576fn read_stsd(track: &mut Track, br: &mut ByteReader, size: u64) -> DemuxerResult<u64> {
577 const KNOWN_STSD_SIZE: u64 = 24;
578 validate!(size >= KNOWN_STSD_SIZE);
579 let start_pos = br.tell();
580 let version = br.read_byte()?;
581 validate!(version == 0);
582 let _flags = br.read_u24be()?;
583 let entries = br.read_u32be()?;
584 validate!(entries > 0);
61cab15b 585 let esize = u64::from(br.read_u32be()?);
58309c73
KS
586 validate!(esize + 8 <= size);
587 let mut fcc = [0u8; 4];
588 br.read_buf(&mut fcc)?;
589 br.read_skip(6)?;
590 let _data_ref = br.read_u16be()?;
591
592 track.fcc = fcc;
593
594 let codec_info;
595 match track.stream_type {
596 StreamType::Video => {
597 let _ver = br.read_u16be()?;
598 let _revision = br.read_u16le()?;
599 let _vendor = br.read_u32be()?;
600 let _temp_quality = br.read_u32be()?;
601 let _spat_quality = br.read_u32be()?;
602 let width = br.read_u16be()? as usize;
603 let height = br.read_u16be()? as usize;
604 let _hor_res = br.read_u32be()?;
605 let _vert_res = br.read_u32be()?;
606 let data_size = br.read_u32be()?;
607 validate!(data_size == 0);
608 let _frame_count = br.read_u16be()? as usize;
609 let _cname_len = br.read_byte()? as usize;
610 br.read_skip(31)?; // actual compressor name
611 let depth = br.read_u16be()?;
612 let ctable_id = br.read_u16be()?;
8019c866
KS
613 let grayscale = depth > 0x20 || depth == 1;
614 let depth = if grayscale { depth & 0x1F } else { depth };
615 validate!(depth <= 8 || (ctable_id == 0xFFFF));
58309c73 616 if ctable_id == 0 {
00333698 617 let max_pal_size = start_pos + size - br.tell();
58cd51fa
KS
618 let mut pal = [0; 1024];
619 read_palette(br, max_pal_size, &mut pal)?;
620 track.pal = Some(Arc::new(pal));
8019c866 621 } else if (depth <= 8) && !grayscale {
2736c15a
KS
622 match depth & 0x1F {
623 2 => {
624 let mut pal = [0; 1024];
625 (&mut pal[..4 * 4]).copy_from_slice(&MOV_DEFAULT_PAL_2BIT);
626 track.pal = Some(Arc::new(pal));
627 },
628 4 => {
629 let mut pal = [0; 1024];
630 (&mut pal[..16 * 4]).copy_from_slice(&MOV_DEFAULT_PAL_4BIT);
631 track.pal = Some(Arc::new(pal));
632 },
633 8 => {
634 track.pal = Some(Arc::new(MOV_DEFAULT_PAL_8BIT));
635 },
636 _ => {},
637 };
2949bcfa 638 } else if grayscale && ctable_id != 0xFFFF {
2736c15a
KS
639 let mut pal = [0; 1024];
640 let cdepth = depth & 0x1F;
641 let size = 1 << cdepth;
642 for i in 0..size {
8019c866 643 let mut clr = ((size - 1 - i) as u8) << (8 - cdepth);
2736c15a
KS
644 let mut off = 8 - cdepth;
645 while off >= cdepth {
646 clr |= clr >> (8 - off);
647 off -= cdepth;
648 }
649 if off > 0 {
650 clr |= clr >> (8 - off);
651 }
652 pal[i * 4] = clr;
653 pal[i * 4 + 1] = clr;
654 pal[i * 4 + 2] = clr;
655 }
656 track.pal = Some(Arc::new(pal));
58309c73
KS
657 }
658// todo other atoms, put as extradata
659 let cname = if let Some(name) = find_codec_from_mov_video_fourcc(&fcc) {
660 name
661 } else if let Some(name) = find_codec_from_avi_fourcc(&fcc) {
662 name
663 } else {
664 "unknown"
665 };
666 let format = if depth > 8 { RGB24_FORMAT } else { PAL8_FORMAT };
8019c866
KS
667 let mut vhdr = NAVideoInfo::new(width, height, false, format);
668 vhdr.bits = depth as u8;
b9862e98
KS
669 //skip various common atoms
670 while br.tell() - start_pos + 4 < size {
671 let mut buf = [0u8; 8];
672 br.peek_buf(&mut buf)?;
673 let tsize = read_u32be(&buf).unwrap() as usize;
674 let tag = &buf[4..8];
675 validate!(tsize >= 8);
676 match tag {
677 b"pasp" | b"clap" => {
678 br.read_skip(tsize)?;
679 },
680 _ => break,
681 };
682 }
61cab15b 683 let edata = if br.tell() - start_pos + 4 < size {
61cab15b 684 let edata_size = br.read_u32be()? as usize;
d341f57a
KS
685 validate!(edata_size >= 4);
686 let mut buf = vec![0; edata_size - 4];
58309c73 687 br.read_buf(buf.as_mut_slice())?;
61cab15b
KS
688 Some(buf)
689 } else {
690 None
691 };
58309c73
KS
692 codec_info = NACodecInfo::new(cname, NACodecTypeInfo::Video(vhdr), edata);
693 },
694 StreamType::Audio => {
89b16ac8 695 let sver = br.read_u16be()?;
58309c73
KS
696 let _revision = br.read_u16le()?;
697 let _vendor = br.read_u32be()?;
9559c2c8
KS
698 let mut nchannels = br.read_u16be()?;
699 if sver != 2 {
700 validate!(nchannels <= 64);
701 }
58309c73
KS
702 let sample_size = br.read_u16be()?;
703 validate!(sample_size <= 128);
704 let _compr_id = br.read_u16be()?;
705 let packet_size = br.read_u16be()? as usize;
706 validate!(packet_size == 0);
9559c2c8
KS
707 let mut sample_rate = br.read_u32be()? >> 16;
708 if sver != 2 {
709 validate!(sample_rate > 0);
710 }
58309c73
KS
711 let cname = if let Some(name) = find_codec_from_mov_audio_fourcc(&fcc) {
712 name
713 } else if let (true, Some(name)) = ((fcc[0] == b'm' && fcc[1] == b's'), find_codec_from_wav_twocc(u16::from(fcc[2]) * 256 + u16::from(fcc[3]))) {
714 name
715 } else {
716 "unknown"
717 };
e5d3fef5
KS
718 let mut soniton = NASoniton::new(sample_size as u8, SONITON_FLAG_SIGNED | SONITON_FLAG_BE);
719 if &fcc == b"raw " && sample_size == 8 {
720 soniton.signed = false;
721 }
58309c73 722 let block_align = 1;
9559c2c8
KS
723 match sver {
724 1 => {
725 let samples_per_packet = br.read_u32be()?;
726 let _bytes_per_packet = br.read_u32be()?;
727 let bytes_per_frame = br.read_u32be()?;
728 let _bytes_per_sample = br.read_u32be()?;
729 track.bsize = bytes_per_frame as usize;
730 track.frame_samples = samples_per_packet as usize;
731 track.tb_num = samples_per_packet;
732 },
733 2 => {
734 br.read_u32be()?; // some size
735 let srate = br.read_f64be()?;
736 validate!(srate > 1.0);
737 sample_rate = srate as u32;
738 let channels = br.read_u32be()?;
739 validate!(channels > 0 && channels < 255);
740 nchannels = channels as u16;
741 br.read_u32be()?; // always 0x7F000000
742 let _bits_per_csample = br.read_u32be()?;
743 let _codec_flags = br.read_u32be()?;
744 let bytes_per_frame = br.read_u32be()?;
745 let samples_per_packet = br.read_u32be()?;
746 track.bsize = bytes_per_frame as usize;
747 track.frame_samples = samples_per_packet as usize;
748 track.tb_num = samples_per_packet;
749 },
750 _ => {
751 track.bsize = (sample_size / 8) as usize;
752 },
753 };
754 track.tb_den = sample_rate;
7afec34d
KS
755 track.raw_audio = match &fcc {
756 b"NONE" | b"raw " | b"twos" | b"sowt" |
757 b"in24" | b"in32" | b"fl32" | b"fl64" |
758 b"ima4" | b"ms\x00\x02" | b"ms\x00\x21" |
759 b"alaw" | b"ulaw" |
760 b"MAC3" | b"MAC6" => true,
761 _ => false,
762 };
9559c2c8 763 let ahdr = NAAudioInfo::new(sample_rate, nchannels as u8, soniton, block_align);
89b16ac8 764 let edata = parse_audio_edata(br, start_pos, size)?;
58309c73
KS
765 codec_info = NACodecInfo::new(cname, NACodecTypeInfo::Audio(ahdr), edata);
766 track.channels = nchannels as usize;
767 track.bits = sample_size as usize;
768 },
769 StreamType::None => {
770 return Err(DemuxerError::InvalidData);
771 },
772 _ => {
773//todo put it all into extradata
774 let edata = None;
775 codec_info = NACodecInfo::new("unknown", NACodecTypeInfo::None, edata);
776 },
777 };
778 let read_size = br.tell() - start_pos;
779 validate!(read_size <= size);
7afec34d 780 track.stream = Some(NAStream::new(track.stream_type, track.track_no, codec_info, track.tb_num, track.tb_den, u64::from(track.duration)));
58309c73
KS
781 track.stsd_found = true;
782 Ok(read_size)
783}
784
bbbf86dd 785fn read_stts(track: &mut Track, br: &mut ByteReader, size: u64) -> DemuxerResult<u64> {
d4809976 786 validate!(size >= 8);
bbbf86dd
KS
787 let start_pos = br.tell();
788 let version = br.read_byte()?;
789 validate!(version == 0);
790 let _flags = br.read_u24be()?;
791 let entries = br.read_u32be()? as usize;
792 validate!(entries as u64 <= (size - 8) / 8);
d4809976
KS
793 if entries == 0 {
794 } else if entries == 1 {
bbbf86dd
KS
795 let _count = br.read_u32be()?;
796 let tb_num = br.read_u32be()?;
4e7deeda 797 validate!(tb_num != 0);
0a0456a8 798 track.rescale(tb_num);
bbbf86dd 799 } else {
37952415 800 track.time_to_sample.clear();
bbbf86dd
KS
801 track.time_to_sample.reserve(entries);
802 for _ in 0..entries {
803 let count = br.read_u32be()?;
804 let mult = br.read_u32be()?;
805 track.time_to_sample.push((count, mult));
806 }
807 }
808 let read_size = br.tell() - start_pos;
809 validate!(read_size <= size);
810 Ok(read_size)
811}
812
58309c73
KS
813fn read_stss(track: &mut Track, br: &mut ByteReader, size: u64) -> DemuxerResult<u64> {
814 let version = br.read_byte()?;
815 validate!(version == 0);
816 let _flags = br.read_u24be()?;
817 let entries = br.read_u32be()? as usize;
818 validate!(entries < ((std::u32::MAX >> 2) - 8) as usize);
819 validate!((entries * 4 + 8) as u64 == size);
820 track.keyframes = Vec::with_capacity(entries);
821 let mut last_sample_no = 0;
822 for _ in 0..entries {
823 let sample_no = br.read_u32be()?;
824 validate!(sample_no > last_sample_no);
825 track.keyframes.push(sample_no);
826 last_sample_no = sample_no;
827 }
828 Ok(size)
829}
830
831fn read_stsc(track: &mut Track, br: &mut ByteReader, size: u64) -> DemuxerResult<u64> {
832 let version = br.read_byte()?;
833 validate!(version == 0);
834 let _flags = br.read_u24be()?;
835 let entries = br.read_u32be()? as usize;
836 validate!(entries < ((std::u32::MAX / 12) - 8) as usize);
837 validate!((entries * 12 + 8) as u64 == size);
838 track.sample_map = Vec::with_capacity(entries);
839 let mut last_sample_no = 0;
840 for _i in 0..entries {
841 let sample_no = br.read_u32be()?;
842 validate!(sample_no > last_sample_no);
843 let nsamples = br.read_u32be()?;
844 let _sample_desc = br.read_u32be()?;
845 track.sample_map.push((sample_no, nsamples));
846 last_sample_no = sample_no;
847 }
848 Ok(size)
849}
850
851fn read_stsz(track: &mut Track, br: &mut ByteReader, size: u64) -> DemuxerResult<u64> {
852 let version = br.read_byte()?;
853 validate!(version == 0);
854 let _flags = br.read_u24be()?;
855 let sample_size = br.read_u32be()?;
856 if sample_size != 0 {
857 track.sample_size = sample_size;
9efec4ed
KS
858 if track.sample_size != 1 || track.bsize == 0 {
859 track.bsize = sample_size as usize;
860 }
58309c73
KS
861 Ok(8)
862 } else {
863 let entries = br.read_u32be()? as usize;
864 validate!((entries * 4 + 12) as u64 == size);
865 track.chunk_sizes = Vec::with_capacity(entries);
866 for _ in 0..entries {
867 let sample_size = br.read_u32be()?;
868 track.chunk_sizes.push(sample_size);
869 }
870 Ok(size)
871 }
872}
873
874fn read_stco(track: &mut Track, br: &mut ByteReader, size: u64) -> DemuxerResult<u64> {
875 let version = br.read_byte()?;
876 validate!(version == 0);
877 let _flags = br.read_u24be()?;
878 let entries = br.read_u32be()? as usize;
879 validate!((entries * 4 + 8) as u64 == size);
880 track.chunk_offsets = Vec::with_capacity(entries);
881 for _i in 0..entries {
882 let sample_offset = br.read_u32be()?;
883 track.chunk_offsets.push(u64::from(sample_offset));
884 }
885 Ok(size)
886}
887
4e7deeda
KS
888fn read_ctts(track: &mut Track, br: &mut ByteReader, size: u64) -> DemuxerResult<u64> {
889 validate!(size >= 8);
890 let version = br.read_byte()?;
891 let _flags = br.read_u24be()?;
892 if version > 1 {
893 return Err(DemuxerError::NotImplemented);
894 }
895 let entries = br.read_u32be()? as usize;
896 track.ctts_version = version;
897 track.ctts_map.resize(entries);
898 match version {
899 0 | 1 => {
900 validate!(size == (entries as u64) * 8 + 8);
901 for _ in 0..entries {
902 let samp_count = br.read_u32be()?;
903 let samp_offset = br.read_u32be()?;
904 track.ctts_map.add(samp_count, samp_offset / track.tb_div);
905 }
906 },
907 _ => unreachable!(),
908 };
909 track.ctts_map.reset();
910
911 Ok(size)
912}
913
0a0456a8
KS
914const TRAF_CHUNK_HANDLERS: &[TrackChunkHandler] = &[
915 TrackChunkHandler { ctype: mktag!(b"tfhd"), parse: read_tfhd },
916 TrackChunkHandler { ctype: mktag!(b"trun"), parse: read_trun },
917 TrackChunkHandler { ctype: mktag!(b"sbgp"), parse: skip_chunk },
918 TrackChunkHandler { ctype: mktag!(b"sgpd"), parse: skip_chunk },
919 TrackChunkHandler { ctype: mktag!(b"subs"), parse: skip_chunk },
920 TrackChunkHandler { ctype: mktag!(b"saiz"), parse: skip_chunk },
921 TrackChunkHandler { ctype: mktag!(b"saio"), parse: skip_chunk },
922 TrackChunkHandler { ctype: mktag!(b"tfdt"), parse: skip_chunk },
923 TrackChunkHandler { ctype: mktag!(b"meta"), parse: skip_chunk },
924];
925
926fn read_tfhd(track: &mut Track, br: &mut ByteReader, size: u64) -> DemuxerResult<u64> {
927 validate!(size >= 8);
928 let start = br.tell();
929 let _version = br.read_byte()?;
930 let flags = br.read_u24be()?;
931 let _track_id = br.read_u32be()?;
932 if (flags & 0x000001) != 0 {
933 let base_offset = br.read_u64be()?;
934 track.moof_off = base_offset;
935 }
936 if (flags & 0x000002) != 0 {
937 let _sample_description_index = br.read_u32be()?;
938 }
939 if (flags & 0x000008) != 0 {
940 let default_sample_duration = br.read_u32be()?;
941 if track.tb_div == 1 {
942 track.rescale(default_sample_duration);
943 }
944 }
945 if (flags & 0x000010) != 0 {
946 let _default_sample_size = br.read_u32be()?;
947 }
948 if (flags & 0x000020) != 0 {
949 let _default_sample_flags = br.read_u32be()?;
950 }
951 if (flags & 0x010000) != 0 {
952 }
953 /*if (flags & 0x020000) != 0 { // base offset is moof start
954 }*/
955 Ok(br.tell() - start)
956}
957
958fn read_trun(track: &mut Track, br: &mut ByteReader, size: u64) -> DemuxerResult<u64> {
959 validate!(size >= 8);
960 let version = br.read_byte()?;
961 let flags = br.read_u24be()?;
962 let data_off_present = (flags & 0x000001) != 0;
963 let first_sample_flags = (flags & 0x000004) != 0;
964 let sample_duration_present = (flags & 0x000100) != 0;
965 let sample_size_present = (flags & 0x000200) != 0;
966 let sample_flags_present = (flags & 0x000400) != 0;
967 let sample_ct_off_present = (flags & 0x000800) != 0;
968
969 let sample_count = br.read_u32be()? as usize;
970
971 let mut hdr_size = 8;
972 let mut arr_size = 0;
973 if data_off_present {
974 hdr_size += 4;
975 }
976 if first_sample_flags {
977 hdr_size += 4;
978 }
979 if sample_duration_present {
980 arr_size += 4;
981 }
982 if sample_size_present {
983 arr_size += 4;
984 }
985 if sample_flags_present {
986 arr_size += 4;
987 }
988 if sample_ct_off_present {
989 arr_size += 4;
990 }
991 validate!(size == hdr_size + arr_size * (sample_count as u64));
992
993 let mut data_off = if data_off_present {
994 let off = br.read_u32be()? as i32;
995 let new_off = (track.moof_off as i64) + i64::from(off);
996 validate!(new_off > 0);
997 new_off as u64
998 } else {
999 track.moof_off
1000 };
1001 if first_sample_flags {
1002 let _flags = br.read_u32be()?;
1003 }
1004
1005 if sample_size_present {
1006 track.chunk_sizes.reserve(sample_count);
1007 track.chunk_offsets.reserve(sample_count);
1008 }
1009
1010 if sample_ct_off_present {
1011 if track.ctts_version != version {
1012 track.ctts_version = version;
1013 }
1014 track.ctts_map.reserve(sample_count);
1015 }
1016
6bb26927
KS
1017 if track.chunk_offsets.len() < (std::u32::MAX as usize) {
1018 track.keyframes.push((track.chunk_offsets.len() + 1) as u32);
1019 }
0a0456a8
KS
1020 for _ in 0..sample_count {
1021 if sample_duration_present {
1022 let _duration = br.read_u32be()?;
1023 }
1024 if sample_size_present {
1025 let ssize = br.read_u32be()?;
1026 track.chunk_sizes.push(ssize);
1027 track.chunk_offsets.push(data_off);
1028 data_off += u64::from(ssize);
1029 }
1030 if sample_flags_present {
1031 let _flags = br.read_u32be()?;
1032 }
1033 if sample_ct_off_present {
1034 let samp_offset = br.read_u32be()?;
1035 if version == 0 {
1036 track.ctts_map.add(1, samp_offset / track.tb_div);
1037 } else {
1038 track.ctts_map.add(1, ((samp_offset as i32) / (track.tb_div as i32)) as u32);
1039 }
1040 }
1041 }
1042
1043 Ok(size)
1044}
1045
58309c73
KS
1046struct MOVDemuxer<'a> {
1047 src: &'a mut ByteReader<'a>,
1048 depth: usize,
1049 mdat_pos: u64,
1050 mdat_size: u64,
1051 tracks: Vec<Track>,
1052 cur_track: usize,
1053 tb_den: u32,
1054 duration: u32,
58cd51fa 1055 pal: Option<Arc<[u8; 1024]>>,
650d7bfb 1056
0a0456a8
KS
1057 moof_off: u64,
1058
650d7bfb 1059 print_chunks: bool,
58309c73
KS
1060}
1061
1062struct Track {
1063 track_id: u32,
1064 track_str_id: usize,
1065 track_no: u32,
bbbf86dd 1066 tb_num: u32,
58309c73 1067 tb_den: u32,
4e7deeda 1068 tb_div: u32,
7afec34d
KS
1069 raw_audio: bool,
1070 raw_apos: u64,
a480a0de 1071 duration: u32,
58309c73
KS
1072 depth: u8,
1073 tkhd_found: bool,
1074 stsd_found: bool,
1075 stream_type: StreamType,
1076 width: usize,
1077 height: usize,
1078 channels: usize,
1079 bits: usize,
9efec4ed 1080 bsize: usize,
58309c73
KS
1081 fcc: [u8; 4],
1082 keyframes: Vec<u32>,
1083 chunk_sizes: Vec<u32>,
1084 chunk_offsets: Vec<u64>,
bbbf86dd 1085 time_to_sample: Vec<(u32, u32)>,
58309c73
KS
1086 sample_map: Vec<(u32, u32)>,
1087 sample_size: u32,
b0e12d42 1088 frame_samples: usize,
4e7deeda
KS
1089 ctts_map: RLESearcher<u32>,
1090 ctts_version: u8,
58309c73
KS
1091 stream: Option<NAStream>,
1092 cur_chunk: usize,
1093 cur_sample: usize,
07972bf3 1094 cur_ts: Option<u64>,
58309c73
KS
1095 samples_left: usize,
1096 last_offset: u64,
58cd51fa 1097 pal: Option<Arc<[u8; 1024]>>,
bbbf86dd 1098 timesearch: TimeSearcher,
650d7bfb 1099
0a0456a8
KS
1100 moof_off: u64,
1101
650d7bfb 1102 print_chunks: bool,
bbbf86dd
KS
1103}
1104
1105#[derive(Default)]
1106struct TimeSearcher {
1107 idx: usize,
1108 base: u64,
1109 sbase: u32,
1110 cur_len: u32,
1111 cur_mul: u32,
1112}
1113
1114impl TimeSearcher {
1115 fn new() -> Self { Self::default() }
1116 fn reset(&mut self) {
1117 *self = Self::default();
1118 }
b7c882c1 1119 fn map_time(&mut self, sample: u32, tts: &[(u32, u32)]) -> u64 {
bbbf86dd
KS
1120 if tts.is_empty() {
1121 u64::from(sample)
1122 } else if sample >= self.sbase {
1123 let mut sample = sample - self.sbase;
1124 if self.idx == 0 {
1125 let (cur_len, cur_mul) = tts[0];
1126 self.cur_len = cur_len;
1127 self.cur_mul = cur_mul;
1128 self.idx += 1;
1129 }
1130 while self.idx < tts.len() && sample > self.cur_len {
1131 sample -= self.cur_len;
1132 self.sbase += self.cur_len;
1133 self.base += u64::from(self.cur_len) * u64::from(self.cur_mul);
1134 self.cur_len = tts[self.idx].0;
1135 self.cur_mul = tts[self.idx].1;
1136 self.idx += 1;
1137 }
1138 self.base + u64::from(sample) * u64::from(self.cur_mul)
1139 } else {
1140 self.reset();
1141 self.map_time(sample, tts)
1142 }
1143 }
58309c73
KS
1144}
1145
4e7deeda
KS
1146#[derive(Default)]
1147struct RLESearcher<T> {
1148 array: Vec<(u32, T)>,
1149 idx: usize,
1150 start: u64,
1151 next: u64,
1152}
1153
1154impl<T:Default+Copy> RLESearcher<T> {
1155 fn new() -> Self { Self::default() }
1156 fn resize(&mut self, size: usize) {
37952415 1157 self.array.clear();
4e7deeda
KS
1158 self.array.reserve(size);
1159 }
0a0456a8
KS
1160 fn reserve(&mut self, size: usize) {
1161 self.array.reserve(size);
1162 }
4e7deeda
KS
1163 fn add(&mut self, len: u32, val: T) {
1164 self.array.push((len, val));
1165 }
1166 fn reset(&mut self) {
1167 self.start = 0;
1168 if !self.array.is_empty() {
1169 self.idx = 0;
1170 self.next = u64::from(self.array[0].0);
1171 } else {
1172 self.idx = self.array.len();
1173 self.next = 0;
1174 }
1175 }
1176 fn map(&mut self, sample: u64) -> Option<T> {
1177 if sample < self.start {
1178 self.reset();
1179 }
1180 if self.idx < self.array.len() {
1181 if sample < self.next {
1182 Some(self.array[self.idx].1)
1183 } else {
1184 while (self.idx < self.array.len()) && (sample >= self.next) {
1185 self.start = self.next;
1186 self.idx += 1;
1187 if self.idx < self.array.len() {
1188 self.next += u64::from(self.array[self.idx].0);
1189 }
1190 }
1191 if self.idx < self.array.len() {
1192 Some(self.array[self.idx].1)
1193 } else {
1194 None
1195 }
1196 }
1197 } else {
1198 None
1199 }
1200 }
1201}
1202
58309c73
KS
1203impl Track {
1204 fn new(track_no: u32, tb_den: u32) -> Self {
1205 Self {
1206 tkhd_found: false,
1207 stsd_found: false,
1208 track_id: 0,
1209 track_str_id: 0,
1210 track_no,
bbbf86dd 1211 tb_num: 1,
58309c73 1212 tb_den,
4e7deeda 1213 tb_div: 1,
7afec34d
KS
1214 raw_audio: false,
1215 raw_apos: 0,
a480a0de 1216 duration: 0,
58309c73
KS
1217 stream_type: StreamType::None,
1218 width: 0,
1219 height: 0,
1220 channels: 0,
1221 bits: 0,
9efec4ed 1222 bsize: 0,
58309c73
KS
1223 fcc: [0; 4],
1224 keyframes: Vec::new(),
1225 chunk_sizes: Vec::new(),
1226 chunk_offsets: Vec::new(),
bbbf86dd 1227 time_to_sample: Vec::new(),
58309c73
KS
1228 sample_map: Vec::new(),
1229 sample_size: 0,
b0e12d42 1230 frame_samples: 0,
4e7deeda
KS
1231 ctts_map: RLESearcher::new(),
1232 ctts_version: 0,
58309c73
KS
1233 stream: None,
1234 depth: 0,
1235 cur_chunk: 0,
1236 cur_sample: 0,
07972bf3 1237 cur_ts: None,
58309c73
KS
1238 samples_left: 0,
1239 last_offset: 0,
58cd51fa 1240 pal: None,
bbbf86dd 1241 timesearch: TimeSearcher::new(),
650d7bfb 1242
0a0456a8
KS
1243 moof_off: 0,
1244
650d7bfb 1245 print_chunks: false,
58309c73
KS
1246 }
1247 }
1248 read_chunk_list!(track; "trak", read_trak, TRAK_CHUNK_HANDLERS);
1249 read_chunk_list!(track; "mdia", read_mdia, MDIA_CHUNK_HANDLERS);
1250 read_chunk_list!(track; "minf", read_minf, MINF_CHUNK_HANDLERS);
1251 read_chunk_list!(track; "stbl", read_stbl, STBL_CHUNK_HANDLERS);
0a0456a8
KS
1252 read_chunk_list!(track; "traf", read_traf, TRAF_CHUNK_HANDLERS);
1253 fn rescale(&mut self, tb_num: u32) {
1254 self.tb_div = tb_num;
1255 if let Some(ref mut stream) = self.stream {
1256 let tb_den = stream.tb_den;
1257 let (tb_num, tb_den) = reduce_timebase(tb_num * stream.tb_num, tb_den);
1258 stream.duration /= u64::from(self.tb_div);
1259 stream.tb_num = tb_num;
1260 stream.tb_den = tb_den;
1261 self.tb_num = tb_num;
1262 self.tb_den = tb_den;
1263 self.duration /= self.tb_div;
1264 }
1265 }
58309c73 1266 fn fill_seek_index(&self, seek_index: &mut SeekIndex) {
61cab15b 1267 if !self.keyframes.is_empty() {
58309c73
KS
1268 seek_index.mode = SeekIndexMode::Present;
1269 }
bbbf86dd 1270 let mut tsearch = TimeSearcher::new();
58309c73 1271 for kf_time in self.keyframes.iter() {
bbbf86dd
KS
1272 let pts = tsearch.map_time(*kf_time - 1, &self.time_to_sample);
1273 let time = NATimeInfo::ts_to_time(pts, 1000, self.tb_num, self.tb_den);
1274 seek_index.add_entry(self.track_no as u32, SeekEntry { time, pts: u64::from(*kf_time - 1), pos: 0 });
58309c73
KS
1275 }
1276 }
1277 fn calculate_chunk_size(&self, nsamp: usize) -> usize {
1278 if nsamp == 0 {
9efec4ed 1279 self.bsize
58309c73
KS
1280 } else {
1281 match &self.fcc {
1282 b"NONE" | b"raw " | b"twos" | b"sowt" => {
1283 (nsamp * self.bits * self.channels + 7) >> 3
1284 },
1285 b"ima4" => {
1286 let nblocks = (nsamp + 63) >> 6;
1287 nblocks * 34 * self.channels
1288 },
1289 b"MAC3" => {
1290 (nsamp + 5) / 6 * 2 * self.channels
1291 },
1292 b"MAC6" => {
1293 (nsamp + 5) / 6 * self.channels
1294 },
1295 b"in24" => nsamp * 3 * self.channels,
1296 b"in32" | b"fl32" => nsamp * 4 * self.channels,
1297 b"fl64" => nsamp * 8 * self.channels,
1298 b"ulaw" | b"alaw" => nsamp,
1299 b"ms\x00\x02" => { //MS ADPCM
1300 ((nsamp - 1) / 2 + 7) * self.channels
1301 },
1302 b"ms\x00\x21" => { //IMA ADPCM
1303 (nsamp / 2 + 4) * self.channels
1304 },
9efec4ed 1305 _ => self.bsize,
58309c73
KS
1306 }
1307 }
1308 }
1309 fn get_next_chunk(&mut self) -> Option<(NATimeInfo, u64, usize)> {
bbbf86dd 1310 let pts_val = self.timesearch.map_time(self.cur_sample as u32, &self.time_to_sample);
4e7deeda
KS
1311 let dts = if let Some(dts_corr) = self.ctts_map.map(self.cur_sample as u64) {
1312 let dts = match self.ctts_version {
1313 0 => pts_val.wrapping_add(u64::from(dts_corr)),
1314 1 => pts_val.wrapping_add(i64::from(dts_corr as i32) as u64),
1315 _ => unimplemented!(),
1316 };
1317 if (dts as i64) < 0 {
1318 None
1319 } else {
1320 Some(dts)
1321 }
1322 } else {
1323 None
1324 };
1325 let mut pts = NATimeInfo::new(Some(pts_val), dts, None, self.tb_num, self.tb_den);
58309c73
KS
1326 if self.chunk_offsets.len() == self.chunk_sizes.len() { // simple one-to-one mapping
1327 if self.cur_sample >= self.chunk_sizes.len() {
1328 return None;
1329 }
1330 let offset = self.chunk_offsets[self.cur_sample];
1331 let size = self.chunk_sizes[self.cur_sample] as usize;
1332 self.cur_sample += 1;
1333 Some((pts, offset, size))
1334 } else {
1335 if self.samples_left == 0 {
1336 if self.cur_chunk >= self.chunk_offsets.len() {
1337 return None;
1338 }
1339 for (idx, samples) in self.sample_map.iter() {
1340 if *idx as usize <= self.cur_chunk + 1 {
1341 self.samples_left = *samples as usize;
1342 } else {
1343 break;
1344 }
1345 }
1346 self.last_offset = self.chunk_offsets[self.cur_chunk];
1347 self.cur_chunk += 1;
1348 }
1349 let offset = self.last_offset;
1350 let size = self.get_size(self.cur_sample);
1351 self.last_offset += size as u64;
1352 if self.stream_type == StreamType::Video {
1353 self.samples_left -= 1;
d940f0c2
KS
1354 } else if self.frame_samples != 0 && self.bsize != 0 {
1355 let nblocks = size / self.bsize;
7afec34d
KS
1356 if self.raw_audio {
1357 pts.pts = Some(self.raw_apos);
1358 pts.duration = Some(nblocks as u64);
1359 self.raw_apos += nblocks as u64;
1360 }
d940f0c2
KS
1361 if nblocks > 0 {
1362 let consumed = (nblocks * self.frame_samples).min(self.samples_left);
1363 self.samples_left -= consumed;
1364 } else {
1365 self.samples_left = 0;
1366 }
7afec34d
KS
1367 } else if !self.raw_audio {
1368 self.samples_left -= 1;
58309c73 1369 } else {
7afec34d
KS
1370 const BLOCK_SAMPLES: usize = 1024 * 6; // should be multiple of 64 and 6 to fit both IMA ADPCM and MACE 6:1 blocks
1371 let max_size = self.calculate_chunk_size(BLOCK_SAMPLES);
1372 let cur_size = self.calculate_chunk_size(self.samples_left);
1373 let add_off = (size - cur_size) as u64;
1374 let dsize = cur_size.min(max_size);
1375 if self.samples_left >= BLOCK_SAMPLES {
1376 self.cur_sample += BLOCK_SAMPLES;
1377 self.samples_left -= BLOCK_SAMPLES;
1378 self.last_offset -= size as u64;
1379 } else {
1380 self.cur_sample += self.samples_left;
1381 self.samples_left = 0;
1382 }
1383 return Some((pts, offset + add_off, dsize));
58309c73
KS
1384 }
1385 self.cur_sample += 1;
1386 Some((pts, offset, size))
1387 }
1388 }
1389 fn get_size(&self, sample_no: usize) -> usize {
61cab15b 1390 if !self.chunk_sizes.is_empty() {
58309c73 1391 self.chunk_sizes[sample_no] as usize
61cab15b 1392 } else if !self.sample_map.is_empty() {
58309c73
KS
1393 let mut nsamp = 0;
1394 for (idx, samples) in self.sample_map.iter() {
1395 if *idx as usize <= self.cur_chunk {
1396 nsamp = *samples;
1397 } else {
1398 break;
1399 }
1400 }
1401 self.calculate_chunk_size(nsamp as usize)
1402 } else {
9efec4ed 1403 self.bsize
58309c73
KS
1404 }
1405 }
b7c882c1 1406 #[allow(clippy::collapsible_if)]
7afec34d 1407 fn seek(&mut self, pts: u64, tpoint: NATimePoint) -> DemuxerResult<()> {
58309c73
KS
1408 self.cur_sample = pts as usize;
1409 self.samples_left = 0;
07972bf3 1410 self.cur_ts = None;
58309c73 1411 if self.stream_type == StreamType::Audio {
7afec34d
KS
1412 if let NATimePoint::Milliseconds(ms) = tpoint {
1413 let exp_pts = NATimeInfo::time_to_ts(ms, 1000, self.tb_num, self.tb_den);
1414 if self.raw_audio {
1415 if self.frame_samples != 0 {
1416 self.raw_apos = exp_pts / (self.frame_samples as u64);
1417 let mut apos = 0;
1418 self.cur_sample = 0;
1419 self.cur_chunk = 0;
1420 let mut cmap = self.sample_map.iter();
1421 let mut cur_samps = 0;
1422 let (mut next_idx, mut next_samples) = cmap.next().unwrap();
1423 loop {
1424 if self.cur_chunk + 1 == next_idx as usize {
1425 self.samples_left = cur_samps;
1426 cur_samps = next_samples as usize;
1427 if let Some((new_idx, new_samples)) = cmap.next() {
1428 next_idx = *new_idx;
1429 next_samples = *new_samples;
1430 }
1431 }
1432 self.raw_apos = apos;
1433 apos += (cur_samps / self.frame_samples) as u64;
1434 if apos > exp_pts {
1435 if cur_samps == self.frame_samples || apos > exp_pts + 1 {
1436 if self.cur_chunk >= self.chunk_offsets.len() {
1437 return Err(DemuxerError::SeekError);
1438 }
1439 self.last_offset = self.chunk_offsets[self.cur_chunk];
1440 break;
1441 }
1442 }
1443 self.cur_chunk += 1;
1444 }
1445 self.samples_left = cur_samps;
1446 self.cur_chunk += 1;
1447 } else {
1448 self.raw_apos = exp_pts;
1449 self.cur_sample = exp_pts as usize;
1450 let mut csamp = 0;
1451 self.cur_chunk = 0;
1452 let mut cmap = self.sample_map.iter();
1453 let mut cur_samps = 0;
1454 let (mut next_idx, mut next_samples) = cmap.next().unwrap();
1455 loop {
1456 if self.cur_chunk + 1 == next_idx as usize {
1457 self.samples_left = cur_samps;
1458 cur_samps = next_samples as usize;
1459 if let Some((new_idx, new_samples)) = cmap.next() {
1460 next_idx = *new_idx;
1461 next_samples = *new_samples;
1462 }
1463 }
1464 csamp += cur_samps;
1465 if csamp > self.cur_sample {
1466 if self.cur_chunk >= self.chunk_offsets.len() {
1467 return Err(DemuxerError::SeekError);
1468 }
1469 self.last_offset = self.chunk_offsets[self.cur_chunk];
1470 break;
1471 }
1472 self.cur_chunk += 1;
1473 }
1474 self.samples_left = csamp - self.cur_sample;
1475 self.cur_chunk += 1;
1476 }
5317ee9c 1477 } else if self.chunk_offsets.len() == self.chunk_sizes.len() {
7afec34d 1478 self.cur_chunk = self.cur_sample;
5317ee9c
KS
1479 } else {
1480 let mut csamp = 0;
1481 self.cur_chunk = 0;
1482 let mut cmap = self.sample_map.iter();
1483 let mut cur_samps = 0;
1484 let (mut next_idx, mut next_samples) = cmap.next().unwrap();
1485 loop {
1486 if self.cur_chunk + 1 == next_idx as usize {
1487 self.samples_left = cur_samps;
1488 cur_samps = next_samples as usize;
1489 if let Some((new_idx, new_samples)) = cmap.next() {
1490 next_idx = *new_idx;
1491 next_samples = *new_samples;
1492 }
1493 }
1494 csamp += cur_samps;
1495 if csamp > self.cur_sample {
1496 if self.cur_chunk >= self.chunk_offsets.len() {
1497 return Err(DemuxerError::SeekError);
1498 }
1499 self.last_offset = self.chunk_offsets[self.cur_chunk];
1500 break;
1501 }
1502 self.cur_chunk += 1;
1503 }
1504 self.cur_sample = csamp - cur_samps;
1505 self.samples_left = cur_samps;
1506 self.last_offset = self.chunk_offsets[self.cur_chunk];
1507 self.cur_chunk += 1;
7afec34d
KS
1508 }
1509 } else {
1510 self.cur_chunk = self.cur_sample;
1511 }
61cab15b 1512 } else if self.chunk_offsets.len() != self.chunk_sizes.len() && !self.sample_map.is_empty() {
58309c73
KS
1513 let mut csamp = 0;
1514 self.cur_chunk = 0;
1515 let mut cmap = self.sample_map.iter();
1516 let mut cur_samps = 0;
1517 let (mut next_idx, mut next_samples) = cmap.next().unwrap();
1518 loop {
bbbf86dd 1519 if self.cur_chunk + 1 == next_idx as usize {
58309c73
KS
1520 self.samples_left = cur_samps;
1521 cur_samps = next_samples as usize;
1522 if let Some((new_idx, new_samples)) = cmap.next() {
1523 next_idx = *new_idx;
1524 next_samples = *new_samples;
1525 }
1526 }
1527 csamp += cur_samps;
1528 if csamp >= self.cur_sample {
7afec34d
KS
1529 if self.cur_chunk >= self.chunk_offsets.len() {
1530 return Err(DemuxerError::SeekError);
1531 }
58309c73
KS
1532 self.last_offset = self.chunk_offsets[self.cur_chunk];
1533 break;
1534 }
1535 self.cur_chunk += 1;
1536 }
1537 csamp -= cur_samps;
bbbf86dd 1538 for sample_no in csamp..self.cur_sample {
58309c73
KS
1539 self.last_offset += self.get_size(sample_no) as u64;
1540 }
bbbf86dd
KS
1541 self.samples_left = csamp + cur_samps - self.cur_sample;
1542 self.cur_chunk += 1;
58309c73 1543 }
7afec34d 1544 Ok(())
58309c73
KS
1545 }
1546}
1547
07972bf3
KS
1548fn process_packet(src: &mut ByteReader, strmgr: &StreamManager, track: &mut Track, pts: NATimeInfo, offset: u64, size: usize, first: bool) -> DemuxerResult<NAPacket> {
1549 if let Some(cpts) = pts.get_pts() {
1550 let ts = NATimeInfo::ts_to_time(cpts, 1000, pts.tb_num, pts.tb_den);
1551 track.cur_ts = Some(ts);
1552 } else {
1553 track.cur_ts = None;
1554 }
1555 let str = strmgr.get_stream(track.track_str_id);
1556 if str.is_none() { return Err(DemuxerError::InvalidData); }
1557 let stream = str.unwrap();
1558 src.seek(SeekFrom::Start(offset))?;
1559 let mut pkt = src.read_packet(stream, pts, false, size)?;
1560 if let Some(ref pal) = track.pal {
1561 let side_data = NASideData::Palette(first, pal.clone());
1562 pkt.add_side_data(side_data);
1563 }
1564 Ok(pkt)
1565}
1566
58309c73
KS
1567impl<'a> DemuxCore<'a> for MOVDemuxer<'a> {
1568 fn open(&mut self, strmgr: &mut StreamManager, seek_index: &mut SeekIndex) -> DemuxerResult<()> {
1569 self.read_root(strmgr)?;
1570 validate!(self.mdat_pos > 0);
61cab15b 1571 validate!(!self.tracks.is_empty());
0a0456a8
KS
1572 for track in self.tracks.iter_mut() {
1573 let mut str = None;
1574 std::mem::swap(&mut track.stream, &mut str);
1575 if let Some(stream) = str {
1576 let str_id = strmgr.add_stream(stream).unwrap();
1577 track.track_str_id = str_id;
1578 }
1579 }
58309c73
KS
1580 for track in self.tracks.iter() {
1581 track.fill_seek_index(seek_index);
1582 }
1583 self.src.seek(SeekFrom::Start(self.mdat_pos))?;
1584 self.cur_track = 0;
1585 Ok(())
1586 }
1587
1588 fn get_frame(&mut self, strmgr: &mut StreamManager) -> DemuxerResult<NAPacket> {
61cab15b 1589 if self.tracks.is_empty() {
58309c73
KS
1590 return Err(DemuxerError::EOF);
1591 }
07972bf3
KS
1592 let mut has_all_time = true;
1593 let mut min_ts = std::u64::MAX;
1594 for trk in self.tracks.iter() {
1595 if let Some(ts) = trk.cur_ts {
1596 min_ts = min_ts.min(ts);
1597 } else {
1598 has_all_time = false;
1599 break;
1600 }
1601 }
1602 if has_all_time {
1603 for (trk_no, track) in self.tracks.iter_mut().enumerate() {
1604 if let Some(ts) = track.cur_ts {
1605 if ts == min_ts {
1606 let first = track.cur_sample == 0;
1607 if let Some((pts, offset, size)) = track.get_next_chunk() {
1608 self.cur_track = trk_no + 1;
1609 return process_packet(&mut self.src, strmgr, track, pts, offset, size, first);
1610 }
1611 }
1612 }
1613 }
1614 }
1615
58309c73
KS
1616 for _ in 0..self.tracks.len() {
1617 if self.cur_track >= self.tracks.len() {
1618 self.cur_track = 0;
1619 }
1620 let track = &mut self.tracks[self.cur_track];
1621 self.cur_track += 1;
58cd51fa 1622 let first = track.cur_sample == 0;
58309c73 1623 if let Some((pts, offset, size)) = track.get_next_chunk() {
07972bf3 1624 return process_packet(&mut self.src, strmgr, track, pts, offset, size, first);
58309c73
KS
1625 }
1626 }
61cab15b 1627 Err(DemuxerError::EOF)
58309c73
KS
1628 }
1629
24d99894 1630 fn seek(&mut self, time: NATimePoint, seek_index: &SeekIndex) -> DemuxerResult<()> {
58309c73
KS
1631 let ret = seek_index.find_pos(time);
1632 if ret.is_none() {
5317ee9c
KS
1633 if let NATimePoint::Milliseconds(_) = time {
1634 let mut aonly = true;
1635 for track in self.tracks.iter() {
1636 if track.stream_type != StreamType::Audio || !track.raw_audio {
1637 aonly = false;
1638 break;
1639 }
1640 }
1641 if aonly {
1642 for track in self.tracks.iter_mut() {
1643 track.seek(0, time)?;
1644 }
1645 return Ok(());
1646 }
1647 }
58309c73
KS
1648 return Err(DemuxerError::SeekError);
1649 }
1650 let seek_info = ret.unwrap();
5317ee9c
KS
1651 let tbn = self.tracks[seek_info.str_id as usize].tb_num;
1652 let tbd = self.tracks[seek_info.str_id as usize].tb_den;
58309c73 1653 for track in self.tracks.iter_mut() {
5317ee9c
KS
1654 let cur_pts = if track.track_id == seek_info.str_id {
1655 seek_info.pts
1656 } else {
1657 seek_info.pts * u64::from(tbn) * u64::from(track.tb_den) / (u64::from(tbd) * u64::from(track.tb_num))
1658 };
1659 track.seek(cur_pts, time)?;
58309c73
KS
1660 }
1661 Ok(())
1662 }
a480a0de
KS
1663 fn get_duration(&self) -> u64 {
1664 if self.tb_den != 0 {
1665 u64::from(self.duration) * 1000 / u64::from(self.tb_den)
1666 } else {
1667 0
1668 }
1669 }
58309c73
KS
1670}
1671
650d7bfb
KS
1672const PRINT_CHUNKS: &str = "print_chunks";
1673
1674const DEMUXER_OPTIONS: &[NAOptionDefinition] = &[
1675 NAOptionDefinition {
1676 name: PRINT_CHUNKS,
1677 description: "Print parsed file structure",
1678 opt_type: NAOptionDefinitionType::Bool },
1679];
1680
787b8d03 1681impl<'a> NAOptionHandler for MOVDemuxer<'a> {
650d7bfb 1682 fn get_supported_options(&self) -> &[NAOptionDefinition] { DEMUXER_OPTIONS }
b7c882c1 1683 #[allow(clippy::single_match)]
650d7bfb
KS
1684 fn set_options(&mut self, options: &[NAOption]) {
1685 for option in options.iter() {
1686 for opt_def in DEMUXER_OPTIONS.iter() {
1687 if opt_def.check(option).is_ok() {
1688 match (option.name, &option.value) {
1689 (PRINT_CHUNKS, NAValue::Bool(val)) => {
1690 self.print_chunks = *val;
1691 },
1692 _ => {},
1693 }
1694 }
1695 }
1696 }
1697 }
1698 fn query_option_value(&self, name: &str) -> Option<NAValue> {
1699 match name {
1700 PRINT_CHUNKS => Some(NAValue::Bool(self.print_chunks)),
1701 _ => None,
1702 }
1703 }
787b8d03
KS
1704}
1705
58309c73
KS
1706impl<'a> MOVDemuxer<'a> {
1707 fn new(io: &'a mut ByteReader<'a>) -> Self {
1708 MOVDemuxer {
1709 src: io,
1710 depth: 0,
1711 mdat_pos: 0,
1712 mdat_size: 0,
1713 tracks: Vec::with_capacity(2),
1714 cur_track: 0,
1715 tb_den: 0,
1716 duration: 0,
58cd51fa 1717 pal: None,
650d7bfb 1718
0a0456a8
KS
1719 moof_off: 0,
1720
650d7bfb 1721 print_chunks: false,
58309c73
KS
1722 }
1723 }
1724 fn read_root(&mut self, strmgr: &mut StreamManager) -> DemuxerResult<()> {
1725 self.depth = 0;
1726 while self.src.left() != 0 {
1727 let ret = read_chunk_header(&mut self.src);
1728 if ret.is_err() { break; }
1729 let (ctype, size) = ret.unwrap();
0a0456a8
KS
1730 if self.print_chunks {
1731 print_cname(ctype, size, self.src.tell(), 0);
1732 }
58309c73
KS
1733 if IGNORED_CHUNKS.contains(&ctype) {
1734 self.src.skip64(size)?;
1735 continue;
1736 }
1737 let handler = ROOT_CHUNK_HANDLERS.iter().find(|x| x.ctype == ctype);
1738 let read_size;
1739 if let Some(ref handler) = handler {
1740 read_size = (handler.parse)(self, strmgr, size)?;
1741 } else {
1742 println!("skipping unknown chunk {:08X} size {}", ctype, size);
1743 read_size = 0;
1744 }
1745 validate!(read_size <= size);
1746 self.src.skip64(size - read_size)?;
1747 }
1748//todo check if all needed chunks are found
1749 Ok(())
1750 }
1751 read_chunk_list!(root; "moov", read_moov, MOOV_CHUNK_HANDLERS);
0a0456a8 1752 read_chunk_list!(root; "moof", read_moof, MOOF_CHUNK_HANDLERS);
58309c73
KS
1753}
1754
1755pub struct MOVDemuxerCreator { }
1756
1757impl DemuxerCreator for MOVDemuxerCreator {
1758 fn new_demuxer<'a>(&self, br: &'a mut ByteReader<'a>) -> Box<dyn DemuxCore<'a> + 'a> {
1759 Box::new(MOVDemuxer::new(br))
1760 }
1761 fn get_name(&self) -> &'static str { "mov" }
1762}
1763
2736c15a
KS
1764const MOV_DEFAULT_PAL_2BIT: [u8; 4 * 4] = [
1765 0x93, 0x65, 0x5E, 0x00,
1766 0xFF, 0xFF, 0xFF, 0x00,
1767 0xDF, 0xD0, 0xAB, 0x00,
1768 0x00, 0x00, 0x00, 0x00
1769];
1770const MOV_DEFAULT_PAL_4BIT: [u8; 16 * 4] = [
1771 0xFF, 0xFB, 0xFF, 0x00,
1772 0xEF, 0xD9, 0xBB, 0x00,
1773 0xE8, 0xC9, 0xB1, 0x00,
1774 0x93, 0x65, 0x5E, 0x00,
1775 0xFC, 0xDE, 0xE8, 0x00,
1776 0x9D, 0x88, 0x91, 0x00,
1777 0xFF, 0xFF, 0xFF, 0x00,
1778 0xFF, 0xFF, 0xFF, 0x00,
1779 0xFF, 0xFF, 0xFF, 0x00,
1780 0x47, 0x48, 0x37, 0x00,
1781 0x7A, 0x5E, 0x55, 0x00,
1782 0xDF, 0xD0, 0xAB, 0x00,
1783 0xFF, 0xFB, 0xF9, 0x00,
1784 0xE8, 0xCA, 0xC5, 0x00,
1785 0x8A, 0x7C, 0x77, 0x00,
1786 0x00, 0x00, 0x00, 0x00
1787];
1788const MOV_DEFAULT_PAL_8BIT: [u8; 256 * 4] = [
1789 0xFF, 0xFF, 0xFF, 0x00,
1790 0xFF, 0xFF, 0xCC, 0x00,
1791 0xFF, 0xFF, 0x99, 0x00,
1792 0xFF, 0xFF, 0x66, 0x00,
1793 0xFF, 0xFF, 0x33, 0x00,
1794 0xFF, 0xFF, 0x00, 0x00,
1795 0xFF, 0xCC, 0xFF, 0x00,
1796 0xFF, 0xCC, 0xCC, 0x00,
1797 0xFF, 0xCC, 0x99, 0x00,
1798 0xFF, 0xCC, 0x66, 0x00,
1799 0xFF, 0xCC, 0x33, 0x00,
1800 0xFF, 0xCC, 0x00, 0x00,
1801 0xFF, 0x99, 0xFF, 0x00,
1802 0xFF, 0x99, 0xCC, 0x00,
1803 0xFF, 0x99, 0x99, 0x00,
1804 0xFF, 0x99, 0x66, 0x00,
1805 0xFF, 0x99, 0x33, 0x00,
1806 0xFF, 0x99, 0x00, 0x00,
1807 0xFF, 0x66, 0xFF, 0x00,
1808 0xFF, 0x66, 0xCC, 0x00,
1809 0xFF, 0x66, 0x99, 0x00,
1810 0xFF, 0x66, 0x66, 0x00,
1811 0xFF, 0x66, 0x33, 0x00,
1812 0xFF, 0x66, 0x00, 0x00,
1813 0xFF, 0x33, 0xFF, 0x00,
1814 0xFF, 0x33, 0xCC, 0x00,
1815 0xFF, 0x33, 0x99, 0x00,
1816 0xFF, 0x33, 0x66, 0x00,
1817 0xFF, 0x33, 0x33, 0x00,
1818 0xFF, 0x33, 0x00, 0x00,
1819 0xFF, 0x00, 0xFF, 0x00,
1820 0xFF, 0x00, 0xCC, 0x00,
1821 0xFF, 0x00, 0x99, 0x00,
1822 0xFF, 0x00, 0x66, 0x00,
1823 0xFF, 0x00, 0x33, 0x00,
1824 0xFF, 0x00, 0x00, 0x00,
1825 0xCC, 0xFF, 0xFF, 0x00,
1826 0xCC, 0xFF, 0xCC, 0x00,
1827 0xCC, 0xFF, 0x99, 0x00,
1828 0xCC, 0xFF, 0x66, 0x00,
1829 0xCC, 0xFF, 0x33, 0x00,
1830 0xCC, 0xFF, 0x00, 0x00,
1831 0xCC, 0xCC, 0xFF, 0x00,
1832 0xCC, 0xCC, 0xCC, 0x00,
1833 0xCC, 0xCC, 0x99, 0x00,
1834 0xCC, 0xCC, 0x66, 0x00,
1835 0xCC, 0xCC, 0x33, 0x00,
1836 0xCC, 0xCC, 0x00, 0x00,
1837 0xCC, 0x99, 0xFF, 0x00,
1838 0xCC, 0x99, 0xCC, 0x00,
1839 0xCC, 0x99, 0x99, 0x00,
1840 0xCC, 0x99, 0x66, 0x00,
1841 0xCC, 0x99, 0x33, 0x00,
1842 0xCC, 0x99, 0x00, 0x00,
1843 0xCC, 0x66, 0xFF, 0x00,
1844 0xCC, 0x66, 0xCC, 0x00,
1845 0xCC, 0x66, 0x99, 0x00,
1846 0xCC, 0x66, 0x66, 0x00,
1847 0xCC, 0x66, 0x33, 0x00,
1848 0xCC, 0x66, 0x00, 0x00,
1849 0xCC, 0x33, 0xFF, 0x00,
1850 0xCC, 0x33, 0xCC, 0x00,
1851 0xCC, 0x33, 0x99, 0x00,
1852 0xCC, 0x33, 0x66, 0x00,
1853 0xCC, 0x33, 0x33, 0x00,
1854 0xCC, 0x33, 0x00, 0x00,
1855 0xCC, 0x00, 0xFF, 0x00,
1856 0xCC, 0x00, 0xCC, 0x00,
1857 0xCC, 0x00, 0x99, 0x00,
1858 0xCC, 0x00, 0x66, 0x00,
1859 0xCC, 0x00, 0x33, 0x00,
1860 0xCC, 0x00, 0x00, 0x00,
1861 0x99, 0xFF, 0xFF, 0x00,
1862 0x99, 0xFF, 0xCC, 0x00,
1863 0x99, 0xFF, 0x99, 0x00,
1864 0x99, 0xFF, 0x66, 0x00,
1865 0x99, 0xFF, 0x33, 0x00,
1866 0x99, 0xFF, 0x00, 0x00,
1867 0x99, 0xCC, 0xFF, 0x00,
1868 0x99, 0xCC, 0xCC, 0x00,
1869 0x99, 0xCC, 0x99, 0x00,
1870 0x99, 0xCC, 0x66, 0x00,
1871 0x99, 0xCC, 0x33, 0x00,
1872 0x99, 0xCC, 0x00, 0x00,
1873 0x99, 0x99, 0xFF, 0x00,
1874 0x99, 0x99, 0xCC, 0x00,
1875 0x99, 0x99, 0x99, 0x00,
1876 0x99, 0x99, 0x66, 0x00,
1877 0x99, 0x99, 0x33, 0x00,
1878 0x99, 0x99, 0x00, 0x00,
1879 0x99, 0x66, 0xFF, 0x00,
1880 0x99, 0x66, 0xCC, 0x00,
1881 0x99, 0x66, 0x99, 0x00,
1882 0x99, 0x66, 0x66, 0x00,
1883 0x99, 0x66, 0x33, 0x00,
1884 0x99, 0x66, 0x00, 0x00,
1885 0x99, 0x33, 0xFF, 0x00,
1886 0x99, 0x33, 0xCC, 0x00,
1887 0x99, 0x33, 0x99, 0x00,
1888 0x99, 0x33, 0x66, 0x00,
1889 0x99, 0x33, 0x33, 0x00,
1890 0x99, 0x33, 0x00, 0x00,
1891 0x99, 0x00, 0xFF, 0x00,
1892 0x99, 0x00, 0xCC, 0x00,
1893 0x99, 0x00, 0x99, 0x00,
1894 0x99, 0x00, 0x66, 0x00,
1895 0x99, 0x00, 0x33, 0x00,
1896 0x99, 0x00, 0x00, 0x00,
1897 0x66, 0xFF, 0xFF, 0x00,
1898 0x66, 0xFF, 0xCC, 0x00,
1899 0x66, 0xFF, 0x99, 0x00,
1900 0x66, 0xFF, 0x66, 0x00,
1901 0x66, 0xFF, 0x33, 0x00,
1902 0x66, 0xFF, 0x00, 0x00,
1903 0x66, 0xCC, 0xFF, 0x00,
1904 0x66, 0xCC, 0xCC, 0x00,
1905 0x66, 0xCC, 0x99, 0x00,
1906 0x66, 0xCC, 0x66, 0x00,
1907 0x66, 0xCC, 0x33, 0x00,
1908 0x66, 0xCC, 0x00, 0x00,
1909 0x66, 0x99, 0xFF, 0x00,
1910 0x66, 0x99, 0xCC, 0x00,
1911 0x66, 0x99, 0x99, 0x00,
1912 0x66, 0x99, 0x66, 0x00,
1913 0x66, 0x99, 0x33, 0x00,
1914 0x66, 0x99, 0x00, 0x00,
1915 0x66, 0x66, 0xFF, 0x00,
1916 0x66, 0x66, 0xCC, 0x00,
1917 0x66, 0x66, 0x99, 0x00,
1918 0x66, 0x66, 0x66, 0x00,
1919 0x66, 0x66, 0x33, 0x00,
1920 0x66, 0x66, 0x00, 0x00,
1921 0x66, 0x33, 0xFF, 0x00,
1922 0x66, 0x33, 0xCC, 0x00,
1923 0x66, 0x33, 0x99, 0x00,
1924 0x66, 0x33, 0x66, 0x00,
1925 0x66, 0x33, 0x33, 0x00,
1926 0x66, 0x33, 0x00, 0x00,
1927 0x66, 0x00, 0xFF, 0x00,
1928 0x66, 0x00, 0xCC, 0x00,
1929 0x66, 0x00, 0x99, 0x00,
1930 0x66, 0x00, 0x66, 0x00,
1931 0x66, 0x00, 0x33, 0x00,
1932 0x66, 0x00, 0x00, 0x00,
1933 0x33, 0xFF, 0xFF, 0x00,
1934 0x33, 0xFF, 0xCC, 0x00,
1935 0x33, 0xFF, 0x99, 0x00,
1936 0x33, 0xFF, 0x66, 0x00,
1937 0x33, 0xFF, 0x33, 0x00,
1938 0x33, 0xFF, 0x00, 0x00,
1939 0x33, 0xCC, 0xFF, 0x00,
1940 0x33, 0xCC, 0xCC, 0x00,
1941 0x33, 0xCC, 0x99, 0x00,
1942 0x33, 0xCC, 0x66, 0x00,
1943 0x33, 0xCC, 0x33, 0x00,
1944 0x33, 0xCC, 0x00, 0x00,
1945 0x33, 0x99, 0xFF, 0x00,
1946 0x33, 0x99, 0xCC, 0x00,
1947 0x33, 0x99, 0x99, 0x00,
1948 0x33, 0x99, 0x66, 0x00,
1949 0x33, 0x99, 0x33, 0x00,
1950 0x33, 0x99, 0x00, 0x00,
1951 0x33, 0x66, 0xFF, 0x00,
1952 0x33, 0x66, 0xCC, 0x00,
1953 0x33, 0x66, 0x99, 0x00,
1954 0x33, 0x66, 0x66, 0x00,
1955 0x33, 0x66, 0x33, 0x00,
1956 0x33, 0x66, 0x00, 0x00,
1957 0x33, 0x33, 0xFF, 0x00,
1958 0x33, 0x33, 0xCC, 0x00,
1959 0x33, 0x33, 0x99, 0x00,
1960 0x33, 0x33, 0x66, 0x00,
1961 0x33, 0x33, 0x33, 0x00,
1962 0x33, 0x33, 0x00, 0x00,
1963 0x33, 0x00, 0xFF, 0x00,
1964 0x33, 0x00, 0xCC, 0x00,
1965 0x33, 0x00, 0x99, 0x00,
1966 0x33, 0x00, 0x66, 0x00,
1967 0x33, 0x00, 0x33, 0x00,
1968 0x33, 0x00, 0x00, 0x00,
1969 0x00, 0xFF, 0xFF, 0x00,
1970 0x00, 0xFF, 0xCC, 0x00,
1971 0x00, 0xFF, 0x99, 0x00,
1972 0x00, 0xFF, 0x66, 0x00,
1973 0x00, 0xFF, 0x33, 0x00,
1974 0x00, 0xFF, 0x00, 0x00,
1975 0x00, 0xCC, 0xFF, 0x00,
1976 0x00, 0xCC, 0xCC, 0x00,
1977 0x00, 0xCC, 0x99, 0x00,
1978 0x00, 0xCC, 0x66, 0x00,
1979 0x00, 0xCC, 0x33, 0x00,
1980 0x00, 0xCC, 0x00, 0x00,
1981 0x00, 0x99, 0xFF, 0x00,
1982 0x00, 0x99, 0xCC, 0x00,
1983 0x00, 0x99, 0x99, 0x00,
1984 0x00, 0x99, 0x66, 0x00,
1985 0x00, 0x99, 0x33, 0x00,
1986 0x00, 0x99, 0x00, 0x00,
1987 0x00, 0x66, 0xFF, 0x00,
1988 0x00, 0x66, 0xCC, 0x00,
1989 0x00, 0x66, 0x99, 0x00,
1990 0x00, 0x66, 0x66, 0x00,
1991 0x00, 0x66, 0x33, 0x00,
1992 0x00, 0x66, 0x00, 0x00,
1993 0x00, 0x33, 0xFF, 0x00,
1994 0x00, 0x33, 0xCC, 0x00,
1995 0x00, 0x33, 0x99, 0x00,
1996 0x00, 0x33, 0x66, 0x00,
1997 0x00, 0x33, 0x33, 0x00,
1998 0x00, 0x33, 0x00, 0x00,
1999 0x00, 0x00, 0xFF, 0x00,
2000 0x00, 0x00, 0xCC, 0x00,
2001 0x00, 0x00, 0x99, 0x00,
2002 0x00, 0x00, 0x66, 0x00,
2003 0x00, 0x00, 0x33, 0x00,
2004 0xEE, 0x00, 0x00, 0x00,
2005 0xDD, 0x00, 0x00, 0x00,
2006 0xBB, 0x00, 0x00, 0x00,
2007 0xAA, 0x00, 0x00, 0x00,
2008 0x88, 0x00, 0x00, 0x00,
2009 0x77, 0x00, 0x00, 0x00,
2010 0x55, 0x00, 0x00, 0x00,
2011 0x44, 0x00, 0x00, 0x00,
2012 0x22, 0x00, 0x00, 0x00,
2013 0x11, 0x00, 0x00, 0x00,
2014 0x00, 0xEE, 0x00, 0x00,
2015 0x00, 0xDD, 0x00, 0x00,
2016 0x00, 0xBB, 0x00, 0x00,
2017 0x00, 0xAA, 0x00, 0x00,
2018 0x00, 0x88, 0x00, 0x00,
2019 0x00, 0x77, 0x00, 0x00,
2020 0x00, 0x55, 0x00, 0x00,
2021 0x00, 0x44, 0x00, 0x00,
2022 0x00, 0x22, 0x00, 0x00,
2023 0x00, 0x11, 0x00, 0x00,
2024 0x00, 0x00, 0xEE, 0x00,
2025 0x00, 0x00, 0xDD, 0x00,
2026 0x00, 0x00, 0xBB, 0x00,
2027 0x00, 0x00, 0xAA, 0x00,
2028 0x00, 0x00, 0x88, 0x00,
2029 0x00, 0x00, 0x77, 0x00,
2030 0x00, 0x00, 0x55, 0x00,
2031 0x00, 0x00, 0x44, 0x00,
2032 0x00, 0x00, 0x22, 0x00,
2033 0x00, 0x00, 0x11, 0x00,
2034 0xEE, 0xEE, 0xEE, 0x00,
2035 0xDD, 0xDD, 0xDD, 0x00,
2036 0xBB, 0xBB, 0xBB, 0x00,
2037 0xAA, 0xAA, 0xAA, 0x00,
2038 0x88, 0x88, 0x88, 0x00,
2039 0x77, 0x77, 0x77, 0x00,
2040 0x55, 0x55, 0x55, 0x00,
2041 0x44, 0x44, 0x44, 0x00,
2042 0x22, 0x22, 0x22, 0x00,
2043 0x11, 0x11, 0x11, 0x00,
2044 0x00, 0x00, 0x00, 0x00
2045];
2046
58309c73
KS
2047#[cfg(test)]
2048mod test {
2049 use super::*;
2050 use std::fs::File;
2051
2052 #[test]
2053 fn test_mov_demux() {
886cde48 2054 // sample: https://samples.mplayerhq.hu/V-codecs/IV32/cubes.mov
58309c73
KS
2055 let mut file = File::open("assets/Indeo/cubes.mov").unwrap();
2056 let mut fr = FileReader::new_read(&mut file);
2057 let mut br = ByteReader::new(&mut fr);
2058 let mut dmx = MOVDemuxer::new(&mut br);
2059 let mut sm = StreamManager::new();
2060 let mut si = SeekIndex::new();
2061 dmx.open(&mut sm, &mut si).unwrap();
2062
2063 loop {
2064 let pktres = dmx.get_frame(&mut sm);
2065 if let Err(e) = pktres {
2066 if e == DemuxerError::EOF { break; }
2067 panic!("error");
2068 }
2069 let pkt = pktres.unwrap();
2070 println!("Got {}", pkt);
2071 }
2072 }
0a0456a8
KS
2073
2074 #[test]
2075 fn test_dash_demux() {
886cde48 2076 // sample: a stream downloaded with youtube-dl
0a0456a8
KS
2077 let mut file = File::open("assets/ITU/dash.m4a").unwrap();
2078 let mut fr = FileReader::new_read(&mut file);
2079 let mut br = ByteReader::new(&mut fr);
2080 let mut dmx = MOVDemuxer::new(&mut br);
2081 let mut sm = StreamManager::new();
2082 let mut si = SeekIndex::new();
2083 dmx.open(&mut sm, &mut si).unwrap();
2084
2085 loop {
2086 let pktres = dmx.get_frame(&mut sm);
2087 if let Err(e) = pktres {
2088 if e == DemuxerError::EOF { break; }
2089 panic!("error");
2090 }
2091 let pkt = pktres.unwrap();
2092 println!("Got {}", pkt);
2093 }
2094 }
58309c73 2095}