]> git.nihav.org Git - nihav.git/blob - nihav-realmedia/src/demuxers/realmedia.rs
1fa52f967283329203518355587c770aaa5cfeb7
[nihav.git] / nihav-realmedia / src / demuxers / realmedia.rs
1 use super::*;
2 use nihav_core::demuxers::DemuxerError::*;
3 use std::io::SeekFrom;
4 use std::mem;
5 use std::fmt;
6
7 macro_rules! mktag {
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)
10 });
11 ($arr:expr) => ({
12 (($arr[0] as u32) << 24) | (($arr[1] as u32) << 16) | (($arr[2] as u32) << 8) | ($arr[3] as u32)
13 });
14 }
15
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 ]
28 ];
29
30 struct RMVideoStream {
31 frame: Vec<u8>,
32 hdr_size: usize,
33 frame_size: usize,
34 frame_pos: usize,
35 }
36
37 impl RMVideoStream {
38 fn new() -> Self {
39 RMVideoStream {
40 frame: Vec::new(),
41 hdr_size: 0,
42 frame_size: 0,
43 frame_pos: 0,
44 }
45 }
46 fn flush(&mut self) {
47 self.frame.truncate(0);
48 self.frame_size = 0;
49 self.frame_pos = 0;
50 }
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;
55 self.frame_pos = 0;
56 self.add_slice(1, data).unwrap();
57 }
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();
64 Ok(())
65 }
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;
76 }
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);
80 self.flush();
81 v
82 }
83 }
84
85 #[derive(Clone,Copy,PartialEq)]
86 enum Deinterleaver {
87 None,
88 RA28_8,
89 Generic,
90 Sipro,
91 VBR,
92 }
93
94 struct RMAudioStream {
95 deint: Deinterleaver,
96 iinfo: Option<InterleaveInfo>,
97 buf: Vec<u8>,
98 sub_packet: usize,
99 }
100
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");
107
108 impl RMAudioStream {
109 fn new(iinfo: Option<InterleaveInfo>) -> Self {
110 let deint;
111 let buf: Vec<u8>;
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 },
121 };
122 match deint {
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];
129 },
130 Deinterleaver::VBR => {
131 buf = Vec::new();
132 },
133 };
134 } else {
135 deint = Deinterleaver::None;
136 buf = Vec::new();
137 }
138 RMAudioStream { deint, iinfo, buf, sub_packet: 0 }
139 }
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);
143
144 if keyframe {
145 self.sub_packet = 0;
146 }
147 match self.deint {
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)?;
159 }
160 self.sub_packet += 1;
161 if self.sub_packet == factor {
162 self.sub_packet = 0;
163 return Ok(NAPacket::new(stream, ts, true, self.buf.clone()));
164 } else {
165 return Err(DemuxerError::TryAgain);
166 }
167 },
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;
175
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)?;
180 }
181 },
182 Deinterleaver::Sipro => {
183 let iinfo = self.iinfo.unwrap();
184 let fsize = iinfo.frame_size as usize;
185 let ppos = self.sub_packet;
186
187 let mut dst = &mut self.buf[fsize * ppos..][..fsize];
188 src.read_buf(&mut dst)?;
189 },
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;
199 tot_size += sz;
200 sizes.push(sz);
201 }
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 };
207 first = false;
208 let pkt = src.read_packet(stream.clone(), cur_ts, true, *size)?;
209 queued_packets.push(pkt);
210 }
211 queued_packets.reverse();
212 let pkt0 = queued_packets.pop().unwrap();
213 return Ok(pkt0);
214 },
215 };
216
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;
220
221 self.sub_packet += 1;
222 if self.sub_packet < factor {
223 return Err(DemuxerError::TryAgain);
224 }
225
226 self.sub_packet = 0;
227
228 if self.deint == Deinterleaver::Sipro {
229 sipro_restore(&mut self.buf, factor, iinfo.frame_size as usize);
230 }
231
232 let mut frames_iter = self.buf.chunks(fsize);
233 let pkt0 = frames_iter.next().unwrap();
234
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);
239 }
240 queued_packets.reverse();
241 Ok(NAPacket::new(stream, ts, true, pkt0.to_vec()))
242 }
243 }
244
245 fn sipro_restore(buf: &mut [u8], factor: usize, fsize: usize) {
246 let stride = factor * fsize * 2 / 96;
247 for i in 0..38 {
248 let mut sidx = (RM_SIPRO_SWAPS[i][0] as usize) * stride;
249 let mut didx = (RM_SIPRO_SWAPS[i][1] as usize) * stride;
250 for _ in 0..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;
255
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))));
258
259 sidx += 1;
260 didx += 1;
261 }
262 }
263 }
264
265 struct SubstreamInfo {
266 id: u32,
267 map: Vec<u16>,
268 str_ids: Vec<u32>,
269 }
270
271 struct MLTIMapper {
272 sub_info: Vec<SubstreamInfo>,
273 sstr_id: u32,
274 }
275
276 impl MLTIMapper {
277 fn new() -> Self {
278 MLTIMapper {
279 sub_info: Vec::new(),
280 sstr_id: 0x10000,
281 }
282 }
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);
286 }
287 fn get_substream_no(&self) -> u32 {
288 self.sstr_id - 1
289 }
290 fn find_idx(&self, stream_no: u32) -> Option<usize> {
291 self.sub_info.iter().position(|x| x.id == stream_no)
292 }
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);
296 }
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);
300 self.sstr_id += 1;
301 }
302 fn is_mlti_stream(&self, stream_no: u32) -> bool {
303 self.find_idx(stream_no).is_some()
304 }
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]);
311 }
312 }
313 }
314 None
315 }
316 }
317
318 enum RMStreamType {
319 Audio(RMAudioStream),
320 Video(RMVideoStream),
321 Logical,
322 Unknown,
323 }
324
325 struct CommonStreamData {
326 streams: Vec<RMStreamType>,
327 str_ids: Vec<u32>,
328 mlti_mapper: MLTIMapper,
329 }
330
331 impl CommonStreamData {
332 fn new() -> Self {
333 CommonStreamData {
334 streams: Vec::new(),
335 str_ids: Vec::new(),
336 mlti_mapper: MLTIMapper::new(),
337 }
338 }
339 fn get_stream_id(&self, str_no: u32, pkt_grp: u16) -> u32 {
340 if !self.mlti_mapper.is_mlti_stream(str_no) {
341 str_no
342 } else {
343 self.mlti_mapper.find_substream(str_no, pkt_grp).unwrap()
344 }
345 }
346 fn find_stream(&self, stream_id: u32) -> Option<usize> {
347 self.str_ids.iter().position(|x| *x == stream_id)
348 }
349 }
350
351 struct RealMediaDemuxer<'a> {
352 src: &'a mut ByteReader<'a>,
353 data_pos: u64,
354 data_end: u64,
355 next_data: u64,
356 data_ver: u16,
357 num_packets: u32,
358 cur_packet: u32,
359
360 str_data: CommonStreamData,
361
362 data_chunks: Vec<(u64, u32, u16)>,
363 cur_data_chunk: usize,
364
365 queued_pkts: Vec<NAPacket>,
366 slice_buf: Vec<u8>,
367 }
368
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; }
372 }
373 "unknown"
374 }
375
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)))
381 } else {
382 let val = ((tmp as u32) << 16) | (src.read_u16be()? as u32);
383 Ok((flag, val & 0x3FFFFFFF))
384 }
385 }
386
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
391 vec[4] = 1;
392 src.read_buf(&mut vec[9..])?;
393
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))
397 }
398
399 fn read_multiple_frame(src: &mut ByteReader, stream: NAStreamRef, keyframe: bool, skip_mtype: bool) -> DemuxerResult<NAPacket> {
400 if !skip_mtype {
401 let mtype = src.read_byte()?;
402 validate!(mtype == 0xC0);
403 }
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);
408
409 read_video_buf(src, stream, timestamp, keyframe, frame_size as usize)
410 }
411
412 struct RMDemuxCommon {}
413
414 impl 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());
428
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()?;
433
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") {
443 is_mlti = true;
444 let num_rules = src.read_u16be()? as usize;
445 let mut max_sub = 0;
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);
451 }
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();
460 {
461 let hdrsrc = &edata[pos..][..hdr_size];
462 let mut mr = MemoryReader::new_read(hdrsrc);
463 let mut hsrc = ByteReader::new(&mut mr);
464
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)?;
473 } else {
474 println!("unknown MLTI substream {:08X} / {:08X}", tag, tag2);
475 return Err(DemuxerError::InvalidData);
476 }
477 str_data.str_ids.push(stream_no);
478 }
479 }
480 } else {
481 str_data.streams.push(RMStreamType::Logical);
482 }
483 Ok(is_mlti)
484 }
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 {
488 3 => {
489 parse_aformat3(src)?
490 },
491 4 => {
492 parse_aformat4(src)?
493 },
494 5 => {
495 parse_aformat5(src)?
496 },
497 _ => {
498 println!("unknown version {}", ver);
499 return Err(DemuxerError::InvalidData);
500 },
501 };
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
506 } else {
507 validate!(ainfo.flavor <= 3);
508 RM_SIPRO_BLOCK_SIZES[ainfo.flavor as usize]
509 };
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 {
514 None
515 } else {
516 let eslice = &edata_[(src.tell() as usize)..];
517 Some(eslice.to_vec())
518 };
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); }
522
523 let astr = RMAudioStream::new(ainfo.ileave_info);
524 streams.push(RMStreamType::Audio(astr));
525 Ok(())
526 }
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<()> {
529 src.read_skip(4)?;
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()?;
536 let _fps;
537 if tag2 == mktag!('V', 'I', 'D', 'O') {
538 _fps = src.read_u32be()?;
539 } else {
540 _fps = 0x10000;
541 }
542 let extradata: Option<Vec<u8>>;
543 if src.left() > 0 {
544 let eslice = &edata_[(src.tell() as usize)..];
545 extradata = Some(eslice.to_vec());
546 } else {
547 extradata = None;
548 }
549 let cname = find_codec_name(RM_VIDEO_CODEC_REGISTER, fcc);
550
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); }
555
556 let vstr = RMVideoStream::new();
557 streams.push(RMStreamType::Video(vstr));
558 Ok(())
559 }
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> {
562 match rmstream {
563 RMStreamType::Video(ref mut vstr) => {
564
565 let pos = src.tell();
566 let b0 = src.read_byte()?;
567 match b0 >> 6 {
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;
578
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())?;
582 if packet_num == 1 {
583 vstr.start_slice(num_pkts, frame_size as usize, slice_buf.as_slice());
584 } else {
585 vstr.add_slice(packet_num as usize, slice_buf.as_slice())?;
586 }
587 if (packet_num as usize) < num_pkts {
588 return Err(DemuxerError::TryAgain);
589 }
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());
594 Ok(pkt)
595 },
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)
600 },
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());
614 } else {
615 vstr.add_slice(packet_num as usize, slice_buf.as_slice())?;
616 }
617
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());
622 }
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());
627 Ok(pkt)
628 },
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());
637 }
638 queued_pkts.reverse();
639 res
640 },
641 }
642 },
643 RMStreamType::Audio(ref mut astr) => {
644 astr.read_apackets(queued_pkts, src, stream, ts, keyframe, payload_size)
645 },
646 _ => {
647 src.read_skip(payload_size)?;
648 Err(DemuxerError::InvalidData)
649 },
650 }
651 }
652 }
653
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)?;
657 Ok(())
658 }
659
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();
664 return Ok(pkt);
665 }
666 loop {
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();
675 self.data_ver = ver;
676 self.data_end = self.data_pos + (size as u64);
677 if self.parse_data_start().is_ok() {
678 continue;
679 }
680 }
681 }
682 return Err(DemuxerError::EOF);
683 }
684
685 let pkt_start = self.src.tell();
686 let ver = self.src.read_u16be()?;
687 validate!(ver <= 1);
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()?;
691 let pkt_grp;
692 let flags;
693 if ver == 0 {
694 pkt_grp = self.src.read_byte()? as u16;
695 flags = self.src.read_byte()?;
696 } else {
697 //asm_rule = self.src.read_u16be()?;
698 //asm_flags = self.src.read_byte()?;
699 self.src.read_skip(2)?;
700 pkt_grp = 0;
701 self.src.read_skip(1)?;
702 flags = 0;
703 }
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;
707
708 let payload_size = len - (hdr_size as usize);
709
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);
712 if sr.is_none() {
713 //println!("stream {} not found", str_no);
714 self.src.read_skip(payload_size)?;
715 return Err(DemuxerError::InvalidData);
716 }
717 let str_id = sr.unwrap();
718
719 let streamres = strmgr.get_stream_by_id(stream_id);
720 if streamres.is_none() {
721 self.src.read_skip(payload_size)?;
722 continue;
723 }
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)?;
728 continue;
729 }
730 //todo skip unwanted packet
731 let keyframe = (flags & KEYFRAME_FLAG) != 0;
732
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 {
735 continue;
736 } else {
737 return ret;
738 }
739 }
740 }
741
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);
746 if ret.is_none() {
747 return Err(DemuxerError::SeekError);
748 }
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);
754 if seek_pos < end {
755 self.cur_packet = 0;
756 self.data_pos = seek_pos;
757 self.data_ver = *ver;
758 self.data_end = end;
759 self.src.seek(SeekFrom::Start(seek_pos))?;
760 return Ok(());
761 }
762 }
763 Err(DemuxerError::SeekError)
764 }
765 }
766
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()?;
771 if size == 0 {
772 let ver = src.read_u16be()?;
773 validate!(ver <= 2);
774 return Ok((id, 0x0FFFFFFF, ver));
775 }
776 validate!(size >= 10);
777 let ver = src.read_u16be()?;
778 validate!(ver <= 2);
779 Ok((id, size, ver))
780 }
781
782 #[derive(Clone,Copy,Debug)]
783 struct InterleaveInfo {
784 id: u32,
785 factor: u16,
786 block_size: u16,
787 frame_size: u16,
788 }
789
790 #[derive(Clone,Copy,Debug)]
791 struct RealAudioInfo {
792 fcc: u32,
793 sample_rate: u32,
794 sample_size: u16,
795 flavor: u16,
796 channels: u16,
797 channel_mask: u32,
798 granularity: u32,
799 bytes_per_minute: u32,
800 total_bytes: u32,
801 edata_size: u32,
802 ileave_info: Option<InterleaveInfo>
803 }
804
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)?;
814 Ok(())
815 }
816
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()?;
826
827 skip_ra_metadata(src)?;
828
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()?;
833
834 let end = src.tell();
835 validate!(end - start <= (header_len as u64) + 2);
836
837 Ok(RealAudioInfo {
838 fcc, flavor,
839 sample_rate: 8000, sample_size: 16, channels: 1, channel_mask: 0,
840 granularity, bytes_per_minute,
841 total_bytes, edata_size: 0,
842 ileave_info: None,
843 })
844 }
845
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()?;
875
876 skip_ra_metadata(src)?;
877
878 let end = src.tell();
879 validate!(end - start <= (header_size as u64) + 10);
880
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,
885 })
886 } else {
887 None
888 };
889
890 Ok(RealAudioInfo {
891 fcc, flavor,
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,
895 ileave_info,
896 })
897 }
898
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");
929 }
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)?;
935 } else {
936 validate!(end - start <= (header_size as u64) + 15);
937 }
938
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,
942 })
943 } else {
944 None
945 };
946 if (fcc == mktag!(b"raac")) || (fcc == mktag!(b"racp")) {
947 validate!(edata_size > 1);
948 edata_size -= 1;
949 src.read_skip(1)?;
950 }
951
952 Ok(RealAudioInfo {
953 fcc, flavor,
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,
957 ileave_info,
958 })
959 }
960
961 const RMVB_HDR_SIZE: u32 = 18;
962 const RMVB_PROP_SIZE: u32 = 50;
963 const KEYFRAME_FLAG: u8 = 0x02;
964
965 impl<'a> RealMediaDemuxer<'a> {
966 fn new(io: &'a mut ByteReader<'a>) -> Self {
967 RealMediaDemuxer {
968 src: io,
969 data_pos: 0,
970 data_end: 0,
971 next_data: 0,
972 data_ver: 0,
973 num_packets: 0,
974 cur_packet: 0,
975 str_data: CommonStreamData::new(),
976 data_chunks: Vec::new(),
977 cur_data_chunk: 0,
978 queued_pkts: Vec::new(),
979 slice_buf: Vec::new(),
980 }
981 }
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)?;
993 }
994
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()?;
1006 if ver == 2 {
1007 self.src.read_skip(4)?;
1008 }
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)?;
1015 }
1016
1017 for _ in 0..num_hdr {
1018 if self.src.is_eof() {
1019 //warn maybe?
1020 break;
1021 }
1022 let res = self.parse_chunk(strmgr, seek_idx);
1023 match res {
1024 Ok(last) => { if last { break; } },
1025 Err(DemuxerError::IOError) => { break; },
1026 Err(etype) => {
1027 if self.data_chunks.len() == 0 { // data is not found, report error
1028 return Err(etype);
1029 }
1030 },
1031 };
1032 }
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()?;
1042 Ok(())
1043 }
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?
1048 }
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;
1053 Ok(())
1054 }
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);
1058
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));
1064 }
1065 else if id == mktag!(b"INDX") {
1066 if !seek_idx.skip_index {
1067 self.parse_index(seek_idx, (size as usize) - 10, ver)?;
1068 }
1069 }
1070 else if id == 0 { return Ok(true); }
1071 else { println!("unknown chunk type {:08X}", id); }
1072
1073 let cpos = self.src.tell();
1074 if cpos < end_pos {
1075 self.src.read_skip((end_pos - cpos) as usize)?;
1076 }
1077 Ok(false)
1078 }
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)?;
1089 Ok(())
1090 }
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)?;
1112 Some(edvec)
1113 };
1114 let mut is_mlti = false;
1115 if edata_size > 8 {
1116 if let Some(edata_) = edata {
1117 is_mlti = RMDemuxCommon::parse_stream_info(&mut self.str_data, strmgr, stream_no, &edata_)?;
1118 }
1119 } else {
1120 self.str_data.streams.push(RMStreamType::Unknown);
1121 }
1122 if !is_mlti {
1123 self.str_data.str_ids.push(stream_no);
1124 }
1125
1126 Ok(())
1127 }
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(()); }
1135
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 });
1145 }
1146 idx.filled = true;
1147 seek_idx.mode = SeekIndexMode::Present;
1148 Ok(())
1149 }
1150 }
1151
1152 fn read_string_size(src: &mut ByteReader, size: usize) -> DemuxerResult<String> {
1153 let mut vec: Vec<u8> = Vec::new();
1154 for _ in 0..size {
1155 let c = src.read_byte()?;
1156 vec.push(c);
1157 }
1158 let str = String::from_utf8(vec);
1159 if str.is_ok() {
1160 Ok(str.unwrap())
1161 } else {
1162 Ok(String::new())
1163 }
1164 }
1165
1166 struct RealAudioDemuxer<'a> {
1167 src: &'a mut ByteReader<'a>,
1168 stream: Option<RMAudioStream>,
1169 data_start: u64,
1170 data_end: u64,
1171 blk_size: usize,
1172 queued_pkts: Vec<NAPacket>,
1173 }
1174
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 {
1182 3 => {
1183 parse_aformat3(&mut self.src)?
1184 },
1185 4 => {
1186 parse_aformat4(&mut self.src)?
1187 },
1188 5 => {
1189 parse_aformat5(&mut self.src)?
1190 },
1191 _ => {
1192 println!("unknown version {}", ver);
1193 return Err(DemuxerError::InvalidData);
1194 },
1195 };
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
1200 } else {
1201 validate!(ainfo.flavor <= 3);
1202 RM_SIPRO_BLOCK_SIZES[ainfo.flavor as usize]
1203 };
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 {
1208 None
1209 } else {
1210 let mut dta: Vec<u8> = vec![0; ainfo.edata_size as usize];
1211 self.src.read_buf(dta.as_mut_slice())?;
1212 Some(dta)
1213 };
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); }
1217
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);
1223
1224 Ok(())
1225 }
1226
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();
1230 return Ok(pkt);
1231 }
1232 if (self.data_end != 0) && (self.src.tell() >= self.data_end) {
1233 return Err(DemuxerError::EOF);
1234 }
1235 let streamres = strmgr.get_stream_by_id(0);
1236 let stream = streamres.unwrap();
1237 if let Some(ref mut astr) = self.stream {
1238 loop {
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 {
1241 continue;
1242 }
1243 return ret;
1244 }
1245 }
1246 Err(DemuxerError::NoSuchInput)
1247 }
1248
1249 #[allow(unused_variables)]
1250 fn seek(&mut self, time: u64, seek_idx: &SeekIndex) -> DemuxerResult<()> {
1251 Err(NotImplemented)
1252 }
1253 }
1254
1255 impl<'a> RealAudioDemuxer<'a> {
1256 fn new(io: &'a mut ByteReader<'a>) -> Self {
1257 RealAudioDemuxer {
1258 src: io,
1259 data_start: 0,
1260 data_end: 0,
1261 blk_size: 0,
1262 stream: None,
1263 queued_pkts: Vec::new(),
1264 }
1265 }
1266 }
1267
1268 enum IVRRecord {
1269 Invalid(u8),
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>),
1275 HeaderEnd,
1276 DataStart,
1277 DataEnd,
1278 }
1279
1280 impl IVRRecord {
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())?;
1285 Ok(val)
1286 }
1287
1288 fn read(src: &mut ByteReader) -> DemuxerResult<Self> {
1289 let code = src.read_byte()?;
1290 match code {
1291 1 => {
1292 let val = src.read_u32be()? as usize;
1293 Ok(IVRRecord::StreamProperties(val))
1294 },
1295 2 => {
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 })
1303 },
1304 3 => {
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))
1310 },
1311 4 => {
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))
1317 },
1318 5 => {
1319 let name = Self::read_string(src)?;
1320 let val = Self::read_string(src)?;
1321 Ok(IVRRecord::StringData(name, val))
1322 },
1323 6 => Ok(IVRRecord::HeaderEnd),
1324 7 => {
1325 src.read_skip(8)?; // always zero?
1326 Ok(IVRRecord::DataEnd)
1327 },
1328 8 => {
1329 src.read_skip(8)?; // always zero?
1330 Ok(IVRRecord::DataStart)
1331 },
1332 _ => Ok(IVRRecord::Invalid(code)),
1333 }
1334 }
1335 fn is_data_start(&self) -> bool {
1336 match *self {
1337 IVRRecord::DataStart => true,
1338 _ => false,
1339 }
1340 }
1341 }
1342
1343 impl fmt::Display for IVRRecord {
1344 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1345 match *self {
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"),
1360 }
1361 }
1362 }
1363
1364 struct RecordDemuxer {
1365 start_pos: u64,
1366 cur_pos: u64,
1367 start_str: u32,
1368 remap_ids: Vec<u32>,
1369 }
1370
1371 impl RecordDemuxer {
1372 fn new(pos: u64, start_str: u32) -> Self {
1373 RecordDemuxer {
1374 start_pos: pos,
1375 cur_pos: pos,
1376 start_str,
1377 remap_ids: Vec::new(),
1378 }
1379 }
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);
1389 }
1390 let mut has_seek_table = false;
1391 let mut cur_str_no = 0;
1392 loop {
1393 let rec = IVRRecord::read(src)?;
1394 match rec {
1395 IVRRecord::HeaderEnd => { break; },
1396 IVRRecord::StreamProperties(num) => {
1397 let stream_no = cur_str_no + self.start_str;
1398 cur_str_no += 1;
1399 let mut parsed = false;
1400 let mut real_stream_no = 0;
1401 for _ in 0..num {
1402 let rec = IVRRecord::read(src)?;
1403 //println!(" strm property {}", rec);
1404 match rec {
1405 IVRRecord::IntValue(ref name, val) => {
1406 if name == b"StreamNumber\0" {
1407 real_stream_no = val;
1408 }
1409 },
1410 IVRRecord::BinaryData(ref name, ref val) => {
1411 if name == b"OpaqueData\0" {
1412 validate!(!parsed);
1413 let is_mlti = RMDemuxCommon::parse_stream_info(str_data, strmgr, stream_no, val)?;
1414 if !is_mlti {
1415 str_data.str_ids.push(stream_no);
1416 }
1417 parsed = true;
1418 }
1419 },
1420 IVRRecord::StringData(ref name, ref val) => {
1421 if (name == b"SeekType\0") && (val != b"None\0") {
1422 has_seek_table = true;
1423 }
1424 },
1425 _ => { return Err(DemuxerError::InvalidData); }
1426 };
1427 }
1428 if !parsed {
1429 str_data.streams.push(RMStreamType::Unknown);
1430 str_data.str_ids.push(stream_no);
1431 }
1432 self.remap_ids.push(real_stream_no);
1433 },
1434 _ => {println!(" unexpected {}", rec); return Err(DemuxerError::InvalidData); }
1435 };
1436 }
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);
1443 if has_seek_table {
1444 src.read_skip(4)?;
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());
1452 } else {
1453 let ntype = src.peek_byte()?;
1454 validate!((ntype == 2) || (ntype == 7)); // packet or data end, no start
1455 }
1456
1457 self.cur_pos = src.tell();
1458
1459 Ok(())
1460 }
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))?;
1463 loop {
1464 let rec = IVRRecord::read(src)?;
1465 match rec {
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);
1474 if sr.is_none() {
1475 src.read_skip(payload_size)?;
1476 return Err(DemuxerError::InvalidData);
1477 }
1478 let str_id = sr.unwrap();
1479
1480 let streamres = strmgr.get_stream_by_id(stream_id);
1481 if streamres.is_none() {
1482 src.read_skip(payload_size)?;
1483 continue;
1484 }
1485 let stream = streamres.unwrap();
1486 if strmgr.is_ignored_id(stream_id) {
1487 src.read_skip(payload_size)?;
1488 continue;
1489 }
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 {
1493 continue;
1494 } else {
1495 self.cur_pos = src.tell();
1496 return ret;
1497 }
1498 },
1499 IVRRecord::DataEnd => return Err(DemuxerError::EOF),
1500 _ => return Err(DemuxerError::InvalidData),
1501 }
1502 }
1503 }
1504 }
1505
1506 struct RealIVRDemuxer<'a> {
1507 src: &'a mut ByteReader<'a>,
1508 recs: Vec<RecordDemuxer>,
1509 cur_rec: usize,
1510 queued_pkts: Vec<NAPacket>,
1511 slice_buf: Vec<u8>,
1512 str_data: CommonStreamData,
1513 }
1514
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)?;
1534 loop {
1535 let typ = self.src.read_byte()?;
1536 println!(" at {:X} type {:02X}", self.src.tell(), typ);
1537 match typ {
1538 1 => {
1539 let len = self.src.read_u32be()?;
1540 self.src.read_skip(len as usize)?;
1541 },
1542 2 => {
1543 let len = self.src.read_u32be()? as u64;
1544 let pos = self.src.tell();
1545 if len > 0x20 {
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);
1550 }
1551 self.src.seek(SeekFrom::Start(pos + len))?;
1552 },
1553 b'R' => {
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
1557 break;
1558 } else {
1559 return Err(DemuxerError::InvalidData);
1560 }
1561 },
1562 _ => { return Err(DemuxerError::InvalidData); },
1563 };
1564 }
1565 } else {
1566 return Err(DemuxerError::InvalidData);
1567 }
1568
1569 Ok(())
1570 }
1571
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();
1575 return Ok(pkt);
1576 }
1577 loop {
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 {
1581 self.cur_rec += 1;
1582 } else {
1583 return res;
1584 }
1585 }
1586 }
1587
1588 #[allow(unused_variables)]
1589 fn seek(&mut self, time: u64, seek_idx: &SeekIndex) -> DemuxerResult<()> {
1590 Err(NotImplemented)
1591 }
1592 }
1593
1594 impl<'a> RealIVRDemuxer<'a> {
1595 fn new(io: &'a mut ByteReader<'a>) -> Self {
1596 RealIVRDemuxer {
1597 src: io,
1598 recs: Vec::new(),
1599 cur_rec: 0,
1600 queued_pkts: Vec::new(),
1601 slice_buf: Vec::new(),
1602 str_data: CommonStreamData::new(),
1603 }
1604 }
1605 }
1606
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"),
1615 ];
1616
1617 static RM_AUDIO_CODEC_REGISTER: &'static [(&[u8;4], &str)] = &[
1618 (b"lpcJ", "ra14.4"),
1619 (b"28_8", "ra28.8"),
1620 (b"cook", "cook"),
1621 (b"dnet", "ac3"),
1622 (b"sipr", "sipro"),
1623 (b"atrc", "atrac3"),
1624 (b"LSD:", "ralf"),
1625 (b"raac", "aac"),
1626 (b"racp", "aac"),
1627 ];
1628
1629 pub struct RealMediaDemuxerCreator { }
1630
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))
1634 }
1635 fn get_name(&self) -> &'static str { "realmedia" }
1636 }
1637
1638 pub struct RealAudioDemuxerCreator { }
1639
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))
1643 }
1644 fn get_name(&self) -> &'static str { "realaudio" }
1645 }
1646
1647 pub struct RealIVRDemuxerCreator { }
1648
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))
1652 }
1653 fn get_name(&self) -> &'static str { "real_ivr" }
1654 }
1655
1656 #[cfg(test)]
1657 mod test {
1658 use super::*;
1659 use std::fs::File;
1660
1661 #[test]
1662 fn test_rm_demux() {
1663 let mut file =
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();
1681
1682 loop {
1683 let pktres = dmx.get_frame(&mut sm);
1684 if let Err(e) = pktres {
1685 if e == DemuxerError::EOF { break; }
1686 panic!("error");
1687 }
1688 let pkt = pktres.unwrap();
1689 println!("Got {}", pkt);
1690 }
1691 //panic!("the end");
1692 }
1693 #[test]
1694 fn test_ra_demux() {
1695 let mut file =
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();
1704
1705 loop {
1706 let pktres = dmx.get_frame(&mut sm);
1707 if let Err(e) = pktres {
1708 if e == DemuxerError::EOF { break; }
1709 panic!("error");
1710 }
1711 let pkt = pktres.unwrap();
1712 println!("Got {}", pkt);
1713 }
1714 }
1715 #[test]
1716 fn test_ivr_demux() {
1717 let mut file =
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();
1726
1727 loop {
1728 let pktres = dmx.get_frame(&mut sm);
1729 if let Err(e) = pktres {
1730 if e == DemuxerError::EOF { break; }
1731 panic!("error");
1732 }
1733 let pkt = pktres.unwrap();
1734 println!("Got {}", pkt);
1735 }
1736 }
1737 }