]>
Commit | Line | Data |
---|---|---|
22cb00db | 1 | use std::cmp::max; |
5869fd63 | 2 | use std::collections::HashMap; |
83e603fa | 3 | use std::fmt; |
8869d452 | 4 | use std::rc::Rc; |
88c03b61 | 5 | use std::cell::*; |
fba6f8e4 | 6 | use formats::*; |
94dbb551 | 7 | |
5869fd63 | 8 | #[allow(dead_code)] |
66116504 | 9 | #[derive(Clone,Copy,PartialEq)] |
5869fd63 KS |
10 | pub struct NAAudioInfo { |
11 | sample_rate: u32, | |
12 | channels: u8, | |
13 | format: NASoniton, | |
14 | block_len: usize, | |
15 | } | |
16 | ||
17 | impl NAAudioInfo { | |
18 | pub fn new(sr: u32, ch: u8, fmt: NASoniton, bl: usize) -> Self { | |
19 | NAAudioInfo { sample_rate: sr, channels: ch, format: fmt, block_len: bl } | |
20 | } | |
66116504 KS |
21 | pub fn get_sample_rate(&self) -> u32 { self.sample_rate } |
22 | pub fn get_channels(&self) -> u8 { self.channels } | |
23 | pub fn get_format(&self) -> NASoniton { self.format } | |
24 | pub fn get_block_len(&self) -> usize { self.block_len } | |
5869fd63 KS |
25 | } |
26 | ||
83e603fa KS |
27 | impl fmt::Display for NAAudioInfo { |
28 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | |
29 | write!(f, "{} Hz, {} ch", self.sample_rate, self.channels) | |
30 | } | |
31 | } | |
32 | ||
5869fd63 | 33 | #[allow(dead_code)] |
66116504 | 34 | #[derive(Clone,Copy,PartialEq)] |
5869fd63 | 35 | pub struct NAVideoInfo { |
66116504 KS |
36 | width: usize, |
37 | height: usize, | |
5869fd63 KS |
38 | flipped: bool, |
39 | format: NAPixelFormaton, | |
40 | } | |
41 | ||
42 | impl NAVideoInfo { | |
66116504 | 43 | pub fn new(w: usize, h: usize, flip: bool, fmt: NAPixelFormaton) -> Self { |
5869fd63 KS |
44 | NAVideoInfo { width: w, height: h, flipped: flip, format: fmt } |
45 | } | |
66116504 KS |
46 | pub fn get_width(&self) -> usize { self.width as usize } |
47 | pub fn get_height(&self) -> usize { self.height as usize } | |
48 | pub fn is_flipped(&self) -> bool { self.flipped } | |
49 | pub fn get_format(&self) -> NAPixelFormaton { self.format } | |
5869fd63 KS |
50 | } |
51 | ||
83e603fa KS |
52 | impl fmt::Display for NAVideoInfo { |
53 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | |
54 | write!(f, "{}x{}", self.width, self.height) | |
55 | } | |
56 | } | |
57 | ||
66116504 | 58 | #[derive(Clone,Copy,PartialEq)] |
5869fd63 KS |
59 | pub enum NACodecTypeInfo { |
60 | None, | |
61 | Audio(NAAudioInfo), | |
62 | Video(NAVideoInfo), | |
63 | } | |
64 | ||
22cb00db KS |
65 | impl NACodecTypeInfo { |
66 | pub fn get_video_info(&self) -> Option<NAVideoInfo> { | |
67 | match *self { | |
68 | NACodecTypeInfo::Video(vinfo) => Some(vinfo), | |
69 | _ => None, | |
70 | } | |
71 | } | |
72 | pub fn get_audio_info(&self) -> Option<NAAudioInfo> { | |
73 | match *self { | |
74 | NACodecTypeInfo::Audio(ainfo) => Some(ainfo), | |
75 | _ => None, | |
76 | } | |
77 | } | |
78 | } | |
79 | ||
83e603fa KS |
80 | impl fmt::Display for NACodecTypeInfo { |
81 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | |
82 | let ret = match *self { | |
83 | NACodecTypeInfo::None => format!(""), | |
84 | NACodecTypeInfo::Audio(fmt) => format!("{}", fmt), | |
85 | NACodecTypeInfo::Video(fmt) => format!("{}", fmt), | |
86 | }; | |
87 | write!(f, "{}", ret) | |
88 | } | |
89 | } | |
90 | ||
22cb00db | 91 | pub type NABufferRefT<T> = Rc<RefCell<Vec<T>>>; |
88c03b61 | 92 | |
22cb00db KS |
93 | #[derive(Clone)] |
94 | pub struct NAVideoBuffer<T> { | |
6c8e5c40 KS |
95 | info: NAVideoInfo, |
96 | data: NABufferRefT<T>, | |
97 | offs: Vec<usize>, | |
98 | strides: Vec<usize>, | |
22cb00db KS |
99 | } |
100 | ||
101 | impl<T: Clone> NAVideoBuffer<T> { | |
102 | pub fn get_offset(&self, idx: usize) -> usize { | |
103 | if idx >= self.offs.len() { 0 } | |
104 | else { self.offs[idx] } | |
105 | } | |
106 | pub fn get_info(&self) -> NAVideoInfo { self.info } | |
107 | pub fn get_data(&self) -> Ref<Vec<T>> { self.data.borrow() } | |
108 | pub fn get_data_mut(&mut self) -> RefMut<Vec<T>> { self.data.borrow_mut() } | |
109 | pub fn copy_buffer(&mut self) -> Self { | |
110 | let mut data: Vec<T> = Vec::with_capacity(self.data.borrow().len()); | |
111 | data.clone_from(self.data.borrow().as_ref()); | |
112 | let mut offs: Vec<usize> = Vec::with_capacity(self.offs.len()); | |
113 | offs.clone_from(&self.offs); | |
6c8e5c40 KS |
114 | let mut strides: Vec<usize> = Vec::with_capacity(self.strides.len()); |
115 | strides.clone_from(&self.strides); | |
116 | NAVideoBuffer { info: self.info, data: Rc::new(RefCell::new(data)), offs: offs, strides: strides } | |
22cb00db KS |
117 | } |
118 | pub fn get_stride(&self, idx: usize) -> usize { | |
6c8e5c40 KS |
119 | if idx >= self.strides.len() { return 0; } |
120 | self.strides[idx] | |
22cb00db KS |
121 | } |
122 | pub fn get_dimensions(&self, idx: usize) -> (usize, usize) { | |
123 | get_plane_size(&self.info, idx) | |
124 | } | |
125 | } | |
126 | ||
127 | #[derive(Clone)] | |
128 | pub struct NAAudioBuffer<T> { | |
129 | info: NAAudioInfo, | |
130 | data: NABufferRefT<T>, | |
131 | offs: Vec<usize>, | |
132 | chmap: NAChannelMap, | |
133 | } | |
134 | ||
135 | impl<T: Clone> NAAudioBuffer<T> { | |
136 | pub fn get_offset(&self, idx: usize) -> usize { | |
137 | if idx >= self.offs.len() { 0 } | |
138 | else { self.offs[idx] } | |
139 | } | |
140 | pub fn get_info(&self) -> NAAudioInfo { self.info } | |
141 | pub fn get_chmap(&self) -> NAChannelMap { self.chmap.clone() } | |
142 | pub fn get_data(&self) -> Ref<Vec<T>> { self.data.borrow() } | |
143 | pub fn get_data_mut(&mut self) -> RefMut<Vec<T>> { self.data.borrow_mut() } | |
144 | pub fn copy_buffer(&mut self) -> Self { | |
145 | let mut data: Vec<T> = Vec::with_capacity(self.data.borrow().len()); | |
146 | data.clone_from(self.data.borrow().as_ref()); | |
147 | let mut offs: Vec<usize> = Vec::with_capacity(self.offs.len()); | |
148 | offs.clone_from(&self.offs); | |
149 | NAAudioBuffer { info: self.info, data: Rc::new(RefCell::new(data)), offs: offs, chmap: self.get_chmap() } | |
150 | } | |
151 | } | |
152 | ||
87a1ebc3 KS |
153 | impl NAAudioBuffer<u8> { |
154 | pub fn new_from_buf(info: NAAudioInfo, data: NABufferRefT<u8>, chmap: NAChannelMap) -> Self { | |
155 | NAAudioBuffer { info: info, data: data, chmap: chmap, offs: Vec::new() } | |
156 | } | |
157 | } | |
158 | ||
22cb00db KS |
159 | #[derive(Clone)] |
160 | pub enum NABufferType { | |
161 | Video (NAVideoBuffer<u8>), | |
162 | Video16 (NAVideoBuffer<u16>), | |
163 | VideoPacked(NAVideoBuffer<u8>), | |
164 | AudioU8 (NAAudioBuffer<u8>), | |
165 | AudioI16 (NAAudioBuffer<i16>), | |
87a1ebc3 | 166 | AudioI32 (NAAudioBuffer<i32>), |
22cb00db KS |
167 | AudioF32 (NAAudioBuffer<f32>), |
168 | AudioPacked(NAAudioBuffer<u8>), | |
169 | Data (NABufferRefT<u8>), | |
170 | None, | |
171 | } | |
172 | ||
173 | impl NABufferType { | |
174 | pub fn get_offset(&self, idx: usize) -> usize { | |
175 | match *self { | |
176 | NABufferType::Video(ref vb) => vb.get_offset(idx), | |
177 | NABufferType::Video16(ref vb) => vb.get_offset(idx), | |
178 | NABufferType::VideoPacked(ref vb) => vb.get_offset(idx), | |
179 | NABufferType::AudioU8(ref ab) => ab.get_offset(idx), | |
180 | NABufferType::AudioI16(ref ab) => ab.get_offset(idx), | |
181 | NABufferType::AudioF32(ref ab) => ab.get_offset(idx), | |
182 | NABufferType::AudioPacked(ref ab) => ab.get_offset(idx), | |
183 | _ => 0, | |
184 | } | |
185 | } | |
186 | pub fn get_vbuf(&mut self) -> Option<NAVideoBuffer<u8>> { | |
187 | match *self { | |
188 | NABufferType::Video(ref vb) => Some(vb.clone()), | |
87a1ebc3 KS |
189 | NABufferType::VideoPacked(ref vb) => Some(vb.clone()), |
190 | _ => None, | |
191 | } | |
192 | } | |
193 | pub fn get_vbuf16(&mut self) -> Option<NAVideoBuffer<u16>> { | |
194 | match *self { | |
195 | NABufferType::Video16(ref vb) => Some(vb.clone()), | |
196 | _ => None, | |
197 | } | |
198 | } | |
199 | pub fn get_abuf_u8(&mut self) -> Option<NAAudioBuffer<u8>> { | |
200 | match *self { | |
201 | NABufferType::AudioU8(ref ab) => Some(ab.clone()), | |
202 | NABufferType::AudioPacked(ref ab) => Some(ab.clone()), | |
203 | _ => None, | |
204 | } | |
205 | } | |
206 | pub fn get_abuf_i16(&mut self) -> Option<NAAudioBuffer<i16>> { | |
207 | match *self { | |
208 | NABufferType::AudioI16(ref ab) => Some(ab.clone()), | |
209 | _ => None, | |
210 | } | |
211 | } | |
212 | pub fn get_abuf_i32(&mut self) -> Option<NAAudioBuffer<i32>> { | |
213 | match *self { | |
214 | NABufferType::AudioI32(ref ab) => Some(ab.clone()), | |
215 | _ => None, | |
216 | } | |
217 | } | |
218 | pub fn get_abuf_f32(&mut self) -> Option<NAAudioBuffer<f32>> { | |
219 | match *self { | |
220 | NABufferType::AudioF32(ref ab) => Some(ab.clone()), | |
22cb00db KS |
221 | _ => None, |
222 | } | |
223 | } | |
224 | } | |
225 | ||
226 | #[derive(Debug,Clone,Copy,PartialEq)] | |
227 | pub enum AllocatorError { | |
228 | TooLargeDimensions, | |
229 | FormatError, | |
230 | } | |
231 | ||
232 | pub fn alloc_video_buffer(vinfo: NAVideoInfo, align: u8) -> Result<NABufferType, AllocatorError> { | |
233 | let fmt = &vinfo.format; | |
234 | let mut new_size: usize = 0; | |
6c8e5c40 KS |
235 | let mut offs: Vec<usize> = Vec::new(); |
236 | let mut strides: Vec<usize> = Vec::new(); | |
22cb00db KS |
237 | |
238 | for i in 0..fmt.get_num_comp() { | |
239 | if fmt.get_chromaton(i) == None { return Err(AllocatorError::FormatError); } | |
240 | } | |
241 | ||
242 | let align_mod = ((1 << align) as usize) - 1; | |
243 | let width = ((vinfo.width as usize) + align_mod) & !align_mod; | |
244 | let height = ((vinfo.height as usize) + align_mod) & !align_mod; | |
245 | let mut max_depth = 0; | |
246 | let mut all_packed = true; | |
247 | for i in 0..fmt.get_num_comp() { | |
6c8e5c40 KS |
248 | let ochr = fmt.get_chromaton(i); |
249 | if let None = ochr { continue; } | |
250 | let chr = ochr.unwrap(); | |
22cb00db KS |
251 | if !chr.is_packed() { |
252 | all_packed = false; | |
253 | break; | |
254 | } | |
255 | max_depth = max(max_depth, chr.get_depth()); | |
256 | } | |
257 | ||
258 | //todo semi-packed like NV12 | |
bc6aac3d KS |
259 | if fmt.is_paletted() { |
260 | //todo various-sized palettes? | |
6c8e5c40 KS |
261 | let stride = vinfo.get_format().get_chromaton(0).unwrap().get_linesize(width); |
262 | let pic_sz = stride.checked_mul(height); | |
bc6aac3d KS |
263 | if pic_sz == None { return Err(AllocatorError::TooLargeDimensions); } |
264 | let pal_size = 256 * (fmt.get_elem_size() as usize); | |
265 | let new_size = pic_sz.unwrap().checked_add(pal_size); | |
266 | if new_size == None { return Err(AllocatorError::TooLargeDimensions); } | |
267 | offs.push(0); | |
6c8e5c40 KS |
268 | offs.push(stride * height); |
269 | strides.push(stride); | |
bc6aac3d KS |
270 | let mut data: Vec<u8> = Vec::with_capacity(new_size.unwrap()); |
271 | data.resize(new_size.unwrap(), 0); | |
6c8e5c40 | 272 | let buf: NAVideoBuffer<u8> = NAVideoBuffer { data: Rc::new(RefCell::new(data)), info: vinfo, offs: offs, strides: strides }; |
bc6aac3d KS |
273 | Ok(NABufferType::Video(buf)) |
274 | } else if !all_packed { | |
22cb00db | 275 | for i in 0..fmt.get_num_comp() { |
6c8e5c40 KS |
276 | let ochr = fmt.get_chromaton(i); |
277 | if let None = ochr { continue; } | |
278 | let chr = ochr.unwrap(); | |
22cb00db KS |
279 | if !vinfo.is_flipped() { |
280 | offs.push(new_size as usize); | |
281 | } | |
6c8e5c40 | 282 | let stride = chr.get_linesize(width); |
22cb00db | 283 | let cur_h = chr.get_height(height); |
6c8e5c40 | 284 | let cur_sz = stride.checked_mul(cur_h); |
22cb00db KS |
285 | if cur_sz == None { return Err(AllocatorError::TooLargeDimensions); } |
286 | let new_sz = new_size.checked_add(cur_sz.unwrap()); | |
287 | if new_sz == None { return Err(AllocatorError::TooLargeDimensions); } | |
288 | new_size = new_sz.unwrap(); | |
289 | if vinfo.is_flipped() { | |
290 | offs.push(new_size as usize); | |
291 | } | |
6c8e5c40 | 292 | strides.push(stride); |
22cb00db KS |
293 | } |
294 | if max_depth <= 8 { | |
295 | let mut data: Vec<u8> = Vec::with_capacity(new_size); | |
296 | data.resize(new_size, 0); | |
6c8e5c40 | 297 | let buf: NAVideoBuffer<u8> = NAVideoBuffer { data: Rc::new(RefCell::new(data)), info: vinfo, offs: offs, strides: strides }; |
22cb00db KS |
298 | Ok(NABufferType::Video(buf)) |
299 | } else { | |
300 | let mut data: Vec<u16> = Vec::with_capacity(new_size); | |
301 | data.resize(new_size, 0); | |
6c8e5c40 | 302 | let buf: NAVideoBuffer<u16> = NAVideoBuffer { data: Rc::new(RefCell::new(data)), info: vinfo, offs: offs, strides: strides }; |
22cb00db KS |
303 | Ok(NABufferType::Video16(buf)) |
304 | } | |
305 | } else { | |
306 | let elem_sz = fmt.get_elem_size(); | |
307 | let line_sz = width.checked_mul(elem_sz as usize); | |
308 | if line_sz == None { return Err(AllocatorError::TooLargeDimensions); } | |
309 | let new_sz = line_sz.unwrap().checked_mul(height); | |
310 | if new_sz == None { return Err(AllocatorError::TooLargeDimensions); } | |
311 | new_size = new_sz.unwrap(); | |
312 | let mut data: Vec<u8> = Vec::with_capacity(new_size); | |
313 | data.resize(new_size, 0); | |
6c8e5c40 KS |
314 | strides.push(line_sz.unwrap()); |
315 | let buf: NAVideoBuffer<u8> = NAVideoBuffer { data: Rc::new(RefCell::new(data)), info: vinfo, offs: offs, strides: strides }; | |
22cb00db KS |
316 | Ok(NABufferType::VideoPacked(buf)) |
317 | } | |
318 | } | |
319 | ||
320 | pub fn alloc_audio_buffer(ainfo: NAAudioInfo, nsamples: usize, chmap: NAChannelMap) -> Result<NABufferType, AllocatorError> { | |
321 | let mut offs: Vec<usize> = Vec::new(); | |
322 | if ainfo.format.is_planar() { | |
323 | let len = nsamples.checked_mul(ainfo.channels as usize); | |
324 | if len == None { return Err(AllocatorError::TooLargeDimensions); } | |
325 | let length = len.unwrap(); | |
326 | for i in 0..ainfo.channels { | |
327 | offs.push((i as usize) * nsamples); | |
328 | } | |
329 | if ainfo.format.is_float() { | |
330 | if ainfo.format.get_bits() == 32 { | |
331 | let mut data: Vec<f32> = Vec::with_capacity(length); | |
332 | data.resize(length, 0.0); | |
333 | let buf: NAAudioBuffer<f32> = NAAudioBuffer { data: Rc::new(RefCell::new(data)), info: ainfo, offs: offs, chmap: chmap }; | |
334 | Ok(NABufferType::AudioF32(buf)) | |
335 | } else { | |
336 | Err(AllocatorError::TooLargeDimensions) | |
337 | } | |
338 | } else { | |
339 | if ainfo.format.get_bits() == 8 && !ainfo.format.is_signed() { | |
340 | let mut data: Vec<u8> = Vec::with_capacity(length); | |
341 | data.resize(length, 0); | |
342 | let buf: NAAudioBuffer<u8> = NAAudioBuffer { data: Rc::new(RefCell::new(data)), info: ainfo, offs: offs, chmap: chmap }; | |
343 | Ok(NABufferType::AudioU8(buf)) | |
344 | } else if ainfo.format.get_bits() == 16 && ainfo.format.is_signed() { | |
345 | let mut data: Vec<i16> = Vec::with_capacity(length); | |
346 | data.resize(length, 0); | |
347 | let buf: NAAudioBuffer<i16> = NAAudioBuffer { data: Rc::new(RefCell::new(data)), info: ainfo, offs: offs, chmap: chmap }; | |
348 | Ok(NABufferType::AudioI16(buf)) | |
349 | } else { | |
350 | Err(AllocatorError::TooLargeDimensions) | |
351 | } | |
352 | } | |
353 | } else { | |
354 | let len = nsamples.checked_mul(ainfo.channels as usize); | |
355 | if len == None { return Err(AllocatorError::TooLargeDimensions); } | |
356 | let length = ainfo.format.get_audio_size(len.unwrap() as u64); | |
357 | let mut data: Vec<u8> = Vec::with_capacity(length); | |
358 | data.resize(length, 0); | |
359 | let buf: NAAudioBuffer<u8> = NAAudioBuffer { data: Rc::new(RefCell::new(data)), info: ainfo, offs: offs, chmap: chmap }; | |
360 | Ok(NABufferType::AudioPacked(buf)) | |
361 | } | |
362 | } | |
363 | ||
364 | pub fn alloc_data_buffer(size: usize) -> Result<NABufferType, AllocatorError> { | |
365 | let mut data: Vec<u8> = Vec::with_capacity(size); | |
366 | data.resize(size, 0); | |
367 | let buf: NABufferRefT<u8> = Rc::new(RefCell::new(data)); | |
368 | Ok(NABufferType::Data(buf)) | |
369 | } | |
370 | ||
371 | pub fn copy_buffer(buf: NABufferType) -> NABufferType { | |
372 | buf.clone() | |
373 | } | |
374 | ||
5869fd63 | 375 | #[allow(dead_code)] |
8869d452 KS |
376 | #[derive(Clone)] |
377 | pub struct NACodecInfo { | |
ccae5343 | 378 | name: &'static str, |
5869fd63 | 379 | properties: NACodecTypeInfo, |
8869d452 | 380 | extradata: Option<Rc<Vec<u8>>>, |
5869fd63 KS |
381 | } |
382 | ||
8869d452 | 383 | impl NACodecInfo { |
ccae5343 | 384 | pub fn new(name: &'static str, p: NACodecTypeInfo, edata: Option<Vec<u8>>) -> Self { |
8869d452 KS |
385 | let extradata = match edata { |
386 | None => None, | |
387 | Some(vec) => Some(Rc::new(vec)), | |
388 | }; | |
ccae5343 | 389 | NACodecInfo { name: name, properties: p, extradata: extradata } |
8869d452 | 390 | } |
66116504 KS |
391 | pub fn new_ref(name: &'static str, p: NACodecTypeInfo, edata: Option<Rc<Vec<u8>>>) -> Self { |
392 | NACodecInfo { name: name, properties: p, extradata: edata } | |
393 | } | |
8869d452 KS |
394 | pub fn get_properties(&self) -> NACodecTypeInfo { self.properties } |
395 | pub fn get_extradata(&self) -> Option<Rc<Vec<u8>>> { | |
396 | if let Some(ref vec) = self.extradata { return Some(vec.clone()); } | |
397 | None | |
5869fd63 | 398 | } |
66116504 KS |
399 | pub fn get_name(&self) -> &'static str { self.name } |
400 | pub fn is_video(&self) -> bool { | |
401 | if let NACodecTypeInfo::Video(_) = self.properties { return true; } | |
402 | false | |
403 | } | |
404 | pub fn is_audio(&self) -> bool { | |
405 | if let NACodecTypeInfo::Audio(_) = self.properties { return true; } | |
406 | false | |
407 | } | |
408 | } | |
409 | ||
410 | impl fmt::Display for NACodecInfo { | |
411 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | |
412 | let edata = match self.extradata.clone() { | |
413 | None => format!("no extradata"), | |
414 | Some(v) => format!("{} byte(s) of extradata", v.len()), | |
415 | }; | |
416 | write!(f, "{}: {} {}", self.name, self.properties, edata) | |
417 | } | |
418 | } | |
419 | ||
420 | pub const DUMMY_CODEC_INFO: NACodecInfo = NACodecInfo { | |
421 | name: "none", | |
422 | properties: NACodecTypeInfo::None, | |
423 | extradata: None }; | |
424 | ||
66116504 KS |
425 | #[derive(Debug,Clone)] |
426 | pub enum NAValue { | |
5869fd63 KS |
427 | None, |
428 | Int(i32), | |
429 | Long(i64), | |
430 | String(String), | |
66116504 | 431 | Data(Rc<Vec<u8>>), |
5869fd63 KS |
432 | } |
433 | ||
88c03b61 KS |
434 | #[derive(Debug,Clone,Copy,PartialEq)] |
435 | #[allow(dead_code)] | |
436 | pub enum FrameType { | |
437 | I, | |
438 | P, | |
439 | B, | |
bc6aac3d | 440 | Skip, |
88c03b61 KS |
441 | Other, |
442 | } | |
443 | ||
444 | impl fmt::Display for FrameType { | |
445 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | |
446 | match *self { | |
447 | FrameType::I => write!(f, "I"), | |
448 | FrameType::P => write!(f, "P"), | |
449 | FrameType::B => write!(f, "B"), | |
bc6aac3d | 450 | FrameType::Skip => write!(f, "skip"), |
88c03b61 KS |
451 | FrameType::Other => write!(f, "x"), |
452 | } | |
453 | } | |
454 | } | |
455 | ||
e189501e KS |
456 | #[derive(Debug,Clone,Copy)] |
457 | pub struct NATimeInfo { | |
5869fd63 KS |
458 | pts: Option<u64>, |
459 | dts: Option<u64>, | |
460 | duration: Option<u64>, | |
e189501e KS |
461 | tb_num: u32, |
462 | tb_den: u32, | |
463 | } | |
464 | ||
465 | impl NATimeInfo { | |
466 | pub fn new(pts: Option<u64>, dts: Option<u64>, duration: Option<u64>, tb_num: u32, tb_den: u32) -> Self { | |
467 | NATimeInfo { pts: pts, dts: dts, duration: duration, tb_num: tb_num, tb_den: tb_den } | |
468 | } | |
469 | pub fn get_pts(&self) -> Option<u64> { self.pts } | |
470 | pub fn get_dts(&self) -> Option<u64> { self.dts } | |
471 | pub fn get_duration(&self) -> Option<u64> { self.duration } | |
472 | pub fn set_pts(&mut self, pts: Option<u64>) { self.pts = pts; } | |
473 | pub fn set_dts(&mut self, dts: Option<u64>) { self.dts = dts; } | |
474 | pub fn set_duration(&mut self, dur: Option<u64>) { self.duration = dur; } | |
475 | } | |
476 | ||
477 | #[allow(dead_code)] | |
478 | #[derive(Clone)] | |
479 | pub struct NAFrame { | |
480 | ts: NATimeInfo, | |
22cb00db | 481 | buffer: NABufferType, |
66116504 | 482 | info: Rc<NACodecInfo>, |
88c03b61 KS |
483 | ftype: FrameType, |
484 | key: bool, | |
66116504 KS |
485 | options: HashMap<String, NAValue>, |
486 | } | |
487 | ||
ebd71c92 KS |
488 | pub type NAFrameRef = Rc<RefCell<NAFrame>>; |
489 | ||
66116504 KS |
490 | fn get_plane_size(info: &NAVideoInfo, idx: usize) -> (usize, usize) { |
491 | let chromaton = info.get_format().get_chromaton(idx); | |
492 | if let None = chromaton { return (0, 0); } | |
493 | let (hs, vs) = chromaton.unwrap().get_subsampling(); | |
494 | let w = (info.get_width() + ((1 << hs) - 1)) >> hs; | |
495 | let h = (info.get_height() + ((1 << vs) - 1)) >> vs; | |
496 | (w, h) | |
497 | } | |
498 | ||
499 | impl NAFrame { | |
e189501e | 500 | pub fn new(ts: NATimeInfo, |
88c03b61 KS |
501 | ftype: FrameType, |
502 | keyframe: bool, | |
66116504 | 503 | info: Rc<NACodecInfo>, |
22cb00db KS |
504 | options: HashMap<String, NAValue>, |
505 | buffer: NABufferType) -> Self { | |
e189501e | 506 | NAFrame { ts: ts, buffer: buffer, info: info, ftype: ftype, key: keyframe, options: options } |
ebd71c92 | 507 | } |
88c03b61 KS |
508 | pub fn get_frame_type(&self) -> FrameType { self.ftype } |
509 | pub fn is_keyframe(&self) -> bool { self.key } | |
88c03b61 KS |
510 | pub fn set_frame_type(&mut self, ftype: FrameType) { self.ftype = ftype; } |
511 | pub fn set_keyframe(&mut self, key: bool) { self.key = key; } | |
e189501e KS |
512 | pub fn get_time_information(&self) -> NATimeInfo { self.ts } |
513 | pub fn get_pts(&self) -> Option<u64> { self.ts.get_pts() } | |
514 | pub fn get_dts(&self) -> Option<u64> { self.ts.get_dts() } | |
515 | pub fn get_duration(&self) -> Option<u64> { self.ts.get_duration() } | |
516 | pub fn set_pts(&mut self, pts: Option<u64>) { self.ts.set_pts(pts); } | |
517 | pub fn set_dts(&mut self, dts: Option<u64>) { self.ts.set_dts(dts); } | |
518 | pub fn set_duration(&mut self, dur: Option<u64>) { self.ts.set_duration(dur); } | |
66116504 | 519 | |
22cb00db | 520 | pub fn get_buffer(&self) -> NABufferType { self.buffer.clone() } |
5869fd63 KS |
521 | } |
522 | ||
ebd71c92 KS |
523 | impl fmt::Display for NAFrame { |
524 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | |
22cb00db | 525 | let mut foo = format!("frame type {}", self.ftype); |
e189501e KS |
526 | if let Some(pts) = self.ts.pts { foo = format!("{} pts {}", foo, pts); } |
527 | if let Some(dts) = self.ts.dts { foo = format!("{} dts {}", foo, dts); } | |
528 | if let Some(dur) = self.ts.duration { foo = format!("{} duration {}", foo, dur); } | |
ebd71c92 KS |
529 | if self.key { foo = format!("{} kf", foo); } |
530 | write!(f, "[{}]", foo) | |
531 | } | |
532 | } | |
88c03b61 | 533 | |
48c88fde KS |
534 | /// Possible stream types. |
535 | #[derive(Debug,Clone,Copy)] | |
5869fd63 | 536 | #[allow(dead_code)] |
48c88fde KS |
537 | pub enum StreamType { |
538 | /// video stream | |
539 | Video, | |
540 | /// audio stream | |
541 | Audio, | |
542 | /// subtitles | |
543 | Subtitles, | |
544 | /// any data stream (or might be an unrecognized audio/video stream) | |
545 | Data, | |
546 | /// nonexistent stream | |
547 | None, | |
548 | } | |
549 | ||
550 | impl fmt::Display for StreamType { | |
551 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | |
552 | match *self { | |
553 | StreamType::Video => write!(f, "Video"), | |
554 | StreamType::Audio => write!(f, "Audio"), | |
555 | StreamType::Subtitles => write!(f, "Subtitles"), | |
556 | StreamType::Data => write!(f, "Data"), | |
557 | StreamType::None => write!(f, "-"), | |
558 | } | |
559 | } | |
560 | } | |
561 | ||
562 | #[allow(dead_code)] | |
563 | #[derive(Clone)] | |
564 | pub struct NAStream { | |
565 | media_type: StreamType, | |
566 | id: u32, | |
567 | num: usize, | |
568 | info: Rc<NACodecInfo>, | |
e189501e KS |
569 | tb_num: u32, |
570 | tb_den: u32, | |
571 | } | |
572 | ||
573 | pub fn reduce_timebase(tb_num: u32, tb_den: u32) -> (u32, u32) { | |
574 | if tb_num == 0 { return (tb_num, tb_den); } | |
575 | if (tb_den % tb_num) == 0 { return (1, tb_den / tb_num); } | |
576 | ||
577 | let mut a = tb_num; | |
578 | let mut b = tb_den; | |
579 | ||
580 | while a != b { | |
581 | if a > b { a -= b; } | |
582 | else if b > a { b -= a; } | |
583 | } | |
584 | ||
585 | (tb_num / a, tb_den / a) | |
5869fd63 | 586 | } |
48c88fde KS |
587 | |
588 | impl NAStream { | |
e189501e KS |
589 | pub fn new(mt: StreamType, id: u32, info: NACodecInfo, tb_num: u32, tb_den: u32) -> Self { |
590 | let (n, d) = reduce_timebase(tb_num, tb_den); | |
591 | NAStream { media_type: mt, id: id, num: 0, info: Rc::new(info), tb_num: n, tb_den: d } | |
48c88fde KS |
592 | } |
593 | pub fn get_id(&self) -> u32 { self.id } | |
594 | pub fn get_num(&self) -> usize { self.num } | |
595 | pub fn set_num(&mut self, num: usize) { self.num = num; } | |
596 | pub fn get_info(&self) -> Rc<NACodecInfo> { self.info.clone() } | |
e189501e KS |
597 | pub fn get_timebase(&self) -> (u32, u32) { (self.tb_num, self.tb_den) } |
598 | pub fn set_timebase(&mut self, tb_num: u32, tb_den: u32) { | |
599 | let (n, d) = reduce_timebase(tb_num, tb_den); | |
600 | self.tb_num = n; | |
601 | self.tb_den = d; | |
602 | } | |
48c88fde KS |
603 | } |
604 | ||
605 | impl fmt::Display for NAStream { | |
606 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | |
e189501e | 607 | write!(f, "({}#{} @ {}/{} - {})", self.media_type, self.id, self.tb_num, self.tb_den, self.info.get_properties()) |
48c88fde KS |
608 | } |
609 | } | |
610 | ||
611 | #[allow(dead_code)] | |
612 | pub struct NAPacket { | |
613 | stream: Rc<NAStream>, | |
e189501e | 614 | ts: NATimeInfo, |
48c88fde KS |
615 | buffer: Rc<Vec<u8>>, |
616 | keyframe: bool, | |
617 | // options: HashMap<String, NAValue<'a>>, | |
618 | } | |
619 | ||
620 | impl NAPacket { | |
e189501e | 621 | pub fn new(str: Rc<NAStream>, ts: NATimeInfo, kf: bool, vec: Vec<u8>) -> Self { |
48c88fde KS |
622 | // let mut vec: Vec<u8> = Vec::new(); |
623 | // vec.resize(size, 0); | |
e189501e | 624 | NAPacket { stream: str, ts: ts, keyframe: kf, buffer: Rc::new(vec) } |
48c88fde KS |
625 | } |
626 | pub fn get_stream(&self) -> Rc<NAStream> { self.stream.clone() } | |
e189501e KS |
627 | pub fn get_time_information(&self) -> NATimeInfo { self.ts } |
628 | pub fn get_pts(&self) -> Option<u64> { self.ts.get_pts() } | |
629 | pub fn get_dts(&self) -> Option<u64> { self.ts.get_dts() } | |
630 | pub fn get_duration(&self) -> Option<u64> { self.ts.get_duration() } | |
48c88fde KS |
631 | pub fn is_keyframe(&self) -> bool { self.keyframe } |
632 | pub fn get_buffer(&self) -> Rc<Vec<u8>> { self.buffer.clone() } | |
633 | } | |
634 | ||
635 | impl Drop for NAPacket { | |
636 | fn drop(&mut self) {} | |
637 | } | |
638 | ||
639 | impl fmt::Display for NAPacket { | |
640 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | |
641 | let mut foo = format!("[pkt for {} size {}", self.stream, self.buffer.len()); | |
e189501e KS |
642 | if let Some(pts) = self.ts.pts { foo = format!("{} pts {}", foo, pts); } |
643 | if let Some(dts) = self.ts.dts { foo = format!("{} dts {}", foo, dts); } | |
644 | if let Some(dur) = self.ts.duration { foo = format!("{} duration {}", foo, dur); } | |
48c88fde KS |
645 | if self.keyframe { foo = format!("{} kf", foo); } |
646 | foo = foo + "]"; | |
647 | write!(f, "{}", foo) | |
648 | } | |
649 | } | |
650 | ||
651 | pub trait FrameFromPacket { | |
22cb00db | 652 | fn new_from_pkt(pkt: &NAPacket, info: Rc<NACodecInfo>, buf: NABufferType) -> NAFrame; |
48c88fde KS |
653 | fn fill_timestamps(&mut self, pkt: &NAPacket); |
654 | } | |
655 | ||
656 | impl FrameFromPacket for NAFrame { | |
22cb00db | 657 | fn new_from_pkt(pkt: &NAPacket, info: Rc<NACodecInfo>, buf: NABufferType) -> NAFrame { |
e189501e | 658 | NAFrame::new(pkt.ts, FrameType::Other, pkt.keyframe, info, HashMap::new(), buf) |
48c88fde KS |
659 | } |
660 | fn fill_timestamps(&mut self, pkt: &NAPacket) { | |
e189501e | 661 | self.ts = pkt.get_time_information(); |
48c88fde KS |
662 | } |
663 | } | |
664 |