core: split options into separate module
[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
9 /// Audio stream information.
10 #[allow(dead_code)]
11 #[derive(Clone,Copy,PartialEq)]
12 pub struct NAAudioInfo {
13 /// Sample rate.
14 pub sample_rate: u32,
15 /// Number of channels.
16 pub channels: u8,
17 /// Audio sample format.
18 pub format: NASoniton,
19 /// Length of one audio block in samples.
20 pub block_len: usize,
21 }
22
23 impl NAAudioInfo {
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 }
27 }
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 }
36 }
37
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)
41 }
42 }
43
44 /// Video stream information.
45 #[allow(dead_code)]
46 #[derive(Clone,Copy,PartialEq)]
47 pub struct NAVideoInfo {
48 /// Picture width.
49 pub width: usize,
50 /// Picture height.
51 pub height: usize,
52 /// Picture is stored downside up.
53 pub flipped: bool,
54 /// Picture pixel format.
55 pub format: NAPixelFormaton,
56 }
57
58 impl NAVideoInfo {
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 }
62 }
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; }
75 }
76
77 impl fmt::Display for NAVideoInfo {
78 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
79 write!(f, "{}x{}", self.width, self.height)
80 }
81 }
82
83 /// A list of possible stream information types.
84 #[derive(Clone,Copy,PartialEq)]
85 pub enum NACodecTypeInfo {
86 /// No codec present.
87 None,
88 /// Audio codec information.
89 Audio(NAAudioInfo),
90 /// Video codec information.
91 Video(NAVideoInfo),
92 }
93
94 impl NACodecTypeInfo {
95 /// Returns video stream information.
96 pub fn get_video_info(&self) -> Option<NAVideoInfo> {
97 match *self {
98 NACodecTypeInfo::Video(vinfo) => Some(vinfo),
99 _ => None,
100 }
101 }
102 /// Returns audio stream information.
103 pub fn get_audio_info(&self) -> Option<NAAudioInfo> {
104 match *self {
105 NACodecTypeInfo::Audio(ainfo) => Some(ainfo),
106 _ => None,
107 }
108 }
109 /// Reports whether the current stream is video stream.
110 pub fn is_video(&self) -> bool {
111 match *self {
112 NACodecTypeInfo::Video(_) => true,
113 _ => false,
114 }
115 }
116 /// Reports whether the current stream is audio stream.
117 pub fn is_audio(&self) -> bool {
118 match *self {
119 NACodecTypeInfo::Audio(_) => true,
120 _ => false,
121 }
122 }
123 }
124
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),
131 };
132 write!(f, "{}", ret)
133 }
134 }
135
136 /// Decoded video frame.
137 ///
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.
140 #[derive(Clone)]
141 pub struct NAVideoBuffer<T> {
142 info: NAVideoInfo,
143 data: NABufferRef<Vec<T>>,
144 offs: Vec<usize>,
145 strides: Vec<usize>,
146 }
147
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] }
153 }
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 }
171 }
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; }
175 self.strides[idx]
176 }
177 /// Returns requested component dimensions.
178 pub fn get_dimensions(&self, idx: usize) -> (usize, usize) {
179 get_plane_size(&self.info, idx)
180 }
181 /// Converts current instance into buffer reference.
182 pub fn into_ref(self) -> NABufferRef<Self> {
183 NABufferRef::new(self)
184 }
185
186 fn print_contents(&self, datatype: &str) {
187 println!("{} video buffer size {}", datatype, self.data.len());
188 println!(" format {}", self.info);
189 print!(" offsets:");
190 for off in self.offs.iter() {
191 print!(" {}", *off);
192 }
193 println!();
194 print!(" strides:");
195 for stride in self.strides.iter() {
196 print!(" {}", *stride);
197 }
198 println!();
199 }
200 }
201
202 /// A specialised type for reference-counted `NAVideoBuffer`.
203 pub type NAVideoBufferRef<T> = NABufferRef<NAVideoBuffer<T>>;
204
205 /// Decoded audio frame.
206 ///
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.
209 #[derive(Clone)]
210 pub struct NAAudioBuffer<T> {
211 info: NAAudioInfo,
212 data: NABufferRef<Vec<T>>,
213 offs: Vec<usize>,
214 stride: usize,
215 step: usize,
216 chmap: NAChannelMap,
217 len: usize,
218 }
219
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] }
225 }
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 }
245 }
246 /// Return the length of frame in samples.
247 pub fn get_length(&self) -> usize { self.len }
248
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);
253 print!(" offsets:");
254 for off in self.offs.iter() {
255 print!(" {}", *off);
256 }
257 println!();
258 }
259 }
260
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 }
266 }
267 }
268
269 /// A list of possible decoded frame types.
270 #[derive(Clone)]
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>>),
292 /// No data present.
293 None,
294 }
295
296 impl NABufferType {
297 /// Returns the offset to the requested component or channel.
298 pub fn get_offset(&self, idx: usize) -> usize {
299 match *self {
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),
309 _ => 0,
310 }
311 }
312 /// Returns information for video frames.
313 pub fn get_video_info(&self) -> Option<NAVideoInfo> {
314 match *self {
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()),
319 _ => None,
320 }
321 }
322 /// Returns reference to 8-bit (or packed) video buffer.
323 pub fn get_vbuf(&self) -> Option<NAVideoBufferRef<u8>> {
324 match *self {
325 NABufferType::Video(ref vb) => Some(vb.clone()),
326 NABufferType::VideoPacked(ref vb) => Some(vb.clone()),
327 _ => None,
328 }
329 }
330 /// Returns reference to 16-bit video buffer.
331 pub fn get_vbuf16(&self) -> Option<NAVideoBufferRef<u16>> {
332 match *self {
333 NABufferType::Video16(ref vb) => Some(vb.clone()),
334 _ => None,
335 }
336 }
337 /// Returns reference to 32-bit video buffer.
338 pub fn get_vbuf32(&self) -> Option<NAVideoBufferRef<u32>> {
339 match *self {
340 NABufferType::Video32(ref vb) => Some(vb.clone()),
341 _ => None,
342 }
343 }
344 /// Returns information for audio frames.
345 pub fn get_audio_info(&self) -> Option<NAAudioInfo> {
346 match *self {
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()),
352 _ => None,
353 }
354 }
355 /// Returns audio channel map.
356 pub fn get_chmap(&self) -> Option<&NAChannelMap> {
357 match *self {
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()),
363 _ => None,
364 }
365 }
366 /// Returns audio frame duration in samples.
367 pub fn get_audio_length(&self) -> usize {
368 match *self {
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(),
374 _ => 0,
375 }
376 }
377 /// Returns the distance between starts of two channels.
378 pub fn get_audio_stride(&self) -> usize {
379 match *self {
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(),
385 _ => 0,
386 }
387 }
388 /// Returns the distance between two samples in one channel.
389 pub fn get_audio_step(&self) -> usize {
390 match *self {
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(),
396 _ => 0,
397 }
398 }
399 /// Returns reference to 8-bit (or packed) audio buffer.
400 pub fn get_abuf_u8(&self) -> Option<NAAudioBuffer<u8>> {
401 match *self {
402 NABufferType::AudioU8(ref ab) => Some(ab.clone()),
403 NABufferType::AudioPacked(ref ab) => Some(ab.clone()),
404 _ => None,
405 }
406 }
407 /// Returns reference to 16-bit audio buffer.
408 pub fn get_abuf_i16(&self) -> Option<NAAudioBuffer<i16>> {
409 match *self {
410 NABufferType::AudioI16(ref ab) => Some(ab.clone()),
411 _ => None,
412 }
413 }
414 /// Returns reference to 32-bit integer audio buffer.
415 pub fn get_abuf_i32(&self) -> Option<NAAudioBuffer<i32>> {
416 match *self {
417 NABufferType::AudioI32(ref ab) => Some(ab.clone()),
418 _ => None,
419 }
420 }
421 /// Returns reference to 32-bit floating point audio buffer.
422 pub fn get_abuf_f32(&self) -> Option<NAAudioBuffer<f32>> {
423 match *self {
424 NABufferType::AudioF32(ref ab) => Some(ab.clone()),
425 _ => None,
426 }
427 }
428 /// Prints internal buffer layout.
429 pub fn print_buffer_metadata(&self) {
430 match *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"); },
442 };
443 }
444 }
445
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.
454 pub flip: bool,
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],
463 }
464
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 {
471 return None;
472 }
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);
479 w[comp] = width;
480 h[comp] = height;
481 s[comp] = vbuf.get_stride(comp);
482 o[comp] = vbuf.get_offset(comp);
483 }
484 let flip = vinfo.flipped;
485 Some(NASimpleVideoFrame {
486 width: w,
487 height: h,
488 flip,
489 stride: s,
490 offset: o,
491 components,
492 data: vbuf.data.as_mut_slice(),
493 })
494 }
495 }
496
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.
501 TooLargeDimensions,
502 /// Invalid input format.
503 FormatError,
504 }
505
506 /// Constructs a new video buffer with requested format.
507 ///
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();
514
515 for i in 0..fmt.get_num_comp() {
516 if fmt.get_chromaton(i) == None { return Err(AllocatorError::FormatError); }
517 }
518
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() {
530 all_packed = false;
531 } else if ((chr.get_shift() + chr.get_depth()) & 7) != 0 {
532 all_bytealigned = false;
533 }
534 max_depth = max(max_depth, chr.get_depth());
535 }
536 let unfit_elem_size = match fmt.get_elem_size() {
537 2 | 4 => false,
538 _ => true,
539 };
540
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); }
550 offs.push(0);
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);
570 }
571 if max_depth <= 8 {
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()))
579 } else {
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()))
583 }
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()))
595 } else {
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();
600 match elem_sz {
601 2 => {
602 let data: Vec<u16> = vec![0; new_size];
603 strides.push(width);
604 let buf: NAVideoBuffer<u16> = NAVideoBuffer { data: NABufferRef::new(data), info: vinfo, offs, strides };
605 Ok(NABufferType::Video16(buf.into_ref()))
606 },
607 4 => {
608 let data: Vec<u32> = vec![0; new_size];
609 strides.push(width);
610 let buf: NAVideoBuffer<u32> = NAVideoBuffer { data: NABufferRef::new(data), info: vinfo, offs, strides };
611 Ok(NABufferType::Video32(buf.into_ref()))
612 },
613 _ => unreachable!(),
614 }
615 }
616 }
617
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();
626 let stride;
627 let step;
628 if ainfo.format.is_planar() {
629 stride = nsamples;
630 step = 1;
631 for i in 0..ainfo.channels {
632 offs.push((i as usize) * stride);
633 }
634 } else {
635 stride = 1;
636 step = ainfo.channels as usize;
637 for i in 0..ainfo.channels {
638 offs.push(i as usize);
639 }
640 }
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))
646 } else {
647 Err(AllocatorError::TooLargeDimensions)
648 }
649 } else {
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))
658 } else {
659 Err(AllocatorError::TooLargeDimensions)
660 }
661 }
662 } else {
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))
669 }
670 }
671
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))
677 }
678
679 /// Creates a clone of current buffer.
680 pub fn copy_buffer(buf: NABufferType) -> NABufferType {
681 buf.clone()
682 }
683
684 /// Video frame pool.
685 ///
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>>,
690 max_len: usize,
691 add_len: usize,
692 }
693
694 impl<T:Copy> NAVideoBufferPool<T> {
695 /// Constructs a new `NAVideoBufferPool` instance.
696 pub fn new(max_len: usize) -> Self {
697 Self {
698 pool: Vec::with_capacity(max_len),
699 max_len,
700 add_len: 0,
701 }
702 }
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;
706 }
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());
712 }
713 }
714 None
715 }
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);
720 Some(dbuf)
721 }
722 /// Clears the pool from all frames.
723 pub fn reset(&mut self) {
724 self.pool.truncate(0);
725 }
726 }
727
728 impl NAVideoBufferPool<u8> {
729 /// Allocates the target amount of video frames using [`alloc_video_buffer`].
730 ///
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();
734 for _ in 0..nbufs {
735 let vbuf = alloc_video_buffer(vinfo, align)?;
736 if let NABufferType::Video(buf) = vbuf {
737 self.pool.push(buf);
738 } else if let NABufferType::VideoPacked(buf) = vbuf {
739 self.pool.push(buf);
740 } else {
741 return Err(AllocatorError::FormatError);
742 }
743 }
744 Ok(())
745 }
746 }
747
748 impl NAVideoBufferPool<u16> {
749 /// Allocates the target amount of video frames using [`alloc_video_buffer`].
750 ///
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();
754 for _ in 0..nbufs {
755 let vbuf = alloc_video_buffer(vinfo, align)?;
756 if let NABufferType::Video16(buf) = vbuf {
757 self.pool.push(buf);
758 } else {
759 return Err(AllocatorError::FormatError);
760 }
761 }
762 Ok(())
763 }
764 }
765
766 impl NAVideoBufferPool<u32> {
767 /// Allocates the target amount of video frames using [`alloc_video_buffer`].
768 ///
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();
772 for _ in 0..nbufs {
773 let vbuf = alloc_video_buffer(vinfo, align)?;
774 if let NABufferType::Video32(buf) = vbuf {
775 self.pool.push(buf);
776 } else {
777 return Err(AllocatorError::FormatError);
778 }
779 }
780 Ok(())
781 }
782 }
783
784 /// Information about codec contained in a stream.
785 #[allow(dead_code)]
786 #[derive(Clone)]
787 pub struct NACodecInfo {
788 name: &'static str,
789 properties: NACodecTypeInfo,
790 extradata: Option<Arc<Vec<u8>>>,
791 }
792
793 /// A specialised type for reference-counted `NACodecInfo`.
794 pub type NACodecInfoRef = Arc<NACodecInfo>;
795
796 impl 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 {
800 None => None,
801 Some(vec) => Some(Arc::new(vec)),
802 };
803 NACodecInfo { name, properties: p, extradata }
804 }
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 }
808 }
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()); }
816 None
817 }
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; }
823 false
824 }
825 /// Reports whether it is an audio codec.
826 pub fn is_audio(&self) -> bool {
827 if let NACodecTypeInfo::Audio(_) = self.properties { return true; }
828 false
829 }
830 /// Constructs a new empty reference-counted instance of `NACodecInfo`.
831 pub fn new_dummy() -> Arc<Self> {
832 Arc::new(DUMMY_CODEC_INFO)
833 }
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() })
837 }
838 }
839
840 impl Default for NACodecInfo {
841 fn default() -> Self { DUMMY_CODEC_INFO }
842 }
843
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()),
849 };
850 write!(f, "{}: {} {}", self.name, self.properties, edata)
851 }
852 }
853
854 /// Default empty codec information.
855 pub const DUMMY_CODEC_INFO: NACodecInfo = NACodecInfo {
856 name: "none",
857 properties: NACodecTypeInfo::None,
858 extradata: None };
859
860 /// A list of recognized frame types.
861 #[derive(Debug,Clone,Copy,PartialEq)]
862 #[allow(dead_code)]
863 pub enum FrameType {
864 /// Intra frame type.
865 I,
866 /// Inter frame type.
867 P,
868 /// Bidirectionally predicted frame.
869 B,
870 /// Skip frame.
871 ///
872 /// When such frame is encountered then last frame should be used again if it is needed.
873 Skip,
874 /// Some other frame type.
875 Other,
876 }
877
878 impl fmt::Display for FrameType {
879 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
880 match *self {
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"),
886 }
887 }
888 }
889
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.
900 pub tb_num: u32,
901 /// Timebase denominator.
902 pub tb_den: u32,
903 }
904
905 impl NATimeInfo {
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 }
909 }
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; }
922
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 {
929 tmp / base / tb_den
930 } else {
931 let tmp = time.checked_mul(tb_num);
932 if let Some(tmp) = tmp {
933 tmp / base / tb_den
934 } else {
935 let coarse = time / base;
936 let tmp = coarse.checked_mul(tb_num);
937 if let Some(tmp) = tmp {
938 tmp / tb_den
939 } else {
940 (coarse / tb_den) * tb_num
941 }
942 }
943 }
944 }
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 {
953 tmp2 / tb_den
954 } else {
955 (tmp / tb_den) * tb_num
956 }
957 } else {
958 let tmp = ts.checked_mul(tb_num);
959 if let Some(tmp) = tmp {
960 (tmp / tb_den) * base
961 } else {
962 (ts / tb_den) * base * tb_num
963 }
964 }
965 }
966 }
967
968 /// Decoded frame information.
969 #[allow(dead_code)]
970 #[derive(Clone)]
971 pub struct NAFrame {
972 /// Frame timestamp.
973 pub ts: NATimeInfo,
974 /// Frame ID.
975 pub id: i64,
976 buffer: NABufferType,
977 info: NACodecInfoRef,
978 /// Frame type.
979 pub frame_type: FrameType,
980 /// Keyframe flag.
981 pub key: bool,
982 // options: HashMap<String, NAValue>,
983 }
984
985 /// A specialised type for reference-counted `NAFrame`.
986 pub type NAFrameRef = Arc<NAFrame>;
987
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;
994 (w, h)
995 }
996
997 impl NAFrame {
998 /// Constructs a new `NAFrame` instance.
999 pub fn new(ts: NATimeInfo,
1000 ftype: FrameType,
1001 keyframe: bool,
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*/ }
1006 }
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); }
1035
1036 /// Returns a reference to the frame data.
1037 pub fn get_buffer(&self) -> NABufferType { self.buffer.clone() }
1038
1039 /// Converts current instance into a reference-counted one.
1040 pub fn into_ref(self) -> NAFrameRef { Arc::new(self) }
1041
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)
1045 }
1046 }
1047
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)
1056 }
1057 }
1058
1059 /// A list of possible stream types.
1060 #[derive(Debug,Clone,Copy,PartialEq)]
1061 #[allow(dead_code)]
1062 pub enum StreamType {
1063 /// Video stream.
1064 Video,
1065 /// Audio stream.
1066 Audio,
1067 /// Subtitles.
1068 Subtitles,
1069 /// Any data stream (or might be an unrecognized audio/video stream).
1070 Data,
1071 /// Nonexistent stream.
1072 None,
1073 }
1074
1075 impl fmt::Display for StreamType {
1076 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1077 match *self {
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, "-"),
1083 }
1084 }
1085 }
1086
1087 /// Stream data.
1088 #[allow(dead_code)]
1089 #[derive(Clone)]
1090 pub struct NAStream {
1091 media_type: StreamType,
1092 /// Stream ID.
1093 pub id: u32,
1094 num: usize,
1095 info: NACodecInfoRef,
1096 /// Timebase numerator.
1097 pub tb_num: u32,
1098 /// Timebase denominator.
1099 pub tb_den: u32,
1100 }
1101
1102 /// A specialised reference-counted `NAStream` type.
1103 pub type NAStreamRef = Arc<NAStream>;
1104
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); }
1109
1110 let mut a = tb_num;
1111 let mut b = tb_den;
1112
1113 while a != b {
1114 if a > b { a -= b; }
1115 else if b > a { b -= a; }
1116 }
1117
1118 (tb_num / a, tb_den / a)
1119 }
1120
1121 impl NAStream {
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 }
1126 }
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);
1142 self.tb_num = n;
1143 self.tb_den = d;
1144 }
1145 /// Converts current instance into a reference-counted one.
1146 pub fn into_ref(self) -> NAStreamRef { Arc::new(self) }
1147 }
1148
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())
1152 }
1153 }
1154
1155 /// Side data that may accompany demuxed data.
1156 #[derive(Clone)]
1157 pub enum NASideData {
1158 /// Palette information.
1159 ///
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>>),
1165 }
1166
1167 /// Packet with compressed data.
1168 #[allow(dead_code)]
1169 pub struct NAPacket {
1170 stream: NAStreamRef,
1171 /// Packet timestamp.
1172 pub ts: NATimeInfo,
1173 buffer: NABufferRef<Vec<u8>>,
1174 /// Keyframe flag.
1175 pub keyframe: bool,
1176 // options: HashMap<String, NAValue<'a>>,
1177 /// Packet side data (e.g. palette for paletted formats).
1178 pub side_data: Vec<NASideData>,
1179 }
1180
1181 impl NAPacket {
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() }
1187 }
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) {
1206 self.stream = str;
1207 self.ts = ts;
1208 }
1209 }
1210
1211 impl Drop for NAPacket {
1212 fn drop(&mut self) {}
1213 }
1214
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); }
1222 ostr += "]";
1223 write!(f, "{}", ostr)
1224 }
1225 }