2 use nihav_core::codecs::*;
3 use nihav_core::demuxers::*;
4 use nihav_registry::detect;
5 use nihav_core::io::byteio::ByteReader;
8 pub struct FullRegister {
9 pub dmx_reg: RegisteredDemuxers,
10 pub rdmx_reg: RegisteredRawDemuxers,
11 pub pkt_reg: RegisteredPacketisers,
12 pub dec_reg: RegisteredDecoders,
16 pub fn new() -> Self {
17 let mut dmx_reg = RegisteredDemuxers::new();
18 nihav_register_all_demuxers(&mut dmx_reg);
19 let mut rdmx_reg = RegisteredRawDemuxers::new();
20 nihav_register_all_raw_demuxers(&mut rdmx_reg);
21 let mut dec_reg = RegisteredDecoders::new();
22 nihav_register_all_decoders(&mut dec_reg);
23 let mut pkt_reg = RegisteredPacketisers::new();
24 nihav_register_all_packetisers(&mut pkt_reg);
25 Self { dmx_reg, rdmx_reg, pkt_reg, dec_reg }
29 pub struct RawStreamCtx<'a> {
31 pkt: Box<dyn NAPacketiser + Send>,
32 br: &'a mut ByteReader<'a>,
37 impl<'a> RawStreamCtx<'a> {
38 fn new(stream: NAStreamRef, packetiser: Box<dyn NAPacketiser + Send>, br: &'a mut ByteReader<'a>) -> Self {
39 let mut seek = SeekIndex::new();
41 Self { stream, pkt: packetiser, br, pts: 0, seek }
43 fn account_for_packet(&mut self, packet: &mut NAPacket) {
44 let pos = self.br.tell() - (self.pkt.bytes_left() as u64);
45 if packet.get_pts().is_none() && packet.get_duration().is_some() {
46 packet.ts.pts = Some(self.pts);
48 if packet.is_keyframe() {
49 let pts = packet.get_pts().unwrap_or(self.pts);
50 let time = NATimeInfo::ts_to_time(pts, 1000, self.stream.tb_num, self.stream.tb_den);
51 let in_range = if let Some(last) = self.seek.seek_info[0].entries.last() {
57 self.seek.add_entry(0, SeekEntry { time, pts, pos });
60 self.pts += packet.get_duration().unwrap_or(0);
62 fn get_frame(&mut self) -> DemuxerResult<NAPacket> {
63 let mut buf = [0; 1048576];
65 match self.pkt.get_packet(self.stream.clone()) {
66 Ok(Some(mut packet)) => {
67 self.account_for_packet(&mut packet);
71 Err(DecoderError::ShortData) => {},
72 _ => return Err(DemuxerError::InvalidData),
74 match self.br.read_buf_some(&mut buf) {
76 self.pkt.add_data(&buf[..size]);
79 match self.pkt.get_packet(self.stream.clone()) {
80 Ok(Some(mut packet)) => {
81 self.account_for_packet(&mut packet);
84 Ok(None) | Err(DecoderError::ShortData) => return Err(DemuxerError::EOF),
85 _ => return Err(DemuxerError::InvalidData),
93 pub enum DemuxerObject<'a> {
96 Raw(RawDemuxer<'a>, Vec<Option<Box<dyn NAPacketiser + Send>>>, bool),
97 RawStream(RawStreamCtx<'a>),
100 impl<'a> DemuxerObject<'a> {
101 pub fn create(br: &'a mut ByteReader<'a>, reg: &FullRegister, name: &str, is_raw: bool) -> DemuxerObject<'a> {
103 let res = detect::detect_format(name, br);
104 let (dmx_name, _) = res.unwrap_or(("", detect::DetectionScore::No));
106 println!("trying demuxer {} on {}", dmx_name, name);
107 if let Some(dmx_fact) = reg.dmx_reg.find_demuxer(dmx_name) {
108 br.seek(SeekFrom::Start(0)).unwrap();
109 let dmx = create_demuxer(dmx_fact, br).unwrap();
110 return DemuxerObject::Normal(dmx);
114 println!("trying raw demuxer {} on {}", dmx_name, name);
115 if let Some(rdmx_fact) = reg.rdmx_reg.find_demuxer(dmx_name) {
116 br.seek(SeekFrom::Start(0)).unwrap();
117 let dmx = create_raw_demuxer(rdmx_fact, br).unwrap();
118 let mut pkts = Vec::new();
119 for stream in dmx.get_streams() {
120 if let Some(pcreate) = reg.pkt_reg.find_packetiser(stream.get_info().get_name()) {
121 let packetiser = (pcreate)();
122 pkts.push(Some(packetiser));
127 return DemuxerObject::Raw(dmx, pkts, false);
130 for rdmx in reg.rdmx_reg.iter() {
131 if rdmx.check_format(br) {
132 println!("detected {} as {}", name, rdmx.get_name());
133 br.seek(SeekFrom::Start(0)).unwrap();
134 let dmx = create_raw_demuxer(*rdmx, br).unwrap();
135 let mut pkts = Vec::new();
136 for stream in dmx.get_streams() {
137 if let Some(pcreate) = reg.pkt_reg.find_packetiser(stream.get_info().get_name()) {
138 let packetiser = (pcreate)();
139 pkts.push(Some(packetiser));
144 return DemuxerObject::Raw(dmx, pkts, false);
148 br.seek(SeekFrom::Start(0)).unwrap();
149 let mut buf = vec![0; 1048576];
150 let size = br.read_buf_some(&mut buf).unwrap();
151 br.seek(SeekFrom::Start(0)).unwrap();
154 for pinfo in reg.pkt_reg.iter() {
155 let mut packetiser = (pinfo.get_packetiser)();
156 packetiser.add_data(&buf[..size]);
157 if packetiser.parse_stream(0).is_ok() {
163 println!("found raw stream of type {} for {}", pname, name);
164 let pcreate = reg.pkt_reg.find_packetiser(pname).unwrap();
165 let mut packetiser = (pcreate)();
166 packetiser.add_data(&buf[..size]);
167 let stream = packetiser.parse_stream(0).unwrap();
169 DemuxerObject::RawStream(RawStreamCtx::new(stream, packetiser, br))
174 pub fn is_none(&self) -> bool {
176 DemuxerObject::None => true,
180 pub fn get_duration(&self) -> u64 {
182 DemuxerObject::Normal(ref dmx) => dmx.get_duration(),
183 DemuxerObject::Raw(ref dmx, _, _) => dmx.get_duration(),
184 DemuxerObject::RawStream(ref ctx) => {
185 NATimeInfo::ts_to_time(ctx.stream.duration, 1000, ctx.stream.tb_num, ctx.stream.tb_den)
190 pub fn get_num_streams(&self) -> usize {
192 DemuxerObject::None => 0,
193 DemuxerObject::Normal(ref dmx) => dmx.get_num_streams(),
194 DemuxerObject::Raw(ref dmx, _, _) => dmx.get_num_streams(),
195 DemuxerObject::RawStream(_) => 1,
198 pub fn get_stream(&self, idx: usize) -> Option<NAStreamRef> {
200 DemuxerObject::Normal(ref dmx) => dmx.get_stream(idx),
201 DemuxerObject::Raw(ref dmx, _, _) => dmx.get_stream(idx),
202 DemuxerObject::RawStream(ref ctx) if idx == 0 => Some(ctx.stream.clone()),
206 pub fn get_frame(&mut self) -> DemuxerResult<NAPacket> {
208 DemuxerObject::Normal(ref mut dmx) => dmx.get_frame(),
209 DemuxerObject::Raw(ref mut dmx, ref mut packetisers, ref mut eof) => {
211 let mut has_some = false;
212 for (stream, p) in dmx.get_streams().zip(packetisers.iter_mut()) {
213 if let Some(ref mut pkts) = p {
214 match pkts.get_packet(stream.clone()) {
215 Ok(Some(pkt)) => return Ok(pkt),
216 Ok(None) | Err(DecoderError::ShortData) => {
222 println!("packetisation error {:?}", err);
223 return Err(DemuxerError::InvalidData);
226 has_some |= p.is_some();
230 return Err(DemuxerError::EOF);
232 if let Ok(data) = dmx.get_data() {
233 let id = data.get_stream().get_id();
234 for (i, stream) in dmx.get_streams().enumerate() {
235 if stream.get_id() == id {
236 if let Some(ref mut pkts) = packetisers[i] {
237 pkts.add_data(&data.get_buffer());
247 DemuxerObject::RawStream(ref mut ctx) => ctx.get_frame(),
251 pub fn seek(&mut self, seek_time: NATimePoint) -> DemuxerResult<()> {
253 DemuxerObject::Normal(ref mut dmx) => dmx.seek(seek_time),
254 DemuxerObject::Raw(ref mut dmx, _, _) => dmx.seek(seek_time),
255 DemuxerObject::RawStream(ref mut ctx) => {
256 if seek_time == NATimePoint::None {
257 return Err(DemuxerError::SeekError);
259 if let Some(last) = ctx.seek.seek_info[0].entries.last() {
260 let in_index = match seek_time {
261 NATimePoint::None => unreachable!(),
262 NATimePoint::PTS(pts) => last.pts >= pts,
263 NATimePoint::Milliseconds(ms) => last.time >= ms,
266 if let Some(result) = ctx.seek.find_pos(seek_time) {
267 ctx.br.seek(SeekFrom::Start(result.pos))?;
268 ctx.pts = result.pts;
274 if let Some(last) = ctx.seek.seek_info[0].entries.last() {
275 ctx.br.seek(SeekFrom::Start(last.pos))?;
280 while let Ok(pkt) = ctx.get_frame() {
281 if !pkt.ts.less_than(seek_time) && !pkt.ts.equal(seek_time) {
284 if pkt.is_keyframe() {
285 key_pts = pkt.get_pts().unwrap_or(0);
288 let result = ctx.seek.find_pos(NATimePoint::PTS(key_pts)).unwrap();
289 ctx.br.seek(SeekFrom::Start(result.pos))?;
290 ctx.pts = result.pts;
294 _ => Err(DemuxerError::NotImplemented),
299 pub fn detect_tags(br: &mut ByteReader) -> (bool, u64, Option<u64>) {
300 let mut is_raw = false;
304 // check for ID3v{2-4}
305 let mut buf = [0; 5];
307 if br.peek_buf(&mut buf).is_err() {
310 if &buf[0..3] == b"ID3" && buf[3] > 0 && buf[3] < 5 && buf[4] == 0 { //ID3 tag found, must be a raw stream
311 br.read_skip(6).unwrap();
314 let b = br.read_byte().unwrap();
316 println!("Invalid ID3 size");
319 size = (size << 7) | u64::from(b);
322 br.read_skip(size as usize).unwrap();
323 while let Ok(0) = br.read_byte() {
326 br.seek(SeekFrom::Start(start)).unwrap();
333 br.seek(SeekFrom::End(-128)).unwrap();
335 br.peek_buf(&mut buf[..3]).unwrap();
336 if &buf[0..3] == b"TAG" {
338 // check for Lyrics v2
339 let mut sig = [0; 9];
340 br.seek(SeekFrom::End(-128 - 9)).unwrap();
341 br.peek_buf(&mut sig).unwrap();
342 if &sig == b"LYRICS200" {
343 br.seek(SeekFrom::Current(-6)).unwrap();
344 let mut sizestr = [0; 6];
345 br.peek_buf(&mut sizestr).unwrap();
346 if let Ok(sstr) = std::str::from_utf8(&sizestr) {
347 if let Ok(size) = sstr.parse::<u64>() {
348 end = Some(br.tell() - size);
354 let mut buf = [0; 8];
355 if let Some(off) = end {
356 br.seek(SeekFrom::Start(off - 32)).unwrap();
358 br.seek(SeekFrom::End(-32)).unwrap();
361 br.read_buf(&mut buf).unwrap();
362 if &buf == b"APETAGEX" {
363 let ver = br.read_u32le().unwrap();
364 let size = u64::from(br.read_u32le().unwrap());
365 let _items = br.read_u32le().unwrap();
366 let flags = br.read_u32le().unwrap();
367 if ver == 1000 || (flags & 0x80000000) == 0 {
368 end = Some(off - size + 32);
370 end = Some(off - size);
373 // check for MusicMatch tag
374 let ret = if let Some(endpos) = end {
375 br.seek(SeekFrom::Start(endpos - 0x30))
377 br.seek(SeekFrom::End(-0x30))
379 if ret.is_ok() && br.tell() > (0x2000 - 0x30) {
380 let mut buf = [0; 19];
381 br.peek_buf(&mut buf).unwrap();
382 if &buf == b"Brava Software Inc." {
383 br.seek(SeekFrom::Current(-20)).unwrap();
384 let mut mm_start = u64::from(br.read_u32le().unwrap());
385 if mm_start > 4 && mm_start + 0x2000 <= br.tell() {
386 let diff = (br.tell() - mm_start) & 3;
388 mm_start -= 4 - diff;
390 end = Some(mm_start);