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::*;
9 /// Audio stream information.
11 #[derive(Clone,Copy,PartialEq)]
12 pub struct NAAudioInfo {
15 /// Number of channels.
17 /// Audio sample format.
18 pub format: NASoniton,
19 /// Length of one audio block in samples.
24 /// Constructs a new `NAAudioInfo` instance.
25 pub fn new(sr: u32, ch: u8, fmt: NASoniton, bl: usize) -> Self {
26 NAAudioInfo { sample_rate: sr, channels: ch, format: fmt, block_len: bl }
28 /// Returns audio sample rate.
29 pub fn get_sample_rate(&self) -> u32 { self.sample_rate }
30 /// Returns the number of channels.
31 pub fn get_channels(&self) -> u8 { self.channels }
32 /// Returns sample format.
33 pub fn get_format(&self) -> NASoniton { self.format }
34 /// Returns one audio block duration in samples.
35 pub fn get_block_len(&self) -> usize { self.block_len }
38 impl fmt::Display for NAAudioInfo {
39 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
40 write!(f, "{} Hz, {} ch", self.sample_rate, self.channels)
44 /// Video stream information.
46 #[derive(Clone,Copy,PartialEq)]
47 pub struct NAVideoInfo {
52 /// Picture is stored downside up.
54 /// Picture pixel format.
55 pub format: NAPixelFormaton,
59 /// Constructs a new `NAVideoInfo` instance.
60 pub fn new(w: usize, h: usize, flip: bool, fmt: NAPixelFormaton) -> Self {
61 NAVideoInfo { width: w, height: h, flipped: flip, format: fmt }
63 /// Returns picture width.
64 pub fn get_width(&self) -> usize { self.width as usize }
65 /// Returns picture height.
66 pub fn get_height(&self) -> usize { self.height as usize }
67 /// Returns picture orientation.
68 pub fn is_flipped(&self) -> bool { self.flipped }
69 /// Returns picture pixel format.
70 pub fn get_format(&self) -> NAPixelFormaton { self.format }
71 /// Sets new picture width.
72 pub fn set_width(&mut self, w: usize) { self.width = w; }
73 /// Sets new picture height.
74 pub fn set_height(&mut self, h: usize) { self.height = h; }
77 impl fmt::Display for NAVideoInfo {
78 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
79 write!(f, "{}x{}", self.width, self.height)
83 /// A list of possible stream information types.
84 #[derive(Clone,Copy,PartialEq)]
85 pub enum NACodecTypeInfo {
88 /// Audio codec information.
90 /// Video codec information.
94 impl NACodecTypeInfo {
95 /// Returns video stream information.
96 pub fn get_video_info(&self) -> Option<NAVideoInfo> {
98 NACodecTypeInfo::Video(vinfo) => Some(vinfo),
102 /// Returns audio stream information.
103 pub fn get_audio_info(&self) -> Option<NAAudioInfo> {
105 NACodecTypeInfo::Audio(ainfo) => Some(ainfo),
109 /// Reports whether the current stream is video stream.
110 pub fn is_video(&self) -> bool {
112 NACodecTypeInfo::Video(_) => true,
116 /// Reports whether the current stream is audio stream.
117 pub fn is_audio(&self) -> bool {
119 NACodecTypeInfo::Audio(_) => true,
125 impl fmt::Display for NACodecTypeInfo {
126 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
127 let ret = match *self {
128 NACodecTypeInfo::None => "".to_string(),
129 NACodecTypeInfo::Audio(fmt) => format!("{}", fmt),
130 NACodecTypeInfo::Video(fmt) => format!("{}", fmt),
136 /// Decoded video frame.
138 /// NihAV frames are stored in native type (8/16/32-bit elements) inside a single buffer.
139 /// In case of image with several components those components are stored sequentially and can be accessed in the buffer starting at corresponding component offset.
141 pub struct NAVideoBuffer<T> {
143 data: NABufferRef<Vec<T>>,
148 impl<T: Clone> NAVideoBuffer<T> {
149 /// Returns the component offset (0 for all unavailable offsets).
150 pub fn get_offset(&self, idx: usize) -> usize {
151 if idx >= self.offs.len() { 0 }
152 else { self.offs[idx] }
154 /// Returns picture info.
155 pub fn get_info(&self) -> NAVideoInfo { self.info }
156 /// Returns an immutable reference to the data.
157 pub fn get_data(&self) -> &Vec<T> { self.data.as_ref() }
158 /// Returns a mutable reference to the data.
159 pub fn get_data_mut(&mut self) -> Option<&mut Vec<T>> { self.data.as_mut() }
160 /// Returns the number of components in picture format.
161 pub fn get_num_components(&self) -> usize { self.offs.len() }
162 /// Creates a copy of current `NAVideoBuffer`.
163 pub fn copy_buffer(&mut self) -> Self {
164 let mut data: Vec<T> = Vec::with_capacity(self.data.len());
165 data.clone_from(self.data.as_ref());
166 let mut offs: Vec<usize> = Vec::with_capacity(self.offs.len());
167 offs.clone_from(&self.offs);
168 let mut strides: Vec<usize> = Vec::with_capacity(self.strides.len());
169 strides.clone_from(&self.strides);
170 NAVideoBuffer { info: self.info, data: NABufferRef::new(data), offs, strides }
172 /// Returns stride (distance between subsequent lines) for the requested component.
173 pub fn get_stride(&self, idx: usize) -> usize {
174 if idx >= self.strides.len() { return 0; }
177 /// Returns requested component dimensions.
178 pub fn get_dimensions(&self, idx: usize) -> (usize, usize) {
179 get_plane_size(&self.info, idx)
181 /// Converts current instance into buffer reference.
182 pub fn into_ref(self) -> NABufferRef<Self> {
183 NABufferRef::new(self)
186 fn print_contents(&self, datatype: &str) {
187 println!("{} video buffer size {}", datatype, self.data.len());
188 println!(" format {}", self.info);
190 for off in self.offs.iter() {
195 for stride in self.strides.iter() {
196 print!(" {}", *stride);
202 /// A specialised type for reference-counted `NAVideoBuffer`.
203 pub type NAVideoBufferRef<T> = NABufferRef<NAVideoBuffer<T>>;
205 /// Decoded audio frame.
207 /// NihAV frames are stored in native type (8/16/32-bit elements) inside a single buffer.
208 /// In case of planar audio samples for each channel are stored sequentially and can be accessed in the buffer starting at corresponding channel offset.
210 pub struct NAAudioBuffer<T> {
212 data: NABufferRef<Vec<T>>,
220 impl<T: Clone> NAAudioBuffer<T> {
221 /// Returns the start position of requested channel data.
222 pub fn get_offset(&self, idx: usize) -> usize {
223 if idx >= self.offs.len() { 0 }
224 else { self.offs[idx] }
226 /// Returns the distance between the start of one channel and the next one.
227 pub fn get_stride(&self) -> usize { self.stride }
228 /// Returns the distance between the samples in one channel.
229 pub fn get_step(&self) -> usize { self.step }
230 /// Returns audio format information.
231 pub fn get_info(&self) -> NAAudioInfo { self.info }
232 /// Returns channel map.
233 pub fn get_chmap(&self) -> &NAChannelMap { &self.chmap }
234 /// Returns an immutable reference to the data.
235 pub fn get_data(&self) -> &Vec<T> { self.data.as_ref() }
236 /// Returns a mutable reference to the data.
237 pub fn get_data_mut(&mut self) -> Option<&mut Vec<T>> { self.data.as_mut() }
238 /// Clones current `NAAudioBuffer` into a new one.
239 pub fn copy_buffer(&mut self) -> Self {
240 let mut data: Vec<T> = Vec::with_capacity(self.data.len());
241 data.clone_from(self.data.as_ref());
242 let mut offs: Vec<usize> = Vec::with_capacity(self.offs.len());
243 offs.clone_from(&self.offs);
244 NAAudioBuffer { info: self.info, data: NABufferRef::new(data), offs, chmap: self.get_chmap().clone(), len: self.len, stride: self.stride, step: self.step }
246 /// Return the length of frame in samples.
247 pub fn get_length(&self) -> usize { self.len }
249 fn print_contents(&self, datatype: &str) {
250 println!("Audio buffer with {} data, stride {}, step {}", datatype, self.stride, self.step);
251 println!(" format {}", self.info);
252 println!(" channel map {}", self.chmap);
254 for off in self.offs.iter() {
261 impl NAAudioBuffer<u8> {
262 /// Constructs a new `NAAudioBuffer` instance.
263 pub fn new_from_buf(info: NAAudioInfo, data: NABufferRef<Vec<u8>>, chmap: NAChannelMap) -> Self {
264 let len = data.len();
265 NAAudioBuffer { info, data, chmap, offs: Vec::new(), len, stride: 0, step: 0 }
269 /// A list of possible decoded frame types.
271 pub enum NABufferType {
272 /// 8-bit video buffer.
273 Video (NAVideoBufferRef<u8>),
274 /// 16-bit video buffer (i.e. every component or packed pixel fits into 16 bits).
275 Video16 (NAVideoBufferRef<u16>),
276 /// 32-bit video buffer (i.e. every component or packed pixel fits into 32 bits).
277 Video32 (NAVideoBufferRef<u32>),
278 /// Packed video buffer.
279 VideoPacked(NAVideoBufferRef<u8>),
280 /// Audio buffer with 8-bit unsigned integer audio.
281 AudioU8 (NAAudioBuffer<u8>),
282 /// Audio buffer with 16-bit signed integer audio.
283 AudioI16 (NAAudioBuffer<i16>),
284 /// Audio buffer with 32-bit signed integer audio.
285 AudioI32 (NAAudioBuffer<i32>),
286 /// Audio buffer with 32-bit floating point audio.
287 AudioF32 (NAAudioBuffer<f32>),
288 /// Packed audio buffer.
289 AudioPacked(NAAudioBuffer<u8>),
290 /// Buffer with generic data (e.g. subtitles).
291 Data (NABufferRef<Vec<u8>>),
297 /// Returns the offset to the requested component or channel.
298 pub fn get_offset(&self, idx: usize) -> usize {
300 NABufferType::Video(ref vb) => vb.get_offset(idx),
301 NABufferType::Video16(ref vb) => vb.get_offset(idx),
302 NABufferType::Video32(ref vb) => vb.get_offset(idx),
303 NABufferType::VideoPacked(ref vb) => vb.get_offset(idx),
304 NABufferType::AudioU8(ref ab) => ab.get_offset(idx),
305 NABufferType::AudioI16(ref ab) => ab.get_offset(idx),
306 NABufferType::AudioI32(ref ab) => ab.get_offset(idx),
307 NABufferType::AudioF32(ref ab) => ab.get_offset(idx),
308 NABufferType::AudioPacked(ref ab) => ab.get_offset(idx),
312 /// Returns information for video frames.
313 pub fn get_video_info(&self) -> Option<NAVideoInfo> {
315 NABufferType::Video(ref vb) => Some(vb.get_info()),
316 NABufferType::Video16(ref vb) => Some(vb.get_info()),
317 NABufferType::Video32(ref vb) => Some(vb.get_info()),
318 NABufferType::VideoPacked(ref vb) => Some(vb.get_info()),
322 /// Returns reference to 8-bit (or packed) video buffer.
323 pub fn get_vbuf(&self) -> Option<NAVideoBufferRef<u8>> {
325 NABufferType::Video(ref vb) => Some(vb.clone()),
326 NABufferType::VideoPacked(ref vb) => Some(vb.clone()),
330 /// Returns reference to 16-bit video buffer.
331 pub fn get_vbuf16(&self) -> Option<NAVideoBufferRef<u16>> {
333 NABufferType::Video16(ref vb) => Some(vb.clone()),
337 /// Returns reference to 32-bit video buffer.
338 pub fn get_vbuf32(&self) -> Option<NAVideoBufferRef<u32>> {
340 NABufferType::Video32(ref vb) => Some(vb.clone()),
344 /// Returns information for audio frames.
345 pub fn get_audio_info(&self) -> Option<NAAudioInfo> {
347 NABufferType::AudioU8(ref ab) => Some(ab.get_info()),
348 NABufferType::AudioI16(ref ab) => Some(ab.get_info()),
349 NABufferType::AudioI32(ref ab) => Some(ab.get_info()),
350 NABufferType::AudioF32(ref ab) => Some(ab.get_info()),
351 NABufferType::AudioPacked(ref ab) => Some(ab.get_info()),
355 /// Returns audio channel map.
356 pub fn get_chmap(&self) -> Option<&NAChannelMap> {
358 NABufferType::AudioU8(ref ab) => Some(ab.get_chmap()),
359 NABufferType::AudioI16(ref ab) => Some(ab.get_chmap()),
360 NABufferType::AudioI32(ref ab) => Some(ab.get_chmap()),
361 NABufferType::AudioF32(ref ab) => Some(ab.get_chmap()),
362 NABufferType::AudioPacked(ref ab) => Some(ab.get_chmap()),
366 /// Returns audio frame duration in samples.
367 pub fn get_audio_length(&self) -> usize {
369 NABufferType::AudioU8(ref ab) => ab.get_length(),
370 NABufferType::AudioI16(ref ab) => ab.get_length(),
371 NABufferType::AudioI32(ref ab) => ab.get_length(),
372 NABufferType::AudioF32(ref ab) => ab.get_length(),
373 NABufferType::AudioPacked(ref ab) => ab.get_length(),
377 /// Returns the distance between starts of two channels.
378 pub fn get_audio_stride(&self) -> usize {
380 NABufferType::AudioU8(ref ab) => ab.get_stride(),
381 NABufferType::AudioI16(ref ab) => ab.get_stride(),
382 NABufferType::AudioI32(ref ab) => ab.get_stride(),
383 NABufferType::AudioF32(ref ab) => ab.get_stride(),
384 NABufferType::AudioPacked(ref ab) => ab.get_stride(),
388 /// Returns the distance between two samples in one channel.
389 pub fn get_audio_step(&self) -> usize {
391 NABufferType::AudioU8(ref ab) => ab.get_step(),
392 NABufferType::AudioI16(ref ab) => ab.get_step(),
393 NABufferType::AudioI32(ref ab) => ab.get_step(),
394 NABufferType::AudioF32(ref ab) => ab.get_step(),
395 NABufferType::AudioPacked(ref ab) => ab.get_step(),
399 /// Returns reference to 8-bit (or packed) audio buffer.
400 pub fn get_abuf_u8(&self) -> Option<NAAudioBuffer<u8>> {
402 NABufferType::AudioU8(ref ab) => Some(ab.clone()),
403 NABufferType::AudioPacked(ref ab) => Some(ab.clone()),
407 /// Returns reference to 16-bit audio buffer.
408 pub fn get_abuf_i16(&self) -> Option<NAAudioBuffer<i16>> {
410 NABufferType::AudioI16(ref ab) => Some(ab.clone()),
414 /// Returns reference to 32-bit integer audio buffer.
415 pub fn get_abuf_i32(&self) -> Option<NAAudioBuffer<i32>> {
417 NABufferType::AudioI32(ref ab) => Some(ab.clone()),
421 /// Returns reference to 32-bit floating point audio buffer.
422 pub fn get_abuf_f32(&self) -> Option<NAAudioBuffer<f32>> {
424 NABufferType::AudioF32(ref ab) => Some(ab.clone()),
428 /// Prints internal buffer layout.
429 pub fn print_buffer_metadata(&self) {
431 NABufferType::Video(ref buf) => buf.print_contents("8-bit"),
432 NABufferType::Video16(ref buf) => buf.print_contents("16-bit"),
433 NABufferType::Video32(ref buf) => buf.print_contents("32-bit"),
434 NABufferType::VideoPacked(ref buf) => buf.print_contents("packed"),
435 NABufferType::AudioU8(ref buf) => buf.print_contents("8-bit unsigned integer"),
436 NABufferType::AudioI16(ref buf) => buf.print_contents("16-bit integer"),
437 NABufferType::AudioI32(ref buf) => buf.print_contents("32-bit integer"),
438 NABufferType::AudioF32(ref buf) => buf.print_contents("32-bit float"),
439 NABufferType::AudioPacked(ref buf) => buf.print_contents("packed"),
440 NABufferType::Data(ref buf) => { println!("Data buffer, len = {}", buf.len()); },
441 NABufferType::None => { println!("No buffer"); },
446 const NA_SIMPLE_VFRAME_COMPONENTS: usize = 4;
447 /// Simplified decoded frame data.
448 pub struct NASimpleVideoFrame<'a, T: Copy> {
449 /// Widths of each picture component.
450 pub width: [usize; NA_SIMPLE_VFRAME_COMPONENTS],
451 /// Heights of each picture component.
452 pub height: [usize; NA_SIMPLE_VFRAME_COMPONENTS],
453 /// Orientation (upside-down or downside-up) flag.
455 /// Strides for each component.
456 pub stride: [usize; NA_SIMPLE_VFRAME_COMPONENTS],
457 /// Start of each component.
458 pub offset: [usize; NA_SIMPLE_VFRAME_COMPONENTS],
459 /// Number of components.
460 pub components: usize,
461 /// Pointer to the picture pixel data.
462 pub data: &'a mut [T],
465 impl<'a, T:Copy> NASimpleVideoFrame<'a, T> {
466 /// Constructs a new instance of `NASimpleVideoFrame` from `NAVideoBuffer`.
467 pub fn from_video_buf(vbuf: &'a mut NAVideoBuffer<T>) -> Option<Self> {
468 let vinfo = vbuf.get_info();
469 let components = vinfo.format.components as usize;
470 if components > NA_SIMPLE_VFRAME_COMPONENTS {
473 let mut w: [usize; NA_SIMPLE_VFRAME_COMPONENTS] = [0; NA_SIMPLE_VFRAME_COMPONENTS];
474 let mut h: [usize; NA_SIMPLE_VFRAME_COMPONENTS] = [0; NA_SIMPLE_VFRAME_COMPONENTS];
475 let mut s: [usize; NA_SIMPLE_VFRAME_COMPONENTS] = [0; NA_SIMPLE_VFRAME_COMPONENTS];
476 let mut o: [usize; NA_SIMPLE_VFRAME_COMPONENTS] = [0; NA_SIMPLE_VFRAME_COMPONENTS];
477 for comp in 0..components {
478 let (width, height) = vbuf.get_dimensions(comp);
481 s[comp] = vbuf.get_stride(comp);
482 o[comp] = vbuf.get_offset(comp);
484 let flip = vinfo.flipped;
485 Some(NASimpleVideoFrame {
492 data: vbuf.data.as_mut_slice(),
497 /// A list of possible frame allocator errors.
498 #[derive(Debug,Clone,Copy,PartialEq)]
499 pub enum AllocatorError {
500 /// Requested picture dimensions are too large.
502 /// Invalid input format.
506 /// Constructs a new video buffer with requested format.
508 /// `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.
509 pub fn alloc_video_buffer(vinfo: NAVideoInfo, align: u8) -> Result<NABufferType, AllocatorError> {
510 let fmt = &vinfo.format;
511 let mut new_size: usize = 0;
512 let mut offs: Vec<usize> = Vec::new();
513 let mut strides: Vec<usize> = Vec::new();
515 for i in 0..fmt.get_num_comp() {
516 if fmt.get_chromaton(i) == None { return Err(AllocatorError::FormatError); }
519 let align_mod = ((1 << align) as usize) - 1;
520 let width = ((vinfo.width as usize) + align_mod) & !align_mod;
521 let height = ((vinfo.height as usize) + align_mod) & !align_mod;
522 let mut max_depth = 0;
523 let mut all_packed = true;
524 let mut all_bytealigned = true;
525 for i in 0..fmt.get_num_comp() {
526 let ochr = fmt.get_chromaton(i);
527 if ochr.is_none() { continue; }
528 let chr = ochr.unwrap();
529 if !chr.is_packed() {
531 } else if ((chr.get_shift() + chr.get_depth()) & 7) != 0 {
532 all_bytealigned = false;
534 max_depth = max(max_depth, chr.get_depth());
536 let unfit_elem_size = match fmt.get_elem_size() {
541 //todo semi-packed like NV12
542 if fmt.is_paletted() {
543 //todo various-sized palettes?
544 let stride = vinfo.get_format().get_chromaton(0).unwrap().get_linesize(width);
545 let pic_sz = stride.checked_mul(height);
546 if pic_sz == None { return Err(AllocatorError::TooLargeDimensions); }
547 let pal_size = 256 * (fmt.get_elem_size() as usize);
548 let new_size = pic_sz.unwrap().checked_add(pal_size);
549 if new_size == None { return Err(AllocatorError::TooLargeDimensions); }
551 offs.push(stride * height);
552 strides.push(stride);
553 let data: Vec<u8> = vec![0; new_size.unwrap()];
554 let buf: NAVideoBuffer<u8> = NAVideoBuffer { data: NABufferRef::new(data), info: vinfo, offs, strides };
555 Ok(NABufferType::Video(buf.into_ref()))
556 } else if !all_packed {
557 for i in 0..fmt.get_num_comp() {
558 let ochr = fmt.get_chromaton(i);
559 if ochr.is_none() { continue; }
560 let chr = ochr.unwrap();
561 offs.push(new_size as usize);
562 let stride = chr.get_linesize(width);
563 let cur_h = chr.get_height(height);
564 let cur_sz = stride.checked_mul(cur_h);
565 if cur_sz == None { return Err(AllocatorError::TooLargeDimensions); }
566 let new_sz = new_size.checked_add(cur_sz.unwrap());
567 if new_sz == None { return Err(AllocatorError::TooLargeDimensions); }
568 new_size = new_sz.unwrap();
569 strides.push(stride);
572 let data: Vec<u8> = vec![0; new_size];
573 let buf: NAVideoBuffer<u8> = NAVideoBuffer { data: NABufferRef::new(data), info: vinfo, offs, strides };
574 Ok(NABufferType::Video(buf.into_ref()))
575 } else if max_depth <= 16 {
576 let data: Vec<u16> = vec![0; new_size];
577 let buf: NAVideoBuffer<u16> = NAVideoBuffer { data: NABufferRef::new(data), info: vinfo, offs, strides };
578 Ok(NABufferType::Video16(buf.into_ref()))
580 let data: Vec<u32> = vec![0; new_size];
581 let buf: NAVideoBuffer<u32> = NAVideoBuffer { data: NABufferRef::new(data), info: vinfo, offs, strides };
582 Ok(NABufferType::Video32(buf.into_ref()))
584 } else if all_bytealigned || unfit_elem_size {
585 let elem_sz = fmt.get_elem_size();
586 let line_sz = width.checked_mul(elem_sz as usize);
587 if line_sz == None { return Err(AllocatorError::TooLargeDimensions); }
588 let new_sz = line_sz.unwrap().checked_mul(height);
589 if new_sz == None { return Err(AllocatorError::TooLargeDimensions); }
590 new_size = new_sz.unwrap();
591 let data: Vec<u8> = vec![0; new_size];
592 strides.push(line_sz.unwrap());
593 let buf: NAVideoBuffer<u8> = NAVideoBuffer { data: NABufferRef::new(data), info: vinfo, offs, strides };
594 Ok(NABufferType::VideoPacked(buf.into_ref()))
596 let elem_sz = fmt.get_elem_size();
597 let new_sz = width.checked_mul(height);
598 if new_sz == None { return Err(AllocatorError::TooLargeDimensions); }
599 new_size = new_sz.unwrap();
602 let data: Vec<u16> = vec![0; new_size];
604 let buf: NAVideoBuffer<u16> = NAVideoBuffer { data: NABufferRef::new(data), info: vinfo, offs, strides };
605 Ok(NABufferType::Video16(buf.into_ref()))
608 let data: Vec<u32> = vec![0; new_size];
610 let buf: NAVideoBuffer<u32> = NAVideoBuffer { data: NABufferRef::new(data), info: vinfo, offs, strides };
611 Ok(NABufferType::Video32(buf.into_ref()))
618 /// Constructs a new audio buffer for the requested format and length.
619 #[allow(clippy::collapsible_if)]
620 pub fn alloc_audio_buffer(ainfo: NAAudioInfo, nsamples: usize, chmap: NAChannelMap) -> Result<NABufferType, AllocatorError> {
621 let mut offs: Vec<usize> = Vec::new();
622 if ainfo.format.is_planar() || ((ainfo.format.get_bits() % 8) == 0) {
623 let len = nsamples.checked_mul(ainfo.channels as usize);
624 if len == None { return Err(AllocatorError::TooLargeDimensions); }
625 let length = len.unwrap();
628 if ainfo.format.is_planar() {
631 for i in 0..ainfo.channels {
632 offs.push((i as usize) * stride);
636 step = ainfo.channels as usize;
637 for i in 0..ainfo.channels {
638 offs.push(i as usize);
641 if ainfo.format.is_float() {
642 if ainfo.format.get_bits() == 32 {
643 let data: Vec<f32> = vec![0.0; length];
644 let buf: NAAudioBuffer<f32> = NAAudioBuffer { data: NABufferRef::new(data), info: ainfo, offs, chmap, len: nsamples, stride, step };
645 Ok(NABufferType::AudioF32(buf))
647 Err(AllocatorError::TooLargeDimensions)
650 if ainfo.format.get_bits() == 8 && !ainfo.format.is_signed() {
651 let data: Vec<u8> = vec![0; length];
652 let buf: NAAudioBuffer<u8> = NAAudioBuffer { data: NABufferRef::new(data), info: ainfo, offs, chmap, len: nsamples, stride, step };
653 Ok(NABufferType::AudioU8(buf))
654 } else if ainfo.format.get_bits() == 16 && ainfo.format.is_signed() {
655 let data: Vec<i16> = vec![0; length];
656 let buf: NAAudioBuffer<i16> = NAAudioBuffer { data: NABufferRef::new(data), info: ainfo, offs, chmap, len: nsamples, stride, step };
657 Ok(NABufferType::AudioI16(buf))
659 Err(AllocatorError::TooLargeDimensions)
663 let len = nsamples.checked_mul(ainfo.channels as usize);
664 if len == None { return Err(AllocatorError::TooLargeDimensions); }
665 let length = ainfo.format.get_audio_size(len.unwrap() as u64);
666 let data: Vec<u8> = vec![0; length];
667 let buf: NAAudioBuffer<u8> = NAAudioBuffer { data: NABufferRef::new(data), info: ainfo, offs, chmap, len: nsamples, stride: 0, step: 0 };
668 Ok(NABufferType::AudioPacked(buf))
672 /// Constructs a new buffer for generic data.
673 pub fn alloc_data_buffer(size: usize) -> Result<NABufferType, AllocatorError> {
674 let data: Vec<u8> = vec![0; size];
675 let buf: NABufferRef<Vec<u8>> = NABufferRef::new(data);
676 Ok(NABufferType::Data(buf))
679 /// Creates a clone of current buffer.
680 pub fn copy_buffer(buf: NABufferType) -> NABufferType {
684 /// Video frame pool.
686 /// This structure allows codec to effectively reuse old frames instead of allocating and de-allocating frames every time.
687 /// Caller can also reserve some frames for its own purposes e.g. display queue.
688 pub struct NAVideoBufferPool<T:Copy> {
689 pool: Vec<NAVideoBufferRef<T>>,
694 impl<T:Copy> NAVideoBufferPool<T> {
695 /// Constructs a new `NAVideoBufferPool` instance.
696 pub fn new(max_len: usize) -> Self {
698 pool: Vec::with_capacity(max_len),
703 /// Sets the number of buffers reserved for the user.
704 pub fn set_dec_bufs(&mut self, add_len: usize) {
705 self.add_len = add_len;
707 /// Returns an unused buffer from the pool.
708 pub fn get_free(&mut self) -> Option<NAVideoBufferRef<T>> {
709 for e in self.pool.iter() {
710 if e.get_num_refs() == 1 {
711 return Some(e.clone());
716 /// Clones provided frame data into a free pool frame.
717 pub fn get_copy(&mut self, rbuf: &NAVideoBufferRef<T>) -> Option<NAVideoBufferRef<T>> {
718 let mut dbuf = self.get_free()?;
719 dbuf.data.copy_from_slice(&rbuf.data);
722 /// Clears the pool from all frames.
723 pub fn reset(&mut self) {
724 self.pool.truncate(0);
728 impl NAVideoBufferPool<u8> {
729 /// Allocates the target amount of video frames using [`alloc_video_buffer`].
731 /// [`alloc_video_buffer`]: ./fn.alloc_video_buffer.html
732 pub fn prealloc_video(&mut self, vinfo: NAVideoInfo, align: u8) -> Result<(), AllocatorError> {
733 let nbufs = self.max_len + self.add_len - self.pool.len();
735 let vbuf = alloc_video_buffer(vinfo, align)?;
736 if let NABufferType::Video(buf) = vbuf {
738 } else if let NABufferType::VideoPacked(buf) = vbuf {
741 return Err(AllocatorError::FormatError);
748 impl NAVideoBufferPool<u16> {
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::Video16(buf) = vbuf {
759 return Err(AllocatorError::FormatError);
766 impl NAVideoBufferPool<u32> {
767 /// Allocates the target amount of video frames using [`alloc_video_buffer`].
769 /// [`alloc_video_buffer`]: ./fn.alloc_video_buffer.html
770 pub fn prealloc_video(&mut self, vinfo: NAVideoInfo, align: u8) -> Result<(), AllocatorError> {
771 let nbufs = self.max_len + self.add_len - self.pool.len();
773 let vbuf = alloc_video_buffer(vinfo, align)?;
774 if let NABufferType::Video32(buf) = vbuf {
777 return Err(AllocatorError::FormatError);
784 /// Information about codec contained in a stream.
787 pub struct NACodecInfo {
789 properties: NACodecTypeInfo,
790 extradata: Option<Arc<Vec<u8>>>,
793 /// A specialised type for reference-counted `NACodecInfo`.
794 pub type NACodecInfoRef = Arc<NACodecInfo>;
797 /// Constructs a new instance of `NACodecInfo`.
798 pub fn new(name: &'static str, p: NACodecTypeInfo, edata: Option<Vec<u8>>) -> Self {
799 let extradata = match edata {
801 Some(vec) => Some(Arc::new(vec)),
803 NACodecInfo { name, properties: p, extradata }
805 /// Constructs a new reference-counted instance of `NACodecInfo`.
806 pub fn new_ref(name: &'static str, p: NACodecTypeInfo, edata: Option<Arc<Vec<u8>>>) -> Self {
807 NACodecInfo { name, properties: p, extradata: edata }
809 /// Converts current instance into a reference-counted one.
810 pub fn into_ref(self) -> NACodecInfoRef { Arc::new(self) }
811 /// Returns codec information.
812 pub fn get_properties(&self) -> NACodecTypeInfo { self.properties }
813 /// Returns additional initialisation data required by the codec.
814 pub fn get_extradata(&self) -> Option<Arc<Vec<u8>>> {
815 if let Some(ref vec) = self.extradata { return Some(vec.clone()); }
818 /// Returns codec name.
819 pub fn get_name(&self) -> &'static str { self.name }
820 /// Reports whether it is a video codec.
821 pub fn is_video(&self) -> bool {
822 if let NACodecTypeInfo::Video(_) = self.properties { return true; }
825 /// Reports whether it is an audio codec.
826 pub fn is_audio(&self) -> bool {
827 if let NACodecTypeInfo::Audio(_) = self.properties { return true; }
830 /// Constructs a new empty reference-counted instance of `NACodecInfo`.
831 pub fn new_dummy() -> Arc<Self> {
832 Arc::new(DUMMY_CODEC_INFO)
834 /// Updates codec infomation.
835 pub fn replace_info(&self, p: NACodecTypeInfo) -> Arc<Self> {
836 Arc::new(NACodecInfo { name: self.name, properties: p, extradata: self.extradata.clone() })
840 impl Default for NACodecInfo {
841 fn default() -> Self { DUMMY_CODEC_INFO }
844 impl fmt::Display for NACodecInfo {
845 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
846 let edata = match self.extradata.clone() {
847 None => "no extradata".to_string(),
848 Some(v) => format!("{} byte(s) of extradata", v.len()),
850 write!(f, "{}: {} {}", self.name, self.properties, edata)
854 /// Default empty codec information.
855 pub const DUMMY_CODEC_INFO: NACodecInfo = NACodecInfo {
857 properties: NACodecTypeInfo::None,
860 /// A list of recognized frame types.
861 #[derive(Debug,Clone,Copy,PartialEq)]
864 /// Intra frame type.
866 /// Inter frame type.
868 /// Bidirectionally predicted frame.
872 /// When such frame is encountered then last frame should be used again if it is needed.
874 /// Some other frame type.
878 impl fmt::Display for FrameType {
879 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
881 FrameType::I => write!(f, "I"),
882 FrameType::P => write!(f, "P"),
883 FrameType::B => write!(f, "B"),
884 FrameType::Skip => write!(f, "skip"),
885 FrameType::Other => write!(f, "x"),
890 /// Timestamp information.
891 #[derive(Debug,Clone,Copy)]
892 pub struct NATimeInfo {
893 /// Presentation timestamp.
894 pub pts: Option<u64>,
895 /// Decode timestamp.
896 pub dts: Option<u64>,
897 /// Duration (in timebase units).
898 pub duration: Option<u64>,
899 /// Timebase numerator.
901 /// Timebase denominator.
906 /// Constructs a new `NATimeInfo` instance.
907 pub fn new(pts: Option<u64>, dts: Option<u64>, duration: Option<u64>, tb_num: u32, tb_den: u32) -> Self {
908 NATimeInfo { pts, dts, duration, tb_num, tb_den }
910 /// Returns presentation timestamp.
911 pub fn get_pts(&self) -> Option<u64> { self.pts }
912 /// Returns decoding timestamp.
913 pub fn get_dts(&self) -> Option<u64> { self.dts }
914 /// Returns duration.
915 pub fn get_duration(&self) -> Option<u64> { self.duration }
916 /// Sets new presentation timestamp.
917 pub fn set_pts(&mut self, pts: Option<u64>) { self.pts = pts; }
918 /// Sets new decoding timestamp.
919 pub fn set_dts(&mut self, dts: Option<u64>) { self.dts = dts; }
920 /// Sets new duration.
921 pub fn set_duration(&mut self, dur: Option<u64>) { self.duration = dur; }
923 /// Converts time in given scale into timestamp in given base.
924 pub fn time_to_ts(time: u64, base: u64, tb_num: u32, tb_den: u32) -> u64 {
925 let tb_num = tb_num as u64;
926 let tb_den = tb_den as u64;
927 let tmp = time.checked_mul(tb_num);
928 if let Some(tmp) = tmp {
931 let tmp = time.checked_mul(tb_num);
932 if let Some(tmp) = tmp {
935 let coarse = time / base;
936 let tmp = coarse.checked_mul(tb_num);
937 if let Some(tmp) = tmp {
940 (coarse / tb_den) * tb_num
945 /// Converts timestamp in given base into time in given scale.
946 pub fn ts_to_time(ts: u64, base: u64, tb_num: u32, tb_den: u32) -> u64 {
947 let tb_num = tb_num as u64;
948 let tb_den = tb_den as u64;
949 let tmp = ts.checked_mul(base);
950 if let Some(tmp) = tmp {
951 let tmp2 = tmp.checked_mul(tb_num);
952 if let Some(tmp2) = tmp2 {
955 (tmp / tb_den) * tb_num
958 let tmp = ts.checked_mul(tb_num);
959 if let Some(tmp) = tmp {
960 (tmp / tb_den) * base
962 (ts / tb_den) * base * tb_num
968 /// Decoded frame information.
976 buffer: NABufferType,
977 info: NACodecInfoRef,
979 pub frame_type: FrameType,
982 // options: HashMap<String, NAValue>,
985 /// A specialised type for reference-counted `NAFrame`.
986 pub type NAFrameRef = Arc<NAFrame>;
988 fn get_plane_size(info: &NAVideoInfo, idx: usize) -> (usize, usize) {
989 let chromaton = info.get_format().get_chromaton(idx);
990 if chromaton.is_none() { return (0, 0); }
991 let (hs, vs) = chromaton.unwrap().get_subsampling();
992 let w = (info.get_width() + ((1 << hs) - 1)) >> hs;
993 let h = (info.get_height() + ((1 << vs) - 1)) >> vs;
998 /// Constructs a new `NAFrame` instance.
999 pub fn new(ts: NATimeInfo,
1002 info: NACodecInfoRef,
1003 /*options: HashMap<String, NAValue>,*/
1004 buffer: NABufferType) -> Self {
1005 NAFrame { ts, id: 0, buffer, info, frame_type: ftype, key: keyframe/*, options*/ }
1007 /// Returns frame format information.
1008 pub fn get_info(&self) -> NACodecInfoRef { self.info.clone() }
1009 /// Returns frame type.
1010 pub fn get_frame_type(&self) -> FrameType { self.frame_type }
1011 /// Reports whether the frame is a keyframe.
1012 pub fn is_keyframe(&self) -> bool { self.key }
1013 /// Sets new frame type.
1014 pub fn set_frame_type(&mut self, ftype: FrameType) { self.frame_type = ftype; }
1015 /// Sets keyframe flag.
1016 pub fn set_keyframe(&mut self, key: bool) { self.key = key; }
1017 /// Returns frame timestamp.
1018 pub fn get_time_information(&self) -> NATimeInfo { self.ts }
1019 /// Returns frame presentation time.
1020 pub fn get_pts(&self) -> Option<u64> { self.ts.get_pts() }
1021 /// Returns frame decoding time.
1022 pub fn get_dts(&self) -> Option<u64> { self.ts.get_dts() }
1023 /// Returns picture ID.
1024 pub fn get_id(&self) -> i64 { self.id }
1025 /// Returns frame display duration.
1026 pub fn get_duration(&self) -> Option<u64> { self.ts.get_duration() }
1027 /// Sets new presentation timestamp.
1028 pub fn set_pts(&mut self, pts: Option<u64>) { self.ts.set_pts(pts); }
1029 /// Sets new decoding timestamp.
1030 pub fn set_dts(&mut self, dts: Option<u64>) { self.ts.set_dts(dts); }
1031 /// Sets new picture ID.
1032 pub fn set_id(&mut self, id: i64) { self.id = id; }
1033 /// Sets new duration.
1034 pub fn set_duration(&mut self, dur: Option<u64>) { self.ts.set_duration(dur); }
1036 /// Returns a reference to the frame data.
1037 pub fn get_buffer(&self) -> NABufferType { self.buffer.clone() }
1039 /// Converts current instance into a reference-counted one.
1040 pub fn into_ref(self) -> NAFrameRef { Arc::new(self) }
1042 /// Creates new frame with metadata from `NAPacket`.
1043 pub fn new_from_pkt(pkt: &NAPacket, info: NACodecInfoRef, buf: NABufferType) -> NAFrame {
1044 NAFrame::new(pkt.ts, FrameType::Other, pkt.keyframe, info, /*HashMap::new(),*/ buf)
1048 impl fmt::Display for NAFrame {
1049 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1050 let mut ostr = format!("frame type {}", self.frame_type);
1051 if let Some(pts) = self.ts.pts { ostr = format!("{} pts {}", ostr, pts); }
1052 if let Some(dts) = self.ts.dts { ostr = format!("{} dts {}", ostr, dts); }
1053 if let Some(dur) = self.ts.duration { ostr = format!("{} duration {}", ostr, dur); }
1054 if self.key { ostr = format!("{} kf", ostr); }
1055 write!(f, "[{}]", ostr)
1059 /// A list of possible stream types.
1060 #[derive(Debug,Clone,Copy,PartialEq)]
1062 pub enum StreamType {
1069 /// Any data stream (or might be an unrecognized audio/video stream).
1071 /// Nonexistent stream.
1075 impl fmt::Display for StreamType {
1076 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1078 StreamType::Video => write!(f, "Video"),
1079 StreamType::Audio => write!(f, "Audio"),
1080 StreamType::Subtitles => write!(f, "Subtitles"),
1081 StreamType::Data => write!(f, "Data"),
1082 StreamType::None => write!(f, "-"),
1090 pub struct NAStream {
1091 media_type: StreamType,
1095 info: NACodecInfoRef,
1096 /// Timebase numerator.
1098 /// Timebase denominator.
1102 /// A specialised reference-counted `NAStream` type.
1103 pub type NAStreamRef = Arc<NAStream>;
1105 /// Downscales the timebase by its greatest common denominator.
1106 pub fn reduce_timebase(tb_num: u32, tb_den: u32) -> (u32, u32) {
1107 if tb_num == 0 { return (tb_num, tb_den); }
1108 if (tb_den % tb_num) == 0 { return (1, tb_den / tb_num); }
1114 if a > b { a -= b; }
1115 else if b > a { b -= a; }
1118 (tb_num / a, tb_den / a)
1122 /// Constructs a new `NAStream` instance.
1123 pub fn new(mt: StreamType, id: u32, info: NACodecInfo, tb_num: u32, tb_den: u32) -> Self {
1124 let (n, d) = reduce_timebase(tb_num, tb_den);
1125 NAStream { media_type: mt, id, num: 0, info: info.into_ref(), tb_num: n, tb_den: d }
1127 /// Returns stream id.
1128 pub fn get_id(&self) -> u32 { self.id }
1129 /// Returns stream type.
1130 pub fn get_media_type(&self) -> StreamType { self.media_type }
1131 /// Returns stream number assigned by demuxer.
1132 pub fn get_num(&self) -> usize { self.num }
1133 /// Sets stream number.
1134 pub fn set_num(&mut self, num: usize) { self.num = num; }
1135 /// Returns codec information.
1136 pub fn get_info(&self) -> NACodecInfoRef { self.info.clone() }
1137 /// Returns stream timebase.
1138 pub fn get_timebase(&self) -> (u32, u32) { (self.tb_num, self.tb_den) }
1139 /// Sets new stream timebase.
1140 pub fn set_timebase(&mut self, tb_num: u32, tb_den: u32) {
1141 let (n, d) = reduce_timebase(tb_num, tb_den);
1145 /// Converts current instance into a reference-counted one.
1146 pub fn into_ref(self) -> NAStreamRef { Arc::new(self) }
1149 impl fmt::Display for NAStream {
1150 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1151 write!(f, "({}#{} @ {}/{} - {})", self.media_type, self.id, self.tb_num, self.tb_den, self.info.get_properties())
1155 /// Side data that may accompany demuxed data.
1157 pub enum NASideData {
1158 /// Palette information.
1160 /// This side data contains a flag signalling that palette has changed since previous time and a reference to the current palette.
1161 /// Palette is stored in 8-bit RGBA format.
1162 Palette(bool, Arc<[u8; 1024]>),
1163 /// Generic user data.
1164 UserData(Arc<Vec<u8>>),
1167 /// Packet with compressed data.
1169 pub struct NAPacket {
1170 stream: NAStreamRef,
1171 /// Packet timestamp.
1173 buffer: NABufferRef<Vec<u8>>,
1176 // options: HashMap<String, NAValue<'a>>,
1177 /// Packet side data (e.g. palette for paletted formats).
1178 pub side_data: Vec<NASideData>,
1182 /// Constructs a new `NAPacket` instance.
1183 pub fn new(str: NAStreamRef, ts: NATimeInfo, kf: bool, vec: Vec<u8>) -> Self {
1184 // let mut vec: Vec<u8> = Vec::new();
1185 // vec.resize(size, 0);
1186 NAPacket { stream: str, ts, keyframe: kf, buffer: NABufferRef::new(vec), side_data: Vec::new() }
1188 /// Returns information about the stream packet belongs to.
1189 pub fn get_stream(&self) -> NAStreamRef { self.stream.clone() }
1190 /// Returns packet timestamp.
1191 pub fn get_time_information(&self) -> NATimeInfo { self.ts }
1192 /// Returns packet presentation timestamp.
1193 pub fn get_pts(&self) -> Option<u64> { self.ts.get_pts() }
1194 /// Returns packet decoding timestamp.
1195 pub fn get_dts(&self) -> Option<u64> { self.ts.get_dts() }
1196 /// Returns packet duration.
1197 pub fn get_duration(&self) -> Option<u64> { self.ts.get_duration() }
1198 /// Reports whether this is a keyframe packet.
1199 pub fn is_keyframe(&self) -> bool { self.keyframe }
1200 /// Returns a reference to packet data.
1201 pub fn get_buffer(&self) -> NABufferRef<Vec<u8>> { self.buffer.clone() }
1202 /// Adds side data for a packet.
1203 pub fn add_side_data(&mut self, side_data: NASideData) { self.side_data.push(side_data); }
1204 /// Assigns packet to a new stream.
1205 pub fn reassign(&mut self, str: NAStreamRef, ts: NATimeInfo) {
1211 impl Drop for NAPacket {
1212 fn drop(&mut self) {}
1215 impl fmt::Display for NAPacket {
1216 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1217 let mut ostr = format!("[pkt for {} size {}", self.stream, self.buffer.len());
1218 if let Some(pts) = self.ts.pts { ostr = format!("{} pts {}", ostr, pts); }
1219 if let Some(dts) = self.ts.dts { ostr = format!("{} dts {}", ostr, dts); }
1220 if let Some(dur) = self.ts.duration { ostr = format!("{} duration {}", ostr, dur); }
1221 if self.keyframe { ostr = format!("{} kf", ostr); }
1223 write!(f, "{}", ostr)