X-Git-Url: https://git.nihav.org/?a=blobdiff_plain;f=src%2Fframe.rs;h=6f22dafc1cdcdc4277704789ad3fbed197f00249;hb=bcfeae48693ae98aa504b86e6ced38de0573e302;hp=d935a0c17aec3e3c418846ee86ffc2b5651f136c;hpb=22cb00db442972f767967809dd89143096700e20;p=nihav.git diff --git a/src/frame.rs b/src/frame.rs index d935a0c..6f22daf 100644 --- a/src/frame.rs +++ b/src/frame.rs @@ -47,6 +47,8 @@ impl NAVideoInfo { pub fn get_height(&self) -> usize { self.height as usize } pub fn is_flipped(&self) -> bool { self.flipped } pub fn get_format(&self) -> NAPixelFormaton { self.format } + pub fn set_width(&mut self, w: usize) { self.width = w; } + pub fn set_height(&mut self, h: usize) { self.height = h; } } impl fmt::Display for NAVideoInfo { @@ -75,6 +77,18 @@ impl NACodecTypeInfo { _ => None, } } + pub fn is_video(&self) -> bool { + match *self { + NACodecTypeInfo::Video(_) => true, + _ => false, + } + } + pub fn is_audio(&self) -> bool { + match *self { + NACodecTypeInfo::Audio(_) => true, + _ => false, + } + } } impl fmt::Display for NACodecTypeInfo { @@ -88,32 +102,14 @@ impl fmt::Display for NACodecTypeInfo { } } -pub type BufferRef = Rc>>; - -#[allow(dead_code)] -#[derive(Clone)] -pub struct NABuffer { - id: u64, - data: BufferRef, -} - -impl Drop for NABuffer { - fn drop(&mut self) { } -} - -impl NABuffer { - pub fn get_data(&self) -> Ref> { self.data.borrow() } - pub fn get_data_mut(&mut self) -> RefMut> { self.data.borrow_mut() } -} - pub type NABufferRefT = Rc>>; -pub type NABufferRef = Rc>; #[derive(Clone)] pub struct NAVideoBuffer { - info: NAVideoInfo, - data: NABufferRefT, - offs: Vec, + info: NAVideoInfo, + data: NABufferRefT, + offs: Vec, + strides: Vec, } impl NAVideoBuffer { @@ -129,11 +125,13 @@ impl NAVideoBuffer { data.clone_from(self.data.borrow().as_ref()); let mut offs: Vec = Vec::with_capacity(self.offs.len()); offs.clone_from(&self.offs); - NAVideoBuffer { info: self.info, data: Rc::new(RefCell::new(data)), offs: offs } + let mut strides: Vec = Vec::with_capacity(self.strides.len()); + strides.clone_from(&self.strides); + NAVideoBuffer { info: self.info, data: Rc::new(RefCell::new(data)), offs: offs, strides: strides } } pub fn get_stride(&self, idx: usize) -> usize { - if idx >= self.info.get_format().get_num_comp() { return 0; } - self.info.get_format().get_chromaton(idx).unwrap().get_linesize(self.info.get_width()) + if idx >= self.strides.len() { return 0; } + self.strides[idx] } pub fn get_dimensions(&self, idx: usize) -> (usize, usize) { get_plane_size(&self.info, idx) @@ -146,6 +144,7 @@ pub struct NAAudioBuffer { data: NABufferRefT, offs: Vec, chmap: NAChannelMap, + len: usize, } impl NAAudioBuffer { @@ -162,7 +161,15 @@ impl NAAudioBuffer { data.clone_from(self.data.borrow().as_ref()); let mut offs: Vec = Vec::with_capacity(self.offs.len()); offs.clone_from(&self.offs); - NAAudioBuffer { info: self.info, data: Rc::new(RefCell::new(data)), offs: offs, chmap: self.get_chmap() } + NAAudioBuffer { info: self.info, data: Rc::new(RefCell::new(data)), offs: offs, chmap: self.get_chmap(), len: self.len } + } + pub fn get_length(&self) -> usize { self.len } +} + +impl NAAudioBuffer { + pub fn new_from_buf(info: NAAudioInfo, data: NABufferRefT, chmap: NAChannelMap) -> Self { + let len = data.borrow().len(); + NAAudioBuffer { info: info, data: data, chmap: chmap, offs: Vec::new(), len: len } } } @@ -173,6 +180,7 @@ pub enum NABufferType { VideoPacked(NAVideoBuffer), AudioU8 (NAAudioBuffer), AudioI16 (NAAudioBuffer), + AudioI32 (NAAudioBuffer), AudioF32 (NAAudioBuffer), AudioPacked(NAAudioBuffer), Data (NABufferRefT), @@ -195,6 +203,38 @@ impl NABufferType { pub fn get_vbuf(&mut self) -> Option> { match *self { NABufferType::Video(ref vb) => Some(vb.clone()), + NABufferType::VideoPacked(ref vb) => Some(vb.clone()), + _ => None, + } + } + pub fn get_vbuf16(&mut self) -> Option> { + match *self { + NABufferType::Video16(ref vb) => Some(vb.clone()), + _ => None, + } + } + pub fn get_abuf_u8(&mut self) -> Option> { + match *self { + NABufferType::AudioU8(ref ab) => Some(ab.clone()), + NABufferType::AudioPacked(ref ab) => Some(ab.clone()), + _ => None, + } + } + pub fn get_abuf_i16(&mut self) -> Option> { + match *self { + NABufferType::AudioI16(ref ab) => Some(ab.clone()), + _ => None, + } + } + pub fn get_abuf_i32(&mut self) -> Option> { + match *self { + NABufferType::AudioI32(ref ab) => Some(ab.clone()), + _ => None, + } + } + pub fn get_abuf_f32(&mut self) -> Option> { + match *self { + NABufferType::AudioF32(ref ab) => Some(ab.clone()), _ => None, } } @@ -209,7 +249,8 @@ pub enum AllocatorError { pub fn alloc_video_buffer(vinfo: NAVideoInfo, align: u8) -> Result { let fmt = &vinfo.format; let mut new_size: usize = 0; - let mut offs: Vec = Vec::new(); + let mut offs: Vec = Vec::new(); + let mut strides: Vec = Vec::new(); for i in 0..fmt.get_num_comp() { if fmt.get_chromaton(i) == None { return Err(AllocatorError::FormatError); } @@ -221,7 +262,9 @@ pub fn alloc_video_buffer(vinfo: NAVideoInfo, align: u8) -> Result Result = Vec::with_capacity(new_size.unwrap()); + data.resize(new_size.unwrap(), 0); + let buf: NAVideoBuffer = NAVideoBuffer { data: Rc::new(RefCell::new(data)), info: vinfo, offs: offs, strides: strides }; + Ok(NABufferType::Video(buf)) + } else if !all_packed { for i in 0..fmt.get_num_comp() { - let chr = fmt.get_chromaton(i).unwrap(); + let ochr = fmt.get_chromaton(i); + if let None = ochr { continue; } + let chr = ochr.unwrap(); if !vinfo.is_flipped() { offs.push(new_size as usize); } - let cur_w = chr.get_width(width); + let stride = chr.get_linesize(width); let cur_h = chr.get_height(height); - let cur_sz = cur_w.checked_mul(cur_h); + let cur_sz = stride.checked_mul(cur_h); if cur_sz == None { return Err(AllocatorError::TooLargeDimensions); } let new_sz = new_size.checked_add(cur_sz.unwrap()); if new_sz == None { return Err(AllocatorError::TooLargeDimensions); } @@ -246,16 +306,17 @@ pub fn alloc_video_buffer(vinfo: NAVideoInfo, align: u8) -> Result = Vec::with_capacity(new_size); data.resize(new_size, 0); - let buf: NAVideoBuffer = NAVideoBuffer { data: Rc::new(RefCell::new(data)), info: vinfo, offs: offs }; + let buf: NAVideoBuffer = NAVideoBuffer { data: Rc::new(RefCell::new(data)), info: vinfo, offs: offs, strides: strides }; Ok(NABufferType::Video(buf)) } else { let mut data: Vec = Vec::with_capacity(new_size); data.resize(new_size, 0); - let buf: NAVideoBuffer = NAVideoBuffer { data: Rc::new(RefCell::new(data)), info: vinfo, offs: offs }; + let buf: NAVideoBuffer = NAVideoBuffer { data: Rc::new(RefCell::new(data)), info: vinfo, offs: offs, strides: strides }; Ok(NABufferType::Video16(buf)) } } else { @@ -267,7 +328,8 @@ pub fn alloc_video_buffer(vinfo: NAVideoInfo, align: u8) -> Result = Vec::with_capacity(new_size); data.resize(new_size, 0); - let buf: NAVideoBuffer = NAVideoBuffer { data: Rc::new(RefCell::new(data)), info: vinfo, offs: offs }; + strides.push(line_sz.unwrap()); + let buf: NAVideoBuffer = NAVideoBuffer { data: Rc::new(RefCell::new(data)), info: vinfo, offs: offs, strides: strides }; Ok(NABufferType::VideoPacked(buf)) } } @@ -285,7 +347,7 @@ pub fn alloc_audio_buffer(ainfo: NAAudioInfo, nsamples: usize, chmap: NAChannelM if ainfo.format.get_bits() == 32 { let mut data: Vec = Vec::with_capacity(length); data.resize(length, 0.0); - let buf: NAAudioBuffer = NAAudioBuffer { data: Rc::new(RefCell::new(data)), info: ainfo, offs: offs, chmap: chmap }; + let buf: NAAudioBuffer = NAAudioBuffer { data: Rc::new(RefCell::new(data)), info: ainfo, offs: offs, chmap: chmap, len: nsamples }; Ok(NABufferType::AudioF32(buf)) } else { Err(AllocatorError::TooLargeDimensions) @@ -294,12 +356,12 @@ pub fn alloc_audio_buffer(ainfo: NAAudioInfo, nsamples: usize, chmap: NAChannelM if ainfo.format.get_bits() == 8 && !ainfo.format.is_signed() { let mut data: Vec = Vec::with_capacity(length); data.resize(length, 0); - let buf: NAAudioBuffer = NAAudioBuffer { data: Rc::new(RefCell::new(data)), info: ainfo, offs: offs, chmap: chmap }; + let buf: NAAudioBuffer = NAAudioBuffer { data: Rc::new(RefCell::new(data)), info: ainfo, offs: offs, chmap: chmap, len: nsamples }; Ok(NABufferType::AudioU8(buf)) } else if ainfo.format.get_bits() == 16 && ainfo.format.is_signed() { let mut data: Vec = Vec::with_capacity(length); data.resize(length, 0); - let buf: NAAudioBuffer = NAAudioBuffer { data: Rc::new(RefCell::new(data)), info: ainfo, offs: offs, chmap: chmap }; + let buf: NAAudioBuffer = NAAudioBuffer { data: Rc::new(RefCell::new(data)), info: ainfo, offs: offs, chmap: chmap, len: nsamples }; Ok(NABufferType::AudioI16(buf)) } else { Err(AllocatorError::TooLargeDimensions) @@ -311,7 +373,7 @@ pub fn alloc_audio_buffer(ainfo: NAAudioInfo, nsamples: usize, chmap: NAChannelM let length = ainfo.format.get_audio_size(len.unwrap() as u64); let mut data: Vec = Vec::with_capacity(length); data.resize(length, 0); - let buf: NAAudioBuffer = NAAudioBuffer { data: Rc::new(RefCell::new(data)), info: ainfo, offs: offs, chmap: chmap }; + let buf: NAAudioBuffer = NAAudioBuffer { data: Rc::new(RefCell::new(data)), info: ainfo, offs: offs, chmap: chmap, len: nsamples }; Ok(NABufferType::AudioPacked(buf)) } } @@ -360,6 +422,12 @@ impl NACodecInfo { if let NACodecTypeInfo::Audio(_) = self.properties { return true; } false } + pub fn new_dummy() -> Rc { + Rc::new(DUMMY_CODEC_INFO) + } + pub fn replace_info(&self, p: NACodecTypeInfo) -> Rc { + Rc::new(NACodecInfo { name: self.name, properties: p, extradata: self.extradata.clone() }) + } } impl fmt::Display for NACodecInfo { @@ -392,6 +460,7 @@ pub enum FrameType { I, P, B, + Skip, Other, } @@ -401,17 +470,37 @@ impl fmt::Display for FrameType { FrameType::I => write!(f, "I"), FrameType::P => write!(f, "P"), FrameType::B => write!(f, "B"), + FrameType::Skip => write!(f, "skip"), FrameType::Other => write!(f, "x"), } } } -#[allow(dead_code)] -#[derive(Clone)] -pub struct NAFrame { +#[derive(Debug,Clone,Copy)] +pub struct NATimeInfo { pts: Option, dts: Option, duration: Option, + tb_num: u32, + tb_den: u32, +} + +impl NATimeInfo { + pub fn new(pts: Option, dts: Option, duration: Option, tb_num: u32, tb_den: u32) -> Self { + NATimeInfo { pts: pts, dts: dts, duration: duration, tb_num: tb_num, tb_den: tb_den } + } + pub fn get_pts(&self) -> Option { self.pts } + pub fn get_dts(&self) -> Option { self.dts } + pub fn get_duration(&self) -> Option { self.duration } + pub fn set_pts(&mut self, pts: Option) { self.pts = pts; } + pub fn set_dts(&mut self, dts: Option) { self.dts = dts; } + pub fn set_duration(&mut self, dur: Option) { self.duration = dur; } +} + +#[allow(dead_code)] +#[derive(Clone)] +pub struct NAFrame { + ts: NATimeInfo, buffer: NABufferType, info: Rc, ftype: FrameType, @@ -431,26 +520,26 @@ fn get_plane_size(info: &NAVideoInfo, idx: usize) -> (usize, usize) { } impl NAFrame { - pub fn new(pts: Option, - dts: Option, - duration: Option, + pub fn new(ts: NATimeInfo, ftype: FrameType, keyframe: bool, info: Rc, options: HashMap, buffer: NABufferType) -> Self { - NAFrame { pts: pts, dts: dts, duration: duration, buffer: buffer, info: info, ftype: ftype, key: keyframe, options: options } + NAFrame { ts: ts, buffer: buffer, info: info, ftype: ftype, key: keyframe, options: options } } - pub fn get_pts(&self) -> Option { self.pts } - pub fn get_dts(&self) -> Option { self.dts } - pub fn get_duration(&self) -> Option { self.duration } + pub fn get_info(&self) -> Rc { self.info.clone() } pub fn get_frame_type(&self) -> FrameType { self.ftype } pub fn is_keyframe(&self) -> bool { self.key } - pub fn set_pts(&mut self, pts: Option) { self.pts = pts; } - pub fn set_dts(&mut self, dts: Option) { self.dts = dts; } - pub fn set_duration(&mut self, dur: Option) { self.duration = dur; } pub fn set_frame_type(&mut self, ftype: FrameType) { self.ftype = ftype; } pub fn set_keyframe(&mut self, key: bool) { self.key = key; } + pub fn get_time_information(&self) -> NATimeInfo { self.ts } + pub fn get_pts(&self) -> Option { self.ts.get_pts() } + pub fn get_dts(&self) -> Option { self.ts.get_dts() } + pub fn get_duration(&self) -> Option { self.ts.get_duration() } + pub fn set_pts(&mut self, pts: Option) { self.ts.set_pts(pts); } + pub fn set_dts(&mut self, dts: Option) { self.ts.set_dts(dts); } + pub fn set_duration(&mut self, dur: Option) { self.ts.set_duration(dur); } pub fn get_buffer(&self) -> NABufferType { self.buffer.clone() } } @@ -458,9 +547,9 @@ impl NAFrame { impl fmt::Display for NAFrame { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { let mut foo = format!("frame type {}", self.ftype); - if let Some(pts) = self.pts { foo = format!("{} pts {}", foo, pts); } - if let Some(dts) = self.dts { foo = format!("{} dts {}", foo, dts); } - if let Some(dur) = self.duration { foo = format!("{} duration {}", foo, dur); } + if let Some(pts) = self.ts.pts { foo = format!("{} pts {}", foo, pts); } + if let Some(dts) = self.ts.dts { foo = format!("{} dts {}", foo, dts); } + if let Some(dur) = self.ts.duration { foo = format!("{} duration {}", foo, dur); } if self.key { foo = format!("{} kf", foo); } write!(f, "[{}]", foo) } @@ -501,45 +590,68 @@ pub struct NAStream { id: u32, num: usize, info: Rc, + tb_num: u32, + tb_den: u32, +} + +pub fn reduce_timebase(tb_num: u32, tb_den: u32) -> (u32, u32) { + if tb_num == 0 { return (tb_num, tb_den); } + if (tb_den % tb_num) == 0 { return (1, tb_den / tb_num); } + + let mut a = tb_num; + let mut b = tb_den; + + while a != b { + if a > b { a -= b; } + else if b > a { b -= a; } + } + + (tb_num / a, tb_den / a) } impl NAStream { - pub fn new(mt: StreamType, id: u32, info: NACodecInfo) -> Self { - NAStream { media_type: mt, id: id, num: 0, info: Rc::new(info) } + pub fn new(mt: StreamType, id: u32, info: NACodecInfo, tb_num: u32, tb_den: u32) -> Self { + let (n, d) = reduce_timebase(tb_num, tb_den); + NAStream { media_type: mt, id: id, num: 0, info: Rc::new(info), tb_num: n, tb_den: d } } pub fn get_id(&self) -> u32 { self.id } pub fn get_num(&self) -> usize { self.num } pub fn set_num(&mut self, num: usize) { self.num = num; } pub fn get_info(&self) -> Rc { self.info.clone() } + pub fn get_timebase(&self) -> (u32, u32) { (self.tb_num, self.tb_den) } + pub fn set_timebase(&mut self, tb_num: u32, tb_den: u32) { + let (n, d) = reduce_timebase(tb_num, tb_den); + self.tb_num = n; + self.tb_den = d; + } } impl fmt::Display for NAStream { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "({}#{} - {})", self.media_type, self.id, self.info.get_properties()) + write!(f, "({}#{} @ {}/{} - {})", self.media_type, self.id, self.tb_num, self.tb_den, self.info.get_properties()) } } #[allow(dead_code)] pub struct NAPacket { stream: Rc, - pts: Option, - dts: Option, - duration: Option, + ts: NATimeInfo, buffer: Rc>, keyframe: bool, // options: HashMap>, } impl NAPacket { - pub fn new(str: Rc, pts: Option, dts: Option, dur: Option, kf: bool, vec: Vec) -> Self { + pub fn new(str: Rc, ts: NATimeInfo, kf: bool, vec: Vec) -> Self { // let mut vec: Vec = Vec::new(); // vec.resize(size, 0); - NAPacket { stream: str, pts: pts, dts: dts, duration: dur, keyframe: kf, buffer: Rc::new(vec) } + NAPacket { stream: str, ts: ts, keyframe: kf, buffer: Rc::new(vec) } } pub fn get_stream(&self) -> Rc { self.stream.clone() } - pub fn get_pts(&self) -> Option { self.pts } - pub fn get_dts(&self) -> Option { self.dts } - pub fn get_duration(&self) -> Option { self.duration } + pub fn get_time_information(&self) -> NATimeInfo { self.ts } + pub fn get_pts(&self) -> Option { self.ts.get_pts() } + pub fn get_dts(&self) -> Option { self.ts.get_dts() } + pub fn get_duration(&self) -> Option { self.ts.get_duration() } pub fn is_keyframe(&self) -> bool { self.keyframe } pub fn get_buffer(&self) -> Rc> { self.buffer.clone() } } @@ -551,9 +663,9 @@ impl Drop for NAPacket { impl fmt::Display for NAPacket { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { let mut foo = format!("[pkt for {} size {}", self.stream, self.buffer.len()); - if let Some(pts) = self.pts { foo = format!("{} pts {}", foo, pts); } - if let Some(dts) = self.dts { foo = format!("{} dts {}", foo, dts); } - if let Some(dur) = self.duration { foo = format!("{} duration {}", foo, dur); } + if let Some(pts) = self.ts.pts { foo = format!("{} pts {}", foo, pts); } + if let Some(dts) = self.ts.dts { foo = format!("{} dts {}", foo, dts); } + if let Some(dur) = self.ts.duration { foo = format!("{} duration {}", foo, dur); } if self.keyframe { foo = format!("{} kf", foo); } foo = foo + "]"; write!(f, "{}", foo) @@ -567,12 +679,10 @@ pub trait FrameFromPacket { impl FrameFromPacket for NAFrame { fn new_from_pkt(pkt: &NAPacket, info: Rc, buf: NABufferType) -> NAFrame { - NAFrame::new(pkt.pts, pkt.dts, pkt.duration, FrameType::Other, pkt.keyframe, info, HashMap::new(), buf) + NAFrame::new(pkt.ts, FrameType::Other, pkt.keyframe, info, HashMap::new(), buf) } fn fill_timestamps(&mut self, pkt: &NAPacket) { - self.set_pts(pkt.pts); - self.set_dts(pkt.dts); - self.set_duration(pkt.duration); + self.ts = pkt.get_time_information(); } }