2 use nihav_core::demuxers::*;
4 const SMK_FLAG_LOOP_FRAME: u32 = 0x01;
6 const SMK_FRAME_FLAG_PALETTE: u8 = 0x01;
8 const SMK_AUD_FLAG_PACKED: u8 = 0x80;
9 const SMK_AUD_FLAG_PRESENT: u8 = 0x40;
10 const SMK_AUD_FLAG_16BIT: u8 = 0x20;
11 const SMK_AUD_FLAG_STEREO: u8 = 0x10;
12 const SMK_AUD_FLAG_BINKAUD_RDFT: u8 = 0x08;
13 const SMK_AUD_FLAG_BINKAUD_DCT: u8 = 0x04;
15 const NUM_AUDIO_TRACKS: usize = 7;
17 const PAL_SIZE: usize = 768;
19 const SMK_DEFAULT_PAL: [u8; 64] = [
20 0x00, 0x04, 0x08, 0x0C, 0x10, 0x14, 0x18, 0x1C,
21 0x20, 0x24, 0x28, 0x2C, 0x30, 0x34, 0x38, 0x3C,
22 0x41, 0x45, 0x49, 0x4D, 0x51, 0x55, 0x59, 0x5D,
23 0x61, 0x65, 0x69, 0x6D, 0x71, 0x75, 0x79, 0x7D,
24 0x82, 0x86, 0x8A, 0x8E, 0x92, 0x96, 0x9A, 0x9E,
25 0xA2, 0xA6, 0xAA, 0xAE, 0xB2, 0xB6, 0xBA, 0xBE,
26 0xC3, 0xC7, 0xCB, 0xCF, 0xD3, 0xD7, 0xDB, 0xDF,
27 0xE3, 0xE7, 0xEB, 0xEF, 0xF3, 0xF7, 0xFB, 0xFF
40 Self { size: 0, flags: 0, srate: 0, id: 0 }
42 fn is_present(&self) -> bool {
43 (self.flags & SMK_AUD_FLAG_PRESENT) != 0
45 fn add_stream(&mut self, strmgr: &mut StreamManager, str_id: usize) -> DemuxerResult<()> {
46 let channels = if (self.flags & SMK_AUD_FLAG_STEREO) != 0 { 2 } else { 1 };
47 let codecname = if (self.flags & SMK_AUD_FLAG_BINKAUD_RDFT) != 0 {
49 } else if (self.flags & SMK_AUD_FLAG_BINKAUD_DCT) != 0 {
51 } else if (self.flags & SMK_AUD_FLAG_PACKED) != 0 {
56 let soniton = if (self.flags & SMK_AUD_FLAG_16BIT) != 0 { SND_S16_FORMAT } else { SND_U8_FORMAT };
57 let ahdr = NAAudioInfo::new(self.srate, channels, soniton, 1);
58 let ainfo = NACodecInfo::new(codecname, NACodecTypeInfo::Audio(ahdr), None);
59 let res = strmgr.add_stream(NAStream::new(StreamType::Audio, (str_id + 1) as u32, ainfo, 1, self.srate));
60 validate!(res.is_some());
61 self.id = res.unwrap();
67 struct SmackerVideoDemuxer<'a> {
68 src: &'a mut ByteReader<'a>,
72 ainfo: [AudioTrack; NUM_AUDIO_TRACKS],
73 frame_sizes: Vec<u32>,
78 queued_packets: Vec<NAPacket>,
82 /*macro_rules! mktag {
83 ($a:expr, $b:expr, $c:expr, $d:expr) => ({
84 (($a as u32) << 24) | (($b as u32) << 16) | (($c as u32) << 8) | ($d as u32)
87 (($arr[0] as u32) << 24) | (($arr[1] as u32) << 16) | (($arr[2] as u32) << 8) | ($arr[3] as u32)
91 fn get_pts_inc(val: i32) -> u64 {
92 if val > 0 { (val as u64) * 100 }
93 else if val < 0 { (-val as u64) }
97 impl<'a> DemuxCore<'a> for SmackerVideoDemuxer<'a> {
98 fn open(&mut self, strmgr: &mut StreamManager, _seek_idx: &mut SeekIndex) -> DemuxerResult<()> {
99 let src = &mut self.src;
100 let mut magic: [u8; 4] = [0; 4];
101 src.read_buf(&mut magic)?;
102 validate!((&magic == b"SMK2") || (&magic == b"SMK4"));
103 let width = src.read_u32le()? as usize;
104 let height = src.read_u32le()? as usize;
105 self.frames = src.read_u32le()? as usize;
106 let pts_inc = src.read_u32le()? as i32;
107 let flags = src.read_u32le()?;
108 validate!((width > 0) && (height > 0) && (self.frames > 0));
109 self.pts_inc = get_pts_inc(pts_inc);
111 if (flags & SMK_FLAG_LOOP_FRAME) != 0 {
115 for i in 0..NUM_AUDIO_TRACKS {
116 self.ainfo[i].size = src.read_u32le()?;
118 let treesize = src.read_u32le()? as usize;
120 let mut treedata: Vec<u8> = Vec::with_capacity(treesize + 20);
121 treedata.resize(treesize + 24, 0);
122 let hdr = &mut treedata[0..4];
123 hdr.copy_from_slice(&magic);
124 let flg = &mut treedata[4..8];
125 flg[0] = (flags & 0xFF) as u8;
126 flg[1] = (flags >> 8) as u8;
127 flg[2] = (flags >> 16) as u8;
128 flg[3] = (flags >> 24) as u8;
129 src.read_buf(&mut treedata[8..][..16])?;
131 for i in 0..NUM_AUDIO_TRACKS {
132 self.ainfo[i].srate = src.read_u24le()?;
133 self.ainfo[i].flags = src.read_byte()?;
138 self.frame_sizes.resize(self.frames, 0);
139 self.frame_flags.resize(self.frames, 0);
140 for i in 0..self.frames {
141 self.frame_sizes[i] = src.read_u32le()?;
143 for i in 0..self.frames {
144 self.frame_flags[i] = src.read_byte()?;
147 src.read_buf(&mut treedata[24..])?;
149 let vhdr = NAVideoInfo::new(width, height, false, PAL8_FORMAT);
150 let vinfo = NACodecInfo::new("smacker-video", NACodecTypeInfo::Video(vhdr), Some(treedata));
151 let res = strmgr.add_stream(NAStream::new(StreamType::Video, 0, vinfo, 1, 100000));
152 validate!(res.is_some());
153 self.video_id = res.unwrap();
155 for i in 0..NUM_AUDIO_TRACKS {
156 if self.ainfo[i].is_present() {
157 self.ainfo[i].add_stream(strmgr, i)?;
161 self.start = src.tell();
167 fn get_frame(&mut self, strmgr: &mut StreamManager) -> DemuxerResult<NAPacket> {
168 if !self.queued_packets.is_empty() {
169 let pkt = self.queued_packets.pop().unwrap();
172 if self.cur_frame >= self.frames { return Err(DemuxerError::EOF); }
174 let mut payload_size = (self.frame_sizes[self.cur_frame] & !3) as usize;
175 let frame_flags = self.frame_flags[self.cur_frame];
176 if (frame_flags & SMK_FRAME_FLAG_PALETTE) != 0 {
177 let chunk_size = (self.src.read_byte()? as usize) * 4;
178 validate!(chunk_size > 0);
179 validate!(payload_size >= chunk_size);
180 payload_size -= chunk_size;
181 let oldpal = self.pal;
183 let endpos = self.src.tell() + (chunk_size as u64) - 1;
185 let op = self.src.read_byte()?;
186 if (op & 0x80) != 0 {
187 idx += ((op & 0x7F) as usize) + 1;
188 } else if (op & 0x40) != 0 {
189 let start = self.src.read_byte()? as usize;
190 let len = ((op & 0x3F) as usize) + 1;
191 validate!(idx + len <= 256);
193 self.pal[idx * 3 + i] = oldpal[start * 3 + i];
197 let ix0 = op as usize;
198 let ix1 = (self.src.read_byte()? & 0x3F) as usize;
199 let ix2 = (self.src.read_byte()? & 0x3F) as usize;
200 self.pal[idx * 3 + 0] = SMK_DEFAULT_PAL[ix0];
201 self.pal[idx * 3 + 1] = SMK_DEFAULT_PAL[ix1];
202 self.pal[idx * 3 + 2] = SMK_DEFAULT_PAL[ix2];
206 validate!(self.src.tell() <= endpos);
207 if self.src.tell() < endpos {
208 let skip_size = endpos - self.src.tell();
209 self.src.read_skip(skip_size as usize)?;
213 let ts = NATimeInfo::new(Some(self.cur_pts), None, None, 1, 100000);
214 for i in 0..NUM_AUDIO_TRACKS {
215 if ((frame_flags >> (i + 1)) & 1) == 0 { continue; }
216 let size = self.src.read_u32le()? as usize;
218 validate!(payload_size >= size);
219 payload_size -= size;
220 if !self.ainfo[i].is_present() {
221 self.src.read_skip(size - 4)?;
224 let strres = strmgr.get_stream(self.ainfo[i].id);
225 validate!(strres.is_some());
226 let stream = strres.unwrap();
227 let pkt = self.src.read_packet(stream.clone(), ts, true, size - 4)?;
228 self.queued_packets.push(pkt);
230 self.queued_packets.reverse();
232 let mut buf: Vec<u8> = Vec::with_capacity(PAL_SIZE + payload_size);
233 buf.resize(PAL_SIZE, 0);
234 buf.copy_from_slice(&self.pal[0..]);
235 if payload_size > 0 {
236 buf.resize(PAL_SIZE + payload_size, 0);
237 self.src.read_buf(&mut buf[PAL_SIZE..])?;
240 let strres = strmgr.get_stream(self.video_id);
241 validate!(strres.is_some());
242 let stream = strres.unwrap();
243 let keyframe = (self.frame_sizes[self.cur_frame] & 1) != 0;
244 let pkt = NAPacket::new(stream, ts, keyframe, buf);
247 self.cur_pts += self.pts_inc;
251 fn seek(&mut self, time: u64, _seek_idx: &SeekIndex) -> DemuxerResult<()> {
253 let start = self.start;
254 self.src.seek(SeekFrom::Start(start))?;
260 Err(DemuxerError::NotImplemented)
264 impl<'a> NAOptionHandler for SmackerVideoDemuxer<'a> {
265 fn get_supported_options(&self) -> &[NAOptionDefinition] { &[] }
266 fn set_options(&mut self, _options: &[NAOption]) { }
267 fn query_option_value(&self, _name: &str) -> Option<NAValue> { None }
270 impl<'a> SmackerVideoDemuxer<'a> {
271 fn new(io: &'a mut ByteReader<'a>) -> Self {
272 SmackerVideoDemuxer {
277 ainfo: [AudioTrack::new(); NUM_AUDIO_TRACKS],
278 frame_sizes: Vec::new(),
279 frame_flags: Vec::new(),
283 queued_packets: Vec::new(),
287 fn reset_state(&mut self) {
288 self.queued_packets.truncate(0);
292 pub struct SMKDemuxerCreator { }
294 impl DemuxerCreator for SMKDemuxerCreator {
295 fn new_demuxer<'a>(&self, br: &'a mut ByteReader<'a>) -> Box<dyn DemuxCore<'a> + 'a> {
296 Box::new(SmackerVideoDemuxer::new(br))
298 fn get_name(&self) -> &'static str { "smacker" }
307 fn test_smk_demux() {
308 let mut file = File::open("assets/RAD/20130507_audio-distortion.smk").unwrap();
309 // let mut file = File::open("assets/RAD/ajfstr1.smk").unwrap();
310 // let mut file = File::open("assets/RAD/credits.smk").unwrap();
311 // let mut file = File::open("assets/RAD/wetlogo.smk").unwrap();
312 let mut fr = FileReader::new_read(&mut file);
313 let mut br = ByteReader::new(&mut fr);
314 let mut dmx = SmackerVideoDemuxer::new(&mut br);
315 let mut sm = StreamManager::new();
316 let mut si = SeekIndex::new();
317 dmx.open(&mut sm, &mut si).unwrap();
319 let pktres = dmx.get_frame(&mut sm);
320 if let Err(e) = pktres {
321 if (e as i32) == (DemuxerError::EOF as i32) { break; }
324 let pkt = pktres.unwrap();
325 println!("Got {}", pkt);