]> git.nihav.org Git - nihav.git/blob - demuxers/avi.rs
add MPEG-4 ASP decoder
[nihav.git] / demuxers / avi.rs
1 use nihav_core::demuxers::*;
2 use nihav_registry::register;
3 use nihav_core::demuxers::DemuxerError::*;
4
5 macro_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
14 struct StreamState {
15 strm_no: u8,
16 got_strf: bool,
17 strm_type: Option<StreamType>,
18 }
19
20 impl StreamState {
21 fn new() -> Self {
22 StreamState { strm_no: 0, got_strf: true, strm_type: None }
23 }
24 fn reset(&mut self) {
25 self.strm_type = None;
26 self.got_strf = true;
27 self.strm_no += 1;
28 }
29 fn valid_state(&self) -> bool {
30 match self.strm_type {
31 None => self.got_strf,
32 _ => false,
33 }
34 }
35 }
36
37 #[allow(dead_code)]
38 struct AVIDemuxer<'a> {
39 src: &'a mut ByteReader<'a>,
40 cur_frame: Vec<u64>,
41 num_streams: u8,
42 size: usize,
43 movi_size: usize,
44 movi_pos: u64,
45 movi_orig: usize,
46 sstate: StreamState,
47 tb_num: u32,
48 tb_den: u32,
49 }
50
51 #[derive(Debug,Clone,Copy,PartialEq)]
52 enum RIFFTag {
53 Chunk(u32),
54 List(u32,u32),
55 }
56
57 struct RIFFParser {
58 tag: RIFFTag,
59 parse: fn(&mut AVIDemuxer, strmgr: &mut StreamManager, size: usize) -> DemuxerResult<usize>,
60 }
61
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)?;
65 Ok(())
66 }
67
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];
71 loop {
72 if (self.src.tell() & 1) == 1 {
73 self.src.read_skip(1)?;
74 self.movi_size -= 1;
75 if self.movi_size == 0 { return Err(EOF); }
76 }
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); }
83 continue;
84 }
85 if mktag!(tag) == mktag!(b"LIST") {
86 self.movi_size -= 12;
87 self.src.read_skip(4)?;
88 if self.movi_size == 0 { return Err(EOF); }
89 continue;
90 }
91 if tag[0] == b'i' && tag[1] == b'x' {
92 return Err(EOF);
93 }
94 if tag[0] < b'0' || tag[0] > b'9' || tag[1] < b'0' || tag[1] > b'9' {
95 return Err(InvalidData);
96 }
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();
101 if size == 0 {
102 self.movi_size -= 8;
103 if self.movi_size == 0 { return Err(EOF); }
104 continue;
105 }
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;
111
112 return Ok(pkt);
113 }
114 }
115
116 fn seek(&mut self, time: u64, seek_index: &SeekIndex) -> DemuxerResult<()> {
117 let ret = seek_index.find_pos(time);
118 if ret.is_none() {
119 return Err(DemuxerError::SeekError);
120 }
121 let seek_info = ret.unwrap();
122
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;
127
128 self.cur_frame[seek_info.str_id as usize] = seek_info.pts;
129 self.src.seek(SeekFrom::Start(seek_info.pos))?;
130
131 Ok(())
132 }
133 }
134
135 impl<'a> AVIDemuxer<'a> {
136 fn new(io: &'a mut ByteReader<'a>) -> Self {
137 AVIDemuxer {
138 cur_frame: Vec::new(),
139 num_streams: 0,
140 src: io,
141 size: 0,
142 movi_size: 0,
143 movi_pos: 0,
144 movi_orig: 0,
145 sstate: StreamState::new(),
146 tb_num: 0,
147 tb_den: 0,
148 }
149 }
150
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); }
154
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));
160 }
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));
165 }
166
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));
173 }
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); }
178 rest_size -= psize;
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); }
182 rest_size -= psize;
183 if ((psize & 1) == 1) && (rest_size > 0) {
184 rest_size -= 1;
185 }
186 }
187
188 return Ok((size + 8, false));
189 }
190 }
191 if !is_list {
192 self.src.read_skip(size)?;
193 } else {
194 if size < 4 { return Err(InvalidData); }
195 self.src.read_skip(size - 4)?;
196 }
197 if (size & 1) == 1 { self.src.read_skip(1)?; }
198 Ok((size + 8, false))
199 }
200
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 {
208 matches = true;
209 break;
210 }
211 }
212 if !matches {
213 return Err(InvalidData);
214 }
215 self.size = size;
216 let mut rest_size = size;
217 loop {
218 let (csz, end) = self.parse_chunk(strmgr, RIFFTag::List(mktag!(b"LIST"), mktag!(b"movi")), rest_size,0)?;
219 if end {
220 self.movi_size = csz - 4;
221 self.movi_orig = self.movi_size;
222 self.movi_pos = self.src.tell();
223 break;
224 }
225 rest_size -= csz;
226 }
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();
233 if end {
234 let _res = parse_idx1(&mut self.src, strmgr, seek_idx, csz, self.movi_pos);
235 break;
236 }
237 rest_size -= csz;
238 }
239 }
240 if self.movi_pos != 0 {
241 self.src.seek(SeekFrom::Start(self.movi_pos))?;
242 } else {
243 return Err(InvalidData);
244 }
245 if !self.sstate.valid_state() || self.sstate.strm_no != self.num_streams {
246 return Err(InvalidData);
247 }
248 Ok(())
249 }
250
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)?;
255 Ok(Some(edvec))
256 }
257 }
258
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") ],
263 ];
264
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 },
273 ];
274
275 fn is_list_tag(tag: u32) -> bool {
276 for chunk in CHUNKS.iter() {
277 if let RIFFTag::List(ltag, _) = chunk.tag {
278 if tag == ltag {
279 return true;
280 }
281 }
282 }
283 false
284 }
285
286 #[allow(unused_variables)]
287 fn parse_hdrl(dmx: &mut AVIDemuxer, strmgr: &mut StreamManager, size: usize) -> DemuxerResult<usize> {
288 Ok(0)
289 }
290
291 #[allow(unused_variables)]
292 fn parse_strl(dmx: &mut AVIDemuxer, strmgr: &mut StreamManager, size: usize) -> DemuxerResult<usize> {
293 Ok(0)
294 }
295
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()?;
316
317 dmx.src.read_skip(size - 0x38)?;
318
319 if !dmx.sstate.valid_state() || dmx.sstate.strm_no >= dmx.num_streams {
320 return Err(InvalidData);
321 }
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);
326 } else {
327 dmx.sstate.strm_type = Some(StreamType::Data);
328 }
329 dmx.sstate.got_strf = false;
330
331 Ok(size)
332 }
333
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),
340 }
341 }
342
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()?;
359
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) {
366 None => "unknown",
367 Some(name) => name,
368 };
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); }
372 dmx.sstate.reset();
373 Ok(size)
374 }
375
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()?;
385
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) {
390 None => "unknown",
391 Some(name) => name,
392 };
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); }
396 dmx.sstate.reset();
397 Ok(size)
398 }
399
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); }
405 dmx.sstate.reset();
406 Ok(size)
407 }
408
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;
421
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
426
427 dmx.cur_frame.resize(streams as usize, 0);
428 dmx.src.read_skip(size - 0x38)?;
429 Ok(size)
430 }
431
432 #[allow(unused_variables)]
433 fn parse_junk(dmx: &mut AVIDemuxer, strmgr: &mut StreamManager, size: usize) -> DemuxerResult<usize> {
434 dmx.src.read_skip(size)?;
435 Ok(size)
436 }
437
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()?;
448
449 if tag[0] < b'0' || tag[0] > b'9' || tag[1] < b'0' || tag[1] > b'9' {
450 return Err(InvalidData);
451 }
452 let stream_no = ((tag[0] - b'0') * 10 + (tag[1] - b'0')) as usize;
453
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 });
461 }
462 }
463 }
464 counter[stream_no] += 1;
465 }
466 Ok(size)
467 }
468
469 pub struct AVIDemuxerCreator { }
470
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))
474 }
475 fn get_name(&self) -> &'static str { "avi" }
476 }
477
478 #[cfg(test)]
479 mod test {
480 use super::*;
481 use std::fs::File;
482
483 #[test]
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();
492
493 loop {
494 let pktres = dmx.get_frame(&mut sm);
495 if let Err(e) = pktres {
496 if e == DemuxerError::EOF { break; }
497 panic!("error");
498 }
499 let pkt = pktres.unwrap();
500 println!("Got {}", pkt);
501 }
502 }
503 }