]> git.nihav.org Git - nihav-tool.git/blob - demux.rs
07862753433ae696796240477228aaa512febd8b
[nihav-tool.git] / demux.rs
1 use std::io::SeekFrom;
2 use nihav_core::codecs::*;
3 use nihav_core::demuxers::*;
4 use nihav_registry::detect;
5 use nihav_core::io::byteio::ByteReader;
6 use nihav_allstuff::*;
7
8 pub struct FullRegister {
9 pub dmx_reg: RegisteredDemuxers,
10 pub rdmx_reg: RegisteredRawDemuxers,
11 pub pkt_reg: RegisteredPacketisers,
12 pub dec_reg: RegisteredDecoders,
13 }
14
15 impl FullRegister {
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 }
26 }
27 }
28
29 pub struct RawStreamCtx<'a> {
30 stream: NAStreamRef,
31 pkt: Box<dyn NAPacketiser + Send>,
32 br: &'a mut ByteReader<'a>,
33 pts: u64,
34 seek: SeekIndex,
35 }
36
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();
40 seek.add_stream(0);
41 Self { stream, pkt: packetiser, br, pts: 0, seek }
42 }
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);
47 }
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() {
52 last.pts >= pts
53 } else {
54 false
55 };
56 if !in_range {
57 self.seek.add_entry(0, SeekEntry { time, pts, pos });
58 }
59 }
60 self.pts += packet.get_duration().unwrap_or(0);
61 }
62 fn get_frame(&mut self) -> DemuxerResult<NAPacket> {
63 let mut buf = [0; 1048576];
64 loop {
65 match self.pkt.get_packet(self.stream.clone()) {
66 Ok(Some(mut packet)) => {
67 self.account_for_packet(&mut packet);
68 return Ok(packet);
69 },
70 Ok(None) => {},
71 Err(DecoderError::ShortData) => {},
72 _ => return Err(DemuxerError::InvalidData),
73 };
74 match self.br.read_buf_some(&mut buf) {
75 Ok(size) => {
76 self.pkt.add_data(&buf[..size]);
77 },
78 Err(_) => {
79 match self.pkt.get_packet(self.stream.clone()) {
80 Ok(Some(mut packet)) => {
81 self.account_for_packet(&mut packet);
82 return Ok(packet);
83 },
84 Ok(None) | Err(DecoderError::ShortData) => return Err(DemuxerError::EOF),
85 _ => return Err(DemuxerError::InvalidData),
86 };
87 },
88 };
89 }
90 }
91 }
92
93 pub enum DemuxerObject<'a> {
94 None,
95 Normal(Demuxer<'a>),
96 Raw(RawDemuxer<'a>, Vec<Option<Box<dyn NAPacketiser + Send>>>, bool),
97 RawStream(RawStreamCtx<'a>),
98 }
99
100 impl<'a> DemuxerObject<'a> {
101 pub fn create(br: &'a mut ByteReader<'a>, reg: &FullRegister, name: &str, is_raw: bool) -> DemuxerObject<'a> {
102 if !is_raw {
103 let res = detect::detect_format(name, br);
104 let (dmx_name, _) = res.unwrap_or(("", detect::DetectionScore::No));
105 if dmx_name != "" {
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);
111 }
112 }
113 if dmx_name != "" {
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));
123 } else {
124 pkts.push(None);
125 }
126 }
127 return DemuxerObject::Raw(dmx, pkts, false);
128 }
129 }
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));
140 } else {
141 pkts.push(None);
142 }
143 }
144 return DemuxerObject::Raw(dmx, pkts, false);
145 }
146 }
147 }
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();
152 let mut pname = "";
153
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() {
158 pname = pinfo.name;
159 break;
160 }
161 }
162 if pname != "" {
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();
168 packetiser.reset();
169 DemuxerObject::RawStream(RawStreamCtx::new(stream, packetiser, br))
170 } else {
171 DemuxerObject::None
172 }
173 }
174 pub fn is_none(&self) -> bool {
175 match *self {
176 DemuxerObject::None => true,
177 _ => false,
178 }
179 }
180 pub fn get_duration(&self) -> u64 {
181 match *self {
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)
186 },
187 _ => 0,
188 }
189 }
190 pub fn get_num_streams(&self) -> usize {
191 match *self {
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,
196 }
197 }
198 pub fn get_stream(&self, idx: usize) -> Option<NAStreamRef> {
199 match *self {
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()),
203 _ => None,
204 }
205 }
206 pub fn get_frame(&mut self) -> DemuxerResult<NAPacket> {
207 match *self {
208 DemuxerObject::Normal(ref mut dmx) => dmx.get_frame(),
209 DemuxerObject::Raw(ref mut dmx, ref mut packetisers, ref mut eof) => {
210 loop {
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) => {
217 if *eof {
218 *p = None;
219 }
220 },
221 Err(err) => {
222 println!("packetisation error {:?}", err);
223 return Err(DemuxerError::InvalidData);
224 }
225 };
226 has_some |= p.is_some();
227 }
228 }
229 if !has_some {
230 return Err(DemuxerError::EOF);
231 }
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());
238 }
239 break;
240 }
241 }
242 } else {
243 *eof = true;
244 }
245 }
246 },
247 DemuxerObject::RawStream(ref mut ctx) => ctx.get_frame(),
248 _ => unreachable!(),
249 }
250 }
251 pub fn seek(&mut self, seek_time: NATimePoint) -> DemuxerResult<()> {
252 match *self {
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);
258 }
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,
264 };
265 if in_index {
266 if let Some(result) = ctx.seek.find_pos(seek_time) {
267 ctx.br.seek(SeekFrom::Start(result.pos))?;
268 ctx.pts = result.pts;
269 ctx.pkt.reset();
270 return Ok(());
271 }
272 }
273 }
274 if let Some(last) = ctx.seek.seek_info[0].entries.last() {
275 ctx.br.seek(SeekFrom::Start(last.pos))?;
276 ctx.pts = last.pts;
277 ctx.pkt.reset();
278 }
279 let mut key_pts = 0;
280 while let Ok(pkt) = ctx.get_frame() {
281 if !pkt.ts.less_than(seek_time) && !pkt.ts.equal(seek_time) {
282 break;
283 }
284 if pkt.is_keyframe() {
285 key_pts = pkt.get_pts().unwrap_or(0);
286 }
287 }
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;
291 ctx.pkt.reset();
292 Ok(())
293 },
294 _ => Err(DemuxerError::NotImplemented),
295 }
296 }
297 }
298
299 pub fn detect_tags(br: &mut ByteReader) -> (bool, u64, Option<u64>) {
300 let mut is_raw = false;
301 let mut start = 0;
302 let mut end = None;
303
304 // check for ID3v{2-4}
305 let mut buf = [0; 5];
306 loop {
307 if br.peek_buf(&mut buf).is_err() {
308 break;
309 }
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();
312 let mut size = 0;
313 for _ in 0..4 {
314 let b = br.read_byte().unwrap();
315 if (b & 0x80) != 0 {
316 println!("Invalid ID3 size");
317 break;
318 }
319 size = (size << 7) | u64::from(b);
320 }
321 start += size + 10;
322 br.read_skip(size as usize).unwrap();
323 while let Ok(0) = br.read_byte() {
324 start += 1;
325 }
326 br.seek(SeekFrom::Start(start)).unwrap();
327 is_raw = true;
328 } else {
329 break;
330 }
331 }
332 // check for ID3v1
333 br.seek(SeekFrom::End(-128)).unwrap();
334 let off = br.tell();
335 br.peek_buf(&mut buf[..3]).unwrap();
336 if &buf[0..3] == b"TAG" {
337 end = Some(off);
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);
349 }
350 }
351 }
352 }
353 // check for APETAG
354 let mut buf = [0; 8];
355 if let Some(off) = end {
356 br.seek(SeekFrom::Start(off - 32)).unwrap();
357 } else {
358 br.seek(SeekFrom::End(-32)).unwrap();
359 }
360 let off = br.tell();
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);
369 } else {
370 end = Some(off - size);
371 }
372 }
373 // check for MusicMatch tag
374 let ret = if let Some(endpos) = end {
375 br.seek(SeekFrom::Start(endpos - 0x30))
376 } else {
377 br.seek(SeekFrom::End(-0x30))
378 };
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;
387 if diff != 0 {
388 mm_start -= 4 - diff;
389 }
390 end = Some(mm_start);
391 }
392 }
393 }
394
395 (is_raw, start, end)
396 }