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