core: add allocation of 32-bit integer audio to alloc_audio_buffer()
[nihav.git] / nihav-core / src / frame.rs
1 //! Packets and decoded frames functionality.
2 use std::cmp::max;
3 //use std::collections::HashMap;
4 use std::fmt;
5 pub use std::sync::Arc;
6 pub use crate::formats::*;
7 pub use crate::refs::*;
8 use std::str::FromStr;
9
10 /// Audio stream information.
11 #[allow(dead_code)]
12 #[derive(Clone,Copy,PartialEq)]
13 pub struct NAAudioInfo {
14 /// Sample rate.
15 pub sample_rate: u32,
16 /// Number of channels.
17 pub channels: u8,
18 /// Audio sample format.
19 pub format: NASoniton,
20 /// Length of one audio block in samples.
21 pub block_len: usize,
22 }
23
24 impl NAAudioInfo {
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 }
28 }
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 }
37 }
38
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)
42 }
43 }
44
45 /// Video stream information.
46 #[allow(dead_code)]
47 #[derive(Clone,Copy,PartialEq)]
48 pub struct NAVideoInfo {
49 /// Picture width.
50 pub width: usize,
51 /// Picture height.
52 pub height: usize,
53 /// Picture is stored downside up.
54 pub flipped: bool,
55 /// Picture pixel format.
56 pub format: NAPixelFormaton,
57 /// Declared bits per sample.
58 pub bits: u8,
59 }
60
61 impl NAVideoInfo {
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 }
66 }
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; }
79 }
80
81 impl fmt::Display for NAVideoInfo {
82 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
83 write!(f, "{}x{}", self.width, self.height)
84 }
85 }
86
87 /// A list of possible stream information types.
88 #[derive(Clone,Copy,PartialEq)]
89 pub enum NACodecTypeInfo {
90 /// No codec present.
91 None,
92 /// Audio codec information.
93 Audio(NAAudioInfo),
94 /// Video codec information.
95 Video(NAVideoInfo),
96 }
97
98 impl NACodecTypeInfo {
99 /// Returns video stream information.
100 pub fn get_video_info(&self) -> Option<NAVideoInfo> {
101 match *self {
102 NACodecTypeInfo::Video(vinfo) => Some(vinfo),
103 _ => None,
104 }
105 }
106 /// Returns audio stream information.
107 pub fn get_audio_info(&self) -> Option<NAAudioInfo> {
108 match *self {
109 NACodecTypeInfo::Audio(ainfo) => Some(ainfo),
110 _ => None,
111 }
112 }
113 /// Reports whether the current stream is video stream.
114 pub fn is_video(&self) -> bool {
115 match *self {
116 NACodecTypeInfo::Video(_) => true,
117 _ => false,
118 }
119 }
120 /// Reports whether the current stream is audio stream.
121 pub fn is_audio(&self) -> bool {
122 match *self {
123 NACodecTypeInfo::Audio(_) => true,
124 _ => false,
125 }
126 }
127 }
128
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),
135 };
136 write!(f, "{}", ret)
137 }
138 }
139
140 /// Decoded video frame.
141 ///
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.
144 #[derive(Clone)]
145 pub struct NAVideoBuffer<T> {
146 info: NAVideoInfo,
147 data: NABufferRef<Vec<T>>,
148 offs: Vec<usize>,
149 strides: Vec<usize>,
150 }
151
152 impl<T: Clone> NAVideoBuffer<T> {
153 /// Returns the component offset (0 for all unavailable offsets).
154 pub fn get_offset(&self, idx: usize) -> usize {
155 if idx >= self.offs.len() { 0 }
156 else { self.offs[idx] }
157 }
158 /// Returns picture info.
159 pub fn get_info(&self) -> NAVideoInfo { self.info }
160 /// Returns an immutable reference to the data.
161 pub fn get_data(&self) -> &Vec<T> { self.data.as_ref() }
162 /// Returns a mutable reference to the data.
163 pub fn get_data_mut(&mut self) -> Option<&mut Vec<T>> { self.data.as_mut() }
164 /// Returns the number of components in picture format.
165 pub fn get_num_components(&self) -> usize { self.offs.len() }
166 /// Creates a copy of current `NAVideoBuffer`.
167 pub fn copy_buffer(&mut self) -> Self {
168 let mut data: Vec<T> = Vec::with_capacity(self.data.len());
169 data.clone_from(self.data.as_ref());
170 let mut offs: Vec<usize> = Vec::with_capacity(self.offs.len());
171 offs.clone_from(&self.offs);
172 let mut strides: Vec<usize> = Vec::with_capacity(self.strides.len());
173 strides.clone_from(&self.strides);
174 NAVideoBuffer { info: self.info, data: NABufferRef::new(data), offs, strides }
175 }
176 /// Returns stride (distance between subsequent lines) for the requested component.
177 pub fn get_stride(&self, idx: usize) -> usize {
178 if idx >= self.strides.len() { return 0; }
179 self.strides[idx]
180 }
181 /// Returns requested component dimensions.
182 pub fn get_dimensions(&self, idx: usize) -> (usize, usize) {
183 get_plane_size(&self.info, idx)
184 }
185 /// Converts current instance into buffer reference.
186 pub fn into_ref(self) -> NABufferRef<Self> {
187 NABufferRef::new(self)
188 }
189
190 fn print_contents(&self, datatype: &str) {
191 println!("{} video buffer size {}", datatype, self.data.len());
192 println!(" format {}", self.info);
193 print!(" offsets:");
194 for off in self.offs.iter() {
195 print!(" {}", *off);
196 }
197 println!();
198 print!(" strides:");
199 for stride in self.strides.iter() {
200 print!(" {}", *stride);
201 }
202 println!();
203 }
204 }
205
206 /// A specialised type for reference-counted `NAVideoBuffer`.
207 pub type NAVideoBufferRef<T> = NABufferRef<NAVideoBuffer<T>>;
208
209 /// Decoded audio frame.
210 ///
211 /// NihAV frames are stored in native type (8/16/32-bit elements) inside a single buffer.
212 /// In case of planar audio samples for each channel are stored sequentially and can be accessed in the buffer starting at corresponding channel offset.
213 #[derive(Clone)]
214 pub struct NAAudioBuffer<T> {
215 info: NAAudioInfo,
216 data: NABufferRef<Vec<T>>,
217 offs: Vec<usize>,
218 stride: usize,
219 step: usize,
220 chmap: NAChannelMap,
221 len: usize,
222 }
223
224 impl<T: Clone> NAAudioBuffer<T> {
225 /// Returns the start position of requested channel data.
226 pub fn get_offset(&self, idx: usize) -> usize {
227 if idx >= self.offs.len() { 0 }
228 else { self.offs[idx] }
229 }
230 /// Returns the distance between the start of one channel and the next one.
231 pub fn get_stride(&self) -> usize { self.stride }
232 /// Returns the distance between the samples in one channel.
233 pub fn get_step(&self) -> usize { self.step }
234 /// Returns audio format information.
235 pub fn get_info(&self) -> NAAudioInfo { self.info }
236 /// Returns channel map.
237 pub fn get_chmap(&self) -> &NAChannelMap { &self.chmap }
238 /// Returns an immutable reference to the data.
239 pub fn get_data(&self) -> &Vec<T> { self.data.as_ref() }
240 /// Returns reference to the data.
241 pub fn get_data_ref(&self) -> NABufferRef<Vec<T>> { self.data.clone() }
242 /// Returns a mutable reference to the data.
243 pub fn get_data_mut(&mut self) -> Option<&mut Vec<T>> { self.data.as_mut() }
244 /// Clones current `NAAudioBuffer` into a new one.
245 pub fn copy_buffer(&mut self) -> Self {
246 let mut data: Vec<T> = Vec::with_capacity(self.data.len());
247 data.clone_from(self.data.as_ref());
248 let mut offs: Vec<usize> = Vec::with_capacity(self.offs.len());
249 offs.clone_from(&self.offs);
250 NAAudioBuffer { info: self.info, data: NABufferRef::new(data), offs, chmap: self.get_chmap().clone(), len: self.len, stride: self.stride, step: self.step }
251 }
252 /// Return the length of frame in samples.
253 pub fn get_length(&self) -> usize { self.len }
254
255 fn print_contents(&self, datatype: &str) {
256 println!("Audio buffer with {} data, stride {}, step {}", datatype, self.stride, self.step);
257 println!(" format {}", self.info);
258 println!(" channel map {}", self.chmap);
259 print!(" offsets:");
260 for off in self.offs.iter() {
261 print!(" {}", *off);
262 }
263 println!();
264 }
265 }
266
267 impl NAAudioBuffer<u8> {
268 /// Constructs a new `NAAudioBuffer` instance.
269 pub fn new_from_buf(info: NAAudioInfo, data: NABufferRef<Vec<u8>>, chmap: NAChannelMap) -> Self {
270 let len = data.len();
271 NAAudioBuffer { info, data, chmap, offs: Vec::new(), len, stride: 0, step: 0 }
272 }
273 }
274
275 /// A list of possible decoded frame types.
276 #[derive(Clone)]
277 pub enum NABufferType {
278 /// 8-bit video buffer.
279 Video (NAVideoBufferRef<u8>),
280 /// 16-bit video buffer (i.e. every component or packed pixel fits into 16 bits).
281 Video16 (NAVideoBufferRef<u16>),
282 /// 32-bit video buffer (i.e. every component or packed pixel fits into 32 bits).
283 Video32 (NAVideoBufferRef<u32>),
284 /// Packed video buffer.
285 VideoPacked(NAVideoBufferRef<u8>),
286 /// Audio buffer with 8-bit unsigned integer audio.
287 AudioU8 (NAAudioBuffer<u8>),
288 /// Audio buffer with 16-bit signed integer audio.
289 AudioI16 (NAAudioBuffer<i16>),
290 /// Audio buffer with 32-bit signed integer audio.
291 AudioI32 (NAAudioBuffer<i32>),
292 /// Audio buffer with 32-bit floating point audio.
293 AudioF32 (NAAudioBuffer<f32>),
294 /// Packed audio buffer.
295 AudioPacked(NAAudioBuffer<u8>),
296 /// Buffer with generic data (e.g. subtitles).
297 Data (NABufferRef<Vec<u8>>),
298 /// No data present.
299 None,
300 }
301
302 impl NABufferType {
303 /// Returns the offset to the requested component or channel.
304 pub fn get_offset(&self, idx: usize) -> usize {
305 match *self {
306 NABufferType::Video(ref vb) => vb.get_offset(idx),
307 NABufferType::Video16(ref vb) => vb.get_offset(idx),
308 NABufferType::Video32(ref vb) => vb.get_offset(idx),
309 NABufferType::VideoPacked(ref vb) => vb.get_offset(idx),
310 NABufferType::AudioU8(ref ab) => ab.get_offset(idx),
311 NABufferType::AudioI16(ref ab) => ab.get_offset(idx),
312 NABufferType::AudioI32(ref ab) => ab.get_offset(idx),
313 NABufferType::AudioF32(ref ab) => ab.get_offset(idx),
314 NABufferType::AudioPacked(ref ab) => ab.get_offset(idx),
315 _ => 0,
316 }
317 }
318 /// Returns information for video frames.
319 pub fn get_video_info(&self) -> Option<NAVideoInfo> {
320 match *self {
321 NABufferType::Video(ref vb) => Some(vb.get_info()),
322 NABufferType::Video16(ref vb) => Some(vb.get_info()),
323 NABufferType::Video32(ref vb) => Some(vb.get_info()),
324 NABufferType::VideoPacked(ref vb) => Some(vb.get_info()),
325 _ => None,
326 }
327 }
328 /// Returns reference to 8-bit (or packed) video buffer.
329 pub fn get_vbuf(&self) -> Option<NAVideoBufferRef<u8>> {
330 match *self {
331 NABufferType::Video(ref vb) => Some(vb.clone()),
332 NABufferType::VideoPacked(ref vb) => Some(vb.clone()),
333 _ => None,
334 }
335 }
336 /// Returns reference to 16-bit video buffer.
337 pub fn get_vbuf16(&self) -> Option<NAVideoBufferRef<u16>> {
338 match *self {
339 NABufferType::Video16(ref vb) => Some(vb.clone()),
340 _ => None,
341 }
342 }
343 /// Returns reference to 32-bit video buffer.
344 pub fn get_vbuf32(&self) -> Option<NAVideoBufferRef<u32>> {
345 match *self {
346 NABufferType::Video32(ref vb) => Some(vb.clone()),
347 _ => None,
348 }
349 }
350 /// Returns information for audio frames.
351 pub fn get_audio_info(&self) -> Option<NAAudioInfo> {
352 match *self {
353 NABufferType::AudioU8(ref ab) => Some(ab.get_info()),
354 NABufferType::AudioI16(ref ab) => Some(ab.get_info()),
355 NABufferType::AudioI32(ref ab) => Some(ab.get_info()),
356 NABufferType::AudioF32(ref ab) => Some(ab.get_info()),
357 NABufferType::AudioPacked(ref ab) => Some(ab.get_info()),
358 _ => None,
359 }
360 }
361 /// Returns audio channel map.
362 pub fn get_chmap(&self) -> Option<&NAChannelMap> {
363 match *self {
364 NABufferType::AudioU8(ref ab) => Some(ab.get_chmap()),
365 NABufferType::AudioI16(ref ab) => Some(ab.get_chmap()),
366 NABufferType::AudioI32(ref ab) => Some(ab.get_chmap()),
367 NABufferType::AudioF32(ref ab) => Some(ab.get_chmap()),
368 NABufferType::AudioPacked(ref ab) => Some(ab.get_chmap()),
369 _ => None,
370 }
371 }
372 /// Returns audio frame duration in samples.
373 pub fn get_audio_length(&self) -> usize {
374 match *self {
375 NABufferType::AudioU8(ref ab) => ab.get_length(),
376 NABufferType::AudioI16(ref ab) => ab.get_length(),
377 NABufferType::AudioI32(ref ab) => ab.get_length(),
378 NABufferType::AudioF32(ref ab) => ab.get_length(),
379 NABufferType::AudioPacked(ref ab) => ab.get_length(),
380 _ => 0,
381 }
382 }
383 /// Returns the distance between starts of two channels.
384 pub fn get_audio_stride(&self) -> usize {
385 match *self {
386 NABufferType::AudioU8(ref ab) => ab.get_stride(),
387 NABufferType::AudioI16(ref ab) => ab.get_stride(),
388 NABufferType::AudioI32(ref ab) => ab.get_stride(),
389 NABufferType::AudioF32(ref ab) => ab.get_stride(),
390 NABufferType::AudioPacked(ref ab) => ab.get_stride(),
391 _ => 0,
392 }
393 }
394 /// Returns the distance between two samples in one channel.
395 pub fn get_audio_step(&self) -> usize {
396 match *self {
397 NABufferType::AudioU8(ref ab) => ab.get_step(),
398 NABufferType::AudioI16(ref ab) => ab.get_step(),
399 NABufferType::AudioI32(ref ab) => ab.get_step(),
400 NABufferType::AudioF32(ref ab) => ab.get_step(),
401 NABufferType::AudioPacked(ref ab) => ab.get_step(),
402 _ => 0,
403 }
404 }
405 /// Returns reference to 8-bit (or packed) audio buffer.
406 pub fn get_abuf_u8(&self) -> Option<NAAudioBuffer<u8>> {
407 match *self {
408 NABufferType::AudioU8(ref ab) => Some(ab.clone()),
409 NABufferType::AudioPacked(ref ab) => Some(ab.clone()),
410 _ => None,
411 }
412 }
413 /// Returns reference to 16-bit audio buffer.
414 pub fn get_abuf_i16(&self) -> Option<NAAudioBuffer<i16>> {
415 match *self {
416 NABufferType::AudioI16(ref ab) => Some(ab.clone()),
417 _ => None,
418 }
419 }
420 /// Returns reference to 32-bit integer audio buffer.
421 pub fn get_abuf_i32(&self) -> Option<NAAudioBuffer<i32>> {
422 match *self {
423 NABufferType::AudioI32(ref ab) => Some(ab.clone()),
424 _ => None,
425 }
426 }
427 /// Returns reference to 32-bit floating point audio buffer.
428 pub fn get_abuf_f32(&self) -> Option<NAAudioBuffer<f32>> {
429 match *self {
430 NABufferType::AudioF32(ref ab) => Some(ab.clone()),
431 _ => None,
432 }
433 }
434 /// Prints internal buffer layout.
435 pub fn print_buffer_metadata(&self) {
436 match *self {
437 NABufferType::Video(ref buf) => buf.print_contents("8-bit"),
438 NABufferType::Video16(ref buf) => buf.print_contents("16-bit"),
439 NABufferType::Video32(ref buf) => buf.print_contents("32-bit"),
440 NABufferType::VideoPacked(ref buf) => buf.print_contents("packed"),
441 NABufferType::AudioU8(ref buf) => buf.print_contents("8-bit unsigned integer"),
442 NABufferType::AudioI16(ref buf) => buf.print_contents("16-bit integer"),
443 NABufferType::AudioI32(ref buf) => buf.print_contents("32-bit integer"),
444 NABufferType::AudioF32(ref buf) => buf.print_contents("32-bit float"),
445 NABufferType::AudioPacked(ref buf) => buf.print_contents("packed"),
446 NABufferType::Data(ref buf) => { println!("Data buffer, len = {}", buf.len()); },
447 NABufferType::None => { println!("No buffer"); },
448 };
449 }
450 }
451
452 const NA_SIMPLE_VFRAME_COMPONENTS: usize = 4;
453 /// Simplified decoded frame data.
454 pub struct NASimpleVideoFrame<'a, T: Copy> {
455 /// Widths of each picture component.
456 pub width: [usize; NA_SIMPLE_VFRAME_COMPONENTS],
457 /// Heights of each picture component.
458 pub height: [usize; NA_SIMPLE_VFRAME_COMPONENTS],
459 /// Orientation (upside-down or downside-up) flag.
460 pub flip: bool,
461 /// Strides for each component.
462 pub stride: [usize; NA_SIMPLE_VFRAME_COMPONENTS],
463 /// Start of each component.
464 pub offset: [usize; NA_SIMPLE_VFRAME_COMPONENTS],
465 /// Number of components.
466 pub components: usize,
467 /// Pointer to the picture pixel data.
468 pub data: &'a mut [T],
469 }
470
471 impl<'a, T:Copy> NASimpleVideoFrame<'a, T> {
472 /// Constructs a new instance of `NASimpleVideoFrame` from `NAVideoBuffer`.
473 pub fn from_video_buf(vbuf: &'a mut NAVideoBuffer<T>) -> Option<Self> {
474 let vinfo = vbuf.get_info();
475 let components = vinfo.format.components as usize;
476 if components > NA_SIMPLE_VFRAME_COMPONENTS {
477 return None;
478 }
479 let mut w: [usize; NA_SIMPLE_VFRAME_COMPONENTS] = [0; NA_SIMPLE_VFRAME_COMPONENTS];
480 let mut h: [usize; NA_SIMPLE_VFRAME_COMPONENTS] = [0; NA_SIMPLE_VFRAME_COMPONENTS];
481 let mut s: [usize; NA_SIMPLE_VFRAME_COMPONENTS] = [0; NA_SIMPLE_VFRAME_COMPONENTS];
482 let mut o: [usize; NA_SIMPLE_VFRAME_COMPONENTS] = [0; NA_SIMPLE_VFRAME_COMPONENTS];
483 for comp in 0..components {
484 let (width, height) = vbuf.get_dimensions(comp);
485 w[comp] = width;
486 h[comp] = height;
487 s[comp] = vbuf.get_stride(comp);
488 o[comp] = vbuf.get_offset(comp);
489 }
490 let flip = vinfo.flipped;
491 Some(NASimpleVideoFrame {
492 width: w,
493 height: h,
494 flip,
495 stride: s,
496 offset: o,
497 components,
498 data: vbuf.data.as_mut_slice(),
499 })
500 }
501 }
502
503 /// A list of possible frame allocator errors.
504 #[derive(Debug,Clone,Copy,PartialEq)]
505 pub enum AllocatorError {
506 /// Requested picture dimensions are too large.
507 TooLargeDimensions,
508 /// Invalid input format.
509 FormatError,
510 }
511
512 /// Constructs a new video buffer with requested format.
513 ///
514 /// `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.
515 pub fn alloc_video_buffer(vinfo: NAVideoInfo, align: u8) -> Result<NABufferType, AllocatorError> {
516 let fmt = &vinfo.format;
517 let mut new_size: usize = 0;
518 let mut offs: Vec<usize> = Vec::new();
519 let mut strides: Vec<usize> = Vec::new();
520
521 for i in 0..fmt.get_num_comp() {
522 if fmt.get_chromaton(i) == None { return Err(AllocatorError::FormatError); }
523 }
524
525 let align_mod = ((1 << align) as usize) - 1;
526 let width = ((vinfo.width as usize) + align_mod) & !align_mod;
527 let height = ((vinfo.height as usize) + align_mod) & !align_mod;
528 let mut max_depth = 0;
529 let mut all_packed = true;
530 let mut all_bytealigned = true;
531 for i in 0..fmt.get_num_comp() {
532 let ochr = fmt.get_chromaton(i);
533 if ochr.is_none() { continue; }
534 let chr = ochr.unwrap();
535 if !chr.is_packed() {
536 all_packed = false;
537 } else if ((chr.get_shift() + chr.get_depth()) & 7) != 0 {
538 all_bytealigned = false;
539 }
540 max_depth = max(max_depth, chr.get_depth());
541 }
542 let unfit_elem_size = match fmt.get_elem_size() {
543 2 | 4 => false,
544 _ => true,
545 };
546
547 //todo semi-packed like NV12
548 if fmt.is_paletted() {
549 //todo various-sized palettes?
550 let stride = vinfo.get_format().get_chromaton(0).unwrap().get_linesize(width);
551 let pic_sz = stride.checked_mul(height);
552 if pic_sz == None { return Err(AllocatorError::TooLargeDimensions); }
553 let pal_size = 256 * (fmt.get_elem_size() as usize);
554 let new_size = pic_sz.unwrap().checked_add(pal_size);
555 if new_size == None { return Err(AllocatorError::TooLargeDimensions); }
556 offs.push(0);
557 offs.push(stride * height);
558 strides.push(stride);
559 let data: Vec<u8> = vec![0; new_size.unwrap()];
560 let buf: NAVideoBuffer<u8> = NAVideoBuffer { data: NABufferRef::new(data), info: vinfo, offs, strides };
561 Ok(NABufferType::Video(buf.into_ref()))
562 } else if !all_packed {
563 for i in 0..fmt.get_num_comp() {
564 let ochr = fmt.get_chromaton(i);
565 if ochr.is_none() { continue; }
566 let chr = ochr.unwrap();
567 offs.push(new_size as usize);
568 let stride = chr.get_linesize(width);
569 let cur_h = chr.get_height(height);
570 let cur_sz = stride.checked_mul(cur_h);
571 if cur_sz == None { return Err(AllocatorError::TooLargeDimensions); }
572 let new_sz = new_size.checked_add(cur_sz.unwrap());
573 if new_sz == None { return Err(AllocatorError::TooLargeDimensions); }
574 new_size = new_sz.unwrap();
575 strides.push(stride);
576 }
577 if max_depth <= 8 {
578 let data: Vec<u8> = vec![0; new_size];
579 let buf: NAVideoBuffer<u8> = NAVideoBuffer { data: NABufferRef::new(data), info: vinfo, offs, strides };
580 Ok(NABufferType::Video(buf.into_ref()))
581 } else if max_depth <= 16 {
582 let data: Vec<u16> = vec![0; new_size];
583 let buf: NAVideoBuffer<u16> = NAVideoBuffer { data: NABufferRef::new(data), info: vinfo, offs, strides };
584 Ok(NABufferType::Video16(buf.into_ref()))
585 } else {
586 let data: Vec<u32> = vec![0; new_size];
587 let buf: NAVideoBuffer<u32> = NAVideoBuffer { data: NABufferRef::new(data), info: vinfo, offs, strides };
588 Ok(NABufferType::Video32(buf.into_ref()))
589 }
590 } else if all_bytealigned || unfit_elem_size {
591 let elem_sz = fmt.get_elem_size();
592 let line_sz = width.checked_mul(elem_sz as usize);
593 if line_sz == None { return Err(AllocatorError::TooLargeDimensions); }
594 let new_sz = line_sz.unwrap().checked_mul(height);
595 if new_sz == None { return Err(AllocatorError::TooLargeDimensions); }
596 new_size = new_sz.unwrap();
597 let data: Vec<u8> = vec![0; new_size];
598 strides.push(line_sz.unwrap());
599 let buf: NAVideoBuffer<u8> = NAVideoBuffer { data: NABufferRef::new(data), info: vinfo, offs, strides };
600 Ok(NABufferType::VideoPacked(buf.into_ref()))
601 } else {
602 let elem_sz = fmt.get_elem_size();
603 let new_sz = width.checked_mul(height);
604 if new_sz == None { return Err(AllocatorError::TooLargeDimensions); }
605 new_size = new_sz.unwrap();
606 match elem_sz {
607 2 => {
608 let data: Vec<u16> = vec![0; new_size];
609 strides.push(width);
610 let buf: NAVideoBuffer<u16> = NAVideoBuffer { data: NABufferRef::new(data), info: vinfo, offs, strides };
611 Ok(NABufferType::Video16(buf.into_ref()))
612 },
613 4 => {
614 let data: Vec<u32> = vec![0; new_size];
615 strides.push(width);
616 let buf: NAVideoBuffer<u32> = NAVideoBuffer { data: NABufferRef::new(data), info: vinfo, offs, strides };
617 Ok(NABufferType::Video32(buf.into_ref()))
618 },
619 _ => unreachable!(),
620 }
621 }
622 }
623
624 /// Constructs a new audio buffer for the requested format and length.
625 #[allow(clippy::collapsible_if)]
626 pub fn alloc_audio_buffer(ainfo: NAAudioInfo, nsamples: usize, chmap: NAChannelMap) -> Result<NABufferType, AllocatorError> {
627 let mut offs: Vec<usize> = Vec::new();
628 if ainfo.format.is_planar() || ((ainfo.format.get_bits() % 8) == 0) {
629 let len = nsamples.checked_mul(ainfo.channels as usize);
630 if len == None { return Err(AllocatorError::TooLargeDimensions); }
631 let length = len.unwrap();
632 let stride;
633 let step;
634 if ainfo.format.is_planar() {
635 stride = nsamples;
636 step = 1;
637 for i in 0..ainfo.channels {
638 offs.push((i as usize) * stride);
639 }
640 } else {
641 stride = 1;
642 step = ainfo.channels as usize;
643 for i in 0..ainfo.channels {
644 offs.push(i as usize);
645 }
646 }
647 if ainfo.format.is_float() {
648 if ainfo.format.get_bits() == 32 {
649 let data: Vec<f32> = vec![0.0; length];
650 let buf: NAAudioBuffer<f32> = NAAudioBuffer { data: NABufferRef::new(data), info: ainfo, offs, chmap, len: nsamples, stride, step };
651 Ok(NABufferType::AudioF32(buf))
652 } else {
653 Err(AllocatorError::TooLargeDimensions)
654 }
655 } else {
656 if ainfo.format.get_bits() == 8 && !ainfo.format.is_signed() {
657 let data: Vec<u8> = vec![0; length];
658 let buf: NAAudioBuffer<u8> = NAAudioBuffer { data: NABufferRef::new(data), info: ainfo, offs, chmap, len: nsamples, stride, step };
659 Ok(NABufferType::AudioU8(buf))
660 } else if ainfo.format.get_bits() == 16 && ainfo.format.is_signed() {
661 let data: Vec<i16> = vec![0; length];
662 let buf: NAAudioBuffer<i16> = NAAudioBuffer { data: NABufferRef::new(data), info: ainfo, offs, chmap, len: nsamples, stride, step };
663 Ok(NABufferType::AudioI16(buf))
664 } else if ainfo.format.get_bits() == 32 && ainfo.format.is_signed() {
665 let data: Vec<i32> = vec![0; length];
666 let buf: NAAudioBuffer<i32> = NAAudioBuffer { data: NABufferRef::new(data), info: ainfo, offs, chmap, len: nsamples, stride, step };
667 Ok(NABufferType::AudioI32(buf))
668 } else {
669 Err(AllocatorError::TooLargeDimensions)
670 }
671 }
672 } else {
673 let len = nsamples.checked_mul(ainfo.channels as usize);
674 if len == None { return Err(AllocatorError::TooLargeDimensions); }
675 let length = ainfo.format.get_audio_size(len.unwrap() as u64);
676 let data: Vec<u8> = vec![0; length];
677 let buf: NAAudioBuffer<u8> = NAAudioBuffer { data: NABufferRef::new(data), info: ainfo, offs, chmap, len: nsamples, stride: 0, step: 0 };
678 Ok(NABufferType::AudioPacked(buf))
679 }
680 }
681
682 /// Constructs a new buffer for generic data.
683 pub fn alloc_data_buffer(size: usize) -> Result<NABufferType, AllocatorError> {
684 let data: Vec<u8> = vec![0; size];
685 let buf: NABufferRef<Vec<u8>> = NABufferRef::new(data);
686 Ok(NABufferType::Data(buf))
687 }
688
689 /// Creates a clone of current buffer.
690 pub fn copy_buffer(buf: NABufferType) -> NABufferType {
691 buf.clone()
692 }
693
694 /// Video frame pool.
695 ///
696 /// This structure allows codec to effectively reuse old frames instead of allocating and de-allocating frames every time.
697 /// Caller can also reserve some frames for its own purposes e.g. display queue.
698 pub struct NAVideoBufferPool<T:Copy> {
699 pool: Vec<NAVideoBufferRef<T>>,
700 max_len: usize,
701 add_len: usize,
702 }
703
704 impl<T:Copy> NAVideoBufferPool<T> {
705 /// Constructs a new `NAVideoBufferPool` instance.
706 pub fn new(max_len: usize) -> Self {
707 Self {
708 pool: Vec::with_capacity(max_len),
709 max_len,
710 add_len: 0,
711 }
712 }
713 /// Sets the number of buffers reserved for the user.
714 pub fn set_dec_bufs(&mut self, add_len: usize) {
715 self.add_len = add_len;
716 }
717 /// Returns an unused buffer from the pool.
718 pub fn get_free(&mut self) -> Option<NAVideoBufferRef<T>> {
719 for e in self.pool.iter() {
720 if e.get_num_refs() == 1 {
721 return Some(e.clone());
722 }
723 }
724 None
725 }
726 /// Clones provided frame data into a free pool frame.
727 pub fn get_copy(&mut self, rbuf: &NAVideoBufferRef<T>) -> Option<NAVideoBufferRef<T>> {
728 let mut dbuf = self.get_free()?;
729 dbuf.data.copy_from_slice(&rbuf.data);
730 Some(dbuf)
731 }
732 /// Clears the pool from all frames.
733 pub fn reset(&mut self) {
734 self.pool.truncate(0);
735 }
736 }
737
738 impl NAVideoBufferPool<u8> {
739 /// Allocates the target amount of video frames using [`alloc_video_buffer`].
740 ///
741 /// [`alloc_video_buffer`]: ./fn.alloc_video_buffer.html
742 pub fn prealloc_video(&mut self, vinfo: NAVideoInfo, align: u8) -> Result<(), AllocatorError> {
743 let nbufs = self.max_len + self.add_len - self.pool.len();
744 for _ in 0..nbufs {
745 let vbuf = alloc_video_buffer(vinfo, align)?;
746 if let NABufferType::Video(buf) = vbuf {
747 self.pool.push(buf);
748 } else if let NABufferType::VideoPacked(buf) = vbuf {
749 self.pool.push(buf);
750 } else {
751 return Err(AllocatorError::FormatError);
752 }
753 }
754 Ok(())
755 }
756 }
757
758 impl NAVideoBufferPool<u16> {
759 /// Allocates the target amount of video frames using [`alloc_video_buffer`].
760 ///
761 /// [`alloc_video_buffer`]: ./fn.alloc_video_buffer.html
762 pub fn prealloc_video(&mut self, vinfo: NAVideoInfo, align: u8) -> Result<(), AllocatorError> {
763 let nbufs = self.max_len + self.add_len - self.pool.len();
764 for _ in 0..nbufs {
765 let vbuf = alloc_video_buffer(vinfo, align)?;
766 if let NABufferType::Video16(buf) = vbuf {
767 self.pool.push(buf);
768 } else {
769 return Err(AllocatorError::FormatError);
770 }
771 }
772 Ok(())
773 }
774 }
775
776 impl NAVideoBufferPool<u32> {
777 /// Allocates the target amount of video frames using [`alloc_video_buffer`].
778 ///
779 /// [`alloc_video_buffer`]: ./fn.alloc_video_buffer.html
780 pub fn prealloc_video(&mut self, vinfo: NAVideoInfo, align: u8) -> Result<(), AllocatorError> {
781 let nbufs = self.max_len + self.add_len - self.pool.len();
782 for _ in 0..nbufs {
783 let vbuf = alloc_video_buffer(vinfo, align)?;
784 if let NABufferType::Video32(buf) = vbuf {
785 self.pool.push(buf);
786 } else {
787 return Err(AllocatorError::FormatError);
788 }
789 }
790 Ok(())
791 }
792 }
793
794 /// Information about codec contained in a stream.
795 #[allow(dead_code)]
796 #[derive(Clone)]
797 pub struct NACodecInfo {
798 name: &'static str,
799 properties: NACodecTypeInfo,
800 extradata: Option<Arc<Vec<u8>>>,
801 }
802
803 /// A specialised type for reference-counted `NACodecInfo`.
804 pub type NACodecInfoRef = Arc<NACodecInfo>;
805
806 impl NACodecInfo {
807 /// Constructs a new instance of `NACodecInfo`.
808 pub fn new(name: &'static str, p: NACodecTypeInfo, edata: Option<Vec<u8>>) -> Self {
809 let extradata = match edata {
810 None => None,
811 Some(vec) => Some(Arc::new(vec)),
812 };
813 NACodecInfo { name, properties: p, extradata }
814 }
815 /// Constructs a new reference-counted instance of `NACodecInfo`.
816 pub fn new_ref(name: &'static str, p: NACodecTypeInfo, edata: Option<Arc<Vec<u8>>>) -> Self {
817 NACodecInfo { name, properties: p, extradata: edata }
818 }
819 /// Converts current instance into a reference-counted one.
820 pub fn into_ref(self) -> NACodecInfoRef { Arc::new(self) }
821 /// Returns codec information.
822 pub fn get_properties(&self) -> NACodecTypeInfo { self.properties }
823 /// Returns additional initialisation data required by the codec.
824 pub fn get_extradata(&self) -> Option<Arc<Vec<u8>>> {
825 if let Some(ref vec) = self.extradata { return Some(vec.clone()); }
826 None
827 }
828 /// Returns codec name.
829 pub fn get_name(&self) -> &'static str { self.name }
830 /// Reports whether it is a video codec.
831 pub fn is_video(&self) -> bool {
832 if let NACodecTypeInfo::Video(_) = self.properties { return true; }
833 false
834 }
835 /// Reports whether it is an audio codec.
836 pub fn is_audio(&self) -> bool {
837 if let NACodecTypeInfo::Audio(_) = self.properties { return true; }
838 false
839 }
840 /// Constructs a new empty reference-counted instance of `NACodecInfo`.
841 pub fn new_dummy() -> Arc<Self> {
842 Arc::new(DUMMY_CODEC_INFO)
843 }
844 /// Updates codec infomation.
845 pub fn replace_info(&self, p: NACodecTypeInfo) -> Arc<Self> {
846 Arc::new(NACodecInfo { name: self.name, properties: p, extradata: self.extradata.clone() })
847 }
848 }
849
850 impl Default for NACodecInfo {
851 fn default() -> Self { DUMMY_CODEC_INFO }
852 }
853
854 impl fmt::Display for NACodecInfo {
855 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
856 let edata = match self.extradata.clone() {
857 None => "no extradata".to_string(),
858 Some(v) => format!("{} byte(s) of extradata", v.len()),
859 };
860 write!(f, "{}: {} {}", self.name, self.properties, edata)
861 }
862 }
863
864 /// Default empty codec information.
865 pub const DUMMY_CODEC_INFO: NACodecInfo = NACodecInfo {
866 name: "none",
867 properties: NACodecTypeInfo::None,
868 extradata: None };
869
870 /// A list of recognized frame types.
871 #[derive(Debug,Clone,Copy,PartialEq)]
872 #[allow(dead_code)]
873 pub enum FrameType {
874 /// Intra frame type.
875 I,
876 /// Inter frame type.
877 P,
878 /// Bidirectionally predicted frame.
879 B,
880 /// Skip frame.
881 ///
882 /// When such frame is encountered then last frame should be used again if it is needed.
883 Skip,
884 /// Some other frame type.
885 Other,
886 }
887
888 impl fmt::Display for FrameType {
889 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
890 match *self {
891 FrameType::I => write!(f, "I"),
892 FrameType::P => write!(f, "P"),
893 FrameType::B => write!(f, "B"),
894 FrameType::Skip => write!(f, "skip"),
895 FrameType::Other => write!(f, "x"),
896 }
897 }
898 }
899
900 /// Timestamp information.
901 #[derive(Debug,Clone,Copy)]
902 pub struct NATimeInfo {
903 /// Presentation timestamp.
904 pub pts: Option<u64>,
905 /// Decode timestamp.
906 pub dts: Option<u64>,
907 /// Duration (in timebase units).
908 pub duration: Option<u64>,
909 /// Timebase numerator.
910 pub tb_num: u32,
911 /// Timebase denominator.
912 pub tb_den: u32,
913 }
914
915 impl NATimeInfo {
916 /// Constructs a new `NATimeInfo` instance.
917 pub fn new(pts: Option<u64>, dts: Option<u64>, duration: Option<u64>, tb_num: u32, tb_den: u32) -> Self {
918 NATimeInfo { pts, dts, duration, tb_num, tb_den }
919 }
920 /// Returns presentation timestamp.
921 pub fn get_pts(&self) -> Option<u64> { self.pts }
922 /// Returns decoding timestamp.
923 pub fn get_dts(&self) -> Option<u64> { self.dts }
924 /// Returns duration.
925 pub fn get_duration(&self) -> Option<u64> { self.duration }
926 /// Sets new presentation timestamp.
927 pub fn set_pts(&mut self, pts: Option<u64>) { self.pts = pts; }
928 /// Sets new decoding timestamp.
929 pub fn set_dts(&mut self, dts: Option<u64>) { self.dts = dts; }
930 /// Sets new duration.
931 pub fn set_duration(&mut self, dur: Option<u64>) { self.duration = dur; }
932
933 /// Converts time in given scale into timestamp in given base.
934 pub fn time_to_ts(time: u64, base: u64, tb_num: u32, tb_den: u32) -> u64 {
935 let tb_num = u64::from(tb_num);
936 let tb_den = u64::from(tb_den);
937 let tmp = time.checked_mul(tb_num);
938 if let Some(tmp) = tmp {
939 tmp / base / tb_den
940 } else {
941 let tmp = time.checked_mul(tb_num);
942 if let Some(tmp) = tmp {
943 tmp / base / tb_den
944 } else {
945 let coarse = time / base;
946 let tmp = coarse.checked_mul(tb_num);
947 if let Some(tmp) = tmp {
948 tmp / tb_den
949 } else {
950 (coarse / tb_den) * tb_num
951 }
952 }
953 }
954 }
955 /// Converts timestamp in given base into time in given scale.
956 pub fn ts_to_time(ts: u64, base: u64, tb_num: u32, tb_den: u32) -> u64 {
957 let tb_num = u64::from(tb_num);
958 let tb_den = u64::from(tb_den);
959 let tmp = ts.checked_mul(base);
960 if let Some(tmp) = tmp {
961 let tmp2 = tmp.checked_mul(tb_num);
962 if let Some(tmp2) = tmp2 {
963 tmp2 / tb_den
964 } else {
965 (tmp / tb_den) * tb_num
966 }
967 } else {
968 let tmp = ts.checked_mul(tb_num);
969 if let Some(tmp) = tmp {
970 (tmp / tb_den) * base
971 } else {
972 (ts / tb_den) * base * tb_num
973 }
974 }
975 }
976 fn get_cur_ts(&self) -> u64 { self.pts.unwrap_or(self.dts.unwrap_or(0)) }
977 fn get_cur_millis(&self) -> u64 {
978 let ts = self.get_cur_ts();
979 Self::ts_to_time(ts, 1000, self.tb_num, self.tb_den)
980 }
981 /// Checks whether the current time information is earler than provided reference time.
982 pub fn less_than(&self, time: NATimePoint) -> bool {
983 if self.pts.is_none() && self.dts.is_none() {
984 return true;
985 }
986 match time {
987 NATimePoint::PTS(rpts) => self.get_cur_ts() < rpts,
988 NATimePoint::Milliseconds(ms) => self.get_cur_millis() < ms,
989 NATimePoint::None => false,
990 }
991 }
992 /// Checks whether the current time information is the same as provided reference time.
993 pub fn equal(&self, time: NATimePoint) -> bool {
994 if self.pts.is_none() && self.dts.is_none() {
995 return time == NATimePoint::None;
996 }
997 match time {
998 NATimePoint::PTS(rpts) => self.get_cur_ts() == rpts,
999 NATimePoint::Milliseconds(ms) => self.get_cur_millis() == ms,
1000 NATimePoint::None => false,
1001 }
1002 }
1003 }
1004
1005 /// Time information for specifying durations or seek positions.
1006 #[derive(Clone,Copy,Debug,PartialEq)]
1007 pub enum NATimePoint {
1008 /// Time in milliseconds.
1009 Milliseconds(u64),
1010 /// Stream timestamp.
1011 PTS(u64),
1012 /// No time information present.
1013 None,
1014 }
1015
1016 impl Default for NATimePoint {
1017 fn default() -> Self {
1018 NATimePoint::None
1019 }
1020 }
1021
1022 impl fmt::Display for NATimePoint {
1023 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1024 match *self {
1025 NATimePoint::Milliseconds(millis) => {
1026 let tot_s = millis / 1000;
1027 let ms = millis % 1000;
1028 if tot_s < 60 {
1029 if ms != 0 {
1030 return write!(f, "{}.{:03}", tot_s, ms);
1031 } else {
1032 return write!(f, "{}", tot_s);
1033 }
1034 }
1035 let tot_m = tot_s / 60;
1036 let s = tot_s % 60;
1037 if tot_m < 60 {
1038 if ms != 0 {
1039 return write!(f, "{}:{:02}.{:03}", tot_m, s, ms);
1040 } else {
1041 return write!(f, "{}:{:02}", tot_m, s);
1042 }
1043 }
1044 let h = tot_m / 60;
1045 let m = tot_m % 60;
1046 if ms != 0 {
1047 write!(f, "{}:{:02}:{:02}.{:03}", h, m, s, ms)
1048 } else {
1049 write!(f, "{}:{:02}:{:02}", h, m, s)
1050 }
1051 },
1052 NATimePoint::PTS(pts) => {
1053 write!(f, "{}pts", pts)
1054 },
1055 NATimePoint::None => {
1056 write!(f, "none")
1057 },
1058 }
1059 }
1060 }
1061
1062 impl FromStr for NATimePoint {
1063 type Err = FormatParseError;
1064
1065 /// Parses the string into time information.
1066 ///
1067 /// Accepted formats are `<u64>pts`, `<u64>ms` or `[hh:][mm:]ss[.ms]`.
1068 fn from_str(s: &str) -> Result<Self, Self::Err> {
1069 if s.is_empty() {
1070 return Err(FormatParseError {});
1071 }
1072 if !s.ends_with("pts") {
1073 if s.ends_with("ms") {
1074 let str_b = s.as_bytes();
1075 let num = std::str::from_utf8(&str_b[..str_b.len() - 2]).unwrap();
1076 let ret = num.parse::<u64>();
1077 if let Ok(val) = ret {
1078 return Ok(NATimePoint::Milliseconds(val));
1079 } else {
1080 return Err(FormatParseError {});
1081 }
1082 }
1083 let mut parts = s.split(':');
1084 let mut hrs = None;
1085 let mut mins = None;
1086 let mut secs = parts.next();
1087 if let Some(part) = parts.next() {
1088 std::mem::swap(&mut mins, &mut secs);
1089 secs = Some(part);
1090 }
1091 if let Some(part) = parts.next() {
1092 std::mem::swap(&mut hrs, &mut mins);
1093 std::mem::swap(&mut mins, &mut secs);
1094 secs = Some(part);
1095 }
1096 if parts.next().is_some() {
1097 return Err(FormatParseError {});
1098 }
1099 let hours = if let Some(val) = hrs {
1100 let ret = val.parse::<u64>();
1101 if ret.is_err() { return Err(FormatParseError {}); }
1102 let val = ret.unwrap();
1103 if val > 1000 { return Err(FormatParseError {}); }
1104 val
1105 } else { 0 };
1106 let minutes = if let Some(val) = mins {
1107 let ret = val.parse::<u64>();
1108 if ret.is_err() { return Err(FormatParseError {}); }
1109 let val = ret.unwrap();
1110 if val >= 60 { return Err(FormatParseError {}); }
1111 val
1112 } else { 0 };
1113 let (seconds, millis) = if let Some(val) = secs {
1114 let mut parts = val.split('.');
1115 let ret = parts.next().unwrap().parse::<u64>();
1116 if ret.is_err() { return Err(FormatParseError {}); }
1117 let seconds = ret.unwrap();
1118 if mins.is_some() && seconds >= 60 { return Err(FormatParseError {}); }
1119 let millis = if let Some(val) = parts.next() {
1120 let mut mval = 0;
1121 let mut base = 0;
1122 for ch in val.chars() {
1123 if ch >= '0' && ch <= '9' {
1124 mval = mval * 10 + u64::from((ch as u8) - b'0');
1125 base += 1;
1126 if base > 3 { break; }
1127 } else {
1128 return Err(FormatParseError {});
1129 }
1130 }
1131 while base < 3 {
1132 mval *= 10;
1133 base += 1;
1134 }
1135 mval
1136 } else { 0 };
1137 (seconds, millis)
1138 } else { unreachable!(); };
1139 let tot_secs = hours * 60 * 60 + minutes * 60 + seconds;
1140 Ok(NATimePoint::Milliseconds(tot_secs * 1000 + millis))
1141 } else {
1142 let str_b = s.as_bytes();
1143 let num = std::str::from_utf8(&str_b[..str_b.len() - 3]).unwrap();
1144 let ret = num.parse::<u64>();
1145 if let Ok(val) = ret {
1146 Ok(NATimePoint::PTS(val))
1147 } else {
1148 Err(FormatParseError {})
1149 }
1150 }
1151 }
1152 }
1153
1154 /// Decoded frame information.
1155 #[allow(dead_code)]
1156 #[derive(Clone)]
1157 pub struct NAFrame {
1158 /// Frame timestamp.
1159 pub ts: NATimeInfo,
1160 /// Frame ID.
1161 pub id: i64,
1162 buffer: NABufferType,
1163 info: NACodecInfoRef,
1164 /// Frame type.
1165 pub frame_type: FrameType,
1166 /// Keyframe flag.
1167 pub key: bool,
1168 // options: HashMap<String, NAValue>,
1169 }
1170
1171 /// A specialised type for reference-counted `NAFrame`.
1172 pub type NAFrameRef = Arc<NAFrame>;
1173
1174 fn get_plane_size(info: &NAVideoInfo, idx: usize) -> (usize, usize) {
1175 let chromaton = info.get_format().get_chromaton(idx);
1176 if chromaton.is_none() { return (0, 0); }
1177 let (hs, vs) = chromaton.unwrap().get_subsampling();
1178 let w = (info.get_width() + ((1 << hs) - 1)) >> hs;
1179 let h = (info.get_height() + ((1 << vs) - 1)) >> vs;
1180 (w, h)
1181 }
1182
1183 impl NAFrame {
1184 /// Constructs a new `NAFrame` instance.
1185 pub fn new(ts: NATimeInfo,
1186 ftype: FrameType,
1187 keyframe: bool,
1188 info: NACodecInfoRef,
1189 /*options: HashMap<String, NAValue>,*/
1190 buffer: NABufferType) -> Self {
1191 NAFrame { ts, id: 0, buffer, info, frame_type: ftype, key: keyframe/*, options*/ }
1192 }
1193 /// Returns frame format information.
1194 pub fn get_info(&self) -> NACodecInfoRef { self.info.clone() }
1195 /// Returns frame type.
1196 pub fn get_frame_type(&self) -> FrameType { self.frame_type }
1197 /// Reports whether the frame is a keyframe.
1198 pub fn is_keyframe(&self) -> bool { self.key }
1199 /// Sets new frame type.
1200 pub fn set_frame_type(&mut self, ftype: FrameType) { self.frame_type = ftype; }
1201 /// Sets keyframe flag.
1202 pub fn set_keyframe(&mut self, key: bool) { self.key = key; }
1203 /// Returns frame timestamp.
1204 pub fn get_time_information(&self) -> NATimeInfo { self.ts }
1205 /// Returns frame presentation time.
1206 pub fn get_pts(&self) -> Option<u64> { self.ts.get_pts() }
1207 /// Returns frame decoding time.
1208 pub fn get_dts(&self) -> Option<u64> { self.ts.get_dts() }
1209 /// Returns picture ID.
1210 pub fn get_id(&self) -> i64 { self.id }
1211 /// Returns frame display duration.
1212 pub fn get_duration(&self) -> Option<u64> { self.ts.get_duration() }
1213 /// Sets new presentation timestamp.
1214 pub fn set_pts(&mut self, pts: Option<u64>) { self.ts.set_pts(pts); }
1215 /// Sets new decoding timestamp.
1216 pub fn set_dts(&mut self, dts: Option<u64>) { self.ts.set_dts(dts); }
1217 /// Sets new picture ID.
1218 pub fn set_id(&mut self, id: i64) { self.id = id; }
1219 /// Sets new duration.
1220 pub fn set_duration(&mut self, dur: Option<u64>) { self.ts.set_duration(dur); }
1221
1222 /// Returns a reference to the frame data.
1223 pub fn get_buffer(&self) -> NABufferType { self.buffer.clone() }
1224
1225 /// Converts current instance into a reference-counted one.
1226 pub fn into_ref(self) -> NAFrameRef { Arc::new(self) }
1227
1228 /// Creates new frame with metadata from `NAPacket`.
1229 pub fn new_from_pkt(pkt: &NAPacket, info: NACodecInfoRef, buf: NABufferType) -> NAFrame {
1230 NAFrame::new(pkt.ts, FrameType::Other, pkt.keyframe, info, /*HashMap::new(),*/ buf)
1231 }
1232 }
1233
1234 impl fmt::Display for NAFrame {
1235 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1236 let mut ostr = format!("frame type {}", self.frame_type);
1237 if let Some(pts) = self.ts.pts { ostr = format!("{} pts {}", ostr, pts); }
1238 if let Some(dts) = self.ts.dts { ostr = format!("{} dts {}", ostr, dts); }
1239 if let Some(dur) = self.ts.duration { ostr = format!("{} duration {}", ostr, dur); }
1240 if self.key { ostr = format!("{} kf", ostr); }
1241 write!(f, "[{}]", ostr)
1242 }
1243 }
1244
1245 /// A list of possible stream types.
1246 #[derive(Debug,Clone,Copy,PartialEq)]
1247 #[allow(dead_code)]
1248 pub enum StreamType {
1249 /// Video stream.
1250 Video,
1251 /// Audio stream.
1252 Audio,
1253 /// Subtitles.
1254 Subtitles,
1255 /// Any data stream (or might be an unrecognized audio/video stream).
1256 Data,
1257 /// Nonexistent stream.
1258 None,
1259 }
1260
1261 impl fmt::Display for StreamType {
1262 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1263 match *self {
1264 StreamType::Video => write!(f, "Video"),
1265 StreamType::Audio => write!(f, "Audio"),
1266 StreamType::Subtitles => write!(f, "Subtitles"),
1267 StreamType::Data => write!(f, "Data"),
1268 StreamType::None => write!(f, "-"),
1269 }
1270 }
1271 }
1272
1273 /// Stream data.
1274 #[allow(dead_code)]
1275 #[derive(Clone)]
1276 pub struct NAStream {
1277 media_type: StreamType,
1278 /// Stream ID.
1279 pub id: u32,
1280 num: usize,
1281 info: NACodecInfoRef,
1282 /// Timebase numerator.
1283 pub tb_num: u32,
1284 /// Timebase denominator.
1285 pub tb_den: u32,
1286 }
1287
1288 /// A specialised reference-counted `NAStream` type.
1289 pub type NAStreamRef = Arc<NAStream>;
1290
1291 /// Downscales the timebase by its greatest common denominator.
1292 pub fn reduce_timebase(tb_num: u32, tb_den: u32) -> (u32, u32) {
1293 if tb_num == 0 { return (tb_num, tb_den); }
1294 if (tb_den % tb_num) == 0 { return (1, tb_den / tb_num); }
1295
1296 let mut a = tb_num;
1297 let mut b = tb_den;
1298
1299 while a != b {
1300 if a > b { a -= b; }
1301 else if b > a { b -= a; }
1302 }
1303
1304 (tb_num / a, tb_den / a)
1305 }
1306
1307 impl NAStream {
1308 /// Constructs a new `NAStream` instance.
1309 pub fn new(mt: StreamType, id: u32, info: NACodecInfo, tb_num: u32, tb_den: u32) -> Self {
1310 let (n, d) = reduce_timebase(tb_num, tb_den);
1311 NAStream { media_type: mt, id, num: 0, info: info.into_ref(), tb_num: n, tb_den: d }
1312 }
1313 /// Returns stream id.
1314 pub fn get_id(&self) -> u32 { self.id }
1315 /// Returns stream type.
1316 pub fn get_media_type(&self) -> StreamType { self.media_type }
1317 /// Returns stream number assigned by demuxer.
1318 pub fn get_num(&self) -> usize { self.num }
1319 /// Sets stream number.
1320 pub fn set_num(&mut self, num: usize) { self.num = num; }
1321 /// Returns codec information.
1322 pub fn get_info(&self) -> NACodecInfoRef { self.info.clone() }
1323 /// Returns stream timebase.
1324 pub fn get_timebase(&self) -> (u32, u32) { (self.tb_num, self.tb_den) }
1325 /// Sets new stream timebase.
1326 pub fn set_timebase(&mut self, tb_num: u32, tb_den: u32) {
1327 let (n, d) = reduce_timebase(tb_num, tb_den);
1328 self.tb_num = n;
1329 self.tb_den = d;
1330 }
1331 /// Converts current instance into a reference-counted one.
1332 pub fn into_ref(self) -> NAStreamRef { Arc::new(self) }
1333 }
1334
1335 impl fmt::Display for NAStream {
1336 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1337 write!(f, "({}#{} @ {}/{} - {})", self.media_type, self.id, self.tb_num, self.tb_den, self.info.get_properties())
1338 }
1339 }
1340
1341 /// Side data that may accompany demuxed data.
1342 #[derive(Clone)]
1343 pub enum NASideData {
1344 /// Palette information.
1345 ///
1346 /// This side data contains a flag signalling that palette has changed since previous time and a reference to the current palette.
1347 /// Palette is stored in 8-bit RGBA format.
1348 Palette(bool, Arc<[u8; 1024]>),
1349 /// Generic user data.
1350 UserData(Arc<Vec<u8>>),
1351 }
1352
1353 /// Packet with compressed data.
1354 #[allow(dead_code)]
1355 pub struct NAPacket {
1356 stream: NAStreamRef,
1357 /// Packet timestamp.
1358 pub ts: NATimeInfo,
1359 buffer: NABufferRef<Vec<u8>>,
1360 /// Keyframe flag.
1361 pub keyframe: bool,
1362 // options: HashMap<String, NAValue<'a>>,
1363 /// Packet side data (e.g. palette for paletted formats).
1364 pub side_data: Vec<NASideData>,
1365 }
1366
1367 impl NAPacket {
1368 /// Constructs a new `NAPacket` instance.
1369 pub fn new(str: NAStreamRef, ts: NATimeInfo, kf: bool, vec: Vec<u8>) -> Self {
1370 // let mut vec: Vec<u8> = Vec::new();
1371 // vec.resize(size, 0);
1372 NAPacket { stream: str, ts, keyframe: kf, buffer: NABufferRef::new(vec), side_data: Vec::new() }
1373 }
1374 /// Constructs a new `NAPacket` instance reusing a buffer reference.
1375 pub fn new_from_refbuf(str: NAStreamRef, ts: NATimeInfo, kf: bool, buffer: NABufferRef<Vec<u8>>) -> Self {
1376 NAPacket { stream: str, ts, keyframe: kf, buffer, side_data: Vec::new() }
1377 }
1378 /// Returns information about the stream packet belongs to.
1379 pub fn get_stream(&self) -> NAStreamRef { self.stream.clone() }
1380 /// Returns packet timestamp.
1381 pub fn get_time_information(&self) -> NATimeInfo { self.ts }
1382 /// Returns packet presentation timestamp.
1383 pub fn get_pts(&self) -> Option<u64> { self.ts.get_pts() }
1384 /// Returns packet decoding timestamp.
1385 pub fn get_dts(&self) -> Option<u64> { self.ts.get_dts() }
1386 /// Returns packet duration.
1387 pub fn get_duration(&self) -> Option<u64> { self.ts.get_duration() }
1388 /// Reports whether this is a keyframe packet.
1389 pub fn is_keyframe(&self) -> bool { self.keyframe }
1390 /// Returns a reference to packet data.
1391 pub fn get_buffer(&self) -> NABufferRef<Vec<u8>> { self.buffer.clone() }
1392 /// Adds side data for a packet.
1393 pub fn add_side_data(&mut self, side_data: NASideData) { self.side_data.push(side_data); }
1394 /// Assigns packet to a new stream.
1395 pub fn reassign(&mut self, str: NAStreamRef, ts: NATimeInfo) {
1396 self.stream = str;
1397 self.ts = ts;
1398 }
1399 }
1400
1401 impl Drop for NAPacket {
1402 fn drop(&mut self) {}
1403 }
1404
1405 impl fmt::Display for NAPacket {
1406 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1407 let mut ostr = format!("[pkt for {} size {}", self.stream, self.buffer.len());
1408 if let Some(pts) = self.ts.pts { ostr = format!("{} pts {}", ostr, pts); }
1409 if let Some(dts) = self.ts.dts { ostr = format!("{} dts {}", ostr, dts); }
1410 if let Some(dur) = self.ts.duration { ostr = format!("{} duration {}", ostr, dur); }
1411 if self.keyframe { ostr = format!("{} kf", ostr); }
1412 ostr += "]";
1413 write!(f, "{}", ostr)
1414 }
1415 }
1416
1417 #[cfg(test)]
1418 mod test {
1419 use super::*;
1420
1421 #[test]
1422 fn test_time_parse() {
1423 assert_eq!(NATimePoint::PTS(42).to_string(), "42pts");
1424 assert_eq!(NATimePoint::Milliseconds(4242000).to_string(), "1:10:42");
1425 assert_eq!(NATimePoint::Milliseconds(42424242).to_string(), "11:47:04.242");
1426 let ret = NATimePoint::from_str("42pts");
1427 assert_eq!(ret.unwrap(), NATimePoint::PTS(42));
1428 let ret = NATimePoint::from_str("1:2:3");
1429 assert_eq!(ret.unwrap(), NATimePoint::Milliseconds(3723000));
1430 let ret = NATimePoint::from_str("1:2:3.42");
1431 assert_eq!(ret.unwrap(), NATimePoint::Milliseconds(3723420));
1432 }
1433 }