]>
Commit | Line | Data |
---|---|---|
1 | use nihav_core::demuxers::*; | |
2 | ||
3 | struct IVFDemuxer<'a> { | |
4 | src: &'a mut ByteReader<'a>, | |
5 | nframes: u32, | |
6 | frameno: u32, | |
7 | } | |
8 | ||
9 | impl<'a> IVFDemuxer<'a> { | |
10 | fn new(src: &'a mut ByteReader<'a>) -> Self { | |
11 | IVFDemuxer { | |
12 | src, | |
13 | nframes: 0, | |
14 | frameno: 0, | |
15 | } | |
16 | } | |
17 | } | |
18 | ||
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()?; | |
24 | validate!(ver == 0); | |
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 { | |
29 | b"VP80" => "vp8", | |
30 | _ => "unknown", | |
31 | }; | |
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()?; | |
38 | self.frameno = 0; | |
39 | ||
40 | self.src.seek(SeekFrom::Start(hdr_len as u64))?; | |
41 | ||
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); | |
46 | } | |
47 | ||
48 | Ok(()) | |
49 | } | |
50 | ||
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()?; | |
55 | self.frameno += 1; | |
56 | ||
57 | if fsize == 0 { | |
58 | continue; | |
59 | } | |
60 | ||
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); | |
64 | } else { | |
65 | return Err(DemuxerError::InvalidData); | |
66 | } | |
67 | } | |
68 | Err(DemuxerError::EOF) | |
69 | } | |
70 | ||
71 | fn seek(&mut self, _time: NATimePoint, _seek_index: &SeekIndex) -> DemuxerResult<()> { | |
72 | Err(DemuxerError::NotPossible) | |
73 | } | |
74 | fn get_duration(&self) -> u64 { 0 } | |
75 | } | |
76 | ||
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 } | |
81 | } | |
82 | ||
83 | pub struct IVFDemuxerCreator { } | |
84 | ||
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)) | |
88 | } | |
89 | fn get_name(&self) -> &'static str { "dkivf" } | |
90 | } | |
91 | ||
92 | #[cfg(test)] | |
93 | mod test { | |
94 | use super::*; | |
95 | use std::fs::File; | |
96 | ||
97 | #[test] | |
98 | fn test_ivf_demux() { | |
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(); | |
107 | ||
108 | loop { | |
109 | let pktres = dmx.get_frame(&mut sm); | |
110 | if let Err(e) = pktres { | |
111 | if e == DemuxerError::EOF { break; } | |
112 | panic!("error"); | |
113 | } | |
114 | let pkt = pktres.unwrap(); | |
115 | println!("Got {}", pkt); | |
116 | } | |
117 | } | |
118 | } |