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