Commit | Line | Data |
---|---|---|
22cb00db | 1 | use std::cmp::max; |
5869fd63 | 2 | use std::collections::HashMap; |
83e603fa | 3 | use std::fmt; |
2422d969 | 4 | use std::sync::Arc; |
4e8b4f31 | 5 | pub use crate::formats::*; |
1a967e6b | 6 | pub use crate::refs::*; |
94dbb551 | 7 | |
5869fd63 | 8 | #[allow(dead_code)] |
66116504 | 9 | #[derive(Clone,Copy,PartialEq)] |
5869fd63 KS |
10 | pub struct NAAudioInfo { |
11 | sample_rate: u32, | |
12 | channels: u8, | |
13 | format: NASoniton, | |
14 | block_len: usize, | |
15 | } | |
16 | ||
17 | impl NAAudioInfo { | |
18 | pub fn new(sr: u32, ch: u8, fmt: NASoniton, bl: usize) -> Self { | |
19 | NAAudioInfo { sample_rate: sr, channels: ch, format: fmt, block_len: bl } | |
20 | } | |
66116504 KS |
21 | pub fn get_sample_rate(&self) -> u32 { self.sample_rate } |
22 | pub fn get_channels(&self) -> u8 { self.channels } | |
23 | pub fn get_format(&self) -> NASoniton { self.format } | |
24 | pub fn get_block_len(&self) -> usize { self.block_len } | |
5869fd63 KS |
25 | } |
26 | ||
83e603fa KS |
27 | impl fmt::Display for NAAudioInfo { |
28 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | |
29 | write!(f, "{} Hz, {} ch", self.sample_rate, self.channels) | |
30 | } | |
31 | } | |
32 | ||
5869fd63 | 33 | #[allow(dead_code)] |
66116504 | 34 | #[derive(Clone,Copy,PartialEq)] |
5869fd63 | 35 | pub struct NAVideoInfo { |
66116504 KS |
36 | width: usize, |
37 | height: usize, | |
5869fd63 KS |
38 | flipped: bool, |
39 | format: NAPixelFormaton, | |
40 | } | |
41 | ||
42 | impl NAVideoInfo { | |
66116504 | 43 | pub fn new(w: usize, h: usize, flip: bool, fmt: NAPixelFormaton) -> Self { |
5869fd63 KS |
44 | NAVideoInfo { width: w, height: h, flipped: flip, format: fmt } |
45 | } | |
66116504 KS |
46 | pub fn get_width(&self) -> usize { self.width as usize } |
47 | pub fn get_height(&self) -> usize { self.height as usize } | |
48 | pub fn is_flipped(&self) -> bool { self.flipped } | |
49 | pub fn get_format(&self) -> NAPixelFormaton { self.format } | |
dd1b60e1 KS |
50 | pub fn set_width(&mut self, w: usize) { self.width = w; } |
51 | pub fn set_height(&mut self, h: usize) { self.height = h; } | |
5869fd63 KS |
52 | } |
53 | ||
83e603fa KS |
54 | impl fmt::Display for NAVideoInfo { |
55 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | |
56 | write!(f, "{}x{}", self.width, self.height) | |
57 | } | |
58 | } | |
59 | ||
66116504 | 60 | #[derive(Clone,Copy,PartialEq)] |
5869fd63 KS |
61 | pub enum NACodecTypeInfo { |
62 | None, | |
63 | Audio(NAAudioInfo), | |
64 | Video(NAVideoInfo), | |
65 | } | |
66 | ||
22cb00db KS |
67 | impl NACodecTypeInfo { |
68 | pub fn get_video_info(&self) -> Option<NAVideoInfo> { | |
69 | match *self { | |
70 | NACodecTypeInfo::Video(vinfo) => Some(vinfo), | |
71 | _ => None, | |
72 | } | |
73 | } | |
74 | pub fn get_audio_info(&self) -> Option<NAAudioInfo> { | |
75 | match *self { | |
76 | NACodecTypeInfo::Audio(ainfo) => Some(ainfo), | |
77 | _ => None, | |
78 | } | |
79 | } | |
5076115b KS |
80 | pub fn is_video(&self) -> bool { |
81 | match *self { | |
82 | NACodecTypeInfo::Video(_) => true, | |
83 | _ => false, | |
84 | } | |
85 | } | |
86 | pub fn is_audio(&self) -> bool { | |
87 | match *self { | |
88 | NACodecTypeInfo::Audio(_) => true, | |
89 | _ => false, | |
90 | } | |
91 | } | |
22cb00db KS |
92 | } |
93 | ||
83e603fa KS |
94 | impl fmt::Display for NACodecTypeInfo { |
95 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | |
96 | let ret = match *self { | |
e243ceb4 | 97 | NACodecTypeInfo::None => "".to_string(), |
83e603fa KS |
98 | NACodecTypeInfo::Audio(fmt) => format!("{}", fmt), |
99 | NACodecTypeInfo::Video(fmt) => format!("{}", fmt), | |
100 | }; | |
101 | write!(f, "{}", ret) | |
102 | } | |
103 | } | |
104 | ||
22cb00db KS |
105 | #[derive(Clone)] |
106 | pub struct NAVideoBuffer<T> { | |
6c8e5c40 | 107 | info: NAVideoInfo, |
1a967e6b | 108 | data: NABufferRef<Vec<T>>, |
6c8e5c40 KS |
109 | offs: Vec<usize>, |
110 | strides: Vec<usize>, | |
22cb00db KS |
111 | } |
112 | ||
113 | impl<T: Clone> NAVideoBuffer<T> { | |
114 | pub fn get_offset(&self, idx: usize) -> usize { | |
115 | if idx >= self.offs.len() { 0 } | |
116 | else { self.offs[idx] } | |
117 | } | |
118 | pub fn get_info(&self) -> NAVideoInfo { self.info } | |
1a967e6b KS |
119 | pub fn get_data(&self) -> &Vec<T> { self.data.as_ref() } |
120 | pub fn get_data_mut(&mut self) -> Option<&mut Vec<T>> { self.data.as_mut() } | |
22cb00db | 121 | pub fn copy_buffer(&mut self) -> Self { |
1a967e6b KS |
122 | let mut data: Vec<T> = Vec::with_capacity(self.data.len()); |
123 | data.clone_from(self.data.as_ref()); | |
22cb00db KS |
124 | let mut offs: Vec<usize> = Vec::with_capacity(self.offs.len()); |
125 | offs.clone_from(&self.offs); | |
6c8e5c40 KS |
126 | let mut strides: Vec<usize> = Vec::with_capacity(self.strides.len()); |
127 | strides.clone_from(&self.strides); | |
e243ceb4 | 128 | NAVideoBuffer { info: self.info, data: NABufferRef::new(data), offs, strides } |
22cb00db KS |
129 | } |
130 | pub fn get_stride(&self, idx: usize) -> usize { | |
6c8e5c40 KS |
131 | if idx >= self.strides.len() { return 0; } |
132 | self.strides[idx] | |
22cb00db KS |
133 | } |
134 | pub fn get_dimensions(&self, idx: usize) -> (usize, usize) { | |
135 | get_plane_size(&self.info, idx) | |
136 | } | |
3fc28ece KS |
137 | pub fn into_ref(self) -> NABufferRef<Self> { |
138 | NABufferRef::new(self) | |
139 | } | |
22cb00db KS |
140 | } |
141 | ||
3fc28ece KS |
142 | pub type NAVideoBufferRef<T> = NABufferRef<NAVideoBuffer<T>>; |
143 | ||
22cb00db KS |
144 | #[derive(Clone)] |
145 | pub struct NAAudioBuffer<T> { | |
146 | info: NAAudioInfo, | |
1a967e6b | 147 | data: NABufferRef<Vec<T>>, |
22cb00db | 148 | offs: Vec<usize>, |
01e2d496 | 149 | stride: usize, |
22cb00db | 150 | chmap: NAChannelMap, |
5076115b | 151 | len: usize, |
22cb00db KS |
152 | } |
153 | ||
154 | impl<T: Clone> NAAudioBuffer<T> { | |
155 | pub fn get_offset(&self, idx: usize) -> usize { | |
156 | if idx >= self.offs.len() { 0 } | |
157 | else { self.offs[idx] } | |
158 | } | |
01e2d496 | 159 | pub fn get_stride(&self) -> usize { self.stride } |
22cb00db | 160 | pub fn get_info(&self) -> NAAudioInfo { self.info } |
8ee4352b | 161 | pub fn get_chmap(&self) -> &NAChannelMap { &self.chmap } |
1a967e6b KS |
162 | pub fn get_data(&self) -> &Vec<T> { self.data.as_ref() } |
163 | pub fn get_data_mut(&mut self) -> Option<&mut Vec<T>> { self.data.as_mut() } | |
22cb00db | 164 | pub fn copy_buffer(&mut self) -> Self { |
1a967e6b KS |
165 | let mut data: Vec<T> = Vec::with_capacity(self.data.len()); |
166 | data.clone_from(self.data.as_ref()); | |
22cb00db KS |
167 | let mut offs: Vec<usize> = Vec::with_capacity(self.offs.len()); |
168 | offs.clone_from(&self.offs); | |
8ee4352b | 169 | NAAudioBuffer { info: self.info, data: NABufferRef::new(data), offs, chmap: self.get_chmap().clone(), len: self.len, stride: self.stride } |
22cb00db | 170 | } |
5076115b | 171 | pub fn get_length(&self) -> usize { self.len } |
22cb00db KS |
172 | } |
173 | ||
87a1ebc3 | 174 | impl NAAudioBuffer<u8> { |
1a967e6b KS |
175 | pub fn new_from_buf(info: NAAudioInfo, data: NABufferRef<Vec<u8>>, chmap: NAChannelMap) -> Self { |
176 | let len = data.len(); | |
01e2d496 | 177 | NAAudioBuffer { info, data, chmap, offs: Vec::new(), len, stride: 0 } |
87a1ebc3 KS |
178 | } |
179 | } | |
180 | ||
22cb00db KS |
181 | #[derive(Clone)] |
182 | pub enum NABufferType { | |
3fc28ece KS |
183 | Video (NAVideoBufferRef<u8>), |
184 | Video16 (NAVideoBufferRef<u16>), | |
185 | Video32 (NAVideoBufferRef<u32>), | |
186 | VideoPacked(NAVideoBufferRef<u8>), | |
22cb00db KS |
187 | AudioU8 (NAAudioBuffer<u8>), |
188 | AudioI16 (NAAudioBuffer<i16>), | |
87a1ebc3 | 189 | AudioI32 (NAAudioBuffer<i32>), |
22cb00db KS |
190 | AudioF32 (NAAudioBuffer<f32>), |
191 | AudioPacked(NAAudioBuffer<u8>), | |
1a967e6b | 192 | Data (NABufferRef<Vec<u8>>), |
22cb00db KS |
193 | None, |
194 | } | |
195 | ||
196 | impl NABufferType { | |
197 | pub fn get_offset(&self, idx: usize) -> usize { | |
198 | match *self { | |
199 | NABufferType::Video(ref vb) => vb.get_offset(idx), | |
200 | NABufferType::Video16(ref vb) => vb.get_offset(idx), | |
3bba1c4a | 201 | NABufferType::Video32(ref vb) => vb.get_offset(idx), |
22cb00db KS |
202 | NABufferType::VideoPacked(ref vb) => vb.get_offset(idx), |
203 | NABufferType::AudioU8(ref ab) => ab.get_offset(idx), | |
204 | NABufferType::AudioI16(ref ab) => ab.get_offset(idx), | |
205 | NABufferType::AudioF32(ref ab) => ab.get_offset(idx), | |
206 | NABufferType::AudioPacked(ref ab) => ab.get_offset(idx), | |
207 | _ => 0, | |
208 | } | |
209 | } | |
3bba1c4a KS |
210 | pub fn get_video_info(&self) -> Option<NAVideoInfo> { |
211 | match *self { | |
212 | NABufferType::Video(ref vb) => Some(vb.get_info()), | |
213 | NABufferType::Video16(ref vb) => Some(vb.get_info()), | |
214 | NABufferType::Video32(ref vb) => Some(vb.get_info()), | |
215 | NABufferType::VideoPacked(ref vb) => Some(vb.get_info()), | |
216 | _ => None, | |
217 | } | |
218 | } | |
3fc28ece | 219 | pub fn get_vbuf(&self) -> Option<NAVideoBufferRef<u8>> { |
22cb00db KS |
220 | match *self { |
221 | NABufferType::Video(ref vb) => Some(vb.clone()), | |
87a1ebc3 KS |
222 | NABufferType::VideoPacked(ref vb) => Some(vb.clone()), |
223 | _ => None, | |
224 | } | |
225 | } | |
3fc28ece | 226 | pub fn get_vbuf16(&self) -> Option<NAVideoBufferRef<u16>> { |
87a1ebc3 KS |
227 | match *self { |
228 | NABufferType::Video16(ref vb) => Some(vb.clone()), | |
229 | _ => None, | |
230 | } | |
231 | } | |
3fc28ece | 232 | pub fn get_vbuf32(&self) -> Option<NAVideoBufferRef<u32>> { |
3bba1c4a KS |
233 | match *self { |
234 | NABufferType::Video32(ref vb) => Some(vb.clone()), | |
235 | _ => None, | |
236 | } | |
237 | } | |
6e09a92e | 238 | pub fn get_abuf_u8(&self) -> Option<NAAudioBuffer<u8>> { |
87a1ebc3 KS |
239 | match *self { |
240 | NABufferType::AudioU8(ref ab) => Some(ab.clone()), | |
241 | NABufferType::AudioPacked(ref ab) => Some(ab.clone()), | |
242 | _ => None, | |
243 | } | |
244 | } | |
6e09a92e | 245 | pub fn get_abuf_i16(&self) -> Option<NAAudioBuffer<i16>> { |
87a1ebc3 KS |
246 | match *self { |
247 | NABufferType::AudioI16(ref ab) => Some(ab.clone()), | |
248 | _ => None, | |
249 | } | |
250 | } | |
6e09a92e | 251 | pub fn get_abuf_i32(&self) -> Option<NAAudioBuffer<i32>> { |
87a1ebc3 KS |
252 | match *self { |
253 | NABufferType::AudioI32(ref ab) => Some(ab.clone()), | |
254 | _ => None, | |
255 | } | |
256 | } | |
6e09a92e | 257 | pub fn get_abuf_f32(&self) -> Option<NAAudioBuffer<f32>> { |
87a1ebc3 KS |
258 | match *self { |
259 | NABufferType::AudioF32(ref ab) => Some(ab.clone()), | |
22cb00db KS |
260 | _ => None, |
261 | } | |
262 | } | |
263 | } | |
264 | ||
cd830591 KS |
265 | const NA_SIMPLE_VFRAME_COMPONENTS: usize = 4; |
266 | pub struct NASimpleVideoFrame<'a, T: Copy> { | |
267 | pub width: [usize; NA_SIMPLE_VFRAME_COMPONENTS], | |
268 | pub height: [usize; NA_SIMPLE_VFRAME_COMPONENTS], | |
269 | pub flip: bool, | |
270 | pub stride: [usize; NA_SIMPLE_VFRAME_COMPONENTS], | |
271 | pub offset: [usize; NA_SIMPLE_VFRAME_COMPONENTS], | |
272 | pub components: usize, | |
dc45d8ce | 273 | pub data: &'a mut [T], |
cd830591 KS |
274 | } |
275 | ||
276 | impl<'a, T:Copy> NASimpleVideoFrame<'a, T> { | |
277 | pub fn from_video_buf(vbuf: &'a mut NAVideoBuffer<T>) -> Option<Self> { | |
278 | let vinfo = vbuf.get_info(); | |
279 | let components = vinfo.format.components as usize; | |
280 | if components > NA_SIMPLE_VFRAME_COMPONENTS { | |
281 | return None; | |
282 | } | |
283 | let mut w: [usize; NA_SIMPLE_VFRAME_COMPONENTS] = [0; NA_SIMPLE_VFRAME_COMPONENTS]; | |
284 | let mut h: [usize; NA_SIMPLE_VFRAME_COMPONENTS] = [0; NA_SIMPLE_VFRAME_COMPONENTS]; | |
285 | let mut s: [usize; NA_SIMPLE_VFRAME_COMPONENTS] = [0; NA_SIMPLE_VFRAME_COMPONENTS]; | |
286 | let mut o: [usize; NA_SIMPLE_VFRAME_COMPONENTS] = [0; NA_SIMPLE_VFRAME_COMPONENTS]; | |
287 | for comp in 0..components { | |
288 | let (width, height) = vbuf.get_dimensions(comp); | |
289 | w[comp] = width; | |
290 | h[comp] = height; | |
291 | s[comp] = vbuf.get_stride(comp); | |
292 | o[comp] = vbuf.get_offset(comp); | |
293 | } | |
294 | let flip = vinfo.flipped; | |
295 | Some(NASimpleVideoFrame { | |
296 | width: w, | |
297 | height: h, | |
298 | flip, | |
299 | stride: s, | |
300 | offset: o, | |
301 | components, | |
dc45d8ce | 302 | data: vbuf.data.as_mut_slice(), |
cd830591 KS |
303 | }) |
304 | } | |
305 | } | |
306 | ||
22cb00db KS |
307 | #[derive(Debug,Clone,Copy,PartialEq)] |
308 | pub enum AllocatorError { | |
309 | TooLargeDimensions, | |
310 | FormatError, | |
311 | } | |
312 | ||
313 | pub fn alloc_video_buffer(vinfo: NAVideoInfo, align: u8) -> Result<NABufferType, AllocatorError> { | |
314 | let fmt = &vinfo.format; | |
315 | let mut new_size: usize = 0; | |
6c8e5c40 KS |
316 | let mut offs: Vec<usize> = Vec::new(); |
317 | let mut strides: Vec<usize> = Vec::new(); | |
22cb00db KS |
318 | |
319 | for i in 0..fmt.get_num_comp() { | |
320 | if fmt.get_chromaton(i) == None { return Err(AllocatorError::FormatError); } | |
321 | } | |
322 | ||
323 | let align_mod = ((1 << align) as usize) - 1; | |
324 | let width = ((vinfo.width as usize) + align_mod) & !align_mod; | |
325 | let height = ((vinfo.height as usize) + align_mod) & !align_mod; | |
326 | let mut max_depth = 0; | |
327 | let mut all_packed = true; | |
3bba1c4a | 328 | let mut all_bytealigned = true; |
22cb00db | 329 | for i in 0..fmt.get_num_comp() { |
6c8e5c40 | 330 | let ochr = fmt.get_chromaton(i); |
e243ceb4 | 331 | if ochr.is_none() { continue; } |
6c8e5c40 | 332 | let chr = ochr.unwrap(); |
22cb00db KS |
333 | if !chr.is_packed() { |
334 | all_packed = false; | |
3bba1c4a KS |
335 | } else if ((chr.get_shift() + chr.get_depth()) & 7) != 0 { |
336 | all_bytealigned = false; | |
22cb00db KS |
337 | } |
338 | max_depth = max(max_depth, chr.get_depth()); | |
339 | } | |
3bba1c4a KS |
340 | let unfit_elem_size = match fmt.get_elem_size() { |
341 | 2 | 4 => false, | |
342 | _ => true, | |
343 | }; | |
22cb00db KS |
344 | |
345 | //todo semi-packed like NV12 | |
bc6aac3d KS |
346 | if fmt.is_paletted() { |
347 | //todo various-sized palettes? | |
6c8e5c40 KS |
348 | let stride = vinfo.get_format().get_chromaton(0).unwrap().get_linesize(width); |
349 | let pic_sz = stride.checked_mul(height); | |
bc6aac3d KS |
350 | if pic_sz == None { return Err(AllocatorError::TooLargeDimensions); } |
351 | let pal_size = 256 * (fmt.get_elem_size() as usize); | |
352 | let new_size = pic_sz.unwrap().checked_add(pal_size); | |
353 | if new_size == None { return Err(AllocatorError::TooLargeDimensions); } | |
354 | offs.push(0); | |
6c8e5c40 KS |
355 | offs.push(stride * height); |
356 | strides.push(stride); | |
e243ceb4 KS |
357 | let data: Vec<u8> = vec![0; new_size.unwrap()]; |
358 | let buf: NAVideoBuffer<u8> = NAVideoBuffer { data: NABufferRef::new(data), info: vinfo, offs, strides }; | |
3fc28ece | 359 | Ok(NABufferType::Video(buf.into_ref())) |
bc6aac3d | 360 | } else if !all_packed { |
22cb00db | 361 | for i in 0..fmt.get_num_comp() { |
6c8e5c40 | 362 | let ochr = fmt.get_chromaton(i); |
e243ceb4 | 363 | if ochr.is_none() { continue; } |
6c8e5c40 | 364 | let chr = ochr.unwrap(); |
74afc7de | 365 | offs.push(new_size as usize); |
6c8e5c40 | 366 | let stride = chr.get_linesize(width); |
22cb00db | 367 | let cur_h = chr.get_height(height); |
6c8e5c40 | 368 | let cur_sz = stride.checked_mul(cur_h); |
22cb00db KS |
369 | if cur_sz == None { return Err(AllocatorError::TooLargeDimensions); } |
370 | let new_sz = new_size.checked_add(cur_sz.unwrap()); | |
371 | if new_sz == None { return Err(AllocatorError::TooLargeDimensions); } | |
372 | new_size = new_sz.unwrap(); | |
6c8e5c40 | 373 | strides.push(stride); |
22cb00db KS |
374 | } |
375 | if max_depth <= 8 { | |
e243ceb4 KS |
376 | let data: Vec<u8> = vec![0; new_size]; |
377 | let buf: NAVideoBuffer<u8> = NAVideoBuffer { data: NABufferRef::new(data), info: vinfo, offs, strides }; | |
3fc28ece | 378 | Ok(NABufferType::Video(buf.into_ref())) |
3bba1c4a | 379 | } else if max_depth <= 16 { |
e243ceb4 KS |
380 | let data: Vec<u16> = vec![0; new_size]; |
381 | let buf: NAVideoBuffer<u16> = NAVideoBuffer { data: NABufferRef::new(data), info: vinfo, offs, strides }; | |
3fc28ece | 382 | Ok(NABufferType::Video16(buf.into_ref())) |
3bba1c4a | 383 | } else { |
e243ceb4 KS |
384 | let data: Vec<u32> = vec![0; new_size]; |
385 | let buf: NAVideoBuffer<u32> = NAVideoBuffer { data: NABufferRef::new(data), info: vinfo, offs, strides }; | |
3fc28ece | 386 | Ok(NABufferType::Video32(buf.into_ref())) |
22cb00db | 387 | } |
3bba1c4a | 388 | } else if all_bytealigned || unfit_elem_size { |
22cb00db KS |
389 | let elem_sz = fmt.get_elem_size(); |
390 | let line_sz = width.checked_mul(elem_sz as usize); | |
391 | if line_sz == None { return Err(AllocatorError::TooLargeDimensions); } | |
392 | let new_sz = line_sz.unwrap().checked_mul(height); | |
393 | if new_sz == None { return Err(AllocatorError::TooLargeDimensions); } | |
394 | new_size = new_sz.unwrap(); | |
e243ceb4 | 395 | let data: Vec<u8> = vec![0; new_size]; |
6c8e5c40 | 396 | strides.push(line_sz.unwrap()); |
e243ceb4 | 397 | let buf: NAVideoBuffer<u8> = NAVideoBuffer { data: NABufferRef::new(data), info: vinfo, offs, strides }; |
3fc28ece | 398 | Ok(NABufferType::VideoPacked(buf.into_ref())) |
3bba1c4a KS |
399 | } else { |
400 | let elem_sz = fmt.get_elem_size(); | |
401 | let new_sz = width.checked_mul(height); | |
402 | if new_sz == None { return Err(AllocatorError::TooLargeDimensions); } | |
403 | new_size = new_sz.unwrap(); | |
404 | match elem_sz { | |
405 | 2 => { | |
e243ceb4 | 406 | let data: Vec<u16> = vec![0; new_size]; |
3bba1c4a | 407 | strides.push(width); |
e243ceb4 | 408 | let buf: NAVideoBuffer<u16> = NAVideoBuffer { data: NABufferRef::new(data), info: vinfo, offs, strides }; |
3fc28ece | 409 | Ok(NABufferType::Video16(buf.into_ref())) |
3bba1c4a KS |
410 | }, |
411 | 4 => { | |
e243ceb4 | 412 | let data: Vec<u32> = vec![0; new_size]; |
3bba1c4a | 413 | strides.push(width); |
e243ceb4 | 414 | let buf: NAVideoBuffer<u32> = NAVideoBuffer { data: NABufferRef::new(data), info: vinfo, offs, strides }; |
3fc28ece | 415 | Ok(NABufferType::Video32(buf.into_ref())) |
3bba1c4a KS |
416 | }, |
417 | _ => unreachable!(), | |
418 | } | |
22cb00db KS |
419 | } |
420 | } | |
421 | ||
e243ceb4 | 422 | #[allow(clippy::collapsible_if)] |
22cb00db KS |
423 | pub fn alloc_audio_buffer(ainfo: NAAudioInfo, nsamples: usize, chmap: NAChannelMap) -> Result<NABufferType, AllocatorError> { |
424 | let mut offs: Vec<usize> = Vec::new(); | |
4c6c19cb | 425 | if ainfo.format.is_planar() || (ainfo.channels == 1 && (ainfo.format.get_bits() % 8) == 0) { |
22cb00db KS |
426 | let len = nsamples.checked_mul(ainfo.channels as usize); |
427 | if len == None { return Err(AllocatorError::TooLargeDimensions); } | |
428 | let length = len.unwrap(); | |
01e2d496 | 429 | let stride = nsamples; |
22cb00db | 430 | for i in 0..ainfo.channels { |
01e2d496 | 431 | offs.push((i as usize) * stride); |
22cb00db KS |
432 | } |
433 | if ainfo.format.is_float() { | |
434 | if ainfo.format.get_bits() == 32 { | |
e243ceb4 | 435 | let data: Vec<f32> = vec![0.0; length]; |
01e2d496 | 436 | let buf: NAAudioBuffer<f32> = NAAudioBuffer { data: NABufferRef::new(data), info: ainfo, offs, chmap, len: nsamples, stride }; |
22cb00db KS |
437 | Ok(NABufferType::AudioF32(buf)) |
438 | } else { | |
439 | Err(AllocatorError::TooLargeDimensions) | |
440 | } | |
441 | } else { | |
442 | if ainfo.format.get_bits() == 8 && !ainfo.format.is_signed() { | |
e243ceb4 | 443 | let data: Vec<u8> = vec![0; length]; |
01e2d496 | 444 | let buf: NAAudioBuffer<u8> = NAAudioBuffer { data: NABufferRef::new(data), info: ainfo, offs, chmap, len: nsamples, stride }; |
22cb00db KS |
445 | Ok(NABufferType::AudioU8(buf)) |
446 | } else if ainfo.format.get_bits() == 16 && ainfo.format.is_signed() { | |
e243ceb4 | 447 | let data: Vec<i16> = vec![0; length]; |
01e2d496 | 448 | let buf: NAAudioBuffer<i16> = NAAudioBuffer { data: NABufferRef::new(data), info: ainfo, offs, chmap, len: nsamples, stride }; |
22cb00db KS |
449 | Ok(NABufferType::AudioI16(buf)) |
450 | } else { | |
451 | Err(AllocatorError::TooLargeDimensions) | |
452 | } | |
453 | } | |
454 | } else { | |
455 | let len = nsamples.checked_mul(ainfo.channels as usize); | |
456 | if len == None { return Err(AllocatorError::TooLargeDimensions); } | |
457 | let length = ainfo.format.get_audio_size(len.unwrap() as u64); | |
e243ceb4 | 458 | let data: Vec<u8> = vec![0; length]; |
01e2d496 | 459 | let buf: NAAudioBuffer<u8> = NAAudioBuffer { data: NABufferRef::new(data), info: ainfo, offs, chmap, len: nsamples, stride: 0 }; |
1a151e53 | 460 | Ok(NABufferType::AudioPacked(buf)) |
22cb00db KS |
461 | } |
462 | } | |
463 | ||
464 | pub fn alloc_data_buffer(size: usize) -> Result<NABufferType, AllocatorError> { | |
e243ceb4 | 465 | let data: Vec<u8> = vec![0; size]; |
1a967e6b | 466 | let buf: NABufferRef<Vec<u8>> = NABufferRef::new(data); |
22cb00db KS |
467 | Ok(NABufferType::Data(buf)) |
468 | } | |
469 | ||
470 | pub fn copy_buffer(buf: NABufferType) -> NABufferType { | |
471 | buf.clone() | |
472 | } | |
473 | ||
01613464 KS |
474 | pub struct NAVideoBufferPool<T:Copy> { |
475 | pool: Vec<NAVideoBufferRef<T>>, | |
1a967e6b | 476 | max_len: usize, |
01613464 | 477 | add_len: usize, |
1a967e6b KS |
478 | } |
479 | ||
01613464 | 480 | impl<T:Copy> NAVideoBufferPool<T> { |
1a967e6b KS |
481 | pub fn new(max_len: usize) -> Self { |
482 | Self { | |
483 | pool: Vec::with_capacity(max_len), | |
484 | max_len, | |
01613464 | 485 | add_len: 0, |
1a967e6b KS |
486 | } |
487 | } | |
01613464 KS |
488 | pub fn set_dec_bufs(&mut self, add_len: usize) { |
489 | self.add_len = add_len; | |
490 | } | |
491 | pub fn get_free(&mut self) -> Option<NAVideoBufferRef<T>> { | |
492 | for e in self.pool.iter() { | |
493 | if e.get_num_refs() == 1 { | |
494 | return Some(e.clone()); | |
495 | } | |
496 | } | |
497 | None | |
498 | } | |
499 | pub fn get_copy(&mut self, rbuf: &NAVideoBufferRef<T>) -> Option<NAVideoBufferRef<T>> { | |
e243ceb4 | 500 | let mut dbuf = self.get_free()?; |
01613464 KS |
501 | dbuf.data.copy_from_slice(&rbuf.data); |
502 | Some(dbuf) | |
503 | } | |
504 | pub fn reset(&mut self) { | |
505 | self.pool.truncate(0); | |
506 | } | |
507 | } | |
508 | ||
509 | impl NAVideoBufferPool<u8> { | |
1a967e6b | 510 | pub fn prealloc_video(&mut self, vinfo: NAVideoInfo, align: u8) -> Result<(), AllocatorError> { |
01613464 | 511 | let nbufs = self.max_len + self.add_len - self.pool.len(); |
1a967e6b | 512 | for _ in 0..nbufs { |
e243ceb4 | 513 | let vbuf = alloc_video_buffer(vinfo, align)?; |
01613464 KS |
514 | if let NABufferType::Video(buf) = vbuf { |
515 | self.pool.push(buf); | |
516 | } else if let NABufferType::VideoPacked(buf) = vbuf { | |
517 | self.pool.push(buf); | |
518 | } else { | |
519 | return Err(AllocatorError::FormatError); | |
520 | } | |
1a967e6b KS |
521 | } |
522 | Ok(()) | |
523 | } | |
01613464 KS |
524 | } |
525 | ||
526 | impl NAVideoBufferPool<u16> { | |
527 | pub fn prealloc_video(&mut self, vinfo: NAVideoInfo, align: u8) -> Result<(), AllocatorError> { | |
528 | let nbufs = self.max_len + self.add_len - self.pool.len(); | |
1a967e6b | 529 | for _ in 0..nbufs { |
e243ceb4 | 530 | let vbuf = alloc_video_buffer(vinfo, align)?; |
01613464 KS |
531 | if let NABufferType::Video16(buf) = vbuf { |
532 | self.pool.push(buf); | |
533 | } else { | |
534 | return Err(AllocatorError::FormatError); | |
535 | } | |
1a967e6b KS |
536 | } |
537 | Ok(()) | |
538 | } | |
01613464 KS |
539 | } |
540 | ||
541 | impl NAVideoBufferPool<u32> { | |
542 | pub fn prealloc_video(&mut self, vinfo: NAVideoInfo, align: u8) -> Result<(), AllocatorError> { | |
543 | let nbufs = self.max_len + self.add_len - self.pool.len(); | |
544 | for _ in 0..nbufs { | |
e243ceb4 | 545 | let vbuf = alloc_video_buffer(vinfo, align)?; |
01613464 KS |
546 | if let NABufferType::Video32(buf) = vbuf { |
547 | self.pool.push(buf); | |
548 | } else { | |
549 | return Err(AllocatorError::FormatError); | |
1a967e6b KS |
550 | } |
551 | } | |
01613464 | 552 | Ok(()) |
1a967e6b KS |
553 | } |
554 | } | |
555 | ||
5869fd63 | 556 | #[allow(dead_code)] |
8869d452 KS |
557 | #[derive(Clone)] |
558 | pub struct NACodecInfo { | |
ccae5343 | 559 | name: &'static str, |
5869fd63 | 560 | properties: NACodecTypeInfo, |
2422d969 | 561 | extradata: Option<Arc<Vec<u8>>>, |
5869fd63 KS |
562 | } |
563 | ||
2422d969 KS |
564 | pub type NACodecInfoRef = Arc<NACodecInfo>; |
565 | ||
8869d452 | 566 | impl NACodecInfo { |
ccae5343 | 567 | pub fn new(name: &'static str, p: NACodecTypeInfo, edata: Option<Vec<u8>>) -> Self { |
8869d452 KS |
568 | let extradata = match edata { |
569 | None => None, | |
2422d969 | 570 | Some(vec) => Some(Arc::new(vec)), |
8869d452 | 571 | }; |
e243ceb4 | 572 | NACodecInfo { name, properties: p, extradata } |
8869d452 | 573 | } |
2422d969 | 574 | pub fn new_ref(name: &'static str, p: NACodecTypeInfo, edata: Option<Arc<Vec<u8>>>) -> Self { |
e243ceb4 | 575 | NACodecInfo { name, properties: p, extradata: edata } |
66116504 | 576 | } |
2422d969 | 577 | pub fn into_ref(self) -> NACodecInfoRef { Arc::new(self) } |
8869d452 | 578 | pub fn get_properties(&self) -> NACodecTypeInfo { self.properties } |
2422d969 | 579 | pub fn get_extradata(&self) -> Option<Arc<Vec<u8>>> { |
8869d452 KS |
580 | if let Some(ref vec) = self.extradata { return Some(vec.clone()); } |
581 | None | |
5869fd63 | 582 | } |
66116504 KS |
583 | pub fn get_name(&self) -> &'static str { self.name } |
584 | pub fn is_video(&self) -> bool { | |
585 | if let NACodecTypeInfo::Video(_) = self.properties { return true; } | |
586 | false | |
587 | } | |
588 | pub fn is_audio(&self) -> bool { | |
589 | if let NACodecTypeInfo::Audio(_) = self.properties { return true; } | |
590 | false | |
591 | } | |
2422d969 KS |
592 | pub fn new_dummy() -> Arc<Self> { |
593 | Arc::new(DUMMY_CODEC_INFO) | |
5076115b | 594 | } |
2422d969 KS |
595 | pub fn replace_info(&self, p: NACodecTypeInfo) -> Arc<Self> { |
596 | Arc::new(NACodecInfo { name: self.name, properties: p, extradata: self.extradata.clone() }) | |
5076115b | 597 | } |
66116504 KS |
598 | } |
599 | ||
241e56f1 KS |
600 | impl Default for NACodecInfo { |
601 | fn default() -> Self { DUMMY_CODEC_INFO } | |
602 | } | |
603 | ||
66116504 KS |
604 | impl fmt::Display for NACodecInfo { |
605 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | |
606 | let edata = match self.extradata.clone() { | |
e243ceb4 | 607 | None => "no extradata".to_string(), |
66116504 KS |
608 | Some(v) => format!("{} byte(s) of extradata", v.len()), |
609 | }; | |
610 | write!(f, "{}: {} {}", self.name, self.properties, edata) | |
611 | } | |
612 | } | |
613 | ||
614 | pub const DUMMY_CODEC_INFO: NACodecInfo = NACodecInfo { | |
615 | name: "none", | |
616 | properties: NACodecTypeInfo::None, | |
617 | extradata: None }; | |
618 | ||
66116504 KS |
619 | #[derive(Debug,Clone)] |
620 | pub enum NAValue { | |
5869fd63 KS |
621 | None, |
622 | Int(i32), | |
623 | Long(i64), | |
624 | String(String), | |
2422d969 | 625 | Data(Arc<Vec<u8>>), |
5869fd63 KS |
626 | } |
627 | ||
88c03b61 KS |
628 | #[derive(Debug,Clone,Copy,PartialEq)] |
629 | #[allow(dead_code)] | |
630 | pub enum FrameType { | |
631 | I, | |
632 | P, | |
633 | B, | |
bc6aac3d | 634 | Skip, |
88c03b61 KS |
635 | Other, |
636 | } | |
637 | ||
638 | impl fmt::Display for FrameType { | |
639 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | |
640 | match *self { | |
641 | FrameType::I => write!(f, "I"), | |
642 | FrameType::P => write!(f, "P"), | |
643 | FrameType::B => write!(f, "B"), | |
bc6aac3d | 644 | FrameType::Skip => write!(f, "skip"), |
88c03b61 KS |
645 | FrameType::Other => write!(f, "x"), |
646 | } | |
647 | } | |
648 | } | |
649 | ||
e189501e KS |
650 | #[derive(Debug,Clone,Copy)] |
651 | pub struct NATimeInfo { | |
5869fd63 KS |
652 | pts: Option<u64>, |
653 | dts: Option<u64>, | |
654 | duration: Option<u64>, | |
e189501e KS |
655 | tb_num: u32, |
656 | tb_den: u32, | |
657 | } | |
658 | ||
659 | impl NATimeInfo { | |
660 | pub fn new(pts: Option<u64>, dts: Option<u64>, duration: Option<u64>, tb_num: u32, tb_den: u32) -> Self { | |
e243ceb4 | 661 | NATimeInfo { pts, dts, duration, tb_num, tb_den } |
e189501e KS |
662 | } |
663 | pub fn get_pts(&self) -> Option<u64> { self.pts } | |
664 | pub fn get_dts(&self) -> Option<u64> { self.dts } | |
665 | pub fn get_duration(&self) -> Option<u64> { self.duration } | |
666 | pub fn set_pts(&mut self, pts: Option<u64>) { self.pts = pts; } | |
667 | pub fn set_dts(&mut self, dts: Option<u64>) { self.dts = dts; } | |
668 | pub fn set_duration(&mut self, dur: Option<u64>) { self.duration = dur; } | |
266da7b9 KS |
669 | |
670 | pub fn time_to_ts(time: u64, base: u64, tb_num: u32, tb_den: u32) -> u64 { | |
671 | let tb_num = tb_num as u64; | |
672 | let tb_den = tb_den as u64; | |
673 | let tmp = time.checked_mul(tb_num); | |
674 | if let Some(tmp) = tmp { | |
675 | tmp / base / tb_den | |
676 | } else { | |
677 | let tmp = time.checked_mul(tb_num); | |
678 | if let Some(tmp) = tmp { | |
679 | tmp / base / tb_den | |
680 | } else { | |
681 | let coarse = time / base; | |
682 | let tmp = coarse.checked_mul(tb_num); | |
683 | if let Some(tmp) = tmp { | |
684 | tmp / tb_den | |
685 | } else { | |
686 | (coarse / tb_den) * tb_num | |
687 | } | |
688 | } | |
689 | } | |
690 | } | |
a65bdeac KS |
691 | pub fn ts_to_time(ts: u64, base: u64, tb_num: u32, tb_den: u32) -> u64 { |
692 | let tb_num = tb_num as u64; | |
693 | let tb_den = tb_den as u64; | |
694 | let tmp = ts.checked_mul(base); | |
695 | if let Some(tmp) = tmp { | |
696 | let tmp2 = tmp.checked_mul(tb_num); | |
697 | if let Some(tmp2) = tmp2 { | |
698 | tmp2 / tb_den | |
699 | } else { | |
700 | (tmp / tb_den) * tb_num | |
701 | } | |
702 | } else { | |
703 | let tmp = ts.checked_mul(tb_num); | |
704 | if let Some(tmp) = tmp { | |
705 | (tmp / tb_den) * base | |
706 | } else { | |
707 | (ts / tb_den) * base * tb_num | |
708 | } | |
709 | } | |
710 | } | |
e189501e KS |
711 | } |
712 | ||
713 | #[allow(dead_code)] | |
714 | #[derive(Clone)] | |
715 | pub struct NAFrame { | |
716 | ts: NATimeInfo, | |
f18bba90 | 717 | id: i64, |
22cb00db | 718 | buffer: NABufferType, |
2422d969 | 719 | info: NACodecInfoRef, |
88c03b61 KS |
720 | ftype: FrameType, |
721 | key: bool, | |
66116504 KS |
722 | options: HashMap<String, NAValue>, |
723 | } | |
724 | ||
171860fc | 725 | pub type NAFrameRef = Arc<NAFrame>; |
ebd71c92 | 726 | |
66116504 KS |
727 | fn get_plane_size(info: &NAVideoInfo, idx: usize) -> (usize, usize) { |
728 | let chromaton = info.get_format().get_chromaton(idx); | |
e243ceb4 | 729 | if chromaton.is_none() { return (0, 0); } |
66116504 KS |
730 | let (hs, vs) = chromaton.unwrap().get_subsampling(); |
731 | let w = (info.get_width() + ((1 << hs) - 1)) >> hs; | |
732 | let h = (info.get_height() + ((1 << vs) - 1)) >> vs; | |
733 | (w, h) | |
734 | } | |
735 | ||
736 | impl NAFrame { | |
e189501e | 737 | pub fn new(ts: NATimeInfo, |
88c03b61 KS |
738 | ftype: FrameType, |
739 | keyframe: bool, | |
2422d969 | 740 | info: NACodecInfoRef, |
22cb00db KS |
741 | options: HashMap<String, NAValue>, |
742 | buffer: NABufferType) -> Self { | |
f18bba90 | 743 | NAFrame { ts, id: 0, buffer, info, ftype, key: keyframe, options } |
ebd71c92 | 744 | } |
2422d969 | 745 | pub fn get_info(&self) -> NACodecInfoRef { self.info.clone() } |
88c03b61 KS |
746 | pub fn get_frame_type(&self) -> FrameType { self.ftype } |
747 | pub fn is_keyframe(&self) -> bool { self.key } | |
88c03b61 KS |
748 | pub fn set_frame_type(&mut self, ftype: FrameType) { self.ftype = ftype; } |
749 | pub fn set_keyframe(&mut self, key: bool) { self.key = key; } | |
e189501e KS |
750 | pub fn get_time_information(&self) -> NATimeInfo { self.ts } |
751 | pub fn get_pts(&self) -> Option<u64> { self.ts.get_pts() } | |
752 | pub fn get_dts(&self) -> Option<u64> { self.ts.get_dts() } | |
f18bba90 | 753 | pub fn get_id(&self) -> i64 { self.id } |
e189501e KS |
754 | pub fn get_duration(&self) -> Option<u64> { self.ts.get_duration() } |
755 | pub fn set_pts(&mut self, pts: Option<u64>) { self.ts.set_pts(pts); } | |
756 | pub fn set_dts(&mut self, dts: Option<u64>) { self.ts.set_dts(dts); } | |
f18bba90 | 757 | pub fn set_id(&mut self, id: i64) { self.id = id; } |
e189501e | 758 | pub fn set_duration(&mut self, dur: Option<u64>) { self.ts.set_duration(dur); } |
66116504 | 759 | |
22cb00db | 760 | pub fn get_buffer(&self) -> NABufferType { self.buffer.clone() } |
171860fc KS |
761 | |
762 | pub fn into_ref(self) -> NAFrameRef { Arc::new(self) } | |
5869fd63 KS |
763 | } |
764 | ||
ebd71c92 KS |
765 | impl fmt::Display for NAFrame { |
766 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | |
e243ceb4 KS |
767 | let mut ostr = format!("frame type {}", self.ftype); |
768 | if let Some(pts) = self.ts.pts { ostr = format!("{} pts {}", ostr, pts); } | |
769 | if let Some(dts) = self.ts.dts { ostr = format!("{} dts {}", ostr, dts); } | |
770 | if let Some(dur) = self.ts.duration { ostr = format!("{} duration {}", ostr, dur); } | |
771 | if self.key { ostr = format!("{} kf", ostr); } | |
772 | write!(f, "[{}]", ostr) | |
ebd71c92 KS |
773 | } |
774 | } | |
88c03b61 | 775 | |
48c88fde | 776 | /// Possible stream types. |
baf5478c | 777 | #[derive(Debug,Clone,Copy,PartialEq)] |
5869fd63 | 778 | #[allow(dead_code)] |
48c88fde KS |
779 | pub enum StreamType { |
780 | /// video stream | |
781 | Video, | |
782 | /// audio stream | |
783 | Audio, | |
784 | /// subtitles | |
785 | Subtitles, | |
786 | /// any data stream (or might be an unrecognized audio/video stream) | |
787 | Data, | |
788 | /// nonexistent stream | |
789 | None, | |
790 | } | |
791 | ||
792 | impl fmt::Display for StreamType { | |
793 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | |
794 | match *self { | |
795 | StreamType::Video => write!(f, "Video"), | |
796 | StreamType::Audio => write!(f, "Audio"), | |
797 | StreamType::Subtitles => write!(f, "Subtitles"), | |
798 | StreamType::Data => write!(f, "Data"), | |
799 | StreamType::None => write!(f, "-"), | |
800 | } | |
801 | } | |
802 | } | |
803 | ||
804 | #[allow(dead_code)] | |
805 | #[derive(Clone)] | |
806 | pub struct NAStream { | |
807 | media_type: StreamType, | |
808 | id: u32, | |
809 | num: usize, | |
2422d969 | 810 | info: NACodecInfoRef, |
e189501e KS |
811 | tb_num: u32, |
812 | tb_den: u32, | |
813 | } | |
814 | ||
70910ac3 KS |
815 | pub type NAStreamRef = Arc<NAStream>; |
816 | ||
e189501e KS |
817 | pub fn reduce_timebase(tb_num: u32, tb_den: u32) -> (u32, u32) { |
818 | if tb_num == 0 { return (tb_num, tb_den); } | |
819 | if (tb_den % tb_num) == 0 { return (1, tb_den / tb_num); } | |
820 | ||
821 | let mut a = tb_num; | |
822 | let mut b = tb_den; | |
823 | ||
824 | while a != b { | |
825 | if a > b { a -= b; } | |
826 | else if b > a { b -= a; } | |
827 | } | |
828 | ||
829 | (tb_num / a, tb_den / a) | |
5869fd63 | 830 | } |
48c88fde KS |
831 | |
832 | impl NAStream { | |
e189501e KS |
833 | pub fn new(mt: StreamType, id: u32, info: NACodecInfo, tb_num: u32, tb_den: u32) -> Self { |
834 | let (n, d) = reduce_timebase(tb_num, tb_den); | |
e243ceb4 | 835 | NAStream { media_type: mt, id, num: 0, info: info.into_ref(), tb_num: n, tb_den: d } |
48c88fde KS |
836 | } |
837 | pub fn get_id(&self) -> u32 { self.id } | |
baf5478c | 838 | pub fn get_media_type(&self) -> StreamType { self.media_type } |
48c88fde KS |
839 | pub fn get_num(&self) -> usize { self.num } |
840 | pub fn set_num(&mut self, num: usize) { self.num = num; } | |
2422d969 | 841 | pub fn get_info(&self) -> NACodecInfoRef { self.info.clone() } |
e189501e KS |
842 | pub fn get_timebase(&self) -> (u32, u32) { (self.tb_num, self.tb_den) } |
843 | pub fn set_timebase(&mut self, tb_num: u32, tb_den: u32) { | |
844 | let (n, d) = reduce_timebase(tb_num, tb_den); | |
845 | self.tb_num = n; | |
846 | self.tb_den = d; | |
847 | } | |
70910ac3 | 848 | pub fn into_ref(self) -> NAStreamRef { Arc::new(self) } |
48c88fde KS |
849 | } |
850 | ||
851 | impl fmt::Display for NAStream { | |
852 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | |
e189501e | 853 | write!(f, "({}#{} @ {}/{} - {})", self.media_type, self.id, self.tb_num, self.tb_den, self.info.get_properties()) |
48c88fde KS |
854 | } |
855 | } | |
856 | ||
857 | #[allow(dead_code)] | |
858 | pub struct NAPacket { | |
70910ac3 | 859 | stream: NAStreamRef, |
e189501e | 860 | ts: NATimeInfo, |
1a967e6b | 861 | buffer: NABufferRef<Vec<u8>>, |
48c88fde KS |
862 | keyframe: bool, |
863 | // options: HashMap<String, NAValue<'a>>, | |
864 | } | |
865 | ||
866 | impl NAPacket { | |
70910ac3 | 867 | pub fn new(str: NAStreamRef, ts: NATimeInfo, kf: bool, vec: Vec<u8>) -> Self { |
48c88fde KS |
868 | // let mut vec: Vec<u8> = Vec::new(); |
869 | // vec.resize(size, 0); | |
e243ceb4 | 870 | NAPacket { stream: str, ts, keyframe: kf, buffer: NABufferRef::new(vec) } |
48c88fde | 871 | } |
70910ac3 | 872 | pub fn get_stream(&self) -> NAStreamRef { self.stream.clone() } |
e189501e KS |
873 | pub fn get_time_information(&self) -> NATimeInfo { self.ts } |
874 | pub fn get_pts(&self) -> Option<u64> { self.ts.get_pts() } | |
875 | pub fn get_dts(&self) -> Option<u64> { self.ts.get_dts() } | |
876 | pub fn get_duration(&self) -> Option<u64> { self.ts.get_duration() } | |
48c88fde | 877 | pub fn is_keyframe(&self) -> bool { self.keyframe } |
1a967e6b | 878 | pub fn get_buffer(&self) -> NABufferRef<Vec<u8>> { self.buffer.clone() } |
48c88fde KS |
879 | } |
880 | ||
881 | impl Drop for NAPacket { | |
882 | fn drop(&mut self) {} | |
883 | } | |
884 | ||
885 | impl fmt::Display for NAPacket { | |
886 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | |
e243ceb4 KS |
887 | let mut ostr = format!("[pkt for {} size {}", self.stream, self.buffer.len()); |
888 | if let Some(pts) = self.ts.pts { ostr = format!("{} pts {}", ostr, pts); } | |
889 | if let Some(dts) = self.ts.dts { ostr = format!("{} dts {}", ostr, dts); } | |
890 | if let Some(dur) = self.ts.duration { ostr = format!("{} duration {}", ostr, dur); } | |
891 | if self.keyframe { ostr = format!("{} kf", ostr); } | |
892 | ostr += "]"; | |
893 | write!(f, "{}", ostr) | |
48c88fde KS |
894 | } |
895 | } | |
896 | ||
897 | pub trait FrameFromPacket { | |
2422d969 | 898 | fn new_from_pkt(pkt: &NAPacket, info: NACodecInfoRef, buf: NABufferType) -> NAFrame; |
48c88fde KS |
899 | fn fill_timestamps(&mut self, pkt: &NAPacket); |
900 | } | |
901 | ||
902 | impl FrameFromPacket for NAFrame { | |
2422d969 | 903 | fn new_from_pkt(pkt: &NAPacket, info: NACodecInfoRef, buf: NABufferType) -> NAFrame { |
e189501e | 904 | NAFrame::new(pkt.ts, FrameType::Other, pkt.keyframe, info, HashMap::new(), buf) |
48c88fde KS |
905 | } |
906 | fn fill_timestamps(&mut self, pkt: &NAPacket) { | |
e189501e | 907 | self.ts = pkt.get_time_information(); |
48c88fde KS |
908 | } |
909 | } | |
910 |