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