2 use super::DemuxerError::*;
10 ($a:expr, $b:expr, $c:expr, $d:expr) => ({
11 (($a as u32) << 24) | (($b as u32) << 16) | (($c as u32) << 8) | ($d as u32)
14 (($arr[0] as u32) << 24) | (($arr[1] as u32) << 16) | (($arr[2] as u32) << 8) | ($arr[3] as u32)
18 struct RMVideoStream {
35 self.frame.truncate(0);
39 fn start_slice(&mut self, num_slices: usize, frame_size: usize, data: &[u8]) {
40 self.hdr_size = num_slices * 8 + 1;
41 self.frame.resize(frame_size + self.hdr_size, 0);
42 self.frame[0] = (num_slices - 1) as u8;
44 self.add_slice(1, data);
46 fn add_slice(&mut self, slice_no: usize, data: &[u8]) {
47 self.write_slice_info(slice_no);
48 let mut dslice = &mut self.frame[self.hdr_size + self.frame_pos..][..data.len()];
49 dslice.copy_from_slice(data);
50 self.frame_pos += data.len();
52 fn write_slice_info(&mut self, slice_no: usize) {
53 let off = 1 + (slice_no - 1) * 8;
54 self.frame[off + 0] = 0;
55 self.frame[off + 1] = 0;
56 self.frame[off + 2] = 0;
57 self.frame[off + 3] = 1;
58 self.frame[off + 4] = (self.frame_pos >> 24) as u8;
59 self.frame[off + 5] = (self.frame_pos >> 16) as u8;
60 self.frame[off + 6] = (self.frame_pos >> 8) as u8;
61 self.frame[off + 7] = (self.frame_pos >> 0) as u8;
63 fn get_frame_data(&mut self) -> Vec<u8> {
64 let mut v: Vec<u8> = Vec::new();
65 mem::swap(&mut v, &mut self.frame);
72 #[derive(Clone,Copy,PartialEq)]
81 struct RMAudioStream {
86 fn new(deint: Deinterleaver) -> Self {
87 RMAudioStream { deint: deint }
98 struct RealMediaDemuxer<'a> {
99 src: &'a mut ByteReader<'a>,
104 streams: Vec<RMStreamType>,
107 queued_pkts: Vec<NAPacket>,
111 fn find_codec_name(registry: &[(&[u8;4], &'static str)], fcc: u32) -> &'static str {
112 for &(fourcc, name) in registry {
113 if mktag!(fourcc) == fcc { return name; }
118 fn read_14or30(src: &mut ByteReader) -> DemuxerResult<(bool, u32)> {
119 let tmp = src.read_u16be()?;
120 let flag = (tmp & 0x8000) != 0;
121 if (tmp & 0x4000) == 0x4000 {
122 Ok((flag, ((tmp & 0x3FFF) as u32)))
124 let val = ((tmp as u32) << 16) | (src.read_u16be()? as u32);
125 Ok((flag, val & 0x3FFFFFFF))
129 fn read_video_buf(src: &mut ByteReader, stream: Rc<NAStream>, ts: u32, keyframe: bool, frame_size: usize) -> DemuxerResult<NAPacket> {
130 let size = (frame_size as usize) + 9;
131 let mut vec: Vec<u8> = Vec::with_capacity(size);
133 //v[0] = 0; // 1 slice
135 src.read_buf(&mut vec[9..])?;
137 let (tb_num, tb_den) = stream.get_timebase();
138 let ts = NATimeInfo::new(Some(ts as u64), None, None, tb_num, tb_den);
139 Ok(NAPacket::new(stream, ts, keyframe, vec))
142 fn read_multiple_frame(src: &mut ByteReader, stream: Rc<NAStream>, keyframe: bool, skip_mtype: bool) -> DemuxerResult<NAPacket> {
144 let mtype = src.read_byte()?;
145 validate!(mtype == 0xC0);
147 let (_, frame_size) = read_14or30(src)?;
148 let (_, timestamp) = read_14or30(src)?;
149 let seq_no = src.read_byte()?;
150 println!(" multiple frame size {} ts {} seq {}", frame_size, timestamp, seq_no);
152 read_video_buf(src, stream, timestamp, keyframe, frame_size as usize)
155 impl<'a> DemuxCore<'a> for RealMediaDemuxer<'a> {
156 #[allow(unused_variables)]
157 fn open(&mut self, strmgr: &mut StreamManager) -> DemuxerResult<()> {
158 self.read_header(strmgr)?;
162 #[allow(unused_variables)]
163 fn get_frame(&mut self, strmgr: &mut StreamManager) -> DemuxerResult<NAPacket> {
164 if !self.queued_pkts.is_empty() {
165 let pkt = self.queued_pkts.pop().unwrap();
169 if self.cur_packet >= self.num_packets { return Err(DemuxerError::EOF); }
171 let pkt_start = self.src.tell();
172 let ver = self.src.read_u16le()?;
174 let len = self.src.read_u16be()? as usize;
175 let str_no = self.src.read_u16be()?;
176 let ts = self.src.read_u32be()?;
179 pkt_grp = self.src.read_byte()?;
181 //asm_rule = self.src.read_u16le()?;
182 self.src.read_skip(2)?;
185 let flags = self.src.read_byte()?;
186 let hdr_size = self.src.tell() - pkt_start;
187 println!("packet @{:X} size {} for {} ts {} grp {} flags {:X}", pkt_start, len, str_no, ts, pkt_grp, flags);
188 self.cur_packet += 1;
190 let payload_size = len - (hdr_size as usize);
192 let sr = self.str_ids.iter().position(|x| *x == str_no);
194 println!("stream {} not found", str_no);
195 self.src.read_skip(payload_size)?;
196 return Err(DemuxerError::InvalidData);
198 let str_id = sr.unwrap();
199 let stream = strmgr.get_stream_by_id(str_no as u32).unwrap();
200 println!(" stream {}", str_id);
201 if strmgr.is_ignored_id(str_no as u32) {
202 self.src.read_skip(payload_size)?;
205 //todo skip unwanted packet
206 let keyframe = (flags & KEYFRAME_FLAG) != 0;
208 let result = match self.streams[str_id] {
209 RMStreamType::Video(ref mut vstr) => {
211 let pos = self.src.tell();
212 let b0 = self.src.read_byte()?;
214 0 => { // partial frame
215 let b1 = self.src.read_byte()?;
216 let hdr1 = ((b0 as u16) << 8) | (b1 as u16);
217 let num_pkts = ((hdr1 >> 7) & 0x7F) as usize;
218 let packet_num = hdr1 & 0x7F;
219 let (_, frame_size) = read_14or30(self.src)?;
220 let (_, off) = read_14or30(self.src)?;
221 let seq_no = self.src.read_byte()?;
222 println!(" mode 0 pkt {}/{} off {}/{} seq {}", packet_num, num_pkts, off, frame_size, seq_no);
223 let hdr_skip = (self.src.tell() - pos) as usize;
225 let slice_size = (payload_size - hdr_skip) as usize;
226 self.slice_buf.resize(slice_size, 0);
227 self.src.read_buf(self.slice_buf.as_mut_slice())?;
229 vstr.start_slice(num_pkts, frame_size as usize, self.slice_buf.as_slice());
231 vstr.add_slice(packet_num as usize, self.slice_buf.as_slice());
235 1 => { // whole frame
236 let seq_no = self.src.read_byte()?;
237 println!(" mode 1 seq {}", seq_no);
238 read_video_buf(self.src, stream, ts, keyframe, payload_size - 1)
240 2 => { // last partial frame
241 let b1 = self.src.read_byte()?;
242 let hdr1 = ((b0 as u16) << 8) | (b1 as u16);
243 let num_pkts = ((hdr1 >> 7) & 0x7F) as usize;
244 let packet_num = hdr1 & 0x7F;
245 let (_, frame_size) = read_14or30(self.src)?;
246 let (_, tail_size) = read_14or30(self.src)?;
247 let seq_no = self.src.read_byte()?;
248 println!(" mode 2 pkt {}/{} tail {}/{} seq {}", packet_num, num_pkts, tail_size, frame_size, seq_no);
249 self.slice_buf.resize(tail_size as usize, 0);
250 self.src.read_buf(self.slice_buf.as_mut_slice())?;
251 vstr.add_slice(packet_num as usize, self.slice_buf.as_slice());
253 while self.src.tell() < pos + (payload_size as u64) {
254 let res = read_multiple_frame(self.src, stream.clone(), false, false);
255 if res.is_err() { break; }
256 self.queued_pkts.push(res.unwrap());
258 self.queued_pkts.reverse();
259 let (tb_num, tb_den) = stream.get_timebase();
260 let ts = NATimeInfo::new(Some(ts as u64), None, None, tb_num, tb_den);
261 let pkt = NAPacket::new(stream, ts, keyframe, vstr.get_frame_data());
264 _ => { // multiple frames
266 let res = read_multiple_frame(self.src, stream.clone(), keyframe, true);
267 if res.is_err() { return res; }
268 while self.src.tell() < pos + (payload_size as u64) {
269 let res = read_multiple_frame(self.src, stream.clone(), false, false);
270 if res.is_err() { break; }
271 self.queued_pkts.push(res.unwrap());
273 self.queued_pkts.reverse();
278 RMStreamType::Audio(ref mut astr) => {
279 let (tb_num, tb_den) = stream.get_timebase();
280 let ts = NATimeInfo::new(Some(ts as u64), None, None, tb_num, tb_den);
281 self.src.read_packet(stream, ts, keyframe, payload_size)
284 // self.src.read_skip(payload_size)?;
285 Err(DemuxerError::InvalidData)
292 #[allow(unused_variables)]
293 fn seek(&mut self, time: u64) -> DemuxerResult<()> {
298 fn read_chunk(src: &mut ByteReader) -> DemuxerResult<(u32, u32, u16)> {
299 let id = src.read_u32be()?;
300 let size = src.read_u32be()?;
301 validate!(size >= 10);
302 let ver = src.read_u16be()?;
307 const RMVB_HDR_SIZE: u32 = 18;
308 const RMVB_PROP_SIZE: u32 = 50;
309 const KEYFRAME_FLAG: u8 = 0x02;
311 impl<'a> RealMediaDemuxer<'a> {
312 fn new(io: &'a mut ByteReader<'a>) -> Self {
320 queued_pkts: Vec::new(),
321 slice_buf: Vec::new(),
324 #[allow(unused_variables)]
325 fn read_header(&mut self, strmgr: &mut StreamManager) -> DemuxerResult<()> {
326 let (id, size, ver) = read_chunk(self.src)?;
327 validate!(id == mktag!(b".RMF"));
328 validate!(size >= RMVB_HDR_SIZE);
329 let fver = self.src.read_u32be()?;
330 validate!(fver <= 1);
331 let num_hdr = self.src.read_u32be()? as usize;
332 validate!(num_hdr >= 1);
333 if size > RMVB_HDR_SIZE {
334 self.src.read_skip((size - RMVB_HDR_SIZE) as usize)?;
337 let (id, size, ver) = read_chunk(self.src)?;
338 validate!(size >= RMVB_PROP_SIZE);
340 let maxbr = self.src.read_u32be()?;
341 let avgbr = self.src.read_u32be()?;
342 let maxps = self.src.read_u32be()?;
343 let avgps = self.src.read_u32be()?;
344 let num_pkt = self.src.read_u32be()? as usize;
345 let duration = self.src.read_u32be()?;
346 let preroll = self.src.read_u32be()?;
347 let idx_off = self.src.read_u32be()?;
348 let data_off = self.src.read_u32be()?;
349 let num_streams = self.src.read_u16be()? as usize;
350 let flags = self.src.read_u16be()?;
351 if size > RMVB_PROP_SIZE {
352 self.src.read_skip((size - RMVB_PROP_SIZE) as usize)?;
355 for _ in 0..num_hdr {
356 self.parse_chunk(strmgr)?;
358 println!("now @ {:X} / {}", self.src.tell(), self.data_pos);
359 validate!(self.data_pos > 0);
360 self.src.seek(SeekFrom::Start(self.data_pos))?;
361 let num_packets = self.src.read_u32be()?;
362 let next_data_hdr = self.src.read_u32be()?;
363 self.num_packets = num_packets;
367 fn parse_chunk(&mut self, strmgr: &mut StreamManager) -> DemuxerResult<()> {
368 let (id, size, ver) = read_chunk(self.src)?;
369 let end_pos = self.src.tell() - 10 + (size as u64);
372 if id == mktag!(b"CONT") { self.parse_content_desc()?; }
373 else if id == mktag!(b"MDPR") { self.parse_mdpr(strmgr)?; }
374 else if id == mktag!(b"DATA") { if self.data_pos == 0 { self.data_pos = self.src.tell(); } }
375 else if id == mktag!(b"INDX") { /* do nothing for now */ }
376 else { println!("unknown chunk type {:08X}", id); }
378 let cpos = self.src.tell();
380 self.src.read_skip((end_pos - cpos) as usize)?;
384 #[allow(unused_variables)]
385 fn parse_content_desc(&mut self) -> DemuxerResult<()> {
386 let title_len = self.src.read_u16be()? as usize;
387 self.src.read_skip(title_len)?;
388 let author_len = self.src.read_u16be()? as usize;
389 self.src.read_skip(author_len)?;
390 let copywrong_len = self.src.read_u16be()? as usize;
391 self.src.read_skip(copywrong_len)?;
392 let comment_len = self.src.read_u16be()? as usize;
393 self.src.read_skip(comment_len)?;
396 #[allow(unused_variables)]
397 fn parse_mdpr(&mut self, strmgr: &mut StreamManager) -> DemuxerResult<()> {
398 let stream_no = self.src.read_u16be()?;
399 //todo check stream_no for duplicates
400 let maxbr = self.src.read_u32be()?;
401 let avgbr = self.src.read_u32be()?;
402 let maxps = self.src.read_u32be()?;
403 let avgps = self.src.read_u32be()?;
404 let start = self.src.read_u32be()?;
405 let preroll = self.src.read_u32be()?;
406 let duration = self.src.read_u32be()?;
407 let sname_size = self.src.read_byte()? as usize;
408 let sname = read_string_size(self.src, sname_size)?;
409 println!("str #{} sname = {} pkts {}/{} start {} preroll {}", stream_no, sname, maxps, avgps, start, preroll);
410 let mime_size = self.src.read_byte()? as usize;
411 let mime = read_string_size(self.src, mime_size)?;
412 println!("mime = {}", mime);
413 let edata_size = self.src.read_u32be()? as usize;
414 let edata: Option<Vec<u8>> = if edata_size == 0 { None } else {
415 let mut edvec: Vec<u8> = Vec::with_capacity(edata_size);
416 edvec.resize(edata_size, 0);
417 self.src.read_buf(&mut edvec)?;
420 self.str_ids.push(stream_no);
422 if let Some(edata_) = edata {
423 let mut mr = MemoryReader::new_read(edata_.as_slice());
424 let mut src = ByteReader::new(&mut mr);
426 let tag = src.read_u32be()?;
427 let tag2 = src.peek_u32be()?;
428 println!("tag1 {:X} tag2 {:X}", tag, tag2);
429 if tag == mktag!('.', 'r', 'a', 0xFD) {
431 let cname = "unknown";//find_codec_name(RM_AUDIO_CODEC_REGISTER, fcc);
432 let soniton = NASoniton::new(16, SONITON_FLAG_SIGNED);
433 let ahdr = NAAudioInfo::new(44100, 2, soniton, 1/*block_align as usize*/);
434 let extradata = None;
435 let ainfo = NACodecInfo::new(cname, NACodecTypeInfo::Audio(ahdr), extradata);
436 let res = strmgr.add_stream(NAStream::new(StreamType::Audio, stream_no as u32, ainfo, 1, 44100));
437 if res.is_none() { return Err(MemoryError); }
439 let astr = RMAudioStream::new(Deinterleaver::None);
440 self.streams.push(RMStreamType::Audio(astr));
441 } else if ((tag2 == mktag!('V', 'I', 'D', 'O')) || (tag2 == mktag!('I', 'M', 'A', 'G'))) && ((tag as usize) <= edata_size) {
443 let fcc = src.read_u32be()?;
444 let width = src.read_u16be()? as usize;
445 let height = src.read_u16be()? as usize;
446 let bpp = src.read_u16be()?;
447 let pad_w = src.read_u16be()?;
448 let pad_h = src.read_u16be()?;
450 if tag2 == mktag!('V', 'I', 'D', 'O') {
451 fps = src.read_u32be()?;
455 let extradata: Option<Vec<u8>>;
457 let eslice = &edata_[(src.tell() as usize)..];
458 extradata = Some(eslice.to_vec());
462 let cname = find_codec_name(RM_VIDEO_CODEC_REGISTER, fcc);
464 let vhdr = NAVideoInfo::new(width, height, false, RGB24_FORMAT);
465 let vinfo = NACodecInfo::new(cname, NACodecTypeInfo::Video(vhdr), extradata);
466 let res = strmgr.add_stream(NAStream::new(StreamType::Video, stream_no as u32, vinfo, 0x10000, fps));
467 if res.is_none() { return Err(DemuxerError::MemoryError); }
469 let vstr = RMVideoStream::new();
470 self.streams.push(RMStreamType::Video(vstr));
472 self.streams.push(RMStreamType::Logical);
476 self.streams.push(RMStreamType::Unknown);
481 /*#[allow(unused_variables)]
482 fn read_pkt_header(&mut self) -> DemuxerResult<()> {
483 let ver = self.src.read_u16be()?;
485 let str_no = self.src.read_u16be()?;
486 let timestamp = self.src.read_u32be()?;
488 let pkt_group = self.src.read_byte()?;
489 let pkt_flags = self.src.read_byte()?;
491 let asm_rule = self.src.read_u16be()?;
492 let asm_flags = self.src.read_byte()?;
498 fn read_string(src: &mut ByteReader) -> DemuxerResult<String> {
499 let mut vec: Vec<u8> = Vec::new();
501 let c = src.read_byte()?;
505 let str = String::from_utf8(vec);
513 fn read_string_size(src: &mut ByteReader, size: usize) -> DemuxerResult<String> {
514 let mut vec: Vec<u8> = Vec::new();
516 let c = src.read_byte()?;
519 let str = String::from_utf8(vec);
528 #[allow(unused_variables)]
529 fn parse_rm_stream(io: &mut ByteReader) -> DemuxerResult<NAStream> {
530 let mimetype = read_string(io)?;
531 let strname = read_string(io)?;
532 let strnum = io.read_u32le()?;
533 let maxbr = io.read_u32le()?;
534 let avgbr = io.read_u32le()?;
535 let maxsize = io.read_u32le()?;
536 let avgsize = io.read_u32le()?;
537 let duration = io.read_u32le()?;
538 let preroll = io.read_u32le()?;
539 let start = io.read_u32le()?;
540 let edatalen = io.read_u32le()? as usize;
541 let mut edata: Vec<u8> = Vec::with_capacity(edatalen);
542 edata.resize(edatalen, 0);
543 io.read_buf(&mut edata)?;
544 let numprops = io.read_u32le()? as usize;
550 #[allow(unused_variables)]
551 fn read_ra_vbr_stream(io: &mut ByteReader) -> DemuxerResult<NAPacket> {
552 let hdrsizesize = io.read_u16le()?;
553 let num_entries = (hdrsizesize / 16) as usize;
554 let mut sizes: Vec<usize> = Vec::with_capacity(num_entries);
555 for _ in 0..num_entries {
556 let sz = io.read_u16le()? as usize;
559 for i in 0..num_entries {
560 //read packet of sizes[i]
570 static RM_VIDEO_CODEC_REGISTER: &'static [(&[u8;4], &str)] = &[
571 (b"RV10", "realvideo1"),
572 (b"RV20", "realvideo2"),
573 (b"RVTR", "realvideo2"),
574 (b"RV30", "realvideo3"),
575 (b"RV40", "realvideo4"),
576 (b"CLV1", "clearvideo"),
580 static RM_AUDIO_CODEC_REGISTER: &'static [(&[u8;4], &str)] = &[
592 pub struct RealMediaDemuxerCreator { }
594 impl DemuxerCreator for RealMediaDemuxerCreator {
595 fn new_demuxer<'a>(&self, br: &'a mut ByteReader<'a>) -> Box<DemuxCore<'a> + 'a> {
596 Box::new(RealMediaDemuxer::new(br))
598 fn get_name(&self) -> &'static str { "realmedia" }
609 File::open("assets/RV/rv10_dnet_640x352_realvideo_encoder_4.0.rm").unwrap();
610 // File::open("assets/RV/rv20_cook_640x352_realproducer_plus_8.51.rm").unwrap();
611 // File::open("assets/RV/rv20_svt_atrc_640x352_realproducer_plus_8.51.rm").unwrap();
612 // File::open("assets/RV/rv30_atrc_384x208_realproducer_plus_8.51.rm").unwrap();
613 // File::open("assets/RV/rv30_chroma_drift.rm").unwrap();
614 // File::open("assets/RV/rv30_weighted_mc.rm").unwrap();
615 // File::open("assets/RV/rv40_weighted_mc.rmvb").unwrap();
616 // File::open("assets/RV/rv40_weighted_mc_2.rmvb").unwrap();
617 // File::open("assets/RV/clv1_sipr_384x208_realvideo_encoder_4.0.rm").unwrap();
618 // File::open("assets/RV/luckynight.rmvb").unwrap();
619 // File::open("assets/RV/rv40_ralf.rmvb").unwrap();
620 let mut fr = FileReader::new_read(&mut file);
621 let mut br = ByteReader::new(&mut fr);
622 let mut dmx = RealMediaDemuxer::new(&mut br);
623 let mut sm = StreamManager::new();
624 dmx.open(&mut sm).unwrap();
627 let pktres = dmx.get_frame(&mut sm);
628 if let Err(e) = pktres {
629 if e == DemuxerError::EOF { break; }
632 let pkt = pktres.unwrap();
633 println!("Got {}", pkt);