]> git.nihav.org Git - nihav.git/blame - src/frame.rs
rmdec: parse audio information
[nihav.git] / src / frame.rs
CommitLineData
22cb00db 1use std::cmp::max;
5869fd63 2use std::collections::HashMap;
83e603fa 3use std::fmt;
8869d452 4use std::rc::Rc;
88c03b61 5use std::cell::*;
fba6f8e4 6use formats::*;
94dbb551 7
5869fd63 8#[allow(dead_code)]
66116504 9#[derive(Clone,Copy,PartialEq)]
5869fd63
KS
10pub struct NAAudioInfo {
11 sample_rate: u32,
12 channels: u8,
13 format: NASoniton,
14 block_len: usize,
15}
16
17impl 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
27impl 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 35pub struct NAVideoInfo {
66116504
KS
36 width: usize,
37 height: usize,
5869fd63
KS
38 flipped: bool,
39 format: NAPixelFormaton,
40}
41
42impl 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
54impl 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
61pub enum NACodecTypeInfo {
62 None,
63 Audio(NAAudioInfo),
64 Video(NAVideoInfo),
65}
66
22cb00db
KS
67impl 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
94impl fmt::Display for NACodecTypeInfo {
95 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
96 let ret = match *self {
97 NACodecTypeInfo::None => format!(""),
98 NACodecTypeInfo::Audio(fmt) => format!("{}", fmt),
99 NACodecTypeInfo::Video(fmt) => format!("{}", fmt),
100 };
101 write!(f, "{}", ret)
102 }
103}
104
22cb00db 105pub type NABufferRefT<T> = Rc<RefCell<Vec<T>>>;
88c03b61 106
22cb00db
KS
107#[derive(Clone)]
108pub struct NAVideoBuffer<T> {
6c8e5c40
KS
109 info: NAVideoInfo,
110 data: NABufferRefT<T>,
111 offs: Vec<usize>,
112 strides: Vec<usize>,
22cb00db
KS
113}
114
115impl<T: Clone> NAVideoBuffer<T> {
116 pub fn get_offset(&self, idx: usize) -> usize {
117 if idx >= self.offs.len() { 0 }
118 else { self.offs[idx] }
119 }
120 pub fn get_info(&self) -> NAVideoInfo { self.info }
121 pub fn get_data(&self) -> Ref<Vec<T>> { self.data.borrow() }
122 pub fn get_data_mut(&mut self) -> RefMut<Vec<T>> { self.data.borrow_mut() }
123 pub fn copy_buffer(&mut self) -> Self {
124 let mut data: Vec<T> = Vec::with_capacity(self.data.borrow().len());
125 data.clone_from(self.data.borrow().as_ref());
126 let mut offs: Vec<usize> = Vec::with_capacity(self.offs.len());
127 offs.clone_from(&self.offs);
6c8e5c40
KS
128 let mut strides: Vec<usize> = Vec::with_capacity(self.strides.len());
129 strides.clone_from(&self.strides);
130 NAVideoBuffer { info: self.info, data: Rc::new(RefCell::new(data)), offs: offs, strides: strides }
22cb00db
KS
131 }
132 pub fn get_stride(&self, idx: usize) -> usize {
6c8e5c40
KS
133 if idx >= self.strides.len() { return 0; }
134 self.strides[idx]
22cb00db
KS
135 }
136 pub fn get_dimensions(&self, idx: usize) -> (usize, usize) {
137 get_plane_size(&self.info, idx)
138 }
139}
140
141#[derive(Clone)]
142pub struct NAAudioBuffer<T> {
143 info: NAAudioInfo,
144 data: NABufferRefT<T>,
145 offs: Vec<usize>,
146 chmap: NAChannelMap,
5076115b 147 len: usize,
22cb00db
KS
148}
149
150impl<T: Clone> NAAudioBuffer<T> {
151 pub fn get_offset(&self, idx: usize) -> usize {
152 if idx >= self.offs.len() { 0 }
153 else { self.offs[idx] }
154 }
155 pub fn get_info(&self) -> NAAudioInfo { self.info }
156 pub fn get_chmap(&self) -> NAChannelMap { self.chmap.clone() }
157 pub fn get_data(&self) -> Ref<Vec<T>> { self.data.borrow() }
158 pub fn get_data_mut(&mut self) -> RefMut<Vec<T>> { self.data.borrow_mut() }
159 pub fn copy_buffer(&mut self) -> Self {
160 let mut data: Vec<T> = Vec::with_capacity(self.data.borrow().len());
161 data.clone_from(self.data.borrow().as_ref());
162 let mut offs: Vec<usize> = Vec::with_capacity(self.offs.len());
163 offs.clone_from(&self.offs);
5076115b 164 NAAudioBuffer { info: self.info, data: Rc::new(RefCell::new(data)), offs: offs, chmap: self.get_chmap(), len: self.len }
22cb00db 165 }
5076115b 166 pub fn get_length(&self) -> usize { self.len }
22cb00db
KS
167}
168
87a1ebc3
KS
169impl NAAudioBuffer<u8> {
170 pub fn new_from_buf(info: NAAudioInfo, data: NABufferRefT<u8>, chmap: NAChannelMap) -> Self {
5076115b
KS
171 let len = data.borrow().len();
172 NAAudioBuffer { info: info, data: data, chmap: chmap, offs: Vec::new(), len: len }
87a1ebc3
KS
173 }
174}
175
22cb00db
KS
176#[derive(Clone)]
177pub enum NABufferType {
178 Video (NAVideoBuffer<u8>),
179 Video16 (NAVideoBuffer<u16>),
180 VideoPacked(NAVideoBuffer<u8>),
181 AudioU8 (NAAudioBuffer<u8>),
182 AudioI16 (NAAudioBuffer<i16>),
87a1ebc3 183 AudioI32 (NAAudioBuffer<i32>),
22cb00db
KS
184 AudioF32 (NAAudioBuffer<f32>),
185 AudioPacked(NAAudioBuffer<u8>),
186 Data (NABufferRefT<u8>),
187 None,
188}
189
190impl 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::VideoPacked(ref vb) => vb.get_offset(idx),
196 NABufferType::AudioU8(ref ab) => ab.get_offset(idx),
197 NABufferType::AudioI16(ref ab) => ab.get_offset(idx),
198 NABufferType::AudioF32(ref ab) => ab.get_offset(idx),
199 NABufferType::AudioPacked(ref ab) => ab.get_offset(idx),
200 _ => 0,
201 }
202 }
203 pub fn get_vbuf(&mut self) -> Option<NAVideoBuffer<u8>> {
204 match *self {
205 NABufferType::Video(ref vb) => Some(vb.clone()),
87a1ebc3
KS
206 NABufferType::VideoPacked(ref vb) => Some(vb.clone()),
207 _ => None,
208 }
209 }
210 pub fn get_vbuf16(&mut self) -> Option<NAVideoBuffer<u16>> {
211 match *self {
212 NABufferType::Video16(ref vb) => Some(vb.clone()),
213 _ => None,
214 }
215 }
216 pub fn get_abuf_u8(&mut self) -> Option<NAAudioBuffer<u8>> {
217 match *self {
218 NABufferType::AudioU8(ref ab) => Some(ab.clone()),
219 NABufferType::AudioPacked(ref ab) => Some(ab.clone()),
220 _ => None,
221 }
222 }
223 pub fn get_abuf_i16(&mut self) -> Option<NAAudioBuffer<i16>> {
224 match *self {
225 NABufferType::AudioI16(ref ab) => Some(ab.clone()),
226 _ => None,
227 }
228 }
229 pub fn get_abuf_i32(&mut self) -> Option<NAAudioBuffer<i32>> {
230 match *self {
231 NABufferType::AudioI32(ref ab) => Some(ab.clone()),
232 _ => None,
233 }
234 }
235 pub fn get_abuf_f32(&mut self) -> Option<NAAudioBuffer<f32>> {
236 match *self {
237 NABufferType::AudioF32(ref ab) => Some(ab.clone()),
22cb00db
KS
238 _ => None,
239 }
240 }
241}
242
243#[derive(Debug,Clone,Copy,PartialEq)]
244pub enum AllocatorError {
245 TooLargeDimensions,
246 FormatError,
247}
248
249pub fn alloc_video_buffer(vinfo: NAVideoInfo, align: u8) -> Result<NABufferType, AllocatorError> {
250 let fmt = &vinfo.format;
251 let mut new_size: usize = 0;
6c8e5c40
KS
252 let mut offs: Vec<usize> = Vec::new();
253 let mut strides: Vec<usize> = Vec::new();
22cb00db
KS
254
255 for i in 0..fmt.get_num_comp() {
256 if fmt.get_chromaton(i) == None { return Err(AllocatorError::FormatError); }
257 }
258
259 let align_mod = ((1 << align) as usize) - 1;
260 let width = ((vinfo.width as usize) + align_mod) & !align_mod;
261 let height = ((vinfo.height as usize) + align_mod) & !align_mod;
262 let mut max_depth = 0;
263 let mut all_packed = true;
264 for i in 0..fmt.get_num_comp() {
6c8e5c40
KS
265 let ochr = fmt.get_chromaton(i);
266 if let None = ochr { continue; }
267 let chr = ochr.unwrap();
22cb00db
KS
268 if !chr.is_packed() {
269 all_packed = false;
270 break;
271 }
272 max_depth = max(max_depth, chr.get_depth());
273 }
274
275//todo semi-packed like NV12
bc6aac3d
KS
276 if fmt.is_paletted() {
277//todo various-sized palettes?
6c8e5c40
KS
278 let stride = vinfo.get_format().get_chromaton(0).unwrap().get_linesize(width);
279 let pic_sz = stride.checked_mul(height);
bc6aac3d
KS
280 if pic_sz == None { return Err(AllocatorError::TooLargeDimensions); }
281 let pal_size = 256 * (fmt.get_elem_size() as usize);
282 let new_size = pic_sz.unwrap().checked_add(pal_size);
283 if new_size == None { return Err(AllocatorError::TooLargeDimensions); }
284 offs.push(0);
6c8e5c40
KS
285 offs.push(stride * height);
286 strides.push(stride);
bc6aac3d
KS
287 let mut data: Vec<u8> = Vec::with_capacity(new_size.unwrap());
288 data.resize(new_size.unwrap(), 0);
6c8e5c40 289 let buf: NAVideoBuffer<u8> = NAVideoBuffer { data: Rc::new(RefCell::new(data)), info: vinfo, offs: offs, strides: strides };
bc6aac3d
KS
290 Ok(NABufferType::Video(buf))
291 } else if !all_packed {
22cb00db 292 for i in 0..fmt.get_num_comp() {
6c8e5c40
KS
293 let ochr = fmt.get_chromaton(i);
294 if let None = ochr { continue; }
295 let chr = ochr.unwrap();
22cb00db
KS
296 if !vinfo.is_flipped() {
297 offs.push(new_size as usize);
298 }
6c8e5c40 299 let stride = chr.get_linesize(width);
22cb00db 300 let cur_h = chr.get_height(height);
6c8e5c40 301 let cur_sz = stride.checked_mul(cur_h);
22cb00db
KS
302 if cur_sz == None { return Err(AllocatorError::TooLargeDimensions); }
303 let new_sz = new_size.checked_add(cur_sz.unwrap());
304 if new_sz == None { return Err(AllocatorError::TooLargeDimensions); }
305 new_size = new_sz.unwrap();
306 if vinfo.is_flipped() {
307 offs.push(new_size as usize);
308 }
6c8e5c40 309 strides.push(stride);
22cb00db
KS
310 }
311 if max_depth <= 8 {
312 let mut data: Vec<u8> = Vec::with_capacity(new_size);
313 data.resize(new_size, 0);
6c8e5c40 314 let buf: NAVideoBuffer<u8> = NAVideoBuffer { data: Rc::new(RefCell::new(data)), info: vinfo, offs: offs, strides: strides };
22cb00db
KS
315 Ok(NABufferType::Video(buf))
316 } else {
317 let mut data: Vec<u16> = Vec::with_capacity(new_size);
318 data.resize(new_size, 0);
6c8e5c40 319 let buf: NAVideoBuffer<u16> = NAVideoBuffer { data: Rc::new(RefCell::new(data)), info: vinfo, offs: offs, strides: strides };
22cb00db
KS
320 Ok(NABufferType::Video16(buf))
321 }
322 } else {
323 let elem_sz = fmt.get_elem_size();
324 let line_sz = width.checked_mul(elem_sz as usize);
325 if line_sz == None { return Err(AllocatorError::TooLargeDimensions); }
326 let new_sz = line_sz.unwrap().checked_mul(height);
327 if new_sz == None { return Err(AllocatorError::TooLargeDimensions); }
328 new_size = new_sz.unwrap();
329 let mut data: Vec<u8> = Vec::with_capacity(new_size);
330 data.resize(new_size, 0);
6c8e5c40
KS
331 strides.push(line_sz.unwrap());
332 let buf: NAVideoBuffer<u8> = NAVideoBuffer { data: Rc::new(RefCell::new(data)), info: vinfo, offs: offs, strides: strides };
22cb00db
KS
333 Ok(NABufferType::VideoPacked(buf))
334 }
335}
336
337pub fn alloc_audio_buffer(ainfo: NAAudioInfo, nsamples: usize, chmap: NAChannelMap) -> Result<NABufferType, AllocatorError> {
338 let mut offs: Vec<usize> = Vec::new();
339 if ainfo.format.is_planar() {
340 let len = nsamples.checked_mul(ainfo.channels as usize);
341 if len == None { return Err(AllocatorError::TooLargeDimensions); }
342 let length = len.unwrap();
343 for i in 0..ainfo.channels {
344 offs.push((i as usize) * nsamples);
345 }
346 if ainfo.format.is_float() {
347 if ainfo.format.get_bits() == 32 {
348 let mut data: Vec<f32> = Vec::with_capacity(length);
349 data.resize(length, 0.0);
5076115b 350 let buf: NAAudioBuffer<f32> = NAAudioBuffer { data: Rc::new(RefCell::new(data)), info: ainfo, offs: offs, chmap: chmap, len: nsamples };
22cb00db
KS
351 Ok(NABufferType::AudioF32(buf))
352 } else {
353 Err(AllocatorError::TooLargeDimensions)
354 }
355 } else {
356 if ainfo.format.get_bits() == 8 && !ainfo.format.is_signed() {
357 let mut data: Vec<u8> = Vec::with_capacity(length);
358 data.resize(length, 0);
5076115b 359 let buf: NAAudioBuffer<u8> = NAAudioBuffer { data: Rc::new(RefCell::new(data)), info: ainfo, offs: offs, chmap: chmap, len: nsamples };
22cb00db
KS
360 Ok(NABufferType::AudioU8(buf))
361 } else if ainfo.format.get_bits() == 16 && ainfo.format.is_signed() {
362 let mut data: Vec<i16> = Vec::with_capacity(length);
363 data.resize(length, 0);
5076115b 364 let buf: NAAudioBuffer<i16> = NAAudioBuffer { data: Rc::new(RefCell::new(data)), info: ainfo, offs: offs, chmap: chmap, len: nsamples };
22cb00db
KS
365 Ok(NABufferType::AudioI16(buf))
366 } else {
367 Err(AllocatorError::TooLargeDimensions)
368 }
369 }
370 } else {
371 let len = nsamples.checked_mul(ainfo.channels as usize);
372 if len == None { return Err(AllocatorError::TooLargeDimensions); }
373 let length = ainfo.format.get_audio_size(len.unwrap() as u64);
374 let mut data: Vec<u8> = Vec::with_capacity(length);
375 data.resize(length, 0);
5076115b 376 let buf: NAAudioBuffer<u8> = NAAudioBuffer { data: Rc::new(RefCell::new(data)), info: ainfo, offs: offs, chmap: chmap, len: nsamples };
22cb00db
KS
377 Ok(NABufferType::AudioPacked(buf))
378 }
379}
380
381pub fn alloc_data_buffer(size: usize) -> Result<NABufferType, AllocatorError> {
382 let mut data: Vec<u8> = Vec::with_capacity(size);
383 data.resize(size, 0);
384 let buf: NABufferRefT<u8> = Rc::new(RefCell::new(data));
385 Ok(NABufferType::Data(buf))
386}
387
388pub fn copy_buffer(buf: NABufferType) -> NABufferType {
389 buf.clone()
390}
391
5869fd63 392#[allow(dead_code)]
8869d452
KS
393#[derive(Clone)]
394pub struct NACodecInfo {
ccae5343 395 name: &'static str,
5869fd63 396 properties: NACodecTypeInfo,
8869d452 397 extradata: Option<Rc<Vec<u8>>>,
5869fd63
KS
398}
399
8869d452 400impl NACodecInfo {
ccae5343 401 pub fn new(name: &'static str, p: NACodecTypeInfo, edata: Option<Vec<u8>>) -> Self {
8869d452
KS
402 let extradata = match edata {
403 None => None,
404 Some(vec) => Some(Rc::new(vec)),
405 };
ccae5343 406 NACodecInfo { name: name, properties: p, extradata: extradata }
8869d452 407 }
66116504
KS
408 pub fn new_ref(name: &'static str, p: NACodecTypeInfo, edata: Option<Rc<Vec<u8>>>) -> Self {
409 NACodecInfo { name: name, properties: p, extradata: edata }
410 }
8869d452
KS
411 pub fn get_properties(&self) -> NACodecTypeInfo { self.properties }
412 pub fn get_extradata(&self) -> Option<Rc<Vec<u8>>> {
413 if let Some(ref vec) = self.extradata { return Some(vec.clone()); }
414 None
5869fd63 415 }
66116504
KS
416 pub fn get_name(&self) -> &'static str { self.name }
417 pub fn is_video(&self) -> bool {
418 if let NACodecTypeInfo::Video(_) = self.properties { return true; }
419 false
420 }
421 pub fn is_audio(&self) -> bool {
422 if let NACodecTypeInfo::Audio(_) = self.properties { return true; }
423 false
424 }
5076115b
KS
425 pub fn new_dummy() -> Rc<Self> {
426 Rc::new(DUMMY_CODEC_INFO)
427 }
428 pub fn replace_info(&self, p: NACodecTypeInfo) -> Rc<Self> {
429 Rc::new(NACodecInfo { name: self.name, properties: p, extradata: self.extradata.clone() })
430 }
66116504
KS
431}
432
433impl fmt::Display for NACodecInfo {
434 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
435 let edata = match self.extradata.clone() {
436 None => format!("no extradata"),
437 Some(v) => format!("{} byte(s) of extradata", v.len()),
438 };
439 write!(f, "{}: {} {}", self.name, self.properties, edata)
440 }
441}
442
443pub const DUMMY_CODEC_INFO: NACodecInfo = NACodecInfo {
444 name: "none",
445 properties: NACodecTypeInfo::None,
446 extradata: None };
447
66116504
KS
448#[derive(Debug,Clone)]
449pub enum NAValue {
5869fd63
KS
450 None,
451 Int(i32),
452 Long(i64),
453 String(String),
66116504 454 Data(Rc<Vec<u8>>),
5869fd63
KS
455}
456
88c03b61
KS
457#[derive(Debug,Clone,Copy,PartialEq)]
458#[allow(dead_code)]
459pub enum FrameType {
460 I,
461 P,
462 B,
bc6aac3d 463 Skip,
88c03b61
KS
464 Other,
465}
466
467impl fmt::Display for FrameType {
468 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
469 match *self {
470 FrameType::I => write!(f, "I"),
471 FrameType::P => write!(f, "P"),
472 FrameType::B => write!(f, "B"),
bc6aac3d 473 FrameType::Skip => write!(f, "skip"),
88c03b61
KS
474 FrameType::Other => write!(f, "x"),
475 }
476 }
477}
478
e189501e
KS
479#[derive(Debug,Clone,Copy)]
480pub struct NATimeInfo {
5869fd63
KS
481 pts: Option<u64>,
482 dts: Option<u64>,
483 duration: Option<u64>,
e189501e
KS
484 tb_num: u32,
485 tb_den: u32,
486}
487
488impl NATimeInfo {
489 pub fn new(pts: Option<u64>, dts: Option<u64>, duration: Option<u64>, tb_num: u32, tb_den: u32) -> Self {
490 NATimeInfo { pts: pts, dts: dts, duration: duration, tb_num: tb_num, tb_den: tb_den }
491 }
492 pub fn get_pts(&self) -> Option<u64> { self.pts }
493 pub fn get_dts(&self) -> Option<u64> { self.dts }
494 pub fn get_duration(&self) -> Option<u64> { self.duration }
495 pub fn set_pts(&mut self, pts: Option<u64>) { self.pts = pts; }
496 pub fn set_dts(&mut self, dts: Option<u64>) { self.dts = dts; }
497 pub fn set_duration(&mut self, dur: Option<u64>) { self.duration = dur; }
498}
499
500#[allow(dead_code)]
501#[derive(Clone)]
502pub struct NAFrame {
503 ts: NATimeInfo,
22cb00db 504 buffer: NABufferType,
66116504 505 info: Rc<NACodecInfo>,
88c03b61
KS
506 ftype: FrameType,
507 key: bool,
66116504
KS
508 options: HashMap<String, NAValue>,
509}
510
ebd71c92
KS
511pub type NAFrameRef = Rc<RefCell<NAFrame>>;
512
66116504
KS
513fn get_plane_size(info: &NAVideoInfo, idx: usize) -> (usize, usize) {
514 let chromaton = info.get_format().get_chromaton(idx);
515 if let None = chromaton { return (0, 0); }
516 let (hs, vs) = chromaton.unwrap().get_subsampling();
517 let w = (info.get_width() + ((1 << hs) - 1)) >> hs;
518 let h = (info.get_height() + ((1 << vs) - 1)) >> vs;
519 (w, h)
520}
521
522impl NAFrame {
e189501e 523 pub fn new(ts: NATimeInfo,
88c03b61
KS
524 ftype: FrameType,
525 keyframe: bool,
66116504 526 info: Rc<NACodecInfo>,
22cb00db
KS
527 options: HashMap<String, NAValue>,
528 buffer: NABufferType) -> Self {
e189501e 529 NAFrame { ts: ts, buffer: buffer, info: info, ftype: ftype, key: keyframe, options: options }
ebd71c92 530 }
5076115b 531 pub fn get_info(&self) -> Rc<NACodecInfo> { self.info.clone() }
88c03b61
KS
532 pub fn get_frame_type(&self) -> FrameType { self.ftype }
533 pub fn is_keyframe(&self) -> bool { self.key }
88c03b61
KS
534 pub fn set_frame_type(&mut self, ftype: FrameType) { self.ftype = ftype; }
535 pub fn set_keyframe(&mut self, key: bool) { self.key = key; }
e189501e
KS
536 pub fn get_time_information(&self) -> NATimeInfo { self.ts }
537 pub fn get_pts(&self) -> Option<u64> { self.ts.get_pts() }
538 pub fn get_dts(&self) -> Option<u64> { self.ts.get_dts() }
539 pub fn get_duration(&self) -> Option<u64> { self.ts.get_duration() }
540 pub fn set_pts(&mut self, pts: Option<u64>) { self.ts.set_pts(pts); }
541 pub fn set_dts(&mut self, dts: Option<u64>) { self.ts.set_dts(dts); }
542 pub fn set_duration(&mut self, dur: Option<u64>) { self.ts.set_duration(dur); }
66116504 543
22cb00db 544 pub fn get_buffer(&self) -> NABufferType { self.buffer.clone() }
5869fd63
KS
545}
546
ebd71c92
KS
547impl fmt::Display for NAFrame {
548 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
22cb00db 549 let mut foo = format!("frame type {}", self.ftype);
e189501e
KS
550 if let Some(pts) = self.ts.pts { foo = format!("{} pts {}", foo, pts); }
551 if let Some(dts) = self.ts.dts { foo = format!("{} dts {}", foo, dts); }
552 if let Some(dur) = self.ts.duration { foo = format!("{} duration {}", foo, dur); }
ebd71c92
KS
553 if self.key { foo = format!("{} kf", foo); }
554 write!(f, "[{}]", foo)
555 }
556}
88c03b61 557
48c88fde
KS
558/// Possible stream types.
559#[derive(Debug,Clone,Copy)]
5869fd63 560#[allow(dead_code)]
48c88fde
KS
561pub enum StreamType {
562 /// video stream
563 Video,
564 /// audio stream
565 Audio,
566 /// subtitles
567 Subtitles,
568 /// any data stream (or might be an unrecognized audio/video stream)
569 Data,
570 /// nonexistent stream
571 None,
572}
573
574impl fmt::Display for StreamType {
575 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
576 match *self {
577 StreamType::Video => write!(f, "Video"),
578 StreamType::Audio => write!(f, "Audio"),
579 StreamType::Subtitles => write!(f, "Subtitles"),
580 StreamType::Data => write!(f, "Data"),
581 StreamType::None => write!(f, "-"),
582 }
583 }
584}
585
586#[allow(dead_code)]
587#[derive(Clone)]
588pub struct NAStream {
589 media_type: StreamType,
590 id: u32,
591 num: usize,
592 info: Rc<NACodecInfo>,
e189501e
KS
593 tb_num: u32,
594 tb_den: u32,
595}
596
597pub fn reduce_timebase(tb_num: u32, tb_den: u32) -> (u32, u32) {
598 if tb_num == 0 { return (tb_num, tb_den); }
599 if (tb_den % tb_num) == 0 { return (1, tb_den / tb_num); }
600
601 let mut a = tb_num;
602 let mut b = tb_den;
603
604 while a != b {
605 if a > b { a -= b; }
606 else if b > a { b -= a; }
607 }
608
609 (tb_num / a, tb_den / a)
5869fd63 610}
48c88fde
KS
611
612impl NAStream {
e189501e
KS
613 pub fn new(mt: StreamType, id: u32, info: NACodecInfo, tb_num: u32, tb_den: u32) -> Self {
614 let (n, d) = reduce_timebase(tb_num, tb_den);
615 NAStream { media_type: mt, id: id, num: 0, info: Rc::new(info), tb_num: n, tb_den: d }
48c88fde
KS
616 }
617 pub fn get_id(&self) -> u32 { self.id }
618 pub fn get_num(&self) -> usize { self.num }
619 pub fn set_num(&mut self, num: usize) { self.num = num; }
620 pub fn get_info(&self) -> Rc<NACodecInfo> { self.info.clone() }
e189501e
KS
621 pub fn get_timebase(&self) -> (u32, u32) { (self.tb_num, self.tb_den) }
622 pub fn set_timebase(&mut self, tb_num: u32, tb_den: u32) {
623 let (n, d) = reduce_timebase(tb_num, tb_den);
624 self.tb_num = n;
625 self.tb_den = d;
626 }
48c88fde
KS
627}
628
629impl fmt::Display for NAStream {
630 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
e189501e 631 write!(f, "({}#{} @ {}/{} - {})", self.media_type, self.id, self.tb_num, self.tb_den, self.info.get_properties())
48c88fde
KS
632 }
633}
634
635#[allow(dead_code)]
636pub struct NAPacket {
637 stream: Rc<NAStream>,
e189501e 638 ts: NATimeInfo,
48c88fde
KS
639 buffer: Rc<Vec<u8>>,
640 keyframe: bool,
641// options: HashMap<String, NAValue<'a>>,
642}
643
644impl NAPacket {
e189501e 645 pub fn new(str: Rc<NAStream>, ts: NATimeInfo, kf: bool, vec: Vec<u8>) -> Self {
48c88fde
KS
646// let mut vec: Vec<u8> = Vec::new();
647// vec.resize(size, 0);
e189501e 648 NAPacket { stream: str, ts: ts, keyframe: kf, buffer: Rc::new(vec) }
48c88fde
KS
649 }
650 pub fn get_stream(&self) -> Rc<NAStream> { self.stream.clone() }
e189501e
KS
651 pub fn get_time_information(&self) -> NATimeInfo { self.ts }
652 pub fn get_pts(&self) -> Option<u64> { self.ts.get_pts() }
653 pub fn get_dts(&self) -> Option<u64> { self.ts.get_dts() }
654 pub fn get_duration(&self) -> Option<u64> { self.ts.get_duration() }
48c88fde
KS
655 pub fn is_keyframe(&self) -> bool { self.keyframe }
656 pub fn get_buffer(&self) -> Rc<Vec<u8>> { self.buffer.clone() }
657}
658
659impl Drop for NAPacket {
660 fn drop(&mut self) {}
661}
662
663impl fmt::Display for NAPacket {
664 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
665 let mut foo = format!("[pkt for {} size {}", self.stream, self.buffer.len());
e189501e
KS
666 if let Some(pts) = self.ts.pts { foo = format!("{} pts {}", foo, pts); }
667 if let Some(dts) = self.ts.dts { foo = format!("{} dts {}", foo, dts); }
668 if let Some(dur) = self.ts.duration { foo = format!("{} duration {}", foo, dur); }
48c88fde
KS
669 if self.keyframe { foo = format!("{} kf", foo); }
670 foo = foo + "]";
671 write!(f, "{}", foo)
672 }
673}
674
675pub trait FrameFromPacket {
22cb00db 676 fn new_from_pkt(pkt: &NAPacket, info: Rc<NACodecInfo>, buf: NABufferType) -> NAFrame;
48c88fde
KS
677 fn fill_timestamps(&mut self, pkt: &NAPacket);
678}
679
680impl FrameFromPacket for NAFrame {
22cb00db 681 fn new_from_pkt(pkt: &NAPacket, info: Rc<NACodecInfo>, buf: NABufferType) -> NAFrame {
e189501e 682 NAFrame::new(pkt.ts, FrameType::Other, pkt.keyframe, info, HashMap::new(), buf)
48c88fde
KS
683 }
684 fn fill_timestamps(&mut self, pkt: &NAPacket) {
e189501e 685 self.ts = pkt.get_time_information();
48c88fde
KS
686 }
687}
688