]> git.nihav.org Git - nihav.git/blob - nihav-commonfmt/src/demuxers/mov.rs
core/scale: add conversion into paletted format
[nihav.git] / nihav-commonfmt / src / demuxers / mov.rs
1 use nihav_core::demuxers::*;
2 use nihav_registry::register::*;
3
4 macro_rules! mktag {
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)
7 });
8 ($arr:expr) => ({
9 (($arr[0] as u32) << 24) | (($arr[1] as u32) << 16) | (($arr[2] as u32) << 8) | ($arr[3] as u32)
10 });
11 }
12
13 trait Skip64 {
14 fn skip64(&mut self, size: u64) -> ByteIOResult<()>;
15 }
16
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))?;
21 } else {
22 self.read_skip(size as usize)?;
23 }
24 Ok(())
25 }
26 }
27
28 fn read_chunk_header(br: &mut ByteReader) -> DemuxerResult<(u32, u64)> {
29 let size = br.read_u32be()?;
30 let ctype = br.read_u32be()?;
31 if size == 0 {
32 Ok((ctype, br.left() as u64))
33 } else if size == 1 {
34 let size64 = br.read_u64be()?;
35 validate!(size64 >= 16);
36 Ok((ctype, size64 - 16))
37 } else {
38 validate!(size >= 8);
39 Ok((ctype, (size as u64) - 8))
40 }
41 }
42
43 struct RootChunkHandler {
44 ctype: u32,
45 parse: fn(dmx: &mut MOVDemuxer, strmgr: &mut StreamManager, size: u64) -> DemuxerResult<u64>,
46 }
47
48 struct TrackChunkHandler {
49 ctype: u32,
50 parse: fn(track: &mut Track, br: &mut ByteReader, size: u64) -> DemuxerResult<u64>,
51 }
52
53 const IGNORED_CHUNKS: &[u32] = &[
54 mktag!(b"free"), mktag!(b"skip"), mktag!(b"udta"), mktag!(b"wide")
55 ];
56
57 const ROOT_CHUNK_HANDLERS: &[RootChunkHandler] = &[
58 RootChunkHandler { ctype: mktag!(b"mdat"), parse: read_mdat },
59 RootChunkHandler { ctype: mktag!(b"moov"), parse: read_moov },
60 ];
61
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<()> {
65 self.depth += 1;
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 {
73 break;
74 }
75 if IGNORED_CHUNKS.contains(&ctype) {
76 self.src.skip64(size)?;
77 continue;
78 }
79 let handler = $handlers.iter().find(|x| x.ctype == ctype);
80 let read_size;
81 if let Some(ref handler) = handler {
82 read_size = (handler.parse)(self, strmgr, size)?;
83 } else {
84 println!("skipping unknown chunk {:08X} size {}", ctype, size);
85 read_size = 0;
86 }
87 validate!(read_size <= size);
88 self.src.skip64(size - read_size)?;
89 }
90 self.depth -= 1;
91 validate!(self.src.tell() == list_end);
92 Ok(())
93 }
94 };
95 (track; $name: expr, $fname: ident, $handlers: ident) => {
96 fn $fname(&mut self, br: &mut ByteReader, size: u64) -> DemuxerResult<()> {
97 self.depth += 1;
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 {
105 break;
106 }
107 if IGNORED_CHUNKS.contains(&ctype) {
108 br.skip64(size)?;
109 continue;
110 }
111 let handler = $handlers.iter().find(|x| x.ctype == ctype);
112 let read_size;
113 if let Some(ref handler) = handler {
114 read_size = (handler.parse)(self, br, size)?;
115 } else {
116 read_size = 0;
117 }
118 validate!(read_size <= size);
119 br.skip64(size - read_size)?;
120 }
121 self.depth -= 1;
122 validate!(br.tell() == list_end);
123 Ok(())
124 }
125 }
126 }
127
128 fn skip_chunk(_track: &mut Track, br: &mut ByteReader, size: u64) -> DemuxerResult<u64> {
129 br.skip64(size)?;
130 Ok(size)
131 }
132
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)?;
137 Ok(size)
138 }
139
140 fn read_moov(dmx: &mut MOVDemuxer, strmgr: &mut StreamManager, size: u64) -> DemuxerResult<u64> {
141 dmx.read_moov(strmgr, size)?;
142 Ok(size)
143 }
144
145 const MOOV_CHUNK_HANDLERS: &[RootChunkHandler] = &[
146 RootChunkHandler { ctype: mktag!(b"mvhd"), parse: read_mvhd },
147 RootChunkHandler { ctype: mktag!(b"trak"), parse: read_trak },
148 ];
149
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()?;
163 br.read_skip(10)?;
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;
173 dmx.tb_den = tscale;
174
175 Ok(KNOWN_MVHD_SIZE)
176 }
177
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());
183 dmx.cur_track += 1;
184 let mut str = 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;
189 }
190 dmx.tracks.push(track);
191 Ok(size)
192 }
193
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 },
203 ];
204
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()?;
214 br.read_skip(4)?;
215 let _duration = br.read_u32be()?;
216 br.read_skip(8)?;
217 let _layer = br.read_u16be()?;
218 let _alt_group = br.read_u16be()?;
219 let _volume = br.read_u16be()?;
220 br.read_skip(2)?;
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;
227
228 track.tkhd_found = true;
229 Ok(KNOWN_TKHD_SIZE)
230 }
231
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)?;
235 Ok(size)
236 }
237
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 },
242 ];
243
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()?;
256
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;
262 } else {
263 track.stream_type = StreamType::Data;
264 }
265 } else if comp_type == mktag!(b"dhlr") {
266 track.stream_type = StreamType::Data;
267 } else {
268 println!("Unknown stream type");
269 track.stream_type = StreamType::Data;
270 }
271
272 Ok(KNOWN_HDLR_SIZE)
273 }
274
275 fn read_minf(track: &mut Track, br: &mut ByteReader, size: u64) -> DemuxerResult<u64> {
276 track.read_minf(br, size)?;
277 Ok(size)
278 }
279
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 },
288 ];
289
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
299 Ok(KNOWN_VMHD_SIZE)
300 }
301
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
310 br.read_skip(2)?;
311 Ok(KNOWN_SMHD_SIZE)
312 }
313
314 fn read_gmhd(track: &mut Track, _br: &mut ByteReader, _size: u64) -> DemuxerResult<u64> {
315 validate!(track.stream_type == StreamType::Data);
316 Ok(0)
317 }
318
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
329 br.read_skip(2)?;
330 Ok(KNOWN_GMIN_SIZE)
331 }
332
333 fn read_stbl(track: &mut Track, br: &mut ByteReader, size: u64) -> DemuxerResult<u64> {
334 track.read_stbl(br, size)?;
335 Ok(size)
336 }
337
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 },
346 ];
347
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)?;
361 br.read_skip(6)?;
362 let _data_ref = br.read_u16be()?;
363
364 track.fcc = fcc;
365
366 let codec_info;
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));
386 if ctable_id == 0 {
387 unimplemented!();
388 }
389 // todo other atoms, put as extradata
390 let cname = if let Some(name) = find_codec_from_mov_video_fourcc(&fcc) {
391 name
392 } else if let Some(name) = find_codec_from_avi_fourcc(&fcc) {
393 name
394 } else {
395 "unknown"
396 };
397 let format = if depth > 8 { RGB24_FORMAT } else { PAL8_FORMAT };
398 let vhdr = NAVideoInfo::new(width, height, false, format);
399 let edata;
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())?;
405 edata = Some(buf);
406 } else {
407 edata = None;
408 }
409 codec_info = NACodecInfo::new(cname, NACodecTypeInfo::Video(vhdr), edata);
410 },
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) {
425 name
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]))) {
427 name
428 } else {
429 "unknown"
430 };
431 //todo adjust format for various PCM kinds
432 let soniton = NASoniton::new(sample_size as u8, SONITON_FLAG_SIGNED | SONITON_FLAG_BE);
433 let block_align = 1;
434 let ahdr = NAAudioInfo::new(sample_rate >> 16, nchannels as u8, soniton, block_align);
435 let edata = None;
436 codec_info = NACodecInfo::new(cname, NACodecTypeInfo::Audio(ahdr), edata);
437 track.channels = nchannels as usize;
438 track.bits = sample_size as usize;
439 },
440 StreamType::None => {
441 return Err(DemuxerError::InvalidData);
442 },
443 _ => {
444 //todo put it all into extradata
445 let edata = None;
446 codec_info = NACodecInfo::new("unknown", NACodecTypeInfo::None, edata);
447 },
448 };
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;
453 Ok(read_size)
454 }
455
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;
470 }
471 Ok(size)
472 }
473
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;
490 }
491 Ok(size)
492 }
493
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;
501 Ok(8)
502 } else {
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);
509 }
510 Ok(size)
511 }
512 }
513
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));
524 }
525 Ok(size)
526 }
527
528 struct MOVDemuxer<'a> {
529 src: &'a mut ByteReader<'a>,
530 depth: usize,
531 mdat_pos: u64,
532 mdat_size: u64,
533 tracks: Vec<Track>,
534 cur_track: usize,
535 tb_den: u32,
536 duration: u32,
537 }
538
539 struct Track {
540 track_id: u32,
541 track_str_id: usize,
542 track_no: u32,
543 tb_den: u32,
544 depth: u8,
545 tkhd_found: bool,
546 stsd_found: bool,
547 stream_type: StreamType,
548 width: usize,
549 height: usize,
550 channels: usize,
551 bits: usize,
552 fcc: [u8; 4],
553 keyframes: Vec<u32>,
554 chunk_sizes: Vec<u32>,
555 chunk_offsets: Vec<u64>,
556 sample_map: Vec<(u32, u32)>,
557 sample_size: u32,
558 stream: Option<NAStream>,
559 cur_chunk: usize,
560 cur_sample: usize,
561 samples_left: usize,
562 last_offset: u64,
563 }
564
565 impl Track {
566 fn new(track_no: u32, tb_den: u32) -> Self {
567 Self {
568 tkhd_found: false,
569 stsd_found: false,
570 track_id: 0,
571 track_str_id: 0,
572 track_no,
573 tb_den,
574 stream_type: StreamType::None,
575 width: 0,
576 height: 0,
577 channels: 0,
578 bits: 0,
579 fcc: [0; 4],
580 keyframes: Vec::new(),
581 chunk_sizes: Vec::new(),
582 chunk_offsets: Vec::new(),
583 sample_map: Vec::new(),
584 sample_size: 0,
585 stream: None,
586 depth: 0,
587 cur_chunk: 0,
588 cur_sample: 0,
589 samples_left: 0,
590 last_offset: 0,
591 }
592 }
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;
600 }
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 });
608 }
609 }
610 }
611 fn calculate_chunk_size(&self, nsamp: usize) -> usize {
612 if nsamp == 0 {
613 self.sample_size as usize
614 } else {
615 match &self.fcc {
616 b"NONE" | b"raw " | b"twos" | b"sowt" => {
617 (nsamp * self.bits * self.channels + 7) >> 3
618 },
619 b"ima4" => {
620 let nblocks = (nsamp + 63) >> 6;
621 nblocks * 34 * self.channels
622 },
623 b"MAC3" => {
624 (nsamp + 5) / 6 * 2 * self.channels
625 },
626 b"MAC6" => {
627 (nsamp + 5) / 6 * self.channels
628 },
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
635 },
636 b"ms\x00\x21" => { //IMA ADPCM
637 (nsamp / 2 + 4) * self.channels
638 },
639 _ => self.sample_size as usize,
640 }
641 }
642 }
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);
645 //todo dts decoding
646 if self.chunk_offsets.len() == self.chunk_sizes.len() { // simple one-to-one mapping
647 if self.cur_sample >= self.chunk_sizes.len() {
648 return None;
649 }
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))
654 } else {
655 if self.samples_left == 0 {
656 if self.cur_chunk >= self.chunk_offsets.len() {
657 return None;
658 }
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;
662 } else {
663 break;
664 }
665 }
666 self.last_offset = self.chunk_offsets[self.cur_chunk];
667 self.cur_chunk += 1;
668 }
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;
674 } else {
675 self.samples_left = 0;
676 }
677 self.cur_sample += 1;
678 Some((pts, offset, size))
679 }
680 }
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 {
685 let mut nsamp = 0;
686 for (idx, samples) in self.sample_map.iter() {
687 if *idx as usize <= self.cur_chunk {
688 nsamp = *samples;
689 } else {
690 break;
691 }
692 }
693 self.calculate_chunk_size(nsamp as usize)
694 } else {
695 self.sample_size as usize
696 }
697 }
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{
704 let mut csamp = 0;
705 self.cur_chunk = 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();
709 loop {
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() {
714 next_idx = *new_idx;
715 next_samples = *new_samples;
716 }
717 }
718 csamp += cur_samps;
719 if csamp >= self.cur_sample {
720 self.last_offset = self.chunk_offsets[self.cur_chunk];
721 break;
722 }
723 self.cur_chunk += 1;
724 }
725 csamp -= cur_samps;
726 for sample_no in csamp..self.cur_chunk {
727 self.last_offset += self.get_size(sample_no) as u64;
728 }
729 self.samples_left = self.cur_sample - csamp - cur_samps;
730 }
731 }
732 }
733
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);
741 }
742 self.src.seek(SeekFrom::Start(self.mdat_pos))?;
743 self.cur_track = 0;
744 Ok(())
745 }
746
747 fn get_frame(&mut self, strmgr: &mut StreamManager) -> DemuxerResult<NAPacket> {
748 if self.tracks.len() == 0 {
749 return Err(DemuxerError::EOF);
750 }
751 for _ in 0..self.tracks.len() {
752 if self.cur_track >= self.tracks.len() {
753 self.cur_track = 0;
754 }
755 let track = &mut self.tracks[self.cur_track];
756 self.cur_track += 1;
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)?;
763 return Ok(pkt);
764 }
765 }
766 return Err(DemuxerError::EOF);
767 }
768
769 fn seek(&mut self, time: u64, seek_index: &SeekIndex) -> DemuxerResult<()> {
770 let ret = seek_index.find_pos(time);
771 if ret.is_none() {
772 return Err(DemuxerError::SeekError);
773 }
774 let seek_info = ret.unwrap();
775 for track in self.tracks.iter_mut() {
776 track.seek(seek_info.pts);
777 }
778 Ok(())
779 }
780 }
781
782 impl<'a> MOVDemuxer<'a> {
783 fn new(io: &'a mut ByteReader<'a>) -> Self {
784 MOVDemuxer {
785 src: io,
786 depth: 0,
787 mdat_pos: 0,
788 mdat_size: 0,
789 tracks: Vec::with_capacity(2),
790 cur_track: 0,
791 tb_den: 0,
792 duration: 0,
793 }
794 }
795 fn read_root(&mut self, strmgr: &mut StreamManager) -> DemuxerResult<()> {
796 self.depth = 0;
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)?;
803 continue;
804 }
805 let handler = ROOT_CHUNK_HANDLERS.iter().find(|x| x.ctype == ctype);
806 let read_size;
807 if let Some(ref handler) = handler {
808 read_size = (handler.parse)(self, strmgr, size)?;
809 } else {
810 println!("skipping unknown chunk {:08X} size {}", ctype, size);
811 read_size = 0;
812 }
813 validate!(read_size <= size);
814 self.src.skip64(size - read_size)?;
815 }
816 //todo check if all needed chunks are found
817 Ok(())
818 }
819 read_chunk_list!(root; "moov", read_moov, MOOV_CHUNK_HANDLERS);
820 }
821
822 pub struct MOVDemuxerCreator { }
823
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))
827 }
828 fn get_name(&self) -> &'static str { "mov" }
829 }
830
831 #[cfg(test)]
832 mod test {
833 use super::*;
834 use std::fs::File;
835
836 #[test]
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();
845
846 loop {
847 let pktres = dmx.get_frame(&mut sm);
848 if let Err(e) = pktres {
849 if e == DemuxerError::EOF { break; }
850 panic!("error");
851 }
852 let pkt = pktres.unwrap();
853 println!("Got {}", pkt);
854 }
855 }
856 }