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
|
use nihav_core::demuxers::*;
struct VivoDemuxer<'a> {
src: &'a mut ByteReader<'a>,
video_id: usize,
audio_id: usize,
video_buf: Vec<u8>,
vpts: u64,
apts: u64,
v_num: u32,
v_den: u32,
a_num: u32,
a_den: u32,
fps: f32,
width: usize,
height: usize,
vname: &'static str,
aname: &'static str,
}
fn read_size(br: &mut ByteReader) -> DemuxerResult<usize> {
let mut ret = 0;
loop {
let c = br.read_byte()?;
ret = (ret << 7) | ((c & 0x7F) as usize);
if (c & 0x80) == 0 {
break;
}
}
Ok(ret)
}
impl<'a> DemuxCore<'a> for VivoDemuxer<'a> {
fn open(&mut self, strmgr: &mut StreamManager, _seek_idx: &mut SeekIndex) -> DemuxerResult<()> {
let mut hdr_data: Vec<u8> = Vec::with_capacity(256);
loop {
let hdr = self.src.peek_byte()?;
if (hdr & 0xF0) != 0 { break; }
self.src.read_skip(1)?;
let hdr_len = read_size(&mut self.src)?;
hdr_data.resize(hdr_len, 0);
self.src.read_buf(&mut hdr_data)?;
self.parse_header_packet(&hdr_data)?;
}
validate!(self.vname != "none");
if self.width == 0 && self.height == 0 {
self.width = 160;
self.height = 120;
}
validate!(self.fps > 0.0 || (self.v_num > 0 && self.v_den > 0));
if self.v_num == 0 {
self.v_num = 1000;
self.v_den = (self.fps * 1000.0) as u32;
}
let vhdr = NAVideoInfo::new(self.width, self.height, false, YUV420_FORMAT);
let vinfo = NACodecInfo::new(self.vname, NACodecTypeInfo::Video(vhdr), None);
let res = strmgr.add_stream(NAStream::new(StreamType::Video, 0, vinfo, self.v_num, self.v_den));
validate!(res.is_some());
self.video_id = res.unwrap();
if self.aname == "none" && self.vname == "vivo1" {
self.aname = "g723.1";
self.a_num = 240;
self.a_den = 8000;
}
if self.aname != "none" {
let ahdr = NAAudioInfo::new(self.a_den, 1, SND_S16_FORMAT, self.a_num as usize);
let ainfo = NACodecInfo::new(self.aname, NACodecTypeInfo::Audio(ahdr), None);
let res = strmgr.add_stream(NAStream::new(StreamType::Audio, 1, ainfo, self.a_num, self.a_den));
validate!(res.is_some());
self.audio_id = res.unwrap();
}
Ok(())
}
fn get_frame(&mut self, strmgr: &mut StreamManager) -> DemuxerResult<NAPacket> {
let br = &mut self.src;
loop {
let ret = br.read_byte();
let hdr1 = match ret {
Err(ByteIOError::EOF) => return Err(DemuxerError::EOF),
Err(error) => return Err(error.into()),
Ok(val) => val,
};
let force_size = hdr1 == 0x82;
let hdr = if force_size { br.read_byte()? } else { hdr1 };
let (is_video, mut size) = match hdr >> 4 {
1 => { (true, 128) },
2 => { validate!(!force_size); (true, read_size(br)?) },
3 => { (false, 40) },
4 => { (false, 24) },
_ => return Err(DemuxerError::InvalidData),
};
if force_size {
size = read_size(br)?;
}
if is_video {
validate!(self.v_den != 0);
let cur_size = self.video_buf.len();
let new_size = cur_size + size;
self.video_buf.resize(new_size, 0);
br.read_buf(&mut self.video_buf[cur_size..])?;
if (hdr >> 4) == 2 {
let mut buf = Vec::new();
std::mem::swap(&mut self.video_buf, &mut buf);
let strres = strmgr.get_stream(self.video_id);
validate!(strres.is_some());
let stream = strres.unwrap();
let ts = NATimeInfo::new(Some(self.vpts), None, None, self.v_num, self.v_den);
let pkt = NAPacket::new(stream, ts, self.vpts == 0, buf);
self.vpts += 1;
return Ok(pkt);
}
} else {
validate!(self.a_den != 0);
let mut buf: Vec<u8> = vec![0; size];
br.read_buf(&mut buf)?;
let strres = strmgr.get_stream(self.audio_id);
validate!(strres.is_some());
let stream = strres.unwrap();
let ts = NATimeInfo::new(Some(self.apts), None, None, self.a_num, self.a_den);
let pkt = NAPacket::new(stream, ts, true, buf);
self.apts += 1;
return Ok(pkt);
}
}
}
fn seek(&mut self, _time: u64, _seek_idx: &SeekIndex) -> DemuxerResult<()> {
Err(DemuxerError::NotPossible)
}
}
impl<'a> NAOptionHandler for VivoDemuxer<'a> {
fn get_supported_options(&self) -> &[NAOptionDefinition] { &[] }
fn set_options(&mut self, _options: &[NAOption]) { }
fn query_option_value(&self, _name: &str) -> Option<NAValue> { None }
}
impl<'a> VivoDemuxer<'a> {
fn new(io: &'a mut ByteReader<'a>) -> Self {
VivoDemuxer {
src: io,
video_id: 0,
audio_id: 0,
video_buf: Vec::new(),
vpts: 0,
apts: 0,
v_num: 0,
v_den: 0,
a_num: 0,
a_den: 0,
width: 0,
height: 0,
fps: 0.0,
vname: "none",
aname: "none",
}
}
fn parse_header_packet(&mut self, pkt: &[u8]) -> DemuxerResult<()> {
for entry in pkt.split(|ch| *ch == 0xD) {
if entry.len() < 3 || !entry.contains(&b':') { continue; }
let entry = if !entry.is_empty() && entry[0] == 0xA { &entry[1..] } else { entry };
let mut split = entry.split(|ch| *ch == b':');
let name = split.next().unwrap();
let value = split.next().unwrap();
let valstr = String::from_utf8_lossy(value);
match name {
b"Version" => {
match value {
b"Vivo/0.90" => { self.vname = "vivo1"; },
b"Vivo/1.00" => { self.vname = "vivo1"; },
b"Vivo/2.00" => { self.vname = "vivo2"; },
_ => {
println!("Unknown codec name {}", valstr);
return Err(DemuxerError::InvalidData);
},
};
},
b"FPS" => {
self.fps = if let Ok(val) = valstr.parse() {
val
} else { return Err(DemuxerError::InvalidData); };
validate!(self.fps > 0.0 && self.fps < 1000.0);
},
b"Width" => {
self.width = if let Ok(val) = valstr.parse() {
val
} else { return Err(DemuxerError::InvalidData); };
validate!(self.width > 0 && self.width <= 1024);
},
b"Height" => {
self.height = if let Ok(val) = valstr.parse() {
val
} else { return Err(DemuxerError::InvalidData); };
validate!(self.height > 0 && self.height <= 768);
},
b"SamplingFrequency" => {
let samp_freq: u32 = if let Ok(val) = valstr.parse() {
val
} else { return Err(DemuxerError::InvalidData); };
validate!(samp_freq == 8000 || samp_freq == 16000);
if samp_freq == 8000 {
self.aname = "g723.1";
self.a_num = 240;
self.a_den = 8000;
} else if samp_freq == 16000 {
self.aname = "siren";
self.a_num = 320;
self.a_den = 16000;
} else {
return Err(DemuxerError::InvalidData);
};
},
/* b"TimeUnitNumerator" => {
self.v_num = if let Ok(val) = valstr.parse() {
val
} else { return Err(DemuxerError::InvalidData); };
validate!(self.v_num > 0);
println!(" video codec tb_num {}", self.v_num);
},
b"TimeUnitDenominator" => {
self.v_den = if let Ok(val) = valstr.parse() {
val
} else { return Err(DemuxerError::InvalidData); };
validate!(self.v_den > 0);
println!(" video codec tb_den {}", self.v_den);
},*/
_ => { },
};
}
Ok(())
}
}
pub struct VivoDemuxerCreator { }
impl DemuxerCreator for VivoDemuxerCreator {
fn new_demuxer<'a>(&self, br: &'a mut ByteReader<'a>) -> Box<dyn DemuxCore<'a> + 'a> {
Box::new(VivoDemuxer::new(br))
}
fn get_name(&self) -> &'static str { "vivo" }
}
#[cfg(test)]
mod test {
use super::*;
use std::fs::File;
#[test]
fn test_vivo_demux() {
// let mut file = File::open("assets/Misc/greetings.viv").unwrap();
let mut file = File::open("assets/Misc/favmovie.viv").unwrap();
let mut fr = FileReader::new_read(&mut file);
let mut br = ByteReader::new(&mut fr);
let mut dmx = VivoDemuxer::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 as i32) == (DemuxerError::EOF as i32) { break; }
panic!("error {:?}", e);
}
let pkt = pktres.unwrap();
println!("Got {}", pkt);
}
}
}
|