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