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
|
use nihav_core::demuxers::*;
struct IVFDemuxer<'a> {
src: &'a mut ByteReader<'a>,
nframes: u32,
vframe: u32,
aframe: u32,
size: u64,
vframes: Vec<Vec<u8>>,
vsizes: Vec<u32>,
aframes: Vec<Vec<u8>>,
do_v: bool,
passes: u8,
}
impl<'a> IVFDemuxer<'a> {
fn new(src: &'a mut ByteReader<'a>) -> Self {
IVFDemuxer {
src,
nframes: 0,
vframe: 0,
aframe: 0,
size: 0,
vframes: Vec::new(),
aframes: Vec::new(),
vsizes: Vec::new(),
do_v: false,
passes: 0,
}
}
}
const IVF_GUID_0: [u8; 16] = [0x50, 0xEF, 0x81, 0x19, 0xB3, 0xBD, 0xD0, 0x11, 0xA3, 0xE5, 0x00, 0xA0, 0xC9, 0x24, 0x44, 0x36];
const IVF_GUID_1: [u8; 16] = [0x50, 0xEF, 0x81, 0x19, 0xB3, 0xBD, 0xD0, 0x11, 0xA3, 0xE5, 0x00, 0xA0, 0xC9, 0x24, 0x44, 0x37];
impl<'a> DemuxCore<'a> for IVFDemuxer<'a> {
fn open(&mut self, strmgr: &mut StreamManager, _seek_index: &mut SeekIndex) -> DemuxerResult<()> {
let mut guid = [0; 16];
self.src.read_buf(&mut guid)?;
let version = match &guid {
&IVF_GUID_0 => 0,
&IVF_GUID_1 => 1,
_ => return Err(DemuxerError::InvalidData),
};
let flags = self.src.read_u32le()?;
// file header - 0x9C bytes
let aframes = self.src.read_u32le()? as usize;
self.src.read_skip(12)?;
self.size = u64::from(self.src.read_u32le()?);
self.src.read_skip(136)?;
// video stream header - 0x8C bytes
let tag = self.src.read_tag()?;
validate!(&tag == b"vids");
self.src.read_skip(16)?;
let tb_num = self.src.read_u32le()?;
let tb_den = self.src.read_u32le()?;
self.src.read_skip(4)?;
self.nframes = self.src.read_u32le()?;
self.src.read_skip(104)?;
let (atb_num, atb_den, aduration) = if (flags & 1) != 0 {
// audio stream header - 0x8C bytes
let tag = self.src.read_tag()?;
validate!(&tag == b"auds");
self.src.read_skip(16)?;
let tb_num = self.src.read_u32le()?;
let tb_den = self.src.read_u32le()?;
self.src.read_skip(4)?;
let duration = self.src.read_u32le()?;
self.src.read_skip(104)?;
(tb_num, tb_den, duration)
} else { (0, 0, 0) };
let vhdr_size = self.src.read_u32le()? as usize;
validate!(vhdr_size >= 40);
let bmpi_size = self.src.read_u32le()? as usize;
validate!(bmpi_size == vhdr_size);
let width = self.src.read_u32le()? as usize;
let height = self.src.read_u32le()? as i32;
let planes = self.src.read_u16le()?;
let bitcount = self.src.read_u16le()?;
let fcc = self.src.read_tag()?;
self.src.read_skip(20)?;
let mut vhdr = NAVideoInfo::new(width, height.abs() as usize, height < 0, YUV420_FORMAT);
vhdr.bits = (planes as u8) * (bitcount as u8);
let cname = match &fcc {
b"IV31" | b"IV32" => "indeo3",
b"IV41" => "indeo4",
b"IV50" => "indeo5s",
_ => "unknown",
};
let edata = if vhdr_size > 40 {
let mut buf = vec![0; vhdr_size - 40];
self.src.read_buf(&mut buf)?;
Some(buf)
} else {
None
};
let vinfo = NACodecInfo::new(cname, NACodecTypeInfo::Video(vhdr), edata);
let res = strmgr.add_stream(NAStream::new(StreamType::Video, 0, vinfo, tb_num, tb_den, u64::from(self.nframes)));
if res.is_none() { return Err(DemuxerError::MemoryError); }
if (flags & 1) != 0 {
let ahdr_size = self.src.read_u32le()? as usize;
validate!(ahdr_size >= 16);
let w_format_tag = self.src.read_u16le()?;
let channels = self.src.read_u16le()?;
let samplespersec = self.src.read_u32le()?;
let _avgbytespersec = self.src.read_u32le()?;
let block_align = self.src.read_u16le()?;
let bits_per_sample = self.src.read_u16le()?;
let signed = bits_per_sample > 8;
let soniton = NASoniton::new(bits_per_sample as u8, if signed { SONITON_FLAG_SIGNED } else { 0 });
let ahdr = NAAudioInfo::new(samplespersec, channels as u8, soniton, block_align as usize);
let edata = if ahdr_size > 16 {
let edata_size = self.src.read_u16le()? as usize;
validate!(edata_size + 18 == ahdr_size);
if edata_size > 0 {
let mut buf = vec![0; edata_size];
self.src.read_buf(&mut buf)?;
Some(buf)
} else {
None
}
} else {
None
};
let cname = match w_format_tag {
0x401 => "iac",
0x402 => "imc",
_ => "unknown",
};
let ainfo = NACodecInfo::new(cname, NACodecTypeInfo::Audio(ahdr), edata);
let res = strmgr.add_stream(NAStream::new(StreamType::Audio, 1, ainfo, atb_num, atb_den, u64::from(aduration)));
if res.is_none() { return Err(DemuxerError::MemoryError); }
}
// video frame table
self.vsizes.reserve(self.nframes as usize);
for _ in 0..self.nframes {
let size = self.src.read_u32le()?;
self.vsizes.push(size);
}
if version == 1 {
self.src.read_skip(128)?;
}
let comment_len = self.src.read_u32le()? as usize;
self.src.read_skip(comment_len)?;
self.vframe = 0;
self.aframe = 0;
self.vframes = Vec::with_capacity(self.nframes as usize);
self.aframes = Vec::with_capacity(aframes);
for _ in 0..self.nframes {
self.vframes.push(Vec::new());
}
for _ in 0..aframes {
self.aframes.push(Vec::new());
}
let mut last_ts = 1 << 31;
let mut pass = 0;
while self.src.tell() < self.size {
let flg = self.src.read_u32le()?;
let fsize = self.src.read_u32le()? as usize;
let tstamp = (flg >> 1) as usize;
if (flg & 1) != 0 {
if last_ts > tstamp {
pass += 1;
if self.passes != 0 && pass > self.passes {
break;
}
}
last_ts = tstamp;
}
let dst = if (flg & 1) != 0 { &mut self.vframes[tstamp] } else { &mut self.aframes[tstamp] };
let cur_size = dst.len();
dst.resize(cur_size + fsize, 0);
self.src.read_buf(&mut dst[cur_size..])?;
}
// remove provisionary code for drop frames if real data is present
for frm in self.vframes.iter_mut() {
if frm.len() > 2 && frm[0] == 0x9F && frm[1] == 0x00 {
frm.remove(0);
frm.remove(0);
}
}
Ok(())
}
fn get_frame(&mut self, strmgr: &mut StreamManager) -> DemuxerResult<NAPacket> {
let has_next = if self.do_v { self.vframe < self.nframes } else { self.aframe < self.nframes };
if has_next {
let (stream_id, tstamp, buf) = if self.do_v {
self.vframe += 1;
(0, self.vframe - 1, self.vframes[self.vframe as usize - 1].clone())
} else {
self.aframe += 1;
(1, self.aframe - 1, self.aframes[self.aframe as usize - 1].clone())
};
if !self.do_v || (self.aframe as usize) < self.aframes.len() {
self.do_v = !self.do_v;
}
if let Some(stream) = strmgr.get_stream(stream_id) {
let (tb_num, tb_den) = stream.get_timebase();
let ts = NATimeInfo::new(Some(tstamp as u64), None, None, tb_num, tb_den);
return Ok(NAPacket::new_from_refbuf(stream, ts, false, NABufferRef::new(buf)));
} else {
return Err(DemuxerError::InvalidData);
}
}
Err(DemuxerError::EOF)
}
fn seek(&mut self, _time: NATimePoint, _seek_index: &SeekIndex) -> DemuxerResult<()> {
Err(DemuxerError::NotPossible)
}
fn get_duration(&self) -> u64 { 0 }
}
const PASSES: &str = "passes";
const DEMUXER_OPTS: &[NAOptionDefinition] = &[
NAOptionDefinition {
name: PASSES, description: "Number of passes to assemble data (0 = all)",
opt_type: NAOptionDefinitionType::Int(Some(0), Some(128)) },
];
impl<'a> NAOptionHandler for IVFDemuxer<'a> {
fn get_supported_options(&self) -> &[NAOptionDefinition] { DEMUXER_OPTS }
fn set_options(&mut self, options: &[NAOption]) {
for option in options.iter() {
for opt_def in DEMUXER_OPTS.iter() {
if opt_def.check(option).is_ok() {
match option.name {
PASSES => {
if let NAValue::Int(intval) = option.value {
self.passes = intval as u8;
}
},
_ => {},
}
}
}
}
}
fn query_option_value(&self, name: &str) -> Option<NAValue> {
match name {
PASSES => Some(NAValue::Int(i64::from(self.passes))),
_ => None,
}
}
}
pub struct IVFDemuxerCreator { }
impl DemuxerCreator for IVFDemuxerCreator {
fn new_demuxer<'a>(&self, br: &'a mut ByteReader<'a>) -> Box<dyn DemuxCore<'a> + 'a> {
Box::new(IVFDemuxer::new(br))
}
fn get_name(&self) -> &'static str { "ivf" }
}
#[cfg(test)]
mod test {
use super::*;
use std::fs::File;
#[test]
fn test_ivf_demux() {
// sample is a trailer for Heart of Darkness game
let mut file = File::open("assets/Indeo/TRAILERIIE.IVF").unwrap();
let mut fr = FileReader::new_read(&mut file);
let mut br = ByteReader::new(&mut fr);
let mut dmx = IVFDemuxer::new(&mut br);
let mut sm = StreamManager::new();
let mut si = SeekIndex::new();
dmx.open(&mut sm, &mut si).unwrap();
loop {
let pktres = dmx.get_frame(&mut sm);
if let Err(e) = pktres {
if e == DemuxerError::EOF { break; }
panic!("error");
}
let pkt = pktres.unwrap();
println!("Got {}", pkt);
}
}
}
|