2 use std::collections::HashMap;
6 pub use crate::formats::*;
7 pub use crate::refs::*;
10 #[derive(Clone,Copy,PartialEq)]
11 pub struct NAAudioInfo {
19 pub fn new(sr: u32, ch: u8, fmt: NASoniton, bl: usize) -> Self {
20 NAAudioInfo { sample_rate: sr, channels: ch, format: fmt, block_len: bl }
22 pub fn get_sample_rate(&self) -> u32 { self.sample_rate }
23 pub fn get_channels(&self) -> u8 { self.channels }
24 pub fn get_format(&self) -> NASoniton { self.format }
25 pub fn get_block_len(&self) -> usize { self.block_len }
28 impl fmt::Display for NAAudioInfo {
29 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
30 write!(f, "{} Hz, {} ch", self.sample_rate, self.channels)
35 #[derive(Clone,Copy,PartialEq)]
36 pub struct NAVideoInfo {
40 format: NAPixelFormaton,
44 pub fn new(w: usize, h: usize, flip: bool, fmt: NAPixelFormaton) -> Self {
45 NAVideoInfo { width: w, height: h, flipped: flip, format: fmt }
47 pub fn get_width(&self) -> usize { self.width as usize }
48 pub fn get_height(&self) -> usize { self.height as usize }
49 pub fn is_flipped(&self) -> bool { self.flipped }
50 pub fn get_format(&self) -> NAPixelFormaton { self.format }
51 pub fn set_width(&mut self, w: usize) { self.width = w; }
52 pub fn set_height(&mut self, h: usize) { self.height = h; }
55 impl fmt::Display for NAVideoInfo {
56 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
57 write!(f, "{}x{}", self.width, self.height)
61 #[derive(Clone,Copy,PartialEq)]
62 pub enum NACodecTypeInfo {
68 impl NACodecTypeInfo {
69 pub fn get_video_info(&self) -> Option<NAVideoInfo> {
71 NACodecTypeInfo::Video(vinfo) => Some(vinfo),
75 pub fn get_audio_info(&self) -> Option<NAAudioInfo> {
77 NACodecTypeInfo::Audio(ainfo) => Some(ainfo),
81 pub fn is_video(&self) -> bool {
83 NACodecTypeInfo::Video(_) => true,
87 pub fn is_audio(&self) -> bool {
89 NACodecTypeInfo::Audio(_) => true,
95 impl fmt::Display for NACodecTypeInfo {
96 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
97 let ret = match *self {
98 NACodecTypeInfo::None => format!(""),
99 NACodecTypeInfo::Audio(fmt) => format!("{}", fmt),
100 NACodecTypeInfo::Video(fmt) => format!("{}", fmt),
107 pub struct NAVideoBuffer<T> {
109 data: NABufferRef<Vec<T>>,
114 impl<T: Clone> NAVideoBuffer<T> {
115 pub fn get_offset(&self, idx: usize) -> usize {
116 if idx >= self.offs.len() { 0 }
117 else { self.offs[idx] }
119 pub fn get_info(&self) -> NAVideoInfo { self.info }
120 pub fn get_data(&self) -> &Vec<T> { self.data.as_ref() }
121 pub fn get_data_mut(&mut self) -> Option<&mut Vec<T>> { self.data.as_mut() }
122 pub fn copy_buffer(&mut self) -> Self {
123 let mut data: Vec<T> = Vec::with_capacity(self.data.len());
124 data.clone_from(self.data.as_ref());
125 let mut offs: Vec<usize> = Vec::with_capacity(self.offs.len());
126 offs.clone_from(&self.offs);
127 let mut strides: Vec<usize> = Vec::with_capacity(self.strides.len());
128 strides.clone_from(&self.strides);
129 NAVideoBuffer { info: self.info, data: NABufferRef::new(data), offs: offs, strides: strides }
131 pub fn get_stride(&self, idx: usize) -> usize {
132 if idx >= self.strides.len() { return 0; }
135 pub fn get_dimensions(&self, idx: usize) -> (usize, usize) {
136 get_plane_size(&self.info, idx)
141 pub struct NAAudioBuffer<T> {
143 data: NABufferRef<Vec<T>>,
149 impl<T: Clone> NAAudioBuffer<T> {
150 pub fn get_offset(&self, idx: usize) -> usize {
151 if idx >= self.offs.len() { 0 }
152 else { self.offs[idx] }
154 pub fn get_info(&self) -> NAAudioInfo { self.info }
155 pub fn get_chmap(&self) -> NAChannelMap { self.chmap.clone() }
156 pub fn get_data(&self) -> &Vec<T> { self.data.as_ref() }
157 pub fn get_data_mut(&mut self) -> Option<&mut Vec<T>> { self.data.as_mut() }
158 pub fn copy_buffer(&mut self) -> Self {
159 let mut data: Vec<T> = Vec::with_capacity(self.data.len());
160 data.clone_from(self.data.as_ref());
161 let mut offs: Vec<usize> = Vec::with_capacity(self.offs.len());
162 offs.clone_from(&self.offs);
163 NAAudioBuffer { info: self.info, data: NABufferRef::new(data), offs: offs, chmap: self.get_chmap(), len: self.len }
165 pub fn get_length(&self) -> usize { self.len }
168 impl NAAudioBuffer<u8> {
169 pub fn new_from_buf(info: NAAudioInfo, data: NABufferRef<Vec<u8>>, chmap: NAChannelMap) -> Self {
170 let len = data.len();
171 NAAudioBuffer { info: info, data: data, chmap: chmap, offs: Vec::new(), len: len }
176 pub enum NABufferType {
177 Video (NAVideoBuffer<u8>),
178 Video16 (NAVideoBuffer<u16>),
179 Video32 (NAVideoBuffer<u32>),
180 VideoPacked(NAVideoBuffer<u8>),
181 AudioU8 (NAAudioBuffer<u8>),
182 AudioI16 (NAAudioBuffer<i16>),
183 AudioI32 (NAAudioBuffer<i32>),
184 AudioF32 (NAAudioBuffer<f32>),
185 AudioPacked(NAAudioBuffer<u8>),
186 Data (NABufferRef<Vec<u8>>),
191 pub fn get_offset(&self, idx: usize) -> usize {
193 NABufferType::Video(ref vb) => vb.get_offset(idx),
194 NABufferType::Video16(ref vb) => vb.get_offset(idx),
195 NABufferType::Video32(ref vb) => vb.get_offset(idx),
196 NABufferType::VideoPacked(ref vb) => vb.get_offset(idx),
197 NABufferType::AudioU8(ref ab) => ab.get_offset(idx),
198 NABufferType::AudioI16(ref ab) => ab.get_offset(idx),
199 NABufferType::AudioF32(ref ab) => ab.get_offset(idx),
200 NABufferType::AudioPacked(ref ab) => ab.get_offset(idx),
204 pub fn get_video_info(&self) -> Option<NAVideoInfo> {
206 NABufferType::Video(ref vb) => Some(vb.get_info()),
207 NABufferType::Video16(ref vb) => Some(vb.get_info()),
208 NABufferType::Video32(ref vb) => Some(vb.get_info()),
209 NABufferType::VideoPacked(ref vb) => Some(vb.get_info()),
213 pub fn get_vbuf(&self) -> Option<NAVideoBuffer<u8>> {
215 NABufferType::Video(ref vb) => Some(vb.clone()),
216 NABufferType::VideoPacked(ref vb) => Some(vb.clone()),
220 pub fn get_vbuf16(&self) -> Option<NAVideoBuffer<u16>> {
222 NABufferType::Video16(ref vb) => Some(vb.clone()),
226 pub fn get_vbuf32(&self) -> Option<NAVideoBuffer<u32>> {
228 NABufferType::Video32(ref vb) => Some(vb.clone()),
232 pub fn get_abuf_u8(&self) -> Option<NAAudioBuffer<u8>> {
234 NABufferType::AudioU8(ref ab) => Some(ab.clone()),
235 NABufferType::AudioPacked(ref ab) => Some(ab.clone()),
239 pub fn get_abuf_i16(&self) -> Option<NAAudioBuffer<i16>> {
241 NABufferType::AudioI16(ref ab) => Some(ab.clone()),
245 pub fn get_abuf_i32(&self) -> Option<NAAudioBuffer<i32>> {
247 NABufferType::AudioI32(ref ab) => Some(ab.clone()),
251 pub fn get_abuf_f32(&self) -> Option<NAAudioBuffer<f32>> {
253 NABufferType::AudioF32(ref ab) => Some(ab.clone()),
259 const NA_SIMPLE_VFRAME_COMPONENTS: usize = 4;
260 pub struct NASimpleVideoFrame<'a, T: Copy> {
261 pub width: [usize; NA_SIMPLE_VFRAME_COMPONENTS],
262 pub height: [usize; NA_SIMPLE_VFRAME_COMPONENTS],
264 pub stride: [usize; NA_SIMPLE_VFRAME_COMPONENTS],
265 pub offset: [usize; NA_SIMPLE_VFRAME_COMPONENTS],
266 pub components: usize,
267 pub data: &'a mut Vec<T>,
270 impl<'a, T:Copy> NASimpleVideoFrame<'a, T> {
271 pub fn from_video_buf(vbuf: &'a mut NAVideoBuffer<T>) -> Option<Self> {
272 let vinfo = vbuf.get_info();
273 let components = vinfo.format.components as usize;
274 if components > NA_SIMPLE_VFRAME_COMPONENTS {
277 let mut w: [usize; NA_SIMPLE_VFRAME_COMPONENTS] = [0; NA_SIMPLE_VFRAME_COMPONENTS];
278 let mut h: [usize; NA_SIMPLE_VFRAME_COMPONENTS] = [0; NA_SIMPLE_VFRAME_COMPONENTS];
279 let mut s: [usize; NA_SIMPLE_VFRAME_COMPONENTS] = [0; NA_SIMPLE_VFRAME_COMPONENTS];
280 let mut o: [usize; NA_SIMPLE_VFRAME_COMPONENTS] = [0; NA_SIMPLE_VFRAME_COMPONENTS];
281 for comp in 0..components {
282 let (width, height) = vbuf.get_dimensions(comp);
285 s[comp] = vbuf.get_stride(comp);
286 o[comp] = vbuf.get_offset(comp);
288 let flip = vinfo.flipped;
289 Some(NASimpleVideoFrame {
296 data: vbuf.data.as_mut().unwrap(),
301 #[derive(Debug,Clone,Copy,PartialEq)]
302 pub enum AllocatorError {
307 pub fn alloc_video_buffer(vinfo: NAVideoInfo, align: u8) -> Result<NABufferType, AllocatorError> {
308 let fmt = &vinfo.format;
309 let mut new_size: usize = 0;
310 let mut offs: Vec<usize> = Vec::new();
311 let mut strides: Vec<usize> = Vec::new();
313 for i in 0..fmt.get_num_comp() {
314 if fmt.get_chromaton(i) == None { return Err(AllocatorError::FormatError); }
317 let align_mod = ((1 << align) as usize) - 1;
318 let width = ((vinfo.width as usize) + align_mod) & !align_mod;
319 let height = ((vinfo.height as usize) + align_mod) & !align_mod;
320 let mut max_depth = 0;
321 let mut all_packed = true;
322 let mut all_bytealigned = true;
323 for i in 0..fmt.get_num_comp() {
324 let ochr = fmt.get_chromaton(i);
325 if let None = ochr { continue; }
326 let chr = ochr.unwrap();
327 if !chr.is_packed() {
329 } else if ((chr.get_shift() + chr.get_depth()) & 7) != 0 {
330 all_bytealigned = false;
332 max_depth = max(max_depth, chr.get_depth());
334 let unfit_elem_size = match fmt.get_elem_size() {
339 //todo semi-packed like NV12
340 if fmt.is_paletted() {
341 //todo various-sized palettes?
342 let stride = vinfo.get_format().get_chromaton(0).unwrap().get_linesize(width);
343 let pic_sz = stride.checked_mul(height);
344 if pic_sz == None { return Err(AllocatorError::TooLargeDimensions); }
345 let pal_size = 256 * (fmt.get_elem_size() as usize);
346 let new_size = pic_sz.unwrap().checked_add(pal_size);
347 if new_size == None { return Err(AllocatorError::TooLargeDimensions); }
349 offs.push(stride * height);
350 strides.push(stride);
351 let mut data: Vec<u8> = Vec::with_capacity(new_size.unwrap());
352 data.resize(new_size.unwrap(), 0);
353 let buf: NAVideoBuffer<u8> = NAVideoBuffer { data: NABufferRef::new(data), info: vinfo, offs: offs, strides: strides };
354 Ok(NABufferType::Video(buf))
355 } else if !all_packed {
356 for i in 0..fmt.get_num_comp() {
357 let ochr = fmt.get_chromaton(i);
358 if let None = ochr { continue; }
359 let chr = ochr.unwrap();
360 if !vinfo.is_flipped() {
361 offs.push(new_size as usize);
363 let stride = chr.get_linesize(width);
364 let cur_h = chr.get_height(height);
365 let cur_sz = stride.checked_mul(cur_h);
366 if cur_sz == None { return Err(AllocatorError::TooLargeDimensions); }
367 let new_sz = new_size.checked_add(cur_sz.unwrap());
368 if new_sz == None { return Err(AllocatorError::TooLargeDimensions); }
369 new_size = new_sz.unwrap();
370 if vinfo.is_flipped() {
371 offs.push(new_size as usize);
373 strides.push(stride);
376 let mut data: Vec<u8> = Vec::with_capacity(new_size);
377 data.resize(new_size, 0);
378 let buf: NAVideoBuffer<u8> = NAVideoBuffer { data: NABufferRef::new(data), info: vinfo, offs: offs, strides: strides };
379 Ok(NABufferType::Video(buf))
380 } else if max_depth <= 16 {
381 let mut data: Vec<u16> = Vec::with_capacity(new_size);
382 data.resize(new_size, 0);
383 let buf: NAVideoBuffer<u16> = NAVideoBuffer { data: NABufferRef::new(data), info: vinfo, offs: offs, strides: strides };
384 Ok(NABufferType::Video16(buf))
386 let mut data: Vec<u32> = Vec::with_capacity(new_size);
387 data.resize(new_size, 0);
388 let buf: NAVideoBuffer<u32> = NAVideoBuffer { data: NABufferRef::new(data), info: vinfo, offs: offs, strides: strides };
389 Ok(NABufferType::Video32(buf))
391 } else if all_bytealigned || unfit_elem_size {
392 let elem_sz = fmt.get_elem_size();
393 let line_sz = width.checked_mul(elem_sz as usize);
394 if line_sz == None { return Err(AllocatorError::TooLargeDimensions); }
395 let new_sz = line_sz.unwrap().checked_mul(height);
396 if new_sz == None { return Err(AllocatorError::TooLargeDimensions); }
397 new_size = new_sz.unwrap();
398 let mut data: Vec<u8> = Vec::with_capacity(new_size);
399 data.resize(new_size, 0);
400 strides.push(line_sz.unwrap());
401 let buf: NAVideoBuffer<u8> = NAVideoBuffer { data: NABufferRef::new(data), info: vinfo, offs: offs, strides: strides };
402 Ok(NABufferType::VideoPacked(buf))
404 let elem_sz = fmt.get_elem_size();
405 let new_sz = width.checked_mul(height);
406 if new_sz == None { return Err(AllocatorError::TooLargeDimensions); }
407 new_size = new_sz.unwrap();
410 let mut data: Vec<u16> = Vec::with_capacity(new_size);
411 data.resize(new_size, 0);
413 let buf: NAVideoBuffer<u16> = NAVideoBuffer { data: NABufferRef::new(data), info: vinfo, offs: offs, strides: strides };
414 Ok(NABufferType::Video16(buf))
417 let mut data: Vec<u32> = Vec::with_capacity(new_size);
418 data.resize(new_size, 0);
420 let buf: NAVideoBuffer<u32> = NAVideoBuffer { data: NABufferRef::new(data), info: vinfo, offs: offs, strides: strides };
421 Ok(NABufferType::Video32(buf))
428 pub fn alloc_audio_buffer(ainfo: NAAudioInfo, nsamples: usize, chmap: NAChannelMap) -> Result<NABufferType, AllocatorError> {
429 let mut offs: Vec<usize> = Vec::new();
430 if ainfo.format.is_planar() || (ainfo.channels == 1 && (ainfo.format.get_bits() % 8) == 0) {
431 let len = nsamples.checked_mul(ainfo.channels as usize);
432 if len == None { return Err(AllocatorError::TooLargeDimensions); }
433 let length = len.unwrap();
434 for i in 0..ainfo.channels {
435 offs.push((i as usize) * nsamples);
437 if ainfo.format.is_float() {
438 if ainfo.format.get_bits() == 32 {
439 let mut data: Vec<f32> = Vec::with_capacity(length);
440 data.resize(length, 0.0);
441 let buf: NAAudioBuffer<f32> = NAAudioBuffer { data: NABufferRef::new(data), info: ainfo, offs: offs, chmap: chmap, len: nsamples };
442 Ok(NABufferType::AudioF32(buf))
444 Err(AllocatorError::TooLargeDimensions)
447 if ainfo.format.get_bits() == 8 && !ainfo.format.is_signed() {
448 let mut data: Vec<u8> = Vec::with_capacity(length);
449 data.resize(length, 0);
450 let buf: NAAudioBuffer<u8> = NAAudioBuffer { data: NABufferRef::new(data), info: ainfo, offs: offs, chmap: chmap, len: nsamples };
451 Ok(NABufferType::AudioU8(buf))
452 } else if ainfo.format.get_bits() == 16 && ainfo.format.is_signed() {
453 let mut data: Vec<i16> = Vec::with_capacity(length);
454 data.resize(length, 0);
455 let buf: NAAudioBuffer<i16> = NAAudioBuffer { data: NABufferRef::new(data), info: ainfo, offs: offs, chmap: chmap, len: nsamples };
456 Ok(NABufferType::AudioI16(buf))
458 Err(AllocatorError::TooLargeDimensions)
462 let len = nsamples.checked_mul(ainfo.channels as usize);
463 if len == None { return Err(AllocatorError::TooLargeDimensions); }
464 let length = ainfo.format.get_audio_size(len.unwrap() as u64);
465 let mut data: Vec<u8> = Vec::with_capacity(length);
466 data.resize(length, 0);
467 let buf: NAAudioBuffer<u8> = NAAudioBuffer { data: NABufferRef::new(data), info: ainfo, offs: offs, chmap: chmap, len: nsamples };
468 Ok(NABufferType::AudioPacked(buf))
472 pub fn alloc_data_buffer(size: usize) -> Result<NABufferType, AllocatorError> {
473 let mut data: Vec<u8> = Vec::with_capacity(size);
474 data.resize(size, 0);
475 let buf: NABufferRef<Vec<u8>> = NABufferRef::new(data);
476 Ok(NABufferType::Data(buf))
479 pub fn copy_buffer(buf: NABufferType) -> NABufferType {
483 pub struct NABufferPool {
484 pool: Vec<NABufferRef<NABufferType>>,
489 pub fn new(max_len: usize) -> Self {
491 pool: Vec::with_capacity(max_len),
495 pub fn prealloc_video(&mut self, vinfo: NAVideoInfo, align: u8) -> Result<(), AllocatorError> {
496 let nbufs = self.max_len - self.pool.len();
498 let buf = alloc_video_buffer(vinfo.clone(), align)?;
499 self.pool.push(NABufferRef::new(buf));
503 pub fn prealloc_audio(&mut self, ainfo: NAAudioInfo, nsamples: usize, chmap: NAChannelMap) -> Result<(), AllocatorError> {
504 let nbufs = self.max_len - self.pool.len();
506 let buf = alloc_audio_buffer(ainfo.clone(), nsamples, chmap.clone())?;
507 self.pool.push(NABufferRef::new(buf));
511 pub fn add(&mut self, buf: NABufferType) -> bool {
512 if self.pool.len() < self.max_len {
513 self.pool.push(NABufferRef::new(buf));
519 pub fn get_free(&mut self) -> Option<NABufferRef<NABufferType>> {
520 for e in self.pool.iter() {
521 if e.get_num_refs() == 1 {
522 return Some(e.clone());
531 pub struct NACodecInfo {
533 properties: NACodecTypeInfo,
534 extradata: Option<Rc<Vec<u8>>>,
538 pub fn new(name: &'static str, p: NACodecTypeInfo, edata: Option<Vec<u8>>) -> Self {
539 let extradata = match edata {
541 Some(vec) => Some(Rc::new(vec)),
543 NACodecInfo { name: name, properties: p, extradata: extradata }
545 pub fn new_ref(name: &'static str, p: NACodecTypeInfo, edata: Option<Rc<Vec<u8>>>) -> Self {
546 NACodecInfo { name: name, properties: p, extradata: edata }
548 pub fn get_properties(&self) -> NACodecTypeInfo { self.properties }
549 pub fn get_extradata(&self) -> Option<Rc<Vec<u8>>> {
550 if let Some(ref vec) = self.extradata { return Some(vec.clone()); }
553 pub fn get_name(&self) -> &'static str { self.name }
554 pub fn is_video(&self) -> bool {
555 if let NACodecTypeInfo::Video(_) = self.properties { return true; }
558 pub fn is_audio(&self) -> bool {
559 if let NACodecTypeInfo::Audio(_) = self.properties { return true; }
562 pub fn new_dummy() -> Rc<Self> {
563 Rc::new(DUMMY_CODEC_INFO)
565 pub fn replace_info(&self, p: NACodecTypeInfo) -> Rc<Self> {
566 Rc::new(NACodecInfo { name: self.name, properties: p, extradata: self.extradata.clone() })
570 impl Default for NACodecInfo {
571 fn default() -> Self { DUMMY_CODEC_INFO }
574 impl fmt::Display for NACodecInfo {
575 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
576 let edata = match self.extradata.clone() {
577 None => format!("no extradata"),
578 Some(v) => format!("{} byte(s) of extradata", v.len()),
580 write!(f, "{}: {} {}", self.name, self.properties, edata)
584 pub const DUMMY_CODEC_INFO: NACodecInfo = NACodecInfo {
586 properties: NACodecTypeInfo::None,
589 #[derive(Debug,Clone)]
598 #[derive(Debug,Clone,Copy,PartialEq)]
608 impl fmt::Display for FrameType {
609 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
611 FrameType::I => write!(f, "I"),
612 FrameType::P => write!(f, "P"),
613 FrameType::B => write!(f, "B"),
614 FrameType::Skip => write!(f, "skip"),
615 FrameType::Other => write!(f, "x"),
620 #[derive(Debug,Clone,Copy)]
621 pub struct NATimeInfo {
624 duration: Option<u64>,
630 pub fn new(pts: Option<u64>, dts: Option<u64>, duration: Option<u64>, tb_num: u32, tb_den: u32) -> Self {
631 NATimeInfo { pts: pts, dts: dts, duration: duration, tb_num: tb_num, tb_den: tb_den }
633 pub fn get_pts(&self) -> Option<u64> { self.pts }
634 pub fn get_dts(&self) -> Option<u64> { self.dts }
635 pub fn get_duration(&self) -> Option<u64> { self.duration }
636 pub fn set_pts(&mut self, pts: Option<u64>) { self.pts = pts; }
637 pub fn set_dts(&mut self, dts: Option<u64>) { self.dts = dts; }
638 pub fn set_duration(&mut self, dur: Option<u64>) { self.duration = dur; }
645 buffer: NABufferType,
646 info: Rc<NACodecInfo>,
649 options: HashMap<String, NAValue>,
652 pub type NAFrameRef = Rc<RefCell<NAFrame>>;
654 fn get_plane_size(info: &NAVideoInfo, idx: usize) -> (usize, usize) {
655 let chromaton = info.get_format().get_chromaton(idx);
656 if let None = chromaton { return (0, 0); }
657 let (hs, vs) = chromaton.unwrap().get_subsampling();
658 let w = (info.get_width() + ((1 << hs) - 1)) >> hs;
659 let h = (info.get_height() + ((1 << vs) - 1)) >> vs;
664 pub fn new(ts: NATimeInfo,
667 info: Rc<NACodecInfo>,
668 options: HashMap<String, NAValue>,
669 buffer: NABufferType) -> Self {
670 NAFrame { ts: ts, buffer: buffer, info: info, ftype: ftype, key: keyframe, options: options }
672 pub fn get_info(&self) -> Rc<NACodecInfo> { self.info.clone() }
673 pub fn get_frame_type(&self) -> FrameType { self.ftype }
674 pub fn is_keyframe(&self) -> bool { self.key }
675 pub fn set_frame_type(&mut self, ftype: FrameType) { self.ftype = ftype; }
676 pub fn set_keyframe(&mut self, key: bool) { self.key = key; }
677 pub fn get_time_information(&self) -> NATimeInfo { self.ts }
678 pub fn get_pts(&self) -> Option<u64> { self.ts.get_pts() }
679 pub fn get_dts(&self) -> Option<u64> { self.ts.get_dts() }
680 pub fn get_duration(&self) -> Option<u64> { self.ts.get_duration() }
681 pub fn set_pts(&mut self, pts: Option<u64>) { self.ts.set_pts(pts); }
682 pub fn set_dts(&mut self, dts: Option<u64>) { self.ts.set_dts(dts); }
683 pub fn set_duration(&mut self, dur: Option<u64>) { self.ts.set_duration(dur); }
685 pub fn get_buffer(&self) -> NABufferType { self.buffer.clone() }
688 impl fmt::Display for NAFrame {
689 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
690 let mut foo = format!("frame type {}", self.ftype);
691 if let Some(pts) = self.ts.pts { foo = format!("{} pts {}", foo, pts); }
692 if let Some(dts) = self.ts.dts { foo = format!("{} dts {}", foo, dts); }
693 if let Some(dur) = self.ts.duration { foo = format!("{} duration {}", foo, dur); }
694 if self.key { foo = format!("{} kf", foo); }
695 write!(f, "[{}]", foo)
699 /// Possible stream types.
700 #[derive(Debug,Clone,Copy)]
702 pub enum StreamType {
709 /// any data stream (or might be an unrecognized audio/video stream)
711 /// nonexistent stream
715 impl fmt::Display for StreamType {
716 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
718 StreamType::Video => write!(f, "Video"),
719 StreamType::Audio => write!(f, "Audio"),
720 StreamType::Subtitles => write!(f, "Subtitles"),
721 StreamType::Data => write!(f, "Data"),
722 StreamType::None => write!(f, "-"),
729 pub struct NAStream {
730 media_type: StreamType,
733 info: Rc<NACodecInfo>,
738 pub fn reduce_timebase(tb_num: u32, tb_den: u32) -> (u32, u32) {
739 if tb_num == 0 { return (tb_num, tb_den); }
740 if (tb_den % tb_num) == 0 { return (1, tb_den / tb_num); }
747 else if b > a { b -= a; }
750 (tb_num / a, tb_den / a)
754 pub fn new(mt: StreamType, id: u32, info: NACodecInfo, tb_num: u32, tb_den: u32) -> Self {
755 let (n, d) = reduce_timebase(tb_num, tb_den);
756 NAStream { media_type: mt, id: id, num: 0, info: Rc::new(info), tb_num: n, tb_den: d }
758 pub fn get_id(&self) -> u32 { self.id }
759 pub fn get_num(&self) -> usize { self.num }
760 pub fn set_num(&mut self, num: usize) { self.num = num; }
761 pub fn get_info(&self) -> Rc<NACodecInfo> { self.info.clone() }
762 pub fn get_timebase(&self) -> (u32, u32) { (self.tb_num, self.tb_den) }
763 pub fn set_timebase(&mut self, tb_num: u32, tb_den: u32) {
764 let (n, d) = reduce_timebase(tb_num, tb_den);
770 impl fmt::Display for NAStream {
771 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
772 write!(f, "({}#{} @ {}/{} - {})", self.media_type, self.id, self.tb_num, self.tb_den, self.info.get_properties())
777 pub struct NAPacket {
778 stream: Rc<NAStream>,
780 buffer: NABufferRef<Vec<u8>>,
782 // options: HashMap<String, NAValue<'a>>,
786 pub fn new(str: Rc<NAStream>, ts: NATimeInfo, kf: bool, vec: Vec<u8>) -> Self {
787 // let mut vec: Vec<u8> = Vec::new();
788 // vec.resize(size, 0);
789 NAPacket { stream: str, ts: ts, keyframe: kf, buffer: NABufferRef::new(vec) }
791 pub fn get_stream(&self) -> Rc<NAStream> { self.stream.clone() }
792 pub fn get_time_information(&self) -> NATimeInfo { self.ts }
793 pub fn get_pts(&self) -> Option<u64> { self.ts.get_pts() }
794 pub fn get_dts(&self) -> Option<u64> { self.ts.get_dts() }
795 pub fn get_duration(&self) -> Option<u64> { self.ts.get_duration() }
796 pub fn is_keyframe(&self) -> bool { self.keyframe }
797 pub fn get_buffer(&self) -> NABufferRef<Vec<u8>> { self.buffer.clone() }
800 impl Drop for NAPacket {
801 fn drop(&mut self) {}
804 impl fmt::Display for NAPacket {
805 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
806 let mut foo = format!("[pkt for {} size {}", self.stream, self.buffer.len());
807 if let Some(pts) = self.ts.pts { foo = format!("{} pts {}", foo, pts); }
808 if let Some(dts) = self.ts.dts { foo = format!("{} dts {}", foo, dts); }
809 if let Some(dur) = self.ts.duration { foo = format!("{} duration {}", foo, dur); }
810 if self.keyframe { foo = format!("{} kf", foo); }
816 pub trait FrameFromPacket {
817 fn new_from_pkt(pkt: &NAPacket, info: Rc<NACodecInfo>, buf: NABufferType) -> NAFrame;
818 fn fill_timestamps(&mut self, pkt: &NAPacket);
821 impl FrameFromPacket for NAFrame {
822 fn new_from_pkt(pkt: &NAPacket, info: Rc<NACodecInfo>, buf: NABufferType) -> NAFrame {
823 NAFrame::new(pkt.ts, FrameType::Other, pkt.keyframe, info, HashMap::new(), buf)
825 fn fill_timestamps(&mut self, pkt: &NAPacket) {
826 self.ts = pkt.get_time_information();