]> git.nihav.org Git - nihav.git/blame - nihav-commonfmt/src/demuxers/avi.rs
make muxers handle options
[nihav.git] / nihav-commonfmt / src / demuxers / avi.rs
CommitLineData
38953fb5 1use nihav_core::demuxers::*;
32f7cbe5 2use nihav_registry::register;
38953fb5 3use nihav_core::demuxers::DemuxerError::*;
ca41f125
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
14struct StreamState {
15 strm_no: u8,
16 got_strf: bool,
17 strm_type: Option<StreamType>,
18}
19
20impl 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
956ef3a0
KS
37struct PalInfo {
38 pal: Arc<[u8; 1024]>,
39 changed: bool,
40 stream_no: usize,
41}
42
ca41f125 43#[allow(dead_code)]
eb71d98f 44struct AVIDemuxer<'a> {
ca41f125
KS
45 src: &'a mut ByteReader<'a>,
46 cur_frame: Vec<u64>,
47 num_streams: u8,
ca41f125
KS
48 size: usize,
49 movi_size: usize,
33b5a8f0
KS
50 movi_pos: u64,
51 movi_orig: usize,
ca41f125 52 sstate: StreamState,
e189501e
KS
53 tb_num: u32,
54 tb_den: u32,
f39f0623 55 key_offs: Vec<u64>,
956ef3a0 56 pal: Vec<PalInfo>,
ca41f125
KS
57}
58
c84e0be0 59#[derive(Debug,Clone,Copy,PartialEq)]
ca41f125
KS
60enum RIFFTag {
61 Chunk(u32),
62 List(u32,u32),
63}
64
65struct RIFFParser {
66 tag: RIFFTag,
bcfeae48 67 parse: fn(&mut AVIDemuxer, strmgr: &mut StreamManager, size: usize) -> DemuxerResult<usize>,
ca41f125
KS
68}
69
bcfeae48 70impl<'a> DemuxCore<'a> for AVIDemuxer<'a> {
33b5a8f0 71 fn open(&mut self, strmgr: &mut StreamManager, seek_index: &mut SeekIndex) -> DemuxerResult<()> {
4a6263cf 72 self.read_header(strmgr, seek_index)?;
ca41f125
KS
73 Ok(())
74 }
75
bcfeae48 76 fn get_frame(&mut self, strmgr: &mut StreamManager) -> DemuxerResult<NAPacket> {
ca41f125
KS
77 if self.movi_size == 0 { return Err(EOF); }
78 let mut tag: [u8; 4] = [0; 4];
79 loop {
aa7dab8e
KS
80 if (self.src.tell() & 1) == 1 {
81 self.src.read_skip(1)?;
82 self.movi_size -= 1;
3ff4cebb 83 if self.movi_size == 0 { return Err(EOF); }
aa7dab8e 84 }
f39f0623 85 let is_keyframe = self.key_offs.binary_search(&self.src.tell()).is_ok();
ca41f125
KS
86 self.src.read_buf(&mut tag)?;
87 let size = self.src.read_u32le()? as usize;
88 if mktag!(tag) == mktag!(b"JUNK") {
89 self.movi_size -= size + 8;
90 self.src.read_skip(size)?;
cf00c65a 91 if self.movi_size == 0 { return Err(EOF); }
ca41f125
KS
92 continue;
93 }
70fc42e8
KS
94 if mktag!(tag) == mktag!(b"LIST") {
95 self.movi_size -= 12;
96 self.src.read_skip(4)?;
97 if self.movi_size == 0 { return Err(EOF); }
98 continue;
99 }
30d39a7a 100 if (tag[0] == b'i' && tag[1] == b'x') || (&tag == b"idx1") {
6350b1ee
KS
101 return Err(EOF);
102 }
ca41f125
KS
103 if tag[0] < b'0' || tag[0] > b'9' || tag[1] < b'0' || tag[1] > b'9' {
104 return Err(InvalidData);
105 }
106 let stream_no = (tag[0] - b'0') * 10 + (tag[1] - b'0');
956ef3a0
KS
107 if tag[2] == b'p' && tag[3] == b'c' {
108 self.parse_palette_change(stream_no as usize, size)?;
109 self.movi_size -= size;
110 if self.movi_size == 0 { return Err(EOF); }
111 continue;
112 }
bcfeae48 113 let str = strmgr.get_stream(stream_no as usize);
c83013a1 114 if str.is_none() { return Err(InvalidData); }
ca41f125 115 let stream = str.unwrap();
741d97d8
KS
116 if size == 0 {
117 self.movi_size -= 8;
118 if self.movi_size == 0 { return Err(EOF); }
119 continue;
120 }
e189501e
KS
121 let (tb_num, tb_den) = stream.get_timebase();
122 let ts = NATimeInfo::new(Some(self.cur_frame[stream_no as usize]), None, None, tb_num, tb_den);
956ef3a0
KS
123 let mut pkt = self.src.read_packet(stream, ts, is_keyframe, size)?;
124 for pe in self.pal.iter_mut() {
125 if pe.stream_no == (stream_no as usize) {
126 pkt.add_side_data(NASideData::Palette(pe.changed, pe.pal.clone()));
127 pe.changed = false;
128 break;
129 }
130 }
1a151e53 131 self.cur_frame[stream_no as usize] += 1;
ca41f125
KS
132 self.movi_size -= size + 8;
133
134 return Ok(pkt);
135 }
136 }
137
33b5a8f0
KS
138 fn seek(&mut self, time: u64, seek_index: &SeekIndex) -> DemuxerResult<()> {
139 let ret = seek_index.find_pos(time);
140 if ret.is_none() {
141 return Err(DemuxerError::SeekError);
142 }
4a6263cf 143 let seek_info = ret.unwrap();
33b5a8f0 144
4a6263cf
KS
145 if seek_info.pos < self.movi_pos { return Err(DemuxerError::SeekError); }
146 let skip_size = (seek_info.pos - self.movi_pos) as usize;
33b5a8f0
KS
147 if skip_size > self.movi_size { return Err(DemuxerError::SeekError); }
148 self.movi_size = self.movi_orig - skip_size;
149
4a6263cf
KS
150 self.cur_frame[seek_info.str_id as usize] = seek_info.pts;
151 self.src.seek(SeekFrom::Start(seek_info.pos))?;
152
33b5a8f0 153 Ok(())
ca41f125
KS
154 }
155}
156
157impl<'a> AVIDemuxer<'a> {
eb71d98f 158 fn new(io: &'a mut ByteReader<'a>) -> Self {
ca41f125
KS
159 AVIDemuxer {
160 cur_frame: Vec::new(),
161 num_streams: 0,
ca41f125
KS
162 src: io,
163 size: 0,
164 movi_size: 0,
33b5a8f0
KS
165 movi_pos: 0,
166 movi_orig: 0,
ca41f125 167 sstate: StreamState::new(),
e189501e
KS
168 tb_num: 0,
169 tb_den: 0,
f39f0623 170 key_offs: Vec::new(),
956ef3a0 171 pal: Vec::new(),
ca41f125
KS
172 }
173 }
174
bcfeae48 175 fn parse_chunk(&mut self, strmgr: &mut StreamManager, end_tag: RIFFTag, csize: usize, depth: u16) -> DemuxerResult<(usize, bool)> {
ca41f125
KS
176 if csize < 8 { return Err(InvalidData); }
177 if depth > 42 { return Err(InvalidData); }
178
179 let tag = self.src.read_u32be()?;
180 let size = self.src.read_u32le()? as usize;
181 if size > csize { return Err(InvalidData); }
c84e0be0
KS
182 if RIFFTag::Chunk(tag) == end_tag {
183 return Ok((size, true));
ca41f125 184 }
f149a5b7
KS
185 let is_list = is_list_tag(tag);
186 let ltag = if is_list { self.src.read_u32be()? } else { 0 };
c84e0be0
KS
187 if RIFFTag::List(tag, ltag) == end_tag {
188 return Ok((size, true));
ca41f125 189 }
c84e0be0 190
c83013a1
KS
191 for chunk in CHUNKS.iter() {
192 if RIFFTag::Chunk(tag) == chunk.tag {
193 let psize = (chunk.parse)(self, strmgr, size)?;
c84e0be0 194 if psize != size { return Err(InvalidData); }
f6fa6f34 195 if (psize & 1) == 1 { self.src.read_skip(1)?; }
c84e0be0 196 return Ok((size + 8, false));
ca41f125 197 }
c83013a1 198 if RIFFTag::List(tag, ltag) == chunk.tag {
c84e0be0 199 let mut rest_size = size - 4;
c83013a1 200 let psize = (chunk.parse)(self, strmgr, rest_size)?;
c84e0be0
KS
201 if psize > rest_size { return Err(InvalidData); }
202 rest_size -= psize;
203 while rest_size > 0 {
bcfeae48 204 let (psize, _) = self.parse_chunk(strmgr, end_tag, rest_size, depth+1)?;
ca41f125
KS
205 if psize > rest_size { return Err(InvalidData); }
206 rest_size -= psize;
c83013a1
KS
207 if ((psize & 1) == 1) && (rest_size > 0) {
208 rest_size -= 1;
f6fa6f34 209 }
ca41f125 210 }
c84e0be0
KS
211
212 return Ok((size + 8, false));
ca41f125
KS
213 }
214 }
f149a5b7
KS
215 if !is_list {
216 self.src.read_skip(size)?;
217 } else {
218 if size < 4 { return Err(InvalidData); }
219 self.src.read_skip(size - 4)?;
220 }
f6fa6f34 221 if (size & 1) == 1 { self.src.read_skip(1)?; }
c83013a1 222 Ok((size + 8, false))
ca41f125
KS
223 }
224
4a6263cf 225 fn read_header(&mut self, strmgr: &mut StreamManager, seek_idx: &mut SeekIndex) -> DemuxerResult<()> {
ca41f125
KS
226 let riff_tag = self.src.read_u32be()?;
227 let size = self.src.read_u32le()? as usize;
228 let avi_tag = self.src.read_u32be()?;
23eeced9
KS
229 let mut matches = false;
230 for rt in RIFF_TAGS.iter() {
231 if rt[0] == riff_tag && rt[1] == avi_tag {
232 matches = true;
233 break;
234 }
235 }
236 if !matches {
ca41f125
KS
237 return Err(InvalidData);
238 }
239 self.size = size;
240 let mut rest_size = size;
241 loop {
bcfeae48 242 let (csz, end) = self.parse_chunk(strmgr, RIFFTag::List(mktag!(b"LIST"), mktag!(b"movi")), rest_size,0)?;
33b5a8f0
KS
243 if end {
244 self.movi_size = csz - 4;
245 self.movi_orig = self.movi_size;
246 self.movi_pos = self.src.tell();
247 break;
248 }
ca41f125
KS
249 rest_size -= csz;
250 }
9da33f04
KS
251 if !seek_idx.skip_index {
252 self.src.read_skip(self.movi_size)?;
253 while rest_size > 0 {
254 let ret = self.parse_chunk(strmgr, RIFFTag::Chunk(mktag!(b"idx1")), rest_size,0);
255 if ret.is_err() { break; }
256 let (csz, end) = ret.unwrap();
257 if end {
f39f0623 258 let _res = parse_idx1(&mut self.src, strmgr, seek_idx, csz, self.movi_pos, &mut self.key_offs);
9da33f04
KS
259 break;
260 }
261 rest_size -= csz;
4a6263cf 262 }
4a6263cf
KS
263 }
264 if self.movi_pos != 0 {
265 self.src.seek(SeekFrom::Start(self.movi_pos))?;
266 } else {
267 return Err(InvalidData);
268 }
ca41f125
KS
269 if !self.sstate.valid_state() || self.sstate.strm_no != self.num_streams {
270 return Err(InvalidData);
271 }
272 Ok(())
273 }
274
275 fn read_extradata(&mut self, size: usize) -> DemuxerResult<Option<Vec<u8>>> {
276 if size == 0 { return Ok(None); }
c83013a1 277 let mut edvec: Vec<u8> = vec![0; size];
ca41f125
KS
278 self.src.read_buf(&mut edvec)?;
279 Ok(Some(edvec))
280 }
956ef3a0
KS
281
282 fn parse_palette_change(&mut self, stream_no: usize, size: usize) -> DemuxerResult<()> {
283 for pe in self.pal.iter_mut() {
284 if pe.stream_no == stream_no {
285 let start_clr = self.src.read_byte()? as usize;
286 let len = self.src.read_byte()? as usize;
287 let _flags = self.src.read_u16le()?;
288 validate!(start_clr + len <= 256);
289 validate!(len * 4 + 4 == size);
290 let mut newpal = *pe.pal;
291 for i in start_clr..(start_clr + len) {
292 newpal[i * 4] = self.src.read_byte()?;
293 newpal[i * 4 + 1] = self.src.read_byte()?;
294 newpal[i * 4 + 2] = self.src.read_byte()?;
93c94df9
KS
295 newpal[i * 4 + 3] = 0;
296 self.src.read_byte()?; // flags
956ef3a0
KS
297 }
298 pe.pal = Arc::new(newpal);
299 pe.changed = true;
300 return Ok(());
301 }
302 }
303 self.src.read_skip(size)?;
304 Ok(())
305 }
ca41f125
KS
306}
307
23eeced9
KS
308const RIFF_TAGS: &[[u32; 2]] = &[
309 [ mktag!(b"RIFF"), mktag!(b"AVI ") ],
310 [ mktag!(b"RIFF"), mktag!(b"AVIX") ],
311 [ mktag!(b"ON2 "), mktag!(b"ON2f") ],
312];
313
314const CHUNKS: [RIFFParser; 7] = [
ca41f125
KS
315 RIFFParser { tag: RIFFTag::List(mktag!(b"LIST"), mktag!(b"hdrl")), parse: parse_hdrl },
316 RIFFParser { tag: RIFFTag::List(mktag!(b"LIST"), mktag!(b"strl")), parse: parse_strl },
317 RIFFParser { tag: RIFFTag::Chunk(mktag!(b"avih")), parse: parse_avih },
23eeced9 318 RIFFParser { tag: RIFFTag::Chunk(mktag!(b"ON2h")), parse: parse_avih },
ca41f125
KS
319 RIFFParser { tag: RIFFTag::Chunk(mktag!(b"strf")), parse: parse_strf },
320 RIFFParser { tag: RIFFTag::Chunk(mktag!(b"strh")), parse: parse_strh },
321 RIFFParser { tag: RIFFTag::Chunk(mktag!(b"JUNK")), parse: parse_junk },
322];
323
324fn is_list_tag(tag: u32) -> bool {
c83013a1
KS
325 for chunk in CHUNKS.iter() {
326 if let RIFFTag::List(ltag, _) = chunk.tag {
ca41f125
KS
327 if tag == ltag {
328 return true;
329 }
330 }
331 }
332 false
333}
334
335#[allow(unused_variables)]
bcfeae48 336fn parse_hdrl(dmx: &mut AVIDemuxer, strmgr: &mut StreamManager, size: usize) -> DemuxerResult<usize> {
ca41f125
KS
337 Ok(0)
338}
339
340#[allow(unused_variables)]
bcfeae48 341fn parse_strl(dmx: &mut AVIDemuxer, strmgr: &mut StreamManager, size: usize) -> DemuxerResult<usize> {
ca41f125
KS
342 Ok(0)
343}
344
345#[allow(unused_variables)]
bcfeae48 346fn parse_strh(dmx: &mut AVIDemuxer, strmgr: &mut StreamManager, size: usize) -> DemuxerResult<usize> {
ca41f125
KS
347 if size < 0x38 { return Err(InvalidData); }
348 let tag = dmx.src.read_u32be()?; //stream type
349 let fcc = dmx.src.read_u32be()?; //handler(fourcc)
350 dmx.src.read_u32le()?; //flags
351 dmx.src.read_skip(2)?; //priority
352 dmx.src.read_skip(2)?; //language
353 dmx.src.read_skip(4)?; //initial frames
e189501e
KS
354 dmx.tb_num = dmx.src.read_u32le()?; //scale
355 dmx.tb_den = dmx.src.read_u32le()?; //rate
ca41f125
KS
356 dmx.src.read_skip(4)?; //start
357 dmx.src.read_skip(4)?; //length
358 dmx.src.read_skip(4)?; //buf size
359 dmx.src.read_skip(4)?; //quality
360 dmx.src.read_skip(4)?; //sample size
361 let a = dmx.src.read_u16le()?;
362 let b = dmx.src.read_u16le()?;
363 let c = dmx.src.read_u16le()?;
364 let d = dmx.src.read_u16le()?;
365
366 dmx.src.read_skip(size - 0x38)?;
367
368 if !dmx.sstate.valid_state() || dmx.sstate.strm_no >= dmx.num_streams {
369 return Err(InvalidData);
370 }
371 if tag == mktag!(b"vids") {
372 dmx.sstate.strm_type = Some(StreamType::Video);
373 } else if tag == mktag!(b"auds") {
374 dmx.sstate.strm_type = Some(StreamType::Audio);
375 } else {
376 dmx.sstate.strm_type = Some(StreamType::Data);
377 }
378 dmx.sstate.got_strf = false;
379
380 Ok(size)
381}
382
bcfeae48 383fn parse_strf(dmx: &mut AVIDemuxer, strmgr: &mut StreamManager, size: usize) -> DemuxerResult<usize> {
c83013a1 384 if dmx.sstate.strm_type.is_none() { return Err(InvalidData); }
ca41f125 385 match dmx.sstate.strm_type.unwrap() {
bcfeae48
KS
386 StreamType::Video => parse_strf_vids(dmx, strmgr, size),
387 StreamType::Audio => parse_strf_auds(dmx, strmgr, size),
388 _ => parse_strf_xxxx(dmx, strmgr, size),
ca41f125
KS
389 }
390}
391
392#[allow(unused_variables)]
bcfeae48 393fn parse_strf_vids(dmx: &mut AVIDemuxer, strmgr: &mut StreamManager, size: usize) -> DemuxerResult<usize> {
ca41f125
KS
394 if size < 40 { return Err(InvalidData); }
395 let bi_size = dmx.src.read_u32le()?;
dbf1b2e1 396 if (bi_size as usize) < 40 { return Err(InvalidData); }
ca41f125
KS
397 let width = dmx.src.read_u32le()?;
398 let height = dmx.src.read_u32le()? as i32;
399 let planes = dmx.src.read_u16le()?;
400 let bitcount = dmx.src.read_u16le()?;
ccae5343
KS
401 let mut compression: [u8; 4] = [0; 4];
402 dmx.src.read_buf(&mut compression)?;
ca41f125
KS
403 let img_size = dmx.src.read_u32le()?;
404 let xdpi = dmx.src.read_u32le()?;
405 let ydpi = dmx.src.read_u32le()?;
406 let colors = dmx.src.read_u32le()?;
956ef3a0 407 validate!(colors <= 256);
ca41f125
KS
408 let imp_colors = dmx.src.read_u32le()?;
409
410 let flip = height < 0;
411 let format = if bitcount > 8 { RGB24_FORMAT } else { PAL8_FORMAT };
045d52bd 412 let vhdr = NAVideoInfo::new(width as usize, if flip { -height as usize } else { height as usize}, flip, format);
ca41f125
KS
413 let vci = NACodecTypeInfo::Video(vhdr);
414 let edata = dmx.read_extradata(size - 40)?;
956ef3a0
KS
415 if colors > 0 {
416 if let Some(ref buf) = edata {
417 let mut pal = [0u8; 1024];
418 for (dpal, spal) in pal.chunks_mut(4).take(colors as usize).zip(buf.chunks(4)) {
93c94df9 419 dpal[0] = spal[2];
956ef3a0 420 dpal[1] = spal[1];
93c94df9
KS
421 dpal[2] = spal[0];
422 dpal[3] = 0;
956ef3a0
KS
423 }
424 let pal = PalInfo { pal: Arc::new(pal), changed: true, stream_no: strmgr.get_num_streams() };
425 dmx.pal.push(pal);
426 }
427 }
ccae5343
KS
428 let cname = match register::find_codec_from_avi_fourcc(&compression) {
429 None => "unknown",
430 Some(name) => name,
431 };
432 let vinfo = NACodecInfo::new(cname, vci, edata);
c83013a1
KS
433 let res = strmgr.add_stream(NAStream::new(StreamType::Video, u32::from(dmx.sstate.strm_no), vinfo, dmx.tb_num, dmx.tb_den));
434 if res.is_none() { return Err(MemoryError); }
ca41f125
KS
435 dmx.sstate.reset();
436 Ok(size)
437}
438
439#[allow(unused_variables)]
bcfeae48 440fn parse_strf_auds(dmx: &mut AVIDemuxer, strmgr: &mut StreamManager, size: usize) -> DemuxerResult<usize> {
ca41f125
KS
441 if size < 16 { return Err(InvalidData); }
442 let w_format_tag = dmx.src.read_u16le()?;
443 let channels = dmx.src.read_u16le()?;
444 let samplespersec = dmx.src.read_u32le()?;
445 let avgbytespersec = dmx.src.read_u32le()?;
446 let block_align = dmx.src.read_u16le()?;
b33d38e0 447 let bits_per_sample = dmx.src.read_u16le()?;
ca41f125
KS
448
449 let soniton = NASoniton::new(bits_per_sample as u8, SONITON_FLAG_SIGNED);
450 let ahdr = NAAudioInfo::new(samplespersec, channels as u8, soniton, block_align as usize);
f8d69720
KS
451 let edata;
452 if size > 16 {
453 let edata_size = dmx.src.read_u16le()? as usize;
454 validate!(edata_size + 18 == size);
455 edata = dmx.read_extradata(size - 18)?;
456 } else {
457 edata = None;
458 }
ccae5343
KS
459 let cname = match register::find_codec_from_wav_twocc(w_format_tag) {
460 None => "unknown",
461 Some(name) => name,
462 };
463 let ainfo = NACodecInfo::new(cname, NACodecTypeInfo::Audio(ahdr), edata);
c83013a1
KS
464 let res = strmgr.add_stream(NAStream::new(StreamType::Audio, u32::from(dmx.sstate.strm_no), ainfo, dmx.tb_num, dmx.tb_den));
465 if res.is_none() { return Err(MemoryError); }
ca41f125
KS
466 dmx.sstate.reset();
467 Ok(size)
468}
469
bcfeae48 470fn parse_strf_xxxx(dmx: &mut AVIDemuxer, strmgr: &mut StreamManager, size: usize) -> DemuxerResult<usize> {
ca41f125 471 let edata = dmx.read_extradata(size)?;
ccae5343 472 let info = NACodecInfo::new("unknown", NACodecTypeInfo::None, edata);
c83013a1
KS
473 let res = strmgr.add_stream(NAStream::new(StreamType::Data, u32::from(dmx.sstate.strm_no), info, dmx.tb_num, dmx.tb_den));
474 if res.is_none() { return Err(MemoryError); }
ca41f125
KS
475 dmx.sstate.reset();
476 Ok(size)
477}
478
479#[allow(unused_variables)]
bcfeae48 480fn parse_avih(dmx: &mut AVIDemuxer, strmgr: &mut StreamManager, size: usize) -> DemuxerResult<usize> {
ca41f125
KS
481 if size < 0x38 { return Err(InvalidData); }
482 let timebase = dmx.src.read_u32le()?; //microsec per frame
483 dmx.src.read_skip(4)?; //max frame size
484 dmx.src.read_skip(4)?; //padding
485 dmx.src.read_u32le()?; //flags
486 let frames = dmx.src.read_u32le()?; //frames
487 dmx.src.read_skip(4)?; //initial frames
488 let streams = dmx.src.read_u32le()?; //streams
489 if streams > 100 { return Err(InvalidData); }
490 dmx.num_streams = streams as u8;
491
492 dmx.src.read_skip(4)?; //buf size
493 let width = dmx.src.read_u32le()?; //width
494 let height = dmx.src.read_u32le()? as i32; //height
495 dmx.src.read_skip(16)?; //reserved
496
497 dmx.cur_frame.resize(streams as usize, 0);
498 dmx.src.read_skip(size - 0x38)?;
499 Ok(size)
500}
501
bcfeae48
KS
502#[allow(unused_variables)]
503fn parse_junk(dmx: &mut AVIDemuxer, strmgr: &mut StreamManager, size: usize) -> DemuxerResult<usize> {
ca41f125
KS
504 dmx.src.read_skip(size)?;
505 Ok(size)
506}
507
f39f0623 508fn parse_idx1(src: &mut ByteReader, strmgr: &mut StreamManager, seek_idx: &mut SeekIndex, size: usize, movi_pos: u64, key_offs: &mut Vec<u64>) -> DemuxerResult<usize> {
4a6263cf
KS
509 validate!((size & 15) == 0);
510 let mut tag = [0u8; 4];
511 let num_entries = size >> 4;
512 let mut counter = [0u64; 100];
513 for _ in 0..num_entries {
514 src.read_buf(&mut tag)?;
515 let flags = src.read_u32le()?;
516 let offset = src.read_u32le()? as u64;
517 let _length = src.read_u32le()?;
518
519 if tag[0] < b'0' || tag[0] > b'9' || tag[1] < b'0' || tag[1] > b'9' {
520 return Err(InvalidData);
521 }
522 let stream_no = ((tag[0] - b'0') * 10 + (tag[1] - b'0')) as usize;
523
524 if (flags & 0x10) != 0 {
525 if let Some(str) = strmgr.get_stream(stream_no) {
526 if str.get_media_type() == StreamType::Video {
527 let (tb_num, tb_den) = str.get_timebase();
528 let pts = counter[stream_no];
529 let time = NATimeInfo::ts_to_time(pts, 1000, tb_num, tb_den);
530 seek_idx.add_entry(stream_no as u32, SeekEntry { time, pts, pos: offset + movi_pos - 4 });
531 }
f39f0623 532 key_offs.push(offset);
4a6263cf
KS
533 }
534 }
535 counter[stream_no] += 1;
536 }
f39f0623 537 key_offs.sort_unstable();
4a6263cf
KS
538 Ok(size)
539}
540
eb71d98f
KS
541pub struct AVIDemuxerCreator { }
542
543impl DemuxerCreator for AVIDemuxerCreator {
6011e201 544 fn new_demuxer<'a>(&self, br: &'a mut ByteReader<'a>) -> Box<dyn DemuxCore<'a> + 'a> {
eb71d98f
KS
545 Box::new(AVIDemuxer::new(br))
546 }
547 fn get_name(&self) -> &'static str { "avi" }
548}
549
ca41f125
KS
550#[cfg(test)]
551mod test {
552 use super::*;
553 use std::fs::File;
554
555 #[test]
556 fn test_avi_demux() {
1678d59a 557 let mut file = File::open("assets/Indeo/laser05.avi").unwrap();
ca41f125
KS
558 let mut fr = FileReader::new_read(&mut file);
559 let mut br = ByteReader::new(&mut fr);
560 let mut dmx = AVIDemuxer::new(&mut br);
bcfeae48 561 let mut sm = StreamManager::new();
caf0f37e
KS
562 let mut si = SeekIndex::new();
563 dmx.open(&mut sm, &mut si).unwrap();
ca41f125
KS
564
565 loop {
bcfeae48 566 let pktres = dmx.get_frame(&mut sm);
ca41f125 567 if let Err(e) = pktres {
b1be9318 568 if e == DemuxerError::EOF { break; }
ca41f125
KS
569 panic!("error");
570 }
571 let pkt = pktres.unwrap();
572 println!("Got {}", pkt);
573 }
574 }
575}