2 use std::collections::HashMap;
7 pub use crate::formats::*;
8 pub use crate::refs::*;
11 #[derive(Clone,Copy,PartialEq)]
12 pub struct NAAudioInfo {
20 pub fn new(sr: u32, ch: u8, fmt: NASoniton, bl: usize) -> Self {
21 NAAudioInfo { sample_rate: sr, channels: ch, format: fmt, block_len: bl }
23 pub fn get_sample_rate(&self) -> u32 { self.sample_rate }
24 pub fn get_channels(&self) -> u8 { self.channels }
25 pub fn get_format(&self) -> NASoniton { self.format }
26 pub fn get_block_len(&self) -> usize { self.block_len }
29 impl fmt::Display for NAAudioInfo {
30 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
31 write!(f, "{} Hz, {} ch", self.sample_rate, self.channels)
36 #[derive(Clone,Copy,PartialEq)]
37 pub struct NAVideoInfo {
41 format: NAPixelFormaton,
45 pub fn new(w: usize, h: usize, flip: bool, fmt: NAPixelFormaton) -> Self {
46 NAVideoInfo { width: w, height: h, flipped: flip, format: fmt }
48 pub fn get_width(&self) -> usize { self.width as usize }
49 pub fn get_height(&self) -> usize { self.height as usize }
50 pub fn is_flipped(&self) -> bool { self.flipped }
51 pub fn get_format(&self) -> NAPixelFormaton { self.format }
52 pub fn set_width(&mut self, w: usize) { self.width = w; }
53 pub fn set_height(&mut self, h: usize) { self.height = h; }
56 impl fmt::Display for NAVideoInfo {
57 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
58 write!(f, "{}x{}", self.width, self.height)
62 #[derive(Clone,Copy,PartialEq)]
63 pub enum NACodecTypeInfo {
69 impl NACodecTypeInfo {
70 pub fn get_video_info(&self) -> Option<NAVideoInfo> {
72 NACodecTypeInfo::Video(vinfo) => Some(vinfo),
76 pub fn get_audio_info(&self) -> Option<NAAudioInfo> {
78 NACodecTypeInfo::Audio(ainfo) => Some(ainfo),
82 pub fn is_video(&self) -> bool {
84 NACodecTypeInfo::Video(_) => true,
88 pub fn is_audio(&self) -> bool {
90 NACodecTypeInfo::Audio(_) => true,
96 impl fmt::Display for NACodecTypeInfo {
97 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
98 let ret = match *self {
99 NACodecTypeInfo::None => format!(""),
100 NACodecTypeInfo::Audio(fmt) => format!("{}", fmt),
101 NACodecTypeInfo::Video(fmt) => format!("{}", fmt),
108 pub struct NAVideoBuffer<T> {
110 data: NABufferRef<Vec<T>>,
115 impl<T: Clone> NAVideoBuffer<T> {
116 pub fn get_offset(&self, idx: usize) -> usize {
117 if idx >= self.offs.len() { 0 }
118 else { self.offs[idx] }
120 pub fn get_info(&self) -> NAVideoInfo { self.info }
121 pub fn get_data(&self) -> &Vec<T> { self.data.as_ref() }
122 pub fn get_data_mut(&mut self) -> Option<&mut Vec<T>> { self.data.as_mut() }
123 pub fn copy_buffer(&mut self) -> Self {
124 let mut data: Vec<T> = Vec::with_capacity(self.data.len());
125 data.clone_from(self.data.as_ref());
126 let mut offs: Vec<usize> = Vec::with_capacity(self.offs.len());
127 offs.clone_from(&self.offs);
128 let mut strides: Vec<usize> = Vec::with_capacity(self.strides.len());
129 strides.clone_from(&self.strides);
130 NAVideoBuffer { info: self.info, data: NABufferRef::new(data), offs: offs, strides: strides }
132 pub fn get_stride(&self, idx: usize) -> usize {
133 if idx >= self.strides.len() { return 0; }
136 pub fn get_dimensions(&self, idx: usize) -> (usize, usize) {
137 get_plane_size(&self.info, idx)
142 pub struct NAAudioBuffer<T> {
144 data: NABufferRef<Vec<T>>,
150 impl<T: Clone> NAAudioBuffer<T> {
151 pub fn get_offset(&self, idx: usize) -> usize {
152 if idx >= self.offs.len() { 0 }
153 else { self.offs[idx] }
155 pub fn get_info(&self) -> NAAudioInfo { self.info }
156 pub fn get_chmap(&self) -> NAChannelMap { self.chmap.clone() }
157 pub fn get_data(&self) -> &Vec<T> { self.data.as_ref() }
158 pub fn get_data_mut(&mut self) -> Option<&mut Vec<T>> { self.data.as_mut() }
159 pub fn copy_buffer(&mut self) -> Self {
160 let mut data: Vec<T> = Vec::with_capacity(self.data.len());
161 data.clone_from(self.data.as_ref());
162 let mut offs: Vec<usize> = Vec::with_capacity(self.offs.len());
163 offs.clone_from(&self.offs);
164 NAAudioBuffer { info: self.info, data: NABufferRef::new(data), offs: offs, chmap: self.get_chmap(), len: self.len }
166 pub fn get_length(&self) -> usize { self.len }
169 impl NAAudioBuffer<u8> {
170 pub fn new_from_buf(info: NAAudioInfo, data: NABufferRef<Vec<u8>>, chmap: NAChannelMap) -> Self {
171 let len = data.len();
172 NAAudioBuffer { info: info, data: data, chmap: chmap, offs: Vec::new(), len: len }
177 pub enum NABufferType {
178 Video (NAVideoBuffer<u8>),
179 Video16 (NAVideoBuffer<u16>),
180 Video32 (NAVideoBuffer<u32>),
181 VideoPacked(NAVideoBuffer<u8>),
182 AudioU8 (NAAudioBuffer<u8>),
183 AudioI16 (NAAudioBuffer<i16>),
184 AudioI32 (NAAudioBuffer<i32>),
185 AudioF32 (NAAudioBuffer<f32>),
186 AudioPacked(NAAudioBuffer<u8>),
187 Data (NABufferRef<Vec<u8>>),
192 pub fn get_offset(&self, idx: usize) -> usize {
194 NABufferType::Video(ref vb) => vb.get_offset(idx),
195 NABufferType::Video16(ref vb) => vb.get_offset(idx),
196 NABufferType::Video32(ref vb) => vb.get_offset(idx),
197 NABufferType::VideoPacked(ref vb) => vb.get_offset(idx),
198 NABufferType::AudioU8(ref ab) => ab.get_offset(idx),
199 NABufferType::AudioI16(ref ab) => ab.get_offset(idx),
200 NABufferType::AudioF32(ref ab) => ab.get_offset(idx),
201 NABufferType::AudioPacked(ref ab) => ab.get_offset(idx),
205 pub fn get_video_info(&self) -> Option<NAVideoInfo> {
207 NABufferType::Video(ref vb) => Some(vb.get_info()),
208 NABufferType::Video16(ref vb) => Some(vb.get_info()),
209 NABufferType::Video32(ref vb) => Some(vb.get_info()),
210 NABufferType::VideoPacked(ref vb) => Some(vb.get_info()),
214 pub fn get_vbuf(&self) -> Option<NAVideoBuffer<u8>> {
216 NABufferType::Video(ref vb) => Some(vb.clone()),
217 NABufferType::VideoPacked(ref vb) => Some(vb.clone()),
221 pub fn get_vbuf16(&self) -> Option<NAVideoBuffer<u16>> {
223 NABufferType::Video16(ref vb) => Some(vb.clone()),
227 pub fn get_vbuf32(&self) -> Option<NAVideoBuffer<u32>> {
229 NABufferType::Video32(ref vb) => Some(vb.clone()),
233 pub fn get_abuf_u8(&self) -> Option<NAAudioBuffer<u8>> {
235 NABufferType::AudioU8(ref ab) => Some(ab.clone()),
236 NABufferType::AudioPacked(ref ab) => Some(ab.clone()),
240 pub fn get_abuf_i16(&self) -> Option<NAAudioBuffer<i16>> {
242 NABufferType::AudioI16(ref ab) => Some(ab.clone()),
246 pub fn get_abuf_i32(&self) -> Option<NAAudioBuffer<i32>> {
248 NABufferType::AudioI32(ref ab) => Some(ab.clone()),
252 pub fn get_abuf_f32(&self) -> Option<NAAudioBuffer<f32>> {
254 NABufferType::AudioF32(ref ab) => Some(ab.clone()),
260 const NA_SIMPLE_VFRAME_COMPONENTS: usize = 4;
261 pub struct NASimpleVideoFrame<'a, T: Copy> {
262 pub width: [usize; NA_SIMPLE_VFRAME_COMPONENTS],
263 pub height: [usize; NA_SIMPLE_VFRAME_COMPONENTS],
265 pub stride: [usize; NA_SIMPLE_VFRAME_COMPONENTS],
266 pub offset: [usize; NA_SIMPLE_VFRAME_COMPONENTS],
267 pub components: usize,
268 pub data: &'a mut Vec<T>,
271 impl<'a, T:Copy> NASimpleVideoFrame<'a, T> {
272 pub fn from_video_buf(vbuf: &'a mut NAVideoBuffer<T>) -> Option<Self> {
273 let vinfo = vbuf.get_info();
274 let components = vinfo.format.components as usize;
275 if components > NA_SIMPLE_VFRAME_COMPONENTS {
278 let mut w: [usize; NA_SIMPLE_VFRAME_COMPONENTS] = [0; NA_SIMPLE_VFRAME_COMPONENTS];
279 let mut h: [usize; NA_SIMPLE_VFRAME_COMPONENTS] = [0; NA_SIMPLE_VFRAME_COMPONENTS];
280 let mut s: [usize; NA_SIMPLE_VFRAME_COMPONENTS] = [0; NA_SIMPLE_VFRAME_COMPONENTS];
281 let mut o: [usize; NA_SIMPLE_VFRAME_COMPONENTS] = [0; NA_SIMPLE_VFRAME_COMPONENTS];
282 for comp in 0..components {
283 let (width, height) = vbuf.get_dimensions(comp);
286 s[comp] = vbuf.get_stride(comp);
287 o[comp] = vbuf.get_offset(comp);
289 let flip = vinfo.flipped;
290 Some(NASimpleVideoFrame {
297 data: vbuf.data.as_mut().unwrap(),
302 #[derive(Debug,Clone,Copy,PartialEq)]
303 pub enum AllocatorError {
308 pub fn alloc_video_buffer(vinfo: NAVideoInfo, align: u8) -> Result<NABufferType, AllocatorError> {
309 let fmt = &vinfo.format;
310 let mut new_size: usize = 0;
311 let mut offs: Vec<usize> = Vec::new();
312 let mut strides: Vec<usize> = Vec::new();
314 for i in 0..fmt.get_num_comp() {
315 if fmt.get_chromaton(i) == None { return Err(AllocatorError::FormatError); }
318 let align_mod = ((1 << align) as usize) - 1;
319 let width = ((vinfo.width as usize) + align_mod) & !align_mod;
320 let height = ((vinfo.height as usize) + align_mod) & !align_mod;
321 let mut max_depth = 0;
322 let mut all_packed = true;
323 let mut all_bytealigned = true;
324 for i in 0..fmt.get_num_comp() {
325 let ochr = fmt.get_chromaton(i);
326 if let None = ochr { continue; }
327 let chr = ochr.unwrap();
328 if !chr.is_packed() {
330 } else if ((chr.get_shift() + chr.get_depth()) & 7) != 0 {
331 all_bytealigned = false;
333 max_depth = max(max_depth, chr.get_depth());
335 let unfit_elem_size = match fmt.get_elem_size() {
340 //todo semi-packed like NV12
341 if fmt.is_paletted() {
342 //todo various-sized palettes?
343 let stride = vinfo.get_format().get_chromaton(0).unwrap().get_linesize(width);
344 let pic_sz = stride.checked_mul(height);
345 if pic_sz == None { return Err(AllocatorError::TooLargeDimensions); }
346 let pal_size = 256 * (fmt.get_elem_size() as usize);
347 let new_size = pic_sz.unwrap().checked_add(pal_size);
348 if new_size == None { return Err(AllocatorError::TooLargeDimensions); }
350 offs.push(stride * height);
351 strides.push(stride);
352 let mut data: Vec<u8> = Vec::with_capacity(new_size.unwrap());
353 data.resize(new_size.unwrap(), 0);
354 let buf: NAVideoBuffer<u8> = NAVideoBuffer { data: NABufferRef::new(data), info: vinfo, offs: offs, strides: strides };
355 Ok(NABufferType::Video(buf))
356 } else if !all_packed {
357 for i in 0..fmt.get_num_comp() {
358 let ochr = fmt.get_chromaton(i);
359 if let None = ochr { continue; }
360 let chr = ochr.unwrap();
361 if !vinfo.is_flipped() {
362 offs.push(new_size as usize);
364 let stride = chr.get_linesize(width);
365 let cur_h = chr.get_height(height);
366 let cur_sz = stride.checked_mul(cur_h);
367 if cur_sz == None { return Err(AllocatorError::TooLargeDimensions); }
368 let new_sz = new_size.checked_add(cur_sz.unwrap());
369 if new_sz == None { return Err(AllocatorError::TooLargeDimensions); }
370 new_size = new_sz.unwrap();
371 if vinfo.is_flipped() {
372 offs.push(new_size as usize);
374 strides.push(stride);
377 let mut data: Vec<u8> = Vec::with_capacity(new_size);
378 data.resize(new_size, 0);
379 let buf: NAVideoBuffer<u8> = NAVideoBuffer { data: NABufferRef::new(data), info: vinfo, offs: offs, strides: strides };
380 Ok(NABufferType::Video(buf))
381 } else if max_depth <= 16 {
382 let mut data: Vec<u16> = Vec::with_capacity(new_size);
383 data.resize(new_size, 0);
384 let buf: NAVideoBuffer<u16> = NAVideoBuffer { data: NABufferRef::new(data), info: vinfo, offs: offs, strides: strides };
385 Ok(NABufferType::Video16(buf))
387 let mut data: Vec<u32> = Vec::with_capacity(new_size);
388 data.resize(new_size, 0);
389 let buf: NAVideoBuffer<u32> = NAVideoBuffer { data: NABufferRef::new(data), info: vinfo, offs: offs, strides: strides };
390 Ok(NABufferType::Video32(buf))
392 } else if all_bytealigned || unfit_elem_size {
393 let elem_sz = fmt.get_elem_size();
394 let line_sz = width.checked_mul(elem_sz as usize);
395 if line_sz == None { return Err(AllocatorError::TooLargeDimensions); }
396 let new_sz = line_sz.unwrap().checked_mul(height);
397 if new_sz == None { return Err(AllocatorError::TooLargeDimensions); }
398 new_size = new_sz.unwrap();
399 let mut data: Vec<u8> = Vec::with_capacity(new_size);
400 data.resize(new_size, 0);
401 strides.push(line_sz.unwrap());
402 let buf: NAVideoBuffer<u8> = NAVideoBuffer { data: NABufferRef::new(data), info: vinfo, offs: offs, strides: strides };
403 Ok(NABufferType::VideoPacked(buf))
405 let elem_sz = fmt.get_elem_size();
406 let new_sz = width.checked_mul(height);
407 if new_sz == None { return Err(AllocatorError::TooLargeDimensions); }
408 new_size = new_sz.unwrap();
411 let mut data: Vec<u16> = Vec::with_capacity(new_size);
412 data.resize(new_size, 0);
414 let buf: NAVideoBuffer<u16> = NAVideoBuffer { data: NABufferRef::new(data), info: vinfo, offs: offs, strides: strides };
415 Ok(NABufferType::Video16(buf))
418 let mut data: Vec<u32> = Vec::with_capacity(new_size);
419 data.resize(new_size, 0);
421 let buf: NAVideoBuffer<u32> = NAVideoBuffer { data: NABufferRef::new(data), info: vinfo, offs: offs, strides: strides };
422 Ok(NABufferType::Video32(buf))
429 pub fn alloc_audio_buffer(ainfo: NAAudioInfo, nsamples: usize, chmap: NAChannelMap) -> Result<NABufferType, AllocatorError> {
430 let mut offs: Vec<usize> = Vec::new();
431 if ainfo.format.is_planar() || (ainfo.channels == 1 && (ainfo.format.get_bits() % 8) == 0) {
432 let len = nsamples.checked_mul(ainfo.channels as usize);
433 if len == None { return Err(AllocatorError::TooLargeDimensions); }
434 let length = len.unwrap();
435 for i in 0..ainfo.channels {
436 offs.push((i as usize) * nsamples);
438 if ainfo.format.is_float() {
439 if ainfo.format.get_bits() == 32 {
440 let mut data: Vec<f32> = Vec::with_capacity(length);
441 data.resize(length, 0.0);
442 let buf: NAAudioBuffer<f32> = NAAudioBuffer { data: NABufferRef::new(data), info: ainfo, offs: offs, chmap: chmap, len: nsamples };
443 Ok(NABufferType::AudioF32(buf))
445 Err(AllocatorError::TooLargeDimensions)
448 if ainfo.format.get_bits() == 8 && !ainfo.format.is_signed() {
449 let mut data: Vec<u8> = Vec::with_capacity(length);
450 data.resize(length, 0);
451 let buf: NAAudioBuffer<u8> = NAAudioBuffer { data: NABufferRef::new(data), info: ainfo, offs: offs, chmap: chmap, len: nsamples };
452 Ok(NABufferType::AudioU8(buf))
453 } else if ainfo.format.get_bits() == 16 && ainfo.format.is_signed() {
454 let mut data: Vec<i16> = Vec::with_capacity(length);
455 data.resize(length, 0);
456 let buf: NAAudioBuffer<i16> = NAAudioBuffer { data: NABufferRef::new(data), info: ainfo, offs: offs, chmap: chmap, len: nsamples };
457 Ok(NABufferType::AudioI16(buf))
459 Err(AllocatorError::TooLargeDimensions)
463 let len = nsamples.checked_mul(ainfo.channels as usize);
464 if len == None { return Err(AllocatorError::TooLargeDimensions); }
465 let length = ainfo.format.get_audio_size(len.unwrap() as u64);
466 let mut data: Vec<u8> = Vec::with_capacity(length);
467 data.resize(length, 0);
468 let buf: NAAudioBuffer<u8> = NAAudioBuffer { data: NABufferRef::new(data), info: ainfo, offs: offs, chmap: chmap, len: nsamples };
469 Ok(NABufferType::AudioPacked(buf))
473 pub fn alloc_data_buffer(size: usize) -> Result<NABufferType, AllocatorError> {
474 let mut data: Vec<u8> = Vec::with_capacity(size);
475 data.resize(size, 0);
476 let buf: NABufferRef<Vec<u8>> = NABufferRef::new(data);
477 Ok(NABufferType::Data(buf))
480 pub fn copy_buffer(buf: NABufferType) -> NABufferType {
484 pub struct NABufferPool {
485 pool: Vec<NABufferRef<NABufferType>>,
490 pub fn new(max_len: usize) -> Self {
492 pool: Vec::with_capacity(max_len),
496 pub fn prealloc_video(&mut self, vinfo: NAVideoInfo, align: u8) -> Result<(), AllocatorError> {
497 let nbufs = self.max_len - self.pool.len();
499 let buf = alloc_video_buffer(vinfo.clone(), align)?;
500 self.pool.push(NABufferRef::new(buf));
504 pub fn prealloc_audio(&mut self, ainfo: NAAudioInfo, nsamples: usize, chmap: NAChannelMap) -> Result<(), AllocatorError> {
505 let nbufs = self.max_len - self.pool.len();
507 let buf = alloc_audio_buffer(ainfo.clone(), nsamples, chmap.clone())?;
508 self.pool.push(NABufferRef::new(buf));
512 pub fn add(&mut self, buf: NABufferType) -> bool {
513 if self.pool.len() < self.max_len {
514 self.pool.push(NABufferRef::new(buf));
520 pub fn get_free(&mut self) -> Option<NABufferRef<NABufferType>> {
521 for e in self.pool.iter() {
522 if e.get_num_refs() == 1 {
523 return Some(e.clone());
532 pub struct NACodecInfo {
534 properties: NACodecTypeInfo,
535 extradata: Option<Arc<Vec<u8>>>,
538 pub type NACodecInfoRef = Arc<NACodecInfo>;
541 pub fn new(name: &'static str, p: NACodecTypeInfo, edata: Option<Vec<u8>>) -> Self {
542 let extradata = match edata {
544 Some(vec) => Some(Arc::new(vec)),
546 NACodecInfo { name: name, properties: p, extradata: extradata }
548 pub fn new_ref(name: &'static str, p: NACodecTypeInfo, edata: Option<Arc<Vec<u8>>>) -> Self {
549 NACodecInfo { name: name, properties: p, extradata: edata }
551 pub fn into_ref(self) -> NACodecInfoRef { Arc::new(self) }
552 pub fn get_properties(&self) -> NACodecTypeInfo { self.properties }
553 pub fn get_extradata(&self) -> Option<Arc<Vec<u8>>> {
554 if let Some(ref vec) = self.extradata { return Some(vec.clone()); }
557 pub fn get_name(&self) -> &'static str { self.name }
558 pub fn is_video(&self) -> bool {
559 if let NACodecTypeInfo::Video(_) = self.properties { return true; }
562 pub fn is_audio(&self) -> bool {
563 if let NACodecTypeInfo::Audio(_) = self.properties { return true; }
566 pub fn new_dummy() -> Arc<Self> {
567 Arc::new(DUMMY_CODEC_INFO)
569 pub fn replace_info(&self, p: NACodecTypeInfo) -> Arc<Self> {
570 Arc::new(NACodecInfo { name: self.name, properties: p, extradata: self.extradata.clone() })
574 impl Default for NACodecInfo {
575 fn default() -> Self { DUMMY_CODEC_INFO }
578 impl fmt::Display for NACodecInfo {
579 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
580 let edata = match self.extradata.clone() {
581 None => format!("no extradata"),
582 Some(v) => format!("{} byte(s) of extradata", v.len()),
584 write!(f, "{}: {} {}", self.name, self.properties, edata)
588 pub const DUMMY_CODEC_INFO: NACodecInfo = NACodecInfo {
590 properties: NACodecTypeInfo::None,
593 #[derive(Debug,Clone)]
602 #[derive(Debug,Clone,Copy,PartialEq)]
612 impl fmt::Display for FrameType {
613 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
615 FrameType::I => write!(f, "I"),
616 FrameType::P => write!(f, "P"),
617 FrameType::B => write!(f, "B"),
618 FrameType::Skip => write!(f, "skip"),
619 FrameType::Other => write!(f, "x"),
624 #[derive(Debug,Clone,Copy)]
625 pub struct NATimeInfo {
628 duration: Option<u64>,
634 pub fn new(pts: Option<u64>, dts: Option<u64>, duration: Option<u64>, tb_num: u32, tb_den: u32) -> Self {
635 NATimeInfo { pts: pts, dts: dts, duration: duration, tb_num: tb_num, tb_den: tb_den }
637 pub fn get_pts(&self) -> Option<u64> { self.pts }
638 pub fn get_dts(&self) -> Option<u64> { self.dts }
639 pub fn get_duration(&self) -> Option<u64> { self.duration }
640 pub fn set_pts(&mut self, pts: Option<u64>) { self.pts = pts; }
641 pub fn set_dts(&mut self, dts: Option<u64>) { self.dts = dts; }
642 pub fn set_duration(&mut self, dur: Option<u64>) { self.duration = dur; }
649 buffer: NABufferType,
650 info: NACodecInfoRef,
653 options: HashMap<String, NAValue>,
656 pub type NAFrameRef = Rc<RefCell<NAFrame>>;
658 fn get_plane_size(info: &NAVideoInfo, idx: usize) -> (usize, usize) {
659 let chromaton = info.get_format().get_chromaton(idx);
660 if let None = chromaton { return (0, 0); }
661 let (hs, vs) = chromaton.unwrap().get_subsampling();
662 let w = (info.get_width() + ((1 << hs) - 1)) >> hs;
663 let h = (info.get_height() + ((1 << vs) - 1)) >> vs;
668 pub fn new(ts: NATimeInfo,
671 info: NACodecInfoRef,
672 options: HashMap<String, NAValue>,
673 buffer: NABufferType) -> Self {
674 NAFrame { ts: ts, buffer: buffer, info: info, ftype: ftype, key: keyframe, options: options }
676 pub fn get_info(&self) -> NACodecInfoRef { self.info.clone() }
677 pub fn get_frame_type(&self) -> FrameType { self.ftype }
678 pub fn is_keyframe(&self) -> bool { self.key }
679 pub fn set_frame_type(&mut self, ftype: FrameType) { self.ftype = ftype; }
680 pub fn set_keyframe(&mut self, key: bool) { self.key = key; }
681 pub fn get_time_information(&self) -> NATimeInfo { self.ts }
682 pub fn get_pts(&self) -> Option<u64> { self.ts.get_pts() }
683 pub fn get_dts(&self) -> Option<u64> { self.ts.get_dts() }
684 pub fn get_duration(&self) -> Option<u64> { self.ts.get_duration() }
685 pub fn set_pts(&mut self, pts: Option<u64>) { self.ts.set_pts(pts); }
686 pub fn set_dts(&mut self, dts: Option<u64>) { self.ts.set_dts(dts); }
687 pub fn set_duration(&mut self, dur: Option<u64>) { self.ts.set_duration(dur); }
689 pub fn get_buffer(&self) -> NABufferType { self.buffer.clone() }
692 impl fmt::Display for NAFrame {
693 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
694 let mut foo = format!("frame type {}", self.ftype);
695 if let Some(pts) = self.ts.pts { foo = format!("{} pts {}", foo, pts); }
696 if let Some(dts) = self.ts.dts { foo = format!("{} dts {}", foo, dts); }
697 if let Some(dur) = self.ts.duration { foo = format!("{} duration {}", foo, dur); }
698 if self.key { foo = format!("{} kf", foo); }
699 write!(f, "[{}]", foo)
703 /// Possible stream types.
704 #[derive(Debug,Clone,Copy)]
706 pub enum StreamType {
713 /// any data stream (or might be an unrecognized audio/video stream)
715 /// nonexistent stream
719 impl fmt::Display for StreamType {
720 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
722 StreamType::Video => write!(f, "Video"),
723 StreamType::Audio => write!(f, "Audio"),
724 StreamType::Subtitles => write!(f, "Subtitles"),
725 StreamType::Data => write!(f, "Data"),
726 StreamType::None => write!(f, "-"),
733 pub struct NAStream {
734 media_type: StreamType,
737 info: NACodecInfoRef,
742 pub fn reduce_timebase(tb_num: u32, tb_den: u32) -> (u32, u32) {
743 if tb_num == 0 { return (tb_num, tb_den); }
744 if (tb_den % tb_num) == 0 { return (1, tb_den / tb_num); }
751 else if b > a { b -= a; }
754 (tb_num / a, tb_den / a)
758 pub fn new(mt: StreamType, id: u32, info: NACodecInfo, tb_num: u32, tb_den: u32) -> Self {
759 let (n, d) = reduce_timebase(tb_num, tb_den);
760 NAStream { media_type: mt, id: id, num: 0, info: info.into_ref(), tb_num: n, tb_den: d }
762 pub fn get_id(&self) -> u32 { self.id }
763 pub fn get_num(&self) -> usize { self.num }
764 pub fn set_num(&mut self, num: usize) { self.num = num; }
765 pub fn get_info(&self) -> NACodecInfoRef { self.info.clone() }
766 pub fn get_timebase(&self) -> (u32, u32) { (self.tb_num, self.tb_den) }
767 pub fn set_timebase(&mut self, tb_num: u32, tb_den: u32) {
768 let (n, d) = reduce_timebase(tb_num, tb_den);
774 impl fmt::Display for NAStream {
775 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
776 write!(f, "({}#{} @ {}/{} - {})", self.media_type, self.id, self.tb_num, self.tb_den, self.info.get_properties())
781 pub struct NAPacket {
782 stream: Rc<NAStream>,
784 buffer: NABufferRef<Vec<u8>>,
786 // options: HashMap<String, NAValue<'a>>,
790 pub fn new(str: Rc<NAStream>, ts: NATimeInfo, kf: bool, vec: Vec<u8>) -> Self {
791 // let mut vec: Vec<u8> = Vec::new();
792 // vec.resize(size, 0);
793 NAPacket { stream: str, ts: ts, keyframe: kf, buffer: NABufferRef::new(vec) }
795 pub fn get_stream(&self) -> Rc<NAStream> { self.stream.clone() }
796 pub fn get_time_information(&self) -> NATimeInfo { self.ts }
797 pub fn get_pts(&self) -> Option<u64> { self.ts.get_pts() }
798 pub fn get_dts(&self) -> Option<u64> { self.ts.get_dts() }
799 pub fn get_duration(&self) -> Option<u64> { self.ts.get_duration() }
800 pub fn is_keyframe(&self) -> bool { self.keyframe }
801 pub fn get_buffer(&self) -> NABufferRef<Vec<u8>> { self.buffer.clone() }
804 impl Drop for NAPacket {
805 fn drop(&mut self) {}
808 impl fmt::Display for NAPacket {
809 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
810 let mut foo = format!("[pkt for {} size {}", self.stream, self.buffer.len());
811 if let Some(pts) = self.ts.pts { foo = format!("{} pts {}", foo, pts); }
812 if let Some(dts) = self.ts.dts { foo = format!("{} dts {}", foo, dts); }
813 if let Some(dur) = self.ts.duration { foo = format!("{} duration {}", foo, dur); }
814 if self.keyframe { foo = format!("{} kf", foo); }
820 pub trait FrameFromPacket {
821 fn new_from_pkt(pkt: &NAPacket, info: NACodecInfoRef, buf: NABufferType) -> NAFrame;
822 fn fill_timestamps(&mut self, pkt: &NAPacket);
825 impl FrameFromPacket for NAFrame {
826 fn new_from_pkt(pkt: &NAPacket, info: NACodecInfoRef, buf: NABufferType) -> NAFrame {
827 NAFrame::new(pkt.ts, FrameType::Other, pkt.keyframe, info, HashMap::new(), buf)
829 fn fill_timestamps(&mut self, pkt: &NAPacket) {
830 self.ts = pkt.get_time_information();