1 //! Packets and decoded frames functionality.
3 //use std::collections::HashMap;
5 pub use std::sync::Arc;
6 pub use crate::formats::*;
7 pub use crate::refs::*;
10 /// Audio stream information.
12 #[derive(Clone,Copy,PartialEq)]
13 pub struct NAAudioInfo {
16 /// Number of channels.
18 /// Audio sample format.
19 pub format: NASoniton,
20 /// Length of one audio block in samples.
25 /// Constructs a new `NAAudioInfo` instance.
26 pub fn new(sr: u32, ch: u8, fmt: NASoniton, bl: usize) -> Self {
27 NAAudioInfo { sample_rate: sr, channels: ch, format: fmt, block_len: bl }
29 /// Returns audio sample rate.
30 pub fn get_sample_rate(&self) -> u32 { self.sample_rate }
31 /// Returns the number of channels.
32 pub fn get_channels(&self) -> u8 { self.channels }
33 /// Returns sample format.
34 pub fn get_format(&self) -> NASoniton { self.format }
35 /// Returns one audio block duration in samples.
36 pub fn get_block_len(&self) -> usize { self.block_len }
39 impl fmt::Display for NAAudioInfo {
40 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
41 write!(f, "{} Hz, {} ch", self.sample_rate, self.channels)
45 /// Video stream information.
47 #[derive(Clone,Copy,PartialEq)]
48 pub struct NAVideoInfo {
53 /// Picture is stored downside up.
55 /// Picture pixel format.
56 pub format: NAPixelFormaton,
57 /// Declared bits per sample.
62 /// Constructs a new `NAVideoInfo` instance.
63 pub fn new(w: usize, h: usize, flip: bool, fmt: NAPixelFormaton) -> Self {
64 let bits = fmt.get_total_depth();
65 NAVideoInfo { width: w, height: h, flipped: flip, format: fmt, bits }
67 /// Returns picture width.
68 pub fn get_width(&self) -> usize { self.width as usize }
69 /// Returns picture height.
70 pub fn get_height(&self) -> usize { self.height as usize }
71 /// Returns picture orientation.
72 pub fn is_flipped(&self) -> bool { self.flipped }
73 /// Returns picture pixel format.
74 pub fn get_format(&self) -> NAPixelFormaton { self.format }
75 /// Sets new picture width.
76 pub fn set_width(&mut self, w: usize) { self.width = w; }
77 /// Sets new picture height.
78 pub fn set_height(&mut self, h: usize) { self.height = h; }
81 impl fmt::Display for NAVideoInfo {
82 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
83 write!(f, "{}x{}", self.width, self.height)
87 /// A list of possible stream information types.
88 #[derive(Clone,Copy,PartialEq)]
89 pub enum NACodecTypeInfo {
92 /// Audio codec information.
94 /// Video codec information.
98 impl NACodecTypeInfo {
99 /// Returns video stream information.
100 pub fn get_video_info(&self) -> Option<NAVideoInfo> {
102 NACodecTypeInfo::Video(vinfo) => Some(vinfo),
106 /// Returns audio stream information.
107 pub fn get_audio_info(&self) -> Option<NAAudioInfo> {
109 NACodecTypeInfo::Audio(ainfo) => Some(ainfo),
113 /// Reports whether the current stream is video stream.
114 pub fn is_video(&self) -> bool {
116 NACodecTypeInfo::Video(_) => true,
120 /// Reports whether the current stream is audio stream.
121 pub fn is_audio(&self) -> bool {
123 NACodecTypeInfo::Audio(_) => true,
129 impl fmt::Display for NACodecTypeInfo {
130 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
131 let ret = match *self {
132 NACodecTypeInfo::None => "".to_string(),
133 NACodecTypeInfo::Audio(fmt) => format!("{}", fmt),
134 NACodecTypeInfo::Video(fmt) => format!("{}", fmt),
140 /// Decoded video frame.
142 /// NihAV frames are stored in native type (8/16/32-bit elements) inside a single buffer.
143 /// In case of image with several components those components are stored sequentially and can be accessed in the buffer starting at corresponding component offset.
145 pub struct NAVideoBuffer<T> {
147 data: NABufferRef<Vec<T>>,
152 impl<T: Clone> NAVideoBuffer<T> {
153 /// Constructs video buffer from the provided components.
154 pub fn from_raw_parts(info: NAVideoInfo, data: NABufferRef<Vec<T>>, offs: Vec<usize>, strides: Vec<usize>) -> Self {
155 Self { info, data, offs, strides }
157 /// Returns the component offset (0 for all unavailable offsets).
158 pub fn get_offset(&self, idx: usize) -> usize {
159 if idx >= self.offs.len() { 0 }
160 else { self.offs[idx] }
162 /// Returns picture info.
163 pub fn get_info(&self) -> NAVideoInfo { self.info }
164 /// Returns an immutable reference to the data.
165 pub fn get_data(&self) -> &Vec<T> { self.data.as_ref() }
166 /// Returns a mutable reference to the data.
167 pub fn get_data_mut(&mut self) -> Option<&mut Vec<T>> { self.data.as_mut() }
168 /// Returns the number of components in picture format.
169 pub fn get_num_components(&self) -> usize { self.offs.len() }
170 /// Creates a copy of current `NAVideoBuffer`.
171 pub fn copy_buffer(&mut self) -> Self {
172 let mut data: Vec<T> = Vec::with_capacity(self.data.len());
173 data.clone_from(self.data.as_ref());
174 let mut offs: Vec<usize> = Vec::with_capacity(self.offs.len());
175 offs.clone_from(&self.offs);
176 let mut strides: Vec<usize> = Vec::with_capacity(self.strides.len());
177 strides.clone_from(&self.strides);
178 NAVideoBuffer { info: self.info, data: NABufferRef::new(data), offs, strides }
180 /// Returns stride (distance between subsequent lines) for the requested component.
181 pub fn get_stride(&self, idx: usize) -> usize {
182 if idx >= self.strides.len() { return 0; }
185 /// Returns requested component dimensions.
186 pub fn get_dimensions(&self, idx: usize) -> (usize, usize) {
187 get_plane_size(&self.info, idx)
189 /// Converts current instance into buffer reference.
190 pub fn into_ref(self) -> NABufferRef<Self> {
191 NABufferRef::new(self)
194 fn print_contents(&self, datatype: &str) {
195 println!("{} video buffer size {}", datatype, self.data.len());
196 println!(" format {}", self.info);
198 for off in self.offs.iter() {
203 for stride in self.strides.iter() {
204 print!(" {}", *stride);
210 /// A specialised type for reference-counted `NAVideoBuffer`.
211 pub type NAVideoBufferRef<T> = NABufferRef<NAVideoBuffer<T>>;
213 /// Decoded audio frame.
215 /// NihAV frames are stored in native type (8/16/32-bit elements) inside a single buffer.
216 /// In case of planar audio samples for each channel are stored sequentially and can be accessed in the buffer starting at corresponding channel offset.
218 pub struct NAAudioBuffer<T> {
220 data: NABufferRef<Vec<T>>,
228 impl<T: Clone> NAAudioBuffer<T> {
229 /// Returns the start position of requested channel data.
230 pub fn get_offset(&self, idx: usize) -> usize {
231 if idx >= self.offs.len() { 0 }
232 else { self.offs[idx] }
234 /// Returns the distance between the start of one channel and the next one.
235 pub fn get_stride(&self) -> usize { self.stride }
236 /// Returns the distance between the samples in one channel.
237 pub fn get_step(&self) -> usize { self.step }
238 /// Returns audio format information.
239 pub fn get_info(&self) -> NAAudioInfo { self.info }
240 /// Returns channel map.
241 pub fn get_chmap(&self) -> &NAChannelMap { &self.chmap }
242 /// Returns an immutable reference to the data.
243 pub fn get_data(&self) -> &Vec<T> { self.data.as_ref() }
244 /// Returns reference to the data.
245 pub fn get_data_ref(&self) -> NABufferRef<Vec<T>> { self.data.clone() }
246 /// Returns a mutable reference to the data.
247 pub fn get_data_mut(&mut self) -> Option<&mut Vec<T>> { self.data.as_mut() }
248 /// Clones current `NAAudioBuffer` into a new one.
249 pub fn copy_buffer(&mut self) -> Self {
250 let mut data: Vec<T> = Vec::with_capacity(self.data.len());
251 data.clone_from(self.data.as_ref());
252 let mut offs: Vec<usize> = Vec::with_capacity(self.offs.len());
253 offs.clone_from(&self.offs);
254 NAAudioBuffer { info: self.info, data: NABufferRef::new(data), offs, chmap: self.get_chmap().clone(), len: self.len, stride: self.stride, step: self.step }
256 /// Return the length of frame in samples.
257 pub fn get_length(&self) -> usize { self.len }
258 /// Truncates buffer length if possible.
260 /// In case when new length is larger than old length nothing is done.
261 pub fn truncate(&mut self, new_len: usize) {
262 self.len = self.len.min(new_len);
265 fn print_contents(&self, datatype: &str) {
266 println!("Audio buffer with {} data, stride {}, step {}", datatype, self.stride, self.step);
267 println!(" format {}", self.info);
268 println!(" channel map {}", self.chmap);
270 for off in self.offs.iter() {
277 impl NAAudioBuffer<u8> {
278 /// Constructs a new `NAAudioBuffer` instance.
279 pub fn new_from_buf(info: NAAudioInfo, data: NABufferRef<Vec<u8>>, chmap: NAChannelMap) -> Self {
280 let len = data.len();
281 NAAudioBuffer { info, data, chmap, offs: Vec::new(), len, stride: 0, step: 0 }
285 /// A list of possible decoded frame types.
287 pub enum NABufferType {
288 /// 8-bit video buffer.
289 Video (NAVideoBufferRef<u8>),
290 /// 16-bit video buffer (i.e. every component or packed pixel fits into 16 bits).
291 Video16 (NAVideoBufferRef<u16>),
292 /// 32-bit video buffer (i.e. every component or packed pixel fits into 32 bits).
293 Video32 (NAVideoBufferRef<u32>),
294 /// Packed video buffer.
295 VideoPacked(NAVideoBufferRef<u8>),
296 /// Audio buffer with 8-bit unsigned integer audio.
297 AudioU8 (NAAudioBuffer<u8>),
298 /// Audio buffer with 16-bit signed integer audio.
299 AudioI16 (NAAudioBuffer<i16>),
300 /// Audio buffer with 32-bit signed integer audio.
301 AudioI32 (NAAudioBuffer<i32>),
302 /// Audio buffer with 32-bit floating point audio.
303 AudioF32 (NAAudioBuffer<f32>),
304 /// Packed audio buffer.
305 AudioPacked(NAAudioBuffer<u8>),
306 /// Buffer with generic data (e.g. subtitles).
307 Data (NABufferRef<Vec<u8>>),
313 /// Returns the offset to the requested component or channel.
314 pub fn get_offset(&self, idx: usize) -> usize {
316 NABufferType::Video(ref vb) => vb.get_offset(idx),
317 NABufferType::Video16(ref vb) => vb.get_offset(idx),
318 NABufferType::Video32(ref vb) => vb.get_offset(idx),
319 NABufferType::VideoPacked(ref vb) => vb.get_offset(idx),
320 NABufferType::AudioU8(ref ab) => ab.get_offset(idx),
321 NABufferType::AudioI16(ref ab) => ab.get_offset(idx),
322 NABufferType::AudioI32(ref ab) => ab.get_offset(idx),
323 NABufferType::AudioF32(ref ab) => ab.get_offset(idx),
324 NABufferType::AudioPacked(ref ab) => ab.get_offset(idx),
328 /// Returns information for video frames.
329 pub fn get_video_info(&self) -> Option<NAVideoInfo> {
331 NABufferType::Video(ref vb) => Some(vb.get_info()),
332 NABufferType::Video16(ref vb) => Some(vb.get_info()),
333 NABufferType::Video32(ref vb) => Some(vb.get_info()),
334 NABufferType::VideoPacked(ref vb) => Some(vb.get_info()),
338 /// Returns reference to 8-bit (or packed) video buffer.
339 pub fn get_vbuf(&self) -> Option<NAVideoBufferRef<u8>> {
341 NABufferType::Video(ref vb) => Some(vb.clone()),
342 NABufferType::VideoPacked(ref vb) => Some(vb.clone()),
346 /// Returns reference to 16-bit video buffer.
347 pub fn get_vbuf16(&self) -> Option<NAVideoBufferRef<u16>> {
349 NABufferType::Video16(ref vb) => Some(vb.clone()),
353 /// Returns reference to 32-bit video buffer.
354 pub fn get_vbuf32(&self) -> Option<NAVideoBufferRef<u32>> {
356 NABufferType::Video32(ref vb) => Some(vb.clone()),
360 /// Returns information for audio frames.
361 pub fn get_audio_info(&self) -> Option<NAAudioInfo> {
363 NABufferType::AudioU8(ref ab) => Some(ab.get_info()),
364 NABufferType::AudioI16(ref ab) => Some(ab.get_info()),
365 NABufferType::AudioI32(ref ab) => Some(ab.get_info()),
366 NABufferType::AudioF32(ref ab) => Some(ab.get_info()),
367 NABufferType::AudioPacked(ref ab) => Some(ab.get_info()),
371 /// Returns audio channel map.
372 pub fn get_chmap(&self) -> Option<&NAChannelMap> {
374 NABufferType::AudioU8(ref ab) => Some(ab.get_chmap()),
375 NABufferType::AudioI16(ref ab) => Some(ab.get_chmap()),
376 NABufferType::AudioI32(ref ab) => Some(ab.get_chmap()),
377 NABufferType::AudioF32(ref ab) => Some(ab.get_chmap()),
378 NABufferType::AudioPacked(ref ab) => Some(ab.get_chmap()),
382 /// Returns audio frame duration in samples.
383 pub fn get_audio_length(&self) -> usize {
385 NABufferType::AudioU8(ref ab) => ab.get_length(),
386 NABufferType::AudioI16(ref ab) => ab.get_length(),
387 NABufferType::AudioI32(ref ab) => ab.get_length(),
388 NABufferType::AudioF32(ref ab) => ab.get_length(),
389 NABufferType::AudioPacked(ref ab) => ab.get_length(),
393 /// Returns the distance between starts of two channels.
394 pub fn get_audio_stride(&self) -> usize {
396 NABufferType::AudioU8(ref ab) => ab.get_stride(),
397 NABufferType::AudioI16(ref ab) => ab.get_stride(),
398 NABufferType::AudioI32(ref ab) => ab.get_stride(),
399 NABufferType::AudioF32(ref ab) => ab.get_stride(),
400 NABufferType::AudioPacked(ref ab) => ab.get_stride(),
404 /// Returns the distance between two samples in one channel.
405 pub fn get_audio_step(&self) -> usize {
407 NABufferType::AudioU8(ref ab) => ab.get_step(),
408 NABufferType::AudioI16(ref ab) => ab.get_step(),
409 NABufferType::AudioI32(ref ab) => ab.get_step(),
410 NABufferType::AudioF32(ref ab) => ab.get_step(),
411 NABufferType::AudioPacked(ref ab) => ab.get_step(),
415 /// Returns reference to 8-bit (or packed) audio buffer.
416 pub fn get_abuf_u8(&self) -> Option<NAAudioBuffer<u8>> {
418 NABufferType::AudioU8(ref ab) => Some(ab.clone()),
419 NABufferType::AudioPacked(ref ab) => Some(ab.clone()),
423 /// Returns reference to 16-bit audio buffer.
424 pub fn get_abuf_i16(&self) -> Option<NAAudioBuffer<i16>> {
426 NABufferType::AudioI16(ref ab) => Some(ab.clone()),
430 /// Returns reference to 32-bit integer audio buffer.
431 pub fn get_abuf_i32(&self) -> Option<NAAudioBuffer<i32>> {
433 NABufferType::AudioI32(ref ab) => Some(ab.clone()),
437 /// Returns reference to 32-bit floating point audio buffer.
438 pub fn get_abuf_f32(&self) -> Option<NAAudioBuffer<f32>> {
440 NABufferType::AudioF32(ref ab) => Some(ab.clone()),
444 /// Prints internal buffer layout.
445 pub fn print_buffer_metadata(&self) {
447 NABufferType::Video(ref buf) => buf.print_contents("8-bit"),
448 NABufferType::Video16(ref buf) => buf.print_contents("16-bit"),
449 NABufferType::Video32(ref buf) => buf.print_contents("32-bit"),
450 NABufferType::VideoPacked(ref buf) => buf.print_contents("packed"),
451 NABufferType::AudioU8(ref buf) => buf.print_contents("8-bit unsigned integer"),
452 NABufferType::AudioI16(ref buf) => buf.print_contents("16-bit integer"),
453 NABufferType::AudioI32(ref buf) => buf.print_contents("32-bit integer"),
454 NABufferType::AudioF32(ref buf) => buf.print_contents("32-bit float"),
455 NABufferType::AudioPacked(ref buf) => buf.print_contents("packed"),
456 NABufferType::Data(ref buf) => { println!("Data buffer, len = {}", buf.len()); },
457 NABufferType::None => { println!("No buffer"); },
462 const NA_SIMPLE_VFRAME_COMPONENTS: usize = 4;
463 /// Simplified decoded frame data.
464 pub struct NASimpleVideoFrame<'a, T: Copy> {
465 /// Widths of each picture component.
466 pub width: [usize; NA_SIMPLE_VFRAME_COMPONENTS],
467 /// Heights of each picture component.
468 pub height: [usize; NA_SIMPLE_VFRAME_COMPONENTS],
469 /// Orientation (upside-down or downside-up) flag.
471 /// Strides for each component.
472 pub stride: [usize; NA_SIMPLE_VFRAME_COMPONENTS],
473 /// Start of each component.
474 pub offset: [usize; NA_SIMPLE_VFRAME_COMPONENTS],
475 /// Number of components.
476 pub components: usize,
477 /// Pointer to the picture pixel data.
478 pub data: &'a mut [T],
481 impl<'a, T:Copy> NASimpleVideoFrame<'a, T> {
482 /// Constructs a new instance of `NASimpleVideoFrame` from `NAVideoBuffer`.
483 pub fn from_video_buf(vbuf: &'a mut NAVideoBuffer<T>) -> Option<Self> {
484 let vinfo = vbuf.get_info();
485 let components = vinfo.format.components as usize;
486 if components > NA_SIMPLE_VFRAME_COMPONENTS {
489 let mut w: [usize; NA_SIMPLE_VFRAME_COMPONENTS] = [0; NA_SIMPLE_VFRAME_COMPONENTS];
490 let mut h: [usize; NA_SIMPLE_VFRAME_COMPONENTS] = [0; NA_SIMPLE_VFRAME_COMPONENTS];
491 let mut s: [usize; NA_SIMPLE_VFRAME_COMPONENTS] = [0; NA_SIMPLE_VFRAME_COMPONENTS];
492 let mut o: [usize; NA_SIMPLE_VFRAME_COMPONENTS] = [0; NA_SIMPLE_VFRAME_COMPONENTS];
493 for comp in 0..components {
494 let (width, height) = vbuf.get_dimensions(comp);
497 s[comp] = vbuf.get_stride(comp);
498 o[comp] = vbuf.get_offset(comp);
500 let flip = vinfo.flipped;
501 Some(NASimpleVideoFrame {
508 data: vbuf.data.as_mut_slice(),
513 /// A list of possible frame allocator errors.
514 #[derive(Debug,Clone,Copy,PartialEq)]
515 pub enum AllocatorError {
516 /// Requested picture dimensions are too large.
518 /// Invalid input format.
522 /// Constructs a new video buffer with requested format.
524 /// `align` is power of two alignment for image. E.g. the value of 5 means that frame dimensions will be padded to be multiple of 32.
525 pub fn alloc_video_buffer(vinfo: NAVideoInfo, align: u8) -> Result<NABufferType, AllocatorError> {
526 let fmt = &vinfo.format;
527 let mut new_size: usize = 0;
528 let mut offs: Vec<usize> = Vec::new();
529 let mut strides: Vec<usize> = Vec::new();
531 for i in 0..fmt.get_num_comp() {
532 if fmt.get_chromaton(i) == None { return Err(AllocatorError::FormatError); }
535 let align_mod = ((1 << align) as usize) - 1;
536 let width = ((vinfo.width as usize) + align_mod) & !align_mod;
537 let height = ((vinfo.height as usize) + align_mod) & !align_mod;
538 let mut max_depth = 0;
539 let mut all_packed = true;
540 let mut all_bytealigned = true;
541 for i in 0..fmt.get_num_comp() {
542 let ochr = fmt.get_chromaton(i);
543 if ochr.is_none() { continue; }
544 let chr = ochr.unwrap();
545 if !chr.is_packed() {
547 } else if ((chr.get_shift() + chr.get_depth()) & 7) != 0 {
548 all_bytealigned = false;
550 max_depth = max(max_depth, chr.get_depth());
552 let unfit_elem_size = match fmt.get_elem_size() {
557 //todo semi-packed like NV12
558 if fmt.is_paletted() {
559 //todo various-sized palettes?
560 let stride = vinfo.get_format().get_chromaton(0).unwrap().get_linesize(width);
561 let pic_sz = stride.checked_mul(height);
562 if pic_sz == None { return Err(AllocatorError::TooLargeDimensions); }
563 let pal_size = 256 * (fmt.get_elem_size() as usize);
564 let new_size = pic_sz.unwrap().checked_add(pal_size);
565 if new_size == None { return Err(AllocatorError::TooLargeDimensions); }
567 offs.push(stride * height);
568 strides.push(stride);
569 let data: Vec<u8> = vec![0; new_size.unwrap()];
570 let buf: NAVideoBuffer<u8> = NAVideoBuffer { data: NABufferRef::new(data), info: vinfo, offs, strides };
571 Ok(NABufferType::Video(buf.into_ref()))
572 } else if !all_packed {
573 for i in 0..fmt.get_num_comp() {
574 let ochr = fmt.get_chromaton(i);
575 if ochr.is_none() { continue; }
576 let chr = ochr.unwrap();
577 offs.push(new_size as usize);
578 let stride = chr.get_linesize(width);
579 let cur_h = chr.get_height(height);
580 let cur_sz = stride.checked_mul(cur_h);
581 if cur_sz == None { return Err(AllocatorError::TooLargeDimensions); }
582 let new_sz = new_size.checked_add(cur_sz.unwrap());
583 if new_sz == None { return Err(AllocatorError::TooLargeDimensions); }
584 new_size = new_sz.unwrap();
585 strides.push(stride);
588 let data: Vec<u8> = vec![0; new_size];
589 let buf: NAVideoBuffer<u8> = NAVideoBuffer { data: NABufferRef::new(data), info: vinfo, offs, strides };
590 Ok(NABufferType::Video(buf.into_ref()))
591 } else if max_depth <= 16 {
592 let data: Vec<u16> = vec![0; new_size];
593 let buf: NAVideoBuffer<u16> = NAVideoBuffer { data: NABufferRef::new(data), info: vinfo, offs, strides };
594 Ok(NABufferType::Video16(buf.into_ref()))
596 let data: Vec<u32> = vec![0; new_size];
597 let buf: NAVideoBuffer<u32> = NAVideoBuffer { data: NABufferRef::new(data), info: vinfo, offs, strides };
598 Ok(NABufferType::Video32(buf.into_ref()))
600 } else if all_bytealigned || unfit_elem_size {
601 let elem_sz = fmt.get_elem_size();
602 let line_sz = width.checked_mul(elem_sz as usize);
603 if line_sz == None { return Err(AllocatorError::TooLargeDimensions); }
604 let new_sz = line_sz.unwrap().checked_mul(height);
605 if new_sz == None { return Err(AllocatorError::TooLargeDimensions); }
606 new_size = new_sz.unwrap();
607 let data: Vec<u8> = vec![0; new_size];
608 strides.push(line_sz.unwrap());
609 let buf: NAVideoBuffer<u8> = NAVideoBuffer { data: NABufferRef::new(data), info: vinfo, offs, strides };
610 Ok(NABufferType::VideoPacked(buf.into_ref()))
612 let elem_sz = fmt.get_elem_size();
613 let new_sz = width.checked_mul(height);
614 if new_sz == None { return Err(AllocatorError::TooLargeDimensions); }
615 new_size = new_sz.unwrap();
618 let data: Vec<u16> = vec![0; new_size];
620 let buf: NAVideoBuffer<u16> = NAVideoBuffer { data: NABufferRef::new(data), info: vinfo, offs, strides };
621 Ok(NABufferType::Video16(buf.into_ref()))
624 let data: Vec<u32> = vec![0; new_size];
626 let buf: NAVideoBuffer<u32> = NAVideoBuffer { data: NABufferRef::new(data), info: vinfo, offs, strides };
627 Ok(NABufferType::Video32(buf.into_ref()))
634 /// Constructs a new audio buffer for the requested format and length.
635 #[allow(clippy::collapsible_if)]
636 pub fn alloc_audio_buffer(ainfo: NAAudioInfo, nsamples: usize, chmap: NAChannelMap) -> Result<NABufferType, AllocatorError> {
637 let mut offs: Vec<usize> = Vec::new();
638 if ainfo.format.is_planar() || ((ainfo.format.get_bits() % 8) == 0) {
639 let len = nsamples.checked_mul(ainfo.channels as usize);
640 if len == None { return Err(AllocatorError::TooLargeDimensions); }
641 let length = len.unwrap();
644 if ainfo.format.is_planar() {
647 for i in 0..ainfo.channels {
648 offs.push((i as usize) * stride);
652 step = ainfo.channels as usize;
653 for i in 0..ainfo.channels {
654 offs.push(i as usize);
657 if ainfo.format.is_float() {
658 if ainfo.format.get_bits() == 32 {
659 let data: Vec<f32> = vec![0.0; length];
660 let buf: NAAudioBuffer<f32> = NAAudioBuffer { data: NABufferRef::new(data), info: ainfo, offs, chmap, len: nsamples, stride, step };
661 Ok(NABufferType::AudioF32(buf))
663 Err(AllocatorError::TooLargeDimensions)
666 if ainfo.format.get_bits() == 8 && !ainfo.format.is_signed() {
667 let data: Vec<u8> = vec![0; length];
668 let buf: NAAudioBuffer<u8> = NAAudioBuffer { data: NABufferRef::new(data), info: ainfo, offs, chmap, len: nsamples, stride, step };
669 Ok(NABufferType::AudioU8(buf))
670 } else if ainfo.format.get_bits() == 16 && ainfo.format.is_signed() {
671 let data: Vec<i16> = vec![0; length];
672 let buf: NAAudioBuffer<i16> = NAAudioBuffer { data: NABufferRef::new(data), info: ainfo, offs, chmap, len: nsamples, stride, step };
673 Ok(NABufferType::AudioI16(buf))
674 } else if ainfo.format.get_bits() == 32 && ainfo.format.is_signed() {
675 let data: Vec<i32> = vec![0; length];
676 let buf: NAAudioBuffer<i32> = NAAudioBuffer { data: NABufferRef::new(data), info: ainfo, offs, chmap, len: nsamples, stride, step };
677 Ok(NABufferType::AudioI32(buf))
679 Err(AllocatorError::TooLargeDimensions)
683 let len = nsamples.checked_mul(ainfo.channels as usize);
684 if len == None { return Err(AllocatorError::TooLargeDimensions); }
685 let length = ainfo.format.get_audio_size(len.unwrap() as u64);
686 let data: Vec<u8> = vec![0; length];
687 let buf: NAAudioBuffer<u8> = NAAudioBuffer { data: NABufferRef::new(data), info: ainfo, offs, chmap, len: nsamples, stride: 0, step: 0 };
688 Ok(NABufferType::AudioPacked(buf))
692 /// Constructs a new buffer for generic data.
693 pub fn alloc_data_buffer(size: usize) -> Result<NABufferType, AllocatorError> {
694 let data: Vec<u8> = vec![0; size];
695 let buf: NABufferRef<Vec<u8>> = NABufferRef::new(data);
696 Ok(NABufferType::Data(buf))
699 /// Creates a clone of current buffer.
700 pub fn copy_buffer(buf: &NABufferType) -> NABufferType {
704 /// Video frame pool.
706 /// This structure allows codec to effectively reuse old frames instead of allocating and de-allocating frames every time.
707 /// Caller can also reserve some frames for its own purposes e.g. display queue.
708 pub struct NAVideoBufferPool<T:Copy> {
709 pool: Vec<NAVideoBufferRef<T>>,
714 impl<T:Copy> NAVideoBufferPool<T> {
715 /// Constructs a new `NAVideoBufferPool` instance.
716 pub fn new(max_len: usize) -> Self {
718 pool: Vec::with_capacity(max_len),
723 /// Sets the number of buffers reserved for the user.
724 pub fn set_dec_bufs(&mut self, add_len: usize) {
725 self.add_len = add_len;
727 /// Returns an unused buffer from the pool.
728 pub fn get_free(&mut self) -> Option<NAVideoBufferRef<T>> {
729 for e in self.pool.iter() {
730 if e.get_num_refs() == 1 {
731 return Some(e.clone());
736 /// Clones provided frame data into a free pool frame.
737 pub fn get_copy(&mut self, rbuf: &NAVideoBufferRef<T>) -> Option<NAVideoBufferRef<T>> {
738 let mut dbuf = self.get_free()?;
739 dbuf.data.copy_from_slice(&rbuf.data);
742 /// Clears the pool from all frames.
743 pub fn reset(&mut self) {
744 self.pool.truncate(0);
748 impl NAVideoBufferPool<u8> {
749 /// Allocates the target amount of video frames using [`alloc_video_buffer`].
751 /// [`alloc_video_buffer`]: ./fn.alloc_video_buffer.html
752 pub fn prealloc_video(&mut self, vinfo: NAVideoInfo, align: u8) -> Result<(), AllocatorError> {
753 let nbufs = self.max_len + self.add_len - self.pool.len();
755 let vbuf = alloc_video_buffer(vinfo, align)?;
756 if let NABufferType::Video(buf) = vbuf {
758 } else if let NABufferType::VideoPacked(buf) = vbuf {
761 return Err(AllocatorError::FormatError);
768 impl NAVideoBufferPool<u16> {
769 /// Allocates the target amount of video frames using [`alloc_video_buffer`].
771 /// [`alloc_video_buffer`]: ./fn.alloc_video_buffer.html
772 pub fn prealloc_video(&mut self, vinfo: NAVideoInfo, align: u8) -> Result<(), AllocatorError> {
773 let nbufs = self.max_len + self.add_len - self.pool.len();
775 let vbuf = alloc_video_buffer(vinfo, align)?;
776 if let NABufferType::Video16(buf) = vbuf {
779 return Err(AllocatorError::FormatError);
786 impl NAVideoBufferPool<u32> {
787 /// Allocates the target amount of video frames using [`alloc_video_buffer`].
789 /// [`alloc_video_buffer`]: ./fn.alloc_video_buffer.html
790 pub fn prealloc_video(&mut self, vinfo: NAVideoInfo, align: u8) -> Result<(), AllocatorError> {
791 let nbufs = self.max_len + self.add_len - self.pool.len();
793 let vbuf = alloc_video_buffer(vinfo, align)?;
794 if let NABufferType::Video32(buf) = vbuf {
797 return Err(AllocatorError::FormatError);
804 /// Information about codec contained in a stream.
807 pub struct NACodecInfo {
809 properties: NACodecTypeInfo,
810 extradata: Option<Arc<Vec<u8>>>,
813 /// A specialised type for reference-counted `NACodecInfo`.
814 pub type NACodecInfoRef = Arc<NACodecInfo>;
817 /// Constructs a new instance of `NACodecInfo`.
818 pub fn new(name: &'static str, p: NACodecTypeInfo, edata: Option<Vec<u8>>) -> Self {
819 let extradata = match edata {
821 Some(vec) => Some(Arc::new(vec)),
823 NACodecInfo { name, properties: p, extradata }
825 /// Constructs a new reference-counted instance of `NACodecInfo`.
826 pub fn new_ref(name: &'static str, p: NACodecTypeInfo, edata: Option<Arc<Vec<u8>>>) -> Self {
827 NACodecInfo { name, properties: p, extradata: edata }
829 /// Converts current instance into a reference-counted one.
830 pub fn into_ref(self) -> NACodecInfoRef { Arc::new(self) }
831 /// Returns codec information.
832 pub fn get_properties(&self) -> NACodecTypeInfo { self.properties }
833 /// Returns additional initialisation data required by the codec.
834 pub fn get_extradata(&self) -> Option<Arc<Vec<u8>>> {
835 if let Some(ref vec) = self.extradata { return Some(vec.clone()); }
838 /// Returns codec name.
839 pub fn get_name(&self) -> &'static str { self.name }
840 /// Reports whether it is a video codec.
841 pub fn is_video(&self) -> bool {
842 if let NACodecTypeInfo::Video(_) = self.properties { return true; }
845 /// Reports whether it is an audio codec.
846 pub fn is_audio(&self) -> bool {
847 if let NACodecTypeInfo::Audio(_) = self.properties { return true; }
850 /// Constructs a new empty reference-counted instance of `NACodecInfo`.
851 pub fn new_dummy() -> Arc<Self> {
852 Arc::new(DUMMY_CODEC_INFO)
854 /// Updates codec infomation.
855 pub fn replace_info(&self, p: NACodecTypeInfo) -> Arc<Self> {
856 Arc::new(NACodecInfo { name: self.name, properties: p, extradata: self.extradata.clone() })
860 impl Default for NACodecInfo {
861 fn default() -> Self { DUMMY_CODEC_INFO }
864 impl fmt::Display for NACodecInfo {
865 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
866 let edata = match self.extradata.clone() {
867 None => "no extradata".to_string(),
868 Some(v) => format!("{} byte(s) of extradata", v.len()),
870 write!(f, "{}: {} {}", self.name, self.properties, edata)
874 /// Default empty codec information.
875 pub const DUMMY_CODEC_INFO: NACodecInfo = NACodecInfo {
877 properties: NACodecTypeInfo::None,
880 /// A list of recognized frame types.
881 #[derive(Debug,Clone,Copy,PartialEq)]
884 /// Intra frame type.
886 /// Inter frame type.
888 /// Bidirectionally predicted frame.
892 /// When such frame is encountered then last frame should be used again if it is needed.
894 /// Some other frame type.
898 impl fmt::Display for FrameType {
899 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
901 FrameType::I => write!(f, "I"),
902 FrameType::P => write!(f, "P"),
903 FrameType::B => write!(f, "B"),
904 FrameType::Skip => write!(f, "skip"),
905 FrameType::Other => write!(f, "x"),
910 /// Timestamp information.
911 #[derive(Debug,Clone,Copy)]
912 pub struct NATimeInfo {
913 /// Presentation timestamp.
914 pub pts: Option<u64>,
915 /// Decode timestamp.
916 pub dts: Option<u64>,
917 /// Duration (in timebase units).
918 pub duration: Option<u64>,
919 /// Timebase numerator.
921 /// Timebase denominator.
926 /// Constructs a new `NATimeInfo` instance.
927 pub fn new(pts: Option<u64>, dts: Option<u64>, duration: Option<u64>, tb_num: u32, tb_den: u32) -> Self {
928 NATimeInfo { pts, dts, duration, tb_num, tb_den }
930 /// Returns presentation timestamp.
931 pub fn get_pts(&self) -> Option<u64> { self.pts }
932 /// Returns decoding timestamp.
933 pub fn get_dts(&self) -> Option<u64> { self.dts }
934 /// Returns duration.
935 pub fn get_duration(&self) -> Option<u64> { self.duration }
936 /// Sets new presentation timestamp.
937 pub fn set_pts(&mut self, pts: Option<u64>) { self.pts = pts; }
938 /// Sets new decoding timestamp.
939 pub fn set_dts(&mut self, dts: Option<u64>) { self.dts = dts; }
940 /// Sets new duration.
941 pub fn set_duration(&mut self, dur: Option<u64>) { self.duration = dur; }
943 /// Converts time in given scale into timestamp in given base.
944 #[allow(clippy::collapsible_if)]
945 pub fn time_to_ts(time: u64, base: u64, tb_num: u32, tb_den: u32) -> u64 {
946 let tb_num = u64::from(tb_num);
947 let tb_den = u64::from(tb_den);
948 let tmp = time.checked_mul(tb_den);
949 if let Some(tmp) = tmp {
953 let coarse = time / tb_num;
954 if let Some(tmp) = coarse.checked_mul(tb_den) {
957 (coarse / base) * tb_den
960 let coarse = time / base;
961 if let Some(tmp) = coarse.checked_mul(tb_den) {
964 (coarse / tb_num) * tb_den
969 /// Converts timestamp in given base into time in given scale.
970 pub fn ts_to_time(ts: u64, base: u64, tb_num: u32, tb_den: u32) -> u64 {
971 let tb_num = u64::from(tb_num);
972 let tb_den = u64::from(tb_den);
973 let tmp = ts.checked_mul(base);
974 if let Some(tmp) = tmp {
975 let tmp2 = tmp.checked_mul(tb_num);
976 if let Some(tmp2) = tmp2 {
979 (tmp / tb_den) * tb_num
982 let tmp = ts.checked_mul(tb_num);
983 if let Some(tmp) = tmp {
984 (tmp / tb_den) * base
986 (ts / tb_den) * base * tb_num
990 fn get_cur_ts(&self) -> u64 { self.pts.unwrap_or_else(|| self.dts.unwrap_or(0)) }
991 fn get_cur_millis(&self) -> u64 {
992 let ts = self.get_cur_ts();
993 Self::ts_to_time(ts, 1000, self.tb_num, self.tb_den)
995 /// Checks whether the current time information is earler than provided reference time.
996 pub fn less_than(&self, time: NATimePoint) -> bool {
997 if self.pts.is_none() && self.dts.is_none() {
1001 NATimePoint::PTS(rpts) => self.get_cur_ts() < rpts,
1002 NATimePoint::Milliseconds(ms) => self.get_cur_millis() < ms,
1003 NATimePoint::None => false,
1006 /// Checks whether the current time information is the same as provided reference time.
1007 pub fn equal(&self, time: NATimePoint) -> bool {
1008 if self.pts.is_none() && self.dts.is_none() {
1009 return time == NATimePoint::None;
1012 NATimePoint::PTS(rpts) => self.get_cur_ts() == rpts,
1013 NATimePoint::Milliseconds(ms) => self.get_cur_millis() == ms,
1014 NATimePoint::None => false,
1019 /// Time information for specifying durations or seek positions.
1020 #[derive(Clone,Copy,Debug,PartialEq)]
1021 pub enum NATimePoint {
1022 /// Time in milliseconds.
1024 /// Stream timestamp.
1026 /// No time information present.
1030 impl Default for NATimePoint {
1031 fn default() -> Self {
1036 impl fmt::Display for NATimePoint {
1037 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1039 NATimePoint::Milliseconds(millis) => {
1040 let tot_s = millis / 1000;
1041 let ms = millis % 1000;
1044 return write!(f, "{}.{:03}", tot_s, ms);
1046 return write!(f, "{}", tot_s);
1049 let tot_m = tot_s / 60;
1053 return write!(f, "{}:{:02}.{:03}", tot_m, s, ms);
1055 return write!(f, "{}:{:02}", tot_m, s);
1061 write!(f, "{}:{:02}:{:02}.{:03}", h, m, s, ms)
1063 write!(f, "{}:{:02}:{:02}", h, m, s)
1066 NATimePoint::PTS(pts) => {
1067 write!(f, "{}pts", pts)
1069 NATimePoint::None => {
1076 impl FromStr for NATimePoint {
1077 type Err = FormatParseError;
1079 /// Parses the string into time information.
1081 /// Accepted formats are `<u64>pts`, `<u64>ms` or `[hh:][mm:]ss[.ms]`.
1082 fn from_str(s: &str) -> Result<Self, Self::Err> {
1084 return Err(FormatParseError {});
1086 if !s.ends_with("pts") {
1087 if s.ends_with("ms") {
1088 let str_b = s.as_bytes();
1089 let num = std::str::from_utf8(&str_b[..str_b.len() - 2]).unwrap();
1090 let ret = num.parse::<u64>();
1091 if let Ok(val) = ret {
1092 return Ok(NATimePoint::Milliseconds(val));
1094 return Err(FormatParseError {});
1097 let mut parts = s.split(':');
1099 let mut mins = None;
1100 let mut secs = parts.next();
1101 if let Some(part) = parts.next() {
1102 std::mem::swap(&mut mins, &mut secs);
1105 if let Some(part) = parts.next() {
1106 std::mem::swap(&mut hrs, &mut mins);
1107 std::mem::swap(&mut mins, &mut secs);
1110 if parts.next().is_some() {
1111 return Err(FormatParseError {});
1113 let hours = if let Some(val) = hrs {
1114 let ret = val.parse::<u64>();
1115 if ret.is_err() { return Err(FormatParseError {}); }
1116 let val = ret.unwrap();
1117 if val > 1000 { return Err(FormatParseError {}); }
1120 let minutes = if let Some(val) = mins {
1121 let ret = val.parse::<u64>();
1122 if ret.is_err() { return Err(FormatParseError {}); }
1123 let val = ret.unwrap();
1124 if val >= 60 { return Err(FormatParseError {}); }
1127 let (seconds, millis) = if let Some(val) = secs {
1128 let mut parts = val.split('.');
1129 let ret = parts.next().unwrap().parse::<u64>();
1130 if ret.is_err() { return Err(FormatParseError {}); }
1131 let seconds = ret.unwrap();
1132 if mins.is_some() && seconds >= 60 { return Err(FormatParseError {}); }
1133 let millis = if let Some(val) = parts.next() {
1136 for ch in val.chars() {
1137 if ch >= '0' && ch <= '9' {
1138 mval = mval * 10 + u64::from((ch as u8) - b'0');
1140 if base > 3 { break; }
1142 return Err(FormatParseError {});
1152 } else { unreachable!(); };
1153 let tot_secs = hours * 60 * 60 + minutes * 60 + seconds;
1154 Ok(NATimePoint::Milliseconds(tot_secs * 1000 + millis))
1156 let str_b = s.as_bytes();
1157 let num = std::str::from_utf8(&str_b[..str_b.len() - 3]).unwrap();
1158 let ret = num.parse::<u64>();
1159 if let Ok(val) = ret {
1160 Ok(NATimePoint::PTS(val))
1162 Err(FormatParseError {})
1168 /// Decoded frame information.
1171 pub struct NAFrame {
1172 /// Frame timestamp.
1176 buffer: NABufferType,
1177 info: NACodecInfoRef,
1179 pub frame_type: FrameType,
1182 // options: HashMap<String, NAValue>,
1185 /// A specialised type for reference-counted `NAFrame`.
1186 pub type NAFrameRef = Arc<NAFrame>;
1188 fn get_plane_size(info: &NAVideoInfo, idx: usize) -> (usize, usize) {
1189 let chromaton = info.get_format().get_chromaton(idx);
1190 if chromaton.is_none() { return (0, 0); }
1191 let (hs, vs) = chromaton.unwrap().get_subsampling();
1192 let w = (info.get_width() + ((1 << hs) - 1)) >> hs;
1193 let h = (info.get_height() + ((1 << vs) - 1)) >> vs;
1198 /// Constructs a new `NAFrame` instance.
1199 pub fn new(ts: NATimeInfo,
1202 info: NACodecInfoRef,
1203 /*options: HashMap<String, NAValue>,*/
1204 buffer: NABufferType) -> Self {
1205 NAFrame { ts, id: 0, buffer, info, frame_type: ftype, key: keyframe/*, options*/ }
1207 /// Returns frame format information.
1208 pub fn get_info(&self) -> NACodecInfoRef { self.info.clone() }
1209 /// Returns frame type.
1210 pub fn get_frame_type(&self) -> FrameType { self.frame_type }
1211 /// Reports whether the frame is a keyframe.
1212 pub fn is_keyframe(&self) -> bool { self.key }
1213 /// Sets new frame type.
1214 pub fn set_frame_type(&mut self, ftype: FrameType) { self.frame_type = ftype; }
1215 /// Sets keyframe flag.
1216 pub fn set_keyframe(&mut self, key: bool) { self.key = key; }
1217 /// Returns frame timestamp.
1218 pub fn get_time_information(&self) -> NATimeInfo { self.ts }
1219 /// Returns frame presentation time.
1220 pub fn get_pts(&self) -> Option<u64> { self.ts.get_pts() }
1221 /// Returns frame decoding time.
1222 pub fn get_dts(&self) -> Option<u64> { self.ts.get_dts() }
1223 /// Returns picture ID.
1224 pub fn get_id(&self) -> i64 { self.id }
1225 /// Returns frame display duration.
1226 pub fn get_duration(&self) -> Option<u64> { self.ts.get_duration() }
1227 /// Sets new presentation timestamp.
1228 pub fn set_pts(&mut self, pts: Option<u64>) { self.ts.set_pts(pts); }
1229 /// Sets new decoding timestamp.
1230 pub fn set_dts(&mut self, dts: Option<u64>) { self.ts.set_dts(dts); }
1231 /// Sets new picture ID.
1232 pub fn set_id(&mut self, id: i64) { self.id = id; }
1233 /// Sets new duration.
1234 pub fn set_duration(&mut self, dur: Option<u64>) { self.ts.set_duration(dur); }
1236 /// Returns a reference to the frame data.
1237 pub fn get_buffer(&self) -> NABufferType { self.buffer.clone() }
1239 /// Converts current instance into a reference-counted one.
1240 pub fn into_ref(self) -> NAFrameRef { Arc::new(self) }
1242 /// Creates new frame with metadata from `NAPacket`.
1243 pub fn new_from_pkt(pkt: &NAPacket, info: NACodecInfoRef, buf: NABufferType) -> NAFrame {
1244 NAFrame::new(pkt.ts, FrameType::Other, pkt.keyframe, info, /*HashMap::new(),*/ buf)
1248 impl fmt::Display for NAFrame {
1249 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1250 let mut ostr = format!("frame type {}", self.frame_type);
1251 if let Some(pts) = self.ts.pts { ostr = format!("{} pts {}", ostr, pts); }
1252 if let Some(dts) = self.ts.dts { ostr = format!("{} dts {}", ostr, dts); }
1253 if let Some(dur) = self.ts.duration { ostr = format!("{} duration {}", ostr, dur); }
1254 if self.key { ostr = format!("{} kf", ostr); }
1255 write!(f, "[{}]", ostr)
1259 /// A list of possible stream types.
1260 #[derive(Debug,Clone,Copy,PartialEq)]
1262 pub enum StreamType {
1269 /// Any data stream (or might be an unrecognized audio/video stream).
1271 /// Nonexistent stream.
1275 impl fmt::Display for StreamType {
1276 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1278 StreamType::Video => write!(f, "Video"),
1279 StreamType::Audio => write!(f, "Audio"),
1280 StreamType::Subtitles => write!(f, "Subtitles"),
1281 StreamType::Data => write!(f, "Data"),
1282 StreamType::None => write!(f, "-"),
1290 pub struct NAStream {
1291 media_type: StreamType,
1295 info: NACodecInfoRef,
1296 /// Timebase numerator.
1298 /// Timebase denominator.
1300 /// Duration in timebase units (zero if not available).
1304 /// A specialised reference-counted `NAStream` type.
1305 pub type NAStreamRef = Arc<NAStream>;
1307 /// Downscales the timebase by its greatest common denominator.
1308 #[allow(clippy::comparison_chain)]
1309 pub fn reduce_timebase(tb_num: u32, tb_den: u32) -> (u32, u32) {
1310 if tb_num == 0 { return (tb_num, tb_den); }
1311 if (tb_den % tb_num) == 0 { return (1, tb_den / tb_num); }
1317 if a > b { a -= b; }
1318 else if b > a { b -= a; }
1321 (tb_num / a, tb_den / a)
1325 /// Constructs a new `NAStream` instance.
1326 pub fn new(mt: StreamType, id: u32, info: NACodecInfo, tb_num: u32, tb_den: u32, duration: u64) -> Self {
1327 let (n, d) = reduce_timebase(tb_num, tb_den);
1328 NAStream { media_type: mt, id, num: 0, info: info.into_ref(), tb_num: n, tb_den: d, duration }
1330 /// Returns stream id.
1331 pub fn get_id(&self) -> u32 { self.id }
1332 /// Returns stream type.
1333 pub fn get_media_type(&self) -> StreamType { self.media_type }
1334 /// Returns stream number assigned by demuxer.
1335 pub fn get_num(&self) -> usize { self.num }
1336 /// Sets stream number.
1337 pub fn set_num(&mut self, num: usize) { self.num = num; }
1338 /// Returns codec information.
1339 pub fn get_info(&self) -> NACodecInfoRef { self.info.clone() }
1340 /// Returns stream timebase.
1341 pub fn get_timebase(&self) -> (u32, u32) { (self.tb_num, self.tb_den) }
1342 /// Sets new stream timebase.
1343 pub fn set_timebase(&mut self, tb_num: u32, tb_den: u32) {
1344 let (n, d) = reduce_timebase(tb_num, tb_den);
1348 /// Returns stream duration.
1349 pub fn get_duration(&self) -> usize { self.num }
1350 /// Converts current instance into a reference-counted one.
1351 pub fn into_ref(self) -> NAStreamRef { Arc::new(self) }
1354 impl fmt::Display for NAStream {
1355 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1356 write!(f, "({}#{} @ {}/{} - {})", self.media_type, self.id, self.tb_num, self.tb_den, self.info.get_properties())
1360 /// Side data that may accompany demuxed data.
1362 pub enum NASideData {
1363 /// Palette information.
1365 /// This side data contains a flag signalling that palette has changed since previous time and a reference to the current palette.
1366 /// Palette is stored in 8-bit RGBA format.
1367 Palette(bool, Arc<[u8; 1024]>),
1368 /// Generic user data.
1369 UserData(Arc<Vec<u8>>),
1372 /// Packet with compressed data.
1374 pub struct NAPacket {
1375 stream: NAStreamRef,
1376 /// Packet timestamp.
1378 buffer: NABufferRef<Vec<u8>>,
1381 // options: HashMap<String, NAValue<'a>>,
1382 /// Packet side data (e.g. palette for paletted formats).
1383 pub side_data: Vec<NASideData>,
1387 /// Constructs a new `NAPacket` instance.
1388 pub fn new(str: NAStreamRef, ts: NATimeInfo, kf: bool, vec: Vec<u8>) -> Self {
1389 // let mut vec: Vec<u8> = Vec::new();
1390 // vec.resize(size, 0);
1391 NAPacket { stream: str, ts, keyframe: kf, buffer: NABufferRef::new(vec), side_data: Vec::new() }
1393 /// Constructs a new `NAPacket` instance reusing a buffer reference.
1394 pub fn new_from_refbuf(str: NAStreamRef, ts: NATimeInfo, kf: bool, buffer: NABufferRef<Vec<u8>>) -> Self {
1395 NAPacket { stream: str, ts, keyframe: kf, buffer, side_data: Vec::new() }
1397 /// Returns information about the stream packet belongs to.
1398 pub fn get_stream(&self) -> NAStreamRef { self.stream.clone() }
1399 /// Returns packet timestamp.
1400 pub fn get_time_information(&self) -> NATimeInfo { self.ts }
1401 /// Returns packet presentation timestamp.
1402 pub fn get_pts(&self) -> Option<u64> { self.ts.get_pts() }
1403 /// Returns packet decoding timestamp.
1404 pub fn get_dts(&self) -> Option<u64> { self.ts.get_dts() }
1405 /// Returns packet duration.
1406 pub fn get_duration(&self) -> Option<u64> { self.ts.get_duration() }
1407 /// Reports whether this is a keyframe packet.
1408 pub fn is_keyframe(&self) -> bool { self.keyframe }
1409 /// Returns a reference to packet data.
1410 pub fn get_buffer(&self) -> NABufferRef<Vec<u8>> { self.buffer.clone() }
1411 /// Adds side data for a packet.
1412 pub fn add_side_data(&mut self, side_data: NASideData) { self.side_data.push(side_data); }
1413 /// Assigns packet to a new stream.
1414 pub fn reassign(&mut self, str: NAStreamRef, ts: NATimeInfo) {
1420 impl Drop for NAPacket {
1421 fn drop(&mut self) {}
1424 impl fmt::Display for NAPacket {
1425 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1426 let mut ostr = format!("[pkt for {} size {}", self.stream, self.buffer.len());
1427 if let Some(pts) = self.ts.pts { ostr = format!("{} pts {}", ostr, pts); }
1428 if let Some(dts) = self.ts.dts { ostr = format!("{} dts {}", ostr, dts); }
1429 if let Some(dur) = self.ts.duration { ostr = format!("{} duration {}", ostr, dur); }
1430 if self.keyframe { ostr = format!("{} kf", ostr); }
1432 write!(f, "{}", ostr)
1441 fn test_time_parse() {
1442 assert_eq!(NATimePoint::PTS(42).to_string(), "42pts");
1443 assert_eq!(NATimePoint::Milliseconds(4242000).to_string(), "1:10:42");
1444 assert_eq!(NATimePoint::Milliseconds(42424242).to_string(), "11:47:04.242");
1445 let ret = NATimePoint::from_str("42pts");
1446 assert_eq!(ret.unwrap(), NATimePoint::PTS(42));
1447 let ret = NATimePoint::from_str("1:2:3");
1448 assert_eq!(ret.unwrap(), NATimePoint::Milliseconds(3723000));
1449 let ret = NATimePoint::from_str("1:2:3.42");
1450 assert_eq!(ret.unwrap(), NATimePoint::Milliseconds(3723420));