1 use nihav_core::demuxers::*;
3 struct IVFDemuxer<'a> {
4 src: &'a mut ByteReader<'a>,
9 impl<'a> IVFDemuxer<'a> {
10 fn new(src: &'a mut ByteReader<'a>) -> Self {
19 impl<'a> DemuxCore<'a> for IVFDemuxer<'a> {
20 fn open(&mut self, strmgr: &mut StreamManager, _seek_index: &mut SeekIndex) -> DemuxerResult<()> {
21 let tag = self.src.read_tag()?;
22 validate!(&tag == b"DKIF");
23 let ver = self.src.read_u16le()?;
25 let hdr_len = self.src.read_u16le()? as usize;
26 validate!(hdr_len >= 32);
27 let fcc = self.src.read_tag()?;
28 let codec_name = match &fcc {
32 let width = self.src.read_u16le()? as usize;
33 let height = self.src.read_u16le()? as usize;
34 validate!(width > 0 && height > 0);
35 let tb_den = self.src.read_u32le()?;
36 let tb_num = self.src.read_u32le()?;
37 self.nframes = self.src.read_u32le()?;
40 self.src.seek(SeekFrom::Start(hdr_len as u64))?;
42 let vci = NACodecTypeInfo::Video(NAVideoInfo::new(width, height, false, YUV420_FORMAT));
43 let vinfo = NACodecInfo::new(codec_name, vci, None);
44 if strmgr.add_stream(NAStream::new(StreamType::Video, 0, vinfo, tb_num, tb_den, u64::from(self.nframes))).is_none() {
45 return Err(DemuxerError::MemoryError);
51 fn get_frame(&mut self, strmgr: &mut StreamManager) -> DemuxerResult<NAPacket> {
52 while self.frameno < self.nframes {
53 let fsize = self.src.read_u32le()? as usize;
54 let tstamp = self.src.read_u64le()?;
61 if let Some(stream) = strmgr.get_stream(0) {
62 let ts = stream.make_ts(Some(tstamp), None, None);
63 return self.src.read_packet(stream, ts, false, fsize);
65 return Err(DemuxerError::InvalidData);
68 Err(DemuxerError::EOF)
71 fn seek(&mut self, _time: NATimePoint, _seek_index: &SeekIndex) -> DemuxerResult<()> {
72 Err(DemuxerError::NotPossible)
74 fn get_duration(&self) -> u64 { 0 }
77 impl<'a> NAOptionHandler for IVFDemuxer<'a> {
78 fn get_supported_options(&self) -> &[NAOptionDefinition] { &[] }
79 fn set_options(&mut self, _options: &[NAOption]) { }
80 fn query_option_value(&self, _name: &str) -> Option<NAValue> { None }
83 pub struct IVFDemuxerCreator { }
85 impl DemuxerCreator for IVFDemuxerCreator {
86 fn new_demuxer<'a>(&self, br: &'a mut ByteReader<'a>) -> Box<dyn DemuxCore<'a> + 'a> {
87 Box::new(IVFDemuxer::new(br))
89 fn get_name(&self) -> &'static str { "dkivf" }
99 // sample is from the official VP8 test bitstream set
100 let mut file = File::open("assets/Duck/VP8/vp80-00-comprehensive-001.ivf").unwrap();
101 let mut fr = FileReader::new_read(&mut file);
102 let mut br = ByteReader::new(&mut fr);
103 let mut dmx = IVFDemuxer::new(&mut br);
104 let mut sm = StreamManager::new();
105 let mut si = SeekIndex::new();
106 dmx.open(&mut sm, &mut si).unwrap();
109 let pktres = dmx.get_frame(&mut sm);
110 if let Err(e) = pktres {
111 if e == DemuxerError::EOF { break; }
114 let pkt = pktres.unwrap();
115 println!("Got {}", pkt);