]>
Commit | Line | Data |
---|---|---|
ce52b3b5 | 1 | use super::*; |
5641dccf | 2 | use nihav_core::demuxers::DemuxerError::*; |
ce52b3b5 KS |
3 | use std::io::SeekFrom; |
4 | use std::mem; | |
418bd0b8 | 5 | use std::fmt; |
ce52b3b5 KS |
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 | ||
02cb1e03 KS |
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 | ||
ce52b3b5 KS |
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); | |
57 | } | |
58 | fn add_slice(&mut self, slice_no: usize, data: &[u8]) { | |
59 | self.write_slice_info(slice_no); | |
9037cf6b | 60 | let dslice = &mut self.frame[self.hdr_size + self.frame_pos..][..data.len()]; |
ce52b3b5 KS |
61 | dslice.copy_from_slice(data); |
62 | self.frame_pos += data.len(); | |
63 | } | |
64 | fn write_slice_info(&mut self, slice_no: usize) { | |
65 | let off = 1 + (slice_no - 1) * 8; | |
66 | self.frame[off + 0] = 0; | |
67 | self.frame[off + 1] = 0; | |
68 | self.frame[off + 2] = 0; | |
69 | self.frame[off + 3] = 1; | |
70 | self.frame[off + 4] = (self.frame_pos >> 24) as u8; | |
71 | self.frame[off + 5] = (self.frame_pos >> 16) as u8; | |
72 | self.frame[off + 6] = (self.frame_pos >> 8) as u8; | |
73 | self.frame[off + 7] = (self.frame_pos >> 0) as u8; | |
74 | } | |
75 | fn get_frame_data(&mut self) -> Vec<u8> { | |
76 | let mut v: Vec<u8> = Vec::new(); | |
77 | mem::swap(&mut v, &mut self.frame); | |
78 | self.flush(); | |
79 | v | |
80 | } | |
81 | } | |
82 | ||
83 | #[derive(Clone,Copy,PartialEq)] | |
84 | enum Deinterleaver { | |
85 | None, | |
c19ce782 | 86 | RA28_8, |
ce52b3b5 KS |
87 | Generic, |
88 | Sipro, | |
89 | VBR, | |
90 | } | |
91 | ||
92 | struct RMAudioStream { | |
93 | deint: Deinterleaver, | |
c19ce782 | 94 | iinfo: Option<InterleaveInfo>, |
fe07b469 KS |
95 | buf: Vec<u8>, |
96 | sub_packet: usize, | |
ce52b3b5 KS |
97 | } |
98 | ||
c19ce782 KS |
99 | const RM_ILEAVE_INT0: u32 = mktag!(b"Int0"); |
100 | const RM_ILEAVE_INT4: u32 = mktag!(b"Int4"); | |
101 | const RM_ILEAVE_GENR: u32 = mktag!(b"genr"); | |
102 | const RM_ILEAVE_SIPR: u32 = mktag!(b"sipr"); | |
bb149b3b | 103 | const RM_ILEAVE_VBRF: u32 = mktag!(b"vbrf"); |
c19ce782 KS |
104 | const RM_ILEAVE_VBRS: u32 = mktag!(b"vbrs"); |
105 | ||
ce52b3b5 | 106 | impl RMAudioStream { |
c19ce782 KS |
107 | fn new(iinfo: Option<InterleaveInfo>) -> Self { |
108 | let deint; | |
1f4aa1f4 | 109 | let buf: Vec<u8>; |
c19ce782 KS |
110 | if let Some(info) = iinfo { |
111 | deint = match info.id { | |
112 | RM_ILEAVE_INT0 => Deinterleaver::None, | |
113 | RM_ILEAVE_INT4 => Deinterleaver::RA28_8, | |
114 | RM_ILEAVE_GENR => Deinterleaver::Generic, | |
115 | RM_ILEAVE_SIPR => Deinterleaver::Sipro, | |
bb149b3b | 116 | RM_ILEAVE_VBRF => Deinterleaver::VBR, |
c19ce782 KS |
117 | RM_ILEAVE_VBRS => Deinterleaver::VBR, |
118 | _ => {println!("unknown deint {:X}", info.id); Deinterleaver::None }, | |
119 | }; | |
fe07b469 KS |
120 | match deint { |
121 | Deinterleaver::None => { buf = Vec::new(); }, | |
6669c73b KS |
122 | Deinterleaver::RA28_8 | |
123 | Deinterleaver::Generic | | |
124 | Deinterleaver::Sipro => { | |
fe07b469 | 125 | let bsize = (info.frame_size as usize) * (info.factor as usize); |
e07387c7 | 126 | buf = vec![0; bsize]; |
fe07b469 | 127 | }, |
480d951e KS |
128 | Deinterleaver::VBR => { |
129 | buf = Vec::new(); | |
130 | }, | |
fe07b469 | 131 | }; |
c19ce782 KS |
132 | } else { |
133 | deint = Deinterleaver::None; | |
fe07b469 KS |
134 | buf = Vec::new(); |
135 | } | |
e07387c7 | 136 | RMAudioStream { deint, iinfo, buf, sub_packet: 0 } |
fe07b469 | 137 | } |
70910ac3 | 138 | fn read_apackets(&mut self, queued_packets: &mut Vec<NAPacket>, src: &mut ByteReader, stream: NAStreamRef, ts: u32, keyframe: bool, payload_size: usize) -> DemuxerResult<NAPacket> { |
fe07b469 KS |
139 | let (tb_num, tb_den) = stream.get_timebase(); |
140 | let ts = NATimeInfo::new(Some(ts as u64), None, None, tb_num, tb_den); | |
141 | ||
142 | if keyframe { | |
143 | self.sub_packet = 0; | |
144 | } | |
145 | match self.deint { | |
6669c73b | 146 | Deinterleaver::None => { return src.read_packet(stream, ts, keyframe, payload_size); }, |
fe07b469 KS |
147 | Deinterleaver::RA28_8 => { |
148 | let iinfo = self.iinfo.unwrap(); | |
149 | let factor = iinfo.factor as usize; | |
150 | let halffact = factor >> 1; | |
151 | let fsize = iinfo.frame_size as usize; | |
152 | let bsize = iinfo.block_size as usize; | |
153 | let ppos = self.sub_packet; | |
154 | for sb in 0..halffact { | |
155 | let mut dst = &mut self.buf[sb * 2 * fsize + ppos * bsize..][..bsize]; | |
156 | src.read_buf(&mut dst)?; | |
157 | } | |
158 | self.sub_packet += 1; | |
159 | if self.sub_packet == factor { | |
160 | self.sub_packet = 0; | |
6669c73b | 161 | return Ok(NAPacket::new(stream, ts, true, self.buf.clone())); |
fe07b469 | 162 | } else { |
6669c73b | 163 | return Err(DemuxerError::TryAgain); |
fe07b469 KS |
164 | } |
165 | }, | |
6669c73b KS |
166 | Deinterleaver::Generic => { |
167 | let iinfo = self.iinfo.unwrap(); | |
168 | let factor = iinfo.factor as usize; | |
169 | let fsize = iinfo.frame_size as usize; | |
170 | let bsize = iinfo.block_size as usize; | |
171 | let factor2 = fsize / bsize; | |
172 | let ppos = self.sub_packet; | |
173 | ||
174 | for sb in 0..factor2 { | |
175 | let sb_pos = factor * sb + ((factor + 1) >> 1) * (ppos & 1) + (ppos >> 1); | |
176 | let mut dst = &mut self.buf[bsize * sb_pos..][..bsize]; | |
177 | src.read_buf(&mut dst)?; | |
178 | } | |
179 | }, | |
180 | Deinterleaver::Sipro => { | |
181 | let iinfo = self.iinfo.unwrap(); | |
182 | let fsize = iinfo.frame_size as usize; | |
183 | let ppos = self.sub_packet; | |
184 | ||
185 | let mut dst = &mut self.buf[fsize * ppos..][..fsize]; | |
186 | src.read_buf(&mut dst)?; | |
187 | }, | |
480d951e KS |
188 | Deinterleaver::VBR => { |
189 | validate!(payload_size >= 5); | |
190 | let hdrsizesize = src.read_u16be()?; | |
191 | let num_entries = (hdrsizesize / 16) as usize; | |
192 | validate!(payload_size >= num_entries * 3 + 2); | |
193 | let mut sizes: Vec<usize> = Vec::with_capacity(num_entries); | |
194 | let mut tot_size = 0; | |
195 | for _ in 0..num_entries { | |
196 | let sz = src.read_u16be()? as usize; | |
197 | tot_size += sz; | |
198 | sizes.push(sz); | |
199 | } | |
200 | validate!(tot_size + num_entries * 2 + 2 == payload_size); | |
201 | let pkt_ts = NATimeInfo::new(None, None, None, tb_num, tb_den); | |
202 | let mut first = true; | |
203 | for size in sizes.iter() { | |
204 | let cur_ts = if first { ts } else { pkt_ts }; | |
205 | first = false; | |
206 | let pkt = src.read_packet(stream.clone(), cur_ts, true, *size)?; | |
207 | queued_packets.push(pkt); | |
208 | } | |
209 | queued_packets.reverse(); | |
210 | let pkt0 = queued_packets.pop().unwrap(); | |
211 | return Ok(pkt0); | |
212 | }, | |
6669c73b KS |
213 | }; |
214 | ||
215 | let iinfo = self.iinfo.unwrap(); | |
216 | let factor = iinfo.factor as usize; | |
5dfd175c | 217 | let fsize = if iinfo.block_size != 0 { iinfo.block_size } else { iinfo.frame_size } as usize; |
6669c73b KS |
218 | |
219 | self.sub_packet += 1; | |
220 | if self.sub_packet < factor { | |
221 | return Err(DemuxerError::TryAgain); | |
222 | } | |
223 | ||
224 | self.sub_packet = 0; | |
225 | ||
226 | if self.deint == Deinterleaver::Sipro { | |
02cb1e03 | 227 | sipro_restore(&mut self.buf, factor, fsize); |
6669c73b KS |
228 | } |
229 | ||
230 | let mut frames_iter = self.buf.chunks(fsize); | |
231 | let pkt0 = frames_iter.next().unwrap(); | |
232 | ||
233 | let pkt_ts = NATimeInfo::new(None, None, None, tb_num, tb_den); | |
234 | for pkts in frames_iter { | |
235 | let pkt = NAPacket::new(stream.clone(), pkt_ts, true, pkts.to_vec()); | |
236 | queued_packets.push(pkt); | |
c19ce782 | 237 | } |
6669c73b KS |
238 | queued_packets.reverse(); |
239 | Ok(NAPacket::new(stream, ts, true, pkt0.to_vec())) | |
ce52b3b5 KS |
240 | } |
241 | } | |
242 | ||
02cb1e03 KS |
243 | fn sipro_restore(buf: &mut [u8], factor: usize, fsize: usize) { |
244 | let stride = factor * fsize * 2 / 96; | |
245 | for i in 0..38 { | |
246 | let mut sidx = (RM_SIPRO_SWAPS[i][0] as usize) * stride; | |
247 | let mut didx = (RM_SIPRO_SWAPS[i][1] as usize) * stride; | |
248 | for _ in 0..stride { | |
249 | let in0 = buf[sidx >> 1]; | |
250 | let in1 = buf[didx >> 1]; | |
251 | let nib0 = (in0 >> ((sidx & 1) * 4)) & 0xF; | |
252 | let nib1 = (in1 >> ((didx & 1) * 4)) & 0xF; | |
253 | ||
254 | buf[didx >> 1] = (nib0 << (4 * (didx & 1))) | (in1 & (0xF << (4 * (!didx & 1)))); | |
255 | buf[sidx >> 1] = (nib1 << (4 * (sidx & 1))) | (in0 & (0xF << (4 * (!sidx & 1)))); | |
256 | ||
257 | sidx += 1; | |
258 | didx += 1; | |
259 | } | |
260 | } | |
261 | } | |
262 | ||
d4df9ee1 KS |
263 | struct SubstreamInfo { |
264 | id: u32, | |
265 | map: Vec<u16>, | |
266 | str_ids: Vec<u32>, | |
267 | } | |
268 | ||
269 | struct MLTIMapper { | |
270 | sub_info: Vec<SubstreamInfo>, | |
418bd0b8 | 271 | sstr_id: u32, |
d4df9ee1 KS |
272 | } |
273 | ||
274 | impl MLTIMapper { | |
275 | fn new() -> Self { | |
276 | MLTIMapper { | |
277 | sub_info: Vec::new(), | |
418bd0b8 | 278 | sstr_id: 0x10000, |
d4df9ee1 KS |
279 | } |
280 | } | |
281 | fn add_stream(&mut self, stream_no: u32) { | |
282 | let ssinfo = SubstreamInfo { id: stream_no, map: Vec::new(), str_ids: Vec::new() }; | |
283 | self.sub_info.push(ssinfo); | |
284 | } | |
418bd0b8 KS |
285 | fn get_substream_no(&self) -> u32 { |
286 | self.sstr_id - 1 | |
287 | } | |
d4df9ee1 KS |
288 | fn find_idx(&self, stream_no: u32) -> Option<usize> { |
289 | self.sub_info.iter().position(|x| x.id == stream_no) | |
290 | } | |
418bd0b8 KS |
291 | fn add_map_rule(&mut self, map_ss: u16) { |
292 | let idx = self.sub_info.len() - 1; | |
d4df9ee1 KS |
293 | self.sub_info[idx].map.push(map_ss); |
294 | } | |
418bd0b8 KS |
295 | fn add_substream(&mut self) { |
296 | let idx = self.sub_info.len() - 1; | |
297 | self.sub_info[idx].str_ids.push(self.sstr_id); | |
298 | self.sstr_id += 1; | |
d4df9ee1 KS |
299 | } |
300 | fn is_mlti_stream(&self, stream_no: u32) -> bool { | |
301 | self.find_idx(stream_no).is_some() | |
302 | } | |
303 | fn find_substream(&self, stream_no: u32, grp: u16) -> Option<u32> { | |
304 | if let Some(idx) = self.find_idx(stream_no) { | |
305 | if (grp as usize) < self.sub_info[idx].map.len() { | |
306 | let sub_id = self.sub_info[idx].map[grp as usize] as usize; | |
307 | if sub_id < self.sub_info[idx].str_ids.len() { | |
308 | return Some(self.sub_info[idx].str_ids[sub_id]); | |
309 | } | |
310 | } | |
311 | } | |
312 | None | |
313 | } | |
314 | } | |
315 | ||
ce52b3b5 KS |
316 | enum RMStreamType { |
317 | Audio(RMAudioStream), | |
318 | Video(RMVideoStream), | |
319 | Logical, | |
320 | Unknown, | |
321 | } | |
322 | ||
418bd0b8 KS |
323 | struct CommonStreamData { |
324 | streams: Vec<RMStreamType>, | |
325 | str_ids: Vec<u32>, | |
326 | mlti_mapper: MLTIMapper, | |
327 | } | |
328 | ||
329 | impl CommonStreamData { | |
330 | fn new() -> Self { | |
331 | CommonStreamData { | |
332 | streams: Vec::new(), | |
333 | str_ids: Vec::new(), | |
334 | mlti_mapper: MLTIMapper::new(), | |
335 | } | |
336 | } | |
337 | fn get_stream_id(&self, str_no: u32, pkt_grp: u16) -> u32 { | |
338 | if !self.mlti_mapper.is_mlti_stream(str_no) { | |
339 | str_no | |
340 | } else { | |
341 | self.mlti_mapper.find_substream(str_no, pkt_grp).unwrap() | |
342 | } | |
343 | } | |
344 | fn find_stream(&self, stream_id: u32) -> Option<usize> { | |
345 | self.str_ids.iter().position(|x| *x == stream_id) | |
346 | } | |
347 | } | |
348 | ||
ce52b3b5 KS |
349 | struct RealMediaDemuxer<'a> { |
350 | src: &'a mut ByteReader<'a>, | |
351 | data_pos: u64, | |
770bef6f | 352 | next_data: u64, |
db5cc44b | 353 | data_ver: u16, |
ce52b3b5 KS |
354 | num_packets: u32, |
355 | cur_packet: u32, | |
356 | ||
418bd0b8 | 357 | str_data: CommonStreamData, |
ce52b3b5 KS |
358 | |
359 | queued_pkts: Vec<NAPacket>, | |
360 | slice_buf: Vec<u8>, | |
361 | } | |
362 | ||
363 | fn find_codec_name(registry: &[(&[u8;4], &'static str)], fcc: u32) -> &'static str { | |
364 | for &(fourcc, name) in registry { | |
365 | if mktag!(fourcc) == fcc { return name; } | |
366 | } | |
367 | "unknown" | |
368 | } | |
369 | ||
370 | fn read_14or30(src: &mut ByteReader) -> DemuxerResult<(bool, u32)> { | |
371 | let tmp = src.read_u16be()?; | |
372 | let flag = (tmp & 0x8000) != 0; | |
373 | if (tmp & 0x4000) == 0x4000 { | |
374 | Ok((flag, ((tmp & 0x3FFF) as u32))) | |
375 | } else { | |
376 | let val = ((tmp as u32) << 16) | (src.read_u16be()? as u32); | |
377 | Ok((flag, val & 0x3FFFFFFF)) | |
378 | } | |
379 | } | |
380 | ||
70910ac3 | 381 | fn read_video_buf(src: &mut ByteReader, stream: NAStreamRef, ts: u32, keyframe: bool, frame_size: usize) -> DemuxerResult<NAPacket> { |
ce52b3b5 | 382 | let size = (frame_size as usize) + 9; |
e07387c7 | 383 | let mut vec: Vec<u8> = vec![0; size]; |
ce52b3b5 KS |
384 | //v[0] = 0; // 1 slice |
385 | vec[4] = 1; | |
386 | src.read_buf(&mut vec[9..])?; | |
387 | ||
388 | let (tb_num, tb_den) = stream.get_timebase(); | |
389 | let ts = NATimeInfo::new(Some(ts as u64), None, None, tb_num, tb_den); | |
390 | Ok(NAPacket::new(stream, ts, keyframe, vec)) | |
391 | } | |
392 | ||
70910ac3 | 393 | fn read_multiple_frame(src: &mut ByteReader, stream: NAStreamRef, keyframe: bool, skip_mtype: bool) -> DemuxerResult<NAPacket> { |
ce52b3b5 KS |
394 | if !skip_mtype { |
395 | let mtype = src.read_byte()?; | |
396 | validate!(mtype == 0xC0); | |
397 | } | |
398 | let (_, frame_size) = read_14or30(src)?; | |
399 | let (_, timestamp) = read_14or30(src)?; | |
c9aba47e KS |
400 | let _seq_no = src.read_byte()?; |
401 | //println!(" multiple frame size {} ts {} seq {}", frame_size, timestamp, seq_no); | |
ce52b3b5 KS |
402 | |
403 | read_video_buf(src, stream, timestamp, keyframe, frame_size as usize) | |
404 | } | |
405 | ||
418bd0b8 KS |
406 | struct RMDemuxCommon {} |
407 | ||
408 | impl RMDemuxCommon { | |
409 | fn parse_stream_info(str_data: &mut CommonStreamData, strmgr: &mut StreamManager, stream_no: u32, edata: &Vec<u8>) -> DemuxerResult<bool> { | |
410 | let mut is_mlti = false; | |
411 | let mut mr = MemoryReader::new_read(edata.as_slice()); | |
412 | let mut src = ByteReader::new(&mut mr); | |
413 | let tag = src.read_u32be()?; | |
414 | let tag2 = src.peek_u32be()?; | |
415 | //println!("tag1 {:X} tag2 {:X}", tag, tag2); | |
416 | if tag == mktag!('.', 'r', 'a', 0xFD) { | |
417 | Self::parse_audio_stream(strmgr, &mut str_data.streams, stream_no, &mut src, edata.as_slice())?; | |
418 | } else if ((tag2 == mktag!('V', 'I', 'D', 'O')) || (tag2 == mktag!('I', 'M', 'A', 'G'))) && ((tag as usize) <= edata.len()) { | |
419 | Self::parse_video_stream(strmgr, &mut str_data.streams, stream_no, &mut src, edata.as_slice(), tag2)?; | |
420 | } else if tag == mktag!(b"LSD:") { | |
e07387c7 | 421 | let extradata = Some(edata.to_owned()); |
418bd0b8 KS |
422 | |
423 | src.read_skip(4)?; //version | |
424 | let channels = src.read_u16be()?; | |
425 | let samp_size = src.read_u16be()?; | |
426 | let sample_rate = src.read_u32be()?; | |
427 | ||
428 | println!("LSD sr {}, {} ch", sample_rate, channels); | |
429 | let soniton = NASoniton::new(samp_size as u8, SONITON_FLAG_SIGNED); | |
430 | let ahdr = NAAudioInfo::new(sample_rate, channels as u8, soniton, 1); | |
431 | let nainfo = NACodecInfo::new("ralf", NACodecTypeInfo::Audio(ahdr), extradata); | |
432 | let res = strmgr.add_stream(NAStream::new(StreamType::Audio, stream_no as u32, nainfo, 1, sample_rate)); | |
433 | if res.is_none() { return Err(MemoryError); } | |
434 | let astr = RMAudioStream::new(None); | |
435 | str_data.streams.push(RMStreamType::Audio(astr)); | |
436 | } else if tag == mktag!(b"MLTI") { | |
437 | is_mlti = true; | |
438 | let num_rules = src.read_u16be()? as usize; | |
439 | let mut max_sub = 0; | |
440 | str_data.mlti_mapper.add_stream(stream_no); | |
441 | for _ in 0..num_rules { | |
442 | let substr = src.read_u16be()?; | |
443 | max_sub = max_sub.max(substr); | |
444 | str_data.mlti_mapper.add_map_rule(substr); | |
445 | } | |
446 | let num_substreams = src.read_u16be()? as usize; | |
447 | validate!(num_substreams > (max_sub as usize)); | |
448 | for _ in 0..num_substreams { | |
449 | let hdr_size = src.read_u32be()? as usize; | |
450 | validate!(hdr_size > 8); | |
451 | let pos = src.tell() as usize; | |
452 | src.read_skip(hdr_size)?; | |
453 | str_data.mlti_mapper.add_substream(); | |
454 | { | |
455 | let hdrsrc = &edata[pos..][..hdr_size]; | |
456 | let mut mr = MemoryReader::new_read(hdrsrc); | |
457 | let mut hsrc = ByteReader::new(&mut mr); | |
458 | ||
459 | let tag = hsrc.read_u32be()?; | |
460 | let tag2 = hsrc.peek_u32be()?; | |
461 | let stream_no = str_data.mlti_mapper.get_substream_no(); | |
462 | //todo check that all substreams are of the same type"); | |
463 | if tag == mktag!('.', 'r', 'a', 0xFD) { | |
464 | Self::parse_audio_stream(strmgr, &mut str_data.streams, stream_no, &mut hsrc, hdrsrc)?; | |
465 | } else if (tag2 == mktag!('V', 'I', 'D', 'O')) && ((tag as usize) <= hdr_size) { | |
466 | Self::parse_video_stream(strmgr, &mut str_data.streams, stream_no, &mut hsrc, hdrsrc, tag2)?; | |
467 | } else { | |
468 | println!("unknown MLTI substream {:08X} / {:08X}", tag, tag2); | |
469 | return Err(DemuxerError::InvalidData); | |
470 | } | |
471 | str_data.str_ids.push(stream_no); | |
472 | } | |
473 | } | |
474 | } else { | |
475 | str_data.streams.push(RMStreamType::Logical); | |
476 | } | |
477 | Ok(is_mlti) | |
478 | } | |
479 | fn parse_audio_stream(strmgr: &mut StreamManager, streams: &mut Vec<RMStreamType>, stream_no: u32, src: &mut ByteReader, edata_: &[u8]) -> DemuxerResult<()> { | |
480 | let ver = src.read_u16be()?; | |
481 | let ainfo = match ver { | |
482 | 3 => { | |
483 | parse_aformat3(src)? | |
484 | }, | |
485 | 4 => { | |
486 | parse_aformat4(src)? | |
487 | }, | |
488 | 5 => { | |
489 | parse_aformat5(src)? | |
490 | }, | |
491 | _ => { | |
492 | println!("unknown version {}", ver); | |
493 | return Err(DemuxerError::InvalidData); | |
494 | }, | |
495 | }; | |
496 | println!(" got ainfo {:?}", ainfo); | |
497 | let cname = find_codec_name(RM_AUDIO_CODEC_REGISTER, ainfo.fcc); | |
498 | let blk_size = if ainfo.fcc != mktag!(b"sipr") { | |
499 | ainfo.granularity as usize | |
500 | } else { | |
501 | validate!(ainfo.flavor <= 3); | |
502 | RM_SIPRO_BLOCK_SIZES[ainfo.flavor as usize] | |
503 | }; | |
504 | let srate = ainfo.sample_rate; | |
505 | let soniton = NASoniton::new(ainfo.sample_size as u8, SONITON_FLAG_SIGNED); | |
506 | let ahdr = NAAudioInfo::new(srate, ainfo.channels as u8, soniton, blk_size); | |
507 | let extradata = if ainfo.edata_size == 0 { | |
508 | None | |
509 | } else { | |
510 | let eslice = &edata_[(src.tell() as usize)..]; | |
511 | Some(eslice.to_vec()) | |
512 | }; | |
513 | let nainfo = NACodecInfo::new(cname, NACodecTypeInfo::Audio(ahdr), extradata); | |
514 | let res = strmgr.add_stream(NAStream::new(StreamType::Audio, stream_no as u32, nainfo, 1, srate)); | |
515 | if res.is_none() { return Err(MemoryError); } | |
516 | ||
517 | let astr = RMAudioStream::new(ainfo.ileave_info); | |
518 | streams.push(RMStreamType::Audio(astr)); | |
519 | Ok(()) | |
520 | } | |
521 | #[allow(unused_variables)] | |
522 | fn parse_video_stream(strmgr: &mut StreamManager, streams: &mut Vec<RMStreamType>, stream_no: u32, src: &mut ByteReader, edata_: &[u8], tag2: u32) -> DemuxerResult<()> { | |
523 | src.read_skip(4)?; | |
524 | let fcc = src.read_u32be()?; | |
525 | let width = src.read_u16be()? as usize; | |
526 | let height = src.read_u16be()? as usize; | |
527 | let bpp = src.read_u16be()?; | |
528 | let pad_w = src.read_u16be()?; | |
529 | let pad_h = src.read_u16be()?; | |
530 | let fps; | |
531 | if tag2 == mktag!('V', 'I', 'D', 'O') { | |
532 | fps = src.read_u32be()?; | |
533 | } else { | |
534 | fps = 0x10000; | |
535 | } | |
536 | let extradata: Option<Vec<u8>>; | |
537 | if src.left() > 0 { | |
538 | let eslice = &edata_[(src.tell() as usize)..]; | |
539 | extradata = Some(eslice.to_vec()); | |
540 | } else { | |
541 | extradata = None; | |
542 | } | |
543 | let cname = find_codec_name(RM_VIDEO_CODEC_REGISTER, fcc); | |
544 | ||
545 | let vhdr = NAVideoInfo::new(width, height, false, RGB24_FORMAT); | |
546 | let vinfo = NACodecInfo::new(cname, NACodecTypeInfo::Video(vhdr), extradata); | |
547 | let res = strmgr.add_stream(NAStream::new(StreamType::Video, stream_no as u32, vinfo, 0x10000, fps)); | |
548 | if res.is_none() { return Err(DemuxerError::MemoryError); } | |
549 | ||
550 | let vstr = RMVideoStream::new(); | |
551 | streams.push(RMStreamType::Video(vstr)); | |
552 | Ok(()) | |
553 | } | |
554 | #[allow(unused_variables)] | |
70910ac3 | 555 | 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> { |
418bd0b8 KS |
556 | match rmstream { |
557 | RMStreamType::Video(ref mut vstr) => { | |
558 | ||
559 | let pos = src.tell(); | |
560 | let b0 = src.read_byte()?; | |
561 | match b0 >> 6 { | |
562 | 0 => { // partial frame | |
563 | let b1 = src.read_byte()?; | |
564 | let hdr1 = ((b0 as u16) << 8) | (b1 as u16); | |
565 | let num_pkts = ((hdr1 >> 7) & 0x7F) as usize; | |
566 | let packet_num = hdr1 & 0x7F; | |
567 | let (_, frame_size) = read_14or30(src)?; | |
568 | let (_, off) = read_14or30(src)?; | |
569 | let seq_no = src.read_byte()?; | |
570 | //println!(" mode 0 pkt {}/{} off {}/{} seq {}", packet_num, num_pkts, off, frame_size, seq_no); | |
571 | let hdr_skip = (src.tell() - pos) as usize; | |
572 | ||
573 | let slice_size = (payload_size - hdr_skip) as usize; | |
574 | slice_buf.resize(slice_size, 0); | |
575 | src.read_buf(slice_buf.as_mut_slice())?; | |
576 | if packet_num == 1 { | |
577 | vstr.start_slice(num_pkts, frame_size as usize, slice_buf.as_slice()); | |
578 | } else { | |
579 | vstr.add_slice(packet_num as usize, slice_buf.as_slice()); | |
580 | } | |
581 | if (packet_num as usize) < num_pkts { | |
582 | return Err(DemuxerError::TryAgain); | |
583 | } | |
584 | //todo: check if full frame is received | |
585 | let (tb_num, tb_den) = stream.get_timebase(); | |
586 | let ts = NATimeInfo::new(Some(ts as u64), None, None, tb_num, tb_den); | |
587 | let pkt = NAPacket::new(stream, ts, keyframe, vstr.get_frame_data()); | |
588 | Ok(pkt) | |
589 | }, | |
590 | 1 => { // whole frame | |
591 | let seq_no = src.read_byte()?; | |
592 | //println!(" mode 1 seq {}", seq_no); | |
593 | read_video_buf(src, stream, ts, keyframe, payload_size - 2) | |
594 | }, | |
595 | 2 => { // last partial frame | |
596 | let b1 = src.read_byte()?; | |
597 | let hdr1 = ((b0 as u16) << 8) | (b1 as u16); | |
598 | let num_pkts = ((hdr1 >> 7) & 0x7F) as usize; | |
599 | let packet_num = hdr1 & 0x7F; | |
600 | let (_, frame_size) = read_14or30(src)?; | |
601 | let (_, tail_size) = read_14or30(src)?; | |
602 | let seq_no = src.read_byte()?; | |
603 | //println!(" mode 2 pkt {}/{} tail {}/{} seq {}", packet_num, num_pkts, tail_size, frame_size, seq_no); | |
604 | slice_buf.resize(tail_size as usize, 0); | |
605 | src.read_buf(slice_buf.as_mut_slice())?; | |
606 | if packet_num == 1 && frame_size == tail_size { | |
607 | vstr.start_slice(num_pkts, frame_size as usize, slice_buf.as_slice()); | |
608 | } else { | |
609 | vstr.add_slice(packet_num as usize, slice_buf.as_slice()); | |
610 | } | |
611 | ||
612 | while src.tell() < pos + (payload_size as u64) { | |
613 | let res = read_multiple_frame(src, stream.clone(), false, false); | |
614 | if res.is_err() { break; } | |
615 | queued_pkts.push(res.unwrap()); | |
616 | } | |
617 | queued_pkts.reverse(); | |
618 | let (tb_num, tb_den) = stream.get_timebase(); | |
619 | let ts = NATimeInfo::new(Some(ts as u64), None, None, tb_num, tb_den); | |
620 | let pkt = NAPacket::new(stream, ts, keyframe, vstr.get_frame_data()); | |
621 | Ok(pkt) | |
622 | }, | |
623 | _ => { // multiple frames | |
624 | //println!(" mode 3"); | |
625 | let res = read_multiple_frame(src, stream.clone(), keyframe, true); | |
626 | if res.is_err() { return res; } | |
627 | while src.tell() < pos + (payload_size as u64) { | |
628 | let res = read_multiple_frame(src, stream.clone(), false, false); | |
629 | if res.is_err() { break; } | |
630 | queued_pkts.push(res.unwrap()); | |
631 | } | |
632 | queued_pkts.reverse(); | |
633 | res | |
634 | }, | |
635 | } | |
636 | }, | |
637 | RMStreamType::Audio(ref mut astr) => { | |
638 | astr.read_apackets(queued_pkts, src, stream, ts, keyframe, payload_size) | |
639 | }, | |
640 | _ => { | |
641 | src.read_skip(payload_size)?; | |
642 | Err(DemuxerError::InvalidData) | |
643 | }, | |
644 | } | |
645 | } | |
646 | } | |
647 | ||
ce52b3b5 KS |
648 | impl<'a> DemuxCore<'a> for RealMediaDemuxer<'a> { |
649 | #[allow(unused_variables)] | |
650 | fn open(&mut self, strmgr: &mut StreamManager) -> DemuxerResult<()> { | |
651 | self.read_header(strmgr)?; | |
652 | Ok(()) | |
653 | } | |
654 | ||
655 | #[allow(unused_variables)] | |
656 | fn get_frame(&mut self, strmgr: &mut StreamManager) -> DemuxerResult<NAPacket> { | |
657 | if !self.queued_pkts.is_empty() { | |
658 | let pkt = self.queued_pkts.pop().unwrap(); | |
659 | return Ok(pkt); | |
660 | } | |
661 | loop { | |
770bef6f KS |
662 | if self.cur_packet >= self.num_packets { |
663 | if (self.next_data != 0) && (self.next_data == self.src.tell()) { | |
664 | let res = read_chunk(self.src); | |
665 | if let Ok((id, size, ver)) = res { | |
666 | self.data_pos = self.src.tell(); | |
667 | self.data_ver = ver; | |
668 | if self.parse_data_start().is_ok() { | |
669 | continue; | |
670 | } | |
671 | } | |
672 | } | |
673 | return Err(DemuxerError::EOF); | |
674 | } | |
ce52b3b5 KS |
675 | |
676 | let pkt_start = self.src.tell(); | |
7348405b | 677 | let ver = self.src.read_u16be()?; |
ce52b3b5 KS |
678 | validate!(ver <= 1); |
679 | let len = self.src.read_u16be()? as usize; | |
d4df9ee1 | 680 | let str_no = self.src.read_u16be()? as u32; |
ce52b3b5 | 681 | let ts = self.src.read_u32be()?; |
d4df9ee1 | 682 | let pkt_grp; |
770bef6f | 683 | let flags; |
ce52b3b5 | 684 | if ver == 0 { |
d4df9ee1 | 685 | pkt_grp = self.src.read_byte()? as u16; |
770bef6f | 686 | flags = self.src.read_byte()?; |
ce52b3b5 | 687 | } else { |
7348405b | 688 | //asm_rule = self.src.read_u16be()?; |
d4df9ee1 | 689 | //asm_flags = self.src.read_byte()?; |
ce52b3b5 | 690 | self.src.read_skip(2)?; |
d4df9ee1 | 691 | pkt_grp = 0; |
770bef6f KS |
692 | self.src.read_skip(1)?; |
693 | flags = 0; | |
ce52b3b5 | 694 | } |
ce52b3b5 | 695 | let hdr_size = self.src.tell() - pkt_start; |
d4df9ee1 | 696 | //println!("packet @{:X} size {} for {} ts {} grp {} flags {:X}", pkt_start, len, str_no, ts, pkt_grp, flags); |
ce52b3b5 KS |
697 | self.cur_packet += 1; |
698 | ||
699 | let payload_size = len - (hdr_size as usize); | |
700 | ||
418bd0b8 KS |
701 | let stream_id = self.str_data.get_stream_id(str_no as u32, pkt_grp); |
702 | let sr = self.str_data.find_stream(stream_id); | |
ce52b3b5 | 703 | if sr.is_none() { |
c9aba47e | 704 | //println!("stream {} not found", str_no); |
ce52b3b5 KS |
705 | self.src.read_skip(payload_size)?; |
706 | return Err(DemuxerError::InvalidData); | |
707 | } | |
708 | let str_id = sr.unwrap(); | |
d4df9ee1 KS |
709 | |
710 | let streamres = strmgr.get_stream_by_id(stream_id); | |
c9aba47e KS |
711 | if streamres.is_none() { |
712 | self.src.read_skip(payload_size)?; | |
713 | continue; | |
714 | } | |
715 | let stream = streamres.unwrap(); | |
716 | //println!(" stream {}", str_id); | |
d4df9ee1 | 717 | if strmgr.is_ignored_id(stream_id) { |
ce52b3b5 KS |
718 | self.src.read_skip(payload_size)?; |
719 | continue; | |
720 | } | |
721 | //todo skip unwanted packet | |
722 | let keyframe = (flags & KEYFRAME_FLAG) != 0; | |
723 | ||
418bd0b8 KS |
724 | 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); |
725 | if let Err(DemuxerError::TryAgain) = ret { | |
726 | continue; | |
727 | } else { | |
728 | return ret; | |
729 | } | |
ce52b3b5 KS |
730 | } |
731 | } | |
732 | ||
733 | #[allow(unused_variables)] | |
734 | fn seek(&mut self, time: u64) -> DemuxerResult<()> { | |
735 | Err(NotImplemented) | |
736 | } | |
737 | } | |
738 | ||
739 | fn read_chunk(src: &mut ByteReader) -> DemuxerResult<(u32, u32, u16)> { | |
740 | let id = src.read_u32be()?; | |
6132225f | 741 | if id == 0 { return Ok((0, 0, 0)); } |
ce52b3b5 | 742 | let size = src.read_u32be()?; |
9f45e528 KS |
743 | if size == 0 { |
744 | let ver = src.read_u16be()?; | |
db5cc44b | 745 | validate!(ver <= 2); |
9f45e528 KS |
746 | return Ok((id, 0x0FFFFFFF, ver)); |
747 | } | |
ce52b3b5 KS |
748 | validate!(size >= 10); |
749 | let ver = src.read_u16be()?; | |
db5cc44b | 750 | validate!(ver <= 2); |
ce52b3b5 KS |
751 | Ok((id, size, ver)) |
752 | } | |
753 | ||
c19ce782 KS |
754 | #[derive(Clone,Copy,Debug)] |
755 | struct InterleaveInfo { | |
756 | id: u32, | |
757 | factor: u16, | |
758 | block_size: u16, | |
759 | frame_size: u16, | |
760 | } | |
761 | ||
762 | #[derive(Clone,Copy,Debug)] | |
763 | struct RealAudioInfo { | |
764 | fcc: u32, | |
765 | sample_rate: u32, | |
766 | sample_size: u16, | |
02cb1e03 | 767 | flavor: u16, |
c19ce782 KS |
768 | channels: u16, |
769 | channel_mask: u32, | |
770 | granularity: u32, | |
771 | bytes_per_minute: u32, | |
772 | total_bytes: u32, | |
773 | edata_size: u32, | |
774 | ileave_info: Option<InterleaveInfo> | |
775 | } | |
776 | ||
777 | fn skip_ra_metadata(src: &mut ByteReader) -> DemuxerResult<()> { | |
778 | let title_len = src.read_byte()? as usize; | |
779 | src.read_skip(title_len)?; | |
780 | let author_len = src.read_byte()? as usize; | |
781 | src.read_skip(author_len)?; | |
782 | let copywrong_len = src.read_byte()? as usize; | |
783 | src.read_skip(copywrong_len)?; | |
784 | let comment_len = src.read_byte()? as usize; | |
785 | src.read_skip(comment_len)?; | |
786 | Ok(()) | |
787 | } | |
788 | ||
789 | #[allow(unused_variables)] | |
790 | fn parse_aformat3(src: &mut ByteReader) -> DemuxerResult<RealAudioInfo> { | |
791 | let start = src.tell(); | |
792 | let header_len = src.read_u16be()?; | |
793 | validate!(header_len >= 24); | |
794 | let flavor = src.read_u16be()?; | |
795 | let granularity = src.read_u32be()?; | |
796 | let bytes_per_minute = src.read_u32be()?; | |
797 | let total_bytes = src.read_u32be()?; | |
798 | ||
799 | skip_ra_metadata(src)?; | |
800 | ||
801 | let _can_copy = src.read_byte()?; | |
802 | let fcc_len = src.read_byte()?; | |
803 | validate!(fcc_len == 4); | |
804 | let fcc = src.read_u32be()?; | |
805 | ||
806 | let end = src.tell(); | |
807 | validate!(end - start <= (header_len as u64) + 2); | |
808 | ||
809 | Ok(RealAudioInfo { | |
e07387c7 | 810 | fcc, flavor, |
02cb1e03 | 811 | sample_rate: 8000, sample_size: 16, channels: 1, channel_mask: 0, |
e07387c7 KS |
812 | granularity, bytes_per_minute, |
813 | total_bytes, edata_size: 0, | |
c19ce782 KS |
814 | ileave_info: None, |
815 | }) | |
816 | } | |
817 | ||
818 | #[allow(unused_variables)] | |
819 | fn parse_aformat4(src: &mut ByteReader) -> DemuxerResult<RealAudioInfo> { | |
820 | let start = src.tell(); | |
821 | src.read_skip(2)?; // zeroes | |
822 | let id = src.read_u32be()?; | |
823 | validate!(id == mktag!(b".ra4")); | |
824 | let data_size = src.read_u32be()?; | |
825 | let _ver4 = src.read_u16be()?; // should be 4 | |
826 | let header_size = src.read_u32be()?; | |
02cb1e03 | 827 | let flavor = src.read_u16be()?; |
c19ce782 KS |
828 | let granularity = src.read_u32be()?; |
829 | let total_bytes = src.read_u32be()?; | |
830 | let bytes_per_minute = src.read_u32be()?; | |
831 | let _bytes_per_minute2 = src.read_u32be()?; | |
832 | let ileave_factor = src.read_u16be()?; | |
833 | let ileave_block_size = src.read_u16be()?; | |
834 | let _user_data = src.read_u16be()?; | |
835 | let sample_rate = src.read_u32be()?; | |
836 | let sample_size = src.read_u32be()?; | |
837 | let channels = src.read_u16be()?; | |
838 | let interleaver_id_len = src.read_byte()?; | |
839 | validate!(interleaver_id_len == 4); | |
840 | let interleaver_id = src.read_u32be()?; | |
841 | let fcc_len = src.read_byte()?; | |
842 | validate!(fcc_len == 4); | |
843 | let fcc = src.read_u32be()?; | |
844 | let is_interleaved = src.read_byte()?; | |
845 | let _can_copy = src.read_byte()?; | |
846 | let _stream_type = src.read_byte()?; | |
847 | ||
848 | skip_ra_metadata(src)?; | |
849 | ||
850 | let end = src.tell(); | |
851 | validate!(end - start <= (header_size as u64) + 10); | |
852 | ||
853 | let ileave_info = if is_interleaved != 0 { | |
854 | Some(InterleaveInfo { | |
fe07b469 KS |
855 | id: interleaver_id, factor: ileave_factor, block_size: granularity as u16, |
856 | frame_size: ileave_block_size, | |
c19ce782 KS |
857 | }) |
858 | } else { | |
859 | None | |
860 | }; | |
861 | ||
862 | Ok(RealAudioInfo { | |
e07387c7 KS |
863 | fcc, flavor, |
864 | sample_rate, sample_size: sample_size as u16, channels, channel_mask: 0, | |
865 | granularity, bytes_per_minute, | |
c19ce782 | 866 | total_bytes: total_bytes & 0xFFFFFF, edata_size: 0, |
e07387c7 | 867 | ileave_info, |
c19ce782 KS |
868 | }) |
869 | } | |
870 | ||
871 | #[allow(unused_variables)] | |
872 | fn parse_aformat5(src: &mut ByteReader) -> DemuxerResult<RealAudioInfo> { | |
873 | let start = src.tell(); | |
874 | src.read_skip(2)?; // zeroes | |
875 | let id = src.read_u32be()?; | |
db5cc44b | 876 | validate!((id == mktag!(b".ra5")) || (id == mktag!(b".ra4"))); |
c19ce782 KS |
877 | let data_size = src.read_u32be()?; |
878 | let _ver5 = src.read_u16be()?; // should be 5 | |
879 | let header_size = src.read_u32be()?; | |
02cb1e03 | 880 | let flavor = src.read_u16be()?; |
c19ce782 KS |
881 | let granularity = src.read_u32be()?; |
882 | let total_bytes = src.read_u32be()?; | |
883 | let bytes_per_minute = src.read_u32be()?; | |
884 | let _bytes_per_minute2 = src.read_u32be()?; | |
885 | let ileave_factor = src.read_u16be()?; | |
c19ce782 | 886 | let frame_size = src.read_u16be()?; |
6669c73b | 887 | let ileave_block_size = src.read_u16be()?; |
c19ce782 KS |
888 | let user_data = src.read_u32be()?; |
889 | let _sample_rate1 = src.read_u16be()?; | |
890 | let sample_rate = src.read_u32be()?; | |
891 | let sample_size = src.read_u32be()?; | |
892 | let channels = src.read_u16be()?; | |
893 | let interleaver_id = src.read_u32be()?; | |
894 | let fcc = src.read_u32be()?; | |
895 | let is_interleaved = src.read_byte()?; | |
896 | let _can_copy = src.read_byte()?; | |
897 | let _stream_type = src.read_byte()?; | |
898 | let has_ileave_pattern = src.read_byte()?; | |
899 | if has_ileave_pattern != 0 { | |
900 | unimplemented!("ra5 interleave pattern"); | |
901 | } | |
843c8c87 | 902 | let mut edata_size = src.read_u32be()?; |
c19ce782 | 903 | let end = src.tell(); |
db5cc44b KS |
904 | if id == mktag!(b".ra5") { |
905 | validate!(end - start <= (header_size as u64) + 10); | |
bb149b3b | 906 | // src.read_skip(((end - start) as usize) - (header_size as usize) - 10)?; |
db5cc44b KS |
907 | } else { |
908 | validate!(end - start <= (header_size as u64) + 15); | |
909 | } | |
c19ce782 KS |
910 | |
911 | let ileave_info = if is_interleaved != 0 { | |
912 | Some(InterleaveInfo { | |
e07387c7 | 913 | id: interleaver_id, factor: ileave_factor, block_size: ileave_block_size, frame_size, |
c19ce782 KS |
914 | }) |
915 | } else { | |
916 | None | |
917 | }; | |
843c8c87 KS |
918 | if (fcc == mktag!(b"raac")) || (fcc == mktag!(b"racp")) { |
919 | validate!(edata_size > 1); | |
920 | edata_size -= 1; | |
921 | src.read_skip(1)?; | |
922 | } | |
c19ce782 KS |
923 | |
924 | Ok(RealAudioInfo { | |
e07387c7 KS |
925 | fcc, flavor, |
926 | sample_rate, sample_size: sample_size as u16, channels, channel_mask: 0, | |
927 | granularity, bytes_per_minute, | |
928 | total_bytes: total_bytes & 0xFFFFFF, edata_size, | |
929 | ileave_info, | |
c19ce782 KS |
930 | }) |
931 | } | |
932 | ||
ce52b3b5 KS |
933 | const RMVB_HDR_SIZE: u32 = 18; |
934 | const RMVB_PROP_SIZE: u32 = 50; | |
935 | const KEYFRAME_FLAG: u8 = 0x02; | |
936 | ||
937 | impl<'a> RealMediaDemuxer<'a> { | |
938 | fn new(io: &'a mut ByteReader<'a>) -> Self { | |
939 | RealMediaDemuxer { | |
940 | src: io, | |
941 | data_pos: 0, | |
770bef6f | 942 | next_data: 0, |
db5cc44b | 943 | data_ver: 0, |
ce52b3b5 KS |
944 | num_packets: 0, |
945 | cur_packet: 0, | |
418bd0b8 | 946 | str_data: CommonStreamData::new(), |
ce52b3b5 KS |
947 | queued_pkts: Vec::new(), |
948 | slice_buf: Vec::new(), | |
949 | } | |
950 | } | |
951 | #[allow(unused_variables)] | |
952 | fn read_header(&mut self, strmgr: &mut StreamManager) -> DemuxerResult<()> { | |
953 | let (id, size, ver) = read_chunk(self.src)?; | |
db5cc44b | 954 | validate!((id == mktag!(b".RMF")) || (id == mktag!(b".RMP"))); |
ce52b3b5 KS |
955 | validate!(size >= RMVB_HDR_SIZE); |
956 | let fver = self.src.read_u32be()?; | |
957 | validate!(fver <= 1); | |
958 | let num_hdr = self.src.read_u32be()? as usize; | |
959 | validate!(num_hdr >= 1); | |
960 | if size > RMVB_HDR_SIZE { | |
961 | self.src.read_skip((size - RMVB_HDR_SIZE) as usize)?; | |
962 | } | |
963 | ||
964 | let (id, size, ver) = read_chunk(self.src)?; | |
db5cc44b KS |
965 | let prop_size = if ver == 0 { RMVB_PROP_SIZE } else { RMVB_PROP_SIZE + 4 }; |
966 | validate!(size >= prop_size); | |
967 | validate!((ver == 0) || (ver == 2)); | |
ce52b3b5 KS |
968 | let maxbr = self.src.read_u32be()?; |
969 | let avgbr = self.src.read_u32be()?; | |
970 | let maxps = self.src.read_u32be()?; | |
971 | let avgps = self.src.read_u32be()?; | |
972 | let num_pkt = self.src.read_u32be()? as usize; | |
973 | let duration = self.src.read_u32be()?; | |
974 | let preroll = self.src.read_u32be()?; | |
db5cc44b KS |
975 | if ver == 2 { |
976 | self.src.read_skip(4)?; | |
977 | } | |
ce52b3b5 KS |
978 | let idx_off = self.src.read_u32be()?; |
979 | let data_off = self.src.read_u32be()?; | |
980 | let num_streams = self.src.read_u16be()? as usize; | |
981 | let flags = self.src.read_u16be()?; | |
db5cc44b KS |
982 | if size > prop_size { |
983 | self.src.read_skip((size - prop_size) as usize)?; | |
ce52b3b5 KS |
984 | } |
985 | ||
986 | for _ in 0..num_hdr { | |
c9aba47e KS |
987 | if self.src.is_eof() { |
988 | //warn maybe? | |
989 | break; | |
990 | } | |
991 | let res = self.parse_chunk(strmgr); | |
992 | match res { | |
993 | Ok(last) => { if last { break; } }, | |
994 | Err(DemuxerError::IOError) => { break; }, | |
b7b96b4c KS |
995 | Err(etype) => { |
996 | if self.data_pos == 0 { // data is not found, report error | |
997 | return Err(etype); | |
998 | } | |
999 | }, | |
c9aba47e | 1000 | }; |
ce52b3b5 | 1001 | } |
c9aba47e | 1002 | //println!("now @ {:X} / {}", self.src.tell(), self.data_pos); |
ce52b3b5 KS |
1003 | validate!(self.data_pos > 0); |
1004 | self.src.seek(SeekFrom::Start(self.data_pos))?; | |
770bef6f KS |
1005 | self.parse_data_start()?; |
1006 | Ok(()) | |
1007 | } | |
1008 | fn parse_data_start(&mut self) -> DemuxerResult<()> { | |
ce52b3b5 | 1009 | let num_packets = self.src.read_u32be()?; |
db5cc44b KS |
1010 | if self.data_ver == 2 { |
1011 | self.src.read_skip(12)?; // zeroes? | |
1012 | } | |
ce52b3b5 | 1013 | let next_data_hdr = self.src.read_u32be()?; |
9f45e528 | 1014 | self.num_packets = if num_packets > 0 { num_packets } else { 0xFFFFFF }; |
ce52b3b5 | 1015 | self.cur_packet = 0; |
770bef6f | 1016 | self.next_data = next_data_hdr as u64; |
ce52b3b5 KS |
1017 | Ok(()) |
1018 | } | |
6132225f | 1019 | fn parse_chunk(&mut self, strmgr: &mut StreamManager) -> DemuxerResult<bool> { |
ce52b3b5 KS |
1020 | let (id, size, ver) = read_chunk(self.src)?; |
1021 | let end_pos = self.src.tell() - 10 + (size as u64); | |
1022 | ||
db5cc44b | 1023 | validate!((ver == 0) || (ver == 2)); |
ce52b3b5 KS |
1024 | if id == mktag!(b"CONT") { self.parse_content_desc()?; } |
1025 | else if id == mktag!(b"MDPR") { self.parse_mdpr(strmgr)?; } | |
db5cc44b | 1026 | else if id == mktag!(b"DATA") { if self.data_pos == 0 { self.data_ver = ver; self.data_pos = self.src.tell(); } } |
ce52b3b5 | 1027 | else if id == mktag!(b"INDX") { /* do nothing for now */ } |
6132225f | 1028 | else if id == 0 { return Ok(true); } |
ce52b3b5 KS |
1029 | else { println!("unknown chunk type {:08X}", id); } |
1030 | ||
1031 | let cpos = self.src.tell(); | |
1032 | if cpos < end_pos { | |
1033 | self.src.read_skip((end_pos - cpos) as usize)?; | |
1034 | } | |
6132225f | 1035 | Ok(false) |
ce52b3b5 KS |
1036 | } |
1037 | #[allow(unused_variables)] | |
1038 | fn parse_content_desc(&mut self) -> DemuxerResult<()> { | |
1039 | let title_len = self.src.read_u16be()? as usize; | |
1040 | self.src.read_skip(title_len)?; | |
1041 | let author_len = self.src.read_u16be()? as usize; | |
1042 | self.src.read_skip(author_len)?; | |
1043 | let copywrong_len = self.src.read_u16be()? as usize; | |
1044 | self.src.read_skip(copywrong_len)?; | |
1045 | let comment_len = self.src.read_u16be()? as usize; | |
1046 | self.src.read_skip(comment_len)?; | |
1047 | Ok(()) | |
1048 | } | |
1049 | #[allow(unused_variables)] | |
1050 | fn parse_mdpr(&mut self, strmgr: &mut StreamManager) -> DemuxerResult<()> { | |
d4df9ee1 | 1051 | let stream_no = self.src.read_u16be()? as u32; |
ce52b3b5 KS |
1052 | //todo check stream_no for duplicates |
1053 | let maxbr = self.src.read_u32be()?; | |
1054 | let avgbr = self.src.read_u32be()?; | |
1055 | let maxps = self.src.read_u32be()?; | |
1056 | let avgps = self.src.read_u32be()?; | |
1057 | let start = self.src.read_u32be()?; | |
1058 | let preroll = self.src.read_u32be()?; | |
1059 | let duration = self.src.read_u32be()?; | |
1060 | let sname_size = self.src.read_byte()? as usize; | |
1061 | let sname = read_string_size(self.src, sname_size)?; | |
c9aba47e | 1062 | //println!("str #{} sname = {} pkts {}/{} start {} preroll {}", stream_no, sname, maxps, avgps, start, preroll); |
ce52b3b5 KS |
1063 | let mime_size = self.src.read_byte()? as usize; |
1064 | let mime = read_string_size(self.src, mime_size)?; | |
c9aba47e | 1065 | //println!("mime = {}", mime); |
ce52b3b5 KS |
1066 | let edata_size = self.src.read_u32be()? as usize; |
1067 | let edata: Option<Vec<u8>> = if edata_size == 0 { None } else { | |
e07387c7 | 1068 | let mut edvec: Vec<u8> = vec![0; edata_size]; |
ce52b3b5 KS |
1069 | self.src.read_buf(&mut edvec)?; |
1070 | Some(edvec) | |
1071 | }; | |
d4df9ee1 | 1072 | let mut is_mlti = false; |
ce52b3b5 KS |
1073 | if edata_size > 8 { |
1074 | if let Some(edata_) = edata { | |
418bd0b8 | 1075 | is_mlti = RMDemuxCommon::parse_stream_info(&mut self.str_data, strmgr, stream_no, &edata_)?; |
ce52b3b5 KS |
1076 | } |
1077 | } else { | |
418bd0b8 | 1078 | self.str_data.streams.push(RMStreamType::Unknown); |
ce52b3b5 | 1079 | } |
d4df9ee1 | 1080 | if !is_mlti { |
418bd0b8 | 1081 | self.str_data.str_ids.push(stream_no); |
d4df9ee1 | 1082 | } |
d4df9ee1 | 1083 | |
ce52b3b5 KS |
1084 | Ok(()) |
1085 | } | |
ce52b3b5 KS |
1086 | } |
1087 | ||
1088 | fn read_string_size(src: &mut ByteReader, size: usize) -> DemuxerResult<String> { | |
1089 | let mut vec: Vec<u8> = Vec::new(); | |
1090 | for _ in 0..size { | |
1091 | let c = src.read_byte()?; | |
1092 | vec.push(c); | |
1093 | } | |
1094 | let str = String::from_utf8(vec); | |
1095 | if str.is_ok() { | |
1096 | Ok(str.unwrap()) | |
1097 | } else { | |
1098 | Ok(String::new()) | |
1099 | } | |
1100 | } | |
1101 | ||
d1304e6e KS |
1102 | struct RealAudioDemuxer<'a> { |
1103 | src: &'a mut ByteReader<'a>, | |
1104 | stream: Option<RMAudioStream>, | |
1105 | data_start: u64, | |
1106 | data_end: u64, | |
1107 | blk_size: usize, | |
1108 | queued_pkts: Vec<NAPacket>, | |
1109 | } | |
1110 | ||
1111 | impl<'a> DemuxCore<'a> for RealAudioDemuxer<'a> { | |
1112 | #[allow(unused_variables)] | |
1113 | fn open(&mut self, strmgr: &mut StreamManager) -> DemuxerResult<()> { | |
1114 | let magic = self.src.read_u32be()?; | |
1115 | validate!(magic == mktag!(b".ra\xFD")); | |
1116 | let ver = self.src.read_u16be()?; | |
1117 | let ainfo = match ver { | |
1118 | 3 => { | |
1119 | parse_aformat3(&mut self.src)? | |
1120 | }, | |
1121 | 4 => { | |
1122 | parse_aformat4(&mut self.src)? | |
1123 | }, | |
1124 | 5 => { | |
1125 | parse_aformat5(&mut self.src)? | |
1126 | }, | |
1127 | _ => { | |
1128 | println!("unknown version {}", ver); | |
1129 | return Err(DemuxerError::InvalidData); | |
1130 | }, | |
1131 | }; | |
1132 | println!(" got ainfo {:?}", ainfo); | |
1133 | let cname = find_codec_name(RM_AUDIO_CODEC_REGISTER, ainfo.fcc); | |
1134 | let blk_size = if ainfo.fcc != mktag!(b"sipr") { | |
1135 | ainfo.granularity as usize | |
1136 | } else { | |
1137 | validate!(ainfo.flavor <= 3); | |
1138 | RM_SIPRO_BLOCK_SIZES[ainfo.flavor as usize] | |
1139 | }; | |
1140 | let srate = ainfo.sample_rate; | |
1141 | let soniton = NASoniton::new(ainfo.sample_size as u8, SONITON_FLAG_SIGNED); | |
1142 | let ahdr = NAAudioInfo::new(srate, ainfo.channels as u8, soniton, blk_size); | |
1143 | let extradata = if ainfo.edata_size == 0 { | |
1144 | None | |
1145 | } else { | |
e07387c7 | 1146 | let mut dta: Vec<u8> = vec![0; ainfo.edata_size as usize]; |
d1304e6e KS |
1147 | self.src.read_buf(dta.as_mut_slice())?; |
1148 | Some(dta) | |
1149 | }; | |
1150 | let nainfo = NACodecInfo::new(cname, NACodecTypeInfo::Audio(ahdr), extradata); | |
1151 | let res = strmgr.add_stream(NAStream::new(StreamType::Audio, 0, nainfo, 1, srate)); | |
1152 | if res.is_none() { return Err(MemoryError); } | |
1153 | ||
1154 | let astr = RMAudioStream::new(ainfo.ileave_info); | |
1155 | self.data_start = self.src.tell(); | |
1156 | self.data_end = if ainfo.total_bytes > 0 { self.src.tell() + (ainfo.total_bytes as u64) } else { 0 }; | |
1157 | self.blk_size = blk_size; | |
1158 | self.stream = Some(astr); | |
1159 | ||
1160 | Ok(()) | |
1161 | } | |
1162 | ||
1163 | fn get_frame(&mut self, strmgr: &mut StreamManager) -> DemuxerResult<NAPacket> { | |
1164 | if !self.queued_pkts.is_empty() { | |
1165 | let pkt = self.queued_pkts.pop().unwrap(); | |
1166 | return Ok(pkt); | |
1167 | } | |
1168 | if (self.data_end != 0) && (self.src.tell() >= self.data_end) { | |
1169 | return Err(DemuxerError::EOF); | |
1170 | } | |
1171 | let streamres = strmgr.get_stream_by_id(0); | |
1172 | let stream = streamres.unwrap(); | |
1173 | if let Some(ref mut astr) = self.stream { | |
1174 | loop { | |
1175 | let ret = astr.read_apackets(&mut self.queued_pkts, &mut self.src, stream.clone(), 0, false, self.blk_size); | |
1176 | if let Err(DemuxerError::TryAgain) = ret { | |
1177 | continue; | |
1178 | } | |
1179 | return ret; | |
1180 | } | |
1181 | } | |
1182 | Err(DemuxerError::NoSuchInput) | |
1183 | } | |
1184 | ||
1185 | #[allow(unused_variables)] | |
1186 | fn seek(&mut self, time: u64) -> DemuxerResult<()> { | |
1187 | Err(NotImplemented) | |
1188 | } | |
1189 | } | |
1190 | ||
1191 | impl<'a> RealAudioDemuxer<'a> { | |
1192 | fn new(io: &'a mut ByteReader<'a>) -> Self { | |
1193 | RealAudioDemuxer { | |
1194 | src: io, | |
1195 | data_start: 0, | |
1196 | data_end: 0, | |
1197 | blk_size: 0, | |
1198 | stream: None, | |
1199 | queued_pkts: Vec::new(), | |
1200 | } | |
1201 | } | |
1202 | } | |
1203 | ||
418bd0b8 KS |
1204 | enum IVRRecord { |
1205 | Invalid(u8), | |
1206 | StreamProperties(usize), | |
1207 | Packet { ts: u32, str: u32, flags: u32, len: usize, checksum: u32 }, | |
1208 | IntValue(Vec<u8>, u32), | |
1209 | BinaryData(Vec<u8>, Vec<u8>), | |
1210 | StringData(Vec<u8>, Vec<u8>), | |
1211 | HeaderEnd, | |
1212 | DataStart, | |
1213 | DataEnd, | |
1214 | } | |
1215 | ||
1216 | impl IVRRecord { | |
1217 | fn read_string(src: &mut ByteReader) -> DemuxerResult<Vec<u8>> { | |
1218 | let len = src.read_u32be()? as usize; | |
e07387c7 | 1219 | let mut val = vec![0; len]; |
418bd0b8 KS |
1220 | src.read_buf(val.as_mut_slice())?; |
1221 | Ok(val) | |
1222 | } | |
1223 | ||
1224 | fn read(src: &mut ByteReader) -> DemuxerResult<Self> { | |
1225 | let code = src.read_byte()?; | |
1226 | match code { | |
1227 | 1 => { | |
1228 | let val = src.read_u32be()? as usize; | |
1229 | Ok(IVRRecord::StreamProperties(val)) | |
1230 | }, | |
1231 | 2 => { | |
1232 | let ts = src.read_u32be()?; | |
1233 | let str = src.read_u16be()? as u32; | |
1234 | let flags = src.read_u32be()?; | |
1235 | let len = src.read_u32be()? as usize; | |
1236 | let chk = src.read_u32be()?; | |
6dc93d35 | 1237 | validate!((len > 0) && (len < (1 << 24))); |
418bd0b8 KS |
1238 | Ok(IVRRecord::Packet { ts, str, flags, len, checksum: chk }) |
1239 | }, | |
1240 | 3 => { | |
1241 | let name = Self::read_string(src)?; | |
1242 | let len = src.read_u32be()?; | |
1243 | validate!(len == 4); | |
1244 | let val = src.read_u32be()?; | |
1245 | Ok(IVRRecord::IntValue(name, val)) | |
1246 | }, | |
1247 | 4 => { | |
1248 | let name = Self::read_string(src)?; | |
1249 | let len = src.read_u32be()? as usize; | |
e07387c7 | 1250 | let mut val = vec![0; len]; |
418bd0b8 KS |
1251 | src.read_buf(val.as_mut_slice())?; |
1252 | Ok(IVRRecord::BinaryData(name, val)) | |
1253 | }, | |
1254 | 5 => { | |
1255 | let name = Self::read_string(src)?; | |
1256 | let val = Self::read_string(src)?; | |
1257 | Ok(IVRRecord::StringData(name, val)) | |
1258 | }, | |
1259 | 6 => Ok(IVRRecord::HeaderEnd), | |
1260 | 7 => { | |
1261 | src.read_skip(8)?; // always zero? | |
1262 | Ok(IVRRecord::DataEnd) | |
1263 | }, | |
1264 | 8 => { | |
1265 | src.read_skip(8)?; // always zero? | |
1266 | Ok(IVRRecord::DataStart) | |
1267 | }, | |
1268 | _ => Ok(IVRRecord::Invalid(code)), | |
1269 | } | |
1270 | } | |
1271 | fn is_data_start(&self) -> bool { | |
1272 | match *self { | |
1273 | IVRRecord::DataStart => true, | |
1274 | _ => false, | |
1275 | } | |
1276 | } | |
1277 | } | |
1278 | ||
1279 | impl fmt::Display for IVRRecord { | |
1280 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | |
1281 | match *self { | |
1282 | IVRRecord::Invalid(typ) => write!(f, "Invalid({:02X})", typ), | |
1283 | IVRRecord::StreamProperties(num) => | |
1284 | write!(f, "({} stream properties)", num), | |
1285 | IVRRecord::Packet { ts, str, flags, len, checksum } => | |
1286 | write!(f, "paket({}, {}, {:X}, {}, {})", ts, str, flags, len, checksum), | |
1287 | IVRRecord::IntValue(ref name, val) => | |
1288 | write!(f, "({} = {})", String::from_utf8_lossy(name), val), | |
1289 | IVRRecord::BinaryData(ref name, ref val) => | |
1290 | write!(f, "({} = {} bytes)", String::from_utf8_lossy(name), val.len()), | |
1291 | IVRRecord::StringData(ref name, ref val) => | |
1292 | write!(f, "({} = {})", String::from_utf8_lossy(name), String::from_utf8_lossy(val)), | |
1293 | IVRRecord::HeaderEnd => write!(f, "header end"), | |
1294 | IVRRecord::DataEnd => write!(f, "data end"), | |
1295 | IVRRecord::DataStart => write!(f, "data start"), | |
1296 | } | |
1297 | } | |
1298 | } | |
1299 | ||
1300 | struct RecordDemuxer { | |
1301 | start_pos: u64, | |
1302 | cur_pos: u64, | |
1303 | start_str: u32, | |
1304 | remap_ids: Vec<u32>, | |
1305 | } | |
1306 | ||
1307 | impl RecordDemuxer { | |
1308 | fn new(pos: u64, start_str: u32) -> Self { | |
1309 | RecordDemuxer { | |
1310 | start_pos: pos, | |
1311 | cur_pos: pos, | |
e07387c7 | 1312 | start_str, |
418bd0b8 KS |
1313 | remap_ids: Vec::new(), |
1314 | } | |
1315 | } | |
1316 | fn parse_header(&mut self, src: &mut ByteReader, strmgr: &mut StreamManager, str_data: &mut CommonStreamData) -> DemuxerResult<()> { | |
1317 | src.seek(SeekFrom::Start(self.cur_pos))?; | |
1318 | let magic = src.read_u32be()?; | |
1319 | validate!(magic == mktag!(b".REC")); | |
1320 | let _smth = src.read_byte()?; | |
1321 | let num_entries = src.read_u32be()? as usize; | |
1322 | for _ in 0..num_entries { | |
1323 | let _rec = IVRRecord::read(src)?; | |
1324 | //println!(" header rec {}", _rec); | |
1325 | } | |
1326 | let mut has_seek_table = false; | |
1327 | let mut cur_str_no = 0; | |
1328 | loop { | |
1329 | let rec = IVRRecord::read(src)?; | |
1330 | match rec { | |
1331 | IVRRecord::HeaderEnd => { break; }, | |
1332 | IVRRecord::StreamProperties(num) => { | |
1333 | let stream_no = cur_str_no + self.start_str; | |
1334 | cur_str_no += 1; | |
1335 | let mut parsed = false; | |
1336 | let mut real_stream_no = 0; | |
1337 | for _ in 0..num { | |
1338 | let rec = IVRRecord::read(src)?; | |
1339 | //println!(" strm property {}", rec); | |
1340 | match rec { | |
1341 | IVRRecord::IntValue(ref name, val) => { | |
1342 | if name == b"StreamNumber\0" { | |
1343 | real_stream_no = val; | |
1344 | } | |
1345 | }, | |
1346 | IVRRecord::BinaryData(ref name, ref val) => { | |
1347 | if name == b"OpaqueData\0" { | |
1348 | validate!(!parsed); | |
1349 | let is_mlti = RMDemuxCommon::parse_stream_info(str_data, strmgr, stream_no, val)?; | |
1350 | if !is_mlti { | |
1351 | str_data.str_ids.push(stream_no); | |
1352 | } | |
1353 | parsed = true; | |
1354 | } | |
1355 | }, | |
1356 | IVRRecord::StringData(ref name, ref val) => { | |
1357 | if (name == b"SeekType\0") && (val != b"None\0") { | |
1358 | has_seek_table = true; | |
1359 | } | |
1360 | }, | |
1361 | _ => { return Err(DemuxerError::InvalidData); } | |
1362 | }; | |
1363 | } | |
1364 | if !parsed { | |
1365 | str_data.streams.push(RMStreamType::Unknown); | |
1366 | str_data.str_ids.push(stream_no); | |
1367 | } | |
1368 | self.remap_ids.push(real_stream_no); | |
1369 | }, | |
1370 | _ => {println!(" unexpected {}", rec); return Err(DemuxerError::InvalidData); } | |
1371 | }; | |
1372 | } | |
1373 | println!(" now @ {:X}", src.tell()); | |
1374 | let off0 = src.read_u32be()? as u64; | |
1375 | let _off1 = src.read_u32be()?; | |
1376 | let _off2 = src.read_u32be()?; | |
1377 | validate!(off0 + self.start_pos == src.tell()); | |
1378 | println!(" has seek tab: {}", has_seek_table); | |
1379 | if has_seek_table { | |
1380 | src.read_skip(4)?; | |
1381 | let data_off = src.read_u32be()? as u64; | |
1382 | println!(" new off {:X}", data_off); | |
1383 | let pos = src.tell(); | |
1384 | validate!(data_off + self.start_pos > pos); | |
1385 | src.read_skip((data_off + self.start_pos - pos) as usize)?; | |
1386 | let rec = IVRRecord::read(src)?; | |
1387 | validate!(rec.is_data_start()); | |
1388 | } else { | |
1389 | let ntype = src.peek_byte()?; | |
1390 | validate!((ntype == 2) || (ntype == 7)); // packet or data end, no start | |
1391 | } | |
1392 | ||
1393 | self.cur_pos = src.tell(); | |
1394 | ||
1395 | Ok(()) | |
1396 | } | |
1397 | 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> { | |
1398 | src.seek(SeekFrom::Start(self.cur_pos))?; | |
1399 | loop { | |
1400 | let rec = IVRRecord::read(src)?; | |
1401 | match rec { | |
e07387c7 | 1402 | IVRRecord::Packet { ts, str, flags, len, .. } => { |
418bd0b8 KS |
1403 | let payload_size = len; |
1404 | let sr = self.remap_ids.iter().position(|x| *x == str); | |
1405 | validate!(sr.is_some()); | |
1406 | let str_no = self.start_str + (sr.unwrap() as u32); | |
6dc93d35 KS |
1407 | let pkt_grp = ((flags >> 8) & 0xFF) as u16; |
1408 | let stream_id = str_data.get_stream_id(str_no as u32, pkt_grp); | |
418bd0b8 KS |
1409 | let sr = str_data.find_stream(stream_id); |
1410 | if sr.is_none() { | |
1411 | src.read_skip(payload_size)?; | |
1412 | return Err(DemuxerError::InvalidData); | |
1413 | } | |
1414 | let str_id = sr.unwrap(); | |
1415 | ||
1416 | let streamres = strmgr.get_stream_by_id(stream_id); | |
1417 | if streamres.is_none() { | |
1418 | src.read_skip(payload_size)?; | |
1419 | continue; | |
1420 | } | |
1421 | let stream = streamres.unwrap(); | |
1422 | if strmgr.is_ignored_id(stream_id) { | |
1423 | src.read_skip(payload_size)?; | |
1424 | continue; | |
1425 | } | |
1426 | let keyframe = false; | |
1427 | let ret = RMDemuxCommon::parse_packet_payload(src, &mut str_data.streams[str_id], stream, slice_buf, queued_pkts, keyframe, ts, payload_size); | |
1428 | if let Err(DemuxerError::TryAgain) = ret { | |
1429 | continue; | |
1430 | } else { | |
1431 | self.cur_pos = src.tell(); | |
1432 | return ret; | |
1433 | } | |
1434 | }, | |
1435 | IVRRecord::DataEnd => return Err(DemuxerError::EOF), | |
1436 | _ => return Err(DemuxerError::InvalidData), | |
1437 | } | |
1438 | } | |
1439 | } | |
1440 | } | |
1441 | ||
1442 | struct RealIVRDemuxer<'a> { | |
1443 | src: &'a mut ByteReader<'a>, | |
1444 | recs: Vec<RecordDemuxer>, | |
1445 | cur_rec: usize, | |
1446 | queued_pkts: Vec<NAPacket>, | |
1447 | slice_buf: Vec<u8>, | |
1448 | str_data: CommonStreamData, | |
1449 | } | |
1450 | ||
1451 | impl<'a> DemuxCore<'a> for RealIVRDemuxer<'a> { | |
1452 | #[allow(unused_variables)] | |
1453 | fn open(&mut self, strmgr: &mut StreamManager) -> DemuxerResult<()> { | |
1454 | let magic = self.src.peek_u32be()?; | |
1455 | if magic == mktag!(b".REC") { | |
1456 | let mut rec = RecordDemuxer::new(0, 0); | |
1457 | rec.parse_header(&mut self.src, strmgr, &mut self.str_data)?; | |
1458 | self.recs.push(rec); | |
1459 | } else if magic == mktag!(b".R1M") { | |
1460 | println!("R1M kind"); | |
1461 | self.src.read_skip(4)?; // magic | |
1462 | self.src.read_skip(3)?; // always 0, 1, 1 ? | |
1463 | let _name = IVRRecord::read_string(&mut self.src)?; | |
1464 | self.src.read_skip(1)?; // always 0? | |
1465 | let len1 = self.src.read_u32be()? as u64; | |
1466 | let off1 = self.src.read_u64be()?; | |
1467 | let cpos = self.src.tell(); | |
1468 | validate!(off1 == len1 + cpos - 8); | |
1469 | self.src.read_skip((off1 - cpos) as usize)?; | |
1470 | loop { | |
1471 | let typ = self.src.read_byte()?; | |
1472 | println!(" at {:X} type {:02X}", self.src.tell(), typ); | |
1473 | match typ { | |
1474 | 1 => { | |
1475 | let len = self.src.read_u32be()?; | |
1476 | self.src.read_skip(len as usize)?; | |
1477 | }, | |
1478 | 2 => { | |
1479 | let len = self.src.read_u32be()? as u64; | |
1480 | let pos = self.src.tell(); | |
6dc93d35 KS |
1481 | if len > 0x20 { |
1482 | let num_streams = self.str_data.streams.len() as u32; | |
1483 | let mut rec = RecordDemuxer::new(pos + 12, num_streams); | |
1484 | rec.parse_header(&mut self.src, strmgr, &mut self.str_data)?; | |
1485 | self.recs.push(rec); | |
1486 | } | |
418bd0b8 KS |
1487 | self.src.seek(SeekFrom::Start(pos + len))?; |
1488 | }, | |
1489 | b'R' => { | |
1490 | let mut buf: [u8; 2] = [0; 2]; | |
1491 | self.src.peek_buf(&mut buf)?; | |
1492 | if (buf[0] == b'J') && (buf[1] == b'M') { // RJMx markers at the end of file | |
1493 | break; | |
1494 | } else { | |
1495 | return Err(DemuxerError::InvalidData); | |
1496 | } | |
1497 | }, | |
1498 | _ => { return Err(DemuxerError::InvalidData); }, | |
1499 | }; | |
1500 | } | |
1501 | } else { | |
1502 | return Err(DemuxerError::InvalidData); | |
1503 | } | |
1504 | ||
1505 | Ok(()) | |
1506 | } | |
1507 | ||
1508 | fn get_frame(&mut self, strmgr: &mut StreamManager) -> DemuxerResult<NAPacket> { | |
1509 | if !self.queued_pkts.is_empty() { | |
1510 | let pkt = self.queued_pkts.pop().unwrap(); | |
1511 | return Ok(pkt); | |
1512 | } | |
1513 | loop { | |
1514 | if self.cur_rec >= self.recs.len() { return Err(DemuxerError::EOF); } | |
1515 | 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); | |
1516 | if let Err(DemuxerError::EOF) = res { | |
1517 | self.cur_rec += 1; | |
1518 | } else { | |
1519 | return res; | |
1520 | } | |
1521 | } | |
1522 | } | |
1523 | ||
1524 | #[allow(unused_variables)] | |
1525 | fn seek(&mut self, time: u64) -> DemuxerResult<()> { | |
1526 | Err(NotImplemented) | |
1527 | } | |
1528 | } | |
1529 | ||
1530 | impl<'a> RealIVRDemuxer<'a> { | |
1531 | fn new(io: &'a mut ByteReader<'a>) -> Self { | |
1532 | RealIVRDemuxer { | |
1533 | src: io, | |
1534 | recs: Vec::new(), | |
1535 | cur_rec: 0, | |
1536 | queued_pkts: Vec::new(), | |
1537 | slice_buf: Vec::new(), | |
1538 | str_data: CommonStreamData::new(), | |
1539 | } | |
1540 | } | |
1541 | } | |
1542 | ||
ce52b3b5 KS |
1543 | static RM_VIDEO_CODEC_REGISTER: &'static [(&[u8;4], &str)] = &[ |
1544 | (b"RV10", "realvideo1"), | |
1545 | (b"RV20", "realvideo2"), | |
1546 | (b"RVTR", "realvideo2"), | |
1547 | (b"RV30", "realvideo3"), | |
1548 | (b"RV40", "realvideo4"), | |
db5cc44b | 1549 | (b"RV60", "realvideo6"), |
750b299c | 1550 | (b"CLV1", "clearvideo_rm"), |
ce52b3b5 KS |
1551 | ]; |
1552 | ||
1553 | static RM_AUDIO_CODEC_REGISTER: &'static [(&[u8;4], &str)] = &[ | |
1554 | (b"lpcJ", "ra14.4"), | |
1555 | (b"28_8", "ra28.8"), | |
1556 | (b"cook", "cook"), | |
1557 | (b"dnet", "ac3"), | |
1558 | (b"sipr", "sipro"), | |
1559 | (b"atrc", "atrac3"), | |
1560 | (b"LSD:", "ralf"), | |
1561 | (b"raac", "aac"), | |
1562 | (b"racp", "aac"), | |
1563 | ]; | |
1564 | ||
1565 | pub struct RealMediaDemuxerCreator { } | |
1566 | ||
1567 | impl DemuxerCreator for RealMediaDemuxerCreator { | |
6011e201 | 1568 | fn new_demuxer<'a>(&self, br: &'a mut ByteReader<'a>) -> Box<dyn DemuxCore<'a> + 'a> { |
ce52b3b5 KS |
1569 | Box::new(RealMediaDemuxer::new(br)) |
1570 | } | |
1571 | fn get_name(&self) -> &'static str { "realmedia" } | |
1572 | } | |
1573 | ||
d1304e6e KS |
1574 | pub struct RealAudioDemuxerCreator { } |
1575 | ||
1576 | impl DemuxerCreator for RealAudioDemuxerCreator { | |
6011e201 | 1577 | fn new_demuxer<'a>(&self, br: &'a mut ByteReader<'a>) -> Box<dyn DemuxCore<'a> + 'a> { |
d1304e6e KS |
1578 | Box::new(RealAudioDemuxer::new(br)) |
1579 | } | |
1580 | fn get_name(&self) -> &'static str { "realaudio" } | |
1581 | } | |
1582 | ||
418bd0b8 | 1583 | pub struct RealIVRDemuxerCreator { } |
d1304e6e KS |
1584 | |
1585 | impl DemuxerCreator for RealIVRDemuxerCreator { | |
6011e201 | 1586 | fn new_demuxer<'a>(&self, br: &'a mut ByteReader<'a>) -> Box<dyn DemuxCore<'a> + 'a> { |
d1304e6e KS |
1587 | Box::new(RealIVRDemuxer::new(br)) |
1588 | } | |
1589 | fn get_name(&self) -> &'static str { "real_ivr" } | |
418bd0b8 | 1590 | } |
d1304e6e | 1591 | |
ce52b3b5 KS |
1592 | #[cfg(test)] |
1593 | mod test { | |
1594 | use super::*; | |
1595 | use std::fs::File; | |
1596 | ||
1597 | #[test] | |
1598 | fn test_rm_demux() { | |
1599 | let mut file = | |
1600 | File::open("assets/RV/rv10_dnet_640x352_realvideo_encoder_4.0.rm").unwrap(); | |
1601 | // File::open("assets/RV/rv20_cook_640x352_realproducer_plus_8.51.rm").unwrap(); | |
1602 | // File::open("assets/RV/rv20_svt_atrc_640x352_realproducer_plus_8.51.rm").unwrap(); | |
1603 | // File::open("assets/RV/rv30_atrc_384x208_realproducer_plus_8.51.rm").unwrap(); | |
1604 | // File::open("assets/RV/rv30_chroma_drift.rm").unwrap(); | |
1605 | // File::open("assets/RV/rv30_weighted_mc.rm").unwrap(); | |
1606 | // File::open("assets/RV/rv40_weighted_mc.rmvb").unwrap(); | |
1607 | // File::open("assets/RV/rv40_weighted_mc_2.rmvb").unwrap(); | |
1608 | // File::open("assets/RV/clv1_sipr_384x208_realvideo_encoder_4.0.rm").unwrap(); | |
1609 | // File::open("assets/RV/luckynight.rmvb").unwrap(); | |
1610 | // File::open("assets/RV/rv40_ralf.rmvb").unwrap(); | |
1611 | let mut fr = FileReader::new_read(&mut file); | |
1612 | let mut br = ByteReader::new(&mut fr); | |
1613 | let mut dmx = RealMediaDemuxer::new(&mut br); | |
1614 | let mut sm = StreamManager::new(); | |
1615 | dmx.open(&mut sm).unwrap(); | |
1616 | ||
1617 | loop { | |
1618 | let pktres = dmx.get_frame(&mut sm); | |
1619 | if let Err(e) = pktres { | |
1620 | if e == DemuxerError::EOF { break; } | |
1621 | panic!("error"); | |
1622 | } | |
1623 | let pkt = pktres.unwrap(); | |
1624 | println!("Got {}", pkt); | |
1625 | } | |
41f11b2b | 1626 | //panic!("the end"); |
ce52b3b5 | 1627 | } |
d1304e6e KS |
1628 | #[test] |
1629 | fn test_ra_demux() { | |
1630 | let mut file = | |
1631 | // File::open("assets/RV/welcome288.ra").unwrap(); | |
1632 | File::open("assets/RV/diemusik.ra").unwrap(); | |
1633 | let mut fr = FileReader::new_read(&mut file); | |
1634 | let mut br = ByteReader::new(&mut fr); | |
1635 | let mut dmx = RealAudioDemuxer::new(&mut br); | |
1636 | let mut sm = StreamManager::new(); | |
1637 | dmx.open(&mut sm).unwrap(); | |
1638 | ||
418bd0b8 KS |
1639 | loop { |
1640 | let pktres = dmx.get_frame(&mut sm); | |
1641 | if let Err(e) = pktres { | |
1642 | if e == DemuxerError::EOF { break; } | |
1643 | panic!("error"); | |
1644 | } | |
1645 | let pkt = pktres.unwrap(); | |
1646 | println!("Got {}", pkt); | |
1647 | } | |
1648 | } | |
1649 | #[test] | |
1650 | fn test_ivr_demux() { | |
1651 | let mut file = | |
1652 | File::open("assets/RV/Opener_rm_hi.ivr").unwrap(); | |
1653 | // File::open("assets/RV/SherwinWilliamsCommercial.ivr").unwrap(); | |
1654 | let mut fr = FileReader::new_read(&mut file); | |
1655 | let mut br = ByteReader::new(&mut fr); | |
1656 | let mut dmx = RealIVRDemuxer::new(&mut br); | |
1657 | let mut sm = StreamManager::new(); | |
1658 | dmx.open(&mut sm).unwrap(); | |
1659 | ||
d1304e6e KS |
1660 | loop { |
1661 | let pktres = dmx.get_frame(&mut sm); | |
1662 | if let Err(e) = pktres { | |
1663 | if e == DemuxerError::EOF { break; } | |
1664 | panic!("error"); | |
1665 | } | |
1666 | let pkt = pktres.unwrap(); | |
1667 | println!("Got {}", pkt); | |
1668 | } | |
1669 | } | |
ce52b3b5 | 1670 | } |