aboutsummaryrefslogtreecommitdiffstats
path: root/nihav-codec-support/src/test
diff options
context:
space:
mode:
authorKostya Shishkov <kostya.shishkov@gmail.com>2020-02-20 11:00:24 +0100
committerKostya Shishkov <kostya.shishkov@gmail.com>2020-02-20 11:00:24 +0100
commitb4d5b8515e75383b4fc59ea2813c90c615d59a96 (patch)
treecf9ea1f458965eea90dff60a607dc90bf42887b3 /nihav-codec-support/src/test
parent2b8bf9a03242bbd6e80091082a50ec13b1a95143 (diff)
downloadnihav-b4d5b8515e75383b4fc59ea2813c90c615d59a96.tar.gz
split nihav-codec-support crate from nihav-core
The former is intended just for NihAV decoders, the latter is for both NihAV crates and for the code using NihAV.
Diffstat (limited to 'nihav-codec-support/src/test')
-rw-r--r--nihav-codec-support/src/test/dec_video.rs536
-rw-r--r--nihav-codec-support/src/test/md5.rs172
-rw-r--r--nihav-codec-support/src/test/mod.rs21
-rw-r--r--nihav-codec-support/src/test/wavwriter.rs122
4 files changed, 851 insertions, 0 deletions
diff --git a/nihav-codec-support/src/test/dec_video.rs b/nihav-codec-support/src/test/dec_video.rs
new file mode 100644
index 0000000..cca4cb4
--- /dev/null
+++ b/nihav-codec-support/src/test/dec_video.rs
@@ -0,0 +1,536 @@
+//! Routines for testing decoders.
+use std::fs::File;
+use std::io::prelude::*;
+use nihav_core::frame::*;
+use nihav_core::codecs::*;
+use nihav_core::demuxers::*;
+//use nihav_core::io::byteio::*;
+use nihav_core::scale::*;
+use super::wavwriter::WavWriter;
+use super::md5::MD5;
+pub use super::ExpectedTestResult;
+
+const OUTPUT_PREFIX: &str = "assets/test_out";
+
+fn write_pgmyuv(pfx: &str, strno: usize, num: u64, frm: NAFrameRef) {
+ if let NABufferType::None = frm.get_buffer() { return; }
+ let name = format!("{}/{}out{:02}_{:06}.pgm", OUTPUT_PREFIX, 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 has_alpha = buf.get_info().get_format().has_alpha();
+ let mut tot_h = h + h2;
+ if has_alpha {
+ tot_h += h;
+ }
+ if w2 > w/2 {
+ tot_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 is_flipped = buf.get_info().is_flipped();
+ if is_flipped {
+ idx += h * ls;
+ idx2 += h * ls;
+ }
+ for _ in 0..h {
+ if is_flipped {
+ idx -= ls;
+ idx2 -= ls;
+ }
+ let line = &dta[idx..idx2];
+ ofile.write_all(line).unwrap();
+ if !is_flipped {
+ idx += ls;
+ idx2 += ls;
+ }
+ }
+ if w2 <= w/2 {
+ let pad: Vec<u8> = vec![0xFF; (w - w2 * 2) / 2];
+ 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);
+ if is_flipped {
+ base1 += h2 * stride1;
+ base2 += h2 * stride2;
+ }
+ for _ in 0..h2 {
+ if is_flipped {
+ base1 -= stride1;
+ base2 -= stride2;
+ }
+ 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();
+
+ if !is_flipped {
+ base1 += stride1;
+ base2 += stride2;
+ }
+ }
+ } else {
+ let pad: Vec<u8> = vec![0xFF; w - w2];
+ let mut base1 = buf.get_offset(1);
+ let stride1 = buf.get_stride(1);
+ if is_flipped {
+ base1 += h2 * stride1;
+ }
+ for _ in 0..h2 {
+ if is_flipped {
+ base1 -= stride1;
+ }
+ let bend1 = base1 + w2;
+ let line = &dta[base1..bend1];
+ ofile.write_all(line).unwrap();
+ ofile.write_all(pad.as_slice()).unwrap();
+ if !is_flipped {
+ base1 += stride1;
+ }
+ }
+ let mut base2 = buf.get_offset(2);
+ let stride2 = buf.get_stride(2);
+ if is_flipped {
+ base2 += h2 * stride2;
+ }
+ for _ in 0..h2 {
+ if is_flipped {
+ base2 -= stride2;
+ }
+ let bend2 = base2 + w2;
+ let line = &dta[base2..bend2];
+ ofile.write_all(line).unwrap();
+ ofile.write_all(pad.as_slice()).unwrap();
+ if !is_flipped {
+ base2 += stride2;
+ }
+ }
+ }
+ if has_alpha {
+ let ls = buf.get_stride(3);
+ let mut idx = buf.get_offset(3);
+ let mut idx2 = idx + w;
+ if is_flipped {
+ idx += h * ls;
+ idx2 += h * ls;
+ }
+ for _ in 0..h {
+ if is_flipped {
+ idx -= ls;
+ idx2 -= ls;
+ }
+ let line = &dta[idx..idx2];
+ ofile.write_all(line).unwrap();
+ if !is_flipped {
+ idx += ls;
+ idx2 += ls;
+ }
+ }
+ }
+}
+
+fn write_palppm(pfx: &str, strno: usize, num: u64, frm: NAFrameRef) {
+ let name = format!("{}/{}out{:02}_{:06}.ppm", OUTPUT_PREFIX, 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 offs: [usize; 3] = [
+ buf.get_info().get_format().get_chromaton(0).unwrap().get_offset() as usize,
+ buf.get_info().get_format().get_chromaton(1).unwrap().get_offset() as usize,
+ buf.get_info().get_format().get_chromaton(2).unwrap().get_offset() as usize
+ ];
+ let mut idx = 0;
+ let mut line: Vec<u8> = vec![0; w * 3];
+ 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 + offs[0]];
+ line[x * 3 + 1] = dta[paloff + pix * 3 + offs[1]];
+ line[x * 3 + 2] = dta[paloff + pix * 3 + offs[2]];
+ }
+ ofile.write_all(line.as_slice()).unwrap();
+ idx += ls;
+ }
+}
+
+fn write_ppm(pfx: &str, strno: usize, num: u64, frm: NAFrameRef) {
+ let name = format!("{}/{}out{:02}_{:06}.ppm", OUTPUT_PREFIX, pfx, strno, num);
+ let mut ofile = File::create(name).unwrap();
+ let info = frm.get_buffer().get_video_info().unwrap();
+ let mut dpic = alloc_video_buffer(NAVideoInfo::new(info.get_width(), info.get_height(), false, RGB24_FORMAT), 0).unwrap();
+ let ifmt = ScaleInfo { width: info.get_width(), height: info.get_height(), fmt: info.get_format() };
+ let ofmt = ScaleInfo { width: info.get_width(), height: info.get_height(), fmt: RGB24_FORMAT };
+ let mut scaler = NAScale::new(ifmt, ofmt).unwrap();
+ scaler.convert(&frm.get_buffer(), &mut dpic).unwrap();
+ let buf = dpic.get_vbuf().unwrap();
+ let (w, h) = buf.get_dimensions(0);
+ let hdr = format!("P6\n{} {}\n255\n", w, h);
+ ofile.write_all(hdr.as_bytes()).unwrap();
+ let dta = buf.get_data();
+ let stride = buf.get_stride(0);
+ for src in dta.chunks(stride) {
+ ofile.write_all(&src[0..w*3]).unwrap();
+ }
+}
+
+/*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)
+}*/
+
+/// Tests decoding of provided file and optionally outputs video frames as PNM (PPM for RGB video, PGM for YUV).
+///
+/// This function expects the following arguments:
+/// * `demuxer` - container format name (used to find proper demuxer for it)
+/// * `name` - input file name
+/// * `limit` - optional PTS value after which decoding is stopped
+/// * `decode_video`/`decode_audio` - flags for enabling video/audio decoding
+/// * `video_pfx` - prefix for video frames written as pictures (if enabled then output picture names should look like `<crate_name>/assets/test_out/PFXout00_000000.ppm`
+/// * `dmx_reg` and `dec_reg` - registered demuxers and decoders that should contain demuxer and decoder(s) needed to decode the provided file.
+///
+/// Since the function is intended for tests, it will panic instead of returning an error.
+pub fn test_file_decoding(demuxer: &str, name: &str, limit: Option<u64>,
+ decode_video: bool, decode_audio: bool,
+ video_pfx: Option<&str>,
+ dmx_reg: &RegisteredDemuxers, dec_reg: &RegisteredDecoders) {
+ let dmx_f = dmx_reg.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 = create_demuxer(dmx_f, &mut br).unwrap();
+
+ let mut decs: Vec<Option<(Box<NADecoderSupport>, Box<dyn NADecoder>)>> = Vec::new();
+ for i in 0..dmx.get_num_streams() {
+ let s = dmx.get_stream(i).unwrap();
+ let info = s.get_info();
+ let decfunc = dec_reg.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)();
+ let mut dsupp = Box::new(NADecoderSupport::new());
+ dec.init(&mut dsupp, info).unwrap();
+ decs.push(Some((dsupp, 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();
+ let streamno = pkt.get_stream().get_id() as usize;
+ if let Some((ref mut dsupp, ref mut dec)) = decs[streamno] {
+ if let (Some(lim), Some(ppts)) = (limit, pkt.get_pts()) {
+ if ppts > lim { break; }
+ }
+ let frm = dec.decode(dsupp, &pkt).unwrap();
+ if pkt.get_stream().get_info().is_video() && video_pfx.is_some() && frm.get_frame_type() != FrameType::Skip {
+ let pfx = video_pfx.unwrap();
+ let pts = if let Some(fpts) = frm.get_pts() { fpts } else { pkt.get_pts().unwrap() };
+ let vinfo = frm.get_buffer().get_video_info().unwrap();
+ if vinfo.get_format().is_paletted() {
+ write_palppm(pfx, streamno, pts, frm);
+ } else if vinfo.get_format().get_model().is_yuv() {
+ write_pgmyuv(pfx, streamno, pts, frm);
+ } else if vinfo.get_format().get_model().is_rgb() {
+ write_ppm(pfx, streamno, pts, frm);
+ } else {
+panic!(" unknown format");
+ }
+ }
+ }
+ }
+}
+
+/// Tests audio decoder with the content in the provided file and optionally outputs decoded audio.
+///
+/// The syntax is very similar to [`test_file_decoding`] except that it is intended for testing audio codecs.
+///
+/// Since the function is intended for tests, it will panic instead of returning an error.
+///
+/// [`test_file_decoding`]: ./fn.test_file_decoding.html
+pub fn test_decode_audio(demuxer: &str, name: &str, limit: Option<u64>, audio_pfx: Option<&str>,
+ dmx_reg: &RegisteredDemuxers, dec_reg: &RegisteredDecoders) {
+ let dmx_f = dmx_reg.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 = create_demuxer(dmx_f, &mut br).unwrap();
+
+ let mut decs: Vec<Option<(Box<NADecoderSupport>, Box<dyn NADecoder>)>> = Vec::new();
+ for i in 0..dmx.get_num_streams() {
+ let s = dmx.get_stream(i).unwrap();
+ let info = s.get_info();
+ let decfunc = dec_reg.find_decoder(info.get_name());
+ if let Some(df) = decfunc {
+ if info.is_audio() {
+ let mut dec = (df)();
+ let mut dsupp = Box::new(NADecoderSupport::new());
+ dec.init(&mut dsupp, info).unwrap();
+ decs.push(Some((dsupp, dec)));
+ } else {
+ decs.push(None);
+ }
+ } else {
+ decs.push(None);
+ }
+ }
+
+ if let Some(audio_pfx) = audio_pfx {
+ let name = format!("{}/{}out.wav", OUTPUT_PREFIX, audio_pfx);
+ let file = File::create(name).unwrap();
+ let mut fw = FileWriter::new_write(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() && pkt.get_pts().unwrap() > limit.unwrap() {
+ break;
+ }
+ let streamno = pkt.get_stream().get_id() as usize;
+ if let Some((ref mut dsupp, ref mut dec)) = decs[streamno] {
+ let frm = dec.decode(dsupp, &pkt).unwrap();
+ 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();
+ }
+ }
+ }
+ } else {
+ 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() && pkt.get_pts().unwrap() > limit.unwrap() {
+ break;
+ }
+ let streamno = pkt.get_stream().get_id() as usize;
+ if let Some((ref mut dsupp, ref mut dec)) = decs[streamno] {
+ let _ = dec.decode(dsupp, &pkt).unwrap();
+ }
+ }
+ }
+}
+
+fn frame_checksum(md5: &mut MD5, frm: NAFrameRef) {
+ match frm.get_buffer() {
+ NABufferType::Video(ref vb) => {
+ md5.update_hash(vb.get_data());
+ },
+ NABufferType::Video16(ref vb) => {
+ let mut samp = [0u8; 2];
+ let data = vb.get_data();
+ for el in data.iter() {
+ samp[0] = (*el >> 8) as u8;
+ samp[1] = (*el >> 0) as u8;
+ md5.update_hash(&samp);
+ }
+ },
+ NABufferType::Video32(ref vb) => {
+ let mut samp = [0u8; 4];
+ let data = vb.get_data();
+ for el in data.iter() {
+ samp[0] = (*el >> 24) as u8;
+ samp[1] = (*el >> 16) as u8;
+ samp[2] = (*el >> 8) as u8;
+ samp[3] = (*el >> 0) as u8;
+ md5.update_hash(&samp);
+ }
+ },
+ NABufferType::VideoPacked(ref vb) => {
+ md5.update_hash(vb.get_data());
+ },
+ NABufferType::AudioU8(ref ab) => {
+ md5.update_hash(ab.get_data());
+ },
+ NABufferType::AudioI16(ref ab) => {
+ let mut samp = [0u8; 2];
+ let data = ab.get_data();
+ for el in data.iter() {
+ samp[0] = (*el >> 8) as u8;
+ samp[1] = (*el >> 0) as u8;
+ md5.update_hash(&samp);
+ }
+ },
+ NABufferType::AudioI32(ref ab) => {
+ let mut samp = [0u8; 4];
+ let data = ab.get_data();
+ for el in data.iter() {
+ samp[0] = (*el >> 24) as u8;
+ samp[1] = (*el >> 16) as u8;
+ samp[2] = (*el >> 8) as u8;
+ samp[3] = (*el >> 0) as u8;
+ md5.update_hash(&samp);
+ }
+ },
+ NABufferType::AudioF32(ref ab) => {
+ let mut samp = [0u8; 4];
+ let data = ab.get_data();
+ for el in data.iter() {
+ let bits = el.to_bits();
+ samp[0] = (bits >> 24) as u8;
+ samp[1] = (bits >> 16) as u8;
+ samp[2] = (bits >> 8) as u8;
+ samp[3] = (bits >> 0) as u8;
+ md5.update_hash(&samp);
+ }
+ },
+ NABufferType::AudioPacked(ref ab) => {
+ md5.update_hash(ab.get_data());
+ },
+ NABufferType::Data(ref db) => {
+ md5.update_hash(db.as_ref());
+ },
+ NABufferType::None => {},
+ };
+}
+
+/// Tests decoder for requested codec in provided file.
+///
+/// This functions tries to decode a stream corresponding to `dec_name` codec in input file and validate the results against expected ones.
+///
+/// Since the function is intended for tests, it will panic instead of returning an error.
+///
+/// # Examples
+///
+/// Test RealVideo 4 decoder in test stream:
+/// ```no_run
+/// use nihav_codec_support::test::ExpectedTestResult;
+/// use nihav_codec_support::test::dec_video::test_decoding;
+/// use nihav_core::codecs::RegisteredDecoders;
+/// use nihav_core::demuxers::RegisteredDemuxers;
+///
+/// let mut dmx_reg = RegisteredDemuxers::new();
+/// let mut dec_reg = RegisteredDecoders::new();
+/// // ... register RealMedia demuxers and RealVideo decoders ...
+/// test_decoding("realmedia", "rv40", "assets/test_file.rmvb", None, &dmx_reg, &dec_reg, ExpectedTestResult::MD5([0x00010203, 0x04050607, 0x08090a0b, 0x0c0d0e0f]));
+/// ```
+pub fn test_decoding(demuxer: &str, dec_name: &str, filename: &str, limit: Option<u64>,
+ dmx_reg: &RegisteredDemuxers, dec_reg: &RegisteredDecoders,
+ test: ExpectedTestResult) {
+ let dmx_f = dmx_reg.find_demuxer(demuxer).unwrap();
+ let mut file = File::open(filename).unwrap();
+ let mut fr = FileReader::new_read(&mut file);
+ let mut br = ByteReader::new(&mut fr);
+ let mut dmx = create_demuxer(dmx_f, &mut br).unwrap();
+
+ let mut decs: Vec<Option<(Box<NADecoderSupport>, Box<dyn NADecoder>)>> = Vec::new();
+ let mut found = false;
+ for i in 0..dmx.get_num_streams() {
+ let s = dmx.get_stream(i).unwrap();
+ let info = s.get_info();
+println!("stream {} codec {} / {}", i, info.get_name(), dec_name);
+ if !found && (info.get_name() == dec_name) {
+ let decfunc = dec_reg.find_decoder(info.get_name());
+ if let Some(df) = decfunc {
+ let mut dec = (df)();
+ let mut dsupp = Box::new(NADecoderSupport::new());
+ dec.init(&mut dsupp, info).unwrap();
+ decs.push(Some((dsupp, dec)));
+ found = true;
+ } else {
+ decs.push(None);
+ }
+ } else {
+ decs.push(None);
+ }
+ }
+
+ let mut md5 = MD5::new();
+ let mut frameiter = if let ExpectedTestResult::MD5Frames(ref vec) = test {
+ Some(vec.iter())
+ } else {
+ None
+ };
+ loop {
+ let pktres = dmx.get_frame();
+ if let Err(e) = pktres {
+ if e == DemuxerError::EOF { break; }
+ panic!("error");
+ }
+ let pkt = pktres.unwrap();
+ let streamno = pkt.get_stream().get_id() as usize;
+ if let Some((ref mut dsupp, ref mut dec)) = decs[streamno] {
+ if limit.is_some() && pkt.get_pts().is_some() && pkt.get_pts().unwrap() > limit.unwrap() {
+ break;
+ }
+ let frm = dec.decode(dsupp, &pkt).unwrap();
+ match &test {
+ ExpectedTestResult::Decodes => {},
+ ExpectedTestResult::MD5(_) => { frame_checksum(&mut md5, frm); },
+ ExpectedTestResult::MD5Frames(_) => {
+ md5 = MD5::new();
+ frame_checksum(&mut md5, frm);
+ md5.finish();
+ if let Some(ref mut iter) = frameiter {
+ let ret = iter.next();
+ if ret.is_none() { break; }
+ let ref_hash = ret.unwrap();
+ let mut hash = [0u32; 4];
+ md5.get_hash(&mut hash);
+println!("frame pts {:?} hash {}", pkt.get_pts(), md5);
+ assert_eq!(&hash, ref_hash);
+ }
+ },
+ ExpectedTestResult::GenerateMD5Frames => {
+ md5 = MD5::new();
+ frame_checksum(&mut md5, frm);
+ md5.finish();
+ let mut hash = [0u32; 4];
+ md5.get_hash(&mut hash);
+println!("frame pts {:?} hash [0x{:08x}, 0x{:08x}, 0x{:08x}, 0x{:08x}],", pkt.get_pts(), hash[0], hash[1], hash[2], hash[3]);
+ },
+ };
+ }
+ }
+ if let ExpectedTestResult::MD5(ref ref_hash) = test {
+ md5.finish();
+ let mut hash = [0u32; 4];
+ md5.get_hash(&mut hash);
+println!("full hash {}", md5);
+ assert_eq!(&hash, ref_hash);
+ }
+ if let ExpectedTestResult::GenerateMD5Frames = test {
+ panic!("generated hashes");
+ }
+}
diff --git a/nihav-codec-support/src/test/md5.rs b/nihav-codec-support/src/test/md5.rs
new file mode 100644
index 0000000..af9a155
--- /dev/null
+++ b/nihav-codec-support/src/test/md5.rs
@@ -0,0 +1,172 @@
+use std::fmt;
+
+const MD5_SHIFTS: [u8; 64] = [
+ 7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22,
+ 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20,
+ 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23,
+ 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21
+];
+
+const MD5_K: [u32; 64] = [
+ 0xD76AA478, 0xE8C7B756, 0x242070DB, 0xC1BDCEEE, 0xF57C0FAF, 0x4787C62A, 0xA8304613, 0xFD469501,
+ 0x698098D8, 0x8B44F7AF, 0xFFFF5BB1, 0x895CD7BE, 0x6B901122, 0xFD987193, 0xA679438E, 0x49B40821,
+ 0xF61E2562, 0xC040B340, 0x265E5A51, 0xE9B6C7AA, 0xD62F105D, 0x02441453, 0xD8A1E681, 0xE7D3FBC8,
+ 0x21E1CDE6, 0xC33707D6, 0xF4D50D87, 0x455A14ED, 0xA9E3E905, 0xFCEFA3F8, 0x676F02D9, 0x8D2A4C8A,
+ 0xFFFA3942, 0x8771F681, 0x6D9D6122, 0xFDE5380C, 0xA4BEEA44, 0x4BDECFA9, 0xF6BB4B60, 0xBEBFBC70,
+ 0x289B7EC6, 0xEAA127FA, 0xD4EF3085, 0x04881D05, 0xD9D4D039, 0xE6DB99E5, 0x1FA27CF8, 0xC4AC5665,
+ 0xF4292244, 0x432AFF97, 0xAB9423A7, 0xFC93A039, 0x655B59C3, 0x8F0CCC92, 0xFFEFF47D, 0x85845DD1,
+ 0x6FA87E4F, 0xFE2CE6E0, 0xA3014314, 0x4E0811A1, 0xF7537E82, 0xBD3AF235, 0x2AD7D2BB, 0xEB86D391
+];
+
+const INITIAL_MD5_HASH: [u32; 4] = [ 0x67452301, 0xEFCDAB89, 0x98BADCFE, 0x10325476 ];
+
+fn box0(b: u32, c: u32, d: u32) -> u32 { (b & c) | (!b & d) }
+fn box1(b: u32, c: u32, d: u32) -> u32 { (d & b) | (!d & c) }
+fn box2(b: u32, c: u32, d: u32) -> u32 { b ^ c ^ d }
+fn box3(b: u32, c: u32, d: u32) -> u32 { c ^ (b | !d) }
+
+#[derive(Clone)]
+pub struct MD5 {
+ pub hash: [u32; 4],
+ inwords: [u32; 16],
+ buf: [u8; 64],
+ pos: usize,
+ count: usize,
+}
+
+impl PartialEq for MD5 {
+ fn eq(&self, other: &Self) -> bool {
+ self.hash == other.hash
+ }
+}
+
+impl Eq for MD5 { }
+
+impl fmt::Display for MD5 {
+ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ write!(f, "{:08x}{:08x}{:08x}{:08x}", self.hash[0].swap_bytes(), self.hash[1].swap_bytes(), self.hash[2].swap_bytes(), self.hash[3].swap_bytes())
+ }
+}
+
+macro_rules! round {
+ ($a: ident, $b: ident, $c: ident, $d: ident, $box: ident, $k: expr, $inval: expr) => {
+ let f = $box($b, $c, $d).wrapping_add($a).wrapping_add(MD5_K[$k]).wrapping_add($inval);
+ $a = $d;
+ $d = $c;
+ $c = $b;
+ $b = $b.wrapping_add(f.rotate_left(MD5_SHIFTS[$k].into()));
+ }
+}
+
+#[allow(dead_code)]
+impl MD5 {
+ pub fn new() -> Self {
+ Self {
+ hash: INITIAL_MD5_HASH,
+ inwords: [0; 16],
+ buf: [0; 64],
+ pos: 0,
+ count: 0,
+ }
+ }
+ fn calc_one_block(&mut self) {
+ let mut a = self.hash[0];
+ let mut b = self.hash[1];
+ let mut c = self.hash[2];
+ let mut d = self.hash[3];
+
+ for (out, src) in self.inwords.iter_mut().zip(self.buf.chunks_exact(4)) {
+ *out = (u32::from(src[0]) << 0) |
+ (u32::from(src[1]) << 8) |
+ (u32::from(src[2]) << 16) |
+ (u32::from(src[3]) << 24);
+ }
+
+ for k in 0..16 { round!(a, b, c, d, box0, k, self.inwords[k]); }
+ for k in 16..32 { round!(a, b, c, d, box1, k, self.inwords[(5 * k + 1) & 0xF]); }
+ for k in 32..48 { round!(a, b, c, d, box2, k, self.inwords[(3 * k + 5) & 0xF]); }
+ for k in 48..64 { round!(a, b, c, d, box3, k, self.inwords[(7 * k) & 0xF]); }
+
+ self.hash[0] = self.hash[0].wrapping_add(a);
+ self.hash[1] = self.hash[1].wrapping_add(b);
+ self.hash[2] = self.hash[2].wrapping_add(c);
+ self.hash[3] = self.hash[3].wrapping_add(d);
+
+ self.pos = 0;
+ }
+ pub fn update_hash(&mut self, src: &[u8]) {
+ for byte in src.iter() {
+ self.buf[self.pos] = *byte;
+ self.pos += 1;
+ if self.pos == 64 {
+ self.calc_one_block();
+ }
+ }
+ self.count += src.len();
+ }
+ pub fn finish(&mut self) {
+ self.buf[self.pos] = 0x80;
+ self.pos += 1;
+ if self.pos > 48 {
+ while self.pos < 64 {
+ self.buf[self.pos] = 0x00;
+ self.pos += 1;
+ }
+ self.calc_one_block();
+ }
+ while self.pos < 64 {
+ self.buf[self.pos] = 0x00;
+ self.pos += 1;
+ }
+ for i in 0..8 {
+ self.buf[56 + i] = ((self.count * 8) >> (i * 8)) as u8;
+ }
+ self.calc_one_block();
+ }
+ pub fn get_hash(&self, dst: &mut [u32; 4]) {
+ for (dst, src) in dst.iter_mut().zip(self.hash.iter()) {
+ *dst = src.swap_bytes();
+ }
+ }
+ pub fn get_hash_bytes(&self, dst: &mut [u8; 4]) {
+ for (dst, src) in dst.chunks_exact_mut(4).zip(self.hash.iter()) {
+ dst[0] = (*src >> 0) as u8;
+ dst[1] = (*src >> 8) as u8;
+ dst[2] = (*src >> 16) as u8;
+ dst[3] = (*src >> 24) as u8;
+ }
+ }
+ pub fn calculate_hash(src: &[u8], hash: &mut [u32; 4]) {
+ let mut md5 = Self::new();
+ md5.update_hash(src);
+ md5.finish();
+ md5.get_hash(hash);
+ }
+}
+
+#[cfg(test)]
+mod test {
+ use super::*;
+
+ #[test]
+ fn test_md5() {
+ let mut hash = [0u32; 4];
+
+ MD5::calculate_hash(&[], &mut hash);
+ assert_eq!(hash, [ 0xD41D8CD9, 0x8F00B204, 0xE9800998, 0xECF8427E ]);
+
+ MD5::calculate_hash(b"abc", &mut hash);
+ assert_eq!(hash, [ 0x90015098, 0x3CD24FB0, 0xD6963F7D, 0x28E17F72 ]);
+
+ MD5::calculate_hash(b"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", &mut hash);
+ assert_eq!(hash, [ 0x8215EF07, 0x96A20BCA, 0xAAE116D3, 0x876C664A ]);
+
+ let mut md5 = MD5::new();
+ for _ in 0..1000000 {
+ md5.update_hash(b"a");
+ }
+ md5.finish();
+ md5.get_hash(&mut hash);
+ assert_eq!(hash, [ 0x7707D6AE, 0x4E027C70, 0xEEA2A935, 0xC2296F21 ]);
+ }
+}
diff --git a/nihav-codec-support/src/test/mod.rs b/nihav-codec-support/src/test/mod.rs
new file mode 100644
index 0000000..367be24
--- /dev/null
+++ b/nihav-codec-support/src/test/mod.rs
@@ -0,0 +1,21 @@
+//! Decoder testing functionality.
+//!
+//! This module provides functions that may be used in internal test to check that decoders produce output and that they produce expected output.
+pub mod dec_video;
+pub mod wavwriter;
+
+mod md5; // for internal checksums only
+
+/// Decoder testing modes.
+///
+/// NihAV has its own MD5 hasher that calculates hash for input frame in a deterministic way (i.e. no endianness issues) and it outputs hash as `[u32; 4]`. During testing the resulting hash will be printed out so when the test fails you can find what was the calculated hash (and use it as a reference if you are implementing a new test).
+pub enum ExpectedTestResult {
+ /// Decoding runs without errors.
+ Decodes,
+ /// Full decoded output is expected to be equal to this MD5 hash.
+ MD5([u32; 4]),
+ /// Each decoded frame hash should be equal to the corresponding MD5 hash.
+ MD5Frames(Vec<[u32; 4]>),
+ /// Test function should report decoded frame hashes to be used as the reference later.
+ GenerateMD5Frames,
+}
diff --git a/nihav-codec-support/src/test/wavwriter.rs b/nihav-codec-support/src/test/wavwriter.rs
new file mode 100644
index 0000000..982fbc3
--- /dev/null
+++ b/nihav-codec-support/src/test/wavwriter.rs
@@ -0,0 +1,122 @@
+//! Audio output in WAV format.
+use nihav_core::io::byteio::*;
+use nihav_core::frame::*;
+use std::io::SeekFrom;
+
+/// WAVE output writer.
+pub struct WavWriter<'a> {
+ io: &'a mut ByteWriter<'a>,
+ data_pos: u64,
+}
+
+fn write_byte(wr: &mut ByteWriter, sample: u8) -> ByteIOResult<()> {
+ wr.write_byte(sample)
+}
+
+fn write_s16(wr: &mut ByteWriter, sample: i16) -> ByteIOResult<()> {
+ wr.write_u16le(sample as u16)
+}
+
+fn write_s32(wr: &mut ByteWriter, sample: i32) -> ByteIOResult<()> {
+ wr.write_u16le((sample >> 16) as u16)
+}
+
+fn write_f32(wr: &mut ByteWriter, sample: f32) -> ByteIOResult<()> {
+ let mut out = (sample * 32768.0) as i32;
+ if out < -32768 { out = -32768; }
+ if out > 32767 { out = 32767; }
+ if out < 0 { out += 65536; }
+ wr.write_u16le(out as u16)
+}
+
+macro_rules! write_data {
+ ($wr:expr, $buf:expr, $write:ident) => ({
+ let len = $buf.get_length();
+ let ainfo = $buf.get_info();
+ let nch = ainfo.get_channels() as usize;
+ let mut offs: Vec<usize> = Vec::with_capacity(nch);
+ for ch in 0..nch { offs.push($buf.get_offset(ch)); }
+ let data = $buf.get_data();
+
+ for i in 0..len {
+ for ch in 0..nch {
+ let sample = data[offs[ch] + i];
+ $write($wr, sample)?;
+ }
+ }
+ })
+}
+
+impl<'a> WavWriter<'a> {
+ /// Constructs a new `WavWriter` instance.
+ pub fn new(io: &'a mut ByteWriter<'a>) -> Self {
+ WavWriter { io, data_pos: 0 }
+ }
+ /// Writes audio format information to the file header.
+ ///
+ /// This function should be called exactly once before writing actual audio data.
+ pub fn write_header(&mut self, ainfo: NAAudioInfo) -> ByteIOResult<()> {
+ let bits = ainfo.get_format().get_bits() as usize;
+
+ self.io.write_buf(b"RIFF")?;
+ self.io.write_u32le(0)?;
+ self.io.write_buf(b"WAVE")?;
+
+ self.io.write_buf(b"fmt ")?;
+ self.io.write_u32le(16)?;
+ self.io.write_u16le(0x0001)?; // PCM
+ self.io.write_u16le(u16::from(ainfo.get_channels()))?;
+ self.io.write_u32le(ainfo.get_sample_rate())?;
+
+ if bits < 16 {
+ self.io.write_u32le(u32::from(ainfo.get_channels()) * ainfo.get_sample_rate())?;
+ self.io.write_u16le(u16::from(ainfo.get_channels()))?; // block align
+ self.io.write_u16le(8)?;
+ } else {
+ self.io.write_u32le(2 * u32::from(ainfo.get_channels()) * ainfo.get_sample_rate())?;
+ self.io.write_u16le(u16::from(2 * ainfo.get_channels()))?; // block align
+ self.io.write_u16le(16)?;
+ }
+
+ self.io.write_buf(b"data")?;
+ self.io.write_u32le(0)?;
+
+ self.data_pos = self.io.tell();
+ Ok(())
+ }
+ /// Writes audio data.
+ pub fn write_frame(&mut self, abuf: NABufferType) -> ByteIOResult<()> {
+ match abuf {
+ NABufferType::AudioU8(ref buf) => {
+ write_data!(self.io, buf, write_byte);
+ }
+ NABufferType::AudioI16(ref buf) => {
+ write_data!(self.io, buf, write_s16);
+ }
+ NABufferType::AudioI32(ref buf) => {
+ write_data!(self.io, buf, write_s32);
+ }
+ NABufferType::AudioF32(ref buf) => {
+ write_data!(self.io, buf, write_f32);
+ }
+ NABufferType::AudioPacked(ref buf) => {
+ self.io.write_buf(buf.get_data().as_slice())?;
+ }
+ _ => {},
+ };
+ Ok(())
+ }
+}
+
+impl<'a> Drop for WavWriter<'a> {
+ #[allow(unused_variables)]
+ fn drop(&mut self) {
+ let size = self.io.tell();
+ if (self.data_pos > 0) && (size >= self.data_pos) {
+ let res = self.io.seek(SeekFrom::Start(4));
+ let res = self.io.write_u32le((size - 8) as u32);
+ let res = self.io.seek(SeekFrom::Start(self.data_pos - 4));
+ let res = self.io.write_u32le(((size as u64) - self.data_pos) as u32);
+ }
+ }
+}