X-Git-Url: https://git.nihav.org/?a=blobdiff_plain;f=src%2Fframe.rs;h=19c1733c9c716b7101f199d9301e62931f68c5e5;hb=e189501ed92ffd55123f9fceb7e4c78ddf1be936;hp=993d125a326aff9afe9f3e3fed6365037be39582;hpb=6611650404a13bca86a311afdc314406e725897c;p=nihav.git diff --git a/src/frame.rs b/src/frame.rs index 993d125..19c1733 100644 --- a/src/frame.rs +++ b/src/frame.rs @@ -1,6 +1,8 @@ +use std::cmp::max; use std::collections::HashMap; use std::fmt; use std::rc::Rc; +use std::cell::*; use formats::*; #[allow(dead_code)] @@ -60,6 +62,21 @@ pub enum NACodecTypeInfo { Video(NAVideoInfo), } +impl NACodecTypeInfo { + pub fn get_video_info(&self) -> Option { + match *self { + NACodecTypeInfo::Video(vinfo) => Some(vinfo), + _ => None, + } + } + pub fn get_audio_info(&self) -> Option { + match *self { + NACodecTypeInfo::Audio(ainfo) => Some(ainfo), + _ => None, + } + } +} + impl fmt::Display for NACodecTypeInfo { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { let ret = match *self { @@ -71,21 +88,226 @@ impl fmt::Display for NACodecTypeInfo { } } +pub type BufferRef = Rc>>; + +pub type NABufferRefT = Rc>>; -#[allow(dead_code)] #[derive(Clone)] -pub struct NABuffer { - id: u64, - data: Rc>, +pub struct NAVideoBuffer { + info: NAVideoInfo, + data: NABufferRefT, + offs: Vec, +} + +impl NAVideoBuffer { + pub fn get_offset(&self, idx: usize) -> usize { + if idx >= self.offs.len() { 0 } + else { self.offs[idx] } + } + pub fn get_info(&self) -> NAVideoInfo { self.info } + pub fn get_data(&self) -> Ref> { self.data.borrow() } + pub fn get_data_mut(&mut self) -> RefMut> { self.data.borrow_mut() } + pub fn copy_buffer(&mut self) -> Self { + let mut data: Vec = Vec::with_capacity(self.data.borrow().len()); + 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 } + } + 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()) + } + pub fn get_dimensions(&self, idx: usize) -> (usize, usize) { + get_plane_size(&self.info, idx) + } } -impl Drop for NABuffer { - fn drop(&mut self) { } +#[derive(Clone)] +pub struct NAAudioBuffer { + info: NAAudioInfo, + data: NABufferRefT, + offs: Vec, + chmap: NAChannelMap, } -impl NABuffer { - pub fn get_data(&self) -> Rc> { self.data.clone() } - pub fn get_data_mut(&mut self) -> Option<&mut Vec> { Rc::get_mut(&mut self.data) } +impl NAAudioBuffer { + pub fn get_offset(&self, idx: usize) -> usize { + if idx >= self.offs.len() { 0 } + else { self.offs[idx] } + } + pub fn get_info(&self) -> NAAudioInfo { self.info } + pub fn get_chmap(&self) -> NAChannelMap { self.chmap.clone() } + pub fn get_data(&self) -> Ref> { self.data.borrow() } + pub fn get_data_mut(&mut self) -> RefMut> { self.data.borrow_mut() } + pub fn copy_buffer(&mut self) -> Self { + let mut data: Vec = Vec::with_capacity(self.data.borrow().len()); + 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() } + } +} + +#[derive(Clone)] +pub enum NABufferType { + Video (NAVideoBuffer), + Video16 (NAVideoBuffer), + VideoPacked(NAVideoBuffer), + AudioU8 (NAAudioBuffer), + AudioI16 (NAAudioBuffer), + AudioF32 (NAAudioBuffer), + AudioPacked(NAAudioBuffer), + Data (NABufferRefT), + None, +} + +impl NABufferType { + pub fn get_offset(&self, idx: usize) -> usize { + match *self { + NABufferType::Video(ref vb) => vb.get_offset(idx), + NABufferType::Video16(ref vb) => vb.get_offset(idx), + NABufferType::VideoPacked(ref vb) => vb.get_offset(idx), + NABufferType::AudioU8(ref ab) => ab.get_offset(idx), + NABufferType::AudioI16(ref ab) => ab.get_offset(idx), + NABufferType::AudioF32(ref ab) => ab.get_offset(idx), + NABufferType::AudioPacked(ref ab) => ab.get_offset(idx), + _ => 0, + } + } + pub fn get_vbuf(&mut self) -> Option> { + match *self { + NABufferType::Video(ref vb) => Some(vb.clone()), + _ => None, + } + } +} + +#[derive(Debug,Clone,Copy,PartialEq)] +pub enum AllocatorError { + TooLargeDimensions, + FormatError, +} + +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(); + + for i in 0..fmt.get_num_comp() { + if fmt.get_chromaton(i) == None { return Err(AllocatorError::FormatError); } + } + + let align_mod = ((1 << align) as usize) - 1; + let width = ((vinfo.width as usize) + align_mod) & !align_mod; + let height = ((vinfo.height as usize) + align_mod) & !align_mod; + let mut max_depth = 0; + let mut all_packed = true; + for i in 0..fmt.get_num_comp() { + let chr = fmt.get_chromaton(i).unwrap(); + if !chr.is_packed() { + all_packed = false; + break; + } + max_depth = max(max_depth, chr.get_depth()); + } + +//todo semi-packed like NV12 + if !all_packed { + for i in 0..fmt.get_num_comp() { + let chr = fmt.get_chromaton(i).unwrap(); + if !vinfo.is_flipped() { + offs.push(new_size as usize); + } + let cur_w = chr.get_width(width); + let cur_h = chr.get_height(height); + let cur_sz = cur_w.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); } + new_size = new_sz.unwrap(); + if vinfo.is_flipped() { + offs.push(new_size as usize); + } + } + if max_depth <= 8 { + 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 }; + 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 }; + Ok(NABufferType::Video16(buf)) + } + } else { + let elem_sz = fmt.get_elem_size(); + let line_sz = width.checked_mul(elem_sz as usize); + if line_sz == None { return Err(AllocatorError::TooLargeDimensions); } + let new_sz = line_sz.unwrap().checked_mul(height); + if new_sz == None { return Err(AllocatorError::TooLargeDimensions); } + new_size = new_sz.unwrap(); + 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 }; + Ok(NABufferType::VideoPacked(buf)) + } +} + +pub fn alloc_audio_buffer(ainfo: NAAudioInfo, nsamples: usize, chmap: NAChannelMap) -> Result { + let mut offs: Vec = Vec::new(); + if ainfo.format.is_planar() { + let len = nsamples.checked_mul(ainfo.channels as usize); + if len == None { return Err(AllocatorError::TooLargeDimensions); } + let length = len.unwrap(); + for i in 0..ainfo.channels { + offs.push((i as usize) * nsamples); + } + if ainfo.format.is_float() { + 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 }; + Ok(NABufferType::AudioF32(buf)) + } else { + Err(AllocatorError::TooLargeDimensions) + } + } else { + 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 }; + 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 }; + Ok(NABufferType::AudioI16(buf)) + } else { + Err(AllocatorError::TooLargeDimensions) + } + } + } else { + let len = nsamples.checked_mul(ainfo.channels as usize); + if len == None { return Err(AllocatorError::TooLargeDimensions); } + 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 }; + Ok(NABufferType::AudioPacked(buf)) + } +} + +pub fn alloc_data_buffer(size: usize) -> Result { + let mut data: Vec = Vec::with_capacity(size); + data.resize(size, 0); + let buf: NABufferRefT = Rc::new(RefCell::new(data)); + Ok(NABufferType::Data(buf)) +} + +pub fn copy_buffer(buf: NABufferType) -> NABufferType { + buf.clone() } #[allow(dead_code)] @@ -138,77 +360,69 @@ pub const DUMMY_CODEC_INFO: NACodecInfo = NACodecInfo { properties: NACodecTypeInfo::None, extradata: None }; -fn alloc_video_buf(vinfo: NAVideoInfo, data: &mut Vec, offs: &mut Vec) { -//todo use overflow detection mul - let width = vinfo.width as usize; - let height = vinfo.height as usize; - let fmt = &vinfo.format; - let mut new_size = 0; - for i in 0..fmt.get_num_comp() { - let chr = fmt.get_chromaton(i).unwrap(); - if !vinfo.is_flipped() { - offs.push(new_size as usize); - } - new_size += chr.get_data_size(width, height); - if vinfo.is_flipped() { - offs.push(new_size as usize); - } - } - data.resize(new_size, 0); +#[derive(Debug,Clone)] +pub enum NAValue { + None, + Int(i32), + Long(i64), + String(String), + Data(Rc>), } -fn alloc_audio_buf(ainfo: NAAudioInfo, data: &mut Vec, offs: &mut Vec) { -//todo better alloc - let length = ((ainfo.sample_rate as usize) * (ainfo.format.get_bits() as usize)) >> 3; - let new_size: usize = length * (ainfo.channels as usize); - data.resize(new_size, 0); - for i in 0..ainfo.channels { - if ainfo.format.is_planar() { - offs.push((i as usize) * length); - } else { - offs.push(((i * ainfo.format.get_bits()) >> 3) as usize); - } - } +#[derive(Debug,Clone,Copy,PartialEq)] +#[allow(dead_code)] +pub enum FrameType { + I, + P, + B, + Other, } -pub fn alloc_buf(info: &NACodecInfo) -> (Rc, Vec) { - let mut data: Vec = Vec::new(); - let mut offs: Vec = Vec::new(); - match info.properties { - NACodecTypeInfo::Audio(ainfo) => alloc_audio_buf(ainfo, &mut data, &mut offs), - NACodecTypeInfo::Video(vinfo) => alloc_video_buf(vinfo, &mut data, &mut offs), - _ => (), +impl fmt::Display for FrameType { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + match *self { + FrameType::I => write!(f, "I"), + FrameType::P => write!(f, "P"), + FrameType::B => write!(f, "B"), + FrameType::Other => write!(f, "x"), + } } - (Rc::new(NABuffer { id: 0, data: Rc::new(data) }), offs) } -pub fn copy_buf(buf: &NABuffer) -> Rc { - let mut data: Vec = Vec::new(); - data.clone_from(buf.get_data().as_ref()); - Rc::new(NABuffer { id: 0, data: Rc::new(data) }) +#[derive(Debug,Clone,Copy)] +pub struct NATimeInfo { + pts: Option, + dts: Option, + duration: Option, + tb_num: u32, + tb_den: u32, } -#[derive(Debug,Clone)] -pub enum NAValue { - None, - Int(i32), - Long(i64), - String(String), - Data(Rc>), +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 { - pts: Option, - dts: Option, - duration: Option, - buffer: Rc, + ts: NATimeInfo, + buffer: NABufferType, info: Rc, - offsets: Vec, + ftype: FrameType, + key: bool, options: HashMap, } +pub type NAFrameRef = Rc>; + fn get_plane_size(info: &NAVideoInfo, idx: usize) -> (usize, usize) { let chromaton = info.get_format().get_chromaton(idx); if let None = chromaton { return (0, 0); } @@ -219,47 +433,168 @@ 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) -> Self { - let (buf, offs) = alloc_buf(&info); - NAFrame { pts: pts, dts: dts, duration: duration, buffer: buf, offsets: offs, info: info, options: options } + options: HashMap, + buffer: NABufferType) -> Self { + NAFrame { ts: ts, buffer: buffer, info: info, ftype: ftype, key: keyframe, options: options } } - pub fn from_copy(src: &NAFrame) -> Self { - let buf = copy_buf(src.get_buffer().as_ref()); - let mut offs: Vec = Vec::new(); - offs.clone_from(&src.offsets); - NAFrame { pts: None, dts: None, duration: None, buffer: buf, offsets: offs, info: src.info.clone(), options: src.options.clone() } + pub fn get_frame_type(&self) -> FrameType { self.ftype } + pub fn is_keyframe(&self) -> bool { self.key } + 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() } +} + +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.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) } - 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; } +} - pub fn get_offset(&self, idx: usize) -> usize { self.offsets[idx] } - pub fn get_buffer(&self) -> Rc { self.buffer.clone() } - pub fn get_buffer_mut(&mut self) -> Option<&mut NABuffer> { Rc::get_mut(&mut self.buffer) } - pub fn get_stride(&self, idx: usize) -> usize { - if let NACodecTypeInfo::Video(vinfo) = self.info.get_properties() { - if idx >= vinfo.get_format().get_num_comp() { return 0; } - vinfo.get_format().get_chromaton(idx).unwrap().get_linesize(vinfo.get_width()) - } else { - 0 +/// Possible stream types. +#[derive(Debug,Clone,Copy)] +#[allow(dead_code)] +pub enum StreamType { + /// video stream + Video, + /// audio stream + Audio, + /// subtitles + Subtitles, + /// any data stream (or might be an unrecognized audio/video stream) + Data, + /// nonexistent stream + None, +} + +impl fmt::Display for StreamType { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + match *self { + StreamType::Video => write!(f, "Video"), + StreamType::Audio => write!(f, "Audio"), + StreamType::Subtitles => write!(f, "Subtitles"), + StreamType::Data => write!(f, "Data"), + StreamType::None => write!(f, "-"), } } - pub fn get_dimensions(&self, idx: usize) -> (usize, usize) { - match self.info.get_properties() { - NACodecTypeInfo::Video(ref vinfo) => get_plane_size(vinfo, idx), - _ => (0, 0), - } +} + +#[allow(dead_code)] +#[derive(Clone)] +pub struct NAStream { + media_type: StreamType, + 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, 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.tb_num, self.tb_den, self.info.get_properties()) } } #[allow(dead_code)] -pub struct NACodecContext<'a> { - info: &'a NACodecInfo, +pub struct NAPacket { + stream: Rc, + ts: NATimeInfo, + buffer: Rc>, + keyframe: bool, +// options: HashMap>, +} + +impl NAPacket { + 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, ts: ts, keyframe: kf, buffer: Rc::new(vec) } + } + pub fn get_stream(&self) -> Rc { self.stream.clone() } + 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() } +} + +impl Drop for NAPacket { + fn drop(&mut self) {} } + +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.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) + } +} + +pub trait FrameFromPacket { + fn new_from_pkt(pkt: &NAPacket, info: Rc, buf: NABufferType) -> NAFrame; + fn fill_timestamps(&mut self, pkt: &NAPacket); +} + +impl FrameFromPacket for NAFrame { + fn new_from_pkt(pkt: &NAPacket, info: Rc, buf: NABufferType) -> NAFrame { + NAFrame::new(pkt.ts, FrameType::Other, pkt.keyframe, info, HashMap::new(), buf) + } + fn fill_timestamps(&mut self, pkt: &NAPacket) { + self.ts = pkt.get_time_information(); + } +} +