1 use nihav_core::frame::*;
2 use nihav_core::demuxers::*;
4 struct ArxelCinemaDemuxer<'a> {
5 src: &'a mut ByteReader<'a>,
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;
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()?;
26 let width = src.read_u32le()? as usize;
27 let height = src.read_u32le()? as usize;
28 validate!(width != 0 && height != 0);
30 self.astreams = src.read_byte()? as usize;
31 validate!(self.astreams < 4);
32 if self.astreams > 1 {
33 return Err(DemuxerError::NotImplemented);
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);
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);
47 if let Some(stream) = strmgr.get_stream_by_id(0) {
48 let (tb_num, tb_den) = stream.get_timebase();
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()?;
58 validate!(channels > 0);
59 validate!(abits >= 8);
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);
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);
80 #[allow(unused_variables)]
81 fn get_frame(&mut self, strmgr: &mut StreamManager) -> DemuxerResult<NAPacket> {
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;
91 self.src.seek(SeekFrom::Start(pos))?;
92 let ftype = self.src.read_byte()?;
94 0x41 | 0x42 | 0x5A => {
95 let size = self.src.read_u32le()? as usize;
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);
103 self.src.read_skip(size)?;
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);
111 return self.src.read_packet(stream, ts, true, size + 0x2F);
113 return Err(DemuxerError::MemoryError);
121 fn seek(&mut self, time: NATimePoint, _seek_index: &SeekIndex) -> DemuxerResult<()> {
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);
128 let pts = NATimeInfo::time_to_ts(ms, 1000, self.tb_num, self.tb_den);
129 self.seek_to_frame(pts)
131 _ => Err(DemuxerError::NotPossible)
134 fn get_duration(&self) -> u64 { 0 }
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 }
141 impl<'a> ArxelCinemaDemuxer<'a> {
142 fn new(io: &'a mut ByteReader<'a>) -> Self {
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;
159 Err(DemuxerError::SeekError)
164 pub struct CNMDemuxerCreator { }
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))
170 fn get_name(&self) -> &'static str { "arxel-cnm" }
178 // samples from the Ring game
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();
189 let pktres = dmx.get_frame(&mut sm);
190 if let Err(e) = pktres {
191 if (e as i32) == (DemuxerError::EOF as i32) { break; }
194 let pkt = pktres.unwrap();
195 println!("Got {}", pkt);
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();
208 let pktres = dmx.get_frame(&mut sm);
209 if let Err(e) = pktres {
210 if (e as i32) == (DemuxerError::EOF as i32) { break; }
213 let pkt = pktres.unwrap();
214 println!("Got {}", pkt);