1 use nihav_core::demuxers::*;
2 use nihav_registry::register;
3 use nihav_core::demuxers::DemuxerError::*;
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)
10 (($arr[0] as u32) << 24) | (($arr[1] as u32) << 16) | (($arr[2] as u32) << 8) | ($arr[3] as u32)
17 strm_type: Option<StreamType>,
22 StreamState { strm_no: 0, got_strf: true, strm_type: None }
25 self.strm_type = None;
29 fn valid_state(&self) -> bool {
30 match self.strm_type {
31 None => self.got_strf,
38 struct AVIDemuxer<'a> {
39 src: &'a mut ByteReader<'a>,
51 #[derive(Debug,Clone,Copy,PartialEq)]
59 parse: fn(&mut AVIDemuxer, strmgr: &mut StreamManager, size: usize) -> DemuxerResult<usize>,
62 impl<'a> DemuxCore<'a> for AVIDemuxer<'a> {
63 fn open(&mut self, strmgr: &mut StreamManager, seek_index: &mut SeekIndex) -> DemuxerResult<()> {
64 self.read_header(strmgr, seek_index)?;
68 fn get_frame(&mut self, strmgr: &mut StreamManager) -> DemuxerResult<NAPacket> {
69 if self.movi_size == 0 { return Err(EOF); }
70 let mut tag: [u8; 4] = [0; 4];
72 if (self.src.tell() & 1) == 1 {
73 self.src.read_skip(1)?;
75 if self.movi_size == 0 { return Err(EOF); }
77 self.src.read_buf(&mut tag)?;
78 let size = self.src.read_u32le()? as usize;
79 if mktag!(tag) == mktag!(b"JUNK") {
80 self.movi_size -= size + 8;
81 self.src.read_skip(size)?;
82 if self.movi_size == 0 { return Err(EOF); }
85 if mktag!(tag) == mktag!(b"LIST") {
87 self.src.read_skip(4)?;
88 if self.movi_size == 0 { return Err(EOF); }
91 if tag[0] == b'i' && tag[1] == b'x' {
94 if tag[0] < b'0' || tag[0] > b'9' || tag[1] < b'0' || tag[1] > b'9' {
95 return Err(InvalidData);
97 let stream_no = (tag[0] - b'0') * 10 + (tag[1] - b'0');
98 let str = strmgr.get_stream(stream_no as usize);
99 if str.is_none() { return Err(InvalidData); }
100 let stream = str.unwrap();
103 if self.movi_size == 0 { return Err(EOF); }
106 let (tb_num, tb_den) = stream.get_timebase();
107 let ts = NATimeInfo::new(Some(self.cur_frame[stream_no as usize]), None, None, tb_num, tb_den);
108 let pkt = self.src.read_packet(stream, ts, false, size)?;
109 self.cur_frame[stream_no as usize] += 1;
110 self.movi_size -= size + 8;
116 fn seek(&mut self, time: u64, seek_index: &SeekIndex) -> DemuxerResult<()> {
117 let ret = seek_index.find_pos(time);
119 return Err(DemuxerError::SeekError);
121 let seek_info = ret.unwrap();
123 if seek_info.pos < self.movi_pos { return Err(DemuxerError::SeekError); }
124 let skip_size = (seek_info.pos - self.movi_pos) as usize;
125 if skip_size > self.movi_size { return Err(DemuxerError::SeekError); }
126 self.movi_size = self.movi_orig - skip_size;
128 self.cur_frame[seek_info.str_id as usize] = seek_info.pts;
129 self.src.seek(SeekFrom::Start(seek_info.pos))?;
135 impl<'a> AVIDemuxer<'a> {
136 fn new(io: &'a mut ByteReader<'a>) -> Self {
138 cur_frame: Vec::new(),
145 sstate: StreamState::new(),
151 fn parse_chunk(&mut self, strmgr: &mut StreamManager, end_tag: RIFFTag, csize: usize, depth: u16) -> DemuxerResult<(usize, bool)> {
152 if csize < 8 { return Err(InvalidData); }
153 if depth > 42 { return Err(InvalidData); }
155 let tag = self.src.read_u32be()?;
156 let size = self.src.read_u32le()? as usize;
157 if size > csize { return Err(InvalidData); }
158 if RIFFTag::Chunk(tag) == end_tag {
159 return Ok((size, true));
161 let is_list = is_list_tag(tag);
162 let ltag = if is_list { self.src.read_u32be()? } else { 0 };
163 if RIFFTag::List(tag, ltag) == end_tag {
164 return Ok((size, true));
167 for chunk in CHUNKS.iter() {
168 if RIFFTag::Chunk(tag) == chunk.tag {
169 let psize = (chunk.parse)(self, strmgr, size)?;
170 if psize != size { return Err(InvalidData); }
171 if (psize & 1) == 1 { self.src.read_skip(1)?; }
172 return Ok((size + 8, false));
174 if RIFFTag::List(tag, ltag) == chunk.tag {
175 let mut rest_size = size - 4;
176 let psize = (chunk.parse)(self, strmgr, rest_size)?;
177 if psize > rest_size { return Err(InvalidData); }
179 while rest_size > 0 {
180 let (psize, _) = self.parse_chunk(strmgr, end_tag, rest_size, depth+1)?;
181 if psize > rest_size { return Err(InvalidData); }
183 if ((psize & 1) == 1) && (rest_size > 0) {
188 return Ok((size + 8, false));
192 self.src.read_skip(size)?;
194 if size < 4 { return Err(InvalidData); }
195 self.src.read_skip(size - 4)?;
197 if (size & 1) == 1 { self.src.read_skip(1)?; }
198 Ok((size + 8, false))
201 fn read_header(&mut self, strmgr: &mut StreamManager, seek_idx: &mut SeekIndex) -> DemuxerResult<()> {
202 let riff_tag = self.src.read_u32be()?;
203 let size = self.src.read_u32le()? as usize;
204 let avi_tag = self.src.read_u32be()?;
205 let mut matches = false;
206 for rt in RIFF_TAGS.iter() {
207 if rt[0] == riff_tag && rt[1] == avi_tag {
213 return Err(InvalidData);
216 let mut rest_size = size;
218 let (csz, end) = self.parse_chunk(strmgr, RIFFTag::List(mktag!(b"LIST"), mktag!(b"movi")), rest_size,0)?;
220 self.movi_size = csz - 4;
221 self.movi_orig = self.movi_size;
222 self.movi_pos = self.src.tell();
227 if !seek_idx.skip_index {
228 self.src.read_skip(self.movi_size)?;
229 while rest_size > 0 {
230 let ret = self.parse_chunk(strmgr, RIFFTag::Chunk(mktag!(b"idx1")), rest_size,0);
231 if ret.is_err() { break; }
232 let (csz, end) = ret.unwrap();
234 let _res = parse_idx1(&mut self.src, strmgr, seek_idx, csz, self.movi_pos);
240 if self.movi_pos != 0 {
241 self.src.seek(SeekFrom::Start(self.movi_pos))?;
243 return Err(InvalidData);
245 if !self.sstate.valid_state() || self.sstate.strm_no != self.num_streams {
246 return Err(InvalidData);
251 fn read_extradata(&mut self, size: usize) -> DemuxerResult<Option<Vec<u8>>> {
252 if size == 0 { return Ok(None); }
253 let mut edvec: Vec<u8> = vec![0; size];
254 self.src.read_buf(&mut edvec)?;
259 const RIFF_TAGS: &[[u32; 2]] = &[
260 [ mktag!(b"RIFF"), mktag!(b"AVI ") ],
261 [ mktag!(b"RIFF"), mktag!(b"AVIX") ],
262 [ mktag!(b"ON2 "), mktag!(b"ON2f") ],
265 const CHUNKS: [RIFFParser; 7] = [
266 RIFFParser { tag: RIFFTag::List(mktag!(b"LIST"), mktag!(b"hdrl")), parse: parse_hdrl },
267 RIFFParser { tag: RIFFTag::List(mktag!(b"LIST"), mktag!(b"strl")), parse: parse_strl },
268 RIFFParser { tag: RIFFTag::Chunk(mktag!(b"avih")), parse: parse_avih },
269 RIFFParser { tag: RIFFTag::Chunk(mktag!(b"ON2h")), parse: parse_avih },
270 RIFFParser { tag: RIFFTag::Chunk(mktag!(b"strf")), parse: parse_strf },
271 RIFFParser { tag: RIFFTag::Chunk(mktag!(b"strh")), parse: parse_strh },
272 RIFFParser { tag: RIFFTag::Chunk(mktag!(b"JUNK")), parse: parse_junk },
275 fn is_list_tag(tag: u32) -> bool {
276 for chunk in CHUNKS.iter() {
277 if let RIFFTag::List(ltag, _) = chunk.tag {
286 #[allow(unused_variables)]
287 fn parse_hdrl(dmx: &mut AVIDemuxer, strmgr: &mut StreamManager, size: usize) -> DemuxerResult<usize> {
291 #[allow(unused_variables)]
292 fn parse_strl(dmx: &mut AVIDemuxer, strmgr: &mut StreamManager, size: usize) -> DemuxerResult<usize> {
296 #[allow(unused_variables)]
297 fn parse_strh(dmx: &mut AVIDemuxer, strmgr: &mut StreamManager, size: usize) -> DemuxerResult<usize> {
298 if size < 0x38 { return Err(InvalidData); }
299 let tag = dmx.src.read_u32be()?; //stream type
300 let fcc = dmx.src.read_u32be()?; //handler(fourcc)
301 dmx.src.read_u32le()?; //flags
302 dmx.src.read_skip(2)?; //priority
303 dmx.src.read_skip(2)?; //language
304 dmx.src.read_skip(4)?; //initial frames
305 dmx.tb_num = dmx.src.read_u32le()?; //scale
306 dmx.tb_den = dmx.src.read_u32le()?; //rate
307 dmx.src.read_skip(4)?; //start
308 dmx.src.read_skip(4)?; //length
309 dmx.src.read_skip(4)?; //buf size
310 dmx.src.read_skip(4)?; //quality
311 dmx.src.read_skip(4)?; //sample size
312 let a = dmx.src.read_u16le()?;
313 let b = dmx.src.read_u16le()?;
314 let c = dmx.src.read_u16le()?;
315 let d = dmx.src.read_u16le()?;
317 dmx.src.read_skip(size - 0x38)?;
319 if !dmx.sstate.valid_state() || dmx.sstate.strm_no >= dmx.num_streams {
320 return Err(InvalidData);
322 if tag == mktag!(b"vids") {
323 dmx.sstate.strm_type = Some(StreamType::Video);
324 } else if tag == mktag!(b"auds") {
325 dmx.sstate.strm_type = Some(StreamType::Audio);
327 dmx.sstate.strm_type = Some(StreamType::Data);
329 dmx.sstate.got_strf = false;
334 fn parse_strf(dmx: &mut AVIDemuxer, strmgr: &mut StreamManager, size: usize) -> DemuxerResult<usize> {
335 if dmx.sstate.strm_type.is_none() { return Err(InvalidData); }
336 match dmx.sstate.strm_type.unwrap() {
337 StreamType::Video => parse_strf_vids(dmx, strmgr, size),
338 StreamType::Audio => parse_strf_auds(dmx, strmgr, size),
339 _ => parse_strf_xxxx(dmx, strmgr, size),
343 #[allow(unused_variables)]
344 fn parse_strf_vids(dmx: &mut AVIDemuxer, strmgr: &mut StreamManager, size: usize) -> DemuxerResult<usize> {
345 if size < 40 { return Err(InvalidData); }
346 let bi_size = dmx.src.read_u32le()?;
347 if (bi_size as usize) > size { return Err(InvalidData); }
348 let width = dmx.src.read_u32le()?;
349 let height = dmx.src.read_u32le()? as i32;
350 let planes = dmx.src.read_u16le()?;
351 let bitcount = dmx.src.read_u16le()?;
352 let mut compression: [u8; 4] = [0; 4];
353 dmx.src.read_buf(&mut compression)?;
354 let img_size = dmx.src.read_u32le()?;
355 let xdpi = dmx.src.read_u32le()?;
356 let ydpi = dmx.src.read_u32le()?;
357 let colors = dmx.src.read_u32le()?;
358 let imp_colors = dmx.src.read_u32le()?;
360 let flip = height < 0;
361 let format = if bitcount > 8 { RGB24_FORMAT } else { PAL8_FORMAT };
362 let vhdr = NAVideoInfo::new(width as usize, if flip { -height as usize } else { height as usize}, flip, PAL8_FORMAT);
363 let vci = NACodecTypeInfo::Video(vhdr);
364 let edata = dmx.read_extradata(size - 40)?;
365 let cname = match register::find_codec_from_avi_fourcc(&compression) {
369 let vinfo = NACodecInfo::new(cname, vci, edata);
370 let res = strmgr.add_stream(NAStream::new(StreamType::Video, u32::from(dmx.sstate.strm_no), vinfo, dmx.tb_num, dmx.tb_den));
371 if res.is_none() { return Err(MemoryError); }
376 #[allow(unused_variables)]
377 fn parse_strf_auds(dmx: &mut AVIDemuxer, strmgr: &mut StreamManager, size: usize) -> DemuxerResult<usize> {
378 if size < 16 { return Err(InvalidData); }
379 let w_format_tag = dmx.src.read_u16le()?;
380 let channels = dmx.src.read_u16le()?;
381 let samplespersec = dmx.src.read_u32le()?;
382 let avgbytespersec = dmx.src.read_u32le()?;
383 let block_align = dmx.src.read_u16le()?;
384 let bits_per_sample = dmx.src.read_u16le()?;
386 let soniton = NASoniton::new(bits_per_sample as u8, SONITON_FLAG_SIGNED);
387 let ahdr = NAAudioInfo::new(samplespersec, channels as u8, soniton, block_align as usize);
388 let edata = dmx.read_extradata(size - 16)?;
389 let cname = match register::find_codec_from_wav_twocc(w_format_tag) {
393 let ainfo = NACodecInfo::new(cname, NACodecTypeInfo::Audio(ahdr), edata);
394 let res = strmgr.add_stream(NAStream::new(StreamType::Audio, u32::from(dmx.sstate.strm_no), ainfo, dmx.tb_num, dmx.tb_den));
395 if res.is_none() { return Err(MemoryError); }
400 fn parse_strf_xxxx(dmx: &mut AVIDemuxer, strmgr: &mut StreamManager, size: usize) -> DemuxerResult<usize> {
401 let edata = dmx.read_extradata(size)?;
402 let info = NACodecInfo::new("unknown", NACodecTypeInfo::None, edata);
403 let res = strmgr.add_stream(NAStream::new(StreamType::Data, u32::from(dmx.sstate.strm_no), info, dmx.tb_num, dmx.tb_den));
404 if res.is_none() { return Err(MemoryError); }
409 #[allow(unused_variables)]
410 fn parse_avih(dmx: &mut AVIDemuxer, strmgr: &mut StreamManager, size: usize) -> DemuxerResult<usize> {
411 if size < 0x38 { return Err(InvalidData); }
412 let timebase = dmx.src.read_u32le()?; //microsec per frame
413 dmx.src.read_skip(4)?; //max frame size
414 dmx.src.read_skip(4)?; //padding
415 dmx.src.read_u32le()?; //flags
416 let frames = dmx.src.read_u32le()?; //frames
417 dmx.src.read_skip(4)?; //initial frames
418 let streams = dmx.src.read_u32le()?; //streams
419 if streams > 100 { return Err(InvalidData); }
420 dmx.num_streams = streams as u8;
422 dmx.src.read_skip(4)?; //buf size
423 let width = dmx.src.read_u32le()?; //width
424 let height = dmx.src.read_u32le()? as i32; //height
425 dmx.src.read_skip(16)?; //reserved
427 dmx.cur_frame.resize(streams as usize, 0);
428 dmx.src.read_skip(size - 0x38)?;
432 #[allow(unused_variables)]
433 fn parse_junk(dmx: &mut AVIDemuxer, strmgr: &mut StreamManager, size: usize) -> DemuxerResult<usize> {
434 dmx.src.read_skip(size)?;
438 fn parse_idx1(src: &mut ByteReader, strmgr: &mut StreamManager, seek_idx: &mut SeekIndex, size: usize, movi_pos: u64) -> DemuxerResult<usize> {
439 validate!((size & 15) == 0);
440 let mut tag = [0u8; 4];
441 let num_entries = size >> 4;
442 let mut counter = [0u64; 100];
443 for _ in 0..num_entries {
444 src.read_buf(&mut tag)?;
445 let flags = src.read_u32le()?;
446 let offset = src.read_u32le()? as u64;
447 let _length = src.read_u32le()?;
449 if tag[0] < b'0' || tag[0] > b'9' || tag[1] < b'0' || tag[1] > b'9' {
450 return Err(InvalidData);
452 let stream_no = ((tag[0] - b'0') * 10 + (tag[1] - b'0')) as usize;
454 if (flags & 0x10) != 0 {
455 if let Some(str) = strmgr.get_stream(stream_no) {
456 if str.get_media_type() == StreamType::Video {
457 let (tb_num, tb_den) = str.get_timebase();
458 let pts = counter[stream_no];
459 let time = NATimeInfo::ts_to_time(pts, 1000, tb_num, tb_den);
460 seek_idx.add_entry(stream_no as u32, SeekEntry { time, pts, pos: offset + movi_pos - 4 });
464 counter[stream_no] += 1;
469 pub struct AVIDemuxerCreator { }
471 impl DemuxerCreator for AVIDemuxerCreator {
472 fn new_demuxer<'a>(&self, br: &'a mut ByteReader<'a>) -> Box<dyn DemuxCore<'a> + 'a> {
473 Box::new(AVIDemuxer::new(br))
475 fn get_name(&self) -> &'static str { "avi" }
484 fn test_avi_demux() {
485 let mut file = File::open("assets/Indeo/laser05.avi").unwrap();
486 let mut fr = FileReader::new_read(&mut file);
487 let mut br = ByteReader::new(&mut fr);
488 let mut dmx = AVIDemuxer::new(&mut br);
489 let mut sm = StreamManager::new();
490 let mut si = SeekIndex::new();
491 dmx.open(&mut sm, &mut si).unwrap();
494 let pktres = dmx.get_frame(&mut sm);
495 if let Err(e) = pktres {
496 if e == DemuxerError::EOF { break; }
499 let pkt = pktres.unwrap();
500 println!("Got {}", pkt);