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