1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
|
use std::io::SeekFrom;
use nihav_core::codecs::*;
use nihav_core::demuxers::*;
use nihav_registry::detect;
use nihav_core::io::byteio::ByteReader;
use nihav_allstuff::*;
pub struct FullRegister {
pub dmx_reg: RegisteredDemuxers,
pub rdmx_reg: RegisteredRawDemuxers,
pub pkt_reg: RegisteredPacketisers,
pub dec_reg: RegisteredDecoders,
}
impl FullRegister {
pub fn new() -> Self {
let mut dmx_reg = RegisteredDemuxers::new();
nihav_register_all_demuxers(&mut dmx_reg);
let mut rdmx_reg = RegisteredRawDemuxers::new();
nihav_register_all_raw_demuxers(&mut rdmx_reg);
let mut dec_reg = RegisteredDecoders::new();
nihav_register_all_decoders(&mut dec_reg);
let mut pkt_reg = RegisteredPacketisers::new();
nihav_register_all_packetisers(&mut pkt_reg);
Self { dmx_reg, rdmx_reg, pkt_reg, dec_reg }
}
}
pub struct RawStreamCtx<'a> {
stream: NAStreamRef,
pkt: Box<dyn NAPacketiser + Send>,
br: &'a mut ByteReader<'a>,
pts: u64,
seek: SeekIndex,
}
impl<'a> RawStreamCtx<'a> {
fn new(stream: NAStreamRef, packetiser: Box<dyn NAPacketiser + Send>, br: &'a mut ByteReader<'a>) -> Self {
let mut seek = SeekIndex::new();
seek.add_stream(0);
Self { stream, pkt: packetiser, br, pts: 0, seek }
}
fn account_for_packet(&mut self, packet: &mut NAPacket) {
let pos = self.br.tell() - (self.pkt.bytes_left() as u64);
if packet.get_pts().is_none() && packet.get_duration().is_some() {
packet.ts.pts = Some(self.pts);
}
if packet.is_keyframe() {
let pts = packet.get_pts().unwrap_or(self.pts);
let time = NATimeInfo::ts_to_time(pts, 1000, self.stream.tb_num, self.stream.tb_den);
let in_range = if let Some(last) = self.seek.seek_info[0].entries.last() {
last.pts >= pts
} else {
false
};
if !in_range {
self.seek.add_entry(0, SeekEntry { time, pts, pos });
}
}
self.pts += packet.get_duration().unwrap_or(0);
}
fn get_frame(&mut self) -> DemuxerResult<NAPacket> {
let mut buf = [0; 1048576];
loop {
match self.pkt.get_packet(self.stream.clone()) {
Ok(Some(mut packet)) => {
self.account_for_packet(&mut packet);
return Ok(packet);
},
Ok(None) => {},
Err(DecoderError::ShortData) => {},
_ => return Err(DemuxerError::InvalidData),
};
match self.br.read_buf_some(&mut buf) {
Ok(size) => {
self.pkt.add_data(&buf[..size]);
},
Err(_) => {
match self.pkt.get_packet(self.stream.clone()) {
Ok(Some(mut packet)) => {
self.account_for_packet(&mut packet);
return Ok(packet);
},
Ok(None) | Err(DecoderError::ShortData) => return Err(DemuxerError::EOF),
_ => return Err(DemuxerError::InvalidData),
};
},
};
}
}
}
pub enum DemuxerObject<'a> {
None,
Normal(Demuxer<'a>),
Raw(RawDemuxer<'a>, Vec<Option<Box<dyn NAPacketiser + Send>>>, bool),
RawStream(RawStreamCtx<'a>),
}
impl<'a> DemuxerObject<'a> {
pub fn create(br: &'a mut ByteReader<'a>, reg: &FullRegister, name: &str, force_dmx: Option<&str>, is_raw: bool, opts: &[NAOption]) -> DemuxerObject<'a> {
if !is_raw {
let res = detect::detect_format(name, br);
if let Some(dmx_name) = force_dmx {
println!("forcing demuxer {} on {}", dmx_name, name);
if let Some(dmx_fact) = reg.dmx_reg.find_demuxer(dmx_name) {
br.seek(SeekFrom::Start(0)).unwrap();
let dmx = create_demuxer_with_options(dmx_fact, br, opts).unwrap();
return DemuxerObject::Normal(dmx);
} else {
return DemuxerObject::None
}
}
let (dmx_name, _) = res.unwrap_or(("", detect::DetectionScore::No));
if dmx_name != "" {
println!("trying demuxer {} on {}", dmx_name, name);
if let Some(dmx_fact) = reg.dmx_reg.find_demuxer(dmx_name) {
br.seek(SeekFrom::Start(0)).unwrap();
let dmx = create_demuxer_with_options(dmx_fact, br, opts).unwrap();
return DemuxerObject::Normal(dmx);
}
}
if dmx_name != "" {
println!("trying raw demuxer {} on {}", dmx_name, name);
if let Some(rdmx_fact) = reg.rdmx_reg.find_demuxer(dmx_name) {
br.seek(SeekFrom::Start(0)).unwrap();
let dmx = create_raw_demuxer_with_options(rdmx_fact, br, opts).unwrap();
let mut pkts = Vec::new();
for stream in dmx.get_streams() {
if let Some(pcreate) = reg.pkt_reg.find_packetiser(stream.get_info().get_name()) {
let packetiser = (pcreate)();
pkts.push(Some(packetiser));
} else {
pkts.push(None);
}
}
return DemuxerObject::Raw(dmx, pkts, false);
}
}
for rdmx in reg.rdmx_reg.iter() {
if rdmx.check_format(br) {
println!("detected {} as {}", name, rdmx.get_name());
br.seek(SeekFrom::Start(0)).unwrap();
let dmx = create_raw_demuxer_with_options(*rdmx, br, opts).unwrap();
let mut pkts = Vec::new();
for stream in dmx.get_streams() {
if let Some(pcreate) = reg.pkt_reg.find_packetiser(stream.get_info().get_name()) {
let packetiser = (pcreate)();
pkts.push(Some(packetiser));
} else {
pkts.push(None);
}
}
return DemuxerObject::Raw(dmx, pkts, false);
}
}
}
br.seek(SeekFrom::Start(0)).unwrap();
let mut buf = vec![0; 1048576];
let size = br.read_buf_some(&mut buf).unwrap();
br.seek(SeekFrom::Start(0)).unwrap();
let mut pname = "";
for pinfo in reg.pkt_reg.iter() {
let mut packetiser = (pinfo.get_packetiser)();
packetiser.add_data(&buf[..size]);
if packetiser.parse_stream(0).is_ok() {
pname = pinfo.name;
break;
}
}
if pname != "" {
println!("found raw stream of type {} for {}", pname, name);
let pcreate = reg.pkt_reg.find_packetiser(pname).unwrap();
let mut packetiser = (pcreate)();
packetiser.add_data(&buf[..size]);
let stream = packetiser.parse_stream(0).unwrap();
packetiser.reset();
DemuxerObject::RawStream(RawStreamCtx::new(stream, packetiser, br))
} else {
DemuxerObject::None
}
}
pub fn is_none(&self) -> bool {
match *self {
DemuxerObject::None => true,
_ => false,
}
}
pub fn get_duration(&self) -> u64 {
match *self {
DemuxerObject::Normal(ref dmx) => dmx.get_duration(),
DemuxerObject::Raw(ref dmx, _, _) => dmx.get_duration(),
DemuxerObject::RawStream(ref ctx) => {
NATimeInfo::ts_to_time(ctx.stream.duration, 1000, ctx.stream.tb_num, ctx.stream.tb_den)
},
_ => 0,
}
}
pub fn get_num_streams(&self) -> usize {
match *self {
DemuxerObject::None => 0,
DemuxerObject::Normal(ref dmx) => dmx.get_num_streams(),
DemuxerObject::Raw(ref dmx, _, _) => dmx.get_num_streams(),
DemuxerObject::RawStream(_) => 1,
}
}
pub fn get_stream(&self, idx: usize) -> Option<NAStreamRef> {
match *self {
DemuxerObject::Normal(ref dmx) => dmx.get_stream(idx),
DemuxerObject::Raw(ref dmx, _, _) => dmx.get_stream(idx),
DemuxerObject::RawStream(ref ctx) if idx == 0 => Some(ctx.stream.clone()),
_ => None,
}
}
pub fn get_frame(&mut self) -> DemuxerResult<NAPacket> {
match *self {
DemuxerObject::Normal(ref mut dmx) => dmx.get_frame(),
DemuxerObject::Raw(ref mut dmx, ref mut packetisers, ref mut eof) => {
loop {
let mut has_some = false;
for (stream, p) in dmx.get_streams().zip(packetisers.iter_mut()) {
if let Some(ref mut pkts) = p {
match pkts.get_packet(stream.clone()) {
Ok(Some(pkt)) => return Ok(pkt),
Ok(None) | Err(DecoderError::ShortData) => {
if *eof {
*p = None;
}
},
Err(err) => {
println!("packetisation error {:?}", err);
return Err(DemuxerError::InvalidData);
}
};
has_some |= p.is_some();
}
}
if !has_some {
return Err(DemuxerError::EOF);
}
if let Ok(data) = dmx.get_data() {
let id = data.get_stream().get_id();
for (i, stream) in dmx.get_streams().enumerate() {
if stream.get_id() == id {
if let Some(ref mut pkts) = packetisers[i] {
pkts.add_data(&data.get_buffer());
}
break;
}
}
} else {
*eof = true;
}
}
},
DemuxerObject::RawStream(ref mut ctx) => ctx.get_frame(),
_ => unreachable!(),
}
}
pub fn seek(&mut self, seek_time: NATimePoint) -> DemuxerResult<()> {
match *self {
DemuxerObject::Normal(ref mut dmx) => dmx.seek(seek_time),
DemuxerObject::Raw(ref mut dmx, _, _) => dmx.seek(seek_time),
DemuxerObject::RawStream(ref mut ctx) => {
if seek_time == NATimePoint::None {
return Err(DemuxerError::SeekError);
}
if let Some(last) = ctx.seek.seek_info[0].entries.last() {
let in_index = match seek_time {
NATimePoint::None => unreachable!(),
NATimePoint::PTS(pts) => last.pts >= pts,
NATimePoint::Milliseconds(ms) => last.time >= ms,
};
if in_index {
if let Some(result) = ctx.seek.find_pos(seek_time) {
ctx.br.seek(SeekFrom::Start(result.pos))?;
ctx.pts = result.pts;
ctx.pkt.reset();
return Ok(());
}
}
}
if let Some(last) = ctx.seek.seek_info[0].entries.last() {
ctx.br.seek(SeekFrom::Start(last.pos))?;
ctx.pts = last.pts;
ctx.pkt.reset();
}
let mut key_pts = 0;
while let Ok(pkt) = ctx.get_frame() {
if !pkt.ts.less_than(seek_time) && !pkt.ts.equal(seek_time) {
break;
}
if pkt.is_keyframe() {
key_pts = pkt.get_pts().unwrap_or(0);
}
}
let result = ctx.seek.find_pos(NATimePoint::PTS(key_pts)).unwrap();
ctx.br.seek(SeekFrom::Start(result.pos))?;
ctx.pts = result.pts;
ctx.pkt.reset();
Ok(())
},
_ => Err(DemuxerError::NotImplemented),
}
}
}
pub fn detect_tags(br: &mut ByteReader) -> (bool, u64, Option<u64>) {
let mut is_raw = false;
let mut start = 0;
let mut end = None;
// check for ID3v{2-4}
let mut buf = [0; 5];
loop {
if br.peek_buf(&mut buf).is_err() {
break;
}
if &buf[0..3] == b"ID3" && buf[3] > 0 && buf[3] < 5 && buf[4] == 0 { //ID3 tag found, must be a raw stream
br.read_skip(6).unwrap();
let mut size = 0;
for _ in 0..4 {
let b = br.read_byte().unwrap();
if (b & 0x80) != 0 {
println!("Invalid ID3 size");
break;
}
size = (size << 7) | u64::from(b);
}
start += size + 10;
br.read_skip(size as usize).unwrap();
while let Ok(0) = br.read_byte() {
start += 1;
}
br.seek(SeekFrom::Start(start)).unwrap();
is_raw = true;
} else {
break;
}
}
// check for ID3v1
br.seek(SeekFrom::End(-128)).unwrap();
let off = br.tell();
br.peek_buf(&mut buf[..3]).unwrap();
if &buf[0..3] == b"TAG" {
end = Some(off);
// check for Lyrics v2
let mut sig = [0; 9];
br.seek(SeekFrom::End(-128 - 9)).unwrap();
br.peek_buf(&mut sig).unwrap();
if &sig == b"LYRICS200" {
br.seek(SeekFrom::Current(-6)).unwrap();
let mut sizestr = [0; 6];
br.peek_buf(&mut sizestr).unwrap();
if let Ok(sstr) = std::str::from_utf8(&sizestr) {
if let Ok(size) = sstr.parse::<u64>() {
end = Some(br.tell() - size);
}
}
}
}
// check for APETAG
let mut buf = [0; 8];
if let Some(off) = end {
br.seek(SeekFrom::Start(off - 32)).unwrap();
} else {
br.seek(SeekFrom::End(-32)).unwrap();
}
let off = br.tell();
br.read_buf(&mut buf).unwrap();
if &buf == b"APETAGEX" {
let ver = br.read_u32le().unwrap();
let size = u64::from(br.read_u32le().unwrap());
let _items = br.read_u32le().unwrap();
let flags = br.read_u32le().unwrap();
if ver == 1000 || (flags & 0x80000000) == 0 {
end = Some(off - size + 32);
} else {
end = Some(off - size);
}
}
// check for MusicMatch tag
let ret = if let Some(endpos) = end {
br.seek(SeekFrom::Start(endpos - 0x30))
} else {
br.seek(SeekFrom::End(-0x30))
};
if ret.is_ok() && br.tell() > (0x2000 - 0x30) {
let mut buf = [0; 19];
br.peek_buf(&mut buf).unwrap();
if &buf == b"Brava Software Inc." {
br.seek(SeekFrom::Current(-20)).unwrap();
let mut mm_start = u64::from(br.read_u32le().unwrap());
if mm_start > 4 && mm_start + 0x2000 <= br.tell() {
let diff = (br.tell() - mm_start) & 3;
if diff != 0 {
mm_start -= 4 - diff;
}
end = Some(mm_start);
}
}
}
(is_raw, start, end)
}
|