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