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