]> git.nihav.org Git - nihav.git/blob - nihav-game/src/demuxers/cnm.rs
fix clippy warnings
[nihav.git] / nihav-game / src / demuxers / cnm.rs
1 use nihav_core::frame::*;
2 use nihav_core::demuxers::*;
3
4 struct ArxelCinemaDemuxer<'a> {
5 src: &'a mut ByteReader<'a>,
6 cur_frame: usize,
7 astreams: usize,
8 offsets: Vec<u32>,
9 vpts: u64,
10 tb_num: u32,
11 tb_den: u32,
12 }
13
14 impl<'a> DemuxCore<'a> for ArxelCinemaDemuxer<'a> {
15 #[allow(unused_variables)]
16 fn open(&mut self, strmgr: &mut StreamManager, _seek_index: &mut SeekIndex) -> DemuxerResult<()> {
17 let src = &mut self.src;
18
19 let magic = src.read_tag()?;
20 validate!(&magic == b"CNM ");
21 let magic = src.read_tag()?;
22 validate!(&magic == b"UNR\x00");
23 let _duration = src.read_u32le()?;
24 let tb_den = src.read_u32le()?;
25 src.read_byte()?;
26 let width = src.read_u32le()? as usize;
27 let height = src.read_u32le()? as usize;
28 validate!(width != 0 && height != 0);
29 src.read_u16le()?;
30 self.astreams = src.read_byte()? as usize;
31 validate!(self.astreams < 4);
32 if self.astreams > 1 {
33 return Err(DemuxerError::NotImplemented);
34 }
35 let nframes = src.read_u32le()? as usize;
36 src.read_u32le()?; //nframes again?
37 let tab_size = src.read_u32le()? as usize;
38 validate!(tab_size == nframes * 8);
39 src.read_skip(0x98)?;
40
41 let vhdr = NAVideoInfo::new(width, height, true, RGB24_FORMAT);
42 let vci = NACodecTypeInfo::Video(vhdr);
43 let vinfo = NACodecInfo::new("arxel-video", vci, None);
44 if strmgr.add_stream(NAStream::new(StreamType::Video, 0, vinfo, 100, tb_den, nframes as u64)).is_none() {
45 return Err(DemuxerError::MemoryError);
46 }
47 if let Some(stream) = strmgr.get_stream_by_id(0) {
48 let (tb_num, tb_den) = stream.get_timebase();
49 self.tb_num = tb_num;
50 self.tb_den = tb_den;
51 }
52
53 for trk in 0..(self.astreams as u32) {
54 let channels = src.read_byte()? + 1;
55 let abits = src.read_byte()?;
56 let arate = src.read_u32le()?;
57 src.read_skip(10)?;
58 validate!(channels > 0);
59 validate!(abits >= 8);
60 validate!(arate > 0);
61
62 let ahdr = NAAudioInfo::new(arate, channels, if abits > 8 { SND_S16_FORMAT } else { SND_U8_FORMAT }, 2);
63 let ainfo = NACodecInfo::new("pcm", NACodecTypeInfo::Audio(ahdr), None);
64 if strmgr.add_stream(NAStream::new(StreamType::Audio, 1 + trk, ainfo, 1, arate, 0)).is_none() {
65 return Err(DemuxerError::MemoryError);
66 }
67 }
68
69 let tab_size = tab_size / 4;
70 self.offsets = Vec::with_capacity(tab_size);
71 for _ in 0..tab_size {
72 let offset = src.read_u32le()?;
73 self.offsets.push(offset);
74 }
75 self.cur_frame = 0;
76 self.vpts = 0;
77 Ok(())
78 }
79
80 #[allow(unused_variables)]
81 fn get_frame(&mut self, strmgr: &mut StreamManager) -> DemuxerResult<NAPacket> {
82 loop {
83 if self.cur_frame >= self.offsets.len() { return Err(DemuxerError::EOF); }
84 let pos = u64::from(self.offsets[self.cur_frame]);
85 let stream_id = (self.cur_frame % (self.astreams.max(1) + 1)) as u32;
86 self.cur_frame += 1;
87 if pos == 0 {
88 continue;
89 }
90
91 self.src.seek(SeekFrom::Start(pos))?;
92 let ftype = self.src.read_byte()?;
93 match ftype {
94 0x41 | 0x42 | 0x5A => {
95 let size = self.src.read_u32le()? as usize;
96 if size == 0 {
97 continue;
98 }
99 if let Some(stream) = strmgr.get_stream_by_id(stream_id) {
100 let ts = stream.make_ts(None, None, None);
101 return self.src.read_packet(stream, ts, true, size);
102 } else {
103 self.src.read_skip(size)?;
104 }
105 },
106 0x53 => {
107 let size = self.src.peek_u32le()? as usize;
108 if let Some(stream) = strmgr.get_stream_by_id(0) {
109 let ts = stream.make_ts(Some(self.vpts), None, None);
110 self.vpts += 1;
111 return self.src.read_packet(stream, ts, true, size + 0x2F);
112 } else {
113 return Err(DemuxerError::MemoryError);
114 }
115 },
116 _ => continue,
117 };
118 }
119 }
120
121 fn seek(&mut self, time: NATimePoint, _seek_index: &SeekIndex) -> DemuxerResult<()> {
122 match time {
123 NATimePoint::PTS(pts) => self.seek_to_frame(pts),
124 NATimePoint::Milliseconds(ms) => {
125 if self.tb_num == 0 || self.tb_den == 0 {
126 return Err(DemuxerError::NotPossible);
127 }
128 let pts = NATimeInfo::time_to_ts(ms, 1000, self.tb_num, self.tb_den);
129 self.seek_to_frame(pts)
130 },
131 _ => Err(DemuxerError::NotPossible)
132 }
133 }
134 fn get_duration(&self) -> u64 { 0 }
135 }
136 impl<'a> NAOptionHandler for ArxelCinemaDemuxer<'a> {
137 fn get_supported_options(&self) -> &[NAOptionDefinition] { &[] }
138 fn set_options(&mut self, _options: &[NAOption]) { }
139 fn query_option_value(&self, _name: &str) -> Option<NAValue> { None }
140 }
141 impl<'a> ArxelCinemaDemuxer<'a> {
142 fn new(io: &'a mut ByteReader<'a>) -> Self {
143 ArxelCinemaDemuxer {
144 src: io,
145 cur_frame: 0,
146 astreams: 0,
147 offsets: Vec::new(),
148 vpts: 0,
149 tb_num: 0,
150 tb_den: 0,
151 }
152 }
153 fn seek_to_frame(&mut self, pts: u64) -> DemuxerResult<()> {
154 let nframe = (pts as usize) * (1 + self.astreams.max(1));
155 if nframe < self.offsets.len() {
156 self.cur_frame = nframe;
157 Ok(())
158 } else {
159 Err(DemuxerError::SeekError)
160 }
161 }
162 }
163
164 pub struct CNMDemuxerCreator { }
165
166 impl DemuxerCreator for CNMDemuxerCreator {
167 fn new_demuxer<'a>(&self, br: &'a mut ByteReader<'a>) -> Box<dyn DemuxCore<'a> + 'a> {
168 Box::new(ArxelCinemaDemuxer::new(br))
169 }
170 fn get_name(&self) -> &'static str { "arxel-cnm" }
171 }
172
173 #[cfg(test)]
174 mod test {
175 use super::*;
176 use std::fs::File;
177
178 // samples from the Ring game
179 #[test]
180 fn test_cnm_demux() {
181 let mut file = File::open("assets/Game/logo.cnm").unwrap();
182 let mut fr = FileReader::new_read(&mut file);
183 let mut br = ByteReader::new(&mut fr);
184 let mut dmx = ArxelCinemaDemuxer::new(&mut br);
185 let mut sm = StreamManager::new();
186 let mut si = SeekIndex::new();
187 dmx.open(&mut sm, &mut si).unwrap();
188 loop {
189 let pktres = dmx.get_frame(&mut sm);
190 if let Err(e) = pktres {
191 if (e as i32) == (DemuxerError::EOF as i32) { break; }
192 panic!("error");
193 }
194 let pkt = pktres.unwrap();
195 println!("Got {}", pkt);
196 }
197 }
198 #[test]
199 fn test_cnm_noaud_demux() {
200 let mut file = File::open("assets/Game/tr_as_ro_d.cnm").unwrap();
201 let mut fr = FileReader::new_read(&mut file);
202 let mut br = ByteReader::new(&mut fr);
203 let mut dmx = ArxelCinemaDemuxer::new(&mut br);
204 let mut sm = StreamManager::new();
205 let mut si = SeekIndex::new();
206 dmx.open(&mut sm, &mut si).unwrap();
207 loop {
208 let pktres = dmx.get_frame(&mut sm);
209 if let Err(e) = pktres {
210 if (e as i32) == (DemuxerError::EOF as i32) { break; }
211 panic!("error");
212 }
213 let pkt = pktres.unwrap();
214 println!("Got {}", pkt);
215 }
216 }
217 }