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 }
69 /// Returns picture height.
70 pub fn get_height(&self) -> usize { self.height }
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 {
115 matches!(*self, NACodecTypeInfo::Video(_))
117 /// Reports whether the current stream is audio stream.
118 pub fn is_audio(&self) -> bool {
119 matches!(*self, NACodecTypeInfo::Audio(_))
123 impl fmt::Display for NACodecTypeInfo {
124 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
125 let ret = match *self {
126 NACodecTypeInfo::None => "".to_string(),
127 NACodecTypeInfo::Audio(fmt) => format!("{}", fmt),
128 NACodecTypeInfo::Video(fmt) => format!("{}", fmt),
134 /// Decoded video frame.
136 /// NihAV frames are stored in native type (8/16/32-bit elements) inside a single buffer.
137 /// In case of image with several components those components are stored sequentially and can be accessed in the buffer starting at corresponding component offset.
139 pub struct NAVideoBuffer<T> {
141 data: NABufferRef<Vec<T>>,
146 impl<T: Clone> NAVideoBuffer<T> {
147 /// Constructs video buffer from the provided components.
148 pub fn from_raw_parts(info: NAVideoInfo, data: NABufferRef<Vec<T>>, offs: Vec<usize>, strides: Vec<usize>) -> Self {
149 Self { info, data, offs, strides }
151 /// Returns the component offset (0 for all unavailable offsets).
152 pub fn get_offset(&self, idx: usize) -> usize {
153 if idx >= self.offs.len() { 0 }
154 else { self.offs[idx] }
156 /// Returns picture info.
157 pub fn get_info(&self) -> NAVideoInfo { self.info }
158 /// Returns an immutable reference to the data.
159 pub fn get_data(&self) -> &Vec<T> { self.data.as_ref() }
160 /// Returns a mutable reference to the data.
161 pub fn get_data_mut(&mut self) -> Option<&mut Vec<T>> { self.data.as_mut() }
162 /// Returns the number of components in picture format.
163 pub fn get_num_components(&self) -> usize { self.offs.len() }
164 /// Creates a copy of current `NAVideoBuffer`.
165 pub fn copy_buffer(&self) -> Self {
166 let mut data: Vec<T> = Vec::with_capacity(self.data.len());
167 data.clone_from(self.data.as_ref());
168 let mut offs: Vec<usize> = Vec::with_capacity(self.offs.len());
169 offs.clone_from(&self.offs);
170 let mut strides: Vec<usize> = Vec::with_capacity(self.strides.len());
171 strides.clone_from(&self.strides);
172 NAVideoBuffer { info: self.info, data: NABufferRef::new(data), offs, strides }
174 /// Returns stride (distance between subsequent lines) for the requested component.
175 pub fn get_stride(&self, idx: usize) -> usize {
176 if idx >= self.strides.len() { return 0; }
179 /// Returns requested component dimensions.
180 pub fn get_dimensions(&self, idx: usize) -> (usize, usize) {
181 get_plane_size(&self.info, idx)
183 /// Converts current instance into buffer reference.
184 pub fn into_ref(self) -> NABufferRef<Self> {
185 NABufferRef::new(self)
188 fn print_contents(&self, datatype: &str) {
189 println!("{} video buffer size {}", datatype, self.data.len());
190 println!(" format {}", self.info);
192 for off in self.offs.iter() {
197 for stride in self.strides.iter() {
198 print!(" {}", *stride);
204 /// A specialised type for reference-counted `NAVideoBuffer`.
205 pub type NAVideoBufferRef<T> = NABufferRef<NAVideoBuffer<T>>;
207 /// Decoded audio frame.
209 /// NihAV frames are stored in native type (8/16/32-bit elements) inside a single buffer.
210 /// In case of planar audio samples for each channel are stored sequentially and can be accessed in the buffer starting at corresponding channel offset.
212 pub struct NAAudioBuffer<T> {
214 data: NABufferRef<Vec<T>>,
222 impl<T: Clone> NAAudioBuffer<T> {
223 /// Returns the start position of requested channel data.
224 pub fn get_offset(&self, idx: usize) -> usize {
225 if idx >= self.offs.len() { 0 }
226 else { self.offs[idx] }
228 /// Returns the distance between the start of one channel and the next one.
229 pub fn get_stride(&self) -> usize { self.stride }
230 /// Returns the distance between the samples in one channel.
231 pub fn get_step(&self) -> usize { self.step }
232 /// Returns audio format information.
233 pub fn get_info(&self) -> NAAudioInfo { self.info }
234 /// Returns channel map.
235 pub fn get_chmap(&self) -> &NAChannelMap { &self.chmap }
236 /// Returns an immutable reference to the data.
237 pub fn get_data(&self) -> &Vec<T> { self.data.as_ref() }
238 /// Returns reference to the data.
239 pub fn get_data_ref(&self) -> NABufferRef<Vec<T>> { self.data.clone() }
240 /// Returns a mutable reference to the data.
241 pub fn get_data_mut(&mut self) -> Option<&mut Vec<T>> { self.data.as_mut() }
242 /// Clones current `NAAudioBuffer` into a new one.
243 pub fn copy_buffer(&mut self) -> Self {
244 let mut data: Vec<T> = Vec::with_capacity(self.data.len());
245 data.clone_from(self.data.as_ref());
246 let mut offs: Vec<usize> = Vec::with_capacity(self.offs.len());
247 offs.clone_from(&self.offs);
248 NAAudioBuffer { info: self.info, data: NABufferRef::new(data), offs, chmap: self.get_chmap().clone(), len: self.len, stride: self.stride, step: self.step }
250 /// Return the length of frame in samples.
251 pub fn get_length(&self) -> usize { self.len }
252 /// Truncates buffer length if possible.
254 /// In case when new length is larger than old length nothing is done.
255 pub fn truncate(&mut self, new_len: usize) {
256 self.len = self.len.min(new_len);
259 fn print_contents(&self, datatype: &str) {
260 println!("Audio buffer with {} data, stride {}, step {}", datatype, self.stride, self.step);
261 println!(" format {}", self.info);
262 println!(" channel map {}", self.chmap);
264 for off in self.offs.iter() {
271 impl NAAudioBuffer<u8> {
272 /// Constructs a new `NAAudioBuffer` instance.
273 pub fn new_from_buf(info: NAAudioInfo, data: NABufferRef<Vec<u8>>, chmap: NAChannelMap) -> Self {
274 let len = data.len() * 8 / chmap.num_channels() / (info.format.bits as usize);
276 NAAudioBuffer { info, data, chmap, offs: Vec::new(), len, stride: 0, step: 0 }
280 /// A list of possible decoded frame types.
282 pub enum NABufferType {
283 /// 8-bit video buffer.
284 Video (NAVideoBufferRef<u8>),
285 /// 16-bit video buffer (i.e. every component or packed pixel fits into 16 bits).
286 Video16 (NAVideoBufferRef<u16>),
287 /// 32-bit video buffer (i.e. every component or packed pixel fits into 32 bits).
288 Video32 (NAVideoBufferRef<u32>),
289 /// Packed video buffer.
290 VideoPacked(NAVideoBufferRef<u8>),
291 /// Audio buffer with 8-bit unsigned integer audio.
292 AudioU8 (NAAudioBuffer<u8>),
293 /// Audio buffer with 16-bit signed integer audio.
294 AudioI16 (NAAudioBuffer<i16>),
295 /// Audio buffer with 32-bit signed integer audio.
296 AudioI32 (NAAudioBuffer<i32>),
297 /// Audio buffer with 32-bit floating point audio.
298 AudioF32 (NAAudioBuffer<f32>),
299 /// Packed audio buffer.
300 AudioPacked(NAAudioBuffer<u8>),
301 /// Buffer with generic data (e.g. subtitles).
302 Data (NABufferRef<Vec<u8>>),
308 /// Returns the offset to the requested component or channel.
309 pub fn get_offset(&self, idx: usize) -> usize {
311 NABufferType::Video(ref vb) => vb.get_offset(idx),
312 NABufferType::Video16(ref vb) => vb.get_offset(idx),
313 NABufferType::Video32(ref vb) => vb.get_offset(idx),
314 NABufferType::VideoPacked(ref vb) => vb.get_offset(idx),
315 NABufferType::AudioU8(ref ab) => ab.get_offset(idx),
316 NABufferType::AudioI16(ref ab) => ab.get_offset(idx),
317 NABufferType::AudioI32(ref ab) => ab.get_offset(idx),
318 NABufferType::AudioF32(ref ab) => ab.get_offset(idx),
319 NABufferType::AudioPacked(ref ab) => ab.get_offset(idx),
323 /// Returns information for video frames.
324 pub fn get_video_info(&self) -> Option<NAVideoInfo> {
326 NABufferType::Video(ref vb) => Some(vb.get_info()),
327 NABufferType::Video16(ref vb) => Some(vb.get_info()),
328 NABufferType::Video32(ref vb) => Some(vb.get_info()),
329 NABufferType::VideoPacked(ref vb) => Some(vb.get_info()),
333 /// Returns reference to 8-bit (or packed) video buffer.
334 pub fn get_vbuf(&self) -> Option<NAVideoBufferRef<u8>> {
336 NABufferType::Video(ref vb) => Some(vb.clone()),
337 NABufferType::VideoPacked(ref vb) => Some(vb.clone()),
341 /// Returns reference to 16-bit video buffer.
342 pub fn get_vbuf16(&self) -> Option<NAVideoBufferRef<u16>> {
344 NABufferType::Video16(ref vb) => Some(vb.clone()),
348 /// Returns reference to 32-bit video buffer.
349 pub fn get_vbuf32(&self) -> Option<NAVideoBufferRef<u32>> {
351 NABufferType::Video32(ref vb) => Some(vb.clone()),
355 /// Returns information for audio frames.
356 pub fn get_audio_info(&self) -> Option<NAAudioInfo> {
358 NABufferType::AudioU8(ref ab) => Some(ab.get_info()),
359 NABufferType::AudioI16(ref ab) => Some(ab.get_info()),
360 NABufferType::AudioI32(ref ab) => Some(ab.get_info()),
361 NABufferType::AudioF32(ref ab) => Some(ab.get_info()),
362 NABufferType::AudioPacked(ref ab) => Some(ab.get_info()),
366 /// Returns audio channel map.
367 pub fn get_chmap(&self) -> Option<&NAChannelMap> {
369 NABufferType::AudioU8(ref ab) => Some(ab.get_chmap()),
370 NABufferType::AudioI16(ref ab) => Some(ab.get_chmap()),
371 NABufferType::AudioI32(ref ab) => Some(ab.get_chmap()),
372 NABufferType::AudioF32(ref ab) => Some(ab.get_chmap()),
373 NABufferType::AudioPacked(ref ab) => Some(ab.get_chmap()),
377 /// Returns audio frame duration in samples.
378 pub fn get_audio_length(&self) -> usize {
380 NABufferType::AudioU8(ref ab) => ab.get_length(),
381 NABufferType::AudioI16(ref ab) => ab.get_length(),
382 NABufferType::AudioI32(ref ab) => ab.get_length(),
383 NABufferType::AudioF32(ref ab) => ab.get_length(),
384 NABufferType::AudioPacked(ref ab) => ab.get_length(),
388 /// Truncates audio frame duration if possible.
389 pub fn truncate_audio(&mut self, len: usize) {
391 NABufferType::AudioU8(ref mut ab) => ab.truncate(len),
392 NABufferType::AudioI16(ref mut ab) => ab.truncate(len),
393 NABufferType::AudioI32(ref mut ab) => ab.truncate(len),
394 NABufferType::AudioF32(ref mut ab) => ab.truncate(len),
395 NABufferType::AudioPacked(ref mut ab) => ab.truncate(len),
399 /// Returns the distance between starts of two channels.
400 pub fn get_audio_stride(&self) -> usize {
402 NABufferType::AudioU8(ref ab) => ab.get_stride(),
403 NABufferType::AudioI16(ref ab) => ab.get_stride(),
404 NABufferType::AudioI32(ref ab) => ab.get_stride(),
405 NABufferType::AudioF32(ref ab) => ab.get_stride(),
406 NABufferType::AudioPacked(ref ab) => ab.get_stride(),
410 /// Returns the distance between two samples in one channel.
411 pub fn get_audio_step(&self) -> usize {
413 NABufferType::AudioU8(ref ab) => ab.get_step(),
414 NABufferType::AudioI16(ref ab) => ab.get_step(),
415 NABufferType::AudioI32(ref ab) => ab.get_step(),
416 NABufferType::AudioF32(ref ab) => ab.get_step(),
417 NABufferType::AudioPacked(ref ab) => ab.get_step(),
421 /// Returns reference to 8-bit (or packed) audio buffer.
422 pub fn get_abuf_u8(&self) -> Option<NAAudioBuffer<u8>> {
424 NABufferType::AudioU8(ref ab) => Some(ab.clone()),
425 NABufferType::AudioPacked(ref ab) => Some(ab.clone()),
429 /// Returns reference to 16-bit audio buffer.
430 pub fn get_abuf_i16(&self) -> Option<NAAudioBuffer<i16>> {
432 NABufferType::AudioI16(ref ab) => Some(ab.clone()),
436 /// Returns reference to 32-bit integer audio buffer.
437 pub fn get_abuf_i32(&self) -> Option<NAAudioBuffer<i32>> {
439 NABufferType::AudioI32(ref ab) => Some(ab.clone()),
443 /// Returns reference to 32-bit floating point audio buffer.
444 pub fn get_abuf_f32(&self) -> Option<NAAudioBuffer<f32>> {
446 NABufferType::AudioF32(ref ab) => Some(ab.clone()),
450 /// Prints internal buffer layout.
451 pub fn print_buffer_metadata(&self) {
453 NABufferType::Video(ref buf) => buf.print_contents("8-bit"),
454 NABufferType::Video16(ref buf) => buf.print_contents("16-bit"),
455 NABufferType::Video32(ref buf) => buf.print_contents("32-bit"),
456 NABufferType::VideoPacked(ref buf) => buf.print_contents("packed"),
457 NABufferType::AudioU8(ref buf) => buf.print_contents("8-bit unsigned integer"),
458 NABufferType::AudioI16(ref buf) => buf.print_contents("16-bit integer"),
459 NABufferType::AudioI32(ref buf) => buf.print_contents("32-bit integer"),
460 NABufferType::AudioF32(ref buf) => buf.print_contents("32-bit float"),
461 NABufferType::AudioPacked(ref buf) => buf.print_contents("packed"),
462 NABufferType::Data(ref buf) => { println!("Data buffer, len = {}", buf.len()); },
463 NABufferType::None => { println!("No buffer"); },
468 const NA_SIMPLE_VFRAME_COMPONENTS: usize = 4;
469 /// Simplified decoded frame data.
470 pub struct NASimpleVideoFrame<'a, T: Copy> {
471 /// Widths of each picture component.
472 pub width: [usize; NA_SIMPLE_VFRAME_COMPONENTS],
473 /// Heights of each picture component.
474 pub height: [usize; NA_SIMPLE_VFRAME_COMPONENTS],
475 /// Orientation (upside-down or downside-up) flag.
477 /// Strides for each component.
478 pub stride: [usize; NA_SIMPLE_VFRAME_COMPONENTS],
479 /// Start of each component.
480 pub offset: [usize; NA_SIMPLE_VFRAME_COMPONENTS],
481 /// Number of components.
482 pub components: usize,
483 /// Pointer to the picture pixel data.
484 pub data: &'a mut [T],
487 impl<'a, T:Copy> NASimpleVideoFrame<'a, T> {
488 /// Constructs a new instance of `NASimpleVideoFrame` from `NAVideoBuffer`.
489 pub fn from_video_buf(vbuf: &'a mut NAVideoBuffer<T>) -> Option<Self> {
490 let vinfo = vbuf.get_info();
491 let components = vinfo.format.components as usize;
492 if components > NA_SIMPLE_VFRAME_COMPONENTS {
495 let mut w: [usize; NA_SIMPLE_VFRAME_COMPONENTS] = [0; NA_SIMPLE_VFRAME_COMPONENTS];
496 let mut h: [usize; NA_SIMPLE_VFRAME_COMPONENTS] = [0; NA_SIMPLE_VFRAME_COMPONENTS];
497 let mut s: [usize; NA_SIMPLE_VFRAME_COMPONENTS] = [0; NA_SIMPLE_VFRAME_COMPONENTS];
498 let mut o: [usize; NA_SIMPLE_VFRAME_COMPONENTS] = [0; NA_SIMPLE_VFRAME_COMPONENTS];
499 for comp in 0..components {
500 let (width, height) = vbuf.get_dimensions(comp);
503 s[comp] = vbuf.get_stride(comp);
504 o[comp] = vbuf.get_offset(comp);
506 let flip = vinfo.flipped;
507 Some(NASimpleVideoFrame {
514 data: vbuf.data.as_mut_slice(),
519 /// A list of possible frame allocator errors.
520 #[derive(Debug,Clone,Copy,PartialEq)]
521 pub enum AllocatorError {
522 /// Requested picture dimensions are too large.
524 /// Invalid input format.
528 /// Constructs a new video buffer with requested format.
530 /// `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.
531 pub fn alloc_video_buffer(vinfo: NAVideoInfo, align: u8) -> Result<NABufferType, AllocatorError> {
532 let fmt = &vinfo.format;
533 let mut new_size: usize = 0;
534 let mut offs: Vec<usize> = Vec::new();
535 let mut strides: Vec<usize> = Vec::new();
537 for i in 0..fmt.get_num_comp() {
538 if fmt.get_chromaton(i).is_none() { return Err(AllocatorError::FormatError); }
541 let align_mod = ((1 << align) as usize) - 1;
542 let width = (vinfo.width + align_mod) & !align_mod;
543 let height = (vinfo.height + align_mod) & !align_mod;
544 let mut max_depth = 0;
545 let mut all_packed = true;
546 let mut all_bytealigned = true;
547 for i in 0..fmt.get_num_comp() {
548 let ochr = fmt.get_chromaton(i);
549 if ochr.is_none() { continue; }
550 let chr = ochr.unwrap();
551 if !chr.is_packed() {
553 } else if ((chr.get_shift() + chr.get_depth()) & 7) != 0 {
554 all_bytealigned = false;
556 max_depth = max(max_depth, chr.get_depth());
558 let unfit_elem_size = !matches!(fmt.get_elem_size(), 2 | 4);
560 //todo semi-packed like NV12
561 if fmt.is_paletted() {
562 //todo various-sized palettes?
563 let stride = vinfo.get_format().get_chromaton(0).unwrap().get_linesize(width);
564 let pic_sz = stride.checked_mul(height);
565 if pic_sz.is_none() { return Err(AllocatorError::TooLargeDimensions); }
566 let pal_size = 256 * (fmt.get_elem_size() as usize);
567 let new_size = pic_sz.unwrap().checked_add(pal_size);
568 if new_size.is_none() { return Err(AllocatorError::TooLargeDimensions); }
570 offs.push(stride * height);
571 strides.push(stride);
572 let data: Vec<u8> = vec![0; new_size.unwrap()];
573 let buf: NAVideoBuffer<u8> = NAVideoBuffer { data: NABufferRef::new(data), info: vinfo, offs, strides };
574 Ok(NABufferType::Video(buf.into_ref()))
575 } else if !all_packed {
576 for i in 0..fmt.get_num_comp() {
577 let ochr = fmt.get_chromaton(i);
578 if ochr.is_none() { continue; }
579 let chr = ochr.unwrap();
581 let stride = chr.get_linesize(width);
582 let cur_h = chr.get_height(height);
583 let cur_sz = stride.checked_mul(cur_h);
584 if cur_sz.is_none() { return Err(AllocatorError::TooLargeDimensions); }
585 let new_sz = new_size.checked_add(cur_sz.unwrap());
586 if new_sz.is_none() { return Err(AllocatorError::TooLargeDimensions); }
587 new_size = new_sz.unwrap();
588 strides.push(stride);
591 let data: Vec<u8> = vec![0; new_size];
592 let buf: NAVideoBuffer<u8> = NAVideoBuffer { data: NABufferRef::new(data), info: vinfo, offs, strides };
593 Ok(NABufferType::Video(buf.into_ref()))
594 } else if max_depth <= 16 {
595 let data: Vec<u16> = vec![0; new_size];
596 let buf: NAVideoBuffer<u16> = NAVideoBuffer { data: NABufferRef::new(data), info: vinfo, offs, strides };
597 Ok(NABufferType::Video16(buf.into_ref()))
599 let data: Vec<u32> = vec![0; new_size];
600 let buf: NAVideoBuffer<u32> = NAVideoBuffer { data: NABufferRef::new(data), info: vinfo, offs, strides };
601 Ok(NABufferType::Video32(buf.into_ref()))
603 } else if all_bytealigned || unfit_elem_size {
604 let elem_sz = fmt.get_elem_size();
605 let line_sz = width.checked_mul(elem_sz as usize);
606 if line_sz.is_none() { return Err(AllocatorError::TooLargeDimensions); }
607 let new_sz = line_sz.unwrap().checked_mul(height);
608 if new_sz.is_none() { return Err(AllocatorError::TooLargeDimensions); }
609 new_size = new_sz.unwrap();
610 let data: Vec<u8> = vec![0; new_size];
611 strides.push(line_sz.unwrap());
612 let buf: NAVideoBuffer<u8> = NAVideoBuffer { data: NABufferRef::new(data), info: vinfo, offs, strides };
613 Ok(NABufferType::VideoPacked(buf.into_ref()))
615 let elem_sz = fmt.get_elem_size();
616 let new_sz = width.checked_mul(height);
617 if new_sz.is_none() { return Err(AllocatorError::TooLargeDimensions); }
618 new_size = new_sz.unwrap();
621 let data: Vec<u16> = vec![0; new_size];
623 let buf: NAVideoBuffer<u16> = NAVideoBuffer { data: NABufferRef::new(data), info: vinfo, offs, strides };
624 Ok(NABufferType::Video16(buf.into_ref()))
627 let data: Vec<u32> = vec![0; new_size];
629 let buf: NAVideoBuffer<u32> = NAVideoBuffer { data: NABufferRef::new(data), info: vinfo, offs, strides };
630 Ok(NABufferType::Video32(buf.into_ref()))
637 /// Constructs a new audio buffer for the requested format and length.
638 #[allow(clippy::collapsible_if)]
639 #[allow(clippy::collapsible_else_if)]
640 pub fn alloc_audio_buffer(ainfo: NAAudioInfo, nsamples: usize, chmap: NAChannelMap) -> Result<NABufferType, AllocatorError> {
641 let mut offs: Vec<usize> = Vec::new();
642 if ainfo.format.is_planar() || ((ainfo.format.get_bits() % 8) == 0) {
643 let len = nsamples.checked_mul(ainfo.channels as usize);
644 if len.is_none() { return Err(AllocatorError::TooLargeDimensions); }
645 let length = len.unwrap();
648 if ainfo.format.is_planar() {
651 for i in 0..ainfo.channels {
652 offs.push((i as usize) * stride);
656 step = ainfo.channels as usize;
657 for i in 0..ainfo.channels {
658 offs.push(i as usize);
661 if ainfo.format.is_float() {
662 if ainfo.format.get_bits() == 32 {
663 let data: Vec<f32> = vec![0.0; length];
664 let buf: NAAudioBuffer<f32> = NAAudioBuffer { data: NABufferRef::new(data), info: ainfo, offs, chmap, len: nsamples, stride, step };
665 Ok(NABufferType::AudioF32(buf))
667 Err(AllocatorError::TooLargeDimensions)
670 if ainfo.format.get_bits() == 8 && !ainfo.format.is_signed() {
671 let data: Vec<u8> = vec![0; length];
672 let buf: NAAudioBuffer<u8> = NAAudioBuffer { data: NABufferRef::new(data), info: ainfo, offs, chmap, len: nsamples, stride, step };
673 Ok(NABufferType::AudioU8(buf))
674 } else if ainfo.format.get_bits() == 16 && ainfo.format.is_signed() {
675 let data: Vec<i16> = vec![0; length];
676 let buf: NAAudioBuffer<i16> = NAAudioBuffer { data: NABufferRef::new(data), info: ainfo, offs, chmap, len: nsamples, stride, step };
677 Ok(NABufferType::AudioI16(buf))
678 } else if ainfo.format.get_bits() == 32 && ainfo.format.is_signed() {
679 let data: Vec<i32> = vec![0; length];
680 let buf: NAAudioBuffer<i32> = NAAudioBuffer { data: NABufferRef::new(data), info: ainfo, offs, chmap, len: nsamples, stride, step };
681 Ok(NABufferType::AudioI32(buf))
683 Err(AllocatorError::TooLargeDimensions)
687 let len = nsamples.checked_mul(ainfo.channels as usize);
688 if len.is_none() { return Err(AllocatorError::TooLargeDimensions); }
689 let length = ainfo.format.get_audio_size(len.unwrap() as u64);
690 let data: Vec<u8> = vec![0; length];
691 let buf: NAAudioBuffer<u8> = NAAudioBuffer { data: NABufferRef::new(data), info: ainfo, offs, chmap, len: nsamples, stride: 0, step: 0 };
692 Ok(NABufferType::AudioPacked(buf))
696 /// Constructs a new buffer for generic data.
697 pub fn alloc_data_buffer(size: usize) -> Result<NABufferType, AllocatorError> {
698 let data: Vec<u8> = vec![0; size];
699 let buf: NABufferRef<Vec<u8>> = NABufferRef::new(data);
700 Ok(NABufferType::Data(buf))
703 /// Creates a clone of current buffer.
704 pub fn copy_buffer(buf: &NABufferType) -> NABufferType {
708 /// Video frame pool.
710 /// This structure allows codec to effectively reuse old frames instead of allocating and de-allocating frames every time.
711 /// Caller can also reserve some frames for its own purposes e.g. display queue.
712 pub struct NAVideoBufferPool<T:Copy> {
713 pool: Vec<NAVideoBufferRef<T>>,
718 impl<T:Copy> NAVideoBufferPool<T> {
719 /// Constructs a new `NAVideoBufferPool` instance.
720 pub fn new(max_len: usize) -> Self {
722 pool: Vec::with_capacity(max_len),
727 /// Sets the number of buffers reserved for the user.
728 pub fn set_dec_bufs(&mut self, add_len: usize) {
729 self.add_len = add_len;
731 /// Returns an unused buffer from the pool.
732 pub fn get_free(&mut self) -> Option<NAVideoBufferRef<T>> {
733 for e in self.pool.iter() {
734 if e.get_num_refs() == 1 {
735 return Some(e.clone());
740 /// Clones provided frame data into a free pool frame.
741 pub fn get_copy(&mut self, rbuf: &NAVideoBufferRef<T>) -> Option<NAVideoBufferRef<T>> {
742 let mut dbuf = self.get_free()?;
743 dbuf.data.copy_from_slice(&rbuf.data);
746 /// Clears the pool from all frames.
747 pub fn reset(&mut self) {
750 /// Returns the number of frames currently in use.
751 pub fn get_num_used(&self) -> usize {
752 self.pool.iter().filter(|el| el.get_num_refs() != 1).count()
754 /// Adds a manually allocated frame to the pool.
755 pub fn add_frame(&mut self, buf: NAVideoBufferRef<T>) {
758 /// Returns current video format (if available).
759 pub fn get_info(&self) -> Option<NAVideoInfo> {
760 if !self.pool.is_empty() {
761 Some(self.pool[0].get_info())
768 impl NAVideoBufferPool<u8> {
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::Video(buf) = vbuf {
778 } else if let NABufferType::VideoPacked(buf) = vbuf {
781 return Err(AllocatorError::FormatError);
788 impl NAVideoBufferPool<u16> {
789 /// Allocates the target amount of video frames using [`alloc_video_buffer`].
791 /// [`alloc_video_buffer`]: ./fn.alloc_video_buffer.html
792 pub fn prealloc_video(&mut self, vinfo: NAVideoInfo, align: u8) -> Result<(), AllocatorError> {
793 let nbufs = self.max_len + self.add_len - self.pool.len();
795 let vbuf = alloc_video_buffer(vinfo, align)?;
796 if let NABufferType::Video16(buf) = vbuf {
799 return Err(AllocatorError::FormatError);
806 impl NAVideoBufferPool<u32> {
807 /// Allocates the target amount of video frames using [`alloc_video_buffer`].
809 /// [`alloc_video_buffer`]: ./fn.alloc_video_buffer.html
810 pub fn prealloc_video(&mut self, vinfo: NAVideoInfo, align: u8) -> Result<(), AllocatorError> {
811 let nbufs = self.max_len + self.add_len - self.pool.len();
813 let vbuf = alloc_video_buffer(vinfo, align)?;
814 if let NABufferType::Video32(buf) = vbuf {
817 return Err(AllocatorError::FormatError);
824 /// Information about codec contained in a stream.
827 pub struct NACodecInfo {
829 properties: NACodecTypeInfo,
830 extradata: Option<Arc<Vec<u8>>>,
833 /// A specialised type for reference-counted `NACodecInfo`.
834 pub type NACodecInfoRef = Arc<NACodecInfo>;
837 /// Constructs a new instance of `NACodecInfo`.
838 pub fn new(name: &'static str, p: NACodecTypeInfo, edata: Option<Vec<u8>>) -> Self {
839 NACodecInfo { name, properties: p, extradata: edata.map(Arc::new) }
841 /// Constructs a new reference-counted instance of `NACodecInfo`.
842 pub fn new_ref(name: &'static str, p: NACodecTypeInfo, edata: Option<Arc<Vec<u8>>>) -> Self {
843 NACodecInfo { name, properties: p, extradata: edata }
845 /// Converts current instance into a reference-counted one.
846 pub fn into_ref(self) -> NACodecInfoRef { Arc::new(self) }
847 /// Returns codec information.
848 pub fn get_properties(&self) -> NACodecTypeInfo { self.properties }
849 /// Returns additional initialisation data required by the codec.
850 pub fn get_extradata(&self) -> Option<Arc<Vec<u8>>> {
851 if let Some(ref vec) = self.extradata { return Some(vec.clone()); }
854 /// Returns codec name.
855 pub fn get_name(&self) -> &'static str { self.name }
856 /// Reports whether it is a video codec.
857 pub fn is_video(&self) -> bool {
858 if let NACodecTypeInfo::Video(_) = self.properties { return true; }
861 /// Reports whether it is an audio codec.
862 pub fn is_audio(&self) -> bool {
863 if let NACodecTypeInfo::Audio(_) = self.properties { return true; }
866 /// Constructs a new empty reference-counted instance of `NACodecInfo`.
867 pub fn new_dummy() -> Arc<Self> {
868 Arc::new(DUMMY_CODEC_INFO)
870 /// Updates codec infomation.
871 pub fn replace_info(&self, p: NACodecTypeInfo) -> Arc<Self> {
872 Arc::new(NACodecInfo { name: self.name, properties: p, extradata: self.extradata.clone() })
876 impl Default for NACodecInfo {
877 fn default() -> Self { DUMMY_CODEC_INFO }
880 impl fmt::Display for NACodecInfo {
881 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
882 let edata = match self.extradata.clone() {
883 None => "no extradata".to_string(),
884 Some(v) => format!("{} byte(s) of extradata", v.len()),
886 write!(f, "{}: {} {}", self.name, self.properties, edata)
890 /// Default empty codec information.
891 pub const DUMMY_CODEC_INFO: NACodecInfo = NACodecInfo {
893 properties: NACodecTypeInfo::None,
896 /// A list of recognized frame types.
897 #[derive(Debug,Clone,Copy,PartialEq)]
900 /// Intra frame type.
902 /// Inter frame type.
904 /// Bidirectionally predicted frame.
908 /// When such frame is encountered then last frame should be used again if it is needed.
910 /// Some other frame type.
914 impl fmt::Display for FrameType {
915 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
917 FrameType::I => write!(f, "I"),
918 FrameType::P => write!(f, "P"),
919 FrameType::B => write!(f, "B"),
920 FrameType::Skip => write!(f, "skip"),
921 FrameType::Other => write!(f, "x"),
926 /// Timestamp information.
927 #[derive(Debug,Clone,Copy)]
928 pub struct NATimeInfo {
929 /// Presentation timestamp.
930 pub pts: Option<u64>,
931 /// Decode timestamp.
932 pub dts: Option<u64>,
933 /// Duration (in timebase units).
934 pub duration: Option<u64>,
935 /// Timebase numerator.
937 /// Timebase denominator.
942 /// Constructs a new `NATimeInfo` instance.
943 pub fn new(pts: Option<u64>, dts: Option<u64>, duration: Option<u64>, tb_num: u32, tb_den: u32) -> Self {
944 NATimeInfo { pts, dts, duration, tb_num, tb_den }
946 /// Returns presentation timestamp.
947 pub fn get_pts(&self) -> Option<u64> { self.pts }
948 /// Returns decoding timestamp.
949 pub fn get_dts(&self) -> Option<u64> { self.dts }
950 /// Returns duration.
951 pub fn get_duration(&self) -> Option<u64> { self.duration }
952 /// Sets new presentation timestamp.
953 pub fn set_pts(&mut self, pts: Option<u64>) { self.pts = pts; }
954 /// Sets new decoding timestamp.
955 pub fn set_dts(&mut self, dts: Option<u64>) { self.dts = dts; }
956 /// Sets new duration.
957 pub fn set_duration(&mut self, dur: Option<u64>) { self.duration = dur; }
959 /// Converts time in given scale into timestamp in given base.
960 #[allow(clippy::collapsible_if)]
961 #[allow(clippy::collapsible_else_if)]
962 pub fn time_to_ts(time: u64, base: u64, tb_num: u32, tb_den: u32) -> u64 {
963 let tb_num = u64::from(tb_num);
964 let tb_den = u64::from(tb_den);
965 let tmp = time.checked_mul(tb_den);
966 if let Some(tmp) = tmp {
970 let coarse = time / tb_num;
971 if let Some(tmp) = coarse.checked_mul(tb_den) {
974 (coarse / base) * tb_den
977 let coarse = time / base;
978 if let Some(tmp) = coarse.checked_mul(tb_den) {
981 (coarse / tb_num) * tb_den
986 /// Converts timestamp in given base into time in given scale.
987 pub fn ts_to_time(ts: u64, base: u64, tb_num: u32, tb_den: u32) -> u64 {
988 let tb_num = u64::from(tb_num);
989 let tb_den = u64::from(tb_den);
990 let tmp = ts.checked_mul(base);
991 if let Some(tmp) = tmp {
992 let tmp2 = tmp.checked_mul(tb_num);
993 if let Some(tmp2) = tmp2 {
996 (tmp / tb_den) * tb_num
999 let tmp = ts.checked_mul(tb_num);
1000 if let Some(tmp) = tmp {
1001 (tmp / tb_den) * base
1003 (ts / tb_den) * base * tb_num
1007 fn get_cur_ts(&self) -> u64 { self.pts.unwrap_or_else(|| self.dts.unwrap_or(0)) }
1008 fn get_cur_millis(&self) -> u64 {
1009 let ts = self.get_cur_ts();
1010 Self::ts_to_time(ts, 1000, self.tb_num, self.tb_den)
1012 /// Checks whether the current time information is earler than provided reference time.
1013 pub fn less_than(&self, time: NATimePoint) -> bool {
1014 if self.pts.is_none() && self.dts.is_none() {
1018 NATimePoint::PTS(rpts) => self.get_cur_ts() < rpts,
1019 NATimePoint::Milliseconds(ms) => self.get_cur_millis() < ms,
1020 NATimePoint::None => false,
1023 /// Checks whether the current time information is the same as provided reference time.
1024 pub fn equal(&self, time: NATimePoint) -> bool {
1025 if self.pts.is_none() && self.dts.is_none() {
1026 return time == NATimePoint::None;
1029 NATimePoint::PTS(rpts) => self.get_cur_ts() == rpts,
1030 NATimePoint::Milliseconds(ms) => self.get_cur_millis() == ms,
1031 NATimePoint::None => false,
1036 /// Time information for specifying durations or seek positions.
1037 #[derive(Clone,Copy,Debug,PartialEq,Default)]
1038 pub enum NATimePoint {
1039 /// Time in milliseconds.
1041 /// Stream timestamp.
1043 /// No time information present.
1048 impl fmt::Display for NATimePoint {
1049 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1051 NATimePoint::Milliseconds(millis) => {
1052 let tot_s = millis / 1000;
1053 let ms = millis % 1000;
1056 return write!(f, "{}.{:03}", tot_s, ms);
1058 return write!(f, "{}", tot_s);
1061 let tot_m = tot_s / 60;
1065 return write!(f, "{}:{:02}.{:03}", tot_m, s, ms);
1067 return write!(f, "{}:{:02}", tot_m, s);
1073 write!(f, "{}:{:02}:{:02}.{:03}", h, m, s, ms)
1075 write!(f, "{}:{:02}:{:02}", h, m, s)
1078 NATimePoint::PTS(pts) => {
1079 write!(f, "{}pts", pts)
1081 NATimePoint::None => {
1088 impl FromStr for NATimePoint {
1089 type Err = FormatParseError;
1091 /// Parses the string into time information.
1093 /// Accepted formats are `<u64>pts`, `<u64>ms` or `[hh:][mm:]ss[.ms]`.
1094 fn from_str(s: &str) -> Result<Self, Self::Err> {
1096 return Err(FormatParseError {});
1098 if !s.ends_with("pts") {
1099 if s.ends_with("ms") {
1100 let str_b = s.as_bytes();
1101 let num = std::str::from_utf8(&str_b[..str_b.len() - 2]).unwrap();
1102 let ret = num.parse::<u64>();
1103 if let Ok(val) = ret {
1104 return Ok(NATimePoint::Milliseconds(val));
1106 return Err(FormatParseError {});
1109 let mut parts = s.split(':');
1111 let mut mins = None;
1112 let mut secs = parts.next();
1113 if let Some(part) = parts.next() {
1114 std::mem::swap(&mut mins, &mut secs);
1117 if let Some(part) = parts.next() {
1118 std::mem::swap(&mut hrs, &mut mins);
1119 std::mem::swap(&mut mins, &mut secs);
1122 if parts.next().is_some() {
1123 return Err(FormatParseError {});
1125 let hours = if let Some(val) = hrs {
1126 let ret = val.parse::<u64>();
1127 if ret.is_err() { return Err(FormatParseError {}); }
1128 let val = ret.unwrap();
1129 if val > 1000 { return Err(FormatParseError {}); }
1132 let minutes = if let Some(val) = mins {
1133 let ret = val.parse::<u64>();
1134 if ret.is_err() { return Err(FormatParseError {}); }
1135 let val = ret.unwrap();
1136 if val >= 60 { return Err(FormatParseError {}); }
1139 let (seconds, millis) = if let Some(val) = secs {
1140 let mut parts = val.split('.');
1141 let ret = parts.next().unwrap().parse::<u64>();
1142 if ret.is_err() { return Err(FormatParseError {}); }
1143 let seconds = ret.unwrap();
1144 if mins.is_some() && seconds >= 60 { return Err(FormatParseError {}); }
1145 let millis = if let Some(val) = parts.next() {
1148 for ch in val.chars() {
1149 if ch.is_ascii_digit() {
1150 mval = mval * 10 + u64::from((ch as u8) - b'0');
1152 if base > 3 { break; }
1154 return Err(FormatParseError {});
1164 } else { unreachable!(); };
1165 let tot_secs = hours * 60 * 60 + minutes * 60 + seconds;
1166 Ok(NATimePoint::Milliseconds(tot_secs * 1000 + millis))
1168 let str_b = s.as_bytes();
1169 let num = std::str::from_utf8(&str_b[..str_b.len() - 3]).unwrap();
1170 let ret = num.parse::<u64>();
1171 if let Ok(val) = ret {
1172 Ok(NATimePoint::PTS(val))
1174 Err(FormatParseError {})
1180 /// Decoded frame information.
1183 pub struct NAFrame {
1184 /// Frame timestamp.
1188 buffer: NABufferType,
1189 info: NACodecInfoRef,
1191 pub frame_type: FrameType,
1194 // options: HashMap<String, NAValue>,
1197 /// A specialised type for reference-counted `NAFrame`.
1198 pub type NAFrameRef = Arc<NAFrame>;
1200 fn get_plane_size(info: &NAVideoInfo, idx: usize) -> (usize, usize) {
1201 let chromaton = info.get_format().get_chromaton(idx);
1202 if chromaton.is_none() { return (0, 0); }
1203 let (hs, vs) = chromaton.unwrap().get_subsampling();
1204 let w = (info.get_width() + ((1 << hs) - 1)) >> hs;
1205 let h = (info.get_height() + ((1 << vs) - 1)) >> vs;
1210 /// Constructs a new `NAFrame` instance.
1211 pub fn new(ts: NATimeInfo,
1214 info: NACodecInfoRef,
1215 /*options: HashMap<String, NAValue>,*/
1216 buffer: NABufferType) -> Self {
1217 NAFrame { ts, id: 0, buffer, info, frame_type: ftype, key: keyframe/*, options*/ }
1219 /// Returns frame format information.
1220 pub fn get_info(&self) -> NACodecInfoRef { self.info.clone() }
1221 /// Returns frame type.
1222 pub fn get_frame_type(&self) -> FrameType { self.frame_type }
1223 /// Reports whether the frame is a keyframe.
1224 pub fn is_keyframe(&self) -> bool { self.key }
1225 /// Sets new frame type.
1226 pub fn set_frame_type(&mut self, ftype: FrameType) { self.frame_type = ftype; }
1227 /// Sets keyframe flag.
1228 pub fn set_keyframe(&mut self, key: bool) { self.key = key; }
1229 /// Returns frame timestamp.
1230 pub fn get_time_information(&self) -> NATimeInfo { self.ts }
1231 /// Returns frame presentation time.
1232 pub fn get_pts(&self) -> Option<u64> { self.ts.get_pts() }
1233 /// Returns frame decoding time.
1234 pub fn get_dts(&self) -> Option<u64> { self.ts.get_dts() }
1235 /// Returns picture ID.
1236 pub fn get_id(&self) -> i64 { self.id }
1237 /// Returns frame display duration.
1238 pub fn get_duration(&self) -> Option<u64> { self.ts.get_duration() }
1239 /// Sets new presentation timestamp.
1240 pub fn set_pts(&mut self, pts: Option<u64>) { self.ts.set_pts(pts); }
1241 /// Sets new decoding timestamp.
1242 pub fn set_dts(&mut self, dts: Option<u64>) { self.ts.set_dts(dts); }
1243 /// Sets new picture ID.
1244 pub fn set_id(&mut self, id: i64) { self.id = id; }
1245 /// Sets new duration.
1246 pub fn set_duration(&mut self, dur: Option<u64>) { self.ts.set_duration(dur); }
1248 /// Returns a reference to the frame data.
1249 pub fn get_buffer(&self) -> NABufferType { self.buffer.clone() }
1251 /// Converts current instance into a reference-counted one.
1252 pub fn into_ref(self) -> NAFrameRef { Arc::new(self) }
1254 /// Creates new frame with metadata from `NAPacket`.
1255 pub fn new_from_pkt(pkt: &NAPacket, info: NACodecInfoRef, buf: NABufferType) -> NAFrame {
1256 NAFrame::new(pkt.ts, FrameType::Other, pkt.keyframe, info, /*HashMap::new(),*/ buf)
1260 impl fmt::Display for NAFrame {
1261 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1262 let mut ostr = format!("frame type {}", self.frame_type);
1263 if let Some(pts) = self.ts.pts { ostr = format!("{} pts {}", ostr, pts); }
1264 if let Some(dts) = self.ts.dts { ostr = format!("{} dts {}", ostr, dts); }
1265 if let Some(dur) = self.ts.duration { ostr = format!("{} duration {}", ostr, dur); }
1266 if self.key { ostr = format!("{} kf", ostr); }
1267 write!(f, "[{}]", ostr)
1271 /// A list of possible stream types.
1272 #[derive(Debug,Clone,Copy,PartialEq)]
1274 pub enum StreamType {
1281 /// Any data stream (or might be an unrecognized audio/video stream).
1283 /// Nonexistent stream.
1287 impl fmt::Display for StreamType {
1288 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1290 StreamType::Video => write!(f, "Video"),
1291 StreamType::Audio => write!(f, "Audio"),
1292 StreamType::Subtitles => write!(f, "Subtitles"),
1293 StreamType::Data => write!(f, "Data"),
1294 StreamType::None => write!(f, "-"),
1302 pub struct NAStream {
1303 media_type: StreamType,
1307 info: NACodecInfoRef,
1308 /// Timebase numerator.
1310 /// Timebase denominator.
1312 /// Duration in timebase units (zero if not available).
1316 /// A specialised reference-counted `NAStream` type.
1317 pub type NAStreamRef = Arc<NAStream>;
1319 /// Downscales the timebase by its greatest common denominator.
1320 #[allow(clippy::comparison_chain)]
1321 pub fn reduce_timebase(tb_num: u32, tb_den: u32) -> (u32, u32) {
1322 if tb_num == 0 { return (tb_num, tb_den); }
1323 if (tb_den % tb_num) == 0 { return (1, tb_den / tb_num); }
1329 if a > b { a -= b; }
1330 else if b > a { b -= a; }
1333 (tb_num / a, tb_den / a)
1337 /// Constructs a new `NAStream` instance.
1338 pub fn new(mt: StreamType, id: u32, info: NACodecInfo, tb_num: u32, tb_den: u32, duration: u64) -> Self {
1339 let (n, d) = reduce_timebase(tb_num, tb_den);
1340 NAStream { media_type: mt, id, num: 0, info: info.into_ref(), tb_num: n, tb_den: d, duration }
1342 /// Returns stream id.
1343 pub fn get_id(&self) -> u32 { self.id }
1344 /// Returns stream type.
1345 pub fn get_media_type(&self) -> StreamType { self.media_type }
1346 /// Returns stream number assigned by demuxer.
1347 pub fn get_num(&self) -> usize { self.num }
1348 /// Sets stream number.
1349 pub fn set_num(&mut self, num: usize) { self.num = num; }
1350 /// Returns codec information.
1351 pub fn get_info(&self) -> NACodecInfoRef { self.info.clone() }
1352 /// Returns stream timebase.
1353 pub fn get_timebase(&self) -> (u32, u32) { (self.tb_num, self.tb_den) }
1354 /// Sets new stream timebase.
1355 pub fn set_timebase(&mut self, tb_num: u32, tb_den: u32) {
1356 let (n, d) = reduce_timebase(tb_num, tb_den);
1360 /// Returns stream duration.
1361 pub fn get_duration(&self) -> u64 { self.duration }
1362 /// Constructs a new timestamp.
1363 pub fn make_ts(&self, pts: Option<u64>, dts: Option<u64>, duration: Option<u64>) -> NATimeInfo {
1364 NATimeInfo::new(pts, dts, duration, self.tb_num, self.tb_den)
1366 /// Converts current instance into a reference-counted one.
1367 pub fn into_ref(self) -> NAStreamRef { Arc::new(self) }
1370 impl fmt::Display for NAStream {
1371 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1372 write!(f, "({}#{} @ {}/{} - {})", self.media_type, self.id, self.tb_num, self.tb_den, self.info.get_properties())
1376 /// Side data that may accompany demuxed data.
1378 pub enum NASideData {
1379 /// Palette information.
1381 /// This side data contains a flag signalling that palette has changed since previous time and a reference to the current palette.
1382 /// Palette is stored in 8-bit RGBA format.
1383 Palette(bool, Arc<[u8; 1024]>),
1384 /// Generic user data.
1385 UserData(Arc<Vec<u8>>),
1388 /// Packet with compressed data.
1390 pub struct NAPacket {
1391 stream: NAStreamRef,
1392 /// Packet timestamp.
1394 buffer: NABufferRef<Vec<u8>>,
1397 // options: HashMap<String, NAValue<'a>>,
1398 /// Packet side data (e.g. palette for paletted formats).
1399 pub side_data: Vec<NASideData>,
1403 /// Constructs a new `NAPacket` instance.
1404 pub fn new(stream: NAStreamRef, ts: NATimeInfo, kf: bool, vec: Vec<u8>) -> Self {
1405 // let mut vec: Vec<u8> = Vec::new();
1406 // vec.resize(size, 0);
1407 NAPacket { stream, ts, keyframe: kf, buffer: NABufferRef::new(vec), side_data: Vec::new() }
1409 /// Constructs a new `NAPacket` instance reusing a buffer reference.
1410 pub fn new_from_refbuf(stream: NAStreamRef, ts: NATimeInfo, kf: bool, buffer: NABufferRef<Vec<u8>>) -> Self {
1411 NAPacket { stream, ts, keyframe: kf, buffer, side_data: Vec::new() }
1413 /// Returns information about the stream packet belongs to.
1414 pub fn get_stream(&self) -> NAStreamRef { self.stream.clone() }
1415 /// Returns packet timestamp.
1416 pub fn get_time_information(&self) -> NATimeInfo { self.ts }
1417 /// Returns packet presentation timestamp.
1418 pub fn get_pts(&self) -> Option<u64> { self.ts.get_pts() }
1419 /// Returns packet decoding timestamp.
1420 pub fn get_dts(&self) -> Option<u64> { self.ts.get_dts() }
1421 /// Returns packet duration.
1422 pub fn get_duration(&self) -> Option<u64> { self.ts.get_duration() }
1423 /// Reports whether this is a keyframe packet.
1424 pub fn is_keyframe(&self) -> bool { self.keyframe }
1425 /// Returns a reference to packet data.
1426 pub fn get_buffer(&self) -> NABufferRef<Vec<u8>> { self.buffer.clone() }
1427 /// Adds side data for a packet.
1428 pub fn add_side_data(&mut self, side_data: NASideData) { self.side_data.push(side_data); }
1429 /// Assigns packet to a new stream.
1430 pub fn reassign(&mut self, stream: NAStreamRef, ts: NATimeInfo) {
1431 self.stream = stream;
1436 impl Drop for NAPacket {
1437 fn drop(&mut self) {}
1440 impl fmt::Display for NAPacket {
1441 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1442 let mut ostr = format!("[pkt for {} size {}", self.stream, self.buffer.len());
1443 if let Some(pts) = self.ts.pts { ostr = format!("{} pts {}", ostr, pts); }
1444 if let Some(dts) = self.ts.dts { ostr = format!("{} dts {}", ostr, dts); }
1445 if let Some(dur) = self.ts.duration { ostr = format!("{} duration {}", ostr, dur); }
1446 if self.keyframe { ostr = format!("{} kf", ostr); }
1448 write!(f, "{}", ostr)
1452 /// Packet with a piece of data for a raw stream.
1453 pub struct NARawData {
1454 stream: NAStreamRef,
1455 buffer: NABufferRef<Vec<u8>>,
1459 /// Constructs a new `NARawData` instance.
1460 pub fn new(stream: NAStreamRef, vec: Vec<u8>) -> Self {
1461 Self { stream, buffer: NABufferRef::new(vec) }
1463 /// Constructs a new `NARawData` instance reusing a buffer reference.
1464 pub fn new_from_refbuf(stream: NAStreamRef, buffer: NABufferRef<Vec<u8>>) -> Self {
1465 Self { stream, buffer }
1467 /// Returns information about the stream this data belongs to.
1468 pub fn get_stream(&self) -> NAStreamRef { self.stream.clone() }
1469 /// Returns a reference to packet data.
1470 pub fn get_buffer(&self) -> NABufferRef<Vec<u8>> { self.buffer.clone() }
1471 /// Assigns raw data to a new stream.
1472 pub fn reassign(&mut self, stream: NAStreamRef) {
1473 self.stream = stream;
1477 impl fmt::Display for NARawData {
1478 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1479 write!(f, "[raw data for {} size {}]", self.stream, self.buffer.len())
1488 fn test_time_parse() {
1489 assert_eq!(NATimePoint::PTS(42).to_string(), "42pts");
1490 assert_eq!(NATimePoint::Milliseconds(4242000).to_string(), "1:10:42");
1491 assert_eq!(NATimePoint::Milliseconds(42424242).to_string(), "11:47:04.242");
1492 let ret = NATimePoint::from_str("42pts");
1493 assert_eq!(ret.unwrap(), NATimePoint::PTS(42));
1494 let ret = NATimePoint::from_str("1:2:3");
1495 assert_eq!(ret.unwrap(), NATimePoint::Milliseconds(3723000));
1496 let ret = NATimePoint::from_str("1:2:3.42");
1497 assert_eq!(ret.unwrap(), NATimePoint::Milliseconds(3723420));