avi: skip LISTs inside movi data
[nihav.git] / src / demuxers / gdv.rs
CommitLineData
5869fd63
KS
1use super::*;
2use io::byteio::*;
3use frame::*;
20ef4353 4use formats::*;
5869fd63
KS
5//use std::collections::HashMap;
6
7enum GDVState {
8 NewFrame,
9 AudioRead,
10}
11
12#[allow(dead_code)]
eb71d98f 13struct GremlinVideoDemuxer<'a> {
20ef4353
KS
14 opened: bool,
15 src: &'a mut ByteReader<'a>,
16 frames: u16,
17 cur_frame: u16,
18 asize: usize,
19 apacked: bool,
20 state: GDVState,
21 pktdta: Vec<u8>,
22 dmx: Demuxer,
23 a_id: Option<usize>,
24 v_id: Option<usize>,
5869fd63
KS
25}
26
20ef4353 27impl<'a> Demux<'a> for GremlinVideoDemuxer<'a> {
5869fd63
KS
28 #[allow(unused_variables)]
29 fn open(&mut self) -> DemuxerResult<()> {
30 let src = &mut self.src;
31 let magic = src.read_u32le()?;
32 if magic != 0x29111994 { return Err(DemuxerError::InvalidData); }
33 let id = src.read_u16le()?;
34 let frames = src.read_u16le()?;
35 let fps = src.read_u16le()?;
36 let aflags = src.read_u16le()?;
37 let rate = src.read_u16le()?;
38 let depth = src.read_u16le()?;
39 let max_fs = src.read_u16le()?;
40 src.read_skip(2)?;
41 let width = src.read_u16le()?;
42 let height = src.read_u16le()?;
5869fd63 43 if max_fs > 0 {
81b3165f
KS
44 let mut edata: Vec<u8> = Vec::with_capacity(768);
45 if depth == 1 {
46 edata.resize(768, 0);
47 src.read_buf(edata.as_mut_slice())?;
48 }
66116504 49 let vhdr = NAVideoInfo::new(width as usize, height as usize, false, PAL8_FORMAT);
5869fd63 50 let vci = NACodecTypeInfo::Video(vhdr);
81b3165f 51 let vinfo = NACodecInfo::new("gdv-video", vci, if edata.len() == 0 { None } else { Some(edata) });
e189501e 52 self.v_id = self.dmx.add_stream(NAStream::new(StreamType::Video, 0, vinfo, 1, fps as u32));
5869fd63
KS
53 }
54 if (aflags & 1) != 0 {
55 let channels = if (aflags & 2) != 0 { 2 } else { 1 };
81b3165f
KS
56 let packed = if (aflags & 8) != 0 { 1 } else { 0 };
57 let depth = if (aflags & 4) != 0 { 16 } else { 8 };
58
59 let ahdr = NAAudioInfo::new(rate as u32, channels as u8, if depth == 16 { SND_S16_FORMAT } else { SND_U8_FORMAT }, 2);
60 let ainfo = NACodecInfo::new(if packed != 0 { "gdv-audio" } else { "pcm" },
61 NACodecTypeInfo::Audio(ahdr), None);
e189501e 62 self.a_id = self.dmx.add_stream(NAStream::new(StreamType::Audio, 1, ainfo, 1, rate as u32));
5869fd63 63
81b3165f 64 self.asize = (((rate / fps) * channels * (depth / 8)) >> packed) as usize;
5869fd63 65 self.apacked = (aflags & 8) != 0;
5869fd63 66 }
5869fd63
KS
67 self.frames = frames;
68 self.opened = true;
69 self.state = GDVState::NewFrame;
70 Ok(())
71 }
72
73 #[allow(unused_variables)]
74 fn get_frame(&mut self) -> DemuxerResult<NAPacket> {
75 if !self.opened { return Err(DemuxerError::NoSuchInput); }
76 if self.cur_frame >= self.frames { return Err(DemuxerError::EOF); }
77 match self.state {
78 GDVState::NewFrame if self.asize > 0 => { self.read_achunk() }
79 _ => { self.read_vchunk() }
80 }
81 }
82
66116504
KS
83 fn get_num_streams(&self) -> usize { self.dmx.get_num_streams() }
84 fn get_stream(&self, idx: usize) -> Option<Rc<NAStream>> { self.dmx.get_stream(idx) }
85
5869fd63
KS
86 #[allow(unused_variables)]
87 fn seek(&mut self, time: u64) -> DemuxerResult<()> {
88 if !self.opened { return Err(DemuxerError::NoSuchInput); }
89 Err(DemuxerError::NotImplemented)
90 }
91}
92/*impl<'a> Drop for GremlinVideoDemuxer<'a> {
93 #[allow(unused_variables)]
94 fn drop(&mut self) {
95 }
96}*/
97impl<'a> GremlinVideoDemuxer<'a> {
eb71d98f 98 fn new(io: &'a mut ByteReader<'a>) -> Self {
5869fd63
KS
99 GremlinVideoDemuxer {
100 cur_frame: 0,
101 frames: 0,
102 opened: false,
103 asize: 0,
104 apacked: false,
105 state: GDVState::NewFrame,
106pktdta: Vec::new(),
107 src: io,
20ef4353
KS
108 a_id: None,
109 v_id: None,
110 dmx: Demuxer::new()
5869fd63
KS
111 }
112 }
113
5869fd63
KS
114 fn read_achunk(&mut self) -> DemuxerResult<NAPacket> {
115 self.state = GDVState::AudioRead;
20ef4353 116 let str = self.dmx.get_stream(self.a_id.unwrap()).unwrap();
e189501e
KS
117 let (tb_num, tb_den) = str.get_timebase();
118 let ts = NATimeInfo::new(Some(self.cur_frame as u64), None, None, tb_num, tb_den);
119 self.src.read_packet(str, ts, true, self.asize)
5869fd63
KS
120 }
121
122 fn read_vchunk(&mut self) -> DemuxerResult<NAPacket> {
20ef4353 123 let str = self.dmx.get_stream(self.v_id.unwrap()).unwrap();
5869fd63
KS
124 let mut src = &mut self.src;
125 let magic = src.read_u16be()?;
126 if magic != 0x0513 { return Err(DemuxerError::InvalidData); }
127 let size = (src.read_u16le()? as usize) + 4;
e24794ee 128 let tmp = src.peek_u32le()?;
5869fd63 129 let flags = (tmp & 0xFF) as usize;
5869fd63
KS
130 self.state = GDVState::NewFrame;
131 self.cur_frame = self.cur_frame + 1;
e189501e
KS
132 let (tb_num, tb_den) = str.get_timebase();
133 let ts = NATimeInfo::new(Some((self.cur_frame - 1) as u64), None, None, tb_num, tb_den);
134 src.read_packet(str, ts, if (flags & 64) != 0 { true } else { false }, size)
5869fd63
KS
135 }
136}
137
eb71d98f
KS
138pub struct GDVDemuxerCreator { }
139
140impl DemuxerCreator for GDVDemuxerCreator {
141 fn new_demuxer<'a>(&self, br: &'a mut ByteReader<'a>) -> Box<Demux<'a> + 'a> {
142 Box::new(GremlinVideoDemuxer::new(br))
143 }
144 fn get_name(&self) -> &'static str { "gdv" }
145}
146
5869fd63
KS
147#[cfg(test)]
148mod test {
149 use super::*;
150 use std::fs::File;
151
152 #[test]
153 fn test_gdv_demux() {
154 let mut file = File::open("assets/intro1.gdv").unwrap();
155 let mut fr = FileReader::new_read(&mut file);
156 let mut br = ByteReader::new(&mut fr);
157 let mut dmx = GremlinVideoDemuxer::new(&mut br);
158 dmx.open().unwrap();
159 loop {
160 let pktres = dmx.get_frame();
161 if let Err(e) = pktres {
162 if (e as i32) == (DemuxerError::EOF as i32) { break; }
163 panic!("error");
164 }
165 let pkt = pktres.unwrap();
166 println!("Got {}", pkt);
167 }
168 }
169}