]>
Commit | Line | Data |
---|---|---|
38953fb5 | 1 | use nihav_core::demuxers::*; |
32f7cbe5 | 2 | use nihav_registry::register; |
38953fb5 | 3 | use nihav_core::demuxers::DemuxerError::*; |
9a59c451 | 4 | use std::str::FromStr; |
ca41f125 KS |
5 | |
6 | macro_rules! mktag { | |
7 | ($a:expr, $b:expr, $c:expr, $d:expr) => ({ | |
8 | (($a as u32) << 24) | (($b as u32) << 16) | (($c as u32) << 8) | ($d as u32) | |
9 | }); | |
10 | ($arr:expr) => ({ | |
11 | (($arr[0] as u32) << 24) | (($arr[1] as u32) << 16) | (($arr[2] as u32) << 8) | ($arr[3] as u32) | |
12 | }); | |
13 | } | |
14 | ||
15 | struct StreamState { | |
16 | strm_no: u8, | |
17 | got_strf: bool, | |
18 | strm_type: Option<StreamType>, | |
19 | } | |
20 | ||
21 | impl StreamState { | |
22 | fn new() -> Self { | |
23 | StreamState { strm_no: 0, got_strf: true, strm_type: None } | |
24 | } | |
25 | fn reset(&mut self) { | |
26 | self.strm_type = None; | |
27 | self.got_strf = true; | |
28 | self.strm_no += 1; | |
29 | } | |
30 | fn valid_state(&self) -> bool { | |
31 | match self.strm_type { | |
32 | None => self.got_strf, | |
33 | _ => false, | |
34 | } | |
35 | } | |
36 | } | |
37 | ||
956ef3a0 KS |
38 | struct PalInfo { |
39 | pal: Arc<[u8; 1024]>, | |
40 | changed: bool, | |
41 | stream_no: usize, | |
42 | } | |
43 | ||
5c390af2 KS |
44 | #[derive(Clone,Copy,Default)] |
45 | struct RIFFSegment { | |
46 | pos: u64, | |
47 | size: usize, | |
48 | movi_pos: u64, | |
49 | movi_size: usize, | |
50 | } | |
51 | ||
52 | impl RIFFSegment { | |
53 | fn contains(&self, pos: u64) -> bool { | |
54 | pos >= self.movi_pos && pos < self.movi_pos + (self.movi_size as u64) | |
55 | } | |
56 | fn get_end(&self) -> u64 { | |
57 | self.pos + (self.size as u64) | |
58 | } | |
59 | } | |
60 | ||
ca41f125 | 61 | #[allow(dead_code)] |
eb71d98f | 62 | struct AVIDemuxer<'a> { |
ca41f125 KS |
63 | src: &'a mut ByteReader<'a>, |
64 | cur_frame: Vec<u64>, | |
65 | num_streams: u8, | |
ca41f125 KS |
66 | size: usize, |
67 | movi_size: usize, | |
33b5a8f0 KS |
68 | movi_pos: u64, |
69 | movi_orig: usize, | |
ca41f125 | 70 | sstate: StreamState, |
e189501e KS |
71 | tb_num: u32, |
72 | tb_den: u32, | |
a480a0de | 73 | strm_duration: u32, |
f39f0623 | 74 | key_offs: Vec<u64>, |
956ef3a0 | 75 | pal: Vec<PalInfo>, |
5c390af2 KS |
76 | odml: bool, |
77 | odml_idx: Vec<u64>, | |
78 | odml_riff: Vec<RIFFSegment>, | |
ca41f125 KS |
79 | } |
80 | ||
c84e0be0 | 81 | #[derive(Debug,Clone,Copy,PartialEq)] |
ca41f125 KS |
82 | enum RIFFTag { |
83 | Chunk(u32), | |
84 | List(u32,u32), | |
85 | } | |
86 | ||
87 | struct RIFFParser { | |
88 | tag: RIFFTag, | |
bcfeae48 | 89 | parse: fn(&mut AVIDemuxer, strmgr: &mut StreamManager, size: usize) -> DemuxerResult<usize>, |
ca41f125 KS |
90 | } |
91 | ||
bcfeae48 | 92 | impl<'a> DemuxCore<'a> for AVIDemuxer<'a> { |
33b5a8f0 | 93 | fn open(&mut self, strmgr: &mut StreamManager, seek_index: &mut SeekIndex) -> DemuxerResult<()> { |
4a6263cf | 94 | self.read_header(strmgr, seek_index)?; |
ca41f125 KS |
95 | Ok(()) |
96 | } | |
97 | ||
bcfeae48 | 98 | fn get_frame(&mut self, strmgr: &mut StreamManager) -> DemuxerResult<NAPacket> { |
5c390af2 KS |
99 | if self.movi_size == 0 { |
100 | if !self.odml { | |
101 | return Err(EOF); | |
102 | } | |
103 | self.try_next_odml_chunk()?; | |
104 | } | |
ca41f125 KS |
105 | let mut tag: [u8; 4] = [0; 4]; |
106 | loop { | |
aa7dab8e KS |
107 | if (self.src.tell() & 1) == 1 { |
108 | self.src.read_skip(1)?; | |
109 | self.movi_size -= 1; | |
5c390af2 KS |
110 | if self.movi_size == 0 { |
111 | if !self.odml { | |
112 | return Err(EOF); | |
113 | } | |
114 | self.try_next_odml_chunk()?; | |
115 | } | |
aa7dab8e | 116 | } |
f39f0623 | 117 | let is_keyframe = self.key_offs.binary_search(&self.src.tell()).is_ok(); |
ca41f125 KS |
118 | self.src.read_buf(&mut tag)?; |
119 | let size = self.src.read_u32le()? as usize; | |
120 | if mktag!(tag) == mktag!(b"JUNK") { | |
121 | self.movi_size -= size + 8; | |
122 | self.src.read_skip(size)?; | |
5c390af2 KS |
123 | if self.movi_size == 0 { |
124 | if !self.odml { | |
125 | return Err(EOF); | |
126 | } | |
127 | self.try_next_odml_chunk()?; | |
128 | } | |
ca41f125 KS |
129 | continue; |
130 | } | |
70fc42e8 KS |
131 | if mktag!(tag) == mktag!(b"LIST") { |
132 | self.movi_size -= 12; | |
133 | self.src.read_skip(4)?; | |
5c390af2 KS |
134 | if self.movi_size == 0 { |
135 | if !self.odml { | |
136 | return Err(EOF); | |
137 | } | |
138 | self.try_next_odml_chunk()?; | |
139 | } | |
70fc42e8 KS |
140 | continue; |
141 | } | |
30d39a7a | 142 | if (tag[0] == b'i' && tag[1] == b'x') || (&tag == b"idx1") { |
51382a9a | 143 | let idx_pos = self.src.tell() - 8; |
5c390af2 KS |
144 | if !self.odml { |
145 | return Err(EOF); | |
146 | } | |
147 | self.src.read_skip(size)?; | |
51382a9a KS |
148 | if idx_pos > self.movi_pos { |
149 | self.try_next_odml_chunk()?; | |
150 | } else { | |
151 | self.movi_pos = self.src.tell(); | |
152 | } | |
5c390af2 | 153 | continue; |
6350b1ee | 154 | } |
ca41f125 KS |
155 | if tag[0] < b'0' || tag[0] > b'9' || tag[1] < b'0' || tag[1] > b'9' { |
156 | return Err(InvalidData); | |
157 | } | |
158 | let stream_no = (tag[0] - b'0') * 10 + (tag[1] - b'0'); | |
956ef3a0 KS |
159 | if tag[2] == b'p' && tag[3] == b'c' { |
160 | self.parse_palette_change(stream_no as usize, size)?; | |
161 | self.movi_size -= size; | |
5c390af2 KS |
162 | if self.movi_size == 0 { |
163 | if !self.odml { | |
164 | return Err(EOF); | |
165 | } | |
166 | self.try_next_odml_chunk()?; | |
167 | } | |
956ef3a0 KS |
168 | continue; |
169 | } | |
bcfeae48 | 170 | let str = strmgr.get_stream(stream_no as usize); |
c83013a1 | 171 | if str.is_none() { return Err(InvalidData); } |
ca41f125 | 172 | let stream = str.unwrap(); |
741d97d8 KS |
173 | if size == 0 { |
174 | self.movi_size -= 8; | |
5c390af2 KS |
175 | if self.movi_size == 0 { |
176 | if !self.odml { | |
177 | return Err(EOF); | |
178 | } | |
179 | self.try_next_odml_chunk()?; | |
180 | } | |
741d97d8 KS |
181 | continue; |
182 | } | |
e189501e | 183 | let (tb_num, tb_den) = stream.get_timebase(); |
bdab55f6 KS |
184 | let mut ts = NATimeInfo::new(Some(self.cur_frame[stream_no as usize]), None, None, tb_num, tb_den); |
185 | if stream.get_media_type() == StreamType::Audio && tb_num == 1 && stream.get_info().get_name() == "pcm" { | |
186 | ts.pts = None; | |
187 | } | |
956ef3a0 KS |
188 | let mut pkt = self.src.read_packet(stream, ts, is_keyframe, size)?; |
189 | for pe in self.pal.iter_mut() { | |
190 | if pe.stream_no == (stream_no as usize) { | |
191 | pkt.add_side_data(NASideData::Palette(pe.changed, pe.pal.clone())); | |
192 | pe.changed = false; | |
193 | break; | |
194 | } | |
195 | } | |
1a151e53 | 196 | self.cur_frame[stream_no as usize] += 1; |
ca41f125 KS |
197 | self.movi_size -= size + 8; |
198 | ||
199 | return Ok(pkt); | |
200 | } | |
201 | } | |
202 | ||
24d99894 | 203 | fn seek(&mut self, time: NATimePoint, seek_index: &SeekIndex) -> DemuxerResult<()> { |
33b5a8f0 KS |
204 | let ret = seek_index.find_pos(time); |
205 | if ret.is_none() { | |
206 | return Err(DemuxerError::SeekError); | |
207 | } | |
4a6263cf | 208 | let seek_info = ret.unwrap(); |
33b5a8f0 | 209 | |
5c390af2 KS |
210 | if self.odml && ((seek_info.pos < self.movi_pos) || (seek_info.pos > self.movi_pos + (self.movi_orig as u64))) { |
211 | let mut found = false; | |
212 | for riff_seg in self.odml_riff.iter() { | |
213 | if riff_seg.contains(seek_info.pos) { | |
214 | found = true; | |
215 | self.movi_pos = riff_seg.movi_pos; | |
216 | self.movi_orig = riff_seg.movi_size; | |
217 | self.movi_size = riff_seg.movi_size; | |
218 | break; | |
219 | } | |
220 | } | |
221 | if !found { | |
222 | let riff_seg = self.odml_riff.last().unwrap(); | |
223 | self.src.seek(SeekFrom::Start(riff_seg.get_end()))?; | |
224 | loop { | |
225 | let ret = self.try_next_odml_chunk(); | |
226 | if ret.is_err() { | |
227 | return Err(DemuxerError::SeekError); | |
228 | } | |
229 | let riff_seg = self.odml_riff.last().unwrap(); | |
230 | if riff_seg.contains(seek_info.pos) { | |
231 | self.movi_pos = riff_seg.movi_pos; | |
232 | self.movi_orig = riff_seg.movi_size; | |
233 | self.movi_size = riff_seg.movi_size; | |
234 | break; | |
235 | } | |
236 | self.src.seek(SeekFrom::Start(riff_seg.get_end()))?; | |
237 | } | |
238 | } | |
239 | } | |
4a6263cf KS |
240 | if seek_info.pos < self.movi_pos { return Err(DemuxerError::SeekError); } |
241 | let skip_size = (seek_info.pos - self.movi_pos) as usize; | |
33b5a8f0 KS |
242 | if skip_size > self.movi_size { return Err(DemuxerError::SeekError); } |
243 | self.movi_size = self.movi_orig - skip_size; | |
244 | ||
4a6263cf KS |
245 | self.cur_frame[seek_info.str_id as usize] = seek_info.pts; |
246 | self.src.seek(SeekFrom::Start(seek_info.pos))?; | |
247 | ||
33b5a8f0 | 248 | Ok(()) |
ca41f125 | 249 | } |
a480a0de | 250 | fn get_duration(&self) -> u64 { 0 } |
ca41f125 KS |
251 | } |
252 | ||
787b8d03 KS |
253 | impl<'a> NAOptionHandler for AVIDemuxer<'a> { |
254 | fn get_supported_options(&self) -> &[NAOptionDefinition] { &[] } | |
255 | fn set_options(&mut self, _options: &[NAOption]) { } | |
256 | fn query_option_value(&self, _name: &str) -> Option<NAValue> { None } | |
257 | } | |
258 | ||
ca41f125 | 259 | impl<'a> AVIDemuxer<'a> { |
eb71d98f | 260 | fn new(io: &'a mut ByteReader<'a>) -> Self { |
ca41f125 KS |
261 | AVIDemuxer { |
262 | cur_frame: Vec::new(), | |
263 | num_streams: 0, | |
ca41f125 KS |
264 | src: io, |
265 | size: 0, | |
266 | movi_size: 0, | |
33b5a8f0 KS |
267 | movi_pos: 0, |
268 | movi_orig: 0, | |
ca41f125 | 269 | sstate: StreamState::new(), |
e189501e KS |
270 | tb_num: 0, |
271 | tb_den: 0, | |
a480a0de | 272 | strm_duration: 0, |
f39f0623 | 273 | key_offs: Vec::new(), |
956ef3a0 | 274 | pal: Vec::new(), |
5c390af2 KS |
275 | odml: false, |
276 | odml_idx: Vec::new(), | |
277 | odml_riff: Vec::with_capacity(1), | |
ca41f125 KS |
278 | } |
279 | } | |
280 | ||
bcfeae48 | 281 | fn parse_chunk(&mut self, strmgr: &mut StreamManager, end_tag: RIFFTag, csize: usize, depth: u16) -> DemuxerResult<(usize, bool)> { |
ca41f125 KS |
282 | if csize < 8 { return Err(InvalidData); } |
283 | if depth > 42 { return Err(InvalidData); } | |
284 | ||
285 | let tag = self.src.read_u32be()?; | |
286 | let size = self.src.read_u32le()? as usize; | |
287 | if size > csize { return Err(InvalidData); } | |
c84e0be0 KS |
288 | if RIFFTag::Chunk(tag) == end_tag { |
289 | return Ok((size, true)); | |
ca41f125 | 290 | } |
f149a5b7 KS |
291 | let is_list = is_list_tag(tag); |
292 | let ltag = if is_list { self.src.read_u32be()? } else { 0 }; | |
c84e0be0 KS |
293 | if RIFFTag::List(tag, ltag) == end_tag { |
294 | return Ok((size, true)); | |
ca41f125 | 295 | } |
c84e0be0 | 296 | |
c83013a1 KS |
297 | for chunk in CHUNKS.iter() { |
298 | if RIFFTag::Chunk(tag) == chunk.tag { | |
299 | let psize = (chunk.parse)(self, strmgr, size)?; | |
c84e0be0 | 300 | if psize != size { return Err(InvalidData); } |
f6fa6f34 | 301 | if (psize & 1) == 1 { self.src.read_skip(1)?; } |
c84e0be0 | 302 | return Ok((size + 8, false)); |
ca41f125 | 303 | } |
c83013a1 | 304 | if RIFFTag::List(tag, ltag) == chunk.tag { |
c84e0be0 | 305 | let mut rest_size = size - 4; |
c83013a1 | 306 | let psize = (chunk.parse)(self, strmgr, rest_size)?; |
c84e0be0 KS |
307 | if psize > rest_size { return Err(InvalidData); } |
308 | rest_size -= psize; | |
309 | while rest_size > 0 { | |
bcfeae48 | 310 | let (psize, _) = self.parse_chunk(strmgr, end_tag, rest_size, depth+1)?; |
ca41f125 KS |
311 | if psize > rest_size { return Err(InvalidData); } |
312 | rest_size -= psize; | |
c83013a1 KS |
313 | if ((psize & 1) == 1) && (rest_size > 0) { |
314 | rest_size -= 1; | |
f6fa6f34 | 315 | } |
ca41f125 | 316 | } |
c84e0be0 KS |
317 | |
318 | return Ok((size + 8, false)); | |
ca41f125 KS |
319 | } |
320 | } | |
f149a5b7 KS |
321 | if !is_list { |
322 | self.src.read_skip(size)?; | |
323 | } else { | |
324 | if size < 4 { return Err(InvalidData); } | |
325 | self.src.read_skip(size - 4)?; | |
326 | } | |
f6fa6f34 | 327 | if (size & 1) == 1 { self.src.read_skip(1)?; } |
c83013a1 | 328 | Ok((size + 8, false)) |
ca41f125 KS |
329 | } |
330 | ||
4a6263cf | 331 | fn read_header(&mut self, strmgr: &mut StreamManager, seek_idx: &mut SeekIndex) -> DemuxerResult<()> { |
ca41f125 KS |
332 | let riff_tag = self.src.read_u32be()?; |
333 | let size = self.src.read_u32le()? as usize; | |
5c390af2 | 334 | self.odml_riff.push(RIFFSegment { pos: self.src.tell() - 8, size: size + 8, movi_pos: 0, movi_size: 0}); |
ca41f125 | 335 | let avi_tag = self.src.read_u32be()?; |
23eeced9 KS |
336 | let mut matches = false; |
337 | for rt in RIFF_TAGS.iter() { | |
338 | if rt[0] == riff_tag && rt[1] == avi_tag { | |
339 | matches = true; | |
340 | break; | |
341 | } | |
342 | } | |
343 | if !matches { | |
ca41f125 KS |
344 | return Err(InvalidData); |
345 | } | |
346 | self.size = size; | |
347 | let mut rest_size = size; | |
348 | loop { | |
bcfeae48 | 349 | let (csz, end) = self.parse_chunk(strmgr, RIFFTag::List(mktag!(b"LIST"), mktag!(b"movi")), rest_size,0)?; |
33b5a8f0 KS |
350 | if end { |
351 | self.movi_size = csz - 4; | |
352 | self.movi_orig = self.movi_size; | |
353 | self.movi_pos = self.src.tell(); | |
5c390af2 KS |
354 | |
355 | self.odml_riff[0].movi_pos = self.movi_pos; | |
356 | self.odml_riff[0].movi_size = self.movi_size; | |
33b5a8f0 KS |
357 | break; |
358 | } | |
ca41f125 KS |
359 | rest_size -= csz; |
360 | } | |
9da33f04 | 361 | if !seek_idx.skip_index { |
5c390af2 KS |
362 | if !self.odml { |
363 | self.src.read_skip(self.movi_size)?; | |
364 | while rest_size > 0 { | |
365 | let ret = self.parse_chunk(strmgr, RIFFTag::Chunk(mktag!(b"idx1")), rest_size,0); | |
366 | if ret.is_err() { break; } | |
367 | let (csz, end) = ret.unwrap(); | |
368 | if end { | |
6f263099 | 369 | let _res = parse_idx1(self.src, strmgr, seek_idx, csz, self.movi_pos, &mut self.key_offs); |
5c390af2 KS |
370 | break; |
371 | } | |
372 | rest_size -= csz; | |
373 | } | |
374 | } else { | |
375 | let mut start = 0; | |
376 | let mut last_strm_no = 255; | |
377 | for &offset in self.odml_idx.iter() { | |
378 | if self.src.seek(SeekFrom::Start(offset)).is_err() { | |
379 | break; | |
380 | } | |
381 | let ret = self.src.read_tag(); | |
382 | if ret.is_err() { break; } | |
383 | let tag = ret.unwrap(); | |
384 | let ret = self.src.read_u32le(); | |
385 | if ret.is_err() { break; } | |
386 | let size = ret.unwrap() as usize; | |
387 | if &tag[..2] != b"ix" || tag[2] < b'0' || tag[2] > b'9' || tag[3] < b'0' || tag[3] > b'9'{ | |
388 | break; | |
389 | } | |
390 | let stream_no = ((tag[2] - b'0') * 10 + (tag[3] - b'0')) as usize; | |
391 | ||
392 | if last_strm_no != stream_no { | |
393 | start = 0; | |
394 | last_strm_no = stream_no; | |
395 | } | |
6f263099 | 396 | let ret = parse_odml_ix(self.src, strmgr, seek_idx, stream_no, size, start); |
5c390af2 KS |
397 | if let Ok(new_start) = ret { |
398 | start = new_start; | |
399 | } else { | |
400 | break; | |
401 | } | |
9da33f04 | 402 | } |
4a6263cf | 403 | } |
4a6263cf KS |
404 | } |
405 | if self.movi_pos != 0 { | |
406 | self.src.seek(SeekFrom::Start(self.movi_pos))?; | |
407 | } else { | |
408 | return Err(InvalidData); | |
409 | } | |
ca41f125 KS |
410 | if !self.sstate.valid_state() || self.sstate.strm_no != self.num_streams { |
411 | return Err(InvalidData); | |
412 | } | |
413 | Ok(()) | |
414 | } | |
415 | ||
416 | fn read_extradata(&mut self, size: usize) -> DemuxerResult<Option<Vec<u8>>> { | |
417 | if size == 0 { return Ok(None); } | |
c83013a1 | 418 | let mut edvec: Vec<u8> = vec![0; size]; |
ca41f125 KS |
419 | self.src.read_buf(&mut edvec)?; |
420 | Ok(Some(edvec)) | |
421 | } | |
956ef3a0 KS |
422 | |
423 | fn parse_palette_change(&mut self, stream_no: usize, size: usize) -> DemuxerResult<()> { | |
424 | for pe in self.pal.iter_mut() { | |
425 | if pe.stream_no == stream_no { | |
426 | let start_clr = self.src.read_byte()? as usize; | |
427 | let len = self.src.read_byte()? as usize; | |
428 | let _flags = self.src.read_u16le()?; | |
429 | validate!(start_clr + len <= 256); | |
430 | validate!(len * 4 + 4 == size); | |
431 | let mut newpal = *pe.pal; | |
432 | for i in start_clr..(start_clr + len) { | |
433 | newpal[i * 4] = self.src.read_byte()?; | |
434 | newpal[i * 4 + 1] = self.src.read_byte()?; | |
435 | newpal[i * 4 + 2] = self.src.read_byte()?; | |
93c94df9 KS |
436 | newpal[i * 4 + 3] = 0; |
437 | self.src.read_byte()?; // flags | |
956ef3a0 KS |
438 | } |
439 | pe.pal = Arc::new(newpal); | |
440 | pe.changed = true; | |
441 | return Ok(()); | |
442 | } | |
443 | } | |
444 | self.src.read_skip(size)?; | |
445 | Ok(()) | |
446 | } | |
5c390af2 KS |
447 | fn try_next_odml_chunk(&mut self) -> DemuxerResult<()> { |
448 | let last_seg = self.odml_riff.last().unwrap(); | |
449 | if self.src.tell() < last_seg.get_end() { | |
450 | for riff_seg in self.odml_riff.iter() { | |
451 | if riff_seg.pos >= self.src.tell() { | |
452 | self.src.seek(SeekFrom::Start(riff_seg.movi_pos))?; | |
453 | self.movi_pos = riff_seg.movi_pos; | |
454 | self.movi_size = riff_seg.movi_size; | |
455 | return Ok(()); | |
456 | } | |
457 | } | |
458 | } | |
459 | self.src.seek(SeekFrom::Start(last_seg.get_end()))?; | |
460 | ||
461 | let riff_pos = self.src.tell(); | |
462 | let ret = self.src.read_tag(); | |
463 | if let Ok([b'R', b'I', b'F', b'F']) = ret { | |
464 | } else { | |
465 | return Err(DemuxerError::EOF); | |
466 | } | |
467 | let riff_size = self.src.read_u32le()? as usize; | |
468 | let tag = self.src.read_tag()?; | |
469 | validate!(&tag == b"AVIX"); | |
470 | let tag = self.src.read_tag()?; | |
471 | validate!(&tag == b"LIST"); | |
472 | let list_size = self.src.read_u32le()? as usize; | |
473 | validate!(list_size >= 4); | |
474 | let tag = self.src.read_tag()?; | |
475 | validate!(&tag == b"movi"); | |
476 | self.odml_riff.push(RIFFSegment{ pos: riff_pos, size: riff_size + 8, movi_pos: riff_pos + 24, movi_size: list_size - 4}); | |
477 | self.movi_pos = riff_pos + 24; | |
478 | self.movi_size = list_size - 4; | |
479 | self.movi_orig = self.movi_size; | |
480 | ||
481 | Ok(()) | |
482 | } | |
ca41f125 KS |
483 | } |
484 | ||
23eeced9 KS |
485 | const RIFF_TAGS: &[[u32; 2]] = &[ |
486 | [ mktag!(b"RIFF"), mktag!(b"AVI ") ], | |
487 | [ mktag!(b"RIFF"), mktag!(b"AVIX") ], | |
488 | [ mktag!(b"ON2 "), mktag!(b"ON2f") ], | |
489 | ]; | |
490 | ||
5c390af2 | 491 | const CHUNKS: &[RIFFParser] = &[ |
ca41f125 KS |
492 | RIFFParser { tag: RIFFTag::List(mktag!(b"LIST"), mktag!(b"hdrl")), parse: parse_hdrl }, |
493 | RIFFParser { tag: RIFFTag::List(mktag!(b"LIST"), mktag!(b"strl")), parse: parse_strl }, | |
494 | RIFFParser { tag: RIFFTag::Chunk(mktag!(b"avih")), parse: parse_avih }, | |
23eeced9 | 495 | RIFFParser { tag: RIFFTag::Chunk(mktag!(b"ON2h")), parse: parse_avih }, |
ca41f125 KS |
496 | RIFFParser { tag: RIFFTag::Chunk(mktag!(b"strf")), parse: parse_strf }, |
497 | RIFFParser { tag: RIFFTag::Chunk(mktag!(b"strh")), parse: parse_strh }, | |
5c390af2 | 498 | RIFFParser { tag: RIFFTag::Chunk(mktag!(b"indx")), parse: parse_indx }, |
ca41f125 | 499 | RIFFParser { tag: RIFFTag::Chunk(mktag!(b"JUNK")), parse: parse_junk }, |
5c390af2 | 500 | RIFFParser { tag: RIFFTag::List(mktag!(b"LIST"), mktag!(b"odml")), parse: parse_odml }, |
ca41f125 KS |
501 | ]; |
502 | ||
503 | fn is_list_tag(tag: u32) -> bool { | |
c83013a1 KS |
504 | for chunk in CHUNKS.iter() { |
505 | if let RIFFTag::List(ltag, _) = chunk.tag { | |
ca41f125 KS |
506 | if tag == ltag { |
507 | return true; | |
508 | } | |
509 | } | |
510 | } | |
511 | false | |
512 | } | |
513 | ||
514 | #[allow(unused_variables)] | |
bcfeae48 | 515 | fn parse_hdrl(dmx: &mut AVIDemuxer, strmgr: &mut StreamManager, size: usize) -> DemuxerResult<usize> { |
ca41f125 KS |
516 | Ok(0) |
517 | } | |
518 | ||
519 | #[allow(unused_variables)] | |
bcfeae48 | 520 | fn parse_strl(dmx: &mut AVIDemuxer, strmgr: &mut StreamManager, size: usize) -> DemuxerResult<usize> { |
ca41f125 KS |
521 | Ok(0) |
522 | } | |
523 | ||
5c390af2 KS |
524 | fn parse_odml(dmx: &mut AVIDemuxer, _strmgr: &mut StreamManager, _size: usize) -> DemuxerResult<usize> { |
525 | dmx.odml = true; | |
526 | Ok(0) | |
527 | } | |
528 | ||
ca41f125 | 529 | #[allow(unused_variables)] |
bcfeae48 | 530 | fn parse_strh(dmx: &mut AVIDemuxer, strmgr: &mut StreamManager, size: usize) -> DemuxerResult<usize> { |
ca41f125 KS |
531 | if size < 0x38 { return Err(InvalidData); } |
532 | let tag = dmx.src.read_u32be()?; //stream type | |
533 | let fcc = dmx.src.read_u32be()?; //handler(fourcc) | |
534 | dmx.src.read_u32le()?; //flags | |
535 | dmx.src.read_skip(2)?; //priority | |
536 | dmx.src.read_skip(2)?; //language | |
537 | dmx.src.read_skip(4)?; //initial frames | |
e189501e KS |
538 | dmx.tb_num = dmx.src.read_u32le()?; //scale |
539 | dmx.tb_den = dmx.src.read_u32le()?; //rate | |
ca41f125 | 540 | dmx.src.read_skip(4)?; //start |
a480a0de | 541 | dmx.strm_duration = dmx.src.read_u32le()?; |
ca41f125 KS |
542 | dmx.src.read_skip(4)?; //buf size |
543 | dmx.src.read_skip(4)?; //quality | |
544 | dmx.src.read_skip(4)?; //sample size | |
545 | let a = dmx.src.read_u16le()?; | |
546 | let b = dmx.src.read_u16le()?; | |
547 | let c = dmx.src.read_u16le()?; | |
548 | let d = dmx.src.read_u16le()?; | |
549 | ||
550 | dmx.src.read_skip(size - 0x38)?; | |
551 | ||
552 | if !dmx.sstate.valid_state() || dmx.sstate.strm_no >= dmx.num_streams { | |
553 | return Err(InvalidData); | |
554 | } | |
555 | if tag == mktag!(b"vids") { | |
556 | dmx.sstate.strm_type = Some(StreamType::Video); | |
557 | } else if tag == mktag!(b"auds") { | |
558 | dmx.sstate.strm_type = Some(StreamType::Audio); | |
559 | } else { | |
560 | dmx.sstate.strm_type = Some(StreamType::Data); | |
561 | } | |
562 | dmx.sstate.got_strf = false; | |
563 | ||
564 | Ok(size) | |
565 | } | |
566 | ||
bcfeae48 | 567 | fn parse_strf(dmx: &mut AVIDemuxer, strmgr: &mut StreamManager, size: usize) -> DemuxerResult<usize> { |
c83013a1 | 568 | if dmx.sstate.strm_type.is_none() { return Err(InvalidData); } |
ca41f125 | 569 | match dmx.sstate.strm_type.unwrap() { |
bcfeae48 KS |
570 | StreamType::Video => parse_strf_vids(dmx, strmgr, size), |
571 | StreamType::Audio => parse_strf_auds(dmx, strmgr, size), | |
572 | _ => parse_strf_xxxx(dmx, strmgr, size), | |
ca41f125 KS |
573 | } |
574 | } | |
575 | ||
576 | #[allow(unused_variables)] | |
bcfeae48 | 577 | fn parse_strf_vids(dmx: &mut AVIDemuxer, strmgr: &mut StreamManager, size: usize) -> DemuxerResult<usize> { |
ca41f125 KS |
578 | if size < 40 { return Err(InvalidData); } |
579 | let bi_size = dmx.src.read_u32le()?; | |
dbf1b2e1 | 580 | if (bi_size as usize) < 40 { return Err(InvalidData); } |
ca41f125 KS |
581 | let width = dmx.src.read_u32le()?; |
582 | let height = dmx.src.read_u32le()? as i32; | |
583 | let planes = dmx.src.read_u16le()?; | |
584 | let bitcount = dmx.src.read_u16le()?; | |
ccae5343 KS |
585 | let mut compression: [u8; 4] = [0; 4]; |
586 | dmx.src.read_buf(&mut compression)?; | |
ca41f125 KS |
587 | let img_size = dmx.src.read_u32le()?; |
588 | let xdpi = dmx.src.read_u32le()?; | |
589 | let ydpi = dmx.src.read_u32le()?; | |
590 | let colors = dmx.src.read_u32le()?; | |
956ef3a0 | 591 | validate!(colors <= 256); |
ca41f125 KS |
592 | let imp_colors = dmx.src.read_u32le()?; |
593 | ||
594 | let flip = height < 0; | |
595 | let format = if bitcount > 8 { RGB24_FORMAT } else { PAL8_FORMAT }; | |
30940e74 KS |
596 | let mut vhdr = NAVideoInfo::new(width as usize, if flip { -height as usize } else { height as usize}, flip, format); |
597 | vhdr.bits = (planes as u8) * (bitcount as u8); | |
da88eeb8 | 598 | let cname = if find_raw_rgb_fmt(&compression, planes, bitcount, flip, &mut vhdr) { |
9a59c451 | 599 | "rawvideo-ms" |
da88eeb8 KS |
600 | } else if find_raw_yuv_fmt(&compression, &mut vhdr) { |
601 | "rawvideo" | |
9a59c451 KS |
602 | } else { |
603 | match register::find_codec_from_avi_fourcc(&compression) { | |
604 | None => "unknown", | |
605 | Some(name) => name, | |
606 | } | |
607 | }; | |
ca41f125 KS |
608 | let vci = NACodecTypeInfo::Video(vhdr); |
609 | let edata = dmx.read_extradata(size - 40)?; | |
956ef3a0 KS |
610 | if colors > 0 { |
611 | if let Some(ref buf) = edata { | |
612 | let mut pal = [0u8; 1024]; | |
613 | for (dpal, spal) in pal.chunks_mut(4).take(colors as usize).zip(buf.chunks(4)) { | |
93c94df9 | 614 | dpal[0] = spal[2]; |
956ef3a0 | 615 | dpal[1] = spal[1]; |
93c94df9 KS |
616 | dpal[2] = spal[0]; |
617 | dpal[3] = 0; | |
956ef3a0 KS |
618 | } |
619 | let pal = PalInfo { pal: Arc::new(pal), changed: true, stream_no: strmgr.get_num_streams() }; | |
620 | dmx.pal.push(pal); | |
621 | } | |
622 | } | |
ccae5343 | 623 | let vinfo = NACodecInfo::new(cname, vci, edata); |
a480a0de | 624 | let res = strmgr.add_stream(NAStream::new(StreamType::Video, u32::from(dmx.sstate.strm_no), vinfo, dmx.tb_num, dmx.tb_den, u64::from(dmx.strm_duration))); |
c83013a1 | 625 | if res.is_none() { return Err(MemoryError); } |
ca41f125 KS |
626 | dmx.sstate.reset(); |
627 | Ok(size) | |
628 | } | |
629 | ||
da88eeb8 | 630 | fn find_raw_rgb_fmt(compr: &[u8; 4], planes: u16, bitcount: u16, flip: bool, vhdr: &mut NAVideoInfo) -> bool { |
9a59c451 KS |
631 | match compr { |
632 | &[0, 0, 0, 0] | b"DIB " => { | |
633 | if planes != 1 { | |
634 | return false; | |
635 | } | |
636 | let fmt_name = match bitcount { | |
637 | 8 => "pal8", | |
638 | 16 => "bgr555", | |
639 | 24 => "bgr24", | |
640 | 32 => "bgra24", | |
641 | _ => return false, | |
642 | }; | |
643 | if let Ok(fmt) = NAPixelFormaton::from_str(fmt_name) { | |
644 | vhdr.format = fmt; | |
6570edcc | 645 | vhdr.flipped = !flip; |
9a59c451 KS |
646 | true |
647 | } else { | |
648 | false | |
649 | } | |
650 | }, | |
651 | _ => false, | |
652 | } | |
653 | } | |
654 | ||
da88eeb8 KS |
655 | fn find_raw_yuv_fmt(compr: &[u8; 4], vhdr: &mut NAVideoInfo) -> bool { |
656 | let (fmt_name, swapuv) = match compr { | |
657 | b"UYVY" | b"UYNY" | b"UYNV" | b"2vuy" => ("uyvy", false), | |
658 | b"I420" | b"IYUV" => ("yuv420p", false), | |
659 | b"YV12" => ("yuv420p", true), | |
660 | b"YVU9" => ("yuv410p", true), | |
661 | _ => return false, | |
662 | }; | |
663 | if let Ok(fmt) = NAPixelFormaton::from_str(fmt_name) { | |
664 | vhdr.format = fmt; | |
665 | if swapuv { | |
666 | vhdr.format.comp_info.swap(1, 2); | |
667 | } | |
668 | true | |
669 | } else { | |
670 | false | |
671 | } | |
672 | } | |
673 | ||
ca41f125 | 674 | #[allow(unused_variables)] |
bcfeae48 | 675 | fn parse_strf_auds(dmx: &mut AVIDemuxer, strmgr: &mut StreamManager, size: usize) -> DemuxerResult<usize> { |
ca41f125 KS |
676 | if size < 16 { return Err(InvalidData); } |
677 | let w_format_tag = dmx.src.read_u16le()?; | |
678 | let channels = dmx.src.read_u16le()?; | |
679 | let samplespersec = dmx.src.read_u32le()?; | |
680 | let avgbytespersec = dmx.src.read_u32le()?; | |
681 | let block_align = dmx.src.read_u16le()?; | |
b33d38e0 | 682 | let bits_per_sample = dmx.src.read_u16le()?; |
ca41f125 | 683 | |
7769ca01 KS |
684 | let signed = bits_per_sample > 8; |
685 | let soniton = NASoniton::new(bits_per_sample as u8, if signed { SONITON_FLAG_SIGNED } else { 0 }); | |
ca41f125 | 686 | let ahdr = NAAudioInfo::new(samplespersec, channels as u8, soniton, block_align as usize); |
61cab15b KS |
687 | let edata = if size > 16 { |
688 | let edata_size = dmx.src.read_u16le()? as usize; | |
c7a13076 | 689 | validate!(edata_size + 18 <= size); |
61cab15b KS |
690 | dmx.read_extradata(size - 18)? |
691 | } else { | |
692 | None | |
693 | }; | |
ccae5343 KS |
694 | let cname = match register::find_codec_from_wav_twocc(w_format_tag) { |
695 | None => "unknown", | |
696 | Some(name) => name, | |
697 | }; | |
698 | let ainfo = NACodecInfo::new(cname, NACodecTypeInfo::Audio(ahdr), edata); | |
a480a0de | 699 | let res = strmgr.add_stream(NAStream::new(StreamType::Audio, u32::from(dmx.sstate.strm_no), ainfo, dmx.tb_num, dmx.tb_den, u64::from(dmx.strm_duration))); |
c83013a1 | 700 | if res.is_none() { return Err(MemoryError); } |
ca41f125 KS |
701 | dmx.sstate.reset(); |
702 | Ok(size) | |
703 | } | |
704 | ||
bcfeae48 | 705 | fn parse_strf_xxxx(dmx: &mut AVIDemuxer, strmgr: &mut StreamManager, size: usize) -> DemuxerResult<usize> { |
ca41f125 | 706 | let edata = dmx.read_extradata(size)?; |
ccae5343 | 707 | let info = NACodecInfo::new("unknown", NACodecTypeInfo::None, edata); |
a480a0de | 708 | let res = strmgr.add_stream(NAStream::new(StreamType::Data, u32::from(dmx.sstate.strm_no), info, dmx.tb_num, dmx.tb_den, u64::from(dmx.strm_duration))); |
c83013a1 | 709 | if res.is_none() { return Err(MemoryError); } |
ca41f125 KS |
710 | dmx.sstate.reset(); |
711 | Ok(size) | |
712 | } | |
713 | ||
714 | #[allow(unused_variables)] | |
bcfeae48 | 715 | fn parse_avih(dmx: &mut AVIDemuxer, strmgr: &mut StreamManager, size: usize) -> DemuxerResult<usize> { |
ca41f125 KS |
716 | if size < 0x38 { return Err(InvalidData); } |
717 | let timebase = dmx.src.read_u32le()?; //microsec per frame | |
718 | dmx.src.read_skip(4)?; //max frame size | |
719 | dmx.src.read_skip(4)?; //padding | |
720 | dmx.src.read_u32le()?; //flags | |
721 | let frames = dmx.src.read_u32le()?; //frames | |
722 | dmx.src.read_skip(4)?; //initial frames | |
723 | let streams = dmx.src.read_u32le()?; //streams | |
724 | if streams > 100 { return Err(InvalidData); } | |
725 | dmx.num_streams = streams as u8; | |
726 | ||
727 | dmx.src.read_skip(4)?; //buf size | |
728 | let width = dmx.src.read_u32le()?; //width | |
729 | let height = dmx.src.read_u32le()? as i32; //height | |
730 | dmx.src.read_skip(16)?; //reserved | |
731 | ||
732 | dmx.cur_frame.resize(streams as usize, 0); | |
733 | dmx.src.read_skip(size - 0x38)?; | |
734 | Ok(size) | |
735 | } | |
736 | ||
5c390af2 KS |
737 | fn parse_indx(dmx: &mut AVIDemuxer, _strmgr: &mut StreamManager, size: usize) -> DemuxerResult<usize> { |
738 | dmx.odml = true; | |
739 | validate!(size >= 24); | |
740 | let entry_size = dmx.src.read_u16le()? as usize; | |
741 | if entry_size != 4 { | |
742 | dmx.src.read_skip(size - 2)?; | |
743 | return Ok(size); | |
744 | } | |
745 | let sub_type = dmx.src.read_byte()?; | |
746 | let idx_type = dmx.src.read_byte()?; | |
747 | validate!(sub_type == 0 && idx_type == 0); | |
748 | let entries = dmx.src.read_u32le()? as usize; | |
749 | validate!(size >= 24 + entries * 4 * entry_size); | |
750 | dmx.src.read_tag()?; //chunk id | |
751 | dmx.src.read_skip(12)?; // reserved | |
752 | for _ in 0..entries { | |
753 | let offset = dmx.src.read_u64le()?; | |
754 | let _idx_len = dmx.src.read_u32le()?; | |
755 | let _nframes = dmx.src.read_u32le()?; | |
756 | dmx.odml_idx.push(offset); | |
757 | } | |
758 | dmx.src.read_skip(size - 24 - entries * 4 * entry_size)?; | |
759 | Ok(size) | |
760 | } | |
761 | ||
bcfeae48 KS |
762 | #[allow(unused_variables)] |
763 | fn parse_junk(dmx: &mut AVIDemuxer, strmgr: &mut StreamManager, size: usize) -> DemuxerResult<usize> { | |
ca41f125 KS |
764 | dmx.src.read_skip(size)?; |
765 | Ok(size) | |
766 | } | |
767 | ||
61cab15b | 768 | #[allow(clippy::verbose_bit_mask)] |
f39f0623 | 769 | fn parse_idx1(src: &mut ByteReader, strmgr: &mut StreamManager, seek_idx: &mut SeekIndex, size: usize, movi_pos: u64, key_offs: &mut Vec<u64>) -> DemuxerResult<usize> { |
4a6263cf KS |
770 | validate!((size & 15) == 0); |
771 | let mut tag = [0u8; 4]; | |
772 | let num_entries = size >> 4; | |
773 | let mut counter = [0u64; 100]; | |
bae2781b KS |
774 | let mut add_offset = 0; |
775 | let mut set_offset = false; | |
4a6263cf KS |
776 | for _ in 0..num_entries { |
777 | src.read_buf(&mut tag)?; | |
778 | let flags = src.read_u32le()?; | |
bae2781b | 779 | let mut offset = src.read_u32le()? as u64; |
4a6263cf KS |
780 | let _length = src.read_u32le()?; |
781 | ||
bae2781b KS |
782 | if !set_offset && offset < movi_pos { |
783 | add_offset = movi_pos - offset; | |
784 | } | |
785 | set_offset = true; | |
786 | ||
787 | offset += add_offset; | |
788 | ||
4a6263cf | 789 | if tag[0] < b'0' || tag[0] > b'9' || tag[1] < b'0' || tag[1] > b'9' { |
735b56ab | 790 | continue; |
4a6263cf KS |
791 | } |
792 | let stream_no = ((tag[0] - b'0') * 10 + (tag[1] - b'0')) as usize; | |
793 | ||
794 | if (flags & 0x10) != 0 { | |
795 | if let Some(str) = strmgr.get_stream(stream_no) { | |
796 | if str.get_media_type() == StreamType::Video { | |
797 | let (tb_num, tb_den) = str.get_timebase(); | |
798 | let pts = counter[stream_no]; | |
799 | let time = NATimeInfo::ts_to_time(pts, 1000, tb_num, tb_den); | |
5dbd1540 KS |
800 | validate!(offset >= movi_pos); |
801 | seek_idx.add_entry(stream_no as u32, SeekEntry { time, pts, pos: offset }); | |
4a6263cf | 802 | } |
f39f0623 | 803 | key_offs.push(offset); |
4a6263cf KS |
804 | } |
805 | } | |
806 | counter[stream_no] += 1; | |
807 | } | |
f39f0623 | 808 | key_offs.sort_unstable(); |
4a6263cf KS |
809 | Ok(size) |
810 | } | |
811 | ||
5c390af2 KS |
812 | fn parse_odml_ix(src: &mut ByteReader, strmgr: &mut StreamManager, seek_idx: &mut SeekIndex, stream_no: usize, size: usize, start: u64) -> DemuxerResult<u64> { |
813 | validate!(size >= 24); | |
814 | let entry_size = src.read_u16le()? as usize; | |
815 | if entry_size != 2 { | |
816 | return Err(DemuxerError::NotImplemented); | |
817 | } | |
818 | let sub_type = src.read_byte()?; | |
819 | let idx_type = src.read_byte()?; | |
820 | validate!(sub_type == 0 && idx_type == 1); | |
821 | let entries = src.read_u32le()? as usize; | |
822 | validate!(size == 24 + entries * 4 * entry_size); | |
823 | src.read_tag()?; //chunk id | |
824 | let base_offset = src.read_u64le()?; | |
825 | src.read_u32le()?; //reserved | |
826 | if let Some(stream) = strmgr.get_stream(stream_no) { | |
827 | if stream.get_media_type() == StreamType::Video { | |
828 | let (tb_num, tb_den) = stream.get_timebase(); | |
829 | ||
830 | for i in 0..entries { | |
831 | let offset = src.read_u32le()?; | |
832 | validate!(offset >= 8); | |
833 | let _size = src.read_u32le()?; | |
834 | ||
835 | let pts = start + (i as u64); | |
836 | let time = NATimeInfo::ts_to_time(pts, 1000, tb_num, tb_den); | |
837 | seek_idx.add_entry(stream_no as u32, SeekEntry { time, pts, pos: base_offset + u64::from(offset - 8) }); | |
838 | } | |
839 | ||
840 | Ok(start + (entries as u64)) | |
841 | } else { | |
842 | Ok(0) | |
843 | } | |
844 | } else { | |
845 | Ok(0) | |
846 | } | |
847 | } | |
848 | ||
eb71d98f KS |
849 | pub struct AVIDemuxerCreator { } |
850 | ||
851 | impl DemuxerCreator for AVIDemuxerCreator { | |
6011e201 | 852 | fn new_demuxer<'a>(&self, br: &'a mut ByteReader<'a>) -> Box<dyn DemuxCore<'a> + 'a> { |
eb71d98f KS |
853 | Box::new(AVIDemuxer::new(br)) |
854 | } | |
855 | fn get_name(&self) -> &'static str { "avi" } | |
856 | } | |
857 | ||
ca41f125 KS |
858 | #[cfg(test)] |
859 | mod test { | |
860 | use super::*; | |
861 | use std::fs::File; | |
862 | ||
863 | #[test] | |
864 | fn test_avi_demux() { | |
886cde48 | 865 | //test sample: https://samples.mplayerhq.hu/V-codecs/RT21/320x240/laser05.avi |
1678d59a | 866 | let mut file = File::open("assets/Indeo/laser05.avi").unwrap(); |
ca41f125 KS |
867 | let mut fr = FileReader::new_read(&mut file); |
868 | let mut br = ByteReader::new(&mut fr); | |
869 | let mut dmx = AVIDemuxer::new(&mut br); | |
bcfeae48 | 870 | let mut sm = StreamManager::new(); |
caf0f37e KS |
871 | let mut si = SeekIndex::new(); |
872 | dmx.open(&mut sm, &mut si).unwrap(); | |
ca41f125 KS |
873 | |
874 | loop { | |
bcfeae48 | 875 | let pktres = dmx.get_frame(&mut sm); |
ca41f125 | 876 | if let Err(e) = pktres { |
b1be9318 | 877 | if e == DemuxerError::EOF { break; } |
ca41f125 KS |
878 | panic!("error"); |
879 | } | |
880 | let pkt = pktres.unwrap(); | |
881 | println!("Got {}", pkt); | |
882 | } | |
883 | } | |
884 | } |