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