From: Kostya Shishkov Date: Fri, 26 May 2017 05:35:11 +0000 (+0200) Subject: more utility code for decoders, move dumping functions to the core X-Git-Url: https://git.nihav.org/?a=commitdiff_plain;h=3f29a2a8c129f8d9bea68b235d5c3a649d60be2c;p=nihav.git more utility code for decoders, move dumping functions to the core --- diff --git a/src/codecs/indeo2.rs b/src/codecs/indeo2.rs index bc2700b..707a59d 100644 --- a/src/codecs/indeo2.rs +++ b/src/codecs/indeo2.rs @@ -371,8 +371,6 @@ mod test { use demuxers::*; use frame::NAFrameRef; use io::byteio::*; - use std::fs::{File, OpenOptions}; - use std::io::prelude::*; #[test] fn test_indeo2() { @@ -404,69 +402,16 @@ mod test { panic!("error"); } let pkt = pktres.unwrap(); + if pkt.get_pts().unwrap() > 10 { break; } let streamno = pkt.get_stream().get_id() as usize; if let Some(ref mut dec) = decs[streamno] { let frm = dec.decode(&pkt).unwrap(); if pkt.get_stream().get_info().is_video() { - write_pgmyuv(streamno, pkt.get_pts().unwrap(), frm); + write_pgmyuv("iv2", streamno, pkt.get_pts().unwrap(), frm); } else { - write_sound(streamno, frm, pkt.get_pts().unwrap() == 0); + write_sound("iv2", streamno, frm, pkt.get_pts().unwrap() == 0); } } } } - - fn write_pgmyuv(strno: usize, num: u64, frmref: NAFrameRef) { - let frm = frmref.borrow(); - let name = format!("assets/out{:02}_{:04}.pgm", strno, num); - let mut ofile = File::create(name).unwrap(); - let buf = frm.get_buffer().get_vbuf().unwrap(); - let (w, h) = buf.get_dimensions(0); - let (w2, h2) = buf.get_dimensions(1); - let tot_h = h + h2; - let hdr = format!("P5\n{} {}\n255\n", w, tot_h); - ofile.write_all(hdr.as_bytes()).unwrap(); - let dta = buf.get_data(); - let ls = buf.get_stride(0); - let mut idx = 0; - let mut idx2 = ls; - let mut pad: Vec = Vec::with_capacity((w - w2 * 2) / 2); - pad.resize((w - w2 * 2) / 2, 0xFF); - for _ in 0..h { - let line = &dta[idx..idx2]; - ofile.write_all(line).unwrap(); - idx += ls; - idx2 += ls; - } - let mut base1 = buf.get_offset(1); - let stride1 = buf.get_stride(1); - let mut base2 = buf.get_offset(2); - let stride2 = buf.get_stride(2); - for _ in 0..h2 { - let bend1 = base1 + w2; - let line = &dta[base1..bend1]; - ofile.write_all(line).unwrap(); - ofile.write_all(pad.as_slice()).unwrap(); - - let bend2 = base2 + w2; - let line = &dta[base2..bend2]; - ofile.write_all(line).unwrap(); - ofile.write_all(pad.as_slice()).unwrap(); - - base1 += stride1; - base2 += stride2; - } - } - - fn write_sound(strno: usize, frmref: NAFrameRef, first: bool) { - let frm = frmref.borrow(); - let name = format!("assets/out{:02}.raw", strno); - let mut file = if first { File::create(name).unwrap() } else { OpenOptions::new().write(true).append(true).open(name).unwrap() }; - let btype = frm.get_buffer(); - let _ = match btype { - NABufferType::AudioU8(ref ab) => file.write_all(ab.get_data().as_ref()), - NABufferType::AudioPacked(ref ab) => file.write_all(ab.get_data().as_ref()), - _ => Ok(()), - }; - } } diff --git a/src/codecs/mod.rs b/src/codecs/mod.rs index 206032e..54dc0f2 100644 --- a/src/codecs/mod.rs +++ b/src/codecs/mod.rs @@ -1,8 +1,3 @@ -#[cfg(feature="decoder_indeo2")] -pub mod indeo2; -#[cfg(feature="decoder_pcm")] -pub mod pcm; - use frame::*; use std::rc::Rc; use std::cell::RefCell; @@ -72,6 +67,30 @@ impl HAMShuffler { } } +#[allow(dead_code)] +struct IPShuffler { + lastframe: Option>, +} + +impl IPShuffler { + #[allow(dead_code)] + fn new() -> Self { IPShuffler { lastframe: None } } + #[allow(dead_code)] + fn clear(&mut self) { self.lastframe = None; } + #[allow(dead_code)] + fn add_frame(&mut self, buf: NAVideoBuffer) { + self.lastframe = Some(buf); + } + #[allow(dead_code)] + fn get_ref(&mut self) -> Option> { + if let Some(ref frm) = self.lastframe { + Some(frm.clone()) + } else { + None + } + } +} + pub trait NADecoder { fn init(&mut self, info: Rc) -> DecoderResult<()>; fn decode(&mut self, pkt: &NAPacket) -> DecoderResult; @@ -83,9 +102,22 @@ pub struct DecoderInfo { get_decoder: fn () -> Box, } +macro_rules! validate { + ($a:expr) => { if !$a { return Err(DecoderError::InvalidData); } }; +} + +#[cfg(feature="decoder_indeo2")] +mod indeo2; +#[cfg(feature="decoder_indeo3")] +mod indeo3; +#[cfg(feature="decoder_pcm")] +mod pcm; + const DECODERS: &[DecoderInfo] = &[ #[cfg(feature="decoder_indeo2")] DecoderInfo { name: "indeo2", get_decoder: indeo2::get_decoder }, +#[cfg(feature="decoder_indeo3")] + DecoderInfo { name: "indeo3", get_decoder: indeo3::get_decoder }, #[cfg(feature="decoder_pcm")] DecoderInfo { name: "pcm", get_decoder: pcm::get_decoder }, ]; @@ -98,3 +130,64 @@ pub fn find_decoder(name: &str) -> Option Box> { } None } + +#[cfg(test)] +use std::fs::{File, OpenOptions}; +#[cfg(test)] +use std::io::prelude::*; + +#[cfg(test)] +fn write_pgmyuv(pfx: &str, strno: usize, num: u64, frmref: NAFrameRef) { + let frm = frmref.borrow(); + let name = format!("assets/{}out{:02}_{:04}.pgm", pfx, strno, num); + let mut ofile = File::create(name).unwrap(); + let buf = frm.get_buffer().get_vbuf().unwrap(); + let (w, h) = buf.get_dimensions(0); + let (w2, h2) = buf.get_dimensions(1); + let tot_h = h + h2; + let hdr = format!("P5\n{} {}\n255\n", w, tot_h); + ofile.write_all(hdr.as_bytes()).unwrap(); + let dta = buf.get_data(); + let ls = buf.get_stride(0); + let mut idx = 0; + let mut idx2 = ls; + let mut pad: Vec = Vec::with_capacity((w - w2 * 2) / 2); + pad.resize((w - w2 * 2) / 2, 0xFF); + for _ in 0..h { + let line = &dta[idx..idx2]; + ofile.write_all(line).unwrap(); + idx += ls; + idx2 += ls; + } + let mut base1 = buf.get_offset(1); + let stride1 = buf.get_stride(1); + let mut base2 = buf.get_offset(2); + let stride2 = buf.get_stride(2); + for _ in 0..h2 { + let bend1 = base1 + w2; + let line = &dta[base1..bend1]; + ofile.write_all(line).unwrap(); + ofile.write_all(pad.as_slice()).unwrap(); + + let bend2 = base2 + w2; + let line = &dta[base2..bend2]; + ofile.write_all(line).unwrap(); + ofile.write_all(pad.as_slice()).unwrap(); + + base1 += stride1; + base2 += stride2; + } +} + +#[cfg(test)] +fn write_sound(pfx: &str, strno: usize, frmref: NAFrameRef, first: bool) { + let frm = frmref.borrow(); + let name = format!("assets/{}out{:02}.raw", pfx, strno); + let mut file = if first { File::create(name).unwrap() } else { OpenOptions::new().write(true).append(true).open(name).unwrap() }; + let btype = frm.get_buffer(); + let _ = match btype { + NABufferType::AudioU8(ref ab) => file.write_all(ab.get_data().as_ref()), + NABufferType::AudioPacked(ref ab) => file.write_all(ab.get_data().as_ref()), + _ => Ok(()), + }; +}