From 379fd78171e79d878c258d9a5fa4c279dcb9b87f Mon Sep 17 00:00:00 2001 From: Kostya Shishkov Date: Sun, 2 Jul 2017 12:37:56 +0200 Subject: [PATCH] factor out decoder test --- src/codecs/gremlinvideo.rs | 49 +-------- src/codecs/h263/intel263.rs | 44 +------- src/codecs/indeo/imc.rs | 70 ++----------- src/codecs/indeo/indeo2.rs | 46 +-------- src/codecs/indeo/indeo3.rs | 54 ++-------- src/codecs/mod.rs | 93 ----------------- src/test/dec_video.rs | 196 ++++++++++++++++++++++++++++++++++++ src/test/mod.rs | 1 + 8 files changed, 217 insertions(+), 336 deletions(-) create mode 100644 src/test/dec_video.rs diff --git a/src/codecs/gremlinvideo.rs b/src/codecs/gremlinvideo.rs index ad326c6..2c0a75d 100644 --- a/src/codecs/gremlinvideo.rs +++ b/src/codecs/gremlinvideo.rs @@ -465,54 +465,9 @@ pub fn get_decoder() -> Box { #[cfg(test)] mod test { - use codecs::*; - use demuxers::*; - use io::byteio::*; - use std::fs::File; - + use test::dec_video::test_file_decoding; #[test] fn test_gdv() { - let gdv_dmx = find_demuxer("gdv").unwrap(); - let mut file = File::open("assets/intro1.gdv").unwrap(); - let mut fr = FileReader::new_read(&mut file); - let mut br = ByteReader::new(&mut fr); - let mut dmx = gdv_dmx.new_demuxer(&mut br); - dmx.open().unwrap(); - - let mut decs: Vec>> = Vec::new(); - for i in 0..dmx.get_num_streams() { - let s = dmx.get_stream(i).unwrap(); - let info = s.get_info(); - let decfunc = find_decoder(info.get_name()); - if !info.is_video() { - decs.push(None); - } else if let Some(df) = decfunc { - let mut dec = (df)(); - dec.init(info).unwrap(); - decs.push(Some(dec)); - } else { -panic!("decoder {} not found", info.get_name()); - } - } - - loop { - let pktres = dmx.get_frame(); - if let Err(e) = pktres { - if e == DemuxerError::EOF { break; } - } - let pkt = pktres.unwrap(); - 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() { -// if frm.borrow().get_frame_type() != FrameType::Skip { -// write_palppm("gdv", streamno, pkt.get_pts().unwrap(), frm); -// } -// } - } - if pkt.get_pts().unwrap() > 8 { break; } - } -//panic!("end"); + test_file_decoding("gdv", "assets/intro1.gdv", Some(10), true, false, None); } } diff --git a/src/codecs/h263/intel263.rs b/src/codecs/h263/intel263.rs index fb9459c..76a1287 100644 --- a/src/codecs/h263/intel263.rs +++ b/src/codecs/h263/intel263.rs @@ -523,49 +523,9 @@ pub fn get_decoder() -> Box { #[cfg(test)] mod test { - use codecs::*; - use demuxers::*; - use io::byteio::*; - + use test::dec_video::test_file_decoding; #[test] fn test_intel263() { - let avi_dmx = find_demuxer("avi").unwrap(); - let mut file = File::open("assets/neal73_saber.avi").unwrap(); - let mut fr = FileReader::new_read(&mut file); - let mut br = ByteReader::new(&mut fr); - let mut dmx = avi_dmx.new_demuxer(&mut br); - dmx.open().unwrap(); - - let mut decs: Vec>> = Vec::new(); - for i in 0..dmx.get_num_streams() { - let s = dmx.get_stream(i).unwrap(); - let info = s.get_info(); - let decfunc = find_decoder(info.get_name()); - if let Some(df) = decfunc { - let mut dec = (df)(); - dec.init(info).unwrap(); - decs.push(Some(dec)); - } else { - decs.push(None); - } - } - - loop { - let pktres = dmx.get_frame(); - if let Err(e) = pktres { - if e == DemuxerError::EOF { break; } - panic!("error"); - } - let pkt = pktres.unwrap(); - if pkt.get_pts().unwrap() > 16 { 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("ih263_", streamno, pkt.get_pts().unwrap(), frm); - } - } - } -//panic!("THE END"); + test_file_decoding("avi", "assets/neal73_saber.avi", Some(16), true, false, None); } } diff --git a/src/codecs/indeo/imc.rs b/src/codecs/indeo/imc.rs index b96b8a7..b6b679f 100644 --- a/src/codecs/indeo/imc.rs +++ b/src/codecs/indeo/imc.rs @@ -1107,69 +1107,15 @@ const IMC_CB_SELECTOR: [[usize; BANDS]; 4] = [ #[cfg(test)] mod test { - use codecs::*; - use demuxers::*; - use io::byteio::*; - use test::wavwriter::WavWriter; - + use test::dec_video::*; #[test] fn test_imc() { - let avi_dmx = find_demuxer("avi").unwrap(); -// let mut file = File::open("assets/neal73_saber.avi").unwrap(); -// let mut file = File::open("assets/IMC/hvalen.avi").unwrap(); -// let mut file = File::open("assets/IMC/8khz.avi").unwrap(); -// let mut file = File::open("assets/STsKlassFist-1a.avi").unwrap(); - let mut file = File::open("assets/IMC/Angel Bday.avi").unwrap(); - let mut fr = FileReader::new_read(&mut file); - let mut br = ByteReader::new(&mut fr); - let mut dmx = avi_dmx.new_demuxer(&mut br); - dmx.open().unwrap(); - - let mut file = File::create("assets/imc-out.wav").unwrap(); - let mut fw = FileWriter::new_write(&mut file); - let mut wr = ByteWriter::new(&mut fw); - let mut wwr = WavWriter::new(&mut wr); - let mut wrote_header = false; - - let mut decs: Vec>> = Vec::new(); - for i in 0..dmx.get_num_streams() { - let s = dmx.get_stream(i).unwrap(); - let info = s.get_info(); - let decfunc = find_decoder(info.get_name()); - if let Some(df) = decfunc { - if info.is_audio() { - let mut dec = (df)(); - dec.init(info).unwrap(); - decs.push(Some(dec)); - } else { - decs.push(None); - } - } else { - decs.push(None); - } - } - - loop { - let pktres = dmx.get_frame(); - if let Err(e) = pktres { - if e == DemuxerError::EOF { break; } - 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(); - let frm = frm_.borrow(); - if frm.get_info().is_audio() { - if !wrote_header { - wwr.write_header(frm.get_info().as_ref().get_properties().get_audio_info().unwrap()).unwrap(); - wrote_header = true; - } - wwr.write_frame(frm.get_buffer()).unwrap(); - } - } - } -panic!("the end"); +// let file = "assets/neal73_saber.avi"; +// let file = "assets/IMC/hvalen.avi"; + let file = "assets/IMC/8khz.avi"; +// let file = "assets/STsKlassFist-1a.avi"; +// let file = "assets/IMC/Angel Bday.avi"; + test_decode_audio("avi", file, None, "imc"); + //test_file_decoding("avi", file, None, false, true, None); } } diff --git a/src/codecs/indeo/indeo2.rs b/src/codecs/indeo/indeo2.rs index 681101f..35201b2 100644 --- a/src/codecs/indeo/indeo2.rs +++ b/src/codecs/indeo/indeo2.rs @@ -367,51 +367,9 @@ pub fn get_decoder() -> Box { #[cfg(test)] mod test { - use codecs::*; - use demuxers::*; - use io::byteio::*; - + use test::dec_video::test_file_decoding; #[test] fn test_indeo2() { - let avi_dmx = find_demuxer("avi").unwrap(); - let mut file = File::open("assets/laser05.avi").unwrap(); - let mut fr = FileReader::new_read(&mut file); - let mut br = ByteReader::new(&mut fr); - let mut dmx = avi_dmx.new_demuxer(&mut br); - dmx.open().unwrap(); - - let mut decs: Vec>> = Vec::new(); - for i in 0..dmx.get_num_streams() { - let s = dmx.get_stream(i).unwrap(); - let info = s.get_info(); - let decfunc = find_decoder(info.get_name()); - if let Some(df) = decfunc { - let mut dec = (df)(); - dec.init(info).unwrap(); - decs.push(Some(dec)); - } else { - decs.push(None); - } - } - - loop { - let pktres = dmx.get_frame(); - if let Err(e) = pktres { - if e == DemuxerError::EOF { break; } - 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("iv2", streamno, pkt.get_pts().unwrap(), frm); -// } else { -// write_sound("iv2", streamno, frm, pkt.get_pts().unwrap() == 0); -// } - } - } + test_file_decoding("avi", "assets/laser05.avi", Some(10), true, false, None); } } diff --git a/src/codecs/indeo/indeo3.rs b/src/codecs/indeo/indeo3.rs index 5851b54..2f8f2b2 100644 --- a/src/codecs/indeo/indeo3.rs +++ b/src/codecs/indeo/indeo3.rs @@ -777,60 +777,18 @@ impl NADecoder for Indeo3Decoder { } } +pub fn get_decoder() -> Box { + Box::new(Indeo3Decoder::new()) +} + #[cfg(test)] mod test { - use codecs::*; - use demuxers::*; - use io::byteio::*; - use std::fs::File; - + use test::dec_video::test_file_decoding; #[test] fn test_indeo3() { - let avi_dmx = find_demuxer("avi").unwrap(); - let mut file = File::open("assets/iv32_example.avi").unwrap(); - let mut fr = FileReader::new_read(&mut file); - let mut br = ByteReader::new(&mut fr); - let mut dmx = avi_dmx.new_demuxer(&mut br); - dmx.open().unwrap(); - - let mut decs: Vec>> = Vec::new(); - for i in 0..dmx.get_num_streams() { - let s = dmx.get_stream(i).unwrap(); - let info = s.get_info(); - let decfunc = find_decoder(info.get_name()); - if let Some(df) = decfunc { - let mut dec = (df)(); - dec.init(info).unwrap(); - decs.push(Some(dec)); - } else { - decs.push(None); -panic!("decoder {} not found", info.get_name()); - } - } - - loop { - let pktres = dmx.get_frame(); - if let Err(e) = pktres { - if e == DemuxerError::EOF { break; } - } - let pkt = pktres.unwrap(); - 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("iv3", streamno, pkt.get_pts().unwrap(), frm); -// } else { -// write_sound("iv3", streamno, frm, pkt.get_pts().unwrap() == 0); -// } - } - if pkt.get_pts().unwrap() > 10 { break; } - } + test_file_decoding("avi", "assets/iv32_example.avi", Some(10), true, false, None); } } -pub fn get_decoder() -> Box { - Box::new(Indeo3Decoder::new()) -} const DT_1_1: IviDeltaCB = IviDeltaCB{ quad_radix: 7, data: &[ 0, 0, 2, 2, -2, -2, -1, 3, diff --git a/src/codecs/mod.rs b/src/codecs/mod.rs index cdbc06b..45005a3 100644 --- a/src/codecs/mod.rs +++ b/src/codecs/mod.rs @@ -152,96 +152,3 @@ pub fn find_decoder(name: &str) -> Option Box> { } None } - -#[cfg(test)] -use std::fs::{File, OpenOptions}; -#[cfg(test)] -use std::io::prelude::*; - -#[cfg(test)] -#[allow(dead_code)] -fn write_pgmyuv(pfx: &str, strno: usize, num: u64, frmref: NAFrameRef) { - let frm = frmref.borrow(); - if let NABufferType::None = frm.get_buffer() { return; } - 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 = w; - 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)] -#[allow(dead_code)] -fn write_palppm(pfx: &str, strno: usize, num: u64, frmref: NAFrameRef) { - let frm = frmref.borrow(); - let name = format!("assets/{}out{:02}_{:04}.ppm", 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 paloff = buf.get_offset(1); - let hdr = format!("P6\n{} {}\n255\n", w, 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 line: Vec = Vec::with_capacity(w * 3); - line.resize(w * 3, 0); - for _ in 0..h { - let src = &dta[idx..(idx+w)]; - for x in 0..w { - let pix = src[x] as usize; - line[x * 3 + 0] = dta[paloff + pix * 3 + 2]; - line[x * 3 + 1] = dta[paloff + pix * 3 + 1]; - line[x * 3 + 2] = dta[paloff + pix * 3 + 0]; - } - ofile.write_all(line.as_slice()).unwrap(); - idx += ls; - } -} - -#[cfg(test)] -#[allow(dead_code)] -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(()), - }; -} diff --git a/src/test/dec_video.rs b/src/test/dec_video.rs new file mode 100644 index 0000000..792d53f --- /dev/null +++ b/src/test/dec_video.rs @@ -0,0 +1,196 @@ +use std::fs::File; +use std::io::prelude::*; +use frame::*; +use codecs::*; +use demuxers::*; +use io::byteio::*; +use super::wavwriter::WavWriter; + +fn write_pgmyuv(pfx: &str, strno: usize, num: u64, frmref: NAFrameRef) { + let frm = frmref.borrow(); + if let NABufferType::None = frm.get_buffer() { return; } + 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 = w; + 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_palppm(pfx: &str, strno: usize, num: u64, frmref: NAFrameRef) { + let frm = frmref.borrow(); + let name = format!("assets/{}out{:02}_{:04}.ppm", 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 paloff = buf.get_offset(1); + let hdr = format!("P6\n{} {}\n255\n", w, 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 line: Vec = Vec::with_capacity(w * 3); + line.resize(w * 3, 0); + for _ in 0..h { + let src = &dta[idx..(idx+w)]; + for x in 0..w { + let pix = src[x] as usize; + line[x * 3 + 0] = dta[paloff + pix * 3 + 2]; + line[x * 3 + 1] = dta[paloff + pix * 3 + 1]; + line[x * 3 + 2] = dta[paloff + pix * 3 + 0]; + } + ofile.write_all(line.as_slice()).unwrap(); + idx += ls; + } +} + +/*fn open_wav_out(pfx: &str, strno: usize) -> WavWriter { + let name = format!("assets/{}out{:02}.wav", pfx, strno); + let mut file = File::create(name).unwrap(); + let mut fw = FileWriter::new_write(&mut file); + let mut wr = ByteWriter::new(&mut fw); + WavWriter::new(&mut wr) +}*/ + +pub fn test_file_decoding(demuxer: &str, name: &str, limit: Option, + decode_video: bool, decode_audio: bool, + video_pfx: Option<&str>) { + let dmx_f = find_demuxer(demuxer).unwrap(); + let mut file = File::open(name).unwrap(); + let mut fr = FileReader::new_read(&mut file); + let mut br = ByteReader::new(&mut fr); + let mut dmx = dmx_f.new_demuxer(&mut br); + dmx.open().unwrap(); + + let mut decs: Vec>> = Vec::new(); + for i in 0..dmx.get_num_streams() { + let s = dmx.get_stream(i).unwrap(); + let info = s.get_info(); + let decfunc = find_decoder(info.get_name()); + if let Some(df) = decfunc { + if (decode_video && info.is_video()) || (decode_audio && info.is_audio()) { + let mut dec = (df)(); + dec.init(info).unwrap(); + decs.push(Some(dec)); + } else { + decs.push(None); + } + } else { + decs.push(None); + } + } + + loop { + let pktres = dmx.get_frame(); + if let Err(e) = pktres { + if e == DemuxerError::EOF { break; } + panic!("error"); + } + let pkt = pktres.unwrap(); + if limit.is_some() && pkt.get_pts().is_some() { + if pkt.get_pts().unwrap() > limit.unwrap() { 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() && video_pfx.is_some() && frm.borrow().get_frame_type() != FrameType::Skip { + let pfx = video_pfx.unwrap(); + if frm.borrow().get_buffer().get_vbuf().unwrap().get_info().get_format().is_paletted() { + write_palppm(pfx, streamno, pkt.get_pts().unwrap(), frm); + } else { + write_pgmyuv(pfx, streamno, pkt.get_pts().unwrap(), frm); + } + } + } + } +} + +pub fn test_decode_audio(demuxer: &str, name: &str, limit: Option, audio_pfx: &str) { + let dmx_f = find_demuxer(demuxer).unwrap(); + let mut file = File::open(name).unwrap(); + let mut fr = FileReader::new_read(&mut file); + let mut br = ByteReader::new(&mut fr); + let mut dmx = dmx_f.new_demuxer(&mut br); + dmx.open().unwrap(); + + let mut decs: Vec>> = Vec::new(); + for i in 0..dmx.get_num_streams() { + let s = dmx.get_stream(i).unwrap(); + let info = s.get_info(); + let decfunc = find_decoder(info.get_name()); + if let Some(df) = decfunc { + if info.is_audio() { + let mut dec = (df)(); + dec.init(info).unwrap(); + decs.push(Some(dec)); + } else { + decs.push(None); + } + } else { + decs.push(None); + } + } + + let name = format!("assets/{}out.wav", audio_pfx); + let mut file = File::create(name).unwrap(); + let mut fw = FileWriter::new_write(&mut file); + let mut wr = ByteWriter::new(&mut fw); + let mut wwr = WavWriter::new(&mut wr); + let mut wrote_header = false; + + loop { + let pktres = dmx.get_frame(); + if let Err(e) = pktres { + if e == DemuxerError::EOF { break; } + panic!("error"); + } + let pkt = pktres.unwrap(); + if limit.is_some() && pkt.get_pts().is_some() { + if pkt.get_pts().unwrap() > limit.unwrap() { break; } + } + let streamno = pkt.get_stream().get_id() as usize; + if let Some(ref mut dec) = decs[streamno] { + let frm_ = dec.decode(&pkt).unwrap(); + let frm = frm_.borrow(); + if frm.get_info().is_audio() { + if !wrote_header { + wwr.write_header(frm.get_info().as_ref().get_properties().get_audio_info().unwrap()).unwrap(); + wrote_header = true; + } + wwr.write_frame(frm.get_buffer()).unwrap(); + } + } + } +} diff --git a/src/test/mod.rs b/src/test/mod.rs index a8fa2dc..aaabd51 100644 --- a/src/test/mod.rs +++ b/src/test/mod.rs @@ -1 +1,2 @@ +pub mod dec_video; pub mod wavwriter; -- 2.39.5