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 | ||
88c03b61 | 91 | pub type BufferRef = Rc<RefCell<Vec<u8>>>; |
83e603fa | 92 | |
5869fd63 | 93 | #[allow(dead_code)] |
66116504 KS |
94 | #[derive(Clone)] |
95 | pub struct NABuffer { | |
5869fd63 | 96 | id: u64, |
88c03b61 | 97 | data: BufferRef, |
66116504 KS |
98 | } |
99 | ||
100 | impl Drop for NABuffer { | |
101 | fn drop(&mut self) { } | |
102 | } | |
103 | ||
104 | impl NABuffer { | |
88c03b61 KS |
105 | pub fn get_data(&self) -> Ref<Vec<u8>> { self.data.borrow() } |
106 | pub fn get_data_mut(&mut self) -> RefMut<Vec<u8>> { self.data.borrow_mut() } | |
5869fd63 KS |
107 | } |
108 | ||
22cb00db | 109 | pub type NABufferRefT<T> = Rc<RefCell<Vec<T>>>; |
88c03b61 KS |
110 | pub type NABufferRef = Rc<RefCell<NABuffer>>; |
111 | ||
22cb00db KS |
112 | #[derive(Clone)] |
113 | pub struct NAVideoBuffer<T> { | |
114 | info: NAVideoInfo, | |
115 | data: NABufferRefT<T>, | |
116 | offs: Vec<usize>, | |
117 | } | |
118 | ||
119 | impl<T: Clone> NAVideoBuffer<T> { | |
120 | pub fn get_offset(&self, idx: usize) -> usize { | |
121 | if idx >= self.offs.len() { 0 } | |
122 | else { self.offs[idx] } | |
123 | } | |
124 | pub fn get_info(&self) -> NAVideoInfo { self.info } | |
125 | pub fn get_data(&self) -> Ref<Vec<T>> { self.data.borrow() } | |
126 | pub fn get_data_mut(&mut self) -> RefMut<Vec<T>> { self.data.borrow_mut() } | |
127 | pub fn copy_buffer(&mut self) -> Self { | |
128 | let mut data: Vec<T> = Vec::with_capacity(self.data.borrow().len()); | |
129 | data.clone_from(self.data.borrow().as_ref()); | |
130 | let mut offs: Vec<usize> = Vec::with_capacity(self.offs.len()); | |
131 | offs.clone_from(&self.offs); | |
132 | NAVideoBuffer { info: self.info, data: Rc::new(RefCell::new(data)), offs: offs } | |
133 | } | |
134 | pub fn get_stride(&self, idx: usize) -> usize { | |
135 | if idx >= self.info.get_format().get_num_comp() { return 0; } | |
136 | self.info.get_format().get_chromaton(idx).unwrap().get_linesize(self.info.get_width()) | |
137 | } | |
138 | pub fn get_dimensions(&self, idx: usize) -> (usize, usize) { | |
139 | get_plane_size(&self.info, idx) | |
140 | } | |
141 | } | |
142 | ||
143 | #[derive(Clone)] | |
144 | pub struct NAAudioBuffer<T> { | |
145 | info: NAAudioInfo, | |
146 | data: NABufferRefT<T>, | |
147 | offs: Vec<usize>, | |
148 | chmap: NAChannelMap, | |
149 | } | |
150 | ||
151 | impl<T: Clone> NAAudioBuffer<T> { | |
152 | pub fn get_offset(&self, idx: usize) -> usize { | |
153 | if idx >= self.offs.len() { 0 } | |
154 | else { self.offs[idx] } | |
155 | } | |
156 | pub fn get_info(&self) -> NAAudioInfo { self.info } | |
157 | pub fn get_chmap(&self) -> NAChannelMap { self.chmap.clone() } | |
158 | pub fn get_data(&self) -> Ref<Vec<T>> { self.data.borrow() } | |
159 | pub fn get_data_mut(&mut self) -> RefMut<Vec<T>> { self.data.borrow_mut() } | |
160 | pub fn copy_buffer(&mut self) -> Self { | |
161 | let mut data: Vec<T> = Vec::with_capacity(self.data.borrow().len()); | |
162 | data.clone_from(self.data.borrow().as_ref()); | |
163 | let mut offs: Vec<usize> = Vec::with_capacity(self.offs.len()); | |
164 | offs.clone_from(&self.offs); | |
165 | NAAudioBuffer { info: self.info, data: Rc::new(RefCell::new(data)), offs: offs, chmap: self.get_chmap() } | |
166 | } | |
167 | } | |
168 | ||
169 | #[derive(Clone)] | |
170 | pub enum NABufferType { | |
171 | Video (NAVideoBuffer<u8>), | |
172 | Video16 (NAVideoBuffer<u16>), | |
173 | VideoPacked(NAVideoBuffer<u8>), | |
174 | AudioU8 (NAAudioBuffer<u8>), | |
175 | AudioI16 (NAAudioBuffer<i16>), | |
176 | AudioF32 (NAAudioBuffer<f32>), | |
177 | AudioPacked(NAAudioBuffer<u8>), | |
178 | Data (NABufferRefT<u8>), | |
179 | None, | |
180 | } | |
181 | ||
182 | impl NABufferType { | |
183 | pub fn get_offset(&self, idx: usize) -> usize { | |
184 | match *self { | |
185 | NABufferType::Video(ref vb) => vb.get_offset(idx), | |
186 | NABufferType::Video16(ref vb) => vb.get_offset(idx), | |
187 | NABufferType::VideoPacked(ref vb) => vb.get_offset(idx), | |
188 | NABufferType::AudioU8(ref ab) => ab.get_offset(idx), | |
189 | NABufferType::AudioI16(ref ab) => ab.get_offset(idx), | |
190 | NABufferType::AudioF32(ref ab) => ab.get_offset(idx), | |
191 | NABufferType::AudioPacked(ref ab) => ab.get_offset(idx), | |
192 | _ => 0, | |
193 | } | |
194 | } | |
195 | pub fn get_vbuf(&mut self) -> Option<NAVideoBuffer<u8>> { | |
196 | match *self { | |
197 | NABufferType::Video(ref vb) => Some(vb.clone()), | |
198 | _ => None, | |
199 | } | |
200 | } | |
201 | } | |
202 | ||
203 | #[derive(Debug,Clone,Copy,PartialEq)] | |
204 | pub enum AllocatorError { | |
205 | TooLargeDimensions, | |
206 | FormatError, | |
207 | } | |
208 | ||
209 | pub fn alloc_video_buffer(vinfo: NAVideoInfo, align: u8) -> Result<NABufferType, AllocatorError> { | |
210 | let fmt = &vinfo.format; | |
211 | let mut new_size: usize = 0; | |
212 | let mut offs: Vec<usize> = Vec::new(); | |
213 | ||
214 | for i in 0..fmt.get_num_comp() { | |
215 | if fmt.get_chromaton(i) == None { return Err(AllocatorError::FormatError); } | |
216 | } | |
217 | ||
218 | let align_mod = ((1 << align) as usize) - 1; | |
219 | let width = ((vinfo.width as usize) + align_mod) & !align_mod; | |
220 | let height = ((vinfo.height as usize) + align_mod) & !align_mod; | |
221 | let mut max_depth = 0; | |
222 | let mut all_packed = true; | |
223 | for i in 0..fmt.get_num_comp() { | |
224 | let chr = fmt.get_chromaton(i).unwrap(); | |
225 | if !chr.is_packed() { | |
226 | all_packed = false; | |
227 | break; | |
228 | } | |
229 | max_depth = max(max_depth, chr.get_depth()); | |
230 | } | |
231 | ||
232 | //todo semi-packed like NV12 | |
233 | if !all_packed { | |
234 | for i in 0..fmt.get_num_comp() { | |
235 | let chr = fmt.get_chromaton(i).unwrap(); | |
236 | if !vinfo.is_flipped() { | |
237 | offs.push(new_size as usize); | |
238 | } | |
239 | let cur_w = chr.get_width(width); | |
240 | let cur_h = chr.get_height(height); | |
241 | let cur_sz = cur_w.checked_mul(cur_h); | |
242 | if cur_sz == None { return Err(AllocatorError::TooLargeDimensions); } | |
243 | let new_sz = new_size.checked_add(cur_sz.unwrap()); | |
244 | if new_sz == None { return Err(AllocatorError::TooLargeDimensions); } | |
245 | new_size = new_sz.unwrap(); | |
246 | if vinfo.is_flipped() { | |
247 | offs.push(new_size as usize); | |
248 | } | |
249 | } | |
250 | if max_depth <= 8 { | |
251 | let mut data: Vec<u8> = Vec::with_capacity(new_size); | |
252 | data.resize(new_size, 0); | |
253 | let buf: NAVideoBuffer<u8> = NAVideoBuffer { data: Rc::new(RefCell::new(data)), info: vinfo, offs: offs }; | |
254 | Ok(NABufferType::Video(buf)) | |
255 | } else { | |
256 | let mut data: Vec<u16> = Vec::with_capacity(new_size); | |
257 | data.resize(new_size, 0); | |
258 | let buf: NAVideoBuffer<u16> = NAVideoBuffer { data: Rc::new(RefCell::new(data)), info: vinfo, offs: offs }; | |
259 | Ok(NABufferType::Video16(buf)) | |
260 | } | |
261 | } else { | |
262 | let elem_sz = fmt.get_elem_size(); | |
263 | let line_sz = width.checked_mul(elem_sz as usize); | |
264 | if line_sz == None { return Err(AllocatorError::TooLargeDimensions); } | |
265 | let new_sz = line_sz.unwrap().checked_mul(height); | |
266 | if new_sz == None { return Err(AllocatorError::TooLargeDimensions); } | |
267 | new_size = new_sz.unwrap(); | |
268 | let mut data: Vec<u8> = Vec::with_capacity(new_size); | |
269 | data.resize(new_size, 0); | |
270 | let buf: NAVideoBuffer<u8> = NAVideoBuffer { data: Rc::new(RefCell::new(data)), info: vinfo, offs: offs }; | |
271 | Ok(NABufferType::VideoPacked(buf)) | |
272 | } | |
273 | } | |
274 | ||
275 | pub fn alloc_audio_buffer(ainfo: NAAudioInfo, nsamples: usize, chmap: NAChannelMap) -> Result<NABufferType, AllocatorError> { | |
276 | let mut offs: Vec<usize> = Vec::new(); | |
277 | if ainfo.format.is_planar() { | |
278 | let len = nsamples.checked_mul(ainfo.channels as usize); | |
279 | if len == None { return Err(AllocatorError::TooLargeDimensions); } | |
280 | let length = len.unwrap(); | |
281 | for i in 0..ainfo.channels { | |
282 | offs.push((i as usize) * nsamples); | |
283 | } | |
284 | if ainfo.format.is_float() { | |
285 | if ainfo.format.get_bits() == 32 { | |
286 | let mut data: Vec<f32> = Vec::with_capacity(length); | |
287 | data.resize(length, 0.0); | |
288 | let buf: NAAudioBuffer<f32> = NAAudioBuffer { data: Rc::new(RefCell::new(data)), info: ainfo, offs: offs, chmap: chmap }; | |
289 | Ok(NABufferType::AudioF32(buf)) | |
290 | } else { | |
291 | Err(AllocatorError::TooLargeDimensions) | |
292 | } | |
293 | } else { | |
294 | if ainfo.format.get_bits() == 8 && !ainfo.format.is_signed() { | |
295 | let mut data: Vec<u8> = Vec::with_capacity(length); | |
296 | data.resize(length, 0); | |
297 | let buf: NAAudioBuffer<u8> = NAAudioBuffer { data: Rc::new(RefCell::new(data)), info: ainfo, offs: offs, chmap: chmap }; | |
298 | Ok(NABufferType::AudioU8(buf)) | |
299 | } else if ainfo.format.get_bits() == 16 && ainfo.format.is_signed() { | |
300 | let mut data: Vec<i16> = Vec::with_capacity(length); | |
301 | data.resize(length, 0); | |
302 | let buf: NAAudioBuffer<i16> = NAAudioBuffer { data: Rc::new(RefCell::new(data)), info: ainfo, offs: offs, chmap: chmap }; | |
303 | Ok(NABufferType::AudioI16(buf)) | |
304 | } else { | |
305 | Err(AllocatorError::TooLargeDimensions) | |
306 | } | |
307 | } | |
308 | } else { | |
309 | let len = nsamples.checked_mul(ainfo.channels as usize); | |
310 | if len == None { return Err(AllocatorError::TooLargeDimensions); } | |
311 | let length = ainfo.format.get_audio_size(len.unwrap() as u64); | |
312 | let mut data: Vec<u8> = Vec::with_capacity(length); | |
313 | data.resize(length, 0); | |
314 | let buf: NAAudioBuffer<u8> = NAAudioBuffer { data: Rc::new(RefCell::new(data)), info: ainfo, offs: offs, chmap: chmap }; | |
315 | Ok(NABufferType::AudioPacked(buf)) | |
316 | } | |
317 | } | |
318 | ||
319 | pub fn alloc_data_buffer(size: usize) -> Result<NABufferType, AllocatorError> { | |
320 | let mut data: Vec<u8> = Vec::with_capacity(size); | |
321 | data.resize(size, 0); | |
322 | let buf: NABufferRefT<u8> = Rc::new(RefCell::new(data)); | |
323 | Ok(NABufferType::Data(buf)) | |
324 | } | |
325 | ||
326 | pub fn copy_buffer(buf: NABufferType) -> NABufferType { | |
327 | buf.clone() | |
328 | } | |
329 | ||
5869fd63 | 330 | #[allow(dead_code)] |
8869d452 KS |
331 | #[derive(Clone)] |
332 | pub struct NACodecInfo { | |
ccae5343 | 333 | name: &'static str, |
5869fd63 | 334 | properties: NACodecTypeInfo, |
8869d452 | 335 | extradata: Option<Rc<Vec<u8>>>, |
5869fd63 KS |
336 | } |
337 | ||
8869d452 | 338 | impl NACodecInfo { |
ccae5343 | 339 | pub fn new(name: &'static str, p: NACodecTypeInfo, edata: Option<Vec<u8>>) -> Self { |
8869d452 KS |
340 | let extradata = match edata { |
341 | None => None, | |
342 | Some(vec) => Some(Rc::new(vec)), | |
343 | }; | |
ccae5343 | 344 | NACodecInfo { name: name, properties: p, extradata: extradata } |
8869d452 | 345 | } |
66116504 KS |
346 | pub fn new_ref(name: &'static str, p: NACodecTypeInfo, edata: Option<Rc<Vec<u8>>>) -> Self { |
347 | NACodecInfo { name: name, properties: p, extradata: edata } | |
348 | } | |
8869d452 KS |
349 | pub fn get_properties(&self) -> NACodecTypeInfo { self.properties } |
350 | pub fn get_extradata(&self) -> Option<Rc<Vec<u8>>> { | |
351 | if let Some(ref vec) = self.extradata { return Some(vec.clone()); } | |
352 | None | |
5869fd63 | 353 | } |
66116504 KS |
354 | pub fn get_name(&self) -> &'static str { self.name } |
355 | pub fn is_video(&self) -> bool { | |
356 | if let NACodecTypeInfo::Video(_) = self.properties { return true; } | |
357 | false | |
358 | } | |
359 | pub fn is_audio(&self) -> bool { | |
360 | if let NACodecTypeInfo::Audio(_) = self.properties { return true; } | |
361 | false | |
362 | } | |
363 | } | |
364 | ||
365 | impl fmt::Display for NACodecInfo { | |
366 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | |
367 | let edata = match self.extradata.clone() { | |
368 | None => format!("no extradata"), | |
369 | Some(v) => format!("{} byte(s) of extradata", v.len()), | |
370 | }; | |
371 | write!(f, "{}: {} {}", self.name, self.properties, edata) | |
372 | } | |
373 | } | |
374 | ||
375 | pub const DUMMY_CODEC_INFO: NACodecInfo = NACodecInfo { | |
376 | name: "none", | |
377 | properties: NACodecTypeInfo::None, | |
378 | extradata: None }; | |
379 | ||
66116504 KS |
380 | #[derive(Debug,Clone)] |
381 | pub enum NAValue { | |
5869fd63 KS |
382 | None, |
383 | Int(i32), | |
384 | Long(i64), | |
385 | String(String), | |
66116504 | 386 | Data(Rc<Vec<u8>>), |
5869fd63 KS |
387 | } |
388 | ||
88c03b61 KS |
389 | #[derive(Debug,Clone,Copy,PartialEq)] |
390 | #[allow(dead_code)] | |
391 | pub enum FrameType { | |
392 | I, | |
393 | P, | |
394 | B, | |
395 | Other, | |
396 | } | |
397 | ||
398 | impl fmt::Display for FrameType { | |
399 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | |
400 | match *self { | |
401 | FrameType::I => write!(f, "I"), | |
402 | FrameType::P => write!(f, "P"), | |
403 | FrameType::B => write!(f, "B"), | |
404 | FrameType::Other => write!(f, "x"), | |
405 | } | |
406 | } | |
407 | } | |
408 | ||
5869fd63 | 409 | #[allow(dead_code)] |
66116504 KS |
410 | #[derive(Clone)] |
411 | pub struct NAFrame { | |
5869fd63 KS |
412 | pts: Option<u64>, |
413 | dts: Option<u64>, | |
414 | duration: Option<u64>, | |
22cb00db | 415 | buffer: NABufferType, |
66116504 | 416 | info: Rc<NACodecInfo>, |
88c03b61 KS |
417 | ftype: FrameType, |
418 | key: bool, | |
66116504 KS |
419 | options: HashMap<String, NAValue>, |
420 | } | |
421 | ||
ebd71c92 KS |
422 | pub type NAFrameRef = Rc<RefCell<NAFrame>>; |
423 | ||
66116504 KS |
424 | fn get_plane_size(info: &NAVideoInfo, idx: usize) -> (usize, usize) { |
425 | let chromaton = info.get_format().get_chromaton(idx); | |
426 | if let None = chromaton { return (0, 0); } | |
427 | let (hs, vs) = chromaton.unwrap().get_subsampling(); | |
428 | let w = (info.get_width() + ((1 << hs) - 1)) >> hs; | |
429 | let h = (info.get_height() + ((1 << vs) - 1)) >> vs; | |
430 | (w, h) | |
431 | } | |
432 | ||
433 | impl NAFrame { | |
434 | pub fn new(pts: Option<u64>, | |
435 | dts: Option<u64>, | |
436 | duration: Option<u64>, | |
88c03b61 KS |
437 | ftype: FrameType, |
438 | keyframe: bool, | |
66116504 | 439 | info: Rc<NACodecInfo>, |
22cb00db KS |
440 | options: HashMap<String, NAValue>, |
441 | buffer: NABufferType) -> Self { | |
442 | NAFrame { pts: pts, dts: dts, duration: duration, buffer: buffer, info: info, ftype: ftype, key: keyframe, options: options } | |
ebd71c92 | 443 | } |
66116504 KS |
444 | pub fn get_pts(&self) -> Option<u64> { self.pts } |
445 | pub fn get_dts(&self) -> Option<u64> { self.dts } | |
446 | pub fn get_duration(&self) -> Option<u64> { self.duration } | |
88c03b61 KS |
447 | pub fn get_frame_type(&self) -> FrameType { self.ftype } |
448 | pub fn is_keyframe(&self) -> bool { self.key } | |
66116504 KS |
449 | pub fn set_pts(&mut self, pts: Option<u64>) { self.pts = pts; } |
450 | pub fn set_dts(&mut self, dts: Option<u64>) { self.dts = dts; } | |
451 | pub fn set_duration(&mut self, dur: Option<u64>) { self.duration = dur; } | |
88c03b61 KS |
452 | pub fn set_frame_type(&mut self, ftype: FrameType) { self.ftype = ftype; } |
453 | pub fn set_keyframe(&mut self, key: bool) { self.key = key; } | |
66116504 | 454 | |
22cb00db | 455 | pub fn get_buffer(&self) -> NABufferType { self.buffer.clone() } |
5869fd63 KS |
456 | } |
457 | ||
ebd71c92 KS |
458 | impl fmt::Display for NAFrame { |
459 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | |
22cb00db | 460 | let mut foo = format!("frame type {}", self.ftype); |
ebd71c92 KS |
461 | if let Some(pts) = self.pts { foo = format!("{} pts {}", foo, pts); } |
462 | if let Some(dts) = self.dts { foo = format!("{} dts {}", foo, dts); } | |
463 | if let Some(dur) = self.duration { foo = format!("{} duration {}", foo, dur); } | |
464 | if self.key { foo = format!("{} kf", foo); } | |
465 | write!(f, "[{}]", foo) | |
466 | } | |
467 | } | |
88c03b61 | 468 | |
48c88fde KS |
469 | /// Possible stream types. |
470 | #[derive(Debug,Clone,Copy)] | |
5869fd63 | 471 | #[allow(dead_code)] |
48c88fde KS |
472 | pub enum StreamType { |
473 | /// video stream | |
474 | Video, | |
475 | /// audio stream | |
476 | Audio, | |
477 | /// subtitles | |
478 | Subtitles, | |
479 | /// any data stream (or might be an unrecognized audio/video stream) | |
480 | Data, | |
481 | /// nonexistent stream | |
482 | None, | |
483 | } | |
484 | ||
485 | impl fmt::Display for StreamType { | |
486 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | |
487 | match *self { | |
488 | StreamType::Video => write!(f, "Video"), | |
489 | StreamType::Audio => write!(f, "Audio"), | |
490 | StreamType::Subtitles => write!(f, "Subtitles"), | |
491 | StreamType::Data => write!(f, "Data"), | |
492 | StreamType::None => write!(f, "-"), | |
493 | } | |
494 | } | |
495 | } | |
496 | ||
497 | #[allow(dead_code)] | |
498 | #[derive(Clone)] | |
499 | pub struct NAStream { | |
500 | media_type: StreamType, | |
501 | id: u32, | |
502 | num: usize, | |
503 | info: Rc<NACodecInfo>, | |
5869fd63 | 504 | } |
48c88fde KS |
505 | |
506 | impl NAStream { | |
507 | pub fn new(mt: StreamType, id: u32, info: NACodecInfo) -> Self { | |
508 | NAStream { media_type: mt, id: id, num: 0, info: Rc::new(info) } | |
509 | } | |
510 | pub fn get_id(&self) -> u32 { self.id } | |
511 | pub fn get_num(&self) -> usize { self.num } | |
512 | pub fn set_num(&mut self, num: usize) { self.num = num; } | |
513 | pub fn get_info(&self) -> Rc<NACodecInfo> { self.info.clone() } | |
514 | } | |
515 | ||
516 | impl fmt::Display for NAStream { | |
517 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | |
518 | write!(f, "({}#{} - {})", self.media_type, self.id, self.info.get_properties()) | |
519 | } | |
520 | } | |
521 | ||
522 | #[allow(dead_code)] | |
523 | pub struct NAPacket { | |
524 | stream: Rc<NAStream>, | |
525 | pts: Option<u64>, | |
526 | dts: Option<u64>, | |
527 | duration: Option<u64>, | |
528 | buffer: Rc<Vec<u8>>, | |
529 | keyframe: bool, | |
530 | // options: HashMap<String, NAValue<'a>>, | |
531 | } | |
532 | ||
533 | impl NAPacket { | |
534 | pub fn new(str: Rc<NAStream>, pts: Option<u64>, dts: Option<u64>, dur: Option<u64>, kf: bool, vec: Vec<u8>) -> Self { | |
535 | // let mut vec: Vec<u8> = Vec::new(); | |
536 | // vec.resize(size, 0); | |
537 | NAPacket { stream: str, pts: pts, dts: dts, duration: dur, keyframe: kf, buffer: Rc::new(vec) } | |
538 | } | |
539 | pub fn get_stream(&self) -> Rc<NAStream> { self.stream.clone() } | |
540 | pub fn get_pts(&self) -> Option<u64> { self.pts } | |
541 | pub fn get_dts(&self) -> Option<u64> { self.dts } | |
542 | pub fn get_duration(&self) -> Option<u64> { self.duration } | |
543 | pub fn is_keyframe(&self) -> bool { self.keyframe } | |
544 | pub fn get_buffer(&self) -> Rc<Vec<u8>> { self.buffer.clone() } | |
545 | } | |
546 | ||
547 | impl Drop for NAPacket { | |
548 | fn drop(&mut self) {} | |
549 | } | |
550 | ||
551 | impl fmt::Display for NAPacket { | |
552 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | |
553 | let mut foo = format!("[pkt for {} size {}", self.stream, self.buffer.len()); | |
554 | if let Some(pts) = self.pts { foo = format!("{} pts {}", foo, pts); } | |
555 | if let Some(dts) = self.dts { foo = format!("{} dts {}", foo, dts); } | |
556 | if let Some(dur) = self.duration { foo = format!("{} duration {}", foo, dur); } | |
557 | if self.keyframe { foo = format!("{} kf", foo); } | |
558 | foo = foo + "]"; | |
559 | write!(f, "{}", foo) | |
560 | } | |
561 | } | |
562 | ||
563 | pub trait FrameFromPacket { | |
22cb00db | 564 | fn new_from_pkt(pkt: &NAPacket, info: Rc<NACodecInfo>, buf: NABufferType) -> NAFrame; |
48c88fde KS |
565 | fn fill_timestamps(&mut self, pkt: &NAPacket); |
566 | } | |
567 | ||
568 | impl FrameFromPacket for NAFrame { | |
22cb00db KS |
569 | fn new_from_pkt(pkt: &NAPacket, info: Rc<NACodecInfo>, buf: NABufferType) -> NAFrame { |
570 | NAFrame::new(pkt.pts, pkt.dts, pkt.duration, FrameType::Other, pkt.keyframe, info, HashMap::new(), buf) | |
48c88fde KS |
571 | } |
572 | fn fill_timestamps(&mut self, pkt: &NAPacket) { | |
573 | self.set_pts(pkt.pts); | |
574 | self.set_dts(pkt.dts); | |
575 | self.set_duration(pkt.duration); | |
576 | } | |
577 | } | |
578 |