]>
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 | ||
ca41f125 | 44 | #[allow(dead_code)] |
eb71d98f | 45 | struct AVIDemuxer<'a> { |
ca41f125 KS |
46 | src: &'a mut ByteReader<'a>, |
47 | cur_frame: Vec<u64>, | |
48 | num_streams: u8, | |
ca41f125 KS |
49 | size: usize, |
50 | movi_size: usize, | |
33b5a8f0 KS |
51 | movi_pos: u64, |
52 | movi_orig: usize, | |
ca41f125 | 53 | sstate: StreamState, |
e189501e KS |
54 | tb_num: u32, |
55 | tb_den: u32, | |
a480a0de | 56 | strm_duration: u32, |
f39f0623 | 57 | key_offs: Vec<u64>, |
956ef3a0 | 58 | pal: Vec<PalInfo>, |
ca41f125 KS |
59 | } |
60 | ||
c84e0be0 | 61 | #[derive(Debug,Clone,Copy,PartialEq)] |
ca41f125 KS |
62 | enum RIFFTag { |
63 | Chunk(u32), | |
64 | List(u32,u32), | |
65 | } | |
66 | ||
67 | struct RIFFParser { | |
68 | tag: RIFFTag, | |
bcfeae48 | 69 | parse: fn(&mut AVIDemuxer, strmgr: &mut StreamManager, size: usize) -> DemuxerResult<usize>, |
ca41f125 KS |
70 | } |
71 | ||
bcfeae48 | 72 | impl<'a> DemuxCore<'a> for AVIDemuxer<'a> { |
33b5a8f0 | 73 | fn open(&mut self, strmgr: &mut StreamManager, seek_index: &mut SeekIndex) -> DemuxerResult<()> { |
4a6263cf | 74 | self.read_header(strmgr, seek_index)?; |
ca41f125 KS |
75 | Ok(()) |
76 | } | |
77 | ||
bcfeae48 | 78 | fn get_frame(&mut self, strmgr: &mut StreamManager) -> DemuxerResult<NAPacket> { |
ca41f125 KS |
79 | if self.movi_size == 0 { return Err(EOF); } |
80 | let mut tag: [u8; 4] = [0; 4]; | |
81 | loop { | |
aa7dab8e KS |
82 | if (self.src.tell() & 1) == 1 { |
83 | self.src.read_skip(1)?; | |
84 | self.movi_size -= 1; | |
3ff4cebb | 85 | if self.movi_size == 0 { return Err(EOF); } |
aa7dab8e | 86 | } |
f39f0623 | 87 | let is_keyframe = self.key_offs.binary_search(&self.src.tell()).is_ok(); |
ca41f125 KS |
88 | self.src.read_buf(&mut tag)?; |
89 | let size = self.src.read_u32le()? as usize; | |
90 | if mktag!(tag) == mktag!(b"JUNK") { | |
91 | self.movi_size -= size + 8; | |
92 | self.src.read_skip(size)?; | |
cf00c65a | 93 | if self.movi_size == 0 { return Err(EOF); } |
ca41f125 KS |
94 | continue; |
95 | } | |
70fc42e8 KS |
96 | if mktag!(tag) == mktag!(b"LIST") { |
97 | self.movi_size -= 12; | |
98 | self.src.read_skip(4)?; | |
99 | if self.movi_size == 0 { return Err(EOF); } | |
100 | continue; | |
101 | } | |
30d39a7a | 102 | if (tag[0] == b'i' && tag[1] == b'x') || (&tag == b"idx1") { |
6350b1ee KS |
103 | return Err(EOF); |
104 | } | |
ca41f125 KS |
105 | if tag[0] < b'0' || tag[0] > b'9' || tag[1] < b'0' || tag[1] > b'9' { |
106 | return Err(InvalidData); | |
107 | } | |
108 | let stream_no = (tag[0] - b'0') * 10 + (tag[1] - b'0'); | |
956ef3a0 KS |
109 | if tag[2] == b'p' && tag[3] == b'c' { |
110 | self.parse_palette_change(stream_no as usize, size)?; | |
111 | self.movi_size -= size; | |
112 | if self.movi_size == 0 { return Err(EOF); } | |
113 | continue; | |
114 | } | |
bcfeae48 | 115 | let str = strmgr.get_stream(stream_no as usize); |
c83013a1 | 116 | if str.is_none() { return Err(InvalidData); } |
ca41f125 | 117 | let stream = str.unwrap(); |
741d97d8 KS |
118 | if size == 0 { |
119 | self.movi_size -= 8; | |
120 | if self.movi_size == 0 { return Err(EOF); } | |
121 | continue; | |
122 | } | |
e189501e | 123 | let (tb_num, tb_den) = stream.get_timebase(); |
bdab55f6 KS |
124 | let mut ts = NATimeInfo::new(Some(self.cur_frame[stream_no as usize]), None, None, tb_num, tb_den); |
125 | if stream.get_media_type() == StreamType::Audio && tb_num == 1 && stream.get_info().get_name() == "pcm" { | |
126 | ts.pts = None; | |
127 | } | |
956ef3a0 KS |
128 | let mut pkt = self.src.read_packet(stream, ts, is_keyframe, size)?; |
129 | for pe in self.pal.iter_mut() { | |
130 | if pe.stream_no == (stream_no as usize) { | |
131 | pkt.add_side_data(NASideData::Palette(pe.changed, pe.pal.clone())); | |
132 | pe.changed = false; | |
133 | break; | |
134 | } | |
135 | } | |
1a151e53 | 136 | self.cur_frame[stream_no as usize] += 1; |
ca41f125 KS |
137 | self.movi_size -= size + 8; |
138 | ||
139 | return Ok(pkt); | |
140 | } | |
141 | } | |
142 | ||
24d99894 | 143 | fn seek(&mut self, time: NATimePoint, seek_index: &SeekIndex) -> DemuxerResult<()> { |
33b5a8f0 KS |
144 | let ret = seek_index.find_pos(time); |
145 | if ret.is_none() { | |
146 | return Err(DemuxerError::SeekError); | |
147 | } | |
4a6263cf | 148 | let seek_info = ret.unwrap(); |
33b5a8f0 | 149 | |
4a6263cf KS |
150 | if seek_info.pos < self.movi_pos { return Err(DemuxerError::SeekError); } |
151 | let skip_size = (seek_info.pos - self.movi_pos) as usize; | |
33b5a8f0 KS |
152 | if skip_size > self.movi_size { return Err(DemuxerError::SeekError); } |
153 | self.movi_size = self.movi_orig - skip_size; | |
154 | ||
4a6263cf KS |
155 | self.cur_frame[seek_info.str_id as usize] = seek_info.pts; |
156 | self.src.seek(SeekFrom::Start(seek_info.pos))?; | |
157 | ||
33b5a8f0 | 158 | Ok(()) |
ca41f125 | 159 | } |
a480a0de | 160 | fn get_duration(&self) -> u64 { 0 } |
ca41f125 KS |
161 | } |
162 | ||
787b8d03 KS |
163 | impl<'a> NAOptionHandler for AVIDemuxer<'a> { |
164 | fn get_supported_options(&self) -> &[NAOptionDefinition] { &[] } | |
165 | fn set_options(&mut self, _options: &[NAOption]) { } | |
166 | fn query_option_value(&self, _name: &str) -> Option<NAValue> { None } | |
167 | } | |
168 | ||
ca41f125 | 169 | impl<'a> AVIDemuxer<'a> { |
eb71d98f | 170 | fn new(io: &'a mut ByteReader<'a>) -> Self { |
ca41f125 KS |
171 | AVIDemuxer { |
172 | cur_frame: Vec::new(), | |
173 | num_streams: 0, | |
ca41f125 KS |
174 | src: io, |
175 | size: 0, | |
176 | movi_size: 0, | |
33b5a8f0 KS |
177 | movi_pos: 0, |
178 | movi_orig: 0, | |
ca41f125 | 179 | sstate: StreamState::new(), |
e189501e KS |
180 | tb_num: 0, |
181 | tb_den: 0, | |
a480a0de | 182 | strm_duration: 0, |
f39f0623 | 183 | key_offs: Vec::new(), |
956ef3a0 | 184 | pal: Vec::new(), |
ca41f125 KS |
185 | } |
186 | } | |
187 | ||
bcfeae48 | 188 | fn parse_chunk(&mut self, strmgr: &mut StreamManager, end_tag: RIFFTag, csize: usize, depth: u16) -> DemuxerResult<(usize, bool)> { |
ca41f125 KS |
189 | if csize < 8 { return Err(InvalidData); } |
190 | if depth > 42 { return Err(InvalidData); } | |
191 | ||
192 | let tag = self.src.read_u32be()?; | |
193 | let size = self.src.read_u32le()? as usize; | |
194 | if size > csize { return Err(InvalidData); } | |
c84e0be0 KS |
195 | if RIFFTag::Chunk(tag) == end_tag { |
196 | return Ok((size, true)); | |
ca41f125 | 197 | } |
f149a5b7 KS |
198 | let is_list = is_list_tag(tag); |
199 | let ltag = if is_list { self.src.read_u32be()? } else { 0 }; | |
c84e0be0 KS |
200 | if RIFFTag::List(tag, ltag) == end_tag { |
201 | return Ok((size, true)); | |
ca41f125 | 202 | } |
c84e0be0 | 203 | |
c83013a1 KS |
204 | for chunk in CHUNKS.iter() { |
205 | if RIFFTag::Chunk(tag) == chunk.tag { | |
206 | let psize = (chunk.parse)(self, strmgr, size)?; | |
c84e0be0 | 207 | if psize != size { return Err(InvalidData); } |
f6fa6f34 | 208 | if (psize & 1) == 1 { self.src.read_skip(1)?; } |
c84e0be0 | 209 | return Ok((size + 8, false)); |
ca41f125 | 210 | } |
c83013a1 | 211 | if RIFFTag::List(tag, ltag) == chunk.tag { |
c84e0be0 | 212 | let mut rest_size = size - 4; |
c83013a1 | 213 | let psize = (chunk.parse)(self, strmgr, rest_size)?; |
c84e0be0 KS |
214 | if psize > rest_size { return Err(InvalidData); } |
215 | rest_size -= psize; | |
216 | while rest_size > 0 { | |
bcfeae48 | 217 | let (psize, _) = self.parse_chunk(strmgr, end_tag, rest_size, depth+1)?; |
ca41f125 KS |
218 | if psize > rest_size { return Err(InvalidData); } |
219 | rest_size -= psize; | |
c83013a1 KS |
220 | if ((psize & 1) == 1) && (rest_size > 0) { |
221 | rest_size -= 1; | |
f6fa6f34 | 222 | } |
ca41f125 | 223 | } |
c84e0be0 KS |
224 | |
225 | return Ok((size + 8, false)); | |
ca41f125 KS |
226 | } |
227 | } | |
f149a5b7 KS |
228 | if !is_list { |
229 | self.src.read_skip(size)?; | |
230 | } else { | |
231 | if size < 4 { return Err(InvalidData); } | |
232 | self.src.read_skip(size - 4)?; | |
233 | } | |
f6fa6f34 | 234 | if (size & 1) == 1 { self.src.read_skip(1)?; } |
c83013a1 | 235 | Ok((size + 8, false)) |
ca41f125 KS |
236 | } |
237 | ||
4a6263cf | 238 | fn read_header(&mut self, strmgr: &mut StreamManager, seek_idx: &mut SeekIndex) -> DemuxerResult<()> { |
ca41f125 KS |
239 | let riff_tag = self.src.read_u32be()?; |
240 | let size = self.src.read_u32le()? as usize; | |
241 | let avi_tag = self.src.read_u32be()?; | |
23eeced9 KS |
242 | let mut matches = false; |
243 | for rt in RIFF_TAGS.iter() { | |
244 | if rt[0] == riff_tag && rt[1] == avi_tag { | |
245 | matches = true; | |
246 | break; | |
247 | } | |
248 | } | |
249 | if !matches { | |
ca41f125 KS |
250 | return Err(InvalidData); |
251 | } | |
252 | self.size = size; | |
253 | let mut rest_size = size; | |
254 | loop { | |
bcfeae48 | 255 | let (csz, end) = self.parse_chunk(strmgr, RIFFTag::List(mktag!(b"LIST"), mktag!(b"movi")), rest_size,0)?; |
33b5a8f0 KS |
256 | if end { |
257 | self.movi_size = csz - 4; | |
258 | self.movi_orig = self.movi_size; | |
259 | self.movi_pos = self.src.tell(); | |
260 | break; | |
261 | } | |
ca41f125 KS |
262 | rest_size -= csz; |
263 | } | |
9da33f04 KS |
264 | if !seek_idx.skip_index { |
265 | self.src.read_skip(self.movi_size)?; | |
266 | while rest_size > 0 { | |
267 | let ret = self.parse_chunk(strmgr, RIFFTag::Chunk(mktag!(b"idx1")), rest_size,0); | |
268 | if ret.is_err() { break; } | |
269 | let (csz, end) = ret.unwrap(); | |
270 | if end { | |
f39f0623 | 271 | let _res = parse_idx1(&mut self.src, strmgr, seek_idx, csz, self.movi_pos, &mut self.key_offs); |
9da33f04 KS |
272 | break; |
273 | } | |
274 | rest_size -= csz; | |
4a6263cf | 275 | } |
4a6263cf KS |
276 | } |
277 | if self.movi_pos != 0 { | |
278 | self.src.seek(SeekFrom::Start(self.movi_pos))?; | |
279 | } else { | |
280 | return Err(InvalidData); | |
281 | } | |
ca41f125 KS |
282 | if !self.sstate.valid_state() || self.sstate.strm_no != self.num_streams { |
283 | return Err(InvalidData); | |
284 | } | |
285 | Ok(()) | |
286 | } | |
287 | ||
288 | fn read_extradata(&mut self, size: usize) -> DemuxerResult<Option<Vec<u8>>> { | |
289 | if size == 0 { return Ok(None); } | |
c83013a1 | 290 | let mut edvec: Vec<u8> = vec![0; size]; |
ca41f125 KS |
291 | self.src.read_buf(&mut edvec)?; |
292 | Ok(Some(edvec)) | |
293 | } | |
956ef3a0 KS |
294 | |
295 | fn parse_palette_change(&mut self, stream_no: usize, size: usize) -> DemuxerResult<()> { | |
296 | for pe in self.pal.iter_mut() { | |
297 | if pe.stream_no == stream_no { | |
298 | let start_clr = self.src.read_byte()? as usize; | |
299 | let len = self.src.read_byte()? as usize; | |
300 | let _flags = self.src.read_u16le()?; | |
301 | validate!(start_clr + len <= 256); | |
302 | validate!(len * 4 + 4 == size); | |
303 | let mut newpal = *pe.pal; | |
304 | for i in start_clr..(start_clr + len) { | |
305 | newpal[i * 4] = self.src.read_byte()?; | |
306 | newpal[i * 4 + 1] = self.src.read_byte()?; | |
307 | newpal[i * 4 + 2] = self.src.read_byte()?; | |
93c94df9 KS |
308 | newpal[i * 4 + 3] = 0; |
309 | self.src.read_byte()?; // flags | |
956ef3a0 KS |
310 | } |
311 | pe.pal = Arc::new(newpal); | |
312 | pe.changed = true; | |
313 | return Ok(()); | |
314 | } | |
315 | } | |
316 | self.src.read_skip(size)?; | |
317 | Ok(()) | |
318 | } | |
ca41f125 KS |
319 | } |
320 | ||
23eeced9 KS |
321 | const RIFF_TAGS: &[[u32; 2]] = &[ |
322 | [ mktag!(b"RIFF"), mktag!(b"AVI ") ], | |
323 | [ mktag!(b"RIFF"), mktag!(b"AVIX") ], | |
324 | [ mktag!(b"ON2 "), mktag!(b"ON2f") ], | |
325 | ]; | |
326 | ||
327 | const CHUNKS: [RIFFParser; 7] = [ | |
ca41f125 KS |
328 | RIFFParser { tag: RIFFTag::List(mktag!(b"LIST"), mktag!(b"hdrl")), parse: parse_hdrl }, |
329 | RIFFParser { tag: RIFFTag::List(mktag!(b"LIST"), mktag!(b"strl")), parse: parse_strl }, | |
330 | RIFFParser { tag: RIFFTag::Chunk(mktag!(b"avih")), parse: parse_avih }, | |
23eeced9 | 331 | RIFFParser { tag: RIFFTag::Chunk(mktag!(b"ON2h")), parse: parse_avih }, |
ca41f125 KS |
332 | RIFFParser { tag: RIFFTag::Chunk(mktag!(b"strf")), parse: parse_strf }, |
333 | RIFFParser { tag: RIFFTag::Chunk(mktag!(b"strh")), parse: parse_strh }, | |
334 | RIFFParser { tag: RIFFTag::Chunk(mktag!(b"JUNK")), parse: parse_junk }, | |
335 | ]; | |
336 | ||
337 | fn is_list_tag(tag: u32) -> bool { | |
c83013a1 KS |
338 | for chunk in CHUNKS.iter() { |
339 | if let RIFFTag::List(ltag, _) = chunk.tag { | |
ca41f125 KS |
340 | if tag == ltag { |
341 | return true; | |
342 | } | |
343 | } | |
344 | } | |
345 | false | |
346 | } | |
347 | ||
348 | #[allow(unused_variables)] | |
bcfeae48 | 349 | fn parse_hdrl(dmx: &mut AVIDemuxer, strmgr: &mut StreamManager, size: usize) -> DemuxerResult<usize> { |
ca41f125 KS |
350 | Ok(0) |
351 | } | |
352 | ||
353 | #[allow(unused_variables)] | |
bcfeae48 | 354 | fn parse_strl(dmx: &mut AVIDemuxer, strmgr: &mut StreamManager, size: usize) -> DemuxerResult<usize> { |
ca41f125 KS |
355 | Ok(0) |
356 | } | |
357 | ||
358 | #[allow(unused_variables)] | |
bcfeae48 | 359 | fn parse_strh(dmx: &mut AVIDemuxer, strmgr: &mut StreamManager, size: usize) -> DemuxerResult<usize> { |
ca41f125 KS |
360 | if size < 0x38 { return Err(InvalidData); } |
361 | let tag = dmx.src.read_u32be()?; //stream type | |
362 | let fcc = dmx.src.read_u32be()?; //handler(fourcc) | |
363 | dmx.src.read_u32le()?; //flags | |
364 | dmx.src.read_skip(2)?; //priority | |
365 | dmx.src.read_skip(2)?; //language | |
366 | dmx.src.read_skip(4)?; //initial frames | |
e189501e KS |
367 | dmx.tb_num = dmx.src.read_u32le()?; //scale |
368 | dmx.tb_den = dmx.src.read_u32le()?; //rate | |
ca41f125 | 369 | dmx.src.read_skip(4)?; //start |
a480a0de | 370 | dmx.strm_duration = dmx.src.read_u32le()?; |
ca41f125 KS |
371 | dmx.src.read_skip(4)?; //buf size |
372 | dmx.src.read_skip(4)?; //quality | |
373 | dmx.src.read_skip(4)?; //sample size | |
374 | let a = dmx.src.read_u16le()?; | |
375 | let b = dmx.src.read_u16le()?; | |
376 | let c = dmx.src.read_u16le()?; | |
377 | let d = dmx.src.read_u16le()?; | |
378 | ||
379 | dmx.src.read_skip(size - 0x38)?; | |
380 | ||
381 | if !dmx.sstate.valid_state() || dmx.sstate.strm_no >= dmx.num_streams { | |
382 | return Err(InvalidData); | |
383 | } | |
384 | if tag == mktag!(b"vids") { | |
385 | dmx.sstate.strm_type = Some(StreamType::Video); | |
386 | } else if tag == mktag!(b"auds") { | |
387 | dmx.sstate.strm_type = Some(StreamType::Audio); | |
388 | } else { | |
389 | dmx.sstate.strm_type = Some(StreamType::Data); | |
390 | } | |
391 | dmx.sstate.got_strf = false; | |
392 | ||
393 | Ok(size) | |
394 | } | |
395 | ||
bcfeae48 | 396 | fn parse_strf(dmx: &mut AVIDemuxer, strmgr: &mut StreamManager, size: usize) -> DemuxerResult<usize> { |
c83013a1 | 397 | if dmx.sstate.strm_type.is_none() { return Err(InvalidData); } |
ca41f125 | 398 | match dmx.sstate.strm_type.unwrap() { |
bcfeae48 KS |
399 | StreamType::Video => parse_strf_vids(dmx, strmgr, size), |
400 | StreamType::Audio => parse_strf_auds(dmx, strmgr, size), | |
401 | _ => parse_strf_xxxx(dmx, strmgr, size), | |
ca41f125 KS |
402 | } |
403 | } | |
404 | ||
405 | #[allow(unused_variables)] | |
bcfeae48 | 406 | fn parse_strf_vids(dmx: &mut AVIDemuxer, strmgr: &mut StreamManager, size: usize) -> DemuxerResult<usize> { |
ca41f125 KS |
407 | if size < 40 { return Err(InvalidData); } |
408 | let bi_size = dmx.src.read_u32le()?; | |
dbf1b2e1 | 409 | if (bi_size as usize) < 40 { return Err(InvalidData); } |
ca41f125 KS |
410 | let width = dmx.src.read_u32le()?; |
411 | let height = dmx.src.read_u32le()? as i32; | |
412 | let planes = dmx.src.read_u16le()?; | |
413 | let bitcount = dmx.src.read_u16le()?; | |
ccae5343 KS |
414 | let mut compression: [u8; 4] = [0; 4]; |
415 | dmx.src.read_buf(&mut compression)?; | |
ca41f125 KS |
416 | let img_size = dmx.src.read_u32le()?; |
417 | let xdpi = dmx.src.read_u32le()?; | |
418 | let ydpi = dmx.src.read_u32le()?; | |
419 | let colors = dmx.src.read_u32le()?; | |
956ef3a0 | 420 | validate!(colors <= 256); |
ca41f125 KS |
421 | let imp_colors = dmx.src.read_u32le()?; |
422 | ||
423 | let flip = height < 0; | |
424 | let format = if bitcount > 8 { RGB24_FORMAT } else { PAL8_FORMAT }; | |
30940e74 KS |
425 | let mut vhdr = NAVideoInfo::new(width as usize, if flip { -height as usize } else { height as usize}, flip, format); |
426 | vhdr.bits = (planes as u8) * (bitcount as u8); | |
da88eeb8 | 427 | let cname = if find_raw_rgb_fmt(&compression, planes, bitcount, flip, &mut vhdr) { |
9a59c451 | 428 | "rawvideo-ms" |
da88eeb8 KS |
429 | } else if find_raw_yuv_fmt(&compression, &mut vhdr) { |
430 | "rawvideo" | |
9a59c451 KS |
431 | } else { |
432 | match register::find_codec_from_avi_fourcc(&compression) { | |
433 | None => "unknown", | |
434 | Some(name) => name, | |
435 | } | |
436 | }; | |
ca41f125 KS |
437 | let vci = NACodecTypeInfo::Video(vhdr); |
438 | let edata = dmx.read_extradata(size - 40)?; | |
956ef3a0 KS |
439 | if colors > 0 { |
440 | if let Some(ref buf) = edata { | |
441 | let mut pal = [0u8; 1024]; | |
442 | for (dpal, spal) in pal.chunks_mut(4).take(colors as usize).zip(buf.chunks(4)) { | |
93c94df9 | 443 | dpal[0] = spal[2]; |
956ef3a0 | 444 | dpal[1] = spal[1]; |
93c94df9 KS |
445 | dpal[2] = spal[0]; |
446 | dpal[3] = 0; | |
956ef3a0 KS |
447 | } |
448 | let pal = PalInfo { pal: Arc::new(pal), changed: true, stream_no: strmgr.get_num_streams() }; | |
449 | dmx.pal.push(pal); | |
450 | } | |
451 | } | |
ccae5343 | 452 | let vinfo = NACodecInfo::new(cname, vci, edata); |
a480a0de | 453 | 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 | 454 | if res.is_none() { return Err(MemoryError); } |
ca41f125 KS |
455 | dmx.sstate.reset(); |
456 | Ok(size) | |
457 | } | |
458 | ||
da88eeb8 | 459 | fn find_raw_rgb_fmt(compr: &[u8; 4], planes: u16, bitcount: u16, flip: bool, vhdr: &mut NAVideoInfo) -> bool { |
9a59c451 KS |
460 | match compr { |
461 | &[0, 0, 0, 0] | b"DIB " => { | |
462 | if planes != 1 { | |
463 | return false; | |
464 | } | |
465 | let fmt_name = match bitcount { | |
466 | 8 => "pal8", | |
467 | 16 => "bgr555", | |
468 | 24 => "bgr24", | |
469 | 32 => "bgra24", | |
470 | _ => return false, | |
471 | }; | |
472 | if let Ok(fmt) = NAPixelFormaton::from_str(fmt_name) { | |
473 | vhdr.format = fmt; | |
6570edcc | 474 | vhdr.flipped = !flip; |
9a59c451 KS |
475 | true |
476 | } else { | |
477 | false | |
478 | } | |
479 | }, | |
480 | _ => false, | |
481 | } | |
482 | } | |
483 | ||
da88eeb8 KS |
484 | fn find_raw_yuv_fmt(compr: &[u8; 4], vhdr: &mut NAVideoInfo) -> bool { |
485 | let (fmt_name, swapuv) = match compr { | |
486 | b"UYVY" | b"UYNY" | b"UYNV" | b"2vuy" => ("uyvy", false), | |
487 | b"I420" | b"IYUV" => ("yuv420p", false), | |
488 | b"YV12" => ("yuv420p", true), | |
489 | b"YVU9" => ("yuv410p", true), | |
490 | _ => return false, | |
491 | }; | |
492 | if let Ok(fmt) = NAPixelFormaton::from_str(fmt_name) { | |
493 | vhdr.format = fmt; | |
494 | if swapuv { | |
495 | vhdr.format.comp_info.swap(1, 2); | |
496 | } | |
497 | true | |
498 | } else { | |
499 | false | |
500 | } | |
501 | } | |
502 | ||
ca41f125 | 503 | #[allow(unused_variables)] |
bcfeae48 | 504 | fn parse_strf_auds(dmx: &mut AVIDemuxer, strmgr: &mut StreamManager, size: usize) -> DemuxerResult<usize> { |
ca41f125 KS |
505 | if size < 16 { return Err(InvalidData); } |
506 | let w_format_tag = dmx.src.read_u16le()?; | |
507 | let channels = dmx.src.read_u16le()?; | |
508 | let samplespersec = dmx.src.read_u32le()?; | |
509 | let avgbytespersec = dmx.src.read_u32le()?; | |
510 | let block_align = dmx.src.read_u16le()?; | |
b33d38e0 | 511 | let bits_per_sample = dmx.src.read_u16le()?; |
ca41f125 | 512 | |
7769ca01 KS |
513 | let signed = bits_per_sample > 8; |
514 | let soniton = NASoniton::new(bits_per_sample as u8, if signed { SONITON_FLAG_SIGNED } else { 0 }); | |
ca41f125 | 515 | let ahdr = NAAudioInfo::new(samplespersec, channels as u8, soniton, block_align as usize); |
61cab15b KS |
516 | let edata = if size > 16 { |
517 | let edata_size = dmx.src.read_u16le()? as usize; | |
c7a13076 | 518 | validate!(edata_size + 18 <= size); |
61cab15b KS |
519 | dmx.read_extradata(size - 18)? |
520 | } else { | |
521 | None | |
522 | }; | |
ccae5343 KS |
523 | let cname = match register::find_codec_from_wav_twocc(w_format_tag) { |
524 | None => "unknown", | |
525 | Some(name) => name, | |
526 | }; | |
527 | let ainfo = NACodecInfo::new(cname, NACodecTypeInfo::Audio(ahdr), edata); | |
a480a0de | 528 | 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 | 529 | if res.is_none() { return Err(MemoryError); } |
ca41f125 KS |
530 | dmx.sstate.reset(); |
531 | Ok(size) | |
532 | } | |
533 | ||
bcfeae48 | 534 | fn parse_strf_xxxx(dmx: &mut AVIDemuxer, strmgr: &mut StreamManager, size: usize) -> DemuxerResult<usize> { |
ca41f125 | 535 | let edata = dmx.read_extradata(size)?; |
ccae5343 | 536 | let info = NACodecInfo::new("unknown", NACodecTypeInfo::None, edata); |
a480a0de | 537 | 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 | 538 | if res.is_none() { return Err(MemoryError); } |
ca41f125 KS |
539 | dmx.sstate.reset(); |
540 | Ok(size) | |
541 | } | |
542 | ||
543 | #[allow(unused_variables)] | |
bcfeae48 | 544 | fn parse_avih(dmx: &mut AVIDemuxer, strmgr: &mut StreamManager, size: usize) -> DemuxerResult<usize> { |
ca41f125 KS |
545 | if size < 0x38 { return Err(InvalidData); } |
546 | let timebase = dmx.src.read_u32le()?; //microsec per frame | |
547 | dmx.src.read_skip(4)?; //max frame size | |
548 | dmx.src.read_skip(4)?; //padding | |
549 | dmx.src.read_u32le()?; //flags | |
550 | let frames = dmx.src.read_u32le()?; //frames | |
551 | dmx.src.read_skip(4)?; //initial frames | |
552 | let streams = dmx.src.read_u32le()?; //streams | |
553 | if streams > 100 { return Err(InvalidData); } | |
554 | dmx.num_streams = streams as u8; | |
555 | ||
556 | dmx.src.read_skip(4)?; //buf size | |
557 | let width = dmx.src.read_u32le()?; //width | |
558 | let height = dmx.src.read_u32le()? as i32; //height | |
559 | dmx.src.read_skip(16)?; //reserved | |
560 | ||
561 | dmx.cur_frame.resize(streams as usize, 0); | |
562 | dmx.src.read_skip(size - 0x38)?; | |
563 | Ok(size) | |
564 | } | |
565 | ||
bcfeae48 KS |
566 | #[allow(unused_variables)] |
567 | fn parse_junk(dmx: &mut AVIDemuxer, strmgr: &mut StreamManager, size: usize) -> DemuxerResult<usize> { | |
ca41f125 KS |
568 | dmx.src.read_skip(size)?; |
569 | Ok(size) | |
570 | } | |
571 | ||
61cab15b | 572 | #[allow(clippy::verbose_bit_mask)] |
f39f0623 | 573 | 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 |
574 | validate!((size & 15) == 0); |
575 | let mut tag = [0u8; 4]; | |
576 | let num_entries = size >> 4; | |
577 | let mut counter = [0u64; 100]; | |
bae2781b KS |
578 | let mut add_offset = 0; |
579 | let mut set_offset = false; | |
4a6263cf KS |
580 | for _ in 0..num_entries { |
581 | src.read_buf(&mut tag)?; | |
582 | let flags = src.read_u32le()?; | |
bae2781b | 583 | let mut offset = src.read_u32le()? as u64; |
4a6263cf KS |
584 | let _length = src.read_u32le()?; |
585 | ||
bae2781b KS |
586 | if !set_offset && offset < movi_pos { |
587 | add_offset = movi_pos - offset; | |
588 | } | |
589 | set_offset = true; | |
590 | ||
591 | offset += add_offset; | |
592 | ||
4a6263cf KS |
593 | if tag[0] < b'0' || tag[0] > b'9' || tag[1] < b'0' || tag[1] > b'9' { |
594 | return Err(InvalidData); | |
595 | } | |
596 | let stream_no = ((tag[0] - b'0') * 10 + (tag[1] - b'0')) as usize; | |
597 | ||
598 | if (flags & 0x10) != 0 { | |
599 | if let Some(str) = strmgr.get_stream(stream_no) { | |
600 | if str.get_media_type() == StreamType::Video { | |
601 | let (tb_num, tb_den) = str.get_timebase(); | |
602 | let pts = counter[stream_no]; | |
603 | let time = NATimeInfo::ts_to_time(pts, 1000, tb_num, tb_den); | |
5dbd1540 KS |
604 | validate!(offset >= movi_pos); |
605 | seek_idx.add_entry(stream_no as u32, SeekEntry { time, pts, pos: offset }); | |
4a6263cf | 606 | } |
f39f0623 | 607 | key_offs.push(offset); |
4a6263cf KS |
608 | } |
609 | } | |
610 | counter[stream_no] += 1; | |
611 | } | |
f39f0623 | 612 | key_offs.sort_unstable(); |
4a6263cf KS |
613 | Ok(size) |
614 | } | |
615 | ||
eb71d98f KS |
616 | pub struct AVIDemuxerCreator { } |
617 | ||
618 | impl DemuxerCreator for AVIDemuxerCreator { | |
6011e201 | 619 | fn new_demuxer<'a>(&self, br: &'a mut ByteReader<'a>) -> Box<dyn DemuxCore<'a> + 'a> { |
eb71d98f KS |
620 | Box::new(AVIDemuxer::new(br)) |
621 | } | |
622 | fn get_name(&self) -> &'static str { "avi" } | |
623 | } | |
624 | ||
ca41f125 KS |
625 | #[cfg(test)] |
626 | mod test { | |
627 | use super::*; | |
628 | use std::fs::File; | |
629 | ||
630 | #[test] | |
631 | fn test_avi_demux() { | |
1678d59a | 632 | let mut file = File::open("assets/Indeo/laser05.avi").unwrap(); |
ca41f125 KS |
633 | let mut fr = FileReader::new_read(&mut file); |
634 | let mut br = ByteReader::new(&mut fr); | |
635 | let mut dmx = AVIDemuxer::new(&mut br); | |
bcfeae48 | 636 | let mut sm = StreamManager::new(); |
caf0f37e KS |
637 | let mut si = SeekIndex::new(); |
638 | dmx.open(&mut sm, &mut si).unwrap(); | |
ca41f125 KS |
639 | |
640 | loop { | |
bcfeae48 | 641 | let pktres = dmx.get_frame(&mut sm); |
ca41f125 | 642 | if let Err(e) = pktres { |
b1be9318 | 643 | if e == DemuxerError::EOF { break; } |
ca41f125 KS |
644 | panic!("error"); |
645 | } | |
646 | let pkt = pktres.unwrap(); | |
647 | println!("Got {}", pkt); | |
648 | } | |
649 | } | |
650 | } |