2 use nihav_core::demuxers::DemuxerError::*;
8 ($a:expr, $b:expr, $c:expr, $d:expr) => ({
9 (($a as u32) << 24) | (($b as u32) << 16) | (($c as u32) << 8) | ($d as u32)
12 (($arr[0] as u32) << 24) | (($arr[1] as u32) << 16) | (($arr[2] as u32) << 8) | ($arr[3] as u32)
16 const RM_SIPRO_BLOCK_SIZES: [usize; 4] = [ 29, 19, 37, 20 ];
17 const RM_SIPRO_SWAPS: [[u8; 2]; 38] = [
18 [ 0, 63 ], [ 1, 22 ], [ 2, 44 ], [ 3, 90 ],
19 [ 5, 81 ], [ 7, 31 ], [ 8, 86 ], [ 9, 58 ],
20 [ 10, 36 ], [ 12, 68 ], [ 13, 39 ], [ 14, 73 ],
21 [ 15, 53 ], [ 16, 69 ], [ 17, 57 ], [ 19, 88 ],
22 [ 20, 34 ], [ 21, 71 ], [ 24, 46 ], [ 25, 94 ],
23 [ 26, 54 ], [ 28, 75 ], [ 29, 50 ], [ 32, 70 ],
24 [ 33, 92 ], [ 35, 74 ], [ 38, 85 ], [ 40, 56 ],
25 [ 42, 87 ], [ 43, 65 ], [ 45, 59 ], [ 48, 79 ],
26 [ 49, 93 ], [ 51, 89 ], [ 55, 95 ], [ 61, 76 ],
27 [ 67, 83 ], [ 77, 80 ]
30 struct RMVideoStream {
47 self.frame.truncate(0);
51 fn start_slice(&mut self, num_slices: usize, frame_size: usize, data: &[u8]) {
52 self.hdr_size = num_slices * 8 + 1;
53 self.frame.resize(frame_size + self.hdr_size, 0);
54 self.frame[0] = (num_slices - 1) as u8;
56 self.add_slice(1, data).unwrap();
58 fn add_slice(&mut self, slice_no: usize, data: &[u8]) -> DemuxerResult<()> {
59 validate!(self.hdr_size + self.frame_pos + data.len() <= self.frame.len());
60 self.write_slice_info(slice_no);
61 let dslice = &mut self.frame[self.hdr_size + self.frame_pos..][..data.len()];
62 dslice.copy_from_slice(data);
63 self.frame_pos += data.len();
66 fn write_slice_info(&mut self, slice_no: usize) {
67 let off = 1 + (slice_no - 1) * 8;
68 self.frame[off + 0] = 0;
69 self.frame[off + 1] = 0;
70 self.frame[off + 2] = 0;
71 self.frame[off + 3] = 1;
72 self.frame[off + 4] = (self.frame_pos >> 24) as u8;
73 self.frame[off + 5] = (self.frame_pos >> 16) as u8;
74 self.frame[off + 6] = (self.frame_pos >> 8) as u8;
75 self.frame[off + 7] = (self.frame_pos >> 0) as u8;
77 fn get_frame_data(&mut self) -> Vec<u8> {
78 let mut v: Vec<u8> = Vec::new();
79 mem::swap(&mut v, &mut self.frame);
85 #[derive(Clone,Copy,PartialEq)]
94 struct RMAudioStream {
96 iinfo: Option<InterleaveInfo>,
101 const RM_ILEAVE_INT0: u32 = mktag!(b"Int0");
102 const RM_ILEAVE_INT4: u32 = mktag!(b"Int4");
103 const RM_ILEAVE_GENR: u32 = mktag!(b"genr");
104 const RM_ILEAVE_SIPR: u32 = mktag!(b"sipr");
105 const RM_ILEAVE_VBRF: u32 = mktag!(b"vbrf");
106 const RM_ILEAVE_VBRS: u32 = mktag!(b"vbrs");
109 fn new(iinfo: Option<InterleaveInfo>) -> Self {
112 if let Some(info) = iinfo {
113 deint = match info.id {
114 RM_ILEAVE_INT0 => Deinterleaver::None,
115 RM_ILEAVE_INT4 => Deinterleaver::RA28_8,
116 RM_ILEAVE_GENR => Deinterleaver::Generic,
117 RM_ILEAVE_SIPR => Deinterleaver::Sipro,
118 RM_ILEAVE_VBRF => Deinterleaver::VBR,
119 RM_ILEAVE_VBRS => Deinterleaver::VBR,
120 _ => {println!("unknown deint {:X}", info.id); Deinterleaver::None },
123 Deinterleaver::None => { buf = Vec::new(); },
124 Deinterleaver::RA28_8 |
125 Deinterleaver::Generic |
126 Deinterleaver::Sipro => {
127 let bsize = (info.frame_size as usize) * (info.factor as usize);
128 buf = vec![0; bsize];
130 Deinterleaver::VBR => {
135 deint = Deinterleaver::None;
138 RMAudioStream { deint, iinfo, buf, sub_packet: 0 }
140 fn read_apackets(&mut self, queued_packets: &mut Vec<NAPacket>, src: &mut ByteReader, stream: NAStreamRef, ts: u32, keyframe: bool, payload_size: usize) -> DemuxerResult<NAPacket> {
141 let (tb_num, tb_den) = stream.get_timebase();
142 let ts = NATimeInfo::new(Some(ts as u64), None, None, tb_num, tb_den);
148 Deinterleaver::None => { return src.read_packet(stream, ts, keyframe, payload_size); },
149 Deinterleaver::RA28_8 => {
150 let iinfo = self.iinfo.unwrap();
151 let factor = iinfo.factor as usize;
152 let halffact = factor >> 1;
153 let fsize = iinfo.frame_size as usize;
154 let bsize = iinfo.block_size as usize;
155 let ppos = self.sub_packet;
156 for sb in 0..halffact {
157 let mut dst = &mut self.buf[sb * 2 * fsize + ppos * bsize..][..bsize];
158 src.read_buf(&mut dst)?;
160 self.sub_packet += 1;
161 if self.sub_packet == factor {
163 return Ok(NAPacket::new(stream, ts, true, self.buf.clone()));
165 return Err(DemuxerError::TryAgain);
168 Deinterleaver::Generic => {
169 let iinfo = self.iinfo.unwrap();
170 let factor = iinfo.factor as usize;
171 let fsize = iinfo.frame_size as usize;
172 let bsize = iinfo.block_size as usize;
173 let factor2 = fsize / bsize;
174 let ppos = self.sub_packet;
176 for sb in 0..factor2 {
177 let sb_pos = factor * sb + ((factor + 1) >> 1) * (ppos & 1) + (ppos >> 1);
178 let mut dst = &mut self.buf[bsize * sb_pos..][..bsize];
179 src.read_buf(&mut dst)?;
182 Deinterleaver::Sipro => {
183 let iinfo = self.iinfo.unwrap();
184 let fsize = iinfo.frame_size as usize;
185 let ppos = self.sub_packet;
187 let mut dst = &mut self.buf[fsize * ppos..][..fsize];
188 src.read_buf(&mut dst)?;
190 Deinterleaver::VBR => {
191 validate!(payload_size >= 5);
192 let hdrsizesize = src.read_u16be()?;
193 let num_entries = (hdrsizesize / 16) as usize;
194 validate!(payload_size >= num_entries * 3 + 2);
195 let mut sizes: Vec<usize> = Vec::with_capacity(num_entries);
196 let mut tot_size = 0;
197 for _ in 0..num_entries {
198 let sz = src.read_u16be()? as usize;
202 validate!(tot_size + num_entries * 2 + 2 == payload_size);
203 let pkt_ts = NATimeInfo::new(None, None, None, tb_num, tb_den);
204 let mut first = true;
205 for size in sizes.iter() {
206 let cur_ts = if first { ts } else { pkt_ts };
208 let pkt = src.read_packet(stream.clone(), cur_ts, true, *size)?;
209 queued_packets.push(pkt);
211 queued_packets.reverse();
212 let pkt0 = queued_packets.pop().unwrap();
217 let iinfo = self.iinfo.unwrap();
218 let factor = iinfo.factor as usize;
219 let fsize = if iinfo.block_size != 0 { iinfo.block_size } else { iinfo.frame_size } as usize;
221 self.sub_packet += 1;
222 if self.sub_packet < factor {
223 return Err(DemuxerError::TryAgain);
228 if self.deint == Deinterleaver::Sipro {
229 sipro_restore(&mut self.buf, factor, iinfo.frame_size as usize);
232 let mut frames_iter = self.buf.chunks(fsize);
233 let pkt0 = frames_iter.next().unwrap();
235 let pkt_ts = NATimeInfo::new(None, None, None, tb_num, tb_den);
236 for pkts in frames_iter {
237 let pkt = NAPacket::new(stream.clone(), pkt_ts, true, pkts.to_vec());
238 queued_packets.push(pkt);
240 queued_packets.reverse();
241 Ok(NAPacket::new(stream, ts, true, pkt0.to_vec()))
245 fn sipro_restore(buf: &mut [u8], factor: usize, fsize: usize) {
246 let stride = factor * fsize * 2 / 96;
248 let mut sidx = (RM_SIPRO_SWAPS[i][0] as usize) * stride;
249 let mut didx = (RM_SIPRO_SWAPS[i][1] as usize) * stride;
251 let in0 = buf[sidx >> 1];
252 let in1 = buf[didx >> 1];
253 let nib0 = (in0 >> ((sidx & 1) * 4)) & 0xF;
254 let nib1 = (in1 >> ((didx & 1) * 4)) & 0xF;
256 buf[didx >> 1] = (nib0 << (4 * (didx & 1))) | (in1 & (0xF << (4 * (!didx & 1))));
257 buf[sidx >> 1] = (nib1 << (4 * (sidx & 1))) | (in0 & (0xF << (4 * (!sidx & 1))));
265 struct SubstreamInfo {
272 sub_info: Vec<SubstreamInfo>,
279 sub_info: Vec::new(),
283 fn add_stream(&mut self, stream_no: u32) {
284 let ssinfo = SubstreamInfo { id: stream_no, map: Vec::new(), str_ids: Vec::new() };
285 self.sub_info.push(ssinfo);
287 fn get_substream_no(&self) -> u32 {
290 fn find_idx(&self, stream_no: u32) -> Option<usize> {
291 self.sub_info.iter().position(|x| x.id == stream_no)
293 fn add_map_rule(&mut self, map_ss: u16) {
294 let idx = self.sub_info.len() - 1;
295 self.sub_info[idx].map.push(map_ss);
297 fn add_substream(&mut self) {
298 let idx = self.sub_info.len() - 1;
299 self.sub_info[idx].str_ids.push(self.sstr_id);
302 fn is_mlti_stream(&self, stream_no: u32) -> bool {
303 self.find_idx(stream_no).is_some()
305 fn find_substream(&self, stream_no: u32, grp: u16) -> Option<u32> {
306 if let Some(idx) = self.find_idx(stream_no) {
307 if (grp as usize) < self.sub_info[idx].map.len() {
308 let sub_id = self.sub_info[idx].map[grp as usize] as usize;
309 if sub_id < self.sub_info[idx].str_ids.len() {
310 return Some(self.sub_info[idx].str_ids[sub_id]);
319 Audio(RMAudioStream),
320 Video(RMVideoStream),
325 struct CommonStreamData {
326 streams: Vec<RMStreamType>,
328 mlti_mapper: MLTIMapper,
331 impl CommonStreamData {
336 mlti_mapper: MLTIMapper::new(),
339 fn get_stream_id(&self, str_no: u32, pkt_grp: u16) -> u32 {
340 if !self.mlti_mapper.is_mlti_stream(str_no) {
343 self.mlti_mapper.find_substream(str_no, pkt_grp).unwrap()
346 fn find_stream(&self, stream_id: u32) -> Option<usize> {
347 self.str_ids.iter().position(|x| *x == stream_id)
351 struct RealMediaDemuxer<'a> {
352 src: &'a mut ByteReader<'a>,
360 str_data: CommonStreamData,
362 data_chunks: Vec<(u64, u32, u16)>,
363 cur_data_chunk: usize,
365 queued_pkts: Vec<NAPacket>,
369 fn find_codec_name(registry: &[(&[u8;4], &'static str)], fcc: u32) -> &'static str {
370 for &(fourcc, name) in registry {
371 if mktag!(fourcc) == fcc { return name; }
376 fn read_14or30(src: &mut ByteReader) -> DemuxerResult<(bool, u32)> {
377 let tmp = src.read_u16be()?;
378 let flag = (tmp & 0x8000) != 0;
379 if (tmp & 0x4000) == 0x4000 {
380 Ok((flag, ((tmp & 0x3FFF) as u32)))
382 let val = ((tmp as u32) << 16) | (src.read_u16be()? as u32);
383 Ok((flag, val & 0x3FFFFFFF))
387 fn read_video_buf(src: &mut ByteReader, stream: NAStreamRef, ts: u32, keyframe: bool, frame_size: usize) -> DemuxerResult<NAPacket> {
388 let size = (frame_size as usize) + 9;
389 let mut vec: Vec<u8> = vec![0; size];
390 //v[0] = 0; // 1 slice
392 src.read_buf(&mut vec[9..])?;
394 let (tb_num, tb_den) = stream.get_timebase();
395 let ts = NATimeInfo::new(Some(ts as u64), None, None, tb_num, tb_den);
396 Ok(NAPacket::new(stream, ts, keyframe, vec))
399 fn read_multiple_frame(src: &mut ByteReader, stream: NAStreamRef, keyframe: bool, skip_mtype: bool) -> DemuxerResult<NAPacket> {
401 let mtype = src.read_byte()?;
402 validate!(mtype == 0xC0);
404 let (_, frame_size) = read_14or30(src)?;
405 let (_, timestamp) = read_14or30(src)?;
406 let _seq_no = src.read_byte()?;
407 //println!(" multiple frame size {} ts {} seq {}", frame_size, timestamp, seq_no);
409 read_video_buf(src, stream, timestamp, keyframe, frame_size as usize)
412 struct RMDemuxCommon {}
415 fn parse_stream_info(str_data: &mut CommonStreamData, strmgr: &mut StreamManager, stream_no: u32, edata: &Vec<u8>) -> DemuxerResult<bool> {
416 let mut is_mlti = false;
417 let mut mr = MemoryReader::new_read(edata.as_slice());
418 let mut src = ByteReader::new(&mut mr);
419 let tag = src.read_u32be()?;
420 let tag2 = src.peek_u32be()?;
421 //println!("tag1 {:X} tag2 {:X}", tag, tag2);
422 if tag == mktag!('.', 'r', 'a', 0xFD) {
423 Self::parse_audio_stream(strmgr, &mut str_data.streams, stream_no, &mut src, edata.as_slice())?;
424 } else if ((tag2 == mktag!('V', 'I', 'D', 'O')) || (tag2 == mktag!('I', 'M', 'A', 'G'))) && ((tag as usize) <= edata.len()) {
425 Self::parse_video_stream(strmgr, &mut str_data.streams, stream_no, &mut src, edata.as_slice(), tag2)?;
426 } else if tag == mktag!(b"LSD:") {
427 let extradata = Some(edata.to_owned());
429 src.read_skip(4)?; //version
430 let channels = src.read_u16be()?;
431 let samp_size = src.read_u16be()?;
432 let sample_rate = src.read_u32be()?;
434 println!("LSD sr {}, {} ch", sample_rate, channels);
435 let soniton = NASoniton::new(samp_size as u8, SONITON_FLAG_SIGNED);
436 let ahdr = NAAudioInfo::new(sample_rate, channels as u8, soniton, 1);
437 let nainfo = NACodecInfo::new("ralf", NACodecTypeInfo::Audio(ahdr), extradata);
438 let res = strmgr.add_stream(NAStream::new(StreamType::Audio, stream_no as u32, nainfo, 1, 1000));
439 if res.is_none() { return Err(MemoryError); }
440 let astr = RMAudioStream::new(None);
441 str_data.streams.push(RMStreamType::Audio(astr));
442 } else if tag == mktag!(b"MLTI") {
444 let num_rules = src.read_u16be()? as usize;
446 str_data.mlti_mapper.add_stream(stream_no);
447 for _ in 0..num_rules {
448 let substr = src.read_u16be()?;
449 max_sub = max_sub.max(substr);
450 str_data.mlti_mapper.add_map_rule(substr);
452 let num_substreams = src.read_u16be()? as usize;
453 validate!(num_substreams > (max_sub as usize));
454 for _ in 0..num_substreams {
455 let hdr_size = src.read_u32be()? as usize;
456 validate!(hdr_size > 8);
457 let pos = src.tell() as usize;
458 src.read_skip(hdr_size)?;
459 str_data.mlti_mapper.add_substream();
461 let hdrsrc = &edata[pos..][..hdr_size];
462 let mut mr = MemoryReader::new_read(hdrsrc);
463 let mut hsrc = ByteReader::new(&mut mr);
465 let tag = hsrc.read_u32be()?;
466 let tag2 = hsrc.peek_u32be()?;
467 let stream_no = str_data.mlti_mapper.get_substream_no();
468 //todo check that all substreams are of the same type");
469 if tag == mktag!('.', 'r', 'a', 0xFD) {
470 Self::parse_audio_stream(strmgr, &mut str_data.streams, stream_no, &mut hsrc, hdrsrc)?;
471 } else if (tag2 == mktag!('V', 'I', 'D', 'O')) && ((tag as usize) <= hdr_size) {
472 Self::parse_video_stream(strmgr, &mut str_data.streams, stream_no, &mut hsrc, hdrsrc, tag2)?;
474 println!("unknown MLTI substream {:08X} / {:08X}", tag, tag2);
475 return Err(DemuxerError::InvalidData);
477 str_data.str_ids.push(stream_no);
481 str_data.streams.push(RMStreamType::Logical);
485 fn parse_audio_stream(strmgr: &mut StreamManager, streams: &mut Vec<RMStreamType>, stream_no: u32, src: &mut ByteReader, edata_: &[u8]) -> DemuxerResult<()> {
486 let ver = src.read_u16be()?;
487 let ainfo = match ver {
498 println!("unknown version {}", ver);
499 return Err(DemuxerError::InvalidData);
502 println!(" got ainfo {:?}", ainfo);
503 let cname = find_codec_name(RM_AUDIO_CODEC_REGISTER, ainfo.fcc);
504 let blk_size = if ainfo.fcc != mktag!(b"sipr") {
505 ainfo.granularity as usize
507 validate!(ainfo.flavor <= 3);
508 RM_SIPRO_BLOCK_SIZES[ainfo.flavor as usize]
510 let srate = ainfo.sample_rate;
511 let soniton = NASoniton::new(ainfo.sample_size as u8, SONITON_FLAG_SIGNED);
512 let ahdr = NAAudioInfo::new(srate, ainfo.channels as u8, soniton, blk_size);
513 let extradata = if ainfo.edata_size == 0 {
516 let eslice = &edata_[(src.tell() as usize)..];
517 Some(eslice.to_vec())
519 let nainfo = NACodecInfo::new(cname, NACodecTypeInfo::Audio(ahdr), extradata);
520 let res = strmgr.add_stream(NAStream::new(StreamType::Audio, stream_no as u32, nainfo, 1, 1000));
521 if res.is_none() { return Err(MemoryError); }
523 let astr = RMAudioStream::new(ainfo.ileave_info);
524 streams.push(RMStreamType::Audio(astr));
527 #[allow(unused_variables)]
528 fn parse_video_stream(strmgr: &mut StreamManager, streams: &mut Vec<RMStreamType>, stream_no: u32, src: &mut ByteReader, edata_: &[u8], tag2: u32) -> DemuxerResult<()> {
530 let fcc = src.read_u32be()?;
531 let width = src.read_u16be()? as usize;
532 let height = src.read_u16be()? as usize;
533 let bpp = src.read_u16be()?;
534 let pad_w = src.read_u16be()?;
535 let pad_h = src.read_u16be()?;
537 if tag2 == mktag!('V', 'I', 'D', 'O') {
538 _fps = src.read_u32be()?;
542 let extradata: Option<Vec<u8>>;
544 let eslice = &edata_[(src.tell() as usize)..];
545 extradata = Some(eslice.to_vec());
549 let cname = find_codec_name(RM_VIDEO_CODEC_REGISTER, fcc);
551 let vhdr = NAVideoInfo::new(width, height, false, RGB24_FORMAT);
552 let vinfo = NACodecInfo::new(cname, NACodecTypeInfo::Video(vhdr), extradata);
553 let res = strmgr.add_stream(NAStream::new(StreamType::Video, stream_no as u32, vinfo, 1, 1000));
554 if res.is_none() { return Err(DemuxerError::MemoryError); }
556 let vstr = RMVideoStream::new();
557 streams.push(RMStreamType::Video(vstr));
560 #[allow(unused_variables)]
561 fn parse_packet_payload(src: &mut ByteReader, rmstream: &mut RMStreamType, stream: NAStreamRef, slice_buf: &mut Vec<u8>, queued_pkts: &mut Vec<NAPacket>, keyframe: bool, ts: u32, payload_size: usize) -> DemuxerResult<NAPacket> {
563 RMStreamType::Video(ref mut vstr) => {
565 let pos = src.tell();
566 let b0 = src.read_byte()?;
568 0 => { // partial frame
569 let b1 = src.read_byte()?;
570 let hdr1 = ((b0 as u16) << 8) | (b1 as u16);
571 let num_pkts = ((hdr1 >> 7) & 0x7F) as usize;
572 let packet_num = hdr1 & 0x7F;
573 let (_, frame_size) = read_14or30(src)?;
574 let (_, off) = read_14or30(src)?;
575 let seq_no = src.read_byte()?;
576 //println!(" mode 0 pkt {}/{} off {}/{} seq {}", packet_num, num_pkts, off, frame_size, seq_no);
577 let hdr_skip = (src.tell() - pos) as usize;
579 let slice_size = (payload_size - hdr_skip) as usize;
580 slice_buf.resize(slice_size, 0);
581 src.read_buf(slice_buf.as_mut_slice())?;
583 vstr.start_slice(num_pkts, frame_size as usize, slice_buf.as_slice());
585 vstr.add_slice(packet_num as usize, slice_buf.as_slice())?;
587 if (packet_num as usize) < num_pkts {
588 return Err(DemuxerError::TryAgain);
590 //todo: check if full frame is received
591 let (tb_num, tb_den) = stream.get_timebase();
592 let ts = NATimeInfo::new(Some(ts as u64), None, None, tb_num, tb_den);
593 let pkt = NAPacket::new(stream, ts, keyframe, vstr.get_frame_data());
596 1 => { // whole frame
597 let seq_no = src.read_byte()?;
598 //println!(" mode 1 seq {}", seq_no);
599 read_video_buf(src, stream, ts, keyframe, payload_size - 2)
601 2 => { // last partial frame
602 let b1 = src.read_byte()?;
603 let hdr1 = ((b0 as u16) << 8) | (b1 as u16);
604 let num_pkts = ((hdr1 >> 7) & 0x7F) as usize;
605 let packet_num = hdr1 & 0x7F;
606 let (_, frame_size) = read_14or30(src)?;
607 let (_, tail_size) = read_14or30(src)?;
608 let seq_no = src.read_byte()?;
609 //println!(" mode 2 pkt {}/{} tail {}/{} seq {}", packet_num, num_pkts, tail_size, frame_size, seq_no);
610 slice_buf.resize(tail_size as usize, 0);
611 src.read_buf(slice_buf.as_mut_slice())?;
612 if packet_num == 1 && frame_size == tail_size {
613 vstr.start_slice(num_pkts, frame_size as usize, slice_buf.as_slice());
615 vstr.add_slice(packet_num as usize, slice_buf.as_slice())?;
618 while src.tell() < pos + (payload_size as u64) {
619 let res = read_multiple_frame(src, stream.clone(), false, false);
620 if res.is_err() { break; }
621 queued_pkts.push(res.unwrap());
623 queued_pkts.reverse();
624 let (tb_num, tb_den) = stream.get_timebase();
625 let ts = NATimeInfo::new(Some(ts as u64), None, None, tb_num, tb_den);
626 let pkt = NAPacket::new(stream, ts, keyframe, vstr.get_frame_data());
629 _ => { // multiple frames
630 //println!(" mode 3");
631 let res = read_multiple_frame(src, stream.clone(), keyframe, true);
632 if res.is_err() { return res; }
633 while src.tell() < pos + (payload_size as u64) {
634 let res = read_multiple_frame(src, stream.clone(), false, false);
635 if res.is_err() { break; }
636 queued_pkts.push(res.unwrap());
638 queued_pkts.reverse();
643 RMStreamType::Audio(ref mut astr) => {
644 astr.read_apackets(queued_pkts, src, stream, ts, keyframe, payload_size)
647 src.read_skip(payload_size)?;
648 Err(DemuxerError::InvalidData)
654 impl<'a> DemuxCore<'a> for RealMediaDemuxer<'a> {
655 fn open(&mut self, strmgr: &mut StreamManager, seek_idx: &mut SeekIndex) -> DemuxerResult<()> {
656 self.read_header(strmgr, seek_idx)?;
660 #[allow(unused_variables)]
661 fn get_frame(&mut self, strmgr: &mut StreamManager) -> DemuxerResult<NAPacket> {
662 if !self.queued_pkts.is_empty() {
663 let pkt = self.queued_pkts.pop().unwrap();
667 if (self.cur_packet >= self.num_packets) || (self.src.tell() >= self.data_end) {
668 self.cur_data_chunk += 1;
669 if self.cur_data_chunk < self.data_chunks.len() {
670 let (pos, _, _) = self.data_chunks[self.cur_data_chunk];
671 self.src.seek(SeekFrom::Start(pos))?;
672 let res = read_chunk(self.src);
673 if let Ok((id, size, ver)) = res {
674 self.data_pos = self.src.tell();
676 self.data_end = self.data_pos + (size as u64);
677 if self.parse_data_start().is_ok() {
682 return Err(DemuxerError::EOF);
685 let pkt_start = self.src.tell();
686 let ver = self.src.read_u16be()?;
688 let len = self.src.read_u16be()? as usize;
689 let str_no = self.src.read_u16be()? as u32;
690 let ts = self.src.read_u32be()?;
694 pkt_grp = self.src.read_byte()? as u16;
695 flags = self.src.read_byte()?;
697 //asm_rule = self.src.read_u16be()?;
698 //asm_flags = self.src.read_byte()?;
699 self.src.read_skip(2)?;
701 self.src.read_skip(1)?;
704 let hdr_size = self.src.tell() - pkt_start;
705 //println!("packet @{:X} size {} for {} ts {} grp {} flags {:X}", pkt_start, len, str_no, ts, pkt_grp, flags);
706 self.cur_packet += 1;
708 let payload_size = len - (hdr_size as usize);
710 let stream_id = self.str_data.get_stream_id(str_no as u32, pkt_grp);
711 let sr = self.str_data.find_stream(stream_id);
713 //println!("stream {} not found", str_no);
714 self.src.read_skip(payload_size)?;
715 return Err(DemuxerError::InvalidData);
717 let str_id = sr.unwrap();
719 let streamres = strmgr.get_stream_by_id(stream_id);
720 if streamres.is_none() {
721 self.src.read_skip(payload_size)?;
724 let stream = streamres.unwrap();
725 //println!(" stream {}", str_id);
726 if strmgr.is_ignored_id(stream_id) {
727 self.src.read_skip(payload_size)?;
730 //todo skip unwanted packet
731 let keyframe = (flags & KEYFRAME_FLAG) != 0;
733 let ret = RMDemuxCommon::parse_packet_payload(&mut self.src, &mut self.str_data.streams[str_id], stream, &mut self.slice_buf, &mut self.queued_pkts, keyframe, ts, payload_size);
734 if let Err(DemuxerError::TryAgain) = ret {
742 #[allow(unused_variables)]
743 fn seek(&mut self, time: u64, seek_idx: &SeekIndex) -> DemuxerResult<()> {
744 self.queued_pkts.clear();
745 let ret = seek_idx.find_pos(time);
747 return Err(DemuxerError::SeekError);
749 let ret = ret.unwrap();
750 let seek_pos = ret.pos;
751 for (pos, size, ver) in self.data_chunks.iter() {
752 if seek_pos < *pos { continue; }
753 let end = *pos + (*size as u64);
756 self.data_pos = seek_pos;
757 self.data_ver = *ver;
759 self.src.seek(SeekFrom::Start(seek_pos))?;
763 Err(DemuxerError::SeekError)
767 fn read_chunk(src: &mut ByteReader) -> DemuxerResult<(u32, u32, u16)> {
768 let id = src.read_u32be()?;
769 if id == 0 { return Ok((0, 0, 0)); }
770 let size = src.read_u32be()?;
772 let ver = src.read_u16be()?;
774 return Ok((id, 0x0FFFFFFF, ver));
776 validate!(size >= 10);
777 let ver = src.read_u16be()?;
782 #[derive(Clone,Copy,Debug)]
783 struct InterleaveInfo {
790 #[derive(Clone,Copy,Debug)]
791 struct RealAudioInfo {
799 bytes_per_minute: u32,
802 ileave_info: Option<InterleaveInfo>
805 fn skip_ra_metadata(src: &mut ByteReader) -> DemuxerResult<()> {
806 let title_len = src.read_byte()? as usize;
807 src.read_skip(title_len)?;
808 let author_len = src.read_byte()? as usize;
809 src.read_skip(author_len)?;
810 let copywrong_len = src.read_byte()? as usize;
811 src.read_skip(copywrong_len)?;
812 let comment_len = src.read_byte()? as usize;
813 src.read_skip(comment_len)?;
817 #[allow(unused_variables)]
818 fn parse_aformat3(src: &mut ByteReader) -> DemuxerResult<RealAudioInfo> {
819 let start = src.tell();
820 let header_len = src.read_u16be()?;
821 validate!(header_len >= 24);
822 let flavor = src.read_u16be()?;
823 let granularity = src.read_u32be()?;
824 let bytes_per_minute = src.read_u32be()?;
825 let total_bytes = src.read_u32be()?;
827 skip_ra_metadata(src)?;
829 let _can_copy = src.read_byte()?;
830 let fcc_len = src.read_byte()?;
831 validate!(fcc_len == 4);
832 let fcc = src.read_u32be()?;
834 let end = src.tell();
835 validate!(end - start <= (header_len as u64) + 2);
839 sample_rate: 8000, sample_size: 16, channels: 1, channel_mask: 0,
840 granularity, bytes_per_minute,
841 total_bytes, edata_size: 0,
846 #[allow(unused_variables)]
847 fn parse_aformat4(src: &mut ByteReader) -> DemuxerResult<RealAudioInfo> {
848 let start = src.tell();
849 src.read_skip(2)?; // zeroes
850 let id = src.read_u32be()?;
851 validate!(id == mktag!(b".ra4"));
852 let data_size = src.read_u32be()?;
853 let _ver4 = src.read_u16be()?; // should be 4
854 let header_size = src.read_u32be()?;
855 let flavor = src.read_u16be()?;
856 let granularity = src.read_u32be()?;
857 let total_bytes = src.read_u32be()?;
858 let bytes_per_minute = src.read_u32be()?;
859 let _bytes_per_minute2 = src.read_u32be()?;
860 let ileave_factor = src.read_u16be()?;
861 let ileave_block_size = src.read_u16be()?;
862 let _user_data = src.read_u16be()?;
863 let sample_rate = src.read_u32be()?;
864 let sample_size = src.read_u32be()?;
865 let channels = src.read_u16be()?;
866 let interleaver_id_len = src.read_byte()?;
867 validate!(interleaver_id_len == 4);
868 let interleaver_id = src.read_u32be()?;
869 let fcc_len = src.read_byte()?;
870 validate!(fcc_len == 4);
871 let fcc = src.read_u32be()?;
872 let is_interleaved = src.read_byte()?;
873 let _can_copy = src.read_byte()?;
874 let _stream_type = src.read_byte()?;
876 skip_ra_metadata(src)?;
878 let end = src.tell();
879 validate!(end - start <= (header_size as u64) + 10);
881 let ileave_info = if is_interleaved != 0 {
882 Some(InterleaveInfo {
883 id: interleaver_id, factor: ileave_factor, block_size: granularity as u16,
884 frame_size: ileave_block_size,
892 sample_rate, sample_size: sample_size as u16, channels, channel_mask: 0,
893 granularity, bytes_per_minute,
894 total_bytes: total_bytes & 0xFFFFFF, edata_size: 0,
899 #[allow(unused_variables)]
900 fn parse_aformat5(src: &mut ByteReader) -> DemuxerResult<RealAudioInfo> {
901 let start = src.tell();
902 src.read_skip(2)?; // zeroes
903 let id = src.read_u32be()?;
904 validate!((id == mktag!(b".ra5")) || (id == mktag!(b".ra4")));
905 let data_size = src.read_u32be()?;
906 let _ver5 = src.read_u16be()?; // should be 5
907 let header_size = src.read_u32be()?;
908 let flavor = src.read_u16be()?;
909 let granularity = src.read_u32be()?;
910 let total_bytes = src.read_u32be()?;
911 let bytes_per_minute = src.read_u32be()?;
912 let _bytes_per_minute2 = src.read_u32be()?;
913 let ileave_factor = src.read_u16be()?;
914 let frame_size = src.read_u16be()?;
915 let ileave_block_size = src.read_u16be()?;
916 let user_data = src.read_u32be()?;
917 let _sample_rate1 = src.read_u16be()?;
918 let sample_rate = src.read_u32be()?;
919 let sample_size = src.read_u32be()?;
920 let channels = src.read_u16be()?;
921 let interleaver_id = src.read_u32be()?;
922 let fcc = src.read_u32be()?;
923 let is_interleaved = src.read_byte()?;
924 let _can_copy = src.read_byte()?;
925 let _stream_type = src.read_byte()?;
926 let has_ileave_pattern = src.read_byte()?;
927 if has_ileave_pattern != 0 {
928 unimplemented!("ra5 interleave pattern");
930 let mut edata_size = src.read_u32be()?;
931 let end = src.tell();
932 if id == mktag!(b".ra5") {
933 validate!(end - start <= (header_size as u64) + 10);
934 // src.read_skip(((end - start) as usize) - (header_size as usize) - 10)?;
936 validate!(end - start <= (header_size as u64) + 15);
939 let ileave_info = if is_interleaved != 0 {
940 Some(InterleaveInfo {
941 id: interleaver_id, factor: ileave_factor, block_size: ileave_block_size, frame_size,
946 if (fcc == mktag!(b"raac")) || (fcc == mktag!(b"racp")) {
947 validate!(edata_size > 1);
954 sample_rate, sample_size: sample_size as u16, channels, channel_mask: 0,
955 granularity, bytes_per_minute,
956 total_bytes: total_bytes & 0xFFFFFF, edata_size,
961 const RMVB_HDR_SIZE: u32 = 18;
962 const RMVB_PROP_SIZE: u32 = 50;
963 const KEYFRAME_FLAG: u8 = 0x02;
965 impl<'a> RealMediaDemuxer<'a> {
966 fn new(io: &'a mut ByteReader<'a>) -> Self {
975 str_data: CommonStreamData::new(),
976 data_chunks: Vec::new(),
978 queued_pkts: Vec::new(),
979 slice_buf: Vec::new(),
982 #[allow(unused_variables)]
983 fn read_header(&mut self, strmgr: &mut StreamManager, seek_idx: &mut SeekIndex) -> DemuxerResult<()> {
984 let (id, size, ver) = read_chunk(self.src)?;
985 validate!((id == mktag!(b".RMF")) || (id == mktag!(b".RMP")));
986 validate!(size >= RMVB_HDR_SIZE);
987 let fver = self.src.read_u32be()?;
988 validate!(fver <= 1);
989 let num_hdr = self.src.read_u32be()? as usize;
990 validate!(num_hdr >= 1);
991 if size > RMVB_HDR_SIZE {
992 self.src.read_skip((size - RMVB_HDR_SIZE) as usize)?;
995 let (id, size, ver) = read_chunk(self.src)?;
996 let prop_size = if ver == 0 { RMVB_PROP_SIZE } else { RMVB_PROP_SIZE + 4 };
997 validate!(size >= prop_size);
998 validate!((ver == 0) || (ver == 2));
999 let maxbr = self.src.read_u32be()?;
1000 let avgbr = self.src.read_u32be()?;
1001 let maxps = self.src.read_u32be()?;
1002 let avgps = self.src.read_u32be()?;
1003 let num_pkt = self.src.read_u32be()? as usize;
1004 let duration = self.src.read_u32be()?;
1005 let preroll = self.src.read_u32be()?;
1007 self.src.read_skip(4)?;
1009 let idx_off = self.src.read_u32be()?;
1010 let data_off = self.src.read_u32be()?;
1011 let num_streams = self.src.read_u16be()? as usize;
1012 let flags = self.src.read_u16be()?;
1013 if size > prop_size {
1014 self.src.read_skip((size - prop_size) as usize)?;
1017 for _ in 0..num_hdr {
1018 if self.src.is_eof() {
1022 let res = self.parse_chunk(strmgr, seek_idx);
1024 Ok(last) => { if last { break; } },
1025 Err(DemuxerError::IOError) => { break; },
1027 if self.data_chunks.len() == 0 { // data is not found, report error
1033 //println!("now @ {:X} / {}", self.src.tell(), self.data_pos);
1034 validate!(self.data_chunks.len() > 0);
1035 self.cur_data_chunk = 0;
1036 let (pos, size, ver) = self.data_chunks[self.cur_data_chunk];
1037 self.data_pos = pos;
1038 self.data_ver = ver;
1039 self.data_end = pos + (size as u64);
1040 self.src.seek(SeekFrom::Start(self.data_pos + 10))?;
1041 self.parse_data_start()?;
1044 fn parse_data_start(&mut self) -> DemuxerResult<()> {
1045 let num_packets = self.src.read_u32be()?;
1046 if self.data_ver == 2 {
1047 self.src.read_skip(12)?; // zeroes?
1049 let next_data_hdr = self.src.read_u32be()?;
1050 self.num_packets = if num_packets > 0 { num_packets } else { 0xFFFFFF };
1051 self.cur_packet = 0;
1052 self.next_data = next_data_hdr as u64;
1055 fn parse_chunk(&mut self, strmgr: &mut StreamManager, seek_idx: &mut SeekIndex) -> DemuxerResult<bool> {
1056 let (id, size, ver) = read_chunk(self.src)?;
1057 let end_pos = self.src.tell() - 10 + (size as u64);
1059 validate!((ver == 0) || (ver == 2));
1060 if id == mktag!(b"CONT") { self.parse_content_desc()?; }
1061 else if id == mktag!(b"MDPR") { self.parse_mdpr(strmgr)?; }
1062 else if id == mktag!(b"DATA") {
1063 self.data_chunks.push((self.src.tell() - 10, size, ver));
1065 else if id == mktag!(b"INDX") {
1066 if !seek_idx.skip_index {
1067 self.parse_index(seek_idx, (size as usize) - 10, ver)?;
1070 else if id == 0 { return Ok(true); }
1071 else { println!("unknown chunk type {:08X}", id); }
1073 let cpos = self.src.tell();
1075 self.src.read_skip((end_pos - cpos) as usize)?;
1079 #[allow(unused_variables)]
1080 fn parse_content_desc(&mut self) -> DemuxerResult<()> {
1081 let title_len = self.src.read_u16be()? as usize;
1082 self.src.read_skip(title_len)?;
1083 let author_len = self.src.read_u16be()? as usize;
1084 self.src.read_skip(author_len)?;
1085 let copywrong_len = self.src.read_u16be()? as usize;
1086 self.src.read_skip(copywrong_len)?;
1087 let comment_len = self.src.read_u16be()? as usize;
1088 self.src.read_skip(comment_len)?;
1091 #[allow(unused_variables)]
1092 fn parse_mdpr(&mut self, strmgr: &mut StreamManager) -> DemuxerResult<()> {
1093 let stream_no = self.src.read_u16be()? as u32;
1094 //todo check stream_no for duplicates
1095 let maxbr = self.src.read_u32be()?;
1096 let avgbr = self.src.read_u32be()?;
1097 let maxps = self.src.read_u32be()?;
1098 let avgps = self.src.read_u32be()?;
1099 let start = self.src.read_u32be()?;
1100 let preroll = self.src.read_u32be()?;
1101 let duration = self.src.read_u32be()?;
1102 let sname_size = self.src.read_byte()? as usize;
1103 let sname = read_string_size(self.src, sname_size)?;
1104 //println!("str #{} sname = {} pkts {}/{} start {} preroll {}", stream_no, sname, maxps, avgps, start, preroll);
1105 let mime_size = self.src.read_byte()? as usize;
1106 let mime = read_string_size(self.src, mime_size)?;
1107 //println!("mime = {}", mime);
1108 let edata_size = self.src.read_u32be()? as usize;
1109 let edata: Option<Vec<u8>> = if edata_size == 0 { None } else {
1110 let mut edvec: Vec<u8> = vec![0; edata_size];
1111 self.src.read_buf(&mut edvec)?;
1114 let mut is_mlti = false;
1116 if let Some(edata_) = edata {
1117 is_mlti = RMDemuxCommon::parse_stream_info(&mut self.str_data, strmgr, stream_no, &edata_)?;
1120 self.str_data.streams.push(RMStreamType::Unknown);
1123 self.str_data.str_ids.push(stream_no);
1128 fn parse_index(&mut self, seek_idx: &mut SeekIndex, chunk_size: usize, ver: u16) -> DemuxerResult<()> {
1129 if ver != 0 { return Ok(()); }
1130 let num_entries = self.src.read_u32be()? as usize;
1131 let str_id = self.src.read_u16be()? as u32;
1132 let _next_idx = self.src.read_u32be()?;
1133 validate!(chunk_size == num_entries * 14 + 10);
1134 if num_entries == 0 { return Ok(()); }
1136 seek_idx.add_stream(str_id);
1137 let idx = seek_idx.get_stream_index(str_id).unwrap();
1138 for _ in 0..num_entries {
1139 let ver = self.src.read_u16be()? as u32;
1140 validate!(ver == 0);
1141 let ts = self.src.read_u32be()? as u64;
1142 let pos = self.src.read_u32be()? as u64;
1143 let _pkt_no = self.src.read_u32be()?;
1144 idx.add_entry(SeekEntry { time: ts, pts: 0, pos });
1147 seek_idx.mode = SeekIndexMode::Present;
1152 fn read_string_size(src: &mut ByteReader, size: usize) -> DemuxerResult<String> {
1153 let mut vec: Vec<u8> = Vec::new();
1155 let c = src.read_byte()?;
1158 let str = String::from_utf8(vec);
1166 struct RealAudioDemuxer<'a> {
1167 src: &'a mut ByteReader<'a>,
1168 stream: Option<RMAudioStream>,
1172 queued_pkts: Vec<NAPacket>,
1175 impl<'a> DemuxCore<'a> for RealAudioDemuxer<'a> {
1176 #[allow(unused_variables)]
1177 fn open(&mut self, strmgr: &mut StreamManager, _seek_idx: &mut SeekIndex) -> DemuxerResult<()> {
1178 let magic = self.src.read_u32be()?;
1179 validate!(magic == mktag!(b".ra\xFD"));
1180 let ver = self.src.read_u16be()?;
1181 let ainfo = match ver {
1183 parse_aformat3(&mut self.src)?
1186 parse_aformat4(&mut self.src)?
1189 parse_aformat5(&mut self.src)?
1192 println!("unknown version {}", ver);
1193 return Err(DemuxerError::InvalidData);
1196 println!(" got ainfo {:?}", ainfo);
1197 let cname = find_codec_name(RM_AUDIO_CODEC_REGISTER, ainfo.fcc);
1198 let blk_size = if ainfo.fcc != mktag!(b"sipr") {
1199 ainfo.granularity as usize
1201 validate!(ainfo.flavor <= 3);
1202 RM_SIPRO_BLOCK_SIZES[ainfo.flavor as usize]
1204 let srate = ainfo.sample_rate;
1205 let soniton = NASoniton::new(ainfo.sample_size as u8, SONITON_FLAG_SIGNED);
1206 let ahdr = NAAudioInfo::new(srate, ainfo.channels as u8, soniton, blk_size);
1207 let extradata = if ainfo.edata_size == 0 {
1210 let mut dta: Vec<u8> = vec![0; ainfo.edata_size as usize];
1211 self.src.read_buf(dta.as_mut_slice())?;
1214 let nainfo = NACodecInfo::new(cname, NACodecTypeInfo::Audio(ahdr), extradata);
1215 let res = strmgr.add_stream(NAStream::new(StreamType::Audio, 0, nainfo, 1, srate));
1216 if res.is_none() { return Err(MemoryError); }
1218 let astr = RMAudioStream::new(ainfo.ileave_info);
1219 self.data_start = self.src.tell();
1220 self.data_end = if ainfo.total_bytes > 0 { self.src.tell() + (ainfo.total_bytes as u64) } else { 0 };
1221 self.blk_size = blk_size;
1222 self.stream = Some(astr);
1227 fn get_frame(&mut self, strmgr: &mut StreamManager) -> DemuxerResult<NAPacket> {
1228 if !self.queued_pkts.is_empty() {
1229 let pkt = self.queued_pkts.pop().unwrap();
1232 if (self.data_end != 0) && (self.src.tell() >= self.data_end) {
1233 return Err(DemuxerError::EOF);
1235 let streamres = strmgr.get_stream_by_id(0);
1236 let stream = streamres.unwrap();
1237 if let Some(ref mut astr) = self.stream {
1239 let ret = astr.read_apackets(&mut self.queued_pkts, &mut self.src, stream.clone(), 0, false, self.blk_size);
1240 if let Err(DemuxerError::TryAgain) = ret {
1246 Err(DemuxerError::NoSuchInput)
1249 #[allow(unused_variables)]
1250 fn seek(&mut self, time: u64, seek_idx: &SeekIndex) -> DemuxerResult<()> {
1255 impl<'a> RealAudioDemuxer<'a> {
1256 fn new(io: &'a mut ByteReader<'a>) -> Self {
1263 queued_pkts: Vec::new(),
1270 StreamProperties(usize),
1271 Packet { ts: u32, str: u32, flags: u32, len: usize, checksum: u32 },
1272 IntValue(Vec<u8>, u32),
1273 BinaryData(Vec<u8>, Vec<u8>),
1274 StringData(Vec<u8>, Vec<u8>),
1281 fn read_string(src: &mut ByteReader) -> DemuxerResult<Vec<u8>> {
1282 let len = src.read_u32be()? as usize;
1283 let mut val = vec![0; len];
1284 src.read_buf(val.as_mut_slice())?;
1288 fn read(src: &mut ByteReader) -> DemuxerResult<Self> {
1289 let code = src.read_byte()?;
1292 let val = src.read_u32be()? as usize;
1293 Ok(IVRRecord::StreamProperties(val))
1296 let ts = src.read_u32be()?;
1297 let str = src.read_u16be()? as u32;
1298 let flags = src.read_u32be()?;
1299 let len = src.read_u32be()? as usize;
1300 let chk = src.read_u32be()?;
1301 validate!((len > 0) && (len < (1 << 24)));
1302 Ok(IVRRecord::Packet { ts, str, flags, len, checksum: chk })
1305 let name = Self::read_string(src)?;
1306 let len = src.read_u32be()?;
1307 validate!(len == 4);
1308 let val = src.read_u32be()?;
1309 Ok(IVRRecord::IntValue(name, val))
1312 let name = Self::read_string(src)?;
1313 let len = src.read_u32be()? as usize;
1314 let mut val = vec![0; len];
1315 src.read_buf(val.as_mut_slice())?;
1316 Ok(IVRRecord::BinaryData(name, val))
1319 let name = Self::read_string(src)?;
1320 let val = Self::read_string(src)?;
1321 Ok(IVRRecord::StringData(name, val))
1323 6 => Ok(IVRRecord::HeaderEnd),
1325 src.read_skip(8)?; // always zero?
1326 Ok(IVRRecord::DataEnd)
1329 src.read_skip(8)?; // always zero?
1330 Ok(IVRRecord::DataStart)
1332 _ => Ok(IVRRecord::Invalid(code)),
1335 fn is_data_start(&self) -> bool {
1337 IVRRecord::DataStart => true,
1343 impl fmt::Display for IVRRecord {
1344 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1346 IVRRecord::Invalid(typ) => write!(f, "Invalid({:02X})", typ),
1347 IVRRecord::StreamProperties(num) =>
1348 write!(f, "({} stream properties)", num),
1349 IVRRecord::Packet { ts, str, flags, len, checksum } =>
1350 write!(f, "paket({}, {}, {:X}, {}, {})", ts, str, flags, len, checksum),
1351 IVRRecord::IntValue(ref name, val) =>
1352 write!(f, "({} = {})", String::from_utf8_lossy(name), val),
1353 IVRRecord::BinaryData(ref name, ref val) =>
1354 write!(f, "({} = {} bytes)", String::from_utf8_lossy(name), val.len()),
1355 IVRRecord::StringData(ref name, ref val) =>
1356 write!(f, "({} = {})", String::from_utf8_lossy(name), String::from_utf8_lossy(val)),
1357 IVRRecord::HeaderEnd => write!(f, "header end"),
1358 IVRRecord::DataEnd => write!(f, "data end"),
1359 IVRRecord::DataStart => write!(f, "data start"),
1364 struct RecordDemuxer {
1368 remap_ids: Vec<u32>,
1371 impl RecordDemuxer {
1372 fn new(pos: u64, start_str: u32) -> Self {
1377 remap_ids: Vec::new(),
1380 fn parse_header(&mut self, src: &mut ByteReader, strmgr: &mut StreamManager, str_data: &mut CommonStreamData) -> DemuxerResult<()> {
1381 src.seek(SeekFrom::Start(self.cur_pos))?;
1382 let magic = src.read_u32be()?;
1383 validate!(magic == mktag!(b".REC"));
1384 let _smth = src.read_byte()?;
1385 let num_entries = src.read_u32be()? as usize;
1386 for _ in 0..num_entries {
1387 let _rec = IVRRecord::read(src)?;
1388 //println!(" header rec {}", _rec);
1390 let mut has_seek_table = false;
1391 let mut cur_str_no = 0;
1393 let rec = IVRRecord::read(src)?;
1395 IVRRecord::HeaderEnd => { break; },
1396 IVRRecord::StreamProperties(num) => {
1397 let stream_no = cur_str_no + self.start_str;
1399 let mut parsed = false;
1400 let mut real_stream_no = 0;
1402 let rec = IVRRecord::read(src)?;
1403 //println!(" strm property {}", rec);
1405 IVRRecord::IntValue(ref name, val) => {
1406 if name == b"StreamNumber\0" {
1407 real_stream_no = val;
1410 IVRRecord::BinaryData(ref name, ref val) => {
1411 if name == b"OpaqueData\0" {
1413 let is_mlti = RMDemuxCommon::parse_stream_info(str_data, strmgr, stream_no, val)?;
1415 str_data.str_ids.push(stream_no);
1420 IVRRecord::StringData(ref name, ref val) => {
1421 if (name == b"SeekType\0") && (val != b"None\0") {
1422 has_seek_table = true;
1425 _ => { return Err(DemuxerError::InvalidData); }
1429 str_data.streams.push(RMStreamType::Unknown);
1430 str_data.str_ids.push(stream_no);
1432 self.remap_ids.push(real_stream_no);
1434 _ => {println!(" unexpected {}", rec); return Err(DemuxerError::InvalidData); }
1437 println!(" now @ {:X}", src.tell());
1438 let off0 = src.read_u32be()? as u64;
1439 let _off1 = src.read_u32be()?;
1440 let _off2 = src.read_u32be()?;
1441 validate!(off0 + self.start_pos == src.tell());
1442 println!(" has seek tab: {}", has_seek_table);
1445 let data_off = src.read_u32be()? as u64;
1446 println!(" new off {:X}", data_off);
1447 let pos = src.tell();
1448 validate!(data_off + self.start_pos > pos);
1449 src.read_skip((data_off + self.start_pos - pos) as usize)?;
1450 let rec = IVRRecord::read(src)?;
1451 validate!(rec.is_data_start());
1453 let ntype = src.peek_byte()?;
1454 validate!((ntype == 2) || (ntype == 7)); // packet or data end, no start
1457 self.cur_pos = src.tell();
1461 fn get_packet(&mut self, src: &mut ByteReader, str_data: &mut CommonStreamData, strmgr: &StreamManager, queued_pkts: &mut Vec<NAPacket>, slice_buf: &mut Vec<u8>) -> DemuxerResult<NAPacket> {
1462 src.seek(SeekFrom::Start(self.cur_pos))?;
1464 let rec = IVRRecord::read(src)?;
1466 IVRRecord::Packet { ts, str, flags, len, .. } => {
1467 let payload_size = len;
1468 let sr = self.remap_ids.iter().position(|x| *x == str);
1469 validate!(sr.is_some());
1470 let str_no = self.start_str + (sr.unwrap() as u32);
1471 let pkt_grp = ((flags >> 8) & 0xFF) as u16;
1472 let stream_id = str_data.get_stream_id(str_no as u32, pkt_grp);
1473 let sr = str_data.find_stream(stream_id);
1475 src.read_skip(payload_size)?;
1476 return Err(DemuxerError::InvalidData);
1478 let str_id = sr.unwrap();
1480 let streamres = strmgr.get_stream_by_id(stream_id);
1481 if streamres.is_none() {
1482 src.read_skip(payload_size)?;
1485 let stream = streamres.unwrap();
1486 if strmgr.is_ignored_id(stream_id) {
1487 src.read_skip(payload_size)?;
1490 let keyframe = false;
1491 let ret = RMDemuxCommon::parse_packet_payload(src, &mut str_data.streams[str_id], stream, slice_buf, queued_pkts, keyframe, ts, payload_size);
1492 if let Err(DemuxerError::TryAgain) = ret {
1495 self.cur_pos = src.tell();
1499 IVRRecord::DataEnd => return Err(DemuxerError::EOF),
1500 _ => return Err(DemuxerError::InvalidData),
1506 struct RealIVRDemuxer<'a> {
1507 src: &'a mut ByteReader<'a>,
1508 recs: Vec<RecordDemuxer>,
1510 queued_pkts: Vec<NAPacket>,
1512 str_data: CommonStreamData,
1515 impl<'a> DemuxCore<'a> for RealIVRDemuxer<'a> {
1516 #[allow(unused_variables)]
1517 fn open(&mut self, strmgr: &mut StreamManager, seek_idx: &mut SeekIndex) -> DemuxerResult<()> {
1518 let magic = self.src.peek_u32be()?;
1519 if magic == mktag!(b".REC") {
1520 let mut rec = RecordDemuxer::new(0, 0);
1521 rec.parse_header(&mut self.src, strmgr, &mut self.str_data)?;
1522 self.recs.push(rec);
1523 } else if magic == mktag!(b".R1M") {
1524 println!("R1M kind");
1525 self.src.read_skip(4)?; // magic
1526 self.src.read_skip(3)?; // always 0, 1, 1 ?
1527 let _name = IVRRecord::read_string(&mut self.src)?;
1528 self.src.read_skip(1)?; // always 0?
1529 let len1 = self.src.read_u32be()? as u64;
1530 let off1 = self.src.read_u64be()?;
1531 let cpos = self.src.tell();
1532 validate!(off1 == len1 + cpos - 8);
1533 self.src.read_skip((off1 - cpos) as usize)?;
1535 let typ = self.src.read_byte()?;
1536 println!(" at {:X} type {:02X}", self.src.tell(), typ);
1539 let len = self.src.read_u32be()?;
1540 self.src.read_skip(len as usize)?;
1543 let len = self.src.read_u32be()? as u64;
1544 let pos = self.src.tell();
1546 let num_streams = self.str_data.streams.len() as u32;
1547 let mut rec = RecordDemuxer::new(pos + 12, num_streams);
1548 rec.parse_header(&mut self.src, strmgr, &mut self.str_data)?;
1549 self.recs.push(rec);
1551 self.src.seek(SeekFrom::Start(pos + len))?;
1554 let mut buf: [u8; 2] = [0; 2];
1555 self.src.peek_buf(&mut buf)?;
1556 if (buf[0] == b'J') && (buf[1] == b'M') { // RJMx markers at the end of file
1559 return Err(DemuxerError::InvalidData);
1562 _ => { return Err(DemuxerError::InvalidData); },
1566 return Err(DemuxerError::InvalidData);
1572 fn get_frame(&mut self, strmgr: &mut StreamManager) -> DemuxerResult<NAPacket> {
1573 if !self.queued_pkts.is_empty() {
1574 let pkt = self.queued_pkts.pop().unwrap();
1578 if self.cur_rec >= self.recs.len() { return Err(DemuxerError::EOF); }
1579 let res = self.recs[self.cur_rec].get_packet(&mut self.src, &mut self.str_data, strmgr, &mut self.queued_pkts, &mut self.slice_buf);
1580 if let Err(DemuxerError::EOF) = res {
1588 #[allow(unused_variables)]
1589 fn seek(&mut self, time: u64, seek_idx: &SeekIndex) -> DemuxerResult<()> {
1594 impl<'a> RealIVRDemuxer<'a> {
1595 fn new(io: &'a mut ByteReader<'a>) -> Self {
1600 queued_pkts: Vec::new(),
1601 slice_buf: Vec::new(),
1602 str_data: CommonStreamData::new(),
1607 static RM_VIDEO_CODEC_REGISTER: &'static [(&[u8;4], &str)] = &[
1608 (b"RV10", "realvideo1"),
1609 (b"RV20", "realvideo2"),
1610 (b"RVTR", "realvideo2"),
1611 (b"RV30", "realvideo3"),
1612 (b"RV40", "realvideo4"),
1613 (b"RV60", "realvideo6"),
1614 (b"CLV1", "clearvideo_rm"),
1617 static RM_AUDIO_CODEC_REGISTER: &'static [(&[u8;4], &str)] = &[
1618 (b"lpcJ", "ra14.4"),
1619 (b"28_8", "ra28.8"),
1623 (b"atrc", "atrac3"),
1629 pub struct RealMediaDemuxerCreator { }
1631 impl DemuxerCreator for RealMediaDemuxerCreator {
1632 fn new_demuxer<'a>(&self, br: &'a mut ByteReader<'a>) -> Box<dyn DemuxCore<'a> + 'a> {
1633 Box::new(RealMediaDemuxer::new(br))
1635 fn get_name(&self) -> &'static str { "realmedia" }
1638 pub struct RealAudioDemuxerCreator { }
1640 impl DemuxerCreator for RealAudioDemuxerCreator {
1641 fn new_demuxer<'a>(&self, br: &'a mut ByteReader<'a>) -> Box<dyn DemuxCore<'a> + 'a> {
1642 Box::new(RealAudioDemuxer::new(br))
1644 fn get_name(&self) -> &'static str { "realaudio" }
1647 pub struct RealIVRDemuxerCreator { }
1649 impl DemuxerCreator for RealIVRDemuxerCreator {
1650 fn new_demuxer<'a>(&self, br: &'a mut ByteReader<'a>) -> Box<dyn DemuxCore<'a> + 'a> {
1651 Box::new(RealIVRDemuxer::new(br))
1653 fn get_name(&self) -> &'static str { "real_ivr" }
1662 fn test_rm_demux() {
1664 File::open("assets/RV/rv10_dnet_640x352_realvideo_encoder_4.0.rm").unwrap();
1665 // File::open("assets/RV/rv20_cook_640x352_realproducer_plus_8.51.rm").unwrap();
1666 // File::open("assets/RV/rv20_svt_atrc_640x352_realproducer_plus_8.51.rm").unwrap();
1667 // File::open("assets/RV/rv30_atrc_384x208_realproducer_plus_8.51.rm").unwrap();
1668 // File::open("assets/RV/rv30_chroma_drift.rm").unwrap();
1669 // File::open("assets/RV/rv30_weighted_mc.rm").unwrap();
1670 // File::open("assets/RV/rv40_weighted_mc.rmvb").unwrap();
1671 // File::open("assets/RV/rv40_weighted_mc_2.rmvb").unwrap();
1672 // File::open("assets/RV/clv1_sipr_384x208_realvideo_encoder_4.0.rm").unwrap();
1673 // File::open("assets/RV/luckynight.rmvb").unwrap();
1674 // File::open("assets/RV/rv40_ralf.rmvb").unwrap();
1675 let mut fr = FileReader::new_read(&mut file);
1676 let mut br = ByteReader::new(&mut fr);
1677 let mut dmx = RealMediaDemuxer::new(&mut br);
1678 let mut sm = StreamManager::new();
1679 let mut si = SeekIndex::new();
1680 dmx.open(&mut sm, &mut si).unwrap();
1683 let pktres = dmx.get_frame(&mut sm);
1684 if let Err(e) = pktres {
1685 if e == DemuxerError::EOF { break; }
1688 let pkt = pktres.unwrap();
1689 println!("Got {}", pkt);
1691 //panic!("the end");
1694 fn test_ra_demux() {
1696 // File::open("assets/RV/welcome288.ra").unwrap();
1697 File::open("assets/RV/diemusik.ra").unwrap();
1698 let mut fr = FileReader::new_read(&mut file);
1699 let mut br = ByteReader::new(&mut fr);
1700 let mut dmx = RealAudioDemuxer::new(&mut br);
1701 let mut sm = StreamManager::new();
1702 let mut si = SeekIndex::new();
1703 dmx.open(&mut sm, &mut si).unwrap();
1706 let pktres = dmx.get_frame(&mut sm);
1707 if let Err(e) = pktres {
1708 if e == DemuxerError::EOF { break; }
1711 let pkt = pktres.unwrap();
1712 println!("Got {}", pkt);
1716 fn test_ivr_demux() {
1718 File::open("assets/RV/Opener_rm_hi.ivr").unwrap();
1719 // File::open("assets/RV/SherwinWilliamsCommercial.ivr").unwrap();
1720 let mut fr = FileReader::new_read(&mut file);
1721 let mut br = ByteReader::new(&mut fr);
1722 let mut dmx = RealIVRDemuxer::new(&mut br);
1723 let mut sm = StreamManager::new();
1724 let mut si = SeekIndex::new();
1725 dmx.open(&mut sm, &mut si).unwrap();
1728 let pktres = dmx.get_frame(&mut sm);
1729 if let Err(e) = pktres {
1730 if e == DemuxerError::EOF { break; }
1733 let pkt = pktres.unwrap();
1734 println!("Got {}", pkt);