1 use nihav_core::demuxers::*;
2 use nihav_registry::register::*;
5 ($a:expr, $b:expr, $c:expr, $d:expr) => ({
6 (($a as u32) << 24) | (($b as u32) << 16) | (($c as u32) << 8) | ($d as u32)
9 (($arr[0] as u32) << 24) | (($arr[1] as u32) << 16) | (($arr[2] as u32) << 8) | ($arr[3] as u32)
14 fn skip64(&mut self, size: u64) -> ByteIOResult<()>;
17 impl<'a> Skip64 for ByteReader<'a> {
18 fn skip64(&mut self, size: u64) -> ByteIOResult<()> {
19 if (size as usize as u64) != size {
20 self.seek(SeekFrom::Current(size as i64))?;
22 self.read_skip(size as usize)?;
28 fn read_chunk_header(br: &mut ByteReader) -> DemuxerResult<(u32, u64)> {
29 let size = br.read_u32be()?;
30 let ctype = br.read_u32be()?;
32 Ok((ctype, br.left() as u64))
34 let size64 = br.read_u64be()?;
35 validate!(size64 >= 16);
36 Ok((ctype, size64 - 16))
39 Ok((ctype, (size as u64) - 8))
43 struct RootChunkHandler {
45 parse: fn(dmx: &mut MOVDemuxer, strmgr: &mut StreamManager, size: u64) -> DemuxerResult<u64>,
48 struct TrackChunkHandler {
50 parse: fn(track: &mut Track, br: &mut ByteReader, size: u64) -> DemuxerResult<u64>,
53 const IGNORED_CHUNKS: &[u32] = &[
54 mktag!(b"free"), mktag!(b"skip"), mktag!(b"udta"), mktag!(b"wide")
57 const ROOT_CHUNK_HANDLERS: &[RootChunkHandler] = &[
58 RootChunkHandler { ctype: mktag!(b"mdat"), parse: read_mdat },
59 RootChunkHandler { ctype: mktag!(b"moov"), parse: read_moov },
62 macro_rules! read_chunk_list {
63 (root; $name: expr, $fname: ident, $handlers: ident) => {
64 fn $fname(&mut self, strmgr: &mut StreamManager, size: u64) -> DemuxerResult<()> {
66 validate!(self.depth < 32);
67 let list_end = self.src.tell() + size;
68 while self.src.tell() < list_end {
69 let ret = read_chunk_header(&mut self.src);
70 if ret.is_err() { break; }
71 let (ctype, size) = ret.unwrap();
72 if self.src.tell() + size > list_end {
75 if IGNORED_CHUNKS.contains(&ctype) {
76 self.src.skip64(size)?;
79 let handler = $handlers.iter().find(|x| x.ctype == ctype);
81 if let Some(ref handler) = handler {
82 read_size = (handler.parse)(self, strmgr, size)?;
84 println!("skipping unknown chunk {:08X} size {}", ctype, size);
87 validate!(read_size <= size);
88 self.src.skip64(size - read_size)?;
91 validate!(self.src.tell() == list_end);
95 (track; $name: expr, $fname: ident, $handlers: ident) => {
96 fn $fname(&mut self, br: &mut ByteReader, size: u64) -> DemuxerResult<()> {
98 validate!(self.depth < 32);
99 let list_end = br.tell() + size;
100 while br.tell() < list_end {
101 let ret = read_chunk_header(br);
102 if ret.is_err() { break; }
103 let (ctype, size) = ret.unwrap();
104 if br.tell() + size > list_end {
107 if IGNORED_CHUNKS.contains(&ctype) {
111 let handler = $handlers.iter().find(|x| x.ctype == ctype);
113 if let Some(ref handler) = handler {
114 read_size = (handler.parse)(self, br, size)?;
118 validate!(read_size <= size);
119 br.skip64(size - read_size)?;
122 validate!(br.tell() == list_end);
128 fn skip_chunk(_track: &mut Track, br: &mut ByteReader, size: u64) -> DemuxerResult<u64> {
133 fn read_mdat(dmx: &mut MOVDemuxer, _strmgr: &mut StreamManager, size: u64) -> DemuxerResult<u64> {
134 dmx.mdat_pos = dmx.src.tell();
135 dmx.mdat_size = size;
136 dmx.src.skip64(size)?;
140 fn read_moov(dmx: &mut MOVDemuxer, strmgr: &mut StreamManager, size: u64) -> DemuxerResult<u64> {
141 dmx.read_moov(strmgr, size)?;
145 const MOOV_CHUNK_HANDLERS: &[RootChunkHandler] = &[
146 RootChunkHandler { ctype: mktag!(b"mvhd"), parse: read_mvhd },
147 RootChunkHandler { ctype: mktag!(b"trak"), parse: read_trak },
150 fn read_mvhd(dmx: &mut MOVDemuxer, _strmgr: &mut StreamManager, size: u64) -> DemuxerResult<u64> {
151 const KNOWN_MVHD_SIZE: u64 = 100;
152 let br = &mut dmx.src;
153 validate!(size >= KNOWN_MVHD_SIZE);
154 let version = br.read_byte()?;
155 validate!(version == 0);
156 let _flags = br.read_u24be()?;
157 let _ctime = br.read_u32be()?;
158 let _mtime = br.read_u32be()?;
159 let tscale = br.read_u32be()?;
160 let duration = br.read_u32be()?;
161 let _pref_rate = br.read_u32be()?;
162 let _pref_volume = br.read_u16be()?;
164 br.read_skip(36)?; // matrix
165 let _preview_time = br.read_u32be()?;
166 let _preview_duration = br.read_u32be()?;
167 let _poster_time = br.read_u32be()?;
168 let _sel_time = br.read_u32be()?;
169 let _sel_duration = br.read_u32be()?;
170 let _cur_time = br.read_u32be()?;
171 let _next_track_id = br.read_u32be()?;
172 dmx.duration = duration;
178 fn read_trak(dmx: &mut MOVDemuxer, strmgr: &mut StreamManager, size: u64) -> DemuxerResult<u64> {
179 let mut track = Track::new(dmx.cur_track as u32, dmx.tb_den);
180 track.read_trak(&mut dmx.src, size)?;
181 validate!(track.tkhd_found && track.stsd_found);
182 validate!(strmgr.get_stream_by_id(track.track_id).is_none());
185 std::mem::swap(&mut track.stream, &mut str);
186 if let Some(stream) = str {
187 let str_id = strmgr.add_stream(stream).unwrap();
188 track.track_str_id = str_id;
190 dmx.tracks.push(track);
194 const TRAK_CHUNK_HANDLERS: &[TrackChunkHandler] = &[
195 TrackChunkHandler { ctype: mktag!(b"clip"), parse: skip_chunk },
196 TrackChunkHandler { ctype: mktag!(b"matt"), parse: skip_chunk },
197 TrackChunkHandler { ctype: mktag!(b"edts"), parse: skip_chunk },
198 TrackChunkHandler { ctype: mktag!(b"tref"), parse: skip_chunk },
199 TrackChunkHandler { ctype: mktag!(b"load"), parse: skip_chunk },
200 TrackChunkHandler { ctype: mktag!(b"imap"), parse: skip_chunk },
201 TrackChunkHandler { ctype: mktag!(b"tkhd"), parse: read_tkhd },
202 TrackChunkHandler { ctype: mktag!(b"mdia"), parse: read_mdia },
205 fn read_tkhd(track: &mut Track, br: &mut ByteReader, size: u64) -> DemuxerResult<u64> {
206 const KNOWN_TKHD_SIZE: u64 = 84;
207 validate!(size >= KNOWN_TKHD_SIZE);
208 let version = br.read_byte()?;
209 validate!(version == 0);
210 let _flags = br.read_u24be()?;
211 let _ctime = br.read_u32be()?;
212 let _mtime = br.read_u32be()?;
213 let track_id = br.read_u32be()?;
215 let _duration = br.read_u32be()?;
217 let _layer = br.read_u16be()?;
218 let _alt_group = br.read_u16be()?;
219 let _volume = br.read_u16be()?;
221 br.read_skip(36)?; // matrix
222 let width = br.read_u32be()? as usize;
223 let height = br.read_u32be()? as usize;
224 track.width = width >> 16;
225 track.height = height >> 16;
226 track.track_id = track_id;
228 track.tkhd_found = true;
232 fn read_mdia(track: &mut Track, br: &mut ByteReader, size: u64) -> DemuxerResult<u64> {
233 track.stream_type = StreamType::None;
234 track.read_mdia(br, size)?;
238 const MDIA_CHUNK_HANDLERS: &[TrackChunkHandler] = &[
239 TrackChunkHandler { ctype: mktag!(b"mdhd"), parse: skip_chunk },
240 TrackChunkHandler { ctype: mktag!(b"hdlr"), parse: read_hdlr },
241 TrackChunkHandler { ctype: mktag!(b"minf"), parse: read_minf },
244 fn read_hdlr(track: &mut Track, br: &mut ByteReader, size: u64) -> DemuxerResult<u64> {
245 const KNOWN_HDLR_SIZE: u64 = 24;
246 validate!(size >= KNOWN_HDLR_SIZE);
247 let version = br.read_byte()?;
248 validate!(version == 0);
249 let flags = br.read_u24be()?;
250 validate!(flags == 0);
251 let comp_type = br.read_u32be()?;
252 let comp_subtype = br.read_u32be()?;
253 let _comp_manufacturer = br.read_u32be()?;
254 let _comp_flags = br.read_u32be()?;
255 let _comp_flags_mask = br.read_u32be()?;
257 if comp_type == mktag!(b"mhlr") {
258 if comp_subtype == mktag!(b"vide") {
259 track.stream_type = StreamType::Video;
260 } else if comp_subtype == mktag!(b"soun") {
261 track.stream_type = StreamType::Audio;
263 track.stream_type = StreamType::Data;
265 } else if comp_type == mktag!(b"dhlr") {
266 track.stream_type = StreamType::Data;
268 println!("Unknown stream type");
269 track.stream_type = StreamType::Data;
275 fn read_minf(track: &mut Track, br: &mut ByteReader, size: u64) -> DemuxerResult<u64> {
276 track.read_minf(br, size)?;
280 const MINF_CHUNK_HANDLERS: &[TrackChunkHandler] = &[
281 TrackChunkHandler { ctype: mktag!(b"hdlr"), parse: skip_chunk },
282 TrackChunkHandler { ctype: mktag!(b"dinf"), parse: skip_chunk },
283 TrackChunkHandler { ctype: mktag!(b"vmhd"), parse: read_vmhd },
284 TrackChunkHandler { ctype: mktag!(b"smhd"), parse: read_smhd },
285 TrackChunkHandler { ctype: mktag!(b"gmhd"), parse: read_gmhd },
286 TrackChunkHandler { ctype: mktag!(b"gmin"), parse: read_gmin },
287 TrackChunkHandler { ctype: mktag!(b"stbl"), parse: read_stbl },
290 fn read_vmhd(track: &mut Track, br: &mut ByteReader, size: u64) -> DemuxerResult<u64> {
291 const KNOWN_VMHD_SIZE: u64 = 12;
292 validate!(track.stream_type == StreamType::Video);
293 validate!(size >= KNOWN_VMHD_SIZE);
294 let version = br.read_byte()?;
295 validate!(version == 0);
296 let _flags = br.read_u24be()?;
297 br.read_skip(2)?; // graphics mode
298 br.read_skip(6)?; // opcolor
302 fn read_smhd(track: &mut Track, br: &mut ByteReader, size: u64) -> DemuxerResult<u64> {
303 const KNOWN_SMHD_SIZE: u64 = 8;
304 validate!(track.stream_type == StreamType::Audio);
305 validate!(size >= KNOWN_SMHD_SIZE);
306 let version = br.read_byte()?;
307 validate!(version == 0);
308 let _flags = br.read_u24be()?;
309 br.read_skip(2)?; // balance
314 fn read_gmhd(track: &mut Track, _br: &mut ByteReader, _size: u64) -> DemuxerResult<u64> {
315 validate!(track.stream_type == StreamType::Data);
319 fn read_gmin(track: &mut Track, br: &mut ByteReader, size: u64) -> DemuxerResult<u64> {
320 validate!(track.stream_type == StreamType::Data);
321 const KNOWN_GMIN_SIZE: u64 = 16;
322 validate!(size >= KNOWN_GMIN_SIZE);
323 let version = br.read_byte()?;
324 validate!(version == 0);
325 let _flags = br.read_u24be()?;
326 br.read_skip(2)?; // graphics mode
327 br.read_skip(6)?; // opcolor
328 br.read_skip(2)?; // balance
333 fn read_stbl(track: &mut Track, br: &mut ByteReader, size: u64) -> DemuxerResult<u64> {
334 track.read_stbl(br, size)?;
338 const STBL_CHUNK_HANDLERS: &[TrackChunkHandler] = &[
339 TrackChunkHandler { ctype: mktag!(b"stsd"), parse: read_stsd },
340 TrackChunkHandler { ctype: mktag!(b"stts"), parse: skip_chunk },
341 TrackChunkHandler { ctype: mktag!(b"stss"), parse: read_stss },
342 TrackChunkHandler { ctype: mktag!(b"stsc"), parse: read_stsc },
343 TrackChunkHandler { ctype: mktag!(b"stsz"), parse: read_stsz },
344 TrackChunkHandler { ctype: mktag!(b"stco"), parse: read_stco },
345 TrackChunkHandler { ctype: mktag!(b"stsh"), parse: skip_chunk },
348 fn read_stsd(track: &mut Track, br: &mut ByteReader, size: u64) -> DemuxerResult<u64> {
349 const KNOWN_STSD_SIZE: u64 = 24;
350 validate!(size >= KNOWN_STSD_SIZE);
351 let start_pos = br.tell();
352 let version = br.read_byte()?;
353 validate!(version == 0);
354 let _flags = br.read_u24be()?;
355 let entries = br.read_u32be()?;
356 validate!(entries > 0);
357 let esize = br.read_u32be()? as u64;
358 validate!(esize + 8 <= size);
359 let mut fcc = [0u8; 4];
360 br.read_buf(&mut fcc)?;
362 let _data_ref = br.read_u16be()?;
367 match track.stream_type {
368 StreamType::Video => {
369 let _ver = br.read_u16be()?;
370 let _revision = br.read_u16le()?;
371 let _vendor = br.read_u32be()?;
372 let _temp_quality = br.read_u32be()?;
373 let _spat_quality = br.read_u32be()?;
374 let width = br.read_u16be()? as usize;
375 let height = br.read_u16be()? as usize;
376 let _hor_res = br.read_u32be()?;
377 let _vert_res = br.read_u32be()?;
378 let data_size = br.read_u32be()?;
379 validate!(data_size == 0);
380 let _frame_count = br.read_u16be()? as usize;
381 let _cname_len = br.read_byte()? as usize;
382 br.read_skip(31)?; // actual compressor name
383 let depth = br.read_u16be()?;
384 let ctable_id = br.read_u16be()?;
385 validate!((depth <= 8) || (ctable_id == 0xFFFF));
389 // todo other atoms, put as extradata
390 let cname = if let Some(name) = find_codec_from_mov_video_fourcc(&fcc) {
392 } else if let Some(name) = find_codec_from_avi_fourcc(&fcc) {
397 let format = if depth > 8 { RGB24_FORMAT } else { PAL8_FORMAT };
398 let vhdr = NAVideoInfo::new(width, height, false, format);
400 if br.tell() - start_pos + 4 < size {
401 //todo skip various common atoms
402 let edata_size = br.read_u32be()? as usize;
403 let mut buf = vec![0; edata_size];
404 br.read_buf(buf.as_mut_slice())?;
409 codec_info = NACodecInfo::new(cname, NACodecTypeInfo::Video(vhdr), edata);
411 StreamType::Audio => {
412 let _ver = br.read_u16be()?;
413 let _revision = br.read_u16le()?;
414 let _vendor = br.read_u32be()?;
415 let nchannels = br.read_u16be()?;
416 validate!(nchannels <= 64);
417 let sample_size = br.read_u16be()?;
418 validate!(sample_size <= 128);
419 let _compr_id = br.read_u16be()?;
420 let packet_size = br.read_u16be()? as usize;
421 validate!(packet_size == 0);
422 let sample_rate = br.read_u32be()?;
423 validate!(sample_rate > 0);
424 let cname = if let Some(name) = find_codec_from_mov_audio_fourcc(&fcc) {
426 } 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]))) {
431 //todo adjust format for various PCM kinds
432 let soniton = NASoniton::new(sample_size as u8, SONITON_FLAG_SIGNED | SONITON_FLAG_BE);
434 let ahdr = NAAudioInfo::new(sample_rate >> 16, nchannels as u8, soniton, block_align);
436 codec_info = NACodecInfo::new(cname, NACodecTypeInfo::Audio(ahdr), edata);
437 track.channels = nchannels as usize;
438 track.bits = sample_size as usize;
440 StreamType::None => {
441 return Err(DemuxerError::InvalidData);
444 //todo put it all into extradata
446 codec_info = NACodecInfo::new("unknown", NACodecTypeInfo::None, edata);
449 let read_size = br.tell() - start_pos;
450 validate!(read_size <= size);
451 track.stream = Some(NAStream::new(track.stream_type, track.track_no, codec_info, 1, track.tb_den));
452 track.stsd_found = true;
456 fn read_stss(track: &mut Track, br: &mut ByteReader, size: u64) -> DemuxerResult<u64> {
457 let version = br.read_byte()?;
458 validate!(version == 0);
459 let _flags = br.read_u24be()?;
460 let entries = br.read_u32be()? as usize;
461 validate!(entries < ((std::u32::MAX >> 2) - 8) as usize);
462 validate!((entries * 4 + 8) as u64 == size);
463 track.keyframes = Vec::with_capacity(entries);
464 let mut last_sample_no = 0;
465 for _ in 0..entries {
466 let sample_no = br.read_u32be()?;
467 validate!(sample_no > last_sample_no);
468 track.keyframes.push(sample_no);
469 last_sample_no = sample_no;
474 fn read_stsc(track: &mut Track, br: &mut ByteReader, size: u64) -> DemuxerResult<u64> {
475 let version = br.read_byte()?;
476 validate!(version == 0);
477 let _flags = br.read_u24be()?;
478 let entries = br.read_u32be()? as usize;
479 validate!(entries < ((std::u32::MAX / 12) - 8) as usize);
480 validate!((entries * 12 + 8) as u64 == size);
481 track.sample_map = Vec::with_capacity(entries);
482 let mut last_sample_no = 0;
483 for _i in 0..entries {
484 let sample_no = br.read_u32be()?;
485 validate!(sample_no > last_sample_no);
486 let nsamples = br.read_u32be()?;
487 let _sample_desc = br.read_u32be()?;
488 track.sample_map.push((sample_no, nsamples));
489 last_sample_no = sample_no;
494 fn read_stsz(track: &mut Track, br: &mut ByteReader, size: u64) -> DemuxerResult<u64> {
495 let version = br.read_byte()?;
496 validate!(version == 0);
497 let _flags = br.read_u24be()?;
498 let sample_size = br.read_u32be()?;
499 if sample_size != 0 {
500 track.sample_size = sample_size;
503 let entries = br.read_u32be()? as usize;
504 validate!((entries * 4 + 12) as u64 == size);
505 track.chunk_sizes = Vec::with_capacity(entries);
506 for _ in 0..entries {
507 let sample_size = br.read_u32be()?;
508 track.chunk_sizes.push(sample_size);
514 fn read_stco(track: &mut Track, br: &mut ByteReader, size: u64) -> DemuxerResult<u64> {
515 let version = br.read_byte()?;
516 validate!(version == 0);
517 let _flags = br.read_u24be()?;
518 let entries = br.read_u32be()? as usize;
519 validate!((entries * 4 + 8) as u64 == size);
520 track.chunk_offsets = Vec::with_capacity(entries);
521 for _i in 0..entries {
522 let sample_offset = br.read_u32be()?;
523 track.chunk_offsets.push(u64::from(sample_offset));
528 struct MOVDemuxer<'a> {
529 src: &'a mut ByteReader<'a>,
547 stream_type: StreamType,
554 chunk_sizes: Vec<u32>,
555 chunk_offsets: Vec<u64>,
556 sample_map: Vec<(u32, u32)>,
558 stream: Option<NAStream>,
566 fn new(track_no: u32, tb_den: u32) -> Self {
574 stream_type: StreamType::None,
580 keyframes: Vec::new(),
581 chunk_sizes: Vec::new(),
582 chunk_offsets: Vec::new(),
583 sample_map: Vec::new(),
593 read_chunk_list!(track; "trak", read_trak, TRAK_CHUNK_HANDLERS);
594 read_chunk_list!(track; "mdia", read_mdia, MDIA_CHUNK_HANDLERS);
595 read_chunk_list!(track; "minf", read_minf, MINF_CHUNK_HANDLERS);
596 read_chunk_list!(track; "stbl", read_stbl, STBL_CHUNK_HANDLERS);
597 fn fill_seek_index(&self, seek_index: &mut SeekIndex) {
598 if self.keyframes.len() > 0 {
599 seek_index.mode = SeekIndexMode::Present;
601 for kf_time in self.keyframes.iter() {
602 let pts = u64::from(*kf_time - 1);
603 let time = NATimeInfo::ts_to_time(pts, 1000, 1, self.tb_den);
604 let idx = (*kf_time - 1) as usize;
605 if idx < self.chunk_offsets.len() {
606 let pos = self.chunk_offsets[idx];
607 seek_index.add_entry(self.track_no as u32, SeekEntry { time, pts, pos });
611 fn calculate_chunk_size(&self, nsamp: usize) -> usize {
613 self.sample_size as usize
616 b"NONE" | b"raw " | b"twos" | b"sowt" => {
617 (nsamp * self.bits * self.channels + 7) >> 3
620 let nblocks = (nsamp + 63) >> 6;
621 nblocks * 34 * self.channels
624 (nsamp + 5) / 6 * 2 * self.channels
627 (nsamp + 5) / 6 * self.channels
629 b"in24" => nsamp * 3 * self.channels,
630 b"in32" | b"fl32" => nsamp * 4 * self.channels,
631 b"fl64" => nsamp * 8 * self.channels,
632 b"ulaw" | b"alaw" => nsamp,
633 b"ms\x00\x02" => { //MS ADPCM
634 ((nsamp - 1) / 2 + 7) * self.channels
636 b"ms\x00\x21" => { //IMA ADPCM
637 (nsamp / 2 + 4) * self.channels
639 _ => self.sample_size as usize,
643 fn get_next_chunk(&mut self) -> Option<(NATimeInfo, u64, usize)> {
644 let pts = NATimeInfo::new(Some(self.cur_sample as u64), None, None, 1, self.tb_den);
646 if self.chunk_offsets.len() == self.chunk_sizes.len() { // simple one-to-one mapping
647 if self.cur_sample >= self.chunk_sizes.len() {
650 let offset = self.chunk_offsets[self.cur_sample];
651 let size = self.chunk_sizes[self.cur_sample] as usize;
652 self.cur_sample += 1;
653 Some((pts, offset, size))
655 if self.samples_left == 0 {
656 if self.cur_chunk >= self.chunk_offsets.len() {
659 for (idx, samples) in self.sample_map.iter() {
660 if *idx as usize <= self.cur_chunk + 1 {
661 self.samples_left = *samples as usize;
666 self.last_offset = self.chunk_offsets[self.cur_chunk];
669 let offset = self.last_offset;
670 let size = self.get_size(self.cur_sample);
671 self.last_offset += size as u64;
672 if self.stream_type == StreamType::Video {
673 self.samples_left -= 1;
675 self.samples_left = 0;
677 self.cur_sample += 1;
678 Some((pts, offset, size))
681 fn get_size(&self, sample_no: usize) -> usize {
682 if self.chunk_sizes.len() > 0 {
683 self.chunk_sizes[sample_no] as usize
684 } else if self.sample_map.len() > 0 {
686 for (idx, samples) in self.sample_map.iter() {
687 if *idx as usize <= self.cur_chunk {
693 self.calculate_chunk_size(nsamp as usize)
695 self.sample_size as usize
698 fn seek(&mut self, pts: u64) {
699 self.cur_sample = pts as usize;
700 self.samples_left = 0;
701 if self.stream_type == StreamType::Audio {
702 self.cur_chunk = self.cur_sample;
703 } else if self.chunk_offsets.len() != self.chunk_sizes.len() && self.sample_map.len() > 0{
706 let mut cmap = self.sample_map.iter();
707 let mut cur_samps = 0;
708 let (mut next_idx, mut next_samples) = cmap.next().unwrap();
710 if self.cur_chunk == next_idx as usize {
711 self.samples_left = cur_samps;
712 cur_samps = next_samples as usize;
713 if let Some((new_idx, new_samples)) = cmap.next() {
715 next_samples = *new_samples;
719 if csamp >= self.cur_sample {
720 self.last_offset = self.chunk_offsets[self.cur_chunk];
726 for sample_no in csamp..self.cur_chunk {
727 self.last_offset += self.get_size(sample_no) as u64;
729 self.samples_left = self.cur_sample - csamp - cur_samps;
734 impl<'a> DemuxCore<'a> for MOVDemuxer<'a> {
735 fn open(&mut self, strmgr: &mut StreamManager, seek_index: &mut SeekIndex) -> DemuxerResult<()> {
736 self.read_root(strmgr)?;
737 validate!(self.mdat_pos > 0);
738 validate!(self.tracks.len() > 0);
739 for track in self.tracks.iter() {
740 track.fill_seek_index(seek_index);
742 self.src.seek(SeekFrom::Start(self.mdat_pos))?;
747 fn get_frame(&mut self, strmgr: &mut StreamManager) -> DemuxerResult<NAPacket> {
748 if self.tracks.len() == 0 {
749 return Err(DemuxerError::EOF);
751 for _ in 0..self.tracks.len() {
752 if self.cur_track >= self.tracks.len() {
755 let track = &mut self.tracks[self.cur_track];
757 if let Some((pts, offset, size)) = track.get_next_chunk() {
758 let str = strmgr.get_stream(track.track_str_id);
759 if str.is_none() { return Err(DemuxerError::InvalidData); }
760 let stream = str.unwrap();
761 self.src.seek(SeekFrom::Start(offset))?;
762 let pkt = self.src.read_packet(stream, pts, false, size)?;
766 return Err(DemuxerError::EOF);
769 fn seek(&mut self, time: u64, seek_index: &SeekIndex) -> DemuxerResult<()> {
770 let ret = seek_index.find_pos(time);
772 return Err(DemuxerError::SeekError);
774 let seek_info = ret.unwrap();
775 for track in self.tracks.iter_mut() {
776 track.seek(seek_info.pts);
782 impl<'a> MOVDemuxer<'a> {
783 fn new(io: &'a mut ByteReader<'a>) -> Self {
789 tracks: Vec::with_capacity(2),
795 fn read_root(&mut self, strmgr: &mut StreamManager) -> DemuxerResult<()> {
797 while self.src.left() != 0 {
798 let ret = read_chunk_header(&mut self.src);
799 if ret.is_err() { break; }
800 let (ctype, size) = ret.unwrap();
801 if IGNORED_CHUNKS.contains(&ctype) {
802 self.src.skip64(size)?;
805 let handler = ROOT_CHUNK_HANDLERS.iter().find(|x| x.ctype == ctype);
807 if let Some(ref handler) = handler {
808 read_size = (handler.parse)(self, strmgr, size)?;
810 println!("skipping unknown chunk {:08X} size {}", ctype, size);
813 validate!(read_size <= size);
814 self.src.skip64(size - read_size)?;
816 //todo check if all needed chunks are found
819 read_chunk_list!(root; "moov", read_moov, MOOV_CHUNK_HANDLERS);
822 pub struct MOVDemuxerCreator { }
824 impl DemuxerCreator for MOVDemuxerCreator {
825 fn new_demuxer<'a>(&self, br: &'a mut ByteReader<'a>) -> Box<dyn DemuxCore<'a> + 'a> {
826 Box::new(MOVDemuxer::new(br))
828 fn get_name(&self) -> &'static str { "mov" }
837 fn test_mov_demux() {
838 let mut file = File::open("assets/Indeo/cubes.mov").unwrap();
839 let mut fr = FileReader::new_read(&mut file);
840 let mut br = ByteReader::new(&mut fr);
841 let mut dmx = MOVDemuxer::new(&mut br);
842 let mut sm = StreamManager::new();
843 let mut si = SeekIndex::new();
844 dmx.open(&mut sm, &mut si).unwrap();
847 let pktres = dmx.get_frame(&mut sm);
848 if let Err(e) = pktres {
849 if e == DemuxerError::EOF { break; }
852 let pkt = pktres.unwrap();
853 println!("Got {}", pkt);