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