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