]> git.nihav.org Git - nihav.git/blame - src/frame.rs
AVI demuxer: align chunk pos
[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
253 if !all_packed {
254 for i in 0..fmt.get_num_comp() {
255 let chr = fmt.get_chromaton(i).unwrap();
256 if !vinfo.is_flipped() {
257 offs.push(new_size as usize);
258 }
259 let cur_w = chr.get_width(width);
260 let cur_h = chr.get_height(height);
261 let cur_sz = cur_w.checked_mul(cur_h);
262 if cur_sz == None { return Err(AllocatorError::TooLargeDimensions); }
263 let new_sz = new_size.checked_add(cur_sz.unwrap());
264 if new_sz == None { return Err(AllocatorError::TooLargeDimensions); }
265 new_size = new_sz.unwrap();
266 if vinfo.is_flipped() {
267 offs.push(new_size as usize);
268 }
269 }
270 if max_depth <= 8 {
271 let mut data: Vec<u8> = Vec::with_capacity(new_size);
272 data.resize(new_size, 0);
273 let buf: NAVideoBuffer<u8> = NAVideoBuffer { data: Rc::new(RefCell::new(data)), info: vinfo, offs: offs };
274 Ok(NABufferType::Video(buf))
275 } else {
276 let mut data: Vec<u16> = Vec::with_capacity(new_size);
277 data.resize(new_size, 0);
278 let buf: NAVideoBuffer<u16> = NAVideoBuffer { data: Rc::new(RefCell::new(data)), info: vinfo, offs: offs };
279 Ok(NABufferType::Video16(buf))
280 }
281 } else {
282 let elem_sz = fmt.get_elem_size();
283 let line_sz = width.checked_mul(elem_sz as usize);
284 if line_sz == None { return Err(AllocatorError::TooLargeDimensions); }
285 let new_sz = line_sz.unwrap().checked_mul(height);
286 if new_sz == None { return Err(AllocatorError::TooLargeDimensions); }
287 new_size = new_sz.unwrap();
288 let mut data: Vec<u8> = Vec::with_capacity(new_size);
289 data.resize(new_size, 0);
290 let buf: NAVideoBuffer<u8> = NAVideoBuffer { data: Rc::new(RefCell::new(data)), info: vinfo, offs: offs };
291 Ok(NABufferType::VideoPacked(buf))
292 }
293}
294
295pub fn alloc_audio_buffer(ainfo: NAAudioInfo, nsamples: usize, chmap: NAChannelMap) -> Result<NABufferType, AllocatorError> {
296 let mut offs: Vec<usize> = Vec::new();
297 if ainfo.format.is_planar() {
298 let len = nsamples.checked_mul(ainfo.channels as usize);
299 if len == None { return Err(AllocatorError::TooLargeDimensions); }
300 let length = len.unwrap();
301 for i in 0..ainfo.channels {
302 offs.push((i as usize) * nsamples);
303 }
304 if ainfo.format.is_float() {
305 if ainfo.format.get_bits() == 32 {
306 let mut data: Vec<f32> = Vec::with_capacity(length);
307 data.resize(length, 0.0);
308 let buf: NAAudioBuffer<f32> = NAAudioBuffer { data: Rc::new(RefCell::new(data)), info: ainfo, offs: offs, chmap: chmap };
309 Ok(NABufferType::AudioF32(buf))
310 } else {
311 Err(AllocatorError::TooLargeDimensions)
312 }
313 } else {
314 if ainfo.format.get_bits() == 8 && !ainfo.format.is_signed() {
315 let mut data: Vec<u8> = Vec::with_capacity(length);
316 data.resize(length, 0);
317 let buf: NAAudioBuffer<u8> = NAAudioBuffer { data: Rc::new(RefCell::new(data)), info: ainfo, offs: offs, chmap: chmap };
318 Ok(NABufferType::AudioU8(buf))
319 } else if ainfo.format.get_bits() == 16 && ainfo.format.is_signed() {
320 let mut data: Vec<i16> = Vec::with_capacity(length);
321 data.resize(length, 0);
322 let buf: NAAudioBuffer<i16> = NAAudioBuffer { data: Rc::new(RefCell::new(data)), info: ainfo, offs: offs, chmap: chmap };
323 Ok(NABufferType::AudioI16(buf))
324 } else {
325 Err(AllocatorError::TooLargeDimensions)
326 }
327 }
328 } else {
329 let len = nsamples.checked_mul(ainfo.channels as usize);
330 if len == None { return Err(AllocatorError::TooLargeDimensions); }
331 let length = ainfo.format.get_audio_size(len.unwrap() as u64);
332 let mut data: Vec<u8> = Vec::with_capacity(length);
333 data.resize(length, 0);
334 let buf: NAAudioBuffer<u8> = NAAudioBuffer { data: Rc::new(RefCell::new(data)), info: ainfo, offs: offs, chmap: chmap };
335 Ok(NABufferType::AudioPacked(buf))
336 }
337}
338
339pub fn alloc_data_buffer(size: usize) -> Result<NABufferType, AllocatorError> {
340 let mut data: Vec<u8> = Vec::with_capacity(size);
341 data.resize(size, 0);
342 let buf: NABufferRefT<u8> = Rc::new(RefCell::new(data));
343 Ok(NABufferType::Data(buf))
344}
345
346pub fn copy_buffer(buf: NABufferType) -> NABufferType {
347 buf.clone()
348}
349
5869fd63 350#[allow(dead_code)]
8869d452
KS
351#[derive(Clone)]
352pub struct NACodecInfo {
ccae5343 353 name: &'static str,
5869fd63 354 properties: NACodecTypeInfo,
8869d452 355 extradata: Option<Rc<Vec<u8>>>,
5869fd63
KS
356}
357
8869d452 358impl NACodecInfo {
ccae5343 359 pub fn new(name: &'static str, p: NACodecTypeInfo, edata: Option<Vec<u8>>) -> Self {
8869d452
KS
360 let extradata = match edata {
361 None => None,
362 Some(vec) => Some(Rc::new(vec)),
363 };
ccae5343 364 NACodecInfo { name: name, properties: p, extradata: extradata }
8869d452 365 }
66116504
KS
366 pub fn new_ref(name: &'static str, p: NACodecTypeInfo, edata: Option<Rc<Vec<u8>>>) -> Self {
367 NACodecInfo { name: name, properties: p, extradata: edata }
368 }
8869d452
KS
369 pub fn get_properties(&self) -> NACodecTypeInfo { self.properties }
370 pub fn get_extradata(&self) -> Option<Rc<Vec<u8>>> {
371 if let Some(ref vec) = self.extradata { return Some(vec.clone()); }
372 None
5869fd63 373 }
66116504
KS
374 pub fn get_name(&self) -> &'static str { self.name }
375 pub fn is_video(&self) -> bool {
376 if let NACodecTypeInfo::Video(_) = self.properties { return true; }
377 false
378 }
379 pub fn is_audio(&self) -> bool {
380 if let NACodecTypeInfo::Audio(_) = self.properties { return true; }
381 false
382 }
383}
384
385impl fmt::Display for NACodecInfo {
386 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
387 let edata = match self.extradata.clone() {
388 None => format!("no extradata"),
389 Some(v) => format!("{} byte(s) of extradata", v.len()),
390 };
391 write!(f, "{}: {} {}", self.name, self.properties, edata)
392 }
393}
394
395pub const DUMMY_CODEC_INFO: NACodecInfo = NACodecInfo {
396 name: "none",
397 properties: NACodecTypeInfo::None,
398 extradata: None };
399
66116504
KS
400#[derive(Debug,Clone)]
401pub enum NAValue {
5869fd63
KS
402 None,
403 Int(i32),
404 Long(i64),
405 String(String),
66116504 406 Data(Rc<Vec<u8>>),
5869fd63
KS
407}
408
88c03b61
KS
409#[derive(Debug,Clone,Copy,PartialEq)]
410#[allow(dead_code)]
411pub enum FrameType {
412 I,
413 P,
414 B,
415 Other,
416}
417
418impl fmt::Display for FrameType {
419 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
420 match *self {
421 FrameType::I => write!(f, "I"),
422 FrameType::P => write!(f, "P"),
423 FrameType::B => write!(f, "B"),
424 FrameType::Other => write!(f, "x"),
425 }
426 }
427}
428
e189501e
KS
429#[derive(Debug,Clone,Copy)]
430pub struct NATimeInfo {
5869fd63
KS
431 pts: Option<u64>,
432 dts: Option<u64>,
433 duration: Option<u64>,
e189501e
KS
434 tb_num: u32,
435 tb_den: u32,
436}
437
438impl NATimeInfo {
439 pub fn new(pts: Option<u64>, dts: Option<u64>, duration: Option<u64>, tb_num: u32, tb_den: u32) -> Self {
440 NATimeInfo { pts: pts, dts: dts, duration: duration, tb_num: tb_num, tb_den: tb_den }
441 }
442 pub fn get_pts(&self) -> Option<u64> { self.pts }
443 pub fn get_dts(&self) -> Option<u64> { self.dts }
444 pub fn get_duration(&self) -> Option<u64> { self.duration }
445 pub fn set_pts(&mut self, pts: Option<u64>) { self.pts = pts; }
446 pub fn set_dts(&mut self, dts: Option<u64>) { self.dts = dts; }
447 pub fn set_duration(&mut self, dur: Option<u64>) { self.duration = dur; }
448}
449
450#[allow(dead_code)]
451#[derive(Clone)]
452pub struct NAFrame {
453 ts: NATimeInfo,
22cb00db 454 buffer: NABufferType,
66116504 455 info: Rc<NACodecInfo>,
88c03b61
KS
456 ftype: FrameType,
457 key: bool,
66116504
KS
458 options: HashMap<String, NAValue>,
459}
460
ebd71c92
KS
461pub type NAFrameRef = Rc<RefCell<NAFrame>>;
462
66116504
KS
463fn get_plane_size(info: &NAVideoInfo, idx: usize) -> (usize, usize) {
464 let chromaton = info.get_format().get_chromaton(idx);
465 if let None = chromaton { return (0, 0); }
466 let (hs, vs) = chromaton.unwrap().get_subsampling();
467 let w = (info.get_width() + ((1 << hs) - 1)) >> hs;
468 let h = (info.get_height() + ((1 << vs) - 1)) >> vs;
469 (w, h)
470}
471
472impl NAFrame {
e189501e 473 pub fn new(ts: NATimeInfo,
88c03b61
KS
474 ftype: FrameType,
475 keyframe: bool,
66116504 476 info: Rc<NACodecInfo>,
22cb00db
KS
477 options: HashMap<String, NAValue>,
478 buffer: NABufferType) -> Self {
e189501e 479 NAFrame { ts: ts, buffer: buffer, info: info, ftype: ftype, key: keyframe, options: options }
ebd71c92 480 }
88c03b61
KS
481 pub fn get_frame_type(&self) -> FrameType { self.ftype }
482 pub fn is_keyframe(&self) -> bool { self.key }
88c03b61
KS
483 pub fn set_frame_type(&mut self, ftype: FrameType) { self.ftype = ftype; }
484 pub fn set_keyframe(&mut self, key: bool) { self.key = key; }
e189501e
KS
485 pub fn get_time_information(&self) -> NATimeInfo { self.ts }
486 pub fn get_pts(&self) -> Option<u64> { self.ts.get_pts() }
487 pub fn get_dts(&self) -> Option<u64> { self.ts.get_dts() }
488 pub fn get_duration(&self) -> Option<u64> { self.ts.get_duration() }
489 pub fn set_pts(&mut self, pts: Option<u64>) { self.ts.set_pts(pts); }
490 pub fn set_dts(&mut self, dts: Option<u64>) { self.ts.set_dts(dts); }
491 pub fn set_duration(&mut self, dur: Option<u64>) { self.ts.set_duration(dur); }
66116504 492
22cb00db 493 pub fn get_buffer(&self) -> NABufferType { self.buffer.clone() }
5869fd63
KS
494}
495
ebd71c92
KS
496impl fmt::Display for NAFrame {
497 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
22cb00db 498 let mut foo = format!("frame type {}", self.ftype);
e189501e
KS
499 if let Some(pts) = self.ts.pts { foo = format!("{} pts {}", foo, pts); }
500 if let Some(dts) = self.ts.dts { foo = format!("{} dts {}", foo, dts); }
501 if let Some(dur) = self.ts.duration { foo = format!("{} duration {}", foo, dur); }
ebd71c92
KS
502 if self.key { foo = format!("{} kf", foo); }
503 write!(f, "[{}]", foo)
504 }
505}
88c03b61 506
48c88fde
KS
507/// Possible stream types.
508#[derive(Debug,Clone,Copy)]
5869fd63 509#[allow(dead_code)]
48c88fde
KS
510pub enum StreamType {
511 /// video stream
512 Video,
513 /// audio stream
514 Audio,
515 /// subtitles
516 Subtitles,
517 /// any data stream (or might be an unrecognized audio/video stream)
518 Data,
519 /// nonexistent stream
520 None,
521}
522
523impl fmt::Display for StreamType {
524 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
525 match *self {
526 StreamType::Video => write!(f, "Video"),
527 StreamType::Audio => write!(f, "Audio"),
528 StreamType::Subtitles => write!(f, "Subtitles"),
529 StreamType::Data => write!(f, "Data"),
530 StreamType::None => write!(f, "-"),
531 }
532 }
533}
534
535#[allow(dead_code)]
536#[derive(Clone)]
537pub struct NAStream {
538 media_type: StreamType,
539 id: u32,
540 num: usize,
541 info: Rc<NACodecInfo>,
e189501e
KS
542 tb_num: u32,
543 tb_den: u32,
544}
545
546pub fn reduce_timebase(tb_num: u32, tb_den: u32) -> (u32, u32) {
547 if tb_num == 0 { return (tb_num, tb_den); }
548 if (tb_den % tb_num) == 0 { return (1, tb_den / tb_num); }
549
550 let mut a = tb_num;
551 let mut b = tb_den;
552
553 while a != b {
554 if a > b { a -= b; }
555 else if b > a { b -= a; }
556 }
557
558 (tb_num / a, tb_den / a)
5869fd63 559}
48c88fde
KS
560
561impl NAStream {
e189501e
KS
562 pub fn new(mt: StreamType, id: u32, info: NACodecInfo, tb_num: u32, tb_den: u32) -> Self {
563 let (n, d) = reduce_timebase(tb_num, tb_den);
564 NAStream { media_type: mt, id: id, num: 0, info: Rc::new(info), tb_num: n, tb_den: d }
48c88fde
KS
565 }
566 pub fn get_id(&self) -> u32 { self.id }
567 pub fn get_num(&self) -> usize { self.num }
568 pub fn set_num(&mut self, num: usize) { self.num = num; }
569 pub fn get_info(&self) -> Rc<NACodecInfo> { self.info.clone() }
e189501e
KS
570 pub fn get_timebase(&self) -> (u32, u32) { (self.tb_num, self.tb_den) }
571 pub fn set_timebase(&mut self, tb_num: u32, tb_den: u32) {
572 let (n, d) = reduce_timebase(tb_num, tb_den);
573 self.tb_num = n;
574 self.tb_den = d;
575 }
48c88fde
KS
576}
577
578impl fmt::Display for NAStream {
579 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
e189501e 580 write!(f, "({}#{} @ {}/{} - {})", self.media_type, self.id, self.tb_num, self.tb_den, self.info.get_properties())
48c88fde
KS
581 }
582}
583
584#[allow(dead_code)]
585pub struct NAPacket {
586 stream: Rc<NAStream>,
e189501e 587 ts: NATimeInfo,
48c88fde
KS
588 buffer: Rc<Vec<u8>>,
589 keyframe: bool,
590// options: HashMap<String, NAValue<'a>>,
591}
592
593impl NAPacket {
e189501e 594 pub fn new(str: Rc<NAStream>, ts: NATimeInfo, kf: bool, vec: Vec<u8>) -> Self {
48c88fde
KS
595// let mut vec: Vec<u8> = Vec::new();
596// vec.resize(size, 0);
e189501e 597 NAPacket { stream: str, ts: ts, keyframe: kf, buffer: Rc::new(vec) }
48c88fde
KS
598 }
599 pub fn get_stream(&self) -> Rc<NAStream> { self.stream.clone() }
e189501e
KS
600 pub fn get_time_information(&self) -> NATimeInfo { self.ts }
601 pub fn get_pts(&self) -> Option<u64> { self.ts.get_pts() }
602 pub fn get_dts(&self) -> Option<u64> { self.ts.get_dts() }
603 pub fn get_duration(&self) -> Option<u64> { self.ts.get_duration() }
48c88fde
KS
604 pub fn is_keyframe(&self) -> bool { self.keyframe }
605 pub fn get_buffer(&self) -> Rc<Vec<u8>> { self.buffer.clone() }
606}
607
608impl Drop for NAPacket {
609 fn drop(&mut self) {}
610}
611
612impl fmt::Display for NAPacket {
613 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
614 let mut foo = format!("[pkt for {} size {}", self.stream, self.buffer.len());
e189501e
KS
615 if let Some(pts) = self.ts.pts { foo = format!("{} pts {}", foo, pts); }
616 if let Some(dts) = self.ts.dts { foo = format!("{} dts {}", foo, dts); }
617 if let Some(dur) = self.ts.duration { foo = format!("{} duration {}", foo, dur); }
48c88fde
KS
618 if self.keyframe { foo = format!("{} kf", foo); }
619 foo = foo + "]";
620 write!(f, "{}", foo)
621 }
622}
623
624pub trait FrameFromPacket {
22cb00db 625 fn new_from_pkt(pkt: &NAPacket, info: Rc<NACodecInfo>, buf: NABufferType) -> NAFrame;
48c88fde
KS
626 fn fill_timestamps(&mut self, pkt: &NAPacket);
627}
628
629impl FrameFromPacket for NAFrame {
22cb00db 630 fn new_from_pkt(pkt: &NAPacket, info: Rc<NACodecInfo>, buf: NABufferType) -> NAFrame {
e189501e 631 NAFrame::new(pkt.ts, FrameType::Other, pkt.keyframe, info, HashMap::new(), buf)
48c88fde
KS
632 }
633 fn fill_timestamps(&mut self, pkt: &NAPacket) {
e189501e 634 self.ts = pkt.get_time_information();
48c88fde
KS
635 }
636}
637