2 use std::collections::HashMap;
5 pub use crate::formats::*;
6 pub use crate::refs::*;
9 #[derive(Clone,Copy,PartialEq)]
10 pub struct 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 }
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 }
27 impl fmt::Display for NAAudioInfo {
28 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
29 write!(f, "{} Hz, {} ch", self.sample_rate, self.channels)
34 #[derive(Clone,Copy,PartialEq)]
35 pub struct NAVideoInfo {
39 format: NAPixelFormaton,
43 pub fn new(w: usize, h: usize, flip: bool, fmt: NAPixelFormaton) -> Self {
44 NAVideoInfo { width: w, height: h, flipped: flip, format: fmt }
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 }
50 pub fn set_width(&mut self, w: usize) { self.width = w; }
51 pub fn set_height(&mut self, h: usize) { self.height = h; }
54 impl fmt::Display for NAVideoInfo {
55 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
56 write!(f, "{}x{}", self.width, self.height)
60 #[derive(Clone,Copy,PartialEq)]
61 pub enum NACodecTypeInfo {
67 impl NACodecTypeInfo {
68 pub fn get_video_info(&self) -> Option<NAVideoInfo> {
70 NACodecTypeInfo::Video(vinfo) => Some(vinfo),
74 pub fn get_audio_info(&self) -> Option<NAAudioInfo> {
76 NACodecTypeInfo::Audio(ainfo) => Some(ainfo),
80 pub fn is_video(&self) -> bool {
82 NACodecTypeInfo::Video(_) => true,
86 pub fn is_audio(&self) -> bool {
88 NACodecTypeInfo::Audio(_) => true,
94 impl fmt::Display for NACodecTypeInfo {
95 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
96 let ret = match *self {
97 NACodecTypeInfo::None => format!(""),
98 NACodecTypeInfo::Audio(fmt) => format!("{}", fmt),
99 NACodecTypeInfo::Video(fmt) => format!("{}", fmt),
106 pub struct NAVideoBuffer<T> {
108 data: NABufferRef<Vec<T>>,
113 impl<T: Clone> NAVideoBuffer<T> {
114 pub fn get_offset(&self, idx: usize) -> usize {
115 if idx >= self.offs.len() { 0 }
116 else { self.offs[idx] }
118 pub fn get_info(&self) -> NAVideoInfo { self.info }
119 pub fn get_data(&self) -> &Vec<T> { self.data.as_ref() }
120 pub fn get_data_mut(&mut self) -> Option<&mut Vec<T>> { self.data.as_mut() }
121 pub fn copy_buffer(&mut self) -> Self {
122 let mut data: Vec<T> = Vec::with_capacity(self.data.len());
123 data.clone_from(self.data.as_ref());
124 let mut offs: Vec<usize> = Vec::with_capacity(self.offs.len());
125 offs.clone_from(&self.offs);
126 let mut strides: Vec<usize> = Vec::with_capacity(self.strides.len());
127 strides.clone_from(&self.strides);
128 NAVideoBuffer { info: self.info, data: NABufferRef::new(data), offs: offs, strides: strides }
130 pub fn get_stride(&self, idx: usize) -> usize {
131 if idx >= self.strides.len() { return 0; }
134 pub fn get_dimensions(&self, idx: usize) -> (usize, usize) {
135 get_plane_size(&self.info, idx)
137 pub fn into_ref(self) -> NABufferRef<Self> {
138 NABufferRef::new(self)
142 pub type NAVideoBufferRef<T> = NABufferRef<NAVideoBuffer<T>>;
145 pub struct NAAudioBuffer<T> {
147 data: NABufferRef<Vec<T>>,
153 impl<T: Clone> NAAudioBuffer<T> {
154 pub fn get_offset(&self, idx: usize) -> usize {
155 if idx >= self.offs.len() { 0 }
156 else { self.offs[idx] }
158 pub fn get_info(&self) -> NAAudioInfo { self.info }
159 pub fn get_chmap(&self) -> NAChannelMap { self.chmap.clone() }
160 pub fn get_data(&self) -> &Vec<T> { self.data.as_ref() }
161 pub fn get_data_mut(&mut self) -> Option<&mut Vec<T>> { self.data.as_mut() }
162 pub fn copy_buffer(&mut self) -> Self {
163 let mut data: Vec<T> = Vec::with_capacity(self.data.len());
164 data.clone_from(self.data.as_ref());
165 let mut offs: Vec<usize> = Vec::with_capacity(self.offs.len());
166 offs.clone_from(&self.offs);
167 NAAudioBuffer { info: self.info, data: NABufferRef::new(data), offs: offs, chmap: self.get_chmap(), len: self.len }
169 pub fn get_length(&self) -> usize { self.len }
172 impl NAAudioBuffer<u8> {
173 pub fn new_from_buf(info: NAAudioInfo, data: NABufferRef<Vec<u8>>, chmap: NAChannelMap) -> Self {
174 let len = data.len();
175 NAAudioBuffer { info: info, data: data, chmap: chmap, offs: Vec::new(), len: len }
180 pub enum NABufferType {
181 Video (NAVideoBufferRef<u8>),
182 Video16 (NAVideoBufferRef<u16>),
183 Video32 (NAVideoBufferRef<u32>),
184 VideoPacked(NAVideoBufferRef<u8>),
185 AudioU8 (NAAudioBuffer<u8>),
186 AudioI16 (NAAudioBuffer<i16>),
187 AudioI32 (NAAudioBuffer<i32>),
188 AudioF32 (NAAudioBuffer<f32>),
189 AudioPacked(NAAudioBuffer<u8>),
190 Data (NABufferRef<Vec<u8>>),
195 pub fn get_offset(&self, idx: usize) -> usize {
197 NABufferType::Video(ref vb) => vb.get_offset(idx),
198 NABufferType::Video16(ref vb) => vb.get_offset(idx),
199 NABufferType::Video32(ref vb) => vb.get_offset(idx),
200 NABufferType::VideoPacked(ref vb) => vb.get_offset(idx),
201 NABufferType::AudioU8(ref ab) => ab.get_offset(idx),
202 NABufferType::AudioI16(ref ab) => ab.get_offset(idx),
203 NABufferType::AudioF32(ref ab) => ab.get_offset(idx),
204 NABufferType::AudioPacked(ref ab) => ab.get_offset(idx),
208 pub fn get_video_info(&self) -> Option<NAVideoInfo> {
210 NABufferType::Video(ref vb) => Some(vb.get_info()),
211 NABufferType::Video16(ref vb) => Some(vb.get_info()),
212 NABufferType::Video32(ref vb) => Some(vb.get_info()),
213 NABufferType::VideoPacked(ref vb) => Some(vb.get_info()),
217 pub fn get_vbuf(&self) -> Option<NAVideoBufferRef<u8>> {
219 NABufferType::Video(ref vb) => Some(vb.clone()),
220 NABufferType::VideoPacked(ref vb) => Some(vb.clone()),
224 pub fn get_vbuf16(&self) -> Option<NAVideoBufferRef<u16>> {
226 NABufferType::Video16(ref vb) => Some(vb.clone()),
230 pub fn get_vbuf32(&self) -> Option<NAVideoBufferRef<u32>> {
232 NABufferType::Video32(ref vb) => Some(vb.clone()),
236 pub fn get_abuf_u8(&self) -> Option<NAAudioBuffer<u8>> {
238 NABufferType::AudioU8(ref ab) => Some(ab.clone()),
239 NABufferType::AudioPacked(ref ab) => Some(ab.clone()),
243 pub fn get_abuf_i16(&self) -> Option<NAAudioBuffer<i16>> {
245 NABufferType::AudioI16(ref ab) => Some(ab.clone()),
249 pub fn get_abuf_i32(&self) -> Option<NAAudioBuffer<i32>> {
251 NABufferType::AudioI32(ref ab) => Some(ab.clone()),
255 pub fn get_abuf_f32(&self) -> Option<NAAudioBuffer<f32>> {
257 NABufferType::AudioF32(ref ab) => Some(ab.clone()),
263 const NA_SIMPLE_VFRAME_COMPONENTS: usize = 4;
264 pub struct NASimpleVideoFrame<'a, T: Copy> {
265 pub width: [usize; NA_SIMPLE_VFRAME_COMPONENTS],
266 pub height: [usize; NA_SIMPLE_VFRAME_COMPONENTS],
268 pub stride: [usize; NA_SIMPLE_VFRAME_COMPONENTS],
269 pub offset: [usize; NA_SIMPLE_VFRAME_COMPONENTS],
270 pub components: usize,
271 pub data: &'a mut Vec<T>,
274 impl<'a, T:Copy> NASimpleVideoFrame<'a, T> {
275 pub fn from_video_buf(vbuf: &'a mut NAVideoBuffer<T>) -> Option<Self> {
276 let vinfo = vbuf.get_info();
277 let components = vinfo.format.components as usize;
278 if components > NA_SIMPLE_VFRAME_COMPONENTS {
281 let mut w: [usize; NA_SIMPLE_VFRAME_COMPONENTS] = [0; NA_SIMPLE_VFRAME_COMPONENTS];
282 let mut h: [usize; NA_SIMPLE_VFRAME_COMPONENTS] = [0; NA_SIMPLE_VFRAME_COMPONENTS];
283 let mut s: [usize; NA_SIMPLE_VFRAME_COMPONENTS] = [0; NA_SIMPLE_VFRAME_COMPONENTS];
284 let mut o: [usize; NA_SIMPLE_VFRAME_COMPONENTS] = [0; NA_SIMPLE_VFRAME_COMPONENTS];
285 for comp in 0..components {
286 let (width, height) = vbuf.get_dimensions(comp);
289 s[comp] = vbuf.get_stride(comp);
290 o[comp] = vbuf.get_offset(comp);
292 let flip = vinfo.flipped;
293 Some(NASimpleVideoFrame {
300 data: vbuf.data.as_mut().unwrap(),
305 #[derive(Debug,Clone,Copy,PartialEq)]
306 pub enum AllocatorError {
311 pub fn alloc_video_buffer(vinfo: NAVideoInfo, align: u8) -> Result<NABufferType, AllocatorError> {
312 let fmt = &vinfo.format;
313 let mut new_size: usize = 0;
314 let mut offs: Vec<usize> = Vec::new();
315 let mut strides: Vec<usize> = Vec::new();
317 for i in 0..fmt.get_num_comp() {
318 if fmt.get_chromaton(i) == None { return Err(AllocatorError::FormatError); }
321 let align_mod = ((1 << align) as usize) - 1;
322 let width = ((vinfo.width as usize) + align_mod) & !align_mod;
323 let height = ((vinfo.height as usize) + align_mod) & !align_mod;
324 let mut max_depth = 0;
325 let mut all_packed = true;
326 let mut all_bytealigned = true;
327 for i in 0..fmt.get_num_comp() {
328 let ochr = fmt.get_chromaton(i);
329 if let None = ochr { continue; }
330 let chr = ochr.unwrap();
331 if !chr.is_packed() {
333 } else if ((chr.get_shift() + chr.get_depth()) & 7) != 0 {
334 all_bytealigned = false;
336 max_depth = max(max_depth, chr.get_depth());
338 let unfit_elem_size = match fmt.get_elem_size() {
343 //todo semi-packed like NV12
344 if fmt.is_paletted() {
345 //todo various-sized palettes?
346 let stride = vinfo.get_format().get_chromaton(0).unwrap().get_linesize(width);
347 let pic_sz = stride.checked_mul(height);
348 if pic_sz == None { return Err(AllocatorError::TooLargeDimensions); }
349 let pal_size = 256 * (fmt.get_elem_size() as usize);
350 let new_size = pic_sz.unwrap().checked_add(pal_size);
351 if new_size == None { return Err(AllocatorError::TooLargeDimensions); }
353 offs.push(stride * height);
354 strides.push(stride);
355 let mut data: Vec<u8> = Vec::with_capacity(new_size.unwrap());
356 data.resize(new_size.unwrap(), 0);
357 let buf: NAVideoBuffer<u8> = NAVideoBuffer { data: NABufferRef::new(data), info: vinfo, offs: offs, strides: strides };
358 Ok(NABufferType::Video(buf.into_ref()))
359 } else if !all_packed {
360 for i in 0..fmt.get_num_comp() {
361 let ochr = fmt.get_chromaton(i);
362 if let None = ochr { continue; }
363 let chr = ochr.unwrap();
364 if !vinfo.is_flipped() {
365 offs.push(new_size as usize);
367 let stride = chr.get_linesize(width);
368 let cur_h = chr.get_height(height);
369 let cur_sz = stride.checked_mul(cur_h);
370 if cur_sz == None { return Err(AllocatorError::TooLargeDimensions); }
371 let new_sz = new_size.checked_add(cur_sz.unwrap());
372 if new_sz == None { return Err(AllocatorError::TooLargeDimensions); }
373 new_size = new_sz.unwrap();
374 if vinfo.is_flipped() {
375 offs.push(new_size as usize);
377 strides.push(stride);
380 let mut data: Vec<u8> = Vec::with_capacity(new_size);
381 data.resize(new_size, 0);
382 let buf: NAVideoBuffer<u8> = NAVideoBuffer { data: NABufferRef::new(data), info: vinfo, offs: offs, strides: strides };
383 Ok(NABufferType::Video(buf.into_ref()))
384 } else if max_depth <= 16 {
385 let mut data: Vec<u16> = Vec::with_capacity(new_size);
386 data.resize(new_size, 0);
387 let buf: NAVideoBuffer<u16> = NAVideoBuffer { data: NABufferRef::new(data), info: vinfo, offs: offs, strides: strides };
388 Ok(NABufferType::Video16(buf.into_ref()))
390 let mut data: Vec<u32> = Vec::with_capacity(new_size);
391 data.resize(new_size, 0);
392 let buf: NAVideoBuffer<u32> = NAVideoBuffer { data: NABufferRef::new(data), info: vinfo, offs: offs, strides: strides };
393 Ok(NABufferType::Video32(buf.into_ref()))
395 } else if all_bytealigned || unfit_elem_size {
396 let elem_sz = fmt.get_elem_size();
397 let line_sz = width.checked_mul(elem_sz as usize);
398 if line_sz == None { return Err(AllocatorError::TooLargeDimensions); }
399 let new_sz = line_sz.unwrap().checked_mul(height);
400 if new_sz == None { return Err(AllocatorError::TooLargeDimensions); }
401 new_size = new_sz.unwrap();
402 let mut data: Vec<u8> = Vec::with_capacity(new_size);
403 data.resize(new_size, 0);
404 strides.push(line_sz.unwrap());
405 let buf: NAVideoBuffer<u8> = NAVideoBuffer { data: NABufferRef::new(data), info: vinfo, offs: offs, strides: strides };
406 Ok(NABufferType::VideoPacked(buf.into_ref()))
408 let elem_sz = fmt.get_elem_size();
409 let new_sz = width.checked_mul(height);
410 if new_sz == None { return Err(AllocatorError::TooLargeDimensions); }
411 new_size = new_sz.unwrap();
414 let mut data: Vec<u16> = Vec::with_capacity(new_size);
415 data.resize(new_size, 0);
417 let buf: NAVideoBuffer<u16> = NAVideoBuffer { data: NABufferRef::new(data), info: vinfo, offs: offs, strides: strides };
418 Ok(NABufferType::Video16(buf.into_ref()))
421 let mut data: Vec<u32> = Vec::with_capacity(new_size);
422 data.resize(new_size, 0);
424 let buf: NAVideoBuffer<u32> = NAVideoBuffer { data: NABufferRef::new(data), info: vinfo, offs: offs, strides: strides };
425 Ok(NABufferType::Video32(buf.into_ref()))
432 pub fn alloc_audio_buffer(ainfo: NAAudioInfo, nsamples: usize, chmap: NAChannelMap) -> Result<NABufferType, AllocatorError> {
433 let mut offs: Vec<usize> = Vec::new();
434 if ainfo.format.is_planar() || (ainfo.channels == 1 && (ainfo.format.get_bits() % 8) == 0) {
435 let len = nsamples.checked_mul(ainfo.channels as usize);
436 if len == None { return Err(AllocatorError::TooLargeDimensions); }
437 let length = len.unwrap();
438 for i in 0..ainfo.channels {
439 offs.push((i as usize) * nsamples);
441 if ainfo.format.is_float() {
442 if ainfo.format.get_bits() == 32 {
443 let mut data: Vec<f32> = Vec::with_capacity(length);
444 data.resize(length, 0.0);
445 let buf: NAAudioBuffer<f32> = NAAudioBuffer { data: NABufferRef::new(data), info: ainfo, offs: offs, chmap: chmap, len: nsamples };
446 Ok(NABufferType::AudioF32(buf))
448 Err(AllocatorError::TooLargeDimensions)
451 if ainfo.format.get_bits() == 8 && !ainfo.format.is_signed() {
452 let mut data: Vec<u8> = Vec::with_capacity(length);
453 data.resize(length, 0);
454 let buf: NAAudioBuffer<u8> = NAAudioBuffer { data: NABufferRef::new(data), info: ainfo, offs: offs, chmap: chmap, len: nsamples };
455 Ok(NABufferType::AudioU8(buf))
456 } else if ainfo.format.get_bits() == 16 && ainfo.format.is_signed() {
457 let mut data: Vec<i16> = Vec::with_capacity(length);
458 data.resize(length, 0);
459 let buf: NAAudioBuffer<i16> = NAAudioBuffer { data: NABufferRef::new(data), info: ainfo, offs: offs, chmap: chmap, len: nsamples };
460 Ok(NABufferType::AudioI16(buf))
462 Err(AllocatorError::TooLargeDimensions)
466 let len = nsamples.checked_mul(ainfo.channels as usize);
467 if len == None { return Err(AllocatorError::TooLargeDimensions); }
468 let length = ainfo.format.get_audio_size(len.unwrap() as u64);
469 let mut data: Vec<u8> = Vec::with_capacity(length);
470 data.resize(length, 0);
471 let buf: NAAudioBuffer<u8> = NAAudioBuffer { data: NABufferRef::new(data), info: ainfo, offs: offs, chmap: chmap, len: nsamples };
472 Ok(NABufferType::AudioPacked(buf))
476 pub fn alloc_data_buffer(size: usize) -> Result<NABufferType, AllocatorError> {
477 let mut data: Vec<u8> = Vec::with_capacity(size);
478 data.resize(size, 0);
479 let buf: NABufferRef<Vec<u8>> = NABufferRef::new(data);
480 Ok(NABufferType::Data(buf))
483 pub fn copy_buffer(buf: NABufferType) -> NABufferType {
487 pub struct NABufferPool {
488 pool: Vec<NABufferRef<NABufferType>>,
493 pub fn new(max_len: usize) -> Self {
495 pool: Vec::with_capacity(max_len),
499 pub fn prealloc_video(&mut self, vinfo: NAVideoInfo, align: u8) -> Result<(), AllocatorError> {
500 let nbufs = self.max_len - self.pool.len();
502 let buf = alloc_video_buffer(vinfo.clone(), align)?;
503 self.pool.push(NABufferRef::new(buf));
507 pub fn prealloc_audio(&mut self, ainfo: NAAudioInfo, nsamples: usize, chmap: NAChannelMap) -> Result<(), AllocatorError> {
508 let nbufs = self.max_len - self.pool.len();
510 let buf = alloc_audio_buffer(ainfo.clone(), nsamples, chmap.clone())?;
511 self.pool.push(NABufferRef::new(buf));
515 pub fn add(&mut self, buf: NABufferType) -> bool {
516 if self.pool.len() < self.max_len {
517 self.pool.push(NABufferRef::new(buf));
523 pub fn get_free(&mut self) -> Option<NABufferRef<NABufferType>> {
524 for e in self.pool.iter() {
525 if e.get_num_refs() == 1 {
526 return Some(e.clone());
535 pub struct NACodecInfo {
537 properties: NACodecTypeInfo,
538 extradata: Option<Arc<Vec<u8>>>,
541 pub type NACodecInfoRef = Arc<NACodecInfo>;
544 pub fn new(name: &'static str, p: NACodecTypeInfo, edata: Option<Vec<u8>>) -> Self {
545 let extradata = match edata {
547 Some(vec) => Some(Arc::new(vec)),
549 NACodecInfo { name: name, properties: p, extradata: extradata }
551 pub fn new_ref(name: &'static str, p: NACodecTypeInfo, edata: Option<Arc<Vec<u8>>>) -> Self {
552 NACodecInfo { name: name, properties: p, extradata: edata }
554 pub fn into_ref(self) -> NACodecInfoRef { Arc::new(self) }
555 pub fn get_properties(&self) -> NACodecTypeInfo { self.properties }
556 pub fn get_extradata(&self) -> Option<Arc<Vec<u8>>> {
557 if let Some(ref vec) = self.extradata { return Some(vec.clone()); }
560 pub fn get_name(&self) -> &'static str { self.name }
561 pub fn is_video(&self) -> bool {
562 if let NACodecTypeInfo::Video(_) = self.properties { return true; }
565 pub fn is_audio(&self) -> bool {
566 if let NACodecTypeInfo::Audio(_) = self.properties { return true; }
569 pub fn new_dummy() -> Arc<Self> {
570 Arc::new(DUMMY_CODEC_INFO)
572 pub fn replace_info(&self, p: NACodecTypeInfo) -> Arc<Self> {
573 Arc::new(NACodecInfo { name: self.name, properties: p, extradata: self.extradata.clone() })
577 impl Default for NACodecInfo {
578 fn default() -> Self { DUMMY_CODEC_INFO }
581 impl fmt::Display for NACodecInfo {
582 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
583 let edata = match self.extradata.clone() {
584 None => format!("no extradata"),
585 Some(v) => format!("{} byte(s) of extradata", v.len()),
587 write!(f, "{}: {} {}", self.name, self.properties, edata)
591 pub const DUMMY_CODEC_INFO: NACodecInfo = NACodecInfo {
593 properties: NACodecTypeInfo::None,
596 #[derive(Debug,Clone)]
605 #[derive(Debug,Clone,Copy,PartialEq)]
615 impl fmt::Display for FrameType {
616 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
618 FrameType::I => write!(f, "I"),
619 FrameType::P => write!(f, "P"),
620 FrameType::B => write!(f, "B"),
621 FrameType::Skip => write!(f, "skip"),
622 FrameType::Other => write!(f, "x"),
627 #[derive(Debug,Clone,Copy)]
628 pub struct NATimeInfo {
631 duration: Option<u64>,
637 pub fn new(pts: Option<u64>, dts: Option<u64>, duration: Option<u64>, tb_num: u32, tb_den: u32) -> Self {
638 NATimeInfo { pts: pts, dts: dts, duration: duration, tb_num: tb_num, tb_den: tb_den }
640 pub fn get_pts(&self) -> Option<u64> { self.pts }
641 pub fn get_dts(&self) -> Option<u64> { self.dts }
642 pub fn get_duration(&self) -> Option<u64> { self.duration }
643 pub fn set_pts(&mut self, pts: Option<u64>) { self.pts = pts; }
644 pub fn set_dts(&mut self, dts: Option<u64>) { self.dts = dts; }
645 pub fn set_duration(&mut self, dur: Option<u64>) { self.duration = dur; }
652 buffer: NABufferType,
653 info: NACodecInfoRef,
656 options: HashMap<String, NAValue>,
659 pub type NAFrameRef = Arc<NAFrame>;
661 fn get_plane_size(info: &NAVideoInfo, idx: usize) -> (usize, usize) {
662 let chromaton = info.get_format().get_chromaton(idx);
663 if let None = chromaton { return (0, 0); }
664 let (hs, vs) = chromaton.unwrap().get_subsampling();
665 let w = (info.get_width() + ((1 << hs) - 1)) >> hs;
666 let h = (info.get_height() + ((1 << vs) - 1)) >> vs;
671 pub fn new(ts: NATimeInfo,
674 info: NACodecInfoRef,
675 options: HashMap<String, NAValue>,
676 buffer: NABufferType) -> Self {
677 NAFrame { ts: ts, buffer: buffer, info: info, ftype: ftype, key: keyframe, options: options }
679 pub fn get_info(&self) -> NACodecInfoRef { self.info.clone() }
680 pub fn get_frame_type(&self) -> FrameType { self.ftype }
681 pub fn is_keyframe(&self) -> bool { self.key }
682 pub fn set_frame_type(&mut self, ftype: FrameType) { self.ftype = ftype; }
683 pub fn set_keyframe(&mut self, key: bool) { self.key = key; }
684 pub fn get_time_information(&self) -> NATimeInfo { self.ts }
685 pub fn get_pts(&self) -> Option<u64> { self.ts.get_pts() }
686 pub fn get_dts(&self) -> Option<u64> { self.ts.get_dts() }
687 pub fn get_duration(&self) -> Option<u64> { self.ts.get_duration() }
688 pub fn set_pts(&mut self, pts: Option<u64>) { self.ts.set_pts(pts); }
689 pub fn set_dts(&mut self, dts: Option<u64>) { self.ts.set_dts(dts); }
690 pub fn set_duration(&mut self, dur: Option<u64>) { self.ts.set_duration(dur); }
692 pub fn get_buffer(&self) -> NABufferType { self.buffer.clone() }
694 pub fn into_ref(self) -> NAFrameRef { Arc::new(self) }
697 impl fmt::Display for NAFrame {
698 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
699 let mut foo = format!("frame type {}", self.ftype);
700 if let Some(pts) = self.ts.pts { foo = format!("{} pts {}", foo, pts); }
701 if let Some(dts) = self.ts.dts { foo = format!("{} dts {}", foo, dts); }
702 if let Some(dur) = self.ts.duration { foo = format!("{} duration {}", foo, dur); }
703 if self.key { foo = format!("{} kf", foo); }
704 write!(f, "[{}]", foo)
708 /// Possible stream types.
709 #[derive(Debug,Clone,Copy)]
711 pub enum StreamType {
718 /// any data stream (or might be an unrecognized audio/video stream)
720 /// nonexistent stream
724 impl fmt::Display for StreamType {
725 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
727 StreamType::Video => write!(f, "Video"),
728 StreamType::Audio => write!(f, "Audio"),
729 StreamType::Subtitles => write!(f, "Subtitles"),
730 StreamType::Data => write!(f, "Data"),
731 StreamType::None => write!(f, "-"),
738 pub struct NAStream {
739 media_type: StreamType,
742 info: NACodecInfoRef,
747 pub type NAStreamRef = Arc<NAStream>;
749 pub fn reduce_timebase(tb_num: u32, tb_den: u32) -> (u32, u32) {
750 if tb_num == 0 { return (tb_num, tb_den); }
751 if (tb_den % tb_num) == 0 { return (1, tb_den / tb_num); }
758 else if b > a { b -= a; }
761 (tb_num / a, tb_den / a)
765 pub fn new(mt: StreamType, id: u32, info: NACodecInfo, tb_num: u32, tb_den: u32) -> Self {
766 let (n, d) = reduce_timebase(tb_num, tb_den);
767 NAStream { media_type: mt, id: id, num: 0, info: info.into_ref(), tb_num: n, tb_den: d }
769 pub fn get_id(&self) -> u32 { self.id }
770 pub fn get_num(&self) -> usize { self.num }
771 pub fn set_num(&mut self, num: usize) { self.num = num; }
772 pub fn get_info(&self) -> NACodecInfoRef { self.info.clone() }
773 pub fn get_timebase(&self) -> (u32, u32) { (self.tb_num, self.tb_den) }
774 pub fn set_timebase(&mut self, tb_num: u32, tb_den: u32) {
775 let (n, d) = reduce_timebase(tb_num, tb_den);
779 pub fn into_ref(self) -> NAStreamRef { Arc::new(self) }
782 impl fmt::Display for NAStream {
783 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
784 write!(f, "({}#{} @ {}/{} - {})", self.media_type, self.id, self.tb_num, self.tb_den, self.info.get_properties())
789 pub struct NAPacket {
792 buffer: NABufferRef<Vec<u8>>,
794 // options: HashMap<String, NAValue<'a>>,
798 pub fn new(str: NAStreamRef, ts: NATimeInfo, kf: bool, vec: Vec<u8>) -> Self {
799 // let mut vec: Vec<u8> = Vec::new();
800 // vec.resize(size, 0);
801 NAPacket { stream: str, ts: ts, keyframe: kf, buffer: NABufferRef::new(vec) }
803 pub fn get_stream(&self) -> NAStreamRef { self.stream.clone() }
804 pub fn get_time_information(&self) -> NATimeInfo { self.ts }
805 pub fn get_pts(&self) -> Option<u64> { self.ts.get_pts() }
806 pub fn get_dts(&self) -> Option<u64> { self.ts.get_dts() }
807 pub fn get_duration(&self) -> Option<u64> { self.ts.get_duration() }
808 pub fn is_keyframe(&self) -> bool { self.keyframe }
809 pub fn get_buffer(&self) -> NABufferRef<Vec<u8>> { self.buffer.clone() }
812 impl Drop for NAPacket {
813 fn drop(&mut self) {}
816 impl fmt::Display for NAPacket {
817 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
818 let mut foo = format!("[pkt for {} size {}", self.stream, self.buffer.len());
819 if let Some(pts) = self.ts.pts { foo = format!("{} pts {}", foo, pts); }
820 if let Some(dts) = self.ts.dts { foo = format!("{} dts {}", foo, dts); }
821 if let Some(dur) = self.ts.duration { foo = format!("{} duration {}", foo, dur); }
822 if self.keyframe { foo = format!("{} kf", foo); }
828 pub trait FrameFromPacket {
829 fn new_from_pkt(pkt: &NAPacket, info: NACodecInfoRef, buf: NABufferType) -> NAFrame;
830 fn fill_timestamps(&mut self, pkt: &NAPacket);
833 impl FrameFromPacket for NAFrame {
834 fn new_from_pkt(pkt: &NAPacket, info: NACodecInfoRef, buf: NABufferType) -> NAFrame {
835 NAFrame::new(pkt.ts, FrameType::Other, pkt.keyframe, info, HashMap::new(), buf)
837 fn fill_timestamps(&mut self, pkt: &NAPacket) {
838 self.ts = pkt.get_time_information();