From: Kostya Shishkov Date: Wed, 17 May 2017 14:16:35 +0000 (+0200) Subject: more work on supporting decoders in framework X-Git-Url: https://git.nihav.org/?a=commitdiff_plain;h=6611650404a13bca86a311afdc314406e725897c;p=nihav.git more work on supporting decoders in framework --- diff --git a/src/demuxers/avi.rs b/src/demuxers/avi.rs index 0b8ade0..034ef70 100644 --- a/src/demuxers/avi.rs +++ b/src/demuxers/avi.rs @@ -68,6 +68,9 @@ impl<'a> Demux<'a> for AVIDemuxer<'a> { Ok(()) } + fn get_num_streams(&self) -> usize { self.dmx.get_num_streams() } + fn get_stream(&self, idx: usize) -> Option> { self.dmx.get_stream(idx) } + fn get_frame(&mut self) -> DemuxerResult { if !self.opened { return Err(NoSuchInput); } if self.movi_size == 0 { return Err(EOF); } @@ -280,7 +283,7 @@ fn parse_strf_vids(dmx: &mut AVIDemuxer, size: usize) -> DemuxerResult { let flip = height < 0; let format = if bitcount > 8 { RGB24_FORMAT } else { PAL8_FORMAT }; - let vhdr = NAVideoInfo::new(width, if flip { -height as u32 } else { height as u32}, flip, PAL8_FORMAT); + let vhdr = NAVideoInfo::new(width as usize, if flip { -height as usize } else { height as usize}, flip, PAL8_FORMAT); let vci = NACodecTypeInfo::Video(vhdr); let edata = dmx.read_extradata(size - 40)?; let cname = match register::find_codec_from_avi_fourcc(&compression) { diff --git a/src/demuxers/gdv.rs b/src/demuxers/gdv.rs index 57b6792..09c2620 100644 --- a/src/demuxers/gdv.rs +++ b/src/demuxers/gdv.rs @@ -41,7 +41,7 @@ impl<'a> Demux<'a> for GremlinVideoDemuxer<'a> { let width = src.read_u16le()?; let height = src.read_u16le()?; if max_fs > 0 { - let vhdr = NAVideoInfo::new(width as u32, height as u32, false, PAL8_FORMAT); + let vhdr = NAVideoInfo::new(width as usize, height as usize, false, PAL8_FORMAT); let vci = NACodecTypeInfo::Video(vhdr); let vinfo = NACodecInfo::new("video-gdv", vci, None); self.v_id = self.dmx.add_stream(NAStream::new(StreamType::Video, 0, vinfo)); @@ -75,6 +75,9 @@ impl<'a> Demux<'a> for GremlinVideoDemuxer<'a> { } } + fn get_num_streams(&self) -> usize { self.dmx.get_num_streams() } + fn get_stream(&self, idx: usize) -> Option> { self.dmx.get_stream(idx) } + #[allow(unused_variables)] fn seek(&mut self, time: u64) -> DemuxerResult<()> { if !self.opened { return Err(DemuxerError::NoSuchInput); } diff --git a/src/demuxers/mod.rs b/src/demuxers/mod.rs index 1590024..6499517 100644 --- a/src/demuxers/mod.rs +++ b/src/demuxers/mod.rs @@ -1,10 +1,12 @@ +#[cfg(feature="demuxer_gdv")] pub mod gdv; +#[cfg(feature="demuxer_avi")] pub mod avi; use std::fmt; use std::rc::Rc; use frame::*; -//use std::collections::HashMap; +use std::collections::HashMap; use io::byteio::*; #[derive(Debug,Clone,Copy)] @@ -74,9 +76,16 @@ impl NAPacket { } 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 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()); @@ -104,6 +113,8 @@ type DemuxerResult = Result; pub trait Demux<'a> { fn open(&mut self) -> DemuxerResult<()>; + fn get_num_streams(&self) -> usize; + fn get_stream(&self, idx: usize) -> Option>; fn get_frame(&mut self) -> DemuxerResult; fn seek(&mut self, time: u64) -> DemuxerResult<()>; } @@ -160,15 +171,25 @@ impl Demuxer { } None } + pub fn get_num_streams(&self) -> usize { self.streams.len() } } impl From for DemuxerError { fn from(_: ByteIOError) -> Self { DemuxerError::IOError } } -//impl NADemuxerBuilder { -// #[allow(unused_variables)] -// pub fn create_demuxer(name: &str, url: &str) -> DemuxerResult>> { -// unimplemented!() -// } -//} +pub trait FrameFromPacket { + fn new_from_pkt(pkt: &NAPacket, info: Rc) -> NAFrame; + fn fill_timestamps(&mut self, pkt: &NAPacket); +} + +impl FrameFromPacket for NAFrame { + fn new_from_pkt(pkt: &NAPacket, info: Rc) -> NAFrame { + NAFrame::new(pkt.pts, pkt.dts, pkt.duration, info, HashMap::new()) + } + fn fill_timestamps(&mut self, pkt: &NAPacket) { + self.set_pts(pkt.pts); + self.set_dts(pkt.dts); + self.set_duration(pkt.duration); + } +} diff --git a/src/frame.rs b/src/frame.rs index 7455b72..993d125 100644 --- a/src/frame.rs +++ b/src/frame.rs @@ -4,7 +4,7 @@ use std::rc::Rc; use formats::*; #[allow(dead_code)] -#[derive(Clone,Copy)] +#[derive(Clone,Copy,PartialEq)] pub struct NAAudioInfo { sample_rate: u32, channels: u8, @@ -16,6 +16,10 @@ impl NAAudioInfo { pub fn new(sr: u32, ch: u8, fmt: NASoniton, bl: usize) -> Self { NAAudioInfo { sample_rate: sr, channels: ch, format: fmt, block_len: bl } } + pub fn get_sample_rate(&self) -> u32 { self.sample_rate } + pub fn get_channels(&self) -> u8 { self.channels } + pub fn get_format(&self) -> NASoniton { self.format } + pub fn get_block_len(&self) -> usize { self.block_len } } impl fmt::Display for NAAudioInfo { @@ -25,18 +29,22 @@ impl fmt::Display for NAAudioInfo { } #[allow(dead_code)] -#[derive(Clone,Copy)] +#[derive(Clone,Copy,PartialEq)] pub struct NAVideoInfo { - width: u32, - height: u32, + width: usize, + height: usize, flipped: bool, format: NAPixelFormaton, } impl NAVideoInfo { - pub fn new(w: u32, h: u32, flip: bool, fmt: NAPixelFormaton) -> Self { + pub fn new(w: usize, h: usize, flip: bool, fmt: NAPixelFormaton) -> Self { NAVideoInfo { width: w, height: h, flipped: flip, format: fmt } } + pub fn get_width(&self) -> usize { self.width as usize } + 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 } } impl fmt::Display for NAVideoInfo { @@ -45,7 +53,7 @@ impl fmt::Display for NAVideoInfo { } } -#[derive(Clone,Copy)] +#[derive(Clone,Copy,PartialEq)] pub enum NACodecTypeInfo { None, Audio(NAAudioInfo), @@ -65,9 +73,19 @@ impl fmt::Display for NACodecTypeInfo { #[allow(dead_code)] -pub struct NABuffer<'a> { +#[derive(Clone)] +pub struct NABuffer { id: u64, - data: &'a mut [u8], + data: Rc>, +} + +impl Drop for NABuffer { + fn drop(&mut self) { } +} + +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) } } #[allow(dead_code)] @@ -86,34 +104,159 @@ impl NACodecInfo { }; NACodecInfo { name: name, properties: p, extradata: extradata } } + pub fn new_ref(name: &'static str, p: NACodecTypeInfo, edata: Option>>) -> Self { + NACodecInfo { name: name, properties: p, extradata: edata } + } pub fn get_properties(&self) -> NACodecTypeInfo { self.properties } pub fn get_extradata(&self) -> Option>> { if let Some(ref vec) = self.extradata { return Some(vec.clone()); } None } + pub fn get_name(&self) -> &'static str { self.name } + pub fn is_video(&self) -> bool { + if let NACodecTypeInfo::Video(_) = self.properties { return true; } + false + } + pub fn is_audio(&self) -> bool { + if let NACodecTypeInfo::Audio(_) = self.properties { return true; } + false + } +} + +impl fmt::Display for NACodecInfo { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + let edata = match self.extradata.clone() { + None => format!("no extradata"), + Some(v) => format!("{} byte(s) of extradata", v.len()), + }; + write!(f, "{}: {} {}", self.name, self.properties, edata) + } +} + +pub const DUMMY_CODEC_INFO: NACodecInfo = NACodecInfo { + name: "none", + 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); } -pub trait NABufferAllocator { - fn alloc_buf(info: &NACodecInfo) -> NABuffer<'static>; +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)] -pub enum NAValue<'a> { +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), + _ => (), + } + (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)] +pub enum NAValue { None, Int(i32), Long(i64), String(String), - Data(&'a [u8]), + Data(Rc>), } #[allow(dead_code)] -pub struct NAFrame<'a> { +#[derive(Clone)] +pub struct NAFrame { pts: Option, dts: Option, duration: Option, - buffer: &'a mut NABuffer<'a>, - info: &'a NACodecInfo, - options: HashMap>, + buffer: Rc, + info: Rc, + offsets: Vec, + options: HashMap, +} + +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); } + let (hs, vs) = chromaton.unwrap().get_subsampling(); + let w = (info.get_width() + ((1 << hs) - 1)) >> hs; + let h = (info.get_height() + ((1 << vs) - 1)) >> vs; + (w, h) +} + +impl NAFrame { + pub fn new(pts: Option, + dts: Option, + duration: Option, + 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 } + } + 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_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 + } + } + 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)]