2 use nihav_core::codecs::*;
3 use nihav_core::demuxers::*;
4 use nihav_core::muxers::*;
5 use nihav_registry::detect;
6 use nihav_core::io::byteio::ByteReader;
10 pub struct FullRegister {
11 pub dmx_reg: RegisteredDemuxers,
12 pub rdmx_reg: RegisteredRawDemuxers,
13 pub pkt_reg: RegisteredPacketisers,
14 pub dec_reg: RegisteredDecoders,
15 pub enc_reg: RegisteredEncoders,
16 pub mux_reg: RegisteredMuxers,
20 pub fn new() -> Self {
21 let mut dmx_reg = RegisteredDemuxers::new();
22 nihav_register_all_demuxers(&mut dmx_reg);
23 let mut rdmx_reg = RegisteredRawDemuxers::new();
24 nihav_register_all_raw_demuxers(&mut rdmx_reg);
25 let mut dec_reg = RegisteredDecoders::new();
26 nihav_register_all_decoders(&mut dec_reg);
27 let mut pkt_reg = RegisteredPacketisers::new();
28 nihav_register_all_packetisers(&mut pkt_reg);
29 let mut enc_reg = RegisteredEncoders::new();
30 nihav_register_all_encoders(&mut enc_reg);
31 enc_reg.add_encoder(NULL_ENCODER);
32 let mut mux_reg = RegisteredMuxers::new();
33 nihav_register_all_muxers(&mut mux_reg);
34 mux_reg.add_muxer(NULL_MUXER);
35 Self { dmx_reg, rdmx_reg, pkt_reg, dec_reg, enc_reg, mux_reg }
39 pub struct RawStreamCtx<'a> {
42 packetiser: Box<dyn NAPacketiser + Send>,
43 br: &'a mut ByteReader<'a>,
46 impl<'a> RawStreamCtx<'a> {
47 fn new(stream: NAStreamRef, packetiser: Box<dyn NAPacketiser + Send>, br: &'a mut ByteReader<'a>) -> Self {
48 let mut sm = StreamManager::new();
49 sm.add_stream_ref(stream.clone());
50 Self { stream, sm, packetiser, br }
54 pub enum DemuxerObject<'a> {
57 Raw(RawDemuxer<'a>, Vec<Option<Box<dyn NAPacketiser + Send>>>, bool),
58 RawStream(RawStreamCtx<'a>),
61 impl<'a> DemuxerObject<'a> {
62 pub fn create(br: &'a mut ByteReader<'a>, reg: &FullRegister, name: &str, ifmt: &Option<String>, is_raw: bool) -> DemuxerObject<'a> {
64 let dmx_name = if let Some(ref dname) = ifmt {
66 } else if let Some((dname, score)) = detect::detect_format(name, br) {
67 println!("detected {} with score {:?}", dname, score);
73 println!("trying demuxer {} on {}", dmx_name, name);
74 if let Some(dmx_fact) = reg.dmx_reg.find_demuxer(dmx_name) {
75 br.seek(SeekFrom::Start(0)).unwrap();
76 let dmx = create_demuxer(dmx_fact, br).unwrap();
77 return DemuxerObject::Normal(dmx);
81 return DemuxerObject::None;
84 println!("trying raw demuxer {} on {}", dmx_name, name);
85 if let Some(rdmx_fact) = reg.rdmx_reg.find_demuxer(dmx_name) {
86 br.seek(SeekFrom::Start(0)).unwrap();
87 let dmx = create_raw_demuxer(rdmx_fact, br).unwrap();
88 let mut pkts = Vec::new();
89 for stream in dmx.get_streams() {
90 if let Some(pcreate) = reg.pkt_reg.find_packetiser(stream.get_info().get_name()) {
91 let packetiser = (pcreate)();
92 pkts.push(Some(packetiser));
97 return DemuxerObject::Raw(dmx, pkts, false);
100 for rdmx in reg.rdmx_reg.iter() {
101 if rdmx.check_format(br) {
102 println!("detected {} as {}", name, rdmx.get_name());
103 br.seek(SeekFrom::Start(0)).unwrap();
104 let dmx = create_raw_demuxer(*rdmx, br).unwrap();
105 let mut pkts = Vec::new();
106 for stream in dmx.get_streams() {
107 if let Some(pcreate) = reg.pkt_reg.find_packetiser(stream.get_info().get_name()) {
108 let packetiser = (pcreate)();
109 pkts.push(Some(packetiser));
114 return DemuxerObject::Raw(dmx, pkts, false);
118 br.seek(SeekFrom::Start(0)).unwrap();
119 let mut buf = vec![0; 1048576];
120 let size = br.peek_buf(&mut buf).unwrap();
123 for pinfo in reg.pkt_reg.iter() {
124 let mut packetiser = (pinfo.get_packetiser)();
125 packetiser.add_data(&buf[..size]);
126 if packetiser.parse_stream(0).is_ok() {
132 println!("found raw stream of type {} for {}", pname, name);
133 let pcreate = reg.pkt_reg.find_packetiser(pname).unwrap();
134 let mut packetiser = (pcreate)();
135 packetiser.add_data(&buf[..size]);
136 let stream = packetiser.parse_stream(0).unwrap();
138 DemuxerObject::RawStream(RawStreamCtx::new(stream, packetiser, br))
143 pub fn is_none(&self) -> bool {
145 DemuxerObject::None => true,
149 /*pub fn get_duration(&self) -> u64 {
151 DemuxerObject::Normal(ref dmx) => dmx.get_duration(),
152 DemuxerObject::Raw(ref dmx, _, _) => dmx.get_duration(),
156 pub fn get_num_streams(&self) -> usize {
158 DemuxerObject::None => 0,
159 DemuxerObject::Normal(ref dmx) => dmx.get_num_streams(),
160 DemuxerObject::Raw(ref dmx, _, _) => dmx.get_num_streams(),
161 DemuxerObject::RawStream(_) => 1,
164 pub fn get_stream(&self, idx: usize) -> Option<NAStreamRef> {
166 DemuxerObject::Normal(ref dmx) => dmx.get_stream(idx),
167 DemuxerObject::Raw(ref dmx, _, _) => dmx.get_stream(idx),
168 DemuxerObject::RawStream(ref ctx) if idx == 0 => Some(ctx.stream.clone()),
172 pub fn get_streams(&self) -> StreamIter {
174 DemuxerObject::Normal(ref dmx) => dmx.get_streams(),
175 DemuxerObject::Raw(ref dmx, _, _) => dmx.get_streams(),
176 DemuxerObject::RawStream(ref ctx) => ctx.sm.iter(),
180 pub fn get_stream_manager(&self) -> &StreamManager {
182 DemuxerObject::Normal(ref dmx) => dmx.get_stream_manager(),
183 DemuxerObject::Raw(ref dmx, _, _) => dmx.get_stream_manager(),
184 DemuxerObject::RawStream(ref ctx) => &ctx.sm,
188 pub fn get_frame(&mut self) -> DemuxerResult<NAPacket> {
190 DemuxerObject::Normal(ref mut dmx) => dmx.get_frame(),
191 DemuxerObject::Raw(ref mut dmx, ref mut packetisers, ref mut eof) => {
193 let mut has_some = false;
194 for (stream, p) in dmx.get_streams().zip(packetisers.iter_mut()) {
195 if let Some(ref mut pkts) = p {
196 match pkts.get_packet(stream.clone()) {
197 Ok(Some(pkt)) => return Ok(pkt),
198 Ok(None) | Err(DecoderError::ShortData) => {
204 println!("packetisation error {:?}", err);
205 return Err(DemuxerError::InvalidData);
208 has_some |= p.is_some();
212 return Err(DemuxerError::EOF);
214 if let Ok(data) = dmx.get_data() {
215 let id = data.get_stream().get_id();
216 for (i, stream) in dmx.get_streams().enumerate() {
217 if stream.get_id() == id {
218 if let Some(ref mut pkts) = packetisers[i] {
219 pkts.add_data(&data.get_buffer());
229 DemuxerObject::RawStream(ref mut ctx) => {
230 let mut buf = [0; 65536];
232 match ctx.packetiser.get_packet(ctx.stream.clone()) {
233 Ok(Some(packet)) => return Ok(packet),
235 Err(DecoderError::ShortData) => {},
236 _ => return Err(DemuxerError::InvalidData),
238 match ctx.br.read_buf_some(&mut buf) {
240 ctx.packetiser.add_data(&buf[..size]);
243 match ctx.packetiser.get_packet(ctx.stream.clone()) {
244 Ok(Some(packet)) => return Ok(packet),
245 Ok(None) | Err(DecoderError::ShortData) => return Err(DemuxerError::EOF),
246 _ => return Err(DemuxerError::InvalidData),
255 pub fn seek(&mut self, seek_time: NATimePoint) -> DemuxerResult<()> {
257 DemuxerObject::Normal(ref mut dmx) => dmx.seek(seek_time),
258 DemuxerObject::Raw(ref mut dmx, _, _) => dmx.seek(seek_time),
259 _ => Err(DemuxerError::NotImplemented),
264 impl<'a> NAOptionHandler for DemuxerObject<'a> {
265 fn get_supported_options(&self) -> &[NAOptionDefinition] {
267 DemuxerObject::Normal(ref dmx) => dmx.get_supported_options(),
268 DemuxerObject::Raw(ref dmx, _, _) => dmx.get_supported_options(),
272 fn set_options(&mut self, options: &[NAOption]) {
274 DemuxerObject::Normal(ref mut dmx) => dmx.set_options(options),
275 DemuxerObject::Raw(ref mut dmx, _, _) => dmx.set_options(options),
279 fn query_option_value(&self, name: &str) -> Option<NAValue> {
281 DemuxerObject::Normal(ref dmx) => dmx.query_option_value(name),
282 DemuxerObject::Raw(ref dmx, _, _) => dmx.query_option_value(name),
288 pub fn detect_tags(br: &mut ByteReader) -> (bool, u64, Option<u64>) {
289 let mut is_raw = false;
293 // check for ID3v{2-4}
294 let mut buf = [0; 5];
295 br.peek_buf(&mut buf).unwrap();
296 if &buf[0..3] == b"ID3" && buf[3] > 0 && buf[3] < 5 && buf[4] == 0 { //ID3 tag found, must be a raw stream
297 br.read_skip(6).unwrap();
300 let b = br.read_byte().unwrap();
302 println!("Invalid ID3 size");
305 size = (size << 7) | u64::from(b);
311 br.seek(SeekFrom::End(-128)).unwrap();
313 br.peek_buf(&mut buf[..3]).unwrap();
314 if &buf[0..3] == b"TAG" {
318 let mut buf = [0; 8];
319 if let Some(off) = end {
320 br.seek(SeekFrom::Start(off - 32)).unwrap();
322 br.seek(SeekFrom::End(-32)).unwrap();
325 br.read_buf(&mut buf).unwrap();
326 if &buf == b"APETAGEX" {
327 let ver = br.read_u32le().unwrap();
328 let size = u64::from(br.read_u32le().unwrap());
329 let _items = br.read_u32le().unwrap();
330 let flags = br.read_u32le().unwrap();
331 if ver == 1000 || (flags & 0x80000000) == 0 {
332 end = Some(off - size + 32);
334 end = Some(off - size);