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