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