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